summaryrefslogtreecommitdiff
path: root/tools/perf/bench/kallsyms-parse.c
blob: 2b0d0f980ae96eca14682aaebac45f65c052c980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: GPL-2.0
/*
 * Benchmark of /proc/kallsyms parsing.
 *
 * Copyright 2020 Google LLC.
 */
#include <stdlib.h>
#include "bench.h"
#include "../util/stat.h"
#include <linux/time64.h>
#include <subcmd/parse-options.h>
#include <symbol/kallsyms.h>

static unsigned int iterations = 100;

static const struct option options[] = {
	OPT_UINTEGER('i', "iterations", &iterations,
		"Number of iterations used to compute average"),
	OPT_END()
};

static const char *const bench_usage[] = {
	"perf bench internals kallsyms-parse <options>",
	NULL
};

static int bench_process_symbol(void *arg __maybe_unused,
				const char *name __maybe_unused,
				char type __maybe_unused,
				u64 start __maybe_unused)
{
	return 0;
}

static int do_kallsyms_parse(void)
{
	struct timeval start, end, diff;
	u64 runtime_us;
	unsigned int i;
	double time_average, time_stddev;
	int err;
	struct stats time_stats;

	init_stats(&time_stats);

	for (i = 0; i < iterations; i++) {
		gettimeofday(&start, NULL);
		err = kallsyms__parse("/proc/kallsyms", NULL,
				bench_process_symbol);
		if (err)
			return err;

		gettimeofday(&end, NULL);
		timersub(&end, &start, &diff);
		runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
		update_stats(&time_stats, runtime_us);
	}

	time_average = avg_stats(&time_stats) / USEC_PER_MSEC;
	time_stddev = stddev_stats(&time_stats) / USEC_PER_MSEC;
	printf("  Average kallsyms__parse took: %.3f ms (+- %.3f ms)\n",
		time_average, time_stddev);
	return 0;
}

int bench_kallsyms_parse(int argc, const char **argv)
{
	argc = parse_options(argc, argv, options, bench_usage, 0);
	if (argc) {
		usage_with_options(bench_usage, options);
		exit(EXIT_FAILURE);
	}

	return do_kallsyms_parse();
}