Input: vmmouse - disable vmmouse before entering suspend mode
[platform/kernel/linux-starfive.git] / tools / perf / util / evlist.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4  *
5  * Parts came from builtin-{top,stat,record}.c, see those files for further
6  * copyright notes.
7  */
8 #include <api/fs/fs.h>
9 #include <errno.h>
10 #include <inttypes.h>
11 #include <poll.h>
12 #include "cpumap.h"
13 #include "util/mmap.h"
14 #include "thread_map.h"
15 #include "target.h"
16 #include "evlist.h"
17 #include "evsel.h"
18 #include "debug.h"
19 #include "units.h"
20 #include "bpf_counter.h"
21 #include <internal/lib.h> // page_size
22 #include "affinity.h"
23 #include "../perf.h"
24 #include "asm/bug.h"
25 #include "bpf-event.h"
26 #include "util/string2.h"
27 #include "util/perf_api_probe.h"
28 #include "util/evsel_fprintf.h"
29 #include "util/evlist-hybrid.h"
30 #include "util/pmu.h"
31 #include <signal.h>
32 #include <unistd.h>
33 #include <sched.h>
34 #include <stdlib.h>
35
36 #include "parse-events.h"
37 #include <subcmd/parse-options.h>
38
39 #include <fcntl.h>
40 #include <sys/ioctl.h>
41 #include <sys/mman.h>
42 #include <sys/prctl.h>
43
44 #include <linux/bitops.h>
45 #include <linux/hash.h>
46 #include <linux/log2.h>
47 #include <linux/err.h>
48 #include <linux/string.h>
49 #include <linux/zalloc.h>
50 #include <perf/evlist.h>
51 #include <perf/evsel.h>
52 #include <perf/cpumap.h>
53 #include <perf/mmap.h>
54
55 #include <internal/xyarray.h>
56
57 #ifdef LACKS_SIGQUEUE_PROTOTYPE
58 int sigqueue(pid_t pid, int sig, const union sigval value);
59 #endif
60
61 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
62 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
63
64 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
65                   struct perf_thread_map *threads)
66 {
67         perf_evlist__init(&evlist->core);
68         perf_evlist__set_maps(&evlist->core, cpus, threads);
69         evlist->workload.pid = -1;
70         evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
71         evlist->ctl_fd.fd = -1;
72         evlist->ctl_fd.ack = -1;
73         evlist->ctl_fd.pos = -1;
74 }
75
76 struct evlist *evlist__new(void)
77 {
78         struct evlist *evlist = zalloc(sizeof(*evlist));
79
80         if (evlist != NULL)
81                 evlist__init(evlist, NULL, NULL);
82
83         return evlist;
84 }
85
86 struct evlist *evlist__new_default(void)
87 {
88         struct evlist *evlist = evlist__new();
89
90         if (evlist && evlist__add_default(evlist)) {
91                 evlist__delete(evlist);
92                 evlist = NULL;
93         }
94
95         return evlist;
96 }
97
98 struct evlist *evlist__new_dummy(void)
99 {
100         struct evlist *evlist = evlist__new();
101
102         if (evlist && evlist__add_dummy(evlist)) {
103                 evlist__delete(evlist);
104                 evlist = NULL;
105         }
106
107         return evlist;
108 }
109
110 /**
111  * evlist__set_id_pos - set the positions of event ids.
112  * @evlist: selected event list
113  *
114  * Events with compatible sample types all have the same id_pos
115  * and is_pos.  For convenience, put a copy on evlist.
116  */
117 void evlist__set_id_pos(struct evlist *evlist)
118 {
119         struct evsel *first = evlist__first(evlist);
120
121         evlist->id_pos = first->id_pos;
122         evlist->is_pos = first->is_pos;
123 }
124
125 static void evlist__update_id_pos(struct evlist *evlist)
126 {
127         struct evsel *evsel;
128
129         evlist__for_each_entry(evlist, evsel)
130                 evsel__calc_id_pos(evsel);
131
132         evlist__set_id_pos(evlist);
133 }
134
135 static void evlist__purge(struct evlist *evlist)
136 {
137         struct evsel *pos, *n;
138
139         evlist__for_each_entry_safe(evlist, n, pos) {
140                 list_del_init(&pos->core.node);
141                 pos->evlist = NULL;
142                 evsel__delete(pos);
143         }
144
145         evlist->core.nr_entries = 0;
146 }
147
148 void evlist__exit(struct evlist *evlist)
149 {
150         zfree(&evlist->mmap);
151         zfree(&evlist->overwrite_mmap);
152         perf_evlist__exit(&evlist->core);
153 }
154
155 void evlist__delete(struct evlist *evlist)
156 {
157         if (evlist == NULL)
158                 return;
159
160         evlist__munmap(evlist);
161         evlist__close(evlist);
162         evlist__purge(evlist);
163         evlist__exit(evlist);
164         free(evlist);
165 }
166
167 void evlist__add(struct evlist *evlist, struct evsel *entry)
168 {
169         perf_evlist__add(&evlist->core, &entry->core);
170         entry->evlist = evlist;
171         entry->tracking = !entry->core.idx;
172
173         if (evlist->core.nr_entries == 1)
174                 evlist__set_id_pos(evlist);
175 }
176
177 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
178 {
179         evsel->evlist = NULL;
180         perf_evlist__remove(&evlist->core, &evsel->core);
181 }
182
183 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
184 {
185         while (!list_empty(list)) {
186                 struct evsel *evsel, *temp, *leader = NULL;
187
188                 __evlist__for_each_entry_safe(list, temp, evsel) {
189                         list_del_init(&evsel->core.node);
190                         evlist__add(evlist, evsel);
191                         leader = evsel;
192                         break;
193                 }
194
195                 __evlist__for_each_entry_safe(list, temp, evsel) {
196                         if (evsel__has_leader(evsel, leader)) {
197                                 list_del_init(&evsel->core.node);
198                                 evlist__add(evlist, evsel);
199                         }
200                 }
201         }
202 }
203
204 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
205                                        const struct evsel_str_handler *assocs, size_t nr_assocs)
206 {
207         size_t i;
208         int err;
209
210         for (i = 0; i < nr_assocs; i++) {
211                 // Adding a handler for an event not in this evlist, just ignore it.
212                 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
213                 if (evsel == NULL)
214                         continue;
215
216                 err = -EEXIST;
217                 if (evsel->handler != NULL)
218                         goto out;
219                 evsel->handler = assocs[i].handler;
220         }
221
222         err = 0;
223 out:
224         return err;
225 }
226
227 void evlist__set_leader(struct evlist *evlist)
228 {
229         perf_evlist__set_leader(&evlist->core);
230 }
231
232 int __evlist__add_default(struct evlist *evlist, bool precise)
233 {
234         struct evsel *evsel;
235
236         evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
237                                   PERF_COUNT_HW_CPU_CYCLES);
238         if (evsel == NULL)
239                 return -ENOMEM;
240
241         evlist__add(evlist, evsel);
242         return 0;
243 }
244
245 int evlist__add_dummy(struct evlist *evlist)
246 {
247         struct perf_event_attr attr = {
248                 .type   = PERF_TYPE_SOFTWARE,
249                 .config = PERF_COUNT_SW_DUMMY,
250                 .size   = sizeof(attr), /* to capture ABI version */
251         };
252         struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
253
254         if (evsel == NULL)
255                 return -ENOMEM;
256
257         evlist__add(evlist, evsel);
258         return 0;
259 }
260
261 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
262 {
263         struct evsel *evsel, *n;
264         LIST_HEAD(head);
265         size_t i;
266
267         for (i = 0; i < nr_attrs; i++) {
268                 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
269                 if (evsel == NULL)
270                         goto out_delete_partial_list;
271                 list_add_tail(&evsel->core.node, &head);
272         }
273
274         evlist__splice_list_tail(evlist, &head);
275
276         return 0;
277
278 out_delete_partial_list:
279         __evlist__for_each_entry_safe(&head, n, evsel)
280                 evsel__delete(evsel);
281         return -1;
282 }
283
284 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
285 {
286         size_t i;
287
288         for (i = 0; i < nr_attrs; i++)
289                 event_attr_init(attrs + i);
290
291         return evlist__add_attrs(evlist, attrs, nr_attrs);
292 }
293
294 __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
295 {
296         return 0;
297 }
298
299 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
300 {
301         struct evsel *evsel;
302
303         evlist__for_each_entry(evlist, evsel) {
304                 if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
305                     (int)evsel->core.attr.config == id)
306                         return evsel;
307         }
308
309         return NULL;
310 }
311
312 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
313 {
314         struct evsel *evsel;
315
316         evlist__for_each_entry(evlist, evsel) {
317                 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
318                     (strcmp(evsel->name, name) == 0))
319                         return evsel;
320         }
321
322         return NULL;
323 }
324
325 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
326 {
327         struct evsel *evsel = evsel__newtp(sys, name);
328
329         if (IS_ERR(evsel))
330                 return -1;
331
332         evsel->handler = handler;
333         evlist__add(evlist, evsel);
334         return 0;
335 }
336
337 static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
338 {
339         if (evsel->core.system_wide)
340                 return 1;
341         else
342                 return perf_thread_map__nr(evlist->core.threads);
343 }
344
345 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
346 {
347         struct evlist_cpu_iterator itr = {
348                 .container = evlist,
349                 .evsel = evlist__first(evlist),
350                 .cpu_map_idx = 0,
351                 .evlist_cpu_map_idx = 0,
352                 .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
353                 .cpu = (struct perf_cpu){ .cpu = -1},
354                 .affinity = affinity,
355         };
356
357         if (itr.affinity) {
358                 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
359                 affinity__set(itr.affinity, itr.cpu.cpu);
360                 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
361                 /*
362                  * If this CPU isn't in the evsel's cpu map then advance through
363                  * the list.
364                  */
365                 if (itr.cpu_map_idx == -1)
366                         evlist_cpu_iterator__next(&itr);
367         }
368         return itr;
369 }
370
371 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
372 {
373         while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
374                 evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
375                 evlist_cpu_itr->cpu_map_idx =
376                         perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
377                                           evlist_cpu_itr->cpu);
378                 if (evlist_cpu_itr->cpu_map_idx != -1)
379                         return;
380         }
381         evlist_cpu_itr->evlist_cpu_map_idx++;
382         if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
383                 evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
384                 evlist_cpu_itr->cpu =
385                         perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
386                                           evlist_cpu_itr->evlist_cpu_map_idx);
387                 if (evlist_cpu_itr->affinity)
388                         affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
389                 evlist_cpu_itr->cpu_map_idx =
390                         perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
391                                           evlist_cpu_itr->cpu);
392                 /*
393                  * If this CPU isn't in the evsel's cpu map then advance through
394                  * the list.
395                  */
396                 if (evlist_cpu_itr->cpu_map_idx == -1)
397                         evlist_cpu_iterator__next(evlist_cpu_itr);
398         }
399 }
400
401 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
402 {
403         return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
404 }
405
406 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
407 {
408         if (!evsel_name)
409                 return 0;
410         if (evsel__is_dummy_event(pos))
411                 return 1;
412         return strcmp(pos->name, evsel_name);
413 }
414
415 static int evlist__is_enabled(struct evlist *evlist)
416 {
417         struct evsel *pos;
418
419         evlist__for_each_entry(evlist, pos) {
420                 if (!evsel__is_group_leader(pos) || !pos->core.fd)
421                         continue;
422                 /* If at least one event is enabled, evlist is enabled. */
423                 if (!pos->disabled)
424                         return true;
425         }
426         return false;
427 }
428
429 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
430 {
431         struct evsel *pos;
432         struct evlist_cpu_iterator evlist_cpu_itr;
433         struct affinity saved_affinity, *affinity = NULL;
434         bool has_imm = false;
435
436         // See explanation in evlist__close()
437         if (!cpu_map__is_dummy(evlist->core.cpus)) {
438                 if (affinity__setup(&saved_affinity) < 0)
439                         return;
440                 affinity = &saved_affinity;
441         }
442
443         /* Disable 'immediate' events last */
444         for (int imm = 0; imm <= 1; imm++) {
445                 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
446                         pos = evlist_cpu_itr.evsel;
447                         if (evsel__strcmp(pos, evsel_name))
448                                 continue;
449                         if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
450                                 continue;
451                         if (pos->immediate)
452                                 has_imm = true;
453                         if (pos->immediate != imm)
454                                 continue;
455                         evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
456                 }
457                 if (!has_imm)
458                         break;
459         }
460
461         affinity__cleanup(affinity);
462         evlist__for_each_entry(evlist, pos) {
463                 if (evsel__strcmp(pos, evsel_name))
464                         continue;
465                 if (!evsel__is_group_leader(pos) || !pos->core.fd)
466                         continue;
467                 pos->disabled = true;
468         }
469
470         /*
471          * If we disabled only single event, we need to check
472          * the enabled state of the evlist manually.
473          */
474         if (evsel_name)
475                 evlist->enabled = evlist__is_enabled(evlist);
476         else
477                 evlist->enabled = false;
478 }
479
480 void evlist__disable(struct evlist *evlist)
481 {
482         __evlist__disable(evlist, NULL);
483 }
484
485 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
486 {
487         __evlist__disable(evlist, evsel_name);
488 }
489
490 static void __evlist__enable(struct evlist *evlist, char *evsel_name)
491 {
492         struct evsel *pos;
493         struct evlist_cpu_iterator evlist_cpu_itr;
494         struct affinity saved_affinity, *affinity = NULL;
495
496         // See explanation in evlist__close()
497         if (!cpu_map__is_dummy(evlist->core.cpus)) {
498                 if (affinity__setup(&saved_affinity) < 0)
499                         return;
500                 affinity = &saved_affinity;
501         }
502
503         evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
504                 pos = evlist_cpu_itr.evsel;
505                 if (evsel__strcmp(pos, evsel_name))
506                         continue;
507                 if (!evsel__is_group_leader(pos) || !pos->core.fd)
508                         continue;
509                 evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
510         }
511         affinity__cleanup(affinity);
512         evlist__for_each_entry(evlist, pos) {
513                 if (evsel__strcmp(pos, evsel_name))
514                         continue;
515                 if (!evsel__is_group_leader(pos) || !pos->core.fd)
516                         continue;
517                 pos->disabled = false;
518         }
519
520         /*
521          * Even single event sets the 'enabled' for evlist,
522          * so the toggle can work properly and toggle to
523          * 'disabled' state.
524          */
525         evlist->enabled = true;
526 }
527
528 void evlist__enable(struct evlist *evlist)
529 {
530         __evlist__enable(evlist, NULL);
531 }
532
533 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
534 {
535         __evlist__enable(evlist, evsel_name);
536 }
537
538 void evlist__toggle_enable(struct evlist *evlist)
539 {
540         (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
541 }
542
543 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
544 {
545         int thread;
546         int nr_threads = evlist__nr_threads(evlist, evsel);
547
548         if (!evsel->core.fd)
549                 return -EINVAL;
550
551         for (thread = 0; thread < nr_threads; thread++) {
552                 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
553                 if (err)
554                         return err;
555         }
556         return 0;
557 }
558
559 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
560 {
561         int cpu;
562         int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
563
564         if (!evsel->core.fd)
565                 return -EINVAL;
566
567         for (cpu = 0; cpu < nr_cpus; cpu++) {
568                 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
569                 if (err)
570                         return err;
571         }
572         return 0;
573 }
574
575 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
576 {
577         bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
578
579         if (per_cpu_mmaps)
580                 return evlist__enable_event_cpu(evlist, evsel, idx);
581
582         return evlist__enable_event_thread(evlist, evsel, idx);
583 }
584
585 int evlist__add_pollfd(struct evlist *evlist, int fd)
586 {
587         return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
588 }
589
590 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
591 {
592         return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
593 }
594
595 #ifdef HAVE_EVENTFD_SUPPORT
596 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
597 {
598         return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
599                                        fdarray_flag__nonfilterable);
600 }
601 #endif
602
603 int evlist__poll(struct evlist *evlist, int timeout)
604 {
605         return perf_evlist__poll(&evlist->core, timeout);
606 }
607
608 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
609 {
610         struct hlist_head *head;
611         struct perf_sample_id *sid;
612         int hash;
613
614         hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
615         head = &evlist->core.heads[hash];
616
617         hlist_for_each_entry(sid, head, node)
618                 if (sid->id == id)
619                         return sid;
620
621         return NULL;
622 }
623
624 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
625 {
626         struct perf_sample_id *sid;
627
628         if (evlist->core.nr_entries == 1 || !id)
629                 return evlist__first(evlist);
630
631         sid = evlist__id2sid(evlist, id);
632         if (sid)
633                 return container_of(sid->evsel, struct evsel, core);
634
635         if (!evlist__sample_id_all(evlist))
636                 return evlist__first(evlist);
637
638         return NULL;
639 }
640
641 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
642 {
643         struct perf_sample_id *sid;
644
645         if (!id)
646                 return NULL;
647
648         sid = evlist__id2sid(evlist, id);
649         if (sid)
650                 return container_of(sid->evsel, struct evsel, core);
651
652         return NULL;
653 }
654
655 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
656 {
657         const __u64 *array = event->sample.array;
658         ssize_t n;
659
660         n = (event->header.size - sizeof(event->header)) >> 3;
661
662         if (event->header.type == PERF_RECORD_SAMPLE) {
663                 if (evlist->id_pos >= n)
664                         return -1;
665                 *id = array[evlist->id_pos];
666         } else {
667                 if (evlist->is_pos > n)
668                         return -1;
669                 n -= evlist->is_pos;
670                 *id = array[n];
671         }
672         return 0;
673 }
674
675 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
676 {
677         struct evsel *first = evlist__first(evlist);
678         struct hlist_head *head;
679         struct perf_sample_id *sid;
680         int hash;
681         u64 id;
682
683         if (evlist->core.nr_entries == 1)
684                 return first;
685
686         if (!first->core.attr.sample_id_all &&
687             event->header.type != PERF_RECORD_SAMPLE)
688                 return first;
689
690         if (evlist__event2id(evlist, event, &id))
691                 return NULL;
692
693         /* Synthesized events have an id of zero */
694         if (!id)
695                 return first;
696
697         hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
698         head = &evlist->core.heads[hash];
699
700         hlist_for_each_entry(sid, head, node) {
701                 if (sid->id == id)
702                         return container_of(sid->evsel, struct evsel, core);
703         }
704         return NULL;
705 }
706
707 static int evlist__set_paused(struct evlist *evlist, bool value)
708 {
709         int i;
710
711         if (!evlist->overwrite_mmap)
712                 return 0;
713
714         for (i = 0; i < evlist->core.nr_mmaps; i++) {
715                 int fd = evlist->overwrite_mmap[i].core.fd;
716                 int err;
717
718                 if (fd < 0)
719                         continue;
720                 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
721                 if (err)
722                         return err;
723         }
724         return 0;
725 }
726
727 static int evlist__pause(struct evlist *evlist)
728 {
729         return evlist__set_paused(evlist, true);
730 }
731
732 static int evlist__resume(struct evlist *evlist)
733 {
734         return evlist__set_paused(evlist, false);
735 }
736
737 static void evlist__munmap_nofree(struct evlist *evlist)
738 {
739         int i;
740
741         if (evlist->mmap)
742                 for (i = 0; i < evlist->core.nr_mmaps; i++)
743                         perf_mmap__munmap(&evlist->mmap[i].core);
744
745         if (evlist->overwrite_mmap)
746                 for (i = 0; i < evlist->core.nr_mmaps; i++)
747                         perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
748 }
749
750 void evlist__munmap(struct evlist *evlist)
751 {
752         evlist__munmap_nofree(evlist);
753         zfree(&evlist->mmap);
754         zfree(&evlist->overwrite_mmap);
755 }
756
757 static void perf_mmap__unmap_cb(struct perf_mmap *map)
758 {
759         struct mmap *m = container_of(map, struct mmap, core);
760
761         mmap__munmap(m);
762 }
763
764 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
765                                        bool overwrite)
766 {
767         int i;
768         struct mmap *map;
769
770         map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
771         if (!map)
772                 return NULL;
773
774         for (i = 0; i < evlist->core.nr_mmaps; i++) {
775                 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
776
777                 /*
778                  * When the perf_mmap() call is made we grab one refcount, plus
779                  * one extra to let perf_mmap__consume() get the last
780                  * events after all real references (perf_mmap__get()) are
781                  * dropped.
782                  *
783                  * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
784                  * thus does perf_mmap__get() on it.
785                  */
786                 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
787         }
788
789         return map;
790 }
791
792 static void
793 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
794                          struct perf_mmap_param *_mp,
795                          int idx, bool per_cpu)
796 {
797         struct evlist *evlist = container_of(_evlist, struct evlist, core);
798         struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
799
800         auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
801 }
802
803 static struct perf_mmap*
804 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
805 {
806         struct evlist *evlist = container_of(_evlist, struct evlist, core);
807         struct mmap *maps;
808
809         maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
810
811         if (!maps) {
812                 maps = evlist__alloc_mmap(evlist, overwrite);
813                 if (!maps)
814                         return NULL;
815
816                 if (overwrite) {
817                         evlist->overwrite_mmap = maps;
818                         if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
819                                 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
820                 } else {
821                         evlist->mmap = maps;
822                 }
823         }
824
825         return &maps[idx].core;
826 }
827
828 static int
829 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
830                           int output, struct perf_cpu cpu)
831 {
832         struct mmap *map = container_of(_map, struct mmap, core);
833         struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
834
835         return mmap__mmap(map, mp, output, cpu);
836 }
837
838 unsigned long perf_event_mlock_kb_in_pages(void)
839 {
840         unsigned long pages;
841         int max;
842
843         if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
844                 /*
845                  * Pick a once upon a time good value, i.e. things look
846                  * strange since we can't read a sysctl value, but lets not
847                  * die yet...
848                  */
849                 max = 512;
850         } else {
851                 max -= (page_size / 1024);
852         }
853
854         pages = (max * 1024) / page_size;
855         if (!is_power_of_2(pages))
856                 pages = rounddown_pow_of_two(pages);
857
858         return pages;
859 }
860
861 size_t evlist__mmap_size(unsigned long pages)
862 {
863         if (pages == UINT_MAX)
864                 pages = perf_event_mlock_kb_in_pages();
865         else if (!is_power_of_2(pages))
866                 return 0;
867
868         return (pages + 1) * page_size;
869 }
870
871 static long parse_pages_arg(const char *str, unsigned long min,
872                             unsigned long max)
873 {
874         unsigned long pages, val;
875         static struct parse_tag tags[] = {
876                 { .tag  = 'B', .mult = 1       },
877                 { .tag  = 'K', .mult = 1 << 10 },
878                 { .tag  = 'M', .mult = 1 << 20 },
879                 { .tag  = 'G', .mult = 1 << 30 },
880                 { .tag  = 0 },
881         };
882
883         if (str == NULL)
884                 return -EINVAL;
885
886         val = parse_tag_value(str, tags);
887         if (val != (unsigned long) -1) {
888                 /* we got file size value */
889                 pages = PERF_ALIGN(val, page_size) / page_size;
890         } else {
891                 /* we got pages count value */
892                 char *eptr;
893                 pages = strtoul(str, &eptr, 10);
894                 if (*eptr != '\0')
895                         return -EINVAL;
896         }
897
898         if (pages == 0 && min == 0) {
899                 /* leave number of pages at 0 */
900         } else if (!is_power_of_2(pages)) {
901                 char buf[100];
902
903                 /* round pages up to next power of 2 */
904                 pages = roundup_pow_of_two(pages);
905                 if (!pages)
906                         return -EINVAL;
907
908                 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
909                 pr_info("rounding mmap pages size to %s (%lu pages)\n",
910                         buf, pages);
911         }
912
913         if (pages > max)
914                 return -EINVAL;
915
916         return pages;
917 }
918
919 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
920 {
921         unsigned long max = UINT_MAX;
922         long pages;
923
924         if (max > SIZE_MAX / page_size)
925                 max = SIZE_MAX / page_size;
926
927         pages = parse_pages_arg(str, 1, max);
928         if (pages < 0) {
929                 pr_err("Invalid argument for --mmap_pages/-m\n");
930                 return -1;
931         }
932
933         *mmap_pages = pages;
934         return 0;
935 }
936
937 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
938 {
939         return __evlist__parse_mmap_pages(opt->value, str);
940 }
941
942 /**
943  * evlist__mmap_ex - Create mmaps to receive events.
944  * @evlist: list of events
945  * @pages: map length in pages
946  * @overwrite: overwrite older events?
947  * @auxtrace_pages - auxtrace map length in pages
948  * @auxtrace_overwrite - overwrite older auxtrace data?
949  *
950  * If @overwrite is %false the user needs to signal event consumption using
951  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
952  * automatically.
953  *
954  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
955  * consumption using auxtrace_mmap__write_tail().
956  *
957  * Return: %0 on success, negative error code otherwise.
958  */
959 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
960                          unsigned int auxtrace_pages,
961                          bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
962                          int comp_level)
963 {
964         /*
965          * Delay setting mp.prot: set it before calling perf_mmap__mmap.
966          * Its value is decided by evsel's write_backward.
967          * So &mp should not be passed through const pointer.
968          */
969         struct mmap_params mp = {
970                 .nr_cblocks     = nr_cblocks,
971                 .affinity       = affinity,
972                 .flush          = flush,
973                 .comp_level     = comp_level
974         };
975         struct perf_evlist_mmap_ops ops = {
976                 .idx  = perf_evlist__mmap_cb_idx,
977                 .get  = perf_evlist__mmap_cb_get,
978                 .mmap = perf_evlist__mmap_cb_mmap,
979         };
980
981         evlist->core.mmap_len = evlist__mmap_size(pages);
982         pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
983
984         auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
985                                    auxtrace_pages, auxtrace_overwrite);
986
987         return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
988 }
989
990 int evlist__mmap(struct evlist *evlist, unsigned int pages)
991 {
992         return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
993 }
994
995 int evlist__create_maps(struct evlist *evlist, struct target *target)
996 {
997         bool all_threads = (target->per_thread && target->system_wide);
998         struct perf_cpu_map *cpus;
999         struct perf_thread_map *threads;
1000
1001         /*
1002          * If specify '-a' and '--per-thread' to perf record, perf record
1003          * will override '--per-thread'. target->per_thread = false and
1004          * target->system_wide = true.
1005          *
1006          * If specify '--per-thread' only to perf record,
1007          * target->per_thread = true and target->system_wide = false.
1008          *
1009          * So target->per_thread && target->system_wide is false.
1010          * For perf record, thread_map__new_str doesn't call
1011          * thread_map__new_all_cpus. That will keep perf record's
1012          * current behavior.
1013          *
1014          * For perf stat, it allows the case that target->per_thread and
1015          * target->system_wide are all true. It means to collect system-wide
1016          * per-thread data. thread_map__new_str will call
1017          * thread_map__new_all_cpus to enumerate all threads.
1018          */
1019         threads = thread_map__new_str(target->pid, target->tid, target->uid,
1020                                       all_threads);
1021
1022         if (!threads)
1023                 return -1;
1024
1025         if (target__uses_dummy_map(target))
1026                 cpus = perf_cpu_map__dummy_new();
1027         else
1028                 cpus = perf_cpu_map__new(target->cpu_list);
1029
1030         if (!cpus)
1031                 goto out_delete_threads;
1032
1033         evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
1034
1035         perf_evlist__set_maps(&evlist->core, cpus, threads);
1036
1037         /* as evlist now has references, put count here */
1038         perf_cpu_map__put(cpus);
1039         perf_thread_map__put(threads);
1040
1041         return 0;
1042
1043 out_delete_threads:
1044         perf_thread_map__put(threads);
1045         return -1;
1046 }
1047
1048 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1049 {
1050         struct evsel *evsel;
1051         int err = 0;
1052
1053         evlist__for_each_entry(evlist, evsel) {
1054                 if (evsel->filter == NULL)
1055                         continue;
1056
1057                 /*
1058                  * filters only work for tracepoint event, which doesn't have cpu limit.
1059                  * So evlist and evsel should always be same.
1060                  */
1061                 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1062                 if (err) {
1063                         *err_evsel = evsel;
1064                         break;
1065                 }
1066         }
1067
1068         return err;
1069 }
1070
1071 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1072 {
1073         struct evsel *evsel;
1074         int err = 0;
1075
1076         if (filter == NULL)
1077                 return -1;
1078
1079         evlist__for_each_entry(evlist, evsel) {
1080                 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1081                         continue;
1082
1083                 err = evsel__set_filter(evsel, filter);
1084                 if (err)
1085                         break;
1086         }
1087
1088         return err;
1089 }
1090
1091 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1092 {
1093         struct evsel *evsel;
1094         int err = 0;
1095
1096         if (filter == NULL)
1097                 return -1;
1098
1099         evlist__for_each_entry(evlist, evsel) {
1100                 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1101                         continue;
1102
1103                 err = evsel__append_tp_filter(evsel, filter);
1104                 if (err)
1105                         break;
1106         }
1107
1108         return err;
1109 }
1110
1111 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1112 {
1113         char *filter;
1114         size_t i;
1115
1116         for (i = 0; i < npids; ++i) {
1117                 if (i == 0) {
1118                         if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1119                                 return NULL;
1120                 } else {
1121                         char *tmp;
1122
1123                         if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1124                                 goto out_free;
1125
1126                         free(filter);
1127                         filter = tmp;
1128                 }
1129         }
1130
1131         return filter;
1132 out_free:
1133         free(filter);
1134         return NULL;
1135 }
1136
1137 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1138 {
1139         char *filter = asprintf__tp_filter_pids(npids, pids);
1140         int ret = evlist__set_tp_filter(evlist, filter);
1141
1142         free(filter);
1143         return ret;
1144 }
1145
1146 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1147 {
1148         return evlist__set_tp_filter_pids(evlist, 1, &pid);
1149 }
1150
1151 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1152 {
1153         char *filter = asprintf__tp_filter_pids(npids, pids);
1154         int ret = evlist__append_tp_filter(evlist, filter);
1155
1156         free(filter);
1157         return ret;
1158 }
1159
1160 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1161 {
1162         return evlist__append_tp_filter_pids(evlist, 1, &pid);
1163 }
1164
1165 bool evlist__valid_sample_type(struct evlist *evlist)
1166 {
1167         struct evsel *pos;
1168
1169         if (evlist->core.nr_entries == 1)
1170                 return true;
1171
1172         if (evlist->id_pos < 0 || evlist->is_pos < 0)
1173                 return false;
1174
1175         evlist__for_each_entry(evlist, pos) {
1176                 if (pos->id_pos != evlist->id_pos ||
1177                     pos->is_pos != evlist->is_pos)
1178                         return false;
1179         }
1180
1181         return true;
1182 }
1183
1184 u64 __evlist__combined_sample_type(struct evlist *evlist)
1185 {
1186         struct evsel *evsel;
1187
1188         if (evlist->combined_sample_type)
1189                 return evlist->combined_sample_type;
1190
1191         evlist__for_each_entry(evlist, evsel)
1192                 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1193
1194         return evlist->combined_sample_type;
1195 }
1196
1197 u64 evlist__combined_sample_type(struct evlist *evlist)
1198 {
1199         evlist->combined_sample_type = 0;
1200         return __evlist__combined_sample_type(evlist);
1201 }
1202
1203 u64 evlist__combined_branch_type(struct evlist *evlist)
1204 {
1205         struct evsel *evsel;
1206         u64 branch_type = 0;
1207
1208         evlist__for_each_entry(evlist, evsel)
1209                 branch_type |= evsel->core.attr.branch_sample_type;
1210         return branch_type;
1211 }
1212
1213 bool evlist__valid_read_format(struct evlist *evlist)
1214 {
1215         struct evsel *first = evlist__first(evlist), *pos = first;
1216         u64 read_format = first->core.attr.read_format;
1217         u64 sample_type = first->core.attr.sample_type;
1218
1219         evlist__for_each_entry(evlist, pos) {
1220                 if (read_format != pos->core.attr.read_format) {
1221                         pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1222                                  read_format, (u64)pos->core.attr.read_format);
1223                 }
1224         }
1225
1226         /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1227         if ((sample_type & PERF_SAMPLE_READ) &&
1228             !(read_format & PERF_FORMAT_ID)) {
1229                 return false;
1230         }
1231
1232         return true;
1233 }
1234
1235 u16 evlist__id_hdr_size(struct evlist *evlist)
1236 {
1237         struct evsel *first = evlist__first(evlist);
1238         struct perf_sample *data;
1239         u64 sample_type;
1240         u16 size = 0;
1241
1242         if (!first->core.attr.sample_id_all)
1243                 goto out;
1244
1245         sample_type = first->core.attr.sample_type;
1246
1247         if (sample_type & PERF_SAMPLE_TID)
1248                 size += sizeof(data->tid) * 2;
1249
1250        if (sample_type & PERF_SAMPLE_TIME)
1251                 size += sizeof(data->time);
1252
1253         if (sample_type & PERF_SAMPLE_ID)
1254                 size += sizeof(data->id);
1255
1256         if (sample_type & PERF_SAMPLE_STREAM_ID)
1257                 size += sizeof(data->stream_id);
1258
1259         if (sample_type & PERF_SAMPLE_CPU)
1260                 size += sizeof(data->cpu) * 2;
1261
1262         if (sample_type & PERF_SAMPLE_IDENTIFIER)
1263                 size += sizeof(data->id);
1264 out:
1265         return size;
1266 }
1267
1268 bool evlist__valid_sample_id_all(struct evlist *evlist)
1269 {
1270         struct evsel *first = evlist__first(evlist), *pos = first;
1271
1272         evlist__for_each_entry_continue(evlist, pos) {
1273                 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1274                         return false;
1275         }
1276
1277         return true;
1278 }
1279
1280 bool evlist__sample_id_all(struct evlist *evlist)
1281 {
1282         struct evsel *first = evlist__first(evlist);
1283         return first->core.attr.sample_id_all;
1284 }
1285
1286 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1287 {
1288         evlist->selected = evsel;
1289 }
1290
1291 void evlist__close(struct evlist *evlist)
1292 {
1293         struct evsel *evsel;
1294         struct evlist_cpu_iterator evlist_cpu_itr;
1295         struct affinity affinity;
1296
1297         /*
1298          * With perf record core.cpus is usually NULL.
1299          * Use the old method to handle this for now.
1300          */
1301         if (!evlist->core.cpus || cpu_map__is_dummy(evlist->core.cpus)) {
1302                 evlist__for_each_entry_reverse(evlist, evsel)
1303                         evsel__close(evsel);
1304                 return;
1305         }
1306
1307         if (affinity__setup(&affinity) < 0)
1308                 return;
1309
1310         evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1311                 perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1312                                       evlist_cpu_itr.cpu_map_idx);
1313         }
1314
1315         affinity__cleanup(&affinity);
1316         evlist__for_each_entry_reverse(evlist, evsel) {
1317                 perf_evsel__free_fd(&evsel->core);
1318                 perf_evsel__free_id(&evsel->core);
1319         }
1320         perf_evlist__reset_id_hash(&evlist->core);
1321 }
1322
1323 static int evlist__create_syswide_maps(struct evlist *evlist)
1324 {
1325         struct perf_cpu_map *cpus;
1326         struct perf_thread_map *threads;
1327         int err = -ENOMEM;
1328
1329         /*
1330          * Try reading /sys/devices/system/cpu/online to get
1331          * an all cpus map.
1332          *
1333          * FIXME: -ENOMEM is the best we can do here, the cpu_map
1334          * code needs an overhaul to properly forward the
1335          * error, and we may not want to do that fallback to a
1336          * default cpu identity map :-\
1337          */
1338         cpus = perf_cpu_map__new(NULL);
1339         if (!cpus)
1340                 goto out;
1341
1342         threads = perf_thread_map__new_dummy();
1343         if (!threads)
1344                 goto out_put;
1345
1346         perf_evlist__set_maps(&evlist->core, cpus, threads);
1347
1348         perf_thread_map__put(threads);
1349 out_put:
1350         perf_cpu_map__put(cpus);
1351 out:
1352         return err;
1353 }
1354
1355 int evlist__open(struct evlist *evlist)
1356 {
1357         struct evsel *evsel;
1358         int err;
1359
1360         /*
1361          * Default: one fd per CPU, all threads, aka systemwide
1362          * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1363          */
1364         if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1365                 err = evlist__create_syswide_maps(evlist);
1366                 if (err < 0)
1367                         goto out_err;
1368         }
1369
1370         evlist__update_id_pos(evlist);
1371
1372         evlist__for_each_entry(evlist, evsel) {
1373                 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1374                 if (err < 0)
1375                         goto out_err;
1376         }
1377
1378         return 0;
1379 out_err:
1380         evlist__close(evlist);
1381         errno = -err;
1382         return err;
1383 }
1384
1385 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1386                              bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1387 {
1388         int child_ready_pipe[2], go_pipe[2];
1389         char bf;
1390
1391         if (pipe(child_ready_pipe) < 0) {
1392                 perror("failed to create 'ready' pipe");
1393                 return -1;
1394         }
1395
1396         if (pipe(go_pipe) < 0) {
1397                 perror("failed to create 'go' pipe");
1398                 goto out_close_ready_pipe;
1399         }
1400
1401         evlist->workload.pid = fork();
1402         if (evlist->workload.pid < 0) {
1403                 perror("failed to fork");
1404                 goto out_close_pipes;
1405         }
1406
1407         if (!evlist->workload.pid) {
1408                 int ret;
1409
1410                 if (pipe_output)
1411                         dup2(2, 1);
1412
1413                 signal(SIGTERM, SIG_DFL);
1414
1415                 close(child_ready_pipe[0]);
1416                 close(go_pipe[1]);
1417                 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1418
1419                 /*
1420                  * Change the name of this process not to confuse --exclude-perf users
1421                  * that sees 'perf' in the window up to the execvp() and thinks that
1422                  * perf samples are not being excluded.
1423                  */
1424                 prctl(PR_SET_NAME, "perf-exec");
1425
1426                 /*
1427                  * Tell the parent we're ready to go
1428                  */
1429                 close(child_ready_pipe[1]);
1430
1431                 /*
1432                  * Wait until the parent tells us to go.
1433                  */
1434                 ret = read(go_pipe[0], &bf, 1);
1435                 /*
1436                  * The parent will ask for the execvp() to be performed by
1437                  * writing exactly one byte, in workload.cork_fd, usually via
1438                  * evlist__start_workload().
1439                  *
1440                  * For cancelling the workload without actually running it,
1441                  * the parent will just close workload.cork_fd, without writing
1442                  * anything, i.e. read will return zero and we just exit()
1443                  * here.
1444                  */
1445                 if (ret != 1) {
1446                         if (ret == -1)
1447                                 perror("unable to read pipe");
1448                         exit(ret);
1449                 }
1450
1451                 execvp(argv[0], (char **)argv);
1452
1453                 if (exec_error) {
1454                         union sigval val;
1455
1456                         val.sival_int = errno;
1457                         if (sigqueue(getppid(), SIGUSR1, val))
1458                                 perror(argv[0]);
1459                 } else
1460                         perror(argv[0]);
1461                 exit(-1);
1462         }
1463
1464         if (exec_error) {
1465                 struct sigaction act = {
1466                         .sa_flags     = SA_SIGINFO,
1467                         .sa_sigaction = exec_error,
1468                 };
1469                 sigaction(SIGUSR1, &act, NULL);
1470         }
1471
1472         if (target__none(target)) {
1473                 if (evlist->core.threads == NULL) {
1474                         fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1475                                 __func__, __LINE__);
1476                         goto out_close_pipes;
1477                 }
1478                 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1479         }
1480
1481         close(child_ready_pipe[1]);
1482         close(go_pipe[0]);
1483         /*
1484          * wait for child to settle
1485          */
1486         if (read(child_ready_pipe[0], &bf, 1) == -1) {
1487                 perror("unable to read pipe");
1488                 goto out_close_pipes;
1489         }
1490
1491         fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1492         evlist->workload.cork_fd = go_pipe[1];
1493         close(child_ready_pipe[0]);
1494         return 0;
1495
1496 out_close_pipes:
1497         close(go_pipe[0]);
1498         close(go_pipe[1]);
1499 out_close_ready_pipe:
1500         close(child_ready_pipe[0]);
1501         close(child_ready_pipe[1]);
1502         return -1;
1503 }
1504
1505 int evlist__start_workload(struct evlist *evlist)
1506 {
1507         if (evlist->workload.cork_fd > 0) {
1508                 char bf = 0;
1509                 int ret;
1510                 /*
1511                  * Remove the cork, let it rip!
1512                  */
1513                 ret = write(evlist->workload.cork_fd, &bf, 1);
1514                 if (ret < 0)
1515                         perror("unable to write to pipe");
1516
1517                 close(evlist->workload.cork_fd);
1518                 return ret;
1519         }
1520
1521         return 0;
1522 }
1523
1524 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1525 {
1526         struct evsel *evsel = evlist__event2evsel(evlist, event);
1527
1528         if (!evsel)
1529                 return -EFAULT;
1530         return evsel__parse_sample(evsel, event, sample);
1531 }
1532
1533 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1534 {
1535         struct evsel *evsel = evlist__event2evsel(evlist, event);
1536
1537         if (!evsel)
1538                 return -EFAULT;
1539         return evsel__parse_sample_timestamp(evsel, event, timestamp);
1540 }
1541
1542 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1543 {
1544         int printed, value;
1545         char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1546
1547         switch (err) {
1548         case EACCES:
1549         case EPERM:
1550                 printed = scnprintf(buf, size,
1551                                     "Error:\t%s.\n"
1552                                     "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1553
1554                 value = perf_event_paranoid();
1555
1556                 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1557
1558                 if (value >= 2) {
1559                         printed += scnprintf(buf + printed, size - printed,
1560                                              "For your workloads it needs to be <= 1\nHint:\t");
1561                 }
1562                 printed += scnprintf(buf + printed, size - printed,
1563                                      "For system wide tracing it needs to be set to -1.\n");
1564
1565                 printed += scnprintf(buf + printed, size - printed,
1566                                     "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1567                                     "Hint:\tThe current value is %d.", value);
1568                 break;
1569         case EINVAL: {
1570                 struct evsel *first = evlist__first(evlist);
1571                 int max_freq;
1572
1573                 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1574                         goto out_default;
1575
1576                 if (first->core.attr.sample_freq < (u64)max_freq)
1577                         goto out_default;
1578
1579                 printed = scnprintf(buf, size,
1580                                     "Error:\t%s.\n"
1581                                     "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1582                                     "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1583                                     emsg, max_freq, first->core.attr.sample_freq);
1584                 break;
1585         }
1586         default:
1587 out_default:
1588                 scnprintf(buf, size, "%s", emsg);
1589                 break;
1590         }
1591
1592         return 0;
1593 }
1594
1595 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1596 {
1597         char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1598         int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1599
1600         switch (err) {
1601         case EPERM:
1602                 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1603                 printed += scnprintf(buf + printed, size - printed,
1604                                      "Error:\t%s.\n"
1605                                      "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1606                                      "Hint:\tTried using %zd kB.\n",
1607                                      emsg, pages_max_per_user, pages_attempted);
1608
1609                 if (pages_attempted >= pages_max_per_user) {
1610                         printed += scnprintf(buf + printed, size - printed,
1611                                              "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1612                                              pages_max_per_user + pages_attempted);
1613                 }
1614
1615                 printed += scnprintf(buf + printed, size - printed,
1616                                      "Hint:\tTry using a smaller -m/--mmap-pages value.");
1617                 break;
1618         default:
1619                 scnprintf(buf, size, "%s", emsg);
1620                 break;
1621         }
1622
1623         return 0;
1624 }
1625
1626 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1627 {
1628         struct evsel *evsel, *n;
1629         LIST_HEAD(move);
1630
1631         if (move_evsel == evlist__first(evlist))
1632                 return;
1633
1634         evlist__for_each_entry_safe(evlist, n, evsel) {
1635                 if (evsel__leader(evsel) == evsel__leader(move_evsel))
1636                         list_move_tail(&evsel->core.node, &move);
1637         }
1638
1639         list_splice(&move, &evlist->core.entries);
1640 }
1641
1642 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1643 {
1644         struct evsel *evsel;
1645
1646         evlist__for_each_entry(evlist, evsel) {
1647                 if (evsel->tracking)
1648                         return evsel;
1649         }
1650
1651         return evlist__first(evlist);
1652 }
1653
1654 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1655 {
1656         struct evsel *evsel;
1657
1658         if (tracking_evsel->tracking)
1659                 return;
1660
1661         evlist__for_each_entry(evlist, evsel) {
1662                 if (evsel != tracking_evsel)
1663                         evsel->tracking = false;
1664         }
1665
1666         tracking_evsel->tracking = true;
1667 }
1668
1669 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1670 {
1671         struct evsel *evsel;
1672
1673         evlist__for_each_entry(evlist, evsel) {
1674                 if (!evsel->name)
1675                         continue;
1676                 if (strcmp(str, evsel->name) == 0)
1677                         return evsel;
1678         }
1679
1680         return NULL;
1681 }
1682
1683 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1684 {
1685         enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1686         enum action {
1687                 NONE,
1688                 PAUSE,
1689                 RESUME,
1690         } action = NONE;
1691
1692         if (!evlist->overwrite_mmap)
1693                 return;
1694
1695         switch (old_state) {
1696         case BKW_MMAP_NOTREADY: {
1697                 if (state != BKW_MMAP_RUNNING)
1698                         goto state_err;
1699                 break;
1700         }
1701         case BKW_MMAP_RUNNING: {
1702                 if (state != BKW_MMAP_DATA_PENDING)
1703                         goto state_err;
1704                 action = PAUSE;
1705                 break;
1706         }
1707         case BKW_MMAP_DATA_PENDING: {
1708                 if (state != BKW_MMAP_EMPTY)
1709                         goto state_err;
1710                 break;
1711         }
1712         case BKW_MMAP_EMPTY: {
1713                 if (state != BKW_MMAP_RUNNING)
1714                         goto state_err;
1715                 action = RESUME;
1716                 break;
1717         }
1718         default:
1719                 WARN_ONCE(1, "Shouldn't get there\n");
1720         }
1721
1722         evlist->bkw_mmap_state = state;
1723
1724         switch (action) {
1725         case PAUSE:
1726                 evlist__pause(evlist);
1727                 break;
1728         case RESUME:
1729                 evlist__resume(evlist);
1730                 break;
1731         case NONE:
1732         default:
1733                 break;
1734         }
1735
1736 state_err:
1737         return;
1738 }
1739
1740 bool evlist__exclude_kernel(struct evlist *evlist)
1741 {
1742         struct evsel *evsel;
1743
1744         evlist__for_each_entry(evlist, evsel) {
1745                 if (!evsel->core.attr.exclude_kernel)
1746                         return false;
1747         }
1748
1749         return true;
1750 }
1751
1752 /*
1753  * Events in data file are not collect in groups, but we still want
1754  * the group display. Set the artificial group and set the leader's
1755  * forced_leader flag to notify the display code.
1756  */
1757 void evlist__force_leader(struct evlist *evlist)
1758 {
1759         if (!evlist->core.nr_groups) {
1760                 struct evsel *leader = evlist__first(evlist);
1761
1762                 evlist__set_leader(evlist);
1763                 leader->forced_leader = true;
1764         }
1765 }
1766
1767 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1768 {
1769         struct evsel *c2, *leader;
1770         bool is_open = true;
1771
1772         leader = evsel__leader(evsel);
1773
1774         pr_debug("Weak group for %s/%d failed\n",
1775                         leader->name, leader->core.nr_members);
1776
1777         /*
1778          * for_each_group_member doesn't work here because it doesn't
1779          * include the first entry.
1780          */
1781         evlist__for_each_entry(evsel_list, c2) {
1782                 if (c2 == evsel)
1783                         is_open = false;
1784                 if (evsel__has_leader(c2, leader)) {
1785                         if (is_open && close)
1786                                 perf_evsel__close(&c2->core);
1787                         evsel__set_leader(c2, c2);
1788                         c2->core.nr_members = 0;
1789                         /*
1790                          * Set this for all former members of the group
1791                          * to indicate they get reopened.
1792                          */
1793                         c2->reset_group = true;
1794                 }
1795         }
1796         return leader;
1797 }
1798
1799 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1800 {
1801         char *s, *p;
1802         int ret = 0, fd;
1803
1804         if (strncmp(str, "fifo:", 5))
1805                 return -EINVAL;
1806
1807         str += 5;
1808         if (!*str || *str == ',')
1809                 return -EINVAL;
1810
1811         s = strdup(str);
1812         if (!s)
1813                 return -ENOMEM;
1814
1815         p = strchr(s, ',');
1816         if (p)
1817                 *p = '\0';
1818
1819         /*
1820          * O_RDWR avoids POLLHUPs which is necessary to allow the other
1821          * end of a FIFO to be repeatedly opened and closed.
1822          */
1823         fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1824         if (fd < 0) {
1825                 pr_err("Failed to open '%s'\n", s);
1826                 ret = -errno;
1827                 goto out_free;
1828         }
1829         *ctl_fd = fd;
1830         *ctl_fd_close = true;
1831
1832         if (p && *++p) {
1833                 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1834                 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1835                 if (fd < 0) {
1836                         pr_err("Failed to open '%s'\n", p);
1837                         ret = -errno;
1838                         goto out_free;
1839                 }
1840                 *ctl_fd_ack = fd;
1841         }
1842
1843 out_free:
1844         free(s);
1845         return ret;
1846 }
1847
1848 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1849 {
1850         char *comma = NULL, *endptr = NULL;
1851
1852         *ctl_fd_close = false;
1853
1854         if (strncmp(str, "fd:", 3))
1855                 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1856
1857         *ctl_fd = strtoul(&str[3], &endptr, 0);
1858         if (endptr == &str[3])
1859                 return -EINVAL;
1860
1861         comma = strchr(str, ',');
1862         if (comma) {
1863                 if (endptr != comma)
1864                         return -EINVAL;
1865
1866                 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1867                 if (endptr == comma + 1 || *endptr != '\0')
1868                         return -EINVAL;
1869         }
1870
1871         return 0;
1872 }
1873
1874 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1875 {
1876         if (*ctl_fd_close) {
1877                 *ctl_fd_close = false;
1878                 close(ctl_fd);
1879                 if (ctl_fd_ack >= 0)
1880                         close(ctl_fd_ack);
1881         }
1882 }
1883
1884 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1885 {
1886         if (fd == -1) {
1887                 pr_debug("Control descriptor is not initialized\n");
1888                 return 0;
1889         }
1890
1891         evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1892                                                      fdarray_flag__nonfilterable);
1893         if (evlist->ctl_fd.pos < 0) {
1894                 evlist->ctl_fd.pos = -1;
1895                 pr_err("Failed to add ctl fd entry: %m\n");
1896                 return -1;
1897         }
1898
1899         evlist->ctl_fd.fd = fd;
1900         evlist->ctl_fd.ack = ack;
1901
1902         return 0;
1903 }
1904
1905 bool evlist__ctlfd_initialized(struct evlist *evlist)
1906 {
1907         return evlist->ctl_fd.pos >= 0;
1908 }
1909
1910 int evlist__finalize_ctlfd(struct evlist *evlist)
1911 {
1912         struct pollfd *entries = evlist->core.pollfd.entries;
1913
1914         if (!evlist__ctlfd_initialized(evlist))
1915                 return 0;
1916
1917         entries[evlist->ctl_fd.pos].fd = -1;
1918         entries[evlist->ctl_fd.pos].events = 0;
1919         entries[evlist->ctl_fd.pos].revents = 0;
1920
1921         evlist->ctl_fd.pos = -1;
1922         evlist->ctl_fd.ack = -1;
1923         evlist->ctl_fd.fd = -1;
1924
1925         return 0;
1926 }
1927
1928 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1929                               char *cmd_data, size_t data_size)
1930 {
1931         int err;
1932         char c;
1933         size_t bytes_read = 0;
1934
1935         *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1936         memset(cmd_data, 0, data_size);
1937         data_size--;
1938
1939         do {
1940                 err = read(evlist->ctl_fd.fd, &c, 1);
1941                 if (err > 0) {
1942                         if (c == '\n' || c == '\0')
1943                                 break;
1944                         cmd_data[bytes_read++] = c;
1945                         if (bytes_read == data_size)
1946                                 break;
1947                         continue;
1948                 } else if (err == -1) {
1949                         if (errno == EINTR)
1950                                 continue;
1951                         if (errno == EAGAIN || errno == EWOULDBLOCK)
1952                                 err = 0;
1953                         else
1954                                 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1955                 }
1956                 break;
1957         } while (1);
1958
1959         pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1960                  bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1961
1962         if (bytes_read > 0) {
1963                 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1964                              (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1965                         *cmd = EVLIST_CTL_CMD_ENABLE;
1966                 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1967                                     (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1968                         *cmd = EVLIST_CTL_CMD_DISABLE;
1969                 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1970                                     (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1971                         *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1972                         pr_debug("is snapshot\n");
1973                 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1974                                     (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1975                         *cmd = EVLIST_CTL_CMD_EVLIST;
1976                 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1977                                     (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1978                         *cmd = EVLIST_CTL_CMD_STOP;
1979                 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
1980                                     (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
1981                         *cmd = EVLIST_CTL_CMD_PING;
1982                 }
1983         }
1984
1985         return bytes_read ? (int)bytes_read : err;
1986 }
1987
1988 int evlist__ctlfd_ack(struct evlist *evlist)
1989 {
1990         int err;
1991
1992         if (evlist->ctl_fd.ack == -1)
1993                 return 0;
1994
1995         err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1996                     sizeof(EVLIST_CTL_CMD_ACK_TAG));
1997         if (err == -1)
1998                 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1999
2000         return err;
2001 }
2002
2003 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2004 {
2005         char *data = cmd_data + cmd_size;
2006
2007         /* no argument */
2008         if (!*data)
2009                 return 0;
2010
2011         /* there's argument */
2012         if (*data == ' ') {
2013                 *arg = data + 1;
2014                 return 1;
2015         }
2016
2017         /* malformed */
2018         return -1;
2019 }
2020
2021 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2022 {
2023         struct evsel *evsel;
2024         char *name;
2025         int err;
2026
2027         err = get_cmd_arg(cmd_data,
2028                           enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2029                                    sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2030                           &name);
2031         if (err < 0) {
2032                 pr_info("failed: wrong command\n");
2033                 return -1;
2034         }
2035
2036         if (err) {
2037                 evsel = evlist__find_evsel_by_str(evlist, name);
2038                 if (evsel) {
2039                         if (enable)
2040                                 evlist__enable_evsel(evlist, name);
2041                         else
2042                                 evlist__disable_evsel(evlist, name);
2043                         pr_info("Event %s %s\n", evsel->name,
2044                                 enable ? "enabled" : "disabled");
2045                 } else {
2046                         pr_info("failed: can't find '%s' event\n", name);
2047                 }
2048         } else {
2049                 if (enable) {
2050                         evlist__enable(evlist);
2051                         pr_info(EVLIST_ENABLED_MSG);
2052                 } else {
2053                         evlist__disable(evlist);
2054                         pr_info(EVLIST_DISABLED_MSG);
2055                 }
2056         }
2057
2058         return 0;
2059 }
2060
2061 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2062 {
2063         struct perf_attr_details details = { .verbose = false, };
2064         struct evsel *evsel;
2065         char *arg;
2066         int err;
2067
2068         err = get_cmd_arg(cmd_data,
2069                           sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2070                           &arg);
2071         if (err < 0) {
2072                 pr_info("failed: wrong command\n");
2073                 return -1;
2074         }
2075
2076         if (err) {
2077                 if (!strcmp(arg, "-v")) {
2078                         details.verbose = true;
2079                 } else if (!strcmp(arg, "-g")) {
2080                         details.event_group = true;
2081                 } else if (!strcmp(arg, "-F")) {
2082                         details.freq = true;
2083                 } else {
2084                         pr_info("failed: wrong command\n");
2085                         return -1;
2086                 }
2087         }
2088
2089         evlist__for_each_entry(evlist, evsel)
2090                 evsel__fprintf(evsel, &details, stderr);
2091
2092         return 0;
2093 }
2094
2095 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2096 {
2097         int err = 0;
2098         char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2099         int ctlfd_pos = evlist->ctl_fd.pos;
2100         struct pollfd *entries = evlist->core.pollfd.entries;
2101
2102         if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2103                 return 0;
2104
2105         if (entries[ctlfd_pos].revents & POLLIN) {
2106                 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2107                                          EVLIST_CTL_CMD_MAX_LEN);
2108                 if (err > 0) {
2109                         switch (*cmd) {
2110                         case EVLIST_CTL_CMD_ENABLE:
2111                         case EVLIST_CTL_CMD_DISABLE:
2112                                 err = evlist__ctlfd_enable(evlist, cmd_data,
2113                                                            *cmd == EVLIST_CTL_CMD_ENABLE);
2114                                 break;
2115                         case EVLIST_CTL_CMD_EVLIST:
2116                                 err = evlist__ctlfd_list(evlist, cmd_data);
2117                                 break;
2118                         case EVLIST_CTL_CMD_SNAPSHOT:
2119                         case EVLIST_CTL_CMD_STOP:
2120                         case EVLIST_CTL_CMD_PING:
2121                                 break;
2122                         case EVLIST_CTL_CMD_ACK:
2123                         case EVLIST_CTL_CMD_UNSUPPORTED:
2124                         default:
2125                                 pr_debug("ctlfd: unsupported %d\n", *cmd);
2126                                 break;
2127                         }
2128                         if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2129                               *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2130                                 evlist__ctlfd_ack(evlist);
2131                 }
2132         }
2133
2134         if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2135                 evlist__finalize_ctlfd(evlist);
2136         else
2137                 entries[ctlfd_pos].revents = 0;
2138
2139         return err;
2140 }
2141
2142 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2143 {
2144         struct evsel *evsel;
2145
2146         evlist__for_each_entry(evlist, evsel) {
2147                 if (evsel->core.idx == idx)
2148                         return evsel;
2149         }
2150         return NULL;
2151 }
2152
2153 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
2154 {
2155         struct evsel *evsel;
2156         int printed = 0;
2157
2158         evlist__for_each_entry(evlist, evsel) {
2159                 if (evsel__is_dummy_event(evsel))
2160                         continue;
2161                 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
2162                         printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
2163                 } else {
2164                         printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
2165                         break;
2166                 }
2167         }
2168
2169         return printed;
2170 }
2171
2172 void evlist__check_mem_load_aux(struct evlist *evlist)
2173 {
2174         struct evsel *leader, *evsel, *pos;
2175
2176         /*
2177          * For some platforms, the 'mem-loads' event is required to use
2178          * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2179          * must be the group leader. Now we disable this group before reporting
2180          * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2181          * any valid memory load information.
2182          */
2183         evlist__for_each_entry(evlist, evsel) {
2184                 leader = evsel__leader(evsel);
2185                 if (leader == evsel)
2186                         continue;
2187
2188                 if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2189                         for_each_group_evsel(pos, leader) {
2190                                 evsel__set_leader(pos, pos);
2191                                 pos->core.nr_members = 0;
2192                         }
2193                 }
2194         }
2195 }