daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
1c4d83
From 0fc51b569570e8bf5aecd5ee03a88eb668b7b385 Mon Sep 17 00:00:00 2001
1c4d83
From: Anita Zhang <the.anitazha@gmail.com>
1c4d83
Date: Tue, 14 Sep 2021 16:33:10 -0700
1c4d83
Subject: [PATCH] fileio: start with 4k buffer for procfs
1c4d83
1c4d83
There's a very gradual increase of anonymous memory in systemd-journald that
1c4d83
blames to 2ac67221bb6270f0fbe7cbd0076653832cd49de2.
1c4d83
1c4d83
systemd-journald makes many calls to read /proc/PID/cmdline and
1c4d83
/proc/PID/status, both of which tend to be well under 4K. However the
1c4d83
combination of allocating 4M read buffers, then using `realloc()` to
1c4d83
shrink the buffer in `read_virtual_file()` appears to be creating
1c4d83
fragmentation in the heap (when combined with the other allocations
1c4d83
systemd-journald is doing).
1c4d83
1c4d83
To help mitigate this, try reading /proc with a 4K buffer as
1c4d83
`read_virtual_file()` did before 2ac67221bb6270f0fbe7cbd0076653832cd49de2.
1c4d83
If it isn't big enough then try again with the larger buffers.
1c4d83
---
1c4d83
 src/basic/fileio.c | 5 +++++
1c4d83
 1 file changed, 5 insertions(+)
1c4d83
1c4d83
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
1c4d83
index 99a44fdea2..466c6321c7 100644
1c4d83
--- a/src/basic/fileio.c
1c4d83
+++ b/src/basic/fileio.c
1c4d83
@@ -431,6 +431,11 @@ int read_virtual_file(const char *filename, size_t max_size, char **ret_contents
1c4d83
                         }
1c4d83
 
1c4d83
                         n_retries--;
1c4d83
+                } else if (n_retries > 1) {
1c4d83
+                        /* Files in /proc are generally smaller than the page size so let's start with a page size
1c4d83
+                         * buffer from malloc and only use the max buffer on the final try. */
1c4d83
+                        size = MIN3(page_size() - 1, READ_VIRTUAL_BYTES_MAX, max_size);
1c4d83
+                        n_retries = 1;
1c4d83
                 } else {
1c4d83
                         size = MIN(READ_VIRTUAL_BYTES_MAX, max_size);
1c4d83
                         n_retries = 0;
1c4d83
-- 
1c4d83
2.31.1
1c4d83