]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - share/man/man9/zone.9
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / share / man / man9 / zone.9
1 .\"-
2 .\" Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\"
14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 .\" SUCH DAMAGE.
25 .\"
26 .\" $FreeBSD$
27 .\"
28 .Dd February 25, 2012
29 .Dt ZONE 9
30 .Os
31 .Sh NAME
32 .Nm uma_zcreate ,
33 .Nm uma_zalloc ,
34 .Nm uma_zalloc_arg ,
35 .Nm uma_zfree ,
36 .Nm uma_zfree_arg ,
37 .Nm uma_zdestroy ,
38 .Nm uma_zone_set_max,
39 .Nm uma_zone_get_max,
40 .Nm uma_zone_get_cur
41 .Nd zone allocator
42 .Sh SYNOPSIS
43 .In sys/param.h
44 .In sys/queue.h
45 .In vm/uma.h
46 .Ft uma_zone_t
47 .Fo uma_zcreate
48 .Fa "char *name" "int size"
49 .Fa "uma_ctor ctor" "uma_dtor dtor" "uma_init uminit" "uma_fini fini"
50 .Fa "int align" "u_int16_t flags"
51 .Fc
52 .Ft "void *"
53 .Fn uma_zalloc "uma_zone_t zone" "int flags"
54 .Ft "void *"
55 .Fn uma_zalloc_arg "uma_zone_t zone" "void *arg" "int flags"
56 .Ft void
57 .Fn uma_zfree "uma_zone_t zone" "void *item"
58 .Ft void
59 .Fn uma_zfree_arg "uma_zone_t zone" "void *item" "void *arg"
60 .Ft void
61 .Fn uma_zdestroy "uma_zone_t zone"
62 .Ft int
63 .Fn uma_zone_set_max "uma_zone_t zone" "int nitems"
64 .Ft int
65 .Fn uma_zone_get_max "uma_zone_t zone"
66 .Ft int
67 .Fn uma_zone_get_cur "uma_zone_t zone"
68 .Sh DESCRIPTION
69 The zone allocator provides an efficient interface for managing
70 dynamically-sized collections of items of similar size.
71 The zone allocator can work with preallocated zones as well as with
72 runtime-allocated ones, and is therefore available much earlier in the
73 boot process than other memory management routines.
74 .Pp
75 A zone is an extensible collection of items of identical size.
76 The zone allocator keeps track of which items are in use and which
77 are not, and provides functions for allocating items from the zone and
78 for releasing them back (which makes them available for later use).
79 .Pp
80 After the first allocation of an item,
81 it will have been cleared to zeroes, however subsequent allocations
82 will retain the contents as of the last free.
83 .Pp
84 The
85 .Fn uma_zcreate
86 function creates a new zone from which items may then be allocated from.
87 The
88 .Fa name
89 argument is a text name of the zone for debugging and stats; this memory
90 should not be freed until the zone has been deallocated.
91 .Pp
92 The
93 .Fa ctor
94 and
95 .Fa dtor
96 arguments are callback functions that are called by
97 the uma subsystem at the time of the call to
98 .Fn uma_zalloc
99 and
100 .Fn uma_zfree
101 respectively.
102 Their purpose is to provide hooks for initializing or
103 destroying things that need to be done at the time of the allocation
104 or release of a resource.
105 A good usage for the
106 .Fa ctor
107 and
108 .Fa dtor
109 callbacks
110 might be to adjust a global count of the number of objects allocated.
111 .Pp
112 The
113 .Fa uminit
114 and
115 .Fa fini
116 arguments are used to optimize the allocation of
117 objects from the zone.
118 They are called by the uma subsystem whenever
119 it needs to allocate or free several items to satisfy requests or memory
120 pressure.
121 A good use for the
122 .Fa uminit
123 and
124 .Fa fini
125 callbacks might be to
126 initialize and destroy mutexes contained within the object.
127 This would
128 allow one to re-use already initialized mutexes when an object is returned
129 from the uma subsystem's object cache.
130 They are not called on each call to
131 .Fn uma_zalloc
132 and
133 .Fn uma_zfree
134 but rather in a batch mode on several objects.
135 .Pp
136 To allocate an item from a zone, simply call
137 .Fn uma_zalloc
138 with a pointer to that zone
139 and set the
140 .Fa flags
141 argument to selected flags as documented in
142 .Xr malloc 9 .
143 It will return a pointer to an item if successful,
144 or
145 .Dv NULL
146 in the rare case where all items in the zone are in use and the
147 allocator is unable to grow the zone
148 or when
149 .Dv M_NOWAIT
150 is specified.
151 .Pp
152 Items are released back to the zone from which they were allocated by
153 calling
154 .Fn uma_zfree
155 with a pointer to the zone and a pointer to the item.
156 If
157 .Fa item
158 is
159 .Dv NULL ,
160 then
161 .Fn uma_zfree
162 does nothing.
163 .Pp
164 The variations
165 .Fn uma_zalloc_arg
166 and
167 .Fn uma_zfree_arg
168 allow to
169 specify an argument for the
170 .Dv ctor
171 and
172 .Dv dtor
173 functions, respectively.
174 .Pp
175 Created zones,
176 which are empty,
177 can be destroyed using
178 .Fn uma_zdestroy ,
179 freeing all memory that was allocated for the zone.
180 All items allocated from the zone with
181 .Fn uma_zalloc
182 must have been freed with
183 .Fn uma_zfree
184 before.
185 .Pp
186 The
187 .Fn uma_zone_set_max
188 function limits the number of items
189 .Pq and therefore memory
190 that can be allocated to
191 .Fa zone .
192 The
193 .Fa nitems
194 argument specifies the requested upper limit number of items.
195 The effective limit is returned to the caller, as it may end up being higher
196 than requested due to the implementation rounding up to ensure all memory pages
197 allocated to the zone are utilised to capacity.
198 The limit applies to the total number of items in the zone, which includes
199 allocated items, free items and free items in the per-cpu caches.
200 On systems with more than one CPU it may not be possible to allocate
201 the specified number of items even when there is no shortage of memory,
202 because all of the remaining free items may be in the caches of the
203 other CPUs when the limit is hit.
204 .Pp
205 The
206 .Fn uma_zone_get_max
207 function returns the effective upper limit number of items for a zone.
208 .Pp
209 The
210 .Fn uma_zone_get_cur
211 function returns the approximate current occupancy of the zone.
212 The returned value is approximate because appropriate synchronisation to
213 determine an exact value is not performed by the implementation.
214 This ensures low overhead at the expense of potentially stale data being used
215 in the calculation.
216 .Sh RETURN VALUES
217 The
218 .Fn uma_zalloc
219 function returns a pointer to an item, or
220 .Dv NULL
221 if the zone ran out of unused items and the allocator was unable to
222 enlarge it.
223 .Sh SEE ALSO
224 .Xr malloc 9
225 .Sh HISTORY
226 The zone allocator first appeared in
227 .Fx 3.0 .
228 It was radically changed in
229 .Fx 5.0
230 to function as a slab allocator.
231 .Sh AUTHORS
232 .An -nosplit
233 The zone allocator was written by
234 .An John S. Dyson .
235 The zone allocator was rewritten in large parts by
236 .An Jeff Roberson Aq jeff@FreeBSD.org
237 to function as a slab allocator.
238 .Pp
239 This manual page was written by
240 .An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
241 Changes for UMA by
242 .An Jeroen Ruigrok van der Werven Aq asmodai@FreeBSD.org .