bench_bpf_loop.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2021 Facebook */
  3. #include <argp.h>
  4. #include "bench.h"
  5. #include "bpf_loop_bench.skel.h"
  6. /* BPF triggering benchmarks */
  7. static struct ctx {
  8. struct bpf_loop_bench *skel;
  9. } ctx;
  10. static struct {
  11. __u32 nr_loops;
  12. } args = {
  13. .nr_loops = 10,
  14. };
  15. enum {
  16. ARG_NR_LOOPS = 4000,
  17. };
  18. static const struct argp_option opts[] = {
  19. { "nr_loops", ARG_NR_LOOPS, "nr_loops", 0,
  20. "Set number of loops for the bpf_loop helper"},
  21. {},
  22. };
  23. static error_t parse_arg(int key, char *arg, struct argp_state *state)
  24. {
  25. switch (key) {
  26. case ARG_NR_LOOPS:
  27. args.nr_loops = strtol(arg, NULL, 10);
  28. break;
  29. default:
  30. return ARGP_ERR_UNKNOWN;
  31. }
  32. return 0;
  33. }
  34. /* exported into benchmark runner */
  35. const struct argp bench_bpf_loop_argp = {
  36. .options = opts,
  37. .parser = parse_arg,
  38. };
  39. static void validate(void)
  40. {
  41. if (env.consumer_cnt != 0) {
  42. fprintf(stderr, "benchmark doesn't support consumer!\n");
  43. exit(1);
  44. }
  45. }
  46. static void *producer(void *input)
  47. {
  48. while (true)
  49. /* trigger the bpf program */
  50. syscall(__NR_getpgid);
  51. return NULL;
  52. }
  53. static void measure(struct bench_res *res)
  54. {
  55. res->hits = atomic_swap(&ctx.skel->bss->hits, 0);
  56. }
  57. static void setup(void)
  58. {
  59. struct bpf_link *link;
  60. setup_libbpf();
  61. ctx.skel = bpf_loop_bench__open_and_load();
  62. if (!ctx.skel) {
  63. fprintf(stderr, "failed to open skeleton\n");
  64. exit(1);
  65. }
  66. link = bpf_program__attach(ctx.skel->progs.benchmark);
  67. if (!link) {
  68. fprintf(stderr, "failed to attach program!\n");
  69. exit(1);
  70. }
  71. ctx.skel->bss->nr_loops = args.nr_loops;
  72. }
  73. const struct bench bench_bpf_loop = {
  74. .name = "bpf-loop",
  75. .argp = &bench_bpf_loop_argp,
  76. .validate = validate,
  77. .setup = setup,
  78. .producer_thread = producer,
  79. .measure = measure,
  80. .report_progress = ops_report_progress,
  81. .report_final = ops_report_final,
  82. };