/* Reread fstat() of the file for detecting deletions at least this often */
#define LAST_STAT_REFRESH_USEC (5*USEC_PER_SEC)
-/* The mmap context to use for the header we pick as one above the last defined typed */
-#define CONTEXT_HEADER _OBJECT_TYPE_MAX
-
/* Longest hash chain to rotate after */
#define HASH_CHAIN_DEPTH_MAX 100
return journal_file_fstat(f);
}
-static unsigned type_to_context(ObjectType type) {
- /* One context for each type, plus one catch-all for the rest */
- assert_cc(_OBJECT_TYPE_MAX <= MMAP_CACHE_MAX_CONTEXTS);
- assert_cc(CONTEXT_HEADER < MMAP_CACHE_MAX_CONTEXTS);
- return type > OBJECT_UNUSED && type < _OBJECT_TYPE_MAX ? type : 0;
-}
-
static int journal_file_move_to(
JournalFile *f,
ObjectType type,
return -EADDRNOTAVAIL;
}
- return mmap_cache_fd_get(f->cache_fd, type_to_context(type), keep_always, offset, size, &f->last_stat, ret);
+ return mmap_cache_fd_get(f->cache_fd, type_to_category(type), keep_always, offset, size, &f->last_stat, ret);
}
static uint64_t minimum_header_size(JournalFile *f, Object *o) {
goto fail;
}
- r = mmap_cache_fd_get(f->cache_fd, CONTEXT_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h);
+ r = mmap_cache_fd_get(f->cache_fd, MMAP_CACHE_CATEGORY_HEADER, true, 0, PAGE_ALIGN(sizeof(Header)), &f->last_stat, &h);
if (r == -EINVAL) {
/* Some file systems (jffs2 or p9fs) don't support mmap() properly (or only read-only
* mmap()), and return EINVAL in that case. Let's propagate that as a more recognizable error
typedef struct Window Window;
typedef enum WindowFlags {
- WINDOW_KEEP_ALWAYS = 1u << (MMAP_CACHE_MAX_CONTEXTS + 0),
- WINDOW_IN_UNUSED = 1u << (MMAP_CACHE_MAX_CONTEXTS + 1),
- WINDOW_INVALIDATED = 1u << (MMAP_CACHE_MAX_CONTEXTS + 2),
+ WINDOW_KEEP_ALWAYS = 1u << (_MMAP_CACHE_CATEGORY_MAX + 0),
+ WINDOW_IN_UNUSED = 1u << (_MMAP_CACHE_CATEGORY_MAX + 1),
+ WINDOW_INVALIDATED = 1u << (_MMAP_CACHE_CATEGORY_MAX + 2),
_WINDOW_USED_MASK = WINDOW_IN_UNUSED - 1, /* The mask contains all bits that indicate the windows
* is currently in use. Covers the all the object types
unsigned n_ref;
unsigned n_windows;
- unsigned n_context_cache_hit;
+ unsigned n_category_cache_hit;
unsigned n_window_list_hit;
unsigned n_missed;
LIST_HEAD(Window, unused);
Window *last_unused;
- Window *windows_by_context[MMAP_CACHE_MAX_CONTEXTS];
+ Window *windows_by_category[_MMAP_CACHE_CATEGORY_MAX];
};
#define WINDOWS_MIN 64
LIST_REMOVE(unused, m->unused, w);
}
- for (unsigned i = 0; i < MMAP_CACHE_MAX_CONTEXTS; i++)
+ for (unsigned i = 0; i < _MMAP_CACHE_CATEGORY_MAX; i++)
if (FLAGS_SET(w->flags, 1u << i))
- assert_se(TAKE_PTR(m->windows_by_context[i]) == w);
+ assert_se(TAKE_PTR(m->windows_by_category[i]) == w);
return LIST_REMOVE(windows, w->fd->windows, w);
}
return LIST_PREPEND(windows, f->windows, w);
}
-static void context_detach_window(MMapCache *m, unsigned c) {
+static void category_detach_window(MMapCache *m, MMapCacheCategory c) {
Window *w;
assert(m);
- assert(c < MMAP_CACHE_MAX_CONTEXTS);
+ assert(c >= 0 && c < _MMAP_CACHE_CATEGORY_MAX);
- w = TAKE_PTR(m->windows_by_context[c]);
+ w = TAKE_PTR(m->windows_by_category[c]);
if (!w)
return; /* Nothing attached. */
}
}
-static void context_attach_window(MMapCache *m, unsigned c, Window *w) {
+static void category_attach_window(MMapCache *m, MMapCacheCategory c, Window *w) {
assert(m);
- assert(c < MMAP_CACHE_MAX_CONTEXTS);
+ assert(c >= 0 && c < _MMAP_CACHE_CATEGORY_MAX);
assert(w);
- if (m->windows_by_context[c] == w)
+ if (m->windows_by_category[c] == w)
return; /* Already attached. */
- context_detach_window(m, c);
+ category_detach_window(m, c);
if (FLAGS_SET(w->flags, WINDOW_IN_UNUSED)) {
/* Used again? */
w->flags &= ~WINDOW_IN_UNUSED;
}
- m->windows_by_context[c] = w;
+ m->windows_by_category[c] = w;
w->flags |= (1u << c);
}
int mmap_cache_fd_get(
MMapFileDescriptor *f,
- unsigned c,
+ MMapCacheCategory c,
bool keep_always,
uint64_t offset,
size_t size,
int r;
assert(size > 0);
- assert(c < MMAP_CACHE_MAX_CONTEXTS);
+ assert(c >= 0 && c < _MMAP_CACHE_CATEGORY_MAX);
assert(ret);
if (f->sigbus)
return -EIO;
- /* Check whether the current context is the right one already */
- if (window_matches(m->windows_by_context[c], f, offset, size)) {
- m->n_context_cache_hit++;
- w = m->windows_by_context[c];
+ /* Check whether the current category is the right one already */
+ if (window_matches(m->windows_by_category[c], f, offset, size)) {
+ m->n_category_cache_hit++;
+ w = m->windows_by_category[c];
goto found;
}
/* Drop the reference to the window, since it's unnecessary now */
- context_detach_window(m, c);
+ category_detach_window(m, c);
/* Search for a matching mmap */
LIST_FOREACH(windows, i, f->windows)
if (keep_always)
w->flags |= WINDOW_KEEP_ALWAYS;
- context_attach_window(m, c, w);
+ category_attach_window(m, c, w);
*ret = (uint8_t*) w->ptr + (offset - w->offset);
return 0;
}
void mmap_cache_stats_log_debug(MMapCache *m) {
assert(m);
- log_debug("mmap cache statistics: %u context cache hit, %u window list hit, %u miss",
- m->n_context_cache_hit, m->n_window_list_hit, m->n_missed);
+ log_debug("mmap cache statistics: %u category cache hit, %u window list hit, %u miss",
+ m->n_category_cache_hit, m->n_window_list_hit, m->n_missed);
}
static void mmap_cache_process_sigbus(MMapCache *m) {
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
+#include <errno.h>
#include <stdbool.h>
#include <sys/stat.h>
-/* One context per object type, plus one of the header, plus one "additional" one */
-#define MMAP_CACHE_MAX_CONTEXTS 9
+#include "journal-def.h"
typedef struct MMapCache MMapCache;
typedef struct MMapFileDescriptor MMapFileDescriptor;
+typedef enum MMapCacheCategory {
+ MMAP_CACHE_CATEGORY_ANY = OBJECT_UNUSED,
+ MMAP_CACHE_CATEGORY_DATA = OBJECT_DATA,
+ MMAP_CACHE_CATEGORY_FIELD = OBJECT_FIELD,
+ MMAP_CACHE_CATEGORY_ENTRY = OBJECT_ENTRY,
+ MMAP_CACHE_CATEGORY_DATA_HASH_TABLE = OBJECT_DATA_HASH_TABLE,
+ MMAP_CACHE_CATEGORY_FIELD_HASH_TABLE = OBJECT_FIELD_HASH_TABLE,
+ MMAP_CACHE_CATEGORY_ENTRY_ARRAY = OBJECT_ENTRY_ARRAY,
+ MMAP_CACHE_CATEGORY_TAG = OBJECT_TAG,
+ MMAP_CACHE_CATEGORY_HEADER, /* for reading file header */
+ _MMAP_CACHE_CATEGORY_MAX,
+ _MMAP_CACHE_CATEGORY_INVALID = -EINVAL,
+} MMapCacheCategory;
+
+assert_cc((int) _OBJECT_TYPE_MAX < (int) _MMAP_CACHE_CATEGORY_MAX);
+
+static inline MMapCacheCategory type_to_category(ObjectType type) {
+ return type >= 0 && type < _OBJECT_TYPE_MAX ? (MMapCacheCategory) type : MMAP_CACHE_CATEGORY_ANY;
+}
+
MMapCache* mmap_cache_new(void);
MMapCache* mmap_cache_ref(MMapCache *m);
MMapCache* mmap_cache_unref(MMapCache *m);
int mmap_cache_fd_get(
MMapFileDescriptor *f,
- unsigned context,
+ MMapCacheCategory c,
bool keep_always,
uint64_t offset,
size_t size,