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