Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hitchhiker's guide to Java Flight Recorder

aragozin
October 04, 2017

Hitchhiker's guide to Java Flight Recorder

aragozin

October 04, 2017
Tweet

More Decks by aragozin

Other Decks in Programming

Transcript

  1. Hitchhiker’s Guide
    to Java Mission Control
    Alexey Ragozin
    [email protected]

    View full-size slide

  2. Profilers are Different
    Instrumentation built into JVM
    Java Visual VM
    JVMTI
    Instrumentation
    JMX

    View full-size slide

  3. Java Mission Control
    • Session Profiling – classic
    • JVM Startup Profiling
    • Continuous Profiling

    View full-size slide

  4. Java Mission Control

    View full-size slide

  5. Java Mission Control
    Do not Panic!

    View full-size slide

  6. Java Mission Control
    Most useful views
     Method Profiling
     Allocation Profiling
     Contention & Monitors Information
     Thread Execution Timeline
     Exception Log
     GC Events
     JVM Configuration Details
     IO Events

    View full-size slide

  7. Method Profiling

    View full-size slide

  8. Allocation Profiling

    View full-size slide

  9. Contention & Monitors

    View full-size slide

  10. Contention & Monitors

    View full-size slide

  11. Thread & GC Timeline

    View full-size slide

  12. Exception Log

    View full-size slide

  13. Garbage Collection Log

    View full-size slide

  14. Garbage Collection Log

    View full-size slide

  15. JVM Configuration Details

    View full-size slide

  16. JVM Configuration Details

    View full-size slide

  17. Profiling Session Configuration
     Garbage Collector
     Complier
     Method Sampling
     Thread Dump
     Exceptions
     Synchronization Threshold
     File I/O Threshold
     Socket I/O Threshold
     Heap Statistics
     Class Loading
     Allocation Profiling
    Enforces Safepoints
    May be slow if there
    are many exceptions
    Set low threshold
    for profiling
    Enforces Full GC

    View full-size slide

  18. Startup Profiling
    Prepare & Export configuration

    View full-size slide

  19. Startup Profiling
    Start JVM with options
    -XX:+UnlockCommercialFeatures -XX:+FlightRecorder
    -XX:StartFlightRecording=
    delay=1s,duration=60s,name=MyRecording,
    filename=myrecording.jfr,
    settings=C:\PathTo\MyProfiling.jfc

    View full-size slide

  20. Method Sampling
    Tracing
     Instrument and record every interesting change in observed system
    Sampling
     Capture periodic snapshots of observed system
    Task.run()
    wait() process()
    getBody()
    read() parse()
    handle()
    load()
    read()
    Time
    encode()
    wait() process()
    getBody()
    read() parse()
    handle()
    load()
    read()
    encode()
    wait() process()
    getBody()
    read() parse()
    handle()
    encode()

    View full-size slide

  21. Method Sampling
    Task.run()
    wait() process()
    getBody()
    read() parse()
    handle()
    load()
    read()
    Time
    encode()
    wait() process()
    getBody()
    read() parse()
    handle()
    load()
    read()
    encode()
    wait() process()
    getBody()
    read() parse()
    handle()
    encode()
    10 Task.run
    7
    3
    process()
    4 handle()
    3 encode()
    2 ...
    1 ...
    1 load()
    geBody()
    2
    1 read()
    parse()
    1 ...
    3 wait()

    View full-size slide

  22. Flame Graph
    Task.run()
    wait() process()
    getBody()
    read() parse()
    handle()
    load()
    read()
    Time
    encode()
    wait() process()
    getBody()
    read() parse()
    handle()
    load()
    read()
    encode()
    wait() process()
    getBody()
    read() parse()
    handle()
    encode()
    Task.run()
    process() wait()
    getBody()
    handle()
    read()
    parse()
    load()
    encode()
    SJK - https://github.com/aragozin/jvm-tools

    View full-size slide

  23. Method Sampling
    Traditional Profilers
     Dump all threads periodically – Stop The World
     JVM can suspend thread on in discrete points of execution
    Flight Recorder
     Threads sampled during execution of Java code
     No Safepoints / No Stop The World
     No samples is thread is not running!

    View full-size slide

  24. Method Sampling

    View full-size slide

  25. Method Sampling Bias
    Problem – Frequent JNI calls into InflaterInputStream
    Java Flight Recorder SJK (JMX sampling) perf *
    * https://github.com/jvm-profiling-tools/perf-map-agent
    Total samples 5653 100.00%
    ZIP 2659 47.04%
    Other 2994 52.96%
    Total samples 2905 100.00%
    ZIP 2870 98.80%
    Other 35 1.20%
    Total samples 59896 100.00%
    ZIP 54771 91.44%
    Other 5125 8.56%

    View full-size slide

  26. Method Sampling Bias
    Problem – Frequent JNI calls into InflaterInputStream
    Fix - 92% (565ms -> 45ms)
    Java Flight Recorder SJK (JMX sampling) perf *
    * https://github.com/jvm-profiling-tools/perf-map-agent
    Total samples 5653 100.00%
    ZIP 2659 47.04%
    Other 2994 52.96%
    Total samples 2905 100.00%
    ZIP 2870 98.80%
    Other 35 1.20%
    Total samples 59896 100.00%
    ZIP 54771 91.44%
    Other 5125 8.56%

    View full-size slide

  27. Method Sampling Bias
    Sampling via JMX
    Flight Recorder Sampling
    Unsafe.park()

    View full-size slide

  28. Method Sampling Bias
    Sampling via JMX
    Filter out Unsafe.park()
    Flight Recorder Sampling

    View full-size slide

  29. Method Sampling Bias
    Flight Recorder Sampling Flight Recorder Sampling
    -XX:+UnlockDiagnosticVMOptions -
    XX:+DebugNonSafepoints

    View full-size slide

  30. Execution Time Optimization
     Prove what you spend time in code not in wait
     Double check with Visual VM or other profiler
     Use contention, events and thread timeline views
     Set thresholds to 0

    View full-size slide

  31. Allocation Sampling
    Instrumentation – Normal Profilers
     Instrument every new instruction
     Expensive!
     Interfere JVM optimizations
    Flight Recorder
     Fast path – Object allocated from TLAB
     Slow path – new TLAB have to be allocated
     Sample on slow path (every few hundreds of kB)
     Non uniform sampling (TLAB size is dynamic)
    Sampled

    View full-size slide

  32. Summary
    Method sampling
    Method tracing
     Generic
     IO
    Allocation sampling
    Heap dump inspection
    Startup profiling
    Continuous profiling
    Flight Recorder
    Only Java code
    Low overhead
    No
    Built-in events
    Low overhead
    No
    Yes
    May require license
    Classic Java Profiler
    On Safepoints
    High overhead
    Yes
    -
    High overhead
    Yes
    ?
    ?

    View full-size slide

  33. KEEP CALM
    AND MAKE YOUR JAVA FAST AGAIN
    Alexey Ragozin
    [email protected]
    http://blog.ragozin.info

    View full-size slide