From 2691513c6189d658765acec7a26ee711239527de Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 9 May 2025 09:48:06 -0700 Subject: [PATCH 1/5] Read hsperfdata for Java PIDs if jvmstat is unavailable --- .../java/datadog/trace/util/PidHelper.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal-api/src/main/java/datadog/trace/util/PidHelper.java b/internal-api/src/main/java/datadog/trace/util/PidHelper.java index dfa6276b879..4970da90bdf 100644 --- a/internal-api/src/main/java/datadog/trace/util/PidHelper.java +++ b/internal-api/src/main/java/datadog/trace/util/PidHelper.java @@ -8,11 +8,16 @@ import java.io.IOException; import java.io.InputStreamReader; import java.lang.management.ManagementFactory; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collections; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -70,6 +75,24 @@ public static Set getJavaPids() { if (directlyObtainedPids != null) { return directlyObtainedPids; } + + // Some JDKs don't have jvmstat available as a module, attempt to read from the hsperfdata + // directory instead + try (Stream stream = + Files.list( + Paths.get( + System.getProperty("java.io.tmpdir") + + "hsperfdata_" + + System.getProperty("user.name")))) { + return stream + .filter(file -> !Files.isDirectory(file)) + .map(Path::getFileName) + .map(Path::toString) + .collect(Collectors.toSet()); + } catch (IOException e) { + log.debug("Unable to obtain Java PIDs via hsperfdata", e); + } + // there is no supported Java API to achieve this // one could use sun.jvmstat.monitor.MonitoredHost but it is an internal API and can go away at // any time - From 1dfd54f112007ac5fc4e73d6347500598ad6f7cc Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 9 May 2025 16:44:47 -0700 Subject: [PATCH 2/5] Add equivalent to JDK os::get_temp_directory() for clean-room jvmstat impl. --- .../java/datadog/trace/util/PidHelper.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/internal-api/src/main/java/datadog/trace/util/PidHelper.java b/internal-api/src/main/java/datadog/trace/util/PidHelper.java index 4970da90bdf..82fe8cf49c4 100644 --- a/internal-api/src/main/java/datadog/trace/util/PidHelper.java +++ b/internal-api/src/main/java/datadog/trace/util/PidHelper.java @@ -69,6 +69,26 @@ private static String findPid() { return pid; } + private static String getOSTempDir() { + // See + // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppatha#remarks + // and + // the JDK OS-specific implementations of os::get_temp_directory(), i.e. + // https://github.com/openjdk/jdk/blob/f50bd0d9ec65a6b9596805d0131aaefc1bb913f3/src/hotspot/os/bsd/os_bsd.cpp#L886-L904 + if (Platform.isLinux()) { + return "/tmp/"; + } else if (Platform.isWindows()) { + return Stream.of(System.getenv("TMP"), System.getenv("TEMP"), System.getenv("USERPROFILE")) + .filter(String::isEmpty) + .findFirst() + .orElse("C:\\Windows"); + } else if (Platform.isMac()) { + return System.getenv("TMPDIR"); + } else { + return System.getProperty("java.io.tmpdir"); + } + } + public static Set getJavaPids() { // Attempt to use jvmstat directly, fall through to jps process fork strategy Set directlyObtainedPids = JPSUtils.getVMPids(); @@ -79,11 +99,7 @@ public static Set getJavaPids() { // Some JDKs don't have jvmstat available as a module, attempt to read from the hsperfdata // directory instead try (Stream stream = - Files.list( - Paths.get( - System.getProperty("java.io.tmpdir") - + "hsperfdata_" - + System.getProperty("user.name")))) { + Files.list(Paths.get(getOSTempDir() + "/hsperfdata_" + System.getProperty("user.name")))) { return stream .filter(file -> !Files.isDirectory(file)) .map(Path::getFileName) From 44ea4e64c997ced60e75fa6db5bbb8b6936762dd Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 12 May 2025 09:06:21 -0400 Subject: [PATCH 3/5] Add a comment documenting how JVM processes are enumerated Co-authored-by: Jaroslav Bachorik --- internal-api/src/main/java/datadog/trace/util/PidHelper.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal-api/src/main/java/datadog/trace/util/PidHelper.java b/internal-api/src/main/java/datadog/trace/util/PidHelper.java index 82fe8cf49c4..371af32783e 100644 --- a/internal-api/src/main/java/datadog/trace/util/PidHelper.java +++ b/internal-api/src/main/java/datadog/trace/util/PidHelper.java @@ -99,6 +99,8 @@ public static Set getJavaPids() { // Some JDKs don't have jvmstat available as a module, attempt to read from the hsperfdata // directory instead try (Stream stream = + // Emulating the hotspot way to enumerate the JVM processes using the perfdata file + // https://github.com/openjdk/jdk/blob/d7cb933b89839b692f5562aeeb92076cd25a99f6/src/hotspot/share/runtime/perfMemory.cpp#L244 Files.list(Paths.get(getOSTempDir() + "/hsperfdata_" + System.getProperty("user.name")))) { return stream .filter(file -> !Files.isDirectory(file)) From 6e7e380ff5d14e5b17e8d908c3be66e972607fec Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 12 May 2025 09:09:14 -0400 Subject: [PATCH 4/5] Remove unnecessary defensive path separator Co-authored-by: Brice Dutheil --- internal-api/src/main/java/datadog/trace/util/PidHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal-api/src/main/java/datadog/trace/util/PidHelper.java b/internal-api/src/main/java/datadog/trace/util/PidHelper.java index 371af32783e..ff0b9b1589e 100644 --- a/internal-api/src/main/java/datadog/trace/util/PidHelper.java +++ b/internal-api/src/main/java/datadog/trace/util/PidHelper.java @@ -101,7 +101,7 @@ public static Set getJavaPids() { try (Stream stream = // Emulating the hotspot way to enumerate the JVM processes using the perfdata file // https://github.com/openjdk/jdk/blob/d7cb933b89839b692f5562aeeb92076cd25a99f6/src/hotspot/share/runtime/perfMemory.cpp#L244 - Files.list(Paths.get(getOSTempDir() + "/hsperfdata_" + System.getProperty("user.name")))) { + Files.list(Paths.get(getOSTempDir(), "hsperfdata_" + System.getProperty("user.name")))) { return stream .filter(file -> !Files.isDirectory(file)) .map(Path::getFileName) From d7886c60bb6d13571dfbeeb2942e42b493e6ad39 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 12 May 2025 09:37:29 -0400 Subject: [PATCH 5/5] Spotless --- internal-api/src/main/java/datadog/trace/util/PidHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal-api/src/main/java/datadog/trace/util/PidHelper.java b/internal-api/src/main/java/datadog/trace/util/PidHelper.java index ff0b9b1589e..eebf6fa7449 100644 --- a/internal-api/src/main/java/datadog/trace/util/PidHelper.java +++ b/internal-api/src/main/java/datadog/trace/util/PidHelper.java @@ -99,8 +99,8 @@ public static Set getJavaPids() { // Some JDKs don't have jvmstat available as a module, attempt to read from the hsperfdata // directory instead try (Stream stream = - // Emulating the hotspot way to enumerate the JVM processes using the perfdata file - // https://github.com/openjdk/jdk/blob/d7cb933b89839b692f5562aeeb92076cd25a99f6/src/hotspot/share/runtime/perfMemory.cpp#L244 + // Emulating the hotspot way to enumerate the JVM processes using the perfdata file + // https://github.com/openjdk/jdk/blob/d7cb933b89839b692f5562aeeb92076cd25a99f6/src/hotspot/share/runtime/perfMemory.cpp#L244 Files.list(Paths.get(getOSTempDir(), "hsperfdata_" + System.getProperty("user.name")))) { return stream .filter(file -> !Files.isDirectory(file))