xref: /original-bsd/usr.bin/vmstat.sparc/mem.c (revision 37071c60)
1 /*
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)mem.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/malloc.h>
14 
15 #include <errno.h>
16 #include <kvm.h>
17 #include <nlist.h>
18 #include <stdio.h>
19 
20 #include "extern.h"
21 
22 static struct nlist nl[] = {
23 #define	X_KMEMSTATS	0
24 	{ "_kmemstats" },
25 #define	X_BUCKET	1
26 	{ "_bucket" },
27 	0
28 };
29 /*
30  * These names are defined in <sys/malloc.h>.
31  */
32 char *kmemnames[] = INITKMEMNAMES;
33 
34 void
35 domem()
36 {
37 	register struct kmembuckets *kp;
38 	register struct kmemstats *ks;
39 	register int i;
40 	int size;
41 	long totuse = 0, totfree = 0, totreq = 0;
42 	struct kmemstats kmemstats[M_LAST];
43 	struct kmembuckets buckets[MINBUCKET + 16];
44 
45 	knlist(nl);
46 	kread(nl[X_BUCKET].n_value, &buckets, sizeof buckets, "kmembucket");
47 	(void)printf("Memory statistics by bucket size\n");
48 	(void)printf(
49 	    "    Size   In Use   Free   Requests  HighWater  Couldfree\n");
50 	for (i = MINBUCKET, kp = &buckets[i]; i < MINBUCKET + 16; i++, kp++) {
51 		if (kp->kb_calls == 0)
52 			continue;
53 		size = 1 << i;
54 		(void)printf("%8d %8ld %6ld %10ld %7ld %10ld\n", size,
55 			kp->kb_total - kp->kb_totalfree,
56 			kp->kb_totalfree, kp->kb_calls,
57 			kp->kb_highwat, kp->kb_couldfree);
58 		totfree += size * kp->kb_totalfree;
59 	}
60 
61 	kread(nl[X_KMEMSTATS].n_value, &kmemstats, sizeof kmemstats,
62 	    "kmemstats");
63 	(void)printf("\nMemory statistics by type\n");
64 	(void)printf(
65 "       Type  In Use  MemUse   HighUse  Limit Requests  TypeLimit KernLimit\n");
66 	for (i = 0, ks = &kmemstats[0]; i < M_LAST; i++, ks++) {
67 		if (ks->ks_calls == 0)
68 			continue;
69 		(void)printf("%11s %6ld %7ldK %8ldK %5ldK %8ld %6u %9u\n",
70 		    kmemnames[i] ? kmemnames[i] : "undefined",
71 		    ks->ks_inuse, (ks->ks_memuse + 1023) / 1024,
72 		    (ks->ks_maxused + 1023) / 1024,
73 		    (ks->ks_limit + 1023) / 1024, ks->ks_calls,
74 		    ks->ks_limblocks, ks->ks_mapblocks);
75 		totuse += ks->ks_memuse;
76 		totreq += ks->ks_calls;
77 	}
78 	(void)printf("\nMemory Totals:  In Use    Free    Requests\n");
79 	(void)printf("              %7ldK %6ldK    %8ld\n",
80 	     (totuse + 1023) / 1024, (totfree + 1023) / 1024, totreq);
81 }
82