Download
on the fly garbage collector n.
Skip this Video
Loading SlideShow in 5 Seconds..
On The Fly Garbage Collector PowerPoint Presentation
Download Presentation
On The Fly Garbage Collector

On The Fly Garbage Collector

168 Views Download Presentation
Download Presentation

On The Fly Garbage Collector

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. On The Fly Garbage Collector Edger W. Dijkstra Leslie Lamport A. J. Martin C. S. Scholten E.F.M. Steffens Presented by: Dana Drachsler

  2. Garbage Collection – Problem description • roots • 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”

  3. Garbage Collection – Problem description • Operations we can apply on reachable nodes: • Redirecting an edge towards an already reachable one • Redirecting an edge towards a not yet reachable node that doesn’t have outgoing edges • Adding an edge towards an already reachable one • Adding an edge towards a not yet reachable node that doesn’t have outgoing edges • Removing an edge • After applying operations of type 1, 2 or 5 a node may become a garbage node.

  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

  5. Implementing a Garbage Collector • The trivial solution for a garbage collector: • While (free list is not empty) continue • 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

  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

  7. Solutions • We present three solutions to the garbage collection problem • We start with a coarse grained solution and we next refine it.

  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: • Redirecting an edge towards an already reachable one • Redirecting an edge towards a not yet reachable node NIL 8

  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

  10. Reformulation of the Problem Step 2 • We are left with a single type of operation: • 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 roots

  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

  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.

  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.

  14. The Coarse Grained Solution • Can we eliminate the overhead of the mutator? • No, consider the following scenario. roots A B C

  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 thatC is reachable • Thus, the mutator must mark insome way the target nodes of edges it redirects roots A B C

  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

  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

  18. Marking the Nodes • Suppose the mutator wants to redirectone 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

  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

  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 successorand • 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

  21. The Marking Phase roots • Shade all roots • i = 0, k = M • While (k > 0) • If (node i is gray) • k = M • Shade all successors of node iand make node i black • Else // node i isn’t gray • k = k – 1 • i = (i + 1) mod M 0 0 1 1 2 2 3 3 5 4 4 k = 1 k = 2 i = 2 i = 1 i = 0 k = 6 k = 5 k = 3 k = 4 k = 0 i = 3 i = 4 i = 5

  22. The Marking Phase roots 0 6 1 2 7 3 5 4 8 NIL

  23. Appending Phase • i = 0 • While (i < M) • If (node i is white) • Append it to the free list • Else if (node i is black) • Mark it white • Else • Error • i = i + 1

  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.

  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 increases • Thus, the collector may violate it only if it makes a non garbage node white or by making a white node non garbage • This is violated only with respect to node i, but then the subsequent increase i = i + 1 restores the invariant

  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 invariant • It 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

  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 nodes • At the end of the appending phase, there are no black nodes • The 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

  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 reachable • Not black nodes, since it only shades nodes

  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

  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. • While (i < M) • If (node i is white) … • Else if (node i is black) … • Else Error • i = i + 1

  31. Proving Correctness Criteria • Proof: • The marking phase terminatessince the quantity k + M * (number of nonblack nodes) decreases by at least one in each iteration of the marking phase CC1: Every garbage node is eventually appended to the free list. • … • i = 0, k = M • While (k > 0) • If (node i is gray) • k = M • … • Else • k = k – 1 • …

  32. 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 D nodes

  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

  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

  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

  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 it • We want to split it into two atomic operations • We 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

  37. Towards a Finer Grained Solution A • 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 phaseand 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 B B No node points from a black node to a white node

  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

  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

  40. A New Coarse Grained Solution For each white reachable node, there exists a propagation path leading to it • 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 roots 0 1 2 3 7 4 5 6 8

  41. A New Coarse Grained Solution For each white reachable node, there exists a propagation path leading to it • Thus, we only need to show that we keep our new invariant roots 0 1 2 3 7 4 5 6 8

  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

  43. The New Invariants For each white reachable node, there exists a propagation path leading to it • Recall the collector’s atomic operation: • Shade all successors of node iand make node iblack Only the last edge placed by the mutator may lead from a black node to a white one

  44. The New Invariants For each white reachable node, there exists a propagation path leading to it • 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 Only the last edge placed by the mutator may lead from a black node to a white one

  45. The New Invariants roots • The mutator’s new atomic operation: • Shade the target of the previouslyredirected edge • redirect an outgoing edge of a reachable node towards a reachable node • This clearly holds A B B C D C 3 Only the last edge placed by the mutator may lead from a black node to a white one

  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 roots 0 0 1 6 1 6 2 7 2 3 3 5 4 8 NIL

  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 roots 0 1 6 2 7 3 5 4 8 NIL

  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 iblack • We need to show that our invariants still hold during the marking phase • We will show stronger invariants

  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 isempty • We create C-edges when we shade a node’s successors • Thec-edges are the node’s edges

  50. A Fine Grained Solution • The strengthened invariants: Every root is gray or black, and for each white reachable node, there exists a propagation path leading to it, containing no C-edges 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