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