scx_sdt.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
  4. * Copyright (c) 2024 Emil Tsalapatis <etsal@meta.com>
  5. * Copyright (c) 2024 Tejun Heo <tj@kernel.org>
  6. * Copyright (c) 2022 David Vernet <dvernet@meta.com>
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <signal.h>
  11. #include <libgen.h>
  12. #include <bpf/bpf.h>
  13. #include <scx/common.h>
  14. #include "scx_sdt.h"
  15. #include "scx_sdt.bpf.skel.h"
  16. const char help_fmt[] =
  17. "A simple arena-based sched_ext scheduler.\n"
  18. "\n"
  19. "Modified version of scx_simple that demonstrates arena-based data structures.\n"
  20. "\n"
  21. "Usage: %s [-f] [-v]\n"
  22. "\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 sig)
  34. {
  35. exit_req = 1;
  36. }
  37. int main(int argc, char **argv)
  38. {
  39. struct scx_sdt *skel;
  40. struct bpf_link *link;
  41. __u32 opt;
  42. __u64 ecode;
  43. libbpf_set_print(libbpf_print_fn);
  44. signal(SIGINT, sigint_handler);
  45. signal(SIGTERM, sigint_handler);
  46. restart:
  47. optind = 1;
  48. skel = SCX_OPS_OPEN(sdt_ops, scx_sdt);
  49. while ((opt = getopt(argc, argv, "vh")) != -1) {
  50. switch (opt) {
  51. case 'v':
  52. verbose = true;
  53. break;
  54. default:
  55. fprintf(stderr, help_fmt, basename(argv[0]));
  56. return opt != 'h';
  57. }
  58. }
  59. SCX_OPS_LOAD(skel, sdt_ops, scx_sdt, uei);
  60. link = SCX_OPS_ATTACH(skel, sdt_ops, scx_sdt);
  61. while (!exit_req && !UEI_EXITED(skel, uei)) {
  62. printf("====SCHEDULING STATS====\n");
  63. printf("enqueues=%llu\t", skel->bss->stat_enqueue);
  64. printf("inits=%llu\t", skel->bss->stat_init);
  65. printf("exits=%llu\t", skel->bss->stat_exit);
  66. printf("\n");
  67. printf("select_idle_cpu=%llu\t", skel->bss->stat_select_idle_cpu);
  68. printf("select_busy_cpu=%llu\t", skel->bss->stat_select_busy_cpu);
  69. printf("\n");
  70. printf("====ALLOCATION STATS====\n");
  71. printf("chunk allocs=%llu\t", skel->bss->alloc_stats.chunk_allocs);
  72. printf("data_allocs=%llu\n", skel->bss->alloc_stats.data_allocs);
  73. printf("alloc_ops=%llu\t", skel->bss->alloc_stats.alloc_ops);
  74. printf("free_ops=%llu\t", skel->bss->alloc_stats.free_ops);
  75. printf("active_allocs=%llu\t", skel->bss->alloc_stats.active_allocs);
  76. printf("arena_pages_used=%llu\t", skel->bss->alloc_stats.arena_pages_used);
  77. printf("\n\n");
  78. fflush(stdout);
  79. sleep(1);
  80. }
  81. bpf_link__destroy(link);
  82. ecode = UEI_REPORT(skel, uei);
  83. scx_sdt__destroy(skel);
  84. if (UEI_ECODE_RESTART(ecode))
  85. goto restart;
  86. return 0;
  87. }