From pjp@legolas.groupofnine.net  Tue Feb 27 21:06:36 2001
Return-Path: <pjp@legolas.groupofnine.net>
Received: from legolas.groupofnine.net (shortbus.jaded.net [216.94.132.8])
	by hub.freebsd.org (Postfix) with ESMTP id BD3C937B71A
	for <FreeBSD-gnats-submit@freebsd.org>; Tue, 27 Feb 2001 21:06:34 -0800 (PST)
	(envelope-from pjp@legolas.groupofnine.net)
Received: (from pjp@localhost)
	by legolas.groupofnine.net (8.11.2/8.11.1) id f1S56X199280;
	Wed, 28 Feb 2001 00:06:33 -0500 (EST)
	(envelope-from pjp)
Message-Id: <200102280506.f1S56X199280@legolas.groupofnine.net>
Date: Wed, 28 Feb 2001 00:06:33 -0500 (EST)
From: Peter Philipp <pjp@legolas.groupofnine.net>
Reply-To: pjp@daemonium.com
To: FreeBSD-gnats-submit@freebsd.org
Cc: code-warriors@groupofnine.net
Subject: /usr/src/usr.bin/vmstat/vmstat.c & /usr/src/sys/sys/malloc.h type differences 
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         25445
>Category:       kern
>Synopsis:       kernel statistics are displayed in wrong types and wrap into negative
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Feb 27 21:10:01 PST 2001
>Closed-Date:    Mon Apr 08 13:55:03 UTC 2013
>Last-Modified:  Mon Apr 15 21:53:47 UTC 2013
>Originator:     Peter Philipp
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
Daemonium
>Environment:
System: FreeBSD legolas.groupofnine.net 5.0-CURRENT FreeBSD 5.0-CURRENT #4: Sun Feb 25 21:48:35 EST 2001 pjp@legolas.groupofnine.net:/usr/obj/usr/src/sys/LEGOLAS i386

>Description:

	vmstat -m reports wrong kernel statistics due to a type mismatch
	in printing vmstat.c's totreq in domem().  Fix includes a bit
	more fixing of unsigned types.


>How-To-Repeat:

	let a busy web server run for a few days.

>Fix:
	This really has bigger problems that are much more complex (and 
	needs some smart advice) in regards to the type definitions of 
	the members in struct malloc_type and also kmembuckets 
	(sys/malloc.h).  I'm of the opinion for example that ks_calls in 
	malloc_type should be unsigned as it's a total count from 0 and 
	should never be negative.  

	I'm unsure of the some others as I've looked for any dependencies 
	in the /usr/src tree but it seems to be only vmstat for ks_inuse,
	ks_calls and ks_maxused.  I've changed those to unsigned and also
	declared the type of integer they are.  This could perhaps make
	the people with the alpha port happy.  Much of those structs remain
	to be just 'long' and should perhaps be changed to their respective
	posix integer type.

	/usr/src/sys/sys/malloc.h patch

--- malloc.h.orig	Tue Feb 27 22:49:28 2001
+++ malloc.h	Tue Feb 27 23:32:55 2001
@@ -55,9 +55,9 @@
 	long 	ks_memuse;	/* total memory held in bytes */
 	long	ks_limit;	/* most that are allowed to exist */
 	long	ks_size;	/* sizes of this thing that are allocated */
-	long	ks_inuse;	/* # of packets of this type currently in use */
-	int64_t	ks_calls;	/* total packets of this type ever allocated */
-	long	ks_maxused;	/* maximum number ever used */
+	u_int32_t ks_inuse;	/* # of packets of this type currently in use */
+	u_int64_t ks_calls;	/* total packets of this type ever allocated */
+	u_int32_t ks_maxused;	/* maximum number ever used */
 	u_long	ks_magic;	/* if it's not magic, don't touch it */
 	const char *ks_shortdesc;	/* short description */
 	u_short	ks_limblocks;	/* number of times blocked for hitting limit */
@@ -102,7 +102,7 @@
 struct kmembuckets {
 	caddr_t kb_next;	/* list of free blocks */
 	caddr_t kb_last;	/* last free block */
-	int64_t	kb_calls;	/* total calls to allocate this size */
+	u_int64_t kb_calls;	/* total calls to allocate this size */
 	long	kb_total;	/* total number of blocks allocated */
 	long	kb_elmpercl;	/* # of elements in this sized allocation */
 	long	kb_totalfree;	/* # of free elements in this bucket */


====
	/usr/src/usr.bin/vmstat/vmstat.c patch
	
--- vmstat.c.orig	Tue Feb 27 23:33:53 2001
+++ vmstat.c	Tue Feb 27 23:44:41 2001
@@ -761,7 +761,8 @@
 	register struct malloc_type *ks;
 	register int i, j;
 	int len, size, first, nkms;
-	long totuse = 0, totfree = 0, totreq = 0;
+	long totuse = 0, totfree = 0;
+	u_int64_t totreq = 0;
 	const char *name;
 	struct malloc_type kmemstats[MAX_KMSTATS], *kmsp;
 	char *kmemnames[MAX_KMSTATS];
@@ -841,7 +842,7 @@
 	for (i = 0, ks = &kmemstats[0]; i < nkms; i++, ks++) {
 		if (ks->ks_calls == 0)
 			continue;
-		(void)printf("%13s%6ld%6ldK%7ldK%6ldK%9ld%5u%6u",
+		(void)printf("%13s%6lu%6ldK%7luK%6ldK%9qu%5u%6u",
 		    ks->ks_shortdesc,
 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
 		    (ks->ks_maxused + 1023) / 1024,
@@ -866,7 +867,7 @@
 		totreq += ks->ks_calls;
 	}
 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
-	(void)printf("              %7ldK %6ldK    %8ld\n",
+	(void)printf("              %7ldK %6ldK    %8qu\n",
 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
 }

>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: Peter Philipp <pjp@dega.daemonium.com>
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: kern/25445: /usr/src/usr.bin/vmstat/vmstat.c & /usr/src/sys/sys/malloc.h
 type differences
Date: Thu, 1 Mar 2001 01:06:40 +1100 (EST)

 On Wed, 28 Feb 2001, Peter Philipp wrote:
 
 > Whoops I messed that up somewhat.  Let's try again this time I won't write
 > the patches because they are erroneous (and actually go back and break
 > alpha while leaving i386 ok, it's a tug-of-war see :P go intel).  
 
 I think you need to reply to freebsd-gnats-submit, not gnats-admin,
 so that the reply gets put in the database instead of in an overworked
 admin's mailbox.  I quoted it all so that it will be in the database
 via this reply.
 
 > 
 > I've also read up on the history why we are seeing this bug.  Revision 1.46 
 > in sys/malloc.h had a member type changed from long to int64_t.  As far as I 
 > know a long for the alpha is 64 bits anyhow but a long for i386 is 32 bits.  
 > So it didn't do anything for the alpha in fact but increased the size of 
 > ks_calls to 64 bits for the i386.  This created a type size problem for 
 > vmstat -m where the type for printing totreq was long which is 32 bits and 
 > not 64 bits.  It creates this in a vmstat -m:
 > 
 > Memory Totals:  In Use    Free    Requests
 >                 94474K   1759K    -2126966145
 > 
 > Perhaps the total requests should be printed as a quad_t which is independent
 > of platform and always 64 bits it seems.
 
 I think that is sufficient.  Someone should turn on -Wformat globally to
 inhibit new printf format errors.  I hope we fixed most of the old ones.
 
 > 
 > Anyhow this seems to open a few cans of worms (shudder) and also goes back to
 > size differences in structures such as struct malloc_type where on alpha
 > a set of stats collected as type long can hold a lot more than on i386 which
 > isn't fair and isn't consistent, which is unfortunately sucky.
 
 This is a fairly large problem.  `long' is the wrong type for all counters,
 but `u_long' is not much better (it has only twice the range).  At least
 using `long' and printing correctly it gives obviously bogus counts when
 counters overflows.  Fixed-size types are not the correct type for
 counters unless the range of the counter is known.  They may be too
 inefficient.  Atomic updates of 64-bit counters are especially inefficient
 on i386's.
 
 > 
 > If I'm totally wrong here, I'd love a good explaining, cuz I'll need it.
 > 
 > -- 
 > -                                                                              -
 > Peter Philipp <pjp@daemonium.com>
 > Daemonium
 

From: Peter Philipp <pjp@dega.daemonium.com>
To: freebsd-gnats-submit@FreeBSD.ORG
Cc:  
Subject: [bde@zeta.org.au: Re: kern/25445: /usr/src/usr.bin/vmstat/vmstat.c & /usr/src/sys/sys/malloc.h type differences
Date: Sun, 1 Apr 2001 03:00:57 -0500

 Ok well since this is still an open pr here is the patch to make usr.bin/vmstat
 compile cleanly against -Wformat (yes -Wformat should still be turned on 
 globally as Bruce Evans suggests) but would most likely need more fixing than 
 this particular PR calls for.  If someone who has an alpha can add temporarily 
 -Wformat into the usr.bin/vmstat/Makefile near the CFLAGS+= line and make if
 there is no errrors with the formatting then dandy commit/close PR.
 
 --- malloc.h.orig       Tue Feb 27 22:49:28 2001
 +++ malloc.h    Sun Apr  1 03:47:41 2001
 @@ -56,7 +56,7 @@
         long    ks_limit;       /* most that are allowed to exist */
         long    ks_size;        /* sizes of this thing that are allocated */
         long    ks_inuse;       /* # of packets of this type currently in use */
 -       int64_t ks_calls;       /* total packets of this type ever allocated */
 +       long    ks_calls;       /* total packets of this type ever allocated */
         long    ks_maxused;     /* maximum number ever used */
         u_long  ks_magic;       /* if it's not magic, don't touch it */
         const char *ks_shortdesc;       /* short description */
 @@ -102,7 +102,7 @@
  struct kmembuckets {
         caddr_t kb_next;        /* list of free blocks */
         caddr_t kb_last;        /* last free block */
 -       int64_t kb_calls;       /* total calls to allocate this size */
 +       long    kb_calls;       /* total calls to allocate this size */
         long    kb_total;       /* total number of blocks allocated */
         long    kb_elmpercl;    /* # of elements in this sized allocation */
         long    kb_totalfree;   /* # of free elements in this bucket */
 
 
 
 -- 
 -                                                                              -
 Peter Philipp <pjp@daemonium.com>
 Daemonium
 
 
State-Changed-From-To: open->closed 
State-Changed-By: emaste 
State-Changed-When: Mon Apr 8 13:53:06 UTC 2013 
State-Changed-Why:  
Fixed quite some time ago (some time in 2002). 
In any case this functionality has been rewritten since. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=25445 
>Unformatted:
