From d78f260a73cc3c895a534cb8eb02334558768d5b Mon Sep 17 00:00:00 2001 From: Ralf Schmelter Date: Mon, 16 Nov 2026 16:12:33 +0100 Subject: [PATCH] Clean downport from head. --- src/hotspot/os/linux/os_linux.cpp | 8 +++++--- src/hotspot/os/linux/vitals_linux_oswrapper.cpp | 10 +++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/hotspot/os/linux/os_linux.cpp b/src/hotspot/os/linux/os_linux.cpp index e0a1f359a3c..d6df3035d5e 100644 --- a/src/hotspot/os/linux/os_linux.cpp +++ b/src/hotspot/os/linux/os_linux.cpp @@ -2286,7 +2286,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]; @@ -2296,7 +2297,8 @@ 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) { + // 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, ')'); if (prefix_end != nullptr) { @@ -2319,7 +2321,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); } ::free(line_buf); diff --git a/src/hotspot/os/linux/vitals_linux_oswrapper.cpp b/src/hotspot/os/linux/vitals_linux_oswrapper.cpp index e09c0be17bc..62848ebf4e4 100644 --- a/src/hotspot/os/linux/vitals_linux_oswrapper.cpp +++ b/src/hotspot/os/linux/vitals_linux_oswrapper.cpp @@ -514,9 +514,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) == 2)) { + _proc_cpu_us = cpu_utime; + _proc_cpu_sy = cpu_stime; + } } #ifdef __GLIBC__