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" 27*142544a9SJiri 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 307b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 308ee29be62SArnaldo Carvalho de Melo { 30932dcd021SJiri Olsa struct evsel *evsel; 310ee29be62SArnaldo Carvalho de Melo 311e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3121fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3131fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 314ee29be62SArnaldo Carvalho de Melo return evsel; 315ee29be62SArnaldo Carvalho de Melo } 316ee29be62SArnaldo Carvalho de Melo 317ee29be62SArnaldo Carvalho de Melo return NULL; 318ee29be62SArnaldo Carvalho de Melo } 319ee29be62SArnaldo Carvalho de Melo 320b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name) 321a2f2804aSDavid Ahern { 32232dcd021SJiri Olsa struct evsel *evsel; 323a2f2804aSDavid Ahern 324e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3251fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 326a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 327a2f2804aSDavid Ahern return evsel; 328a2f2804aSDavid Ahern } 329a2f2804aSDavid Ahern 330a2f2804aSDavid Ahern return NULL; 331a2f2804aSDavid Ahern } 332a2f2804aSDavid Ahern 333e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler) 33439876e7dSArnaldo Carvalho de Melo { 3358f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp(sys, name); 33639876e7dSArnaldo Carvalho de Melo 3378dd2a131SJiri Olsa if (IS_ERR(evsel)) 33839876e7dSArnaldo Carvalho de Melo return -1; 33939876e7dSArnaldo Carvalho de Melo 340744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 341a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 34239876e7dSArnaldo Carvalho de Melo return 0; 34339876e7dSArnaldo Carvalho de Melo } 34439876e7dSArnaldo Carvalho de Melo 345f63c2f5aSArnaldo Carvalho de Melo static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel) 346bf8e8f4bSAdrian Hunter { 347648b5af3SJiri Olsa if (evsel->core.system_wide) 348bf8e8f4bSAdrian Hunter return 1; 349bf8e8f4bSAdrian Hunter else 350a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 351bf8e8f4bSAdrian Hunter } 352bf8e8f4bSAdrian Hunter 353a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist) 354a8cbe40fSAndi Kleen { 355a8cbe40fSAndi Kleen struct evsel *pos; 356a8cbe40fSAndi Kleen 357a8cbe40fSAndi Kleen /* 358a8cbe40fSAndi Kleen * Reset the per evsel cpu_iter. This is needed because 359a8cbe40fSAndi Kleen * each evsel's cpumap may have a different index space, 360a8cbe40fSAndi Kleen * and some operations need the index to modify 361a8cbe40fSAndi Kleen * the FD xyarray (e.g. open, close) 362a8cbe40fSAndi Kleen */ 363a8cbe40fSAndi Kleen evlist__for_each_entry(evlist, pos) 364a8cbe40fSAndi Kleen pos->cpu_iter = 0; 365a8cbe40fSAndi Kleen } 366a8cbe40fSAndi Kleen 367a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu) 368a8cbe40fSAndi Kleen { 369a8cbe40fSAndi Kleen if (ev->cpu_iter >= ev->core.cpus->nr) 370a8cbe40fSAndi Kleen return true; 371a8cbe40fSAndi Kleen if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu) 372a8cbe40fSAndi Kleen return true; 373a8cbe40fSAndi Kleen return false; 374a8cbe40fSAndi Kleen } 375a8cbe40fSAndi Kleen 376a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu) 377a8cbe40fSAndi Kleen { 378a8cbe40fSAndi Kleen if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) { 379a8cbe40fSAndi Kleen ev->cpu_iter++; 380a8cbe40fSAndi Kleen return false; 381a8cbe40fSAndi Kleen } 382a8cbe40fSAndi Kleen return true; 383a8cbe40fSAndi Kleen } 384a8cbe40fSAndi Kleen 3857cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name) 3867cfcd1e0SJiri Olsa { 3877cfcd1e0SJiri Olsa if (!evsel_name) 3887cfcd1e0SJiri Olsa return 0; 3897cfcd1e0SJiri Olsa if (evsel__is_dummy_event(pos)) 3907cfcd1e0SJiri Olsa return 1; 3917cfcd1e0SJiri Olsa return strcmp(pos->name, evsel_name); 3927cfcd1e0SJiri Olsa } 3937cfcd1e0SJiri Olsa 3947cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist) 3957cfcd1e0SJiri Olsa { 3967cfcd1e0SJiri Olsa struct evsel *pos; 3977cfcd1e0SJiri Olsa 3987cfcd1e0SJiri Olsa evlist__for_each_entry(evlist, pos) { 3997cfcd1e0SJiri Olsa if (!evsel__is_group_leader(pos) || !pos->core.fd) 4007cfcd1e0SJiri Olsa continue; 4017cfcd1e0SJiri Olsa /* If at least one event is enabled, evlist is enabled. */ 4027cfcd1e0SJiri Olsa if (!pos->disabled) 4037cfcd1e0SJiri Olsa return true; 4047cfcd1e0SJiri Olsa } 4057cfcd1e0SJiri Olsa return false; 4067cfcd1e0SJiri Olsa } 4077cfcd1e0SJiri Olsa 4087cfcd1e0SJiri Olsa static void __evlist__disable(struct evlist *evlist, char *evsel_name) 4094152ab37SArnaldo Carvalho de Melo { 41032dcd021SJiri Olsa struct evsel *pos; 411704e2f5bSAndi Kleen struct affinity affinity; 41287cf8360SAdrian Hunter int cpu, i, imm = 0; 41387cf8360SAdrian Hunter bool has_imm = false; 414704e2f5bSAndi Kleen 415704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 416704e2f5bSAndi Kleen return; 417704e2f5bSAndi Kleen 41887cf8360SAdrian Hunter /* Disable 'immediate' events last */ 41987cf8360SAdrian Hunter for (imm = 0; imm <= 1; imm++) { 420704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 421704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 4224152ab37SArnaldo Carvalho de Melo 423e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 4247cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name)) 4257cfcd1e0SJiri Olsa continue; 426704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 427704e2f5bSAndi Kleen continue; 428c754c382SArnaldo Carvalho de Melo if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd) 4293fe4430dSJiri Olsa continue; 43087cf8360SAdrian Hunter if (pos->immediate) 43187cf8360SAdrian Hunter has_imm = true; 43287cf8360SAdrian Hunter if (pos->immediate != imm) 43387cf8360SAdrian Hunter continue; 434704e2f5bSAndi Kleen evsel__disable_cpu(pos, pos->cpu_iter - 1); 435704e2f5bSAndi Kleen } 436704e2f5bSAndi Kleen } 43787cf8360SAdrian Hunter if (!has_imm) 43887cf8360SAdrian Hunter break; 43987cf8360SAdrian Hunter } 44087cf8360SAdrian Hunter 441704e2f5bSAndi Kleen affinity__cleanup(&affinity); 442704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 4437cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name)) 4447cfcd1e0SJiri Olsa continue; 445c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 446704e2f5bSAndi Kleen continue; 447704e2f5bSAndi Kleen pos->disabled = true; 4484152ab37SArnaldo Carvalho de Melo } 4492b56bcfbSArnaldo Carvalho de Melo 4507cfcd1e0SJiri Olsa /* 4517cfcd1e0SJiri Olsa * If we disabled only single event, we need to check 4527cfcd1e0SJiri Olsa * the enabled state of the evlist manually. 4537cfcd1e0SJiri Olsa */ 4547cfcd1e0SJiri Olsa if (evsel_name) 4557cfcd1e0SJiri Olsa evlist->enabled = evlist__is_enabled(evlist); 4567cfcd1e0SJiri Olsa else 4572b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 4584152ab37SArnaldo Carvalho de Melo } 4594152ab37SArnaldo Carvalho de Melo 4607cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist) 4617cfcd1e0SJiri Olsa { 4627cfcd1e0SJiri Olsa __evlist__disable(evlist, NULL); 4637cfcd1e0SJiri Olsa } 4647cfcd1e0SJiri Olsa 4657cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name) 4667cfcd1e0SJiri Olsa { 4677cfcd1e0SJiri Olsa __evlist__disable(evlist, evsel_name); 4687cfcd1e0SJiri Olsa } 4697cfcd1e0SJiri Olsa 4707cfcd1e0SJiri Olsa static void __evlist__enable(struct evlist *evlist, char *evsel_name) 471764e16a3SDavid Ahern { 47232dcd021SJiri Olsa struct evsel *pos; 473704e2f5bSAndi Kleen struct affinity affinity; 474704e2f5bSAndi Kleen int cpu, i; 475764e16a3SDavid Ahern 476704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 477704e2f5bSAndi Kleen return; 478704e2f5bSAndi Kleen 479704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 480704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 481704e2f5bSAndi Kleen 482704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 4837cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name)) 4847cfcd1e0SJiri Olsa continue; 485704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 486704e2f5bSAndi Kleen continue; 487c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 488704e2f5bSAndi Kleen continue; 489704e2f5bSAndi Kleen evsel__enable_cpu(pos, pos->cpu_iter - 1); 490704e2f5bSAndi Kleen } 491704e2f5bSAndi Kleen } 492704e2f5bSAndi Kleen affinity__cleanup(&affinity); 493e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 4947cfcd1e0SJiri Olsa if (evsel__strcmp(pos, evsel_name)) 4957cfcd1e0SJiri Olsa continue; 496c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 4973fe4430dSJiri Olsa continue; 498704e2f5bSAndi Kleen pos->disabled = false; 499764e16a3SDavid Ahern } 5002b56bcfbSArnaldo Carvalho de Melo 5017cfcd1e0SJiri Olsa /* 5027cfcd1e0SJiri Olsa * Even single event sets the 'enabled' for evlist, 5037cfcd1e0SJiri Olsa * so the toggle can work properly and toggle to 5047cfcd1e0SJiri Olsa * 'disabled' state. 5057cfcd1e0SJiri Olsa */ 5062b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 5072b56bcfbSArnaldo Carvalho de Melo } 5082b56bcfbSArnaldo Carvalho de Melo 5097cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist) 5107cfcd1e0SJiri Olsa { 5117cfcd1e0SJiri Olsa __evlist__enable(evlist, NULL); 5127cfcd1e0SJiri Olsa } 5137cfcd1e0SJiri Olsa 5147cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name) 5157cfcd1e0SJiri Olsa { 5167cfcd1e0SJiri Olsa __evlist__enable(evlist, evsel_name); 5177cfcd1e0SJiri Olsa } 5187cfcd1e0SJiri Olsa 519ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist) 5202b56bcfbSArnaldo Carvalho de Melo { 521e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 522764e16a3SDavid Ahern } 523764e16a3SDavid Ahern 52437b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu) 5251c65056cSAdrian Hunter { 52618ef15c6SArnaldo Carvalho de Melo int thread; 527f63c2f5aSArnaldo Carvalho de Melo int nr_threads = evlist__nr_threads(evlist, evsel); 5281c65056cSAdrian Hunter 5299dfcb759SJiri Olsa if (!evsel->core.fd) 5301c65056cSAdrian Hunter return -EINVAL; 5311c65056cSAdrian Hunter 5321c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 53318ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 5341c65056cSAdrian Hunter if (err) 5351c65056cSAdrian Hunter return err; 5361c65056cSAdrian Hunter } 5371c65056cSAdrian Hunter return 0; 5381c65056cSAdrian Hunter } 5391c65056cSAdrian Hunter 54037b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread) 5411c65056cSAdrian Hunter { 54218ef15c6SArnaldo Carvalho de Melo int cpu; 5436549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 5441c65056cSAdrian Hunter 5459dfcb759SJiri Olsa if (!evsel->core.fd) 5461c65056cSAdrian Hunter return -EINVAL; 5471c65056cSAdrian Hunter 5481c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 54918ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 5501c65056cSAdrian Hunter if (err) 5511c65056cSAdrian Hunter return err; 5521c65056cSAdrian Hunter } 5531c65056cSAdrian Hunter return 0; 5541c65056cSAdrian Hunter } 5551c65056cSAdrian Hunter 55637b01abeSArnaldo Carvalho de Melo int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx) 5571c65056cSAdrian Hunter { 558315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 5591c65056cSAdrian Hunter 5601c65056cSAdrian Hunter if (per_cpu_mmaps) 56137b01abeSArnaldo Carvalho de Melo return evlist__enable_event_cpu(evlist, evsel, idx); 56237b01abeSArnaldo Carvalho de Melo 56337b01abeSArnaldo Carvalho de Melo return evlist__enable_event_thread(evlist, evsel, idx); 5641c65056cSAdrian Hunter } 5651c65056cSAdrian Hunter 566f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 567e4b356b5SArnaldo Carvalho de Melo { 568ab4c1f9fSAlexey Budankov return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default); 569e4b356b5SArnaldo Carvalho de Melo } 5701b85337dSArnaldo Carvalho de Melo 571f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 5721ddec7f0SArnaldo Carvalho de Melo { 57384227cb1SJiri Olsa return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 5741ddec7f0SArnaldo Carvalho de Melo } 5751ddec7f0SArnaldo Carvalho de Melo 57680ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 577f66a889dSArnaldo Carvalho de Melo { 57880ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 579f66a889dSArnaldo Carvalho de Melo } 580f66a889dSArnaldo Carvalho de Melo 5813ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id) 58270db7533SArnaldo Carvalho de Melo { 58370db7533SArnaldo Carvalho de Melo struct hlist_head *head; 58470db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 58570db7533SArnaldo Carvalho de Melo int hash; 58670db7533SArnaldo Carvalho de Melo 58770db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5881d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 58970db7533SArnaldo Carvalho de Melo 590b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 59170db7533SArnaldo Carvalho de Melo if (sid->id == id) 592932a3594SJiri Olsa return sid; 593932a3594SJiri Olsa 594932a3594SJiri Olsa return NULL; 595932a3594SJiri Olsa } 596932a3594SJiri Olsa 5973ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id) 598932a3594SJiri Olsa { 599932a3594SJiri Olsa struct perf_sample_id *sid; 600932a3594SJiri Olsa 6016484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 602515dbe48SJiri Olsa return evlist__first(evlist); 603932a3594SJiri Olsa 6043ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id); 605932a3594SJiri Olsa if (sid) 60670c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 60730e68bccSNamhyung Kim 6088cedf3a5SArnaldo Carvalho de Melo if (!evlist__sample_id_all(evlist)) 609515dbe48SJiri Olsa return evlist__first(evlist); 61030e68bccSNamhyung Kim 61170db7533SArnaldo Carvalho de Melo return NULL; 61270db7533SArnaldo Carvalho de Melo } 61304391debSArnaldo Carvalho de Melo 6143ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id) 615dddcf6abSAdrian Hunter { 616dddcf6abSAdrian Hunter struct perf_sample_id *sid; 617dddcf6abSAdrian Hunter 618dddcf6abSAdrian Hunter if (!id) 619dddcf6abSAdrian Hunter return NULL; 620dddcf6abSAdrian Hunter 6213ccf8a7bSArnaldo Carvalho de Melo sid = evlist__id2sid(evlist, id); 622dddcf6abSAdrian Hunter if (sid) 62370c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 624dddcf6abSAdrian Hunter 625dddcf6abSAdrian Hunter return NULL; 626dddcf6abSAdrian Hunter } 627dddcf6abSAdrian Hunter 6283ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id) 62975562573SAdrian Hunter { 630b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 63175562573SAdrian Hunter ssize_t n; 63275562573SAdrian Hunter 63375562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 63475562573SAdrian Hunter 63575562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 63675562573SAdrian Hunter if (evlist->id_pos >= n) 63775562573SAdrian Hunter return -1; 63875562573SAdrian Hunter *id = array[evlist->id_pos]; 63975562573SAdrian Hunter } else { 64075562573SAdrian Hunter if (evlist->is_pos > n) 64175562573SAdrian Hunter return -1; 64275562573SAdrian Hunter n -= evlist->is_pos; 64375562573SAdrian Hunter *id = array[n]; 64475562573SAdrian Hunter } 64575562573SAdrian Hunter return 0; 64675562573SAdrian Hunter } 64775562573SAdrian Hunter 6483ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event) 64975562573SAdrian Hunter { 650515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 65175562573SAdrian Hunter struct hlist_head *head; 65275562573SAdrian Hunter struct perf_sample_id *sid; 65375562573SAdrian Hunter int hash; 65475562573SAdrian Hunter u64 id; 65575562573SAdrian Hunter 6566484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 65798be6966SAdrian Hunter return first; 65898be6966SAdrian Hunter 6591fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 66098be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 66198be6966SAdrian Hunter return first; 66275562573SAdrian Hunter 6633ccf8a7bSArnaldo Carvalho de Melo if (evlist__event2id(evlist, event, &id)) 66475562573SAdrian Hunter return NULL; 66575562573SAdrian Hunter 66675562573SAdrian Hunter /* Synthesized events have an id of zero */ 66775562573SAdrian Hunter if (!id) 66898be6966SAdrian Hunter return first; 66975562573SAdrian Hunter 67075562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 6711d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 67275562573SAdrian Hunter 67375562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 67475562573SAdrian Hunter if (sid->id == id) 67570c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 67675562573SAdrian Hunter } 67775562573SAdrian Hunter return NULL; 67875562573SAdrian Hunter } 67975562573SAdrian Hunter 6800a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value) 68165aea233SWang Nan { 68265aea233SWang Nan int i; 68365aea233SWang Nan 6840b72d69aSWang Nan if (!evlist->overwrite_mmap) 685078c3386SWang Nan return 0; 686078c3386SWang Nan 687c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6882cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 68965aea233SWang Nan int err; 69065aea233SWang Nan 69165aea233SWang Nan if (fd < 0) 69265aea233SWang Nan continue; 69365aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 69465aea233SWang Nan if (err) 69565aea233SWang Nan return err; 69665aea233SWang Nan } 69765aea233SWang Nan return 0; 69865aea233SWang Nan } 69965aea233SWang Nan 7000a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist) 70165aea233SWang Nan { 7020a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, true); 70365aea233SWang Nan } 70465aea233SWang Nan 7050a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist) 70665aea233SWang Nan { 7070a60b339SArnaldo Carvalho de Melo return evlist__set_paused(evlist, false); 70865aea233SWang Nan } 70965aea233SWang Nan 710db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 711f8a95309SArnaldo Carvalho de Melo { 712aece948fSArnaldo Carvalho de Melo int i; 713f8a95309SArnaldo Carvalho de Melo 714b2cb615dSWang Nan if (evlist->mmap) 715c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 71680e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 717b2cb615dSWang Nan 7180b72d69aSWang Nan if (evlist->overwrite_mmap) 719c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 72080e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 721a1f72618SWang Nan } 722aece948fSArnaldo Carvalho de Melo 723db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 724a1f72618SWang Nan { 725db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 72604662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 7270b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 728f8a95309SArnaldo Carvalho de Melo } 729f8a95309SArnaldo Carvalho de Melo 73080e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 73180e53d11SJiri Olsa { 73280e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 73380e53d11SJiri Olsa 73480e53d11SJiri Olsa mmap__munmap(m); 73580e53d11SJiri Olsa } 73680e53d11SJiri Olsa 737d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 7382c5f6d87SKan Liang bool overwrite) 739f8a95309SArnaldo Carvalho de Melo { 740d4c6fb36SWang Nan int i; 741a5830532SJiri Olsa struct mmap *map; 742d4c6fb36SWang Nan 743c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 7448db6d6b1SWang Nan if (!map) 7458db6d6b1SWang Nan return NULL; 746946ae1d4SWang Nan 747c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 7486eb65f7aSJiri Olsa struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 7496eb65f7aSJiri Olsa 7504738ca30SArnaldo Carvalho de Melo /* 7514738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 7526afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 7534738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 7544738ca30SArnaldo Carvalho de Melo * dropped. 7554738ca30SArnaldo Carvalho de Melo * 7564738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 7574738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 7584738ca30SArnaldo Carvalho de Melo */ 7596eb65f7aSJiri Olsa perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 7604738ca30SArnaldo Carvalho de Melo } 761353120b4SJiri Olsa 7628db6d6b1SWang Nan return map; 763f8a95309SArnaldo Carvalho de Melo } 764f8a95309SArnaldo Carvalho de Melo 7659abd2ab2SJiri Olsa static void 7669abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 7679abd2ab2SJiri Olsa struct perf_mmap_param *_mp, 7689abd2ab2SJiri Olsa int idx, bool per_cpu) 7699abd2ab2SJiri Olsa { 7709abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7719abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 7729abd2ab2SJiri Olsa 7739abd2ab2SJiri Olsa auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 7749abd2ab2SJiri Olsa } 7759abd2ab2SJiri Olsa 776bb1b1885SJiri Olsa static struct perf_mmap* 777bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 778bb1b1885SJiri Olsa { 779bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7803805e4f3SJiri Olsa struct mmap *maps; 781bb1b1885SJiri Olsa 7823805e4f3SJiri Olsa maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 783bb1b1885SJiri Olsa 784bb1b1885SJiri Olsa if (!maps) { 7853805e4f3SJiri Olsa maps = evlist__alloc_mmap(evlist, overwrite); 786bb1b1885SJiri Olsa if (!maps) 787bb1b1885SJiri Olsa return NULL; 788bb1b1885SJiri Olsa 7893805e4f3SJiri Olsa if (overwrite) { 790bb1b1885SJiri Olsa evlist->overwrite_mmap = maps; 791bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 792ade9d208SArnaldo Carvalho de Melo evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 7933805e4f3SJiri Olsa } else { 7943805e4f3SJiri Olsa evlist->mmap = maps; 795bb1b1885SJiri Olsa } 796bb1b1885SJiri Olsa } 797bb1b1885SJiri Olsa 798bb1b1885SJiri Olsa return &maps[idx].core; 799bb1b1885SJiri Olsa } 800bb1b1885SJiri Olsa 801b80132b1SJiri Olsa static int 802b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 803b80132b1SJiri Olsa int output, int cpu) 804b80132b1SJiri Olsa { 805b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core); 806b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 807b80132b1SJiri Olsa 808b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu); 809b80132b1SJiri Olsa } 810b80132b1SJiri Olsa 811f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 812994a1f78SJiri Olsa { 813f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 8148185e881SArnaldo Carvalho de Melo int max; 8158185e881SArnaldo Carvalho de Melo 8168185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 8178185e881SArnaldo Carvalho de Melo /* 8188185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 8198185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 8208185e881SArnaldo Carvalho de Melo * die yet... 8218185e881SArnaldo Carvalho de Melo */ 8228185e881SArnaldo Carvalho de Melo max = 512; 8238185e881SArnaldo Carvalho de Melo } else { 8248185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 8258185e881SArnaldo Carvalho de Melo } 8268185e881SArnaldo Carvalho de Melo 8278185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 8281be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 8291be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 830f5e7150cSArnaldo Carvalho de Melo 831f5e7150cSArnaldo Carvalho de Melo return pages; 832f5e7150cSArnaldo Carvalho de Melo } 833f5e7150cSArnaldo Carvalho de Melo 8349521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 835f5e7150cSArnaldo Carvalho de Melo { 836f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 837f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 838f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 839994a1f78SJiri Olsa return 0; 840994a1f78SJiri Olsa 841994a1f78SJiri Olsa return (pages + 1) * page_size; 842994a1f78SJiri Olsa } 843994a1f78SJiri Olsa 84433c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 84533c2dcfdSDavid Ahern unsigned long max) 846994a1f78SJiri Olsa { 8472fbe4abeSAdrian Hunter unsigned long pages, val; 84827050f53SJiri Olsa static struct parse_tag tags[] = { 84927050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 85027050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 85127050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 85227050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 85327050f53SJiri Olsa { .tag = 0 }, 85427050f53SJiri Olsa }; 855994a1f78SJiri Olsa 8568973504bSDavid Ahern if (str == NULL) 85733c2dcfdSDavid Ahern return -EINVAL; 8588973504bSDavid Ahern 85927050f53SJiri Olsa val = parse_tag_value(str, tags); 8602fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 86127050f53SJiri Olsa /* we got file size value */ 86227050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 86327050f53SJiri Olsa } else { 86427050f53SJiri Olsa /* we got pages count value */ 86527050f53SJiri Olsa char *eptr; 866994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 86733c2dcfdSDavid Ahern if (*eptr != '\0') 86833c2dcfdSDavid Ahern return -EINVAL; 86927050f53SJiri Olsa } 870994a1f78SJiri Olsa 8712bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 87233c2dcfdSDavid Ahern /* leave number of pages at 0 */ 8731dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 8749808143bSJiri Olsa char buf[100]; 8759808143bSJiri Olsa 87633c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 87791529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 8781dbfa938SAdrian Hunter if (!pages) 8791dbfa938SAdrian Hunter return -EINVAL; 8809808143bSJiri Olsa 8819808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 8829808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 8839808143bSJiri Olsa buf, pages); 8842fbe4abeSAdrian Hunter } 8852fbe4abeSAdrian Hunter 88633c2dcfdSDavid Ahern if (pages > max) 88733c2dcfdSDavid Ahern return -EINVAL; 88833c2dcfdSDavid Ahern 88933c2dcfdSDavid Ahern return pages; 89033c2dcfdSDavid Ahern } 89133c2dcfdSDavid Ahern 89225f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 89333c2dcfdSDavid Ahern { 89433c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 89533c2dcfdSDavid Ahern long pages; 89633c2dcfdSDavid Ahern 897f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 89833c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 89933c2dcfdSDavid Ahern 90033c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 90133c2dcfdSDavid Ahern if (pages < 0) { 90233c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 903994a1f78SJiri Olsa return -1; 904994a1f78SJiri Olsa } 905994a1f78SJiri Olsa 906994a1f78SJiri Olsa *mmap_pages = pages; 907994a1f78SJiri Olsa return 0; 908994a1f78SJiri Olsa } 909994a1f78SJiri Olsa 91025f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused) 911e9db1310SAdrian Hunter { 91225f84702SArnaldo Carvalho de Melo return __evlist__parse_mmap_pages(opt->value, str); 913e9db1310SAdrian Hunter } 914e9db1310SAdrian Hunter 915c83fa7f2SAdrian Hunter /** 9169521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 917c83fa7f2SAdrian Hunter * @evlist: list of events 918c83fa7f2SAdrian Hunter * @pages: map length in pages 919c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 920718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 921718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 922f8a95309SArnaldo Carvalho de Melo * 923c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 9249521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 925c83fa7f2SAdrian Hunter * automatically. 926f8a95309SArnaldo Carvalho de Melo * 927718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 928718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 929718c602dSAdrian Hunter * 930c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 931f8a95309SArnaldo Carvalho de Melo */ 9329521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 9337a276ff6SWang Nan unsigned int auxtrace_pages, 93451255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 93551255a8aSAlexey Budankov int comp_level) 936f8a95309SArnaldo Carvalho de Melo { 93771f566a3SWang Nan /* 93871f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 93971f566a3SWang Nan * Its value is decided by evsel's write_backward. 94071f566a3SWang Nan * So &mp should not be passed through const pointer. 94171f566a3SWang Nan */ 942e440979fSJiri Olsa struct mmap_params mp = { 943e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 944e440979fSJiri Olsa .affinity = affinity, 945e440979fSJiri Olsa .flush = flush, 946e440979fSJiri Olsa .comp_level = comp_level 947e440979fSJiri Olsa }; 948923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = { 9499abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx, 950bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get, 951b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap, 9529abd2ab2SJiri Olsa }; 95350a682ceSArnaldo Carvalho de Melo 954f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 955f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 956f8a95309SArnaldo Carvalho de Melo 957f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 958718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 959718c602dSAdrian Hunter 960923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 961f8a95309SArnaldo Carvalho de Melo } 9627e2ed097SArnaldo Carvalho de Melo 9639521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 964718c602dSAdrian Hunter { 9659521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 966718c602dSAdrian Hunter } 967718c602dSAdrian Hunter 9687748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target) 9697e2ed097SArnaldo Carvalho de Melo { 970147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 971f854839bSJiri Olsa struct perf_cpu_map *cpus; 9729749b90eSJiri Olsa struct perf_thread_map *threads; 9737e2ed097SArnaldo Carvalho de Melo 974147c508fSJin Yao /* 975147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 976147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 977147c508fSJin Yao * target->system_wide = true. 978147c508fSJin Yao * 979147c508fSJin Yao * If specify '--per-thread' only to perf record, 980147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 981147c508fSJin Yao * 982147c508fSJin Yao * So target->per_thread && target->system_wide is false. 983147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 984147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 985147c508fSJin Yao * current behavior. 986147c508fSJin Yao * 987147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 988147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 989147c508fSJin Yao * per-thread data. thread_map__new_str will call 990147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 991147c508fSJin Yao */ 99273c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 993147c508fSJin Yao all_threads); 99474bfd2b2SAdrian Hunter 99574bfd2b2SAdrian Hunter if (!threads) 9967e2ed097SArnaldo Carvalho de Melo return -1; 9977e2ed097SArnaldo Carvalho de Melo 9989c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 999397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 1000879d77d0SNamhyung Kim else 10019c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 10027e2ed097SArnaldo Carvalho de Melo 100374bfd2b2SAdrian Hunter if (!cpus) 10047e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 10057e2ed097SArnaldo Carvalho de Melo 1006ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 1007ec9a77a7SAdrian Hunter 1008453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1009d5bc056eSAdrian Hunter 1010bfd1b83dSNamhyung Kim /* as evlist now has references, put count here */ 1011bfd1b83dSNamhyung Kim perf_cpu_map__put(cpus); 1012bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 1013bfd1b83dSNamhyung Kim 1014d5bc056eSAdrian Hunter return 0; 10157e2ed097SArnaldo Carvalho de Melo 10167e2ed097SArnaldo Carvalho de Melo out_delete_threads: 10177836e52eSJiri Olsa perf_thread_map__put(threads); 10187e2ed097SArnaldo Carvalho de Melo return -1; 10197e2ed097SArnaldo Carvalho de Melo } 10207e2ed097SArnaldo Carvalho de Melo 102124bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 10220a102479SFrederic Weisbecker { 102332dcd021SJiri Olsa struct evsel *evsel; 1024745cefc5SArnaldo Carvalho de Melo int err = 0; 10250a102479SFrederic Weisbecker 1026e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1027745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 10280a102479SFrederic Weisbecker continue; 1029745cefc5SArnaldo Carvalho de Melo 1030d988d5eeSKan Liang /* 1031d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1032d988d5eeSKan Liang * So evlist and evsel should always be same. 1033d988d5eeSKan Liang */ 1034a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 103523d4aad4SArnaldo Carvalho de Melo if (err) { 103623d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1037745cefc5SArnaldo Carvalho de Melo break; 10380a102479SFrederic Weisbecker } 103923d4aad4SArnaldo Carvalho de Melo } 10400a102479SFrederic Weisbecker 1041745cefc5SArnaldo Carvalho de Melo return err; 1042745cefc5SArnaldo Carvalho de Melo } 1043745cefc5SArnaldo Carvalho de Melo 104424bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1045745cefc5SArnaldo Carvalho de Melo { 104632dcd021SJiri Olsa struct evsel *evsel; 1047745cefc5SArnaldo Carvalho de Melo int err = 0; 1048745cefc5SArnaldo Carvalho de Melo 104905cea449SArnaldo Carvalho de Melo if (filter == NULL) 105005cea449SArnaldo Carvalho de Melo return -1; 105105cea449SArnaldo Carvalho de Melo 1052e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 10531fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1054fdf14720SWang Nan continue; 1055fdf14720SWang Nan 1056ad681adfSArnaldo Carvalho de Melo err = evsel__set_filter(evsel, filter); 1057745cefc5SArnaldo Carvalho de Melo if (err) 1058745cefc5SArnaldo Carvalho de Melo break; 1059745cefc5SArnaldo Carvalho de Melo } 1060745cefc5SArnaldo Carvalho de Melo 1061745cefc5SArnaldo Carvalho de Melo return err; 10620a102479SFrederic Weisbecker } 106374429964SFrederic Weisbecker 106424bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter) 106553c92f73SArnaldo Carvalho de Melo { 106653c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 106753c92f73SArnaldo Carvalho de Melo int err = 0; 106853c92f73SArnaldo Carvalho de Melo 106953c92f73SArnaldo Carvalho de Melo if (filter == NULL) 107053c92f73SArnaldo Carvalho de Melo return -1; 107153c92f73SArnaldo Carvalho de Melo 107253c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 107353c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 107453c92f73SArnaldo Carvalho de Melo continue; 107553c92f73SArnaldo Carvalho de Melo 1076ad681adfSArnaldo Carvalho de Melo err = evsel__append_tp_filter(evsel, filter); 107753c92f73SArnaldo Carvalho de Melo if (err) 107853c92f73SArnaldo Carvalho de Melo break; 107953c92f73SArnaldo Carvalho de Melo } 108053c92f73SArnaldo Carvalho de Melo 108153c92f73SArnaldo Carvalho de Melo return err; 108253c92f73SArnaldo Carvalho de Melo } 108353c92f73SArnaldo Carvalho de Melo 1084da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1085cfd70a26SArnaldo Carvalho de Melo { 1086cfd70a26SArnaldo Carvalho de Melo char *filter; 1087be199adaSArnaldo Carvalho de Melo size_t i; 1088cfd70a26SArnaldo Carvalho de Melo 1089be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1090be199adaSArnaldo Carvalho de Melo if (i == 0) { 1091be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 109205cea449SArnaldo Carvalho de Melo return NULL; 1093be199adaSArnaldo Carvalho de Melo } else { 1094be199adaSArnaldo Carvalho de Melo char *tmp; 1095be199adaSArnaldo Carvalho de Melo 1096be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1097be199adaSArnaldo Carvalho de Melo goto out_free; 1098be199adaSArnaldo Carvalho de Melo 1099be199adaSArnaldo Carvalho de Melo free(filter); 1100be199adaSArnaldo Carvalho de Melo filter = tmp; 1101be199adaSArnaldo Carvalho de Melo } 1102be199adaSArnaldo Carvalho de Melo } 1103cfd70a26SArnaldo Carvalho de Melo 110405cea449SArnaldo Carvalho de Melo return filter; 1105be199adaSArnaldo Carvalho de Melo out_free: 1106cfd70a26SArnaldo Carvalho de Melo free(filter); 110705cea449SArnaldo Carvalho de Melo return NULL; 110805cea449SArnaldo Carvalho de Melo } 110905cea449SArnaldo Carvalho de Melo 111024bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 111105cea449SArnaldo Carvalho de Melo { 111205cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 111324bf91a7SArnaldo Carvalho de Melo int ret = evlist__set_tp_filter(evlist, filter); 111405cea449SArnaldo Carvalho de Melo 111505cea449SArnaldo Carvalho de Melo free(filter); 1116cfd70a26SArnaldo Carvalho de Melo return ret; 1117cfd70a26SArnaldo Carvalho de Melo } 1118cfd70a26SArnaldo Carvalho de Melo 111924bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1120be199adaSArnaldo Carvalho de Melo { 112124bf91a7SArnaldo Carvalho de Melo return evlist__set_tp_filter_pids(evlist, 1, &pid); 1122be199adaSArnaldo Carvalho de Melo } 1123be199adaSArnaldo Carvalho de Melo 112424bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 11251827ab5bSArnaldo Carvalho de Melo { 11261827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 112724bf91a7SArnaldo Carvalho de Melo int ret = evlist__append_tp_filter(evlist, filter); 11281827ab5bSArnaldo Carvalho de Melo 11291827ab5bSArnaldo Carvalho de Melo free(filter); 11301827ab5bSArnaldo Carvalho de Melo return ret; 11311827ab5bSArnaldo Carvalho de Melo } 11321827ab5bSArnaldo Carvalho de Melo 113324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 11341827ab5bSArnaldo Carvalho de Melo { 113524bf91a7SArnaldo Carvalho de Melo return evlist__append_tp_filter_pids(evlist, 1, &pid); 11361827ab5bSArnaldo Carvalho de Melo } 11371827ab5bSArnaldo Carvalho de Melo 1138b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist) 113974429964SFrederic Weisbecker { 114032dcd021SJiri Olsa struct evsel *pos; 1141c2a70653SArnaldo Carvalho de Melo 11426484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 114375562573SAdrian Hunter return true; 114475562573SAdrian Hunter 114575562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 114675562573SAdrian Hunter return false; 114775562573SAdrian Hunter 1148e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 114975562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 115075562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1151c2a70653SArnaldo Carvalho de Melo return false; 115274429964SFrederic Weisbecker } 115374429964SFrederic Weisbecker 1154c2a70653SArnaldo Carvalho de Melo return true; 1155c2a70653SArnaldo Carvalho de Melo } 1156c2a70653SArnaldo Carvalho de Melo 1157b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist) 1158c2a70653SArnaldo Carvalho de Melo { 115932dcd021SJiri Olsa struct evsel *evsel; 116075562573SAdrian Hunter 116175562573SAdrian Hunter if (evlist->combined_sample_type) 116275562573SAdrian Hunter return evlist->combined_sample_type; 116375562573SAdrian Hunter 1164e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11651fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 116675562573SAdrian Hunter 116775562573SAdrian Hunter return evlist->combined_sample_type; 116875562573SAdrian Hunter } 116975562573SAdrian Hunter 1170b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist) 117175562573SAdrian Hunter { 117275562573SAdrian Hunter evlist->combined_sample_type = 0; 1173b3c2cc2bSArnaldo Carvalho de Melo return __evlist__combined_sample_type(evlist); 1174c2a70653SArnaldo Carvalho de Melo } 1175c2a70653SArnaldo Carvalho de Melo 117692c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist) 117798df858eSAndi Kleen { 117832dcd021SJiri Olsa struct evsel *evsel; 117998df858eSAndi Kleen u64 branch_type = 0; 118098df858eSAndi Kleen 1181e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11821fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 118398df858eSAndi Kleen return branch_type; 118498df858eSAndi Kleen } 118598df858eSAndi Kleen 118678e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist) 11879ede473cSJiri Olsa { 1188515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 11891fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 11901fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 11919ede473cSJiri Olsa 1192e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 119394d3820fSAdrian Hunter if (read_format != pos->core.attr.read_format) { 119494d3820fSAdrian Hunter pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n", 119594d3820fSAdrian Hunter read_format, (u64)pos->core.attr.read_format); 119694d3820fSAdrian Hunter } 11979ede473cSJiri Olsa } 11989ede473cSJiri Olsa 11999ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 12009ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 12019ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 12029ede473cSJiri Olsa return false; 12039ede473cSJiri Olsa } 12049ede473cSJiri Olsa 12059ede473cSJiri Olsa return true; 12069ede473cSJiri Olsa } 12079ede473cSJiri Olsa 12081420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist) 120981e36bffSArnaldo Carvalho de Melo { 1210515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 121181e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 121281e36bffSArnaldo Carvalho de Melo u64 sample_type; 121381e36bffSArnaldo Carvalho de Melo u16 size = 0; 121481e36bffSArnaldo Carvalho de Melo 12151fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 121681e36bffSArnaldo Carvalho de Melo goto out; 121781e36bffSArnaldo Carvalho de Melo 12181fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 121981e36bffSArnaldo Carvalho de Melo 122081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 122181e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 122281e36bffSArnaldo Carvalho de Melo 122381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 122481e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 122581e36bffSArnaldo Carvalho de Melo 122681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 122781e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 122881e36bffSArnaldo Carvalho de Melo 122981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 123081e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 123181e36bffSArnaldo Carvalho de Melo 123281e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 123381e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 123475562573SAdrian Hunter 123575562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 123675562573SAdrian Hunter size += sizeof(data->id); 123781e36bffSArnaldo Carvalho de Melo out: 123881e36bffSArnaldo Carvalho de Melo return size; 123981e36bffSArnaldo Carvalho de Melo } 124081e36bffSArnaldo Carvalho de Melo 12418cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist) 1242c2a70653SArnaldo Carvalho de Melo { 1243515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1244c2a70653SArnaldo Carvalho de Melo 1245e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 12461fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1247c2a70653SArnaldo Carvalho de Melo return false; 1248c2a70653SArnaldo Carvalho de Melo } 1249c2a70653SArnaldo Carvalho de Melo 1250c2a70653SArnaldo Carvalho de Melo return true; 125174429964SFrederic Weisbecker } 125274429964SFrederic Weisbecker 12538cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist) 125474429964SFrederic Weisbecker { 1255515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 12561fc632ceSJiri Olsa return first->core.attr.sample_id_all; 125774429964SFrederic Weisbecker } 125881cce8deSArnaldo Carvalho de Melo 1259900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel) 126081cce8deSArnaldo Carvalho de Melo { 126181cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 126281cce8deSArnaldo Carvalho de Melo } 1263727ab04eSArnaldo Carvalho de Melo 1264750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1265a74b4b66SNamhyung Kim { 126632dcd021SJiri Olsa struct evsel *evsel; 12677736627bSAndi Kleen struct affinity affinity; 12687736627bSAndi Kleen int cpu, i; 1269a74b4b66SNamhyung Kim 12707736627bSAndi Kleen /* 12717736627bSAndi Kleen * With perf record core.cpus is usually NULL. 12727736627bSAndi Kleen * Use the old method to handle this for now. 12737736627bSAndi Kleen */ 12747736627bSAndi Kleen if (!evlist->core.cpus) { 1275475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 127688761fa1SJiri Olsa evsel__close(evsel); 12777736627bSAndi Kleen return; 12787736627bSAndi Kleen } 12797736627bSAndi Kleen 12807736627bSAndi Kleen if (affinity__setup(&affinity) < 0) 12817736627bSAndi Kleen return; 12827736627bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 12837736627bSAndi Kleen affinity__set(&affinity, cpu); 12847736627bSAndi Kleen 12857736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12867736627bSAndi Kleen if (evsel__cpu_iter_skip(evsel, cpu)) 12877736627bSAndi Kleen continue; 12887736627bSAndi Kleen perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1); 12897736627bSAndi Kleen } 12907736627bSAndi Kleen } 12917736627bSAndi Kleen affinity__cleanup(&affinity); 12927736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12937736627bSAndi Kleen perf_evsel__free_fd(&evsel->core); 12947736627bSAndi Kleen perf_evsel__free_id(&evsel->core); 12957736627bSAndi Kleen } 1296a74b4b66SNamhyung Kim } 1297a74b4b66SNamhyung Kim 12987748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist) 12994112eb18SArnaldo Carvalho de Melo { 1300f854839bSJiri Olsa struct perf_cpu_map *cpus; 13019749b90eSJiri Olsa struct perf_thread_map *threads; 13024112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 13034112eb18SArnaldo Carvalho de Melo 13044112eb18SArnaldo Carvalho de Melo /* 13054112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 13064112eb18SArnaldo Carvalho de Melo * an all cpus map. 13074112eb18SArnaldo Carvalho de Melo * 13084112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 13094112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 13104112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 13114112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 13124112eb18SArnaldo Carvalho de Melo */ 13139c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 13148c0498b6SAdrian Hunter if (!cpus) 13154112eb18SArnaldo Carvalho de Melo goto out; 13164112eb18SArnaldo Carvalho de Melo 13174b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 13188c0498b6SAdrian Hunter if (!threads) 13198c0498b6SAdrian Hunter goto out_put; 13204112eb18SArnaldo Carvalho de Melo 1321453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1322bfd1b83dSNamhyung Kim 1323bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 13248c0498b6SAdrian Hunter out_put: 132538f01d8dSJiri Olsa perf_cpu_map__put(cpus); 1326bfd1b83dSNamhyung Kim out: 1327bfd1b83dSNamhyung Kim return err; 13284112eb18SArnaldo Carvalho de Melo } 13294112eb18SArnaldo Carvalho de Melo 1330474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1331727ab04eSArnaldo Carvalho de Melo { 133232dcd021SJiri Olsa struct evsel *evsel; 1333a74b4b66SNamhyung Kim int err; 1334727ab04eSArnaldo Carvalho de Melo 13354112eb18SArnaldo Carvalho de Melo /* 13364112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 13374112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 13384112eb18SArnaldo Carvalho de Melo */ 133903617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 13407748bb71SArnaldo Carvalho de Melo err = evlist__create_syswide_maps(evlist); 13414112eb18SArnaldo Carvalho de Melo if (err < 0) 13424112eb18SArnaldo Carvalho de Melo goto out_err; 13434112eb18SArnaldo Carvalho de Melo } 13444112eb18SArnaldo Carvalho de Melo 13450a7e7ec9SArnaldo Carvalho de Melo evlist__update_id_pos(evlist); 1346733cd2feSAdrian Hunter 1347e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1348af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1349727ab04eSArnaldo Carvalho de Melo if (err < 0) 1350727ab04eSArnaldo Carvalho de Melo goto out_err; 1351727ab04eSArnaldo Carvalho de Melo } 1352727ab04eSArnaldo Carvalho de Melo 1353727ab04eSArnaldo Carvalho de Melo return 0; 1354727ab04eSArnaldo Carvalho de Melo out_err: 1355750b4edeSJiri Olsa evlist__close(evlist); 135641c21a68SNamhyung Kim errno = -err; 1357727ab04eSArnaldo Carvalho de Melo return err; 1358727ab04eSArnaldo Carvalho de Melo } 135935b9d88eSArnaldo Carvalho de Melo 13607b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[], 13617b392ef0SArnaldo Carvalho de Melo bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 136235b9d88eSArnaldo Carvalho de Melo { 136335b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 136435b9d88eSArnaldo Carvalho de Melo char bf; 136535b9d88eSArnaldo Carvalho de Melo 136635b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 136735b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 136835b9d88eSArnaldo Carvalho de Melo return -1; 136935b9d88eSArnaldo Carvalho de Melo } 137035b9d88eSArnaldo Carvalho de Melo 137135b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 137235b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 137335b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 137435b9d88eSArnaldo Carvalho de Melo } 137535b9d88eSArnaldo Carvalho de Melo 137635b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 137735b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 137835b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 137935b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 138035b9d88eSArnaldo Carvalho de Melo } 138135b9d88eSArnaldo Carvalho de Melo 138235b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13835f1c4225SArnaldo Carvalho de Melo int ret; 13845f1c4225SArnaldo Carvalho de Melo 1385119fa3c9SNamhyung Kim if (pipe_output) 138635b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 138735b9d88eSArnaldo Carvalho de Melo 13880817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13890817df08SDavid Ahern 139035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 139135b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 139235b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 139335b9d88eSArnaldo Carvalho de Melo 139435b9d88eSArnaldo Carvalho de Melo /* 139535b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 139635b9d88eSArnaldo Carvalho de Melo */ 139735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 139835b9d88eSArnaldo Carvalho de Melo 139935b9d88eSArnaldo Carvalho de Melo /* 140035b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 140135b9d88eSArnaldo Carvalho de Melo */ 14025f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 14035f1c4225SArnaldo Carvalho de Melo /* 14045f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 14055f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 14067b392ef0SArnaldo Carvalho de Melo * evlist__start_workload(). 14075f1c4225SArnaldo Carvalho de Melo * 140820f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 14095f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 14105f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 14115f1c4225SArnaldo Carvalho de Melo * here. 14125f1c4225SArnaldo Carvalho de Melo */ 14135f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 14145f1c4225SArnaldo Carvalho de Melo if (ret == -1) 141535b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 14165f1c4225SArnaldo Carvalho de Melo exit(ret); 14175f1c4225SArnaldo Carvalho de Melo } 141835b9d88eSArnaldo Carvalho de Melo 141935b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 142035b9d88eSArnaldo Carvalho de Melo 1421735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1422f33cbe72SArnaldo Carvalho de Melo union sigval val; 1423f33cbe72SArnaldo Carvalho de Melo 1424f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1425f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 142635b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1427f33cbe72SArnaldo Carvalho de Melo } else 1428f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 142935b9d88eSArnaldo Carvalho de Melo exit(-1); 143035b9d88eSArnaldo Carvalho de Melo } 143135b9d88eSArnaldo Carvalho de Melo 1432735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1433735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1434735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1435735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1436735f7e0bSArnaldo Carvalho de Melo }; 1437735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1438735f7e0bSArnaldo Carvalho de Melo } 1439735f7e0bSArnaldo Carvalho de Melo 14401aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 144103617c22SJiri Olsa if (evlist->core.threads == NULL) { 14421aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 14431aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 14441aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 14451aaf63b1SArnaldo Carvalho de Melo } 144603617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 14471aaf63b1SArnaldo Carvalho de Melo } 144835b9d88eSArnaldo Carvalho de Melo 144935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 145035b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 145135b9d88eSArnaldo Carvalho de Melo /* 145235b9d88eSArnaldo Carvalho de Melo * wait for child to settle 145335b9d88eSArnaldo Carvalho de Melo */ 145435b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 145535b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 145635b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 145735b9d88eSArnaldo Carvalho de Melo } 145835b9d88eSArnaldo Carvalho de Melo 1459bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 146035b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 146135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 146235b9d88eSArnaldo Carvalho de Melo return 0; 146335b9d88eSArnaldo Carvalho de Melo 146435b9d88eSArnaldo Carvalho de Melo out_close_pipes: 146535b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 146635b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 146735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 146835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 146935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 147035b9d88eSArnaldo Carvalho de Melo return -1; 147135b9d88eSArnaldo Carvalho de Melo } 147235b9d88eSArnaldo Carvalho de Melo 14737b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist) 147435b9d88eSArnaldo Carvalho de Melo { 147535b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1476b3824404SDavid Ahern char bf = 0; 1477bcf3145fSNamhyung Kim int ret; 147835b9d88eSArnaldo Carvalho de Melo /* 147935b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 148035b9d88eSArnaldo Carvalho de Melo */ 1481bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1482bcf3145fSNamhyung Kim if (ret < 0) 1483e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1484bcf3145fSNamhyung Kim 1485bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1486bcf3145fSNamhyung Kim return ret; 148735b9d88eSArnaldo Carvalho de Melo } 148835b9d88eSArnaldo Carvalho de Melo 148935b9d88eSArnaldo Carvalho de Melo return 0; 149035b9d88eSArnaldo Carvalho de Melo } 1491cb0b29e0SArnaldo Carvalho de Melo 14922a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample) 1493cb0b29e0SArnaldo Carvalho de Melo { 14943ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event); 149575562573SAdrian Hunter 149675562573SAdrian Hunter if (!evsel) 149775562573SAdrian Hunter return -EFAULT; 14986b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample(evsel, event, sample); 1499cb0b29e0SArnaldo Carvalho de Melo } 150078f067b3SArnaldo Carvalho de Melo 15012a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp) 150201468120SJiri Olsa { 15033ccf8a7bSArnaldo Carvalho de Melo struct evsel *evsel = evlist__event2evsel(evlist, event); 150401468120SJiri Olsa 150501468120SJiri Olsa if (!evsel) 150601468120SJiri Olsa return -EFAULT; 15076b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample_timestamp(evsel, event, timestamp); 150801468120SJiri Olsa } 150901468120SJiri Olsa 1510d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size) 1511a8f23d8fSArnaldo Carvalho de Melo { 1512a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1513c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1514a8f23d8fSArnaldo Carvalho de Melo 1515a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1516a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1517a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1518a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1519a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1520a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1521a8f23d8fSArnaldo Carvalho de Melo 15221a47245dSAdrian Hunter value = perf_event_paranoid(); 1523a8f23d8fSArnaldo Carvalho de Melo 1524a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1525a8f23d8fSArnaldo Carvalho de Melo 1526a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1527a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1528a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1529a8f23d8fSArnaldo Carvalho de Melo } 1530a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15315229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1532a8f23d8fSArnaldo Carvalho de Melo 1533a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15345229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 15355229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1536a8f23d8fSArnaldo Carvalho de Melo break; 1537d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1538515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1539d9aade7fSArnaldo Carvalho de Melo int max_freq; 1540d9aade7fSArnaldo Carvalho de Melo 1541d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1542d9aade7fSArnaldo Carvalho de Melo goto out_default; 1543d9aade7fSArnaldo Carvalho de Melo 15441fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1545d9aade7fSArnaldo Carvalho de Melo goto out_default; 1546d9aade7fSArnaldo Carvalho de Melo 1547d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1548d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1549d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1550d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 15511fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1552d9aade7fSArnaldo Carvalho de Melo break; 1553d9aade7fSArnaldo Carvalho de Melo } 1554a8f23d8fSArnaldo Carvalho de Melo default: 1555d9aade7fSArnaldo Carvalho de Melo out_default: 1556a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1557a8f23d8fSArnaldo Carvalho de Melo break; 1558a8f23d8fSArnaldo Carvalho de Melo } 1559a8f23d8fSArnaldo Carvalho de Melo 1560a8f23d8fSArnaldo Carvalho de Melo return 0; 1561a8f23d8fSArnaldo Carvalho de Melo } 1562a025e4f0SAdrian Hunter 1563d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1564956fa571SArnaldo Carvalho de Melo { 1565c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1566f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1567956fa571SArnaldo Carvalho de Melo 1568956fa571SArnaldo Carvalho de Melo switch (err) { 1569956fa571SArnaldo Carvalho de Melo case EPERM: 1570e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1571e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1572e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1573956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1574e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1575e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1576e965bea1SArnaldo Carvalho de Melo 1577e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1578e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1579e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1580e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1581e965bea1SArnaldo Carvalho de Melo } 1582e965bea1SArnaldo Carvalho de Melo 1583e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1584e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1585956fa571SArnaldo Carvalho de Melo break; 1586956fa571SArnaldo Carvalho de Melo default: 1587956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1588956fa571SArnaldo Carvalho de Melo break; 1589956fa571SArnaldo Carvalho de Melo } 1590956fa571SArnaldo Carvalho de Melo 1591956fa571SArnaldo Carvalho de Melo return 0; 1592956fa571SArnaldo Carvalho de Melo } 1593956fa571SArnaldo Carvalho de Melo 1594e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel) 1595a025e4f0SAdrian Hunter { 159632dcd021SJiri Olsa struct evsel *evsel, *n; 1597a025e4f0SAdrian Hunter LIST_HEAD(move); 1598a025e4f0SAdrian Hunter 1599515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1600a025e4f0SAdrian Hunter return; 1601a025e4f0SAdrian Hunter 1602e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1603a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1604b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1605a025e4f0SAdrian Hunter } 1606a025e4f0SAdrian Hunter 1607ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1608a025e4f0SAdrian Hunter } 160960b0896cSAdrian Hunter 1610e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist) 1611442ad225SAdrian Hunter { 1612442ad225SAdrian Hunter struct evsel *evsel; 1613442ad225SAdrian Hunter 1614442ad225SAdrian Hunter evlist__for_each_entry(evlist, evsel) { 1615442ad225SAdrian Hunter if (evsel->tracking) 1616442ad225SAdrian Hunter return evsel; 1617442ad225SAdrian Hunter } 1618442ad225SAdrian Hunter 1619442ad225SAdrian Hunter return evlist__first(evlist); 1620442ad225SAdrian Hunter } 1621442ad225SAdrian Hunter 1622e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel) 162360b0896cSAdrian Hunter { 162432dcd021SJiri Olsa struct evsel *evsel; 162560b0896cSAdrian Hunter 162660b0896cSAdrian Hunter if (tracking_evsel->tracking) 162760b0896cSAdrian Hunter return; 162860b0896cSAdrian Hunter 1629e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 163060b0896cSAdrian Hunter if (evsel != tracking_evsel) 163160b0896cSAdrian Hunter evsel->tracking = false; 163260b0896cSAdrian Hunter } 163360b0896cSAdrian Hunter 163460b0896cSAdrian Hunter tracking_evsel->tracking = true; 163560b0896cSAdrian Hunter } 16367630b3e2SWang Nan 1637b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str) 16387630b3e2SWang Nan { 163932dcd021SJiri Olsa struct evsel *evsel; 16407630b3e2SWang Nan 1641e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16427630b3e2SWang Nan if (!evsel->name) 16437630b3e2SWang Nan continue; 16447630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 16457630b3e2SWang Nan return evsel; 16467630b3e2SWang Nan } 16477630b3e2SWang Nan 16487630b3e2SWang Nan return NULL; 16497630b3e2SWang Nan } 165054cc54deSWang Nan 1651ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state) 165254cc54deSWang Nan { 165354cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 165454cc54deSWang Nan enum action { 165554cc54deSWang Nan NONE, 165654cc54deSWang Nan PAUSE, 165754cc54deSWang Nan RESUME, 165854cc54deSWang Nan } action = NONE; 165954cc54deSWang Nan 16600b72d69aSWang Nan if (!evlist->overwrite_mmap) 166154cc54deSWang Nan return; 166254cc54deSWang Nan 166354cc54deSWang Nan switch (old_state) { 166454cc54deSWang Nan case BKW_MMAP_NOTREADY: { 166554cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1666dd8bd53aSLuis de Bethencourt goto state_err; 166754cc54deSWang Nan break; 166854cc54deSWang Nan } 166954cc54deSWang Nan case BKW_MMAP_RUNNING: { 167054cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 167154cc54deSWang Nan goto state_err; 167254cc54deSWang Nan action = PAUSE; 167354cc54deSWang Nan break; 167454cc54deSWang Nan } 167554cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 167654cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 167754cc54deSWang Nan goto state_err; 167854cc54deSWang Nan break; 167954cc54deSWang Nan } 168054cc54deSWang Nan case BKW_MMAP_EMPTY: { 168154cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 168254cc54deSWang Nan goto state_err; 168354cc54deSWang Nan action = RESUME; 168454cc54deSWang Nan break; 168554cc54deSWang Nan } 168654cc54deSWang Nan default: 168754cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 168854cc54deSWang Nan } 168954cc54deSWang Nan 169054cc54deSWang Nan evlist->bkw_mmap_state = state; 169154cc54deSWang Nan 169254cc54deSWang Nan switch (action) { 169354cc54deSWang Nan case PAUSE: 16940a60b339SArnaldo Carvalho de Melo evlist__pause(evlist); 169554cc54deSWang Nan break; 169654cc54deSWang Nan case RESUME: 16970a60b339SArnaldo Carvalho de Melo evlist__resume(evlist); 169854cc54deSWang Nan break; 169954cc54deSWang Nan case NONE: 170054cc54deSWang Nan default: 170154cc54deSWang Nan break; 170254cc54deSWang Nan } 170354cc54deSWang Nan 170454cc54deSWang Nan state_err: 170554cc54deSWang Nan return; 170654cc54deSWang Nan } 170707d6f446SArnaldo Carvalho de Melo 170878e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist) 170907d6f446SArnaldo Carvalho de Melo { 171032dcd021SJiri Olsa struct evsel *evsel; 171107d6f446SArnaldo Carvalho de Melo 171207d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 17131fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 171407d6f446SArnaldo Carvalho de Melo return false; 171507d6f446SArnaldo Carvalho de Melo } 171607d6f446SArnaldo Carvalho de Melo 171707d6f446SArnaldo Carvalho de Melo return true; 171807d6f446SArnaldo Carvalho de Melo } 1719e2bdbe80SJin Yao 1720e2bdbe80SJin Yao /* 1721e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1722e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1723e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1724e2bdbe80SJin Yao */ 172564b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist) 1726e2bdbe80SJin Yao { 1727e2bdbe80SJin Yao if (!evlist->nr_groups) { 1728515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1729e2bdbe80SJin Yao 1730a622eafaSArnaldo Carvalho de Melo evlist__set_leader(evlist); 1731e2bdbe80SJin Yao leader->forced_leader = true; 1732e2bdbe80SJin Yao } 1733e2bdbe80SJin Yao } 1734c3537fc2SAndi Kleen 173564b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close) 1736c3537fc2SAndi Kleen { 173732dcd021SJiri Olsa struct evsel *c2, *leader; 1738c3537fc2SAndi Kleen bool is_open = true; 1739c3537fc2SAndi Kleen 1740c3537fc2SAndi Kleen leader = evsel->leader; 1741c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 17425643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1743c3537fc2SAndi Kleen 1744c3537fc2SAndi Kleen /* 1745c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1746c3537fc2SAndi Kleen * include the first entry. 1747c3537fc2SAndi Kleen */ 1748c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1749c3537fc2SAndi Kleen if (c2 == evsel) 1750c3537fc2SAndi Kleen is_open = false; 1751c3537fc2SAndi Kleen if (c2->leader == leader) { 17524804e011SAndi Kleen if (is_open && close) 17535a40e199SAndi Kleen perf_evsel__close(&c2->core); 1754c3537fc2SAndi Kleen c2->leader = c2; 17555643b1a5SJiri Olsa c2->core.nr_members = 0; 17564804e011SAndi Kleen /* 17574804e011SAndi Kleen * Set this for all former members of the group 17584804e011SAndi Kleen * to indicate they get reopened. 17594804e011SAndi Kleen */ 17604804e011SAndi Kleen c2->reset_group = true; 1761c3537fc2SAndi Kleen } 1762c3537fc2SAndi Kleen } 1763c3537fc2SAndi Kleen return leader; 1764c3537fc2SAndi Kleen } 1765ec886bf5SAlexey Budankov 1766a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1767a8fcbd26SAdrian Hunter { 1768a8fcbd26SAdrian Hunter char *s, *p; 1769a8fcbd26SAdrian Hunter int ret = 0, fd; 1770a8fcbd26SAdrian Hunter 1771a8fcbd26SAdrian Hunter if (strncmp(str, "fifo:", 5)) 1772a8fcbd26SAdrian Hunter return -EINVAL; 1773a8fcbd26SAdrian Hunter 1774a8fcbd26SAdrian Hunter str += 5; 1775a8fcbd26SAdrian Hunter if (!*str || *str == ',') 1776a8fcbd26SAdrian Hunter return -EINVAL; 1777a8fcbd26SAdrian Hunter 1778a8fcbd26SAdrian Hunter s = strdup(str); 1779a8fcbd26SAdrian Hunter if (!s) 1780a8fcbd26SAdrian Hunter return -ENOMEM; 1781a8fcbd26SAdrian Hunter 1782a8fcbd26SAdrian Hunter p = strchr(s, ','); 1783a8fcbd26SAdrian Hunter if (p) 1784a8fcbd26SAdrian Hunter *p = '\0'; 1785a8fcbd26SAdrian Hunter 1786a8fcbd26SAdrian Hunter /* 1787a8fcbd26SAdrian Hunter * O_RDWR avoids POLLHUPs which is necessary to allow the other 1788a8fcbd26SAdrian Hunter * end of a FIFO to be repeatedly opened and closed. 1789a8fcbd26SAdrian Hunter */ 1790a8fcbd26SAdrian Hunter fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1791a8fcbd26SAdrian Hunter if (fd < 0) { 1792a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", s); 1793a8fcbd26SAdrian Hunter ret = -errno; 1794a8fcbd26SAdrian Hunter goto out_free; 1795a8fcbd26SAdrian Hunter } 1796a8fcbd26SAdrian Hunter *ctl_fd = fd; 1797a8fcbd26SAdrian Hunter *ctl_fd_close = true; 1798a8fcbd26SAdrian Hunter 1799a8fcbd26SAdrian Hunter if (p && *++p) { 1800a8fcbd26SAdrian Hunter /* O_RDWR | O_NONBLOCK means the other end need not be open */ 1801a8fcbd26SAdrian Hunter fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1802a8fcbd26SAdrian Hunter if (fd < 0) { 1803a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", p); 1804a8fcbd26SAdrian Hunter ret = -errno; 1805a8fcbd26SAdrian Hunter goto out_free; 1806a8fcbd26SAdrian Hunter } 1807a8fcbd26SAdrian Hunter *ctl_fd_ack = fd; 1808a8fcbd26SAdrian Hunter } 1809a8fcbd26SAdrian Hunter 1810a8fcbd26SAdrian Hunter out_free: 1811a8fcbd26SAdrian Hunter free(s); 1812a8fcbd26SAdrian Hunter return ret; 1813a8fcbd26SAdrian Hunter } 1814a8fcbd26SAdrian Hunter 1815a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 18169864a66dSAdrian Hunter { 18179864a66dSAdrian Hunter char *comma = NULL, *endptr = NULL; 18189864a66dSAdrian Hunter 1819a8fcbd26SAdrian Hunter *ctl_fd_close = false; 1820a8fcbd26SAdrian Hunter 18219864a66dSAdrian Hunter if (strncmp(str, "fd:", 3)) 1822a8fcbd26SAdrian Hunter return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close); 18239864a66dSAdrian Hunter 18249864a66dSAdrian Hunter *ctl_fd = strtoul(&str[3], &endptr, 0); 18259864a66dSAdrian Hunter if (endptr == &str[3]) 18269864a66dSAdrian Hunter return -EINVAL; 18279864a66dSAdrian Hunter 18289864a66dSAdrian Hunter comma = strchr(str, ','); 18299864a66dSAdrian Hunter if (comma) { 18309864a66dSAdrian Hunter if (endptr != comma) 18319864a66dSAdrian Hunter return -EINVAL; 18329864a66dSAdrian Hunter 18339864a66dSAdrian Hunter *ctl_fd_ack = strtoul(comma + 1, &endptr, 0); 18349864a66dSAdrian Hunter if (endptr == comma + 1 || *endptr != '\0') 18359864a66dSAdrian Hunter return -EINVAL; 18369864a66dSAdrian Hunter } 18379864a66dSAdrian Hunter 18389864a66dSAdrian Hunter return 0; 18399864a66dSAdrian Hunter } 18409864a66dSAdrian Hunter 1841ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close) 1842ee7fe31eSAdrian Hunter { 1843ee7fe31eSAdrian Hunter if (*ctl_fd_close) { 1844ee7fe31eSAdrian Hunter *ctl_fd_close = false; 1845ee7fe31eSAdrian Hunter close(ctl_fd); 1846ee7fe31eSAdrian Hunter if (ctl_fd_ack >= 0) 1847ee7fe31eSAdrian Hunter close(ctl_fd_ack); 1848ee7fe31eSAdrian Hunter } 1849ee7fe31eSAdrian Hunter } 1850ee7fe31eSAdrian Hunter 1851ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack) 1852ec886bf5SAlexey Budankov { 1853ec886bf5SAlexey Budankov if (fd == -1) { 1854ec886bf5SAlexey Budankov pr_debug("Control descriptor is not initialized\n"); 1855ec886bf5SAlexey Budankov return 0; 1856ec886bf5SAlexey Budankov } 1857ec886bf5SAlexey Budankov 1858ec886bf5SAlexey Budankov evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 1859ec886bf5SAlexey Budankov fdarray_flag__nonfilterable); 1860ec886bf5SAlexey Budankov if (evlist->ctl_fd.pos < 0) { 1861ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1862ec886bf5SAlexey Budankov pr_err("Failed to add ctl fd entry: %m\n"); 1863ec886bf5SAlexey Budankov return -1; 1864ec886bf5SAlexey Budankov } 1865ec886bf5SAlexey Budankov 1866ec886bf5SAlexey Budankov evlist->ctl_fd.fd = fd; 1867ec886bf5SAlexey Budankov evlist->ctl_fd.ack = ack; 1868ec886bf5SAlexey Budankov 1869ec886bf5SAlexey Budankov return 0; 1870ec886bf5SAlexey Budankov } 1871ec886bf5SAlexey Budankov 1872ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist) 1873ec886bf5SAlexey Budankov { 1874ec886bf5SAlexey Budankov return evlist->ctl_fd.pos >= 0; 1875ec886bf5SAlexey Budankov } 1876ec886bf5SAlexey Budankov 1877ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist) 1878ec886bf5SAlexey Budankov { 1879ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 1880ec886bf5SAlexey Budankov 1881ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist)) 1882ec886bf5SAlexey Budankov return 0; 1883ec886bf5SAlexey Budankov 1884ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].fd = -1; 1885ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].events = 0; 1886ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].revents = 0; 1887ec886bf5SAlexey Budankov 1888ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1889ec886bf5SAlexey Budankov evlist->ctl_fd.ack = -1; 1890ec886bf5SAlexey Budankov evlist->ctl_fd.fd = -1; 1891ec886bf5SAlexey Budankov 1892ec886bf5SAlexey Budankov return 0; 1893ec886bf5SAlexey Budankov } 1894ec886bf5SAlexey Budankov 1895ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd, 1896ec886bf5SAlexey Budankov char *cmd_data, size_t data_size) 1897ec886bf5SAlexey Budankov { 1898ec886bf5SAlexey Budankov int err; 1899ec886bf5SAlexey Budankov char c; 1900ec886bf5SAlexey Budankov size_t bytes_read = 0; 1901ec886bf5SAlexey Budankov 190240db8ff5SAdrian Hunter *cmd = EVLIST_CTL_CMD_UNSUPPORTED; 1903ec886bf5SAlexey Budankov memset(cmd_data, 0, data_size); 1904ec886bf5SAlexey Budankov data_size--; 1905ec886bf5SAlexey Budankov 1906ec886bf5SAlexey Budankov do { 1907ec886bf5SAlexey Budankov err = read(evlist->ctl_fd.fd, &c, 1); 1908ec886bf5SAlexey Budankov if (err > 0) { 1909ec886bf5SAlexey Budankov if (c == '\n' || c == '\0') 1910ec886bf5SAlexey Budankov break; 1911ec886bf5SAlexey Budankov cmd_data[bytes_read++] = c; 1912ec886bf5SAlexey Budankov if (bytes_read == data_size) 1913ec886bf5SAlexey Budankov break; 191440db8ff5SAdrian Hunter continue; 191540db8ff5SAdrian Hunter } else if (err == -1) { 191640db8ff5SAdrian Hunter if (errno == EINTR) 191740db8ff5SAdrian Hunter continue; 191840db8ff5SAdrian Hunter if (errno == EAGAIN || errno == EWOULDBLOCK) 191940db8ff5SAdrian Hunter err = 0; 192040db8ff5SAdrian Hunter else 1921ec886bf5SAlexey Budankov pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd); 1922ec886bf5SAlexey Budankov } 192340db8ff5SAdrian Hunter break; 1924ec886bf5SAlexey Budankov } while (1); 1925ec886bf5SAlexey Budankov 1926ec886bf5SAlexey Budankov pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data, 1927ec886bf5SAlexey Budankov bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0"); 1928ec886bf5SAlexey Budankov 192940db8ff5SAdrian Hunter if (bytes_read > 0) { 1930ec886bf5SAlexey Budankov if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG, 1931ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) { 1932ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_ENABLE; 1933ec886bf5SAlexey Budankov } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG, 1934ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) { 1935ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_DISABLE; 1936d20aff15SAdrian Hunter } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG, 1937d20aff15SAdrian Hunter (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) { 1938d20aff15SAdrian Hunter *cmd = EVLIST_CTL_CMD_SNAPSHOT; 1939d20aff15SAdrian Hunter pr_debug("is snapshot\n"); 1940*142544a9SJiri Olsa } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG, 1941*142544a9SJiri Olsa (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) { 1942*142544a9SJiri Olsa *cmd = EVLIST_CTL_CMD_EVLIST; 1943ec886bf5SAlexey Budankov } 1944ec886bf5SAlexey Budankov } 1945ec886bf5SAlexey Budankov 194640db8ff5SAdrian Hunter return bytes_read ? (int)bytes_read : err; 1947ec886bf5SAlexey Budankov } 1948ec886bf5SAlexey Budankov 1949d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist) 1950ec886bf5SAlexey Budankov { 1951ec886bf5SAlexey Budankov int err; 1952ec886bf5SAlexey Budankov 1953ec886bf5SAlexey Budankov if (evlist->ctl_fd.ack == -1) 1954ec886bf5SAlexey Budankov return 0; 1955ec886bf5SAlexey Budankov 1956ec886bf5SAlexey Budankov err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG, 1957ec886bf5SAlexey Budankov sizeof(EVLIST_CTL_CMD_ACK_TAG)); 1958ec886bf5SAlexey Budankov if (err == -1) 1959ec886bf5SAlexey Budankov pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack); 1960ec886bf5SAlexey Budankov 1961ec886bf5SAlexey Budankov return err; 1962ec886bf5SAlexey Budankov } 1963ec886bf5SAlexey Budankov 1964991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg) 1965991ae4ebSJiri Olsa { 1966991ae4ebSJiri Olsa char *data = cmd_data + cmd_size; 1967991ae4ebSJiri Olsa 1968991ae4ebSJiri Olsa /* no argument */ 1969991ae4ebSJiri Olsa if (!*data) 1970991ae4ebSJiri Olsa return 0; 1971991ae4ebSJiri Olsa 1972991ae4ebSJiri Olsa /* there's argument */ 1973991ae4ebSJiri Olsa if (*data == ' ') { 1974991ae4ebSJiri Olsa *arg = data + 1; 1975991ae4ebSJiri Olsa return 1; 1976991ae4ebSJiri Olsa } 1977991ae4ebSJiri Olsa 1978991ae4ebSJiri Olsa /* malformed */ 1979991ae4ebSJiri Olsa return -1; 1980991ae4ebSJiri Olsa } 1981991ae4ebSJiri Olsa 1982991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable) 1983991ae4ebSJiri Olsa { 1984991ae4ebSJiri Olsa struct evsel *evsel; 1985991ae4ebSJiri Olsa char *name; 1986991ae4ebSJiri Olsa int err; 1987991ae4ebSJiri Olsa 1988991ae4ebSJiri Olsa err = get_cmd_arg(cmd_data, 1989991ae4ebSJiri Olsa enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 : 1990991ae4ebSJiri Olsa sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1, 1991991ae4ebSJiri Olsa &name); 1992991ae4ebSJiri Olsa if (err < 0) { 1993991ae4ebSJiri Olsa pr_info("failed: wrong command\n"); 1994991ae4ebSJiri Olsa return -1; 1995991ae4ebSJiri Olsa } 1996991ae4ebSJiri Olsa 1997991ae4ebSJiri Olsa if (err) { 1998991ae4ebSJiri Olsa evsel = evlist__find_evsel_by_str(evlist, name); 1999991ae4ebSJiri Olsa if (evsel) { 2000991ae4ebSJiri Olsa if (enable) 2001991ae4ebSJiri Olsa evlist__enable_evsel(evlist, name); 2002991ae4ebSJiri Olsa else 2003991ae4ebSJiri Olsa evlist__disable_evsel(evlist, name); 2004991ae4ebSJiri Olsa pr_info("Event %s %s\n", evsel->name, 2005991ae4ebSJiri Olsa enable ? "enabled" : "disabled"); 2006991ae4ebSJiri Olsa } else { 2007991ae4ebSJiri Olsa pr_info("failed: can't find '%s' event\n", name); 2008991ae4ebSJiri Olsa } 2009991ae4ebSJiri Olsa } else { 2010991ae4ebSJiri Olsa if (enable) { 2011991ae4ebSJiri Olsa evlist__enable(evlist); 2012991ae4ebSJiri Olsa pr_info(EVLIST_ENABLED_MSG); 2013991ae4ebSJiri Olsa } else { 2014991ae4ebSJiri Olsa evlist__disable(evlist); 2015991ae4ebSJiri Olsa pr_info(EVLIST_DISABLED_MSG); 2016991ae4ebSJiri Olsa } 2017991ae4ebSJiri Olsa } 2018991ae4ebSJiri Olsa 2019991ae4ebSJiri Olsa return 0; 2020991ae4ebSJiri Olsa } 2021991ae4ebSJiri Olsa 2022*142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data) 2023*142544a9SJiri Olsa { 2024*142544a9SJiri Olsa struct perf_attr_details details = { .verbose = false, }; 2025*142544a9SJiri Olsa struct evsel *evsel; 2026*142544a9SJiri Olsa char *arg; 2027*142544a9SJiri Olsa int err; 2028*142544a9SJiri Olsa 2029*142544a9SJiri Olsa err = get_cmd_arg(cmd_data, 2030*142544a9SJiri Olsa sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1, 2031*142544a9SJiri Olsa &arg); 2032*142544a9SJiri Olsa if (err < 0) { 2033*142544a9SJiri Olsa pr_info("failed: wrong command\n"); 2034*142544a9SJiri Olsa return -1; 2035*142544a9SJiri Olsa } 2036*142544a9SJiri Olsa 2037*142544a9SJiri Olsa if (err) { 2038*142544a9SJiri Olsa if (!strcmp(arg, "-v")) { 2039*142544a9SJiri Olsa details.verbose = true; 2040*142544a9SJiri Olsa } else if (!strcmp(arg, "-g")) { 2041*142544a9SJiri Olsa details.event_group = true; 2042*142544a9SJiri Olsa } else if (!strcmp(arg, "-F")) { 2043*142544a9SJiri Olsa details.freq = true; 2044*142544a9SJiri Olsa } else { 2045*142544a9SJiri Olsa pr_info("failed: wrong command\n"); 2046*142544a9SJiri Olsa return -1; 2047*142544a9SJiri Olsa } 2048*142544a9SJiri Olsa } 2049*142544a9SJiri Olsa 2050*142544a9SJiri Olsa evlist__for_each_entry(evlist, evsel) 2051*142544a9SJiri Olsa evsel__fprintf(evsel, &details, stderr); 2052*142544a9SJiri Olsa 2053*142544a9SJiri Olsa return 0; 2054*142544a9SJiri Olsa } 2055*142544a9SJiri Olsa 2056ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd) 2057ec886bf5SAlexey Budankov { 2058ec886bf5SAlexey Budankov int err = 0; 2059ec886bf5SAlexey Budankov char cmd_data[EVLIST_CTL_CMD_MAX_LEN]; 2060ec886bf5SAlexey Budankov int ctlfd_pos = evlist->ctl_fd.pos; 2061ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 2062ec886bf5SAlexey Budankov 2063ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents) 2064ec886bf5SAlexey Budankov return 0; 2065ec886bf5SAlexey Budankov 2066ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & POLLIN) { 2067ec886bf5SAlexey Budankov err = evlist__ctlfd_recv(evlist, cmd, cmd_data, 2068ec886bf5SAlexey Budankov EVLIST_CTL_CMD_MAX_LEN); 2069ec886bf5SAlexey Budankov if (err > 0) { 2070ec886bf5SAlexey Budankov switch (*cmd) { 2071ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ENABLE: 2072ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_DISABLE: 2073991ae4ebSJiri Olsa err = evlist__ctlfd_enable(evlist, cmd_data, 2074991ae4ebSJiri Olsa *cmd == EVLIST_CTL_CMD_ENABLE); 2075ec886bf5SAlexey Budankov break; 2076*142544a9SJiri Olsa case EVLIST_CTL_CMD_EVLIST: 2077*142544a9SJiri Olsa err = evlist__ctlfd_list(evlist, cmd_data); 2078*142544a9SJiri Olsa break; 2079d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 2080d20aff15SAdrian Hunter break; 2081ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ACK: 2082ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 2083ec886bf5SAlexey Budankov default: 2084ec886bf5SAlexey Budankov pr_debug("ctlfd: unsupported %d\n", *cmd); 2085ec886bf5SAlexey Budankov break; 2086ec886bf5SAlexey Budankov } 2087d20aff15SAdrian Hunter if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED || 2088d20aff15SAdrian Hunter *cmd == EVLIST_CTL_CMD_SNAPSHOT)) 2089ec886bf5SAlexey Budankov evlist__ctlfd_ack(evlist); 2090ec886bf5SAlexey Budankov } 2091ec886bf5SAlexey Budankov } 2092ec886bf5SAlexey Budankov 2093ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR)) 2094ec886bf5SAlexey Budankov evlist__finalize_ctlfd(evlist); 2095ec886bf5SAlexey Budankov else 2096ec886bf5SAlexey Budankov entries[ctlfd_pos].revents = 0; 2097ec886bf5SAlexey Budankov 2098ec886bf5SAlexey Budankov return err; 2099ec886bf5SAlexey Budankov } 2100b214ba8cSNamhyung Kim 2101b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx) 2102b214ba8cSNamhyung Kim { 2103b214ba8cSNamhyung Kim struct evsel *evsel; 2104b214ba8cSNamhyung Kim 2105b214ba8cSNamhyung Kim evlist__for_each_entry(evlist, evsel) { 2106b214ba8cSNamhyung Kim if (evsel->idx == idx) 2107b214ba8cSNamhyung Kim return evsel; 2108b214ba8cSNamhyung Kim } 2109b214ba8cSNamhyung Kim return NULL; 2110b214ba8cSNamhyung Kim } 2111