RFR: 7903455: JMH: Add "mempool" profiler

Aleksey Shipilev shade at openjdk.org
Fri Apr 14 15:24:17 UTC 2023


On Thu, 6 Apr 2023 17:23:28 GMT, Eric Caspole <ecaspole at openjdk.org> wrote:

> I started off to show only the code cache related size, but I don't see any reason to not print all the available mbean pool info. We often adapt app or library code to run as JMH, to use all its benefits. In those cases seeing the code cache stats is more interesting than say, for a regular JDK API JMH benchmark. I verified the CodeHeap stats match well with +PrintCodeCache. There is mbean pool data for all the GCs as well.
> 
> Example output:
> 
> Benchmark                                                              (iterations)  Mode  Cnt      Score   Error  Units
> SpillCode.testSpillForManyInts                                                   10  avgt    5    705.433 ± 9.570  ns/op
> SpillCode.testSpillForManyInts:·CodeHeap 'non-nmethods'.used                     10  avgt    5   1139.000             kB
> SpillCode.testSpillForManyInts:·CodeHeap 'non-profiled nmethods'.used            10  avgt    5    209.750             kB
> SpillCode.testSpillForManyInts:·CodeHeap 'profiled nmethods'.used                10  avgt    5    924.875             kB
> SpillCode.testSpillForManyInts:·Compressed Class Space.used                      10  avgt    5    403.375             kB
> SpillCode.testSpillForManyInts:·G1 Eden Space.used                               10  avgt    5   8192.000             kB
> SpillCode.testSpillForManyInts:·G1 Old Gen.used                                  10  avgt    5   8160.000             kB
> SpillCode.testSpillForManyInts:·G1 Survivor Space.used                           10  avgt    5        ≈ 0             kB
> SpillCode.testSpillForManyInts:·Metaspace.used                                   10  avgt    5   3617.320             kB
> SpillCode.testSpillForManyInts:·Total CodeHeap.used                              10  avgt    5   2267.000             kB
> SpillCode.testSpillForManyInts:·Total MemPools.used                              10  avgt    5  22639.695             kB

This looks useful! 

I think the whole thing can be simpler, though:


public class MemPoolProfiler implements InternalProfiler {
    static final double BYTES_PER_KIB = 1024D;

    @Override
    public String getDescription() {
        return "Memory pool/footprint profiling via standard MBeans";
    }

    @Override
    public void beforeIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams) {
        for (MemoryPoolMXBean b : ManagementFactory.getMemoryPoolMXBeans()) {
            b.resetPeakUsage();
        }
    }

    @Override
    public Collection<? extends Result> afterIteration(BenchmarkParams benchmarkParams, IterationParams iterationParams, IterationResult result) {
        List<Result> results = new ArrayList<>();

        long sumCodeHeap = 0L;
        long sum = 0L;
        for (MemoryPoolMXBean bean : ManagementFactory.getMemoryPoolMXBeans()) {
            long used = bean.getPeakUsage().getUsed();
            if (bean.getName().contains("CodeHeap")) {
                sumCodeHeap += used;
            }
            sum += used;

            results.add(new ScalarResult(Defaults.PREFIX + "mempool." + bean.getName() + ".used", used / BYTES_PER_KIB, "KiB", AggregationPolicy.MAX));
        }

        results.add(new ScalarResult(Defaults.PREFIX + "mempool.total.codeheap.used", sumCodeHeap / BYTES_PER_KIB, "KiB", AggregationPolicy.MAX));
        results.add(new ScalarResult(Defaults.PREFIX + "mempool.total.used", sum / BYTES_PER_KIB, "KiB", AggregationPolicy.MAX));
        return results;
    }
}


In addition to this version being more readable, it also does only a single pass through the beans, which means sums and individual metrics would always agree.

Please also make sure to pass GHA tests: go to https://github.com/ericcaspole/jmh/actions and enable the workflows there.

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

Changes requested by shade (Committer).

PR Review: https://git.openjdk.org/jmh/pull/99#pullrequestreview-1379048737
PR Comment: https://git.openjdk.org/jmh/pull/99#issuecomment-1503175326


More information about the jmh-dev mailing list