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