diff options
author | Nathan Scott <nathans@redhat.com> | 2021-03-30 15:55:48 +1100 |
---|---|---|
committer | Sohaib <sohaib.amhmd@gmail.com> | 2021-04-05 23:41:07 +0200 |
commit | 356488aa53e8c0bedeb7641685d931c8900098c5 (patch) | |
tree | c09f2df8264cc4075b72a55409ae261c021cda55 /ProcessList.c | |
parent | 421bdeec603b4fb1a4edec0e802c437fbe47fca0 (diff) |
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
Diffstat (limited to 'ProcessList.c')
-rw-r--r-- | ProcessList.c | 27 |
1 files changed, 12 insertions, 15 deletions
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 <assert.h> #include <stdlib.h> #include <string.h> +#include <sys/time.h> -#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); |