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