1f8a95309SArnaldo Carvalho de Melo /* 2f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3f8a95309SArnaldo Carvalho de Melo * 4f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 5f8a95309SArnaldo Carvalho de Melo * copyright notes. 6f8a95309SArnaldo Carvalho de Melo * 7f8a95309SArnaldo Carvalho de Melo * Released under the GPL v2. (and only v2, not any later version) 8f8a95309SArnaldo Carvalho de Melo */ 9a8c9ae18SArnaldo Carvalho de Melo #include "util.h" 10a8c9ae18SArnaldo Carvalho de Melo #include "debugfs.h" 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1412864b31SNamhyung Kim #include "target.h" 15361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 16361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 1735b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 18361c99a6SArnaldo Carvalho de Melo 1950d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 2050d08e47SArnaldo Carvalho de Melo 21f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 22f8a95309SArnaldo Carvalho de Melo 2370db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2470db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 2570db7533SArnaldo Carvalho de Melo 26f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 27a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 28f8a95309SArnaldo Carvalho de Melo 297e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 307e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 31361c99a6SArnaldo Carvalho de Melo { 3270db7533SArnaldo Carvalho de Melo int i; 3370db7533SArnaldo Carvalho de Melo 3470db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 3570db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 36361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 377e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 3835b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 39361c99a6SArnaldo Carvalho de Melo } 40361c99a6SArnaldo Carvalho de Melo 417e2ed097SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new(struct cpu_map *cpus, 427e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 43ef1d1af2SArnaldo Carvalho de Melo { 44ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 45ef1d1af2SArnaldo Carvalho de Melo 46ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 477e2ed097SArnaldo Carvalho de Melo perf_evlist__init(evlist, cpus, threads); 48ef1d1af2SArnaldo Carvalho de Melo 49361c99a6SArnaldo Carvalho de Melo return evlist; 50361c99a6SArnaldo Carvalho de Melo } 51361c99a6SArnaldo Carvalho de Melo 520f82ebc4SArnaldo Carvalho de Melo void perf_evlist__config_attrs(struct perf_evlist *evlist, 530f82ebc4SArnaldo Carvalho de Melo struct perf_record_opts *opts) 540f82ebc4SArnaldo Carvalho de Melo { 55cac21425SJiri Olsa struct perf_evsel *evsel; 560f82ebc4SArnaldo Carvalho de Melo 570f82ebc4SArnaldo Carvalho de Melo if (evlist->cpus->map[0] < 0) 580f82ebc4SArnaldo Carvalho de Melo opts->no_inherit = true; 590f82ebc4SArnaldo Carvalho de Melo 600f82ebc4SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 61cac21425SJiri Olsa perf_evsel__config(evsel, opts); 620f82ebc4SArnaldo Carvalho de Melo 630f82ebc4SArnaldo Carvalho de Melo if (evlist->nr_entries > 1) 640f82ebc4SArnaldo Carvalho de Melo evsel->attr.sample_type |= PERF_SAMPLE_ID; 650f82ebc4SArnaldo Carvalho de Melo } 660f82ebc4SArnaldo Carvalho de Melo } 670f82ebc4SArnaldo Carvalho de Melo 68361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 69361c99a6SArnaldo Carvalho de Melo { 70361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 71361c99a6SArnaldo Carvalho de Melo 72361c99a6SArnaldo Carvalho de Melo list_for_each_entry_safe(pos, n, &evlist->entries, node) { 73361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 74361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 75361c99a6SArnaldo Carvalho de Melo } 76361c99a6SArnaldo Carvalho de Melo 77361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 78361c99a6SArnaldo Carvalho de Melo } 79361c99a6SArnaldo Carvalho de Melo 80ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 81ef1d1af2SArnaldo Carvalho de Melo { 82ef1d1af2SArnaldo Carvalho de Melo free(evlist->mmap); 83ef1d1af2SArnaldo Carvalho de Melo free(evlist->pollfd); 84ef1d1af2SArnaldo Carvalho de Melo evlist->mmap = NULL; 85ef1d1af2SArnaldo Carvalho de Melo evlist->pollfd = NULL; 86ef1d1af2SArnaldo Carvalho de Melo } 87ef1d1af2SArnaldo Carvalho de Melo 88361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 89361c99a6SArnaldo Carvalho de Melo { 90361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 91ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 92361c99a6SArnaldo Carvalho de Melo free(evlist); 93361c99a6SArnaldo Carvalho de Melo } 94361c99a6SArnaldo Carvalho de Melo 95361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 96361c99a6SArnaldo Carvalho de Melo { 97361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 98361c99a6SArnaldo Carvalho de Melo ++evlist->nr_entries; 99361c99a6SArnaldo Carvalho de Melo } 100361c99a6SArnaldo Carvalho de Melo 1010529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 10250d08e47SArnaldo Carvalho de Melo struct list_head *list, 10350d08e47SArnaldo Carvalho de Melo int nr_entries) 10450d08e47SArnaldo Carvalho de Melo { 10550d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 10650d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 10750d08e47SArnaldo Carvalho de Melo } 10850d08e47SArnaldo Carvalho de Melo 10963dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 11063dab225SArnaldo Carvalho de Melo { 11163dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 11263dab225SArnaldo Carvalho de Melo 11363dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 11463dab225SArnaldo Carvalho de Melo leader->leader = NULL; 11563dab225SArnaldo Carvalho de Melo 11663dab225SArnaldo Carvalho de Melo list_for_each_entry(evsel, list, node) { 11763dab225SArnaldo Carvalho de Melo if (evsel != leader) 11863dab225SArnaldo Carvalho de Melo evsel->leader = leader; 11963dab225SArnaldo Carvalho de Melo } 12063dab225SArnaldo Carvalho de Melo } 12163dab225SArnaldo Carvalho de Melo 12263dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1236a4bb04cSJiri Olsa { 1246a4bb04cSJiri Olsa if (evlist->nr_entries) 12563dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1266a4bb04cSJiri Olsa } 1276a4bb04cSJiri Olsa 128361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 129361c99a6SArnaldo Carvalho de Melo { 130361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 131361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 132361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 133361c99a6SArnaldo Carvalho de Melo }; 1341aed2671SJoerg Roedel struct perf_evsel *evsel; 135361c99a6SArnaldo Carvalho de Melo 1361aed2671SJoerg Roedel event_attr_init(&attr); 1371aed2671SJoerg Roedel 1381aed2671SJoerg Roedel evsel = perf_evsel__new(&attr, 0); 139361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 140cc2d86b0SStephane Eranian goto error; 141cc2d86b0SStephane Eranian 142cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 143cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 144cc2d86b0SStephane Eranian if (!evsel->name) 145cc2d86b0SStephane Eranian goto error_free; 146361c99a6SArnaldo Carvalho de Melo 147361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 148361c99a6SArnaldo Carvalho de Melo return 0; 149cc2d86b0SStephane Eranian error_free: 150cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 151cc2d86b0SStephane Eranian error: 152cc2d86b0SStephane Eranian return -ENOMEM; 153361c99a6SArnaldo Carvalho de Melo } 1545c581041SArnaldo Carvalho de Melo 155e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 15650d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 15750d08e47SArnaldo Carvalho de Melo { 15850d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 15950d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 16050d08e47SArnaldo Carvalho de Melo size_t i; 16150d08e47SArnaldo Carvalho de Melo 16250d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 16350d08e47SArnaldo Carvalho de Melo evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); 16450d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 16550d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 16650d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 16750d08e47SArnaldo Carvalho de Melo } 16850d08e47SArnaldo Carvalho de Melo 16950d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 17050d08e47SArnaldo Carvalho de Melo 17150d08e47SArnaldo Carvalho de Melo return 0; 17250d08e47SArnaldo Carvalho de Melo 17350d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 17450d08e47SArnaldo Carvalho de Melo list_for_each_entry_safe(evsel, n, &head, node) 17550d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 17650d08e47SArnaldo Carvalho de Melo return -1; 17750d08e47SArnaldo Carvalho de Melo } 17850d08e47SArnaldo Carvalho de Melo 17979695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 18079695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 18179695e1bSArnaldo Carvalho de Melo { 18279695e1bSArnaldo Carvalho de Melo size_t i; 18379695e1bSArnaldo Carvalho de Melo 18479695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 18579695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 18679695e1bSArnaldo Carvalho de Melo 18779695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 18879695e1bSArnaldo Carvalho de Melo } 18979695e1bSArnaldo Carvalho de Melo 190da378962SArnaldo Carvalho de Melo struct perf_evsel * 191ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 192ee29be62SArnaldo Carvalho de Melo { 193ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 194ee29be62SArnaldo Carvalho de Melo 195ee29be62SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 196ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 197ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 198ee29be62SArnaldo Carvalho de Melo return evsel; 199ee29be62SArnaldo Carvalho de Melo } 200ee29be62SArnaldo Carvalho de Melo 201ee29be62SArnaldo Carvalho de Melo return NULL; 202ee29be62SArnaldo Carvalho de Melo } 203ee29be62SArnaldo Carvalho de Melo 20439876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 20539876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 20639876e7dSArnaldo Carvalho de Melo { 20739876e7dSArnaldo Carvalho de Melo struct perf_evsel *evsel; 20839876e7dSArnaldo Carvalho de Melo 20939876e7dSArnaldo Carvalho de Melo evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); 21039876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 21139876e7dSArnaldo Carvalho de Melo return -1; 21239876e7dSArnaldo Carvalho de Melo 21339876e7dSArnaldo Carvalho de Melo evsel->handler.func = handler; 21439876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 21539876e7dSArnaldo Carvalho de Melo return 0; 21639876e7dSArnaldo Carvalho de Melo } 21739876e7dSArnaldo Carvalho de Melo 2184152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2194152ab37SArnaldo Carvalho de Melo { 2204152ab37SArnaldo Carvalho de Melo int cpu, thread; 2214152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 2224152ab37SArnaldo Carvalho de Melo 2234152ab37SArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 2244152ab37SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 2254152ab37SArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) 22655da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 22755da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2284152ab37SArnaldo Carvalho de Melo } 2294152ab37SArnaldo Carvalho de Melo } 2304152ab37SArnaldo Carvalho de Melo } 2314152ab37SArnaldo Carvalho de Melo 232764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 233764e16a3SDavid Ahern { 234764e16a3SDavid Ahern int cpu, thread; 235764e16a3SDavid Ahern struct perf_evsel *pos; 236764e16a3SDavid Ahern 237a14bb7a6SArnaldo Carvalho de Melo for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) { 238764e16a3SDavid Ahern list_for_each_entry(pos, &evlist->entries, node) { 239764e16a3SDavid Ahern for (thread = 0; thread < evlist->threads->nr; thread++) 24055da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 24155da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 242764e16a3SDavid Ahern } 243764e16a3SDavid Ahern } 244764e16a3SDavid Ahern } 245764e16a3SDavid Ahern 246806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 2475c581041SArnaldo Carvalho de Melo { 248a14bb7a6SArnaldo Carvalho de Melo int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries; 2495c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 2505c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 2515c581041SArnaldo Carvalho de Melo } 25270082dd9SArnaldo Carvalho de Melo 25370082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 25470082dd9SArnaldo Carvalho de Melo { 25570082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 25670082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 25770082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 25870082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 25970082dd9SArnaldo Carvalho de Melo } 26070db7533SArnaldo Carvalho de Melo 261a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 262a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 2633d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 2643d3b5e95SArnaldo Carvalho de Melo { 2653d3b5e95SArnaldo Carvalho de Melo int hash; 2663d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 2673d3b5e95SArnaldo Carvalho de Melo 2683d3b5e95SArnaldo Carvalho de Melo sid->id = id; 2693d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 2703d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 2713d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 2723d3b5e95SArnaldo Carvalho de Melo } 2733d3b5e95SArnaldo Carvalho de Melo 274a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 275a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 276a91e5431SArnaldo Carvalho de Melo { 277a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 278a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 279a91e5431SArnaldo Carvalho de Melo } 280a91e5431SArnaldo Carvalho de Melo 281a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 2823d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 283f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 284f8a95309SArnaldo Carvalho de Melo { 285f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 2863d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 287f8a95309SArnaldo Carvalho de Melo 288f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 289f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 290f8a95309SArnaldo Carvalho de Melo return -1; 291f8a95309SArnaldo Carvalho de Melo 292f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 293f8a95309SArnaldo Carvalho de Melo ++id_idx; 294f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 295f8a95309SArnaldo Carvalho de Melo ++id_idx; 296f8a95309SArnaldo Carvalho de Melo 297a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 298f8a95309SArnaldo Carvalho de Melo return 0; 299f8a95309SArnaldo Carvalho de Melo } 300f8a95309SArnaldo Carvalho de Melo 30170db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 30270db7533SArnaldo Carvalho de Melo { 30370db7533SArnaldo Carvalho de Melo struct hlist_head *head; 30470db7533SArnaldo Carvalho de Melo struct hlist_node *pos; 30570db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 30670db7533SArnaldo Carvalho de Melo int hash; 30770db7533SArnaldo Carvalho de Melo 30870db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 3090c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 31070db7533SArnaldo Carvalho de Melo 31170db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 31270db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 31370db7533SArnaldo Carvalho de Melo 31470db7533SArnaldo Carvalho de Melo hlist_for_each_entry(sid, pos, head, node) 31570db7533SArnaldo Carvalho de Melo if (sid->id == id) 31670db7533SArnaldo Carvalho de Melo return sid->evsel; 31730e68bccSNamhyung Kim 31830e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 3190c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 32030e68bccSNamhyung Kim 32170db7533SArnaldo Carvalho de Melo return NULL; 32270db7533SArnaldo Carvalho de Melo } 32304391debSArnaldo Carvalho de Melo 324aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 32504391debSArnaldo Carvalho de Melo { 326aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 32704391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 32804391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 32904391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3308115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 33104391debSArnaldo Carvalho de Melo 3327bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 33304391debSArnaldo Carvalho de Melo /* 33404391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 33504391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 33604391debSArnaldo Carvalho de Melo * 33704391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 33804391debSArnaldo Carvalho de Melo * 33904391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 34004391debSArnaldo Carvalho de Melo */ 3417bb41152SArnaldo Carvalho de Melo int diff = head - old; 34204391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 34304391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 34404391debSArnaldo Carvalho de Melo 34504391debSArnaldo Carvalho de Melo /* 34604391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 34704391debSArnaldo Carvalho de Melo */ 34804391debSArnaldo Carvalho de Melo old = head; 34904391debSArnaldo Carvalho de Melo } 3507bb41152SArnaldo Carvalho de Melo } 35104391debSArnaldo Carvalho de Melo 35204391debSArnaldo Carvalho de Melo if (old != head) { 35304391debSArnaldo Carvalho de Melo size_t size; 35404391debSArnaldo Carvalho de Melo 3558115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 35604391debSArnaldo Carvalho de Melo size = event->header.size; 35704391debSArnaldo Carvalho de Melo 35804391debSArnaldo Carvalho de Melo /* 35904391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 36004391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 36104391debSArnaldo Carvalho de Melo */ 36204391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 36304391debSArnaldo Carvalho de Melo unsigned int offset = old; 36404391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 36504391debSArnaldo Carvalho de Melo void *dst = &evlist->event_copy; 36604391debSArnaldo Carvalho de Melo 36704391debSArnaldo Carvalho de Melo do { 36804391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 36904391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 37004391debSArnaldo Carvalho de Melo offset += cpy; 37104391debSArnaldo Carvalho de Melo dst += cpy; 37204391debSArnaldo Carvalho de Melo len -= cpy; 37304391debSArnaldo Carvalho de Melo } while (len); 37404391debSArnaldo Carvalho de Melo 37504391debSArnaldo Carvalho de Melo event = &evlist->event_copy; 37604391debSArnaldo Carvalho de Melo } 37704391debSArnaldo Carvalho de Melo 37804391debSArnaldo Carvalho de Melo old += size; 37904391debSArnaldo Carvalho de Melo } 38004391debSArnaldo Carvalho de Melo 38104391debSArnaldo Carvalho de Melo md->prev = old; 3827bb41152SArnaldo Carvalho de Melo 3837bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 3847bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 3857bb41152SArnaldo Carvalho de Melo 38604391debSArnaldo Carvalho de Melo return event; 38704391debSArnaldo Carvalho de Melo } 388f8a95309SArnaldo Carvalho de Melo 3897e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 390f8a95309SArnaldo Carvalho de Melo { 391aece948fSArnaldo Carvalho de Melo int i; 392f8a95309SArnaldo Carvalho de Melo 393aece948fSArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 394aece948fSArnaldo Carvalho de Melo if (evlist->mmap[i].base != NULL) { 395aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[i].base, evlist->mmap_len); 396aece948fSArnaldo Carvalho de Melo evlist->mmap[i].base = NULL; 397f8a95309SArnaldo Carvalho de Melo } 398f8a95309SArnaldo Carvalho de Melo } 399aece948fSArnaldo Carvalho de Melo 400aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 401aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 402f8a95309SArnaldo Carvalho de Melo } 403f8a95309SArnaldo Carvalho de Melo 404806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 405f8a95309SArnaldo Carvalho de Melo { 406a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 407a14bb7a6SArnaldo Carvalho de Melo if (cpu_map__all(evlist->cpus)) 408aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->threads->nr; 409aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 410f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 411f8a95309SArnaldo Carvalho de Melo } 412f8a95309SArnaldo Carvalho de Melo 413bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 414aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 415f8a95309SArnaldo Carvalho de Melo { 416aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 417aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 418aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 419f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 420301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 421301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 422f8a95309SArnaldo Carvalho de Melo return -1; 423301b195dSNelson Elhage } 424f8a95309SArnaldo Carvalho de Melo 425f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 426f8a95309SArnaldo Carvalho de Melo return 0; 427f8a95309SArnaldo Carvalho de Melo } 428f8a95309SArnaldo Carvalho de Melo 429aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 430aece948fSArnaldo Carvalho de Melo { 431aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 432aece948fSArnaldo Carvalho de Melo int cpu, thread; 433aece948fSArnaldo Carvalho de Melo 434aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 435aece948fSArnaldo Carvalho de Melo int output = -1; 436aece948fSArnaldo Carvalho de Melo 437aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 438aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 439aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 440aece948fSArnaldo Carvalho de Melo 441aece948fSArnaldo Carvalho de Melo if (output == -1) { 442aece948fSArnaldo Carvalho de Melo output = fd; 443bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 444aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 445aece948fSArnaldo Carvalho de Melo goto out_unmap; 446aece948fSArnaldo Carvalho de Melo } else { 447aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 448aece948fSArnaldo Carvalho de Melo goto out_unmap; 449aece948fSArnaldo Carvalho de Melo } 450aece948fSArnaldo Carvalho de Melo 451aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 452aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 453aece948fSArnaldo Carvalho de Melo goto out_unmap; 454aece948fSArnaldo Carvalho de Melo } 455aece948fSArnaldo Carvalho de Melo } 456aece948fSArnaldo Carvalho de Melo } 457aece948fSArnaldo Carvalho de Melo 458aece948fSArnaldo Carvalho de Melo return 0; 459aece948fSArnaldo Carvalho de Melo 460aece948fSArnaldo Carvalho de Melo out_unmap: 461aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 462aece948fSArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 463aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 464aece948fSArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 465aece948fSArnaldo Carvalho de Melo } 466aece948fSArnaldo Carvalho de Melo } 467aece948fSArnaldo Carvalho de Melo return -1; 468aece948fSArnaldo Carvalho de Melo } 469aece948fSArnaldo Carvalho de Melo 470aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 471aece948fSArnaldo Carvalho de Melo { 472aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 473aece948fSArnaldo Carvalho de Melo int thread; 474aece948fSArnaldo Carvalho de Melo 475aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 476aece948fSArnaldo Carvalho de Melo int output = -1; 477aece948fSArnaldo Carvalho de Melo 478aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 479aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 480aece948fSArnaldo Carvalho de Melo 481aece948fSArnaldo Carvalho de Melo if (output == -1) { 482aece948fSArnaldo Carvalho de Melo output = fd; 483bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 484aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 485aece948fSArnaldo Carvalho de Melo goto out_unmap; 486aece948fSArnaldo Carvalho de Melo } else { 487aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 488aece948fSArnaldo Carvalho de Melo goto out_unmap; 489aece948fSArnaldo Carvalho de Melo } 490aece948fSArnaldo Carvalho de Melo 491aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 492aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 493aece948fSArnaldo Carvalho de Melo goto out_unmap; 494aece948fSArnaldo Carvalho de Melo } 495aece948fSArnaldo Carvalho de Melo } 496aece948fSArnaldo Carvalho de Melo 497aece948fSArnaldo Carvalho de Melo return 0; 498aece948fSArnaldo Carvalho de Melo 499aece948fSArnaldo Carvalho de Melo out_unmap: 500aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 501aece948fSArnaldo Carvalho de Melo if (evlist->mmap[thread].base != NULL) { 502aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[thread].base, evlist->mmap_len); 503aece948fSArnaldo Carvalho de Melo evlist->mmap[thread].base = NULL; 504aece948fSArnaldo Carvalho de Melo } 505aece948fSArnaldo Carvalho de Melo } 506aece948fSArnaldo Carvalho de Melo return -1; 507aece948fSArnaldo Carvalho de Melo } 508aece948fSArnaldo Carvalho de Melo 509f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 510f8a95309SArnaldo Carvalho de Melo * 511f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 512f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 513f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 514f8a95309SArnaldo Carvalho de Melo * 515f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 516f8a95309SArnaldo Carvalho de Melo * 517f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 518f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 519f8a95309SArnaldo Carvalho de Melo * 520f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5217e2ed097SArnaldo Carvalho de Melo * 5227e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 523f8a95309SArnaldo Carvalho de Melo */ 52450a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 52550a682ceSArnaldo Carvalho de Melo bool overwrite) 526f8a95309SArnaldo Carvalho de Melo { 527aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5287e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5297e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 53050a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 53150a682ceSArnaldo Carvalho de Melo 53250a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 53350a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 53450a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 53541d0d933SNelson Elhage else if (!is_power_of_2(pages)) 53641d0d933SNelson Elhage return -EINVAL; 53750a682ceSArnaldo Carvalho de Melo 53850a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 539f8a95309SArnaldo Carvalho de Melo 5407e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 541f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 542f8a95309SArnaldo Carvalho de Melo 5437e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 544f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 545f8a95309SArnaldo Carvalho de Melo 546f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 547f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 548f8a95309SArnaldo Carvalho de Melo 549f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 550f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 551a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 552a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 553f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 554f8a95309SArnaldo Carvalho de Melo } 555f8a95309SArnaldo Carvalho de Melo 556a14bb7a6SArnaldo Carvalho de Melo if (cpu_map__all(cpus)) 557aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 558f8a95309SArnaldo Carvalho de Melo 559aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 560f8a95309SArnaldo Carvalho de Melo } 5617e2ed097SArnaldo Carvalho de Melo 562b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 563b809ac10SNamhyung Kim struct perf_target *target) 5647e2ed097SArnaldo Carvalho de Melo { 565b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 566b809ac10SNamhyung Kim target->uid); 5677e2ed097SArnaldo Carvalho de Melo 5687e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 5697e2ed097SArnaldo Carvalho de Melo return -1; 5707e2ed097SArnaldo Carvalho de Melo 571879d77d0SNamhyung Kim if (perf_target__has_task(target)) 572d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 573d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 574d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 575879d77d0SNamhyung Kim else 576879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 5777e2ed097SArnaldo Carvalho de Melo 5787e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 5797e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 5807e2ed097SArnaldo Carvalho de Melo 5817e2ed097SArnaldo Carvalho de Melo return 0; 5827e2ed097SArnaldo Carvalho de Melo 5837e2ed097SArnaldo Carvalho de Melo out_delete_threads: 5847e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 5857e2ed097SArnaldo Carvalho de Melo return -1; 5867e2ed097SArnaldo Carvalho de Melo } 5877e2ed097SArnaldo Carvalho de Melo 5887e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 5897e2ed097SArnaldo Carvalho de Melo { 5907e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 5917e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 5927e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 5937e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 5947e2ed097SArnaldo Carvalho de Melo } 5950a102479SFrederic Weisbecker 5961491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 5970a102479SFrederic Weisbecker { 5980a102479SFrederic Weisbecker struct perf_evsel *evsel; 599745cefc5SArnaldo Carvalho de Melo int err = 0; 600745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 601745cefc5SArnaldo Carvalho de Melo nthreads = evlist->threads->nr; 6020a102479SFrederic Weisbecker 6030a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 604745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 6050a102479SFrederic Weisbecker continue; 606745cefc5SArnaldo Carvalho de Melo 607745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 6080a102479SFrederic Weisbecker if (err) 609745cefc5SArnaldo Carvalho de Melo break; 6100a102479SFrederic Weisbecker } 6110a102479SFrederic Weisbecker 612745cefc5SArnaldo Carvalho de Melo return err; 613745cefc5SArnaldo Carvalho de Melo } 614745cefc5SArnaldo Carvalho de Melo 615745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 616745cefc5SArnaldo Carvalho de Melo { 617745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 618745cefc5SArnaldo Carvalho de Melo int err = 0; 619745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 620745cefc5SArnaldo Carvalho de Melo nthreads = evlist->threads->nr; 621745cefc5SArnaldo Carvalho de Melo 622745cefc5SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 623745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 624745cefc5SArnaldo Carvalho de Melo if (err) 625745cefc5SArnaldo Carvalho de Melo break; 626745cefc5SArnaldo Carvalho de Melo } 627745cefc5SArnaldo Carvalho de Melo 628745cefc5SArnaldo Carvalho de Melo return err; 6290a102479SFrederic Weisbecker } 63074429964SFrederic Weisbecker 6310c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 63274429964SFrederic Weisbecker { 6330c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 634c2a70653SArnaldo Carvalho de Melo 635c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 636c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 637c2a70653SArnaldo Carvalho de Melo return false; 63874429964SFrederic Weisbecker } 63974429964SFrederic Weisbecker 640c2a70653SArnaldo Carvalho de Melo return true; 641c2a70653SArnaldo Carvalho de Melo } 642c2a70653SArnaldo Carvalho de Melo 6430c21f736SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(struct perf_evlist *evlist) 644c2a70653SArnaldo Carvalho de Melo { 6450c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 646c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 647c2a70653SArnaldo Carvalho de Melo } 648c2a70653SArnaldo Carvalho de Melo 6490c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 65081e36bffSArnaldo Carvalho de Melo { 6510c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 65281e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 65381e36bffSArnaldo Carvalho de Melo u64 sample_type; 65481e36bffSArnaldo Carvalho de Melo u16 size = 0; 65581e36bffSArnaldo Carvalho de Melo 65681e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 65781e36bffSArnaldo Carvalho de Melo goto out; 65881e36bffSArnaldo Carvalho de Melo 65981e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 66081e36bffSArnaldo Carvalho de Melo 66181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 66281e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 66381e36bffSArnaldo Carvalho de Melo 66481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 66581e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 66681e36bffSArnaldo Carvalho de Melo 66781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 66881e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 66981e36bffSArnaldo Carvalho de Melo 67081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 67181e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 67281e36bffSArnaldo Carvalho de Melo 67381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 67481e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 67581e36bffSArnaldo Carvalho de Melo out: 67681e36bffSArnaldo Carvalho de Melo return size; 67781e36bffSArnaldo Carvalho de Melo } 67881e36bffSArnaldo Carvalho de Melo 6790c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 680c2a70653SArnaldo Carvalho de Melo { 6810c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 682c2a70653SArnaldo Carvalho de Melo 683c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 684c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 685c2a70653SArnaldo Carvalho de Melo return false; 686c2a70653SArnaldo Carvalho de Melo } 687c2a70653SArnaldo Carvalho de Melo 688c2a70653SArnaldo Carvalho de Melo return true; 68974429964SFrederic Weisbecker } 69074429964SFrederic Weisbecker 6910c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 69274429964SFrederic Weisbecker { 6930c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 694c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 69574429964SFrederic Weisbecker } 69681cce8deSArnaldo Carvalho de Melo 69781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 69881cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 69981cce8deSArnaldo Carvalho de Melo { 70081cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 70181cce8deSArnaldo Carvalho de Melo } 702727ab04eSArnaldo Carvalho de Melo 7036a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 704727ab04eSArnaldo Carvalho de Melo { 7056a4bb04cSJiri Olsa struct perf_evsel *evsel; 706727ab04eSArnaldo Carvalho de Melo int err, ncpus, nthreads; 707727ab04eSArnaldo Carvalho de Melo 708727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 7096a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 710727ab04eSArnaldo Carvalho de Melo if (err < 0) 711727ab04eSArnaldo Carvalho de Melo goto out_err; 712727ab04eSArnaldo Carvalho de Melo } 713727ab04eSArnaldo Carvalho de Melo 714727ab04eSArnaldo Carvalho de Melo return 0; 715727ab04eSArnaldo Carvalho de Melo out_err: 716727ab04eSArnaldo Carvalho de Melo ncpus = evlist->cpus ? evlist->cpus->nr : 1; 717727ab04eSArnaldo Carvalho de Melo nthreads = evlist->threads ? evlist->threads->nr : 1; 718727ab04eSArnaldo Carvalho de Melo 719727ab04eSArnaldo Carvalho de Melo list_for_each_entry_reverse(evsel, &evlist->entries, node) 720727ab04eSArnaldo Carvalho de Melo perf_evsel__close(evsel, ncpus, nthreads); 721727ab04eSArnaldo Carvalho de Melo 72241c21a68SNamhyung Kim errno = -err; 723727ab04eSArnaldo Carvalho de Melo return err; 724727ab04eSArnaldo Carvalho de Melo } 72535b9d88eSArnaldo Carvalho de Melo 72635b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 72735b9d88eSArnaldo Carvalho de Melo struct perf_record_opts *opts, 72835b9d88eSArnaldo Carvalho de Melo const char *argv[]) 72935b9d88eSArnaldo Carvalho de Melo { 73035b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 73135b9d88eSArnaldo Carvalho de Melo char bf; 73235b9d88eSArnaldo Carvalho de Melo 73335b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 73435b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 73535b9d88eSArnaldo Carvalho de Melo return -1; 73635b9d88eSArnaldo Carvalho de Melo } 73735b9d88eSArnaldo Carvalho de Melo 73835b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 73935b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 74035b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 74135b9d88eSArnaldo Carvalho de Melo } 74235b9d88eSArnaldo Carvalho de Melo 74335b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 74435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 74535b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 74635b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 74735b9d88eSArnaldo Carvalho de Melo } 74835b9d88eSArnaldo Carvalho de Melo 74935b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 75035b9d88eSArnaldo Carvalho de Melo if (opts->pipe_output) 75135b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 75235b9d88eSArnaldo Carvalho de Melo 75335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 75435b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 75535b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 75635b9d88eSArnaldo Carvalho de Melo 75735b9d88eSArnaldo Carvalho de Melo /* 75835b9d88eSArnaldo Carvalho de Melo * Do a dummy execvp to get the PLT entry resolved, 75935b9d88eSArnaldo Carvalho de Melo * so we avoid the resolver overhead on the real 76035b9d88eSArnaldo Carvalho de Melo * execvp call. 76135b9d88eSArnaldo Carvalho de Melo */ 76235b9d88eSArnaldo Carvalho de Melo execvp("", (char **)argv); 76335b9d88eSArnaldo Carvalho de Melo 76435b9d88eSArnaldo Carvalho de Melo /* 76535b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 76635b9d88eSArnaldo Carvalho de Melo */ 76735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 76835b9d88eSArnaldo Carvalho de Melo 76935b9d88eSArnaldo Carvalho de Melo /* 77035b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 77135b9d88eSArnaldo Carvalho de Melo */ 77235b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 77335b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 77435b9d88eSArnaldo Carvalho de Melo 77535b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 77635b9d88eSArnaldo Carvalho de Melo 77735b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 77835b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 77935b9d88eSArnaldo Carvalho de Melo exit(-1); 78035b9d88eSArnaldo Carvalho de Melo } 78135b9d88eSArnaldo Carvalho de Melo 782d67356e7SNamhyung Kim if (perf_target__none(&opts->target)) 78335b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 78435b9d88eSArnaldo Carvalho de Melo 78535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 78635b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 78735b9d88eSArnaldo Carvalho de Melo /* 78835b9d88eSArnaldo Carvalho de Melo * wait for child to settle 78935b9d88eSArnaldo Carvalho de Melo */ 79035b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 79135b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 79235b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 79335b9d88eSArnaldo Carvalho de Melo } 79435b9d88eSArnaldo Carvalho de Melo 79535b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 79635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 79735b9d88eSArnaldo Carvalho de Melo return 0; 79835b9d88eSArnaldo Carvalho de Melo 79935b9d88eSArnaldo Carvalho de Melo out_close_pipes: 80035b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 80135b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 80235b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 80335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 80435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 80535b9d88eSArnaldo Carvalho de Melo return -1; 80635b9d88eSArnaldo Carvalho de Melo } 80735b9d88eSArnaldo Carvalho de Melo 80835b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 80935b9d88eSArnaldo Carvalho de Melo { 81035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 81135b9d88eSArnaldo Carvalho de Melo /* 81235b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 81335b9d88eSArnaldo Carvalho de Melo */ 81435b9d88eSArnaldo Carvalho de Melo return close(evlist->workload.cork_fd); 81535b9d88eSArnaldo Carvalho de Melo } 81635b9d88eSArnaldo Carvalho de Melo 81735b9d88eSArnaldo Carvalho de Melo return 0; 81835b9d88eSArnaldo Carvalho de Melo } 819cb0b29e0SArnaldo Carvalho de Melo 820a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 8210807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 822cb0b29e0SArnaldo Carvalho de Melo { 8230c21f736SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evlist__first(evlist); 8240807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 825cb0b29e0SArnaldo Carvalho de Melo } 82678f067b3SArnaldo Carvalho de Melo 82778f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 82878f067b3SArnaldo Carvalho de Melo { 82978f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 83078f067b3SArnaldo Carvalho de Melo size_t printed = 0; 83178f067b3SArnaldo Carvalho de Melo 83278f067b3SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 83378f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 83478f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 83578f067b3SArnaldo Carvalho de Melo } 83678f067b3SArnaldo Carvalho de Melo 83778f067b3SArnaldo Carvalho de Melo return printed + fprintf(fp, "\n");; 83878f067b3SArnaldo Carvalho de Melo } 839