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 169aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 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); 173aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 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 { 238aece948fSArnaldo Carvalho de Melo int i; 239f8a95309SArnaldo Carvalho de Melo 240aece948fSArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 241aece948fSArnaldo Carvalho de Melo if (evlist->mmap[i].base != NULL) { 242aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[i].base, evlist->mmap_len); 243aece948fSArnaldo Carvalho de Melo evlist->mmap[i].base = NULL; 244f8a95309SArnaldo Carvalho de Melo } 245f8a95309SArnaldo Carvalho de Melo } 246aece948fSArnaldo Carvalho de Melo 247aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 248aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 249f8a95309SArnaldo Carvalho de Melo } 250f8a95309SArnaldo Carvalho de Melo 2517e2ed097SArnaldo Carvalho de Melo int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 252f8a95309SArnaldo Carvalho de Melo { 253aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->cpus->nr; 254aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[0] == -1) 255aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->threads->nr; 256aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 257f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 258f8a95309SArnaldo Carvalho de Melo } 259f8a95309SArnaldo Carvalho de Melo 2605d2cd909SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, struct perf_evsel *evsel, 261aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 262f8a95309SArnaldo Carvalho de Melo { 263aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 264aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 265aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 266f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 267aece948fSArnaldo Carvalho de Melo if (evlist->mmap[idx].base == MAP_FAILED) { 268aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[idx] == -1 && evsel->attr.inherit) 2695d2cd909SArnaldo Carvalho de Melo ui__warning("Inherit is not allowed on per-task " 2705d2cd909SArnaldo Carvalho de Melo "events using mmap.\n"); 271f8a95309SArnaldo Carvalho de Melo return -1; 2725d2cd909SArnaldo Carvalho de Melo } 273f8a95309SArnaldo Carvalho de Melo 274f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 275f8a95309SArnaldo Carvalho de Melo return 0; 276f8a95309SArnaldo Carvalho de Melo } 277f8a95309SArnaldo Carvalho de Melo 278aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 279aece948fSArnaldo Carvalho de Melo { 280aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 281aece948fSArnaldo Carvalho de Melo int cpu, thread; 282aece948fSArnaldo Carvalho de Melo 283aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 284aece948fSArnaldo Carvalho de Melo int output = -1; 285aece948fSArnaldo Carvalho de Melo 286aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 287aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 288aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 289aece948fSArnaldo Carvalho de Melo 290aece948fSArnaldo Carvalho de Melo if (output == -1) { 291aece948fSArnaldo Carvalho de Melo output = fd; 292aece948fSArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, evsel, cpu, 293aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 294aece948fSArnaldo Carvalho de Melo goto out_unmap; 295aece948fSArnaldo Carvalho de Melo } else { 296aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 297aece948fSArnaldo Carvalho de Melo goto out_unmap; 298aece948fSArnaldo Carvalho de Melo } 299aece948fSArnaldo Carvalho de Melo 300aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 301aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 302aece948fSArnaldo Carvalho de Melo goto out_unmap; 303aece948fSArnaldo Carvalho de Melo } 304aece948fSArnaldo Carvalho de Melo } 305aece948fSArnaldo Carvalho de Melo } 306aece948fSArnaldo Carvalho de Melo 307aece948fSArnaldo Carvalho de Melo return 0; 308aece948fSArnaldo Carvalho de Melo 309aece948fSArnaldo Carvalho de Melo out_unmap: 310aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 311aece948fSArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 312aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 313aece948fSArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 314aece948fSArnaldo Carvalho de Melo } 315aece948fSArnaldo Carvalho de Melo } 316aece948fSArnaldo Carvalho de Melo return -1; 317aece948fSArnaldo Carvalho de Melo } 318aece948fSArnaldo Carvalho de Melo 319aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 320aece948fSArnaldo Carvalho de Melo { 321aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 322aece948fSArnaldo Carvalho de Melo int thread; 323aece948fSArnaldo Carvalho de Melo 324aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 325aece948fSArnaldo Carvalho de Melo int output = -1; 326aece948fSArnaldo Carvalho de Melo 327aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 328aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 329aece948fSArnaldo Carvalho de Melo 330aece948fSArnaldo Carvalho de Melo if (output == -1) { 331aece948fSArnaldo Carvalho de Melo output = fd; 332aece948fSArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, evsel, thread, 333aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 334aece948fSArnaldo Carvalho de Melo goto out_unmap; 335aece948fSArnaldo Carvalho de Melo } else { 336aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 337aece948fSArnaldo Carvalho de Melo goto out_unmap; 338aece948fSArnaldo Carvalho de Melo } 339aece948fSArnaldo Carvalho de Melo 340aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 341aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 342aece948fSArnaldo Carvalho de Melo goto out_unmap; 343aece948fSArnaldo Carvalho de Melo } 344aece948fSArnaldo Carvalho de Melo } 345aece948fSArnaldo Carvalho de Melo 346aece948fSArnaldo Carvalho de Melo return 0; 347aece948fSArnaldo Carvalho de Melo 348aece948fSArnaldo Carvalho de Melo out_unmap: 349aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 350aece948fSArnaldo Carvalho de Melo if (evlist->mmap[thread].base != NULL) { 351aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[thread].base, evlist->mmap_len); 352aece948fSArnaldo Carvalho de Melo evlist->mmap[thread].base = NULL; 353aece948fSArnaldo Carvalho de Melo } 354aece948fSArnaldo Carvalho de Melo } 355aece948fSArnaldo Carvalho de Melo return -1; 356aece948fSArnaldo Carvalho de Melo } 357aece948fSArnaldo Carvalho de Melo 358f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 359f8a95309SArnaldo Carvalho de Melo * 360f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 361f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 362f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 363f8a95309SArnaldo Carvalho de Melo * 364f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 365f8a95309SArnaldo Carvalho de Melo * 366f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 367f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 368f8a95309SArnaldo Carvalho de Melo * 369f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 3707e2ed097SArnaldo Carvalho de Melo * 3717e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 372f8a95309SArnaldo Carvalho de Melo */ 3737e2ed097SArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite) 374f8a95309SArnaldo Carvalho de Melo { 375f8a95309SArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 376aece948fSArnaldo Carvalho de Melo int mask = pages * page_size - 1; 377aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 3787e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 3797e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 380aece948fSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE); 381f8a95309SArnaldo Carvalho de Melo 3827e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 383f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 384f8a95309SArnaldo Carvalho de Melo 3857e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 386f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 387f8a95309SArnaldo Carvalho de Melo 388f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 389f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 390f8a95309SArnaldo Carvalho de Melo 391f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 392f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 393a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 394f8a95309SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) 395f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 396f8a95309SArnaldo Carvalho de Melo } 397f8a95309SArnaldo Carvalho de Melo 398aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[0] == -1) 399aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 400f8a95309SArnaldo Carvalho de Melo 401aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 402f8a95309SArnaldo Carvalho de Melo } 4037e2ed097SArnaldo Carvalho de Melo 4047e2ed097SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid, 4057e2ed097SArnaldo Carvalho de Melo pid_t target_tid, const char *cpu_list) 4067e2ed097SArnaldo Carvalho de Melo { 4077e2ed097SArnaldo Carvalho de Melo evlist->threads = thread_map__new(target_pid, target_tid); 4087e2ed097SArnaldo Carvalho de Melo 4097e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 4107e2ed097SArnaldo Carvalho de Melo return -1; 4117e2ed097SArnaldo Carvalho de Melo 412b9019418SArnaldo Carvalho de Melo if (cpu_list == NULL && target_tid != -1) 4137e2ed097SArnaldo Carvalho de Melo evlist->cpus = cpu_map__dummy_new(); 4147e2ed097SArnaldo Carvalho de Melo else 4157e2ed097SArnaldo Carvalho de Melo evlist->cpus = cpu_map__new(cpu_list); 4167e2ed097SArnaldo Carvalho de Melo 4177e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 4187e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 4197e2ed097SArnaldo Carvalho de Melo 4207e2ed097SArnaldo Carvalho de Melo return 0; 4217e2ed097SArnaldo Carvalho de Melo 4227e2ed097SArnaldo Carvalho de Melo out_delete_threads: 4237e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 4247e2ed097SArnaldo Carvalho de Melo return -1; 4257e2ed097SArnaldo Carvalho de Melo } 4267e2ed097SArnaldo Carvalho de Melo 4277e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 4287e2ed097SArnaldo Carvalho de Melo { 4297e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 4307e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 4317e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 4327e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 4337e2ed097SArnaldo Carvalho de Melo } 4340a102479SFrederic Weisbecker 4350a102479SFrederic Weisbecker int perf_evlist__set_filters(struct perf_evlist *evlist) 4360a102479SFrederic Weisbecker { 4370a102479SFrederic Weisbecker const struct thread_map *threads = evlist->threads; 4380a102479SFrederic Weisbecker const struct cpu_map *cpus = evlist->cpus; 4390a102479SFrederic Weisbecker struct perf_evsel *evsel; 4400a102479SFrederic Weisbecker char *filter; 4410a102479SFrederic Weisbecker int thread; 4420a102479SFrederic Weisbecker int cpu; 4430a102479SFrederic Weisbecker int err; 4440a102479SFrederic Weisbecker int fd; 4450a102479SFrederic Weisbecker 4460a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 4470a102479SFrederic Weisbecker filter = evsel->filter; 4480a102479SFrederic Weisbecker if (!filter) 4490a102479SFrederic Weisbecker continue; 4500a102479SFrederic Weisbecker for (cpu = 0; cpu < cpus->nr; cpu++) { 4510a102479SFrederic Weisbecker for (thread = 0; thread < threads->nr; thread++) { 4520a102479SFrederic Weisbecker fd = FD(evsel, cpu, thread); 4530a102479SFrederic Weisbecker err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); 4540a102479SFrederic Weisbecker if (err) 4550a102479SFrederic Weisbecker return err; 4560a102479SFrederic Weisbecker } 4570a102479SFrederic Weisbecker } 4580a102479SFrederic Weisbecker } 4590a102479SFrederic Weisbecker 4600a102479SFrederic Weisbecker return 0; 4610a102479SFrederic Weisbecker } 462