]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dde/fbsd/lib/common/bsd/modified/kern/kern_malloc.c
Inital import
[l4.git] / l4 / pkg / dde / fbsd / lib / common / bsd / modified / kern / kern_malloc.c
1 /**
2  * Original FreeBSD kern_malloc.c adapted to the specifics of the DDE
3  * uma (slab) implementation.
4  *
5  * \author Thomas Friebel <tf13@os.inf.tu-dresden.de>
6  */
7
8 /*-
9  * Copyright (c) 1987, 1991, 1993
10  *      The Regents of the University of California.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
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.3 (Berkeley) 1/4/94
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/sys/kern/kern_malloc.c,v 1.135.2.2 2005/01/31 23:26:16 imp Exp $");
41
42 #include "opt_vm.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kdb.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/mutex.h>
52 #include <sys/vmmeter.h>
53 #include <sys/proc.h>
54 #include <sys/sysctl.h>
55 #include <sys/time.h>
56
57 #include <vm/vm.h>
58 #include <vm/pmap.h>
59 #include <vm/vm_param.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_extern.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_page.h>
64 #include <vm/uma.h>
65 #include <vm/uma_int.h>
66 #include <vm/uma_dbg.h>
67 #ifdef DDE_FBSD
68 #include <dde_fbsd/uma.h>
69 #include <l4/dde/ddekit/pgtab.h>
70 #include <l4/dde/ddekit/panic.h>
71 #endif
72
73 #if defined(INVARIANTS) && defined(__i386__)
74 #include <machine/cpu.h>
75 #endif
76
77 /*
78  * When realloc() is called, if the new size is sufficiently smaller than
79  * the old size, realloc() will allocate a new, smaller block to avoid
80  * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
81  * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
82  */
83 #ifndef REALLOC_FRACTION
84 #define REALLOC_FRACTION        1       /* new block if <= half the size */
85 #endif
86
87 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
88 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
89 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
90
91 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
92 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
93
94 static void kmeminit(void *);
95 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
96
97 static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
98
99 static struct malloc_type *kmemstatistics;
100 #ifndef DDE_FBSD
101 static char *kmembase;
102 static char *kmemlimit;
103 #endif
104
105 #define KMEM_ZSHIFT     4
106 #define KMEM_ZBASE      16
107 #define KMEM_ZMASK      (KMEM_ZBASE - 1)
108
109 #ifndef DDE_FBSD
110 #define KMEM_ZMAX       PAGE_SIZE
111 #else
112 #define KMEM_ZMAX       (PAGE_SIZE - 80)
113 #endif
114 #define KMEM_ZSIZE      (KMEM_ZMAX >> KMEM_ZSHIFT)
115 static u_int8_t kmemsize[KMEM_ZSIZE + 1];
116
117 /* These won't be powers of two for long */
118 struct {
119         int kz_size;
120         char *kz_name;
121         uma_zone_t kz_zone;
122 } kmemzones[] = {
123         {16, "16", NULL},
124         {32, "32", NULL},
125         {64, "64", NULL},
126         {128, "128", NULL},
127         {256, "256", NULL},
128         {512, "512", NULL},
129         {1024, "1024", NULL},
130         {2048, "2048", NULL},
131 #ifndef DDE_FBSD
132         {4096, "4096", NULL},
133 #if PAGE_SIZE > 4096
134         {8192, "8192", NULL},
135 #if PAGE_SIZE > 8192
136         {16384, "16384", NULL},
137 #if PAGE_SIZE > 16384
138         {32768, "32768", NULL},
139 #if PAGE_SIZE > 32768
140         {65536, "65536", NULL},
141 #if PAGE_SIZE > 65536
142 #error  "Unsupported PAGE_SIZE"
143 #endif  /* 65536 */
144 #endif  /* 32768 */
145 #endif  /* 16384 */
146 #endif  /* 8192 */
147 #endif  /* 4096 */
148 #endif  /* DDE_FBSD */
149         {0, NULL},
150 };
151
152 u_int vm_kmem_size;
153 SYSCTL_UINT(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0,
154     "Size of kernel memory");
155
156 u_int vm_kmem_size_max;
157 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RD, &vm_kmem_size_max, 0,
158     "Maximum size of kernel memory");
159
160 u_int vm_kmem_size_scale;
161 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RD, &vm_kmem_size_scale, 0,
162     "Scale factor for kernel memory size");
163
164 /*
165  * The malloc_mtx protects the kmemstatistics linked list.
166  */
167
168 struct mtx malloc_mtx;
169
170 #ifdef MALLOC_PROFILE
171 uint64_t krequests[KMEM_ZSIZE + 1];
172
173 static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
174 #endif
175
176 static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS);
177
178 /* time_uptime of last malloc(9) failure */
179 static time_t t_malloc_fail;
180
181 #ifdef MALLOC_MAKE_FAILURES
182 /*
183  * Causes malloc failures every (n) mallocs with M_NOWAIT.  If set to 0,
184  * doesn't cause failures.
185  */
186 SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
187     "Kernel malloc debugging options");
188
189 static int malloc_failure_rate;
190 static int malloc_nowait_count;
191 static int malloc_failure_count;
192 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW,
193     &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
194 TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate);
195 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
196     &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
197 #endif
198
199 int
200 malloc_last_fail(void)
201 {
202
203         return (time_uptime - t_malloc_fail);
204 }
205
206 /*
207  * Add this to the informational malloc_type bucket.
208  */
209 static void
210 malloc_type_zone_allocated(struct malloc_type *ksp, unsigned long size,
211     int zindx)
212 {
213         mtx_lock(&ksp->ks_mtx);
214         ksp->ks_calls++;
215         if (zindx != -1)
216                 ksp->ks_size |= 1 << zindx;
217         if (size != 0) {
218                 ksp->ks_memuse += size;
219                 ksp->ks_inuse++;
220                 if (ksp->ks_memuse > ksp->ks_maxused)
221                         ksp->ks_maxused = ksp->ks_memuse;
222         }
223         mtx_unlock(&ksp->ks_mtx);
224 }
225
226 void
227 malloc_type_allocated(struct malloc_type *ksp, unsigned long size)
228 {
229         malloc_type_zone_allocated(ksp, size, -1);
230 }
231
232 /*
233  * Remove this allocation from the informational malloc_type bucket.
234  */
235 void
236 malloc_type_freed(struct malloc_type *ksp, unsigned long size)
237 {
238         mtx_lock(&ksp->ks_mtx);
239         KASSERT(size <= ksp->ks_memuse,
240                 ("malloc(9)/free(9) confusion.\n%s",
241                  "Probably freeing with wrong type, but maybe not here."));
242         ksp->ks_memuse -= size;
243         ksp->ks_inuse--;
244         mtx_unlock(&ksp->ks_mtx);
245 }
246
247 /*
248  *      malloc:
249  *
250  *      Allocate a block of memory.
251  *
252  *      If M_NOWAIT is set, this routine will not block and return NULL if
253  *      the allocation fails.
254  */
255 void *
256 #ifndef DDE_FBSD
257 malloc(size, type, flags)
258 #else
259 bsd_malloc(size, type, flags)
260 #endif
261         unsigned long size;
262         struct malloc_type *type;
263         int flags;
264 {
265         int indx;
266         caddr_t va;
267         uma_zone_t zone;
268 #ifndef DDE_FBSD
269         uma_keg_t keg;
270 #endif
271 #ifdef DIAGNOSTIC
272         unsigned long osize = size;
273 #endif
274
275 #ifdef INVARIANTS
276         /*
277          * To make sure that WAITOK or NOWAIT is set, but not more than
278          * one, and check against the API botches that are common.
279          */
280         indx = flags & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT);
281         if (indx != M_NOWAIT && indx != M_WAITOK) {
282 #ifdef DDE_FBSD
283                 static  int once;
284                 if (once == 0) {
285                         printf("Bad malloc flags: %x\n", indx);
286                         ddekit_debug("check backtrace");
287                         flags |= M_WAITOK;
288                         once++;
289                 }
290 #else
291                 static  struct timeval lasterr;
292                 static  int curerr, once;
293                 if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
294                         printf("Bad malloc flags: %x\n", indx);
295                         kdb_backtrace();
296                         flags |= M_WAITOK;
297                         once++;
298                 }
299 #endif
300         }
301 #endif
302 #if 0
303         if (size == 0)
304                 kdb_enter("zero size malloc");
305 #endif
306 #ifdef MALLOC_MAKE_FAILURES
307         if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
308                 atomic_add_int(&malloc_nowait_count, 1);
309                 if ((malloc_nowait_count % malloc_failure_rate) == 0) {
310                         atomic_add_int(&malloc_failure_count, 1);
311                         t_malloc_fail = time_uptime;
312                         return (NULL);
313                 }
314         }
315 #endif
316         if (flags & M_WAITOK)
317                 KASSERT(curthread->td_intr_nesting_level == 0,
318                    ("malloc(M_WAITOK) in interrupt context"));
319         if (size <= KMEM_ZMAX) {
320                 if (size & KMEM_ZMASK)
321                         size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
322                 indx = kmemsize[size >> KMEM_ZSHIFT];
323                 zone = kmemzones[indx].kz_zone;
324 #ifndef DDE_FBSD
325                 keg = zone->uz_keg;
326 #endif
327 #ifdef MALLOC_PROFILE
328                 krequests[size >> KMEM_ZSHIFT]++;
329 #endif
330                 va = uma_zalloc(zone, flags);
331                 if (va != NULL)
332 #ifndef DDE_FBSD
333                         size = keg->uk_size;
334 #else
335                         size = zone->size;
336 #endif
337                 malloc_type_zone_allocated(type, va == NULL ? 0 : size, indx);
338         } else {
339                 size = roundup(size, PAGE_SIZE);
340                 zone = NULL;
341 #ifndef DDE_FBSD
342                 keg = NULL;
343                 va = uma_large_malloc(size, flags);
344 #else
345                 va = bsd_large_malloc(size, flags);
346 #endif
347                 malloc_type_allocated(type, va == NULL ? 0 : size);
348         }
349         if (flags & M_WAITOK)
350                 KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
351         else if (va == NULL)
352                 t_malloc_fail = time_uptime;
353 #ifdef DIAGNOSTIC
354         if (va != NULL && !(flags & M_ZERO)) {
355                 memset(va, 0x70, osize);
356         }
357 #endif
358         return ((void *) va);
359 }
360
361 /*
362  *      free:
363  *
364  *      Free a block of memory allocated by malloc.
365  *
366  *      This routine may not block.
367  */
368 void
369 #ifndef DDE_FBSD
370 free(addr, type)
371 #else
372 bsd_free(addr, type)
373 #endif
374         void *addr;
375         struct malloc_type *type;
376 {
377 #ifndef DDE_FBSD
378         uma_slab_t slab;
379 #else
380         uma_zone_t zone;
381 #endif
382         u_long size;
383
384         /* free(NULL, ...) does nothing */
385         if (addr == NULL)
386                 return;
387
388         KASSERT(type->ks_memuse > 0,
389                 ("malloc(9)/free(9) confusion.\n%s",
390                  "Probably freeing with wrong type, but maybe not here."));
391         size = 0;
392
393 #ifndef DDE_FBSD
394         slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
395
396         if (slab == NULL)
397                 panic("free: address %p(%p) has not been allocated.\n",
398                     addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
399
400
401         if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
402 #ifdef INVARIANTS
403                 struct malloc_type **mtp = addr;
404 #endif
405                 size = slab->us_keg->uk_size;
406 #ifdef INVARIANTS
407                 /*
408                  * Cache a pointer to the malloc_type that most recently freed
409                  * this memory here.  This way we know who is most likely to
410                  * have stepped on it later.
411                  *
412                  * This code assumes that size is a multiple of 8 bytes for
413                  * 64 bit machines
414                  */
415                 mtp = (struct malloc_type **)
416                     ((unsigned long)mtp & ~UMA_ALIGN_PTR);
417                 mtp += (size - sizeof(struct malloc_type *)) /
418                     sizeof(struct malloc_type *);
419                 *mtp = type;
420 #endif
421                 uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab);
422         } else {
423                 size = slab->us_size;
424                 uma_large_free(slab);
425         }
426 #else /* DDE_FBSD */
427         switch (ddekit_pgtab_get_type(addr)) {
428                 case PTE_TYPE_UMA:
429                         // size was small enough to use uma
430                         zone = uma_getzone(addr);
431                         KASSERT(zone->flags&UMA_ZONE_MALLOC, ("free called on uma allocated item"));
432                         size = zone->size;
433                         uma_zfree(zone, addr);
434                         break;
435                 case PTE_TYPE_LARGE:
436                         // we had to use large_malloc, so now use large_free
437                         zone = uma_getzone(addr);
438                         size = ddekit_pgtab_get_size(addr);
439                         bsd_large_free(addr);
440                         break;
441                 default:
442                         ddekit_debug("unknown memory class");
443                         break;
444         }
445 #endif /* DDE_FBSD */
446
447         malloc_type_freed(type, size);
448 }
449
450 /*
451  *      realloc: change the size of a memory block
452  */
453 void *
454 #ifndef DDE_FBSD
455 realloc(addr, size, type, flags)
456 #else
457 bsd_realloc(addr, size, type, flags)
458 #endif
459         void *addr;
460         unsigned long size;
461         struct malloc_type *type;
462         int flags;
463 {
464 #ifndef DDE_FBSD
465         uma_slab_t slab;
466 #else
467         uma_zone_t zone;
468 #endif /* DDE_FBSD */
469         unsigned long alloc;
470         void *newaddr;
471
472         /* realloc(NULL, ...) is equivalent to malloc(...) */
473         if (addr == NULL)
474                 return (malloc(size, type, flags));
475
476 #ifndef DDE_FBSD
477         slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
478
479         /* Sanity check */
480         KASSERT(slab != NULL,
481             ("realloc: address %p out of range", (void *)addr));
482
483         /* Get the size of the original block */
484         if (slab->us_keg)
485                 alloc = slab->us_keg->uk_size;
486         else
487                 alloc = slab->us_size;
488 #else /* DDE_FBSD */
489         alloc = 0;
490         switch (ddekit_pgtab_get_type(addr)) {
491                 case PTE_TYPE_UMA:
492                         // was allocated with uma
493                         zone = uma_getzone(addr);
494                         alloc = zone->size;
495                         break;
496                 case PTE_TYPE_LARGE:
497                         // had to use large_malloc
498                         alloc = ddekit_pgtab_get_size(addr);
499                         break;
500         }
501 #endif /* DDE_FBSD */
502
503
504         /* Reuse the original block if appropriate */
505         if (size <= alloc
506             && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
507                 return (addr);
508
509         /* Allocate a new, bigger (or smaller) block */
510         if ((newaddr = malloc(size, type, flags)) == NULL)
511                 return (NULL);
512
513         /* Copy over original contents */
514         bcopy(addr, newaddr, min(size, alloc));
515         free(addr, type);
516         return (newaddr);
517 }
518
519 /*
520  *      reallocf: same as realloc() but free memory on failure.
521  */
522 void *
523 #ifndef DDE_FBSD
524 reallocf(addr, size, type, flags)
525 #else
526 bsd_reallocf(addr, size, type, flags)
527 #endif
528         void *addr;
529         unsigned long size;
530         struct malloc_type *type;
531         int flags;
532 {
533         void *mem;
534
535         if ((mem = realloc(addr, size, type, flags)) == NULL)
536                 free(addr, type);
537         return (mem);
538 }
539
540 /*
541  * Initialize the kernel memory allocator
542  */
543 /* ARGSUSED*/
544 static void
545 kmeminit(dummy)
546         void *dummy;
547 {
548         u_int8_t indx;
549 #ifndef DDE_FBSD
550         u_long mem_size;
551 #endif
552         int i;
553  
554         mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
555
556 #ifndef DDE_FBSD
557         /*
558          * Try to auto-tune the kernel memory size, so that it is
559          * more applicable for a wider range of machine sizes.
560          * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
561          * a VM_KMEM_SIZE of 12MB is a fair compromise.  The
562          * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
563          * available, and on an X86 with a total KVA space of 256MB,
564          * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
565          *
566          * Note that the kmem_map is also used by the zone allocator,
567          * so make sure that there is enough space.
568          */
569         vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE;
570         mem_size = cnt.v_page_count;
571
572 #if defined(VM_KMEM_SIZE_SCALE)
573         vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
574 #endif
575         TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale);
576         if (vm_kmem_size_scale > 0 &&
577             (mem_size / vm_kmem_size_scale) > (vm_kmem_size / PAGE_SIZE))
578                 vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE;
579
580 #if defined(VM_KMEM_SIZE_MAX)
581         vm_kmem_size_max = VM_KMEM_SIZE_MAX;
582 #endif
583         TUNABLE_INT_FETCH("vm.kmem_size_max", &vm_kmem_size_max);
584         if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
585                 vm_kmem_size = vm_kmem_size_max;
586
587         /* Allow final override from the kernel environment */
588 #ifndef BURN_BRIDGES
589         if (TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size) != 0)
590                 printf("kern.vm.kmem.size is now called vm.kmem_size!\n");
591 #endif
592         TUNABLE_INT_FETCH("vm.kmem_size", &vm_kmem_size);
593
594         /*
595          * Limit kmem virtual size to twice the physical memory.
596          * This allows for kmem map sparseness, but limits the size
597          * to something sane. Be careful to not overflow the 32bit
598          * ints while doing the check.
599          */
600         if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count)
601                 vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
602
603         /*
604          * Tune settings based on the kernel map's size at this time.
605          */
606         init_param3(vm_kmem_size / PAGE_SIZE);
607
608         kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
609                 (vm_offset_t *)&kmemlimit, vm_kmem_size);
610         kmem_map->system_map = 1;
611
612         uma_startup2();
613 #endif /* DDE_FBSD */
614
615         for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
616                 int size = kmemzones[indx].kz_size;
617                 char *name = kmemzones[indx].kz_name;
618
619                 kmemzones[indx].kz_zone = uma_zcreate(name, size,
620 #ifdef INVARIANTS
621                     mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
622 #else
623                     NULL, NULL, NULL, NULL,
624 #endif
625                     UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
626                     
627                 for (;i <= size; i+= KMEM_ZBASE)
628                         kmemsize[i >> KMEM_ZSHIFT] = indx;
629                 
630         }
631 }
632
633 void
634 malloc_init(data)
635         void *data;
636 {
637         struct malloc_type *type = (struct malloc_type *)data;
638
639         mtx_lock(&malloc_mtx);
640         if (type->ks_magic != M_MAGIC)
641                 panic("malloc type lacks magic");
642
643         if (cnt.v_page_count == 0)
644                 panic("malloc_init not allowed before vm init");
645
646         if (type->ks_next != NULL)
647                 return;
648
649         type->ks_next = kmemstatistics; 
650         kmemstatistics = type;
651         mtx_init(&type->ks_mtx, type->ks_shortdesc, "Malloc Stats", MTX_DEF);
652         mtx_unlock(&malloc_mtx);
653 }
654
655 void
656 malloc_uninit(data)
657         void *data;
658 {
659         struct malloc_type *type = (struct malloc_type *)data;
660         struct malloc_type *t;
661
662         mtx_lock(&malloc_mtx);
663         mtx_lock(&type->ks_mtx);
664         if (type->ks_magic != M_MAGIC)
665                 panic("malloc type lacks magic");
666
667         if (cnt.v_page_count == 0)
668                 panic("malloc_uninit not allowed before vm init");
669
670         if (type == kmemstatistics)
671                 kmemstatistics = type->ks_next;
672         else {
673                 for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
674                         if (t->ks_next == type) {
675                                 t->ks_next = type->ks_next;
676                                 break;
677                         }
678                 }
679         }
680         type->ks_next = NULL;
681         mtx_destroy(&type->ks_mtx);
682         mtx_unlock(&malloc_mtx);
683 }
684
685 static int
686 sysctl_kern_malloc(SYSCTL_HANDLER_ARGS)
687 {
688         struct malloc_type *type;
689         int linesize = 128;
690         int curline;
691         int bufsize;
692         int first;
693         int error;
694         char *buf;
695         char *p;
696         int cnt;
697         int len;
698         int i;
699
700         cnt = 0;
701
702         mtx_lock(&malloc_mtx);
703         for (type = kmemstatistics; type != NULL; type = type->ks_next)
704                 cnt++;
705
706         mtx_unlock(&malloc_mtx);
707         bufsize = linesize * (cnt + 1);
708         p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
709         mtx_lock(&malloc_mtx);
710
711         len = snprintf(p, linesize,
712             "\n        Type  InUse MemUse HighUse Requests  Size(s)\n");
713         p += len;
714
715         for (type = kmemstatistics; cnt != 0 && type != NULL;
716             type = type->ks_next, cnt--) {
717                 if (type->ks_calls == 0)
718                         continue;
719
720                 curline = linesize - 2; /* Leave room for the \n */
721                 len = snprintf(p, curline, "%13s%6lu%6luK%7luK%9llu",
722                         type->ks_shortdesc,
723                         type->ks_inuse,
724                         (type->ks_memuse + 1023) / 1024,
725                         (type->ks_maxused + 1023) / 1024,
726                         (long long unsigned)type->ks_calls);
727                 curline -= len;
728                 p += len;
729
730                 first = 1;
731                 for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1;
732                     i++) {
733                         if (type->ks_size & (1 << i)) {
734                                 if (first)
735                                         len = snprintf(p, curline, "  ");
736                                 else
737                                         len = snprintf(p, curline, ",");
738                                 curline -= len;
739                                 p += len;
740
741                                 len = snprintf(p, curline,
742                                     "%s", kmemzones[i].kz_name);
743                                 curline -= len;
744                                 p += len;
745
746                                 first = 0;
747                         }
748                 }
749
750                 len = snprintf(p, 2, "\n");
751                 p += len;
752         }
753
754         mtx_unlock(&malloc_mtx);
755         error = SYSCTL_OUT(req, buf, p - buf);
756
757         free(buf, M_TEMP);
758         return (error);
759 }
760
761 SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD,
762     NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats");
763
764 #ifdef MALLOC_PROFILE
765
766 static int
767 sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
768 {
769         int linesize = 64;
770         uint64_t count;
771         uint64_t waste;
772         uint64_t mem;
773         int bufsize;
774         int error;
775         char *buf;
776         int rsize;
777         int size;
778         char *p;
779         int len;
780         int i;
781
782         bufsize = linesize * (KMEM_ZSIZE + 1);
783         bufsize += 128;         /* For the stats line */
784         bufsize += 128;         /* For the banner line */
785         waste = 0;
786         mem = 0;
787
788         p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
789         len = snprintf(p, bufsize,
790             "\n  Size                    Requests  Real Size\n");
791         bufsize -= len;
792         p += len;
793
794         for (i = 0; i < KMEM_ZSIZE; i++) {
795                 size = i << KMEM_ZSHIFT;
796                 rsize = kmemzones[kmemsize[i]].kz_size;
797                 count = (long long unsigned)krequests[i];
798
799                 len = snprintf(p, bufsize, "%6d%28llu%11d\n",
800                     size, (unsigned long long)count, rsize);
801                 bufsize -= len;
802                 p += len;
803
804                 if ((rsize * count) > (size * count))
805                         waste += (rsize * count) - (size * count);
806                 mem += (rsize * count);
807         }
808
809         len = snprintf(p, bufsize,
810             "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
811             (unsigned long long)mem, (unsigned long long)waste);
812         p += len;
813
814         error = SYSCTL_OUT(req, buf, p - buf);
815
816         free(buf, M_TEMP);
817         return (error);
818 }
819
820 SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
821     NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
822 #endif /* MALLOC_PROFILE */