|
| 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 | +/* Simple busy-wait function for test tasks */ |
| 27 | +static void process_func(void) |
| 28 | +{ |
| 29 | + while (1) { |
| 30 | + /* Busy wait */ |
| 31 | + for (volatile unsigned long i = 0; i < 10000000UL; i++) |
| 32 | + ; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/* Set CPU affinity to a specific core */ |
| 37 | +static void set_affinity(int cpu) |
| 38 | +{ |
| 39 | + cpu_set_t mask; |
| 40 | + |
| 41 | + CPU_ZERO(&mask); |
| 42 | + CPU_SET(cpu, &mask); |
| 43 | + if (sched_setaffinity(0, sizeof(mask), &mask) != 0) { |
| 44 | + perror("sched_setaffinity"); |
| 45 | + exit(EXIT_FAILURE); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/* Set task scheduling policy and priority */ |
| 50 | +static void set_sched(int policy, int priority) |
| 51 | +{ |
| 52 | + struct sched_param param; |
| 53 | + |
| 54 | + param.sched_priority = priority; |
| 55 | + if (sched_setscheduler(0, policy, ¶m) != 0) { |
| 56 | + perror("sched_setscheduler"); |
| 57 | + exit(EXIT_FAILURE); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/* Get process runtime from /proc/<pid>/stat */ |
| 62 | +static float get_process_runtime(int pid) |
| 63 | +{ |
| 64 | + char path[256]; |
| 65 | + FILE *file; |
| 66 | + long utime, stime; |
| 67 | + int fields; |
| 68 | + |
| 69 | + snprintf(path, sizeof(path), "/proc/%d/stat", pid); |
| 70 | + file = fopen(path, "r"); |
| 71 | + if (file == NULL) { |
| 72 | + perror("Failed to open stat file"); |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + /* Skip the first 13 fields and read the 14th and 15th */ |
| 77 | + fields = fscanf(file, |
| 78 | + "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu", |
| 79 | + &utime, &stime); |
| 80 | + fclose(file); |
| 81 | + |
| 82 | + if (fields != 2) { |
| 83 | + fprintf(stderr, "Failed to read stat file\n"); |
| 84 | + return -1; |
| 85 | + } |
| 86 | + |
| 87 | + /* Calculate the total time spent in the process */ |
| 88 | + long total_time = utime + stime; |
| 89 | + long ticks_per_second = sysconf(_SC_CLK_TCK); |
| 90 | + float runtime_seconds = total_time * 1.0 / ticks_per_second; |
| 91 | + |
| 92 | + return runtime_seconds; |
| 93 | +} |
| 94 | + |
| 95 | +static enum scx_test_status setup(void **ctx) |
| 96 | +{ |
| 97 | + struct rt_stall *skel; |
| 98 | + |
| 99 | + skel = rt_stall__open(); |
| 100 | + SCX_FAIL_IF(!skel, "Failed to open"); |
| 101 | + SCX_ENUM_INIT(skel); |
| 102 | + SCX_FAIL_IF(rt_stall__load(skel), "Failed to load skel"); |
| 103 | + |
| 104 | + *ctx = skel; |
| 105 | + |
| 106 | + return SCX_TEST_PASS; |
| 107 | +} |
| 108 | + |
| 109 | +static bool sched_stress_test(bool is_ext) |
| 110 | +{ |
| 111 | + /* |
| 112 | + * We're expecting the EXT task to get around 5% of CPU time when |
| 113 | + * competing with the RT task (small 1% fluctuations are expected). |
| 114 | + * |
| 115 | + * However, the EXT task should get at least 4% of the CPU to prove |
| 116 | + * that the EXT deadline server is working correctly. A percentage |
| 117 | + * less than 4% indicates a bug where RT tasks can potentially |
| 118 | + * stall SCHED_EXT tasks, causing the test to fail. |
| 119 | + */ |
| 120 | + const float expected_min_ratio = 0.04; /* 4% */ |
| 121 | + const char *class_str = is_ext ? "EXT" : "FAIR"; |
| 122 | + |
| 123 | + float ext_runtime, rt_runtime, actual_ratio; |
| 124 | + int ext_pid, rt_pid; |
| 125 | + |
| 126 | + ksft_print_header(); |
| 127 | + ksft_set_plan(1); |
| 128 | + |
| 129 | + /* Create and set up a EXT task */ |
| 130 | + ext_pid = fork(); |
| 131 | + if (ext_pid == 0) { |
| 132 | + set_affinity(CORE_ID); |
| 133 | + process_func(); |
| 134 | + exit(0); |
| 135 | + } else if (ext_pid < 0) { |
| 136 | + perror("fork task"); |
| 137 | + ksft_exit_fail(); |
| 138 | + } |
| 139 | + |
| 140 | + /* Create an RT task */ |
| 141 | + rt_pid = fork(); |
| 142 | + if (rt_pid == 0) { |
| 143 | + set_affinity(CORE_ID); |
| 144 | + set_sched(SCHED_FIFO, 50); |
| 145 | + process_func(); |
| 146 | + exit(0); |
| 147 | + } else if (rt_pid < 0) { |
| 148 | + perror("fork for RT task"); |
| 149 | + ksft_exit_fail(); |
| 150 | + } |
| 151 | + |
| 152 | + /* Let the processes run for the specified time */ |
| 153 | + sleep(RUN_TIME); |
| 154 | + |
| 155 | + /* Get runtime for the EXT task */ |
| 156 | + ext_runtime = get_process_runtime(ext_pid); |
| 157 | + if (ext_runtime == -1) |
| 158 | + ksft_exit_fail_msg("Error getting runtime for %s task (PID %d)\n", |
| 159 | + class_str, ext_pid); |
| 160 | + ksft_print_msg("Runtime of %s task (PID %d) is %f seconds\n", |
| 161 | + class_str, ext_pid, ext_runtime); |
| 162 | + |
| 163 | + /* Get runtime for the RT task */ |
| 164 | + rt_runtime = get_process_runtime(rt_pid); |
| 165 | + if (rt_runtime == -1) |
| 166 | + ksft_exit_fail_msg("Error getting runtime for RT task (PID %d)\n", rt_pid); |
| 167 | + ksft_print_msg("Runtime of RT task (PID %d) is %f seconds\n", rt_pid, rt_runtime); |
| 168 | + |
| 169 | + /* Kill the processes */ |
| 170 | + kill(ext_pid, SIGKILL); |
| 171 | + kill(rt_pid, SIGKILL); |
| 172 | + waitpid(ext_pid, NULL, 0); |
| 173 | + waitpid(rt_pid, NULL, 0); |
| 174 | + |
| 175 | + /* Verify that the scx task got enough runtime */ |
| 176 | + actual_ratio = ext_runtime / (ext_runtime + rt_runtime); |
| 177 | + ksft_print_msg("%s task got %.2f%% of total runtime\n", |
| 178 | + class_str, actual_ratio * 100); |
| 179 | + |
| 180 | + if (actual_ratio >= expected_min_ratio) { |
| 181 | + ksft_test_result_pass("PASS: %s task got more than %.2f%% of runtime\n", |
| 182 | + class_str, expected_min_ratio * 100); |
| 183 | + return true; |
| 184 | + } |
| 185 | + ksft_test_result_fail("FAIL: %s task got less than %.2f%% of runtime\n", |
| 186 | + class_str, expected_min_ratio * 100); |
| 187 | + return false; |
| 188 | +} |
| 189 | + |
| 190 | +static enum scx_test_status run(void *ctx) |
| 191 | +{ |
| 192 | + struct rt_stall *skel = ctx; |
| 193 | + struct bpf_link *link = NULL; |
| 194 | + bool res; |
| 195 | + int i; |
| 196 | + |
| 197 | + /* |
| 198 | + * Test if the dl_server is working both with and without the |
| 199 | + * sched_ext scheduler attached. |
| 200 | + * |
| 201 | + * This ensures all the scenarios are covered: |
| 202 | + * - fair_server stop -> ext_server start |
| 203 | + * - ext_server stop -> fair_server stop |
| 204 | + */ |
| 205 | + for (i = 0; i < 4; i++) { |
| 206 | + bool is_ext = i % 2; |
| 207 | + |
| 208 | + if (is_ext) { |
| 209 | + memset(&skel->data->uei, 0, sizeof(skel->data->uei)); |
| 210 | + link = bpf_map__attach_struct_ops(skel->maps.rt_stall_ops); |
| 211 | + SCX_FAIL_IF(!link, "Failed to attach scheduler"); |
| 212 | + } |
| 213 | + res = sched_stress_test(is_ext); |
| 214 | + if (is_ext) { |
| 215 | + SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE)); |
| 216 | + bpf_link__destroy(link); |
| 217 | + } |
| 218 | + |
| 219 | + if (!res) |
| 220 | + ksft_exit_fail(); |
| 221 | + } |
| 222 | + |
| 223 | + return SCX_TEST_PASS; |
| 224 | +} |
| 225 | + |
| 226 | +static void cleanup(void *ctx) |
| 227 | +{ |
| 228 | + struct rt_stall *skel = ctx; |
| 229 | + |
| 230 | + rt_stall__destroy(skel); |
| 231 | +} |
| 232 | + |
| 233 | +struct scx_test rt_stall = { |
| 234 | + .name = "rt_stall", |
| 235 | + .description = "Verify that RT tasks cannot stall SCHED_EXT tasks", |
| 236 | + .setup = setup, |
| 237 | + .run = run, |
| 238 | + .cleanup = cleanup, |
| 239 | +}; |
| 240 | +REGISTER_SCX_TEST(&rt_stall) |
0 commit comments