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