]> granicus.if.org Git - procps-ng/commitdiff
top gets pcpu work
authoralbert <>
Thu, 10 Oct 2002 00:01:37 +0000 (00:01 +0000)
committeralbert <>
Thu, 10 Oct 2002 00:01:37 +0000 (00:01 +0000)
proc/alloc.c
proc/output.c
proc/sig.c
proc/status.c
proc/version.c
top.c
top.h

index 4f495e5d74ad6f01f5b46901017d9b2eb96a4521..0868c68a6ee562726c1f00cdd9bb3901ad7645c7 100644 (file)
@@ -7,6 +7,7 @@
 \***********************************************************************/
 #include <stdlib.h>
 #include <stdio.h>
+#include "procps.h"
 
 void *xcalloc(void *pointer, int size) {
     void * ret;
index a1a0c6a59729b606261edd5e25ff285d2b4ef9cf..76ae26163f56ee53bcab90b970e412257aa4add2 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
+#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).
index 3f35d31b6e382e3d9583001a7718daeeb122ae15..d2c543ef475332cf23fe0992df55a1292a8f2932 100644 (file)
@@ -12,6 +12,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include "sig.h"
 
 /* Linux signals:
  *
index 712743aadd2095573c7c195746d9a141acc2a41a..c267200ed7b6c5d8d1c80ee353321412547d2a25 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "proc/procps.h"
 #include "proc/readproc.h"
+#include "status.h"
 
 char * status(proc_t* task) {
     static char buf[4] = "   ";
index 538fe55695ceba5afeab847c1d1668edfd600a8c..6410bf0e278e11ab2ea13adfdb57c7daec6d204b 100644 (file)
@@ -9,6 +9,7 @@
 #endif
 #include <stdio.h>
 #include <stdlib.h>
+#include "version.h"
 
 #ifdef MINORVERSION
 char procps_version[] = "procps version " VERSION "." SUBVERSION "." MINORVERSION;
diff --git a/top.c b/top.c
index 4ef740db1fbaf2863db9ea1d1da6ea0c166470d5..a6a7279f7df19f5b743a1aed3217fbc17661473c 100644 (file)
--- 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  */
         /* ////////////////////////////////////////////////////////////// */
 
 \f
@@ -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));
 }
 
 \f
@@ -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 f87024a61023537064d66c3e1825fee3b3e45ef2..f0759ab9b1032cd5ecde596ea4ee9c220c303e15 100644 (file)
--- 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);