From 9803f8b5fc68aba873aebcc2c084ca2a2031ada5 Mon Sep 17 00:00:00 2001 From: Leonardo Parente Date: Mon, 27 Jun 2022 14:22:08 -0400 Subject: [PATCH 1/2] Replace int with string on thread monitor stat collection --- src/handlers/input_resources/ThreadMonitor.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/handlers/input_resources/ThreadMonitor.h b/src/handlers/input_resources/ThreadMonitor.h index 8172e5fae..d592e2176 100644 --- a/src/handlers/input_resources/ThreadMonitor.h +++ b/src/handlers/input_resources/ThreadMonitor.h @@ -46,17 +46,17 @@ class ThreadMonitor } system_total_time = system_total_time / sysconf(_SC_NPROCESSORS_ONLN); - std::vector stats; + std::vector stats; std::ifstream thread_stat("/proc/thread-self/stat"); - thread_stat.ignore(' '); - while (thread_stat >> stat) { - stats.push_back(stat); + std::string stat_str; + while (thread_stat >> stat_str) { + stats.push_back(stat_str); } - if(stats.size() < 10) { + if(stats.size() < 16) { return 0.0; } - uint64_t thread_total_time = (stats[8] + stats[9]); + uint64_t thread_total_time = std::stoull(stats[14]) + std::stoull(stats[15]); uint64_t current_thread_time = thread_total_time - _last_thread_time; _last_thread_time = thread_total_time; From 41ec2663b11e6e330210631fd8d0c46b908f968c Mon Sep 17 00:00:00 2001 From: Leonardo Parente Date: Mon, 27 Jun 2022 18:59:48 -0400 Subject: [PATCH 2/2] remove magic numbers --- src/handlers/input_resources/ThreadMonitor.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/handlers/input_resources/ThreadMonitor.h b/src/handlers/input_resources/ThreadMonitor.h index d592e2176..83f5498b2 100644 --- a/src/handlers/input_resources/ThreadMonitor.h +++ b/src/handlers/input_resources/ThreadMonitor.h @@ -20,6 +20,8 @@ class ThreadMonitor #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #elif __APPLE__ #elif __linux__ + static constexpr size_t PROC_STAT_POS_UTIME = 13; + static constexpr size_t PROC_STAT_POS_STIME = 14; uint64_t _last_system_time = 0; uint64_t _last_thread_time = 0; #endif @@ -51,12 +53,15 @@ class ThreadMonitor std::string stat_str; while (thread_stat >> stat_str) { stats.push_back(stat_str); + if (stats.size() > PROC_STAT_POS_STIME) + break; } - if(stats.size() < 16) { + + if (stats.size() <= PROC_STAT_POS_STIME) { return 0.0; } - uint64_t thread_total_time = std::stoull(stats[14]) + std::stoull(stats[15]); + uint64_t thread_total_time = std::stoull(stats[PROC_STAT_POS_UTIME]) + std::stoull(stats[PROC_STAT_POS_STIME]); uint64_t current_thread_time = thread_total_time - _last_thread_time; _last_thread_time = thread_total_time;