AsyncProfiler - Unable to load profiler in JMH benchmark runner

1.5k Views Asked by At

I'm using JMH to benchmark JUnit tests. I want to start using async-profiler, to profile the benchmarks and get more information regarding CPU usage.

My benchmark runner:

import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.profile.AsyncProfiler;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.TimeValue;

import java.util.concurrent.TimeUnit;

public class BenchmarkRunner {
    public static void main(String[] args) throws Exception {
        Options opt = new OptionsBuilder()
                .include(Benchmarks.class.getSimpleName())
                .addProfiler(AsyncProfiler.class, "output=flamegraph")
                .mode(Mode.Throughput)
                .resultFormat(ResultFormatType.CSV)
                .result("target/test-classes/benchmarkcsv/BM " + System.currentTimeMillis() + ".csv")
                .timeUnit(TimeUnit.MILLISECONDS)
                .warmupIterations(3)
                .warmupTime(TimeValue.seconds(1))
                .measurementIterations(3)
                .measurementTime(TimeValue.seconds(1))
                .timeout(TimeValue.seconds(5))
                .forks(1)
                .warmupForks(1)
                .threads(1)
                .build();

        new Runner(opt).run();
    }
}

While running this, I'm getting the following error:

Exception in thread "main" org.openjdk.jmh.runner.ProfilersFailedException: Profilers failed to initialize, exiting.
    at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:228)
    at org.openjdk.jmh.runner.Runner.run(Runner.java:209)
    at DummyApp.BenchmarkRunner.main(BenchmarkRunner.java:33)
Caused by: org.openjdk.jmh.profile.ProfilerException: Unable to load async-profiler. Ensure asyncProfiler library is on LD_LIBRARY_PATH (Linux), DYLD_LIBRARY_PATH (Mac OS), or -Djava.library.path. Alternatively, point to explicit library location with -prof async:libPath=<path>.
    at org.openjdk.jmh.profile.AsyncProfiler.<init>(AsyncProfiler.java:237)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openjdk.jmh.profile.ProfilerFactory.instantiate(ProfilerFactory.java:82)
    at org.openjdk.jmh.profile.ProfilerFactory.getProfiler(ProfilerFactory.java:77)
    at org.openjdk.jmh.profile.ProfilerFactory.getProfilerOrException(ProfilerFactory.java:37)
    at org.openjdk.jmh.runner.Runner.internalRun(Runner.java:225)
    ... 2 more
Caused by: java.lang.UnsatisfiedLinkError: no asyncProfiler in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at org.openjdk.jmh.profile.AsyncProfiler$JavaApi.<init>(AsyncProfiler.java:584)
    at org.openjdk.jmh.profile.AsyncProfiler$JavaApi.getInstance(AsyncProfiler.java:573)
    at org.openjdk.jmh.profile.AsyncProfiler.<init>(AsyncProfiler.java:234)
    ... 10 more

Process finished with exit code 1

According to the async-profiler Github page, async-profiler comes bundled with IntelliJ IDEA Ultimate 2018.3 and later. I'm running the IntelliJ IDEA Community Edition 2021.3.2 on my system, but I'm unable to find the library.

How can I find this library/solve this error I'm getting?

1

There are 1 best solutions below

2
On BEST ANSWER

The exception message tells exactly what happens and how to solve it:

Unable to load async-profiler. Ensure asyncProfiler library is on
LD_LIBRARY_PATH (Linux), DYLD_LIBRARY_PATH (Mac OS), or -Djava.library.path.
Alternatively, point to explicit library location with -prof async:libPath=<path>.

Get async-profiler from https://github.com/jvm-profiling-tools/async-profiler/releases, and set the path to libasyncProfiler.so in LD_LIBRARY_PATH environment variable or in -Djava.library.path JVM option.

On Windows you can choose another JMH profiler: -prof jfr or addProfiler(JavaFlightRecorderProfiler.class). This will produce .jfr recording that you can open directly in JDK Mission Control or convert to a Flame Graph using converter.jar from async-profiler.