Skip to content

Commit

Permalink
Replace int with string on thread monitor stat collection (#310)
Browse files Browse the repository at this point in the history
* Replace int with string on thread monitor stat collection
  • Loading branch information
Leonardo Parente authored Jun 28, 2022
1 parent 5583d28 commit fbcfbb8
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/handlers/input_resources/ThreadMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,17 +48,20 @@ class ThreadMonitor
}
system_total_time = system_total_time / sysconf(_SC_NPROCESSORS_ONLN);

std::vector<uint64_t> stats;
std::vector<std::string> 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() > PROC_STAT_POS_STIME)
break;
}
if(stats.size() < 10) {

if (stats.size() <= PROC_STAT_POS_STIME) {
return 0.0;
}

uint64_t thread_total_time = (stats[8] + stats[9]);
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;
Expand Down

0 comments on commit fbcfbb8

Please sign in to comment.