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