---
name: "libmemstat"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/libmemstat"
---

# libmemstat(3)

library interface to retrieve kernel memory allocator statistics

## NAME

**libmemstat** – library interface to retrieve kernel memory allocator statistics

## LIBRARY

library “libmemstat”

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<memstat.h\>**

### General Functions

*const char \**\

**memstat\_strerror**(*int error*);

### Memory Type List Management Functions

*struct memory\_type\_list \**\

**memstat\_mtl\_alloc**(*void*);

*struct memory\_type \**\

**memstat\_mtl\_first**(*struct memory\_type\_list \*list*);

*struct memory\_type \**\

**memstat\_mtl\_next**(*struct memory\_type \*mtp*);

*struct memory\_type \**\

**memstat\_mtl\_find**(*struct memory\_type\_list \*list*, *int allocator*, *const char \*name*);

*void*\

**memstat\_mtl\_free**(*struct memory\_type\_list \*list*);

*int*\

**memstat\_mtl\_geterror**(*struct memory\_type\_list \*list*);

### Allocator Query Functions

*int*\

**memstat\_kvm\_all**(*struct memory\_type\_list \*list*, *void \*kvm\_handle*);

*int*\

**memstat\_kvm\_malloc**(*struct memory\_type\_list \*list*, *void \*kvm\_handle*);

*int*\

**memstat\_kvm\_uma**(*struct memory\_type\_list \*list*, *void \*kvm\_handle*);

*int*\

**memstat\_sysctl\_all**(*struct memory\_type\_list \*list*, *int flags*);

*int*\

**memstat\_sysctl\_malloc**(*struct memory\_type\_list \*list*, *int flags*);

*int*\

**memstat\_sysctl\_uma**(*struct memory\_type\_list \*list*, *int flags*);

### Memory Type Accessor Methods

*const char \**\

**memstat\_get\_name**(*const struct memory\_type \*mtp*);

*int*\

**memstat\_get\_allocator**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_countlimit**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_byteslimit**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_sizemask**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_size**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_rsize**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_memalloced**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_memfreed**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_numallocs**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_numfrees**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_bytes**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_count**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_free**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_failures**(*const struct memory\_type \*mtp*);

*void \**\

**memstat\_get\_caller\_pointer**(*const struct memory\_type \*mtp*, *int index*);

*void*\

**memstat\_set\_caller\_pointer**(*struct memory\_type \*mtp*, *int index*, *void \*value*);

*uint64\_t*\

**memstat\_get\_caller\_uint64**(*const struct memory\_type \*mtp*, *int index*);

*void*\

**memstat\_set\_caller\_uint64**(*struct memory\_type \*mtp*, *int index*, *uint64\_t value*);

*uint64\_t*\

**memstat\_get\_zonefree**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_kegfree**(*const struct memory\_type \*mtp*);

*uint64\_t*\

**memstat\_get\_percpu\_memalloced**(*const struct memory\_type \*mtp*, *int cpu*);

*uint64\_t*\

**memstat\_get\_percpu\_memfreed**(*const struct memory\_type \*mtp*, *int cpu*);

*uint64\_t*\

**memstat\_get\_percpu\_numallocs**(*const struct memory\_type \*mtp*, *int cpu*);

*uint64\_t*\

**memstat\_get\_percpu\_numfrees**(*const struct memory\_type \*mtp*, *int cpu*);

*uint64\_t*\

**memstat\_get\_percpu\_sizemask**(*const struct memory\_type \*mtp*, *int cpu*);

*void \**\

**memstat\_get\_percpu\_caller\_pointer**(*const struct memory\_type \*mtp*, *int cpu*, *int index*);

*void*\

**memstat\_set\_percpu\_caller\_pointer**(*struct memory\_type \*mtp*, *int cpu*, *int index*, *void \*value*);

*uint64\_t*\

**memstat\_get\_percpu\_caller\_uint64**(*const struct memory\_type \*mtp*, *int cpu*, *int index*);

*void*\

**memstat\_set\_percpu\_caller\_uint64**(*struct memory\_type \*mtp*, *int cpu*, *int index*, *uint64\_t value*);

*uint64\_t*\

**memstat\_get\_percpu\_free**(*const struct memory\_type \*mtp*, *int cpu*);

## DESCRIPTION

**libmemstat** provides an interface to retrieve kernel memory allocator statistics, for the purposes of debugging and system monitoring, insulating applications from implementation details of the allocators, and allowing a tool to transparently support multiple allocators. **libmemstat** supports both retrieving a single statistics snapshot, as well as incrementally updating statistics for long-term monitoring.

**libmemstat** describes each memory type using a *struct memory\_type*, an opaque memory type accessed by the application using accessor functions in the library. **libmemstat** returns and updates chains of *struct memory\_type* via a *struct memory\_type\_list*, which will be allocated by calling **memstat\_mtl\_alloc**(), and freed on completion using **memstat\_mtl\_free**(). Lists of memory types are populated via calls that query the kernel for statistics information; currently: **memstat\_kvm\_all**(), **memstat\_kvm\_malloc**(), **memstat\_kvm\_uma**(), **memstat\_sysctl\_all**(), **memstat\_sysctl\_uma**(), and **memstat\_sysctl\_malloc**(). Repeated calls will incrementally update the list of memory types, permitting tracking over time without recreating all list state. If an error is detected during a query call, error condition information may be retrieved using **memstat\_mtl\_geterror**(), and converted to a user-readable string using **memstat\_strerror**().

Freeing the list will free all memory type data in the list, and so invalidates any outstanding pointers to entries in the list. *struct memory\_type* entries in the list may be iterated over using **memstat\_mtl\_first**() and **memstat\_mtl\_next**(), which respectively return the first entry in a list, and the next entry in a list. **memstat\_mtl\_find**(), which will return a pointer to the first entry matching the passed parameters.

A series of accessor methods is provided to access fields of the structure, including retrieving statistics and properties, as well as setting of caller owned fields. Direct application access to the data structure fields is not supported.

### Library Vt memory\_type Ss Fields

Each *struct memory\_type* holds a description of the memory type, including its name and the allocator it is managed by, as well as current statistics on use. Some statistics are directly measured, others are derived from directly measured statistics. Certain high level statistics are present across all available allocators, such as the number of allocation and free operations; other measurements, such as the quantity of free items in per-CPU caches, or administrative limit on the number of allocations, is available only for specific allocators.

### Caller Vt memory\_type Ss Fields

*struct memory\_type* includes fields to allow the application to store data, in the form of pointers and 64-bit integers, with memory types. For example, the application author might make use of one of the caller pointers to reference a more complex data structure tracking long-term behavior of the memory type, or a window system object that is used to render the state of the memory type. General and per-CPU storage is provided with each *struct memory\_type* in the form of an array of pointers and integers. The array entries are accessed via the *index* argument to the get and set accessor methods. Possible values of *index* range between 0 and MEMSTAT\_MAXCALLER.

Caller-owned fields are initialized to 0 or NULL when a new *struct memory\_type* is allocated and attached to a memory type list; these fields retain their values across queries that update library-owned fields.

### Allocator Types

Currently, **libmemstat** supports two kernel allocators: ALLOCATOR\_UMA for [uma(9)](/uma/9.md), and ALLOCATOR\_MALLOC for [malloc(9)](/malloc/9.md). These values may be passed to **memstat\_mtl\_find**(), and will be returned by **memstat\_get\_allocator**(). Two additional constants in the allocator name space are defined: ALLOCATOR\_UNKNOWN, which will only be returned as a result of a library error, and ALLOCATOR\_ANY, which can be used to specify that returning types matching any allocator is permittable from **memstat\_mtl\_find**().

### Access Method List

The following accessor methods are defined, of which some will be valid for a given memory type:

- **memstat\_get\_name**()

  Return a pointer to the name of the memory type. Memory for the name is owned by **libmemstat** and will be valid through a call to **memstat\_mtl\_free**(). Note that names will be unique with respect to a single allocator, but that the same name might be used by different memory types owned by different memory allocators.

- **memstat\_get\_allocator**()

  Return an integer identifier for the memory allocator that owns the memory type.

- **memstat\_get\_countlimit**()

  If the memory type has an administrative limit on the number of simultaneous allocations, return it.

- **memstat\_get\_byteslimit**()

  If the memory type has an administrative limit on the number of bytes of memory that may be simultaneously allocated for the memory type, return it.

- **memstat\_get\_sizemask**()

  If the memory type supports variable allocation sizes, return a bitmask of sizes allocated for the memory type.

- **memstat\_get\_size**()

  If the memory type supports a fixed allocation size, return that size.

- **memstat\_get\_rsize**()

  If the memory type supports a fixed allocation size, return real size of an allocation. Real size can exceed requested size due to alignment constraints or implicit padding.

- **memstat\_get\_memalloced**()

  Return the total number of bytes allocated for the memory type over its lifetime.

- **memstat\_get\_memfreed**()

  Return the total number of bytes freed for the memory type over its lifetime.

- **memstat\_get\_numallocs**()

  Return the total number of allocations for the memory type over its lifetime.

- **memstat\_get\_numfrees**()

  Return the total number of frees for the memory type over its lifetime.

- **memstat\_get\_bytes**()

  Return the current number of bytes allocated to the memory type.

- **memstat\_get\_count**()

  Return the current number of allocations for the memory type.

- **memstat\_get\_free**()

  If the memory allocator supports a cache, return the number of items in the cache.

- **memstat\_get\_failures**()

  If the memory allocator and type permit allocation failures, return the number of allocation failures measured.

- **memstat\_get\_caller\_pointer**()

  Return a caller-owned pointer for the memory type.

- **memstat\_set\_caller\_pointer**()

  Set a caller-owned pointer for the memory type.

- **memstat\_get\_caller\_uint64**()

  Return a caller-owned integer for the memory type.

- **memstat\_set\_caller\_uint64**()

  Set a caller-owned integer for the memory type.

- **memstat\_get\_zonefree**()

  If the memory allocator supports a multi-level allocation structure, return the number of cached items in the zone. These items will be in a fully constructed state available for immediate use.

- **memstat\_get\_kegfree**()

  If the memory allocator supports a multi-level allocation structure, return the number of cached items in the keg. These items may be in a partially constructed state, and may require further processing before they can be made available for use.

- **memstat\_get\_percpu\_memalloced**()

  If the memory allocator supports per-CPU statistics, return the number of bytes of memory allocated for the memory type on the CPU over its lifetime.

- **memstat\_get\_percpu\_memfreed**()

  If the memory allocator supports per-CPU statistics, return the number of bytes of memory freed from the memory type on the CPU over its lifetime.

- **memstat\_get\_percpu\_numallocs**()

  If the memory allocator supports per-CPU statistics, return the number of allocations for the memory type on the CPU over its lifetime.

- **memstat\_get\_percpu\_numfrees**()

  If the memory allocator supports per-CPU statistics, return the number of frees for the memory type on the CPU over its lifetime.

- **memstat\_get\_percpu\_sizemask**()

  If the memory allocator supports variable size memory allocation and per-CPU statistics, return the size bitmask for the memory type on the CPU.

- **memstat\_get\_percpu\_caller\_pointer**()

  Return a caller-owned per-CPU pointer for the memory type.

- **memstat\_set\_percpu\_caller\_pointer**()

  Set a caller-owned per-CPU pointer for the memory type.

- **memstat\_get\_percpu\_caller\_uint64**()

  Return a caller-owned per-CPU integer for the memory type.

- **memstat\_set\_percpu\_caller\_uint64**()

  Set a caller-owned per-CPU integer for the memory type.

- **memstat\_get\_percpu\_free**()

  If the memory allocator supports a per-CPU cache, return the number of free items in the per-CPU cache of the designated CPU.

## RETURN VALUES

**libmemstat** functions fall into three categories: functions returning a pointer to an object, functions returning an integer return value, and functions implementing accessor methods returning data from a *struct memory\_type*.

Functions returning a pointer to an object will generally return NULL on failure. **memstat\_mtl\_alloc**() will return an error value via *errno*, which will consist of the value ENOMEM. Functions **memstat\_mtl\_first**(), **memstat\_mtl\_next**(), and **memstat\_mtl\_find**() will return NULL when there is no entry or match in the list; however, this is not considered a failure mode and no error value is available.

Functions returning an integer success value will return 0 on success, or -1 on failure. If a failure is returned, the list error access method, **memstat\_mtl\_geterror**(), may be used to retrieve the error state. The string representation of the error may be retrieved using **memstat\_strerror**(). Possible error values are:

- MEMSTAT\_ERROR\_UNDEFINED

  Undefined error. Occurs if **memstat\_mtl\_geterror**() is called on a list before an error associated with the list has occurred.

- MEMSTAT\_ERROR\_NOMEMORY

  Insufficient memory. Occurs if library calls to [malloc(3)](/malloc/3.md) fail, or if a system call to retrieve kernel statistics fails with ENOMEM.

- MEMSTAT\_ERROR\_VERSION

  Returned if the current version of **libmemstat** is unable to interpret the statistics data returned by the kernel due to an explicit version mismatch, or to differences in data structures that cannot be reconciled.

- MEMSTAT\_ERROR\_PERMISSION

  Returned if a statistics source returns *errno* values of EACCES or EPERM.

- MEMSTAT\_ERROR\_DATAERROR

  Returned if **libmemstat** is unable to interpret statistics data returned by the data source, even though there does not appear to be a version problem.

- MEMSTAT\_ERROR\_KVM

  Returned if **libmemstat** experiences an error while using [kvm(3)](/kvm/3.md) interfaces to query statistics data. Use [kvm\_geterr(3)](/kvm_geterr/3.md) to retrieve the error.

- MEMSTAT\_ERROR\_KVM\_NOSYMBOL

  Returned if **libmemstat** is unable to read a required symbol from the kernel being operated on.

- MEMSTAT\_ERROR\_KVM\_SHORTREAD

  Returned if **libmemstat** attempts to read data from a live memory image or kernel core dump and insufficient data is returned.

Finally, functions returning data from a *struct memory\_type* pointer are not permitted to fail, and directly return either a statistic or pointer to a string.

## EXAMPLES

Create a memory type list, query the [uma(9)](/uma/9.md) memory allocator for available statistics, and print out the number of allocations performed by the mbuf zone.

```
struct memory_type_list *mtlp;
struct memory_type *mtp;
uint64_t mbuf_count;
mtlp = memstat_mtl_alloc();
if (mtlp == NULL)
    err(-1, "memstat_mtl_alloc");
if (memstat_sysctl_uma(mtlp, 0) < 0)
    err(-1, "memstat_sysctl_uma");
mtp = memstat_mtl_find(mtlp, ALLOCATOR_UMA, "mbuf");
if (mtp == NULL)
    errx(-1, "memstat_mtl_find: mbuf not found");
mbuf_count = memstat_get_count(mtp);
memstat_mtl_free(mtlp);
printf("mbufs: %llu\n", (unsigned long long)mbuf_count);
```

## SEE ALSO

[malloc(9)](/malloc/9.md), [uma(9)](/uma/9.md)

## HISTORY

The **libmemstat** library appeared in FreeBSD 6.0.

## AUTHORS

The kernel memory allocator changes necessary to support a general purpose monitoring library, along with the library, were written by Robert Watson ⟨*rwatson@FreeBSD.org*⟩.

## BUGS

There are memory allocators in the kernel, such as the VM page allocator and **sf\_buf** allocator, which are not currently supported by **libmemstat**.

Once a memory type is present on a memory type list, it will not be removed even if the kernel no longer presents information on the type via its monitoring interfaces. In order to flush removed memory types, it is necessary to free the entire list and allocate a new one.
