xref: /freebsd/lib/libmemstat/libmemstat.3 (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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.Dd February 11, 2014
26.Dt LIBMEMSTAT 3
27.Os
28.Sh NAME
29.Nm libmemstat
30.Nd "library interface to retrieve kernel memory allocator statistics"
31.Sh LIBRARY
32.Lb libmemstat
33.Sh SYNOPSIS
34.In sys/types.h
35.In memstat.h
36.Ss General Functions
37.Ft "const char *"
38.Fn memstat_strerror "int error"
39.Ss Memory Type List Management Functions
40.Ft "struct memory_type_list *"
41.Fn memstat_mtl_alloc "void"
42.Ft "struct memory_type *"
43.Fn memstat_mtl_first "struct memory_type_list *list"
44.Ft "struct memory_type *"
45.Fn memstat_mtl_next "struct memory_type *mtp"
46.Ft "struct memory_type *"
47.Fo memstat_mtl_find
48.Fa "struct memory_type_list *list" "int allocator" "const char *name"
49.Fc
50.Ft void
51.Fn memstat_mtl_free "struct memory_type_list *list"
52.Ft int
53.Fn memstat_mtl_geterror "struct memory_type_list *list"
54.Ss Allocator Query Functions
55.Ft int
56.Fn memstat_kvm_all "struct memory_type_list *list" "void *kvm_handle"
57.Ft int
58.Fn memstat_kvm_malloc "struct memory_type_list *list" "void *kvm_handle"
59.Ft int
60.Fn memstat_kvm_uma "struct memory_type_list *list" "void *kvm_handle"
61.Ft int
62.Fn memstat_sysctl_all "struct memory_type_list *list" "int flags"
63.Ft int
64.Fn memstat_sysctl_malloc "struct memory_type_list *list" "int flags"
65.Ft int
66.Fn memstat_sysctl_uma "struct memory_type_list *list" "int flags"
67.Ss Memory Type Accessor Methods
68.Ft "const char *"
69.Fn memstat_get_name "const struct memory_type *mtp"
70.Ft int
71.Fn memstat_get_allocator "const struct memory_type *mtp"
72.Ft uint64_t
73.Fn memstat_get_countlimit "const struct memory_type *mtp"
74.Ft uint64_t
75.Fn memstat_get_byteslimit "const struct memory_type *mtp"
76.Ft uint64_t
77.Fn memstat_get_sizemask "const struct memory_type *mtp"
78.Ft uint64_t
79.Fn memstat_get_size "const struct memory_type *mtp"
80.Ft uint64_t
81.Fn memstat_get_rsize "const struct memory_type *mtp"
82.Ft uint64_t
83.Fn memstat_get_memalloced "const struct memory_type *mtp"
84.Ft uint64_t
85.Fn memstat_get_memfreed "const struct memory_type *mtp"
86.Ft uint64_t
87.Fn memstat_get_numallocs "const struct memory_type *mtp"
88.Ft uint64_t
89.Fn memstat_get_numfrees "const struct memory_type *mtp"
90.Ft uint64_t
91.Fn memstat_get_bytes "const struct memory_type *mtp"
92.Ft uint64_t
93.Fn memstat_get_count "const struct memory_type *mtp"
94.Ft uint64_t
95.Fn memstat_get_free "const struct memory_type *mtp"
96.Ft uint64_t
97.Fn memstat_get_failures "const struct memory_type *mtp"
98.Ft "void *"
99.Fn memstat_get_caller_pointer "const struct memory_type *mtp" "int index"
100.Ft void
101.Fo memstat_set_caller_pointer
102.Fa "struct memory_type *mtp" "int index" "void *value"
103.Fc
104.Ft uint64_t
105.Fn memstat_get_caller_uint64 "const struct memory_type *mtp" "int index"
106.Ft void
107.Fo memstat_set_caller_uint64
108.Fa "struct memory_type *mtp" "int index" "uint64_t value"
109.Fc
110.Ft uint64_t
111.Fn memstat_get_zonefree "const struct memory_type *mtp"
112.Ft uint64_t
113.Fn memstat_get_kegfree "const struct memory_type *mtp"
114.Ft uint64_t
115.Fn memstat_get_percpu_memalloced "const struct memory_type *mtp" "int cpu"
116.Ft uint64_t
117.Fn memstat_get_percpu_memfreed "const struct memory_type *mtp" "int cpu"
118.Ft uint64_t
119.Fn memstat_get_percpu_numallocs "const struct memory_type *mtp" "int cpu"
120.Ft uint64_t
121.Fn memstat_get_percpu_numfrees "const struct memory_type *mtp" "int cpu"
122.Ft uint64_t
123.Fn memstat_get_percpu_sizemask "const struct memory_type *mtp" "int cpu"
124.Ft "void *"
125.Fo memstat_get_percpu_caller_pointer
126.Fa "const struct memory_type *mtp" "int cpu" "int index"
127.Fc
128.Ft void
129.Fo memstat_set_percpu_caller_pointer
130.Fa "struct memory_type *mtp" "int cpu" "int index" "void *value"
131.Fc
132.Ft uint64_t
133.Fo memstat_get_percpu_caller_uint64
134.Fa "const struct memory_type *mtp" "int cpu" "int index"
135.Fc
136.Ft void
137.Fo memstat_set_percpu_caller_uint64
138.Fa "struct memory_type *mtp" "int cpu" "int index" "uint64_t value"
139.Fc
140.Ft uint64_t
141.Fn memstat_get_percpu_free "const struct memory_type *mtp" "int cpu"
142.Sh DESCRIPTION
143.Nm
144provides an interface to retrieve kernel memory allocator statistics, for
145the purposes of debugging and system monitoring, insulating applications
146from implementation details of the allocators, and allowing a tool to
147transparently support multiple allocators.
148.Nm
149supports both retrieving a single statistics snapshot, as well as
150incrementally updating statistics for long-term monitoring.
151.Pp
152.Nm
153describes each memory type using a
154.Vt "struct memory_type" ,
155an opaque memory type accessed by the application using accessor functions
156in the library.
157.Nm
158returns and updates chains of
159.Vt "struct memory_type"
160via a
161.Vt "struct memory_type_list" ,
162which will be allocated by calling
163.Fn memstat_mtl_alloc ,
164and freed on completion using
165.Fn memstat_mtl_free .
166Lists of memory types are populated via calls that query the kernel for
167statistics information; currently:
168.Fn memstat_kvm_all ,
169.Fn memstat_kvm_malloc ,
170.Fn memstat_kvm_uma ,
171.Fn memstat_sysctl_all ,
172.Fn memstat_sysctl_uma ,
173and
174.Fn memstat_sysctl_malloc .
175Repeated calls will incrementally update the list of memory types, permitting
176tracking over time without recreating all list state.
177If an error is detected during a query call, error condition information may
178be retrieved using
179.Fn memstat_mtl_geterror ,
180and converted to a user-readable string using
181.Fn memstat_strerror .
182.Pp
183Freeing the list will free all memory type data in the list, and so
184invalidates any outstanding pointers to entries in the list.
185.Vt "struct memory_type"
186entries in the list may be iterated over using
187.Fn memstat_mtl_first
188and
189.Fn memstat_mtl_next ,
190which respectively return the first entry in a list, and the next entry in a
191list.
192.Fn memstat_mtl_find ,
193which will return a pointer to the first entry matching the passed
194parameters.
195.Pp
196A series of accessor methods is provided to access fields of the structure,
197including retrieving statistics and properties, as well as setting of caller
198owned fields.
199Direct application access to the data structure fields is not supported.
200.Ss Library Vt memory_type Ss Fields
201Each
202.Vt "struct memory_type"
203holds a description of the memory type, including its name and the allocator
204it is managed by, as well as current statistics on use.
205Some statistics are directly measured, others are derived from directly
206measured statistics.
207Certain high level statistics are present across all available allocators,
208such as the number of allocation and free operations; other measurements,
209such as the quantity of free items in per-CPU caches, or administrative
210limit on the number of allocations, is available only for specific
211allocators.
212.Ss Caller Vt memory_type Ss Fields
213.Vt "struct memory_type"
214includes fields to allow the application to store data, in the form of
215pointers and 64-bit integers, with memory types.
216For example, the application author might make use of one of the caller
217pointers to reference a more complex data structure tracking long-term
218behavior of the memory type, or a window system object that is used to
219render the state of the memory type.
220General and per-CPU storage is provided with each
221.Vt "struct memory_type"
222in the form of an array of pointers and integers.
223The array entries are accessed via the
224.Fa index
225argument to the get and set accessor methods.
226Possible values of
227.Fa index
228range between
2290
230and
231.Dv MEMSTAT_MAXCALLER .
232.Pp
233Caller-owned fields are initialized to
2340
235or
236.Dv NULL
237when a new
238.Vt "struct memory_type"
239is allocated and attached to a memory type list; these fields retain their
240values across queries that update library-owned fields.
241.Ss Allocator Types
242Currently,
243.Nm
244supports two kernel allocators:
245.Dv ALLOCATOR_UMA
246for
247.Xr uma 9 ,
248and
249.Dv ALLOCATOR_MALLOC
250for
251.Xr malloc 9 .
252These values may be passed to
253.Fn memstat_mtl_find ,
254and will be returned by
255.Fn memstat_get_allocator .
256Two additional constants in the allocator name space are defined:
257.Dv ALLOCATOR_UNKNOWN ,
258which will only be returned as a result of a library error, and
259.Dv ALLOCATOR_ANY ,
260which can be used to specify that returning types matching any allocator is
261permittable from
262.Fn memstat_mtl_find .
263.Ss Access Method List
264The following accessor methods are defined, of which some will be valid for
265a given memory type:
266.Bl -tag -width indent
267.It Fn memstat_get_name
268Return a pointer to the name of the memory type.
269Memory for the name is owned by
270.Nm
271and will be valid through a call to
272.Fn memstat_mtl_free .
273Note that names will be unique with respect to a single allocator, but that
274the same name might be used by different memory types owned by different
275memory allocators.
276.It Fn memstat_get_allocator
277Return an integer identifier for the memory allocator that owns the memory
278type.
279.It Fn memstat_get_countlimit
280If the memory type has an administrative limit on the number of simultaneous
281allocations, return it.
282.It Fn memstat_get_byteslimit
283If the memory type has an administrative limit on the number of bytes of
284memory that may be simultaneously allocated for the memory type, return it.
285.It Fn memstat_get_sizemask
286If the memory type supports variable allocation sizes, return a bitmask of
287sizes allocated for the memory type.
288.It Fn memstat_get_size
289If the memory type supports a fixed allocation size, return that size.
290.It Fn memstat_get_rsize
291If the memory type supports a fixed allocation size, return real size
292of an allocation.
293Real size can exceed requested size due to alignment constraints or
294implicit padding.
295.It Fn memstat_get_memalloced
296Return the total number of bytes allocated for the memory type over its
297lifetime.
298.It Fn memstat_get_memfreed
299Return the total number of bytes freed for the memory type over its lifetime.
300.It Fn memstat_get_numallocs
301Return the total number of allocations for the memory type over its lifetime.
302.It Fn memstat_get_numfrees
303Return the total number of frees for the memory type over its lifetime.
304.It Fn memstat_get_bytes
305Return the current number of bytes allocated to the memory type.
306.It Fn memstat_get_count
307Return the current number of allocations for the memory type.
308.It Fn memstat_get_free
309If the memory allocator supports a cache, return the number of items in the
310cache.
311.It Fn memstat_get_failures
312If the memory allocator and type permit allocation failures, return the
313number of allocation failures measured.
314.It Fn memstat_get_caller_pointer
315Return a caller-owned pointer for the memory type.
316.It Fn memstat_set_caller_pointer
317Set a caller-owned pointer for the memory type.
318.It Fn memstat_get_caller_uint64
319Return a caller-owned integer for the memory type.
320.It Fn memstat_set_caller_uint64
321Set a caller-owned integer for the memory type.
322.It Fn memstat_get_zonefree
323If the memory allocator supports a multi-level allocation structure, return
324the number of cached items in the zone.
325These items will be in a fully constructed state available for immediate
326use.
327.It Fn memstat_get_kegfree
328If the memory allocator supports a multi-level allocation structure, return
329the number of cached items in the keg.
330These items may be in a partially constructed state, and may require further
331processing before they can be made available for use.
332.It Fn memstat_get_percpu_memalloced
333If the memory allocator supports per-CPU statistics, return the number of
334bytes of memory allocated for the memory type on the CPU over its lifetime.
335.It Fn memstat_get_percpu_memfreed
336If the memory allocator supports per-CPU statistics, return the number of
337bytes of memory freed from the memory type on the CPU over its lifetime.
338.It Fn memstat_get_percpu_numallocs
339If the memory allocator supports per-CPU statistics, return the number of
340allocations for the memory type on the CPU over its lifetime.
341.It Fn memstat_get_percpu_numfrees
342If the memory allocator supports per-CPU statistics, return the number of
343frees for the memory type on the CPU over its lifetime.
344.It Fn memstat_get_percpu_sizemask
345If the memory allocator supports variable size memory allocation and per-CPU
346statistics, return the size bitmask for the memory type on the CPU.
347.It Fn memstat_get_percpu_caller_pointer
348Return a caller-owned per-CPU pointer for the memory type.
349.It Fn memstat_set_percpu_caller_pointer
350Set a caller-owned per-CPU pointer for the memory type.
351.It Fn memstat_get_percpu_caller_uint64
352Return a caller-owned per-CPU integer for the memory type.
353.It Fn memstat_set_percpu_caller_uint64
354Set a caller-owned per-CPU integer for the memory type.
355.It Fn memstat_get_percpu_free
356If the memory allocator supports a per-CPU cache, return the number of free
357items in the per-CPU cache of the designated CPU.
358.El
359.Sh RETURN VALUES
360.Nm
361functions fall into three categories: functions returning a pointer to an
362object, functions returning an integer return value, and functions
363implementing accessor methods returning data from a
364.Vt "struct memory_type" .
365.Pp
366Functions returning a pointer to an object will generally return
367.Dv NULL
368on failure.
369.Fn memstat_mtl_alloc
370will return an error value via
371.Va errno ,
372which will consist of the value
373.Er ENOMEM .
374Functions
375.Fn memstat_mtl_first ,
376.Fn memstat_mtl_next ,
377and
378.Fn memstat_mtl_find
379will return
380.Dv NULL
381when there is no entry or match in the list; however, this is not considered
382a failure mode and no error value is available.
383.Pp
384Functions returning an integer success value will return
3850
386on success, or
387\-1
388on failure.
389If a failure is returned, the list error access method,
390.Fn memstat_mtl_geterror ,
391may be used to retrieve the error state.
392The string representation of the error may be retrieved using
393.Fn memstat_strerror .
394Possible error values are:
395.Bl -tag -width ".Dv MEMSTAT_ERROR_KVM_SHORTREAD"
396.It Dv MEMSTAT_ERROR_UNDEFINED
397Undefined error.
398Occurs if
399.Fn memstat_mtl_geterror
400is called on a list before an error associated with the list has occurred.
401.It Dv MEMSTAT_ERROR_NOMEMORY
402Insufficient memory.
403Occurs if library calls to
404.Xr malloc 3
405fail, or if a system call to retrieve kernel statistics fails with
406.Er ENOMEM .
407.It Dv MEMSTAT_ERROR_VERSION
408Returned if the current version of
409.Nm
410is unable to interpret the statistics data returned by the kernel due to an
411explicit version mismatch, or to differences in data structures that cannot
412be reconciled.
413.It Dv MEMSTAT_ERROR_PERMISSION
414Returned if a statistics source returns
415.Va errno
416values of
417.Er EACCES
418or
419.Er EPERM .
420.It Dv MEMSTAT_ERROR_DATAERROR
421Returned if
422.Nm
423is unable to interpret statistics data returned by the data source, even
424though there does not appear to be a version problem.
425.It Dv MEMSTAT_ERROR_KVM
426Returned if
427.Nm
428experiences an error while using
429.Xr kvm 3
430interfaces to query statistics data.
431Use
432.Xr kvm_geterr 3
433to retrieve the error.
434.It Dv MEMSTAT_ERROR_KVM_NOSYMBOL
435Returned if
436.Nm
437is unable to read a required symbol from the kernel being operated on.
438.It Dv MEMSTAT_ERROR_KVM_SHORTREAD
439Returned if
440.Nm
441attempts to read data from a live memory image or kernel core dump and
442insufficient data is returned.
443.El
444.Pp
445Finally, functions returning data from a
446.Vt "struct memory_type"
447pointer are not permitted to fail, and directly return either a statistic
448or pointer to a string.
449.Sh EXAMPLES
450Create a memory type list, query the
451.Xr uma 9
452memory allocator for available statistics, and print out the number of
453allocations performed by the
454.Dv mbuf
455zone.
456.Bd -literal -offset indent
457struct memory_type_list *mtlp;
458struct memory_type *mtp;
459uint64_t mbuf_count;
460
461mtlp = memstat_mtl_alloc();
462if (mtlp == NULL)
463    err(-1, "memstat_mtl_alloc");
464if (memstat_sysctl_uma(mtlp, 0) < 0)
465    err(-1, "memstat_sysctl_uma");
466mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, "mbuf");
467if (mtp == NULL)
468    errx(-1, "memstat_mtl_find: mbuf not found");
469mbuf_count = memstat_get_count(mtp);
470memstat_mtl_free(mtlp);
471
472printf("mbufs: %llu\en", (unsigned long long)mbuf_count);
473.Ed
474.Sh SEE ALSO
475.Xr malloc 9 ,
476.Xr uma 9
477.Sh HISTORY
478The
479.Nm
480library appeared in
481.Fx 6.0 .
482.Sh AUTHORS
483The kernel memory allocator changes necessary to support a general purpose
484monitoring library, along with the library, were written by
485.An Robert Watson Aq Mt rwatson@FreeBSD.org .
486.Sh BUGS
487There are memory allocators in the kernel, such as the VM page allocator
488and
489.Nm sf_buf
490allocator, which are not currently supported by
491.Nm .
492.Pp
493Once a memory type is present on a memory type list, it will not be removed
494even if the kernel no longer presents information on the type via its
495monitoring interfaces.
496In order to flush removed memory types, it is necessary to free the entire
497list and allocate a new one.
498