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, ¶m) != 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 if (!__COMPAT_struct_has_field("rq", "ext_server")) { 123 fprintf(stderr, "SKIP: ext DL server not supported\n"); 124 return SCX_TEST_SKIP; 125 } 126 127 skel = rt_stall__open(); 128 SCX_FAIL_IF(!skel, "Failed to open"); 129 SCX_ENUM_INIT(skel); 130 SCX_FAIL_IF(rt_stall__load(skel), "Failed to load skel"); 131 132 *ctx = skel; 133 134 return SCX_TEST_PASS; 135 } 136 137 static bool sched_stress_test(bool is_ext) 138 { 139 /* 140 * We're expecting the EXT task to get around 5% of CPU time when 141 * competing with the RT task (small 1% fluctuations are expected). 142 * 143 * However, the EXT task should get at least 4% of the CPU to prove 144 * that the EXT deadline server is working correctly. A percentage 145 * less than 4% indicates a bug where RT tasks can potentially 146 * stall SCHED_EXT tasks, causing the test to fail. 147 */ 148 const float expected_min_ratio = 0.04; /* 4% */ 149 const char *class_str = is_ext ? "EXT" : "FAIR"; 150 151 float ext_runtime, rt_runtime, actual_ratio; 152 int ext_pid, rt_pid; 153 int ext_ready[2], rt_ready[2]; 154 155 ksft_print_header(); 156 ksft_set_plan(1); 157 158 if (pipe(ext_ready) || pipe(rt_ready)) { 159 perror("pipe"); 160 ksft_exit_fail(); 161 } 162 163 /* Create and set up a EXT task */ 164 ext_pid = fork(); 165 if (ext_pid == 0) { 166 close(ext_ready[0]); 167 close(rt_ready[0]); 168 close(rt_ready[1]); 169 set_affinity(CORE_ID); 170 signal_ready(ext_ready[1]); 171 process_func(); 172 exit(0); 173 } else if (ext_pid < 0) { 174 perror("fork task"); 175 ksft_exit_fail(); 176 } 177 178 /* Create an RT task */ 179 rt_pid = fork(); 180 if (rt_pid == 0) { 181 close(ext_ready[0]); 182 close(ext_ready[1]); 183 close(rt_ready[0]); 184 set_affinity(CORE_ID); 185 set_sched(SCHED_FIFO, 50); 186 signal_ready(rt_ready[1]); 187 process_func(); 188 exit(0); 189 } else if (rt_pid < 0) { 190 perror("fork for RT task"); 191 ksft_exit_fail(); 192 } 193 194 /* 195 * Wait for both children to complete their setup (affinity and 196 * scheduling policy) before starting the measurement window. 197 * This prevents flaky failures caused by the RT child's setup 198 * time eating into the measurement period. 199 */ 200 close(ext_ready[1]); 201 close(rt_ready[1]); 202 wait_ready(ext_ready[0]); 203 wait_ready(rt_ready[0]); 204 205 /* Let the processes run for the specified time */ 206 sleep(RUN_TIME); 207 208 /* Get runtime for the EXT task */ 209 ext_runtime = get_process_runtime(ext_pid); 210 if (ext_runtime == -1) 211 ksft_exit_fail_msg("Error getting runtime for %s task (PID %d)\n", 212 class_str, ext_pid); 213 ksft_print_msg("Runtime of %s task (PID %d) is %f seconds\n", 214 class_str, ext_pid, ext_runtime); 215 216 /* Get runtime for the RT task */ 217 rt_runtime = get_process_runtime(rt_pid); 218 if (rt_runtime == -1) 219 ksft_exit_fail_msg("Error getting runtime for RT task (PID %d)\n", rt_pid); 220 ksft_print_msg("Runtime of RT task (PID %d) is %f seconds\n", rt_pid, rt_runtime); 221 222 /* Kill the processes */ 223 kill(ext_pid, SIGKILL); 224 kill(rt_pid, SIGKILL); 225 waitpid(ext_pid, NULL, 0); 226 waitpid(rt_pid, NULL, 0); 227 228 /* Verify that the scx task got enough runtime */ 229 actual_ratio = ext_runtime / (ext_runtime + rt_runtime); 230 ksft_print_msg("%s task got %.2f%% of total runtime\n", 231 class_str, actual_ratio * 100); 232 233 if (actual_ratio >= expected_min_ratio) { 234 ksft_test_result_pass("PASS: %s task got more than %.2f%% of runtime\n", 235 class_str, expected_min_ratio * 100); 236 return true; 237 } 238 ksft_test_result_fail("FAIL: %s task got less than %.2f%% of runtime\n", 239 class_str, expected_min_ratio * 100); 240 return false; 241 } 242 243 static enum scx_test_status run(void *ctx) 244 { 245 struct rt_stall *skel = ctx; 246 struct bpf_link *link = NULL; 247 bool res; 248 int i; 249 250 /* 251 * Test if the dl_server is working both with and without the 252 * sched_ext scheduler attached. 253 * 254 * This ensures all the scenarios are covered: 255 * - fair_server stop -> ext_server start 256 * - ext_server stop -> fair_server stop 257 */ 258 for (i = 0; i < 4; i++) { 259 bool is_ext = i % 2; 260 261 if (is_ext) { 262 memset(&skel->data->uei, 0, sizeof(skel->data->uei)); 263 link = bpf_map__attach_struct_ops(skel->maps.rt_stall_ops); 264 SCX_FAIL_IF(!link, "Failed to attach scheduler"); 265 } 266 res = sched_stress_test(is_ext); 267 if (is_ext) { 268 SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE)); 269 bpf_link__destroy(link); 270 } 271 272 if (!res) 273 ksft_exit_fail(); 274 } 275 276 return SCX_TEST_PASS; 277 } 278 279 static void cleanup(void *ctx) 280 { 281 struct rt_stall *skel = ctx; 282 283 rt_stall__destroy(skel); 284 } 285 286 struct scx_test rt_stall = { 287 .name = "rt_stall", 288 .description = "Verify that RT tasks cannot stall SCHED_EXT tasks", 289 .setup = setup, 290 .run = run, 291 .cleanup = cleanup, 292 }; 293 REGISTER_SCX_TEST(&rt_stall) 294