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 21c1a604dfSArnaldo Carvalho de Melo #include "../perf.h" 2254cc54deSWang Nan #include "asm/bug.h" 23657ee553SSong Liu #include "bpf-event.h" 24da949f50SArnaldo Carvalho de Melo #include "util/string2.h" 259607ad3aSArnaldo Carvalho de Melo #include <signal.h> 2635b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 27b397f846SArnaldo Carvalho de Melo #include <sched.h> 28f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h> 29361c99a6SArnaldo Carvalho de Melo 3050d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 314b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 3250d08e47SArnaldo Carvalho de Melo 33bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 3486a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 35f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 36f8a95309SArnaldo Carvalho de Melo 3770db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 3870db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 390389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 408dd2a131SJiri Olsa #include <linux/err.h> 418520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 427f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 434562a739SJiri Olsa #include <perf/evlist.h> 4488761fa1SJiri Olsa #include <perf/evsel.h> 459c3516d1SJiri Olsa #include <perf/cpumap.h> 467728fa0cSJiri Olsa #include <perf/mmap.h> 4770db7533SArnaldo Carvalho de Melo 48e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h> 49e14e5497SArnaldo Carvalho de Melo 50748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 51748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 52748fe088SArnaldo Carvalho de Melo #endif 53748fe088SArnaldo Carvalho de Melo 549dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 558cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 56f8a95309SArnaldo Carvalho de Melo 5752c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 589749b90eSJiri Olsa struct perf_thread_map *threads) 59361c99a6SArnaldo Carvalho de Melo { 604562a739SJiri Olsa perf_evlist__init(&evlist->core); 61453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 6235b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 6354cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 64361c99a6SArnaldo Carvalho de Melo } 65361c99a6SArnaldo Carvalho de Melo 660f98b11cSJiri Olsa struct evlist *evlist__new(void) 67ef1d1af2SArnaldo Carvalho de Melo { 6863503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist)); 69ef1d1af2SArnaldo Carvalho de Melo 70ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 7152c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL); 72ef1d1af2SArnaldo Carvalho de Melo 73361c99a6SArnaldo Carvalho de Melo return evlist; 74361c99a6SArnaldo Carvalho de Melo } 75361c99a6SArnaldo Carvalho de Melo 7663503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void) 77b22d54b0SJiri Olsa { 780f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 79b22d54b0SJiri Olsa 80b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 81c12995a5SJiri Olsa evlist__delete(evlist); 82b22d54b0SJiri Olsa evlist = NULL; 83b22d54b0SJiri Olsa } 84b22d54b0SJiri Olsa 85b22d54b0SJiri Olsa return evlist; 86b22d54b0SJiri Olsa } 87b22d54b0SJiri Olsa 8863503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void) 895bae0250SArnaldo Carvalho de Melo { 900f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 915bae0250SArnaldo Carvalho de Melo 925bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 93c12995a5SJiri Olsa evlist__delete(evlist); 945bae0250SArnaldo Carvalho de Melo evlist = NULL; 955bae0250SArnaldo Carvalho de Melo } 965bae0250SArnaldo Carvalho de Melo 975bae0250SArnaldo Carvalho de Melo return evlist; 985bae0250SArnaldo Carvalho de Melo } 995bae0250SArnaldo Carvalho de Melo 10075562573SAdrian Hunter /** 10175562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 10275562573SAdrian Hunter * @evlist: selected event list 10375562573SAdrian Hunter * 10475562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 10575562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 10675562573SAdrian Hunter */ 10763503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist) 10875562573SAdrian Hunter { 109515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11075562573SAdrian Hunter 11175562573SAdrian Hunter evlist->id_pos = first->id_pos; 11275562573SAdrian Hunter evlist->is_pos = first->is_pos; 11375562573SAdrian Hunter } 11475562573SAdrian Hunter 11563503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist) 116733cd2feSAdrian Hunter { 11732dcd021SJiri Olsa struct evsel *evsel; 118733cd2feSAdrian Hunter 119e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 120733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 121733cd2feSAdrian Hunter 122733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 123733cd2feSAdrian Hunter } 124733cd2feSAdrian Hunter 125e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist) 126361c99a6SArnaldo Carvalho de Melo { 12732dcd021SJiri Olsa struct evsel *pos, *n; 128361c99a6SArnaldo Carvalho de Melo 129e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 130b27c4eceSJiri Olsa list_del_init(&pos->core.node); 131d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 1325eb2dd2aSJiri Olsa evsel__delete(pos); 133361c99a6SArnaldo Carvalho de Melo } 134361c99a6SArnaldo Carvalho de Melo 1356484d2f9SJiri Olsa evlist->core.nr_entries = 0; 136361c99a6SArnaldo Carvalho de Melo } 137361c99a6SArnaldo Carvalho de Melo 138470579b0SJiri Olsa void evlist__exit(struct evlist *evlist) 139ef1d1af2SArnaldo Carvalho de Melo { 14004662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1410b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 14293dd6e28SJiri Olsa perf_evlist__exit(&evlist->core); 143ef1d1af2SArnaldo Carvalho de Melo } 144ef1d1af2SArnaldo Carvalho de Melo 145c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist) 146361c99a6SArnaldo Carvalho de Melo { 1470b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1480b04b3dcSArnaldo Carvalho de Melo return; 1490b04b3dcSArnaldo Carvalho de Melo 150db6b7b13SJiri Olsa evlist__munmap(evlist); 151750b4edeSJiri Olsa evlist__close(evlist); 152e6b1878dSJiri Olsa evlist__purge(evlist); 153470579b0SJiri Olsa evlist__exit(evlist); 154361c99a6SArnaldo Carvalho de Melo free(evlist); 155361c99a6SArnaldo Carvalho de Melo } 156361c99a6SArnaldo Carvalho de Melo 157a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 158361c99a6SArnaldo Carvalho de Melo { 159d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1606484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 16160b0896cSAdrian Hunter entry->tracking = !entry->idx; 162ef503831SArnaldo Carvalho de Melo 1636484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1646484d2f9SJiri Olsa 1656484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 16675562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 167361c99a6SArnaldo Carvalho de Melo } 168361c99a6SArnaldo Carvalho de Melo 16916251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1704768230aSAdrian Hunter { 1714768230aSAdrian Hunter evsel->evlist = NULL; 17252e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1734768230aSAdrian Hunter } 1744768230aSAdrian Hunter 17563503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist, 176f114d6efSAdrian Hunter struct list_head *list) 17750d08e47SArnaldo Carvalho de Melo { 17832dcd021SJiri Olsa struct evsel *evsel, *temp; 17975562573SAdrian Hunter 180e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 181b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 182a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 183f114d6efSAdrian Hunter } 18450d08e47SArnaldo Carvalho de Melo } 18550d08e47SArnaldo Carvalho de Melo 186c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist, 187c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs) 188c0e53476SArnaldo Carvalho de Melo { 189c0e53476SArnaldo Carvalho de Melo struct evsel *evsel; 190c0e53476SArnaldo Carvalho de Melo size_t i; 191c0e53476SArnaldo Carvalho de Melo int err; 192c0e53476SArnaldo Carvalho de Melo 193c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 194c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it. 195c0e53476SArnaldo Carvalho de Melo evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name); 196c0e53476SArnaldo Carvalho de Melo if (evsel == NULL) 197c0e53476SArnaldo Carvalho de Melo continue; 198c0e53476SArnaldo Carvalho de Melo 199c0e53476SArnaldo Carvalho de Melo err = -EEXIST; 200c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL) 201c0e53476SArnaldo Carvalho de Melo goto out; 202c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler; 203c0e53476SArnaldo Carvalho de Melo } 204c0e53476SArnaldo Carvalho de Melo 205c0e53476SArnaldo Carvalho de Melo err = 0; 206c0e53476SArnaldo Carvalho de Melo out: 207c0e53476SArnaldo Carvalho de Melo return err; 208c0e53476SArnaldo Carvalho de Melo } 209c0e53476SArnaldo Carvalho de Melo 21063dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 21163dab225SArnaldo Carvalho de Melo { 21232dcd021SJiri Olsa struct evsel *evsel, *leader; 21363dab225SArnaldo Carvalho de Melo 214b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 215b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 21697f63e4aSNamhyung Kim 2175643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 21863dab225SArnaldo Carvalho de Melo 219e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22063dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22163dab225SArnaldo Carvalho de Melo } 22263dab225SArnaldo Carvalho de Melo } 22363dab225SArnaldo Carvalho de Melo 22463503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist) 2256a4bb04cSJiri Olsa { 2266484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2276484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 228ce9036a6SJiri Olsa __perf_evlist__set_leader(&evlist->core.entries); 2296a4bb04cSJiri Olsa } 23097f63e4aSNamhyung Kim } 2316a4bb04cSJiri Olsa 23263503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise) 233361c99a6SArnaldo Carvalho de Melo { 23432dcd021SJiri Olsa struct evsel *evsel = perf_evsel__new_cycles(precise); 235361c99a6SArnaldo Carvalho de Melo 236361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2377c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 238361c99a6SArnaldo Carvalho de Melo 239a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 240361c99a6SArnaldo Carvalho de Melo return 0; 241361c99a6SArnaldo Carvalho de Melo } 2425c581041SArnaldo Carvalho de Melo 24363503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist) 2445bae0250SArnaldo Carvalho de Melo { 2455bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2465bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2475bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2485bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2495bae0250SArnaldo Carvalho de Melo }; 2506484d2f9SJiri Olsa struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries); 2515bae0250SArnaldo Carvalho de Melo 2525bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2535bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2545bae0250SArnaldo Carvalho de Melo 255a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2565bae0250SArnaldo Carvalho de Melo return 0; 2575bae0250SArnaldo Carvalho de Melo } 2585bae0250SArnaldo Carvalho de Melo 259a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist, 26050d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 26150d08e47SArnaldo Carvalho de Melo { 26232dcd021SJiri Olsa struct evsel *evsel, *n; 26350d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 26450d08e47SArnaldo Carvalho de Melo size_t i; 26550d08e47SArnaldo Carvalho de Melo 26650d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2676484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 26850d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 26950d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 270b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 27150d08e47SArnaldo Carvalho de Melo } 27250d08e47SArnaldo Carvalho de Melo 273f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 27450d08e47SArnaldo Carvalho de Melo 27550d08e47SArnaldo Carvalho de Melo return 0; 27650d08e47SArnaldo Carvalho de Melo 27750d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 278e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2795eb2dd2aSJiri Olsa evsel__delete(evsel); 28050d08e47SArnaldo Carvalho de Melo return -1; 28150d08e47SArnaldo Carvalho de Melo } 28250d08e47SArnaldo Carvalho de Melo 28363503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist, 28479695e1bSArnaldo Carvalho de Melo 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 32363503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist, 32439876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 32539876e7dSArnaldo Carvalho de Melo { 32632dcd021SJiri Olsa struct evsel *evsel = perf_evsel__newtp(sys, name); 32739876e7dSArnaldo Carvalho de Melo 3288dd2a131SJiri Olsa if (IS_ERR(evsel)) 32939876e7dSArnaldo Carvalho de Melo return -1; 33039876e7dSArnaldo Carvalho de Melo 331744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 332a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 33339876e7dSArnaldo Carvalho de Melo return 0; 33439876e7dSArnaldo Carvalho de Melo } 33539876e7dSArnaldo Carvalho de Melo 33663503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist, 33732dcd021SJiri Olsa struct evsel *evsel) 338bf8e8f4bSAdrian Hunter { 339648b5af3SJiri Olsa if (evsel->core.system_wide) 340bf8e8f4bSAdrian Hunter return 1; 341bf8e8f4bSAdrian Hunter else 342a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 343bf8e8f4bSAdrian Hunter } 344bf8e8f4bSAdrian Hunter 345a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist) 346a8cbe40fSAndi Kleen { 347a8cbe40fSAndi Kleen struct evsel *pos; 348a8cbe40fSAndi Kleen 349a8cbe40fSAndi Kleen /* 350a8cbe40fSAndi Kleen * Reset the per evsel cpu_iter. This is needed because 351a8cbe40fSAndi Kleen * each evsel's cpumap may have a different index space, 352a8cbe40fSAndi Kleen * and some operations need the index to modify 353a8cbe40fSAndi Kleen * the FD xyarray (e.g. open, close) 354a8cbe40fSAndi Kleen */ 355a8cbe40fSAndi Kleen evlist__for_each_entry(evlist, pos) 356a8cbe40fSAndi Kleen pos->cpu_iter = 0; 357a8cbe40fSAndi Kleen } 358a8cbe40fSAndi Kleen 359a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu) 360a8cbe40fSAndi Kleen { 361a8cbe40fSAndi Kleen if (ev->cpu_iter >= ev->core.cpus->nr) 362a8cbe40fSAndi Kleen return true; 363a8cbe40fSAndi Kleen if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu) 364a8cbe40fSAndi Kleen return true; 365a8cbe40fSAndi Kleen return false; 366a8cbe40fSAndi Kleen } 367a8cbe40fSAndi Kleen 368a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu) 369a8cbe40fSAndi Kleen { 370a8cbe40fSAndi Kleen if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) { 371a8cbe40fSAndi Kleen ev->cpu_iter++; 372a8cbe40fSAndi Kleen return false; 373a8cbe40fSAndi Kleen } 374a8cbe40fSAndi Kleen return true; 375a8cbe40fSAndi Kleen } 376a8cbe40fSAndi Kleen 377e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3784152ab37SArnaldo Carvalho de Melo { 37932dcd021SJiri Olsa struct evsel *pos; 3804152ab37SArnaldo Carvalho de Melo 381e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3829dfcb759SJiri Olsa if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd) 3833fe4430dSJiri Olsa continue; 3849a10bb22SJiri Olsa evsel__disable(pos); 3854152ab37SArnaldo Carvalho de Melo } 3862b56bcfbSArnaldo Carvalho de Melo 3872b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3884152ab37SArnaldo Carvalho de Melo } 3894152ab37SArnaldo Carvalho de Melo 3901c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 391764e16a3SDavid Ahern { 39232dcd021SJiri Olsa struct evsel *pos; 393764e16a3SDavid Ahern 394e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3959dfcb759SJiri Olsa if (!perf_evsel__is_group_leader(pos) || !pos->core.fd) 3963fe4430dSJiri Olsa continue; 397ec7f24efSJiri Olsa evsel__enable(pos); 398764e16a3SDavid Ahern } 3992b56bcfbSArnaldo Carvalho de Melo 4002b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 4012b56bcfbSArnaldo Carvalho de Melo } 4022b56bcfbSArnaldo Carvalho de Melo 40363503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist) 4042b56bcfbSArnaldo Carvalho de Melo { 405e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 406764e16a3SDavid Ahern } 407764e16a3SDavid Ahern 40863503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist, 40932dcd021SJiri Olsa struct evsel *evsel, int cpu) 4101c65056cSAdrian Hunter { 41118ef15c6SArnaldo Carvalho de Melo int thread; 4121c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 4131c65056cSAdrian Hunter 4149dfcb759SJiri Olsa if (!evsel->core.fd) 4151c65056cSAdrian Hunter return -EINVAL; 4161c65056cSAdrian Hunter 4171c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 41818ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4191c65056cSAdrian Hunter if (err) 4201c65056cSAdrian Hunter return err; 4211c65056cSAdrian Hunter } 4221c65056cSAdrian Hunter return 0; 4231c65056cSAdrian Hunter } 4241c65056cSAdrian Hunter 42563503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist, 42632dcd021SJiri Olsa struct evsel *evsel, 4271c65056cSAdrian Hunter int thread) 4281c65056cSAdrian Hunter { 42918ef15c6SArnaldo Carvalho de Melo int cpu; 4306549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 4311c65056cSAdrian Hunter 4329dfcb759SJiri Olsa if (!evsel->core.fd) 4331c65056cSAdrian Hunter return -EINVAL; 4341c65056cSAdrian Hunter 4351c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 43618ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4371c65056cSAdrian Hunter if (err) 4381c65056cSAdrian Hunter return err; 4391c65056cSAdrian Hunter } 4401c65056cSAdrian Hunter return 0; 4411c65056cSAdrian Hunter } 4421c65056cSAdrian Hunter 44363503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist, 44432dcd021SJiri Olsa struct evsel *evsel, int idx) 4451c65056cSAdrian Hunter { 446315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 4471c65056cSAdrian Hunter 4481c65056cSAdrian Hunter if (per_cpu_mmaps) 4491c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4501c65056cSAdrian Hunter else 4511c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4521c65056cSAdrian Hunter } 4531c65056cSAdrian Hunter 454f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 455e4b356b5SArnaldo Carvalho de Melo { 456f4009e7bSJiri Olsa return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN); 457e4b356b5SArnaldo Carvalho de Melo } 4581b85337dSArnaldo Carvalho de Melo 459f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 4601ddec7f0SArnaldo Carvalho de Melo { 46184227cb1SJiri Olsa return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 4621ddec7f0SArnaldo Carvalho de Melo } 4631ddec7f0SArnaldo Carvalho de Melo 46480ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 465f66a889dSArnaldo Carvalho de Melo { 46680ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 467f66a889dSArnaldo Carvalho de Melo } 468f66a889dSArnaldo Carvalho de Melo 46963503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 47070db7533SArnaldo Carvalho de Melo { 47170db7533SArnaldo Carvalho de Melo struct hlist_head *head; 47270db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 47370db7533SArnaldo Carvalho de Melo int hash; 47470db7533SArnaldo Carvalho de Melo 47570db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 4761d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 47770db7533SArnaldo Carvalho de Melo 478b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 47970db7533SArnaldo Carvalho de Melo if (sid->id == id) 480932a3594SJiri Olsa return sid; 481932a3594SJiri Olsa 482932a3594SJiri Olsa return NULL; 483932a3594SJiri Olsa } 484932a3594SJiri Olsa 48563503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 486932a3594SJiri Olsa { 487932a3594SJiri Olsa struct perf_sample_id *sid; 488932a3594SJiri Olsa 4896484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 490515dbe48SJiri Olsa return evlist__first(evlist); 491932a3594SJiri Olsa 492932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 493932a3594SJiri Olsa if (sid) 49470c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 49530e68bccSNamhyung Kim 49630e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 497515dbe48SJiri Olsa return evlist__first(evlist); 49830e68bccSNamhyung Kim 49970db7533SArnaldo Carvalho de Melo return NULL; 50070db7533SArnaldo Carvalho de Melo } 50104391debSArnaldo Carvalho de Melo 50263503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 503dddcf6abSAdrian Hunter u64 id) 504dddcf6abSAdrian Hunter { 505dddcf6abSAdrian Hunter struct perf_sample_id *sid; 506dddcf6abSAdrian Hunter 507dddcf6abSAdrian Hunter if (!id) 508dddcf6abSAdrian Hunter return NULL; 509dddcf6abSAdrian Hunter 510dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 511dddcf6abSAdrian Hunter if (sid) 51270c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 513dddcf6abSAdrian Hunter 514dddcf6abSAdrian Hunter return NULL; 515dddcf6abSAdrian Hunter } 516dddcf6abSAdrian Hunter 51763503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist, 51875562573SAdrian Hunter union perf_event *event, u64 *id) 51975562573SAdrian Hunter { 520b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 52175562573SAdrian Hunter ssize_t n; 52275562573SAdrian Hunter 52375562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 52475562573SAdrian Hunter 52575562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 52675562573SAdrian Hunter if (evlist->id_pos >= n) 52775562573SAdrian Hunter return -1; 52875562573SAdrian Hunter *id = array[evlist->id_pos]; 52975562573SAdrian Hunter } else { 53075562573SAdrian Hunter if (evlist->is_pos > n) 53175562573SAdrian Hunter return -1; 53275562573SAdrian Hunter n -= evlist->is_pos; 53375562573SAdrian Hunter *id = array[n]; 53475562573SAdrian Hunter } 53575562573SAdrian Hunter return 0; 53675562573SAdrian Hunter } 53775562573SAdrian Hunter 53863503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 53975562573SAdrian Hunter union perf_event *event) 54075562573SAdrian Hunter { 541515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 54275562573SAdrian Hunter struct hlist_head *head; 54375562573SAdrian Hunter struct perf_sample_id *sid; 54475562573SAdrian Hunter int hash; 54575562573SAdrian Hunter u64 id; 54675562573SAdrian Hunter 5476484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 54898be6966SAdrian Hunter return first; 54998be6966SAdrian Hunter 5501fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 55198be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 55298be6966SAdrian Hunter return first; 55375562573SAdrian Hunter 55475562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 55575562573SAdrian Hunter return NULL; 55675562573SAdrian Hunter 55775562573SAdrian Hunter /* Synthesized events have an id of zero */ 55875562573SAdrian Hunter if (!id) 55998be6966SAdrian Hunter return first; 56075562573SAdrian Hunter 56175562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5621d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 56375562573SAdrian Hunter 56475562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 56575562573SAdrian Hunter if (sid->id == id) 56670c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 56775562573SAdrian Hunter } 56875562573SAdrian Hunter return NULL; 56975562573SAdrian Hunter } 57075562573SAdrian Hunter 57163503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value) 57265aea233SWang Nan { 57365aea233SWang Nan int i; 57465aea233SWang Nan 5750b72d69aSWang Nan if (!evlist->overwrite_mmap) 576078c3386SWang Nan return 0; 577078c3386SWang Nan 578c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 5792cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 58065aea233SWang Nan int err; 58165aea233SWang Nan 58265aea233SWang Nan if (fd < 0) 58365aea233SWang Nan continue; 58465aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 58565aea233SWang Nan if (err) 58665aea233SWang Nan return err; 58765aea233SWang Nan } 58865aea233SWang Nan return 0; 58965aea233SWang Nan } 59065aea233SWang Nan 59163503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist) 59265aea233SWang Nan { 59365aea233SWang Nan return perf_evlist__set_paused(evlist, true); 59465aea233SWang Nan } 59565aea233SWang Nan 59663503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist) 59765aea233SWang Nan { 59865aea233SWang Nan return perf_evlist__set_paused(evlist, false); 59965aea233SWang Nan } 60065aea233SWang Nan 601db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 602f8a95309SArnaldo Carvalho de Melo { 603aece948fSArnaldo Carvalho de Melo int i; 604f8a95309SArnaldo Carvalho de Melo 605b2cb615dSWang Nan if (evlist->mmap) 606c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 60780e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 608b2cb615dSWang Nan 6090b72d69aSWang Nan if (evlist->overwrite_mmap) 610c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 61180e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 612a1f72618SWang Nan } 613aece948fSArnaldo Carvalho de Melo 614db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 615a1f72618SWang Nan { 616db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 61704662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6180b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 619f8a95309SArnaldo Carvalho de Melo } 620f8a95309SArnaldo Carvalho de Melo 62180e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 62280e53d11SJiri Olsa { 62380e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 62480e53d11SJiri Olsa 62580e53d11SJiri Olsa mmap__munmap(m); 62680e53d11SJiri Olsa } 62780e53d11SJiri Olsa 628d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 6292c5f6d87SKan Liang bool overwrite) 630f8a95309SArnaldo Carvalho de Melo { 631d4c6fb36SWang Nan int i; 632a5830532SJiri Olsa struct mmap *map; 633d4c6fb36SWang Nan 634c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 6358db6d6b1SWang Nan if (!map) 6368db6d6b1SWang Nan return NULL; 637946ae1d4SWang Nan 638c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6396eb65f7aSJiri Olsa struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 6406eb65f7aSJiri Olsa 6414738ca30SArnaldo Carvalho de Melo /* 6424738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 6436afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 6444738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 6454738ca30SArnaldo Carvalho de Melo * dropped. 6464738ca30SArnaldo Carvalho de Melo * 6474738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 6484738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 6494738ca30SArnaldo Carvalho de Melo */ 6506eb65f7aSJiri Olsa perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 6514738ca30SArnaldo Carvalho de Melo } 652353120b4SJiri Olsa 6538db6d6b1SWang Nan return map; 654f8a95309SArnaldo Carvalho de Melo } 655f8a95309SArnaldo Carvalho de Melo 6569abd2ab2SJiri Olsa static void 6579abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 6589abd2ab2SJiri Olsa struct perf_mmap_param *_mp, 6599abd2ab2SJiri Olsa int idx, bool per_cpu) 6609abd2ab2SJiri Olsa { 6619abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 6629abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 6639abd2ab2SJiri Olsa 6649abd2ab2SJiri Olsa auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 6659abd2ab2SJiri Olsa } 6669abd2ab2SJiri Olsa 667bb1b1885SJiri Olsa static struct perf_mmap* 668bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 669bb1b1885SJiri Olsa { 670bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 6713805e4f3SJiri Olsa struct mmap *maps; 672bb1b1885SJiri Olsa 6733805e4f3SJiri Olsa maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 674bb1b1885SJiri Olsa 675bb1b1885SJiri Olsa if (!maps) { 6763805e4f3SJiri Olsa maps = evlist__alloc_mmap(evlist, overwrite); 677bb1b1885SJiri Olsa if (!maps) 678bb1b1885SJiri Olsa return NULL; 679bb1b1885SJiri Olsa 6803805e4f3SJiri Olsa if (overwrite) { 681bb1b1885SJiri Olsa evlist->overwrite_mmap = maps; 682bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 683bb1b1885SJiri Olsa perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 6843805e4f3SJiri Olsa } else { 6853805e4f3SJiri Olsa evlist->mmap = maps; 686bb1b1885SJiri Olsa } 687bb1b1885SJiri Olsa } 688bb1b1885SJiri Olsa 689bb1b1885SJiri Olsa return &maps[idx].core; 690bb1b1885SJiri Olsa } 691bb1b1885SJiri Olsa 692b80132b1SJiri Olsa static int 693b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 694b80132b1SJiri Olsa int output, int cpu) 695b80132b1SJiri Olsa { 696b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core); 697b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 698b80132b1SJiri Olsa 699b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu); 700b80132b1SJiri Olsa } 701b80132b1SJiri Olsa 702f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 703994a1f78SJiri Olsa { 704f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 7058185e881SArnaldo Carvalho de Melo int max; 7068185e881SArnaldo Carvalho de Melo 7078185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 7088185e881SArnaldo Carvalho de Melo /* 7098185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 7108185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 7118185e881SArnaldo Carvalho de Melo * die yet... 7128185e881SArnaldo Carvalho de Melo */ 7138185e881SArnaldo Carvalho de Melo max = 512; 7148185e881SArnaldo Carvalho de Melo } else { 7158185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 7168185e881SArnaldo Carvalho de Melo } 7178185e881SArnaldo Carvalho de Melo 7188185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 7191be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 7201be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 721f5e7150cSArnaldo Carvalho de Melo 722f5e7150cSArnaldo Carvalho de Melo return pages; 723f5e7150cSArnaldo Carvalho de Melo } 724f5e7150cSArnaldo Carvalho de Melo 7259521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 726f5e7150cSArnaldo Carvalho de Melo { 727f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 728f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 729f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 730994a1f78SJiri Olsa return 0; 731994a1f78SJiri Olsa 732994a1f78SJiri Olsa return (pages + 1) * page_size; 733994a1f78SJiri Olsa } 734994a1f78SJiri Olsa 73533c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 73633c2dcfdSDavid Ahern unsigned long max) 737994a1f78SJiri Olsa { 7382fbe4abeSAdrian Hunter unsigned long pages, val; 73927050f53SJiri Olsa static struct parse_tag tags[] = { 74027050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 74127050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 74227050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 74327050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 74427050f53SJiri Olsa { .tag = 0 }, 74527050f53SJiri Olsa }; 746994a1f78SJiri Olsa 7478973504bSDavid Ahern if (str == NULL) 74833c2dcfdSDavid Ahern return -EINVAL; 7498973504bSDavid Ahern 75027050f53SJiri Olsa val = parse_tag_value(str, tags); 7512fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 75227050f53SJiri Olsa /* we got file size value */ 75327050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 75427050f53SJiri Olsa } else { 75527050f53SJiri Olsa /* we got pages count value */ 75627050f53SJiri Olsa char *eptr; 757994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 75833c2dcfdSDavid Ahern if (*eptr != '\0') 75933c2dcfdSDavid Ahern return -EINVAL; 76027050f53SJiri Olsa } 761994a1f78SJiri Olsa 7622bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 76333c2dcfdSDavid Ahern /* leave number of pages at 0 */ 7641dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 7659808143bSJiri Olsa char buf[100]; 7669808143bSJiri Olsa 76733c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 76891529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 7691dbfa938SAdrian Hunter if (!pages) 7701dbfa938SAdrian Hunter return -EINVAL; 7719808143bSJiri Olsa 7729808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 7739808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 7749808143bSJiri Olsa buf, pages); 7752fbe4abeSAdrian Hunter } 7762fbe4abeSAdrian Hunter 77733c2dcfdSDavid Ahern if (pages > max) 77833c2dcfdSDavid Ahern return -EINVAL; 77933c2dcfdSDavid Ahern 78033c2dcfdSDavid Ahern return pages; 78133c2dcfdSDavid Ahern } 78233c2dcfdSDavid Ahern 783e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 78433c2dcfdSDavid Ahern { 78533c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 78633c2dcfdSDavid Ahern long pages; 78733c2dcfdSDavid Ahern 788f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 78933c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 79033c2dcfdSDavid Ahern 79133c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 79233c2dcfdSDavid Ahern if (pages < 0) { 79333c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 794994a1f78SJiri Olsa return -1; 795994a1f78SJiri Olsa } 796994a1f78SJiri Olsa 797994a1f78SJiri Olsa *mmap_pages = pages; 798994a1f78SJiri Olsa return 0; 799994a1f78SJiri Olsa } 800994a1f78SJiri Olsa 801e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 802e9db1310SAdrian Hunter int unset __maybe_unused) 803e9db1310SAdrian Hunter { 804e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 805e9db1310SAdrian Hunter } 806e9db1310SAdrian Hunter 807c83fa7f2SAdrian Hunter /** 8089521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 809c83fa7f2SAdrian Hunter * @evlist: list of events 810c83fa7f2SAdrian Hunter * @pages: map length in pages 811c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 812718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 813718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 814f8a95309SArnaldo Carvalho de Melo * 815c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 8169521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 817c83fa7f2SAdrian Hunter * automatically. 818f8a95309SArnaldo Carvalho de Melo * 819718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 820718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 821718c602dSAdrian Hunter * 822c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 823f8a95309SArnaldo Carvalho de Melo */ 8249521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 8257a276ff6SWang Nan unsigned int auxtrace_pages, 82651255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 82751255a8aSAlexey Budankov int comp_level) 828f8a95309SArnaldo Carvalho de Melo { 82971f566a3SWang Nan /* 83071f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 83171f566a3SWang Nan * Its value is decided by evsel's write_backward. 83271f566a3SWang Nan * So &mp should not be passed through const pointer. 83371f566a3SWang Nan */ 834e440979fSJiri Olsa struct mmap_params mp = { 835e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 836e440979fSJiri Olsa .affinity = affinity, 837e440979fSJiri Olsa .flush = flush, 838e440979fSJiri Olsa .comp_level = comp_level 839e440979fSJiri Olsa }; 840923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = { 8419abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx, 842bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get, 843b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap, 8449abd2ab2SJiri Olsa }; 84550a682ceSArnaldo Carvalho de Melo 846f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 847f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 848f8a95309SArnaldo Carvalho de Melo 849f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 850718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 851718c602dSAdrian Hunter 852923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 853f8a95309SArnaldo Carvalho de Melo } 8547e2ed097SArnaldo Carvalho de Melo 8559521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 856718c602dSAdrian Hunter { 8579521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 858718c602dSAdrian Hunter } 859718c602dSAdrian Hunter 86063503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 8617e2ed097SArnaldo Carvalho de Melo { 862147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 863f854839bSJiri Olsa struct perf_cpu_map *cpus; 8649749b90eSJiri Olsa struct perf_thread_map *threads; 8657e2ed097SArnaldo Carvalho de Melo 866147c508fSJin Yao /* 867147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 868147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 869147c508fSJin Yao * target->system_wide = true. 870147c508fSJin Yao * 871147c508fSJin Yao * If specify '--per-thread' only to perf record, 872147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 873147c508fSJin Yao * 874147c508fSJin Yao * So target->per_thread && target->system_wide is false. 875147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 876147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 877147c508fSJin Yao * current behavior. 878147c508fSJin Yao * 879147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 880147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 881147c508fSJin Yao * per-thread data. thread_map__new_str will call 882147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 883147c508fSJin Yao */ 88473c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 885147c508fSJin Yao all_threads); 88674bfd2b2SAdrian Hunter 88774bfd2b2SAdrian Hunter if (!threads) 8887e2ed097SArnaldo Carvalho de Melo return -1; 8897e2ed097SArnaldo Carvalho de Melo 8909c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 891397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 892879d77d0SNamhyung Kim else 8939c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 8947e2ed097SArnaldo Carvalho de Melo 89574bfd2b2SAdrian Hunter if (!cpus) 8967e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 8977e2ed097SArnaldo Carvalho de Melo 898ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 899ec9a77a7SAdrian Hunter 900453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 901d5bc056eSAdrian Hunter 902d5bc056eSAdrian Hunter return 0; 9037e2ed097SArnaldo Carvalho de Melo 9047e2ed097SArnaldo Carvalho de Melo out_delete_threads: 9057836e52eSJiri Olsa perf_thread_map__put(threads); 9067e2ed097SArnaldo Carvalho de Melo return -1; 9077e2ed097SArnaldo Carvalho de Melo } 9087e2ed097SArnaldo Carvalho de Melo 90963503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist, 91022c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 91122c8a376SArnaldo Carvalho de Melo { 91232dcd021SJiri Olsa struct evsel *evsel; 91322c8a376SArnaldo Carvalho de Melo 914e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 91522c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 91622c8a376SArnaldo Carvalho de Melo } 91722c8a376SArnaldo Carvalho de Melo 91863503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist, 91922c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 92022c8a376SArnaldo Carvalho de Melo { 92132dcd021SJiri Olsa struct evsel *evsel; 92222c8a376SArnaldo Carvalho de Melo 923e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 92422c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 92522c8a376SArnaldo Carvalho de Melo } 92622c8a376SArnaldo Carvalho de Melo 92763503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 9280a102479SFrederic Weisbecker { 92932dcd021SJiri Olsa struct evsel *evsel; 930745cefc5SArnaldo Carvalho de Melo int err = 0; 9310a102479SFrederic Weisbecker 932e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 933745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 9340a102479SFrederic Weisbecker continue; 935745cefc5SArnaldo Carvalho de Melo 936d988d5eeSKan Liang /* 937d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 938d988d5eeSKan Liang * So evlist and evsel should always be same. 939d988d5eeSKan Liang */ 940a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 94123d4aad4SArnaldo Carvalho de Melo if (err) { 94223d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 943745cefc5SArnaldo Carvalho de Melo break; 9440a102479SFrederic Weisbecker } 94523d4aad4SArnaldo Carvalho de Melo } 9460a102479SFrederic Weisbecker 947745cefc5SArnaldo Carvalho de Melo return err; 948745cefc5SArnaldo Carvalho de Melo } 949745cefc5SArnaldo Carvalho de Melo 95063503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 951745cefc5SArnaldo Carvalho de Melo { 95232dcd021SJiri Olsa struct evsel *evsel; 953745cefc5SArnaldo Carvalho de Melo int err = 0; 954745cefc5SArnaldo Carvalho de Melo 95505cea449SArnaldo Carvalho de Melo if (filter == NULL) 95605cea449SArnaldo Carvalho de Melo return -1; 95705cea449SArnaldo Carvalho de Melo 958e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 9591fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 960fdf14720SWang Nan continue; 961fdf14720SWang Nan 96294ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 963745cefc5SArnaldo Carvalho de Melo if (err) 964745cefc5SArnaldo Carvalho de Melo break; 965745cefc5SArnaldo Carvalho de Melo } 966745cefc5SArnaldo Carvalho de Melo 967745cefc5SArnaldo Carvalho de Melo return err; 9680a102479SFrederic Weisbecker } 96974429964SFrederic Weisbecker 97053c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter) 97153c92f73SArnaldo Carvalho de Melo { 97253c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 97353c92f73SArnaldo Carvalho de Melo int err = 0; 97453c92f73SArnaldo Carvalho de Melo 97553c92f73SArnaldo Carvalho de Melo if (filter == NULL) 97653c92f73SArnaldo Carvalho de Melo return -1; 97753c92f73SArnaldo Carvalho de Melo 97853c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 97953c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 98053c92f73SArnaldo Carvalho de Melo continue; 98153c92f73SArnaldo Carvalho de Melo 98253c92f73SArnaldo Carvalho de Melo err = perf_evsel__append_tp_filter(evsel, filter); 98353c92f73SArnaldo Carvalho de Melo if (err) 98453c92f73SArnaldo Carvalho de Melo break; 98553c92f73SArnaldo Carvalho de Melo } 98653c92f73SArnaldo Carvalho de Melo 98753c92f73SArnaldo Carvalho de Melo return err; 98853c92f73SArnaldo Carvalho de Melo } 98953c92f73SArnaldo Carvalho de Melo 990da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 991cfd70a26SArnaldo Carvalho de Melo { 992cfd70a26SArnaldo Carvalho de Melo char *filter; 993be199adaSArnaldo Carvalho de Melo size_t i; 994cfd70a26SArnaldo Carvalho de Melo 995be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 996be199adaSArnaldo Carvalho de Melo if (i == 0) { 997be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 99805cea449SArnaldo Carvalho de Melo return NULL; 999be199adaSArnaldo Carvalho de Melo } else { 1000be199adaSArnaldo Carvalho de Melo char *tmp; 1001be199adaSArnaldo Carvalho de Melo 1002be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1003be199adaSArnaldo Carvalho de Melo goto out_free; 1004be199adaSArnaldo Carvalho de Melo 1005be199adaSArnaldo Carvalho de Melo free(filter); 1006be199adaSArnaldo Carvalho de Melo filter = tmp; 1007be199adaSArnaldo Carvalho de Melo } 1008be199adaSArnaldo Carvalho de Melo } 1009cfd70a26SArnaldo Carvalho de Melo 101005cea449SArnaldo Carvalho de Melo return filter; 1011be199adaSArnaldo Carvalho de Melo out_free: 1012cfd70a26SArnaldo Carvalho de Melo free(filter); 101305cea449SArnaldo Carvalho de Melo return NULL; 101405cea449SArnaldo Carvalho de Melo } 101505cea449SArnaldo Carvalho de Melo 101605cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 101705cea449SArnaldo Carvalho de Melo { 101805cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 101905cea449SArnaldo Carvalho de Melo int ret = perf_evlist__set_tp_filter(evlist, filter); 102005cea449SArnaldo Carvalho de Melo 102105cea449SArnaldo Carvalho de Melo free(filter); 1022cfd70a26SArnaldo Carvalho de Melo return ret; 1023cfd70a26SArnaldo Carvalho de Melo } 1024cfd70a26SArnaldo Carvalho de Melo 102563503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1026be199adaSArnaldo Carvalho de Melo { 10277ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1028be199adaSArnaldo Carvalho de Melo } 1029be199adaSArnaldo Carvalho de Melo 10301827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 10311827ab5bSArnaldo Carvalho de Melo { 10321827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 10331827ab5bSArnaldo Carvalho de Melo int ret = perf_evlist__append_tp_filter(evlist, filter); 10341827ab5bSArnaldo Carvalho de Melo 10351827ab5bSArnaldo Carvalho de Melo free(filter); 10361827ab5bSArnaldo Carvalho de Melo return ret; 10371827ab5bSArnaldo Carvalho de Melo } 10381827ab5bSArnaldo Carvalho de Melo 10391827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 10401827ab5bSArnaldo Carvalho de Melo { 10411827ab5bSArnaldo Carvalho de Melo return perf_evlist__append_tp_filter_pids(evlist, 1, &pid); 10421827ab5bSArnaldo Carvalho de Melo } 10431827ab5bSArnaldo Carvalho de Melo 104463503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist) 104574429964SFrederic Weisbecker { 104632dcd021SJiri Olsa struct evsel *pos; 1047c2a70653SArnaldo Carvalho de Melo 10486484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 104975562573SAdrian Hunter return true; 105075562573SAdrian Hunter 105175562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 105275562573SAdrian Hunter return false; 105375562573SAdrian Hunter 1054e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 105575562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 105675562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1057c2a70653SArnaldo Carvalho de Melo return false; 105874429964SFrederic Weisbecker } 105974429964SFrederic Weisbecker 1060c2a70653SArnaldo Carvalho de Melo return true; 1061c2a70653SArnaldo Carvalho de Melo } 1062c2a70653SArnaldo Carvalho de Melo 106363503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist) 1064c2a70653SArnaldo Carvalho de Melo { 106532dcd021SJiri Olsa struct evsel *evsel; 106675562573SAdrian Hunter 106775562573SAdrian Hunter if (evlist->combined_sample_type) 106875562573SAdrian Hunter return evlist->combined_sample_type; 106975562573SAdrian Hunter 1070e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 10711fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 107275562573SAdrian Hunter 107375562573SAdrian Hunter return evlist->combined_sample_type; 107475562573SAdrian Hunter } 107575562573SAdrian Hunter 107663503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist) 107775562573SAdrian Hunter { 107875562573SAdrian Hunter evlist->combined_sample_type = 0; 107975562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1080c2a70653SArnaldo Carvalho de Melo } 1081c2a70653SArnaldo Carvalho de Melo 108263503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist) 108398df858eSAndi Kleen { 108432dcd021SJiri Olsa struct evsel *evsel; 108598df858eSAndi Kleen u64 branch_type = 0; 108698df858eSAndi Kleen 1087e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 10881fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 108998df858eSAndi Kleen return branch_type; 109098df858eSAndi Kleen } 109198df858eSAndi Kleen 109263503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 10939ede473cSJiri Olsa { 1094515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 10951fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 10961fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 10979ede473cSJiri Olsa 1098e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 10991fc632ceSJiri Olsa if (read_format != pos->core.attr.read_format) 11009ede473cSJiri Olsa return false; 11019ede473cSJiri Olsa } 11029ede473cSJiri Olsa 11039ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 11049ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 11059ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 11069ede473cSJiri Olsa return false; 11079ede473cSJiri Olsa } 11089ede473cSJiri Olsa 11099ede473cSJiri Olsa return true; 11109ede473cSJiri Olsa } 11119ede473cSJiri Olsa 111263503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 111381e36bffSArnaldo Carvalho de Melo { 1114515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 111581e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 111681e36bffSArnaldo Carvalho de Melo u64 sample_type; 111781e36bffSArnaldo Carvalho de Melo u16 size = 0; 111881e36bffSArnaldo Carvalho de Melo 11191fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 112081e36bffSArnaldo Carvalho de Melo goto out; 112181e36bffSArnaldo Carvalho de Melo 11221fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 112381e36bffSArnaldo Carvalho de Melo 112481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 112581e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 112681e36bffSArnaldo Carvalho de Melo 112781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 112881e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 112981e36bffSArnaldo Carvalho de Melo 113081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 113181e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 113281e36bffSArnaldo Carvalho de Melo 113381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 113481e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 113581e36bffSArnaldo Carvalho de Melo 113681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 113781e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 113875562573SAdrian Hunter 113975562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 114075562573SAdrian Hunter size += sizeof(data->id); 114181e36bffSArnaldo Carvalho de Melo out: 114281e36bffSArnaldo Carvalho de Melo return size; 114381e36bffSArnaldo Carvalho de Melo } 114481e36bffSArnaldo Carvalho de Melo 114563503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist) 1146c2a70653SArnaldo Carvalho de Melo { 1147515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1148c2a70653SArnaldo Carvalho de Melo 1149e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 11501fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1151c2a70653SArnaldo Carvalho de Melo return false; 1152c2a70653SArnaldo Carvalho de Melo } 1153c2a70653SArnaldo Carvalho de Melo 1154c2a70653SArnaldo Carvalho de Melo return true; 115574429964SFrederic Weisbecker } 115674429964SFrederic Weisbecker 115763503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist) 115874429964SFrederic Weisbecker { 1159515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11601fc632ceSJiri Olsa return first->core.attr.sample_id_all; 116174429964SFrederic Weisbecker } 116281cce8deSArnaldo Carvalho de Melo 116363503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 116432dcd021SJiri Olsa struct evsel *evsel) 116581cce8deSArnaldo Carvalho de Melo { 116681cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 116781cce8deSArnaldo Carvalho de Melo } 1168727ab04eSArnaldo Carvalho de Melo 1169750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1170a74b4b66SNamhyung Kim { 117132dcd021SJiri Olsa struct evsel *evsel; 1172a74b4b66SNamhyung Kim 1173475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 117488761fa1SJiri Olsa evsel__close(evsel); 1175a74b4b66SNamhyung Kim } 1176a74b4b66SNamhyung Kim 117763503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 11784112eb18SArnaldo Carvalho de Melo { 1179f854839bSJiri Olsa struct perf_cpu_map *cpus; 11809749b90eSJiri Olsa struct perf_thread_map *threads; 11814112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 11824112eb18SArnaldo Carvalho de Melo 11834112eb18SArnaldo Carvalho de Melo /* 11844112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 11854112eb18SArnaldo Carvalho de Melo * an all cpus map. 11864112eb18SArnaldo Carvalho de Melo * 11874112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 11884112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 11894112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 11904112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 11914112eb18SArnaldo Carvalho de Melo */ 11929c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 11938c0498b6SAdrian Hunter if (!cpus) 11944112eb18SArnaldo Carvalho de Melo goto out; 11954112eb18SArnaldo Carvalho de Melo 11964b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 11978c0498b6SAdrian Hunter if (!threads) 11988c0498b6SAdrian Hunter goto out_put; 11994112eb18SArnaldo Carvalho de Melo 1200453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 12014112eb18SArnaldo Carvalho de Melo out: 12024112eb18SArnaldo Carvalho de Melo return err; 12038c0498b6SAdrian Hunter out_put: 120438f01d8dSJiri Olsa perf_cpu_map__put(cpus); 12054112eb18SArnaldo Carvalho de Melo goto out; 12064112eb18SArnaldo Carvalho de Melo } 12074112eb18SArnaldo Carvalho de Melo 1208474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1209727ab04eSArnaldo Carvalho de Melo { 121032dcd021SJiri Olsa struct evsel *evsel; 1211a74b4b66SNamhyung Kim int err; 1212727ab04eSArnaldo Carvalho de Melo 12134112eb18SArnaldo Carvalho de Melo /* 12144112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 12154112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 12164112eb18SArnaldo Carvalho de Melo */ 121703617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 12184112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 12194112eb18SArnaldo Carvalho de Melo if (err < 0) 12204112eb18SArnaldo Carvalho de Melo goto out_err; 12214112eb18SArnaldo Carvalho de Melo } 12224112eb18SArnaldo Carvalho de Melo 1223733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1224733cd2feSAdrian Hunter 1225e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1226af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1227727ab04eSArnaldo Carvalho de Melo if (err < 0) 1228727ab04eSArnaldo Carvalho de Melo goto out_err; 1229727ab04eSArnaldo Carvalho de Melo } 1230727ab04eSArnaldo Carvalho de Melo 1231727ab04eSArnaldo Carvalho de Melo return 0; 1232727ab04eSArnaldo Carvalho de Melo out_err: 1233750b4edeSJiri Olsa evlist__close(evlist); 123441c21a68SNamhyung Kim errno = -err; 1235727ab04eSArnaldo Carvalho de Melo return err; 1236727ab04eSArnaldo Carvalho de Melo } 123735b9d88eSArnaldo Carvalho de Melo 123863503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 123955e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1240735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 124135b9d88eSArnaldo Carvalho de Melo { 124235b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 124335b9d88eSArnaldo Carvalho de Melo char bf; 124435b9d88eSArnaldo Carvalho de Melo 124535b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 124635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 124735b9d88eSArnaldo Carvalho de Melo return -1; 124835b9d88eSArnaldo Carvalho de Melo } 124935b9d88eSArnaldo Carvalho de Melo 125035b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 125135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 125235b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 125335b9d88eSArnaldo Carvalho de Melo } 125435b9d88eSArnaldo Carvalho de Melo 125535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 125635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 125735b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 125835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 125935b9d88eSArnaldo Carvalho de Melo } 126035b9d88eSArnaldo Carvalho de Melo 126135b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 12625f1c4225SArnaldo Carvalho de Melo int ret; 12635f1c4225SArnaldo Carvalho de Melo 1264119fa3c9SNamhyung Kim if (pipe_output) 126535b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 126635b9d88eSArnaldo Carvalho de Melo 12670817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 12680817df08SDavid Ahern 126935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 127035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 127135b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 127235b9d88eSArnaldo Carvalho de Melo 127335b9d88eSArnaldo Carvalho de Melo /* 127435b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 127535b9d88eSArnaldo Carvalho de Melo */ 127635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 127735b9d88eSArnaldo Carvalho de Melo 127835b9d88eSArnaldo Carvalho de Melo /* 127935b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 128035b9d88eSArnaldo Carvalho de Melo */ 12815f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 12825f1c4225SArnaldo Carvalho de Melo /* 12835f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 12845f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 12855f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 12865f1c4225SArnaldo Carvalho de Melo * 128720f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 12885f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 12895f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 12905f1c4225SArnaldo Carvalho de Melo * here. 12915f1c4225SArnaldo Carvalho de Melo */ 12925f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 12935f1c4225SArnaldo Carvalho de Melo if (ret == -1) 129435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 12955f1c4225SArnaldo Carvalho de Melo exit(ret); 12965f1c4225SArnaldo Carvalho de Melo } 129735b9d88eSArnaldo Carvalho de Melo 129835b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 129935b9d88eSArnaldo Carvalho de Melo 1300735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1301f33cbe72SArnaldo Carvalho de Melo union sigval val; 1302f33cbe72SArnaldo Carvalho de Melo 1303f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1304f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 130535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1306f33cbe72SArnaldo Carvalho de Melo } else 1307f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 130835b9d88eSArnaldo Carvalho de Melo exit(-1); 130935b9d88eSArnaldo Carvalho de Melo } 131035b9d88eSArnaldo Carvalho de Melo 1311735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1312735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1313735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1314735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1315735f7e0bSArnaldo Carvalho de Melo }; 1316735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1317735f7e0bSArnaldo Carvalho de Melo } 1318735f7e0bSArnaldo Carvalho de Melo 13191aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 132003617c22SJiri Olsa if (evlist->core.threads == NULL) { 13211aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 13221aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 13231aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 13241aaf63b1SArnaldo Carvalho de Melo } 132503617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 13261aaf63b1SArnaldo Carvalho de Melo } 132735b9d88eSArnaldo Carvalho de Melo 132835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 132935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 133035b9d88eSArnaldo Carvalho de Melo /* 133135b9d88eSArnaldo Carvalho de Melo * wait for child to settle 133235b9d88eSArnaldo Carvalho de Melo */ 133335b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 133435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 133535b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 133635b9d88eSArnaldo Carvalho de Melo } 133735b9d88eSArnaldo Carvalho de Melo 1338bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 133935b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 134035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 134135b9d88eSArnaldo Carvalho de Melo return 0; 134235b9d88eSArnaldo Carvalho de Melo 134335b9d88eSArnaldo Carvalho de Melo out_close_pipes: 134435b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 134535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 134635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 134735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 134835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 134935b9d88eSArnaldo Carvalho de Melo return -1; 135035b9d88eSArnaldo Carvalho de Melo } 135135b9d88eSArnaldo Carvalho de Melo 135263503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist) 135335b9d88eSArnaldo Carvalho de Melo { 135435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1355b3824404SDavid Ahern char bf = 0; 1356bcf3145fSNamhyung Kim int ret; 135735b9d88eSArnaldo Carvalho de Melo /* 135835b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 135935b9d88eSArnaldo Carvalho de Melo */ 1360bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1361bcf3145fSNamhyung Kim if (ret < 0) 1362e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1363bcf3145fSNamhyung Kim 1364bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1365bcf3145fSNamhyung Kim return ret; 136635b9d88eSArnaldo Carvalho de Melo } 136735b9d88eSArnaldo Carvalho de Melo 136835b9d88eSArnaldo Carvalho de Melo return 0; 136935b9d88eSArnaldo Carvalho de Melo } 1370cb0b29e0SArnaldo Carvalho de Melo 137163503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 13720807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1373cb0b29e0SArnaldo Carvalho de Melo { 137432dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 137575562573SAdrian Hunter 137675562573SAdrian Hunter if (!evsel) 137775562573SAdrian Hunter return -EFAULT; 13780807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1379cb0b29e0SArnaldo Carvalho de Melo } 138078f067b3SArnaldo Carvalho de Melo 138163503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 138201468120SJiri Olsa union perf_event *event, 138301468120SJiri Olsa u64 *timestamp) 138401468120SJiri Olsa { 138532dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 138601468120SJiri Olsa 138701468120SJiri Olsa if (!evsel) 138801468120SJiri Olsa return -EFAULT; 138901468120SJiri Olsa return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); 139001468120SJiri Olsa } 139101468120SJiri Olsa 139263503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist, 1393a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1394a8f23d8fSArnaldo Carvalho de Melo { 1395a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1396c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1397a8f23d8fSArnaldo Carvalho de Melo 1398a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1399a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1400a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1401a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1402a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1403a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1404a8f23d8fSArnaldo Carvalho de Melo 14051a47245dSAdrian Hunter value = perf_event_paranoid(); 1406a8f23d8fSArnaldo Carvalho de Melo 1407a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1408a8f23d8fSArnaldo Carvalho de Melo 1409a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1410a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1411a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1412a8f23d8fSArnaldo Carvalho de Melo } 1413a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14145229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1415a8f23d8fSArnaldo Carvalho de Melo 1416a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14175229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 14185229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1419a8f23d8fSArnaldo Carvalho de Melo break; 1420d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1421515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1422d9aade7fSArnaldo Carvalho de Melo int max_freq; 1423d9aade7fSArnaldo Carvalho de Melo 1424d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1425d9aade7fSArnaldo Carvalho de Melo goto out_default; 1426d9aade7fSArnaldo Carvalho de Melo 14271fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1428d9aade7fSArnaldo Carvalho de Melo goto out_default; 1429d9aade7fSArnaldo Carvalho de Melo 1430d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1431d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1432d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1433d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 14341fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1435d9aade7fSArnaldo Carvalho de Melo break; 1436d9aade7fSArnaldo Carvalho de Melo } 1437a8f23d8fSArnaldo Carvalho de Melo default: 1438d9aade7fSArnaldo Carvalho de Melo out_default: 1439a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1440a8f23d8fSArnaldo Carvalho de Melo break; 1441a8f23d8fSArnaldo Carvalho de Melo } 1442a8f23d8fSArnaldo Carvalho de Melo 1443a8f23d8fSArnaldo Carvalho de Melo return 0; 1444a8f23d8fSArnaldo Carvalho de Melo } 1445a025e4f0SAdrian Hunter 144663503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1447956fa571SArnaldo Carvalho de Melo { 1448c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1449f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1450956fa571SArnaldo Carvalho de Melo 1451956fa571SArnaldo Carvalho de Melo switch (err) { 1452956fa571SArnaldo Carvalho de Melo case EPERM: 1453e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1454e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1455e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1456956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1457e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1458e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1459e965bea1SArnaldo Carvalho de Melo 1460e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1461e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1462e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1463e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1464e965bea1SArnaldo Carvalho de Melo } 1465e965bea1SArnaldo Carvalho de Melo 1466e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1467e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1468956fa571SArnaldo Carvalho de Melo break; 1469956fa571SArnaldo Carvalho de Melo default: 1470956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1471956fa571SArnaldo Carvalho de Melo break; 1472956fa571SArnaldo Carvalho de Melo } 1473956fa571SArnaldo Carvalho de Melo 1474956fa571SArnaldo Carvalho de Melo return 0; 1475956fa571SArnaldo Carvalho de Melo } 1476956fa571SArnaldo Carvalho de Melo 147763503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 147832dcd021SJiri Olsa struct evsel *move_evsel) 1479a025e4f0SAdrian Hunter { 148032dcd021SJiri Olsa struct evsel *evsel, *n; 1481a025e4f0SAdrian Hunter LIST_HEAD(move); 1482a025e4f0SAdrian Hunter 1483515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1484a025e4f0SAdrian Hunter return; 1485a025e4f0SAdrian Hunter 1486e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1487a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1488b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1489a025e4f0SAdrian Hunter } 1490a025e4f0SAdrian Hunter 1491ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1492a025e4f0SAdrian Hunter } 149360b0896cSAdrian Hunter 149463503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist, 149532dcd021SJiri Olsa struct evsel *tracking_evsel) 149660b0896cSAdrian Hunter { 149732dcd021SJiri Olsa struct evsel *evsel; 149860b0896cSAdrian Hunter 149960b0896cSAdrian Hunter if (tracking_evsel->tracking) 150060b0896cSAdrian Hunter return; 150160b0896cSAdrian Hunter 1502e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 150360b0896cSAdrian Hunter if (evsel != tracking_evsel) 150460b0896cSAdrian Hunter evsel->tracking = false; 150560b0896cSAdrian Hunter } 150660b0896cSAdrian Hunter 150760b0896cSAdrian Hunter tracking_evsel->tracking = true; 150860b0896cSAdrian Hunter } 15097630b3e2SWang Nan 151032dcd021SJiri Olsa struct evsel * 151163503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist, 15127630b3e2SWang Nan const char *str) 15137630b3e2SWang Nan { 151432dcd021SJiri Olsa struct evsel *evsel; 15157630b3e2SWang Nan 1516e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 15177630b3e2SWang Nan if (!evsel->name) 15187630b3e2SWang Nan continue; 15197630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 15207630b3e2SWang Nan return evsel; 15217630b3e2SWang Nan } 15227630b3e2SWang Nan 15237630b3e2SWang Nan return NULL; 15247630b3e2SWang Nan } 152554cc54deSWang Nan 152663503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 152754cc54deSWang Nan enum bkw_mmap_state state) 152854cc54deSWang Nan { 152954cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 153054cc54deSWang Nan enum action { 153154cc54deSWang Nan NONE, 153254cc54deSWang Nan PAUSE, 153354cc54deSWang Nan RESUME, 153454cc54deSWang Nan } action = NONE; 153554cc54deSWang Nan 15360b72d69aSWang Nan if (!evlist->overwrite_mmap) 153754cc54deSWang Nan return; 153854cc54deSWang Nan 153954cc54deSWang Nan switch (old_state) { 154054cc54deSWang Nan case BKW_MMAP_NOTREADY: { 154154cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1542dd8bd53aSLuis de Bethencourt goto state_err; 154354cc54deSWang Nan break; 154454cc54deSWang Nan } 154554cc54deSWang Nan case BKW_MMAP_RUNNING: { 154654cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 154754cc54deSWang Nan goto state_err; 154854cc54deSWang Nan action = PAUSE; 154954cc54deSWang Nan break; 155054cc54deSWang Nan } 155154cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 155254cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 155354cc54deSWang Nan goto state_err; 155454cc54deSWang Nan break; 155554cc54deSWang Nan } 155654cc54deSWang Nan case BKW_MMAP_EMPTY: { 155754cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 155854cc54deSWang Nan goto state_err; 155954cc54deSWang Nan action = RESUME; 156054cc54deSWang Nan break; 156154cc54deSWang Nan } 156254cc54deSWang Nan default: 156354cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 156454cc54deSWang Nan } 156554cc54deSWang Nan 156654cc54deSWang Nan evlist->bkw_mmap_state = state; 156754cc54deSWang Nan 156854cc54deSWang Nan switch (action) { 156954cc54deSWang Nan case PAUSE: 157054cc54deSWang Nan perf_evlist__pause(evlist); 157154cc54deSWang Nan break; 157254cc54deSWang Nan case RESUME: 157354cc54deSWang Nan perf_evlist__resume(evlist); 157454cc54deSWang Nan break; 157554cc54deSWang Nan case NONE: 157654cc54deSWang Nan default: 157754cc54deSWang Nan break; 157854cc54deSWang Nan } 157954cc54deSWang Nan 158054cc54deSWang Nan state_err: 158154cc54deSWang Nan return; 158254cc54deSWang Nan } 158307d6f446SArnaldo Carvalho de Melo 158463503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 158507d6f446SArnaldo Carvalho de Melo { 158632dcd021SJiri Olsa struct evsel *evsel; 158707d6f446SArnaldo Carvalho de Melo 158807d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 15891fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 159007d6f446SArnaldo Carvalho de Melo return false; 159107d6f446SArnaldo Carvalho de Melo } 159207d6f446SArnaldo Carvalho de Melo 159307d6f446SArnaldo Carvalho de Melo return true; 159407d6f446SArnaldo Carvalho de Melo } 1595e2bdbe80SJin Yao 1596e2bdbe80SJin Yao /* 1597e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1598e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1599e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1600e2bdbe80SJin Yao */ 160163503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1602e2bdbe80SJin Yao { 1603e2bdbe80SJin Yao if (!evlist->nr_groups) { 1604515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1605e2bdbe80SJin Yao 1606e2bdbe80SJin Yao perf_evlist__set_leader(evlist); 1607e2bdbe80SJin Yao leader->forced_leader = true; 1608e2bdbe80SJin Yao } 1609e2bdbe80SJin Yao } 1610c3537fc2SAndi Kleen 161163503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 161232dcd021SJiri Olsa struct evsel *evsel) 1613c3537fc2SAndi Kleen { 161432dcd021SJiri Olsa struct evsel *c2, *leader; 1615c3537fc2SAndi Kleen bool is_open = true; 1616c3537fc2SAndi Kleen 1617c3537fc2SAndi Kleen leader = evsel->leader; 1618c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 16195643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1620c3537fc2SAndi Kleen 1621c3537fc2SAndi Kleen /* 1622c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1623c3537fc2SAndi Kleen * include the first entry. 1624c3537fc2SAndi Kleen */ 1625c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1626c3537fc2SAndi Kleen if (c2 == evsel) 1627c3537fc2SAndi Kleen is_open = false; 1628c3537fc2SAndi Kleen if (c2->leader == leader) { 1629c3537fc2SAndi Kleen if (is_open) 16305a40e199SAndi Kleen perf_evsel__close(&c2->core); 1631c3537fc2SAndi Kleen c2->leader = c2; 16325643b1a5SJiri Olsa c2->core.nr_members = 0; 1633c3537fc2SAndi Kleen } 1634c3537fc2SAndi Kleen } 1635c3537fc2SAndi Kleen return leader; 1636c3537fc2SAndi Kleen } 1637657ee553SSong Liu 163863503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist, 1639657ee553SSong Liu struct perf_event_attr *attr, 1640657ee553SSong Liu perf_evsel__sb_cb_t cb, 1641657ee553SSong Liu void *data) 1642657ee553SSong Liu { 164332dcd021SJiri Olsa struct evsel *evsel; 1644657ee553SSong Liu bool new_evlist = (*evlist) == NULL; 1645657ee553SSong Liu 1646657ee553SSong Liu if (*evlist == NULL) 16470f98b11cSJiri Olsa *evlist = evlist__new(); 1648657ee553SSong Liu if (*evlist == NULL) 1649657ee553SSong Liu return -1; 1650657ee553SSong Liu 1651657ee553SSong Liu if (!attr->sample_id_all) { 1652657ee553SSong Liu pr_warning("enabling sample_id_all for all side band events\n"); 1653657ee553SSong Liu attr->sample_id_all = 1; 1654657ee553SSong Liu } 1655657ee553SSong Liu 16566484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries); 1657657ee553SSong Liu if (!evsel) 1658657ee553SSong Liu goto out_err; 1659657ee553SSong Liu 1660657ee553SSong Liu evsel->side_band.cb = cb; 1661657ee553SSong Liu evsel->side_band.data = data; 1662a1cf3a75SJiri Olsa evlist__add(*evlist, evsel); 1663657ee553SSong Liu return 0; 1664657ee553SSong Liu 1665657ee553SSong Liu out_err: 1666657ee553SSong Liu if (new_evlist) { 1667c12995a5SJiri Olsa evlist__delete(*evlist); 1668657ee553SSong Liu *evlist = NULL; 1669657ee553SSong Liu } 1670657ee553SSong Liu return -1; 1671657ee553SSong Liu } 1672657ee553SSong Liu 1673657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg) 1674657ee553SSong Liu { 167563503dbaSJiri Olsa struct evlist *evlist = arg; 1676657ee553SSong Liu bool draining = false; 1677adc6257cSJiri Olsa int i, done = 0; 1678b397f846SArnaldo Carvalho de Melo /* 1679b397f846SArnaldo Carvalho de Melo * In order to read symbols from other namespaces perf to needs to call 1680b397f846SArnaldo Carvalho de Melo * setns(2). This isn't permitted if the struct_fs has multiple users. 1681b397f846SArnaldo Carvalho de Melo * unshare(2) the fs so that we may continue to setns into namespaces 1682b397f846SArnaldo Carvalho de Melo * that we're observing when, for instance, reading the build-ids at 1683b397f846SArnaldo Carvalho de Melo * the end of a 'perf record' session. 1684b397f846SArnaldo Carvalho de Melo */ 1685b397f846SArnaldo Carvalho de Melo unshare(CLONE_FS); 1686657ee553SSong Liu 1687adc6257cSJiri Olsa while (!done) { 1688adc6257cSJiri Olsa bool got_data = false; 1689adc6257cSJiri Olsa 1690adc6257cSJiri Olsa if (evlist->thread.done) 1691657ee553SSong Liu draining = true; 1692657ee553SSong Liu 1693657ee553SSong Liu if (!draining) 169480ab2987SJiri Olsa evlist__poll(evlist, 1000); 1695657ee553SSong Liu 1696c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 1697a5830532SJiri Olsa struct mmap *map = &evlist->mmap[i]; 1698657ee553SSong Liu union perf_event *event; 1699657ee553SSong Liu 17007c4d4182SJiri Olsa if (perf_mmap__read_init(&map->core)) 1701657ee553SSong Liu continue; 1702151ed5d7SJiri Olsa while ((event = perf_mmap__read_event(&map->core)) != NULL) { 170332dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1704657ee553SSong Liu 1705657ee553SSong Liu if (evsel && evsel->side_band.cb) 1706657ee553SSong Liu evsel->side_band.cb(event, evsel->side_band.data); 1707657ee553SSong Liu else 1708657ee553SSong Liu pr_warning("cannot locate proper evsel for the side band event\n"); 1709657ee553SSong Liu 17107728fa0cSJiri Olsa perf_mmap__consume(&map->core); 1711adc6257cSJiri Olsa got_data = true; 1712657ee553SSong Liu } 171332fdc2caSJiri Olsa perf_mmap__read_done(&map->core); 1714657ee553SSong Liu } 1715adc6257cSJiri Olsa 1716adc6257cSJiri Olsa if (draining && !got_data) 1717adc6257cSJiri Olsa break; 1718657ee553SSong Liu } 1719657ee553SSong Liu return NULL; 1720657ee553SSong Liu } 1721657ee553SSong Liu 172263503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist, 1723657ee553SSong Liu struct target *target) 1724657ee553SSong Liu { 172532dcd021SJiri Olsa struct evsel *counter; 1726657ee553SSong Liu 1727657ee553SSong Liu if (!evlist) 1728657ee553SSong Liu return 0; 1729657ee553SSong Liu 1730657ee553SSong Liu if (perf_evlist__create_maps(evlist, target)) 1731657ee553SSong Liu goto out_delete_evlist; 1732657ee553SSong Liu 1733657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1734f72f901dSJiri Olsa if (evsel__open(counter, evlist->core.cpus, 173503617c22SJiri Olsa evlist->core.threads) < 0) 1736657ee553SSong Liu goto out_delete_evlist; 1737657ee553SSong Liu } 1738657ee553SSong Liu 17399521b5f2SJiri Olsa if (evlist__mmap(evlist, UINT_MAX)) 1740657ee553SSong Liu goto out_delete_evlist; 1741657ee553SSong Liu 1742657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1743ec7f24efSJiri Olsa if (evsel__enable(counter)) 1744657ee553SSong Liu goto out_delete_evlist; 1745657ee553SSong Liu } 1746657ee553SSong Liu 1747657ee553SSong Liu evlist->thread.done = 0; 1748657ee553SSong Liu if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist)) 1749657ee553SSong Liu goto out_delete_evlist; 1750657ee553SSong Liu 1751657ee553SSong Liu return 0; 1752657ee553SSong Liu 1753657ee553SSong Liu out_delete_evlist: 1754c12995a5SJiri Olsa evlist__delete(evlist); 1755657ee553SSong Liu evlist = NULL; 1756657ee553SSong Liu return -1; 1757657ee553SSong Liu } 1758657ee553SSong Liu 175963503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist) 1760657ee553SSong Liu { 1761657ee553SSong Liu if (!evlist) 1762657ee553SSong Liu return; 1763657ee553SSong Liu evlist->thread.done = 1; 1764657ee553SSong Liu pthread_join(evlist->thread.th, NULL); 1765c12995a5SJiri Olsa evlist__delete(evlist); 1766657ee553SSong Liu } 1767