TOP wasn't reporting correctly CPU states without -M
authorJavier Alcázar <javucho1000@yahoo.com.mx>
Thu, 3 Jan 2013 21:36:32 +0000 (06:36 +0900)
committerFrançois Tigeot <ftigeot@wolfpond.org>
Wed, 23 Jan 2013 11:49:16 +0000 (12:49 +0100)
Adding code to get averages and updating man page.

This commit fixes issue #2447

usr.bin/top/m_dragonfly.c
usr.bin/top/top.1

index 462894d..ec2652e 100644 (file)
@@ -125,6 +125,7 @@ char *procstatenames[] = {
 /* these are for detailing the cpu states */
 #define CPU_STATES 5
 int *cpu_states;
+int* cpu_averages;
 char *cpustatenames[CPU_STATES + 1] = {
        "user", "nice", "system", "interrupt", "idle", NULL
 };
@@ -344,10 +345,29 @@ get_system_info(struct system_info *si)
        lastpid = 0;
 
        /* convert cp_time counts to percentages */
+       int combine_cpus = (enable_ncpus == 0 && n_cpus > 1);
        for (cpu = 0; cpu < n_cpus; ++cpu) {
                cputime_percentages(cpu_states + cpu * CPU_STATES,
                    &cp_time[cpu], &cp_old[cpu]);
        }
+       if (combine_cpus) {
+               if (cpu_averages == NULL) {
+                       cpu_averages = malloc(sizeof(*cpu_averages) * CPU_STATES);
+                       if (cpu_averages == NULL)
+                               err(1, "cpu_averages");
+               }
+               bzero(cpu_averages, sizeof(*cpu_averages) * CPU_STATES);
+               for (cpu = 0; cpu < n_cpus; ++cpu) {
+                       int j = 0;
+                       cpu_averages[0] += *(cpu_states + ((cpu * CPU_STATES) + j++) );
+                       cpu_averages[1] += *(cpu_states + ((cpu * CPU_STATES) + j++) );
+                       cpu_averages[2] += *(cpu_states + ((cpu * CPU_STATES) + j++) );
+                       cpu_averages[3] += *(cpu_states + ((cpu * CPU_STATES) + j++) );
+                       cpu_averages[4] += *(cpu_states + ((cpu * CPU_STATES) + j++) );
+               }
+               for (int i = 0; i < CPU_STATES; ++i)
+                       cpu_averages[i] /= n_cpus;
+       }
 
        /* sum memory & swap statistics */
        {
@@ -404,7 +424,8 @@ get_system_info(struct system_info *si)
        }
 
        /* set arrays and strings */
-       si->cpustates = cpu_states;
+       si->cpustates = combine_cpus == 1 ?
+           cpu_averages : cpu_states;
        si->memory = memory_stats;
        si->swap = swap_stats;
 
index ce5c1e6..b814ba1 100644 (file)
@@ -330,10 +330,10 @@ The second line displays the total number of processes followed
 by a breakdown of processes per state.  Examples of states common
 to Unix systems are sleeping, running, starting, stopped, and zombie.
 The next line displays a percentage of time spent in each of the
-processor states (typically user, nice, system, idle, and iowait).
+processor states (user, nice, system, interrupt, idle).
 These percentages show the processor activity during the time since
 the last update.  For multi-processor systems, this information is 
-a summation of time across all processors.  The next line shows
+an average of all processors.  The next line shows
 kernel-related activity (not available on all systems).  The numbers
 shown on this line are per-second rates sampled since the last update.
 The exact