The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_malloc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 6243ed21b92af913593a5981e1d54fd5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.