FreeBSD/Linux Kernel Cross Reference
sys/sys/pool.h
1 /* $NetBSD: pool.h,v 1.42.2.1 2004/06/22 08:59:04 tron Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #ifndef _SYS_POOL_H_
41 #define _SYS_POOL_H_
42
43 #ifdef _KERNEL
44 #define __POOL_EXPOSE
45 #endif
46
47 #if defined(_KERNEL_OPT)
48 #include "opt_pool.h"
49 #endif
50
51 #ifdef __POOL_EXPOSE
52 #include <sys/lock.h>
53 #include <sys/queue.h>
54 #include <sys/time.h>
55 #include <sys/tree.h>
56 #endif
57
58 #define PCG_NOBJECTS 16
59
60 #define POOL_PADDR_INVALID ((paddr_t) -1)
61
62 #ifdef __POOL_EXPOSE
63 /* The pool cache group. */
64 struct pool_cache_group {
65 TAILQ_ENTRY(pool_cache_group)
66 pcg_list; /* link in the pool cache's group list */
67 u_int pcg_avail; /* # available objects */
68 /* pointers to the objects */
69 struct {
70 void *pcgo_va; /* cache object virtual address */
71 paddr_t pcgo_pa;/* cache object physical address */
72 } pcg_objects[PCG_NOBJECTS];
73 };
74
75 struct pool_cache {
76 TAILQ_ENTRY(pool_cache)
77 pc_poollist; /* entry on pool's group list */
78 TAILQ_HEAD(, pool_cache_group)
79 pc_grouplist; /* Cache group list */
80 struct pool_cache_group
81 *pc_allocfrom; /* group to allocate from */
82 struct pool_cache_group
83 *pc_freeto; /* grop to free to */
84 struct pool *pc_pool; /* parent pool */
85 struct simplelock pc_slock; /* mutex */
86
87 int (*pc_ctor)(void *, void *, int);
88 void (*pc_dtor)(void *, void *);
89 void *pc_arg;
90
91 /* Statistics. */
92 unsigned long pc_hits; /* cache hits */
93 unsigned long pc_misses; /* cache misses */
94
95 unsigned long pc_ngroups; /* # cache groups */
96
97 unsigned long pc_nitems; /* # objects currently in cache */
98 };
99
100 struct pool_allocator {
101 void *(*pa_alloc)(struct pool *, int);
102 void (*pa_free)(struct pool *, void *);
103 unsigned int pa_pagesz;
104
105 /* The following fields are for internal use only. */
106 struct simplelock pa_slock;
107 TAILQ_HEAD(, pool) pa_list; /* list of pools using this allocator */
108 int pa_flags;
109 #define PA_INITIALIZED 0x01
110 #define PA_WANT 0x02 /* wakeup any sleeping pools on free */
111 int pa_pagemask;
112 int pa_pageshift;
113 };
114
115 LIST_HEAD(pool_pagelist,pool_item_header);
116
117 struct pool {
118 TAILQ_ENTRY(pool)
119 pr_poollist;
120 struct pool_pagelist
121 pr_emptypages; /* Empty pages */
122 struct pool_pagelist
123 pr_fullpages; /* Full pages */
124 struct pool_pagelist
125 pr_partpages; /* Partially-allocated pages */
126 struct pool_item_header *pr_curpage;
127 TAILQ_HEAD(,pool_cache)
128 pr_cachelist; /* Caches for this pool */
129 unsigned int pr_size; /* Size of item */
130 unsigned int pr_align; /* Requested alignment, must be 2^n */
131 unsigned int pr_itemoffset; /* Align this offset in item */
132 unsigned int pr_minitems; /* minimum # of items to keep */
133 unsigned int pr_minpages; /* same in page units */
134 unsigned int pr_maxpages; /* maximum # of pages to keep */
135 unsigned int pr_npages; /* # of pages allocated */
136 unsigned int pr_itemsperpage;/* # items that fit in a page */
137 unsigned int pr_slack; /* unused space in a page */
138 unsigned int pr_nitems; /* number of available items in pool */
139 unsigned int pr_nout; /* # items currently allocated */
140 unsigned int pr_hardlimit; /* hard limit to number of allocated
141 items */
142 struct pool_allocator *pr_alloc;/* back-end allocator */
143 TAILQ_ENTRY(pool) pr_alloc_list;/* link on allocator's pool list */
144
145 /* Drain hook. */
146 void (*pr_drain_hook)(void *, int);
147 void *pr_drain_hook_arg;
148
149 const char *pr_wchan; /* tsleep(9) identifier */
150 unsigned int pr_flags; /* r/w flags */
151 unsigned int pr_roflags; /* r/o flags */
152 #define PR_NOWAIT 0x00 /* for symmetry */
153 #define PR_WAITOK 0x02
154 #define PR_WANTED 0x04
155 #define PR_PHINPAGE 0x40
156 #define PR_LOGGING 0x80
157 #define PR_LIMITFAIL 0x100 /* even if waiting, fail if we hit limit */
158 #define PR_RECURSIVE 0x200 /* pool contains pools, for vmstat(8) */
159
160 /*
161 * `pr_slock' protects the pool's data structures when removing
162 * items from or returning items to the pool, or when reading
163 * or updating read/write fields in the pool descriptor.
164 *
165 * We assume back-end page allocators provide their own locking
166 * scheme. They will be called with the pool descriptor _unlocked_,
167 * since the page allocators may block.
168 */
169 struct simplelock pr_slock;
170
171 SPLAY_HEAD(phtree, pool_item_header) pr_phtree;
172
173 int pr_maxcolor; /* Cache colouring */
174 int pr_curcolor;
175 int pr_phoffset; /* Offset in page of page header */
176
177 /*
178 * Warning message to be issued, and a per-time-delta rate cap,
179 * if the hard limit is reached.
180 */
181 const char *pr_hardlimit_warning;
182 struct timeval pr_hardlimit_ratecap;
183 struct timeval pr_hardlimit_warning_last;
184
185 /*
186 * Instrumentation
187 */
188 unsigned long pr_nget; /* # of successful requests */
189 unsigned long pr_nfail; /* # of unsuccessful requests */
190 unsigned long pr_nput; /* # of releases */
191 unsigned long pr_npagealloc; /* # of pages allocated */
192 unsigned long pr_npagefree; /* # of pages released */
193 unsigned int pr_hiwat; /* max # of pages in pool */
194 unsigned long pr_nidle; /* # of idle pages */
195
196 /*
197 * Diagnostic aides.
198 */
199 struct pool_log *pr_log;
200 int pr_curlogentry;
201 int pr_logsize;
202
203 const char *pr_entered_file; /* reentrancy check */
204 long pr_entered_line;
205 };
206 #endif /* __POOL_EXPOSE */
207
208 #ifdef _KERNEL
209 /*
210 * pool_allocator_kmem is the default that all pools get unless
211 * otherwise specified. pool_allocator_nointr is provided for
212 * pools that know they will never be accessed in interrupt
213 * context.
214 */
215 extern struct pool_allocator pool_allocator_kmem;
216 extern struct pool_allocator pool_allocator_nointr;
217
218 void pool_init(struct pool *, size_t, u_int, u_int,
219 int, const char *, struct pool_allocator *);
220 void pool_destroy(struct pool *);
221
222 void pool_set_drain_hook(struct pool *,
223 void (*)(void *, int), void *);
224
225 void *pool_get(struct pool *, int);
226 void pool_put(struct pool *, void *);
227 int pool_reclaim(struct pool *);
228
229 #ifdef POOL_DIAGNOSTIC
230 /*
231 * These versions do reentrancy checking.
232 */
233 void *_pool_get(struct pool *, int, const char *, long);
234 void _pool_put(struct pool *, void *, const char *, long);
235 int _pool_reclaim(struct pool *, const char *, long);
236 #define pool_get(h, f) _pool_get((h), (f), __FILE__, __LINE__)
237 #define pool_put(h, v) _pool_put((h), (v), __FILE__, __LINE__)
238 #define pool_reclaim(h) _pool_reclaim((h), __FILE__, __LINE__)
239 #endif /* POOL_DIAGNOSTIC */
240
241 int pool_prime(struct pool *, int);
242 void pool_setlowat(struct pool *, int);
243 void pool_sethiwat(struct pool *, int);
244 void pool_sethardlimit(struct pool *, int, const char *, int);
245 void pool_drain(void *);
246
247 /*
248 * Debugging and diagnostic aides.
249 */
250 void pool_print(struct pool *, const char *);
251 void pool_printit(struct pool *, const char *,
252 void (*)(const char *, ...));
253 int pool_chk(struct pool *, const char *);
254
255 /*
256 * Pool cache routines.
257 */
258 void pool_cache_init(struct pool_cache *, struct pool *,
259 int (*)(void *, void *, int),
260 void (*)(void *, void *),
261 void *);
262 void pool_cache_destroy(struct pool_cache *);
263 void *pool_cache_get_paddr(struct pool_cache *, int, paddr_t *);
264 #define pool_cache_get(pc, f) pool_cache_get_paddr((pc), (f), NULL)
265 void pool_cache_put_paddr(struct pool_cache *, void *, paddr_t);
266 #define pool_cache_put(pc, o) pool_cache_put_paddr((pc), (o), \
267 POOL_PADDR_INVALID)
268 void pool_cache_destruct_object(struct pool_cache *, void *);
269 void pool_cache_invalidate(struct pool_cache *);
270 #endif /* _KERNEL */
271
272 #endif /* _SYS_POOL_H_ */
Cache object: 68fef2b6aa78ae1b260e2e5b54a1ac14
|