191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f8a95309SArnaldo Carvalho de Melo /* 3f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 4f8a95309SArnaldo Carvalho de Melo * 5f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 6f8a95309SArnaldo Carvalho de Melo * copyright notes. 7f8a95309SArnaldo Carvalho de Melo */ 8956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h> 9a43783aeSArnaldo Carvalho de Melo #include <errno.h> 10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1512864b31SNamhyung Kim #include "target.h" 16361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 17361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 18e3e1a54fSAdrian Hunter #include "debug.h" 1958db1d6eSArnaldo Carvalho de Melo #include "units.h" 2020f2be1dSJiri Olsa #include <internal/lib.h> // page_size 217736627bSAndi Kleen #include "affinity.h" 22c1a604dfSArnaldo Carvalho de Melo #include "../perf.h" 2354cc54deSWang Nan #include "asm/bug.h" 24657ee553SSong Liu #include "bpf-event.h" 25da949f50SArnaldo Carvalho de Melo #include "util/string2.h" 2640c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h" 279607ad3aSArnaldo Carvalho de Melo #include <signal.h> 2835b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 29b397f846SArnaldo Carvalho de Melo #include <sched.h> 30f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h> 31361c99a6SArnaldo Carvalho de Melo 3250d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 334b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 3450d08e47SArnaldo Carvalho de Melo 35bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 3686a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 37f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 38f8a95309SArnaldo Carvalho de Melo 3970db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 4070db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 410389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 428dd2a131SJiri Olsa #include <linux/err.h> 438520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 447f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 454562a739SJiri Olsa #include <perf/evlist.h> 4688761fa1SJiri Olsa #include <perf/evsel.h> 479c3516d1SJiri Olsa #include <perf/cpumap.h> 487728fa0cSJiri Olsa #include <perf/mmap.h> 4970db7533SArnaldo Carvalho de Melo 50e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h> 51e14e5497SArnaldo Carvalho de Melo 52748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 53748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 54748fe088SArnaldo Carvalho de Melo #endif 55748fe088SArnaldo Carvalho de Melo 569dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 578cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 58f8a95309SArnaldo Carvalho de Melo 5952c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 609749b90eSJiri Olsa struct perf_thread_map *threads) 61361c99a6SArnaldo Carvalho de Melo { 624562a739SJiri Olsa perf_evlist__init(&evlist->core); 63453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 6435b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 6554cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 66361c99a6SArnaldo Carvalho de Melo } 67361c99a6SArnaldo Carvalho de Melo 680f98b11cSJiri Olsa struct evlist *evlist__new(void) 69ef1d1af2SArnaldo Carvalho de Melo { 7063503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist)); 71ef1d1af2SArnaldo Carvalho de Melo 72ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 7352c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL); 74ef1d1af2SArnaldo Carvalho de Melo 75361c99a6SArnaldo Carvalho de Melo return evlist; 76361c99a6SArnaldo Carvalho de Melo } 77361c99a6SArnaldo Carvalho de Melo 7863503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void) 79b22d54b0SJiri Olsa { 800f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 81b22d54b0SJiri Olsa 82e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_default(evlist)) { 83c12995a5SJiri Olsa evlist__delete(evlist); 84b22d54b0SJiri Olsa evlist = NULL; 85b22d54b0SJiri Olsa } 86b22d54b0SJiri Olsa 87b22d54b0SJiri Olsa return evlist; 88b22d54b0SJiri Olsa } 89b22d54b0SJiri Olsa 9063503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void) 915bae0250SArnaldo Carvalho de Melo { 920f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 935bae0250SArnaldo Carvalho de Melo 94e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_dummy(evlist)) { 95c12995a5SJiri Olsa evlist__delete(evlist); 965bae0250SArnaldo Carvalho de Melo evlist = NULL; 975bae0250SArnaldo Carvalho de Melo } 985bae0250SArnaldo Carvalho de Melo 995bae0250SArnaldo Carvalho de Melo return evlist; 1005bae0250SArnaldo Carvalho de Melo } 1015bae0250SArnaldo Carvalho de Melo 10275562573SAdrian Hunter /** 10375562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 10475562573SAdrian Hunter * @evlist: selected event list 10575562573SAdrian Hunter * 10675562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 10775562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 10875562573SAdrian Hunter */ 10963503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist) 11075562573SAdrian Hunter { 111515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11275562573SAdrian Hunter 11375562573SAdrian Hunter evlist->id_pos = first->id_pos; 11475562573SAdrian Hunter evlist->is_pos = first->is_pos; 11575562573SAdrian Hunter } 11675562573SAdrian Hunter 11763503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist) 118733cd2feSAdrian Hunter { 11932dcd021SJiri Olsa struct evsel *evsel; 120733cd2feSAdrian Hunter 121e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 1224b5e87b7SArnaldo Carvalho de Melo evsel__calc_id_pos(evsel); 123733cd2feSAdrian Hunter 124733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 125733cd2feSAdrian Hunter } 126733cd2feSAdrian Hunter 127e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist) 128361c99a6SArnaldo Carvalho de Melo { 12932dcd021SJiri Olsa struct evsel *pos, *n; 130361c99a6SArnaldo Carvalho de Melo 131e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 132b27c4eceSJiri Olsa list_del_init(&pos->core.node); 133d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 1345eb2dd2aSJiri Olsa evsel__delete(pos); 135361c99a6SArnaldo Carvalho de Melo } 136361c99a6SArnaldo Carvalho de Melo 1376484d2f9SJiri Olsa evlist->core.nr_entries = 0; 138361c99a6SArnaldo Carvalho de Melo } 139361c99a6SArnaldo Carvalho de Melo 140470579b0SJiri Olsa void evlist__exit(struct evlist *evlist) 141ef1d1af2SArnaldo Carvalho de Melo { 14204662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1430b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 14493dd6e28SJiri Olsa perf_evlist__exit(&evlist->core); 145ef1d1af2SArnaldo Carvalho de Melo } 146ef1d1af2SArnaldo Carvalho de Melo 147c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist) 148361c99a6SArnaldo Carvalho de Melo { 1490b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1500b04b3dcSArnaldo Carvalho de Melo return; 1510b04b3dcSArnaldo Carvalho de Melo 152db6b7b13SJiri Olsa evlist__munmap(evlist); 153750b4edeSJiri Olsa evlist__close(evlist); 154e6b1878dSJiri Olsa evlist__purge(evlist); 155470579b0SJiri Olsa evlist__exit(evlist); 156361c99a6SArnaldo Carvalho de Melo free(evlist); 157361c99a6SArnaldo Carvalho de Melo } 158361c99a6SArnaldo Carvalho de Melo 159a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 160361c99a6SArnaldo Carvalho de Melo { 161d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1626484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 16360b0896cSAdrian Hunter entry->tracking = !entry->idx; 164ef503831SArnaldo Carvalho de Melo 1656484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1666484d2f9SJiri Olsa 1676484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 16875562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 169361c99a6SArnaldo Carvalho de Melo } 170361c99a6SArnaldo Carvalho de Melo 17116251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1724768230aSAdrian Hunter { 1734768230aSAdrian Hunter evsel->evlist = NULL; 17452e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1754768230aSAdrian Hunter } 1764768230aSAdrian Hunter 17763503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist, 178f114d6efSAdrian Hunter struct list_head *list) 17950d08e47SArnaldo Carvalho de Melo { 18032dcd021SJiri Olsa struct evsel *evsel, *temp; 18175562573SAdrian Hunter 182e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 183b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 184a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 185f114d6efSAdrian Hunter } 18650d08e47SArnaldo Carvalho de Melo } 18750d08e47SArnaldo Carvalho de Melo 188c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist, 189c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs) 190c0e53476SArnaldo Carvalho de Melo { 191c0e53476SArnaldo Carvalho de Melo struct evsel *evsel; 192c0e53476SArnaldo Carvalho de Melo size_t i; 193c0e53476SArnaldo Carvalho de Melo int err; 194c0e53476SArnaldo Carvalho de Melo 195c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 196c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it. 197c0e53476SArnaldo Carvalho de Melo evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name); 198c0e53476SArnaldo Carvalho de Melo if (evsel == NULL) 199c0e53476SArnaldo Carvalho de Melo continue; 200c0e53476SArnaldo Carvalho de Melo 201c0e53476SArnaldo Carvalho de Melo err = -EEXIST; 202c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL) 203c0e53476SArnaldo Carvalho de Melo goto out; 204c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler; 205c0e53476SArnaldo Carvalho de Melo } 206c0e53476SArnaldo Carvalho de Melo 207c0e53476SArnaldo Carvalho de Melo err = 0; 208c0e53476SArnaldo Carvalho de Melo out: 209c0e53476SArnaldo Carvalho de Melo return err; 210c0e53476SArnaldo Carvalho de Melo } 211c0e53476SArnaldo Carvalho de Melo 21263dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 21363dab225SArnaldo Carvalho de Melo { 21432dcd021SJiri Olsa struct evsel *evsel, *leader; 21563dab225SArnaldo Carvalho de Melo 216b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 217b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 21897f63e4aSNamhyung Kim 2195643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 22063dab225SArnaldo Carvalho de Melo 221e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22263dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22363dab225SArnaldo Carvalho de Melo } 22463dab225SArnaldo Carvalho de Melo } 22563dab225SArnaldo Carvalho de Melo 22663503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist) 2276a4bb04cSJiri Olsa { 2286484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2296484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 230ce9036a6SJiri Olsa __perf_evlist__set_leader(&evlist->core.entries); 2316a4bb04cSJiri Olsa } 23297f63e4aSNamhyung Kim } 2336a4bb04cSJiri Olsa 234e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise) 235361c99a6SArnaldo Carvalho de Melo { 2368f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_cycles(precise); 237361c99a6SArnaldo Carvalho de Melo 238361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2397c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 240361c99a6SArnaldo Carvalho de Melo 241a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 242361c99a6SArnaldo Carvalho de Melo return 0; 243361c99a6SArnaldo Carvalho de Melo } 2445c581041SArnaldo Carvalho de Melo 245e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist) 2465bae0250SArnaldo Carvalho de Melo { 2475bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2485bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2495bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2505bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2515bae0250SArnaldo Carvalho de Melo }; 2528f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries); 2535bae0250SArnaldo Carvalho de Melo 2545bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2555bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2565bae0250SArnaldo Carvalho de Melo 257a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2585bae0250SArnaldo Carvalho de Melo return 0; 2595bae0250SArnaldo Carvalho de Melo } 2605bae0250SArnaldo Carvalho de Melo 261e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 26250d08e47SArnaldo Carvalho de Melo { 26332dcd021SJiri Olsa struct evsel *evsel, *n; 26450d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 26550d08e47SArnaldo Carvalho de Melo size_t i; 26650d08e47SArnaldo Carvalho de Melo 26750d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2688f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 26950d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27050d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 271b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 27250d08e47SArnaldo Carvalho de Melo } 27350d08e47SArnaldo Carvalho de Melo 274f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 27550d08e47SArnaldo Carvalho de Melo 27650d08e47SArnaldo Carvalho de Melo return 0; 27750d08e47SArnaldo Carvalho de Melo 27850d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 279e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2805eb2dd2aSJiri Olsa evsel__delete(evsel); 28150d08e47SArnaldo Carvalho de Melo return -1; 28250d08e47SArnaldo Carvalho de Melo } 28350d08e47SArnaldo Carvalho de Melo 284e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 28579695e1bSArnaldo Carvalho de Melo { 28679695e1bSArnaldo Carvalho de Melo size_t i; 28779695e1bSArnaldo Carvalho de Melo 28879695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 28979695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 29079695e1bSArnaldo Carvalho de Melo 291a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 29279695e1bSArnaldo Carvalho de Melo } 29379695e1bSArnaldo Carvalho de Melo 29432dcd021SJiri Olsa struct evsel * 29563503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 296ee29be62SArnaldo Carvalho de Melo { 29732dcd021SJiri Olsa struct evsel *evsel; 298ee29be62SArnaldo Carvalho de Melo 299e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3001fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3011fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 302ee29be62SArnaldo Carvalho de Melo return evsel; 303ee29be62SArnaldo Carvalho de Melo } 304ee29be62SArnaldo Carvalho de Melo 305ee29be62SArnaldo Carvalho de Melo return NULL; 306ee29be62SArnaldo Carvalho de Melo } 307ee29be62SArnaldo Carvalho de Melo 30832dcd021SJiri Olsa struct evsel * 30963503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist, 310a2f2804aSDavid Ahern const char *name) 311a2f2804aSDavid Ahern { 31232dcd021SJiri Olsa struct evsel *evsel; 313a2f2804aSDavid Ahern 314e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3151fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 316a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 317a2f2804aSDavid Ahern return evsel; 318a2f2804aSDavid Ahern } 319a2f2804aSDavid Ahern 320a2f2804aSDavid Ahern return NULL; 321a2f2804aSDavid Ahern } 322a2f2804aSDavid Ahern 323e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler) 32439876e7dSArnaldo Carvalho de Melo { 3258f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__newtp(sys, name); 32639876e7dSArnaldo Carvalho de Melo 3278dd2a131SJiri Olsa if (IS_ERR(evsel)) 32839876e7dSArnaldo Carvalho de Melo return -1; 32939876e7dSArnaldo Carvalho de Melo 330744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 331a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 33239876e7dSArnaldo Carvalho de Melo return 0; 33339876e7dSArnaldo Carvalho de Melo } 33439876e7dSArnaldo Carvalho de Melo 33563503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist, 33632dcd021SJiri Olsa struct evsel *evsel) 337bf8e8f4bSAdrian Hunter { 338648b5af3SJiri Olsa if (evsel->core.system_wide) 339bf8e8f4bSAdrian Hunter return 1; 340bf8e8f4bSAdrian Hunter else 341a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 342bf8e8f4bSAdrian Hunter } 343bf8e8f4bSAdrian Hunter 344a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist) 345a8cbe40fSAndi Kleen { 346a8cbe40fSAndi Kleen struct evsel *pos; 347a8cbe40fSAndi Kleen 348a8cbe40fSAndi Kleen /* 349a8cbe40fSAndi Kleen * Reset the per evsel cpu_iter. This is needed because 350a8cbe40fSAndi Kleen * each evsel's cpumap may have a different index space, 351a8cbe40fSAndi Kleen * and some operations need the index to modify 352a8cbe40fSAndi Kleen * the FD xyarray (e.g. open, close) 353a8cbe40fSAndi Kleen */ 354a8cbe40fSAndi Kleen evlist__for_each_entry(evlist, pos) 355a8cbe40fSAndi Kleen pos->cpu_iter = 0; 356a8cbe40fSAndi Kleen } 357a8cbe40fSAndi Kleen 358a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu) 359a8cbe40fSAndi Kleen { 360a8cbe40fSAndi Kleen if (ev->cpu_iter >= ev->core.cpus->nr) 361a8cbe40fSAndi Kleen return true; 362a8cbe40fSAndi Kleen if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu) 363a8cbe40fSAndi Kleen return true; 364a8cbe40fSAndi Kleen return false; 365a8cbe40fSAndi Kleen } 366a8cbe40fSAndi Kleen 367a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu) 368a8cbe40fSAndi Kleen { 369a8cbe40fSAndi Kleen if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) { 370a8cbe40fSAndi Kleen ev->cpu_iter++; 371a8cbe40fSAndi Kleen return false; 372a8cbe40fSAndi Kleen } 373a8cbe40fSAndi Kleen return true; 374a8cbe40fSAndi Kleen } 375a8cbe40fSAndi Kleen 376e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3774152ab37SArnaldo Carvalho de Melo { 37832dcd021SJiri Olsa struct evsel *pos; 379704e2f5bSAndi Kleen struct affinity affinity; 38087cf8360SAdrian Hunter int cpu, i, imm = 0; 38187cf8360SAdrian Hunter bool has_imm = false; 382704e2f5bSAndi Kleen 383704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 384704e2f5bSAndi Kleen return; 385704e2f5bSAndi Kleen 38687cf8360SAdrian Hunter /* Disable 'immediate' events last */ 38787cf8360SAdrian Hunter for (imm = 0; imm <= 1; imm++) { 388704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 389704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 3904152ab37SArnaldo Carvalho de Melo 391e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 392704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 393704e2f5bSAndi Kleen continue; 394c754c382SArnaldo Carvalho de Melo if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd) 3953fe4430dSJiri Olsa continue; 39687cf8360SAdrian Hunter if (pos->immediate) 39787cf8360SAdrian Hunter has_imm = true; 39887cf8360SAdrian Hunter if (pos->immediate != imm) 39987cf8360SAdrian Hunter continue; 400704e2f5bSAndi Kleen evsel__disable_cpu(pos, pos->cpu_iter - 1); 401704e2f5bSAndi Kleen } 402704e2f5bSAndi Kleen } 40387cf8360SAdrian Hunter if (!has_imm) 40487cf8360SAdrian Hunter break; 40587cf8360SAdrian Hunter } 40687cf8360SAdrian Hunter 407704e2f5bSAndi Kleen affinity__cleanup(&affinity); 408704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 409c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 410704e2f5bSAndi Kleen continue; 411704e2f5bSAndi Kleen pos->disabled = true; 4124152ab37SArnaldo Carvalho de Melo } 4132b56bcfbSArnaldo Carvalho de Melo 4142b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 4154152ab37SArnaldo Carvalho de Melo } 4164152ab37SArnaldo Carvalho de Melo 4171c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 418764e16a3SDavid Ahern { 41932dcd021SJiri Olsa struct evsel *pos; 420704e2f5bSAndi Kleen struct affinity affinity; 421704e2f5bSAndi Kleen int cpu, i; 422764e16a3SDavid Ahern 423704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 424704e2f5bSAndi Kleen return; 425704e2f5bSAndi Kleen 426704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 427704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 428704e2f5bSAndi Kleen 429704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 430704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 431704e2f5bSAndi Kleen continue; 432c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 433704e2f5bSAndi Kleen continue; 434704e2f5bSAndi Kleen evsel__enable_cpu(pos, pos->cpu_iter - 1); 435704e2f5bSAndi Kleen } 436704e2f5bSAndi Kleen } 437704e2f5bSAndi Kleen affinity__cleanup(&affinity); 438e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 439c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 4403fe4430dSJiri Olsa continue; 441704e2f5bSAndi Kleen pos->disabled = false; 442764e16a3SDavid Ahern } 4432b56bcfbSArnaldo Carvalho de Melo 4442b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 4452b56bcfbSArnaldo Carvalho de Melo } 4462b56bcfbSArnaldo Carvalho de Melo 44763503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist) 4482b56bcfbSArnaldo Carvalho de Melo { 449e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 450764e16a3SDavid Ahern } 451764e16a3SDavid Ahern 45263503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist, 45332dcd021SJiri Olsa struct evsel *evsel, int cpu) 4541c65056cSAdrian Hunter { 45518ef15c6SArnaldo Carvalho de Melo int thread; 4561c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 4571c65056cSAdrian Hunter 4589dfcb759SJiri Olsa if (!evsel->core.fd) 4591c65056cSAdrian Hunter return -EINVAL; 4601c65056cSAdrian Hunter 4611c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 46218ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4631c65056cSAdrian Hunter if (err) 4641c65056cSAdrian Hunter return err; 4651c65056cSAdrian Hunter } 4661c65056cSAdrian Hunter return 0; 4671c65056cSAdrian Hunter } 4681c65056cSAdrian Hunter 46963503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist, 47032dcd021SJiri Olsa struct evsel *evsel, 4711c65056cSAdrian Hunter int thread) 4721c65056cSAdrian Hunter { 47318ef15c6SArnaldo Carvalho de Melo int cpu; 4746549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 4751c65056cSAdrian Hunter 4769dfcb759SJiri Olsa if (!evsel->core.fd) 4771c65056cSAdrian Hunter return -EINVAL; 4781c65056cSAdrian Hunter 4791c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 48018ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4811c65056cSAdrian Hunter if (err) 4821c65056cSAdrian Hunter return err; 4831c65056cSAdrian Hunter } 4841c65056cSAdrian Hunter return 0; 4851c65056cSAdrian Hunter } 4861c65056cSAdrian Hunter 48763503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist, 48832dcd021SJiri Olsa struct evsel *evsel, int idx) 4891c65056cSAdrian Hunter { 490315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 4911c65056cSAdrian Hunter 4921c65056cSAdrian Hunter if (per_cpu_mmaps) 4931c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4941c65056cSAdrian Hunter else 4951c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4961c65056cSAdrian Hunter } 4971c65056cSAdrian Hunter 498f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 499e4b356b5SArnaldo Carvalho de Melo { 500ab4c1f9fSAlexey Budankov return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default); 501e4b356b5SArnaldo Carvalho de Melo } 5021b85337dSArnaldo Carvalho de Melo 503f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 5041ddec7f0SArnaldo Carvalho de Melo { 50584227cb1SJiri Olsa return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 5061ddec7f0SArnaldo Carvalho de Melo } 5071ddec7f0SArnaldo Carvalho de Melo 50880ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 509f66a889dSArnaldo Carvalho de Melo { 51080ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 511f66a889dSArnaldo Carvalho de Melo } 512f66a889dSArnaldo Carvalho de Melo 51363503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 51470db7533SArnaldo Carvalho de Melo { 51570db7533SArnaldo Carvalho de Melo struct hlist_head *head; 51670db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 51770db7533SArnaldo Carvalho de Melo int hash; 51870db7533SArnaldo Carvalho de Melo 51970db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5201d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 52170db7533SArnaldo Carvalho de Melo 522b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 52370db7533SArnaldo Carvalho de Melo if (sid->id == id) 524932a3594SJiri Olsa return sid; 525932a3594SJiri Olsa 526932a3594SJiri Olsa return NULL; 527932a3594SJiri Olsa } 528932a3594SJiri Olsa 52963503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 530932a3594SJiri Olsa { 531932a3594SJiri Olsa struct perf_sample_id *sid; 532932a3594SJiri Olsa 5336484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 534515dbe48SJiri Olsa return evlist__first(evlist); 535932a3594SJiri Olsa 536932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 537932a3594SJiri Olsa if (sid) 53870c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 53930e68bccSNamhyung Kim 5408cedf3a5SArnaldo Carvalho de Melo if (!evlist__sample_id_all(evlist)) 541515dbe48SJiri Olsa return evlist__first(evlist); 54230e68bccSNamhyung Kim 54370db7533SArnaldo Carvalho de Melo return NULL; 54470db7533SArnaldo Carvalho de Melo } 54504391debSArnaldo Carvalho de Melo 54663503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 547dddcf6abSAdrian Hunter u64 id) 548dddcf6abSAdrian Hunter { 549dddcf6abSAdrian Hunter struct perf_sample_id *sid; 550dddcf6abSAdrian Hunter 551dddcf6abSAdrian Hunter if (!id) 552dddcf6abSAdrian Hunter return NULL; 553dddcf6abSAdrian Hunter 554dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 555dddcf6abSAdrian Hunter if (sid) 55670c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 557dddcf6abSAdrian Hunter 558dddcf6abSAdrian Hunter return NULL; 559dddcf6abSAdrian Hunter } 560dddcf6abSAdrian Hunter 56163503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist, 56275562573SAdrian Hunter union perf_event *event, u64 *id) 56375562573SAdrian Hunter { 564b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 56575562573SAdrian Hunter ssize_t n; 56675562573SAdrian Hunter 56775562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 56875562573SAdrian Hunter 56975562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 57075562573SAdrian Hunter if (evlist->id_pos >= n) 57175562573SAdrian Hunter return -1; 57275562573SAdrian Hunter *id = array[evlist->id_pos]; 57375562573SAdrian Hunter } else { 57475562573SAdrian Hunter if (evlist->is_pos > n) 57575562573SAdrian Hunter return -1; 57675562573SAdrian Hunter n -= evlist->is_pos; 57775562573SAdrian Hunter *id = array[n]; 57875562573SAdrian Hunter } 57975562573SAdrian Hunter return 0; 58075562573SAdrian Hunter } 58175562573SAdrian Hunter 58263503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 58375562573SAdrian Hunter union perf_event *event) 58475562573SAdrian Hunter { 585515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 58675562573SAdrian Hunter struct hlist_head *head; 58775562573SAdrian Hunter struct perf_sample_id *sid; 58875562573SAdrian Hunter int hash; 58975562573SAdrian Hunter u64 id; 59075562573SAdrian Hunter 5916484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 59298be6966SAdrian Hunter return first; 59398be6966SAdrian Hunter 5941fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 59598be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 59698be6966SAdrian Hunter return first; 59775562573SAdrian Hunter 59875562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 59975562573SAdrian Hunter return NULL; 60075562573SAdrian Hunter 60175562573SAdrian Hunter /* Synthesized events have an id of zero */ 60275562573SAdrian Hunter if (!id) 60398be6966SAdrian Hunter return first; 60475562573SAdrian Hunter 60575562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 6061d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 60775562573SAdrian Hunter 60875562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 60975562573SAdrian Hunter if (sid->id == id) 61070c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 61175562573SAdrian Hunter } 61275562573SAdrian Hunter return NULL; 61375562573SAdrian Hunter } 61475562573SAdrian Hunter 61563503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value) 61665aea233SWang Nan { 61765aea233SWang Nan int i; 61865aea233SWang Nan 6190b72d69aSWang Nan if (!evlist->overwrite_mmap) 620078c3386SWang Nan return 0; 621078c3386SWang Nan 622c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6232cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 62465aea233SWang Nan int err; 62565aea233SWang Nan 62665aea233SWang Nan if (fd < 0) 62765aea233SWang Nan continue; 62865aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 62965aea233SWang Nan if (err) 63065aea233SWang Nan return err; 63165aea233SWang Nan } 63265aea233SWang Nan return 0; 63365aea233SWang Nan } 63465aea233SWang Nan 63563503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist) 63665aea233SWang Nan { 63765aea233SWang Nan return perf_evlist__set_paused(evlist, true); 63865aea233SWang Nan } 63965aea233SWang Nan 64063503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist) 64165aea233SWang Nan { 64265aea233SWang Nan return perf_evlist__set_paused(evlist, false); 64365aea233SWang Nan } 64465aea233SWang Nan 645db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 646f8a95309SArnaldo Carvalho de Melo { 647aece948fSArnaldo Carvalho de Melo int i; 648f8a95309SArnaldo Carvalho de Melo 649b2cb615dSWang Nan if (evlist->mmap) 650c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 65180e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 652b2cb615dSWang Nan 6530b72d69aSWang Nan if (evlist->overwrite_mmap) 654c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 65580e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 656a1f72618SWang Nan } 657aece948fSArnaldo Carvalho de Melo 658db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 659a1f72618SWang Nan { 660db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 66104662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6620b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 663f8a95309SArnaldo Carvalho de Melo } 664f8a95309SArnaldo Carvalho de Melo 66580e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 66680e53d11SJiri Olsa { 66780e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 66880e53d11SJiri Olsa 66980e53d11SJiri Olsa mmap__munmap(m); 67080e53d11SJiri Olsa } 67180e53d11SJiri Olsa 672d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 6732c5f6d87SKan Liang bool overwrite) 674f8a95309SArnaldo Carvalho de Melo { 675d4c6fb36SWang Nan int i; 676a5830532SJiri Olsa struct mmap *map; 677d4c6fb36SWang Nan 678c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 6798db6d6b1SWang Nan if (!map) 6808db6d6b1SWang Nan return NULL; 681946ae1d4SWang Nan 682c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6836eb65f7aSJiri Olsa struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 6846eb65f7aSJiri Olsa 6854738ca30SArnaldo Carvalho de Melo /* 6864738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 6876afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 6884738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 6894738ca30SArnaldo Carvalho de Melo * dropped. 6904738ca30SArnaldo Carvalho de Melo * 6914738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 6924738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 6934738ca30SArnaldo Carvalho de Melo */ 6946eb65f7aSJiri Olsa perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 6954738ca30SArnaldo Carvalho de Melo } 696353120b4SJiri Olsa 6978db6d6b1SWang Nan return map; 698f8a95309SArnaldo Carvalho de Melo } 699f8a95309SArnaldo Carvalho de Melo 7009abd2ab2SJiri Olsa static void 7019abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 7029abd2ab2SJiri Olsa struct perf_mmap_param *_mp, 7039abd2ab2SJiri Olsa int idx, bool per_cpu) 7049abd2ab2SJiri Olsa { 7059abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7069abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 7079abd2ab2SJiri Olsa 7089abd2ab2SJiri Olsa auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 7099abd2ab2SJiri Olsa } 7109abd2ab2SJiri Olsa 711bb1b1885SJiri Olsa static struct perf_mmap* 712bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 713bb1b1885SJiri Olsa { 714bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7153805e4f3SJiri Olsa struct mmap *maps; 716bb1b1885SJiri Olsa 7173805e4f3SJiri Olsa maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 718bb1b1885SJiri Olsa 719bb1b1885SJiri Olsa if (!maps) { 7203805e4f3SJiri Olsa maps = evlist__alloc_mmap(evlist, overwrite); 721bb1b1885SJiri Olsa if (!maps) 722bb1b1885SJiri Olsa return NULL; 723bb1b1885SJiri Olsa 7243805e4f3SJiri Olsa if (overwrite) { 725bb1b1885SJiri Olsa evlist->overwrite_mmap = maps; 726bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 727bb1b1885SJiri Olsa perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 7283805e4f3SJiri Olsa } else { 7293805e4f3SJiri Olsa evlist->mmap = maps; 730bb1b1885SJiri Olsa } 731bb1b1885SJiri Olsa } 732bb1b1885SJiri Olsa 733bb1b1885SJiri Olsa return &maps[idx].core; 734bb1b1885SJiri Olsa } 735bb1b1885SJiri Olsa 736b80132b1SJiri Olsa static int 737b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 738b80132b1SJiri Olsa int output, int cpu) 739b80132b1SJiri Olsa { 740b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core); 741b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 742b80132b1SJiri Olsa 743b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu); 744b80132b1SJiri Olsa } 745b80132b1SJiri Olsa 746f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 747994a1f78SJiri Olsa { 748f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 7498185e881SArnaldo Carvalho de Melo int max; 7508185e881SArnaldo Carvalho de Melo 7518185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 7528185e881SArnaldo Carvalho de Melo /* 7538185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 7548185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 7558185e881SArnaldo Carvalho de Melo * die yet... 7568185e881SArnaldo Carvalho de Melo */ 7578185e881SArnaldo Carvalho de Melo max = 512; 7588185e881SArnaldo Carvalho de Melo } else { 7598185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 7608185e881SArnaldo Carvalho de Melo } 7618185e881SArnaldo Carvalho de Melo 7628185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 7631be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 7641be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 765f5e7150cSArnaldo Carvalho de Melo 766f5e7150cSArnaldo Carvalho de Melo return pages; 767f5e7150cSArnaldo Carvalho de Melo } 768f5e7150cSArnaldo Carvalho de Melo 7699521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 770f5e7150cSArnaldo Carvalho de Melo { 771f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 772f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 773f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 774994a1f78SJiri Olsa return 0; 775994a1f78SJiri Olsa 776994a1f78SJiri Olsa return (pages + 1) * page_size; 777994a1f78SJiri Olsa } 778994a1f78SJiri Olsa 77933c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 78033c2dcfdSDavid Ahern unsigned long max) 781994a1f78SJiri Olsa { 7822fbe4abeSAdrian Hunter unsigned long pages, val; 78327050f53SJiri Olsa static struct parse_tag tags[] = { 78427050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 78527050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 78627050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 78727050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 78827050f53SJiri Olsa { .tag = 0 }, 78927050f53SJiri Olsa }; 790994a1f78SJiri Olsa 7918973504bSDavid Ahern if (str == NULL) 79233c2dcfdSDavid Ahern return -EINVAL; 7938973504bSDavid Ahern 79427050f53SJiri Olsa val = parse_tag_value(str, tags); 7952fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 79627050f53SJiri Olsa /* we got file size value */ 79727050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 79827050f53SJiri Olsa } else { 79927050f53SJiri Olsa /* we got pages count value */ 80027050f53SJiri Olsa char *eptr; 801994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 80233c2dcfdSDavid Ahern if (*eptr != '\0') 80333c2dcfdSDavid Ahern return -EINVAL; 80427050f53SJiri Olsa } 805994a1f78SJiri Olsa 8062bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 80733c2dcfdSDavid Ahern /* leave number of pages at 0 */ 8081dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 8099808143bSJiri Olsa char buf[100]; 8109808143bSJiri Olsa 81133c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 81291529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 8131dbfa938SAdrian Hunter if (!pages) 8141dbfa938SAdrian Hunter return -EINVAL; 8159808143bSJiri Olsa 8169808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 8179808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 8189808143bSJiri Olsa buf, pages); 8192fbe4abeSAdrian Hunter } 8202fbe4abeSAdrian Hunter 82133c2dcfdSDavid Ahern if (pages > max) 82233c2dcfdSDavid Ahern return -EINVAL; 82333c2dcfdSDavid Ahern 82433c2dcfdSDavid Ahern return pages; 82533c2dcfdSDavid Ahern } 82633c2dcfdSDavid Ahern 827e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 82833c2dcfdSDavid Ahern { 82933c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 83033c2dcfdSDavid Ahern long pages; 83133c2dcfdSDavid Ahern 832f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 83333c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 83433c2dcfdSDavid Ahern 83533c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 83633c2dcfdSDavid Ahern if (pages < 0) { 83733c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 838994a1f78SJiri Olsa return -1; 839994a1f78SJiri Olsa } 840994a1f78SJiri Olsa 841994a1f78SJiri Olsa *mmap_pages = pages; 842994a1f78SJiri Olsa return 0; 843994a1f78SJiri Olsa } 844994a1f78SJiri Olsa 845e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 846e9db1310SAdrian Hunter int unset __maybe_unused) 847e9db1310SAdrian Hunter { 848e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 849e9db1310SAdrian Hunter } 850e9db1310SAdrian Hunter 851c83fa7f2SAdrian Hunter /** 8529521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 853c83fa7f2SAdrian Hunter * @evlist: list of events 854c83fa7f2SAdrian Hunter * @pages: map length in pages 855c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 856718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 857718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 858f8a95309SArnaldo Carvalho de Melo * 859c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 8609521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 861c83fa7f2SAdrian Hunter * automatically. 862f8a95309SArnaldo Carvalho de Melo * 863718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 864718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 865718c602dSAdrian Hunter * 866c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 867f8a95309SArnaldo Carvalho de Melo */ 8689521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 8697a276ff6SWang Nan unsigned int auxtrace_pages, 87051255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 87151255a8aSAlexey Budankov int comp_level) 872f8a95309SArnaldo Carvalho de Melo { 87371f566a3SWang Nan /* 87471f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 87571f566a3SWang Nan * Its value is decided by evsel's write_backward. 87671f566a3SWang Nan * So &mp should not be passed through const pointer. 87771f566a3SWang Nan */ 878e440979fSJiri Olsa struct mmap_params mp = { 879e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 880e440979fSJiri Olsa .affinity = affinity, 881e440979fSJiri Olsa .flush = flush, 882e440979fSJiri Olsa .comp_level = comp_level 883e440979fSJiri Olsa }; 884923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = { 8859abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx, 886bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get, 887b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap, 8889abd2ab2SJiri Olsa }; 88950a682ceSArnaldo Carvalho de Melo 890f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 891f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 892f8a95309SArnaldo Carvalho de Melo 893f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 894718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 895718c602dSAdrian Hunter 896923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 897f8a95309SArnaldo Carvalho de Melo } 8987e2ed097SArnaldo Carvalho de Melo 8999521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 900718c602dSAdrian Hunter { 9019521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 902718c602dSAdrian Hunter } 903718c602dSAdrian Hunter 90463503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 9057e2ed097SArnaldo Carvalho de Melo { 906147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 907f854839bSJiri Olsa struct perf_cpu_map *cpus; 9089749b90eSJiri Olsa struct perf_thread_map *threads; 9097e2ed097SArnaldo Carvalho de Melo 910147c508fSJin Yao /* 911147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 912147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 913147c508fSJin Yao * target->system_wide = true. 914147c508fSJin Yao * 915147c508fSJin Yao * If specify '--per-thread' only to perf record, 916147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 917147c508fSJin Yao * 918147c508fSJin Yao * So target->per_thread && target->system_wide is false. 919147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 920147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 921147c508fSJin Yao * current behavior. 922147c508fSJin Yao * 923147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 924147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 925147c508fSJin Yao * per-thread data. thread_map__new_str will call 926147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 927147c508fSJin Yao */ 92873c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 929147c508fSJin Yao all_threads); 93074bfd2b2SAdrian Hunter 93174bfd2b2SAdrian Hunter if (!threads) 9327e2ed097SArnaldo Carvalho de Melo return -1; 9337e2ed097SArnaldo Carvalho de Melo 9349c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 935397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 936879d77d0SNamhyung Kim else 9379c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 9387e2ed097SArnaldo Carvalho de Melo 93974bfd2b2SAdrian Hunter if (!cpus) 9407e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 9417e2ed097SArnaldo Carvalho de Melo 942ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 943ec9a77a7SAdrian Hunter 944453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 945d5bc056eSAdrian Hunter 946d5bc056eSAdrian Hunter return 0; 9477e2ed097SArnaldo Carvalho de Melo 9487e2ed097SArnaldo Carvalho de Melo out_delete_threads: 9497836e52eSJiri Olsa perf_thread_map__put(threads); 9507e2ed097SArnaldo Carvalho de Melo return -1; 9517e2ed097SArnaldo Carvalho de Melo } 9527e2ed097SArnaldo Carvalho de Melo 95363503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist, 95422c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 95522c8a376SArnaldo Carvalho de Melo { 95632dcd021SJiri Olsa struct evsel *evsel; 95722c8a376SArnaldo Carvalho de Melo 958e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 959862b2f8fSArnaldo Carvalho de Melo __evsel__set_sample_bit(evsel, bit); 96022c8a376SArnaldo Carvalho de Melo } 96122c8a376SArnaldo Carvalho de Melo 96263503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist, 96322c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 96422c8a376SArnaldo Carvalho de Melo { 96532dcd021SJiri Olsa struct evsel *evsel; 96622c8a376SArnaldo Carvalho de Melo 967e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 968862b2f8fSArnaldo Carvalho de Melo __evsel__reset_sample_bit(evsel, bit); 96922c8a376SArnaldo Carvalho de Melo } 97022c8a376SArnaldo Carvalho de Melo 97163503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 9720a102479SFrederic Weisbecker { 97332dcd021SJiri Olsa struct evsel *evsel; 974745cefc5SArnaldo Carvalho de Melo int err = 0; 9750a102479SFrederic Weisbecker 976e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 977745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 9780a102479SFrederic Weisbecker continue; 979745cefc5SArnaldo Carvalho de Melo 980d988d5eeSKan Liang /* 981d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 982d988d5eeSKan Liang * So evlist and evsel should always be same. 983d988d5eeSKan Liang */ 984a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 98523d4aad4SArnaldo Carvalho de Melo if (err) { 98623d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 987745cefc5SArnaldo Carvalho de Melo break; 9880a102479SFrederic Weisbecker } 98923d4aad4SArnaldo Carvalho de Melo } 9900a102479SFrederic Weisbecker 991745cefc5SArnaldo Carvalho de Melo return err; 992745cefc5SArnaldo Carvalho de Melo } 993745cefc5SArnaldo Carvalho de Melo 99463503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 995745cefc5SArnaldo Carvalho de Melo { 99632dcd021SJiri Olsa struct evsel *evsel; 997745cefc5SArnaldo Carvalho de Melo int err = 0; 998745cefc5SArnaldo Carvalho de Melo 99905cea449SArnaldo Carvalho de Melo if (filter == NULL) 100005cea449SArnaldo Carvalho de Melo return -1; 100105cea449SArnaldo Carvalho de Melo 1002e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 10031fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1004fdf14720SWang Nan continue; 1005fdf14720SWang Nan 1006ad681adfSArnaldo Carvalho de Melo err = evsel__set_filter(evsel, filter); 1007745cefc5SArnaldo Carvalho de Melo if (err) 1008745cefc5SArnaldo Carvalho de Melo break; 1009745cefc5SArnaldo Carvalho de Melo } 1010745cefc5SArnaldo Carvalho de Melo 1011745cefc5SArnaldo Carvalho de Melo return err; 10120a102479SFrederic Weisbecker } 101374429964SFrederic Weisbecker 101453c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter) 101553c92f73SArnaldo Carvalho de Melo { 101653c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 101753c92f73SArnaldo Carvalho de Melo int err = 0; 101853c92f73SArnaldo Carvalho de Melo 101953c92f73SArnaldo Carvalho de Melo if (filter == NULL) 102053c92f73SArnaldo Carvalho de Melo return -1; 102153c92f73SArnaldo Carvalho de Melo 102253c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 102353c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 102453c92f73SArnaldo Carvalho de Melo continue; 102553c92f73SArnaldo Carvalho de Melo 1026ad681adfSArnaldo Carvalho de Melo err = evsel__append_tp_filter(evsel, filter); 102753c92f73SArnaldo Carvalho de Melo if (err) 102853c92f73SArnaldo Carvalho de Melo break; 102953c92f73SArnaldo Carvalho de Melo } 103053c92f73SArnaldo Carvalho de Melo 103153c92f73SArnaldo Carvalho de Melo return err; 103253c92f73SArnaldo Carvalho de Melo } 103353c92f73SArnaldo Carvalho de Melo 1034da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1035cfd70a26SArnaldo Carvalho de Melo { 1036cfd70a26SArnaldo Carvalho de Melo char *filter; 1037be199adaSArnaldo Carvalho de Melo size_t i; 1038cfd70a26SArnaldo Carvalho de Melo 1039be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1040be199adaSArnaldo Carvalho de Melo if (i == 0) { 1041be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 104205cea449SArnaldo Carvalho de Melo return NULL; 1043be199adaSArnaldo Carvalho de Melo } else { 1044be199adaSArnaldo Carvalho de Melo char *tmp; 1045be199adaSArnaldo Carvalho de Melo 1046be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1047be199adaSArnaldo Carvalho de Melo goto out_free; 1048be199adaSArnaldo Carvalho de Melo 1049be199adaSArnaldo Carvalho de Melo free(filter); 1050be199adaSArnaldo Carvalho de Melo filter = tmp; 1051be199adaSArnaldo Carvalho de Melo } 1052be199adaSArnaldo Carvalho de Melo } 1053cfd70a26SArnaldo Carvalho de Melo 105405cea449SArnaldo Carvalho de Melo return filter; 1055be199adaSArnaldo Carvalho de Melo out_free: 1056cfd70a26SArnaldo Carvalho de Melo free(filter); 105705cea449SArnaldo Carvalho de Melo return NULL; 105805cea449SArnaldo Carvalho de Melo } 105905cea449SArnaldo Carvalho de Melo 106005cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 106105cea449SArnaldo Carvalho de Melo { 106205cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 106305cea449SArnaldo Carvalho de Melo int ret = perf_evlist__set_tp_filter(evlist, filter); 106405cea449SArnaldo Carvalho de Melo 106505cea449SArnaldo Carvalho de Melo free(filter); 1066cfd70a26SArnaldo Carvalho de Melo return ret; 1067cfd70a26SArnaldo Carvalho de Melo } 1068cfd70a26SArnaldo Carvalho de Melo 106963503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1070be199adaSArnaldo Carvalho de Melo { 10717ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1072be199adaSArnaldo Carvalho de Melo } 1073be199adaSArnaldo Carvalho de Melo 10741827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 10751827ab5bSArnaldo Carvalho de Melo { 10761827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 10771827ab5bSArnaldo Carvalho de Melo int ret = perf_evlist__append_tp_filter(evlist, filter); 10781827ab5bSArnaldo Carvalho de Melo 10791827ab5bSArnaldo Carvalho de Melo free(filter); 10801827ab5bSArnaldo Carvalho de Melo return ret; 10811827ab5bSArnaldo Carvalho de Melo } 10821827ab5bSArnaldo Carvalho de Melo 10831827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 10841827ab5bSArnaldo Carvalho de Melo { 10851827ab5bSArnaldo Carvalho de Melo return perf_evlist__append_tp_filter_pids(evlist, 1, &pid); 10861827ab5bSArnaldo Carvalho de Melo } 10871827ab5bSArnaldo Carvalho de Melo 1088b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist) 108974429964SFrederic Weisbecker { 109032dcd021SJiri Olsa struct evsel *pos; 1091c2a70653SArnaldo Carvalho de Melo 10926484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 109375562573SAdrian Hunter return true; 109475562573SAdrian Hunter 109575562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 109675562573SAdrian Hunter return false; 109775562573SAdrian Hunter 1098e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 109975562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 110075562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1101c2a70653SArnaldo Carvalho de Melo return false; 110274429964SFrederic Weisbecker } 110374429964SFrederic Weisbecker 1104c2a70653SArnaldo Carvalho de Melo return true; 1105c2a70653SArnaldo Carvalho de Melo } 1106c2a70653SArnaldo Carvalho de Melo 1107b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist) 1108c2a70653SArnaldo Carvalho de Melo { 110932dcd021SJiri Olsa struct evsel *evsel; 111075562573SAdrian Hunter 111175562573SAdrian Hunter if (evlist->combined_sample_type) 111275562573SAdrian Hunter return evlist->combined_sample_type; 111375562573SAdrian Hunter 1114e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11151fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 111675562573SAdrian Hunter 111775562573SAdrian Hunter return evlist->combined_sample_type; 111875562573SAdrian Hunter } 111975562573SAdrian Hunter 1120b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist) 112175562573SAdrian Hunter { 112275562573SAdrian Hunter evlist->combined_sample_type = 0; 1123b3c2cc2bSArnaldo Carvalho de Melo return __evlist__combined_sample_type(evlist); 1124c2a70653SArnaldo Carvalho de Melo } 1125c2a70653SArnaldo Carvalho de Melo 112692c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist) 112798df858eSAndi Kleen { 112832dcd021SJiri Olsa struct evsel *evsel; 112998df858eSAndi Kleen u64 branch_type = 0; 113098df858eSAndi Kleen 1131e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11321fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 113398df858eSAndi Kleen return branch_type; 113498df858eSAndi Kleen } 113598df858eSAndi Kleen 113663503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 11379ede473cSJiri Olsa { 1138515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 11391fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 11401fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 11419ede473cSJiri Olsa 1142e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 114394d3820fSAdrian Hunter if (read_format != pos->core.attr.read_format) { 114494d3820fSAdrian Hunter pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n", 114594d3820fSAdrian Hunter read_format, (u64)pos->core.attr.read_format); 114694d3820fSAdrian Hunter } 11479ede473cSJiri Olsa } 11489ede473cSJiri Olsa 11499ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 11509ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 11519ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 11529ede473cSJiri Olsa return false; 11539ede473cSJiri Olsa } 11549ede473cSJiri Olsa 11559ede473cSJiri Olsa return true; 11569ede473cSJiri Olsa } 11579ede473cSJiri Olsa 115863503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 115981e36bffSArnaldo Carvalho de Melo { 1160515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 116181e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 116281e36bffSArnaldo Carvalho de Melo u64 sample_type; 116381e36bffSArnaldo Carvalho de Melo u16 size = 0; 116481e36bffSArnaldo Carvalho de Melo 11651fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 116681e36bffSArnaldo Carvalho de Melo goto out; 116781e36bffSArnaldo Carvalho de Melo 11681fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 116981e36bffSArnaldo Carvalho de Melo 117081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 117181e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 117281e36bffSArnaldo Carvalho de Melo 117381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 117481e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 117581e36bffSArnaldo Carvalho de Melo 117681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 117781e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 117881e36bffSArnaldo Carvalho de Melo 117981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 118081e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 118181e36bffSArnaldo Carvalho de Melo 118281e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 118381e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 118475562573SAdrian Hunter 118575562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 118675562573SAdrian Hunter size += sizeof(data->id); 118781e36bffSArnaldo Carvalho de Melo out: 118881e36bffSArnaldo Carvalho de Melo return size; 118981e36bffSArnaldo Carvalho de Melo } 119081e36bffSArnaldo Carvalho de Melo 11918cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist) 1192c2a70653SArnaldo Carvalho de Melo { 1193515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1194c2a70653SArnaldo Carvalho de Melo 1195e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 11961fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1197c2a70653SArnaldo Carvalho de Melo return false; 1198c2a70653SArnaldo Carvalho de Melo } 1199c2a70653SArnaldo Carvalho de Melo 1200c2a70653SArnaldo Carvalho de Melo return true; 120174429964SFrederic Weisbecker } 120274429964SFrederic Weisbecker 12038cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist) 120474429964SFrederic Weisbecker { 1205515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 12061fc632ceSJiri Olsa return first->core.attr.sample_id_all; 120774429964SFrederic Weisbecker } 120881cce8deSArnaldo Carvalho de Melo 120963503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 121032dcd021SJiri Olsa struct evsel *evsel) 121181cce8deSArnaldo Carvalho de Melo { 121281cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 121381cce8deSArnaldo Carvalho de Melo } 1214727ab04eSArnaldo Carvalho de Melo 1215750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1216a74b4b66SNamhyung Kim { 121732dcd021SJiri Olsa struct evsel *evsel; 12187736627bSAndi Kleen struct affinity affinity; 12197736627bSAndi Kleen int cpu, i; 1220a74b4b66SNamhyung Kim 12217736627bSAndi Kleen /* 12227736627bSAndi Kleen * With perf record core.cpus is usually NULL. 12237736627bSAndi Kleen * Use the old method to handle this for now. 12247736627bSAndi Kleen */ 12257736627bSAndi Kleen if (!evlist->core.cpus) { 1226475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 122788761fa1SJiri Olsa evsel__close(evsel); 12287736627bSAndi Kleen return; 12297736627bSAndi Kleen } 12307736627bSAndi Kleen 12317736627bSAndi Kleen if (affinity__setup(&affinity) < 0) 12327736627bSAndi Kleen return; 12337736627bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 12347736627bSAndi Kleen affinity__set(&affinity, cpu); 12357736627bSAndi Kleen 12367736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12377736627bSAndi Kleen if (evsel__cpu_iter_skip(evsel, cpu)) 12387736627bSAndi Kleen continue; 12397736627bSAndi Kleen perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1); 12407736627bSAndi Kleen } 12417736627bSAndi Kleen } 12427736627bSAndi Kleen affinity__cleanup(&affinity); 12437736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12447736627bSAndi Kleen perf_evsel__free_fd(&evsel->core); 12457736627bSAndi Kleen perf_evsel__free_id(&evsel->core); 12467736627bSAndi Kleen } 1247a74b4b66SNamhyung Kim } 1248a74b4b66SNamhyung Kim 124963503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 12504112eb18SArnaldo Carvalho de Melo { 1251f854839bSJiri Olsa struct perf_cpu_map *cpus; 12529749b90eSJiri Olsa struct perf_thread_map *threads; 12534112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 12544112eb18SArnaldo Carvalho de Melo 12554112eb18SArnaldo Carvalho de Melo /* 12564112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 12574112eb18SArnaldo Carvalho de Melo * an all cpus map. 12584112eb18SArnaldo Carvalho de Melo * 12594112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 12604112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 12614112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 12624112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 12634112eb18SArnaldo Carvalho de Melo */ 12649c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 12658c0498b6SAdrian Hunter if (!cpus) 12664112eb18SArnaldo Carvalho de Melo goto out; 12674112eb18SArnaldo Carvalho de Melo 12684b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 12698c0498b6SAdrian Hunter if (!threads) 12708c0498b6SAdrian Hunter goto out_put; 12714112eb18SArnaldo Carvalho de Melo 1272453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 12734112eb18SArnaldo Carvalho de Melo out: 12744112eb18SArnaldo Carvalho de Melo return err; 12758c0498b6SAdrian Hunter out_put: 127638f01d8dSJiri Olsa perf_cpu_map__put(cpus); 12774112eb18SArnaldo Carvalho de Melo goto out; 12784112eb18SArnaldo Carvalho de Melo } 12794112eb18SArnaldo Carvalho de Melo 1280474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1281727ab04eSArnaldo Carvalho de Melo { 128232dcd021SJiri Olsa struct evsel *evsel; 1283a74b4b66SNamhyung Kim int err; 1284727ab04eSArnaldo Carvalho de Melo 12854112eb18SArnaldo Carvalho de Melo /* 12864112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 12874112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 12884112eb18SArnaldo Carvalho de Melo */ 128903617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 12904112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 12914112eb18SArnaldo Carvalho de Melo if (err < 0) 12924112eb18SArnaldo Carvalho de Melo goto out_err; 12934112eb18SArnaldo Carvalho de Melo } 12944112eb18SArnaldo Carvalho de Melo 1295733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1296733cd2feSAdrian Hunter 1297e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1298af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1299727ab04eSArnaldo Carvalho de Melo if (err < 0) 1300727ab04eSArnaldo Carvalho de Melo goto out_err; 1301727ab04eSArnaldo Carvalho de Melo } 1302727ab04eSArnaldo Carvalho de Melo 1303727ab04eSArnaldo Carvalho de Melo return 0; 1304727ab04eSArnaldo Carvalho de Melo out_err: 1305750b4edeSJiri Olsa evlist__close(evlist); 130641c21a68SNamhyung Kim errno = -err; 1307727ab04eSArnaldo Carvalho de Melo return err; 1308727ab04eSArnaldo Carvalho de Melo } 130935b9d88eSArnaldo Carvalho de Melo 131063503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 131155e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1312735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 131335b9d88eSArnaldo Carvalho de Melo { 131435b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 131535b9d88eSArnaldo Carvalho de Melo char bf; 131635b9d88eSArnaldo Carvalho de Melo 131735b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 131835b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 131935b9d88eSArnaldo Carvalho de Melo return -1; 132035b9d88eSArnaldo Carvalho de Melo } 132135b9d88eSArnaldo Carvalho de Melo 132235b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 132335b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 132435b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 132535b9d88eSArnaldo Carvalho de Melo } 132635b9d88eSArnaldo Carvalho de Melo 132735b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 132835b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 132935b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 133035b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 133135b9d88eSArnaldo Carvalho de Melo } 133235b9d88eSArnaldo Carvalho de Melo 133335b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13345f1c4225SArnaldo Carvalho de Melo int ret; 13355f1c4225SArnaldo Carvalho de Melo 1336119fa3c9SNamhyung Kim if (pipe_output) 133735b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 133835b9d88eSArnaldo Carvalho de Melo 13390817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13400817df08SDavid Ahern 134135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 134235b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 134335b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 134435b9d88eSArnaldo Carvalho de Melo 134535b9d88eSArnaldo Carvalho de Melo /* 134635b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 134735b9d88eSArnaldo Carvalho de Melo */ 134835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 134935b9d88eSArnaldo Carvalho de Melo 135035b9d88eSArnaldo Carvalho de Melo /* 135135b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 135235b9d88eSArnaldo Carvalho de Melo */ 13535f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 13545f1c4225SArnaldo Carvalho de Melo /* 13555f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 13565f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 13575f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 13585f1c4225SArnaldo Carvalho de Melo * 135920f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 13605f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 13615f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 13625f1c4225SArnaldo Carvalho de Melo * here. 13635f1c4225SArnaldo Carvalho de Melo */ 13645f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 13655f1c4225SArnaldo Carvalho de Melo if (ret == -1) 136635b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 13675f1c4225SArnaldo Carvalho de Melo exit(ret); 13685f1c4225SArnaldo Carvalho de Melo } 136935b9d88eSArnaldo Carvalho de Melo 137035b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 137135b9d88eSArnaldo Carvalho de Melo 1372735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1373f33cbe72SArnaldo Carvalho de Melo union sigval val; 1374f33cbe72SArnaldo Carvalho de Melo 1375f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1376f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 137735b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1378f33cbe72SArnaldo Carvalho de Melo } else 1379f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 138035b9d88eSArnaldo Carvalho de Melo exit(-1); 138135b9d88eSArnaldo Carvalho de Melo } 138235b9d88eSArnaldo Carvalho de Melo 1383735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1384735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1385735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1386735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1387735f7e0bSArnaldo Carvalho de Melo }; 1388735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1389735f7e0bSArnaldo Carvalho de Melo } 1390735f7e0bSArnaldo Carvalho de Melo 13911aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 139203617c22SJiri Olsa if (evlist->core.threads == NULL) { 13931aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 13941aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 13951aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 13961aaf63b1SArnaldo Carvalho de Melo } 139703617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 13981aaf63b1SArnaldo Carvalho de Melo } 139935b9d88eSArnaldo Carvalho de Melo 140035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 140135b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 140235b9d88eSArnaldo Carvalho de Melo /* 140335b9d88eSArnaldo Carvalho de Melo * wait for child to settle 140435b9d88eSArnaldo Carvalho de Melo */ 140535b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 140635b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 140735b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 140835b9d88eSArnaldo Carvalho de Melo } 140935b9d88eSArnaldo Carvalho de Melo 1410bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 141135b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 141235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 141335b9d88eSArnaldo Carvalho de Melo return 0; 141435b9d88eSArnaldo Carvalho de Melo 141535b9d88eSArnaldo Carvalho de Melo out_close_pipes: 141635b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 141735b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 141835b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 141935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 142035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 142135b9d88eSArnaldo Carvalho de Melo return -1; 142235b9d88eSArnaldo Carvalho de Melo } 142335b9d88eSArnaldo Carvalho de Melo 142463503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist) 142535b9d88eSArnaldo Carvalho de Melo { 142635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1427b3824404SDavid Ahern char bf = 0; 1428bcf3145fSNamhyung Kim int ret; 142935b9d88eSArnaldo Carvalho de Melo /* 143035b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 143135b9d88eSArnaldo Carvalho de Melo */ 1432bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1433bcf3145fSNamhyung Kim if (ret < 0) 1434e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1435bcf3145fSNamhyung Kim 1436bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1437bcf3145fSNamhyung Kim return ret; 143835b9d88eSArnaldo Carvalho de Melo } 143935b9d88eSArnaldo Carvalho de Melo 144035b9d88eSArnaldo Carvalho de Melo return 0; 144135b9d88eSArnaldo Carvalho de Melo } 1442cb0b29e0SArnaldo Carvalho de Melo 144363503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 14440807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1445cb0b29e0SArnaldo Carvalho de Melo { 144632dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 144775562573SAdrian Hunter 144875562573SAdrian Hunter if (!evsel) 144975562573SAdrian Hunter return -EFAULT; 14506b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample(evsel, event, sample); 1451cb0b29e0SArnaldo Carvalho de Melo } 145278f067b3SArnaldo Carvalho de Melo 145363503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 145401468120SJiri Olsa union perf_event *event, 145501468120SJiri Olsa u64 *timestamp) 145601468120SJiri Olsa { 145732dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 145801468120SJiri Olsa 145901468120SJiri Olsa if (!evsel) 146001468120SJiri Olsa return -EFAULT; 14616b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample_timestamp(evsel, event, timestamp); 146201468120SJiri Olsa } 146301468120SJiri Olsa 1464d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size) 1465a8f23d8fSArnaldo Carvalho de Melo { 1466a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1467c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1468a8f23d8fSArnaldo Carvalho de Melo 1469a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1470a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1471a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1472a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1473a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1474a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1475a8f23d8fSArnaldo Carvalho de Melo 14761a47245dSAdrian Hunter value = perf_event_paranoid(); 1477a8f23d8fSArnaldo Carvalho de Melo 1478a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1479a8f23d8fSArnaldo Carvalho de Melo 1480a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1481a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1482a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1483a8f23d8fSArnaldo Carvalho de Melo } 1484a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14855229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1486a8f23d8fSArnaldo Carvalho de Melo 1487a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14885229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 14895229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1490a8f23d8fSArnaldo Carvalho de Melo break; 1491d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1492515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1493d9aade7fSArnaldo Carvalho de Melo int max_freq; 1494d9aade7fSArnaldo Carvalho de Melo 1495d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1496d9aade7fSArnaldo Carvalho de Melo goto out_default; 1497d9aade7fSArnaldo Carvalho de Melo 14981fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1499d9aade7fSArnaldo Carvalho de Melo goto out_default; 1500d9aade7fSArnaldo Carvalho de Melo 1501d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1502d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1503d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1504d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 15051fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1506d9aade7fSArnaldo Carvalho de Melo break; 1507d9aade7fSArnaldo Carvalho de Melo } 1508a8f23d8fSArnaldo Carvalho de Melo default: 1509d9aade7fSArnaldo Carvalho de Melo out_default: 1510a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1511a8f23d8fSArnaldo Carvalho de Melo break; 1512a8f23d8fSArnaldo Carvalho de Melo } 1513a8f23d8fSArnaldo Carvalho de Melo 1514a8f23d8fSArnaldo Carvalho de Melo return 0; 1515a8f23d8fSArnaldo Carvalho de Melo } 1516a025e4f0SAdrian Hunter 1517d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1518956fa571SArnaldo Carvalho de Melo { 1519c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1520f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1521956fa571SArnaldo Carvalho de Melo 1522956fa571SArnaldo Carvalho de Melo switch (err) { 1523956fa571SArnaldo Carvalho de Melo case EPERM: 1524e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1525e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1526e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1527956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1528e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1529e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1530e965bea1SArnaldo Carvalho de Melo 1531e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1532e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1533e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1534e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1535e965bea1SArnaldo Carvalho de Melo } 1536e965bea1SArnaldo Carvalho de Melo 1537e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1538e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1539956fa571SArnaldo Carvalho de Melo break; 1540956fa571SArnaldo Carvalho de Melo default: 1541956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1542956fa571SArnaldo Carvalho de Melo break; 1543956fa571SArnaldo Carvalho de Melo } 1544956fa571SArnaldo Carvalho de Melo 1545956fa571SArnaldo Carvalho de Melo return 0; 1546956fa571SArnaldo Carvalho de Melo } 1547956fa571SArnaldo Carvalho de Melo 154863503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 154932dcd021SJiri Olsa struct evsel *move_evsel) 1550a025e4f0SAdrian Hunter { 155132dcd021SJiri Olsa struct evsel *evsel, *n; 1552a025e4f0SAdrian Hunter LIST_HEAD(move); 1553a025e4f0SAdrian Hunter 1554515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1555a025e4f0SAdrian Hunter return; 1556a025e4f0SAdrian Hunter 1557e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1558a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1559b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1560a025e4f0SAdrian Hunter } 1561a025e4f0SAdrian Hunter 1562ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1563a025e4f0SAdrian Hunter } 156460b0896cSAdrian Hunter 1565442ad225SAdrian Hunter struct evsel *perf_evlist__get_tracking_event(struct evlist *evlist) 1566442ad225SAdrian Hunter { 1567442ad225SAdrian Hunter struct evsel *evsel; 1568442ad225SAdrian Hunter 1569442ad225SAdrian Hunter evlist__for_each_entry(evlist, evsel) { 1570442ad225SAdrian Hunter if (evsel->tracking) 1571442ad225SAdrian Hunter return evsel; 1572442ad225SAdrian Hunter } 1573442ad225SAdrian Hunter 1574442ad225SAdrian Hunter return evlist__first(evlist); 1575442ad225SAdrian Hunter } 1576442ad225SAdrian Hunter 157763503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist, 157832dcd021SJiri Olsa struct evsel *tracking_evsel) 157960b0896cSAdrian Hunter { 158032dcd021SJiri Olsa struct evsel *evsel; 158160b0896cSAdrian Hunter 158260b0896cSAdrian Hunter if (tracking_evsel->tracking) 158360b0896cSAdrian Hunter return; 158460b0896cSAdrian Hunter 1585e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 158660b0896cSAdrian Hunter if (evsel != tracking_evsel) 158760b0896cSAdrian Hunter evsel->tracking = false; 158860b0896cSAdrian Hunter } 158960b0896cSAdrian Hunter 159060b0896cSAdrian Hunter tracking_evsel->tracking = true; 159160b0896cSAdrian Hunter } 15927630b3e2SWang Nan 159332dcd021SJiri Olsa struct evsel * 159463503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist, 15957630b3e2SWang Nan const char *str) 15967630b3e2SWang Nan { 159732dcd021SJiri Olsa struct evsel *evsel; 15987630b3e2SWang Nan 1599e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16007630b3e2SWang Nan if (!evsel->name) 16017630b3e2SWang Nan continue; 16027630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 16037630b3e2SWang Nan return evsel; 16047630b3e2SWang Nan } 16057630b3e2SWang Nan 16067630b3e2SWang Nan return NULL; 16077630b3e2SWang Nan } 160854cc54deSWang Nan 160963503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 161054cc54deSWang Nan enum bkw_mmap_state state) 161154cc54deSWang Nan { 161254cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 161354cc54deSWang Nan enum action { 161454cc54deSWang Nan NONE, 161554cc54deSWang Nan PAUSE, 161654cc54deSWang Nan RESUME, 161754cc54deSWang Nan } action = NONE; 161854cc54deSWang Nan 16190b72d69aSWang Nan if (!evlist->overwrite_mmap) 162054cc54deSWang Nan return; 162154cc54deSWang Nan 162254cc54deSWang Nan switch (old_state) { 162354cc54deSWang Nan case BKW_MMAP_NOTREADY: { 162454cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1625dd8bd53aSLuis de Bethencourt goto state_err; 162654cc54deSWang Nan break; 162754cc54deSWang Nan } 162854cc54deSWang Nan case BKW_MMAP_RUNNING: { 162954cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 163054cc54deSWang Nan goto state_err; 163154cc54deSWang Nan action = PAUSE; 163254cc54deSWang Nan break; 163354cc54deSWang Nan } 163454cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 163554cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 163654cc54deSWang Nan goto state_err; 163754cc54deSWang Nan break; 163854cc54deSWang Nan } 163954cc54deSWang Nan case BKW_MMAP_EMPTY: { 164054cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 164154cc54deSWang Nan goto state_err; 164254cc54deSWang Nan action = RESUME; 164354cc54deSWang Nan break; 164454cc54deSWang Nan } 164554cc54deSWang Nan default: 164654cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 164754cc54deSWang Nan } 164854cc54deSWang Nan 164954cc54deSWang Nan evlist->bkw_mmap_state = state; 165054cc54deSWang Nan 165154cc54deSWang Nan switch (action) { 165254cc54deSWang Nan case PAUSE: 165354cc54deSWang Nan perf_evlist__pause(evlist); 165454cc54deSWang Nan break; 165554cc54deSWang Nan case RESUME: 165654cc54deSWang Nan perf_evlist__resume(evlist); 165754cc54deSWang Nan break; 165854cc54deSWang Nan case NONE: 165954cc54deSWang Nan default: 166054cc54deSWang Nan break; 166154cc54deSWang Nan } 166254cc54deSWang Nan 166354cc54deSWang Nan state_err: 166454cc54deSWang Nan return; 166554cc54deSWang Nan } 166607d6f446SArnaldo Carvalho de Melo 166763503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 166807d6f446SArnaldo Carvalho de Melo { 166932dcd021SJiri Olsa struct evsel *evsel; 167007d6f446SArnaldo Carvalho de Melo 167107d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16721fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 167307d6f446SArnaldo Carvalho de Melo return false; 167407d6f446SArnaldo Carvalho de Melo } 167507d6f446SArnaldo Carvalho de Melo 167607d6f446SArnaldo Carvalho de Melo return true; 167707d6f446SArnaldo Carvalho de Melo } 1678e2bdbe80SJin Yao 1679e2bdbe80SJin Yao /* 1680e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1681e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1682e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1683e2bdbe80SJin Yao */ 168463503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1685e2bdbe80SJin Yao { 1686e2bdbe80SJin Yao if (!evlist->nr_groups) { 1687515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1688e2bdbe80SJin Yao 1689e2bdbe80SJin Yao perf_evlist__set_leader(evlist); 1690e2bdbe80SJin Yao leader->forced_leader = true; 1691e2bdbe80SJin Yao } 1692e2bdbe80SJin Yao } 1693c3537fc2SAndi Kleen 169463503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 16954804e011SAndi Kleen struct evsel *evsel, 16964804e011SAndi Kleen bool close) 1697c3537fc2SAndi Kleen { 169832dcd021SJiri Olsa struct evsel *c2, *leader; 1699c3537fc2SAndi Kleen bool is_open = true; 1700c3537fc2SAndi Kleen 1701c3537fc2SAndi Kleen leader = evsel->leader; 1702c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 17035643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1704c3537fc2SAndi Kleen 1705c3537fc2SAndi Kleen /* 1706c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1707c3537fc2SAndi Kleen * include the first entry. 1708c3537fc2SAndi Kleen */ 1709c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1710c3537fc2SAndi Kleen if (c2 == evsel) 1711c3537fc2SAndi Kleen is_open = false; 1712c3537fc2SAndi Kleen if (c2->leader == leader) { 17134804e011SAndi Kleen if (is_open && close) 17145a40e199SAndi Kleen perf_evsel__close(&c2->core); 1715c3537fc2SAndi Kleen c2->leader = c2; 17165643b1a5SJiri Olsa c2->core.nr_members = 0; 17174804e011SAndi Kleen /* 17184804e011SAndi Kleen * Set this for all former members of the group 17194804e011SAndi Kleen * to indicate they get reopened. 17204804e011SAndi Kleen */ 17214804e011SAndi Kleen c2->reset_group = true; 1722c3537fc2SAndi Kleen } 1723c3537fc2SAndi Kleen } 1724c3537fc2SAndi Kleen return leader; 1725c3537fc2SAndi Kleen } 1726