Skip to content

Commit

Permalink
proper name for exectuable under windows os
Browse files Browse the repository at this point in the history
  • Loading branch information
kcrimson committed Feb 2, 2024
1 parent bcb02ff commit 8928419
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,6 +21,8 @@
public final class JFRProfilerDelegate implements ProfilerDelegate {
private static final String RECORDING_NAME = "pyroscope";
private static final String JFR_SETTINGS_RESOURCE = "/jfr/pyroscope.jfc";

private static final String OS_NAME = "os.name";
private Config config;
private File tempJFRFile;
private Path jcmdBin;
Expand Down Expand Up @@ -85,10 +90,8 @@ public synchronized void stop() {
if (exitCode != 0) {
throw new RuntimeException("Invalid exit code: " + exitCode);
}
} catch (IOException e) {
throw new IllegalStateException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (IOException | InterruptedException e) {
throw new RuntimeException("cannot stop JFR recording", e);
}
}

Expand Down Expand Up @@ -123,17 +126,26 @@ private Snapshot dumpImpl(Instant started, Instant ended) {

private static Path findJcmdBin() {
Path javaHome = Paths.get(System.getProperty("java.home"));
String jcmd = jcmdExecutable();
Path jcmdBin = javaHome.resolve("bin").resolve(jcmd);
//find jcmd binary
Path jcmdBin = javaHome.resolve("bin/jcmd");
if (!Files.isExecutable(jcmdBin)) {
jcmdBin = javaHome.getParent().resolve("bin/jcmd");
jcmdBin = javaHome.getParent().resolve("bin").resolve(jcmd);
if (!Files.isExecutable(jcmdBin)) {
throw new RuntimeException("cannot find executable jcmd in Java home");
}
}
return jcmdBin;
}

private static String jcmdExecutable() {
String jcmd = "jcmd";
if (isWindowsOS()) {
jcmd = "jcmd.exe";
}
return jcmd;
}

private static Path findJfrSettingsPath() {
try (InputStream inputStream = JFRProfilerDelegate.class.getResourceAsStream(JFR_SETTINGS_RESOURCE)) {
Path jfrSettingsPath = Files.createTempFile("pyroscope", ".jfc");
Expand All @@ -144,5 +156,12 @@ private static Path findJfrSettingsPath() {
}
}

private static boolean isWindowsOS() {
String osName = System.getProperty(OS_NAME);
if (osName.contains("Windows")) {
return true;
}
return false;
}

}

0 comments on commit 8928419

Please sign in to comment.