write barriers in g1 gc - jfokus...the garbage first collector - pause histogram 32 pause time in...

50
Write Barriers in Garbage First Garbage Collector -By Monica Beckwith Code Karam LLC @mon_beck; [email protected] 1

Upload: others

Post on 02-Jun-2020

4 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Write Barriers in Garbage First Garbage Collector

-By Monica Beckwith Code Karam LLC

@mon_beck; [email protected]

1

Page 2: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

About me

• Java performance engineer

• I am working as a consultant and an instructor

• I have worked with Oracle, Sun, AMD …

• I used to work in the capacity of G1 GC performance lead @Oracle.

2

Page 3: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Agenda• Heap regions and additional data structures

• RSets

• Barriers

• Concurrent refinement

• G1 GC Stages

• Concurrent marking in G1 GC

• Pre-write barrier

• SATB algorithm

3

Page 4: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Regionalized Heap

4

Page 5: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Traditional Java Heap

5

Contiguous Java Heap

Eden S0 S1

Old Generation

Page 6: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Garbage First GC - Heap Regions

6

Contiguous Java Heap

Free Region

Non-Free Region

Page 7: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

G1 GC Heap Regions

• Young Regions - Regions that house objects in the Eden and Survivor Spaces

• Old Regions - Regions that house objects in the Old generation.

• Humongous Regions - Regions that house Humongous Objects.

7

Page 8: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Additional Data Structures

8

Page 9: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

G1 GC Collection Set & Remembered Sets

• Additional data structure to help with maintenance and collection

• Add a slight footprint overhead (~5%)

9

Page 10: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Remembered Sets (RSets)

10

Page 11: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

• Maintains and tracks incoming references into its region

• old-to-young references

• old-to-old references

11

Remembered Sets

Page 12: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

• Remembered sets have varying granularity based on the “popularity” of objects or regions.

12

Remembered Sets

Page 13: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Remembered Sets

• Different granularities:

• sparse per-region-table (PRT)

• fine-grained PRT

• coarse-grained bitmap

13

Page 14: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram14

Figure 2.3 Remembered sets with incoming object references

Page 15: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

RSet Maintenance: Barriers + Refinement

Threads

15

Page 16: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Write Barrier

16

Page 17: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Post-Write Barrier

Consider the following assignment:

object.field = some_other_object

G1 GC will issue a write barrier after the reference is updated, hence the name.

17

Page 18: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Post-Write BarrierG1 GC filters the need for a barrier by way of a simple check as explained below:

(&object.field XOR &some_other_object) >> RegionSize

If the check evaluates to zero, a barrier is not needed.

If the check != zero, G1 GC enqueues the card in the update log buffer

18

Page 19: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Concurrent Refinement Threads

19

Page 20: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Concurrent Refinement Threads

• The refinement threads will scan cards in the filled update log buffers to update the RSets for their corresponding regions.

• The refinement threads are always active

• G1 GC deploys them in a tiered manner to keep up with the filled buffers

20

Page 21: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Concurrent Refinement Threads

• Mutator threads can be enlisted to help with processing of filled buffers.

• Avoid this scenario, since the Java application will be halted until the filled buffers are processed!

21

Page 22: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

G1 GC Stages

22

Page 23: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

A Young Collection

23

Page 24: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

G1 GC Stages - Young Collection

24

• When young regions are full and no further allocations can happen

• Need to start a stop-the-world collection

• Age objects in survivor regions

• Promote aged objects into the old regions

Page 25: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Marking Threshold

25

Page 26: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Initiating Heap Occupancy Percent

26

• Threshold to start a marking cycle to identify candidate old regions for collection during a mixed/incremental collection

• When old generation occupancy crosses this adaptive threshold, a marking cycle can start

Page 27: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Marking

27

Page 28: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Stages of Marking

28

• Initial-mark

• Root region scan

• Concurrent mark

• Remark/ Final mark

• Cleanup

Page 29: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

A Mixed Collection

29

Page 30: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

G1 GC Stages - Mixed Collection

30

• When candidate old regions are available and the potential of recovered space is over the (internal) reclaimable threshold

• This is a stop-the-world collection

• All regions in the young generation are included in this collection

• Candidate old regions are added based on the reclaimable space and other min/max thresholds

• Can have multiple mixed collection pauses based on the total old regions identified and a (internal) count target.

Page 31: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

G1 GC Pause Histogram

31

Page 32: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

The Garbage First Collector - Pause Histogram

32

Paus

e tim

e in

milli

seco

nds

0

30

60

90

120

Timestamps3415 3416.3 3417.2 3418.4 3419 3422 3423.4 3432.2 3433.2 3436.8 3437.6 3438.9 3440

Young Collection Initial Mark Remark Cleanup Mixed Collection

Initiating Heap Occupancy Percent

Page 33: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Concurrent Marking in G1 GC

33

Page 34: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Recap: Stages of Marking

34

• Initial-mark

• Root region scan

• Concurrent mark

• Remark/ Final mark

• Cleanup

Page 35: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Marking Highlights

• Employs ‘Snapshot At The Beginning (SATB) algorithm

• Incremental and concurrent marking algorithm.

• A pre-write barrier is needed to gather the snapshot

35

Page 36: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

SATB Algorithm For Concurrent & Incremental

Marking

36

Page 37: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram37

A Java Heap

End

Bottom

~ ~ ~ ~

Page 38: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram38

A Java Heap With Previous Bitmap

End

Bottom

Previous Bitmap

~ ~ ~ ~

Page 39: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram39

A Java Heap (Showing PTAMS) With Previous Bitmap

End

Bottom

Previous Bitmap

PTAMS

Top

~ ~ ~ ~

Page 40: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram40

A Java Heap (Showing PTAMS) With Previous Bitmap

End

Bottom

Previous Bitmap

PTAMS

Objects Are Already Marked

Top

~ ~ ~ ~

Page 41: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram41

A Java Heap (Showing PTAMS + NTAMS) With Previous Bitmap

Top

Bottom

Next Bitmap

PTAMS

NTAMS

End~ ~ ~ ~

Page 42: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram42

Same Java Heap During Concurrent Marking

End

Bottom

PTAMS

NTAMSNew Objects Are

Allocated In This SpaceTop ~ ~ ~ ~

Next Bitmap

Page 43: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram43

Bottom

PTAMS

NTAMS

End

Same Java Heap During Concurrent Marking

Top

Objects Will Be Considered

Implicitly Live

~ ~ ~ ~

Next Bitmap

Page 44: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

Pre-Write Barrier

44

Page 45: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Pre-Write Barrier

• To record the previous value of the reference fields of objects that were reachable at the start of marking + were a part of the snapshot.

• Prevents those objects from being overwritten by the mutator thread

• Mutator thread logs the previous value of the pointer in an SATB buffer

45

Page 46: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

Pre-Write Barrier - Pseudo Code

if (marking_is_active) {

pre_val:= x.f;

if (pre_val:= NULL) {

satb_enqueue(pre_val);

}

}

46

Page 47: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram47

End

Bottom

PTAMS

NTAMS

All Live Objects Are Marked

Next Bitmap

Top ~ ~ ~ ~

Same Java Heap At The End Of Remark Pause

Page 48: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram48

Same Java Heap At The End Of Remark Pause

End

Bottom

PTAMS

NTAMS

Top

All Objects Are Implicitly Live

~ ~ ~ ~

Next Bitmap

Page 49: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram49

Same Java Heap During Cleanup

End

Bottom

PTAMS

Top ~ ~ ~ ~

Previous Bitmap

Page 50: Write Barriers in G1 GC - Jfokus...The Garbage First Collector - Pause Histogram 32 Pause time in milliseconds 0 30 60 90 120 Timestamps 3415 3416.3 3417.2 3418.4 3419 3422 3423.4

©2017 CodeKaram

References and Additional Reading

• D. L. Detlefs, C. H. Flood, S. Heller, and T. Printezis. Garbage-First Garbage Collection.

• C. Hunt, M. Beckwith, P. Parhar, B. Rutisson. Java Performance Companion.

50