along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <assert.h>
-#include <sys/mman.h>
#include <errno.h>
#include <stdlib.h>
+#include <sys/mman.h>
#include <string.h>
+#include "hashmap.h"
+#include "list.h"
+#include "log.h"
#include "util.h"
-
+#include "macro.h"
#include "mmap-cache.h"
-#define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
-#define WINDOWS_MAX 32
+typedef struct Window Window;
+typedef struct Context Context;
+typedef struct FileDescriptor FileDescriptor;
-typedef struct Window {
- int fd;
+struct Window {
+ MMapCache *cache;
+
+ bool keep_always;
+ bool in_unused;
+
+ int prot;
void *ptr;
uint64_t offset;
- uint64_t size;
+ size_t size;
- unsigned n_ref;
- unsigned lru_prev;
- unsigned lru_next;
+ FileDescriptor *fd;
- unsigned by_fd_prev;
- unsigned by_fd_next;
-} Window;
+ LIST_FIELDS(Window, by_fd);
+ LIST_FIELDS(Window, unused);
-typedef struct FileDescriptor {
+ LIST_HEAD(Context, contexts);
+};
+
+struct Context {
+ MMapCache *cache;
+ unsigned id;
+ Window *window;
+
+ LIST_FIELDS(Context, by_window);
+};
+
+struct FileDescriptor {
+ MMapCache *cache;
int fd;
- unsigned windows;
-} FileDescriptor;
+ LIST_HEAD(Window, windows);
+};
struct MMapCache {
- unsigned n_ref;
+ int n_ref;
+ unsigned n_windows;
- unsigned contexts_max;
- unsigned windows_max;
- unsigned fds_max;
+ unsigned n_hit, n_missed;
- unsigned n_windows;
- unsigned n_fds;
- unsigned lru_first, lru_last;
+ Hashmap *fds;
+ Hashmap *contexts;
- Window *windows;
- unsigned *by_context;
- FileDescriptor *by_fd;
+ LIST_HEAD(Window, unused);
+ Window *last_unused;
};
-static void mmap_cache_window_unmap(MMapCache *m, unsigned w) {
- Window *v;
+#define WINDOWS_MIN 64
+#define WINDOW_SIZE (8ULL*1024ULL*1024ULL)
- assert(m);
- assert(w < m->n_windows);
+MMapCache* mmap_cache_new(void) {
+ MMapCache *m;
- v = m->windows + w;
- if (!v->ptr)
- return;
+ m = new0(MMapCache, 1);
+ if (!m)
+ return NULL;
- munmap(v->ptr, v->size);
- v->ptr = NULL;
+ m->n_ref = 1;
+ return m;
}
-static void mmap_cache_window_add_lru(MMapCache *m, unsigned w) {
- Window *v;
-
+MMapCache* mmap_cache_ref(MMapCache *m) {
assert(m);
- assert(w < m->n_windows);
-
- v = m->windows + w;
- v->lru_prev = m->lru_last;
- v->lru_next = (unsigned) -1;
+ assert(m->n_ref > 0);
- m->lru_last = w;
- if (m->lru_first == (unsigned) -1)
- m->lru_first = w;
+ m->n_ref ++;
+ return m;
}
-static void mmap_cache_window_remove_lru(MMapCache *m, unsigned w) {
- Window *v;
+static void window_unlink(Window *w) {
+ Context *c;
- assert(m);
- assert(w < m->n_windows);
-
- v = m->windows + w;
-
- if (v->lru_prev == (unsigned) -1)
- m->lru_first = v->lru_next;
- else
- m->windows[v->lru_prev].lru_next = v->lru_next;
+ assert(w);
- if (v->lru_next == (unsigned) -1)
- m->lru_last = v->lru_prev;
- else
- m->windows[v->lru_next].lru_prev = v->lru_prev;
-}
+ if (w->ptr)
+ munmap(w->ptr, w->size);
-static void mmap_cache_fd_add(MMapCache *m, unsigned fd_index, unsigned w) {
- Window *v;
+ if (w->fd)
+ LIST_REMOVE(by_fd, w->fd->windows, w);
- assert(m);
- assert(fd_index < m->n_fds);
+ if (w->in_unused) {
+ if (w->cache->last_unused == w)
+ w->cache->last_unused = w->unused_prev;
- v = m->windows + w;
- v->by_fd_next = m->by_fd[fd_index].windows;
- v->by_fd_prev = (unsigned) -1;
+ LIST_REMOVE(unused, w->cache->unused, w);
+ }
- m->by_fd[fd_index].windows = w;
+ LIST_FOREACH(by_window, c, w->contexts) {
+ assert(c->window == w);
+ c->window = NULL;
+ }
}
-static void mmap_cache_fd_remove(MMapCache *m, unsigned fd_index, unsigned w) {
- Window *v;
+static void window_free(Window *w) {
+ assert(w);
- assert(m);
- assert(fd_index < m->n_fds);
+ window_unlink(w);
+ w->cache->n_windows--;
+ free(w);
+}
- v = m->windows + w;
- if (v->by_fd_prev == (unsigned) -1)
- m->by_fd[fd_index].windows = v->by_fd_next;
- else
- m->windows[v->by_fd_prev].by_fd_next = v->by_fd_next;
+_pure_ static bool window_matches(Window *w, int fd, int prot, uint64_t offset, size_t size) {
+ assert(w);
+ assert(fd >= 0);
+ assert(size > 0);
- if (v->by_fd_next != (unsigned) -1)
- m->windows[v->by_fd_next].by_fd_prev = v->by_fd_prev;
+ return
+ w->fd &&
+ fd == w->fd->fd &&
+ prot == w->prot &&
+ offset >= w->offset &&
+ offset + size <= w->offset + w->size;
}
-static void mmap_cache_context_unset(MMapCache *m, unsigned c) {
- Window *v;
- unsigned w;
+static Window *window_add(MMapCache *m) {
+ Window *w;
assert(m);
- assert(c < m->contexts_max);
- if (m->by_context[c] == (unsigned) -1)
- return;
+ if (!m->last_unused || m->n_windows <= WINDOWS_MIN) {
- w = m->by_context[c];
- m->by_context[c] = (unsigned) -1;
+ /* Allocate a new window */
+ w = new0(Window, 1);
+ if (!w)
+ return NULL;
+ m->n_windows++;
+ } else {
- v = m->windows + w;
- assert(v->n_ref > 0);
- v->n_ref --;
+ /* Reuse an existing one */
+ w = m->last_unused;
+ window_unlink(w);
+ zero(*w);
+ }
- if (v->n_ref == 0)
- mmap_cache_window_add_lru(m, w);
+ w->cache = m;
+ return w;
}
-static void mmap_cache_context_set(MMapCache *m, unsigned c, unsigned w) {
- Window *v;
+static void context_detach_window(Context *c) {
+ Window *w;
- assert(m);
- assert(c < m->contexts_max);
- assert(w < m->n_windows);
+ assert(c);
- if (m->by_context[c] == w)
+ if (!c->window)
return;
- mmap_cache_context_unset(m, c);
+ w = c->window;
+ c->window = NULL;
+ LIST_REMOVE(by_window, w->contexts, c);
- m->by_context[c] = w;
+ if (!w->contexts && !w->keep_always) {
+ /* Not used anymore? */
+ LIST_PREPEND(unused, c->cache->unused, w);
+ if (!c->cache->last_unused)
+ c->cache->last_unused = w;
- v = m->windows + w;
- v->n_ref ++;
- if (v->n_ref == 1)
- mmap_cache_window_remove_lru(m, w);
+ w->in_unused = true;
+ }
}
-static void mmap_cache_free(MMapCache *m) {
+static void context_attach_window(Context *c, Window *w) {
+ assert(c);
+ assert(w);
- assert(m);
+ if (c->window == w)
+ return;
- if (m->windows) {
- unsigned w;
+ context_detach_window(c);
- for (w = 0; w < m->n_windows; w++)
- mmap_cache_window_unmap(m, w);
+ if (w->in_unused) {
+ /* Used again? */
+ LIST_REMOVE(unused, c->cache->unused, w);
+ if (c->cache->last_unused == w)
+ c->cache->last_unused = w->unused_prev;
- free(m->windows);
+ w->in_unused = false;
}
- free(m->by_context);
- free(m->by_fd);
- free(m);
+ c->window = w;
+ LIST_PREPEND(by_window, w->contexts, c);
}
-MMapCache* mmap_cache_new(unsigned contexts_max, unsigned fds_max) {
- MMapCache *m;
+static Context *context_add(MMapCache *m, unsigned id) {
+ Context *c;
+ int r;
- assert(contexts_max > 0);
- assert(fds_max > 0);
+ assert(m);
- m = new0(MMapCache, 1);
- if (!m)
- return NULL;
+ c = hashmap_get(m->contexts, UINT_TO_PTR(id + 1));
+ if (c)
+ return c;
- m->contexts_max = contexts_max;
- m->fds_max = fds_max;
- m->windows_max = MAX(m->contexts_max, WINDOWS_MAX);
- m->n_ref = 1;
- m->lru_first = (unsigned) -1;
- m->lru_last = (unsigned) -1;
-
- m->windows = new(Window, m->windows_max);
- if (!m->windows) {
- mmap_cache_free(m);
+ r = hashmap_ensure_allocated(&m->contexts, trivial_hash_func, trivial_compare_func);
+ if (r < 0)
return NULL;
- }
- m->by_context = new(unsigned, m->contexts_max);
- if (!m->by_context) {
- mmap_cache_free(m);
+ c = new0(Context, 1);
+ if (!c)
return NULL;
- }
- memset(m->by_context, -1, m->contexts_max * sizeof(unsigned));
- m->by_fd = new(FileDescriptor, m->fds_max);
- if (!m->by_fd) {
- mmap_cache_free(m);
+ c->cache = m;
+ c->id = id;
+
+ r = hashmap_put(m->contexts, UINT_TO_PTR(id + 1), c);
+ if (r < 0) {
+ free(c);
return NULL;
}
- return m;
+ return c;
}
-MMapCache* mmap_cache_ref(MMapCache *m) {
- assert(m);
- assert(m->n_ref > 0);
+static void context_free(Context *c) {
+ assert(c);
- m->n_ref++;
- return m;
-}
+ context_detach_window(c);
-MMapCache* mmap_cache_unref(MMapCache *m) {
- assert(m);
- assert(m->n_ref > 0);
+ if (c->cache)
+ assert_se(hashmap_remove(c->cache->contexts, UINT_TO_PTR(c->id + 1)));
- if (m->n_ref == 1)
- mmap_cache_free(m);
- else
- m->n_ref--;
-
- return NULL;
+ free(c);
}
-static int mmap_cache_allocate_window(MMapCache *m, unsigned *w) {
- assert(m);
- assert(w);
-
- if (m->n_windows < m->windows_max) {
- *w = m->n_windows ++;
- return 0;
- }
+static void fd_free(FileDescriptor *f) {
+ assert(f);
- if (m->lru_first == (unsigned) -1)
- return -E2BIG;
+ while (f->windows)
+ window_free(f->windows);
- *w = m->lru_first;
- mmap_cache_window_unmap(m, *w);
- mmap_cache_window_remove_lru(m, *w);
+ if (f->cache)
+ assert_se(hashmap_remove(f->cache->fds, INT_TO_PTR(f->fd + 1)));
- return 0;
+ free(f);
}
-static int mmap_cache_make_room(MMapCache *m) {
- unsigned w;
+static FileDescriptor* fd_add(MMapCache *m, int fd) {
+ FileDescriptor *f;
+ int r;
assert(m);
+ assert(fd >= 0);
+
+ f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
+ if (f)
+ return f;
- w = m->lru_first;
- while (w != (unsigned) -1) {
- Window *v;
+ r = hashmap_ensure_allocated(&m->fds, trivial_hash_func, trivial_compare_func);
+ if (r < 0)
+ return NULL;
- v = m->windows + w;
+ f = new0(FileDescriptor, 1);
+ if (!f)
+ return NULL;
- if (v->ptr) {
- mmap_cache_window_unmap(m, w);
- return 1;
- }
+ f->cache = m;
+ f->fd = fd;
- w = v->lru_next;
+ r = hashmap_put(m->fds, UINT_TO_PTR(fd + 1), f);
+ if (r < 0) {
+ free(f);
+ return NULL;
}
- return 0;
+ return f;
}
-static int mmap_cache_put(
- MMapCache *m,
- int fd,
- unsigned fd_index,
- int prot,
- unsigned context,
- uint64_t offset,
- uint64_t size,
- void **ret) {
-
- unsigned w;
- Window *v;
- void *d;
- uint64_t woffset, wsize;
- int r;
+static void mmap_cache_free(MMapCache *m) {
+ Context *c;
+ FileDescriptor *f;
assert(m);
- assert(fd >= 0);
- assert(context < m->contexts_max);
- assert(size > 0);
- assert(ret);
- woffset = offset & ~((uint64_t) page_size() - 1ULL);
- wsize = size + (offset - woffset);
- wsize = PAGE_ALIGN(wsize);
+ while ((c = hashmap_first(m->contexts)))
+ context_free(c);
- if (wsize < WINDOW_SIZE) {
- uint64_t delta;
+ hashmap_free(m->contexts);
- delta = PAGE_ALIGN((WINDOW_SIZE - wsize) / 2);
+ while ((f = hashmap_first(m->fds)))
+ fd_free(f);
- if (delta > offset)
- woffset = 0;
- else
- woffset -= delta;
+ hashmap_free(m->fds);
- wsize = WINDOW_SIZE;
- }
+ while (m->unused)
+ window_free(m->unused);
- for (;;) {
- d = mmap(NULL, wsize, prot, MAP_SHARED, fd, woffset);
- if (d != MAP_FAILED)
- break;
- if (errno != ENOMEM)
- return -errno;
+ free(m);
+}
- r = mmap_cache_make_room(m);
- if (r < 0)
- return r;
- if (r == 0)
- return -ENOMEM;
- }
+MMapCache* mmap_cache_unref(MMapCache *m) {
+ assert(m);
+ assert(m->n_ref > 0);
- r = mmap_cache_allocate_window(m, &w);
- if (r < 0) {
- munmap(d, wsize);
- return r;
- }
+ m->n_ref --;
+ if (m->n_ref == 0)
+ mmap_cache_free(m);
- v = m->windows + w;
- v->fd = fd;
- v->ptr = d;
- v->offset = woffset;
- v->size = wsize;
+ return NULL;
+}
- v->n_ref = 0;
- v->lru_prev = v->lru_next = (unsigned) -1;
+static int make_room(MMapCache *m) {
+ assert(m);
- mmap_cache_fd_add(m, fd_index, w);
- mmap_cache_context_set(m, context, w);
+ if (!m->last_unused)
+ return 0;
- *ret = (uint8_t*) d + (offset - woffset);
+ window_free(m->last_unused);
return 1;
}
-static int fd_cmp(const void *_a, const void *_b) {
- const FileDescriptor *a = _a, *b = _b;
-
- if (a->fd < b->fd)
- return -1;
- if (a->fd > b->fd)
- return 1;
-
- return 0;
-}
+static int try_context(
+ MMapCache *m,
+ int fd,
+ int prot,
+ unsigned context,
+ bool keep_always,
+ uint64_t offset,
+ size_t size,
+ void **ret) {
-static int mmap_cache_get_fd_index(MMapCache *m, int fd, unsigned *fd_index) {
- FileDescriptor *j;
+ Context *c;
assert(m);
+ assert(m->n_ref > 0);
assert(fd >= 0);
- assert(fd_index);
+ assert(size > 0);
+ assert(ret);
- j = bsearch(&fd, m->by_fd, m->n_fds, sizeof(m->by_fd[0]), fd_cmp);
- if (!j) {
- if (m->n_fds >= m->fds_max)
- return -E2BIG;
+ c = hashmap_get(m->contexts, UINT_TO_PTR(context+1));
+ if (!c)
+ return 0;
- j = m->by_fd + m->n_fds ++;
- j->fd = fd;
- j->windows = (unsigned) -1;
+ assert(c->id == context);
- qsort(m->by_fd, m->n_fds, sizeof(m->by_fd[0]), fd_cmp);
- j = bsearch(&fd, m->by_fd, m->n_fds, sizeof(m->by_fd[0]), fd_cmp);
- }
+ if (!c->window)
+ return 0;
- *fd_index = (unsigned) (j - m->by_fd);
- return 0;
-}
+ if (!window_matches(c->window, fd, prot, offset, size)) {
-static bool mmap_cache_test_window(
- MMapCache *m,
- unsigned w,
- uint64_t offset,
- uint64_t size) {
- Window *v;
-
- assert(m);
- assert(w < m->n_windows);
- assert(size > 0);
+ /* Drop the reference to the window, since it's unnecessary now */
+ context_detach_window(c);
+ return 0;
+ }
- v = m->windows + w;
+ c->window->keep_always = c->window->keep_always || keep_always;
- return offset >= v->offset &&
- offset + size <= v->offset + v->size;
+ *ret = (uint8_t*) c->window->ptr + (offset - c->window->offset);
+ return 1;
}
-static int mmap_cache_current(
+static int find_mmap(
MMapCache *m,
int fd,
+ int prot,
unsigned context,
+ bool keep_always,
uint64_t offset,
- uint64_t size,
+ size_t size,
void **ret) {
- Window *v;
- unsigned w;
+ FileDescriptor *f;
+ Window *w;
+ Context *c;
assert(m);
+ assert(m->n_ref > 0);
assert(fd >= 0);
- assert(context < m->contexts_max);
assert(size > 0);
assert(ret);
- if (m->by_context[context] == (unsigned) -1)
+ f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
+ if (!f)
return 0;
- w = m->by_context[context];
- v = m->windows + w;
+ assert(f->fd == fd);
- if (v->fd != fd)
- return 0;
+ LIST_FOREACH(by_fd, w, f->windows)
+ if (window_matches(w, fd, prot, offset, size))
+ break;
- if (!mmap_cache_test_window(m, w, offset, size))
+ if (!w)
return 0;
- *ret = (uint8_t*) v->ptr + (offset - v->offset);
+ c = context_add(m, context);
+ if (!c)
+ return -ENOMEM;
+
+ context_attach_window(c, w);
+ w->keep_always = w->keep_always || keep_always;
+
+ *ret = (uint8_t*) w->ptr + (offset - w->offset);
return 1;
}
-static int mmap_cache_find(
+static int add_mmap(
MMapCache *m,
- unsigned fd_index,
+ int fd,
+ int prot,
unsigned context,
+ bool keep_always,
uint64_t offset,
- uint64_t size,
+ size_t size,
+ struct stat *st,
void **ret) {
- Window *v = NULL;
- unsigned w;
+ uint64_t woffset, wsize;
+ Context *c;
+ FileDescriptor *f;
+ Window *w;
+ void *d;
+ int r;
assert(m);
- assert(fd_index < m->n_fds);
- assert(context < m->contexts_max);
+ assert(m->n_ref > 0);
+ assert(fd >= 0);
assert(size > 0);
assert(ret);
- w = m->by_fd[fd_index].windows;
- while (w != (unsigned) -1) {
- if (mmap_cache_test_window(m, w, offset, size))
+ woffset = offset & ~((uint64_t) page_size() - 1ULL);
+ wsize = size + (offset - woffset);
+ wsize = PAGE_ALIGN(wsize);
+
+ if (wsize < WINDOW_SIZE) {
+ uint64_t delta;
+
+ delta = PAGE_ALIGN((WINDOW_SIZE - wsize) / 2);
+
+ if (delta > offset)
+ woffset = 0;
+ else
+ woffset -= delta;
+
+ wsize = WINDOW_SIZE;
+ }
+
+ if (st) {
+ /* Memory maps that are larger then the files
+ underneath have undefined behavior. Hence, clamp
+ things to the file size if we know it */
+
+ if (woffset >= (uint64_t) st->st_size)
+ return -EADDRNOTAVAIL;
+
+ if (woffset + wsize > (uint64_t) st->st_size)
+ wsize = PAGE_ALIGN(st->st_size - woffset);
+ }
+
+ for (;;) {
+ d = mmap(NULL, wsize, prot, MAP_SHARED, fd, woffset);
+ if (d != MAP_FAILED)
break;
+ if (errno != ENOMEM)
+ return -errno;
- w = m->windows[w].by_fd_next;
+ r = make_room(m);
+ if (r < 0)
+ return r;
+ if (r == 0)
+ return -ENOMEM;
}
- if (w == (unsigned) -1)
- return 0;
+ c = context_add(m, context);
+ if (!c)
+ return -ENOMEM;
+
+ f = fd_add(m, fd);
+ if (!f)
+ return -ENOMEM;
- mmap_cache_context_set(m, context, w);
+ w = window_add(m);
+ if (!w)
+ return -ENOMEM;
- v = m->windows + w;
- *ret = (uint8_t*) v->ptr + (offset - v->offset);
+ w->keep_always = keep_always;
+ w->ptr = d;
+ w->offset = woffset;
+ w->prot = prot;
+ w->size = wsize;
+ w->fd = f;
+
+ LIST_PREPEND(by_fd, f->windows, w);
+
+ context_detach_window(c);
+ c->window = w;
+ LIST_PREPEND(by_window, w->contexts, c);
+
+ *ret = (uint8_t*) w->ptr + (offset - w->offset);
return 1;
}
int fd,
int prot,
unsigned context,
+ bool keep_always,
uint64_t offset,
- uint64_t size,
+ size_t size,
+ struct stat *st,
void **ret) {
- unsigned fd_index;
int r;
assert(m);
+ assert(m->n_ref > 0);
assert(fd >= 0);
- assert(context < m->contexts_max);
assert(size > 0);
assert(ret);
- /* Maybe the current pointer for this context is already the
- * right one? */
- r = mmap_cache_current(m, fd, context, offset, size, ret);
- if (r != 0)
+ /* Check whether the current context is the right one already */
+ r = try_context(m, fd, prot, context, keep_always, offset, size, ret);
+ if (r != 0) {
+ m->n_hit ++;
return r;
+ }
- /* OK, let's find the chain for this FD */
- r = mmap_cache_get_fd_index(m, fd, &fd_index);
- if (r < 0)
+ /* Search for a matching mmap */
+ r = find_mmap(m, fd, prot, context, keep_always, offset, size, ret);
+ if (r != 0) {
+ m->n_hit ++;
return r;
+ }
- /* And let's look through the available mmaps */
- r = mmap_cache_find(m, fd_index, context, offset, size, ret);
- if (r != 0)
- return r;
+ m->n_missed++;
- /* Not found? Then, let's add it */
- return mmap_cache_put(m, fd, fd_index, prot, context, offset, size, ret);
+ /* Create a new mmap */
+ return add_mmap(m, fd, prot, context, keep_always, offset, size, st, ret);
}
void mmap_cache_close_fd(MMapCache *m, int fd) {
- FileDescriptor *j;
- unsigned fd_index, c, w;
+ FileDescriptor *f;
assert(m);
- assert(fd > 0);
+ assert(fd >= 0);
- j = bsearch(&fd, m->by_fd, m->n_fds, sizeof(m->by_fd[0]), fd_cmp);
- if (!j)
+ f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
+ if (!f)
return;
- fd_index = (unsigned) (j - m->by_fd);
- for (c = 0; c < m->contexts_max; c++) {
- w = m->by_context[c];
- if (w == (unsigned) -1)
- continue;
+ fd_free(f);
+}
- if (m->windows[w].fd == fd)
- mmap_cache_context_unset(m, c);
- }
+void mmap_cache_close_context(MMapCache *m, unsigned context) {
+ Context *c;
+
+ assert(m);
- w = m->by_fd[fd_index].windows;
- while (w != (unsigned) -1) {
+ c = hashmap_get(m->contexts, UINT_TO_PTR(context + 1));
+ if (!c)
+ return;
- mmap_cache_fd_remove(m, fd_index, w);
- mmap_cache_window_unmap(m, w);
+ context_free(c);
+}
- w = m->by_fd[fd_index].windows;
- }
+unsigned mmap_cache_get_hit(MMapCache *m) {
+ assert(m);
- memmove(m->by_fd + fd_index, m->by_fd + fd_index + 1, (m->n_fds - (fd_index + 1)) * sizeof(FileDescriptor));
- m->n_fds --;
+ return m->n_hit;
}
-void mmap_cache_close_context(MMapCache *m, unsigned context) {
- mmap_cache_context_unset(m, context);
+unsigned mmap_cache_get_missed(MMapCache *m) {
+ assert(m);
+
+ return m->n_missed;
}