]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: make journal_file_read_tail_timestamp() notify to the caller that some...
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 2 Jan 2024 19:28:03 +0000 (04:28 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 11 Feb 2024 16:27:50 +0000 (01:27 +0900)
Tiny optimization for journal_file_find_newest_for_boot_id().

src/libsystemd/sd-journal/sd-journal.c

index d7136d250ecb002796eeba8f6b6cc5cd14b4737e..f082f3f6f9b4146f80aee4a14ed66c837f7e4ce1 100644 (file)
@@ -449,6 +449,11 @@ static int journal_file_find_newest_for_boot_id(
                 r = journal_file_read_tail_timestamp(j, f);
                 if (r < 0)
                         return log_debug_errno(r, "Failed to read tail timestamp while trying to find newest journal file for boot ID %s.", SD_ID128_TO_STRING(id));
+                if (r == 0) {
+                        /* No new entry found. */
+                        *ret = f;
+                        return 0;
+                }
 
                 /* Refreshing the timestamp we read might have reshuffled the prioq, hence let's check the
                  * prioq again and only use the information once we reached an equilibrium or hit a limit */
@@ -2490,6 +2495,17 @@ static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f) {
         if (mo > rt) /* monotonic clock is further ahead than realtime? that's weird, refuse to use the data */
                 return -ENODATA;
 
+        if (offset == f->newest_entry_offset) {
+                /* Cached data and the current one should be equivalent. */
+                if (!sd_id128_equal(f->newest_machine_id, f->header->machine_id) ||
+                    !sd_id128_equal(f->newest_boot_id, id) ||
+                    f->newest_monotonic_usec != mo ||
+                    f->newest_realtime_usec != rt)
+                        return -EBADMSG;
+
+                return 0; /* No new entry is added after we read last time. */
+        }
+
         if (!sd_id128_equal(f->newest_boot_id, id))
                 journal_file_unlink_newest_by_boot_id(j, f);
 
@@ -2504,7 +2520,7 @@ static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f) {
         if (r < 0)
                 return r;
 
-        return 0;
+        return 1; /* Updated. */
 }
 
 _public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {