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