CodeFest 2014. Шипилёв А. — Java Benchmarking: как два таймстампа...

80
Java Benchmarking как два таймстампа прочитать! Алексей Шипилёв [email protected], @shipilev
  • date post

    20-Oct-2014
  • Category

    Internet

  • view

    646
  • download

    1

description

 

Transcript of CodeFest 2014. Шипилёв А. — Java Benchmarking: как два таймстампа...

Java Benchmarking - !

Java Benchmarking !

[email protected], @shipilev

The following is intended to outline our general product direction. Itis intended for information purposes only, and may not beincorporated into any contract. It is not a commitment to deliver anymaterial, code, or functionality, and should not be relied upon inmaking purchasing decisions. The development, release, and timingof any features or functionality described for Oracles productsremains at the sole discretion of Oracle.

Slide 2/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

Slide 3/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

String?

long startTime = System.nanoTime ();for (int i = 0; i < 1000; i++) {

String s = new String("");}long stopTime = System.nanoTime ();System.out.println("Time:" + (stopTime - startTime ));

Slide 4/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

Slide 5/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: ?

1. : 2. : ,

3. :

,

4. : , ,

Slide 6/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

Computer Language Benchmarks Game:1

: e.g. AOT vs. JIT : e.g. pidigits GMP

, -:

, CLBG ,

1http://benchmarksgame.alioth.debian.org/Slide 7/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

SPEC benchmarks: , /

, , ..

, ,

Slide 8/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

If you cant measure it, you cant optimize it , ,

, ,

Slide 9/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

Science Town PD: To Explain and Predict

, ,

,

Slide 10/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: ?

1. : 2. : ,

3. :

,

4. : , ,

Slide 11/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

:

1. : ? ?

2. : ? ? ?

3. : ? , ?

Slide 12/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

:

Bandwidth ()

steadystate

active-idle

Latency () latency

1

Slide 13/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: bandwidth latency

: !

: ,

=1

, ! 1 ? .

Slide 14/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

Slide 15/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

:

Time-based

Fixed work

!

Slide 16/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: Transients

.

.

.

! : C PrintCompilation. WTF?

Slide 17/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: Transients

.

.

. !

: C PrintCompilation. WTF?

Slide 17/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: Transients

.

.

. ! : C PrintCompilation. WTF?

Slide 17/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: Steady state

(steady state),

steady stateSlide 18/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: steady state !

public class FibonacciGen {BigInteger n1 = ONE; BigInteger n2 = ZERO;public BigInteger next() {

BigInteger cur = n1.add(n2);n2 = n1; n1 = cur;return cur;

}}

next() N transients!

Slide 19/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

?

:1. ?2. , ?3. , ?

Slide 20/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

?

, :1. : ?2. :

, ?3. :

, ?Slide 21/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

Slide 22/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: JMH

:http://openjdk.java.net/projects/code-tools/jmh/

JMH is Serious Business: VM- , VM , JMH

JMH, .

Slide 23/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: JMH

:http://openjdk.java.net/projects/code-tools/jmh/

JMH is Serious Business: VM- , VM , JMH

JMH, .

Slide 23/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

,

:

() () ()

Slide 24/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: (A)

. , 4 CPU.Threads Ops/nsec Scale

1 3.06 0.102 5.72 0.10 1.87 0.034 5.87 0.02 1.91 0.03

1: 2 4 ? 2: 1 2 1.87x?

Slide 25/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: (A)

. , 4 CPU.Threads Ops/nsec Scale

1 3.06 0.102 5.72 0.10 1.87 0.034 5.87 0.02 1.91 0.03

1: 2 4 ?

2: 1 2 1.87x?

Slide 25/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: (A)

. , 4 CPU.Threads Ops/nsec Scale

1 3.06 0.102 5.72 0.10 1.87 0.034 5.87 0.02 1.91 0.03

1: 2 4 ? 2: 1 2 1.87x?

Slide 25/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Power management

,+ CPU 2 GHz:

Threads Ops/nsec Scale1 1.97 0.022 3.94 0.05 2.00 0.024 4.03 0.04 2.04 0.02

Slide 26/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Power management

power-vs-performance

(Ex.: cpufreq, SpeedStep, Cool&Quiet, TurboBoost)

: : PM, CPU JMH: ,

Slide 27/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: OS Schedulers

OS affinity-vs-power

(Ex.: Solaris schedulers, Linux power-efficient taskqueues)

: CPU : JMH: ,

Slide 28/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Time Sharing

.

(Ex.: )

: / , ,

: ,

JMH: synchronize iterationsSlide 29/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Time Sharing, #2

JMH synchronize iterations:

Slide 30/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Time Sharing, Quiz (B)

public void measure () {long startTime = System.nanoTime ();while(! isDone) {

work ();}println(System.nanoTime () - startTime );

}

Slide 31/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Time Sharing, (B)

Is there a problem, officer?public void measure () {

long realTime = 0;while(! isDone) {

setup (); // skip thislong time = System.nanoTime ();

work ();realTime += (System.nanoTime () - time);

}println(realTime );

}

Slide 32/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Time Sharing, (B)

, ?

0

200

400

600

0 10 20 30# Threads

thro

ughp

ut, o

ps/u

s

Timing the entire loop Timing the sum[iterations]

... CPU WTF?!

Slide 33/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Time Sharing, (B)

public void measure () {long startTime = System.nanoTime ();long realTime = 0;while(! isDone) {

setup (); // skip thislong time = System.nanoTime ();

work ();realTime += (System.nanoTime () - time);...WHOOPS, WE DE-SCHEDULE HERE...

}println(realTime );println(System.nanoTime () - startTime );

}

Slide 34/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

System: Time Sharing

: !

: ! JMH:

Slide 35/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (C)

@GenerateMicroBenchmarkpublic void baseline () {}

0.5 0.1 ns

@GenerateMicroBenchmarkpublic void measureWrong () {

Math.log(x);}

0.5 0.1 ns

@GenerateMicroBenchmarkpublic double measureRight () {

return Math.log(x);}

34.0 1.0 ns

Slide 36/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: Dead-code elimination

.

: : (,, , ..)

JMH: API

Slide 37/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (D)@GenerateMicroBenchmarkpublic void baseline () {}

0.5 0.1 ns

@GenerateMicroBenchmarkpublic double measureWrong () {

return Math.log (42);}

1.0 0.1 ns

private double x = 42;@GenerateMicroBenchmarkpublic double measureRight () {

return Math.log(x);}

34.0 1.0 ns

Slide 38/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: Constant folding, etc.

.

: : JMH: API

Slide 39/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: CSE

JMH @GMB

double x;

@GenerateMicroBenchmarkdouble doWork () {

doStuff(x);}

volatile boolean done;void doMeasure () {

while (!done) {doWork ();

}}

(: !)

Slide 40/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: DCE, CSE... !

, . .

Slide 41/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: DCE, CSE... !

, . .

Slide 41/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: DCE, CSE... !

, . .

Slide 41/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (E)// changing N, will performance differ?static int N = 100;

@GenerateMicroBenchmarkpublic int test() { return doWork(N); }

int x = 1, y = 2;

private int doWork(int reps) {int s = 0;for (int i = 0; i < reps; i++)

s += (x + y);return s;

}Slide 42/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (E), #2

N ns/call ns/add1 1.5 0.1 1.5 0.110 2.0 0.1 0.1 0.01100 2.7 0.2 0.05 0.021000 68.8 0.9 0.07 0.0110000 410.3 2.1 0.04 0.01100000 3836.1 40.6 0.04 0.01

?0.04 ns/add 25 adds/ns GTFO!

Slide 43/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (E), #2

N ns/call ns/add1 1.5 0.1 1.5 0.110 2.0 0.1 0.1 0.01100 2.7 0.2 0.05 0.021000 68.8 0.9 0.07 0.0110000 410.3 2.1 0.04 0.01100000 3836.1 40.6 0.04 0.01

?0.04 ns/add 25 adds/ns GTFO!

Slide 43/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: Loop unrolling

.

: , . ns, [0; +)

: ,

JMH: , CSE/DCE

Slide 44/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (F)

interface M {void inc();

}

abstract class AM implements M {int c;void inc() {

c++;}

}

class M1 extends AM {}class M2 extends AM {}

Slide 45/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (F), #2

M m1 = new M1();M m2 = new M2();

@GenerateMicroBenchmarkpublic void testM1 () { test(m1); }

@GenerateMicroBenchmarkpublic void testM2 () { test(m2); }

void test(M m) {for (int i = 0; i < 100; i++)

m.inc();}

Slide 46/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (F), #3

test ns/optestM1 4.6 0.1testM2 36.0 0.4

repeat testM1 35.8 0.4forked testM1 4.5 0.1forked testM2 4.5 0.1

Slide 47/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (F), #3

test ns/optestM1 4.6 0.1testM2 36.0 0.4

repeat testM1 35.8 0.4

forked testM1 4.5 0.1forked testM2 4.5 0.1

Slide 47/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (F), #3

test ns/optestM1 4.6 0.1testM2 36.0 0.4

repeat testM1 35.8 0.4forked testM1 4.5 0.1forked testM2 4.5 0.1

Slide 47/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: Profile feedback

(Ex.: call profile, type profile, CHA info)

: VM VM

: , JVM

JMH: bulk warmup; forkingSlide 48/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (G)

Slide 49/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: (G), #2

Slide 50/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: Run-to-run variance

!

(Ex.: memory allocators, profiler counters, non-fair locks, concurrentdata structures, ...)

: () ()

: (e.g. ), JVM

JMH: JVMSlide 51/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: Inlining budgets

(inlining) -

: , ,

: , , -XX:+PrintInlining,

JMH: , @CompileControl

Slide 52/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

VM: JMH

JIT- :

public void testLong_loop(Loop loop , Result r, MyBenchmark bench) {

long ops = 0;r.start = System.nanoTime ();do {

bench.testLong (); // @GenerateMicroBenchmarkops ++;

} while (!loop.isDone );r.end = System.nanoTime ();r.ops = ops;

}

Slide 53/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (H)@Statepublic class TreeMapBench {

Map map = new TreeMap ();

@Setuppublic void setup() { populate(map); }

@GenerateMicroBenchmarkpublic void test(BlackHole bh) {

for(String key : map.keySet ()) {String value = map.get(key);bh.consume(value );

}}

}Slide 54/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (H), #2@GenerateMicroBenchmarkpublic void test(BlackHole bh) {

for(String key : map.keySet ()) {String value = map.get(key);bh.consume(value );

}}

Exclusive SharedThroughput, op/sec 615 12 828 21

Threads 4 4Maps 4 1

Footprint, Kb 1024 256

Slide 55/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (H), #2@GenerateMicroBenchmarkpublic void test(BlackHole bh) {

for(String key : map.keySet ()) {String value = map.get(key);bh.consume(value );

}}

Exclusive SharedThroughput, op/sec 615 12 828 21

Threads 4 4

Maps 4 1Footprint, Kb 1024 256

Slide 55/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (H), #2@GenerateMicroBenchmarkpublic void test(BlackHole bh) {

for(String key : map.keySet ()) {String value = map.get(key);bh.consume(value );

}}

Exclusive SharedThroughput, op/sec 615 12 828 21

Threads 4 4Maps 4 1

Footprint, Kb 1024 256

Slide 55/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (H), #2@GenerateMicroBenchmarkpublic void test(BlackHole bh) {

for(String key : map.keySet ()) {String value = map.get(key);bh.consume(value );

}}

Exclusive SharedThroughput, op/sec 615 12 828 21

Threads 4 4Maps 4 1

Footprint, Kb 1024 256Slide 55/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: Cache capacity

. !

: , ,

: ; ; shared/unshared

JMH: @State sharing

Slide 56/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (I)

?@State(Scope.Benchmark) class Shared {

final int[] c = new int [64];}

@State(Scope.Thread) class Local {static final AtomicInteger COUNTER = ...;final int index = COUNTER.incrementAndGet ();

}

@GenerateMicroBenchmarkvoid work(Shared s, Local l) {

s.c[l.index ]++;}Slide 57/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (I), #2

Threads Average ns/call Hit1 2.0 0.12 18.5 2.4 9x4 32.9 6.2 16x8 85.4 13.4 42x16 208.9 52.1 104x32 464.2 46.1 232x

?

Slide 58/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: Bulk method transfers

. : 32, 64, 128

.

: (false sharing)

: , subclass juggling, @Contended JMH: ,@State-

Slide 59/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (J)2

Exhibit B. Exhibit P.int sum = 0;for (int x : a) {

if (x < 0) {sum -= x;

} else {sum += x;

}}return sum;

int sum = 0;for (int x : a) {

sum += Math.abs(x);}return sum;

?2Credits: Sergey Kuksenko (@kuksenk0)

Slide 60/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (J)

E. Branched E. PredicatedL0: mov 0xc(%ecx ,%ebp ,4),%ebx

test %ebx,%ebxjl L1add %ebx ,%eaxjmp L2

L1: sub %ebx ,%eaxL2: inc %ebp

cmp %edx ,%ebpjl L0

L0: mov 0xc(%ecx ,%ebp ,4),%ebxmov %ebx ,%esineg %esitest %ebx ,%ebxcmovl %esi,%ebxadd %ebx ,%eaxinc %ebpcmp %edx ,%ebpjl Loop

?

Slide 61/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: (J)

Regular Pattern = (+, )*

NHM Bldzr C-A9 SNBbranch_regular 0.9 0.8 5.0 0.5branch_shuffled 6.2 2.8 9.4 1.0branch_sorted 0.9 1.0 5.0 0.6predicated_regular 2.0 1.0 5.3 0.8predicated_shuffled 2.0 1.0 9.3 0.8predicated_sorted 2.0 1.0 5.7 0.8

time, nsec/op

Slide 62/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

CPU: Branch Prediction

Out-of-Order . (99%+)

!

: ,

: ! ,

Slide 63/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

Slide 64/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

: Benchmarking is Serious Business

.

,

Slide 65/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

1.

2. , ,

3. , , I/O peer review

Slide 66/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

:

1. JMH: http://openjdk.java.net/projects/code-tools/jmh/

2. VisualVM, JRockit Mission Control, Oracle Studio PerformanceAnalyzer

top, vmstat, mpstat, iostat, dtrace, strace3.

-XX:+PrintAssembly https:

//wikis.sun.com/display/HotSpotInternals/PrintAssembly

Slide 67/67. Copyright c 2013, Oracle and/or its affiliates. All rights reserved.

SystemVMCPU