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 8163503dbaSJiri Olsa struct evlist *perf_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 9363503dbaSJiri Olsa struct evlist *perf_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 18063503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist, 181f114d6efSAdrian Hunter struct list_head *list) 18250d08e47SArnaldo Carvalho de Melo { 18332dcd021SJiri Olsa struct evsel *evsel, *temp; 18475562573SAdrian Hunter 185e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 186b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 187a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 188f114d6efSAdrian Hunter } 18950d08e47SArnaldo Carvalho de Melo } 19050d08e47SArnaldo Carvalho de Melo 191c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist, 192c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs) 193c0e53476SArnaldo Carvalho de Melo { 194c0e53476SArnaldo Carvalho de Melo size_t i; 195c0e53476SArnaldo Carvalho de Melo int err; 196c0e53476SArnaldo Carvalho de Melo 197c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 198c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it. 199b02736f7SArnaldo Carvalho de Melo struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name); 200c0e53476SArnaldo Carvalho de Melo if (evsel == NULL) 201c0e53476SArnaldo Carvalho de Melo continue; 202c0e53476SArnaldo Carvalho de Melo 203c0e53476SArnaldo Carvalho de Melo err = -EEXIST; 204c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL) 205c0e53476SArnaldo Carvalho de Melo goto out; 206c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler; 207c0e53476SArnaldo Carvalho de Melo } 208c0e53476SArnaldo Carvalho de Melo 209c0e53476SArnaldo Carvalho de Melo err = 0; 210c0e53476SArnaldo Carvalho de Melo out: 211c0e53476SArnaldo Carvalho de Melo return err; 212c0e53476SArnaldo Carvalho de Melo } 213c0e53476SArnaldo Carvalho de Melo 214a622eafaSArnaldo Carvalho de Melo void __evlist__set_leader(struct list_head *list) 21563dab225SArnaldo Carvalho de Melo { 21632dcd021SJiri Olsa struct evsel *evsel, *leader; 21763dab225SArnaldo Carvalho de Melo 218b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 219b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 22097f63e4aSNamhyung Kim 2215643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 22263dab225SArnaldo Carvalho de Melo 223e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22463dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22563dab225SArnaldo Carvalho de Melo } 22663dab225SArnaldo Carvalho de Melo } 22763dab225SArnaldo Carvalho de Melo 228a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist) 2296a4bb04cSJiri Olsa { 2306484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2316484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 232a622eafaSArnaldo Carvalho de Melo __evlist__set_leader(&evlist->core.entries); 2336a4bb04cSJiri Olsa } 23497f63e4aSNamhyung Kim } 2356a4bb04cSJiri Olsa 236e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise) 237361c99a6SArnaldo Carvalho de Melo { 2388f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_cycles(precise); 239361c99a6SArnaldo Carvalho de Melo 240361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2417c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 242361c99a6SArnaldo Carvalho de Melo 243a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 244361c99a6SArnaldo Carvalho de Melo return 0; 245361c99a6SArnaldo Carvalho de Melo } 2465c581041SArnaldo Carvalho de Melo 247e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist) 2485bae0250SArnaldo Carvalho de Melo { 2495bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2505bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2515bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2525bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2535bae0250SArnaldo Carvalho de Melo }; 2548f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries); 2555bae0250SArnaldo Carvalho de Melo 2565bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2575bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2585bae0250SArnaldo Carvalho de Melo 259a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2605bae0250SArnaldo Carvalho de Melo return 0; 2615bae0250SArnaldo Carvalho de Melo } 2625bae0250SArnaldo Carvalho de Melo 263e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 26450d08e47SArnaldo Carvalho de Melo { 26532dcd021SJiri Olsa struct evsel *evsel, *n; 26650d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 26750d08e47SArnaldo Carvalho de Melo size_t i; 26850d08e47SArnaldo Carvalho de Melo 26950d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2708f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 27150d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27250d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 273b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 27450d08e47SArnaldo Carvalho de Melo } 27550d08e47SArnaldo Carvalho de Melo 276f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 27750d08e47SArnaldo Carvalho de Melo 27850d08e47SArnaldo Carvalho de Melo return 0; 27950d08e47SArnaldo Carvalho de Melo 28050d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 281e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2825eb2dd2aSJiri Olsa evsel__delete(evsel); 28350d08e47SArnaldo Carvalho de Melo return -1; 28450d08e47SArnaldo Carvalho de Melo } 28550d08e47SArnaldo Carvalho de Melo 286e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 28779695e1bSArnaldo Carvalho de Melo { 28879695e1bSArnaldo Carvalho de Melo size_t i; 28979695e1bSArnaldo Carvalho de Melo 29079695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 29179695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 29279695e1bSArnaldo Carvalho de Melo 293a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 29479695e1bSArnaldo Carvalho de Melo } 29579695e1bSArnaldo Carvalho de Melo 296b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 297ee29be62SArnaldo Carvalho de Melo { 29832dcd021SJiri Olsa struct evsel *evsel; 299ee29be62SArnaldo Carvalho de Melo 300e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3011fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3021fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 303ee29be62SArnaldo Carvalho de Melo return evsel; 304ee29be62SArnaldo Carvalho de Melo } 305ee29be62SArnaldo Carvalho de Melo 306ee29be62SArnaldo Carvalho de Melo return NULL; 307ee29be62SArnaldo Carvalho de Melo } 308ee29be62SArnaldo Carvalho de Melo 309b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name) 310a2f2804aSDavid Ahern { 31132dcd021SJiri Olsa struct evsel *evsel; 312a2f2804aSDavid Ahern 313e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3141fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 315a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 316a2f2804aSDavid Ahern return evsel; 317a2f2804aSDavid Ahern } 318a2f2804aSDavid Ahern 319a2f2804aSDavid Ahern return NULL; 320a2f2804aSDavid Ahern } 321a2f2804aSDavid Ahern 322e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler) 32339876e7dSArnaldo Carvalho de Melo { 3248f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp(sys, name); 32539876e7dSArnaldo Carvalho de Melo 3268dd2a131SJiri Olsa if (IS_ERR(evsel)) 32739876e7dSArnaldo Carvalho de Melo return -1; 32839876e7dSArnaldo Carvalho de Melo 329744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 330a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 33139876e7dSArnaldo Carvalho de Melo return 0; 33239876e7dSArnaldo Carvalho de Melo } 33339876e7dSArnaldo Carvalho de Melo 33463503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist, 33532dcd021SJiri Olsa struct evsel *evsel) 336bf8e8f4bSAdrian Hunter { 337648b5af3SJiri Olsa if (evsel->core.system_wide) 338bf8e8f4bSAdrian Hunter return 1; 339bf8e8f4bSAdrian Hunter else 340a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 341bf8e8f4bSAdrian Hunter } 342bf8e8f4bSAdrian Hunter 343a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist) 344a8cbe40fSAndi Kleen { 345a8cbe40fSAndi Kleen struct evsel *pos; 346a8cbe40fSAndi Kleen 347a8cbe40fSAndi Kleen /* 348a8cbe40fSAndi Kleen * Reset the per evsel cpu_iter. This is needed because 349a8cbe40fSAndi Kleen * each evsel's cpumap may have a different index space, 350a8cbe40fSAndi Kleen * and some operations need the index to modify 351a8cbe40fSAndi Kleen * the FD xyarray (e.g. open, close) 352a8cbe40fSAndi Kleen */ 353a8cbe40fSAndi Kleen evlist__for_each_entry(evlist, pos) 354a8cbe40fSAndi Kleen pos->cpu_iter = 0; 355a8cbe40fSAndi Kleen } 356a8cbe40fSAndi Kleen 357a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu) 358a8cbe40fSAndi Kleen { 359a8cbe40fSAndi Kleen if (ev->cpu_iter >= ev->core.cpus->nr) 360a8cbe40fSAndi Kleen return true; 361a8cbe40fSAndi Kleen if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu) 362a8cbe40fSAndi Kleen return true; 363a8cbe40fSAndi Kleen return false; 364a8cbe40fSAndi Kleen } 365a8cbe40fSAndi Kleen 366a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu) 367a8cbe40fSAndi Kleen { 368a8cbe40fSAndi Kleen if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) { 369a8cbe40fSAndi Kleen ev->cpu_iter++; 370a8cbe40fSAndi Kleen return false; 371a8cbe40fSAndi Kleen } 372a8cbe40fSAndi Kleen return true; 373a8cbe40fSAndi Kleen } 374a8cbe40fSAndi Kleen 375e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3764152ab37SArnaldo Carvalho de Melo { 37732dcd021SJiri Olsa struct evsel *pos; 378704e2f5bSAndi Kleen struct affinity affinity; 37987cf8360SAdrian Hunter int cpu, i, imm = 0; 38087cf8360SAdrian Hunter bool has_imm = false; 381704e2f5bSAndi Kleen 382704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 383704e2f5bSAndi Kleen return; 384704e2f5bSAndi Kleen 38587cf8360SAdrian Hunter /* Disable 'immediate' events last */ 38687cf8360SAdrian Hunter for (imm = 0; imm <= 1; imm++) { 387704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 388704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 3894152ab37SArnaldo Carvalho de Melo 390e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 391704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 392704e2f5bSAndi Kleen continue; 393c754c382SArnaldo Carvalho de Melo if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd) 3943fe4430dSJiri Olsa continue; 39587cf8360SAdrian Hunter if (pos->immediate) 39687cf8360SAdrian Hunter has_imm = true; 39787cf8360SAdrian Hunter if (pos->immediate != imm) 39887cf8360SAdrian Hunter continue; 399704e2f5bSAndi Kleen evsel__disable_cpu(pos, pos->cpu_iter - 1); 400704e2f5bSAndi Kleen } 401704e2f5bSAndi Kleen } 40287cf8360SAdrian Hunter if (!has_imm) 40387cf8360SAdrian Hunter break; 40487cf8360SAdrian Hunter } 40587cf8360SAdrian Hunter 406704e2f5bSAndi Kleen affinity__cleanup(&affinity); 407704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 408c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 409704e2f5bSAndi Kleen continue; 410704e2f5bSAndi Kleen pos->disabled = true; 4114152ab37SArnaldo Carvalho de Melo } 4122b56bcfbSArnaldo Carvalho de Melo 4132b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 4144152ab37SArnaldo Carvalho de Melo } 4154152ab37SArnaldo Carvalho de Melo 4161c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 417764e16a3SDavid Ahern { 41832dcd021SJiri Olsa struct evsel *pos; 419704e2f5bSAndi Kleen struct affinity affinity; 420704e2f5bSAndi Kleen int cpu, i; 421764e16a3SDavid Ahern 422704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 423704e2f5bSAndi Kleen return; 424704e2f5bSAndi Kleen 425704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 426704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 427704e2f5bSAndi Kleen 428704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 429704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 430704e2f5bSAndi Kleen continue; 431c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 432704e2f5bSAndi Kleen continue; 433704e2f5bSAndi Kleen evsel__enable_cpu(pos, pos->cpu_iter - 1); 434704e2f5bSAndi Kleen } 435704e2f5bSAndi Kleen } 436704e2f5bSAndi Kleen affinity__cleanup(&affinity); 437e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 438c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 4393fe4430dSJiri Olsa continue; 440704e2f5bSAndi Kleen pos->disabled = false; 441764e16a3SDavid Ahern } 4422b56bcfbSArnaldo Carvalho de Melo 4432b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 4442b56bcfbSArnaldo Carvalho de Melo } 4452b56bcfbSArnaldo Carvalho de Melo 446ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist) 4472b56bcfbSArnaldo Carvalho de Melo { 448e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 449764e16a3SDavid Ahern } 450764e16a3SDavid Ahern 45137b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu) 4521c65056cSAdrian Hunter { 45318ef15c6SArnaldo Carvalho de Melo int thread; 4541c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 4551c65056cSAdrian Hunter 4569dfcb759SJiri Olsa if (!evsel->core.fd) 4571c65056cSAdrian Hunter return -EINVAL; 4581c65056cSAdrian Hunter 4591c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 46018ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4611c65056cSAdrian Hunter if (err) 4621c65056cSAdrian Hunter return err; 4631c65056cSAdrian Hunter } 4641c65056cSAdrian Hunter return 0; 4651c65056cSAdrian Hunter } 4661c65056cSAdrian Hunter 46737b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread) 4681c65056cSAdrian Hunter { 46918ef15c6SArnaldo Carvalho de Melo int cpu; 4706549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 4711c65056cSAdrian Hunter 4729dfcb759SJiri Olsa if (!evsel->core.fd) 4731c65056cSAdrian Hunter return -EINVAL; 4741c65056cSAdrian Hunter 4751c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 47618ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4771c65056cSAdrian Hunter if (err) 4781c65056cSAdrian Hunter return err; 4791c65056cSAdrian Hunter } 4801c65056cSAdrian Hunter return 0; 4811c65056cSAdrian Hunter } 4821c65056cSAdrian Hunter 48337b01abeSArnaldo Carvalho de Melo int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx) 4841c65056cSAdrian Hunter { 485315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 4861c65056cSAdrian Hunter 4871c65056cSAdrian Hunter if (per_cpu_mmaps) 48837b01abeSArnaldo Carvalho de Melo return evlist__enable_event_cpu(evlist, evsel, idx); 48937b01abeSArnaldo Carvalho de Melo 49037b01abeSArnaldo Carvalho de Melo return evlist__enable_event_thread(evlist, evsel, idx); 4911c65056cSAdrian Hunter } 4921c65056cSAdrian Hunter 493f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 494e4b356b5SArnaldo Carvalho de Melo { 495ab4c1f9fSAlexey Budankov return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default); 496e4b356b5SArnaldo Carvalho de Melo } 4971b85337dSArnaldo Carvalho de Melo 498f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 4991ddec7f0SArnaldo Carvalho de Melo { 50084227cb1SJiri Olsa return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 5011ddec7f0SArnaldo Carvalho de Melo } 5021ddec7f0SArnaldo Carvalho de Melo 50380ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 504f66a889dSArnaldo Carvalho de Melo { 50580ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 506f66a889dSArnaldo Carvalho de Melo } 507f66a889dSArnaldo Carvalho de Melo 5083ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id) 50970db7533SArnaldo Carvalho de Melo { 51070db7533SArnaldo Carvalho de Melo struct hlist_head *head; 51170db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 51270db7533SArnaldo Carvalho de Melo int hash; 51370db7533SArnaldo Carvalho de Melo 51470db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5151d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 51670db7533SArnaldo Carvalho de Melo 517b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 51870db7533SArnaldo Carvalho de Melo if (sid->id == id) 519932a3594SJiri Olsa return sid; 520932a3594SJiri Olsa 521932a3594SJiri Olsa return NULL; 522932a3594SJiri Olsa } 523932a3594SJiri Olsa 5243ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id) 525932a3594SJiri Olsa { 526932a3594SJiri Olsa struct perf_sample_id *sid; 527932a3594SJiri Olsa 5286484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 529515dbe48SJiri Olsa return evlist__first(evlist); 530932a3594SJiri Olsa 5313ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id); 532932a3594SJiri Olsa if (sid) 53370c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 53430e68bccSNamhyung Kim 5358cedf3a5SArnaldo Carvalho de Melo if (!evlist__sample_id_all(evlist)) 536515dbe48SJiri Olsa return evlist__first(evlist); 53730e68bccSNamhyung Kim 53870db7533SArnaldo Carvalho de Melo return NULL; 53970db7533SArnaldo Carvalho de Melo } 54004391debSArnaldo Carvalho de Melo 5413ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id) 542dddcf6abSAdrian Hunter { 543dddcf6abSAdrian Hunter struct perf_sample_id *sid; 544dddcf6abSAdrian Hunter 545dddcf6abSAdrian Hunter if (!id) 546dddcf6abSAdrian Hunter return NULL; 547dddcf6abSAdrian Hunter 5483ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id); 549dddcf6abSAdrian Hunter if (sid) 55070c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 551dddcf6abSAdrian Hunter 552dddcf6abSAdrian Hunter return NULL; 553dddcf6abSAdrian Hunter } 554dddcf6abSAdrian Hunter 5553ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id) 55675562573SAdrian Hunter { 557b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 55875562573SAdrian Hunter ssize_t n; 55975562573SAdrian Hunter 56075562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 56175562573SAdrian Hunter 56275562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 56375562573SAdrian Hunter if (evlist->id_pos >= n) 56475562573SAdrian Hunter return -1; 56575562573SAdrian Hunter *id = array[evlist->id_pos]; 56675562573SAdrian Hunter } else { 56775562573SAdrian Hunter if (evlist->is_pos > n) 56875562573SAdrian Hunter return -1; 56975562573SAdrian Hunter n -= evlist->is_pos; 57075562573SAdrian Hunter *id = array[n]; 57175562573SAdrian Hunter } 57275562573SAdrian Hunter return 0; 57375562573SAdrian Hunter } 57475562573SAdrian Hunter 5753ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event) 57675562573SAdrian Hunter { 577515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 57875562573SAdrian Hunter struct hlist_head *head; 57975562573SAdrian Hunter struct perf_sample_id *sid; 58075562573SAdrian Hunter int hash; 58175562573SAdrian Hunter u64 id; 58275562573SAdrian Hunter 5836484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 58498be6966SAdrian Hunter return first; 58598be6966SAdrian Hunter 5861fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 58798be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 58898be6966SAdrian Hunter return first; 58975562573SAdrian Hunter 5903ccf8a7bSArnaldo Carvalho de Melo if (evlist__event2id(evlist, event, &id)) 59175562573SAdrian Hunter return NULL; 59275562573SAdrian Hunter 59375562573SAdrian Hunter /* Synthesized events have an id of zero */ 59475562573SAdrian Hunter if (!id) 59598be6966SAdrian Hunter return first; 59675562573SAdrian Hunter 59775562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5981d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 59975562573SAdrian Hunter 60075562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 60175562573SAdrian Hunter if (sid->id == id) 60270c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 60375562573SAdrian Hunter } 60475562573SAdrian Hunter return NULL; 60575562573SAdrian Hunter } 60675562573SAdrian Hunter 607*0a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value) 60865aea233SWang Nan { 60965aea233SWang Nan int i; 61065aea233SWang Nan 6110b72d69aSWang Nan if (!evlist->overwrite_mmap) 612078c3386SWang Nan return 0; 613078c3386SWang Nan 614c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6152cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 61665aea233SWang Nan int err; 61765aea233SWang Nan 61865aea233SWang Nan if (fd < 0) 61965aea233SWang Nan continue; 62065aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 62165aea233SWang Nan if (err) 62265aea233SWang Nan return err; 62365aea233SWang Nan } 62465aea233SWang Nan return 0; 62565aea233SWang Nan } 62665aea233SWang Nan 627*0a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist) 62865aea233SWang Nan { 629*0a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, true); 63065aea233SWang Nan } 63165aea233SWang Nan 632*0a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist) 63365aea233SWang Nan { 634*0a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, false); 63565aea233SWang Nan } 63665aea233SWang Nan 637db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 638f8a95309SArnaldo Carvalho de Melo { 639aece948fSArnaldo Carvalho de Melo int i; 640f8a95309SArnaldo Carvalho de Melo 641b2cb615dSWang Nan if (evlist->mmap) 642c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 64380e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 644b2cb615dSWang Nan 6450b72d69aSWang Nan if (evlist->overwrite_mmap) 646c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 64780e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 648a1f72618SWang Nan } 649aece948fSArnaldo Carvalho de Melo 650db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 651a1f72618SWang Nan { 652db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 65304662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6540b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 655f8a95309SArnaldo Carvalho de Melo } 656f8a95309SArnaldo Carvalho de Melo 65780e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 65880e53d11SJiri Olsa { 65980e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 66080e53d11SJiri Olsa 66180e53d11SJiri Olsa mmap__munmap(m); 66280e53d11SJiri Olsa } 66380e53d11SJiri Olsa 664d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 6652c5f6d87SKan Liang bool overwrite) 666f8a95309SArnaldo Carvalho de Melo { 667d4c6fb36SWang Nan int i; 668a5830532SJiri Olsa struct mmap *map; 669d4c6fb36SWang Nan 670c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 6718db6d6b1SWang Nan if (!map) 6728db6d6b1SWang Nan return NULL; 673946ae1d4SWang Nan 674c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6756eb65f7aSJiri Olsa struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 6766eb65f7aSJiri Olsa 6774738ca30SArnaldo Carvalho de Melo /* 6784738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 6796afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 6804738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 6814738ca30SArnaldo Carvalho de Melo * dropped. 6824738ca30SArnaldo Carvalho de Melo * 6834738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 6844738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 6854738ca30SArnaldo Carvalho de Melo */ 6866eb65f7aSJiri Olsa perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 6874738ca30SArnaldo Carvalho de Melo } 688353120b4SJiri Olsa 6898db6d6b1SWang Nan return map; 690f8a95309SArnaldo Carvalho de Melo } 691f8a95309SArnaldo Carvalho de Melo 6929abd2ab2SJiri Olsa static void 6939abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 6949abd2ab2SJiri Olsa struct perf_mmap_param *_mp, 6959abd2ab2SJiri Olsa int idx, bool per_cpu) 6969abd2ab2SJiri Olsa { 6979abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 6989abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 6999abd2ab2SJiri Olsa 7009abd2ab2SJiri Olsa auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 7019abd2ab2SJiri Olsa } 7029abd2ab2SJiri Olsa 703bb1b1885SJiri Olsa static struct perf_mmap* 704bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 705bb1b1885SJiri Olsa { 706bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7073805e4f3SJiri Olsa struct mmap *maps; 708bb1b1885SJiri Olsa 7093805e4f3SJiri Olsa maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 710bb1b1885SJiri Olsa 711bb1b1885SJiri Olsa if (!maps) { 7123805e4f3SJiri Olsa maps = evlist__alloc_mmap(evlist, overwrite); 713bb1b1885SJiri Olsa if (!maps) 714bb1b1885SJiri Olsa return NULL; 715bb1b1885SJiri Olsa 7163805e4f3SJiri Olsa if (overwrite) { 717bb1b1885SJiri Olsa evlist->overwrite_mmap = maps; 718bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 719ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 7203805e4f3SJiri Olsa } else { 7213805e4f3SJiri Olsa evlist->mmap = maps; 722bb1b1885SJiri Olsa } 723bb1b1885SJiri Olsa } 724bb1b1885SJiri Olsa 725bb1b1885SJiri Olsa return &maps[idx].core; 726bb1b1885SJiri Olsa } 727bb1b1885SJiri Olsa 728b80132b1SJiri Olsa static int 729b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 730b80132b1SJiri Olsa int output, int cpu) 731b80132b1SJiri Olsa { 732b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core); 733b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 734b80132b1SJiri Olsa 735b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu); 736b80132b1SJiri Olsa } 737b80132b1SJiri Olsa 738f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 739994a1f78SJiri Olsa { 740f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 7418185e881SArnaldo Carvalho de Melo int max; 7428185e881SArnaldo Carvalho de Melo 7438185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 7448185e881SArnaldo Carvalho de Melo /* 7458185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 7468185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 7478185e881SArnaldo Carvalho de Melo * die yet... 7488185e881SArnaldo Carvalho de Melo */ 7498185e881SArnaldo Carvalho de Melo max = 512; 7508185e881SArnaldo Carvalho de Melo } else { 7518185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 7528185e881SArnaldo Carvalho de Melo } 7538185e881SArnaldo Carvalho de Melo 7548185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 7551be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 7561be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 757f5e7150cSArnaldo Carvalho de Melo 758f5e7150cSArnaldo Carvalho de Melo return pages; 759f5e7150cSArnaldo Carvalho de Melo } 760f5e7150cSArnaldo Carvalho de Melo 7619521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 762f5e7150cSArnaldo Carvalho de Melo { 763f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 764f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 765f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 766994a1f78SJiri Olsa return 0; 767994a1f78SJiri Olsa 768994a1f78SJiri Olsa return (pages + 1) * page_size; 769994a1f78SJiri Olsa } 770994a1f78SJiri Olsa 77133c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 77233c2dcfdSDavid Ahern unsigned long max) 773994a1f78SJiri Olsa { 7742fbe4abeSAdrian Hunter unsigned long pages, val; 77527050f53SJiri Olsa static struct parse_tag tags[] = { 77627050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 77727050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 77827050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 77927050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 78027050f53SJiri Olsa { .tag = 0 }, 78127050f53SJiri Olsa }; 782994a1f78SJiri Olsa 7838973504bSDavid Ahern if (str == NULL) 78433c2dcfdSDavid Ahern return -EINVAL; 7858973504bSDavid Ahern 78627050f53SJiri Olsa val = parse_tag_value(str, tags); 7872fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 78827050f53SJiri Olsa /* we got file size value */ 78927050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 79027050f53SJiri Olsa } else { 79127050f53SJiri Olsa /* we got pages count value */ 79227050f53SJiri Olsa char *eptr; 793994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 79433c2dcfdSDavid Ahern if (*eptr != '\0') 79533c2dcfdSDavid Ahern return -EINVAL; 79627050f53SJiri Olsa } 797994a1f78SJiri Olsa 7982bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 79933c2dcfdSDavid Ahern /* leave number of pages at 0 */ 8001dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 8019808143bSJiri Olsa char buf[100]; 8029808143bSJiri Olsa 80333c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 80491529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 8051dbfa938SAdrian Hunter if (!pages) 8061dbfa938SAdrian Hunter return -EINVAL; 8079808143bSJiri Olsa 8089808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 8099808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 8109808143bSJiri Olsa buf, pages); 8112fbe4abeSAdrian Hunter } 8122fbe4abeSAdrian Hunter 81333c2dcfdSDavid Ahern if (pages > max) 81433c2dcfdSDavid Ahern return -EINVAL; 81533c2dcfdSDavid Ahern 81633c2dcfdSDavid Ahern return pages; 81733c2dcfdSDavid Ahern } 81833c2dcfdSDavid Ahern 819e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 82033c2dcfdSDavid Ahern { 82133c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 82233c2dcfdSDavid Ahern long pages; 82333c2dcfdSDavid Ahern 824f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 82533c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 82633c2dcfdSDavid Ahern 82733c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 82833c2dcfdSDavid Ahern if (pages < 0) { 82933c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 830994a1f78SJiri Olsa return -1; 831994a1f78SJiri Olsa } 832994a1f78SJiri Olsa 833994a1f78SJiri Olsa *mmap_pages = pages; 834994a1f78SJiri Olsa return 0; 835994a1f78SJiri Olsa } 836994a1f78SJiri Olsa 837e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 838e9db1310SAdrian Hunter int unset __maybe_unused) 839e9db1310SAdrian Hunter { 840e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 841e9db1310SAdrian Hunter } 842e9db1310SAdrian Hunter 843c83fa7f2SAdrian Hunter /** 8449521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 845c83fa7f2SAdrian Hunter * @evlist: list of events 846c83fa7f2SAdrian Hunter * @pages: map length in pages 847c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 848718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 849718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 850f8a95309SArnaldo Carvalho de Melo * 851c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 8529521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 853c83fa7f2SAdrian Hunter * automatically. 854f8a95309SArnaldo Carvalho de Melo * 855718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 856718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 857718c602dSAdrian Hunter * 858c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 859f8a95309SArnaldo Carvalho de Melo */ 8609521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 8617a276ff6SWang Nan unsigned int auxtrace_pages, 86251255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 86351255a8aSAlexey Budankov int comp_level) 864f8a95309SArnaldo Carvalho de Melo { 86571f566a3SWang Nan /* 86671f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 86771f566a3SWang Nan * Its value is decided by evsel's write_backward. 86871f566a3SWang Nan * So &mp should not be passed through const pointer. 86971f566a3SWang Nan */ 870e440979fSJiri Olsa struct mmap_params mp = { 871e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 872e440979fSJiri Olsa .affinity = affinity, 873e440979fSJiri Olsa .flush = flush, 874e440979fSJiri Olsa .comp_level = comp_level 875e440979fSJiri Olsa }; 876923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = { 8779abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx, 878bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get, 879b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap, 8809abd2ab2SJiri Olsa }; 88150a682ceSArnaldo Carvalho de Melo 882f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 883f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 884f8a95309SArnaldo Carvalho de Melo 885f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 886718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 887718c602dSAdrian Hunter 888923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 889f8a95309SArnaldo Carvalho de Melo } 8907e2ed097SArnaldo Carvalho de Melo 8919521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 892718c602dSAdrian Hunter { 8939521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 894718c602dSAdrian Hunter } 895718c602dSAdrian Hunter 89663503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 8977e2ed097SArnaldo Carvalho de Melo { 898147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 899f854839bSJiri Olsa struct perf_cpu_map *cpus; 9009749b90eSJiri Olsa struct perf_thread_map *threads; 9017e2ed097SArnaldo Carvalho de Melo 902147c508fSJin Yao /* 903147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 904147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 905147c508fSJin Yao * target->system_wide = true. 906147c508fSJin Yao * 907147c508fSJin Yao * If specify '--per-thread' only to perf record, 908147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 909147c508fSJin Yao * 910147c508fSJin Yao * So target->per_thread && target->system_wide is false. 911147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 912147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 913147c508fSJin Yao * current behavior. 914147c508fSJin Yao * 915147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 916147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 917147c508fSJin Yao * per-thread data. thread_map__new_str will call 918147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 919147c508fSJin Yao */ 92073c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 921147c508fSJin Yao all_threads); 92274bfd2b2SAdrian Hunter 92374bfd2b2SAdrian Hunter if (!threads) 9247e2ed097SArnaldo Carvalho de Melo return -1; 9257e2ed097SArnaldo Carvalho de Melo 9269c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 927397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 928879d77d0SNamhyung Kim else 9299c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 9307e2ed097SArnaldo Carvalho de Melo 93174bfd2b2SAdrian Hunter if (!cpus) 9327e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 9337e2ed097SArnaldo Carvalho de Melo 934ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 935ec9a77a7SAdrian Hunter 936453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 937d5bc056eSAdrian Hunter 938bfd1b83dSNamhyung Kim /* as evlist now has references, put count here */ 939bfd1b83dSNamhyung Kim perf_cpu_map__put(cpus); 940bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 941bfd1b83dSNamhyung Kim 942d5bc056eSAdrian Hunter return 0; 9437e2ed097SArnaldo Carvalho de Melo 9447e2ed097SArnaldo Carvalho de Melo out_delete_threads: 9457836e52eSJiri Olsa perf_thread_map__put(threads); 9467e2ed097SArnaldo Carvalho de Melo return -1; 9477e2ed097SArnaldo Carvalho de Melo } 9487e2ed097SArnaldo Carvalho de Melo 94924bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 9500a102479SFrederic Weisbecker { 95132dcd021SJiri Olsa struct evsel *evsel; 952745cefc5SArnaldo Carvalho de Melo int err = 0; 9530a102479SFrederic Weisbecker 954e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 955745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 9560a102479SFrederic Weisbecker continue; 957745cefc5SArnaldo Carvalho de Melo 958d988d5eeSKan Liang /* 959d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 960d988d5eeSKan Liang * So evlist and evsel should always be same. 961d988d5eeSKan Liang */ 962a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 96323d4aad4SArnaldo Carvalho de Melo if (err) { 96423d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 965745cefc5SArnaldo Carvalho de Melo break; 9660a102479SFrederic Weisbecker } 96723d4aad4SArnaldo Carvalho de Melo } 9680a102479SFrederic Weisbecker 969745cefc5SArnaldo Carvalho de Melo return err; 970745cefc5SArnaldo Carvalho de Melo } 971745cefc5SArnaldo Carvalho de Melo 97224bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter) 973745cefc5SArnaldo Carvalho de Melo { 97432dcd021SJiri Olsa struct evsel *evsel; 975745cefc5SArnaldo Carvalho de Melo int err = 0; 976745cefc5SArnaldo Carvalho de Melo 97705cea449SArnaldo Carvalho de Melo if (filter == NULL) 97805cea449SArnaldo Carvalho de Melo return -1; 97905cea449SArnaldo Carvalho de Melo 980e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 9811fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 982fdf14720SWang Nan continue; 983fdf14720SWang Nan 984ad681adfSArnaldo Carvalho de Melo err = evsel__set_filter(evsel, filter); 985745cefc5SArnaldo Carvalho de Melo if (err) 986745cefc5SArnaldo Carvalho de Melo break; 987745cefc5SArnaldo Carvalho de Melo } 988745cefc5SArnaldo Carvalho de Melo 989745cefc5SArnaldo Carvalho de Melo return err; 9900a102479SFrederic Weisbecker } 99174429964SFrederic Weisbecker 99224bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter) 99353c92f73SArnaldo Carvalho de Melo { 99453c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 99553c92f73SArnaldo Carvalho de Melo int err = 0; 99653c92f73SArnaldo Carvalho de Melo 99753c92f73SArnaldo Carvalho de Melo if (filter == NULL) 99853c92f73SArnaldo Carvalho de Melo return -1; 99953c92f73SArnaldo Carvalho de Melo 100053c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 100153c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 100253c92f73SArnaldo Carvalho de Melo continue; 100353c92f73SArnaldo Carvalho de Melo 1004ad681adfSArnaldo Carvalho de Melo err = evsel__append_tp_filter(evsel, filter); 100553c92f73SArnaldo Carvalho de Melo if (err) 100653c92f73SArnaldo Carvalho de Melo break; 100753c92f73SArnaldo Carvalho de Melo } 100853c92f73SArnaldo Carvalho de Melo 100953c92f73SArnaldo Carvalho de Melo return err; 101053c92f73SArnaldo Carvalho de Melo } 101153c92f73SArnaldo Carvalho de Melo 1012da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1013cfd70a26SArnaldo Carvalho de Melo { 1014cfd70a26SArnaldo Carvalho de Melo char *filter; 1015be199adaSArnaldo Carvalho de Melo size_t i; 1016cfd70a26SArnaldo Carvalho de Melo 1017be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1018be199adaSArnaldo Carvalho de Melo if (i == 0) { 1019be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 102005cea449SArnaldo Carvalho de Melo return NULL; 1021be199adaSArnaldo Carvalho de Melo } else { 1022be199adaSArnaldo Carvalho de Melo char *tmp; 1023be199adaSArnaldo Carvalho de Melo 1024be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1025be199adaSArnaldo Carvalho de Melo goto out_free; 1026be199adaSArnaldo Carvalho de Melo 1027be199adaSArnaldo Carvalho de Melo free(filter); 1028be199adaSArnaldo Carvalho de Melo filter = tmp; 1029be199adaSArnaldo Carvalho de Melo } 1030be199adaSArnaldo Carvalho de Melo } 1031cfd70a26SArnaldo Carvalho de Melo 103205cea449SArnaldo Carvalho de Melo return filter; 1033be199adaSArnaldo Carvalho de Melo out_free: 1034cfd70a26SArnaldo Carvalho de Melo free(filter); 103505cea449SArnaldo Carvalho de Melo return NULL; 103605cea449SArnaldo Carvalho de Melo } 103705cea449SArnaldo Carvalho de Melo 103824bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 103905cea449SArnaldo Carvalho de Melo { 104005cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 104124bf91a7SArnaldo Carvalho de Melo int ret = evlist__set_tp_filter(evlist, filter); 104205cea449SArnaldo Carvalho de Melo 104305cea449SArnaldo Carvalho de Melo free(filter); 1044cfd70a26SArnaldo Carvalho de Melo return ret; 1045cfd70a26SArnaldo Carvalho de Melo } 1046cfd70a26SArnaldo Carvalho de Melo 104724bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1048be199adaSArnaldo Carvalho de Melo { 104924bf91a7SArnaldo Carvalho de Melo return evlist__set_tp_filter_pids(evlist, 1, &pid); 1050be199adaSArnaldo Carvalho de Melo } 1051be199adaSArnaldo Carvalho de Melo 105224bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 10531827ab5bSArnaldo Carvalho de Melo { 10541827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 105524bf91a7SArnaldo Carvalho de Melo int ret = evlist__append_tp_filter(evlist, filter); 10561827ab5bSArnaldo Carvalho de Melo 10571827ab5bSArnaldo Carvalho de Melo free(filter); 10581827ab5bSArnaldo Carvalho de Melo return ret; 10591827ab5bSArnaldo Carvalho de Melo } 10601827ab5bSArnaldo Carvalho de Melo 106124bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 10621827ab5bSArnaldo Carvalho de Melo { 106324bf91a7SArnaldo Carvalho de Melo return evlist__append_tp_filter_pids(evlist, 1, &pid); 10641827ab5bSArnaldo Carvalho de Melo } 10651827ab5bSArnaldo Carvalho de Melo 1066b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist) 106774429964SFrederic Weisbecker { 106832dcd021SJiri Olsa struct evsel *pos; 1069c2a70653SArnaldo Carvalho de Melo 10706484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 107175562573SAdrian Hunter return true; 107275562573SAdrian Hunter 107375562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 107475562573SAdrian Hunter return false; 107575562573SAdrian Hunter 1076e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 107775562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 107875562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1079c2a70653SArnaldo Carvalho de Melo return false; 108074429964SFrederic Weisbecker } 108174429964SFrederic Weisbecker 1082c2a70653SArnaldo Carvalho de Melo return true; 1083c2a70653SArnaldo Carvalho de Melo } 1084c2a70653SArnaldo Carvalho de Melo 1085b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist) 1086c2a70653SArnaldo Carvalho de Melo { 108732dcd021SJiri Olsa struct evsel *evsel; 108875562573SAdrian Hunter 108975562573SAdrian Hunter if (evlist->combined_sample_type) 109075562573SAdrian Hunter return evlist->combined_sample_type; 109175562573SAdrian Hunter 1092e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 10931fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 109475562573SAdrian Hunter 109575562573SAdrian Hunter return evlist->combined_sample_type; 109675562573SAdrian Hunter } 109775562573SAdrian Hunter 1098b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist) 109975562573SAdrian Hunter { 110075562573SAdrian Hunter evlist->combined_sample_type = 0; 1101b3c2cc2bSArnaldo Carvalho de Melo return __evlist__combined_sample_type(evlist); 1102c2a70653SArnaldo Carvalho de Melo } 1103c2a70653SArnaldo Carvalho de Melo 110492c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist) 110598df858eSAndi Kleen { 110632dcd021SJiri Olsa struct evsel *evsel; 110798df858eSAndi Kleen u64 branch_type = 0; 110898df858eSAndi Kleen 1109e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11101fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 111198df858eSAndi Kleen return branch_type; 111298df858eSAndi Kleen } 111398df858eSAndi Kleen 111463503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 11159ede473cSJiri Olsa { 1116515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 11171fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 11181fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 11199ede473cSJiri Olsa 1120e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 112194d3820fSAdrian Hunter if (read_format != pos->core.attr.read_format) { 112294d3820fSAdrian Hunter pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n", 112394d3820fSAdrian Hunter read_format, (u64)pos->core.attr.read_format); 112494d3820fSAdrian Hunter } 11259ede473cSJiri Olsa } 11269ede473cSJiri Olsa 11279ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 11289ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 11299ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 11309ede473cSJiri Olsa return false; 11319ede473cSJiri Olsa } 11329ede473cSJiri Olsa 11339ede473cSJiri Olsa return true; 11349ede473cSJiri Olsa } 11359ede473cSJiri Olsa 113663503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 113781e36bffSArnaldo Carvalho de Melo { 1138515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 113981e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 114081e36bffSArnaldo Carvalho de Melo u64 sample_type; 114181e36bffSArnaldo Carvalho de Melo u16 size = 0; 114281e36bffSArnaldo Carvalho de Melo 11431fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 114481e36bffSArnaldo Carvalho de Melo goto out; 114581e36bffSArnaldo Carvalho de Melo 11461fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 114781e36bffSArnaldo Carvalho de Melo 114881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 114981e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 115081e36bffSArnaldo Carvalho de Melo 115181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 115281e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 115381e36bffSArnaldo Carvalho de Melo 115481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 115581e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 115681e36bffSArnaldo Carvalho de Melo 115781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 115881e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 115981e36bffSArnaldo Carvalho de Melo 116081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 116181e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 116275562573SAdrian Hunter 116375562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 116475562573SAdrian Hunter size += sizeof(data->id); 116581e36bffSArnaldo Carvalho de Melo out: 116681e36bffSArnaldo Carvalho de Melo return size; 116781e36bffSArnaldo Carvalho de Melo } 116881e36bffSArnaldo Carvalho de Melo 11698cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist) 1170c2a70653SArnaldo Carvalho de Melo { 1171515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1172c2a70653SArnaldo Carvalho de Melo 1173e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 11741fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1175c2a70653SArnaldo Carvalho de Melo return false; 1176c2a70653SArnaldo Carvalho de Melo } 1177c2a70653SArnaldo Carvalho de Melo 1178c2a70653SArnaldo Carvalho de Melo return true; 117974429964SFrederic Weisbecker } 118074429964SFrederic Weisbecker 11818cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist) 118274429964SFrederic Weisbecker { 1183515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11841fc632ceSJiri Olsa return first->core.attr.sample_id_all; 118574429964SFrederic Weisbecker } 118681cce8deSArnaldo Carvalho de Melo 118763503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 118832dcd021SJiri Olsa struct evsel *evsel) 118981cce8deSArnaldo Carvalho de Melo { 119081cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 119181cce8deSArnaldo Carvalho de Melo } 1192727ab04eSArnaldo Carvalho de Melo 1193750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1194a74b4b66SNamhyung Kim { 119532dcd021SJiri Olsa struct evsel *evsel; 11967736627bSAndi Kleen struct affinity affinity; 11977736627bSAndi Kleen int cpu, i; 1198a74b4b66SNamhyung Kim 11997736627bSAndi Kleen /* 12007736627bSAndi Kleen * With perf record core.cpus is usually NULL. 12017736627bSAndi Kleen * Use the old method to handle this for now. 12027736627bSAndi Kleen */ 12037736627bSAndi Kleen if (!evlist->core.cpus) { 1204475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 120588761fa1SJiri Olsa evsel__close(evsel); 12067736627bSAndi Kleen return; 12077736627bSAndi Kleen } 12087736627bSAndi Kleen 12097736627bSAndi Kleen if (affinity__setup(&affinity) < 0) 12107736627bSAndi Kleen return; 12117736627bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 12127736627bSAndi Kleen affinity__set(&affinity, cpu); 12137736627bSAndi Kleen 12147736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12157736627bSAndi Kleen if (evsel__cpu_iter_skip(evsel, cpu)) 12167736627bSAndi Kleen continue; 12177736627bSAndi Kleen perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1); 12187736627bSAndi Kleen } 12197736627bSAndi Kleen } 12207736627bSAndi Kleen affinity__cleanup(&affinity); 12217736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12227736627bSAndi Kleen perf_evsel__free_fd(&evsel->core); 12237736627bSAndi Kleen perf_evsel__free_id(&evsel->core); 12247736627bSAndi Kleen } 1225a74b4b66SNamhyung Kim } 1226a74b4b66SNamhyung Kim 122763503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 12284112eb18SArnaldo Carvalho de Melo { 1229f854839bSJiri Olsa struct perf_cpu_map *cpus; 12309749b90eSJiri Olsa struct perf_thread_map *threads; 12314112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 12324112eb18SArnaldo Carvalho de Melo 12334112eb18SArnaldo Carvalho de Melo /* 12344112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 12354112eb18SArnaldo Carvalho de Melo * an all cpus map. 12364112eb18SArnaldo Carvalho de Melo * 12374112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 12384112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 12394112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 12404112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 12414112eb18SArnaldo Carvalho de Melo */ 12429c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 12438c0498b6SAdrian Hunter if (!cpus) 12444112eb18SArnaldo Carvalho de Melo goto out; 12454112eb18SArnaldo Carvalho de Melo 12464b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 12478c0498b6SAdrian Hunter if (!threads) 12488c0498b6SAdrian Hunter goto out_put; 12494112eb18SArnaldo Carvalho de Melo 1250453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1251bfd1b83dSNamhyung Kim 1252bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 12538c0498b6SAdrian Hunter out_put: 125438f01d8dSJiri Olsa perf_cpu_map__put(cpus); 1255bfd1b83dSNamhyung Kim out: 1256bfd1b83dSNamhyung Kim return err; 12574112eb18SArnaldo Carvalho de Melo } 12584112eb18SArnaldo Carvalho de Melo 1259474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1260727ab04eSArnaldo Carvalho de Melo { 126132dcd021SJiri Olsa struct evsel *evsel; 1262a74b4b66SNamhyung Kim int err; 1263727ab04eSArnaldo Carvalho de Melo 12644112eb18SArnaldo Carvalho de Melo /* 12654112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 12664112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 12674112eb18SArnaldo Carvalho de Melo */ 126803617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 12694112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 12704112eb18SArnaldo Carvalho de Melo if (err < 0) 12714112eb18SArnaldo Carvalho de Melo goto out_err; 12724112eb18SArnaldo Carvalho de Melo } 12734112eb18SArnaldo Carvalho de Melo 12740a7e7ec9SArnaldo Carvalho de Melo evlist__update_id_pos(evlist); 1275733cd2feSAdrian Hunter 1276e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1277af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1278727ab04eSArnaldo Carvalho de Melo if (err < 0) 1279727ab04eSArnaldo Carvalho de Melo goto out_err; 1280727ab04eSArnaldo Carvalho de Melo } 1281727ab04eSArnaldo Carvalho de Melo 1282727ab04eSArnaldo Carvalho de Melo return 0; 1283727ab04eSArnaldo Carvalho de Melo out_err: 1284750b4edeSJiri Olsa evlist__close(evlist); 128541c21a68SNamhyung Kim errno = -err; 1286727ab04eSArnaldo Carvalho de Melo return err; 1287727ab04eSArnaldo Carvalho de Melo } 128835b9d88eSArnaldo Carvalho de Melo 12897b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[], 12907b392ef0SArnaldo Carvalho de Melo bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 129135b9d88eSArnaldo Carvalho de Melo { 129235b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 129335b9d88eSArnaldo Carvalho de Melo char bf; 129435b9d88eSArnaldo Carvalho de Melo 129535b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 129635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 129735b9d88eSArnaldo Carvalho de Melo return -1; 129835b9d88eSArnaldo Carvalho de Melo } 129935b9d88eSArnaldo Carvalho de Melo 130035b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 130135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 130235b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 130335b9d88eSArnaldo Carvalho de Melo } 130435b9d88eSArnaldo Carvalho de Melo 130535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 130635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 130735b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 130835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 130935b9d88eSArnaldo Carvalho de Melo } 131035b9d88eSArnaldo Carvalho de Melo 131135b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13125f1c4225SArnaldo Carvalho de Melo int ret; 13135f1c4225SArnaldo Carvalho de Melo 1314119fa3c9SNamhyung Kim if (pipe_output) 131535b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 131635b9d88eSArnaldo Carvalho de Melo 13170817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13180817df08SDavid Ahern 131935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 132035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 132135b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 132235b9d88eSArnaldo Carvalho de Melo 132335b9d88eSArnaldo Carvalho de Melo /* 132435b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 132535b9d88eSArnaldo Carvalho de Melo */ 132635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 132735b9d88eSArnaldo Carvalho de Melo 132835b9d88eSArnaldo Carvalho de Melo /* 132935b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 133035b9d88eSArnaldo Carvalho de Melo */ 13315f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 13325f1c4225SArnaldo Carvalho de Melo /* 13335f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 13345f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 13357b392ef0SArnaldo Carvalho de Melo * evlist__start_workload(). 13365f1c4225SArnaldo Carvalho de Melo * 133720f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 13385f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 13395f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 13405f1c4225SArnaldo Carvalho de Melo * here. 13415f1c4225SArnaldo Carvalho de Melo */ 13425f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 13435f1c4225SArnaldo Carvalho de Melo if (ret == -1) 134435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 13455f1c4225SArnaldo Carvalho de Melo exit(ret); 13465f1c4225SArnaldo Carvalho de Melo } 134735b9d88eSArnaldo Carvalho de Melo 134835b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 134935b9d88eSArnaldo Carvalho de Melo 1350735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1351f33cbe72SArnaldo Carvalho de Melo union sigval val; 1352f33cbe72SArnaldo Carvalho de Melo 1353f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1354f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 135535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1356f33cbe72SArnaldo Carvalho de Melo } else 1357f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 135835b9d88eSArnaldo Carvalho de Melo exit(-1); 135935b9d88eSArnaldo Carvalho de Melo } 136035b9d88eSArnaldo Carvalho de Melo 1361735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1362735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1363735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1364735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1365735f7e0bSArnaldo Carvalho de Melo }; 1366735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1367735f7e0bSArnaldo Carvalho de Melo } 1368735f7e0bSArnaldo Carvalho de Melo 13691aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 137003617c22SJiri Olsa if (evlist->core.threads == NULL) { 13711aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 13721aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 13731aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 13741aaf63b1SArnaldo Carvalho de Melo } 137503617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 13761aaf63b1SArnaldo Carvalho de Melo } 137735b9d88eSArnaldo Carvalho de Melo 137835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 137935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 138035b9d88eSArnaldo Carvalho de Melo /* 138135b9d88eSArnaldo Carvalho de Melo * wait for child to settle 138235b9d88eSArnaldo Carvalho de Melo */ 138335b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 138435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 138535b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 138635b9d88eSArnaldo Carvalho de Melo } 138735b9d88eSArnaldo Carvalho de Melo 1388bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 138935b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 139035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 139135b9d88eSArnaldo Carvalho de Melo return 0; 139235b9d88eSArnaldo Carvalho de Melo 139335b9d88eSArnaldo Carvalho de Melo out_close_pipes: 139435b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 139535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 139635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 139735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 139835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 139935b9d88eSArnaldo Carvalho de Melo return -1; 140035b9d88eSArnaldo Carvalho de Melo } 140135b9d88eSArnaldo Carvalho de Melo 14027b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist) 140335b9d88eSArnaldo Carvalho de Melo { 140435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1405b3824404SDavid Ahern char bf = 0; 1406bcf3145fSNamhyung Kim int ret; 140735b9d88eSArnaldo Carvalho de Melo /* 140835b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 140935b9d88eSArnaldo Carvalho de Melo */ 1410bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1411bcf3145fSNamhyung Kim if (ret < 0) 1412e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1413bcf3145fSNamhyung Kim 1414bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1415bcf3145fSNamhyung Kim return ret; 141635b9d88eSArnaldo Carvalho de Melo } 141735b9d88eSArnaldo Carvalho de Melo 141835b9d88eSArnaldo Carvalho de Melo return 0; 141935b9d88eSArnaldo Carvalho de Melo } 1420cb0b29e0SArnaldo Carvalho de Melo 14212a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample) 1422cb0b29e0SArnaldo Carvalho de Melo { 14233ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event); 142475562573SAdrian Hunter 142575562573SAdrian Hunter if (!evsel) 142675562573SAdrian Hunter return -EFAULT; 14276b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample(evsel, event, sample); 1428cb0b29e0SArnaldo Carvalho de Melo } 142978f067b3SArnaldo Carvalho de Melo 14302a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp) 143101468120SJiri Olsa { 14323ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event); 143301468120SJiri Olsa 143401468120SJiri Olsa if (!evsel) 143501468120SJiri Olsa return -EFAULT; 14366b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample_timestamp(evsel, event, timestamp); 143701468120SJiri Olsa } 143801468120SJiri Olsa 1439d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size) 1440a8f23d8fSArnaldo Carvalho de Melo { 1441a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1442c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1443a8f23d8fSArnaldo Carvalho de Melo 1444a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1445a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1446a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1447a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1448a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1449a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1450a8f23d8fSArnaldo Carvalho de Melo 14511a47245dSAdrian Hunter value = perf_event_paranoid(); 1452a8f23d8fSArnaldo Carvalho de Melo 1453a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1454a8f23d8fSArnaldo Carvalho de Melo 1455a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1456a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1457a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1458a8f23d8fSArnaldo Carvalho de Melo } 1459a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14605229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1461a8f23d8fSArnaldo Carvalho de Melo 1462a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14635229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 14645229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1465a8f23d8fSArnaldo Carvalho de Melo break; 1466d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1467515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1468d9aade7fSArnaldo Carvalho de Melo int max_freq; 1469d9aade7fSArnaldo Carvalho de Melo 1470d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1471d9aade7fSArnaldo Carvalho de Melo goto out_default; 1472d9aade7fSArnaldo Carvalho de Melo 14731fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1474d9aade7fSArnaldo Carvalho de Melo goto out_default; 1475d9aade7fSArnaldo Carvalho de Melo 1476d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1477d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1478d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1479d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 14801fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1481d9aade7fSArnaldo Carvalho de Melo break; 1482d9aade7fSArnaldo Carvalho de Melo } 1483a8f23d8fSArnaldo Carvalho de Melo default: 1484d9aade7fSArnaldo Carvalho de Melo out_default: 1485a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1486a8f23d8fSArnaldo Carvalho de Melo break; 1487a8f23d8fSArnaldo Carvalho de Melo } 1488a8f23d8fSArnaldo Carvalho de Melo 1489a8f23d8fSArnaldo Carvalho de Melo return 0; 1490a8f23d8fSArnaldo Carvalho de Melo } 1491a025e4f0SAdrian Hunter 1492d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1493956fa571SArnaldo Carvalho de Melo { 1494c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1495f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1496956fa571SArnaldo Carvalho de Melo 1497956fa571SArnaldo Carvalho de Melo switch (err) { 1498956fa571SArnaldo Carvalho de Melo case EPERM: 1499e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1500e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1501e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1502956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1503e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1504e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1505e965bea1SArnaldo Carvalho de Melo 1506e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1507e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1508e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1509e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1510e965bea1SArnaldo Carvalho de Melo } 1511e965bea1SArnaldo Carvalho de Melo 1512e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1513e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1514956fa571SArnaldo Carvalho de Melo break; 1515956fa571SArnaldo Carvalho de Melo default: 1516956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1517956fa571SArnaldo Carvalho de Melo break; 1518956fa571SArnaldo Carvalho de Melo } 1519956fa571SArnaldo Carvalho de Melo 1520956fa571SArnaldo Carvalho de Melo return 0; 1521956fa571SArnaldo Carvalho de Melo } 1522956fa571SArnaldo Carvalho de Melo 152363503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 152432dcd021SJiri Olsa struct evsel *move_evsel) 1525a025e4f0SAdrian Hunter { 152632dcd021SJiri Olsa struct evsel *evsel, *n; 1527a025e4f0SAdrian Hunter LIST_HEAD(move); 1528a025e4f0SAdrian Hunter 1529515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1530a025e4f0SAdrian Hunter return; 1531a025e4f0SAdrian Hunter 1532e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1533a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1534b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1535a025e4f0SAdrian Hunter } 1536a025e4f0SAdrian Hunter 1537ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1538a025e4f0SAdrian Hunter } 153960b0896cSAdrian Hunter 1540e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist) 1541442ad225SAdrian Hunter { 1542442ad225SAdrian Hunter struct evsel *evsel; 1543442ad225SAdrian Hunter 1544442ad225SAdrian Hunter evlist__for_each_entry(evlist, evsel) { 1545442ad225SAdrian Hunter if (evsel->tracking) 1546442ad225SAdrian Hunter return evsel; 1547442ad225SAdrian Hunter } 1548442ad225SAdrian Hunter 1549442ad225SAdrian Hunter return evlist__first(evlist); 1550442ad225SAdrian Hunter } 1551442ad225SAdrian Hunter 1552e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel) 155360b0896cSAdrian Hunter { 155432dcd021SJiri Olsa struct evsel *evsel; 155560b0896cSAdrian Hunter 155660b0896cSAdrian Hunter if (tracking_evsel->tracking) 155760b0896cSAdrian Hunter return; 155860b0896cSAdrian Hunter 1559e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 156060b0896cSAdrian Hunter if (evsel != tracking_evsel) 156160b0896cSAdrian Hunter evsel->tracking = false; 156260b0896cSAdrian Hunter } 156360b0896cSAdrian Hunter 156460b0896cSAdrian Hunter tracking_evsel->tracking = true; 156560b0896cSAdrian Hunter } 15667630b3e2SWang Nan 1567b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str) 15687630b3e2SWang Nan { 156932dcd021SJiri Olsa struct evsel *evsel; 15707630b3e2SWang Nan 1571e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 15727630b3e2SWang Nan if (!evsel->name) 15737630b3e2SWang Nan continue; 15747630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 15757630b3e2SWang Nan return evsel; 15767630b3e2SWang Nan } 15777630b3e2SWang Nan 15787630b3e2SWang Nan return NULL; 15797630b3e2SWang Nan } 158054cc54deSWang Nan 1581ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state) 158254cc54deSWang Nan { 158354cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 158454cc54deSWang Nan enum action { 158554cc54deSWang Nan NONE, 158654cc54deSWang Nan PAUSE, 158754cc54deSWang Nan RESUME, 158854cc54deSWang Nan } action = NONE; 158954cc54deSWang Nan 15900b72d69aSWang Nan if (!evlist->overwrite_mmap) 159154cc54deSWang Nan return; 159254cc54deSWang Nan 159354cc54deSWang Nan switch (old_state) { 159454cc54deSWang Nan case BKW_MMAP_NOTREADY: { 159554cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1596dd8bd53aSLuis de Bethencourt goto state_err; 159754cc54deSWang Nan break; 159854cc54deSWang Nan } 159954cc54deSWang Nan case BKW_MMAP_RUNNING: { 160054cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 160154cc54deSWang Nan goto state_err; 160254cc54deSWang Nan action = PAUSE; 160354cc54deSWang Nan break; 160454cc54deSWang Nan } 160554cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 160654cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 160754cc54deSWang Nan goto state_err; 160854cc54deSWang Nan break; 160954cc54deSWang Nan } 161054cc54deSWang Nan case BKW_MMAP_EMPTY: { 161154cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 161254cc54deSWang Nan goto state_err; 161354cc54deSWang Nan action = RESUME; 161454cc54deSWang Nan break; 161554cc54deSWang Nan } 161654cc54deSWang Nan default: 161754cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 161854cc54deSWang Nan } 161954cc54deSWang Nan 162054cc54deSWang Nan evlist->bkw_mmap_state = state; 162154cc54deSWang Nan 162254cc54deSWang Nan switch (action) { 162354cc54deSWang Nan case PAUSE: 1624*0a60b339SArnaldo Carvalho de Melo evlist__pause(evlist); 162554cc54deSWang Nan break; 162654cc54deSWang Nan case RESUME: 1627*0a60b339SArnaldo Carvalho de Melo evlist__resume(evlist); 162854cc54deSWang Nan break; 162954cc54deSWang Nan case NONE: 163054cc54deSWang Nan default: 163154cc54deSWang Nan break; 163254cc54deSWang Nan } 163354cc54deSWang Nan 163454cc54deSWang Nan state_err: 163554cc54deSWang Nan return; 163654cc54deSWang Nan } 163707d6f446SArnaldo Carvalho de Melo 163863503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 163907d6f446SArnaldo Carvalho de Melo { 164032dcd021SJiri Olsa struct evsel *evsel; 164107d6f446SArnaldo Carvalho de Melo 164207d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16431fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 164407d6f446SArnaldo Carvalho de Melo return false; 164507d6f446SArnaldo Carvalho de Melo } 164607d6f446SArnaldo Carvalho de Melo 164707d6f446SArnaldo Carvalho de Melo return true; 164807d6f446SArnaldo Carvalho de Melo } 1649e2bdbe80SJin Yao 1650e2bdbe80SJin Yao /* 1651e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1652e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1653e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1654e2bdbe80SJin Yao */ 165563503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1656e2bdbe80SJin Yao { 1657e2bdbe80SJin Yao if (!evlist->nr_groups) { 1658515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1659e2bdbe80SJin Yao 1660a622eafaSArnaldo Carvalho de Melo evlist__set_leader(evlist); 1661e2bdbe80SJin Yao leader->forced_leader = true; 1662e2bdbe80SJin Yao } 1663e2bdbe80SJin Yao } 1664c3537fc2SAndi Kleen 166563503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 16664804e011SAndi Kleen struct evsel *evsel, 16674804e011SAndi Kleen bool close) 1668c3537fc2SAndi Kleen { 166932dcd021SJiri Olsa struct evsel *c2, *leader; 1670c3537fc2SAndi Kleen bool is_open = true; 1671c3537fc2SAndi Kleen 1672c3537fc2SAndi Kleen leader = evsel->leader; 1673c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 16745643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1675c3537fc2SAndi Kleen 1676c3537fc2SAndi Kleen /* 1677c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1678c3537fc2SAndi Kleen * include the first entry. 1679c3537fc2SAndi Kleen */ 1680c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1681c3537fc2SAndi Kleen if (c2 == evsel) 1682c3537fc2SAndi Kleen is_open = false; 1683c3537fc2SAndi Kleen if (c2->leader == leader) { 16844804e011SAndi Kleen if (is_open && close) 16855a40e199SAndi Kleen perf_evsel__close(&c2->core); 1686c3537fc2SAndi Kleen c2->leader = c2; 16875643b1a5SJiri Olsa c2->core.nr_members = 0; 16884804e011SAndi Kleen /* 16894804e011SAndi Kleen * Set this for all former members of the group 16904804e011SAndi Kleen * to indicate they get reopened. 16914804e011SAndi Kleen */ 16924804e011SAndi Kleen c2->reset_group = true; 1693c3537fc2SAndi Kleen } 1694c3537fc2SAndi Kleen } 1695c3537fc2SAndi Kleen return leader; 1696c3537fc2SAndi Kleen } 1697ec886bf5SAlexey Budankov 1698a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1699a8fcbd26SAdrian Hunter { 1700a8fcbd26SAdrian Hunter char *s, *p; 1701a8fcbd26SAdrian Hunter int ret = 0, fd; 1702a8fcbd26SAdrian Hunter 1703a8fcbd26SAdrian Hunter if (strncmp(str, "fifo:", 5)) 1704a8fcbd26SAdrian Hunter return -EINVAL; 1705a8fcbd26SAdrian Hunter 1706a8fcbd26SAdrian Hunter str += 5; 1707a8fcbd26SAdrian Hunter if (!*str || *str == ',') 1708a8fcbd26SAdrian Hunter return -EINVAL; 1709a8fcbd26SAdrian Hunter 1710a8fcbd26SAdrian Hunter s = strdup(str); 1711a8fcbd26SAdrian Hunter if (!s) 1712a8fcbd26SAdrian Hunter return -ENOMEM; 1713a8fcbd26SAdrian Hunter 1714a8fcbd26SAdrian Hunter p = strchr(s, ','); 1715a8fcbd26SAdrian Hunter if (p) 1716a8fcbd26SAdrian Hunter *p = '\0'; 1717a8fcbd26SAdrian Hunter 1718a8fcbd26SAdrian Hunter /* 1719a8fcbd26SAdrian Hunter * O_RDWR avoids POLLHUPs which is necessary to allow the other 1720a8fcbd26SAdrian Hunter * end of a FIFO to be repeatedly opened and closed. 1721a8fcbd26SAdrian Hunter */ 1722a8fcbd26SAdrian Hunter fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1723a8fcbd26SAdrian Hunter if (fd < 0) { 1724a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", s); 1725a8fcbd26SAdrian Hunter ret = -errno; 1726a8fcbd26SAdrian Hunter goto out_free; 1727a8fcbd26SAdrian Hunter } 1728a8fcbd26SAdrian Hunter *ctl_fd = fd; 1729a8fcbd26SAdrian Hunter *ctl_fd_close = true; 1730a8fcbd26SAdrian Hunter 1731a8fcbd26SAdrian Hunter if (p && *++p) { 1732a8fcbd26SAdrian Hunter /* O_RDWR | O_NONBLOCK means the other end need not be open */ 1733a8fcbd26SAdrian Hunter fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1734a8fcbd26SAdrian Hunter if (fd < 0) { 1735a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", p); 1736a8fcbd26SAdrian Hunter ret = -errno; 1737a8fcbd26SAdrian Hunter goto out_free; 1738a8fcbd26SAdrian Hunter } 1739a8fcbd26SAdrian Hunter *ctl_fd_ack = fd; 1740a8fcbd26SAdrian Hunter } 1741a8fcbd26SAdrian Hunter 1742a8fcbd26SAdrian Hunter out_free: 1743a8fcbd26SAdrian Hunter free(s); 1744a8fcbd26SAdrian Hunter return ret; 1745a8fcbd26SAdrian Hunter } 1746a8fcbd26SAdrian Hunter 1747a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 17489864a66dSAdrian Hunter { 17499864a66dSAdrian Hunter char *comma = NULL, *endptr = NULL; 17509864a66dSAdrian Hunter 1751a8fcbd26SAdrian Hunter *ctl_fd_close = false; 1752a8fcbd26SAdrian Hunter 17539864a66dSAdrian Hunter if (strncmp(str, "fd:", 3)) 1754a8fcbd26SAdrian Hunter return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close); 17559864a66dSAdrian Hunter 17569864a66dSAdrian Hunter *ctl_fd = strtoul(&str[3], &endptr, 0); 17579864a66dSAdrian Hunter if (endptr == &str[3]) 17589864a66dSAdrian Hunter return -EINVAL; 17599864a66dSAdrian Hunter 17609864a66dSAdrian Hunter comma = strchr(str, ','); 17619864a66dSAdrian Hunter if (comma) { 17629864a66dSAdrian Hunter if (endptr != comma) 17639864a66dSAdrian Hunter return -EINVAL; 17649864a66dSAdrian Hunter 17659864a66dSAdrian Hunter *ctl_fd_ack = strtoul(comma + 1, &endptr, 0); 17669864a66dSAdrian Hunter if (endptr == comma + 1 || *endptr != '\0') 17679864a66dSAdrian Hunter return -EINVAL; 17689864a66dSAdrian Hunter } 17699864a66dSAdrian Hunter 17709864a66dSAdrian Hunter return 0; 17719864a66dSAdrian Hunter } 17729864a66dSAdrian Hunter 1773ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close) 1774ee7fe31eSAdrian Hunter { 1775ee7fe31eSAdrian Hunter if (*ctl_fd_close) { 1776ee7fe31eSAdrian Hunter *ctl_fd_close = false; 1777ee7fe31eSAdrian Hunter close(ctl_fd); 1778ee7fe31eSAdrian Hunter if (ctl_fd_ack >= 0) 1779ee7fe31eSAdrian Hunter close(ctl_fd_ack); 1780ee7fe31eSAdrian Hunter } 1781ee7fe31eSAdrian Hunter } 1782ee7fe31eSAdrian Hunter 1783ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack) 1784ec886bf5SAlexey Budankov { 1785ec886bf5SAlexey Budankov if (fd == -1) { 1786ec886bf5SAlexey Budankov pr_debug("Control descriptor is not initialized\n"); 1787ec886bf5SAlexey Budankov return 0; 1788ec886bf5SAlexey Budankov } 1789ec886bf5SAlexey Budankov 1790ec886bf5SAlexey Budankov evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 1791ec886bf5SAlexey Budankov fdarray_flag__nonfilterable); 1792ec886bf5SAlexey Budankov if (evlist->ctl_fd.pos < 0) { 1793ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1794ec886bf5SAlexey Budankov pr_err("Failed to add ctl fd entry: %m\n"); 1795ec886bf5SAlexey Budankov return -1; 1796ec886bf5SAlexey Budankov } 1797ec886bf5SAlexey Budankov 1798ec886bf5SAlexey Budankov evlist->ctl_fd.fd = fd; 1799ec886bf5SAlexey Budankov evlist->ctl_fd.ack = ack; 1800ec886bf5SAlexey Budankov 1801ec886bf5SAlexey Budankov return 0; 1802ec886bf5SAlexey Budankov } 1803ec886bf5SAlexey Budankov 1804ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist) 1805ec886bf5SAlexey Budankov { 1806ec886bf5SAlexey Budankov return evlist->ctl_fd.pos >= 0; 1807ec886bf5SAlexey Budankov } 1808ec886bf5SAlexey Budankov 1809ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist) 1810ec886bf5SAlexey Budankov { 1811ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 1812ec886bf5SAlexey Budankov 1813ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist)) 1814ec886bf5SAlexey Budankov return 0; 1815ec886bf5SAlexey Budankov 1816ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].fd = -1; 1817ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].events = 0; 1818ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].revents = 0; 1819ec886bf5SAlexey Budankov 1820ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1821ec886bf5SAlexey Budankov evlist->ctl_fd.ack = -1; 1822ec886bf5SAlexey Budankov evlist->ctl_fd.fd = -1; 1823ec886bf5SAlexey Budankov 1824ec886bf5SAlexey Budankov return 0; 1825ec886bf5SAlexey Budankov } 1826ec886bf5SAlexey Budankov 1827ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd, 1828ec886bf5SAlexey Budankov char *cmd_data, size_t data_size) 1829ec886bf5SAlexey Budankov { 1830ec886bf5SAlexey Budankov int err; 1831ec886bf5SAlexey Budankov char c; 1832ec886bf5SAlexey Budankov size_t bytes_read = 0; 1833ec886bf5SAlexey Budankov 183440db8ff5SAdrian Hunter *cmd = EVLIST_CTL_CMD_UNSUPPORTED; 1835ec886bf5SAlexey Budankov memset(cmd_data, 0, data_size); 1836ec886bf5SAlexey Budankov data_size--; 1837ec886bf5SAlexey Budankov 1838ec886bf5SAlexey Budankov do { 1839ec886bf5SAlexey Budankov err = read(evlist->ctl_fd.fd, &c, 1); 1840ec886bf5SAlexey Budankov if (err > 0) { 1841ec886bf5SAlexey Budankov if (c == '\n' || c == '\0') 1842ec886bf5SAlexey Budankov break; 1843ec886bf5SAlexey Budankov cmd_data[bytes_read++] = c; 1844ec886bf5SAlexey Budankov if (bytes_read == data_size) 1845ec886bf5SAlexey Budankov break; 184640db8ff5SAdrian Hunter continue; 184740db8ff5SAdrian Hunter } else if (err == -1) { 184840db8ff5SAdrian Hunter if (errno == EINTR) 184940db8ff5SAdrian Hunter continue; 185040db8ff5SAdrian Hunter if (errno == EAGAIN || errno == EWOULDBLOCK) 185140db8ff5SAdrian Hunter err = 0; 185240db8ff5SAdrian Hunter else 1853ec886bf5SAlexey Budankov pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd); 1854ec886bf5SAlexey Budankov } 185540db8ff5SAdrian Hunter break; 1856ec886bf5SAlexey Budankov } while (1); 1857ec886bf5SAlexey Budankov 1858ec886bf5SAlexey Budankov pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data, 1859ec886bf5SAlexey Budankov bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0"); 1860ec886bf5SAlexey Budankov 186140db8ff5SAdrian Hunter if (bytes_read > 0) { 1862ec886bf5SAlexey Budankov if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG, 1863ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) { 1864ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_ENABLE; 1865ec886bf5SAlexey Budankov } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG, 1866ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) { 1867ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_DISABLE; 1868d20aff15SAdrian Hunter } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG, 1869d20aff15SAdrian Hunter (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) { 1870d20aff15SAdrian Hunter *cmd = EVLIST_CTL_CMD_SNAPSHOT; 1871d20aff15SAdrian Hunter pr_debug("is snapshot\n"); 1872ec886bf5SAlexey Budankov } 1873ec886bf5SAlexey Budankov } 1874ec886bf5SAlexey Budankov 187540db8ff5SAdrian Hunter return bytes_read ? (int)bytes_read : err; 1876ec886bf5SAlexey Budankov } 1877ec886bf5SAlexey Budankov 1878d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist) 1879ec886bf5SAlexey Budankov { 1880ec886bf5SAlexey Budankov int err; 1881ec886bf5SAlexey Budankov 1882ec886bf5SAlexey Budankov if (evlist->ctl_fd.ack == -1) 1883ec886bf5SAlexey Budankov return 0; 1884ec886bf5SAlexey Budankov 1885ec886bf5SAlexey Budankov err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG, 1886ec886bf5SAlexey Budankov sizeof(EVLIST_CTL_CMD_ACK_TAG)); 1887ec886bf5SAlexey Budankov if (err == -1) 1888ec886bf5SAlexey Budankov pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack); 1889ec886bf5SAlexey Budankov 1890ec886bf5SAlexey Budankov return err; 1891ec886bf5SAlexey Budankov } 1892ec886bf5SAlexey Budankov 1893ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd) 1894ec886bf5SAlexey Budankov { 1895ec886bf5SAlexey Budankov int err = 0; 1896ec886bf5SAlexey Budankov char cmd_data[EVLIST_CTL_CMD_MAX_LEN]; 1897ec886bf5SAlexey Budankov int ctlfd_pos = evlist->ctl_fd.pos; 1898ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 1899ec886bf5SAlexey Budankov 1900ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents) 1901ec886bf5SAlexey Budankov return 0; 1902ec886bf5SAlexey Budankov 1903ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & POLLIN) { 1904ec886bf5SAlexey Budankov err = evlist__ctlfd_recv(evlist, cmd, cmd_data, 1905ec886bf5SAlexey Budankov EVLIST_CTL_CMD_MAX_LEN); 1906ec886bf5SAlexey Budankov if (err > 0) { 1907ec886bf5SAlexey Budankov switch (*cmd) { 1908ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ENABLE: 1909ec886bf5SAlexey Budankov evlist__enable(evlist); 1910ec886bf5SAlexey Budankov break; 1911ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_DISABLE: 1912ec886bf5SAlexey Budankov evlist__disable(evlist); 1913ec886bf5SAlexey Budankov break; 1914d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 1915d20aff15SAdrian Hunter break; 1916ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ACK: 1917ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 1918ec886bf5SAlexey Budankov default: 1919ec886bf5SAlexey Budankov pr_debug("ctlfd: unsupported %d\n", *cmd); 1920ec886bf5SAlexey Budankov break; 1921ec886bf5SAlexey Budankov } 1922d20aff15SAdrian Hunter if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED || 1923d20aff15SAdrian Hunter *cmd == EVLIST_CTL_CMD_SNAPSHOT)) 1924ec886bf5SAlexey Budankov evlist__ctlfd_ack(evlist); 1925ec886bf5SAlexey Budankov } 1926ec886bf5SAlexey Budankov } 1927ec886bf5SAlexey Budankov 1928ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR)) 1929ec886bf5SAlexey Budankov evlist__finalize_ctlfd(evlist); 1930ec886bf5SAlexey Budankov else 1931ec886bf5SAlexey Budankov entries[ctlfd_pos].revents = 0; 1932ec886bf5SAlexey Budankov 1933ec886bf5SAlexey Budankov return err; 1934ec886bf5SAlexey Budankov } 1935b214ba8cSNamhyung Kim 1936b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx) 1937b214ba8cSNamhyung Kim { 1938b214ba8cSNamhyung Kim struct evsel *evsel; 1939b214ba8cSNamhyung Kim 1940b214ba8cSNamhyung Kim evlist__for_each_entry(evlist, evsel) { 1941b214ba8cSNamhyung Kim if (evsel->idx == idx) 1942b214ba8cSNamhyung Kim return evsel; 1943b214ba8cSNamhyung Kim } 1944b214ba8cSNamhyung Kim return NULL; 1945b214ba8cSNamhyung Kim } 1946