dryang / rpms / systemd

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