blob: f74ea2e55fde893ef579733108fc7801f820d504 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
Arnaldo Carvalho de Meloa8c9ae12011-11-05 08:41:51 -02009#include "util.h"
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -030010#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030011#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030012#include <inttypes.h>
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -020013#include <poll.h>
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020014#include "cpumap.h"
15#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090016#include "target.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020017#include "evlist.h"
18#include "evsel.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030019#include "debug.h"
Wang Nan54cc54d2016-07-14 08:34:42 +000020#include "asm/bug.h"
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020021#include <unistd.h>
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020022
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020023#include "parse-events.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060024#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020025
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020026#include <sys/mman.h>
27
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020028#include <linux/bitops.h>
29#include <linux/hash.h>
Arnaldo Carvalho de Melo0389cd12014-12-15 16:04:11 -030030#include <linux/log2.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020031#include <linux/err.h>
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020032
Wang Nan8db6d6b2016-07-14 08:34:35 +000033static void perf_mmap__munmap(struct perf_mmap *map);
Wang Nan48760752016-07-14 08:34:37 +000034static void perf_mmap__put(struct perf_mmap *map);
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -030035
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020036#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030037#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020038
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020039void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
40 struct thread_map *threads)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020041{
42 int i;
43
44 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
45 INIT_HLIST_HEAD(&evlist->heads[i]);
46 INIT_LIST_HEAD(&evlist->entries);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020047 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030048 fdarray__init(&evlist->pollfd, 64);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020049 evlist->workload.pid = -1;
Wang Nan54cc54d2016-07-14 08:34:42 +000050 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020051}
52
Namhyung Kim334fe7a2013-03-11 16:43:12 +090053struct perf_evlist *perf_evlist__new(void)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020054{
55 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
56
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020057 if (evlist != NULL)
Namhyung Kim334fe7a2013-03-11 16:43:12 +090058 perf_evlist__init(evlist, NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020059
60 return evlist;
61}
62
Jiri Olsab22d54b2013-09-01 12:36:14 +020063struct perf_evlist *perf_evlist__new_default(void)
64{
65 struct perf_evlist *evlist = perf_evlist__new();
66
67 if (evlist && perf_evlist__add_default(evlist)) {
68 perf_evlist__delete(evlist);
69 evlist = NULL;
70 }
71
72 return evlist;
73}
74
Arnaldo Carvalho de Melo5bae0252016-01-07 13:14:56 -030075struct perf_evlist *perf_evlist__new_dummy(void)
76{
77 struct perf_evlist *evlist = perf_evlist__new();
78
79 if (evlist && perf_evlist__add_dummy(evlist)) {
80 perf_evlist__delete(evlist);
81 evlist = NULL;
82 }
83
84 return evlist;
85}
86
Adrian Hunter75562572013-08-27 11:23:09 +030087/**
88 * perf_evlist__set_id_pos - set the positions of event ids.
89 * @evlist: selected event list
90 *
91 * Events with compatible sample types all have the same id_pos
92 * and is_pos. For convenience, put a copy on evlist.
93 */
94void perf_evlist__set_id_pos(struct perf_evlist *evlist)
95{
96 struct perf_evsel *first = perf_evlist__first(evlist);
97
98 evlist->id_pos = first->id_pos;
99 evlist->is_pos = first->is_pos;
100}
101
Adrian Hunter733cd2f2013-09-06 22:40:11 +0300102static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
103{
104 struct perf_evsel *evsel;
105
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300106 evlist__for_each_entry(evlist, evsel)
Adrian Hunter733cd2f2013-09-06 22:40:11 +0300107 perf_evsel__calc_id_pos(evsel);
108
109 perf_evlist__set_id_pos(evlist);
110}
111
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200112static void perf_evlist__purge(struct perf_evlist *evlist)
113{
114 struct perf_evsel *pos, *n;
115
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300116 evlist__for_each_entry_safe(evlist, n, pos) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200117 list_del_init(&pos->node);
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -0400118 pos->evlist = NULL;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200119 perf_evsel__delete(pos);
120 }
121
122 evlist->nr_entries = 0;
123}
124
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200125void perf_evlist__exit(struct perf_evlist *evlist)
126{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300127 zfree(&evlist->mmap);
Wang Nanb2cb6152016-07-14 08:34:39 +0000128 zfree(&evlist->backward_mmap);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300129 fdarray__exit(&evlist->pollfd);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200130}
131
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200132void perf_evlist__delete(struct perf_evlist *evlist)
133{
Arnaldo Carvalho de Melo0b04b3d2016-06-21 18:15:45 -0300134 if (evlist == NULL)
135 return;
136
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300137 perf_evlist__munmap(evlist);
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -0300138 perf_evlist__close(evlist);
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200139 cpu_map__put(evlist->cpus);
Jiri Olsa186fbb72015-06-23 00:36:05 +0200140 thread_map__put(evlist->threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300141 evlist->cpus = NULL;
142 evlist->threads = NULL;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200143 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200144 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200145 free(evlist);
146}
147
Adrian Hunteradc0c3e2015-09-08 10:58:58 +0300148static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
149 struct perf_evsel *evsel)
150{
151 /*
152 * We already have cpus for evsel (via PMU sysfs) so
153 * keep it, if there's no target cpu list defined.
154 */
155 if (!evsel->own_cpus || evlist->has_user_cpus) {
156 cpu_map__put(evsel->cpus);
157 evsel->cpus = cpu_map__get(evlist->cpus);
158 } else if (evsel->cpus != evsel->own_cpus) {
159 cpu_map__put(evsel->cpus);
160 evsel->cpus = cpu_map__get(evsel->own_cpus);
161 }
162
163 thread_map__put(evsel->threads);
164 evsel->threads = thread_map__get(evlist->threads);
165}
166
167static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
168{
169 struct perf_evsel *evsel;
170
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300171 evlist__for_each_entry(evlist, evsel)
Adrian Hunteradc0c3e2015-09-08 10:58:58 +0300172 __perf_evlist__propagate_maps(evlist, evsel);
173}
174
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200175void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
176{
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -0400177 entry->evlist = evlist;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200178 list_add_tail(&entry->node, &evlist->entries);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300179 entry->idx = evlist->nr_entries;
Adrian Hunter60b08962014-07-31 09:00:52 +0300180 entry->tracking = !entry->idx;
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300181
Adrian Hunter75562572013-08-27 11:23:09 +0300182 if (!evlist->nr_entries++)
183 perf_evlist__set_id_pos(evlist);
Adrian Hunter44c42d72015-09-08 10:58:59 +0300184
185 __perf_evlist__propagate_maps(evlist, entry);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200186}
187
Adrian Hunter47682302015-09-25 16:15:53 +0300188void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel)
189{
190 evsel->evlist = NULL;
191 list_del_init(&evsel->node);
192 evlist->nr_entries -= 1;
193}
194
Jiri Olsa0529bc12012-01-27 15:34:20 +0100195void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
Adrian Hunterf114d6e2015-09-08 10:58:53 +0300196 struct list_head *list)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200197{
Adrian Hunterf114d6e2015-09-08 10:58:53 +0300198 struct perf_evsel *evsel, *temp;
Adrian Hunter75562572013-08-27 11:23:09 +0300199
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300200 __evlist__for_each_entry_safe(list, temp, evsel) {
Adrian Hunterf114d6e2015-09-08 10:58:53 +0300201 list_del_init(&evsel->node);
202 perf_evlist__add(evlist, evsel);
203 }
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200204}
205
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300206void __perf_evlist__set_leader(struct list_head *list)
207{
208 struct perf_evsel *evsel, *leader;
209
210 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900211 evsel = list_entry(list->prev, struct perf_evsel, node);
212
213 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300214
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300215 __evlist__for_each_entry(list, evsel) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100216 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300217 }
218}
219
220void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200221{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900222 if (evlist->nr_entries) {
223 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300224 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900225 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200226}
227
Jiri Olsa45cf6c32015-10-05 20:06:04 +0200228void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr)
Arnaldo Carvalho de Melo7f8d1ad2015-09-30 17:49:49 -0300229{
230 attr->precise_ip = 3;
231
232 while (attr->precise_ip != 0) {
233 int fd = sys_perf_event_open(attr, 0, -1, -1, 0);
234 if (fd != -1) {
235 close(fd);
236 break;
237 }
238 --attr->precise_ip;
239 }
240}
241
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200242int perf_evlist__add_default(struct perf_evlist *evlist)
243{
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300244 struct perf_evsel *evsel = perf_evsel__new_cycles();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200245
246 if (evsel == NULL)
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300247 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200248
249 perf_evlist__add(evlist, evsel);
250 return 0;
251}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200252
Arnaldo Carvalho de Melo5bae0252016-01-07 13:14:56 -0300253int perf_evlist__add_dummy(struct perf_evlist *evlist)
254{
255 struct perf_event_attr attr = {
256 .type = PERF_TYPE_SOFTWARE,
257 .config = PERF_COUNT_SW_DUMMY,
258 .size = sizeof(attr), /* to capture ABI version */
259 };
260 struct perf_evsel *evsel = perf_evsel__new(&attr);
261
262 if (evsel == NULL)
263 return -ENOMEM;
264
265 perf_evlist__add(evlist, evsel);
266 return 0;
267}
268
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300269static int perf_evlist__add_attrs(struct perf_evlist *evlist,
270 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200271{
272 struct perf_evsel *evsel, *n;
273 LIST_HEAD(head);
274 size_t i;
275
276 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300277 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200278 if (evsel == NULL)
279 goto out_delete_partial_list;
280 list_add_tail(&evsel->node, &head);
281 }
282
Adrian Hunterf114d6e2015-09-08 10:58:53 +0300283 perf_evlist__splice_list_tail(evlist, &head);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200284
285 return 0;
286
287out_delete_partial_list:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300288 __evlist__for_each_entry_safe(&head, n, evsel)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200289 perf_evsel__delete(evsel);
290 return -1;
291}
292
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300293int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
294 struct perf_event_attr *attrs, size_t nr_attrs)
295{
296 size_t i;
297
298 for (i = 0; i < nr_attrs; i++)
299 event_attr_init(attrs + i);
300
301 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
302}
303
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300304struct perf_evsel *
305perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200306{
307 struct perf_evsel *evsel;
308
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300309 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200310 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
311 (int)evsel->attr.config == id)
312 return evsel;
313 }
314
315 return NULL;
316}
317
David Aherna2f28042013-08-28 22:29:51 -0600318struct perf_evsel *
319perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
320 const char *name)
321{
322 struct perf_evsel *evsel;
323
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300324 evlist__for_each_entry(evlist, evsel) {
David Aherna2f28042013-08-28 22:29:51 -0600325 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
326 (strcmp(evsel->name, name) == 0))
327 return evsel;
328 }
329
330 return NULL;
331}
332
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300333int perf_evlist__add_newtp(struct perf_evlist *evlist,
334 const char *sys, const char *name, void *handler)
335{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300336 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300337
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200338 if (IS_ERR(evsel))
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300339 return -1;
340
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300341 evsel->handler = handler;
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300342 perf_evlist__add(evlist, evsel);
343 return 0;
344}
345
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300346static int perf_evlist__nr_threads(struct perf_evlist *evlist,
347 struct perf_evsel *evsel)
348{
349 if (evsel->system_wide)
350 return 1;
351 else
352 return thread_map__nr(evlist->threads);
353}
354
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300355void perf_evlist__disable(struct perf_evlist *evlist)
356{
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300357 struct perf_evsel *pos;
358
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300359 evlist__for_each_entry(evlist, pos) {
Jiri Olsa3e27c922015-12-03 10:06:42 +0100360 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
361 continue;
362 perf_evsel__disable(pos);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300363 }
Arnaldo Carvalho de Melo2b56bcf2015-06-17 16:40:26 -0300364
365 evlist->enabled = false;
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300366}
367
David Ahern764e16a32011-08-25 10:17:55 -0600368void perf_evlist__enable(struct perf_evlist *evlist)
369{
David Ahern764e16a32011-08-25 10:17:55 -0600370 struct perf_evsel *pos;
371
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300372 evlist__for_each_entry(evlist, pos) {
Jiri Olsa3e27c922015-12-03 10:06:42 +0100373 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
374 continue;
375 perf_evsel__enable(pos);
David Ahern764e16a32011-08-25 10:17:55 -0600376 }
Arnaldo Carvalho de Melo2b56bcf2015-06-17 16:40:26 -0300377
378 evlist->enabled = true;
379}
380
381void perf_evlist__toggle_enable(struct perf_evlist *evlist)
382{
383 (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
David Ahern764e16a32011-08-25 10:17:55 -0600384}
385
Adrian Hunter1c650562014-07-31 09:00:56 +0300386static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
387 struct perf_evsel *evsel, int cpu)
388{
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -0300389 int thread;
Adrian Hunter1c650562014-07-31 09:00:56 +0300390 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
391
392 if (!evsel->fd)
393 return -EINVAL;
394
395 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -0300396 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
Adrian Hunter1c650562014-07-31 09:00:56 +0300397 if (err)
398 return err;
399 }
400 return 0;
401}
402
403static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
404 struct perf_evsel *evsel,
405 int thread)
406{
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -0300407 int cpu;
Adrian Hunter1c650562014-07-31 09:00:56 +0300408 int nr_cpus = cpu_map__nr(evlist->cpus);
409
410 if (!evsel->fd)
411 return -EINVAL;
412
413 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -0300414 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
Adrian Hunter1c650562014-07-31 09:00:56 +0300415 if (err)
416 return err;
417 }
418 return 0;
419}
420
421int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
422 struct perf_evsel *evsel, int idx)
423{
424 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
425
426 if (per_cpu_mmaps)
427 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
428 else
429 return perf_evlist__enable_event_thread(evlist, evsel, idx);
430}
431
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300432int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200433{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900434 int nr_cpus = cpu_map__nr(evlist->cpus);
435 int nr_threads = thread_map__nr(evlist->threads);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300436 int nfds = 0;
437 struct perf_evsel *evsel;
438
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300439 evlist__for_each_entry(evlist, evsel) {
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300440 if (evsel->system_wide)
441 nfds += nr_cpus;
442 else
443 nfds += nr_cpus * nr_threads;
444 }
445
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300446 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
447 fdarray__grow(&evlist->pollfd, nfds) < 0)
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300448 return -ENOMEM;
449
450 return 0;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200451}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200452
Wang Nan48760752016-07-14 08:34:37 +0000453static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd,
454 struct perf_mmap *map, short revent)
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300455{
Wang Nanf3058a12016-05-24 02:28:59 +0000456 int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP);
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300457 /*
458 * Save the idx so that when we filter out fds POLLHUP'ed we can
459 * close the associated evlist->mmap[] entry.
460 */
461 if (pos >= 0) {
Wang Nan48760752016-07-14 08:34:37 +0000462 evlist->pollfd.priv[pos].ptr = map;
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300463
464 fcntl(fd, F_SETFL, O_NONBLOCK);
465 }
466
467 return pos;
468}
469
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300470int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200471{
Wang Nan48760752016-07-14 08:34:37 +0000472 return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN);
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300473}
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300474
Wang Nan258e4bf2016-05-25 13:44:57 +0000475static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
476 void *arg __maybe_unused)
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300477{
Wang Nan48760752016-07-14 08:34:37 +0000478 struct perf_mmap *map = fda->priv[fd].ptr;
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300479
Wang Nan48760752016-07-14 08:34:37 +0000480 if (map)
481 perf_mmap__put(map);
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200482}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200483
Arnaldo Carvalho de Melo1ddec7f2014-08-12 23:04:11 -0300484int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
485{
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300486 return fdarray__filter(&evlist->pollfd, revents_and_mask,
Wang Nan258e4bf2016-05-25 13:44:57 +0000487 perf_evlist__munmap_filtered, NULL);
Arnaldo Carvalho de Melo1ddec7f2014-08-12 23:04:11 -0300488}
489
Arnaldo Carvalho de Melof66a8892014-08-18 17:25:59 -0300490int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
491{
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300492 return fdarray__poll(&evlist->pollfd, timeout);
Arnaldo Carvalho de Melof66a8892014-08-18 17:25:59 -0300493}
494
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300495static void perf_evlist__id_hash(struct perf_evlist *evlist,
496 struct perf_evsel *evsel,
497 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200498{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300499 int hash;
500 struct perf_sample_id *sid = SID(evsel, cpu, thread);
501
502 sid->id = id;
503 sid->evsel = evsel;
504 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
505 hlist_add_head(&sid->node, &evlist->heads[hash]);
506}
507
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300508void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
509 int cpu, int thread, u64 id)
510{
511 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
512 evsel->id[evsel->ids++] = id;
513}
514
Jiri Olsa1c596122015-11-05 15:40:49 +0100515int perf_evlist__id_add_fd(struct perf_evlist *evlist,
516 struct perf_evsel *evsel,
517 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300518{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200519 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300520 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200521 u64 id;
522 int ret;
523
524 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
525 if (!ret)
526 goto add;
527
528 if (errno != ENOTTY)
529 return -1;
530
531 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200532
Jiri Olsac4861af2012-10-12 13:02:21 +0200533 /*
534 * This way does not work with group format read, so bail
535 * out in that case.
536 */
537 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
538 return -1;
539
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200540 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
541 read(fd, &read_data, sizeof(read_data)) == -1)
542 return -1;
543
544 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
545 ++id_idx;
546 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
547 ++id_idx;
548
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200549 id = read_data[id_idx];
550
551 add:
552 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200553 return 0;
554}
555
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200556static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
557 struct perf_evsel *evsel, int idx, int cpu,
558 int thread)
559{
560 struct perf_sample_id *sid = SID(evsel, cpu, thread);
561 sid->idx = idx;
562 if (evlist->cpus && cpu >= 0)
563 sid->cpu = evlist->cpus->map[cpu];
564 else
565 sid->cpu = -1;
566 if (!evsel->system_wide && evlist->threads && thread >= 0)
Jiri Olsae13798c2015-06-23 00:36:02 +0200567 sid->tid = thread_map__pid(evlist->threads, thread);
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200568 else
569 sid->tid = -1;
570}
571
Jiri Olsa932a3592012-10-11 14:10:35 +0200572struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200573{
574 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200575 struct perf_sample_id *sid;
576 int hash;
577
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200578 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
579 head = &evlist->heads[hash];
580
Sasha Levinb67bfe02013-02-27 17:06:00 -0800581 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200582 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200583 return sid;
584
585 return NULL;
586}
587
588struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
589{
590 struct perf_sample_id *sid;
591
Adrian Hunter05169df2015-08-20 11:26:45 +0300592 if (evlist->nr_entries == 1 || !id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200593 return perf_evlist__first(evlist);
594
595 sid = perf_evlist__id2sid(evlist, id);
596 if (sid)
597 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900598
599 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300600 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900601
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200602 return NULL;
603}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200604
Adrian Hunterdddcf6a2015-09-25 16:15:52 +0300605struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
606 u64 id)
607{
608 struct perf_sample_id *sid;
609
610 if (!id)
611 return NULL;
612
613 sid = perf_evlist__id2sid(evlist, id);
614 if (sid)
615 return sid->evsel;
616
617 return NULL;
618}
619
Adrian Hunter75562572013-08-27 11:23:09 +0300620static int perf_evlist__event2id(struct perf_evlist *evlist,
621 union perf_event *event, u64 *id)
622{
623 const u64 *array = event->sample.array;
624 ssize_t n;
625
626 n = (event->header.size - sizeof(event->header)) >> 3;
627
628 if (event->header.type == PERF_RECORD_SAMPLE) {
629 if (evlist->id_pos >= n)
630 return -1;
631 *id = array[evlist->id_pos];
632 } else {
633 if (evlist->is_pos > n)
634 return -1;
635 n -= evlist->is_pos;
636 *id = array[n];
637 }
638 return 0;
639}
640
Jiri Olsa7cb5c5a2016-07-10 13:07:53 +0200641struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
642 union perf_event *event)
Adrian Hunter75562572013-08-27 11:23:09 +0300643{
Adrian Hunter98be6962013-09-04 23:18:17 +0300644 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300645 struct hlist_head *head;
646 struct perf_sample_id *sid;
647 int hash;
648 u64 id;
649
650 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300651 return first;
652
653 if (!first->attr.sample_id_all &&
654 event->header.type != PERF_RECORD_SAMPLE)
655 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300656
657 if (perf_evlist__event2id(evlist, event, &id))
658 return NULL;
659
660 /* Synthesized events have an id of zero */
661 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300662 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300663
664 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
665 head = &evlist->heads[hash];
666
667 hlist_for_each_entry(sid, head, node) {
668 if (sid->id == id)
669 return sid->evsel;
670 }
671 return NULL;
672}
673
Wang Nan65aea232016-05-23 07:13:38 +0000674static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value)
675{
676 int i;
677
Wang Nan078c3382016-07-14 08:34:40 +0000678 if (!evlist->backward_mmap)
679 return 0;
680
Wang Nan65aea232016-05-23 07:13:38 +0000681 for (i = 0; i < evlist->nr_mmaps; i++) {
Wang Nan078c3382016-07-14 08:34:40 +0000682 int fd = evlist->backward_mmap[i].fd;
Wang Nan65aea232016-05-23 07:13:38 +0000683 int err;
684
685 if (fd < 0)
686 continue;
687 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
688 if (err)
689 return err;
690 }
691 return 0;
692}
693
Wang Nanf6cdff82016-07-14 08:34:44 +0000694static int perf_evlist__pause(struct perf_evlist *evlist)
Wang Nan65aea232016-05-23 07:13:38 +0000695{
696 return perf_evlist__set_paused(evlist, true);
697}
698
Wang Nanf6cdff82016-07-14 08:34:44 +0000699static int perf_evlist__resume(struct perf_evlist *evlist)
Wang Nan65aea232016-05-23 07:13:38 +0000700{
701 return perf_evlist__set_paused(evlist, false);
702}
703
Wang Nanb6b85da2016-04-27 02:19:21 +0000704/* When check_messup is true, 'end' must points to a good entry */
Wang Nan0f4ccd12016-04-27 02:19:20 +0000705static union perf_event *
Wang Nanb6b85da2016-04-27 02:19:21 +0000706perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start,
707 u64 end, u64 *prev)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200708{
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200709 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200710 union perf_event *event = NULL;
Wang Nanb6b85da2016-04-27 02:19:21 +0000711 int diff = end - start;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200712
Wang Nanb6b85da2016-04-27 02:19:21 +0000713 if (check_messup) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200714 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200715 * If we're further behind than half the buffer, there's a chance
716 * the writer will bite our tail and mess up the samples under us.
717 *
Wang Nanb6b85da2016-04-27 02:19:21 +0000718 * If we somehow ended up ahead of the 'end', we got messed up.
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200719 *
Wang Nanb6b85da2016-04-27 02:19:21 +0000720 * In either case, truncate and restart at 'end'.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200721 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200722 if (diff > md->mask / 2 || diff < 0) {
723 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
724
725 /*
Wang Nanb6b85da2016-04-27 02:19:21 +0000726 * 'end' points to a known good entry, start there.
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200727 */
Wang Nanb6b85da2016-04-27 02:19:21 +0000728 start = end;
Wang Nanb04b7022016-04-26 02:28:54 +0000729 diff = 0;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200730 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200731 }
732
Wang Nanb04b7022016-04-26 02:28:54 +0000733 if (diff >= (int)sizeof(event->header)) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200734 size_t size;
735
Wang Nanb6b85da2016-04-27 02:19:21 +0000736 event = (union perf_event *)&data[start & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200737 size = event->header.size;
738
Wang Nanb04b7022016-04-26 02:28:54 +0000739 if (size < sizeof(event->header) || diff < (int)size) {
740 event = NULL;
741 goto broken_event;
742 }
743
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200744 /*
745 * Event straddles the mmap boundary -- header should always
746 * be inside due to u64 alignment of output.
747 */
Wang Nanb6b85da2016-04-27 02:19:21 +0000748 if ((start & md->mask) + size != ((start + size) & md->mask)) {
749 unsigned int offset = start;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200750 unsigned int len = min(sizeof(*event), size), cpy;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200751 void *dst = md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200752
753 do {
754 cpy = min(md->mask + 1 - (offset & md->mask), len);
755 memcpy(dst, &data[offset & md->mask], cpy);
756 offset += cpy;
757 dst += cpy;
758 len -= cpy;
759 } while (len);
760
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200761 event = (union perf_event *) md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200762 }
763
Wang Nanb6b85da2016-04-27 02:19:21 +0000764 start += size;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200765 }
766
Wang Nanb04b7022016-04-26 02:28:54 +0000767broken_event:
Wang Nan0f4ccd12016-04-27 02:19:20 +0000768 if (prev)
Wang Nanb6b85da2016-04-27 02:19:21 +0000769 *prev = start;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200770
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200771 return event;
772}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200773
Wang Nan8db6d6b2016-07-14 08:34:35 +0000774union perf_event *perf_mmap__read_forward(struct perf_mmap *md, bool check_messup)
Wang Nan0f4ccd12016-04-27 02:19:20 +0000775{
Wang Nan0f4ccd12016-04-27 02:19:20 +0000776 u64 head;
777 u64 old = md->prev;
778
779 /*
780 * Check if event was unmapped due to a POLLHUP/POLLERR.
781 */
Elena Reshetova25a37202017-02-21 17:35:01 +0200782 if (!refcount_read(&md->refcnt))
Wang Nan0f4ccd12016-04-27 02:19:20 +0000783 return NULL;
784
785 head = perf_mmap__read_head(md);
786
Wang Nan8db6d6b2016-07-14 08:34:35 +0000787 return perf_mmap__read(md, check_messup, old, head, &md->prev);
Wang Nan0f4ccd12016-04-27 02:19:20 +0000788}
789
Wang Nane24c7522016-05-09 01:47:50 +0000790union perf_event *
Wang Nan8db6d6b2016-07-14 08:34:35 +0000791perf_mmap__read_backward(struct perf_mmap *md)
Wang Nane24c7522016-05-09 01:47:50 +0000792{
Wang Nane24c7522016-05-09 01:47:50 +0000793 u64 head, end;
794 u64 start = md->prev;
795
796 /*
797 * Check if event was unmapped due to a POLLHUP/POLLERR.
798 */
Elena Reshetova25a37202017-02-21 17:35:01 +0200799 if (!refcount_read(&md->refcnt))
Wang Nane24c7522016-05-09 01:47:50 +0000800 return NULL;
801
802 head = perf_mmap__read_head(md);
803 if (!head)
804 return NULL;
805
806 /*
807 * 'head' pointer starts from 0. Kernel minus sizeof(record) form
808 * it each time when kernel writes to it, so in fact 'head' is
809 * negative. 'end' pointer is made manually by adding the size of
810 * the ring buffer to 'head' pointer, means the validate data can
811 * read is the whole ring buffer. If 'end' is positive, the ring
812 * buffer has not fully filled, so we must adjust 'end' to 0.
813 *
814 * However, since both 'head' and 'end' is unsigned, we can't
815 * simply compare 'end' against 0. Here we compare '-head' and
816 * the size of the ring buffer, where -head is the number of bytes
817 * kernel write to the ring buffer.
818 */
819 if (-head < (u64)(md->mask + 1))
820 end = 0;
821 else
822 end = head + md->mask + 1;
823
824 return perf_mmap__read(md, false, start, end, &md->prev);
825}
826
Wang Nan8db6d6b2016-07-14 08:34:35 +0000827union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx)
828{
829 struct perf_mmap *md = &evlist->mmap[idx];
830
831 /*
832 * Check messup is required for forward overwritable ring buffer:
833 * memory pointed by md->prev can be overwritten in this case.
834 * No need for read-write ring buffer: kernel stop outputting when
835 * it hit md->prev (perf_mmap__consume()).
836 */
837 return perf_mmap__read_forward(md, evlist->overwrite);
838}
839
840union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx)
841{
842 struct perf_mmap *md = &evlist->mmap[idx];
843
844 /*
845 * No need to check messup for backward ring buffer:
846 * We can always read arbitrary long data from a backward
847 * ring buffer unless we forget to pause it before reading.
848 */
849 return perf_mmap__read_backward(md);
850}
851
Wang Nan5a5ddeb2016-05-25 13:44:50 +0000852union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
853{
Wang Nana0c6f452016-07-14 08:34:41 +0000854 return perf_evlist__mmap_read_forward(evlist, idx);
Wang Nan5a5ddeb2016-05-25 13:44:50 +0000855}
856
Wang Nan8db6d6b2016-07-14 08:34:35 +0000857void perf_mmap__read_catchup(struct perf_mmap *md)
Wang Nane24c7522016-05-09 01:47:50 +0000858{
Wang Nane24c7522016-05-09 01:47:50 +0000859 u64 head;
860
Elena Reshetova25a37202017-02-21 17:35:01 +0200861 if (!refcount_read(&md->refcnt))
Wang Nane24c7522016-05-09 01:47:50 +0000862 return;
863
864 head = perf_mmap__read_head(md);
865 md->prev = head;
866}
867
Wang Nan8db6d6b2016-07-14 08:34:35 +0000868void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx)
869{
870 perf_mmap__read_catchup(&evlist->mmap[idx]);
871}
872
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300873static bool perf_mmap__empty(struct perf_mmap *md)
874{
Adrian Hunterb72e74d2015-04-24 22:29:43 +0300875 return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300876}
877
Wang Nan8db6d6b2016-07-14 08:34:35 +0000878static void perf_mmap__get(struct perf_mmap *map)
879{
Elena Reshetova25a37202017-02-21 17:35:01 +0200880 refcount_inc(&map->refcnt);
Wang Nan8db6d6b2016-07-14 08:34:35 +0000881}
882
883static void perf_mmap__put(struct perf_mmap *md)
884{
Elena Reshetova25a37202017-02-21 17:35:01 +0200885 BUG_ON(md->base && refcount_read(&md->refcnt) == 0);
Wang Nan8db6d6b2016-07-14 08:34:35 +0000886
Elena Reshetova25a37202017-02-21 17:35:01 +0200887 if (refcount_dec_and_test(&md->refcnt))
Wang Nan8db6d6b2016-07-14 08:34:35 +0000888 perf_mmap__munmap(md);
889}
890
Wang Nan8db6d6b2016-07-14 08:34:35 +0000891void perf_mmap__consume(struct perf_mmap *md, bool overwrite)
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800892{
Wang Nan8db6d6b2016-07-14 08:34:35 +0000893 if (!overwrite) {
David Ahern7b8283b52015-04-07 09:20:37 -0600894 u64 old = md->prev;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800895
896 perf_mmap__write_tail(md, old);
897 }
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300898
Elena Reshetova25a37202017-02-21 17:35:01 +0200899 if (refcount_read(&md->refcnt) == 1 && perf_mmap__empty(md))
Wang Nan8db6d6b2016-07-14 08:34:35 +0000900 perf_mmap__put(md);
901}
902
903void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
904{
905 perf_mmap__consume(&evlist->mmap[idx], evlist->overwrite);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800906}
907
Adrian Hunter718c6022015-04-09 18:53:42 +0300908int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
909 struct auxtrace_mmap_params *mp __maybe_unused,
910 void *userpg __maybe_unused,
911 int fd __maybe_unused)
912{
913 return 0;
914}
915
916void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
917{
918}
919
920void __weak auxtrace_mmap_params__init(
921 struct auxtrace_mmap_params *mp __maybe_unused,
922 off_t auxtrace_offset __maybe_unused,
923 unsigned int auxtrace_pages __maybe_unused,
924 bool auxtrace_overwrite __maybe_unused)
925{
926}
927
928void __weak auxtrace_mmap_params__set_idx(
929 struct auxtrace_mmap_params *mp __maybe_unused,
930 struct perf_evlist *evlist __maybe_unused,
931 int idx __maybe_unused,
932 bool per_cpu __maybe_unused)
933{
934}
935
Wang Nan8db6d6b2016-07-14 08:34:35 +0000936static void perf_mmap__munmap(struct perf_mmap *map)
937{
938 if (map->base != NULL) {
939 munmap(map->base, perf_mmap__mmap_len(map));
940 map->base = NULL;
941 map->fd = -1;
Elena Reshetova25a37202017-02-21 17:35:01 +0200942 refcount_set(&map->refcnt, 0);
Wang Nan8db6d6b2016-07-14 08:34:35 +0000943 }
944 auxtrace_mmap__munmap(&map->auxtrace_mmap);
945}
946
Wang Nana1f72612016-07-14 08:34:38 +0000947static void perf_evlist__munmap_nofree(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200948{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300949 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200950
Wang Nanb2cb6152016-07-14 08:34:39 +0000951 if (evlist->mmap)
952 for (i = 0; i < evlist->nr_mmaps; i++)
953 perf_mmap__munmap(&evlist->mmap[i]);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300954
Wang Nanb2cb6152016-07-14 08:34:39 +0000955 if (evlist->backward_mmap)
956 for (i = 0; i < evlist->nr_mmaps; i++)
957 perf_mmap__munmap(&evlist->backward_mmap[i]);
Wang Nana1f72612016-07-14 08:34:38 +0000958}
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300959
Wang Nana1f72612016-07-14 08:34:38 +0000960void perf_evlist__munmap(struct perf_evlist *evlist)
961{
962 perf_evlist__munmap_nofree(evlist);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300963 zfree(&evlist->mmap);
Wang Nanb2cb6152016-07-14 08:34:39 +0000964 zfree(&evlist->backward_mmap);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200965}
966
Wang Nan8db6d6b2016-07-14 08:34:35 +0000967static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200968{
Wang Nand4c6fb32016-05-20 16:38:24 +0000969 int i;
Wang Nan8db6d6b2016-07-14 08:34:35 +0000970 struct perf_mmap *map;
Wang Nand4c6fb32016-05-20 16:38:24 +0000971
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300972 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700973 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900974 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Wang Nan8db6d6b2016-07-14 08:34:35 +0000975 map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
976 if (!map)
977 return NULL;
Wang Nan946ae1d2016-05-31 13:06:15 +0000978
Arnaldo Carvalho de Melo4738ca32017-02-23 13:24:34 -0300979 for (i = 0; i < evlist->nr_mmaps; i++) {
Wang Nan8db6d6b2016-07-14 08:34:35 +0000980 map[i].fd = -1;
Arnaldo Carvalho de Melo4738ca32017-02-23 13:24:34 -0300981 /*
982 * When the perf_mmap() call is made we grab one refcount, plus
983 * one extra to let perf_evlist__mmap_consume() get the last
984 * events after all real references (perf_mmap__get()) are
985 * dropped.
986 *
987 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
988 * thus does perf_mmap__get() on it.
989 */
990 refcount_set(&map[i].refcnt, 0);
991 }
Wang Nan8db6d6b2016-07-14 08:34:35 +0000992 return map;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200993}
994
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300995struct mmap_params {
996 int prot;
997 int mask;
Adrian Hunter718c6022015-04-09 18:53:42 +0300998 struct auxtrace_mmap_params auxtrace_mp;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300999};
1000
Wang Nan8db6d6b2016-07-14 08:34:35 +00001001static int perf_mmap__mmap(struct perf_mmap *map,
1002 struct mmap_params *mp, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001003{
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -03001004 /*
1005 * The last one will be done at perf_evlist__mmap_consume(), so that we
1006 * make sure we don't prevent tools from consuming every last event in
1007 * the ring buffer.
1008 *
1009 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
1010 * anymore, but the last events for it are still in the ring buffer,
1011 * waiting to be consumed.
1012 *
1013 * Tools can chose to ignore this at their own discretion, but the
1014 * evlist layer can't just drop it when filtering events in
1015 * perf_evlist__filter_pollfd().
1016 */
Elena Reshetova25a37202017-02-21 17:35:01 +02001017 refcount_set(&map->refcnt, 2);
Wang Nan8db6d6b2016-07-14 08:34:35 +00001018 map->prev = 0;
1019 map->mask = mp->mask;
1020 map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot,
1021 MAP_SHARED, fd, 0);
1022 if (map->base == MAP_FAILED) {
Adrian Hunter02635962013-11-01 15:51:33 +02001023 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
1024 errno);
Wang Nan8db6d6b2016-07-14 08:34:35 +00001025 map->base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001026 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -05001027 }
Wang Nan8db6d6b2016-07-14 08:34:35 +00001028 map->fd = fd;
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -03001029
Wang Nan8db6d6b2016-07-14 08:34:35 +00001030 if (auxtrace_mmap__mmap(&map->auxtrace_mmap,
1031 &mp->auxtrace_mp, map->base, fd))
Adrian Hunter718c6022015-04-09 18:53:42 +03001032 return -1;
1033
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001034 return 0;
1035}
1036
Wang Nanf3058a12016-05-24 02:28:59 +00001037static bool
1038perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused,
1039 struct perf_evsel *evsel)
1040{
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001041 if (evsel->attr.write_backward)
Wang Nanf3058a12016-05-24 02:28:59 +00001042 return false;
1043 return true;
1044}
1045
Adrian Hunter04e21312013-10-18 15:29:13 +03001046static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
Mark Rutland9f21b812016-09-08 11:21:51 +01001047 struct mmap_params *mp, int cpu_idx,
Wang Nan078c3382016-07-14 08:34:40 +00001048 int thread, int *_output, int *_output_backward)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001049{
1050 struct perf_evsel *evsel;
Wang Nanf3058a12016-05-24 02:28:59 +00001051 int revent;
Mark Rutland9f21b812016-09-08 11:21:51 +01001052 int evlist_cpu = cpu_map__cpu(evlist->cpus, cpu_idx);
Adrian Hunter04e21312013-10-18 15:29:13 +03001053
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001054 evlist__for_each_entry(evlist, evsel) {
Wang Nan078c3382016-07-14 08:34:40 +00001055 struct perf_mmap *maps = evlist->mmap;
1056 int *output = _output;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001057 int fd;
Mark Rutland9f21b812016-09-08 11:21:51 +01001058 int cpu;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001059
Wang Nan078c3382016-07-14 08:34:40 +00001060 if (evsel->attr.write_backward) {
1061 output = _output_backward;
1062 maps = evlist->backward_mmap;
1063
1064 if (!maps) {
1065 maps = perf_evlist__alloc_mmap(evlist);
1066 if (!maps)
1067 return -1;
1068 evlist->backward_mmap = maps;
Wang Nan54cc54d2016-07-14 08:34:42 +00001069 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
1070 perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
Wang Nan078c3382016-07-14 08:34:40 +00001071 }
1072 }
Wang Nanf3058a12016-05-24 02:28:59 +00001073
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001074 if (evsel->system_wide && thread)
1075 continue;
1076
Mark Rutland9f21b812016-09-08 11:21:51 +01001077 cpu = cpu_map__idx(evsel->cpus, evlist_cpu);
1078 if (cpu == -1)
1079 continue;
1080
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001081 fd = FD(evsel, cpu, thread);
Adrian Hunter04e21312013-10-18 15:29:13 +03001082
1083 if (*output == -1) {
1084 *output = fd;
Wang Nan078c3382016-07-14 08:34:40 +00001085
1086 if (perf_mmap__mmap(&maps[idx], mp, *output) < 0)
Adrian Hunter04e21312013-10-18 15:29:13 +03001087 return -1;
1088 } else {
1089 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
1090 return -1;
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -03001091
Wang Nan078c3382016-07-14 08:34:40 +00001092 perf_mmap__get(&maps[idx]);
Adrian Hunter04e21312013-10-18 15:29:13 +03001093 }
1094
Wang Nanf3058a12016-05-24 02:28:59 +00001095 revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
1096
Adrian Hunterf90d1942014-11-11 16:16:39 +02001097 /*
1098 * The system_wide flag causes a selected event to be opened
1099 * always without a pid. Consequently it will never get a
1100 * POLLHUP, but it is used for tracking in combination with
1101 * other events, so it should not need to be polled anyway.
1102 * Therefore don't add it for polling.
1103 */
1104 if (!evsel->system_wide &&
Wang Nan078c3382016-07-14 08:34:40 +00001105 __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) {
1106 perf_mmap__put(&maps[idx]);
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -03001107 return -1;
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -03001108 }
Arnaldo Carvalho de Melo033fa712014-09-08 12:55:12 -03001109
Adrian Hunter3c659ee2014-10-27 15:49:22 +02001110 if (evsel->attr.read_format & PERF_FORMAT_ID) {
1111 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
1112 fd) < 0)
1113 return -1;
1114 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
1115 thread);
1116 }
Adrian Hunter04e21312013-10-18 15:29:13 +03001117 }
1118
1119 return 0;
1120}
1121
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001122static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
1123 struct mmap_params *mp)
Adrian Hunter04e21312013-10-18 15:29:13 +03001124{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001125 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +09001126 int nr_cpus = cpu_map__nr(evlist->cpus);
1127 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001128
Adrian Huntere3e1a542013-08-14 15:48:24 +03001129 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +09001130 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001131 int output = -1;
Wang Nan078c3382016-07-14 08:34:40 +00001132 int output_backward = -1;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001133
Adrian Hunter718c6022015-04-09 18:53:42 +03001134 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
1135 true);
1136
Namhyung Kimb3a319d2013-03-11 16:43:14 +09001137 for (thread = 0; thread < nr_threads; thread++) {
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001138 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
Wang Nan078c3382016-07-14 08:34:40 +00001139 thread, &output, &output_backward))
Adrian Hunter04e21312013-10-18 15:29:13 +03001140 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001141 }
1142 }
1143
1144 return 0;
1145
1146out_unmap:
Wang Nana1f72612016-07-14 08:34:38 +00001147 perf_evlist__munmap_nofree(evlist);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001148 return -1;
1149}
1150
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001151static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
1152 struct mmap_params *mp)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001153{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001154 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +09001155 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001156
Adrian Huntere3e1a542013-08-14 15:48:24 +03001157 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +09001158 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001159 int output = -1;
Wang Nan078c3382016-07-14 08:34:40 +00001160 int output_backward = -1;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001161
Adrian Hunter718c6022015-04-09 18:53:42 +03001162 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
1163 false);
1164
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001165 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
Wang Nan078c3382016-07-14 08:34:40 +00001166 &output, &output_backward))
Adrian Hunter04e21312013-10-18 15:29:13 +03001167 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001168 }
1169
1170 return 0;
1171
1172out_unmap:
Wang Nana1f72612016-07-14 08:34:38 +00001173 perf_evlist__munmap_nofree(evlist);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001174 return -1;
1175}
1176
Arnaldo Carvalho de Melof5e71502016-04-15 17:46:31 -03001177unsigned long perf_event_mlock_kb_in_pages(void)
1178{
1179 unsigned long pages;
1180 int max;
1181
1182 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
1183 /*
1184 * Pick a once upon a time good value, i.e. things look
1185 * strange since we can't read a sysctl value, but lets not
1186 * die yet...
1187 */
1188 max = 512;
1189 } else {
1190 max -= (page_size / 1024);
1191 }
1192
1193 pages = (max * 1024) / page_size;
1194 if (!is_power_of_2(pages))
1195 pages = rounddown_pow_of_two(pages);
1196
1197 return pages;
1198}
1199
Jiri Olsa0c582442017-01-09 10:51:59 +01001200size_t perf_evlist__mmap_size(unsigned long pages)
Jiri Olsa994a1f72013-09-01 12:36:12 +02001201{
Arnaldo Carvalho de Melof5e71502016-04-15 17:46:31 -03001202 if (pages == UINT_MAX)
1203 pages = perf_event_mlock_kb_in_pages();
1204 else if (!is_power_of_2(pages))
Jiri Olsa994a1f72013-09-01 12:36:12 +02001205 return 0;
1206
1207 return (pages + 1) * page_size;
1208}
1209
David Ahern33c2dcf2013-11-12 07:46:55 -07001210static long parse_pages_arg(const char *str, unsigned long min,
1211 unsigned long max)
Jiri Olsa994a1f72013-09-01 12:36:12 +02001212{
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +03001213 unsigned long pages, val;
Jiri Olsa27050f52013-09-01 12:36:13 +02001214 static struct parse_tag tags[] = {
1215 { .tag = 'B', .mult = 1 },
1216 { .tag = 'K', .mult = 1 << 10 },
1217 { .tag = 'M', .mult = 1 << 20 },
1218 { .tag = 'G', .mult = 1 << 30 },
1219 { .tag = 0 },
1220 };
Jiri Olsa994a1f72013-09-01 12:36:12 +02001221
David Ahern89735042013-11-12 07:46:53 -07001222 if (str == NULL)
David Ahern33c2dcf2013-11-12 07:46:55 -07001223 return -EINVAL;
David Ahern89735042013-11-12 07:46:53 -07001224
Jiri Olsa27050f52013-09-01 12:36:13 +02001225 val = parse_tag_value(str, tags);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +03001226 if (val != (unsigned long) -1) {
Jiri Olsa27050f52013-09-01 12:36:13 +02001227 /* we got file size value */
1228 pages = PERF_ALIGN(val, page_size) / page_size;
Jiri Olsa27050f52013-09-01 12:36:13 +02001229 } else {
1230 /* we got pages count value */
1231 char *eptr;
1232 pages = strtoul(str, &eptr, 10);
David Ahern33c2dcf2013-11-12 07:46:55 -07001233 if (*eptr != '\0')
1234 return -EINVAL;
Jiri Olsa994a1f72013-09-01 12:36:12 +02001235 }
1236
Adrian Hunter2bcab6c2013-12-09 15:18:37 +02001237 if (pages == 0 && min == 0) {
David Ahern33c2dcf2013-11-12 07:46:55 -07001238 /* leave number of pages at 0 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +02001239 } else if (!is_power_of_2(pages)) {
Jiri Olsa98081432017-01-09 10:51:55 +01001240 char buf[100];
1241
David Ahern33c2dcf2013-11-12 07:46:55 -07001242 /* round pages up to next power of 2 */
Arnaldo Carvalho de Melo91529832014-12-16 13:24:41 -03001243 pages = roundup_pow_of_two(pages);
Adrian Hunter1dbfa9382013-12-09 15:18:39 +02001244 if (!pages)
1245 return -EINVAL;
Jiri Olsa98081432017-01-09 10:51:55 +01001246
1247 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
1248 pr_info("rounding mmap pages size to %s (%lu pages)\n",
1249 buf, pages);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +03001250 }
1251
David Ahern33c2dcf2013-11-12 07:46:55 -07001252 if (pages > max)
1253 return -EINVAL;
1254
1255 return pages;
1256}
1257
Adrian Huntere9db1312015-04-09 18:53:46 +03001258int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
David Ahern33c2dcf2013-11-12 07:46:55 -07001259{
David Ahern33c2dcf2013-11-12 07:46:55 -07001260 unsigned long max = UINT_MAX;
1261 long pages;
1262
Adrian Hunterf5ae9c42013-12-09 15:18:38 +02001263 if (max > SIZE_MAX / page_size)
David Ahern33c2dcf2013-11-12 07:46:55 -07001264 max = SIZE_MAX / page_size;
1265
1266 pages = parse_pages_arg(str, 1, max);
1267 if (pages < 0) {
1268 pr_err("Invalid argument for --mmap_pages/-m\n");
Jiri Olsa994a1f72013-09-01 12:36:12 +02001269 return -1;
1270 }
1271
1272 *mmap_pages = pages;
1273 return 0;
1274}
1275
Adrian Huntere9db1312015-04-09 18:53:46 +03001276int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1277 int unset __maybe_unused)
1278{
1279 return __perf_evlist__parse_mmap_pages(opt->value, str);
1280}
1281
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001282/**
Adrian Hunter718c6022015-04-09 18:53:42 +03001283 * perf_evlist__mmap_ex - Create mmaps to receive events.
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001284 * @evlist: list of events
1285 * @pages: map length in pages
1286 * @overwrite: overwrite older events?
Adrian Hunter718c6022015-04-09 18:53:42 +03001287 * @auxtrace_pages - auxtrace map length in pages
1288 * @auxtrace_overwrite - overwrite older auxtrace data?
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001289 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001290 * If @overwrite is %false the user needs to signal event consumption using
1291 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
1292 * automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001293 *
Adrian Hunter718c6022015-04-09 18:53:42 +03001294 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1295 * consumption using auxtrace_mmap__write_tail().
1296 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001297 * Return: %0 on success, negative error code otherwise.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001298 */
Adrian Hunter718c6022015-04-09 18:53:42 +03001299int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1300 bool overwrite, unsigned int auxtrace_pages,
1301 bool auxtrace_overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001302{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001303 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001304 const struct cpu_map *cpus = evlist->cpus;
1305 const struct thread_map *threads = evlist->threads;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001306 struct mmap_params mp = {
1307 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1308 };
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -02001309
Wang Nan8db6d6b2016-07-14 08:34:35 +00001310 if (!evlist->mmap)
1311 evlist->mmap = perf_evlist__alloc_mmap(evlist);
1312 if (!evlist->mmap)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001313 return -ENOMEM;
1314
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -03001315 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001316 return -ENOMEM;
1317
1318 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +02001319 evlist->mmap_len = perf_evlist__mmap_size(pages);
Adrian Hunter2af68ef2013-10-18 15:29:07 +03001320 pr_debug("mmap size %zuB\n", evlist->mmap_len);
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001321 mp.mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001322
Adrian Hunter718c6022015-04-09 18:53:42 +03001323 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1324 auxtrace_pages, auxtrace_overwrite);
1325
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001326 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001327 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001328 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -03001329 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001330 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001331 }
1332
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -07001333 if (cpu_map__empty(cpus))
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001334 return perf_evlist__mmap_per_thread(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001335
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001336 return perf_evlist__mmap_per_cpu(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001337}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001338
Adrian Hunter718c6022015-04-09 18:53:42 +03001339int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1340 bool overwrite)
1341{
1342 return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1343}
1344
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001345int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001346{
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001347 struct cpu_map *cpus;
1348 struct thread_map *threads;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001349
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001350 threads = thread_map__new_str(target->pid, target->tid, target->uid);
1351
1352 if (!threads)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001353 return -1;
1354
Dongsheng Yang9c105fb2013-12-04 17:56:40 -05001355 if (target__uses_dummy_map(target))
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001356 cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +09001357 else
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001358 cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001359
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001360 if (!cpus)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001361 goto out_delete_threads;
1362
Adrian Hunterec9a77a2015-09-08 10:58:52 +03001363 evlist->has_user_cpus = !!target->cpu_list;
1364
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001365 perf_evlist__set_maps(evlist, cpus, threads);
Adrian Hunterd5bc0562015-09-08 10:58:51 +03001366
1367 return 0;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001368
1369out_delete_threads:
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001370 thread_map__put(threads);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001371 return -1;
1372}
1373
Adrian Hunterd5bc0562015-09-08 10:58:51 +03001374void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
1375 struct thread_map *threads)
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001376{
Adrian Hunter934e0f22015-09-08 10:58:56 +03001377 /*
1378 * Allow for the possibility that one or another of the maps isn't being
1379 * changed i.e. don't put it. Note we are assuming the maps that are
1380 * being applied are brand new and evlist is taking ownership of the
1381 * original reference count of 1. If that is not the case it is up to
1382 * the caller to increase the reference count.
1383 */
1384 if (cpus != evlist->cpus) {
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001385 cpu_map__put(evlist->cpus);
Arnaldo Carvalho de Meloa55e5662016-02-17 10:57:19 -03001386 evlist->cpus = cpu_map__get(cpus);
Adrian Hunter934e0f22015-09-08 10:58:56 +03001387 }
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001388
Adrian Hunter934e0f22015-09-08 10:58:56 +03001389 if (threads != evlist->threads) {
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001390 thread_map__put(evlist->threads);
Arnaldo Carvalho de Meloa55e5662016-02-17 10:57:19 -03001391 evlist->threads = thread_map__get(threads);
Adrian Hunter934e0f22015-09-08 10:58:56 +03001392 }
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001393
Adrian Hunterec9a77a2015-09-08 10:58:52 +03001394 perf_evlist__propagate_maps(evlist);
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001395}
1396
Arnaldo Carvalho de Melo22c8a372016-04-11 18:37:45 -03001397void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
1398 enum perf_event_sample_format bit)
1399{
1400 struct perf_evsel *evsel;
1401
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001402 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo22c8a372016-04-11 18:37:45 -03001403 __perf_evsel__set_sample_bit(evsel, bit);
1404}
1405
1406void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
1407 enum perf_event_sample_format bit)
1408{
1409 struct perf_evsel *evsel;
1410
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001411 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo22c8a372016-04-11 18:37:45 -03001412 __perf_evsel__reset_sample_bit(evsel, bit);
1413}
1414
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001415int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001416{
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001417 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001418 int err = 0;
1419 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +09001420 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001421
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001422 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001423 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001424 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001425
Kan Liangd988d5e2015-08-21 02:23:14 -04001426 /*
1427 * filters only work for tracepoint event, which doesn't have cpu limit.
1428 * So evlist and evsel should always be same.
1429 */
Arnaldo Carvalho de Melof47805a2015-07-03 15:53:49 -03001430 err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001431 if (err) {
1432 *err_evsel = evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001433 break;
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001434 }
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001435 }
1436
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001437 return err;
1438}
1439
1440int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1441{
1442 struct perf_evsel *evsel;
1443 int err = 0;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001444
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001445 evlist__for_each_entry(evlist, evsel) {
Wang Nanfdf14722016-02-26 09:31:53 +00001446 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
1447 continue;
1448
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03001449 err = perf_evsel__set_filter(evsel, filter);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001450 if (err)
1451 break;
1452 }
1453
1454 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001455}
Frederic Weisbecker74429962011-05-21 17:49:00 +02001456
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001457int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001458{
1459 char *filter;
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001460 int ret = -1;
1461 size_t i;
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001462
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001463 for (i = 0; i < npids; ++i) {
1464 if (i == 0) {
1465 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1466 return -1;
1467 } else {
1468 char *tmp;
1469
1470 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1471 goto out_free;
1472
1473 free(filter);
1474 filter = tmp;
1475 }
1476 }
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001477
1478 ret = perf_evlist__set_filter(evlist, filter);
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001479out_free:
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001480 free(filter);
1481 return ret;
1482}
1483
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001484int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1485{
1486 return perf_evlist__set_filter_pids(evlist, 1, &pid);
1487}
1488
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001489bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001490{
Adrian Hunter75562572013-08-27 11:23:09 +03001491 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001492
Adrian Hunter75562572013-08-27 11:23:09 +03001493 if (evlist->nr_entries == 1)
1494 return true;
1495
1496 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1497 return false;
1498
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001499 evlist__for_each_entry(evlist, pos) {
Adrian Hunter75562572013-08-27 11:23:09 +03001500 if (pos->id_pos != evlist->id_pos ||
1501 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001502 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001503 }
1504
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001505 return true;
1506}
1507
Adrian Hunter75562572013-08-27 11:23:09 +03001508u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001509{
Adrian Hunter75562572013-08-27 11:23:09 +03001510 struct perf_evsel *evsel;
1511
1512 if (evlist->combined_sample_type)
1513 return evlist->combined_sample_type;
1514
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001515 evlist__for_each_entry(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +03001516 evlist->combined_sample_type |= evsel->attr.sample_type;
1517
1518 return evlist->combined_sample_type;
1519}
1520
1521u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1522{
1523 evlist->combined_sample_type = 0;
1524 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001525}
1526
Andi Kleen98df8582015-07-18 08:24:47 -07001527u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
1528{
1529 struct perf_evsel *evsel;
1530 u64 branch_type = 0;
1531
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001532 evlist__for_each_entry(evlist, evsel)
Andi Kleen98df8582015-07-18 08:24:47 -07001533 branch_type |= evsel->attr.branch_sample_type;
1534 return branch_type;
1535}
1536
Jiri Olsa9ede4732012-10-10 17:38:13 +02001537bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1538{
1539 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1540 u64 read_format = first->attr.read_format;
1541 u64 sample_type = first->attr.sample_type;
1542
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001543 evlist__for_each_entry(evlist, pos) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001544 if (read_format != pos->attr.read_format)
1545 return false;
1546 }
1547
1548 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1549 if ((sample_type & PERF_SAMPLE_READ) &&
1550 !(read_format & PERF_FORMAT_ID)) {
1551 return false;
1552 }
1553
1554 return true;
1555}
1556
1557u64 perf_evlist__read_format(struct perf_evlist *evlist)
1558{
1559 struct perf_evsel *first = perf_evlist__first(evlist);
1560 return first->attr.read_format;
1561}
1562
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001563u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -02001564{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001565 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -02001566 struct perf_sample *data;
1567 u64 sample_type;
1568 u16 size = 0;
1569
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -02001570 if (!first->attr.sample_id_all)
1571 goto out;
1572
1573 sample_type = first->attr.sample_type;
1574
1575 if (sample_type & PERF_SAMPLE_TID)
1576 size += sizeof(data->tid) * 2;
1577
1578 if (sample_type & PERF_SAMPLE_TIME)
1579 size += sizeof(data->time);
1580
1581 if (sample_type & PERF_SAMPLE_ID)
1582 size += sizeof(data->id);
1583
1584 if (sample_type & PERF_SAMPLE_STREAM_ID)
1585 size += sizeof(data->stream_id);
1586
1587 if (sample_type & PERF_SAMPLE_CPU)
1588 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +03001589
1590 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1591 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -02001592out:
1593 return size;
1594}
1595
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001596bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001597{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001598 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001599
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001600 evlist__for_each_entry_continue(evlist, pos) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001601 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1602 return false;
1603 }
1604
1605 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001606}
1607
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001608bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001609{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001610 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001611 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001612}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001613
1614void perf_evlist__set_selected(struct perf_evlist *evlist,
1615 struct perf_evsel *evsel)
1616{
1617 evlist->selected = evsel;
1618}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001619
Namhyung Kima74b4b62013-03-15 14:48:48 +09001620void perf_evlist__close(struct perf_evlist *evlist)
1621{
1622 struct perf_evsel *evsel;
1623 int ncpus = cpu_map__nr(evlist->cpus);
1624 int nthreads = thread_map__nr(evlist->threads);
1625
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001626 evlist__for_each_entry_reverse(evlist, evsel) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001627 int n = evsel->cpus ? evsel->cpus->nr : ncpus;
Stephane Eranian8ad92192014-01-17 16:34:06 +01001628 perf_evsel__close(evsel, n, nthreads);
1629 }
Namhyung Kima74b4b62013-03-15 14:48:48 +09001630}
1631
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001632static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1633{
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001634 struct cpu_map *cpus;
1635 struct thread_map *threads;
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001636 int err = -ENOMEM;
1637
1638 /*
1639 * Try reading /sys/devices/system/cpu/online to get
1640 * an all cpus map.
1641 *
1642 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1643 * code needs an overhaul to properly forward the
1644 * error, and we may not want to do that fallback to a
1645 * default cpu identity map :-\
1646 */
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001647 cpus = cpu_map__new(NULL);
1648 if (!cpus)
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001649 goto out;
1650
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001651 threads = thread_map__new_dummy();
1652 if (!threads)
1653 goto out_put;
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001654
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001655 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001656out:
1657 return err;
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001658out_put:
1659 cpu_map__put(cpus);
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001660 goto out;
1661}
1662
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001663int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001664{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001665 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001666 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001667
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001668 /*
1669 * Default: one fd per CPU, all threads, aka systemwide
1670 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1671 */
1672 if (evlist->threads == NULL && evlist->cpus == NULL) {
1673 err = perf_evlist__create_syswide_maps(evlist);
1674 if (err < 0)
1675 goto out_err;
1676 }
1677
Adrian Hunter733cd2f2013-09-06 22:40:11 +03001678 perf_evlist__update_id_pos(evlist);
1679
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001680 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter23df7f72016-01-07 10:13:59 +01001681 err = perf_evsel__open(evsel, evsel->cpus, evsel->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001682 if (err < 0)
1683 goto out_err;
1684 }
1685
1686 return 0;
1687out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001688 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001689 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001690 return err;
1691}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001692
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001693int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001694 const char *argv[], bool pipe_output,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001695 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001696{
1697 int child_ready_pipe[2], go_pipe[2];
1698 char bf;
1699
1700 if (pipe(child_ready_pipe) < 0) {
1701 perror("failed to create 'ready' pipe");
1702 return -1;
1703 }
1704
1705 if (pipe(go_pipe) < 0) {
1706 perror("failed to create 'go' pipe");
1707 goto out_close_ready_pipe;
1708 }
1709
1710 evlist->workload.pid = fork();
1711 if (evlist->workload.pid < 0) {
1712 perror("failed to fork");
1713 goto out_close_pipes;
1714 }
1715
1716 if (!evlist->workload.pid) {
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001717 int ret;
1718
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001719 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001720 dup2(2, 1);
1721
David Ahern0817df02013-05-25 17:50:39 -06001722 signal(SIGTERM, SIG_DFL);
1723
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001724 close(child_ready_pipe[0]);
1725 close(go_pipe[1]);
1726 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1727
1728 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001729 * Tell the parent we're ready to go
1730 */
1731 close(child_ready_pipe[1]);
1732
1733 /*
1734 * Wait until the parent tells us to go.
1735 */
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001736 ret = read(go_pipe[0], &bf, 1);
1737 /*
1738 * The parent will ask for the execvp() to be performed by
1739 * writing exactly one byte, in workload.cork_fd, usually via
1740 * perf_evlist__start_workload().
1741 *
Arnaldo Carvalho de Melo20f86fc2015-02-03 13:29:05 -03001742 * For cancelling the workload without actually running it,
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001743 * the parent will just close workload.cork_fd, without writing
1744 * anything, i.e. read will return zero and we just exit()
1745 * here.
1746 */
1747 if (ret != 1) {
1748 if (ret == -1)
1749 perror("unable to read pipe");
1750 exit(ret);
1751 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001752
1753 execvp(argv[0], (char **)argv);
1754
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001755 if (exec_error) {
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001756 union sigval val;
1757
1758 val.sival_int = errno;
1759 if (sigqueue(getppid(), SIGUSR1, val))
1760 perror(argv[0]);
1761 } else
1762 perror(argv[0]);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001763 exit(-1);
1764 }
1765
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001766 if (exec_error) {
1767 struct sigaction act = {
1768 .sa_flags = SA_SIGINFO,
1769 .sa_sigaction = exec_error,
1770 };
1771 sigaction(SIGUSR1, &act, NULL);
1772 }
1773
Arnaldo Carvalho de Melo1aaf63b2014-10-10 14:29:49 -03001774 if (target__none(target)) {
1775 if (evlist->threads == NULL) {
1776 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1777 __func__, __LINE__);
1778 goto out_close_pipes;
1779 }
Jiri Olsae13798c2015-06-23 00:36:02 +02001780 thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
Arnaldo Carvalho de Melo1aaf63b2014-10-10 14:29:49 -03001781 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001782
1783 close(child_ready_pipe[1]);
1784 close(go_pipe[0]);
1785 /*
1786 * wait for child to settle
1787 */
1788 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1789 perror("unable to read pipe");
1790 goto out_close_pipes;
1791 }
1792
Namhyung Kimbcf31452013-06-26 16:14:15 +09001793 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001794 evlist->workload.cork_fd = go_pipe[1];
1795 close(child_ready_pipe[0]);
1796 return 0;
1797
1798out_close_pipes:
1799 close(go_pipe[0]);
1800 close(go_pipe[1]);
1801out_close_ready_pipe:
1802 close(child_ready_pipe[0]);
1803 close(child_ready_pipe[1]);
1804 return -1;
1805}
1806
1807int perf_evlist__start_workload(struct perf_evlist *evlist)
1808{
1809 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001810 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001811 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001812 /*
1813 * Remove the cork, let it rip!
1814 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001815 ret = write(evlist->workload.cork_fd, &bf, 1);
1816 if (ret < 0)
Soramichi Akiyamae978be92017-01-10 10:41:00 -03001817 perror("unable to write to pipe");
Namhyung Kimbcf31452013-06-26 16:14:15 +09001818
1819 close(evlist->workload.cork_fd);
1820 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001821 }
1822
1823 return 0;
1824}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001825
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001826int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001827 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001828{
Adrian Hunter75562572013-08-27 11:23:09 +03001829 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1830
1831 if (!evsel)
1832 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001833 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001834}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001835
1836size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1837{
1838 struct perf_evsel *evsel;
1839 size_t printed = 0;
1840
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001841 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001842 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1843 perf_evsel__name(evsel));
1844 }
1845
Davidlohr Buesob2222132013-11-12 22:24:24 -08001846 return printed + fprintf(fp, "\n");
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001847}
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03001848
Arnaldo Carvalho de Melod9aade7f2016-02-18 13:34:09 -03001849int perf_evlist__strerror_open(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001850 int err, char *buf, size_t size)
1851{
1852 int printed, value;
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001853 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001854
1855 switch (err) {
1856 case EACCES:
1857 case EPERM:
1858 printed = scnprintf(buf, size,
1859 "Error:\t%s.\n"
1860 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1861
Adrian Hunter1a472452013-12-11 14:36:23 +02001862 value = perf_event_paranoid();
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001863
1864 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1865
1866 if (value >= 2) {
1867 printed += scnprintf(buf + printed, size - printed,
1868 "For your workloads it needs to be <= 1\nHint:\t");
1869 }
1870 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001871 "For system wide tracing it needs to be set to -1.\n");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001872
1873 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001874 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1875 "Hint:\tThe current value is %d.", value);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001876 break;
Arnaldo Carvalho de Melod9aade7f2016-02-18 13:34:09 -03001877 case EINVAL: {
1878 struct perf_evsel *first = perf_evlist__first(evlist);
1879 int max_freq;
1880
1881 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1882 goto out_default;
1883
1884 if (first->attr.sample_freq < (u64)max_freq)
1885 goto out_default;
1886
1887 printed = scnprintf(buf, size,
1888 "Error:\t%s.\n"
1889 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1890 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1891 emsg, max_freq, first->attr.sample_freq);
1892 break;
1893 }
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001894 default:
Arnaldo Carvalho de Melod9aade7f2016-02-18 13:34:09 -03001895out_default:
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001896 scnprintf(buf, size, "%s", emsg);
1897 break;
1898 }
1899
1900 return 0;
1901}
Adrian Huntera025e4f2013-12-11 14:36:35 +02001902
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001903int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1904{
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001905 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001906 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001907
1908 switch (err) {
1909 case EPERM:
Arnaldo Carvalho de Meloe5d4a292014-12-12 15:59:51 -03001910 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001911 printed += scnprintf(buf + printed, size - printed,
1912 "Error:\t%s.\n"
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001913 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001914 "Hint:\tTried using %zd kB.\n",
Arnaldo Carvalho de Meloe5d4a292014-12-12 15:59:51 -03001915 emsg, pages_max_per_user, pages_attempted);
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001916
1917 if (pages_attempted >= pages_max_per_user) {
1918 printed += scnprintf(buf + printed, size - printed,
1919 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1920 pages_max_per_user + pages_attempted);
1921 }
1922
1923 printed += scnprintf(buf + printed, size - printed,
1924 "Hint:\tTry using a smaller -m/--mmap-pages value.");
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001925 break;
1926 default:
1927 scnprintf(buf, size, "%s", emsg);
1928 break;
1929 }
1930
1931 return 0;
1932}
1933
Adrian Huntera025e4f2013-12-11 14:36:35 +02001934void perf_evlist__to_front(struct perf_evlist *evlist,
1935 struct perf_evsel *move_evsel)
1936{
1937 struct perf_evsel *evsel, *n;
1938 LIST_HEAD(move);
1939
1940 if (move_evsel == perf_evlist__first(evlist))
1941 return;
1942
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001943 evlist__for_each_entry_safe(evlist, n, evsel) {
Adrian Huntera025e4f2013-12-11 14:36:35 +02001944 if (evsel->leader == move_evsel->leader)
1945 list_move_tail(&evsel->node, &move);
1946 }
1947
1948 list_splice(&move, &evlist->entries);
1949}
Adrian Hunter60b08962014-07-31 09:00:52 +03001950
1951void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1952 struct perf_evsel *tracking_evsel)
1953{
1954 struct perf_evsel *evsel;
1955
1956 if (tracking_evsel->tracking)
1957 return;
1958
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001959 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter60b08962014-07-31 09:00:52 +03001960 if (evsel != tracking_evsel)
1961 evsel->tracking = false;
1962 }
1963
1964 tracking_evsel->tracking = true;
1965}
Wang Nan7630b3e2016-02-22 09:10:33 +00001966
1967struct perf_evsel *
1968perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,
1969 const char *str)
1970{
1971 struct perf_evsel *evsel;
1972
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001973 evlist__for_each_entry(evlist, evsel) {
Wang Nan7630b3e2016-02-22 09:10:33 +00001974 if (!evsel->name)
1975 continue;
1976 if (strcmp(str, evsel->name) == 0)
1977 return evsel;
1978 }
1979
1980 return NULL;
1981}
Wang Nan54cc54d2016-07-14 08:34:42 +00001982
1983void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist,
1984 enum bkw_mmap_state state)
1985{
1986 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1987 enum action {
1988 NONE,
1989 PAUSE,
1990 RESUME,
1991 } action = NONE;
1992
1993 if (!evlist->backward_mmap)
1994 return;
1995
1996 switch (old_state) {
1997 case BKW_MMAP_NOTREADY: {
1998 if (state != BKW_MMAP_RUNNING)
1999 goto state_err;;
2000 break;
2001 }
2002 case BKW_MMAP_RUNNING: {
2003 if (state != BKW_MMAP_DATA_PENDING)
2004 goto state_err;
2005 action = PAUSE;
2006 break;
2007 }
2008 case BKW_MMAP_DATA_PENDING: {
2009 if (state != BKW_MMAP_EMPTY)
2010 goto state_err;
2011 break;
2012 }
2013 case BKW_MMAP_EMPTY: {
2014 if (state != BKW_MMAP_RUNNING)
2015 goto state_err;
2016 action = RESUME;
2017 break;
2018 }
2019 default:
2020 WARN_ONCE(1, "Shouldn't get there\n");
2021 }
2022
2023 evlist->bkw_mmap_state = state;
2024
2025 switch (action) {
2026 case PAUSE:
2027 perf_evlist__pause(evlist);
2028 break;
2029 case RESUME:
2030 perf_evlist__resume(evlist);
2031 break;
2032 case NONE:
2033 default:
2034 break;
2035 }
2036
2037state_err:
2038 return;
2039}