Debian Codegraph @

src/test/test-sched-prio.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2012 Holger Hans Peter Freyther
4 ***/
5
6 #include <sched.h>
7
8 #include "all-units.h"
9 #include "manager.h"
10 #include "rm-rf.h"
11 #include "tests.h"
12
13 int main(int argc, char *argv[]) {
14 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
15 _cleanup_(manager_freep) Manager *m = NULL;
16 Unit *idle_ok, *idle_bad, *rr_ok, *rr_bad, *rr_sched, *ext_ok;
17 Service *ser;
18 int r;
19
20 test_setup_logging(LOG_INFO);
21
22 r = enter_cgroup_subroot(NULL);
23 if (r == -ENOMEDIUM)
24 return log_tests_skipped("cgroupfs not available");
25
26 /* prepare the test */
27 _cleanup_free_ char *unit_dir = NULL;
28 ASSERT_OK(get_testdata_dir("test-sched-prio", &unit_dir));
29 ASSERT_OK(setenv_unit_path(unit_dir));
30 assert_se(runtime_dir = setup_fake_runtime_dir());
31
32 r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &m);
33 if (manager_errno_skip_test(r))
34 return log_tests_skipped_errno(r, "manager_new");
35 assert_se(r >= 0);
36 assert_se(manager_startup(m, NULL, NULL, NULL, NULL) >= 0);
37
38 /* load idle ok */
39 assert_se(manager_load_startable_unit_or_warn(m, "sched_idle_ok.service", NULL, &idle_ok) >= 0);
40 ser = SERVICE(idle_ok);
41 assert_se(ser->exec_context.cpu_sched_policy == SCHED_OTHER);
42 assert_se(ser->exec_context.cpu_sched_priority == 0);
43
44 /*
45 * load idle bad. This should print a warning but we have no way to look at it.
46 */
47 assert_se(manager_load_startable_unit_or_warn(m, "sched_idle_bad.service", NULL, &idle_bad) >= 0);
48 ser = SERVICE(idle_ok);
49 assert_se(ser->exec_context.cpu_sched_policy == SCHED_OTHER);
50 assert_se(ser->exec_context.cpu_sched_priority == 0);
51
52 /*
53 * load rr ok.
54 * Test that the default priority is moving from 0 to 1.
55 */
56 assert_se(manager_load_startable_unit_or_warn(m, "sched_rr_ok.service", NULL, &rr_ok) >= 0);
57 ser = SERVICE(rr_ok);
58 assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
59 assert_se(ser->exec_context.cpu_sched_priority == 1);
60
61 /*
62 * load rr bad.
63 * Test that the value of 0 and 100 is ignored.
64 */
65 assert_se(manager_load_startable_unit_or_warn(m, "sched_rr_bad.service", NULL, &rr_bad) >= 0);
66 ser = SERVICE(rr_bad);
67 assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
68 assert_se(ser->exec_context.cpu_sched_priority == 1);
69
70 /*
71 * load rr change.
72 * Test that anything between 1 and 99 can be set.
73 */
74 assert_se(manager_load_startable_unit_or_warn(m, "sched_rr_change.service", NULL, &rr_sched) >= 0);
75 ser = SERVICE(rr_sched);
76 assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
77 assert_se(ser->exec_context.cpu_sched_priority == 99);
78
79 /* load ext ok */
80 assert_se(manager_load_startable_unit_or_warn(m, "sched_ext_ok.service", NULL, &ext_ok) >= 0);
81 ser = SERVICE(ext_ok);
82 assert_se(ser->exec_context.cpu_sched_policy == SCHED_EXT);
83 assert_se(ser->exec_context.cpu_sched_priority == 0);
84
85 return EXIT_SUCCESS;
86 }