[net.bugs.4bsd] negative kmem: sa.c and kern_acct.c

liberte@uiucdcs.Uiuc.ARPA (07/13/85)

Index: etc/sa.c /sys/sys/kern_acct.c 4.2BSD Fix

Description:
	Two separate bugs cause negative numbers to occasionally appear
	in the k*sec field of accounting reports. 

Repeat-By:
	I was not terribly successful at reproducing these negative 
	numbers since it requires exceedingly large cpu times and/or
	memory usage.  But our users would manage to do it periodically.
	With both fixes, I haven't seen any.

Description:
	One bug in sa.c causes overflow because calculations are done
	with float values instead of double values in three places.  The
	other bug in the kernal accounting, kern_acct.c, causes overflow
	because an int is used instead of unsigned int to hold cpu time.
	Making it unsigned doubles the maximum and keeps it positive,
	though overflow is still possible.

	Another bug makes `sa -u` give trash.

Fix:
	The following diffs are from old to new.

In /usr/src/etc/sa.c
636c636
< 			printf("%3d%6.1fcp %6dmem %6dio %.14s\n",
---
> 			printf("%4d %6dcp %6dmem %6dio %.14s\n",
645c645
< 		up->us_imem += x * y;
---
> 		up->us_imem += x * (double)y;
650,651c650,651
< 		tp->p.imem += x * y;
< 		timem += x * y;
---
> 		tp->p.imem += x * (double)y;
> 		timem += x * (double)y;


In /sys/sys/kern_acct.c
67c71
< 	register int i;
---
> 	register unsigned int i;				/* BUGFIX */
100c104,105	/* The following is someone else's bug fix */
< 	if (i = u.u_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec)
---
> 	if (i = (u.u_ru.ru_utime.tv_sec + u.u_ru.ru_stime.tv_sec)*hz +		/* BUGFIX */
> 		(u.u_ru.ru_utime.tv_usec + u.u_ru.ru_stime.tv_usec)/tick
/* BUGFIX */

Dan LaLiberte
liberte@uiucdcs.Uiuc.ARPA
ihnp4!uiucdcs!liberte