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