Top Banner
JVM Profiling Under da Hood Richard Warburton - @RichardWarburto Nitsan Wakart - @nitsanw
64

Jvm profiling under the hood

Jul 31, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Jvm profiling under the hood

JVM ProfilingUnder da Hood

Richard Warburton - @RichardWarburtoNitsan Wakart - @nitsanw

Page 2: Jvm profiling under the hood

Why Profile?

Lies, Damn Lies and Statistical Profiling

Under the Hood

Conclusion

Page 3: Jvm profiling under the hood
Page 4: Jvm profiling under the hood
Page 5: Jvm profiling under the hood

Measure data from your application

Page 6: Jvm profiling under the hood

Exploratory Profiling

Page 7: Jvm profiling under the hood

Execution Profiling=

Where in code is my applicationspending time?

Page 8: Jvm profiling under the hood

CPU Profiling Limitations● Finds CPU bound bottlenecks

● Many problems not CPU Bound○ Networking○ Database or External Service○ I/O○ Garbage Collection○ Insufficient Parallelism○ Blocking & Queuing Effects

Page 9: Jvm profiling under the hood

Why Profile?

Lies, Damn Lies and Statistical Profiling

Under the Hood

Conclusion

Page 10: Jvm profiling under the hood
Page 11: Jvm profiling under the hood

Different Execution Profilers● Instrumenting

○ Adds timing code to application

● Sampling○ Collects thread dumps periodically

Page 12: Jvm profiling under the hood

Sampling Profilers

WebServerThread.run()

Controller.doSomething() Controller.next()

Repo.readPerson()

new Person()

View.printHtml()

Page 13: Jvm profiling under the hood

Periodicity Bias

● Bias from sampling at a fixed interval

● Periodic operations with the same frequency as the samples

● Timed operations

Page 14: Jvm profiling under the hood

Periodicity Bias

a() ??? a() ??? a() ??? a() ???

Page 15: Jvm profiling under the hood

Stack Trace Sampling

● JVMTI interface: GetCallTrace○ Trigger a global safepoint(not on Zing)

○ Collect stack trace

● Large impact on application

● Samples only at safepoints

Page 16: Jvm profiling under the hood

Exampleprivate static void outer()

{

for (int i = 0; i < OUTER; i++)

{

hotMethod(i);

}

}

// https://github.com/RichardWarburton/profiling-samples

Page 17: Jvm profiling under the hood

Example (2)private static void hotMethod(final int i)

{

for (int k = 0; k < N; k++)

{

final int[] array = SafePointBias. array;

final int index = i % SIZE;

for (int j = index; j < SIZE; j++)

{

array[index] += array[j];

}

}

}

Page 18: Jvm profiling under the hood
Page 19: Jvm profiling under the hood

-XX:+PrintSafepointStatistics

ThreadDump 48

Maximum sync time 985 ms

Page 20: Jvm profiling under the hood

Whats a safepoint?

● Java threads poll global flag○ At ‘uncounted’ loops back edge○ At method exit/enter

● A safepoint poll can be delayed by:○ Large methods○ Long running ‘counted’ loops○ BONUS: Page faults/thread suspension

Page 21: Jvm profiling under the hood
Page 22: Jvm profiling under the hood

Safepoint Bias

WebServerThread.run()

Controller.doSomething() Controller.next()

Repo.readPerson()

new Person()

View.printHtml() ???

Page 23: Jvm profiling under the hood
Page 24: Jvm profiling under the hood

Let sleeping dogs lie?

● ‘GetCallTrace’ profilers will sample ALL

threads

● Even sleeping threads...

Page 25: Jvm profiling under the hood

This Application Mostly Sleeps

JVisualVM snapshot

Page 26: Jvm profiling under the hood

No CPU? No profile!

JMC profile

Page 27: Jvm profiling under the hood

Why Profile?

Lies, Damn Lies and Statistical Profiling

Under the Hood

Conclusion

Page 28: Jvm profiling under the hood

Honest Profiler

https://github.com/richardwarburton/honest-profiler

Page 29: Jvm profiling under the hood
Page 30: Jvm profiling under the hood

AsyncGetCallTrace

● Used by Oracle Solaris Studio

● Adapted to open source prototype by Google’s Jeremy Manson

● Unsupported, Undocumented … Underestimated

Page 31: Jvm profiling under the hood

SIGPROF - Interrupt Handlers

● OS Managed timing based interrupt

● Interrupts the thread and directly calls an event handler

● Used by profilers we’ll be talking about

Page 32: Jvm profiling under the hood

Design

Log File

Processor Thread Graphical UI

Console UI

Signal Handler

Signal Handler

Os Timer Thread

Page 33: Jvm profiling under the hood

“You are in a maze of twisty little stack frames,

all alike”

Page 34: Jvm profiling under the hood

AsyncGetCallTrace under the hood

● A Java thread is ‘possessed’

● You have the PC/FP/SP

● What is the call trace?○ jmethodId - Java Method Identifier

○ bci - Byte Code Index -> used to find line number

Page 35: Jvm profiling under the hood

Where Am I?

● Given a PC what is the current method?

● Is this a Java method?○ Each method ‘lives’ in a range of addresses

● If not, what do we do?

Page 36: Jvm profiling under the hood

Java Method? Which line?

● Given a PC, what is the current line?○ Not all instructions map directly to a source line

● Given super-scalar CPUs what does PC

mean?

● What are the limits of PC accuracy?

Page 37: Jvm profiling under the hood

“> I think Andi mentioned this to me last year -- > that instruction profiling was no longer reliable.

It never was.”

http://permalink.gmane.org/gmane.linux.kernel.perf.user/1948Exchange between Brenden Gregg and Andi Kleen

Page 38: Jvm profiling under the hood

Skid

● PC indicated will be >= to PC at sample time

● Known limitation of instruction profiling

● Leads to harder ‘blame analysis’

Page 39: Jvm profiling under the hood

Limits of line number accuracy:

Line number (derived from BCI) is the closest

attributable BCI to the PC (-XX:+DebugNonSafepoint)

The PC itself is within some skid distance from

actual sampled instruction

Page 40: Jvm profiling under the hood

● Divided into frames○ frame { sender*, stack*, pc }

● A single linked list:root(null, s0, pc1) <- call1 (root, s1, pc2) <- call2(call1, s2, pc2)

● Convert to: (jmethodId,lineno)

The Stack

Page 41: Jvm profiling under the hood

A typical stack

● JVM Thread runner infra:○ JavaThread::run to JavaCalls::call_helper

● Interleaved Java frames:○ Interpreted○ Compiled○ Java to Native and back

● Top frame may be Java or Native

Page 42: Jvm profiling under the hood

Native frames

● Ignored, but need to navigate through

● Use a dedicated FP register to find sender

● But only if compiled to do so…

● Use a last remembered Java frame insteadSee: http://duartes.org/gustavo/blog/post/journey-to-the-stack/

Page 43: Jvm profiling under the hood

Java Compiled Frames

● C1/C2 produce native code

● No FP register: use set frame size

● Challenge: methods can move (GC)

● Challenge: methods can get recompiled

Page 44: Jvm profiling under the hood

Java Interpreter frames

● Separately managed by the runtime

● Make an effort to look like normal frames

● Challenge: may be interrupted half-way

through construction...

Page 45: Jvm profiling under the hood

Virtual Frames

● C1/C2 inline code (intrinsics/other methods)

● No data on stack

● Must use JVM debug info

Page 46: Jvm profiling under the hood

AsyncGetCallTrace Limitations

● Only profiles running threads

● Accuracy of line info limited by reality

● Only reports Java frames/threads

● Must lookup debug info during call

Page 47: Jvm profiling under the hood

Compilers: Friend or Fiend?void safe_reset(void *start, size_t size) {

char *base = reinterpret_cast<char *>(start);

char *end = base + size;

for (char *p = base; p < end; p++) {

*p = 0;

}

}

Page 48: Jvm profiling under the hood

Compilers: Friend or Fiend?safe_reset(void*, unsigned long):

lea rdx, [rdi+rsi]

cmp rdi, rdx

jae .L3

sub rdx, rdi

xor esi, esi

jmp memset

.L3:

rep ret

Page 49: Jvm profiling under the hood

Concurrency Bug

● Even simple concurrency bugs are hard to spot

● Unspotted race condition in the ring buffer

● Spotted thanks to open source & Rajiv Signal

Page 50: Jvm profiling under the hood

WriterReader

Page 51: Jvm profiling under the hood

WriterReader

Page 52: Jvm profiling under the hood

Extra Credit!

Page 53: Jvm profiling under the hood

Native Profiling Tools

● Profile native methods

● Profile at the instruction level

● Profile hardware counters

Page 54: Jvm profiling under the hood

Perf

● A Linux profiling tool

● Can be made to work with Java

● JMH integration

● Ongoing integration efforts

Page 55: Jvm profiling under the hood

Solaris Studio

● Works on Linux!

● Secret Weapon!

● Give it a go!

Page 56: Jvm profiling under the hood

ZVision

● Works for Zing

● No HWC support

● Very informative

Page 57: Jvm profiling under the hood

Why Profile?

Lies, Damn Lies and Statistical Profiling

Under the Hood

Conclusion

Page 58: Jvm profiling under the hood

What did we cover?

● Biases in Profilers

● More accurate sampling

● Alternative Profiling Approaches

Page 59: Jvm profiling under the hood

Don’t just blindly trust your tooling.

Page 60: Jvm profiling under the hood

Test your measuring instruments

Page 61: Jvm profiling under the hood

Open Source enables implementation review

Page 62: Jvm profiling under the hood

Q & A@nitsanw

psy-lob-saw.blogspot.co.uk@richardwarburtoinsightfullogic.comjava8training.comwww.pluralsight.

com/author/richard-warburton

Page 63: Jvm profiling under the hood

Slides after here just for reference, don’t delete or show

Page 64: Jvm profiling under the hood