scx_simple.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <signal.h>
  10. #include <assert.h>
  11. #include <libgen.h>
  12. #include <bpf/bpf.h>
  13. #include <scx/common.h>
  14. #include "scx_simple.bpf.skel.h"
  15. const char help_fmt[] =
  16. "A simple sched_ext scheduler.\n"
  17. "\n"
  18. "See the top-level comment in .bpf.c for more details.\n"
  19. "\n"
  20. "Usage: %s [-f] [-v]\n"
  21. "\n"
  22. " -f Use FIFO scheduling instead of weighted vtime scheduling\n"
  23. " -v Print libbpf debug messages\n"
  24. " -h Display this help and exit\n";
  25. static bool verbose;
  26. static volatile int exit_req;
  27. static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
  28. {
  29. if (level == LIBBPF_DEBUG && !verbose)
  30. return 0;
  31. return vfprintf(stderr, format, args);
  32. }
  33. static void sigint_handler(int simple)
  34. {
  35. exit_req = 1;
  36. }
  37. static void read_stats(struct scx_simple *skel, __u64 *stats)
  38. {
  39. int nr_cpus = libbpf_num_possible_cpus();
  40. assert(nr_cpus > 0);
  41. __u64 cnts[2][nr_cpus];
  42. __u32 idx;
  43. memset(stats, 0, sizeof(stats[0]) * 2);
  44. for (idx = 0; idx < 2; idx++) {
  45. int ret, cpu;
  46. ret = bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats),
  47. &idx, cnts[idx]);
  48. if (ret < 0)
  49. continue;
  50. for (cpu = 0; cpu < nr_cpus; cpu++)
  51. stats[idx] += cnts[idx][cpu];
  52. }
  53. }
  54. int main(int argc, char **argv)
  55. {
  56. struct scx_simple *skel;
  57. struct bpf_link *link;
  58. __u32 opt;
  59. __u64 ecode;
  60. libbpf_set_print(libbpf_print_fn);
  61. signal(SIGINT, sigint_handler);
  62. signal(SIGTERM, sigint_handler);
  63. restart:
  64. optind = 1;
  65. skel = SCX_OPS_OPEN(simple_ops, scx_simple);
  66. while ((opt = getopt(argc, argv, "fvh")) != -1) {
  67. switch (opt) {
  68. case 'f':
  69. skel->rodata->fifo_sched = true;
  70. break;
  71. case 'v':
  72. verbose = true;
  73. break;
  74. default:
  75. fprintf(stderr, help_fmt, basename(argv[0]));
  76. return opt != 'h';
  77. }
  78. }
  79. SCX_OPS_LOAD(skel, simple_ops, scx_simple, uei);
  80. link = SCX_OPS_ATTACH(skel, simple_ops, scx_simple);
  81. while (!exit_req && !UEI_EXITED(skel, uei)) {
  82. __u64 stats[2];
  83. read_stats(skel, stats);
  84. printf("local=%llu global=%llu\n", stats[0], stats[1]);
  85. fflush(stdout);
  86. sleep(1);
  87. }
  88. bpf_link__destroy(link);
  89. ecode = UEI_REPORT(skel, uei);
  90. scx_simple__destroy(skel);
  91. if (UEI_ECODE_RESTART(ecode))
  92. goto restart;
  93. return 0;
  94. }