]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: refactor compare_job_priority()
authorLennart Poettering <lennart@poettering.net>
Fri, 6 Oct 2023 17:04:00 +0000 (19:04 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 7 Oct 2023 13:22:00 +0000 (22:22 +0900)
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
src/core/cgroup.h
src/core/manager.c
src/core/unit.c
src/core/unit.h

index b6c1a7265e92ea1687f1c16a605230aabc98eeac..47bab04f4210681ec8be87614046cfd06ca28620 100644 (file)
@@ -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;
index b189ed7485ba2563d0d46a5435b6f16fa7b7e78b..072c92f0117980f9306a146c7324a83571e03aa9 100644 (file)
@@ -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);
 
index 3068f633d7518fb1ef198cacb3ff4480f5ea5e0b..9af6fd4184b1992145935b8965277383235cb71e 100644 (file)
@@ -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;
index 952c3cbecdcce0f7943398c80bfc50cf84564ee9..5623e949217cad770730c93989984803b9af3102 100644 (file)
@@ -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,
index a95aec345fb2c96a1376b5416f549b74cfe9749c..434981755af1dac1a95808f5d4bf18e10a89098c 100644 (file)
@@ -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, ...)             \