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