blob: 398dab1a08cc8ae1fdad4c3cafc7bbf2b8076a5c [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"
Borislav Petkov553873e2013-12-09 17:14:23 +010010#include <api/fs/debugfs.h>
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -020011#include <poll.h>
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020012#include "cpumap.h"
13#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090014#include "target.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020015#include "evlist.h"
16#include "evsel.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030017#include "debug.h"
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020018#include <unistd.h>
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020019
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020020#include "parse-events.h"
Jiri Olsa994a1f72013-09-01 12:36:12 +020021#include "parse-options.h"
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020022
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020023#include <sys/mman.h>
24
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020025#include <linux/bitops.h>
26#include <linux/hash.h>
27
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020028#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030029#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020030
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020031void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
32 struct thread_map *threads)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020033{
34 int i;
35
36 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
37 INIT_HLIST_HEAD(&evlist->heads[i]);
38 INIT_LIST_HEAD(&evlist->entries);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020039 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030040 fdarray__init(&evlist->pollfd, 64);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020041 evlist->workload.pid = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020042}
43
Namhyung Kim334fe7a2013-03-11 16:43:12 +090044struct perf_evlist *perf_evlist__new(void)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020045{
46 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
47
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020048 if (evlist != NULL)
Namhyung Kim334fe7a2013-03-11 16:43:12 +090049 perf_evlist__init(evlist, NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020050
51 return evlist;
52}
53
Jiri Olsab22d54b2013-09-01 12:36:14 +020054struct perf_evlist *perf_evlist__new_default(void)
55{
56 struct perf_evlist *evlist = perf_evlist__new();
57
58 if (evlist && perf_evlist__add_default(evlist)) {
59 perf_evlist__delete(evlist);
60 evlist = NULL;
61 }
62
63 return evlist;
64}
65
Adrian Hunter75562572013-08-27 11:23:09 +030066/**
67 * perf_evlist__set_id_pos - set the positions of event ids.
68 * @evlist: selected event list
69 *
70 * Events with compatible sample types all have the same id_pos
71 * and is_pos. For convenience, put a copy on evlist.
72 */
73void perf_evlist__set_id_pos(struct perf_evlist *evlist)
74{
75 struct perf_evsel *first = perf_evlist__first(evlist);
76
77 evlist->id_pos = first->id_pos;
78 evlist->is_pos = first->is_pos;
79}
80
Adrian Hunter733cd2f2013-09-06 22:40:11 +030081static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
82{
83 struct perf_evsel *evsel;
84
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030085 evlist__for_each(evlist, evsel)
Adrian Hunter733cd2f2013-09-06 22:40:11 +030086 perf_evsel__calc_id_pos(evsel);
87
88 perf_evlist__set_id_pos(evlist);
89}
90
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020091static void perf_evlist__purge(struct perf_evlist *evlist)
92{
93 struct perf_evsel *pos, *n;
94
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030095 evlist__for_each_safe(evlist, n, pos) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020096 list_del_init(&pos->node);
97 perf_evsel__delete(pos);
98 }
99
100 evlist->nr_entries = 0;
101}
102
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200103void perf_evlist__exit(struct perf_evlist *evlist)
104{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300105 zfree(&evlist->mmap);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300106 fdarray__exit(&evlist->pollfd);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200107}
108
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200109void perf_evlist__delete(struct perf_evlist *evlist)
110{
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300111 perf_evlist__munmap(evlist);
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -0300112 perf_evlist__close(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300113 cpu_map__delete(evlist->cpus);
114 thread_map__delete(evlist->threads);
115 evlist->cpus = NULL;
116 evlist->threads = NULL;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200117 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200118 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200119 free(evlist);
120}
121
122void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
123{
124 list_add_tail(&entry->node, &evlist->entries);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300125 entry->idx = evlist->nr_entries;
Adrian Hunter60b08962014-07-31 09:00:52 +0300126 entry->tracking = !entry->idx;
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300127
Adrian Hunter75562572013-08-27 11:23:09 +0300128 if (!evlist->nr_entries++)
129 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200130}
131
Jiri Olsa0529bc12012-01-27 15:34:20 +0100132void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
133 struct list_head *list,
134 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200135{
Adrian Hunter75562572013-08-27 11:23:09 +0300136 bool set_id_pos = !evlist->nr_entries;
137
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200138 list_splice_tail(list, &evlist->entries);
139 evlist->nr_entries += nr_entries;
Adrian Hunter75562572013-08-27 11:23:09 +0300140 if (set_id_pos)
141 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200142}
143
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300144void __perf_evlist__set_leader(struct list_head *list)
145{
146 struct perf_evsel *evsel, *leader;
147
148 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900149 evsel = list_entry(list->prev, struct perf_evsel, node);
150
151 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300152
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300153 __evlist__for_each(list, evsel) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100154 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300155 }
156}
157
158void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200159{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900160 if (evlist->nr_entries) {
161 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300162 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900163 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200164}
165
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200166int perf_evlist__add_default(struct perf_evlist *evlist)
167{
168 struct perf_event_attr attr = {
169 .type = PERF_TYPE_HARDWARE,
170 .config = PERF_COUNT_HW_CPU_CYCLES,
171 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100172 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200173
Joerg Roedel1aed2672012-01-04 17:54:20 +0100174 event_attr_init(&attr);
175
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300176 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200177 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200178 goto error;
179
180 /* use strdup() because free(evsel) assumes name is allocated */
181 evsel->name = strdup("cycles");
182 if (!evsel->name)
183 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200184
185 perf_evlist__add(evlist, evsel);
186 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200187error_free:
188 perf_evsel__delete(evsel);
189error:
190 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200191}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200192
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300193static int perf_evlist__add_attrs(struct perf_evlist *evlist,
194 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200195{
196 struct perf_evsel *evsel, *n;
197 LIST_HEAD(head);
198 size_t i;
199
200 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300201 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200202 if (evsel == NULL)
203 goto out_delete_partial_list;
204 list_add_tail(&evsel->node, &head);
205 }
206
207 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
208
209 return 0;
210
211out_delete_partial_list:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300212 __evlist__for_each_safe(&head, n, evsel)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200213 perf_evsel__delete(evsel);
214 return -1;
215}
216
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300217int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
218 struct perf_event_attr *attrs, size_t nr_attrs)
219{
220 size_t i;
221
222 for (i = 0; i < nr_attrs; i++)
223 event_attr_init(attrs + i);
224
225 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
226}
227
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300228struct perf_evsel *
229perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200230{
231 struct perf_evsel *evsel;
232
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300233 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200234 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
235 (int)evsel->attr.config == id)
236 return evsel;
237 }
238
239 return NULL;
240}
241
David Aherna2f28042013-08-28 22:29:51 -0600242struct perf_evsel *
243perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
244 const char *name)
245{
246 struct perf_evsel *evsel;
247
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300248 evlist__for_each(evlist, evsel) {
David Aherna2f28042013-08-28 22:29:51 -0600249 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
250 (strcmp(evsel->name, name) == 0))
251 return evsel;
252 }
253
254 return NULL;
255}
256
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300257int perf_evlist__add_newtp(struct perf_evlist *evlist,
258 const char *sys, const char *name, void *handler)
259{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300260 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300261
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300262 if (evsel == NULL)
263 return -1;
264
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300265 evsel->handler = handler;
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300266 perf_evlist__add(evlist, evsel);
267 return 0;
268}
269
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300270static int perf_evlist__nr_threads(struct perf_evlist *evlist,
271 struct perf_evsel *evsel)
272{
273 if (evsel->system_wide)
274 return 1;
275 else
276 return thread_map__nr(evlist->threads);
277}
278
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300279void perf_evlist__disable(struct perf_evlist *evlist)
280{
281 int cpu, thread;
282 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900283 int nr_cpus = cpu_map__nr(evlist->cpus);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300284 int nr_threads;
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300285
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900286 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300287 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300288 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100289 continue;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300290 nr_threads = perf_evlist__nr_threads(evlist, pos);
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900291 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900292 ioctl(FD(pos, cpu, thread),
293 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300294 }
295 }
296}
297
David Ahern764e16a32011-08-25 10:17:55 -0600298void perf_evlist__enable(struct perf_evlist *evlist)
299{
300 int cpu, thread;
301 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900302 int nr_cpus = cpu_map__nr(evlist->cpus);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300303 int nr_threads;
David Ahern764e16a32011-08-25 10:17:55 -0600304
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900305 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300306 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300307 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100308 continue;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300309 nr_threads = perf_evlist__nr_threads(evlist, pos);
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900310 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900311 ioctl(FD(pos, cpu, thread),
312 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600313 }
314 }
315}
316
Adrian Hunter395c3072013-08-31 21:50:53 +0300317int perf_evlist__disable_event(struct perf_evlist *evlist,
318 struct perf_evsel *evsel)
319{
320 int cpu, thread, err;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300321 int nr_cpus = cpu_map__nr(evlist->cpus);
322 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
Adrian Hunter395c3072013-08-31 21:50:53 +0300323
324 if (!evsel->fd)
325 return 0;
326
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300327 for (cpu = 0; cpu < nr_cpus; cpu++) {
328 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300329 err = ioctl(FD(evsel, cpu, thread),
330 PERF_EVENT_IOC_DISABLE, 0);
331 if (err)
332 return err;
333 }
334 }
335 return 0;
336}
337
338int perf_evlist__enable_event(struct perf_evlist *evlist,
339 struct perf_evsel *evsel)
340{
341 int cpu, thread, err;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300342 int nr_cpus = cpu_map__nr(evlist->cpus);
343 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
Adrian Hunter395c3072013-08-31 21:50:53 +0300344
345 if (!evsel->fd)
346 return -EINVAL;
347
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300348 for (cpu = 0; cpu < nr_cpus; cpu++) {
349 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300350 err = ioctl(FD(evsel, cpu, thread),
351 PERF_EVENT_IOC_ENABLE, 0);
352 if (err)
353 return err;
354 }
355 }
356 return 0;
357}
358
Adrian Hunter1c650562014-07-31 09:00:56 +0300359static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
360 struct perf_evsel *evsel, int cpu)
361{
362 int thread, err;
363 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
364
365 if (!evsel->fd)
366 return -EINVAL;
367
368 for (thread = 0; thread < nr_threads; thread++) {
369 err = ioctl(FD(evsel, cpu, thread),
370 PERF_EVENT_IOC_ENABLE, 0);
371 if (err)
372 return err;
373 }
374 return 0;
375}
376
377static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
378 struct perf_evsel *evsel,
379 int thread)
380{
381 int cpu, err;
382 int nr_cpus = cpu_map__nr(evlist->cpus);
383
384 if (!evsel->fd)
385 return -EINVAL;
386
387 for (cpu = 0; cpu < nr_cpus; cpu++) {
388 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
389 if (err)
390 return err;
391 }
392 return 0;
393}
394
395int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
396 struct perf_evsel *evsel, int idx)
397{
398 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
399
400 if (per_cpu_mmaps)
401 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
402 else
403 return perf_evlist__enable_event_thread(evlist, evsel, idx);
404}
405
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300406int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200407{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900408 int nr_cpus = cpu_map__nr(evlist->cpus);
409 int nr_threads = thread_map__nr(evlist->threads);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300410 int nfds = 0;
411 struct perf_evsel *evsel;
412
413 list_for_each_entry(evsel, &evlist->entries, node) {
414 if (evsel->system_wide)
415 nfds += nr_cpus;
416 else
417 nfds += nr_cpus * nr_threads;
418 }
419
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300420 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
421 fdarray__grow(&evlist->pollfd, nfds) < 0)
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300422 return -ENOMEM;
423
424 return 0;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200425}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200426
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300427int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200428{
429 fcntl(fd, F_SETFL, O_NONBLOCK);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300430
431 return fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200432}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200433
Arnaldo Carvalho de Melo1ddec7f2014-08-12 23:04:11 -0300434int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
435{
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300436 return fdarray__filter(&evlist->pollfd, revents_and_mask);
Arnaldo Carvalho de Melo1ddec7f2014-08-12 23:04:11 -0300437}
438
Arnaldo Carvalho de Melof66a8892014-08-18 17:25:59 -0300439int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
440{
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300441 return fdarray__poll(&evlist->pollfd, timeout);
Arnaldo Carvalho de Melof66a8892014-08-18 17:25:59 -0300442}
443
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300444static void perf_evlist__id_hash(struct perf_evlist *evlist,
445 struct perf_evsel *evsel,
446 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200447{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300448 int hash;
449 struct perf_sample_id *sid = SID(evsel, cpu, thread);
450
451 sid->id = id;
452 sid->evsel = evsel;
453 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
454 hlist_add_head(&sid->node, &evlist->heads[hash]);
455}
456
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300457void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
458 int cpu, int thread, u64 id)
459{
460 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
461 evsel->id[evsel->ids++] = id;
462}
463
464static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
465 struct perf_evsel *evsel,
466 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300467{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200468 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300469 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200470 u64 id;
471 int ret;
472
473 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
474 if (!ret)
475 goto add;
476
477 if (errno != ENOTTY)
478 return -1;
479
480 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200481
Jiri Olsac4861af2012-10-12 13:02:21 +0200482 /*
483 * This way does not work with group format read, so bail
484 * out in that case.
485 */
486 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
487 return -1;
488
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200489 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
490 read(fd, &read_data, sizeof(read_data)) == -1)
491 return -1;
492
493 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
494 ++id_idx;
495 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
496 ++id_idx;
497
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200498 id = read_data[id_idx];
499
500 add:
501 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200502 return 0;
503}
504
Jiri Olsa932a3592012-10-11 14:10:35 +0200505struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200506{
507 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200508 struct perf_sample_id *sid;
509 int hash;
510
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200511 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
512 head = &evlist->heads[hash];
513
Sasha Levinb67bfe02013-02-27 17:06:00 -0800514 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200515 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200516 return sid;
517
518 return NULL;
519}
520
521struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
522{
523 struct perf_sample_id *sid;
524
525 if (evlist->nr_entries == 1)
526 return perf_evlist__first(evlist);
527
528 sid = perf_evlist__id2sid(evlist, id);
529 if (sid)
530 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900531
532 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300533 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900534
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200535 return NULL;
536}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200537
Adrian Hunter75562572013-08-27 11:23:09 +0300538static int perf_evlist__event2id(struct perf_evlist *evlist,
539 union perf_event *event, u64 *id)
540{
541 const u64 *array = event->sample.array;
542 ssize_t n;
543
544 n = (event->header.size - sizeof(event->header)) >> 3;
545
546 if (event->header.type == PERF_RECORD_SAMPLE) {
547 if (evlist->id_pos >= n)
548 return -1;
549 *id = array[evlist->id_pos];
550 } else {
551 if (evlist->is_pos > n)
552 return -1;
553 n -= evlist->is_pos;
554 *id = array[n];
555 }
556 return 0;
557}
558
559static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
560 union perf_event *event)
561{
Adrian Hunter98be6962013-09-04 23:18:17 +0300562 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300563 struct hlist_head *head;
564 struct perf_sample_id *sid;
565 int hash;
566 u64 id;
567
568 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300569 return first;
570
571 if (!first->attr.sample_id_all &&
572 event->header.type != PERF_RECORD_SAMPLE)
573 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300574
575 if (perf_evlist__event2id(evlist, event, &id))
576 return NULL;
577
578 /* Synthesized events have an id of zero */
579 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300580 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300581
582 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
583 head = &evlist->heads[hash];
584
585 hlist_for_each_entry(sid, head, node) {
586 if (sid->id == id)
587 return sid->evsel;
588 }
589 return NULL;
590}
591
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300592union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200593{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300594 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200595 unsigned int head = perf_mmap__read_head(md);
596 unsigned int old = md->prev;
597 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200598 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200599
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200600 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200601 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200602 * If we're further behind than half the buffer, there's a chance
603 * the writer will bite our tail and mess up the samples under us.
604 *
605 * If we somehow ended up ahead of the head, we got messed up.
606 *
607 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200608 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200609 int diff = head - old;
610 if (diff > md->mask / 2 || diff < 0) {
611 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
612
613 /*
614 * head points to a known good entry, start there.
615 */
616 old = head;
617 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200618 }
619
620 if (old != head) {
621 size_t size;
622
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200623 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200624 size = event->header.size;
625
626 /*
627 * Event straddles the mmap boundary -- header should always
628 * be inside due to u64 alignment of output.
629 */
630 if ((old & md->mask) + size != ((old + size) & md->mask)) {
631 unsigned int offset = old;
632 unsigned int len = min(sizeof(*event), size), cpy;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200633 void *dst = md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200634
635 do {
636 cpy = min(md->mask + 1 - (offset & md->mask), len);
637 memcpy(dst, &data[offset & md->mask], cpy);
638 offset += cpy;
639 dst += cpy;
640 len -= cpy;
641 } while (len);
642
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200643 event = (union perf_event *) md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200644 }
645
646 old += size;
647 }
648
649 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200650
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200651 return event;
652}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200653
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800654void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
655{
656 if (!evlist->overwrite) {
657 struct perf_mmap *md = &evlist->mmap[idx];
658 unsigned int old = md->prev;
659
660 perf_mmap__write_tail(md, old);
661 }
662}
663
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300664static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
665{
666 if (evlist->mmap[idx].base != NULL) {
667 munmap(evlist->mmap[idx].base, evlist->mmap_len);
668 evlist->mmap[idx].base = NULL;
669 }
670}
671
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200672void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200673{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300674 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200675
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300676 if (evlist->mmap == NULL)
677 return;
678
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300679 for (i = 0; i < evlist->nr_mmaps; i++)
680 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300681
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300682 zfree(&evlist->mmap);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200683}
684
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200685static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200686{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300687 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700688 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900689 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300690 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200691 return evlist->mmap != NULL ? 0 : -ENOMEM;
692}
693
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300694struct mmap_params {
695 int prot;
696 int mask;
697};
698
699static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
700 struct mmap_params *mp, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200701{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300702 evlist->mmap[idx].prev = 0;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300703 evlist->mmap[idx].mask = mp->mask;
704 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200705 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500706 if (evlist->mmap[idx].base == MAP_FAILED) {
Adrian Hunter02635962013-11-01 15:51:33 +0200707 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
708 errno);
Nelson Elhage301b1952011-12-19 08:39:30 -0500709 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200710 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500711 }
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300712
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200713 return 0;
714}
715
Adrian Hunter04e21312013-10-18 15:29:13 +0300716static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300717 struct mmap_params *mp, int cpu,
718 int thread, int *output)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300719{
720 struct perf_evsel *evsel;
Adrian Hunter04e21312013-10-18 15:29:13 +0300721
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300722 evlist__for_each(evlist, evsel) {
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300723 int fd;
724
725 if (evsel->system_wide && thread)
726 continue;
727
728 fd = FD(evsel, cpu, thread);
Adrian Hunter04e21312013-10-18 15:29:13 +0300729
730 if (*output == -1) {
731 *output = fd;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300732 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
Adrian Hunter04e21312013-10-18 15:29:13 +0300733 return -1;
734 } else {
735 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
736 return -1;
737 }
738
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300739 if (perf_evlist__add_pollfd(evlist, fd) < 0)
740 return -1;
Arnaldo Carvalho de Melo033fa712014-09-08 12:55:12 -0300741
Adrian Hunter04e21312013-10-18 15:29:13 +0300742 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
743 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
744 return -1;
745 }
746
747 return 0;
748}
749
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300750static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
751 struct mmap_params *mp)
Adrian Hunter04e21312013-10-18 15:29:13 +0300752{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300753 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900754 int nr_cpus = cpu_map__nr(evlist->cpus);
755 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300756
Adrian Huntere3e1a542013-08-14 15:48:24 +0300757 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900758 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300759 int output = -1;
760
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900761 for (thread = 0; thread < nr_threads; thread++) {
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300762 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
763 thread, &output))
Adrian Hunter04e21312013-10-18 15:29:13 +0300764 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300765 }
766 }
767
768 return 0;
769
770out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300771 for (cpu = 0; cpu < nr_cpus; cpu++)
772 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300773 return -1;
774}
775
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300776static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
777 struct mmap_params *mp)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300778{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300779 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900780 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300781
Adrian Huntere3e1a542013-08-14 15:48:24 +0300782 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900783 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300784 int output = -1;
785
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300786 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
787 &output))
Adrian Hunter04e21312013-10-18 15:29:13 +0300788 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300789 }
790
791 return 0;
792
793out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300794 for (thread = 0; thread < nr_threads; thread++)
795 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300796 return -1;
797}
798
Jiri Olsa994a1f72013-09-01 12:36:12 +0200799static size_t perf_evlist__mmap_size(unsigned long pages)
800{
801 /* 512 kiB: default amount of unprivileged mlocked memory */
802 if (pages == UINT_MAX)
803 pages = (512 * 1024) / page_size;
804 else if (!is_power_of_2(pages))
805 return 0;
806
807 return (pages + 1) * page_size;
808}
809
David Ahern33c2dcf2013-11-12 07:46:55 -0700810static long parse_pages_arg(const char *str, unsigned long min,
811 unsigned long max)
Jiri Olsa994a1f72013-09-01 12:36:12 +0200812{
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300813 unsigned long pages, val;
Jiri Olsa27050f52013-09-01 12:36:13 +0200814 static struct parse_tag tags[] = {
815 { .tag = 'B', .mult = 1 },
816 { .tag = 'K', .mult = 1 << 10 },
817 { .tag = 'M', .mult = 1 << 20 },
818 { .tag = 'G', .mult = 1 << 30 },
819 { .tag = 0 },
820 };
Jiri Olsa994a1f72013-09-01 12:36:12 +0200821
David Ahern89735042013-11-12 07:46:53 -0700822 if (str == NULL)
David Ahern33c2dcf2013-11-12 07:46:55 -0700823 return -EINVAL;
David Ahern89735042013-11-12 07:46:53 -0700824
Jiri Olsa27050f52013-09-01 12:36:13 +0200825 val = parse_tag_value(str, tags);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300826 if (val != (unsigned long) -1) {
Jiri Olsa27050f52013-09-01 12:36:13 +0200827 /* we got file size value */
828 pages = PERF_ALIGN(val, page_size) / page_size;
Jiri Olsa27050f52013-09-01 12:36:13 +0200829 } else {
830 /* we got pages count value */
831 char *eptr;
832 pages = strtoul(str, &eptr, 10);
David Ahern33c2dcf2013-11-12 07:46:55 -0700833 if (*eptr != '\0')
834 return -EINVAL;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200835 }
836
Adrian Hunter2bcab6c2013-12-09 15:18:37 +0200837 if (pages == 0 && min == 0) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700838 /* leave number of pages at 0 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200839 } else if (!is_power_of_2(pages)) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700840 /* round pages up to next power of 2 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200841 pages = next_pow2_l(pages);
842 if (!pages)
843 return -EINVAL;
David Ahern96398372013-11-12 07:46:54 -0700844 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
845 pages * page_size, pages);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300846 }
847
David Ahern33c2dcf2013-11-12 07:46:55 -0700848 if (pages > max)
849 return -EINVAL;
850
851 return pages;
852}
853
854int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
855 int unset __maybe_unused)
856{
857 unsigned int *mmap_pages = opt->value;
858 unsigned long max = UINT_MAX;
859 long pages;
860
Adrian Hunterf5ae9c42013-12-09 15:18:38 +0200861 if (max > SIZE_MAX / page_size)
David Ahern33c2dcf2013-11-12 07:46:55 -0700862 max = SIZE_MAX / page_size;
863
864 pages = parse_pages_arg(str, 1, max);
865 if (pages < 0) {
866 pr_err("Invalid argument for --mmap_pages/-m\n");
Jiri Olsa994a1f72013-09-01 12:36:12 +0200867 return -1;
868 }
869
870 *mmap_pages = pages;
871 return 0;
872}
873
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300874/**
875 * perf_evlist__mmap - Create mmaps to receive events.
876 * @evlist: list of events
877 * @pages: map length in pages
878 * @overwrite: overwrite older events?
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200879 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300880 * If @overwrite is %false the user needs to signal event consumption using
881 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
882 * automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200883 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300884 * Return: %0 on success, negative error code otherwise.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200885 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200886int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
887 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200888{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300889 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200890 const struct cpu_map *cpus = evlist->cpus;
891 const struct thread_map *threads = evlist->threads;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300892 struct mmap_params mp = {
893 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
894 };
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200895
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200896 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200897 return -ENOMEM;
898
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300899 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200900 return -ENOMEM;
901
902 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200903 evlist->mmap_len = perf_evlist__mmap_size(pages);
Adrian Hunter2af68ef2013-10-18 15:29:07 +0300904 pr_debug("mmap size %zuB\n", evlist->mmap_len);
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300905 mp.mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200906
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300907 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200908 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300909 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300910 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200911 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200912 }
913
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700914 if (cpu_map__empty(cpus))
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300915 return perf_evlist__mmap_per_thread(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200916
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300917 return perf_evlist__mmap_per_cpu(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200918}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200919
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300920int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200921{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900922 evlist->threads = thread_map__new_str(target->pid, target->tid,
923 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200924
925 if (evlist->threads == NULL)
926 return -1;
927
Dongsheng Yang9c105fb2013-12-04 17:56:40 -0500928 if (target__uses_dummy_map(target))
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900929 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900930 else
931 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200932
933 if (evlist->cpus == NULL)
934 goto out_delete_threads;
935
936 return 0;
937
938out_delete_threads:
939 thread_map__delete(evlist->threads);
940 return -1;
941}
942
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300943int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100944{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100945 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300946 int err = 0;
947 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900948 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100949
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300950 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300951 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100952 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300953
954 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
955 if (err)
956 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100957 }
958
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300959 return err;
960}
961
962int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
963{
964 struct perf_evsel *evsel;
965 int err = 0;
966 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900967 nthreads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300968
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300969 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300970 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
971 if (err)
972 break;
973 }
974
975 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100976}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200977
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300978bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200979{
Adrian Hunter75562572013-08-27 11:23:09 +0300980 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300981
Adrian Hunter75562572013-08-27 11:23:09 +0300982 if (evlist->nr_entries == 1)
983 return true;
984
985 if (evlist->id_pos < 0 || evlist->is_pos < 0)
986 return false;
987
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300988 evlist__for_each(evlist, pos) {
Adrian Hunter75562572013-08-27 11:23:09 +0300989 if (pos->id_pos != evlist->id_pos ||
990 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300991 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200992 }
993
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300994 return true;
995}
996
Adrian Hunter75562572013-08-27 11:23:09 +0300997u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300998{
Adrian Hunter75562572013-08-27 11:23:09 +0300999 struct perf_evsel *evsel;
1000
1001 if (evlist->combined_sample_type)
1002 return evlist->combined_sample_type;
1003
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001004 evlist__for_each(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +03001005 evlist->combined_sample_type |= evsel->attr.sample_type;
1006
1007 return evlist->combined_sample_type;
1008}
1009
1010u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1011{
1012 evlist->combined_sample_type = 0;
1013 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001014}
1015
Jiri Olsa9ede4732012-10-10 17:38:13 +02001016bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1017{
1018 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1019 u64 read_format = first->attr.read_format;
1020 u64 sample_type = first->attr.sample_type;
1021
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001022 evlist__for_each(evlist, pos) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001023 if (read_format != pos->attr.read_format)
1024 return false;
1025 }
1026
1027 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1028 if ((sample_type & PERF_SAMPLE_READ) &&
1029 !(read_format & PERF_FORMAT_ID)) {
1030 return false;
1031 }
1032
1033 return true;
1034}
1035
1036u64 perf_evlist__read_format(struct perf_evlist *evlist)
1037{
1038 struct perf_evsel *first = perf_evlist__first(evlist);
1039 return first->attr.read_format;
1040}
1041
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001042u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001043{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001044 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001045 struct perf_sample *data;
1046 u64 sample_type;
1047 u16 size = 0;
1048
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001049 if (!first->attr.sample_id_all)
1050 goto out;
1051
1052 sample_type = first->attr.sample_type;
1053
1054 if (sample_type & PERF_SAMPLE_TID)
1055 size += sizeof(data->tid) * 2;
1056
1057 if (sample_type & PERF_SAMPLE_TIME)
1058 size += sizeof(data->time);
1059
1060 if (sample_type & PERF_SAMPLE_ID)
1061 size += sizeof(data->id);
1062
1063 if (sample_type & PERF_SAMPLE_STREAM_ID)
1064 size += sizeof(data->stream_id);
1065
1066 if (sample_type & PERF_SAMPLE_CPU)
1067 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +03001068
1069 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1070 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001071out:
1072 return size;
1073}
1074
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001075bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001076{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001077 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001078
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001079 evlist__for_each_continue(evlist, pos) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001080 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1081 return false;
1082 }
1083
1084 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001085}
1086
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001087bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001088{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001089 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001090 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001091}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001092
1093void perf_evlist__set_selected(struct perf_evlist *evlist,
1094 struct perf_evsel *evsel)
1095{
1096 evlist->selected = evsel;
1097}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001098
Namhyung Kima74b4b62013-03-15 14:48:48 +09001099void perf_evlist__close(struct perf_evlist *evlist)
1100{
1101 struct perf_evsel *evsel;
1102 int ncpus = cpu_map__nr(evlist->cpus);
1103 int nthreads = thread_map__nr(evlist->threads);
Stephane Eranian8ad92192014-01-17 16:34:06 +01001104 int n;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001105
Stephane Eranian8ad92192014-01-17 16:34:06 +01001106 evlist__for_each_reverse(evlist, evsel) {
1107 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1108 perf_evsel__close(evsel, n, nthreads);
1109 }
Namhyung Kima74b4b62013-03-15 14:48:48 +09001110}
1111
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001112int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001113{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001114 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001115 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001116
Adrian Hunter733cd2f2013-09-06 22:40:11 +03001117 perf_evlist__update_id_pos(evlist);
1118
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001119 evlist__for_each(evlist, evsel) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001120 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001121 if (err < 0)
1122 goto out_err;
1123 }
1124
1125 return 0;
1126out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001127 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001128 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001129 return err;
1130}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001131
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001132int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001133 const char *argv[], bool pipe_output,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001134 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001135{
1136 int child_ready_pipe[2], go_pipe[2];
1137 char bf;
1138
1139 if (pipe(child_ready_pipe) < 0) {
1140 perror("failed to create 'ready' pipe");
1141 return -1;
1142 }
1143
1144 if (pipe(go_pipe) < 0) {
1145 perror("failed to create 'go' pipe");
1146 goto out_close_ready_pipe;
1147 }
1148
1149 evlist->workload.pid = fork();
1150 if (evlist->workload.pid < 0) {
1151 perror("failed to fork");
1152 goto out_close_pipes;
1153 }
1154
1155 if (!evlist->workload.pid) {
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001156 int ret;
1157
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001158 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001159 dup2(2, 1);
1160
David Ahern0817df02013-05-25 17:50:39 -06001161 signal(SIGTERM, SIG_DFL);
1162
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001163 close(child_ready_pipe[0]);
1164 close(go_pipe[1]);
1165 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1166
1167 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001168 * Tell the parent we're ready to go
1169 */
1170 close(child_ready_pipe[1]);
1171
1172 /*
1173 * Wait until the parent tells us to go.
1174 */
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001175 ret = read(go_pipe[0], &bf, 1);
1176 /*
1177 * The parent will ask for the execvp() to be performed by
1178 * writing exactly one byte, in workload.cork_fd, usually via
1179 * perf_evlist__start_workload().
1180 *
1181 * For cancelling the workload without actuallin running it,
1182 * the parent will just close workload.cork_fd, without writing
1183 * anything, i.e. read will return zero and we just exit()
1184 * here.
1185 */
1186 if (ret != 1) {
1187 if (ret == -1)
1188 perror("unable to read pipe");
1189 exit(ret);
1190 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001191
1192 execvp(argv[0], (char **)argv);
1193
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001194 if (exec_error) {
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001195 union sigval val;
1196
1197 val.sival_int = errno;
1198 if (sigqueue(getppid(), SIGUSR1, val))
1199 perror(argv[0]);
1200 } else
1201 perror(argv[0]);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001202 exit(-1);
1203 }
1204
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001205 if (exec_error) {
1206 struct sigaction act = {
1207 .sa_flags = SA_SIGINFO,
1208 .sa_sigaction = exec_error,
1209 };
1210 sigaction(SIGUSR1, &act, NULL);
1211 }
1212
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001213 if (target__none(target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001214 evlist->threads->map[0] = evlist->workload.pid;
1215
1216 close(child_ready_pipe[1]);
1217 close(go_pipe[0]);
1218 /*
1219 * wait for child to settle
1220 */
1221 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1222 perror("unable to read pipe");
1223 goto out_close_pipes;
1224 }
1225
Namhyung Kimbcf31452013-06-26 16:14:15 +09001226 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001227 evlist->workload.cork_fd = go_pipe[1];
1228 close(child_ready_pipe[0]);
1229 return 0;
1230
1231out_close_pipes:
1232 close(go_pipe[0]);
1233 close(go_pipe[1]);
1234out_close_ready_pipe:
1235 close(child_ready_pipe[0]);
1236 close(child_ready_pipe[1]);
1237 return -1;
1238}
1239
1240int perf_evlist__start_workload(struct perf_evlist *evlist)
1241{
1242 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001243 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001244 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001245 /*
1246 * Remove the cork, let it rip!
1247 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001248 ret = write(evlist->workload.cork_fd, &bf, 1);
1249 if (ret < 0)
1250 perror("enable to write to pipe");
1251
1252 close(evlist->workload.cork_fd);
1253 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001254 }
1255
1256 return 0;
1257}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001258
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001259int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001260 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001261{
Adrian Hunter75562572013-08-27 11:23:09 +03001262 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1263
1264 if (!evsel)
1265 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001266 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001267}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001268
1269size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1270{
1271 struct perf_evsel *evsel;
1272 size_t printed = 0;
1273
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001274 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001275 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1276 perf_evsel__name(evsel));
1277 }
1278
Davidlohr Buesob2222132013-11-12 22:24:24 -08001279 return printed + fprintf(fp, "\n");
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001280}
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03001281
1282int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1283 int err, char *buf, size_t size)
1284{
1285 char sbuf[128];
1286
1287 switch (err) {
1288 case ENOENT:
1289 scnprintf(buf, size, "%s",
1290 "Error:\tUnable to find debugfs\n"
1291 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1292 "Hint:\tIs the debugfs filesystem mounted?\n"
1293 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1294 break;
1295 case EACCES:
1296 scnprintf(buf, size,
1297 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1298 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1299 debugfs_mountpoint, debugfs_mountpoint);
1300 break;
1301 default:
1302 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1303 break;
1304 }
1305
1306 return 0;
1307}
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001308
1309int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1310 int err, char *buf, size_t size)
1311{
1312 int printed, value;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00001313 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001314
1315 switch (err) {
1316 case EACCES:
1317 case EPERM:
1318 printed = scnprintf(buf, size,
1319 "Error:\t%s.\n"
1320 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1321
Adrian Hunter1a472452013-12-11 14:36:23 +02001322 value = perf_event_paranoid();
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001323
1324 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1325
1326 if (value >= 2) {
1327 printed += scnprintf(buf + printed, size - printed,
1328 "For your workloads it needs to be <= 1\nHint:\t");
1329 }
1330 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001331 "For system wide tracing it needs to be set to -1.\n");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001332
1333 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001334 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1335 "Hint:\tThe current value is %d.", value);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001336 break;
1337 default:
1338 scnprintf(buf, size, "%s", emsg);
1339 break;
1340 }
1341
1342 return 0;
1343}
Adrian Huntera025e4f2013-12-11 14:36:35 +02001344
1345void perf_evlist__to_front(struct perf_evlist *evlist,
1346 struct perf_evsel *move_evsel)
1347{
1348 struct perf_evsel *evsel, *n;
1349 LIST_HEAD(move);
1350
1351 if (move_evsel == perf_evlist__first(evlist))
1352 return;
1353
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001354 evlist__for_each_safe(evlist, n, evsel) {
Adrian Huntera025e4f2013-12-11 14:36:35 +02001355 if (evsel->leader == move_evsel->leader)
1356 list_move_tail(&evsel->node, &move);
1357 }
1358
1359 list_splice(&move, &evlist->entries);
1360}
Adrian Hunter60b08962014-07-31 09:00:52 +03001361
1362void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1363 struct perf_evsel *tracking_evsel)
1364{
1365 struct perf_evsel *evsel;
1366
1367 if (tracking_evsel->tracking)
1368 return;
1369
1370 evlist__for_each(evlist, evsel) {
1371 if (evsel != tracking_evsel)
1372 evsel->tracking = false;
1373 }
1374
1375 tracking_evsel->tracking = true;
1376}