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)) 22f8a95309SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->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) 88361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 89361c99a6SArnaldo Carvalho de Melo 90361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 91361c99a6SArnaldo Carvalho de Melo return 0; 92361c99a6SArnaldo Carvalho de Melo } 935c581041SArnaldo Carvalho de Melo 947e2ed097SArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 955c581041SArnaldo Carvalho de Melo { 967e2ed097SArnaldo Carvalho de Melo int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries; 975c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 985c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 995c581041SArnaldo Carvalho de Melo } 10070082dd9SArnaldo Carvalho de Melo 10170082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 10270082dd9SArnaldo Carvalho de Melo { 10370082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 10470082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 10570082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 10670082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 10770082dd9SArnaldo Carvalho de Melo } 10870db7533SArnaldo Carvalho de Melo 109f8a95309SArnaldo Carvalho de Melo static int perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel, 110f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 111f8a95309SArnaldo Carvalho de Melo { 112f8a95309SArnaldo Carvalho de Melo struct perf_sample_id *sid; 113f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 114f8a95309SArnaldo Carvalho de Melo int hash, id_idx = 1; /* The first entry is the counter value */ 115f8a95309SArnaldo Carvalho de Melo 116f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 117f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 118f8a95309SArnaldo Carvalho de Melo return -1; 119f8a95309SArnaldo Carvalho de Melo 120f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 121f8a95309SArnaldo Carvalho de Melo ++id_idx; 122f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 123f8a95309SArnaldo Carvalho de Melo ++id_idx; 124f8a95309SArnaldo Carvalho de Melo 125f8a95309SArnaldo Carvalho de Melo sid = SID(evsel, cpu, thread); 126f8a95309SArnaldo Carvalho de Melo sid->id = read_data[id_idx]; 127f8a95309SArnaldo Carvalho de Melo sid->evsel = evsel; 128f8a95309SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 129f8a95309SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 130f8a95309SArnaldo Carvalho de Melo return 0; 131f8a95309SArnaldo Carvalho de Melo } 132f8a95309SArnaldo Carvalho de Melo 13370db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 13470db7533SArnaldo Carvalho de Melo { 13570db7533SArnaldo Carvalho de Melo struct hlist_head *head; 13670db7533SArnaldo Carvalho de Melo struct hlist_node *pos; 13770db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 13870db7533SArnaldo Carvalho de Melo int hash; 13970db7533SArnaldo Carvalho de Melo 14070db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 14170db7533SArnaldo Carvalho de Melo return list_entry(evlist->entries.next, struct perf_evsel, node); 14270db7533SArnaldo Carvalho de Melo 14370db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 14470db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 14570db7533SArnaldo Carvalho de Melo 14670db7533SArnaldo Carvalho de Melo hlist_for_each_entry(sid, pos, head, node) 14770db7533SArnaldo Carvalho de Melo if (sid->id == id) 14870db7533SArnaldo Carvalho de Melo return sid->evsel; 14970db7533SArnaldo Carvalho de Melo return NULL; 15070db7533SArnaldo Carvalho de Melo } 15104391debSArnaldo Carvalho de Melo 1528115d60cSArnaldo Carvalho de Melo union perf_event *perf_evlist__read_on_cpu(struct perf_evlist *evlist, int cpu) 15304391debSArnaldo Carvalho de Melo { 15404391debSArnaldo Carvalho de Melo /* XXX Move this to perf.c, making it generally available */ 15504391debSArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 15604391debSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[cpu]; 15704391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 15804391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 15904391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 1608115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 16104391debSArnaldo Carvalho de Melo 1627bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 16304391debSArnaldo Carvalho de Melo /* 16404391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 16504391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 16604391debSArnaldo Carvalho de Melo * 16704391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 16804391debSArnaldo Carvalho de Melo * 16904391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 17004391debSArnaldo Carvalho de Melo */ 1717bb41152SArnaldo Carvalho de Melo int diff = head - old; 17204391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 17304391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 17404391debSArnaldo Carvalho de Melo 17504391debSArnaldo Carvalho de Melo /* 17604391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 17704391debSArnaldo Carvalho de Melo */ 17804391debSArnaldo Carvalho de Melo old = head; 17904391debSArnaldo Carvalho de Melo } 1807bb41152SArnaldo Carvalho de Melo } 18104391debSArnaldo Carvalho de Melo 18204391debSArnaldo Carvalho de Melo if (old != head) { 18304391debSArnaldo Carvalho de Melo size_t size; 18404391debSArnaldo Carvalho de Melo 1858115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 18604391debSArnaldo Carvalho de Melo size = event->header.size; 18704391debSArnaldo Carvalho de Melo 18804391debSArnaldo Carvalho de Melo /* 18904391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 19004391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 19104391debSArnaldo Carvalho de Melo */ 19204391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 19304391debSArnaldo Carvalho de Melo unsigned int offset = old; 19404391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 19504391debSArnaldo Carvalho de Melo void *dst = &evlist->event_copy; 19604391debSArnaldo Carvalho de Melo 19704391debSArnaldo Carvalho de Melo do { 19804391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 19904391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 20004391debSArnaldo Carvalho de Melo offset += cpy; 20104391debSArnaldo Carvalho de Melo dst += cpy; 20204391debSArnaldo Carvalho de Melo len -= cpy; 20304391debSArnaldo Carvalho de Melo } while (len); 20404391debSArnaldo Carvalho de Melo 20504391debSArnaldo Carvalho de Melo event = &evlist->event_copy; 20604391debSArnaldo Carvalho de Melo } 20704391debSArnaldo Carvalho de Melo 20804391debSArnaldo Carvalho de Melo old += size; 20904391debSArnaldo Carvalho de Melo } 21004391debSArnaldo Carvalho de Melo 21104391debSArnaldo Carvalho de Melo md->prev = old; 2127bb41152SArnaldo Carvalho de Melo 2137bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 2147bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 2157bb41152SArnaldo Carvalho de Melo 21604391debSArnaldo Carvalho de Melo return event; 21704391debSArnaldo Carvalho de Melo } 218f8a95309SArnaldo Carvalho de Melo 2197e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 220f8a95309SArnaldo Carvalho de Melo { 221f8a95309SArnaldo Carvalho de Melo int cpu; 222f8a95309SArnaldo Carvalho de Melo 2237e2ed097SArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 224f8a95309SArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 225f8a95309SArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 226f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 227f8a95309SArnaldo Carvalho de Melo } 228f8a95309SArnaldo Carvalho de Melo } 229f8a95309SArnaldo Carvalho de Melo } 230f8a95309SArnaldo Carvalho de Melo 2317e2ed097SArnaldo Carvalho de Melo int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 232f8a95309SArnaldo Carvalho de Melo { 2337e2ed097SArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->cpus->nr * sizeof(struct perf_mmap)); 234f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 235f8a95309SArnaldo Carvalho de Melo } 236f8a95309SArnaldo Carvalho de Melo 237f8a95309SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, int cpu, int prot, 238f8a95309SArnaldo Carvalho de Melo int mask, int fd) 239f8a95309SArnaldo Carvalho de Melo { 240f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].prev = 0; 241f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].mask = mask; 242f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].base = mmap(NULL, evlist->mmap_len, prot, 243f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 244f8a95309SArnaldo Carvalho de Melo if (evlist->mmap[cpu].base == MAP_FAILED) 245f8a95309SArnaldo Carvalho de Melo return -1; 246f8a95309SArnaldo Carvalho de Melo 247f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 248f8a95309SArnaldo Carvalho de Melo return 0; 249f8a95309SArnaldo Carvalho de Melo } 250f8a95309SArnaldo Carvalho de Melo 251f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 252f8a95309SArnaldo Carvalho de Melo * 253f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 254f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 255f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 256f8a95309SArnaldo Carvalho de Melo * 257f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 258f8a95309SArnaldo Carvalho de Melo * 259f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 260f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 261f8a95309SArnaldo Carvalho de Melo * 262f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 2637e2ed097SArnaldo Carvalho de Melo * 2647e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 265f8a95309SArnaldo Carvalho de Melo */ 2667e2ed097SArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite) 267f8a95309SArnaldo Carvalho de Melo { 268f8a95309SArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 269f8a95309SArnaldo Carvalho de Melo int mask = pages * page_size - 1, cpu; 270f8a95309SArnaldo Carvalho de Melo struct perf_evsel *first_evsel, *evsel; 2717e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 2727e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 273f8a95309SArnaldo Carvalho de Melo int thread, prot = PROT_READ | (overwrite ? 0 : PROT_WRITE); 274f8a95309SArnaldo Carvalho de Melo 2757e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 276f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 277f8a95309SArnaldo Carvalho de Melo 2787e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 279f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 280f8a95309SArnaldo Carvalho de Melo 281f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 282f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 283f8a95309SArnaldo Carvalho de Melo first_evsel = list_entry(evlist->entries.next, struct perf_evsel, node); 284f8a95309SArnaldo Carvalho de Melo 285f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 286f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 287f8a95309SArnaldo Carvalho de Melo evsel->id == NULL && 288f8a95309SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) 289f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 290f8a95309SArnaldo Carvalho de Melo 291f8a95309SArnaldo Carvalho de Melo for (cpu = 0; cpu < cpus->nr; cpu++) { 292f8a95309SArnaldo Carvalho de Melo for (thread = 0; thread < threads->nr; thread++) { 293f8a95309SArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 294f8a95309SArnaldo Carvalho de Melo 295f8a95309SArnaldo Carvalho de Melo if (evsel->idx || thread) { 296f8a95309SArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, 297f8a95309SArnaldo Carvalho de Melo FD(first_evsel, cpu, 0)) != 0) 298f8a95309SArnaldo Carvalho de Melo goto out_unmap; 299f8a95309SArnaldo Carvalho de Melo } else if (__perf_evlist__mmap(evlist, cpu, prot, mask, fd) < 0) 300f8a95309SArnaldo Carvalho de Melo goto out_unmap; 301f8a95309SArnaldo Carvalho de Melo 302f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 303f8a95309SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, fd) < 0) 304f8a95309SArnaldo Carvalho de Melo goto out_unmap; 305f8a95309SArnaldo Carvalho de Melo } 306f8a95309SArnaldo Carvalho de Melo } 307f8a95309SArnaldo Carvalho de Melo } 308f8a95309SArnaldo Carvalho de Melo 309f8a95309SArnaldo Carvalho de Melo return 0; 310f8a95309SArnaldo Carvalho de Melo 311f8a95309SArnaldo Carvalho de Melo out_unmap: 312f8a95309SArnaldo Carvalho de Melo for (cpu = 0; cpu < cpus->nr; cpu++) { 313f8a95309SArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 314f8a95309SArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 315f8a95309SArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 316f8a95309SArnaldo Carvalho de Melo } 317f8a95309SArnaldo Carvalho de Melo } 318f8a95309SArnaldo Carvalho de Melo return -1; 319f8a95309SArnaldo Carvalho de Melo } 3207e2ed097SArnaldo Carvalho de Melo 3217e2ed097SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid, 3227e2ed097SArnaldo Carvalho de Melo pid_t target_tid, const char *cpu_list) 3237e2ed097SArnaldo Carvalho de Melo { 3247e2ed097SArnaldo Carvalho de Melo evlist->threads = thread_map__new(target_pid, target_tid); 3257e2ed097SArnaldo Carvalho de Melo 3267e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 3277e2ed097SArnaldo Carvalho de Melo return -1; 3287e2ed097SArnaldo Carvalho de Melo 3297e2ed097SArnaldo Carvalho de Melo if (target_tid != -1) 3307e2ed097SArnaldo Carvalho de Melo evlist->cpus = cpu_map__dummy_new(); 3317e2ed097SArnaldo Carvalho de Melo else 3327e2ed097SArnaldo Carvalho de Melo evlist->cpus = cpu_map__new(cpu_list); 3337e2ed097SArnaldo Carvalho de Melo 3347e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 3357e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 3367e2ed097SArnaldo Carvalho de Melo 3377e2ed097SArnaldo Carvalho de Melo return 0; 3387e2ed097SArnaldo Carvalho de Melo 3397e2ed097SArnaldo Carvalho de Melo out_delete_threads: 3407e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 3417e2ed097SArnaldo Carvalho de Melo return -1; 3427e2ed097SArnaldo Carvalho de Melo } 3437e2ed097SArnaldo Carvalho de Melo 3447e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 3457e2ed097SArnaldo Carvalho de Melo { 3467e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 3477e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 3487e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 3497e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 3507e2ed097SArnaldo Carvalho de Melo } 3510a102479SFrederic Weisbecker 3520a102479SFrederic Weisbecker int perf_evlist__set_filters(struct perf_evlist *evlist) 3530a102479SFrederic Weisbecker { 3540a102479SFrederic Weisbecker const struct thread_map *threads = evlist->threads; 3550a102479SFrederic Weisbecker const struct cpu_map *cpus = evlist->cpus; 3560a102479SFrederic Weisbecker struct perf_evsel *evsel; 3570a102479SFrederic Weisbecker char *filter; 3580a102479SFrederic Weisbecker int thread; 3590a102479SFrederic Weisbecker int cpu; 3600a102479SFrederic Weisbecker int err; 3610a102479SFrederic Weisbecker int fd; 3620a102479SFrederic Weisbecker 3630a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 3640a102479SFrederic Weisbecker filter = evsel->filter; 3650a102479SFrederic Weisbecker if (!filter) 3660a102479SFrederic Weisbecker continue; 3670a102479SFrederic Weisbecker for (cpu = 0; cpu < cpus->nr; cpu++) { 3680a102479SFrederic Weisbecker for (thread = 0; thread < threads->nr; thread++) { 3690a102479SFrederic Weisbecker fd = FD(evsel, cpu, thread); 3700a102479SFrederic Weisbecker err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); 3710a102479SFrederic Weisbecker if (err) 3720a102479SFrederic Weisbecker return err; 3730a102479SFrederic Weisbecker } 3740a102479SFrederic Weisbecker } 3750a102479SFrederic Weisbecker } 3760a102479SFrederic Weisbecker 3770a102479SFrederic Weisbecker return 0; 3780a102479SFrederic Weisbecker } 379