2 * Copyright (c) 1987, 1991, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2005-2009 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$");
49 #include "opt_kdtrace.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/mutex.h>
60 #include <sys/vmmeter.h>
63 #include <sys/sysctl.h>
68 #include <vm/vm_param.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_page.h>
74 #include <vm/uma_int.h>
75 #include <vm/uma_dbg.h>
78 #include <vm/memguard.h>
81 #include <vm/redzone.h>
84 #if defined(INVARIANTS) && defined(__i386__)
85 #include <machine/cpu.h>
91 #include <sys/dtrace_bsd.h>
93 dtrace_malloc_probe_func_t dtrace_malloc_probe;
97 * When realloc() is called, if the new size is sufficiently smaller than
98 * the old size, realloc() will allocate a new, smaller block to avoid
99 * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
100 * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
102 #ifndef REALLOC_FRACTION
103 #define REALLOC_FRACTION 1 /* new block if <= half the size */
107 * Centrally define some common malloc types.
109 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
110 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
111 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
113 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
114 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
116 static void kmeminit(void *);
117 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL);
119 static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
121 static struct malloc_type *kmemstatistics;
122 static vm_offset_t kmembase;
123 static vm_offset_t kmemlimit;
124 static int kmemcount;
126 #define KMEM_ZSHIFT 4
127 #define KMEM_ZBASE 16
128 #define KMEM_ZMASK (KMEM_ZBASE - 1)
130 #define KMEM_ZMAX PAGE_SIZE
131 #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT)
132 static u_int8_t kmemsize[KMEM_ZSIZE + 1];
135 * Small malloc(9) memory allocations are allocated from a set of UMA buckets
138 * XXX: The comment here used to read "These won't be powers of two for
139 * long." It's possible that a significant amount of wasted memory could be
140 * recovered by tuning the sizes of these buckets.
153 {1024, "1024", NULL},
154 {2048, "2048", NULL},
155 {4096, "4096", NULL},
157 {8192, "8192", NULL},
159 {16384, "16384", NULL},
160 #if PAGE_SIZE > 16384
161 {32768, "32768", NULL},
162 #if PAGE_SIZE > 32768
163 {65536, "65536", NULL},
164 #if PAGE_SIZE > 65536
165 #error "Unsupported PAGE_SIZE"
175 * Zone to allocate malloc type descriptions from. For ABI reasons, memory
176 * types are described by a data structure passed by the declaring code, but
177 * the malloc(9) implementation has its own data structure describing the
178 * type and statistics. This permits the malloc(9)-internal data structures
179 * to be modified without breaking binary-compiled kernel modules that
180 * declare malloc types.
182 static uma_zone_t mt_zone;
185 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0,
186 "Size of kernel memory");
188 static u_long vm_kmem_size_min;
189 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RD, &vm_kmem_size_min, 0,
190 "Minimum size of kernel memory");
192 static u_long vm_kmem_size_max;
193 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RD, &vm_kmem_size_max, 0,
194 "Maximum size of kernel memory");
196 static u_int vm_kmem_size_scale;
197 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RD, &vm_kmem_size_scale, 0,
198 "Scale factor for kernel memory size");
201 * The malloc_mtx protects the kmemstatistics linked list.
203 struct mtx malloc_mtx;
205 #ifdef MALLOC_PROFILE
206 uint64_t krequests[KMEM_ZSIZE + 1];
208 static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
211 static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS);
214 * time_uptime of the last malloc(9) failure (induced or real).
216 static time_t t_malloc_fail;
219 * malloc(9) fault injection -- cause malloc failures every (n) mallocs when
220 * the caller specifies M_NOWAIT. If set to 0, no failures are caused.
222 #ifdef MALLOC_MAKE_FAILURES
223 SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
224 "Kernel malloc debugging options");
226 static int malloc_failure_rate;
227 static int malloc_nowait_count;
228 static int malloc_failure_count;
229 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW,
230 &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
231 TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate);
232 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
233 &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
237 malloc_last_fail(void)
240 return (time_uptime - t_malloc_fail);
244 * An allocation has succeeded -- update malloc type statistics for the
245 * amount of bucket size. Occurs within a critical section so that the
246 * thread isn't preempted and doesn't migrate while updating per-PCU
250 malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size,
253 struct malloc_type_internal *mtip;
254 struct malloc_type_stats *mtsp;
257 mtip = mtp->ks_handle;
258 mtsp = &mtip->mti_stats[curcpu];
260 mtsp->mts_memalloced += size;
261 mtsp->mts_numallocs++;
264 mtsp->mts_size |= 1 << zindx;
267 if (dtrace_malloc_probe != NULL) {
268 uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC];
270 (dtrace_malloc_probe)(probe_id,
271 (uintptr_t) mtp, (uintptr_t) mtip,
272 (uintptr_t) mtsp, size, zindx);
280 malloc_type_allocated(struct malloc_type *mtp, unsigned long size)
284 malloc_type_zone_allocated(mtp, size, -1);
288 * A free operation has occurred -- update malloc type statistics for the
289 * amount of the bucket size. Occurs within a critical section so that the
290 * thread isn't preempted and doesn't migrate while updating per-CPU
294 malloc_type_freed(struct malloc_type *mtp, unsigned long size)
296 struct malloc_type_internal *mtip;
297 struct malloc_type_stats *mtsp;
300 mtip = mtp->ks_handle;
301 mtsp = &mtip->mti_stats[curcpu];
302 mtsp->mts_memfreed += size;
303 mtsp->mts_numfrees++;
306 if (dtrace_malloc_probe != NULL) {
307 uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE];
309 (dtrace_malloc_probe)(probe_id,
310 (uintptr_t) mtp, (uintptr_t) mtip,
311 (uintptr_t) mtsp, size, 0);
321 * Allocate a block of memory.
323 * If M_NOWAIT is set, this routine will not block and return NULL if
324 * the allocation fails.
327 malloc(unsigned long size, struct malloc_type *mtp, int flags)
332 #if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE)
333 unsigned long osize = size;
337 KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic"));
339 * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
341 indx = flags & (M_WAITOK | M_NOWAIT);
342 if (indx != M_NOWAIT && indx != M_WAITOK) {
343 static struct timeval lasterr;
344 static int curerr, once;
345 if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
346 printf("Bad malloc flags: %x\n", indx);
353 #ifdef MALLOC_MAKE_FAILURES
354 if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
355 atomic_add_int(&malloc_nowait_count, 1);
356 if ((malloc_nowait_count % malloc_failure_rate) == 0) {
357 atomic_add_int(&malloc_failure_count, 1);
358 t_malloc_fail = time_uptime;
363 if (flags & M_WAITOK)
364 KASSERT(curthread->td_intr_nesting_level == 0,
365 ("malloc(M_WAITOK) in interrupt context"));
367 #ifdef DEBUG_MEMGUARD
368 if (memguard_cmp(mtp))
369 return memguard_alloc(size, flags);
373 size = redzone_size_ntor(size);
376 if (size <= KMEM_ZMAX) {
377 if (size & KMEM_ZMASK)
378 size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
379 indx = kmemsize[size >> KMEM_ZSHIFT];
380 zone = kmemzones[indx].kz_zone;
381 #ifdef MALLOC_PROFILE
382 krequests[size >> KMEM_ZSHIFT]++;
384 va = uma_zalloc(zone, flags);
386 size = zone->uz_size;
387 malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
389 size = roundup(size, PAGE_SIZE);
391 va = uma_large_malloc(size, flags);
392 malloc_type_allocated(mtp, va == NULL ? 0 : size);
394 if (flags & M_WAITOK)
395 KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
397 t_malloc_fail = time_uptime;
399 if (va != NULL && !(flags & M_ZERO)) {
400 memset(va, 0x70, osize);
405 va = redzone_setup(va, osize);
407 return ((void *) va);
413 * Free a block of memory allocated by malloc.
415 * This routine may not block.
418 free(void *addr, struct malloc_type *mtp)
423 KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic"));
425 /* free(NULL, ...) does nothing */
429 #ifdef DEBUG_MEMGUARD
430 if (memguard_cmp(mtp)) {
438 addr = redzone_addr_ntor(addr);
441 slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
444 panic("free: address %p(%p) has not been allocated.\n",
445 addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
448 if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
450 struct malloc_type **mtpp = addr;
452 size = slab->us_keg->uk_size;
455 * Cache a pointer to the malloc_type that most recently freed
456 * this memory here. This way we know who is most likely to
457 * have stepped on it later.
459 * This code assumes that size is a multiple of 8 bytes for
462 mtpp = (struct malloc_type **)
463 ((unsigned long)mtpp & ~UMA_ALIGN_PTR);
464 mtpp += (size - sizeof(struct malloc_type *)) /
465 sizeof(struct malloc_type *);
468 uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab);
470 size = slab->us_size;
471 uma_large_free(slab);
473 malloc_type_freed(mtp, size);
477 * realloc: change the size of a memory block
480 realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
486 KASSERT(mtp->ks_magic == M_MAGIC,
487 ("realloc: bad malloc type magic"));
489 /* realloc(NULL, ...) is equivalent to malloc(...) */
491 return (malloc(size, mtp, flags));
494 * XXX: Should report free of old memory and alloc of new memory to
498 #ifdef DEBUG_MEMGUARD
499 if (memguard_cmp(mtp)) {
507 alloc = redzone_get_size(addr);
509 slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
512 KASSERT(slab != NULL,
513 ("realloc: address %p out of range", (void *)addr));
515 /* Get the size of the original block */
516 if (!(slab->us_flags & UMA_SLAB_MALLOC))
517 alloc = slab->us_keg->uk_size;
519 alloc = slab->us_size;
521 /* Reuse the original block if appropriate */
523 && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
525 #endif /* !DEBUG_REDZONE */
527 #ifdef DEBUG_MEMGUARD
531 /* Allocate a new, bigger (or smaller) block */
532 if ((newaddr = malloc(size, mtp, flags)) == NULL)
535 /* Copy over original contents */
536 bcopy(addr, newaddr, min(size, alloc));
542 * reallocf: same as realloc() but free memory on failure.
545 reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
549 if ((mem = realloc(addr, size, mtp, flags)) == NULL)
555 * Initialize the kernel memory allocator
559 kmeminit(void *dummy)
565 mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
568 * Try to auto-tune the kernel memory size, so that it is
569 * more applicable for a wider range of machine sizes.
570 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
571 * a VM_KMEM_SIZE of 12MB is a fair compromise. The
572 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
573 * available, and on an X86 with a total KVA space of 256MB,
574 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
576 * Note that the kmem_map is also used by the zone allocator,
577 * so make sure that there is enough space.
579 vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE;
580 mem_size = cnt.v_page_count;
582 #if defined(VM_KMEM_SIZE_SCALE)
583 vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
585 TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale);
586 if (vm_kmem_size_scale > 0 &&
587 (mem_size / vm_kmem_size_scale) > (vm_kmem_size / PAGE_SIZE))
588 vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE;
590 #if defined(VM_KMEM_SIZE_MIN)
591 vm_kmem_size_min = VM_KMEM_SIZE_MIN;
593 TUNABLE_ULONG_FETCH("vm.kmem_size_min", &vm_kmem_size_min);
594 if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) {
595 vm_kmem_size = vm_kmem_size_min;
598 #if defined(VM_KMEM_SIZE_MAX)
599 vm_kmem_size_max = VM_KMEM_SIZE_MAX;
601 TUNABLE_ULONG_FETCH("vm.kmem_size_max", &vm_kmem_size_max);
602 if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
603 vm_kmem_size = vm_kmem_size_max;
605 /* Allow final override from the kernel environment */
606 TUNABLE_ULONG_FETCH("vm.kmem_size", &vm_kmem_size);
609 * Limit kmem virtual size to twice the physical memory.
610 * This allows for kmem map sparseness, but limits the size
611 * to something sane. Be careful to not overflow the 32bit
612 * ints while doing the check.
614 if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count)
615 vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
618 * Tune settings based on the kmem map's size at this time.
620 init_param3(vm_kmem_size / PAGE_SIZE);
622 kmem_map = kmem_suballoc(kernel_map, &kmembase, &kmemlimit,
624 kmem_map->system_map = 1;
626 #ifdef DEBUG_MEMGUARD
628 * Initialize MemGuard if support compiled in. MemGuard is a
629 * replacement allocator used for detecting tamper-after-free
630 * scenarios as they occur. It is only used for debugging.
632 vm_memguard_divisor = 10;
633 TUNABLE_INT_FETCH("vm.memguard.divisor", &vm_memguard_divisor);
635 /* Pick a conservative value if provided value sucks. */
636 if ((vm_memguard_divisor <= 0) ||
637 ((vm_kmem_size / vm_memguard_divisor) == 0))
638 vm_memguard_divisor = 10;
639 memguard_init(kmem_map, vm_kmem_size / vm_memguard_divisor);
644 mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal),
646 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
648 NULL, NULL, NULL, NULL,
650 UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
651 for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
652 int size = kmemzones[indx].kz_size;
653 char *name = kmemzones[indx].kz_name;
655 kmemzones[indx].kz_zone = uma_zcreate(name, size,
657 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
659 NULL, NULL, NULL, NULL,
661 UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
663 for (;i <= size; i+= KMEM_ZBASE)
664 kmemsize[i >> KMEM_ZSHIFT] = indx;
670 malloc_init(void *data)
672 struct malloc_type_internal *mtip;
673 struct malloc_type *mtp;
675 KASSERT(cnt.v_page_count != 0, ("malloc_register before vm_init"));
678 if (mtp->ks_magic != M_MAGIC)
679 panic("malloc_init: bad malloc type magic");
681 mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO);
682 mtp->ks_handle = mtip;
684 mtx_lock(&malloc_mtx);
685 mtp->ks_next = kmemstatistics;
686 kmemstatistics = mtp;
688 mtx_unlock(&malloc_mtx);
692 malloc_uninit(void *data)
694 struct malloc_type_internal *mtip;
695 struct malloc_type_stats *mtsp;
696 struct malloc_type *mtp, *temp;
698 long temp_allocs, temp_bytes;
702 KASSERT(mtp->ks_magic == M_MAGIC,
703 ("malloc_uninit: bad malloc type magic"));
704 KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL"));
706 mtx_lock(&malloc_mtx);
707 mtip = mtp->ks_handle;
708 mtp->ks_handle = NULL;
709 if (mtp != kmemstatistics) {
710 for (temp = kmemstatistics; temp != NULL;
711 temp = temp->ks_next) {
712 if (temp->ks_next == mtp) {
713 temp->ks_next = mtp->ks_next;
718 ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc));
720 kmemstatistics = mtp->ks_next;
722 mtx_unlock(&malloc_mtx);
725 * Look for memory leaks.
727 temp_allocs = temp_bytes = 0;
728 for (i = 0; i < MAXCPU; i++) {
729 mtsp = &mtip->mti_stats[i];
730 temp_allocs += mtsp->mts_numallocs;
731 temp_allocs -= mtsp->mts_numfrees;
732 temp_bytes += mtsp->mts_memalloced;
733 temp_bytes -= mtsp->mts_memfreed;
735 if (temp_allocs > 0 || temp_bytes > 0) {
736 printf("Warning: memory type %s leaked memory on destroy "
737 "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
738 temp_allocs, temp_bytes);
741 slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK));
742 uma_zfree_arg(mt_zone, mtip, slab);
746 malloc_desc2type(const char *desc)
748 struct malloc_type *mtp;
750 mtx_assert(&malloc_mtx, MA_OWNED);
751 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
752 if (strcmp(mtp->ks_shortdesc, desc) == 0)
759 sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS)
761 struct malloc_type_stream_header mtsh;
762 struct malloc_type_internal *mtip;
763 struct malloc_type_header mth;
764 struct malloc_type *mtp;
765 int buflen, count, error, i;
769 mtx_lock(&malloc_mtx);
771 mtx_assert(&malloc_mtx, MA_OWNED);
773 mtx_unlock(&malloc_mtx);
774 buflen = sizeof(mtsh) + count * (sizeof(mth) +
775 sizeof(struct malloc_type_stats) * MAXCPU) + 1;
776 buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
777 mtx_lock(&malloc_mtx);
778 if (count < kmemcount) {
779 free(buffer, M_TEMP);
783 sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
786 * Insert stream header.
788 bzero(&mtsh, sizeof(mtsh));
789 mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION;
790 mtsh.mtsh_maxcpus = MAXCPU;
791 mtsh.mtsh_count = kmemcount;
792 if (sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)) < 0) {
793 mtx_unlock(&malloc_mtx);
799 * Insert alternating sequence of type headers and type statistics.
801 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
802 mtip = (struct malloc_type_internal *)mtp->ks_handle;
805 * Insert type header.
807 bzero(&mth, sizeof(mth));
808 strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME);
809 if (sbuf_bcat(&sbuf, &mth, sizeof(mth)) < 0) {
810 mtx_unlock(&malloc_mtx);
816 * Insert type statistics for each CPU.
818 for (i = 0; i < MAXCPU; i++) {
819 if (sbuf_bcat(&sbuf, &mtip->mti_stats[i],
820 sizeof(mtip->mti_stats[i])) < 0) {
821 mtx_unlock(&malloc_mtx);
827 mtx_unlock(&malloc_mtx);
829 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
832 free(buffer, M_TEMP);
836 SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
837 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats",
838 "Return malloc types");
840 SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0,
841 "Count of kernel malloc types");
844 malloc_type_list(malloc_type_list_func_t *func, void *arg)
846 struct malloc_type *mtp, **bufmtp;
850 mtx_lock(&malloc_mtx);
852 mtx_assert(&malloc_mtx, MA_OWNED);
854 mtx_unlock(&malloc_mtx);
856 buflen = sizeof(struct malloc_type *) * count;
857 bufmtp = malloc(buflen, M_TEMP, M_WAITOK);
859 mtx_lock(&malloc_mtx);
861 if (count < kmemcount) {
862 free(bufmtp, M_TEMP);
866 for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++)
869 mtx_unlock(&malloc_mtx);
871 for (i = 0; i < count; i++)
872 (func)(bufmtp[i], arg);
874 free(bufmtp, M_TEMP);
878 DB_SHOW_COMMAND(malloc, db_show_malloc)
880 struct malloc_type_internal *mtip;
881 struct malloc_type *mtp;
882 u_int64_t allocs, frees;
883 u_int64_t alloced, freed;
886 db_printf("%18s %12s %12s %12s\n", "Type", "InUse", "MemUse",
888 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
889 mtip = (struct malloc_type_internal *)mtp->ks_handle;
894 for (i = 0; i < MAXCPU; i++) {
895 allocs += mtip->mti_stats[i].mts_numallocs;
896 frees += mtip->mti_stats[i].mts_numfrees;
897 alloced += mtip->mti_stats[i].mts_memalloced;
898 freed += mtip->mti_stats[i].mts_memfreed;
900 db_printf("%18s %12ju %12juK %12ju\n",
901 mtp->ks_shortdesc, allocs - frees,
902 (alloced - freed + 1023) / 1024, allocs);
907 #ifdef MALLOC_PROFILE
910 sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
924 bufsize = linesize * (KMEM_ZSIZE + 1);
925 bufsize += 128; /* For the stats line */
926 bufsize += 128; /* For the banner line */
930 buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
931 sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN);
933 "\n Size Requests Real Size\n");
934 for (i = 0; i < KMEM_ZSIZE; i++) {
935 size = i << KMEM_ZSHIFT;
936 rsize = kmemzones[kmemsize[i]].kz_size;
937 count = (long long unsigned)krequests[i];
939 sbuf_printf(&sbuf, "%6d%28llu%11d\n", size,
940 (unsigned long long)count, rsize);
942 if ((rsize * count) > (size * count))
943 waste += (rsize * count) - (size * count);
944 mem += (rsize * count);
947 "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
948 (unsigned long long)mem, (unsigned long long)waste);
951 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
958 SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
959 NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
960 #endif /* MALLOC_PROFILE */