rt_stall.c 6.7 KB

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