ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
65878a
From eb38fb593b201a97620555c0aca83ff487a50748 Mon Sep 17 00:00:00 2001
65878a
From: Lennart Poettering <lennart@poettering.net>
65878a
Date: Tue, 26 Nov 2013 18:39:42 +0100
65878a
Subject: [PATCH] journal: when appending to journal file, allocate larger
65878a
 blocks at once
65878a
65878a
Conflicts:
65878a
	src/journal/journal-file.c
65878a
---
65878a
 src/journal/journal-file.c | 51 +++++++++++++++++++++++-----------------------
65878a
 1 file changed, 26 insertions(+), 25 deletions(-)
65878a
65878a
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
65878a
index 81c344f..4062a83 100644
65878a
--- a/src/journal/journal-file.c
65878a
+++ b/src/journal/journal-file.c
65878a
@@ -68,6 +68,9 @@
65878a
 /* How many entries to keep in the entry array chain cache at max */
65878a
 #define CHAIN_CACHE_MAX 20
65878a
 
65878a
+/* How much to increase the journal file size at once each time we allocate something new. */
65878a
+#define FILE_SIZE_INCREASE (8ULL*1024ULL*1024ULL)              /* 8MB */
65878a
+
65878a
 int journal_file_set_online(JournalFile *f) {
65878a
         assert(f);
65878a
 
65878a
@@ -218,8 +221,7 @@ static int journal_file_refresh_header(JournalFile *f) {
65878a
         journal_file_set_online(f);
65878a
 
65878a
         /* Sync the online state to disk */
65878a
-        msync(f->header, PAGE_ALIGN(sizeof(Header)), MS_SYNC);
65878a
-        fdatasync(f->fd);
65878a
+        fsync(f->fd);
65878a
 
65878a
         return 0;
65878a
 }
65878a
@@ -313,7 +315,7 @@ static int journal_file_verify_header(JournalFile *f) {
65878a
 }
65878a
 
65878a
 static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size) {
65878a
-        uint64_t old_size, new_size;
65878a
+        uint64_t old_size, new_size, file_size;
65878a
         int r;
65878a
 
65878a
         assert(f);
65878a
@@ -333,12 +335,10 @@ static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size)
65878a
         if (new_size <= old_size)
65878a
                 return 0;
65878a
 
65878a
-        if (f->metrics.max_size > 0 &&
65878a
-            new_size > f->metrics.max_size)
65878a
+        if (f->metrics.max_size > 0 && new_size > f->metrics.max_size)
65878a
                 return -E2BIG;
65878a
 
65878a
-        if (new_size > f->metrics.min_size &&
65878a
-            f->metrics.keep_free > 0) {
65878a
+        if (new_size > f->metrics.min_size && f->metrics.keep_free > 0) {
65878a
                 struct statvfs svfs;
65878a
 
65878a
                 if (fstatvfs(f->fd, &svfs) >= 0) {
65878a
@@ -363,8 +363,16 @@ static int journal_file_allocate(JournalFile *f, uint64_t offset, uint64_t size)
65878a
         if (r != 0)
65878a
                 return -r;
65878a
 
65878a
-        if (fstat(f->fd, &f->last_stat) < 0)
65878a
-                return -errno;
65878a
+        /* Increase the file size a bit further than this, so that we
65878a
+         * we can create larger memory maps to cache */
65878a
+        file_size = ((new_size+FILE_SIZE_INCREASE-1) / FILE_SIZE_INCREASE) * FILE_SIZE_INCREASE;
65878a
+        if (file_size > (uint64_t) f->last_stat.st_size) {
65878a
+                if (file_size > new_size)
65878a
+                        ftruncate(f->fd, file_size);
65878a
+
65878a
+                if (fstat(f->fd, &f->last_stat) < 0)
65878a
+                        return -errno;
65878a
+        }
65878a
 
65878a
         f->header->arena_size = htole64(new_size - le64toh(f->header->header_size));
65878a
 
65878a
@@ -1344,7 +1352,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
65878a
 
65878a
         /* Order by the position on disk, in order to improve seek
65878a
          * times for rotating media. */
65878a
-        qsort(items, n_iovec, sizeof(EntryItem), entry_item_cmp);
65878a
+        qsort_safe(items, n_iovec, sizeof(EntryItem), entry_item_cmp);
65878a
 
65878a
         r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
65878a
 
65878a
@@ -2551,7 +2559,7 @@ fail:
65878a
 }
65878a
 
65878a
 int journal_file_rotate(JournalFile **f, bool compress, bool seal) {
65878a
-        char *p;
65878a
+        _cleanup_free_ char *p = NULL;
65878a
         size_t l;
65878a
         JournalFile *old_file, *new_file = NULL;
65878a
         int r;
65878a
@@ -2568,22 +2576,15 @@ int journal_file_rotate(JournalFile **f, bool compress, bool seal) {
65878a
                 return -EINVAL;
65878a
 
65878a
         l = strlen(old_file->path);
65878a
-
65878a
-        p = new(char, l + 1 + 32 + 1 + 16 + 1 + 16 + 1);
65878a
-        if (!p)
65878a
+        r = asprintf(&p, "%.*s@" SD_ID128_FORMAT_STR "-%016"PRIx64"-%016"PRIx64".journal",
65878a
+                     (int) l - 8, old_file->path,
65878a
+                     SD_ID128_FORMAT_VAL(old_file->header->seqnum_id),
65878a
+                     le64toh((*f)->header->head_entry_seqnum),
65878a
+                     le64toh((*f)->header->head_entry_realtime));
65878a
+        if (r < 0)
65878a
                 return -ENOMEM;
65878a
 
65878a
-        memcpy(p, old_file->path, l - 8);
65878a
-        p[l-8] = '@';
65878a
-        sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1);
65878a
-        snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1,
65878a
-                 "-%016"PRIx64"-%016"PRIx64".journal",
65878a
-                 le64toh((*f)->header->head_entry_seqnum),
65878a
-                 le64toh((*f)->header->head_entry_realtime));
65878a
-
65878a
         r = rename(old_file->path, p);
65878a
-        free(p);
65878a
-
65878a
         if (r < 0)
65878a
                 return -errno;
65878a
 
65878a
@@ -2634,7 +2635,7 @@ int journal_file_open_reliably(
65878a
 
65878a
         l = strlen(fname);
65878a
         if (asprintf(&p, "%.*s@%016llx-%016llx.journal~",
65878a
-                     (int) (l-8), fname,
65878a
+                     (int) l - 8, fname,
65878a
                      (unsigned long long) now(CLOCK_REALTIME),
65878a
                      random_ull()) < 0)
65878a
                 return -ENOMEM;