Skip to content
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

Cache current process handle #339

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
18 changes: 8 additions & 10 deletions os/src/main/java/io/smallrye/common/os/Process.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public final class Process {
private static final ProcessHandle current = doPrivileged((PrivilegedAction<ProcessHandle>) ProcessHandle::current);
dmlloyd marked this conversation as resolved.
Show resolved Hide resolved
private static final ProcessHandle.Info currentInfo = current.info();
private static final String name = doPrivileged((PrivilegedAction<String>) Process::computeProcessName);

private Process() {
}

Expand All @@ -39,14 +43,13 @@ private Process() {
* @return the process name (not {@code null})
*/
public static String getProcessName() {
return doPrivileged((PrivilegedAction<String>) 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 = "<unknown>";
Expand All @@ -62,7 +65,7 @@ private static String computeProcessName() {
*/
@Deprecated(since = "2.4", forRemoval = true)
public static long getProcessId() {
return currentProcess().pid();
return current.pid();
}

/**
Expand All @@ -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>) ProcessHandle::current);
return new ProcessInfo(current.pid(), getProcessName());
}

/**
Expand Down