1 // SPDX-License-Identifier: GPL-2.0
7 #include "../util/debug.h"
8 #include "../util/stat.h"
9 #include "../util/evlist.h"
10 #include "../util/evsel.h"
11 #include "../util/strbuf.h"
12 #include "../util/record.h"
13 #include "../util/parse-events.h"
14 #include "internal/threadmap.h"
15 #include "internal/cpumap.h"
16 #include <linux/perf_event.h>
17 #include <linux/kernel.h>
18 #include <linux/time64.h>
19 #include <linux/string.h>
20 #include <subcmd/parse-options.h>
22 #define MMAP_FLUSH_DEFAULT 1
24 static int iterations = 100;
25 static int nr_events = 1;
26 static const char *event_string = "dummy";
28 static struct record_opts opts = {
30 .mmap_pages = UINT_MAX,
31 .user_freq = UINT_MAX,
32 .user_interval = ULLONG_MAX,
36 .default_per_cpu = true,
38 .mmap_flush = MMAP_FLUSH_DEFAULT,
39 .nr_threads_synthesize = 1,
44 static const struct option options[] = {
45 OPT_STRING('e', "event", &event_string, "event", "event selector. use 'perf list' to list available events"),
46 OPT_INTEGER('n', "nr-events", &nr_events,
47 "number of dummy events to create (default 1). If used with -e, it clones those events n times (1 = no change)"),
48 OPT_INTEGER('i', "iterations", &iterations, "Number of iterations used to compute average (default=100)"),
49 OPT_BOOLEAN('a', "all-cpus", &opts.target.system_wide, "system-wide collection from all CPUs"),
50 OPT_STRING('C', "cpu", &opts.target.cpu_list, "cpu", "list of cpus where to open events"),
51 OPT_STRING('p', "pid", &opts.target.pid, "pid", "record events on existing process id"),
52 OPT_STRING('t', "tid", &opts.target.tid, "tid", "record events on existing thread id"),
53 OPT_STRING('u', "uid", &opts.target.uid_str, "user", "user to profile"),
54 OPT_BOOLEAN(0, "per-thread", &opts.target.per_thread, "use per-thread mmaps"),
58 static const char *const bench_usage[] = {
59 "perf bench internals evlist-open-close <options>",
63 static int evlist__count_evsel_fds(struct evlist *evlist)
68 evlist__for_each_entry(evlist, evsel)
69 cnt += evsel->core.threads->nr * evsel->core.cpus->nr;
74 static struct evlist *bench__create_evlist(char *evstr)
76 struct parse_events_error err = { .idx = 0, };
77 struct evlist *evlist = evlist__new();
81 pr_err("Not enough memory to create evlist\n");
85 ret = parse_events(evlist, evstr, &err);
87 parse_events_print_error(&err, evstr);
88 pr_err("Run 'perf list' for a list of valid events\n");
90 goto out_delete_evlist;
93 ret = evlist__create_maps(evlist, &opts.target);
95 pr_err("Not enough memory to create thread/cpu maps\n");
96 goto out_delete_evlist;
99 evlist__config(evlist, &opts, NULL);
104 evlist__delete(evlist);
108 static int bench__do_evlist_open_close(struct evlist *evlist)
110 char sbuf[STRERR_BUFSIZE];
111 int err = evlist__open(evlist);
114 pr_err("evlist__open: %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
118 err = evlist__mmap(evlist, opts.mmap_pages);
120 pr_err("evlist__mmap: %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
124 evlist__enable(evlist);
125 evlist__disable(evlist);
126 evlist__munmap(evlist);
127 evlist__close(evlist);
132 static int bench_evlist_open_close__run(char *evstr)
134 // used to print statistics only
135 struct evlist *evlist = bench__create_evlist(evstr);
136 double time_average, time_stddev;
137 struct timeval start, end, diff;
138 struct stats time_stats;
145 init_stats(&time_stats);
147 printf(" Number of cpus:\t%d\n", evlist->core.cpus->nr);
148 printf(" Number of threads:\t%d\n", evlist->core.threads->nr);
149 printf(" Number of events:\t%d (%d fds)\n",
150 evlist->core.nr_entries, evlist__count_evsel_fds(evlist));
151 printf(" Number of iterations:\t%d\n", iterations);
153 evlist__delete(evlist);
155 for (i = 0; i < iterations; i++) {
156 pr_debug("Started iteration %d\n", i);
157 evlist = bench__create_evlist(evstr);
161 gettimeofday(&start, NULL);
162 err = bench__do_evlist_open_close(evlist);
164 evlist__delete(evlist);
168 gettimeofday(&end, NULL);
169 timersub(&end, &start, &diff);
170 runtime_us = diff.tv_sec * USEC_PER_SEC + diff.tv_usec;
171 update_stats(&time_stats, runtime_us);
173 evlist__delete(evlist);
174 pr_debug("Iteration %d took:\t%" PRIu64 "us\n", i, runtime_us);
177 time_average = avg_stats(&time_stats);
178 time_stddev = stddev_stats(&time_stats);
179 printf(" Average open-close took: %.3f usec (+- %.3f usec)\n", time_average, time_stddev);
184 static char *bench__repeat_event_string(const char *evstr, int n)
186 char sbuf[STRERR_BUFSIZE];
188 int i, str_size = strlen(evstr),
189 final_size = str_size * n + n,
190 err = strbuf_init(&buf, final_size);
193 pr_err("strbuf_init: %s\n", str_error_r(err, sbuf, sizeof(sbuf)));
197 for (i = 0; i < n; i++) {
198 err = strbuf_add(&buf, evstr, str_size);
200 pr_err("strbuf_add: %s\n", str_error_r(err, sbuf, sizeof(sbuf)));
204 err = strbuf_addch(&buf, i == n-1 ? '\0' : ',');
206 pr_err("strbuf_addch: %s\n", str_error_r(err, sbuf, sizeof(sbuf)));
211 return strbuf_detach(&buf, NULL);
214 strbuf_release(&buf);
219 int bench_evlist_open_close(int argc, const char **argv)
221 char *evstr, errbuf[BUFSIZ];
224 argc = parse_options(argc, argv, options, bench_usage, 0);
226 usage_with_options(bench_usage, options);
230 err = target__validate(&opts.target);
232 target__strerror(&opts.target, err, errbuf, sizeof(errbuf));
233 pr_err("%s\n", errbuf);
237 err = target__parse_uid(&opts.target);
239 target__strerror(&opts.target, err, errbuf, sizeof(errbuf));
240 pr_err("%s", errbuf);
244 /* Enable ignoring missing threads when -u/-p option is defined. */
245 opts.ignore_missing_thread = opts.target.uid != UINT_MAX || opts.target.pid;
247 evstr = bench__repeat_event_string(event_string, nr_events);
253 err = bench_evlist_open_close__run(evstr);