From a63cfc8b7c172aa7e849521a479d39af737681a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 13 Oct 2020 14:26:40 +0200 Subject: Refactor generating starttime string into Process class --- Process.c | 6 ++++++ Process.h | 2 ++ darwin/DarwinProcess.c | 14 ++++---------- darwin/DarwinProcess.h | 4 +--- darwin/DarwinProcessList.c | 5 +---- freebsd/FreeBSDProcessList.c | 8 +------- linux/LinuxProcessList.c | 4 +--- openbsd/OpenBSDProcessList.c | 7 +------ solaris/SolarisProcessList.c | 7 +------ unsupported/UnsupportedProcessList.c | 2 +- 10 files changed, 19 insertions(+), 40 deletions(-) diff --git a/Process.c b/Process.c index 2d5ffb4f..43a4c47e 100644 --- a/Process.c +++ b/Process.c @@ -181,6 +181,12 @@ void Process_printTime(RichString* str, unsigned long long totalHundredths) { } } +void Process_fillStarttimeBuffer(Process* this) { + struct tm date; + (void) localtime_r(&this->starttime_ctime, &date); + strftime(this->starttime_show, sizeof(this->starttime_show) - 1, (this->starttime_ctime > (time(NULL) - 86400)) ? "%R " : "%b%d ", &date); +} + static inline void Process_writeCommand(const Process* this, int attr, int baseattr, RichString* str) { int start = RichString_size(str), finish = 0; const char* comm = this->comm; diff --git a/Process.h b/Process.h index 7e1bda68..a4abf03e 100644 --- a/Process.h +++ b/Process.h @@ -156,6 +156,8 @@ void Process_colorNumber(RichString* str, unsigned long long number, bool colori void Process_printTime(RichString* str, unsigned long long totalHundredths); +void Process_fillStarttimeBuffer(Process* this); + void Process_outputRate(RichString* str, char* buffer, int n, double rate, int coloring); void Process_display(const Object* cast, RichString* out); diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 8c90c2a5..48574e2f 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -50,14 +50,6 @@ bool Process_isThread(const Process* this) { return false; } -void DarwinProcess_setStartTime(Process *proc, struct extern_proc *ep, time_t now) { - struct tm date; - - proc->starttime_ctime = ep->p_starttime.tv_sec; - (void) localtime_r(&proc->starttime_ctime, &date); - strftime(proc->starttime_show, 7, ((proc->starttime_ctime > now - 86400) ? "%R " : "%b%d "), &date); -} - char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset) { /* This function is from the old Mac version of htop. Originally from ps? */ int mib[3], argmax, nargs, c = 0; @@ -201,7 +193,7 @@ ERROR_A: return retval; } -void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t now, bool exists) { +void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, bool exists) { struct extern_proc *ep = &ps->kp_proc; /* UNSET HERE : @@ -231,7 +223,9 @@ void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t /* e_tdev == -1 for "no device" */ proc->tty_nr = ps->kp_eproc.e_tdev & 0xff; /* TODO tty_nr is unsigned */ - DarwinProcess_setStartTime(proc, ep, now); + proc->starttime_ctime = ep->p_starttime.tv_sec; + Process_fillStarttimeBuffer(proc); + proc->comm = DarwinProcess_getCmdLine(ps, &(proc->basenameOffset)); } diff --git a/darwin/DarwinProcess.h b/darwin/DarwinProcess.h index fc932afe..69a1d63b 100644 --- a/darwin/DarwinProcess.h +++ b/darwin/DarwinProcess.h @@ -28,11 +28,9 @@ void Process_delete(Object* cast); bool Process_isThread(const Process* this); -void DarwinProcess_setStartTime(Process *proc, struct extern_proc *ep, time_t now); - char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset); -void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t now, bool exists); +void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, bool exists); void DarwinProcess_setFromLibprocPidinfo(DarwinProcess *proc, DarwinProcessList *dpl); diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index ad848877..10d0697f 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -150,9 +150,6 @@ void ProcessList_goThroughEntries(ProcessList* super) { struct kinfo_proc *ps; size_t count; DarwinProcess *proc; - struct timeval tv; - - gettimeofday(&tv, NULL); /* Start processing time */ /* Update the global data (CPU times and VM stats) */ ProcessList_freeCPULoadInfo(&dpl->prev_load); @@ -187,7 +184,7 @@ void ProcessList_goThroughEntries(ProcessList* super) { for(size_t i = 0; i < count; ++i) { proc = (DarwinProcess *)ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, (Process_New)DarwinProcess_new); - DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], tv.tv_sec, preExisting); + DarwinProcess_setFromKInfoProc(&proc->super, &ps[i], preExisting); DarwinProcess_setFromLibprocPidinfo(proc, dpl); // Disabled for High Sierra due to bug in macOS High Sierra diff --git a/freebsd/FreeBSDProcessList.c b/freebsd/FreeBSDProcessList.c index 71f87e16..92620c9b 100644 --- a/freebsd/FreeBSDProcessList.c +++ b/freebsd/FreeBSDProcessList.c @@ -389,14 +389,10 @@ void ProcessList_goThroughEntries(ProcessList* this) { int count = 0; struct kinfo_proc* kprocs = kvm_getprocs(fpl->kd, KERN_PROC_PROC, 0, &count); - struct timeval tv; - gettimeofday(&tv, NULL); - for (int i = 0; i < count; i++) { struct kinfo_proc* kproc = &kprocs[i]; bool preExisting = false; // TODO: bool isIdleProcess = false; - struct tm date; Process* proc = ProcessList_getProcess(this, kproc->ki_pid, &preExisting, (Process_New) FreeBSDProcess_new); FreeBSDProcess* fp = (FreeBSDProcess*) proc; @@ -417,6 +413,7 @@ void ProcessList_goThroughEntries(ProcessList* this) { proc->pgrp = kproc->ki_pgid; proc->st_uid = kproc->ki_uid; proc->starttime_ctime = kproc->ki_start.tv_sec; + Process_fillStarttimeBuffer(proc); proc->user = UsersTable_getRef(this->usersTable, proc->st_uid); ProcessList_add((ProcessList*)this, proc); proc->comm = FreeBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset); @@ -490,9 +487,6 @@ void ProcessList_goThroughEntries(ProcessList* this) { this->kernelThreads++; } - (void) localtime_r((time_t*) &proc->starttime_ctime, &date); - strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date); - this->totalTasks++; if (proc->state == 'R') this->runningTasks++; diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index ceb7f7bf..6dcc5e5a 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -1035,9 +1035,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* goto errorReadingProcess; } - struct tm date; - (void) localtime_r(&proc->starttime_ctime, &date); - strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date); + Process_fillStarttimeBuffer(proc); ProcessList_add(pl, proc); } else { diff --git a/openbsd/OpenBSDProcessList.c b/openbsd/OpenBSDProcessList.c index c3ba763a..4b030707 100644 --- a/openbsd/OpenBSDProcessList.c +++ b/openbsd/OpenBSDProcessList.c @@ -192,8 +192,6 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { bool preExisting; Process* proc; OpenBSDProcess* fp; - struct tm date; - struct timeval tv; int count = 0; int i; @@ -201,8 +199,6 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { struct kinfo_proc* kprocs = kvm_getprocs(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count); //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count); - gettimeofday(&tv, NULL); - for (i = 0; i < count; i++) { kproc = &kprocs[i]; @@ -222,11 +218,10 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) { proc->pgrp = kproc->p__pgid; proc->st_uid = kproc->p_uid; proc->starttime_ctime = kproc->p_ustart_sec; + Process_fillStarttimeBuffer(proc); proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid); ProcessList_add(&this->super, proc); proc->comm = OpenBSDProcessList_readProcessName(this->kd, kproc, &proc->basenameOffset); - (void) localtime_r((time_t*) &kproc->p_ustart_sec, &date); - strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date); } else { if (settings->updateProcessNames) { free(proc->comm); diff --git a/solaris/SolarisProcessList.c b/solaris/SolarisProcessList.c index dbbd54c2..9662f830 100644 --- a/solaris/SolarisProcessList.c +++ b/solaris/SolarisProcessList.c @@ -260,8 +260,6 @@ void ProcessList_delete(ProcessList* pl) { */ int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void *listptr) { - struct timeval tv; - struct tm date; bool preExisting; pid_t getpid; @@ -281,8 +279,6 @@ int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void * Process *proc = ProcessList_getProcess(pl, getpid, &preExisting, (Process_New) SolarisProcess_new); SolarisProcess *sproc = (SolarisProcess*) proc; - gettimeofday(&tv, NULL); - // Common code pass 1 proc->show = false; sproc->taskid = _psinfo->pr_taskid; @@ -368,8 +364,7 @@ int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void * } else { sproc->kernel = false; } - (void) localtime_r((time_t*) &proc->starttime_ctime, &date); - strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date); + Process_fillStarttimeBuffer(proc); ProcessList_add(pl, proc); } proc->updated = true; diff --git a/unsupported/UnsupportedProcessList.c b/unsupported/UnsupportedProcessList.c index 3531d0f9..40a24c28 100644 --- a/unsupported/UnsupportedProcessList.c +++ b/unsupported/UnsupportedProcessList.c @@ -56,8 +56,8 @@ void ProcessList_goThroughEntries(ProcessList* super) { proc->priority = 0; proc->nice = 0; proc->nlwp = 1; - strncpy(proc->starttime_show, "Jun 01 ", sizeof(proc->starttime_show)); proc->starttime_ctime = 1433116800; // Jun 01, 2015 + Process_fillStarttimeBuffer(proc); proc->m_size = 100; proc->m_resident = 100; -- cgit v1.2.3