xref: /freebsd/lib/libmemstat/libmemstat.3 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
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.\"
25345e3f4dSGleb Smirnoff.Dd February 11, 2014
260cddce49SRobert Watson.Dt LIBMEMSTAT 3
27aa12cea2SUlrich Spörlein.Os
280cddce49SRobert Watson.Sh NAME
290cddce49SRobert Watson.Nm libmemstat
300cddce49SRobert Watson.Nd "library interface to retrieve kernel memory allocator statistics"
310cddce49SRobert Watson.Sh LIBRARY
320cddce49SRobert Watson.Lb libmemstat
330cddce49SRobert Watson.Sh SYNOPSIS
340cddce49SRobert Watson.In sys/types.h
350cddce49SRobert Watson.In memstat.h
36e754c6bbSRobert Watson.Ss General Functions
3733d6b9fbSRuslan Ermilov.Ft "const char *"
38e754c6bbSRobert Watson.Fn memstat_strerror "int error"
390cddce49SRobert Watson.Ss Memory Type List Management Functions
4033d6b9fbSRuslan Ermilov.Ft "struct memory_type_list *"
410cddce49SRobert Watson.Fn memstat_mtl_alloc "void"
4233d6b9fbSRuslan Ermilov.Ft "struct memory_type *"
430cddce49SRobert Watson.Fn memstat_mtl_first "struct memory_type_list *list"
4433d6b9fbSRuslan Ermilov.Ft "struct memory_type *"
450cddce49SRobert Watson.Fn memstat_mtl_next "struct memory_type *mtp"
4633d6b9fbSRuslan Ermilov.Ft "struct memory_type *"
4733d6b9fbSRuslan Ermilov.Fo memstat_mtl_find
4833d6b9fbSRuslan Ermilov.Fa "struct memory_type_list *list" "int allocator" "const char *name"
4933d6b9fbSRuslan Ermilov.Fc
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
56ba23fa9bSRobert Watson.Fn memstat_kvm_all "struct memory_type_list *list" "void *kvm_handle"
57ba23fa9bSRobert Watson.Ft int
58ba23fa9bSRobert Watson.Fn memstat_kvm_malloc "struct memory_type_list *list" "void *kvm_handle"
59ba23fa9bSRobert Watson.Ft int
6033c20d18SRobert Watson.Fn memstat_kvm_uma "struct memory_type_list *list" "void *kvm_handle"
6133c20d18SRobert Watson.Ft int
620cddce49SRobert Watson.Fn memstat_sysctl_all "struct memory_type_list *list" "int flags"
630cddce49SRobert Watson.Ft int
640cddce49SRobert Watson.Fn memstat_sysctl_malloc "struct memory_type_list *list" "int flags"
650cddce49SRobert Watson.Ft int
660cddce49SRobert Watson.Fn memstat_sysctl_uma "struct memory_type_list *list" "int flags"
670cddce49SRobert Watson.Ss Memory Type Accessor Methods
6833d6b9fbSRuslan Ermilov.Ft "const char *"
690cddce49SRobert Watson.Fn memstat_get_name "const struct memory_type *mtp"
700cddce49SRobert Watson.Ft int
710cddce49SRobert Watson.Fn memstat_get_allocator "const struct memory_type *mtp"
720cddce49SRobert Watson.Ft uint64_t
730cddce49SRobert Watson.Fn memstat_get_countlimit "const struct memory_type *mtp"
740cddce49SRobert Watson.Ft uint64_t
750cddce49SRobert Watson.Fn memstat_get_byteslimit "const struct memory_type *mtp"
760cddce49SRobert Watson.Ft uint64_t
770cddce49SRobert Watson.Fn memstat_get_sizemask "const struct memory_type *mtp"
780cddce49SRobert Watson.Ft uint64_t
790cddce49SRobert Watson.Fn memstat_get_size "const struct memory_type *mtp"
800cddce49SRobert Watson.Ft uint64_t
81345e3f4dSGleb Smirnoff.Fn memstat_get_rsize "const struct memory_type *mtp"
82345e3f4dSGleb Smirnoff.Ft uint64_t
830cddce49SRobert Watson.Fn memstat_get_memalloced "const struct memory_type *mtp"
840cddce49SRobert Watson.Ft uint64_t
850cddce49SRobert Watson.Fn memstat_get_memfreed "const struct memory_type *mtp"
860cddce49SRobert Watson.Ft uint64_t
870cddce49SRobert Watson.Fn memstat_get_numallocs "const struct memory_type *mtp"
880cddce49SRobert Watson.Ft uint64_t
890cddce49SRobert Watson.Fn memstat_get_numfrees "const struct memory_type *mtp"
900cddce49SRobert Watson.Ft uint64_t
910cddce49SRobert Watson.Fn memstat_get_bytes "const struct memory_type *mtp"
920cddce49SRobert Watson.Ft uint64_t
930cddce49SRobert Watson.Fn memstat_get_count "const struct memory_type *mtp"
940cddce49SRobert Watson.Ft uint64_t
950cddce49SRobert Watson.Fn memstat_get_free "const struct memory_type *mtp"
960cddce49SRobert Watson.Ft uint64_t
970cddce49SRobert Watson.Fn memstat_get_failures "const struct memory_type *mtp"
9833d6b9fbSRuslan Ermilov.Ft "void *"
990cddce49SRobert Watson.Fn memstat_get_caller_pointer "const struct memory_type *mtp" "int index"
1000cddce49SRobert Watson.Ft void
10133d6b9fbSRuslan Ermilov.Fo memstat_set_caller_pointer
10233d6b9fbSRuslan Ermilov.Fa "struct memory_type *mtp" "int index" "void *value"
10333d6b9fbSRuslan Ermilov.Fc
1040cddce49SRobert Watson.Ft uint64_t
1050cddce49SRobert Watson.Fn memstat_get_caller_uint64 "const struct memory_type *mtp" "int index"
1060cddce49SRobert Watson.Ft void
10733d6b9fbSRuslan Ermilov.Fo memstat_set_caller_uint64
10833d6b9fbSRuslan Ermilov.Fa "struct memory_type *mtp" "int index" "uint64_t value"
10933d6b9fbSRuslan Ermilov.Fc
1100cddce49SRobert Watson.Ft uint64_t
1110cddce49SRobert Watson.Fn memstat_get_zonefree "const struct memory_type *mtp"
1120cddce49SRobert Watson.Ft uint64_t
113ca108fe2SRobert Watson.Fn memstat_get_kegfree "const struct memory_type *mtp"
114ca108fe2SRobert Watson.Ft uint64_t
1150cddce49SRobert Watson.Fn memstat_get_percpu_memalloced "const struct memory_type *mtp" "int cpu"
1160cddce49SRobert Watson.Ft uint64_t
1170cddce49SRobert Watson.Fn memstat_get_percpu_memfreed "const struct memory_type *mtp" "int cpu"
1180cddce49SRobert Watson.Ft uint64_t
1190cddce49SRobert Watson.Fn memstat_get_percpu_numallocs "const struct memory_type *mtp" "int cpu"
1200cddce49SRobert Watson.Ft uint64_t
1210cddce49SRobert Watson.Fn memstat_get_percpu_numfrees "const struct memory_type *mtp" "int cpu"
1220cddce49SRobert Watson.Ft uint64_t
1230cddce49SRobert Watson.Fn memstat_get_percpu_sizemask "const struct memory_type *mtp" "int cpu"
12433d6b9fbSRuslan Ermilov.Ft "void *"
12533d6b9fbSRuslan Ermilov.Fo memstat_get_percpu_caller_pointer
12633d6b9fbSRuslan Ermilov.Fa "const struct memory_type *mtp" "int cpu" "int index"
12733d6b9fbSRuslan Ermilov.Fc
1280cddce49SRobert Watson.Ft void
12933d6b9fbSRuslan Ermilov.Fo memstat_set_percpu_caller_pointer
13033d6b9fbSRuslan Ermilov.Fa "struct memory_type *mtp" "int cpu" "int index" "void *value"
13133d6b9fbSRuslan Ermilov.Fc
1320cddce49SRobert Watson.Ft uint64_t
13333d6b9fbSRuslan Ermilov.Fo memstat_get_percpu_caller_uint64
13433d6b9fbSRuslan Ermilov.Fa "const struct memory_type *mtp" "int cpu" "int index"
13533d6b9fbSRuslan Ermilov.Fc
1360cddce49SRobert Watson.Ft void
13733d6b9fbSRuslan Ermilov.Fo memstat_set_percpu_caller_uint64
13833d6b9fbSRuslan Ermilov.Fa "struct memory_type *mtp" "int cpu" "int index" "uint64_t value"
13933d6b9fbSRuslan Ermilov.Fc
1400cddce49SRobert Watson.Ft uint64_t
1410cddce49SRobert Watson.Fn memstat_get_percpu_free "const struct memory_type *mtp" "int cpu"
1420cddce49SRobert Watson.Sh DESCRIPTION
1430cddce49SRobert Watson.Nm
1440cddce49SRobert Watsonprovides an interface to retrieve kernel memory allocator statistics, for
1450cddce49SRobert Watsonthe purposes of debugging and system monitoring, insulating applications
1460cddce49SRobert Watsonfrom implementation details of the allocators, and allowing a tool to
1470cddce49SRobert Watsontransparently support multiple allocators.
1480cddce49SRobert Watson.Nm
1490cddce49SRobert Watsonsupports both retrieving a single statistics snapshot, as well as
1500cddce49SRobert Watsonincrementally updating statistics for long-term monitoring.
1510cddce49SRobert Watson.Pp
1520cddce49SRobert Watson.Nm
1530cddce49SRobert Watsondescribes each memory type using a
15433d6b9fbSRuslan Ermilov.Vt "struct memory_type" ,
1550cddce49SRobert Watsonan opaque memory type accessed by the application using accessor functions
1560cddce49SRobert Watsonin the library.
1570cddce49SRobert Watson.Nm
1580cddce49SRobert Watsonreturns and updates chains of
15933d6b9fbSRuslan Ermilov.Vt "struct memory_type"
1600cddce49SRobert Watsonvia a
16133d6b9fbSRuslan Ermilov.Vt "struct memory_type_list" ,
1620cddce49SRobert Watsonwhich will be allocated by calling
1630cddce49SRobert Watson.Fn memstat_mtl_alloc ,
1640cddce49SRobert Watsonand freed on completion using
1650cddce49SRobert Watson.Fn memstat_mtl_free .
1660cddce49SRobert WatsonLists of memory types are populated via calls that query the kernel for
1670cddce49SRobert Watsonstatistics information; currently:
168ba23fa9bSRobert Watson.Fn memstat_kvm_all ,
169ba23fa9bSRobert Watson.Fn memstat_kvm_malloc ,
17033c20d18SRobert Watson.Fn memstat_kvm_uma ,
1710cddce49SRobert Watson.Fn memstat_sysctl_all ,
1720cddce49SRobert Watson.Fn memstat_sysctl_uma ,
1730cddce49SRobert Watsonand
1740cddce49SRobert Watson.Fn memstat_sysctl_malloc .
1750cddce49SRobert WatsonRepeated calls will incrementally update the list of memory types, permitting
1760cddce49SRobert Watsontracking over time without recreating all list state.
17770ee997eSRobert WatsonIf an error is detected during a query call, error condition information may
17870ee997eSRobert Watsonbe retrieved using
179e754c6bbSRobert Watson.Fn memstat_mtl_geterror ,
180e754c6bbSRobert Watsonand converted to a user-readable string using
181e754c6bbSRobert Watson.Fn memstat_strerror .
18270ee997eSRobert Watson.Pp
1830cddce49SRobert WatsonFreeing the list will free all memory type data in the list, and so
1840cddce49SRobert Watsoninvalidates any outstanding pointers to entries in the list.
18533d6b9fbSRuslan Ermilov.Vt "struct memory_type"
1860cddce49SRobert Watsonentries in the list may be iterated over using
1870cddce49SRobert Watson.Fn memstat_mtl_first
1880cddce49SRobert Watsonand
18933d6b9fbSRuslan Ermilov.Fn memstat_mtl_next ,
1900cddce49SRobert Watsonwhich respectively return the first entry in a list, and the next entry in a
1910cddce49SRobert Watsonlist.
1920cddce49SRobert Watson.Fn memstat_mtl_find ,
1930cddce49SRobert Watsonwhich will return a pointer to the first entry matching the passed
1940cddce49SRobert Watsonparameters.
1950cddce49SRobert Watson.Pp
1960cddce49SRobert WatsonA series of accessor methods is provided to access fields of the structure,
1970cddce49SRobert Watsonincluding retrieving statistics and properties, as well as setting of caller
1980cddce49SRobert Watsonowned fields.
1990cddce49SRobert WatsonDirect application access to the data structure fields is not supported.
20033d6b9fbSRuslan Ermilov.Ss Library Vt memory_type Ss Fields
2010cddce49SRobert WatsonEach
20233d6b9fbSRuslan Ermilov.Vt "struct memory_type"
2030cddce49SRobert Watsonholds a description of the memory type, including its name and the allocator
2040cddce49SRobert Watsonit is managed by, as well as current statistics on use.
2050cddce49SRobert WatsonSome statistics are directly measured, others are derived from directly
2060cddce49SRobert Watsonmeasured statistics.
2070cddce49SRobert WatsonCertain high level statistics are present across all available allocators,
2080cddce49SRobert Watsonsuch as the number of allocation and free operations; other measurements,
2090cddce49SRobert Watsonsuch as the quantity of free items in per-CPU caches, or administrative
2100cddce49SRobert Watsonlimit on the number of allocations, is available only for specific
2110cddce49SRobert Watsonallocators.
21233d6b9fbSRuslan Ermilov.Ss Caller Vt memory_type Ss Fields
21333d6b9fbSRuslan Ermilov.Vt "struct memory_type"
214e86bb57bSRobert Watsonincludes fields to allow the application to store data, in the form of
215e86bb57bSRobert Watsonpointers and 64-bit integers, with memory types.
2160cddce49SRobert WatsonFor example, the application author might make use of one of the caller
2170cddce49SRobert Watsonpointers to reference a more complex data structure tracking long-term
2180cddce49SRobert Watsonbehavior of the memory type, or a window system object that is used to
2190cddce49SRobert Watsonrender the state of the memory type.
220e86bb57bSRobert WatsonGeneral and per-CPU storage is provided with each
22133d6b9fbSRuslan Ermilov.Vt "struct memory_type"
222e86bb57bSRobert Watsonin the form of an array of pointers and integers.
223e86bb57bSRobert WatsonThe array entries are accessed via the
224e86bb57bSRobert Watson.Fa index
225e86bb57bSRobert Watsonargument to the get and set accessor methods.
226e86bb57bSRobert WatsonPossible values of
227e86bb57bSRobert Watson.Fa index
228e86bb57bSRobert Watsonrange between
22933d6b9fbSRuslan Ermilov0
230e86bb57bSRobert Watsonand
231e86bb57bSRobert Watson.Dv MEMSTAT_MAXCALLER .
232e86bb57bSRobert Watson.Pp
233e86bb57bSRobert WatsonCaller-owned fields are initialized to
23433d6b9fbSRuslan Ermilov0
2350cddce49SRobert Watsonor
2360cddce49SRobert Watson.Dv NULL
237e86bb57bSRobert Watsonwhen a new
23833d6b9fbSRuslan Ermilov.Vt "struct memory_type"
239e86bb57bSRobert Watsonis allocated and attached to a memory type list; these fields retain their
240e86bb57bSRobert Watsonvalues across queries that update library-owned fields.
24109df0a9aSRobert Watson.Ss Allocator Types
24209df0a9aSRobert WatsonCurrently,
24309df0a9aSRobert Watson.Nm
24409df0a9aSRobert Watsonsupports two kernel allocators:
24509df0a9aSRobert Watson.Dv ALLOCATOR_UMA
24609df0a9aSRobert Watsonfor
24709df0a9aSRobert Watson.Xr uma 9 ,
24809df0a9aSRobert Watsonand
24909df0a9aSRobert Watson.Dv ALLOCATOR_MALLOC
25009df0a9aSRobert Watsonfor
25109df0a9aSRobert Watson.Xr malloc 9 .
25209df0a9aSRobert WatsonThese values may be passed to
25309df0a9aSRobert Watson.Fn memstat_mtl_find ,
25409df0a9aSRobert Watsonand will be returned by
25509df0a9aSRobert Watson.Fn memstat_get_allocator .
25609df0a9aSRobert WatsonTwo additional constants in the allocator name space are defined:
25709df0a9aSRobert Watson.Dv ALLOCATOR_UNKNOWN ,
25809df0a9aSRobert Watsonwhich will only be returned as a result of a library error, and
25909df0a9aSRobert Watson.Dv ALLOCATOR_ANY ,
26009df0a9aSRobert Watsonwhich can be used to specify that returning types matching any allocator is
261799162a6SJoel Dahlpermittable from
26209df0a9aSRobert Watson.Fn memstat_mtl_find .
26370ee997eSRobert Watson.Ss Access Method List
26470ee997eSRobert WatsonThe following accessor methods are defined, of which some will be valid for
26570ee997eSRobert Watsona given memory type:
26633d6b9fbSRuslan Ermilov.Bl -tag -width indent
26733d6b9fbSRuslan Ermilov.It Fn memstat_get_name
26870ee997eSRobert WatsonReturn a pointer to the name of the memory type.
26970ee997eSRobert WatsonMemory for the name is owned by
27070ee997eSRobert Watson.Nm
27170ee997eSRobert Watsonand will be valid through a call to
27270ee997eSRobert Watson.Fn memstat_mtl_free .
27370ee997eSRobert WatsonNote that names will be unique with respect to a single allocator, but that
27470ee997eSRobert Watsonthe same name might be used by different memory types owned by different
27570ee997eSRobert Watsonmemory allocators.
27633d6b9fbSRuslan Ermilov.It Fn memstat_get_allocator
27770ee997eSRobert WatsonReturn an integer identifier for the memory allocator that owns the memory
27870ee997eSRobert Watsontype.
27933d6b9fbSRuslan Ermilov.It Fn memstat_get_countlimit
28070ee997eSRobert WatsonIf the memory type has an administrative limit on the number of simultaneous
28170ee997eSRobert Watsonallocations, return it.
28233d6b9fbSRuslan Ermilov.It Fn memstat_get_byteslimit
28370ee997eSRobert WatsonIf the memory type has an administrative limit on the number of bytes of
284799162a6SJoel Dahlmemory that may be simultaneously allocated for the memory type, return it.
28533d6b9fbSRuslan Ermilov.It Fn memstat_get_sizemask
28670ee997eSRobert WatsonIf the memory type supports variable allocation sizes, return a bitmask of
28770ee997eSRobert Watsonsizes allocated for the memory type.
28833d6b9fbSRuslan Ermilov.It Fn memstat_get_size
28970ee997eSRobert WatsonIf the memory type supports a fixed allocation size, return that size.
290345e3f4dSGleb Smirnoff.It Fn memstat_get_rsize
291345e3f4dSGleb SmirnoffIf the memory type supports a fixed allocation size, return real size
292345e3f4dSGleb Smirnoffof an allocation.
293345e3f4dSGleb SmirnoffReal size can exceed requested size due to alignment constraints or
294345e3f4dSGleb Smirnoffimplicit padding.
29533d6b9fbSRuslan Ermilov.It Fn memstat_get_memalloced
29670ee997eSRobert WatsonReturn the total number of bytes allocated for the memory type over its
29770ee997eSRobert Watsonlifetime.
29833d6b9fbSRuslan Ermilov.It Fn memstat_get_memfreed
29970ee997eSRobert WatsonReturn the total number of bytes freed for the memory type over its lifetime.
30033d6b9fbSRuslan Ermilov.It Fn memstat_get_numallocs
30170ee997eSRobert WatsonReturn the total number of allocations for the memory type over its lifetime.
30233d6b9fbSRuslan Ermilov.It Fn memstat_get_numfrees
30370ee997eSRobert WatsonReturn the total number of frees for the memory type over its lifetime.
30433d6b9fbSRuslan Ermilov.It Fn memstat_get_bytes
30570ee997eSRobert WatsonReturn the current number of bytes allocated to the memory type.
30633d6b9fbSRuslan Ermilov.It Fn memstat_get_count
30770ee997eSRobert WatsonReturn the current number of allocations for the memory type.
30833d6b9fbSRuslan Ermilov.It Fn memstat_get_free
30970ee997eSRobert WatsonIf the memory allocator supports a cache, return the number of items in the
31070ee997eSRobert Watsoncache.
31133d6b9fbSRuslan Ermilov.It Fn memstat_get_failures
31270ee997eSRobert WatsonIf the memory allocator and type permit allocation failures, return the
31370ee997eSRobert Watsonnumber of allocation failures measured.
31433d6b9fbSRuslan Ermilov.It Fn memstat_get_caller_pointer
31570ee997eSRobert WatsonReturn a caller-owned pointer for the memory type.
31633d6b9fbSRuslan Ermilov.It Fn memstat_set_caller_pointer
31770ee997eSRobert WatsonSet a caller-owned pointer for the memory type.
31833d6b9fbSRuslan Ermilov.It Fn memstat_get_caller_uint64
31970ee997eSRobert WatsonReturn a caller-owned integer for the memory type.
32033d6b9fbSRuslan Ermilov.It Fn memstat_set_caller_uint64
32170ee997eSRobert WatsonSet a caller-owned integer for the memory type.
32233d6b9fbSRuslan Ermilov.It Fn memstat_get_zonefree
32370ee997eSRobert WatsonIf the memory allocator supports a multi-level allocation structure, return
32470ee997eSRobert Watsonthe number of cached items in the zone.
32570ee997eSRobert WatsonThese items will be in a fully constructed state available for immediate
32670ee997eSRobert Watsonuse.
32733d6b9fbSRuslan Ermilov.It Fn memstat_get_kegfree
32870ee997eSRobert WatsonIf the memory allocator supports a multi-level allocation structure, return
32970ee997eSRobert Watsonthe number of cached items in the keg.
33070ee997eSRobert WatsonThese items may be in a partially constructed state, and may require further
33170ee997eSRobert Watsonprocessing before they can be made available for use.
33233d6b9fbSRuslan Ermilov.It Fn memstat_get_percpu_memalloced
33370ee997eSRobert WatsonIf the memory allocator supports per-CPU statistics, return the number of
33470ee997eSRobert Watsonbytes of memory allocated for the memory type on the CPU over its lifetime.
33533d6b9fbSRuslan Ermilov.It Fn memstat_get_percpu_memfreed
33670ee997eSRobert WatsonIf the memory allocator supports per-CPU statistics, return the number of
33770ee997eSRobert Watsonbytes of memory freed from the memory type on the CPU over its lifetime.
33833d6b9fbSRuslan Ermilov.It Fn memstat_get_percpu_numallocs
33970ee997eSRobert WatsonIf the memory allocator supports per-CPU statistics, return the number of
34070ee997eSRobert Watsonallocations for the memory type on the CPU over its lifetime.
34133d6b9fbSRuslan Ermilov.It Fn memstat_get_percpu_numfrees
34270ee997eSRobert WatsonIf the memory allocator supports per-CPU statistics, return the number of
34370ee997eSRobert Watsonfrees for the memory type on the CPU over its lifetime.
34433d6b9fbSRuslan Ermilov.It Fn memstat_get_percpu_sizemask
34570ee997eSRobert WatsonIf the memory allocator supports variable size memory allocation and per-CPU
34670ee997eSRobert Watsonstatistics, return the size bitmask for the memory type on the CPU.
34733d6b9fbSRuslan Ermilov.It Fn memstat_get_percpu_caller_pointer
34870ee997eSRobert WatsonReturn a caller-owned per-CPU pointer for the memory type.
34933d6b9fbSRuslan Ermilov.It Fn memstat_set_percpu_caller_pointer
35070ee997eSRobert WatsonSet a caller-owned per-CPU pointer for the memory type.
35133d6b9fbSRuslan Ermilov.It Fn memstat_get_percpu_caller_uint64
35270ee997eSRobert WatsonReturn a caller-owned per-CPU integer for the memory type.
3533102cfe2SGlen Barber.It Fn memstat_set_percpu_caller_uint64
35470ee997eSRobert WatsonSet a caller-owned per-CPU integer for the memory type.
35533d6b9fbSRuslan Ermilov.It Fn memstat_get_percpu_free
35670ee997eSRobert WatsonIf the memory allocator supports a per-CPU cache, return the number of free
35770ee997eSRobert Watsonitems in the per-CPU cache of the designated CPU.
35870ee997eSRobert Watson.El
35970ee997eSRobert Watson.Sh RETURN VALUES
36070ee997eSRobert Watson.Nm
36170ee997eSRobert Watsonfunctions fall into three categories: functions returning a pointer to an
36270ee997eSRobert Watsonobject, functions returning an integer return value, and functions
36370ee997eSRobert Watsonimplementing accessor methods returning data from a
36433d6b9fbSRuslan Ermilov.Vt "struct memory_type" .
36570ee997eSRobert Watson.Pp
36670ee997eSRobert WatsonFunctions returning a pointer to an object will generally return
36770ee997eSRobert Watson.Dv NULL
36870ee997eSRobert Watsonon failure.
36970ee997eSRobert Watson.Fn memstat_mtl_alloc
37070ee997eSRobert Watsonwill return an error value via
37170ee997eSRobert Watson.Va errno ,
37270ee997eSRobert Watsonwhich will consist of the value
37333d6b9fbSRuslan Ermilov.Er ENOMEM .
37470ee997eSRobert WatsonFunctions
37570ee997eSRobert Watson.Fn memstat_mtl_first ,
37670ee997eSRobert Watson.Fn memstat_mtl_next ,
37770ee997eSRobert Watsonand
37870ee997eSRobert Watson.Fn memstat_mtl_find
37970ee997eSRobert Watsonwill return
38070ee997eSRobert Watson.Dv NULL
38170ee997eSRobert Watsonwhen there is no entry or match in the list; however, this is not considered
38270ee997eSRobert Watsona failure mode and no error value is available.
38370ee997eSRobert Watson.Pp
38433d6b9fbSRuslan ErmilovFunctions returning an integer success value will return
38533d6b9fbSRuslan Ermilov0
38670ee997eSRobert Watsonon success, or
38733d6b9fbSRuslan Ermilov\-1
38870ee997eSRobert Watsonon failure.
38970ee997eSRobert WatsonIf a failure is returned, the list error access method,
39070ee997eSRobert Watson.Fn memstat_mtl_geterror ,
391e754c6bbSRobert Watsonmay be used to retrieve the error state.
392e754c6bbSRobert WatsonThe string representation of the error may be retrieved using
393e754c6bbSRobert Watson.Fn memstat_strerror .
394e754c6bbSRobert WatsonPossible error values are:
39533d6b9fbSRuslan Ermilov.Bl -tag -width ".Dv MEMSTAT_ERROR_KVM_SHORTREAD"
39670ee997eSRobert Watson.It Dv MEMSTAT_ERROR_UNDEFINED
39733d6b9fbSRuslan ErmilovUndefined error.
39833d6b9fbSRuslan ErmilovOccurs if
39970ee997eSRobert Watson.Fn memstat_mtl_geterror
40070ee997eSRobert Watsonis called on a list before an error associated with the list has occurred.
40170ee997eSRobert Watson.It Dv MEMSTAT_ERROR_NOMEMORY
40233d6b9fbSRuslan ErmilovInsufficient memory.
40333d6b9fbSRuslan ErmilovOccurs if library calls to
40470ee997eSRobert Watson.Xr malloc 3
40570ee997eSRobert Watsonfail, or if a system call to retrieve kernel statistics fails with
40670ee997eSRobert Watson.Er ENOMEM .
40770ee997eSRobert Watson.It Dv MEMSTAT_ERROR_VERSION
40870ee997eSRobert WatsonReturned if the current version of
40970ee997eSRobert Watson.Nm
41070ee997eSRobert Watsonis unable to interpret the statistics data returned by the kernel due to an
41170ee997eSRobert Watsonexplicit version mismatch, or to differences in data structures that cannot
41270ee997eSRobert Watsonbe reconciled.
41370ee997eSRobert Watson.It Dv MEMSTAT_ERROR_PERMISSION
41470ee997eSRobert WatsonReturned if a statistics source returns
41570ee997eSRobert Watson.Va errno
41670ee997eSRobert Watsonvalues of
41733d6b9fbSRuslan Ermilov.Er EACCES
41870ee997eSRobert Watsonor
41933d6b9fbSRuslan Ermilov.Er EPERM .
42070ee997eSRobert Watson.It Dv MEMSTAT_ERROR_DATAERROR
42170ee997eSRobert WatsonReturned if
42270ee997eSRobert Watson.Nm
42370ee997eSRobert Watsonis unable to interpret statistics data returned by the data source, even
42470ee997eSRobert Watsonthough there does not appear to be a version problem.
425513c89ceSRobert Watson.It Dv MEMSTAT_ERROR_KVM
426513c89ceSRobert WatsonReturned if
427513c89ceSRobert Watson.Nm
428513c89ceSRobert Watsonexperiences an error while using
429513c89ceSRobert Watson.Xr kvm 3
430513c89ceSRobert Watsoninterfaces to query statistics data.
431513c89ceSRobert WatsonUse
432513c89ceSRobert Watson.Xr kvm_geterr 3
433513c89ceSRobert Watsonto retrieve the error.
434513c89ceSRobert Watson.It Dv MEMSTAT_ERROR_KVM_NOSYMBOL
435513c89ceSRobert WatsonReturned if
436513c89ceSRobert Watson.Nm
437513c89ceSRobert Watsonis unable to read a required symbol from the kernel being operated on.
438513c89ceSRobert Watson.It Dv MEMSTAT_ERROR_KVM_SHORTREAD
439513c89ceSRobert WatsonReturned if
440513c89ceSRobert Watson.Nm
441513c89ceSRobert Watsonattempts to read data from a live memory image or kernel core dump and
442513c89ceSRobert Watsoninsufficient data is returned.
44370ee997eSRobert Watson.El
44470ee997eSRobert Watson.Pp
44570ee997eSRobert WatsonFinally, functions returning data from a
44633d6b9fbSRuslan Ermilov.Vt "struct memory_type"
44770ee997eSRobert Watsonpointer are not permitted to fail, and directly return either a statistic
44870ee997eSRobert Watsonor pointer to a string.
4490cddce49SRobert Watson.Sh EXAMPLES
4500cddce49SRobert WatsonCreate a memory type list, query the
4510cddce49SRobert Watson.Xr uma 9
4520cddce49SRobert Watsonmemory allocator for available statistics, and print out the number of
4530cddce49SRobert Watsonallocations performed by the
45433f23dc2SRobert Watson.Dv mbuf
4550cddce49SRobert Watsonzone.
4560cddce49SRobert Watson.Bd -literal -offset indent
4570cddce49SRobert Watsonstruct memory_type_list *mtlp;
4580cddce49SRobert Watsonstruct memory_type *mtp;
4590cddce49SRobert Watsonuint64_t mbuf_count;
4600cddce49SRobert Watson
4610cddce49SRobert Watsonmtlp = memstat_mtl_alloc();
4620cddce49SRobert Watsonif (mtlp == NULL)
4630cddce49SRobert Watson    err(-1, "memstat_mtl_alloc");
4640cddce49SRobert Watsonif (memstat_sysctl_uma(mtlp, 0) < 0)
4650cddce49SRobert Watson    err(-1, "memstat_sysctl_uma");
46633f23dc2SRobert Watsonmtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, "mbuf");
4670cddce49SRobert Watsonif (mtp == NULL)
46833f23dc2SRobert Watson    errx(-1, "memstat_mtl_find: mbuf not found");
4690cddce49SRobert Watsonmbuf_count = memstat_get_count(mtp);
4700cddce49SRobert Watsonmemstat_mtl_free(mtlp);
4710cddce49SRobert Watson
47233d6b9fbSRuslan Ermilovprintf("mbufs: %llu\en", (unsigned long long)mbuf_count);
4730cddce49SRobert Watson.Ed
4740cddce49SRobert Watson.Sh SEE ALSO
4750cddce49SRobert Watson.Xr malloc 9 ,
4760cddce49SRobert Watson.Xr uma 9
4770cddce49SRobert Watson.Sh HISTORY
4780cddce49SRobert WatsonThe
47933d6b9fbSRuslan Ermilov.Nm
4800cddce49SRobert Watsonlibrary appeared in
4810cddce49SRobert Watson.Fx 6.0 .
4820cddce49SRobert Watson.Sh AUTHORS
4830cddce49SRobert WatsonThe kernel memory allocator changes necessary to support a general purpose
4840cddce49SRobert Watsonmonitoring library, along with the library, were written by
485*2b7af31cSBaptiste Daroussin.An Robert Watson Aq Mt rwatson@FreeBSD.org .
4860cddce49SRobert Watson.Sh BUGS
487ba23fa9bSRobert WatsonThere are memory allocators in the kernel, such as the VM page allocator
48833d6b9fbSRuslan Ermilovand
48933d6b9fbSRuslan Ermilov.Nm sf_buf
49033d6b9fbSRuslan Ermilovallocator, which are not currently supported by
49133d6b9fbSRuslan Ermilov.Nm .
4920cddce49SRobert Watson.Pp
4930cddce49SRobert WatsonOnce a memory type is present on a memory type list, it will not be removed
4940cddce49SRobert Watsoneven if the kernel no longer presents information on the type via its
4950cddce49SRobert Watsonmonitoring interfaces.
4960cddce49SRobert WatsonIn order to flush removed memory types, it is necessary to free the entire
4970cddce49SRobert Watsonlist and allocate a new one.
498