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