Estimate project
← All posts

Getting Started with JVM Method Profiling

How big a method compiled by the JIT compiler gets depends on how much profiling data it contains. What does that really mean? This article answers that question and describes how the profiling mechanism works in general.

11 min read
JavaJVMJava Internals

How big a method compiled by the JIT compiler gets depends on how much profiling data it contains. What does that really mean, though? This article answers that question and also describes how the profiling mechanism works in general.

Experiment

Environment

I did my research on a VM on GCP, running Debian GNU/Linux 12 (bookworm) on the amd64 architecture. The installed Java version was Temurin-25+36 (build 25+36-LTS). To look into the state of the JVM internal objects living in Metaspace, I used the jhsdb utility.

Running the program

The program used in the experiment is essentially a simple loop, shown below:

static void main(String[] args) throws InterruptedException {
    var nrOfIterations = Integer.parseInt(args[0]);
    var sleepInMillis = Integer.parseInt(args[1]);
    var argumentHolder = new ArgumentsHolder(nrOfIterations, sleepInMillis);
    var sum = 0;
    for (int i = 1; i <= argumentHolder.getNrOfIterations(); i++) {
        IO.println("This is: " + i + " iteration");
        Thread.sleep(resolveSleepInMillis(i, argumentHolder.getSleepInMillis()));
        sum += resolveNumber(i);
    }
    IO.println("My fantastic sum is: " + sum);
}

The Thread.sleep() call is there so that I can pause the JVM at particular moments.

I started the program with this command:

java -XX:+PrintCompilation -jar target/app.jar 100000 100

I added the PrintCompilation flag to the run command, because it shows me when particular methods get compiled by the JIT compiler or decompiled back to interpreter mode.

To attach a command-line debugger to the running JVM, I first had to find out its process ID. With the pid in hand, I could run:

jhsdb clhsdb --pid <pid>

Once the debugger was attached, I could view method details, but a few preliminary steps were needed first.

  • Finding the class memory pointer from the fully qualified class name:
class org.zygiert.Main
  • This command gave back the pointer to the actual InstanceKlass object in Metaspace
  • Using it, I could run:
inspect <memory_address>
  • This command gave back detailed information about the class. The most important part was the pointer to the methods array:
Array<Method*>* InstanceKlass::_methods: Array<Method*> @ 0x00007f307c400400
  • To view the array’s actual contents, I ran the command below (the 5 means five memory words are shown):
mem 0x00007f307c400400/5
  • The output was as follows (the first line gives the array size, and the next four are consecutive pointers to this class’s methods):
0x00007f307c400400: 0x0000000000000004
0x00007f307c400408: 0x00007f307c400478
0x00007f307c400410: 0x00007f307c4005c0
0x00007f307c400418: 0x00007f307c400768
0x00007f307c400420: 0x00007f307c400698

This gave me some interesting insights into how the method metadata evolved while the application was running. I used the inspect command mentioned earlier. The same steps can be repeated for any class in the codebase. In my case, there was one more class I was especially interested in.

Tier 0

My program first paused at the 235th iteration. Knowing the memory address of every method, I could reach the method metadata. For the main method, it looked like this:

Type is Method (size of 88)
ConstMethod* Method::_constMethod: ConstMethod @ 0x00007f307c4004d0
MethodData* Method::_method_data: MethodData @ null
MethodCounters* Method::_method_counters: MethodCounters @ 0x00007f307c4009a0
AccessFlags Method::_access_flags: 8
int Method::_vtable_index: -2
u2 Method::_intrinsic_id: 0
nmethod* Method::_code: nmethod @ null
address Method::_i2i_entry: address @ 0x00007f307c4005f8
address Method::_from_compiled_entry: address @ 0x00007f307c400600
address Method::_from_interpreted_entry: address @ 0x00007f307c400610

From the profiling point of view, several important things can be noticed:

  • At this stage there is no reference to MethodData.
  • A MethodCounters reference exists, which shows that profiling is already under way.
  • There is no reference to compiled machine code – the nmethod pointer is null, meaning the method is running in interpreted mode for now.

Now let’s look at the MethodCounters objects. Below is the output for three methods:

  • main:
Type is MethodCounters (size of 64)
int MethodCounters::_invoke_mask: 254
int MethodCounters::_backedge_mask: 2046
u2 MethodCounters::_interpreter_throwout_count: 0
u2 MethodCounters::_number_of_breakpoints: 0
InvocationCounter MethodCounters::_invocation_counter: 2
InvocationCounter MethodCounters::_backedge_counter: 468
  • resolveNumber:
Type is MethodCounters (size of 64)
int MethodCounters::_invoke_mask: 254
int MethodCounters::_backedge_mask: 2046
u2 MethodCounters::_interpreter_throwout_count: 0
u2 MethodCounters::_number_of_breakpoints: 0
InvocationCounter MethodCounters::_invocation_counter: 468
InvocationCounter MethodCounters::_backedge_counter: 0
  • resolveSleepInMillis:
Type is MethodCounters (size of 64)
int MethodCounters::_invoke_mask: 254
int MethodCounters::_backedge_mask: 2046
u2 MethodCounters::_interpreter_throwout_count: 0
u2 MethodCounters::_number_of_breakpoints: 0
InvocationCounter MethodCounters::_invocation_counter: 470
InvocationCounter MethodCounters::_backedge_counter: 0

This also tells us something interesting about how profiling works. For the main method, the invocation counter is 2 and the backedge counter is 468. It shows that in interpreted mode, the JVM collects information about:

  • How many times a method gets invoked – this is the invocation counter
  • How many times a loop inside that method iterates – this is the backedge counter

So why, at the 235th iteration, is the backedge counter 468 rather than 235? Because the JVM increases counters by 2, not by 1. The value is 468 and not 470 because we are halfway through an iteration step, so the backedge counter hasn’t yet been bumped to 470.

Coming back to the original question, the JVM increases counters by 2 for the sake of performance. The counter value is kept as an integer, which is 32 bits long. The first bit is called the carry bit. Incrementing by 2 leaves the carry bit untouched.

This matters because, to decide whether a method should be checked for compilation to a higher Tier, the JVM performs a bitwise AND on the counter value and the mask, which is cheaper than an arithmetic comparison. Having a carry bit makes this possible.

What’s more, the JVM doesn’t check after every increment whether a method should be compiled to a higher Tier. How often these checks happen is instead governed by the invoke_mask and backedge_mask values. In interpreter mode, they are derived from Tier0InvokeNotifyFreqLog (default value: 7) and Tier0BackedgeNotifyFreqLog (default value: 10). The code that performs the actual calculation can be found in methodCounters.cpp.

In our example, with an invoke_mask of 254, the checks for compiling to a higher Tier happen every 128 invocations of a method. For backedges, the checks happen every 1024 backedges.

Tier 1

In my code, I deliberately added the ArgumentsHolder class with two getters to show that Tier 1 has no profiling data. For both getters, the counters look like this:

InvocationCounter MethodCounters::_invocation_counter: 256
InvocationCounter MethodCounters::_backedge_counter: 0

The counters stopped being bumped after 128 method invocations. By then, the C1 compiler has compiled the getters. We can confirm this by looking at the method metadata. For getNrOfIterations, it looks like this:

Type is Method (size of 88)
ConstMethod* Method::_constMethod: ConstMethod @ 0x00007f307c400c60
MethodData* Method::_method_data: MethodData @ null
MethodCounters* Method::_method_counters: MethodCounters @ 0x00007f307c400ec8
AccessFlags Method::_access_flags: 2
int Method::_vtable_index: -2
u2 Method::_intrinsic_id: 0
nmethod* Method::_code: nmethod @ 0x00007f30a3ecc788
address Method::_i2i_entry: address @ 0x00007f307c400ce8
address Method::_from_compiled_entry: address @ 0x00007f307c400cf0
address Method::_from_interpreted_entry: address @ 0x00007f307c400d00

This lets us notice the following:

  • Just as in interpreter mode, there is no reference to MethodData.
  • The nmethod isn’t null, which means machine code for this method is stored in the code cache. That machine code, however, contains no extra instrumentation for incrementing counters.

It’s worth noting that jhsdb can also be used to get disassembled machine code. All it takes is the method’s memory address, after which we can run this command:

disassemble <memory_address>

Tier 3

The following pause happened at the 314th iteration. By now, the C1 compiler has also compiled the resolveNumber method. Looking at the method metadata, we see details like these:

Type is Method (size of 88)
ConstMethod* Method::_constMethod: ConstMethod @ 0x00007f307c400638
MethodData* Method::_method_data: MethodData @ 0x00007f307c401928
MethodCounters* Method::_method_counters: MethodCounters @ 0x00007f307c400f88
AccessFlags Method::_access_flags: 10
int Method::_vtable_index: -2
u2 Method::_intrinsic_id: 0
nmethod* Method::_code: nmethod @ 0x00007f309c427588
address Method::_i2i_entry: address @ 0x00007f307c4006d0
address Method::_from_compiled_entry: address @ 0x00007f307c4006d8
address Method::_from_interpreted_entry: address @ 0x00007f307c4006e8

The key things to notice are:

  • A MethodData object has appeared, and it will collect more detailed profiling information
  • The MethodCounters object is still there, but it is now ‘frozen’
  • There is compiled machine code, as shown by a non-null nmethod

Here is the source code of the resolveNumber method:

private static int resolveNumber(int i) {
    if (i < 10000) {
        return i;
    } else {
        return i * 2;
    }
}

The machine code generated for this method contains extra code that loads counters, increments them and stores them back. “Counters” here means not just the invocation counter but also the taken and not taken branch counters kept in MethodData. That is what “full profiling data” means for this method.

From now on, the MethodCounters won’t be changed any more:

InvocationCounter MethodCounters::_invocation_counter: 512
InvocationCounter MethodCounters::_backedge_counter: 0

The invocation_counter stopped after 256 iterations. There is, however, a second invocation_counter inside MethodData.

Looking at the method’s machine code, we can find the memory address it uses. The machine code fragment relevant to the invocation counter looks like this:

0x00007f309c42769b:    movabs $0x7f307c401928,%rax
0x00007f309c4276a5:    mov    0x90(%rax),%edi
0x00007f309c4276ab:    add    $0x2,%edi
0x00007f309c4276ae:    mov    %edi,0x90(%rax)

The second line loads the counter from offset 0x90 (hexadecimal, i.e. 144 in decimal) relative to the MethodData pointer. Since we know the MethodData address, we can work out the address of the invocation counter and inspect it with this jhsdb command:

printas InvocationCounter <memory_address>

The result is:

pointer to unsigned InvocationCounter @ 0x00007f307c4019b8 (size = 4)
unsigned int InvocationCounter::_counter: 114

This counter, too, goes up by 2 on every invocation. That means it has been incremented 57 times since it came into existence. The counter was created once this method moved to a higher Tier, which happened after 256 invocations. Even though my application log reported 314 loop iterations, the resolveNumber method hadn’t been invoked yet. That is why the counter value is 114 (314 - 256 = 58; with that invocation not having happened yet, we get 57).

We can see that this counter has been used ever since it was created, and at the 1739th iteration its value is:

pointer to unsigned InvocationCounter @ 0x00007f307c4019b8 (size = 4)
unsigned int InvocationCounter::_counter: 2964

Tier 4

After 5,120 invocations, the C2 compiler has compiled the resolveNumber method. I made the program pause again at the 5,513th loop iteration. By now, the invocation counter is frozen at the value that triggered the C2 compilation, because code compiled by C2 collects no profiling data. This is the invocation counter value:

pointer to unsigned InvocationCounter @ 0x00007f307c4019b8 (size = 4)
unsigned int InvocationCounter::_counter: 10240

Deoptimization

After 10,000 invocations, the resolveNumber method got deoptimized. This is how the method is defined:

private static int resolveNumber(int i) {
    if (i < 10000) {
        return i;
    } else {
        return i * 2;
    }
}

At first, the JIT compiler applied a speculative optimization, having found that for values of i below 10,000 the method always returned i. Once the calls went past 10,000, however, the code started taking the else branch, which forced a deoptimization.

Let’s look more closely at what this means in practice. Here is the method’s metadata:

Type is Method (size of 88)
ConstMethod* Method::_constMethod: ConstMethod @ 0x00007f307c400638
MethodData* Method::_method_data: MethodData @ 0x00007f307c401928
MethodCounters* Method::_method_counters: MethodCounters @ 0x00007f307c400f88
AccessFlags Method::_access_flags: 10
int Method::_vtable_index: -2
u2 Method::_intrinsic_id: 0
nmethod* Method::_code: nmethod @ null
address Method::_i2i_entry: address @ 0x00007f307c4006d0
address Method::_from_compiled_entry: address @ 0x00007f307c4006d8
address Method::_from_interpreted_entry: address @ 0x00007f307c4006e8
  • The reference to MethodData is non-null, which shows it wasn’t discarded after deoptimization
  • The reference to nmethod is gone, since the method has been deoptimized

The invocation counter has begun to grow again:

pointer to unsigned InvocationCounter @ 0x00007f307c4019b8 (size = 4)
unsigned int InvocationCounter::_counter: 10290

In addition, MethodData has been updated:

uint MethodData::_compiler_counters._nof_decompiles: 1

The missing piece

The earlier sections went through every compilation Tier but one – Tier 2. This Tier seems to come with “limited profiling data”. Getting a method compiled at Tier 2 isn’t simple, so I made the JVM do it by running this command:

java -XX:+PrintCompilation -XX:TieredStopAtLevel=2 -jar target/app.jar 1000 100

“Limited profiling” means that at this tier, the JVM doesn’t gather detailed profiling information such as, e.g., taken branches. It only increments the invocation and backedge counters. A MethodData object does exist, but the data it collects is minimal.

Type is Method (size of 88)
ConstMethod* Method::_constMethod: ConstMethod @ 0x00007fa840400638
MethodData* Method::_method_data: MethodData @ 0x00007fa840401928
MethodCounters* Method::_method_counters: MethodCounters @ 0x00007fa840400f88
AccessFlags Method::_access_flags: 10
int Method::_vtable_index: -2
u2 Method::_intrinsic_id: 0
nmethod* Method::_code: nmethod @ 0x00007fa860546188
address Method::_i2i_entry: address @ 0x00007fa8404006d0
address Method::_from_compiled_entry: address @ 0x00007fa8404006d8
address Method::_from_interpreted_entry: address @ 0x00007fa8404006e8

Conclusion

The JVM relies on two key data structures to hold profiling information:

  • MethodCounters, used when the program starts running
  • MethodData, a structure created once the JIT compiler compiles a method. It stays in use even when the method is deoptimized and falls back to interpreter mode. On top of that, MethodData lets the JVM collect more detailed information, which the C2 compiler can later use to optimize machine code more effectively

I hope this article sheds some useful light on how method profiling works in general.