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 */
35 #define PAGE_NUMBER(pos) ((pos) & ~(sh_off_t)(c->page_size - 1))
36 #define PAGE_OFFSET(pos) ((pos) & (c->page_size - 1))
38 #define KEY_PAGE(key) PAGE_NUMBER(key)
39 #define KEY_FD(key) PAGE_OFFSET(key)
42 pgc_open(uns page_size, uns max_pages)
44 struct page_cache *c = xmalloc(sizeof(struct page_cache));
48 init_list(&c->free_pages);
49 init_list(&c->locked_pages);
50 init_list(&c->dirty_pages);
51 c->page_size = page_size;
52 c->max_pages = max_pages;
53 c->hash_size = nextprime(c->max_pages);
54 c->hash_table = xmalloc(sizeof(list) * c->hash_size);
55 for(i=0; i<c->hash_size; i++)
56 init_list(&c->hash_table[i]);
61 pgc_close(struct page_cache *c)
64 ASSERT(EMPTY_LIST(c->locked_pages));
65 ASSERT(EMPTY_LIST(c->dirty_pages));
66 ASSERT(EMPTY_LIST(c->free_pages));
72 pgc_debug_page(struct page *p)
74 printf("\tk=%08x f=%x c=%d\n", (uns) p->key, p->flags, p->lock_count);
78 pgc_debug(struct page_cache *c, int mode)
82 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);
83 printf(">> stats: %d hits, %d misses, %d writes\n", c->stat_hit, c->stat_miss, c->stat_write);
87 WALK_LIST(p, c->free_pages)
90 WALK_LIST(p, c->locked_pages)
93 WALK_LIST(p, c->dirty_pages)
99 flush_page(struct page_cache *c, struct page *p)
101 int fd = KEY_FD(p->key);
102 sh_off_t pos = KEY_PAGE(p->key);
105 ASSERT(p->flags & PG_FLAG_DIRTY);
106 #ifdef SHERLOCK_HAVE_PREAD
107 s = pwrite(fd, p->data, c->page_size, pos);
110 sh_seek(fd, pos, SEEK_SET);
111 s = write(fd, p->data, c->page_size);
115 die("pgc_write(%d): %m", 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 hash_page(struct page_cache *c, sh_off_t key)
125 return key % c->hash_size;
129 get_page(struct page_cache *c, sh_off_t key)
133 uns hash = hash_page(c, key);
136 * Return locked buffer for given page.
139 WALK_LIST(n, c->hash_table[hash])
141 p = SKIP_BACK(struct page, hn, n);
144 /* Found in the cache */
151 if (c->total_count < c->max_pages || !c->free_count)
153 /* Enough free space, expand the cache */
154 p = xmalloc(sizeof(struct page) + c->page_size);
159 /* Discard the oldest unlocked page */
160 p = HEAD(c->free_pages);
163 /* There are only dirty pages here */
164 p = HEAD(c->dirty_pages);
168 ASSERT(!p->lock_count);
176 add_tail(&c->hash_table[hash], &p->hn);
181 pgc_flush(struct page_cache *c)
186 WALK_LIST_DELSAFE(p, n, c->dirty_pages)
190 add_tail(&c->free_pages, &p->n);
192 WALK_LIST(p, c->locked_pages)
193 if (p->flags & PG_FLAG_DIRTY)
200 pgc_cleanup(struct page_cache *c)
206 WALK_LIST_DELSAFE(p, n, c->free_pages)
208 ASSERT(!(p->flags & PG_FLAG_DIRTY) && !p->lock_count);
215 ASSERT(!c->free_count);
218 static inline struct page *
219 get_and_lock_page(struct page_cache *c, sh_off_t key)
221 struct page *p = get_page(c, key);
223 add_tail(&c->locked_pages, &p->n);
229 pgc_read(struct page_cache *c, int fd, sh_off_t pos)
235 ASSERT(!PAGE_OFFSET(pos));
236 ASSERT(!PAGE_NUMBER(fd));
238 p = get_and_lock_page(c, key);
239 if (p->flags & PG_FLAG_VALID)
244 #ifdef SHERLOCK_HAVE_PREAD
245 s = pread(fd, p->data, c->page_size, pos);
248 sh_seek(fd, pos, SEEK_SET);
249 s = read(fd, p->data, c->page_size);
253 die("pgc_read(%d): %m", fd);
254 if (s != (int) c->page_size)
255 die("pgc_read(%d): incomplete page (only %d of %d)", s, c->page_size);
256 p->flags |= PG_FLAG_VALID;
262 pgc_get(struct page_cache *c, int fd, sh_off_t pos)
267 ASSERT(!PAGE_OFFSET(pos));
268 ASSERT(!PAGE_NUMBER(fd));
270 p = get_and_lock_page(c, key);
271 p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
276 pgc_put(struct page_cache *c, struct page *p)
278 ASSERT(p->lock_count);
282 if (p->flags & PG_FLAG_DIRTY)
284 add_tail(&c->dirty_pages, &p->n);
287 else if (c->free_count < c->max_pages)
289 add_tail(&c->free_pages, &p->n);
301 pgc_mark_dirty(struct page_cache *c, struct page *p)
303 ASSERT(p->lock_count);
304 if (!(p->flags & PG_FLAG_DIRTY))
306 p->flags |= PG_FLAG_DIRTY;
308 add_head(&c->locked_pages, &p->n);
313 pgc_read_data(struct page_cache *c, int fd, sh_off_t pos, uns *len)
316 sh_off_t page = PAGE_NUMBER(pos);
317 uns offset = PAGE_OFFSET(pos);
319 p = pgc_read(c, fd, page);
321 *len = c->page_size - offset;
322 return p->data + offset;
326 pgc_page_pos(struct page_cache *c, struct page *p)
328 return PAGE_NUMBER(p->key);
333 int main(int argc, char **argv)
335 struct page_cache *c = pgc_open(1024, 2);
336 struct page *p, *q, *r;
337 int fd = open("test", O_RDWR | O_CREAT | O_TRUNC, 0666);
341 p = pgc_get(c, fd, 0);
343 strcpy(p->data, "one");
346 p = pgc_get(c, fd, 1024);
348 strcpy(p->data, "two");
351 p = pgc_get(c, fd, 2048);
353 strcpy(p->data, "three");
358 p = pgc_read(c, fd, 0);
360 strcpy(p->data, "odin");
361 pgc_mark_dirty(c, p);
365 q = pgc_read(c, fd, 1024);
367 r = pgc_read(c, fd, 2048);
373 p = pgc_get(c, fd, 3072);
375 strcpy(p->data, "four");