diff options
author | Sea-Eun Lee <seaeunlee@microsoft.com> | 2024-11-18 20:13:40 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2025-01-14 14:42:23 +0100 |
commit | 015a3b8cb1d40d420eaa961e27cdee463f57f8a0 (patch) | |
tree | 19bc2336378ef6b6415fd9ed1e73da6bf3856156 | |
parent | core: serialize API bus id and validate before deserializing bus tracks, plus... (diff) | |
download | systemd-015a3b8cb1d40d420eaa961e27cdee463f57f8a0.tar.xz systemd-015a3b8cb1d40d420eaa961e27cdee463f57f8a0.zip |
oomd: support reloading configuration at runtime
-rw-r--r-- | man/systemd-oomd.service.xml | 15 | ||||
-rw-r--r-- | src/oom/meson.build | 5 | ||||
-rw-r--r-- | src/oom/oomd-conf.c | 87 | ||||
-rw-r--r-- | src/oom/oomd-conf.h | 9 | ||||
-rw-r--r-- | src/oom/oomd-manager.c | 45 | ||||
-rw-r--r-- | src/oom/oomd-manager.h | 2 | ||||
-rw-r--r-- | src/oom/oomd.c | 64 | ||||
-rw-r--r-- | src/shared/conf-parser.c | 28 | ||||
-rw-r--r-- | src/shared/conf-parser.h | 1 | ||||
-rwxr-xr-x | test/units/TEST-55-OOMD.sh | 34 | ||||
-rw-r--r-- | units/systemd-oomd.service.in | 2 |
11 files changed, 202 insertions, 90 deletions
diff --git a/man/systemd-oomd.service.xml b/man/systemd-oomd.service.xml index 53a9250983..9d04c9da98 100644 --- a/man/systemd-oomd.service.xml +++ b/man/systemd-oomd.service.xml @@ -128,6 +128,21 @@ </refsect1> <refsect1> + <title>Signals</title> + + <variablelist> + <varlistentry> + <term><constant>SIGHUP</constant></term> + + <listitem><para>Upon reception of the <constant>SIGHUP</constant> process signal + <command>systemd-oomd</command> will reload its configuration file.</para> + + <xi:include href="version-info.xml" xpointer="v258"/></listitem> + </varlistentry> + </variablelist> + </refsect1> + + <refsect1> <title>See Also</title> <para><simplelist type="inline"> <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> diff --git a/src/oom/meson.build b/src/oom/meson.build index 690ed7ac6b..bd4b734e0f 100644 --- a/src/oom/meson.build +++ b/src/oom/meson.build @@ -1,10 +1,11 @@ # SPDX-License-Identifier: LGPL-2.1-or-later systemd_oomd_sources = files( - 'oomd-manager-bus.c', + 'oomd.c', + 'oomd-conf.c', 'oomd-manager.c', + 'oomd-manager-bus.c', 'oomd-util.c', - 'oomd.c', ) executables += [ diff --git a/src/oom/oomd-conf.c b/src/oom/oomd-conf.c new file mode 100644 index 0000000000..1fcac36251 --- /dev/null +++ b/src/oom/oomd-conf.c @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "conf-parser.h" +#include "log.h" +#include "oomd-conf.h" +#include "oomd-manager.h" + +static int config_parse_duration( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + usec_t usec, *duration = ASSERT_PTR(data); + int r; + + if (isempty(rvalue)) { + *duration = DEFAULT_MEM_PRESSURE_DURATION_USEC; + return 0; + } + + r = parse_sec(rvalue, &usec); + if (r < 0) + return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); + + if (usec == 0) { + /* Map zero -> default for backwards compatibility. */ + *duration = DEFAULT_MEM_PRESSURE_DURATION_USEC; + return 0; + } + + if (usec < 1 * USEC_PER_SEC || usec == USEC_INFINITY) + return log_syntax( + unit, + LOG_WARNING, + filename, + line, + 0, + "%s= must be at least 1s and less than infinity, ignoring: %s", + lvalue, + rvalue); + + *duration = usec; + return 0; +} + +void manager_set_defaults(Manager *m) { + int r; + + assert(m); + + m->default_mem_pressure_duration_usec = DEFAULT_MEM_PRESSURE_DURATION_USEC; + + m->swap_used_limit_permyriad = DEFAULT_SWAP_USED_LIMIT_PERCENT * 100; + r = store_loadavg_fixed_point(DEFAULT_MEM_PRESSURE_LIMIT_PERCENT, 0, &m->default_mem_pressure_limit); + if (r < 0) + log_warning_errno(r, "Failed to set default for default_mem_pressure_limit, ignoring: %m"); +} + +void manager_parse_config_file(Manager *m) { + int r; + + assert(m); + + const ConfigTableItem items[] = { + { "OOM", "SwapUsedLimit", config_parse_permyriad, 0, &m->swap_used_limit_permyriad }, + { "OOM", "DefaultMemoryPressureLimit", config_parse_loadavg, 0, &m->default_mem_pressure_limit }, + { "OOM", "DefaultMemoryPressureDurationSec", config_parse_duration, 0, &m->default_mem_pressure_duration_usec }, + {} + }; + + r = config_parse_standard_file_with_dropins( + "systemd/oomd.conf", + "OOM\0", + config_item_table_lookup, + items, + CONFIG_PARSE_WARN, + /* userdata= */ m); + if (r >= 0) + log_debug("Config file successfully parsed."); +} diff --git a/src/oom/oomd-conf.h b/src/oom/oomd-conf.h new file mode 100644 index 0000000000..0283b9e61d --- /dev/null +++ b/src/oom/oomd-conf.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "conf-parser.h" +#include "oomd-manager.h" + +void manager_set_defaults(Manager *m); + +void manager_parse_config_file(Manager *m); diff --git a/src/oom/oomd-manager.c b/src/oom/oomd-manager.c index baa88a2f2a..ce3674365a 100644 --- a/src/oom/oomd-manager.c +++ b/src/oom/oomd-manager.c @@ -4,15 +4,17 @@ #include "sd-json.h" #include "bus-log-control-api.h" -#include "bus-util.h" #include "bus-polkit.h" +#include "bus-util.h" #include "cgroup-util.h" +#include "daemon-util.h" #include "fd-util.h" #include "fileio.h" #include "format-util.h" #include "json-util.h" #include "memory-util.h" #include "memstream-util.h" +#include "oomd-conf.h" #include "oomd-manager-bus.h" #include "oomd-manager.h" #include "path-util.h" @@ -649,6 +651,18 @@ Manager* manager_free(Manager *m) { return mfree(m); } +static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { + Manager *m = ASSERT_PTR(userdata); + + (void) notify_reloading(); + + manager_set_defaults(m); + manager_parse_config_file(m); + + (void) sd_notify(/* unset= */ false, NOTIFY_READY); + return 0; +} + int manager_new(Manager **ret) { _cleanup_(manager_freep) Manager *m = NULL; int r; @@ -659,12 +673,19 @@ int manager_new(Manager **ret) { if (!m) return -ENOMEM; + manager_set_defaults(m); + manager_parse_config_file(m); + r = sd_event_default(&m->event); if (r < 0) return r; (void) sd_event_set_watchdog(m->event, true); + r = sd_event_add_signal(m->event, /* ret_event_source= */ NULL, SIGHUP | SD_EVENT_SIGNAL_PROCMASK, manager_dispatch_reload_signal, m); + if (r < 0) + return r; + r = sd_event_set_signal_exit(m->event, true); if (r < 0) return r; @@ -753,36 +774,14 @@ static int manager_varlink_init(Manager *m, int fd) { int manager_start( Manager *m, bool dry_run, - int swap_used_limit_permyriad, - int mem_pressure_limit_permyriad, - usec_t mem_pressure_usec, int fd) { - unsigned long l, f; int r; assert(m); m->dry_run = dry_run; - m->swap_used_limit_permyriad = swap_used_limit_permyriad >= 0 ? swap_used_limit_permyriad : DEFAULT_SWAP_USED_LIMIT_PERCENT * 100; - assert(m->swap_used_limit_permyriad <= 10000); - - if (mem_pressure_limit_permyriad >= 0) { - assert(mem_pressure_limit_permyriad <= 10000); - - l = mem_pressure_limit_permyriad / 100; - f = mem_pressure_limit_permyriad % 100; - } else { - l = DEFAULT_MEM_PRESSURE_LIMIT_PERCENT; - f = 0; - } - r = store_loadavg_fixed_point(l, f, &m->default_mem_pressure_limit); - if (r < 0) - return r; - - m->default_mem_pressure_duration_usec = mem_pressure_usec; - r = manager_connect_bus(m); if (r < 0) return r; diff --git a/src/oom/oomd-manager.h b/src/oom/oomd-manager.h index 635e16ddfc..a40946ce4e 100644 --- a/src/oom/oomd-manager.h +++ b/src/oom/oomd-manager.h @@ -66,7 +66,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); int manager_new(Manager **ret); -int manager_start(Manager *m, bool dry_run, int swap_used_limit_permyriad, int mem_pressure_limit_permyriad, usec_t mem_pressure_usec, int fd); +int manager_start(Manager *m, bool dry_run, int fd); int manager_get_dump_string(Manager *m, char **ret); diff --git a/src/oom/oomd.c b/src/oom/oomd.c index dd34251cd2..518ddc47c1 100644 --- a/src/oom/oomd.c +++ b/src/oom/oomd.c @@ -11,6 +11,7 @@ #include "fileio.h" #include "log.h" #include "main-func.h" +#include "oomd-conf.h" #include "oomd-manager-bus.h" #include "oomd-manager.h" #include "parse-util.h" @@ -19,62 +20,6 @@ #include "signal-util.h" static bool arg_dry_run = false; -static int arg_swap_used_limit_permyriad = -1; -static int arg_mem_pressure_limit_permyriad = -1; -static usec_t arg_mem_pressure_usec = DEFAULT_MEM_PRESSURE_DURATION_USEC; - -static int config_parse_duration( - const char *unit, - const char *filename, - unsigned line, - const char *section, - unsigned section_line, - const char *lvalue, - int ltype, - const char *rvalue, - void *data, - void *userdata) { - - usec_t usec, *duration = ASSERT_PTR(data); - int r; - - if (isempty(rvalue)) { - *duration = DEFAULT_MEM_PRESSURE_DURATION_USEC; - return 0; - } - - r = parse_sec(rvalue, &usec); - if (r < 0) - return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); - - if (usec == 0) { - /* Map zero -> default for backwards compatibility. */ - *duration = DEFAULT_MEM_PRESSURE_DURATION_USEC; - return 0; - } - - if (usec < 1 * USEC_PER_SEC || usec == USEC_INFINITY) - return log_syntax(unit, LOG_WARNING, filename, line, 0, "%s= must be at least 1s and less than infinity, ignoring: %s", lvalue, rvalue); - - *duration = usec; - return 0; -} - -static int parse_config(void) { - static const ConfigTableItem items[] = { - { "OOM", "SwapUsedLimit", config_parse_permyriad, 0, &arg_swap_used_limit_permyriad }, - { "OOM", "DefaultMemoryPressureLimit", config_parse_permyriad, 0, &arg_mem_pressure_limit_permyriad }, - { "OOM", "DefaultMemoryPressureDurationSec", config_parse_duration, 0, &arg_mem_pressure_usec }, - {} - }; - - return config_parse_standard_file_with_dropins( - "systemd/oomd.conf", - "OOM\0", - config_item_table_lookup, items, - CONFIG_PARSE_WARN, - /* userdata= */ NULL); -} static int help(void) { _cleanup_free_ char *link = NULL; @@ -166,10 +111,6 @@ static int run(int argc, char *argv[]) { if (r <= 0) return r; - r = parse_config(); - if (r < 0) - return r; - /* Do some basic requirement checks for running systemd-oomd. It's not exhaustive as some of the other * requirements do not have a reliable means to check for in code. */ @@ -211,9 +152,6 @@ static int run(int argc, char *argv[]) { r = manager_start( m, arg_dry_run, - arg_swap_used_limit_permyriad, - arg_mem_pressure_limit_permyriad, - arg_mem_pressure_usec, fd); if (r < 0) return log_error_errno(r, "Failed to start up daemon: %m"); diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index eaa8a5f11c..6ba8adf5c2 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -2180,3 +2180,31 @@ int config_parse_ip_protocol( *proto = r; return 1; /* done. */ } + +int config_parse_loadavg( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + loadavg_t *i = ASSERT_PTR(data); + int r; + + assert(rvalue); + + r = parse_permyriad(rvalue); + if (r < 0) + return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); + + r = store_loadavg_fixed_point(r / 100, r % 100, i); + if (r < 0) + return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue); + + return 1; /* done. */ +} diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h index 1599738f84..d882e2d320 100644 --- a/src/shared/conf-parser.h +++ b/src/shared/conf-parser.h @@ -313,6 +313,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_sec_fix_0); CONFIG_PARSER_PROTOTYPE(config_parse_timezone); CONFIG_PARSER_PROTOTYPE(config_parse_calendar); CONFIG_PARSER_PROTOTYPE(config_parse_ip_protocol); +CONFIG_PARSER_PROTOTYPE(config_parse_loadavg); typedef enum Disabled { DISABLED_CONFIGURATION, diff --git a/test/units/TEST-55-OOMD.sh b/test/units/TEST-55-OOMD.sh index 3c9b4391a9..6679337858 100755 --- a/test/units/TEST-55-OOMD.sh +++ b/test/units/TEST-55-OOMD.sh @@ -264,6 +264,40 @@ EOF systemctl daemon-reload } +testcase_reload() { + # Check if the oomd.conf drop-in config is loaded. + assert_in 'Swap Used Limit: 90.00%' "$(oomctl)" + assert_in 'Default Memory Pressure Limit: 60.00%' "$(oomctl)" + assert_in 'Default Memory Pressure Duration: 2s' "$(oomctl)" + + # Test oomd reload + mkdir -p /run/systemd/oomd.conf.d/ + { + echo "[OOM]" + echo "SwapUsedLimit=80%" + echo "DefaultMemoryPressureLimit=55%" + echo "DefaultMemoryPressureDurationSec=5s" + } >/run/systemd/oomd.conf.d/99-oomd-test.conf + + systemctl reload systemd-oomd.service + assert_in 'Swap Used Limit: 80.00%' "$(oomctl)" + assert_in 'Default Memory Pressure Limit: 55.00%' "$(oomctl)" + assert_in 'Default Memory Pressure Duration: 5s' "$(oomctl)" + + # Set back to default via reload + mkdir -p /run/systemd/oomd.conf.d/ + { + echo "[OOM]" + echo "DefaultMemoryPressureDurationSec=2s" + } >/run/systemd/oomd.conf.d/99-oomd-test.conf + + systemctl reload systemd-oomd.service + + assert_in 'Swap Used Limit: 90.00%' "$(oomctl)" + assert_in 'Default Memory Pressure Limit: 60.00%' "$(oomctl)" + assert_in 'Default Memory Pressure Duration: 2s' "$(oomctl)" +} + run_testcases systemd-analyze log-level info diff --git a/units/systemd-oomd.service.in b/units/systemd-oomd.service.in index 6838ddccef..3e9533dac4 100644 --- a/units/systemd-oomd.service.in +++ b/units/systemd-oomd.service.in @@ -53,7 +53,7 @@ RestrictSUIDSGID=yes SystemCallArchitectures=native SystemCallErrorNumber=EPERM SystemCallFilter=@system-service -Type=notify +Type=notify-reload User=systemd-oom {{SERVICE_WATCHDOG}} |