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" 10a8c9ae18SArnaldo Carvalho de Melo #include "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 417e2ed097SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new(struct cpu_map *cpus, 427e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 43ef1d1af2SArnaldo Carvalho de Melo { 44ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 45ef1d1af2SArnaldo Carvalho de Melo 46ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 477e2ed097SArnaldo Carvalho de Melo perf_evlist__init(evlist, cpus, threads); 48ef1d1af2SArnaldo Carvalho de Melo 49361c99a6SArnaldo Carvalho de Melo return evlist; 50361c99a6SArnaldo Carvalho de Melo } 51361c99a6SArnaldo Carvalho de Melo 52f77a9518SArnaldo Carvalho de Melo void perf_evlist__config(struct perf_evlist *evlist, 530f82ebc4SArnaldo Carvalho de Melo struct perf_record_opts *opts) 540f82ebc4SArnaldo Carvalho de Melo { 55cac21425SJiri Olsa struct perf_evsel *evsel; 56f77a9518SArnaldo Carvalho de Melo /* 57f77a9518SArnaldo Carvalho de Melo * Set the evsel leader links before we configure attributes, 58f77a9518SArnaldo Carvalho de Melo * since some might depend on this info. 59f77a9518SArnaldo Carvalho de Melo */ 60f77a9518SArnaldo Carvalho de Melo if (opts->group) 61f77a9518SArnaldo Carvalho de Melo perf_evlist__set_leader(evlist); 620f82ebc4SArnaldo Carvalho de Melo 630f82ebc4SArnaldo Carvalho de Melo if (evlist->cpus->map[0] < 0) 640f82ebc4SArnaldo Carvalho de Melo opts->no_inherit = true; 650f82ebc4SArnaldo Carvalho de Melo 660f82ebc4SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 67cac21425SJiri Olsa perf_evsel__config(evsel, opts); 680f82ebc4SArnaldo Carvalho de Melo 690f82ebc4SArnaldo Carvalho de Melo if (evlist->nr_entries > 1) 703a5afaecSArnaldo Carvalho de Melo perf_evsel__set_sample_id(evsel); 710f82ebc4SArnaldo Carvalho de Melo } 720f82ebc4SArnaldo Carvalho de Melo } 730f82ebc4SArnaldo Carvalho de Melo 74361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 75361c99a6SArnaldo Carvalho de Melo { 76361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 77361c99a6SArnaldo Carvalho de Melo 78361c99a6SArnaldo Carvalho de Melo list_for_each_entry_safe(pos, n, &evlist->entries, node) { 79361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 80361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 81361c99a6SArnaldo Carvalho de Melo } 82361c99a6SArnaldo Carvalho de Melo 83361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 84361c99a6SArnaldo Carvalho de Melo } 85361c99a6SArnaldo Carvalho de Melo 86ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 87ef1d1af2SArnaldo Carvalho de Melo { 88ef1d1af2SArnaldo Carvalho de Melo free(evlist->mmap); 89ef1d1af2SArnaldo Carvalho de Melo free(evlist->pollfd); 90ef1d1af2SArnaldo Carvalho de Melo evlist->mmap = NULL; 91ef1d1af2SArnaldo Carvalho de Melo evlist->pollfd = NULL; 92ef1d1af2SArnaldo Carvalho de Melo } 93ef1d1af2SArnaldo Carvalho de Melo 94361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 95361c99a6SArnaldo Carvalho de Melo { 96361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 97ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 98361c99a6SArnaldo Carvalho de Melo free(evlist); 99361c99a6SArnaldo Carvalho de Melo } 100361c99a6SArnaldo Carvalho de Melo 101361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 102361c99a6SArnaldo Carvalho de Melo { 103361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 104361c99a6SArnaldo Carvalho de Melo ++evlist->nr_entries; 105361c99a6SArnaldo Carvalho de Melo } 106361c99a6SArnaldo Carvalho de Melo 1070529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 10850d08e47SArnaldo Carvalho de Melo struct list_head *list, 10950d08e47SArnaldo Carvalho de Melo int nr_entries) 11050d08e47SArnaldo Carvalho de Melo { 11150d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 11250d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 11350d08e47SArnaldo Carvalho de Melo } 11450d08e47SArnaldo Carvalho de Melo 11563dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 11663dab225SArnaldo Carvalho de Melo { 11763dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 11863dab225SArnaldo Carvalho de Melo 11963dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 12097f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 12197f63e4aSNamhyung Kim 12297f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 12363dab225SArnaldo Carvalho de Melo 12463dab225SArnaldo Carvalho de Melo list_for_each_entry(evsel, list, node) { 12563dab225SArnaldo Carvalho de Melo evsel->leader = leader; 12663dab225SArnaldo Carvalho de Melo } 12763dab225SArnaldo Carvalho de Melo } 12863dab225SArnaldo Carvalho de Melo 12963dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1306a4bb04cSJiri Olsa { 13197f63e4aSNamhyung Kim if (evlist->nr_entries) { 13297f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 13363dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1346a4bb04cSJiri Olsa } 13597f63e4aSNamhyung Kim } 1366a4bb04cSJiri Olsa 137361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 138361c99a6SArnaldo Carvalho de Melo { 139361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 140361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 141361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 142361c99a6SArnaldo Carvalho de Melo }; 1431aed2671SJoerg Roedel struct perf_evsel *evsel; 144361c99a6SArnaldo Carvalho de Melo 1451aed2671SJoerg Roedel event_attr_init(&attr); 1461aed2671SJoerg Roedel 1471aed2671SJoerg Roedel evsel = perf_evsel__new(&attr, 0); 148361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 149cc2d86b0SStephane Eranian goto error; 150cc2d86b0SStephane Eranian 151cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 152cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 153cc2d86b0SStephane Eranian if (!evsel->name) 154cc2d86b0SStephane Eranian goto error_free; 155361c99a6SArnaldo Carvalho de Melo 156361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 157361c99a6SArnaldo Carvalho de Melo return 0; 158cc2d86b0SStephane Eranian error_free: 159cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 160cc2d86b0SStephane Eranian error: 161cc2d86b0SStephane Eranian return -ENOMEM; 162361c99a6SArnaldo Carvalho de Melo } 1635c581041SArnaldo Carvalho de Melo 164e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 16550d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 16650d08e47SArnaldo Carvalho de Melo { 16750d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 16850d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 16950d08e47SArnaldo Carvalho de Melo size_t i; 17050d08e47SArnaldo Carvalho de Melo 17150d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 17250d08e47SArnaldo Carvalho de Melo evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); 17350d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 17450d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 17550d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 17650d08e47SArnaldo Carvalho de Melo } 17750d08e47SArnaldo Carvalho de Melo 17850d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 17950d08e47SArnaldo Carvalho de Melo 18050d08e47SArnaldo Carvalho de Melo return 0; 18150d08e47SArnaldo Carvalho de Melo 18250d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 18350d08e47SArnaldo Carvalho de Melo list_for_each_entry_safe(evsel, n, &head, node) 18450d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 18550d08e47SArnaldo Carvalho de Melo return -1; 18650d08e47SArnaldo Carvalho de Melo } 18750d08e47SArnaldo Carvalho de Melo 18879695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 18979695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 19079695e1bSArnaldo Carvalho de Melo { 19179695e1bSArnaldo Carvalho de Melo size_t i; 19279695e1bSArnaldo Carvalho de Melo 19379695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 19479695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 19579695e1bSArnaldo Carvalho de Melo 19679695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 19779695e1bSArnaldo Carvalho de Melo } 19879695e1bSArnaldo Carvalho de Melo 199da378962SArnaldo Carvalho de Melo struct perf_evsel * 200ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 201ee29be62SArnaldo Carvalho de Melo { 202ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 203ee29be62SArnaldo Carvalho de Melo 204ee29be62SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 205ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 206ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 207ee29be62SArnaldo Carvalho de Melo return evsel; 208ee29be62SArnaldo Carvalho de Melo } 209ee29be62SArnaldo Carvalho de Melo 210ee29be62SArnaldo Carvalho de Melo return NULL; 211ee29be62SArnaldo Carvalho de Melo } 212ee29be62SArnaldo Carvalho de Melo 21339876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 21439876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 21539876e7dSArnaldo Carvalho de Melo { 21639876e7dSArnaldo Carvalho de Melo struct perf_evsel *evsel; 21739876e7dSArnaldo Carvalho de Melo 21839876e7dSArnaldo Carvalho de Melo evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); 21939876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 22039876e7dSArnaldo Carvalho de Melo return -1; 22139876e7dSArnaldo Carvalho de Melo 22239876e7dSArnaldo Carvalho de Melo evsel->handler.func = handler; 22339876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 22439876e7dSArnaldo Carvalho de Melo return 0; 22539876e7dSArnaldo Carvalho de Melo } 22639876e7dSArnaldo Carvalho de Melo 2274152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2284152ab37SArnaldo Carvalho de Melo { 2294152ab37SArnaldo Carvalho de Melo int cpu, thread; 2304152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 2314152ab37SArnaldo Carvalho de Melo 2324152ab37SArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 2334152ab37SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 234823254edSNamhyung Kim if (!perf_evsel__is_group_leader(pos)) 2353fe4430dSJiri Olsa continue; 2364152ab37SArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) 23755da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 23855da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2394152ab37SArnaldo Carvalho de Melo } 2404152ab37SArnaldo Carvalho de Melo } 2414152ab37SArnaldo Carvalho de Melo } 2424152ab37SArnaldo Carvalho de Melo 243764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 244764e16a3SDavid Ahern { 245764e16a3SDavid Ahern int cpu, thread; 246764e16a3SDavid Ahern struct perf_evsel *pos; 247764e16a3SDavid Ahern 248a14bb7a6SArnaldo Carvalho de Melo for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) { 249764e16a3SDavid Ahern list_for_each_entry(pos, &evlist->entries, node) { 250823254edSNamhyung Kim if (!perf_evsel__is_group_leader(pos)) 2513fe4430dSJiri Olsa continue; 252764e16a3SDavid Ahern for (thread = 0; thread < evlist->threads->nr; thread++) 25355da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 25455da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 255764e16a3SDavid Ahern } 256764e16a3SDavid Ahern } 257764e16a3SDavid Ahern } 258764e16a3SDavid Ahern 259806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 2605c581041SArnaldo Carvalho de Melo { 261a14bb7a6SArnaldo Carvalho de Melo int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries; 2625c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 2635c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 2645c581041SArnaldo Carvalho de Melo } 26570082dd9SArnaldo Carvalho de Melo 26670082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 26770082dd9SArnaldo Carvalho de Melo { 26870082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 26970082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 27070082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 27170082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 27270082dd9SArnaldo Carvalho de Melo } 27370db7533SArnaldo Carvalho de Melo 274a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 275a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 2763d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 2773d3b5e95SArnaldo Carvalho de Melo { 2783d3b5e95SArnaldo Carvalho de Melo int hash; 2793d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 2803d3b5e95SArnaldo Carvalho de Melo 2813d3b5e95SArnaldo Carvalho de Melo sid->id = id; 2823d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 2833d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 2843d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 2853d3b5e95SArnaldo Carvalho de Melo } 2863d3b5e95SArnaldo Carvalho de Melo 287a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 288a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 289a91e5431SArnaldo Carvalho de Melo { 290a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 291a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 292a91e5431SArnaldo Carvalho de Melo } 293a91e5431SArnaldo Carvalho de Melo 294a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 2953d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 296f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 297f8a95309SArnaldo Carvalho de Melo { 298f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 2993d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 300f8a95309SArnaldo Carvalho de Melo 301f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 302f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 303f8a95309SArnaldo Carvalho de Melo return -1; 304f8a95309SArnaldo Carvalho de Melo 305f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 306f8a95309SArnaldo Carvalho de Melo ++id_idx; 307f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 308f8a95309SArnaldo Carvalho de Melo ++id_idx; 309f8a95309SArnaldo Carvalho de Melo 310a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 311f8a95309SArnaldo Carvalho de Melo return 0; 312f8a95309SArnaldo Carvalho de Melo } 313f8a95309SArnaldo Carvalho de Melo 31470db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 31570db7533SArnaldo Carvalho de Melo { 31670db7533SArnaldo Carvalho de Melo struct hlist_head *head; 31770db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 31870db7533SArnaldo Carvalho de Melo int hash; 31970db7533SArnaldo Carvalho de Melo 32070db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 3210c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 32270db7533SArnaldo Carvalho de Melo 32370db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 32470db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 32570db7533SArnaldo Carvalho de Melo 326b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 32770db7533SArnaldo Carvalho de Melo if (sid->id == id) 32870db7533SArnaldo Carvalho de Melo return sid->evsel; 32930e68bccSNamhyung Kim 33030e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 3310c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 33230e68bccSNamhyung Kim 33370db7533SArnaldo Carvalho de Melo return NULL; 33470db7533SArnaldo Carvalho de Melo } 33504391debSArnaldo Carvalho de Melo 336aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 33704391debSArnaldo Carvalho de Melo { 338aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 33904391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 34004391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 34104391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3428115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 34304391debSArnaldo Carvalho de Melo 3447bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 34504391debSArnaldo Carvalho de Melo /* 34604391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 34704391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 34804391debSArnaldo Carvalho de Melo * 34904391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 35004391debSArnaldo Carvalho de Melo * 35104391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 35204391debSArnaldo Carvalho de Melo */ 3537bb41152SArnaldo Carvalho de Melo int diff = head - old; 35404391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 35504391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 35604391debSArnaldo Carvalho de Melo 35704391debSArnaldo Carvalho de Melo /* 35804391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 35904391debSArnaldo Carvalho de Melo */ 36004391debSArnaldo Carvalho de Melo old = head; 36104391debSArnaldo Carvalho de Melo } 3627bb41152SArnaldo Carvalho de Melo } 36304391debSArnaldo Carvalho de Melo 36404391debSArnaldo Carvalho de Melo if (old != head) { 36504391debSArnaldo Carvalho de Melo size_t size; 36604391debSArnaldo Carvalho de Melo 3678115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 36804391debSArnaldo Carvalho de Melo size = event->header.size; 36904391debSArnaldo Carvalho de Melo 37004391debSArnaldo Carvalho de Melo /* 37104391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 37204391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 37304391debSArnaldo Carvalho de Melo */ 37404391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 37504391debSArnaldo Carvalho de Melo unsigned int offset = old; 37604391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 3770479b8b9SDavid Ahern void *dst = &md->event_copy; 37804391debSArnaldo Carvalho de Melo 37904391debSArnaldo Carvalho de Melo do { 38004391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 38104391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 38204391debSArnaldo Carvalho de Melo offset += cpy; 38304391debSArnaldo Carvalho de Melo dst += cpy; 38404391debSArnaldo Carvalho de Melo len -= cpy; 38504391debSArnaldo Carvalho de Melo } while (len); 38604391debSArnaldo Carvalho de Melo 3870479b8b9SDavid Ahern event = &md->event_copy; 38804391debSArnaldo Carvalho de Melo } 38904391debSArnaldo Carvalho de Melo 39004391debSArnaldo Carvalho de Melo old += size; 39104391debSArnaldo Carvalho de Melo } 39204391debSArnaldo Carvalho de Melo 39304391debSArnaldo Carvalho de Melo md->prev = old; 3947bb41152SArnaldo Carvalho de Melo 3957bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 3967bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 3977bb41152SArnaldo Carvalho de Melo 39804391debSArnaldo Carvalho de Melo return event; 39904391debSArnaldo Carvalho de Melo } 400f8a95309SArnaldo Carvalho de Melo 4017e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 402f8a95309SArnaldo Carvalho de Melo { 403aece948fSArnaldo Carvalho de Melo int i; 404f8a95309SArnaldo Carvalho de Melo 405aece948fSArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 406aece948fSArnaldo Carvalho de Melo if (evlist->mmap[i].base != NULL) { 407aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[i].base, evlist->mmap_len); 408aece948fSArnaldo Carvalho de Melo evlist->mmap[i].base = NULL; 409f8a95309SArnaldo Carvalho de Melo } 410f8a95309SArnaldo Carvalho de Melo } 411aece948fSArnaldo Carvalho de Melo 412aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 413aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 414f8a95309SArnaldo Carvalho de Melo } 415f8a95309SArnaldo Carvalho de Melo 416806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 417f8a95309SArnaldo Carvalho de Melo { 418a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 419a14bb7a6SArnaldo Carvalho de Melo if (cpu_map__all(evlist->cpus)) 420aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->threads->nr; 421aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 422f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 423f8a95309SArnaldo Carvalho de Melo } 424f8a95309SArnaldo Carvalho de Melo 425bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 426aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 427f8a95309SArnaldo Carvalho de Melo { 428aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 429aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 430aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 431f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 432301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 433301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 434f8a95309SArnaldo Carvalho de Melo return -1; 435301b195dSNelson Elhage } 436f8a95309SArnaldo Carvalho de Melo 437f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 438f8a95309SArnaldo Carvalho de Melo return 0; 439f8a95309SArnaldo Carvalho de Melo } 440f8a95309SArnaldo Carvalho de Melo 441aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 442aece948fSArnaldo Carvalho de Melo { 443aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 444aece948fSArnaldo Carvalho de Melo int cpu, thread; 445aece948fSArnaldo Carvalho de Melo 446aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 447aece948fSArnaldo Carvalho de Melo int output = -1; 448aece948fSArnaldo Carvalho de Melo 449aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 450aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 451aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 452aece948fSArnaldo Carvalho de Melo 453aece948fSArnaldo Carvalho de Melo if (output == -1) { 454aece948fSArnaldo Carvalho de Melo output = fd; 455bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 456aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 457aece948fSArnaldo Carvalho de Melo goto out_unmap; 458aece948fSArnaldo Carvalho de Melo } else { 459aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 460aece948fSArnaldo Carvalho de Melo goto out_unmap; 461aece948fSArnaldo Carvalho de Melo } 462aece948fSArnaldo Carvalho de Melo 463aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 464aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 465aece948fSArnaldo Carvalho de Melo goto out_unmap; 466aece948fSArnaldo Carvalho de Melo } 467aece948fSArnaldo Carvalho de Melo } 468aece948fSArnaldo Carvalho de Melo } 469aece948fSArnaldo Carvalho de Melo 470aece948fSArnaldo Carvalho de Melo return 0; 471aece948fSArnaldo Carvalho de Melo 472aece948fSArnaldo Carvalho de Melo out_unmap: 473aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 474aece948fSArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 475aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 476aece948fSArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 477aece948fSArnaldo Carvalho de Melo } 478aece948fSArnaldo Carvalho de Melo } 479aece948fSArnaldo Carvalho de Melo return -1; 480aece948fSArnaldo Carvalho de Melo } 481aece948fSArnaldo Carvalho de Melo 482aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 483aece948fSArnaldo Carvalho de Melo { 484aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 485aece948fSArnaldo Carvalho de Melo int thread; 486aece948fSArnaldo Carvalho de Melo 487aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 488aece948fSArnaldo Carvalho de Melo int output = -1; 489aece948fSArnaldo Carvalho de Melo 490aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 491aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 492aece948fSArnaldo Carvalho de Melo 493aece948fSArnaldo Carvalho de Melo if (output == -1) { 494aece948fSArnaldo Carvalho de Melo output = fd; 495bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 496aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 497aece948fSArnaldo Carvalho de Melo goto out_unmap; 498aece948fSArnaldo Carvalho de Melo } else { 499aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 500aece948fSArnaldo Carvalho de Melo goto out_unmap; 501aece948fSArnaldo Carvalho de Melo } 502aece948fSArnaldo Carvalho de Melo 503aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 504aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 505aece948fSArnaldo Carvalho de Melo goto out_unmap; 506aece948fSArnaldo Carvalho de Melo } 507aece948fSArnaldo Carvalho de Melo } 508aece948fSArnaldo Carvalho de Melo 509aece948fSArnaldo Carvalho de Melo return 0; 510aece948fSArnaldo Carvalho de Melo 511aece948fSArnaldo Carvalho de Melo out_unmap: 512aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 513aece948fSArnaldo Carvalho de Melo if (evlist->mmap[thread].base != NULL) { 514aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[thread].base, evlist->mmap_len); 515aece948fSArnaldo Carvalho de Melo evlist->mmap[thread].base = NULL; 516aece948fSArnaldo Carvalho de Melo } 517aece948fSArnaldo Carvalho de Melo } 518aece948fSArnaldo Carvalho de Melo return -1; 519aece948fSArnaldo Carvalho de Melo } 520aece948fSArnaldo Carvalho de Melo 521f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 522f8a95309SArnaldo Carvalho de Melo * 523f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 524f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 525f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 526f8a95309SArnaldo Carvalho de Melo * 527f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 528f8a95309SArnaldo Carvalho de Melo * 529f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 530f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 531f8a95309SArnaldo Carvalho de Melo * 532f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5337e2ed097SArnaldo Carvalho de Melo * 5347e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 535f8a95309SArnaldo Carvalho de Melo */ 53650a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 53750a682ceSArnaldo Carvalho de Melo bool overwrite) 538f8a95309SArnaldo Carvalho de Melo { 539aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5407e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5417e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 54250a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 54350a682ceSArnaldo Carvalho de Melo 54450a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 54550a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 54650a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 54741d0d933SNelson Elhage else if (!is_power_of_2(pages)) 54841d0d933SNelson Elhage return -EINVAL; 54950a682ceSArnaldo Carvalho de Melo 55050a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 551f8a95309SArnaldo Carvalho de Melo 5527e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 553f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 554f8a95309SArnaldo Carvalho de Melo 5557e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 556f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 557f8a95309SArnaldo Carvalho de Melo 558f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 559f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 560f8a95309SArnaldo Carvalho de Melo 561f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 562f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 563a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 564a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 565f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 566f8a95309SArnaldo Carvalho de Melo } 567f8a95309SArnaldo Carvalho de Melo 568a14bb7a6SArnaldo Carvalho de Melo if (cpu_map__all(cpus)) 569aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 570f8a95309SArnaldo Carvalho de Melo 571aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 572f8a95309SArnaldo Carvalho de Melo } 5737e2ed097SArnaldo Carvalho de Melo 574b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 575b809ac10SNamhyung Kim struct perf_target *target) 5767e2ed097SArnaldo Carvalho de Melo { 577b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 578b809ac10SNamhyung Kim target->uid); 5797e2ed097SArnaldo Carvalho de Melo 5807e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 5817e2ed097SArnaldo Carvalho de Melo return -1; 5827e2ed097SArnaldo Carvalho de Melo 583879d77d0SNamhyung Kim if (perf_target__has_task(target)) 584d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 585d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 586d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 587879d77d0SNamhyung Kim else 588879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 5897e2ed097SArnaldo Carvalho de Melo 5907e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 5917e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 5927e2ed097SArnaldo Carvalho de Melo 5937e2ed097SArnaldo Carvalho de Melo return 0; 5947e2ed097SArnaldo Carvalho de Melo 5957e2ed097SArnaldo Carvalho de Melo out_delete_threads: 5967e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 5977e2ed097SArnaldo Carvalho de Melo return -1; 5987e2ed097SArnaldo Carvalho de Melo } 5997e2ed097SArnaldo Carvalho de Melo 6007e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 6017e2ed097SArnaldo Carvalho de Melo { 6027e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 6037e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6047e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 6057e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 6067e2ed097SArnaldo Carvalho de Melo } 6070a102479SFrederic Weisbecker 6081491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 6090a102479SFrederic Weisbecker { 6100a102479SFrederic Weisbecker struct perf_evsel *evsel; 611745cefc5SArnaldo Carvalho de Melo int err = 0; 612745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 613745cefc5SArnaldo Carvalho de Melo nthreads = evlist->threads->nr; 6140a102479SFrederic Weisbecker 6150a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 616745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 6170a102479SFrederic Weisbecker continue; 618745cefc5SArnaldo Carvalho de Melo 619745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 6200a102479SFrederic Weisbecker if (err) 621745cefc5SArnaldo Carvalho de Melo break; 6220a102479SFrederic Weisbecker } 6230a102479SFrederic Weisbecker 624745cefc5SArnaldo Carvalho de Melo return err; 625745cefc5SArnaldo Carvalho de Melo } 626745cefc5SArnaldo Carvalho de Melo 627745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 628745cefc5SArnaldo Carvalho de Melo { 629745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 630745cefc5SArnaldo Carvalho de Melo int err = 0; 631745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 632745cefc5SArnaldo Carvalho de Melo nthreads = evlist->threads->nr; 633745cefc5SArnaldo Carvalho de Melo 634745cefc5SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 635745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 636745cefc5SArnaldo Carvalho de Melo if (err) 637745cefc5SArnaldo Carvalho de Melo break; 638745cefc5SArnaldo Carvalho de Melo } 639745cefc5SArnaldo Carvalho de Melo 640745cefc5SArnaldo Carvalho de Melo return err; 6410a102479SFrederic Weisbecker } 64274429964SFrederic Weisbecker 6430c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 64474429964SFrederic Weisbecker { 6450c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 646c2a70653SArnaldo Carvalho de Melo 647c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 648c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 649c2a70653SArnaldo Carvalho de Melo return false; 65074429964SFrederic Weisbecker } 65174429964SFrederic Weisbecker 652c2a70653SArnaldo Carvalho de Melo return true; 653c2a70653SArnaldo Carvalho de Melo } 654c2a70653SArnaldo Carvalho de Melo 6550c21f736SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(struct perf_evlist *evlist) 656c2a70653SArnaldo Carvalho de Melo { 6570c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 658c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 659c2a70653SArnaldo Carvalho de Melo } 660c2a70653SArnaldo Carvalho de Melo 6610c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 66281e36bffSArnaldo Carvalho de Melo { 6630c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 66481e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 66581e36bffSArnaldo Carvalho de Melo u64 sample_type; 66681e36bffSArnaldo Carvalho de Melo u16 size = 0; 66781e36bffSArnaldo Carvalho de Melo 66881e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 66981e36bffSArnaldo Carvalho de Melo goto out; 67081e36bffSArnaldo Carvalho de Melo 67181e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 67281e36bffSArnaldo Carvalho de Melo 67381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 67481e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 67581e36bffSArnaldo Carvalho de Melo 67681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 67781e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 67881e36bffSArnaldo Carvalho de Melo 67981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 68081e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 68181e36bffSArnaldo Carvalho de Melo 68281e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 68381e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 68481e36bffSArnaldo Carvalho de Melo 68581e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 68681e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 68781e36bffSArnaldo Carvalho de Melo out: 68881e36bffSArnaldo Carvalho de Melo return size; 68981e36bffSArnaldo Carvalho de Melo } 69081e36bffSArnaldo Carvalho de Melo 6910c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 692c2a70653SArnaldo Carvalho de Melo { 6930c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 694c2a70653SArnaldo Carvalho de Melo 695c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 696c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 697c2a70653SArnaldo Carvalho de Melo return false; 698c2a70653SArnaldo Carvalho de Melo } 699c2a70653SArnaldo Carvalho de Melo 700c2a70653SArnaldo Carvalho de Melo return true; 70174429964SFrederic Weisbecker } 70274429964SFrederic Weisbecker 7030c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 70474429964SFrederic Weisbecker { 7050c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 706c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 70774429964SFrederic Weisbecker } 70881cce8deSArnaldo Carvalho de Melo 70981cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 71081cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 71181cce8deSArnaldo Carvalho de Melo { 71281cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 71381cce8deSArnaldo Carvalho de Melo } 714727ab04eSArnaldo Carvalho de Melo 7156a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 716727ab04eSArnaldo Carvalho de Melo { 7176a4bb04cSJiri Olsa struct perf_evsel *evsel; 718727ab04eSArnaldo Carvalho de Melo int err, ncpus, nthreads; 719727ab04eSArnaldo Carvalho de Melo 720727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 7216a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 722727ab04eSArnaldo Carvalho de Melo if (err < 0) 723727ab04eSArnaldo Carvalho de Melo goto out_err; 724727ab04eSArnaldo Carvalho de Melo } 725727ab04eSArnaldo Carvalho de Melo 726727ab04eSArnaldo Carvalho de Melo return 0; 727727ab04eSArnaldo Carvalho de Melo out_err: 728727ab04eSArnaldo Carvalho de Melo ncpus = evlist->cpus ? evlist->cpus->nr : 1; 729727ab04eSArnaldo Carvalho de Melo nthreads = evlist->threads ? evlist->threads->nr : 1; 730727ab04eSArnaldo Carvalho de Melo 731727ab04eSArnaldo Carvalho de Melo list_for_each_entry_reverse(evsel, &evlist->entries, node) 732727ab04eSArnaldo Carvalho de Melo perf_evsel__close(evsel, ncpus, nthreads); 733727ab04eSArnaldo Carvalho de Melo 73441c21a68SNamhyung Kim errno = -err; 735727ab04eSArnaldo Carvalho de Melo return err; 736727ab04eSArnaldo Carvalho de Melo } 73735b9d88eSArnaldo Carvalho de Melo 73835b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 73935b9d88eSArnaldo Carvalho de Melo struct perf_record_opts *opts, 74035b9d88eSArnaldo Carvalho de Melo const char *argv[]) 74135b9d88eSArnaldo Carvalho de Melo { 74235b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 74335b9d88eSArnaldo Carvalho de Melo char bf; 74435b9d88eSArnaldo Carvalho de Melo 74535b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 74635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 74735b9d88eSArnaldo Carvalho de Melo return -1; 74835b9d88eSArnaldo Carvalho de Melo } 74935b9d88eSArnaldo Carvalho de Melo 75035b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 75135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 75235b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 75335b9d88eSArnaldo Carvalho de Melo } 75435b9d88eSArnaldo Carvalho de Melo 75535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 75635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 75735b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 75835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 75935b9d88eSArnaldo Carvalho de Melo } 76035b9d88eSArnaldo Carvalho de Melo 76135b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 76235b9d88eSArnaldo Carvalho de Melo if (opts->pipe_output) 76335b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 76435b9d88eSArnaldo Carvalho de Melo 76535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 76635b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 76735b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 76835b9d88eSArnaldo Carvalho de Melo 76935b9d88eSArnaldo Carvalho de Melo /* 77035b9d88eSArnaldo Carvalho de Melo * Do a dummy execvp to get the PLT entry resolved, 77135b9d88eSArnaldo Carvalho de Melo * so we avoid the resolver overhead on the real 77235b9d88eSArnaldo Carvalho de Melo * execvp call. 77335b9d88eSArnaldo Carvalho de Melo */ 77435b9d88eSArnaldo Carvalho de Melo execvp("", (char **)argv); 77535b9d88eSArnaldo Carvalho de Melo 77635b9d88eSArnaldo Carvalho de Melo /* 77735b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 77835b9d88eSArnaldo Carvalho de Melo */ 77935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 78035b9d88eSArnaldo Carvalho de Melo 78135b9d88eSArnaldo Carvalho de Melo /* 78235b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 78335b9d88eSArnaldo Carvalho de Melo */ 78435b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 78535b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 78635b9d88eSArnaldo Carvalho de Melo 78735b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 78835b9d88eSArnaldo Carvalho de Melo 78935b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 79035b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 79135b9d88eSArnaldo Carvalho de Melo exit(-1); 79235b9d88eSArnaldo Carvalho de Melo } 79335b9d88eSArnaldo Carvalho de Melo 794d67356e7SNamhyung Kim if (perf_target__none(&opts->target)) 79535b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 79635b9d88eSArnaldo Carvalho de Melo 79735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 79835b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 79935b9d88eSArnaldo Carvalho de Melo /* 80035b9d88eSArnaldo Carvalho de Melo * wait for child to settle 80135b9d88eSArnaldo Carvalho de Melo */ 80235b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 80335b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 80435b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 80535b9d88eSArnaldo Carvalho de Melo } 80635b9d88eSArnaldo Carvalho de Melo 80735b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 80835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 80935b9d88eSArnaldo Carvalho de Melo return 0; 81035b9d88eSArnaldo Carvalho de Melo 81135b9d88eSArnaldo Carvalho de Melo out_close_pipes: 81235b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 81335b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 81435b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 81535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 81635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 81735b9d88eSArnaldo Carvalho de Melo return -1; 81835b9d88eSArnaldo Carvalho de Melo } 81935b9d88eSArnaldo Carvalho de Melo 82035b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 82135b9d88eSArnaldo Carvalho de Melo { 82235b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 82335b9d88eSArnaldo Carvalho de Melo /* 82435b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 82535b9d88eSArnaldo Carvalho de Melo */ 82635b9d88eSArnaldo Carvalho de Melo return close(evlist->workload.cork_fd); 82735b9d88eSArnaldo Carvalho de Melo } 82835b9d88eSArnaldo Carvalho de Melo 82935b9d88eSArnaldo Carvalho de Melo return 0; 83035b9d88eSArnaldo Carvalho de Melo } 831cb0b29e0SArnaldo Carvalho de Melo 832a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 8330807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 834cb0b29e0SArnaldo Carvalho de Melo { 8350c21f736SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evlist__first(evlist); 8360807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 837cb0b29e0SArnaldo Carvalho de Melo } 83878f067b3SArnaldo Carvalho de Melo 83978f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 84078f067b3SArnaldo Carvalho de Melo { 84178f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 84278f067b3SArnaldo Carvalho de Melo size_t printed = 0; 84378f067b3SArnaldo Carvalho de Melo 84478f067b3SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 84578f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 84678f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 84778f067b3SArnaldo Carvalho de Melo } 84878f067b3SArnaldo Carvalho de Melo 84978f067b3SArnaldo Carvalho de Melo return printed + fprintf(fp, "\n");; 85078f067b3SArnaldo Carvalho de Melo } 851