kallsyms-parse.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Benchmark of /proc/kallsyms parsing.
  4. *
  5. * Copyright 2020 Google LLC.
  6. */
  7. #include <stdlib.h>
  8. #include "bench.h"
  9. #include "../util/stat.h"
  10. #include <linux/time64.h>
  11. #include <subcmd/parse-options.h>
  12. #include <symbol/kallsyms.h>
  13. static unsigned int iterations = 100;
  14. static const struct option options[] = {
  15. OPT_UINTEGER('i', "iterations", &iterations,
  16. "Number of iterations used to compute average"),
  17. OPT_END()
  18. };
  19. static const char *const bench_usage[] = {
  20. "perf bench internals kallsyms-parse <options>",
  21. NULL
  22. };
  23. static int bench_process_symbol(void *arg __maybe_unused,
  24. const char *name __maybe_unused,
  25. char type __maybe_unused,
  26. u64 start __maybe_unused)
  27. {
  28. return 0;
  29. }
  30. static int do_kallsyms_parse(void)
  31. {
  32. struct timeval start, end, diff;
  33. u64 runtime_us;
  34. unsigned int i;
  35. double time_average, time_stddev;
  36. int err;
  37. struct stats time_stats;
  38. init_stats(&time_stats);
  39. for (i = 0; i < iterations; i++) {
  40. gettimeofday(&start, NULL);
  41. err = kallsyms__parse("/proc/kallsyms", NULL,
  42. bench_process_symbol);
  43. if (err)
  44. return err;
  45. gettimeofday(&end, NULL);
  46. timersub(&end, &start, &diff);
  47. runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
  48. update_stats(&time_stats, runtime_us);
  49. }
  50. time_average = avg_stats(&time_stats) / USEC_PER_MSEC;
  51. time_stddev = stddev_stats(&time_stats) / USEC_PER_MSEC;
  52. printf(" Average kallsyms__parse took: %.3f ms (+- %.3f ms)\n",
  53. time_average, time_stddev);
  54. return 0;
  55. }
  56. int bench_kallsyms_parse(int argc, const char **argv)
  57. {
  58. argc = parse_options(argc, argv, options, bench_usage, 0);
  59. if (argc) {
  60. usage_with_options(bench_usage, options);
  61. exit(EXIT_FAILURE);
  62. }
  63. return do_kallsyms_parse();
  64. }