2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
13 #include "thread_map.h"
18 #include "parse-events.h"
22 #include <linux/bitops.h>
23 #include <linux/hash.h>
25 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
26 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
28 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
29 struct thread_map *threads)
33 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
34 INIT_HLIST_HEAD(&evlist->heads[i]);
35 INIT_LIST_HEAD(&evlist->entries);
36 perf_evlist__set_maps(evlist, cpus, threads);
37 evlist->workload.pid = -1;
40 struct perf_evlist *perf_evlist__new(struct cpu_map *cpus,
41 struct thread_map *threads)
43 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
46 perf_evlist__init(evlist, cpus, threads);
51 void perf_evlist__config_attrs(struct perf_evlist *evlist,
52 struct perf_record_opts *opts)
54 struct perf_evsel *evsel;
56 if (evlist->cpus->map[0] < 0)
57 opts->no_inherit = true;
59 list_for_each_entry(evsel, &evlist->entries, node) {
60 perf_evsel__config(evsel, opts);
62 if (evlist->nr_entries > 1)
63 evsel->attr.sample_type |= PERF_SAMPLE_ID;
67 static void perf_evlist__purge(struct perf_evlist *evlist)
69 struct perf_evsel *pos, *n;
71 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
72 list_del_init(&pos->node);
73 perf_evsel__delete(pos);
76 evlist->nr_entries = 0;
79 void perf_evlist__exit(struct perf_evlist *evlist)
84 evlist->pollfd = NULL;
87 void perf_evlist__delete(struct perf_evlist *evlist)
89 perf_evlist__purge(evlist);
90 perf_evlist__exit(evlist);
94 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
96 list_add_tail(&entry->node, &evlist->entries);
100 static void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
101 struct list_head *list,
104 list_splice_tail(list, &evlist->entries);
105 evlist->nr_entries += nr_entries;
108 int perf_evlist__add_default(struct perf_evlist *evlist)
110 struct perf_event_attr attr = {
111 .type = PERF_TYPE_HARDWARE,
112 .config = PERF_COUNT_HW_CPU_CYCLES,
114 struct perf_evsel *evsel = perf_evsel__new(&attr, 0);
119 /* use strdup() because free(evsel) assumes name is allocated */
120 evsel->name = strdup("cycles");
124 perf_evlist__add(evlist, evsel);
127 perf_evsel__delete(evsel);
132 int perf_evlist__add_attrs(struct perf_evlist *evlist,
133 struct perf_event_attr *attrs, size_t nr_attrs)
135 struct perf_evsel *evsel, *n;
139 for (i = 0; i < nr_attrs; i++) {
140 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
142 goto out_delete_partial_list;
143 list_add_tail(&evsel->node, &head);
146 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
150 out_delete_partial_list:
151 list_for_each_entry_safe(evsel, n, &head, node)
152 perf_evsel__delete(evsel);
156 static int trace_event__id(const char *evname)
158 char *filename, *colon;
161 if (asprintf(&filename, "%s/%s/id", tracing_events_path, evname) < 0)
164 colon = strrchr(filename, ':');
168 fd = open(filename, O_RDONLY);
171 if (read(fd, id, sizeof(id)) > 0)
180 int perf_evlist__add_tracepoints(struct perf_evlist *evlist,
181 const char *tracepoints[],
182 size_t nr_tracepoints)
186 struct perf_event_attr *attrs = zalloc(nr_tracepoints * sizeof(*attrs));
191 for (i = 0; i < nr_tracepoints; i++) {
192 err = trace_event__id(tracepoints[i]);
197 attrs[i].type = PERF_TYPE_TRACEPOINT;
198 attrs[i].config = err;
199 attrs[i].sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
201 attrs[i].sample_period = 1;
204 err = perf_evlist__add_attrs(evlist, attrs, nr_tracepoints);
210 static struct perf_evsel *
211 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
213 struct perf_evsel *evsel;
215 list_for_each_entry(evsel, &evlist->entries, node) {
216 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
217 (int)evsel->attr.config == id)
224 int perf_evlist__set_tracepoints_handlers(struct perf_evlist *evlist,
225 const struct perf_evsel_str_handler *assocs,
228 struct perf_evsel *evsel;
232 for (i = 0; i < nr_assocs; i++) {
233 err = trace_event__id(assocs[i].name);
237 evsel = perf_evlist__find_tracepoint_by_id(evlist, err);
242 if (evsel->handler.func != NULL)
244 evsel->handler.func = assocs[i].handler;
252 void perf_evlist__disable(struct perf_evlist *evlist)
255 struct perf_evsel *pos;
257 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
258 list_for_each_entry(pos, &evlist->entries, node) {
259 for (thread = 0; thread < evlist->threads->nr; thread++)
260 ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_DISABLE);
265 void perf_evlist__enable(struct perf_evlist *evlist)
268 struct perf_evsel *pos;
270 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
271 list_for_each_entry(pos, &evlist->entries, node) {
272 for (thread = 0; thread < evlist->threads->nr; thread++)
273 ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_ENABLE);
278 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
280 int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries;
281 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
282 return evlist->pollfd != NULL ? 0 : -ENOMEM;
285 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
287 fcntl(fd, F_SETFL, O_NONBLOCK);
288 evlist->pollfd[evlist->nr_fds].fd = fd;
289 evlist->pollfd[evlist->nr_fds].events = POLLIN;
293 static void perf_evlist__id_hash(struct perf_evlist *evlist,
294 struct perf_evsel *evsel,
295 int cpu, int thread, u64 id)
298 struct perf_sample_id *sid = SID(evsel, cpu, thread);
302 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
303 hlist_add_head(&sid->node, &evlist->heads[hash]);
306 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
307 int cpu, int thread, u64 id)
309 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
310 evsel->id[evsel->ids++] = id;
313 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
314 struct perf_evsel *evsel,
315 int cpu, int thread, int fd)
317 u64 read_data[4] = { 0, };
318 int id_idx = 1; /* The first entry is the counter value */
320 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
321 read(fd, &read_data, sizeof(read_data)) == -1)
324 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
326 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
329 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
333 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
335 struct hlist_head *head;
336 struct hlist_node *pos;
337 struct perf_sample_id *sid;
340 if (evlist->nr_entries == 1)
341 return list_entry(evlist->entries.next, struct perf_evsel, node);
343 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
344 head = &evlist->heads[hash];
346 hlist_for_each_entry(sid, pos, head, node)
352 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
354 /* XXX Move this to perf.c, making it generally available */
355 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
356 struct perf_mmap *md = &evlist->mmap[idx];
357 unsigned int head = perf_mmap__read_head(md);
358 unsigned int old = md->prev;
359 unsigned char *data = md->base + page_size;
360 union perf_event *event = NULL;
362 if (evlist->overwrite) {
364 * If we're further behind than half the buffer, there's a chance
365 * the writer will bite our tail and mess up the samples under us.
367 * If we somehow ended up ahead of the head, we got messed up.
369 * In either case, truncate and restart at head.
371 int diff = head - old;
372 if (diff > md->mask / 2 || diff < 0) {
373 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
376 * head points to a known good entry, start there.
385 event = (union perf_event *)&data[old & md->mask];
386 size = event->header.size;
389 * Event straddles the mmap boundary -- header should always
390 * be inside due to u64 alignment of output.
392 if ((old & md->mask) + size != ((old + size) & md->mask)) {
393 unsigned int offset = old;
394 unsigned int len = min(sizeof(*event), size), cpy;
395 void *dst = &evlist->event_copy;
398 cpy = min(md->mask + 1 - (offset & md->mask), len);
399 memcpy(dst, &data[offset & md->mask], cpy);
405 event = &evlist->event_copy;
413 if (!evlist->overwrite)
414 perf_mmap__write_tail(md, old);
419 void perf_evlist__munmap(struct perf_evlist *evlist)
423 for (i = 0; i < evlist->nr_mmaps; i++) {
424 if (evlist->mmap[i].base != NULL) {
425 munmap(evlist->mmap[i].base, evlist->mmap_len);
426 evlist->mmap[i].base = NULL;
434 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
436 evlist->nr_mmaps = evlist->cpus->nr;
437 if (evlist->cpus->map[0] == -1)
438 evlist->nr_mmaps = evlist->threads->nr;
439 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
440 return evlist->mmap != NULL ? 0 : -ENOMEM;
443 static int __perf_evlist__mmap(struct perf_evlist *evlist,
444 int idx, int prot, int mask, int fd)
446 evlist->mmap[idx].prev = 0;
447 evlist->mmap[idx].mask = mask;
448 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
450 if (evlist->mmap[idx].base == MAP_FAILED)
453 perf_evlist__add_pollfd(evlist, fd);
457 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
459 struct perf_evsel *evsel;
462 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
465 for (thread = 0; thread < evlist->threads->nr; thread++) {
466 list_for_each_entry(evsel, &evlist->entries, node) {
467 int fd = FD(evsel, cpu, thread);
471 if (__perf_evlist__mmap(evlist, cpu,
472 prot, mask, output) < 0)
475 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
479 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
480 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
489 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
490 if (evlist->mmap[cpu].base != NULL) {
491 munmap(evlist->mmap[cpu].base, evlist->mmap_len);
492 evlist->mmap[cpu].base = NULL;
498 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
500 struct perf_evsel *evsel;
503 for (thread = 0; thread < evlist->threads->nr; thread++) {
506 list_for_each_entry(evsel, &evlist->entries, node) {
507 int fd = FD(evsel, 0, thread);
511 if (__perf_evlist__mmap(evlist, thread,
512 prot, mask, output) < 0)
515 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
519 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
520 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
528 for (thread = 0; thread < evlist->threads->nr; thread++) {
529 if (evlist->mmap[thread].base != NULL) {
530 munmap(evlist->mmap[thread].base, evlist->mmap_len);
531 evlist->mmap[thread].base = NULL;
537 /** perf_evlist__mmap - Create per cpu maps to receive events
539 * @evlist - list of events
540 * @pages - map length in pages
541 * @overwrite - overwrite older events?
543 * If overwrite is false the user needs to signal event consuption using:
545 * struct perf_mmap *m = &evlist->mmap[cpu];
546 * unsigned int head = perf_mmap__read_head(m);
548 * perf_mmap__write_tail(m, head)
550 * Using perf_evlist__read_on_cpu does this automatically.
552 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
555 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
556 struct perf_evsel *evsel;
557 const struct cpu_map *cpus = evlist->cpus;
558 const struct thread_map *threads = evlist->threads;
559 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
561 /* 512 kiB: default amount of unprivileged mlocked memory */
562 if (pages == UINT_MAX)
563 pages = (512 * 1024) / page_size;
565 mask = pages * page_size - 1;
567 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
570 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
573 evlist->overwrite = overwrite;
574 evlist->mmap_len = (pages + 1) * page_size;
576 list_for_each_entry(evsel, &evlist->entries, node) {
577 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
578 evsel->sample_id == NULL &&
579 perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
583 if (evlist->cpus->map[0] == -1)
584 return perf_evlist__mmap_per_thread(evlist, prot, mask);
586 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
589 int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid,
590 pid_t target_tid, const char *cpu_list)
592 evlist->threads = thread_map__new(target_pid, target_tid);
594 if (evlist->threads == NULL)
597 if (cpu_list == NULL && target_tid != -1)
598 evlist->cpus = cpu_map__dummy_new();
600 evlist->cpus = cpu_map__new(cpu_list);
602 if (evlist->cpus == NULL)
603 goto out_delete_threads;
608 thread_map__delete(evlist->threads);
612 void perf_evlist__delete_maps(struct perf_evlist *evlist)
614 cpu_map__delete(evlist->cpus);
615 thread_map__delete(evlist->threads);
617 evlist->threads = NULL;
620 int perf_evlist__set_filters(struct perf_evlist *evlist)
622 const struct thread_map *threads = evlist->threads;
623 const struct cpu_map *cpus = evlist->cpus;
624 struct perf_evsel *evsel;
631 list_for_each_entry(evsel, &evlist->entries, node) {
632 filter = evsel->filter;
635 for (cpu = 0; cpu < cpus->nr; cpu++) {
636 for (thread = 0; thread < threads->nr; thread++) {
637 fd = FD(evsel, cpu, thread);
638 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
648 bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist)
650 struct perf_evsel *pos, *first;
652 pos = first = list_entry(evlist->entries.next, struct perf_evsel, node);
654 list_for_each_entry_continue(pos, &evlist->entries, node) {
655 if (first->attr.sample_type != pos->attr.sample_type)
662 u64 perf_evlist__sample_type(const struct perf_evlist *evlist)
664 struct perf_evsel *first;
666 first = list_entry(evlist->entries.next, struct perf_evsel, node);
667 return first->attr.sample_type;
670 u16 perf_evlist__id_hdr_size(const struct perf_evlist *evlist)
672 struct perf_evsel *first;
673 struct perf_sample *data;
677 first = list_entry(evlist->entries.next, struct perf_evsel, node);
679 if (!first->attr.sample_id_all)
682 sample_type = first->attr.sample_type;
684 if (sample_type & PERF_SAMPLE_TID)
685 size += sizeof(data->tid) * 2;
687 if (sample_type & PERF_SAMPLE_TIME)
688 size += sizeof(data->time);
690 if (sample_type & PERF_SAMPLE_ID)
691 size += sizeof(data->id);
693 if (sample_type & PERF_SAMPLE_STREAM_ID)
694 size += sizeof(data->stream_id);
696 if (sample_type & PERF_SAMPLE_CPU)
697 size += sizeof(data->cpu) * 2;
702 bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist)
704 struct perf_evsel *pos, *first;
706 pos = first = list_entry(evlist->entries.next, struct perf_evsel, node);
708 list_for_each_entry_continue(pos, &evlist->entries, node) {
709 if (first->attr.sample_id_all != pos->attr.sample_id_all)
716 bool perf_evlist__sample_id_all(const struct perf_evlist *evlist)
718 struct perf_evsel *first;
720 first = list_entry(evlist->entries.next, struct perf_evsel, node);
721 return first->attr.sample_id_all;
724 void perf_evlist__set_selected(struct perf_evlist *evlist,
725 struct perf_evsel *evsel)
727 evlist->selected = evsel;
730 int perf_evlist__open(struct perf_evlist *evlist, bool group)
732 struct perf_evsel *evsel, *first;
733 int err, ncpus, nthreads;
735 first = list_entry(evlist->entries.next, struct perf_evsel, node);
737 list_for_each_entry(evsel, &evlist->entries, node) {
738 struct xyarray *group_fd = NULL;
740 if (group && evsel != first)
741 group_fd = first->fd;
743 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads,
751 ncpus = evlist->cpus ? evlist->cpus->nr : 1;
752 nthreads = evlist->threads ? evlist->threads->nr : 1;
754 list_for_each_entry_reverse(evsel, &evlist->entries, node)
755 perf_evsel__close(evsel, ncpus, nthreads);
760 int perf_evlist__prepare_workload(struct perf_evlist *evlist,
761 struct perf_record_opts *opts,
764 int child_ready_pipe[2], go_pipe[2];
767 if (pipe(child_ready_pipe) < 0) {
768 perror("failed to create 'ready' pipe");
772 if (pipe(go_pipe) < 0) {
773 perror("failed to create 'go' pipe");
774 goto out_close_ready_pipe;
777 evlist->workload.pid = fork();
778 if (evlist->workload.pid < 0) {
779 perror("failed to fork");
780 goto out_close_pipes;
783 if (!evlist->workload.pid) {
784 if (opts->pipe_output)
787 close(child_ready_pipe[0]);
789 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
792 * Do a dummy execvp to get the PLT entry resolved,
793 * so we avoid the resolver overhead on the real
796 execvp("", (char **)argv);
799 * Tell the parent we're ready to go
801 close(child_ready_pipe[1]);
804 * Wait until the parent tells us to go.
806 if (read(go_pipe[0], &bf, 1) == -1)
807 perror("unable to read pipe");
809 execvp(argv[0], (char **)argv);
812 kill(getppid(), SIGUSR1);
816 if (!opts->system_wide && opts->target_tid == -1 && opts->target_pid == -1)
817 evlist->threads->map[0] = evlist->workload.pid;
819 close(child_ready_pipe[1]);
822 * wait for child to settle
824 if (read(child_ready_pipe[0], &bf, 1) == -1) {
825 perror("unable to read pipe");
826 goto out_close_pipes;
829 evlist->workload.cork_fd = go_pipe[1];
830 close(child_ready_pipe[0]);
836 out_close_ready_pipe:
837 close(child_ready_pipe[0]);
838 close(child_ready_pipe[1]);
842 int perf_evlist__start_workload(struct perf_evlist *evlist)
844 if (evlist->workload.cork_fd > 0) {
846 * Remove the cork, let it rip!
848 return close(evlist->workload.cork_fd);