Is there a way to tell from within the JVM whether a particular method has been JIT compiled?
Asked Answered
O

2

6

When writing microbenchmarks, one can observe a large difference in runtime depending on whether a method has been compiled or not. Is there a way to tell from within a program whether a particular method has been compiled? Alternatively, is there a way to request it, or to know how to warm it up adequately without any extra information about e.g. flags passed to the JVM? Obviously this will not necessarily be perfect (e.g. there may be some condition present that causes the JVM to fall back to interpreted code), but it would certainly be an improvement.

Opia answered 17/11, 2012 at 22:30 Comment(3)
You could empirically measure the execution time of the method and check the distribution of results. You will typically have a jump when it gets compiled. There was a question on SO about this methodology but I can't find it right now. Obviously, this is somewhat obtrusive.Mucoid
I'm almost positive the answer is "no, definitely not."Inhabit
@Mucoid - That only works if you are certain that the method hasn't already been compiled. Otherwise there's no jump to find. Also, robust change-detection algorithms are a pain to code.Opia
S
3

For Sun/Oracle JVM you can use the -XX:CompileThreshold=1000 setting.

This - as the official documentation states - defines:

Number of method invocations/branches before compiling

Then, just use the number to "warm up" the JVM.

You can also use the -XX:-PrintCompilation together with -XX:CompileThreshold in order to be notified (in the console) when a method is compiled.

Squabble answered 17/11, 2012 at 22:50 Comment(3)
What is a "branch", as counted by the JVM?Opia
@RexKerr -- The JVM attempts to estimate how much time is being spent in the method (and hence how much advantage there would be to compiling it), mainly by counting entries and backward branches (which would indicate loops). But there may be other factors that are included.Wilsonwilt
Well, this looks like the best answer available, unsatisfying though it may be. Thanks!Opia
W
1

I'm pretty sure you can turn on logging that will show when methods are JITCed. But I don't know of any way from within Java to tell.

And keep in mind that JIT compilation is not an event but a process -- a method may be recompiled several times, as more information about its characteristics becomes available.

Finally, note that "warming up" is iffy in the general case. While you can usually "warm up" a single method reliably, it's much harder with even a modestly large application, due to a number of factors.

(Though I don't know of any reason why the ability to read some sort of JITC status for a method could not be added to embedded debug tools.)

Added: One thing to beware of, when benchmarking code "snippets", is that the outer-most method that does all the looping is often not JITC-able (depending on how the JITC is implemented) due to the fact that it never returns and hence the JITCed version can never be called. So one should always place the "meat" of the code to be benchmarked in a separate method that is called repeatedly, vs putting the loop and the to-be-benchmarked code in the same method.

Wilsonwilt answered 17/11, 2012 at 23:14 Comment(2)
I did specify "microbenchmark". Larger applications are harder to pin down.Opia
Indeed. One also should be aware of garbage collection, whether your test case has the expected level of multiple dispatch for real code, whether results are being discarded and thus code could be elided, etc. etc...even microbenchmarks are not simple to get right!Opia

© 2022 - 2024 — McMap. All rights reserved.