scx_simple.c 2.3 KB

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