scx_pair.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <unistd.h>
  9. #include <inttypes.h>
  10. #include <signal.h>
  11. #include <assert.h>
  12. #include <libgen.h>
  13. #include <bpf/bpf.h>
  14. #include <scx/common.h>
  15. #include "scx_pair.h"
  16. #include "scx_pair.bpf.skel.h"
  17. const char help_fmt[] =
  18. "A demo sched_ext core-scheduler which always makes every sibling CPU pair\n"
  19. "execute from the same CPU cgroup.\n"
  20. "\n"
  21. "See the top-level comment in .bpf.c for more details.\n"
  22. "\n"
  23. "Usage: %s [-S STRIDE]\n"
  24. "\n"
  25. " -S STRIDE Override CPU pair stride (default: nr_cpus_ids / 2)\n"
  26. " -v Print libbpf debug messages\n"
  27. " -h Display this help and exit\n";
  28. static bool verbose;
  29. static volatile int exit_req;
  30. static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
  31. {
  32. if (level == LIBBPF_DEBUG && !verbose)
  33. return 0;
  34. return vfprintf(stderr, format, args);
  35. }
  36. static void sigint_handler(int dummy)
  37. {
  38. exit_req = 1;
  39. }
  40. int main(int argc, char **argv)
  41. {
  42. struct scx_pair *skel;
  43. struct bpf_link *link;
  44. __u64 seq = 0, ecode;
  45. __s32 stride, i, opt, outer_fd;
  46. libbpf_set_print(libbpf_print_fn);
  47. signal(SIGINT, sigint_handler);
  48. signal(SIGTERM, sigint_handler);
  49. restart:
  50. optind = 1;
  51. skel = SCX_OPS_OPEN(pair_ops, scx_pair);
  52. skel->rodata->nr_cpu_ids = libbpf_num_possible_cpus();
  53. skel->rodata->pair_batch_dur_ns = __COMPAT_ENUM_OR_ZERO("scx_public_consts", "SCX_SLICE_DFL");
  54. /* pair up the earlier half to the latter by default, override with -s */
  55. stride = skel->rodata->nr_cpu_ids / 2;
  56. while ((opt = getopt(argc, argv, "S:vh")) != -1) {
  57. switch (opt) {
  58. case 'S':
  59. stride = strtoul(optarg, NULL, 0);
  60. break;
  61. case 'v':
  62. verbose = true;
  63. break;
  64. default:
  65. fprintf(stderr, help_fmt, basename(argv[0]));
  66. return opt != 'h';
  67. }
  68. }
  69. /* Stride must be positive to pair distinct CPUs. */
  70. if (stride <= 0) {
  71. fprintf(stderr, "Invalid stride %d, must be positive\n", stride);
  72. scx_pair__destroy(skel);
  73. return -1;
  74. }
  75. bpf_map__set_max_entries(skel->maps.pair_ctx, skel->rodata->nr_cpu_ids / 2);
  76. /* Resize arrays so their element count is equal to cpu count. */
  77. RESIZE_ARRAY(skel, rodata, pair_cpu, skel->rodata->nr_cpu_ids);
  78. RESIZE_ARRAY(skel, rodata, pair_id, skel->rodata->nr_cpu_ids);
  79. RESIZE_ARRAY(skel, rodata, in_pair_idx, skel->rodata->nr_cpu_ids);
  80. for (i = 0; i < skel->rodata->nr_cpu_ids; i++)
  81. skel->rodata_pair_cpu->pair_cpu[i] = -1;
  82. printf("Pairs: ");
  83. for (i = 0; i < skel->rodata->nr_cpu_ids; i++) {
  84. int j = (i + stride) % skel->rodata->nr_cpu_ids;
  85. if (skel->rodata_pair_cpu->pair_cpu[i] >= 0)
  86. continue;
  87. SCX_BUG_ON(i == j,
  88. "Invalid stride %d - CPU%d wants to be its own pair",
  89. stride, i);
  90. SCX_BUG_ON(skel->rodata_pair_cpu->pair_cpu[j] >= 0,
  91. "Invalid stride %d - three CPUs (%d, %d, %d) want to be a pair",
  92. stride, i, j, skel->rodata_pair_cpu->pair_cpu[j]);
  93. skel->rodata_pair_cpu->pair_cpu[i] = j;
  94. skel->rodata_pair_cpu->pair_cpu[j] = i;
  95. skel->rodata_pair_id->pair_id[i] = i;
  96. skel->rodata_pair_id->pair_id[j] = i;
  97. skel->rodata_in_pair_idx->in_pair_idx[i] = 0;
  98. skel->rodata_in_pair_idx->in_pair_idx[j] = 1;
  99. printf("[%d, %d] ", i, j);
  100. }
  101. printf("\n");
  102. SCX_OPS_LOAD(skel, pair_ops, scx_pair, uei);
  103. /*
  104. * Populate the cgrp_q_arr map which is an array containing per-cgroup
  105. * queues. It'd probably be better to do this from BPF but there are too
  106. * many to initialize statically and there's no way to dynamically
  107. * populate from BPF.
  108. */
  109. outer_fd = bpf_map__fd(skel->maps.cgrp_q_arr);
  110. SCX_BUG_ON(outer_fd < 0, "Failed to get outer_fd: %d", outer_fd);
  111. printf("Initializing");
  112. for (i = 0; i < MAX_CGRPS; i++) {
  113. __s32 inner_fd;
  114. if (exit_req)
  115. break;
  116. inner_fd = bpf_map_create(BPF_MAP_TYPE_QUEUE, NULL, 0,
  117. sizeof(__u32), MAX_QUEUED, NULL);
  118. SCX_BUG_ON(inner_fd < 0, "Failed to get inner_fd: %d",
  119. inner_fd);
  120. SCX_BUG_ON(bpf_map_update_elem(outer_fd, &i, &inner_fd, BPF_ANY),
  121. "Failed to set inner map");
  122. close(inner_fd);
  123. if (!(i % 10))
  124. printf(".");
  125. fflush(stdout);
  126. }
  127. printf("\n");
  128. /*
  129. * Fully initialized, attach and run.
  130. */
  131. link = SCX_OPS_ATTACH(skel, pair_ops, scx_pair);
  132. while (!exit_req && !UEI_EXITED(skel, uei)) {
  133. printf("[SEQ %llu]\n", seq++);
  134. printf(" total:%10" PRIu64 " dispatch:%10" PRIu64 " missing:%10" PRIu64 "\n",
  135. skel->bss->nr_total,
  136. skel->bss->nr_dispatched,
  137. skel->bss->nr_missing);
  138. printf(" kicks:%10" PRIu64 " preemptions:%7" PRIu64 "\n",
  139. skel->bss->nr_kicks,
  140. skel->bss->nr_preemptions);
  141. printf(" exp:%10" PRIu64 " exp_wait:%10" PRIu64 " exp_empty:%10" PRIu64 "\n",
  142. skel->bss->nr_exps,
  143. skel->bss->nr_exp_waits,
  144. skel->bss->nr_exp_empty);
  145. printf("cgnext:%10" PRIu64 " cgcoll:%10" PRIu64 " cgempty:%10" PRIu64 "\n",
  146. skel->bss->nr_cgrp_next,
  147. skel->bss->nr_cgrp_coll,
  148. skel->bss->nr_cgrp_empty);
  149. fflush(stdout);
  150. sleep(1);
  151. }
  152. bpf_link__destroy(link);
  153. ecode = UEI_REPORT(skel, uei);
  154. scx_pair__destroy(skel);
  155. if (UEI_ECODE_RESTART(ecode))
  156. goto restart;
  157. return 0;
  158. }