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 38e754c6bbSRobert Watson.Ss General Functions 39e754c6bbSRobert Watson.Ft const char * 40e754c6bbSRobert Watson.Fn memstat_strerror "int error" 410cddce49SRobert Watson.Ss Memory Type List Management Functions 420cddce49SRobert Watson.Ft struct memory_type_list * 430cddce49SRobert Watson.Fn memstat_mtl_alloc "void" 440cddce49SRobert Watson.Ft struct memory_type * 450cddce49SRobert Watson.Fn memstat_mtl_first "struct memory_type_list *list" 460cddce49SRobert Watson.Ft struct memory_type * 470cddce49SRobert Watson.Fn memstat_mtl_next "struct memory_type *mtp" 480cddce49SRobert Watson.Ft struct memory_type * 490cddce49SRobert Watson.Fn memstat_mtl_find "struct memory_type_list *list" "int allocator" "const char *name" 500cddce49SRobert Watson.Ft void 510cddce49SRobert Watson.Fn memstat_mtl_free "struct memory_type_list *list" 5270ee997eSRobert Watson.Ft int 5370ee997eSRobert Watson.Fn memstat_mtl_geterror "struct memory_type_list *list" 540cddce49SRobert Watson.Ss Allocator Query Functions 550cddce49SRobert Watson.Ft int 5633c20d18SRobert Watson.Fn memstat_kvm_uma "struct memory_type_list *list" "void *kvm_handle" 5733c20d18SRobert Watson.Ft int 580cddce49SRobert Watson.Fn memstat_sysctl_all "struct memory_type_list *list" "int flags" 590cddce49SRobert Watson.Ft int 600cddce49SRobert Watson.Fn memstat_sysctl_malloc "struct memory_type_list *list" "int flags" 610cddce49SRobert Watson.Ft int 620cddce49SRobert Watson.Fn memstat_sysctl_uma "struct memory_type_list *list" "int flags" 630cddce49SRobert Watson.Ss Memory Type Accessor Methods 640cddce49SRobert Watson.Ft const char * 650cddce49SRobert Watson.Fn memstat_get_name "const struct memory_type *mtp" 660cddce49SRobert Watson.Ft int 670cddce49SRobert Watson.Fn memstat_get_allocator "const struct memory_type *mtp" 680cddce49SRobert Watson.Ft uint64_t 690cddce49SRobert Watson.Fn memstat_get_countlimit "const struct memory_type *mtp" 700cddce49SRobert Watson.Ft uint64_t 710cddce49SRobert Watson.Fn memstat_get_byteslimit "const struct memory_type *mtp" 720cddce49SRobert Watson.Ft uint64_t 730cddce49SRobert Watson.Fn memstat_get_sizemask "const struct memory_type *mtp" 740cddce49SRobert Watson.Ft uint64_t 750cddce49SRobert Watson.Fn memstat_get_size "const struct memory_type *mtp" 760cddce49SRobert Watson.Ft uint64_t 770cddce49SRobert Watson.Fn memstat_get_memalloced "const struct memory_type *mtp" 780cddce49SRobert Watson.Ft uint64_t 790cddce49SRobert Watson.Fn memstat_get_memfreed "const struct memory_type *mtp" 800cddce49SRobert Watson.Ft uint64_t 810cddce49SRobert Watson.Fn memstat_get_numallocs "const struct memory_type *mtp" 820cddce49SRobert Watson.Ft uint64_t 830cddce49SRobert Watson.Fn memstat_get_numfrees "const struct memory_type *mtp" 840cddce49SRobert Watson.Ft uint64_t 850cddce49SRobert Watson.Fn memstat_get_bytes "const struct memory_type *mtp" 860cddce49SRobert Watson.Ft uint64_t 870cddce49SRobert Watson.Fn memstat_get_count "const struct memory_type *mtp" 880cddce49SRobert Watson.Ft uint64_t 890cddce49SRobert Watson.Fn memstat_get_free "const struct memory_type *mtp" 900cddce49SRobert Watson.Ft uint64_t 910cddce49SRobert Watson.Fn memstat_get_failures "const struct memory_type *mtp" 920cddce49SRobert Watson.Ft void * 930cddce49SRobert Watson.Fn memstat_get_caller_pointer "const struct memory_type *mtp" "int index" 940cddce49SRobert Watson.Ft void 950cddce49SRobert Watson.Fn memstat_set_caller_pointer "struct memory_type *mtp" "int index" "void *value" 960cddce49SRobert Watson.Ft uint64_t 970cddce49SRobert Watson.Fn memstat_get_caller_uint64 "const struct memory_type *mtp" "int index" 980cddce49SRobert Watson.Ft void 990cddce49SRobert Watson.Fn memstat_set_caller_uint64 "struct memory_type *mtp" "int index" "uint64_t value" 1000cddce49SRobert Watson.Ft uint64_t 1010cddce49SRobert Watson.Fn memstat_get_zonefree "const struct memory_type *mtp" 1020cddce49SRobert Watson.Ft uint64_t 103ca108fe2SRobert Watson.Fn memstat_get_kegfree "const struct memory_type *mtp" 104ca108fe2SRobert Watson.Ft uint64_t 1050cddce49SRobert Watson.Fn memstat_get_percpu_memalloced "const struct memory_type *mtp" "int cpu" 1060cddce49SRobert Watson.Ft uint64_t 1070cddce49SRobert Watson.Fn memstat_get_percpu_memfreed "const struct memory_type *mtp" "int cpu" 1080cddce49SRobert Watson.Ft uint64_t 1090cddce49SRobert Watson.Fn memstat_get_percpu_numallocs "const struct memory_type *mtp" "int cpu" 1100cddce49SRobert Watson.Ft uint64_t 1110cddce49SRobert Watson.Fn memstat_get_percpu_numfrees "const struct memory_type *mtp" "int cpu" 1120cddce49SRobert Watson.Ft uint64_t 1130cddce49SRobert Watson.Fn memstat_get_percpu_sizemask "const struct memory_type *mtp" "int cpu" 1140cddce49SRobert Watson.Ft void * 1150cddce49SRobert Watson.Fn memstat_get_percpu_caller_pointer "const struct memory_type *mtp" "int cpu" "int index" 1160cddce49SRobert Watson.Ft void 1170cddce49SRobert Watson.Fn memstat_set_percpu_caller_pointer "struct memory_type *mtp" "int cpu" "int index" "void *value" 1180cddce49SRobert Watson.Ft uint64_t 1190cddce49SRobert Watson.Fn memstat_get_percpu_caller_uint64 "const struct memory_type *mtp" "int cpu" "int index" 1200cddce49SRobert Watson.Ft void 1210cddce49SRobert Watson.Fn memstat_set_percpu_caller_uint64 "struct memory_type *mtp" "int cpu" "int index" "uint64_t value" 1220cddce49SRobert Watson.Ft uint64_t 1230cddce49SRobert Watson.Fn memstat_get_percpu_free "const struct memory_type *mtp" "int cpu" 1240cddce49SRobert Watson.Sh DESCRIPTION 1250cddce49SRobert Watson.Nm 1260cddce49SRobert Watsonprovides an interface to retrieve kernel memory allocator statistics, for 1270cddce49SRobert Watsonthe purposes of debugging and system monitoring, insulating applications 1280cddce49SRobert Watsonfrom implementation details of the allocators, and allowing a tool to 1290cddce49SRobert Watsontransparently support multiple allocators. 1300cddce49SRobert Watson.Nm 1310cddce49SRobert Watsonsupports both retrieving a single statistics snapshot, as well as 1320cddce49SRobert Watsonincrementally updating statistics for long-term monitoring. 1330cddce49SRobert Watson.Pp 1340cddce49SRobert Watson.Nm 1350cddce49SRobert Watsondescribes each memory type using a 1360cddce49SRobert Watson.Vt struct memory_type , 1370cddce49SRobert Watsonan opaque memory type accessed by the application using accessor functions 1380cddce49SRobert Watsonin the library. 1390cddce49SRobert Watson.Nm 1400cddce49SRobert Watsonreturns and updates chains of 1410cddce49SRobert Watson.Vt struct memory_type 1420cddce49SRobert Watsonvia a 1430cddce49SRobert Watson.Vt struct memory_type_list , 1440cddce49SRobert Watsonwhich will be allocated by calling 1450cddce49SRobert Watson.Fn memstat_mtl_alloc , 1460cddce49SRobert Watsonand freed on completion using 1470cddce49SRobert Watson.Fn memstat_mtl_free . 1480cddce49SRobert WatsonLists of memory types are populated via calls that query the kernel for 1490cddce49SRobert Watsonstatistics information; currently: 15033c20d18SRobert Watson.Fn memstat_kvm_uma , 1510cddce49SRobert Watson.Fn memstat_sysctl_all , 1520cddce49SRobert Watson.Fn memstat_sysctl_uma , 1530cddce49SRobert Watsonand 1540cddce49SRobert Watson.Fn memstat_sysctl_malloc . 1550cddce49SRobert WatsonRepeated calls will incrementally update the list of memory types, permitting 1560cddce49SRobert Watsontracking over time without recreating all list state. 15770ee997eSRobert WatsonIf an error is detected during a query call, error condition information may 15870ee997eSRobert Watsonbe retrieved using 159e754c6bbSRobert Watson.Fn memstat_mtl_geterror , 160e754c6bbSRobert Watsonand converted to a user-readable string using 161e754c6bbSRobert Watson.Fn memstat_strerror . 16270ee997eSRobert Watson.Pp 1630cddce49SRobert WatsonFreeing the list will free all memory type data in the list, and so 1640cddce49SRobert Watsoninvalidates any outstanding pointers to entries in the list. 1650cddce49SRobert Watson.Vt struct memory_type 1660cddce49SRobert Watsonentries in the list may be iterated over using 1670cddce49SRobert Watson.Fn memstat_mtl_first 1680cddce49SRobert Watsonand 1690cddce49SRobert Watson.fn memstat_mtl_next , 1700cddce49SRobert Watsonwhich respectively return the first entry in a list, and the next entry in a 1710cddce49SRobert Watsonlist. 1720cddce49SRobert Watson.Fn memstat_mtl_find , 1730cddce49SRobert Watsonwhich will return a pointer to the first entry matching the passed 1740cddce49SRobert Watsonparameters. 1750cddce49SRobert Watson.Pp 1760cddce49SRobert WatsonA series of accessor methods is provided to access fields of the structure, 1770cddce49SRobert Watsonincluding retrieving statistics and properties, as well as setting of caller 1780cddce49SRobert Watsonowned fields. 1790cddce49SRobert WatsonDirect application access to the data structure fields is not supported. 1800cddce49SRobert Watson.Ss Library memory_type Fields 1810cddce49SRobert WatsonEach 1820cddce49SRobert Watson.Vt struct memory_type 1830cddce49SRobert Watsonholds a description of the memory type, including its name and the allocator 1840cddce49SRobert Watsonit is managed by, as well as current statistics on use. 1850cddce49SRobert WatsonSome statistics are directly measured, others are derived from directly 1860cddce49SRobert Watsonmeasured statistics. 1870cddce49SRobert WatsonCertain high level statistics are present across all available allocators, 1880cddce49SRobert Watsonsuch as the number of allocation and free operations; other measurements, 1890cddce49SRobert Watsonsuch as the quantity of free items in per-CPU caches, or administrative 1900cddce49SRobert Watsonlimit on the number of allocations, is available only for specific 1910cddce49SRobert Watsonallocators. 192e86bb57bSRobert Watson.Ss Caller memory_type Fields 1930cddce49SRobert Watson.Vt struct memory_type 194e86bb57bSRobert Watsonincludes fields to allow the application to store data, in the form of 195e86bb57bSRobert Watsonpointers and 64-bit integers, with memory types. 1960cddce49SRobert WatsonFor example, the application author might make use of one of the caller 1970cddce49SRobert Watsonpointers to reference a more complex data structure tracking long-term 1980cddce49SRobert Watsonbehavior of the memory type, or a window system object that is used to 1990cddce49SRobert Watsonrender the state of the memory type. 200e86bb57bSRobert WatsonGeneral and per-CPU storage is provided with each 2010cddce49SRobert Watson.Vt struct memory_type 202e86bb57bSRobert Watsonin the form of an array of pointers and integers. 203e86bb57bSRobert WatsonThe array entries are accessed via the 204e86bb57bSRobert Watson.Fa index 205e86bb57bSRobert Watsonargument to the get and set accessor methods. 206e86bb57bSRobert WatsonPossible values of 207e86bb57bSRobert Watson.Fa index 208e86bb57bSRobert Watsonrange between 209e86bb57bSRobert Watson.Dv 0 210e86bb57bSRobert Watsonand 211e86bb57bSRobert Watson.Dv MEMSTAT_MAXCALLER . 212e86bb57bSRobert Watson.Pp 213e86bb57bSRobert WatsonCaller-owned fields are initialized to 2140cddce49SRobert Watson.Dv 0 2150cddce49SRobert Watsonor 2160cddce49SRobert Watson.Dv NULL 217e86bb57bSRobert Watsonwhen a new 218e86bb57bSRobert Watson.Vt struct memory_type 219e86bb57bSRobert Watsonis allocated and attached to a memory type list; these fields retain their 220e86bb57bSRobert Watsonvalues across queries that update library-owned fields. 22109df0a9aSRobert Watson.Ss Allocator Types 22209df0a9aSRobert WatsonCurrently, 22309df0a9aSRobert Watson.Nm 22409df0a9aSRobert Watsonsupports two kernel allocators: 22509df0a9aSRobert Watson.Dv ALLOCATOR_UMA 22609df0a9aSRobert Watsonfor 22709df0a9aSRobert Watson.Xr uma 9 , 22809df0a9aSRobert Watsonand 22909df0a9aSRobert Watson.Dv ALLOCATOR_MALLOC 23009df0a9aSRobert Watsonfor 23109df0a9aSRobert Watson.Xr malloc 9 . 23209df0a9aSRobert WatsonThese values may be passed to 23309df0a9aSRobert Watson.Fn memstat_mtl_find , 23409df0a9aSRobert Watsonand will be returned by 23509df0a9aSRobert Watson.Fn memstat_get_allocator . 23609df0a9aSRobert WatsonTwo additional constants in the allocator name space are defined: 23709df0a9aSRobert Watson.Dv ALLOCATOR_UNKNOWN , 23809df0a9aSRobert Watsonwhich will only be returned as a result of a library error, and 23909df0a9aSRobert Watson.Dv ALLOCATOR_ANY , 24009df0a9aSRobert Watsonwhich can be used to specify that returning types matching any allocator is 24109df0a9aSRobert Watsonpermittible from 24209df0a9aSRobert Watson.Fn memstat_mtl_find . 24370ee997eSRobert Watson.Ss Access Method List 24470ee997eSRobert WatsonThe following accessor methods are defined, of which some will be valid for 24570ee997eSRobert Watsona given memory type: 24670ee997eSRobert Watson.Pp 24770ee997eSRobert Watson.Bl -tag -width "memstat_get_name" -compact -offset wee 24870ee997eSRobert Watson.It memstat_get_name 24970ee997eSRobert WatsonReturn a pointer to the name of the memory type. 25070ee997eSRobert WatsonMemory for the name is owned by 25170ee997eSRobert Watson.Nm 25270ee997eSRobert Watsonand will be valid through a call to 25370ee997eSRobert Watson.Fn memstat_mtl_free . 25470ee997eSRobert WatsonNote that names will be unique with respect to a single allocator, but that 25570ee997eSRobert Watsonthe same name might be used by different memory types owned by different 25670ee997eSRobert Watsonmemory allocators. 25770ee997eSRobert Watson.It memstat_get_allocator 25870ee997eSRobert WatsonReturn an integer identifier for the memory allocator that owns the memory 25970ee997eSRobert Watsontype. 26070ee997eSRobert Watson.It memstat_get_countlimit 26170ee997eSRobert WatsonIf the memory type has an administrative limit on the number of simultaneous 26270ee997eSRobert Watsonallocations, return it. 26370ee997eSRobert Watson.It memstat_get_byteslimit 26470ee997eSRobert WatsonIf the memory type has an administrative limit on the number of bytes of 26570ee997eSRobert Watsonmemory that may be simultaenously allocated for the memory type, return it. 26670ee997eSRobert Watson.It memstat_get_sizemask 26770ee997eSRobert WatsonIf the memory type supports variable allocation sizes, return a bitmask of 26870ee997eSRobert Watsonsizes allocated for the memory type. 26970ee997eSRobert Watson.It memstat_get_size 27070ee997eSRobert WatsonIf the memory type supports a fixed allocation size, return that size. 27170ee997eSRobert Watson.It memstat_get_memalloced 27270ee997eSRobert WatsonReturn the total number of bytes allocated for the memory type over its 27370ee997eSRobert Watsonlifetime. 27470ee997eSRobert Watson.It memstat_get_memfreed 27570ee997eSRobert WatsonReturn the total number of bytes freed for the memory type over its lifetime. 27670ee997eSRobert Watson.It memstat_get_numallocs 27770ee997eSRobert WatsonReturn the total number of allocations for the memory type over its lifetime. 27870ee997eSRobert Watson.It memstat_get_numfrees 27970ee997eSRobert WatsonReturn the total number of frees for the memory type over its lifetime. 28070ee997eSRobert Watson.It memstat_get_bytes 28170ee997eSRobert WatsonReturn the current number of bytes allocated to the memory type. 28270ee997eSRobert Watson.It memstat_get_count 28370ee997eSRobert WatsonReturn the current number of allocations for the memory type. 28470ee997eSRobert Watson.It memstat_get_free 28570ee997eSRobert WatsonIf the memory allocator supports a cache, return the number of items in the 28670ee997eSRobert Watsoncache. 28770ee997eSRobert Watson.It memstat_get_failures 28870ee997eSRobert WatsonIf the memory allocator and type permit allocation failures, return the 28970ee997eSRobert Watsonnumber of allocation failures measured. 29070ee997eSRobert Watson.It memstat_get_caller_pointer 29170ee997eSRobert WatsonReturn a caller-owned pointer for the memory type. 29270ee997eSRobert Watson.It memstat_set_caller_pointer 29370ee997eSRobert WatsonSet a caller-owned pointer for the memory type. 29470ee997eSRobert Watson.It memstat_get_caller_uint64 29570ee997eSRobert WatsonReturn a caller-owned integer for the memory type. 29670ee997eSRobert Watson.It memstat_set_caller_uint64 29770ee997eSRobert WatsonSet a caller-owned integer for the memory type. 29870ee997eSRobert Watson.It memstat_get_zonefree 29970ee997eSRobert WatsonIf the memory allocator supports a multi-level allocation structure, return 30070ee997eSRobert Watsonthe number of cached items in the zone. 30170ee997eSRobert WatsonThese items will be in a fully constructed state available for immediate 30270ee997eSRobert Watsonuse. 30370ee997eSRobert Watson.It memstat_get_kegfree 30470ee997eSRobert WatsonIf the memory allocator supports a multi-level allocation structure, return 30570ee997eSRobert Watsonthe number of cached items in the keg. 30670ee997eSRobert WatsonThese items may be in a partially constructed state, and may require further 30770ee997eSRobert Watsonprocessing before they can be made available for use. 30870ee997eSRobert Watson.It memstat_get_percpu_memalloced 30970ee997eSRobert WatsonIf the memory allocator supports per-CPU statistics, return the number of 31070ee997eSRobert Watsonbytes of memory allocated for the memory type on the CPU over its lifetime. 31170ee997eSRobert Watson.It memstat_get_percpu_memfreed 31270ee997eSRobert WatsonIf the memory allocator supports per-CPU statistics, return the number of 31370ee997eSRobert Watsonbytes of memory freed from the memory type on the CPU over its lifetime. 31470ee997eSRobert Watson.It memstat_get_percpu_numallocs 31570ee997eSRobert WatsonIf the memory allocator supports per-CPU statistics, return the number of 31670ee997eSRobert Watsonallocations for the memory type on the CPU over its lifetime. 31770ee997eSRobert Watson.It memstat_get_percpu_numfrees 31870ee997eSRobert WatsonIf the memory allocator supports per-CPU statistics, return the number of 31970ee997eSRobert Watsonfrees for the memory type on the CPU over its lifetime. 32070ee997eSRobert Watson.It memstat_get_percpu_sizemask 32170ee997eSRobert WatsonIf the memory allocator supports variable size memory allocation and per-CPU 32270ee997eSRobert Watsonstatistics, return the size bitmask for the memory type on the CPU. 32370ee997eSRobert Watson.It memstat_get_percpu_caller_pointer 32470ee997eSRobert WatsonReturn a caller-owned per-CPU pointer for the memory type. 32570ee997eSRobert Watson.It memstat_set_percpu_caller_pointer 32670ee997eSRobert WatsonSet a caller-owned per-CPU pointer for the memory type. 32770ee997eSRobert Watson.It memstat_get_percpu_caller_uint64 32870ee997eSRobert WatsonReturn a caller-owned per-CPU integer for the memory type. 32970ee997eSRobert Watson.It memsttat_set_percpu_caller_uint64 33070ee997eSRobert WatsonSet a caller-owned per-CPU integer for the memory type. 33170ee997eSRobert Watson.It memstat_get_percpu_free 33270ee997eSRobert WatsonIf the memory allocator supports a per-CPU cache, return the number of free 33370ee997eSRobert Watsonitems in the per-CPU cache of the designated CPU. 33470ee997eSRobert Watson.El 33570ee997eSRobert Watson.Sh RETURN VALUES 33670ee997eSRobert Watson.Nm 33770ee997eSRobert Watsonfunctions fall into three categories: functions returning a pointer to an 33870ee997eSRobert Watsonobject, functions returning an integer return value, and functions 33970ee997eSRobert Watsonimplementing accessor methods returning data from a 34070ee997eSRobert Watson.Vt struct memory_type . 34170ee997eSRobert Watson.Pp 34270ee997eSRobert WatsonFunctions returning a pointer to an object will generally return 34370ee997eSRobert Watson.Dv NULL 34470ee997eSRobert Watsonon failure. 34570ee997eSRobert Watson.Fn memstat_mtl_alloc 34670ee997eSRobert Watsonwill return an error value via 34770ee997eSRobert Watson.Va errno , 34870ee997eSRobert Watsonwhich will consist of the value 34970ee997eSRobert Watson.Dv ENOMEM . 35070ee997eSRobert WatsonFunctions 35170ee997eSRobert Watson.Fn memstat_mtl_first , 35270ee997eSRobert Watson.Fn memstat_mtl_next , 35370ee997eSRobert Watsonand 35470ee997eSRobert Watson.Fn memstat_mtl_find 35570ee997eSRobert Watsonwill return 35670ee997eSRobert Watson.Dv NULL 35770ee997eSRobert Watsonwhen there is no entry or match in the list; however, this is not considered 35870ee997eSRobert Watsona failure mode and no error value is available. 35970ee997eSRobert Watson.Pp 36070ee997eSRobert WatsonFunctions returning a integer success valuye will return 36170ee997eSRobert Watson.Dv 0 36270ee997eSRobert Watsonon success, or 36370ee997eSRobert Watson.Dv -1 36470ee997eSRobert Watsonon failure. 36570ee997eSRobert WatsonIf a failure is returned, the list error access method, 36670ee997eSRobert Watson.Fn memstat_mtl_geterror , 367e754c6bbSRobert Watsonmay be used to retrieve the error state. 368e754c6bbSRobert WatsonThe string representation of the error may be retrieved using 369e754c6bbSRobert Watson.Fn memstat_strerror . 370e754c6bbSRobert WatsonPossible error values are: 37170ee997eSRobert Watson.Pp 37270ee997eSRobert Watson.Bl -tag -width "MEMSTAT_ERROR_TOOMANYCPUS" -compact -offset wee 37370ee997eSRobert Watson.It Dv MEMSTAT_ERROR_UNDEFINED 37470ee997eSRobert WatsonUndefined error. Occurs if 37570ee997eSRobert Watson.Fn memstat_mtl_geterror 37670ee997eSRobert Watsonis called on a list before an error associated with the list has occurred. 37770ee997eSRobert Watson.It Dv MEMSTAT_ERROR_NOMEMORY 37870ee997eSRobert WatsonInsufficient memory. Occurs if library calls to 37970ee997eSRobert Watson.Xr malloc 3 38070ee997eSRobert Watsonfail, or if a system call to retrieve kernel statistics fails with 38170ee997eSRobert Watson.Er ENOMEM . 38270ee997eSRobert Watson.It Dv MEMSTAT_ERROR_VERSION 38370ee997eSRobert WatsonReturned if the current version of 38470ee997eSRobert Watson.Nm 38570ee997eSRobert Watsonis unable to interpret the statistics data returned by the kernel due to an 38670ee997eSRobert Watsonexplicit version mismatch, or to differences in data structures that cannot 38770ee997eSRobert Watsonbe reconciled. 38870ee997eSRobert Watson.It Dv MEMSTAT_ERROR_PERMISSION 38970ee997eSRobert WatsonReturned if a statistics source returns 39070ee997eSRobert Watson.Va errno 39170ee997eSRobert Watsonvalues of 39270ee997eSRobert Watson.Dv EACCES 39370ee997eSRobert Watsonor 39470ee997eSRobert Watson.Dv EPERM . 39570ee997eSRobert Watson.It Dv MEMSTAT_ERROR_TOOMANYCPUS 39670ee997eSRobert WatsonReturned if the compile-time limit on the number of CPUs in 39770ee997eSRobert Watson.Nm 39870ee997eSRobert Watsonis lower than the number of CPUs returned by a statistics data source. 39970ee997eSRobert Watson.It Dv MEMSTAT_ERROR_DATAERROR 40070ee997eSRobert WatsonReturned if 40170ee997eSRobert Watson.Nm 40270ee997eSRobert Watsonis unable to interpret statistics data returned by the data source, even 40370ee997eSRobert Watsonthough there does not appear to be a version problem. 40470ee997eSRobert Watson.El 40570ee997eSRobert Watson.Pp 40670ee997eSRobert WatsonFinally, functions returning data from a 40770ee997eSRobert Watson.Dt struct memory_type 40870ee997eSRobert Watsonpointer are not permitted to fail, and directly return either a statistic 40970ee997eSRobert Watsonor pointer to a string. 4100cddce49SRobert Watson.Sh EXAMPLES 4110cddce49SRobert WatsonCreate a memory type list, query the 4120cddce49SRobert Watson.Xr uma 9 4130cddce49SRobert Watsonmemory allocator for available statistics, and print out the number of 4140cddce49SRobert Watsonallocations performed by the 4150cddce49SRobert Watson.Dv Mbuf 4160cddce49SRobert Watsonzone. 4170cddce49SRobert Watson.Bd -literal -offset indent 4180cddce49SRobert Watsonstruct memory_type_list *mtlp; 4190cddce49SRobert Watsonstruct memory_type *mtp; 4200cddce49SRobert Watsonuint64_t mbuf_count; 4210cddce49SRobert Watson 4220cddce49SRobert Watsonmtlp = memstat_mtl_alloc(); 4230cddce49SRobert Watsonif (mtlp == NULL) 4240cddce49SRobert Watson err(-1, "memstat_mtl_alloc"); 4250cddce49SRobert Watsonif (memstat_sysctl_uma(mtlp, 0) < 0) 4260cddce49SRobert Watson err(-1, "memstat_sysctl_uma"); 4270cddce49SRobert Watsonmtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, "Mbuf"); 4280cddce49SRobert Watsonif (mtp == NULL) 4290cddce49SRobert Watson errx(-1, "memstat_mtl_find: Mbuf not found"); 4300cddce49SRobert Watsonmbuf_count = memstat_get_count(mtp); 4310cddce49SRobert Watsonmemstat_mtl_free(mtlp); 4320cddce49SRobert Watson 4330cddce49SRobert Watsonprintf("Mbufs: %llu\\n", (unsigned long long)mbuf_count); 4340cddce49SRobert Watson.Ed 4350cddce49SRobert Watson.Sh SEE ALSO 4360cddce49SRobert Watson.Xr malloc 9 , 4370cddce49SRobert Watson.Xr uma 9 4380cddce49SRobert Watson.Sh HISTORY 4390cddce49SRobert WatsonThe 440e754c6bbSRobert Watson.Nm libmemstat 4410cddce49SRobert Watsonlibrary appeared in 4420cddce49SRobert Watson.Fx 6.0 . 4430cddce49SRobert Watson.Sh AUTHORS 4440cddce49SRobert WatsonThe kernel memory allocator changes necessary to support a general purpose 4450cddce49SRobert Watsonmonitoring library, along with the library, were written by 4460cddce49SRobert Watson.An Robert Watson Aq rwatson@FreeBSD.org 4470cddce49SRobert Watson.Sh BUGS 4480cddce49SRobert Watson.Nm 44933c20d18SRobert Watsoncannot yet extract 45033c20d18SRobert Watson.Xr malloc 9 45133c20d18SRobert Watsonstatistics from kernel core dumps, although this should be straight forward 45233c20d18SRobert Watsonto implement. 4530cddce49SRobert Watson.Pp 4540cddce49SRobert WatsonOnce a memory type is present on a memory type list, it will not be removed 4550cddce49SRobert Watsoneven if the kernel no longer presents information on the type via its 4560cddce49SRobert Watsonmonitoring interfaces. 4570cddce49SRobert WatsonIn order to flush removed memory types, it is necessary to free the entire 4580cddce49SRobert Watsonlist and allocate a new one. 459