automatic fine-grain locking using shape properties

32
Guy Golan GuetaTel-Aviv University Nathan Bronson Stanford University Alex Aiken Stanford University G. Ramalingam Microsoft Research Mooly Sagiv Tel-Aviv University Eran Yahav Technion Automatic Fine-Grain Locking using Shape Properties 1

Upload: traci

Post on 24-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

Automatic Fine-Grain Locking using Shape Properties. Guy Golan Gueta Tel-Aviv University Nathan Bronson Stanford University Alex Aiken Stanford University G. Ramalingam Microsoft Research Mooly Sagiv Tel-Aviv University Eran Yahav Technion. Concurrent Data Structures. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Automatic Fine-Grain Locking  using  Shape Properties

1

Guy Golan Gueta Tel-Aviv UniversityNathan Bronson Stanford UniversityAlex Aiken Stanford UniversityG. Ramalingam Microsoft ResearchMooly Sagiv Tel-Aviv UniversityEran Yahav Technion

Automatic Fine-Grain Locking using

Shape Properties

Page 2: Automatic Fine-Grain Locking  using  Shape Properties

2

Concurrent Data Structures

• Widely used in many software systems

• Coarse-grain locking – e.g.: a single lock– easy to implement and understand– often not efficient enough

• limited concurrency

• Fine-grain locking– high-degree of concurrency– extremely hard to implement and understand

Page 3: Automatic Fine-Grain Locking  using  Shape Properties

3

Challenge: Automatic Fine-Grain Locking

• Automatically add fine-grain locking to sequential code– recursive data structures (e.g. trees, lists)

int GetMax(Node p) { Node c; c = p.right; while (c!=null) { p=c; c=c.right; } int t = p.value; return t;}

Sequential Codeint GetMax(Node p) { Node c; lock(p); c = p.right; lock(c); while (c!=null) { unlock(p); p=c; c=c.right; if(c!=null) lock(c); } int t = p.value; unlock(p); return t;}

Concurrent Code

Page 4: Automatic Fine-Grain Locking  using  Shape Properties

4

Fine-Grain Locking• Each heap object has its own lock

n1

n2 n4

n5 n6n3

… … …

Data Structure

Page 5: Automatic Fine-Grain Locking  using  Shape Properties

5

n1

n2 n4

n5 n6n3

… … …

Thread TThread T’ Data Structureearly unlock

Fine-Grain Locking• Each heap object has its own lock• A lock is only held while necessary

Page 6: Automatic Fine-Grain Locking  using  Shape Properties

6

Adding fine-grain locking is hard

• Adding fine-grain locking to pointer based data structures is hard

• Need to understand complicated properties • Hard in sequential programs• Harder in the presence of concurrency

Page 7: Automatic Fine-Grain Locking  using  Shape Properties

7

This paper provides two ideas• Locking protocol for fine-grain locking, Domination Locking

– Conditions that guarantees atomicity and deadlock-freedom– Can be enforced by programmers – Requires only sequential reasoning– Generalization of several known locking protocols

• Examples: hand-over-hand locking, dynamic DAG locking

• Automatic method to enforce the protocol– Assumes that the heap is a forest at the beginning of each operation

• Can be checked dynamically– No use of shape analysis– Static + Dynamic: adds conditional lock and unlock statements

Page 8: Automatic Fine-Grain Locking  using  Shape Properties

8

Domination Locking Protocol

Page 9: Automatic Fine-Grain Locking  using  Shape Properties

9

Restricted Objects Access

• Leverage the restricted way well-typed programs access heap objects– Cannot access n3 without accessing n2

n1

n2 n4

n5 n6n3

… … …

Data StructureLocal Stack

X

Page 10: Automatic Fine-Grain Locking  using  Shape Properties

10

Two Types of Objects• Distinguishes between two types of objects:– Exposed objects: “roots” of data structures– Hidden objects: reachable only via exposed objects

n1

n2 n4

n5 n6n3

… … …

exposed

hiddenhidden

hidden hidden hidden

Data StructureLocal Stack

X

Page 11: Automatic Fine-Grain Locking  using  Shape Properties

11

DominationThread T dominates object u:

Paths from exposed objects to u include an object locked by T

e2

h3

exposede1

h4

exposed

h5

Thread T

T dominates h4, h5

T does not dominate h3

Page 12: Automatic Fine-Grain Locking  using  Shape Properties

12

Domination Locking Protocol1. Thread can access object, only when holding its lock

e1 h2 h3exposed

Thread T

Page 13: Automatic Fine-Grain Locking  using  Shape Properties

13

Domination Locking Protocol1. Thread can access object, only when holding its lock 2. Thread can lock hidden object u, only when it dominates u

e1 h2 h3exposed

Thread T

Early unlock is legal Cycle

Page 14: Automatic Fine-Grain Locking  using  Shape Properties

14

Domination Locking Protocol1. Thread can access object, only when holding its lock 2. Thread can lock hidden object u, only when it dominates u

e1 h2 h3 h4exposed

Thread T Heap graph can be dynamically changed

Early unlock is legal Cycle

Page 15: Automatic Fine-Grain Locking  using  Shape Properties

15

Domination Locking Protocol1. Thread can access object, only when holding its lock 2. Thread can lock hidden object u, only when it dominates u3. For exposed objects, use variant of two-phase locking that

avoids deadlocks

e1 h2 h3 h4exposed

Thread T Heap graph can be dynamically changed

Early unlock is legal Cycle

Page 16: Automatic Fine-Grain Locking  using  Shape Properties

16

Domination Locking Protocol1. Thread can access object, only when holding its lock 2. Thread can lock hidden object u, only when it dominates u3. For exposed objects, use variant of two-phase locking that

avoids deadlocks

e1 h2 h3 h4exposed

e5exposed

Several exposed objects

Page 17: Automatic Fine-Grain Locking  using  Shape Properties

17

Concurrent Correctness from Sequential Conditions

• If every sequential execution satisfies the protocol and is able to terminate → all concurrent executions are linearizable and are able to terminate

• Simplifies reasoning • But automatic enforcement in real code with

complicated data structures is still not obvious

Page 18: Automatic Fine-Grain Locking  using  Shape Properties

18

boolean remove(Nd par, int key) { Nd n = null; n = par.right; while (n != null && key != n.key) { par = n; n = (key < n.key) ? n.left : n.right; } if (n == null) return false; Nd nL = n.left; Nd nR = n.right; while (true) { Nd bestChild = (nL == null || (nR != null && nR.prio > nL.prio)) ? nR : nL; if (n == par.left) par.left = bestChild; else par.right = bestChild; if (bestChild == null) break; if (bestChild == nL) { // ROTATION n.left = nL.right; nL.right = n; nL = n.left; } else {// ROTATION n.right = nR.left; nR.left = n; nR = n.right; } par = bestChild; } return true;}

?

Sequential code

Page 19: Automatic Fine-Grain Locking  using  Shape Properties

19

Automatic Locking for

Dynamic Forests

Page 20: Automatic Fine-Grain Locking  using  Shape Properties

20

Dynamic Forest Data Structure

• In every sequential execution, shape is a forest at the beginning and end of operations

• Example:

h7

ListA

ListB

h8h6e5

e1 h2 h3 h4

Page 21: Automatic Fine-Grain Locking  using  Shape Properties

21

Dynamic Forest Data Structure

• In every sequential execution, shape is a forest at the beginning and end of operations

• Example:

h7

ListA

ListB

h8h6e5

e1 h2 h3 h4Forest violation

Move h3 from ListA to ListB

Page 22: Automatic Fine-Grain Locking  using  Shape Properties

22

How does forestness help?

• Guarantees unique ownership at the beginning of every operation

• Helps identifying where to safely release locks

Page 23: Automatic Fine-Grain Locking  using  Shape Properties

23

Automatic Method

• Adds code that collects runtime information• Adds code that uses this information for

locking and unlocking

Page 24: Automatic Fine-Grain Locking  using  Shape Properties

24

Runtime Information

Two reference counters to every heap object

• Stack reference counter – counts number of incoming pointers from local

stack (private memory)• Heap reference counter– counts number of incoming pointers from heap

objects

Page 25: Automatic Fine-Grain Locking  using  Shape Properties

25

Runtime Information

Local Stack

Heap Ref = 0

Stack Ref = 0 Heap Ref = 2

Stack Ref = 0 Heap Ref = 1

Stack Ref = 1

Heap Ref = 0

Stack Ref = 1X

Y

e1 h2 h3

h4

exposed

Page 26: Automatic Fine-Grain Locking  using  Shape Properties

26

Locking and Unlocking

Locking\Unlocking by using the reference counters

• Lock object when – its stack counter becomes positive

• Unlock object when – its stack counter becomes 0– not part of a forest violation (according the heap

counter)

Page 27: Automatic Fine-Grain Locking  using  Shape Properties

27

Multiple Trees

void Move(Tree X, Tree Y) {…

Code that locks pointedobjects in a fixed order

void Move(Tree X, Tree Y) {{ if( address(X) <= address(Y) ) { lock(X); lock(Y); } else { lock(Y); lock(X); } …

Page 28: Automatic Fine-Grain Locking  using  Shape Properties

28

Performance Evaluation

• Add fine grain locking to several data structures– 2 balanced search trees (Treap, Red-Black Tree)– Self Adjusting Heap (Skew Heap)– 2 specialized data structures

(Apriori, and Barnes-Hut)

• Runtime experiments show good scalability– No optimization were used (a lot of room for

optimizations)

Page 29: Automatic Fine-Grain Locking  using  Shape Properties

29

Automatic FGL vs. Manual FGL

1 2 4 6 80

200400600800

10001200140016001800

Single Manual hand-over-hand Automatic

Threads

Thro

ughp

ut (o

ps/m

sec)

Treap

Page 30: Automatic Fine-Grain Locking  using  Shape Properties

30

Automatic FGL vs. Manual FGL

Apriori

1 2 4 80%

20%

40%

60%

80%

100%

120%Original hand-over-hand (manual) Automatic

Threads

Nor

mal

ized

Tim

e

Page 31: Automatic Fine-Grain Locking  using  Shape Properties

31

Summary

• A new fine-grain locking protocol for dynamic heaps – Domination Locking

• Automatic realization for dynamic forests– Adds fine-grain locking for red-black trees and

others– Preliminary Performance Evaluation• scales similarly to hand crafted locking

Page 32: Automatic Fine-Grain Locking  using  Shape Properties

32

Thank You