Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 736
mik01aj Avatar asked Dec 01 '14 13:12

mik01aj


1 Answers

Good news, the FlameGraph repository has "a script" to work with jstacks already in it.

https://github.com/brendangregg/FlameGraph

It's the stackcollapse-jstack.pl.

It seems that by default it expects just stack trace after stack trace in its input, and counts each one as "a sample point."

So you can just do multiple jstack's into a file (run this once or a few times, or once a second "for awhile" etc.):

jstack pid_of_your_jvm >> my_jstack

Then execute that script:

 ./stackcollapse-jstack.pl my_jstack > my_jstack.folded

and finally convert to flamegraph:

 ./flamegraph.pl --color=java my_jstack.folded > my_jstack.svg

No third party helpers required (though they may still be useful).

Note also that the stackcollapse-jstack.pl file discards non RUNNABLE threads, you may want to tweak that if you want to also include "idle" threads (typically you don't).

Apparently you could use the linux "perf" command to generate stacks for a java process, as well, see the README https://github.com/brendangregg/FlameGraph

This might include more native calls, for instance.

like image 104
rogerdpack Avatar answered Oct 03 '22 14:10

rogerdpack