grafana / pyroscope-java

pyroscope java integration
Apache License 2.0
72 stars 31 forks source link

Loading Pyroscope Java agent causes UnsatisfiedLinkError on systems running Homebrew OpenJDK #67

Closed deejgregor closed 1 year ago

deejgregor commented 1 year ago

Here is what I get in code that calls java.lang.management.ManagementFactory.getRuntimeMXBean():

$ /opt/homebrew/opt/openjdk@11/bin/java -javaagent:/Users/dgregor/Downloads/pyroscope.jar -cp . ManagementTest
2022-09-18 16:42:07.026 [INFO] We recommend specifying application name via env variable PYROSCOPE_APPLICATION_NAME
2022-09-18 16:42:07.034 [INFO] For now we chose the name for you and it's javaspy.VeRo3kppTE6W-aS19qBBug
2022-09-18 16:42:07.034 [WARN] PYROSCOPE_SERVER_ADDRESS is not defined, using http://localhost:4040
2022-09-18 16:42:07.707 [INFO] Profiling started
Exception in thread "main" java.lang.UnsatisfiedLinkError: no management in java.library.path: [/Users/dgregor/Library/Java/Extensions, /Library/Java/Extensions, /Network/Library/Java/Extensions, /System/Library/Java/Extensions, /usr/lib/java, .]
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2673)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
    at java.base/java.lang.System.loadLibrary(System.java:1873)
    at java.management/java.lang.management.ManagementFactory.lambda$static$8(ManagementFactory.java:1020)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.management/java.lang.management.ManagementFactory.<clinit>(ManagementFactory.java:1019)
    at ManagementTest.main(ManagementTest.java:6)
^CProfiling stopped after 0 seconds. No dump options specified
$ 

This appears to be due to an rpath issue in Homebrew OpenJDK builds that breaks loading of other shared objects after async-profiler is loaded (in my case, libmanagement.so/dylib): https://github.com/jvm-profiling-tools/async-profiler/issues/647

I opened up an issue on homebrew-core including a simple reproducer: https://github.com/Homebrew/homebrew-core/issues/111068

I was able to resolve the issue with Pyroscope by rebuilding the pyroscope.jar with an updated version of your async-profiler fork that includes b5634b9 from async-profiler. Details are in the tail end of https://github.com/Homebrew/homebrew-core/issues/111068.

petethepig commented 1 year ago

Thanks for the report @deejgregor

We'll look into this soon!

korniltsev commented 1 year ago

I applied https://github.com/jvm-profiling-tools/async-profiler/commit/b5634b9d88304e64c0f77cf961119f240a979c38 to our branch Released java agent 0.10.2 with the fix. Please let us know if it fixed the problem.

Thank you for the report.

deejgregor commented 1 year ago

Released java agent 0.10.2 with the fix. Please let us know if it fixed the problem.

It does! Thanks for the quick turn around.

I also got a response from Homebrew, so I'll be giving that a try shortly, too (with the older version of the library): https://github.com/Homebrew/homebrew-core/issues/111068#issuecomment-1251126134

deejgregor commented 1 year ago

FYI, the underlying problem in Homebrew OpenJDK has been addressed: https://github.com/Homebrew/homebrew-core/pull/111255

Rperry2174 commented 1 year ago

Awesome thanks for the update @deejgregor !