blob: c6c891e154a63c67971c04512c0b576d5bf43087 [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"
Arnaldo Carvalho de Melo58db1d62017-04-19 16:05:56 -030020#include "units.h"
Wang Nan54cc54d2016-07-14 08:34:42 +000021#include "asm/bug.h"
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030022#include <signal.h>
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020023#include <unistd.h>
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020024
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020025#include "parse-events.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060026#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020027
Arnaldo Carvalho de Melo86a5e0c2017-04-19 19:03:14 -030028#include <sys/ioctl.h>
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020029#include <sys/mman.h>
30
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020031#include <linux/bitops.h>
32#include <linux/hash.h>
Arnaldo Carvalho de Melo0389cd12014-12-15 16:04:11 -030033#include <linux/log2.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020034#include <linux/err.h>
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020035
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 Melodb918ac2017-07-03 13:10:20 -0300242int __perf_evlist__add_default(struct perf_evlist *evlist, bool precise)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200243{
Arnaldo Carvalho de Melodb918ac2017-07-03 13:10:20 -0300244 struct perf_evsel *evsel = perf_evsel__new_cycles(precise);
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 Nan8db6d6b2016-07-14 08:34:35 +0000704union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx)
705{
706 struct perf_mmap *md = &evlist->mmap[idx];
707
708 /*
709 * Check messup is required for forward overwritable ring buffer:
710 * memory pointed by md->prev can be overwritten in this case.
711 * No need for read-write ring buffer: kernel stop outputting when
712 * it hit md->prev (perf_mmap__consume()).
713 */
714 return perf_mmap__read_forward(md, evlist->overwrite);
715}
716
717union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx)
718{
719 struct perf_mmap *md = &evlist->mmap[idx];
720
721 /*
722 * No need to check messup for backward ring buffer:
723 * We can always read arbitrary long data from a backward
724 * ring buffer unless we forget to pause it before reading.
725 */
726 return perf_mmap__read_backward(md);
727}
728
Wang Nan5a5ddeb2016-05-25 13:44:50 +0000729union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
730{
Wang Nana0c6f452016-07-14 08:34:41 +0000731 return perf_evlist__mmap_read_forward(evlist, idx);
Wang Nan5a5ddeb2016-05-25 13:44:50 +0000732}
733
Wang Nan8db6d6b2016-07-14 08:34:35 +0000734void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx)
735{
736 perf_mmap__read_catchup(&evlist->mmap[idx]);
737}
738
Wang Nan8db6d6b2016-07-14 08:34:35 +0000739void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
740{
741 perf_mmap__consume(&evlist->mmap[idx], evlist->overwrite);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800742}
743
Wang Nana1f72612016-07-14 08:34:38 +0000744static void perf_evlist__munmap_nofree(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200745{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300746 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200747
Wang Nanb2cb6152016-07-14 08:34:39 +0000748 if (evlist->mmap)
749 for (i = 0; i < evlist->nr_mmaps; i++)
750 perf_mmap__munmap(&evlist->mmap[i]);
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300751
Wang Nanb2cb6152016-07-14 08:34:39 +0000752 if (evlist->backward_mmap)
753 for (i = 0; i < evlist->nr_mmaps; i++)
754 perf_mmap__munmap(&evlist->backward_mmap[i]);
Wang Nana1f72612016-07-14 08:34:38 +0000755}
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300756
Wang Nana1f72612016-07-14 08:34:38 +0000757void perf_evlist__munmap(struct perf_evlist *evlist)
758{
759 perf_evlist__munmap_nofree(evlist);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300760 zfree(&evlist->mmap);
Wang Nanb2cb6152016-07-14 08:34:39 +0000761 zfree(&evlist->backward_mmap);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200762}
763
Wang Nan8db6d6b2016-07-14 08:34:35 +0000764static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200765{
Wang Nand4c6fb32016-05-20 16:38:24 +0000766 int i;
Wang Nan8db6d6b2016-07-14 08:34:35 +0000767 struct perf_mmap *map;
Wang Nand4c6fb32016-05-20 16:38:24 +0000768
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300769 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700770 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900771 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Wang Nan8db6d6b2016-07-14 08:34:35 +0000772 map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
773 if (!map)
774 return NULL;
Wang Nan946ae1d2016-05-31 13:06:15 +0000775
Arnaldo Carvalho de Melo4738ca32017-02-23 13:24:34 -0300776 for (i = 0; i < evlist->nr_mmaps; i++) {
Wang Nan8db6d6b2016-07-14 08:34:35 +0000777 map[i].fd = -1;
Arnaldo Carvalho de Melo4738ca32017-02-23 13:24:34 -0300778 /*
779 * When the perf_mmap() call is made we grab one refcount, plus
780 * one extra to let perf_evlist__mmap_consume() get the last
781 * events after all real references (perf_mmap__get()) are
782 * dropped.
783 *
784 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
785 * thus does perf_mmap__get() on it.
786 */
787 refcount_set(&map[i].refcnt, 0);
788 }
Wang Nan8db6d6b2016-07-14 08:34:35 +0000789 return map;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200790}
791
Wang Nanf3058a12016-05-24 02:28:59 +0000792static bool
793perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused,
794 struct perf_evsel *evsel)
795{
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +0000796 if (evsel->attr.write_backward)
Wang Nanf3058a12016-05-24 02:28:59 +0000797 return false;
798 return true;
799}
800
Adrian Hunter04e21312013-10-18 15:29:13 +0300801static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
Mark Rutland9f21b812016-09-08 11:21:51 +0100802 struct mmap_params *mp, int cpu_idx,
Wang Nan078c3382016-07-14 08:34:40 +0000803 int thread, int *_output, int *_output_backward)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300804{
805 struct perf_evsel *evsel;
Wang Nanf3058a12016-05-24 02:28:59 +0000806 int revent;
Mark Rutland9f21b812016-09-08 11:21:51 +0100807 int evlist_cpu = cpu_map__cpu(evlist->cpus, cpu_idx);
Adrian Hunter04e21312013-10-18 15:29:13 +0300808
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300809 evlist__for_each_entry(evlist, evsel) {
Wang Nan078c3382016-07-14 08:34:40 +0000810 struct perf_mmap *maps = evlist->mmap;
811 int *output = _output;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300812 int fd;
Mark Rutland9f21b812016-09-08 11:21:51 +0100813 int cpu;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300814
Wang Nan078c3382016-07-14 08:34:40 +0000815 if (evsel->attr.write_backward) {
816 output = _output_backward;
817 maps = evlist->backward_mmap;
818
819 if (!maps) {
820 maps = perf_evlist__alloc_mmap(evlist);
821 if (!maps)
822 return -1;
823 evlist->backward_mmap = maps;
Wang Nan54cc54d2016-07-14 08:34:42 +0000824 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
825 perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
Wang Nan078c3382016-07-14 08:34:40 +0000826 }
827 }
Wang Nanf3058a12016-05-24 02:28:59 +0000828
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300829 if (evsel->system_wide && thread)
830 continue;
831
Mark Rutland9f21b812016-09-08 11:21:51 +0100832 cpu = cpu_map__idx(evsel->cpus, evlist_cpu);
833 if (cpu == -1)
834 continue;
835
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300836 fd = FD(evsel, cpu, thread);
Adrian Hunter04e21312013-10-18 15:29:13 +0300837
838 if (*output == -1) {
839 *output = fd;
Wang Nan078c3382016-07-14 08:34:40 +0000840
841 if (perf_mmap__mmap(&maps[idx], mp, *output) < 0)
Adrian Hunter04e21312013-10-18 15:29:13 +0300842 return -1;
843 } else {
844 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
845 return -1;
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300846
Wang Nan078c3382016-07-14 08:34:40 +0000847 perf_mmap__get(&maps[idx]);
Adrian Hunter04e21312013-10-18 15:29:13 +0300848 }
849
Wang Nanf3058a12016-05-24 02:28:59 +0000850 revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
851
Adrian Hunterf90d1942014-11-11 16:16:39 +0200852 /*
853 * The system_wide flag causes a selected event to be opened
854 * always without a pid. Consequently it will never get a
855 * POLLHUP, but it is used for tracking in combination with
856 * other events, so it should not need to be polled anyway.
857 * Therefore don't add it for polling.
858 */
859 if (!evsel->system_wide &&
Wang Nan078c3382016-07-14 08:34:40 +0000860 __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) {
861 perf_mmap__put(&maps[idx]);
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300862 return -1;
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300863 }
Arnaldo Carvalho de Melo033fa712014-09-08 12:55:12 -0300864
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200865 if (evsel->attr.read_format & PERF_FORMAT_ID) {
866 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
867 fd) < 0)
868 return -1;
869 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
870 thread);
871 }
Adrian Hunter04e21312013-10-18 15:29:13 +0300872 }
873
874 return 0;
875}
876
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300877static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
878 struct mmap_params *mp)
Adrian Hunter04e21312013-10-18 15:29:13 +0300879{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300880 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900881 int nr_cpus = cpu_map__nr(evlist->cpus);
882 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300883
Adrian Huntere3e1a542013-08-14 15:48:24 +0300884 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900885 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300886 int output = -1;
Wang Nan078c3382016-07-14 08:34:40 +0000887 int output_backward = -1;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300888
Adrian Hunter718c6022015-04-09 18:53:42 +0300889 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
890 true);
891
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900892 for (thread = 0; thread < nr_threads; thread++) {
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300893 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
Wang Nan078c3382016-07-14 08:34:40 +0000894 thread, &output, &output_backward))
Adrian Hunter04e21312013-10-18 15:29:13 +0300895 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300896 }
897 }
898
899 return 0;
900
901out_unmap:
Wang Nana1f72612016-07-14 08:34:38 +0000902 perf_evlist__munmap_nofree(evlist);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300903 return -1;
904}
905
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300906static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
907 struct mmap_params *mp)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300908{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300909 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900910 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300911
Adrian Huntere3e1a542013-08-14 15:48:24 +0300912 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900913 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300914 int output = -1;
Wang Nan078c3382016-07-14 08:34:40 +0000915 int output_backward = -1;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300916
Adrian Hunter718c6022015-04-09 18:53:42 +0300917 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
918 false);
919
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300920 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
Wang Nan078c3382016-07-14 08:34:40 +0000921 &output, &output_backward))
Adrian Hunter04e21312013-10-18 15:29:13 +0300922 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300923 }
924
925 return 0;
926
927out_unmap:
Wang Nana1f72612016-07-14 08:34:38 +0000928 perf_evlist__munmap_nofree(evlist);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300929 return -1;
930}
931
Arnaldo Carvalho de Melof5e71502016-04-15 17:46:31 -0300932unsigned long perf_event_mlock_kb_in_pages(void)
933{
934 unsigned long pages;
935 int max;
936
937 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
938 /*
939 * Pick a once upon a time good value, i.e. things look
940 * strange since we can't read a sysctl value, but lets not
941 * die yet...
942 */
943 max = 512;
944 } else {
945 max -= (page_size / 1024);
946 }
947
948 pages = (max * 1024) / page_size;
949 if (!is_power_of_2(pages))
950 pages = rounddown_pow_of_two(pages);
951
952 return pages;
953}
954
Jiri Olsa0c582442017-01-09 10:51:59 +0100955size_t perf_evlist__mmap_size(unsigned long pages)
Jiri Olsa994a1f72013-09-01 12:36:12 +0200956{
Arnaldo Carvalho de Melof5e71502016-04-15 17:46:31 -0300957 if (pages == UINT_MAX)
958 pages = perf_event_mlock_kb_in_pages();
959 else if (!is_power_of_2(pages))
Jiri Olsa994a1f72013-09-01 12:36:12 +0200960 return 0;
961
962 return (pages + 1) * page_size;
963}
964
David Ahern33c2dcf2013-11-12 07:46:55 -0700965static long parse_pages_arg(const char *str, unsigned long min,
966 unsigned long max)
Jiri Olsa994a1f72013-09-01 12:36:12 +0200967{
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300968 unsigned long pages, val;
Jiri Olsa27050f52013-09-01 12:36:13 +0200969 static struct parse_tag tags[] = {
970 { .tag = 'B', .mult = 1 },
971 { .tag = 'K', .mult = 1 << 10 },
972 { .tag = 'M', .mult = 1 << 20 },
973 { .tag = 'G', .mult = 1 << 30 },
974 { .tag = 0 },
975 };
Jiri Olsa994a1f72013-09-01 12:36:12 +0200976
David Ahern89735042013-11-12 07:46:53 -0700977 if (str == NULL)
David Ahern33c2dcf2013-11-12 07:46:55 -0700978 return -EINVAL;
David Ahern89735042013-11-12 07:46:53 -0700979
Jiri Olsa27050f52013-09-01 12:36:13 +0200980 val = parse_tag_value(str, tags);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300981 if (val != (unsigned long) -1) {
Jiri Olsa27050f52013-09-01 12:36:13 +0200982 /* we got file size value */
983 pages = PERF_ALIGN(val, page_size) / page_size;
Jiri Olsa27050f52013-09-01 12:36:13 +0200984 } else {
985 /* we got pages count value */
986 char *eptr;
987 pages = strtoul(str, &eptr, 10);
David Ahern33c2dcf2013-11-12 07:46:55 -0700988 if (*eptr != '\0')
989 return -EINVAL;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200990 }
991
Adrian Hunter2bcab6c2013-12-09 15:18:37 +0200992 if (pages == 0 && min == 0) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700993 /* leave number of pages at 0 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200994 } else if (!is_power_of_2(pages)) {
Jiri Olsa98081432017-01-09 10:51:55 +0100995 char buf[100];
996
David Ahern33c2dcf2013-11-12 07:46:55 -0700997 /* round pages up to next power of 2 */
Arnaldo Carvalho de Melo91529832014-12-16 13:24:41 -0300998 pages = roundup_pow_of_two(pages);
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200999 if (!pages)
1000 return -EINVAL;
Jiri Olsa98081432017-01-09 10:51:55 +01001001
1002 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
1003 pr_info("rounding mmap pages size to %s (%lu pages)\n",
1004 buf, pages);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +03001005 }
1006
David Ahern33c2dcf2013-11-12 07:46:55 -07001007 if (pages > max)
1008 return -EINVAL;
1009
1010 return pages;
1011}
1012
Adrian Huntere9db1312015-04-09 18:53:46 +03001013int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
David Ahern33c2dcf2013-11-12 07:46:55 -07001014{
David Ahern33c2dcf2013-11-12 07:46:55 -07001015 unsigned long max = UINT_MAX;
1016 long pages;
1017
Adrian Hunterf5ae9c42013-12-09 15:18:38 +02001018 if (max > SIZE_MAX / page_size)
David Ahern33c2dcf2013-11-12 07:46:55 -07001019 max = SIZE_MAX / page_size;
1020
1021 pages = parse_pages_arg(str, 1, max);
1022 if (pages < 0) {
1023 pr_err("Invalid argument for --mmap_pages/-m\n");
Jiri Olsa994a1f72013-09-01 12:36:12 +02001024 return -1;
1025 }
1026
1027 *mmap_pages = pages;
1028 return 0;
1029}
1030
Adrian Huntere9db1312015-04-09 18:53:46 +03001031int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1032 int unset __maybe_unused)
1033{
1034 return __perf_evlist__parse_mmap_pages(opt->value, str);
1035}
1036
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001037/**
Adrian Hunter718c6022015-04-09 18:53:42 +03001038 * perf_evlist__mmap_ex - Create mmaps to receive events.
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001039 * @evlist: list of events
1040 * @pages: map length in pages
1041 * @overwrite: overwrite older events?
Adrian Hunter718c6022015-04-09 18:53:42 +03001042 * @auxtrace_pages - auxtrace map length in pages
1043 * @auxtrace_overwrite - overwrite older auxtrace data?
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001044 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001045 * If @overwrite is %false the user needs to signal event consumption using
1046 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
1047 * automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001048 *
Adrian Hunter718c6022015-04-09 18:53:42 +03001049 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1050 * consumption using auxtrace_mmap__write_tail().
1051 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001052 * Return: %0 on success, negative error code otherwise.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001053 */
Adrian Hunter718c6022015-04-09 18:53:42 +03001054int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1055 bool overwrite, unsigned int auxtrace_pages,
1056 bool auxtrace_overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001057{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001058 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001059 const struct cpu_map *cpus = evlist->cpus;
1060 const struct thread_map *threads = evlist->threads;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001061 struct mmap_params mp = {
1062 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1063 };
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -02001064
Wang Nan8db6d6b2016-07-14 08:34:35 +00001065 if (!evlist->mmap)
1066 evlist->mmap = perf_evlist__alloc_mmap(evlist);
1067 if (!evlist->mmap)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001068 return -ENOMEM;
1069
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -03001070 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001071 return -ENOMEM;
1072
1073 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +02001074 evlist->mmap_len = perf_evlist__mmap_size(pages);
Adrian Hunter2af68ef2013-10-18 15:29:07 +03001075 pr_debug("mmap size %zuB\n", evlist->mmap_len);
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001076 mp.mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001077
Adrian Hunter718c6022015-04-09 18:53:42 +03001078 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1079 auxtrace_pages, auxtrace_overwrite);
1080
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001081 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001082 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001083 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -03001084 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001085 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001086 }
1087
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -07001088 if (cpu_map__empty(cpus))
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001089 return perf_evlist__mmap_per_thread(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001090
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001091 return perf_evlist__mmap_per_cpu(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001092}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001093
Adrian Hunter718c6022015-04-09 18:53:42 +03001094int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1095 bool overwrite)
1096{
1097 return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1098}
1099
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001100int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001101{
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001102 struct cpu_map *cpus;
1103 struct thread_map *threads;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001104
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001105 threads = thread_map__new_str(target->pid, target->tid, target->uid);
1106
1107 if (!threads)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001108 return -1;
1109
Dongsheng Yang9c105fb2013-12-04 17:56:40 -05001110 if (target__uses_dummy_map(target))
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001111 cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +09001112 else
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001113 cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001114
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001115 if (!cpus)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001116 goto out_delete_threads;
1117
Adrian Hunterec9a77a2015-09-08 10:58:52 +03001118 evlist->has_user_cpus = !!target->cpu_list;
1119
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001120 perf_evlist__set_maps(evlist, cpus, threads);
Adrian Hunterd5bc0562015-09-08 10:58:51 +03001121
1122 return 0;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001123
1124out_delete_threads:
Adrian Hunter74bfd2b2015-09-08 10:58:57 +03001125 thread_map__put(threads);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001126 return -1;
1127}
1128
Adrian Hunterd5bc0562015-09-08 10:58:51 +03001129void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
1130 struct thread_map *threads)
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001131{
Adrian Hunter934e0f22015-09-08 10:58:56 +03001132 /*
1133 * Allow for the possibility that one or another of the maps isn't being
1134 * changed i.e. don't put it. Note we are assuming the maps that are
1135 * being applied are brand new and evlist is taking ownership of the
1136 * original reference count of 1. If that is not the case it is up to
1137 * the caller to increase the reference count.
1138 */
1139 if (cpus != evlist->cpus) {
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001140 cpu_map__put(evlist->cpus);
Arnaldo Carvalho de Meloa55e5662016-02-17 10:57:19 -03001141 evlist->cpus = cpu_map__get(cpus);
Adrian Hunter934e0f22015-09-08 10:58:56 +03001142 }
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001143
Adrian Hunter934e0f22015-09-08 10:58:56 +03001144 if (threads != evlist->threads) {
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001145 thread_map__put(evlist->threads);
Arnaldo Carvalho de Meloa55e5662016-02-17 10:57:19 -03001146 evlist->threads = thread_map__get(threads);
Adrian Hunter934e0f22015-09-08 10:58:56 +03001147 }
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001148
Adrian Hunterec9a77a2015-09-08 10:58:52 +03001149 perf_evlist__propagate_maps(evlist);
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001150}
1151
Arnaldo Carvalho de Melo22c8a372016-04-11 18:37:45 -03001152void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
1153 enum perf_event_sample_format bit)
1154{
1155 struct perf_evsel *evsel;
1156
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001157 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo22c8a372016-04-11 18:37:45 -03001158 __perf_evsel__set_sample_bit(evsel, bit);
1159}
1160
1161void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
1162 enum perf_event_sample_format bit)
1163{
1164 struct perf_evsel *evsel;
1165
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001166 evlist__for_each_entry(evlist, evsel)
Arnaldo Carvalho de Melo22c8a372016-04-11 18:37:45 -03001167 __perf_evsel__reset_sample_bit(evsel, bit);
1168}
1169
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001170int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001171{
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001172 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001173 int err = 0;
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001174
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001175 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001176 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001177 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001178
Kan Liangd988d5e2015-08-21 02:23:14 -04001179 /*
1180 * filters only work for tracepoint event, which doesn't have cpu limit.
1181 * So evlist and evsel should always be same.
1182 */
Andi Kleen475fb532017-08-11 16:26:17 -07001183 err = perf_evsel__apply_filter(evsel, evsel->filter);
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001184 if (err) {
1185 *err_evsel = evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001186 break;
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001187 }
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001188 }
1189
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001190 return err;
1191}
1192
1193int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1194{
1195 struct perf_evsel *evsel;
1196 int err = 0;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001197
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001198 evlist__for_each_entry(evlist, evsel) {
Wang Nanfdf14722016-02-26 09:31:53 +00001199 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
1200 continue;
1201
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03001202 err = perf_evsel__set_filter(evsel, filter);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001203 if (err)
1204 break;
1205 }
1206
1207 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001208}
Frederic Weisbecker74429962011-05-21 17:49:00 +02001209
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001210int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001211{
1212 char *filter;
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001213 int ret = -1;
1214 size_t i;
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001215
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001216 for (i = 0; i < npids; ++i) {
1217 if (i == 0) {
1218 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1219 return -1;
1220 } else {
1221 char *tmp;
1222
1223 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1224 goto out_free;
1225
1226 free(filter);
1227 filter = tmp;
1228 }
1229 }
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001230
1231 ret = perf_evlist__set_filter(evlist, filter);
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001232out_free:
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001233 free(filter);
1234 return ret;
1235}
1236
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001237int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1238{
1239 return perf_evlist__set_filter_pids(evlist, 1, &pid);
1240}
1241
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001242bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001243{
Adrian Hunter75562572013-08-27 11:23:09 +03001244 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001245
Adrian Hunter75562572013-08-27 11:23:09 +03001246 if (evlist->nr_entries == 1)
1247 return true;
1248
1249 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1250 return false;
1251
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001252 evlist__for_each_entry(evlist, pos) {
Adrian Hunter75562572013-08-27 11:23:09 +03001253 if (pos->id_pos != evlist->id_pos ||
1254 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001255 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001256 }
1257
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001258 return true;
1259}
1260
Adrian Hunter75562572013-08-27 11:23:09 +03001261u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001262{
Adrian Hunter75562572013-08-27 11:23:09 +03001263 struct perf_evsel *evsel;
1264
1265 if (evlist->combined_sample_type)
1266 return evlist->combined_sample_type;
1267
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001268 evlist__for_each_entry(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +03001269 evlist->combined_sample_type |= evsel->attr.sample_type;
1270
1271 return evlist->combined_sample_type;
1272}
1273
1274u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1275{
1276 evlist->combined_sample_type = 0;
1277 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001278}
1279
Andi Kleen98df8582015-07-18 08:24:47 -07001280u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
1281{
1282 struct perf_evsel *evsel;
1283 u64 branch_type = 0;
1284
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001285 evlist__for_each_entry(evlist, evsel)
Andi Kleen98df8582015-07-18 08:24:47 -07001286 branch_type |= evsel->attr.branch_sample_type;
1287 return branch_type;
1288}
1289
Jiri Olsa9ede4732012-10-10 17:38:13 +02001290bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1291{
1292 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1293 u64 read_format = first->attr.read_format;
1294 u64 sample_type = first->attr.sample_type;
1295
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001296 evlist__for_each_entry(evlist, pos) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001297 if (read_format != pos->attr.read_format)
1298 return false;
1299 }
1300
1301 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1302 if ((sample_type & PERF_SAMPLE_READ) &&
1303 !(read_format & PERF_FORMAT_ID)) {
1304 return false;
1305 }
1306
1307 return true;
1308}
1309
1310u64 perf_evlist__read_format(struct perf_evlist *evlist)
1311{
1312 struct perf_evsel *first = perf_evlist__first(evlist);
1313 return first->attr.read_format;
1314}
1315
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001316u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -02001317{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001318 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -02001319 struct perf_sample *data;
1320 u64 sample_type;
1321 u16 size = 0;
1322
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -02001323 if (!first->attr.sample_id_all)
1324 goto out;
1325
1326 sample_type = first->attr.sample_type;
1327
1328 if (sample_type & PERF_SAMPLE_TID)
1329 size += sizeof(data->tid) * 2;
1330
1331 if (sample_type & PERF_SAMPLE_TIME)
1332 size += sizeof(data->time);
1333
1334 if (sample_type & PERF_SAMPLE_ID)
1335 size += sizeof(data->id);
1336
1337 if (sample_type & PERF_SAMPLE_STREAM_ID)
1338 size += sizeof(data->stream_id);
1339
1340 if (sample_type & PERF_SAMPLE_CPU)
1341 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +03001342
1343 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1344 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -02001345out:
1346 return size;
1347}
1348
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001349bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001350{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001351 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001352
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001353 evlist__for_each_entry_continue(evlist, pos) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001354 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1355 return false;
1356 }
1357
1358 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001359}
1360
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001361bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001362{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001363 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001364 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001365}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001366
1367void perf_evlist__set_selected(struct perf_evlist *evlist,
1368 struct perf_evsel *evsel)
1369{
1370 evlist->selected = evsel;
1371}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001372
Namhyung Kima74b4b62013-03-15 14:48:48 +09001373void perf_evlist__close(struct perf_evlist *evlist)
1374{
1375 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001376
Andi Kleen475fb532017-08-11 16:26:17 -07001377 evlist__for_each_entry_reverse(evlist, evsel)
1378 perf_evsel__close(evsel);
Namhyung Kima74b4b62013-03-15 14:48:48 +09001379}
1380
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001381static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1382{
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001383 struct cpu_map *cpus;
1384 struct thread_map *threads;
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001385 int err = -ENOMEM;
1386
1387 /*
1388 * Try reading /sys/devices/system/cpu/online to get
1389 * an all cpus map.
1390 *
1391 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1392 * code needs an overhaul to properly forward the
1393 * error, and we may not want to do that fallback to a
1394 * default cpu identity map :-\
1395 */
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001396 cpus = cpu_map__new(NULL);
1397 if (!cpus)
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001398 goto out;
1399
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001400 threads = thread_map__new_dummy();
1401 if (!threads)
1402 goto out_put;
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001403
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001404 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001405out:
1406 return err;
Adrian Hunter8c0498b2015-09-08 10:59:00 +03001407out_put:
1408 cpu_map__put(cpus);
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001409 goto out;
1410}
1411
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001412int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001413{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001414 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001415 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001416
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001417 /*
1418 * Default: one fd per CPU, all threads, aka systemwide
1419 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1420 */
1421 if (evlist->threads == NULL && evlist->cpus == NULL) {
1422 err = perf_evlist__create_syswide_maps(evlist);
1423 if (err < 0)
1424 goto out_err;
1425 }
1426
Adrian Hunter733cd2f2013-09-06 22:40:11 +03001427 perf_evlist__update_id_pos(evlist);
1428
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001429 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter23df7f72016-01-07 10:13:59 +01001430 err = perf_evsel__open(evsel, evsel->cpus, evsel->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001431 if (err < 0)
1432 goto out_err;
1433 }
1434
1435 return 0;
1436out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001437 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001438 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001439 return err;
1440}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001441
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001442int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001443 const char *argv[], bool pipe_output,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001444 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001445{
1446 int child_ready_pipe[2], go_pipe[2];
1447 char bf;
1448
1449 if (pipe(child_ready_pipe) < 0) {
1450 perror("failed to create 'ready' pipe");
1451 return -1;
1452 }
1453
1454 if (pipe(go_pipe) < 0) {
1455 perror("failed to create 'go' pipe");
1456 goto out_close_ready_pipe;
1457 }
1458
1459 evlist->workload.pid = fork();
1460 if (evlist->workload.pid < 0) {
1461 perror("failed to fork");
1462 goto out_close_pipes;
1463 }
1464
1465 if (!evlist->workload.pid) {
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001466 int ret;
1467
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001468 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001469 dup2(2, 1);
1470
David Ahern0817df02013-05-25 17:50:39 -06001471 signal(SIGTERM, SIG_DFL);
1472
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001473 close(child_ready_pipe[0]);
1474 close(go_pipe[1]);
1475 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1476
1477 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001478 * Tell the parent we're ready to go
1479 */
1480 close(child_ready_pipe[1]);
1481
1482 /*
1483 * Wait until the parent tells us to go.
1484 */
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001485 ret = read(go_pipe[0], &bf, 1);
1486 /*
1487 * The parent will ask for the execvp() to be performed by
1488 * writing exactly one byte, in workload.cork_fd, usually via
1489 * perf_evlist__start_workload().
1490 *
Arnaldo Carvalho de Melo20f86fc2015-02-03 13:29:05 -03001491 * For cancelling the workload without actually running it,
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001492 * the parent will just close workload.cork_fd, without writing
1493 * anything, i.e. read will return zero and we just exit()
1494 * here.
1495 */
1496 if (ret != 1) {
1497 if (ret == -1)
1498 perror("unable to read pipe");
1499 exit(ret);
1500 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001501
1502 execvp(argv[0], (char **)argv);
1503
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001504 if (exec_error) {
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001505 union sigval val;
1506
1507 val.sival_int = errno;
1508 if (sigqueue(getppid(), SIGUSR1, val))
1509 perror(argv[0]);
1510 } else
1511 perror(argv[0]);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001512 exit(-1);
1513 }
1514
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001515 if (exec_error) {
1516 struct sigaction act = {
1517 .sa_flags = SA_SIGINFO,
1518 .sa_sigaction = exec_error,
1519 };
1520 sigaction(SIGUSR1, &act, NULL);
1521 }
1522
Arnaldo Carvalho de Melo1aaf63b2014-10-10 14:29:49 -03001523 if (target__none(target)) {
1524 if (evlist->threads == NULL) {
1525 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1526 __func__, __LINE__);
1527 goto out_close_pipes;
1528 }
Jiri Olsae13798c2015-06-23 00:36:02 +02001529 thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
Arnaldo Carvalho de Melo1aaf63b2014-10-10 14:29:49 -03001530 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001531
1532 close(child_ready_pipe[1]);
1533 close(go_pipe[0]);
1534 /*
1535 * wait for child to settle
1536 */
1537 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1538 perror("unable to read pipe");
1539 goto out_close_pipes;
1540 }
1541
Namhyung Kimbcf31452013-06-26 16:14:15 +09001542 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001543 evlist->workload.cork_fd = go_pipe[1];
1544 close(child_ready_pipe[0]);
1545 return 0;
1546
1547out_close_pipes:
1548 close(go_pipe[0]);
1549 close(go_pipe[1]);
1550out_close_ready_pipe:
1551 close(child_ready_pipe[0]);
1552 close(child_ready_pipe[1]);
1553 return -1;
1554}
1555
1556int perf_evlist__start_workload(struct perf_evlist *evlist)
1557{
1558 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001559 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001560 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001561 /*
1562 * Remove the cork, let it rip!
1563 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001564 ret = write(evlist->workload.cork_fd, &bf, 1);
1565 if (ret < 0)
Soramichi Akiyamae978be92017-01-10 10:41:00 -03001566 perror("unable to write to pipe");
Namhyung Kimbcf31452013-06-26 16:14:15 +09001567
1568 close(evlist->workload.cork_fd);
1569 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001570 }
1571
1572 return 0;
1573}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001574
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001575int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001576 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001577{
Adrian Hunter75562572013-08-27 11:23:09 +03001578 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1579
1580 if (!evsel)
1581 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001582 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001583}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001584
1585size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1586{
1587 struct perf_evsel *evsel;
1588 size_t printed = 0;
1589
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001590 evlist__for_each_entry(evlist, evsel) {
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001591 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1592 perf_evsel__name(evsel));
1593 }
1594
Davidlohr Buesob2222132013-11-12 22:24:24 -08001595 return printed + fprintf(fp, "\n");
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001596}
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03001597
Arnaldo Carvalho de Melod9aade7f2016-02-18 13:34:09 -03001598int perf_evlist__strerror_open(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001599 int err, char *buf, size_t size)
1600{
1601 int printed, value;
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001602 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001603
1604 switch (err) {
1605 case EACCES:
1606 case EPERM:
1607 printed = scnprintf(buf, size,
1608 "Error:\t%s.\n"
1609 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1610
Adrian Hunter1a472452013-12-11 14:36:23 +02001611 value = perf_event_paranoid();
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001612
1613 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1614
1615 if (value >= 2) {
1616 printed += scnprintf(buf + printed, size - printed,
1617 "For your workloads it needs to be <= 1\nHint:\t");
1618 }
1619 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001620 "For system wide tracing it needs to be set to -1.\n");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001621
1622 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001623 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1624 "Hint:\tThe current value is %d.", value);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001625 break;
Arnaldo Carvalho de Melod9aade7f2016-02-18 13:34:09 -03001626 case EINVAL: {
1627 struct perf_evsel *first = perf_evlist__first(evlist);
1628 int max_freq;
1629
1630 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1631 goto out_default;
1632
1633 if (first->attr.sample_freq < (u64)max_freq)
1634 goto out_default;
1635
1636 printed = scnprintf(buf, size,
1637 "Error:\t%s.\n"
1638 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1639 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1640 emsg, max_freq, first->attr.sample_freq);
1641 break;
1642 }
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001643 default:
Arnaldo Carvalho de Melod9aade7f2016-02-18 13:34:09 -03001644out_default:
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001645 scnprintf(buf, size, "%s", emsg);
1646 break;
1647 }
1648
1649 return 0;
1650}
Adrian Huntera025e4f2013-12-11 14:36:35 +02001651
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001652int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1653{
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03001654 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001655 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001656
1657 switch (err) {
1658 case EPERM:
Arnaldo Carvalho de Meloe5d4a292014-12-12 15:59:51 -03001659 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001660 printed += scnprintf(buf + printed, size - printed,
1661 "Error:\t%s.\n"
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001662 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001663 "Hint:\tTried using %zd kB.\n",
Arnaldo Carvalho de Meloe5d4a292014-12-12 15:59:51 -03001664 emsg, pages_max_per_user, pages_attempted);
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001665
1666 if (pages_attempted >= pages_max_per_user) {
1667 printed += scnprintf(buf + printed, size - printed,
1668 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1669 pages_max_per_user + pages_attempted);
1670 }
1671
1672 printed += scnprintf(buf + printed, size - printed,
1673 "Hint:\tTry using a smaller -m/--mmap-pages value.");
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001674 break;
1675 default:
1676 scnprintf(buf, size, "%s", emsg);
1677 break;
1678 }
1679
1680 return 0;
1681}
1682
Adrian Huntera025e4f2013-12-11 14:36:35 +02001683void perf_evlist__to_front(struct perf_evlist *evlist,
1684 struct perf_evsel *move_evsel)
1685{
1686 struct perf_evsel *evsel, *n;
1687 LIST_HEAD(move);
1688
1689 if (move_evsel == perf_evlist__first(evlist))
1690 return;
1691
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001692 evlist__for_each_entry_safe(evlist, n, evsel) {
Adrian Huntera025e4f2013-12-11 14:36:35 +02001693 if (evsel->leader == move_evsel->leader)
1694 list_move_tail(&evsel->node, &move);
1695 }
1696
1697 list_splice(&move, &evlist->entries);
1698}
Adrian Hunter60b08962014-07-31 09:00:52 +03001699
1700void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1701 struct perf_evsel *tracking_evsel)
1702{
1703 struct perf_evsel *evsel;
1704
1705 if (tracking_evsel->tracking)
1706 return;
1707
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001708 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter60b08962014-07-31 09:00:52 +03001709 if (evsel != tracking_evsel)
1710 evsel->tracking = false;
1711 }
1712
1713 tracking_evsel->tracking = true;
1714}
Wang Nan7630b3e2016-02-22 09:10:33 +00001715
1716struct perf_evsel *
1717perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,
1718 const char *str)
1719{
1720 struct perf_evsel *evsel;
1721
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001722 evlist__for_each_entry(evlist, evsel) {
Wang Nan7630b3e2016-02-22 09:10:33 +00001723 if (!evsel->name)
1724 continue;
1725 if (strcmp(str, evsel->name) == 0)
1726 return evsel;
1727 }
1728
1729 return NULL;
1730}
Wang Nan54cc54d2016-07-14 08:34:42 +00001731
1732void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist,
1733 enum bkw_mmap_state state)
1734{
1735 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1736 enum action {
1737 NONE,
1738 PAUSE,
1739 RESUME,
1740 } action = NONE;
1741
1742 if (!evlist->backward_mmap)
1743 return;
1744
1745 switch (old_state) {
1746 case BKW_MMAP_NOTREADY: {
1747 if (state != BKW_MMAP_RUNNING)
1748 goto state_err;;
1749 break;
1750 }
1751 case BKW_MMAP_RUNNING: {
1752 if (state != BKW_MMAP_DATA_PENDING)
1753 goto state_err;
1754 action = PAUSE;
1755 break;
1756 }
1757 case BKW_MMAP_DATA_PENDING: {
1758 if (state != BKW_MMAP_EMPTY)
1759 goto state_err;
1760 break;
1761 }
1762 case BKW_MMAP_EMPTY: {
1763 if (state != BKW_MMAP_RUNNING)
1764 goto state_err;
1765 action = RESUME;
1766 break;
1767 }
1768 default:
1769 WARN_ONCE(1, "Shouldn't get there\n");
1770 }
1771
1772 evlist->bkw_mmap_state = state;
1773
1774 switch (action) {
1775 case PAUSE:
1776 perf_evlist__pause(evlist);
1777 break;
1778 case RESUME:
1779 perf_evlist__resume(evlist);
1780 break;
1781 case NONE:
1782 default:
1783 break;
1784 }
1785
1786state_err:
1787 return;
1788}