Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
Use ap_loader, fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
parttimenerd committed Nov 11, 2022
1 parent d28249b commit 1ff66a8
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 78 deletions.
Binary file removed lib/async-profiler-2.8.3/async-profiler.jar
Binary file not shown.
Binary file removed lib/async-profiler-2.8.3/converter.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed lib/async-profiler-2.8.3/libasyncProfiler-macos.so
Binary file not shown.
21 changes: 14 additions & 7 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def jpprofVersion = project.properties['jpprof_version']

repositories {
mavenCentral()
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots")
mavenContent {
snapshotsOnly()
}
}
}

java {
Expand All @@ -29,8 +35,14 @@ dependencies {

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:31.0.1-jre'
implementation files('async-profiler-2.8.3/async-profiler.jar')
implementation files('async-profiler-2.8.3/converter.jar')
implementation('me.bechberger:ap-loader:2.8.3-all-SNAPSHOT') {
changing = true
}
}

configurations.all {
resolutionStrategy.cacheDynamicVersionsFor 4, 'hours'
resolutionStrategy.cacheChangingModulesFor 4, 'seconds'
}

tasks.withType(Test) {
Expand All @@ -50,11 +62,6 @@ testing {
shadowJar {
exclude '**.html'

from("async-profiler-2.8.3") {
include "**.so"
into "async-profiler-libs"
}

archiveBaseName.set('jpprof')
archiveClassifier.set('')
archiveVersion.set(jpprofVersion)
Expand Down
73 changes: 2 additions & 71 deletions lib/src/main/java/jpprof/CPUProfiler.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
package jpprof;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.zip.GZIPOutputStream;

import one.jfr.JfrReader;
import one.profiler.AsyncProfiler;
import one.profiler.AsyncProfilerLoader;
import one.profiler.Events;

/**
* CPUProfiler is a CPU profiler.
*/
public class CPUProfiler {
private static final File tmpDir;
private static final String nativeLibPath;

static {
try {
tmpDir = Files.createTempDirectory("jpprof-").toFile();
nativeLibPath = copyLibrary();
} catch (final Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -39,7 +35,7 @@ public class CPUProfiler {
*/
public static void start(Duration duration, OutputStream out) throws IOException, InterruptedException {
File jfrFile = File.createTempFile("profile-", "jfr", tmpDir);
AsyncProfiler instance = AsyncProfiler.getInstance(nativeLibPath);
AsyncProfiler instance = AsyncProfilerLoader.load();
instance.execute(buildStartCommand(jfrFile.getAbsolutePath()));
Thread.sleep(duration.toMillis());
instance.stop();
Expand All @@ -51,71 +47,6 @@ public static void start(Duration duration, OutputStream out) throws IOException
jfrFile.delete();
}

/**
* Copy the embedded native async-profiler library to
* a tmp path, and returns the absolute path.
*/
private static String copyLibrary() throws Exception {

String embeddedLibPrefix = "/async-profiler-libs/libasyncProfiler";
String embeddedLibSuffix = getLibrarySuffix();

InputStream is = CPUProfiler.class.getResourceAsStream(embeddedLibPrefix + embeddedLibSuffix);
if (is == null) {
return System.getProperty("user.dir") + "/async-profiler-2.8.3/" + "libasyncProfiler"
+ getLibrarySuffix();
}

Path libCopyPath = new File(tmpDir, "libasyncProfiler.so").toPath().toAbsolutePath();
Files.copy(is, libCopyPath, StandardCopyOption.REPLACE_EXISTING);
return libCopyPath.toString();
}

/**
* Returns the library suffix for the current platform.
*
* @return the library suffix depending on the current platform
* @throws Exception
*/
private static String getLibrarySuffix() throws Exception {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
String embeddedLibSuffix = "";

switch (osName) {
case "linux":
switch (osArch) {
case "amd64":
embeddedLibSuffix = "-linux-x64.so";
break;

case "aarch64":
embeddedLibSuffix = "-linux-arm64.so";
break;

default:
throw new Exception("Unsupported Linux arch: " + osArch);
}
break;

case "mac os x":
switch (osArch) {
case "x86_64":
case "aarch64":
embeddedLibSuffix = "-macos.so";
break;

default:
throw new Exception("Unsupported OSX arch: " + osArch);
}
break;

default:
throw new Exception("Unsupported OS: " + osName);
}
return embeddedLibSuffix;
}

private static String buildStartCommand(String dst) {
StringBuilder sb = new StringBuilder();
sb.append("start,event=").append(Events.CPU);
Expand Down

0 comments on commit 1ff66a8

Please sign in to comment.