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