]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-sched-prio.c
804cee34e961f514899a56f46e0191766ca574a1
[thirdparty/systemd.git] / src / test / test-sched-prio.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Holger Hans Peter Freyther
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <sched.h>
22
23 #include "macro.h"
24 #include "manager.h"
25 #include "rm-rf.h"
26 #include "test-helper.h"
27 #include "tests.h"
28
29 int main(int argc, char *argv[]) {
30 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
31 Manager *m = NULL;
32 Unit *idle_ok, *idle_bad, *rr_ok, *rr_bad, *rr_sched;
33 Service *ser;
34 FILE *serial = NULL;
35 FDSet *fdset = NULL;
36 int r;
37
38 r = enter_cgroup_subroot();
39 if (r == -ENOMEDIUM) {
40 log_notice_errno(r, "Skipping test: cgroupfs not available");
41 return EXIT_TEST_SKIP;
42 }
43
44 /* prepare the test */
45 assert_se(set_unit_path(get_testdata_dir("")) >= 0);
46 assert_se(runtime_dir = setup_fake_runtime_dir());
47 r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
48 if (MANAGER_SKIP_TEST(r)) {
49 log_notice_errno(r, "Skipping test: manager_new: %m");
50 return EXIT_TEST_SKIP;
51 }
52 assert_se(r >= 0);
53 assert_se(manager_startup(m, serial, fdset) >= 0);
54
55 /* load idle ok */
56 assert_se(manager_load_unit(m, "sched_idle_ok.service", NULL, NULL, &idle_ok) >= 0);
57 assert_se(idle_ok->load_state == UNIT_LOADED);
58 ser = SERVICE(idle_ok);
59 assert_se(ser->exec_context.cpu_sched_policy == SCHED_OTHER);
60 assert_se(ser->exec_context.cpu_sched_priority == 0);
61
62 /*
63 * load idle bad. This should print a warning but we have no way to look at it.
64 */
65 assert_se(manager_load_unit(m, "sched_idle_bad.service", NULL, NULL, &idle_bad) >= 0);
66 assert_se(idle_bad->load_state == UNIT_LOADED);
67 ser = SERVICE(idle_ok);
68 assert_se(ser->exec_context.cpu_sched_policy == SCHED_OTHER);
69 assert_se(ser->exec_context.cpu_sched_priority == 0);
70
71 /*
72 * load rr ok.
73 * Test that the default priority is moving from 0 to 1.
74 */
75 assert_se(manager_load_unit(m, "sched_rr_ok.service", NULL, NULL, &rr_ok) >= 0);
76 assert_se(rr_ok->load_state == UNIT_LOADED);
77 ser = SERVICE(rr_ok);
78 assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
79 assert_se(ser->exec_context.cpu_sched_priority == 1);
80
81 /*
82 * load rr bad.
83 * Test that the value of 0 and 100 is ignored.
84 */
85 assert_se(manager_load_unit(m, "sched_rr_bad.service", NULL, NULL, &rr_bad) >= 0);
86 assert_se(rr_bad->load_state == UNIT_LOADED);
87 ser = SERVICE(rr_bad);
88 assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
89 assert_se(ser->exec_context.cpu_sched_priority == 1);
90
91 /*
92 * load rr change.
93 * Test that anything between 1 and 99 can be set.
94 */
95 assert_se(manager_load_unit(m, "sched_rr_change.service", NULL, NULL, &rr_sched) >= 0);
96 assert_se(rr_sched->load_state == UNIT_LOADED);
97 ser = SERVICE(rr_sched);
98 assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
99 assert_se(ser->exec_context.cpu_sched_priority == 99);
100
101 manager_free(m);
102
103 return EXIT_SUCCESS;
104 }