winning with java at market open

64
©2013 Azul Systems, Inc. Winning with Java At Market Open Getting Fast & Staying Fast Gil Tene, CTO & co-Founder, Azul Systems

Upload: azul-systems-inc

Post on 17-Jul-2015

173 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Winning with Java At Market Open

Getting Fast & Staying Fast

Gil Tene, CTO & co-Founder, Azul Systems

Page 2: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Are you fast at Market Open?

Page 3: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java at Market Open

. . .

Market Open

Page 4: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Page 5: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Page 6: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Lazy loading & initialization

Page 7: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

Page 8: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

Page 9: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

Page 10: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

. . .

Page 11: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

Warmup

. . .

Page 12: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

Warmup

Deoptimization

. . .

Page 13: Winning With Java at Market Open

Speculative compiler tricks

JIT compilers can do things that static compilers can have

a hard time with…

Page 14: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Untaken path example

Page 15: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Untaken path example“Never taken” paths can be optimized away with benefits:

void computeMagnitude(int val) {if (val > 10) { bias = computeBias(val);else { bias = 1;}return Math.log10(bias + 99);

}

Page 16: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Untaken path example“Never taken” paths can be optimized away with benefits:

void computeMagnitude(int val) {if (val > 10) { bias = computeBias(val);else { bias = 1;}return Math.log10(bias + 99);

}

When all values so far were <= 10 , can be optimized to:

void computeMagnitude(int val) {if (val > 10) uncommonTrap();return 2;

}

Page 17: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Implicit Null Check example

Page 18: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Implicit Null Check exampleAll field and array access in Java is null checked

x = foo.x;

is (in equivalent required machine code):

if (foo == null) throw new NullPointerException();x = foo.x;

Page 19: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Implicit Null Check exampleAll field and array access in Java is null checked

x = foo.x;

is (in equivalent required machine code):

if (foo == null) throw new NullPointerException();x = foo.x;

But compiler can “hope” for non-nulls, and handle SEGV

<Point where later SEGV will appear to throw>x = foo.x;

Page 20: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Implicit Null Check exampleAll field and array access in Java is null checked

x = foo.x;

is (in equivalent required machine code):

if (foo == null) throw new NullPointerException();x = foo.x;

But compiler can “hope” for non-nulls, and handle SEGV

<Point where later SEGV will appear to throw>x = foo.x;

This is faster *IF* no nulls are encountered…

Page 21: Winning With Java at Market Open

Deoptimization

Page 22: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptive

Page 23: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptiveJIT Compilers are adaptive and speculative

Page 24: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptiveJIT Compilers are adaptive and speculative

Leverage observations of behavior

Page 25: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptiveJIT Compilers are adaptive and speculative

Leverage observations of behavior

Can “hope” about things and can recover if wrong

Page 26: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptiveJIT Compilers are adaptive and speculative

Leverage observations of behavior

Can “hope” about things and can recover if wrong

Warming up code is a black art

Page 27: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptiveJIT Compilers are adaptive and speculative

Leverage observations of behavior

Can “hope” about things and can recover if wrong

Warming up code is a black artRunning code long enough to compile is just the start…

Page 28: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptiveJIT Compilers are adaptive and speculative

Leverage observations of behavior

Can “hope” about things and can recover if wrong

Warming up code is a black artRunning code long enough to compile is just the start…

Deoptimization can occur at any time

Page 29: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptiveJIT Compilers are adaptive and speculative

Leverage observations of behavior

Can “hope” about things and can recover if wrong

Warming up code is a black artRunning code long enough to compile is just the start…

Deoptimization can occur at any timeoften occur after you *think* the code is warmed up.

Page 30: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Deoptimization: Adaptive compilation is… adaptiveJIT Compilers are adaptive and speculative

Leverage observations of behavior

Can “hope” about things and can recover if wrong

Warming up code is a black artRunning code long enough to compile is just the start…

Deoptimization can occur at any timeoften occur after you *think* the code is warmed up.

Deoptimization has many potential causes

Page 31: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Warmup often doesn’t cut it…

Page 32: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Warmup often doesn’t cut it…

Common Example: Trading system wants to have the first trade be fast

So run 20,000 “fake” messages through the system to warm up

let JIT compilers optimize, learn, and deopt before actual trades

Page 33: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Warmup often doesn’t cut it…

Common Example: Trading system wants to have the first trade be fast

So run 20,000 “fake” messages through the system to warm up

let JIT compilers optimize, learn, and deopt before actual trades

What really happens Code is written to do different things “if this is a fake message”

e.g. “Don’t send to the exchange if this is a fake message”

JITs optimize for fake path, including speculatively assuming “fake”

First real message through deopts...

Page 34: Winning With Java at Market Open

©2012 Azul Systems, Inc.

A few example causes for depotimization

Page 35: Winning With Java at Market Open

©2012 Azul Systems, Inc.

A few example causes for depotimization

Newly encountered code breaking prior assumptions

Page 36: Winning With Java at Market Open

©2012 Azul Systems, Inc.

A few example causes for depotimization

Newly encountered code breaking prior assumptions

First calls to method in an uninitialized class

Page 37: Winning With Java at Market Open

©2012 Azul Systems, Inc.

A few example causes for depotimization

Newly encountered code breaking prior assumptions

First calls to method in an uninitialized class

First call to a method in a not-yet-loaded class

Page 38: Winning With Java at Market Open

©2012 Azul Systems, Inc.

A few example causes for depotimization

Newly encountered code breaking prior assumptions

First calls to method in an uninitialized class

First call to a method in a not-yet-loaded class

Dynamic branch elimination hitting an unexpected path

Page 39: Winning With Java at Market Open

©2012 Azul Systems, Inc.

A few example causes for depotimization

Newly encountered code breaking prior assumptions

First calls to method in an uninitialized class

First call to a method in a not-yet-loaded class

Dynamic branch elimination hitting an unexpected path

Implicit Null Check hitting a null

Page 40: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java’s “Just In Time” Reality

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

Warmup

Deoptimization

. . .

Page 41: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

What we have

Java’s “Just In Time” Reality

Page 42: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

What we have What we want

Java’s “Just In Time” Reality

Page 43: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

What we have What we want

No Slow Ops or Trades

Java’s “Just In Time” Reality

Page 44: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Starts slow, learns fast

Lazy loading & initialization

Aggressively optimized for the common case

(temporarily) Reverts to slower execution to adapt

What we have What we want

No Slow Ops or Trades

ReadyNow! to the rescue

Java’s “Just In Time” Reality

Page 45: Winning With Java at Market Open

©2012 Azul Systems, Inc.

What can we do about it?

Page 46: Winning With Java at Market Open

©2012 Azul Systems, Inc.

What can we do about it?

Azul’s Zing JVM has a feature set called “ReadyNow!” Avoids deoptimization while keeping code fast

Allows code to reach optimized levels without requiring “exercise”

Page 47: Winning With Java at Market Open

©2012 Azul Systems, Inc.

What can we do about it?

Azul’s Zing JVM has a feature set called “ReadyNow!” Avoids deoptimization while keeping code fast

Allows code to reach optimized levels without requiring “exercise”

Learning and retaining optimizations With Zing, your next run can learn from the previous one

Page 48: Winning With Java at Market Open

©2012 Azul Systems, Inc.

What can we do about it?

Azul’s Zing JVM has a feature set called “ReadyNow!” Avoids deoptimization while keeping code fast

Allows code to reach optimized levels without requiring “exercise”

Learning and retaining optimizations With Zing, your next run can learn from the previous one

Fine grained control options Per method compiler directives

Page 49: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Logging & “replaying”optimizations

Page 50: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Logging & “replaying”optimizationsZing’s ReadyNow includes optimization logging

Records ongoing optimization decisions and stats

records optimization dependencies

Establishes “stable optimization state” at end of previous run

Page 51: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Logging & “replaying”optimizationsZing’s ReadyNow includes optimization logging

Records ongoing optimization decisions and stats

records optimization dependencies

Establishes “stable optimization state” at end of previous run

Zing can then read prior logs at startup Prime JVM with knowledge of prior stable optimizations

Optimizations are applied as their dependencies get resolved

Page 52: Winning With Java at Market Open

©2012 Azul Systems, Inc.

Logging & “replaying”optimizationsZing’s ReadyNow includes optimization logging

Records ongoing optimization decisions and stats

records optimization dependencies

Establishes “stable optimization state” at end of previous run

Zing can then read prior logs at startup Prime JVM with knowledge of prior stable optimizations

Optimizations are applied as their dependencies get resolved

ReadyNow workflow promotes confidence You’ll know if/when all optimizations have been applied

If some optimization haven’t been applied, you’ll know why…

Page 53: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java at “Load Start”

. . .

Load Start

Page 54: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java at “Load Start”

. . .

Load Start

Deoptimization

Page 55: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java at “Load Start”

. . .

Load Start

DeoptimizationReadyNow!

avoids deoptimization

Page 56: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java at “Load Start”

. . .

Load Start

With Zing & ReadyNow!

Page 57: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java at “Load Start”

. . .

Load Start

With ReadyNow!

Warmup?

Page 58: Winning With Java at Market Open

©2013 Azul Systems, Inc.

Java at “Load Start”

. . .

Load Start

With ReadyNow!

Warmup?Fast From The Start

Page 59: Winning With Java at Market Open

©2013 Azul Systems, Inc.

. . .

Load Start

With ReadyNow! and

Start Fast & Stay Fast

Java at “Load Start”

Page 60: Winning With Java at Market Open

©2013 Azul Systems, Inc.

. . .

Load Start

With Zing & ReadyNow!

Start Fast & Stay Fast

Java at “Load Start”

Page 61: Winning With Java at Market Open

©2013 Azul Systems, Inc.

. . .

Load Start

With Zing & ReadyNow!

Start Fast & Stay Fast

Java at “Load Start”

Page 62: Winning With Java at Market Open

©2013 Azul Systems, Inc.

. . .

Load Start

With Zing & ReadyNow!

Start Fast & Stay Fast

Java at “Load Start”

With Confidence

Page 63: Winning With Java at Market Open

©2013 Azul Systems, Inc.

One liner takeaway

. . .

Load Start

Page 64: Winning With Java at Market Open

©2013 Azul Systems, Inc.

One liner takeaway

. . .

Zing: A cure for the Java hiccups

Load Start