37
1 Shenandoah: An ultra-low pause time garbage collector for OpenJDK Christine Flood Principal Software Engineer Red Hat

Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

1

Shenandoah: An ultra-low pause

time garbage collector for

OpenJDK

Christine FloodPrincipal Software EngineerRed Hat

Page 2: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

2

Why do we need another Garbage Collector?

● OpenJDK currently has:● SerialGC● ParallelGC● ParNew/Concurrent Mark Sweep(CMS)● Garbage First (G1)

Page 3: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

3

Why do we need another Garbage Collector?

● OpenJDK currently has:● SerialGC● ParallelGC● ParNew/Concurrent Mark Sweep(CMS)● Garbage First (G1)

● Shenandoah● Pause times similar to CMS.● Region based like G1.● Concurrent Compaction

Page 4: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

4

Stack FrameMethod Foo

Heap

Reference

42Value

Reference

Stack FrameMethod Bar

Reference

Value 6.847

Reference

Reference

Reference

Why is concurrent compaction difficult?

C

ObjectObjectObjectObjectObjectObjectObjectA

B

Page 5: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

5

Stack FrameMethod Foo

Heap

Reference

42Value

Reference

Stack FrameMethod Bar

Reference

Value 6.847

Reference

Reference

Reference

Pause to update the roots, and have heap object accesses go through a forwarding pointer.

C

A

B

C'

Forwarding ptr

Forwarding ptr

Forwarding ptr

Forwarding ptr

Page 6: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

6

Forwarding Pointers

Foo

Foo indirection pointer

Bar

Bar indirection pointer

● You can still walk the heap.

● You can still choose your GC at runtime.

● Software only solution.

Page 7: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

7

Shenandoah divides the heap into regions.

Region 1

Region 2

Region 3

Region 4

Region 5

Page 8: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

8

Region 1

Region 2

Region 3

Region 4

Region 5

20k

100k

500k

10k

70k

Regions Live Data

We use concurrent marking to keep track of the live data in each region.

Region 6

Region 7

Region 8

Region 9

Region 10

200k

100k

empty

empty

empty

Regions Live Data

Page 9: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

9

Region 1

Region 2

Region 3

Region 4

Region 5

20kFrom-region

100k

500k

10kFrom-region

70k

Regions Live Data

We pick the most garbage-y regions for evacuation.

Region 6

Region 7

Region 8

Region 9

Region 10

20k

100k

to-region

empty

empty

Regions Live Data

Page 10: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

10

Stack FrameMethod Foo

Heap

Reference

42Value

Reference

Stack FrameMethod Bar

Reference

Value 6.847

Reference

Reference

Object

Object

Object

Object

Array

ObjectObjectObjectObjectObjectObjectObject

Reference

Object

Concurrent Marking tells us which objects need to be copied.

Page 11: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

11

BAR

FOO'FOO

BAR'

We evacuate the live objects while the Java threads are running.

Page 12: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

12

Reclaiming free space

● Eagerly● Run another pass over the data to update

references.● Lazily

● Wait for the next concurrent mark to update references.

● Once the references have been updated we can free the now empty regions.

Page 13: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

13

Shenandoah

JavaInit Mark

Java FinalMark

Concurrent Mark Concurrent Evacuation

Java

We use as many threads as are available to do concurrent phases.

Page 14: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

14

A

B

From-Region To-Region

FOO A'

Page 15: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

15

Added benefit to forwarding pointers

Object

We can be lazy about updatingreferences to objects in from regions.

We no longer need remembered sets.

Page 16: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

16

Concurrent Marking

● SATB – Snapshot At The Beginning● Anything live at Initial Marking is considered live.● Anything allocated since Initial Marking is considered

live.● Used to update references, keep track of amount of

live data for each region, and tell us which objects are live and need to be evacuated.

Page 17: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

17

What's tricky about SATB?

X

w

z

Y

X

Start of Concurrent Marking

Sometime during marking

Requires a write barrier to ensure overwritten values get marked.

y

Page 18: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

18

How to move an object while the program is running.

● Read the forwarding pointer to the from-region.

● Allocate a temporary copy of the object in a to-region.

● Speculatively copy the data.

● CAS the forwarding pointer to point to the new copy.● If the CAS fails, another thread already copied the

object and you can roll back your speculative copy.

Page 19: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

19

Forwarding Pointers - reads

Reading an object in a From-region doesn't trigger an evacuation.

A

B

From-Region To-Region

Note: If reads were to cause copying we might have a “read storm” where every operationrequired copying an object. Our intention is that since we are only copying on writes we will have less bursty behavior.

Page 20: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

20

Forwarding Pointers - writes

Writing an object in a From-Region will trigger an evacuation of that object to a To-Region and the write will occur in there.

Invariant: Writes never occur in from-regions.

From-Region To-Region

A

B

A'

Page 21: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

21

Forwarding Pointers – writes of references

We resolve all references before we write them.

Invariant: Never write a reference to a from-region object into a to-region object.

From-Region To-Region

A

B

A'

B

Page 22: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

22

Forwarding Pointers - CAS

● CompareAndSwap(A.X, Foo, Bar)

From-Region To-Region

Foo

A

Bar

Page 23: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

23

Forwarding Pointers - CAS

● CompareAndSwap(A.X, Foo, Bar)

From-Region To-Region

Foo

A

Bar

A'

Page 24: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

24

Forwarding Pointers - CAS

● CompareAndSwap(A.X, Foo, Bar)

From-Region To-Region

Foo

A

Bar

A'

Foo'

Page 25: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

25

Forwarding Pointers - CAS

● CompareAndSwap(A.X, Foo, Bar)

From-Region To-Region

Foo

A

Bar

A'

Foo'

Page 26: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

26

Forwarding Pointers - CAS

● CompareAndSwap(A.X, Foo, Bar)

From-Region To-Region

Foo

A

Bar

A'

Foo'

Bar'

Now we can CAS.

Page 27: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

27

Forwarding Pointers - CAS

● CompareAndSwap(A.X, Foo, Bar)

From-Region To-Region

Foo

A

Bar

A'

Foo'

Bar'

Page 28: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

28

Does that mean a write could keep a Java thread from making progress for an unbounded amount of time?

● No

● Copies are bounded by region size.

● Objects that are larger than a region are treated specially.

Page 29: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

29

Read Barriers?

Unconditional Read Barrier

movq R10, [R10 + #-8 (8-bit)] # ptr

movq RBX, [R10 + #16 (8-bit)] # ptr ! Field: java/lang/ClassLoader.parent

Page 30: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

30

Write Barriers?

● We need the SATB write barrier which adds previous reference values onto a queue to be scanned.

● We also need write barriers on all writes (even base types) to ensure we copy objects in targeted regions before we write to them.

Page 31: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

31

Costs and Benefits

● Costs

● Space● An extra word /

object can be expensive if you have a lot of small objects.

● Time● Reads and Writes

require barriers

● Benefits

● Ultra-low pause times which can be important to interactive and SLA applications.

Page 32: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

32

Current status

● We have something working.

● We can pass small tests specjvm, specjbb

● We have passed the smoke test for● Eclipse, Thermostat

● We are working on performance tuning● Radargun, Elastic Search/Lucene

Page 33: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

33

Performance Tuning

● One very important area for application specific performance tuning will be Shenandoah heuristics.

● Lazy Heuristics● GC as little as possible

● Aggressive Heuristics● GC as frequently as possible to maintain minimum heap

size.

● ...

Page 34: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

34

Encouraging preliminary results SpecJVM2008 compiler

● Not our target application.

● We are just starting performance tuning.

● Shenandoah● Initial Mark (avg=5.65ms,max=9.53ms, total=1.40s)● Final Mark (avg=8.74ms,max=15.43ms,total=2.17s)

● As compared to G1● (avg=31.38ms, max=75.48ms, total=6.84s)

Page 35: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

35

References

● “Trading Data Space for Reduced Time and Code Space in Real-Time Garbage Collection on Stock Hardware” - Brooks

● “Garbage-first garbage collection” - Detlefs, Flood, Heller, Printezis.

Page 36: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

36

Future Work

● Finish big application testing.

● Move the barriers to right before code generation.

● Heuristics tuning.

● Round Robbin Thread Stopping?

● NUMA Aware?

Page 37: Shenandoah: An ultra-low pause time garbage collector for ...€¦ · 18 How to move an object while the program is running. Read the forwarding pointer to the from-region. Allocate

37

More information

● Download the code and try it.● http://icedtea.classpath.org/wiki/Shenandoah

● Blogs● http://christineflood.wordpress.com/● http://rkennke.wordpress.com/

● Email● [email protected][email protected]