RFR: CODETOOLS-7903484: JMH: Use ThreadMXBean.getTotalThreadAllocatedBytes for -prof gc

Aleksey Shipilev shade at openjdk.org
Thu Jun 1 11:24:52 UTC 2023


JDK 21 comes with a new method that gives the total allocated rate for all the threads, [JDK-8304074](https://bugs.openjdk.org/browse/JDK-8304074). JMH -prof gc can use it, when available. This allows catching the totality of the thread allocations, regardless whether the threads went in or out during the lifetime of the benchmark. Current `-prof gc` is missing those threads most of the time. 

Example benchmark:


@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(value = 1, jvmArgsAppend = {"-Xmx1g", "-Xms1g", "-XX:+AlwaysPreTouch", "-XX:+UseParallelGC"})
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Thread)
public class TAB {

    @Benchmark
    public void inline(Blackhole bh) throws InterruptedException {
        bh.consume(new byte[1_000_000]);
    }

    @Benchmark
    public void separate(Blackhole bh) throws InterruptedException {
        Thread thread = new Thread(() -> { bh.consume(new byte[1_000_000]); });
        thread.start();
        thread.join();
    }
}


Current JDK 20:


Benchmark                          Mode  Cnt        Score      Error   Units

TAB.inline                        thrpt    5   100632,205 ± 1237,322   ops/s
TAB.inline:·gc.alloc.rate         thrpt    5    95961,825 ± 1181,577  MB/sec
TAB.inline:·gc.alloc.rate.norm    thrpt    5  1000016,004 ±    0,001    B/op
TAB.inline:·gc.count              thrpt    5     1432,000             counts
TAB.inline:·gc.time               thrpt    5      207,000                 ms

TAB.separate                      thrpt    5    18169,567 ±  374,656   ops/s
TAB.separate:·gc.alloc.rate       thrpt    5        5,822 ±    0,120  MB/sec  ; <--- missing allocations
TAB.separate:·gc.alloc.rate.norm  thrpt    5      336,024 ±    0,010    B/op  ; <--- missing allocations
TAB.separate:·gc.count            thrpt    5     2023,000             counts
TAB.separate:·gc.time             thrpt    5      229,000                 ms


Self-built JDK 21-ea:


Benchmark                          Mode  Cnt        Score      Error   Units

TAB.inline                        thrpt    5   103061,266 ± 2086,154   ops/s
TAB.inline:·gc.alloc.rate         thrpt    5    98277,065 ± 1988,502  MB/sec
TAB.inline:·gc.alloc.rate.norm    thrpt    5  1000016,056 ±    0,001    B/op
TAB.inline:·gc.count              thrpt    5     1466,000             counts
TAB.inline:·gc.time               thrpt    5      180,000                 ms

TAB.separate                      thrpt    5    18105,424 ± 1444,031   ops/s
TAB.separate:·gc.alloc.rate       thrpt    5    17270,918 ± 1378,016  MB/sec
TAB.separate:·gc.alloc.rate.norm  thrpt    5  1000360,319 ±    0,030    B/op ; <--- same alloc rate
TAB.separate:·gc.count            thrpt    5     2015,000             counts
TAB.separate:·gc.time             thrpt    5      197,000                 ms


Note: GHA testing would not be clean until `21-ea` release catches up and contains JDK-8304074.

-------------

Commit messages:
 - Touchups
 - Add integration test
 - Initial work

Changes: https://git.openjdk.org/jmh/pull/106/files
 Webrev: https://webrevs.openjdk.org/?repo=jmh&pr=106&range=00
  Issue: https://bugs.openjdk.org/browse/CODETOOLS-7903484
  Stats: 166 lines in 2 files changed: 146 ins; 11 del; 9 mod
  Patch: https://git.openjdk.org/jmh/pull/106.diff
  Fetch: git fetch https://git.openjdk.org/jmh.git pull/106/head:pull/106

PR: https://git.openjdk.org/jmh/pull/106


More information about the jmh-dev mailing list