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