Module Name:    src
Committed By:   he
Date:           Mon Apr  5 07:16:13 UTC 2010

Modified Files:
        src/sys/kern: kern_malloc.c
        src/sys/sys: mallocvar.h param.h
        src/usr.bin/vmstat: vmstat.c

Log Message:
Extend struct malloc_type to count the number of active allocations
per size, and make vmstat report this information under the "Memory
statistics by type" display, which is only printed when the kernel
has been compiled with KMEMSTATS defined, like this:

Memory statistics by type                                Type  Kern
           Type InUse  MemUse HighUse   Limit   Requests Limit Limit Size(s)
          wapbl    15   4192K   4192K  78644K     376426     0     0 
32:0,256:3,512:6,131072:1,262144:2,524288:3

Since struct malloc_type is user-visible and is changed, bump kernel
revision to 5.99.26.

While it is true that malloc(9) is in general on the path of slowly
being replaced by kmem(9) (kmem_alloc/kmem_free), there remains a
lot of points of usage of malloc/free, and this could aid in finding
any leaks.  (It helped finding the leak fixed in PR#42661.)

This was discussed with and somewhat hestitantly OKed by rmind@


To generate a diff of this commit:
cvs rdiff -u -r1.128 -r1.129 src/sys/kern/kern_malloc.c
cvs rdiff -u -r1.7 -r1.8 src/sys/sys/mallocvar.h
cvs rdiff -u -r1.360 -r1.361 src/sys/sys/param.h
cvs rdiff -u -r1.166 -r1.167 src/usr.bin/vmstat/vmstat.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_malloc.c
diff -u src/sys/kern/kern_malloc.c:1.128 src/sys/kern/kern_malloc.c:1.129
--- src/sys/kern/kern_malloc.c:1.128	Fri Jan 22 08:32:05 2010
+++ src/sys/kern/kern_malloc.c	Mon Apr  5 07:16:13 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_malloc.c,v 1.128 2010/01/22 08:32:05 hubertf Exp $	*/
+/*	$NetBSD: kern_malloc.c,v 1.129 2010/04/05 07:16:13 he Exp $	*/
 
 /*
  * Copyright (c) 1987, 1991, 1993
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.128 2010/01/22 08:32:05 hubertf Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.129 2010/04/05 07:16:13 he Exp $");
 
 #include <sys/param.h>
 #include <sys/proc.h>
@@ -371,6 +371,7 @@
 			&malloc_lock);
 	}
 	ksp->ks_size |= 1 << indx;
+	ksp->ks_active[indx]++;
 #endif
 #ifdef DIAGNOSTIC
 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
@@ -604,6 +605,7 @@
 #ifdef KMEMSTATS
 		size = kup->ku_pagecnt << PGSHIFT;
 		ksp->ks_memuse -= size;
+		ksp->ks_active[kup->ku_indx]--;
 		kup->ku_indx = 0;
 		kup->ku_pagecnt = 0;
 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
@@ -660,6 +662,7 @@
 	}
 	kbp->kb_totalfree++;
 	ksp->ks_memuse -= size;
+	ksp->ks_active[kup->ku_indx]--;
 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
 	    ksp->ks_memuse < ksp->ks_limit)
 		wakeup((void *)ksp);

Index: src/sys/sys/mallocvar.h
diff -u src/sys/sys/mallocvar.h:1.7 src/sys/sys/mallocvar.h:1.8
--- src/sys/sys/mallocvar.h:1.7	Wed Nov  7 16:12:25 2007
+++ src/sys/sys/mallocvar.h	Mon Apr  5 07:16:12 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: mallocvar.h,v 1.7 2007/11/07 16:12:25 matt Exp $	*/
+/*	$NetBSD: mallocvar.h,v 1.8 2010/04/05 07:16:12 he Exp $	*/
 
 /*
  * Copyright (c) 1987, 1993
@@ -56,7 +56,7 @@
 	u_long	ks_maxused;	/* maximum number ever used */
 	u_long	ks_limit;	/* most that are allowed to exist */
 	u_long	ks_size;	/* sizes of this thing that are allocated */
-	u_long	ks_spare;
+	u_int	ks_active[32];	/* number of active allocations per size */
 };
 
 #ifdef _KERNEL

Index: src/sys/sys/param.h
diff -u src/sys/sys/param.h:1.360 src/sys/sys/param.h:1.361
--- src/sys/sys/param.h:1.360	Mon Mar 29 13:41:06 2010
+++ src/sys/sys/param.h	Mon Apr  5 07:16:12 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: param.h,v 1.360 2010/03/29 13:41:06 pooka Exp $	*/
+/*	$NetBSD: param.h,v 1.361 2010/04/05 07:16:12 he Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -63,7 +63,7 @@
  *	2.99.9		(299000900)
  */
 
-#define	__NetBSD_Version__	599002500	/* NetBSD 5.99.25 */
+#define	__NetBSD_Version__	599002600	/* NetBSD 5.99.26 */
 
 #define __NetBSD_Prereq__(M,m,p) (((((M) * 100000000) + \
     (m) * 1000000) + (p) * 100) <= __NetBSD_Version__)

Index: src/usr.bin/vmstat/vmstat.c
diff -u src/usr.bin/vmstat/vmstat.c:1.166 src/usr.bin/vmstat/vmstat.c:1.167
--- src/usr.bin/vmstat/vmstat.c:1.166	Wed Oct 21 21:12:07 2009
+++ src/usr.bin/vmstat/vmstat.c	Mon Apr  5 07:16:13 2010
@@ -1,4 +1,4 @@
-/* $NetBSD: vmstat.c,v 1.166 2009/10/21 21:12:07 rmind Exp $ */
+/* $NetBSD: vmstat.c,v 1.167 2010/04/05 07:16:13 he Exp $ */
 
 /*-
  * Copyright (c) 1998, 2000, 2001, 2007 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
 #if 0
 static char sccsid[] = "@(#)vmstat.c	8.2 (Berkeley) 3/1/95";
 #else
-__RCSID("$NetBSD: vmstat.c,v 1.166 2009/10/21 21:12:07 rmind Exp $");
+__RCSID("$NetBSD: vmstat.c,v 1.167 2010/04/05 07:16:13 he Exp $");
 #endif
 #endif /* not lint */
 
@@ -1172,7 +1172,10 @@
 		    howmany(ks.ks_limit, KILO), ks.ks_calls,
 		    ks.ks_limblocks, ks.ks_mapblocks);
 		first = 1;
-		for (j =  1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
+		for (j = 1 << MINBUCKET, i = MINBUCKET;
+		     j < 1 << (MINBUCKET + 16);
+		     j <<= 1, i++)
+		{
 			if ((ks.ks_size & j) == 0)
 				continue;
 			if (first)
@@ -1180,6 +1183,7 @@
 			else
 				(void)printf(",%d", j);
 			first = 0;
+			(void)printf(":%u", ks.ks_active[i]);
 		}
 		(void)printf("\n");
 		totuse += ks.ks_memuse;

Reply via email to