2 * UCW Library -- File Page Cache
4 * (c) 1999--2002 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/pagecache.h"
22 list free_pages; /* LRU queue of free non-dirty pages */
23 list locked_pages; /* List of locked pages (starts with dirty ones) */
24 list dirty_pages; /* List of free dirty pages */
25 uns page_size; /* Bytes per page (must be a power of two) */
26 uns free_count; /* Number of free / dirty pages */
27 uns total_count; /* Total number of pages */
28 uns max_pages; /* Maximum number of free pages */
29 uns hash_size; /* Hash table size */
30 uns stat_hit; /* Number of cache hits */
31 uns stat_miss; /* Number of cache misses */
32 uns stat_write; /* Number of writes */
33 list *hash_table; /* List heads corresponding to hash buckets */
35 sh_off_t pos; /* Current position in the file */
36 int pos_fd; /* FD the position corresponds to */
40 #define PAGE_NUMBER(pos) ((pos) & ~(sh_off_t)(c->page_size - 1))
41 #define PAGE_OFFSET(pos) ((pos) & (c->page_size - 1))
44 pgc_open(uns page_size, uns max_pages)
46 struct page_cache *c = xmalloc_zero(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]);
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("\tp=%08x d=%d f=%x c=%d\n", (uns) p->pos, p->fd, 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)
107 ASSERT(p->flags & PG_FLAG_DIRTY);
109 s = sh_pwrite(p->fd, p->data, c->page_size, p->pos);
111 if (c->pos != p->pos || c->pos_fd != (int) p->fd)
112 sh_seek(p->fd, p->pos, SEEK_SET);
113 s = write(p->fd, p->data, c->page_size);
118 die("pgc_write(%d): %m", p->fd);
119 if (s != (int) c->page_size)
120 die("pgc_write(%d): incomplete page (only %d of %d)", p->fd, s, c->page_size);
121 p->flags &= ~PG_FLAG_DIRTY;
126 flush_cmp(const void *X, const void *Y)
128 struct page *x = *((struct page **)X);
129 struct page *y = *((struct page **)Y);
143 flush_pages(struct page_cache *c, uns force)
146 uns max = force ? ~0U : c->free_count / 2;
148 struct page *p, *q, **req, **rr;
150 WALK_LIST(p, c->dirty_pages)
156 req = rr = alloca(cnt * sizeof(struct page *));
158 p = HEAD(c->dirty_pages);
159 while ((q = (struct page *) p->n.next) && i--)
162 add_tail(&c->free_pages, &p->n);
166 qsort(req, cnt, sizeof(struct page *), flush_cmp);
168 flush_page(c, req[i]);
172 hash_page(struct page_cache *c, sh_off_t pos, uns fd)
174 return (pos + fd) % c->hash_size;
178 get_page(struct page_cache *c, sh_off_t pos, uns fd)
182 uns hash = hash_page(c, pos, fd);
185 * Return locked buffer for given page.
188 WALK_LIST(n, c->hash_table[hash])
190 p = SKIP_BACK(struct page, hn, n);
191 if (p->pos == pos && p->fd == fd)
193 /* Found in the cache */
200 if (c->total_count < c->max_pages || !c->free_count)
202 /* Enough free space, expand the cache */
203 p = xmalloc(sizeof(struct page) + c->page_size);
208 /* Discard the oldest unlocked page */
209 p = HEAD(c->free_pages);
212 /* There are only dirty pages here */
214 p = HEAD(c->free_pages);
217 ASSERT(!p->lock_count);
226 add_tail(&c->hash_table[hash], &p->hn);
231 pgc_flush(struct page_cache *c)
236 WALK_LIST(p, c->locked_pages)
237 if (p->flags & PG_FLAG_DIRTY)
244 pgc_cleanup(struct page_cache *c)
250 WALK_LIST_DELSAFE(p, n, c->free_pages)
252 ASSERT(!(p->flags & PG_FLAG_DIRTY) && !p->lock_count);
259 ASSERT(!c->free_count);
262 static inline struct page *
263 get_and_lock_page(struct page_cache *c, sh_off_t pos, uns fd)
265 struct page *p = get_page(c, pos, fd);
267 add_tail(&c->locked_pages, &p->n);
273 pgc_read(struct page_cache *c, int fd, sh_off_t pos)
278 ASSERT(!PAGE_OFFSET(pos));
279 p = get_and_lock_page(c, pos, fd);
280 if (p->flags & PG_FLAG_VALID)
286 s = sh_pread(fd, p->data, c->page_size, pos);
288 if (c->pos != pos || c->pos_fd != (int)fd)
289 sh_seek(fd, pos, SEEK_SET);
290 s = read(fd, p->data, c->page_size);
295 die("pgc_read(%d): %m", fd);
296 if (s != (int) c->page_size)
297 die("pgc_read(%d): incomplete page (only %d of %d)", p->fd, s, c->page_size);
298 p->flags |= PG_FLAG_VALID;
304 pgc_get(struct page_cache *c, int fd, sh_off_t pos)
308 ASSERT(!PAGE_OFFSET(pos));
309 p = get_and_lock_page(c, pos, fd);
310 p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
315 pgc_get_zero(struct page_cache *c, int fd, sh_off_t pos)
319 ASSERT(!PAGE_OFFSET(pos));
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");