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