xref: /netbsd-src/sys/kern/kern_malloc.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: kern_malloc.c,v 1.52 2000/05/26 23:18:26 sommerfeld Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
5  * Copyright (c) 1987, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
37  */
38 
39 #include "opt_lockdebug.h"
40 
41 #include <sys/param.h>
42 #include <sys/proc.h>
43 #include <sys/map.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/systm.h>
47 
48 #include <vm/vm.h>
49 #include <vm/vm_kern.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 static struct vm_map_intrsafe kmem_map_store;
54 vm_map_t kmem_map = NULL;
55 
56 #include "opt_kmempages.h"
57 
58 #ifdef NKMEMCLUSTERS
59 #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
60 #endif
61 
62 /*
63  * Default number of pages in kmem_map.  We attempt to calculate this
64  * at run-time, but allow it to be either patched or set in the kernel
65  * config file.
66  */
67 #ifndef NKMEMPAGES
68 #define	NKMEMPAGES	0
69 #endif
70 int	nkmempages = NKMEMPAGES;
71 
72 /*
73  * Defaults for lower- and upper-bounds for the kmem_map page count.
74  * Can be overridden by kernel config options.
75  */
76 #ifndef	NKMEMPAGES_MIN
77 #define	NKMEMPAGES_MIN	NKMEMPAGES_MIN_DEFAULT
78 #endif
79 
80 #ifndef NKMEMPAGES_MAX
81 #define	NKMEMPAGES_MAX	NKMEMPAGES_MAX_DEFAULT
82 #endif
83 
84 #include "opt_kmemstats.h"
85 #include "opt_malloclog.h"
86 
87 struct kmembuckets bucket[MINBUCKET + 16];
88 struct kmemstats kmemstats[M_LAST];
89 struct kmemusage *kmemusage;
90 char *kmembase, *kmemlimit;
91 const char *memname[] = INITKMEMNAMES;
92 
93 #ifdef MALLOCLOG
94 #ifndef MALLOCLOGSIZE
95 #define	MALLOCLOGSIZE	100000
96 #endif
97 
98 struct malloclog {
99 	void *addr;
100 	long size;
101 	int type;
102 	int action;
103 	const char *file;
104 	long line;
105 } malloclog[MALLOCLOGSIZE];
106 
107 long	malloclogptr;
108 
109 static void domlog __P((void *a, long size, int type, int action,
110 	const char *file, long line));
111 static void hitmlog __P((void *a));
112 
113 static void
114 domlog(a, size, type, action, file, line)
115 	void *a;
116 	long size;
117 	int type;
118 	int action;
119 	const char *file;
120 	long line;
121 {
122 
123 	malloclog[malloclogptr].addr = a;
124 	malloclog[malloclogptr].size = size;
125 	malloclog[malloclogptr].type = type;
126 	malloclog[malloclogptr].action = action;
127 	malloclog[malloclogptr].file = file;
128 	malloclog[malloclogptr].line = line;
129 	malloclogptr++;
130 	if (malloclogptr >= MALLOCLOGSIZE)
131 		malloclogptr = 0;
132 }
133 
134 static void
135 hitmlog(a)
136 	void *a;
137 {
138 	struct malloclog *lp;
139 	long l;
140 
141 #define	PRT \
142 	if (malloclog[l].addr == a && malloclog[l].action) { \
143 		lp = &malloclog[l]; \
144 		printf("malloc log entry %ld:\n", l); \
145 		printf("\taddr = %p\n", lp->addr); \
146 		printf("\tsize = %ld\n", lp->size); \
147 		printf("\ttype = %s\n", memname[lp->type]); \
148 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
149 		printf("\tfile = %s\n", lp->file); \
150 		printf("\tline = %ld\n", lp->line); \
151 	}
152 
153 	for (l = malloclogptr; l < MALLOCLOGSIZE; l++)
154 		PRT
155 
156 	for (l = 0; l < malloclogptr; l++)
157 		PRT
158 }
159 #endif /* MALLOCLOG */
160 
161 #ifdef DIAGNOSTIC
162 /*
163  * This structure provides a set of masks to catch unaligned frees.
164  */
165 long addrmask[] = { 0,
166 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
167 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
168 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
169 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
170 };
171 
172 /*
173  * The WEIRD_ADDR is used as known text to copy into free objects so
174  * that modifications after frees can be detected.
175  */
176 #define WEIRD_ADDR	((unsigned) 0xdeadbeef)
177 #define MAX_COPY	32
178 
179 /*
180  * Normally the freelist structure is used only to hold the list pointer
181  * for free objects.  However, when running with diagnostics, the first
182  * 8 bytes of the structure is unused except for diagnostic information,
183  * and the free list pointer is at offst 8 in the structure.  Since the
184  * first 8 bytes is the portion of the structure most often modified, this
185  * helps to detect memory reuse problems and avoid free list corruption.
186  */
187 struct freelist {
188 	int32_t	spare0;
189 	int16_t	type;
190 	int16_t	spare1;
191 	caddr_t	next;
192 };
193 #else /* !DIAGNOSTIC */
194 struct freelist {
195 	caddr_t	next;
196 };
197 #endif /* DIAGNOSTIC */
198 
199 /*
200  * Allocate a block of memory
201  */
202 #ifdef MALLOCLOG
203 void *
204 _malloc(size, type, flags, file, line)
205 	unsigned long size;
206 	int type, flags;
207 	const char *file;
208 	long line;
209 #else
210 void *
211 malloc(size, type, flags)
212 	unsigned long size;
213 	int type, flags;
214 #endif /* MALLOCLOG */
215 {
216 	struct kmembuckets *kbp;
217 	struct kmemusage *kup;
218 	struct freelist *freep;
219 	long indx, npg, allocsize;
220 	int s;
221 	caddr_t va, cp, savedlist;
222 #ifdef DIAGNOSTIC
223 	int32_t *end, *lp;
224 	int copysize;
225 	const char *savedtype;
226 #endif
227 #ifdef KMEMSTATS
228 	struct kmemstats *ksp = &kmemstats[type];
229 
230 	if (__predict_false(((unsigned long)type) > M_LAST))
231 		panic("malloc - bogus type");
232 #endif
233 	indx = BUCKETINDX(size);
234 	kbp = &bucket[indx];
235 	s = splmem();
236 #ifdef KMEMSTATS
237 	while (ksp->ks_memuse >= ksp->ks_limit) {
238 		if (flags & M_NOWAIT) {
239 			splx(s);
240 			return ((void *) NULL);
241 		}
242 		if (ksp->ks_limblocks < 65535)
243 			ksp->ks_limblocks++;
244 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
245 	}
246 	ksp->ks_size |= 1 << indx;
247 #endif
248 #ifdef DIAGNOSTIC
249 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
250 #endif
251 	if (kbp->kb_next == NULL) {
252 		kbp->kb_last = NULL;
253 		if (size > MAXALLOCSAVE)
254 			allocsize = roundup(size, PAGE_SIZE);
255 		else
256 			allocsize = 1 << indx;
257 		npg = btoc(allocsize);
258 		va = (caddr_t) uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
259 				(vsize_t)ctob(npg),
260 				(flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0);
261 		if (__predict_false(va == NULL)) {
262 			/*
263 			 * Kmem_malloc() can return NULL, even if it can
264 			 * wait, if there is no map space avaiable, because
265 			 * it can't fix that problem.  Neither can we,
266 			 * right now.  (We should release pages which
267 			 * are completely free and which are in buckets
268 			 * with too many free elements.)
269 			 */
270 			if ((flags & M_NOWAIT) == 0)
271 				panic("malloc: out of space in kmem_map");
272 			splx(s);
273 			return ((void *) NULL);
274 		}
275 #ifdef KMEMSTATS
276 		kbp->kb_total += kbp->kb_elmpercl;
277 #endif
278 		kup = btokup(va);
279 		kup->ku_indx = indx;
280 		if (allocsize > MAXALLOCSAVE) {
281 			if (npg > 65535)
282 				panic("malloc: allocation too large");
283 			kup->ku_pagecnt = npg;
284 #ifdef KMEMSTATS
285 			ksp->ks_memuse += allocsize;
286 #endif
287 			goto out;
288 		}
289 #ifdef KMEMSTATS
290 		kup->ku_freecnt = kbp->kb_elmpercl;
291 		kbp->kb_totalfree += kbp->kb_elmpercl;
292 #endif
293 		/*
294 		 * Just in case we blocked while allocating memory,
295 		 * and someone else also allocated memory for this
296 		 * bucket, don't assume the list is still empty.
297 		 */
298 		savedlist = kbp->kb_next;
299 		kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
300 		for (;;) {
301 			freep = (struct freelist *)cp;
302 #ifdef DIAGNOSTIC
303 			/*
304 			 * Copy in known text to detect modification
305 			 * after freeing.
306 			 */
307 			end = (int32_t *)&cp[copysize];
308 			for (lp = (int32_t *)cp; lp < end; lp++)
309 				*lp = WEIRD_ADDR;
310 			freep->type = M_FREE;
311 #endif /* DIAGNOSTIC */
312 			if (cp <= va)
313 				break;
314 			cp -= allocsize;
315 			freep->next = cp;
316 		}
317 		freep->next = savedlist;
318 		if (kbp->kb_last == NULL)
319 			kbp->kb_last = (caddr_t)freep;
320 	}
321 	va = kbp->kb_next;
322 	kbp->kb_next = ((struct freelist *)va)->next;
323 #ifdef DIAGNOSTIC
324 	freep = (struct freelist *)va;
325 	savedtype = (unsigned)freep->type < M_LAST ?
326 		memname[freep->type] : "???";
327 	if (kbp->kb_next) {
328 		int rv;
329 		vaddr_t addr = (vaddr_t)kbp->kb_next;
330 
331 		vm_map_lock(kmem_map);
332 		rv = uvm_map_checkprot(kmem_map, addr,
333 				       addr + sizeof(struct freelist),
334 				       VM_PROT_WRITE);
335 		vm_map_unlock(kmem_map);
336 
337 		if (__predict_false(rv == 0)) {
338 			printf(
339 		    "%s %ld of object %p size %ld %s %s (invalid addr %p)\n",
340 			    "Data modified on freelist: word",
341 			    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
342 			    va, size, "previous type", savedtype, kbp->kb_next);
343 #ifdef MALLOCLOG
344 			hitmlog(va);
345 #endif
346 			kbp->kb_next = NULL;
347 		}
348 	}
349 
350 	/* Fill the fields that we've used with WEIRD_ADDR */
351 #if BYTE_ORDER == BIG_ENDIAN
352 	freep->type = WEIRD_ADDR >> 16;
353 #endif
354 #if BYTE_ORDER == LITTLE_ENDIAN
355 	freep->type = (short)WEIRD_ADDR;
356 #endif
357 	end = (int32_t *)&freep->next +
358 	    (sizeof(freep->next) / sizeof(int32_t));
359 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
360 		*lp = WEIRD_ADDR;
361 
362 	/* and check that the data hasn't been modified. */
363 	end = (int32_t *)&va[copysize];
364 	for (lp = (int32_t *)va; lp < end; lp++) {
365 		if (__predict_true(*lp == WEIRD_ADDR))
366 			continue;
367 		printf("%s %ld of object %p size %ld %s %s (0x%x != 0x%x)\n",
368 		    "Data modified on freelist: word",
369 		    (long)(lp - (int32_t *)va), va, size, "previous type",
370 		    savedtype, *lp, WEIRD_ADDR);
371 #ifdef MALLOCLOG
372 		hitmlog(va);
373 #endif
374 		break;
375 	}
376 
377 	freep->spare0 = 0;
378 #endif /* DIAGNOSTIC */
379 #ifdef KMEMSTATS
380 	kup = btokup(va);
381 	if (kup->ku_indx != indx)
382 		panic("malloc: wrong bucket");
383 	if (kup->ku_freecnt == 0)
384 		panic("malloc: lost data");
385 	kup->ku_freecnt--;
386 	kbp->kb_totalfree--;
387 	ksp->ks_memuse += 1 << indx;
388 out:
389 	kbp->kb_calls++;
390 	ksp->ks_inuse++;
391 	ksp->ks_calls++;
392 	if (ksp->ks_memuse > ksp->ks_maxused)
393 		ksp->ks_maxused = ksp->ks_memuse;
394 #else
395 out:
396 #endif
397 #ifdef MALLOCLOG
398 	domlog(va, size, type, 1, file, line);
399 #endif
400 	splx(s);
401 	return ((void *) va);
402 }
403 
404 /*
405  * Free a block of memory allocated by malloc.
406  */
407 #ifdef MALLOCLOG
408 void
409 _free(addr, type, file, line)
410 	void *addr;
411 	int type;
412 	const char *file;
413 	long line;
414 #else
415 void
416 free(addr, type)
417 	void *addr;
418 	int type;
419 #endif /* MALLOCLOG */
420 {
421 	struct kmembuckets *kbp;
422 	struct kmemusage *kup;
423 	struct freelist *freep;
424 	long size;
425 	int s;
426 #ifdef DIAGNOSTIC
427 	caddr_t cp;
428 	int32_t *end, *lp;
429 	long alloc, copysize;
430 #endif
431 #ifdef KMEMSTATS
432 	struct kmemstats *ksp = &kmemstats[type];
433 #endif
434 
435 #ifdef DIAGNOSTIC
436 	/*
437 	 * Ensure that we're free'ing something that we could
438 	 * have allocated in the first place.  That is, check
439 	 * to see that the address is within kmem_map.
440 	 */
441 	if (__predict_false((vaddr_t)addr < kmem_map->header.start ||
442 			    (vaddr_t)addr >= kmem_map->header.end))
443 		panic("free: addr %p not within kmem_map", addr);
444 #endif
445 
446 	kup = btokup(addr);
447 	size = 1 << kup->ku_indx;
448 	kbp = &bucket[kup->ku_indx];
449 	s = splmem();
450 #ifdef MALLOCLOG
451 	domlog(addr, 0, type, 2, file, line);
452 #endif
453 #ifdef DIAGNOSTIC
454 	/*
455 	 * Check for returns of data that do not point to the
456 	 * beginning of the allocation.
457 	 */
458 	if (size > PAGE_SIZE)
459 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
460 	else
461 		alloc = addrmask[kup->ku_indx];
462 	if (((u_long)addr & alloc) != 0)
463 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n",
464 			addr, size, memname[type], alloc);
465 #endif /* DIAGNOSTIC */
466 	if (size > MAXALLOCSAVE) {
467 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt));
468 #ifdef KMEMSTATS
469 		size = kup->ku_pagecnt << PGSHIFT;
470 		ksp->ks_memuse -= size;
471 		kup->ku_indx = 0;
472 		kup->ku_pagecnt = 0;
473 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
474 		    ksp->ks_memuse < ksp->ks_limit)
475 			wakeup((caddr_t)ksp);
476 		ksp->ks_inuse--;
477 		kbp->kb_total -= 1;
478 #endif
479 		splx(s);
480 		return;
481 	}
482 	freep = (struct freelist *)addr;
483 #ifdef DIAGNOSTIC
484 	/*
485 	 * Check for multiple frees. Use a quick check to see if
486 	 * it looks free before laboriously searching the freelist.
487 	 */
488 	if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
489 		for (cp = kbp->kb_next; cp;
490 		    cp = ((struct freelist *)cp)->next) {
491 			if (addr != cp)
492 				continue;
493 			printf("multiply freed item %p\n", addr);
494 #ifdef MALLOCLOG
495 			hitmlog(addr);
496 #endif
497 			panic("free: duplicated free");
498 		}
499 	}
500 #ifdef LOCKDEBUG
501 	/*
502 	 * Check if we're freeing a locked simple lock.
503 	 */
504 	simple_lock_freecheck(addr, (char *)addr + size);
505 #endif
506 	/*
507 	 * Copy in known text to detect modification after freeing
508 	 * and to make it look free. Also, save the type being freed
509 	 * so we can list likely culprit if modification is detected
510 	 * when the object is reallocated.
511 	 */
512 	copysize = size < MAX_COPY ? size : MAX_COPY;
513 	end = (int32_t *)&((caddr_t)addr)[copysize];
514 	for (lp = (int32_t *)addr; lp < end; lp++)
515 		*lp = WEIRD_ADDR;
516 	freep->type = type;
517 #endif /* DIAGNOSTIC */
518 #ifdef KMEMSTATS
519 	kup->ku_freecnt++;
520 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
521 		if (kup->ku_freecnt > kbp->kb_elmpercl)
522 			panic("free: multiple frees");
523 		else if (kbp->kb_totalfree > kbp->kb_highwat)
524 			kbp->kb_couldfree++;
525 	}
526 	kbp->kb_totalfree++;
527 	ksp->ks_memuse -= size;
528 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
529 	    ksp->ks_memuse < ksp->ks_limit)
530 		wakeup((caddr_t)ksp);
531 	ksp->ks_inuse--;
532 #endif
533 	if (kbp->kb_next == NULL)
534 		kbp->kb_next = addr;
535 	else
536 		((struct freelist *)kbp->kb_last)->next = addr;
537 	freep->next = NULL;
538 	kbp->kb_last = addr;
539 	splx(s);
540 }
541 
542 /*
543  * Change the size of a block of memory.
544  */
545 void *
546 realloc(curaddr, newsize, type, flags)
547 	void *curaddr;
548 	unsigned long newsize;
549 	int type, flags;
550 {
551 	struct kmemusage *kup;
552 	long cursize;
553 	void *newaddr;
554 #ifdef DIAGNOSTIC
555 	long alloc;
556 #endif
557 
558 	/*
559 	 * Realloc() with a NULL pointer is the same as malloc().
560 	 */
561 	if (curaddr == NULL)
562 		return (malloc(newsize, type, flags));
563 
564 	/*
565 	 * Realloc() with zero size is the same as free().
566 	 */
567 	if (newsize == 0) {
568 		free(curaddr, type);
569 		return (NULL);
570 	}
571 
572 	/*
573 	 * Find out how large the old allocation was (and do some
574 	 * sanity checking).
575 	 */
576 	kup = btokup(curaddr);
577 	cursize = 1 << kup->ku_indx;
578 
579 #ifdef DIAGNOSTIC
580 	/*
581 	 * Check for returns of data that do not point to the
582 	 * beginning of the allocation.
583 	 */
584 	if (cursize > PAGE_SIZE)
585 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
586 	else
587 		alloc = addrmask[kup->ku_indx];
588 	if (((u_long)curaddr & alloc) != 0)
589 		panic("realloc: unaligned addr %p, size %ld, type %s, mask %ld\n",
590 			curaddr, cursize, memname[type], alloc);
591 #endif /* DIAGNOSTIC */
592 
593 	if (cursize > MAXALLOCSAVE)
594 		cursize = ctob(kup->ku_pagecnt);
595 
596 	/*
597 	 * If we already actually have as much as they want, we're done.
598 	 */
599 	if (newsize <= cursize)
600 		return (curaddr);
601 
602 	/*
603 	 * Can't satisfy the allocation with the existing block.
604 	 * Allocate a new one and copy the data.
605 	 */
606 	newaddr = malloc(newsize, type, flags);
607 	if (__predict_false(newaddr == NULL)) {
608 		/*
609 		 * Malloc() failed, because flags included M_NOWAIT.
610 		 * Return NULL to indicate that failure.  The old
611 		 * pointer is still valid.
612 		 */
613 		return NULL;
614 	}
615 	memcpy(newaddr, curaddr, cursize);
616 
617 	/*
618 	 * We were successful: free the old allocation and return
619 	 * the new one.
620 	 */
621 	free(curaddr, type);
622 	return (newaddr);
623 }
624 
625 /*
626  * Compute the number of pages that kmem_map will map, that is,
627  * the size of the kernel malloc arena.
628  */
629 void
630 kmeminit_nkmempages()
631 {
632 	int npages;
633 
634 	if (nkmempages != 0) {
635 		/*
636 		 * It's already been set (by us being here before, or
637 		 * by patching or kernel config options), bail out now.
638 		 */
639 		return;
640 	}
641 
642 	/*
643 	 * We use the following (simple) formula:
644 	 *
645 	 *	- Starting point is physical memory / 4.
646 	 *
647 	 *	- Clamp it down to NKMEMPAGES_MAX.
648 	 *
649 	 *	- Round it up to NKMEMPAGES_MIN.
650 	 */
651 	npages = physmem / 4;
652 
653 	if (npages > NKMEMPAGES_MAX)
654 		npages = NKMEMPAGES_MAX;
655 
656 	if (npages < NKMEMPAGES_MIN)
657 		npages = NKMEMPAGES_MIN;
658 
659 	nkmempages = npages;
660 }
661 
662 /*
663  * Initialize the kernel memory allocator
664  */
665 void
666 kmeminit()
667 {
668 #ifdef KMEMSTATS
669 	long indx;
670 #endif
671 
672 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
673 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
674 #endif
675 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
676 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
677 #endif
678 #if	(MAXALLOCSAVE < NBPG)
679 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
680 #endif
681 
682 	if (sizeof(struct freelist) > (1 << MINBUCKET))
683 		panic("minbucket too small/struct freelist too big");
684 
685 	/*
686 	 * Compute the number of kmem_map pages, if we have not
687 	 * done so already.
688 	 */
689 	kmeminit_nkmempages();
690 
691 	kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
692 		(vsize_t)(nkmempages * sizeof(struct kmemusage)));
693 	kmem_map = uvm_km_suballoc(kernel_map, (vaddr_t *)&kmembase,
694 		(vaddr_t *)&kmemlimit, (vsize_t)(nkmempages << PAGE_SHIFT),
695 			VM_MAP_INTRSAFE, FALSE, &kmem_map_store.vmi_map);
696 #ifdef KMEMSTATS
697 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
698 		if (1 << indx >= PAGE_SIZE)
699 			bucket[indx].kb_elmpercl = 1;
700 		else
701 			bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
702 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
703 	}
704 	for (indx = 0; indx < M_LAST; indx++)
705 		kmemstats[indx].ks_limit = (nkmempages << PAGE_SHIFT) * 6 / 10;
706 #endif
707 }
708 
709 #ifdef DDB
710 #include <ddb/db_output.h>
711 
712 /*
713  * Dump kmem statistics from ddb.
714  *
715  * usage: call dump_kmemstats
716  */
717 void	dump_kmemstats __P((void));
718 
719 void
720 dump_kmemstats()
721 {
722 #ifdef KMEMSTATS
723 	const char *name;
724 	int i;
725 
726 	for (i = 0; i < M_LAST; i++) {
727 		name = memname[i] ? memname[i] : "";
728 
729 		db_printf("%2d %s%.*s %ld\n", i, name,
730 		    (int)(20 - strlen(name)), "                    ",
731 		    kmemstats[i].ks_memuse);
732 	}
733 #else
734 	db_printf("Kmem stats are not being collected.\n");
735 #endif /* KMEMSTATS */
736 }
737 #endif /* DDB */
738