journald: hook up journald with the memory pressure and SIGRTMIN+18 logic

This commit is contained in:
Lennart Poettering
2023-03-01 09:43:23 +01:00
parent bb3517181b
commit 375c34d8cd
4 changed files with 56 additions and 2 deletions
+5 -1
View 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);
+1
View 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;
+48 -1
View 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();
+2
View 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;