perf top: Fix -z option behavior
[deliverable/linux.git] / tools / perf / util / evlist.c
1 /*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9 #include "util.h"
10 #include <api/fs/debugfs.h>
11 #include <poll.h>
12 #include "cpumap.h"
13 #include "thread_map.h"
14 #include "target.h"
15 #include "evlist.h"
16 #include "evsel.h"
17 #include "debug.h"
18 #include <unistd.h>
19
20 #include "parse-events.h"
21 #include "parse-options.h"
22
23 #include <sys/mman.h>
24
25 #include <linux/bitops.h>
26 #include <linux/hash.h>
27
28 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
29 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
30
31 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
32 struct thread_map *threads)
33 {
34 int i;
35
36 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
37 INIT_HLIST_HEAD(&evlist->heads[i]);
38 INIT_LIST_HEAD(&evlist->entries);
39 perf_evlist__set_maps(evlist, cpus, threads);
40 evlist->workload.pid = -1;
41 }
42
43 struct perf_evlist *perf_evlist__new(void)
44 {
45 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
46
47 if (evlist != NULL)
48 perf_evlist__init(evlist, NULL, NULL);
49
50 return evlist;
51 }
52
53 struct perf_evlist *perf_evlist__new_default(void)
54 {
55 struct perf_evlist *evlist = perf_evlist__new();
56
57 if (evlist && perf_evlist__add_default(evlist)) {
58 perf_evlist__delete(evlist);
59 evlist = NULL;
60 }
61
62 return evlist;
63 }
64
65 /**
66 * perf_evlist__set_id_pos - set the positions of event ids.
67 * @evlist: selected event list
68 *
69 * Events with compatible sample types all have the same id_pos
70 * and is_pos. For convenience, put a copy on evlist.
71 */
72 void perf_evlist__set_id_pos(struct perf_evlist *evlist)
73 {
74 struct perf_evsel *first = perf_evlist__first(evlist);
75
76 evlist->id_pos = first->id_pos;
77 evlist->is_pos = first->is_pos;
78 }
79
80 static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
81 {
82 struct perf_evsel *evsel;
83
84 evlist__for_each(evlist, evsel)
85 perf_evsel__calc_id_pos(evsel);
86
87 perf_evlist__set_id_pos(evlist);
88 }
89
90 static void perf_evlist__purge(struct perf_evlist *evlist)
91 {
92 struct perf_evsel *pos, *n;
93
94 evlist__for_each_safe(evlist, n, pos) {
95 list_del_init(&pos->node);
96 perf_evsel__delete(pos);
97 }
98
99 evlist->nr_entries = 0;
100 }
101
102 void perf_evlist__exit(struct perf_evlist *evlist)
103 {
104 zfree(&evlist->mmap);
105 zfree(&evlist->pollfd);
106 }
107
108 void perf_evlist__delete(struct perf_evlist *evlist)
109 {
110 perf_evlist__munmap(evlist);
111 perf_evlist__close(evlist);
112 cpu_map__delete(evlist->cpus);
113 thread_map__delete(evlist->threads);
114 evlist->cpus = NULL;
115 evlist->threads = NULL;
116 perf_evlist__purge(evlist);
117 perf_evlist__exit(evlist);
118 free(evlist);
119 }
120
121 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
122 {
123 list_add_tail(&entry->node, &evlist->entries);
124 entry->idx = evlist->nr_entries;
125
126 if (!evlist->nr_entries++)
127 perf_evlist__set_id_pos(evlist);
128 }
129
130 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
131 struct list_head *list,
132 int nr_entries)
133 {
134 bool set_id_pos = !evlist->nr_entries;
135
136 list_splice_tail(list, &evlist->entries);
137 evlist->nr_entries += nr_entries;
138 if (set_id_pos)
139 perf_evlist__set_id_pos(evlist);
140 }
141
142 void __perf_evlist__set_leader(struct list_head *list)
143 {
144 struct perf_evsel *evsel, *leader;
145
146 leader = list_entry(list->next, struct perf_evsel, node);
147 evsel = list_entry(list->prev, struct perf_evsel, node);
148
149 leader->nr_members = evsel->idx - leader->idx + 1;
150
151 __evlist__for_each(list, evsel) {
152 evsel->leader = leader;
153 }
154 }
155
156 void perf_evlist__set_leader(struct perf_evlist *evlist)
157 {
158 if (evlist->nr_entries) {
159 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
160 __perf_evlist__set_leader(&evlist->entries);
161 }
162 }
163
164 int perf_evlist__add_default(struct perf_evlist *evlist)
165 {
166 struct perf_event_attr attr = {
167 .type = PERF_TYPE_HARDWARE,
168 .config = PERF_COUNT_HW_CPU_CYCLES,
169 };
170 struct perf_evsel *evsel;
171
172 event_attr_init(&attr);
173
174 evsel = perf_evsel__new(&attr);
175 if (evsel == NULL)
176 goto error;
177
178 /* use strdup() because free(evsel) assumes name is allocated */
179 evsel->name = strdup("cycles");
180 if (!evsel->name)
181 goto error_free;
182
183 perf_evlist__add(evlist, evsel);
184 return 0;
185 error_free:
186 perf_evsel__delete(evsel);
187 error:
188 return -ENOMEM;
189 }
190
191 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
192 struct perf_event_attr *attrs, size_t nr_attrs)
193 {
194 struct perf_evsel *evsel, *n;
195 LIST_HEAD(head);
196 size_t i;
197
198 for (i = 0; i < nr_attrs; i++) {
199 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
200 if (evsel == NULL)
201 goto out_delete_partial_list;
202 list_add_tail(&evsel->node, &head);
203 }
204
205 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
206
207 return 0;
208
209 out_delete_partial_list:
210 __evlist__for_each_safe(&head, n, evsel)
211 perf_evsel__delete(evsel);
212 return -1;
213 }
214
215 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
216 struct perf_event_attr *attrs, size_t nr_attrs)
217 {
218 size_t i;
219
220 for (i = 0; i < nr_attrs; i++)
221 event_attr_init(attrs + i);
222
223 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
224 }
225
226 struct perf_evsel *
227 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
228 {
229 struct perf_evsel *evsel;
230
231 evlist__for_each(evlist, evsel) {
232 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
233 (int)evsel->attr.config == id)
234 return evsel;
235 }
236
237 return NULL;
238 }
239
240 struct perf_evsel *
241 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
242 const char *name)
243 {
244 struct perf_evsel *evsel;
245
246 evlist__for_each(evlist, evsel) {
247 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
248 (strcmp(evsel->name, name) == 0))
249 return evsel;
250 }
251
252 return NULL;
253 }
254
255 int perf_evlist__add_newtp(struct perf_evlist *evlist,
256 const char *sys, const char *name, void *handler)
257 {
258 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
259
260 if (evsel == NULL)
261 return -1;
262
263 evsel->handler = handler;
264 perf_evlist__add(evlist, evsel);
265 return 0;
266 }
267
268 void perf_evlist__disable(struct perf_evlist *evlist)
269 {
270 int cpu, thread;
271 struct perf_evsel *pos;
272 int nr_cpus = cpu_map__nr(evlist->cpus);
273 int nr_threads = thread_map__nr(evlist->threads);
274
275 for (cpu = 0; cpu < nr_cpus; cpu++) {
276 evlist__for_each(evlist, pos) {
277 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
278 continue;
279 for (thread = 0; thread < nr_threads; thread++)
280 ioctl(FD(pos, cpu, thread),
281 PERF_EVENT_IOC_DISABLE, 0);
282 }
283 }
284 }
285
286 void perf_evlist__enable(struct perf_evlist *evlist)
287 {
288 int cpu, thread;
289 struct perf_evsel *pos;
290 int nr_cpus = cpu_map__nr(evlist->cpus);
291 int nr_threads = thread_map__nr(evlist->threads);
292
293 for (cpu = 0; cpu < nr_cpus; cpu++) {
294 evlist__for_each(evlist, pos) {
295 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
296 continue;
297 for (thread = 0; thread < nr_threads; thread++)
298 ioctl(FD(pos, cpu, thread),
299 PERF_EVENT_IOC_ENABLE, 0);
300 }
301 }
302 }
303
304 int perf_evlist__disable_event(struct perf_evlist *evlist,
305 struct perf_evsel *evsel)
306 {
307 int cpu, thread, err;
308
309 if (!evsel->fd)
310 return 0;
311
312 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
313 for (thread = 0; thread < evlist->threads->nr; thread++) {
314 err = ioctl(FD(evsel, cpu, thread),
315 PERF_EVENT_IOC_DISABLE, 0);
316 if (err)
317 return err;
318 }
319 }
320 return 0;
321 }
322
323 int perf_evlist__enable_event(struct perf_evlist *evlist,
324 struct perf_evsel *evsel)
325 {
326 int cpu, thread, err;
327
328 if (!evsel->fd)
329 return -EINVAL;
330
331 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
332 for (thread = 0; thread < evlist->threads->nr; thread++) {
333 err = ioctl(FD(evsel, cpu, thread),
334 PERF_EVENT_IOC_ENABLE, 0);
335 if (err)
336 return err;
337 }
338 }
339 return 0;
340 }
341
342 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
343 {
344 int nr_cpus = cpu_map__nr(evlist->cpus);
345 int nr_threads = thread_map__nr(evlist->threads);
346 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
347 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
348 return evlist->pollfd != NULL ? 0 : -ENOMEM;
349 }
350
351 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
352 {
353 fcntl(fd, F_SETFL, O_NONBLOCK);
354 evlist->pollfd[evlist->nr_fds].fd = fd;
355 evlist->pollfd[evlist->nr_fds].events = POLLIN;
356 evlist->nr_fds++;
357 }
358
359 static void perf_evlist__id_hash(struct perf_evlist *evlist,
360 struct perf_evsel *evsel,
361 int cpu, int thread, u64 id)
362 {
363 int hash;
364 struct perf_sample_id *sid = SID(evsel, cpu, thread);
365
366 sid->id = id;
367 sid->evsel = evsel;
368 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
369 hlist_add_head(&sid->node, &evlist->heads[hash]);
370 }
371
372 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
373 int cpu, int thread, u64 id)
374 {
375 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
376 evsel->id[evsel->ids++] = id;
377 }
378
379 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
380 struct perf_evsel *evsel,
381 int cpu, int thread, int fd)
382 {
383 u64 read_data[4] = { 0, };
384 int id_idx = 1; /* The first entry is the counter value */
385 u64 id;
386 int ret;
387
388 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
389 if (!ret)
390 goto add;
391
392 if (errno != ENOTTY)
393 return -1;
394
395 /* Legacy way to get event id.. All hail to old kernels! */
396
397 /*
398 * This way does not work with group format read, so bail
399 * out in that case.
400 */
401 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
402 return -1;
403
404 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
405 read(fd, &read_data, sizeof(read_data)) == -1)
406 return -1;
407
408 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
409 ++id_idx;
410 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
411 ++id_idx;
412
413 id = read_data[id_idx];
414
415 add:
416 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
417 return 0;
418 }
419
420 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
421 {
422 struct hlist_head *head;
423 struct perf_sample_id *sid;
424 int hash;
425
426 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
427 head = &evlist->heads[hash];
428
429 hlist_for_each_entry(sid, head, node)
430 if (sid->id == id)
431 return sid;
432
433 return NULL;
434 }
435
436 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
437 {
438 struct perf_sample_id *sid;
439
440 if (evlist->nr_entries == 1)
441 return perf_evlist__first(evlist);
442
443 sid = perf_evlist__id2sid(evlist, id);
444 if (sid)
445 return sid->evsel;
446
447 if (!perf_evlist__sample_id_all(evlist))
448 return perf_evlist__first(evlist);
449
450 return NULL;
451 }
452
453 static int perf_evlist__event2id(struct perf_evlist *evlist,
454 union perf_event *event, u64 *id)
455 {
456 const u64 *array = event->sample.array;
457 ssize_t n;
458
459 n = (event->header.size - sizeof(event->header)) >> 3;
460
461 if (event->header.type == PERF_RECORD_SAMPLE) {
462 if (evlist->id_pos >= n)
463 return -1;
464 *id = array[evlist->id_pos];
465 } else {
466 if (evlist->is_pos > n)
467 return -1;
468 n -= evlist->is_pos;
469 *id = array[n];
470 }
471 return 0;
472 }
473
474 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
475 union perf_event *event)
476 {
477 struct perf_evsel *first = perf_evlist__first(evlist);
478 struct hlist_head *head;
479 struct perf_sample_id *sid;
480 int hash;
481 u64 id;
482
483 if (evlist->nr_entries == 1)
484 return first;
485
486 if (!first->attr.sample_id_all &&
487 event->header.type != PERF_RECORD_SAMPLE)
488 return first;
489
490 if (perf_evlist__event2id(evlist, event, &id))
491 return NULL;
492
493 /* Synthesized events have an id of zero */
494 if (!id)
495 return first;
496
497 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
498 head = &evlist->heads[hash];
499
500 hlist_for_each_entry(sid, head, node) {
501 if (sid->id == id)
502 return sid->evsel;
503 }
504 return NULL;
505 }
506
507 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
508 {
509 struct perf_mmap *md = &evlist->mmap[idx];
510 unsigned int head = perf_mmap__read_head(md);
511 unsigned int old = md->prev;
512 unsigned char *data = md->base + page_size;
513 union perf_event *event = NULL;
514
515 if (evlist->overwrite) {
516 /*
517 * If we're further behind than half the buffer, there's a chance
518 * the writer will bite our tail and mess up the samples under us.
519 *
520 * If we somehow ended up ahead of the head, we got messed up.
521 *
522 * In either case, truncate and restart at head.
523 */
524 int diff = head - old;
525 if (diff > md->mask / 2 || diff < 0) {
526 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
527
528 /*
529 * head points to a known good entry, start there.
530 */
531 old = head;
532 }
533 }
534
535 if (old != head) {
536 size_t size;
537
538 event = (union perf_event *)&data[old & md->mask];
539 size = event->header.size;
540
541 /*
542 * Event straddles the mmap boundary -- header should always
543 * be inside due to u64 alignment of output.
544 */
545 if ((old & md->mask) + size != ((old + size) & md->mask)) {
546 unsigned int offset = old;
547 unsigned int len = min(sizeof(*event), size), cpy;
548 void *dst = md->event_copy;
549
550 do {
551 cpy = min(md->mask + 1 - (offset & md->mask), len);
552 memcpy(dst, &data[offset & md->mask], cpy);
553 offset += cpy;
554 dst += cpy;
555 len -= cpy;
556 } while (len);
557
558 event = (union perf_event *) md->event_copy;
559 }
560
561 old += size;
562 }
563
564 md->prev = old;
565
566 return event;
567 }
568
569 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
570 {
571 if (!evlist->overwrite) {
572 struct perf_mmap *md = &evlist->mmap[idx];
573 unsigned int old = md->prev;
574
575 perf_mmap__write_tail(md, old);
576 }
577 }
578
579 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
580 {
581 if (evlist->mmap[idx].base != NULL) {
582 munmap(evlist->mmap[idx].base, evlist->mmap_len);
583 evlist->mmap[idx].base = NULL;
584 }
585 }
586
587 void perf_evlist__munmap(struct perf_evlist *evlist)
588 {
589 int i;
590
591 if (evlist->mmap == NULL)
592 return;
593
594 for (i = 0; i < evlist->nr_mmaps; i++)
595 __perf_evlist__munmap(evlist, i);
596
597 zfree(&evlist->mmap);
598 }
599
600 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
601 {
602 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
603 if (cpu_map__empty(evlist->cpus))
604 evlist->nr_mmaps = thread_map__nr(evlist->threads);
605 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
606 return evlist->mmap != NULL ? 0 : -ENOMEM;
607 }
608
609 struct mmap_params {
610 int prot;
611 int mask;
612 };
613
614 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
615 struct mmap_params *mp, int fd)
616 {
617 evlist->mmap[idx].prev = 0;
618 evlist->mmap[idx].mask = mp->mask;
619 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
620 MAP_SHARED, fd, 0);
621 if (evlist->mmap[idx].base == MAP_FAILED) {
622 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
623 errno);
624 evlist->mmap[idx].base = NULL;
625 return -1;
626 }
627
628 perf_evlist__add_pollfd(evlist, fd);
629 return 0;
630 }
631
632 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
633 struct mmap_params *mp, int cpu,
634 int thread, int *output)
635 {
636 struct perf_evsel *evsel;
637
638 evlist__for_each(evlist, evsel) {
639 int fd = FD(evsel, cpu, thread);
640
641 if (*output == -1) {
642 *output = fd;
643 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
644 return -1;
645 } else {
646 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
647 return -1;
648 }
649
650 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
651 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
652 return -1;
653 }
654
655 return 0;
656 }
657
658 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
659 struct mmap_params *mp)
660 {
661 int cpu, thread;
662 int nr_cpus = cpu_map__nr(evlist->cpus);
663 int nr_threads = thread_map__nr(evlist->threads);
664
665 pr_debug2("perf event ring buffer mmapped per cpu\n");
666 for (cpu = 0; cpu < nr_cpus; cpu++) {
667 int output = -1;
668
669 for (thread = 0; thread < nr_threads; thread++) {
670 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
671 thread, &output))
672 goto out_unmap;
673 }
674 }
675
676 return 0;
677
678 out_unmap:
679 for (cpu = 0; cpu < nr_cpus; cpu++)
680 __perf_evlist__munmap(evlist, cpu);
681 return -1;
682 }
683
684 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
685 struct mmap_params *mp)
686 {
687 int thread;
688 int nr_threads = thread_map__nr(evlist->threads);
689
690 pr_debug2("perf event ring buffer mmapped per thread\n");
691 for (thread = 0; thread < nr_threads; thread++) {
692 int output = -1;
693
694 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
695 &output))
696 goto out_unmap;
697 }
698
699 return 0;
700
701 out_unmap:
702 for (thread = 0; thread < nr_threads; thread++)
703 __perf_evlist__munmap(evlist, thread);
704 return -1;
705 }
706
707 static size_t perf_evlist__mmap_size(unsigned long pages)
708 {
709 /* 512 kiB: default amount of unprivileged mlocked memory */
710 if (pages == UINT_MAX)
711 pages = (512 * 1024) / page_size;
712 else if (!is_power_of_2(pages))
713 return 0;
714
715 return (pages + 1) * page_size;
716 }
717
718 static long parse_pages_arg(const char *str, unsigned long min,
719 unsigned long max)
720 {
721 unsigned long pages, val;
722 static struct parse_tag tags[] = {
723 { .tag = 'B', .mult = 1 },
724 { .tag = 'K', .mult = 1 << 10 },
725 { .tag = 'M', .mult = 1 << 20 },
726 { .tag = 'G', .mult = 1 << 30 },
727 { .tag = 0 },
728 };
729
730 if (str == NULL)
731 return -EINVAL;
732
733 val = parse_tag_value(str, tags);
734 if (val != (unsigned long) -1) {
735 /* we got file size value */
736 pages = PERF_ALIGN(val, page_size) / page_size;
737 } else {
738 /* we got pages count value */
739 char *eptr;
740 pages = strtoul(str, &eptr, 10);
741 if (*eptr != '\0')
742 return -EINVAL;
743 }
744
745 if (pages == 0 && min == 0) {
746 /* leave number of pages at 0 */
747 } else if (!is_power_of_2(pages)) {
748 /* round pages up to next power of 2 */
749 pages = next_pow2_l(pages);
750 if (!pages)
751 return -EINVAL;
752 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
753 pages * page_size, pages);
754 }
755
756 if (pages > max)
757 return -EINVAL;
758
759 return pages;
760 }
761
762 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
763 int unset __maybe_unused)
764 {
765 unsigned int *mmap_pages = opt->value;
766 unsigned long max = UINT_MAX;
767 long pages;
768
769 if (max > SIZE_MAX / page_size)
770 max = SIZE_MAX / page_size;
771
772 pages = parse_pages_arg(str, 1, max);
773 if (pages < 0) {
774 pr_err("Invalid argument for --mmap_pages/-m\n");
775 return -1;
776 }
777
778 *mmap_pages = pages;
779 return 0;
780 }
781
782 /**
783 * perf_evlist__mmap - Create mmaps to receive events.
784 * @evlist: list of events
785 * @pages: map length in pages
786 * @overwrite: overwrite older events?
787 *
788 * If @overwrite is %false the user needs to signal event consumption using
789 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
790 * automatically.
791 *
792 * Return: %0 on success, negative error code otherwise.
793 */
794 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
795 bool overwrite)
796 {
797 struct perf_evsel *evsel;
798 const struct cpu_map *cpus = evlist->cpus;
799 const struct thread_map *threads = evlist->threads;
800 struct mmap_params mp = {
801 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
802 };
803
804 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
805 return -ENOMEM;
806
807 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
808 return -ENOMEM;
809
810 evlist->overwrite = overwrite;
811 evlist->mmap_len = perf_evlist__mmap_size(pages);
812 pr_debug("mmap size %zuB\n", evlist->mmap_len);
813 mp.mask = evlist->mmap_len - page_size - 1;
814
815 evlist__for_each(evlist, evsel) {
816 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
817 evsel->sample_id == NULL &&
818 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
819 return -ENOMEM;
820 }
821
822 if (cpu_map__empty(cpus))
823 return perf_evlist__mmap_per_thread(evlist, &mp);
824
825 return perf_evlist__mmap_per_cpu(evlist, &mp);
826 }
827
828 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
829 {
830 evlist->threads = thread_map__new_str(target->pid, target->tid,
831 target->uid);
832
833 if (evlist->threads == NULL)
834 return -1;
835
836 if (target__uses_dummy_map(target))
837 evlist->cpus = cpu_map__dummy_new();
838 else
839 evlist->cpus = cpu_map__new(target->cpu_list);
840
841 if (evlist->cpus == NULL)
842 goto out_delete_threads;
843
844 return 0;
845
846 out_delete_threads:
847 thread_map__delete(evlist->threads);
848 return -1;
849 }
850
851 int perf_evlist__apply_filters(struct perf_evlist *evlist)
852 {
853 struct perf_evsel *evsel;
854 int err = 0;
855 const int ncpus = cpu_map__nr(evlist->cpus),
856 nthreads = thread_map__nr(evlist->threads);
857
858 evlist__for_each(evlist, evsel) {
859 if (evsel->filter == NULL)
860 continue;
861
862 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
863 if (err)
864 break;
865 }
866
867 return err;
868 }
869
870 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
871 {
872 struct perf_evsel *evsel;
873 int err = 0;
874 const int ncpus = cpu_map__nr(evlist->cpus),
875 nthreads = thread_map__nr(evlist->threads);
876
877 evlist__for_each(evlist, evsel) {
878 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
879 if (err)
880 break;
881 }
882
883 return err;
884 }
885
886 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
887 {
888 struct perf_evsel *pos;
889
890 if (evlist->nr_entries == 1)
891 return true;
892
893 if (evlist->id_pos < 0 || evlist->is_pos < 0)
894 return false;
895
896 evlist__for_each(evlist, pos) {
897 if (pos->id_pos != evlist->id_pos ||
898 pos->is_pos != evlist->is_pos)
899 return false;
900 }
901
902 return true;
903 }
904
905 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
906 {
907 struct perf_evsel *evsel;
908
909 if (evlist->combined_sample_type)
910 return evlist->combined_sample_type;
911
912 evlist__for_each(evlist, evsel)
913 evlist->combined_sample_type |= evsel->attr.sample_type;
914
915 return evlist->combined_sample_type;
916 }
917
918 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
919 {
920 evlist->combined_sample_type = 0;
921 return __perf_evlist__combined_sample_type(evlist);
922 }
923
924 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
925 {
926 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
927 u64 read_format = first->attr.read_format;
928 u64 sample_type = first->attr.sample_type;
929
930 evlist__for_each(evlist, pos) {
931 if (read_format != pos->attr.read_format)
932 return false;
933 }
934
935 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
936 if ((sample_type & PERF_SAMPLE_READ) &&
937 !(read_format & PERF_FORMAT_ID)) {
938 return false;
939 }
940
941 return true;
942 }
943
944 u64 perf_evlist__read_format(struct perf_evlist *evlist)
945 {
946 struct perf_evsel *first = perf_evlist__first(evlist);
947 return first->attr.read_format;
948 }
949
950 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
951 {
952 struct perf_evsel *first = perf_evlist__first(evlist);
953 struct perf_sample *data;
954 u64 sample_type;
955 u16 size = 0;
956
957 if (!first->attr.sample_id_all)
958 goto out;
959
960 sample_type = first->attr.sample_type;
961
962 if (sample_type & PERF_SAMPLE_TID)
963 size += sizeof(data->tid) * 2;
964
965 if (sample_type & PERF_SAMPLE_TIME)
966 size += sizeof(data->time);
967
968 if (sample_type & PERF_SAMPLE_ID)
969 size += sizeof(data->id);
970
971 if (sample_type & PERF_SAMPLE_STREAM_ID)
972 size += sizeof(data->stream_id);
973
974 if (sample_type & PERF_SAMPLE_CPU)
975 size += sizeof(data->cpu) * 2;
976
977 if (sample_type & PERF_SAMPLE_IDENTIFIER)
978 size += sizeof(data->id);
979 out:
980 return size;
981 }
982
983 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
984 {
985 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
986
987 evlist__for_each_continue(evlist, pos) {
988 if (first->attr.sample_id_all != pos->attr.sample_id_all)
989 return false;
990 }
991
992 return true;
993 }
994
995 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
996 {
997 struct perf_evsel *first = perf_evlist__first(evlist);
998 return first->attr.sample_id_all;
999 }
1000
1001 void perf_evlist__set_selected(struct perf_evlist *evlist,
1002 struct perf_evsel *evsel)
1003 {
1004 evlist->selected = evsel;
1005 }
1006
1007 void perf_evlist__close(struct perf_evlist *evlist)
1008 {
1009 struct perf_evsel *evsel;
1010 int ncpus = cpu_map__nr(evlist->cpus);
1011 int nthreads = thread_map__nr(evlist->threads);
1012 int n;
1013
1014 evlist__for_each_reverse(evlist, evsel) {
1015 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1016 perf_evsel__close(evsel, n, nthreads);
1017 }
1018 }
1019
1020 int perf_evlist__open(struct perf_evlist *evlist)
1021 {
1022 struct perf_evsel *evsel;
1023 int err;
1024
1025 perf_evlist__update_id_pos(evlist);
1026
1027 evlist__for_each(evlist, evsel) {
1028 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1029 if (err < 0)
1030 goto out_err;
1031 }
1032
1033 return 0;
1034 out_err:
1035 perf_evlist__close(evlist);
1036 errno = -err;
1037 return err;
1038 }
1039
1040 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1041 const char *argv[], bool pipe_output,
1042 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1043 {
1044 int child_ready_pipe[2], go_pipe[2];
1045 char bf;
1046
1047 if (pipe(child_ready_pipe) < 0) {
1048 perror("failed to create 'ready' pipe");
1049 return -1;
1050 }
1051
1052 if (pipe(go_pipe) < 0) {
1053 perror("failed to create 'go' pipe");
1054 goto out_close_ready_pipe;
1055 }
1056
1057 evlist->workload.pid = fork();
1058 if (evlist->workload.pid < 0) {
1059 perror("failed to fork");
1060 goto out_close_pipes;
1061 }
1062
1063 if (!evlist->workload.pid) {
1064 int ret;
1065
1066 if (pipe_output)
1067 dup2(2, 1);
1068
1069 signal(SIGTERM, SIG_DFL);
1070
1071 close(child_ready_pipe[0]);
1072 close(go_pipe[1]);
1073 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1074
1075 /*
1076 * Tell the parent we're ready to go
1077 */
1078 close(child_ready_pipe[1]);
1079
1080 /*
1081 * Wait until the parent tells us to go.
1082 */
1083 ret = read(go_pipe[0], &bf, 1);
1084 /*
1085 * The parent will ask for the execvp() to be performed by
1086 * writing exactly one byte, in workload.cork_fd, usually via
1087 * perf_evlist__start_workload().
1088 *
1089 * For cancelling the workload without actuallin running it,
1090 * the parent will just close workload.cork_fd, without writing
1091 * anything, i.e. read will return zero and we just exit()
1092 * here.
1093 */
1094 if (ret != 1) {
1095 if (ret == -1)
1096 perror("unable to read pipe");
1097 exit(ret);
1098 }
1099
1100 execvp(argv[0], (char **)argv);
1101
1102 if (exec_error) {
1103 union sigval val;
1104
1105 val.sival_int = errno;
1106 if (sigqueue(getppid(), SIGUSR1, val))
1107 perror(argv[0]);
1108 } else
1109 perror(argv[0]);
1110 exit(-1);
1111 }
1112
1113 if (exec_error) {
1114 struct sigaction act = {
1115 .sa_flags = SA_SIGINFO,
1116 .sa_sigaction = exec_error,
1117 };
1118 sigaction(SIGUSR1, &act, NULL);
1119 }
1120
1121 if (target__none(target))
1122 evlist->threads->map[0] = evlist->workload.pid;
1123
1124 close(child_ready_pipe[1]);
1125 close(go_pipe[0]);
1126 /*
1127 * wait for child to settle
1128 */
1129 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1130 perror("unable to read pipe");
1131 goto out_close_pipes;
1132 }
1133
1134 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1135 evlist->workload.cork_fd = go_pipe[1];
1136 close(child_ready_pipe[0]);
1137 return 0;
1138
1139 out_close_pipes:
1140 close(go_pipe[0]);
1141 close(go_pipe[1]);
1142 out_close_ready_pipe:
1143 close(child_ready_pipe[0]);
1144 close(child_ready_pipe[1]);
1145 return -1;
1146 }
1147
1148 int perf_evlist__start_workload(struct perf_evlist *evlist)
1149 {
1150 if (evlist->workload.cork_fd > 0) {
1151 char bf = 0;
1152 int ret;
1153 /*
1154 * Remove the cork, let it rip!
1155 */
1156 ret = write(evlist->workload.cork_fd, &bf, 1);
1157 if (ret < 0)
1158 perror("enable to write to pipe");
1159
1160 close(evlist->workload.cork_fd);
1161 return ret;
1162 }
1163
1164 return 0;
1165 }
1166
1167 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1168 struct perf_sample *sample)
1169 {
1170 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1171
1172 if (!evsel)
1173 return -EFAULT;
1174 return perf_evsel__parse_sample(evsel, event, sample);
1175 }
1176
1177 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1178 {
1179 struct perf_evsel *evsel;
1180 size_t printed = 0;
1181
1182 evlist__for_each(evlist, evsel) {
1183 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1184 perf_evsel__name(evsel));
1185 }
1186
1187 return printed + fprintf(fp, "\n");
1188 }
1189
1190 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1191 int err, char *buf, size_t size)
1192 {
1193 char sbuf[128];
1194
1195 switch (err) {
1196 case ENOENT:
1197 scnprintf(buf, size, "%s",
1198 "Error:\tUnable to find debugfs\n"
1199 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1200 "Hint:\tIs the debugfs filesystem mounted?\n"
1201 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1202 break;
1203 case EACCES:
1204 scnprintf(buf, size,
1205 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1206 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1207 debugfs_mountpoint, debugfs_mountpoint);
1208 break;
1209 default:
1210 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1211 break;
1212 }
1213
1214 return 0;
1215 }
1216
1217 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1218 int err, char *buf, size_t size)
1219 {
1220 int printed, value;
1221 char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1222
1223 switch (err) {
1224 case EACCES:
1225 case EPERM:
1226 printed = scnprintf(buf, size,
1227 "Error:\t%s.\n"
1228 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1229
1230 value = perf_event_paranoid();
1231
1232 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1233
1234 if (value >= 2) {
1235 printed += scnprintf(buf + printed, size - printed,
1236 "For your workloads it needs to be <= 1\nHint:\t");
1237 }
1238 printed += scnprintf(buf + printed, size - printed,
1239 "For system wide tracing it needs to be set to -1.\n");
1240
1241 printed += scnprintf(buf + printed, size - printed,
1242 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1243 "Hint:\tThe current value is %d.", value);
1244 break;
1245 default:
1246 scnprintf(buf, size, "%s", emsg);
1247 break;
1248 }
1249
1250 return 0;
1251 }
1252
1253 void perf_evlist__to_front(struct perf_evlist *evlist,
1254 struct perf_evsel *move_evsel)
1255 {
1256 struct perf_evsel *evsel, *n;
1257 LIST_HEAD(move);
1258
1259 if (move_evsel == perf_evlist__first(evlist))
1260 return;
1261
1262 evlist__for_each_safe(evlist, n, evsel) {
1263 if (evsel->leader == move_evsel->leader)
1264 list_move_tail(&evsel->node, &move);
1265 }
1266
1267 list_splice(&move, &evlist->entries);
1268 }
This page took 0.058018 seconds and 5 git commands to generate.