blob: 3b9f411a6b461272903e9b8559ada718d2e57867 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
Arnaldo Carvalho de Meloa8c9ae12011-11-05 08:41:51 -02009#include "util.h"
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -030010#include <api/fs/fs.h>
Arnaldo Carvalho de 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>
Arnaldo Carvalho de Melo0389cd12014-12-15 16:04:11 -030027#include <linux/log2.h>
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020028
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -030029static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
30static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
31
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020032#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030033#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020034
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020035void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
36 struct thread_map *threads)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020037{
38 int i;
39
40 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
41 INIT_HLIST_HEAD(&evlist->heads[i]);
42 INIT_LIST_HEAD(&evlist->entries);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020043 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -030044 fdarray__init(&evlist->pollfd, 64);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020045 evlist->workload.pid = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020046}
47
Namhyung Kim334fe7a2013-03-11 16:43:12 +090048struct perf_evlist *perf_evlist__new(void)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020049{
50 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
51
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020052 if (evlist != NULL)
Namhyung Kim334fe7a2013-03-11 16:43:12 +090053 perf_evlist__init(evlist, NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020054
55 return evlist;
56}
57
Jiri Olsab22d54b2013-09-01 12:36:14 +020058struct perf_evlist *perf_evlist__new_default(void)
59{
60 struct perf_evlist *evlist = perf_evlist__new();
61
62 if (evlist && perf_evlist__add_default(evlist)) {
63 perf_evlist__delete(evlist);
64 evlist = NULL;
65 }
66
67 return evlist;
68}
69
Adrian Hunter75562572013-08-27 11:23:09 +030070/**
71 * perf_evlist__set_id_pos - set the positions of event ids.
72 * @evlist: selected event list
73 *
74 * Events with compatible sample types all have the same id_pos
75 * and is_pos. For convenience, put a copy on evlist.
76 */
77void perf_evlist__set_id_pos(struct perf_evlist *evlist)
78{
79 struct perf_evsel *first = perf_evlist__first(evlist);
80
81 evlist->id_pos = first->id_pos;
82 evlist->is_pos = first->is_pos;
83}
84
Adrian Hunter733cd2f2013-09-06 22:40:11 +030085static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
86{
87 struct perf_evsel *evsel;
88
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030089 evlist__for_each(evlist, evsel)
Adrian Hunter733cd2f2013-09-06 22:40:11 +030090 perf_evsel__calc_id_pos(evsel);
91
92 perf_evlist__set_id_pos(evlist);
93}
94
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020095static void perf_evlist__purge(struct perf_evlist *evlist)
96{
97 struct perf_evsel *pos, *n;
98
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030099 evlist__for_each_safe(evlist, n, pos) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200100 list_del_init(&pos->node);
101 perf_evsel__delete(pos);
102 }
103
104 evlist->nr_entries = 0;
105}
106
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200107void perf_evlist__exit(struct perf_evlist *evlist)
108{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300109 zfree(&evlist->mmap);
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300110 fdarray__exit(&evlist->pollfd);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200111}
112
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200113void perf_evlist__delete(struct perf_evlist *evlist)
114{
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300115 perf_evlist__munmap(evlist);
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -0300116 perf_evlist__close(evlist);
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200117 cpu_map__put(evlist->cpus);
Jiri Olsa186fbb72015-06-23 00:36:05 +0200118 thread_map__put(evlist->threads);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300119 evlist->cpus = NULL;
120 evlist->threads = NULL;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200121 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200122 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200123 free(evlist);
124}
125
126void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
127{
128 list_add_tail(&entry->node, &evlist->entries);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300129 entry->idx = evlist->nr_entries;
Adrian Hunter60b08962014-07-31 09:00:52 +0300130 entry->tracking = !entry->idx;
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300131
Adrian Hunter75562572013-08-27 11:23:09 +0300132 if (!evlist->nr_entries++)
133 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200134}
135
Jiri Olsa0529bc12012-01-27 15:34:20 +0100136void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
137 struct list_head *list,
138 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200139{
Adrian Hunter75562572013-08-27 11:23:09 +0300140 bool set_id_pos = !evlist->nr_entries;
141
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200142 list_splice_tail(list, &evlist->entries);
143 evlist->nr_entries += nr_entries;
Adrian Hunter75562572013-08-27 11:23:09 +0300144 if (set_id_pos)
145 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200146}
147
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300148void __perf_evlist__set_leader(struct list_head *list)
149{
150 struct perf_evsel *evsel, *leader;
151
152 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900153 evsel = list_entry(list->prev, struct perf_evsel, node);
154
155 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300156
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300157 __evlist__for_each(list, evsel) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100158 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300159 }
160}
161
162void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200163{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900164 if (evlist->nr_entries) {
165 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300166 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900167 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200168}
169
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200170int perf_evlist__add_default(struct perf_evlist *evlist)
171{
172 struct perf_event_attr attr = {
173 .type = PERF_TYPE_HARDWARE,
174 .config = PERF_COUNT_HW_CPU_CYCLES,
175 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100176 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200177
Joerg Roedel1aed2672012-01-04 17:54:20 +0100178 event_attr_init(&attr);
179
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300180 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200181 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200182 goto error;
183
184 /* use strdup() because free(evsel) assumes name is allocated */
185 evsel->name = strdup("cycles");
186 if (!evsel->name)
187 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200188
189 perf_evlist__add(evlist, evsel);
190 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200191error_free:
192 perf_evsel__delete(evsel);
193error:
194 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200195}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200196
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300197static int perf_evlist__add_attrs(struct perf_evlist *evlist,
198 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200199{
200 struct perf_evsel *evsel, *n;
201 LIST_HEAD(head);
202 size_t i;
203
204 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300205 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200206 if (evsel == NULL)
207 goto out_delete_partial_list;
208 list_add_tail(&evsel->node, &head);
209 }
210
211 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
212
213 return 0;
214
215out_delete_partial_list:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300216 __evlist__for_each_safe(&head, n, evsel)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200217 perf_evsel__delete(evsel);
218 return -1;
219}
220
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300221int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
222 struct perf_event_attr *attrs, size_t nr_attrs)
223{
224 size_t i;
225
226 for (i = 0; i < nr_attrs; i++)
227 event_attr_init(attrs + i);
228
229 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
230}
231
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300232struct perf_evsel *
233perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200234{
235 struct perf_evsel *evsel;
236
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300237 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200238 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
239 (int)evsel->attr.config == id)
240 return evsel;
241 }
242
243 return NULL;
244}
245
David Aherna2f28042013-08-28 22:29:51 -0600246struct perf_evsel *
247perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
248 const char *name)
249{
250 struct perf_evsel *evsel;
251
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300252 evlist__for_each(evlist, evsel) {
David Aherna2f28042013-08-28 22:29:51 -0600253 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
254 (strcmp(evsel->name, name) == 0))
255 return evsel;
256 }
257
258 return NULL;
259}
260
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300261int perf_evlist__add_newtp(struct perf_evlist *evlist,
262 const char *sys, const char *name, void *handler)
263{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300264 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300265
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300266 if (evsel == NULL)
267 return -1;
268
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300269 evsel->handler = handler;
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300270 perf_evlist__add(evlist, evsel);
271 return 0;
272}
273
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300274static int perf_evlist__nr_threads(struct perf_evlist *evlist,
275 struct perf_evsel *evsel)
276{
277 if (evsel->system_wide)
278 return 1;
279 else
280 return thread_map__nr(evlist->threads);
281}
282
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300283void perf_evlist__disable(struct perf_evlist *evlist)
284{
285 int cpu, thread;
286 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900287 int nr_cpus = cpu_map__nr(evlist->cpus);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300288 int nr_threads;
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300289
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900290 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300291 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300292 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100293 continue;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300294 nr_threads = perf_evlist__nr_threads(evlist, pos);
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900295 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900296 ioctl(FD(pos, cpu, thread),
297 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300298 }
299 }
Arnaldo Carvalho de Melo2b56bcf2015-06-17 16:40:26 -0300300
301 evlist->enabled = false;
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300302}
303
David Ahern764e16a32011-08-25 10:17:55 -0600304void perf_evlist__enable(struct perf_evlist *evlist)
305{
306 int cpu, thread;
307 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900308 int nr_cpus = cpu_map__nr(evlist->cpus);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300309 int nr_threads;
David Ahern764e16a32011-08-25 10:17:55 -0600310
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900311 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300312 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300313 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100314 continue;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300315 nr_threads = perf_evlist__nr_threads(evlist, pos);
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900316 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900317 ioctl(FD(pos, cpu, thread),
318 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600319 }
320 }
Arnaldo Carvalho de Melo2b56bcf2015-06-17 16:40:26 -0300321
322 evlist->enabled = true;
323}
324
325void perf_evlist__toggle_enable(struct perf_evlist *evlist)
326{
327 (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
David Ahern764e16a32011-08-25 10:17:55 -0600328}
329
Adrian Hunter395c3072013-08-31 21:50:53 +0300330int perf_evlist__disable_event(struct perf_evlist *evlist,
331 struct perf_evsel *evsel)
332{
333 int cpu, thread, err;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300334 int nr_cpus = cpu_map__nr(evlist->cpus);
335 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
Adrian Hunter395c3072013-08-31 21:50:53 +0300336
337 if (!evsel->fd)
338 return 0;
339
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300340 for (cpu = 0; cpu < nr_cpus; cpu++) {
341 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300342 err = ioctl(FD(evsel, cpu, thread),
343 PERF_EVENT_IOC_DISABLE, 0);
344 if (err)
345 return err;
346 }
347 }
348 return 0;
349}
350
351int perf_evlist__enable_event(struct perf_evlist *evlist,
352 struct perf_evsel *evsel)
353{
354 int cpu, thread, err;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300355 int nr_cpus = cpu_map__nr(evlist->cpus);
356 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
Adrian Hunter395c3072013-08-31 21:50:53 +0300357
358 if (!evsel->fd)
359 return -EINVAL;
360
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300361 for (cpu = 0; cpu < nr_cpus; cpu++) {
362 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300363 err = ioctl(FD(evsel, cpu, thread),
364 PERF_EVENT_IOC_ENABLE, 0);
365 if (err)
366 return err;
367 }
368 }
369 return 0;
370}
371
Adrian Hunter1c650562014-07-31 09:00:56 +0300372static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
373 struct perf_evsel *evsel, int cpu)
374{
375 int thread, err;
376 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
377
378 if (!evsel->fd)
379 return -EINVAL;
380
381 for (thread = 0; thread < nr_threads; thread++) {
382 err = ioctl(FD(evsel, cpu, thread),
383 PERF_EVENT_IOC_ENABLE, 0);
384 if (err)
385 return err;
386 }
387 return 0;
388}
389
390static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
391 struct perf_evsel *evsel,
392 int thread)
393{
394 int cpu, err;
395 int nr_cpus = cpu_map__nr(evlist->cpus);
396
397 if (!evsel->fd)
398 return -EINVAL;
399
400 for (cpu = 0; cpu < nr_cpus; cpu++) {
401 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
402 if (err)
403 return err;
404 }
405 return 0;
406}
407
408int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
409 struct perf_evsel *evsel, int idx)
410{
411 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
412
413 if (per_cpu_mmaps)
414 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
415 else
416 return perf_evlist__enable_event_thread(evlist, evsel, idx);
417}
418
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300419int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200420{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900421 int nr_cpus = cpu_map__nr(evlist->cpus);
422 int nr_threads = thread_map__nr(evlist->threads);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300423 int nfds = 0;
424 struct perf_evsel *evsel;
425
Arnaldo Carvalho de Melocba9b842014-10-29 11:31:54 -0200426 evlist__for_each(evlist, evsel) {
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300427 if (evsel->system_wide)
428 nfds += nr_cpus;
429 else
430 nfds += nr_cpus * nr_threads;
431 }
432
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300433 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
434 fdarray__grow(&evlist->pollfd, nfds) < 0)
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300435 return -ENOMEM;
436
437 return 0;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200438}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200439
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300440static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
441{
442 int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
443 /*
444 * Save the idx so that when we filter out fds POLLHUP'ed we can
445 * close the associated evlist->mmap[] entry.
446 */
447 if (pos >= 0) {
448 evlist->pollfd.priv[pos].idx = idx;
449
450 fcntl(fd, F_SETFL, O_NONBLOCK);
451 }
452
453 return pos;
454}
455
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300456int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200457{
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300458 return __perf_evlist__add_pollfd(evlist, fd, -1);
459}
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300460
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300461static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
462{
463 struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
464
465 perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200466}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200467
Arnaldo Carvalho de Melo1ddec7f2014-08-12 23:04:11 -0300468int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
469{
Arnaldo Carvalho de Meloe4b356b2014-09-08 11:27:49 -0300470 return fdarray__filter(&evlist->pollfd, revents_and_mask,
471 perf_evlist__munmap_filtered);
Arnaldo Carvalho de Melo1ddec7f2014-08-12 23:04:11 -0300472}
473
Arnaldo Carvalho de Melof66a8892014-08-18 17:25:59 -0300474int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
475{
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300476 return fdarray__poll(&evlist->pollfd, timeout);
Arnaldo Carvalho de Melof66a8892014-08-18 17:25:59 -0300477}
478
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300479static void perf_evlist__id_hash(struct perf_evlist *evlist,
480 struct perf_evsel *evsel,
481 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200482{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300483 int hash;
484 struct perf_sample_id *sid = SID(evsel, cpu, thread);
485
486 sid->id = id;
487 sid->evsel = evsel;
488 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
489 hlist_add_head(&sid->node, &evlist->heads[hash]);
490}
491
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300492void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
493 int cpu, int thread, u64 id)
494{
495 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
496 evsel->id[evsel->ids++] = id;
497}
498
499static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
500 struct perf_evsel *evsel,
501 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300502{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200503 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300504 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200505 u64 id;
506 int ret;
507
508 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
509 if (!ret)
510 goto add;
511
512 if (errno != ENOTTY)
513 return -1;
514
515 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200516
Jiri Olsac4861af2012-10-12 13:02:21 +0200517 /*
518 * This way does not work with group format read, so bail
519 * out in that case.
520 */
521 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
522 return -1;
523
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200524 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
525 read(fd, &read_data, sizeof(read_data)) == -1)
526 return -1;
527
528 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
529 ++id_idx;
530 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
531 ++id_idx;
532
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200533 id = read_data[id_idx];
534
535 add:
536 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200537 return 0;
538}
539
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200540static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
541 struct perf_evsel *evsel, int idx, int cpu,
542 int thread)
543{
544 struct perf_sample_id *sid = SID(evsel, cpu, thread);
545 sid->idx = idx;
546 if (evlist->cpus && cpu >= 0)
547 sid->cpu = evlist->cpus->map[cpu];
548 else
549 sid->cpu = -1;
550 if (!evsel->system_wide && evlist->threads && thread >= 0)
Jiri Olsae13798c2015-06-23 00:36:02 +0200551 sid->tid = thread_map__pid(evlist->threads, thread);
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200552 else
553 sid->tid = -1;
554}
555
Jiri Olsa932a3592012-10-11 14:10:35 +0200556struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200557{
558 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200559 struct perf_sample_id *sid;
560 int hash;
561
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200562 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
563 head = &evlist->heads[hash];
564
Sasha Levinb67bfe02013-02-27 17:06:00 -0800565 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200566 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200567 return sid;
568
569 return NULL;
570}
571
572struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
573{
574 struct perf_sample_id *sid;
575
576 if (evlist->nr_entries == 1)
577 return perf_evlist__first(evlist);
578
579 sid = perf_evlist__id2sid(evlist, id);
580 if (sid)
581 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900582
583 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300584 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900585
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200586 return NULL;
587}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200588
Adrian Hunter75562572013-08-27 11:23:09 +0300589static int perf_evlist__event2id(struct perf_evlist *evlist,
590 union perf_event *event, u64 *id)
591{
592 const u64 *array = event->sample.array;
593 ssize_t n;
594
595 n = (event->header.size - sizeof(event->header)) >> 3;
596
597 if (event->header.type == PERF_RECORD_SAMPLE) {
598 if (evlist->id_pos >= n)
599 return -1;
600 *id = array[evlist->id_pos];
601 } else {
602 if (evlist->is_pos > n)
603 return -1;
604 n -= evlist->is_pos;
605 *id = array[n];
606 }
607 return 0;
608}
609
610static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
611 union perf_event *event)
612{
Adrian Hunter98be6962013-09-04 23:18:17 +0300613 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300614 struct hlist_head *head;
615 struct perf_sample_id *sid;
616 int hash;
617 u64 id;
618
619 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300620 return first;
621
622 if (!first->attr.sample_id_all &&
623 event->header.type != PERF_RECORD_SAMPLE)
624 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300625
626 if (perf_evlist__event2id(evlist, event, &id))
627 return NULL;
628
629 /* Synthesized events have an id of zero */
630 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300631 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300632
633 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
634 head = &evlist->heads[hash];
635
636 hlist_for_each_entry(sid, head, node) {
637 if (sid->id == id)
638 return sid->evsel;
639 }
640 return NULL;
641}
642
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300643union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200644{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300645 struct perf_mmap *md = &evlist->mmap[idx];
Sukadev Bhattiprolu79517222015-06-12 01:28:36 -0400646 u64 head;
David Ahern7b8283b52015-04-07 09:20:37 -0600647 u64 old = md->prev;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200648 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200649 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200650
Sukadev Bhattiprolu79517222015-06-12 01:28:36 -0400651 /*
652 * Check if event was unmapped due to a POLLHUP/POLLERR.
653 */
654 if (!atomic_read(&md->refcnt))
655 return NULL;
656
657 head = perf_mmap__read_head(md);
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200658 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200659 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200660 * If we're further behind than half the buffer, there's a chance
661 * the writer will bite our tail and mess up the samples under us.
662 *
663 * If we somehow ended up ahead of the head, we got messed up.
664 *
665 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200666 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200667 int diff = head - old;
668 if (diff > md->mask / 2 || diff < 0) {
669 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
670
671 /*
672 * head points to a known good entry, start there.
673 */
674 old = head;
675 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200676 }
677
678 if (old != head) {
679 size_t size;
680
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200681 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200682 size = event->header.size;
683
684 /*
685 * Event straddles the mmap boundary -- header should always
686 * be inside due to u64 alignment of output.
687 */
688 if ((old & md->mask) + size != ((old + size) & md->mask)) {
689 unsigned int offset = old;
690 unsigned int len = min(sizeof(*event), size), cpy;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200691 void *dst = md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200692
693 do {
694 cpy = min(md->mask + 1 - (offset & md->mask), len);
695 memcpy(dst, &data[offset & md->mask], cpy);
696 offset += cpy;
697 dst += cpy;
698 len -= cpy;
699 } while (len);
700
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200701 event = (union perf_event *) md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200702 }
703
704 old += size;
705 }
706
707 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200708
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200709 return event;
710}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200711
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300712static bool perf_mmap__empty(struct perf_mmap *md)
713{
Adrian Hunterb72e74d2015-04-24 22:29:43 +0300714 return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300715}
716
717static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
718{
Arnaldo Carvalho de Melo71438492015-05-15 15:45:16 -0300719 atomic_inc(&evlist->mmap[idx].refcnt);
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300720}
721
722static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
723{
Arnaldo Carvalho de Melo71438492015-05-15 15:45:16 -0300724 BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300725
Arnaldo Carvalho de Melo71438492015-05-15 15:45:16 -0300726 if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300727 __perf_evlist__munmap(evlist, idx);
728}
729
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800730void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
731{
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300732 struct perf_mmap *md = &evlist->mmap[idx];
733
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800734 if (!evlist->overwrite) {
David Ahern7b8283b52015-04-07 09:20:37 -0600735 u64 old = md->prev;
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800736
737 perf_mmap__write_tail(md, old);
738 }
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300739
Arnaldo Carvalho de Melo71438492015-05-15 15:45:16 -0300740 if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300741 perf_evlist__mmap_put(evlist, idx);
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800742}
743
Adrian Hunter718c6022015-04-09 18:53:42 +0300744int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
745 struct auxtrace_mmap_params *mp __maybe_unused,
746 void *userpg __maybe_unused,
747 int fd __maybe_unused)
748{
749 return 0;
750}
751
752void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
753{
754}
755
756void __weak auxtrace_mmap_params__init(
757 struct auxtrace_mmap_params *mp __maybe_unused,
758 off_t auxtrace_offset __maybe_unused,
759 unsigned int auxtrace_pages __maybe_unused,
760 bool auxtrace_overwrite __maybe_unused)
761{
762}
763
764void __weak auxtrace_mmap_params__set_idx(
765 struct auxtrace_mmap_params *mp __maybe_unused,
766 struct perf_evlist *evlist __maybe_unused,
767 int idx __maybe_unused,
768 bool per_cpu __maybe_unused)
769{
770}
771
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300772static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
773{
774 if (evlist->mmap[idx].base != NULL) {
775 munmap(evlist->mmap[idx].base, evlist->mmap_len);
776 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melo71438492015-05-15 15:45:16 -0300777 atomic_set(&evlist->mmap[idx].refcnt, 0);
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300778 }
Adrian Hunter718c6022015-04-09 18:53:42 +0300779 auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300780}
781
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200782void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200783{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300784 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200785
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300786 if (evlist->mmap == NULL)
787 return;
788
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300789 for (i = 0; i < evlist->nr_mmaps; i++)
790 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300791
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300792 zfree(&evlist->mmap);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200793}
794
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200795static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200796{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300797 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700798 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900799 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300800 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200801 return evlist->mmap != NULL ? 0 : -ENOMEM;
802}
803
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300804struct mmap_params {
805 int prot;
806 int mask;
Adrian Hunter718c6022015-04-09 18:53:42 +0300807 struct auxtrace_mmap_params auxtrace_mp;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300808};
809
810static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
811 struct mmap_params *mp, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200812{
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300813 /*
814 * The last one will be done at perf_evlist__mmap_consume(), so that we
815 * make sure we don't prevent tools from consuming every last event in
816 * the ring buffer.
817 *
818 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
819 * anymore, but the last events for it are still in the ring buffer,
820 * waiting to be consumed.
821 *
822 * Tools can chose to ignore this at their own discretion, but the
823 * evlist layer can't just drop it when filtering events in
824 * perf_evlist__filter_pollfd().
825 */
Arnaldo Carvalho de Melo71438492015-05-15 15:45:16 -0300826 atomic_set(&evlist->mmap[idx].refcnt, 2);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300827 evlist->mmap[idx].prev = 0;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300828 evlist->mmap[idx].mask = mp->mask;
829 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200830 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500831 if (evlist->mmap[idx].base == MAP_FAILED) {
Adrian Hunter02635962013-11-01 15:51:33 +0200832 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
833 errno);
Nelson Elhage301b1952011-12-19 08:39:30 -0500834 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200835 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500836 }
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300837
Adrian Hunter718c6022015-04-09 18:53:42 +0300838 if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap,
839 &mp->auxtrace_mp, evlist->mmap[idx].base, fd))
840 return -1;
841
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200842 return 0;
843}
844
Adrian Hunter04e21312013-10-18 15:29:13 +0300845static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300846 struct mmap_params *mp, int cpu,
847 int thread, int *output)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300848{
849 struct perf_evsel *evsel;
Adrian Hunter04e21312013-10-18 15:29:13 +0300850
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300851 evlist__for_each(evlist, evsel) {
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300852 int fd;
853
854 if (evsel->system_wide && thread)
855 continue;
856
857 fd = FD(evsel, cpu, thread);
Adrian Hunter04e21312013-10-18 15:29:13 +0300858
859 if (*output == -1) {
860 *output = fd;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300861 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
Adrian Hunter04e21312013-10-18 15:29:13 +0300862 return -1;
863 } else {
864 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
865 return -1;
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300866
867 perf_evlist__mmap_get(evlist, idx);
Adrian Hunter04e21312013-10-18 15:29:13 +0300868 }
869
Adrian Hunterf90d1942014-11-11 16:16:39 +0200870 /*
871 * The system_wide flag causes a selected event to be opened
872 * always without a pid. Consequently it will never get a
873 * POLLHUP, but it is used for tracking in combination with
874 * other events, so it should not need to be polled anyway.
875 * Therefore don't add it for polling.
876 */
877 if (!evsel->system_wide &&
878 __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300879 perf_evlist__mmap_put(evlist, idx);
Arnaldo Carvalho de Meload6765d2014-08-18 16:44:06 -0300880 return -1;
Arnaldo Carvalho de Melo82396982014-09-08 13:26:35 -0300881 }
Arnaldo Carvalho de Melo033fa712014-09-08 12:55:12 -0300882
Adrian Hunter3c659ee2014-10-27 15:49:22 +0200883 if (evsel->attr.read_format & PERF_FORMAT_ID) {
884 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
885 fd) < 0)
886 return -1;
887 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
888 thread);
889 }
Adrian Hunter04e21312013-10-18 15:29:13 +0300890 }
891
892 return 0;
893}
894
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300895static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
896 struct mmap_params *mp)
Adrian Hunter04e21312013-10-18 15:29:13 +0300897{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300898 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900899 int nr_cpus = cpu_map__nr(evlist->cpus);
900 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300901
Adrian Huntere3e1a542013-08-14 15:48:24 +0300902 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900903 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300904 int output = -1;
905
Adrian Hunter718c6022015-04-09 18:53:42 +0300906 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
907 true);
908
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900909 for (thread = 0; thread < nr_threads; thread++) {
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300910 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
911 thread, &output))
Adrian Hunter04e21312013-10-18 15:29:13 +0300912 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300913 }
914 }
915
916 return 0;
917
918out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300919 for (cpu = 0; cpu < nr_cpus; cpu++)
920 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300921 return -1;
922}
923
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300924static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
925 struct mmap_params *mp)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300926{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300927 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900928 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300929
Adrian Huntere3e1a542013-08-14 15:48:24 +0300930 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900931 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300932 int output = -1;
933
Adrian Hunter718c6022015-04-09 18:53:42 +0300934 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
935 false);
936
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300937 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
938 &output))
Adrian Hunter04e21312013-10-18 15:29:13 +0300939 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300940 }
941
942 return 0;
943
944out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300945 for (thread = 0; thread < nr_threads; thread++)
946 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300947 return -1;
948}
949
Jiri Olsa994a1f72013-09-01 12:36:12 +0200950static size_t perf_evlist__mmap_size(unsigned long pages)
951{
Arnaldo Carvalho de Melo8185e882014-12-12 17:46:45 -0300952 if (pages == UINT_MAX) {
953 int max;
954
955 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
956 /*
957 * Pick a once upon a time good value, i.e. things look
958 * strange since we can't read a sysctl value, but lets not
959 * die yet...
960 */
961 max = 512;
962 } else {
963 max -= (page_size / 1024);
964 }
965
966 pages = (max * 1024) / page_size;
Arnaldo Carvalho de Melo1be300f2014-12-16 12:19:00 -0300967 if (!is_power_of_2(pages))
968 pages = rounddown_pow_of_two(pages);
Arnaldo Carvalho de Melo8185e882014-12-12 17:46:45 -0300969 } else if (!is_power_of_2(pages))
Jiri Olsa994a1f72013-09-01 12:36:12 +0200970 return 0;
971
972 return (pages + 1) * page_size;
973}
974
David Ahern33c2dcf2013-11-12 07:46:55 -0700975static long parse_pages_arg(const char *str, unsigned long min,
976 unsigned long max)
Jiri Olsa994a1f72013-09-01 12:36:12 +0200977{
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300978 unsigned long pages, val;
Jiri Olsa27050f52013-09-01 12:36:13 +0200979 static struct parse_tag tags[] = {
980 { .tag = 'B', .mult = 1 },
981 { .tag = 'K', .mult = 1 << 10 },
982 { .tag = 'M', .mult = 1 << 20 },
983 { .tag = 'G', .mult = 1 << 30 },
984 { .tag = 0 },
985 };
Jiri Olsa994a1f72013-09-01 12:36:12 +0200986
David Ahern89735042013-11-12 07:46:53 -0700987 if (str == NULL)
David Ahern33c2dcf2013-11-12 07:46:55 -0700988 return -EINVAL;
David Ahern89735042013-11-12 07:46:53 -0700989
Jiri Olsa27050f52013-09-01 12:36:13 +0200990 val = parse_tag_value(str, tags);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300991 if (val != (unsigned long) -1) {
Jiri Olsa27050f52013-09-01 12:36:13 +0200992 /* we got file size value */
993 pages = PERF_ALIGN(val, page_size) / page_size;
Jiri Olsa27050f52013-09-01 12:36:13 +0200994 } else {
995 /* we got pages count value */
996 char *eptr;
997 pages = strtoul(str, &eptr, 10);
David Ahern33c2dcf2013-11-12 07:46:55 -0700998 if (*eptr != '\0')
999 return -EINVAL;
Jiri Olsa994a1f72013-09-01 12:36:12 +02001000 }
1001
Adrian Hunter2bcab6c2013-12-09 15:18:37 +02001002 if (pages == 0 && min == 0) {
David Ahern33c2dcf2013-11-12 07:46:55 -07001003 /* leave number of pages at 0 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +02001004 } else if (!is_power_of_2(pages)) {
David Ahern33c2dcf2013-11-12 07:46:55 -07001005 /* round pages up to next power of 2 */
Arnaldo Carvalho de Melo91529832014-12-16 13:24:41 -03001006 pages = roundup_pow_of_two(pages);
Adrian Hunter1dbfa9382013-12-09 15:18:39 +02001007 if (!pages)
1008 return -EINVAL;
David Ahern96398372013-11-12 07:46:54 -07001009 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
1010 pages * page_size, pages);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +03001011 }
1012
David Ahern33c2dcf2013-11-12 07:46:55 -07001013 if (pages > max)
1014 return -EINVAL;
1015
1016 return pages;
1017}
1018
Adrian Huntere9db1312015-04-09 18:53:46 +03001019int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
David Ahern33c2dcf2013-11-12 07:46:55 -07001020{
David Ahern33c2dcf2013-11-12 07:46:55 -07001021 unsigned long max = UINT_MAX;
1022 long pages;
1023
Adrian Hunterf5ae9c42013-12-09 15:18:38 +02001024 if (max > SIZE_MAX / page_size)
David Ahern33c2dcf2013-11-12 07:46:55 -07001025 max = SIZE_MAX / page_size;
1026
1027 pages = parse_pages_arg(str, 1, max);
1028 if (pages < 0) {
1029 pr_err("Invalid argument for --mmap_pages/-m\n");
Jiri Olsa994a1f72013-09-01 12:36:12 +02001030 return -1;
1031 }
1032
1033 *mmap_pages = pages;
1034 return 0;
1035}
1036
Adrian Huntere9db1312015-04-09 18:53:46 +03001037int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1038 int unset __maybe_unused)
1039{
1040 return __perf_evlist__parse_mmap_pages(opt->value, str);
1041}
1042
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001043/**
Adrian Hunter718c6022015-04-09 18:53:42 +03001044 * perf_evlist__mmap_ex - Create mmaps to receive events.
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001045 * @evlist: list of events
1046 * @pages: map length in pages
1047 * @overwrite: overwrite older events?
Adrian Hunter718c6022015-04-09 18:53:42 +03001048 * @auxtrace_pages - auxtrace map length in pages
1049 * @auxtrace_overwrite - overwrite older auxtrace data?
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001050 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001051 * If @overwrite is %false the user needs to signal event consumption using
1052 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
1053 * automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001054 *
Adrian Hunter718c6022015-04-09 18:53:42 +03001055 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1056 * consumption using auxtrace_mmap__write_tail().
1057 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +03001058 * Return: %0 on success, negative error code otherwise.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001059 */
Adrian Hunter718c6022015-04-09 18:53:42 +03001060int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1061 bool overwrite, unsigned int auxtrace_pages,
1062 bool auxtrace_overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001063{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -03001064 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001065 const struct cpu_map *cpus = evlist->cpus;
1066 const struct thread_map *threads = evlist->threads;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001067 struct mmap_params mp = {
1068 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1069 };
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -02001070
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001071 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001072 return -ENOMEM;
1073
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -03001074 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001075 return -ENOMEM;
1076
1077 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +02001078 evlist->mmap_len = perf_evlist__mmap_size(pages);
Adrian Hunter2af68ef2013-10-18 15:29:07 +03001079 pr_debug("mmap size %zuB\n", evlist->mmap_len);
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001080 mp.mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001081
Adrian Hunter718c6022015-04-09 18:53:42 +03001082 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1083 auxtrace_pages, auxtrace_overwrite);
1084
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001085 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001086 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001087 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -03001088 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001089 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001090 }
1091
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -07001092 if (cpu_map__empty(cpus))
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001093 return perf_evlist__mmap_per_thread(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001094
Adrian Huntera8a8f3e2014-07-14 13:02:52 +03001095 return perf_evlist__mmap_per_cpu(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001096}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001097
Adrian Hunter718c6022015-04-09 18:53:42 +03001098int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1099 bool overwrite)
1100{
1101 return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1102}
1103
Jiri Olsab7f0c202015-06-23 00:36:06 +02001104static int perf_evlist__propagate_maps(struct perf_evlist *evlist,
Jiri Olsa52361ff2015-07-21 14:31:28 +02001105 bool has_user_cpus)
Jiri Olsab7f0c202015-06-23 00:36:06 +02001106{
1107 struct perf_evsel *evsel;
1108
1109 evlist__for_each(evlist, evsel) {
1110 /*
1111 * We already have cpus for evsel (via PMU sysfs) so
1112 * keep it, if there's no target cpu list defined.
1113 */
Jiri Olsa52361ff2015-07-21 14:31:28 +02001114 if (evsel->cpus && has_user_cpus)
Jiri Olsab7f0c202015-06-23 00:36:06 +02001115 cpu_map__put(evsel->cpus);
1116
Jiri Olsa52361ff2015-07-21 14:31:28 +02001117 if (!evsel->cpus || has_user_cpus)
Jiri Olsab7f0c202015-06-23 00:36:06 +02001118 evsel->cpus = cpu_map__get(evlist->cpus);
1119
Jiri Olsa578e91e2015-06-23 00:36:07 +02001120 evsel->threads = thread_map__get(evlist->threads);
1121
Jiri Olsa0e5ffb32015-07-21 14:31:29 +02001122 if ((evlist->cpus && !evsel->cpus) ||
1123 (evlist->threads && !evsel->threads))
Jiri Olsab7f0c202015-06-23 00:36:06 +02001124 return -ENOMEM;
1125 }
1126
1127 return 0;
1128}
1129
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001130int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001131{
Namhyung Kimb809ac12012-04-26 14:15:19 +09001132 evlist->threads = thread_map__new_str(target->pid, target->tid,
1133 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001134
1135 if (evlist->threads == NULL)
1136 return -1;
1137
Dongsheng Yang9c105fb2013-12-04 17:56:40 -05001138 if (target__uses_dummy_map(target))
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +09001139 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +09001140 else
1141 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001142
1143 if (evlist->cpus == NULL)
1144 goto out_delete_threads;
1145
Jiri Olsa52361ff2015-07-21 14:31:28 +02001146 return perf_evlist__propagate_maps(evlist, !!target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001147
1148out_delete_threads:
Jiri Olsa186fbb72015-06-23 00:36:05 +02001149 thread_map__put(evlist->threads);
Yasser Shalabib2e19a92014-10-04 11:37:57 -04001150 evlist->threads = NULL;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001151 return -1;
1152}
1153
Jiri Olsa3de5cfb2015-07-21 14:31:30 +02001154int perf_evlist__set_maps(struct perf_evlist *evlist,
1155 struct cpu_map *cpus,
1156 struct thread_map *threads)
1157{
1158 if (evlist->cpus)
1159 cpu_map__put(evlist->cpus);
1160
1161 evlist->cpus = cpus;
1162
1163 if (evlist->threads)
1164 thread_map__put(evlist->threads);
1165
1166 evlist->threads = threads;
1167
1168 return perf_evlist__propagate_maps(evlist, false);
1169}
1170
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001171int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001172{
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001173 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001174 int err = 0;
1175 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +09001176 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001177
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001178 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001179 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001180 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001181
Arnaldo Carvalho de Melof47805a2015-07-03 15:53:49 -03001182 err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001183 if (err) {
1184 *err_evsel = evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001185 break;
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -03001186 }
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001187 }
1188
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001189 return err;
1190}
1191
1192int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1193{
1194 struct perf_evsel *evsel;
1195 int err = 0;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001196
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001197 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo94ad89b2015-07-03 17:42:03 -03001198 err = perf_evsel__set_filter(evsel, filter);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001199 if (err)
1200 break;
1201 }
1202
1203 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +01001204}
Frederic Weisbecker74429962011-05-21 17:49:00 +02001205
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001206int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001207{
1208 char *filter;
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001209 int ret = -1;
1210 size_t i;
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001211
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001212 for (i = 0; i < npids; ++i) {
1213 if (i == 0) {
1214 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1215 return -1;
1216 } else {
1217 char *tmp;
1218
1219 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1220 goto out_free;
1221
1222 free(filter);
1223 filter = tmp;
1224 }
1225 }
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001226
1227 ret = perf_evlist__set_filter(evlist, filter);
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001228out_free:
Arnaldo Carvalho de Melocfd70a22015-02-21 10:09:55 -08001229 free(filter);
1230 return ret;
1231}
1232
Arnaldo Carvalho de Melobe199ad2015-02-21 11:33:47 -08001233int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1234{
1235 return perf_evlist__set_filter_pids(evlist, 1, &pid);
1236}
1237
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001238bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001239{
Adrian Hunter75562572013-08-27 11:23:09 +03001240 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001241
Adrian Hunter75562572013-08-27 11:23:09 +03001242 if (evlist->nr_entries == 1)
1243 return true;
1244
1245 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1246 return false;
1247
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001248 evlist__for_each(evlist, pos) {
Adrian Hunter75562572013-08-27 11:23:09 +03001249 if (pos->id_pos != evlist->id_pos ||
1250 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001251 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001252 }
1253
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001254 return true;
1255}
1256
Adrian Hunter75562572013-08-27 11:23:09 +03001257u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001258{
Adrian Hunter75562572013-08-27 11:23:09 +03001259 struct perf_evsel *evsel;
1260
1261 if (evlist->combined_sample_type)
1262 return evlist->combined_sample_type;
1263
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001264 evlist__for_each(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +03001265 evlist->combined_sample_type |= evsel->attr.sample_type;
1266
1267 return evlist->combined_sample_type;
1268}
1269
1270u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1271{
1272 evlist->combined_sample_type = 0;
1273 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001274}
1275
Jiri Olsa9ede4732012-10-10 17:38:13 +02001276bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1277{
1278 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1279 u64 read_format = first->attr.read_format;
1280 u64 sample_type = first->attr.sample_type;
1281
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001282 evlist__for_each(evlist, pos) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001283 if (read_format != pos->attr.read_format)
1284 return false;
1285 }
1286
1287 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1288 if ((sample_type & PERF_SAMPLE_READ) &&
1289 !(read_format & PERF_FORMAT_ID)) {
1290 return false;
1291 }
1292
1293 return true;
1294}
1295
1296u64 perf_evlist__read_format(struct perf_evlist *evlist)
1297{
1298 struct perf_evsel *first = perf_evlist__first(evlist);
1299 return first->attr.read_format;
1300}
1301
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001302u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001303{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001304 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001305 struct perf_sample *data;
1306 u64 sample_type;
1307 u16 size = 0;
1308
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001309 if (!first->attr.sample_id_all)
1310 goto out;
1311
1312 sample_type = first->attr.sample_type;
1313
1314 if (sample_type & PERF_SAMPLE_TID)
1315 size += sizeof(data->tid) * 2;
1316
1317 if (sample_type & PERF_SAMPLE_TIME)
1318 size += sizeof(data->time);
1319
1320 if (sample_type & PERF_SAMPLE_ID)
1321 size += sizeof(data->id);
1322
1323 if (sample_type & PERF_SAMPLE_STREAM_ID)
1324 size += sizeof(data->stream_id);
1325
1326 if (sample_type & PERF_SAMPLE_CPU)
1327 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +03001328
1329 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1330 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001331out:
1332 return size;
1333}
1334
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001335bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001336{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001337 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001338
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001339 evlist__for_each_continue(evlist, pos) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001340 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1341 return false;
1342 }
1343
1344 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001345}
1346
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001347bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001348{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001349 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001350 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001351}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001352
1353void perf_evlist__set_selected(struct perf_evlist *evlist,
1354 struct perf_evsel *evsel)
1355{
1356 evlist->selected = evsel;
1357}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001358
Namhyung Kima74b4b62013-03-15 14:48:48 +09001359void perf_evlist__close(struct perf_evlist *evlist)
1360{
1361 struct perf_evsel *evsel;
1362 int ncpus = cpu_map__nr(evlist->cpus);
1363 int nthreads = thread_map__nr(evlist->threads);
Stephane Eranian8ad92192014-01-17 16:34:06 +01001364 int n;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001365
Stephane Eranian8ad92192014-01-17 16:34:06 +01001366 evlist__for_each_reverse(evlist, evsel) {
1367 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1368 perf_evsel__close(evsel, n, nthreads);
1369 }
Namhyung Kima74b4b62013-03-15 14:48:48 +09001370}
1371
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001372static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1373{
1374 int err = -ENOMEM;
1375
1376 /*
1377 * Try reading /sys/devices/system/cpu/online to get
1378 * an all cpus map.
1379 *
1380 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1381 * code needs an overhaul to properly forward the
1382 * error, and we may not want to do that fallback to a
1383 * default cpu identity map :-\
1384 */
1385 evlist->cpus = cpu_map__new(NULL);
1386 if (evlist->cpus == NULL)
1387 goto out;
1388
1389 evlist->threads = thread_map__new_dummy();
1390 if (evlist->threads == NULL)
1391 goto out_free_cpus;
1392
1393 err = 0;
1394out:
1395 return err;
1396out_free_cpus:
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001397 cpu_map__put(evlist->cpus);
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001398 evlist->cpus = NULL;
1399 goto out;
1400}
1401
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001402int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001403{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001404 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001405 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001406
Arnaldo Carvalho de Melo4112eb12014-10-10 15:55:15 -03001407 /*
1408 * Default: one fd per CPU, all threads, aka systemwide
1409 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1410 */
1411 if (evlist->threads == NULL && evlist->cpus == NULL) {
1412 err = perf_evlist__create_syswide_maps(evlist);
1413 if (err < 0)
1414 goto out_err;
1415 }
1416
Adrian Hunter733cd2f2013-09-06 22:40:11 +03001417 perf_evlist__update_id_pos(evlist);
1418
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001419 evlist__for_each(evlist, evsel) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001420 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001421 if (err < 0)
1422 goto out_err;
1423 }
1424
1425 return 0;
1426out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001427 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001428 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001429 return err;
1430}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001431
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001432int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001433 const char *argv[], bool pipe_output,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001434 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001435{
1436 int child_ready_pipe[2], go_pipe[2];
1437 char bf;
1438
1439 if (pipe(child_ready_pipe) < 0) {
1440 perror("failed to create 'ready' pipe");
1441 return -1;
1442 }
1443
1444 if (pipe(go_pipe) < 0) {
1445 perror("failed to create 'go' pipe");
1446 goto out_close_ready_pipe;
1447 }
1448
1449 evlist->workload.pid = fork();
1450 if (evlist->workload.pid < 0) {
1451 perror("failed to fork");
1452 goto out_close_pipes;
1453 }
1454
1455 if (!evlist->workload.pid) {
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001456 int ret;
1457
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001458 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001459 dup2(2, 1);
1460
David Ahern0817df02013-05-25 17:50:39 -06001461 signal(SIGTERM, SIG_DFL);
1462
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001463 close(child_ready_pipe[0]);
1464 close(go_pipe[1]);
1465 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1466
1467 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001468 * Tell the parent we're ready to go
1469 */
1470 close(child_ready_pipe[1]);
1471
1472 /*
1473 * Wait until the parent tells us to go.
1474 */
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001475 ret = read(go_pipe[0], &bf, 1);
1476 /*
1477 * The parent will ask for the execvp() to be performed by
1478 * writing exactly one byte, in workload.cork_fd, usually via
1479 * perf_evlist__start_workload().
1480 *
Arnaldo Carvalho de Melo20f86fc2015-02-03 13:29:05 -03001481 * For cancelling the workload without actually running it,
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001482 * the parent will just close workload.cork_fd, without writing
1483 * anything, i.e. read will return zero and we just exit()
1484 * here.
1485 */
1486 if (ret != 1) {
1487 if (ret == -1)
1488 perror("unable to read pipe");
1489 exit(ret);
1490 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001491
1492 execvp(argv[0], (char **)argv);
1493
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001494 if (exec_error) {
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001495 union sigval val;
1496
1497 val.sival_int = errno;
1498 if (sigqueue(getppid(), SIGUSR1, val))
1499 perror(argv[0]);
1500 } else
1501 perror(argv[0]);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001502 exit(-1);
1503 }
1504
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001505 if (exec_error) {
1506 struct sigaction act = {
1507 .sa_flags = SA_SIGINFO,
1508 .sa_sigaction = exec_error,
1509 };
1510 sigaction(SIGUSR1, &act, NULL);
1511 }
1512
Arnaldo Carvalho de Melo1aaf63b2014-10-10 14:29:49 -03001513 if (target__none(target)) {
1514 if (evlist->threads == NULL) {
1515 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1516 __func__, __LINE__);
1517 goto out_close_pipes;
1518 }
Jiri Olsae13798c2015-06-23 00:36:02 +02001519 thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
Arnaldo Carvalho de Melo1aaf63b2014-10-10 14:29:49 -03001520 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001521
1522 close(child_ready_pipe[1]);
1523 close(go_pipe[0]);
1524 /*
1525 * wait for child to settle
1526 */
1527 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1528 perror("unable to read pipe");
1529 goto out_close_pipes;
1530 }
1531
Namhyung Kimbcf31452013-06-26 16:14:15 +09001532 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001533 evlist->workload.cork_fd = go_pipe[1];
1534 close(child_ready_pipe[0]);
1535 return 0;
1536
1537out_close_pipes:
1538 close(go_pipe[0]);
1539 close(go_pipe[1]);
1540out_close_ready_pipe:
1541 close(child_ready_pipe[0]);
1542 close(child_ready_pipe[1]);
1543 return -1;
1544}
1545
1546int perf_evlist__start_workload(struct perf_evlist *evlist)
1547{
1548 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001549 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001550 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001551 /*
1552 * Remove the cork, let it rip!
1553 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001554 ret = write(evlist->workload.cork_fd, &bf, 1);
1555 if (ret < 0)
1556 perror("enable to write to pipe");
1557
1558 close(evlist->workload.cork_fd);
1559 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001560 }
1561
1562 return 0;
1563}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001564
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001565int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001566 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001567{
Adrian Hunter75562572013-08-27 11:23:09 +03001568 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1569
1570 if (!evsel)
1571 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001572 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001573}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001574
1575size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1576{
1577 struct perf_evsel *evsel;
1578 size_t printed = 0;
1579
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001580 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001581 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1582 perf_evsel__name(evsel));
1583 }
1584
Davidlohr Buesob2222132013-11-12 22:24:24 -08001585 return printed + fprintf(fp, "\n");
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001586}
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03001587
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001588int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1589 int err, char *buf, size_t size)
1590{
1591 int printed, value;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00001592 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001593
1594 switch (err) {
1595 case EACCES:
1596 case EPERM:
1597 printed = scnprintf(buf, size,
1598 "Error:\t%s.\n"
1599 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1600
Adrian Hunter1a472452013-12-11 14:36:23 +02001601 value = perf_event_paranoid();
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001602
1603 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1604
1605 if (value >= 2) {
1606 printed += scnprintf(buf + printed, size - printed,
1607 "For your workloads it needs to be <= 1\nHint:\t");
1608 }
1609 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001610 "For system wide tracing it needs to be set to -1.\n");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001611
1612 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001613 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1614 "Hint:\tThe current value is %d.", value);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001615 break;
1616 default:
1617 scnprintf(buf, size, "%s", emsg);
1618 break;
1619 }
1620
1621 return 0;
1622}
Adrian Huntera025e4f2013-12-11 14:36:35 +02001623
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001624int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1625{
1626 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001627 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001628
1629 switch (err) {
1630 case EPERM:
Arnaldo Carvalho de Meloe5d4a292014-12-12 15:59:51 -03001631 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001632 printed += scnprintf(buf + printed, size - printed,
1633 "Error:\t%s.\n"
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001634 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001635 "Hint:\tTried using %zd kB.\n",
Arnaldo Carvalho de Meloe5d4a292014-12-12 15:59:51 -03001636 emsg, pages_max_per_user, pages_attempted);
Arnaldo Carvalho de Meloe965bea2014-12-12 16:25:33 -03001637
1638 if (pages_attempted >= pages_max_per_user) {
1639 printed += scnprintf(buf + printed, size - printed,
1640 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1641 pages_max_per_user + pages_attempted);
1642 }
1643
1644 printed += scnprintf(buf + printed, size - printed,
1645 "Hint:\tTry using a smaller -m/--mmap-pages value.");
Arnaldo Carvalho de Melo956fa572014-12-11 18:03:01 -03001646 break;
1647 default:
1648 scnprintf(buf, size, "%s", emsg);
1649 break;
1650 }
1651
1652 return 0;
1653}
1654
Adrian Huntera025e4f2013-12-11 14:36:35 +02001655void perf_evlist__to_front(struct perf_evlist *evlist,
1656 struct perf_evsel *move_evsel)
1657{
1658 struct perf_evsel *evsel, *n;
1659 LIST_HEAD(move);
1660
1661 if (move_evsel == perf_evlist__first(evlist))
1662 return;
1663
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001664 evlist__for_each_safe(evlist, n, evsel) {
Adrian Huntera025e4f2013-12-11 14:36:35 +02001665 if (evsel->leader == move_evsel->leader)
1666 list_move_tail(&evsel->node, &move);
1667 }
1668
1669 list_splice(&move, &evlist->entries);
1670}
Adrian Hunter60b08962014-07-31 09:00:52 +03001671
1672void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1673 struct perf_evsel *tracking_evsel)
1674{
1675 struct perf_evsel *evsel;
1676
1677 if (tracking_evsel->tracking)
1678 return;
1679
1680 evlist__for_each(evlist, evsel) {
1681 if (evsel != tracking_evsel)
1682 evsel->tracking = false;
1683 }
1684
1685 tracking_evsel->tracking = true;
1686}