Skip to content

Instantly share code, notes, and snippets.

@franz1981
Created December 17, 2024 13:53
Show Gist options
  • Save franz1981/d169ff9a7a3dc5c1ff4de45271ab755f to your computer and use it in GitHub Desktop.
Save franz1981/d169ff9a7a3dc5c1ff4de45271ab755f to your computer and use it in GitHub Desktop.
package redhat.app.services.benchmark;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
/**
* This benchmark should be used with the following JVM options to tune the tier compilation level:
* -XX:TieredStopAtLevel=
*
*/
@State(Scope.Benchmark)
@Fork(2)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
public class MethodDataSharing {
@Benchmark
public void doFoo() {
foo(1000, true);
}
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
private static int foo(int count, boolean countAll) {
int total = 0;
for (int i = 0; i < count; i++) {
if (countAll) {
total++;
}
}
return total;
}
}
@franz1981
Copy link
Author

franz1981 commented Dec 17, 2024

$ java -jar target/benchmark.jar MethodDataSharing -t 2 --jvmArgs="-XX:TieredStopAtLevel=3" -prof perfasm
Benchmark                    Mode  Cnt      Score      Error  Units
MethodDataSharing.doFoo      avgt   20  15414.012 ± 1616.955  ns/op

lstopo shows:
image

$ numactl --physcpubind 0,8 java -jar target/benchmark.jar MethodDataSharing -t 2 --jvmArgs="-XX:TieredStopAtLevel=3" -prof perfasm
Benchmark                    Mode  Cnt      Score      Error  Units
MethodDataSharing.doFoo      avgt   20  16427.929 ± 1475.128  ns/op

while keeping everything on the same socket

$ numactl --physcpubind 0,1 java -jar target/benchmark.jar MethodDataSharing -t 2 --jvmArgs="-XX:TieredStopAtLevel=3" -prof perfasm
Benchmark                Mode  Cnt     Score     Error  Units
MethodDataSharing.doFoo  avgt   20  8662.030 ± 731.919  ns/op

and perf analysis shows , in the more costly case:

....[Hottest Region 1]..............................................................................
c1, level 3, redhat.app.services.benchmark.MethodDataSharing::foo, version 2, compile id 719 

                0x00007fb68cf14168:   mov    $0x0,%edi
          ╭     0x00007fb68cf1416d:   jmp    0x00007fb68cf141f7           ;*iload_3 {reexecute=0 rethrow=0 return_oop=0}
          │                                                               ; - redhat.app.services.benchmark.MethodDataSharing::foo@4 (line 39)
          │     0x00007fb68cf14172:   nopw   0x0(%rax,%rax,1)
   0.03%  │     0x00007fb68cf14178:   cmp    $0x0,%edx
          │     0x00007fb68cf1417b:   movabs $0x7fb6104de900,%rbx         ;   {metadata(method data for {method} {0x00007fb6104783c8} &apos;foo&apos; &apos;(IZ)I&apos; in &apos;redhat/app/services/benchmark/MethodDataSharing&apos;)}
          │     0x00007fb68cf14185:   movabs $0x158,%rcx
          │╭    0x00007fb68cf1418f:   je     0x00007fb68cf1419f
          ││    0x00007fb68cf14195:   movabs $0x168,%rcx
          │↘    0x00007fb68cf1419f:   mov    (%rbx,%rcx,1),%r8
   9.41%  │     0x00007fb68cf141a3:   lea    0x1(%r8),%r8
          │     0x00007fb68cf141a7:   mov    %r8,(%rbx,%rcx,1)
   0.11%  │ ╭   0x00007fb68cf141ab:   je     0x00007fb68cf141b3           ;*ifeq {reexecute=0 rethrow=0 return_oop=0}
          │ │                                                             ; - redhat.app.services.benchmark.MethodDataSharing::foo@10 (line 40)
          │ │   0x00007fb68cf141b1:   inc    %edi
          │ ↘   0x00007fb68cf141b3:   inc    %eax
          │     0x00007fb68cf141b5:   movabs $0x7fb6104de900,%rbx         ;   {metadata(method data for {method} {0x00007fb6104783c8} &apos;foo&apos; &apos;(IZ)I&apos; in &apos;redhat/app/services/benchmark/MethodDataSharing&apos;)}
          │     0x00007fb68cf141bf:   mov    0xf8(%rbx),%ecx
  17.60%  │     0x00007fb68cf141c5:   add    $0x2,%ecx
   0.01%  │     0x00007fb68cf141c8:   mov    %ecx,0xf8(%rbx)
   0.02%  │     0x00007fb68cf141ce:   and    $0x3ffe,%ecx
   0.05%  │     0x00007fb68cf141d4:   cmp    $0x0,%ecx
          │     0x00007fb68cf141d7:   je     0x00007fb68cf14266           ;*goto {reexecute=0 rethrow=0 return_oop=0}
          │                                                               ; - redhat.app.services.benchmark.MethodDataSharing::foo@19 (line 39)
          │     0x00007fb68cf141dd:   mov    0x458(%r15),%r10             ; ImmutableOopMap {}
          │                                                               ;*goto {reexecute=1 rethrow=0 return_oop=0}
          │                                                               ; - (reexecute) redhat.app.services.benchmark.MethodDataSharing::foo@19 (line 39)
   0.03%  │     0x00007fb68cf141e4:   test   %eax,(%r10)                  ;   {poll}
          │     0x00007fb68cf141e7:   movabs $0x7fb6104de900,%rbx         ;   {metadata(method data for {method} {0x00007fb6104783c8} &apos;foo&apos; &apos;(IZ)I&apos; in &apos;redhat/app/services/benchmark/MethodDataSharing&apos;)}
          │     0x00007fb68cf141f1:   incl   0x178(%rbx)                  ;*goto {reexecute=0 rethrow=0 return_oop=0}
          │                                                               ; - redhat.app.services.benchmark.MethodDataSharing::foo@19 (line 39)
  40.64%  ↘     0x00007fb68cf141f7:   cmp    %esi,%eax
                0x00007fb68cf141f9:   movabs $0x7fb6104de900,%rbx         ;   {metadata(method data for {method} {0x00007fb6104783c8} &apos;foo&apos; &apos;(IZ)I&apos; in &apos;redhat/app/services/benchmark/MethodDataSharing&apos;)}
                0x00007fb68cf14203:   movabs $0x148,%rcx
             ╭  0x00007fb68cf1420d:   jl     0x00007fb68cf1421d
             │  0x00007fb68cf14213:   movabs $0x138,%rcx
   0.01%     ↘  0x00007fb68cf1421d:   mov    (%rbx,%rcx,1),%r8
  30.63%        0x00007fb68cf14221:   lea    0x1(%r8),%r8
   0.03%        0x00007fb68cf14225:   mov    %r8,(%rbx,%rcx,1)
   0.04%        0x00007fb68cf14229:   jl     0x00007fb68cf14178           ;*if_icmpge {reexecute=0 rethrow=0 return_oop=0}
                                                                          ; - redhat.app.services.benchmark.MethodDataSharing::foo@6 (line 39)
....................................................................................................
  67.86%  <total for region 1>

which shows several metadata(method data for {method} {0x00007fb6104783c8} comments, which refer to the method data object starting at 0x7fb6104de900, which contains few counters in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment