Upgrade Async Profiler API to v2.9 (#2608)

This commit is contained in:
Winson Huang 2023-08-11 20:14:52 +08:00 committed by GitHub
parent 4dc65baf7f
commit ede902329d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 11 deletions

3
.gitignore vendored
View File

@ -25,3 +25,6 @@ pom.xml.versionsBackup
**/.flattened-pom.xml
**/.idea/**
**/cmake-build-debug/**
# VSCode
.vscode/

View File

@ -16,7 +16,10 @@
package one.profiler;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Java API for in-process profiling. Serves as a wrapper around
@ -39,14 +42,79 @@ public class AsyncProfiler implements AsyncProfilerMXBean {
return instance;
}
if (libPath == null) {
System.loadLibrary("asyncProfiler");
} else {
AsyncProfiler profiler = new AsyncProfiler();
if (libPath != null) {
System.load(libPath);
} else {
try {
// No need to load library, if it has been preloaded with -agentpath
profiler.getVersion();
} catch (UnsatisfiedLinkError e) {
File file = extractEmbeddedLib();
if (file != null) {
try {
System.load(file.getPath());
} finally {
file.delete();
}
} else {
System.loadLibrary("asyncProfiler");
}
}
}
instance = new AsyncProfiler();
return instance;
instance = profiler;
return profiler;
}
private static File extractEmbeddedLib() {
String resourceName = "/" + getPlatformTag() + "/libasyncProfiler.so";
InputStream in = AsyncProfiler.class.getResourceAsStream(resourceName);
if (in == null) {
return null;
}
try {
String extractPath = System.getProperty("one.profiler.extractPath");
File file = File.createTempFile("libasyncProfiler-", ".so",
extractPath == null || extractPath.isEmpty() ? null : new File(extractPath));
try (FileOutputStream out = new FileOutputStream(file)) {
byte[] buf = new byte[32000];
for (int bytes; (bytes = in.read(buf)) >= 0; ) {
out.write(buf, 0, bytes);
}
}
return file;
} catch (IOException e) {
throw new IllegalStateException(e);
} finally {
try {
in.close();
} catch (IOException e) {
// ignore
}
}
}
private static String getPlatformTag() {
String os = System.getProperty("os.name").toLowerCase();
String arch = System.getProperty("os.arch").toLowerCase();
if (os.contains("linux")) {
if (arch.equals("amd64") || arch.equals("x86_64") || arch.contains("x64")) {
return "linux-x64";
} else if (arch.equals("aarch64") || arch.contains("arm64")) {
return "linux-arm64";
} else if (arch.equals("aarch32") || arch.contains("arm")) {
return "linux-arm32";
} else if (arch.contains("86")) {
return "linux-x86";
} else if (arch.contains("ppc64")) {
return "linux-ppc64le";
}
} else if (os.contains("mac")) {
return "macos";
}
throw new UnsupportedOperationException("Unsupported platform: " + os + "-" + arch);
}
/**
@ -209,7 +277,10 @@ public class AsyncProfiler implements AsyncProfilerMXBean {
}
private native void start0(String event, long interval, boolean reset) throws IllegalStateException;
private native void stop0() throws IllegalStateException;
private native String execute0(String command) throws IllegalArgumentException, IllegalStateException, IOException;
private native void filterThread0(Thread thread, boolean enable);
}

View File

@ -1,5 +1,5 @@
/**
* from https://github.com/jvm-profiling-tools/async-profiler
* This package is from https://github.com/async-profiler/async-profiler/
* tag v2.9 commit 32601bc
*/
package one.profiler;