2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: mpool.c /main/4 1996/10/04 09:47:13 drk $ */
25 * Copyright (c) 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #if defined(LIBC_SCCS) && !defined(lint)
58 static char sccsid[] = "@(#)mpool.c 8.1 (Berkeley) 6/6/93";
59 #endif /* LIBC_SCCS and not lint */
61 #include <sys/param.h>
71 #define __MPOOLINTERFACE_PRIVATE
74 static BKT *mpool_bkt __P((MPOOL *));
75 static BKT *mpool_look __P((MPOOL *, pgno_t));
76 static int mpool_write __P((MPOOL *, BKT *));
78 static void __mpoolerr __P((const char *fmt, ...));
82 * MPOOL_OPEN -- initialize a memory pool.
85 * key: Shared buffer key.
86 * fd: File descriptor.
87 * pagesize: File page size.
88 * maxcache: Max number of cached pages.
91 * MPOOL pointer, NULL on error.
94 mpool_open(key, fd, pagesize, maxcache)
97 pgno_t pagesize, maxcache;
106 * We should only set st_size to 0 for pipes -- 4.4BSD has the fix so
107 * that stat(2) returns true for ISSOCK on pipes. Until then, this is
110 if (!S_ISREG(sb.st_mode)) {
115 if ((mp = malloc(sizeof(MPOOL))) == NULL)
117 mp->free.cnext = mp->free.cprev = (BKT *)&mp->free;
118 mp->lru.cnext = mp->lru.cprev = (BKT *)&mp->lru;
119 for (entry = 0; entry < HASHSIZE; ++entry)
120 mp->hashtable[entry].hnext = mp->hashtable[entry].hprev =
121 mp->hashtable[entry].cnext = mp->hashtable[entry].cprev =
122 (BKT *)&mp->hashtable[entry];
124 mp->maxcache = maxcache;
125 mp->pagesize = pagesize;
126 mp->npages = sb.st_size / pagesize;
129 mp->pgin = mp->pgout = NULL;
132 mp->cachehit = mp->cachemiss = mp->pagealloc = mp->pageflush =
133 mp->pageget = mp->pagenew = mp->pageput = mp->pageread =
140 * MPOOL_FILTER -- initialize input/output filters.
143 * pgin: Page in conversion routine.
144 * pgout: Page out conversion routine.
145 * pgcookie: Cookie for page in/out routines.
148 mpool_filter(mp, pgin, pgout, pgcookie)
150 void (*pgin) __P((void *, pgno_t, void *));
151 void (*pgout) __P((void *, pgno_t, void *));
156 mp->pgcookie = pgcookie;
160 * MPOOL_NEW -- get a new page
164 * pgnoadddr: place to store new page number
166 * RET_ERROR, RET_SUCCESS
169 mpool_new(mp, pgnoaddr)
180 * Get a BKT from the cache. Assign a new page number, attach it to
181 * the hash and lru chains and return.
183 if ((b = mpool_bkt(mp)) == NULL)
185 *pgnoaddr = b->pgno = mp->npages++;
186 b->flags = MPOOL_PINNED;
188 inschain(b, &mp->lru);
193 * MPOOL_GET -- get a page from the pool
201 * RET_ERROR, RET_SUCCESS
204 mpool_get(mp, pgno, flags)
207 u_int flags; /* XXX not used? */
215 * If asking for a specific page that is already in the cache, find
218 if (b = mpool_look(mp, pgno)) {
223 if (b->flags & MPOOL_PINNED)
224 __mpoolerr("mpool_get: page %d already pinned",
228 inschain(b, &mp->lru);
229 b->flags |= MPOOL_PINNED;
233 /* Not allowed to retrieve a non-existent page. */
234 if (pgno >= mp->npages) {
239 /* Get a page from the cache. */
240 if ((b = mpool_bkt(mp)) == NULL)
243 b->flags = MPOOL_PINNED;
248 /* Read in the contents. */
249 off = mp->pagesize * pgno;
250 if (lseek(mp->fd, off, SEEK_SET) != off)
252 if ((nr = read(mp->fd, b->page, mp->pagesize)) != mp->pagesize) {
258 (mp->pgin)(mp->pgcookie, b->pgno, b->page);
261 inschain(b, &mp->lru);
269 * MPOOL_PUT -- return a page to the pool
277 * RET_ERROR, RET_SUCCESS
280 mpool_put(mp, page, flags)
293 baddr = (BKT *)((char *)page - sizeof(BKT));
295 if (!(baddr->flags & MPOOL_PINNED))
296 __mpoolerr("mpool_put: page %d not pinned", b->pgno);
297 for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
298 if (b == (BKT *)&mp->lru)
299 __mpoolerr("mpool_put: %0x: bad address", baddr);
304 baddr->flags &= ~MPOOL_PINNED;
305 baddr->flags |= flags & MPOOL_DIRTY;
306 return (RET_SUCCESS);
310 * MPOOL_CLOSE -- close the buffer pool
316 * RET_ERROR, RET_SUCCESS
324 /* Free up any space allocated to the lru pages. */
325 for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = next) {
330 return (RET_SUCCESS);
334 * MPOOL_SYNC -- sync the file to disk.
340 * RET_ERROR, RET_SUCCESS
348 for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
349 if (b->flags & MPOOL_DIRTY && mpool_write(mp, b) == RET_ERROR)
351 return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
355 * MPOOL_BKT -- get/create a BKT from the cache
361 * NULL on failure and a pointer to the BKT on success
369 if (mp->curcache < mp->maxcache)
373 * If the cache is maxxed out, search the lru list for a buffer we
374 * can flush. If we find one, write it if necessary and take it off
375 * any lists. If we don't find anything we grow the cache anyway.
376 * The cache never shrinks.
378 for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
379 if (!(b->flags & MPOOL_PINNED)) {
380 if (b->flags & MPOOL_DIRTY &&
381 mpool_write(mp, b) == RET_ERROR)
392 memset(b, 0xff, sizeof(BKT) + mp->pagesize);
399 new: if ((b = malloc(sizeof(BKT) + mp->pagesize)) == NULL)
407 memset(b, 0xff, sizeof(BKT) + mp->pagesize);
411 b->page = (char *)b + sizeof(BKT);
418 * MPOOL_WRITE -- sync a page to disk
424 * RET_ERROR, RET_SUCCESS
434 (mp->pgout)(mp->pgcookie, b->pgno, b->page);
439 off = mp->pagesize * b->pgno;
440 if (lseek(mp->fd, off, SEEK_SET) != off)
442 if (write(mp->fd, b->page, mp->pagesize) != mp->pagesize)
444 b->flags &= ~MPOOL_DIRTY;
445 return (RET_SUCCESS);
449 * MPOOL_LOOK -- lookup a page
456 * NULL on failure and a pointer to the BKT on success
467 * If find the buffer, put it first on the hash chain so can
468 * find it again quickly.
470 tb = &mp->hashtable[HASHKEY(pgno)];
471 for (b = tb->hnext; b != (BKT *)tb; b = b->hnext)
472 if (b->pgno == pgno) {
486 * MPOOL_STAT -- cache statistics
499 (void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
500 (void)fprintf(stderr,
501 "page size %lu, cacheing %lu pages of %lu page max cache\n",
502 mp->pagesize, mp->curcache, mp->maxcache);
503 (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
504 mp->pageput, mp->pageget, mp->pagenew);
505 (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
506 mp->pagealloc, mp->pageflush);
507 if (mp->cachehit + mp->cachemiss)
508 (void)fprintf(stderr,
509 "%.0f%% cache hit rate (%lu hits, %lu misses)\n",
510 ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
511 * 100, mp->cachehit, mp->cachemiss);
512 (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
513 mp->pageread, mp->pagewrite);
517 for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
518 (void)fprintf(stderr, "%s%ld", sep, b->pgno);
519 if (b->flags & MPOOL_DIRTY)
520 (void)fprintf(stderr, "d");
521 if (b->flags & MPOOL_PINNED)
522 (void)fprintf(stderr, "P");
530 (void)fprintf(stderr, "\n");
543 __mpoolerr(const char *fmt, ...)
545 __mpoolerr(fmt, va_alist)
556 (void)vfprintf(stderr, fmt, ap);
558 (void)fprintf(stderr, "\n");