views:

436

answers:

7

I'm looking for a way to measure the cpu usage for different methods in my java code. I understand that this can be achieved using JNI and C, but I wouldn't know where to start...

The purpose of this is to compare different algorithms, and provide qualitative results.

A: 

If you want to use one of already available profiling tools, then you can try Shark

Aviator
+1  A: 

I don't think you can really identify CPU usage down to the method level with the current range of profilers. For most methods it's pretty obvious (if the method is compute-bound and single-threaded then it'll use 100% of CPU subject to allocation by the OS).

You may want to identify hot-spots though (methods consuming more CPU than you'd anticipate - or possibly less?) and I'd recommend looking at YourKit for an easy-to-configure profiler.

Failing that, take a look at the JVM Profiling Interface (JVMPI), which may give you some further pointers.

Brian Agnew
A: 

Have a look at the OperatingSystemMXBean perhaps you could look at something before and after your method

long startProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();
long endProcessCpuTime = operatingSystemMXBean.getProcessCpuTime();

Java6 only

Paul Whelan
+1  A: 

Probably the most common way is to use sampling. The JVM provides facilities to ask it the current stack trace of all threads (or ones you're interested in), along with how much CPU they've consumed. So you periodically do this. On each call, if a thread is inside the method you're interested in, then assume that it's spent half of the reported CPU time since the last poll inside that method.

If this method sounds appropriate, a little while back I wrote some material on the Java 5 profiling facilities that might help you.

Java 5 also provides an Instrumentation framework, by which you can doctor classes as they're being loaded in to include calls on the entry and exit to your given method, so you can measure CPU usage just inside that method. However, this is a little more complex to program because you need to doctor the actual class binaries as they're being loaded.

Neil Coffey
++ If on, say, 2 out of 5 samples, you see it doing some particular thing that isn't *really* necessary, you've found a gold nugget. If you fix it you can expect a time reduction between 1/5 and 3/5. http://stackoverflow.com/questions/375913/what-can-i-use-to-profile-c-code-in-linux/378024#378024
Mike Dunlavey
... Although I look askance at things like "how much CPU time a method has consumed", because I think what really matters is how much inclusive wall clock time a method is responsible for, as a percent of total time, and I think the same statistic for the statements within it is even more important.
Mike Dunlavey
Yes, agreed, if you don't actually need to distinguish between CPU time and wall clock time (sometimes you might, though), then you don't even need to worry about the CPU measurement: you can just assume that a method at the top of the stack in on average 1 out of 20 "ticks" is taking approximately a 20th of the time.
Neil Coffey
@Neil: Yes, and the same is true if the method is *not* at the top of the stack. And the same is also true not only for the method, but for the statement (within the method) that is on the stack. Much performance tuning lore seems to assume that there is no such thing as an avoidable function call. In my experience, that's where the money is. Not to belabor, but here's a short explanation: http://stackoverflow.com/questions/406760/whats-your-most-controversial-programming-opinion/1562802#1562802
Mike Dunlavey
+1  A: 

Sun VisualVM is integrated in recent JDK's and its profiling capabilities are explained here. Note that it seems to require a pretty up to date version of OSX if I understand this correctly.
Netbeans has basically the same profiling machinery on board, I don't know if that's of any help.

fvu
A: 

I I'm not sure if this is what you want but I've used jrat for profiling in the past with decent results.

wds
A: 

Check JaMon. Is very easy to use.