From 05050f0aa66090417c36a3b9f7f8f59f6746c938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Alc=C3=A1zar?= Date: Fri, 4 Jan 2013 06:36:32 +0900 Subject: [PATCH] refs #2447 TOP isn't reporting correctly CPU states without -M Adding code to get averages and updating man page. --- usr.bin/top/m_dragonfly.c | 23 ++++++++++++++++++++++- usr.bin/top/top.1 | 4 ++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/usr.bin/top/m_dragonfly.c b/usr.bin/top/m_dragonfly.c index 462894d..f649ca5 100644 --- a/usr.bin/top/m_dragonfly.c +++ b/usr.bin/top/m_dragonfly.c @@ -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 = calloc(CPU_STATES, sizeof(*cpu_averages)); + if (cpu_averages == NULL) + err(1, "cpu_averages"); + } + memset(cpu_averages, 0, 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; diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1 index ce5c1e6..b814ba1 100644 --- a/usr.bin/top/top.1 +++ b/usr.bin/top/top.1 @@ -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 -- 1.7.12