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