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