concurrent garbage collection presented by roman kecher gc seminar, tel-aviv university 23-dec-141

71
Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-14 1

Upload: eugene-morgan

Post on 20-Jan-2016

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

1

Concurrent Garbage Collection

Presented by Roman Kecher

GC Seminar, Tel-Aviv University

23-Dec-14

Page 2: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

2

Outline General concurrent GC concepts

Incremental, mostly concurrent, “on-the-fly” collections

The lost object problem Tricolor invariants

Barrier techniques for concurrent GC Complete enumeration of techniques Efficient implementation through card tables

Summary

23-Dec-14

Page 3: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

3

Outline General concurrent GC concepts

Incremental, mostly concurrent, “on-the-fly” collections

23-Dec-14

Page 4: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

4

Concurrent GC A concurrent GC is essentially a collector

running in parallel to the mutator (in different threads) It may stop the mutator at some points, to

synchronize etc

Different from what we have seen by now A collector must be able to operate incrementally

We will describe a number of different collection types Suffering from similar limitations Eventually leading to the concurrent collection23-Dec-14

Page 5: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

5

Incremental collection First, on a Uniprocessor

Time

23-Dec-14

Page 6: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

6

Incremental collection On a Multiprocessor:

23-Dec-14

Page 7: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

7

Incremental collection On a Multiprocessor:

Can also be parallelized:

23-Dec-14

Page 8: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

8

Mostly concurrent collection

23-Dec-14

Page 9: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

9

Mostly concurrent collection

Can also be incremental:

23-Dec-14

Page 10: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

10

Concurrent (“on-the-fly”) collection

23-Dec-14

Page 11: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

11

Concurrent (“on-the-fly”) collection

Can also be incremental:

23-Dec-14

Page 12: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

12

Correctness of concurrent collection Safety

Retains at least all reachable objects May leave “floating garbage”

Liveness Collector must eventually complete its collection

cycle

23-Dec-14

Page 13: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

13

Atomicity Each collector and mutator operation will be

specified as operating atomically Without loss of generality, can consider only a

single mutator and a single collector

Actual implementation of the concurrency control Left to the discretion of the implementer Possibilities discussed in the “Concurrency

Preliminaries” chapter

23-Dec-14

Page 14: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

14

Revisiting the tricolor abstraction White objects

Have not (yet) been reached by the collector Considered garbage at the end

Grey objects Have been reached but not yet fully processed

(may still point to white objects)

Black objects Have been fully processed (don’t point to white

objects immediately after the scan)

23-Dec-14

Page 15: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

15

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 16: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

16

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 17: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

17

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 18: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

18

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 19: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

19

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 20: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

20

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 21: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

21

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 22: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

22

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 23: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

23

Tricolor abstraction and grey wavefront

Roots

23-Dec-14

Page 24: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

24

The grey wavefront The boundary between black and white

objects

Works great when there is no interleaving of collector and mutator

Recall the definition of a write operation: atomic Write(src, i, new):

old src[i]src[i] new

23-Dec-14

Page 25: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

25

Outline General concurrent GC concepts

Incremental, mostly concurrent, “on-the-fly” collections

The lost object problem

23-Dec-14

Page 26: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

26

The lost object problem Mutator can insert an object behind the

wavefront A reference to a white object is taken (ahead of

the wavefront) Moved to a black object (behind the wavefront) Original path(s) to it deleted

Two examples: Direct

The link to the object itself is deleted Transitive

A link on the path to the object is deleted

23-Dec-14

Page 27: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

27

Hiding a reachable object – direct

Roots

X Y

a

Z

23-Dec-14

Page 28: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

28

Hiding a reachable object – direct

Roots

X Y

a

Z

b

D1: Write(X, b, Read(Y, a))

23-Dec-14

Page 29: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

29

Hiding a reachable object – direct

D2: Write(Y, a, null)

Roots

X Y

Z

b

23-Dec-14

Page 30: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

30

Hiding a reachable object – direct

D3: scan(Y)

Roots

X Y

Z

b

23-Dec-14

Page 31: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

31

Hiding a reachable object – direct

Roots

X Y

Z

b

23-Dec-14

Page 32: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

32

Hiding a reachable object – transitive

Roots

P Q

c

RSd

23-Dec-14

Page 33: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

33

Hiding a reachable object – transitive

Roots

P Q

c

RSd

T1: Write(P, e, Read(R, d))

e

23-Dec-14

Page 34: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

34

Hiding a reachable object – transitive

Roots

P Q

RSd

T2: Write(Q, c, null)

e

23-Dec-14

Page 35: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

35

Hiding a reachable object – transitive

Roots

P Q

RSd

T3: scan(Q)

e

23-Dec-14

Page 36: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

36

Hiding a reachable object – transitive

Roots

P Q

RSd

e

23-Dec-14

Page 37: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

37

Outline General concurrent GC concepts

Incremental, mostly concurrent, “on-the-fly” collections

The lost object problem Tricolor invariants

23-Dec-14

Page 38: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

38

Losing objectsWilson [1994] states that an object can be lost

only if the following two conditions hold:

1. Mutator stores a pointer to a white object into a black object, and

2. All paths from grey objects to that white object are destroyed

23-Dec-14

Page 39: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

39

Not losing any objects Must ensure that both conditions don’t hold

simultaneously

Weak tricolor invariant: All white objects pointed to by black objects are

“grey protected” (reachable from some grey object, directly or through a

white chain)

This invalidates the second condition

Strong tricolor invariant: There are no pointers from black objects to white

objects This invalidates both conditions

23-Dec-14

Page 40: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

40

Maintaining the invariants Our examples:

Wrote a pointer to a white object in a black object (D1/T1) Broke the strong invariant

Deleted all paths from grey objects to that white object (D2/T2) Broke the weak invariant

Therefore, a black object pointed to a (presumed garbage) white object, violating correctness

Solutions will have to operate at one of these steps

23-Dec-14

Page 41: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

41

Tradeoffs in the solution space Solution properties

Precision: amount of “floating garbage” Efficiency: throughput Atomicity: degree of concurrency

Example: A stop-the-world collector obtains:

Maximal precision No concurrency with the mutator

Finer grained atomicity increases concurrency, at the expense of possibly accuracy and/or overhead of atomic operations

23-Dec-14

Page 42: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

42

Mutator color Consider the mutator itself an object

Grey mutator Has not yet been scanned by the collector, or Roots have been scanned but have to be rescanned

Black Mutator Scanned by the collector; roots won’t be rescanned Under strong invariant: roots don’t point to white

objects Under weak invariant: can hold white objects IF “grey

protected”

23-Dec-14

Page 43: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

43

Nuances of mutator color Mutator color has implications for collection

termination If a grey mutator is permitted, it may be necessary

to halt all mutator threads for a final scan of the roots

Real on-the-fly collectors distinguish among multiple mutator threads Because they do not stop all at once to sample roots Therefore, must operate with mutators of different

colors

Also, may separate the roots to different groups (black/grey) For example, by stack frames

23-Dec-14

Page 44: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

44

Allocation color Mutator color influences color of allocated

memory Must satisfy the invariant that applies given the

mutator color Grey mutator can allocate white objects Black mutator can not allocate white objects

Unless it knows that the reference will be stored ahead of wavefront

Allocating black objects is always safe

If an object is allocated grey/black, it will not be reclaimed in the current allocation cycle Even if reference dropped right away

23-Dec-14

Page 45: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

45

Short recap

23-Dec-14

Tricolor invariant types Weak invariant

Every white object pointed by a black object, is “grey protected”

Strong invariant No pointers from black objects to white objects

Mutator color Grey mutator

Some of the roots haven’t been scanned yet Black mutator

All roots scanned

Page 46: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

46

Incremental update solutions Address T1/D1 mutations (adding a white

pointer to a black object) Conservatively treat a white object inserted behind

the wavefront as alive Increase (increment) the set of objects known to be live

Use write barrier to re-color source/destination to prevent black to white pointers Can use a read barrier to load references to a black

mutator

Thus, preserve the strong invariant23-Dec-14

Page 47: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

47

Snapshot-at-the-beginning solutions Address T2/D2 mutations (deletion of a white

pointer from a white/grey object) Conservatively treat any white object ahead of the

wavefront as alive

Use a write barrier to protect against deletion Must snapshot the mutator and operate only with black

mutator

Maintain the weak invariant No way to delete all paths from grey objects to any

object that was alive at the beginning of the collection cycle

23-Dec-14

Page 48: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

48

Outline General concurrent GC concepts

Incremental, mostly concurrent, “on-the-fly” collections

The lost object problem Tricolor invariants

Barrier techniques for concurrent GC Complete enumeration of techniques

23-Dec-14

Page 49: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

49

Barrier techniquesTo maintain one of the two invariants, the following

actions can be taken at barriers:

Add to the wavefront by shading a white object grey

Advance the wavefront by scanning an object (to black)

Retreat the wavefront by reverting a black object to grey

Any other action would break the invariants

23-Dec-14

Page 50: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

50

Grey mutator incremental update (v1) Steele [1975, 1976]

atomic Write(src, i, ref):src[i] refif isBlack(src)

if isWhite(ref)revert(src)

23-Dec-14

Page 51: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

51

Grey mutator incremental update (v1) Steele [1975, 1976]

atomic Write(src, i, ref):src[i] refif isBlack(src)

if isWhite(ref)revert(src)

Maintains the strong invariant No black to white pointers exist

Most precise barrier, at the cost of progress

23-Dec-14

Page 52: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

52

Grey mutator incremental update (v2) Boehm et al [1991]

atomic Write(src, i, ref):src[i] refif isBlack(src)

revert(src)

23-Dec-14

Page 53: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

53

Grey mutator incremental update (v2) Boehm et al [1991]

atomic Write(src, i, ref):src[i] refif isBlack(src)

revert(src)

Less precise Uses virtual memory dirty bits to record

modifications Stop-the-world to rescan dirty pages

23-Dec-14

Page 54: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

54

Grey mutator incremental update (v3) Dijkstra et al [1976, 1978]

atomic Write(src, i, ref):src[i] refif isBlack(src)

shade(ref)

23-Dec-14

Page 55: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

55

Grey mutator incremental update (v3) Dijkstra et al [1976, 1978]

atomic Write(src, i, ref):src[i] refif isBlack(src)

shade(ref)

Original version even removed the condition check In order to relax (remove) atomicity And the original-original version ..

23-Dec-14

Page 56: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

56

Dijkstra, Owicki and Gries

23-Dec-14

Dijkstra initially suggested the following (reordered) barrier Write(src, i, ref):

shade(ref)src[i] ref

Does it work?

Page 57: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

57 23-Dec-14

Page 58: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

58

Black mutator incremental update (v1) Baker [1978]

atomic Read(src, i):ref src[i]if isGrey(src)

ref shade(ref)return ref

23-Dec-14

Page 59: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

59

Black mutator incremental update (v1) Baker [1978]

atomic Read(src, i):ref src[i]if isGrey(src)

ref shade(ref)return ref

Maintains the strong invariant (after snapshotting mutator) No pointers from black to white

Supports a copying-collector23-Dec-14

Page 60: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

60

Black mutator incremental update (v2) Appel et al [1988]

atomic Read(src, i):if isGrey(src)

scan(src)return src[i]

23-Dec-14

Page 61: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

61

Black mutator incremental update (v2) Appel et al [1988]

atomic Read(src, i):if isGrey(src)

scan(src)return src[i]

Less precise (more coarse-grained) than before Uses virtual memory page protection mechanisms

to trap Trapped instruction continues after scanning

23-Dec-14

Page 62: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

62

Black mutator snapshot-at-the-beginning Abraham and Patel [1987] and Yuasa [1990]

atomic Write(src, i, ref):if isGrey(src) || isWhite(src)

shade(src[i])src[i] ref

23-Dec-14

Page 63: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

63

Black mutator snapshot-at-the-beginning Abraham and Patel [1987] and Yuasa [1990]

atomic Write(src, i, ref):if isGrey(src) || isWhite(src)

shade(src[i])src[i] ref

Maintains the weak invariant White objects that were alive at the beginning are

grey protected Less precise of all

Initially wasn’t conditioned (used virtual memory COW) 23-Dec-14

Page 64: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

64

Black mutator hybrid barrier Pirinen [1998]

atomic Read(src, i):if isWhite(src)

shade(src[i])return src[i]

atomic Write(src, i, ref):if isGrey(src)

shade(src[i])src[i] ref

23-Dec-14

Page 65: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

65

Black mutator hybrid barrier Pirinen [1998]

atomic Read(src, i):if isWhite(src): shade(src[i])return src[i]

atomic Write(src, i, ref):if isGrey(src): shade(src[i])src[i] ref

Maintains the weak invariant Actually every black to white pointer has a direct

grey father23-Dec-14

Page 66: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

66

Completeness of barrier techniques Pirinen [1998] claims that this is the complete

list, other than possible short-circuiting/coarsening: Scanning instead of shading Scan origin in a deletion barrier rather than

shading deleted ..

The given barrier techniques cover the minimum requirements to maintain their invariants

23-Dec-14

Page 67: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

67

Outline General concurrent GC concepts

Incremental, mostly concurrent, “on-the-fly” collections

The lost object problem Tricolor invariants

Barrier techniques for concurrent GC Complete enumeration of techniques Efficient implementation through card tables

23-Dec-14

Page 68: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

68

Concurrent write barrier mechanisms Write barriers must detect and record all grey

objects To be inspected (and traced) by the collector Mutators and collectors will access such data

concurrently Must work correctly and efficiently

One way is to add these grey objects to a log Which we explored before (Chapter 13; queues etc)

Another solution is using card tables

23-Dec-14

Page 69: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

69

Card tables A card table holds one byte for every 512bytes

of memory Marking only the dirty blocks

In our case, the card table is the work list of the collector Mutators mark dirty blocks and collectors scan them

for grey objects They do it repeatedly; the collector’s job is to make

all the cards clean in order to complete marking phase Might require stop-the-world period to keep mutator from

constantly updating23-Dec-14

Page 70: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

70

Outline General concurrent GC concepts

Incremental, mostly concurrent, “on-the-fly” collections

The lost object problem Tricolor invariants

Barrier techniques for concurrent GC Complete enumeration of techniques Efficient implementation through card tables

Summary

23-Dec-14

Page 71: Concurrent Garbage Collection Presented by Roman Kecher GC Seminar, Tel-Aviv University 23-Dec-141

71

Summary The solution space is huge

Strong/weak tricolor invariants Grey/black mutators Many different barrier flavors to consider

Will be useful in the following chapters

There is no free-lunch Concurrent/incremental garbage collectors require

synchronization and possibly do more work overall But this is in order to reduce observable pause

times Which is important in many situations

23-Dec-14