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 520f82ebc4SArnaldo Carvalho de Melo void perf_evlist__config_attrs(struct perf_evlist *evlist, 530f82ebc4SArnaldo Carvalho de Melo struct perf_record_opts *opts) 540f82ebc4SArnaldo Carvalho de Melo { 555090c6aeSNamhyung Kim struct perf_evsel *evsel, *first; 560f82ebc4SArnaldo Carvalho de Melo 570f82ebc4SArnaldo Carvalho de Melo if (evlist->cpus->map[0] < 0) 580f82ebc4SArnaldo Carvalho de Melo opts->no_inherit = true; 590f82ebc4SArnaldo Carvalho de Melo 605090c6aeSNamhyung Kim first = list_entry(evlist->entries.next, struct perf_evsel, node); 615090c6aeSNamhyung Kim 620f82ebc4SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 635090c6aeSNamhyung Kim perf_evsel__config(evsel, opts, first); 640f82ebc4SArnaldo Carvalho de Melo 650f82ebc4SArnaldo Carvalho de Melo if (evlist->nr_entries > 1) 660f82ebc4SArnaldo Carvalho de Melo evsel->attr.sample_type |= PERF_SAMPLE_ID; 670f82ebc4SArnaldo Carvalho de Melo } 680f82ebc4SArnaldo Carvalho de Melo } 690f82ebc4SArnaldo Carvalho de Melo 70361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 71361c99a6SArnaldo Carvalho de Melo { 72361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 73361c99a6SArnaldo Carvalho de Melo 74361c99a6SArnaldo Carvalho de Melo list_for_each_entry_safe(pos, n, &evlist->entries, node) { 75361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 76361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 77361c99a6SArnaldo Carvalho de Melo } 78361c99a6SArnaldo Carvalho de Melo 79361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 80361c99a6SArnaldo Carvalho de Melo } 81361c99a6SArnaldo Carvalho de Melo 82ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 83ef1d1af2SArnaldo Carvalho de Melo { 84ef1d1af2SArnaldo Carvalho de Melo free(evlist->mmap); 85ef1d1af2SArnaldo Carvalho de Melo free(evlist->pollfd); 86ef1d1af2SArnaldo Carvalho de Melo evlist->mmap = NULL; 87ef1d1af2SArnaldo Carvalho de Melo evlist->pollfd = NULL; 88ef1d1af2SArnaldo Carvalho de Melo } 89ef1d1af2SArnaldo Carvalho de Melo 90361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 91361c99a6SArnaldo Carvalho de Melo { 92361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 93ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 94361c99a6SArnaldo Carvalho de Melo free(evlist); 95361c99a6SArnaldo Carvalho de Melo } 96361c99a6SArnaldo Carvalho de Melo 97361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 98361c99a6SArnaldo Carvalho de Melo { 99361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 100361c99a6SArnaldo Carvalho de Melo ++evlist->nr_entries; 101361c99a6SArnaldo Carvalho de Melo } 102361c99a6SArnaldo Carvalho de Melo 1030529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 10450d08e47SArnaldo Carvalho de Melo struct list_head *list, 10550d08e47SArnaldo Carvalho de Melo int nr_entries) 10650d08e47SArnaldo Carvalho de Melo { 10750d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 10850d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 10950d08e47SArnaldo Carvalho de Melo } 11050d08e47SArnaldo Carvalho de Melo 1116a4bb04cSJiri Olsa void perf_evlist__group(struct perf_evlist *evlist) 1126a4bb04cSJiri Olsa { 1136a4bb04cSJiri Olsa if (evlist->nr_entries) 1146a4bb04cSJiri Olsa parse_events__set_leader(&evlist->entries); 1156a4bb04cSJiri Olsa } 1166a4bb04cSJiri Olsa 117361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 118361c99a6SArnaldo Carvalho de Melo { 119361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 120361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 121361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 122361c99a6SArnaldo Carvalho de Melo }; 1231aed2671SJoerg Roedel struct perf_evsel *evsel; 124361c99a6SArnaldo Carvalho de Melo 1251aed2671SJoerg Roedel event_attr_init(&attr); 1261aed2671SJoerg Roedel 1271aed2671SJoerg Roedel evsel = perf_evsel__new(&attr, 0); 128361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 129cc2d86b0SStephane Eranian goto error; 130cc2d86b0SStephane Eranian 131cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 132cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 133cc2d86b0SStephane Eranian if (!evsel->name) 134cc2d86b0SStephane Eranian goto error_free; 135361c99a6SArnaldo Carvalho de Melo 136361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 137361c99a6SArnaldo Carvalho de Melo return 0; 138cc2d86b0SStephane Eranian error_free: 139cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 140cc2d86b0SStephane Eranian error: 141cc2d86b0SStephane Eranian return -ENOMEM; 142361c99a6SArnaldo Carvalho de Melo } 1435c581041SArnaldo Carvalho de Melo 14450d08e47SArnaldo Carvalho de Melo int perf_evlist__add_attrs(struct perf_evlist *evlist, 14550d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 14650d08e47SArnaldo Carvalho de Melo { 14750d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 14850d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 14950d08e47SArnaldo Carvalho de Melo size_t i; 15050d08e47SArnaldo Carvalho de Melo 15150d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 15250d08e47SArnaldo Carvalho de Melo evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); 15350d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 15450d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 15550d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 15650d08e47SArnaldo Carvalho de Melo } 15750d08e47SArnaldo Carvalho de Melo 15850d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 15950d08e47SArnaldo Carvalho de Melo 16050d08e47SArnaldo Carvalho de Melo return 0; 16150d08e47SArnaldo Carvalho de Melo 16250d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 16350d08e47SArnaldo Carvalho de Melo list_for_each_entry_safe(evsel, n, &head, node) 16450d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 16550d08e47SArnaldo Carvalho de Melo return -1; 16650d08e47SArnaldo Carvalho de Melo } 16750d08e47SArnaldo Carvalho de Melo 16879695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 16979695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 17079695e1bSArnaldo Carvalho de Melo { 17179695e1bSArnaldo Carvalho de Melo size_t i; 17279695e1bSArnaldo Carvalho de Melo 17379695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 17479695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 17579695e1bSArnaldo Carvalho de Melo 17679695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 17779695e1bSArnaldo Carvalho de Melo } 17879695e1bSArnaldo Carvalho de Melo 179a8c9ae18SArnaldo Carvalho de Melo static int trace_event__id(const char *evname) 180a8c9ae18SArnaldo Carvalho de Melo { 181a8c9ae18SArnaldo Carvalho de Melo char *filename, *colon; 182a8c9ae18SArnaldo Carvalho de Melo int err = -1, fd; 183a8c9ae18SArnaldo Carvalho de Melo 184a8c9ae18SArnaldo Carvalho de Melo if (asprintf(&filename, "%s/%s/id", tracing_events_path, evname) < 0) 185a8c9ae18SArnaldo Carvalho de Melo return -1; 186a8c9ae18SArnaldo Carvalho de Melo 187a8c9ae18SArnaldo Carvalho de Melo colon = strrchr(filename, ':'); 188a8c9ae18SArnaldo Carvalho de Melo if (colon != NULL) 189a8c9ae18SArnaldo Carvalho de Melo *colon = '/'; 190a8c9ae18SArnaldo Carvalho de Melo 191a8c9ae18SArnaldo Carvalho de Melo fd = open(filename, O_RDONLY); 192a8c9ae18SArnaldo Carvalho de Melo if (fd >= 0) { 193a8c9ae18SArnaldo Carvalho de Melo char id[16]; 194a8c9ae18SArnaldo Carvalho de Melo if (read(fd, id, sizeof(id)) > 0) 195a8c9ae18SArnaldo Carvalho de Melo err = atoi(id); 196a8c9ae18SArnaldo Carvalho de Melo close(fd); 197a8c9ae18SArnaldo Carvalho de Melo } 198a8c9ae18SArnaldo Carvalho de Melo 199a8c9ae18SArnaldo Carvalho de Melo free(filename); 200a8c9ae18SArnaldo Carvalho de Melo return err; 201a8c9ae18SArnaldo Carvalho de Melo } 202a8c9ae18SArnaldo Carvalho de Melo 203a8c9ae18SArnaldo Carvalho de Melo int perf_evlist__add_tracepoints(struct perf_evlist *evlist, 204a8c9ae18SArnaldo Carvalho de Melo const char *tracepoints[], 205a8c9ae18SArnaldo Carvalho de Melo size_t nr_tracepoints) 206a8c9ae18SArnaldo Carvalho de Melo { 207a8c9ae18SArnaldo Carvalho de Melo int err; 208a8c9ae18SArnaldo Carvalho de Melo size_t i; 209a8c9ae18SArnaldo Carvalho de Melo struct perf_event_attr *attrs = zalloc(nr_tracepoints * sizeof(*attrs)); 210a8c9ae18SArnaldo Carvalho de Melo 211a8c9ae18SArnaldo Carvalho de Melo if (attrs == NULL) 212a8c9ae18SArnaldo Carvalho de Melo return -1; 213a8c9ae18SArnaldo Carvalho de Melo 214a8c9ae18SArnaldo Carvalho de Melo for (i = 0; i < nr_tracepoints; i++) { 215a8c9ae18SArnaldo Carvalho de Melo err = trace_event__id(tracepoints[i]); 216a8c9ae18SArnaldo Carvalho de Melo 217a8c9ae18SArnaldo Carvalho de Melo if (err < 0) 218a8c9ae18SArnaldo Carvalho de Melo goto out_free_attrs; 219a8c9ae18SArnaldo Carvalho de Melo 220a8c9ae18SArnaldo Carvalho de Melo attrs[i].type = PERF_TYPE_TRACEPOINT; 221a8c9ae18SArnaldo Carvalho de Melo attrs[i].config = err; 222a8c9ae18SArnaldo Carvalho de Melo attrs[i].sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 2230983cc0dSFrederic Weisbecker PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD); 224a8c9ae18SArnaldo Carvalho de Melo attrs[i].sample_period = 1; 225a8c9ae18SArnaldo Carvalho de Melo } 226a8c9ae18SArnaldo Carvalho de Melo 227a8c9ae18SArnaldo Carvalho de Melo err = perf_evlist__add_attrs(evlist, attrs, nr_tracepoints); 228a8c9ae18SArnaldo Carvalho de Melo out_free_attrs: 229a8c9ae18SArnaldo Carvalho de Melo free(attrs); 230a8c9ae18SArnaldo Carvalho de Melo return err; 231a8c9ae18SArnaldo Carvalho de Melo } 232a8c9ae18SArnaldo Carvalho de Melo 233da378962SArnaldo Carvalho de Melo struct perf_evsel * 234ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 235ee29be62SArnaldo Carvalho de Melo { 236ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 237ee29be62SArnaldo Carvalho de Melo 238ee29be62SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 239ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 240ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 241ee29be62SArnaldo Carvalho de Melo return evsel; 242ee29be62SArnaldo Carvalho de Melo } 243ee29be62SArnaldo Carvalho de Melo 244ee29be62SArnaldo Carvalho de Melo return NULL; 245ee29be62SArnaldo Carvalho de Melo } 246ee29be62SArnaldo Carvalho de Melo 247ee29be62SArnaldo Carvalho de Melo int perf_evlist__set_tracepoints_handlers(struct perf_evlist *evlist, 248ee29be62SArnaldo Carvalho de Melo const struct perf_evsel_str_handler *assocs, 249ee29be62SArnaldo Carvalho de Melo size_t nr_assocs) 250ee29be62SArnaldo Carvalho de Melo { 251ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 252ee29be62SArnaldo Carvalho de Melo int err; 253ee29be62SArnaldo Carvalho de Melo size_t i; 254ee29be62SArnaldo Carvalho de Melo 255ee29be62SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 256ee29be62SArnaldo Carvalho de Melo err = trace_event__id(assocs[i].name); 257ee29be62SArnaldo Carvalho de Melo if (err < 0) 258ee29be62SArnaldo Carvalho de Melo goto out; 259ee29be62SArnaldo Carvalho de Melo 260ee29be62SArnaldo Carvalho de Melo evsel = perf_evlist__find_tracepoint_by_id(evlist, err); 261ee29be62SArnaldo Carvalho de Melo if (evsel == NULL) 262ee29be62SArnaldo Carvalho de Melo continue; 263ee29be62SArnaldo Carvalho de Melo 264ee29be62SArnaldo Carvalho de Melo err = -EEXIST; 265ee29be62SArnaldo Carvalho de Melo if (evsel->handler.func != NULL) 266ee29be62SArnaldo Carvalho de Melo goto out; 267ee29be62SArnaldo Carvalho de Melo evsel->handler.func = assocs[i].handler; 268ee29be62SArnaldo Carvalho de Melo } 269ee29be62SArnaldo Carvalho de Melo 270ee29be62SArnaldo Carvalho de Melo err = 0; 271ee29be62SArnaldo Carvalho de Melo out: 272ee29be62SArnaldo Carvalho de Melo return err; 273ee29be62SArnaldo Carvalho de Melo } 274ee29be62SArnaldo Carvalho de Melo 2754152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2764152ab37SArnaldo Carvalho de Melo { 2774152ab37SArnaldo Carvalho de Melo int cpu, thread; 2784152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 2794152ab37SArnaldo Carvalho de Melo 2804152ab37SArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 2814152ab37SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 2824152ab37SArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) 28355da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 28455da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2854152ab37SArnaldo Carvalho de Melo } 2864152ab37SArnaldo Carvalho de Melo } 2874152ab37SArnaldo Carvalho de Melo } 2884152ab37SArnaldo Carvalho de Melo 289764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 290764e16a3SDavid Ahern { 291764e16a3SDavid Ahern int cpu, thread; 292764e16a3SDavid Ahern struct perf_evsel *pos; 293764e16a3SDavid Ahern 294764e16a3SDavid Ahern for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 295764e16a3SDavid Ahern list_for_each_entry(pos, &evlist->entries, node) { 296764e16a3SDavid Ahern for (thread = 0; thread < evlist->threads->nr; thread++) 29755da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 29855da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 299764e16a3SDavid Ahern } 300764e16a3SDavid Ahern } 301764e16a3SDavid Ahern } 302764e16a3SDavid Ahern 303806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 3045c581041SArnaldo Carvalho de Melo { 3057e2ed097SArnaldo Carvalho de Melo int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries; 3065c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 3075c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 3085c581041SArnaldo Carvalho de Melo } 30970082dd9SArnaldo Carvalho de Melo 31070082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 31170082dd9SArnaldo Carvalho de Melo { 31270082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 31370082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 31470082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 31570082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 31670082dd9SArnaldo Carvalho de Melo } 31770db7533SArnaldo Carvalho de Melo 318a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 319a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 3203d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 3213d3b5e95SArnaldo Carvalho de Melo { 3223d3b5e95SArnaldo Carvalho de Melo int hash; 3233d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 3243d3b5e95SArnaldo Carvalho de Melo 3253d3b5e95SArnaldo Carvalho de Melo sid->id = id; 3263d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 3273d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 3283d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 3293d3b5e95SArnaldo Carvalho de Melo } 3303d3b5e95SArnaldo Carvalho de Melo 331a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 332a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 333a91e5431SArnaldo Carvalho de Melo { 334a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 335a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 336a91e5431SArnaldo Carvalho de Melo } 337a91e5431SArnaldo Carvalho de Melo 338a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 3393d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 340f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 341f8a95309SArnaldo Carvalho de Melo { 342f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 3433d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 344f8a95309SArnaldo Carvalho de Melo 345f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 346f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 347f8a95309SArnaldo Carvalho de Melo return -1; 348f8a95309SArnaldo Carvalho de Melo 349f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 350f8a95309SArnaldo Carvalho de Melo ++id_idx; 351f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 352f8a95309SArnaldo Carvalho de Melo ++id_idx; 353f8a95309SArnaldo Carvalho de Melo 354a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 355f8a95309SArnaldo Carvalho de Melo return 0; 356f8a95309SArnaldo Carvalho de Melo } 357f8a95309SArnaldo Carvalho de Melo 35870db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 35970db7533SArnaldo Carvalho de Melo { 36070db7533SArnaldo Carvalho de Melo struct hlist_head *head; 36170db7533SArnaldo Carvalho de Melo struct hlist_node *pos; 36270db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 36370db7533SArnaldo Carvalho de Melo int hash; 36470db7533SArnaldo Carvalho de Melo 36570db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 36670db7533SArnaldo Carvalho de Melo return list_entry(evlist->entries.next, struct perf_evsel, node); 36770db7533SArnaldo Carvalho de Melo 36870db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 36970db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 37070db7533SArnaldo Carvalho de Melo 37170db7533SArnaldo Carvalho de Melo hlist_for_each_entry(sid, pos, head, node) 37270db7533SArnaldo Carvalho de Melo if (sid->id == id) 37370db7533SArnaldo Carvalho de Melo return sid->evsel; 37430e68bccSNamhyung Kim 37530e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 37630e68bccSNamhyung Kim return list_entry(evlist->entries.next, struct perf_evsel, node); 37730e68bccSNamhyung Kim 37870db7533SArnaldo Carvalho de Melo return NULL; 37970db7533SArnaldo Carvalho de Melo } 38004391debSArnaldo Carvalho de Melo 381aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 38204391debSArnaldo Carvalho de Melo { 38304391debSArnaldo Carvalho de Melo /* XXX Move this to perf.c, making it generally available */ 38404391debSArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 385aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 38604391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 38704391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 38804391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3898115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 39004391debSArnaldo Carvalho de Melo 3917bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 39204391debSArnaldo Carvalho de Melo /* 39304391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 39404391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 39504391debSArnaldo Carvalho de Melo * 39604391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 39704391debSArnaldo Carvalho de Melo * 39804391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 39904391debSArnaldo Carvalho de Melo */ 4007bb41152SArnaldo Carvalho de Melo int diff = head - old; 40104391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 40204391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 40304391debSArnaldo Carvalho de Melo 40404391debSArnaldo Carvalho de Melo /* 40504391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 40604391debSArnaldo Carvalho de Melo */ 40704391debSArnaldo Carvalho de Melo old = head; 40804391debSArnaldo Carvalho de Melo } 4097bb41152SArnaldo Carvalho de Melo } 41004391debSArnaldo Carvalho de Melo 41104391debSArnaldo Carvalho de Melo if (old != head) { 41204391debSArnaldo Carvalho de Melo size_t size; 41304391debSArnaldo Carvalho de Melo 4148115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 41504391debSArnaldo Carvalho de Melo size = event->header.size; 41604391debSArnaldo Carvalho de Melo 41704391debSArnaldo Carvalho de Melo /* 41804391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 41904391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 42004391debSArnaldo Carvalho de Melo */ 42104391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 42204391debSArnaldo Carvalho de Melo unsigned int offset = old; 42304391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 42404391debSArnaldo Carvalho de Melo void *dst = &evlist->event_copy; 42504391debSArnaldo Carvalho de Melo 42604391debSArnaldo Carvalho de Melo do { 42704391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 42804391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 42904391debSArnaldo Carvalho de Melo offset += cpy; 43004391debSArnaldo Carvalho de Melo dst += cpy; 43104391debSArnaldo Carvalho de Melo len -= cpy; 43204391debSArnaldo Carvalho de Melo } while (len); 43304391debSArnaldo Carvalho de Melo 43404391debSArnaldo Carvalho de Melo event = &evlist->event_copy; 43504391debSArnaldo Carvalho de Melo } 43604391debSArnaldo Carvalho de Melo 43704391debSArnaldo Carvalho de Melo old += size; 43804391debSArnaldo Carvalho de Melo } 43904391debSArnaldo Carvalho de Melo 44004391debSArnaldo Carvalho de Melo md->prev = old; 4417bb41152SArnaldo Carvalho de Melo 4427bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 4437bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 4447bb41152SArnaldo Carvalho de Melo 44504391debSArnaldo Carvalho de Melo return event; 44604391debSArnaldo Carvalho de Melo } 447f8a95309SArnaldo Carvalho de Melo 4487e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 449f8a95309SArnaldo Carvalho de Melo { 450aece948fSArnaldo Carvalho de Melo int i; 451f8a95309SArnaldo Carvalho de Melo 452aece948fSArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 453aece948fSArnaldo Carvalho de Melo if (evlist->mmap[i].base != NULL) { 454aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[i].base, evlist->mmap_len); 455aece948fSArnaldo Carvalho de Melo evlist->mmap[i].base = NULL; 456f8a95309SArnaldo Carvalho de Melo } 457f8a95309SArnaldo Carvalho de Melo } 458aece948fSArnaldo Carvalho de Melo 459aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 460aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 461f8a95309SArnaldo Carvalho de Melo } 462f8a95309SArnaldo Carvalho de Melo 463806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 464f8a95309SArnaldo Carvalho de Melo { 465aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->cpus->nr; 466aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[0] == -1) 467aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->threads->nr; 468aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 469f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 470f8a95309SArnaldo Carvalho de Melo } 471f8a95309SArnaldo Carvalho de Melo 472bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 473aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 474f8a95309SArnaldo Carvalho de Melo { 475aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 476aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 477aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 478f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 479301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 480301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 481f8a95309SArnaldo Carvalho de Melo return -1; 482301b195dSNelson Elhage } 483f8a95309SArnaldo Carvalho de Melo 484f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 485f8a95309SArnaldo Carvalho de Melo return 0; 486f8a95309SArnaldo Carvalho de Melo } 487f8a95309SArnaldo Carvalho de Melo 488aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 489aece948fSArnaldo Carvalho de Melo { 490aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 491aece948fSArnaldo Carvalho de Melo int cpu, thread; 492aece948fSArnaldo Carvalho de Melo 493aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 494aece948fSArnaldo Carvalho de Melo int output = -1; 495aece948fSArnaldo Carvalho de Melo 496aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 497aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 498aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 499aece948fSArnaldo Carvalho de Melo 500aece948fSArnaldo Carvalho de Melo if (output == -1) { 501aece948fSArnaldo Carvalho de Melo output = fd; 502bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 503aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 504aece948fSArnaldo Carvalho de Melo goto out_unmap; 505aece948fSArnaldo Carvalho de Melo } else { 506aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 507aece948fSArnaldo Carvalho de Melo goto out_unmap; 508aece948fSArnaldo Carvalho de Melo } 509aece948fSArnaldo Carvalho de Melo 510aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 511aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 512aece948fSArnaldo Carvalho de Melo goto out_unmap; 513aece948fSArnaldo Carvalho de Melo } 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: 520aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 521aece948fSArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 522aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 523aece948fSArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 524aece948fSArnaldo Carvalho de Melo } 525aece948fSArnaldo Carvalho de Melo } 526aece948fSArnaldo Carvalho de Melo return -1; 527aece948fSArnaldo Carvalho de Melo } 528aece948fSArnaldo Carvalho de Melo 529aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 530aece948fSArnaldo Carvalho de Melo { 531aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 532aece948fSArnaldo Carvalho de Melo int thread; 533aece948fSArnaldo Carvalho de Melo 534aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 535aece948fSArnaldo Carvalho de Melo int output = -1; 536aece948fSArnaldo Carvalho de Melo 537aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 538aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 539aece948fSArnaldo Carvalho de Melo 540aece948fSArnaldo Carvalho de Melo if (output == -1) { 541aece948fSArnaldo Carvalho de Melo output = fd; 542bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 543aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 544aece948fSArnaldo Carvalho de Melo goto out_unmap; 545aece948fSArnaldo Carvalho de Melo } else { 546aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 547aece948fSArnaldo Carvalho de Melo goto out_unmap; 548aece948fSArnaldo Carvalho de Melo } 549aece948fSArnaldo Carvalho de Melo 550aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 551aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 552aece948fSArnaldo Carvalho de Melo goto out_unmap; 553aece948fSArnaldo Carvalho de Melo } 554aece948fSArnaldo Carvalho de Melo } 555aece948fSArnaldo Carvalho de Melo 556aece948fSArnaldo Carvalho de Melo return 0; 557aece948fSArnaldo Carvalho de Melo 558aece948fSArnaldo Carvalho de Melo out_unmap: 559aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 560aece948fSArnaldo Carvalho de Melo if (evlist->mmap[thread].base != NULL) { 561aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[thread].base, evlist->mmap_len); 562aece948fSArnaldo Carvalho de Melo evlist->mmap[thread].base = NULL; 563aece948fSArnaldo Carvalho de Melo } 564aece948fSArnaldo Carvalho de Melo } 565aece948fSArnaldo Carvalho de Melo return -1; 566aece948fSArnaldo Carvalho de Melo } 567aece948fSArnaldo Carvalho de Melo 568f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 569f8a95309SArnaldo Carvalho de Melo * 570f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 571f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 572f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 573f8a95309SArnaldo Carvalho de Melo * 574f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 575f8a95309SArnaldo Carvalho de Melo * 576f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 577f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 578f8a95309SArnaldo Carvalho de Melo * 579f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5807e2ed097SArnaldo Carvalho de Melo * 5817e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 582f8a95309SArnaldo Carvalho de Melo */ 58350a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 58450a682ceSArnaldo Carvalho de Melo bool overwrite) 585f8a95309SArnaldo Carvalho de Melo { 586f8a95309SArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 587aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5887e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5897e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 59050a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 59150a682ceSArnaldo Carvalho de Melo 59250a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 59350a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 59450a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 59541d0d933SNelson Elhage else if (!is_power_of_2(pages)) 59641d0d933SNelson Elhage return -EINVAL; 59750a682ceSArnaldo Carvalho de Melo 59850a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 599f8a95309SArnaldo Carvalho de Melo 6007e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 601f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 602f8a95309SArnaldo Carvalho de Melo 6037e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 604f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 605f8a95309SArnaldo Carvalho de Melo 606f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 607f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 608f8a95309SArnaldo Carvalho de Melo 609f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 610f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 611a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 612f8a95309SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) 613f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 614f8a95309SArnaldo Carvalho de Melo } 615f8a95309SArnaldo Carvalho de Melo 616aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[0] == -1) 617aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 618f8a95309SArnaldo Carvalho de Melo 619aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 620f8a95309SArnaldo Carvalho de Melo } 6217e2ed097SArnaldo Carvalho de Melo 622b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 623b809ac10SNamhyung Kim struct perf_target *target) 6247e2ed097SArnaldo Carvalho de Melo { 625b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 626b809ac10SNamhyung Kim target->uid); 6277e2ed097SArnaldo Carvalho de Melo 6287e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 6297e2ed097SArnaldo Carvalho de Melo return -1; 6307e2ed097SArnaldo Carvalho de Melo 631879d77d0SNamhyung Kim if (perf_target__has_task(target)) 632d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 633d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 634d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 635879d77d0SNamhyung Kim else 636879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 6377e2ed097SArnaldo Carvalho de Melo 6387e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 6397e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 6407e2ed097SArnaldo Carvalho de Melo 6417e2ed097SArnaldo Carvalho de Melo return 0; 6427e2ed097SArnaldo Carvalho de Melo 6437e2ed097SArnaldo Carvalho de Melo out_delete_threads: 6447e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6457e2ed097SArnaldo Carvalho de Melo return -1; 6467e2ed097SArnaldo Carvalho de Melo } 6477e2ed097SArnaldo Carvalho de Melo 6487e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 6497e2ed097SArnaldo Carvalho de Melo { 6507e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 6517e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6527e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 6537e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 6547e2ed097SArnaldo Carvalho de Melo } 6550a102479SFrederic Weisbecker 6560a102479SFrederic Weisbecker int perf_evlist__set_filters(struct perf_evlist *evlist) 6570a102479SFrederic Weisbecker { 6580a102479SFrederic Weisbecker const struct thread_map *threads = evlist->threads; 6590a102479SFrederic Weisbecker const struct cpu_map *cpus = evlist->cpus; 6600a102479SFrederic Weisbecker struct perf_evsel *evsel; 6610a102479SFrederic Weisbecker char *filter; 6620a102479SFrederic Weisbecker int thread; 6630a102479SFrederic Weisbecker int cpu; 6640a102479SFrederic Weisbecker int err; 6650a102479SFrederic Weisbecker int fd; 6660a102479SFrederic Weisbecker 6670a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 6680a102479SFrederic Weisbecker filter = evsel->filter; 6690a102479SFrederic Weisbecker if (!filter) 6700a102479SFrederic Weisbecker continue; 6710a102479SFrederic Weisbecker for (cpu = 0; cpu < cpus->nr; cpu++) { 6720a102479SFrederic Weisbecker for (thread = 0; thread < threads->nr; thread++) { 6730a102479SFrederic Weisbecker fd = FD(evsel, cpu, thread); 6740a102479SFrederic Weisbecker err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); 6750a102479SFrederic Weisbecker if (err) 6760a102479SFrederic Weisbecker return err; 6770a102479SFrederic Weisbecker } 6780a102479SFrederic Weisbecker } 6790a102479SFrederic Weisbecker } 6800a102479SFrederic Weisbecker 6810a102479SFrederic Weisbecker return 0; 6820a102479SFrederic Weisbecker } 68374429964SFrederic Weisbecker 684c2a70653SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist) 68574429964SFrederic Weisbecker { 686c2a70653SArnaldo Carvalho de Melo struct perf_evsel *pos, *first; 68774429964SFrederic Weisbecker 688c2a70653SArnaldo Carvalho de Melo pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); 689c2a70653SArnaldo Carvalho de Melo 690c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 691c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 692c2a70653SArnaldo Carvalho de Melo return false; 69374429964SFrederic Weisbecker } 69474429964SFrederic Weisbecker 695c2a70653SArnaldo Carvalho de Melo return true; 696c2a70653SArnaldo Carvalho de Melo } 697c2a70653SArnaldo Carvalho de Melo 698c2a70653SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(const struct perf_evlist *evlist) 699c2a70653SArnaldo Carvalho de Melo { 700c2a70653SArnaldo Carvalho de Melo struct perf_evsel *first; 701c2a70653SArnaldo Carvalho de Melo 702c2a70653SArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 703c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 704c2a70653SArnaldo Carvalho de Melo } 705c2a70653SArnaldo Carvalho de Melo 70681e36bffSArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(const struct perf_evlist *evlist) 70781e36bffSArnaldo Carvalho de Melo { 70881e36bffSArnaldo Carvalho de Melo struct perf_evsel *first; 70981e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 71081e36bffSArnaldo Carvalho de Melo u64 sample_type; 71181e36bffSArnaldo Carvalho de Melo u16 size = 0; 71281e36bffSArnaldo Carvalho de Melo 71381e36bffSArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 71481e36bffSArnaldo Carvalho de Melo 71581e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 71681e36bffSArnaldo Carvalho de Melo goto out; 71781e36bffSArnaldo Carvalho de Melo 71881e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 71981e36bffSArnaldo Carvalho de Melo 72081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 72181e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 72281e36bffSArnaldo Carvalho de Melo 72381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 72481e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 72581e36bffSArnaldo Carvalho de Melo 72681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 72781e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 72881e36bffSArnaldo Carvalho de Melo 72981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 73081e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 73181e36bffSArnaldo Carvalho de Melo 73281e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 73381e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 73481e36bffSArnaldo Carvalho de Melo out: 73581e36bffSArnaldo Carvalho de Melo return size; 73681e36bffSArnaldo Carvalho de Melo } 73781e36bffSArnaldo Carvalho de Melo 738c2a70653SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist) 739c2a70653SArnaldo Carvalho de Melo { 740c2a70653SArnaldo Carvalho de Melo struct perf_evsel *pos, *first; 741c2a70653SArnaldo Carvalho de Melo 742c2a70653SArnaldo Carvalho de Melo pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); 743c2a70653SArnaldo Carvalho de Melo 744c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 745c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 746c2a70653SArnaldo Carvalho de Melo return false; 747c2a70653SArnaldo Carvalho de Melo } 748c2a70653SArnaldo Carvalho de Melo 749c2a70653SArnaldo Carvalho de Melo return true; 75074429964SFrederic Weisbecker } 75174429964SFrederic Weisbecker 75274429964SFrederic Weisbecker bool perf_evlist__sample_id_all(const struct perf_evlist *evlist) 75374429964SFrederic Weisbecker { 754c2a70653SArnaldo Carvalho de Melo struct perf_evsel *first; 75574429964SFrederic Weisbecker 756c2a70653SArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 757c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 75874429964SFrederic Weisbecker } 75981cce8deSArnaldo Carvalho de Melo 76081cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 76181cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 76281cce8deSArnaldo Carvalho de Melo { 76381cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 76481cce8deSArnaldo Carvalho de Melo } 765727ab04eSArnaldo Carvalho de Melo 7666a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 767727ab04eSArnaldo Carvalho de Melo { 7686a4bb04cSJiri Olsa struct perf_evsel *evsel; 769727ab04eSArnaldo Carvalho de Melo int err, ncpus, nthreads; 770727ab04eSArnaldo Carvalho de Melo 771727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 7726a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 773727ab04eSArnaldo Carvalho de Melo if (err < 0) 774727ab04eSArnaldo Carvalho de Melo goto out_err; 775727ab04eSArnaldo Carvalho de Melo } 776727ab04eSArnaldo Carvalho de Melo 777727ab04eSArnaldo Carvalho de Melo return 0; 778727ab04eSArnaldo Carvalho de Melo out_err: 779727ab04eSArnaldo Carvalho de Melo ncpus = evlist->cpus ? evlist->cpus->nr : 1; 780727ab04eSArnaldo Carvalho de Melo nthreads = evlist->threads ? evlist->threads->nr : 1; 781727ab04eSArnaldo Carvalho de Melo 782727ab04eSArnaldo Carvalho de Melo list_for_each_entry_reverse(evsel, &evlist->entries, node) 783727ab04eSArnaldo Carvalho de Melo perf_evsel__close(evsel, ncpus, nthreads); 784727ab04eSArnaldo Carvalho de Melo 78541c21a68SNamhyung Kim errno = -err; 786727ab04eSArnaldo Carvalho de Melo return err; 787727ab04eSArnaldo Carvalho de Melo } 78835b9d88eSArnaldo Carvalho de Melo 78935b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 79035b9d88eSArnaldo Carvalho de Melo struct perf_record_opts *opts, 79135b9d88eSArnaldo Carvalho de Melo const char *argv[]) 79235b9d88eSArnaldo Carvalho de Melo { 79335b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 79435b9d88eSArnaldo Carvalho de Melo char bf; 79535b9d88eSArnaldo Carvalho de Melo 79635b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 79735b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 79835b9d88eSArnaldo Carvalho de Melo return -1; 79935b9d88eSArnaldo Carvalho de Melo } 80035b9d88eSArnaldo Carvalho de Melo 80135b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 80235b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 80335b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 80435b9d88eSArnaldo Carvalho de Melo } 80535b9d88eSArnaldo Carvalho de Melo 80635b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 80735b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 80835b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 80935b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 81035b9d88eSArnaldo Carvalho de Melo } 81135b9d88eSArnaldo Carvalho de Melo 81235b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 81335b9d88eSArnaldo Carvalho de Melo if (opts->pipe_output) 81435b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 81535b9d88eSArnaldo Carvalho de Melo 81635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 81735b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 81835b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 81935b9d88eSArnaldo Carvalho de Melo 82035b9d88eSArnaldo Carvalho de Melo /* 82135b9d88eSArnaldo Carvalho de Melo * Do a dummy execvp to get the PLT entry resolved, 82235b9d88eSArnaldo Carvalho de Melo * so we avoid the resolver overhead on the real 82335b9d88eSArnaldo Carvalho de Melo * execvp call. 82435b9d88eSArnaldo Carvalho de Melo */ 82535b9d88eSArnaldo Carvalho de Melo execvp("", (char **)argv); 82635b9d88eSArnaldo Carvalho de Melo 82735b9d88eSArnaldo Carvalho de Melo /* 82835b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 82935b9d88eSArnaldo Carvalho de Melo */ 83035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 83135b9d88eSArnaldo Carvalho de Melo 83235b9d88eSArnaldo Carvalho de Melo /* 83335b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 83435b9d88eSArnaldo Carvalho de Melo */ 83535b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 83635b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 83735b9d88eSArnaldo Carvalho de Melo 83835b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 83935b9d88eSArnaldo Carvalho de Melo 84035b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 84135b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 84235b9d88eSArnaldo Carvalho de Melo exit(-1); 84335b9d88eSArnaldo Carvalho de Melo } 84435b9d88eSArnaldo Carvalho de Melo 845d67356e7SNamhyung Kim if (perf_target__none(&opts->target)) 84635b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 84735b9d88eSArnaldo Carvalho de Melo 84835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 84935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 85035b9d88eSArnaldo Carvalho de Melo /* 85135b9d88eSArnaldo Carvalho de Melo * wait for child to settle 85235b9d88eSArnaldo Carvalho de Melo */ 85335b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 85435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 85535b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 85635b9d88eSArnaldo Carvalho de Melo } 85735b9d88eSArnaldo Carvalho de Melo 85835b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 85935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 86035b9d88eSArnaldo Carvalho de Melo return 0; 86135b9d88eSArnaldo Carvalho de Melo 86235b9d88eSArnaldo Carvalho de Melo out_close_pipes: 86335b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 86435b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 86535b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 86635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 86735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 86835b9d88eSArnaldo Carvalho de Melo return -1; 86935b9d88eSArnaldo Carvalho de Melo } 87035b9d88eSArnaldo Carvalho de Melo 87135b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 87235b9d88eSArnaldo Carvalho de Melo { 87335b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 87435b9d88eSArnaldo Carvalho de Melo /* 87535b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 87635b9d88eSArnaldo Carvalho de Melo */ 87735b9d88eSArnaldo Carvalho de Melo return close(evlist->workload.cork_fd); 87835b9d88eSArnaldo Carvalho de Melo } 87935b9d88eSArnaldo Carvalho de Melo 88035b9d88eSArnaldo Carvalho de Melo return 0; 88135b9d88eSArnaldo Carvalho de Melo } 882cb0b29e0SArnaldo Carvalho de Melo 883a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 884cb0b29e0SArnaldo Carvalho de Melo struct perf_sample *sample, bool swapped) 885cb0b29e0SArnaldo Carvalho de Melo { 886cb0b29e0SArnaldo Carvalho de Melo struct perf_evsel *e = list_entry(evlist->entries.next, struct perf_evsel, node); 887a3f698feSArnaldo Carvalho de Melo return perf_evsel__parse_sample(e, event, sample, swapped); 888cb0b29e0SArnaldo Carvalho de Melo } 889