xref: /csrg-svn/lib/libc/db/btree/lrucache.c (revision 46561)
145875Sbostic /*-
245875Sbostic  * Copyright (c) 1990 The Regents of the University of California.
345875Sbostic  * All rights reserved.
445875Sbostic  *
545875Sbostic  * This code is derived from software contributed to Berkeley by
645875Sbostic  * Mike Olson.
745875Sbostic  *
845875Sbostic  * %sccs.include.redist.c%
945875Sbostic  */
1045875Sbostic 
1145875Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46561Sbostic char sccsid[] = "@(#)lrucache.c	5.3 (Berkeley) 02/22/91";
1345875Sbostic #endif /* LIBC_SCCS and not lint */
1445875Sbostic 
1545875Sbostic /*
1645875Sbostic  *  lrucache.c -- LRU cache for disk-based btree pages.
1745875Sbostic  *
1845875Sbostic  *	This file implements an LRU cache in user space for disk-based
1945875Sbostic  *	btrees.
2045875Sbostic  */
2145875Sbostic #include <sys/param.h>
22*46561Sbostic #include <stdlib.h>
23*46561Sbostic #include <string.h>
24*46561Sbostic #include <unistd.h>
2546127Smao #include "lrucache.h"
2645875Sbostic 
2745875Sbostic /*
2845875Sbostic  *  LRUINIT -- Initialize a new LRU cache.
2945875Sbostic  *
3045875Sbostic  *	There's a separate LRU cache for every open file descriptor on which
3145875Sbostic  *	the user wants caching.  The desired cache size and logical page
3245875Sbostic  *	size are passed in.  We try to avoid growing the cache beyond the
3345875Sbostic  *	limit specified by the user, but if we cannot satisfy a block request
3445875Sbostic  *	without growing the cache, we do so.
3545875Sbostic  *
3645875Sbostic  *	Note that the page size passed in is the logical page size for
3745875Sbostic  *	use with this file descriptor, and doesn't necessarily have anything
3845875Sbostic  *	to do with the underlying hardware page size.
3945875Sbostic  *
4045875Sbostic  *	Parameters:
4145875Sbostic  *		fd -- file descriptor for this cache
4245875Sbostic  *		cachesz -- number of buffers in cache (suggested)
4345875Sbostic  *		pagesz -- logical page size inside this file
4445875Sbostic  *		inproc -- routine to call when a buffer is read
4545875Sbostic  *		outproc -- routine to call when a buffer is written
4645875Sbostic  *
4745875Sbostic  *	Returns:
4845875Sbostic  *		Opaque pointer to the LRU cache on success, NULL on failure.
4945875Sbostic  *
5045875Sbostic  *	Side Effects:
5145875Sbostic  *		Allocates memory for the hash table and LRU cache.  Buffers
5245875Sbostic  *		are allocated on demand, later.
5345875Sbostic  */
5445875Sbostic LRU
lruinit(fd,cachesz,pagesz,opaque,inproc,outproc)5545875Sbostic lruinit(fd, cachesz, pagesz, opaque, inproc, outproc)
5645875Sbostic 	int fd;
5745875Sbostic 	int cachesz;
5845875Sbostic 	int pagesz;
5945875Sbostic 	char *opaque;
6045875Sbostic 	int (*inproc)();
6145875Sbostic 	int (*outproc)();
6245875Sbostic {
6345875Sbostic 	register LRUCACHE *l;
6445875Sbostic 	int nbytes;
6545875Sbostic 
6645875Sbostic 	/* allocate the LRU cache struct */
6745875Sbostic 	if ((l = (LRUCACHE *) malloc((unsigned) sizeof(LRUCACHE)))
6845875Sbostic 	    == (LRUCACHE *) NULL)
6945875Sbostic 		return ((LRU) NULL);
7045875Sbostic 
7145875Sbostic 	/* allocate the hash table */
7245875Sbostic 	nbytes = cachesz * sizeof(CACHE_ENT *);
7345875Sbostic 	if ((l->lru_cache = (CACHE_ENT **) malloc((unsigned) nbytes))
7445875Sbostic 	    == (CACHE_ENT **) NULL) {
7546127Smao 		(void) free((char *) l);
7645875Sbostic 		return ((LRU) NULL);
7745875Sbostic 	}
7845875Sbostic 
7945875Sbostic 	/* init memory */
8046127Smao 	bzero((char *) (l->lru_cache), (size_t) nbytes);
8145875Sbostic 	l->lru_fd = fd;
8245875Sbostic 	l->lru_psize = pagesz;
8345875Sbostic 	l->lru_csize = cachesz;
8445875Sbostic 	l->lru_cursz = 0;
8545875Sbostic 	l->lru_opaque = opaque;
8645875Sbostic 	l->lru_head = l->lru_tail = (LRU_ENT *) NULL;
8745875Sbostic 	l->lru_inproc = inproc;
8845875Sbostic 	l->lru_outproc = outproc;
8945875Sbostic 
9045875Sbostic 	return ((LRU) l);
9145875Sbostic }
9245875Sbostic 
9345875Sbostic /*
9445875Sbostic  *  LRUGET -- Get a buffer from an LRU cache.
9545875Sbostic  *
9645875Sbostic  *	If the buffer is not in the cache at present, this routine will
9745875Sbostic  *	instantiate it from the file.  This REQUIRES that the desired
9845875Sbostic  *	block actually be on disk; we don't do non-blocking reads, so
9945875Sbostic  *	if it's not actually out there, this routine won't return for
10045875Sbostic  *	a very long time.  In order to instantiate a new buffer, use
10145875Sbostic  *	lrugetnew().
10245875Sbostic  *
10345875Sbostic  *	Parameters:
10445875Sbostic  *		lru -- the LRU cache to use.
10545875Sbostic  *		pgno -- the logical block number to get.
10645875Sbostic  *		nread -- pointer to an int, in which the number of bytes
10745875Sbostic  *			 read is returned.
10845875Sbostic  *
10945875Sbostic  *	Returns:
11045875Sbostic  *		(char *) pointer to the buffer for the desired block.  The
11145875Sbostic  *		number of bytes actually read is returned in *nread.
11245875Sbostic  *
11345875Sbostic  *	Warnings:
11445875Sbostic  *		The requested buffer is locked down until the user does
11545875Sbostic  *		an explicit lrurelease() on it.
11645875Sbostic  */
11745875Sbostic 
11845875Sbostic char *
lruget(lru,pgno,nread)11945875Sbostic lruget(lru, pgno, nread)
12045875Sbostic 	LRU lru;
12145875Sbostic 	int pgno;
12245875Sbostic 	int *nread;
12345875Sbostic {
12445875Sbostic 	LRUCACHE *l = (LRUCACHE *) lru;
12545875Sbostic 	CACHE_ENT *ce;
12645875Sbostic 	LRU_ENT *lruent;
12745875Sbostic 	char *buffer;
12845875Sbostic 	long pos;
12945875Sbostic 
13045875Sbostic 	/* is it already in the cache? */
13145875Sbostic 	if ((ce = lruhashget(l, pgno)) != (CACHE_ENT *) NULL) {
13245875Sbostic 
13345875Sbostic 		/* yes, move it to the head and return it */
13445875Sbostic 		lruent = ce->c_lruent;
13545875Sbostic 		lruent->l_flags &= ~F_FREE;
13645875Sbostic 		lruhead(l, ce->c_lruent);
13745875Sbostic 		*nread = l->lru_psize;
13845875Sbostic 		return (ce->c_lruent->l_buffer);
13945875Sbostic 	}
14045875Sbostic 
14145875Sbostic 	/* not there, get a free page */
14245875Sbostic 	if ((buffer = lrugetpg(l, pgno, nread, lruget)) == (char *) NULL)
14345875Sbostic 		return ((char *) NULL);
14445875Sbostic 
14545875Sbostic 	/* okay, got a buffer -- fill it */
14645875Sbostic 	pos = (long) (l->lru_psize * pgno);
14745875Sbostic 	if (lseek(l->lru_fd, pos, L_SET) != pos)
14845875Sbostic 		return ((char *) NULL);
14945875Sbostic 
15045875Sbostic 	*nread = read(l->lru_fd, buffer, l->lru_psize);
15145875Sbostic 
15245875Sbostic 	if (l->lru_inproc)
15345875Sbostic 		(*(l->lru_inproc))(buffer, l->lru_opaque);
15445875Sbostic 
15545875Sbostic 	return (buffer);
15645875Sbostic }
15745875Sbostic 
15845875Sbostic /*
15945875Sbostic  *  LRUGETNEW -- Get a page for a new block in an LRU cache.
16045875Sbostic  *
16145875Sbostic  *	This routine allows users to instantiate pages for a file if they
16245875Sbostic  *	don't exist on disk yet.  It's used to make a file bigger.
16345875Sbostic  *
16445875Sbostic  *	Parameters:
16545875Sbostic  *		lru -- the LRU cache to use
16645875Sbostic  *		pgno -- the (new) logical page number to instantiate
16745875Sbostic  *		nread -- ptr to int to get number of bytes read (this is
16845875Sbostic  *			 guaranteed to be zero, since the page isn't on disk)
16945875Sbostic  *
17045875Sbostic  *	Returns:
17145875Sbostic  *		Pointer to a buffer for the associated page, or NULL on
17245875Sbostic  *		failure.
17345875Sbostic  *
17445875Sbostic  *	Warnings:
17545875Sbostic  *		The associated buffer is locked down until the user
17645875Sbostic  *		explicitly does an lrurelease() on it.
17745875Sbostic  */
17845875Sbostic 
17945875Sbostic char *
lrugetnew(lru,pgno,nread)18045875Sbostic lrugetnew(lru, pgno, nread)
18145875Sbostic 	LRU lru;
18245875Sbostic 	int pgno;
18345875Sbostic 	int *nread;
18445875Sbostic {
18545875Sbostic 	LRUCACHE *l = (LRUCACHE *) lru;
18645875Sbostic 
18745875Sbostic 	*nread = 0;
18845875Sbostic 	return (lrugetpg(l, pgno, nread, lrugetnew));
18945875Sbostic }
19045875Sbostic 
19145875Sbostic /*
19245875Sbostic  *  LRUFLUSH -- flush an LRU page to disk.
19345875Sbostic  *
19445875Sbostic  *	This routine forces a page to disk.  Users should use lruwrite(),
19545875Sbostic  *	which simply marks a page dirty and does late writing.
19645875Sbostic  *
19745875Sbostic  *	Parameters:
19845875Sbostic  *		l -- LRU cache
19945875Sbostic  *		lruent -- the LRU cache entry whose page we should flush
20045875Sbostic  *
20145875Sbostic  *	Returns:
20245875Sbostic  *		Zero on success, -1 on failure.
20345875Sbostic  */
20445875Sbostic 
lruflush(l,lruent)20545875Sbostic lruflush(l, lruent)
20645875Sbostic 	LRUCACHE *l;
20745875Sbostic 	LRU_ENT *lruent;
20845875Sbostic {
20945875Sbostic 	long pos;
21045875Sbostic 
21145875Sbostic 	if (l->lru_outproc)
21245875Sbostic 		(*(l->lru_outproc))(lruent->l_buffer, l->lru_opaque);
21345875Sbostic 
21445875Sbostic 	pos = (long) (l->lru_psize * lruent->l_pgno);
21545875Sbostic 	if (lseek(l->lru_fd, pos, L_SET) != pos)
21645875Sbostic 		return (-1);
21745875Sbostic 	if (write(l->lru_fd, lruent->l_buffer, l->lru_psize) != l->lru_psize)
21845875Sbostic 		return (-1);
21945875Sbostic 
22045875Sbostic 	if (l->lru_inproc)
22145875Sbostic 		(*(l->lru_inproc))(lruent->l_buffer, l->lru_opaque);
22245875Sbostic 
22345875Sbostic 	lruent->l_flags &= ~F_DIRTY;
22445875Sbostic 	return (0);
22545875Sbostic }
22645875Sbostic 
22745875Sbostic /*
22845875Sbostic  *  LRUWRITE -- Mark an LRU cache buffer dirty
22945875Sbostic  *
23045875Sbostic  *	This routine is how users should move their changes to disk.  The
23145875Sbostic  *	cache code marks the associated buffer dirty, and flushes it to
23245875Sbostic  *	disk if we need to reuse the buffer for some other page.
23345875Sbostic  *
23445875Sbostic  *	Parameters:
23545875Sbostic  *		lru -- LRU cache
23645875Sbostic  *		pgno -- page number to flush
23745875Sbostic  *
23845875Sbostic  *	Returns:
23945875Sbostic  *		Zero on success, -1 on failure.
24045875Sbostic  */
24145875Sbostic 
24245875Sbostic int
lruwrite(lru,pgno)24345875Sbostic lruwrite(lru, pgno)
24445875Sbostic 	LRU lru;
24545875Sbostic 	int pgno;
24645875Sbostic {
24745875Sbostic 	LRUCACHE *l = (LRUCACHE *) lru;
24845875Sbostic 	CACHE_ENT *ce;
24945875Sbostic 
25045875Sbostic 	if ((ce = lruhashget(l, pgno)) == (CACHE_ENT *) NULL)
25145875Sbostic 		return (-1);
25245875Sbostic 
25345875Sbostic 	/* mark the entry dirty */
25445875Sbostic 	ce->c_lruent->l_flags |= F_DIRTY;
25545875Sbostic 
25645875Sbostic 	return (0);
25745875Sbostic }
25845875Sbostic 
25945875Sbostic /*
26045875Sbostic  *  LRUSYNC -- Flush all changes to disk
26145875Sbostic  *
26245875Sbostic  *	This routine allows users to force all changes to buffers currently
26345875Sbostic  *	in memory to disk.  This is expensive.
26445875Sbostic  *
26545875Sbostic  *	Parameters:
26645875Sbostic  *		lru -- LRU cache to flush
26745875Sbostic  *
26845875Sbostic  *	Returns:
26945875Sbostic  *		Zero on success, -1 on failure
27045875Sbostic  *
27145875Sbostic  *	Side Effects:
27245875Sbostic  *		After this call, all buffers are clean.
27345875Sbostic  */
27445875Sbostic 
27545875Sbostic int
lrusync(lru)27645875Sbostic lrusync(lru)
27745875Sbostic 	LRU lru;
27845875Sbostic {
27945875Sbostic 	LRUCACHE *l = (LRUCACHE *) lru;
28045875Sbostic 	LRU_ENT *le;
28145875Sbostic 
28245875Sbostic 	for (le = l->lru_head; le != (LRU_ENT *) NULL; le = le->l_next)
28345875Sbostic 		if (le->l_flags & F_DIRTY)
28445875Sbostic 			if (lruflush(l, le) < 0)
28545875Sbostic 				return (-1);
28645875Sbostic 	return (0);
28745875Sbostic }
28845875Sbostic 
28945875Sbostic /*
29045875Sbostic  *  LRURELEASE -- Release a buffer in the LRU cache for reuse
29145875Sbostic  *
29245875Sbostic  *	When the user does an lruget() or lrugetnew(), the buffer we return
29345875Sbostic  *	is locked down, to guarantee that it's not reused while the user
29445875Sbostic  *	still needs it.  Once a buffer is no longer needed, it should be
29545875Sbostic  *	released (via this routine) so that we can use it for other pages
29645875Sbostic  *	if necessary.
29745875Sbostic  *
29845875Sbostic  *	Parameters:
29945875Sbostic  *		lru -- LRU cache
30045875Sbostic  *		pgno -- page number of buffer to release
30145875Sbostic  *
30245875Sbostic  *	Returns:
30345875Sbostic  *		Zero on success, -1 on failure
30445875Sbostic  */
30545875Sbostic 
30645875Sbostic int
lrurelease(lru,pgno)30745875Sbostic lrurelease(lru, pgno)
30845875Sbostic 	LRU lru;
30945875Sbostic 	int pgno;
31045875Sbostic {
31145875Sbostic 	LRUCACHE *l = (LRUCACHE *) lru;
31245875Sbostic 	CACHE_ENT *ce;
31345875Sbostic 
31445875Sbostic 	if ((ce = lruhashget(l, pgno)) == (CACHE_ENT *) NULL)
31545875Sbostic 		return (-1);
31645875Sbostic 	ce->c_lruent->l_flags |= F_FREE;
31745875Sbostic 	return (0);
31845875Sbostic }
31945875Sbostic 
32045875Sbostic /*
32145875Sbostic  *  LRUFREE -- Free an entire LRU cache
32245875Sbostic  *
32345875Sbostic  *	This routine releases an LRU cache.  The cache should not be
32445875Sbostic  *	used again.
32545875Sbostic  *
32645875Sbostic  *	Parameters:
32745875Sbostic  *		lru -- LRU cache to free
32845875Sbostic  *
32945875Sbostic  *	Returns:
33045875Sbostic  *		None.
33145875Sbostic  *
33245875Sbostic  *	Side Effects:
33345875Sbostic  *		Frees a lot of memory.
33445875Sbostic  */
33545875Sbostic 
33645875Sbostic void
lrufree(lru)33745875Sbostic lrufree(lru)
33845875Sbostic 	LRU lru;
33945875Sbostic {
34045875Sbostic 	LRUCACHE *l = (LRUCACHE *) lru;
34145875Sbostic 	LRU_ENT *le;
34245875Sbostic 	LRU_ENT *sle;
34345875Sbostic 
34445875Sbostic 	for (le = l->lru_head; le != (LRU_ENT *) NULL; ) {
34546127Smao 		free((char *) (le->l_buffer));
34645875Sbostic 		sle = le;
34745875Sbostic 		le = le->l_next;
34846127Smao 		free((char *) sle);
34945875Sbostic 	}
35046127Smao 	free ((char *) l);
35145875Sbostic }
352