blob: 502cd11ab17e52ae219d9b4df71882280fee0c82 [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 Melo35b9d882011-11-09 08:47:15 -020040 evlist->workload.pid = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020041}
42
Namhyung Kim334fe7a2013-03-11 16:43:12 +090043struct perf_evlist *perf_evlist__new(void)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020044{
45 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
46
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020047 if (evlist != NULL)
Namhyung Kim334fe7a2013-03-11 16:43:12 +090048 perf_evlist__init(evlist, NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020049
50 return evlist;
51}
52
Jiri Olsab22d54b2013-09-01 12:36:14 +020053struct perf_evlist *perf_evlist__new_default(void)
54{
55 struct perf_evlist *evlist = perf_evlist__new();
56
57 if (evlist && perf_evlist__add_default(evlist)) {
58 perf_evlist__delete(evlist);
59 evlist = NULL;
60 }
61
62 return evlist;
63}
64
Adrian Hunter75562572013-08-27 11:23:09 +030065/**
66 * perf_evlist__set_id_pos - set the positions of event ids.
67 * @evlist: selected event list
68 *
69 * Events with compatible sample types all have the same id_pos
70 * and is_pos. For convenience, put a copy on evlist.
71 */
72void perf_evlist__set_id_pos(struct perf_evlist *evlist)
73{
74 struct perf_evsel *first = perf_evlist__first(evlist);
75
76 evlist->id_pos = first->id_pos;
77 evlist->is_pos = first->is_pos;
78}
79
Adrian Hunter733cd2f2013-09-06 22:40:11 +030080static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
81{
82 struct perf_evsel *evsel;
83
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030084 evlist__for_each(evlist, evsel)
Adrian Hunter733cd2f2013-09-06 22:40:11 +030085 perf_evsel__calc_id_pos(evsel);
86
87 perf_evlist__set_id_pos(evlist);
88}
89
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020090static void perf_evlist__purge(struct perf_evlist *evlist)
91{
92 struct perf_evsel *pos, *n;
93
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030094 evlist__for_each_safe(evlist, n, pos) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020095 list_del_init(&pos->node);
96 perf_evsel__delete(pos);
97 }
98
99 evlist->nr_entries = 0;
100}
101
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200102void perf_evlist__exit(struct perf_evlist *evlist)
103{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300104 zfree(&evlist->mmap);
105 zfree(&evlist->pollfd);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200106}
107
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200108void perf_evlist__delete(struct perf_evlist *evlist)
109{
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300110 perf_evlist__munmap(evlist);
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -0300111 perf_evlist__close(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300112 cpu_map__delete(evlist->cpus);
113 thread_map__delete(evlist->threads);
114 evlist->cpus = NULL;
115 evlist->threads = NULL;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200116 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200117 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200118 free(evlist);
119}
120
121void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
122{
123 list_add_tail(&entry->node, &evlist->entries);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300124 entry->idx = evlist->nr_entries;
Adrian Hunter60b08962014-07-31 09:00:52 +0300125 entry->tracking = !entry->idx;
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300126
Adrian Hunter75562572013-08-27 11:23:09 +0300127 if (!evlist->nr_entries++)
128 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200129}
130
Jiri Olsa0529bc12012-01-27 15:34:20 +0100131void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
132 struct list_head *list,
133 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200134{
Adrian Hunter75562572013-08-27 11:23:09 +0300135 bool set_id_pos = !evlist->nr_entries;
136
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200137 list_splice_tail(list, &evlist->entries);
138 evlist->nr_entries += nr_entries;
Adrian Hunter75562572013-08-27 11:23:09 +0300139 if (set_id_pos)
140 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200141}
142
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300143void __perf_evlist__set_leader(struct list_head *list)
144{
145 struct perf_evsel *evsel, *leader;
146
147 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900148 evsel = list_entry(list->prev, struct perf_evsel, node);
149
150 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300151
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300152 __evlist__for_each(list, evsel) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100153 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300154 }
155}
156
157void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200158{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900159 if (evlist->nr_entries) {
160 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300161 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900162 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200163}
164
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200165int perf_evlist__add_default(struct perf_evlist *evlist)
166{
167 struct perf_event_attr attr = {
168 .type = PERF_TYPE_HARDWARE,
169 .config = PERF_COUNT_HW_CPU_CYCLES,
170 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100171 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200172
Joerg Roedel1aed2672012-01-04 17:54:20 +0100173 event_attr_init(&attr);
174
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300175 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200176 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200177 goto error;
178
179 /* use strdup() because free(evsel) assumes name is allocated */
180 evsel->name = strdup("cycles");
181 if (!evsel->name)
182 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200183
184 perf_evlist__add(evlist, evsel);
185 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200186error_free:
187 perf_evsel__delete(evsel);
188error:
189 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200190}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200191
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300192static int perf_evlist__add_attrs(struct perf_evlist *evlist,
193 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200194{
195 struct perf_evsel *evsel, *n;
196 LIST_HEAD(head);
197 size_t i;
198
199 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300200 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200201 if (evsel == NULL)
202 goto out_delete_partial_list;
203 list_add_tail(&evsel->node, &head);
204 }
205
206 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
207
208 return 0;
209
210out_delete_partial_list:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300211 __evlist__for_each_safe(&head, n, evsel)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200212 perf_evsel__delete(evsel);
213 return -1;
214}
215
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300216int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
217 struct perf_event_attr *attrs, size_t nr_attrs)
218{
219 size_t i;
220
221 for (i = 0; i < nr_attrs; i++)
222 event_attr_init(attrs + i);
223
224 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
225}
226
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300227struct perf_evsel *
228perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200229{
230 struct perf_evsel *evsel;
231
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300232 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200233 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
234 (int)evsel->attr.config == id)
235 return evsel;
236 }
237
238 return NULL;
239}
240
David Aherna2f28042013-08-28 22:29:51 -0600241struct perf_evsel *
242perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
243 const char *name)
244{
245 struct perf_evsel *evsel;
246
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300247 evlist__for_each(evlist, evsel) {
David Aherna2f28042013-08-28 22:29:51 -0600248 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
249 (strcmp(evsel->name, name) == 0))
250 return evsel;
251 }
252
253 return NULL;
254}
255
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300256int perf_evlist__add_newtp(struct perf_evlist *evlist,
257 const char *sys, const char *name, void *handler)
258{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300259 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300260
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300261 if (evsel == NULL)
262 return -1;
263
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300264 evsel->handler = handler;
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300265 perf_evlist__add(evlist, evsel);
266 return 0;
267}
268
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300269static int perf_evlist__nr_threads(struct perf_evlist *evlist,
270 struct perf_evsel *evsel)
271{
272 if (evsel->system_wide)
273 return 1;
274 else
275 return thread_map__nr(evlist->threads);
276}
277
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300278void perf_evlist__disable(struct perf_evlist *evlist)
279{
280 int cpu, thread;
281 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900282 int nr_cpus = cpu_map__nr(evlist->cpus);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300283 int nr_threads;
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300284
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900285 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300286 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300287 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100288 continue;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300289 nr_threads = perf_evlist__nr_threads(evlist, pos);
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900290 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900291 ioctl(FD(pos, cpu, thread),
292 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300293 }
294 }
295}
296
David Ahern764e16a32011-08-25 10:17:55 -0600297void perf_evlist__enable(struct perf_evlist *evlist)
298{
299 int cpu, thread;
300 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900301 int nr_cpus = cpu_map__nr(evlist->cpus);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300302 int nr_threads;
David Ahern764e16a32011-08-25 10:17:55 -0600303
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900304 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300305 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300306 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100307 continue;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300308 nr_threads = perf_evlist__nr_threads(evlist, pos);
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900309 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900310 ioctl(FD(pos, cpu, thread),
311 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600312 }
313 }
314}
315
Adrian Hunter395c3072013-08-31 21:50:53 +0300316int perf_evlist__disable_event(struct perf_evlist *evlist,
317 struct perf_evsel *evsel)
318{
319 int cpu, thread, err;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300320 int nr_cpus = cpu_map__nr(evlist->cpus);
321 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
Adrian Hunter395c3072013-08-31 21:50:53 +0300322
323 if (!evsel->fd)
324 return 0;
325
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300326 for (cpu = 0; cpu < nr_cpus; cpu++) {
327 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300328 err = ioctl(FD(evsel, cpu, thread),
329 PERF_EVENT_IOC_DISABLE, 0);
330 if (err)
331 return err;
332 }
333 }
334 return 0;
335}
336
337int perf_evlist__enable_event(struct perf_evlist *evlist,
338 struct perf_evsel *evsel)
339{
340 int cpu, thread, err;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300341 int nr_cpus = cpu_map__nr(evlist->cpus);
342 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
Adrian Hunter395c3072013-08-31 21:50:53 +0300343
344 if (!evsel->fd)
345 return -EINVAL;
346
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300347 for (cpu = 0; cpu < nr_cpus; cpu++) {
348 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300349 err = ioctl(FD(evsel, cpu, thread),
350 PERF_EVENT_IOC_ENABLE, 0);
351 if (err)
352 return err;
353 }
354 }
355 return 0;
356}
357
Adrian Hunter1c650562014-07-31 09:00:56 +0300358static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
359 struct perf_evsel *evsel, int cpu)
360{
361 int thread, err;
362 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
363
364 if (!evsel->fd)
365 return -EINVAL;
366
367 for (thread = 0; thread < nr_threads; thread++) {
368 err = ioctl(FD(evsel, cpu, thread),
369 PERF_EVENT_IOC_ENABLE, 0);
370 if (err)
371 return err;
372 }
373 return 0;
374}
375
376static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
377 struct perf_evsel *evsel,
378 int thread)
379{
380 int cpu, err;
381 int nr_cpus = cpu_map__nr(evlist->cpus);
382
383 if (!evsel->fd)
384 return -EINVAL;
385
386 for (cpu = 0; cpu < nr_cpus; cpu++) {
387 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
388 if (err)
389 return err;
390 }
391 return 0;
392}
393
394int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
395 struct perf_evsel *evsel, int idx)
396{
397 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
398
399 if (per_cpu_mmaps)
400 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
401 else
402 return perf_evlist__enable_event_thread(evlist, evsel, idx);
403}
404
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200405static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200406{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900407 int nr_cpus = cpu_map__nr(evlist->cpus);
408 int nr_threads = thread_map__nr(evlist->threads);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300409 int nfds = 0;
410 struct perf_evsel *evsel;
411
412 list_for_each_entry(evsel, &evlist->entries, node) {
413 if (evsel->system_wide)
414 nfds += nr_cpus;
415 else
416 nfds += nr_cpus * nr_threads;
417 }
418
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200419 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
420 return evlist->pollfd != NULL ? 0 : -ENOMEM;
421}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200422
423void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
424{
425 fcntl(fd, F_SETFL, O_NONBLOCK);
426 evlist->pollfd[evlist->nr_fds].fd = fd;
Arnaldo Carvalho de Melo81796722014-08-13 11:26:21 -0300427 evlist->pollfd[evlist->nr_fds].events = POLLIN | POLLERR | POLLHUP;
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200428 evlist->nr_fds++;
429}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200430
Arnaldo Carvalho de Melo1ddec7f2014-08-12 23:04:11 -0300431int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
432{
433 int fd, nr_fds = 0;
434
435 if (evlist->nr_fds == 0)
436 return 0;
437
438 for (fd = 0; fd < evlist->nr_fds; ++fd) {
439 if (evlist->pollfd[fd].revents & revents_and_mask)
440 continue;
441
442 if (fd != nr_fds)
443 evlist->pollfd[nr_fds] = evlist->pollfd[fd];
444
445 ++nr_fds;
446 }
447
448 evlist->nr_fds = nr_fds;
449 return nr_fds;
450}
451
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300452static void perf_evlist__id_hash(struct perf_evlist *evlist,
453 struct perf_evsel *evsel,
454 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200455{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300456 int hash;
457 struct perf_sample_id *sid = SID(evsel, cpu, thread);
458
459 sid->id = id;
460 sid->evsel = evsel;
461 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
462 hlist_add_head(&sid->node, &evlist->heads[hash]);
463}
464
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300465void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
466 int cpu, int thread, u64 id)
467{
468 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
469 evsel->id[evsel->ids++] = id;
470}
471
472static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
473 struct perf_evsel *evsel,
474 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300475{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200476 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300477 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200478 u64 id;
479 int ret;
480
481 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
482 if (!ret)
483 goto add;
484
485 if (errno != ENOTTY)
486 return -1;
487
488 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200489
Jiri Olsac4861af2012-10-12 13:02:21 +0200490 /*
491 * This way does not work with group format read, so bail
492 * out in that case.
493 */
494 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
495 return -1;
496
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200497 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
498 read(fd, &read_data, sizeof(read_data)) == -1)
499 return -1;
500
501 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
502 ++id_idx;
503 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
504 ++id_idx;
505
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200506 id = read_data[id_idx];
507
508 add:
509 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200510 return 0;
511}
512
Jiri Olsa932a3592012-10-11 14:10:35 +0200513struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200514{
515 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200516 struct perf_sample_id *sid;
517 int hash;
518
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200519 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
520 head = &evlist->heads[hash];
521
Sasha Levinb67bfe02013-02-27 17:06:00 -0800522 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200523 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200524 return sid;
525
526 return NULL;
527}
528
529struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
530{
531 struct perf_sample_id *sid;
532
533 if (evlist->nr_entries == 1)
534 return perf_evlist__first(evlist);
535
536 sid = perf_evlist__id2sid(evlist, id);
537 if (sid)
538 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900539
540 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300541 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900542
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200543 return NULL;
544}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200545
Adrian Hunter75562572013-08-27 11:23:09 +0300546static int perf_evlist__event2id(struct perf_evlist *evlist,
547 union perf_event *event, u64 *id)
548{
549 const u64 *array = event->sample.array;
550 ssize_t n;
551
552 n = (event->header.size - sizeof(event->header)) >> 3;
553
554 if (event->header.type == PERF_RECORD_SAMPLE) {
555 if (evlist->id_pos >= n)
556 return -1;
557 *id = array[evlist->id_pos];
558 } else {
559 if (evlist->is_pos > n)
560 return -1;
561 n -= evlist->is_pos;
562 *id = array[n];
563 }
564 return 0;
565}
566
567static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
568 union perf_event *event)
569{
Adrian Hunter98be6962013-09-04 23:18:17 +0300570 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300571 struct hlist_head *head;
572 struct perf_sample_id *sid;
573 int hash;
574 u64 id;
575
576 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300577 return first;
578
579 if (!first->attr.sample_id_all &&
580 event->header.type != PERF_RECORD_SAMPLE)
581 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300582
583 if (perf_evlist__event2id(evlist, event, &id))
584 return NULL;
585
586 /* Synthesized events have an id of zero */
587 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300588 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300589
590 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
591 head = &evlist->heads[hash];
592
593 hlist_for_each_entry(sid, head, node) {
594 if (sid->id == id)
595 return sid->evsel;
596 }
597 return NULL;
598}
599
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300600union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200601{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300602 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200603 unsigned int head = perf_mmap__read_head(md);
604 unsigned int old = md->prev;
605 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200606 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200607
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200608 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200609 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200610 * If we're further behind than half the buffer, there's a chance
611 * the writer will bite our tail and mess up the samples under us.
612 *
613 * If we somehow ended up ahead of the head, we got messed up.
614 *
615 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200616 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200617 int diff = head - old;
618 if (diff > md->mask / 2 || diff < 0) {
619 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
620
621 /*
622 * head points to a known good entry, start there.
623 */
624 old = head;
625 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200626 }
627
628 if (old != head) {
629 size_t size;
630
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200631 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200632 size = event->header.size;
633
634 /*
635 * Event straddles the mmap boundary -- header should always
636 * be inside due to u64 alignment of output.
637 */
638 if ((old & md->mask) + size != ((old + size) & md->mask)) {
639 unsigned int offset = old;
640 unsigned int len = min(sizeof(*event), size), cpy;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200641 void *dst = md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200642
643 do {
644 cpy = min(md->mask + 1 - (offset & md->mask), len);
645 memcpy(dst, &data[offset & md->mask], cpy);
646 offset += cpy;
647 dst += cpy;
648 len -= cpy;
649 } while (len);
650
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200651 event = (union perf_event *) md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200652 }
653
654 old += size;
655 }
656
657 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200658
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200659 return event;
660}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200661
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800662void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
663{
664 if (!evlist->overwrite) {
665 struct perf_mmap *md = &evlist->mmap[idx];
666 unsigned int old = md->prev;
667
668 perf_mmap__write_tail(md, old);
669 }
670}
671
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300672static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
673{
674 if (evlist->mmap[idx].base != NULL) {
675 munmap(evlist->mmap[idx].base, evlist->mmap_len);
676 evlist->mmap[idx].base = NULL;
677 }
678}
679
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200680void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200681{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300682 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200683
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300684 if (evlist->mmap == NULL)
685 return;
686
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300687 for (i = 0; i < evlist->nr_mmaps; i++)
688 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300689
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300690 zfree(&evlist->mmap);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200691}
692
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200693static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200694{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300695 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700696 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900697 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300698 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200699 return evlist->mmap != NULL ? 0 : -ENOMEM;
700}
701
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300702struct mmap_params {
703 int prot;
704 int mask;
705};
706
707static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
708 struct mmap_params *mp, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200709{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300710 evlist->mmap[idx].prev = 0;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300711 evlist->mmap[idx].mask = mp->mask;
712 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200713 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500714 if (evlist->mmap[idx].base == MAP_FAILED) {
Adrian Hunter02635962013-11-01 15:51:33 +0200715 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
716 errno);
Nelson Elhage301b1952011-12-19 08:39:30 -0500717 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200718 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500719 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200720
721 perf_evlist__add_pollfd(evlist, fd);
722 return 0;
723}
724
Adrian Hunter04e21312013-10-18 15:29:13 +0300725static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300726 struct mmap_params *mp, int cpu,
727 int thread, int *output)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300728{
729 struct perf_evsel *evsel;
Adrian Hunter04e21312013-10-18 15:29:13 +0300730
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300731 evlist__for_each(evlist, evsel) {
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300732 int fd;
733
734 if (evsel->system_wide && thread)
735 continue;
736
737 fd = FD(evsel, cpu, thread);
Adrian Hunter04e21312013-10-18 15:29:13 +0300738
739 if (*output == -1) {
740 *output = fd;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300741 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
Adrian Hunter04e21312013-10-18 15:29:13 +0300742 return -1;
743 } else {
744 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
745 return -1;
746 }
747
748 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
749 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
750 return -1;
751 }
752
753 return 0;
754}
755
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300756static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
757 struct mmap_params *mp)
Adrian Hunter04e21312013-10-18 15:29:13 +0300758{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300759 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900760 int nr_cpus = cpu_map__nr(evlist->cpus);
761 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300762
Adrian Huntere3e1a542013-08-14 15:48:24 +0300763 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900764 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300765 int output = -1;
766
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900767 for (thread = 0; thread < nr_threads; thread++) {
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300768 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
769 thread, &output))
Adrian Hunter04e21312013-10-18 15:29:13 +0300770 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300771 }
772 }
773
774 return 0;
775
776out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300777 for (cpu = 0; cpu < nr_cpus; cpu++)
778 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300779 return -1;
780}
781
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300782static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
783 struct mmap_params *mp)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300784{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300785 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900786 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300787
Adrian Huntere3e1a542013-08-14 15:48:24 +0300788 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900789 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300790 int output = -1;
791
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300792 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
793 &output))
Adrian Hunter04e21312013-10-18 15:29:13 +0300794 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300795 }
796
797 return 0;
798
799out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300800 for (thread = 0; thread < nr_threads; thread++)
801 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300802 return -1;
803}
804
Jiri Olsa994a1f72013-09-01 12:36:12 +0200805static size_t perf_evlist__mmap_size(unsigned long pages)
806{
807 /* 512 kiB: default amount of unprivileged mlocked memory */
808 if (pages == UINT_MAX)
809 pages = (512 * 1024) / page_size;
810 else if (!is_power_of_2(pages))
811 return 0;
812
813 return (pages + 1) * page_size;
814}
815
David Ahern33c2dcf2013-11-12 07:46:55 -0700816static long parse_pages_arg(const char *str, unsigned long min,
817 unsigned long max)
Jiri Olsa994a1f72013-09-01 12:36:12 +0200818{
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300819 unsigned long pages, val;
Jiri Olsa27050f52013-09-01 12:36:13 +0200820 static struct parse_tag tags[] = {
821 { .tag = 'B', .mult = 1 },
822 { .tag = 'K', .mult = 1 << 10 },
823 { .tag = 'M', .mult = 1 << 20 },
824 { .tag = 'G', .mult = 1 << 30 },
825 { .tag = 0 },
826 };
Jiri Olsa994a1f72013-09-01 12:36:12 +0200827
David Ahern89735042013-11-12 07:46:53 -0700828 if (str == NULL)
David Ahern33c2dcf2013-11-12 07:46:55 -0700829 return -EINVAL;
David Ahern89735042013-11-12 07:46:53 -0700830
Jiri Olsa27050f52013-09-01 12:36:13 +0200831 val = parse_tag_value(str, tags);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300832 if (val != (unsigned long) -1) {
Jiri Olsa27050f52013-09-01 12:36:13 +0200833 /* we got file size value */
834 pages = PERF_ALIGN(val, page_size) / page_size;
Jiri Olsa27050f52013-09-01 12:36:13 +0200835 } else {
836 /* we got pages count value */
837 char *eptr;
838 pages = strtoul(str, &eptr, 10);
David Ahern33c2dcf2013-11-12 07:46:55 -0700839 if (*eptr != '\0')
840 return -EINVAL;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200841 }
842
Adrian Hunter2bcab6c2013-12-09 15:18:37 +0200843 if (pages == 0 && min == 0) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700844 /* leave number of pages at 0 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200845 } else if (!is_power_of_2(pages)) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700846 /* round pages up to next power of 2 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200847 pages = next_pow2_l(pages);
848 if (!pages)
849 return -EINVAL;
David Ahern96398372013-11-12 07:46:54 -0700850 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
851 pages * page_size, pages);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300852 }
853
David Ahern33c2dcf2013-11-12 07:46:55 -0700854 if (pages > max)
855 return -EINVAL;
856
857 return pages;
858}
859
860int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
861 int unset __maybe_unused)
862{
863 unsigned int *mmap_pages = opt->value;
864 unsigned long max = UINT_MAX;
865 long pages;
866
Adrian Hunterf5ae9c42013-12-09 15:18:38 +0200867 if (max > SIZE_MAX / page_size)
David Ahern33c2dcf2013-11-12 07:46:55 -0700868 max = SIZE_MAX / page_size;
869
870 pages = parse_pages_arg(str, 1, max);
871 if (pages < 0) {
872 pr_err("Invalid argument for --mmap_pages/-m\n");
Jiri Olsa994a1f72013-09-01 12:36:12 +0200873 return -1;
874 }
875
876 *mmap_pages = pages;
877 return 0;
878}
879
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300880/**
881 * perf_evlist__mmap - Create mmaps to receive events.
882 * @evlist: list of events
883 * @pages: map length in pages
884 * @overwrite: overwrite older events?
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200885 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300886 * If @overwrite is %false the user needs to signal event consumption using
887 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
888 * automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200889 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300890 * Return: %0 on success, negative error code otherwise.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200891 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200892int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
893 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200894{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300895 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200896 const struct cpu_map *cpus = evlist->cpus;
897 const struct thread_map *threads = evlist->threads;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300898 struct mmap_params mp = {
899 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
900 };
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200901
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200902 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200903 return -ENOMEM;
904
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200905 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200906 return -ENOMEM;
907
908 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200909 evlist->mmap_len = perf_evlist__mmap_size(pages);
Adrian Hunter2af68ef2013-10-18 15:29:07 +0300910 pr_debug("mmap size %zuB\n", evlist->mmap_len);
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300911 mp.mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200912
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300913 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200914 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300915 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300916 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200917 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200918 }
919
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700920 if (cpu_map__empty(cpus))
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300921 return perf_evlist__mmap_per_thread(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200922
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300923 return perf_evlist__mmap_per_cpu(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200924}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200925
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300926int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200927{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900928 evlist->threads = thread_map__new_str(target->pid, target->tid,
929 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200930
931 if (evlist->threads == NULL)
932 return -1;
933
Dongsheng Yang9c105fb2013-12-04 17:56:40 -0500934 if (target__uses_dummy_map(target))
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900935 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900936 else
937 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200938
939 if (evlist->cpus == NULL)
940 goto out_delete_threads;
941
942 return 0;
943
944out_delete_threads:
945 thread_map__delete(evlist->threads);
946 return -1;
947}
948
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300949int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100950{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100951 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300952 int err = 0;
953 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900954 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100955
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300956 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300957 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100958 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300959
960 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
961 if (err)
962 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100963 }
964
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300965 return err;
966}
967
968int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
969{
970 struct perf_evsel *evsel;
971 int err = 0;
972 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900973 nthreads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300974
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300975 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300976 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
977 if (err)
978 break;
979 }
980
981 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100982}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200983
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300984bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200985{
Adrian Hunter75562572013-08-27 11:23:09 +0300986 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300987
Adrian Hunter75562572013-08-27 11:23:09 +0300988 if (evlist->nr_entries == 1)
989 return true;
990
991 if (evlist->id_pos < 0 || evlist->is_pos < 0)
992 return false;
993
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300994 evlist__for_each(evlist, pos) {
Adrian Hunter75562572013-08-27 11:23:09 +0300995 if (pos->id_pos != evlist->id_pos ||
996 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300997 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200998 }
999
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001000 return true;
1001}
1002
Adrian Hunter75562572013-08-27 11:23:09 +03001003u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001004{
Adrian Hunter75562572013-08-27 11:23:09 +03001005 struct perf_evsel *evsel;
1006
1007 if (evlist->combined_sample_type)
1008 return evlist->combined_sample_type;
1009
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001010 evlist__for_each(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +03001011 evlist->combined_sample_type |= evsel->attr.sample_type;
1012
1013 return evlist->combined_sample_type;
1014}
1015
1016u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1017{
1018 evlist->combined_sample_type = 0;
1019 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001020}
1021
Jiri Olsa9ede4732012-10-10 17:38:13 +02001022bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1023{
1024 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1025 u64 read_format = first->attr.read_format;
1026 u64 sample_type = first->attr.sample_type;
1027
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001028 evlist__for_each(evlist, pos) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001029 if (read_format != pos->attr.read_format)
1030 return false;
1031 }
1032
1033 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1034 if ((sample_type & PERF_SAMPLE_READ) &&
1035 !(read_format & PERF_FORMAT_ID)) {
1036 return false;
1037 }
1038
1039 return true;
1040}
1041
1042u64 perf_evlist__read_format(struct perf_evlist *evlist)
1043{
1044 struct perf_evsel *first = perf_evlist__first(evlist);
1045 return first->attr.read_format;
1046}
1047
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001048u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001049{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001050 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001051 struct perf_sample *data;
1052 u64 sample_type;
1053 u16 size = 0;
1054
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001055 if (!first->attr.sample_id_all)
1056 goto out;
1057
1058 sample_type = first->attr.sample_type;
1059
1060 if (sample_type & PERF_SAMPLE_TID)
1061 size += sizeof(data->tid) * 2;
1062
1063 if (sample_type & PERF_SAMPLE_TIME)
1064 size += sizeof(data->time);
1065
1066 if (sample_type & PERF_SAMPLE_ID)
1067 size += sizeof(data->id);
1068
1069 if (sample_type & PERF_SAMPLE_STREAM_ID)
1070 size += sizeof(data->stream_id);
1071
1072 if (sample_type & PERF_SAMPLE_CPU)
1073 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +03001074
1075 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1076 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001077out:
1078 return size;
1079}
1080
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001081bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001082{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001083 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001084
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001085 evlist__for_each_continue(evlist, pos) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001086 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1087 return false;
1088 }
1089
1090 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001091}
1092
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001093bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001094{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001095 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001096 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001097}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001098
1099void perf_evlist__set_selected(struct perf_evlist *evlist,
1100 struct perf_evsel *evsel)
1101{
1102 evlist->selected = evsel;
1103}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001104
Namhyung Kima74b4b62013-03-15 14:48:48 +09001105void perf_evlist__close(struct perf_evlist *evlist)
1106{
1107 struct perf_evsel *evsel;
1108 int ncpus = cpu_map__nr(evlist->cpus);
1109 int nthreads = thread_map__nr(evlist->threads);
Stephane Eranian8ad92192014-01-17 16:34:06 +01001110 int n;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001111
Stephane Eranian8ad92192014-01-17 16:34:06 +01001112 evlist__for_each_reverse(evlist, evsel) {
1113 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1114 perf_evsel__close(evsel, n, nthreads);
1115 }
Namhyung Kima74b4b62013-03-15 14:48:48 +09001116}
1117
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001118int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001119{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001120 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001121 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001122
Adrian Hunter733cd2f2013-09-06 22:40:11 +03001123 perf_evlist__update_id_pos(evlist);
1124
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001125 evlist__for_each(evlist, evsel) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001126 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001127 if (err < 0)
1128 goto out_err;
1129 }
1130
1131 return 0;
1132out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001133 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001134 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001135 return err;
1136}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001137
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001138int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001139 const char *argv[], bool pipe_output,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001140 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001141{
1142 int child_ready_pipe[2], go_pipe[2];
1143 char bf;
1144
1145 if (pipe(child_ready_pipe) < 0) {
1146 perror("failed to create 'ready' pipe");
1147 return -1;
1148 }
1149
1150 if (pipe(go_pipe) < 0) {
1151 perror("failed to create 'go' pipe");
1152 goto out_close_ready_pipe;
1153 }
1154
1155 evlist->workload.pid = fork();
1156 if (evlist->workload.pid < 0) {
1157 perror("failed to fork");
1158 goto out_close_pipes;
1159 }
1160
1161 if (!evlist->workload.pid) {
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001162 int ret;
1163
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001164 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001165 dup2(2, 1);
1166
David Ahern0817df02013-05-25 17:50:39 -06001167 signal(SIGTERM, SIG_DFL);
1168
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001169 close(child_ready_pipe[0]);
1170 close(go_pipe[1]);
1171 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1172
1173 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001174 * Tell the parent we're ready to go
1175 */
1176 close(child_ready_pipe[1]);
1177
1178 /*
1179 * Wait until the parent tells us to go.
1180 */
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001181 ret = read(go_pipe[0], &bf, 1);
1182 /*
1183 * The parent will ask for the execvp() to be performed by
1184 * writing exactly one byte, in workload.cork_fd, usually via
1185 * perf_evlist__start_workload().
1186 *
1187 * For cancelling the workload without actuallin running it,
1188 * the parent will just close workload.cork_fd, without writing
1189 * anything, i.e. read will return zero and we just exit()
1190 * here.
1191 */
1192 if (ret != 1) {
1193 if (ret == -1)
1194 perror("unable to read pipe");
1195 exit(ret);
1196 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001197
1198 execvp(argv[0], (char **)argv);
1199
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001200 if (exec_error) {
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001201 union sigval val;
1202
1203 val.sival_int = errno;
1204 if (sigqueue(getppid(), SIGUSR1, val))
1205 perror(argv[0]);
1206 } else
1207 perror(argv[0]);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001208 exit(-1);
1209 }
1210
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001211 if (exec_error) {
1212 struct sigaction act = {
1213 .sa_flags = SA_SIGINFO,
1214 .sa_sigaction = exec_error,
1215 };
1216 sigaction(SIGUSR1, &act, NULL);
1217 }
1218
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001219 if (target__none(target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001220 evlist->threads->map[0] = evlist->workload.pid;
1221
1222 close(child_ready_pipe[1]);
1223 close(go_pipe[0]);
1224 /*
1225 * wait for child to settle
1226 */
1227 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1228 perror("unable to read pipe");
1229 goto out_close_pipes;
1230 }
1231
Namhyung Kimbcf31452013-06-26 16:14:15 +09001232 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001233 evlist->workload.cork_fd = go_pipe[1];
1234 close(child_ready_pipe[0]);
1235 return 0;
1236
1237out_close_pipes:
1238 close(go_pipe[0]);
1239 close(go_pipe[1]);
1240out_close_ready_pipe:
1241 close(child_ready_pipe[0]);
1242 close(child_ready_pipe[1]);
1243 return -1;
1244}
1245
1246int perf_evlist__start_workload(struct perf_evlist *evlist)
1247{
1248 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001249 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001250 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001251 /*
1252 * Remove the cork, let it rip!
1253 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001254 ret = write(evlist->workload.cork_fd, &bf, 1);
1255 if (ret < 0)
1256 perror("enable to write to pipe");
1257
1258 close(evlist->workload.cork_fd);
1259 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001260 }
1261
1262 return 0;
1263}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001264
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001265int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001266 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001267{
Adrian Hunter75562572013-08-27 11:23:09 +03001268 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1269
1270 if (!evsel)
1271 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001272 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001273}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001274
1275size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1276{
1277 struct perf_evsel *evsel;
1278 size_t printed = 0;
1279
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001280 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001281 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1282 perf_evsel__name(evsel));
1283 }
1284
Davidlohr Buesob2222132013-11-12 22:24:24 -08001285 return printed + fprintf(fp, "\n");
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001286}
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03001287
1288int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1289 int err, char *buf, size_t size)
1290{
1291 char sbuf[128];
1292
1293 switch (err) {
1294 case ENOENT:
1295 scnprintf(buf, size, "%s",
1296 "Error:\tUnable to find debugfs\n"
1297 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1298 "Hint:\tIs the debugfs filesystem mounted?\n"
1299 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1300 break;
1301 case EACCES:
1302 scnprintf(buf, size,
1303 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1304 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1305 debugfs_mountpoint, debugfs_mountpoint);
1306 break;
1307 default:
1308 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1309 break;
1310 }
1311
1312 return 0;
1313}
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001314
1315int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1316 int err, char *buf, size_t size)
1317{
1318 int printed, value;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00001319 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001320
1321 switch (err) {
1322 case EACCES:
1323 case EPERM:
1324 printed = scnprintf(buf, size,
1325 "Error:\t%s.\n"
1326 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1327
Adrian Hunter1a472452013-12-11 14:36:23 +02001328 value = perf_event_paranoid();
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001329
1330 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1331
1332 if (value >= 2) {
1333 printed += scnprintf(buf + printed, size - printed,
1334 "For your workloads it needs to be <= 1\nHint:\t");
1335 }
1336 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001337 "For system wide tracing it needs to be set to -1.\n");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001338
1339 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001340 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1341 "Hint:\tThe current value is %d.", value);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001342 break;
1343 default:
1344 scnprintf(buf, size, "%s", emsg);
1345 break;
1346 }
1347
1348 return 0;
1349}
Adrian Huntera025e4f2013-12-11 14:36:35 +02001350
1351void perf_evlist__to_front(struct perf_evlist *evlist,
1352 struct perf_evsel *move_evsel)
1353{
1354 struct perf_evsel *evsel, *n;
1355 LIST_HEAD(move);
1356
1357 if (move_evsel == perf_evlist__first(evlist))
1358 return;
1359
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001360 evlist__for_each_safe(evlist, n, evsel) {
Adrian Huntera025e4f2013-12-11 14:36:35 +02001361 if (evsel->leader == move_evsel->leader)
1362 list_move_tail(&evsel->node, &move);
1363 }
1364
1365 list_splice(&move, &evlist->entries);
1366}
Adrian Hunter60b08962014-07-31 09:00:52 +03001367
1368void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1369 struct perf_evsel *tracking_evsel)
1370{
1371 struct perf_evsel *evsel;
1372
1373 if (tracking_evsel->tracking)
1374 return;
1375
1376 evlist__for_each(evlist, evsel) {
1377 if (evsel != tracking_evsel)
1378 evsel->tracking = false;
1379 }
1380
1381 tracking_evsel->tracking = true;
1382}