Search code examples
javaprofilingvisualizationstack-trace

How to get Java profiling dump for creating flame graphs on the mac?


I'd like to collect stacktraces from my Java app for creating CPU Flame Graphs for profiling.

This is very similar to this question: How to get complete stack dump from profiler in every sample for use in flame graph? with 2 differences:

  1. I work with Java code and I need Java stacktraces
  2. I'm working on Mac (this means there is no pref and AFAIK dtrace on OSX doesn't support jstackextension).

I have already tried lightweight-java-profiler and Honest profiler, and both of them don't seem to work on Mac. I also Tried VisualVM, but I couldn't get it to produce the stacktrace dumps that I needed.

First prioirty for me are flame graphs generated from Java stacktraces, but having the native call stack as well would be great, because it would let me address the I/O issues (and maybe even generate hot/cold flame graphs).


Solution

  • I created 2 little shell scripts based on @cello's answer. They generate hot/cold flame graphs.

    Get them from this Gist.

    Usage:

    ps ax | grep java # find the PID of your process
    ./profile.sh 20402 stacks.txt
    ./gen.sh stacks.txt
    

    Alternatively, to measure application from startup (in this, case, my gradle build that also needed to be run in another directory and with some input stream) I used:

    cd ../my-project; ./gradlew --no-daemon clean build < /dev/zero &; cd -; ./profile.sh $! stacks.txt
    ./gen.sh stacks.txt
    

    Results:

    flame graphs

    In this example, I can clearly see that my application is I/O bound (notice blue bars on top).