2 * Sherlock Library -- File Page Cache
4 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
8 #include "lib/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 = sh_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)", p->fd, 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 p = get_and_lock_page(c, pos, fd);
277 if (p->flags & PG_FLAG_VALID)
282 #ifdef SHERLOCK_HAVE_PREAD
283 s = sh_pread(fd, p->data, c->page_size, pos);
285 if (c->pos != pos || c->pos_fd != fd)
286 sh_seek(fd, pos, SEEK_SET);
287 s = read(fd, p->data, c->page_size);
292 die("pgc_read(%d): %m", fd);
293 if (s != (int) c->page_size)
294 die("pgc_read(%d): incomplete page (only %d of %d)", p->fd, s, c->page_size);
295 p->flags |= PG_FLAG_VALID;
301 pgc_get(struct page_cache *c, int fd, sh_off_t pos)
305 ASSERT(!PAGE_OFFSET(pos));
306 p = get_and_lock_page(c, pos, fd);
307 p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
312 pgc_get_zero(struct page_cache *c, int fd, sh_off_t pos)
316 ASSERT(!PAGE_OFFSET(pos));
317 p = get_and_lock_page(c, pos, fd);
318 bzero(p->data, c->page_size);
319 p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
324 pgc_put(struct page_cache *c, struct page *p)
326 ASSERT(p->lock_count);
330 if (p->flags & PG_FLAG_DIRTY)
332 add_tail(&c->dirty_pages, &p->n);
335 else if (c->free_count < c->max_pages)
337 add_tail(&c->free_pages, &p->n);
349 pgc_mark_dirty(struct page_cache *c, struct page *p)
351 ASSERT(p->lock_count);
352 if (!(p->flags & PG_FLAG_DIRTY))
354 p->flags |= PG_FLAG_DIRTY;
356 add_head(&c->locked_pages, &p->n);
361 pgc_read_data(struct page_cache *c, int fd, sh_off_t pos, uns *len)
364 sh_off_t page = PAGE_NUMBER(pos);
365 uns offset = PAGE_OFFSET(pos);
367 p = pgc_read(c, fd, page);
369 *len = c->page_size - offset;
370 return p->data + offset;
375 int main(int argc, char **argv)
377 struct page_cache *c = pgc_open(1024, 2);
378 struct page *p, *q, *r;
379 int fd = open("test", O_RDWR | O_CREAT | O_TRUNC, 0666);
383 p = pgc_get(c, fd, 0);
385 strcpy(p->data, "one");
388 p = pgc_get(c, fd, 1024);
390 strcpy(p->data, "two");
393 p = pgc_get(c, fd, 2048);
395 strcpy(p->data, "three");
400 p = pgc_read(c, fd, 0);
402 strcpy(p->data, "odin");
403 pgc_mark_dirty(c, p);
407 q = pgc_read(c, fd, 1024);
409 r = pgc_read(c, fd, 2048);
415 p = pgc_get(c, fd, 3072);
417 strcpy(p->data, "four");