From 581916b5bf9c277ea3341f1db000e2efc5bdbb5c Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 2 Jul 2026 14:15:07 +0200 Subject: [PATCH 1/2] Refactor BSD os::rss and os::Bsd::get_process_uid Moved the common sysctl call to a helper function `bsd_get_procinfo`, and simplified it a bit. This work is sponsored by The FreeBSD Foundation --- src/hotspot/os/bsd/os_bsd.cpp | 40 ++++++++++++++++------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/hotspot/os/bsd/os_bsd.cpp b/src/hotspot/os/bsd/os_bsd.cpp index 50fe90e0a3768..67318b7fa4f92 100644 --- a/src/hotspot/os/bsd/os_bsd.cpp +++ b/src/hotspot/os/bsd/os_bsd.cpp @@ -66,6 +66,7 @@ #include "utilities/defaultStream.hpp" #include "utilities/events.hpp" #include "utilities/growableArray.hpp" +#include "utilities/macros.hpp" #include "utilities/vmError.hpp" #if INCLUDE_JFR #include "jfr/jfrEvents.hpp" @@ -354,6 +355,17 @@ physical_memory_size_type os::Machine::physical_memory() { return Bsd::physical_memory(); } +static int bsd_get_procinfo(pid_t pid, struct KINFO_PROC_T *kp, size_t *bufSize) { + int mib[] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid, +# if !defined(__FreeBSD__) && !defined(__APPLE__) + static_cast(*bufSize), 1 +# endif + }; + const u_int namelen = sizeof(mib)/sizeof(mib[0]); + + return sysctl(mib, namelen, kp, bufSize, nullptr, 0); +} + size_t os::rss() { size_t rss = 0; #ifdef __APPLE__ @@ -366,19 +378,11 @@ size_t os::rss() { rss = info.resident_size; } #else - pid_t pid = getpid(); struct KINFO_PROC_T kp; - size_t bufSize = sizeof kp; -#ifndef __FreeBSD__ - u_int namelen = 6; - int mib[6] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid, - static_cast(bufSize), 1}; -#else - u_int namelen = 4; - int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid}; -#endif - if (sysctl(mib, namelen, &kp, &bufSize, nullptr, 0) != -1) { - return kp.KI_RSS * getpagesize(); + size_t bufSize = sizeof(kp); + + if (bsd_get_procinfo(getpid(), &kp, &bufSize) != -1) { + rss = kp.KI_RSS * getpagesize(); } #endif // __APPLE__ @@ -1034,17 +1038,9 @@ pid_t os::Bsd::gettid() { // Returns the uid of a process or -1 on error. uid_t os::Bsd::get_process_uid(pid_t pid) { - struct KINFO_PROC_T kp; + struct kinfo_proc kp; size_t size = sizeof kp; -#if defined(__FreeBSD__) || defined(__APPLE__) - u_int namelen = 4; - int mib_kern[4] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid}; -#else - u_int namelen = 6; - int mib_kern[6] = {CTL_KERN, KERN_PROC_MIB, KERN_PROC_PID, pid, - static_cast(size), 1}; -#endif - if (sysctl(mib_kern, namelen, &kp, &size, nullptr, 0) == 0) { + if (bsd_get_procinfo(pid, &kp, &size) == 0) { if (size > 0 && kp.KI_PID == pid) { return kp.KI_UID; } From de577e8023e6478958b056cddc3a5087173ec175 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 12 Jul 2026 12:29:38 +0200 Subject: [PATCH 2/2] Remove unnecessary include of macros.hpp This work is sponsored by The FreeBSD Foundation --- src/hotspot/os/bsd/os_bsd.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hotspot/os/bsd/os_bsd.cpp b/src/hotspot/os/bsd/os_bsd.cpp index 67318b7fa4f92..bf615e72b3b66 100644 --- a/src/hotspot/os/bsd/os_bsd.cpp +++ b/src/hotspot/os/bsd/os_bsd.cpp @@ -66,7 +66,6 @@ #include "utilities/defaultStream.hpp" #include "utilities/events.hpp" #include "utilities/growableArray.hpp" -#include "utilities/macros.hpp" #include "utilities/vmError.hpp" #if INCLUDE_JFR #include "jfr/jfrEvents.hpp"