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@
This commit is contained in:
he 2010-04-05 07:16:12 +00:00
parent 92bfeebb2c
commit bb89b7208d
4 changed files with 16 additions and 9 deletions

View File

@ -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 @@ kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
&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 @@ kern_free(void *addr, struct malloc_type *ksp)
#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 @@ kern_free(void *addr, struct malloc_type *ksp)
}
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);

View File

@ -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 @@ struct malloc_type {
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

View File

@ -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__)

View File

@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1986, 1991, 1993\
#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 @@ domem(void)
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 @@ domem(void)
else
(void)printf(",%d", j);
first = 0;
(void)printf(":%u", ks.ks_active[i]);
}
(void)printf("\n");
totuse += ks.ks_memuse;