]>
Commit | Line | Data |
---|---|---|
9385eb3d A |
1 | /*- |
2 | * Copyright (c) 1990, 1993, 1994 | |
e9ce8d39 A |
3 | * The Regents of the University of California. 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. | |
e9ce8d39 A |
13 | * 4. Neither the name of the University nor the names of its contributors |
14 | * may be used to endorse or promote products derived from this software | |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
28 | */ | |
29 | ||
9385eb3d A |
30 | #if defined(LIBC_SCCS) && !defined(lint) |
31 | static char sccsid[] = "@(#)mpool.c 8.5 (Berkeley) 7/26/94"; | |
32 | #endif /* LIBC_SCCS and not lint */ | |
33 | #include <sys/cdefs.h> | |
1f2f436a | 34 | __FBSDID("$FreeBSD: src/lib/libc/db/mpool/mpool.c,v 1.16 2009/03/28 04:00:46 delphij Exp $"); |
e9ce8d39 | 35 | |
59e0d9fe | 36 | #include "namespace.h" |
e9ce8d39 | 37 | #include <sys/param.h> |
9385eb3d | 38 | #include <sys/queue.h> |
e9ce8d39 A |
39 | #include <sys/stat.h> |
40 | ||
41 | #include <errno.h> | |
42 | #include <stdio.h> | |
43 | #include <stdlib.h> | |
44 | #include <string.h> | |
45 | #include <unistd.h> | |
59e0d9fe | 46 | #include "un-namespace.h" |
e9ce8d39 A |
47 | |
48 | #include <db.h> | |
9385eb3d | 49 | |
e9ce8d39 | 50 | #define __MPOOLINTERFACE_PRIVATE |
9385eb3d | 51 | #include <mpool.h> |
e9ce8d39 | 52 | |
9385eb3d A |
53 | static BKT *mpool_bkt(MPOOL *); |
54 | static BKT *mpool_look(MPOOL *, pgno_t); | |
55 | static int mpool_write(MPOOL *, BKT *); | |
e9ce8d39 A |
56 | |
57 | /* | |
9385eb3d A |
58 | * mpool_open -- |
59 | * Initialize a memory pool. | |
e9ce8d39 | 60 | */ |
1f2f436a | 61 | /* ARGSUSED */ |
e9ce8d39 | 62 | MPOOL * |
1f2f436a | 63 | mpool_open(void *key, int fd, pgno_t pagesize, pgno_t maxcache) |
e9ce8d39 A |
64 | { |
65 | struct stat sb; | |
66 | MPOOL *mp; | |
67 | int entry; | |
68 | ||
9385eb3d A |
69 | /* |
70 | * Get information about the file. | |
71 | * | |
72 | * XXX | |
73 | * We don't currently handle pipes, although we should. | |
74 | */ | |
59e0d9fe | 75 | if (_fstat(fd, &sb)) |
e9ce8d39 | 76 | return (NULL); |
e9ce8d39 A |
77 | if (!S_ISREG(sb.st_mode)) { |
78 | errno = ESPIPE; | |
79 | return (NULL); | |
80 | } | |
81 | ||
9385eb3d A |
82 | /* Allocate and initialize the MPOOL cookie. */ |
83 | if ((mp = (MPOOL *)calloc(1, sizeof(MPOOL))) == NULL) | |
e9ce8d39 | 84 | return (NULL); |
9385eb3d | 85 | TAILQ_INIT(&mp->lqh); |
e9ce8d39 | 86 | for (entry = 0; entry < HASHSIZE; ++entry) |
9385eb3d | 87 | TAILQ_INIT(&mp->hqh[entry]); |
e9ce8d39 | 88 | mp->maxcache = maxcache; |
e9ce8d39 | 89 | mp->npages = sb.st_size / pagesize; |
9385eb3d | 90 | mp->pagesize = pagesize; |
e9ce8d39 | 91 | mp->fd = fd; |
e9ce8d39 A |
92 | return (mp); |
93 | } | |
94 | ||
95 | /* | |
9385eb3d A |
96 | * mpool_filter -- |
97 | * Initialize input/output filters. | |
e9ce8d39 A |
98 | */ |
99 | void | |
1f2f436a A |
100 | mpool_filter(MPOOL *mp, void (*pgin) (void *, pgno_t, void *), |
101 | void (*pgout) (void *, pgno_t, void *), void *pgcookie) | |
e9ce8d39 A |
102 | { |
103 | mp->pgin = pgin; | |
104 | mp->pgout = pgout; | |
105 | mp->pgcookie = pgcookie; | |
106 | } | |
107 | ||
108 | /* | |
9385eb3d A |
109 | * mpool_new -- |
110 | * Get a new page of memory. | |
e9ce8d39 A |
111 | */ |
112 | void * | |
113 | mpool_new(mp, pgnoaddr) | |
114 | MPOOL *mp; | |
115 | pgno_t *pgnoaddr; | |
116 | { | |
9385eb3d A |
117 | struct _hqh *head; |
118 | BKT *bp; | |
e9ce8d39 | 119 | |
9385eb3d A |
120 | if (mp->npages == MAX_PAGE_NUMBER) { |
121 | (void)fprintf(stderr, "mpool_new: page allocation overflow.\n"); | |
ad3c9f2a | 122 | LIBC_ABORT("page allocation overflow"); |
9385eb3d | 123 | } |
e9ce8d39 A |
124 | #ifdef STATISTICS |
125 | ++mp->pagenew; | |
126 | #endif | |
127 | /* | |
9385eb3d A |
128 | * Get a BKT from the cache. Assign a new page number, attach |
129 | * it to the head of the hash chain, the tail of the lru chain, | |
130 | * and return. | |
e9ce8d39 | 131 | */ |
9385eb3d | 132 | if ((bp = mpool_bkt(mp)) == NULL) |
e9ce8d39 | 133 | return (NULL); |
9385eb3d A |
134 | *pgnoaddr = bp->pgno = mp->npages++; |
135 | bp->flags = MPOOL_PINNED; | |
136 | ||
137 | head = &mp->hqh[HASHKEY(bp->pgno)]; | |
138 | TAILQ_INSERT_HEAD(head, bp, hq); | |
139 | TAILQ_INSERT_TAIL(&mp->lqh, bp, q); | |
140 | return (bp->page); | |
e9ce8d39 A |
141 | } |
142 | ||
143 | /* | |
9385eb3d A |
144 | * mpool_get |
145 | * Get a page. | |
e9ce8d39 | 146 | */ |
1f2f436a | 147 | /* ARGSUSED */ |
e9ce8d39 | 148 | void * |
1f2f436a A |
149 | mpool_get(MPOOL *mp, pgno_t pgno, |
150 | u_int flags) /* XXX not used? */ | |
e9ce8d39 | 151 | { |
9385eb3d A |
152 | struct _hqh *head; |
153 | BKT *bp; | |
e9ce8d39 A |
154 | off_t off; |
155 | int nr; | |
156 | ||
9385eb3d A |
157 | /* Check for attempt to retrieve a non-existent page. */ |
158 | if (pgno >= mp->npages) { | |
159 | errno = EINVAL; | |
160 | return (NULL); | |
161 | } | |
162 | ||
e9ce8d39 | 163 | #ifdef STATISTICS |
9385eb3d | 164 | ++mp->pageget; |
e9ce8d39 | 165 | #endif |
9385eb3d A |
166 | |
167 | /* Check for a page that is cached. */ | |
168 | if ((bp = mpool_look(mp, pgno)) != NULL) { | |
e9ce8d39 | 169 | #ifdef DEBUG |
9385eb3d A |
170 | if (bp->flags & MPOOL_PINNED) { |
171 | (void)fprintf(stderr, | |
172 | "mpool_get: page %d already pinned\n", bp->pgno); | |
ad3c9f2a | 173 | LIBC_ABORT("page %d already pinned", bp->pgno); |
9385eb3d | 174 | } |
e9ce8d39 | 175 | #endif |
9385eb3d A |
176 | /* |
177 | * Move the page to the head of the hash chain and the tail | |
178 | * of the lru chain. | |
179 | */ | |
180 | head = &mp->hqh[HASHKEY(bp->pgno)]; | |
181 | TAILQ_REMOVE(head, bp, hq); | |
182 | TAILQ_INSERT_HEAD(head, bp, hq); | |
183 | TAILQ_REMOVE(&mp->lqh, bp, q); | |
184 | TAILQ_INSERT_TAIL(&mp->lqh, bp, q); | |
185 | ||
186 | /* Return a pinned page. */ | |
187 | bp->flags |= MPOOL_PINNED; | |
188 | return (bp->page); | |
e9ce8d39 A |
189 | } |
190 | ||
191 | /* Get a page from the cache. */ | |
9385eb3d | 192 | if ((bp = mpool_bkt(mp)) == NULL) |
e9ce8d39 | 193 | return (NULL); |
e9ce8d39 | 194 | |
9385eb3d | 195 | /* Read in the contents. */ |
e9ce8d39 A |
196 | #ifdef STATISTICS |
197 | ++mp->pageread; | |
198 | #endif | |
e9ce8d39 | 199 | off = mp->pagesize * pgno; |
3d9156a7 A |
200 | nr = pread(mp->fd, bp->page, mp->pagesize, off); |
201 | if (nr != mp->pagesize) { | |
e9ce8d39 A |
202 | if (nr >= 0) |
203 | errno = EFTYPE; | |
204 | return (NULL); | |
205 | } | |
e9ce8d39 | 206 | |
9385eb3d A |
207 | /* Set the page number, pin the page. */ |
208 | bp->pgno = pgno; | |
209 | bp->flags = MPOOL_PINNED; | |
210 | ||
211 | /* | |
212 | * Add the page to the head of the hash chain and the tail | |
213 | * of the lru chain. | |
214 | */ | |
215 | head = &mp->hqh[HASHKEY(bp->pgno)]; | |
216 | TAILQ_INSERT_HEAD(head, bp, hq); | |
217 | TAILQ_INSERT_TAIL(&mp->lqh, bp, q); | |
218 | ||
219 | /* Run through the user's filter. */ | |
220 | if (mp->pgin != NULL) | |
221 | (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); | |
222 | ||
223 | return (bp->page); | |
e9ce8d39 A |
224 | } |
225 | ||
226 | /* | |
9385eb3d A |
227 | * mpool_put |
228 | * Return a page. | |
e9ce8d39 | 229 | */ |
1f2f436a | 230 | /* ARGSUSED */ |
e9ce8d39 | 231 | int |
1f2f436a | 232 | mpool_put(MPOOL *mp, void *page, u_int flags) |
e9ce8d39 | 233 | { |
9385eb3d | 234 | BKT *bp; |
e9ce8d39 A |
235 | |
236 | #ifdef STATISTICS | |
237 | ++mp->pageput; | |
238 | #endif | |
9385eb3d | 239 | bp = (BKT *)((char *)page - sizeof(BKT)); |
e9ce8d39 | 240 | #ifdef DEBUG |
9385eb3d A |
241 | if (!(bp->flags & MPOOL_PINNED)) { |
242 | (void)fprintf(stderr, | |
243 | "mpool_put: page %d not pinned\n", bp->pgno); | |
ad3c9f2a | 244 | LIBC_ABORT("page %d not pinned", bp->pgno); |
e9ce8d39 A |
245 | } |
246 | #endif | |
9385eb3d A |
247 | bp->flags &= ~MPOOL_PINNED; |
248 | bp->flags |= flags & MPOOL_DIRTY; | |
e9ce8d39 A |
249 | return (RET_SUCCESS); |
250 | } | |
251 | ||
252 | /* | |
9385eb3d A |
253 | * mpool_close |
254 | * Close the buffer pool. | |
e9ce8d39 A |
255 | */ |
256 | int | |
1f2f436a | 257 | mpool_close(MPOOL *mp) |
e9ce8d39 | 258 | { |
9385eb3d | 259 | BKT *bp; |
e9ce8d39 A |
260 | |
261 | /* Free up any space allocated to the lru pages. */ | |
9385eb3d A |
262 | while (!TAILQ_EMPTY(&mp->lqh)) { |
263 | bp = TAILQ_FIRST(&mp->lqh); | |
264 | TAILQ_REMOVE(&mp->lqh, bp, q); | |
265 | free(bp); | |
e9ce8d39 | 266 | } |
9385eb3d A |
267 | |
268 | /* Free the MPOOL cookie. */ | |
e9ce8d39 A |
269 | free(mp); |
270 | return (RET_SUCCESS); | |
271 | } | |
272 | ||
273 | /* | |
9385eb3d A |
274 | * mpool_sync |
275 | * Sync the pool to disk. | |
e9ce8d39 A |
276 | */ |
277 | int | |
1f2f436a | 278 | mpool_sync(MPOOL *mp) |
e9ce8d39 | 279 | { |
9385eb3d | 280 | BKT *bp; |
e9ce8d39 | 281 | |
9385eb3d | 282 | /* Walk the lru chain, flushing any dirty pages to disk. */ |
ad3c9f2a A |
283 | TAILQ_FOREACH(bp, &mp->lqh, q) { |
284 | if (bp->flags & MPOOL_DIRTY) | |
285 | if (mpool_write(mp, bp) == RET_ERROR) { | |
286 | return (RET_ERROR); | |
287 | } else { | |
288 | /* 4874757: Re-run through the user's pgin filter. */ | |
289 | if (mp->pgin != NULL) | |
290 | (mp->pgin)(mp->pgcookie, bp->pgno, bp->page); | |
291 | } | |
292 | } | |
9385eb3d A |
293 | |
294 | /* Sync the file descriptor. */ | |
59e0d9fe | 295 | return (_fsync(mp->fd) ? RET_ERROR : RET_SUCCESS); |
e9ce8d39 A |
296 | } |
297 | ||
298 | /* | |
9385eb3d A |
299 | * mpool_bkt |
300 | * Get a page from the cache (or create one). | |
e9ce8d39 A |
301 | */ |
302 | static BKT * | |
1f2f436a | 303 | mpool_bkt(MPOOL *mp) |
e9ce8d39 | 304 | { |
9385eb3d A |
305 | struct _hqh *head; |
306 | BKT *bp; | |
e9ce8d39 | 307 | |
9385eb3d | 308 | /* If under the max cached, always create a new page. */ |
e9ce8d39 A |
309 | if (mp->curcache < mp->maxcache) |
310 | goto new; | |
311 | ||
312 | /* | |
9385eb3d A |
313 | * If the cache is max'd out, walk the lru list for a buffer we |
314 | * can flush. If we find one, write it (if necessary) and take it | |
315 | * off any lists. If we don't find anything we grow the cache anyway. | |
e9ce8d39 A |
316 | * The cache never shrinks. |
317 | */ | |
9385eb3d A |
318 | TAILQ_FOREACH(bp, &mp->lqh, q) |
319 | if (!(bp->flags & MPOOL_PINNED)) { | |
320 | /* Flush if dirty. */ | |
321 | if (bp->flags & MPOOL_DIRTY && | |
322 | mpool_write(mp, bp) == RET_ERROR) | |
e9ce8d39 | 323 | return (NULL); |
e9ce8d39 A |
324 | #ifdef STATISTICS |
325 | ++mp->pageflush; | |
326 | #endif | |
9385eb3d A |
327 | /* Remove from the hash and lru queues. */ |
328 | head = &mp->hqh[HASHKEY(bp->pgno)]; | |
329 | TAILQ_REMOVE(head, bp, hq); | |
330 | TAILQ_REMOVE(&mp->lqh, bp, q); | |
e9ce8d39 | 331 | #ifdef DEBUG |
9385eb3d A |
332 | { void *spage; |
333 | spage = bp->page; | |
334 | memset(bp, 0xff, sizeof(BKT) + mp->pagesize); | |
335 | bp->page = spage; | |
e9ce8d39 A |
336 | } |
337 | #endif | |
9385eb3d | 338 | return (bp); |
e9ce8d39 A |
339 | } |
340 | ||
1f2f436a | 341 | new: if ((bp = (BKT *)calloc(1, sizeof(BKT) + mp->pagesize)) == NULL) |
e9ce8d39 A |
342 | return (NULL); |
343 | #ifdef STATISTICS | |
344 | ++mp->pagealloc; | |
e9ce8d39 | 345 | #endif |
9385eb3d | 346 | bp->page = (char *)bp + sizeof(BKT); |
e9ce8d39 | 347 | ++mp->curcache; |
9385eb3d | 348 | return (bp); |
e9ce8d39 A |
349 | } |
350 | ||
351 | /* | |
9385eb3d A |
352 | * mpool_write |
353 | * Write a page to disk. | |
e9ce8d39 A |
354 | */ |
355 | static int | |
1f2f436a | 356 | mpool_write(MPOOL *mp, BKT *bp) |
e9ce8d39 A |
357 | { |
358 | off_t off; | |
359 | ||
e9ce8d39 A |
360 | #ifdef STATISTICS |
361 | ++mp->pagewrite; | |
362 | #endif | |
9385eb3d A |
363 | |
364 | /* Run through the user's filter. */ | |
365 | if (mp->pgout) | |
366 | (mp->pgout)(mp->pgcookie, bp->pgno, bp->page); | |
367 | ||
368 | off = mp->pagesize * bp->pgno; | |
3d9156a7 | 369 | if (pwrite(mp->fd, bp->page, mp->pagesize, off) != mp->pagesize) |
e9ce8d39 | 370 | return (RET_ERROR); |
9385eb3d A |
371 | |
372 | bp->flags &= ~MPOOL_DIRTY; | |
e9ce8d39 A |
373 | return (RET_SUCCESS); |
374 | } | |
375 | ||
376 | /* | |
9385eb3d A |
377 | * mpool_look |
378 | * Lookup a page in the cache. | |
e9ce8d39 A |
379 | */ |
380 | static BKT * | |
1f2f436a | 381 | mpool_look(MPOOL *mp, pgno_t pgno) |
e9ce8d39 | 382 | { |
9385eb3d A |
383 | struct _hqh *head; |
384 | BKT *bp; | |
e9ce8d39 | 385 | |
9385eb3d A |
386 | head = &mp->hqh[HASHKEY(pgno)]; |
387 | TAILQ_FOREACH(bp, head, hq) | |
388 | if (bp->pgno == pgno) { | |
e9ce8d39 A |
389 | #ifdef STATISTICS |
390 | ++mp->cachehit; | |
391 | #endif | |
9385eb3d | 392 | return (bp); |
e9ce8d39 A |
393 | } |
394 | #ifdef STATISTICS | |
395 | ++mp->cachemiss; | |
396 | #endif | |
397 | return (NULL); | |
398 | } | |
399 | ||
400 | #ifdef STATISTICS | |
401 | /* | |
9385eb3d A |
402 | * mpool_stat |
403 | * Print out cache statistics. | |
e9ce8d39 A |
404 | */ |
405 | void | |
1f2f436a | 406 | mpool_stat(MPOOL *mp) |
e9ce8d39 | 407 | { |
9385eb3d | 408 | BKT *bp; |
e9ce8d39 A |
409 | int cnt; |
410 | char *sep; | |
411 | ||
1f2f436a | 412 | (void)fprintf(stderr, "%lu pages in the file\n", mp->npages); |
e9ce8d39 | 413 | (void)fprintf(stderr, |
1f2f436a | 414 | "page size %lu, cacheing %lu pages of %lu page max cache\n", |
e9ce8d39 A |
415 | mp->pagesize, mp->curcache, mp->maxcache); |
416 | (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n", | |
417 | mp->pageput, mp->pageget, mp->pagenew); | |
418 | (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n", | |
419 | mp->pagealloc, mp->pageflush); | |
420 | if (mp->cachehit + mp->cachemiss) | |
421 | (void)fprintf(stderr, | |
422 | "%.0f%% cache hit rate (%lu hits, %lu misses)\n", | |
423 | ((double)mp->cachehit / (mp->cachehit + mp->cachemiss)) | |
424 | * 100, mp->cachehit, mp->cachemiss); | |
425 | (void)fprintf(stderr, "%lu page reads, %lu page writes\n", | |
426 | mp->pageread, mp->pagewrite); | |
427 | ||
428 | sep = ""; | |
429 | cnt = 0; | |
9385eb3d A |
430 | TAILQ_FOREACH(bp, &mp->lqh, q) { |
431 | (void)fprintf(stderr, "%s%d", sep, bp->pgno); | |
432 | if (bp->flags & MPOOL_DIRTY) | |
e9ce8d39 | 433 | (void)fprintf(stderr, "d"); |
9385eb3d | 434 | if (bp->flags & MPOOL_PINNED) |
e9ce8d39 A |
435 | (void)fprintf(stderr, "P"); |
436 | if (++cnt == 10) { | |
437 | sep = "\n"; | |
438 | cnt = 0; | |
439 | } else | |
440 | sep = ", "; | |
441 | ||
442 | } | |
443 | (void)fprintf(stderr, "\n"); | |
444 | } | |
445 | #endif |