Simplify process cpu% and vsz% calculations.

Remove the code that avoided integer divides when generating the cpu%.
The loop will be dominated by the system calls to read the command line.
So even if the division is a code loop it is probably noise.

On Linux the process cpu time values are in 'jiffies' so there is
no need to re-scale the values based on the sum of the process times.
(This was already assumed because of issues with short lived processes.)

Signed-off-by: David Laight <[email protected]>

---


--- a/top.c     2021-11-11 09:49:08.165982660 +0000
+++ b/top.c     2021-11-12 11:25:43.849264597 +0000
@@ -139,7 +139,6 @@
        unsigned long long usr, nic, sys, idle;
        unsigned long long iowait, irq, softirq, steal;
        unsigned long long total;
-       unsigned long long busy;
 } jiffy_counts_t;
 
 /* This structure stores some critical information from one frame to
@@ -292,8 +291,6 @@
        if (ret >= 4) {
                p_jif->total = p_jif->usr + p_jif->nic + p_jif->sys + 
p_jif->idle
                        + p_jif->iowait + p_jif->irq + p_jif->softirq + 
p_jif->steal;
-               /* procps 2.x does not count iowait as busy time */
-               p_jif->busy = p_jif->total - p_jif->idle - p_jif->iowait;
        }
 
        return ret;
@@ -604,19 +601,10 @@
 
 static NOINLINE void display_process_list(int lines_rem, int scr_width)
 {
-       enum {
-               BITS_PER_INT = sizeof(int) * 8
-       };
-
        top_status_t *s;
        unsigned long total_memory = display_header(scr_width, &lines_rem); /* 
or use total_vsz? */
-       /* xxx_shift and xxx_scale variables allow us to replace
-        * expensive divides with multiply and shift */
-       unsigned pmem_shift, pmem_scale, pmem_half;
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-       unsigned tmp_unsigned;
-       unsigned pcpu_shift, pcpu_scale, pcpu_half;
-       unsigned busy_jifs;
+       unsigned delta_jifs;
 #endif
 
        /* what info of the processes is shown */
@@ -638,49 +626,10 @@
 # define SHOW_STAT(name) name
 # define FMT "%4u%%"
 #endif
-       /*
-        * %VSZ = s->vsz/MemTotal
-        */
-       pmem_shift = BITS_PER_INT-11;
-       pmem_scale = UPSCALE*(1U<<(BITS_PER_INT-11)) / total_memory;
-       /* s->vsz is in kb. we want (s->vsz * pmem_scale) to never overflow */
-       while (pmem_scale >= 512) {
-               pmem_scale /= 4;
-               pmem_shift -= 2;
-       }
-       pmem_half = (1U << pmem_shift) / (ENABLE_FEATURE_TOP_DECIMALS ? 20 : 2);
-#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-       busy_jifs = cur_jif.busy - prev_jif.busy;
-       /* This happens if there were lots of short-lived processes
-        * between two top updates (e.g. compilation) */
-       if (total_pcpu < busy_jifs) total_pcpu = busy_jifs;
 
-       /*
-        * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
-        * (pcpu is delta of sys+user time between samples)
-        */
-       /* (cur_jif.xxx - prev_jif.xxx) and s->pcpu are
-        * in 0..~64000 range (HZ*update_interval).
-        * we assume that unsigned is at least 32-bit.
-        */
-       pcpu_shift = 6;
-       pcpu_scale = UPSCALE*64 * (uint16_t)busy_jifs;
-       if (pcpu_scale == 0)
-               pcpu_scale = 1;
-       while (pcpu_scale < (1U << (BITS_PER_INT-2))) {
-               pcpu_scale *= 4;
-               pcpu_shift += 2;
-       }
-       tmp_unsigned = (uint16_t)(cur_jif.total - prev_jif.total) * total_pcpu;
-       if (tmp_unsigned != 0)
-               pcpu_scale /= tmp_unsigned;
-       /* we want (s->pcpu * pcpu_scale) to never overflow */
-       while (pcpu_scale >= 1024) {
-               pcpu_scale /= 4;
-               pcpu_shift -= 2;
-       }
-       pcpu_half = (1U << pcpu_shift) / (ENABLE_FEATURE_TOP_DECIMALS ? 20 : 2);
-       /* printf(" pmem_scale=%u pcpu_scale=%u ", pmem_scale, pcpu_scale); */
+#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
+       /* s->pcpu is in jiffies */
+       delta_jifs = cur_jif.total - prev_jif.total;
 #endif
 
        /* Ok, all preliminary data is ready, go through the list */
@@ -692,9 +641,9 @@
                char vsz_str_buf[8];
                unsigned col;
 
-               CALC_STAT(pmem, (s->vsz*pmem_scale + pmem_half) >> pmem_shift);
+               CALC_STAT(pmem, (s->vsz * UPSCALE + total_memory/2) / 
total_memory);
 #if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE
-               CALC_STAT(pcpu, (s->pcpu*pcpu_scale + pcpu_half) >> pcpu_shift);
+               CALC_STAT(pcpu, (s->pcpu * UPSCALE + delta_jifs/2) / 
delta_jifs);
 #endif
 
                smart_ulltoa5(s->vsz, vsz_str_buf, " mgtpezy");

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, 
UK
Registration No: 1397386 (Wales)

_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to