Subject: pkg/6979: Top gives incorrect CPU percentage when HZ!=100
To: None <gnats-bugs@gnats.netbsd.org>
From: Richard Earnshaw <rearnsha@cambridge.arm.com>
List: netbsd-bugs
Date: 02/10/1999 15:09:54
>Number:         6979
>Category:       pkg
>Synopsis:       Top gives incorrect CPU percentage when HZ!=100
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    gnats-admin (GNATS administrator)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Feb 10 07:20:01 1999
>Last-Modified:
>Originator:     Richard Earnshaw
>Organization:
ARM Ltd
-- 
>Release:        NetBSD-current 1999-02-10<NetBSD-current source date>
>Environment:
System: NetBSD shark1 1.3I NetBSD 1.3I (SHARK) #11: Mon Feb 8 10:11:59 GMT 1999 rearnsha@shark1:/work/rearnsha/netbsd/sys/arch/arm32/compile/SHARK arm32


>Description:
	Sharks running arm/32 define HZ=64 in the config file.  Top uses this 
	value to scale the p_pctcpu value returned from the kernel.  This 
	scaling is incorrect, since it has already been done by the kernel
	(this is a cpu percentage figure, not a number of ticks).  The result
	is that percentage cpu utilization can exceed 100% by a large margin.

	The relevent bit of the kernel is kern/kern_synch.c near line 205:

	#if     (FSHIFT >= CCPU_SHIFT)
        	        p->p_pctcpu += (hz == 100)?
                	        ((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
                        	100 * (((fixpt_t) p->p_cpticks)
                                	<< (FSHIFT - CCPU_SHIFT)) / hz;
	#else
        	        p->p_pctcpu += ((FSCALE - ccpu) *
                	        (p->p_cpticks * FSCALE / hz)) >> FSHIFT;
	#endif

	Where it can be seen that the setting of hz has already been taken
	into account.


>How-To-Repeat:
	Build and run top on a machine with HZ!=100

>Fix:
	Multiply the weighted and unweighted cpu figures by a factor of
	100 less, and don't divide by HZ.  Patch below.  I haven't looked
	closely, but the kernel setting of HZ may no-longer be needed by
	top.

--- /work/rearnsha/pkgsrc/sysutils/top/work/top-3.5beta7/machine/m_netbsd13.c	Wed Feb 10 09:43:46 1999
+++ ./m_netbsd13.c	Wed Feb 10 14:12:56 1999
@@ -560,8 +560,8 @@ format_next_process(handle, get_userid)
 	    format_k(pagetok(VP(pp, vm_rssize))),
 	    state_abbrev[(unsigned char) PP(pp, p_stat)],
 	    format_time(cputime),
-	    10000.0 * weighted_cpu(pct, pp) / hz,
-	    10000.0 * pct / hz,
+	    100.0 * weighted_cpu(pct, pp),
+	    100.0 * pct,
 	    printable(PP(pp, p_comm)));
 
     /* return the result */



>Audit-Trail:
>Unformatted: