Debian Codegraph @

tests/linux_kernel/helpers/test_sched.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import contextlib
5 import os
6 from pathlib import Path
7 import resource
8 import signal
9 from threading import Condition, Thread
10 import time
11 import unittest
12
13 from drgn import container_of
14 from drgn.helpers.linux.cgroup import cgroup_get_from_path
15 from drgn.helpers.linux.cpumask import for_each_possible_cpu
16 from drgn.helpers.linux.pid import find_task
17 from drgn.helpers.linux.sched import (
18 cfs_rq_for_each_entity,
19 cpu_curr,
20 cpu_rq,
21 get_task_state,
22 idle_task,
23 loadavg,
24 rq_for_each_fair_task,
25 rq_for_each_rt_task,
26 sched_entity_is_task,
27 sched_entity_to_task,
28 task_cpu,
29 task_group_name,
30 task_on_cpu,
31 task_since_last_arrival_ns,
32 task_state_to_char,
33 task_thread_info,
34 thread_group_leader,
35 )
36 from tests.linux_kernel import (
37 LinuxKernelTestCase,
38 fork_and_stop,
39 proc_state,
40 skip_unless_have_test_kmod,
41 wait_until,
42 )
43 from tests.linux_kernel.helpers.test_cgroup import tmp_cgroup
44
45
46 @contextlib.contextmanager
47 def tmp_cgroup_with_cpu_controller():
48 with tmp_cgroup() as cgroup_dir:
49 if "cpu" not in (cgroup_dir / "cgroup.controllers").read_text().split():
50 raise unittest.SkipTest("cgroup CPU controller not enabled")
51 yield cgroup_dir
52
53
54 class TestSched(LinuxKernelTestCase):
55 @skip_unless_have_test_kmod
56 def test_task_thread_info(self):
57 self.assertEqual(
58 task_thread_info(self.prog["drgn_test_kthread"]),
59 self.prog["drgn_test_kthread_info"],
60 )
61
62 def test_task_cpu(self):
63 cpu = os.cpu_count() - 1
64 with fork_and_stop(os.sched_setaffinity, 0, (cpu,)) as (pid, _):
65 self.assertEqual(task_cpu(find_task(self.prog, pid)), cpu)
66
67 def test_task_state_to_char(self):
68 task = find_task(self.prog, os.getpid())
69 self.assertEqual(task_state_to_char(task), "R")
70 self.assertEqual(get_task_state(task), "R (running)")
71
72 pid = os.fork()
73 try:
74 if pid == 0:
75 try:
76 while True:
77 signal.pause()
78 finally:
79 os._exit(1)
80
81 task = find_task(self.prog, pid)
82
83 wait_until(lambda: proc_state(pid) == "S")
84 self.assertEqual(task_state_to_char(task), "S")
85 self.assertEqual(get_task_state(task), "S (sleeping)")
86
87 os.kill(pid, signal.SIGSTOP)
88 wait_until(lambda: proc_state(pid) == "T")
89 self.assertEqual(task_state_to_char(task), "T")
90
91 os.kill(pid, signal.SIGKILL)
92 wait_until(lambda: proc_state(pid) == "Z")
93 self.assertEqual(task_state_to_char(task), "Z")
94 finally:
95 os.kill(pid, signal.SIGKILL)
96 os.waitpid(pid, 0)
97
98 def test_task_on_cpu(self):
99 task = find_task(self.prog, os.getpid())
100 self.assertTrue(task_on_cpu(task))
101
102 with fork_and_stop() as pid:
103 task = find_task(self.prog, pid)
104 self.assertFalse(task_on_cpu(task))
105
106 def test_cpu_curr(self):
107 task = find_task(self.prog, os.getpid())
108 cpu = os.cpu_count() - 1
109 old_affinity = os.sched_getaffinity(0)
110 os.sched_setaffinity(0, (cpu,))
111 try:
112 self.assertEqual(cpu_curr(self.prog, cpu), task)
113 finally:
114 os.sched_setaffinity(0, old_affinity)
115
116 def test_idle_task(self):
117 for cpu in for_each_possible_cpu(self.prog):
118 task = idle_task(self.prog, cpu)
119 if cpu == 0:
120 self.assertEqual(task, self.prog["init_task"].address_of_())
121 else:
122 self.assertEqual(task.comm.string_(), f"swapper/{cpu}".encode())
123
124 def test_loadavg(self):
125 values = loadavg(self.prog)
126 self.assertEqual(len(values), 3)
127 self.assertTrue(all(v >= 0.0 for v in values))
128
129 def test_task_since_last_arrival_ns(self):
130 with fork_and_stop() as pid:
131 time.sleep(0.01)
132 # Forcing the process to migrate also forces the rq clock to update
133 # so we can get a reliable reading.
134 affinity = os.sched_getaffinity(pid)
135 if len(affinity) > 1:
136 other_affinity = {affinity.pop()}
137 os.sched_setaffinity(pid, affinity)
138 os.sched_setaffinity(pid, other_affinity)
139 task = find_task(self.prog, pid)
140 self.assertGreaterEqual(task_since_last_arrival_ns(task), 10000000)
141
142 def test_rq_for_each_fair_task(self):
143 old_affinity = os.sched_getaffinity(0)
144 cpu = max(old_affinity)
145 os.sched_setaffinity(0, (cpu,))
146 self.addCleanup(os.sched_setaffinity, 0, old_affinity)
147
148 rq = cpu_rq(self.prog, cpu)
149 pids = [task.pid.value_() for task in rq_for_each_fair_task(rq)]
150 self.assertIn(os.getpid(), pids)
151
152 def test_rq_for_each_rt_task(self):
153 old_affinity = os.sched_getaffinity(0)
154 cpu = max(old_affinity)
155 os.sched_setaffinity(0, (cpu,))
156 self.addCleanup(os.sched_setaffinity, 0, old_affinity)
157
158 old_rlimit = resource.getrlimit(resource.RLIMIT_RTTIME)
159 resource.setrlimit(
160 resource.RLIMIT_RTTIME, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)
161 )
162 self.addCleanup(resource.setrlimit, resource.RLIMIT_RTTIME, old_rlimit)
163
164 old_scheduler = os.sched_getscheduler(0)
165 old_param = os.sched_getparam(0)
166 os.sched_setscheduler(0, os.SCHED_RR, os.sched_param(1))
167 self.addCleanup(os.sched_setscheduler, 0, old_scheduler, old_param)
168
169 rq = cpu_rq(self.prog, cpu)
170 pids = [task.pid.value_() for task in rq_for_each_rt_task(rq)]
171 self.assertIn(os.getpid(), pids)
172
173 def test_sched_entity_is_task(self):
174 task = find_task(self.prog, os.getpid())
175 self.assertTrue(sched_entity_is_task(task.se.address_of_()))
176
177 def test_sched_entity_is_task_false(self):
178 with tmp_cgroup_with_cpu_controller() as cgroup_dir:
179 cgrp = cgroup_get_from_path(self.prog, cgroup_dir.name)
180 css = cgrp.subsys[self.prog["cpu_cgrp_id"]]
181 task_group = container_of(css, "struct task_group", "css")
182 cpu = min(os.sched_getaffinity(0))
183 se = task_group.se[cpu].read_()
184 self.assertFalse(sched_entity_is_task(se))
185
186 def test_sched_entity_to_task(self):
187 task = find_task(self.prog, os.getpid())
188 self.assertEqual(sched_entity_to_task(task.se.address_of_()), task)
189
190 def test_task_group_name(self):
191 with tmp_cgroup_with_cpu_controller() as cgroup_dir:
192 cgrp = cgroup_get_from_path(self.prog, cgroup_dir.name)
193 css = cgrp.subsys[self.prog["cpu_cgrp_id"]]
194 task_group = container_of(css, "struct task_group", "css")
195 self.assertEqual(task_group_name(task_group), cgroup_dir.name.encode())
196
197 def test_cfs_rq_for_each_entity(self):
198 old_affinity = os.sched_getaffinity(0)
199 cpu = max(old_affinity)
200 self.addCleanup(os.sched_setaffinity, 0, old_affinity)
201 os.sched_setaffinity(0, (cpu,))
202
203 with tmp_cgroup_with_cpu_controller() as cgroup_dir:
204 old_cgroup = (
205 Path("/proc/self/cgroup").read_text().strip().partition("::")[2]
206 )
207 old_cgroup_procs = (
208 cgroup_dir.parent / old_cgroup.lstrip("/") / "cgroup.procs"
209 )
210 (cgroup_dir / "cgroup.procs").write_text(str(os.getpid()))
211 try:
212 cgrp = cgroup_get_from_path(self.prog, cgroup_dir.name)
213 css = cgrp.subsys[self.prog["cpu_cgrp_id"]]
214 task_group = container_of(css, "struct task_group", "css")
215 task_group_se = task_group.se[cpu].read_()
216
217 task = find_task(self.prog, os.getpid())
218 task_se = task.se.address_of_()
219
220 entities = list(
221 cfs_rq_for_each_entity(cpu_rq(self.prog, cpu).cfs.address_of_())
222 )
223 self.assertIn((task_group_se, 0, True, False), entities)
224 self.assertIn((task_se, 1, True, True), entities)
225 finally:
226 old_cgroup_procs.write_text(str(os.getpid()))
227
228 def test_thread_group_leader(self):
229 condition = Condition()
230
231 def thread_func():
232 with condition:
233 condition.wait()
234
235 thread = Thread(target=thread_func)
236 try:
237 thread.start()
238
239 self.assertTrue(thread_group_leader(find_task(self.prog, os.getpid())))
240 self.assertFalse(
241 thread_group_leader(find_task(self.prog, thread.native_id))
242 )
243 finally:
244 with condition:
245 condition.notify_all()
246 thread.join()