scx_qmap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2022 Meta Platforms, Inc. and affiliates.
  4. * Copyright (c) 2022 Tejun Heo <tj@kernel.org>
  5. * Copyright (c) 2022 David Vernet <dvernet@meta.com>
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <inttypes.h>
  11. #include <signal.h>
  12. #include <libgen.h>
  13. #include <bpf/bpf.h>
  14. #include <scx/common.h>
  15. #include "scx_qmap.bpf.skel.h"
  16. const char help_fmt[] =
  17. "A simple five-level FIFO queue sched_ext scheduler.\n"
  18. "\n"
  19. "See the top-level comment in .bpf.c for more details.\n"
  20. "\n"
  21. "Usage: %s [-s SLICE_US] [-e COUNT] [-t COUNT] [-T COUNT] [-l COUNT] [-b COUNT]\n"
  22. " [-P] [-M] [-d PID] [-D LEN] [-p] [-v]\n"
  23. "\n"
  24. " -s SLICE_US Override slice duration\n"
  25. " -e COUNT Trigger scx_bpf_error() after COUNT enqueues\n"
  26. " -t COUNT Stall every COUNT'th user thread\n"
  27. " -T COUNT Stall every COUNT'th kernel thread\n"
  28. " -l COUNT Trigger dispatch infinite looping after COUNT dispatches\n"
  29. " -b COUNT Dispatch upto COUNT tasks together\n"
  30. " -P Print out DSQ content and event counters to trace_pipe every second\n"
  31. " -M Print out debug messages to trace_pipe\n"
  32. " -H Boost nice -20 tasks in SHARED_DSQ, use with -b\n"
  33. " -d PID Disallow a process from switching into SCHED_EXT (-1 for self)\n"
  34. " -D LEN Set scx_exit_info.dump buffer length\n"
  35. " -S Suppress qmap-specific debug dump\n"
  36. " -p Switch only tasks on SCHED_EXT policy instead of all\n"
  37. " -v Print libbpf debug messages\n"
  38. " -h Display this help and exit\n";
  39. static bool verbose;
  40. static volatile int exit_req;
  41. static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
  42. {
  43. if (level == LIBBPF_DEBUG && !verbose)
  44. return 0;
  45. return vfprintf(stderr, format, args);
  46. }
  47. static void sigint_handler(int dummy)
  48. {
  49. exit_req = 1;
  50. }
  51. int main(int argc, char **argv)
  52. {
  53. struct scx_qmap *skel;
  54. struct bpf_link *link;
  55. int opt;
  56. libbpf_set_print(libbpf_print_fn);
  57. signal(SIGINT, sigint_handler);
  58. signal(SIGTERM, sigint_handler);
  59. skel = SCX_OPS_OPEN(qmap_ops, scx_qmap);
  60. skel->rodata->slice_ns = __COMPAT_ENUM_OR_ZERO("scx_public_consts", "SCX_SLICE_DFL");
  61. while ((opt = getopt(argc, argv, "s:e:t:T:l:b:PMHd:D:Spvh")) != -1) {
  62. switch (opt) {
  63. case 's':
  64. skel->rodata->slice_ns = strtoull(optarg, NULL, 0) * 1000;
  65. break;
  66. case 'e':
  67. skel->bss->test_error_cnt = strtoul(optarg, NULL, 0);
  68. break;
  69. case 't':
  70. skel->rodata->stall_user_nth = strtoul(optarg, NULL, 0);
  71. break;
  72. case 'T':
  73. skel->rodata->stall_kernel_nth = strtoul(optarg, NULL, 0);
  74. break;
  75. case 'l':
  76. skel->rodata->dsp_inf_loop_after = strtoul(optarg, NULL, 0);
  77. break;
  78. case 'b':
  79. skel->rodata->dsp_batch = strtoul(optarg, NULL, 0);
  80. break;
  81. case 'P':
  82. skel->rodata->print_dsqs_and_events = true;
  83. break;
  84. case 'M':
  85. skel->rodata->print_msgs = true;
  86. break;
  87. case 'H':
  88. skel->rodata->highpri_boosting = true;
  89. break;
  90. case 'd':
  91. skel->rodata->disallow_tgid = strtol(optarg, NULL, 0);
  92. if (skel->rodata->disallow_tgid < 0)
  93. skel->rodata->disallow_tgid = getpid();
  94. break;
  95. case 'D':
  96. skel->struct_ops.qmap_ops->exit_dump_len = strtoul(optarg, NULL, 0);
  97. break;
  98. case 'S':
  99. skel->rodata->suppress_dump = true;
  100. break;
  101. case 'p':
  102. skel->struct_ops.qmap_ops->flags |= SCX_OPS_SWITCH_PARTIAL;
  103. break;
  104. case 'v':
  105. verbose = true;
  106. break;
  107. default:
  108. fprintf(stderr, help_fmt, basename(argv[0]));
  109. return opt != 'h';
  110. }
  111. }
  112. SCX_OPS_LOAD(skel, qmap_ops, scx_qmap, uei);
  113. link = SCX_OPS_ATTACH(skel, qmap_ops, scx_qmap);
  114. while (!exit_req && !UEI_EXITED(skel, uei)) {
  115. long nr_enqueued = skel->bss->nr_enqueued;
  116. long nr_dispatched = skel->bss->nr_dispatched;
  117. printf("stats : enq=%lu dsp=%lu delta=%ld reenq=%"PRIu64" deq=%"PRIu64" core=%"PRIu64" enq_ddsp=%"PRIu64"\n",
  118. nr_enqueued, nr_dispatched, nr_enqueued - nr_dispatched,
  119. skel->bss->nr_reenqueued, skel->bss->nr_dequeued,
  120. skel->bss->nr_core_sched_execed,
  121. skel->bss->nr_ddsp_from_enq);
  122. printf(" exp_local=%"PRIu64" exp_remote=%"PRIu64" exp_timer=%"PRIu64" exp_lost=%"PRIu64"\n",
  123. skel->bss->nr_expedited_local,
  124. skel->bss->nr_expedited_remote,
  125. skel->bss->nr_expedited_from_timer,
  126. skel->bss->nr_expedited_lost);
  127. if (__COMPAT_has_ksym("scx_bpf_cpuperf_cur"))
  128. printf("cpuperf: cur min/avg/max=%u/%u/%u target min/avg/max=%u/%u/%u\n",
  129. skel->bss->cpuperf_min,
  130. skel->bss->cpuperf_avg,
  131. skel->bss->cpuperf_max,
  132. skel->bss->cpuperf_target_min,
  133. skel->bss->cpuperf_target_avg,
  134. skel->bss->cpuperf_target_max);
  135. fflush(stdout);
  136. sleep(1);
  137. }
  138. bpf_link__destroy(link);
  139. UEI_REPORT(skel, uei);
  140. scx_qmap__destroy(skel);
  141. /*
  142. * scx_qmap implements ops.cpu_on/offline() and doesn't need to restart
  143. * on CPU hotplug events.
  144. */
  145. return 0;
  146. }