From 356488aa53e8c0bedeb7641685d931c8900098c5 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Tue, 30 Mar 2021 15:55:48 +1100 Subject: Request the realtime and monotonic clock times once per sample Refactor the sample time code to make one call to gettimeofday (aka the realtime clock in clock_gettime, when available) and one to the monotonic clock. Stores each in more appropriately named ProcessList fields for ready access when needed. Every platform gets the opportunity to provide their own clock code, and the existing Mac OS X specific code is moved below darwin instead of in Compat. A couple of leftover time(2) calls are converted to use these ProcessList fields as well, instead of yet again sampling the system clock. Related to https://github.com/htop-dev/htop/pull/574 --- ProcessList.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'ProcessList.c') diff --git a/ProcessList.c b/ProcessList.c index cd8ebd8a..6bc15611 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -10,11 +10,12 @@ in the source distribution for its full text. #include #include #include +#include -#include "Compat.h" #include "CRT.h" #include "Hashtable.h" #include "Macros.h" +#include "Platform.h" #include "Vector.h" #include "XUtils.h" @@ -35,7 +36,7 @@ ProcessList* ProcessList_init(ProcessList* this, const ObjectClass* klass, Users // set later by platform-specific code this->cpuCount = 0; - this->scanTs = 0; + this->monotonicMs = 0; #ifdef HAVE_LIBHWLOC this->topologyOk = false; @@ -131,7 +132,7 @@ void ProcessList_add(ProcessList* this, Process* p) { p->processList = this; // highlighting processes found in first scan by first scan marked "far in the past" - p->seenTs = this->scanTs; + p->seenStampMs = this->monotonicMs; Vector_add(this->processes, p); Hashtable_put(this->processTable, p->pid, p); @@ -582,8 +583,6 @@ Process* ProcessList_getProcess(ProcessList* this, pid_t pid, bool* preExisting, } void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate) { - struct timespec now; - // in pause mode only gather global data for meters (CPU/memory/...) if (pauseProcessUpdate) { ProcessList_goThroughEntries(this, true); @@ -604,31 +603,29 @@ void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate) { this->runningTasks = 0; - // set scanTs + // set scan timestamp static bool firstScanDone = false; - if (!firstScanDone) { - this->scanTs = 0; + if (firstScanDone) { + Platform_gettime_monotonic(&this->monotonicMs); + } else { + this->monotonicMs = 0; firstScanDone = true; - } else if (Compat_clock_monotonic_gettime(&now) == 0) { - // save time in millisecond, so with a delay in deciseconds - // there are no irregularities - this->scanTs = 1000 * now.tv_sec + now.tv_nsec / 1000000; } ProcessList_goThroughEntries(this, false); for (int i = Vector_size(this->processes) - 1; i >= 0; i--) { Process* p = (Process*) Vector_get(this->processes, i); - if (p->tombTs > 0) { + if (p->tombStampMs > 0) { // remove tombed process - if (this->scanTs >= p->tombTs) { + if (this->monotonicMs >= p->tombStampMs) { ProcessList_remove(this, p); } } else if (p->updated == false) { // process no longer exists if (this->settings->highlightChanges && p->wasShown) { // mark tombed - p->tombTs = this->scanTs + 1000 * this->settings->highlightDelaySecs; + p->tombStampMs = this->monotonicMs + 1000 * this->settings->highlightDelaySecs; } else { // immediately remove ProcessList_remove(this, p); -- cgit v1.2.3