NAME¶
uma_zcreate
,
uma_zalloc
,
uma_zalloc_arg
,
uma_zfree
,
uma_zfree_arg
,
uma_find_refcnt
,
uma_zdestroy
,
uma_zone_set_max,
uma_zone_get_max,
uma_zone_get_cur,
uma_zone_set_warning
—
zone allocator
SYNOPSIS¶
#include
<sys/param.h>
#include
<sys/queue.h>
#include
<vm/uma.h>
uma_zone_t
uma_zcreate
(
char
*name,
int size,
uma_ctor ctor,
uma_dtor dtor,
uma_init uminit,
uma_fini fini,
int
align,
uint16_t flags);
void *
uma_zalloc
(
uma_zone_t
zone,
int
flags);
void *
uma_zalloc_arg
(
uma_zone_t
zone,
void
*arg,
int
flags);
void
uma_zfree
(
uma_zone_t
zone,
void
*item);
void
uma_zfree_arg
(
uma_zone_t
zone,
void
*item,
void
*arg);
uint32_t *
uma_find_refcnt
(
uma_zone_t
zone,
void
*item);
void
uma_zdestroy
(
uma_zone_t
zone);
int
uma_zone_set_max
(
uma_zone_t
zone,
int
nitems);
int
uma_zone_get_max
(
uma_zone_t
zone);
int
uma_zone_get_cur
(
uma_zone_t
zone);
void
uma_zone_set_warning
(
uma_zone_t
zone,
const char
*warning);
#include
<sys/sysctl.h>
SYSCTL_UMA_MAX
(
parent,
nbr,
name,
access,
zone,
descr);
SYSCTL_ADD_UMA_MAX
(
ctx,
parent,
nbr,
name,
access,
zone,
descr);
SYSCTL_UMA_CUR
(
parent,
nbr,
name,
access,
zone,
descr);
SYSCTL_ADD_UMA_CUR
(
ctx,
parent,
nbr,
name,
access,
zone,
descr);
DESCRIPTION¶
The zone allocator provides an efficient interface for managing
dynamically-sized collections of items of similar size. The zone allocator can
work with preallocated zones as well as with runtime-allocated ones, and is
therefore available much earlier in the boot process than other memory
management routines.
A zone is an extensible collection of items of identical size. The zone
allocator keeps track of which items are in use and which are not, and
provides functions for allocating items from the zone and for releasing them
back (which makes them available for later use).
After the first allocation of an item, it will have been cleared to zeroes,
however subsequent allocations will retain the contents as of the last free.
The
uma_zcreate
() function creates a new zone
from which items may then be allocated from. The
name argument is a text name of the zone for
debugging and stats; this memory should not be freed until the zone has been
deallocated.
The
ctor and
dtor arguments are callback functions that
are called by the uma subsystem at the time of the call to
uma_zalloc
() and
uma_zfree
() respectively. Their purpose is
to provide hooks for initializing or destroying things that need to be done at
the time of the allocation or release of a resource. A good usage for the
ctor and
dtor callbacks might be to adjust a global
count of the number of objects allocated.
The
uminit and
fini arguments are used to optimize the
allocation of objects from the zone. They are called by the uma subsystem
whenever it needs to allocate or free several items to satisfy requests or
memory pressure. A good use for the
uminit
and
fini callbacks might be to initialize and
destroy mutexes contained within the object. This would allow one to re-use
already initialized mutexes when an object is returned from the uma
subsystem's object cache. They are not called on each call to
uma_zalloc
() and
uma_zfree
() but rather in a batch mode on
several objects.
The
flags argument of the
uma_zcreate
() is a subset of the following
flags:
UMA_ZONE_NOFREE
- Slabs of the zone are never returned back to VM.
UMA_ZONE_REFCNT
- Each item in the zone would have internal reference counter associated
with it. See
uma_find_refcnt
().
UMA_ZONE_NODUMP
- Pages belonging to the zone will not be included into mini-dumps.
UMA_ZONE_PCPU
- An allocation from zone would have
mp_ncpu shadow copies, that are privately
assigned to CPUs. A CPU can address its private copy using base allocation
address plus multiple of current CPU id and
sizeof
(struct
pcpu):
foo_zone = uma_zcreate(..., UMA_ZONE_PCPU);
...
foo_base = uma_zalloc(foo_zone, ...);
...
critical_enter();
foo_pcpu = (foo_t *)zpcpu_get(foo_base);
/* do something with foo_pcpu */
critical_exit();
UMA_ZONE_OFFPAGE
- By default book-keeping of items within a slab is done in the slab page
itself. This flag explicitly tells subsystem that book-keeping structure
should be allocated separately from special internal zone. This flag
requires either
UMA_ZONE_VTOSLAB
or
UMA_ZONE_HASH
, since subsystem requires
a mechanism to find a book-keeping structure to an item beeing freed. The
subsystem may choose to prefer offpage book-keeping for certain zones
implicitly.
UMA_ZONE_ZINIT
- The zone will have its uma_init method
set to internal method that initializes a new allocated slab to all zeros.
Do not mistake uma_init method with
uma_ctor. A zone with
UMA_ZONE_ZINIT
flag would not return
zeroed memory on every
uma_zalloc
().
UMA_ZONE_HASH
- The zone should use an internal hash table to find slab book-keeping
structure where an allocation being freed belongs to.
UMA_ZONE_VTOSLAB
- The zone should use special field of
vm_page_t to find slab book-keeping
structure where an allocation being freed belongs to.
UMA_ZONE_MALLOC
- The zone is for the malloc(9) subsystem.
UMA_ZONE_VM
- The zone is for the VM subsystem.
To allocate an item from a zone, simply call
uma_zalloc
() with a pointer to that zone
and set the
flags argument to selected flags
as documented in
malloc(9). It will return a
pointer to an item if successful, or
NULL
in the rare case where all items in the zone are in use and the allocator is
unable to grow the zone and
M_NOWAIT
is
specified.
Items are released back to the zone from which they were allocated by calling
uma_zfree
() with a pointer to the zone and
a pointer to the item. If
item is
NULL
, then
uma_zfree
() does nothing.
The variations
uma_zalloc_arg
() and
uma_zfree_arg
() allow to specify an
argument for the
ctor
and
dtor
functions, respectively.
If zone was created with
UMA_ZONE_REFCNT
flag, then pointer to reference counter for an item can be retrieved with help
of the
uma_find_refcnt
() function.
Created zones, which are empty, can be destroyed using
uma_zdestroy
(), freeing all memory that was
allocated for the zone. All items allocated from the zone with
uma_zalloc
() must have been freed with
uma_zfree
() before.
The
uma_zone_set_max
() function limits the
number of items (and therefore memory) that can be allocated to
zone. The
nitems argument specifies the requested upper
limit number of items. The effective limit is returned to the caller, as it
may end up being higher than requested due to the implementation rounding up
to ensure all memory pages allocated to the zone are utilised to capacity. The
limit applies to the total number of items in the zone, which includes
allocated items, free items and free items in the per-cpu caches. On systems
with more than one CPU it may not be possible to allocate the specified number
of items even when there is no shortage of memory, because all of the
remaining free items may be in the caches of the other CPUs when the limit is
hit.
The
uma_zone_get_max
() function returns the
effective upper limit number of items for a zone.
The
uma_zone_get_cur
() function returns the
approximate current occupancy of the zone. The returned value is approximate
because appropriate synchronisation to determine an exact value is not
performed by the implementation. This ensures low overhead at the expense of
potentially stale data being used in the calculation.
The
uma_zone_set_warning
() function sets a
warning that will be printed on the system console when the given zone becomes
full and fails to allocate an item. The warning will be printed not often than
every five minutes. Warnings can be turned off globally by setting the
vm.zone_warnings sysctl tunable to
0.
The
SYSCTL_UMA_MAX
(
parent,
nbr,
name,
access,
zone,
descr)
macro declares a static
sysctl oid that exports
the effective upper limit number of items for a zone. The
zone argument should be a pointer to
uma_zone_t. A read of the oid returns value
obtained through
uma_zone_get_max
(). A
write to the oid sets new value via
uma_zone_set_max
(). The
SYSCTL_ADD_UMA_MAX
(
ctx,
parent,
nbr,
name,
access,
zone,
descr)
macro is provided to create this type of oid dynamically.
The
SYSCTL_UMA_CUR
(
parent,
nbr,
name,
access,
zone,
descr)
macro declares a static read only
sysctl oid that
exports the approximate current occupancy of the zone. The
zone argument should be a pointer to
uma_zone_t. A read of the oid returns value
obtained through
uma_zone_get_cur
(). The
SYSCTL_ADD_UMA_CUR
(
ctx,
parent,
nbr,
name,
zone,
descr) macro is provided to create this type
of oid dynamically.
RETURN VALUES¶
The
uma_zalloc
() function returns a pointer
to an item, or
NULL
if the zone ran out of
unused items and
M_NOWAIT
was specified.
SEE ALSO¶
malloc(9)
HISTORY¶
The zone allocator first appeared in
FreeBSD 3.0. It was
radically changed in
FreeBSD 5.0 to function as a slab
allocator.
AUTHORS¶
The zone allocator was written by
John S.
Dyson. The zone allocator was rewritten in large parts by
Jeff Roberson
⟨jeff@FreeBSD.org⟩ to function as a slab allocator.
This manual page was written by
Dag-Erling
Smørgrav ⟨des@FreeBSD.org⟩. Changes for UMA by
Jeroen Ruigrok van der Werven
⟨asmodai@FreeBSD.org⟩.