diff options
author | jhb <jhb@FreeBSD.org> | 2011-07-18 21:15:47 +0000 |
---|---|---|
committer | jhb <jhb@FreeBSD.org> | 2011-07-18 21:15:47 +0000 |
commit | f3caef077b39887e5b737c600ec46379c99b7dc0 (patch) | |
tree | 51cdc1a8e25efa9aaf2fc32d7b692e155c57606c /contrib/top | |
parent | b1d0d78c9992104334a7be8a336e5ee2460b18e5 (diff) | |
download | FreeBSD-src-f3caef077b39887e5b737c600ec46379c99b7dc0.zip FreeBSD-src-f3caef077b39887e5b737c600ec46379c99b7dc0.tar.gz |
Rework the dynamic per-CPU stats code a bit. Always set 'statics->ncpus'
to the maximum number of CPUs to ensure that lcpustates[] array is always
allocated to the maximum size. Previously, if top was started without
per-CPU stats it would allocate a smaller lcpustates[] array. When
per-CPU stats were then enabled, it would overflow the array and trash
the cpustates_columns[] array causing the CPU stats to be printed in the
wrong locations.
Approved by: re (kib)
MFC after: 1 week
Diffstat (limited to 'contrib/top')
-rw-r--r-- | contrib/top/display.c | 16 | ||||
-rw-r--r-- | contrib/top/top.c | 2 |
2 files changed, 15 insertions, 3 deletions
diff --git a/contrib/top/display.c b/contrib/top/display.c index a5a4e9e..89795c0 100644 --- a/contrib/top/display.c +++ b/contrib/top/display.c @@ -156,18 +156,30 @@ int display_updatecpus(statics) struct statics *statics; { + register int *lp; register int lines; register int i; /* call resize to do the dirty work */ lines = display_resize(); - num_cpus = statics->ncpus; + if (pcpu_stats) + num_cpus = statics->ncpus; + else + num_cpus = 1; cpustates_column = 5; /* CPU: */ if (num_cpus != 1) cpustates_column += 2; /* CPU 0: */ for (i = num_cpus; i > 9; i /= 10) cpustates_column++; + /* fill the "last" array with all -1s, to insure correct updating */ + lp = lcpustates; + i = num_cpustates * num_cpus; + while (--i >= 0) + { + *lp++ = -1; + } + return(lines); } @@ -197,7 +209,7 @@ struct statics *statics; num_swap = string_count(swap_names); lswap = (int *)malloc(num_swap * sizeof(int)); num_cpustates = string_count(cpustate_names); - lcpustates = (int *)malloc(num_cpustates * sizeof(int) * num_cpus); + lcpustates = (int *)malloc(num_cpustates * sizeof(int) * statics->ncpus); cpustate_columns = (int *)malloc(num_cpustates * sizeof(int)); memory_names = statics->memory_names; diff --git a/contrib/top/top.c b/contrib/top/top.c index 1a017a3..e7e27d5 100644 --- a/contrib/top/top.c +++ b/contrib/top/top.c @@ -1094,7 +1094,7 @@ restart: new_message(MT_standout | MT_delayed, " Displaying %sCPU statistics.", pcpu_stats ? "per-" : "global "); - toggle_pcpustats(&statics); + toggle_pcpustats(); max_topn = display_updatecpus(&statics); reset_display(); putchar('\r'); |