From 21c9ffa15ed099940c2f5e0094d97bfe00bb06da Mon Sep 17 00:00:00 2001 From: albert <> Date: Thu, 10 Oct 2002 00:01:37 +0000 Subject: [PATCH] top gets pcpu work --- proc/alloc.c | 1 + proc/output.c | 1 + proc/sig.c | 1 + proc/status.c | 1 + proc/version.c | 1 + top.c | 44 ++++++++++++++++++++++++-------------------- top.h | 2 +- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/proc/alloc.c b/proc/alloc.c index 4f495e5d..0868c68a 100644 --- a/proc/alloc.c +++ b/proc/alloc.c @@ -7,6 +7,7 @@ \***********************************************************************/ #include #include +#include "procps.h" void *xcalloc(void *pointer, int size) { void * ret; diff --git a/proc/output.c b/proc/output.c index a1a0c6a5..76ae2616 100644 --- a/proc/output.c +++ b/proc/output.c @@ -5,6 +5,7 @@ #include #include #include +#include "procps.h" /* output a string, converting unprintables to octal as we go, and stopping after processing max chars of output (accounting for expansion due to octal rep). diff --git a/proc/sig.c b/proc/sig.c index 3f35d31b..d2c543ef 100644 --- a/proc/sig.c +++ b/proc/sig.c @@ -12,6 +12,7 @@ #include #include #include +#include "sig.h" /* Linux signals: * diff --git a/proc/status.c b/proc/status.c index 712743aa..c267200e 100644 --- a/proc/status.c +++ b/proc/status.c @@ -9,6 +9,7 @@ #include "proc/procps.h" #include "proc/readproc.h" +#include "status.h" char * status(proc_t* task) { static char buf[4] = " "; diff --git a/proc/version.c b/proc/version.c index 538fe556..6410bf0e 100644 --- a/proc/version.c +++ b/proc/version.c @@ -9,6 +9,7 @@ #endif #include #include +#include "version.h" #ifdef MINORVERSION char procps_version[] = "procps version " VERSION "." SUBVERSION "." MINORVERSION; diff --git a/top.c b/top.c index 4ef740db..a6a7279f 100644 --- a/top.c +++ b/top.c @@ -149,11 +149,13 @@ static WIN_t *Winstk [GROUPSMAX], /* Frame oriented stuff that can't remain local to any 1 function and/or that would be too cumbersome managed as parms */ -static int Frame_maxtask, /* last known number of active tasks */ +static int Frame_maxtask; /* last known number of active tasks */ /* ie. current 'size' of proc table */ - Frame_srtflg, /* the subject window sort direction */ - Frame_ctimes, /* the subject window's ctimes flag */ - Frame_cmdlin; /* the subject window's cmdlin flag */ +static float Frame_etime, /* elapsed time twix this & prior */ + Frame_scale; /* so we can '*' vs. '/' IF 'pcpu' */ +static int Frame_srtflg, /* the subject window sort direction */ + Frame_ctimes, /* the subject window's ctimes flag */ + Frame_cmdlin; /* the subject window's cmdlin flag */ /* ////////////////////////////////////////////////////////////// */ @@ -753,9 +755,10 @@ static char *scale_tics (TICS_t tics, const int width) /* - * Calculate and return the elapsed time since the last update - * which is then used in % CPU calc's. */ -static float time_elapsed (void) + * Calculate and the elapsed time since the last update along with the + * scaling factor used in multiplication (vs. division) when calculating + * a displayable task's %CPU. */ +static void time_elapsed (void) { static struct timeval oldtimev; struct timeval timev; @@ -767,7 +770,9 @@ static float time_elapsed (void) + (float)(timev.tv_usec - oldtimev.tv_usec) / 1000000.0; oldtimev.tv_sec = timev.tv_sec; oldtimev.tv_usec = timev.tv_usec; - return et; + Frame_etime = et; + /* if in Solaris mode, adjust our scaling for all cpus */ + Frame_scale = 100.0f / ((float)Hertz * (float)et * (Mode_irixps ? 1 : Cpu_tot)); } @@ -1731,7 +1736,7 @@ static void cpudo (CPUS_t *cpu, const char *pfx) /* * Calc the number of tasks in each state (run, sleep, etc) - * Calc percent cpu usage for each task (pcpu) + * Prepare for the possible calculation of percent cpu usage (pcpu) * Calc the cpu(s) percent in each state (user, system, nice, idle) * AND establish the total number of tasks for this frame! */ static void frame_states (proc_t **ppt, int show) @@ -1740,7 +1745,6 @@ static void frame_states (proc_t **ppt, int show) static unsigned hist_siz; HIST_t *hist_new; unsigned total, running, sleeping, stopped, zombie; - float etime; int i; if (!hist_sav) { @@ -1750,7 +1754,7 @@ static void frame_states (proc_t **ppt, int show) } hist_new = alloc_c(hist_siz); total = running = sleeping = stopped = zombie = 0; - etime = time_elapsed(); + time_elapsed(); /* make a pass through the data to get stats */ while (-1 != ppt[total]->pid) { /* calculations //// */ @@ -1790,13 +1794,9 @@ static void frame_states (proc_t **ppt, int show) break; } } - /* finally calculate an integer version of %cpu for this task - and plug it into the unfilled slot in proc_t */ - this->pcpu = (tics * 1000 / (TICS_t)Hertz) / etime; - if (this->pcpu > 999) this->pcpu = 999; - /* if in Solaris mode, adjust cpu percentage not only for the cpu - the process is running on, but for all cpus together */ - if (!Mode_irixps) this->pcpu /= Cpu_tot; + /* we're just saving elapsed tics, to be converted into %cpu if + this task wins it's displayable screen row lottery... */ + this->pcpu = tics; total++; } /* end: while 'pids' */ @@ -1982,7 +1982,11 @@ static void show_a_task (WIN_t *q, proc_t *task) #endif break; case P_CPU: - MKCOL(q, i, a, &pad, cbuf, (float)task->pcpu / 10); + { float u = (float)task->pcpu * Frame_scale; + + if (99.9 < u) u = 99.9; + MKCOL(q, i, a, &pad, cbuf, u); + } break; case P_DAT: MKCOL(q, i, a, &pad, cbuf, scale_num(PAGES_2K(task->drs), w, s)); @@ -2465,7 +2469,7 @@ static proc_t **do_summary (void) } /* - ** Display Tasks and Cpu(s) states and also calc 'pcpu', + ** Display Tasks and Cpu(s) states and also prime for potential 'pcpu', ** but NO table sort yet -- that's done on a per window basis! */ p_table = refreshprocs(p_table); frame_states(p_table, CHKw(Curwin, View_STATES)); diff --git a/top.h b/top.h index f87024a6..f0759ab9 100644 --- a/top.h +++ b/top.h @@ -528,7 +528,7 @@ typedef struct win { //atic int get_int (const char *prompt); //atic char *scale_num (unsigned num, const int width, const unsigned type); //atic char *scale_tics (TICS_t tics, const int width); -//atic float time_elapsed (void); +//atic void time_elapsed (void); /*------ Library Alternatives ------------------------------------------*/ //atic void *alloc_c (unsigned numb); //atic void *alloc_r (void *q, unsigned numb);