2005-10-02 19:17:32 +04:00
|
|
|
.\" $NetBSD: pool.9,v 1.38 2005/10/02 15:17:32 bouyer Exp $
|
1997-12-14 02:45:07 +03:00
|
|
|
.\"
|
1998-08-17 19:27:13 +04:00
|
|
|
.\" Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
|
1997-12-14 02:45:07 +03:00
|
|
|
.\" 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
|
2001-06-21 15:59:00 +04:00
|
|
|
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
1997-12-14 02:45:07 +03:00
|
|
|
.\" 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.
|
|
|
|
.\"
|
2005-10-02 19:17:32 +04:00
|
|
|
.Dd Oct 2, 2005
|
1997-12-14 02:45:07 +03:00
|
|
|
.Dt POOL 9
|
1999-03-16 03:40:46 +03:00
|
|
|
.Os
|
1997-12-14 02:45:07 +03:00
|
|
|
.Sh NAME
|
2000-05-31 19:27:33 +04:00
|
|
|
.Nm pool_init ,
|
1997-12-14 02:45:07 +03:00
|
|
|
.Nm pool_destroy ,
|
|
|
|
.Nm pool_get ,
|
|
|
|
.Nm pool_put ,
|
1999-04-03 18:50:21 +04:00
|
|
|
.Nm pool_prime ,
|
|
|
|
.Nm pool_sethiwat ,
|
|
|
|
.Nm pool_setlowat
|
1997-12-14 02:45:07 +03:00
|
|
|
.Nd resource-pool manager
|
|
|
|
.Sh SYNOPSIS
|
2003-04-16 17:34:34 +04:00
|
|
|
.In sys/pool.h
|
2000-05-31 19:27:33 +04:00
|
|
|
.Ft void
|
|
|
|
.Fo pool_init
|
2002-11-12 18:45:42 +03:00
|
|
|
.Fa "struct pool *pp"
|
2000-05-31 19:27:33 +04:00
|
|
|
.Fa "size_t size"
|
|
|
|
.Fa "u_int align"
|
|
|
|
.Fa "u_int align_offset"
|
2003-12-21 10:35:37 +03:00
|
|
|
.Fa "int flags"
|
2000-05-31 19:27:33 +04:00
|
|
|
.Fa "char *wchan"
|
2002-08-11 16:12:23 +04:00
|
|
|
.Fa "struct pool_allocator *palloc"
|
2000-05-31 19:27:33 +04:00
|
|
|
.Fc
|
2001-07-20 01:35:53 +04:00
|
|
|
.Ft void
|
|
|
|
.Fn pool_destroy "struct pool *pp"
|
1997-12-14 02:45:07 +03:00
|
|
|
.Ft void *
|
|
|
|
.Fn pool_get "struct pool *pp" "int flags"
|
|
|
|
.Ft void
|
|
|
|
.Fn pool_put "struct pool *pp" "void *item"
|
|
|
|
.Ft int
|
2005-06-02 15:42:16 +04:00
|
|
|
.Fn pool_prime "struct pool *pp" "int nitems"
|
1999-04-03 18:50:21 +04:00
|
|
|
.Ft void
|
1998-07-30 02:10:47 +04:00
|
|
|
.Fn pool_sethiwat "struct pool *pp" "int n"
|
1999-04-03 18:50:21 +04:00
|
|
|
.Ft void
|
1998-07-30 02:10:47 +04:00
|
|
|
.Fn pool_setlowat "struct pool *pp" "int n"
|
1997-12-14 02:45:07 +03:00
|
|
|
.Sh DESCRIPTION
|
1998-09-03 05:58:56 +04:00
|
|
|
These utility routines provide management of pools of fixed-sized
|
1998-09-03 04:54:23 +04:00
|
|
|
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
|
1998-09-03 05:58:56 +04:00
|
|
|
resources currently available from the system-wide memory allocator
|
1998-09-03 04:54:23 +04:00
|
|
|
.Pq Xr malloc 9 .
|
2001-07-20 01:35:53 +04:00
|
|
|
.Ss INITIALIZING A POOL
|
1997-12-14 02:45:07 +03:00
|
|
|
The function
|
2001-07-20 01:35:53 +04:00
|
|
|
.Fn pool_init
|
|
|
|
initializes a resource pool.
|
1998-09-03 04:54:23 +04:00
|
|
|
The arguments are:
|
1997-12-14 02:45:07 +03:00
|
|
|
.Pp
|
2002-08-11 16:12:23 +04:00
|
|
|
.Bl -tag -offset indent -width "align_offset"
|
2001-07-20 01:35:53 +04:00
|
|
|
.It Fa pp
|
|
|
|
The handle identifying the pool resource instance.
|
1997-12-14 02:45:07 +03:00
|
|
|
.It Fa size
|
1998-09-03 04:54:23 +04:00
|
|
|
Specifies the size of the memory items managed by the pool.
|
1998-07-30 02:10:47 +04:00
|
|
|
.It Fa align
|
2001-04-06 19:54:10 +04:00
|
|
|
Specifies the memory address alignment of the items returned by
|
1998-07-30 02:10:47 +04:00
|
|
|
.Fn pool_get .
|
1998-09-03 04:54:23 +04:00
|
|
|
This argument must be a power of two.
|
|
|
|
If zero,
|
2003-05-01 12:42:07 +04:00
|
|
|
the alignment defaults to an architecture-specific natural alignment.
|
1998-07-30 02:10:47 +04:00
|
|
|
.It Fa align_offset
|
|
|
|
The offset within an item to which the
|
|
|
|
.Fa align
|
|
|
|
parameter applies.
|
2003-12-21 10:35:37 +03:00
|
|
|
.It Fa flags
|
2005-01-02 00:26:20 +03:00
|
|
|
Should be set to zero or
|
|
|
|
.Dv PR_NOTOUCH .
|
|
|
|
If
|
|
|
|
.Dv PR_NOTOUCH
|
|
|
|
is given, free items are never used to keep internal state so that
|
|
|
|
the pool can be used for non memory backed objects.
|
1997-12-14 02:45:07 +03:00
|
|
|
.It Fa wchan
|
1998-09-03 04:54:23 +04:00
|
|
|
The
|
1997-12-14 02:45:07 +03:00
|
|
|
.Sq wait channel
|
|
|
|
passed on to
|
|
|
|
.Xr tsleep 9
|
|
|
|
if
|
|
|
|
.Fn pool_get
|
|
|
|
must wait for items to be returned to the pool.
|
1998-07-30 02:10:47 +04:00
|
|
|
.It Fa palloc
|
2002-08-11 16:12:23 +04:00
|
|
|
can be set to
|
|
|
|
.Dv NULL
|
|
|
|
or
|
|
|
|
.Dv pool_allocator_kmem ,
|
|
|
|
in which case the default kernel memory allocator will be used.
|
|
|
|
It can also be set to
|
|
|
|
.Dv pool_allocator_nointr
|
|
|
|
when the pool will never be accessed from interrupt context.
|
1997-12-14 02:45:07 +03:00
|
|
|
.El
|
2004-04-25 20:50:56 +04:00
|
|
|
.Pp
|
|
|
|
The
|
|
|
|
.Fn POOL_INIT
|
|
|
|
macro can be used to both declare and initialize a resource pool.
|
|
|
|
The
|
|
|
|
.Fn POOL_INIT
|
|
|
|
macro has the same arguments as the
|
|
|
|
.Fn pool_init
|
|
|
|
function and the resource pool will be initialized automatically
|
|
|
|
during system startup.
|
2001-07-20 01:35:53 +04:00
|
|
|
.Ss DESTROYING A POOL
|
|
|
|
The function
|
|
|
|
.Fn pool_destroy
|
2002-10-14 17:43:14 +04:00
|
|
|
destroys a resource pool.
|
|
|
|
It takes a single argument
|
2001-07-20 01:35:53 +04:00
|
|
|
.Fa pp
|
|
|
|
identifying the pool resource instance.
|
1998-09-03 04:54:23 +04:00
|
|
|
.Ss ALLOCATING ITEMS FROM A POOL
|
1997-12-14 02:45:07 +03:00
|
|
|
.Fn pool_get
|
|
|
|
allocates an item from the pool and returns a pointer to it.
|
2001-07-20 01:35:53 +04:00
|
|
|
The arguments are:
|
1997-12-14 02:45:07 +03:00
|
|
|
.Bl -tag -offset indent -width "flags"
|
|
|
|
.It Fa pp
|
|
|
|
The handle identifying the pool resource instance.
|
|
|
|
.It Fa flags
|
2004-03-24 20:44:22 +03:00
|
|
|
The flags can be used to define behaviour in case the pooled resources
|
|
|
|
are depleted.
|
1997-12-14 02:45:07 +03:00
|
|
|
If no resources are available and
|
2004-03-24 20:44:22 +03:00
|
|
|
.Dv PR_NOWAIT
|
1998-09-03 04:54:23 +04:00
|
|
|
is given,
|
1997-12-14 02:45:07 +03:00
|
|
|
.Fn pool_get
|
|
|
|
returns
|
|
|
|
.Dv NULL .
|
1998-07-30 02:10:47 +04:00
|
|
|
If
|
2004-03-24 20:44:22 +03:00
|
|
|
.Dv PR_WAITOK
|
|
|
|
is given and allocation is attempted with no resources available,
|
|
|
|
the function will sleep until items are returned to the pool.
|
1998-07-30 02:10:47 +04:00
|
|
|
.\"Undefined behaviour results if
|
|
|
|
.\".Dv PR_MALLOCOK
|
|
|
|
.\"is specified on a pool handle that was created using client-provided
|
|
|
|
.\"storage.
|
1999-08-05 08:00:03 +04:00
|
|
|
.\" a bunch of other flags aren't documented.
|
|
|
|
If both
|
2001-06-21 15:59:00 +04:00
|
|
|
.Dv PR_LIMITFAIL
|
1999-08-05 08:00:03 +04:00
|
|
|
and
|
|
|
|
.Dv PR_WAITOK
|
2004-03-24 20:44:22 +03:00
|
|
|
are specified, and the pool has reached its hard limit,
|
2001-06-21 15:59:00 +04:00
|
|
|
.Fn pool_get
|
|
|
|
will return
|
1999-08-05 08:00:03 +04:00
|
|
|
.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.
|
1997-12-14 02:45:07 +03:00
|
|
|
.El
|
1998-09-03 04:54:23 +04:00
|
|
|
.Ss RETURNING ITEMS TO A POOL
|
1997-12-14 02:45:07 +03:00
|
|
|
.Fn pool_put
|
|
|
|
returns the pool item pointed at by
|
|
|
|
.Fa item
|
|
|
|
to the resource pool identified by the pool handle
|
|
|
|
.Fa pp .
|
1998-07-30 02:10:47 +04:00
|
|
|
If the number of available items in the pool exceeds the maximum pool
|
|
|
|
size set by
|
|
|
|
.Fn pool_sethiwat
|
1998-09-03 04:54:23 +04:00
|
|
|
and there are no outstanding requests for pool items,
|
2002-08-11 16:12:23 +04:00
|
|
|
the excess items will be returned to the system.
|
2001-07-20 01:35:53 +04:00
|
|
|
The arguments to
|
|
|
|
.Fn pool_put
|
|
|
|
are:
|
1997-12-14 02:45:07 +03:00
|
|
|
.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
|
1998-09-03 04:54:23 +04:00
|
|
|
.Ss PRIMING A POOL
|
1997-12-14 02:45:07 +03:00
|
|
|
.Fn pool_prime
|
|
|
|
adds items to the pool.
|
2005-06-02 15:42:16 +04:00
|
|
|
Storage space for the items is allocated by using the page allocation
|
2000-05-31 19:27:33 +04:00
|
|
|
routine specified to
|
2005-06-02 15:42:16 +04:00
|
|
|
.Fn pool_create .
|
1998-09-03 04:54:23 +04:00
|
|
|
.Pp
|
2001-07-20 01:35:53 +04:00
|
|
|
The arguments to
|
1998-09-03 04:54:23 +04:00
|
|
|
.Fn pool_prime
|
2001-07-20 01:35:53 +04:00
|
|
|
are:
|
2002-08-11 16:12:23 +04:00
|
|
|
.Bl -tag -offset indent -width "storage"
|
1997-12-14 02:45:07 +03:00
|
|
|
.It Fa pp
|
|
|
|
The handle identifying the pool resource instance.
|
|
|
|
.It Fa nitems
|
1998-09-03 04:54:23 +04:00
|
|
|
The number of items to add to the pool.
|
2000-05-31 19:27:33 +04:00
|
|
|
.El
|
|
|
|
.Pp
|
1997-12-14 02:45:07 +03:00
|
|
|
This function may return
|
|
|
|
.Dv ENOMEM
|
1998-09-03 04:54:23 +04:00
|
|
|
in case the requested number of items could not be allocated.
|
|
|
|
Otherwise,
|
1997-12-14 02:45:07 +03:00
|
|
|
the return value is 0.
|
1998-09-03 04:54:23 +04:00
|
|
|
.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
|
2001-06-21 15:59:00 +04:00
|
|
|
.Sy watermarks .
|
1998-09-03 04:54:23 +04:00
|
|
|
The functions
|
|
|
|
.Fn pool_sethiwat
|
|
|
|
and
|
|
|
|
.Fn pool_setlowat
|
|
|
|
set a pool's high and low watermarks, respectively.
|
1998-07-30 02:10:47 +04:00
|
|
|
.Pp
|
|
|
|
.Fn pool_sethiwat
|
|
|
|
.Bl -tag -offset indent -width "flags"
|
|
|
|
.It Fa pp
|
|
|
|
The handle identifying the pool resource instance.
|
|
|
|
.It Fa n
|
1998-09-03 04:54:23 +04:00
|
|
|
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,
|
2002-08-11 16:12:23 +04:00
|
|
|
any completely unused pages are released immediately.
|
1998-09-03 04:54:23 +04:00
|
|
|
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.
|
1998-07-30 02:10:47 +04:00
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
.Fn pool_setlowat
|
|
|
|
.Bl -tag -offset indent -width "flags"
|
|
|
|
.It Fa pp
|
|
|
|
The handle identifying the pool resource instance.
|
|
|
|
.It Fa n
|
1998-09-03 04:54:23 +04:00
|
|
|
The minimum number of items to keep in the pool.
|
|
|
|
The number pages in the pool will not decrease below the required value to
|
|
|
|
accommodate the minimum number of items specified by this function.
|
1998-07-30 02:10:47 +04:00
|
|
|
Unlike
|
|
|
|
.Fn pool_prime ,
|
2001-06-21 15:59:00 +04:00
|
|
|
this function does not allocate the necessary memory up-front.
|
1998-07-30 02:10:47 +04:00
|
|
|
.El
|
1998-09-03 04:54:23 +04:00
|
|
|
.Ss POTENTIAL PITFALLS
|
1998-02-20 03:00:00 +03:00
|
|
|
Note that undefined behaviour results when mixing the storage providing
|
|
|
|
methods supported by the pool resource routines.
|
1998-07-30 02:10:47 +04:00
|
|
|
.Pp
|
1998-09-03 05:58:56 +04:00
|
|
|
The pool resource code uses a per-pool lock to protect its internal state.
|
1998-09-03 04:54:23 +04:00
|
|
|
If any pool functions are called in an interrupt context,
|
|
|
|
the caller must block all interrupts that might cause the
|
|
|
|
code to be reentered.
|
2004-03-09 01:58:34 +03:00
|
|
|
Additionally, the functions
|
|
|
|
.Fn pool_init
|
|
|
|
and
|
|
|
|
.Fn pool_destroy
|
|
|
|
should never be called in interrupt context.
|
1998-09-03 04:54:23 +04:00
|
|
|
.Ss DIAGNOSTICS
|
1998-09-03 05:58:56 +04:00
|
|
|
Pool usage logs can be enabled by defining the compile-time option
|
1998-07-30 02:10:47 +04:00
|
|
|
.Dv POOL_DIAGNOSTIC .
|
1998-09-03 04:54:23 +04:00
|
|
|
.\" .Sh RETURN VALUES
|
|
|
|
.\" .Sh EXAMPLES
|
1997-12-14 02:45:07 +03:00
|
|
|
.Sh CODE REFERENCES
|
|
|
|
The pool manager is implemented in the file
|
1998-09-02 09:13:07 +04:00
|
|
|
.Pa sys/kern/subr_pool.c .
|
1998-09-03 04:54:23 +04:00
|
|
|
.\" .Sh AUTHOR
|
1997-12-14 02:45:07 +03:00
|
|
|
.Sh SEE ALSO
|
1998-09-03 04:54:23 +04:00
|
|
|
.Xr free 9 ,
|
2001-12-26 03:16:30 +03:00
|
|
|
.Xr malloc 9 ,
|
2005-10-02 19:17:32 +04:00
|
|
|
.Xr pool_cache 9 ,
|
1998-09-03 12:12:56 +04:00
|
|
|
.Xr uvm 9
|
1997-12-14 02:45:07 +03:00
|
|
|
.Sh HISTORY
|
1998-02-06 08:54:12 +03:00
|
|
|
The
|
|
|
|
.Nx
|
|
|
|
pool manager appeared in
|
1998-01-10 04:04:20 +03:00
|
|
|
.Nx 1.4 .
|