Automatic Fine-Grain Locking using Shape Properties
description
Transcript of 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
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
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
4
Fine-Grain Locking• Each heap object has its own lock
n1
n2 n4
n5 n6n3
… … …
Data Structure
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
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
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
8
Domination Locking Protocol
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
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
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
12
Domination Locking Protocol1. Thread can access object, only when holding its lock
e1 h2 h3exposed
Thread T
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
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
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
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
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
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
19
Automatic Locking for
Dynamic Forests
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
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
22
How does forestness help?
• Guarantees unique ownership at the beginning of every operation
• Helps identifying where to safely release locks
23
Automatic Method
• Adds code that collects runtime information• Adds code that uses this information for
locking and unlocking
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
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
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)
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); } …
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)
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
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
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
32
Thank You