1.\" $NetBSD: pool_cache.9,v 1.19 2011/11/15 00:32:34 jym Exp $ 2.\" 3.\" Copyright (c)2003 YAMAMOTO Takashi, 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.\" Copyright (c) 1997, 1999, 2000, 2007, 2008 The NetBSD Foundation, Inc. 28.\" All rights reserved. 29.\" 30.\" This code is derived from software contributed to The NetBSD Foundation 31.\" by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace 32.\" Simulation Facility, NASA Ames Research Center, and by Andrew Doran. 33.\" 34.\" Redistribution and use in source and binary forms, with or without 35.\" modification, are permitted provided that the following conditions 36.\" are met: 37.\" 1. Redistributions of source code must retain the above copyright 38.\" notice, this list of conditions and the following disclaimer. 39.\" 2. Redistributions in binary form must reproduce the above copyright 40.\" notice, this list of conditions and the following disclaimer in the 41.\" documentation and/or other materials provided with the distribution. 42.\" 43.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 44.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 45.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 46.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 47.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 48.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 49.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 50.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 51.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 52.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 53.\" POSSIBILITY OF SUCH DAMAGE. 54.\" 55.\" ------------------------------------------------------------ 56.Dd November 15, 2011 57.Dt POOL_CACHE 9 58.Os 59.\" ------------------------------------------------------------ 60.Sh NAME 61.Nm pool_cache , 62.Nm pool_cache_init , 63.Nm pool_cache_destroy , 64.Nm pool_cache_get_paddr , 65.Nm pool_cache_get , 66.Nm pool_cache_put_paddr , 67.Nm pool_cache_put , 68.Nm pool_cache_destruct_object , 69.Nm pool_cache_invalidate , 70.Nm pool_cache_sethiwat , 71.Nm pool_cache_setlowat , 72.Nm pool_cache_sethardlimit 73.Nd resource-pool cache manager 74.\" ------------------------------------------------------------ 75.Sh SYNOPSIS 76.In sys/pool.h 77.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 78.Ft pool_cache_t 79.Fn pool_cache_init \ 80"size_t size" "u_int align" "u_int align_offset" "int flags" \ 81"const char *name" "struct pool_allocator *palloc" "int ipl" \ 82"int (*ctor)(void *, void *, int)" "void (*dtor)(void *, void *)" "void *arg" 83.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 84.Ft void 85.Fn pool_cache_destroy \ 86"pool_cache_t pc" 87.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 88.Ft void * 89.Fn pool_cache_get_paddr \ 90"pool_cache_t pc" "int flags" "paddr_t *pap" 91.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 92.Ft void * 93.Fn pool_cache_get \ 94"pool_cache_t pc" "int flags" 95.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 96.Ft void 97.Fn pool_cache_put_paddr \ 98"pool_cache_t pc" "void *object" "paddr_t pa" 99.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 100.Ft void 101.Fn pool_cache_put \ 102"pool_cache_t pc" "void *object" 103.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 104.Ft void 105.Fn pool_cache_destruct_object \ 106"pool_cache_t pc" "void *object" 107.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 108.Ft void 109.Fn pool_cache_invalidate \ 110"pool_cache_t pc" 111.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 112.Ft void 113.Fn pool_cache_sethiwat \ 114"pool_cache_t pc" "int nitems" 115.Ft void 116.Fn pool_cache_setlowat \ 117"pool_cache_t pc" "int nitems" 118.Ft void 119.Fn pool_cache_sethardlimit \ 120"pool_cache_t pc" "int nitems" "const char *warnmess" "int ratecap" 121.\" ------------------------------------------------------------ 122.Sh DESCRIPTION 123These utility routines provide management of pools of fixed-sized 124areas of memory. 125Resource pools set aside an amount of memory for exclusive use by the resource 126pool owner. 127This can be used by applications to guarantee the availability of a minimum 128amount of memory needed to continue operation independent of the memory 129resources currently available from the system-wide memory allocator. 130.Pp 131.Nm 132follows the 133.Xr pool 9 134API closely and offers routines that are functionally equivalent to 135their 136.Xr pool 9 137counterparts. 138In addition, 139.Nm 140provides object management functions used to manipulate 141objects allocated from the pool. 142It also maintains global and per-CPU caches, both levels 143of cache work together to allow for low overhead 144allocation and release of objects, and improved L1/L2/L3 hardware 145cache locality in multiprocessor systems. 146.\" ------------------------------------------------------------ 147.Sh FUNCTIONS 148.Bl -tag -width compact 149.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 150.It Fn pool_cache_init "size" "align" "align_offset" "flags" \ 151"name" "palloc" "ipl" "ctor" "dtor" "arg" 152.Pp 153Allocate and initialize a pool cache. 154The arguments are: 155.Pp 156.Bl -tag -width four 157.It Fa size 158.Pp 159Specifies the size of the memory items managed by the pool. 160.It Fa align 161.Pp 162Specifies the memory address alignment of the items returned by 163.Fn pool_cache_get . 164This argument must be a power of two. 165If zero, 166the alignment defaults to an architecture-specific natural alignment. 167.It Fa align_offset 168.Pp 169The offset within an item to which the 170.Fa align 171parameter applies. 172.It Fa flags 173.Pp 174Should be set to zero or 175.Dv PR_NOTOUCH . 176If 177.Dv PR_NOTOUCH 178is given, free items are never used to keep internal state so that 179the pool can be used for non memory backed objects. 180.It Fa name 181.Pp 182The name used to identify the object in diagnostic output. 183.It Fa palloc 184.Pp 185Should be typically be set to NULL, instructing 186.Fn pool_cache_init 187to select an appropriate back-end allocator. 188Alternate allocators can be used to partition space from arbitrary sources. 189Use of alternate allocators is not documented here as it is not a stable, 190endorsed part of the API. 191.It Fa ipl 192.Pp 193Specifies an interrupt priority level that will block all interrupt 194handlers that could potentially access the pool. 195The 196.Nm 197facility provides its own synchronization. 198The users of any given 199.Nm 200need not provide additional synchronization for access to it. 201.It Fa ctor 202.Pp 203Specifies a constructor used to initialize newly allocated objects. 204If no constructor is required, specify 205.Dv NULL . 206The first argument to 207.Fa ctor 208is 209.Fa arg , 210the second is the new object, and the third is 211.Fa flags . 212.It Fa dtor 213.Pp 214Specifies a destructor used to destroy cached objects prior to 215their release to backing store. 216If no destructor is required, specify 217.Dv NULL . 218The first argument to 219.Fa dtor 220is 221.Fa arg , 222and the second is the object. 223.It Fa arg 224.Pp 225This value of this argument will be passed to both the constructor 226and destructor routines. 227.El 228.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 229.It Fn pool_cache_destroy "pc" 230.Pp 231Destroy a pool cache 232.Fa pc . 233All other access to the cache must be stopped before this call 234can be made. 235.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 236.It Fn pool_cache_get_paddr "pc" "flags" "pap" 237.Pp 238Get an object from a pool cache 239.Fa pc . 240If 241.Fa pap 242is not 243.Dv NULL , 244physical address of the object or 245.Dv POOL_PADDR_INVALID 246will be returned via it. 247.Fa flags 248will be passed to 249.Fn pool_get 250function of the backing 251.Xr pool 9 252and the object constructor specified when the pool cache is created by 253.Fn pool_cache_init . 254.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 255.It Fn pool_cache_get "pc" "flags" 256.Pp 257.Fn pool_cache_get 258is the same as 259.Fn pool_cache_get_paddr 260with 261.Dv NULL 262.Fa pap 263argument. 264It's implemented as a macro. 265.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 266.It Fn pool_cache_put_paddr "pc" "object" "pa" 267.Pp 268Put an object 269.Fa object 270back to the pool cache 271.Fa pc . 272.Fa pa 273should be physical address of the object 274.Fa object 275or 276.Dv POOL_PADDR_INVALID . 277.Fa pp . 278If the number of available items in the backing pool exceeds the maximum 279pool size set by 280.Fn pool_cache_sethiwat 281and there are no outstanding requests for pool items, 282the excess items will be returned to the system. 283.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 284.It Fn pool_cache_put "pc" "object" 285.Pp 286.Fn pool_cache_put 287is the same as 288.Fn pool_cache_put_paddr 289with 290.Dv POOL_PADDR_INVALID 291.Fa pa 292argument. 293It's implemented as a macro. 294.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 295.It Fn pool_cache_destruct_object "pc" "object" 296.Pp 297Force destruction of an object 298.Fa object 299and release it back into the pool. 300.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 301.It Fn pool_cache_invalidate "pc" 302.Pp 303Invalidate a pool cache 304.Fa pc . 305All objects in the cache will be destructed and freed back to the pool 306backing the cache. 307For pool caches that vend constructed objects, consumers of this API 308must take care to provide proper synchronization between the input to 309the constructor and cache invalidation. 310.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 311.It Fn pool_cache_sethiwat "pc" "nitems" 312.Pp 313A pool will attempt to increase its resource usage to keep up with the demand 314for its items. 315Conversely, 316it will return unused memory to the system should the number of accumulated 317unused items in the pool exceed a programmable limit. 318The limits for the minimum and maximum number of items which a pool should keep 319at hand are known as the high and low 320.Sy watermarks . 321.Pp 322The function 323.Fn pool_cache_sethiwat 324sets the backing pool's high water mark. 325As items are returned and the total number of pages in the pool is larger 326than the maximum set by this function, 327any completely unused pages are released immediately. 328If this function is not used to specify a maximum number of items, 329the pages will remain associated with the pool until the system runs low 330on memory, 331at which point the VM system will try to reclaim unused pages. 332.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 333.It Fn pool_cache_setlowat "pc" "nitems" 334.Pp 335Set the minimum number of items to keep in the pool. 336The number pages in the pool will not decrease below the required value to 337accommodate the minimum number of items specified by this function. 338.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 339.It Fn pool_cache_sethardlimit "pc" "nitems" "warnmess" "ratecap" 340Set the hard limit for the backing 341.Xr pool 9 342to 343.Fa nitems . 344When the hard limit is reached, the warning message 345.Fa warnmess 346will be logged. 347.Fa ratecap 348represents the minimal interval (in seconds) after which another warning 349message is issued when the pool hits its hard limit again. 350.El 351.\" ------------------------------------------------------------ 352.Sh CODE REFERENCES 353The 354.Nm 355subsystem is implemented within the file 356.Pa sys/kern/subr_pool.c . 357.Sh SEE ALSO 358.Xr intro 9 , 359.Xr kmem 9 , 360.Xr memoryallocators 9 , 361.Xr percpu 9 , 362.Xr pool 9 363