1

I want to add an option in my application where the end user can start CPU profiling, reproduce a performance issue, stop CPU profiling, and then send the profiling information to our support team as an attachment to an email. I'm hoping I can use the same mechanism that JVisualVM uses to stop and start the profiling and return the results.

Does anybody know how JVisualVM makes this happen? It doesn't require the -agentlib:hprof option to be set on the command line - does it use hprof or some other mechanism?

valiano
  • 16,433
  • 7
  • 64
  • 79
Jason
  • 321
  • 2
  • 16

2 Answers2

2

JVisualVM instruments the code by using a JMX command to add an agent dynamically. This is non-trivial to implement. You might find that JVisualVM has an API, if so I would use that rather than attempt to implement it yourself. If it doesn't have a public API and I suspect it doesn't, I suggest attempting something simpler.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Thanks for the answer - I was hoping to just call whatever methods JVisualVM calls to instrument the code and start profiling so that I didn't have to implement it myself (and just include the JVisualVM jars in my application classpath). I may start with CPU sampling which just uses the JMX connection to the process and calls ThreadMXBean.dumpAllThreads() to collect the data. If that isn't accurate enough, I may investigate full profiling further. Thanks again. – Jason Sep 18 '15 at 20:25
1

The tool uses dynamic instrumentation.

When the CPU button in Profiler tab is pressed, the profiler attaches to the application and starts profiling its performance. At the beginning the profiler needs to instrument some methods of the application, the actual number of instrumented methods is displayed in the Status area. Since the profiler uses dynamic instrumentation, the number of instrumented methods may change during the profiling session. After the profiler is attached and starts collecting the data...

https://blogs.oracle.com/nbprofiler/entry/profiling_with_visualvm_part_1

mvd
  • 2,596
  • 2
  • 33
  • 47