xref: /openbsd-src/lib/libc/db/mpool/mpool.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: mpool.c,v 1.6 1999/02/15 05:11:25 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.6 1999/02/15 05:11:25 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 __P((MPOOL *));
60 static BKT *mpool_look __P((MPOOL *, pgno_t));
61 static int  mpool_write __P((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) __P((void *, pgno_t, void *));
112 	void (*pgout) __P((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 (lseek(mp->fd, off, SEEK_SET) != off)
242 		return (NULL);
243 
244 	if ((nr = read(mp->fd, bp->page, mp->pagesize)) != mp->pagesize) {
245 		if (nr > 0) {
246 			/* A partial read is definitely bad. */
247 			errno = EINVAL;
248 			return (NULL);
249 		} else {
250 			/*
251 			 * A zero-length reads, means you need to create a
252 			 * new page.
253 			 */
254 			memset(bp->page, 0, mp->pagesize);
255 		}
256 	}
257 
258 	/* Set the page number, pin the page. */
259 	bp->pgno = pgno;
260 	if (!(flags & MPOOL_IGNOREPIN))
261 		bp->flags = MPOOL_PINNED;
262 	bp->flags |= MPOOL_INUSE;
263 
264 	/*
265 	 * Add the page to the head of the hash chain and the tail
266 	 * of the lru chain.
267 	 */
268 	head = &mp->hqh[HASHKEY(bp->pgno)];
269 	CIRCLEQ_INSERT_HEAD(head, bp, hq);
270 	CIRCLEQ_INSERT_TAIL(&mp->lqh, bp, q);
271 
272 	/* Run through the user's filter. */
273 	if (mp->pgin != NULL)
274 		(mp->pgin)(mp->pgcookie, bp->pgno, bp->page);
275 
276 	return (bp->page);
277 }
278 
279 /*
280  * mpool_put
281  *	Return a page.
282  */
283 /* ARGSUSED */
284 int
285 mpool_put(mp, page, flags)
286 	MPOOL *mp;
287 	void *page;
288 	u_int flags;
289 {
290 	BKT *bp;
291 
292 #ifdef STATISTICS
293 	++mp->pageput;
294 #endif
295 	bp = (BKT *)((char *)page - sizeof(BKT));
296 #ifdef DEBUG
297 	if (!(bp->flags & MPOOL_PINNED)) {
298 		(void)fprintf(stderr,
299 		    "mpool_put: page %d not pinned\n", bp->pgno);
300 		abort();
301 	}
302 #endif
303 	bp->flags &= ~MPOOL_PINNED;
304 	if (flags & MPOOL_DIRTY)
305 		bp->flags |= flags & MPOOL_DIRTY;
306 	return (RET_SUCCESS);
307 }
308 
309 /*
310  * mpool_close
311  *	Close the buffer pool.
312  */
313 int
314 mpool_close(mp)
315 	MPOOL *mp;
316 {
317 	BKT *bp;
318 
319 	/* Free up any space allocated to the lru pages. */
320 	while ((bp = mp->lqh.cqh_first) != (void *)&mp->lqh) {
321 		CIRCLEQ_REMOVE(&mp->lqh, mp->lqh.cqh_first, q);
322 		free(bp);
323 	}
324 
325 	/* Free the MPOOL cookie. */
326 	free(mp);
327 	return (RET_SUCCESS);
328 }
329 
330 /*
331  * mpool_sync
332  *	Sync the pool to disk.
333  */
334 int
335 mpool_sync(mp)
336 	MPOOL *mp;
337 {
338 	BKT *bp;
339 
340 	/* Walk the lru chain, flushing any dirty pages to disk. */
341 	for (bp = mp->lqh.cqh_first;
342 	    bp != (void *)&mp->lqh; bp = bp->q.cqe_next)
343 		if (bp->flags & MPOOL_DIRTY &&
344 		    mpool_write(mp, bp) == RET_ERROR)
345 			return (RET_ERROR);
346 
347 	/* Sync the file descriptor. */
348 	return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
349 }
350 
351 /*
352  * mpool_bkt
353  *	Get a page from the cache (or create one).
354  */
355 static BKT *
356 mpool_bkt(mp)
357 	MPOOL *mp;
358 {
359 	struct _hqh *head;
360 	BKT *bp;
361 
362 	/* If under the max cached, always create a new page. */
363 	if (mp->curcache < mp->maxcache)
364 		goto new;
365 
366 	/*
367 	 * If the cache is max'd out, walk the lru list for a buffer we
368 	 * can flush.  If we find one, write it (if necessary) and take it
369 	 * off any lists.  If we don't find anything we grow the cache anyway.
370 	 * The cache never shrinks.
371 	 */
372 	for (bp = mp->lqh.cqh_first;
373 	    bp != (void *)&mp->lqh; bp = bp->q.cqe_next)
374 		if (!(bp->flags & MPOOL_PINNED)) {
375 			/* Flush if dirty. */
376 			if (bp->flags & MPOOL_DIRTY &&
377 			    mpool_write(mp, bp) == RET_ERROR)
378 				return (NULL);
379 #ifdef STATISTICS
380 			++mp->pageflush;
381 #endif
382 			/* Remove from the hash and lru queues. */
383 			head = &mp->hqh[HASHKEY(bp->pgno)];
384 			CIRCLEQ_REMOVE(head, bp, hq);
385 			CIRCLEQ_REMOVE(&mp->lqh, bp, q);
386 #ifdef DEBUG
387 			{ void *spage;
388 				spage = bp->page;
389 				memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
390 				bp->page = spage;
391 			}
392 #endif
393 			bp->flags = 0;
394 			return (bp);
395 		}
396 
397 new:	if ((bp = (BKT *)malloc(sizeof(BKT) + mp->pagesize)) == NULL)
398 		return (NULL);
399 #ifdef STATISTICS
400 	++mp->pagealloc;
401 #endif
402 	memset(bp, 0xff, sizeof(BKT) + mp->pagesize);
403 	bp->page = (char *)bp + sizeof(BKT);
404 	bp->flags = 0;
405 	++mp->curcache;
406 	return (bp);
407 }
408 
409 /*
410  * mpool_write
411  *	Write a page to disk.
412  */
413 static int
414 mpool_write(mp, bp)
415 	MPOOL *mp;
416 	BKT *bp;
417 {
418 	off_t off;
419 
420 #ifdef STATISTICS
421 	++mp->pagewrite;
422 #endif
423 
424 	/* Run through the user's filter. */
425 	if (mp->pgout)
426 		(mp->pgout)(mp->pgcookie, bp->pgno, bp->page);
427 
428 	off = mp->pagesize * bp->pgno;
429 	if (lseek(mp->fd, off, SEEK_SET) != off)
430 		return (RET_ERROR);
431 	if (write(mp->fd, bp->page, mp->pagesize) != mp->pagesize)
432 		return (RET_ERROR);
433 
434 	bp->flags &= ~MPOOL_DIRTY;
435 	return (RET_SUCCESS);
436 }
437 
438 /*
439  * mpool_look
440  *	Lookup a page in the cache.
441  */
442 static BKT *
443 mpool_look(mp, pgno)
444 	MPOOL *mp;
445 	pgno_t pgno;
446 {
447 	struct _hqh *head;
448 	BKT *bp;
449 
450 	head = &mp->hqh[HASHKEY(pgno)];
451 	for (bp = head->cqh_first; bp != (void *)head; bp = bp->hq.cqe_next)
452 		if ((bp->pgno == pgno) &&
453 			(bp->flags & MPOOL_INUSE == MPOOL_INUSE)) {
454 #ifdef STATISTICS
455 			++mp->cachehit;
456 #endif
457 			return (bp);
458 		}
459 #ifdef STATISTICS
460 	++mp->cachemiss;
461 #endif
462 	return (NULL);
463 }
464 
465 #ifdef STATISTICS
466 /*
467  * mpool_stat
468  *	Print out cache statistics.
469  */
470 void
471 mpool_stat(mp)
472 	MPOOL *mp;
473 {
474 	BKT *bp;
475 	int cnt;
476 	char *sep;
477 
478 	(void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
479 	(void)fprintf(stderr,
480 	    "page size %lu, cacheing %lu pages of %lu page max cache\n",
481 	    mp->pagesize, mp->curcache, mp->maxcache);
482 	(void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
483 	    mp->pageput, mp->pageget, mp->pagenew);
484 	(void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
485 	    mp->pagealloc, mp->pageflush);
486 	if (mp->cachehit + mp->cachemiss)
487 		(void)fprintf(stderr,
488 		    "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
489 		    ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
490 		    * 100, mp->cachehit, mp->cachemiss);
491 	(void)fprintf(stderr, "%lu page reads, %lu page writes\n",
492 	    mp->pageread, mp->pagewrite);
493 
494 	sep = "";
495 	cnt = 0;
496 	for (bp = mp->lqh.cqh_first;
497 	    bp != (void *)&mp->lqh; bp = bp->q.cqe_next) {
498 		(void)fprintf(stderr, "%s%d", sep, bp->pgno);
499 		if (bp->flags & MPOOL_DIRTY)
500 			(void)fprintf(stderr, "d");
501 		if (bp->flags & MPOOL_PINNED)
502 			(void)fprintf(stderr, "P");
503 		if (++cnt == 10) {
504 			sep = "\n";
505 			cnt = 0;
506 		} else
507 			sep = ", ";
508 
509 	}
510 	(void)fprintf(stderr, "\n");
511 }
512 #endif
513