xref: /freebsd/lib/libmemstat/libmemstat.3 (revision ca108fe268b738a363dde8e3cba5be3bbc2d9cc9)
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
96ca108fe2SRobert Watson.Fn memstat_get_kegfree "const struct memory_type *mtp"
97ca108fe2SRobert Watson.Ft uint64_t
980cddce49SRobert Watson.Fn memstat_get_percpu_memalloced "const struct memory_type *mtp" "int cpu"
990cddce49SRobert Watson.Ft uint64_t
1000cddce49SRobert Watson.Fn memstat_get_percpu_memfreed "const struct memory_type *mtp" "int cpu"
1010cddce49SRobert Watson.Ft uint64_t
1020cddce49SRobert Watson.Fn memstat_get_percpu_numallocs "const struct memory_type *mtp" "int cpu"
1030cddce49SRobert Watson.Ft uint64_t
1040cddce49SRobert Watson.Fn memstat_get_percpu_numfrees "const struct memory_type *mtp" "int cpu"
1050cddce49SRobert Watson.Ft uint64_t
1060cddce49SRobert Watson.Fn memstat_get_percpu_sizemask "const struct memory_type *mtp" "int cpu"
1070cddce49SRobert Watson.Ft void *
1080cddce49SRobert Watson.Fn memstat_get_percpu_caller_pointer "const struct memory_type *mtp" "int cpu" "int index"
1090cddce49SRobert Watson.Ft void
1100cddce49SRobert Watson.Fn memstat_set_percpu_caller_pointer "struct memory_type *mtp" "int cpu" "int index" "void *value"
1110cddce49SRobert Watson.Ft uint64_t
1120cddce49SRobert Watson.Fn memstat_get_percpu_caller_uint64 "const struct memory_type *mtp" "int cpu" "int index"
1130cddce49SRobert Watson.Ft void
1140cddce49SRobert Watson.Fn memstat_set_percpu_caller_uint64 "struct memory_type *mtp" "int cpu" "int index" "uint64_t value"
1150cddce49SRobert Watson.Ft uint64_t
1160cddce49SRobert Watson.Fn memstat_get_percpu_free "const struct memory_type *mtp" "int cpu"
1170cddce49SRobert Watson.Sh DESCRIPTION
1180cddce49SRobert Watson.Nm
1190cddce49SRobert Watsonprovides an interface to retrieve kernel memory allocator statistics, for
1200cddce49SRobert Watsonthe purposes of debugging and system monitoring, insulating applications
1210cddce49SRobert Watsonfrom implementation details of the allocators, and allowing a tool to
1220cddce49SRobert Watsontransparently support multiple allocators.
1230cddce49SRobert Watson.Nm
1240cddce49SRobert Watsonsupports both retrieving a single statistics snapshot, as well as
1250cddce49SRobert Watsonincrementally updating statistics for long-term monitoring.
1260cddce49SRobert Watson.Pp
1270cddce49SRobert Watson.Nm
1280cddce49SRobert Watsondescribes each memory type using a
1290cddce49SRobert Watson.Vt struct memory_type ,
1300cddce49SRobert Watsonan opaque memory type accessed by the application using accessor functions
1310cddce49SRobert Watsonin the library.
1320cddce49SRobert Watson.Nm
1330cddce49SRobert Watsonreturns and updates chains of
1340cddce49SRobert Watson.Vt struct memory_type
1350cddce49SRobert Watsonvia a
1360cddce49SRobert Watson.Vt struct memory_type_list ,
1370cddce49SRobert Watsonwhich will be allocated by calling
1380cddce49SRobert Watson.Fn memstat_mtl_alloc ,
1390cddce49SRobert Watsonand freed on completion using
1400cddce49SRobert Watson.Fn memstat_mtl_free .
1410cddce49SRobert WatsonLists of memory types are populated via calls that query the kernel for
1420cddce49SRobert Watsonstatistics information; currently:
1430cddce49SRobert Watson.Fn memstat_sysctl_all ,
1440cddce49SRobert Watson.Fn memstat_sysctl_uma ,
1450cddce49SRobert Watsonand
1460cddce49SRobert Watson.Fn memstat_sysctl_malloc .
1470cddce49SRobert WatsonRepeated calls will incrementally update the list of memory types, permitting
1480cddce49SRobert Watsontracking over time without recreating all list state.
1490cddce49SRobert WatsonFreeing the list will free all memory type data in the list, and so
1500cddce49SRobert Watsoninvalidates any outstanding pointers to entries in the list.
1510cddce49SRobert Watson.Vt struct memory_type
1520cddce49SRobert Watsonentries in the list may be iterated over using
1530cddce49SRobert Watson.Fn memstat_mtl_first
1540cddce49SRobert Watsonand
1550cddce49SRobert Watson.fn memstat_mtl_next ,
1560cddce49SRobert Watsonwhich respectively return the first entry in a list, and the next entry in a
1570cddce49SRobert Watsonlist.
1580cddce49SRobert Watson.Fn memstat_mtl_find ,
1590cddce49SRobert Watsonwhich will return a pointer to the first entry matching the passed
1600cddce49SRobert Watsonparameters.
1610cddce49SRobert Watson.Pp
1620cddce49SRobert WatsonA series of accessor methods is provided to access fields of the structure,
1630cddce49SRobert Watsonincluding retrieving statistics and properties, as well as setting of caller
1640cddce49SRobert Watsonowned fields.
1650cddce49SRobert WatsonDirect application access to the data structure fields is not supported.
1660cddce49SRobert Watson.Ss Library memory_type Fields
1670cddce49SRobert WatsonEach
1680cddce49SRobert Watson.Vt struct memory_type
1690cddce49SRobert Watsonholds a description of the memory type, including its name and the allocator
1700cddce49SRobert Watsonit is managed by, as well as current statistics on use.
1710cddce49SRobert WatsonSome statistics are directly measured, others are derived from directly
1720cddce49SRobert Watsonmeasured statistics.
1730cddce49SRobert WatsonCertain high level statistics are present across all available allocators,
1740cddce49SRobert Watsonsuch as the number of allocation and free operations; other measurements,
1750cddce49SRobert Watsonsuch as the quantity of free items in per-CPU caches, or administrative
1760cddce49SRobert Watsonlimit on the number of allocations, is available only for specific
1770cddce49SRobert Watsonallocators.
178e86bb57bSRobert Watson.Ss Caller memory_type Fields
1790cddce49SRobert Watson.Vt struct memory_type
180e86bb57bSRobert Watsonincludes fields to allow the application to store data, in the form of
181e86bb57bSRobert Watsonpointers and 64-bit integers, with memory types.
1820cddce49SRobert WatsonFor example, the application author might make use of one of the caller
1830cddce49SRobert Watsonpointers to reference a more complex data structure tracking long-term
1840cddce49SRobert Watsonbehavior of the memory type, or a window system object that is used to
1850cddce49SRobert Watsonrender the state of the memory type.
186e86bb57bSRobert WatsonGeneral and per-CPU storage is provided with each
1870cddce49SRobert Watson.Vt struct memory_type
188e86bb57bSRobert Watsonin the form of an array of pointers and integers.
189e86bb57bSRobert WatsonThe array entries are accessed via the
190e86bb57bSRobert Watson.Fa index
191e86bb57bSRobert Watsonargument to the get and set accessor methods.
192e86bb57bSRobert WatsonPossible values of
193e86bb57bSRobert Watson.Fa index
194e86bb57bSRobert Watsonrange between
195e86bb57bSRobert Watson.Dv 0
196e86bb57bSRobert Watsonand
197e86bb57bSRobert Watson.Dv MEMSTAT_MAXCALLER .
198e86bb57bSRobert Watson.Pp
199e86bb57bSRobert WatsonCaller-owned fields are initialized to
2000cddce49SRobert Watson.Dv 0
2010cddce49SRobert Watsonor
2020cddce49SRobert Watson.Dv NULL
203e86bb57bSRobert Watsonwhen a new
204e86bb57bSRobert Watson.Vt struct memory_type
205e86bb57bSRobert Watsonis allocated and attached to a memory type list; these fields retain their
206e86bb57bSRobert Watsonvalues across queries that update library-owned fields.
20709df0a9aSRobert Watson.Ss Allocator Types
20809df0a9aSRobert WatsonCurrently,
20909df0a9aSRobert Watson.Nm
21009df0a9aSRobert Watsonsupports two kernel allocators:
21109df0a9aSRobert Watson.Dv ALLOCATOR_UMA
21209df0a9aSRobert Watsonfor
21309df0a9aSRobert Watson.Xr uma 9 ,
21409df0a9aSRobert Watsonand
21509df0a9aSRobert Watson.Dv ALLOCATOR_MALLOC
21609df0a9aSRobert Watsonfor
21709df0a9aSRobert Watson.Xr malloc 9 .
21809df0a9aSRobert WatsonThese values may be passed to
21909df0a9aSRobert Watson.Fn memstat_mtl_find ,
22009df0a9aSRobert Watsonand will be returned by
22109df0a9aSRobert Watson.Fn memstat_get_allocator .
22209df0a9aSRobert WatsonTwo additional constants in the allocator name space are defined:
22309df0a9aSRobert Watson.Dv ALLOCATOR_UNKNOWN ,
22409df0a9aSRobert Watsonwhich will only be returned as a result of a library error, and
22509df0a9aSRobert Watson.Dv ALLOCATOR_ANY ,
22609df0a9aSRobert Watsonwhich can be used to specify that returning types matching any allocator is
22709df0a9aSRobert Watsonpermittible from
22809df0a9aSRobert Watson.Fn memstat_mtl_find .
2290cddce49SRobert Watson.Sh EXAMPLES
2300cddce49SRobert WatsonCreate a memory type list, query the
2310cddce49SRobert Watson.Xr uma 9
2320cddce49SRobert Watsonmemory allocator for available statistics, and print out the number of
2330cddce49SRobert Watsonallocations performed by the
2340cddce49SRobert Watson.Dv Mbuf
2350cddce49SRobert Watsonzone.
2360cddce49SRobert Watson.Bd -literal -offset indent
2370cddce49SRobert Watsonstruct memory_type_list *mtlp;
2380cddce49SRobert Watsonstruct memory_type *mtp;
2390cddce49SRobert Watsonuint64_t mbuf_count;
2400cddce49SRobert Watson
2410cddce49SRobert Watsonmtlp = memstat_mtl_alloc();
2420cddce49SRobert Watsonif (mtlp == NULL)
2430cddce49SRobert Watson    err(-1, "memstat_mtl_alloc");
2440cddce49SRobert Watsonif (memstat_sysctl_uma(mtlp, 0) < 0)
2450cddce49SRobert Watson    err(-1, "memstat_sysctl_uma");
2460cddce49SRobert Watsonmtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, "Mbuf");
2470cddce49SRobert Watsonif (mtp == NULL)
2480cddce49SRobert Watson    errx(-1, "memstat_mtl_find: Mbuf not found");
2490cddce49SRobert Watsonmbuf_count = memstat_get_count(mtp);
2500cddce49SRobert Watsonmemstat_mtl_free(mtlp);
2510cddce49SRobert Watson
2520cddce49SRobert Watsonprintf("Mbufs: %llu\\n", (unsigned long long)mbuf_count);
2530cddce49SRobert Watson.Ed
2540cddce49SRobert Watson.Sh SEE ALSO
2550cddce49SRobert Watson.Xr malloc 9 ,
2560cddce49SRobert Watson.Xr uma 9
2570cddce49SRobert Watson.Sh HISTORY
2580cddce49SRobert WatsonThe
2590cddce49SRobert Watson.Nm memstat
2600cddce49SRobert Watsonlibrary appeared in
2610cddce49SRobert Watson.Fx 6.0 .
2620cddce49SRobert Watson.Sh AUTHORS
2630cddce49SRobert WatsonThe kernel memory allocator changes necessary to support a general purpose
2640cddce49SRobert Watsonmonitoring library, along with the library, were written by
2650cddce49SRobert Watson.An Robert Watson Aq rwatson@FreeBSD.org
2660cddce49SRobert Watson.Sh BUGS
2670cddce49SRobert Watson.Nm
2680cddce49SRobert Watsoncannot yet extract statistics from kernel core dumps, although this should be
2690cddce49SRobert Watsonstraight forward to implement.
2700cddce49SRobert Watson.Pp
2710cddce49SRobert WatsonOnce a memory type is present on a memory type list, it will not be removed
2720cddce49SRobert Watsoneven if the kernel no longer presents information on the type via its
2730cddce49SRobert Watsonmonitoring interfaces.
2740cddce49SRobert WatsonIn order to flush removed memory types, it is necessary to free the entire
2750cddce49SRobert Watsonlist and allocate a new one.
276