Skip to content

Commit

Permalink
Upgrade Async Profiler API to v2.9 (#2608)
Browse files Browse the repository at this point in the history
  • Loading branch information
Winson-Huang authored Aug 11, 2023
1 parent 4dc65ba commit ede9023
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ pom.xml.versionsBackup
**/.flattened-pom.xml
**/.idea/**
**/cmake-build-debug/**

# VSCode
.vscode/
87 changes: 79 additions & 8 deletions core/src/main/java/one/profiler/AsyncProfiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,20 +42,85 @@ public static synchronized AsyncProfiler getInstance(String libPath) {
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 = 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
}
}
}

instance = new AsyncProfiler();
return instance;
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);
}

/**
* Start profiling
*
* @param event Profiling event, see {@link Events}
* @param event Profiling event, see {@link Events}
* @param interval Sampling interval, e.g. nanoseconds for Events.CPU
* @throws IllegalStateException If profiler is already running
*/
Expand All @@ -68,7 +136,7 @@ public void start(String event, long interval) throws IllegalStateException {
* Start or resume profiling without resetting collected data.
* Note that event and interval may change since the previous profiling session.
*
* @param event Profiling event, see {@link Events}
* @param event Profiling event, see {@link Events}
* @param interval Sampling interval, e.g. nanoseconds for Events.CPU
* @throws IllegalStateException If profiler is already running
*/
Expand Down Expand Up @@ -119,7 +187,7 @@ public String getVersion() {
* @param command Profiling command
* @return The command result
* @throws IllegalArgumentException If failed to parse the command
* @throws IOException If failed to create output file
* @throws IOException If failed to create output file
*/
@Override
public String execute(String command) throws IllegalArgumentException, IllegalStateException, IOException {
Expand Down Expand Up @@ -209,7 +277,10 @@ private void filterThread(Thread thread, boolean enable) {
}

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);
}
6 changes: 3 additions & 3 deletions core/src/main/java/one/profiler/package-info.java
Original file line number Diff line number Diff line change
@@ -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;
package one.profiler;

0 comments on commit ede9023

Please sign in to comment.