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" 15361c99a6SArnaldo Carvalho de Melo 16f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 17f8a95309SArnaldo Carvalho de Melo 1870db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 1970db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 2070db7533SArnaldo Carvalho de Melo 21f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 22a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 23f8a95309SArnaldo Carvalho de Melo 247e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 257e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 26361c99a6SArnaldo Carvalho de Melo { 2770db7533SArnaldo Carvalho de Melo int i; 2870db7533SArnaldo Carvalho de Melo 2970db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 3070db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 31361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 327e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 33361c99a6SArnaldo Carvalho de Melo } 34361c99a6SArnaldo Carvalho de Melo 357e2ed097SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new(struct cpu_map *cpus, 367e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 37ef1d1af2SArnaldo Carvalho de Melo { 38ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 39ef1d1af2SArnaldo Carvalho de Melo 40ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 417e2ed097SArnaldo Carvalho de Melo perf_evlist__init(evlist, cpus, threads); 42ef1d1af2SArnaldo Carvalho de Melo 43361c99a6SArnaldo Carvalho de Melo return evlist; 44361c99a6SArnaldo Carvalho de Melo } 45361c99a6SArnaldo Carvalho de Melo 46361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 47361c99a6SArnaldo Carvalho de Melo { 48361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 49361c99a6SArnaldo Carvalho de Melo 50361c99a6SArnaldo Carvalho de Melo list_for_each_entry_safe(pos, n, &evlist->entries, node) { 51361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 52361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 53361c99a6SArnaldo Carvalho de Melo } 54361c99a6SArnaldo Carvalho de Melo 55361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 56361c99a6SArnaldo Carvalho de Melo } 57361c99a6SArnaldo Carvalho de Melo 58ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 59ef1d1af2SArnaldo Carvalho de Melo { 60ef1d1af2SArnaldo Carvalho de Melo free(evlist->mmap); 61ef1d1af2SArnaldo Carvalho de Melo free(evlist->pollfd); 62ef1d1af2SArnaldo Carvalho de Melo evlist->mmap = NULL; 63ef1d1af2SArnaldo Carvalho de Melo evlist->pollfd = NULL; 64ef1d1af2SArnaldo Carvalho de Melo } 65ef1d1af2SArnaldo Carvalho de Melo 66361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 67361c99a6SArnaldo Carvalho de Melo { 68361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 69ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 70361c99a6SArnaldo Carvalho de Melo free(evlist); 71361c99a6SArnaldo Carvalho de Melo } 72361c99a6SArnaldo Carvalho de Melo 73361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 74361c99a6SArnaldo Carvalho de Melo { 75361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 76361c99a6SArnaldo Carvalho de Melo ++evlist->nr_entries; 77361c99a6SArnaldo Carvalho de Melo } 78361c99a6SArnaldo Carvalho de Melo 79361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 80361c99a6SArnaldo Carvalho de Melo { 81361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 82361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 83361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 84361c99a6SArnaldo Carvalho de Melo }; 85361c99a6SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new(&attr, 0); 86361c99a6SArnaldo Carvalho de Melo 87361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 88cc2d86b0SStephane Eranian goto error; 89cc2d86b0SStephane Eranian 90cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 91cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 92cc2d86b0SStephane Eranian if (!evsel->name) 93cc2d86b0SStephane Eranian goto error_free; 94361c99a6SArnaldo Carvalho de Melo 95361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 96361c99a6SArnaldo Carvalho de Melo return 0; 97cc2d86b0SStephane Eranian error_free: 98cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 99cc2d86b0SStephane Eranian error: 100cc2d86b0SStephane Eranian return -ENOMEM; 101361c99a6SArnaldo Carvalho de Melo } 1025c581041SArnaldo Carvalho de Melo 1034152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 1044152ab37SArnaldo Carvalho de Melo { 1054152ab37SArnaldo Carvalho de Melo int cpu, thread; 1064152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 1074152ab37SArnaldo Carvalho de Melo 1084152ab37SArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 1094152ab37SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 1104152ab37SArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) 1114152ab37SArnaldo Carvalho de Melo ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_DISABLE); 1124152ab37SArnaldo Carvalho de Melo } 1134152ab37SArnaldo Carvalho de Melo } 1144152ab37SArnaldo Carvalho de Melo } 1154152ab37SArnaldo Carvalho de Melo 116764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 117764e16a3SDavid Ahern { 118764e16a3SDavid Ahern int cpu, thread; 119764e16a3SDavid Ahern struct perf_evsel *pos; 120764e16a3SDavid Ahern 121764e16a3SDavid Ahern for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 122764e16a3SDavid Ahern list_for_each_entry(pos, &evlist->entries, node) { 123764e16a3SDavid Ahern for (thread = 0; thread < evlist->threads->nr; thread++) 124764e16a3SDavid Ahern ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_ENABLE); 125764e16a3SDavid Ahern } 126764e16a3SDavid Ahern } 127764e16a3SDavid Ahern } 128764e16a3SDavid Ahern 1297e2ed097SArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 1305c581041SArnaldo Carvalho de Melo { 1317e2ed097SArnaldo Carvalho de Melo int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries; 1325c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 1335c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 1345c581041SArnaldo Carvalho de Melo } 13570082dd9SArnaldo Carvalho de Melo 13670082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 13770082dd9SArnaldo Carvalho de Melo { 13870082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 13970082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 14070082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 14170082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 14270082dd9SArnaldo Carvalho de Melo } 14370db7533SArnaldo Carvalho de Melo 144a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 145a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 1463d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 1473d3b5e95SArnaldo Carvalho de Melo { 1483d3b5e95SArnaldo Carvalho de Melo int hash; 1493d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 1503d3b5e95SArnaldo Carvalho de Melo 1513d3b5e95SArnaldo Carvalho de Melo sid->id = id; 1523d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 1533d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 1543d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 1553d3b5e95SArnaldo Carvalho de Melo } 1563d3b5e95SArnaldo Carvalho de Melo 157a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 158a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 159a91e5431SArnaldo Carvalho de Melo { 160a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 161a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 162a91e5431SArnaldo Carvalho de Melo } 163a91e5431SArnaldo Carvalho de Melo 164a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 1653d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 166f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 167f8a95309SArnaldo Carvalho de Melo { 168f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 1693d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 170f8a95309SArnaldo Carvalho de Melo 171f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 172f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 173f8a95309SArnaldo Carvalho de Melo return -1; 174f8a95309SArnaldo Carvalho de Melo 175f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 176f8a95309SArnaldo Carvalho de Melo ++id_idx; 177f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 178f8a95309SArnaldo Carvalho de Melo ++id_idx; 179f8a95309SArnaldo Carvalho de Melo 180a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 181f8a95309SArnaldo Carvalho de Melo return 0; 182f8a95309SArnaldo Carvalho de Melo } 183f8a95309SArnaldo Carvalho de Melo 18470db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 18570db7533SArnaldo Carvalho de Melo { 18670db7533SArnaldo Carvalho de Melo struct hlist_head *head; 18770db7533SArnaldo Carvalho de Melo struct hlist_node *pos; 18870db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 18970db7533SArnaldo Carvalho de Melo int hash; 19070db7533SArnaldo Carvalho de Melo 19170db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 19270db7533SArnaldo Carvalho de Melo return list_entry(evlist->entries.next, struct perf_evsel, node); 19370db7533SArnaldo Carvalho de Melo 19470db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 19570db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 19670db7533SArnaldo Carvalho de Melo 19770db7533SArnaldo Carvalho de Melo hlist_for_each_entry(sid, pos, head, node) 19870db7533SArnaldo Carvalho de Melo if (sid->id == id) 19970db7533SArnaldo Carvalho de Melo return sid->evsel; 20070db7533SArnaldo Carvalho de Melo return NULL; 20170db7533SArnaldo Carvalho de Melo } 20204391debSArnaldo Carvalho de Melo 203aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 20404391debSArnaldo Carvalho de Melo { 20504391debSArnaldo Carvalho de Melo /* XXX Move this to perf.c, making it generally available */ 20604391debSArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 207aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 20804391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 20904391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 21004391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 2118115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 21204391debSArnaldo Carvalho de Melo 2137bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 21404391debSArnaldo Carvalho de Melo /* 21504391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 21604391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 21704391debSArnaldo Carvalho de Melo * 21804391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 21904391debSArnaldo Carvalho de Melo * 22004391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 22104391debSArnaldo Carvalho de Melo */ 2227bb41152SArnaldo Carvalho de Melo int diff = head - old; 22304391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 22404391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 22504391debSArnaldo Carvalho de Melo 22604391debSArnaldo Carvalho de Melo /* 22704391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 22804391debSArnaldo Carvalho de Melo */ 22904391debSArnaldo Carvalho de Melo old = head; 23004391debSArnaldo Carvalho de Melo } 2317bb41152SArnaldo Carvalho de Melo } 23204391debSArnaldo Carvalho de Melo 23304391debSArnaldo Carvalho de Melo if (old != head) { 23404391debSArnaldo Carvalho de Melo size_t size; 23504391debSArnaldo Carvalho de Melo 2368115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 23704391debSArnaldo Carvalho de Melo size = event->header.size; 23804391debSArnaldo Carvalho de Melo 23904391debSArnaldo Carvalho de Melo /* 24004391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 24104391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 24204391debSArnaldo Carvalho de Melo */ 24304391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 24404391debSArnaldo Carvalho de Melo unsigned int offset = old; 24504391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 24604391debSArnaldo Carvalho de Melo void *dst = &evlist->event_copy; 24704391debSArnaldo Carvalho de Melo 24804391debSArnaldo Carvalho de Melo do { 24904391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 25004391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 25104391debSArnaldo Carvalho de Melo offset += cpy; 25204391debSArnaldo Carvalho de Melo dst += cpy; 25304391debSArnaldo Carvalho de Melo len -= cpy; 25404391debSArnaldo Carvalho de Melo } while (len); 25504391debSArnaldo Carvalho de Melo 25604391debSArnaldo Carvalho de Melo event = &evlist->event_copy; 25704391debSArnaldo Carvalho de Melo } 25804391debSArnaldo Carvalho de Melo 25904391debSArnaldo Carvalho de Melo old += size; 26004391debSArnaldo Carvalho de Melo } 26104391debSArnaldo Carvalho de Melo 26204391debSArnaldo Carvalho de Melo md->prev = old; 2637bb41152SArnaldo Carvalho de Melo 2647bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 2657bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 2667bb41152SArnaldo Carvalho de Melo 26704391debSArnaldo Carvalho de Melo return event; 26804391debSArnaldo Carvalho de Melo } 269f8a95309SArnaldo Carvalho de Melo 2707e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 271f8a95309SArnaldo Carvalho de Melo { 272aece948fSArnaldo Carvalho de Melo int i; 273f8a95309SArnaldo Carvalho de Melo 274aece948fSArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 275aece948fSArnaldo Carvalho de Melo if (evlist->mmap[i].base != NULL) { 276aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[i].base, evlist->mmap_len); 277aece948fSArnaldo Carvalho de Melo evlist->mmap[i].base = NULL; 278f8a95309SArnaldo Carvalho de Melo } 279f8a95309SArnaldo Carvalho de Melo } 280aece948fSArnaldo Carvalho de Melo 281aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 282aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 283f8a95309SArnaldo Carvalho de Melo } 284f8a95309SArnaldo Carvalho de Melo 2857e2ed097SArnaldo Carvalho de Melo int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 286f8a95309SArnaldo Carvalho de Melo { 287aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->cpus->nr; 288aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[0] == -1) 289aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->threads->nr; 290aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 291f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 292f8a95309SArnaldo Carvalho de Melo } 293f8a95309SArnaldo Carvalho de Melo 294bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 295aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 296f8a95309SArnaldo Carvalho de Melo { 297aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 298aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 299aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 300f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 301bccdaba0SArnaldo Carvalho de Melo if (evlist->mmap[idx].base == MAP_FAILED) 302f8a95309SArnaldo Carvalho de Melo return -1; 303f8a95309SArnaldo Carvalho de Melo 304f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 305f8a95309SArnaldo Carvalho de Melo return 0; 306f8a95309SArnaldo Carvalho de Melo } 307f8a95309SArnaldo Carvalho de Melo 308aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 309aece948fSArnaldo Carvalho de Melo { 310aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 311aece948fSArnaldo Carvalho de Melo int cpu, thread; 312aece948fSArnaldo Carvalho de Melo 313aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 314aece948fSArnaldo Carvalho de Melo int output = -1; 315aece948fSArnaldo Carvalho de Melo 316aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 317aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 318aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 319aece948fSArnaldo Carvalho de Melo 320aece948fSArnaldo Carvalho de Melo if (output == -1) { 321aece948fSArnaldo Carvalho de Melo output = fd; 322bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 323aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 324aece948fSArnaldo Carvalho de Melo goto out_unmap; 325aece948fSArnaldo Carvalho de Melo } else { 326aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 327aece948fSArnaldo Carvalho de Melo goto out_unmap; 328aece948fSArnaldo Carvalho de Melo } 329aece948fSArnaldo Carvalho de Melo 330aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 331aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 332aece948fSArnaldo Carvalho de Melo goto out_unmap; 333aece948fSArnaldo Carvalho de Melo } 334aece948fSArnaldo Carvalho de Melo } 335aece948fSArnaldo Carvalho de Melo } 336aece948fSArnaldo Carvalho de Melo 337aece948fSArnaldo Carvalho de Melo return 0; 338aece948fSArnaldo Carvalho de Melo 339aece948fSArnaldo Carvalho de Melo out_unmap: 340aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 341aece948fSArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 342aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 343aece948fSArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 344aece948fSArnaldo Carvalho de Melo } 345aece948fSArnaldo Carvalho de Melo } 346aece948fSArnaldo Carvalho de Melo return -1; 347aece948fSArnaldo Carvalho de Melo } 348aece948fSArnaldo Carvalho de Melo 349aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 350aece948fSArnaldo Carvalho de Melo { 351aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 352aece948fSArnaldo Carvalho de Melo int thread; 353aece948fSArnaldo Carvalho de Melo 354aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 355aece948fSArnaldo Carvalho de Melo int output = -1; 356aece948fSArnaldo Carvalho de Melo 357aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 358aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 359aece948fSArnaldo Carvalho de Melo 360aece948fSArnaldo Carvalho de Melo if (output == -1) { 361aece948fSArnaldo Carvalho de Melo output = fd; 362bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 363aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 364aece948fSArnaldo Carvalho de Melo goto out_unmap; 365aece948fSArnaldo Carvalho de Melo } else { 366aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 367aece948fSArnaldo Carvalho de Melo goto out_unmap; 368aece948fSArnaldo Carvalho de Melo } 369aece948fSArnaldo Carvalho de Melo 370aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 371aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 372aece948fSArnaldo Carvalho de Melo goto out_unmap; 373aece948fSArnaldo Carvalho de Melo } 374aece948fSArnaldo Carvalho de Melo } 375aece948fSArnaldo Carvalho de Melo 376aece948fSArnaldo Carvalho de Melo return 0; 377aece948fSArnaldo Carvalho de Melo 378aece948fSArnaldo Carvalho de Melo out_unmap: 379aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 380aece948fSArnaldo Carvalho de Melo if (evlist->mmap[thread].base != NULL) { 381aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[thread].base, evlist->mmap_len); 382aece948fSArnaldo Carvalho de Melo evlist->mmap[thread].base = NULL; 383aece948fSArnaldo Carvalho de Melo } 384aece948fSArnaldo Carvalho de Melo } 385aece948fSArnaldo Carvalho de Melo return -1; 386aece948fSArnaldo Carvalho de Melo } 387aece948fSArnaldo Carvalho de Melo 388f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 389f8a95309SArnaldo Carvalho de Melo * 390f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 391f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 392f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 393f8a95309SArnaldo Carvalho de Melo * 394f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 395f8a95309SArnaldo Carvalho de Melo * 396f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 397f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 398f8a95309SArnaldo Carvalho de Melo * 399f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 4007e2ed097SArnaldo Carvalho de Melo * 4017e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 402f8a95309SArnaldo Carvalho de Melo */ 4037e2ed097SArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite) 404f8a95309SArnaldo Carvalho de Melo { 405f8a95309SArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 406aece948fSArnaldo Carvalho de Melo int mask = pages * page_size - 1; 407aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 4087e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 4097e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 410aece948fSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE); 411f8a95309SArnaldo Carvalho de Melo 4127e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 413f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 414f8a95309SArnaldo Carvalho de Melo 4157e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 416f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 417f8a95309SArnaldo Carvalho de Melo 418f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 419f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 420f8a95309SArnaldo Carvalho de Melo 421f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 422f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 423a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 424f8a95309SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) 425f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 426f8a95309SArnaldo Carvalho de Melo } 427f8a95309SArnaldo Carvalho de Melo 428aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[0] == -1) 429aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 430f8a95309SArnaldo Carvalho de Melo 431aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 432f8a95309SArnaldo Carvalho de Melo } 4337e2ed097SArnaldo Carvalho de Melo 4347e2ed097SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid, 4357e2ed097SArnaldo Carvalho de Melo pid_t target_tid, const char *cpu_list) 4367e2ed097SArnaldo Carvalho de Melo { 4377e2ed097SArnaldo Carvalho de Melo evlist->threads = thread_map__new(target_pid, target_tid); 4387e2ed097SArnaldo Carvalho de Melo 4397e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 4407e2ed097SArnaldo Carvalho de Melo return -1; 4417e2ed097SArnaldo Carvalho de Melo 442b9019418SArnaldo Carvalho de Melo if (cpu_list == NULL && target_tid != -1) 4437e2ed097SArnaldo Carvalho de Melo evlist->cpus = cpu_map__dummy_new(); 4447e2ed097SArnaldo Carvalho de Melo else 4457e2ed097SArnaldo Carvalho de Melo evlist->cpus = cpu_map__new(cpu_list); 4467e2ed097SArnaldo Carvalho de Melo 4477e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 4487e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 4497e2ed097SArnaldo Carvalho de Melo 4507e2ed097SArnaldo Carvalho de Melo return 0; 4517e2ed097SArnaldo Carvalho de Melo 4527e2ed097SArnaldo Carvalho de Melo out_delete_threads: 4537e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 4547e2ed097SArnaldo Carvalho de Melo return -1; 4557e2ed097SArnaldo Carvalho de Melo } 4567e2ed097SArnaldo Carvalho de Melo 4577e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 4587e2ed097SArnaldo Carvalho de Melo { 4597e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 4607e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 4617e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 4627e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 4637e2ed097SArnaldo Carvalho de Melo } 4640a102479SFrederic Weisbecker 4650a102479SFrederic Weisbecker int perf_evlist__set_filters(struct perf_evlist *evlist) 4660a102479SFrederic Weisbecker { 4670a102479SFrederic Weisbecker const struct thread_map *threads = evlist->threads; 4680a102479SFrederic Weisbecker const struct cpu_map *cpus = evlist->cpus; 4690a102479SFrederic Weisbecker struct perf_evsel *evsel; 4700a102479SFrederic Weisbecker char *filter; 4710a102479SFrederic Weisbecker int thread; 4720a102479SFrederic Weisbecker int cpu; 4730a102479SFrederic Weisbecker int err; 4740a102479SFrederic Weisbecker int fd; 4750a102479SFrederic Weisbecker 4760a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 4770a102479SFrederic Weisbecker filter = evsel->filter; 4780a102479SFrederic Weisbecker if (!filter) 4790a102479SFrederic Weisbecker continue; 4800a102479SFrederic Weisbecker for (cpu = 0; cpu < cpus->nr; cpu++) { 4810a102479SFrederic Weisbecker for (thread = 0; thread < threads->nr; thread++) { 4820a102479SFrederic Weisbecker fd = FD(evsel, cpu, thread); 4830a102479SFrederic Weisbecker err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); 4840a102479SFrederic Weisbecker if (err) 4850a102479SFrederic Weisbecker return err; 4860a102479SFrederic Weisbecker } 4870a102479SFrederic Weisbecker } 4880a102479SFrederic Weisbecker } 4890a102479SFrederic Weisbecker 4900a102479SFrederic Weisbecker return 0; 4910a102479SFrederic Weisbecker } 49274429964SFrederic Weisbecker 493c2a70653SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist) 49474429964SFrederic Weisbecker { 495c2a70653SArnaldo Carvalho de Melo struct perf_evsel *pos, *first; 49674429964SFrederic Weisbecker 497c2a70653SArnaldo Carvalho de Melo pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); 498c2a70653SArnaldo Carvalho de Melo 499c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 500c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 501c2a70653SArnaldo Carvalho de Melo return false; 50274429964SFrederic Weisbecker } 50374429964SFrederic Weisbecker 504c2a70653SArnaldo Carvalho de Melo return true; 505c2a70653SArnaldo Carvalho de Melo } 506c2a70653SArnaldo Carvalho de Melo 507c2a70653SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(const struct perf_evlist *evlist) 508c2a70653SArnaldo Carvalho de Melo { 509c2a70653SArnaldo Carvalho de Melo struct perf_evsel *first; 510c2a70653SArnaldo Carvalho de Melo 511c2a70653SArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 512c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 513c2a70653SArnaldo Carvalho de Melo } 514c2a70653SArnaldo Carvalho de Melo 515c2a70653SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist) 516c2a70653SArnaldo Carvalho de Melo { 517c2a70653SArnaldo Carvalho de Melo struct perf_evsel *pos, *first; 518c2a70653SArnaldo Carvalho de Melo 519c2a70653SArnaldo Carvalho de Melo pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); 520c2a70653SArnaldo Carvalho de Melo 521c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 522c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 523c2a70653SArnaldo Carvalho de Melo return false; 524c2a70653SArnaldo Carvalho de Melo } 525c2a70653SArnaldo Carvalho de Melo 526c2a70653SArnaldo Carvalho de Melo return true; 52774429964SFrederic Weisbecker } 52874429964SFrederic Weisbecker 52974429964SFrederic Weisbecker bool perf_evlist__sample_id_all(const struct perf_evlist *evlist) 53074429964SFrederic Weisbecker { 531c2a70653SArnaldo Carvalho de Melo struct perf_evsel *first; 53274429964SFrederic Weisbecker 533c2a70653SArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 534c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 53574429964SFrederic Weisbecker } 53681cce8deSArnaldo Carvalho de Melo 53781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 53881cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 53981cce8deSArnaldo Carvalho de Melo { 54081cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 54181cce8deSArnaldo Carvalho de Melo } 542