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