xref: /f-stack/tools/libmemstat/libmemstat.3 (revision 1eaf0ac3)
1*1eaf0ac3Slogwang.\" Copyright (c) 2005 Robert N. M. Watson
2*1eaf0ac3Slogwang.\" All rights reserved.
3*1eaf0ac3Slogwang.\"
4*1eaf0ac3Slogwang.\" Redistribution and use in source and binary forms, with or without
5*1eaf0ac3Slogwang.\" modification, are permitted provided that the following conditions
6*1eaf0ac3Slogwang.\" are met:
7*1eaf0ac3Slogwang.\" 1. Redistributions of source code must retain the above copyright
8*1eaf0ac3Slogwang.\"    notice, this list of conditions and the following disclaimer.
9*1eaf0ac3Slogwang.\" 2. Redistributions in binary form must reproduce the above copyright
10*1eaf0ac3Slogwang.\"    notice, this list of conditions and the following disclaimer in the
11*1eaf0ac3Slogwang.\"    documentation and/or other materials provided with the distribution.
12*1eaf0ac3Slogwang.\"
13*1eaf0ac3Slogwang.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
14*1eaf0ac3Slogwang.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*1eaf0ac3Slogwang.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*1eaf0ac3Slogwang.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
17*1eaf0ac3Slogwang.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*1eaf0ac3Slogwang.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*1eaf0ac3Slogwang.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*1eaf0ac3Slogwang.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*1eaf0ac3Slogwang.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*1eaf0ac3Slogwang.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*1eaf0ac3Slogwang.\" SUCH DAMAGE.
24*1eaf0ac3Slogwang.\"
25*1eaf0ac3Slogwang.\" $FreeBSD$
26*1eaf0ac3Slogwang.\"
27*1eaf0ac3Slogwang.Dd February 11, 2014
28*1eaf0ac3Slogwang.Dt LIBMEMSTAT 3
29*1eaf0ac3Slogwang.Os
30*1eaf0ac3Slogwang.Sh NAME
31*1eaf0ac3Slogwang.Nm libmemstat
32*1eaf0ac3Slogwang.Nd "library interface to retrieve kernel memory allocator statistics"
33*1eaf0ac3Slogwang.Sh LIBRARY
34*1eaf0ac3Slogwang.Lb libmemstat
35*1eaf0ac3Slogwang.Sh SYNOPSIS
36*1eaf0ac3Slogwang.In sys/types.h
37*1eaf0ac3Slogwang.In memstat.h
38*1eaf0ac3Slogwang.Ss General Functions
39*1eaf0ac3Slogwang.Ft "const char *"
40*1eaf0ac3Slogwang.Fn memstat_strerror "int error"
41*1eaf0ac3Slogwang.Ss Memory Type List Management Functions
42*1eaf0ac3Slogwang.Ft "struct memory_type_list *"
43*1eaf0ac3Slogwang.Fn memstat_mtl_alloc "void"
44*1eaf0ac3Slogwang.Ft "struct memory_type *"
45*1eaf0ac3Slogwang.Fn memstat_mtl_first "struct memory_type_list *list"
46*1eaf0ac3Slogwang.Ft "struct memory_type *"
47*1eaf0ac3Slogwang.Fn memstat_mtl_next "struct memory_type *mtp"
48*1eaf0ac3Slogwang.Ft "struct memory_type *"
49*1eaf0ac3Slogwang.Fo memstat_mtl_find
50*1eaf0ac3Slogwang.Fa "struct memory_type_list *list" "int allocator" "const char *name"
51*1eaf0ac3Slogwang.Fc
52*1eaf0ac3Slogwang.Ft void
53*1eaf0ac3Slogwang.Fn memstat_mtl_free "struct memory_type_list *list"
54*1eaf0ac3Slogwang.Ft int
55*1eaf0ac3Slogwang.Fn memstat_mtl_geterror "struct memory_type_list *list"
56*1eaf0ac3Slogwang.Ss Allocator Query Functions
57*1eaf0ac3Slogwang.Ft int
58*1eaf0ac3Slogwang.Fn memstat_kvm_all "struct memory_type_list *list" "void *kvm_handle"
59*1eaf0ac3Slogwang.Ft int
60*1eaf0ac3Slogwang.Fn memstat_kvm_malloc "struct memory_type_list *list" "void *kvm_handle"
61*1eaf0ac3Slogwang.Ft int
62*1eaf0ac3Slogwang.Fn memstat_kvm_uma "struct memory_type_list *list" "void *kvm_handle"
63*1eaf0ac3Slogwang.Ft int
64*1eaf0ac3Slogwang.Fn memstat_sysctl_all "struct memory_type_list *list" "int flags"
65*1eaf0ac3Slogwang.Ft int
66*1eaf0ac3Slogwang.Fn memstat_sysctl_malloc "struct memory_type_list *list" "int flags"
67*1eaf0ac3Slogwang.Ft int
68*1eaf0ac3Slogwang.Fn memstat_sysctl_uma "struct memory_type_list *list" "int flags"
69*1eaf0ac3Slogwang.Ss Memory Type Accessor Methods
70*1eaf0ac3Slogwang.Ft "const char *"
71*1eaf0ac3Slogwang.Fn memstat_get_name "const struct memory_type *mtp"
72*1eaf0ac3Slogwang.Ft int
73*1eaf0ac3Slogwang.Fn memstat_get_allocator "const struct memory_type *mtp"
74*1eaf0ac3Slogwang.Ft uint64_t
75*1eaf0ac3Slogwang.Fn memstat_get_countlimit "const struct memory_type *mtp"
76*1eaf0ac3Slogwang.Ft uint64_t
77*1eaf0ac3Slogwang.Fn memstat_get_byteslimit "const struct memory_type *mtp"
78*1eaf0ac3Slogwang.Ft uint64_t
79*1eaf0ac3Slogwang.Fn memstat_get_sizemask "const struct memory_type *mtp"
80*1eaf0ac3Slogwang.Ft uint64_t
81*1eaf0ac3Slogwang.Fn memstat_get_size "const struct memory_type *mtp"
82*1eaf0ac3Slogwang.Ft uint64_t
83*1eaf0ac3Slogwang.Fn memstat_get_rsize "const struct memory_type *mtp"
84*1eaf0ac3Slogwang.Ft uint64_t
85*1eaf0ac3Slogwang.Fn memstat_get_memalloced "const struct memory_type *mtp"
86*1eaf0ac3Slogwang.Ft uint64_t
87*1eaf0ac3Slogwang.Fn memstat_get_memfreed "const struct memory_type *mtp"
88*1eaf0ac3Slogwang.Ft uint64_t
89*1eaf0ac3Slogwang.Fn memstat_get_numallocs "const struct memory_type *mtp"
90*1eaf0ac3Slogwang.Ft uint64_t
91*1eaf0ac3Slogwang.Fn memstat_get_numfrees "const struct memory_type *mtp"
92*1eaf0ac3Slogwang.Ft uint64_t
93*1eaf0ac3Slogwang.Fn memstat_get_bytes "const struct memory_type *mtp"
94*1eaf0ac3Slogwang.Ft uint64_t
95*1eaf0ac3Slogwang.Fn memstat_get_count "const struct memory_type *mtp"
96*1eaf0ac3Slogwang.Ft uint64_t
97*1eaf0ac3Slogwang.Fn memstat_get_free "const struct memory_type *mtp"
98*1eaf0ac3Slogwang.Ft uint64_t
99*1eaf0ac3Slogwang.Fn memstat_get_failures "const struct memory_type *mtp"
100*1eaf0ac3Slogwang.Ft "void *"
101*1eaf0ac3Slogwang.Fn memstat_get_caller_pointer "const struct memory_type *mtp" "int index"
102*1eaf0ac3Slogwang.Ft void
103*1eaf0ac3Slogwang.Fo memstat_set_caller_pointer
104*1eaf0ac3Slogwang.Fa "struct memory_type *mtp" "int index" "void *value"
105*1eaf0ac3Slogwang.Fc
106*1eaf0ac3Slogwang.Ft uint64_t
107*1eaf0ac3Slogwang.Fn memstat_get_caller_uint64 "const struct memory_type *mtp" "int index"
108*1eaf0ac3Slogwang.Ft void
109*1eaf0ac3Slogwang.Fo memstat_set_caller_uint64
110*1eaf0ac3Slogwang.Fa "struct memory_type *mtp" "int index" "uint64_t value"
111*1eaf0ac3Slogwang.Fc
112*1eaf0ac3Slogwang.Ft uint64_t
113*1eaf0ac3Slogwang.Fn memstat_get_zonefree "const struct memory_type *mtp"
114*1eaf0ac3Slogwang.Ft uint64_t
115*1eaf0ac3Slogwang.Fn memstat_get_kegfree "const struct memory_type *mtp"
116*1eaf0ac3Slogwang.Ft uint64_t
117*1eaf0ac3Slogwang.Fn memstat_get_percpu_memalloced "const struct memory_type *mtp" "int cpu"
118*1eaf0ac3Slogwang.Ft uint64_t
119*1eaf0ac3Slogwang.Fn memstat_get_percpu_memfreed "const struct memory_type *mtp" "int cpu"
120*1eaf0ac3Slogwang.Ft uint64_t
121*1eaf0ac3Slogwang.Fn memstat_get_percpu_numallocs "const struct memory_type *mtp" "int cpu"
122*1eaf0ac3Slogwang.Ft uint64_t
123*1eaf0ac3Slogwang.Fn memstat_get_percpu_numfrees "const struct memory_type *mtp" "int cpu"
124*1eaf0ac3Slogwang.Ft uint64_t
125*1eaf0ac3Slogwang.Fn memstat_get_percpu_sizemask "const struct memory_type *mtp" "int cpu"
126*1eaf0ac3Slogwang.Ft "void *"
127*1eaf0ac3Slogwang.Fo memstat_get_percpu_caller_pointer
128*1eaf0ac3Slogwang.Fa "const struct memory_type *mtp" "int cpu" "int index"
129*1eaf0ac3Slogwang.Fc
130*1eaf0ac3Slogwang.Ft void
131*1eaf0ac3Slogwang.Fo memstat_set_percpu_caller_pointer
132*1eaf0ac3Slogwang.Fa "struct memory_type *mtp" "int cpu" "int index" "void *value"
133*1eaf0ac3Slogwang.Fc
134*1eaf0ac3Slogwang.Ft uint64_t
135*1eaf0ac3Slogwang.Fo memstat_get_percpu_caller_uint64
136*1eaf0ac3Slogwang.Fa "const struct memory_type *mtp" "int cpu" "int index"
137*1eaf0ac3Slogwang.Fc
138*1eaf0ac3Slogwang.Ft void
139*1eaf0ac3Slogwang.Fo memstat_set_percpu_caller_uint64
140*1eaf0ac3Slogwang.Fa "struct memory_type *mtp" "int cpu" "int index" "uint64_t value"
141*1eaf0ac3Slogwang.Fc
142*1eaf0ac3Slogwang.Ft uint64_t
143*1eaf0ac3Slogwang.Fn memstat_get_percpu_free "const struct memory_type *mtp" "int cpu"
144*1eaf0ac3Slogwang.Sh DESCRIPTION
145*1eaf0ac3Slogwang.Nm
146*1eaf0ac3Slogwangprovides an interface to retrieve kernel memory allocator statistics, for
147*1eaf0ac3Slogwangthe purposes of debugging and system monitoring, insulating applications
148*1eaf0ac3Slogwangfrom implementation details of the allocators, and allowing a tool to
149*1eaf0ac3Slogwangtransparently support multiple allocators.
150*1eaf0ac3Slogwang.Nm
151*1eaf0ac3Slogwangsupports both retrieving a single statistics snapshot, as well as
152*1eaf0ac3Slogwangincrementally updating statistics for long-term monitoring.
153*1eaf0ac3Slogwang.Pp
154*1eaf0ac3Slogwang.Nm
155*1eaf0ac3Slogwangdescribes each memory type using a
156*1eaf0ac3Slogwang.Vt "struct memory_type" ,
157*1eaf0ac3Slogwangan opaque memory type accessed by the application using accessor functions
158*1eaf0ac3Slogwangin the library.
159*1eaf0ac3Slogwang.Nm
160*1eaf0ac3Slogwangreturns and updates chains of
161*1eaf0ac3Slogwang.Vt "struct memory_type"
162*1eaf0ac3Slogwangvia a
163*1eaf0ac3Slogwang.Vt "struct memory_type_list" ,
164*1eaf0ac3Slogwangwhich will be allocated by calling
165*1eaf0ac3Slogwang.Fn memstat_mtl_alloc ,
166*1eaf0ac3Slogwangand freed on completion using
167*1eaf0ac3Slogwang.Fn memstat_mtl_free .
168*1eaf0ac3SlogwangLists of memory types are populated via calls that query the kernel for
169*1eaf0ac3Slogwangstatistics information; currently:
170*1eaf0ac3Slogwang.Fn memstat_kvm_all ,
171*1eaf0ac3Slogwang.Fn memstat_kvm_malloc ,
172*1eaf0ac3Slogwang.Fn memstat_kvm_uma ,
173*1eaf0ac3Slogwang.Fn memstat_sysctl_all ,
174*1eaf0ac3Slogwang.Fn memstat_sysctl_uma ,
175*1eaf0ac3Slogwangand
176*1eaf0ac3Slogwang.Fn memstat_sysctl_malloc .
177*1eaf0ac3SlogwangRepeated calls will incrementally update the list of memory types, permitting
178*1eaf0ac3Slogwangtracking over time without recreating all list state.
179*1eaf0ac3SlogwangIf an error is detected during a query call, error condition information may
180*1eaf0ac3Slogwangbe retrieved using
181*1eaf0ac3Slogwang.Fn memstat_mtl_geterror ,
182*1eaf0ac3Slogwangand converted to a user-readable string using
183*1eaf0ac3Slogwang.Fn memstat_strerror .
184*1eaf0ac3Slogwang.Pp
185*1eaf0ac3SlogwangFreeing the list will free all memory type data in the list, and so
186*1eaf0ac3Slogwanginvalidates any outstanding pointers to entries in the list.
187*1eaf0ac3Slogwang.Vt "struct memory_type"
188*1eaf0ac3Slogwangentries in the list may be iterated over using
189*1eaf0ac3Slogwang.Fn memstat_mtl_first
190*1eaf0ac3Slogwangand
191*1eaf0ac3Slogwang.Fn memstat_mtl_next ,
192*1eaf0ac3Slogwangwhich respectively return the first entry in a list, and the next entry in a
193*1eaf0ac3Slogwanglist.
194*1eaf0ac3Slogwang.Fn memstat_mtl_find ,
195*1eaf0ac3Slogwangwhich will return a pointer to the first entry matching the passed
196*1eaf0ac3Slogwangparameters.
197*1eaf0ac3Slogwang.Pp
198*1eaf0ac3SlogwangA series of accessor methods is provided to access fields of the structure,
199*1eaf0ac3Slogwangincluding retrieving statistics and properties, as well as setting of caller
200*1eaf0ac3Slogwangowned fields.
201*1eaf0ac3SlogwangDirect application access to the data structure fields is not supported.
202*1eaf0ac3Slogwang.Ss Library Vt memory_type Ss Fields
203*1eaf0ac3SlogwangEach
204*1eaf0ac3Slogwang.Vt "struct memory_type"
205*1eaf0ac3Slogwangholds a description of the memory type, including its name and the allocator
206*1eaf0ac3Slogwangit is managed by, as well as current statistics on use.
207*1eaf0ac3SlogwangSome statistics are directly measured, others are derived from directly
208*1eaf0ac3Slogwangmeasured statistics.
209*1eaf0ac3SlogwangCertain high level statistics are present across all available allocators,
210*1eaf0ac3Slogwangsuch as the number of allocation and free operations; other measurements,
211*1eaf0ac3Slogwangsuch as the quantity of free items in per-CPU caches, or administrative
212*1eaf0ac3Slogwanglimit on the number of allocations, is available only for specific
213*1eaf0ac3Slogwangallocators.
214*1eaf0ac3Slogwang.Ss Caller Vt memory_type Ss Fields
215*1eaf0ac3Slogwang.Vt "struct memory_type"
216*1eaf0ac3Slogwangincludes fields to allow the application to store data, in the form of
217*1eaf0ac3Slogwangpointers and 64-bit integers, with memory types.
218*1eaf0ac3SlogwangFor example, the application author might make use of one of the caller
219*1eaf0ac3Slogwangpointers to reference a more complex data structure tracking long-term
220*1eaf0ac3Slogwangbehavior of the memory type, or a window system object that is used to
221*1eaf0ac3Slogwangrender the state of the memory type.
222*1eaf0ac3SlogwangGeneral and per-CPU storage is provided with each
223*1eaf0ac3Slogwang.Vt "struct memory_type"
224*1eaf0ac3Slogwangin the form of an array of pointers and integers.
225*1eaf0ac3SlogwangThe array entries are accessed via the
226*1eaf0ac3Slogwang.Fa index
227*1eaf0ac3Slogwangargument to the get and set accessor methods.
228*1eaf0ac3SlogwangPossible values of
229*1eaf0ac3Slogwang.Fa index
230*1eaf0ac3Slogwangrange between
231*1eaf0ac3Slogwang0
232*1eaf0ac3Slogwangand
233*1eaf0ac3Slogwang.Dv MEMSTAT_MAXCALLER .
234*1eaf0ac3Slogwang.Pp
235*1eaf0ac3SlogwangCaller-owned fields are initialized to
236*1eaf0ac3Slogwang0
237*1eaf0ac3Slogwangor
238*1eaf0ac3Slogwang.Dv NULL
239*1eaf0ac3Slogwangwhen a new
240*1eaf0ac3Slogwang.Vt "struct memory_type"
241*1eaf0ac3Slogwangis allocated and attached to a memory type list; these fields retain their
242*1eaf0ac3Slogwangvalues across queries that update library-owned fields.
243*1eaf0ac3Slogwang.Ss Allocator Types
244*1eaf0ac3SlogwangCurrently,
245*1eaf0ac3Slogwang.Nm
246*1eaf0ac3Slogwangsupports two kernel allocators:
247*1eaf0ac3Slogwang.Dv ALLOCATOR_UMA
248*1eaf0ac3Slogwangfor
249*1eaf0ac3Slogwang.Xr uma 9 ,
250*1eaf0ac3Slogwangand
251*1eaf0ac3Slogwang.Dv ALLOCATOR_MALLOC
252*1eaf0ac3Slogwangfor
253*1eaf0ac3Slogwang.Xr malloc 9 .
254*1eaf0ac3SlogwangThese values may be passed to
255*1eaf0ac3Slogwang.Fn memstat_mtl_find ,
256*1eaf0ac3Slogwangand will be returned by
257*1eaf0ac3Slogwang.Fn memstat_get_allocator .
258*1eaf0ac3SlogwangTwo additional constants in the allocator name space are defined:
259*1eaf0ac3Slogwang.Dv ALLOCATOR_UNKNOWN ,
260*1eaf0ac3Slogwangwhich will only be returned as a result of a library error, and
261*1eaf0ac3Slogwang.Dv ALLOCATOR_ANY ,
262*1eaf0ac3Slogwangwhich can be used to specify that returning types matching any allocator is
263*1eaf0ac3Slogwangpermittable from
264*1eaf0ac3Slogwang.Fn memstat_mtl_find .
265*1eaf0ac3Slogwang.Ss Access Method List
266*1eaf0ac3SlogwangThe following accessor methods are defined, of which some will be valid for
267*1eaf0ac3Slogwanga given memory type:
268*1eaf0ac3Slogwang.Bl -tag -width indent
269*1eaf0ac3Slogwang.It Fn memstat_get_name
270*1eaf0ac3SlogwangReturn a pointer to the name of the memory type.
271*1eaf0ac3SlogwangMemory for the name is owned by
272*1eaf0ac3Slogwang.Nm
273*1eaf0ac3Slogwangand will be valid through a call to
274*1eaf0ac3Slogwang.Fn memstat_mtl_free .
275*1eaf0ac3SlogwangNote that names will be unique with respect to a single allocator, but that
276*1eaf0ac3Slogwangthe same name might be used by different memory types owned by different
277*1eaf0ac3Slogwangmemory allocators.
278*1eaf0ac3Slogwang.It Fn memstat_get_allocator
279*1eaf0ac3SlogwangReturn an integer identifier for the memory allocator that owns the memory
280*1eaf0ac3Slogwangtype.
281*1eaf0ac3Slogwang.It Fn memstat_get_countlimit
282*1eaf0ac3SlogwangIf the memory type has an administrative limit on the number of simultaneous
283*1eaf0ac3Slogwangallocations, return it.
284*1eaf0ac3Slogwang.It Fn memstat_get_byteslimit
285*1eaf0ac3SlogwangIf the memory type has an administrative limit on the number of bytes of
286*1eaf0ac3Slogwangmemory that may be simultaneously allocated for the memory type, return it.
287*1eaf0ac3Slogwang.It Fn memstat_get_sizemask
288*1eaf0ac3SlogwangIf the memory type supports variable allocation sizes, return a bitmask of
289*1eaf0ac3Slogwangsizes allocated for the memory type.
290*1eaf0ac3Slogwang.It Fn memstat_get_size
291*1eaf0ac3SlogwangIf the memory type supports a fixed allocation size, return that size.
292*1eaf0ac3Slogwang.It Fn memstat_get_rsize
293*1eaf0ac3SlogwangIf the memory type supports a fixed allocation size, return real size
294*1eaf0ac3Slogwangof an allocation.
295*1eaf0ac3SlogwangReal size can exceed requested size due to alignment constraints or
296*1eaf0ac3Slogwangimplicit padding.
297*1eaf0ac3Slogwang.It Fn memstat_get_memalloced
298*1eaf0ac3SlogwangReturn the total number of bytes allocated for the memory type over its
299*1eaf0ac3Slogwanglifetime.
300*1eaf0ac3Slogwang.It Fn memstat_get_memfreed
301*1eaf0ac3SlogwangReturn the total number of bytes freed for the memory type over its lifetime.
302*1eaf0ac3Slogwang.It Fn memstat_get_numallocs
303*1eaf0ac3SlogwangReturn the total number of allocations for the memory type over its lifetime.
304*1eaf0ac3Slogwang.It Fn memstat_get_numfrees
305*1eaf0ac3SlogwangReturn the total number of frees for the memory type over its lifetime.
306*1eaf0ac3Slogwang.It Fn memstat_get_bytes
307*1eaf0ac3SlogwangReturn the current number of bytes allocated to the memory type.
308*1eaf0ac3Slogwang.It Fn memstat_get_count
309*1eaf0ac3SlogwangReturn the current number of allocations for the memory type.
310*1eaf0ac3Slogwang.It Fn memstat_get_free
311*1eaf0ac3SlogwangIf the memory allocator supports a cache, return the number of items in the
312*1eaf0ac3Slogwangcache.
313*1eaf0ac3Slogwang.It Fn memstat_get_failures
314*1eaf0ac3SlogwangIf the memory allocator and type permit allocation failures, return the
315*1eaf0ac3Slogwangnumber of allocation failures measured.
316*1eaf0ac3Slogwang.It Fn memstat_get_caller_pointer
317*1eaf0ac3SlogwangReturn a caller-owned pointer for the memory type.
318*1eaf0ac3Slogwang.It Fn memstat_set_caller_pointer
319*1eaf0ac3SlogwangSet a caller-owned pointer for the memory type.
320*1eaf0ac3Slogwang.It Fn memstat_get_caller_uint64
321*1eaf0ac3SlogwangReturn a caller-owned integer for the memory type.
322*1eaf0ac3Slogwang.It Fn memstat_set_caller_uint64
323*1eaf0ac3SlogwangSet a caller-owned integer for the memory type.
324*1eaf0ac3Slogwang.It Fn memstat_get_zonefree
325*1eaf0ac3SlogwangIf the memory allocator supports a multi-level allocation structure, return
326*1eaf0ac3Slogwangthe number of cached items in the zone.
327*1eaf0ac3SlogwangThese items will be in a fully constructed state available for immediate
328*1eaf0ac3Slogwanguse.
329*1eaf0ac3Slogwang.It Fn memstat_get_kegfree
330*1eaf0ac3SlogwangIf the memory allocator supports a multi-level allocation structure, return
331*1eaf0ac3Slogwangthe number of cached items in the keg.
332*1eaf0ac3SlogwangThese items may be in a partially constructed state, and may require further
333*1eaf0ac3Slogwangprocessing before they can be made available for use.
334*1eaf0ac3Slogwang.It Fn memstat_get_percpu_memalloced
335*1eaf0ac3SlogwangIf the memory allocator supports per-CPU statistics, return the number of
336*1eaf0ac3Slogwangbytes of memory allocated for the memory type on the CPU over its lifetime.
337*1eaf0ac3Slogwang.It Fn memstat_get_percpu_memfreed
338*1eaf0ac3SlogwangIf the memory allocator supports per-CPU statistics, return the number of
339*1eaf0ac3Slogwangbytes of memory freed from the memory type on the CPU over its lifetime.
340*1eaf0ac3Slogwang.It Fn memstat_get_percpu_numallocs
341*1eaf0ac3SlogwangIf the memory allocator supports per-CPU statistics, return the number of
342*1eaf0ac3Slogwangallocations for the memory type on the CPU over its lifetime.
343*1eaf0ac3Slogwang.It Fn memstat_get_percpu_numfrees
344*1eaf0ac3SlogwangIf the memory allocator supports per-CPU statistics, return the number of
345*1eaf0ac3Slogwangfrees for the memory type on the CPU over its lifetime.
346*1eaf0ac3Slogwang.It Fn memstat_get_percpu_sizemask
347*1eaf0ac3SlogwangIf the memory allocator supports variable size memory allocation and per-CPU
348*1eaf0ac3Slogwangstatistics, return the size bitmask for the memory type on the CPU.
349*1eaf0ac3Slogwang.It Fn memstat_get_percpu_caller_pointer
350*1eaf0ac3SlogwangReturn a caller-owned per-CPU pointer for the memory type.
351*1eaf0ac3Slogwang.It Fn memstat_set_percpu_caller_pointer
352*1eaf0ac3SlogwangSet a caller-owned per-CPU pointer for the memory type.
353*1eaf0ac3Slogwang.It Fn memstat_get_percpu_caller_uint64
354*1eaf0ac3SlogwangReturn a caller-owned per-CPU integer for the memory type.
355*1eaf0ac3Slogwang.It Fn memstat_set_percpu_caller_uint64
356*1eaf0ac3SlogwangSet a caller-owned per-CPU integer for the memory type.
357*1eaf0ac3Slogwang.It Fn memstat_get_percpu_free
358*1eaf0ac3SlogwangIf the memory allocator supports a per-CPU cache, return the number of free
359*1eaf0ac3Slogwangitems in the per-CPU cache of the designated CPU.
360*1eaf0ac3Slogwang.El
361*1eaf0ac3Slogwang.Sh RETURN VALUES
362*1eaf0ac3Slogwang.Nm
363*1eaf0ac3Slogwangfunctions fall into three categories: functions returning a pointer to an
364*1eaf0ac3Slogwangobject, functions returning an integer return value, and functions
365*1eaf0ac3Slogwangimplementing accessor methods returning data from a
366*1eaf0ac3Slogwang.Vt "struct memory_type" .
367*1eaf0ac3Slogwang.Pp
368*1eaf0ac3SlogwangFunctions returning a pointer to an object will generally return
369*1eaf0ac3Slogwang.Dv NULL
370*1eaf0ac3Slogwangon failure.
371*1eaf0ac3Slogwang.Fn memstat_mtl_alloc
372*1eaf0ac3Slogwangwill return an error value via
373*1eaf0ac3Slogwang.Va errno ,
374*1eaf0ac3Slogwangwhich will consist of the value
375*1eaf0ac3Slogwang.Er ENOMEM .
376*1eaf0ac3SlogwangFunctions
377*1eaf0ac3Slogwang.Fn memstat_mtl_first ,
378*1eaf0ac3Slogwang.Fn memstat_mtl_next ,
379*1eaf0ac3Slogwangand
380*1eaf0ac3Slogwang.Fn memstat_mtl_find
381*1eaf0ac3Slogwangwill return
382*1eaf0ac3Slogwang.Dv NULL
383*1eaf0ac3Slogwangwhen there is no entry or match in the list; however, this is not considered
384*1eaf0ac3Slogwanga failure mode and no error value is available.
385*1eaf0ac3Slogwang.Pp
386*1eaf0ac3SlogwangFunctions returning an integer success value will return
387*1eaf0ac3Slogwang0
388*1eaf0ac3Slogwangon success, or
389*1eaf0ac3Slogwang\-1
390*1eaf0ac3Slogwangon failure.
391*1eaf0ac3SlogwangIf a failure is returned, the list error access method,
392*1eaf0ac3Slogwang.Fn memstat_mtl_geterror ,
393*1eaf0ac3Slogwangmay be used to retrieve the error state.
394*1eaf0ac3SlogwangThe string representation of the error may be retrieved using
395*1eaf0ac3Slogwang.Fn memstat_strerror .
396*1eaf0ac3SlogwangPossible error values are:
397*1eaf0ac3Slogwang.Bl -tag -width ".Dv MEMSTAT_ERROR_KVM_SHORTREAD"
398*1eaf0ac3Slogwang.It Dv MEMSTAT_ERROR_UNDEFINED
399*1eaf0ac3SlogwangUndefined error.
400*1eaf0ac3SlogwangOccurs if
401*1eaf0ac3Slogwang.Fn memstat_mtl_geterror
402*1eaf0ac3Slogwangis called on a list before an error associated with the list has occurred.
403*1eaf0ac3Slogwang.It Dv MEMSTAT_ERROR_NOMEMORY
404*1eaf0ac3SlogwangInsufficient memory.
405*1eaf0ac3SlogwangOccurs if library calls to
406*1eaf0ac3Slogwang.Xr malloc 3
407*1eaf0ac3Slogwangfail, or if a system call to retrieve kernel statistics fails with
408*1eaf0ac3Slogwang.Er ENOMEM .
409*1eaf0ac3Slogwang.It Dv MEMSTAT_ERROR_VERSION
410*1eaf0ac3SlogwangReturned if the current version of
411*1eaf0ac3Slogwang.Nm
412*1eaf0ac3Slogwangis unable to interpret the statistics data returned by the kernel due to an
413*1eaf0ac3Slogwangexplicit version mismatch, or to differences in data structures that cannot
414*1eaf0ac3Slogwangbe reconciled.
415*1eaf0ac3Slogwang.It Dv MEMSTAT_ERROR_PERMISSION
416*1eaf0ac3SlogwangReturned if a statistics source returns
417*1eaf0ac3Slogwang.Va errno
418*1eaf0ac3Slogwangvalues of
419*1eaf0ac3Slogwang.Er EACCES
420*1eaf0ac3Slogwangor
421*1eaf0ac3Slogwang.Er EPERM .
422*1eaf0ac3Slogwang.It Dv MEMSTAT_ERROR_DATAERROR
423*1eaf0ac3SlogwangReturned if
424*1eaf0ac3Slogwang.Nm
425*1eaf0ac3Slogwangis unable to interpret statistics data returned by the data source, even
426*1eaf0ac3Slogwangthough there does not appear to be a version problem.
427*1eaf0ac3Slogwang.It Dv MEMSTAT_ERROR_KVM
428*1eaf0ac3SlogwangReturned if
429*1eaf0ac3Slogwang.Nm
430*1eaf0ac3Slogwangexperiences an error while using
431*1eaf0ac3Slogwang.Xr kvm 3
432*1eaf0ac3Slogwanginterfaces to query statistics data.
433*1eaf0ac3SlogwangUse
434*1eaf0ac3Slogwang.Xr kvm_geterr 3
435*1eaf0ac3Slogwangto retrieve the error.
436*1eaf0ac3Slogwang.It Dv MEMSTAT_ERROR_KVM_NOSYMBOL
437*1eaf0ac3SlogwangReturned if
438*1eaf0ac3Slogwang.Nm
439*1eaf0ac3Slogwangis unable to read a required symbol from the kernel being operated on.
440*1eaf0ac3Slogwang.It Dv MEMSTAT_ERROR_KVM_SHORTREAD
441*1eaf0ac3SlogwangReturned if
442*1eaf0ac3Slogwang.Nm
443*1eaf0ac3Slogwangattempts to read data from a live memory image or kernel core dump and
444*1eaf0ac3Slogwanginsufficient data is returned.
445*1eaf0ac3Slogwang.El
446*1eaf0ac3Slogwang.Pp
447*1eaf0ac3SlogwangFinally, functions returning data from a
448*1eaf0ac3Slogwang.Vt "struct memory_type"
449*1eaf0ac3Slogwangpointer are not permitted to fail, and directly return either a statistic
450*1eaf0ac3Slogwangor pointer to a string.
451*1eaf0ac3Slogwang.Sh EXAMPLES
452*1eaf0ac3SlogwangCreate a memory type list, query the
453*1eaf0ac3Slogwang.Xr uma 9
454*1eaf0ac3Slogwangmemory allocator for available statistics, and print out the number of
455*1eaf0ac3Slogwangallocations performed by the
456*1eaf0ac3Slogwang.Dv mbuf
457*1eaf0ac3Slogwangzone.
458*1eaf0ac3Slogwang.Bd -literal -offset indent
459*1eaf0ac3Slogwangstruct memory_type_list *mtlp;
460*1eaf0ac3Slogwangstruct memory_type *mtp;
461*1eaf0ac3Slogwanguint64_t mbuf_count;
462*1eaf0ac3Slogwang
463*1eaf0ac3Slogwangmtlp = memstat_mtl_alloc();
464*1eaf0ac3Slogwangif (mtlp == NULL)
465*1eaf0ac3Slogwang    err(-1, "memstat_mtl_alloc");
466*1eaf0ac3Slogwangif (memstat_sysctl_uma(mtlp, 0) < 0)
467*1eaf0ac3Slogwang    err(-1, "memstat_sysctl_uma");
468*1eaf0ac3Slogwangmtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, "mbuf");
469*1eaf0ac3Slogwangif (mtp == NULL)
470*1eaf0ac3Slogwang    errx(-1, "memstat_mtl_find: mbuf not found");
471*1eaf0ac3Slogwangmbuf_count = memstat_get_count(mtp);
472*1eaf0ac3Slogwangmemstat_mtl_free(mtlp);
473*1eaf0ac3Slogwang
474*1eaf0ac3Slogwangprintf("mbufs: %llu\en", (unsigned long long)mbuf_count);
475*1eaf0ac3Slogwang.Ed
476*1eaf0ac3Slogwang.Sh SEE ALSO
477*1eaf0ac3Slogwang.Xr malloc 9 ,
478*1eaf0ac3Slogwang.Xr uma 9
479*1eaf0ac3Slogwang.Sh HISTORY
480*1eaf0ac3SlogwangThe
481*1eaf0ac3Slogwang.Nm
482*1eaf0ac3Slogwanglibrary appeared in
483*1eaf0ac3Slogwang.Fx 6.0 .
484*1eaf0ac3Slogwang.Sh AUTHORS
485*1eaf0ac3SlogwangThe kernel memory allocator changes necessary to support a general purpose
486*1eaf0ac3Slogwangmonitoring library, along with the library, were written by
487*1eaf0ac3Slogwang.An Robert Watson Aq Mt [email protected] .
488*1eaf0ac3Slogwang.Sh BUGS
489*1eaf0ac3SlogwangThere are memory allocators in the kernel, such as the VM page allocator
490*1eaf0ac3Slogwangand
491*1eaf0ac3Slogwang.Nm sf_buf
492*1eaf0ac3Slogwangallocator, which are not currently supported by
493*1eaf0ac3Slogwang.Nm .
494*1eaf0ac3Slogwang.Pp
495*1eaf0ac3SlogwangOnce a memory type is present on a memory type list, it will not be removed
496*1eaf0ac3Slogwangeven if the kernel no longer presents information on the type via its
497*1eaf0ac3Slogwangmonitoring interfaces.
498*1eaf0ac3SlogwangIn order to flush removed memory types, it is necessary to free the entire
499*1eaf0ac3Slogwanglist and allocate a new one.
500