From fabe3bd755d3b64b68cef20cbf38b1b61df35d0e Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Thu, 29 Aug 2024 08:36:23 -0500 Subject: [PATCH] Cache current process handle Fixes performance problem when creating log records or other things which frequently request the PID or process name. See quarkusio/quarkus#42858 for more info. --- .../java/io/smallrye/common/os/Process.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/os/src/main/java/io/smallrye/common/os/Process.java b/os/src/main/java/io/smallrye/common/os/Process.java index 894f719..5715bf6 100644 --- a/os/src/main/java/io/smallrye/common/os/Process.java +++ b/os/src/main/java/io/smallrye/common/os/Process.java @@ -29,6 +29,10 @@ * @author David M. Lloyd */ public final class Process { + private static final ProcessHandle current = doPrivileged((PrivilegedAction) ProcessHandle::current); + private static final ProcessHandle.Info currentInfo = current.info(); + private static final String name = doPrivileged((PrivilegedAction) Process::computeProcessName); + private Process() { } @@ -39,14 +43,13 @@ private Process() { * @return the process name (not {@code null}) */ public static String getProcessName() { - return doPrivileged((PrivilegedAction) Process::computeProcessName); + return name; } private static String computeProcessName() { - final ProcessHandle processHandle = ProcessHandle.current(); String processName = System.getProperty("jboss.process.name"); if (processName == null) { - processName = processHandle.info().command().orElse(null); + processName = currentInfo.command().orElse(null); } if (processName == null) { processName = ""; @@ -62,7 +65,7 @@ private static String computeProcessName() { */ @Deprecated(since = "2.4", forRemoval = true) public static long getProcessId() { - return currentProcess().pid(); + return current.pid(); } /** @@ -73,12 +76,7 @@ public static long getProcessId() { */ @Deprecated(since = "2.4", forRemoval = true) public static ProcessInfo getCurrentProcess() { - return new ProcessInfo(currentProcess().pid(), getProcessName()); - } - - // do not make this public - private static ProcessHandle currentProcess() { - return doPrivileged((PrivilegedAction) ProcessHandle::current); + return new ProcessInfo(current.pid(), getProcessName()); } /**