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 */ 95c581041SArnaldo Carvalho de Melo #include <poll.h> 10f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 11f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 12361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 13361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 14361c99a6SArnaldo Carvalho de Melo #include "util.h" 155d2cd909SArnaldo Carvalho de Melo #include "debug.h" 16361c99a6SArnaldo Carvalho de Melo 17f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 18f8a95309SArnaldo Carvalho de Melo 1970db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2070db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 2170db7533SArnaldo Carvalho de Melo 22f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 23a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 24f8a95309SArnaldo Carvalho de Melo 257e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 267e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 27361c99a6SArnaldo Carvalho de Melo { 2870db7533SArnaldo Carvalho de Melo int i; 2970db7533SArnaldo Carvalho de Melo 3070db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 3170db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 32361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 337e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 34361c99a6SArnaldo Carvalho de Melo } 35361c99a6SArnaldo Carvalho de Melo 367e2ed097SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new(struct cpu_map *cpus, 377e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 38ef1d1af2SArnaldo Carvalho de Melo { 39ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 40ef1d1af2SArnaldo Carvalho de Melo 41ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 427e2ed097SArnaldo Carvalho de Melo perf_evlist__init(evlist, cpus, threads); 43ef1d1af2SArnaldo Carvalho de Melo 44361c99a6SArnaldo Carvalho de Melo return evlist; 45361c99a6SArnaldo Carvalho de Melo } 46361c99a6SArnaldo Carvalho de Melo 47361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 48361c99a6SArnaldo Carvalho de Melo { 49361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 50361c99a6SArnaldo Carvalho de Melo 51361c99a6SArnaldo Carvalho de Melo list_for_each_entry_safe(pos, n, &evlist->entries, node) { 52361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 53361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 54361c99a6SArnaldo Carvalho de Melo } 55361c99a6SArnaldo Carvalho de Melo 56361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 57361c99a6SArnaldo Carvalho de Melo } 58361c99a6SArnaldo Carvalho de Melo 59ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 60ef1d1af2SArnaldo Carvalho de Melo { 61ef1d1af2SArnaldo Carvalho de Melo free(evlist->mmap); 62ef1d1af2SArnaldo Carvalho de Melo free(evlist->pollfd); 63ef1d1af2SArnaldo Carvalho de Melo evlist->mmap = NULL; 64ef1d1af2SArnaldo Carvalho de Melo evlist->pollfd = NULL; 65ef1d1af2SArnaldo Carvalho de Melo } 66ef1d1af2SArnaldo Carvalho de Melo 67361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 68361c99a6SArnaldo Carvalho de Melo { 69361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 70ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 71361c99a6SArnaldo Carvalho de Melo free(evlist); 72361c99a6SArnaldo Carvalho de Melo } 73361c99a6SArnaldo Carvalho de Melo 74361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 75361c99a6SArnaldo Carvalho de Melo { 76361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 77361c99a6SArnaldo Carvalho de Melo ++evlist->nr_entries; 78361c99a6SArnaldo Carvalho de Melo } 79361c99a6SArnaldo Carvalho de Melo 80361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 81361c99a6SArnaldo Carvalho de Melo { 82361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 83361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 84361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 85361c99a6SArnaldo Carvalho de Melo }; 86361c99a6SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new(&attr, 0); 87361c99a6SArnaldo Carvalho de Melo 88361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 89361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 90361c99a6SArnaldo Carvalho de Melo 91361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 92361c99a6SArnaldo Carvalho de Melo return 0; 93361c99a6SArnaldo Carvalho de Melo } 945c581041SArnaldo Carvalho de Melo 957e2ed097SArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 965c581041SArnaldo Carvalho de Melo { 977e2ed097SArnaldo Carvalho de Melo int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries; 985c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 995c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 1005c581041SArnaldo Carvalho de Melo } 10170082dd9SArnaldo Carvalho de Melo 10270082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 10370082dd9SArnaldo Carvalho de Melo { 10470082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 10570082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 10670082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 10770082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 10870082dd9SArnaldo Carvalho de Melo } 10970db7533SArnaldo Carvalho de Melo 110a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 111a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 1123d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 1133d3b5e95SArnaldo Carvalho de Melo { 1143d3b5e95SArnaldo Carvalho de Melo int hash; 1153d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 1163d3b5e95SArnaldo Carvalho de Melo 1173d3b5e95SArnaldo Carvalho de Melo sid->id = id; 1183d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 1193d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 1203d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 1213d3b5e95SArnaldo Carvalho de Melo } 1223d3b5e95SArnaldo Carvalho de Melo 123a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 124a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 125a91e5431SArnaldo Carvalho de Melo { 126a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 127a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 128a91e5431SArnaldo Carvalho de Melo } 129a91e5431SArnaldo Carvalho de Melo 130a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 1313d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 132f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 133f8a95309SArnaldo Carvalho de Melo { 134f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 1353d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 136f8a95309SArnaldo Carvalho de Melo 137f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 138f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 139f8a95309SArnaldo Carvalho de Melo return -1; 140f8a95309SArnaldo Carvalho de Melo 141f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 142f8a95309SArnaldo Carvalho de Melo ++id_idx; 143f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 144f8a95309SArnaldo Carvalho de Melo ++id_idx; 145f8a95309SArnaldo Carvalho de Melo 146a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 147f8a95309SArnaldo Carvalho de Melo return 0; 148f8a95309SArnaldo Carvalho de Melo } 149f8a95309SArnaldo Carvalho de Melo 15070db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 15170db7533SArnaldo Carvalho de Melo { 15270db7533SArnaldo Carvalho de Melo struct hlist_head *head; 15370db7533SArnaldo Carvalho de Melo struct hlist_node *pos; 15470db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 15570db7533SArnaldo Carvalho de Melo int hash; 15670db7533SArnaldo Carvalho de Melo 15770db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 15870db7533SArnaldo Carvalho de Melo return list_entry(evlist->entries.next, struct perf_evsel, node); 15970db7533SArnaldo Carvalho de Melo 16070db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 16170db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 16270db7533SArnaldo Carvalho de Melo 16370db7533SArnaldo Carvalho de Melo hlist_for_each_entry(sid, pos, head, node) 16470db7533SArnaldo Carvalho de Melo if (sid->id == id) 16570db7533SArnaldo Carvalho de Melo return sid->evsel; 16670db7533SArnaldo Carvalho de Melo return NULL; 16770db7533SArnaldo Carvalho de Melo } 16804391debSArnaldo Carvalho de Melo 1698115d60cSArnaldo Carvalho de Melo union perf_event *perf_evlist__read_on_cpu(struct perf_evlist *evlist, int cpu) 17004391debSArnaldo Carvalho de Melo { 17104391debSArnaldo Carvalho de Melo /* XXX Move this to perf.c, making it generally available */ 17204391debSArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 17304391debSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[cpu]; 17404391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 17504391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 17604391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 1778115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 17804391debSArnaldo Carvalho de Melo 1797bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 18004391debSArnaldo Carvalho de Melo /* 18104391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 18204391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 18304391debSArnaldo Carvalho de Melo * 18404391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 18504391debSArnaldo Carvalho de Melo * 18604391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 18704391debSArnaldo Carvalho de Melo */ 1887bb41152SArnaldo Carvalho de Melo int diff = head - old; 18904391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 19004391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 19104391debSArnaldo Carvalho de Melo 19204391debSArnaldo Carvalho de Melo /* 19304391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 19404391debSArnaldo Carvalho de Melo */ 19504391debSArnaldo Carvalho de Melo old = head; 19604391debSArnaldo Carvalho de Melo } 1977bb41152SArnaldo Carvalho de Melo } 19804391debSArnaldo Carvalho de Melo 19904391debSArnaldo Carvalho de Melo if (old != head) { 20004391debSArnaldo Carvalho de Melo size_t size; 20104391debSArnaldo Carvalho de Melo 2028115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 20304391debSArnaldo Carvalho de Melo size = event->header.size; 20404391debSArnaldo Carvalho de Melo 20504391debSArnaldo Carvalho de Melo /* 20604391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 20704391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 20804391debSArnaldo Carvalho de Melo */ 20904391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 21004391debSArnaldo Carvalho de Melo unsigned int offset = old; 21104391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 21204391debSArnaldo Carvalho de Melo void *dst = &evlist->event_copy; 21304391debSArnaldo Carvalho de Melo 21404391debSArnaldo Carvalho de Melo do { 21504391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 21604391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 21704391debSArnaldo Carvalho de Melo offset += cpy; 21804391debSArnaldo Carvalho de Melo dst += cpy; 21904391debSArnaldo Carvalho de Melo len -= cpy; 22004391debSArnaldo Carvalho de Melo } while (len); 22104391debSArnaldo Carvalho de Melo 22204391debSArnaldo Carvalho de Melo event = &evlist->event_copy; 22304391debSArnaldo Carvalho de Melo } 22404391debSArnaldo Carvalho de Melo 22504391debSArnaldo Carvalho de Melo old += size; 22604391debSArnaldo Carvalho de Melo } 22704391debSArnaldo Carvalho de Melo 22804391debSArnaldo Carvalho de Melo md->prev = old; 2297bb41152SArnaldo Carvalho de Melo 2307bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 2317bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 2327bb41152SArnaldo Carvalho de Melo 23304391debSArnaldo Carvalho de Melo return event; 23404391debSArnaldo Carvalho de Melo } 235f8a95309SArnaldo Carvalho de Melo 2367e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 237f8a95309SArnaldo Carvalho de Melo { 238f8a95309SArnaldo Carvalho de Melo int cpu; 239f8a95309SArnaldo Carvalho de Melo 2407e2ed097SArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 241f8a95309SArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 242f8a95309SArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 243f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 244f8a95309SArnaldo Carvalho de Melo } 245f8a95309SArnaldo Carvalho de Melo } 246f8a95309SArnaldo Carvalho de Melo } 247f8a95309SArnaldo Carvalho de Melo 2487e2ed097SArnaldo Carvalho de Melo int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 249f8a95309SArnaldo Carvalho de Melo { 2507e2ed097SArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->cpus->nr * sizeof(struct perf_mmap)); 251f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 252f8a95309SArnaldo Carvalho de Melo } 253f8a95309SArnaldo Carvalho de Melo 2545d2cd909SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, struct perf_evsel *evsel, 2555d2cd909SArnaldo Carvalho de Melo int cpu, int prot, int mask, int fd) 256f8a95309SArnaldo Carvalho de Melo { 257f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].prev = 0; 258f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].mask = mask; 259f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].base = mmap(NULL, evlist->mmap_len, prot, 260f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 2615d2cd909SArnaldo Carvalho de Melo if (evlist->mmap[cpu].base == MAP_FAILED) { 2625d2cd909SArnaldo Carvalho de Melo if (evlist->cpus->map[cpu] == -1 && evsel->attr.inherit) 2635d2cd909SArnaldo Carvalho de Melo ui__warning("Inherit is not allowed on per-task " 2645d2cd909SArnaldo Carvalho de Melo "events using mmap.\n"); 265f8a95309SArnaldo Carvalho de Melo return -1; 2665d2cd909SArnaldo Carvalho de Melo } 267f8a95309SArnaldo Carvalho de Melo 268f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 269f8a95309SArnaldo Carvalho de Melo return 0; 270f8a95309SArnaldo Carvalho de Melo } 271f8a95309SArnaldo Carvalho de Melo 272f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 273f8a95309SArnaldo Carvalho de Melo * 274f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 275f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 276f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 277f8a95309SArnaldo Carvalho de Melo * 278f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 279f8a95309SArnaldo Carvalho de Melo * 280f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 281f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 282f8a95309SArnaldo Carvalho de Melo * 283f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 2847e2ed097SArnaldo Carvalho de Melo * 2857e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 286f8a95309SArnaldo Carvalho de Melo */ 2877e2ed097SArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite) 288f8a95309SArnaldo Carvalho de Melo { 289f8a95309SArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 290f8a95309SArnaldo Carvalho de Melo int mask = pages * page_size - 1, cpu; 291f8a95309SArnaldo Carvalho de Melo struct perf_evsel *first_evsel, *evsel; 2927e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 2937e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 294f8a95309SArnaldo Carvalho de Melo int thread, prot = PROT_READ | (overwrite ? 0 : PROT_WRITE); 295f8a95309SArnaldo Carvalho de Melo 2967e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 297f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 298f8a95309SArnaldo Carvalho de Melo 2997e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 300f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 301f8a95309SArnaldo Carvalho de Melo 302f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 303f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 304f8a95309SArnaldo Carvalho de Melo first_evsel = list_entry(evlist->entries.next, struct perf_evsel, node); 305f8a95309SArnaldo Carvalho de Melo 306f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 307f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 308a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 309f8a95309SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) 310f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 311f8a95309SArnaldo Carvalho de Melo 312f8a95309SArnaldo Carvalho de Melo for (cpu = 0; cpu < cpus->nr; cpu++) { 313f8a95309SArnaldo Carvalho de Melo for (thread = 0; thread < threads->nr; thread++) { 314f8a95309SArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 315f8a95309SArnaldo Carvalho de Melo 316f8a95309SArnaldo Carvalho de Melo if (evsel->idx || thread) { 317f8a95309SArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, 318f8a95309SArnaldo Carvalho de Melo FD(first_evsel, cpu, 0)) != 0) 319f8a95309SArnaldo Carvalho de Melo goto out_unmap; 3205d2cd909SArnaldo Carvalho de Melo } else if (__perf_evlist__mmap(evlist, evsel, cpu, 3215d2cd909SArnaldo Carvalho de Melo prot, mask, fd) < 0) 322f8a95309SArnaldo Carvalho de Melo goto out_unmap; 323f8a95309SArnaldo Carvalho de Melo 324f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 325a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 326f8a95309SArnaldo Carvalho de Melo goto out_unmap; 327f8a95309SArnaldo Carvalho de Melo } 328f8a95309SArnaldo Carvalho de Melo } 329f8a95309SArnaldo Carvalho de Melo } 330f8a95309SArnaldo Carvalho de Melo 331f8a95309SArnaldo Carvalho de Melo return 0; 332f8a95309SArnaldo Carvalho de Melo 333f8a95309SArnaldo Carvalho de Melo out_unmap: 334f8a95309SArnaldo Carvalho de Melo for (cpu = 0; cpu < cpus->nr; cpu++) { 335f8a95309SArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 336f8a95309SArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 337f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 338f8a95309SArnaldo Carvalho de Melo } 339f8a95309SArnaldo Carvalho de Melo } 340f8a95309SArnaldo Carvalho de Melo return -1; 341f8a95309SArnaldo Carvalho de Melo } 3427e2ed097SArnaldo Carvalho de Melo 3437e2ed097SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid, 3447e2ed097SArnaldo Carvalho de Melo pid_t target_tid, const char *cpu_list) 3457e2ed097SArnaldo Carvalho de Melo { 3467e2ed097SArnaldo Carvalho de Melo evlist->threads = thread_map__new(target_pid, target_tid); 3477e2ed097SArnaldo Carvalho de Melo 3487e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 3497e2ed097SArnaldo Carvalho de Melo return -1; 3507e2ed097SArnaldo Carvalho de Melo 3517e2ed097SArnaldo Carvalho de Melo if (target_tid != -1) 3527e2ed097SArnaldo Carvalho de Melo evlist->cpus = cpu_map__dummy_new(); 3537e2ed097SArnaldo Carvalho de Melo else 3547e2ed097SArnaldo Carvalho de Melo evlist->cpus = cpu_map__new(cpu_list); 3557e2ed097SArnaldo Carvalho de Melo 3567e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 3577e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 3587e2ed097SArnaldo Carvalho de Melo 3597e2ed097SArnaldo Carvalho de Melo return 0; 3607e2ed097SArnaldo Carvalho de Melo 3617e2ed097SArnaldo Carvalho de Melo out_delete_threads: 3627e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 3637e2ed097SArnaldo Carvalho de Melo return -1; 3647e2ed097SArnaldo Carvalho de Melo } 3657e2ed097SArnaldo Carvalho de Melo 3667e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 3677e2ed097SArnaldo Carvalho de Melo { 3687e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 3697e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 3707e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 3717e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 3727e2ed097SArnaldo Carvalho de Melo } 3730a102479SFrederic Weisbecker 3740a102479SFrederic Weisbecker int perf_evlist__set_filters(struct perf_evlist *evlist) 3750a102479SFrederic Weisbecker { 3760a102479SFrederic Weisbecker const struct thread_map *threads = evlist->threads; 3770a102479SFrederic Weisbecker const struct cpu_map *cpus = evlist->cpus; 3780a102479SFrederic Weisbecker struct perf_evsel *evsel; 3790a102479SFrederic Weisbecker char *filter; 3800a102479SFrederic Weisbecker int thread; 3810a102479SFrederic Weisbecker int cpu; 3820a102479SFrederic Weisbecker int err; 3830a102479SFrederic Weisbecker int fd; 3840a102479SFrederic Weisbecker 3850a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 3860a102479SFrederic Weisbecker filter = evsel->filter; 3870a102479SFrederic Weisbecker if (!filter) 3880a102479SFrederic Weisbecker continue; 3890a102479SFrederic Weisbecker for (cpu = 0; cpu < cpus->nr; cpu++) { 3900a102479SFrederic Weisbecker for (thread = 0; thread < threads->nr; thread++) { 3910a102479SFrederic Weisbecker fd = FD(evsel, cpu, thread); 3920a102479SFrederic Weisbecker err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); 3930a102479SFrederic Weisbecker if (err) 3940a102479SFrederic Weisbecker return err; 3950a102479SFrederic Weisbecker } 3960a102479SFrederic Weisbecker } 3970a102479SFrederic Weisbecker } 3980a102479SFrederic Weisbecker 3990a102479SFrederic Weisbecker return 0; 4000a102479SFrederic Weisbecker } 40174429964SFrederic Weisbecker 40274429964SFrederic Weisbecker u64 perf_evlist__sample_type(struct perf_evlist *evlist) 40374429964SFrederic Weisbecker { 40474429964SFrederic Weisbecker struct perf_evsel *pos; 40574429964SFrederic Weisbecker u64 type = 0; 40674429964SFrederic Weisbecker 40774429964SFrederic Weisbecker list_for_each_entry(pos, &evlist->entries, node) { 40874429964SFrederic Weisbecker if (!type) 40974429964SFrederic Weisbecker type = pos->attr.sample_type; 41074429964SFrederic Weisbecker else if (type != pos->attr.sample_type) 41174429964SFrederic Weisbecker die("non matching sample_type"); 41274429964SFrederic Weisbecker } 41374429964SFrederic Weisbecker 41474429964SFrederic Weisbecker return type; 41574429964SFrederic Weisbecker } 41674429964SFrederic Weisbecker 41774429964SFrederic Weisbecker bool perf_evlist__sample_id_all(const struct perf_evlist *evlist) 41874429964SFrederic Weisbecker { 41974429964SFrederic Weisbecker bool value = false, first = true; 42074429964SFrederic Weisbecker struct perf_evsel *pos; 42174429964SFrederic Weisbecker 42274429964SFrederic Weisbecker list_for_each_entry(pos, &evlist->entries, node) { 42374429964SFrederic Weisbecker if (first) { 42474429964SFrederic Weisbecker value = pos->attr.sample_id_all; 42574429964SFrederic Weisbecker first = false; 42674429964SFrederic Weisbecker } else if (value != pos->attr.sample_id_all) 42774429964SFrederic Weisbecker die("non matching sample_id_all"); 42874429964SFrederic Weisbecker } 42974429964SFrederic Weisbecker 43074429964SFrederic Weisbecker return value; 43174429964SFrederic Weisbecker } 432