]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mmap-cache: introduce enum MMapCacheCategory
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 30 Sep 2023 03:44:22 +0000 (12:44 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 4 Nov 2023 02:41:15 +0000 (11:41 +0900)
Then rename context -> category.

No functional change, just refactoring and modernization.

src/libsystemd/sd-journal/journal-def.h
src/libsystemd/sd-journal/journal-file.c
src/libsystemd/sd-journal/mmap-cache.c
src/libsystemd/sd-journal/mmap-cache.h

index e52a307c9fb76ebc4bc361acd9263a12640493ec..b773f26719eb791c0768e89497203402137b52c9 100644 (file)
@@ -30,7 +30,7 @@ typedef struct FSSHeader FSSHeader;
 
 /* Object types */
 typedef enum ObjectType {
-        OBJECT_UNUSED, /* also serves as "any type" or "additional context" */
+        OBJECT_UNUSED, /* also serves as "any type" or "additional category" */
         OBJECT_DATA,
         OBJECT_FIELD,
         OBJECT_ENTRY,
index 334a28f9486ed6d75f98a842bb8858fc4d886464..4466142784267320b7676cf2fad588e4a6d4b18b 100644 (file)
@@ -88,9 +88,6 @@
 /* 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
 
@@ -821,13 +818,6 @@ static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size)
         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,
@@ -864,7 +854,7 @@ static int journal_file_move_to(
                         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) {
@@ -4086,7 +4076,7 @@ int journal_file_open(
                 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
index c4741b2291c9dd39dab4b9c9a6801eae8735aad5..9e69e31ba3c5c0cc0a7c54198c70e58a69553bd0 100644 (file)
@@ -18,9 +18,9 @@
 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
@@ -56,7 +56,7 @@ struct MMapCache {
         unsigned n_ref;
         unsigned n_windows;
 
-        unsigned n_context_cache_hit;
+        unsigned n_category_cache_hit;
         unsigned n_window_list_hit;
         unsigned n_missed;
 
@@ -65,7 +65,7 @@ struct MMapCache {
         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
@@ -105,9 +105,9 @@ static Window* window_unlink(Window *w) {
                 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);
 }
@@ -170,13 +170,13 @@ static Window* window_add(MMapFileDescriptor *f, uint64_t offset, size_t size, v
         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. */
 
@@ -197,15 +197,15 @@ static void context_detach_window(MMapCache *m, unsigned c) {
         }
 }
 
-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? */
@@ -215,7 +215,7 @@ static void context_attach_window(MMapCache *m, unsigned c, Window *w) {
                 w->flags &= ~WINDOW_IN_UNUSED;
         }
 
-        m->windows_by_context[c] = w;
+        m->windows_by_category[c] = w;
         w->flags |= (1u << c);
 }
 
@@ -322,7 +322,7 @@ static int add_mmap(
 
 int mmap_cache_fd_get(
                 MMapFileDescriptor *f,
-                unsigned c,
+                MMapCacheCategory c,
                 bool keep_always,
                 uint64_t offset,
                 size_t size,
@@ -334,21 +334,21 @@ int mmap_cache_fd_get(
         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)
@@ -369,7 +369,7 @@ found:
         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;
 }
@@ -377,8 +377,8 @@ found:
 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) {
index 1279337cdd062e4da1226442e1a500ee2d5ad699..7568746e9322d274260876f75906ab502b991705 100644 (file)
@@ -1,15 +1,35 @@
 /* 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);
@@ -17,7 +37,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(MMapCache*, mmap_cache_unref);
 
 int mmap_cache_fd_get(
         MMapFileDescriptor *f,
-        unsigned context,
+        MMapCacheCategory c,
         bool keep_always,
         uint64_t offset,
         size_t size,