Merge tag 'gpio-updates-for-v5.15' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/kernel/linux-starfive.git] / tools / perf / bench / evlist-open-close.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include "bench.h"
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>
21
22 #define MMAP_FLUSH_DEFAULT 1
23
24 static int iterations = 100;
25 static int nr_events = 1;
26 static const char *event_string = "dummy";
27
28 static struct record_opts opts = {
29         .sample_time         = true,
30         .mmap_pages          = UINT_MAX,
31         .user_freq           = UINT_MAX,
32         .user_interval       = ULLONG_MAX,
33         .freq                = 4000,
34         .target              = {
35                 .uses_mmap   = true,
36                 .default_per_cpu = true,
37         },
38         .mmap_flush          = MMAP_FLUSH_DEFAULT,
39         .nr_threads_synthesize = 1,
40         .ctl_fd              = -1,
41         .ctl_fd_ack          = -1,
42 };
43
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"),
55         OPT_END()
56 };
57
58 static const char *const bench_usage[] = {
59         "perf bench internals evlist-open-close <options>",
60         NULL
61 };
62
63 static int evlist__count_evsel_fds(struct evlist *evlist)
64 {
65         struct evsel *evsel;
66         int cnt = 0;
67
68         evlist__for_each_entry(evlist, evsel)
69                 cnt += evsel->core.threads->nr * evsel->core.cpus->nr;
70
71         return cnt;
72 }
73
74 static struct evlist *bench__create_evlist(char *evstr)
75 {
76         struct parse_events_error err = { .idx = 0, };
77         struct evlist *evlist = evlist__new();
78         int ret;
79
80         if (!evlist) {
81                 pr_err("Not enough memory to create evlist\n");
82                 return NULL;
83         }
84
85         ret = parse_events(evlist, evstr, &err);
86         if (ret) {
87                 parse_events_print_error(&err, evstr);
88                 pr_err("Run 'perf list' for a list of valid events\n");
89                 ret = 1;
90                 goto out_delete_evlist;
91         }
92
93         ret = evlist__create_maps(evlist, &opts.target);
94         if (ret < 0) {
95                 pr_err("Not enough memory to create thread/cpu maps\n");
96                 goto out_delete_evlist;
97         }
98
99         evlist__config(evlist, &opts, NULL);
100
101         return evlist;
102
103 out_delete_evlist:
104         evlist__delete(evlist);
105         return NULL;
106 }
107
108 static int bench__do_evlist_open_close(struct evlist *evlist)
109 {
110         char sbuf[STRERR_BUFSIZE];
111         int err = evlist__open(evlist);
112
113         if (err < 0) {
114                 pr_err("evlist__open: %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
115                 return err;
116         }
117
118         err = evlist__mmap(evlist, opts.mmap_pages);
119         if (err < 0) {
120                 pr_err("evlist__mmap: %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
121                 return err;
122         }
123
124         evlist__enable(evlist);
125         evlist__disable(evlist);
126         evlist__munmap(evlist);
127         evlist__close(evlist);
128
129         return 0;
130 }
131
132 static int bench_evlist_open_close__run(char *evstr)
133 {
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;
139         u64 runtime_us;
140         int i, err;
141
142         if (!evlist)
143                 return -ENOMEM;
144
145         init_stats(&time_stats);
146
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);
152
153         evlist__delete(evlist);
154
155         for (i = 0; i < iterations; i++) {
156                 pr_debug("Started iteration %d\n", i);
157                 evlist = bench__create_evlist(evstr);
158                 if (!evlist)
159                         return -ENOMEM;
160
161                 gettimeofday(&start, NULL);
162                 err = bench__do_evlist_open_close(evlist);
163                 if (err) {
164                         evlist__delete(evlist);
165                         return err;
166                 }
167
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);
172
173                 evlist__delete(evlist);
174                 pr_debug("Iteration %d took:\t%" PRIu64 "us\n", i, runtime_us);
175         }
176
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);
180
181         return 0;
182 }
183
184 static char *bench__repeat_event_string(const char *evstr, int n)
185 {
186         char sbuf[STRERR_BUFSIZE];
187         struct strbuf buf;
188         int i, str_size = strlen(evstr),
189             final_size = str_size * n + n,
190             err = strbuf_init(&buf, final_size);
191
192         if (err) {
193                 pr_err("strbuf_init: %s\n", str_error_r(err, sbuf, sizeof(sbuf)));
194                 goto out_error;
195         }
196
197         for (i = 0; i < n; i++) {
198                 err = strbuf_add(&buf, evstr, str_size);
199                 if (err) {
200                         pr_err("strbuf_add: %s\n", str_error_r(err, sbuf, sizeof(sbuf)));
201                         goto out_error;
202                 }
203
204                 err = strbuf_addch(&buf, i == n-1 ? '\0' : ',');
205                 if (err) {
206                         pr_err("strbuf_addch: %s\n", str_error_r(err, sbuf, sizeof(sbuf)));
207                         goto out_error;
208                 }
209         }
210
211         return strbuf_detach(&buf, NULL);
212
213 out_error:
214         strbuf_release(&buf);
215         return NULL;
216 }
217
218
219 int bench_evlist_open_close(int argc, const char **argv)
220 {
221         char *evstr, errbuf[BUFSIZ];
222         int err;
223
224         argc = parse_options(argc, argv, options, bench_usage, 0);
225         if (argc) {
226                 usage_with_options(bench_usage, options);
227                 exit(EXIT_FAILURE);
228         }
229
230         err = target__validate(&opts.target);
231         if (err) {
232                 target__strerror(&opts.target, err, errbuf, sizeof(errbuf));
233                 pr_err("%s\n", errbuf);
234                 goto out;
235         }
236
237         err = target__parse_uid(&opts.target);
238         if (err) {
239                 target__strerror(&opts.target, err, errbuf, sizeof(errbuf));
240                 pr_err("%s", errbuf);
241                 goto out;
242         }
243
244         /* Enable ignoring missing threads when -u/-p option is defined. */
245         opts.ignore_missing_thread = opts.target.uid != UINT_MAX || opts.target.pid;
246
247         evstr = bench__repeat_event_string(event_string, nr_events);
248         if (!evstr) {
249                 err = -ENOMEM;
250                 goto out;
251         }
252
253         err = bench_evlist_open_close__run(evstr);
254
255         free(evstr);
256 out:
257         return err;
258 }