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 111361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 112361c99a6SArnaldo Carvalho de Melo { 113361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 114361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 115361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 116361c99a6SArnaldo Carvalho de Melo }; 1171aed2671SJoerg Roedel struct perf_evsel *evsel; 118361c99a6SArnaldo Carvalho de Melo 1191aed2671SJoerg Roedel event_attr_init(&attr); 1201aed2671SJoerg Roedel 1211aed2671SJoerg Roedel evsel = perf_evsel__new(&attr, 0); 122361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 123cc2d86b0SStephane Eranian goto error; 124cc2d86b0SStephane Eranian 125cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 126cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 127cc2d86b0SStephane Eranian if (!evsel->name) 128cc2d86b0SStephane Eranian goto error_free; 129361c99a6SArnaldo Carvalho de Melo 130361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 131361c99a6SArnaldo Carvalho de Melo return 0; 132cc2d86b0SStephane Eranian error_free: 133cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 134cc2d86b0SStephane Eranian error: 135cc2d86b0SStephane Eranian return -ENOMEM; 136361c99a6SArnaldo Carvalho de Melo } 1375c581041SArnaldo Carvalho de Melo 13850d08e47SArnaldo Carvalho de Melo int perf_evlist__add_attrs(struct perf_evlist *evlist, 13950d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 14050d08e47SArnaldo Carvalho de Melo { 14150d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 14250d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 14350d08e47SArnaldo Carvalho de Melo size_t i; 14450d08e47SArnaldo Carvalho de Melo 14550d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 14650d08e47SArnaldo Carvalho de Melo evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); 14750d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 14850d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 14950d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 15050d08e47SArnaldo Carvalho de Melo } 15150d08e47SArnaldo Carvalho de Melo 15250d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 15350d08e47SArnaldo Carvalho de Melo 15450d08e47SArnaldo Carvalho de Melo return 0; 15550d08e47SArnaldo Carvalho de Melo 15650d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 15750d08e47SArnaldo Carvalho de Melo list_for_each_entry_safe(evsel, n, &head, node) 15850d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 15950d08e47SArnaldo Carvalho de Melo return -1; 16050d08e47SArnaldo Carvalho de Melo } 16150d08e47SArnaldo Carvalho de Melo 16279695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 16379695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 16479695e1bSArnaldo Carvalho de Melo { 16579695e1bSArnaldo Carvalho de Melo size_t i; 16679695e1bSArnaldo Carvalho de Melo 16779695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 16879695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 16979695e1bSArnaldo Carvalho de Melo 17079695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 17179695e1bSArnaldo Carvalho de Melo } 17279695e1bSArnaldo Carvalho de Melo 173a8c9ae18SArnaldo Carvalho de Melo static int trace_event__id(const char *evname) 174a8c9ae18SArnaldo Carvalho de Melo { 175a8c9ae18SArnaldo Carvalho de Melo char *filename, *colon; 176a8c9ae18SArnaldo Carvalho de Melo int err = -1, fd; 177a8c9ae18SArnaldo Carvalho de Melo 178a8c9ae18SArnaldo Carvalho de Melo if (asprintf(&filename, "%s/%s/id", tracing_events_path, evname) < 0) 179a8c9ae18SArnaldo Carvalho de Melo return -1; 180a8c9ae18SArnaldo Carvalho de Melo 181a8c9ae18SArnaldo Carvalho de Melo colon = strrchr(filename, ':'); 182a8c9ae18SArnaldo Carvalho de Melo if (colon != NULL) 183a8c9ae18SArnaldo Carvalho de Melo *colon = '/'; 184a8c9ae18SArnaldo Carvalho de Melo 185a8c9ae18SArnaldo Carvalho de Melo fd = open(filename, O_RDONLY); 186a8c9ae18SArnaldo Carvalho de Melo if (fd >= 0) { 187a8c9ae18SArnaldo Carvalho de Melo char id[16]; 188a8c9ae18SArnaldo Carvalho de Melo if (read(fd, id, sizeof(id)) > 0) 189a8c9ae18SArnaldo Carvalho de Melo err = atoi(id); 190a8c9ae18SArnaldo Carvalho de Melo close(fd); 191a8c9ae18SArnaldo Carvalho de Melo } 192a8c9ae18SArnaldo Carvalho de Melo 193a8c9ae18SArnaldo Carvalho de Melo free(filename); 194a8c9ae18SArnaldo Carvalho de Melo return err; 195a8c9ae18SArnaldo Carvalho de Melo } 196a8c9ae18SArnaldo Carvalho de Melo 197a8c9ae18SArnaldo Carvalho de Melo int perf_evlist__add_tracepoints(struct perf_evlist *evlist, 198a8c9ae18SArnaldo Carvalho de Melo const char *tracepoints[], 199a8c9ae18SArnaldo Carvalho de Melo size_t nr_tracepoints) 200a8c9ae18SArnaldo Carvalho de Melo { 201a8c9ae18SArnaldo Carvalho de Melo int err; 202a8c9ae18SArnaldo Carvalho de Melo size_t i; 203a8c9ae18SArnaldo Carvalho de Melo struct perf_event_attr *attrs = zalloc(nr_tracepoints * sizeof(*attrs)); 204a8c9ae18SArnaldo Carvalho de Melo 205a8c9ae18SArnaldo Carvalho de Melo if (attrs == NULL) 206a8c9ae18SArnaldo Carvalho de Melo return -1; 207a8c9ae18SArnaldo Carvalho de Melo 208a8c9ae18SArnaldo Carvalho de Melo for (i = 0; i < nr_tracepoints; i++) { 209a8c9ae18SArnaldo Carvalho de Melo err = trace_event__id(tracepoints[i]); 210a8c9ae18SArnaldo Carvalho de Melo 211a8c9ae18SArnaldo Carvalho de Melo if (err < 0) 212a8c9ae18SArnaldo Carvalho de Melo goto out_free_attrs; 213a8c9ae18SArnaldo Carvalho de Melo 214a8c9ae18SArnaldo Carvalho de Melo attrs[i].type = PERF_TYPE_TRACEPOINT; 215a8c9ae18SArnaldo Carvalho de Melo attrs[i].config = err; 216a8c9ae18SArnaldo Carvalho de Melo attrs[i].sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | 217a8c9ae18SArnaldo Carvalho de Melo PERF_SAMPLE_CPU); 218a8c9ae18SArnaldo Carvalho de Melo attrs[i].sample_period = 1; 219a8c9ae18SArnaldo Carvalho de Melo } 220a8c9ae18SArnaldo Carvalho de Melo 221a8c9ae18SArnaldo Carvalho de Melo err = perf_evlist__add_attrs(evlist, attrs, nr_tracepoints); 222a8c9ae18SArnaldo Carvalho de Melo out_free_attrs: 223a8c9ae18SArnaldo Carvalho de Melo free(attrs); 224a8c9ae18SArnaldo Carvalho de Melo return err; 225a8c9ae18SArnaldo Carvalho de Melo } 226a8c9ae18SArnaldo Carvalho de Melo 227da378962SArnaldo Carvalho de Melo struct perf_evsel * 228ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 229ee29be62SArnaldo Carvalho de Melo { 230ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 231ee29be62SArnaldo Carvalho de Melo 232ee29be62SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 233ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 234ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 235ee29be62SArnaldo Carvalho de Melo return evsel; 236ee29be62SArnaldo Carvalho de Melo } 237ee29be62SArnaldo Carvalho de Melo 238ee29be62SArnaldo Carvalho de Melo return NULL; 239ee29be62SArnaldo Carvalho de Melo } 240ee29be62SArnaldo Carvalho de Melo 241ee29be62SArnaldo Carvalho de Melo int perf_evlist__set_tracepoints_handlers(struct perf_evlist *evlist, 242ee29be62SArnaldo Carvalho de Melo const struct perf_evsel_str_handler *assocs, 243ee29be62SArnaldo Carvalho de Melo size_t nr_assocs) 244ee29be62SArnaldo Carvalho de Melo { 245ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 246ee29be62SArnaldo Carvalho de Melo int err; 247ee29be62SArnaldo Carvalho de Melo size_t i; 248ee29be62SArnaldo Carvalho de Melo 249ee29be62SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 250ee29be62SArnaldo Carvalho de Melo err = trace_event__id(assocs[i].name); 251ee29be62SArnaldo Carvalho de Melo if (err < 0) 252ee29be62SArnaldo Carvalho de Melo goto out; 253ee29be62SArnaldo Carvalho de Melo 254ee29be62SArnaldo Carvalho de Melo evsel = perf_evlist__find_tracepoint_by_id(evlist, err); 255ee29be62SArnaldo Carvalho de Melo if (evsel == NULL) 256ee29be62SArnaldo Carvalho de Melo continue; 257ee29be62SArnaldo Carvalho de Melo 258ee29be62SArnaldo Carvalho de Melo err = -EEXIST; 259ee29be62SArnaldo Carvalho de Melo if (evsel->handler.func != NULL) 260ee29be62SArnaldo Carvalho de Melo goto out; 261ee29be62SArnaldo Carvalho de Melo evsel->handler.func = assocs[i].handler; 262ee29be62SArnaldo Carvalho de Melo } 263ee29be62SArnaldo Carvalho de Melo 264ee29be62SArnaldo Carvalho de Melo err = 0; 265ee29be62SArnaldo Carvalho de Melo out: 266ee29be62SArnaldo Carvalho de Melo return err; 267ee29be62SArnaldo Carvalho de Melo } 268ee29be62SArnaldo Carvalho de Melo 2694152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2704152ab37SArnaldo Carvalho de Melo { 2714152ab37SArnaldo Carvalho de Melo int cpu, thread; 2724152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 2734152ab37SArnaldo Carvalho de Melo 2744152ab37SArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 2754152ab37SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 2764152ab37SArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) 27755da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 27855da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2794152ab37SArnaldo Carvalho de Melo } 2804152ab37SArnaldo Carvalho de Melo } 2814152ab37SArnaldo Carvalho de Melo } 2824152ab37SArnaldo Carvalho de Melo 283764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 284764e16a3SDavid Ahern { 285764e16a3SDavid Ahern int cpu, thread; 286764e16a3SDavid Ahern struct perf_evsel *pos; 287764e16a3SDavid Ahern 288764e16a3SDavid Ahern for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 289764e16a3SDavid Ahern list_for_each_entry(pos, &evlist->entries, node) { 290764e16a3SDavid Ahern for (thread = 0; thread < evlist->threads->nr; thread++) 29155da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 29255da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 293764e16a3SDavid Ahern } 294764e16a3SDavid Ahern } 295764e16a3SDavid Ahern } 296764e16a3SDavid Ahern 297806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 2985c581041SArnaldo Carvalho de Melo { 2997e2ed097SArnaldo Carvalho de Melo int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries; 3005c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 3015c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 3025c581041SArnaldo Carvalho de Melo } 30370082dd9SArnaldo Carvalho de Melo 30470082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 30570082dd9SArnaldo Carvalho de Melo { 30670082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 30770082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 30870082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 30970082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 31070082dd9SArnaldo Carvalho de Melo } 31170db7533SArnaldo Carvalho de Melo 312a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 313a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 3143d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 3153d3b5e95SArnaldo Carvalho de Melo { 3163d3b5e95SArnaldo Carvalho de Melo int hash; 3173d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 3183d3b5e95SArnaldo Carvalho de Melo 3193d3b5e95SArnaldo Carvalho de Melo sid->id = id; 3203d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 3213d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 3223d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 3233d3b5e95SArnaldo Carvalho de Melo } 3243d3b5e95SArnaldo Carvalho de Melo 325a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 326a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 327a91e5431SArnaldo Carvalho de Melo { 328a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 329a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 330a91e5431SArnaldo Carvalho de Melo } 331a91e5431SArnaldo Carvalho de Melo 332a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 3333d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 334f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 335f8a95309SArnaldo Carvalho de Melo { 336f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 3373d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 338f8a95309SArnaldo Carvalho de Melo 339f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 340f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 341f8a95309SArnaldo Carvalho de Melo return -1; 342f8a95309SArnaldo Carvalho de Melo 343f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 344f8a95309SArnaldo Carvalho de Melo ++id_idx; 345f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 346f8a95309SArnaldo Carvalho de Melo ++id_idx; 347f8a95309SArnaldo Carvalho de Melo 348a91e5431SArnaldo Carvalho de Melo perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); 349f8a95309SArnaldo Carvalho de Melo return 0; 350f8a95309SArnaldo Carvalho de Melo } 351f8a95309SArnaldo Carvalho de Melo 35270db7533SArnaldo Carvalho de Melo struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 35370db7533SArnaldo Carvalho de Melo { 35470db7533SArnaldo Carvalho de Melo struct hlist_head *head; 35570db7533SArnaldo Carvalho de Melo struct hlist_node *pos; 35670db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 35770db7533SArnaldo Carvalho de Melo int hash; 35870db7533SArnaldo Carvalho de Melo 35970db7533SArnaldo Carvalho de Melo if (evlist->nr_entries == 1) 36070db7533SArnaldo Carvalho de Melo return list_entry(evlist->entries.next, struct perf_evsel, node); 36170db7533SArnaldo Carvalho de Melo 36270db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 36370db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 36470db7533SArnaldo Carvalho de Melo 36570db7533SArnaldo Carvalho de Melo hlist_for_each_entry(sid, pos, head, node) 36670db7533SArnaldo Carvalho de Melo if (sid->id == id) 36770db7533SArnaldo Carvalho de Melo return sid->evsel; 36830e68bccSNamhyung Kim 36930e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 37030e68bccSNamhyung Kim return list_entry(evlist->entries.next, struct perf_evsel, node); 37130e68bccSNamhyung Kim 37270db7533SArnaldo Carvalho de Melo return NULL; 37370db7533SArnaldo Carvalho de Melo } 37404391debSArnaldo Carvalho de Melo 375aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 37604391debSArnaldo Carvalho de Melo { 37704391debSArnaldo Carvalho de Melo /* XXX Move this to perf.c, making it generally available */ 37804391debSArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 379aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 38004391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 38104391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 38204391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3838115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 38404391debSArnaldo Carvalho de Melo 3857bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 38604391debSArnaldo Carvalho de Melo /* 38704391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 38804391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 38904391debSArnaldo Carvalho de Melo * 39004391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 39104391debSArnaldo Carvalho de Melo * 39204391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 39304391debSArnaldo Carvalho de Melo */ 3947bb41152SArnaldo Carvalho de Melo int diff = head - old; 39504391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 39604391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 39704391debSArnaldo Carvalho de Melo 39804391debSArnaldo Carvalho de Melo /* 39904391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 40004391debSArnaldo Carvalho de Melo */ 40104391debSArnaldo Carvalho de Melo old = head; 40204391debSArnaldo Carvalho de Melo } 4037bb41152SArnaldo Carvalho de Melo } 40404391debSArnaldo Carvalho de Melo 40504391debSArnaldo Carvalho de Melo if (old != head) { 40604391debSArnaldo Carvalho de Melo size_t size; 40704391debSArnaldo Carvalho de Melo 4088115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 40904391debSArnaldo Carvalho de Melo size = event->header.size; 41004391debSArnaldo Carvalho de Melo 41104391debSArnaldo Carvalho de Melo /* 41204391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 41304391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 41404391debSArnaldo Carvalho de Melo */ 41504391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 41604391debSArnaldo Carvalho de Melo unsigned int offset = old; 41704391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 41804391debSArnaldo Carvalho de Melo void *dst = &evlist->event_copy; 41904391debSArnaldo Carvalho de Melo 42004391debSArnaldo Carvalho de Melo do { 42104391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 42204391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 42304391debSArnaldo Carvalho de Melo offset += cpy; 42404391debSArnaldo Carvalho de Melo dst += cpy; 42504391debSArnaldo Carvalho de Melo len -= cpy; 42604391debSArnaldo Carvalho de Melo } while (len); 42704391debSArnaldo Carvalho de Melo 42804391debSArnaldo Carvalho de Melo event = &evlist->event_copy; 42904391debSArnaldo Carvalho de Melo } 43004391debSArnaldo Carvalho de Melo 43104391debSArnaldo Carvalho de Melo old += size; 43204391debSArnaldo Carvalho de Melo } 43304391debSArnaldo Carvalho de Melo 43404391debSArnaldo Carvalho de Melo md->prev = old; 4357bb41152SArnaldo Carvalho de Melo 4367bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 4377bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 4387bb41152SArnaldo Carvalho de Melo 43904391debSArnaldo Carvalho de Melo return event; 44004391debSArnaldo Carvalho de Melo } 441f8a95309SArnaldo Carvalho de Melo 4427e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 443f8a95309SArnaldo Carvalho de Melo { 444aece948fSArnaldo Carvalho de Melo int i; 445f8a95309SArnaldo Carvalho de Melo 446aece948fSArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 447aece948fSArnaldo Carvalho de Melo if (evlist->mmap[i].base != NULL) { 448aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[i].base, evlist->mmap_len); 449aece948fSArnaldo Carvalho de Melo evlist->mmap[i].base = NULL; 450f8a95309SArnaldo Carvalho de Melo } 451f8a95309SArnaldo Carvalho de Melo } 452aece948fSArnaldo Carvalho de Melo 453aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 454aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 455f8a95309SArnaldo Carvalho de Melo } 456f8a95309SArnaldo Carvalho de Melo 457806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 458f8a95309SArnaldo Carvalho de Melo { 459aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->cpus->nr; 460aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[0] == -1) 461aece948fSArnaldo Carvalho de Melo evlist->nr_mmaps = evlist->threads->nr; 462aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 463f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 464f8a95309SArnaldo Carvalho de Melo } 465f8a95309SArnaldo Carvalho de Melo 466bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 467aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 468f8a95309SArnaldo Carvalho de Melo { 469aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 470aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 471aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 472f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 473301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 474301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 475f8a95309SArnaldo Carvalho de Melo return -1; 476301b195dSNelson Elhage } 477f8a95309SArnaldo Carvalho de Melo 478f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 479f8a95309SArnaldo Carvalho de Melo return 0; 480f8a95309SArnaldo Carvalho de Melo } 481f8a95309SArnaldo Carvalho de Melo 482aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(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 cpu, thread; 486aece948fSArnaldo Carvalho de Melo 487aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 488aece948fSArnaldo Carvalho de Melo int output = -1; 489aece948fSArnaldo Carvalho de Melo 490aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 491aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 492aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 493aece948fSArnaldo Carvalho de Melo 494aece948fSArnaldo Carvalho de Melo if (output == -1) { 495aece948fSArnaldo Carvalho de Melo output = fd; 496bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 497aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 498aece948fSArnaldo Carvalho de Melo goto out_unmap; 499aece948fSArnaldo Carvalho de Melo } else { 500aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 501aece948fSArnaldo Carvalho de Melo goto out_unmap; 502aece948fSArnaldo Carvalho de Melo } 503aece948fSArnaldo Carvalho de Melo 504aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 505aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 506aece948fSArnaldo Carvalho de Melo goto out_unmap; 507aece948fSArnaldo Carvalho de Melo } 508aece948fSArnaldo Carvalho de Melo } 509aece948fSArnaldo Carvalho de Melo } 510aece948fSArnaldo Carvalho de Melo 511aece948fSArnaldo Carvalho de Melo return 0; 512aece948fSArnaldo Carvalho de Melo 513aece948fSArnaldo Carvalho de Melo out_unmap: 514aece948fSArnaldo Carvalho de Melo for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { 515aece948fSArnaldo Carvalho de Melo if (evlist->mmap[cpu].base != NULL) { 516aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[cpu].base, evlist->mmap_len); 517aece948fSArnaldo Carvalho de Melo evlist->mmap[cpu].base = NULL; 518aece948fSArnaldo Carvalho de Melo } 519aece948fSArnaldo Carvalho de Melo } 520aece948fSArnaldo Carvalho de Melo return -1; 521aece948fSArnaldo Carvalho de Melo } 522aece948fSArnaldo Carvalho de Melo 523aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) 524aece948fSArnaldo Carvalho de Melo { 525aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 526aece948fSArnaldo Carvalho de Melo int thread; 527aece948fSArnaldo Carvalho de Melo 528aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 529aece948fSArnaldo Carvalho de Melo int output = -1; 530aece948fSArnaldo Carvalho de Melo 531aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 532aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 533aece948fSArnaldo Carvalho de Melo 534aece948fSArnaldo Carvalho de Melo if (output == -1) { 535aece948fSArnaldo Carvalho de Melo output = fd; 536bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 537aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 538aece948fSArnaldo Carvalho de Melo goto out_unmap; 539aece948fSArnaldo Carvalho de Melo } else { 540aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 541aece948fSArnaldo Carvalho de Melo goto out_unmap; 542aece948fSArnaldo Carvalho de Melo } 543aece948fSArnaldo Carvalho de Melo 544aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 545aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 546aece948fSArnaldo Carvalho de Melo goto out_unmap; 547aece948fSArnaldo Carvalho de Melo } 548aece948fSArnaldo Carvalho de Melo } 549aece948fSArnaldo Carvalho de Melo 550aece948fSArnaldo Carvalho de Melo return 0; 551aece948fSArnaldo Carvalho de Melo 552aece948fSArnaldo Carvalho de Melo out_unmap: 553aece948fSArnaldo Carvalho de Melo for (thread = 0; thread < evlist->threads->nr; thread++) { 554aece948fSArnaldo Carvalho de Melo if (evlist->mmap[thread].base != NULL) { 555aece948fSArnaldo Carvalho de Melo munmap(evlist->mmap[thread].base, evlist->mmap_len); 556aece948fSArnaldo Carvalho de Melo evlist->mmap[thread].base = NULL; 557aece948fSArnaldo Carvalho de Melo } 558aece948fSArnaldo Carvalho de Melo } 559aece948fSArnaldo Carvalho de Melo return -1; 560aece948fSArnaldo Carvalho de Melo } 561aece948fSArnaldo Carvalho de Melo 562f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 563f8a95309SArnaldo Carvalho de Melo * 564f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 565f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 566f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 567f8a95309SArnaldo Carvalho de Melo * 568f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 569f8a95309SArnaldo Carvalho de Melo * 570f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 571f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 572f8a95309SArnaldo Carvalho de Melo * 573f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5747e2ed097SArnaldo Carvalho de Melo * 5757e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 576f8a95309SArnaldo Carvalho de Melo */ 57750a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 57850a682ceSArnaldo Carvalho de Melo bool overwrite) 579f8a95309SArnaldo Carvalho de Melo { 580f8a95309SArnaldo Carvalho de Melo unsigned int page_size = sysconf(_SC_PAGE_SIZE); 581aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5827e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5837e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 58450a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 58550a682ceSArnaldo Carvalho de Melo 58650a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 58750a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 58850a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 58941d0d933SNelson Elhage else if (!is_power_of_2(pages)) 59041d0d933SNelson Elhage return -EINVAL; 59150a682ceSArnaldo Carvalho de Melo 59250a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 593f8a95309SArnaldo Carvalho de Melo 5947e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 595f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 596f8a95309SArnaldo Carvalho de Melo 5977e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 598f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 599f8a95309SArnaldo Carvalho de Melo 600f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 601f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 602f8a95309SArnaldo Carvalho de Melo 603f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 604f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 605a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 606f8a95309SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) 607f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 608f8a95309SArnaldo Carvalho de Melo } 609f8a95309SArnaldo Carvalho de Melo 610aece948fSArnaldo Carvalho de Melo if (evlist->cpus->map[0] == -1) 611aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 612f8a95309SArnaldo Carvalho de Melo 613aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 614f8a95309SArnaldo Carvalho de Melo } 6157e2ed097SArnaldo Carvalho de Melo 616b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 617b809ac10SNamhyung Kim struct perf_target *target) 6187e2ed097SArnaldo Carvalho de Melo { 619b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 620b809ac10SNamhyung Kim target->uid); 6217e2ed097SArnaldo Carvalho de Melo 6227e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 6237e2ed097SArnaldo Carvalho de Melo return -1; 6247e2ed097SArnaldo Carvalho de Melo 625879d77d0SNamhyung Kim if (perf_target__has_task(target)) 626d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 627d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 628d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 629879d77d0SNamhyung Kim else 630879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 6317e2ed097SArnaldo Carvalho de Melo 6327e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 6337e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 6347e2ed097SArnaldo Carvalho de Melo 6357e2ed097SArnaldo Carvalho de Melo return 0; 6367e2ed097SArnaldo Carvalho de Melo 6377e2ed097SArnaldo Carvalho de Melo out_delete_threads: 6387e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6397e2ed097SArnaldo Carvalho de Melo return -1; 6407e2ed097SArnaldo Carvalho de Melo } 6417e2ed097SArnaldo Carvalho de Melo 6427e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 6437e2ed097SArnaldo Carvalho de Melo { 6447e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 6457e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6467e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 6477e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 6487e2ed097SArnaldo Carvalho de Melo } 6490a102479SFrederic Weisbecker 6500a102479SFrederic Weisbecker int perf_evlist__set_filters(struct perf_evlist *evlist) 6510a102479SFrederic Weisbecker { 6520a102479SFrederic Weisbecker const struct thread_map *threads = evlist->threads; 6530a102479SFrederic Weisbecker const struct cpu_map *cpus = evlist->cpus; 6540a102479SFrederic Weisbecker struct perf_evsel *evsel; 6550a102479SFrederic Weisbecker char *filter; 6560a102479SFrederic Weisbecker int thread; 6570a102479SFrederic Weisbecker int cpu; 6580a102479SFrederic Weisbecker int err; 6590a102479SFrederic Weisbecker int fd; 6600a102479SFrederic Weisbecker 6610a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 6620a102479SFrederic Weisbecker filter = evsel->filter; 6630a102479SFrederic Weisbecker if (!filter) 6640a102479SFrederic Weisbecker continue; 6650a102479SFrederic Weisbecker for (cpu = 0; cpu < cpus->nr; cpu++) { 6660a102479SFrederic Weisbecker for (thread = 0; thread < threads->nr; thread++) { 6670a102479SFrederic Weisbecker fd = FD(evsel, cpu, thread); 6680a102479SFrederic Weisbecker err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); 6690a102479SFrederic Weisbecker if (err) 6700a102479SFrederic Weisbecker return err; 6710a102479SFrederic Weisbecker } 6720a102479SFrederic Weisbecker } 6730a102479SFrederic Weisbecker } 6740a102479SFrederic Weisbecker 6750a102479SFrederic Weisbecker return 0; 6760a102479SFrederic Weisbecker } 67774429964SFrederic Weisbecker 678c2a70653SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist) 67974429964SFrederic Weisbecker { 680c2a70653SArnaldo Carvalho de Melo struct perf_evsel *pos, *first; 68174429964SFrederic Weisbecker 682c2a70653SArnaldo Carvalho de Melo pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); 683c2a70653SArnaldo Carvalho de Melo 684c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 685c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 686c2a70653SArnaldo Carvalho de Melo return false; 68774429964SFrederic Weisbecker } 68874429964SFrederic Weisbecker 689c2a70653SArnaldo Carvalho de Melo return true; 690c2a70653SArnaldo Carvalho de Melo } 691c2a70653SArnaldo Carvalho de Melo 692c2a70653SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(const struct perf_evlist *evlist) 693c2a70653SArnaldo Carvalho de Melo { 694c2a70653SArnaldo Carvalho de Melo struct perf_evsel *first; 695c2a70653SArnaldo Carvalho de Melo 696c2a70653SArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 697c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 698c2a70653SArnaldo Carvalho de Melo } 699c2a70653SArnaldo Carvalho de Melo 70081e36bffSArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(const struct perf_evlist *evlist) 70181e36bffSArnaldo Carvalho de Melo { 70281e36bffSArnaldo Carvalho de Melo struct perf_evsel *first; 70381e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 70481e36bffSArnaldo Carvalho de Melo u64 sample_type; 70581e36bffSArnaldo Carvalho de Melo u16 size = 0; 70681e36bffSArnaldo Carvalho de Melo 70781e36bffSArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 70881e36bffSArnaldo Carvalho de Melo 70981e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 71081e36bffSArnaldo Carvalho de Melo goto out; 71181e36bffSArnaldo Carvalho de Melo 71281e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 71381e36bffSArnaldo Carvalho de Melo 71481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 71581e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 71681e36bffSArnaldo Carvalho de Melo 71781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 71881e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 71981e36bffSArnaldo Carvalho de Melo 72081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 72181e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 72281e36bffSArnaldo Carvalho de Melo 72381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 72481e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 72581e36bffSArnaldo Carvalho de Melo 72681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 72781e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 72881e36bffSArnaldo Carvalho de Melo out: 72981e36bffSArnaldo Carvalho de Melo return size; 73081e36bffSArnaldo Carvalho de Melo } 73181e36bffSArnaldo Carvalho de Melo 732c2a70653SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist) 733c2a70653SArnaldo Carvalho de Melo { 734c2a70653SArnaldo Carvalho de Melo struct perf_evsel *pos, *first; 735c2a70653SArnaldo Carvalho de Melo 736c2a70653SArnaldo Carvalho de Melo pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); 737c2a70653SArnaldo Carvalho de Melo 738c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 739c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 740c2a70653SArnaldo Carvalho de Melo return false; 741c2a70653SArnaldo Carvalho de Melo } 742c2a70653SArnaldo Carvalho de Melo 743c2a70653SArnaldo Carvalho de Melo return true; 74474429964SFrederic Weisbecker } 74574429964SFrederic Weisbecker 74674429964SFrederic Weisbecker bool perf_evlist__sample_id_all(const struct perf_evlist *evlist) 74774429964SFrederic Weisbecker { 748c2a70653SArnaldo Carvalho de Melo struct perf_evsel *first; 74974429964SFrederic Weisbecker 750c2a70653SArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 751c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 75274429964SFrederic Weisbecker } 75381cce8deSArnaldo Carvalho de Melo 75481cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 75581cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 75681cce8deSArnaldo Carvalho de Melo { 75781cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 75881cce8deSArnaldo Carvalho de Melo } 759727ab04eSArnaldo Carvalho de Melo 760727ab04eSArnaldo Carvalho de Melo int perf_evlist__open(struct perf_evlist *evlist, bool group) 761727ab04eSArnaldo Carvalho de Melo { 762727ab04eSArnaldo Carvalho de Melo struct perf_evsel *evsel, *first; 763727ab04eSArnaldo Carvalho de Melo int err, ncpus, nthreads; 764727ab04eSArnaldo Carvalho de Melo 765727ab04eSArnaldo Carvalho de Melo first = list_entry(evlist->entries.next, struct perf_evsel, node); 766727ab04eSArnaldo Carvalho de Melo 767727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 768727ab04eSArnaldo Carvalho de Melo struct xyarray *group_fd = NULL; 769727ab04eSArnaldo Carvalho de Melo 770727ab04eSArnaldo Carvalho de Melo if (group && evsel != first) 771727ab04eSArnaldo Carvalho de Melo group_fd = first->fd; 772727ab04eSArnaldo Carvalho de Melo 773727ab04eSArnaldo Carvalho de Melo err = perf_evsel__open(evsel, evlist->cpus, evlist->threads, 774727ab04eSArnaldo Carvalho de Melo group, group_fd); 775727ab04eSArnaldo Carvalho de Melo if (err < 0) 776727ab04eSArnaldo Carvalho de Melo goto out_err; 777727ab04eSArnaldo Carvalho de Melo } 778727ab04eSArnaldo Carvalho de Melo 779727ab04eSArnaldo Carvalho de Melo return 0; 780727ab04eSArnaldo Carvalho de Melo out_err: 781727ab04eSArnaldo Carvalho de Melo ncpus = evlist->cpus ? evlist->cpus->nr : 1; 782727ab04eSArnaldo Carvalho de Melo nthreads = evlist->threads ? evlist->threads->nr : 1; 783727ab04eSArnaldo Carvalho de Melo 784727ab04eSArnaldo Carvalho de Melo list_for_each_entry_reverse(evsel, &evlist->entries, node) 785727ab04eSArnaldo Carvalho de Melo perf_evsel__close(evsel, ncpus, nthreads); 786727ab04eSArnaldo Carvalho de Melo 78741c21a68SNamhyung Kim errno = -err; 788727ab04eSArnaldo Carvalho de Melo return err; 789727ab04eSArnaldo Carvalho de Melo } 79035b9d88eSArnaldo Carvalho de Melo 79135b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 79235b9d88eSArnaldo Carvalho de Melo struct perf_record_opts *opts, 79335b9d88eSArnaldo Carvalho de Melo const char *argv[]) 79435b9d88eSArnaldo Carvalho de Melo { 79535b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 79635b9d88eSArnaldo Carvalho de Melo char bf; 79735b9d88eSArnaldo Carvalho de Melo 79835b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 79935b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 80035b9d88eSArnaldo Carvalho de Melo return -1; 80135b9d88eSArnaldo Carvalho de Melo } 80235b9d88eSArnaldo Carvalho de Melo 80335b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 80435b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 80535b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 80635b9d88eSArnaldo Carvalho de Melo } 80735b9d88eSArnaldo Carvalho de Melo 80835b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 80935b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 81035b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 81135b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 81235b9d88eSArnaldo Carvalho de Melo } 81335b9d88eSArnaldo Carvalho de Melo 81435b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 81535b9d88eSArnaldo Carvalho de Melo if (opts->pipe_output) 81635b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 81735b9d88eSArnaldo Carvalho de Melo 81835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 81935b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 82035b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 82135b9d88eSArnaldo Carvalho de Melo 82235b9d88eSArnaldo Carvalho de Melo /* 82335b9d88eSArnaldo Carvalho de Melo * Do a dummy execvp to get the PLT entry resolved, 82435b9d88eSArnaldo Carvalho de Melo * so we avoid the resolver overhead on the real 82535b9d88eSArnaldo Carvalho de Melo * execvp call. 82635b9d88eSArnaldo Carvalho de Melo */ 82735b9d88eSArnaldo Carvalho de Melo execvp("", (char **)argv); 82835b9d88eSArnaldo Carvalho de Melo 82935b9d88eSArnaldo Carvalho de Melo /* 83035b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 83135b9d88eSArnaldo Carvalho de Melo */ 83235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 83335b9d88eSArnaldo Carvalho de Melo 83435b9d88eSArnaldo Carvalho de Melo /* 83535b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 83635b9d88eSArnaldo Carvalho de Melo */ 83735b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 83835b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 83935b9d88eSArnaldo Carvalho de Melo 84035b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 84135b9d88eSArnaldo Carvalho de Melo 84235b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 84335b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 84435b9d88eSArnaldo Carvalho de Melo exit(-1); 84535b9d88eSArnaldo Carvalho de Melo } 84635b9d88eSArnaldo Carvalho de Melo 847d67356e7SNamhyung Kim if (perf_target__none(&opts->target)) 84835b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 84935b9d88eSArnaldo Carvalho de Melo 85035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 85135b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 85235b9d88eSArnaldo Carvalho de Melo /* 85335b9d88eSArnaldo Carvalho de Melo * wait for child to settle 85435b9d88eSArnaldo Carvalho de Melo */ 85535b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 85635b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 85735b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 85835b9d88eSArnaldo Carvalho de Melo } 85935b9d88eSArnaldo Carvalho de Melo 86035b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 86135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 86235b9d88eSArnaldo Carvalho de Melo return 0; 86335b9d88eSArnaldo Carvalho de Melo 86435b9d88eSArnaldo Carvalho de Melo out_close_pipes: 86535b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 86635b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 86735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 86835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 86935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 87035b9d88eSArnaldo Carvalho de Melo return -1; 87135b9d88eSArnaldo Carvalho de Melo } 87235b9d88eSArnaldo Carvalho de Melo 87335b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 87435b9d88eSArnaldo Carvalho de Melo { 87535b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 87635b9d88eSArnaldo Carvalho de Melo /* 87735b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 87835b9d88eSArnaldo Carvalho de Melo */ 87935b9d88eSArnaldo Carvalho de Melo return close(evlist->workload.cork_fd); 88035b9d88eSArnaldo Carvalho de Melo } 88135b9d88eSArnaldo Carvalho de Melo 88235b9d88eSArnaldo Carvalho de Melo return 0; 88335b9d88eSArnaldo Carvalho de Melo } 884