bench_rename.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2020 Facebook */
  3. #include <fcntl.h>
  4. #include "bench.h"
  5. #include "test_overhead.skel.h"
  6. /* BPF triggering benchmarks */
  7. static struct ctx {
  8. struct test_overhead *skel;
  9. struct counter hits;
  10. int fd;
  11. } ctx;
  12. static void validate(void)
  13. {
  14. if (env.producer_cnt != 1) {
  15. fprintf(stderr, "benchmark doesn't support multi-producer!\n");
  16. exit(1);
  17. }
  18. if (env.consumer_cnt != 0) {
  19. fprintf(stderr, "benchmark doesn't support consumer!\n");
  20. exit(1);
  21. }
  22. }
  23. static void *producer(void *input)
  24. {
  25. char buf[] = "test_overhead";
  26. int err;
  27. while (true) {
  28. err = write(ctx.fd, buf, sizeof(buf));
  29. if (err < 0) {
  30. fprintf(stderr, "write failed\n");
  31. exit(1);
  32. }
  33. atomic_inc(&ctx.hits.value);
  34. }
  35. }
  36. static void measure(struct bench_res *res)
  37. {
  38. res->hits = atomic_swap(&ctx.hits.value, 0);
  39. }
  40. static void setup_ctx(void)
  41. {
  42. setup_libbpf();
  43. ctx.skel = test_overhead__open_and_load();
  44. if (!ctx.skel) {
  45. fprintf(stderr, "failed to open skeleton\n");
  46. exit(1);
  47. }
  48. ctx.fd = open("/proc/self/comm", O_WRONLY|O_TRUNC);
  49. if (ctx.fd < 0) {
  50. fprintf(stderr, "failed to open /proc/self/comm: %d\n", -errno);
  51. exit(1);
  52. }
  53. }
  54. static void attach_bpf(struct bpf_program *prog)
  55. {
  56. struct bpf_link *link;
  57. link = bpf_program__attach(prog);
  58. if (!link) {
  59. fprintf(stderr, "failed to attach program!\n");
  60. exit(1);
  61. }
  62. }
  63. static void setup_base(void)
  64. {
  65. setup_ctx();
  66. }
  67. static void setup_kprobe(void)
  68. {
  69. setup_ctx();
  70. attach_bpf(ctx.skel->progs.prog1);
  71. }
  72. static void setup_kretprobe(void)
  73. {
  74. setup_ctx();
  75. attach_bpf(ctx.skel->progs.prog2);
  76. }
  77. static void setup_rawtp(void)
  78. {
  79. setup_ctx();
  80. attach_bpf(ctx.skel->progs.prog3);
  81. }
  82. static void setup_fentry(void)
  83. {
  84. setup_ctx();
  85. attach_bpf(ctx.skel->progs.prog4);
  86. }
  87. static void setup_fexit(void)
  88. {
  89. setup_ctx();
  90. attach_bpf(ctx.skel->progs.prog5);
  91. }
  92. const struct bench bench_rename_base = {
  93. .name = "rename-base",
  94. .validate = validate,
  95. .setup = setup_base,
  96. .producer_thread = producer,
  97. .measure = measure,
  98. .report_progress = hits_drops_report_progress,
  99. .report_final = hits_drops_report_final,
  100. };
  101. const struct bench bench_rename_kprobe = {
  102. .name = "rename-kprobe",
  103. .validate = validate,
  104. .setup = setup_kprobe,
  105. .producer_thread = producer,
  106. .measure = measure,
  107. .report_progress = hits_drops_report_progress,
  108. .report_final = hits_drops_report_final,
  109. };
  110. const struct bench bench_rename_kretprobe = {
  111. .name = "rename-kretprobe",
  112. .validate = validate,
  113. .setup = setup_kretprobe,
  114. .producer_thread = producer,
  115. .measure = measure,
  116. .report_progress = hits_drops_report_progress,
  117. .report_final = hits_drops_report_final,
  118. };
  119. const struct bench bench_rename_rawtp = {
  120. .name = "rename-rawtp",
  121. .validate = validate,
  122. .setup = setup_rawtp,
  123. .producer_thread = producer,
  124. .measure = measure,
  125. .report_progress = hits_drops_report_progress,
  126. .report_final = hits_drops_report_final,
  127. };
  128. const struct bench bench_rename_fentry = {
  129. .name = "rename-fentry",
  130. .validate = validate,
  131. .setup = setup_fentry,
  132. .producer_thread = producer,
  133. .measure = measure,
  134. .report_progress = hits_drops_report_progress,
  135. .report_final = hits_drops_report_final,
  136. };
  137. const struct bench bench_rename_fexit = {
  138. .name = "rename-fexit",
  139. .validate = validate,
  140. .setup = setup_fexit,
  141. .producer_thread = producer,
  142. .measure = measure,
  143. .report_progress = hits_drops_report_progress,
  144. .report_final = hits_drops_report_final,
  145. };