From d3ac5c5b2435b95cdb6fb77a38fd236739d5f3b8 Mon Sep 17 00:00:00 2001 From: Ralf Schmelter Date: Thu, 29 Oct 2026 16:32:05 +0100 Subject: [PATCH 1/4] Temp --- src/hotspot/os/linux/os_linux.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index b2db5a51d54..977fee1ef0f 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -2484,7 +2484,11 @@ static bool is_numerical_id(const char* s) { bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) { TraceTime timer("Iterating all processes", TRACETIME_LOG(Trace, os, timer)); DIR* d = ::opendir("/proc"); - if (d != nullptr) { + size_t line_length = 256; + char* line_buf = (char*) os::malloc(line_length, mtInternal); + bool use_getline = true; + bool result = false; + if ((d != nullptr) && (line_buf != nullptr)) { uint64_t procs_count = 0; uint64_t threads_count = 0; struct dirent* en = nullptr; @@ -2503,7 +2507,17 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) // See man proc(5) // (20) num_threads %ld long num_threads = 0; - if (fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %ld", &num_threads) == 1) { + if (use_getline && (getline(&line_buf, &line_length, fp) > 0)) { + char* prefix_end = strrchr(line_buf, ')'); + + if (prefix_end != nullptr) { + if (sscanf(prefix_end + 1, " %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %ld", + &num_threads) == 1) { + threads_count += num_threads; + } + } + } else if (fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %ld", + &num_threads) == 1) { threads_count += num_threads; } ::fclose(fp); @@ -2519,10 +2533,12 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) *threads = threads_count; } - return true; + result = true; } - return false; + os::free(line_buf); + + return result; } #ifdef __GLIBC__ From 2ff807d43a8323226d3063873ae002dcf3db78d9 Mon Sep 17 00:00:00 2001 From: Ralf Schmelter Date: Fri, 30 Oct 2026 10:40:17 +0100 Subject: [PATCH 2/4] Removed fallback and used glib/musl malloc. --- src/hotspot/os/linux/os_linux.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 977fee1ef0f..319d1463c13 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -2485,8 +2485,7 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) TraceTime timer("Iterating all processes", TRACETIME_LOG(Trace, os, timer)); DIR* d = ::opendir("/proc"); size_t line_length = 256; - char* line_buf = (char*) os::malloc(line_length, mtInternal); - bool use_getline = true; + char* line_buf = threads == nullptr ? nullptr : (char*) permit_forbidden_function::malloc(line_length); bool result = false; if ((d != nullptr) && (line_buf != nullptr)) { uint64_t procs_count = 0; @@ -2507,7 +2506,7 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) // See man proc(5) // (20) num_threads %ld long num_threads = 0; - if (use_getline && (getline(&line_buf, &line_length, fp) > 0)) { + if (getline(&line_buf, &line_length, fp) > 0) { char* prefix_end = strrchr(line_buf, ')'); if (prefix_end != nullptr) { @@ -2516,9 +2515,6 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) threads_count += num_threads; } } - } else if (fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %ld", - &num_threads) == 1) { - threads_count += num_threads; } ::fclose(fp); } @@ -2536,7 +2532,7 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) result = true; } - os::free(line_buf); + permit_forbidden_function::free(line_buf); return result; } From 6daeebce2a9a460ee7e38db4db51e0d94408ff3f Mon Sep 17 00:00:00 2001 From: Ralf Schmelter Date: Fri, 30 Oct 2026 14:59:12 +0100 Subject: [PATCH 3/4] Get number of procs without number of threads. --- src/hotspot/os/linux/os_linux.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 319d1463c13..9398c49cbe1 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -2487,7 +2487,7 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) size_t line_length = 256; char* line_buf = threads == nullptr ? nullptr : (char*) permit_forbidden_function::malloc(line_length); bool result = false; - if ((d != nullptr) && (line_buf != nullptr)) { + if (d != nullptr) { uint64_t procs_count = 0; uint64_t threads_count = 0; struct dirent* en = nullptr; From 5efd64503828ffa58298e8b47c0223d40fb040e1 Mon Sep 17 00:00:00 2001 From: Ralf Schmelter Date: Sun, 1 Nov 2026 12:51:22 +0100 Subject: [PATCH 4/4] Fixed missing include. --- src/hotspot/os/linux/os_linux.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 9398c49cbe1..71d73e17329 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -77,6 +77,8 @@ #include "utilities/globalDefinitions.hpp" #include "utilities/growableArray.hpp" #include "utilities/macros.hpp" +// SapMachine 2026-11-01 +#include "utilities/permitForbiddenFunctions.hpp" #include "utilities/powerOfTwo.hpp" #include "utilities/vmError.hpp" #if INCLUDE_JFR