1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-27 07:22:31 +03:00

Merge pull request #6355 from vcaputo/journal_avoid_mmap_cache_get_calls

journal: avoid unnecessary mmap_cache_get() calls
This commit is contained in:
Lennart Poettering 2017-07-17 10:03:52 +02:00 committed by GitHub
commit ddf1819bc2
5 changed files with 38 additions and 23 deletions

View File

@ -727,7 +727,7 @@ static unsigned type_to_context(ObjectType type) {
return type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX ? type : 0;
}
static int journal_file_move_to(JournalFile *f, ObjectType type, bool keep_always, uint64_t offset, uint64_t size, void **ret) {
static int journal_file_move_to(JournalFile *f, ObjectType type, bool keep_always, uint64_t offset, uint64_t size, void **ret, size_t *ret_size) {
int r;
assert(f);
@ -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, ret_size);
}
static uint64_t minimum_header_size(Object *o) {
@ -773,6 +773,7 @@ static uint64_t minimum_header_size(Object *o) {
int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset, Object **ret) {
int r;
void *t;
size_t tsize;
Object *o;
uint64_t s;
@ -791,7 +792,7 @@ int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset
return -EBADMSG;
}
r = journal_file_move_to(f, type, false, offset, sizeof(ObjectHeader), &t);
r = journal_file_move_to(f, type, false, offset, sizeof(ObjectHeader), &t, &tsize);
if (r < 0)
return r;
@ -822,8 +823,8 @@ int journal_file_move_to_object(JournalFile *f, ObjectType type, uint64_t offset
return -EBADMSG;
}
if (s > sizeof(ObjectHeader)) {
r = journal_file_move_to(f, type, false, offset, s, &t);
if (s > tsize) {
r = journal_file_move_to(f, type, false, offset, s, &t, NULL);
if (r < 0)
return r;
@ -893,7 +894,7 @@ int journal_file_append_object(JournalFile *f, ObjectType type, uint64_t size, O
if (r < 0)
return r;
r = journal_file_move_to(f, type, false, p, size, &t);
r = journal_file_move_to(f, type, false, p, size, &t, NULL);
if (r < 0)
return r;
@ -991,7 +992,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 +1018,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 +3197,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;

View File

@ -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;

View File

@ -345,7 +345,8 @@ static int try_context(
bool keep_always,
uint64_t offset,
size_t size,
void **ret) {
void **ret,
size_t *ret_size) {
Context *c;
@ -377,6 +378,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;
}
@ -388,7 +392,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;
@ -416,6 +421,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;
}
@ -455,7 +463,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;
@ -513,6 +522,9 @@ static int add_mmap(
context_attach_window(c, w);
*ret = (uint8_t*) w->ptr + (offset - w->offset);
if (ret_size)
*ret_size = w->size - (offset - w->offset);
return 1;
outofmem:
@ -529,7 +541,8 @@ int mmap_cache_get(
uint64_t offset,
size_t size,
struct stat *st,
void **ret) {
void **ret,
size_t *ret_size) {
int r;
@ -541,14 +554,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;
@ -557,7 +570,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) {

View File

@ -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);

View File

@ -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);