10cddce49SRobert Watson.\" Copyright (c) 2005 Robert N. M. Watson 20cddce49SRobert Watson.\" All rights reserved. 30cddce49SRobert Watson.\" 40cddce49SRobert Watson.\" Redistribution and use in source and binary forms, with or without 50cddce49SRobert Watson.\" modification, are permitted provided that the following conditions 60cddce49SRobert Watson.\" are met: 70cddce49SRobert Watson.\" 1. Redistributions of source code must retain the above copyright 80cddce49SRobert Watson.\" notice, this list of conditions and the following disclaimer. 90cddce49SRobert Watson.\" 2. Redistributions in binary form must reproduce the above copyright 100cddce49SRobert Watson.\" notice, this list of conditions and the following disclaimer in the 110cddce49SRobert Watson.\" documentation and/or other materials provided with the distribution. 120cddce49SRobert Watson.\" 130cddce49SRobert Watson.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 140cddce49SRobert Watson.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 150cddce49SRobert Watson.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 160cddce49SRobert Watson.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 170cddce49SRobert Watson.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 180cddce49SRobert Watson.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 190cddce49SRobert Watson.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 200cddce49SRobert Watson.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 210cddce49SRobert Watson.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 220cddce49SRobert Watson.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 230cddce49SRobert Watson.\" SUCH DAMAGE. 240cddce49SRobert Watson.\" 250cddce49SRobert Watson.\" $FreeBSD$ 260cddce49SRobert Watson.\" 270cddce49SRobert Watson.Dd June 27, 2005 280cddce49SRobert Watson.Os 290cddce49SRobert Watson.Dt LIBMEMSTAT 3 300cddce49SRobert Watson.Sh NAME 310cddce49SRobert Watson.Nm libmemstat 320cddce49SRobert Watson.Nd "library interface to retrieve kernel memory allocator statistics" 330cddce49SRobert Watson.Sh LIBRARY 340cddce49SRobert Watson.Lb libmemstat 350cddce49SRobert Watson.Sh SYNOPSIS 360cddce49SRobert Watson.In sys/types.h 370cddce49SRobert Watson.In memstat.h 380cddce49SRobert Watson.Ss Memory Type List Management Functions 390cddce49SRobert Watson.Ft struct memory_type_list * 400cddce49SRobert Watson.Fn memstat_mtl_alloc "void" 410cddce49SRobert Watson.Ft struct memory_type * 420cddce49SRobert Watson.Fn memstat_mtl_first "struct memory_type_list *list" 430cddce49SRobert Watson.Ft struct memory_type * 440cddce49SRobert Watson.Fn memstat_mtl_next "struct memory_type *mtp" 450cddce49SRobert Watson.Ft struct memory_type * 460cddce49SRobert Watson.Fn memstat_mtl_find "struct memory_type_list *list" "int allocator" "const char *name" 470cddce49SRobert Watson.Ft void 480cddce49SRobert Watson.Fn memstat_mtl_free "struct memory_type_list *list" 490cddce49SRobert Watson.Ss Allocator Query Functions 500cddce49SRobert Watson.Ft int 510cddce49SRobert Watson.Fn memstat_sysctl_all "struct memory_type_list *list" "int flags" 520cddce49SRobert Watson.Ft int 530cddce49SRobert Watson.Fn memstat_sysctl_malloc "struct memory_type_list *list" "int flags" 540cddce49SRobert Watson.Ft int 550cddce49SRobert Watson.Fn memstat_sysctl_uma "struct memory_type_list *list" "int flags" 560cddce49SRobert Watson.Ss Memory Type Accessor Methods 570cddce49SRobert Watson.Ft const char * 580cddce49SRobert Watson.Fn memstat_get_name "const struct memory_type *mtp" 590cddce49SRobert Watson.Ft int 600cddce49SRobert Watson.Fn memstat_get_allocator "const struct memory_type *mtp" 610cddce49SRobert Watson.Ft uint64_t 620cddce49SRobert Watson.Fn memstat_get_countlimit "const struct memory_type *mtp" 630cddce49SRobert Watson.Ft uint64_t 640cddce49SRobert Watson.Fn memstat_get_byteslimit "const struct memory_type *mtp" 650cddce49SRobert Watson.Ft uint64_t 660cddce49SRobert Watson.Fn memstat_get_sizemask "const struct memory_type *mtp" 670cddce49SRobert Watson.Ft uint64_t 680cddce49SRobert Watson.Fn memstat_get_size "const struct memory_type *mtp" 690cddce49SRobert Watson.Ft uint64_t 700cddce49SRobert Watson.Fn memstat_get_memalloced "const struct memory_type *mtp" 710cddce49SRobert Watson.Ft uint64_t 720cddce49SRobert Watson.Fn memstat_get_memfreed "const struct memory_type *mtp" 730cddce49SRobert Watson.Ft uint64_t 740cddce49SRobert Watson.Fn memstat_get_numallocs "const struct memory_type *mtp" 750cddce49SRobert Watson.Ft uint64_t 760cddce49SRobert Watson.Fn memstat_get_numfrees "const struct memory_type *mtp" 770cddce49SRobert Watson.Ft uint64_t 780cddce49SRobert Watson.Fn memstat_get_bytes "const struct memory_type *mtp" 790cddce49SRobert Watson.Ft uint64_t 800cddce49SRobert Watson.Fn memstat_get_count "const struct memory_type *mtp" 810cddce49SRobert Watson.Ft uint64_t 820cddce49SRobert Watson.Fn memstat_get_free "const struct memory_type *mtp" 830cddce49SRobert Watson.Ft uint64_t 840cddce49SRobert Watson.Fn memstat_get_failures "const struct memory_type *mtp" 850cddce49SRobert Watson.Ft void * 860cddce49SRobert Watson.Fn memstat_get_caller_pointer "const struct memory_type *mtp" "int index" 870cddce49SRobert Watson.Ft void 880cddce49SRobert Watson.Fn memstat_set_caller_pointer "struct memory_type *mtp" "int index" "void *value" 890cddce49SRobert Watson.Ft uint64_t 900cddce49SRobert Watson.Fn memstat_get_caller_uint64 "const struct memory_type *mtp" "int index" 910cddce49SRobert Watson.Ft void 920cddce49SRobert Watson.Fn memstat_set_caller_uint64 "struct memory_type *mtp" "int index" "uint64_t value" 930cddce49SRobert Watson.Ft uint64_t 940cddce49SRobert Watson.Fn memstat_get_zonefree "const struct memory_type *mtp" 950cddce49SRobert Watson.Ft uint64_t 960cddce49SRobert Watson.Fn memstat_get_percpu_memalloced "const struct memory_type *mtp" "int cpu" 970cddce49SRobert Watson.Ft uint64_t 980cddce49SRobert Watson.Fn memstat_get_percpu_memfreed "const struct memory_type *mtp" "int cpu" 990cddce49SRobert Watson.Ft uint64_t 1000cddce49SRobert Watson.Fn memstat_get_percpu_numallocs "const struct memory_type *mtp" "int cpu" 1010cddce49SRobert Watson.Ft uint64_t 1020cddce49SRobert Watson.Fn memstat_get_percpu_numfrees "const struct memory_type *mtp" "int cpu" 1030cddce49SRobert Watson.Ft uint64_t 1040cddce49SRobert Watson.Fn memstat_get_percpu_sizemask "const struct memory_type *mtp" "int cpu" 1050cddce49SRobert Watson.Ft void * 1060cddce49SRobert Watson.Fn memstat_get_percpu_caller_pointer "const struct memory_type *mtp" "int cpu" "int index" 1070cddce49SRobert Watson.Ft void 1080cddce49SRobert Watson.Fn memstat_set_percpu_caller_pointer "struct memory_type *mtp" "int cpu" "int index" "void *value" 1090cddce49SRobert Watson.Ft uint64_t 1100cddce49SRobert Watson.Fn memstat_get_percpu_caller_uint64 "const struct memory_type *mtp" "int cpu" "int index" 1110cddce49SRobert Watson.Ft void 1120cddce49SRobert Watson.Fn memstat_set_percpu_caller_uint64 "struct memory_type *mtp" "int cpu" "int index" "uint64_t value" 1130cddce49SRobert Watson.Ft uint64_t 1140cddce49SRobert Watson.Fn memstat_get_percpu_free "const struct memory_type *mtp" "int cpu" 1150cddce49SRobert Watson.Sh DESCRIPTION 1160cddce49SRobert Watson.Nm 1170cddce49SRobert Watsonprovides an interface to retrieve kernel memory allocator statistics, for 1180cddce49SRobert Watsonthe purposes of debugging and system monitoring, insulating applications 1190cddce49SRobert Watsonfrom implementation details of the allocators, and allowing a tool to 1200cddce49SRobert Watsontransparently support multiple allocators. 1210cddce49SRobert Watson.Nm 1220cddce49SRobert Watsonsupports both retrieving a single statistics snapshot, as well as 1230cddce49SRobert Watsonincrementally updating statistics for long-term monitoring. 1240cddce49SRobert Watson.Pp 1250cddce49SRobert Watson.Nm 1260cddce49SRobert Watsondescribes each memory type using a 1270cddce49SRobert Watson.Vt struct memory_type , 1280cddce49SRobert Watsonan opaque memory type accessed by the application using accessor functions 1290cddce49SRobert Watsonin the library. 1300cddce49SRobert Watson.Nm 1310cddce49SRobert Watsonreturns and updates chains of 1320cddce49SRobert Watson.Vt struct memory_type 1330cddce49SRobert Watsonvia a 1340cddce49SRobert Watson.Vt struct memory_type_list , 1350cddce49SRobert Watsonwhich will be allocated by calling 1360cddce49SRobert Watson.Fn memstat_mtl_alloc , 1370cddce49SRobert Watsonand freed on completion using 1380cddce49SRobert Watson.Fn memstat_mtl_free . 1390cddce49SRobert WatsonLists of memory types are populated via calls that query the kernel for 1400cddce49SRobert Watsonstatistics information; currently: 1410cddce49SRobert Watson.Fn memstat_sysctl_all , 1420cddce49SRobert Watson.Fn memstat_sysctl_uma , 1430cddce49SRobert Watsonand 1440cddce49SRobert Watson.Fn memstat_sysctl_malloc . 1450cddce49SRobert WatsonRepeated calls will incrementally update the list of memory types, permitting 1460cddce49SRobert Watsontracking over time without recreating all list state. 1470cddce49SRobert WatsonFreeing the list will free all memory type data in the list, and so 1480cddce49SRobert Watsoninvalidates any outstanding pointers to entries in the list. 1490cddce49SRobert Watson.Vt struct memory_type 1500cddce49SRobert Watsonentries in the list may be iterated over using 1510cddce49SRobert Watson.Fn memstat_mtl_first 1520cddce49SRobert Watsonand 1530cddce49SRobert Watson.fn memstat_mtl_next , 1540cddce49SRobert Watsonwhich respectively return the first entry in a list, and the next entry in a 1550cddce49SRobert Watsonlist. 1560cddce49SRobert Watson.Fn memstat_mtl_find , 1570cddce49SRobert Watsonwhich will return a pointer to the first entry matching the passed 1580cddce49SRobert Watsonparameters. 1590cddce49SRobert Watson.Pp 1600cddce49SRobert WatsonA series of accessor methods is provided to access fields of the structure, 1610cddce49SRobert Watsonincluding retrieving statistics and properties, as well as setting of caller 1620cddce49SRobert Watsonowned fields. 1630cddce49SRobert WatsonDirect application access to the data structure fields is not supported. 1640cddce49SRobert Watson.Ss Library memory_type Fields 1650cddce49SRobert WatsonEach 1660cddce49SRobert Watson.Vt struct memory_type 1670cddce49SRobert Watsonholds a description of the memory type, including its name and the allocator 1680cddce49SRobert Watsonit is managed by, as well as current statistics on use. 1690cddce49SRobert WatsonSome statistics are directly measured, others are derived from directly 1700cddce49SRobert Watsonmeasured statistics. 1710cddce49SRobert WatsonCertain high level statistics are present across all available allocators, 1720cddce49SRobert Watsonsuch as the number of allocation and free operations; other measurements, 1730cddce49SRobert Watsonsuch as the quantity of free items in per-CPU caches, or administrative 1740cddce49SRobert Watsonlimit on the number of allocations, is available only for specific 1750cddce49SRobert Watsonallocators. 1760cddce49SRobert Watson.Ss User memory_type Fields 1770cddce49SRobert Watson.Vt struct memory_type 1780cddce49SRobert Watsonincludes fields appropriate for use by the application in order to more 1790cddce49SRobert Watsoneasily maintain state associated with memory types across updates. 1800cddce49SRobert WatsonFor example, the application author might make use of one of the caller 1810cddce49SRobert Watsonpointers to reference a more complex data structure tracking long-term 1820cddce49SRobert Watsonbehavior of the memory type, or a window system object that is used to 1830cddce49SRobert Watsonrender the state of the memory type. 1840cddce49SRobert WatsonQuery updates may reset or otherwise modify all other fields in the 1850cddce49SRobert Watson.Vt struct memory_type 1860cddce49SRobert Watsondata structure, but will preserve the caller-provided values, which will 1870cddce49SRobert Watsonbe initialized to 1880cddce49SRobert Watson.Dv 0 1890cddce49SRobert Watsonor 1900cddce49SRobert Watson.Dv NULL 1910cddce49SRobert Watsonbefore first use. 19209df0a9aSRobert Watson.Ss Allocator Types 19309df0a9aSRobert WatsonCurrently, 19409df0a9aSRobert Watson.Nm 19509df0a9aSRobert Watsonsupports two kernel allocators: 19609df0a9aSRobert Watson.Dv ALLOCATOR_UMA 19709df0a9aSRobert Watsonfor 19809df0a9aSRobert Watson.Xr uma 9 , 19909df0a9aSRobert Watsonand 20009df0a9aSRobert Watson.Dv ALLOCATOR_MALLOC 20109df0a9aSRobert Watsonfor 20209df0a9aSRobert Watson.Xr malloc 9 . 20309df0a9aSRobert WatsonThese values may be passed to 20409df0a9aSRobert Watson.Fn memstat_mtl_find , 20509df0a9aSRobert Watsonand will be returned by 20609df0a9aSRobert Watson.Fn memstat_get_allocator . 20709df0a9aSRobert WatsonTwo additional constants in the allocator name space are defined: 20809df0a9aSRobert Watson.Dv ALLOCATOR_UNKNOWN , 20909df0a9aSRobert Watsonwhich will only be returned as a result of a library error, and 21009df0a9aSRobert Watson.Dv ALLOCATOR_ANY , 21109df0a9aSRobert Watsonwhich can be used to specify that returning types matching any allocator is 21209df0a9aSRobert Watsonpermittible from 21309df0a9aSRobert Watson.Fn memstat_mtl_find . 2140cddce49SRobert Watson.Sh EXAMPLES 2150cddce49SRobert WatsonCreate a memory type list, query the 2160cddce49SRobert Watson.Xr uma 9 2170cddce49SRobert Watsonmemory allocator for available statistics, and print out the number of 2180cddce49SRobert Watsonallocations performed by the 2190cddce49SRobert Watson.Dv Mbuf 2200cddce49SRobert Watsonzone. 2210cddce49SRobert Watson.Bd -literal -offset indent 2220cddce49SRobert Watsonstruct memory_type_list *mtlp; 2230cddce49SRobert Watsonstruct memory_type *mtp; 2240cddce49SRobert Watsonuint64_t mbuf_count; 2250cddce49SRobert Watson 2260cddce49SRobert Watsonmtlp = memstat_mtl_alloc(); 2270cddce49SRobert Watsonif (mtlp == NULL) 2280cddce49SRobert Watson err(-1, "memstat_mtl_alloc"); 2290cddce49SRobert Watsonif (memstat_sysctl_uma(mtlp, 0) < 0) 2300cddce49SRobert Watson err(-1, "memstat_sysctl_uma"); 2310cddce49SRobert Watsonmtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, "Mbuf"); 2320cddce49SRobert Watsonif (mtp == NULL) 2330cddce49SRobert Watson errx(-1, "memstat_mtl_find: Mbuf not found"); 2340cddce49SRobert Watsonmbuf_count = memstat_get_count(mtp); 2350cddce49SRobert Watsonmemstat_mtl_free(mtlp); 2360cddce49SRobert Watson 2370cddce49SRobert Watsonprintf("Mbufs: %llu\\n", (unsigned long long)mbuf_count); 2380cddce49SRobert Watson.Ed 2390cddce49SRobert Watson.Sh SEE ALSO 2400cddce49SRobert Watson.Xr malloc 9 , 2410cddce49SRobert Watson.Xr uma 9 2420cddce49SRobert Watson.Sh HISTORY 2430cddce49SRobert WatsonThe 2440cddce49SRobert Watson.Nm memstat 2450cddce49SRobert Watsonlibrary appeared in 2460cddce49SRobert Watson.Fx 6.0 . 2470cddce49SRobert Watson.Sh AUTHORS 2480cddce49SRobert WatsonThe kernel memory allocator changes necessary to support a general purpose 2490cddce49SRobert Watsonmonitoring library, along with the library, were written by 2500cddce49SRobert Watson.An Robert Watson Aq rwatson@FreeBSD.org 2510cddce49SRobert Watson.Sh BUGS 2520cddce49SRobert Watson.Nm 2530cddce49SRobert Watsoncannot yet extract statistics from kernel core dumps, although this should be 2540cddce49SRobert Watsonstraight forward to implement. 2550cddce49SRobert Watson.Pp 2560cddce49SRobert WatsonOnce a memory type is present on a memory type list, it will not be removed 2570cddce49SRobert Watsoneven if the kernel no longer presents information on the type via its 2580cddce49SRobert Watsonmonitoring interfaces. 2590cddce49SRobert WatsonIn order to flush removed memory types, it is necessary to free the entire 2600cddce49SRobert Watsonlist and allocate a new one. 261