2 * Sherlock Library -- File Page Cache
4 * (c) 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
14 #include "pagecache.h"
18 list free_pages; /* LRU queue of free non-dirty pages */
19 list locked_pages; /* List of locked pages (starts with dirty ones) */
20 list dirty_pages; /* List of free dirty pages */
21 uns page_size; /* Bytes per page (must be a power of two) */
22 uns free_count; /* Number of free / dirty pages */
23 uns total_count; /* Total number of pages */
24 uns max_pages; /* Maximum number of free pages */
25 uns hash_size; /* Hash table size */
26 uns stat_hit; /* Number of cache hits */
27 uns stat_miss; /* Number of cache misses */
28 uns stat_write; /* Number of writes */
29 list *hash_table; /* List heads corresponding to hash buckets */
30 #ifndef SHERLOCK_HAVE_PREAD
31 sh_off_t pos; /* Current position in the file */
32 int pos_fd; /* FD the position corresponds to */
36 #define PAGE_NUMBER(pos) ((pos) & ~(sh_off_t)(c->page_size - 1))
37 #define PAGE_OFFSET(pos) ((pos) & (c->page_size - 1))
40 pgc_open(uns page_size, uns max_pages)
42 struct page_cache *c = xmalloc(sizeof(struct page_cache));
46 init_list(&c->free_pages);
47 init_list(&c->locked_pages);
48 init_list(&c->dirty_pages);
49 c->page_size = page_size;
50 c->max_pages = max_pages;
51 c->hash_size = nextprime(c->max_pages);
52 c->hash_table = xmalloc(sizeof(list) * c->hash_size);
53 for(i=0; i<c->hash_size; i++)
54 init_list(&c->hash_table[i]);
55 #ifndef SHERLOCK_HAVE_PREAD
62 pgc_close(struct page_cache *c)
65 ASSERT(EMPTY_LIST(c->locked_pages));
66 ASSERT(EMPTY_LIST(c->dirty_pages));
67 ASSERT(EMPTY_LIST(c->free_pages));
73 pgc_debug_page(struct page *p)
75 printf("\tp=%08x d=%d f=%x c=%d\n", (uns) p->pos, p->fd, p->flags, p->lock_count);
79 pgc_debug(struct page_cache *c, int mode)
83 printf(">> Page cache dump: pgsize=%d, pages=%d, freepages=%d of %d, hash=%d\n", c->page_size, c->total_count, c->free_count, c->max_pages, c->hash_size);
84 printf(">> stats: %d hits, %d misses, %d writes\n", c->stat_hit, c->stat_miss, c->stat_write);
88 WALK_LIST(p, c->free_pages)
91 WALK_LIST(p, c->locked_pages)
94 WALK_LIST(p, c->dirty_pages)
100 flush_page(struct page_cache *c, struct page *p)
104 ASSERT(p->flags & PG_FLAG_DIRTY);
105 #ifdef SHERLOCK_HAVE_PREAD
106 s = pwrite(p->fd, p->data, c->page_size, p->pos);
108 if (c->pos != p->pos || c->pos_fd != p->fd)
109 sh_seek(p->fd, p->pos, SEEK_SET);
110 s = write(p->fd, p->data, c->page_size);
115 die("pgc_write(%d): %m", p->fd);
116 if (s != (int) c->page_size)
117 die("pgc_write(%d): incomplete page (only %d of %d)", s, c->page_size);
118 p->flags &= ~PG_FLAG_DIRTY;
123 flush_cmp(const void *X, const void *Y)
125 struct page *x = *((struct page **)X);
126 struct page *y = *((struct page **)Y);
140 flush_pages(struct page_cache *c, uns force)
143 uns max = force ? ~0 : c->free_count / 2; /* FIXME: Needs tuning */
145 struct page *p, *q, **req, **rr;
147 WALK_LIST(p, c->dirty_pages)
153 req = rr = alloca(cnt * sizeof(struct page *));
155 p = HEAD(c->dirty_pages);
156 while ((q = (struct page *) p->n.next) && i--)
159 add_tail(&c->free_pages, &p->n);
163 qsort(req, cnt, sizeof(struct page *), flush_cmp);
165 flush_page(c, req[i]);
169 hash_page(struct page_cache *c, sh_off_t pos, uns fd)
171 return (pos + fd) % c->hash_size;
175 get_page(struct page_cache *c, sh_off_t pos, uns fd)
179 uns hash = hash_page(c, pos, fd);
182 * Return locked buffer for given page.
185 WALK_LIST(n, c->hash_table[hash])
187 p = SKIP_BACK(struct page, hn, n);
188 if (p->pos == pos && p->fd == fd)
190 /* Found in the cache */
197 if (c->total_count < c->max_pages || !c->free_count)
199 /* Enough free space, expand the cache */
200 p = xmalloc(sizeof(struct page) + c->page_size);
205 /* Discard the oldest unlocked page */
206 p = HEAD(c->free_pages);
209 /* There are only dirty pages here */
211 p = HEAD(c->free_pages);
214 ASSERT(!p->lock_count);
223 add_tail(&c->hash_table[hash], &p->hn);
228 pgc_flush(struct page_cache *c)
233 WALK_LIST(p, c->locked_pages)
234 if (p->flags & PG_FLAG_DIRTY)
241 pgc_cleanup(struct page_cache *c)
247 WALK_LIST_DELSAFE(p, n, c->free_pages)
249 ASSERT(!(p->flags & PG_FLAG_DIRTY) && !p->lock_count);
256 ASSERT(!c->free_count);
259 static inline struct page *
260 get_and_lock_page(struct page_cache *c, sh_off_t pos, uns fd)
262 struct page *p = get_page(c, pos, fd);
264 add_tail(&c->locked_pages, &p->n);
270 pgc_read(struct page_cache *c, int fd, sh_off_t pos)
275 ASSERT(!PAGE_OFFSET(pos));
276 ASSERT(!PAGE_NUMBER(fd));
277 p = get_and_lock_page(c, pos, fd);
278 if (p->flags & PG_FLAG_VALID)
283 #ifdef SHERLOCK_HAVE_PREAD
284 s = pread(fd, p->data, c->page_size, pos);
286 if (c->pos != pos || c->pos_fd != fd)
287 sh_seek(fd, pos, SEEK_SET);
288 s = read(fd, p->data, c->page_size);
293 die("pgc_read(%d): %m", fd);
294 if (s != (int) c->page_size)
295 die("pgc_read(%d): incomplete page (only %d of %d)", s, c->page_size);
296 p->flags |= PG_FLAG_VALID;
302 pgc_get(struct page_cache *c, int fd, sh_off_t pos)
306 ASSERT(!PAGE_OFFSET(pos));
307 ASSERT(!PAGE_NUMBER(fd));
308 p = get_and_lock_page(c, pos, fd);
309 p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
314 pgc_get_zero(struct page_cache *c, int fd, sh_off_t pos)
318 ASSERT(!PAGE_OFFSET(pos));
319 ASSERT(!PAGE_NUMBER(fd));
320 p = get_and_lock_page(c, pos, fd);
321 bzero(p->data, c->page_size);
322 p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
327 pgc_put(struct page_cache *c, struct page *p)
329 ASSERT(p->lock_count);
333 if (p->flags & PG_FLAG_DIRTY)
335 add_tail(&c->dirty_pages, &p->n);
338 else if (c->free_count < c->max_pages)
340 add_tail(&c->free_pages, &p->n);
352 pgc_mark_dirty(struct page_cache *c, struct page *p)
354 ASSERT(p->lock_count);
355 if (!(p->flags & PG_FLAG_DIRTY))
357 p->flags |= PG_FLAG_DIRTY;
359 add_head(&c->locked_pages, &p->n);
364 pgc_read_data(struct page_cache *c, int fd, sh_off_t pos, uns *len)
367 sh_off_t page = PAGE_NUMBER(pos);
368 uns offset = PAGE_OFFSET(pos);
370 p = pgc_read(c, fd, page);
372 *len = c->page_size - offset;
373 return p->data + offset;
378 int main(int argc, char **argv)
380 struct page_cache *c = pgc_open(1024, 2);
381 struct page *p, *q, *r;
382 int fd = open("test", O_RDWR | O_CREAT | O_TRUNC, 0666);
386 p = pgc_get(c, fd, 0);
388 strcpy(p->data, "one");
391 p = pgc_get(c, fd, 1024);
393 strcpy(p->data, "two");
396 p = pgc_get(c, fd, 2048);
398 strcpy(p->data, "three");
403 p = pgc_read(c, fd, 0);
405 strcpy(p->data, "odin");
406 pgc_mark_dirty(c, p);
410 q = pgc_read(c, fd, 1024);
412 r = pgc_read(c, fd, 2048);
418 p = pgc_get(c, fd, 3072);
420 strcpy(p->data, "four");