Top Banner
ON THE FLY GARBAGE COLLECTOR Edger W. Dijkstra Leslie Lamport A. J. Martin C. S. Scholten E.F.M. Steffens Presented by: Dana Drachsler 1
62

On The Fly Garbage Collector

Feb 23, 2016

Download

Documents

Sudeshna Sen

On The Fly Garbage Collector. Edger W. Dijkstra Leslie Lamport A. J. Martin C. S. Scholten E.F.M. Steffens Presented by: Dana Drachsler. Garbage Collection – Problem description. roots. Directed graph The number of nodes is fixed, M The edges may change - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: On The Fly Garbage Collector

1

ON THE FLY GARBAGE COLLECTOR

Edger W. DijkstraLeslie LamportA. J. MartinC. S. ScholtenE.F.M. Steffens

Presented by: Dana Drachsler

Page 2: On The Fly Garbage Collector

2

roots

GARBAGE COLLECTION – PROBLEM DESCRIPTION Directed graph

The number of nodes is fixed, M The edges may change Each node has two outgoing edges:

left edge, right edge Either of them can be missing

We have a set of “root nodes” A node is reachable if it is reachable from

some root The data structure consists of all reachable

nodes and their interconnections Nodes that are not reachable are called “garbage

nodes”

Page 3: On The Fly Garbage Collector

3

GARBAGE COLLECTION – PROBLEM DESCRIPTION Operations we can apply on reachable nodes:

1. Redirecting an edge towards an already reachable one

2. Redirecting an edge towards a not yet reachable node that doesn’t have outgoing edges

3. Adding an edge towards an already reachable one

4. Adding an edge towards a not yet reachable node that doesn’t have outgoing edges

5. Removing an edge After applying operations of type 1, 2 or 5 a node may become a garbage node.

Page 4: On The Fly Garbage Collector

4

IMPLEMENTING A GARBAGE COLLECTOR We maintain a list of “free list” of nodes that

have been identified as garbage nodes These nodes are available to be added to the

data structure

Page 5: On The Fly Garbage Collector

5

IMPLEMENTING A GARBAGE COLLECTOR The trivial solution for a garbage collector:

1. While (free list is not empty) continue2. Halt every processor, and start collecting

garbage: Starting from the roots, mark all reachable

nodes The “marking phase”

Append all unmarked nodes to the free listand remove the marking The “sweeping phase”

Goto 1

roots

Page 6: On The Fly Garbage Collector

6

DISADVANTAGES OF THIS SOLUTION In 1978, the minor disadvantage was the

delay of the computation The major disadvantage was the unpredicted

interludes caused by the garbage collector This led to difficulties upon designing real-time

systems. To this end, we study the case where we

have two processors: The “mutator” – responsible only for the

computation The collector – responsible for collecting garbage

They both operate concurrently

Page 7: On The Fly Garbage Collector

7

SOLUTIONS We present three solutions to the garbage

collection problem We start with a coarse grained solution and

we next refine it.

Page 8: On The Fly Garbage Collector

8

REFORMULATION OF THE PROBLEM STEP 1 We have a special root node named NIL

Its two outgoing edges point to it

A missing edge will be replaced with anedge to NIL

Thus, we are left with only two possible operations:1. Redirecting an edge towards an already

reachable one2. Redirecting an edge towards a not yet

reachable node8

NIL

Page 9: On The Fly Garbage Collector

9

REFORMULATION OF THE PROBLEM STEP 2 We add special root nodes that NIL

and all garbage nodes will be reachable from them but no other node will be reachable from

them. Thus, all nodes are now part of the

data structure

NIL roots

roots

Page 10: On The Fly Garbage Collector

10

roots

REFORMULATION OF THE PROBLEM STEP 2 We are left with a single type of

operation:1. Redirecting an edge towards an already

reachable one Operation of type 2 is translated into

two modifications of type 1: Redirect an edge towards a node

in the free list Redirect edges of free list’s nodes to

remove this node from the free list

NIL roots

Page 11: On The Fly Garbage Collector

11

REFORMULATION OF THE PROBLEM STEP 2 Now, the activities of the mutator and

collector are repeated executions of: Mutator:

Redirect an outgoing edge of a reachable node towards an already reachable one

Collector: Marking phase:

Mark all reachable nodes Appending phase:

Append all unmarked nodes to the free list Remove the marking from all marked nodes

Page 12: On The Fly Garbage Collector

12

CORRECTNESS CRITERIA The mutator and collector keep throughout

the execution the following correctness criteria:

CC1 (Liveness):Every garbage node is eventually

appended to the free list.

CC2 (Safety) :Appending a garbage node to the free

list is the collector’s only modification of the data structure.

Page 13: On The Fly Garbage Collector

13

ATOMIC OPERATIONS We will assume that the following operations

are atomic: Redirecting an edge Finding the left or right successor of a node Testing and/ or setting certain attributes of a

node Appending node to the free list

This is simple, provided that the free list remains long enough and then the mutator does not interfere with the collector’s appending operation.

Page 14: On The Fly Garbage Collector

14

THE COARSE GRAINED SOLUTION Can we eliminate the overhead of the

mutator? No, consider the following scenario. rootsA

B

C

Page 15: On The Fly Garbage Collector

15

THE COARSE GRAINED SOLUTION Can we eliminate the overhead of the

mutator? No, consider the following scenario. The collector observes nodes

one at a time Hence, it may never discover that

C is reachable Thus, the mutator must mark in

some way the target nodes of edges it redirects

rootsAB

C

Page 16: On The Fly Garbage Collector

16

MARKING THE NODES We will use colors for marking We start with all nodes white During the marking phase all

reachable nodes will be marked black

At the end of the marking phase, all white nodes are garbage nodes

Page 17: On The Fly Garbage Collector

17

MARKING THE NODES During the marking phase we keep the

following invariants:

No node will become lighter

No edge points from a black node to a white node

Page 18: On The Fly Garbage Collector

18

MARKING THE NODES Suppose the mutator wants to redirect

one of its edge to a white node It will violate our invariant

Can it mark it black? No, the white node may have white

successors Thus, we need to introduce another color

Gray

No edge points from a black node to a white node

Page 19: On The Fly Garbage Collector

19

THE MUTATOR We define “shading a node” as

marking it gray if it was white, and leave it unchanged otherwise

The mutator repeatedly performs the following atomic operation: Redirect an outgoing edge of a

reachable node towards an already reachable one and

Shade it

Page 20: On The Fly Garbage Collector

20

THE COLLECTOR The collector will also use the gray color in

order to ensure it doesn’t violate the invariant Upon encountering a gray node, the collector

will: Mark it black

and Shade its left successor

and Shade its right successor

The marking phase will terminate once there are no gray nodes This will be detected after scanning all nodes

without finding gray ones

Page 21: On The Fly Garbage Collector

21

THE MARKING PHASE1. Shade all roots2. i = 0, k = M 3. While (k > 0)

1. If (node i is gray) 1. k = M2. Shade all successors of node i

and make node i black2. Else // node i isn’t gray

1. k = k – 13. i = (i + 1) mod M

roots01

23

4

i = 0 k = 6

01

2

i = 1

3

i = 2

4

i = 3i = 4i = 5

5

k = 5k = 4k = 3k = 2k = 1k = 0

Page 22: On The Fly Garbage Collector

22

THE MARKING PHASEroots0

1

23

4

6

7

5 8

NIL

Page 23: On The Fly Garbage Collector

23

APPENDING PHASE1. i = 02. While (i < M)

1. If (node i is white) 1. Append it to the free list

2. Else if (node i is black)1. Mark it white

3. Else1. Error

4. i = i + 1

Page 24: On The Fly Garbage Collector

24

PROVING CORRECTNESS CRITERIA

Proof: It suffices to show that in the appending phase

we append only garbage nodes to the free list To this end, we prove the invariant:

a white node with a number ≥ i is garbage

CC2:Appending a garbage node to the free

list is the collector’s only modification of the data structure.

Page 25: On The Fly Garbage Collector

25

PROVING CORRECTNESS CRITERIA “A white node with a number ≥ i is garbage” Proof: This is held between the appending cycles:

Throughout the appending phase i only increasesThus, the collector may violate it only if it makes

a non garbage node white or by making a white node non garbageThis is violated only with respect to node i, but

then the subsequent increase i = i + 1 restores the invariant

Page 26: On The Fly Garbage Collector

26

PROVING CORRECTNESS CRITERIA “A white node with a number ≥ i is garbage” Proof: This is held between the appending cycles:

The mutator cannot violate this invariantIt doesn’t update i It doesn’t color nodes in white (only gray)It can’t redirect edges to non reachable nodes

thus, it can’t make a white node non garbagebecause it is not reachable

Page 27: On The Fly Garbage Collector

27

PROVING CORRECTNESS CRITERIA “A white node with a number ≥ i is garbage” Proof: This is held when we enter the appending phase:

We need to show that the marking phase has established that “all white nodes are garbage”

To prove this, we assume that at the beginning of the marking phase there are no black nodesAt the end of the appending phase, there are

no black nodesThe mutator doesn’t color nodes in black

Recall the mutator and collector maintain the following:No edge points from a black node to a

white node

Page 28: On The Fly Garbage Collector

28

PROVING CORRECTNESS CRITERIA “A white node with a number ≥ i is garbage” Proof: This is held when we enter the appending phase:

Thus, when there are no more gray nodes all black nodes are reachable and all white node are garbage

We determine that there are no gray nodes after scanning all nodes without encountering gray nodes

If only the collector would have colored nodes in gray, this was trivially correct

Can the mutator also color nodes in gray?Not white nodes, since they are not reachableNot black nodes, since it only shades nodes

Page 29: On The Fly Garbage Collector

29

PROVING CORRECTNESS CRITERIA “A white node with a number ≥ i is garbage” Proof: This is held when we enter the appending phase:

Thus if a collector has scanned all nodes and didn’t encounter a gray node, it implies that at the beginning of that scan there were no gray nodes If there was a gray node at the beginning of the

scan the collector must have encountered it The mutator leaves gray nodes gray

Thus, we can safely determine that there are no gray nodes and all white nodes are garbage

Page 30: On The Fly Garbage Collector

30

PROVING CORRECTNESS CRITERIA

Proof: We first show that the collector’s

two phases terminate properly The appending phase terminates

unless it encounters a gray node At the end of the marking phase there are no gray

nodes Also, every white node is garbage, thus the

mutator cannot shade them Thus, there are no gray nodes during this phase

CC1:Every garbage node is eventually

appended to the free list.1. While (i < M)

1. If (node i is white) …2. Else if (node i is black)

…3. Else Error4. i = i + 1

Page 31: On The Fly Garbage Collector

31

PROVING CORRECTNESS CRITERIA

Proof: The marking phase terminates

since the quantity k + M * (number of nonblack nodes) decreases by at least one in each iteration of the marking phase

1. …2. i = 0, k = M 3. While (k > 0)

1. If (node i is gray) 1. k = M2. …

2. Else 1. k = k – 1

3. …

CC1:Every garbage node is eventually

appended to the free list.

Page 32: On The Fly Garbage Collector

32

D nodes

PROVING CORRECTNESS CRITERIA At the beginning of the appending phase we

have 3 sets: The set of reachable nodes

which are black The set of white garbage nodes

which will be appended to the freelist

The set of black garbage nodes We name them D-nodes

We want to show that D-nodes will be appended to the free list in the next appending phase

Page 33: On The Fly Garbage Collector

33

PROVING CORRECTNESS CRITERIA We say that an edge

“leads into D” if its source is not in D and its target is in D.

Because D-nodes are garbage, the sources of edges that lead into D are white.

Since D-nodes are garbage, the mutator will not redirect edges towards them

Since they are black they will not be appended during this appending phase

D nodes

Page 34: On The Fly Garbage Collector

34

PROVING CORRECTNESS CRITERIA But the collector will append

all white nodes to the free list, thus redirect their edges

Thus at the end of this phase: There will be no edges leading

into D All D nodes will be white

No new edges that lead into D until the next appending phase The mutator surely cannot create new ones The collector doesn’t redirect edges during the

marking phase

D nodes

Page 35: On The Fly Garbage Collector

35

PROVING CORRECTNESS CRITERIA Thus, at the next marking round they will

remain white And will be appended to the free list in the

next appending phase

Page 36: On The Fly Garbage Collector

36

TOWARDS A FINER GRAINED SOLUTION Recall the mutator atomic operation:

Redirect an outgoing edge of a reachable node towards an already reachable one

Shade itWe want to split it into two atomic operationsWe also want to maintain our old invariant

The trivial solution: shade the new target and then redirect the edge

No edge points from a black node to a white node

Page 37: On The Fly Garbage Collector

37

TOWARDS A FINER GRAINED SOLUTION Consider the following scenario:

The mutator shades B and goes to sleep The collector performs a marking phase Then, it performs an appending phase

Afterwards B’s color is white! The collector begins another marking phase

and color A in black and goes to sleep The mutator redirect A’s edge towards B

The mutator redirects all edges that their target is B The collector completes the marking phase, and in the

appending phase identifies B as garbage!

A

BB

A

No node points from a black node to a white node

Page 38: On The Fly Garbage Collector

38

TOWARDS A FINER GRAINED SOLUTION Thus, we must change the mutator’s atomic

operation Thus, before introducing a finer grained

solution we need a new coarse grained solution The collector will remain the same

Page 39: On The Fly Garbage Collector

39

A NEW COARSE GRAINED SOLUTION The pervious invariant allowed us to deduce

that if we encountered a reachable white node then there exists a gray node

Propagation path: A path that begins with a gray node and all

other nodes are white

We used the old invariant to conclude that if there are no gray nodes, all white nodes are garbage The new invariant suffices for this conclusion

For each white reachable node, there exists a propagation path leading to it

Page 40: On The Fly Garbage Collector

40

A NEW COARSE GRAINED SOLUTION

Corollary: If each root is gray or black,

the absence of edges from black to white implies our invariant. In particular it is true at the beginning of the marking cycle because all nodes have been shaded and there are no black nodes

For each white reachable node, there exists a propagation path leading to it

roots0

1

34

6

2

7

5 8

Page 41: On The Fly Garbage Collector

41

A NEW COARSE GRAINED SOLUTION

Thus, we only need to show that we keep our new invariant

For each white reachable node, there exists a propagation path leading to it

roots0

1

34

6

2

7

5 8

Page 42: On The Fly Garbage Collector

42

A NEW COARSE GRAINED SOLUTION To prove this, we need to maintain another

invariant

Note that in the absence of black nodes, this clearly holds Thus, at the beginning of the marking phase, this

holds We now show that both invariants are held

during the marking phase

Only the last edge placed by the mutator may lead from a black node to a white

one

Page 43: On The Fly Garbage Collector

43

THE NEW INVARIANTS

Recall the collector’s atomic operation: Shade all successors of node i and

make node i black

For each white reachable node, there exists a propagation path leading to it

Only the last edge placed by the mutator may lead from a black node to a white

one

Page 44: On The Fly Garbage Collector

44

THE NEW INVARIANTS

Shading the successors means that: The node’s edges are not part of any propagation

path, thus, making the node black doesn’t violate the first invariant

There is no black-to-white edge, thus the second invariant is held

For each white reachable node, there exists a propagation path leading to it

Only the last edge placed by the mutator may lead from a black node to a white

one

Page 45: On The Fly Garbage Collector

45

THE NEW INVARIANTS The mutator’s new atomic operation:

Shade the target of the previously redirected edge

redirect an outgoing edge of a reachable node towards a reachable node

This clearly holds

rootsAB

CC 3DOnly the last edge placed by the mutator

may lead from a black node to a white one

B

Page 46: On The Fly Garbage Collector

46

THE NEW INVARIANTS

We only redirect to reachable nodes, thus,if they are white they had a propagation pathbefore this operation.

If the source node isblack, then its outgoing edge was not part of any propagation path

For each white reachable node, there exists a

propagation path leading to it

roots0

1

23

4

6

7

5 8

NIL

01 6

23

Page 47: On The Fly Garbage Collector

47

THE NEW INVARIANTS

If the source node was white or gray, then afterthis operation, there willbe no edges from a blacknode to a white node

The roots must be grayof black, thus, accordingto the corollary, the invariant holds

For each white reachable node, there exists a

propagation path leading to it

roots0

1

23

4

6

7

5 8

NIL

Page 48: On The Fly Garbage Collector

48

A FINE GRAINED SOLUTION We split the mutator’s atomic operation:

Shade the target of the previously redirected edge

Redirect an outgoing edge of a reachable node towards a reachable node

We split the collector’s atomic operation: Shade the left-hand successor of node i Shade the right-hand successor of node i Make node i black

We need to show that our invariants still hold during the marking phase We will show stronger invariants

Page 49: On The Fly Garbage Collector

49

A FINE GRAINED SOLUTION A C-edge is an edge whose source has been

detected as gray by the collector during the marking phase Note that a C-edge remains a C-edge even if the

target is changed by the mutator At the beginning, the set of C-edges is empty We create C-edges when we shade a node’s

successors The c-edges are the node’s edges

Page 50: On The Fly Garbage Collector

A FINE GRAINED SOLUTION The strengthened invariants:

50

Every root is gray or black, and for each white reachable node, there exists a propagation path

leading to it, containing no C-edgesThere exists at most one edge E satisfying E is a black to white edge or E is a C-edge with a white

target

Page 51: On The Fly Garbage Collector

51

UNDERSTANDING THE INVARIANTSEvery root is gray or

black, and for each white reachable node, there

exists a propagation path leading to it, containing

no C-edges

roots0

1

34

6

2

7

5 8

There exists at most one edge E satisfying E is a black to white

edge or E is a C-edge with a white target

6

3

Page 52: On The Fly Garbage Collector

52

PROVING THE INVARIANTS

At the beginning, There are no C-edges and all roots are gray, thus

the first invariant holds There are no black nodes or C-edges, thus the

second invariant holds

Every root is gray or black, and for each white reachable node, there exists a propagation path

leading to it, containing no C-edgesThere exists at most one edge E satisfying E is a black to white edge or E is a C-edge with a white

target

Page 53: On The Fly Garbage Collector

53

PROVING THE INVARIANTS

None of the atomic operations introduces a new reachable white node

Thus, it suffices to show that if we have a propagation path before applying any of the operations, we have one afterwards

Every root is gray or black, and for each white reachable node, there exists a propagation path

leading to it, containing no C-edgesThere exists at most one edge E satisfying E is a black to white edge or E is a C-edge with a white

target

Page 54: On The Fly Garbage Collector

54

PROVING THE INVARIANTS The mutator’s atomic operation:

Shade the target of the previously redirected edge

Redirect an outgoing edge of a reachable node towards a reachable node

The collector’s atomic operation: Shade the left-hand successor of node i Shade the right-hand successor of node i Make node i black

If we had propagation path without C-edges before these operations, we will have the same paths or shortened paths

0

1

2

0

33

Page 55: On The Fly Garbage Collector

55

PROVING THE INVARIANTS

The collector’s shading operations create C-edges but their targets are black or gray, thus they did not belong to a propagation path

The mutator’s shading operation may only remove edge E if existed

There exists at most one edge E satisfying E is a black to white edge or E is a C-edge with a white

target

Page 56: On The Fly Garbage Collector

56

PROVING THE INVARIANTS The collector’s atomic operation:

Shade the left-hand successor of node i Shade the right-hand successor of node i Make node i black

Node i is gray, thus all its outgoing edges are C-edges, thus they are not part of any propagation path

Every root is gray or black, and for each white reachable node, there exists a propagation path

leading to it, containing no C-edges

Page 57: On The Fly Garbage Collector

57

PROVING THE INVARIANTS The collector’s atomic operation:

Shade the left-hand successor of node i Shade the right-hand successor of node i Make node i black

It may introduce a black to white edge, but then this edge was already a C-edge with a white target

There exists at most one edge E satisfying E is a black to white edge or E is a C-edge with a white

target

Page 58: On The Fly Garbage Collector

58

PROVING THE INVARIANTS The mutator’s atomic operation:

Shade the target of the previously redirected edge Redirect an outgoing edge of a reachable node

towards a reachable node

If this invariant was held before, then there could not have been a black to white edge or a C-edge with a white target.

This operation creates at most one edge of this type

There exists at most one edge E satisfying E is a black to white edge or E is a C-edge with a white

target

Page 59: On The Fly Garbage Collector

59

PROVING THE INVARIANTS The mutator’s atomic operation:

Shade the target of the previously redirected edge Redirect an outgoing edge of a reachable node

towards a reachable node

If the source is black, or the edge is C-edge then the edge didn’t belong to any propagation path

Thus, since this operation does not create other C-edges, the same paths exist

Every root is gray or black, and for each white reachable node, there exists a propagation path

leading to it, containing no C-edges

Page 60: On The Fly Garbage Collector

60

PROVING THE INVARIANTS

Otherwise, the edge to be redirected is not a C-edge and has a white or gray source

Since there is at most one black-to-white edge or a C edge, we know that there are no C-edges and no black-to-white edges at all, using the corollary we get our invariant

Every root is gray or black, and for each white reachable node, there exists a propagation path

leading to it, containing no C-edges

Page 61: On The Fly Garbage Collector

61

SUMMARY We have shown three solutions We first showed a simple coarse grained-

solution Which its invariants were quite straight-forward

We aimed to refine this solution This turned out to be not a simple task

We needed to change our implementation and the invariants

Afterwards we could refine the solution, and “fix” the proof of the coarse grained solution

Page 62: On The Fly Garbage Collector

62

QUESTIONS?