1f8a95309SArnaldo Carvalho de Melo /* 2f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3f8a95309SArnaldo Carvalho de Melo * 4f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 5f8a95309SArnaldo Carvalho de Melo * copyright notes. 6f8a95309SArnaldo Carvalho de Melo * 7f8a95309SArnaldo Carvalho de Melo * Released under the GPL v2. (and only v2, not any later version) 8f8a95309SArnaldo Carvalho de Melo */ 9a8c9ae18SArnaldo Carvalho de Melo #include "util.h" 1085c66be1SBorislav Petkov #include <lk/debugfs.h> 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1412864b31SNamhyung Kim #include "target.h" 15361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 16361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 1735b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 18361c99a6SArnaldo Carvalho de Melo 1950d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 2050d08e47SArnaldo Carvalho de Melo 21f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 22f8a95309SArnaldo Carvalho de Melo 2370db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2470db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 2570db7533SArnaldo Carvalho de Melo 26f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 27a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 28f8a95309SArnaldo Carvalho de Melo 297e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 307e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 31361c99a6SArnaldo Carvalho de Melo { 3270db7533SArnaldo Carvalho de Melo int i; 3370db7533SArnaldo Carvalho de Melo 3470db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 3570db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 36361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 377e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 3835b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 39361c99a6SArnaldo Carvalho de Melo } 40361c99a6SArnaldo Carvalho de Melo 41334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 42ef1d1af2SArnaldo Carvalho de Melo { 43ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 44ef1d1af2SArnaldo Carvalho de Melo 45ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 46334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 47ef1d1af2SArnaldo Carvalho de Melo 48361c99a6SArnaldo Carvalho de Melo return evlist; 49361c99a6SArnaldo Carvalho de Melo } 50361c99a6SArnaldo Carvalho de Melo 51f77a9518SArnaldo Carvalho de Melo void perf_evlist__config(struct perf_evlist *evlist, 520f82ebc4SArnaldo Carvalho de Melo struct perf_record_opts *opts) 530f82ebc4SArnaldo Carvalho de Melo { 54cac21425SJiri Olsa struct perf_evsel *evsel; 55f77a9518SArnaldo Carvalho de Melo /* 56f77a9518SArnaldo Carvalho de Melo * Set the evsel leader links before we configure attributes, 57f77a9518SArnaldo Carvalho de Melo * since some might depend on this info. 58f77a9518SArnaldo Carvalho de Melo */ 59f77a9518SArnaldo Carvalho de Melo if (opts->group) 60f77a9518SArnaldo Carvalho de Melo perf_evlist__set_leader(evlist); 610f82ebc4SArnaldo Carvalho de Melo 620f82ebc4SArnaldo Carvalho de Melo if (evlist->cpus->map[0] < 0) 630f82ebc4SArnaldo Carvalho de Melo opts->no_inherit = true; 640f82ebc4SArnaldo Carvalho de Melo 650f82ebc4SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 66cac21425SJiri Olsa perf_evsel__config(evsel, opts); 670f82ebc4SArnaldo Carvalho de Melo 680f82ebc4SArnaldo Carvalho de Melo if (evlist->nr_entries > 1) 693a5afaecSArnaldo Carvalho de Melo perf_evsel__set_sample_id(evsel); 700f82ebc4SArnaldo Carvalho de Melo } 710f82ebc4SArnaldo Carvalho de Melo } 720f82ebc4SArnaldo Carvalho de Melo 73361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 74361c99a6SArnaldo Carvalho de Melo { 75361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 76361c99a6SArnaldo Carvalho de Melo 77361c99a6SArnaldo Carvalho de Melo list_for_each_entry_safe(pos, n, &evlist->entries, node) { 78361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 79361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 80361c99a6SArnaldo Carvalho de Melo } 81361c99a6SArnaldo Carvalho de Melo 82361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 83361c99a6SArnaldo Carvalho de Melo } 84361c99a6SArnaldo Carvalho de Melo 85ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 86ef1d1af2SArnaldo Carvalho de Melo { 87ef1d1af2SArnaldo Carvalho de Melo free(evlist->mmap); 88ef1d1af2SArnaldo Carvalho de Melo free(evlist->pollfd); 89ef1d1af2SArnaldo Carvalho de Melo evlist->mmap = NULL; 90ef1d1af2SArnaldo Carvalho de Melo evlist->pollfd = NULL; 91ef1d1af2SArnaldo Carvalho de Melo } 92ef1d1af2SArnaldo Carvalho de Melo 93361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 94361c99a6SArnaldo Carvalho de Melo { 95361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 96ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 97361c99a6SArnaldo Carvalho de Melo free(evlist); 98361c99a6SArnaldo Carvalho de Melo } 99361c99a6SArnaldo Carvalho de Melo 100361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 101361c99a6SArnaldo Carvalho de Melo { 102361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 103361c99a6SArnaldo Carvalho de Melo ++evlist->nr_entries; 104361c99a6SArnaldo Carvalho de Melo } 105361c99a6SArnaldo Carvalho de Melo 1060529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 10750d08e47SArnaldo Carvalho de Melo struct list_head *list, 10850d08e47SArnaldo Carvalho de Melo int nr_entries) 10950d08e47SArnaldo Carvalho de Melo { 11050d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 11150d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 11250d08e47SArnaldo Carvalho de Melo } 11350d08e47SArnaldo Carvalho de Melo 11463dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 11563dab225SArnaldo Carvalho de Melo { 11663dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 11763dab225SArnaldo Carvalho de Melo 11863dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 11997f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 12097f63e4aSNamhyung Kim 12197f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 12263dab225SArnaldo Carvalho de Melo 12363dab225SArnaldo Carvalho de Melo list_for_each_entry(evsel, list, node) { 12463dab225SArnaldo Carvalho de Melo evsel->leader = leader; 12563dab225SArnaldo Carvalho de Melo } 12663dab225SArnaldo Carvalho de Melo } 12763dab225SArnaldo Carvalho de Melo 12863dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1296a4bb04cSJiri Olsa { 13097f63e4aSNamhyung Kim if (evlist->nr_entries) { 13197f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 13263dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1336a4bb04cSJiri Olsa } 13497f63e4aSNamhyung Kim } 1356a4bb04cSJiri Olsa 136361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 137361c99a6SArnaldo Carvalho de Melo { 138361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 139361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 140361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 141361c99a6SArnaldo Carvalho de Melo }; 1421aed2671SJoerg Roedel struct perf_evsel *evsel; 143361c99a6SArnaldo Carvalho de Melo 1441aed2671SJoerg Roedel event_attr_init(&attr); 1451aed2671SJoerg Roedel 1461aed2671SJoerg Roedel evsel = perf_evsel__new(&attr, 0); 147361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 148cc2d86b0SStephane Eranian goto error; 149cc2d86b0SStephane Eranian 150cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 151cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 152cc2d86b0SStephane Eranian if (!evsel->name) 153cc2d86b0SStephane Eranian goto error_free; 154361c99a6SArnaldo Carvalho de Melo 155361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 156361c99a6SArnaldo Carvalho de Melo return 0; 157cc2d86b0SStephane Eranian error_free: 158cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 159cc2d86b0SStephane Eranian error: 160cc2d86b0SStephane Eranian return -ENOMEM; 161361c99a6SArnaldo Carvalho de Melo } 1625c581041SArnaldo Carvalho de Melo 163e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 16450d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 16550d08e47SArnaldo Carvalho de Melo { 16650d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 16750d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 16850d08e47SArnaldo Carvalho de Melo size_t i; 16950d08e47SArnaldo Carvalho de Melo 17050d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 17150d08e47SArnaldo Carvalho de Melo evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); 17250d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 17350d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 17450d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 17550d08e47SArnaldo Carvalho de Melo } 17650d08e47SArnaldo Carvalho de Melo 17750d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 17850d08e47SArnaldo Carvalho de Melo 17950d08e47SArnaldo Carvalho de Melo return 0; 18050d08e47SArnaldo Carvalho de Melo 18150d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 18250d08e47SArnaldo Carvalho de Melo list_for_each_entry_safe(evsel, n, &head, node) 18350d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 18450d08e47SArnaldo Carvalho de Melo return -1; 18550d08e47SArnaldo Carvalho de Melo } 18650d08e47SArnaldo Carvalho de Melo 18779695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 18879695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 18979695e1bSArnaldo Carvalho de Melo { 19079695e1bSArnaldo Carvalho de Melo size_t i; 19179695e1bSArnaldo Carvalho de Melo 19279695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 19379695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 19479695e1bSArnaldo Carvalho de Melo 19579695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 19679695e1bSArnaldo Carvalho de Melo } 19779695e1bSArnaldo Carvalho de Melo 198da378962SArnaldo Carvalho de Melo struct perf_evsel * 199ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 200ee29be62SArnaldo Carvalho de Melo { 201ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 202ee29be62SArnaldo Carvalho de Melo 203ee29be62SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 204ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 205ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 206ee29be62SArnaldo Carvalho de Melo return evsel; 207ee29be62SArnaldo Carvalho de Melo } 208ee29be62SArnaldo Carvalho de Melo 209ee29be62SArnaldo Carvalho de Melo return NULL; 210ee29be62SArnaldo Carvalho de Melo } 211ee29be62SArnaldo Carvalho de Melo 21239876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 21339876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 21439876e7dSArnaldo Carvalho de Melo { 21539876e7dSArnaldo Carvalho de Melo struct perf_evsel *evsel; 21639876e7dSArnaldo Carvalho de Melo 21739876e7dSArnaldo Carvalho de Melo evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); 21839876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 21939876e7dSArnaldo Carvalho de Melo return -1; 22039876e7dSArnaldo Carvalho de Melo 22139876e7dSArnaldo Carvalho de Melo evsel->handler.func = handler; 22239876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 22339876e7dSArnaldo Carvalho de Melo return 0; 22439876e7dSArnaldo Carvalho de Melo } 22539876e7dSArnaldo Carvalho de Melo 2264152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2274152ab37SArnaldo Carvalho de Melo { 2284152ab37SArnaldo Carvalho de Melo int cpu, thread; 2294152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 230b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 231b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 2324152ab37SArnaldo Carvalho de Melo 233b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 2344152ab37SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 235823254edSNamhyung Kim if (!perf_evsel__is_group_leader(pos)) 2363fe4430dSJiri Olsa continue; 237b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 23855da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 23955da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2404152ab37SArnaldo Carvalho de Melo } 2414152ab37SArnaldo Carvalho de Melo } 2424152ab37SArnaldo Carvalho de Melo } 2434152ab37SArnaldo Carvalho de Melo 244764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 245764e16a3SDavid Ahern { 246764e16a3SDavid Ahern int cpu, thread; 247764e16a3SDavid Ahern struct perf_evsel *pos; 248b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 249b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 250764e16a3SDavid Ahern 251b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 252764e16a3SDavid Ahern list_for_each_entry(pos, &evlist->entries, node) { 253823254edSNamhyung Kim if (!perf_evsel__is_group_leader(pos)) 2543fe4430dSJiri Olsa continue; 255b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 25655da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 25755da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 258764e16a3SDavid Ahern } 259764e16a3SDavid Ahern } 260764e16a3SDavid Ahern } 261764e16a3SDavid Ahern 262806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 2635c581041SArnaldo Carvalho de Melo { 264b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 265b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 266b3a319d5SNamhyung Kim int nfds = nr_cpus * nr_threads * evlist->nr_entries; 2675c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 2685c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 2695c581041SArnaldo Carvalho de Melo } 27070082dd9SArnaldo Carvalho de Melo 27170082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 27270082dd9SArnaldo Carvalho de Melo { 27370082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 27470082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 27570082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 27670082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 27770082dd9SArnaldo Carvalho de Melo } 27870db7533SArnaldo Carvalho de Melo 279a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 280a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 2813d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 2823d3b5e95SArnaldo Carvalho de Melo { 2833d3b5e95SArnaldo Carvalho de Melo int hash; 2843d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 2853d3b5e95SArnaldo Carvalho de Melo 2863d3b5e95SArnaldo Carvalho de Melo sid->id = id; 2873d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 2883d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 2893d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 2903d3b5e95SArnaldo Carvalho de Melo } 2913d3b5e95SArnaldo Carvalho de Melo 292a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 293a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 294a91e5431SArnaldo Carvalho de Melo { 295a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 296a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 297a91e5431SArnaldo Carvalho de Melo } 298a91e5431SArnaldo Carvalho de Melo 299a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 3003d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 301f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 302f8a95309SArnaldo Carvalho de Melo { 303f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 3043d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 305f8a95309SArnaldo Carvalho de Melo 306f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 307f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 308f8a95309SArnaldo Carvalho de Melo return -1; 309f8a95309SArnaldo Carvalho de Melo 310f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 311f8a95309SArnaldo Carvalho de Melo ++id_idx; 312f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 313f8a95309SArnaldo Carvalho de Melo ++id_idx; 314f8a95309SArnaldo Carvalho de Melo 315a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 316f8a95309SArnaldo Carvalho de Melo return 0; 317f8a95309SArnaldo Carvalho de Melo } 318f8a95309SArnaldo Carvalho de Melo 31970db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 32070db7533SArnaldo Carvalho de Melo { 32170db7533SArnaldo Carvalho de Melo struct hlist_head *head; 32270db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 32370db7533SArnaldo Carvalho de Melo int hash; 32470db7533SArnaldo Carvalho de Melo 32570db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 3260c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 32770db7533SArnaldo Carvalho de Melo 32870db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 32970db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 33070db7533SArnaldo Carvalho de Melo 331b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 33270db7533SArnaldo Carvalho de Melo if (sid->id == id) 33370db7533SArnaldo Carvalho de Melo return sid->evsel; 33430e68bccSNamhyung Kim 33530e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 3360c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 33730e68bccSNamhyung Kim 33870db7533SArnaldo Carvalho de Melo return NULL; 33970db7533SArnaldo Carvalho de Melo } 34004391debSArnaldo Carvalho de Melo 341aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 34204391debSArnaldo Carvalho de Melo { 343aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 34404391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 34504391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 34604391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3478115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 34804391debSArnaldo Carvalho de Melo 3497bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 35004391debSArnaldo Carvalho de Melo /* 35104391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 35204391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 35304391debSArnaldo Carvalho de Melo * 35404391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 35504391debSArnaldo Carvalho de Melo * 35604391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 35704391debSArnaldo Carvalho de Melo */ 3587bb41152SArnaldo Carvalho de Melo int diff = head - old; 35904391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 36004391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 36104391debSArnaldo Carvalho de Melo 36204391debSArnaldo Carvalho de Melo /* 36304391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 36404391debSArnaldo Carvalho de Melo */ 36504391debSArnaldo Carvalho de Melo old = head; 36604391debSArnaldo Carvalho de Melo } 3677bb41152SArnaldo Carvalho de Melo } 36804391debSArnaldo Carvalho de Melo 36904391debSArnaldo Carvalho de Melo if (old != head) { 37004391debSArnaldo Carvalho de Melo size_t size; 37104391debSArnaldo Carvalho de Melo 3728115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 37304391debSArnaldo Carvalho de Melo size = event->header.size; 37404391debSArnaldo Carvalho de Melo 37504391debSArnaldo Carvalho de Melo /* 37604391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 37704391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 37804391debSArnaldo Carvalho de Melo */ 37904391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 38004391debSArnaldo Carvalho de Melo unsigned int offset = old; 38104391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 3820479b8b9SDavid Ahern void *dst = &md->event_copy; 38304391debSArnaldo Carvalho de Melo 38404391debSArnaldo Carvalho de Melo do { 38504391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 38604391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 38704391debSArnaldo Carvalho de Melo offset += cpy; 38804391debSArnaldo Carvalho de Melo dst += cpy; 38904391debSArnaldo Carvalho de Melo len -= cpy; 39004391debSArnaldo Carvalho de Melo } while (len); 39104391debSArnaldo Carvalho de Melo 3920479b8b9SDavid Ahern event = &md->event_copy; 39304391debSArnaldo Carvalho de Melo } 39404391debSArnaldo Carvalho de Melo 39504391debSArnaldo Carvalho de Melo old += size; 39604391debSArnaldo Carvalho de Melo } 39704391debSArnaldo Carvalho de Melo 39804391debSArnaldo Carvalho de Melo md->prev = old; 3997bb41152SArnaldo Carvalho de Melo 4007bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 4017bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 4027bb41152SArnaldo Carvalho de Melo 40304391debSArnaldo Carvalho de Melo return event; 40404391debSArnaldo Carvalho de Melo } 405f8a95309SArnaldo Carvalho de Melo 4067e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 407f8a95309SArnaldo Carvalho de Melo { 408aece948fSArnaldo Carvalho de Melo int i; 409f8a95309SArnaldo Carvalho de Melo 410aece948fSArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 411aece948fSArnaldo Carvalho de Melo if (evlist->mmap[i].base != NULL) { 412aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[i].base, evlist->mmap_len); 413aece948fSArnaldo Carvalho de Melo evlist->mmap[i].base = NULL; 414f8a95309SArnaldo Carvalho de Melo } 415f8a95309SArnaldo Carvalho de Melo } 416aece948fSArnaldo Carvalho de Melo 417aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 418aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 419f8a95309SArnaldo Carvalho de Melo } 420f8a95309SArnaldo Carvalho de Melo 421806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 422f8a95309SArnaldo Carvalho de Melo { 423a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 424ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 425b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 426aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 427f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 428f8a95309SArnaldo Carvalho de Melo } 429f8a95309SArnaldo Carvalho de Melo 430bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 431aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 432f8a95309SArnaldo Carvalho de Melo { 433aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 434aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 435aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 436f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 437301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 438301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 439f8a95309SArnaldo Carvalho de Melo return -1; 440301b195dSNelson Elhage } 441f8a95309SArnaldo Carvalho de Melo 442f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 443f8a95309SArnaldo Carvalho de Melo return 0; 444f8a95309SArnaldo Carvalho de Melo } 445f8a95309SArnaldo Carvalho de Melo 446aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 447aece948fSArnaldo Carvalho de Melo { 448aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 449aece948fSArnaldo Carvalho de Melo int cpu, thread; 450b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 451b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 452aece948fSArnaldo Carvalho de Melo 453b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 454aece948fSArnaldo Carvalho de Melo int output = -1; 455aece948fSArnaldo Carvalho de Melo 456b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 457aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 458aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 459aece948fSArnaldo Carvalho de Melo 460aece948fSArnaldo Carvalho de Melo if (output == -1) { 461aece948fSArnaldo Carvalho de Melo output = fd; 462bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 463aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 464aece948fSArnaldo Carvalho de Melo goto out_unmap; 465aece948fSArnaldo Carvalho de Melo } else { 466aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 467aece948fSArnaldo Carvalho de Melo goto out_unmap; 468aece948fSArnaldo Carvalho de Melo } 469aece948fSArnaldo Carvalho de Melo 470aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 471aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 472aece948fSArnaldo Carvalho de Melo goto out_unmap; 473aece948fSArnaldo Carvalho de Melo } 474aece948fSArnaldo Carvalho de Melo } 475aece948fSArnaldo Carvalho de Melo } 476aece948fSArnaldo Carvalho de Melo 477aece948fSArnaldo Carvalho de Melo return 0; 478aece948fSArnaldo Carvalho de Melo 479aece948fSArnaldo Carvalho de Melo out_unmap: 480b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 481aece948fSArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 482aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 483aece948fSArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 484aece948fSArnaldo Carvalho de Melo } 485aece948fSArnaldo Carvalho de Melo } 486aece948fSArnaldo Carvalho de Melo return -1; 487aece948fSArnaldo Carvalho de Melo } 488aece948fSArnaldo Carvalho de Melo 489aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 490aece948fSArnaldo Carvalho de Melo { 491aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 492aece948fSArnaldo Carvalho de Melo int thread; 493b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 494aece948fSArnaldo Carvalho de Melo 495b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 496aece948fSArnaldo Carvalho de Melo int output = -1; 497aece948fSArnaldo Carvalho de Melo 498aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 499aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 500aece948fSArnaldo Carvalho de Melo 501aece948fSArnaldo Carvalho de Melo if (output == -1) { 502aece948fSArnaldo Carvalho de Melo output = fd; 503bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 504aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 505aece948fSArnaldo Carvalho de Melo goto out_unmap; 506aece948fSArnaldo Carvalho de Melo } else { 507aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 508aece948fSArnaldo Carvalho de Melo goto out_unmap; 509aece948fSArnaldo Carvalho de Melo } 510aece948fSArnaldo Carvalho de Melo 511aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 512aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 513aece948fSArnaldo Carvalho de Melo goto out_unmap; 514aece948fSArnaldo Carvalho de Melo } 515aece948fSArnaldo Carvalho de Melo } 516aece948fSArnaldo Carvalho de Melo 517aece948fSArnaldo Carvalho de Melo return 0; 518aece948fSArnaldo Carvalho de Melo 519aece948fSArnaldo Carvalho de Melo out_unmap: 520b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 521aece948fSArnaldo Carvalho de Melo if (evlist->mmap[thread].base != NULL) { 522aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[thread].base, evlist->mmap_len); 523aece948fSArnaldo Carvalho de Melo evlist->mmap[thread].base = NULL; 524aece948fSArnaldo Carvalho de Melo } 525aece948fSArnaldo Carvalho de Melo } 526aece948fSArnaldo Carvalho de Melo return -1; 527aece948fSArnaldo Carvalho de Melo } 528aece948fSArnaldo Carvalho de Melo 529f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 530f8a95309SArnaldo Carvalho de Melo * 531f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 532f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 533f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 534f8a95309SArnaldo Carvalho de Melo * 535f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 536f8a95309SArnaldo Carvalho de Melo * 537f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 538f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 539f8a95309SArnaldo Carvalho de Melo * 540f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5417e2ed097SArnaldo Carvalho de Melo * 5427e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 543f8a95309SArnaldo Carvalho de Melo */ 54450a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 54550a682ceSArnaldo Carvalho de Melo bool overwrite) 546f8a95309SArnaldo Carvalho de Melo { 547aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5487e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5497e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 55050a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 55150a682ceSArnaldo Carvalho de Melo 55250a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 55350a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 55450a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 55541d0d933SNelson Elhage else if (!is_power_of_2(pages)) 55641d0d933SNelson Elhage return -EINVAL; 55750a682ceSArnaldo Carvalho de Melo 55850a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 559f8a95309SArnaldo Carvalho de Melo 5607e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 561f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 562f8a95309SArnaldo Carvalho de Melo 5637e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 564f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 565f8a95309SArnaldo Carvalho de Melo 566f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 567f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 568f8a95309SArnaldo Carvalho de Melo 569f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 570f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 571a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 572a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 573f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 574f8a95309SArnaldo Carvalho de Melo } 575f8a95309SArnaldo Carvalho de Melo 576ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 577aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 578f8a95309SArnaldo Carvalho de Melo 579aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 580f8a95309SArnaldo Carvalho de Melo } 5817e2ed097SArnaldo Carvalho de Melo 582b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 583b809ac10SNamhyung Kim struct perf_target *target) 5847e2ed097SArnaldo Carvalho de Melo { 585b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 586b809ac10SNamhyung Kim target->uid); 5877e2ed097SArnaldo Carvalho de Melo 5887e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 5897e2ed097SArnaldo Carvalho de Melo return -1; 5907e2ed097SArnaldo Carvalho de Melo 591879d77d0SNamhyung Kim if (perf_target__has_task(target)) 592d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 593d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 594d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 595879d77d0SNamhyung Kim else 596879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 5977e2ed097SArnaldo Carvalho de Melo 5987e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 5997e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 6007e2ed097SArnaldo Carvalho de Melo 6017e2ed097SArnaldo Carvalho de Melo return 0; 6027e2ed097SArnaldo Carvalho de Melo 6037e2ed097SArnaldo Carvalho de Melo out_delete_threads: 6047e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6057e2ed097SArnaldo Carvalho de Melo return -1; 6067e2ed097SArnaldo Carvalho de Melo } 6077e2ed097SArnaldo Carvalho de Melo 6087e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 6097e2ed097SArnaldo Carvalho de Melo { 6107e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 6117e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6127e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 6137e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 6147e2ed097SArnaldo Carvalho de Melo } 6150a102479SFrederic Weisbecker 6161491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 6170a102479SFrederic Weisbecker { 6180a102479SFrederic Weisbecker struct perf_evsel *evsel; 619745cefc5SArnaldo Carvalho de Melo int err = 0; 620745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 621b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 6220a102479SFrederic Weisbecker 6230a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 624745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 6250a102479SFrederic Weisbecker continue; 626745cefc5SArnaldo Carvalho de Melo 627745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 6280a102479SFrederic Weisbecker if (err) 629745cefc5SArnaldo Carvalho de Melo break; 6300a102479SFrederic Weisbecker } 6310a102479SFrederic Weisbecker 632745cefc5SArnaldo Carvalho de Melo return err; 633745cefc5SArnaldo Carvalho de Melo } 634745cefc5SArnaldo Carvalho de Melo 635745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 636745cefc5SArnaldo Carvalho de Melo { 637745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 638745cefc5SArnaldo Carvalho de Melo int err = 0; 639745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 640b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 641745cefc5SArnaldo Carvalho de Melo 642745cefc5SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 643745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 644745cefc5SArnaldo Carvalho de Melo if (err) 645745cefc5SArnaldo Carvalho de Melo break; 646745cefc5SArnaldo Carvalho de Melo } 647745cefc5SArnaldo Carvalho de Melo 648745cefc5SArnaldo Carvalho de Melo return err; 6490a102479SFrederic Weisbecker } 65074429964SFrederic Weisbecker 6510c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 65274429964SFrederic Weisbecker { 6530c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 654c2a70653SArnaldo Carvalho de Melo 655c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 656c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 657c2a70653SArnaldo Carvalho de Melo return false; 65874429964SFrederic Weisbecker } 65974429964SFrederic Weisbecker 660c2a70653SArnaldo Carvalho de Melo return true; 661c2a70653SArnaldo Carvalho de Melo } 662c2a70653SArnaldo Carvalho de Melo 6630c21f736SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(struct perf_evlist *evlist) 664c2a70653SArnaldo Carvalho de Melo { 6650c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 666c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 667c2a70653SArnaldo Carvalho de Melo } 668c2a70653SArnaldo Carvalho de Melo 6690c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 67081e36bffSArnaldo Carvalho de Melo { 6710c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 67281e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 67381e36bffSArnaldo Carvalho de Melo u64 sample_type; 67481e36bffSArnaldo Carvalho de Melo u16 size = 0; 67581e36bffSArnaldo Carvalho de Melo 67681e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 67781e36bffSArnaldo Carvalho de Melo goto out; 67881e36bffSArnaldo Carvalho de Melo 67981e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 68081e36bffSArnaldo Carvalho de Melo 68181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 68281e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 68381e36bffSArnaldo Carvalho de Melo 68481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 68581e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 68681e36bffSArnaldo Carvalho de Melo 68781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 68881e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 68981e36bffSArnaldo Carvalho de Melo 69081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 69181e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 69281e36bffSArnaldo Carvalho de Melo 69381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 69481e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 69581e36bffSArnaldo Carvalho de Melo out: 69681e36bffSArnaldo Carvalho de Melo return size; 69781e36bffSArnaldo Carvalho de Melo } 69881e36bffSArnaldo Carvalho de Melo 6990c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 700c2a70653SArnaldo Carvalho de Melo { 7010c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 702c2a70653SArnaldo Carvalho de Melo 703c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 704c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 705c2a70653SArnaldo Carvalho de Melo return false; 706c2a70653SArnaldo Carvalho de Melo } 707c2a70653SArnaldo Carvalho de Melo 708c2a70653SArnaldo Carvalho de Melo return true; 70974429964SFrederic Weisbecker } 71074429964SFrederic Weisbecker 7110c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 71274429964SFrederic Weisbecker { 7130c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 714c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 71574429964SFrederic Weisbecker } 71681cce8deSArnaldo Carvalho de Melo 71781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 71881cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 71981cce8deSArnaldo Carvalho de Melo { 72081cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 72181cce8deSArnaldo Carvalho de Melo } 722727ab04eSArnaldo Carvalho de Melo 723a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 724a74b4b66SNamhyung Kim { 725a74b4b66SNamhyung Kim struct perf_evsel *evsel; 726a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 727a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 728a74b4b66SNamhyung Kim 729a74b4b66SNamhyung Kim list_for_each_entry_reverse(evsel, &evlist->entries, node) 730a74b4b66SNamhyung Kim perf_evsel__close(evsel, ncpus, nthreads); 731a74b4b66SNamhyung Kim } 732a74b4b66SNamhyung Kim 7336a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 734727ab04eSArnaldo Carvalho de Melo { 7356a4bb04cSJiri Olsa struct perf_evsel *evsel; 736a74b4b66SNamhyung Kim int err; 737727ab04eSArnaldo Carvalho de Melo 738727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 7396a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 740727ab04eSArnaldo Carvalho de Melo if (err < 0) 741727ab04eSArnaldo Carvalho de Melo goto out_err; 742727ab04eSArnaldo Carvalho de Melo } 743727ab04eSArnaldo Carvalho de Melo 744727ab04eSArnaldo Carvalho de Melo return 0; 745727ab04eSArnaldo Carvalho de Melo out_err: 746a74b4b66SNamhyung Kim perf_evlist__close(evlist); 74741c21a68SNamhyung Kim errno = -err; 748727ab04eSArnaldo Carvalho de Melo return err; 749727ab04eSArnaldo Carvalho de Melo } 75035b9d88eSArnaldo Carvalho de Melo 75135b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 7526ef73ec4SNamhyung Kim struct perf_target *target, 75355e162eaSNamhyung Kim const char *argv[], bool pipe_output, 75455e162eaSNamhyung Kim bool want_signal) 75535b9d88eSArnaldo Carvalho de Melo { 75635b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 75735b9d88eSArnaldo Carvalho de Melo char bf; 75835b9d88eSArnaldo Carvalho de Melo 75935b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 76035b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 76135b9d88eSArnaldo Carvalho de Melo return -1; 76235b9d88eSArnaldo Carvalho de Melo } 76335b9d88eSArnaldo Carvalho de Melo 76435b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 76535b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 76635b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 76735b9d88eSArnaldo Carvalho de Melo } 76835b9d88eSArnaldo Carvalho de Melo 76935b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 77035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 77135b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 77235b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 77335b9d88eSArnaldo Carvalho de Melo } 77435b9d88eSArnaldo Carvalho de Melo 77535b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 776119fa3c9SNamhyung Kim if (pipe_output) 77735b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 77835b9d88eSArnaldo Carvalho de Melo 7790817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 7800817df08SDavid Ahern 78135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 78235b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 78335b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 78435b9d88eSArnaldo Carvalho de Melo 78535b9d88eSArnaldo Carvalho de Melo /* 78635b9d88eSArnaldo Carvalho de Melo * Do a dummy execvp to get the PLT entry resolved, 78735b9d88eSArnaldo Carvalho de Melo * so we avoid the resolver overhead on the real 78835b9d88eSArnaldo Carvalho de Melo * execvp call. 78935b9d88eSArnaldo Carvalho de Melo */ 79035b9d88eSArnaldo Carvalho de Melo execvp("", (char **)argv); 79135b9d88eSArnaldo Carvalho de Melo 79235b9d88eSArnaldo Carvalho de Melo /* 79335b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 79435b9d88eSArnaldo Carvalho de Melo */ 79535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 79635b9d88eSArnaldo Carvalho de Melo 79735b9d88eSArnaldo Carvalho de Melo /* 79835b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 79935b9d88eSArnaldo Carvalho de Melo */ 80035b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 80135b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 80235b9d88eSArnaldo Carvalho de Melo 80335b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 80435b9d88eSArnaldo Carvalho de Melo 80535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 80655e162eaSNamhyung Kim if (want_signal) 80735b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 80835b9d88eSArnaldo Carvalho de Melo exit(-1); 80935b9d88eSArnaldo Carvalho de Melo } 81035b9d88eSArnaldo Carvalho de Melo 8116ef73ec4SNamhyung Kim if (perf_target__none(target)) 81235b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 81335b9d88eSArnaldo Carvalho de Melo 81435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 81535b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 81635b9d88eSArnaldo Carvalho de Melo /* 81735b9d88eSArnaldo Carvalho de Melo * wait for child to settle 81835b9d88eSArnaldo Carvalho de Melo */ 81935b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 82035b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 82135b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 82235b9d88eSArnaldo Carvalho de Melo } 82335b9d88eSArnaldo Carvalho de Melo 824bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 82535b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 82635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 82735b9d88eSArnaldo Carvalho de Melo return 0; 82835b9d88eSArnaldo Carvalho de Melo 82935b9d88eSArnaldo Carvalho de Melo out_close_pipes: 83035b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 83135b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 83235b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 83335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 83435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 83535b9d88eSArnaldo Carvalho de Melo return -1; 83635b9d88eSArnaldo Carvalho de Melo } 83735b9d88eSArnaldo Carvalho de Melo 83835b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 83935b9d88eSArnaldo Carvalho de Melo { 84035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 841b3824404SDavid Ahern char bf = 0; 842bcf3145fSNamhyung Kim int ret; 84335b9d88eSArnaldo Carvalho de Melo /* 84435b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 84535b9d88eSArnaldo Carvalho de Melo */ 846bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 847bcf3145fSNamhyung Kim if (ret < 0) 848bcf3145fSNamhyung Kim perror("enable to write to pipe"); 849bcf3145fSNamhyung Kim 850bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 851bcf3145fSNamhyung Kim return ret; 85235b9d88eSArnaldo Carvalho de Melo } 85335b9d88eSArnaldo Carvalho de Melo 85435b9d88eSArnaldo Carvalho de Melo return 0; 85535b9d88eSArnaldo Carvalho de Melo } 856cb0b29e0SArnaldo Carvalho de Melo 857a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 8580807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 859cb0b29e0SArnaldo Carvalho de Melo { 8600c21f736SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evlist__first(evlist); 8610807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 862cb0b29e0SArnaldo Carvalho de Melo } 86378f067b3SArnaldo Carvalho de Melo 86478f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 86578f067b3SArnaldo Carvalho de Melo { 86678f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 86778f067b3SArnaldo Carvalho de Melo size_t printed = 0; 86878f067b3SArnaldo Carvalho de Melo 86978f067b3SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 87078f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 87178f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 87278f067b3SArnaldo Carvalho de Melo } 87378f067b3SArnaldo Carvalho de Melo 87478f067b3SArnaldo Carvalho de Melo return printed + fprintf(fp, "\n");; 87578f067b3SArnaldo Carvalho de Melo } 876