From b42549ad69ab51789d5b8fa9f1dfc6c2fe45f43e Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 12 Jul 2017 22:08:58 -0700 Subject: [PATCH] journal: return mapped size from mmap_cache_get() If requested, return the actual mapping size to the caller in addition to the address. journal_file_move_to_object() often performs two successive mmap_cache_get() calls via journal_file_move_to(); one to get the object header, then another to get the entire object when it's larger than the header's size. If mmap_cache_get() returned the actual mapping's size, it's probable that the second mmap_cache_get() could be skipped when the established mapping already encompassed the desired size. --- src/journal/journal-file.c | 8 ++++---- src/journal/journal-verify.c | 2 +- src/journal/mmap-cache.c | 27 ++++++++++++++++++++------- src/journal/mmap-cache.h | 3 ++- src/journal/test-mmap-cache.c | 10 +++++----- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index 4ff38de..05cbf08 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -749,7 +749,7 @@ static int journal_file_move_to(JournalFile *f, ObjectType type, bool keep_alway return -EADDRNOTAVAIL; } - return mmap_cache_get(f->mmap, f->cache_fd, f->prot, type_to_context(type), keep_always, offset, size, &f->last_stat, ret); + return mmap_cache_get(f->mmap, f->cache_fd, f->prot, type_to_context(type), keep_always, offset, size, &f->last_stat, ret, NULL); } static uint64_t minimum_header_size(Object *o) { @@ -991,7 +991,7 @@ int journal_file_map_data_hash_table(JournalFile *f) { OBJECT_DATA_HASH_TABLE, true, p, s, - &t); + &t, NULL); if (r < 0) return r; @@ -1017,7 +1017,7 @@ int journal_file_map_field_hash_table(JournalFile *f) { OBJECT_FIELD_HASH_TABLE, true, p, s, - &t); + &t, NULL); if (r < 0) return r; @@ -3196,7 +3196,7 @@ int journal_file_open( goto fail; } - r = mmap_cache_get(f->mmap, f->cache_fd, f->prot, CONTEXT_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h); + r = mmap_cache_get(f->mmap, f->cache_fd, f->prot, CONTEXT_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h, NULL); if (r < 0) goto fail; diff --git a/src/journal/journal-verify.c b/src/journal/journal-verify.c index 9feb5b5..3f4c38c 100644 --- a/src/journal/journal-verify.c +++ b/src/journal/journal-verify.c @@ -392,7 +392,7 @@ static int contains_uint64(MMapCache *m, MMapFileDescriptor *f, uint64_t n, uint c = (a + b) / 2; - r = mmap_cache_get(m, f, PROT_READ|PROT_WRITE, 0, false, c * sizeof(uint64_t), sizeof(uint64_t), NULL, (void **) &z); + r = mmap_cache_get(m, f, PROT_READ|PROT_WRITE, 0, false, c * sizeof(uint64_t), sizeof(uint64_t), NULL, (void **) &z, NULL); if (r < 0) return r; diff --git a/src/journal/mmap-cache.c b/src/journal/mmap-cache.c index 5dfda73..546710c 100644 --- a/src/journal/mmap-cache.c +++ b/src/journal/mmap-cache.c @@ -338,7 +338,8 @@ static int try_context( bool keep_always, uint64_t offset, size_t size, - void **ret) { + void **ret, + size_t *ret_size) { Context *c; @@ -370,6 +371,9 @@ static int try_context( c->window->keep_always = c->window->keep_always || keep_always; *ret = (uint8_t*) c->window->ptr + (offset - c->window->offset); + if (ret_size) + *ret_size = c->window->size - (offset - c->window->offset); + return 1; } @@ -381,7 +385,8 @@ static int find_mmap( bool keep_always, uint64_t offset, size_t size, - void **ret) { + void **ret, + size_t *ret_size) { Window *w; Context *c; @@ -409,6 +414,9 @@ static int find_mmap( w->keep_always = w->keep_always || keep_always; *ret = (uint8_t*) w->ptr + (offset - w->offset); + if (ret_size) + *ret_size = w->size - (offset - w->offset); + return 1; } @@ -448,7 +456,8 @@ static int add_mmap( uint64_t offset, size_t size, struct stat *st, - void **ret) { + void **ret, + size_t *ret_size) { uint64_t woffset, wsize; Context *c; @@ -508,6 +517,9 @@ static int add_mmap( LIST_PREPEND(by_window, w->contexts, c); *ret = (uint8_t*) w->ptr + (offset - w->offset); + if (ret_size) + *ret_size = w->size - (offset - w->offset); + return 1; outofmem: @@ -524,7 +536,8 @@ int mmap_cache_get( uint64_t offset, size_t size, struct stat *st, - void **ret) { + void **ret, + size_t *ret_size) { int r; @@ -536,14 +549,14 @@ int mmap_cache_get( assert(context < MMAP_CACHE_MAX_CONTEXTS); /* Check whether the current context is the right one already */ - r = try_context(m, f, prot, context, keep_always, offset, size, ret); + r = try_context(m, f, prot, context, keep_always, offset, size, ret, ret_size); if (r != 0) { m->n_hit++; return r; } /* Search for a matching mmap */ - r = find_mmap(m, f, prot, context, keep_always, offset, size, ret); + r = find_mmap(m, f, prot, context, keep_always, offset, size, ret, ret_size); if (r != 0) { m->n_hit++; return r; @@ -552,7 +565,7 @@ int mmap_cache_get( m->n_missed++; /* Create a new mmap */ - return add_mmap(m, f, prot, context, keep_always, offset, size, st, ret); + return add_mmap(m, f, prot, context, keep_always, offset, size, st, ret, ret_size); } unsigned mmap_cache_get_hit(MMapCache *m) { diff --git a/src/journal/mmap-cache.h b/src/journal/mmap-cache.h index 7b33218..cf6af19 100644 --- a/src/journal/mmap-cache.h +++ b/src/journal/mmap-cache.h @@ -41,7 +41,8 @@ int mmap_cache_get( uint64_t offset, size_t size, struct stat *st, - void **ret); + void **ret, + size_t *ret_size); MMapFileDescriptor * mmap_cache_add_fd(MMapCache *m, int fd); void mmap_cache_free_fd(MMapCache *m, MMapFileDescriptor *f); diff --git a/src/journal/test-mmap-cache.c b/src/journal/test-mmap-cache.c index c51b069..702434e 100644 --- a/src/journal/test-mmap-cache.c +++ b/src/journal/test-mmap-cache.c @@ -51,23 +51,23 @@ int main(int argc, char *argv[]) { assert_se(z >= 0); unlink(pz); - r = mmap_cache_get(m, fx, PROT_READ, 0, false, 1, 2, NULL, &p); + r = mmap_cache_get(m, fx, PROT_READ, 0, false, 1, 2, NULL, &p, NULL); assert_se(r >= 0); - r = mmap_cache_get(m, fx, PROT_READ, 0, false, 2, 2, NULL, &q); + r = mmap_cache_get(m, fx, PROT_READ, 0, false, 2, 2, NULL, &q, NULL); assert_se(r >= 0); assert_se((uint8_t*) p + 1 == (uint8_t*) q); - r = mmap_cache_get(m, fx, PROT_READ, 1, false, 3, 2, NULL, &q); + r = mmap_cache_get(m, fx, PROT_READ, 1, false, 3, 2, NULL, &q, NULL); assert_se(r >= 0); assert_se((uint8_t*) p + 2 == (uint8_t*) q); - r = mmap_cache_get(m, fx, PROT_READ, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p); + r = mmap_cache_get(m, fx, PROT_READ, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p, NULL); assert_se(r >= 0); - r = mmap_cache_get(m, fx, PROT_READ, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q); + r = mmap_cache_get(m, fx, PROT_READ, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q, NULL); assert_se(r >= 0); assert_se((uint8_t*) p + 1 == (uint8_t*) q); -- 2.7.4