Blame SOURCES/0059-journal-make-skipping-of-exhausted-journal-files-eff.patch

17b0f1
From 0ad01db952718d3437fa8f077a065d17efe5279b Mon Sep 17 00:00:00 2001
17b0f1
From: Michal Schmidt <mschmidt@redhat.com>
17b0f1
Date: Tue, 24 Feb 2015 19:45:17 +0100
17b0f1
Subject: [PATCH] journal: make skipping of exhausted journal files effective
17b0f1
 again
17b0f1
17b0f1
Commit 668c965af "journal: skipping of exhausted journal files is bad if
17b0f1
direction changed" fixed a correctness issue, but it also significantly
17b0f1
limited the cases where the optimization that skips exhausted journal
17b0f1
files could apply.
17b0f1
As a result, some journalctl queries are much slower in v219 than in v218.
17b0f1
(e.g. queries where a "--since" cutoff should have quickly eliminated
17b0f1
older journal files from consideration, but didn't.)
17b0f1
17b0f1
If already in the initial iteration find_location_with_matches() finds
17b0f1
no entry, the journal file's location is not updated. This is fine,
17b0f1
except that:
17b0f1
 - We must update at least f->last_direction. The optimization relies on
17b0f1
   it. Let's separate that from journal_file_save_location() and update
17b0f1
   it immediately after the direction checks.
17b0f1
 - The optimization was conditional on "f->current_offset > 0", but it
17b0f1
   would always be 0 in this scenario. This check is unnecessary for the
17b0f1
   optimization.
17b0f1
17b0f1
(cherry picked from commit 950c07d421c04e5aae99973479f4f13131fb45e1)
17b0f1
---
17b0f1
 src/journal/journal-file.c |  3 +--
17b0f1
 src/journal/journal-file.h |  2 +-
17b0f1
 src/journal/sd-journal.c   | 24 +++++++++++++++---------
17b0f1
 3 files changed, 17 insertions(+), 12 deletions(-)
17b0f1
17b0f1
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
17b0f1
index 0f28718b0e..24c49b916a 100644
17b0f1
--- a/src/journal/journal-file.c
17b0f1
+++ b/src/journal/journal-file.c
17b0f1
@@ -2014,8 +2014,7 @@ void journal_file_reset_location(JournalFile *f) {
17b0f1
         f->current_xor_hash = 0;
17b0f1
 }
17b0f1
 
17b0f1
-void journal_file_save_location(JournalFile *f, direction_t direction, Object *o, uint64_t offset) {
17b0f1
-        f->last_direction = direction;
17b0f1
+void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset) {
17b0f1
         f->location_type = LOCATION_SEEK;
17b0f1
         f->current_offset = offset;
17b0f1
         f->current_seqnum = le64toh(o->entry.seqnum);
17b0f1
diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h
17b0f1
index 2526e14d65..403c8f760c 100644
17b0f1
--- a/src/journal/journal-file.h
17b0f1
+++ b/src/journal/journal-file.h
17b0f1
@@ -199,7 +199,7 @@ int journal_file_find_field_object(JournalFile *f, const void *field, uint64_t s
17b0f1
 int journal_file_find_field_object_with_hash(JournalFile *f, const void *field, uint64_t size, uint64_t hash, Object **ret, uint64_t *offset);
17b0f1
 
17b0f1
 void journal_file_reset_location(JournalFile *f);
17b0f1
-void journal_file_save_location(JournalFile *f, direction_t direction, Object *o, uint64_t offset);
17b0f1
+void journal_file_save_location(JournalFile *f, Object *o, uint64_t offset);
17b0f1
 int journal_file_compare_locations(JournalFile *af, JournalFile *bf);
17b0f1
 int journal_file_next_entry(JournalFile *f, uint64_t p, direction_t direction, Object **ret, uint64_t *offset);
17b0f1
 
17b0f1
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
17b0f1
index 94891cdf35..9b57e5945d 100644
17b0f1
--- a/src/journal/sd-journal.c
17b0f1
+++ b/src/journal/sd-journal.c
17b0f1
@@ -723,13 +723,17 @@ static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direc
17b0f1
         assert(j);
17b0f1
         assert(f);
17b0f1
 
17b0f1
-        if (f->last_direction == direction && f->current_offset > 0) {
17b0f1
-                /* If we hit EOF before, recheck if any new entries arrived. */
17b0f1
-                n_entries = le64toh(f->header->n_entries);
17b0f1
-                if (f->location_type == LOCATION_TAIL && n_entries == f->last_n_entries)
17b0f1
-                        return 0;
17b0f1
-                f->last_n_entries = n_entries;
17b0f1
+        n_entries = le64toh(f->header->n_entries);
17b0f1
+
17b0f1
+        /* If we hit EOF before, we don't need to look into this file again
17b0f1
+         * unless direction changed or new entries appeared. */
17b0f1
+        if (f->last_direction == direction && f->location_type == LOCATION_TAIL &&
17b0f1
+            n_entries == f->last_n_entries)
17b0f1
+                return 0;
17b0f1
 
17b0f1
+        f->last_n_entries = n_entries;
17b0f1
+
17b0f1
+        if (f->last_direction == direction && f->current_offset > 0) {
17b0f1
                 /* LOCATION_SEEK here means we did the work in a previous
17b0f1
                  * iteration and the current location already points to a
17b0f1
                  * candidate entry. */
17b0f1
@@ -738,14 +742,16 @@ static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direc
17b0f1
                         if (r <= 0)
17b0f1
                                 return r;
17b0f1
 
17b0f1
-                        journal_file_save_location(f, direction, c, cp);
17b0f1
+                        journal_file_save_location(f, c, cp);
17b0f1
                 }
17b0f1
         } else {
17b0f1
+                f->last_direction = direction;
17b0f1
+
17b0f1
                 r = find_location_with_matches(j, f, direction, &c, &cp;;
17b0f1
                 if (r <= 0)
17b0f1
                         return r;
17b0f1
 
17b0f1
-                journal_file_save_location(f, direction, c, cp);
17b0f1
+                journal_file_save_location(f, c, cp);
17b0f1
         }
17b0f1
 
17b0f1
         /* OK, we found the spot, now let's advance until an entry
17b0f1
@@ -773,7 +779,7 @@ static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direc
17b0f1
                 if (r <= 0)
17b0f1
                         return r;
17b0f1
 
17b0f1
-                journal_file_save_location(f, direction, c, cp);
17b0f1
+                journal_file_save_location(f, c, cp);
17b0f1
         }
17b0f1
 }
17b0f1