Skip to content

Only fork jps when required #8419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static boolean copyOOMEscript(Path scriptPath) {
private static class ScriptCleanupVisitor implements FileVisitor<Path> {
private static final Pattern PID_PATTERN = Pattern.compile(".*?" + PID_PREFIX + "(\\d+)");

private final Set<String> pidSet = PidHelper.getJavaPids();
private Set<String> pidSet;

static void run(Path dir) {
try {
Expand Down Expand Up @@ -145,9 +145,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
Matcher matcher = PID_PATTERN.matcher(fileName);
if (matcher.find()) {
String pid = matcher.group(1);
if (pid != null && !pid.equals(PidHelper.getPid()) && !this.pidSet.contains(pid)) {
LOG.debug("Cleaning process specific file {}", file);
Files.delete(file);
if (pid != null && !pid.equals(PidHelper.getPid())) {
if (this.pidSet == null) {
this.pidSet = PidHelper.getJavaPids(); // only fork jps when required
}
if (!this.pidSet.contains(pid)) {
LOG.debug("Cleaning process specific file {}", file);
Files.delete(file);
}
}
}
return CONTINUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ default void onCleanupStart(boolean selfCleanup, long timeout, TimeUnit unit) {}
private final class CleanupVisitor implements FileVisitor<Path> {
private boolean shouldClean;

private final Set<String> pidSet = PidHelper.getJavaPids();
private Set<String> pidSet;

private final boolean cleanSelf;
private final Instant cutoff;
Expand Down Expand Up @@ -120,7 +120,14 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
// the JFR repository directories are under <basedir>/pid_<pid>
String pid = fileName.startsWith(TEMPDIR_PREFIX) ? fileName.substring(4) : null;
boolean isSelfPid = pid != null && pid.equals(PidHelper.getPid());
shouldClean |= cleanSelf ? isSelfPid : !isSelfPid && !pidSet.contains(pid);
if (cleanSelf) {
shouldClean |= isSelfPid;
} else if (!isSelfPid) {
if (pidSet == null) {
pidSet = PidHelper.getJavaPids(); // only fork jps when required
}
shouldClean |= !pidSet.contains(pid);
}
if (shouldClean) {
log.debug("Cleaning temporary location {}", dir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
Files.createFile(otherTempdir.resolve("dummy"));
boolean rslt =
instance.cleanup(
selfCleanup, (long) (timeoutMs * (shouldSucceed ? 10 : 0.5d)), TimeUnit.MILLISECONDS);
selfCleanup, (long) (timeoutMs * (shouldSucceed ? 20 : 0.5d)), TimeUnit.MILLISECONDS);
assertEquals(shouldSucceed, rslt);
}

Expand Down
Loading