Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a...

64
Lock Freedom Race Detection Chris Rossbach cs378 Fall 2018 12/5/18

Transcript of Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a...

Page 1: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Lock FreedomRace Detection

Chris Rossbach

cs378 Fall 2018

12/5/18

Page 2: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Pro Forma

• Questions?

• Administrivia:

• Course/Instructor Survey

• Midterm Study materials posted soon

• Lab 5 grading—be clear about Extra Credit pls.

• Projects going OK?

• Agenda

• Non-blocking Sync wrap-up

• Race Detection

• Acknowledgements:

• https://www.cl.cam.ac.uk/teaching/1718/R204/slides-tharris-2-lock-free.pptx

• http://concurrencyfreaks.blogspot.com/2013/05/lock-free-and-wait-free-definition-and.html

• http://swtv.kaist.ac.kr/courses/cs492b-spring-16/lec6-data-race-bug.pptx

• https://www.cs.cmu.edu/~clegoues/docs/static-analysis.pptx

• http://www.cs.sfu.ca/~fedorova/Teaching/CMPT401/Summer2008/Lectures/Lecture8-GlobalClocks.pptx

Page 3: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Race Detection Faux Quiz

Are linearizable objects composable? Why/why not? Is serializable code composable?

What is a data race? What kinds of conditions make them difficult to detect automatically?

What is a consistent cut in a distributed causality interaction graph?

List some tradeoffs between static and dynamic race detection

What are some pros and cons of happens-before analysis for race detection? Same for lockset analysis?

Why might one use a vector clock instead of a logical clock?

What are some advantages and disadvantages of combined lock-set and happens-before analysis?

Page 4: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Non-Blocking Synchronization

Locks: a litany of problems

• Deadlock

• Priority inversion

• Convoys

• Fault Isolation

• Preemption Tolerance

• Performance

Solution: don’t use locks

Page 5: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Lock-free programming

• Subset of a broader class: Non-blocking Synchronization

• Thread-safe access shared mutable state without mutual exclusion

• Possible without HW support• e.g. Lamport’s Concurrent Buffer• …but not really practical wo HW

• Built on atomic instructions like CAS + clever algorithmic tricks

• Lock-free algorithms are hard, so

• General approach: encapsulate lock-free algorithms in data structures• Queue, list, hash-table, skip list, etc.• New LF data structure → research result

Page 6: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Basic List Append

• Is this thread safe?

• What can go wrong?

Page 7: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Example: List Append

• What property do the locks enforce?

• What does the mutual exclusion ensure?

• Can we ensure consistent view (invariants hold) sans mutual exclusion?

• Key insight: allow inconsistent view and fix it up algorithmically

Page 8: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Example: SP-SC Queue

• Single-producer single-consumer

• Why/when does this work?

next(x): if(x == Q_size-1) return 0;else return x+1;

Q_get(data): Q_put(data):t = Q_tail; h = Q_head;while(t == Q_head) while(next(h) == Q_tail); ;

data = Q_buf[t]; Q_buf[h] = data;Q_tail = next(t); Q_head = next(h);

1. Q_head is last write in Q_put, so Q_getnever gets “ahead”.

2. *single* p,c only (as advertised)3. Requires fence before setting Q head4. Devil in the details of “wait”5. No lock → “optimistic”

Page 9: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Lock-Free Stack

• Why does is it work?

• Does it enforce all invariants?

Page 10: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Lock-Free Stack: ABA Problem

Page 11: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

ABA Problem

• Thread 1 observes shared variable → ‘A’

• Thread 1 calculates using that value

• Thread 2 changes variable to B • if Thread 1 wakes up now and tries to CAS, CAS fails and Thread 1 retries

• Instead, Thread 2 changes variable back to A! • Very bad if the variables are pointers

• Anyone see a work-around?• Keep update count → DCAS• Avoid re-using memory• Multi-CAS support → HTM

Page 12: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Correctness: Searching a sorted list

• find(20):

H 10 30 T

20?

find(20) -> false

13

Page 13: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Inserting an item with CAS

• insert(20):

H 10 30 T

20

30 → 20✓

insert(20) -> true

14

Page 14: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Inserting an item with CAS

• insert(20):

H 10 30 T

20

30 → 20

25

30 → 25

• insert(25):

15

Page 15: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Searching and finding together• find(20)

H 10 30 T

-> false

20

20?

• insert(20) -> true

This thread saw 20 was not in the set...

...but this thread succeeded in putting

it in!

• Is this a correct implementation?

• Should the programmer be surprised if this happens?

• What about more complicated mixes of operations?

16

Page 16: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Correctness criteria

17

Informally:

Look at the behaviour of the data structure

• what operations are called on it

• what their results are

If behaviour is indistinguishable from atomic calls to a sequential implementation then the concurrent implementation is correct.

Page 17: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Sequential history

time

T1: in

sert(10

)

-> t

rue

T2: in

sert(20

)

-> t

rue

T1: fin

d(1

5)

-> f

alse

• No overlapping invocations

10 10, 20 10, 20

18

Linearizability: concurrent behaviour should be similar

• even when threads can see intermediate state

• Recall: mutual exclusion precludes overlap

Page 18: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Concurrent history

time

Allow overlapping invocations

Thread 2:

Thread 1:

insert(10)->true insert(20)->true

find(20)->false

19

Linearizability:

• Is there a correct sequential history:

• Same results as the concurrent one

• Consistent with the timing of the invocations/responses?

• Start/end impose ordering constraints

Total Order: 1. Insert(10)2. Find(20)3. Insert(20)• Is consistent with real-time order• 2, 3 overlap, but return order OK

Why is this one OK?

Page 19: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Example: linearizable

time

Thread 2:

Thread 1:

insert(10)->true insert(20)->true

find(20)->falseA valid sequential history: this concurrent execution

is OKNote: linearization point

20

Page 20: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Example: not linearizable

time

Thread 2:

Thread 1:

insert(10)->true insert(10)->false

delete(10)->true

21

Possible Total Orders1. Insert(10)2. Delete(10)3. Insert(10)• Both consistent with real-time order• 1, 2 overlap, but 3 doesn’t

Why is this one NOT OK?

1. Delete(10)2. Insert(10)3. Insert(10)

How can things like this happen?

Page 21: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Example Revisited

• find(20)

H 10 30 T

-> false

20

20?

• insert(20) -> true

Thread 2:

Thread 1:

insert(20)->true

find(20)->false

A valid sequential history: this concurrent execution

is OK because a linearization point exists

22

Recurring Techniques:

• For updates• Perform an essential step of an

operation by a single atomic instruction

• E.g. CAS to insert an item into a list• This forms a “linearization point”

• For reads• Identify a point during the operation’s

execution when the result is valid • Not always a specific instruction

Page 22: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Formal Properties

• Wait-free• A thread finishes its own operation if it continues executing steps

• Strong: everyone eventually finishes

• Lock-free• Some thread finishes its operation if threads continue taking steps

• Weaker: some forward progress guaranateed, but admits unfairness, live-lock, etc.

• Obstruction-free• A thread finishes its own operation if it runs in isolation

• Very weak. Means if you remove contention, someone finishes

Page 23: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Wait-free

• A thread finishes its own operation if it continues executing steps

28

time

Start

Finish

Finish

Start

Finish

Start

Page 24: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Lock-free

• Some thread finishes its operation if threads continue taking steps

time

Start

Start

Finish

Finish

Start

Start

Finish

29

• Red never finishes• Orange does• Still lock-free

Page 25: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Obstruction-free• A thread finishes its own operation if it runs in isolation

• Meaning, if you de-schedule contenders

time

Start

Start

FinishInterference here can prevent

any operation finishing

30

Page 26: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Formal Properties

• Wait-free• A thread finishes its own operation if it continues executing steps

• Strong: everyone eventually finishes

• Lock-free• Some thread finishes its operation if threads continue taking steps

• Weaker: some forward progress guaranateed, but admits unfairness, live-lock, etc.

• Obstruction-free• A thread finishes its own operation if it runs in isolation

• Very weak. Means if you remove contention, someone finishes

Blocking

1. Blocking

2. Starvation-Free

Obstruction-Free

3. Obstruction-Free

Lock-Free

4. Lock-Free (LF)

Wait-Free

5. Wait-Free (WF)

6. Wait-Free Bounded (WFB)

7. Wait-Free Population Oblivious (WFPO)

stronger

Page 27: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

• non-blocking• one method is never forced to wait to sync with another.

• local property: • a system is linearizable iff each individual object is linearizable.

• gives us composability.

• Why is it important? • Serializability is not composable.

Linearizability Properties

32

Huh? Composable?

Page 28: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Composability

• Lock-based code doesn’t compose

• If list were a linearizable concurrent data structure, composition OK

void move(list s, list d, Obj key){

tmp = s.remove(key);

d.insert(key, tmp);

}

T * list::remove(Obj key){

LOCK(this);

tmp = __do_remove(key);

UNLOCK(this);

return tmp;

}

void list::insert(Obj key, T * val){

LOCK(this);

__do_insert(key, val);

UNLOCK(this);

}

void move(list s, list d, Obj key){

LOCK(s);

LOCK(d);

tmp = s.remove(key);

d.insert(key, tmp);

UNLOCK(d);

UNLOCK(s);

}

Thread-safe?

Page 29: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

• non-blocking• one method is never forced to wait to sync with another.

• local property: • a system is linearizable iff each individual object is linearizable.

• gives us composability.

• Why is it important? • Serializability is not composable.

• Core hypotheses: • structuring all as concurrent objects buys composability

• structuring all as concurrent objects is tractable/possible

Linearizability Properties

34

Page 30: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Race Detection

Locks: a litany of problems

• Deadlock

• Priority inversion

• Convoys

• Fault Isolation

• Preemption Tolerance

• Performance

Solution: don’t use locks• non-blocking• Data-structure-centric• HTM• blah, blah, blah..

Use locks!• But automate bug-finding!

Page 31: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Races

1 Lock(lock);

2 Read-Write(X);

3 Unlock(lock);

1

2 Read-Write(X);

3

• Is there a race here?• What is a race?• Informally: accesses with missing/incorrect synchronization• Formally:

• >1 threads access same item• No intervening synchronization• At least one access is a write

How to detect races: forall(X) {

if(not_synchronized(X)) declare_race()

}

Page 32: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Races1 read-write(X);2 fork(thread-proc);3 do_stuff();4 do_more_stuff();5 join(thread-proc);6 read-Write(X);

1 thread-proc() {2 3 read-write(X);4 5 }

Is there a race here?How can a race detector tell?

Unsynchronized access can be

• Benign due to fork/join

• Benign due to view serializability

• Benign due to application-level constraints

• E.g. approximate stats counters

Page 33: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Detecting Races

• Static• Run a tool that analyses just code

• Maybe code is annotated to help

• Conservative: detect races that never occur

• Dynamic• Instrument code

• Check synchronization invariants on accesses

• More precise

• Difficult to make fast

• Lockset vs happens-before

How to detect races: forall(X) {

if(not_synchronized(X)) declare_race()

}

1 Lock(lock);

2 Read-Write(X);

3 Unlock(lock);

1

2 Read-Write(X);

3

Page 34: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Static Data Race Detection

• Type-based analysis • Language type system augmented

• express common synchronization relationships”: correct typing→no data races• Difficult to do • Restricts the type of synchronization primitives

• Language features• e.g., use of monitors• Only works for static data – not dynamic data

• Model Checking• Path analysis

• Doesn’t scale well• Too many false positives

1 Lock(lock);

2 Read-Write(X);

3 Unlock(lock);

1

2 Read-Write(X);

3

What if these *never* run concurrently? (False Positive)

Page 35: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Lockset Algorithm

• Locking discipline• Every shared variable is protected by some locks

• Core idea• Track locks held by thread t

• On access to var v, check if t holds the proper locks

• Challenge: how to know what locks are required?

• Infer protection relation• Infer which locks protect which variable from execution history.

• Assume every lock protects every variable

• On each access, use locks held by thread to narrow that assumption

Narrow down set of locks maybe protecting v

Page 36: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Lockset Algorithm Example

43

lock(lockA);

v++;

unlock(lockA);

lock(lockB);

v++;

unlock(lockB);

{}

{lockA}

{}

{lockB}

{}

{lockA, lockB}

{lockA}

{}

thread t locks_held(t) C(v)

ACK! race

Pretty clever!Why isn’t this

a complete solution?

Page 37: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Improving over lockset

1 read-write(X);2 fork(thread-proc);3 do_stuff();4 do_more_stuff();5 join(thread-proc);6 read-Write(X);

1 thread-proc() {2 3 read-write(X);4 5 }

Lockset detects a raceThere is no race: why not?• A-1 happens before B-3• B-3 happens before A-6• Insight: races occur when “happens-before” cannot be known

thread A thread B

Page 38: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Thread 2

Happens-before

• Happens-before relation• Within single thread

• Between threads

• Accessing variables not ordered by “happens-before” is a race

• Captures locks and dynamism

• How to track “happens-before”?• Sync objects are ordering events

• Generalizes to fork/join, etc

Thread 1 Thread 2

T1 access to V“Happens-before”T2 access to V

Page 39: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Ordering and Causality

A, B, C have local orders

• Want total order• But only for causality

Different types of clocks

• Physical

• Logical• TS(A) later than others A knows about

• Vector • TS(A): what A knows about other TS’s

• Matrix• TS(A) is N^2 showing pairwise

knowledge

Page 40: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

A Naïve Approach

• Each system records each event it performed and its timestamp

• Suppose events in the this system happened in this real order:• Time Tc0: System C sent data to System B (before C stopped

responding)

• Time Ta0: System A asked for work from System B

• Time Tb0: System B asked for data from System C

Tc0 Ta0 Tb0

Page 41: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

A Naïve Approach (cont)

• Ideally, we will construct real order of events from local timestamps and detect this dependency chain:

System A

System B

System C

System C sent data

Tc

Ta

System A asked for work Tb

System B asked for data

Page 42: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

A Naïve Approach (cont)

• But in reality, we do not know if Tc occurred before Ta and Tb, because in an asynchronous distributed system clocks are not synchronized!

System A

System B

System C

System C sent data

Tc

Ta

System A asked for work Tb

System B asked for data

System C sent data

Tc

Page 43: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Rules for Ordering of Events

• local events precede one another → precede one another globally:• If ei

k ,eim Є hi and k < m, then ei

k→eim

• Sending a message always precedes receipt of that message:• If ei = send(m) and ej= receive(m), then ei→ej

• Event ordering is transitive:• If e → e’ and e’ → e”, then e → e”

Page 44: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Space-time Diagram for Distributed Computation

p1e1

1 e12 e1

3 e14 e1

5 e16

p2e2

1 e22 e2

3

p3e3

1 e32 e3

3 e34 e3

5 e36

e21→e3

6 e22 || e3

6

local events precede one another → precede one another globally:

If eik ,ei

m Є hi and k < m, then eik→ei

m

Sending a message always precedes receipt of that message:

If ei = send(m) and ej= receive(m), then ei→ejEvent ordering is associative:

If e → e’ and e’ → e”, then e → e”

Page 45: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Cuts of a Distributed Computation

• Suppose there is an external monitor process

• External monitor constructs a global state:• Asks processes to send it local history

• Global state constructed from these local histories is:

a cut of a distributed computation

Page 46: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Example Cuts

e11 e1

2 e13 e1

4 e15 e1

6

e21 e2

2 e23

e31 e3

2 e33 e3

4 e35 e3

6

C C’

p1

p2

p3

Page 47: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Consistent vs. Inconsistent Cuts

• A cut is consistent if • for any event e included in the cut

• any event e’ that causally precedes e is also included in that cut

• For cut C:(e Є C) Λ (e’→ e) => e’ Є C

Page 48: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Are These Cuts Consistent?

e11 e1

2 e13 e1

4 e15 e1

6

e21 e2

2 e23

e31 e3

2 e33 e3

4 e35 e3

6

C

p1

p2

p3

Page 49: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Are These Cuts Consistent?

e11 e1

2 e13 e1

4 e15 e1

6

e21 e2

2 e23

e31 e3

2 e33 e3

4 e35 e3

6

C’

inconsistentincluded

in C

causally precedes e3

6

…but not included

in C

A consistent cut corresponds to a consistent global state

Page 50: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

What Do We Need to Know to Construct a Consistent Cut?

e11 e1

2 e13 e1

4 e15 e1

6

e21 e2

2 e23

e31 e3

2 e33 e3

4 e35 e3

6

C’

inconsistentincluded

in C

causally precedes e3

6

…but not included

in CWe must know the causal ordering of events. If we

do we can detect an inconsistent cut

Page 51: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Logical Clocks

• Each process maintains a local value of a logical clock LC

• Logical clock of process p counts how many events in a distributed computation causally preceded the current event at p (including the current event).

• LC(ei) – the logical clock value at process pi at event ei

• Suppose we had a distributed system with only a single process

e11 e1

2 e13 e1

4 e15 e1

6

LC=1 LC=2 LC=3 LC=4 LC=5 LC=6

Page 52: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Logical Clocks (cont.)

• In a system with more than one process logical clocks are updated as follows:

• Each message m that is sent contains a timestamp TS(m)

• TS(m) is the logical clock value associated with sending event at the sending process

e11 e1

2 e13 e1

4 e15 e1

6

LC=1 send(m)

TS(m) = 1

Page 53: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Logical Clocks (cont)

• When the receiving process receives message m, it updates its logical clock to:

max{LC, TS(m)} + 1

e11 e1

2 e13 e1

4 e15 e1

6

LC=1 send(m) TS(m) = 1

e22

What is the LC value of e2

2?

LC=2

e21

LC=1

Page 54: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Illustration of a Logical Clock

1 2 4 5 6 7

1 5 6

1 2 3 4 5 7

3\p1

p1

p1

Awesome, right?Any drawbacks?

Total vs Partial Order

Page 55: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Vector Clock

Replace Single Logical value with Vector!

Vi[i] : #events occurred at i

Vi[j] : #events i knows occurred at j

Update

• On local-event: increment Vi[I]

• On send-message: increment, piggyback entire local vector V

• On recv-message: Vj[k] = max( Vj[k],Vi[k] )

• Vj[i] = Vj[i]+1 (increment local clock)

• Receiver learns about number of events sender knows occurred elsewhere

Page 56: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Vector Clock Example

Each process i maintains a vector Vi

• Vi[i] : number of events that have occurred at i

• Vi[j] : number of events I knows have occurred at process j

Update

• Local event: increment Vi[I]

• Send a message :piggyback entire vector V

• Receipt of a message: Vj[k] = max( Vj[k],Vi[k] )

• Receiver is told about how many events the sender knows occurred at another process k

• Also Vj[i] = Vj[i]+1

• Need to order operations• Can’t rely on real-time• Vector clock: timestamping algorithm s.t.

• TS(A) < TS(B) → A happens before B• Independent ops remain unordered

See any drawbacks?

Page 57: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Thread 2

Happens-before

• Happens-before relation• Within single thread

• Between threads

• Accessing variables not ordered by “happens-before” is a race

• Captures locks and dynamism

• How to track “happens-before”?• Sync objects are ordering events

• Generalizes to fork/join, etc

Thread 1 Thread 2

T1 access to V“Happens-before”T2 access to V

Page 58: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Flaws of Happens-before

• Difficult to implement• Requires per-thread information

• Dependent on the interleaving produced by the scheduler

• Example• T1-acc(v) happens before T2-acc(v)• T1-acc(y) happens before T1-acc(v)• T2-acc(v) happens before T2-acc(y)• Conclusion: no race on Y!• Finding doesn’t generalize

y := y+1;

Lock(mu);

v := v+1;

Unlock(mu);

Lock(mu);

v := v+1;

Unlock(mu);

y := y+1;

Thread 1

Thread 2

y := y+1;

Lock(mu);

v := v+1;

Unlock(mu);

Thread 1

Lock(mu);

v := v+1;

Unlock(mu);

y := y+1;

Thread 2

Page 59: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Dynamic Race Detection Summary

⚫ Lockset: verify locking discipline for shared memory✓Detect race regardless of thread scheduling

False positives because other synchronization primitives (fork/join, signal/wait) not supported

⚫ Happens-before: track partial order of program events✓ Supports general synchronization primitives

Higher overhead compared to lockset

False negatives due to sensitivity to thread scheduling

RaceTrack = Lockset + Happens-before

Page 60: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

False positive using Lockset

Inst State Lockset

1 Virgin { }

3 Exclusive:t { }

6 Shared Modified {a}

9 Report race { }

Tracking accesses to X

Page 61: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

RaceTrack Notations

Notation Meaning

Lt

Lockset of thread t

Cx

Lockset of memory x

Bu

Vector clock of thread u

Sx

Threadset of memory x

ti

Thread t at clock time i

Page 62: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

RaceTrack Algorithm

Notation Meaning

Lt

Lockset of thread t

Cx

Lockset of memory x

Bt

Vector clock of thread t

Sx

Threadset of memory x

t1

Thread t at clock time 1

Page 63: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Avoiding Lockset's false positive (1)

Inst Cx

Sx

Lt

Bt

Lu

Bu

0 All { } { } {t1} - -

1 {t2} { } { t

1,u

1 }

2 {a}

3 {a} {t2}

4 { }

5 {a}

6 {t2,u

1}

7 { }

8 {t2,u

1} - -

Notation Meaning

Lt

Lockset of thread t

Cx

Lockset of memory x

Bt

Vector clock of thread t

Sx

Threadset of memory x

t1

Thread t at clock time 1

Page 64: Lock Freedom Race Detection - University of Texas at AustinNon-Blocking Synchronization Locks: a litany of problems •Deadlock •Priority inversion •Convoys •Fault Isolation

Avoiding Lockset's false positive (2)

Inst Cx

Sx

Lt

Bt

Lv

Bv

8 {a} {t2,u

1} { } {t

2,u

1} - -

9 { } {t2}

10 {t3,u

1} { } {t

2,v

1}

11 {a}

12 {a} {t3}

13 { }

14 {a}

15 {t3,v

1}

16 { }

Notation Meaning

Lt

Lockset of thread t

Cx

Lockset of memory x

Bt

Vector clock of thread t

Sx

Threadset of memory x

t1

Thread t at clock time 1

Only one thread!Are we done?