xref: /freebsd-13.1/lib/libmemstat/libmemstat.3 (revision 70ee997e)
1.\" Copyright (c) 2005 Robert N. M. Watson
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd June 27, 2005
28.Os
29.Dt LIBMEMSTAT 3
30.Sh NAME
31.Nm libmemstat
32.Nd "library interface to retrieve kernel memory allocator statistics"
33.Sh LIBRARY
34.Lb libmemstat
35.Sh SYNOPSIS
36.In sys/types.h
37.In memstat.h
38.Ss Memory Type List Management Functions
39.Ft struct memory_type_list *
40.Fn memstat_mtl_alloc "void"
41.Ft struct memory_type *
42.Fn memstat_mtl_first "struct memory_type_list *list"
43.Ft struct memory_type *
44.Fn memstat_mtl_next "struct memory_type *mtp"
45.Ft struct memory_type *
46.Fn memstat_mtl_find "struct memory_type_list *list" "int allocator" "const char *name"
47.Ft void
48.Fn memstat_mtl_free "struct memory_type_list *list"
49.Ft int
50.Fn memstat_mtl_geterror "struct memory_type_list *list"
51.Ss Allocator Query Functions
52.Ft int
53.Fn memstat_sysctl_all "struct memory_type_list *list" "int flags"
54.Ft int
55.Fn memstat_sysctl_malloc "struct memory_type_list *list" "int flags"
56.Ft int
57.Fn memstat_sysctl_uma "struct memory_type_list *list" "int flags"
58.Ss Memory Type Accessor Methods
59.Ft const char *
60.Fn memstat_get_name "const struct memory_type *mtp"
61.Ft int
62.Fn memstat_get_allocator "const struct memory_type *mtp"
63.Ft uint64_t
64.Fn memstat_get_countlimit "const struct memory_type *mtp"
65.Ft uint64_t
66.Fn memstat_get_byteslimit "const struct memory_type *mtp"
67.Ft uint64_t
68.Fn memstat_get_sizemask "const struct memory_type *mtp"
69.Ft uint64_t
70.Fn memstat_get_size "const struct memory_type *mtp"
71.Ft uint64_t
72.Fn memstat_get_memalloced "const struct memory_type *mtp"
73.Ft uint64_t
74.Fn memstat_get_memfreed "const struct memory_type *mtp"
75.Ft uint64_t
76.Fn memstat_get_numallocs "const struct memory_type *mtp"
77.Ft uint64_t
78.Fn memstat_get_numfrees "const struct memory_type *mtp"
79.Ft uint64_t
80.Fn memstat_get_bytes "const struct memory_type *mtp"
81.Ft uint64_t
82.Fn memstat_get_count "const struct memory_type *mtp"
83.Ft uint64_t
84.Fn memstat_get_free "const struct memory_type *mtp"
85.Ft uint64_t
86.Fn memstat_get_failures "const struct memory_type *mtp"
87.Ft void *
88.Fn memstat_get_caller_pointer "const struct memory_type *mtp" "int index"
89.Ft void
90.Fn memstat_set_caller_pointer "struct memory_type *mtp" "int index" "void *value"
91.Ft uint64_t
92.Fn memstat_get_caller_uint64 "const struct memory_type *mtp" "int index"
93.Ft void
94.Fn memstat_set_caller_uint64 "struct memory_type *mtp" "int index" "uint64_t value"
95.Ft uint64_t
96.Fn memstat_get_zonefree "const struct memory_type *mtp"
97.Ft uint64_t
98.Fn memstat_get_kegfree "const struct memory_type *mtp"
99.Ft uint64_t
100.Fn memstat_get_percpu_memalloced "const struct memory_type *mtp" "int cpu"
101.Ft uint64_t
102.Fn memstat_get_percpu_memfreed "const struct memory_type *mtp" "int cpu"
103.Ft uint64_t
104.Fn memstat_get_percpu_numallocs "const struct memory_type *mtp" "int cpu"
105.Ft uint64_t
106.Fn memstat_get_percpu_numfrees "const struct memory_type *mtp" "int cpu"
107.Ft uint64_t
108.Fn memstat_get_percpu_sizemask "const struct memory_type *mtp" "int cpu"
109.Ft void *
110.Fn memstat_get_percpu_caller_pointer "const struct memory_type *mtp" "int cpu" "int index"
111.Ft void
112.Fn memstat_set_percpu_caller_pointer "struct memory_type *mtp" "int cpu" "int index" "void *value"
113.Ft uint64_t
114.Fn memstat_get_percpu_caller_uint64 "const struct memory_type *mtp" "int cpu" "int index"
115.Ft void
116.Fn memstat_set_percpu_caller_uint64 "struct memory_type *mtp" "int cpu" "int index" "uint64_t value"
117.Ft uint64_t
118.Fn memstat_get_percpu_free "const struct memory_type *mtp" "int cpu"
119.Sh DESCRIPTION
120.Nm
121provides an interface to retrieve kernel memory allocator statistics, for
122the purposes of debugging and system monitoring, insulating applications
123from implementation details of the allocators, and allowing a tool to
124transparently support multiple allocators.
125.Nm
126supports both retrieving a single statistics snapshot, as well as
127incrementally updating statistics for long-term monitoring.
128.Pp
129.Nm
130describes each memory type using a
131.Vt struct memory_type ,
132an opaque memory type accessed by the application using accessor functions
133in the library.
134.Nm
135returns and updates chains of
136.Vt struct memory_type
137via a
138.Vt struct memory_type_list ,
139which will be allocated by calling
140.Fn memstat_mtl_alloc ,
141and freed on completion using
142.Fn memstat_mtl_free .
143Lists of memory types are populated via calls that query the kernel for
144statistics information; currently:
145.Fn memstat_sysctl_all ,
146.Fn memstat_sysctl_uma ,
147and
148.Fn memstat_sysctl_malloc .
149Repeated calls will incrementally update the list of memory types, permitting
150tracking over time without recreating all list state.
151If an error is detected during a query call, error condition information may
152be retrieved using
153.Fn memstat_mtl_geterror .
154.Pp
155Freeing the list will free all memory type data in the list, and so
156invalidates any outstanding pointers to entries in the list.
157.Vt struct memory_type
158entries in the list may be iterated over using
159.Fn memstat_mtl_first
160and
161.fn memstat_mtl_next ,
162which respectively return the first entry in a list, and the next entry in a
163list.
164.Fn memstat_mtl_find ,
165which will return a pointer to the first entry matching the passed
166parameters.
167.Pp
168A series of accessor methods is provided to access fields of the structure,
169including retrieving statistics and properties, as well as setting of caller
170owned fields.
171Direct application access to the data structure fields is not supported.
172.Ss Library memory_type Fields
173Each
174.Vt struct memory_type
175holds a description of the memory type, including its name and the allocator
176it is managed by, as well as current statistics on use.
177Some statistics are directly measured, others are derived from directly
178measured statistics.
179Certain high level statistics are present across all available allocators,
180such as the number of allocation and free operations; other measurements,
181such as the quantity of free items in per-CPU caches, or administrative
182limit on the number of allocations, is available only for specific
183allocators.
184.Ss Caller memory_type Fields
185.Vt struct memory_type
186includes fields to allow the application to store data, in the form of
187pointers and 64-bit integers, with memory types.
188For example, the application author might make use of one of the caller
189pointers to reference a more complex data structure tracking long-term
190behavior of the memory type, or a window system object that is used to
191render the state of the memory type.
192General and per-CPU storage is provided with each
193.Vt struct memory_type
194in the form of an array of pointers and integers.
195The array entries are accessed via the
196.Fa index
197argument to the get and set accessor methods.
198Possible values of
199.Fa index
200range between
201.Dv 0
202and
203.Dv MEMSTAT_MAXCALLER .
204.Pp
205Caller-owned fields are initialized to
206.Dv 0
207or
208.Dv NULL
209when a new
210.Vt struct memory_type
211is allocated and attached to a memory type list; these fields retain their
212values across queries that update library-owned fields.
213.Ss Allocator Types
214Currently,
215.Nm
216supports two kernel allocators:
217.Dv ALLOCATOR_UMA
218for
219.Xr uma 9 ,
220and
221.Dv ALLOCATOR_MALLOC
222for
223.Xr malloc 9 .
224These values may be passed to
225.Fn memstat_mtl_find ,
226and will be returned by
227.Fn memstat_get_allocator .
228Two additional constants in the allocator name space are defined:
229.Dv ALLOCATOR_UNKNOWN ,
230which will only be returned as a result of a library error, and
231.Dv ALLOCATOR_ANY ,
232which can be used to specify that returning types matching any allocator is
233permittible from
234.Fn memstat_mtl_find .
235.Ss Access Method List
236The following accessor methods are defined, of which some will be valid for
237a given memory type:
238.Pp
239.Bl -tag -width "memstat_get_name" -compact -offset wee
240.It memstat_get_name
241Return a pointer to the name of the memory type.
242Memory for the name is owned by
243.Nm
244and will be valid through a call to
245.Fn memstat_mtl_free .
246Note that names will be unique with respect to a single allocator, but that
247the same name might be used by different memory types owned by different
248memory allocators.
249.It memstat_get_allocator
250Return an integer identifier for the memory allocator that owns the memory
251type.
252.It memstat_get_countlimit
253If the memory type has an administrative limit on the number of simultaneous
254allocations, return it.
255.It memstat_get_byteslimit
256If the memory type has an administrative limit on the number of bytes of
257memory that may be simultaenously allocated for the memory type, return it.
258.It memstat_get_sizemask
259If the memory type supports variable allocation sizes, return a bitmask of
260sizes allocated for the memory type.
261.It memstat_get_size
262If the memory type supports a fixed allocation size, return that size.
263.It memstat_get_memalloced
264Return the total number of bytes allocated for the memory type over its
265lifetime.
266.It memstat_get_memfreed
267Return the total number of bytes freed for the memory type over its lifetime.
268.It memstat_get_numallocs
269Return the total number of allocations for the memory type over its lifetime.
270.It memstat_get_numfrees
271Return the total number of frees for the memory type over its lifetime.
272.It memstat_get_bytes
273Return the current number of bytes allocated to the memory type.
274.It memstat_get_count
275Return the current number of allocations for the memory type.
276.It memstat_get_free
277If the memory allocator supports a cache, return the number of items in the
278cache.
279.It memstat_get_failures
280If the memory allocator and type permit allocation failures, return the
281number of allocation failures measured.
282.It memstat_get_caller_pointer
283Return a caller-owned pointer for the memory type.
284.It memstat_set_caller_pointer
285Set a caller-owned pointer for the memory type.
286.It memstat_get_caller_uint64
287Return a caller-owned integer for the memory type.
288.It memstat_set_caller_uint64
289Set a caller-owned integer for the memory type.
290.It memstat_get_zonefree
291If the memory allocator supports a multi-level allocation structure, return
292the number of cached items in the zone.
293These items will be in a fully constructed state available for immediate
294use.
295.It memstat_get_kegfree
296If the memory allocator supports a multi-level allocation structure, return
297the number of cached items in the keg.
298These items may be in a partially constructed state, and may require further
299processing before they can be made available for use.
300.It memstat_get_percpu_memalloced
301If the memory allocator supports per-CPU statistics, return the number of
302bytes of memory allocated for the memory type on the CPU over its lifetime.
303.It memstat_get_percpu_memfreed
304If the memory allocator supports per-CPU statistics, return the number of
305bytes of memory freed from the memory type on the CPU over its lifetime.
306.It memstat_get_percpu_numallocs
307If the memory allocator supports per-CPU statistics, return the number of
308allocations for the memory type on the CPU over its lifetime.
309.It memstat_get_percpu_numfrees
310If the memory allocator supports per-CPU statistics, return the number of
311frees for the memory type on the CPU over its lifetime.
312.It memstat_get_percpu_sizemask
313If the memory allocator supports variable size memory allocation and per-CPU
314statistics, return the size bitmask for the memory type on the CPU.
315.It memstat_get_percpu_caller_pointer
316Return a caller-owned per-CPU pointer for the memory type.
317.It memstat_set_percpu_caller_pointer
318Set a caller-owned per-CPU pointer for the memory type.
319.It memstat_get_percpu_caller_uint64
320Return a caller-owned per-CPU integer for the memory type.
321.It memsttat_set_percpu_caller_uint64
322Set a caller-owned per-CPU integer for the memory type.
323.It memstat_get_percpu_free
324If the memory allocator supports a per-CPU cache, return the number of free
325items in the per-CPU cache of the designated CPU.
326.El
327.Sh RETURN VALUES
328.Nm
329functions fall into three categories: functions returning a pointer to an
330object, functions returning an integer return value, and functions
331implementing accessor methods returning data from a
332.Vt struct memory_type .
333.Pp
334Functions returning a pointer to an object will generally return
335.Dv NULL
336on failure.
337.Fn memstat_mtl_alloc
338will return an error value via
339.Va errno ,
340which will consist of the value
341.Dv ENOMEM .
342Functions
343.Fn memstat_mtl_first ,
344.Fn memstat_mtl_next ,
345and
346.Fn memstat_mtl_find
347will return
348.Dv NULL
349when there is no entry or match in the list; however, this is not considered
350a failure mode and no error value is available.
351.Pp
352Functions returning a integer success valuye will return
353.Dv 0
354on success, or
355.Dv -1
356on failure.
357If a failure is returned, the list error access method,
358.Fn memstat_mtl_geterror ,
359may be used to retrieve the error state.  Possible error values are:
360.Pp
361.Bl -tag -width "MEMSTAT_ERROR_TOOMANYCPUS" -compact -offset wee
362.It Dv MEMSTAT_ERROR_UNDEFINED
363Undefined error.  Occurs if
364.Fn memstat_mtl_geterror
365is called on a list before an error associated with the list has occurred.
366.It Dv MEMSTAT_ERROR_NOMEMORY
367Insufficient memory.  Occurs if library calls to
368.Xr malloc 3
369fail, or if a system call to retrieve kernel statistics fails with
370.Er ENOMEM .
371.It Dv MEMSTAT_ERROR_VERSION
372Returned if the current version of
373.Nm
374is unable to interpret the statistics data returned by the kernel due to an
375explicit version mismatch, or to differences in data structures that cannot
376be reconciled.
377.It Dv MEMSTAT_ERROR_PERMISSION
378Returned if a statistics source returns
379.Va errno
380values of
381.Dv EACCES
382or
383.Dv EPERM .
384.It Dv MEMSTAT_ERROR_TOOMANYCPUS
385Returned if the compile-time limit on the number of CPUs in
386.Nm
387is lower than the number of CPUs returned by a statistics data source.
388.It Dv MEMSTAT_ERROR_DATAERROR
389Returned if
390.Nm
391is unable to interpret statistics data returned by the data source, even
392though there does not appear to be a version problem.
393.El
394.Pp
395Finally, functions returning data from a
396.Dt struct memory_type
397pointer are not permitted to fail, and directly return either a statistic
398or pointer to a string.
399.Sh EXAMPLES
400Create a memory type list, query the
401.Xr uma 9
402memory allocator for available statistics, and print out the number of
403allocations performed by the
404.Dv Mbuf
405zone.
406.Bd -literal -offset indent
407struct memory_type_list *mtlp;
408struct memory_type *mtp;
409uint64_t mbuf_count;
410
411mtlp = memstat_mtl_alloc();
412if (mtlp == NULL)
413    err(-1, "memstat_mtl_alloc");
414if (memstat_sysctl_uma(mtlp, 0) < 0)
415    err(-1, "memstat_sysctl_uma");
416mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, "Mbuf");
417if (mtp == NULL)
418    errx(-1, "memstat_mtl_find: Mbuf not found");
419mbuf_count = memstat_get_count(mtp);
420memstat_mtl_free(mtlp);
421
422printf("Mbufs: %llu\\n", (unsigned long long)mbuf_count);
423.Ed
424.Sh SEE ALSO
425.Xr malloc 9 ,
426.Xr uma 9
427.Sh HISTORY
428The
429.Nm memstat
430library appeared in
431.Fx 6.0 .
432.Sh AUTHORS
433The kernel memory allocator changes necessary to support a general purpose
434monitoring library, along with the library, were written by
435.An Robert Watson Aq [email protected]
436.Sh BUGS
437.Nm
438cannot yet extract statistics from kernel core dumps, although this should be
439straight forward to implement.
440.Pp
441Once a memory type is present on a memory type list, it will not be removed
442even if the kernel no longer presents information on the type via its
443monitoring interfaces.
444In order to flush removed memory types, it is necessary to free the entire
445list and allocate a new one.
446