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 */ 305e2b5abe0SJiri Olsa u64 id; 306e2b5abe0SJiri Olsa int ret; 307e2b5abe0SJiri Olsa 308e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 309e2b5abe0SJiri Olsa if (!ret) 310e2b5abe0SJiri Olsa goto add; 311e2b5abe0SJiri Olsa 312e2b5abe0SJiri Olsa if (errno != ENOTTY) 313e2b5abe0SJiri Olsa return -1; 314e2b5abe0SJiri Olsa 315e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 316f8a95309SArnaldo Carvalho de Melo 317f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 318f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 319f8a95309SArnaldo Carvalho de Melo return -1; 320f8a95309SArnaldo Carvalho de Melo 321f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 322f8a95309SArnaldo Carvalho de Melo ++id_idx; 323f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 324f8a95309SArnaldo Carvalho de Melo ++id_idx; 325f8a95309SArnaldo Carvalho de Melo 326e2b5abe0SJiri Olsa id = read_data[id_idx]; 327e2b5abe0SJiri Olsa 328e2b5abe0SJiri Olsa add: 329e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 330f8a95309SArnaldo Carvalho de Melo return 0; 331f8a95309SArnaldo Carvalho de Melo } 332f8a95309SArnaldo Carvalho de Melo 33370db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 33470db7533SArnaldo Carvalho de Melo { 33570db7533SArnaldo Carvalho de Melo struct hlist_head *head; 33670db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 33770db7533SArnaldo Carvalho de Melo int hash; 33870db7533SArnaldo Carvalho de Melo 33970db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 3400c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 34170db7533SArnaldo Carvalho de Melo 34270db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 34370db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 34470db7533SArnaldo Carvalho de Melo 345b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 34670db7533SArnaldo Carvalho de Melo if (sid->id == id) 34770db7533SArnaldo Carvalho de Melo return sid->evsel; 34830e68bccSNamhyung Kim 34930e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 3500c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 35130e68bccSNamhyung Kim 35270db7533SArnaldo Carvalho de Melo return NULL; 35370db7533SArnaldo Carvalho de Melo } 35404391debSArnaldo Carvalho de Melo 355aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 35604391debSArnaldo Carvalho de Melo { 357aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 35804391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 35904391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 36004391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3618115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 36204391debSArnaldo Carvalho de Melo 3637bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 36404391debSArnaldo Carvalho de Melo /* 36504391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 36604391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 36704391debSArnaldo Carvalho de Melo * 36804391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 36904391debSArnaldo Carvalho de Melo * 37004391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 37104391debSArnaldo Carvalho de Melo */ 3727bb41152SArnaldo Carvalho de Melo int diff = head - old; 37304391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 37404391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 37504391debSArnaldo Carvalho de Melo 37604391debSArnaldo Carvalho de Melo /* 37704391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 37804391debSArnaldo Carvalho de Melo */ 37904391debSArnaldo Carvalho de Melo old = head; 38004391debSArnaldo Carvalho de Melo } 3817bb41152SArnaldo Carvalho de Melo } 38204391debSArnaldo Carvalho de Melo 38304391debSArnaldo Carvalho de Melo if (old != head) { 38404391debSArnaldo Carvalho de Melo size_t size; 38504391debSArnaldo Carvalho de Melo 3868115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 38704391debSArnaldo Carvalho de Melo size = event->header.size; 38804391debSArnaldo Carvalho de Melo 38904391debSArnaldo Carvalho de Melo /* 39004391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 39104391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 39204391debSArnaldo Carvalho de Melo */ 39304391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 39404391debSArnaldo Carvalho de Melo unsigned int offset = old; 39504391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 3960479b8b9SDavid Ahern void *dst = &md->event_copy; 39704391debSArnaldo Carvalho de Melo 39804391debSArnaldo Carvalho de Melo do { 39904391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 40004391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 40104391debSArnaldo Carvalho de Melo offset += cpy; 40204391debSArnaldo Carvalho de Melo dst += cpy; 40304391debSArnaldo Carvalho de Melo len -= cpy; 40404391debSArnaldo Carvalho de Melo } while (len); 40504391debSArnaldo Carvalho de Melo 4060479b8b9SDavid Ahern event = &md->event_copy; 40704391debSArnaldo Carvalho de Melo } 40804391debSArnaldo Carvalho de Melo 40904391debSArnaldo Carvalho de Melo old += size; 41004391debSArnaldo Carvalho de Melo } 41104391debSArnaldo Carvalho de Melo 41204391debSArnaldo Carvalho de Melo md->prev = old; 4137bb41152SArnaldo Carvalho de Melo 4147bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 4157bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 4167bb41152SArnaldo Carvalho de Melo 41704391debSArnaldo Carvalho de Melo return event; 41804391debSArnaldo Carvalho de Melo } 419f8a95309SArnaldo Carvalho de Melo 42093edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 42193edcbd9SAdrian Hunter { 42293edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 42393edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 42493edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 42593edcbd9SAdrian Hunter } 42693edcbd9SAdrian Hunter } 42793edcbd9SAdrian Hunter 4287e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 429f8a95309SArnaldo Carvalho de Melo { 430aece948fSArnaldo Carvalho de Melo int i; 431f8a95309SArnaldo Carvalho de Melo 43293edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 43393edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 434aece948fSArnaldo Carvalho de Melo 435aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 436aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 437f8a95309SArnaldo Carvalho de Melo } 438f8a95309SArnaldo Carvalho de Melo 439806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 440f8a95309SArnaldo Carvalho de Melo { 441a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 442ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 443b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 444aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 445f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 446f8a95309SArnaldo Carvalho de Melo } 447f8a95309SArnaldo Carvalho de Melo 448bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 449aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 450f8a95309SArnaldo Carvalho de Melo { 451aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 452aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 453aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 454f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 455301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 456301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 457f8a95309SArnaldo Carvalho de Melo return -1; 458301b195dSNelson Elhage } 459f8a95309SArnaldo Carvalho de Melo 460f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 461f8a95309SArnaldo Carvalho de Melo return 0; 462f8a95309SArnaldo Carvalho de Melo } 463f8a95309SArnaldo Carvalho de Melo 464aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 465aece948fSArnaldo Carvalho de Melo { 466aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 467aece948fSArnaldo Carvalho de Melo int cpu, thread; 468b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 469b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 470aece948fSArnaldo Carvalho de Melo 471b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 472aece948fSArnaldo Carvalho de Melo int output = -1; 473aece948fSArnaldo Carvalho de Melo 474b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 475aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 476aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 477aece948fSArnaldo Carvalho de Melo 478aece948fSArnaldo Carvalho de Melo if (output == -1) { 479aece948fSArnaldo Carvalho de Melo output = fd; 480bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 481aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 482aece948fSArnaldo Carvalho de Melo goto out_unmap; 483aece948fSArnaldo Carvalho de Melo } else { 484aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 485aece948fSArnaldo Carvalho de Melo goto out_unmap; 486aece948fSArnaldo Carvalho de Melo } 487aece948fSArnaldo Carvalho de Melo 488aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 489aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 490aece948fSArnaldo Carvalho de Melo goto out_unmap; 491aece948fSArnaldo Carvalho de Melo } 492aece948fSArnaldo Carvalho de Melo } 493aece948fSArnaldo Carvalho de Melo } 494aece948fSArnaldo Carvalho de Melo 495aece948fSArnaldo Carvalho de Melo return 0; 496aece948fSArnaldo Carvalho de Melo 497aece948fSArnaldo Carvalho de Melo out_unmap: 49893edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 49993edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 500aece948fSArnaldo Carvalho de Melo return -1; 501aece948fSArnaldo Carvalho de Melo } 502aece948fSArnaldo Carvalho de Melo 503aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 504aece948fSArnaldo Carvalho de Melo { 505aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 506aece948fSArnaldo Carvalho de Melo int thread; 507b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 508aece948fSArnaldo Carvalho de Melo 509b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 510aece948fSArnaldo Carvalho de Melo int output = -1; 511aece948fSArnaldo Carvalho de Melo 512aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 513aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 514aece948fSArnaldo Carvalho de Melo 515aece948fSArnaldo Carvalho de Melo if (output == -1) { 516aece948fSArnaldo Carvalho de Melo output = fd; 517bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 518aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 519aece948fSArnaldo Carvalho de Melo goto out_unmap; 520aece948fSArnaldo Carvalho de Melo } else { 521aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 522aece948fSArnaldo Carvalho de Melo goto out_unmap; 523aece948fSArnaldo Carvalho de Melo } 524aece948fSArnaldo Carvalho de Melo 525aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 526aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 527aece948fSArnaldo Carvalho de Melo goto out_unmap; 528aece948fSArnaldo Carvalho de Melo } 529aece948fSArnaldo Carvalho de Melo } 530aece948fSArnaldo Carvalho de Melo 531aece948fSArnaldo Carvalho de Melo return 0; 532aece948fSArnaldo Carvalho de Melo 533aece948fSArnaldo Carvalho de Melo out_unmap: 53493edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 53593edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 536aece948fSArnaldo Carvalho de Melo return -1; 537aece948fSArnaldo Carvalho de Melo } 538aece948fSArnaldo Carvalho de Melo 539f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 540f8a95309SArnaldo Carvalho de Melo * 541f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 542f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 543f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 544f8a95309SArnaldo Carvalho de Melo * 545f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 546f8a95309SArnaldo Carvalho de Melo * 547f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 548f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 549f8a95309SArnaldo Carvalho de Melo * 550f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5517e2ed097SArnaldo Carvalho de Melo * 5527e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 553f8a95309SArnaldo Carvalho de Melo */ 55450a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 55550a682ceSArnaldo Carvalho de Melo bool overwrite) 556f8a95309SArnaldo Carvalho de Melo { 557aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5587e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5597e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 56050a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 56150a682ceSArnaldo Carvalho de Melo 56250a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 56350a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 56450a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 56541d0d933SNelson Elhage else if (!is_power_of_2(pages)) 56641d0d933SNelson Elhage return -EINVAL; 56750a682ceSArnaldo Carvalho de Melo 56850a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 569f8a95309SArnaldo Carvalho de Melo 5707e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 571f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 572f8a95309SArnaldo Carvalho de Melo 5737e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 574f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 575f8a95309SArnaldo Carvalho de Melo 576f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 577f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 578f8a95309SArnaldo Carvalho de Melo 579f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 580f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 581a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 582a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 583f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 584f8a95309SArnaldo Carvalho de Melo } 585f8a95309SArnaldo Carvalho de Melo 586ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 587aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 588f8a95309SArnaldo Carvalho de Melo 589aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 590f8a95309SArnaldo Carvalho de Melo } 5917e2ed097SArnaldo Carvalho de Melo 592b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 593b809ac10SNamhyung Kim struct perf_target *target) 5947e2ed097SArnaldo Carvalho de Melo { 595b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 596b809ac10SNamhyung Kim target->uid); 5977e2ed097SArnaldo Carvalho de Melo 5987e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 5997e2ed097SArnaldo Carvalho de Melo return -1; 6007e2ed097SArnaldo Carvalho de Melo 601879d77d0SNamhyung Kim if (perf_target__has_task(target)) 602d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 603d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 604d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 605879d77d0SNamhyung Kim else 606879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 6077e2ed097SArnaldo Carvalho de Melo 6087e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 6097e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 6107e2ed097SArnaldo Carvalho de Melo 6117e2ed097SArnaldo Carvalho de Melo return 0; 6127e2ed097SArnaldo Carvalho de Melo 6137e2ed097SArnaldo Carvalho de Melo out_delete_threads: 6147e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6157e2ed097SArnaldo Carvalho de Melo return -1; 6167e2ed097SArnaldo Carvalho de Melo } 6177e2ed097SArnaldo Carvalho de Melo 6187e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 6197e2ed097SArnaldo Carvalho de Melo { 6207e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 6217e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6227e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 6237e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 6247e2ed097SArnaldo Carvalho de Melo } 6250a102479SFrederic Weisbecker 6261491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 6270a102479SFrederic Weisbecker { 6280a102479SFrederic Weisbecker struct perf_evsel *evsel; 629745cefc5SArnaldo Carvalho de Melo int err = 0; 630745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 631b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 6320a102479SFrederic Weisbecker 6330a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 634745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 6350a102479SFrederic Weisbecker continue; 636745cefc5SArnaldo Carvalho de Melo 637745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 6380a102479SFrederic Weisbecker if (err) 639745cefc5SArnaldo Carvalho de Melo break; 6400a102479SFrederic Weisbecker } 6410a102479SFrederic Weisbecker 642745cefc5SArnaldo Carvalho de Melo return err; 643745cefc5SArnaldo Carvalho de Melo } 644745cefc5SArnaldo Carvalho de Melo 645745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 646745cefc5SArnaldo Carvalho de Melo { 647745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 648745cefc5SArnaldo Carvalho de Melo int err = 0; 649745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 650b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 651745cefc5SArnaldo Carvalho de Melo 652745cefc5SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 653745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 654745cefc5SArnaldo Carvalho de Melo if (err) 655745cefc5SArnaldo Carvalho de Melo break; 656745cefc5SArnaldo Carvalho de Melo } 657745cefc5SArnaldo Carvalho de Melo 658745cefc5SArnaldo Carvalho de Melo return err; 6590a102479SFrederic Weisbecker } 66074429964SFrederic Weisbecker 6610c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 66274429964SFrederic Weisbecker { 6630c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 664c2a70653SArnaldo Carvalho de Melo 665c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 666c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 667c2a70653SArnaldo Carvalho de Melo return false; 66874429964SFrederic Weisbecker } 66974429964SFrederic Weisbecker 670c2a70653SArnaldo Carvalho de Melo return true; 671c2a70653SArnaldo Carvalho de Melo } 672c2a70653SArnaldo Carvalho de Melo 6730c21f736SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(struct perf_evlist *evlist) 674c2a70653SArnaldo Carvalho de Melo { 6750c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 676c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 677c2a70653SArnaldo Carvalho de Melo } 678c2a70653SArnaldo Carvalho de Melo 6790c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 68081e36bffSArnaldo Carvalho de Melo { 6810c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 68281e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 68381e36bffSArnaldo Carvalho de Melo u64 sample_type; 68481e36bffSArnaldo Carvalho de Melo u16 size = 0; 68581e36bffSArnaldo Carvalho de Melo 68681e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 68781e36bffSArnaldo Carvalho de Melo goto out; 68881e36bffSArnaldo Carvalho de Melo 68981e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 69081e36bffSArnaldo Carvalho de Melo 69181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 69281e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 69381e36bffSArnaldo Carvalho de Melo 69481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 69581e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 69681e36bffSArnaldo Carvalho de Melo 69781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 69881e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 69981e36bffSArnaldo Carvalho de Melo 70081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 70181e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 70281e36bffSArnaldo Carvalho de Melo 70381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 70481e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 70581e36bffSArnaldo Carvalho de Melo out: 70681e36bffSArnaldo Carvalho de Melo return size; 70781e36bffSArnaldo Carvalho de Melo } 70881e36bffSArnaldo Carvalho de Melo 7090c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 710c2a70653SArnaldo Carvalho de Melo { 7110c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 712c2a70653SArnaldo Carvalho de Melo 713c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 714c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 715c2a70653SArnaldo Carvalho de Melo return false; 716c2a70653SArnaldo Carvalho de Melo } 717c2a70653SArnaldo Carvalho de Melo 718c2a70653SArnaldo Carvalho de Melo return true; 71974429964SFrederic Weisbecker } 72074429964SFrederic Weisbecker 7210c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 72274429964SFrederic Weisbecker { 7230c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 724c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 72574429964SFrederic Weisbecker } 72681cce8deSArnaldo Carvalho de Melo 72781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 72881cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 72981cce8deSArnaldo Carvalho de Melo { 73081cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 73181cce8deSArnaldo Carvalho de Melo } 732727ab04eSArnaldo Carvalho de Melo 733a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 734a74b4b66SNamhyung Kim { 735a74b4b66SNamhyung Kim struct perf_evsel *evsel; 736a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 737a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 738a74b4b66SNamhyung Kim 739a74b4b66SNamhyung Kim list_for_each_entry_reverse(evsel, &evlist->entries, node) 740a74b4b66SNamhyung Kim perf_evsel__close(evsel, ncpus, nthreads); 741a74b4b66SNamhyung Kim } 742a74b4b66SNamhyung Kim 7436a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 744727ab04eSArnaldo Carvalho de Melo { 7456a4bb04cSJiri Olsa struct perf_evsel *evsel; 746a74b4b66SNamhyung Kim int err; 747727ab04eSArnaldo Carvalho de Melo 748727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 7496a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 750727ab04eSArnaldo Carvalho de Melo if (err < 0) 751727ab04eSArnaldo Carvalho de Melo goto out_err; 752727ab04eSArnaldo Carvalho de Melo } 753727ab04eSArnaldo Carvalho de Melo 754727ab04eSArnaldo Carvalho de Melo return 0; 755727ab04eSArnaldo Carvalho de Melo out_err: 756a74b4b66SNamhyung Kim perf_evlist__close(evlist); 75741c21a68SNamhyung Kim errno = -err; 758727ab04eSArnaldo Carvalho de Melo return err; 759727ab04eSArnaldo Carvalho de Melo } 76035b9d88eSArnaldo Carvalho de Melo 76135b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 7626ef73ec4SNamhyung Kim struct perf_target *target, 76355e162eaSNamhyung Kim const char *argv[], bool pipe_output, 76455e162eaSNamhyung Kim bool want_signal) 76535b9d88eSArnaldo Carvalho de Melo { 76635b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 76735b9d88eSArnaldo Carvalho de Melo char bf; 76835b9d88eSArnaldo Carvalho de Melo 76935b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 77035b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 77135b9d88eSArnaldo Carvalho de Melo return -1; 77235b9d88eSArnaldo Carvalho de Melo } 77335b9d88eSArnaldo Carvalho de Melo 77435b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 77535b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 77635b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 77735b9d88eSArnaldo Carvalho de Melo } 77835b9d88eSArnaldo Carvalho de Melo 77935b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 78035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 78135b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 78235b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 78335b9d88eSArnaldo Carvalho de Melo } 78435b9d88eSArnaldo Carvalho de Melo 78535b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 786119fa3c9SNamhyung Kim if (pipe_output) 78735b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 78835b9d88eSArnaldo Carvalho de Melo 7890817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 7900817df08SDavid Ahern 79135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 79235b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 79335b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 79435b9d88eSArnaldo Carvalho de Melo 79535b9d88eSArnaldo Carvalho de Melo /* 79635b9d88eSArnaldo Carvalho de Melo * Do a dummy execvp to get the PLT entry resolved, 79735b9d88eSArnaldo Carvalho de Melo * so we avoid the resolver overhead on the real 79835b9d88eSArnaldo Carvalho de Melo * execvp call. 79935b9d88eSArnaldo Carvalho de Melo */ 80035b9d88eSArnaldo Carvalho de Melo execvp("", (char **)argv); 80135b9d88eSArnaldo Carvalho de Melo 80235b9d88eSArnaldo Carvalho de Melo /* 80335b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 80435b9d88eSArnaldo Carvalho de Melo */ 80535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 80635b9d88eSArnaldo Carvalho de Melo 80735b9d88eSArnaldo Carvalho de Melo /* 80835b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 80935b9d88eSArnaldo Carvalho de Melo */ 81035b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 81135b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 81235b9d88eSArnaldo Carvalho de Melo 81335b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 81435b9d88eSArnaldo Carvalho de Melo 81535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 81655e162eaSNamhyung Kim if (want_signal) 81735b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 81835b9d88eSArnaldo Carvalho de Melo exit(-1); 81935b9d88eSArnaldo Carvalho de Melo } 82035b9d88eSArnaldo Carvalho de Melo 8216ef73ec4SNamhyung Kim if (perf_target__none(target)) 82235b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 82335b9d88eSArnaldo Carvalho de Melo 82435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 82535b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 82635b9d88eSArnaldo Carvalho de Melo /* 82735b9d88eSArnaldo Carvalho de Melo * wait for child to settle 82835b9d88eSArnaldo Carvalho de Melo */ 82935b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 83035b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 83135b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 83235b9d88eSArnaldo Carvalho de Melo } 83335b9d88eSArnaldo Carvalho de Melo 834bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 83535b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 83635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 83735b9d88eSArnaldo Carvalho de Melo return 0; 83835b9d88eSArnaldo Carvalho de Melo 83935b9d88eSArnaldo Carvalho de Melo out_close_pipes: 84035b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 84135b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 84235b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 84335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 84435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 84535b9d88eSArnaldo Carvalho de Melo return -1; 84635b9d88eSArnaldo Carvalho de Melo } 84735b9d88eSArnaldo Carvalho de Melo 84835b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 84935b9d88eSArnaldo Carvalho de Melo { 85035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 851b3824404SDavid Ahern char bf = 0; 852bcf3145fSNamhyung Kim int ret; 85335b9d88eSArnaldo Carvalho de Melo /* 85435b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 85535b9d88eSArnaldo Carvalho de Melo */ 856bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 857bcf3145fSNamhyung Kim if (ret < 0) 858bcf3145fSNamhyung Kim perror("enable to write to pipe"); 859bcf3145fSNamhyung Kim 860bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 861bcf3145fSNamhyung Kim return ret; 86235b9d88eSArnaldo Carvalho de Melo } 86335b9d88eSArnaldo Carvalho de Melo 86435b9d88eSArnaldo Carvalho de Melo return 0; 86535b9d88eSArnaldo Carvalho de Melo } 866cb0b29e0SArnaldo Carvalho de Melo 867a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 8680807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 869cb0b29e0SArnaldo Carvalho de Melo { 8700c21f736SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evlist__first(evlist); 8710807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 872cb0b29e0SArnaldo Carvalho de Melo } 87378f067b3SArnaldo Carvalho de Melo 87478f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 87578f067b3SArnaldo Carvalho de Melo { 87678f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 87778f067b3SArnaldo Carvalho de Melo size_t printed = 0; 87878f067b3SArnaldo Carvalho de Melo 87978f067b3SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 88078f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 88178f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 88278f067b3SArnaldo Carvalho de Melo } 88378f067b3SArnaldo Carvalho de Melo 88478f067b3SArnaldo Carvalho de Melo return printed + fprintf(fp, "\n");; 88578f067b3SArnaldo Carvalho de Melo } 886