The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based...

42
David F. Bacon David F. Bacon Perry Cheng Perry Cheng David Grove David Grove V.T. V.T. Rajan Rajan Martin Martin Vechev Vechev IBM T.J. Watson Research Center IBM T.J. Watson Research Center The Metronome: The Metronome: A Hard Real A Hard Real - - time time Garbage Collector Garbage Collector

Transcript of The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based...

Page 1: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

David F. BaconDavid F. Bacon

Perry ChengPerry Cheng David GroveDavid Grove

V.T. V.T. RajanRajan Martin Martin VechevVechev

IBM T.J. Watson Research CenterIBM T.J. Watson Research Center

The Metronome:The Metronome:A Hard RealA Hard Real--timetimeGarbage CollectorGarbage Collector

Page 2: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Real Time: The Final Frontier

• Real-time systems growing in importance– Becoming more complex– BMW CEO: “80% of innovation in SW”

• Programmers left behind– Still using assembler and C– Lack productivity advantages of Java– Often must co-exist w/large, non-real-time Java application

• Result– Complexity– Low reliability or very high validation cost

Page 3: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Scope

• Time-sensitive applications– Traditional “hard” real-time systems– Soft real-time systems (games, media, ...)– OS components: device drivers, fault handlers, …

• Uniprocessor– Multiprocessors rare in real-time systems– Complication: collector must be finely interleaved– Simplification: memory model easier to program

• No truly concurrent operations• Sequentially consistent

Page 4: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Metronome Project Goals

• Make GC feasible for hard real-time systems• Provide simple application interface• Develop technology that is efficient:

– Throughput, Space comparable to stop-the-world

• BREAK THE MILLISECOND BARRIER– While providing even CPU utilization

Page 5: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Where’s the Beef?

• Maximum pause time < 4 ms• Utilization > 50% ±2%• Memory requirement < 2 X max live

Page 6: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Controlling Time:Scheduling Collection

Page 7: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

The Baker Fallacy

“A real-time list processing system is one in which the time required by the elementary list processing operations…is bounded by a small constant” [Baker’78]

DeadlinFinis

Why Doesn’t It Work?

• Operations not smooth• Constant not so small• Traps are not smooth

Page 8: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Scheduling: Work vs. Time

• Baker approach is work-based– On barriers/allocations do a little GC work– For constant k, utilization may drop to 1/k

• Metronome is time-based– Tick (user), Tock (GC)– Utilization is guaranteed to be tick/(tick+tock)

• Minimum Mutator Utilization (MMU)

– Issue: can we keep up with the mutator?

Page 9: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Pause time distribution: javac

Time-based Scheduling Work-based Scheduling

0 2 4 6 8 10 12 140

100

200

300

400

500

600

Pause Time(ms)

Co

un

t

javac.time.opt: Distribution of pause time

0 5 10 15 20 25 30 350

5

10

15

20

25

30

35

40

45

50

Pause Time(ms)

Co

un

t

javac.work.opt: Distribution of pause time

12 ms 12 ms

Page 10: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Utilization vs Time: javac

0 5 10 15 20 25 300

0.2

0.4

0.6

0.8

1

Time (s)

Muta

tor

CP

U U

tilization

0 5 10 15 20 25 300

0.2

0.4

0.6

0.8

1

Time (s)

Muta

tor

CP

U U

tilization

Time (s)

0.4

0.2

0.0

0.6

0.8

1.0

Util

izat

ion

(%)

0.4

0.2

0.0

0.6

0.8

1.0

Time-based Scheduling Work-based Scheduling

0.45

Time (s)

Page 11: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Space Usage: javac

0 5 10 15 20 25 300

10

20

30

40

50

60

70

80

Time (s)

Mem

ory

(Mb)

javac.work.opt: Space usage vs time

In Use (Time−Based)In Use (Work−Based)Maximum Live DataGC trigger

Page 12: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Parameterization

Mutator

a*(∆GC)m

Collector

R

Tuner

∆tsu

Allocation Rate

Maximum LiveMemory

Collection Rate

Real TimeInterval

Maximum UsedMemory

CPU Utilization

Page 13: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Controlling Space:Layout and Compaction

Page 14: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Two Basic Organizations

• Semi-space Copying– Concurrently copies between spaces– Cost of copying, consistency

– Fragmentation 2x, but bounded

• Mark-sweep (non-copying)– Fragmentation avoidance, coalescing– Fragmentation unbounded.

Page 15: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Segregated Free-list Architecture

• Suffers from 5 (!) kinds of fragmentation– Internal and page-internal: bound (e.g. 1/8)– External: Break up large objects (arraylets)– Page external: Defragment– Size external: eat it (up to 1 page/size class)

24

16

12

Page 16: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Selective Defragmentation

• When do we move objects?– When there is fragmentation

• Usually: program exhibits locality of size (λ) – Dead objects are re-used quickly– Fragmentation is low

• But: defragment either when– Dead objects are not re-used for n GC cycles– Free pages fall below limit for performing a GC

• In practice: we move 2-3% of live data– Major improvement over copying collector– Very resilient to adversary programs

Page 17: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Metronome Adapts

• Semi-space Copying– Concurrently copies between spaces– Fragmentation 2x, but bounded

• Mark-sweep (non-copying)– Fragmentation avoidance, coalescing– Fragmentation unbounded

• The Metronome– Mark-sweep, selective defragmentation– Best of both, adaptively adjusts

Page 18: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Metronome:The Next Generation

Page 19: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Time to Get Serious

• Significant interest– But Jikes RVM is not product-level

• Pause time/MMU meets wide class of needs– But utilization during GC is an issue

• Next-generation Implementation– In IBM’s J9 product (no commitment expressed…)– Higher performance

• Generational

– Native threads (challenging)– Binary distribution to groups at Berkeley, Salzburg

Page 20: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Generational RTGC

Nursery Heapa a'

• Allocation rate is single biggest factor– Nursery reduces heap allocation rate

• Our STW GC: collects 64K in 4ms (ARM)– So, collect small nursery synchronously

• Survival rate usually < 15% (with tricks)– But predictability is harder (interface/methodology issue)

Page 21: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Conclusions

• The Metronome provides true real-time GC– First collector to do so without major sacrifice

• Short pauses (4 ms)• High MMU during collection (50%)• Low memory consumption (2x max live)

• Critical features– Time-based scheduling– Hybrid, mostly non-copying approach– Integration w/compiler

Page 22: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Future Work

• Two main goals:– Reduce pause time, memory requirement– Increase predictability

• Pause time:– Expect sub-millisecond using current techniques– For 10’s of microseconds, need interrupt-based

• Predictability– Studying parameterization of collector– Good research area

Page 23: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Backup Slides

Page 24: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Components of the Metronome

• Incremental mark-sweep collector– Mark phase fixes stale pointers

• Selective incremental defragmentation– Moves < 2% of traced objects

• Time-based scheduling• Segregated free list allocator

– Geometric size progression limits internal fragmentation

Old New

Page 25: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Support Technologies

• Read barrier: to-space invariant [Brooks]– New techniques with only 4% overhead

• Write barrier: snapshot-at-the-beginning [Yuasa]• Arraylets: bound fragmentation, large object ops

Old New

Page 26: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Limiting Internal Fragmentation

• Choose page size P and block sizes sk such that– sk = sk-1(1+ρ)– smax = P ρ

• Then– Internal and page-internal fragmentation < ρ

• Example:– P =16KB, ρ =1/8, smax = 2KB– Internal and page-internal fragmentation < 1/8

Page 27: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Write Barrier: Snapshot-at-start

• Problem: mutator changes object graph• Solution: write barrier prevents lost objects• Logically, collector takes atomic snapshot

– Objects live at snapshot will not be collected– Write barrier saves overwritten pointers [Yuasa]– Write buffer must be drained periodically

WB

A

C

BB

Page 28: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Read Barrier: To-space Invariant• Problem: Collector moves objects (defragmentation)

– and mutator is finely interleaved

• Solution: read barrier ensures consistency– Each object contains a forwarding pointer [Brooks]– Read barrier unconditionally forwards all pointers– Mutator never sees old versions of objects

A

X

Y

Z

A

X

Y

Z

A′

From-space To-space

BEFORE AFTER

Page 29: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Incremental Mark-Sweep

• Mark/sweep finely interleaved with mutator• Write barrier ensures no lost objects

– Must treat objects in write buffer as roots

• Read barrier ensures consistency– Marker always traces correct object

• With barriers, interleaving is simple

Page 30: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Pointer Fixup During Mark

• When can a moved object be freed?– When there are no more pointers to it

• Mark phase updates pointers– Redirects forwarded pointers as it marks them

• Object moved in collection n can be freed:– At the end of mark phase of collection n+1

A

X

Y

Z

A′

From-space To-space

Page 31: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Work-based Scheduling

• Trigger the collector to collect CW bytes– Whenever the mutator allocates QW bytes

0

0.2

0.4

0.6

0.8

1

0.01 10

Smooth AllocUneven AllocHigh Alloc

MM

U (C

PU U

tiliz

atio

n)

Window Size (s) - log

0

20

40

60

80

100Any

Spac

e (M

B)

Time (s)

Page 32: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Time-based Scheduling

• Trigger collector to run for CT seconds– Whenever mutator runs for QT seconds

0

0.2

0.4

0.6

0.8

1

0.01 10

AnyMM

U (C

PU U

tiliz

atio

n)

Window Size (s) - log

Spac

e (M

B)

Time (s)

0102030405060708090

100

Smooth AllocUneven AllocHigh Alloc

Page 33: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Fragmentation: ρ=1/8 vs ρ=1/2

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

db

jack

javac jes

s

mtrt

mpegau

dio

compres

s

Internal Page-Internal External Recently Dead Live

Page 34: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Read Barrier Optimizations

• Barrier variants: when to redirect– Lazy: easier for collector (no fixup)– Eager: better for performance (loop over a[i])

• Standard optimizations: CSE, code motion• Problem: pointers can be null

– Augment read barrier for GetField(x,offset):tmp = x[offset];return tmp == null ? null : tmp[redirect]

– Optimize by null-check combining, sinking

Page 35: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Read Barrier Results

• Conventional wisdom: read barriers too slow– Previous studies: 20-40% overhead [Zorn,Nielsen]

0

2

4

6

8

10

12

compr

ess

jess db

javac

mpega

udio

mtrt

jack

Geo. M

ean

LazyEager

Page 36: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Arraylets

• Large arrays create problems– Fragment memory space– Can not be moved in a short, bounded time

• Solution: break large arrays into arraylets– Access via indirection; move one arraylet at a time

• Optimizations– Type-dependent code optimized for contiguous case– Opportunistic contiguous allocation

A1 A2 A3A

Page 37: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Locality of Size: λA

lloca

ted

Free

d

Allo

cate

d

Free

d

Allo

cate

d

Free

d

• Measures reuse• Normalized: 0≤λ≤1• Segregated by sizeB

ytes

PerfectReuse

FreedReused

AllocatedReused

λ = Σi min ( fi / f, ai / a)

Page 38: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

λ in Real-time GC Context

• Mark-sweep (non-copying)

• Semi-space Copying

• The Metronome

Assumes λ=1

Assumes λ=0

Adapts as λ varies

Page 39: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

λ in Practice

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

javac db jack mtrt jess fragger

λ% Defrag

Page 40: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Triggering Collection

Free

Defrag

PA

GE

S

MS 1 DF 1 MS 2 DF 2 MS 3

TIME

Page 41: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

Factors in Space Consumption

MS 1 MS 2 MS 3DF 1 DF 2

λ

R

Page 42: The Metronome: A Hard Real-time Garbage Collector€¦ · Pause time distribution: javac Time-based Scheduling Work-based Scheduling 0 2 4 6 8 10 12 14 0 100 200 300 400 500 600 Pause

MMU: javac

10−2

10−1

100

101

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Delta t (s)

Util

izat

ion

Time−BasedWork−Based