From a81577961ca3fa90a054f2ccbfc9343f8545b3dc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 6 Oct 2023 19:04:00 +0200 Subject: [PATCH] core: refactor compare_job_priority() Let's move it out of cgroup.[ch]. The function primarily compares the priority values for units, hence let's move the core of it into a new function unit_compare_priority() in unit.[ch], and then make compare_job_priority() a local wrapper for it in manager.[ch] Shorten the code a bit while we are at it. --- src/core/cgroup.c | 43 +++---------------------------------------- src/core/cgroup.h | 5 +++-- src/core/manager.c | 6 ++++++ src/core/unit.c | 32 ++++++++++++++++++++++++++++++++ src/core/unit.h | 2 ++ 5 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/core/cgroup.c b/src/core/cgroup.c index b6c1a7265e..47bab04f42 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -1025,7 +1025,9 @@ static bool cgroup_context_has_allowed_mems(CGroupContext *c) { return c->cpuset_mems.set || c->startup_cpuset_mems.set; } -static uint64_t cgroup_context_cpu_weight(CGroupContext *c, ManagerState state) { +uint64_t cgroup_context_cpu_weight(CGroupContext *c, ManagerState state) { + assert(c); + if (IN_SET(state, MANAGER_STARTING, MANAGER_INITIALIZING, MANAGER_STOPPING) && c->startup_cpu_weight != CGROUP_WEIGHT_INVALID) return c->startup_cpu_weight; @@ -4353,45 +4355,6 @@ void manager_invalidate_startup_units(Manager *m) { unit_invalidate_cgroup(u, CGROUP_MASK_CPU|CGROUP_MASK_IO|CGROUP_MASK_BLKIO|CGROUP_MASK_CPUSET); } -static int unit_get_nice(Unit *u) { - ExecContext *ec; - - ec = unit_get_exec_context(u); - return ec ? ec->nice : 0; -} - -static uint64_t unit_get_cpu_weight(Unit *u) { - ManagerState state = manager_state(u->manager); - CGroupContext *cc; - - cc = unit_get_cgroup_context(u); - return cc ? cgroup_context_cpu_weight(cc, state) : CGROUP_WEIGHT_DEFAULT; -} - -int compare_job_priority(const void *a, const void *b) { - const Job *x = a, *y = b; - int nice_x, nice_y; - uint64_t weight_x, weight_y; - int ret; - - if ((ret = CMP(x->unit->type, y->unit->type)) != 0) - return -ret; - - weight_x = unit_get_cpu_weight(x->unit); - weight_y = unit_get_cpu_weight(y->unit); - - if ((ret = CMP(weight_x, weight_y)) != 0) - return -ret; - - nice_x = unit_get_nice(x->unit); - nice_y = unit_get_nice(y->unit); - - if ((ret = CMP(nice_x, nice_y)) != 0) - return ret; - - return strcmp(x->unit->id, y->unit->id); -} - int unit_cgroup_freezer_action(Unit *u, FreezerAction action) { _cleanup_free_ char *path = NULL; FreezerState target, kernel = _FREEZER_STATE_INVALID; diff --git a/src/core/cgroup.h b/src/core/cgroup.h index b189ed7485..072c92f011 100644 --- a/src/core/cgroup.h +++ b/src/core/cgroup.h @@ -251,6 +251,9 @@ typedef enum CGroupIOAccountingMetric { typedef struct Unit Unit; typedef struct Manager Manager; +typedef enum ManagerState ManagerState; + +uint64_t cgroup_context_cpu_weight(CGroupContext *c, ManagerState state); usec_t cgroup_cpu_adjust_period(usec_t period, usec_t quota, usec_t resolution, usec_t max_period); @@ -372,8 +375,6 @@ void unit_cgroup_catchup(Unit *u); bool unit_cgroup_delegate(Unit *u); -int compare_job_priority(const void *a, const void *b); - int unit_get_cpuset(Unit *u, CPUSet *cpus, const char *name); int unit_cgroup_freezer_action(Unit *u, FreezerAction action); diff --git a/src/core/manager.c b/src/core/manager.c index 3068f633d7..9af6fd4184 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -857,6 +857,12 @@ void manager_set_switching_root(Manager *m, bool switching_root) { m->switching_root = MANAGER_IS_SYSTEM(m) && switching_root; } +static int compare_job_priority(const void *a, const void *b) { + const Job *x = a, *y = b; + + return unit_compare_priority(x->unit, y->unit); +} + int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags, Manager **_m) { _cleanup_(manager_freep) Manager *m = NULL; int r; diff --git a/src/core/unit.c b/src/core/unit.c index 952c3cbecd..5623e94921 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -6359,6 +6359,38 @@ int unit_arm_timer( return 0; } +static int unit_get_nice(Unit *u) { + ExecContext *ec; + + ec = unit_get_exec_context(u); + return ec ? ec->nice : 0; +} + +static uint64_t unit_get_cpu_weight(Unit *u) { + CGroupContext *cc; + + cc = unit_get_cgroup_context(u); + return cc ? cgroup_context_cpu_weight(cc, manager_state(u->manager)) : CGROUP_WEIGHT_DEFAULT; +} + +int unit_compare_priority(Unit *a, Unit *b) { + int ret; + + ret = CMP(a->type, b->type); + if (ret != 0) + return -ret; + + ret = CMP(unit_get_cpu_weight(a), unit_get_cpu_weight(b)); + if (ret != 0) + return -ret; + + ret = CMP(unit_get_nice(a), unit_get_nice(b)); + if (ret != 0) + return ret; + + return strcmp(a->id, b->id); +} + const ActivationDetailsVTable * const activation_details_vtable[_UNIT_TYPE_MAX] = { [UNIT_PATH] = &activation_details_path_vtable, [UNIT_TIMER] = &activation_details_timer_vtable, diff --git a/src/core/unit.h b/src/core/unit.h index a95aec345f..434981755a 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -1093,6 +1093,8 @@ Condition *unit_find_failed_condition(Unit *u); int unit_arm_timer(Unit *u, sd_event_source **source, bool relative, usec_t usec, sd_event_time_handler_t handler); +int unit_compare_priority(Unit *a, Unit *b); + /* Macros which append UNIT= or USER_UNIT= to the message */ #define log_unit_full_errno_zerook(unit, level, error, ...) \ -- 2.25.1