191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f8a95309SArnaldo Carvalho de Melo /* 3f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 4f8a95309SArnaldo Carvalho de Melo * 5f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 6f8a95309SArnaldo Carvalho de Melo * copyright notes. 7f8a95309SArnaldo Carvalho de Melo */ 8956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h> 9a43783aeSArnaldo Carvalho de Melo #include <errno.h> 10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1512864b31SNamhyung Kim #include "target.h" 16361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 17361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 18e3e1a54fSAdrian Hunter #include "debug.h" 1958db1d6eSArnaldo Carvalho de Melo #include "units.h" 2020f2be1dSJiri Olsa #include <internal/lib.h> // page_size 217736627bSAndi Kleen #include "affinity.h" 22c1a604dfSArnaldo Carvalho de Melo #include "../perf.h" 2354cc54deSWang Nan #include "asm/bug.h" 24657ee553SSong Liu #include "bpf-event.h" 25da949f50SArnaldo Carvalho de Melo #include "util/string2.h" 2640c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h" 279607ad3aSArnaldo Carvalho de Melo #include <signal.h> 2835b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 29b397f846SArnaldo Carvalho de Melo #include <sched.h> 30f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h> 31361c99a6SArnaldo Carvalho de Melo 3250d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 334b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 3450d08e47SArnaldo Carvalho de Melo 35bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 3686a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 37f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 38f8a95309SArnaldo Carvalho de Melo 3970db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 4070db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 410389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 428dd2a131SJiri Olsa #include <linux/err.h> 438520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 447f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 454562a739SJiri Olsa #include <perf/evlist.h> 4688761fa1SJiri Olsa #include <perf/evsel.h> 479c3516d1SJiri Olsa #include <perf/cpumap.h> 487728fa0cSJiri Olsa #include <perf/mmap.h> 4970db7533SArnaldo Carvalho de Melo 50e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h> 51e14e5497SArnaldo Carvalho de Melo 52748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 53748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 54748fe088SArnaldo Carvalho de Melo #endif 55748fe088SArnaldo Carvalho de Melo 569dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 578cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 58f8a95309SArnaldo Carvalho de Melo 5952c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 609749b90eSJiri Olsa struct perf_thread_map *threads) 61361c99a6SArnaldo Carvalho de Melo { 624562a739SJiri Olsa perf_evlist__init(&evlist->core); 63453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 6435b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 6554cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 668ab705b5SAlexey Budankov evlist->ctl_fd.fd = -1; 678ab705b5SAlexey Budankov evlist->ctl_fd.ack = -1; 688ab705b5SAlexey Budankov evlist->ctl_fd.pos = -1; 69361c99a6SArnaldo Carvalho de Melo } 70361c99a6SArnaldo Carvalho de Melo 710f98b11cSJiri Olsa struct evlist *evlist__new(void) 72ef1d1af2SArnaldo Carvalho de Melo { 7363503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist)); 74ef1d1af2SArnaldo Carvalho de Melo 75ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 7652c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL); 77ef1d1af2SArnaldo Carvalho de Melo 78361c99a6SArnaldo Carvalho de Melo return evlist; 79361c99a6SArnaldo Carvalho de Melo } 80361c99a6SArnaldo Carvalho de Melo 81*606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void) 82b22d54b0SJiri Olsa { 830f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 84b22d54b0SJiri Olsa 85e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_default(evlist)) { 86c12995a5SJiri Olsa evlist__delete(evlist); 87b22d54b0SJiri Olsa evlist = NULL; 88b22d54b0SJiri Olsa } 89b22d54b0SJiri Olsa 90b22d54b0SJiri Olsa return evlist; 91b22d54b0SJiri Olsa } 92b22d54b0SJiri Olsa 93*606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void) 945bae0250SArnaldo Carvalho de Melo { 950f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 965bae0250SArnaldo Carvalho de Melo 97e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_dummy(evlist)) { 98c12995a5SJiri Olsa evlist__delete(evlist); 995bae0250SArnaldo Carvalho de Melo evlist = NULL; 1005bae0250SArnaldo Carvalho de Melo } 1015bae0250SArnaldo Carvalho de Melo 1025bae0250SArnaldo Carvalho de Melo return evlist; 1035bae0250SArnaldo Carvalho de Melo } 1045bae0250SArnaldo Carvalho de Melo 10575562573SAdrian Hunter /** 1060a7e7ec9SArnaldo Carvalho de Melo * evlist__set_id_pos - set the positions of event ids. 10775562573SAdrian Hunter * @evlist: selected event list 10875562573SAdrian Hunter * 10975562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 11075562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 11175562573SAdrian Hunter */ 1120a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist) 11375562573SAdrian Hunter { 114515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11575562573SAdrian Hunter 11675562573SAdrian Hunter evlist->id_pos = first->id_pos; 11775562573SAdrian Hunter evlist->is_pos = first->is_pos; 11875562573SAdrian Hunter } 11975562573SAdrian Hunter 1200a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist) 121733cd2feSAdrian Hunter { 12232dcd021SJiri Olsa struct evsel *evsel; 123733cd2feSAdrian Hunter 124e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 1254b5e87b7SArnaldo Carvalho de Melo evsel__calc_id_pos(evsel); 126733cd2feSAdrian Hunter 1270a7e7ec9SArnaldo Carvalho de Melo evlist__set_id_pos(evlist); 128733cd2feSAdrian Hunter } 129733cd2feSAdrian Hunter 130e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist) 131361c99a6SArnaldo Carvalho de Melo { 13232dcd021SJiri Olsa struct evsel *pos, *n; 133361c99a6SArnaldo Carvalho de Melo 134e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 135b27c4eceSJiri Olsa list_del_init(&pos->core.node); 136d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 1375eb2dd2aSJiri Olsa evsel__delete(pos); 138361c99a6SArnaldo Carvalho de Melo } 139361c99a6SArnaldo Carvalho de Melo 1406484d2f9SJiri Olsa evlist->core.nr_entries = 0; 141361c99a6SArnaldo Carvalho de Melo } 142361c99a6SArnaldo Carvalho de Melo 143470579b0SJiri Olsa void evlist__exit(struct evlist *evlist) 144ef1d1af2SArnaldo Carvalho de Melo { 14504662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1460b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 14793dd6e28SJiri Olsa perf_evlist__exit(&evlist->core); 148ef1d1af2SArnaldo Carvalho de Melo } 149ef1d1af2SArnaldo Carvalho de Melo 150c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist) 151361c99a6SArnaldo Carvalho de Melo { 1520b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1530b04b3dcSArnaldo Carvalho de Melo return; 1540b04b3dcSArnaldo Carvalho de Melo 155db6b7b13SJiri Olsa evlist__munmap(evlist); 156750b4edeSJiri Olsa evlist__close(evlist); 157e6b1878dSJiri Olsa evlist__purge(evlist); 158470579b0SJiri Olsa evlist__exit(evlist); 159361c99a6SArnaldo Carvalho de Melo free(evlist); 160361c99a6SArnaldo Carvalho de Melo } 161361c99a6SArnaldo Carvalho de Melo 162a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 163361c99a6SArnaldo Carvalho de Melo { 164d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1656484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 16660b0896cSAdrian Hunter entry->tracking = !entry->idx; 167ef503831SArnaldo Carvalho de Melo 1686484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1696484d2f9SJiri Olsa 1706484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 1710a7e7ec9SArnaldo Carvalho de Melo evlist__set_id_pos(evlist); 172361c99a6SArnaldo Carvalho de Melo } 173361c99a6SArnaldo Carvalho de Melo 17416251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1754768230aSAdrian Hunter { 1764768230aSAdrian Hunter evsel->evlist = NULL; 17752e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1784768230aSAdrian Hunter } 1794768230aSAdrian Hunter 180e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list) 18150d08e47SArnaldo Carvalho de Melo { 18232dcd021SJiri Olsa struct evsel *evsel, *temp; 18375562573SAdrian Hunter 184e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 185b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 186a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 187f114d6efSAdrian Hunter } 18850d08e47SArnaldo Carvalho de Melo } 18950d08e47SArnaldo Carvalho de Melo 190c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist, 191c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs) 192c0e53476SArnaldo Carvalho de Melo { 193c0e53476SArnaldo Carvalho de Melo size_t i; 194c0e53476SArnaldo Carvalho de Melo int err; 195c0e53476SArnaldo Carvalho de Melo 196c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 197c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it. 198b02736f7SArnaldo Carvalho de Melo struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name); 199c0e53476SArnaldo Carvalho de Melo if (evsel == NULL) 200c0e53476SArnaldo Carvalho de Melo continue; 201c0e53476SArnaldo Carvalho de Melo 202c0e53476SArnaldo Carvalho de Melo err = -EEXIST; 203c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL) 204c0e53476SArnaldo Carvalho de Melo goto out; 205c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler; 206c0e53476SArnaldo Carvalho de Melo } 207c0e53476SArnaldo Carvalho de Melo 208c0e53476SArnaldo Carvalho de Melo err = 0; 209c0e53476SArnaldo Carvalho de Melo out: 210c0e53476SArnaldo Carvalho de Melo return err; 211c0e53476SArnaldo Carvalho de Melo } 212c0e53476SArnaldo Carvalho de Melo 213a622eafaSArnaldo Carvalho de Melo void __evlist__set_leader(struct list_head *list) 21463dab225SArnaldo Carvalho de Melo { 21532dcd021SJiri Olsa struct evsel *evsel, *leader; 21663dab225SArnaldo Carvalho de Melo 217b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 218b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 21997f63e4aSNamhyung Kim 2205643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 22163dab225SArnaldo Carvalho de Melo 222e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22363dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22463dab225SArnaldo Carvalho de Melo } 22563dab225SArnaldo Carvalho de Melo } 22663dab225SArnaldo Carvalho de Melo 227a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist) 2286a4bb04cSJiri Olsa { 2296484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2306484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 231a622eafaSArnaldo Carvalho de Melo __evlist__set_leader(&evlist->core.entries); 2326a4bb04cSJiri Olsa } 23397f63e4aSNamhyung Kim } 2346a4bb04cSJiri Olsa 235e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise) 236361c99a6SArnaldo Carvalho de Melo { 2378f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_cycles(precise); 238361c99a6SArnaldo Carvalho de Melo 239361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2407c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 241361c99a6SArnaldo Carvalho de Melo 242a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 243361c99a6SArnaldo Carvalho de Melo return 0; 244361c99a6SArnaldo Carvalho de Melo } 2455c581041SArnaldo Carvalho de Melo 246e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist) 2475bae0250SArnaldo Carvalho de Melo { 2485bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2495bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2505bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2515bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2525bae0250SArnaldo Carvalho de Melo }; 2538f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries); 2545bae0250SArnaldo Carvalho de Melo 2555bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2565bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2575bae0250SArnaldo Carvalho de Melo 258a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2595bae0250SArnaldo Carvalho de Melo return 0; 2605bae0250SArnaldo Carvalho de Melo } 2615bae0250SArnaldo Carvalho de Melo 262e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 26350d08e47SArnaldo Carvalho de Melo { 26432dcd021SJiri Olsa struct evsel *evsel, *n; 26550d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 26650d08e47SArnaldo Carvalho de Melo size_t i; 26750d08e47SArnaldo Carvalho de Melo 26850d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2698f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 27050d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27150d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 272b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 27350d08e47SArnaldo Carvalho de Melo } 27450d08e47SArnaldo Carvalho de Melo 275e414fd1aSArnaldo Carvalho de Melo evlist__splice_list_tail(evlist, &head); 27650d08e47SArnaldo Carvalho de Melo 27750d08e47SArnaldo Carvalho de Melo return 0; 27850d08e47SArnaldo Carvalho de Melo 27950d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 280e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2815eb2dd2aSJiri Olsa evsel__delete(evsel); 28250d08e47SArnaldo Carvalho de Melo return -1; 28350d08e47SArnaldo Carvalho de Melo } 28450d08e47SArnaldo Carvalho de Melo 285e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 28679695e1bSArnaldo Carvalho de Melo { 28779695e1bSArnaldo Carvalho de Melo size_t i; 28879695e1bSArnaldo Carvalho de Melo 28979695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 29079695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 29179695e1bSArnaldo Carvalho de Melo 292a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 29379695e1bSArnaldo Carvalho de Melo } 29479695e1bSArnaldo Carvalho de Melo 295b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 296ee29be62SArnaldo Carvalho de Melo { 29732dcd021SJiri Olsa struct evsel *evsel; 298ee29be62SArnaldo Carvalho de Melo 299e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3001fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3011fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 302ee29be62SArnaldo Carvalho de Melo return evsel; 303ee29be62SArnaldo Carvalho de Melo } 304ee29be62SArnaldo Carvalho de Melo 305ee29be62SArnaldo Carvalho de Melo return NULL; 306ee29be62SArnaldo Carvalho de Melo } 307ee29be62SArnaldo Carvalho de Melo 308b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name) 309a2f2804aSDavid Ahern { 31032dcd021SJiri Olsa struct evsel *evsel; 311a2f2804aSDavid Ahern 312e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3131fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 314a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 315a2f2804aSDavid Ahern return evsel; 316a2f2804aSDavid Ahern } 317a2f2804aSDavid Ahern 318a2f2804aSDavid Ahern return NULL; 319a2f2804aSDavid Ahern } 320a2f2804aSDavid Ahern 321e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler) 32239876e7dSArnaldo Carvalho de Melo { 3238f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp(sys, name); 32439876e7dSArnaldo Carvalho de Melo 3258dd2a131SJiri Olsa if (IS_ERR(evsel)) 32639876e7dSArnaldo Carvalho de Melo return -1; 32739876e7dSArnaldo Carvalho de Melo 328744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 329a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 33039876e7dSArnaldo Carvalho de Melo return 0; 33139876e7dSArnaldo Carvalho de Melo } 33239876e7dSArnaldo Carvalho de Melo 33363503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist, 33432dcd021SJiri Olsa struct evsel *evsel) 335bf8e8f4bSAdrian Hunter { 336648b5af3SJiri Olsa if (evsel->core.system_wide) 337bf8e8f4bSAdrian Hunter return 1; 338bf8e8f4bSAdrian Hunter else 339a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 340bf8e8f4bSAdrian Hunter } 341bf8e8f4bSAdrian Hunter 342a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist) 343a8cbe40fSAndi Kleen { 344a8cbe40fSAndi Kleen struct evsel *pos; 345a8cbe40fSAndi Kleen 346a8cbe40fSAndi Kleen /* 347a8cbe40fSAndi Kleen * Reset the per evsel cpu_iter. This is needed because 348a8cbe40fSAndi Kleen * each evsel's cpumap may have a different index space, 349a8cbe40fSAndi Kleen * and some operations need the index to modify 350a8cbe40fSAndi Kleen * the FD xyarray (e.g. open, close) 351a8cbe40fSAndi Kleen */ 352a8cbe40fSAndi Kleen evlist__for_each_entry(evlist, pos) 353a8cbe40fSAndi Kleen pos->cpu_iter = 0; 354a8cbe40fSAndi Kleen } 355a8cbe40fSAndi Kleen 356a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu) 357a8cbe40fSAndi Kleen { 358a8cbe40fSAndi Kleen if (ev->cpu_iter >= ev->core.cpus->nr) 359a8cbe40fSAndi Kleen return true; 360a8cbe40fSAndi Kleen if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu) 361a8cbe40fSAndi Kleen return true; 362a8cbe40fSAndi Kleen return false; 363a8cbe40fSAndi Kleen } 364a8cbe40fSAndi Kleen 365a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu) 366a8cbe40fSAndi Kleen { 367a8cbe40fSAndi Kleen if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) { 368a8cbe40fSAndi Kleen ev->cpu_iter++; 369a8cbe40fSAndi Kleen return false; 370a8cbe40fSAndi Kleen } 371a8cbe40fSAndi Kleen return true; 372a8cbe40fSAndi Kleen } 373a8cbe40fSAndi Kleen 374e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3754152ab37SArnaldo Carvalho de Melo { 37632dcd021SJiri Olsa struct evsel *pos; 377704e2f5bSAndi Kleen struct affinity affinity; 37887cf8360SAdrian Hunter int cpu, i, imm = 0; 37987cf8360SAdrian Hunter bool has_imm = false; 380704e2f5bSAndi Kleen 381704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 382704e2f5bSAndi Kleen return; 383704e2f5bSAndi Kleen 38487cf8360SAdrian Hunter /* Disable 'immediate' events last */ 38587cf8360SAdrian Hunter for (imm = 0; imm <= 1; imm++) { 386704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 387704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 3884152ab37SArnaldo Carvalho de Melo 389e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 390704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 391704e2f5bSAndi Kleen continue; 392c754c382SArnaldo Carvalho de Melo if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd) 3933fe4430dSJiri Olsa continue; 39487cf8360SAdrian Hunter if (pos->immediate) 39587cf8360SAdrian Hunter has_imm = true; 39687cf8360SAdrian Hunter if (pos->immediate != imm) 39787cf8360SAdrian Hunter continue; 398704e2f5bSAndi Kleen evsel__disable_cpu(pos, pos->cpu_iter - 1); 399704e2f5bSAndi Kleen } 400704e2f5bSAndi Kleen } 40187cf8360SAdrian Hunter if (!has_imm) 40287cf8360SAdrian Hunter break; 40387cf8360SAdrian Hunter } 40487cf8360SAdrian Hunter 405704e2f5bSAndi Kleen affinity__cleanup(&affinity); 406704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 407c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 408704e2f5bSAndi Kleen continue; 409704e2f5bSAndi Kleen pos->disabled = true; 4104152ab37SArnaldo Carvalho de Melo } 4112b56bcfbSArnaldo Carvalho de Melo 4122b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 4134152ab37SArnaldo Carvalho de Melo } 4144152ab37SArnaldo Carvalho de Melo 4151c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 416764e16a3SDavid Ahern { 41732dcd021SJiri Olsa struct evsel *pos; 418704e2f5bSAndi Kleen struct affinity affinity; 419704e2f5bSAndi Kleen int cpu, i; 420764e16a3SDavid Ahern 421704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 422704e2f5bSAndi Kleen return; 423704e2f5bSAndi Kleen 424704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 425704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 426704e2f5bSAndi Kleen 427704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 428704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 429704e2f5bSAndi Kleen continue; 430c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 431704e2f5bSAndi Kleen continue; 432704e2f5bSAndi Kleen evsel__enable_cpu(pos, pos->cpu_iter - 1); 433704e2f5bSAndi Kleen } 434704e2f5bSAndi Kleen } 435704e2f5bSAndi Kleen affinity__cleanup(&affinity); 436e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 437c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 4383fe4430dSJiri Olsa continue; 439704e2f5bSAndi Kleen pos->disabled = false; 440764e16a3SDavid Ahern } 4412b56bcfbSArnaldo Carvalho de Melo 4422b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 4432b56bcfbSArnaldo Carvalho de Melo } 4442b56bcfbSArnaldo Carvalho de Melo 445ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist) 4462b56bcfbSArnaldo Carvalho de Melo { 447e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 448764e16a3SDavid Ahern } 449764e16a3SDavid Ahern 45037b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu) 4511c65056cSAdrian Hunter { 45218ef15c6SArnaldo Carvalho de Melo int thread; 4531c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 4541c65056cSAdrian Hunter 4559dfcb759SJiri Olsa if (!evsel->core.fd) 4561c65056cSAdrian Hunter return -EINVAL; 4571c65056cSAdrian Hunter 4581c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 45918ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4601c65056cSAdrian Hunter if (err) 4611c65056cSAdrian Hunter return err; 4621c65056cSAdrian Hunter } 4631c65056cSAdrian Hunter return 0; 4641c65056cSAdrian Hunter } 4651c65056cSAdrian Hunter 46637b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread) 4671c65056cSAdrian Hunter { 46818ef15c6SArnaldo Carvalho de Melo int cpu; 4696549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 4701c65056cSAdrian Hunter 4719dfcb759SJiri Olsa if (!evsel->core.fd) 4721c65056cSAdrian Hunter return -EINVAL; 4731c65056cSAdrian Hunter 4741c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 47518ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4761c65056cSAdrian Hunter if (err) 4771c65056cSAdrian Hunter return err; 4781c65056cSAdrian Hunter } 4791c65056cSAdrian Hunter return 0; 4801c65056cSAdrian Hunter } 4811c65056cSAdrian Hunter 48237b01abeSArnaldo Carvalho de Melo int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx) 4831c65056cSAdrian Hunter { 484315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 4851c65056cSAdrian Hunter 4861c65056cSAdrian Hunter if (per_cpu_mmaps) 48737b01abeSArnaldo Carvalho de Melo return evlist__enable_event_cpu(evlist, evsel, idx); 48837b01abeSArnaldo Carvalho de Melo 48937b01abeSArnaldo Carvalho de Melo return evlist__enable_event_thread(evlist, evsel, idx); 4901c65056cSAdrian Hunter } 4911c65056cSAdrian Hunter 492f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 493e4b356b5SArnaldo Carvalho de Melo { 494ab4c1f9fSAlexey Budankov return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default); 495e4b356b5SArnaldo Carvalho de Melo } 4961b85337dSArnaldo Carvalho de Melo 497f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 4981ddec7f0SArnaldo Carvalho de Melo { 49984227cb1SJiri Olsa return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 5001ddec7f0SArnaldo Carvalho de Melo } 5011ddec7f0SArnaldo Carvalho de Melo 50280ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 503f66a889dSArnaldo Carvalho de Melo { 50480ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 505f66a889dSArnaldo Carvalho de Melo } 506f66a889dSArnaldo Carvalho de Melo 5073ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id) 50870db7533SArnaldo Carvalho de Melo { 50970db7533SArnaldo Carvalho de Melo struct hlist_head *head; 51070db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 51170db7533SArnaldo Carvalho de Melo int hash; 51270db7533SArnaldo Carvalho de Melo 51370db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5141d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 51570db7533SArnaldo Carvalho de Melo 516b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 51770db7533SArnaldo Carvalho de Melo if (sid->id == id) 518932a3594SJiri Olsa return sid; 519932a3594SJiri Olsa 520932a3594SJiri Olsa return NULL; 521932a3594SJiri Olsa } 522932a3594SJiri Olsa 5233ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id) 524932a3594SJiri Olsa { 525932a3594SJiri Olsa struct perf_sample_id *sid; 526932a3594SJiri Olsa 5276484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 528515dbe48SJiri Olsa return evlist__first(evlist); 529932a3594SJiri Olsa 5303ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id); 531932a3594SJiri Olsa if (sid) 53270c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 53330e68bccSNamhyung Kim 5348cedf3a5SArnaldo Carvalho de Melo if (!evlist__sample_id_all(evlist)) 535515dbe48SJiri Olsa return evlist__first(evlist); 53630e68bccSNamhyung Kim 53770db7533SArnaldo Carvalho de Melo return NULL; 53870db7533SArnaldo Carvalho de Melo } 53904391debSArnaldo Carvalho de Melo 5403ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id) 541dddcf6abSAdrian Hunter { 542dddcf6abSAdrian Hunter struct perf_sample_id *sid; 543dddcf6abSAdrian Hunter 544dddcf6abSAdrian Hunter if (!id) 545dddcf6abSAdrian Hunter return NULL; 546dddcf6abSAdrian Hunter 5473ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id); 548dddcf6abSAdrian Hunter if (sid) 54970c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 550dddcf6abSAdrian Hunter 551dddcf6abSAdrian Hunter return NULL; 552dddcf6abSAdrian Hunter } 553dddcf6abSAdrian Hunter 5543ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id) 55575562573SAdrian Hunter { 556b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 55775562573SAdrian Hunter ssize_t n; 55875562573SAdrian Hunter 55975562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 56075562573SAdrian Hunter 56175562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 56275562573SAdrian Hunter if (evlist->id_pos >= n) 56375562573SAdrian Hunter return -1; 56475562573SAdrian Hunter *id = array[evlist->id_pos]; 56575562573SAdrian Hunter } else { 56675562573SAdrian Hunter if (evlist->is_pos > n) 56775562573SAdrian Hunter return -1; 56875562573SAdrian Hunter n -= evlist->is_pos; 56975562573SAdrian Hunter *id = array[n]; 57075562573SAdrian Hunter } 57175562573SAdrian Hunter return 0; 57275562573SAdrian Hunter } 57375562573SAdrian Hunter 5743ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event) 57575562573SAdrian Hunter { 576515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 57775562573SAdrian Hunter struct hlist_head *head; 57875562573SAdrian Hunter struct perf_sample_id *sid; 57975562573SAdrian Hunter int hash; 58075562573SAdrian Hunter u64 id; 58175562573SAdrian Hunter 5826484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 58398be6966SAdrian Hunter return first; 58498be6966SAdrian Hunter 5851fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 58698be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 58798be6966SAdrian Hunter return first; 58875562573SAdrian Hunter 5893ccf8a7bSArnaldo Carvalho de Melo if (evlist__event2id(evlist, event, &id)) 59075562573SAdrian Hunter return NULL; 59175562573SAdrian Hunter 59275562573SAdrian Hunter /* Synthesized events have an id of zero */ 59375562573SAdrian Hunter if (!id) 59498be6966SAdrian Hunter return first; 59575562573SAdrian Hunter 59675562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5971d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 59875562573SAdrian Hunter 59975562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 60075562573SAdrian Hunter if (sid->id == id) 60170c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 60275562573SAdrian Hunter } 60375562573SAdrian Hunter return NULL; 60475562573SAdrian Hunter } 60575562573SAdrian Hunter 6060a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value) 60765aea233SWang Nan { 60865aea233SWang Nan int i; 60965aea233SWang Nan 6100b72d69aSWang Nan if (!evlist->overwrite_mmap) 611078c3386SWang Nan return 0; 612078c3386SWang Nan 613c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6142cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 61565aea233SWang Nan int err; 61665aea233SWang Nan 61765aea233SWang Nan if (fd < 0) 61865aea233SWang Nan continue; 61965aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 62065aea233SWang Nan if (err) 62165aea233SWang Nan return err; 62265aea233SWang Nan } 62365aea233SWang Nan return 0; 62465aea233SWang Nan } 62565aea233SWang Nan 6260a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist) 62765aea233SWang Nan { 6280a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, true); 62965aea233SWang Nan } 63065aea233SWang Nan 6310a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist) 63265aea233SWang Nan { 6330a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, false); 63465aea233SWang Nan } 63565aea233SWang Nan 636db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 637f8a95309SArnaldo Carvalho de Melo { 638aece948fSArnaldo Carvalho de Melo int i; 639f8a95309SArnaldo Carvalho de Melo 640b2cb615dSWang Nan if (evlist->mmap) 641c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 64280e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 643b2cb615dSWang Nan 6440b72d69aSWang Nan if (evlist->overwrite_mmap) 645c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 64680e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 647a1f72618SWang Nan } 648aece948fSArnaldo Carvalho de Melo 649db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 650a1f72618SWang Nan { 651db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 65204662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6530b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 654f8a95309SArnaldo Carvalho de Melo } 655f8a95309SArnaldo Carvalho de Melo 65680e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 65780e53d11SJiri Olsa { 65880e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 65980e53d11SJiri Olsa 66080e53d11SJiri Olsa mmap__munmap(m); 66180e53d11SJiri Olsa } 66280e53d11SJiri Olsa 663d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 6642c5f6d87SKan Liang bool overwrite) 665f8a95309SArnaldo Carvalho de Melo { 666d4c6fb36SWang Nan int i; 667a5830532SJiri Olsa struct mmap *map; 668d4c6fb36SWang Nan 669c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 6708db6d6b1SWang Nan if (!map) 6718db6d6b1SWang Nan return NULL; 672946ae1d4SWang Nan 673c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6746eb65f7aSJiri Olsa struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 6756eb65f7aSJiri Olsa 6764738ca30SArnaldo Carvalho de Melo /* 6774738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 6786afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 6794738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 6804738ca30SArnaldo Carvalho de Melo * dropped. 6814738ca30SArnaldo Carvalho de Melo * 6824738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 6834738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 6844738ca30SArnaldo Carvalho de Melo */ 6856eb65f7aSJiri Olsa perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 6864738ca30SArnaldo Carvalho de Melo } 687353120b4SJiri Olsa 6888db6d6b1SWang Nan return map; 689f8a95309SArnaldo Carvalho de Melo } 690f8a95309SArnaldo Carvalho de Melo 6919abd2ab2SJiri Olsa static void 6929abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 6939abd2ab2SJiri Olsa struct perf_mmap_param *_mp, 6949abd2ab2SJiri Olsa int idx, bool per_cpu) 6959abd2ab2SJiri Olsa { 6969abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 6979abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 6989abd2ab2SJiri Olsa 6999abd2ab2SJiri Olsa auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 7009abd2ab2SJiri Olsa } 7019abd2ab2SJiri Olsa 702bb1b1885SJiri Olsa static struct perf_mmap* 703bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 704bb1b1885SJiri Olsa { 705bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7063805e4f3SJiri Olsa struct mmap *maps; 707bb1b1885SJiri Olsa 7083805e4f3SJiri Olsa maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 709bb1b1885SJiri Olsa 710bb1b1885SJiri Olsa if (!maps) { 7113805e4f3SJiri Olsa maps = evlist__alloc_mmap(evlist, overwrite); 712bb1b1885SJiri Olsa if (!maps) 713bb1b1885SJiri Olsa return NULL; 714bb1b1885SJiri Olsa 7153805e4f3SJiri Olsa if (overwrite) { 716bb1b1885SJiri Olsa evlist->overwrite_mmap = maps; 717bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 718ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 7193805e4f3SJiri Olsa } else { 7203805e4f3SJiri Olsa evlist->mmap = maps; 721bb1b1885SJiri Olsa } 722bb1b1885SJiri Olsa } 723bb1b1885SJiri Olsa 724bb1b1885SJiri Olsa return &maps[idx].core; 725bb1b1885SJiri Olsa } 726bb1b1885SJiri Olsa 727b80132b1SJiri Olsa static int 728b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 729b80132b1SJiri Olsa int output, int cpu) 730b80132b1SJiri Olsa { 731b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core); 732b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 733b80132b1SJiri Olsa 734b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu); 735b80132b1SJiri Olsa } 736b80132b1SJiri Olsa 737f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 738994a1f78SJiri Olsa { 739f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 7408185e881SArnaldo Carvalho de Melo int max; 7418185e881SArnaldo Carvalho de Melo 7428185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 7438185e881SArnaldo Carvalho de Melo /* 7448185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 7458185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 7468185e881SArnaldo Carvalho de Melo * die yet... 7478185e881SArnaldo Carvalho de Melo */ 7488185e881SArnaldo Carvalho de Melo max = 512; 7498185e881SArnaldo Carvalho de Melo } else { 7508185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 7518185e881SArnaldo Carvalho de Melo } 7528185e881SArnaldo Carvalho de Melo 7538185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 7541be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 7551be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 756f5e7150cSArnaldo Carvalho de Melo 757f5e7150cSArnaldo Carvalho de Melo return pages; 758f5e7150cSArnaldo Carvalho de Melo } 759f5e7150cSArnaldo Carvalho de Melo 7609521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 761f5e7150cSArnaldo Carvalho de Melo { 762f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 763f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 764f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 765994a1f78SJiri Olsa return 0; 766994a1f78SJiri Olsa 767994a1f78SJiri Olsa return (pages + 1) * page_size; 768994a1f78SJiri Olsa } 769994a1f78SJiri Olsa 77033c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 77133c2dcfdSDavid Ahern unsigned long max) 772994a1f78SJiri Olsa { 7732fbe4abeSAdrian Hunter unsigned long pages, val; 77427050f53SJiri Olsa static struct parse_tag tags[] = { 77527050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 77627050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 77727050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 77827050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 77927050f53SJiri Olsa { .tag = 0 }, 78027050f53SJiri Olsa }; 781994a1f78SJiri Olsa 7828973504bSDavid Ahern if (str == NULL) 78333c2dcfdSDavid Ahern return -EINVAL; 7848973504bSDavid Ahern 78527050f53SJiri Olsa val = parse_tag_value(str, tags); 7862fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 78727050f53SJiri Olsa /* we got file size value */ 78827050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 78927050f53SJiri Olsa } else { 79027050f53SJiri Olsa /* we got pages count value */ 79127050f53SJiri Olsa char *eptr; 792994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 79333c2dcfdSDavid Ahern if (*eptr != '\0') 79433c2dcfdSDavid Ahern return -EINVAL; 79527050f53SJiri Olsa } 796994a1f78SJiri Olsa 7972bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 79833c2dcfdSDavid Ahern /* leave number of pages at 0 */ 7991dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 8009808143bSJiri Olsa char buf[100]; 8019808143bSJiri Olsa 80233c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 80391529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 8041dbfa938SAdrian Hunter if (!pages) 8051dbfa938SAdrian Hunter return -EINVAL; 8069808143bSJiri Olsa 8079808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 8089808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 8099808143bSJiri Olsa buf, pages); 8102fbe4abeSAdrian Hunter } 8112fbe4abeSAdrian Hunter 81233c2dcfdSDavid Ahern if (pages > max) 81333c2dcfdSDavid Ahern return -EINVAL; 81433c2dcfdSDavid Ahern 81533c2dcfdSDavid Ahern return pages; 81633c2dcfdSDavid Ahern } 81733c2dcfdSDavid Ahern 818e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 81933c2dcfdSDavid Ahern { 82033c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 82133c2dcfdSDavid Ahern long pages; 82233c2dcfdSDavid Ahern 823f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 82433c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 82533c2dcfdSDavid Ahern 82633c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 82733c2dcfdSDavid Ahern if (pages < 0) { 82833c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 829994a1f78SJiri Olsa return -1; 830994a1f78SJiri Olsa } 831994a1f78SJiri Olsa 832994a1f78SJiri Olsa *mmap_pages = pages; 833994a1f78SJiri Olsa return 0; 834994a1f78SJiri Olsa } 835994a1f78SJiri Olsa 836e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 837e9db1310SAdrian Hunter int unset __maybe_unused) 838e9db1310SAdrian Hunter { 839e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 840e9db1310SAdrian Hunter } 841e9db1310SAdrian Hunter 842c83fa7f2SAdrian Hunter /** 8439521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 844c83fa7f2SAdrian Hunter * @evlist: list of events 845c83fa7f2SAdrian Hunter * @pages: map length in pages 846c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 847718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 848718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 849f8a95309SArnaldo Carvalho de Melo * 850c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 8519521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 852c83fa7f2SAdrian Hunter * automatically. 853f8a95309SArnaldo Carvalho de Melo * 854718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 855718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 856718c602dSAdrian Hunter * 857c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 858f8a95309SArnaldo Carvalho de Melo */ 8599521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 8607a276ff6SWang Nan unsigned int auxtrace_pages, 86151255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 86251255a8aSAlexey Budankov int comp_level) 863f8a95309SArnaldo Carvalho de Melo { 86471f566a3SWang Nan /* 86571f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 86671f566a3SWang Nan * Its value is decided by evsel's write_backward. 86771f566a3SWang Nan * So &mp should not be passed through const pointer. 86871f566a3SWang Nan */ 869e440979fSJiri Olsa struct mmap_params mp = { 870e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 871e440979fSJiri Olsa .affinity = affinity, 872e440979fSJiri Olsa .flush = flush, 873e440979fSJiri Olsa .comp_level = comp_level 874e440979fSJiri Olsa }; 875923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = { 8769abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx, 877bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get, 878b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap, 8799abd2ab2SJiri Olsa }; 88050a682ceSArnaldo Carvalho de Melo 881f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 882f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 883f8a95309SArnaldo Carvalho de Melo 884f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 885718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 886718c602dSAdrian Hunter 887923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 888f8a95309SArnaldo Carvalho de Melo } 8897e2ed097SArnaldo Carvalho de Melo 8909521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 891718c602dSAdrian Hunter { 8929521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 893718c602dSAdrian Hunter } 894718c602dSAdrian Hunter 8957748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target) 8967e2ed097SArnaldo Carvalho de Melo { 897147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 898f854839bSJiri Olsa struct perf_cpu_map *cpus; 8999749b90eSJiri Olsa struct perf_thread_map *threads; 9007e2ed097SArnaldo Carvalho de Melo 901147c508fSJin Yao /* 902147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 903147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 904147c508fSJin Yao * target->system_wide = true. 905147c508fSJin Yao * 906147c508fSJin Yao * If specify '--per-thread' only to perf record, 907147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 908147c508fSJin Yao * 909147c508fSJin Yao * So target->per_thread && target->system_wide is false. 910147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 911147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 912147c508fSJin Yao * current behavior. 913147c508fSJin Yao * 914147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 915147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 916147c508fSJin Yao * per-thread data. thread_map__new_str will call 917147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 918147c508fSJin Yao */ 91973c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 920147c508fSJin Yao all_threads); 92174bfd2b2SAdrian Hunter 92274bfd2b2SAdrian Hunter if (!threads) 9237e2ed097SArnaldo Carvalho de Melo return -1; 9247e2ed097SArnaldo Carvalho de Melo 9259c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 926397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 927879d77d0SNamhyung Kim else 9289c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 9297e2ed097SArnaldo Carvalho de Melo 93074bfd2b2SAdrian Hunter if (!cpus) 9317e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 9327e2ed097SArnaldo Carvalho de Melo 933ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 934ec9a77a7SAdrian Hunter 935453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 936d5bc056eSAdrian Hunter 937bfd1b83dSNamhyung Kim /* as evlist now has references, put count here */ 938bfd1b83dSNamhyung Kim perf_cpu_map__put(cpus); 939bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 940bfd1b83dSNamhyung Kim 941d5bc056eSAdrian Hunter return 0; 9427e2ed097SArnaldo Carvalho de Melo 9437e2ed097SArnaldo Carvalho de Melo out_delete_threads: 9447836e52eSJiri Olsa perf_thread_map__put(threads); 9457e2ed097SArnaldo Carvalho de Melo return -1; 9467e2ed097SArnaldo Carvalho de Melo } 9477e2ed097SArnaldo Carvalho de Melo 94824bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 9490a102479SFrederic Weisbecker { 95032dcd021SJiri Olsa struct evsel *evsel; 951745cefc5SArnaldo Carvalho de Melo int err = 0; 9520a102479SFrederic Weisbecker 953e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 954745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 9550a102479SFrederic Weisbecker continue; 956745cefc5SArnaldo Carvalho de Melo 957d988d5eeSKan Liang /* 958d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 959d988d5eeSKan Liang * So evlist and evsel should always be same. 960d988d5eeSKan Liang */ 961a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 96223d4aad4SArnaldo Carvalho de Melo if (err) { 96323d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 964745cefc5SArnaldo Carvalho de Melo break; 9650a102479SFrederic Weisbecker } 96623d4aad4SArnaldo Carvalho de Melo } 9670a102479SFrederic Weisbecker 968745cefc5SArnaldo Carvalho de Melo return err; 969745cefc5SArnaldo Carvalho de Melo } 970745cefc5SArnaldo Carvalho de Melo 97124bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter) 972745cefc5SArnaldo Carvalho de Melo { 97332dcd021SJiri Olsa struct evsel *evsel; 974745cefc5SArnaldo Carvalho de Melo int err = 0; 975745cefc5SArnaldo Carvalho de Melo 97605cea449SArnaldo Carvalho de Melo if (filter == NULL) 97705cea449SArnaldo Carvalho de Melo return -1; 97805cea449SArnaldo Carvalho de Melo 979e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 9801fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 981fdf14720SWang Nan continue; 982fdf14720SWang Nan 983ad681adfSArnaldo Carvalho de Melo err = evsel__set_filter(evsel, filter); 984745cefc5SArnaldo Carvalho de Melo if (err) 985745cefc5SArnaldo Carvalho de Melo break; 986745cefc5SArnaldo Carvalho de Melo } 987745cefc5SArnaldo Carvalho de Melo 988745cefc5SArnaldo Carvalho de Melo return err; 9890a102479SFrederic Weisbecker } 99074429964SFrederic Weisbecker 99124bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter) 99253c92f73SArnaldo Carvalho de Melo { 99353c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 99453c92f73SArnaldo Carvalho de Melo int err = 0; 99553c92f73SArnaldo Carvalho de Melo 99653c92f73SArnaldo Carvalho de Melo if (filter == NULL) 99753c92f73SArnaldo Carvalho de Melo return -1; 99853c92f73SArnaldo Carvalho de Melo 99953c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 100053c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 100153c92f73SArnaldo Carvalho de Melo continue; 100253c92f73SArnaldo Carvalho de Melo 1003ad681adfSArnaldo Carvalho de Melo err = evsel__append_tp_filter(evsel, filter); 100453c92f73SArnaldo Carvalho de Melo if (err) 100553c92f73SArnaldo Carvalho de Melo break; 100653c92f73SArnaldo Carvalho de Melo } 100753c92f73SArnaldo Carvalho de Melo 100853c92f73SArnaldo Carvalho de Melo return err; 100953c92f73SArnaldo Carvalho de Melo } 101053c92f73SArnaldo Carvalho de Melo 1011da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1012cfd70a26SArnaldo Carvalho de Melo { 1013cfd70a26SArnaldo Carvalho de Melo char *filter; 1014be199adaSArnaldo Carvalho de Melo size_t i; 1015cfd70a26SArnaldo Carvalho de Melo 1016be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1017be199adaSArnaldo Carvalho de Melo if (i == 0) { 1018be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 101905cea449SArnaldo Carvalho de Melo return NULL; 1020be199adaSArnaldo Carvalho de Melo } else { 1021be199adaSArnaldo Carvalho de Melo char *tmp; 1022be199adaSArnaldo Carvalho de Melo 1023be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1024be199adaSArnaldo Carvalho de Melo goto out_free; 1025be199adaSArnaldo Carvalho de Melo 1026be199adaSArnaldo Carvalho de Melo free(filter); 1027be199adaSArnaldo Carvalho de Melo filter = tmp; 1028be199adaSArnaldo Carvalho de Melo } 1029be199adaSArnaldo Carvalho de Melo } 1030cfd70a26SArnaldo Carvalho de Melo 103105cea449SArnaldo Carvalho de Melo return filter; 1032be199adaSArnaldo Carvalho de Melo out_free: 1033cfd70a26SArnaldo Carvalho de Melo free(filter); 103405cea449SArnaldo Carvalho de Melo return NULL; 103505cea449SArnaldo Carvalho de Melo } 103605cea449SArnaldo Carvalho de Melo 103724bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 103805cea449SArnaldo Carvalho de Melo { 103905cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 104024bf91a7SArnaldo Carvalho de Melo int ret = evlist__set_tp_filter(evlist, filter); 104105cea449SArnaldo Carvalho de Melo 104205cea449SArnaldo Carvalho de Melo free(filter); 1043cfd70a26SArnaldo Carvalho de Melo return ret; 1044cfd70a26SArnaldo Carvalho de Melo } 1045cfd70a26SArnaldo Carvalho de Melo 104624bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1047be199adaSArnaldo Carvalho de Melo { 104824bf91a7SArnaldo Carvalho de Melo return evlist__set_tp_filter_pids(evlist, 1, &pid); 1049be199adaSArnaldo Carvalho de Melo } 1050be199adaSArnaldo Carvalho de Melo 105124bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 10521827ab5bSArnaldo Carvalho de Melo { 10531827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 105424bf91a7SArnaldo Carvalho de Melo int ret = evlist__append_tp_filter(evlist, filter); 10551827ab5bSArnaldo Carvalho de Melo 10561827ab5bSArnaldo Carvalho de Melo free(filter); 10571827ab5bSArnaldo Carvalho de Melo return ret; 10581827ab5bSArnaldo Carvalho de Melo } 10591827ab5bSArnaldo Carvalho de Melo 106024bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 10611827ab5bSArnaldo Carvalho de Melo { 106224bf91a7SArnaldo Carvalho de Melo return evlist__append_tp_filter_pids(evlist, 1, &pid); 10631827ab5bSArnaldo Carvalho de Melo } 10641827ab5bSArnaldo Carvalho de Melo 1065b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist) 106674429964SFrederic Weisbecker { 106732dcd021SJiri Olsa struct evsel *pos; 1068c2a70653SArnaldo Carvalho de Melo 10696484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 107075562573SAdrian Hunter return true; 107175562573SAdrian Hunter 107275562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 107375562573SAdrian Hunter return false; 107475562573SAdrian Hunter 1075e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 107675562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 107775562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1078c2a70653SArnaldo Carvalho de Melo return false; 107974429964SFrederic Weisbecker } 108074429964SFrederic Weisbecker 1081c2a70653SArnaldo Carvalho de Melo return true; 1082c2a70653SArnaldo Carvalho de Melo } 1083c2a70653SArnaldo Carvalho de Melo 1084b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist) 1085c2a70653SArnaldo Carvalho de Melo { 108632dcd021SJiri Olsa struct evsel *evsel; 108775562573SAdrian Hunter 108875562573SAdrian Hunter if (evlist->combined_sample_type) 108975562573SAdrian Hunter return evlist->combined_sample_type; 109075562573SAdrian Hunter 1091e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 10921fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 109375562573SAdrian Hunter 109475562573SAdrian Hunter return evlist->combined_sample_type; 109575562573SAdrian Hunter } 109675562573SAdrian Hunter 1097b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist) 109875562573SAdrian Hunter { 109975562573SAdrian Hunter evlist->combined_sample_type = 0; 1100b3c2cc2bSArnaldo Carvalho de Melo return __evlist__combined_sample_type(evlist); 1101c2a70653SArnaldo Carvalho de Melo } 1102c2a70653SArnaldo Carvalho de Melo 110392c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist) 110498df858eSAndi Kleen { 110532dcd021SJiri Olsa struct evsel *evsel; 110698df858eSAndi Kleen u64 branch_type = 0; 110798df858eSAndi Kleen 1108e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11091fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 111098df858eSAndi Kleen return branch_type; 111198df858eSAndi Kleen } 111298df858eSAndi Kleen 111363503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 11149ede473cSJiri Olsa { 1115515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 11161fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 11171fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 11189ede473cSJiri Olsa 1119e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 112094d3820fSAdrian Hunter if (read_format != pos->core.attr.read_format) { 112194d3820fSAdrian Hunter pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n", 112294d3820fSAdrian Hunter read_format, (u64)pos->core.attr.read_format); 112394d3820fSAdrian Hunter } 11249ede473cSJiri Olsa } 11259ede473cSJiri Olsa 11269ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 11279ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 11289ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 11299ede473cSJiri Olsa return false; 11309ede473cSJiri Olsa } 11319ede473cSJiri Olsa 11329ede473cSJiri Olsa return true; 11339ede473cSJiri Olsa } 11349ede473cSJiri Olsa 113563503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 113681e36bffSArnaldo Carvalho de Melo { 1137515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 113881e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 113981e36bffSArnaldo Carvalho de Melo u64 sample_type; 114081e36bffSArnaldo Carvalho de Melo u16 size = 0; 114181e36bffSArnaldo Carvalho de Melo 11421fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 114381e36bffSArnaldo Carvalho de Melo goto out; 114481e36bffSArnaldo Carvalho de Melo 11451fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 114681e36bffSArnaldo Carvalho de Melo 114781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 114881e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 114981e36bffSArnaldo Carvalho de Melo 115081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 115181e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 115281e36bffSArnaldo Carvalho de Melo 115381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 115481e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 115581e36bffSArnaldo Carvalho de Melo 115681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 115781e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 115881e36bffSArnaldo Carvalho de Melo 115981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 116081e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 116175562573SAdrian Hunter 116275562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 116375562573SAdrian Hunter size += sizeof(data->id); 116481e36bffSArnaldo Carvalho de Melo out: 116581e36bffSArnaldo Carvalho de Melo return size; 116681e36bffSArnaldo Carvalho de Melo } 116781e36bffSArnaldo Carvalho de Melo 11688cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist) 1169c2a70653SArnaldo Carvalho de Melo { 1170515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1171c2a70653SArnaldo Carvalho de Melo 1172e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 11731fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1174c2a70653SArnaldo Carvalho de Melo return false; 1175c2a70653SArnaldo Carvalho de Melo } 1176c2a70653SArnaldo Carvalho de Melo 1177c2a70653SArnaldo Carvalho de Melo return true; 117874429964SFrederic Weisbecker } 117974429964SFrederic Weisbecker 11808cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist) 118174429964SFrederic Weisbecker { 1182515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11831fc632ceSJiri Olsa return first->core.attr.sample_id_all; 118474429964SFrederic Weisbecker } 118581cce8deSArnaldo Carvalho de Melo 1186900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel) 118781cce8deSArnaldo Carvalho de Melo { 118881cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 118981cce8deSArnaldo Carvalho de Melo } 1190727ab04eSArnaldo Carvalho de Melo 1191750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1192a74b4b66SNamhyung Kim { 119332dcd021SJiri Olsa struct evsel *evsel; 11947736627bSAndi Kleen struct affinity affinity; 11957736627bSAndi Kleen int cpu, i; 1196a74b4b66SNamhyung Kim 11977736627bSAndi Kleen /* 11987736627bSAndi Kleen * With perf record core.cpus is usually NULL. 11997736627bSAndi Kleen * Use the old method to handle this for now. 12007736627bSAndi Kleen */ 12017736627bSAndi Kleen if (!evlist->core.cpus) { 1202475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 120388761fa1SJiri Olsa evsel__close(evsel); 12047736627bSAndi Kleen return; 12057736627bSAndi Kleen } 12067736627bSAndi Kleen 12077736627bSAndi Kleen if (affinity__setup(&affinity) < 0) 12087736627bSAndi Kleen return; 12097736627bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 12107736627bSAndi Kleen affinity__set(&affinity, cpu); 12117736627bSAndi Kleen 12127736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12137736627bSAndi Kleen if (evsel__cpu_iter_skip(evsel, cpu)) 12147736627bSAndi Kleen continue; 12157736627bSAndi Kleen perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1); 12167736627bSAndi Kleen } 12177736627bSAndi Kleen } 12187736627bSAndi Kleen affinity__cleanup(&affinity); 12197736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12207736627bSAndi Kleen perf_evsel__free_fd(&evsel->core); 12217736627bSAndi Kleen perf_evsel__free_id(&evsel->core); 12227736627bSAndi Kleen } 1223a74b4b66SNamhyung Kim } 1224a74b4b66SNamhyung Kim 12257748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist) 12264112eb18SArnaldo Carvalho de Melo { 1227f854839bSJiri Olsa struct perf_cpu_map *cpus; 12289749b90eSJiri Olsa struct perf_thread_map *threads; 12294112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 12304112eb18SArnaldo Carvalho de Melo 12314112eb18SArnaldo Carvalho de Melo /* 12324112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 12334112eb18SArnaldo Carvalho de Melo * an all cpus map. 12344112eb18SArnaldo Carvalho de Melo * 12354112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 12364112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 12374112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 12384112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 12394112eb18SArnaldo Carvalho de Melo */ 12409c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 12418c0498b6SAdrian Hunter if (!cpus) 12424112eb18SArnaldo Carvalho de Melo goto out; 12434112eb18SArnaldo Carvalho de Melo 12444b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 12458c0498b6SAdrian Hunter if (!threads) 12468c0498b6SAdrian Hunter goto out_put; 12474112eb18SArnaldo Carvalho de Melo 1248453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1249bfd1b83dSNamhyung Kim 1250bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 12518c0498b6SAdrian Hunter out_put: 125238f01d8dSJiri Olsa perf_cpu_map__put(cpus); 1253bfd1b83dSNamhyung Kim out: 1254bfd1b83dSNamhyung Kim return err; 12554112eb18SArnaldo Carvalho de Melo } 12564112eb18SArnaldo Carvalho de Melo 1257474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1258727ab04eSArnaldo Carvalho de Melo { 125932dcd021SJiri Olsa struct evsel *evsel; 1260a74b4b66SNamhyung Kim int err; 1261727ab04eSArnaldo Carvalho de Melo 12624112eb18SArnaldo Carvalho de Melo /* 12634112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 12644112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 12654112eb18SArnaldo Carvalho de Melo */ 126603617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 12677748bb71SArnaldo Carvalho de Melo err = evlist__create_syswide_maps(evlist); 12684112eb18SArnaldo Carvalho de Melo if (err < 0) 12694112eb18SArnaldo Carvalho de Melo goto out_err; 12704112eb18SArnaldo Carvalho de Melo } 12714112eb18SArnaldo Carvalho de Melo 12720a7e7ec9SArnaldo Carvalho de Melo evlist__update_id_pos(evlist); 1273733cd2feSAdrian Hunter 1274e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1275af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1276727ab04eSArnaldo Carvalho de Melo if (err < 0) 1277727ab04eSArnaldo Carvalho de Melo goto out_err; 1278727ab04eSArnaldo Carvalho de Melo } 1279727ab04eSArnaldo Carvalho de Melo 1280727ab04eSArnaldo Carvalho de Melo return 0; 1281727ab04eSArnaldo Carvalho de Melo out_err: 1282750b4edeSJiri Olsa evlist__close(evlist); 128341c21a68SNamhyung Kim errno = -err; 1284727ab04eSArnaldo Carvalho de Melo return err; 1285727ab04eSArnaldo Carvalho de Melo } 128635b9d88eSArnaldo Carvalho de Melo 12877b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[], 12887b392ef0SArnaldo Carvalho de Melo bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 128935b9d88eSArnaldo Carvalho de Melo { 129035b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 129135b9d88eSArnaldo Carvalho de Melo char bf; 129235b9d88eSArnaldo Carvalho de Melo 129335b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 129435b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 129535b9d88eSArnaldo Carvalho de Melo return -1; 129635b9d88eSArnaldo Carvalho de Melo } 129735b9d88eSArnaldo Carvalho de Melo 129835b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 129935b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 130035b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 130135b9d88eSArnaldo Carvalho de Melo } 130235b9d88eSArnaldo Carvalho de Melo 130335b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 130435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 130535b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 130635b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 130735b9d88eSArnaldo Carvalho de Melo } 130835b9d88eSArnaldo Carvalho de Melo 130935b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13105f1c4225SArnaldo Carvalho de Melo int ret; 13115f1c4225SArnaldo Carvalho de Melo 1312119fa3c9SNamhyung Kim if (pipe_output) 131335b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 131435b9d88eSArnaldo Carvalho de Melo 13150817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13160817df08SDavid Ahern 131735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 131835b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 131935b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 132035b9d88eSArnaldo Carvalho de Melo 132135b9d88eSArnaldo Carvalho de Melo /* 132235b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 132335b9d88eSArnaldo Carvalho de Melo */ 132435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 132535b9d88eSArnaldo Carvalho de Melo 132635b9d88eSArnaldo Carvalho de Melo /* 132735b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 132835b9d88eSArnaldo Carvalho de Melo */ 13295f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 13305f1c4225SArnaldo Carvalho de Melo /* 13315f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 13325f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 13337b392ef0SArnaldo Carvalho de Melo * evlist__start_workload(). 13345f1c4225SArnaldo Carvalho de Melo * 133520f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 13365f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 13375f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 13385f1c4225SArnaldo Carvalho de Melo * here. 13395f1c4225SArnaldo Carvalho de Melo */ 13405f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 13415f1c4225SArnaldo Carvalho de Melo if (ret == -1) 134235b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 13435f1c4225SArnaldo Carvalho de Melo exit(ret); 13445f1c4225SArnaldo Carvalho de Melo } 134535b9d88eSArnaldo Carvalho de Melo 134635b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 134735b9d88eSArnaldo Carvalho de Melo 1348735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1349f33cbe72SArnaldo Carvalho de Melo union sigval val; 1350f33cbe72SArnaldo Carvalho de Melo 1351f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1352f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 135335b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1354f33cbe72SArnaldo Carvalho de Melo } else 1355f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 135635b9d88eSArnaldo Carvalho de Melo exit(-1); 135735b9d88eSArnaldo Carvalho de Melo } 135835b9d88eSArnaldo Carvalho de Melo 1359735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1360735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1361735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1362735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1363735f7e0bSArnaldo Carvalho de Melo }; 1364735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1365735f7e0bSArnaldo Carvalho de Melo } 1366735f7e0bSArnaldo Carvalho de Melo 13671aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 136803617c22SJiri Olsa if (evlist->core.threads == NULL) { 13691aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 13701aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 13711aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 13721aaf63b1SArnaldo Carvalho de Melo } 137303617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 13741aaf63b1SArnaldo Carvalho de Melo } 137535b9d88eSArnaldo Carvalho de Melo 137635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 137735b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 137835b9d88eSArnaldo Carvalho de Melo /* 137935b9d88eSArnaldo Carvalho de Melo * wait for child to settle 138035b9d88eSArnaldo Carvalho de Melo */ 138135b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 138235b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 138335b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 138435b9d88eSArnaldo Carvalho de Melo } 138535b9d88eSArnaldo Carvalho de Melo 1386bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 138735b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 138835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 138935b9d88eSArnaldo Carvalho de Melo return 0; 139035b9d88eSArnaldo Carvalho de Melo 139135b9d88eSArnaldo Carvalho de Melo out_close_pipes: 139235b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 139335b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 139435b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 139535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 139635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 139735b9d88eSArnaldo Carvalho de Melo return -1; 139835b9d88eSArnaldo Carvalho de Melo } 139935b9d88eSArnaldo Carvalho de Melo 14007b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist) 140135b9d88eSArnaldo Carvalho de Melo { 140235b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1403b3824404SDavid Ahern char bf = 0; 1404bcf3145fSNamhyung Kim int ret; 140535b9d88eSArnaldo Carvalho de Melo /* 140635b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 140735b9d88eSArnaldo Carvalho de Melo */ 1408bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1409bcf3145fSNamhyung Kim if (ret < 0) 1410e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1411bcf3145fSNamhyung Kim 1412bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1413bcf3145fSNamhyung Kim return ret; 141435b9d88eSArnaldo Carvalho de Melo } 141535b9d88eSArnaldo Carvalho de Melo 141635b9d88eSArnaldo Carvalho de Melo return 0; 141735b9d88eSArnaldo Carvalho de Melo } 1418cb0b29e0SArnaldo Carvalho de Melo 14192a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample) 1420cb0b29e0SArnaldo Carvalho de Melo { 14213ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event); 142275562573SAdrian Hunter 142375562573SAdrian Hunter if (!evsel) 142475562573SAdrian Hunter return -EFAULT; 14256b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample(evsel, event, sample); 1426cb0b29e0SArnaldo Carvalho de Melo } 142778f067b3SArnaldo Carvalho de Melo 14282a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp) 142901468120SJiri Olsa { 14303ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event); 143101468120SJiri Olsa 143201468120SJiri Olsa if (!evsel) 143301468120SJiri Olsa return -EFAULT; 14346b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample_timestamp(evsel, event, timestamp); 143501468120SJiri Olsa } 143601468120SJiri Olsa 1437d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size) 1438a8f23d8fSArnaldo Carvalho de Melo { 1439a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1440c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1441a8f23d8fSArnaldo Carvalho de Melo 1442a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1443a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1444a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1445a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1446a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1447a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1448a8f23d8fSArnaldo Carvalho de Melo 14491a47245dSAdrian Hunter value = perf_event_paranoid(); 1450a8f23d8fSArnaldo Carvalho de Melo 1451a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1452a8f23d8fSArnaldo Carvalho de Melo 1453a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1454a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1455a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1456a8f23d8fSArnaldo Carvalho de Melo } 1457a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14585229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1459a8f23d8fSArnaldo Carvalho de Melo 1460a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14615229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 14625229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1463a8f23d8fSArnaldo Carvalho de Melo break; 1464d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1465515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1466d9aade7fSArnaldo Carvalho de Melo int max_freq; 1467d9aade7fSArnaldo Carvalho de Melo 1468d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1469d9aade7fSArnaldo Carvalho de Melo goto out_default; 1470d9aade7fSArnaldo Carvalho de Melo 14711fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1472d9aade7fSArnaldo Carvalho de Melo goto out_default; 1473d9aade7fSArnaldo Carvalho de Melo 1474d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1475d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1476d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1477d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 14781fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1479d9aade7fSArnaldo Carvalho de Melo break; 1480d9aade7fSArnaldo Carvalho de Melo } 1481a8f23d8fSArnaldo Carvalho de Melo default: 1482d9aade7fSArnaldo Carvalho de Melo out_default: 1483a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1484a8f23d8fSArnaldo Carvalho de Melo break; 1485a8f23d8fSArnaldo Carvalho de Melo } 1486a8f23d8fSArnaldo Carvalho de Melo 1487a8f23d8fSArnaldo Carvalho de Melo return 0; 1488a8f23d8fSArnaldo Carvalho de Melo } 1489a025e4f0SAdrian Hunter 1490d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1491956fa571SArnaldo Carvalho de Melo { 1492c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1493f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1494956fa571SArnaldo Carvalho de Melo 1495956fa571SArnaldo Carvalho de Melo switch (err) { 1496956fa571SArnaldo Carvalho de Melo case EPERM: 1497e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1498e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1499e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1500956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1501e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1502e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1503e965bea1SArnaldo Carvalho de Melo 1504e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1505e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1506e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1507e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1508e965bea1SArnaldo Carvalho de Melo } 1509e965bea1SArnaldo Carvalho de Melo 1510e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1511e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1512956fa571SArnaldo Carvalho de Melo break; 1513956fa571SArnaldo Carvalho de Melo default: 1514956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1515956fa571SArnaldo Carvalho de Melo break; 1516956fa571SArnaldo Carvalho de Melo } 1517956fa571SArnaldo Carvalho de Melo 1518956fa571SArnaldo Carvalho de Melo return 0; 1519956fa571SArnaldo Carvalho de Melo } 1520956fa571SArnaldo Carvalho de Melo 1521e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel) 1522a025e4f0SAdrian Hunter { 152332dcd021SJiri Olsa struct evsel *evsel, *n; 1524a025e4f0SAdrian Hunter LIST_HEAD(move); 1525a025e4f0SAdrian Hunter 1526515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1527a025e4f0SAdrian Hunter return; 1528a025e4f0SAdrian Hunter 1529e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1530a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1531b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1532a025e4f0SAdrian Hunter } 1533a025e4f0SAdrian Hunter 1534ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1535a025e4f0SAdrian Hunter } 153660b0896cSAdrian Hunter 1537e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist) 1538442ad225SAdrian Hunter { 1539442ad225SAdrian Hunter struct evsel *evsel; 1540442ad225SAdrian Hunter 1541442ad225SAdrian Hunter evlist__for_each_entry(evlist, evsel) { 1542442ad225SAdrian Hunter if (evsel->tracking) 1543442ad225SAdrian Hunter return evsel; 1544442ad225SAdrian Hunter } 1545442ad225SAdrian Hunter 1546442ad225SAdrian Hunter return evlist__first(evlist); 1547442ad225SAdrian Hunter } 1548442ad225SAdrian Hunter 1549e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel) 155060b0896cSAdrian Hunter { 155132dcd021SJiri Olsa struct evsel *evsel; 155260b0896cSAdrian Hunter 155360b0896cSAdrian Hunter if (tracking_evsel->tracking) 155460b0896cSAdrian Hunter return; 155560b0896cSAdrian Hunter 1556e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 155760b0896cSAdrian Hunter if (evsel != tracking_evsel) 155860b0896cSAdrian Hunter evsel->tracking = false; 155960b0896cSAdrian Hunter } 156060b0896cSAdrian Hunter 156160b0896cSAdrian Hunter tracking_evsel->tracking = true; 156260b0896cSAdrian Hunter } 15637630b3e2SWang Nan 1564b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str) 15657630b3e2SWang Nan { 156632dcd021SJiri Olsa struct evsel *evsel; 15677630b3e2SWang Nan 1568e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 15697630b3e2SWang Nan if (!evsel->name) 15707630b3e2SWang Nan continue; 15717630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 15727630b3e2SWang Nan return evsel; 15737630b3e2SWang Nan } 15747630b3e2SWang Nan 15757630b3e2SWang Nan return NULL; 15767630b3e2SWang Nan } 157754cc54deSWang Nan 1578ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state) 157954cc54deSWang Nan { 158054cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 158154cc54deSWang Nan enum action { 158254cc54deSWang Nan NONE, 158354cc54deSWang Nan PAUSE, 158454cc54deSWang Nan RESUME, 158554cc54deSWang Nan } action = NONE; 158654cc54deSWang Nan 15870b72d69aSWang Nan if (!evlist->overwrite_mmap) 158854cc54deSWang Nan return; 158954cc54deSWang Nan 159054cc54deSWang Nan switch (old_state) { 159154cc54deSWang Nan case BKW_MMAP_NOTREADY: { 159254cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1593dd8bd53aSLuis de Bethencourt goto state_err; 159454cc54deSWang Nan break; 159554cc54deSWang Nan } 159654cc54deSWang Nan case BKW_MMAP_RUNNING: { 159754cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 159854cc54deSWang Nan goto state_err; 159954cc54deSWang Nan action = PAUSE; 160054cc54deSWang Nan break; 160154cc54deSWang Nan } 160254cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 160354cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 160454cc54deSWang Nan goto state_err; 160554cc54deSWang Nan break; 160654cc54deSWang Nan } 160754cc54deSWang Nan case BKW_MMAP_EMPTY: { 160854cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 160954cc54deSWang Nan goto state_err; 161054cc54deSWang Nan action = RESUME; 161154cc54deSWang Nan break; 161254cc54deSWang Nan } 161354cc54deSWang Nan default: 161454cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 161554cc54deSWang Nan } 161654cc54deSWang Nan 161754cc54deSWang Nan evlist->bkw_mmap_state = state; 161854cc54deSWang Nan 161954cc54deSWang Nan switch (action) { 162054cc54deSWang Nan case PAUSE: 16210a60b339SArnaldo Carvalho de Melo evlist__pause(evlist); 162254cc54deSWang Nan break; 162354cc54deSWang Nan case RESUME: 16240a60b339SArnaldo Carvalho de Melo evlist__resume(evlist); 162554cc54deSWang Nan break; 162654cc54deSWang Nan case NONE: 162754cc54deSWang Nan default: 162854cc54deSWang Nan break; 162954cc54deSWang Nan } 163054cc54deSWang Nan 163154cc54deSWang Nan state_err: 163254cc54deSWang Nan return; 163354cc54deSWang Nan } 163407d6f446SArnaldo Carvalho de Melo 163563503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 163607d6f446SArnaldo Carvalho de Melo { 163732dcd021SJiri Olsa struct evsel *evsel; 163807d6f446SArnaldo Carvalho de Melo 163907d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16401fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 164107d6f446SArnaldo Carvalho de Melo return false; 164207d6f446SArnaldo Carvalho de Melo } 164307d6f446SArnaldo Carvalho de Melo 164407d6f446SArnaldo Carvalho de Melo return true; 164507d6f446SArnaldo Carvalho de Melo } 1646e2bdbe80SJin Yao 1647e2bdbe80SJin Yao /* 1648e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1649e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1650e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1651e2bdbe80SJin Yao */ 165264b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist) 1653e2bdbe80SJin Yao { 1654e2bdbe80SJin Yao if (!evlist->nr_groups) { 1655515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1656e2bdbe80SJin Yao 1657a622eafaSArnaldo Carvalho de Melo evlist__set_leader(evlist); 1658e2bdbe80SJin Yao leader->forced_leader = true; 1659e2bdbe80SJin Yao } 1660e2bdbe80SJin Yao } 1661c3537fc2SAndi Kleen 166264b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close) 1663c3537fc2SAndi Kleen { 166432dcd021SJiri Olsa struct evsel *c2, *leader; 1665c3537fc2SAndi Kleen bool is_open = true; 1666c3537fc2SAndi Kleen 1667c3537fc2SAndi Kleen leader = evsel->leader; 1668c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 16695643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1670c3537fc2SAndi Kleen 1671c3537fc2SAndi Kleen /* 1672c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1673c3537fc2SAndi Kleen * include the first entry. 1674c3537fc2SAndi Kleen */ 1675c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1676c3537fc2SAndi Kleen if (c2 == evsel) 1677c3537fc2SAndi Kleen is_open = false; 1678c3537fc2SAndi Kleen if (c2->leader == leader) { 16794804e011SAndi Kleen if (is_open && close) 16805a40e199SAndi Kleen perf_evsel__close(&c2->core); 1681c3537fc2SAndi Kleen c2->leader = c2; 16825643b1a5SJiri Olsa c2->core.nr_members = 0; 16834804e011SAndi Kleen /* 16844804e011SAndi Kleen * Set this for all former members of the group 16854804e011SAndi Kleen * to indicate they get reopened. 16864804e011SAndi Kleen */ 16874804e011SAndi Kleen c2->reset_group = true; 1688c3537fc2SAndi Kleen } 1689c3537fc2SAndi Kleen } 1690c3537fc2SAndi Kleen return leader; 1691c3537fc2SAndi Kleen } 1692ec886bf5SAlexey Budankov 1693a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1694a8fcbd26SAdrian Hunter { 1695a8fcbd26SAdrian Hunter char *s, *p; 1696a8fcbd26SAdrian Hunter int ret = 0, fd; 1697a8fcbd26SAdrian Hunter 1698a8fcbd26SAdrian Hunter if (strncmp(str, "fifo:", 5)) 1699a8fcbd26SAdrian Hunter return -EINVAL; 1700a8fcbd26SAdrian Hunter 1701a8fcbd26SAdrian Hunter str += 5; 1702a8fcbd26SAdrian Hunter if (!*str || *str == ',') 1703a8fcbd26SAdrian Hunter return -EINVAL; 1704a8fcbd26SAdrian Hunter 1705a8fcbd26SAdrian Hunter s = strdup(str); 1706a8fcbd26SAdrian Hunter if (!s) 1707a8fcbd26SAdrian Hunter return -ENOMEM; 1708a8fcbd26SAdrian Hunter 1709a8fcbd26SAdrian Hunter p = strchr(s, ','); 1710a8fcbd26SAdrian Hunter if (p) 1711a8fcbd26SAdrian Hunter *p = '\0'; 1712a8fcbd26SAdrian Hunter 1713a8fcbd26SAdrian Hunter /* 1714a8fcbd26SAdrian Hunter * O_RDWR avoids POLLHUPs which is necessary to allow the other 1715a8fcbd26SAdrian Hunter * end of a FIFO to be repeatedly opened and closed. 1716a8fcbd26SAdrian Hunter */ 1717a8fcbd26SAdrian Hunter fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1718a8fcbd26SAdrian Hunter if (fd < 0) { 1719a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", s); 1720a8fcbd26SAdrian Hunter ret = -errno; 1721a8fcbd26SAdrian Hunter goto out_free; 1722a8fcbd26SAdrian Hunter } 1723a8fcbd26SAdrian Hunter *ctl_fd = fd; 1724a8fcbd26SAdrian Hunter *ctl_fd_close = true; 1725a8fcbd26SAdrian Hunter 1726a8fcbd26SAdrian Hunter if (p && *++p) { 1727a8fcbd26SAdrian Hunter /* O_RDWR | O_NONBLOCK means the other end need not be open */ 1728a8fcbd26SAdrian Hunter fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1729a8fcbd26SAdrian Hunter if (fd < 0) { 1730a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", p); 1731a8fcbd26SAdrian Hunter ret = -errno; 1732a8fcbd26SAdrian Hunter goto out_free; 1733a8fcbd26SAdrian Hunter } 1734a8fcbd26SAdrian Hunter *ctl_fd_ack = fd; 1735a8fcbd26SAdrian Hunter } 1736a8fcbd26SAdrian Hunter 1737a8fcbd26SAdrian Hunter out_free: 1738a8fcbd26SAdrian Hunter free(s); 1739a8fcbd26SAdrian Hunter return ret; 1740a8fcbd26SAdrian Hunter } 1741a8fcbd26SAdrian Hunter 1742a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 17439864a66dSAdrian Hunter { 17449864a66dSAdrian Hunter char *comma = NULL, *endptr = NULL; 17459864a66dSAdrian Hunter 1746a8fcbd26SAdrian Hunter *ctl_fd_close = false; 1747a8fcbd26SAdrian Hunter 17489864a66dSAdrian Hunter if (strncmp(str, "fd:", 3)) 1749a8fcbd26SAdrian Hunter return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close); 17509864a66dSAdrian Hunter 17519864a66dSAdrian Hunter *ctl_fd = strtoul(&str[3], &endptr, 0); 17529864a66dSAdrian Hunter if (endptr == &str[3]) 17539864a66dSAdrian Hunter return -EINVAL; 17549864a66dSAdrian Hunter 17559864a66dSAdrian Hunter comma = strchr(str, ','); 17569864a66dSAdrian Hunter if (comma) { 17579864a66dSAdrian Hunter if (endptr != comma) 17589864a66dSAdrian Hunter return -EINVAL; 17599864a66dSAdrian Hunter 17609864a66dSAdrian Hunter *ctl_fd_ack = strtoul(comma + 1, &endptr, 0); 17619864a66dSAdrian Hunter if (endptr == comma + 1 || *endptr != '\0') 17629864a66dSAdrian Hunter return -EINVAL; 17639864a66dSAdrian Hunter } 17649864a66dSAdrian Hunter 17659864a66dSAdrian Hunter return 0; 17669864a66dSAdrian Hunter } 17679864a66dSAdrian Hunter 1768ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close) 1769ee7fe31eSAdrian Hunter { 1770ee7fe31eSAdrian Hunter if (*ctl_fd_close) { 1771ee7fe31eSAdrian Hunter *ctl_fd_close = false; 1772ee7fe31eSAdrian Hunter close(ctl_fd); 1773ee7fe31eSAdrian Hunter if (ctl_fd_ack >= 0) 1774ee7fe31eSAdrian Hunter close(ctl_fd_ack); 1775ee7fe31eSAdrian Hunter } 1776ee7fe31eSAdrian Hunter } 1777ee7fe31eSAdrian Hunter 1778ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack) 1779ec886bf5SAlexey Budankov { 1780ec886bf5SAlexey Budankov if (fd == -1) { 1781ec886bf5SAlexey Budankov pr_debug("Control descriptor is not initialized\n"); 1782ec886bf5SAlexey Budankov return 0; 1783ec886bf5SAlexey Budankov } 1784ec886bf5SAlexey Budankov 1785ec886bf5SAlexey Budankov evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 1786ec886bf5SAlexey Budankov fdarray_flag__nonfilterable); 1787ec886bf5SAlexey Budankov if (evlist->ctl_fd.pos < 0) { 1788ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1789ec886bf5SAlexey Budankov pr_err("Failed to add ctl fd entry: %m\n"); 1790ec886bf5SAlexey Budankov return -1; 1791ec886bf5SAlexey Budankov } 1792ec886bf5SAlexey Budankov 1793ec886bf5SAlexey Budankov evlist->ctl_fd.fd = fd; 1794ec886bf5SAlexey Budankov evlist->ctl_fd.ack = ack; 1795ec886bf5SAlexey Budankov 1796ec886bf5SAlexey Budankov return 0; 1797ec886bf5SAlexey Budankov } 1798ec886bf5SAlexey Budankov 1799ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist) 1800ec886bf5SAlexey Budankov { 1801ec886bf5SAlexey Budankov return evlist->ctl_fd.pos >= 0; 1802ec886bf5SAlexey Budankov } 1803ec886bf5SAlexey Budankov 1804ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist) 1805ec886bf5SAlexey Budankov { 1806ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 1807ec886bf5SAlexey Budankov 1808ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist)) 1809ec886bf5SAlexey Budankov return 0; 1810ec886bf5SAlexey Budankov 1811ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].fd = -1; 1812ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].events = 0; 1813ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].revents = 0; 1814ec886bf5SAlexey Budankov 1815ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1816ec886bf5SAlexey Budankov evlist->ctl_fd.ack = -1; 1817ec886bf5SAlexey Budankov evlist->ctl_fd.fd = -1; 1818ec886bf5SAlexey Budankov 1819ec886bf5SAlexey Budankov return 0; 1820ec886bf5SAlexey Budankov } 1821ec886bf5SAlexey Budankov 1822ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd, 1823ec886bf5SAlexey Budankov char *cmd_data, size_t data_size) 1824ec886bf5SAlexey Budankov { 1825ec886bf5SAlexey Budankov int err; 1826ec886bf5SAlexey Budankov char c; 1827ec886bf5SAlexey Budankov size_t bytes_read = 0; 1828ec886bf5SAlexey Budankov 182940db8ff5SAdrian Hunter *cmd = EVLIST_CTL_CMD_UNSUPPORTED; 1830ec886bf5SAlexey Budankov memset(cmd_data, 0, data_size); 1831ec886bf5SAlexey Budankov data_size--; 1832ec886bf5SAlexey Budankov 1833ec886bf5SAlexey Budankov do { 1834ec886bf5SAlexey Budankov err = read(evlist->ctl_fd.fd, &c, 1); 1835ec886bf5SAlexey Budankov if (err > 0) { 1836ec886bf5SAlexey Budankov if (c == '\n' || c == '\0') 1837ec886bf5SAlexey Budankov break; 1838ec886bf5SAlexey Budankov cmd_data[bytes_read++] = c; 1839ec886bf5SAlexey Budankov if (bytes_read == data_size) 1840ec886bf5SAlexey Budankov break; 184140db8ff5SAdrian Hunter continue; 184240db8ff5SAdrian Hunter } else if (err == -1) { 184340db8ff5SAdrian Hunter if (errno == EINTR) 184440db8ff5SAdrian Hunter continue; 184540db8ff5SAdrian Hunter if (errno == EAGAIN || errno == EWOULDBLOCK) 184640db8ff5SAdrian Hunter err = 0; 184740db8ff5SAdrian Hunter else 1848ec886bf5SAlexey Budankov pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd); 1849ec886bf5SAlexey Budankov } 185040db8ff5SAdrian Hunter break; 1851ec886bf5SAlexey Budankov } while (1); 1852ec886bf5SAlexey Budankov 1853ec886bf5SAlexey Budankov pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data, 1854ec886bf5SAlexey Budankov bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0"); 1855ec886bf5SAlexey Budankov 185640db8ff5SAdrian Hunter if (bytes_read > 0) { 1857ec886bf5SAlexey Budankov if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG, 1858ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) { 1859ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_ENABLE; 1860ec886bf5SAlexey Budankov } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG, 1861ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) { 1862ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_DISABLE; 1863d20aff15SAdrian Hunter } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG, 1864d20aff15SAdrian Hunter (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) { 1865d20aff15SAdrian Hunter *cmd = EVLIST_CTL_CMD_SNAPSHOT; 1866d20aff15SAdrian Hunter pr_debug("is snapshot\n"); 1867ec886bf5SAlexey Budankov } 1868ec886bf5SAlexey Budankov } 1869ec886bf5SAlexey Budankov 187040db8ff5SAdrian Hunter return bytes_read ? (int)bytes_read : err; 1871ec886bf5SAlexey Budankov } 1872ec886bf5SAlexey Budankov 1873d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist) 1874ec886bf5SAlexey Budankov { 1875ec886bf5SAlexey Budankov int err; 1876ec886bf5SAlexey Budankov 1877ec886bf5SAlexey Budankov if (evlist->ctl_fd.ack == -1) 1878ec886bf5SAlexey Budankov return 0; 1879ec886bf5SAlexey Budankov 1880ec886bf5SAlexey Budankov err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG, 1881ec886bf5SAlexey Budankov sizeof(EVLIST_CTL_CMD_ACK_TAG)); 1882ec886bf5SAlexey Budankov if (err == -1) 1883ec886bf5SAlexey Budankov pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack); 1884ec886bf5SAlexey Budankov 1885ec886bf5SAlexey Budankov return err; 1886ec886bf5SAlexey Budankov } 1887ec886bf5SAlexey Budankov 1888ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd) 1889ec886bf5SAlexey Budankov { 1890ec886bf5SAlexey Budankov int err = 0; 1891ec886bf5SAlexey Budankov char cmd_data[EVLIST_CTL_CMD_MAX_LEN]; 1892ec886bf5SAlexey Budankov int ctlfd_pos = evlist->ctl_fd.pos; 1893ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 1894ec886bf5SAlexey Budankov 1895ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents) 1896ec886bf5SAlexey Budankov return 0; 1897ec886bf5SAlexey Budankov 1898ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & POLLIN) { 1899ec886bf5SAlexey Budankov err = evlist__ctlfd_recv(evlist, cmd, cmd_data, 1900ec886bf5SAlexey Budankov EVLIST_CTL_CMD_MAX_LEN); 1901ec886bf5SAlexey Budankov if (err > 0) { 1902ec886bf5SAlexey Budankov switch (*cmd) { 1903ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ENABLE: 1904ec886bf5SAlexey Budankov evlist__enable(evlist); 1905ec886bf5SAlexey Budankov break; 1906ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_DISABLE: 1907ec886bf5SAlexey Budankov evlist__disable(evlist); 1908ec886bf5SAlexey Budankov break; 1909d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 1910d20aff15SAdrian Hunter break; 1911ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ACK: 1912ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 1913ec886bf5SAlexey Budankov default: 1914ec886bf5SAlexey Budankov pr_debug("ctlfd: unsupported %d\n", *cmd); 1915ec886bf5SAlexey Budankov break; 1916ec886bf5SAlexey Budankov } 1917d20aff15SAdrian Hunter if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED || 1918d20aff15SAdrian Hunter *cmd == EVLIST_CTL_CMD_SNAPSHOT)) 1919ec886bf5SAlexey Budankov evlist__ctlfd_ack(evlist); 1920ec886bf5SAlexey Budankov } 1921ec886bf5SAlexey Budankov } 1922ec886bf5SAlexey Budankov 1923ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR)) 1924ec886bf5SAlexey Budankov evlist__finalize_ctlfd(evlist); 1925ec886bf5SAlexey Budankov else 1926ec886bf5SAlexey Budankov entries[ctlfd_pos].revents = 0; 1927ec886bf5SAlexey Budankov 1928ec886bf5SAlexey Budankov return err; 1929ec886bf5SAlexey Budankov } 1930b214ba8cSNamhyung Kim 1931b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx) 1932b214ba8cSNamhyung Kim { 1933b214ba8cSNamhyung Kim struct evsel *evsel; 1934b214ba8cSNamhyung Kim 1935b214ba8cSNamhyung Kim evlist__for_each_entry(evlist, evsel) { 1936b214ba8cSNamhyung Kim if (evsel->idx == idx) 1937b214ba8cSNamhyung Kim return evsel; 1938b214ba8cSNamhyung Kim } 1939b214ba8cSNamhyung Kim return NULL; 1940b214ba8cSNamhyung Kim } 1941