summaryrefslogtreecommitdiff
path: root/share/man/man9/pool.9
blob: f68d5c028fb3ed4e6ad5536aae1d528f7c914fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
.\"	$OpenBSD: pool.9,v 1.33 2006/05/07 20:24:45 pedro Exp $
.\"	$NetBSD: pool.9,v 1.18 2001/06/21 11:59:01 wiz Exp $
.\"
.\" Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Paul Kranenburg.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"        This product includes software developed by the NetBSD
.\"        Foundation, Inc. and its contributors.
.\" 4. Neither the name of The NetBSD Foundation nor the names of its
.\"    contributors may be used to endorse or promote products derived
.\"    from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd July 23, 1998
.Dt POOL 9
.Os
.Sh NAME
.Nm pool_init ,
.Nm pool_destroy ,
.Nm pool_get ,
.Nm pool_put ,
.Nm pool_prime ,
.Nm pool_sethiwat ,
.Nm pool_setlowat ,
.Nm pool_sethardlimit ,
.Nm pool_cache_init ,
.Nm pool_cache_destroy ,
.Nm pool_cache_get ,
.Nm pool_cache_put ,
.Nm pool_cache_destruct_object ,
.Nm pool_cache_invalidate
.Nd resource-pool manager
.Sh SYNOPSIS
.Fd #include <sys/types.h>
.Fd #include <sys/pool.h>
.Ft void
.Fo pool_init
.Fa "struct pool *pool"
.Fa "size_t size"
.Fa "u_int align"
.Fa "u_int align_offset"
.Fa "int flags"
.Fa "const char *wmesg"
.Fa "struct pool_allocator *palloc"
.Fc
.Ft void
.Fo pool_destroy
.Fa "struct pool *pp"
.Fc
.Ft void *
.Fn pool_get "struct pool *pp" "int flags"
.Ft void
.Fn pool_put "struct pool *pp" "void *item"
.Ft int
.Fn pool_prime "struct pool *pp" "int nitems"
.Ft void
.Fn pool_sethiwat "struct pool *pp" "int n"
.Ft void
.Fn pool_setlowat "struct pool *pp" "int n"
.Ft int
.Fo pool_sethardlimit
.Fa "struct pool *pp"
.Fa "unsigned n"
.Fa "const char *warnmess"
.Fa "int ratecap"
.Fc
.Ft void
.Fo pool_cache_init
.Fa "struct pool_cache *pc"
.Fa "struct pool *pp"
.Fa "int (*ctor)(void *, void *, int)"
.Fa "void (*dtor)(void *, void *)"
.Fa "void *arg"
.Fc
.Ft void
.Fn pool_cache_destroy "struct pool_cache *pc"
.Ft void *
.Fn pool_cache_get "struct pool_cache *pc" "int flags"
.Ft void
.Fn pool_cache_put "struct pool_cache *pc" "void *object"
.Ft void
.Fn pool_cache_destruct_object "struct pool_cache *pc" "void *object"
.Ft void
.Fn pool_cache_invalidate "struct pool_cache *pc"
.Sh DESCRIPTION
These utility routines provide management of pools of fixed-sized
areas of memory.
Resource pools set aside an amount of memory for exclusive use by the resource
pool owner.
This can be used by applications to guarantee the availability of a minimum
amount of memory needed to continue operation independent of the memory
resources currently available from the system-wide memory allocator
.Pq Xr malloc 9 .
The pool manager obtains memory by using the special-purpose memory
allocator
.Fa palloc
passed to
.Fn pool_init ,
for extra pool items in case the number of allocations exceeds
the nominal number of pool items managed by a pool resource.
This temporary memory will be automatically returned to the system
at a later time.
.Ss CREATING A POOL
The function
.Fn pool_init
initializes a resource pool.
The arguments are:
.Bl -tag -offset indent -width "align_offset"
.It Fa pool
Specifies the pool storage to be initialized.
.It Fa size
Specifies the size of the memory items managed by the pool.
.It Fa align
Specifies the memory address alignment of the items returned by
.Fn pool_get .
This argument must be a power of two.
If zero,
the alignment defaults to an architecture-specific natural alignment.
.It Fa align_offset
The offset within an item to which the
.Fa align
parameter applies.
.It Fa flags
Specifies various flags set on the pool at creation time.
.It Fa wmesg
The message passed on to
.Xr tsleep 9
if
.Fn pool_get
must wait for items to be returned to the pool.
.It Fa palloc
The back-end allocator used to manage the memory for the pool.
.Fa palloc
may be
.Dv NULL ,
in which case the pool manager uses an interrupt safe allocator.
It is recommended that this be set to
.Em pool_allocator_nointr
if the pool will never be accessed in an interrupt context.
.El
.Ss DESTROYING A POOL
The
.Fn pool_destroy
function destroys a resource pool.
It takes a single argument
.Fa pp
identifying the pool resource instance.
.Ss ALLOCATING ITEMS FROM A POOL
.Fn pool_get
allocates an item from the pool and returns a pointer to it.
.Bl -tag -offset indent -width "flags"
.It Fa pp
The handle identifying the pool resource instance.
.It Fa flags
One or more of
.Dv PR_URGENT ,
.Dv PR_WAITOK
or
.Dv PR_LIMITFAIL ,
that define behaviour in case the pooled resources are depleted.
If no resources are available and
.Dv PR_WAITOK
is given,
this function will wait until items are returned to the pool.
Otherwise
.Fn pool_get
returns
.Dv NULL .
If
.Dv PR_URGENT
is specified and no items are available and
.Fa palloc
cannot allocate a new page,
the system will panic
.Pq XXX .
.\"Undefined behaviour results if
.\".Dv PR_MALLOCOK
.\"is specified on a pool handle that was created using client-provided
.\"storage.
.\" a bunch of other flags aren't documented.
If both
.Dv PR_LIMITFAIL
and
.Dv PR_WAITOK
are specified, and the pool has reached its hard limit,
.Fn pool_get
will return
.Dv NULL
without waiting, allowing the caller to do its own garbage collection;
however, it will still wait if the pool is not yet at its hard limit.
.El
.Ss RETURNING ITEMS TO A POOL
.Fn pool_put
returns the pool item pointed at by
.Fa item
to the resource pool identified by the pool handle
.Fa pp .
If the number of available items in the pool exceeds the maximum pool
size set by
.Fn pool_sethiwat
and there are no outstanding requests for pool items,
the excess items will be returned to the system by calling
.Fn prelease .
.Bl -tag -offset indent -width "item"
.It Fa pp
The handle identifying the pool resource instance.
.It Fa item
A pointer to a pool item previously obtained by
.Fn pool_get .
.El
.Ss PRIMING A POOL
.Fn pool_prime
adds items to the pool.
Storage space for the items is allocated by using the page allocation
routine specified to
.Fn pool_init .
.Pp
.Fn pool_prime
.Bl -tag -offset indent -width "nitems"
.It Fa pp
The handle identifying the pool resource instance.
.It Fa nitems
The number of items to add to the pool.
.El
.Pp
This function may return
.Dv ENOMEM
in case the requested number of items could not be allocated.
Otherwise,
the return value is 0.
.Ss SETTING POOL RESOURCE WATERMARKS
A pool will attempt to increase its resource usage to keep up with the demand
for its items.
Conversely,
it will return unused memory to the system should the number of accumulated
unused items in the pool exceed a programmable limit.
The limits for the minimum and maximum number of items which a pool should keep
at hand are known as the high and low
.Sy watermarks .
The functions
.Fn pool_sethiwat
and
.Fn pool_setlowat
set a pool's high and low watermarks, respectively.
.Pp
.Fn pool_sethiwat
.Bl -tag -offset indent -width "flags"
.It Fa pp
The handle identifying the pool resource instance.
.It Fa n
The maximum number of items to keep in the pool.
As items are returned and the total number of pages in the pool is larger
than the maximum set by this function,
any completely unused pages are released immediately
.Pq by calling Fn prelease .
If this function is not used to specify a maximum number of items,
the pages will remain associated with the pool until the system runs low
on memory,
at which point the VM system will try to reclaim unused pages.
.El
.Pp
.Fn pool_setlowat
.Bl -tag -offset indent -width "flags"
.It Fa pp
The handle identifying the pool resource instance.
.It Fa n
The minimum number of items to keep in the pool.
The number of pages in the pool will not decrease below the required value to
accommodate the minimum number of items specified by this function.
Unlike
.Fn pool_prime ,
this function does not allocate the necessary memory up-front.
.El
.Ss SETTING HARD LIMITS
The function
.Fn pool_sethardlimit
sets a hard limit on the pool to
.Fa n
items.
If the hard limit is reached
.Fa warnmess
will be printed to the console, but no more than every
.Fa ratecap
seconds.
Upon successful completion, a value of 0 is returned.
The value EINVAL is returned when the current size of the pool
already exceeds the requested hard limit.
.Ss POTENTIAL PITFALLS
Note that undefined behaviour results when mixing the storage providing
methods supported by the pool resource routines.
.Pp
The pool resource code uses a per-pool lock to protect its internal state.
If any pool functions are called in an interrupt context,
the caller must block all interrupts that might cause the
code to be reentered.
.Ss POOL CACHES
Another set of functions are available as extensions to the pool manager.
The pool cache functions automatically call constructors and destructors
when objects are allocated from the pool or returned to it.
They have similar semantics as the other pool functions.
The
.Fa pp
argument to
.Fn pool_cache_init
must already be initialized.
.Pp
Objects are not immediately deconstructed when put into the pool cache.
Instead, they are maintained for future allocations.
When the system determines that memory needs to be reclaimed, then the
deconstructor is called on each free object and it is placed back into the
pool.
The
.Fa ctor
and
.Fa dtor
functions are passed
.Fa arg
and a pointer to the object, in that order.
The
.Fa ctor
is also passed the same
.Fa flags
that are passed to
.Fn pool_cache_get .
The
.Fn pool_cache_destruct_object
function deconstructs and puts an object back into the pool immediately.
.Fn pool_cache_invalidate
deconstructs all cached objects and releases their memory.
.Pp
Pool caches are also commonly referred to as a slab allocator.
.Ss DIAGNOSTICS
Pool usage logs can be enabled by defining the compile-time option
.Dv POOL_DIAGNOSTIC .
.Ss DEBUGGING
To debug a misbehaving pool, a kernel can be compiled with the
.Dv MALLOC_DEBUG
option and memory debugging on pools can be enabled with the
.Dv PR_DEBUG
flag passed in the
.Fa flags
argument in the call to
.Fn pool_init .
See
.Xr malloc 9
for more information about
.Dv MALLOC_DEBUG .
.Sh CODE REFERENCES
The pool manager is implemented in the file
.Pa sys/kern/subr_pool.c .
.Sh SEE ALSO
.Xr free 9 ,
.Xr malloc 9 ,
.Xr uvm 9
.Sh HISTORY
The pool manager first appeared in
.Nx 1.4
and was ported to
.Ox
by
.An Artur Grabowski Aq art@openbsd.org .