blob: c74d8ecc9c70a457d1c595d1c79d67036811ad79 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
Arnaldo Carvalho de Meloa8c9ae12011-11-05 08:41:51 -02009#include "util.h"
Borislav Petkov553873e2013-12-09 17:14:23 +010010#include <api/fs/debugfs.h>
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -020011#include <poll.h>
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020012#include "cpumap.h"
13#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090014#include "target.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020015#include "evlist.h"
16#include "evsel.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030017#include "debug.h"
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020018#include <unistd.h>
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020019
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020020#include "parse-events.h"
Jiri Olsa994a1f72013-09-01 12:36:12 +020021#include "parse-options.h"
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020022
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020023#include <sys/mman.h>
24
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020025#include <linux/bitops.h>
26#include <linux/hash.h>
27
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020028#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030029#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020030
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020031void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
32 struct thread_map *threads)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020033{
34 int i;
35
36 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
37 INIT_HLIST_HEAD(&evlist->heads[i]);
38 INIT_LIST_HEAD(&evlist->entries);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020039 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020040 evlist->workload.pid = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020041}
42
Namhyung Kim334fe7a2013-03-11 16:43:12 +090043struct perf_evlist *perf_evlist__new(void)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020044{
45 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
46
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020047 if (evlist != NULL)
Namhyung Kim334fe7a2013-03-11 16:43:12 +090048 perf_evlist__init(evlist, NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020049
50 return evlist;
51}
52
Jiri Olsab22d54b2013-09-01 12:36:14 +020053struct perf_evlist *perf_evlist__new_default(void)
54{
55 struct perf_evlist *evlist = perf_evlist__new();
56
57 if (evlist && perf_evlist__add_default(evlist)) {
58 perf_evlist__delete(evlist);
59 evlist = NULL;
60 }
61
62 return evlist;
63}
64
Adrian Hunter75562572013-08-27 11:23:09 +030065/**
66 * perf_evlist__set_id_pos - set the positions of event ids.
67 * @evlist: selected event list
68 *
69 * Events with compatible sample types all have the same id_pos
70 * and is_pos. For convenience, put a copy on evlist.
71 */
72void perf_evlist__set_id_pos(struct perf_evlist *evlist)
73{
74 struct perf_evsel *first = perf_evlist__first(evlist);
75
76 evlist->id_pos = first->id_pos;
77 evlist->is_pos = first->is_pos;
78}
79
Adrian Hunter733cd2f2013-09-06 22:40:11 +030080static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
81{
82 struct perf_evsel *evsel;
83
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030084 evlist__for_each(evlist, evsel)
Adrian Hunter733cd2f2013-09-06 22:40:11 +030085 perf_evsel__calc_id_pos(evsel);
86
87 perf_evlist__set_id_pos(evlist);
88}
89
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020090static void perf_evlist__purge(struct perf_evlist *evlist)
91{
92 struct perf_evsel *pos, *n;
93
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -030094 evlist__for_each_safe(evlist, n, pos) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020095 list_del_init(&pos->node);
96 perf_evsel__delete(pos);
97 }
98
99 evlist->nr_entries = 0;
100}
101
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200102void perf_evlist__exit(struct perf_evlist *evlist)
103{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300104 zfree(&evlist->mmap);
105 zfree(&evlist->pollfd);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200106}
107
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200108void perf_evlist__delete(struct perf_evlist *evlist)
109{
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300110 perf_evlist__munmap(evlist);
Arnaldo Carvalho de Melof26e1c72014-01-03 16:54:12 -0300111 perf_evlist__close(evlist);
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -0300112 cpu_map__delete(evlist->cpus);
113 thread_map__delete(evlist->threads);
114 evlist->cpus = NULL;
115 evlist->threads = NULL;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200116 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200117 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200118 free(evlist);
119}
120
121void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
122{
123 list_add_tail(&entry->node, &evlist->entries);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300124 entry->idx = evlist->nr_entries;
125
Adrian Hunter75562572013-08-27 11:23:09 +0300126 if (!evlist->nr_entries++)
127 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200128}
129
Jiri Olsa0529bc12012-01-27 15:34:20 +0100130void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
131 struct list_head *list,
132 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200133{
Adrian Hunter75562572013-08-27 11:23:09 +0300134 bool set_id_pos = !evlist->nr_entries;
135
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200136 list_splice_tail(list, &evlist->entries);
137 evlist->nr_entries += nr_entries;
Adrian Hunter75562572013-08-27 11:23:09 +0300138 if (set_id_pos)
139 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200140}
141
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300142void __perf_evlist__set_leader(struct list_head *list)
143{
144 struct perf_evsel *evsel, *leader;
145
146 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900147 evsel = list_entry(list->prev, struct perf_evsel, node);
148
149 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300150
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300151 __evlist__for_each(list, evsel) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100152 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300153 }
154}
155
156void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200157{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900158 if (evlist->nr_entries) {
159 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300160 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900161 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200162}
163
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200164int perf_evlist__add_default(struct perf_evlist *evlist)
165{
166 struct perf_event_attr attr = {
167 .type = PERF_TYPE_HARDWARE,
168 .config = PERF_COUNT_HW_CPU_CYCLES,
169 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100170 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200171
Joerg Roedel1aed2672012-01-04 17:54:20 +0100172 event_attr_init(&attr);
173
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300174 evsel = perf_evsel__new(&attr);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200175 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200176 goto error;
177
178 /* use strdup() because free(evsel) assumes name is allocated */
179 evsel->name = strdup("cycles");
180 if (!evsel->name)
181 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200182
183 perf_evlist__add(evlist, evsel);
184 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200185error_free:
186 perf_evsel__delete(evsel);
187error:
188 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200189}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200190
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300191static int perf_evlist__add_attrs(struct perf_evlist *evlist,
192 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200193{
194 struct perf_evsel *evsel, *n;
195 LIST_HEAD(head);
196 size_t i;
197
198 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300199 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200200 if (evsel == NULL)
201 goto out_delete_partial_list;
202 list_add_tail(&evsel->node, &head);
203 }
204
205 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
206
207 return 0;
208
209out_delete_partial_list:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300210 __evlist__for_each_safe(&head, n, evsel)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200211 perf_evsel__delete(evsel);
212 return -1;
213}
214
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300215int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
216 struct perf_event_attr *attrs, size_t nr_attrs)
217{
218 size_t i;
219
220 for (i = 0; i < nr_attrs; i++)
221 event_attr_init(attrs + i);
222
223 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
224}
225
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300226struct perf_evsel *
227perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200228{
229 struct perf_evsel *evsel;
230
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300231 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200232 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
233 (int)evsel->attr.config == id)
234 return evsel;
235 }
236
237 return NULL;
238}
239
David Aherna2f28042013-08-28 22:29:51 -0600240struct perf_evsel *
241perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
242 const char *name)
243{
244 struct perf_evsel *evsel;
245
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300246 evlist__for_each(evlist, evsel) {
David Aherna2f28042013-08-28 22:29:51 -0600247 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
248 (strcmp(evsel->name, name) == 0))
249 return evsel;
250 }
251
252 return NULL;
253}
254
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300255int perf_evlist__add_newtp(struct perf_evlist *evlist,
256 const char *sys, const char *name, void *handler)
257{
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300258 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300259
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300260 if (evsel == NULL)
261 return -1;
262
Arnaldo Carvalho de Melo744a9712013-11-06 10:17:38 -0300263 evsel->handler = handler;
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300264 perf_evlist__add(evlist, evsel);
265 return 0;
266}
267
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300268static int perf_evlist__nr_threads(struct perf_evlist *evlist,
269 struct perf_evsel *evsel)
270{
271 if (evsel->system_wide)
272 return 1;
273 else
274 return thread_map__nr(evlist->threads);
275}
276
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300277void perf_evlist__disable(struct perf_evlist *evlist)
278{
279 int cpu, thread;
280 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900281 int nr_cpus = cpu_map__nr(evlist->cpus);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300282 int nr_threads;
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300283
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900284 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300285 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300286 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100287 continue;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300288 nr_threads = perf_evlist__nr_threads(evlist, pos);
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900289 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900290 ioctl(FD(pos, cpu, thread),
291 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300292 }
293 }
294}
295
David Ahern764e16a32011-08-25 10:17:55 -0600296void perf_evlist__enable(struct perf_evlist *evlist)
297{
298 int cpu, thread;
299 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900300 int nr_cpus = cpu_map__nr(evlist->cpus);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300301 int nr_threads;
David Ahern764e16a32011-08-25 10:17:55 -0600302
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900303 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300304 evlist__for_each(evlist, pos) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300305 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100306 continue;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300307 nr_threads = perf_evlist__nr_threads(evlist, pos);
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900308 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900309 ioctl(FD(pos, cpu, thread),
310 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600311 }
312 }
313}
314
Adrian Hunter395c3072013-08-31 21:50:53 +0300315int perf_evlist__disable_event(struct perf_evlist *evlist,
316 struct perf_evsel *evsel)
317{
318 int cpu, thread, err;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300319 int nr_cpus = cpu_map__nr(evlist->cpus);
320 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
Adrian Hunter395c3072013-08-31 21:50:53 +0300321
322 if (!evsel->fd)
323 return 0;
324
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300325 for (cpu = 0; cpu < nr_cpus; cpu++) {
326 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300327 err = ioctl(FD(evsel, cpu, thread),
328 PERF_EVENT_IOC_DISABLE, 0);
329 if (err)
330 return err;
331 }
332 }
333 return 0;
334}
335
336int perf_evlist__enable_event(struct perf_evlist *evlist,
337 struct perf_evsel *evsel)
338{
339 int cpu, thread, err;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300340 int nr_cpus = cpu_map__nr(evlist->cpus);
341 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
Adrian Hunter395c3072013-08-31 21:50:53 +0300342
343 if (!evsel->fd)
344 return -EINVAL;
345
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300346 for (cpu = 0; cpu < nr_cpus; cpu++) {
347 for (thread = 0; thread < nr_threads; thread++) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300348 err = ioctl(FD(evsel, cpu, thread),
349 PERF_EVENT_IOC_ENABLE, 0);
350 if (err)
351 return err;
352 }
353 }
354 return 0;
355}
356
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200357static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200358{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900359 int nr_cpus = cpu_map__nr(evlist->cpus);
360 int nr_threads = thread_map__nr(evlist->threads);
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300361 int nfds = 0;
362 struct perf_evsel *evsel;
363
364 list_for_each_entry(evsel, &evlist->entries, node) {
365 if (evsel->system_wide)
366 nfds += nr_cpus;
367 else
368 nfds += nr_cpus * nr_threads;
369 }
370
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200371 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
372 return evlist->pollfd != NULL ? 0 : -ENOMEM;
373}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200374
375void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
376{
377 fcntl(fd, F_SETFL, O_NONBLOCK);
378 evlist->pollfd[evlist->nr_fds].fd = fd;
379 evlist->pollfd[evlist->nr_fds].events = POLLIN;
380 evlist->nr_fds++;
381}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200382
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300383static void perf_evlist__id_hash(struct perf_evlist *evlist,
384 struct perf_evsel *evsel,
385 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200386{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300387 int hash;
388 struct perf_sample_id *sid = SID(evsel, cpu, thread);
389
390 sid->id = id;
391 sid->evsel = evsel;
392 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
393 hlist_add_head(&sid->node, &evlist->heads[hash]);
394}
395
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300396void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
397 int cpu, int thread, u64 id)
398{
399 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
400 evsel->id[evsel->ids++] = id;
401}
402
403static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
404 struct perf_evsel *evsel,
405 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300406{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200407 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300408 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200409 u64 id;
410 int ret;
411
412 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
413 if (!ret)
414 goto add;
415
416 if (errno != ENOTTY)
417 return -1;
418
419 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200420
Jiri Olsac4861af2012-10-12 13:02:21 +0200421 /*
422 * This way does not work with group format read, so bail
423 * out in that case.
424 */
425 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
426 return -1;
427
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200428 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
429 read(fd, &read_data, sizeof(read_data)) == -1)
430 return -1;
431
432 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
433 ++id_idx;
434 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
435 ++id_idx;
436
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200437 id = read_data[id_idx];
438
439 add:
440 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200441 return 0;
442}
443
Jiri Olsa932a3592012-10-11 14:10:35 +0200444struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200445{
446 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200447 struct perf_sample_id *sid;
448 int hash;
449
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200450 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
451 head = &evlist->heads[hash];
452
Sasha Levinb67bfe02013-02-27 17:06:00 -0800453 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200454 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200455 return sid;
456
457 return NULL;
458}
459
460struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
461{
462 struct perf_sample_id *sid;
463
464 if (evlist->nr_entries == 1)
465 return perf_evlist__first(evlist);
466
467 sid = perf_evlist__id2sid(evlist, id);
468 if (sid)
469 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900470
471 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300472 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900473
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200474 return NULL;
475}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200476
Adrian Hunter75562572013-08-27 11:23:09 +0300477static int perf_evlist__event2id(struct perf_evlist *evlist,
478 union perf_event *event, u64 *id)
479{
480 const u64 *array = event->sample.array;
481 ssize_t n;
482
483 n = (event->header.size - sizeof(event->header)) >> 3;
484
485 if (event->header.type == PERF_RECORD_SAMPLE) {
486 if (evlist->id_pos >= n)
487 return -1;
488 *id = array[evlist->id_pos];
489 } else {
490 if (evlist->is_pos > n)
491 return -1;
492 n -= evlist->is_pos;
493 *id = array[n];
494 }
495 return 0;
496}
497
498static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
499 union perf_event *event)
500{
Adrian Hunter98be6962013-09-04 23:18:17 +0300501 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300502 struct hlist_head *head;
503 struct perf_sample_id *sid;
504 int hash;
505 u64 id;
506
507 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300508 return first;
509
510 if (!first->attr.sample_id_all &&
511 event->header.type != PERF_RECORD_SAMPLE)
512 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300513
514 if (perf_evlist__event2id(evlist, event, &id))
515 return NULL;
516
517 /* Synthesized events have an id of zero */
518 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300519 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300520
521 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
522 head = &evlist->heads[hash];
523
524 hlist_for_each_entry(sid, head, node) {
525 if (sid->id == id)
526 return sid->evsel;
527 }
528 return NULL;
529}
530
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300531union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200532{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300533 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200534 unsigned int head = perf_mmap__read_head(md);
535 unsigned int old = md->prev;
536 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200537 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200538
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200539 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200540 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200541 * If we're further behind than half the buffer, there's a chance
542 * the writer will bite our tail and mess up the samples under us.
543 *
544 * If we somehow ended up ahead of the head, we got messed up.
545 *
546 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200547 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200548 int diff = head - old;
549 if (diff > md->mask / 2 || diff < 0) {
550 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
551
552 /*
553 * head points to a known good entry, start there.
554 */
555 old = head;
556 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200557 }
558
559 if (old != head) {
560 size_t size;
561
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200562 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200563 size = event->header.size;
564
565 /*
566 * Event straddles the mmap boundary -- header should always
567 * be inside due to u64 alignment of output.
568 */
569 if ((old & md->mask) + size != ((old + size) & md->mask)) {
570 unsigned int offset = old;
571 unsigned int len = min(sizeof(*event), size), cpy;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200572 void *dst = md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200573
574 do {
575 cpy = min(md->mask + 1 - (offset & md->mask), len);
576 memcpy(dst, &data[offset & md->mask], cpy);
577 offset += cpy;
578 dst += cpy;
579 len -= cpy;
580 } while (len);
581
Jiri Olsaa65cb4b2013-10-02 15:46:39 +0200582 event = (union perf_event *) md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200583 }
584
585 old += size;
586 }
587
588 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200589
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200590 return event;
591}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200592
Zhouyi Zhou8e50d382013-10-24 15:43:33 +0800593void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
594{
595 if (!evlist->overwrite) {
596 struct perf_mmap *md = &evlist->mmap[idx];
597 unsigned int old = md->prev;
598
599 perf_mmap__write_tail(md, old);
600 }
601}
602
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300603static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
604{
605 if (evlist->mmap[idx].base != NULL) {
606 munmap(evlist->mmap[idx].base, evlist->mmap_len);
607 evlist->mmap[idx].base = NULL;
608 }
609}
610
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200611void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200612{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300613 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200614
Arnaldo Carvalho de Melo983874d2014-01-03 17:25:49 -0300615 if (evlist->mmap == NULL)
616 return;
617
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300618 for (i = 0; i < evlist->nr_mmaps; i++)
619 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300620
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300621 zfree(&evlist->mmap);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200622}
623
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200624static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200625{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300626 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700627 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900628 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300629 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200630 return evlist->mmap != NULL ? 0 : -ENOMEM;
631}
632
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300633struct mmap_params {
634 int prot;
635 int mask;
636};
637
638static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
639 struct mmap_params *mp, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200640{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300641 evlist->mmap[idx].prev = 0;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300642 evlist->mmap[idx].mask = mp->mask;
643 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200644 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500645 if (evlist->mmap[idx].base == MAP_FAILED) {
Adrian Hunter02635962013-11-01 15:51:33 +0200646 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
647 errno);
Nelson Elhage301b1952011-12-19 08:39:30 -0500648 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200649 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500650 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200651
652 perf_evlist__add_pollfd(evlist, fd);
653 return 0;
654}
655
Adrian Hunter04e21312013-10-18 15:29:13 +0300656static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300657 struct mmap_params *mp, int cpu,
658 int thread, int *output)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300659{
660 struct perf_evsel *evsel;
Adrian Hunter04e21312013-10-18 15:29:13 +0300661
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300662 evlist__for_each(evlist, evsel) {
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300663 int fd;
664
665 if (evsel->system_wide && thread)
666 continue;
667
668 fd = FD(evsel, cpu, thread);
Adrian Hunter04e21312013-10-18 15:29:13 +0300669
670 if (*output == -1) {
671 *output = fd;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300672 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
Adrian Hunter04e21312013-10-18 15:29:13 +0300673 return -1;
674 } else {
675 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
676 return -1;
677 }
678
679 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
680 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
681 return -1;
682 }
683
684 return 0;
685}
686
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300687static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
688 struct mmap_params *mp)
Adrian Hunter04e21312013-10-18 15:29:13 +0300689{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300690 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900691 int nr_cpus = cpu_map__nr(evlist->cpus);
692 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300693
Adrian Huntere3e1a542013-08-14 15:48:24 +0300694 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900695 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300696 int output = -1;
697
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900698 for (thread = 0; thread < nr_threads; thread++) {
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300699 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
700 thread, &output))
Adrian Hunter04e21312013-10-18 15:29:13 +0300701 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300702 }
703 }
704
705 return 0;
706
707out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300708 for (cpu = 0; cpu < nr_cpus; cpu++)
709 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300710 return -1;
711}
712
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300713static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
714 struct mmap_params *mp)
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300715{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300716 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900717 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300718
Adrian Huntere3e1a542013-08-14 15:48:24 +0300719 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900720 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300721 int output = -1;
722
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300723 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
724 &output))
Adrian Hunter04e21312013-10-18 15:29:13 +0300725 goto out_unmap;
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300726 }
727
728 return 0;
729
730out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300731 for (thread = 0; thread < nr_threads; thread++)
732 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300733 return -1;
734}
735
Jiri Olsa994a1f72013-09-01 12:36:12 +0200736static size_t perf_evlist__mmap_size(unsigned long pages)
737{
738 /* 512 kiB: default amount of unprivileged mlocked memory */
739 if (pages == UINT_MAX)
740 pages = (512 * 1024) / page_size;
741 else if (!is_power_of_2(pages))
742 return 0;
743
744 return (pages + 1) * page_size;
745}
746
David Ahern33c2dcf2013-11-12 07:46:55 -0700747static long parse_pages_arg(const char *str, unsigned long min,
748 unsigned long max)
Jiri Olsa994a1f72013-09-01 12:36:12 +0200749{
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300750 unsigned long pages, val;
Jiri Olsa27050f52013-09-01 12:36:13 +0200751 static struct parse_tag tags[] = {
752 { .tag = 'B', .mult = 1 },
753 { .tag = 'K', .mult = 1 << 10 },
754 { .tag = 'M', .mult = 1 << 20 },
755 { .tag = 'G', .mult = 1 << 30 },
756 { .tag = 0 },
757 };
Jiri Olsa994a1f72013-09-01 12:36:12 +0200758
David Ahern89735042013-11-12 07:46:53 -0700759 if (str == NULL)
David Ahern33c2dcf2013-11-12 07:46:55 -0700760 return -EINVAL;
David Ahern89735042013-11-12 07:46:53 -0700761
Jiri Olsa27050f52013-09-01 12:36:13 +0200762 val = parse_tag_value(str, tags);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300763 if (val != (unsigned long) -1) {
Jiri Olsa27050f52013-09-01 12:36:13 +0200764 /* we got file size value */
765 pages = PERF_ALIGN(val, page_size) / page_size;
Jiri Olsa27050f52013-09-01 12:36:13 +0200766 } else {
767 /* we got pages count value */
768 char *eptr;
769 pages = strtoul(str, &eptr, 10);
David Ahern33c2dcf2013-11-12 07:46:55 -0700770 if (*eptr != '\0')
771 return -EINVAL;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200772 }
773
Adrian Hunter2bcab6c2013-12-09 15:18:37 +0200774 if (pages == 0 && min == 0) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700775 /* leave number of pages at 0 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200776 } else if (!is_power_of_2(pages)) {
David Ahern33c2dcf2013-11-12 07:46:55 -0700777 /* round pages up to next power of 2 */
Adrian Hunter1dbfa9382013-12-09 15:18:39 +0200778 pages = next_pow2_l(pages);
779 if (!pages)
780 return -EINVAL;
David Ahern96398372013-11-12 07:46:54 -0700781 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
782 pages * page_size, pages);
Adrian Hunter2fbe4ab2013-10-22 10:34:18 +0300783 }
784
David Ahern33c2dcf2013-11-12 07:46:55 -0700785 if (pages > max)
786 return -EINVAL;
787
788 return pages;
789}
790
791int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
792 int unset __maybe_unused)
793{
794 unsigned int *mmap_pages = opt->value;
795 unsigned long max = UINT_MAX;
796 long pages;
797
Adrian Hunterf5ae9c42013-12-09 15:18:38 +0200798 if (max > SIZE_MAX / page_size)
David Ahern33c2dcf2013-11-12 07:46:55 -0700799 max = SIZE_MAX / page_size;
800
801 pages = parse_pages_arg(str, 1, max);
802 if (pages < 0) {
803 pr_err("Invalid argument for --mmap_pages/-m\n");
Jiri Olsa994a1f72013-09-01 12:36:12 +0200804 return -1;
805 }
806
807 *mmap_pages = pages;
808 return 0;
809}
810
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300811/**
812 * perf_evlist__mmap - Create mmaps to receive events.
813 * @evlist: list of events
814 * @pages: map length in pages
815 * @overwrite: overwrite older events?
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200816 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300817 * If @overwrite is %false the user needs to signal event consumption using
818 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
819 * automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200820 *
Adrian Hunterc83fa7f2013-10-18 15:29:12 +0300821 * Return: %0 on success, negative error code otherwise.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200822 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200823int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
824 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200825{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300826 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200827 const struct cpu_map *cpus = evlist->cpus;
828 const struct thread_map *threads = evlist->threads;
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300829 struct mmap_params mp = {
830 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
831 };
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200832
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200833 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200834 return -ENOMEM;
835
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200836 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200837 return -ENOMEM;
838
839 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200840 evlist->mmap_len = perf_evlist__mmap_size(pages);
Adrian Hunter2af68ef2013-10-18 15:29:07 +0300841 pr_debug("mmap size %zuB\n", evlist->mmap_len);
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300842 mp.mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200843
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300844 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200845 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300846 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300847 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200848 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200849 }
850
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700851 if (cpu_map__empty(cpus))
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300852 return perf_evlist__mmap_per_thread(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200853
Adrian Huntera8a8f3e2014-07-14 13:02:52 +0300854 return perf_evlist__mmap_per_cpu(evlist, &mp);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200855}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200856
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300857int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200858{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900859 evlist->threads = thread_map__new_str(target->pid, target->tid,
860 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200861
862 if (evlist->threads == NULL)
863 return -1;
864
Dongsheng Yang9c105fb2013-12-04 17:56:40 -0500865 if (target__uses_dummy_map(target))
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900866 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900867 else
868 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200869
870 if (evlist->cpus == NULL)
871 goto out_delete_threads;
872
873 return 0;
874
875out_delete_threads:
876 thread_map__delete(evlist->threads);
877 return -1;
878}
879
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300880int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100881{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100882 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300883 int err = 0;
884 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900885 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100886
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300887 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300888 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100889 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300890
891 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
892 if (err)
893 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100894 }
895
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300896 return err;
897}
898
899int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
900{
901 struct perf_evsel *evsel;
902 int err = 0;
903 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900904 nthreads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300905
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300906 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300907 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
908 if (err)
909 break;
910 }
911
912 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100913}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200914
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300915bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200916{
Adrian Hunter75562572013-08-27 11:23:09 +0300917 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300918
Adrian Hunter75562572013-08-27 11:23:09 +0300919 if (evlist->nr_entries == 1)
920 return true;
921
922 if (evlist->id_pos < 0 || evlist->is_pos < 0)
923 return false;
924
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300925 evlist__for_each(evlist, pos) {
Adrian Hunter75562572013-08-27 11:23:09 +0300926 if (pos->id_pos != evlist->id_pos ||
927 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300928 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200929 }
930
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300931 return true;
932}
933
Adrian Hunter75562572013-08-27 11:23:09 +0300934u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300935{
Adrian Hunter75562572013-08-27 11:23:09 +0300936 struct perf_evsel *evsel;
937
938 if (evlist->combined_sample_type)
939 return evlist->combined_sample_type;
940
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300941 evlist__for_each(evlist, evsel)
Adrian Hunter75562572013-08-27 11:23:09 +0300942 evlist->combined_sample_type |= evsel->attr.sample_type;
943
944 return evlist->combined_sample_type;
945}
946
947u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
948{
949 evlist->combined_sample_type = 0;
950 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300951}
952
Jiri Olsa9ede4732012-10-10 17:38:13 +0200953bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
954{
955 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
956 u64 read_format = first->attr.read_format;
957 u64 sample_type = first->attr.sample_type;
958
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300959 evlist__for_each(evlist, pos) {
Jiri Olsa9ede4732012-10-10 17:38:13 +0200960 if (read_format != pos->attr.read_format)
961 return false;
962 }
963
964 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
965 if ((sample_type & PERF_SAMPLE_READ) &&
966 !(read_format & PERF_FORMAT_ID)) {
967 return false;
968 }
969
970 return true;
971}
972
973u64 perf_evlist__read_format(struct perf_evlist *evlist)
974{
975 struct perf_evsel *first = perf_evlist__first(evlist);
976 return first->attr.read_format;
977}
978
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300979u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200980{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300981 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200982 struct perf_sample *data;
983 u64 sample_type;
984 u16 size = 0;
985
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200986 if (!first->attr.sample_id_all)
987 goto out;
988
989 sample_type = first->attr.sample_type;
990
991 if (sample_type & PERF_SAMPLE_TID)
992 size += sizeof(data->tid) * 2;
993
994 if (sample_type & PERF_SAMPLE_TIME)
995 size += sizeof(data->time);
996
997 if (sample_type & PERF_SAMPLE_ID)
998 size += sizeof(data->id);
999
1000 if (sample_type & PERF_SAMPLE_STREAM_ID)
1001 size += sizeof(data->stream_id);
1002
1003 if (sample_type & PERF_SAMPLE_CPU)
1004 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +03001005
1006 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1007 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -02001008out:
1009 return size;
1010}
1011
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001012bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001013{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001014 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001015
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001016 evlist__for_each_continue(evlist, pos) {
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001017 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1018 return false;
1019 }
1020
1021 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001022}
1023
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001024bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +02001025{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001026 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -03001027 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +02001028}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001029
1030void perf_evlist__set_selected(struct perf_evlist *evlist,
1031 struct perf_evsel *evsel)
1032{
1033 evlist->selected = evsel;
1034}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001035
Namhyung Kima74b4b62013-03-15 14:48:48 +09001036void perf_evlist__close(struct perf_evlist *evlist)
1037{
1038 struct perf_evsel *evsel;
1039 int ncpus = cpu_map__nr(evlist->cpus);
1040 int nthreads = thread_map__nr(evlist->threads);
Stephane Eranian8ad92192014-01-17 16:34:06 +01001041 int n;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001042
Stephane Eranian8ad92192014-01-17 16:34:06 +01001043 evlist__for_each_reverse(evlist, evsel) {
1044 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1045 perf_evsel__close(evsel, n, nthreads);
1046 }
Namhyung Kima74b4b62013-03-15 14:48:48 +09001047}
1048
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001049int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001050{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001051 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +09001052 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001053
Adrian Hunter733cd2f2013-09-06 22:40:11 +03001054 perf_evlist__update_id_pos(evlist);
1055
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001056 evlist__for_each(evlist, evsel) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001057 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001058 if (err < 0)
1059 goto out_err;
1060 }
1061
1062 return 0;
1063out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001064 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001065 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001066 return err;
1067}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001068
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001069int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001070 const char *argv[], bool pipe_output,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001071 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001072{
1073 int child_ready_pipe[2], go_pipe[2];
1074 char bf;
1075
1076 if (pipe(child_ready_pipe) < 0) {
1077 perror("failed to create 'ready' pipe");
1078 return -1;
1079 }
1080
1081 if (pipe(go_pipe) < 0) {
1082 perror("failed to create 'go' pipe");
1083 goto out_close_ready_pipe;
1084 }
1085
1086 evlist->workload.pid = fork();
1087 if (evlist->workload.pid < 0) {
1088 perror("failed to fork");
1089 goto out_close_pipes;
1090 }
1091
1092 if (!evlist->workload.pid) {
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001093 int ret;
1094
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001095 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001096 dup2(2, 1);
1097
David Ahern0817df02013-05-25 17:50:39 -06001098 signal(SIGTERM, SIG_DFL);
1099
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001100 close(child_ready_pipe[0]);
1101 close(go_pipe[1]);
1102 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1103
1104 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001105 * Tell the parent we're ready to go
1106 */
1107 close(child_ready_pipe[1]);
1108
1109 /*
1110 * Wait until the parent tells us to go.
1111 */
Arnaldo Carvalho de Melo5f1c4222014-07-28 12:39:50 -03001112 ret = read(go_pipe[0], &bf, 1);
1113 /*
1114 * The parent will ask for the execvp() to be performed by
1115 * writing exactly one byte, in workload.cork_fd, usually via
1116 * perf_evlist__start_workload().
1117 *
1118 * For cancelling the workload without actuallin running it,
1119 * the parent will just close workload.cork_fd, without writing
1120 * anything, i.e. read will return zero and we just exit()
1121 * here.
1122 */
1123 if (ret != 1) {
1124 if (ret == -1)
1125 perror("unable to read pipe");
1126 exit(ret);
1127 }
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001128
1129 execvp(argv[0], (char **)argv);
1130
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001131 if (exec_error) {
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -03001132 union sigval val;
1133
1134 val.sival_int = errno;
1135 if (sigqueue(getppid(), SIGUSR1, val))
1136 perror(argv[0]);
1137 } else
1138 perror(argv[0]);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001139 exit(-1);
1140 }
1141
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -03001142 if (exec_error) {
1143 struct sigaction act = {
1144 .sa_flags = SA_SIGINFO,
1145 .sa_sigaction = exec_error,
1146 };
1147 sigaction(SIGUSR1, &act, NULL);
1148 }
1149
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001150 if (target__none(target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001151 evlist->threads->map[0] = evlist->workload.pid;
1152
1153 close(child_ready_pipe[1]);
1154 close(go_pipe[0]);
1155 /*
1156 * wait for child to settle
1157 */
1158 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1159 perror("unable to read pipe");
1160 goto out_close_pipes;
1161 }
1162
Namhyung Kimbcf31452013-06-26 16:14:15 +09001163 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001164 evlist->workload.cork_fd = go_pipe[1];
1165 close(child_ready_pipe[0]);
1166 return 0;
1167
1168out_close_pipes:
1169 close(go_pipe[0]);
1170 close(go_pipe[1]);
1171out_close_ready_pipe:
1172 close(child_ready_pipe[0]);
1173 close(child_ready_pipe[1]);
1174 return -1;
1175}
1176
1177int perf_evlist__start_workload(struct perf_evlist *evlist)
1178{
1179 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001180 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001181 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001182 /*
1183 * Remove the cork, let it rip!
1184 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001185 ret = write(evlist->workload.cork_fd, &bf, 1);
1186 if (ret < 0)
1187 perror("enable to write to pipe");
1188
1189 close(evlist->workload.cork_fd);
1190 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001191 }
1192
1193 return 0;
1194}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001195
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001196int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001197 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001198{
Adrian Hunter75562572013-08-27 11:23:09 +03001199 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1200
1201 if (!evsel)
1202 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001203 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001204}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001205
1206size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1207{
1208 struct perf_evsel *evsel;
1209 size_t printed = 0;
1210
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001211 evlist__for_each(evlist, evsel) {
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001212 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1213 perf_evsel__name(evsel));
1214 }
1215
Davidlohr Buesob2222132013-11-12 22:24:24 -08001216 return printed + fprintf(fp, "\n");
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001217}
Arnaldo Carvalho de Melo6ef068c2013-10-17 12:07:58 -03001218
1219int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1220 int err, char *buf, size_t size)
1221{
1222 char sbuf[128];
1223
1224 switch (err) {
1225 case ENOENT:
1226 scnprintf(buf, size, "%s",
1227 "Error:\tUnable to find debugfs\n"
1228 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1229 "Hint:\tIs the debugfs filesystem mounted?\n"
1230 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1231 break;
1232 case EACCES:
1233 scnprintf(buf, size,
1234 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1235 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1236 debugfs_mountpoint, debugfs_mountpoint);
1237 break;
1238 default:
1239 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1240 break;
1241 }
1242
1243 return 0;
1244}
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001245
1246int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1247 int err, char *buf, size_t size)
1248{
1249 int printed, value;
1250 char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1251
1252 switch (err) {
1253 case EACCES:
1254 case EPERM:
1255 printed = scnprintf(buf, size,
1256 "Error:\t%s.\n"
1257 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1258
Adrian Hunter1a472452013-12-11 14:36:23 +02001259 value = perf_event_paranoid();
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001260
1261 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1262
1263 if (value >= 2) {
1264 printed += scnprintf(buf + printed, size - printed,
1265 "For your workloads it needs to be <= 1\nHint:\t");
1266 }
1267 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001268 "For system wide tracing it needs to be set to -1.\n");
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001269
1270 printed += scnprintf(buf + printed, size - printed,
Arnaldo Carvalho de Melo5229e362014-06-10 17:18:54 -03001271 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1272 "Hint:\tThe current value is %d.", value);
Arnaldo Carvalho de Meloa8f23d82013-10-17 17:38:29 -03001273 break;
1274 default:
1275 scnprintf(buf, size, "%s", emsg);
1276 break;
1277 }
1278
1279 return 0;
1280}
Adrian Huntera025e4f2013-12-11 14:36:35 +02001281
1282void perf_evlist__to_front(struct perf_evlist *evlist,
1283 struct perf_evsel *move_evsel)
1284{
1285 struct perf_evsel *evsel, *n;
1286 LIST_HEAD(move);
1287
1288 if (move_evsel == perf_evlist__first(evlist))
1289 return;
1290
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001291 evlist__for_each_safe(evlist, n, evsel) {
Adrian Huntera025e4f2013-12-11 14:36:35 +02001292 if (evsel->leader == move_evsel->leader)
1293 list_move_tail(&evsel->node, &move);
1294 }
1295
1296 list_splice(&move, &evlist->entries);
1297}