1f8a95309SArnaldo Carvalho de Melo /* 2f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3f8a95309SArnaldo Carvalho de Melo * 4f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 5f8a95309SArnaldo Carvalho de Melo * copyright notes. 6f8a95309SArnaldo Carvalho de Melo * 7f8a95309SArnaldo Carvalho de Melo * Released under the GPL v2. (and only v2, not any later version) 8f8a95309SArnaldo Carvalho de Melo */ 9a8c9ae18SArnaldo Carvalho de Melo #include "util.h" 1085c66be1SBorislav Petkov #include <lk/debugfs.h> 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1412864b31SNamhyung Kim #include "target.h" 15361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 16361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 17e3e1a54fSAdrian Hunter #include "debug.h" 1835b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 19361c99a6SArnaldo Carvalho de Melo 2050d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 2150d08e47SArnaldo Carvalho de Melo 22f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 23f8a95309SArnaldo Carvalho de Melo 2470db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2570db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 2670db7533SArnaldo Carvalho de Melo 27f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 28a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 29f8a95309SArnaldo Carvalho de Melo 307e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 317e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 32361c99a6SArnaldo Carvalho de Melo { 3370db7533SArnaldo Carvalho de Melo int i; 3470db7533SArnaldo Carvalho de Melo 3570db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 3670db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 37361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 387e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 3935b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 40361c99a6SArnaldo Carvalho de Melo } 41361c99a6SArnaldo Carvalho de Melo 42334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 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) 47334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 48ef1d1af2SArnaldo Carvalho de Melo 49361c99a6SArnaldo Carvalho de Melo return evlist; 50361c99a6SArnaldo Carvalho de Melo } 51361c99a6SArnaldo Carvalho de Melo 52f77a9518SArnaldo Carvalho de Melo void perf_evlist__config(struct perf_evlist *evlist, 530f82ebc4SArnaldo Carvalho de Melo struct perf_record_opts *opts) 540f82ebc4SArnaldo Carvalho de Melo { 55cac21425SJiri Olsa struct perf_evsel *evsel; 56f77a9518SArnaldo Carvalho de Melo /* 57f77a9518SArnaldo Carvalho de Melo * Set the evsel leader links before we configure attributes, 58f77a9518SArnaldo Carvalho de Melo * since some might depend on this info. 59f77a9518SArnaldo Carvalho de Melo */ 60f77a9518SArnaldo Carvalho de Melo if (opts->group) 61f77a9518SArnaldo Carvalho de Melo perf_evlist__set_leader(evlist); 620f82ebc4SArnaldo Carvalho de Melo 630f82ebc4SArnaldo Carvalho de Melo if (evlist->cpus->map[0] < 0) 640f82ebc4SArnaldo Carvalho de Melo opts->no_inherit = true; 650f82ebc4SArnaldo Carvalho de Melo 660f82ebc4SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 67cac21425SJiri Olsa perf_evsel__config(evsel, opts); 680f82ebc4SArnaldo Carvalho de Melo 690f82ebc4SArnaldo Carvalho de Melo if (evlist->nr_entries > 1) 703a5afaecSArnaldo Carvalho de Melo perf_evsel__set_sample_id(evsel); 710f82ebc4SArnaldo Carvalho de Melo } 720f82ebc4SArnaldo Carvalho de Melo } 730f82ebc4SArnaldo Carvalho de Melo 74361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 75361c99a6SArnaldo Carvalho de Melo { 76361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 77361c99a6SArnaldo Carvalho de Melo 78361c99a6SArnaldo Carvalho de Melo list_for_each_entry_safe(pos, n, &evlist->entries, node) { 79361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 80361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 81361c99a6SArnaldo Carvalho de Melo } 82361c99a6SArnaldo Carvalho de Melo 83361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 84361c99a6SArnaldo Carvalho de Melo } 85361c99a6SArnaldo Carvalho de Melo 86ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 87ef1d1af2SArnaldo Carvalho de Melo { 88ef1d1af2SArnaldo Carvalho de Melo free(evlist->mmap); 89ef1d1af2SArnaldo Carvalho de Melo free(evlist->pollfd); 90ef1d1af2SArnaldo Carvalho de Melo evlist->mmap = NULL; 91ef1d1af2SArnaldo Carvalho de Melo evlist->pollfd = NULL; 92ef1d1af2SArnaldo Carvalho de Melo } 93ef1d1af2SArnaldo Carvalho de Melo 94361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 95361c99a6SArnaldo Carvalho de Melo { 96361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 97ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 98361c99a6SArnaldo Carvalho de Melo free(evlist); 99361c99a6SArnaldo Carvalho de Melo } 100361c99a6SArnaldo Carvalho de Melo 101361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 102361c99a6SArnaldo Carvalho de Melo { 103361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 104361c99a6SArnaldo Carvalho de Melo ++evlist->nr_entries; 105361c99a6SArnaldo Carvalho de Melo } 106361c99a6SArnaldo Carvalho de Melo 1070529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 10850d08e47SArnaldo Carvalho de Melo struct list_head *list, 10950d08e47SArnaldo Carvalho de Melo int nr_entries) 11050d08e47SArnaldo Carvalho de Melo { 11150d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 11250d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 11350d08e47SArnaldo Carvalho de Melo } 11450d08e47SArnaldo Carvalho de Melo 11563dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 11663dab225SArnaldo Carvalho de Melo { 11763dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 11863dab225SArnaldo Carvalho de Melo 11963dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 12097f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 12197f63e4aSNamhyung Kim 12297f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 12363dab225SArnaldo Carvalho de Melo 12463dab225SArnaldo Carvalho de Melo list_for_each_entry(evsel, list, node) { 12563dab225SArnaldo Carvalho de Melo evsel->leader = leader; 12663dab225SArnaldo Carvalho de Melo } 12763dab225SArnaldo Carvalho de Melo } 12863dab225SArnaldo Carvalho de Melo 12963dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1306a4bb04cSJiri Olsa { 13197f63e4aSNamhyung Kim if (evlist->nr_entries) { 13297f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 13363dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1346a4bb04cSJiri Olsa } 13597f63e4aSNamhyung Kim } 1366a4bb04cSJiri Olsa 137361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 138361c99a6SArnaldo Carvalho de Melo { 139361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 140361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 141361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 142361c99a6SArnaldo Carvalho de Melo }; 1431aed2671SJoerg Roedel struct perf_evsel *evsel; 144361c99a6SArnaldo Carvalho de Melo 1451aed2671SJoerg Roedel event_attr_init(&attr); 1461aed2671SJoerg Roedel 1471aed2671SJoerg Roedel evsel = perf_evsel__new(&attr, 0); 148361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 149cc2d86b0SStephane Eranian goto error; 150cc2d86b0SStephane Eranian 151cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 152cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 153cc2d86b0SStephane Eranian if (!evsel->name) 154cc2d86b0SStephane Eranian goto error_free; 155361c99a6SArnaldo Carvalho de Melo 156361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 157361c99a6SArnaldo Carvalho de Melo return 0; 158cc2d86b0SStephane Eranian error_free: 159cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 160cc2d86b0SStephane Eranian error: 161cc2d86b0SStephane Eranian return -ENOMEM; 162361c99a6SArnaldo Carvalho de Melo } 1635c581041SArnaldo Carvalho de Melo 164e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 16550d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 16650d08e47SArnaldo Carvalho de Melo { 16750d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 16850d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 16950d08e47SArnaldo Carvalho de Melo size_t i; 17050d08e47SArnaldo Carvalho de Melo 17150d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 17250d08e47SArnaldo Carvalho de Melo evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); 17350d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 17450d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 17550d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 17650d08e47SArnaldo Carvalho de Melo } 17750d08e47SArnaldo Carvalho de Melo 17850d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 17950d08e47SArnaldo Carvalho de Melo 18050d08e47SArnaldo Carvalho de Melo return 0; 18150d08e47SArnaldo Carvalho de Melo 18250d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 18350d08e47SArnaldo Carvalho de Melo list_for_each_entry_safe(evsel, n, &head, node) 18450d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 18550d08e47SArnaldo Carvalho de Melo return -1; 18650d08e47SArnaldo Carvalho de Melo } 18750d08e47SArnaldo Carvalho de Melo 18879695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 18979695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 19079695e1bSArnaldo Carvalho de Melo { 19179695e1bSArnaldo Carvalho de Melo size_t i; 19279695e1bSArnaldo Carvalho de Melo 19379695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 19479695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 19579695e1bSArnaldo Carvalho de Melo 19679695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 19779695e1bSArnaldo Carvalho de Melo } 19879695e1bSArnaldo Carvalho de Melo 199da378962SArnaldo Carvalho de Melo struct perf_evsel * 200ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 201ee29be62SArnaldo Carvalho de Melo { 202ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 203ee29be62SArnaldo Carvalho de Melo 204ee29be62SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 205ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 206ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 207ee29be62SArnaldo Carvalho de Melo return evsel; 208ee29be62SArnaldo Carvalho de Melo } 209ee29be62SArnaldo Carvalho de Melo 210ee29be62SArnaldo Carvalho de Melo return NULL; 211ee29be62SArnaldo Carvalho de Melo } 212ee29be62SArnaldo Carvalho de Melo 21339876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 21439876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 21539876e7dSArnaldo Carvalho de Melo { 21639876e7dSArnaldo Carvalho de Melo struct perf_evsel *evsel; 21739876e7dSArnaldo Carvalho de Melo 21839876e7dSArnaldo Carvalho de Melo evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); 21939876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 22039876e7dSArnaldo Carvalho de Melo return -1; 22139876e7dSArnaldo Carvalho de Melo 22239876e7dSArnaldo Carvalho de Melo evsel->handler.func = handler; 22339876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 22439876e7dSArnaldo Carvalho de Melo return 0; 22539876e7dSArnaldo Carvalho de Melo } 22639876e7dSArnaldo Carvalho de Melo 2274152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2284152ab37SArnaldo Carvalho de Melo { 2294152ab37SArnaldo Carvalho de Melo int cpu, thread; 2304152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 231b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 232b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 2334152ab37SArnaldo Carvalho de Melo 234b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 2354152ab37SArnaldo Carvalho de Melo list_for_each_entry(pos, &evlist->entries, node) { 236823254edSNamhyung Kim if (!perf_evsel__is_group_leader(pos)) 2373fe4430dSJiri Olsa continue; 238b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 23955da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 24055da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2414152ab37SArnaldo Carvalho de Melo } 2424152ab37SArnaldo Carvalho de Melo } 2434152ab37SArnaldo Carvalho de Melo } 2444152ab37SArnaldo Carvalho de Melo 245764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 246764e16a3SDavid Ahern { 247764e16a3SDavid Ahern int cpu, thread; 248764e16a3SDavid Ahern struct perf_evsel *pos; 249b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 250b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 251764e16a3SDavid Ahern 252b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 253764e16a3SDavid Ahern list_for_each_entry(pos, &evlist->entries, node) { 254823254edSNamhyung Kim if (!perf_evsel__is_group_leader(pos)) 2553fe4430dSJiri Olsa continue; 256b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 25755da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 25855da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 259764e16a3SDavid Ahern } 260764e16a3SDavid Ahern } 261764e16a3SDavid Ahern } 262764e16a3SDavid Ahern 263806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 2645c581041SArnaldo Carvalho de Melo { 265b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 266b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 267b3a319d5SNamhyung Kim int nfds = nr_cpus * nr_threads * evlist->nr_entries; 2685c581041SArnaldo Carvalho de Melo evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); 2695c581041SArnaldo Carvalho de Melo return evlist->pollfd != NULL ? 0 : -ENOMEM; 2705c581041SArnaldo Carvalho de Melo } 27170082dd9SArnaldo Carvalho de Melo 27270082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 27370082dd9SArnaldo Carvalho de Melo { 27470082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 27570082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].fd = fd; 27670082dd9SArnaldo Carvalho de Melo evlist->pollfd[evlist->nr_fds].events = POLLIN; 27770082dd9SArnaldo Carvalho de Melo evlist->nr_fds++; 27870082dd9SArnaldo Carvalho de Melo } 27970db7533SArnaldo Carvalho de Melo 280a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 281a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 2823d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 2833d3b5e95SArnaldo Carvalho de Melo { 2843d3b5e95SArnaldo Carvalho de Melo int hash; 2853d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 2863d3b5e95SArnaldo Carvalho de Melo 2873d3b5e95SArnaldo Carvalho de Melo sid->id = id; 2883d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 2893d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 2903d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 2913d3b5e95SArnaldo Carvalho de Melo } 2923d3b5e95SArnaldo Carvalho de Melo 293a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 294a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 295a91e5431SArnaldo Carvalho de Melo { 296a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 297a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 298a91e5431SArnaldo Carvalho de Melo } 299a91e5431SArnaldo Carvalho de Melo 300a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 3013d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 302f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 303f8a95309SArnaldo Carvalho de Melo { 304f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 3053d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 306e2b5abe0SJiri Olsa u64 id; 307e2b5abe0SJiri Olsa int ret; 308e2b5abe0SJiri Olsa 309e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 310e2b5abe0SJiri Olsa if (!ret) 311e2b5abe0SJiri Olsa goto add; 312e2b5abe0SJiri Olsa 313e2b5abe0SJiri Olsa if (errno != ENOTTY) 314e2b5abe0SJiri Olsa return -1; 315e2b5abe0SJiri Olsa 316e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 317f8a95309SArnaldo Carvalho de Melo 318c4861afeSJiri Olsa /* 319c4861afeSJiri Olsa * This way does not work with group format read, so bail 320c4861afeSJiri Olsa * out in that case. 321c4861afeSJiri Olsa */ 322c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 323c4861afeSJiri Olsa return -1; 324c4861afeSJiri Olsa 325f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 326f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 327f8a95309SArnaldo Carvalho de Melo return -1; 328f8a95309SArnaldo Carvalho de Melo 329f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 330f8a95309SArnaldo Carvalho de Melo ++id_idx; 331f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 332f8a95309SArnaldo Carvalho de Melo ++id_idx; 333f8a95309SArnaldo Carvalho de Melo 334e2b5abe0SJiri Olsa id = read_data[id_idx]; 335e2b5abe0SJiri Olsa 336e2b5abe0SJiri Olsa add: 337e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 338f8a95309SArnaldo Carvalho de Melo return 0; 339f8a95309SArnaldo Carvalho de Melo } 340f8a95309SArnaldo Carvalho de Melo 341932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 34270db7533SArnaldo Carvalho de Melo { 34370db7533SArnaldo Carvalho de Melo struct hlist_head *head; 34470db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 34570db7533SArnaldo Carvalho de Melo int hash; 34670db7533SArnaldo Carvalho de Melo 34770db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 34870db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 34970db7533SArnaldo Carvalho de Melo 350b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 35170db7533SArnaldo Carvalho de Melo if (sid->id == id) 352932a3594SJiri Olsa return sid; 353932a3594SJiri Olsa 354932a3594SJiri Olsa return NULL; 355932a3594SJiri Olsa } 356932a3594SJiri Olsa 357932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 358932a3594SJiri Olsa { 359932a3594SJiri Olsa struct perf_sample_id *sid; 360932a3594SJiri Olsa 361932a3594SJiri Olsa if (evlist->nr_entries == 1) 362932a3594SJiri Olsa return perf_evlist__first(evlist); 363932a3594SJiri Olsa 364932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 365932a3594SJiri Olsa if (sid) 36670db7533SArnaldo Carvalho de Melo return sid->evsel; 36730e68bccSNamhyung Kim 36830e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 3690c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 37030e68bccSNamhyung Kim 37170db7533SArnaldo Carvalho de Melo return NULL; 37270db7533SArnaldo Carvalho de Melo } 37304391debSArnaldo Carvalho de Melo 374aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 37504391debSArnaldo Carvalho de Melo { 376aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 37704391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 37804391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 37904391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 3808115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 38104391debSArnaldo Carvalho de Melo 3827bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 38304391debSArnaldo Carvalho de Melo /* 38404391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 38504391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 38604391debSArnaldo Carvalho de Melo * 38704391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 38804391debSArnaldo Carvalho de Melo * 38904391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 39004391debSArnaldo Carvalho de Melo */ 3917bb41152SArnaldo Carvalho de Melo int diff = head - old; 39204391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 39304391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 39404391debSArnaldo Carvalho de Melo 39504391debSArnaldo Carvalho de Melo /* 39604391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 39704391debSArnaldo Carvalho de Melo */ 39804391debSArnaldo Carvalho de Melo old = head; 39904391debSArnaldo Carvalho de Melo } 4007bb41152SArnaldo Carvalho de Melo } 40104391debSArnaldo Carvalho de Melo 40204391debSArnaldo Carvalho de Melo if (old != head) { 40304391debSArnaldo Carvalho de Melo size_t size; 40404391debSArnaldo Carvalho de Melo 4058115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 40604391debSArnaldo Carvalho de Melo size = event->header.size; 40704391debSArnaldo Carvalho de Melo 40804391debSArnaldo Carvalho de Melo /* 40904391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 41004391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 41104391debSArnaldo Carvalho de Melo */ 41204391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 41304391debSArnaldo Carvalho de Melo unsigned int offset = old; 41404391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 4150479b8b9SDavid Ahern void *dst = &md->event_copy; 41604391debSArnaldo Carvalho de Melo 41704391debSArnaldo Carvalho de Melo do { 41804391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 41904391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 42004391debSArnaldo Carvalho de Melo offset += cpy; 42104391debSArnaldo Carvalho de Melo dst += cpy; 42204391debSArnaldo Carvalho de Melo len -= cpy; 42304391debSArnaldo Carvalho de Melo } while (len); 42404391debSArnaldo Carvalho de Melo 4250479b8b9SDavid Ahern event = &md->event_copy; 42604391debSArnaldo Carvalho de Melo } 42704391debSArnaldo Carvalho de Melo 42804391debSArnaldo Carvalho de Melo old += size; 42904391debSArnaldo Carvalho de Melo } 43004391debSArnaldo Carvalho de Melo 43104391debSArnaldo Carvalho de Melo md->prev = old; 4327bb41152SArnaldo Carvalho de Melo 4337bb41152SArnaldo Carvalho de Melo if (!evlist->overwrite) 4347bb41152SArnaldo Carvalho de Melo perf_mmap__write_tail(md, old); 4357bb41152SArnaldo Carvalho de Melo 43604391debSArnaldo Carvalho de Melo return event; 43704391debSArnaldo Carvalho de Melo } 438f8a95309SArnaldo Carvalho de Melo 43993edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 44093edcbd9SAdrian Hunter { 44193edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 44293edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 44393edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 44493edcbd9SAdrian Hunter } 44593edcbd9SAdrian Hunter } 44693edcbd9SAdrian Hunter 4477e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 448f8a95309SArnaldo Carvalho de Melo { 449aece948fSArnaldo Carvalho de Melo int i; 450f8a95309SArnaldo Carvalho de Melo 45193edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 45293edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 453aece948fSArnaldo Carvalho de Melo 454aece948fSArnaldo Carvalho de Melo free(evlist->mmap); 455aece948fSArnaldo Carvalho de Melo evlist->mmap = NULL; 456f8a95309SArnaldo Carvalho de Melo } 457f8a95309SArnaldo Carvalho de Melo 458806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 459f8a95309SArnaldo Carvalho de Melo { 460a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 461ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 462b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 463aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 464f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 465f8a95309SArnaldo Carvalho de Melo } 466f8a95309SArnaldo Carvalho de Melo 467bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist, 468aece948fSArnaldo Carvalho de Melo int idx, int prot, int mask, int fd) 469f8a95309SArnaldo Carvalho de Melo { 470aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 471aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].mask = mask; 472aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, 473f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 474301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 475301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 476f8a95309SArnaldo Carvalho de Melo return -1; 477301b195dSNelson Elhage } 478f8a95309SArnaldo Carvalho de Melo 479f8a95309SArnaldo Carvalho de Melo perf_evlist__add_pollfd(evlist, fd); 480f8a95309SArnaldo Carvalho de Melo return 0; 481f8a95309SArnaldo Carvalho de Melo } 482f8a95309SArnaldo Carvalho de Melo 483aece948fSArnaldo Carvalho de Melo static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) 484aece948fSArnaldo Carvalho de Melo { 485aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 486aece948fSArnaldo Carvalho de Melo int cpu, thread; 487b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 488b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 489aece948fSArnaldo Carvalho de Melo 490e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 491b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 492aece948fSArnaldo Carvalho de Melo int output = -1; 493aece948fSArnaldo Carvalho de Melo 494b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 495aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 496aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, cpu, thread); 497aece948fSArnaldo Carvalho de Melo 498aece948fSArnaldo Carvalho de Melo if (output == -1) { 499aece948fSArnaldo Carvalho de Melo output = fd; 500bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, cpu, 501aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 502aece948fSArnaldo Carvalho de Melo goto out_unmap; 503aece948fSArnaldo Carvalho de Melo } else { 504aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 505aece948fSArnaldo Carvalho de Melo goto out_unmap; 506aece948fSArnaldo Carvalho de Melo } 507aece948fSArnaldo Carvalho de Melo 508aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 509aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 510aece948fSArnaldo Carvalho de Melo goto out_unmap; 511aece948fSArnaldo Carvalho de Melo } 512aece948fSArnaldo Carvalho de Melo } 513aece948fSArnaldo Carvalho de Melo } 514aece948fSArnaldo Carvalho de Melo 515aece948fSArnaldo Carvalho de Melo return 0; 516aece948fSArnaldo Carvalho de Melo 517aece948fSArnaldo Carvalho de Melo out_unmap: 51893edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 51993edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 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; 527b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 528aece948fSArnaldo Carvalho de Melo 529e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 530b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 531aece948fSArnaldo Carvalho de Melo int output = -1; 532aece948fSArnaldo Carvalho de Melo 533aece948fSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 534aece948fSArnaldo Carvalho de Melo int fd = FD(evsel, 0, thread); 535aece948fSArnaldo Carvalho de Melo 536aece948fSArnaldo Carvalho de Melo if (output == -1) { 537aece948fSArnaldo Carvalho de Melo output = fd; 538bccdaba0SArnaldo Carvalho de Melo if (__perf_evlist__mmap(evlist, thread, 539aece948fSArnaldo Carvalho de Melo prot, mask, output) < 0) 540aece948fSArnaldo Carvalho de Melo goto out_unmap; 541aece948fSArnaldo Carvalho de Melo } else { 542aece948fSArnaldo Carvalho de Melo if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) 543aece948fSArnaldo Carvalho de Melo goto out_unmap; 544aece948fSArnaldo Carvalho de Melo } 545aece948fSArnaldo Carvalho de Melo 546aece948fSArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 547aece948fSArnaldo Carvalho de Melo perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) 548aece948fSArnaldo Carvalho de Melo goto out_unmap; 549aece948fSArnaldo Carvalho de Melo } 550aece948fSArnaldo Carvalho de Melo } 551aece948fSArnaldo Carvalho de Melo 552aece948fSArnaldo Carvalho de Melo return 0; 553aece948fSArnaldo Carvalho de Melo 554aece948fSArnaldo Carvalho de Melo out_unmap: 55593edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 55693edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 557aece948fSArnaldo Carvalho de Melo return -1; 558aece948fSArnaldo Carvalho de Melo } 559aece948fSArnaldo Carvalho de Melo 560f8a95309SArnaldo Carvalho de Melo /** perf_evlist__mmap - Create per cpu maps to receive events 561f8a95309SArnaldo Carvalho de Melo * 562f8a95309SArnaldo Carvalho de Melo * @evlist - list of events 563f8a95309SArnaldo Carvalho de Melo * @pages - map length in pages 564f8a95309SArnaldo Carvalho de Melo * @overwrite - overwrite older events? 565f8a95309SArnaldo Carvalho de Melo * 566f8a95309SArnaldo Carvalho de Melo * If overwrite is false the user needs to signal event consuption using: 567f8a95309SArnaldo Carvalho de Melo * 568f8a95309SArnaldo Carvalho de Melo * struct perf_mmap *m = &evlist->mmap[cpu]; 569f8a95309SArnaldo Carvalho de Melo * unsigned int head = perf_mmap__read_head(m); 570f8a95309SArnaldo Carvalho de Melo * 571f8a95309SArnaldo Carvalho de Melo * perf_mmap__write_tail(m, head) 5727e2ed097SArnaldo Carvalho de Melo * 5737e2ed097SArnaldo Carvalho de Melo * Using perf_evlist__read_on_cpu does this automatically. 574f8a95309SArnaldo Carvalho de Melo */ 57550a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 57650a682ceSArnaldo Carvalho de Melo bool overwrite) 577f8a95309SArnaldo Carvalho de Melo { 578aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 5797e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 5807e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 58150a682ceSArnaldo Carvalho de Melo int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; 58250a682ceSArnaldo Carvalho de Melo 58350a682ceSArnaldo Carvalho de Melo /* 512 kiB: default amount of unprivileged mlocked memory */ 58450a682ceSArnaldo Carvalho de Melo if (pages == UINT_MAX) 58550a682ceSArnaldo Carvalho de Melo pages = (512 * 1024) / page_size; 58641d0d933SNelson Elhage else if (!is_power_of_2(pages)) 58741d0d933SNelson Elhage return -EINVAL; 58850a682ceSArnaldo Carvalho de Melo 58950a682ceSArnaldo Carvalho de Melo mask = pages * page_size - 1; 590f8a95309SArnaldo Carvalho de Melo 5917e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 592f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 593f8a95309SArnaldo Carvalho de Melo 5947e2ed097SArnaldo Carvalho de Melo if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 595f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 596f8a95309SArnaldo Carvalho de Melo 597f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 598f8a95309SArnaldo Carvalho de Melo evlist->mmap_len = (pages + 1) * page_size; 599f8a95309SArnaldo Carvalho de Melo 600f8a95309SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 601f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 602a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 603a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 604f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 605f8a95309SArnaldo Carvalho de Melo } 606f8a95309SArnaldo Carvalho de Melo 607ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 608aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_thread(evlist, prot, mask); 609f8a95309SArnaldo Carvalho de Melo 610aece948fSArnaldo Carvalho de Melo return perf_evlist__mmap_per_cpu(evlist, prot, mask); 611f8a95309SArnaldo Carvalho de Melo } 6127e2ed097SArnaldo Carvalho de Melo 613b809ac10SNamhyung Kim int perf_evlist__create_maps(struct perf_evlist *evlist, 614b809ac10SNamhyung Kim struct perf_target *target) 6157e2ed097SArnaldo Carvalho de Melo { 616b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 617b809ac10SNamhyung Kim target->uid); 6187e2ed097SArnaldo Carvalho de Melo 6197e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 6207e2ed097SArnaldo Carvalho de Melo return -1; 6217e2ed097SArnaldo Carvalho de Melo 622879d77d0SNamhyung Kim if (perf_target__has_task(target)) 623d67356e7SNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 624d1cb9fceSNamhyung Kim else if (!perf_target__has_cpu(target) && !target->uses_mmap) 625d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 626879d77d0SNamhyung Kim else 627879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 6287e2ed097SArnaldo Carvalho de Melo 6297e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 6307e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 6317e2ed097SArnaldo Carvalho de Melo 6327e2ed097SArnaldo Carvalho de Melo return 0; 6337e2ed097SArnaldo Carvalho de Melo 6347e2ed097SArnaldo Carvalho de Melo out_delete_threads: 6357e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6367e2ed097SArnaldo Carvalho de Melo return -1; 6377e2ed097SArnaldo Carvalho de Melo } 6387e2ed097SArnaldo Carvalho de Melo 6397e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist) 6407e2ed097SArnaldo Carvalho de Melo { 6417e2ed097SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 6427e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 6437e2ed097SArnaldo Carvalho de Melo evlist->cpus = NULL; 6447e2ed097SArnaldo Carvalho de Melo evlist->threads = NULL; 6457e2ed097SArnaldo Carvalho de Melo } 6460a102479SFrederic Weisbecker 6471491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 6480a102479SFrederic Weisbecker { 6490a102479SFrederic Weisbecker struct perf_evsel *evsel; 650745cefc5SArnaldo Carvalho de Melo int err = 0; 651745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 652b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 6530a102479SFrederic Weisbecker 6540a102479SFrederic Weisbecker list_for_each_entry(evsel, &evlist->entries, node) { 655745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 6560a102479SFrederic Weisbecker continue; 657745cefc5SArnaldo Carvalho de Melo 658745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 6590a102479SFrederic Weisbecker if (err) 660745cefc5SArnaldo Carvalho de Melo break; 6610a102479SFrederic Weisbecker } 6620a102479SFrederic Weisbecker 663745cefc5SArnaldo Carvalho de Melo return err; 664745cefc5SArnaldo Carvalho de Melo } 665745cefc5SArnaldo Carvalho de Melo 666745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 667745cefc5SArnaldo Carvalho de Melo { 668745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 669745cefc5SArnaldo Carvalho de Melo int err = 0; 670745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 671b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 672745cefc5SArnaldo Carvalho de Melo 673745cefc5SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 674745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 675745cefc5SArnaldo Carvalho de Melo if (err) 676745cefc5SArnaldo Carvalho de Melo break; 677745cefc5SArnaldo Carvalho de Melo } 678745cefc5SArnaldo Carvalho de Melo 679745cefc5SArnaldo Carvalho de Melo return err; 6800a102479SFrederic Weisbecker } 68174429964SFrederic Weisbecker 6820c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 68374429964SFrederic Weisbecker { 6840c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 685c2a70653SArnaldo Carvalho de Melo 686c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 687c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_type != pos->attr.sample_type) 688c2a70653SArnaldo Carvalho de Melo return false; 68974429964SFrederic Weisbecker } 69074429964SFrederic Weisbecker 691c2a70653SArnaldo Carvalho de Melo return true; 692c2a70653SArnaldo Carvalho de Melo } 693c2a70653SArnaldo Carvalho de Melo 6940c21f736SArnaldo Carvalho de Melo u64 perf_evlist__sample_type(struct perf_evlist *evlist) 695c2a70653SArnaldo Carvalho de Melo { 6960c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 697c2a70653SArnaldo Carvalho de Melo return first->attr.sample_type; 698c2a70653SArnaldo Carvalho de Melo } 699c2a70653SArnaldo Carvalho de Melo 7009ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 7019ede473cSJiri Olsa { 7029ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 7039ede473cSJiri Olsa u64 read_format = first->attr.read_format; 7049ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 7059ede473cSJiri Olsa 7069ede473cSJiri Olsa list_for_each_entry_continue(pos, &evlist->entries, node) { 7079ede473cSJiri Olsa if (read_format != pos->attr.read_format) 7089ede473cSJiri Olsa return false; 7099ede473cSJiri Olsa } 7109ede473cSJiri Olsa 7119ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 7129ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 7139ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 7149ede473cSJiri Olsa return false; 7159ede473cSJiri Olsa } 7169ede473cSJiri Olsa 7179ede473cSJiri Olsa return true; 7189ede473cSJiri Olsa } 7199ede473cSJiri Olsa 7209ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 7219ede473cSJiri Olsa { 7229ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 7239ede473cSJiri Olsa return first->attr.read_format; 7249ede473cSJiri Olsa } 7259ede473cSJiri Olsa 7260c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 72781e36bffSArnaldo Carvalho de Melo { 7280c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 72981e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 73081e36bffSArnaldo Carvalho de Melo u64 sample_type; 73181e36bffSArnaldo Carvalho de Melo u16 size = 0; 73281e36bffSArnaldo Carvalho de Melo 73381e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 73481e36bffSArnaldo Carvalho de Melo goto out; 73581e36bffSArnaldo Carvalho de Melo 73681e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 73781e36bffSArnaldo Carvalho de Melo 73881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 73981e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 74081e36bffSArnaldo Carvalho de Melo 74181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 74281e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 74381e36bffSArnaldo Carvalho de Melo 74481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 74581e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 74681e36bffSArnaldo Carvalho de Melo 74781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 74881e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 74981e36bffSArnaldo Carvalho de Melo 75081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 75181e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 75281e36bffSArnaldo Carvalho de Melo out: 75381e36bffSArnaldo Carvalho de Melo return size; 75481e36bffSArnaldo Carvalho de Melo } 75581e36bffSArnaldo Carvalho de Melo 7560c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 757c2a70653SArnaldo Carvalho de Melo { 7580c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 759c2a70653SArnaldo Carvalho de Melo 760c2a70653SArnaldo Carvalho de Melo list_for_each_entry_continue(pos, &evlist->entries, node) { 761c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 762c2a70653SArnaldo Carvalho de Melo return false; 763c2a70653SArnaldo Carvalho de Melo } 764c2a70653SArnaldo Carvalho de Melo 765c2a70653SArnaldo Carvalho de Melo return true; 76674429964SFrederic Weisbecker } 76774429964SFrederic Weisbecker 7680c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 76974429964SFrederic Weisbecker { 7700c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 771c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 77274429964SFrederic Weisbecker } 77381cce8deSArnaldo Carvalho de Melo 77481cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 77581cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 77681cce8deSArnaldo Carvalho de Melo { 77781cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 77881cce8deSArnaldo Carvalho de Melo } 779727ab04eSArnaldo Carvalho de Melo 780a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 781a74b4b66SNamhyung Kim { 782a74b4b66SNamhyung Kim struct perf_evsel *evsel; 783a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 784a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 785a74b4b66SNamhyung Kim 786a74b4b66SNamhyung Kim list_for_each_entry_reverse(evsel, &evlist->entries, node) 787a74b4b66SNamhyung Kim perf_evsel__close(evsel, ncpus, nthreads); 788a74b4b66SNamhyung Kim } 789a74b4b66SNamhyung Kim 7906a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 791727ab04eSArnaldo Carvalho de Melo { 7926a4bb04cSJiri Olsa struct perf_evsel *evsel; 793a74b4b66SNamhyung Kim int err; 794727ab04eSArnaldo Carvalho de Melo 795727ab04eSArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 7966a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 797727ab04eSArnaldo Carvalho de Melo if (err < 0) 798727ab04eSArnaldo Carvalho de Melo goto out_err; 799727ab04eSArnaldo Carvalho de Melo } 800727ab04eSArnaldo Carvalho de Melo 801727ab04eSArnaldo Carvalho de Melo return 0; 802727ab04eSArnaldo Carvalho de Melo out_err: 803a74b4b66SNamhyung Kim perf_evlist__close(evlist); 80441c21a68SNamhyung Kim errno = -err; 805727ab04eSArnaldo Carvalho de Melo return err; 806727ab04eSArnaldo Carvalho de Melo } 80735b9d88eSArnaldo Carvalho de Melo 80835b9d88eSArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, 8096ef73ec4SNamhyung Kim struct perf_target *target, 81055e162eaSNamhyung Kim const char *argv[], bool pipe_output, 81155e162eaSNamhyung Kim bool want_signal) 81235b9d88eSArnaldo Carvalho de Melo { 81335b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 81435b9d88eSArnaldo Carvalho de Melo char bf; 81535b9d88eSArnaldo Carvalho de Melo 81635b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 81735b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 81835b9d88eSArnaldo Carvalho de Melo return -1; 81935b9d88eSArnaldo Carvalho de Melo } 82035b9d88eSArnaldo Carvalho de Melo 82135b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 82235b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 82335b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 82435b9d88eSArnaldo Carvalho de Melo } 82535b9d88eSArnaldo Carvalho de Melo 82635b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 82735b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 82835b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 82935b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 83035b9d88eSArnaldo Carvalho de Melo } 83135b9d88eSArnaldo Carvalho de Melo 83235b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 833119fa3c9SNamhyung Kim if (pipe_output) 83435b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 83535b9d88eSArnaldo Carvalho de Melo 8360817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 8370817df08SDavid Ahern 83835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 83935b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 84035b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 84135b9d88eSArnaldo Carvalho de Melo 84235b9d88eSArnaldo Carvalho de Melo /* 84335b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 84435b9d88eSArnaldo Carvalho de Melo */ 84535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 84635b9d88eSArnaldo Carvalho de Melo 84735b9d88eSArnaldo Carvalho de Melo /* 84835b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 84935b9d88eSArnaldo Carvalho de Melo */ 85035b9d88eSArnaldo Carvalho de Melo if (read(go_pipe[0], &bf, 1) == -1) 85135b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 85235b9d88eSArnaldo Carvalho de Melo 85335b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 85435b9d88eSArnaldo Carvalho de Melo 85535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 85655e162eaSNamhyung Kim if (want_signal) 85735b9d88eSArnaldo Carvalho de Melo kill(getppid(), SIGUSR1); 85835b9d88eSArnaldo Carvalho de Melo exit(-1); 85935b9d88eSArnaldo Carvalho de Melo } 86035b9d88eSArnaldo Carvalho de Melo 8616ef73ec4SNamhyung Kim if (perf_target__none(target)) 86235b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 86335b9d88eSArnaldo Carvalho de Melo 86435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 86535b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 86635b9d88eSArnaldo Carvalho de Melo /* 86735b9d88eSArnaldo Carvalho de Melo * wait for child to settle 86835b9d88eSArnaldo Carvalho de Melo */ 86935b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 87035b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 87135b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 87235b9d88eSArnaldo Carvalho de Melo } 87335b9d88eSArnaldo Carvalho de Melo 874bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 87535b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 87635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 87735b9d88eSArnaldo Carvalho de Melo return 0; 87835b9d88eSArnaldo Carvalho de Melo 87935b9d88eSArnaldo Carvalho de Melo out_close_pipes: 88035b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 88135b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 88235b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 88335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 88435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 88535b9d88eSArnaldo Carvalho de Melo return -1; 88635b9d88eSArnaldo Carvalho de Melo } 88735b9d88eSArnaldo Carvalho de Melo 88835b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 88935b9d88eSArnaldo Carvalho de Melo { 89035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 891b3824404SDavid Ahern char bf = 0; 892bcf3145fSNamhyung Kim int ret; 89335b9d88eSArnaldo Carvalho de Melo /* 89435b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 89535b9d88eSArnaldo Carvalho de Melo */ 896bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 897bcf3145fSNamhyung Kim if (ret < 0) 898bcf3145fSNamhyung Kim perror("enable to write to pipe"); 899bcf3145fSNamhyung Kim 900bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 901bcf3145fSNamhyung Kim return ret; 90235b9d88eSArnaldo Carvalho de Melo } 90335b9d88eSArnaldo Carvalho de Melo 90435b9d88eSArnaldo Carvalho de Melo return 0; 90535b9d88eSArnaldo Carvalho de Melo } 906cb0b29e0SArnaldo Carvalho de Melo 907a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 9080807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 909cb0b29e0SArnaldo Carvalho de Melo { 9100c21f736SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evlist__first(evlist); 9110807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 912cb0b29e0SArnaldo Carvalho de Melo } 91378f067b3SArnaldo Carvalho de Melo 91478f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 91578f067b3SArnaldo Carvalho de Melo { 91678f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 91778f067b3SArnaldo Carvalho de Melo size_t printed = 0; 91878f067b3SArnaldo Carvalho de Melo 91978f067b3SArnaldo Carvalho de Melo list_for_each_entry(evsel, &evlist->entries, node) { 92078f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 92178f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 92278f067b3SArnaldo Carvalho de Melo } 92378f067b3SArnaldo Carvalho de Melo 92478f067b3SArnaldo Carvalho de Melo return printed + fprintf(fp, "\n");; 92578f067b3SArnaldo Carvalho de Melo } 926