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 317c4861afeSJiri Olsa /* 318c4861afeSJiri Olsa * This way does not work with group format read, so bail 319c4861afeSJiri Olsa * out in that case. 320c4861afeSJiri Olsa */ 321c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 322c4861afeSJiri Olsa return -1; 323c4861afeSJiri Olsa 324f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 325f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 326f8a95309SArnaldo Carvalho de Melo return -1; 327f8a95309SArnaldo Carvalho de Melo 328f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 329f8a95309SArnaldo Carvalho de Melo ++id_idx; 330f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 331f8a95309SArnaldo Carvalho de Melo ++id_idx; 332f8a95309SArnaldo Carvalho de Melo 333e2b5abe0SJiri Olsa id = read_data[id_idx]; 334e2b5abe0SJiri Olsa 335e2b5abe0SJiri Olsa add: 336e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 337f8a95309SArnaldo Carvalho de Melo return 0; 338f8a95309SArnaldo Carvalho de Melo } 339f8a95309SArnaldo Carvalho de Melo 34070db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 34170db7533SArnaldo Carvalho de Melo { 34270db7533SArnaldo Carvalho de Melo struct hlist_head *head; 34370db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 34470db7533SArnaldo Carvalho de Melo int hash; 34570db7533SArnaldo Carvalho de Melo 34670db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 3470c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 34870db7533SArnaldo Carvalho de Melo 34970db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 35070db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 35170db7533SArnaldo Carvalho de Melo 352b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 35370db7533SArnaldo Carvalho de Melo if (sid->id == id) 35470db7533SArnaldo Carvalho de Melo return sid->evsel; 35530e68bccSNamhyung Kim 35630e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 3570c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 35830e68bccSNamhyung Kim 35970db7533SArnaldo Carvalho de Melo return NULL; 36070db7533SArnaldo Carvalho de Melo } 36104391debSArnaldo Carvalho de Melo 362aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 36304391debSArnaldo Carvalho de Melo { 364aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 36504391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 36604391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 36704391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3688115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 36904391debSArnaldo Carvalho de Melo 3707bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 37104391debSArnaldo Carvalho de Melo /* 37204391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 37304391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 37404391debSArnaldo Carvalho de Melo * 37504391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 37604391debSArnaldo Carvalho de Melo * 37704391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 37804391debSArnaldo Carvalho de Melo */ 3797bb41152SArnaldo Carvalho de Melo int diff = head - old; 38004391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 38104391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 38204391debSArnaldo Carvalho de Melo 38304391debSArnaldo Carvalho de Melo /* 38404391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 38504391debSArnaldo Carvalho de Melo */ 38604391debSArnaldo Carvalho de Melo old = head; 38704391debSArnaldo Carvalho de Melo } 3887bb41152SArnaldo Carvalho de Melo } 38904391debSArnaldo Carvalho de Melo 39004391debSArnaldo Carvalho de Melo if (old != head) { 39104391debSArnaldo Carvalho de Melo size_t size; 39204391debSArnaldo Carvalho de Melo 3938115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 39404391debSArnaldo Carvalho de Melo size = event->header.size; 39504391debSArnaldo Carvalho de Melo 39604391debSArnaldo Carvalho de Melo /* 39704391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 39804391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 39904391debSArnaldo Carvalho de Melo */ 40004391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 40104391debSArnaldo Carvalho de Melo unsigned int offset = old; 40204391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 4030479b8b9SDavid Ahern void *dst = &md->event_copy; 40404391debSArnaldo Carvalho de Melo 40504391debSArnaldo Carvalho de Melo do { 40604391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 40704391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 40804391debSArnaldo Carvalho de Melo offset += cpy; 40904391debSArnaldo Carvalho de Melo dst += cpy; 41004391debSArnaldo Carvalho de Melo len -= cpy; 41104391debSArnaldo Carvalho de Melo } while (len); 41204391debSArnaldo Carvalho de Melo 4130479b8b9SDavid Ahern event = &md->event_copy; 41404391debSArnaldo Carvalho de Melo } 41504391debSArnaldo Carvalho de Melo 41604391debSArnaldo Carvalho de Melo old += size; 41704391debSArnaldo Carvalho de Melo } 41804391debSArnaldo Carvalho de Melo 41904391debSArnaldo Carvalho de Melo md->prev = old; 4207bb41152SArnaldo Carvalho de Melo 4217bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 4227bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 4237bb41152SArnaldo Carvalho de Melo 42404391debSArnaldo Carvalho de Melo return event; 42504391debSArnaldo Carvalho de Melo } 426f8a95309SArnaldo Carvalho de Melo 42793edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 42893edcbd9SAdrian Hunter { 42993edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 43093edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 43193edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 43293edcbd9SAdrian Hunter } 43393edcbd9SAdrian Hunter } 43493edcbd9SAdrian Hunter 4357e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 436f8a95309SArnaldo Carvalho de Melo { 437aece948fSArnaldo Carvalho de Melo int i; 438f8a95309SArnaldo Carvalho de Melo 43993edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 44093edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 441aece948fSArnaldo Carvalho de Melo 442aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 443aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 444f8a95309SArnaldo Carvalho de Melo } 445f8a95309SArnaldo Carvalho de Melo 446806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 447f8a95309SArnaldo Carvalho de Melo { 448a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 449ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 450b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 451aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 452f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 453f8a95309SArnaldo Carvalho de Melo } 454f8a95309SArnaldo Carvalho de Melo 455bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 456aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 457f8a95309SArnaldo Carvalho de Melo { 458aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 459aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 460aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 461f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 462301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 463301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 464f8a95309SArnaldo Carvalho de Melo return -1; 465301b195dSNelson Elhage } 466f8a95309SArnaldo Carvalho de Melo 467f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 468f8a95309SArnaldo Carvalho de Melo return 0; 469f8a95309SArnaldo Carvalho de Melo } 470f8a95309SArnaldo Carvalho de Melo 471aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 472aece948fSArnaldo Carvalho de Melo { 473aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 474aece948fSArnaldo Carvalho de Melo int cpu, thread; 475b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 476b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 477aece948fSArnaldo Carvalho de Melo 478b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 479aece948fSArnaldo Carvalho de Melo int output = -1; 480aece948fSArnaldo Carvalho de Melo 481b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 482aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 483aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 484aece948fSArnaldo Carvalho de Melo 485aece948fSArnaldo Carvalho de Melo if (output == -1) { 486aece948fSArnaldo Carvalho de Melo output = fd; 487bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 488aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 489aece948fSArnaldo Carvalho de Melo goto out_unmap; 490aece948fSArnaldo Carvalho de Melo } else { 491aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 492aece948fSArnaldo Carvalho de Melo goto out_unmap; 493aece948fSArnaldo Carvalho de Melo } 494aece948fSArnaldo Carvalho de Melo 495aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 496aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 497aece948fSArnaldo Carvalho de Melo goto out_unmap; 498aece948fSArnaldo Carvalho de Melo } 499aece948fSArnaldo Carvalho de Melo } 500aece948fSArnaldo Carvalho de Melo } 501aece948fSArnaldo Carvalho de Melo 502aece948fSArnaldo Carvalho de Melo return 0; 503aece948fSArnaldo Carvalho de Melo 504aece948fSArnaldo Carvalho de Melo out_unmap: 50593edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 50693edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 507aece948fSArnaldo Carvalho de Melo return -1; 508aece948fSArnaldo Carvalho de Melo } 509aece948fSArnaldo Carvalho de Melo 510aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 511aece948fSArnaldo Carvalho de Melo { 512aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 513aece948fSArnaldo Carvalho de Melo int thread; 514b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 515aece948fSArnaldo Carvalho de Melo 516b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 517aece948fSArnaldo Carvalho de Melo int output = -1; 518aece948fSArnaldo Carvalho de Melo 519aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 520aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 521aece948fSArnaldo Carvalho de Melo 522aece948fSArnaldo Carvalho de Melo if (output == -1) { 523aece948fSArnaldo Carvalho de Melo output = fd; 524bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 525aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 526aece948fSArnaldo Carvalho de Melo goto out_unmap; 527aece948fSArnaldo Carvalho de Melo } else { 528aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 529aece948fSArnaldo Carvalho de Melo goto out_unmap; 530aece948fSArnaldo Carvalho de Melo } 531aece948fSArnaldo Carvalho de Melo 532aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 533aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 534aece948fSArnaldo Carvalho de Melo goto out_unmap; 535aece948fSArnaldo Carvalho de Melo } 536aece948fSArnaldo Carvalho de Melo } 537aece948fSArnaldo Carvalho de Melo 538aece948fSArnaldo Carvalho de Melo return 0; 539aece948fSArnaldo Carvalho de Melo 540aece948fSArnaldo Carvalho de Melo out_unmap: 54193edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 54293edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 543aece948fSArnaldo Carvalho de Melo return -1; 544aece948fSArnaldo Carvalho de Melo } 545aece948fSArnaldo Carvalho de Melo 546f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 547f8a95309SArnaldo Carvalho de Melo * 548f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 549f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 550f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 551f8a95309SArnaldo Carvalho de Melo * 552f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 553f8a95309SArnaldo Carvalho de Melo * 554f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 555f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 556f8a95309SArnaldo Carvalho de Melo * 557f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5587e2ed097SArnaldo Carvalho de Melo * 5597e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 560f8a95309SArnaldo Carvalho de Melo */ 56150a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 56250a682ceSArnaldo Carvalho de Melo bool overwrite) 563f8a95309SArnaldo Carvalho de Melo { 564aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5657e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5667e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 56750a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 56850a682ceSArnaldo Carvalho de Melo 56950a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 57050a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 57150a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 57241d0d933SNelson Elhage else if (!is_power_of_2(pages)) 57341d0d933SNelson Elhage return -EINVAL; 57450a682ceSArnaldo Carvalho de Melo 57550a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 576f8a95309SArnaldo Carvalho de Melo 5777e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 578f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 579f8a95309SArnaldo Carvalho de Melo 5807e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 581f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 582f8a95309SArnaldo Carvalho de Melo 583f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 584f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 585f8a95309SArnaldo Carvalho de Melo 586f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 587f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 588a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 589a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 590f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 591f8a95309SArnaldo Carvalho de Melo } 592f8a95309SArnaldo Carvalho de Melo 593ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 594aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 595f8a95309SArnaldo Carvalho de Melo 596aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 597f8a95309SArnaldo Carvalho de Melo } 5987e2ed097SArnaldo Carvalho de Melo 599b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 600b809ac10SNamhyung Kim struct perf_target *target) 6017e2ed097SArnaldo Carvalho de Melo { 602b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 603b809ac10SNamhyung Kim target->uid); 6047e2ed097SArnaldo Carvalho de Melo 6057e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 6067e2ed097SArnaldo Carvalho de Melo return -1; 6077e2ed097SArnaldo Carvalho de Melo 608879d77d0SNamhyung Kim if (perf_target__has_task(target)) 609d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 610d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 611d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 612879d77d0SNamhyung Kim else 613879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 6147e2ed097SArnaldo Carvalho de Melo 6157e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 6167e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 6177e2ed097SArnaldo Carvalho de Melo 6187e2ed097SArnaldo Carvalho de Melo return 0; 6197e2ed097SArnaldo Carvalho de Melo 6207e2ed097SArnaldo Carvalho de Melo out_delete_threads: 6217e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6227e2ed097SArnaldo Carvalho de Melo return -1; 6237e2ed097SArnaldo Carvalho de Melo } 6247e2ed097SArnaldo Carvalho de Melo 6257e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 6267e2ed097SArnaldo Carvalho de Melo { 6277e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 6287e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6297e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 6307e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 6317e2ed097SArnaldo Carvalho de Melo } 6320a102479SFrederic Weisbecker 6331491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 6340a102479SFrederic Weisbecker { 6350a102479SFrederic Weisbecker struct perf_evsel *evsel; 636745cefc5SArnaldo Carvalho de Melo int err = 0; 637745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 638b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 6390a102479SFrederic Weisbecker 6400a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 641745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 6420a102479SFrederic Weisbecker continue; 643745cefc5SArnaldo Carvalho de Melo 644745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 6450a102479SFrederic Weisbecker if (err) 646745cefc5SArnaldo Carvalho de Melo break; 6470a102479SFrederic Weisbecker } 6480a102479SFrederic Weisbecker 649745cefc5SArnaldo Carvalho de Melo return err; 650745cefc5SArnaldo Carvalho de Melo } 651745cefc5SArnaldo Carvalho de Melo 652745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 653745cefc5SArnaldo Carvalho de Melo { 654745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 655745cefc5SArnaldo Carvalho de Melo int err = 0; 656745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 657b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 658745cefc5SArnaldo Carvalho de Melo 659745cefc5SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 660745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 661745cefc5SArnaldo Carvalho de Melo if (err) 662745cefc5SArnaldo Carvalho de Melo break; 663745cefc5SArnaldo Carvalho de Melo } 664745cefc5SArnaldo Carvalho de Melo 665745cefc5SArnaldo Carvalho de Melo return err; 6660a102479SFrederic Weisbecker } 66774429964SFrederic Weisbecker 6680c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 66974429964SFrederic Weisbecker { 6700c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 671c2a70653SArnaldo Carvalho de Melo 672c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 673c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 674c2a70653SArnaldo Carvalho de Melo return false; 67574429964SFrederic Weisbecker } 67674429964SFrederic Weisbecker 677c2a70653SArnaldo Carvalho de Melo return true; 678c2a70653SArnaldo Carvalho de Melo } 679c2a70653SArnaldo Carvalho de Melo 6800c21f736SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(struct perf_evlist *evlist) 681c2a70653SArnaldo Carvalho de Melo { 6820c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 683c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 684c2a70653SArnaldo Carvalho de Melo } 685c2a70653SArnaldo Carvalho de Melo 6869ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 6879ede473cSJiri Olsa { 6889ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 6899ede473cSJiri Olsa u64 read_format = first->attr.read_format; 6909ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 6919ede473cSJiri Olsa 6929ede473cSJiri Olsa list_for_each_entry_continue(pos, &evlist->entries, node) { 6939ede473cSJiri Olsa if (read_format != pos->attr.read_format) 6949ede473cSJiri Olsa return false; 6959ede473cSJiri Olsa } 6969ede473cSJiri Olsa 6979ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 6989ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 6999ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 7009ede473cSJiri Olsa return false; 7019ede473cSJiri Olsa } 7029ede473cSJiri Olsa 7039ede473cSJiri Olsa return true; 7049ede473cSJiri Olsa } 7059ede473cSJiri Olsa 7069ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 7079ede473cSJiri Olsa { 7089ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 7099ede473cSJiri Olsa return first->attr.read_format; 7109ede473cSJiri Olsa } 7119ede473cSJiri Olsa 7120c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 71381e36bffSArnaldo Carvalho de Melo { 7140c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 71581e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 71681e36bffSArnaldo Carvalho de Melo u64 sample_type; 71781e36bffSArnaldo Carvalho de Melo u16 size = 0; 71881e36bffSArnaldo Carvalho de Melo 71981e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 72081e36bffSArnaldo Carvalho de Melo goto out; 72181e36bffSArnaldo Carvalho de Melo 72281e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 72381e36bffSArnaldo Carvalho de Melo 72481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 72581e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 72681e36bffSArnaldo Carvalho de Melo 72781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 72881e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 72981e36bffSArnaldo Carvalho de Melo 73081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 73181e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 73281e36bffSArnaldo Carvalho de Melo 73381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 73481e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 73581e36bffSArnaldo Carvalho de Melo 73681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 73781e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 73881e36bffSArnaldo Carvalho de Melo out: 73981e36bffSArnaldo Carvalho de Melo return size; 74081e36bffSArnaldo Carvalho de Melo } 74181e36bffSArnaldo Carvalho de Melo 7420c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 743c2a70653SArnaldo Carvalho de Melo { 7440c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 745c2a70653SArnaldo Carvalho de Melo 746c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 747c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 748c2a70653SArnaldo Carvalho de Melo return false; 749c2a70653SArnaldo Carvalho de Melo } 750c2a70653SArnaldo Carvalho de Melo 751c2a70653SArnaldo Carvalho de Melo return true; 75274429964SFrederic Weisbecker } 75374429964SFrederic Weisbecker 7540c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 75574429964SFrederic Weisbecker { 7560c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 757c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 75874429964SFrederic Weisbecker } 75981cce8deSArnaldo Carvalho de Melo 76081cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 76181cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 76281cce8deSArnaldo Carvalho de Melo { 76381cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 76481cce8deSArnaldo Carvalho de Melo } 765727ab04eSArnaldo Carvalho de Melo 766a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 767a74b4b66SNamhyung Kim { 768a74b4b66SNamhyung Kim struct perf_evsel *evsel; 769a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 770a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 771a74b4b66SNamhyung Kim 772a74b4b66SNamhyung Kim list_for_each_entry_reverse(evsel, &evlist->entries, node) 773a74b4b66SNamhyung Kim perf_evsel__close(evsel, ncpus, nthreads); 774a74b4b66SNamhyung Kim } 775a74b4b66SNamhyung Kim 7766a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 777727ab04eSArnaldo Carvalho de Melo { 7786a4bb04cSJiri Olsa struct perf_evsel *evsel; 779a74b4b66SNamhyung Kim int err; 780727ab04eSArnaldo Carvalho de Melo 781727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 7826a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 783727ab04eSArnaldo Carvalho de Melo if (err < 0) 784727ab04eSArnaldo Carvalho de Melo goto out_err; 785727ab04eSArnaldo Carvalho de Melo } 786727ab04eSArnaldo Carvalho de Melo 787727ab04eSArnaldo Carvalho de Melo return 0; 788727ab04eSArnaldo Carvalho de Melo out_err: 789a74b4b66SNamhyung Kim perf_evlist__close(evlist); 79041c21a68SNamhyung Kim errno = -err; 791727ab04eSArnaldo Carvalho de Melo return err; 792727ab04eSArnaldo Carvalho de Melo } 79335b9d88eSArnaldo Carvalho de Melo 79435b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 7956ef73ec4SNamhyung Kim struct perf_target *target, 79655e162eaSNamhyung Kim const char *argv[], bool pipe_output, 79755e162eaSNamhyung Kim bool want_signal) 79835b9d88eSArnaldo Carvalho de Melo { 79935b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 80035b9d88eSArnaldo Carvalho de Melo char bf; 80135b9d88eSArnaldo Carvalho de Melo 80235b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 80335b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 80435b9d88eSArnaldo Carvalho de Melo return -1; 80535b9d88eSArnaldo Carvalho de Melo } 80635b9d88eSArnaldo Carvalho de Melo 80735b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 80835b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 80935b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 81035b9d88eSArnaldo Carvalho de Melo } 81135b9d88eSArnaldo Carvalho de Melo 81235b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 81335b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 81435b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 81535b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 81635b9d88eSArnaldo Carvalho de Melo } 81735b9d88eSArnaldo Carvalho de Melo 81835b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 819119fa3c9SNamhyung Kim if (pipe_output) 82035b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 82135b9d88eSArnaldo Carvalho de Melo 8220817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 8230817df08SDavid Ahern 82435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 82535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 82635b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 82735b9d88eSArnaldo Carvalho de Melo 82835b9d88eSArnaldo Carvalho de Melo /* 82935b9d88eSArnaldo Carvalho de Melo * Do a dummy execvp to get the PLT entry resolved, 83035b9d88eSArnaldo Carvalho de Melo * so we avoid the resolver overhead on the real 83135b9d88eSArnaldo Carvalho de Melo * execvp call. 83235b9d88eSArnaldo Carvalho de Melo */ 83335b9d88eSArnaldo Carvalho de Melo execvp("", (char **)argv); 83435b9d88eSArnaldo Carvalho de Melo 83535b9d88eSArnaldo Carvalho de Melo /* 83635b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 83735b9d88eSArnaldo Carvalho de Melo */ 83835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 83935b9d88eSArnaldo Carvalho de Melo 84035b9d88eSArnaldo Carvalho de Melo /* 84135b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 84235b9d88eSArnaldo Carvalho de Melo */ 84335b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 84435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 84535b9d88eSArnaldo Carvalho de Melo 84635b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 84735b9d88eSArnaldo Carvalho de Melo 84835b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 84955e162eaSNamhyung Kim if (want_signal) 85035b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 85135b9d88eSArnaldo Carvalho de Melo exit(-1); 85235b9d88eSArnaldo Carvalho de Melo } 85335b9d88eSArnaldo Carvalho de Melo 8546ef73ec4SNamhyung Kim if (perf_target__none(target)) 85535b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 85635b9d88eSArnaldo Carvalho de Melo 85735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 85835b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 85935b9d88eSArnaldo Carvalho de Melo /* 86035b9d88eSArnaldo Carvalho de Melo * wait for child to settle 86135b9d88eSArnaldo Carvalho de Melo */ 86235b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 86335b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 86435b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 86535b9d88eSArnaldo Carvalho de Melo } 86635b9d88eSArnaldo Carvalho de Melo 867bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 86835b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 86935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 87035b9d88eSArnaldo Carvalho de Melo return 0; 87135b9d88eSArnaldo Carvalho de Melo 87235b9d88eSArnaldo Carvalho de Melo out_close_pipes: 87335b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 87435b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 87535b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 87635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 87735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 87835b9d88eSArnaldo Carvalho de Melo return -1; 87935b9d88eSArnaldo Carvalho de Melo } 88035b9d88eSArnaldo Carvalho de Melo 88135b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 88235b9d88eSArnaldo Carvalho de Melo { 88335b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 884b3824404SDavid Ahern char bf = 0; 885bcf3145fSNamhyung Kim int ret; 88635b9d88eSArnaldo Carvalho de Melo /* 88735b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 88835b9d88eSArnaldo Carvalho de Melo */ 889bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 890bcf3145fSNamhyung Kim if (ret < 0) 891bcf3145fSNamhyung Kim perror("enable to write to pipe"); 892bcf3145fSNamhyung Kim 893bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 894bcf3145fSNamhyung Kim return ret; 89535b9d88eSArnaldo Carvalho de Melo } 89635b9d88eSArnaldo Carvalho de Melo 89735b9d88eSArnaldo Carvalho de Melo return 0; 89835b9d88eSArnaldo Carvalho de Melo } 899cb0b29e0SArnaldo Carvalho de Melo 900a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 9010807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 902cb0b29e0SArnaldo Carvalho de Melo { 9030c21f736SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evlist__first(evlist); 9040807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 905cb0b29e0SArnaldo Carvalho de Melo } 90678f067b3SArnaldo Carvalho de Melo 90778f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 90878f067b3SArnaldo Carvalho de Melo { 90978f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 91078f067b3SArnaldo Carvalho de Melo size_t printed = 0; 91178f067b3SArnaldo Carvalho de Melo 91278f067b3SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 91378f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 91478f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 91578f067b3SArnaldo Carvalho de Melo } 91678f067b3SArnaldo Carvalho de Melo 91778f067b3SArnaldo Carvalho de Melo return printed + fprintf(fp, "\n");; 91878f067b3SArnaldo Carvalho de Melo } 919