xref: /linux/tools/testing/selftests/sched_ext/rt_stall.c (revision 57885276cc16a2e2b76282c808a4e84cbecb3aae)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2025 NVIDIA Corporation.
4  */
5 #define _GNU_SOURCE
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <sched.h>
10 #include <sys/prctl.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <time.h>
14 #include <linux/sched.h>
15 #include <signal.h>
16 #include <bpf/bpf.h>
17 #include <scx/common.h>
18 #include "rt_stall.bpf.skel.h"
19 #include "scx_test.h"
20 #include "../kselftest.h"
21 
22 #define CORE_ID		0	/* CPU to pin tasks to */
23 #define RUN_TIME        5	/* How long to run the test in seconds */
24 
25 /* Signal the parent that setup is complete by writing to a pipe */
26 static void signal_ready(int fd)
27 {
28 	char c = 1;
29 
30 	if (write(fd, &c, 1) != 1) {
31 		perror("write to ready pipe");
32 		exit(EXIT_FAILURE);
33 	}
34 	close(fd);
35 }
36 
37 /* Wait for a child to signal readiness via a pipe */
38 static void wait_ready(int fd)
39 {
40 	char c;
41 
42 	if (read(fd, &c, 1) != 1) {
43 		perror("read from ready pipe");
44 		exit(EXIT_FAILURE);
45 	}
46 	close(fd);
47 }
48 
49 /* Simple busy-wait function for test tasks */
50 static void process_func(void)
51 {
52 	while (1) {
53 		/* Busy wait */
54 		for (volatile unsigned long i = 0; i < 10000000UL; i++)
55 			;
56 	}
57 }
58 
59 /* Set CPU affinity to a specific core */
60 static void set_affinity(int cpu)
61 {
62 	cpu_set_t mask;
63 
64 	CPU_ZERO(&mask);
65 	CPU_SET(cpu, &mask);
66 	if (sched_setaffinity(0, sizeof(mask), &mask) != 0) {
67 		perror("sched_setaffinity");
68 		exit(EXIT_FAILURE);
69 	}
70 }
71 
72 /* Set task scheduling policy and priority */
73 static void set_sched(int policy, int priority)
74 {
75 	struct sched_param param;
76 
77 	param.sched_priority = priority;
78 	if (sched_setscheduler(0, policy, &param) != 0) {
79 		perror("sched_setscheduler");
80 		exit(EXIT_FAILURE);
81 	}
82 }
83 
84 /* Get process runtime from /proc/<pid>/stat */
85 static float get_process_runtime(int pid)
86 {
87 	char path[256];
88 	FILE *file;
89 	long utime, stime;
90 	int fields;
91 
92 	snprintf(path, sizeof(path), "/proc/%d/stat", pid);
93 	file = fopen(path, "r");
94 	if (file == NULL) {
95 		perror("Failed to open stat file");
96 		return -1;
97 	}
98 
99 	/* Skip the first 13 fields and read the 14th and 15th */
100 	fields = fscanf(file,
101 			"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu",
102 			&utime, &stime);
103 	fclose(file);
104 
105 	if (fields != 2) {
106 		fprintf(stderr, "Failed to read stat file\n");
107 		return -1;
108 	}
109 
110 	/* Calculate the total time spent in the process */
111 	long total_time = utime + stime;
112 	long ticks_per_second = sysconf(_SC_CLK_TCK);
113 	float runtime_seconds = total_time * 1.0 / ticks_per_second;
114 
115 	return runtime_seconds;
116 }
117 
118 static enum scx_test_status setup(void **ctx)
119 {
120 	struct rt_stall *skel;
121 
122 	skel = rt_stall__open();
123 	SCX_FAIL_IF(!skel, "Failed to open");
124 	SCX_ENUM_INIT(skel);
125 	SCX_FAIL_IF(rt_stall__load(skel), "Failed to load skel");
126 
127 	*ctx = skel;
128 
129 	return SCX_TEST_PASS;
130 }
131 
132 static bool sched_stress_test(bool is_ext)
133 {
134 	/*
135 	 * We're expecting the EXT task to get around 5% of CPU time when
136 	 * competing with the RT task (small 1% fluctuations are expected).
137 	 *
138 	 * However, the EXT task should get at least 4% of the CPU to prove
139 	 * that the EXT deadline server is working correctly. A percentage
140 	 * less than 4% indicates a bug where RT tasks can potentially
141 	 * stall SCHED_EXT tasks, causing the test to fail.
142 	 */
143 	const float expected_min_ratio = 0.04; /* 4% */
144 	const char *class_str = is_ext ? "EXT" : "FAIR";
145 
146 	float ext_runtime, rt_runtime, actual_ratio;
147 	int ext_pid, rt_pid;
148 	int ext_ready[2], rt_ready[2];
149 
150 	ksft_print_header();
151 	ksft_set_plan(1);
152 
153 	if (pipe(ext_ready) || pipe(rt_ready)) {
154 		perror("pipe");
155 		ksft_exit_fail();
156 	}
157 
158 	/* Create and set up a EXT task */
159 	ext_pid = fork();
160 	if (ext_pid == 0) {
161 		close(ext_ready[0]);
162 		close(rt_ready[0]);
163 		close(rt_ready[1]);
164 		set_affinity(CORE_ID);
165 		signal_ready(ext_ready[1]);
166 		process_func();
167 		exit(0);
168 	} else if (ext_pid < 0) {
169 		perror("fork task");
170 		ksft_exit_fail();
171 	}
172 
173 	/* Create an RT task */
174 	rt_pid = fork();
175 	if (rt_pid == 0) {
176 		close(ext_ready[0]);
177 		close(ext_ready[1]);
178 		close(rt_ready[0]);
179 		set_affinity(CORE_ID);
180 		set_sched(SCHED_FIFO, 50);
181 		signal_ready(rt_ready[1]);
182 		process_func();
183 		exit(0);
184 	} else if (rt_pid < 0) {
185 		perror("fork for RT task");
186 		ksft_exit_fail();
187 	}
188 
189 	/*
190 	 * Wait for both children to complete their setup (affinity and
191 	 * scheduling policy) before starting the measurement window.
192 	 * This prevents flaky failures caused by the RT child's setup
193 	 * time eating into the measurement period.
194 	 */
195 	close(ext_ready[1]);
196 	close(rt_ready[1]);
197 	wait_ready(ext_ready[0]);
198 	wait_ready(rt_ready[0]);
199 
200 	/* Let the processes run for the specified time */
201 	sleep(RUN_TIME);
202 
203 	/* Get runtime for the EXT task */
204 	ext_runtime = get_process_runtime(ext_pid);
205 	if (ext_runtime == -1)
206 		ksft_exit_fail_msg("Error getting runtime for %s task (PID %d)\n",
207 				   class_str, ext_pid);
208 	ksft_print_msg("Runtime of %s task (PID %d) is %f seconds\n",
209 		       class_str, ext_pid, ext_runtime);
210 
211 	/* Get runtime for the RT task */
212 	rt_runtime = get_process_runtime(rt_pid);
213 	if (rt_runtime == -1)
214 		ksft_exit_fail_msg("Error getting runtime for RT task (PID %d)\n", rt_pid);
215 	ksft_print_msg("Runtime of RT task (PID %d) is %f seconds\n", rt_pid, rt_runtime);
216 
217 	/* Kill the processes */
218 	kill(ext_pid, SIGKILL);
219 	kill(rt_pid, SIGKILL);
220 	waitpid(ext_pid, NULL, 0);
221 	waitpid(rt_pid, NULL, 0);
222 
223 	/* Verify that the scx task got enough runtime */
224 	actual_ratio = ext_runtime / (ext_runtime + rt_runtime);
225 	ksft_print_msg("%s task got %.2f%% of total runtime\n",
226 		       class_str, actual_ratio * 100);
227 
228 	if (actual_ratio >= expected_min_ratio) {
229 		ksft_test_result_pass("PASS: %s task got more than %.2f%% of runtime\n",
230 				      class_str, expected_min_ratio * 100);
231 		return true;
232 	}
233 	ksft_test_result_fail("FAIL: %s task got less than %.2f%% of runtime\n",
234 			      class_str, expected_min_ratio * 100);
235 	return false;
236 }
237 
238 static enum scx_test_status run(void *ctx)
239 {
240 	struct rt_stall *skel = ctx;
241 	struct bpf_link *link = NULL;
242 	bool res;
243 	int i;
244 
245 	/*
246 	 * Test if the dl_server is working both with and without the
247 	 * sched_ext scheduler attached.
248 	 *
249 	 * This ensures all the scenarios are covered:
250 	 *   - fair_server stop -> ext_server start
251 	 *   - ext_server stop -> fair_server stop
252 	 */
253 	for (i = 0; i < 4; i++) {
254 		bool is_ext = i % 2;
255 
256 		if (is_ext) {
257 			memset(&skel->data->uei, 0, sizeof(skel->data->uei));
258 			link = bpf_map__attach_struct_ops(skel->maps.rt_stall_ops);
259 			SCX_FAIL_IF(!link, "Failed to attach scheduler");
260 		}
261 		res = sched_stress_test(is_ext);
262 		if (is_ext) {
263 			SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE));
264 			bpf_link__destroy(link);
265 		}
266 
267 		if (!res)
268 			ksft_exit_fail();
269 	}
270 
271 	return SCX_TEST_PASS;
272 }
273 
274 static void cleanup(void *ctx)
275 {
276 	struct rt_stall *skel = ctx;
277 
278 	rt_stall__destroy(skel);
279 }
280 
281 struct scx_test rt_stall = {
282 	.name = "rt_stall",
283 	.description = "Verify that RT tasks cannot stall SCHED_EXT tasks",
284 	.setup = setup,
285 	.run = run,
286 	.cleanup = cleanup,
287 };
288 REGISTER_SCX_TEST(&rt_stall)
289