ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
572a44
From 563bf9e9305aa88ad403a81c0c91418b7846f465 Mon Sep 17 00:00:00 2001
572a44
From: Lennart Poettering <lennart@poettering.net>
572a44
Date: Tue, 26 Nov 2013 20:37:53 +0100
572a44
Subject: [PATCH] journal: optimize bisection logic a bit by caching the last
572a44
 position
572a44
572a44
This way we can do a quick restart limiting a bit how wildly we need to
572a44
jump around during the bisection process.
572a44
---
572a44
 src/journal/journal-file.c | 134 +++++++++++++++++++++++++++++++++------------
572a44
 1 file changed, 99 insertions(+), 35 deletions(-)
572a44
572a44
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
572a44
index 4062a83..7adb1b5 100644
572a44
--- a/src/journal/journal-file.c
572a44
+++ b/src/journal/journal-file.c
572a44
@@ -1366,6 +1366,7 @@ typedef struct ChainCacheItem {
572a44
         uint64_t array; /* the cached array */
572a44
         uint64_t begin; /* the first item in the cached array */
572a44
         uint64_t total; /* the total number of items in all arrays before this one in the chain */
572a44
+        uint64_t last_index; /* the last index we looked at, to optimize locality when bisecting */
572a44
 } ChainCacheItem;
572a44
 
572a44
 static void chain_cache_put(
572a44
@@ -1374,7 +1375,8 @@ static void chain_cache_put(
572a44
                 uint64_t first,
572a44
                 uint64_t array,
572a44
                 uint64_t begin,
572a44
-                uint64_t total) {
572a44
+                uint64_t total,
572a44
+                uint64_t last_index) {
572a44
 
572a44
         if (!ci) {
572a44
                 /* If the chain item to cache for this chain is the
572a44
@@ -1402,12 +1404,14 @@ static void chain_cache_put(
572a44
         ci->array = array;
572a44
         ci->begin = begin;
572a44
         ci->total = total;
572a44
+        ci->last_index = last_index;
572a44
 }
572a44
 
572a44
-static int generic_array_get(JournalFile *f,
572a44
-                             uint64_t first,
572a44
-                             uint64_t i,
572a44
-                             Object **ret, uint64_t *offset) {
572a44
+static int generic_array_get(
572a44
+                JournalFile *f,
572a44
+                uint64_t first,
572a44
+                uint64_t i,
572a44
+                Object **ret, uint64_t *offset) {
572a44
 
572a44
         Object *o;
572a44
         uint64_t p = 0, a, t = 0;
572a44
@@ -1448,7 +1452,7 @@ static int generic_array_get(JournalFile *f,
572a44
 
572a44
 found:
572a44
         /* Let's cache this item for the next invocation */
572a44
-        chain_cache_put(f->chain_cache, ci, first, a, o->entry_array.items[0], t);
572a44
+        chain_cache_put(f->chain_cache, ci, first, a, o->entry_array.items[0], t, i);
572a44
 
572a44
         r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
572a44
         if (r < 0)
572a44
@@ -1463,11 +1467,12 @@ found:
572a44
         return 1;
572a44
 }
572a44
 
572a44
-static int generic_array_get_plus_one(JournalFile *f,
572a44
-                                      uint64_t extra,
572a44
-                                      uint64_t first,
572a44
-                                      uint64_t i,
572a44
-                                      Object **ret, uint64_t *offset) {
572a44
+static int generic_array_get_plus_one(
572a44
+                JournalFile *f,
572a44
+                uint64_t extra,
572a44
+                uint64_t first,
572a44
+                uint64_t i,
572a44
+                Object **ret, uint64_t *offset) {
572a44
 
572a44
         Object *o;
572a44
 
572a44
@@ -1498,17 +1503,18 @@ enum {
572a44
         TEST_RIGHT
572a44
 };
572a44
 
572a44
-static int generic_array_bisect(JournalFile *f,
572a44
-                                uint64_t first,
572a44
-                                uint64_t n,
572a44
-                                uint64_t needle,
572a44
-                                int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
572a44
-                                direction_t direction,
572a44
-                                Object **ret,
572a44
-                                uint64_t *offset,
572a44
-                                uint64_t *idx) {
572a44
-
572a44
-        uint64_t a, p, t = 0, i = 0, last_p = 0;
572a44
+static int generic_array_bisect(
572a44
+                JournalFile *f,
572a44
+                uint64_t first,
572a44
+                uint64_t n,
572a44
+                uint64_t needle,
572a44
+                int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
572a44
+                direction_t direction,
572a44
+                Object **ret,
572a44
+                uint64_t *offset,
572a44
+                uint64_t *idx) {
572a44
+
572a44
+        uint64_t a, p, t = 0, i = 0, last_p = 0, last_index = (uint64_t) -1;
572a44
         bool subtract_one = false;
572a44
         Object *o, *array = NULL;
572a44
         int r;
572a44
@@ -1533,7 +1539,7 @@ static int generic_array_bisect(JournalFile *f,
572a44
                         return r;
572a44
 
572a44
                 if (r == TEST_LEFT) {
572a44
-                        /* OK, what we are looking for is right of th
572a44
+                        /* OK, what we are looking for is right of the
572a44
                          * begin of this EntryArray, so let's jump
572a44
                          * straight to previously cached array in the
572a44
                          * chain */
572a44
@@ -1541,6 +1547,7 @@ static int generic_array_bisect(JournalFile *f,
572a44
                         a = ci->array;
572a44
                         n -= ci->total;
572a44
                         t = ci->total;
572a44
+                        last_index = ci->last_index;
572a44
                 }
572a44
         }
572a44
 
572a44
@@ -1571,6 +1578,60 @@ static int generic_array_bisect(JournalFile *f,
572a44
                 if (r == TEST_RIGHT) {
572a44
                         left = 0;
572a44
                         right -= 1;
572a44
+
572a44
+                        if (last_index != (uint64_t) -1) {
572a44
+                                assert(last_index <= right);
572a44
+
572a44
+                                /* If we cached the last index we
572a44
+                                 * looked at, let's try to not to jump
572a44
+                                 * too wildly around and see if we can
572a44
+                                 * limit the range to look at early to
572a44
+                                 * the immediate neighbors of the last
572a44
+                                 * index we looked at. */
572a44
+
572a44
+                                if (last_index > 0) {
572a44
+                                        uint64_t x = last_index - 1;
572a44
+
572a44
+                                        p = le64toh(array->entry_array.items[x]);
572a44
+                                        if (p <= 0)
572a44
+                                                return -EBADMSG;
572a44
+
572a44
+                                        r = test_object(f, p, needle);
572a44
+                                        if (r < 0)
572a44
+                                                return r;
572a44
+
572a44
+                                        if (r == TEST_FOUND)
572a44
+                                                r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
572a44
+
572a44
+                                        if (r == TEST_RIGHT)
572a44
+                                                right = x;
572a44
+                                        else
572a44
+                                                left = x + 1;
572a44
+                                }
572a44
+
572a44
+                                if (last_index < right) {
572a44
+                                        uint64_t y = last_index + 1;
572a44
+
572a44
+                                        p = le64toh(array->entry_array.items[y]);
572a44
+                                        if (p <= 0)
572a44
+                                                return -EBADMSG;
572a44
+
572a44
+                                        r = test_object(f, p, needle);
572a44
+                                        if (r < 0)
572a44
+                                                return r;
572a44
+
572a44
+                                        if (r == TEST_FOUND)
572a44
+                                                r = direction == DIRECTION_DOWN ? TEST_RIGHT : TEST_LEFT;
572a44
+
572a44
+                                        if (r == TEST_RIGHT)
572a44
+                                                right = y;
572a44
+                                        else
572a44
+                                                left = y + 1;
572a44
+                                }
572a44
+
572a44
+                                last_index = (uint64_t) -1;
572a44
+                        }
572a44
+
572a44
                         for (;;) {
572a44
                                 if (left == right) {
572a44
                                         if (direction == DIRECTION_UP)
572a44
@@ -1581,8 +1642,8 @@ static int generic_array_bisect(JournalFile *f,
572a44
                                 }
572a44
 
572a44
                                 assert(left < right);
572a44
-
572a44
                                 i = (left + right) / 2;
572a44
+
572a44
                                 p = le64toh(array->entry_array.items[i]);
572a44
                                 if (p <= 0)
572a44
                                         return -EBADMSG;
572a44
@@ -1615,6 +1676,7 @@ static int generic_array_bisect(JournalFile *f,
572a44
 
572a44
                 n -= k;
572a44
                 t += k;
572a44
+                last_index = (uint64_t) -1;
572a44
                 a = le64toh(array->entry_array.next_entry_array_offset);
572a44
         }
572a44
 
572a44
@@ -1625,7 +1687,7 @@ found:
572a44
                 return 0;
572a44
 
572a44
         /* Let's cache this item for the next invocation */
572a44
-        chain_cache_put(f->chain_cache, ci, first, a, array->entry_array.items[0], t);
572a44
+        chain_cache_put(f->chain_cache, ci, first, a, array->entry_array.items[0], t, i + (subtract_one ? -1 : 0));
572a44
 
572a44
         if (subtract_one && i == 0)
572a44
                 p = last_p;
572a44
@@ -1650,16 +1712,18 @@ found:
572a44
         return 1;
572a44
 }
572a44
 
572a44
-static int generic_array_bisect_plus_one(JournalFile *f,
572a44
-                                         uint64_t extra,
572a44
-                                         uint64_t first,
572a44
-                                         uint64_t n,
572a44
-                                         uint64_t needle,
572a44
-                                         int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
572a44
-                                         direction_t direction,
572a44
-                                         Object **ret,
572a44
-                                         uint64_t *offset,
572a44
-                                         uint64_t *idx) {
572a44
+
572a44
+static int generic_array_bisect_plus_one(
572a44
+                JournalFile *f,
572a44
+                uint64_t extra,
572a44
+                uint64_t first,
572a44
+                uint64_t n,
572a44
+                uint64_t needle,
572a44
+                int (*test_object)(JournalFile *f, uint64_t p, uint64_t needle),
572a44
+                direction_t direction,
572a44
+                Object **ret,
572a44
+                uint64_t *offset,
572a44
+                uint64_t *idx) {
572a44
 
572a44
         int r;
572a44
         bool step_back = false;