From c43ef2e8836a497cedc3a06e6f0aebe484bf688a Mon Sep 17 00:00:00 2001 From: Ryan Wilson Date: Wed, 16 Oct 2024 10:40:30 -0700 Subject: [PATCH] oomd: Refactor DefaultMemoryPressureDurationSec= to use conf parser Parsing DefaultMemoryPressureDurationSec= is currently split between conf parser, main() and manager_start() methods. This commit centralizes parsing and bounds checking logic within a single custom conf parser function. --- src/oom/oomd-manager.c | 2 +- src/oom/oomd.c | 44 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/oom/oomd-manager.c b/src/oom/oomd-manager.c index 33e9f288b41..6d1b4f024b2 100644 --- a/src/oom/oomd-manager.c +++ b/src/oom/oomd-manager.c @@ -770,7 +770,7 @@ int manager_start( if (r < 0) return r; - m->default_mem_pressure_duration_usec = mem_pressure_usec ?: DEFAULT_MEM_PRESSURE_DURATION_USEC; + m->default_mem_pressure_duration_usec = mem_pressure_usec; r = manager_connect_bus(m); if (r < 0) diff --git a/src/oom/oomd.c b/src/oom/oomd.c index 336549aff7d..dd34251cd24 100644 --- a/src/oom/oomd.c +++ b/src/oom/oomd.c @@ -21,13 +21,50 @@ 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 = 0; +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_sec, 0, &arg_mem_pressure_usec }, + { "OOM", "DefaultMemoryPressureDurationSec", config_parse_duration, 0, &arg_mem_pressure_usec }, {} }; @@ -167,9 +204,6 @@ static int run(int argc, char *argv[]) { if (!FLAGS_SET(mask, CGROUP_MASK_MEMORY)) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Requires the cgroup memory controller."); - if (arg_mem_pressure_usec > 0 && arg_mem_pressure_usec < 1 * USEC_PER_SEC) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "DefaultMemoryPressureDurationSec= must be 0 or at least 1s"); - r = manager_new(&m); if (r < 0) return log_error_errno(r, "Failed to create manager: %m"); -- 2.47.3