2 * Copyright (c) 1987, 1991, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2005-2006 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94
35 * Kernel malloc(9) implementation -- general purpose kernel memory allocator
36 * based on memory types. Back end is implemented using the UMA(9) zone
37 * allocator. A set of fixed-size buckets are used for smaller allocations,
38 * and a special UMA allocation interface is used for larger allocations.
39 * Callers declare memory types, and statistics are maintained independently
40 * for each memory type. Statistics are maintained per-CPU for performance
41 * reasons. See malloc(9) and comments in malloc.h for a detailed
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
52 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/vmmeter.h>
62 #include <sys/sysctl.h>
67 #include <vm/vm_param.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_page.h>
73 #include <vm/uma_int.h>
74 #include <vm/uma_dbg.h>
77 #include <vm/memguard.h>
80 #include <vm/redzone.h>
83 #if defined(INVARIANTS) && defined(__i386__)
84 #include <machine/cpu.h>
90 * When realloc() is called, if the new size is sufficiently smaller than
91 * the old size, realloc() will allocate a new, smaller block to avoid
92 * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
93 * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
95 #ifndef REALLOC_FRACTION
96 #define REALLOC_FRACTION 1 /* new block if <= half the size */
100 * Centrally define some common malloc types.
102 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
103 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
104 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
106 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
107 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
109 static void kmeminit(void *);
110 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL);
112 static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
114 static struct malloc_type *kmemstatistics;
115 static vm_offset_t kmembase;
116 static vm_offset_t kmemlimit;
117 static int kmemcount;
119 #define KMEM_ZSHIFT 4
120 #define KMEM_ZBASE 16
121 #define KMEM_ZMASK (KMEM_ZBASE - 1)
123 #define KMEM_ZMAX PAGE_SIZE
124 #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT)
125 static u_int8_t kmemsize[KMEM_ZSIZE + 1];
128 * Small malloc(9) memory allocations are allocated from a set of UMA buckets
131 * XXX: The comment here used to read "These won't be powers of two for
132 * long." It's possible that a significant amount of wasted memory could be
133 * recovered by tuning the sizes of these buckets.
146 {1024, "1024", NULL},
147 {2048, "2048", NULL},
148 {4096, "4096", NULL},
150 {8192, "8192", NULL},
152 {16384, "16384", NULL},
153 #if PAGE_SIZE > 16384
154 {32768, "32768", NULL},
155 #if PAGE_SIZE > 32768
156 {65536, "65536", NULL},
157 #if PAGE_SIZE > 65536
158 #error "Unsupported PAGE_SIZE"
168 * Zone to allocate malloc type descriptions from. For ABI reasons, memory
169 * types are described by a data structure passed by the declaring code, but
170 * the malloc(9) implementation has its own data structure describing the
171 * type and statistics. This permits the malloc(9)-internal data structures
172 * to be modified without breaking binary-compiled kernel modules that
173 * declare malloc types.
175 static uma_zone_t mt_zone;
178 SYSCTL_UINT(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0,
179 "Size of kernel memory");
181 u_int vm_kmem_size_min;
182 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RD, &vm_kmem_size_min, 0,
183 "Minimum size of kernel memory");
185 u_int vm_kmem_size_max;
186 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RD, &vm_kmem_size_max, 0,
187 "Maximum size of kernel memory");
189 u_int vm_kmem_size_scale;
190 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RD, &vm_kmem_size_scale, 0,
191 "Scale factor for kernel memory size");
194 * The malloc_mtx protects the kmemstatistics linked list.
196 struct mtx malloc_mtx;
198 #ifdef MALLOC_PROFILE
199 uint64_t krequests[KMEM_ZSIZE + 1];
201 static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
204 static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS);
207 * time_uptime of the last malloc(9) failure (induced or real).
209 static time_t t_malloc_fail;
212 * malloc(9) fault injection -- cause malloc failures every (n) mallocs when
213 * the caller specifies M_NOWAIT. If set to 0, no failures are caused.
215 #ifdef MALLOC_MAKE_FAILURES
216 SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
217 "Kernel malloc debugging options");
219 static int malloc_failure_rate;
220 static int malloc_nowait_count;
221 static int malloc_failure_count;
222 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW,
223 &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
224 TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate);
225 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
226 &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
230 malloc_last_fail(void)
233 return (time_uptime - t_malloc_fail);
237 * An allocation has succeeded -- update malloc type statistics for the
238 * amount of bucket size. Occurs within a critical section so that the
239 * thread isn't preempted and doesn't migrate while updating per-PCU
243 malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size,
246 struct malloc_type_internal *mtip;
247 struct malloc_type_stats *mtsp;
250 mtip = mtp->ks_handle;
251 mtsp = &mtip->mti_stats[curcpu];
253 mtsp->mts_memalloced += size;
254 mtsp->mts_numallocs++;
257 mtsp->mts_size |= 1 << zindx;
262 malloc_type_allocated(struct malloc_type *mtp, unsigned long size)
266 malloc_type_zone_allocated(mtp, size, -1);
270 * A free operation has occurred -- update malloc type statistics for the
271 * amount of the bucket size. Occurs within a critical section so that the
272 * thread isn't preempted and doesn't migrate while updating per-CPU
276 malloc_type_freed(struct malloc_type *mtp, unsigned long size)
278 struct malloc_type_internal *mtip;
279 struct malloc_type_stats *mtsp;
282 mtip = mtp->ks_handle;
283 mtsp = &mtip->mti_stats[curcpu];
284 mtsp->mts_memfreed += size;
285 mtsp->mts_numfrees++;
292 * Allocate a block of memory.
294 * If M_NOWAIT is set, this routine will not block and return NULL if
295 * the allocation fails.
298 malloc(unsigned long size, struct malloc_type *mtp, int flags)
304 #if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE)
305 unsigned long osize = size;
310 * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
312 indx = flags & (M_WAITOK | M_NOWAIT);
313 if (indx != M_NOWAIT && indx != M_WAITOK) {
314 static struct timeval lasterr;
315 static int curerr, once;
316 if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
317 printf("Bad malloc flags: %x\n", indx);
324 #ifdef MALLOC_MAKE_FAILURES
325 if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
326 atomic_add_int(&malloc_nowait_count, 1);
327 if ((malloc_nowait_count % malloc_failure_rate) == 0) {
328 atomic_add_int(&malloc_failure_count, 1);
329 t_malloc_fail = time_uptime;
334 if (flags & M_WAITOK)
335 KASSERT(curthread->td_intr_nesting_level == 0,
336 ("malloc(M_WAITOK) in interrupt context"));
338 #ifdef DEBUG_MEMGUARD
339 if (memguard_cmp(mtp))
340 return memguard_alloc(size, flags);
344 size = redzone_size_ntor(size);
347 if (size <= KMEM_ZMAX) {
348 if (size & KMEM_ZMASK)
349 size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
350 indx = kmemsize[size >> KMEM_ZSHIFT];
351 zone = kmemzones[indx].kz_zone;
353 #ifdef MALLOC_PROFILE
354 krequests[size >> KMEM_ZSHIFT]++;
356 va = uma_zalloc(zone, flags);
359 malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
361 size = roundup(size, PAGE_SIZE);
364 va = uma_large_malloc(size, flags);
365 malloc_type_allocated(mtp, va == NULL ? 0 : size);
367 if (flags & M_WAITOK)
368 KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
370 t_malloc_fail = time_uptime;
372 if (va != NULL && !(flags & M_ZERO)) {
373 memset(va, 0x70, osize);
378 va = redzone_setup(va, osize);
380 return ((void *) va);
386 * Free a block of memory allocated by malloc.
388 * This routine may not block.
391 free(void *addr, struct malloc_type *mtp)
396 /* free(NULL, ...) does nothing */
400 #ifdef DEBUG_MEMGUARD
401 if (memguard_cmp(mtp)) {
409 addr = redzone_addr_ntor(addr);
414 slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
417 panic("free: address %p(%p) has not been allocated.\n",
418 addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
421 if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
423 struct malloc_type **mtpp = addr;
425 size = slab->us_keg->uk_size;
428 * Cache a pointer to the malloc_type that most recently freed
429 * this memory here. This way we know who is most likely to
430 * have stepped on it later.
432 * This code assumes that size is a multiple of 8 bytes for
435 mtpp = (struct malloc_type **)
436 ((unsigned long)mtpp & ~UMA_ALIGN_PTR);
437 mtpp += (size - sizeof(struct malloc_type *)) /
438 sizeof(struct malloc_type *);
441 uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab);
443 size = slab->us_size;
444 uma_large_free(slab);
446 malloc_type_freed(mtp, size);
450 * realloc: change the size of a memory block
453 realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
459 /* realloc(NULL, ...) is equivalent to malloc(...) */
461 return (malloc(size, mtp, flags));
464 * XXX: Should report free of old memory and alloc of new memory to
468 #ifdef DEBUG_MEMGUARD
469 if (memguard_cmp(mtp)) {
477 alloc = redzone_get_size(addr);
479 slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
482 KASSERT(slab != NULL,
483 ("realloc: address %p out of range", (void *)addr));
485 /* Get the size of the original block */
486 if (!(slab->us_flags & UMA_SLAB_MALLOC))
487 alloc = slab->us_keg->uk_size;
489 alloc = slab->us_size;
491 /* Reuse the original block if appropriate */
493 && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
495 #endif /* !DEBUG_REDZONE */
497 #ifdef DEBUG_MEMGUARD
501 /* Allocate a new, bigger (or smaller) block */
502 if ((newaddr = malloc(size, mtp, flags)) == NULL)
505 /* Copy over original contents */
506 bcopy(addr, newaddr, min(size, alloc));
512 * reallocf: same as realloc() but free memory on failure.
515 reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
519 if ((mem = realloc(addr, size, mtp, flags)) == NULL)
525 * Initialize the kernel memory allocator
529 kmeminit(void *dummy)
535 mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
538 * Try to auto-tune the kernel memory size, so that it is
539 * more applicable for a wider range of machine sizes.
540 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
541 * a VM_KMEM_SIZE of 12MB is a fair compromise. The
542 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
543 * available, and on an X86 with a total KVA space of 256MB,
544 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
546 * Note that the kmem_map is also used by the zone allocator,
547 * so make sure that there is enough space.
549 vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE;
550 mem_size = cnt.v_page_count;
552 #if defined(VM_KMEM_SIZE_SCALE)
553 vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
555 TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale);
556 if (vm_kmem_size_scale > 0 &&
557 (mem_size / vm_kmem_size_scale) > (vm_kmem_size / PAGE_SIZE))
558 vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE;
560 #if defined(VM_KMEM_SIZE_MIN)
561 vm_kmem_size_min = VM_KMEM_SIZE_MIN;
563 TUNABLE_INT_FETCH("vm.kmem_size_min", &vm_kmem_size_min);
564 if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) {
565 vm_kmem_size = vm_kmem_size_min;
568 #if defined(VM_KMEM_SIZE_MAX)
569 vm_kmem_size_max = VM_KMEM_SIZE_MAX;
571 TUNABLE_INT_FETCH("vm.kmem_size_max", &vm_kmem_size_max);
572 if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
573 vm_kmem_size = vm_kmem_size_max;
575 /* Allow final override from the kernel environment */
577 if (TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size) != 0)
578 printf("kern.vm.kmem.size is now called vm.kmem_size!\n");
580 TUNABLE_INT_FETCH("vm.kmem_size", &vm_kmem_size);
583 * Limit kmem virtual size to twice the physical memory.
584 * This allows for kmem map sparseness, but limits the size
585 * to something sane. Be careful to not overflow the 32bit
586 * ints while doing the check.
588 if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count)
589 vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
592 * Tune settings based on the kernel map's size at this time.
594 init_param3(vm_kmem_size / PAGE_SIZE);
596 kmem_map = kmem_suballoc(kernel_map, &kmembase, &kmemlimit,
598 kmem_map->system_map = 1;
600 #ifdef DEBUG_MEMGUARD
602 * Initialize MemGuard if support compiled in. MemGuard is a
603 * replacement allocator used for detecting tamper-after-free
604 * scenarios as they occur. It is only used for debugging.
606 vm_memguard_divisor = 10;
607 TUNABLE_INT_FETCH("vm.memguard.divisor", &vm_memguard_divisor);
609 /* Pick a conservative value if provided value sucks. */
610 if ((vm_memguard_divisor <= 0) ||
611 ((vm_kmem_size / vm_memguard_divisor) == 0))
612 vm_memguard_divisor = 10;
613 memguard_init(kmem_map, vm_kmem_size / vm_memguard_divisor);
618 mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal),
620 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
622 NULL, NULL, NULL, NULL,
624 UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
625 for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
626 int size = kmemzones[indx].kz_size;
627 char *name = kmemzones[indx].kz_name;
629 kmemzones[indx].kz_zone = uma_zcreate(name, size,
631 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
633 NULL, NULL, NULL, NULL,
635 UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
637 for (;i <= size; i+= KMEM_ZBASE)
638 kmemsize[i >> KMEM_ZSHIFT] = indx;
644 malloc_init(void *data)
646 struct malloc_type_internal *mtip;
647 struct malloc_type *mtp;
649 KASSERT(cnt.v_page_count != 0, ("malloc_register before vm_init"));
652 mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO);
653 mtp->ks_handle = mtip;
655 mtx_lock(&malloc_mtx);
656 mtp->ks_next = kmemstatistics;
657 kmemstatistics = mtp;
659 mtx_unlock(&malloc_mtx);
663 malloc_uninit(void *data)
665 struct malloc_type_internal *mtip;
666 struct malloc_type_stats *mtsp;
667 struct malloc_type *mtp, *temp;
669 long temp_allocs, temp_bytes;
673 KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL"));
674 mtx_lock(&malloc_mtx);
675 mtip = mtp->ks_handle;
676 mtp->ks_handle = NULL;
677 if (mtp != kmemstatistics) {
678 for (temp = kmemstatistics; temp != NULL;
679 temp = temp->ks_next) {
680 if (temp->ks_next == mtp)
681 temp->ks_next = mtp->ks_next;
684 kmemstatistics = mtp->ks_next;
686 mtx_unlock(&malloc_mtx);
689 * Look for memory leaks.
691 temp_allocs = temp_bytes = 0;
692 for (i = 0; i < MAXCPU; i++) {
693 mtsp = &mtip->mti_stats[i];
694 temp_allocs += mtsp->mts_numallocs;
695 temp_allocs -= mtsp->mts_numfrees;
696 temp_bytes += mtsp->mts_memalloced;
697 temp_bytes -= mtsp->mts_memfreed;
699 if (temp_allocs > 0 || temp_bytes > 0) {
700 printf("Warning: memory type %s leaked memory on destroy "
701 "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
702 temp_allocs, temp_bytes);
705 slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK));
706 uma_zfree_arg(mt_zone, mtip, slab);
710 malloc_desc2type(const char *desc)
712 struct malloc_type *mtp;
714 mtx_assert(&malloc_mtx, MA_OWNED);
715 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
716 if (strcmp(mtp->ks_shortdesc, desc) == 0)
723 sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS)
725 struct malloc_type_stream_header mtsh;
726 struct malloc_type_internal *mtip;
727 struct malloc_type_header mth;
728 struct malloc_type *mtp;
729 int buflen, count, error, i;
733 mtx_lock(&malloc_mtx);
735 mtx_assert(&malloc_mtx, MA_OWNED);
737 mtx_unlock(&malloc_mtx);
738 buflen = sizeof(mtsh) + count * (sizeof(mth) +
739 sizeof(struct malloc_type_stats) * MAXCPU) + 1;
740 buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
741 mtx_lock(&malloc_mtx);
742 if (count < kmemcount) {
743 free(buffer, M_TEMP);
747 sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
750 * Insert stream header.
752 bzero(&mtsh, sizeof(mtsh));
753 mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION;
754 mtsh.mtsh_maxcpus = MAXCPU;
755 mtsh.mtsh_count = kmemcount;
756 if (sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)) < 0) {
757 mtx_unlock(&malloc_mtx);
763 * Insert alternating sequence of type headers and type statistics.
765 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
766 mtip = (struct malloc_type_internal *)mtp->ks_handle;
769 * Insert type header.
771 bzero(&mth, sizeof(mth));
772 strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME);
773 if (sbuf_bcat(&sbuf, &mth, sizeof(mth)) < 0) {
774 mtx_unlock(&malloc_mtx);
780 * Insert type statistics for each CPU.
782 for (i = 0; i < MAXCPU; i++) {
783 if (sbuf_bcat(&sbuf, &mtip->mti_stats[i],
784 sizeof(mtip->mti_stats[i])) < 0) {
785 mtx_unlock(&malloc_mtx);
791 mtx_unlock(&malloc_mtx);
793 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
796 free(buffer, M_TEMP);
800 SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
801 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats",
802 "Return malloc types");
804 SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0,
805 "Count of kernel malloc types");
808 DB_SHOW_COMMAND(malloc, db_show_malloc)
810 struct malloc_type_internal *mtip;
811 struct malloc_type *mtp;
812 u_int64_t allocs, frees;
813 u_int64_t alloced, freed;
816 db_printf("%18s %12s %12s %12s\n", "Type", "InUse", "MemUse",
818 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
819 mtip = (struct malloc_type_internal *)mtp->ks_handle;
824 for (i = 0; i < MAXCPU; i++) {
825 allocs += mtip->mti_stats[i].mts_numallocs;
826 frees += mtip->mti_stats[i].mts_numfrees;
827 alloced += mtip->mti_stats[i].mts_memalloced;
828 freed += mtip->mti_stats[i].mts_memfreed;
830 db_printf("%18s %12ju %12juK %12ju\n",
831 mtp->ks_shortdesc, allocs - frees,
832 (alloced - freed + 1023) / 1024, allocs);
837 #ifdef MALLOC_PROFILE
840 sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
854 bufsize = linesize * (KMEM_ZSIZE + 1);
855 bufsize += 128; /* For the stats line */
856 bufsize += 128; /* For the banner line */
860 buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
861 sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN);
863 "\n Size Requests Real Size\n");
864 for (i = 0; i < KMEM_ZSIZE; i++) {
865 size = i << KMEM_ZSHIFT;
866 rsize = kmemzones[kmemsize[i]].kz_size;
867 count = (long long unsigned)krequests[i];
869 sbuf_printf(&sbuf, "%6d%28llu%11d\n", size,
870 (unsigned long long)count, rsize);
872 if ((rsize * count) > (size * count))
873 waste += (rsize * count) - (size * count);
874 mem += (rsize * count);
877 "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
878 (unsigned long long)mem, (unsigned long long)waste);
881 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
888 SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
889 NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
890 #endif /* MALLOC_PROFILE */