1f8a95309SArnaldo Carvalho de Melo /* 2f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3f8a95309SArnaldo Carvalho de Melo * 4f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 5f8a95309SArnaldo Carvalho de Melo * copyright notes. 6f8a95309SArnaldo Carvalho de Melo * 7f8a95309SArnaldo Carvalho de Melo * Released under the GPL v2. (and only v2, not any later version) 8f8a95309SArnaldo Carvalho de Melo */ 9a8c9ae18SArnaldo Carvalho de Melo #include "util.h" 1085c66be1SBorislav Petkov #include <lk/debugfs.h> 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1412864b31SNamhyung Kim #include "target.h" 15361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 16361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 1735b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 18361c99a6SArnaldo Carvalho de Melo 1950d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 2050d08e47SArnaldo Carvalho de Melo 21f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 22f8a95309SArnaldo Carvalho de Melo 2370db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2470db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 2570db7533SArnaldo Carvalho de Melo 26f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 27a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 28f8a95309SArnaldo Carvalho de Melo 297e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 307e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 31361c99a6SArnaldo Carvalho de Melo { 3270db7533SArnaldo Carvalho de Melo int i; 3370db7533SArnaldo Carvalho de Melo 3470db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 3570db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 36361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 377e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 3835b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 39361c99a6SArnaldo Carvalho de Melo } 40361c99a6SArnaldo Carvalho de Melo 41334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 42ef1d1af2SArnaldo Carvalho de Melo { 43ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 44ef1d1af2SArnaldo Carvalho de Melo 45ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 46334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 47ef1d1af2SArnaldo Carvalho de Melo 48361c99a6SArnaldo Carvalho de Melo return evlist; 49361c99a6SArnaldo Carvalho de Melo } 50361c99a6SArnaldo Carvalho de Melo 51f77a9518SArnaldo Carvalho de Melo void perf_evlist__config(struct perf_evlist *evlist, 520f82ebc4SArnaldo Carvalho de Melo struct perf_record_opts *opts) 530f82ebc4SArnaldo Carvalho de Melo { 54cac21425SJiri Olsa struct perf_evsel *evsel; 55f77a9518SArnaldo Carvalho de Melo /* 56f77a9518SArnaldo Carvalho de Melo * Set the evsel leader links before we configure attributes, 57f77a9518SArnaldo Carvalho de Melo * since some might depend on this info. 58f77a9518SArnaldo Carvalho de Melo */ 59f77a9518SArnaldo Carvalho de Melo if (opts->group) 60f77a9518SArnaldo Carvalho de Melo perf_evlist__set_leader(evlist); 610f82ebc4SArnaldo Carvalho de Melo 620f82ebc4SArnaldo Carvalho de Melo if (evlist->cpus->map[0] < 0) 630f82ebc4SArnaldo Carvalho de Melo opts->no_inherit = true; 640f82ebc4SArnaldo Carvalho de Melo 650f82ebc4SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 66cac21425SJiri Olsa perf_evsel__config(evsel, opts); 670f82ebc4SArnaldo Carvalho de Melo 680f82ebc4SArnaldo Carvalho de Melo if (evlist->nr_entries > 1) 693a5afaecSArnaldo Carvalho de Melo perf_evsel__set_sample_id(evsel); 700f82ebc4SArnaldo Carvalho de Melo } 710f82ebc4SArnaldo Carvalho de Melo } 720f82ebc4SArnaldo Carvalho de Melo 73361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 74361c99a6SArnaldo Carvalho de Melo { 75361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 76361c99a6SArnaldo Carvalho de Melo 77361c99a6SArnaldo Carvalho de Melo list_for_each_entry_safe(pos, n, &evlist->entries, node) { 78361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 79361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 80361c99a6SArnaldo Carvalho de Melo } 81361c99a6SArnaldo Carvalho de Melo 82361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 83361c99a6SArnaldo Carvalho de Melo } 84361c99a6SArnaldo Carvalho de Melo 85ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 86ef1d1af2SArnaldo Carvalho de Melo { 87ef1d1af2SArnaldo Carvalho de Melo free(evlist->mmap); 88ef1d1af2SArnaldo Carvalho de Melo free(evlist->pollfd); 89ef1d1af2SArnaldo Carvalho de Melo evlist->mmap = NULL; 90ef1d1af2SArnaldo Carvalho de Melo evlist->pollfd = NULL; 91ef1d1af2SArnaldo Carvalho de Melo } 92ef1d1af2SArnaldo Carvalho de Melo 93361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 94361c99a6SArnaldo Carvalho de Melo { 95361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 96ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 97361c99a6SArnaldo Carvalho de Melo free(evlist); 98361c99a6SArnaldo Carvalho de Melo } 99361c99a6SArnaldo Carvalho de Melo 100361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 101361c99a6SArnaldo Carvalho de Melo { 102361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 103361c99a6SArnaldo Carvalho de Melo ++evlist->nr_entries; 104361c99a6SArnaldo Carvalho de Melo } 105361c99a6SArnaldo Carvalho de Melo 1060529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 10750d08e47SArnaldo Carvalho de Melo struct list_head *list, 10850d08e47SArnaldo Carvalho de Melo int nr_entries) 10950d08e47SArnaldo Carvalho de Melo { 11050d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 11150d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 11250d08e47SArnaldo Carvalho de Melo } 11350d08e47SArnaldo Carvalho de Melo 11463dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 11563dab225SArnaldo Carvalho de Melo { 11663dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 11763dab225SArnaldo Carvalho de Melo 11863dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 11997f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 12097f63e4aSNamhyung Kim 12197f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 12263dab225SArnaldo Carvalho de Melo 12363dab225SArnaldo Carvalho de Melo list_for_each_entry(evsel, list, node) { 12463dab225SArnaldo Carvalho de Melo evsel->leader = leader; 12563dab225SArnaldo Carvalho de Melo } 12663dab225SArnaldo Carvalho de Melo } 12763dab225SArnaldo Carvalho de Melo 12863dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1296a4bb04cSJiri Olsa { 13097f63e4aSNamhyung Kim if (evlist->nr_entries) { 13197f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 13263dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1336a4bb04cSJiri Olsa } 13497f63e4aSNamhyung Kim } 1356a4bb04cSJiri Olsa 136361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 137361c99a6SArnaldo Carvalho de Melo { 138361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 139361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 140361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 141361c99a6SArnaldo Carvalho de Melo }; 1421aed2671SJoerg Roedel struct perf_evsel *evsel; 143361c99a6SArnaldo Carvalho de Melo 1441aed2671SJoerg Roedel event_attr_init(&attr); 1451aed2671SJoerg Roedel 1461aed2671SJoerg Roedel evsel = perf_evsel__new(&attr, 0); 147361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 148cc2d86b0SStephane Eranian goto error; 149cc2d86b0SStephane Eranian 150cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 151cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 152cc2d86b0SStephane Eranian if (!evsel->name) 153cc2d86b0SStephane Eranian goto error_free; 154361c99a6SArnaldo Carvalho de Melo 155361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 156361c99a6SArnaldo Carvalho de Melo return 0; 157cc2d86b0SStephane Eranian error_free: 158cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 159cc2d86b0SStephane Eranian error: 160cc2d86b0SStephane Eranian return -ENOMEM; 161361c99a6SArnaldo Carvalho de Melo } 1625c581041SArnaldo Carvalho de Melo 163e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 16450d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 16550d08e47SArnaldo Carvalho de Melo { 16650d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 16750d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 16850d08e47SArnaldo Carvalho de Melo size_t i; 16950d08e47SArnaldo Carvalho de Melo 17050d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 17150d08e47SArnaldo Carvalho de Melo evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); 17250d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 17350d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 17450d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 17550d08e47SArnaldo Carvalho de Melo } 17650d08e47SArnaldo Carvalho de Melo 17750d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 17850d08e47SArnaldo Carvalho de Melo 17950d08e47SArnaldo Carvalho de Melo return 0; 18050d08e47SArnaldo Carvalho de Melo 18150d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 18250d08e47SArnaldo Carvalho de Melo list_for_each_entry_safe(evsel, n, &head, node) 18350d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 18450d08e47SArnaldo Carvalho de Melo return -1; 18550d08e47SArnaldo Carvalho de Melo } 18650d08e47SArnaldo Carvalho de Melo 18779695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 18879695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 18979695e1bSArnaldo Carvalho de Melo { 19079695e1bSArnaldo Carvalho de Melo size_t i; 19179695e1bSArnaldo Carvalho de Melo 19279695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 19379695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 19479695e1bSArnaldo Carvalho de Melo 19579695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 19679695e1bSArnaldo Carvalho de Melo } 19779695e1bSArnaldo Carvalho de Melo 198da378962SArnaldo Carvalho de Melo struct perf_evsel * 199ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 200ee29be62SArnaldo Carvalho de Melo { 201ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 202ee29be62SArnaldo Carvalho de Melo 203ee29be62SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 204ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 205ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 206ee29be62SArnaldo Carvalho de Melo return evsel; 207ee29be62SArnaldo Carvalho de Melo } 208ee29be62SArnaldo Carvalho de Melo 209ee29be62SArnaldo Carvalho de Melo return NULL; 210ee29be62SArnaldo Carvalho de Melo } 211ee29be62SArnaldo Carvalho de Melo 21239876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 21339876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 21439876e7dSArnaldo Carvalho de Melo { 21539876e7dSArnaldo Carvalho de Melo struct perf_evsel *evsel; 21639876e7dSArnaldo Carvalho de Melo 21739876e7dSArnaldo Carvalho de Melo evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); 21839876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 21939876e7dSArnaldo Carvalho de Melo return -1; 22039876e7dSArnaldo Carvalho de Melo 22139876e7dSArnaldo Carvalho de Melo evsel->handler.func = handler; 22239876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 22339876e7dSArnaldo Carvalho de Melo return 0; 22439876e7dSArnaldo Carvalho de Melo } 22539876e7dSArnaldo Carvalho de Melo 2264152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2274152ab37SArnaldo Carvalho de Melo { 2284152ab37SArnaldo Carvalho de Melo int cpu, thread; 2294152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 230b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 231b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 2324152ab37SArnaldo Carvalho de Melo 233b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 2344152ab37SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 235823254edSNamhyung Kim if (!perf_evsel__is_group_leader(pos)) 2363fe4430dSJiri Olsa continue; 237b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 23855da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 23955da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2404152ab37SArnaldo Carvalho de Melo } 2414152ab37SArnaldo Carvalho de Melo } 2424152ab37SArnaldo Carvalho de Melo } 2434152ab37SArnaldo Carvalho de Melo 244764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 245764e16a3SDavid Ahern { 246764e16a3SDavid Ahern int cpu, thread; 247764e16a3SDavid Ahern struct perf_evsel *pos; 248b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 249b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 250764e16a3SDavid Ahern 251b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 252764e16a3SDavid Ahern list_for_each_entry(pos, &evlist->entries, node) { 253823254edSNamhyung Kim if (!perf_evsel__is_group_leader(pos)) 2543fe4430dSJiri Olsa continue; 255b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 25655da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 25755da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 258764e16a3SDavid Ahern } 259764e16a3SDavid Ahern } 260764e16a3SDavid Ahern } 261764e16a3SDavid Ahern 262806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 2635c581041SArnaldo Carvalho de Melo { 264b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 265b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 266b3a319d5SNamhyung Kim int nfds = nr_cpus * nr_threads * evlist->nr_entries; 2675c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 2685c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 2695c581041SArnaldo Carvalho de Melo } 27070082dd9SArnaldo Carvalho de Melo 27170082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 27270082dd9SArnaldo Carvalho de Melo { 27370082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 27470082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 27570082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 27670082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 27770082dd9SArnaldo Carvalho de Melo } 27870db7533SArnaldo Carvalho de Melo 279a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 280a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 2813d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 2823d3b5e95SArnaldo Carvalho de Melo { 2833d3b5e95SArnaldo Carvalho de Melo int hash; 2843d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 2853d3b5e95SArnaldo Carvalho de Melo 2863d3b5e95SArnaldo Carvalho de Melo sid->id = id; 2873d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 2883d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 2893d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 2903d3b5e95SArnaldo Carvalho de Melo } 2913d3b5e95SArnaldo Carvalho de Melo 292a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 293a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 294a91e5431SArnaldo Carvalho de Melo { 295a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 296a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 297a91e5431SArnaldo Carvalho de Melo } 298a91e5431SArnaldo Carvalho de Melo 299a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 3003d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 301f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 302f8a95309SArnaldo Carvalho de Melo { 303f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 3043d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 305f8a95309SArnaldo Carvalho de Melo 306f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 307f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 308f8a95309SArnaldo Carvalho de Melo return -1; 309f8a95309SArnaldo Carvalho de Melo 310f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 311f8a95309SArnaldo Carvalho de Melo ++id_idx; 312f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 313f8a95309SArnaldo Carvalho de Melo ++id_idx; 314f8a95309SArnaldo Carvalho de Melo 315a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 316f8a95309SArnaldo Carvalho de Melo return 0; 317f8a95309SArnaldo Carvalho de Melo } 318f8a95309SArnaldo Carvalho de Melo 31970db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 32070db7533SArnaldo Carvalho de Melo { 32170db7533SArnaldo Carvalho de Melo struct hlist_head *head; 32270db7533SArnaldo Carvalho de Melo struct hlist_node *pos; 32370db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 32470db7533SArnaldo Carvalho de Melo int hash; 32570db7533SArnaldo Carvalho de Melo 32670db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 3270c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 32870db7533SArnaldo Carvalho de Melo 32970db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 33070db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 33170db7533SArnaldo Carvalho de Melo 33270db7533SArnaldo Carvalho de Melo hlist_for_each_entry(sid, pos, head, node) 33370db7533SArnaldo Carvalho de Melo if (sid->id == id) 33470db7533SArnaldo Carvalho de Melo return sid->evsel; 33530e68bccSNamhyung Kim 33630e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 3370c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 33830e68bccSNamhyung Kim 33970db7533SArnaldo Carvalho de Melo return NULL; 34070db7533SArnaldo Carvalho de Melo } 34104391debSArnaldo Carvalho de Melo 342aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 34304391debSArnaldo Carvalho de Melo { 344aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 34504391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 34604391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 34704391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3488115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 34904391debSArnaldo Carvalho de Melo 3507bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 35104391debSArnaldo Carvalho de Melo /* 35204391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 35304391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 35404391debSArnaldo Carvalho de Melo * 35504391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 35604391debSArnaldo Carvalho de Melo * 35704391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 35804391debSArnaldo Carvalho de Melo */ 3597bb41152SArnaldo Carvalho de Melo int diff = head - old; 36004391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 36104391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 36204391debSArnaldo Carvalho de Melo 36304391debSArnaldo Carvalho de Melo /* 36404391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 36504391debSArnaldo Carvalho de Melo */ 36604391debSArnaldo Carvalho de Melo old = head; 36704391debSArnaldo Carvalho de Melo } 3687bb41152SArnaldo Carvalho de Melo } 36904391debSArnaldo Carvalho de Melo 37004391debSArnaldo Carvalho de Melo if (old != head) { 37104391debSArnaldo Carvalho de Melo size_t size; 37204391debSArnaldo Carvalho de Melo 3738115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 37404391debSArnaldo Carvalho de Melo size = event->header.size; 37504391debSArnaldo Carvalho de Melo 37604391debSArnaldo Carvalho de Melo /* 37704391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 37804391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 37904391debSArnaldo Carvalho de Melo */ 38004391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 38104391debSArnaldo Carvalho de Melo unsigned int offset = old; 38204391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 3830479b8b9SDavid Ahern void *dst = &md->event_copy; 38404391debSArnaldo Carvalho de Melo 38504391debSArnaldo Carvalho de Melo do { 38604391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 38704391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 38804391debSArnaldo Carvalho de Melo offset += cpy; 38904391debSArnaldo Carvalho de Melo dst += cpy; 39004391debSArnaldo Carvalho de Melo len -= cpy; 39104391debSArnaldo Carvalho de Melo } while (len); 39204391debSArnaldo Carvalho de Melo 3930479b8b9SDavid Ahern event = &md->event_copy; 39404391debSArnaldo Carvalho de Melo } 39504391debSArnaldo Carvalho de Melo 39604391debSArnaldo Carvalho de Melo old += size; 39704391debSArnaldo Carvalho de Melo } 39804391debSArnaldo Carvalho de Melo 39904391debSArnaldo Carvalho de Melo md->prev = old; 4007bb41152SArnaldo Carvalho de Melo 4017bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 4027bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 4037bb41152SArnaldo Carvalho de Melo 40404391debSArnaldo Carvalho de Melo return event; 40504391debSArnaldo Carvalho de Melo } 406f8a95309SArnaldo Carvalho de Melo 4077e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 408f8a95309SArnaldo Carvalho de Melo { 409aece948fSArnaldo Carvalho de Melo int i; 410f8a95309SArnaldo Carvalho de Melo 411aece948fSArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 412aece948fSArnaldo Carvalho de Melo if (evlist->mmap[i].base != NULL) { 413aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[i].base, evlist->mmap_len); 414aece948fSArnaldo Carvalho de Melo evlist->mmap[i].base = NULL; 415f8a95309SArnaldo Carvalho de Melo } 416f8a95309SArnaldo Carvalho de Melo } 417aece948fSArnaldo Carvalho de Melo 418aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 419aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 420f8a95309SArnaldo Carvalho de Melo } 421f8a95309SArnaldo Carvalho de Melo 422806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 423f8a95309SArnaldo Carvalho de Melo { 424a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 425a14bb7a6SArnaldo Carvalho de Melo if (cpu_map__all(evlist->cpus)) 426b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 427aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 428f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 429f8a95309SArnaldo Carvalho de Melo } 430f8a95309SArnaldo Carvalho de Melo 431bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 432aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 433f8a95309SArnaldo Carvalho de Melo { 434aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 435aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 436aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 437f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 438301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 439301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 440f8a95309SArnaldo Carvalho de Melo return -1; 441301b195dSNelson Elhage } 442f8a95309SArnaldo Carvalho de Melo 443f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 444f8a95309SArnaldo Carvalho de Melo return 0; 445f8a95309SArnaldo Carvalho de Melo } 446f8a95309SArnaldo Carvalho de Melo 447aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 448aece948fSArnaldo Carvalho de Melo { 449aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 450aece948fSArnaldo Carvalho de Melo int cpu, thread; 451b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 452b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 453aece948fSArnaldo Carvalho de Melo 454b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 455aece948fSArnaldo Carvalho de Melo int output = -1; 456aece948fSArnaldo Carvalho de Melo 457b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 458aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 459aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 460aece948fSArnaldo Carvalho de Melo 461aece948fSArnaldo Carvalho de Melo if (output == -1) { 462aece948fSArnaldo Carvalho de Melo output = fd; 463bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 464aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 465aece948fSArnaldo Carvalho de Melo goto out_unmap; 466aece948fSArnaldo Carvalho de Melo } else { 467aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 468aece948fSArnaldo Carvalho de Melo goto out_unmap; 469aece948fSArnaldo Carvalho de Melo } 470aece948fSArnaldo Carvalho de Melo 471aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 472aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 473aece948fSArnaldo Carvalho de Melo goto out_unmap; 474aece948fSArnaldo Carvalho de Melo } 475aece948fSArnaldo Carvalho de Melo } 476aece948fSArnaldo Carvalho de Melo } 477aece948fSArnaldo Carvalho de Melo 478aece948fSArnaldo Carvalho de Melo return 0; 479aece948fSArnaldo Carvalho de Melo 480aece948fSArnaldo Carvalho de Melo out_unmap: 481b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 482aece948fSArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 483aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 484aece948fSArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 485aece948fSArnaldo Carvalho de Melo } 486aece948fSArnaldo Carvalho de Melo } 487aece948fSArnaldo Carvalho de Melo return -1; 488aece948fSArnaldo Carvalho de Melo } 489aece948fSArnaldo Carvalho de Melo 490aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 491aece948fSArnaldo Carvalho de Melo { 492aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 493aece948fSArnaldo Carvalho de Melo int thread; 494b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 495aece948fSArnaldo Carvalho de Melo 496b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 497aece948fSArnaldo Carvalho de Melo int output = -1; 498aece948fSArnaldo Carvalho de Melo 499aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 500aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 501aece948fSArnaldo Carvalho de Melo 502aece948fSArnaldo Carvalho de Melo if (output == -1) { 503aece948fSArnaldo Carvalho de Melo output = fd; 504bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 505aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 506aece948fSArnaldo Carvalho de Melo goto out_unmap; 507aece948fSArnaldo Carvalho de Melo } else { 508aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 509aece948fSArnaldo Carvalho de Melo goto out_unmap; 510aece948fSArnaldo Carvalho de Melo } 511aece948fSArnaldo Carvalho de Melo 512aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 513aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 514aece948fSArnaldo Carvalho de Melo goto out_unmap; 515aece948fSArnaldo Carvalho de Melo } 516aece948fSArnaldo Carvalho de Melo } 517aece948fSArnaldo Carvalho de Melo 518aece948fSArnaldo Carvalho de Melo return 0; 519aece948fSArnaldo Carvalho de Melo 520aece948fSArnaldo Carvalho de Melo out_unmap: 521b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 522aece948fSArnaldo Carvalho de Melo if (evlist->mmap[thread].base != NULL) { 523aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[thread].base, evlist->mmap_len); 524aece948fSArnaldo Carvalho de Melo evlist->mmap[thread].base = NULL; 525aece948fSArnaldo Carvalho de Melo } 526aece948fSArnaldo Carvalho de Melo } 527aece948fSArnaldo Carvalho de Melo return -1; 528aece948fSArnaldo Carvalho de Melo } 529aece948fSArnaldo Carvalho de Melo 530f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 531f8a95309SArnaldo Carvalho de Melo * 532f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 533f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 534f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 535f8a95309SArnaldo Carvalho de Melo * 536f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 537f8a95309SArnaldo Carvalho de Melo * 538f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 539f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 540f8a95309SArnaldo Carvalho de Melo * 541f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5427e2ed097SArnaldo Carvalho de Melo * 5437e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 544f8a95309SArnaldo Carvalho de Melo */ 54550a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 54650a682ceSArnaldo Carvalho de Melo bool overwrite) 547f8a95309SArnaldo Carvalho de Melo { 548aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5497e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5507e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 55150a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 55250a682ceSArnaldo Carvalho de Melo 55350a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 55450a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 55550a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 55641d0d933SNelson Elhage else if (!is_power_of_2(pages)) 55741d0d933SNelson Elhage return -EINVAL; 55850a682ceSArnaldo Carvalho de Melo 55950a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 560f8a95309SArnaldo Carvalho de Melo 5617e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 562f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 563f8a95309SArnaldo Carvalho de Melo 5647e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 565f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 566f8a95309SArnaldo Carvalho de Melo 567f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 568f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 569f8a95309SArnaldo Carvalho de Melo 570f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 571f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 572a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 573a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 574f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 575f8a95309SArnaldo Carvalho de Melo } 576f8a95309SArnaldo Carvalho de Melo 577a14bb7a6SArnaldo Carvalho de Melo if (cpu_map__all(cpus)) 578aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 579f8a95309SArnaldo Carvalho de Melo 580aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 581f8a95309SArnaldo Carvalho de Melo } 5827e2ed097SArnaldo Carvalho de Melo 583b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 584b809ac10SNamhyung Kim struct perf_target *target) 5857e2ed097SArnaldo Carvalho de Melo { 586b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 587b809ac10SNamhyung Kim target->uid); 5887e2ed097SArnaldo Carvalho de Melo 5897e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 5907e2ed097SArnaldo Carvalho de Melo return -1; 5917e2ed097SArnaldo Carvalho de Melo 592879d77d0SNamhyung Kim if (perf_target__has_task(target)) 593d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 594d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 595d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 596879d77d0SNamhyung Kim else 597879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 5987e2ed097SArnaldo Carvalho de Melo 5997e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 6007e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 6017e2ed097SArnaldo Carvalho de Melo 6027e2ed097SArnaldo Carvalho de Melo return 0; 6037e2ed097SArnaldo Carvalho de Melo 6047e2ed097SArnaldo Carvalho de Melo out_delete_threads: 6057e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6067e2ed097SArnaldo Carvalho de Melo return -1; 6077e2ed097SArnaldo Carvalho de Melo } 6087e2ed097SArnaldo Carvalho de Melo 6097e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 6107e2ed097SArnaldo Carvalho de Melo { 6117e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 6127e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6137e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 6147e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 6157e2ed097SArnaldo Carvalho de Melo } 6160a102479SFrederic Weisbecker 6171491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 6180a102479SFrederic Weisbecker { 6190a102479SFrederic Weisbecker struct perf_evsel *evsel; 620745cefc5SArnaldo Carvalho de Melo int err = 0; 621745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 622b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 6230a102479SFrederic Weisbecker 6240a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 625745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 6260a102479SFrederic Weisbecker continue; 627745cefc5SArnaldo Carvalho de Melo 628745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 6290a102479SFrederic Weisbecker if (err) 630745cefc5SArnaldo Carvalho de Melo break; 6310a102479SFrederic Weisbecker } 6320a102479SFrederic Weisbecker 633745cefc5SArnaldo Carvalho de Melo return err; 634745cefc5SArnaldo Carvalho de Melo } 635745cefc5SArnaldo Carvalho de Melo 636745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 637745cefc5SArnaldo Carvalho de Melo { 638745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 639745cefc5SArnaldo Carvalho de Melo int err = 0; 640745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 641b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 642745cefc5SArnaldo Carvalho de Melo 643745cefc5SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 644745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 645745cefc5SArnaldo Carvalho de Melo if (err) 646745cefc5SArnaldo Carvalho de Melo break; 647745cefc5SArnaldo Carvalho de Melo } 648745cefc5SArnaldo Carvalho de Melo 649745cefc5SArnaldo Carvalho de Melo return err; 6500a102479SFrederic Weisbecker } 65174429964SFrederic Weisbecker 6520c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 65374429964SFrederic Weisbecker { 6540c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 655c2a70653SArnaldo Carvalho de Melo 656c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 657c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 658c2a70653SArnaldo Carvalho de Melo return false; 65974429964SFrederic Weisbecker } 66074429964SFrederic Weisbecker 661c2a70653SArnaldo Carvalho de Melo return true; 662c2a70653SArnaldo Carvalho de Melo } 663c2a70653SArnaldo Carvalho de Melo 6640c21f736SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(struct perf_evlist *evlist) 665c2a70653SArnaldo Carvalho de Melo { 6660c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 667c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 668c2a70653SArnaldo Carvalho de Melo } 669c2a70653SArnaldo Carvalho de Melo 6700c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 67181e36bffSArnaldo Carvalho de Melo { 6720c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 67381e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 67481e36bffSArnaldo Carvalho de Melo u64 sample_type; 67581e36bffSArnaldo Carvalho de Melo u16 size = 0; 67681e36bffSArnaldo Carvalho de Melo 67781e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 67881e36bffSArnaldo Carvalho de Melo goto out; 67981e36bffSArnaldo Carvalho de Melo 68081e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 68181e36bffSArnaldo Carvalho de Melo 68281e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 68381e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 68481e36bffSArnaldo Carvalho de Melo 68581e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 68681e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 68781e36bffSArnaldo Carvalho de Melo 68881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 68981e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 69081e36bffSArnaldo Carvalho de Melo 69181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 69281e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 69381e36bffSArnaldo Carvalho de Melo 69481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 69581e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 69681e36bffSArnaldo Carvalho de Melo out: 69781e36bffSArnaldo Carvalho de Melo return size; 69881e36bffSArnaldo Carvalho de Melo } 69981e36bffSArnaldo Carvalho de Melo 7000c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 701c2a70653SArnaldo Carvalho de Melo { 7020c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 703c2a70653SArnaldo Carvalho de Melo 704c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 705c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 706c2a70653SArnaldo Carvalho de Melo return false; 707c2a70653SArnaldo Carvalho de Melo } 708c2a70653SArnaldo Carvalho de Melo 709c2a70653SArnaldo Carvalho de Melo return true; 71074429964SFrederic Weisbecker } 71174429964SFrederic Weisbecker 7120c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 71374429964SFrederic Weisbecker { 7140c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 715c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 71674429964SFrederic Weisbecker } 71781cce8deSArnaldo Carvalho de Melo 71881cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 71981cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 72081cce8deSArnaldo Carvalho de Melo { 72181cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 72281cce8deSArnaldo Carvalho de Melo } 723727ab04eSArnaldo Carvalho de Melo 7246a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 725727ab04eSArnaldo Carvalho de Melo { 7266a4bb04cSJiri Olsa struct perf_evsel *evsel; 727727ab04eSArnaldo Carvalho de Melo int err, ncpus, nthreads; 728727ab04eSArnaldo Carvalho de Melo 729727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 7306a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 731727ab04eSArnaldo Carvalho de Melo if (err < 0) 732727ab04eSArnaldo Carvalho de Melo goto out_err; 733727ab04eSArnaldo Carvalho de Melo } 734727ab04eSArnaldo Carvalho de Melo 735727ab04eSArnaldo Carvalho de Melo return 0; 736727ab04eSArnaldo Carvalho de Melo out_err: 73785397956SNamhyung Kim ncpus = cpu_map__nr(evlist->cpus); 738b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 739727ab04eSArnaldo Carvalho de Melo 740727ab04eSArnaldo Carvalho de Melo list_for_each_entry_reverse(evsel, &evlist->entries, node) 741727ab04eSArnaldo Carvalho de Melo perf_evsel__close(evsel, ncpus, nthreads); 742727ab04eSArnaldo Carvalho de Melo 74341c21a68SNamhyung Kim errno = -err; 744727ab04eSArnaldo Carvalho de Melo return err; 745727ab04eSArnaldo Carvalho de Melo } 74635b9d88eSArnaldo Carvalho de Melo 74735b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 74835b9d88eSArnaldo Carvalho de Melo struct perf_record_opts *opts, 74935b9d88eSArnaldo Carvalho de Melo const char *argv[]) 75035b9d88eSArnaldo Carvalho de Melo { 75135b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 75235b9d88eSArnaldo Carvalho de Melo char bf; 75335b9d88eSArnaldo Carvalho de Melo 75435b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 75535b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 75635b9d88eSArnaldo Carvalho de Melo return -1; 75735b9d88eSArnaldo Carvalho de Melo } 75835b9d88eSArnaldo Carvalho de Melo 75935b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 76035b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 76135b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 76235b9d88eSArnaldo Carvalho de Melo } 76335b9d88eSArnaldo Carvalho de Melo 76435b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 76535b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 76635b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 76735b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 76835b9d88eSArnaldo Carvalho de Melo } 76935b9d88eSArnaldo Carvalho de Melo 77035b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 77135b9d88eSArnaldo Carvalho de Melo if (opts->pipe_output) 77235b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 77335b9d88eSArnaldo Carvalho de Melo 77435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 77535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 77635b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 77735b9d88eSArnaldo Carvalho de Melo 77835b9d88eSArnaldo Carvalho de Melo /* 77935b9d88eSArnaldo Carvalho de Melo * Do a dummy execvp to get the PLT entry resolved, 78035b9d88eSArnaldo Carvalho de Melo * so we avoid the resolver overhead on the real 78135b9d88eSArnaldo Carvalho de Melo * execvp call. 78235b9d88eSArnaldo Carvalho de Melo */ 78335b9d88eSArnaldo Carvalho de Melo execvp("", (char **)argv); 78435b9d88eSArnaldo Carvalho de Melo 78535b9d88eSArnaldo Carvalho de Melo /* 78635b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 78735b9d88eSArnaldo Carvalho de Melo */ 78835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 78935b9d88eSArnaldo Carvalho de Melo 79035b9d88eSArnaldo Carvalho de Melo /* 79135b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 79235b9d88eSArnaldo Carvalho de Melo */ 79335b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 79435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 79535b9d88eSArnaldo Carvalho de Melo 79635b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 79735b9d88eSArnaldo Carvalho de Melo 79835b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 79935b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 80035b9d88eSArnaldo Carvalho de Melo exit(-1); 80135b9d88eSArnaldo Carvalho de Melo } 80235b9d88eSArnaldo Carvalho de Melo 803d67356e7SNamhyung Kim if (perf_target__none(&opts->target)) 80435b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 80535b9d88eSArnaldo Carvalho de Melo 80635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 80735b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 80835b9d88eSArnaldo Carvalho de Melo /* 80935b9d88eSArnaldo Carvalho de Melo * wait for child to settle 81035b9d88eSArnaldo Carvalho de Melo */ 81135b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 81235b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 81335b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 81435b9d88eSArnaldo Carvalho de Melo } 81535b9d88eSArnaldo Carvalho de Melo 81635b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 81735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 81835b9d88eSArnaldo Carvalho de Melo return 0; 81935b9d88eSArnaldo Carvalho de Melo 82035b9d88eSArnaldo Carvalho de Melo out_close_pipes: 82135b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 82235b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 82335b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 82435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 82535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 82635b9d88eSArnaldo Carvalho de Melo return -1; 82735b9d88eSArnaldo Carvalho de Melo } 82835b9d88eSArnaldo Carvalho de Melo 82935b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 83035b9d88eSArnaldo Carvalho de Melo { 83135b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 83235b9d88eSArnaldo Carvalho de Melo /* 83335b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 83435b9d88eSArnaldo Carvalho de Melo */ 83535b9d88eSArnaldo Carvalho de Melo return close(evlist->workload.cork_fd); 83635b9d88eSArnaldo Carvalho de Melo } 83735b9d88eSArnaldo Carvalho de Melo 83835b9d88eSArnaldo Carvalho de Melo return 0; 83935b9d88eSArnaldo Carvalho de Melo } 840cb0b29e0SArnaldo Carvalho de Melo 841a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 8420807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 843cb0b29e0SArnaldo Carvalho de Melo { 8440c21f736SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evlist__first(evlist); 8450807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 846cb0b29e0SArnaldo Carvalho de Melo } 84778f067b3SArnaldo Carvalho de Melo 84878f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 84978f067b3SArnaldo Carvalho de Melo { 85078f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 85178f067b3SArnaldo Carvalho de Melo size_t printed = 0; 85278f067b3SArnaldo Carvalho de Melo 85378f067b3SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 85478f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 85578f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 85678f067b3SArnaldo Carvalho de Melo } 85778f067b3SArnaldo Carvalho de Melo 85878f067b3SArnaldo Carvalho de Melo return printed + fprintf(fp, "\n");; 85978f067b3SArnaldo Carvalho de Melo } 860