bench_strncmp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2021. Huawei Technologies Co., Ltd */
  3. #include <argp.h>
  4. #include "bench.h"
  5. #include "strncmp_bench.skel.h"
  6. static struct strncmp_ctx {
  7. struct strncmp_bench *skel;
  8. } ctx;
  9. static struct strncmp_args {
  10. u32 cmp_str_len;
  11. } args = {
  12. .cmp_str_len = 32,
  13. };
  14. enum {
  15. ARG_CMP_STR_LEN = 5000,
  16. };
  17. static const struct argp_option opts[] = {
  18. { "cmp-str-len", ARG_CMP_STR_LEN, "CMP_STR_LEN", 0,
  19. "Set the length of compared string" },
  20. {},
  21. };
  22. static error_t strncmp_parse_arg(int key, char *arg, struct argp_state *state)
  23. {
  24. switch (key) {
  25. case ARG_CMP_STR_LEN:
  26. args.cmp_str_len = strtoul(arg, NULL, 10);
  27. if (!args.cmp_str_len ||
  28. args.cmp_str_len >= sizeof(ctx.skel->bss->str)) {
  29. fprintf(stderr, "Invalid cmp str len (limit %zu)\n",
  30. sizeof(ctx.skel->bss->str));
  31. argp_usage(state);
  32. }
  33. break;
  34. default:
  35. return ARGP_ERR_UNKNOWN;
  36. }
  37. return 0;
  38. }
  39. const struct argp bench_strncmp_argp = {
  40. .options = opts,
  41. .parser = strncmp_parse_arg,
  42. };
  43. static void strncmp_validate(void)
  44. {
  45. if (env.consumer_cnt != 0) {
  46. fprintf(stderr, "strncmp benchmark doesn't support consumer!\n");
  47. exit(1);
  48. }
  49. }
  50. static void strncmp_setup(void)
  51. {
  52. int err;
  53. char *target;
  54. size_t i, sz;
  55. sz = sizeof(ctx.skel->rodata->target);
  56. if (!sz || sz < sizeof(ctx.skel->bss->str)) {
  57. fprintf(stderr, "invalid string size (target %zu, src %zu)\n",
  58. sz, sizeof(ctx.skel->bss->str));
  59. exit(1);
  60. }
  61. setup_libbpf();
  62. ctx.skel = strncmp_bench__open();
  63. if (!ctx.skel) {
  64. fprintf(stderr, "failed to open skeleton\n");
  65. exit(1);
  66. }
  67. srandom(time(NULL));
  68. target = ctx.skel->rodata->target;
  69. for (i = 0; i < sz - 1; i++)
  70. target[i] = '1' + random() % 9;
  71. target[sz - 1] = '\0';
  72. ctx.skel->rodata->cmp_str_len = args.cmp_str_len;
  73. memcpy(ctx.skel->bss->str, target, args.cmp_str_len);
  74. ctx.skel->bss->str[args.cmp_str_len] = '\0';
  75. /* Make bss->str < rodata->target */
  76. ctx.skel->bss->str[args.cmp_str_len - 1] -= 1;
  77. err = strncmp_bench__load(ctx.skel);
  78. if (err) {
  79. fprintf(stderr, "failed to load skeleton\n");
  80. strncmp_bench__destroy(ctx.skel);
  81. exit(1);
  82. }
  83. }
  84. static void strncmp_attach_prog(struct bpf_program *prog)
  85. {
  86. struct bpf_link *link;
  87. link = bpf_program__attach(prog);
  88. if (!link) {
  89. fprintf(stderr, "failed to attach program!\n");
  90. exit(1);
  91. }
  92. }
  93. static void strncmp_no_helper_setup(void)
  94. {
  95. strncmp_setup();
  96. strncmp_attach_prog(ctx.skel->progs.strncmp_no_helper);
  97. }
  98. static void strncmp_helper_setup(void)
  99. {
  100. strncmp_setup();
  101. strncmp_attach_prog(ctx.skel->progs.strncmp_helper);
  102. }
  103. static void *strncmp_producer(void *ctx)
  104. {
  105. while (true)
  106. (void)syscall(__NR_getpgid);
  107. return NULL;
  108. }
  109. static void strncmp_measure(struct bench_res *res)
  110. {
  111. res->hits = atomic_swap(&ctx.skel->bss->hits, 0);
  112. }
  113. const struct bench bench_strncmp_no_helper = {
  114. .name = "strncmp-no-helper",
  115. .argp = &bench_strncmp_argp,
  116. .validate = strncmp_validate,
  117. .setup = strncmp_no_helper_setup,
  118. .producer_thread = strncmp_producer,
  119. .measure = strncmp_measure,
  120. .report_progress = hits_drops_report_progress,
  121. .report_final = hits_drops_report_final,
  122. };
  123. const struct bench bench_strncmp_helper = {
  124. .name = "strncmp-helper",
  125. .argp = &bench_strncmp_argp,
  126. .validate = strncmp_validate,
  127. .setup = strncmp_helper_setup,
  128. .producer_thread = strncmp_producer,
  129. .measure = strncmp_measure,
  130. .report_progress = hits_drops_report_progress,
  131. .report_final = hits_drops_report_final,
  132. };