Refactor generating starttime string into Process class

This commit is contained in:
Christian Göttsche 2020-10-13 14:26:40 +02:00 committed by cgzones
parent 783be7711d
commit a63cfc8b7c
10 changed files with 19 additions and 40 deletions

View File

@ -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) { static inline void Process_writeCommand(const Process* this, int attr, int baseattr, RichString* str) {
int start = RichString_size(str), finish = 0; int start = RichString_size(str), finish = 0;
const char* comm = this->comm; const char* comm = this->comm;

View File

@ -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_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_outputRate(RichString* str, char* buffer, int n, double rate, int coloring);
void Process_display(const Object* cast, RichString* out); void Process_display(const Object* cast, RichString* out);

View File

@ -50,14 +50,6 @@ bool Process_isThread(const Process* this) {
return false; 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) { char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int* basenameOffset) {
/* This function is from the old Mac version of htop. Originally from ps? */ /* This function is from the old Mac version of htop. Originally from ps? */
int mib[3], argmax, nargs, c = 0; int mib[3], argmax, nargs, c = 0;
@ -201,7 +193,7 @@ ERROR_A:
return retval; 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; struct extern_proc *ep = &ps->kp_proc;
/* UNSET HERE : /* UNSET HERE :
@ -231,7 +223,9 @@ void DarwinProcess_setFromKInfoProc(Process *proc, struct kinfo_proc *ps, time_t
/* e_tdev == -1 for "no device" */ /* e_tdev == -1 for "no device" */
proc->tty_nr = ps->kp_eproc.e_tdev & 0xff; /* TODO tty_nr is unsigned */ 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)); proc->comm = DarwinProcess_getCmdLine(ps, &(proc->basenameOffset));
} }

View File

@ -28,11 +28,9 @@ void Process_delete(Object* cast);
bool Process_isThread(const Process* this); 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); 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); void DarwinProcess_setFromLibprocPidinfo(DarwinProcess *proc, DarwinProcessList *dpl);

View File

@ -150,9 +150,6 @@ void ProcessList_goThroughEntries(ProcessList* super) {
struct kinfo_proc *ps; struct kinfo_proc *ps;
size_t count; size_t count;
DarwinProcess *proc; DarwinProcess *proc;
struct timeval tv;
gettimeofday(&tv, NULL); /* Start processing time */
/* Update the global data (CPU times and VM stats) */ /* Update the global data (CPU times and VM stats) */
ProcessList_freeCPULoadInfo(&dpl->prev_load); ProcessList_freeCPULoadInfo(&dpl->prev_load);
@ -187,7 +184,7 @@ void ProcessList_goThroughEntries(ProcessList* super) {
for(size_t i = 0; i < count; ++i) { for(size_t i = 0; i < count; ++i) {
proc = (DarwinProcess *)ProcessList_getProcess(super, ps[i].kp_proc.p_pid, &preExisting, (Process_New)DarwinProcess_new); 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); DarwinProcess_setFromLibprocPidinfo(proc, dpl);
// Disabled for High Sierra due to bug in macOS High Sierra // Disabled for High Sierra due to bug in macOS High Sierra

View File

@ -389,14 +389,10 @@ void ProcessList_goThroughEntries(ProcessList* this) {
int count = 0; int count = 0;
struct kinfo_proc* kprocs = kvm_getprocs(fpl->kd, KERN_PROC_PROC, 0, &count); 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++) { for (int i = 0; i < count; i++) {
struct kinfo_proc* kproc = &kprocs[i]; struct kinfo_proc* kproc = &kprocs[i];
bool preExisting = false; bool preExisting = false;
// TODO: bool isIdleProcess = false; // TODO: bool isIdleProcess = false;
struct tm date;
Process* proc = ProcessList_getProcess(this, kproc->ki_pid, &preExisting, (Process_New) FreeBSDProcess_new); Process* proc = ProcessList_getProcess(this, kproc->ki_pid, &preExisting, (Process_New) FreeBSDProcess_new);
FreeBSDProcess* fp = (FreeBSDProcess*) proc; FreeBSDProcess* fp = (FreeBSDProcess*) proc;
@ -417,6 +413,7 @@ void ProcessList_goThroughEntries(ProcessList* this) {
proc->pgrp = kproc->ki_pgid; proc->pgrp = kproc->ki_pgid;
proc->st_uid = kproc->ki_uid; proc->st_uid = kproc->ki_uid;
proc->starttime_ctime = kproc->ki_start.tv_sec; proc->starttime_ctime = kproc->ki_start.tv_sec;
Process_fillStarttimeBuffer(proc);
proc->user = UsersTable_getRef(this->usersTable, proc->st_uid); proc->user = UsersTable_getRef(this->usersTable, proc->st_uid);
ProcessList_add((ProcessList*)this, proc); ProcessList_add((ProcessList*)this, proc);
proc->comm = FreeBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset); proc->comm = FreeBSDProcessList_readProcessName(fpl->kd, kproc, &proc->basenameOffset);
@ -490,9 +487,6 @@ void ProcessList_goThroughEntries(ProcessList* this) {
this->kernelThreads++; 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++; this->totalTasks++;
if (proc->state == 'R') if (proc->state == 'R')
this->runningTasks++; this->runningTasks++;

View File

@ -1035,9 +1035,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char*
goto errorReadingProcess; goto errorReadingProcess;
} }
struct tm date; Process_fillStarttimeBuffer(proc);
(void) localtime_r(&proc->starttime_ctime, &date);
strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date);
ProcessList_add(pl, proc); ProcessList_add(pl, proc);
} else { } else {

View File

@ -192,8 +192,6 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {
bool preExisting; bool preExisting;
Process* proc; Process* proc;
OpenBSDProcess* fp; OpenBSDProcess* fp;
struct tm date;
struct timeval tv;
int count = 0; int count = 0;
int i; 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 = kvm_getprocs(this->kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
//struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count); //struct kinfo_proc* kprocs = getprocs(KERN_PROC_ALL, 0, &count);
gettimeofday(&tv, NULL);
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
kproc = &kprocs[i]; kproc = &kprocs[i];
@ -222,11 +218,10 @@ static inline void OpenBSDProcessList_scanProcs(OpenBSDProcessList* this) {
proc->pgrp = kproc->p__pgid; proc->pgrp = kproc->p__pgid;
proc->st_uid = kproc->p_uid; proc->st_uid = kproc->p_uid;
proc->starttime_ctime = kproc->p_ustart_sec; proc->starttime_ctime = kproc->p_ustart_sec;
Process_fillStarttimeBuffer(proc);
proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid); proc->user = UsersTable_getRef(this->super.usersTable, proc->st_uid);
ProcessList_add(&this->super, proc); ProcessList_add(&this->super, proc);
proc->comm = OpenBSDProcessList_readProcessName(this->kd, kproc, &proc->basenameOffset); 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 { } else {
if (settings->updateProcessNames) { if (settings->updateProcessNames) {
free(proc->comm); free(proc->comm);

View File

@ -260,8 +260,6 @@ void ProcessList_delete(ProcessList* pl) {
*/ */
int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void *listptr) { int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void *listptr) {
struct timeval tv;
struct tm date;
bool preExisting; bool preExisting;
pid_t getpid; 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); Process *proc = ProcessList_getProcess(pl, getpid, &preExisting, (Process_New) SolarisProcess_new);
SolarisProcess *sproc = (SolarisProcess*) proc; SolarisProcess *sproc = (SolarisProcess*) proc;
gettimeofday(&tv, NULL);
// Common code pass 1 // Common code pass 1
proc->show = false; proc->show = false;
sproc->taskid = _psinfo->pr_taskid; sproc->taskid = _psinfo->pr_taskid;
@ -368,8 +364,7 @@ int SolarisProcessList_walkproc(psinfo_t *_psinfo, lwpsinfo_t *_lwpsinfo, void *
} else { } else {
sproc->kernel = false; sproc->kernel = false;
} }
(void) localtime_r((time_t*) &proc->starttime_ctime, &date); Process_fillStarttimeBuffer(proc);
strftime(proc->starttime_show, 7, ((proc->starttime_ctime > tv.tv_sec - 86400) ? "%R " : "%b%d "), &date);
ProcessList_add(pl, proc); ProcessList_add(pl, proc);
} }
proc->updated = true; proc->updated = true;

View File

@ -56,8 +56,8 @@ void ProcessList_goThroughEntries(ProcessList* super) {
proc->priority = 0; proc->priority = 0;
proc->nice = 0; proc->nice = 0;
proc->nlwp = 1; proc->nlwp = 1;
strncpy(proc->starttime_show, "Jun 01 ", sizeof(proc->starttime_show));
proc->starttime_ctime = 1433116800; // Jun 01, 2015 proc->starttime_ctime = 1433116800; // Jun 01, 2015
Process_fillStarttimeBuffer(proc);
proc->m_size = 100; proc->m_size = 100;
proc->m_resident = 100; proc->m_resident = 100;