]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journald: hook up journald with the memory pressure and SIGRTMIN+18 logic
authorLennart Poettering <lennart@poettering.net>
Tue, 14 Feb 2023 17:49:50 +0000 (18:49 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 1 Mar 2023 08:43:23 +0000 (09:43 +0100)
src/journal/journald-context.c
src/journal/journald-context.h
src/journal/journald-server.c
src/journal/journald-server.h

index 5f0c638d4747e3bf1fbcf897e50bab878182a3ee..9a09d544533b0058240fe31e173c34c99c032909 100644 (file)
@@ -636,6 +636,10 @@ static void client_context_try_shrink_to(Server *s, size_t limit) {
         }
 }
 
+void client_context_flush_regular(Server *s) {
+        client_context_try_shrink_to(s, 0);
+}
+
 void client_context_flush_all(Server *s) {
         assert(s);
 
@@ -644,7 +648,7 @@ void client_context_flush_all(Server *s) {
         s->my_context = client_context_release(s, s->my_context);
         s->pid1_context = client_context_release(s, s->pid1_context);
 
-        client_context_try_shrink_to(s, 0);
+        client_context_flush_regular(s);
 
         assert(prioq_size(s->client_contexts_lru) == 0);
         assert(hashmap_size(s->client_contexts) == 0);
index 4a998ba42e3f5de7f4907f9932b9e746b31ba673..6e0d9f6f75dd7600c5df95ac56af489d3752de72 100644 (file)
@@ -89,6 +89,7 @@ void client_context_maybe_refresh(
 
 void client_context_acquire_default(Server *s);
 void client_context_flush_all(Server *s);
+void client_context_flush_regular(Server *s);
 
 static inline size_t client_context_extra_fields_n_iovec(const ClientContext *c) {
         return c ? c->extra_fields_n_iovec : 0;
index 23942f957506c0acc94fc0402aefdda1696e6358..3e3b6d44d67e6fa933d26d027c1c304e8710f928 100644 (file)
@@ -1707,7 +1707,7 @@ static int server_setup_signals(Server *s) {
 
         assert(s);
 
-        assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0);
+        assert_se(sigprocmask_many(SIG_SETMASK, NULL, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, SIGRTMIN+1, SIGRTMIN+18, -1) >= 0);
 
         r = sd_event_add_signal(s->event, &s->sigusr1_event_source, SIGUSR1, dispatch_sigusr1, s);
         if (r < 0)
@@ -1747,6 +1747,10 @@ static int server_setup_signals(Server *s) {
         if (r < 0)
                 return r;
 
+        r = sd_event_add_signal(s->event, NULL, SIGRTMIN+18, sigrtmin18_handler, &s->sigrtmin18_info);
+        if (r < 0)
+                return r;
+
         return 0;
 }
 
@@ -2420,6 +2424,42 @@ static int server_set_namespace(Server *s, const char *namespace) {
         return 1;
 }
 
+static int server_memory_pressure(sd_event_source *es, void *userdata) {
+        Server *s = ASSERT_PTR(userdata);
+
+        log_info("Under memory pressure, flushing caches.");
+
+        /* Flushed the cached info we might have about client processes */
+        client_context_flush_regular(s);
+
+        /* Let's also close all user files (but keep the system/runtime one open) */
+        for (;;) {
+                ManagedJournalFile *first = ordered_hashmap_steal_first(s->user_journals);
+
+                if (!first)
+                        break;
+
+                (void) managed_journal_file_close(first);
+        }
+
+        sd_event_trim_memory();
+
+        return 0;
+}
+
+static int server_setup_memory_pressure(Server *s) {
+        int r;
+
+        assert(s);
+
+        r = sd_event_add_memory_pressure(s->event, NULL, server_memory_pressure, s);
+        if (r < 0)
+                log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || (r == -EHOSTDOWN) ? LOG_DEBUG : LOG_NOTICE, r,
+                               "Failed to install memory pressure event source, ignoring: %m");
+
+        return 0;
+}
+
 int server_init(Server *s, const char *namespace) {
         const char *native_socket, *syslog_socket, *stdout_socket, *varlink_socket, *e;
         _cleanup_fdset_free_ FDSet *fds = NULL;
@@ -2470,6 +2510,9 @@ int server_init(Server *s, const char *namespace) {
                         .interval = DEFAULT_KMSG_OWN_INTERVAL,
                         .burst = DEFAULT_KMSG_OWN_BURST,
                 },
+
+                .sigrtmin18_info.memory_pressure_handler = server_memory_pressure,
+                .sigrtmin18_info.memory_pressure_userdata = s,
         };
 
         r = server_set_namespace(s, namespace);
@@ -2652,6 +2695,10 @@ int server_init(Server *s, const char *namespace) {
         if (r < 0)
                 return r;
 
+        r = server_setup_memory_pressure(s);
+        if (r < 0)
+                return r;
+
         s->ratelimit = journal_ratelimit_new();
         if (!s->ratelimit)
                 return log_oom();
index e7bf750a59c9f517d9d6499ad1fe2d483ad33188..e03cfc04c9407c67ba689523220d1fa6cd05d829 100644 (file)
@@ -8,6 +8,7 @@
 
 typedef struct Server Server;
 
+#include "common-signal.h"
 #include "conf-parser.h"
 #include "hashmap.h"
 #include "journald-context.h"
@@ -95,6 +96,7 @@ struct Server {
         sd_event_source *notify_event_source;
         sd_event_source *watchdog_event_source;
         sd_event_source *idle_event_source;
+        struct sigrtmin18_info sigrtmin18_info;
 
         ManagedJournalFile *runtime_journal;
         ManagedJournalFile *system_journal;