Semantic Atomicity for Multithreaded Programs Jacob Burnim, George - - PowerPoint PPT Presentation

semantic atomicity for multithreaded programs
SMART_READER_LITE
LIVE PREVIEW

Semantic Atomicity for Multithreaded Programs Jacob Burnim, George - - PowerPoint PPT Presentation

EECS EECS Electrical Engineering and Electrical Engineering and Computer Sciences Computer Sciences B ERKELEY P AR L AB B ERKELEY P AR L AB P A R A L L E L C O M P U T I N G L A


slide-1
SLIDE 1

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

P A R A L L E L C O M P U T I N G L A B O R A T O R Y

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Semantic Atomicity for Multithreaded Programs

Jacob Burnim, George Necula, Koushik Sen Parallel Computing Laboratory University of California, Berkeley

slide-2
SLIDE 2

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Parallel Correctness is Hard

! Difficult to write correct parallel software.

" Key: Interference between parallel threads. " Atomicity – freedom from harmful interference; a fundamental parallel correctness property.

! Today: Semantic atomicity.

" Specifying atomicity with respect to user- defined, semantic equivalence. " Efficiently testing such specifications. " Overall Goal: Lightweight, useful specs to help programmers find and fix parallelism bugs.

2

slide-3
SLIDE 3

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Outline

! Overview + Motivation ! Background: Atomicity ! Specifying Semantic Atomicity ! Testing Semantic Atomicity ! Experimental Evaluation ! Conclusion

3

slide-4
SLIDE 4

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Background: Atomicity

! Atomicity a non-interference property.

" Block of code is atomic if it behaves as if executed all-at-once and without interruption. " Interference from other threads is benign – cannot change overall program behavior.

4

slide-5
SLIDE 5

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Background: Atomicity

! Atomicity a non-interference property.

" Block of code is atomic if it behaves as if executed all-at-once and without interruption.

5

Want to check specification.

  • Is the code actually atomic?
  • int bal = 0;

deposit(int a) { @atomic { int t = bal; bal = t + a; } }

Atomic specification.

  • Programmer intends

that this code is atomic.

slide-6
SLIDE 6

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Background: Atomicity

! Atomicity a non-interference property.

" Block of code is atomic if it behaves as if executed all-at-once and without interruption.

6

int bal = 0; deposit(int a) { @atomic { int t = bal; bal = t + a; } }

Thread 1: Thread 2:

t = 0 bal = 10 deposit(10) t = 0 bal = 5 deposit(5)

Atomicity specification does not hold.

slide-7
SLIDE 7

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Background: Atomicity

! Atomicity a non-interference property.

" Block of code is atomic if it behaves as if executed all-at-once and without interruption.


7

int bal = 0; deposit(int a) { @atomic { int t = bal; while (!CAS(&bal, t, t+a)) t = bal; } }

Atomicity specification does hold.

  • With CAS, updates to

balance are atomic.

slide-8
SLIDE 8

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

! Formally: Two semantics for a program


P with specified atomic blocks.

Background: Atomicity

8

Initial
 State s0

  • Final


State s1’ serial execution E Initial
 State s0

  • Final


State s1 interleaved execution E

" Interleaved: Threads interleave normally. " Serial: When one thread opens an atomic block, no other thread runs until it closes.

slide-9
SLIDE 9

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Background: Atomicity

! Formally, program P is atomic iff:

" For all interleaved executions E yielding s1,
 there exists a serial E’ yielding an identical final state.

9

Initial
 State s0

  • Final


State s1 Final
 State s1’ ∃ serial execution

∀ interleaved executions

q.equals(q’)

s1 == s1’

slide-10
SLIDE 10

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Outline

! Overview ! Background: Atomicity ! Specifying Semantic Atomicity ! Testing Semantic Atomicity ! Experimental Evaluation ! Conclusion

10

slide-11
SLIDE 11

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Motivating Example

! Michael & Scott non-blocking queue, in


the Java standard library

! Internally, a linked list with lazy deletion.

11

ConcurrentLinkedQueue q; q.add(1); q.add(1); Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } }

slide-12
SLIDE 12

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Motivating Example

! In any serial execution:

12

Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } } head: null

1 1

head: null

1

head: null remove(1) remove(1)

slide-13
SLIDE 13

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Motivating Example

! But in an interleaved execution:

13

Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } } head: null

1 1

head: null null remove(1) remove(1)

slide-14
SLIDE 14

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Motivating Example

! Traditional atomicity requires:

14

Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } } Initial
 State s0

  • Final


State s1 Final
 State s1’ ∃ serial execution

∀ interleaved executions

q.equals(q’)

s1 == s1’

null null

slide-15
SLIDE 15

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Motivating Example

! Traditional atomicity requires:

15

Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } } Initial
 State s0

  • Final


State s1 Final
 State s1’ q.equals(q’)

s1 == s1’

∃ serial execution

∀ interleaved executions

!

null null null

slide-16
SLIDE 16

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Motivating Example

! Traditional atomicity requires:

16

Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } } Initial
 State s0

  • Final


State s1 Final
 State s1’ q.equals(q’)

s1 == s1’

∃ serial execution

∀ interleaved executions

!

Replace with user-defined
 semantic equivalence.

null null null

slide-17
SLIDE 17

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Semantic Atomicity

17

Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } } Initial
 State s0

  • Final


State s1 Final
 State s1’ q.equals(q’) ∃ serial execution

∀ interleaved executions

null null null

"(s

1, #

s

1)

Replace with user-defined
 semantic equivalence.

slide-18
SLIDE 18

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Semantic Atomicity Example

18

Initial
 State s0

  • Final


State s1 Final
 State s1’ ∃ serial execution

∀ interleaved executions

q.equals(q’) Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } } Atomicity predicate: q.equals(q’)

"(s

1, #

s

1)

slide-19
SLIDE 19

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

! Burnim, Sen, “Asserting and Checking Determinism for

Multithreaded Programs”, FSE 2009, CACM 2010.

Bridge Predicates

19

Thread 1: Thread 2: @atomic { @atomic { q.remove(1); q.remove(1); } } Atomicity predicate: q.equals(q’) Bridge predicate.

slide-20
SLIDE 20

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

! Semantic Atomicity: ! Semantic Determinism:

Atomicity vs. Determinism

20

Initial
 State s0

  • Final


State s1 ` Final
 State s1’ ∃ serial execution

∀ interleaved executions

"(s

1, #

s

1) Final
 State s1 ` Final
 State s1’

∀ interleaved executions

"(s

1, #

s

1) Initial
 State s0

  • ∀ interleaved executions
slide-21
SLIDE 21

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Semantic Atomicity Example

21

int bal = 0; int conflicts = 0; deposit(int a) { @atomic { int t = bal; while (!CAS(&bal, t, t+a)) { t = bal; conflicts += 1; } } } Atomicity predicate: bal == bal’

With CAS, updates to balance are atomic.

  • “Performance counter”
  • f # of CAS failures.
slide-22
SLIDE 22

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Semantic Atomicity Example

! If list is [1,3,2,4], an atomicity violation?

" User must specify intended atomicity.

22

ConcurrentList list; Thread 1: Thread 2: @atomic { @atomic {

... ...

list.add(1); list.add(3);

... ...

list.add(2); list.add(4); } } Atomicity predicate: eqSets(list,list’)

slide-23
SLIDE 23

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Outline

! Introduction + Motivation ! Background: Atomicity ! Specifying Semantic Atomicity ! Testing Semantic Atomicity ! Experimental Evaluation ! Conclusion

23

slide-24
SLIDE 24

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Testing Semantic Atomicity

24

E A A B B C D D E

E : s0 s

1

" E E

"

"(s

1, #

s

1)

E

" E

! Interleaved run is semantically atomic

w.r.t. iff there exists a serial run s.t.:

" The final states of , satisfy .

Is semantically atomic w/ respect to ?

  • E

"

slide-25
SLIDE 25

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Testing Semantic Atomicity

25

E A A B B C D D E

E : s0 s

1

A B C D E

" E : s0 " s

1

" E E

"

"(s

1, #

s

1)

E

" E

! Interleaved run is semantically atomic

w.r.t. iff there exists a serial run s.t.:

" The final states of , satisfy .

A B C D E

" E : s0 " s

1 "(s

1, #

s

1)?

"(s

1, #

s

1)?

" E : s0 " s

1

A B F D G H

"(s

1, #

s

1)?

slide-26
SLIDE 26

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

! Interleaved run is semantically atomic

w.r.t. iff there exists a serial run s.t.:

" The final states of , satisfy .

Testing Semantic Atomicity

26

E A A B B C D D E

E : s0 s

1

A B C D E

" E : s0 " s

1

" E E

"

"(s

1, #

s

1)

E

" E

A B C D E

" E : s0 " s

1

" E : s0 " s

1

A B F D G H

Infeasible to try all serial executions.

  • Can we restrict this search?
slide-27
SLIDE 27

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Testing Semantic Atomicity

27

E A A B B C D D E

E : s0 s

1

A B C D E

" E : s0 " s

1

A B C D E

" E : s0 " s

1

" E : s0 " s

1

A B F D G H

  • 1. The final states of , satisfy .
  • 2. and execute the same atomic blocks.

"(s

1, #

s

1)

E

" E

E

" E

!

slide-28
SLIDE 28

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Testing Semantic Atomicity

28

E A A B B C D D E

E : s0 s

1

A B C D E

" E : s0 " s

1

A B C D E

" E : s0 " s

1

" E : s0 " s

1

A B F D G H

  • 1. The final states of , satisfy .
  • 2. and execute the same atomic blocks.
  • 3. Non-overlapping atomic blocks appear in


the same order in and .

"(s

1, #

s

1)

E

" E

E

" E

E

" E

! !

slide-29
SLIDE 29

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Semantic Serializability

29

E A A B B C D D E

E : s0 s

1

A B C D E

" E : s0 " s

1

A B C D E

" E : s0 " s

1

" E : s0 " s

1

A B F D G H

!

! Def: Interleaved run is semantically

serializable iff exists a serial run s.t.:

  • 1. The final states of , satisfy .
  • 2. and execute the same atomic blocks.

E " E

"(s

1, #

s

1)

"(s

1, #

s

1)

E

" E

E

" E

slide-30
SLIDE 30

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Semantic Strict Serializability

30

! Def: Interleaved is semantically strictly

serializable iff exists a serial run s.t.:

  • 1. The final states of , satisfy .
  • 2. and execute the same atomic blocks.
  • 3. Non-overlapping atomic blocks appear in


the same order in and .

E " E

"(s

1, #

s

1)

E

" E

E

" E

E

" E

E A A B B C D D E

E : s0 s

1

A B C D E

" E : s0 " s

1

A B C D E

" E : s0 " s

1

!

slide-31
SLIDE 31

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

31

! Def: Interleaved is semantically strictly

serializable iff exists a serial run s.t.:

  • 1. The final states of , satisfy .
  • 2. and execute the same atomic blocks.
  • 3. Non-overlapping atomic blocks appear in


the same order in and .

E " E

"(s

1, #

s

1)

E

" E

E

" E

E

" E

Semantic Strict Serializability

E has N blocks, with ≤ K overlapping.

  • ==>
  • Can check semantic strict serializability


by examining ≤ K! serial runs.

slide-32
SLIDE 32

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Testing Semantic Atomicity

! To test atomicity of program P:

" Systematically/randomly generate executions E with ≤ K overlapping atomic blocks. " For each E, report a violation if not semantically strictly serializable.

! Small Scope Hypothesis: Can find bugs

with small # of overlapping atomic blocks.

32

slide-33
SLIDE 33

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Outline

! Introduction + Motivation ! Background: Atomicity ! Specifying Semantic Atomicity ! Testing Semantic Atomicity ! Experimental Evaluation ! Conclusion

33

slide-34
SLIDE 34

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Experimental Evaluation

! Wrote semantic atomicity specs for


several Java benchmarks.

" Concurrent data structures and parallel apps.

! Setup: For each benchmark:

" Generate 200-900 random interleaved runs, with one atomic block interrupted by ≤ 4 others.

  • " Check semantic strict serializability of each.

" To compare, also check conflict-serializability.

34

slide-35
SLIDE 35

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Experimental Results I

Benchmark LoC Test Runs Semantic Atomicity Violations Strict Atomicity Violations

Runs Static Blocks Runs Static Blocks

JDK LinkedQueue 200 241 7 2

≫7

4 JDK SkipListMap 1400 487 6 2

≫7

4 JDK CwArrayList 600 222 lock-free list 100 319 57 1

≫57

2 lazy list-based set 100 231

≫0

2

35

slide-36
SLIDE 36

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Experimental Results II

Benchmark LoC Test Runs Semantic Atomicity Violations Strict Atomicity Violations

Runs Static Blocks Runs Static Blocks

PJ pi 150 20 5 1

5

1 PJ keysearch 200 904 PJ fractal 250 73 PJ phylogenetic 4400 603 27 1

≫27

2

36

Application benchmarks from Parallel Java Library
 (Kaminsky 2007), use ~15000 LoC from PJ library.

slide-37
SLIDE 37

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

JDK Atomicity Bug

! Not atomic: q.size() can return sz=3.

37

ConcurrentLinkedQueue q; q.add(1); q.add(2); Thread 1: Thread 2: @atomic { q.remove(1); @atomic { } sz = q.size(); @atomic { } q.add(3); } Atomic with respect to: q.equals(q’) ∧ (sz == sz’)

slide-38
SLIDE 38

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Semantic Atomicity Bug II

38

parallel-for (t in trees) { @atomic { cost = compute_cost(t); synchronized (min_cost) { min_cost = min(min_cost, cost); } if (cost == min_cost) { min_tree = t; } } } Atomic with respect to: min_tree.equals(min_tree’)

∧ (min_cost == min_cost’) Updates to min_tree not synchronized.

  • Updates to

min_tree not synchronized.

slide-39
SLIDE 39

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Outline

! Introduction + Motivation ! Background: Atomicity ! Specifying Semantic Atomicity ! Testing Semantic Atomicity ! Experimental Evaluation ! Conclusion

39

slide-40
SLIDE 40

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Conclusion

! Semantic atomicity.

" Generalization for capturing high-level non- interference properties of real, complex code. " Testing via strict serializability. " Found several unknown atomicity errors.

! Overall Goal: Lightweight specifications

for parallel correctness.

" Easy for programmers to write. " With testing, effective in finding real bugs. " Determinism [CACMʼ10,ICSEʻ10], NDSeq [PLDI ʻ11]

  • 40
slide-41
SLIDE 41

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

P A R A L L E L C O M P U T I N G L A B O R A T O R Y

EECS

Electrical Engineering and Computer Sciences

BERKELEY PAR LAB

Questions?

  • 41