blob: af250556b33f9e0d56493aa390aaa826db6eb08a [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 Petkov85c66be2013-02-20 16:32:30 +010010#include <lk/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
84 list_for_each_entry(evsel, &evlist->entries, node)
85 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
94 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
95 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{
104 free(evlist->mmap);
105 free(evlist->pollfd);
106 evlist->mmap = NULL;
107 evlist->pollfd = NULL;
108}
109
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200110void perf_evlist__delete(struct perf_evlist *evlist)
111{
112 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200113 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200114 free(evlist);
115}
116
117void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
118{
119 list_add_tail(&entry->node, &evlist->entries);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300120 entry->idx = evlist->nr_entries;
121
Adrian Hunter75562572013-08-27 11:23:09 +0300122 if (!evlist->nr_entries++)
123 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200124}
125
Jiri Olsa0529bc12012-01-27 15:34:20 +0100126void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
127 struct list_head *list,
128 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200129{
Adrian Hunter75562572013-08-27 11:23:09 +0300130 bool set_id_pos = !evlist->nr_entries;
131
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200132 list_splice_tail(list, &evlist->entries);
133 evlist->nr_entries += nr_entries;
Adrian Hunter75562572013-08-27 11:23:09 +0300134 if (set_id_pos)
135 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200136}
137
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300138void __perf_evlist__set_leader(struct list_head *list)
139{
140 struct perf_evsel *evsel, *leader;
141
142 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900143 evsel = list_entry(list->prev, struct perf_evsel, node);
144
145 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300146
147 list_for_each_entry(evsel, list, node) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100148 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300149 }
150}
151
152void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200153{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900154 if (evlist->nr_entries) {
155 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300156 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900157 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200158}
159
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200160int perf_evlist__add_default(struct perf_evlist *evlist)
161{
162 struct perf_event_attr attr = {
163 .type = PERF_TYPE_HARDWARE,
164 .config = PERF_COUNT_HW_CPU_CYCLES,
165 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100166 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200167
Joerg Roedel1aed2672012-01-04 17:54:20 +0100168 event_attr_init(&attr);
169
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300170 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200171 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200172 goto error;
173
174 /* use strdup() because free(evsel) assumes name is allocated */
175 evsel->name = strdup("cycles");
176 if (!evsel->name)
177 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200178
179 perf_evlist__add(evlist, evsel);
180 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200181error_free:
182 perf_evsel__delete(evsel);
183error:
184 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200185}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200186
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300187static int perf_evlist__add_attrs(struct perf_evlist *evlist,
188 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200189{
190 struct perf_evsel *evsel, *n;
191 LIST_HEAD(head);
192 size_t i;
193
194 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300195 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200196 if (evsel == NULL)
197 goto out_delete_partial_list;
198 list_add_tail(&evsel->node, &head);
199 }
200
201 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
202
203 return 0;
204
205out_delete_partial_list:
206 list_for_each_entry_safe(evsel, n, &head, node)
207 perf_evsel__delete(evsel);
208 return -1;
209}
210
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300211int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
212 struct perf_event_attr *attrs, size_t nr_attrs)
213{
214 size_t i;
215
216 for (i = 0; i < nr_attrs; i++)
217 event_attr_init(attrs + i);
218
219 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
220}
221
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300222struct perf_evsel *
223perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200224{
225 struct perf_evsel *evsel;
226
227 list_for_each_entry(evsel, &evlist->entries, node) {
228 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
229 (int)evsel->attr.config == id)
230 return evsel;
231 }
232
233 return NULL;
234}
235
David Aherna2f28042013-08-28 22:29:51 -0600236struct perf_evsel *
237perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
238 const char *name)
239{
240 struct perf_evsel *evsel;
241
242 list_for_each_entry(evsel, &evlist->entries, node) {
243 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
244 (strcmp(evsel->name, name) == 0))
245 return evsel;
246 }
247
248 return NULL;
249}
250
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300251int perf_evlist__add_newtp(struct perf_evlist *evlist,
252 const char *sys, const char *name, void *handler)
253{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300254 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300255
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300256 if (evsel == NULL)
257 return -1;
258
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300259 evsel->handler = handler;
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300260 perf_evlist__add(evlist, evsel);
261 return 0;
262}
263
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300264void perf_evlist__disable(struct perf_evlist *evlist)
265{
266 int cpu, thread;
267 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900268 int nr_cpus = cpu_map__nr(evlist->cpus);
269 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300270
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900271 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300272 list_for_each_entry(pos, &evlist->entries, node) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300273 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100274 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900275 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900276 ioctl(FD(pos, cpu, thread),
277 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300278 }
279 }
280}
281
David Ahern764e16a32011-08-25 10:17:55 -0600282void perf_evlist__enable(struct perf_evlist *evlist)
283{
284 int cpu, thread;
285 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900286 int nr_cpus = cpu_map__nr(evlist->cpus);
287 int nr_threads = thread_map__nr(evlist->threads);
David Ahern764e16a32011-08-25 10:17:55 -0600288
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900289 for (cpu = 0; cpu < nr_cpus; cpu++) {
David Ahern764e16a32011-08-25 10:17:55 -0600290 list_for_each_entry(pos, &evlist->entries, node) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300291 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100292 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900293 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900294 ioctl(FD(pos, cpu, thread),
295 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600296 }
297 }
298}
299
Adrian Hunter395c3072013-08-31 21:50:53 +0300300int perf_evlist__disable_event(struct perf_evlist *evlist,
301 struct perf_evsel *evsel)
302{
303 int cpu, thread, err;
304
305 if (!evsel->fd)
306 return 0;
307
308 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
309 for (thread = 0; thread < evlist->threads->nr; thread++) {
310 err = ioctl(FD(evsel, cpu, thread),
311 PERF_EVENT_IOC_DISABLE, 0);
312 if (err)
313 return err;
314 }
315 }
316 return 0;
317}
318
319int perf_evlist__enable_event(struct perf_evlist *evlist,
320 struct perf_evsel *evsel)
321{
322 int cpu, thread, err;
323
324 if (!evsel->fd)
325 return -EINVAL;
326
327 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
328 for (thread = 0; thread < evlist->threads->nr; thread++) {
329 err = ioctl(FD(evsel, cpu, thread),
330 PERF_EVENT_IOC_ENABLE, 0);
331 if (err)
332 return err;
333 }
334 }
335 return 0;
336}
337
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200338static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200339{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900340 int nr_cpus = cpu_map__nr(evlist->cpus);
341 int nr_threads = thread_map__nr(evlist->threads);
342 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200343 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
344 return evlist->pollfd != NULL ? 0 : -ENOMEM;
345}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200346
347void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
348{
349 fcntl(fd, F_SETFL, O_NONBLOCK);
350 evlist->pollfd[evlist->nr_fds].fd = fd;
351 evlist->pollfd[evlist->nr_fds].events = POLLIN;
352 evlist->nr_fds++;
353}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200354
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300355static void perf_evlist__id_hash(struct perf_evlist *evlist,
356 struct perf_evsel *evsel,
357 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200358{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300359 int hash;
360 struct perf_sample_id *sid = SID(evsel, cpu, thread);
361
362 sid->id = id;
363 sid->evsel = evsel;
364 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
365 hlist_add_head(&sid->node, &evlist->heads[hash]);
366}
367
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300368void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
369 int cpu, int thread, u64 id)
370{
371 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
372 evsel->id[evsel->ids++] = id;
373}
374
375static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
376 struct perf_evsel *evsel,
377 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300378{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200379 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300380 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200381 u64 id;
382 int ret;
383
384 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
385 if (!ret)
386 goto add;
387
388 if (errno != ENOTTY)
389 return -1;
390
391 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200392
Jiri Olsac4861af2012-10-12 13:02:21 +0200393 /*
394 * This way does not work with group format read, so bail
395 * out in that case.
396 */
397 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
398 return -1;
399
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200400 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
401 read(fd, &read_data, sizeof(read_data)) == -1)
402 return -1;
403
404 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
405 ++id_idx;
406 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
407 ++id_idx;
408
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200409 id = read_data[id_idx];
410
411 add:
412 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200413 return 0;
414}
415
Jiri Olsa932a3592012-10-11 14:10:35 +0200416struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200417{
418 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200419 struct perf_sample_id *sid;
420 int hash;
421
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200422 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
423 head = &evlist->heads[hash];
424
Sasha Levinb67bfe02013-02-27 17:06:00 -0800425 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200426 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200427 return sid;
428
429 return NULL;
430}
431
432struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
433{
434 struct perf_sample_id *sid;
435
436 if (evlist->nr_entries == 1)
437 return perf_evlist__first(evlist);
438
439 sid = perf_evlist__id2sid(evlist, id);
440 if (sid)
441 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900442
443 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300444 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900445
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200446 return NULL;
447}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200448
Adrian Hunter75562572013-08-27 11:23:09 +0300449static int perf_evlist__event2id(struct perf_evlist *evlist,
450 union perf_event *event, u64 *id)
451{
452 const u64 *array = event->sample.array;
453 ssize_t n;
454
455 n = (event->header.size - sizeof(event->header)) >> 3;
456
457 if (event->header.type == PERF_RECORD_SAMPLE) {
458 if (evlist->id_pos >= n)
459 return -1;
460 *id = array[evlist->id_pos];
461 } else {
462 if (evlist->is_pos > n)
463 return -1;
464 n -= evlist->is_pos;
465 *id = array[n];
466 }
467 return 0;
468}
469
470static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
471 union perf_event *event)
472{
Adrian Hunter98be6962013-09-04 23:18:17 +0300473 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300474 struct hlist_head *head;
475 struct perf_sample_id *sid;
476 int hash;
477 u64 id;
478
479 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300480 return first;
481
482 if (!first->attr.sample_id_all &&
483 event->header.type != PERF_RECORD_SAMPLE)
484 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300485
486 if (perf_evlist__event2id(evlist, event, &id))
487 return NULL;
488
489 /* Synthesized events have an id of zero */
490 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300491 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300492
493 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
494 head = &evlist->heads[hash];
495
496 hlist_for_each_entry(sid, head, node) {
497 if (sid->id == id)
498 return sid->evsel;
499 }
500 return NULL;
501}
502
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300503union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200504{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300505 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200506 unsigned int head = perf_mmap__read_head(md);
507 unsigned int old = md->prev;
508 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200509 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200510
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200511 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200512 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200513 * If we're further behind than half the buffer, there's a chance
514 * the writer will bite our tail and mess up the samples under us.
515 *
516 * If we somehow ended up ahead of the head, we got messed up.
517 *
518 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200519 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200520 int diff = head - old;
521 if (diff > md->mask / 2 || diff < 0) {
522 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
523
524 /*
525 * head points to a known good entry, start there.
526 */
527 old = head;
528 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200529 }
530
531 if (old != head) {
532 size_t size;
533
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200534 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200535 size = event->header.size;
536
537 /*
538 * Event straddles the mmap boundary -- header should always
539 * be inside due to u64 alignment of output.
540 */
541 if ((old & md->mask) + size != ((old + size) & md->mask)) {
542 unsigned int offset = old;
543 unsigned int len = min(sizeof(*event), size), cpy;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200544 void *dst = md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200545
546 do {
547 cpy = min(md->mask + 1 - (offset & md->mask), len);
548 memcpy(dst, &data[offset & md->mask], cpy);
549 offset += cpy;
550 dst += cpy;
551 len -= cpy;
552 } while (len);
553
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200554 event = (union perf_event *) md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200555 }
556
557 old += size;
558 }
559
560 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200561
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200562 return event;
563}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200564
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800565void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
566{
567 if (!evlist->overwrite) {
568 struct perf_mmap *md = &evlist->mmap[idx];
569 unsigned int old = md->prev;
570
571 perf_mmap__write_tail(md, old);
572 }
573}
574
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300575static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
576{
577 if (evlist->mmap[idx].base != NULL) {
578 munmap(evlist->mmap[idx].base, evlist->mmap_len);
579 evlist->mmap[idx].base = NULL;
580 }
581}
582
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200583void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200584{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300585 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200586
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300587 for (i = 0; i < evlist->nr_mmaps; i++)
588 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300589
590 free(evlist->mmap);
591 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200592}
593
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200594static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200595{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300596 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700597 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900598 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300599 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200600 return evlist->mmap != NULL ? 0 : -ENOMEM;
601}
602
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300603static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300604 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200605{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300606 evlist->mmap[idx].prev = 0;
607 evlist->mmap[idx].mask = mask;
608 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200609 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500610 if (evlist->mmap[idx].base == MAP_FAILED) {
Adrian Hunter02635962013-11-01 15:51:33 +0200611 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
612 errno);
Nelson Elhage301b1952011-12-19 08:39:30 -0500613 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200614 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500615 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200616
617 perf_evlist__add_pollfd(evlist, fd);
618 return 0;
619}
620
Adrian Hunter04e21312013-10-18 15:29:13 +0300621static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
622 int prot, int mask, int cpu, int thread,
623 int *output)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300624{
625 struct perf_evsel *evsel;
Adrian Hunter04e21312013-10-18 15:29:13 +0300626
627 list_for_each_entry(evsel, &evlist->entries, node) {
628 int fd = FD(evsel, cpu, thread);
629
630 if (*output == -1) {
631 *output = fd;
632 if (__perf_evlist__mmap(evlist, idx, prot, mask,
633 *output) < 0)
634 return -1;
635 } else {
636 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
637 return -1;
638 }
639
640 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
641 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
642 return -1;
643 }
644
645 return 0;
646}
647
648static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
649 int mask)
650{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300651 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900652 int nr_cpus = cpu_map__nr(evlist->cpus);
653 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300654
Adrian Huntere3e1a542013-08-14 15:48:24 +0300655 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900656 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300657 int output = -1;
658
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900659 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter04e21312013-10-18 15:29:13 +0300660 if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
661 cpu, thread, &output))
662 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300663 }
664 }
665
666 return 0;
667
668out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300669 for (cpu = 0; cpu < nr_cpus; cpu++)
670 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300671 return -1;
672}
673
Adrian Hunter04e21312013-10-18 15:29:13 +0300674static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
675 int mask)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300676{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300677 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900678 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300679
Adrian Huntere3e1a542013-08-14 15:48:24 +0300680 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900681 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300682 int output = -1;
683
Adrian Hunter04e21312013-10-18 15:29:13 +0300684 if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
685 thread, &output))
686 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300687 }
688
689 return 0;
690
691out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300692 for (thread = 0; thread < nr_threads; thread++)
693 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300694 return -1;
695}
696
Jiri Olsa994a1f72013-09-01 12:36:12 +0200697static size_t perf_evlist__mmap_size(unsigned long pages)
698{
699 /* 512 kiB: default amount of unprivileged mlocked memory */
700 if (pages == UINT_MAX)
701 pages = (512 * 1024) / page_size;
702 else if (!is_power_of_2(pages))
703 return 0;
704
705 return (pages + 1) * page_size;
706}
707
David Ahern33c2dcf2013-11-12 07:46:55 -0700708static long parse_pages_arg(const char *str, unsigned long min,
709 unsigned long max)
Jiri Olsa994a1f72013-09-01 12:36:12 +0200710{
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300711 unsigned long pages, val;
Jiri Olsa27050f52013-09-01 12:36:13 +0200712 static struct parse_tag tags[] = {
713 { .tag = 'B', .mult = 1 },
714 { .tag = 'K', .mult = 1 << 10 },
715 { .tag = 'M', .mult = 1 << 20 },
716 { .tag = 'G', .mult = 1 << 30 },
717 { .tag = 0 },
718 };
Jiri Olsa994a1f72013-09-01 12:36:12 +0200719
David Ahern89735042013-11-12 07:46:53 -0700720 if (str == NULL)
David Ahern33c2dcf2013-11-12 07:46:55 -0700721 return -EINVAL;
David Ahern89735042013-11-12 07:46:53 -0700722
Jiri Olsa27050f52013-09-01 12:36:13 +0200723 val = parse_tag_value(str, tags);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300724 if (val != (unsigned long) -1) {
Jiri Olsa27050f52013-09-01 12:36:13 +0200725 /* we got file size value */
726 pages = PERF_ALIGN(val, page_size) / page_size;
Jiri Olsa27050f52013-09-01 12:36:13 +0200727 } else {
728 /* we got pages count value */
729 char *eptr;
730 pages = strtoul(str, &eptr, 10);
David Ahern33c2dcf2013-11-12 07:46:55 -0700731 if (*eptr != '\0')
732 return -EINVAL;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200733 }
734
Adrian Hunter2bcab6c2013-12-09 15:18:37 +0200735 if (pages == 0 && min == 0) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700736 /* leave number of pages at 0 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200737 } else if (!is_power_of_2(pages)) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700738 /* round pages up to next power of 2 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200739 pages = next_pow2_l(pages);
740 if (!pages)
741 return -EINVAL;
David Ahern96398372013-11-12 07:46:54 -0700742 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
743 pages * page_size, pages);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300744 }
745
David Ahern33c2dcf2013-11-12 07:46:55 -0700746 if (pages > max)
747 return -EINVAL;
748
749 return pages;
750}
751
752int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
753 int unset __maybe_unused)
754{
755 unsigned int *mmap_pages = opt->value;
756 unsigned long max = UINT_MAX;
757 long pages;
758
Adrian Hunterf5ae9c42013-12-09 15:18:38 +0200759 if (max > SIZE_MAX / page_size)
David Ahern33c2dcf2013-11-12 07:46:55 -0700760 max = SIZE_MAX / page_size;
761
762 pages = parse_pages_arg(str, 1, max);
763 if (pages < 0) {
764 pr_err("Invalid argument for --mmap_pages/-m\n");
Jiri Olsa994a1f72013-09-01 12:36:12 +0200765 return -1;
766 }
767
768 *mmap_pages = pages;
769 return 0;
770}
771
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300772/**
773 * perf_evlist__mmap - Create mmaps to receive events.
774 * @evlist: list of events
775 * @pages: map length in pages
776 * @overwrite: overwrite older events?
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200777 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300778 * If @overwrite is %false the user needs to signal event consumption using
779 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
780 * automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200781 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300782 * Return: %0 on success, negative error code otherwise.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200783 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200784int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
785 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200786{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300787 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200788 const struct cpu_map *cpus = evlist->cpus;
789 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200790 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
791
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200792 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200793 return -ENOMEM;
794
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200795 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200796 return -ENOMEM;
797
798 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200799 evlist->mmap_len = perf_evlist__mmap_size(pages);
Adrian Hunter2af68ef2013-10-18 15:29:07 +0300800 pr_debug("mmap size %zuB\n", evlist->mmap_len);
Jiri Olsa994a1f72013-09-01 12:36:12 +0200801 mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200802
803 list_for_each_entry(evsel, &evlist->entries, node) {
804 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300805 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300806 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200807 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200808 }
809
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700810 if (cpu_map__empty(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300811 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200812
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300813 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200814}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200815
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300816int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200817{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900818 evlist->threads = thread_map__new_str(target->pid, target->tid,
819 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200820
821 if (evlist->threads == NULL)
822 return -1;
823
Dongsheng Yang9c105fb2013-12-04 17:56:40 -0500824 if (target__uses_dummy_map(target))
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900825 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900826 else
827 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200828
829 if (evlist->cpus == NULL)
830 goto out_delete_threads;
831
832 return 0;
833
834out_delete_threads:
835 thread_map__delete(evlist->threads);
836 return -1;
837}
838
839void perf_evlist__delete_maps(struct perf_evlist *evlist)
840{
841 cpu_map__delete(evlist->cpus);
842 thread_map__delete(evlist->threads);
843 evlist->cpus = NULL;
844 evlist->threads = NULL;
845}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100846
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300847int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100848{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100849 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300850 int err = 0;
851 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900852 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100853
854 list_for_each_entry(evsel, &evlist->entries, node) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300855 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100856 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300857
858 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
859 if (err)
860 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100861 }
862
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300863 return err;
864}
865
866int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
867{
868 struct perf_evsel *evsel;
869 int err = 0;
870 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900871 nthreads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300872
873 list_for_each_entry(evsel, &evlist->entries, node) {
874 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
875 if (err)
876 break;
877 }
878
879 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100880}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200881
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300882bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200883{
Adrian Hunter75562572013-08-27 11:23:09 +0300884 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300885
Adrian Hunter75562572013-08-27 11:23:09 +0300886 if (evlist->nr_entries == 1)
887 return true;
888
889 if (evlist->id_pos < 0 || evlist->is_pos < 0)
890 return false;
891
892 list_for_each_entry(pos, &evlist->entries, node) {
893 if (pos->id_pos != evlist->id_pos ||
894 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300895 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200896 }
897
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300898 return true;
899}
900
Adrian Hunter75562572013-08-27 11:23:09 +0300901u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300902{
Adrian Hunter75562572013-08-27 11:23:09 +0300903 struct perf_evsel *evsel;
904
905 if (evlist->combined_sample_type)
906 return evlist->combined_sample_type;
907
908 list_for_each_entry(evsel, &evlist->entries, node)
909 evlist->combined_sample_type |= evsel->attr.sample_type;
910
911 return evlist->combined_sample_type;
912}
913
914u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
915{
916 evlist->combined_sample_type = 0;
917 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300918}
919
Jiri Olsa9ede4732012-10-10 17:38:13 +0200920bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
921{
922 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
923 u64 read_format = first->attr.read_format;
924 u64 sample_type = first->attr.sample_type;
925
926 list_for_each_entry_continue(pos, &evlist->entries, node) {
927 if (read_format != pos->attr.read_format)
928 return false;
929 }
930
931 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
932 if ((sample_type & PERF_SAMPLE_READ) &&
933 !(read_format & PERF_FORMAT_ID)) {
934 return false;
935 }
936
937 return true;
938}
939
940u64 perf_evlist__read_format(struct perf_evlist *evlist)
941{
942 struct perf_evsel *first = perf_evlist__first(evlist);
943 return first->attr.read_format;
944}
945
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300946u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200947{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300948 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200949 struct perf_sample *data;
950 u64 sample_type;
951 u16 size = 0;
952
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200953 if (!first->attr.sample_id_all)
954 goto out;
955
956 sample_type = first->attr.sample_type;
957
958 if (sample_type & PERF_SAMPLE_TID)
959 size += sizeof(data->tid) * 2;
960
961 if (sample_type & PERF_SAMPLE_TIME)
962 size += sizeof(data->time);
963
964 if (sample_type & PERF_SAMPLE_ID)
965 size += sizeof(data->id);
966
967 if (sample_type & PERF_SAMPLE_STREAM_ID)
968 size += sizeof(data->stream_id);
969
970 if (sample_type & PERF_SAMPLE_CPU)
971 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +0300972
973 if (sample_type & PERF_SAMPLE_IDENTIFIER)
974 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200975out:
976 return size;
977}
978
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300979bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300980{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300981 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300982
983 list_for_each_entry_continue(pos, &evlist->entries, node) {
984 if (first->attr.sample_id_all != pos->attr.sample_id_all)
985 return false;
986 }
987
988 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200989}
990
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300991bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200992{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300993 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300994 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200995}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300996
997void perf_evlist__set_selected(struct perf_evlist *evlist,
998 struct perf_evsel *evsel)
999{
1000 evlist->selected = evsel;
1001}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001002
Namhyung Kima74b4b62013-03-15 14:48:48 +09001003void perf_evlist__close(struct perf_evlist *evlist)
1004{
1005 struct perf_evsel *evsel;
1006 int ncpus = cpu_map__nr(evlist->cpus);
1007 int nthreads = thread_map__nr(evlist->threads);
1008
1009 list_for_each_entry_reverse(evsel, &evlist->entries, node)
1010 perf_evsel__close(evsel, ncpus, nthreads);
1011}
1012
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001013int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001014{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001015 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001016 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001017
Adrian Hunter733cd2f2013-09-06 22:40:11 +03001018 perf_evlist__update_id_pos(evlist);
1019
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001020 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001021 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001022 if (err < 0)
1023 goto out_err;
1024 }
1025
1026 return 0;
1027out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001028 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001029 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001030 return err;
1031}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001032
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001033int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001034 const char *argv[], bool pipe_output,
1035 bool want_signal)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001036{
1037 int child_ready_pipe[2], go_pipe[2];
1038 char bf;
1039
1040 if (pipe(child_ready_pipe) < 0) {
1041 perror("failed to create 'ready' pipe");
1042 return -1;
1043 }
1044
1045 if (pipe(go_pipe) < 0) {
1046 perror("failed to create 'go' pipe");
1047 goto out_close_ready_pipe;
1048 }
1049
1050 evlist->workload.pid = fork();
1051 if (evlist->workload.pid < 0) {
1052 perror("failed to fork");
1053 goto out_close_pipes;
1054 }
1055
1056 if (!evlist->workload.pid) {
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001057 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001058 dup2(2, 1);
1059
David Ahern0817df02013-05-25 17:50:39 -06001060 signal(SIGTERM, SIG_DFL);
1061
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001062 close(child_ready_pipe[0]);
1063 close(go_pipe[1]);
1064 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1065
1066 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001067 * Tell the parent we're ready to go
1068 */
1069 close(child_ready_pipe[1]);
1070
1071 /*
1072 * Wait until the parent tells us to go.
1073 */
1074 if (read(go_pipe[0], &bf, 1) == -1)
1075 perror("unable to read pipe");
1076
1077 execvp(argv[0], (char **)argv);
1078
1079 perror(argv[0]);
Namhyung Kim55e162e2013-03-11 16:43:17 +09001080 if (want_signal)
1081 kill(getppid(), SIGUSR1);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001082 exit(-1);
1083 }
1084
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001085 if (target__none(target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001086 evlist->threads->map[0] = evlist->workload.pid;
1087
1088 close(child_ready_pipe[1]);
1089 close(go_pipe[0]);
1090 /*
1091 * wait for child to settle
1092 */
1093 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1094 perror("unable to read pipe");
1095 goto out_close_pipes;
1096 }
1097
Namhyung Kimbcf31452013-06-26 16:14:15 +09001098 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001099 evlist->workload.cork_fd = go_pipe[1];
1100 close(child_ready_pipe[0]);
1101 return 0;
1102
1103out_close_pipes:
1104 close(go_pipe[0]);
1105 close(go_pipe[1]);
1106out_close_ready_pipe:
1107 close(child_ready_pipe[0]);
1108 close(child_ready_pipe[1]);
1109 return -1;
1110}
1111
1112int perf_evlist__start_workload(struct perf_evlist *evlist)
1113{
1114 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001115 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001116 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001117 /*
1118 * Remove the cork, let it rip!
1119 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001120 ret = write(evlist->workload.cork_fd, &bf, 1);
1121 if (ret < 0)
1122 perror("enable to write to pipe");
1123
1124 close(evlist->workload.cork_fd);
1125 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001126 }
1127
1128 return 0;
1129}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001130
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001131int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001132 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001133{
Adrian Hunter75562572013-08-27 11:23:09 +03001134 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1135
1136 if (!evsel)
1137 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001138 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001139}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001140
1141size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1142{
1143 struct perf_evsel *evsel;
1144 size_t printed = 0;
1145
1146 list_for_each_entry(evsel, &evlist->entries, node) {
1147 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1148 perf_evsel__name(evsel));
1149 }
1150
Davidlohr Buesob2222132013-11-12 22:24:24 -08001151 return printed + fprintf(fp, "\n");
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001152}
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03001153
1154int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1155 int err, char *buf, size_t size)
1156{
1157 char sbuf[128];
1158
1159 switch (err) {
1160 case ENOENT:
1161 scnprintf(buf, size, "%s",
1162 "Error:\tUnable to find debugfs\n"
1163 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1164 "Hint:\tIs the debugfs filesystem mounted?\n"
1165 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1166 break;
1167 case EACCES:
1168 scnprintf(buf, size,
1169 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1170 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1171 debugfs_mountpoint, debugfs_mountpoint);
1172 break;
1173 default:
1174 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1175 break;
1176 }
1177
1178 return 0;
1179}
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001180
1181int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1182 int err, char *buf, size_t size)
1183{
1184 int printed, value;
1185 char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1186
1187 switch (err) {
1188 case EACCES:
1189 case EPERM:
1190 printed = scnprintf(buf, size,
1191 "Error:\t%s.\n"
1192 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1193
1194 if (filename__read_int("/proc/sys/kernel/perf_event_paranoid", &value))
1195 break;
1196
1197 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1198
1199 if (value >= 2) {
1200 printed += scnprintf(buf + printed, size - printed,
1201 "For your workloads it needs to be <= 1\nHint:\t");
1202 }
1203 printed += scnprintf(buf + printed, size - printed,
1204 "For system wide tracing it needs to be set to -1");
1205
1206 printed += scnprintf(buf + printed, size - printed,
1207 ".\nHint:\tThe current value is %d.", value);
1208 break;
1209 default:
1210 scnprintf(buf, size, "%s", emsg);
1211 break;
1212 }
1213
1214 return 0;
1215}