blob: 40bd2c04df8ad7cb02e713e4f02a832850b0a43f [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;
125
Adrian Hunter75562572013-08-27 11:23:09 +0300126 if (!evlist->nr_entries++)
127 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200128}
129
Jiri Olsa0529bc12012-01-27 15:34:20 +0100130void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
131 struct list_head *list,
132 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200133{
Adrian Hunter75562572013-08-27 11:23:09 +0300134 bool set_id_pos = !evlist->nr_entries;
135
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200136 list_splice_tail(list, &evlist->entries);
137 evlist->nr_entries += nr_entries;
Adrian Hunter75562572013-08-27 11:23:09 +0300138 if (set_id_pos)
139 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200140}
141
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300142void __perf_evlist__set_leader(struct list_head *list)
143{
144 struct perf_evsel *evsel, *leader;
145
146 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900147 evsel = list_entry(list->prev, struct perf_evsel, node);
148
149 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300150
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300151 __evlist__for_each(list, evsel) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100152 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300153 }
154}
155
156void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200157{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900158 if (evlist->nr_entries) {
159 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300160 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900161 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200162}
163
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200164int perf_evlist__add_default(struct perf_evlist *evlist)
165{
166 struct perf_event_attr attr = {
167 .type = PERF_TYPE_HARDWARE,
168 .config = PERF_COUNT_HW_CPU_CYCLES,
169 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100170 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200171
Joerg Roedel1aed2672012-01-04 17:54:20 +0100172 event_attr_init(&attr);
173
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300174 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200175 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200176 goto error;
177
178 /* use strdup() because free(evsel) assumes name is allocated */
179 evsel->name = strdup("cycles");
180 if (!evsel->name)
181 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200182
183 perf_evlist__add(evlist, evsel);
184 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200185error_free:
186 perf_evsel__delete(evsel);
187error:
188 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200189}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200190
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300191static int perf_evlist__add_attrs(struct perf_evlist *evlist,
192 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200193{
194 struct perf_evsel *evsel, *n;
195 LIST_HEAD(head);
196 size_t i;
197
198 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300199 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200200 if (evsel == NULL)
201 goto out_delete_partial_list;
202 list_add_tail(&evsel->node, &head);
203 }
204
205 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
206
207 return 0;
208
209out_delete_partial_list:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300210 __evlist__for_each_safe(&head, n, evsel)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200211 perf_evsel__delete(evsel);
212 return -1;
213}
214
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300215int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
216 struct perf_event_attr *attrs, size_t nr_attrs)
217{
218 size_t i;
219
220 for (i = 0; i < nr_attrs; i++)
221 event_attr_init(attrs + i);
222
223 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
224}
225
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300226struct perf_evsel *
227perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200228{
229 struct perf_evsel *evsel;
230
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300231 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200232 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
233 (int)evsel->attr.config == id)
234 return evsel;
235 }
236
237 return NULL;
238}
239
David Aherna2f28042013-08-28 22:29:51 -0600240struct perf_evsel *
241perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
242 const char *name)
243{
244 struct perf_evsel *evsel;
245
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300246 evlist__for_each(evlist, evsel) {
David Aherna2f28042013-08-28 22:29:51 -0600247 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
248 (strcmp(evsel->name, name) == 0))
249 return evsel;
250 }
251
252 return NULL;
253}
254
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300255int perf_evlist__add_newtp(struct perf_evlist *evlist,
256 const char *sys, const char *name, void *handler)
257{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300258 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300259
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300260 if (evsel == NULL)
261 return -1;
262
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300263 evsel->handler = handler;
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300264 perf_evlist__add(evlist, evsel);
265 return 0;
266}
267
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300268void perf_evlist__disable(struct perf_evlist *evlist)
269{
270 int cpu, thread;
271 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900272 int nr_cpus = cpu_map__nr(evlist->cpus);
273 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300274
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900275 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300276 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300277 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100278 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900279 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900280 ioctl(FD(pos, cpu, thread),
281 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300282 }
283 }
284}
285
David Ahern764e16a32011-08-25 10:17:55 -0600286void perf_evlist__enable(struct perf_evlist *evlist)
287{
288 int cpu, thread;
289 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900290 int nr_cpus = cpu_map__nr(evlist->cpus);
291 int nr_threads = thread_map__nr(evlist->threads);
David Ahern764e16a32011-08-25 10:17:55 -0600292
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900293 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300294 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300295 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100296 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900297 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900298 ioctl(FD(pos, cpu, thread),
299 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600300 }
301 }
302}
303
Adrian Hunter395c3072013-08-31 21:50:53 +0300304int perf_evlist__disable_event(struct perf_evlist *evlist,
305 struct perf_evsel *evsel)
306{
307 int cpu, thread, err;
308
309 if (!evsel->fd)
310 return 0;
311
312 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
313 for (thread = 0; thread < evlist->threads->nr; thread++) {
314 err = ioctl(FD(evsel, cpu, thread),
315 PERF_EVENT_IOC_DISABLE, 0);
316 if (err)
317 return err;
318 }
319 }
320 return 0;
321}
322
323int perf_evlist__enable_event(struct perf_evlist *evlist,
324 struct perf_evsel *evsel)
325{
326 int cpu, thread, err;
327
328 if (!evsel->fd)
329 return -EINVAL;
330
331 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
332 for (thread = 0; thread < evlist->threads->nr; thread++) {
333 err = ioctl(FD(evsel, cpu, thread),
334 PERF_EVENT_IOC_ENABLE, 0);
335 if (err)
336 return err;
337 }
338 }
339 return 0;
340}
341
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200342static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200343{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900344 int nr_cpus = cpu_map__nr(evlist->cpus);
345 int nr_threads = thread_map__nr(evlist->threads);
346 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200347 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
348 return evlist->pollfd != NULL ? 0 : -ENOMEM;
349}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200350
351void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
352{
353 fcntl(fd, F_SETFL, O_NONBLOCK);
354 evlist->pollfd[evlist->nr_fds].fd = fd;
355 evlist->pollfd[evlist->nr_fds].events = POLLIN;
356 evlist->nr_fds++;
357}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200358
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300359static void perf_evlist__id_hash(struct perf_evlist *evlist,
360 struct perf_evsel *evsel,
361 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200362{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300363 int hash;
364 struct perf_sample_id *sid = SID(evsel, cpu, thread);
365
366 sid->id = id;
367 sid->evsel = evsel;
368 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
369 hlist_add_head(&sid->node, &evlist->heads[hash]);
370}
371
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300372void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
373 int cpu, int thread, u64 id)
374{
375 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
376 evsel->id[evsel->ids++] = id;
377}
378
379static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
380 struct perf_evsel *evsel,
381 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300382{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200383 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300384 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200385 u64 id;
386 int ret;
387
388 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
389 if (!ret)
390 goto add;
391
392 if (errno != ENOTTY)
393 return -1;
394
395 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200396
Jiri Olsac4861af2012-10-12 13:02:21 +0200397 /*
398 * This way does not work with group format read, so bail
399 * out in that case.
400 */
401 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
402 return -1;
403
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200404 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
405 read(fd, &read_data, sizeof(read_data)) == -1)
406 return -1;
407
408 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
409 ++id_idx;
410 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
411 ++id_idx;
412
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200413 id = read_data[id_idx];
414
415 add:
416 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200417 return 0;
418}
419
Jiri Olsa932a3592012-10-11 14:10:35 +0200420struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200421{
422 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200423 struct perf_sample_id *sid;
424 int hash;
425
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200426 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
427 head = &evlist->heads[hash];
428
Sasha Levinb67bfe02013-02-27 17:06:00 -0800429 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200430 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200431 return sid;
432
433 return NULL;
434}
435
436struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
437{
438 struct perf_sample_id *sid;
439
440 if (evlist->nr_entries == 1)
441 return perf_evlist__first(evlist);
442
443 sid = perf_evlist__id2sid(evlist, id);
444 if (sid)
445 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900446
447 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300448 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900449
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200450 return NULL;
451}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200452
Adrian Hunter75562572013-08-27 11:23:09 +0300453static int perf_evlist__event2id(struct perf_evlist *evlist,
454 union perf_event *event, u64 *id)
455{
456 const u64 *array = event->sample.array;
457 ssize_t n;
458
459 n = (event->header.size - sizeof(event->header)) >> 3;
460
461 if (event->header.type == PERF_RECORD_SAMPLE) {
462 if (evlist->id_pos >= n)
463 return -1;
464 *id = array[evlist->id_pos];
465 } else {
466 if (evlist->is_pos > n)
467 return -1;
468 n -= evlist->is_pos;
469 *id = array[n];
470 }
471 return 0;
472}
473
474static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
475 union perf_event *event)
476{
Adrian Hunter98be6962013-09-04 23:18:17 +0300477 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300478 struct hlist_head *head;
479 struct perf_sample_id *sid;
480 int hash;
481 u64 id;
482
483 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300484 return first;
485
486 if (!first->attr.sample_id_all &&
487 event->header.type != PERF_RECORD_SAMPLE)
488 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300489
490 if (perf_evlist__event2id(evlist, event, &id))
491 return NULL;
492
493 /* Synthesized events have an id of zero */
494 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300495 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300496
497 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
498 head = &evlist->heads[hash];
499
500 hlist_for_each_entry(sid, head, node) {
501 if (sid->id == id)
502 return sid->evsel;
503 }
504 return NULL;
505}
506
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300507union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200508{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300509 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200510 unsigned int head = perf_mmap__read_head(md);
511 unsigned int old = md->prev;
512 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200513 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200514
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200515 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200516 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200517 * If we're further behind than half the buffer, there's a chance
518 * the writer will bite our tail and mess up the samples under us.
519 *
520 * If we somehow ended up ahead of the head, we got messed up.
521 *
522 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200523 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200524 int diff = head - old;
525 if (diff > md->mask / 2 || diff < 0) {
526 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
527
528 /*
529 * head points to a known good entry, start there.
530 */
531 old = head;
532 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200533 }
534
535 if (old != head) {
536 size_t size;
537
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200538 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200539 size = event->header.size;
540
541 /*
542 * Event straddles the mmap boundary -- header should always
543 * be inside due to u64 alignment of output.
544 */
545 if ((old & md->mask) + size != ((old + size) & md->mask)) {
546 unsigned int offset = old;
547 unsigned int len = min(sizeof(*event), size), cpy;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200548 void *dst = md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200549
550 do {
551 cpy = min(md->mask + 1 - (offset & md->mask), len);
552 memcpy(dst, &data[offset & md->mask], cpy);
553 offset += cpy;
554 dst += cpy;
555 len -= cpy;
556 } while (len);
557
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200558 event = (union perf_event *) md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200559 }
560
561 old += size;
562 }
563
564 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200565
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200566 return event;
567}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200568
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800569void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
570{
571 if (!evlist->overwrite) {
572 struct perf_mmap *md = &evlist->mmap[idx];
573 unsigned int old = md->prev;
574
575 perf_mmap__write_tail(md, old);
576 }
577}
578
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300579static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
580{
581 if (evlist->mmap[idx].base != NULL) {
582 munmap(evlist->mmap[idx].base, evlist->mmap_len);
583 evlist->mmap[idx].base = NULL;
584 }
585}
586
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200587void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200588{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300589 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200590
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300591 if (evlist->mmap == NULL)
592 return;
593
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300594 for (i = 0; i < evlist->nr_mmaps; i++)
595 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300596
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300597 zfree(&evlist->mmap);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200598}
599
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200600static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200601{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300602 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700603 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900604 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300605 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200606 return evlist->mmap != NULL ? 0 : -ENOMEM;
607}
608
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300609static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300610 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200611{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300612 evlist->mmap[idx].prev = 0;
613 evlist->mmap[idx].mask = mask;
614 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200615 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500616 if (evlist->mmap[idx].base == MAP_FAILED) {
Adrian Hunter02635962013-11-01 15:51:33 +0200617 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
618 errno);
Nelson Elhage301b1952011-12-19 08:39:30 -0500619 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200620 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500621 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200622
623 perf_evlist__add_pollfd(evlist, fd);
624 return 0;
625}
626
Adrian Hunter04e21312013-10-18 15:29:13 +0300627static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
628 int prot, int mask, int cpu, int thread,
629 int *output)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300630{
631 struct perf_evsel *evsel;
Adrian Hunter04e21312013-10-18 15:29:13 +0300632
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300633 evlist__for_each(evlist, evsel) {
Adrian Hunter04e21312013-10-18 15:29:13 +0300634 int fd = FD(evsel, cpu, thread);
635
636 if (*output == -1) {
637 *output = fd;
638 if (__perf_evlist__mmap(evlist, idx, prot, mask,
639 *output) < 0)
640 return -1;
641 } else {
642 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
643 return -1;
644 }
645
646 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
647 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
648 return -1;
649 }
650
651 return 0;
652}
653
654static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
655 int mask)
656{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300657 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900658 int nr_cpus = cpu_map__nr(evlist->cpus);
659 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300660
Adrian Huntere3e1a542013-08-14 15:48:24 +0300661 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900662 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300663 int output = -1;
664
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900665 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter04e21312013-10-18 15:29:13 +0300666 if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
667 cpu, thread, &output))
668 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300669 }
670 }
671
672 return 0;
673
674out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300675 for (cpu = 0; cpu < nr_cpus; cpu++)
676 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300677 return -1;
678}
679
Adrian Hunter04e21312013-10-18 15:29:13 +0300680static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
681 int mask)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300682{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300683 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900684 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300685
Adrian Huntere3e1a542013-08-14 15:48:24 +0300686 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900687 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300688 int output = -1;
689
Adrian Hunter04e21312013-10-18 15:29:13 +0300690 if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
691 thread, &output))
692 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300693 }
694
695 return 0;
696
697out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300698 for (thread = 0; thread < nr_threads; thread++)
699 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300700 return -1;
701}
702
Jiri Olsa994a1f72013-09-01 12:36:12 +0200703static size_t perf_evlist__mmap_size(unsigned long pages)
704{
705 /* 512 kiB: default amount of unprivileged mlocked memory */
706 if (pages == UINT_MAX)
707 pages = (512 * 1024) / page_size;
708 else if (!is_power_of_2(pages))
709 return 0;
710
711 return (pages + 1) * page_size;
712}
713
David Ahern33c2dcf2013-11-12 07:46:55 -0700714static long parse_pages_arg(const char *str, unsigned long min,
715 unsigned long max)
Jiri Olsa994a1f72013-09-01 12:36:12 +0200716{
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300717 unsigned long pages, val;
Jiri Olsa27050f52013-09-01 12:36:13 +0200718 static struct parse_tag tags[] = {
719 { .tag = 'B', .mult = 1 },
720 { .tag = 'K', .mult = 1 << 10 },
721 { .tag = 'M', .mult = 1 << 20 },
722 { .tag = 'G', .mult = 1 << 30 },
723 { .tag = 0 },
724 };
Jiri Olsa994a1f72013-09-01 12:36:12 +0200725
David Ahern89735042013-11-12 07:46:53 -0700726 if (str == NULL)
David Ahern33c2dcf2013-11-12 07:46:55 -0700727 return -EINVAL;
David Ahern89735042013-11-12 07:46:53 -0700728
Jiri Olsa27050f52013-09-01 12:36:13 +0200729 val = parse_tag_value(str, tags);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300730 if (val != (unsigned long) -1) {
Jiri Olsa27050f52013-09-01 12:36:13 +0200731 /* we got file size value */
732 pages = PERF_ALIGN(val, page_size) / page_size;
Jiri Olsa27050f52013-09-01 12:36:13 +0200733 } else {
734 /* we got pages count value */
735 char *eptr;
736 pages = strtoul(str, &eptr, 10);
David Ahern33c2dcf2013-11-12 07:46:55 -0700737 if (*eptr != '\0')
738 return -EINVAL;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200739 }
740
Adrian Hunter2bcab6c2013-12-09 15:18:37 +0200741 if (pages == 0 && min == 0) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700742 /* leave number of pages at 0 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200743 } else if (!is_power_of_2(pages)) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700744 /* round pages up to next power of 2 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200745 pages = next_pow2_l(pages);
746 if (!pages)
747 return -EINVAL;
David Ahern96398372013-11-12 07:46:54 -0700748 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
749 pages * page_size, pages);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300750 }
751
David Ahern33c2dcf2013-11-12 07:46:55 -0700752 if (pages > max)
753 return -EINVAL;
754
755 return pages;
756}
757
758int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
759 int unset __maybe_unused)
760{
761 unsigned int *mmap_pages = opt->value;
762 unsigned long max = UINT_MAX;
763 long pages;
764
Adrian Hunterf5ae9c42013-12-09 15:18:38 +0200765 if (max > SIZE_MAX / page_size)
David Ahern33c2dcf2013-11-12 07:46:55 -0700766 max = SIZE_MAX / page_size;
767
768 pages = parse_pages_arg(str, 1, max);
769 if (pages < 0) {
770 pr_err("Invalid argument for --mmap_pages/-m\n");
Jiri Olsa994a1f72013-09-01 12:36:12 +0200771 return -1;
772 }
773
774 *mmap_pages = pages;
775 return 0;
776}
777
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300778/**
779 * perf_evlist__mmap - Create mmaps to receive events.
780 * @evlist: list of events
781 * @pages: map length in pages
782 * @overwrite: overwrite older events?
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200783 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300784 * If @overwrite is %false the user needs to signal event consumption using
785 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
786 * automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200787 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300788 * Return: %0 on success, negative error code otherwise.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200789 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200790int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
791 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200792{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300793 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200794 const struct cpu_map *cpus = evlist->cpus;
795 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200796 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
797
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200798 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200799 return -ENOMEM;
800
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200801 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200802 return -ENOMEM;
803
804 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200805 evlist->mmap_len = perf_evlist__mmap_size(pages);
Adrian Hunter2af68ef2013-10-18 15:29:07 +0300806 pr_debug("mmap size %zuB\n", evlist->mmap_len);
Jiri Olsa994a1f72013-09-01 12:36:12 +0200807 mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200808
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300809 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200810 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300811 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300812 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200813 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200814 }
815
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700816 if (cpu_map__empty(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300817 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200818
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300819 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200820}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200821
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300822int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200823{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900824 evlist->threads = thread_map__new_str(target->pid, target->tid,
825 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200826
827 if (evlist->threads == NULL)
828 return -1;
829
Dongsheng Yang9c105fb2013-12-04 17:56:40 -0500830 if (target__uses_dummy_map(target))
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900831 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900832 else
833 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200834
835 if (evlist->cpus == NULL)
836 goto out_delete_threads;
837
838 return 0;
839
840out_delete_threads:
841 thread_map__delete(evlist->threads);
842 return -1;
843}
844
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300845int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100846{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100847 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300848 int err = 0;
849 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900850 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100851
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300852 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300853 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100854 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300855
856 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
857 if (err)
858 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100859 }
860
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300861 return err;
862}
863
864int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
865{
866 struct perf_evsel *evsel;
867 int err = 0;
868 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900869 nthreads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300870
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300871 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300872 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
873 if (err)
874 break;
875 }
876
877 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100878}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200879
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300880bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200881{
Adrian Hunter75562572013-08-27 11:23:09 +0300882 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300883
Adrian Hunter75562572013-08-27 11:23:09 +0300884 if (evlist->nr_entries == 1)
885 return true;
886
887 if (evlist->id_pos < 0 || evlist->is_pos < 0)
888 return false;
889
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300890 evlist__for_each(evlist, pos) {
Adrian Hunter75562572013-08-27 11:23:09 +0300891 if (pos->id_pos != evlist->id_pos ||
892 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300893 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200894 }
895
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300896 return true;
897}
898
Adrian Hunter75562572013-08-27 11:23:09 +0300899u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300900{
Adrian Hunter75562572013-08-27 11:23:09 +0300901 struct perf_evsel *evsel;
902
903 if (evlist->combined_sample_type)
904 return evlist->combined_sample_type;
905
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300906 evlist__for_each(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +0300907 evlist->combined_sample_type |= evsel->attr.sample_type;
908
909 return evlist->combined_sample_type;
910}
911
912u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
913{
914 evlist->combined_sample_type = 0;
915 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300916}
917
Jiri Olsa9ede4732012-10-10 17:38:13 +0200918bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
919{
920 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
921 u64 read_format = first->attr.read_format;
922 u64 sample_type = first->attr.sample_type;
923
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300924 evlist__for_each(evlist, pos) {
Jiri Olsa9ede4732012-10-10 17:38:13 +0200925 if (read_format != pos->attr.read_format)
926 return false;
927 }
928
929 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
930 if ((sample_type & PERF_SAMPLE_READ) &&
931 !(read_format & PERF_FORMAT_ID)) {
932 return false;
933 }
934
935 return true;
936}
937
938u64 perf_evlist__read_format(struct perf_evlist *evlist)
939{
940 struct perf_evsel *first = perf_evlist__first(evlist);
941 return first->attr.read_format;
942}
943
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300944u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200945{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300946 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200947 struct perf_sample *data;
948 u64 sample_type;
949 u16 size = 0;
950
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200951 if (!first->attr.sample_id_all)
952 goto out;
953
954 sample_type = first->attr.sample_type;
955
956 if (sample_type & PERF_SAMPLE_TID)
957 size += sizeof(data->tid) * 2;
958
959 if (sample_type & PERF_SAMPLE_TIME)
960 size += sizeof(data->time);
961
962 if (sample_type & PERF_SAMPLE_ID)
963 size += sizeof(data->id);
964
965 if (sample_type & PERF_SAMPLE_STREAM_ID)
966 size += sizeof(data->stream_id);
967
968 if (sample_type & PERF_SAMPLE_CPU)
969 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +0300970
971 if (sample_type & PERF_SAMPLE_IDENTIFIER)
972 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200973out:
974 return size;
975}
976
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300977bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300978{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300979 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300980
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300981 evlist__for_each_continue(evlist, pos) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300982 if (first->attr.sample_id_all != pos->attr.sample_id_all)
983 return false;
984 }
985
986 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200987}
988
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300989bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200990{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300991 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300992 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200993}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300994
995void perf_evlist__set_selected(struct perf_evlist *evlist,
996 struct perf_evsel *evsel)
997{
998 evlist->selected = evsel;
999}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001000
Namhyung Kima74b4b62013-03-15 14:48:48 +09001001void perf_evlist__close(struct perf_evlist *evlist)
1002{
1003 struct perf_evsel *evsel;
1004 int ncpus = cpu_map__nr(evlist->cpus);
1005 int nthreads = thread_map__nr(evlist->threads);
1006
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001007 evlist__for_each_reverse(evlist, evsel)
Namhyung Kima74b4b62013-03-15 14:48:48 +09001008 perf_evsel__close(evsel, ncpus, nthreads);
1009}
1010
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001011int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001012{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001013 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001014 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001015
Adrian Hunter733cd2f2013-09-06 22:40:11 +03001016 perf_evlist__update_id_pos(evlist);
1017
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001018 evlist__for_each(evlist, evsel) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001019 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001020 if (err < 0)
1021 goto out_err;
1022 }
1023
1024 return 0;
1025out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001026 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001027 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001028 return err;
1029}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001030
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001031int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001032 const char *argv[], bool pipe_output,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001033 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001034{
1035 int child_ready_pipe[2], go_pipe[2];
1036 char bf;
1037
1038 if (pipe(child_ready_pipe) < 0) {
1039 perror("failed to create 'ready' pipe");
1040 return -1;
1041 }
1042
1043 if (pipe(go_pipe) < 0) {
1044 perror("failed to create 'go' pipe");
1045 goto out_close_ready_pipe;
1046 }
1047
1048 evlist->workload.pid = fork();
1049 if (evlist->workload.pid < 0) {
1050 perror("failed to fork");
1051 goto out_close_pipes;
1052 }
1053
1054 if (!evlist->workload.pid) {
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001055 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001056 dup2(2, 1);
1057
David Ahern0817df02013-05-25 17:50:39 -06001058 signal(SIGTERM, SIG_DFL);
1059
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001060 close(child_ready_pipe[0]);
1061 close(go_pipe[1]);
1062 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1063
1064 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001065 * Tell the parent we're ready to go
1066 */
1067 close(child_ready_pipe[1]);
1068
1069 /*
1070 * Wait until the parent tells us to go.
1071 */
1072 if (read(go_pipe[0], &bf, 1) == -1)
1073 perror("unable to read pipe");
1074
1075 execvp(argv[0], (char **)argv);
1076
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001077 if (exec_error) {
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001078 union sigval val;
1079
1080 val.sival_int = errno;
1081 if (sigqueue(getppid(), SIGUSR1, val))
1082 perror(argv[0]);
1083 } else
1084 perror(argv[0]);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001085 exit(-1);
1086 }
1087
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001088 if (exec_error) {
1089 struct sigaction act = {
1090 .sa_flags = SA_SIGINFO,
1091 .sa_sigaction = exec_error,
1092 };
1093 sigaction(SIGUSR1, &act, NULL);
1094 }
1095
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001096 if (target__none(target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001097 evlist->threads->map[0] = evlist->workload.pid;
1098
1099 close(child_ready_pipe[1]);
1100 close(go_pipe[0]);
1101 /*
1102 * wait for child to settle
1103 */
1104 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1105 perror("unable to read pipe");
1106 goto out_close_pipes;
1107 }
1108
Namhyung Kimbcf31452013-06-26 16:14:15 +09001109 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001110 evlist->workload.cork_fd = go_pipe[1];
1111 close(child_ready_pipe[0]);
1112 return 0;
1113
1114out_close_pipes:
1115 close(go_pipe[0]);
1116 close(go_pipe[1]);
1117out_close_ready_pipe:
1118 close(child_ready_pipe[0]);
1119 close(child_ready_pipe[1]);
1120 return -1;
1121}
1122
1123int perf_evlist__start_workload(struct perf_evlist *evlist)
1124{
1125 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001126 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001127 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001128 /*
1129 * Remove the cork, let it rip!
1130 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001131 ret = write(evlist->workload.cork_fd, &bf, 1);
1132 if (ret < 0)
1133 perror("enable to write to pipe");
1134
1135 close(evlist->workload.cork_fd);
1136 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001137 }
1138
1139 return 0;
1140}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001141
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001142int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001143 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001144{
Adrian Hunter75562572013-08-27 11:23:09 +03001145 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1146
1147 if (!evsel)
1148 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001149 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001150}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001151
1152size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1153{
1154 struct perf_evsel *evsel;
1155 size_t printed = 0;
1156
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001157 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001158 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1159 perf_evsel__name(evsel));
1160 }
1161
Davidlohr Buesob2222132013-11-12 22:24:24 -08001162 return printed + fprintf(fp, "\n");
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001163}
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03001164
1165int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1166 int err, char *buf, size_t size)
1167{
1168 char sbuf[128];
1169
1170 switch (err) {
1171 case ENOENT:
1172 scnprintf(buf, size, "%s",
1173 "Error:\tUnable to find debugfs\n"
1174 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1175 "Hint:\tIs the debugfs filesystem mounted?\n"
1176 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1177 break;
1178 case EACCES:
1179 scnprintf(buf, size,
1180 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1181 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1182 debugfs_mountpoint, debugfs_mountpoint);
1183 break;
1184 default:
1185 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1186 break;
1187 }
1188
1189 return 0;
1190}
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001191
1192int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1193 int err, char *buf, size_t size)
1194{
1195 int printed, value;
1196 char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1197
1198 switch (err) {
1199 case EACCES:
1200 case EPERM:
1201 printed = scnprintf(buf, size,
1202 "Error:\t%s.\n"
1203 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1204
Adrian Hunter1a472452013-12-11 14:36:23 +02001205 value = perf_event_paranoid();
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001206
1207 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1208
1209 if (value >= 2) {
1210 printed += scnprintf(buf + printed, size - printed,
1211 "For your workloads it needs to be <= 1\nHint:\t");
1212 }
1213 printed += scnprintf(buf + printed, size - printed,
1214 "For system wide tracing it needs to be set to -1");
1215
1216 printed += scnprintf(buf + printed, size - printed,
1217 ".\nHint:\tThe current value is %d.", value);
1218 break;
1219 default:
1220 scnprintf(buf, size, "%s", emsg);
1221 break;
1222 }
1223
1224 return 0;
1225}
Adrian Huntera025e4f2013-12-11 14:36:35 +02001226
1227void perf_evlist__to_front(struct perf_evlist *evlist,
1228 struct perf_evsel *move_evsel)
1229{
1230 struct perf_evsel *evsel, *n;
1231 LIST_HEAD(move);
1232
1233 if (move_evsel == perf_evlist__first(evlist))
1234 return;
1235
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001236 evlist__for_each_safe(evlist, n, evsel) {
Adrian Huntera025e4f2013-12-11 14:36:35 +02001237 if (evsel->leader == move_evsel->leader)
1238 list_move_tail(&evsel->node, &move);
1239 }
1240
1241 list_splice(&move, &evlist->entries);
1242}