ZGC Concurrent Class Unloading - Oraclecr.openjdk.java.net › ~pliden › slides ›...

Post on 27-Jun-2020

3 views 0 download

Transcript of ZGC Concurrent Class Unloading - Oraclecr.openjdk.java.net › ~pliden › slides ›...

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Concurrent Class UnloadingAnother safepoint operation bites the dust

Erik ÖsterlundGarbage Collection EngineerJava Platform Group, OracleFebruary 04, 2019

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

2

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Agenda

What is ZGC? What is class unloading?

Overview of phases

Concurrent code and metadata unloading

Evaluation

Future plans

1

2

3

4

5

3

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

What is ZGC?

4

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

New Concurrent GC in JDK 11

(Experimental feature, Linux/x86_64 only)

5

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Goals

Multi-terabyte heaps Max GC pause time

Easy to tuneMax application throughput reduction

10msTB

15%

6

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

What is a concurrent GC

Serial Parallel CMS G1 ZGCMarking - - ✔* ✔* ✔

Relocation/Compaction - - - - ✔

Reference Processing - - - - ✔

Relocation Set Selection - - - - ✔

StringTable Cleaning - - - - ✔

JNI WeakRef Cleaning - - - - ✔

JNI GlobalRefs Scanning - - - - ✔

Class Unloading - - - - ✔

Thread Stack Scanning - - - - -

7

**) Old Gen strong references Only

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Concurrent Class Unloading

Released in JDK 12 for ZGC

8

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Traditional Class Unloading

• Step 1: Marking (concurrent)

–Mark metadata (classes, CLDs) when marking objects

• Step 2: Reference processing (STW)

– Need to know what is reachable from finalizers before class unloading

• Step 3: Unloading (STW)– Unload code cache

– Unload metadata

9

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Concurrnet Class Unloading

• Step 1: Marking (concurrent)

–Mark metadata (classes, CLDs) when marking objects

–Mark both strong and final reachable graphs

• Step 2: Reference processing (concurrent)– Already know what is reachable from finalizers before class unloading

• Step 3: Unloading (concurrent)

– Unload code cache

– Unload metadata

10

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Phases

ConcurrentMark/Remap

Pause Mark End Pause Relocate StartPause Mark Start

ConcurrentPrepare for Reloc.

ConcurrentRelocate

GC Cycle

11

ConcurrentReference Processing

ConcurrentClass Unloading

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Step 1: Marking

12

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Marking overview

• Color heap object pointers with appropriate marked color

– Special bit pattern for edges reachable from finalizers only

–Mutator load barriers upgrade them to strongly reachable when loaded

• Mark metadata objects similarly–Mark metadata reachable from objects, with strong/final reachability

13

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 14

Java Object

CLD Handle Area

handle

handle

Class Loader Data

Class loader

holder

handleref count Java.lang.Class

Klass

mirror

Java.lang.ClassLoader Java Object

Java Object

Metadata graph

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Step 2: Reference Processing

15

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Reference processing overview

• WeakReferences cleared if referent not strongly reachable

• PhantomReferences cleared if referent not reachable

• "Weak" VM datastructures have "phantom" strength

– Classes die if not reachable (including from finalizers)

• Each access on weak/phantom is annotated using my Access API

• A class is dead if a phantom load of its holder returns NULL

16

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Step 3: Unloading

17

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Stale Datastructures after Reference Processing

Subklass/sibling/implementor lists

Method data objects

Instance class dependency context

jli.CallSite dependency context

Class loader data graph

Protection domain cache table

Module table

String table

18

Package table

Symbol table

Resolved method table

Loader constraint table

Resolution error table

Metaspace

Code cache (JIT compiled code)

Inline caches

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Basically everything is a huge mess...

...and we just continue running anyway

19

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Unloading Overview

Pause Mark End

20

Reference Processing

Class Unloading

Unlink Purge

Han

dsh

ake

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Unloading Overview

Pause Mark End

21

Reference Processing

Class Unloading

Unlink Purge

Han

dsh

ake

Metadata Metadata

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Unloading Overview

Pause Mark End

22

Reference Processing

Class Unloading

Unlink Purge

Han

dsh

ake

Metadata Code Metadata Code

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Concurrent Code Unloading

23

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Code cache

• Colored pointers into Java heap

–Misaligned immediate values

–Which color should native compiled object references have?

– Need a way to paint native compiled object references

• Inline caches (CompiledIC) pointing at now dead native methods (nmethods), because of dead object references

– Running any such code yields crashes

– Need a way of preventing calls to dead native methods

24

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

NMethod Entry Barriers

• Arm all nmethods not on stack in GC pause

– Change global epoch value, caught with cmp; je; at verified entry

• Trap calls to armed nmethods

– NMethods are "good" or "bad" based on object pointer liveness

• When entering good nmethods– Fix up object pointers (oops)

– Disarm barrier by patching cmp immediate value

• When entering bad nmethods

– Re-resolve the call

25

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Example: Calling inline cache to dead nmethod

26

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Java Heap MetaspaceCode Heap

27

nmethod A

CompiledIC

Method

Klass

nmethod B

oop

oop

Java Object

Java Object

Java Objectoop

Method

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Java Heap MetaspaceCode Heap

28

nmethod A

CompiledIC

C2I adapter

oop

oop

Java Object

Java Object

Method

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Example: Calling inline cache to stale but live nmethod

29

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Java Heap MetaspaceCode Heap

30

nmethod A

CompiledIC

Methodnmethod B

oop

oop

Java Object

Java Object

Java Object

oop

Method

Java Object

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Java Heap MetaspaceCode Heap

31

nmethod A

CompiledIC

Methodnmethod B

oop

oop

Java Object

Java Object

Java Object

oop

Method

Java Object

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Concurrent Code Unloading

• Unlink

– Clean stale inline caches (patch machine code that Java threads run)

– Fixup object references (patching more machine code)

– Disarm entry barriers (yet some more machine code patching)

– Unlink nmethods from dependency contexts (lock-free unlinking)

– Unlink exception caches (more lock-free unlinking)

• Global rendezvous handshake

• Purge– Purge unloading nmethods with make_unloaded()

– Sweeper subsequently frees up memory in code cache

32

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Concurrent Metadata Unloading

33

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Structure

• Unlink

– Expose logically already unlinked view of data to mutators

– Subclass/sibling/implementor lists (lock-free)

–Method data object (lock-free and per-MDO lock)

– Protection domain cache (lock)

– Class loader data graph (lock)

– StringTable and SymbolTable (crazy concurrent)

• Rendezvous handshake

• Purge

– Delete Klass, Method, CLD, handles, table entries, etc.

34

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Evaluation

35

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Mode: Composite

Heap Size: 128G

OS: Oracle Linux 7.4

HW: Intel Xeon E5-2690 2.9GHz2 sockets, 16 cores (32 hw-threads)

SPECjbb®2015 is a registered trademark of the Standard PerformanceEvaluation Corporation (spec.org). The actual results are not representedas compliant because the SUT may not meet SPEC's requirements forgeneral availability.

SPECjbb®2015 – Score

36

0%

10%

20%

30%

40%

50%

60%

70%

80%

90%

100%

ZGC Parallel G1

(Higher is better)

max-JOPS (Throughput) critical-JOPS (Throughput with latency requirements)

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Mode: Composite

Heap Size: 128G

OS: Oracle Linux 7.4

HW: Intel Xeon E5-2690 2.9GHz2 sockets, 16 cores (32 hw-threads)

SPECjbb®2015 is a registered trademark of the Standard PerformanceEvaluation Corporation (spec.org). The actual results are not representedas compliant because the SUT may not meet SPEC's requirements forgeneral availability.

SPECjbb®2015 – Score

37

0%

10%

20%

30%

40%

50%

60%

70%

80%

90%

100%

ZGC Parallel G1

(Higher is better)

max-JOPS (Throughput) critical-JOPS (Throughput with latency requirements)

50%29%

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

SPECjbb®2015 – Pause Times

38

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

SPECjbb®2015 – Pause Times

39

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

SPECjbb®2015 – Pause Times

40

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

41

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

• Short-term

– Turn ZGC into a product feature

42

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

• Short-term

– Turn ZGC into a product feature

Remove experimental status

43

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

• Short-term

– Turn ZGC into a product feature

44

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

• Short-term

– Turn ZGC into a product feature

• Long-term

– Generational

– Sub-millisecond max pause times

– Additional platform support

– Graal JIT support

45

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

• Short-term

– Turn ZGC into a product feature

• Long-term

– Generational

– Sub-millisecond max pause times

– Additional platform support

– Graal JIT support

Generational• Withstand higher allocation rates• Lower heap overhead• Lower CPU usage

46

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

• Short-term

– Turn ZGC into a product feature

• Long-term

– Generational

– Sub-millisecond max pause times

– Additional platform support

– Graal JIT support

Sub-millisecond max pause times• Within reach• Reduce root set size• Time-to-Safepoint, etc

47

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

• Short-term

– Turn ZGC into a product feature

• Long-term

– Generational

– Sub-millisecond max pause times

– Additional platform support

– Graal JIT support

Additional platform support• macOS?• Windows?• Sparc?• Aarch64?

48

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Future Plans

• Short-term

– Turn ZGC into a product feature

• Long-term

– Generational

– Sub-millisecond max pause times

– Additional platform support

– Graal JIT support

49

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Get Involved!

50

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Project

zgc-dev@openjdk.java.net

http://wiki.openjdk.java.net/display/zgc/Main

Follow, Participate, Give Feedback

51

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

ZGC Project

http://hg.openjdk.java.net/jdk/jdk

http://hg.openjdk.java.net/zgc/zgc

Source Code

52

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Thanks!

53

Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |

Questions?

54