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))
39 #define KEY_PAGE(key) PAGE_NUMBER(key)
40 #define KEY_FD(key) PAGE_OFFSET(key)
43 pgc_open(uns page_size, uns max_pages)
45 struct page_cache *c = xmalloc(sizeof(struct page_cache));
49 init_list(&c->free_pages);
50 init_list(&c->locked_pages);
51 init_list(&c->dirty_pages);
52 c->page_size = page_size;
53 c->max_pages = max_pages;
54 c->hash_size = nextprime(c->max_pages);
55 c->hash_table = xmalloc(sizeof(list) * c->hash_size);
56 for(i=0; i<c->hash_size; i++)
57 init_list(&c->hash_table[i]);
58 #ifndef SHERLOCK_HAVE_PREAD
65 pgc_close(struct page_cache *c)
68 ASSERT(EMPTY_LIST(c->locked_pages));
69 ASSERT(EMPTY_LIST(c->dirty_pages));
70 ASSERT(EMPTY_LIST(c->free_pages));
76 pgc_debug_page(struct page *p)
78 printf("\tk=%08x f=%x c=%d\n", (uns) p->key, p->flags, p->lock_count);
82 pgc_debug(struct page_cache *c, int mode)
86 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);
87 printf(">> stats: %d hits, %d misses, %d writes\n", c->stat_hit, c->stat_miss, c->stat_write);
91 WALK_LIST(p, c->free_pages)
94 WALK_LIST(p, c->locked_pages)
97 WALK_LIST(p, c->dirty_pages)
103 flush_page(struct page_cache *c, struct page *p)
105 int fd = KEY_FD(p->key);
106 sh_off_t pos = KEY_PAGE(p->key);
109 ASSERT(p->flags & PG_FLAG_DIRTY);
110 #ifdef SHERLOCK_HAVE_PREAD
111 s = pwrite(fd, p->data, c->page_size, pos);
113 if (c->pos != pos || c->pos_fd != fd)
114 sh_seek(fd, pos, SEEK_SET);
115 s = write(fd, p->data, c->page_size);
120 die("pgc_write(%d): %m", fd);
121 if (s != (int) c->page_size)
122 die("pgc_write(%d): incomplete page (only %d of %d)", s, c->page_size);
123 p->flags &= ~PG_FLAG_DIRTY;
128 hash_page(struct page_cache *c, sh_off_t key)
130 return key % c->hash_size;
134 get_page(struct page_cache *c, sh_off_t key)
138 uns hash = hash_page(c, key);
141 * Return locked buffer for given page.
144 WALK_LIST(n, c->hash_table[hash])
146 p = SKIP_BACK(struct page, hn, n);
149 /* Found in the cache */
156 if (c->total_count < c->max_pages || !c->free_count)
158 /* Enough free space, expand the cache */
159 p = xmalloc(sizeof(struct page) + c->page_size);
164 /* Discard the oldest unlocked page */
165 p = HEAD(c->free_pages);
168 /* There are only dirty pages here */
169 p = HEAD(c->dirty_pages);
173 ASSERT(!p->lock_count);
181 add_tail(&c->hash_table[hash], &p->hn);
186 pgc_flush(struct page_cache *c)
191 WALK_LIST_DELSAFE(p, n, c->dirty_pages)
195 add_tail(&c->free_pages, &p->n);
197 WALK_LIST(p, c->locked_pages)
198 if (p->flags & PG_FLAG_DIRTY)
205 pgc_cleanup(struct page_cache *c)
211 WALK_LIST_DELSAFE(p, n, c->free_pages)
213 ASSERT(!(p->flags & PG_FLAG_DIRTY) && !p->lock_count);
220 ASSERT(!c->free_count);
223 static inline struct page *
224 get_and_lock_page(struct page_cache *c, sh_off_t key)
226 struct page *p = get_page(c, key);
228 add_tail(&c->locked_pages, &p->n);
234 pgc_read(struct page_cache *c, int fd, sh_off_t pos)
240 ASSERT(!PAGE_OFFSET(pos));
241 ASSERT(!PAGE_NUMBER(fd));
243 p = get_and_lock_page(c, key);
244 if (p->flags & PG_FLAG_VALID)
249 #ifdef SHERLOCK_HAVE_PREAD
250 s = pread(fd, p->data, c->page_size, pos);
252 if (c->pos != pos || c->pos_fd != fd)
253 sh_seek(fd, pos, SEEK_SET);
254 s = read(fd, p->data, c->page_size);
259 die("pgc_read(%d): %m", fd);
260 if (s != (int) c->page_size)
261 die("pgc_read(%d): incomplete page (only %d of %d)", s, c->page_size);
262 p->flags |= PG_FLAG_VALID;
268 pgc_get(struct page_cache *c, int fd, sh_off_t pos)
273 ASSERT(!PAGE_OFFSET(pos));
274 ASSERT(!PAGE_NUMBER(fd));
276 p = get_and_lock_page(c, key);
277 p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
282 pgc_get_zero(struct page_cache *c, int fd, sh_off_t pos)
287 ASSERT(!PAGE_OFFSET(pos));
288 ASSERT(!PAGE_NUMBER(fd));
290 p = get_and_lock_page(c, key);
291 bzero(p->data, c->page_size);
292 p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
297 pgc_put(struct page_cache *c, struct page *p)
299 ASSERT(p->lock_count);
303 if (p->flags & PG_FLAG_DIRTY)
305 add_tail(&c->dirty_pages, &p->n);
308 else if (c->free_count < c->max_pages)
310 add_tail(&c->free_pages, &p->n);
322 pgc_mark_dirty(struct page_cache *c, struct page *p)
324 ASSERT(p->lock_count);
325 if (!(p->flags & PG_FLAG_DIRTY))
327 p->flags |= PG_FLAG_DIRTY;
329 add_head(&c->locked_pages, &p->n);
334 pgc_read_data(struct page_cache *c, int fd, sh_off_t pos, uns *len)
337 sh_off_t page = PAGE_NUMBER(pos);
338 uns offset = PAGE_OFFSET(pos);
340 p = pgc_read(c, fd, page);
342 *len = c->page_size - offset;
343 return p->data + offset;
347 pgc_page_pos(struct page_cache *c, struct page *p)
349 return PAGE_NUMBER(p->key);
354 int main(int argc, char **argv)
356 struct page_cache *c = pgc_open(1024, 2);
357 struct page *p, *q, *r;
358 int fd = open("test", O_RDWR | O_CREAT | O_TRUNC, 0666);
362 p = pgc_get(c, fd, 0);
364 strcpy(p->data, "one");
367 p = pgc_get(c, fd, 1024);
369 strcpy(p->data, "two");
372 p = pgc_get(c, fd, 2048);
374 strcpy(p->data, "three");
379 p = pgc_read(c, fd, 0);
381 strcpy(p->data, "odin");
382 pgc_mark_dirty(c, p);
386 q = pgc_read(c, fd, 1024);
388 r = pgc_read(c, fd, 2048);
394 p = pgc_get(c, fd, 3072);
396 strcpy(p->data, "four");