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