From owner-svn-src-all@FreeBSD.ORG Mon Jul 18 21:15:48 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 2E45E106566C; Mon, 18 Jul 2011 21:15:48 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 140A38FC15; Mon, 18 Jul 2011 21:15:48 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id p6ILFlxf039708; Mon, 18 Jul 2011 21:15:47 GMT (envelope-from jhb@svn.freebsd.org) Received: (from jhb@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p6ILFlGL039704; Mon, 18 Jul 2011 21:15:47 GMT (envelope-from jhb@svn.freebsd.org) Message-Id: <201107182115.p6ILFlGL039704@svn.freebsd.org> From: John Baldwin Date: Mon, 18 Jul 2011 21:15:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r224205 - in head: contrib/top usr.bin/top X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Jul 2011 21:15:48 -0000 Author: jhb Date: Mon Jul 18 21:15:47 2011 New Revision: 224205 URL: http://svn.freebsd.org/changeset/base/224205 Log: 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 Modified: head/contrib/top/display.c head/contrib/top/top.c head/usr.bin/top/machine.c Modified: head/contrib/top/display.c ============================================================================== --- head/contrib/top/display.c Mon Jul 18 21:08:14 2011 (r224204) +++ head/contrib/top/display.c Mon Jul 18 21:15:47 2011 (r224205) @@ -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; Modified: head/contrib/top/top.c ============================================================================== --- head/contrib/top/top.c Mon Jul 18 21:08:14 2011 (r224204) +++ head/contrib/top/top.c Mon Jul 18 21:15:47 2011 (r224205) @@ -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'); Modified: head/usr.bin/top/machine.c ============================================================================== --- head/usr.bin/top/machine.c Mon Jul 18 21:08:14 2011 (r224204) +++ head/usr.bin/top/machine.c Mon Jul 18 21:15:47 2011 (r224205) @@ -241,7 +241,7 @@ static void getsysctl(const char *name, static int swapmode(int *retavail, int *retfree); void -toggle_pcpustats(struct statics *statics) +toggle_pcpustats(void) { if (ncpus == 1) @@ -256,7 +256,6 @@ toggle_pcpustats(struct statics *statics y_header += ncpus - 1; /* 6 */ y_procs += ncpus - 1; /* 7 */ Header_lines += ncpus - 1; /* 7 */ - statics->ncpus = ncpus; } else { y_mem = 3; y_swap = 4; @@ -265,7 +264,6 @@ toggle_pcpustats(struct statics *statics y_header = 6; y_procs = 7; Header_lines = 7; - statics->ncpus = 1; } } @@ -356,10 +354,10 @@ machine_init(struct statics *statics, ch pcpu_cp_old = calloc(1, size); pcpu_cp_diff = calloc(1, size); pcpu_cpu_states = calloc(1, size); - statics->ncpus = 1; + statics->ncpus = ncpus; if (pcpu_stats) - toggle_pcpustats(statics); + toggle_pcpustats(); /* all done! */ return (0);