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" 27142544a9SJiri Olsa #include "util/evsel_fprintf.h" 289607ad3aSArnaldo Carvalho de Melo #include <signal.h> 2935b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 30b397f846SArnaldo Carvalho de Melo #include <sched.h> 31f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h> 32361c99a6SArnaldo Carvalho de Melo 3350d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 344b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 3550d08e47SArnaldo Carvalho de Melo 36bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 3786a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 38f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 39f8a95309SArnaldo Carvalho de Melo 4070db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 4170db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 420389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 438dd2a131SJiri Olsa #include <linux/err.h> 448520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 457f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 464562a739SJiri Olsa #include <perf/evlist.h> 4788761fa1SJiri Olsa #include <perf/evsel.h> 489c3516d1SJiri Olsa #include <perf/cpumap.h> 497728fa0cSJiri Olsa #include <perf/mmap.h> 5070db7533SArnaldo Carvalho de Melo 51e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h> 52e14e5497SArnaldo Carvalho de Melo 53748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 54748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 55748fe088SArnaldo Carvalho de Melo #endif 56748fe088SArnaldo Carvalho de Melo 579dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 588cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 59f8a95309SArnaldo Carvalho de Melo 6052c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 619749b90eSJiri Olsa struct perf_thread_map *threads) 62361c99a6SArnaldo Carvalho de Melo { 634562a739SJiri Olsa perf_evlist__init(&evlist->core); 64453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 6535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 6654cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 678ab705b5SAlexey Budankov evlist->ctl_fd.fd = -1; 688ab705b5SAlexey Budankov evlist->ctl_fd.ack = -1; 698ab705b5SAlexey Budankov evlist->ctl_fd.pos = -1; 70361c99a6SArnaldo Carvalho de Melo } 71361c99a6SArnaldo Carvalho de Melo 720f98b11cSJiri Olsa struct evlist *evlist__new(void) 73ef1d1af2SArnaldo Carvalho de Melo { 7463503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist)); 75ef1d1af2SArnaldo Carvalho de Melo 76ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 7752c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL); 78ef1d1af2SArnaldo Carvalho de Melo 79361c99a6SArnaldo Carvalho de Melo return evlist; 80361c99a6SArnaldo Carvalho de Melo } 81361c99a6SArnaldo Carvalho de Melo 82606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void) 83b22d54b0SJiri Olsa { 840f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 85b22d54b0SJiri Olsa 86e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_default(evlist)) { 87c12995a5SJiri Olsa evlist__delete(evlist); 88b22d54b0SJiri Olsa evlist = NULL; 89b22d54b0SJiri Olsa } 90b22d54b0SJiri Olsa 91b22d54b0SJiri Olsa return evlist; 92b22d54b0SJiri Olsa } 93b22d54b0SJiri Olsa 94606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void) 955bae0250SArnaldo Carvalho de Melo { 960f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 975bae0250SArnaldo Carvalho de Melo 98e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_dummy(evlist)) { 99c12995a5SJiri Olsa evlist__delete(evlist); 1005bae0250SArnaldo Carvalho de Melo evlist = NULL; 1015bae0250SArnaldo Carvalho de Melo } 1025bae0250SArnaldo Carvalho de Melo 1035bae0250SArnaldo Carvalho de Melo return evlist; 1045bae0250SArnaldo Carvalho de Melo } 1055bae0250SArnaldo Carvalho de Melo 10675562573SAdrian Hunter /** 1070a7e7ec9SArnaldo Carvalho de Melo * evlist__set_id_pos - set the positions of event ids. 10875562573SAdrian Hunter * @evlist: selected event list 10975562573SAdrian Hunter * 11075562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 11175562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 11275562573SAdrian Hunter */ 1130a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist) 11475562573SAdrian Hunter { 115515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11675562573SAdrian Hunter 11775562573SAdrian Hunter evlist->id_pos = first->id_pos; 11875562573SAdrian Hunter evlist->is_pos = first->is_pos; 11975562573SAdrian Hunter } 12075562573SAdrian Hunter 1210a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist) 122733cd2feSAdrian Hunter { 12332dcd021SJiri Olsa struct evsel *evsel; 124733cd2feSAdrian Hunter 125e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 1264b5e87b7SArnaldo Carvalho de Melo evsel__calc_id_pos(evsel); 127733cd2feSAdrian Hunter 1280a7e7ec9SArnaldo Carvalho de Melo evlist__set_id_pos(evlist); 129733cd2feSAdrian Hunter } 130733cd2feSAdrian Hunter 131e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist) 132361c99a6SArnaldo Carvalho de Melo { 13332dcd021SJiri Olsa struct evsel *pos, *n; 134361c99a6SArnaldo Carvalho de Melo 135e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 136b27c4eceSJiri Olsa list_del_init(&pos->core.node); 137d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 1385eb2dd2aSJiri Olsa evsel__delete(pos); 139361c99a6SArnaldo Carvalho de Melo } 140361c99a6SArnaldo Carvalho de Melo 1416484d2f9SJiri Olsa evlist->core.nr_entries = 0; 142361c99a6SArnaldo Carvalho de Melo } 143361c99a6SArnaldo Carvalho de Melo 144470579b0SJiri Olsa void evlist__exit(struct evlist *evlist) 145ef1d1af2SArnaldo Carvalho de Melo { 14604662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1470b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 14893dd6e28SJiri Olsa perf_evlist__exit(&evlist->core); 149ef1d1af2SArnaldo Carvalho de Melo } 150ef1d1af2SArnaldo Carvalho de Melo 151c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist) 152361c99a6SArnaldo Carvalho de Melo { 1530b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1540b04b3dcSArnaldo Carvalho de Melo return; 1550b04b3dcSArnaldo Carvalho de Melo 156db6b7b13SJiri Olsa evlist__munmap(evlist); 157750b4edeSJiri Olsa evlist__close(evlist); 158e6b1878dSJiri Olsa evlist__purge(evlist); 159470579b0SJiri Olsa evlist__exit(evlist); 160361c99a6SArnaldo Carvalho de Melo free(evlist); 161361c99a6SArnaldo Carvalho de Melo } 162361c99a6SArnaldo Carvalho de Melo 163a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 164361c99a6SArnaldo Carvalho de Melo { 165d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1666484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 16760b0896cSAdrian Hunter entry->tracking = !entry->idx; 168ef503831SArnaldo Carvalho de Melo 1696484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1706484d2f9SJiri Olsa 1716484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 1720a7e7ec9SArnaldo Carvalho de Melo evlist__set_id_pos(evlist); 173361c99a6SArnaldo Carvalho de Melo } 174361c99a6SArnaldo Carvalho de Melo 17516251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1764768230aSAdrian Hunter { 1774768230aSAdrian Hunter evsel->evlist = NULL; 17852e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1794768230aSAdrian Hunter } 1804768230aSAdrian Hunter 181e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list) 18250d08e47SArnaldo Carvalho de Melo { 1836d2783feSJohn Garry while (!list_empty(list)) { 1846d2783feSJohn Garry struct evsel *evsel, *temp, *leader = NULL; 18575562573SAdrian Hunter 186e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 187b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 188a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 1896d2783feSJohn Garry leader = evsel; 1906d2783feSJohn Garry break; 1916d2783feSJohn Garry } 1926d2783feSJohn Garry 1936d2783feSJohn Garry __evlist__for_each_entry_safe(list, temp, evsel) { 1946d2783feSJohn Garry if (evsel->leader == leader) { 1956d2783feSJohn Garry list_del_init(&evsel->core.node); 1966d2783feSJohn Garry evlist__add(evlist, evsel); 1976d2783feSJohn Garry } 1986d2783feSJohn Garry } 199f114d6efSAdrian Hunter } 20050d08e47SArnaldo Carvalho de Melo } 20150d08e47SArnaldo Carvalho de Melo 202c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist, 203c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs) 204c0e53476SArnaldo Carvalho de Melo { 205c0e53476SArnaldo Carvalho de Melo size_t i; 206c0e53476SArnaldo Carvalho de Melo int err; 207c0e53476SArnaldo Carvalho de Melo 208c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 209c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it. 210b02736f7SArnaldo Carvalho de Melo struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name); 211c0e53476SArnaldo Carvalho de Melo if (evsel == NULL) 212c0e53476SArnaldo Carvalho de Melo continue; 213c0e53476SArnaldo Carvalho de Melo 214c0e53476SArnaldo Carvalho de Melo err = -EEXIST; 215c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL) 216c0e53476SArnaldo Carvalho de Melo goto out; 217c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler; 218c0e53476SArnaldo Carvalho de Melo } 219c0e53476SArnaldo Carvalho de Melo 220c0e53476SArnaldo Carvalho de Melo err = 0; 221c0e53476SArnaldo Carvalho de Melo out: 222c0e53476SArnaldo Carvalho de Melo return err; 223c0e53476SArnaldo Carvalho de Melo } 224c0e53476SArnaldo Carvalho de Melo 225a622eafaSArnaldo Carvalho de Melo void __evlist__set_leader(struct list_head *list) 22663dab225SArnaldo Carvalho de Melo { 22732dcd021SJiri Olsa struct evsel *evsel, *leader; 22863dab225SArnaldo Carvalho de Melo 229b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 230b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 23197f63e4aSNamhyung Kim 2325643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 23363dab225SArnaldo Carvalho de Melo 234e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 23563dab225SArnaldo Carvalho de Melo evsel->leader = leader; 23663dab225SArnaldo Carvalho de Melo } 23763dab225SArnaldo Carvalho de Melo } 23863dab225SArnaldo Carvalho de Melo 239a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist) 2406a4bb04cSJiri Olsa { 2416484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2426484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 243a622eafaSArnaldo Carvalho de Melo __evlist__set_leader(&evlist->core.entries); 2446a4bb04cSJiri Olsa } 24597f63e4aSNamhyung Kim } 2466a4bb04cSJiri Olsa 247e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise) 248361c99a6SArnaldo Carvalho de Melo { 2498f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_cycles(precise); 250361c99a6SArnaldo Carvalho de Melo 251361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2527c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 253361c99a6SArnaldo Carvalho de Melo 254a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 255361c99a6SArnaldo Carvalho de Melo return 0; 256361c99a6SArnaldo Carvalho de Melo } 2575c581041SArnaldo Carvalho de Melo 258e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist) 2595bae0250SArnaldo Carvalho de Melo { 2605bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2615bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2625bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2635bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2645bae0250SArnaldo Carvalho de Melo }; 2658f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries); 2665bae0250SArnaldo Carvalho de Melo 2675bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2685bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2695bae0250SArnaldo Carvalho de Melo 270a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2715bae0250SArnaldo Carvalho de Melo return 0; 2725bae0250SArnaldo Carvalho de Melo } 2735bae0250SArnaldo Carvalho de Melo 274e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 27550d08e47SArnaldo Carvalho de Melo { 27632dcd021SJiri Olsa struct evsel *evsel, *n; 27750d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 27850d08e47SArnaldo Carvalho de Melo size_t i; 27950d08e47SArnaldo Carvalho de Melo 28050d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2818f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 28250d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 28350d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 284b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 28550d08e47SArnaldo Carvalho de Melo } 28650d08e47SArnaldo Carvalho de Melo 287e414fd1aSArnaldo Carvalho de Melo evlist__splice_list_tail(evlist, &head); 28850d08e47SArnaldo Carvalho de Melo 28950d08e47SArnaldo Carvalho de Melo return 0; 29050d08e47SArnaldo Carvalho de Melo 29150d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 292e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2935eb2dd2aSJiri Olsa evsel__delete(evsel); 29450d08e47SArnaldo Carvalho de Melo return -1; 29550d08e47SArnaldo Carvalho de Melo } 29650d08e47SArnaldo Carvalho de Melo 297e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 29879695e1bSArnaldo Carvalho de Melo { 29979695e1bSArnaldo Carvalho de Melo size_t i; 30079695e1bSArnaldo Carvalho de Melo 30179695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 30279695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 30379695e1bSArnaldo Carvalho de Melo 304a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 30579695e1bSArnaldo Carvalho de Melo } 30679695e1bSArnaldo Carvalho de Melo 307*42641d6fSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused) 308*42641d6fSKan Liang { 309*42641d6fSKan Liang return 0; 310*42641d6fSKan Liang } 311*42641d6fSKan Liang 312b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 313ee29be62SArnaldo Carvalho de Melo { 31432dcd021SJiri Olsa struct evsel *evsel; 315ee29be62SArnaldo Carvalho de Melo 316e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3171fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3181fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 319ee29be62SArnaldo Carvalho de Melo return evsel; 320ee29be62SArnaldo Carvalho de Melo } 321ee29be62SArnaldo Carvalho de Melo 322ee29be62SArnaldo Carvalho de Melo return NULL; 323ee29be62SArnaldo Carvalho de Melo } 324ee29be62SArnaldo Carvalho de Melo 325b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name) 326a2f2804aSDavid Ahern { 32732dcd021SJiri Olsa struct evsel *evsel; 328a2f2804aSDavid Ahern 329e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3301fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 331a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 332a2f2804aSDavid Ahern return evsel; 333a2f2804aSDavid Ahern } 334a2f2804aSDavid Ahern 335a2f2804aSDavid Ahern return NULL; 336a2f2804aSDavid Ahern } 337a2f2804aSDavid Ahern 338e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler) 33939876e7dSArnaldo Carvalho de Melo { 3408f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp(sys, name); 34139876e7dSArnaldo Carvalho de Melo 3428dd2a131SJiri Olsa if (IS_ERR(evsel)) 34339876e7dSArnaldo Carvalho de Melo return -1; 34439876e7dSArnaldo Carvalho de Melo 345744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 346a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 34739876e7dSArnaldo Carvalho de Melo return 0; 34839876e7dSArnaldo Carvalho de Melo } 34939876e7dSArnaldo Carvalho de Melo 350f63c2f5aSArnaldo Carvalho de Melo static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel) 351bf8e8f4bSAdrian Hunter { 352648b5af3SJiri Olsa if (evsel->core.system_wide) 353bf8e8f4bSAdrian Hunter return 1; 354bf8e8f4bSAdrian Hunter else 355a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 356bf8e8f4bSAdrian Hunter } 357bf8e8f4bSAdrian Hunter 358a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist) 359a8cbe40fSAndi Kleen { 360a8cbe40fSAndi Kleen struct evsel *pos; 361a8cbe40fSAndi Kleen 362a8cbe40fSAndi Kleen /* 363a8cbe40fSAndi Kleen * Reset the per evsel cpu_iter. This is needed because 364a8cbe40fSAndi Kleen * each evsel's cpumap may have a different index space, 365a8cbe40fSAndi Kleen * and some operations need the index to modify 366a8cbe40fSAndi Kleen * the FD xyarray (e.g. open, close) 367a8cbe40fSAndi Kleen */ 368a8cbe40fSAndi Kleen evlist__for_each_entry(evlist, pos) 369a8cbe40fSAndi Kleen pos->cpu_iter = 0; 370a8cbe40fSAndi Kleen } 371a8cbe40fSAndi Kleen 372a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu) 373a8cbe40fSAndi Kleen { 374a8cbe40fSAndi Kleen if (ev->cpu_iter >= ev->core.cpus->nr) 375a8cbe40fSAndi Kleen return true; 376a8cbe40fSAndi Kleen if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu) 377a8cbe40fSAndi Kleen return true; 378a8cbe40fSAndi Kleen return false; 379a8cbe40fSAndi Kleen } 380a8cbe40fSAndi Kleen 381a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu) 382a8cbe40fSAndi Kleen { 383a8cbe40fSAndi Kleen if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) { 384a8cbe40fSAndi Kleen ev->cpu_iter++; 385a8cbe40fSAndi Kleen return false; 386a8cbe40fSAndi Kleen } 387a8cbe40fSAndi Kleen return true; 388a8cbe40fSAndi Kleen } 389a8cbe40fSAndi Kleen 3907cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name) 3917cfcd1e0SJiri Olsa { 3927cfcd1e0SJiri Olsa if (!evsel_name) 3937cfcd1e0SJiri Olsa return 0; 3947cfcd1e0SJiri Olsa if (evsel__is_dummy_event(pos)) 3957cfcd1e0SJiri Olsa return 1; 3967cfcd1e0SJiri Olsa return strcmp(pos->name, evsel_name); 3977cfcd1e0SJiri Olsa } 3987cfcd1e0SJiri Olsa 3997cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist) 4007cfcd1e0SJiri Olsa { 4017cfcd1e0SJiri Olsa struct evsel *pos; 4027cfcd1e0SJiri Olsa 4037cfcd1e0SJiri Olsa evlist__for_each_entry(evlist, pos) { 4047cfcd1e0SJiri Olsa if (!evsel__is_group_leader(pos) || !pos->core.fd) 4057cfcd1e0SJiri Olsa continue; 4067cfcd1e0SJiri Olsa /* If at least one event is enabled, evlist is enabled. */ 4077cfcd1e0SJiri Olsa if (!pos->disabled) 4087cfcd1e0SJiri Olsa return true; 4097cfcd1e0SJiri Olsa } 4107cfcd1e0SJiri Olsa return false; 4117cfcd1e0SJiri Olsa } 4127cfcd1e0SJiri Olsa 4137cfcd1e0SJiri Olsa static void __evlist__disable(struct evlist *evlist, char *evsel_name) 4144152ab37SArnaldo Carvalho de Melo { 41532dcd021SJiri Olsa struct evsel *pos; 416704e2f5bSAndi Kleen struct affinity affinity; 41787cf8360SAdrian Hunter int cpu, i, imm = 0; 41887cf8360SAdrian Hunter bool has_imm = false; 419704e2f5bSAndi Kleen 420704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 421704e2f5bSAndi Kleen return; 422704e2f5bSAndi Kleen 42387cf8360SAdrian Hunter /* Disable 'immediate' events last */ 42487cf8360SAdrian Hunter for (imm = 0; imm <= 1; imm++) { 425704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 426704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 4274152ab37SArnaldo Carvalho de Melo 428e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 4297cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name)) 4307cfcd1e0SJiri Olsa continue; 431704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 432704e2f5bSAndi Kleen continue; 433c754c382SArnaldo Carvalho de Melo if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd) 4343fe4430dSJiri Olsa continue; 43587cf8360SAdrian Hunter if (pos->immediate) 43687cf8360SAdrian Hunter has_imm = true; 43787cf8360SAdrian Hunter if (pos->immediate != imm) 43887cf8360SAdrian Hunter continue; 439704e2f5bSAndi Kleen evsel__disable_cpu(pos, pos->cpu_iter - 1); 440704e2f5bSAndi Kleen } 441704e2f5bSAndi Kleen } 44287cf8360SAdrian Hunter if (!has_imm) 44387cf8360SAdrian Hunter break; 44487cf8360SAdrian Hunter } 44587cf8360SAdrian Hunter 446704e2f5bSAndi Kleen affinity__cleanup(&affinity); 447704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 4487cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name)) 4497cfcd1e0SJiri Olsa continue; 450c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 451704e2f5bSAndi Kleen continue; 452704e2f5bSAndi Kleen pos->disabled = true; 4534152ab37SArnaldo Carvalho de Melo } 4542b56bcfbSArnaldo Carvalho de Melo 4557cfcd1e0SJiri Olsa /* 4567cfcd1e0SJiri Olsa * If we disabled only single event, we need to check 4577cfcd1e0SJiri Olsa * the enabled state of the evlist manually. 4587cfcd1e0SJiri Olsa */ 4597cfcd1e0SJiri Olsa if (evsel_name) 4607cfcd1e0SJiri Olsa evlist->enabled = evlist__is_enabled(evlist); 4617cfcd1e0SJiri Olsa else 4622b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 4634152ab37SArnaldo Carvalho de Melo } 4644152ab37SArnaldo Carvalho de Melo 4657cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist) 4667cfcd1e0SJiri Olsa { 4677cfcd1e0SJiri Olsa __evlist__disable(evlist, NULL); 4687cfcd1e0SJiri Olsa } 4697cfcd1e0SJiri Olsa 4707cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name) 4717cfcd1e0SJiri Olsa { 4727cfcd1e0SJiri Olsa __evlist__disable(evlist, evsel_name); 4737cfcd1e0SJiri Olsa } 4747cfcd1e0SJiri Olsa 4757cfcd1e0SJiri Olsa static void __evlist__enable(struct evlist *evlist, char *evsel_name) 476764e16a3SDavid Ahern { 47732dcd021SJiri Olsa struct evsel *pos; 478704e2f5bSAndi Kleen struct affinity affinity; 479704e2f5bSAndi Kleen int cpu, i; 480764e16a3SDavid Ahern 481704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 482704e2f5bSAndi Kleen return; 483704e2f5bSAndi Kleen 484704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 485704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 486704e2f5bSAndi Kleen 487704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 4887cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name)) 4897cfcd1e0SJiri Olsa continue; 490704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 491704e2f5bSAndi Kleen continue; 492c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 493704e2f5bSAndi Kleen continue; 494704e2f5bSAndi Kleen evsel__enable_cpu(pos, pos->cpu_iter - 1); 495704e2f5bSAndi Kleen } 496704e2f5bSAndi Kleen } 497704e2f5bSAndi Kleen affinity__cleanup(&affinity); 498e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 4997cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name)) 5007cfcd1e0SJiri Olsa continue; 501c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 5023fe4430dSJiri Olsa continue; 503704e2f5bSAndi Kleen pos->disabled = false; 504764e16a3SDavid Ahern } 5052b56bcfbSArnaldo Carvalho de Melo 5067cfcd1e0SJiri Olsa /* 5077cfcd1e0SJiri Olsa * Even single event sets the 'enabled' for evlist, 5087cfcd1e0SJiri Olsa * so the toggle can work properly and toggle to 5097cfcd1e0SJiri Olsa * 'disabled' state. 5107cfcd1e0SJiri Olsa */ 5112b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 5122b56bcfbSArnaldo Carvalho de Melo } 5132b56bcfbSArnaldo Carvalho de Melo 5147cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist) 5157cfcd1e0SJiri Olsa { 5167cfcd1e0SJiri Olsa __evlist__enable(evlist, NULL); 5177cfcd1e0SJiri Olsa } 5187cfcd1e0SJiri Olsa 5197cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name) 5207cfcd1e0SJiri Olsa { 5217cfcd1e0SJiri Olsa __evlist__enable(evlist, evsel_name); 5227cfcd1e0SJiri Olsa } 5237cfcd1e0SJiri Olsa 524ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist) 5252b56bcfbSArnaldo Carvalho de Melo { 526e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 527764e16a3SDavid Ahern } 528764e16a3SDavid Ahern 52937b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu) 5301c65056cSAdrian Hunter { 53118ef15c6SArnaldo Carvalho de Melo int thread; 532f63c2f5aSArnaldo Carvalho de Melo int nr_threads = evlist__nr_threads(evlist, evsel); 5331c65056cSAdrian Hunter 5349dfcb759SJiri Olsa if (!evsel->core.fd) 5351c65056cSAdrian Hunter return -EINVAL; 5361c65056cSAdrian Hunter 5371c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 53818ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 5391c65056cSAdrian Hunter if (err) 5401c65056cSAdrian Hunter return err; 5411c65056cSAdrian Hunter } 5421c65056cSAdrian Hunter return 0; 5431c65056cSAdrian Hunter } 5441c65056cSAdrian Hunter 54537b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread) 5461c65056cSAdrian Hunter { 54718ef15c6SArnaldo Carvalho de Melo int cpu; 5486549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 5491c65056cSAdrian Hunter 5509dfcb759SJiri Olsa if (!evsel->core.fd) 5511c65056cSAdrian Hunter return -EINVAL; 5521c65056cSAdrian Hunter 5531c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 55418ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 5551c65056cSAdrian Hunter if (err) 5561c65056cSAdrian Hunter return err; 5571c65056cSAdrian Hunter } 5581c65056cSAdrian Hunter return 0; 5591c65056cSAdrian Hunter } 5601c65056cSAdrian Hunter 56137b01abeSArnaldo Carvalho de Melo int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx) 5621c65056cSAdrian Hunter { 563315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 5641c65056cSAdrian Hunter 5651c65056cSAdrian Hunter if (per_cpu_mmaps) 56637b01abeSArnaldo Carvalho de Melo return evlist__enable_event_cpu(evlist, evsel, idx); 56737b01abeSArnaldo Carvalho de Melo 56837b01abeSArnaldo Carvalho de Melo return evlist__enable_event_thread(evlist, evsel, idx); 5691c65056cSAdrian Hunter } 5701c65056cSAdrian Hunter 571f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 572e4b356b5SArnaldo Carvalho de Melo { 573ab4c1f9fSAlexey Budankov return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default); 574e4b356b5SArnaldo Carvalho de Melo } 5751b85337dSArnaldo Carvalho de Melo 576f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 5771ddec7f0SArnaldo Carvalho de Melo { 57884227cb1SJiri Olsa return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 5791ddec7f0SArnaldo Carvalho de Melo } 5801ddec7f0SArnaldo Carvalho de Melo 58180ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 582f66a889dSArnaldo Carvalho de Melo { 58380ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 584f66a889dSArnaldo Carvalho de Melo } 585f66a889dSArnaldo Carvalho de Melo 5863ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id) 58770db7533SArnaldo Carvalho de Melo { 58870db7533SArnaldo Carvalho de Melo struct hlist_head *head; 58970db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 59070db7533SArnaldo Carvalho de Melo int hash; 59170db7533SArnaldo Carvalho de Melo 59270db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5931d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 59470db7533SArnaldo Carvalho de Melo 595b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 59670db7533SArnaldo Carvalho de Melo if (sid->id == id) 597932a3594SJiri Olsa return sid; 598932a3594SJiri Olsa 599932a3594SJiri Olsa return NULL; 600932a3594SJiri Olsa } 601932a3594SJiri Olsa 6023ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id) 603932a3594SJiri Olsa { 604932a3594SJiri Olsa struct perf_sample_id *sid; 605932a3594SJiri Olsa 6066484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 607515dbe48SJiri Olsa return evlist__first(evlist); 608932a3594SJiri Olsa 6093ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id); 610932a3594SJiri Olsa if (sid) 61170c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 61230e68bccSNamhyung Kim 6138cedf3a5SArnaldo Carvalho de Melo if (!evlist__sample_id_all(evlist)) 614515dbe48SJiri Olsa return evlist__first(evlist); 61530e68bccSNamhyung Kim 61670db7533SArnaldo Carvalho de Melo return NULL; 61770db7533SArnaldo Carvalho de Melo } 61804391debSArnaldo Carvalho de Melo 6193ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id) 620dddcf6abSAdrian Hunter { 621dddcf6abSAdrian Hunter struct perf_sample_id *sid; 622dddcf6abSAdrian Hunter 623dddcf6abSAdrian Hunter if (!id) 624dddcf6abSAdrian Hunter return NULL; 625dddcf6abSAdrian Hunter 6263ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id); 627dddcf6abSAdrian Hunter if (sid) 62870c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 629dddcf6abSAdrian Hunter 630dddcf6abSAdrian Hunter return NULL; 631dddcf6abSAdrian Hunter } 632dddcf6abSAdrian Hunter 6333ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id) 63475562573SAdrian Hunter { 635b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 63675562573SAdrian Hunter ssize_t n; 63775562573SAdrian Hunter 63875562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 63975562573SAdrian Hunter 64075562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 64175562573SAdrian Hunter if (evlist->id_pos >= n) 64275562573SAdrian Hunter return -1; 64375562573SAdrian Hunter *id = array[evlist->id_pos]; 64475562573SAdrian Hunter } else { 64575562573SAdrian Hunter if (evlist->is_pos > n) 64675562573SAdrian Hunter return -1; 64775562573SAdrian Hunter n -= evlist->is_pos; 64875562573SAdrian Hunter *id = array[n]; 64975562573SAdrian Hunter } 65075562573SAdrian Hunter return 0; 65175562573SAdrian Hunter } 65275562573SAdrian Hunter 6533ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event) 65475562573SAdrian Hunter { 655515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 65675562573SAdrian Hunter struct hlist_head *head; 65775562573SAdrian Hunter struct perf_sample_id *sid; 65875562573SAdrian Hunter int hash; 65975562573SAdrian Hunter u64 id; 66075562573SAdrian Hunter 6616484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 66298be6966SAdrian Hunter return first; 66398be6966SAdrian Hunter 6641fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 66598be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 66698be6966SAdrian Hunter return first; 66775562573SAdrian Hunter 6683ccf8a7bSArnaldo Carvalho de Melo if (evlist__event2id(evlist, event, &id)) 66975562573SAdrian Hunter return NULL; 67075562573SAdrian Hunter 67175562573SAdrian Hunter /* Synthesized events have an id of zero */ 67275562573SAdrian Hunter if (!id) 67398be6966SAdrian Hunter return first; 67475562573SAdrian Hunter 67575562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 6761d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 67775562573SAdrian Hunter 67875562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 67975562573SAdrian Hunter if (sid->id == id) 68070c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 68175562573SAdrian Hunter } 68275562573SAdrian Hunter return NULL; 68375562573SAdrian Hunter } 68475562573SAdrian Hunter 6850a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value) 68665aea233SWang Nan { 68765aea233SWang Nan int i; 68865aea233SWang Nan 6890b72d69aSWang Nan if (!evlist->overwrite_mmap) 690078c3386SWang Nan return 0; 691078c3386SWang Nan 692c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6932cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 69465aea233SWang Nan int err; 69565aea233SWang Nan 69665aea233SWang Nan if (fd < 0) 69765aea233SWang Nan continue; 69865aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 69965aea233SWang Nan if (err) 70065aea233SWang Nan return err; 70165aea233SWang Nan } 70265aea233SWang Nan return 0; 70365aea233SWang Nan } 70465aea233SWang Nan 7050a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist) 70665aea233SWang Nan { 7070a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, true); 70865aea233SWang Nan } 70965aea233SWang Nan 7100a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist) 71165aea233SWang Nan { 7120a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, false); 71365aea233SWang Nan } 71465aea233SWang Nan 715db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 716f8a95309SArnaldo Carvalho de Melo { 717aece948fSArnaldo Carvalho de Melo int i; 718f8a95309SArnaldo Carvalho de Melo 719b2cb615dSWang Nan if (evlist->mmap) 720c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 72180e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 722b2cb615dSWang Nan 7230b72d69aSWang Nan if (evlist->overwrite_mmap) 724c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 72580e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 726a1f72618SWang Nan } 727aece948fSArnaldo Carvalho de Melo 728db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 729a1f72618SWang Nan { 730db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 73104662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 7320b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 733f8a95309SArnaldo Carvalho de Melo } 734f8a95309SArnaldo Carvalho de Melo 73580e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 73680e53d11SJiri Olsa { 73780e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 73880e53d11SJiri Olsa 73980e53d11SJiri Olsa mmap__munmap(m); 74080e53d11SJiri Olsa } 74180e53d11SJiri Olsa 742d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 7432c5f6d87SKan Liang bool overwrite) 744f8a95309SArnaldo Carvalho de Melo { 745d4c6fb36SWang Nan int i; 746a5830532SJiri Olsa struct mmap *map; 747d4c6fb36SWang Nan 748c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 7498db6d6b1SWang Nan if (!map) 7508db6d6b1SWang Nan return NULL; 751946ae1d4SWang Nan 752c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 7536eb65f7aSJiri Olsa struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 7546eb65f7aSJiri Olsa 7554738ca30SArnaldo Carvalho de Melo /* 7564738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 7576afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 7584738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 7594738ca30SArnaldo Carvalho de Melo * dropped. 7604738ca30SArnaldo Carvalho de Melo * 7614738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 7624738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 7634738ca30SArnaldo Carvalho de Melo */ 7646eb65f7aSJiri Olsa perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 7654738ca30SArnaldo Carvalho de Melo } 766353120b4SJiri Olsa 7678db6d6b1SWang Nan return map; 768f8a95309SArnaldo Carvalho de Melo } 769f8a95309SArnaldo Carvalho de Melo 7709abd2ab2SJiri Olsa static void 7719abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 7729abd2ab2SJiri Olsa struct perf_mmap_param *_mp, 7739abd2ab2SJiri Olsa int idx, bool per_cpu) 7749abd2ab2SJiri Olsa { 7759abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7769abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 7779abd2ab2SJiri Olsa 7789abd2ab2SJiri Olsa auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 7799abd2ab2SJiri Olsa } 7809abd2ab2SJiri Olsa 781bb1b1885SJiri Olsa static struct perf_mmap* 782bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 783bb1b1885SJiri Olsa { 784bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7853805e4f3SJiri Olsa struct mmap *maps; 786bb1b1885SJiri Olsa 7873805e4f3SJiri Olsa maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 788bb1b1885SJiri Olsa 789bb1b1885SJiri Olsa if (!maps) { 7903805e4f3SJiri Olsa maps = evlist__alloc_mmap(evlist, overwrite); 791bb1b1885SJiri Olsa if (!maps) 792bb1b1885SJiri Olsa return NULL; 793bb1b1885SJiri Olsa 7943805e4f3SJiri Olsa if (overwrite) { 795bb1b1885SJiri Olsa evlist->overwrite_mmap = maps; 796bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 797ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 7983805e4f3SJiri Olsa } else { 7993805e4f3SJiri Olsa evlist->mmap = maps; 800bb1b1885SJiri Olsa } 801bb1b1885SJiri Olsa } 802bb1b1885SJiri Olsa 803bb1b1885SJiri Olsa return &maps[idx].core; 804bb1b1885SJiri Olsa } 805bb1b1885SJiri Olsa 806b80132b1SJiri Olsa static int 807b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 808b80132b1SJiri Olsa int output, int cpu) 809b80132b1SJiri Olsa { 810b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core); 811b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 812b80132b1SJiri Olsa 813b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu); 814b80132b1SJiri Olsa } 815b80132b1SJiri Olsa 816f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 817994a1f78SJiri Olsa { 818f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 8198185e881SArnaldo Carvalho de Melo int max; 8208185e881SArnaldo Carvalho de Melo 8218185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 8228185e881SArnaldo Carvalho de Melo /* 8238185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 8248185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 8258185e881SArnaldo Carvalho de Melo * die yet... 8268185e881SArnaldo Carvalho de Melo */ 8278185e881SArnaldo Carvalho de Melo max = 512; 8288185e881SArnaldo Carvalho de Melo } else { 8298185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 8308185e881SArnaldo Carvalho de Melo } 8318185e881SArnaldo Carvalho de Melo 8328185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 8331be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 8341be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 835f5e7150cSArnaldo Carvalho de Melo 836f5e7150cSArnaldo Carvalho de Melo return pages; 837f5e7150cSArnaldo Carvalho de Melo } 838f5e7150cSArnaldo Carvalho de Melo 8399521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 840f5e7150cSArnaldo Carvalho de Melo { 841f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 842f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 843f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 844994a1f78SJiri Olsa return 0; 845994a1f78SJiri Olsa 846994a1f78SJiri Olsa return (pages + 1) * page_size; 847994a1f78SJiri Olsa } 848994a1f78SJiri Olsa 84933c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 85033c2dcfdSDavid Ahern unsigned long max) 851994a1f78SJiri Olsa { 8522fbe4abeSAdrian Hunter unsigned long pages, val; 85327050f53SJiri Olsa static struct parse_tag tags[] = { 85427050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 85527050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 85627050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 85727050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 85827050f53SJiri Olsa { .tag = 0 }, 85927050f53SJiri Olsa }; 860994a1f78SJiri Olsa 8618973504bSDavid Ahern if (str == NULL) 86233c2dcfdSDavid Ahern return -EINVAL; 8638973504bSDavid Ahern 86427050f53SJiri Olsa val = parse_tag_value(str, tags); 8652fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 86627050f53SJiri Olsa /* we got file size value */ 86727050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 86827050f53SJiri Olsa } else { 86927050f53SJiri Olsa /* we got pages count value */ 87027050f53SJiri Olsa char *eptr; 871994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 87233c2dcfdSDavid Ahern if (*eptr != '\0') 87333c2dcfdSDavid Ahern return -EINVAL; 87427050f53SJiri Olsa } 875994a1f78SJiri Olsa 8762bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 87733c2dcfdSDavid Ahern /* leave number of pages at 0 */ 8781dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 8799808143bSJiri Olsa char buf[100]; 8809808143bSJiri Olsa 88133c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 88291529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 8831dbfa938SAdrian Hunter if (!pages) 8841dbfa938SAdrian Hunter return -EINVAL; 8859808143bSJiri Olsa 8869808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 8879808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 8889808143bSJiri Olsa buf, pages); 8892fbe4abeSAdrian Hunter } 8902fbe4abeSAdrian Hunter 89133c2dcfdSDavid Ahern if (pages > max) 89233c2dcfdSDavid Ahern return -EINVAL; 89333c2dcfdSDavid Ahern 89433c2dcfdSDavid Ahern return pages; 89533c2dcfdSDavid Ahern } 89633c2dcfdSDavid Ahern 89725f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 89833c2dcfdSDavid Ahern { 89933c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 90033c2dcfdSDavid Ahern long pages; 90133c2dcfdSDavid Ahern 902f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 90333c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 90433c2dcfdSDavid Ahern 90533c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 90633c2dcfdSDavid Ahern if (pages < 0) { 90733c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 908994a1f78SJiri Olsa return -1; 909994a1f78SJiri Olsa } 910994a1f78SJiri Olsa 911994a1f78SJiri Olsa *mmap_pages = pages; 912994a1f78SJiri Olsa return 0; 913994a1f78SJiri Olsa } 914994a1f78SJiri Olsa 91525f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused) 916e9db1310SAdrian Hunter { 91725f84702SArnaldo Carvalho de Melo return __evlist__parse_mmap_pages(opt->value, str); 918e9db1310SAdrian Hunter } 919e9db1310SAdrian Hunter 920c83fa7f2SAdrian Hunter /** 9219521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 922c83fa7f2SAdrian Hunter * @evlist: list of events 923c83fa7f2SAdrian Hunter * @pages: map length in pages 924c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 925718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 926718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 927f8a95309SArnaldo Carvalho de Melo * 928c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 9299521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 930c83fa7f2SAdrian Hunter * automatically. 931f8a95309SArnaldo Carvalho de Melo * 932718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 933718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 934718c602dSAdrian Hunter * 935c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 936f8a95309SArnaldo Carvalho de Melo */ 9379521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 9387a276ff6SWang Nan unsigned int auxtrace_pages, 93951255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 94051255a8aSAlexey Budankov int comp_level) 941f8a95309SArnaldo Carvalho de Melo { 94271f566a3SWang Nan /* 94371f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 94471f566a3SWang Nan * Its value is decided by evsel's write_backward. 94571f566a3SWang Nan * So &mp should not be passed through const pointer. 94671f566a3SWang Nan */ 947e440979fSJiri Olsa struct mmap_params mp = { 948e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 949e440979fSJiri Olsa .affinity = affinity, 950e440979fSJiri Olsa .flush = flush, 951e440979fSJiri Olsa .comp_level = comp_level 952e440979fSJiri Olsa }; 953923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = { 9549abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx, 955bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get, 956b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap, 9579abd2ab2SJiri Olsa }; 95850a682ceSArnaldo Carvalho de Melo 959f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 960f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 961f8a95309SArnaldo Carvalho de Melo 962f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 963718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 964718c602dSAdrian Hunter 965923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 966f8a95309SArnaldo Carvalho de Melo } 9677e2ed097SArnaldo Carvalho de Melo 9689521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 969718c602dSAdrian Hunter { 9709521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 971718c602dSAdrian Hunter } 972718c602dSAdrian Hunter 9737748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target) 9747e2ed097SArnaldo Carvalho de Melo { 975147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 976f854839bSJiri Olsa struct perf_cpu_map *cpus; 9779749b90eSJiri Olsa struct perf_thread_map *threads; 9787e2ed097SArnaldo Carvalho de Melo 979147c508fSJin Yao /* 980147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 981147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 982147c508fSJin Yao * target->system_wide = true. 983147c508fSJin Yao * 984147c508fSJin Yao * If specify '--per-thread' only to perf record, 985147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 986147c508fSJin Yao * 987147c508fSJin Yao * So target->per_thread && target->system_wide is false. 988147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 989147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 990147c508fSJin Yao * current behavior. 991147c508fSJin Yao * 992147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 993147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 994147c508fSJin Yao * per-thread data. thread_map__new_str will call 995147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 996147c508fSJin Yao */ 99773c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 998147c508fSJin Yao all_threads); 99974bfd2b2SAdrian Hunter 100074bfd2b2SAdrian Hunter if (!threads) 10017e2ed097SArnaldo Carvalho de Melo return -1; 10027e2ed097SArnaldo Carvalho de Melo 10039c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 1004397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 1005879d77d0SNamhyung Kim else 10069c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 10077e2ed097SArnaldo Carvalho de Melo 100874bfd2b2SAdrian Hunter if (!cpus) 10097e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 10107e2ed097SArnaldo Carvalho de Melo 1011ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 1012ec9a77a7SAdrian Hunter 1013453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1014d5bc056eSAdrian Hunter 1015bfd1b83dSNamhyung Kim /* as evlist now has references, put count here */ 1016bfd1b83dSNamhyung Kim perf_cpu_map__put(cpus); 1017bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 1018bfd1b83dSNamhyung Kim 1019d5bc056eSAdrian Hunter return 0; 10207e2ed097SArnaldo Carvalho de Melo 10217e2ed097SArnaldo Carvalho de Melo out_delete_threads: 10227836e52eSJiri Olsa perf_thread_map__put(threads); 10237e2ed097SArnaldo Carvalho de Melo return -1; 10247e2ed097SArnaldo Carvalho de Melo } 10257e2ed097SArnaldo Carvalho de Melo 102624bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 10270a102479SFrederic Weisbecker { 102832dcd021SJiri Olsa struct evsel *evsel; 1029745cefc5SArnaldo Carvalho de Melo int err = 0; 10300a102479SFrederic Weisbecker 1031e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1032745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 10330a102479SFrederic Weisbecker continue; 1034745cefc5SArnaldo Carvalho de Melo 1035d988d5eeSKan Liang /* 1036d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1037d988d5eeSKan Liang * So evlist and evsel should always be same. 1038d988d5eeSKan Liang */ 1039a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 104023d4aad4SArnaldo Carvalho de Melo if (err) { 104123d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1042745cefc5SArnaldo Carvalho de Melo break; 10430a102479SFrederic Weisbecker } 104423d4aad4SArnaldo Carvalho de Melo } 10450a102479SFrederic Weisbecker 1046745cefc5SArnaldo Carvalho de Melo return err; 1047745cefc5SArnaldo Carvalho de Melo } 1048745cefc5SArnaldo Carvalho de Melo 104924bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1050745cefc5SArnaldo Carvalho de Melo { 105132dcd021SJiri Olsa struct evsel *evsel; 1052745cefc5SArnaldo Carvalho de Melo int err = 0; 1053745cefc5SArnaldo Carvalho de Melo 105405cea449SArnaldo Carvalho de Melo if (filter == NULL) 105505cea449SArnaldo Carvalho de Melo return -1; 105605cea449SArnaldo Carvalho de Melo 1057e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 10581fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1059fdf14720SWang Nan continue; 1060fdf14720SWang Nan 1061ad681adfSArnaldo Carvalho de Melo err = evsel__set_filter(evsel, filter); 1062745cefc5SArnaldo Carvalho de Melo if (err) 1063745cefc5SArnaldo Carvalho de Melo break; 1064745cefc5SArnaldo Carvalho de Melo } 1065745cefc5SArnaldo Carvalho de Melo 1066745cefc5SArnaldo Carvalho de Melo return err; 10670a102479SFrederic Weisbecker } 106874429964SFrederic Weisbecker 106924bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter) 107053c92f73SArnaldo Carvalho de Melo { 107153c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 107253c92f73SArnaldo Carvalho de Melo int err = 0; 107353c92f73SArnaldo Carvalho de Melo 107453c92f73SArnaldo Carvalho de Melo if (filter == NULL) 107553c92f73SArnaldo Carvalho de Melo return -1; 107653c92f73SArnaldo Carvalho de Melo 107753c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 107853c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 107953c92f73SArnaldo Carvalho de Melo continue; 108053c92f73SArnaldo Carvalho de Melo 1081ad681adfSArnaldo Carvalho de Melo err = evsel__append_tp_filter(evsel, filter); 108253c92f73SArnaldo Carvalho de Melo if (err) 108353c92f73SArnaldo Carvalho de Melo break; 108453c92f73SArnaldo Carvalho de Melo } 108553c92f73SArnaldo Carvalho de Melo 108653c92f73SArnaldo Carvalho de Melo return err; 108753c92f73SArnaldo Carvalho de Melo } 108853c92f73SArnaldo Carvalho de Melo 1089da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1090cfd70a26SArnaldo Carvalho de Melo { 1091cfd70a26SArnaldo Carvalho de Melo char *filter; 1092be199adaSArnaldo Carvalho de Melo size_t i; 1093cfd70a26SArnaldo Carvalho de Melo 1094be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1095be199adaSArnaldo Carvalho de Melo if (i == 0) { 1096be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 109705cea449SArnaldo Carvalho de Melo return NULL; 1098be199adaSArnaldo Carvalho de Melo } else { 1099be199adaSArnaldo Carvalho de Melo char *tmp; 1100be199adaSArnaldo Carvalho de Melo 1101be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1102be199adaSArnaldo Carvalho de Melo goto out_free; 1103be199adaSArnaldo Carvalho de Melo 1104be199adaSArnaldo Carvalho de Melo free(filter); 1105be199adaSArnaldo Carvalho de Melo filter = tmp; 1106be199adaSArnaldo Carvalho de Melo } 1107be199adaSArnaldo Carvalho de Melo } 1108cfd70a26SArnaldo Carvalho de Melo 110905cea449SArnaldo Carvalho de Melo return filter; 1110be199adaSArnaldo Carvalho de Melo out_free: 1111cfd70a26SArnaldo Carvalho de Melo free(filter); 111205cea449SArnaldo Carvalho de Melo return NULL; 111305cea449SArnaldo Carvalho de Melo } 111405cea449SArnaldo Carvalho de Melo 111524bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 111605cea449SArnaldo Carvalho de Melo { 111705cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 111824bf91a7SArnaldo Carvalho de Melo int ret = evlist__set_tp_filter(evlist, filter); 111905cea449SArnaldo Carvalho de Melo 112005cea449SArnaldo Carvalho de Melo free(filter); 1121cfd70a26SArnaldo Carvalho de Melo return ret; 1122cfd70a26SArnaldo Carvalho de Melo } 1123cfd70a26SArnaldo Carvalho de Melo 112424bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1125be199adaSArnaldo Carvalho de Melo { 112624bf91a7SArnaldo Carvalho de Melo return evlist__set_tp_filter_pids(evlist, 1, &pid); 1127be199adaSArnaldo Carvalho de Melo } 1128be199adaSArnaldo Carvalho de Melo 112924bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 11301827ab5bSArnaldo Carvalho de Melo { 11311827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 113224bf91a7SArnaldo Carvalho de Melo int ret = evlist__append_tp_filter(evlist, filter); 11331827ab5bSArnaldo Carvalho de Melo 11341827ab5bSArnaldo Carvalho de Melo free(filter); 11351827ab5bSArnaldo Carvalho de Melo return ret; 11361827ab5bSArnaldo Carvalho de Melo } 11371827ab5bSArnaldo Carvalho de Melo 113824bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 11391827ab5bSArnaldo Carvalho de Melo { 114024bf91a7SArnaldo Carvalho de Melo return evlist__append_tp_filter_pids(evlist, 1, &pid); 11411827ab5bSArnaldo Carvalho de Melo } 11421827ab5bSArnaldo Carvalho de Melo 1143b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist) 114474429964SFrederic Weisbecker { 114532dcd021SJiri Olsa struct evsel *pos; 1146c2a70653SArnaldo Carvalho de Melo 11476484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 114875562573SAdrian Hunter return true; 114975562573SAdrian Hunter 115075562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 115175562573SAdrian Hunter return false; 115275562573SAdrian Hunter 1153e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 115475562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 115575562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1156c2a70653SArnaldo Carvalho de Melo return false; 115774429964SFrederic Weisbecker } 115874429964SFrederic Weisbecker 1159c2a70653SArnaldo Carvalho de Melo return true; 1160c2a70653SArnaldo Carvalho de Melo } 1161c2a70653SArnaldo Carvalho de Melo 1162b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist) 1163c2a70653SArnaldo Carvalho de Melo { 116432dcd021SJiri Olsa struct evsel *evsel; 116575562573SAdrian Hunter 116675562573SAdrian Hunter if (evlist->combined_sample_type) 116775562573SAdrian Hunter return evlist->combined_sample_type; 116875562573SAdrian Hunter 1169e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11701fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 117175562573SAdrian Hunter 117275562573SAdrian Hunter return evlist->combined_sample_type; 117375562573SAdrian Hunter } 117475562573SAdrian Hunter 1175b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist) 117675562573SAdrian Hunter { 117775562573SAdrian Hunter evlist->combined_sample_type = 0; 1178b3c2cc2bSArnaldo Carvalho de Melo return __evlist__combined_sample_type(evlist); 1179c2a70653SArnaldo Carvalho de Melo } 1180c2a70653SArnaldo Carvalho de Melo 118192c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist) 118298df858eSAndi Kleen { 118332dcd021SJiri Olsa struct evsel *evsel; 118498df858eSAndi Kleen u64 branch_type = 0; 118598df858eSAndi Kleen 1186e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11871fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 118898df858eSAndi Kleen return branch_type; 118998df858eSAndi Kleen } 119098df858eSAndi Kleen 119178e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist) 11929ede473cSJiri Olsa { 1193515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 11941fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 11951fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 11969ede473cSJiri Olsa 1197e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 119894d3820fSAdrian Hunter if (read_format != pos->core.attr.read_format) { 119994d3820fSAdrian Hunter pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n", 120094d3820fSAdrian Hunter read_format, (u64)pos->core.attr.read_format); 120194d3820fSAdrian Hunter } 12029ede473cSJiri Olsa } 12039ede473cSJiri Olsa 12049ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 12059ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 12069ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 12079ede473cSJiri Olsa return false; 12089ede473cSJiri Olsa } 12099ede473cSJiri Olsa 12109ede473cSJiri Olsa return true; 12119ede473cSJiri Olsa } 12129ede473cSJiri Olsa 12131420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist) 121481e36bffSArnaldo Carvalho de Melo { 1215515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 121681e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 121781e36bffSArnaldo Carvalho de Melo u64 sample_type; 121881e36bffSArnaldo Carvalho de Melo u16 size = 0; 121981e36bffSArnaldo Carvalho de Melo 12201fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 122181e36bffSArnaldo Carvalho de Melo goto out; 122281e36bffSArnaldo Carvalho de Melo 12231fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 122481e36bffSArnaldo Carvalho de Melo 122581e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 122681e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 122781e36bffSArnaldo Carvalho de Melo 122881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 122981e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 123081e36bffSArnaldo Carvalho de Melo 123181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 123281e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 123381e36bffSArnaldo Carvalho de Melo 123481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 123581e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 123681e36bffSArnaldo Carvalho de Melo 123781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 123881e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 123975562573SAdrian Hunter 124075562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 124175562573SAdrian Hunter size += sizeof(data->id); 124281e36bffSArnaldo Carvalho de Melo out: 124381e36bffSArnaldo Carvalho de Melo return size; 124481e36bffSArnaldo Carvalho de Melo } 124581e36bffSArnaldo Carvalho de Melo 12468cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist) 1247c2a70653SArnaldo Carvalho de Melo { 1248515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1249c2a70653SArnaldo Carvalho de Melo 1250e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 12511fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1252c2a70653SArnaldo Carvalho de Melo return false; 1253c2a70653SArnaldo Carvalho de Melo } 1254c2a70653SArnaldo Carvalho de Melo 1255c2a70653SArnaldo Carvalho de Melo return true; 125674429964SFrederic Weisbecker } 125774429964SFrederic Weisbecker 12588cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist) 125974429964SFrederic Weisbecker { 1260515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 12611fc632ceSJiri Olsa return first->core.attr.sample_id_all; 126274429964SFrederic Weisbecker } 126381cce8deSArnaldo Carvalho de Melo 1264900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel) 126581cce8deSArnaldo Carvalho de Melo { 126681cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 126781cce8deSArnaldo Carvalho de Melo } 1268727ab04eSArnaldo Carvalho de Melo 1269750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1270a74b4b66SNamhyung Kim { 127132dcd021SJiri Olsa struct evsel *evsel; 12727736627bSAndi Kleen struct affinity affinity; 12737736627bSAndi Kleen int cpu, i; 1274a74b4b66SNamhyung Kim 12757736627bSAndi Kleen /* 12767736627bSAndi Kleen * With perf record core.cpus is usually NULL. 12777736627bSAndi Kleen * Use the old method to handle this for now. 12787736627bSAndi Kleen */ 12797736627bSAndi Kleen if (!evlist->core.cpus) { 1280475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 128188761fa1SJiri Olsa evsel__close(evsel); 12827736627bSAndi Kleen return; 12837736627bSAndi Kleen } 12847736627bSAndi Kleen 12857736627bSAndi Kleen if (affinity__setup(&affinity) < 0) 12867736627bSAndi Kleen return; 12877736627bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 12887736627bSAndi Kleen affinity__set(&affinity, cpu); 12897736627bSAndi Kleen 12907736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12917736627bSAndi Kleen if (evsel__cpu_iter_skip(evsel, cpu)) 12927736627bSAndi Kleen continue; 12937736627bSAndi Kleen perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1); 12947736627bSAndi Kleen } 12957736627bSAndi Kleen } 12967736627bSAndi Kleen affinity__cleanup(&affinity); 12977736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12987736627bSAndi Kleen perf_evsel__free_fd(&evsel->core); 12997736627bSAndi Kleen perf_evsel__free_id(&evsel->core); 13007736627bSAndi Kleen } 1301a74b4b66SNamhyung Kim } 1302a74b4b66SNamhyung Kim 13037748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist) 13044112eb18SArnaldo Carvalho de Melo { 1305f854839bSJiri Olsa struct perf_cpu_map *cpus; 13069749b90eSJiri Olsa struct perf_thread_map *threads; 13074112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 13084112eb18SArnaldo Carvalho de Melo 13094112eb18SArnaldo Carvalho de Melo /* 13104112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 13114112eb18SArnaldo Carvalho de Melo * an all cpus map. 13124112eb18SArnaldo Carvalho de Melo * 13134112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 13144112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 13154112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 13164112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 13174112eb18SArnaldo Carvalho de Melo */ 13189c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 13198c0498b6SAdrian Hunter if (!cpus) 13204112eb18SArnaldo Carvalho de Melo goto out; 13214112eb18SArnaldo Carvalho de Melo 13224b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 13238c0498b6SAdrian Hunter if (!threads) 13248c0498b6SAdrian Hunter goto out_put; 13254112eb18SArnaldo Carvalho de Melo 1326453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1327bfd1b83dSNamhyung Kim 1328bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 13298c0498b6SAdrian Hunter out_put: 133038f01d8dSJiri Olsa perf_cpu_map__put(cpus); 1331bfd1b83dSNamhyung Kim out: 1332bfd1b83dSNamhyung Kim return err; 13334112eb18SArnaldo Carvalho de Melo } 13344112eb18SArnaldo Carvalho de Melo 1335474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1336727ab04eSArnaldo Carvalho de Melo { 133732dcd021SJiri Olsa struct evsel *evsel; 1338a74b4b66SNamhyung Kim int err; 1339727ab04eSArnaldo Carvalho de Melo 13404112eb18SArnaldo Carvalho de Melo /* 13414112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 13424112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 13434112eb18SArnaldo Carvalho de Melo */ 134403617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 13457748bb71SArnaldo Carvalho de Melo err = evlist__create_syswide_maps(evlist); 13464112eb18SArnaldo Carvalho de Melo if (err < 0) 13474112eb18SArnaldo Carvalho de Melo goto out_err; 13484112eb18SArnaldo Carvalho de Melo } 13494112eb18SArnaldo Carvalho de Melo 13500a7e7ec9SArnaldo Carvalho de Melo evlist__update_id_pos(evlist); 1351733cd2feSAdrian Hunter 1352e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1353af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1354727ab04eSArnaldo Carvalho de Melo if (err < 0) 1355727ab04eSArnaldo Carvalho de Melo goto out_err; 1356727ab04eSArnaldo Carvalho de Melo } 1357727ab04eSArnaldo Carvalho de Melo 1358727ab04eSArnaldo Carvalho de Melo return 0; 1359727ab04eSArnaldo Carvalho de Melo out_err: 1360750b4edeSJiri Olsa evlist__close(evlist); 136141c21a68SNamhyung Kim errno = -err; 1362727ab04eSArnaldo Carvalho de Melo return err; 1363727ab04eSArnaldo Carvalho de Melo } 136435b9d88eSArnaldo Carvalho de Melo 13657b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[], 13667b392ef0SArnaldo Carvalho de Melo bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 136735b9d88eSArnaldo Carvalho de Melo { 136835b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 136935b9d88eSArnaldo Carvalho de Melo char bf; 137035b9d88eSArnaldo Carvalho de Melo 137135b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 137235b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 137335b9d88eSArnaldo Carvalho de Melo return -1; 137435b9d88eSArnaldo Carvalho de Melo } 137535b9d88eSArnaldo Carvalho de Melo 137635b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 137735b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 137835b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 137935b9d88eSArnaldo Carvalho de Melo } 138035b9d88eSArnaldo Carvalho de Melo 138135b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 138235b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 138335b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 138435b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 138535b9d88eSArnaldo Carvalho de Melo } 138635b9d88eSArnaldo Carvalho de Melo 138735b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13885f1c4225SArnaldo Carvalho de Melo int ret; 13895f1c4225SArnaldo Carvalho de Melo 1390119fa3c9SNamhyung Kim if (pipe_output) 139135b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 139235b9d88eSArnaldo Carvalho de Melo 13930817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13940817df08SDavid Ahern 139535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 139635b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 139735b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 139835b9d88eSArnaldo Carvalho de Melo 139935b9d88eSArnaldo Carvalho de Melo /* 140035b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 140135b9d88eSArnaldo Carvalho de Melo */ 140235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 140335b9d88eSArnaldo Carvalho de Melo 140435b9d88eSArnaldo Carvalho de Melo /* 140535b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 140635b9d88eSArnaldo Carvalho de Melo */ 14075f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 14085f1c4225SArnaldo Carvalho de Melo /* 14095f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 14105f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 14117b392ef0SArnaldo Carvalho de Melo * evlist__start_workload(). 14125f1c4225SArnaldo Carvalho de Melo * 141320f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 14145f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 14155f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 14165f1c4225SArnaldo Carvalho de Melo * here. 14175f1c4225SArnaldo Carvalho de Melo */ 14185f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 14195f1c4225SArnaldo Carvalho de Melo if (ret == -1) 142035b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 14215f1c4225SArnaldo Carvalho de Melo exit(ret); 14225f1c4225SArnaldo Carvalho de Melo } 142335b9d88eSArnaldo Carvalho de Melo 142435b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 142535b9d88eSArnaldo Carvalho de Melo 1426735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1427f33cbe72SArnaldo Carvalho de Melo union sigval val; 1428f33cbe72SArnaldo Carvalho de Melo 1429f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1430f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 143135b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1432f33cbe72SArnaldo Carvalho de Melo } else 1433f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 143435b9d88eSArnaldo Carvalho de Melo exit(-1); 143535b9d88eSArnaldo Carvalho de Melo } 143635b9d88eSArnaldo Carvalho de Melo 1437735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1438735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1439735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1440735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1441735f7e0bSArnaldo Carvalho de Melo }; 1442735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1443735f7e0bSArnaldo Carvalho de Melo } 1444735f7e0bSArnaldo Carvalho de Melo 14451aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 144603617c22SJiri Olsa if (evlist->core.threads == NULL) { 14471aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 14481aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 14491aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 14501aaf63b1SArnaldo Carvalho de Melo } 145103617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 14521aaf63b1SArnaldo Carvalho de Melo } 145335b9d88eSArnaldo Carvalho de Melo 145435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 145535b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 145635b9d88eSArnaldo Carvalho de Melo /* 145735b9d88eSArnaldo Carvalho de Melo * wait for child to settle 145835b9d88eSArnaldo Carvalho de Melo */ 145935b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 146035b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 146135b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 146235b9d88eSArnaldo Carvalho de Melo } 146335b9d88eSArnaldo Carvalho de Melo 1464bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 146535b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 146635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 146735b9d88eSArnaldo Carvalho de Melo return 0; 146835b9d88eSArnaldo Carvalho de Melo 146935b9d88eSArnaldo Carvalho de Melo out_close_pipes: 147035b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 147135b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 147235b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 147335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 147435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 147535b9d88eSArnaldo Carvalho de Melo return -1; 147635b9d88eSArnaldo Carvalho de Melo } 147735b9d88eSArnaldo Carvalho de Melo 14787b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist) 147935b9d88eSArnaldo Carvalho de Melo { 148035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1481b3824404SDavid Ahern char bf = 0; 1482bcf3145fSNamhyung Kim int ret; 148335b9d88eSArnaldo Carvalho de Melo /* 148435b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 148535b9d88eSArnaldo Carvalho de Melo */ 1486bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1487bcf3145fSNamhyung Kim if (ret < 0) 1488e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1489bcf3145fSNamhyung Kim 1490bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1491bcf3145fSNamhyung Kim return ret; 149235b9d88eSArnaldo Carvalho de Melo } 149335b9d88eSArnaldo Carvalho de Melo 149435b9d88eSArnaldo Carvalho de Melo return 0; 149535b9d88eSArnaldo Carvalho de Melo } 1496cb0b29e0SArnaldo Carvalho de Melo 14972a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample) 1498cb0b29e0SArnaldo Carvalho de Melo { 14993ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event); 150075562573SAdrian Hunter 150175562573SAdrian Hunter if (!evsel) 150275562573SAdrian Hunter return -EFAULT; 15036b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample(evsel, event, sample); 1504cb0b29e0SArnaldo Carvalho de Melo } 150578f067b3SArnaldo Carvalho de Melo 15062a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp) 150701468120SJiri Olsa { 15083ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event); 150901468120SJiri Olsa 151001468120SJiri Olsa if (!evsel) 151101468120SJiri Olsa return -EFAULT; 15126b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample_timestamp(evsel, event, timestamp); 151301468120SJiri Olsa } 151401468120SJiri Olsa 1515d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size) 1516a8f23d8fSArnaldo Carvalho de Melo { 1517a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1518c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1519a8f23d8fSArnaldo Carvalho de Melo 1520a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1521a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1522a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1523a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1524a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1525a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1526a8f23d8fSArnaldo Carvalho de Melo 15271a47245dSAdrian Hunter value = perf_event_paranoid(); 1528a8f23d8fSArnaldo Carvalho de Melo 1529a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1530a8f23d8fSArnaldo Carvalho de Melo 1531a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1532a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1533a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1534a8f23d8fSArnaldo Carvalho de Melo } 1535a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15365229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1537a8f23d8fSArnaldo Carvalho de Melo 1538a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15395229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 15405229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1541a8f23d8fSArnaldo Carvalho de Melo break; 1542d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1543515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1544d9aade7fSArnaldo Carvalho de Melo int max_freq; 1545d9aade7fSArnaldo Carvalho de Melo 1546d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1547d9aade7fSArnaldo Carvalho de Melo goto out_default; 1548d9aade7fSArnaldo Carvalho de Melo 15491fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1550d9aade7fSArnaldo Carvalho de Melo goto out_default; 1551d9aade7fSArnaldo Carvalho de Melo 1552d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1553d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1554d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1555d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 15561fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1557d9aade7fSArnaldo Carvalho de Melo break; 1558d9aade7fSArnaldo Carvalho de Melo } 1559a8f23d8fSArnaldo Carvalho de Melo default: 1560d9aade7fSArnaldo Carvalho de Melo out_default: 1561a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1562a8f23d8fSArnaldo Carvalho de Melo break; 1563a8f23d8fSArnaldo Carvalho de Melo } 1564a8f23d8fSArnaldo Carvalho de Melo 1565a8f23d8fSArnaldo Carvalho de Melo return 0; 1566a8f23d8fSArnaldo Carvalho de Melo } 1567a025e4f0SAdrian Hunter 1568d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1569956fa571SArnaldo Carvalho de Melo { 1570c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1571f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1572956fa571SArnaldo Carvalho de Melo 1573956fa571SArnaldo Carvalho de Melo switch (err) { 1574956fa571SArnaldo Carvalho de Melo case EPERM: 1575e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1576e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1577e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1578956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1579e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1580e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1581e965bea1SArnaldo Carvalho de Melo 1582e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1583e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1584e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1585e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1586e965bea1SArnaldo Carvalho de Melo } 1587e965bea1SArnaldo Carvalho de Melo 1588e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1589e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1590956fa571SArnaldo Carvalho de Melo break; 1591956fa571SArnaldo Carvalho de Melo default: 1592956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1593956fa571SArnaldo Carvalho de Melo break; 1594956fa571SArnaldo Carvalho de Melo } 1595956fa571SArnaldo Carvalho de Melo 1596956fa571SArnaldo Carvalho de Melo return 0; 1597956fa571SArnaldo Carvalho de Melo } 1598956fa571SArnaldo Carvalho de Melo 1599e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel) 1600a025e4f0SAdrian Hunter { 160132dcd021SJiri Olsa struct evsel *evsel, *n; 1602a025e4f0SAdrian Hunter LIST_HEAD(move); 1603a025e4f0SAdrian Hunter 1604515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1605a025e4f0SAdrian Hunter return; 1606a025e4f0SAdrian Hunter 1607e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1608a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1609b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1610a025e4f0SAdrian Hunter } 1611a025e4f0SAdrian Hunter 1612ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1613a025e4f0SAdrian Hunter } 161460b0896cSAdrian Hunter 1615e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist) 1616442ad225SAdrian Hunter { 1617442ad225SAdrian Hunter struct evsel *evsel; 1618442ad225SAdrian Hunter 1619442ad225SAdrian Hunter evlist__for_each_entry(evlist, evsel) { 1620442ad225SAdrian Hunter if (evsel->tracking) 1621442ad225SAdrian Hunter return evsel; 1622442ad225SAdrian Hunter } 1623442ad225SAdrian Hunter 1624442ad225SAdrian Hunter return evlist__first(evlist); 1625442ad225SAdrian Hunter } 1626442ad225SAdrian Hunter 1627e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel) 162860b0896cSAdrian Hunter { 162932dcd021SJiri Olsa struct evsel *evsel; 163060b0896cSAdrian Hunter 163160b0896cSAdrian Hunter if (tracking_evsel->tracking) 163260b0896cSAdrian Hunter return; 163360b0896cSAdrian Hunter 1634e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 163560b0896cSAdrian Hunter if (evsel != tracking_evsel) 163660b0896cSAdrian Hunter evsel->tracking = false; 163760b0896cSAdrian Hunter } 163860b0896cSAdrian Hunter 163960b0896cSAdrian Hunter tracking_evsel->tracking = true; 164060b0896cSAdrian Hunter } 16417630b3e2SWang Nan 1642b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str) 16437630b3e2SWang Nan { 164432dcd021SJiri Olsa struct evsel *evsel; 16457630b3e2SWang Nan 1646e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16477630b3e2SWang Nan if (!evsel->name) 16487630b3e2SWang Nan continue; 16497630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 16507630b3e2SWang Nan return evsel; 16517630b3e2SWang Nan } 16527630b3e2SWang Nan 16537630b3e2SWang Nan return NULL; 16547630b3e2SWang Nan } 165554cc54deSWang Nan 1656ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state) 165754cc54deSWang Nan { 165854cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 165954cc54deSWang Nan enum action { 166054cc54deSWang Nan NONE, 166154cc54deSWang Nan PAUSE, 166254cc54deSWang Nan RESUME, 166354cc54deSWang Nan } action = NONE; 166454cc54deSWang Nan 16650b72d69aSWang Nan if (!evlist->overwrite_mmap) 166654cc54deSWang Nan return; 166754cc54deSWang Nan 166854cc54deSWang Nan switch (old_state) { 166954cc54deSWang Nan case BKW_MMAP_NOTREADY: { 167054cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1671dd8bd53aSLuis de Bethencourt goto state_err; 167254cc54deSWang Nan break; 167354cc54deSWang Nan } 167454cc54deSWang Nan case BKW_MMAP_RUNNING: { 167554cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 167654cc54deSWang Nan goto state_err; 167754cc54deSWang Nan action = PAUSE; 167854cc54deSWang Nan break; 167954cc54deSWang Nan } 168054cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 168154cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 168254cc54deSWang Nan goto state_err; 168354cc54deSWang Nan break; 168454cc54deSWang Nan } 168554cc54deSWang Nan case BKW_MMAP_EMPTY: { 168654cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 168754cc54deSWang Nan goto state_err; 168854cc54deSWang Nan action = RESUME; 168954cc54deSWang Nan break; 169054cc54deSWang Nan } 169154cc54deSWang Nan default: 169254cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 169354cc54deSWang Nan } 169454cc54deSWang Nan 169554cc54deSWang Nan evlist->bkw_mmap_state = state; 169654cc54deSWang Nan 169754cc54deSWang Nan switch (action) { 169854cc54deSWang Nan case PAUSE: 16990a60b339SArnaldo Carvalho de Melo evlist__pause(evlist); 170054cc54deSWang Nan break; 170154cc54deSWang Nan case RESUME: 17020a60b339SArnaldo Carvalho de Melo evlist__resume(evlist); 170354cc54deSWang Nan break; 170454cc54deSWang Nan case NONE: 170554cc54deSWang Nan default: 170654cc54deSWang Nan break; 170754cc54deSWang Nan } 170854cc54deSWang Nan 170954cc54deSWang Nan state_err: 171054cc54deSWang Nan return; 171154cc54deSWang Nan } 171207d6f446SArnaldo Carvalho de Melo 171378e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist) 171407d6f446SArnaldo Carvalho de Melo { 171532dcd021SJiri Olsa struct evsel *evsel; 171607d6f446SArnaldo Carvalho de Melo 171707d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 17181fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 171907d6f446SArnaldo Carvalho de Melo return false; 172007d6f446SArnaldo Carvalho de Melo } 172107d6f446SArnaldo Carvalho de Melo 172207d6f446SArnaldo Carvalho de Melo return true; 172307d6f446SArnaldo Carvalho de Melo } 1724e2bdbe80SJin Yao 1725e2bdbe80SJin Yao /* 1726e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1727e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1728e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1729e2bdbe80SJin Yao */ 173064b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist) 1731e2bdbe80SJin Yao { 1732e2bdbe80SJin Yao if (!evlist->nr_groups) { 1733515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1734e2bdbe80SJin Yao 1735a622eafaSArnaldo Carvalho de Melo evlist__set_leader(evlist); 1736e2bdbe80SJin Yao leader->forced_leader = true; 1737e2bdbe80SJin Yao } 1738e2bdbe80SJin Yao } 1739c3537fc2SAndi Kleen 174064b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close) 1741c3537fc2SAndi Kleen { 174232dcd021SJiri Olsa struct evsel *c2, *leader; 1743c3537fc2SAndi Kleen bool is_open = true; 1744c3537fc2SAndi Kleen 1745c3537fc2SAndi Kleen leader = evsel->leader; 1746c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 17475643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1748c3537fc2SAndi Kleen 1749c3537fc2SAndi Kleen /* 1750c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1751c3537fc2SAndi Kleen * include the first entry. 1752c3537fc2SAndi Kleen */ 1753c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1754c3537fc2SAndi Kleen if (c2 == evsel) 1755c3537fc2SAndi Kleen is_open = false; 1756c3537fc2SAndi Kleen if (c2->leader == leader) { 17574804e011SAndi Kleen if (is_open && close) 17585a40e199SAndi Kleen perf_evsel__close(&c2->core); 1759c3537fc2SAndi Kleen c2->leader = c2; 17605643b1a5SJiri Olsa c2->core.nr_members = 0; 17614804e011SAndi Kleen /* 17624804e011SAndi Kleen * Set this for all former members of the group 17634804e011SAndi Kleen * to indicate they get reopened. 17644804e011SAndi Kleen */ 17654804e011SAndi Kleen c2->reset_group = true; 1766c3537fc2SAndi Kleen } 1767c3537fc2SAndi Kleen } 1768c3537fc2SAndi Kleen return leader; 1769c3537fc2SAndi Kleen } 1770ec886bf5SAlexey Budankov 1771a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1772a8fcbd26SAdrian Hunter { 1773a8fcbd26SAdrian Hunter char *s, *p; 1774a8fcbd26SAdrian Hunter int ret = 0, fd; 1775a8fcbd26SAdrian Hunter 1776a8fcbd26SAdrian Hunter if (strncmp(str, "fifo:", 5)) 1777a8fcbd26SAdrian Hunter return -EINVAL; 1778a8fcbd26SAdrian Hunter 1779a8fcbd26SAdrian Hunter str += 5; 1780a8fcbd26SAdrian Hunter if (!*str || *str == ',') 1781a8fcbd26SAdrian Hunter return -EINVAL; 1782a8fcbd26SAdrian Hunter 1783a8fcbd26SAdrian Hunter s = strdup(str); 1784a8fcbd26SAdrian Hunter if (!s) 1785a8fcbd26SAdrian Hunter return -ENOMEM; 1786a8fcbd26SAdrian Hunter 1787a8fcbd26SAdrian Hunter p = strchr(s, ','); 1788a8fcbd26SAdrian Hunter if (p) 1789a8fcbd26SAdrian Hunter *p = '\0'; 1790a8fcbd26SAdrian Hunter 1791a8fcbd26SAdrian Hunter /* 1792a8fcbd26SAdrian Hunter * O_RDWR avoids POLLHUPs which is necessary to allow the other 1793a8fcbd26SAdrian Hunter * end of a FIFO to be repeatedly opened and closed. 1794a8fcbd26SAdrian Hunter */ 1795a8fcbd26SAdrian Hunter fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1796a8fcbd26SAdrian Hunter if (fd < 0) { 1797a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", s); 1798a8fcbd26SAdrian Hunter ret = -errno; 1799a8fcbd26SAdrian Hunter goto out_free; 1800a8fcbd26SAdrian Hunter } 1801a8fcbd26SAdrian Hunter *ctl_fd = fd; 1802a8fcbd26SAdrian Hunter *ctl_fd_close = true; 1803a8fcbd26SAdrian Hunter 1804a8fcbd26SAdrian Hunter if (p && *++p) { 1805a8fcbd26SAdrian Hunter /* O_RDWR | O_NONBLOCK means the other end need not be open */ 1806a8fcbd26SAdrian Hunter fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1807a8fcbd26SAdrian Hunter if (fd < 0) { 1808a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", p); 1809a8fcbd26SAdrian Hunter ret = -errno; 1810a8fcbd26SAdrian Hunter goto out_free; 1811a8fcbd26SAdrian Hunter } 1812a8fcbd26SAdrian Hunter *ctl_fd_ack = fd; 1813a8fcbd26SAdrian Hunter } 1814a8fcbd26SAdrian Hunter 1815a8fcbd26SAdrian Hunter out_free: 1816a8fcbd26SAdrian Hunter free(s); 1817a8fcbd26SAdrian Hunter return ret; 1818a8fcbd26SAdrian Hunter } 1819a8fcbd26SAdrian Hunter 1820a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 18219864a66dSAdrian Hunter { 18229864a66dSAdrian Hunter char *comma = NULL, *endptr = NULL; 18239864a66dSAdrian Hunter 1824a8fcbd26SAdrian Hunter *ctl_fd_close = false; 1825a8fcbd26SAdrian Hunter 18269864a66dSAdrian Hunter if (strncmp(str, "fd:", 3)) 1827a8fcbd26SAdrian Hunter return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close); 18289864a66dSAdrian Hunter 18299864a66dSAdrian Hunter *ctl_fd = strtoul(&str[3], &endptr, 0); 18309864a66dSAdrian Hunter if (endptr == &str[3]) 18319864a66dSAdrian Hunter return -EINVAL; 18329864a66dSAdrian Hunter 18339864a66dSAdrian Hunter comma = strchr(str, ','); 18349864a66dSAdrian Hunter if (comma) { 18359864a66dSAdrian Hunter if (endptr != comma) 18369864a66dSAdrian Hunter return -EINVAL; 18379864a66dSAdrian Hunter 18389864a66dSAdrian Hunter *ctl_fd_ack = strtoul(comma + 1, &endptr, 0); 18399864a66dSAdrian Hunter if (endptr == comma + 1 || *endptr != '\0') 18409864a66dSAdrian Hunter return -EINVAL; 18419864a66dSAdrian Hunter } 18429864a66dSAdrian Hunter 18439864a66dSAdrian Hunter return 0; 18449864a66dSAdrian Hunter } 18459864a66dSAdrian Hunter 1846ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close) 1847ee7fe31eSAdrian Hunter { 1848ee7fe31eSAdrian Hunter if (*ctl_fd_close) { 1849ee7fe31eSAdrian Hunter *ctl_fd_close = false; 1850ee7fe31eSAdrian Hunter close(ctl_fd); 1851ee7fe31eSAdrian Hunter if (ctl_fd_ack >= 0) 1852ee7fe31eSAdrian Hunter close(ctl_fd_ack); 1853ee7fe31eSAdrian Hunter } 1854ee7fe31eSAdrian Hunter } 1855ee7fe31eSAdrian Hunter 1856ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack) 1857ec886bf5SAlexey Budankov { 1858ec886bf5SAlexey Budankov if (fd == -1) { 1859ec886bf5SAlexey Budankov pr_debug("Control descriptor is not initialized\n"); 1860ec886bf5SAlexey Budankov return 0; 1861ec886bf5SAlexey Budankov } 1862ec886bf5SAlexey Budankov 1863ec886bf5SAlexey Budankov evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 1864ec886bf5SAlexey Budankov fdarray_flag__nonfilterable); 1865ec886bf5SAlexey Budankov if (evlist->ctl_fd.pos < 0) { 1866ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1867ec886bf5SAlexey Budankov pr_err("Failed to add ctl fd entry: %m\n"); 1868ec886bf5SAlexey Budankov return -1; 1869ec886bf5SAlexey Budankov } 1870ec886bf5SAlexey Budankov 1871ec886bf5SAlexey Budankov evlist->ctl_fd.fd = fd; 1872ec886bf5SAlexey Budankov evlist->ctl_fd.ack = ack; 1873ec886bf5SAlexey Budankov 1874ec886bf5SAlexey Budankov return 0; 1875ec886bf5SAlexey Budankov } 1876ec886bf5SAlexey Budankov 1877ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist) 1878ec886bf5SAlexey Budankov { 1879ec886bf5SAlexey Budankov return evlist->ctl_fd.pos >= 0; 1880ec886bf5SAlexey Budankov } 1881ec886bf5SAlexey Budankov 1882ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist) 1883ec886bf5SAlexey Budankov { 1884ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 1885ec886bf5SAlexey Budankov 1886ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist)) 1887ec886bf5SAlexey Budankov return 0; 1888ec886bf5SAlexey Budankov 1889ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].fd = -1; 1890ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].events = 0; 1891ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].revents = 0; 1892ec886bf5SAlexey Budankov 1893ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1894ec886bf5SAlexey Budankov evlist->ctl_fd.ack = -1; 1895ec886bf5SAlexey Budankov evlist->ctl_fd.fd = -1; 1896ec886bf5SAlexey Budankov 1897ec886bf5SAlexey Budankov return 0; 1898ec886bf5SAlexey Budankov } 1899ec886bf5SAlexey Budankov 1900ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd, 1901ec886bf5SAlexey Budankov char *cmd_data, size_t data_size) 1902ec886bf5SAlexey Budankov { 1903ec886bf5SAlexey Budankov int err; 1904ec886bf5SAlexey Budankov char c; 1905ec886bf5SAlexey Budankov size_t bytes_read = 0; 1906ec886bf5SAlexey Budankov 190740db8ff5SAdrian Hunter *cmd = EVLIST_CTL_CMD_UNSUPPORTED; 1908ec886bf5SAlexey Budankov memset(cmd_data, 0, data_size); 1909ec886bf5SAlexey Budankov data_size--; 1910ec886bf5SAlexey Budankov 1911ec886bf5SAlexey Budankov do { 1912ec886bf5SAlexey Budankov err = read(evlist->ctl_fd.fd, &c, 1); 1913ec886bf5SAlexey Budankov if (err > 0) { 1914ec886bf5SAlexey Budankov if (c == '\n' || c == '\0') 1915ec886bf5SAlexey Budankov break; 1916ec886bf5SAlexey Budankov cmd_data[bytes_read++] = c; 1917ec886bf5SAlexey Budankov if (bytes_read == data_size) 1918ec886bf5SAlexey Budankov break; 191940db8ff5SAdrian Hunter continue; 192040db8ff5SAdrian Hunter } else if (err == -1) { 192140db8ff5SAdrian Hunter if (errno == EINTR) 192240db8ff5SAdrian Hunter continue; 192340db8ff5SAdrian Hunter if (errno == EAGAIN || errno == EWOULDBLOCK) 192440db8ff5SAdrian Hunter err = 0; 192540db8ff5SAdrian Hunter else 1926ec886bf5SAlexey Budankov pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd); 1927ec886bf5SAlexey Budankov } 192840db8ff5SAdrian Hunter break; 1929ec886bf5SAlexey Budankov } while (1); 1930ec886bf5SAlexey Budankov 1931ec886bf5SAlexey Budankov pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data, 1932ec886bf5SAlexey Budankov bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0"); 1933ec886bf5SAlexey Budankov 193440db8ff5SAdrian Hunter if (bytes_read > 0) { 1935ec886bf5SAlexey Budankov if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG, 1936ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) { 1937ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_ENABLE; 1938ec886bf5SAlexey Budankov } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG, 1939ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) { 1940ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_DISABLE; 1941d20aff15SAdrian Hunter } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG, 1942d20aff15SAdrian Hunter (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) { 1943d20aff15SAdrian Hunter *cmd = EVLIST_CTL_CMD_SNAPSHOT; 1944d20aff15SAdrian Hunter pr_debug("is snapshot\n"); 1945142544a9SJiri Olsa } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG, 1946142544a9SJiri Olsa (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) { 1947142544a9SJiri Olsa *cmd = EVLIST_CTL_CMD_EVLIST; 1948f186cd61SJiri Olsa } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG, 1949f186cd61SJiri Olsa (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) { 1950f186cd61SJiri Olsa *cmd = EVLIST_CTL_CMD_STOP; 195147fddcb4SJiri Olsa } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG, 195247fddcb4SJiri Olsa (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) { 195347fddcb4SJiri Olsa *cmd = EVLIST_CTL_CMD_PING; 1954ec886bf5SAlexey Budankov } 1955ec886bf5SAlexey Budankov } 1956ec886bf5SAlexey Budankov 195740db8ff5SAdrian Hunter return bytes_read ? (int)bytes_read : err; 1958ec886bf5SAlexey Budankov } 1959ec886bf5SAlexey Budankov 1960d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist) 1961ec886bf5SAlexey Budankov { 1962ec886bf5SAlexey Budankov int err; 1963ec886bf5SAlexey Budankov 1964ec886bf5SAlexey Budankov if (evlist->ctl_fd.ack == -1) 1965ec886bf5SAlexey Budankov return 0; 1966ec886bf5SAlexey Budankov 1967ec886bf5SAlexey Budankov err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG, 1968ec886bf5SAlexey Budankov sizeof(EVLIST_CTL_CMD_ACK_TAG)); 1969ec886bf5SAlexey Budankov if (err == -1) 1970ec886bf5SAlexey Budankov pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack); 1971ec886bf5SAlexey Budankov 1972ec886bf5SAlexey Budankov return err; 1973ec886bf5SAlexey Budankov } 1974ec886bf5SAlexey Budankov 1975991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg) 1976991ae4ebSJiri Olsa { 1977991ae4ebSJiri Olsa char *data = cmd_data + cmd_size; 1978991ae4ebSJiri Olsa 1979991ae4ebSJiri Olsa /* no argument */ 1980991ae4ebSJiri Olsa if (!*data) 1981991ae4ebSJiri Olsa return 0; 1982991ae4ebSJiri Olsa 1983991ae4ebSJiri Olsa /* there's argument */ 1984991ae4ebSJiri Olsa if (*data == ' ') { 1985991ae4ebSJiri Olsa *arg = data + 1; 1986991ae4ebSJiri Olsa return 1; 1987991ae4ebSJiri Olsa } 1988991ae4ebSJiri Olsa 1989991ae4ebSJiri Olsa /* malformed */ 1990991ae4ebSJiri Olsa return -1; 1991991ae4ebSJiri Olsa } 1992991ae4ebSJiri Olsa 1993991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable) 1994991ae4ebSJiri Olsa { 1995991ae4ebSJiri Olsa struct evsel *evsel; 1996991ae4ebSJiri Olsa char *name; 1997991ae4ebSJiri Olsa int err; 1998991ae4ebSJiri Olsa 1999991ae4ebSJiri Olsa err = get_cmd_arg(cmd_data, 2000991ae4ebSJiri Olsa enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 : 2001991ae4ebSJiri Olsa sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1, 2002991ae4ebSJiri Olsa &name); 2003991ae4ebSJiri Olsa if (err < 0) { 2004991ae4ebSJiri Olsa pr_info("failed: wrong command\n"); 2005991ae4ebSJiri Olsa return -1; 2006991ae4ebSJiri Olsa } 2007991ae4ebSJiri Olsa 2008991ae4ebSJiri Olsa if (err) { 2009991ae4ebSJiri Olsa evsel = evlist__find_evsel_by_str(evlist, name); 2010991ae4ebSJiri Olsa if (evsel) { 2011991ae4ebSJiri Olsa if (enable) 2012991ae4ebSJiri Olsa evlist__enable_evsel(evlist, name); 2013991ae4ebSJiri Olsa else 2014991ae4ebSJiri Olsa evlist__disable_evsel(evlist, name); 2015991ae4ebSJiri Olsa pr_info("Event %s %s\n", evsel->name, 2016991ae4ebSJiri Olsa enable ? "enabled" : "disabled"); 2017991ae4ebSJiri Olsa } else { 2018991ae4ebSJiri Olsa pr_info("failed: can't find '%s' event\n", name); 2019991ae4ebSJiri Olsa } 2020991ae4ebSJiri Olsa } else { 2021991ae4ebSJiri Olsa if (enable) { 2022991ae4ebSJiri Olsa evlist__enable(evlist); 2023991ae4ebSJiri Olsa pr_info(EVLIST_ENABLED_MSG); 2024991ae4ebSJiri Olsa } else { 2025991ae4ebSJiri Olsa evlist__disable(evlist); 2026991ae4ebSJiri Olsa pr_info(EVLIST_DISABLED_MSG); 2027991ae4ebSJiri Olsa } 2028991ae4ebSJiri Olsa } 2029991ae4ebSJiri Olsa 2030991ae4ebSJiri Olsa return 0; 2031991ae4ebSJiri Olsa } 2032991ae4ebSJiri Olsa 2033142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data) 2034142544a9SJiri Olsa { 2035142544a9SJiri Olsa struct perf_attr_details details = { .verbose = false, }; 2036142544a9SJiri Olsa struct evsel *evsel; 2037142544a9SJiri Olsa char *arg; 2038142544a9SJiri Olsa int err; 2039142544a9SJiri Olsa 2040142544a9SJiri Olsa err = get_cmd_arg(cmd_data, 2041142544a9SJiri Olsa sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1, 2042142544a9SJiri Olsa &arg); 2043142544a9SJiri Olsa if (err < 0) { 2044142544a9SJiri Olsa pr_info("failed: wrong command\n"); 2045142544a9SJiri Olsa return -1; 2046142544a9SJiri Olsa } 2047142544a9SJiri Olsa 2048142544a9SJiri Olsa if (err) { 2049142544a9SJiri Olsa if (!strcmp(arg, "-v")) { 2050142544a9SJiri Olsa details.verbose = true; 2051142544a9SJiri Olsa } else if (!strcmp(arg, "-g")) { 2052142544a9SJiri Olsa details.event_group = true; 2053142544a9SJiri Olsa } else if (!strcmp(arg, "-F")) { 2054142544a9SJiri Olsa details.freq = true; 2055142544a9SJiri Olsa } else { 2056142544a9SJiri Olsa pr_info("failed: wrong command\n"); 2057142544a9SJiri Olsa return -1; 2058142544a9SJiri Olsa } 2059142544a9SJiri Olsa } 2060142544a9SJiri Olsa 2061142544a9SJiri Olsa evlist__for_each_entry(evlist, evsel) 2062142544a9SJiri Olsa evsel__fprintf(evsel, &details, stderr); 2063142544a9SJiri Olsa 2064142544a9SJiri Olsa return 0; 2065142544a9SJiri Olsa } 2066142544a9SJiri Olsa 2067ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd) 2068ec886bf5SAlexey Budankov { 2069ec886bf5SAlexey Budankov int err = 0; 2070ec886bf5SAlexey Budankov char cmd_data[EVLIST_CTL_CMD_MAX_LEN]; 2071ec886bf5SAlexey Budankov int ctlfd_pos = evlist->ctl_fd.pos; 2072ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 2073ec886bf5SAlexey Budankov 2074ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents) 2075ec886bf5SAlexey Budankov return 0; 2076ec886bf5SAlexey Budankov 2077ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & POLLIN) { 2078ec886bf5SAlexey Budankov err = evlist__ctlfd_recv(evlist, cmd, cmd_data, 2079ec886bf5SAlexey Budankov EVLIST_CTL_CMD_MAX_LEN); 2080ec886bf5SAlexey Budankov if (err > 0) { 2081ec886bf5SAlexey Budankov switch (*cmd) { 2082ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ENABLE: 2083ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_DISABLE: 2084991ae4ebSJiri Olsa err = evlist__ctlfd_enable(evlist, cmd_data, 2085991ae4ebSJiri Olsa *cmd == EVLIST_CTL_CMD_ENABLE); 2086ec886bf5SAlexey Budankov break; 2087142544a9SJiri Olsa case EVLIST_CTL_CMD_EVLIST: 2088142544a9SJiri Olsa err = evlist__ctlfd_list(evlist, cmd_data); 2089142544a9SJiri Olsa break; 2090d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 2091f186cd61SJiri Olsa case EVLIST_CTL_CMD_STOP: 209247fddcb4SJiri Olsa case EVLIST_CTL_CMD_PING: 2093d20aff15SAdrian Hunter break; 2094ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ACK: 2095ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 2096ec886bf5SAlexey Budankov default: 2097ec886bf5SAlexey Budankov pr_debug("ctlfd: unsupported %d\n", *cmd); 2098ec886bf5SAlexey Budankov break; 2099ec886bf5SAlexey Budankov } 2100d20aff15SAdrian Hunter if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED || 2101d20aff15SAdrian Hunter *cmd == EVLIST_CTL_CMD_SNAPSHOT)) 2102ec886bf5SAlexey Budankov evlist__ctlfd_ack(evlist); 2103ec886bf5SAlexey Budankov } 2104ec886bf5SAlexey Budankov } 2105ec886bf5SAlexey Budankov 2106ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR)) 2107ec886bf5SAlexey Budankov evlist__finalize_ctlfd(evlist); 2108ec886bf5SAlexey Budankov else 2109ec886bf5SAlexey Budankov entries[ctlfd_pos].revents = 0; 2110ec886bf5SAlexey Budankov 2111ec886bf5SAlexey Budankov return err; 2112ec886bf5SAlexey Budankov } 2113b214ba8cSNamhyung Kim 2114b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx) 2115b214ba8cSNamhyung Kim { 2116b214ba8cSNamhyung Kim struct evsel *evsel; 2117b214ba8cSNamhyung Kim 2118b214ba8cSNamhyung Kim evlist__for_each_entry(evlist, evsel) { 2119b214ba8cSNamhyung Kim if (evsel->idx == idx) 2120b214ba8cSNamhyung Kim return evsel; 2121b214ba8cSNamhyung Kim } 2122b214ba8cSNamhyung Kim return NULL; 2123b214ba8cSNamhyung Kim } 2124