Skip to content

Commit d8086f4

Browse files
committed
Only fork jps when required (reduces overhead as well as risk of port collisions)
1 parent 5b7780b commit d8086f4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

dd-java-agent/agent-crashtracking/src/main/java/com/datadog/crashtracking/OOMENotifierScriptInitializer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static boolean copyOOMEscript(Path scriptPath) {
116116
private static class ScriptCleanupVisitor implements FileVisitor<Path> {
117117
private static final Pattern PID_PATTERN = Pattern.compile(".*?" + PID_PREFIX + "(\\d+)");
118118

119-
private final Set<String> pidSet = PidHelper.getJavaPids();
119+
private Set<String> pidSet;
120120

121121
static void run(Path dir) {
122122
try {
@@ -145,9 +145,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
145145
Matcher matcher = PID_PATTERN.matcher(fileName);
146146
if (matcher.find()) {
147147
String pid = matcher.group(1);
148-
if (pid != null && !pid.equals(PidHelper.getPid()) && !this.pidSet.contains(pid)) {
149-
LOG.debug("Cleaning process specific file {}", file);
150-
Files.delete(file);
148+
if (pid != null && !pid.equals(PidHelper.getPid())) {
149+
if (this.pidSet == null) {
150+
this.pidSet = PidHelper.getJavaPids(); // only fork jps when required
151+
}
152+
if (!this.pidSet.contains(pid)) {
153+
LOG.debug("Cleaning process specific file {}", file);
154+
Files.delete(file);
155+
}
151156
}
152157
}
153158
return CONTINUE;

dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller/TempLocationManager.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ default void onCleanupStart(boolean selfCleanup, long timeout, TimeUnit unit) {}
7373
private final class CleanupVisitor implements FileVisitor<Path> {
7474
private boolean shouldClean;
7575

76-
private final Set<String> pidSet = PidHelper.getJavaPids();
76+
private Set<String> pidSet;
7777

7878
private final boolean cleanSelf;
7979
private final Instant cutoff;
@@ -120,7 +120,14 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
120120
// the JFR repository directories are under <basedir>/pid_<pid>
121121
String pid = fileName.startsWith(TEMPDIR_PREFIX) ? fileName.substring(4) : null;
122122
boolean isSelfPid = pid != null && pid.equals(PidHelper.getPid());
123-
shouldClean |= cleanSelf ? isSelfPid : !isSelfPid && !pidSet.contains(pid);
123+
if (cleanSelf) {
124+
shouldClean |= isSelfPid;
125+
} else {
126+
if (pidSet == null) {
127+
pidSet = PidHelper.getJavaPids(); // only fork jps when required
128+
}
129+
shouldClean |= !isSelfPid && !pidSet.contains(pid);
130+
}
124131
if (shouldClean) {
125132
log.debug("Cleaning temporary location {}", dir);
126133
}

0 commit comments

Comments
 (0)