1

External tools are giving me trouble. Is there a way to get simple cpu usage/time spent per function without the use of some external gui tool?

I've been trying to profile my java program with VisualVM, but I'm having terrible, soul crushing, ambition killing results. It will only display heap usage, what I'm interested in is CPU usage, but that panel simply says Not supported for this JVM. Doesn't tell me which JVM to use, by the way. I've download JDK 6 and launched it using that, I made sure my program targets the same VM, but nothing! Still the same, unhelpful error message. My needs are pretty simple. I just want to find out where the program is spending its time. Python has an excellent built in profiler that print out where time was spent in each function, both with per call, and total time formats. That's really the extent of what I'm looking for right now. Anyone have any suggestions?

Zack Yoshyaro
  • 2,056
  • 6
  • 24
  • 46
  • I don't see any way for you to do this ***without*** an external tool. Are you really looking only for that, or just recommendations for ones that'll do what you're asking, that are not VisualVM? – sharakan Feb 27 '13 at 21:04
  • @sharakan - Well, I'll settle for what works. I had hopes for something "pythonic" where `main()` is an argument to the profiler, which then simply executes the program, and prints out the results, but if such a thing doesn't exist, I'm open to suggestions for whatever works. – Zack Yoshyaro Feb 27 '13 at 21:07
  • Possible workaround using a visualvm plugin: http://stackoverflow.com/questions/1755819/cpu-and-profiling-not-supported-for-remote-jvisualvm-session – sharakan Feb 27 '13 at 21:21

1 Answers1

2

It's not pretty, but you could use the built in hprof profiling mechanism, by adding a switch to the command line.

-Xrunhprof:cpu=times

There are many options available; see the Oracle documentation page for HPROF for more information.

So, for example, if you had an executable jar you wanted to profile, you could type:

java -Xrunhprof:cpu=times -jar Hello.jar

When the run completes, you'll have a (large) text file called "java.hprof.txt".

That file will contain a pile of interesting data, but the part you're looking for is the part which starts:

CPU TIME (ms) BEGIN (total = 500) Wed Feb 27 16:03:18 2013
rank   self  accum   count trace method
   1  8.00%  8.00%    2000 301837 sun.nio.cs.UTF_8$Encoder.encodeArrayLoop
   2  5.40% 13.40%    2000 301863 sun.nio.cs.StreamEncoder.writeBytes
   3  4.20% 17.60%    2000 301844 sun.nio.cs.StreamEncoder.implWrite
   4  3.40% 21.00%    2000 301836 sun.nio.cs.UTF_8.updatePositions

Alternatively, if you've not already done so, I would try installing the VisualVM-Extensions, VisualGC, Threads Inspector, and at least the Swing, JVM, Monitor, and Jvmstat Tracer Probes.

Go to Tools->Plugins to install them. If you need more details, comment, and I'll extend this answer further.

Greg Jandl
  • 831
  • 9
  • 12
  • Arg.. I don't get it. I tried using your suggestion, as well as several from the sire you linked, and all it ever says is `Dumping CPU usage by sampling running threads ... done.` – Zack Yoshyaro Feb 27 '13 at 21:51