From fc98e10fefa9500036a82f7a112d17a243622cd8 Mon Sep 17 00:00:00 2001 From: Ralf Schmelter Date: Wed, 11 Nov 2026 14:08:56 +0100 Subject: [PATCH 1/2] Fixed parsing /proc//stat for all possible process names. --- src/hotspot/os/linux/os_linux.cpp | 5 +++-- src/hotspot/os/linux/vitals_linux_oswrapper.cpp | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 1994c7084e5..7b1f729ed3c 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -2498,7 +2498,8 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) if (en != nullptr) { if (is_numerical_id(en->d_name)) { procs_count++; - if (threads == nullptr) { + if (line_buf == nullptr) { + // No thread count to get or malloc failed. continue; } char tmp[128]; @@ -2508,7 +2509,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 (getline(&line_buf, &line_length, fp) > 0) { + if (getdelim(&line_buf, &line_length, 0, fp) > 0) { char* prefix_end = strrchr(line_buf, ')'); if (prefix_end != nullptr) { diff --git a/src/hotspot/os/linux/vitals_linux_oswrapper.cpp b/src/hotspot/os/linux/vitals_linux_oswrapper.cpp index 444d6367218..782a7abf769 100644 --- a/src/hotspot/os/linux/vitals_linux_oswrapper.cpp +++ b/src/hotspot/os/linux/vitals_linux_oswrapper.cpp @@ -498,9 +498,13 @@ ALL_VALUES_DO(RESETVAL) // (15) stime %lu long unsigned cpu_utime = 0; long unsigned cpu_stime = 0; - ::sscanf(text, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &cpu_utime, &cpu_stime); - _proc_cpu_us = cpu_utime; - _proc_cpu_sy = cpu_stime; + const char* prefix_end = strrchr(text, ')'); + + if (prefix_end != nullptr) { + ::sscanf(prefix_end + 1, " %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &cpu_utime, &cpu_stime); + _proc_cpu_us = cpu_utime; + _proc_cpu_sy = cpu_stime; + } } #ifdef __GLIBC__ From 9ed1579a11b1efe77211e8326f42cb2b56a93bfa Mon Sep 17 00:00:00 2001 From: Ralf Schmelter Date: Thu, 12 Nov 2026 14:17:25 +0100 Subject: [PATCH 2/2] Some improvements. --- src/hotspot/os/linux/os_linux.cpp | 3 ++- src/hotspot/os/linux/vitals_linux_oswrapper.cpp | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index 7b1f729ed3c..64a6bb816c4 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -2509,6 +2509,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; + // Use getdelim to read the whole file into the buffer if (getdelim(&line_buf, &line_length, 0, fp) > 0) { char* prefix_end = strrchr(line_buf, ')'); @@ -2532,7 +2533,7 @@ bool os::Linux::get_total_procs_and_threads(uint64_t* procs, uint64_t* threads) *threads = threads_count; } - result = true; + result = (line_buf != nullptr) || (threads == nullptr); } permit_forbidden_function::free(line_buf); diff --git a/src/hotspot/os/linux/vitals_linux_oswrapper.cpp b/src/hotspot/os/linux/vitals_linux_oswrapper.cpp index 782a7abf769..7b38d04a080 100644 --- a/src/hotspot/os/linux/vitals_linux_oswrapper.cpp +++ b/src/hotspot/os/linux/vitals_linux_oswrapper.cpp @@ -500,8 +500,8 @@ ALL_VALUES_DO(RESETVAL) long unsigned cpu_stime = 0; const char* prefix_end = strrchr(text, ')'); - if (prefix_end != nullptr) { - ::sscanf(prefix_end + 1, " %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &cpu_utime, &cpu_stime); + if ((prefix_end != nullptr) && + (::sscanf(prefix_end + 1, " %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", &cpu_utime, &cpu_stime) == 2)) { _proc_cpu_us = cpu_utime; _proc_cpu_sy = cpu_stime; }