xref: /linux/tools/perf/bench/kallsyms-parse.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
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 
14 static unsigned int iterations = 100;
15 
16 static const struct option options[] = {
17 	OPT_UINTEGER('i', "iterations", &iterations,
18 		"Number of iterations used to compute average"),
19 	OPT_END()
20 };
21 
22 static const char *const bench_usage[] = {
23 	"perf bench internals kallsyms-parse <options>",
24 	NULL
25 };
26 
27 static int bench_process_symbol(void *arg __maybe_unused,
28 				const char *name __maybe_unused,
29 				char type __maybe_unused,
30 				u64 start __maybe_unused)
31 {
32 	return 0;
33 }
34 
35 static int do_kallsyms_parse(void)
36 {
37 	struct timeval start, end, diff;
38 	u64 runtime_us;
39 	unsigned int i;
40 	double time_average, time_stddev;
41 	int err;
42 	struct stats time_stats;
43 
44 	init_stats(&time_stats);
45 
46 	for (i = 0; i < iterations; i++) {
47 		gettimeofday(&start, NULL);
48 		err = kallsyms__parse("/proc/kallsyms", NULL,
49 				bench_process_symbol);
50 		if (err)
51 			return err;
52 
53 		gettimeofday(&end, NULL);
54 		timersub(&end, &start, &diff);
55 		runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
56 		update_stats(&time_stats, runtime_us);
57 	}
58 
59 	time_average = avg_stats(&time_stats) / USEC_PER_MSEC;
60 	time_stddev = stddev_stats(&time_stats) / USEC_PER_MSEC;
61 	printf("  Average kallsyms__parse took: %.3f ms (+- %.3f ms)\n",
62 		time_average, time_stddev);
63 	return 0;
64 }
65 
66 int bench_kallsyms_parse(int argc, const char **argv)
67 {
68 	argc = parse_options(argc, argv, options, bench_usage, 0);
69 	if (argc) {
70 		usage_with_options(bench_usage, options);
71 		exit(EXIT_FAILURE);
72 	}
73 
74 	return do_kallsyms_parse();
75 }
76