1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-09 01:18:19 +03:00

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.
This commit is contained in:
Lennart Poettering 2023-10-06 19:04:00 +02:00 committed by Yu Watanabe
parent 328539c21c
commit a81577961c
5 changed files with 46 additions and 42 deletions

View 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;

View 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);

View 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;

View 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,

View 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, ...) \