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" 249607ad3aSArnaldo Carvalho de Melo #include <signal.h> 2535b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 26b397f846SArnaldo Carvalho de Melo #include <sched.h> 27f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h> 28361c99a6SArnaldo Carvalho de Melo 2950d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 304b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 3150d08e47SArnaldo Carvalho de Melo 32bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 3386a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 34f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 35f8a95309SArnaldo Carvalho de Melo 3670db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 3770db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 380389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 398dd2a131SJiri Olsa #include <linux/err.h> 408520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 417f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 424562a739SJiri Olsa #include <perf/evlist.h> 4388761fa1SJiri Olsa #include <perf/evsel.h> 449c3516d1SJiri Olsa #include <perf/cpumap.h> 457728fa0cSJiri Olsa #include <perf/mmap.h> 4670db7533SArnaldo Carvalho de Melo 47e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h> 48e14e5497SArnaldo Carvalho de Melo 49748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 50748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 51748fe088SArnaldo Carvalho de Melo #endif 52748fe088SArnaldo Carvalho de Melo 539dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 548cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 55f8a95309SArnaldo Carvalho de Melo 5652c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 579749b90eSJiri Olsa struct perf_thread_map *threads) 58361c99a6SArnaldo Carvalho de Melo { 594562a739SJiri Olsa perf_evlist__init(&evlist->core); 60453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 6140cb2d51SJiri Olsa fdarray__init(&evlist->core.pollfd, 64); 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); 14240cb2d51SJiri Olsa fdarray__exit(&evlist->core.pollfd); 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); 152f72f901dSJiri Olsa perf_cpu_map__put(evlist->core.cpus); 15303617c22SJiri Olsa perf_thread_map__put(evlist->core.threads); 154f72f901dSJiri Olsa evlist->core.cpus = NULL; 15503617c22SJiri Olsa evlist->core.threads = NULL; 156e6b1878dSJiri Olsa evlist__purge(evlist); 157470579b0SJiri Olsa evlist__exit(evlist); 158361c99a6SArnaldo Carvalho de Melo free(evlist); 159361c99a6SArnaldo Carvalho de Melo } 160361c99a6SArnaldo Carvalho de Melo 161a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 162361c99a6SArnaldo Carvalho de Melo { 163d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1646484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 16560b0896cSAdrian Hunter entry->tracking = !entry->idx; 166ef503831SArnaldo Carvalho de Melo 1676484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1686484d2f9SJiri Olsa 1696484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 17075562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 171361c99a6SArnaldo Carvalho de Melo } 172361c99a6SArnaldo Carvalho de Melo 17316251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1744768230aSAdrian Hunter { 1754768230aSAdrian Hunter evsel->evlist = NULL; 17652e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1774768230aSAdrian Hunter } 1784768230aSAdrian Hunter 17963503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist, 180f114d6efSAdrian Hunter struct list_head *list) 18150d08e47SArnaldo Carvalho de Melo { 18232dcd021SJiri Olsa struct evsel *evsel, *temp; 18375562573SAdrian Hunter 184e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 185b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 186a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 187f114d6efSAdrian Hunter } 18850d08e47SArnaldo Carvalho de Melo } 18950d08e47SArnaldo Carvalho de Melo 190c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist, 191c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs) 192c0e53476SArnaldo Carvalho de Melo { 193c0e53476SArnaldo Carvalho de Melo struct evsel *evsel; 194c0e53476SArnaldo Carvalho de Melo size_t i; 195c0e53476SArnaldo Carvalho de Melo int err; 196c0e53476SArnaldo Carvalho de Melo 197c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 198c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it. 199c0e53476SArnaldo Carvalho de Melo evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name); 200c0e53476SArnaldo Carvalho de Melo if (evsel == NULL) 201c0e53476SArnaldo Carvalho de Melo continue; 202c0e53476SArnaldo Carvalho de Melo 203c0e53476SArnaldo Carvalho de Melo err = -EEXIST; 204c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL) 205c0e53476SArnaldo Carvalho de Melo goto out; 206c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler; 207c0e53476SArnaldo Carvalho de Melo } 208c0e53476SArnaldo Carvalho de Melo 209c0e53476SArnaldo Carvalho de Melo err = 0; 210c0e53476SArnaldo Carvalho de Melo out: 211c0e53476SArnaldo Carvalho de Melo return err; 212c0e53476SArnaldo Carvalho de Melo } 213c0e53476SArnaldo Carvalho de Melo 21463dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 21563dab225SArnaldo Carvalho de Melo { 21632dcd021SJiri Olsa struct evsel *evsel, *leader; 21763dab225SArnaldo Carvalho de Melo 218b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 219b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 22097f63e4aSNamhyung Kim 2215643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 22263dab225SArnaldo Carvalho de Melo 223e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22463dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22563dab225SArnaldo Carvalho de Melo } 22663dab225SArnaldo Carvalho de Melo } 22763dab225SArnaldo Carvalho de Melo 22863503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist) 2296a4bb04cSJiri Olsa { 2306484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2316484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 232ce9036a6SJiri Olsa __perf_evlist__set_leader(&evlist->core.entries); 2336a4bb04cSJiri Olsa } 23497f63e4aSNamhyung Kim } 2356a4bb04cSJiri Olsa 23663503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise) 237361c99a6SArnaldo Carvalho de Melo { 23832dcd021SJiri Olsa struct evsel *evsel = perf_evsel__new_cycles(precise); 239361c99a6SArnaldo Carvalho de Melo 240361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2417c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 242361c99a6SArnaldo Carvalho de Melo 243a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 244361c99a6SArnaldo Carvalho de Melo return 0; 245361c99a6SArnaldo Carvalho de Melo } 2465c581041SArnaldo Carvalho de Melo 24763503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist) 2485bae0250SArnaldo Carvalho de Melo { 2495bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2505bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2515bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2525bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2535bae0250SArnaldo Carvalho de Melo }; 2546484d2f9SJiri Olsa struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries); 2555bae0250SArnaldo Carvalho de Melo 2565bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2575bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2585bae0250SArnaldo Carvalho de Melo 259a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2605bae0250SArnaldo Carvalho de Melo return 0; 2615bae0250SArnaldo Carvalho de Melo } 2625bae0250SArnaldo Carvalho de Melo 263a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist, 26450d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 26550d08e47SArnaldo Carvalho de Melo { 26632dcd021SJiri Olsa struct evsel *evsel, *n; 26750d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 26850d08e47SArnaldo Carvalho de Melo size_t i; 26950d08e47SArnaldo Carvalho de Melo 27050d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2716484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 27250d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27350d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 274b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 27550d08e47SArnaldo Carvalho de Melo } 27650d08e47SArnaldo Carvalho de Melo 277f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 27850d08e47SArnaldo Carvalho de Melo 27950d08e47SArnaldo Carvalho de Melo return 0; 28050d08e47SArnaldo Carvalho de Melo 28150d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 282e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2835eb2dd2aSJiri Olsa evsel__delete(evsel); 28450d08e47SArnaldo Carvalho de Melo return -1; 28550d08e47SArnaldo Carvalho de Melo } 28650d08e47SArnaldo Carvalho de Melo 28763503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist, 28879695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 28979695e1bSArnaldo Carvalho de Melo { 29079695e1bSArnaldo Carvalho de Melo size_t i; 29179695e1bSArnaldo Carvalho de Melo 29279695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 29379695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 29479695e1bSArnaldo Carvalho de Melo 295a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 29679695e1bSArnaldo Carvalho de Melo } 29779695e1bSArnaldo Carvalho de Melo 29832dcd021SJiri Olsa struct evsel * 29963503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 300ee29be62SArnaldo Carvalho de Melo { 30132dcd021SJiri Olsa struct evsel *evsel; 302ee29be62SArnaldo Carvalho de Melo 303e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3041fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3051fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 306ee29be62SArnaldo Carvalho de Melo return evsel; 307ee29be62SArnaldo Carvalho de Melo } 308ee29be62SArnaldo Carvalho de Melo 309ee29be62SArnaldo Carvalho de Melo return NULL; 310ee29be62SArnaldo Carvalho de Melo } 311ee29be62SArnaldo Carvalho de Melo 31232dcd021SJiri Olsa struct evsel * 31363503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist, 314a2f2804aSDavid Ahern const char *name) 315a2f2804aSDavid Ahern { 31632dcd021SJiri Olsa struct evsel *evsel; 317a2f2804aSDavid Ahern 318e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3191fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 320a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 321a2f2804aSDavid Ahern return evsel; 322a2f2804aSDavid Ahern } 323a2f2804aSDavid Ahern 324a2f2804aSDavid Ahern return NULL; 325a2f2804aSDavid Ahern } 326a2f2804aSDavid Ahern 32763503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist, 32839876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 32939876e7dSArnaldo Carvalho de Melo { 33032dcd021SJiri Olsa struct evsel *evsel = perf_evsel__newtp(sys, name); 33139876e7dSArnaldo Carvalho de Melo 3328dd2a131SJiri Olsa if (IS_ERR(evsel)) 33339876e7dSArnaldo Carvalho de Melo return -1; 33439876e7dSArnaldo Carvalho de Melo 335744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 336a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 33739876e7dSArnaldo Carvalho de Melo return 0; 33839876e7dSArnaldo Carvalho de Melo } 33939876e7dSArnaldo Carvalho de Melo 34063503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist, 34132dcd021SJiri Olsa struct evsel *evsel) 342bf8e8f4bSAdrian Hunter { 343648b5af3SJiri Olsa if (evsel->core.system_wide) 344bf8e8f4bSAdrian Hunter return 1; 345bf8e8f4bSAdrian Hunter else 346a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 347bf8e8f4bSAdrian Hunter } 348bf8e8f4bSAdrian Hunter 349e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3504152ab37SArnaldo Carvalho de Melo { 35132dcd021SJiri Olsa struct evsel *pos; 3524152ab37SArnaldo Carvalho de Melo 353e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3549dfcb759SJiri Olsa if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd) 3553fe4430dSJiri Olsa continue; 3569a10bb22SJiri Olsa evsel__disable(pos); 3574152ab37SArnaldo Carvalho de Melo } 3582b56bcfbSArnaldo Carvalho de Melo 3592b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3604152ab37SArnaldo Carvalho de Melo } 3614152ab37SArnaldo Carvalho de Melo 3621c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 363764e16a3SDavid Ahern { 36432dcd021SJiri Olsa struct evsel *pos; 365764e16a3SDavid Ahern 366e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3679dfcb759SJiri Olsa if (!perf_evsel__is_group_leader(pos) || !pos->core.fd) 3683fe4430dSJiri Olsa continue; 369ec7f24efSJiri Olsa evsel__enable(pos); 370764e16a3SDavid Ahern } 3712b56bcfbSArnaldo Carvalho de Melo 3722b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3732b56bcfbSArnaldo Carvalho de Melo } 3742b56bcfbSArnaldo Carvalho de Melo 37563503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist) 3762b56bcfbSArnaldo Carvalho de Melo { 377e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 378764e16a3SDavid Ahern } 379764e16a3SDavid Ahern 38063503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist, 38132dcd021SJiri Olsa struct evsel *evsel, int cpu) 3821c65056cSAdrian Hunter { 38318ef15c6SArnaldo Carvalho de Melo int thread; 3841c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3851c65056cSAdrian Hunter 3869dfcb759SJiri Olsa if (!evsel->core.fd) 3871c65056cSAdrian Hunter return -EINVAL; 3881c65056cSAdrian Hunter 3891c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 39018ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3911c65056cSAdrian Hunter if (err) 3921c65056cSAdrian Hunter return err; 3931c65056cSAdrian Hunter } 3941c65056cSAdrian Hunter return 0; 3951c65056cSAdrian Hunter } 3961c65056cSAdrian Hunter 39763503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist, 39832dcd021SJiri Olsa struct evsel *evsel, 3991c65056cSAdrian Hunter int thread) 4001c65056cSAdrian Hunter { 40118ef15c6SArnaldo Carvalho de Melo int cpu; 4026549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 4031c65056cSAdrian Hunter 4049dfcb759SJiri Olsa if (!evsel->core.fd) 4051c65056cSAdrian Hunter return -EINVAL; 4061c65056cSAdrian Hunter 4071c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 40818ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4091c65056cSAdrian Hunter if (err) 4101c65056cSAdrian Hunter return err; 4111c65056cSAdrian Hunter } 4121c65056cSAdrian Hunter return 0; 4131c65056cSAdrian Hunter } 4141c65056cSAdrian Hunter 41563503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist, 41632dcd021SJiri Olsa struct evsel *evsel, int idx) 4171c65056cSAdrian Hunter { 418315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 4191c65056cSAdrian Hunter 4201c65056cSAdrian Hunter if (per_cpu_mmaps) 4211c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4221c65056cSAdrian Hunter else 4231c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4241c65056cSAdrian Hunter } 4251c65056cSAdrian Hunter 426f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 427e4b356b5SArnaldo Carvalho de Melo { 428f4009e7bSJiri Olsa return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN); 429e4b356b5SArnaldo Carvalho de Melo } 4301b85337dSArnaldo Carvalho de Melo 431258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 432258e4bfcSWang Nan void *arg __maybe_unused) 433e4b356b5SArnaldo Carvalho de Melo { 434923d0f18SJiri Olsa struct perf_mmap *map = fda->priv[fd].ptr; 435e4b356b5SArnaldo Carvalho de Melo 4364876075bSWang Nan if (map) 437923d0f18SJiri Olsa perf_mmap__put(map); 43870082dd9SArnaldo Carvalho de Melo } 43970db7533SArnaldo Carvalho de Melo 440f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 4411ddec7f0SArnaldo Carvalho de Melo { 44240cb2d51SJiri Olsa return fdarray__filter(&evlist->core.pollfd, revents_and_mask, 443258e4bfcSWang Nan perf_evlist__munmap_filtered, NULL); 4441ddec7f0SArnaldo Carvalho de Melo } 4451ddec7f0SArnaldo Carvalho de Melo 44680ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 447f66a889dSArnaldo Carvalho de Melo { 44880ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 449f66a889dSArnaldo Carvalho de Melo } 450f66a889dSArnaldo Carvalho de Melo 45163503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 45270db7533SArnaldo Carvalho de Melo { 45370db7533SArnaldo Carvalho de Melo struct hlist_head *head; 45470db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 45570db7533SArnaldo Carvalho de Melo int hash; 45670db7533SArnaldo Carvalho de Melo 45770db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 4581d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 45970db7533SArnaldo Carvalho de Melo 460b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 46170db7533SArnaldo Carvalho de Melo if (sid->id == id) 462932a3594SJiri Olsa return sid; 463932a3594SJiri Olsa 464932a3594SJiri Olsa return NULL; 465932a3594SJiri Olsa } 466932a3594SJiri Olsa 46763503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 468932a3594SJiri Olsa { 469932a3594SJiri Olsa struct perf_sample_id *sid; 470932a3594SJiri Olsa 4716484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 472515dbe48SJiri Olsa return evlist__first(evlist); 473932a3594SJiri Olsa 474932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 475932a3594SJiri Olsa if (sid) 47670c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 47730e68bccSNamhyung Kim 47830e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 479515dbe48SJiri Olsa return evlist__first(evlist); 48030e68bccSNamhyung Kim 48170db7533SArnaldo Carvalho de Melo return NULL; 48270db7533SArnaldo Carvalho de Melo } 48304391debSArnaldo Carvalho de Melo 48463503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 485dddcf6abSAdrian Hunter u64 id) 486dddcf6abSAdrian Hunter { 487dddcf6abSAdrian Hunter struct perf_sample_id *sid; 488dddcf6abSAdrian Hunter 489dddcf6abSAdrian Hunter if (!id) 490dddcf6abSAdrian Hunter return NULL; 491dddcf6abSAdrian Hunter 492dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 493dddcf6abSAdrian Hunter if (sid) 49470c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 495dddcf6abSAdrian Hunter 496dddcf6abSAdrian Hunter return NULL; 497dddcf6abSAdrian Hunter } 498dddcf6abSAdrian Hunter 49963503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist, 50075562573SAdrian Hunter union perf_event *event, u64 *id) 50175562573SAdrian Hunter { 502b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 50375562573SAdrian Hunter ssize_t n; 50475562573SAdrian Hunter 50575562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 50675562573SAdrian Hunter 50775562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 50875562573SAdrian Hunter if (evlist->id_pos >= n) 50975562573SAdrian Hunter return -1; 51075562573SAdrian Hunter *id = array[evlist->id_pos]; 51175562573SAdrian Hunter } else { 51275562573SAdrian Hunter if (evlist->is_pos > n) 51375562573SAdrian Hunter return -1; 51475562573SAdrian Hunter n -= evlist->is_pos; 51575562573SAdrian Hunter *id = array[n]; 51675562573SAdrian Hunter } 51775562573SAdrian Hunter return 0; 51875562573SAdrian Hunter } 51975562573SAdrian Hunter 52063503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 52175562573SAdrian Hunter union perf_event *event) 52275562573SAdrian Hunter { 523515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 52475562573SAdrian Hunter struct hlist_head *head; 52575562573SAdrian Hunter struct perf_sample_id *sid; 52675562573SAdrian Hunter int hash; 52775562573SAdrian Hunter u64 id; 52875562573SAdrian Hunter 5296484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 53098be6966SAdrian Hunter return first; 53198be6966SAdrian Hunter 5321fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 53398be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 53498be6966SAdrian Hunter return first; 53575562573SAdrian Hunter 53675562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 53775562573SAdrian Hunter return NULL; 53875562573SAdrian Hunter 53975562573SAdrian Hunter /* Synthesized events have an id of zero */ 54075562573SAdrian Hunter if (!id) 54198be6966SAdrian Hunter return first; 54275562573SAdrian Hunter 54375562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5441d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 54575562573SAdrian Hunter 54675562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 54775562573SAdrian Hunter if (sid->id == id) 54870c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 54975562573SAdrian Hunter } 55075562573SAdrian Hunter return NULL; 55175562573SAdrian Hunter } 55275562573SAdrian Hunter 55363503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value) 55465aea233SWang Nan { 55565aea233SWang Nan int i; 55665aea233SWang Nan 5570b72d69aSWang Nan if (!evlist->overwrite_mmap) 558078c3386SWang Nan return 0; 559078c3386SWang Nan 560c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 5612cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 56265aea233SWang Nan int err; 56365aea233SWang Nan 56465aea233SWang Nan if (fd < 0) 56565aea233SWang Nan continue; 56665aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 56765aea233SWang Nan if (err) 56865aea233SWang Nan return err; 56965aea233SWang Nan } 57065aea233SWang Nan return 0; 57165aea233SWang Nan } 57265aea233SWang Nan 57363503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist) 57465aea233SWang Nan { 57565aea233SWang Nan return perf_evlist__set_paused(evlist, true); 57665aea233SWang Nan } 57765aea233SWang Nan 57863503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist) 57965aea233SWang Nan { 58065aea233SWang Nan return perf_evlist__set_paused(evlist, false); 58165aea233SWang Nan } 58265aea233SWang Nan 583db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 584f8a95309SArnaldo Carvalho de Melo { 585aece948fSArnaldo Carvalho de Melo int i; 586f8a95309SArnaldo Carvalho de Melo 587b2cb615dSWang Nan if (evlist->mmap) 588c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 58980e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 590b2cb615dSWang Nan 5910b72d69aSWang Nan if (evlist->overwrite_mmap) 592c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 59380e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 594a1f72618SWang Nan } 595aece948fSArnaldo Carvalho de Melo 596db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 597a1f72618SWang Nan { 598db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 59904662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6000b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 601f8a95309SArnaldo Carvalho de Melo } 602f8a95309SArnaldo Carvalho de Melo 60380e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 60480e53d11SJiri Olsa { 60580e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 60680e53d11SJiri Olsa 60780e53d11SJiri Olsa mmap__munmap(m); 60880e53d11SJiri Olsa } 60980e53d11SJiri Olsa 610d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 6112c5f6d87SKan Liang bool overwrite) 612f8a95309SArnaldo Carvalho de Melo { 613d4c6fb36SWang Nan int i; 614a5830532SJiri Olsa struct mmap *map; 615d4c6fb36SWang Nan 616c976ee11SJiri Olsa evlist->core.nr_mmaps = perf_cpu_map__nr(evlist->core.cpus); 617315c0a1fSJiri Olsa if (perf_cpu_map__empty(evlist->core.cpus)) 618c976ee11SJiri Olsa evlist->core.nr_mmaps = perf_thread_map__nr(evlist->core.threads); 619c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 6208db6d6b1SWang Nan if (!map) 6218db6d6b1SWang Nan return NULL; 622946ae1d4SWang Nan 623c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6244738ca30SArnaldo Carvalho de Melo /* 6254738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 6266afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 6274738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 6284738ca30SArnaldo Carvalho de Melo * dropped. 6294738ca30SArnaldo Carvalho de Melo * 6304738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 6314738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 6324738ca30SArnaldo Carvalho de Melo */ 63380e53d11SJiri Olsa perf_mmap__init(&map[i].core, overwrite, perf_mmap__unmap_cb); 6344738ca30SArnaldo Carvalho de Melo } 635353120b4SJiri Olsa 6368db6d6b1SWang Nan return map; 637f8a95309SArnaldo Carvalho de Melo } 638f8a95309SArnaldo Carvalho de Melo 6399abd2ab2SJiri Olsa static void 6409abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 6419abd2ab2SJiri Olsa struct perf_mmap_param *_mp, 6429abd2ab2SJiri Olsa int idx, bool per_cpu) 6439abd2ab2SJiri Olsa { 6449abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 6459abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 6469abd2ab2SJiri Olsa 6479abd2ab2SJiri Olsa auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 6489abd2ab2SJiri Olsa } 6499abd2ab2SJiri Olsa 650bb1b1885SJiri Olsa static struct perf_mmap* 651bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 652bb1b1885SJiri Olsa { 653bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 654bb1b1885SJiri Olsa struct mmap *maps = evlist->mmap; 655bb1b1885SJiri Olsa 656bb1b1885SJiri Olsa if (overwrite) { 657bb1b1885SJiri Olsa maps = evlist->overwrite_mmap; 658bb1b1885SJiri Olsa 659bb1b1885SJiri Olsa if (!maps) { 660bb1b1885SJiri Olsa maps = evlist__alloc_mmap(evlist, true); 661bb1b1885SJiri Olsa if (!maps) 662bb1b1885SJiri Olsa return NULL; 663bb1b1885SJiri Olsa 664bb1b1885SJiri Olsa evlist->overwrite_mmap = maps; 665bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 666bb1b1885SJiri Olsa perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 667bb1b1885SJiri Olsa } 668bb1b1885SJiri Olsa } 669bb1b1885SJiri Olsa 670bb1b1885SJiri Olsa return &maps[idx].core; 671bb1b1885SJiri Olsa } 672bb1b1885SJiri Olsa 673b80132b1SJiri Olsa static int 674b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 675b80132b1SJiri Olsa int output, int cpu) 676b80132b1SJiri Olsa { 677b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core); 678b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 679b80132b1SJiri Olsa 680b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu); 681b80132b1SJiri Olsa } 682b80132b1SJiri Olsa 683f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 684994a1f78SJiri Olsa { 685f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 6868185e881SArnaldo Carvalho de Melo int max; 6878185e881SArnaldo Carvalho de Melo 6888185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 6898185e881SArnaldo Carvalho de Melo /* 6908185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 6918185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 6928185e881SArnaldo Carvalho de Melo * die yet... 6938185e881SArnaldo Carvalho de Melo */ 6948185e881SArnaldo Carvalho de Melo max = 512; 6958185e881SArnaldo Carvalho de Melo } else { 6968185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 6978185e881SArnaldo Carvalho de Melo } 6988185e881SArnaldo Carvalho de Melo 6998185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 7001be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 7011be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 702f5e7150cSArnaldo Carvalho de Melo 703f5e7150cSArnaldo Carvalho de Melo return pages; 704f5e7150cSArnaldo Carvalho de Melo } 705f5e7150cSArnaldo Carvalho de Melo 7069521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 707f5e7150cSArnaldo Carvalho de Melo { 708f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 709f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 710f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 711994a1f78SJiri Olsa return 0; 712994a1f78SJiri Olsa 713994a1f78SJiri Olsa return (pages + 1) * page_size; 714994a1f78SJiri Olsa } 715994a1f78SJiri Olsa 71633c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 71733c2dcfdSDavid Ahern unsigned long max) 718994a1f78SJiri Olsa { 7192fbe4abeSAdrian Hunter unsigned long pages, val; 72027050f53SJiri Olsa static struct parse_tag tags[] = { 72127050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 72227050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 72327050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 72427050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 72527050f53SJiri Olsa { .tag = 0 }, 72627050f53SJiri Olsa }; 727994a1f78SJiri Olsa 7288973504bSDavid Ahern if (str == NULL) 72933c2dcfdSDavid Ahern return -EINVAL; 7308973504bSDavid Ahern 73127050f53SJiri Olsa val = parse_tag_value(str, tags); 7322fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 73327050f53SJiri Olsa /* we got file size value */ 73427050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 73527050f53SJiri Olsa } else { 73627050f53SJiri Olsa /* we got pages count value */ 73727050f53SJiri Olsa char *eptr; 738994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 73933c2dcfdSDavid Ahern if (*eptr != '\0') 74033c2dcfdSDavid Ahern return -EINVAL; 74127050f53SJiri Olsa } 742994a1f78SJiri Olsa 7432bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 74433c2dcfdSDavid Ahern /* leave number of pages at 0 */ 7451dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 7469808143bSJiri Olsa char buf[100]; 7479808143bSJiri Olsa 74833c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 74991529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 7501dbfa938SAdrian Hunter if (!pages) 7511dbfa938SAdrian Hunter return -EINVAL; 7529808143bSJiri Olsa 7539808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 7549808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 7559808143bSJiri Olsa buf, pages); 7562fbe4abeSAdrian Hunter } 7572fbe4abeSAdrian Hunter 75833c2dcfdSDavid Ahern if (pages > max) 75933c2dcfdSDavid Ahern return -EINVAL; 76033c2dcfdSDavid Ahern 76133c2dcfdSDavid Ahern return pages; 76233c2dcfdSDavid Ahern } 76333c2dcfdSDavid Ahern 764e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 76533c2dcfdSDavid Ahern { 76633c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 76733c2dcfdSDavid Ahern long pages; 76833c2dcfdSDavid Ahern 769f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 77033c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 77133c2dcfdSDavid Ahern 77233c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 77333c2dcfdSDavid Ahern if (pages < 0) { 77433c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 775994a1f78SJiri Olsa return -1; 776994a1f78SJiri Olsa } 777994a1f78SJiri Olsa 778994a1f78SJiri Olsa *mmap_pages = pages; 779994a1f78SJiri Olsa return 0; 780994a1f78SJiri Olsa } 781994a1f78SJiri Olsa 782e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 783e9db1310SAdrian Hunter int unset __maybe_unused) 784e9db1310SAdrian Hunter { 785e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 786e9db1310SAdrian Hunter } 787e9db1310SAdrian Hunter 788c83fa7f2SAdrian Hunter /** 7899521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 790c83fa7f2SAdrian Hunter * @evlist: list of events 791c83fa7f2SAdrian Hunter * @pages: map length in pages 792c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 793718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 794718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 795f8a95309SArnaldo Carvalho de Melo * 796c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 7979521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 798c83fa7f2SAdrian Hunter * automatically. 799f8a95309SArnaldo Carvalho de Melo * 800718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 801718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 802718c602dSAdrian Hunter * 803c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 804f8a95309SArnaldo Carvalho de Melo */ 8059521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 8067a276ff6SWang Nan unsigned int auxtrace_pages, 80751255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 80851255a8aSAlexey Budankov int comp_level) 809f8a95309SArnaldo Carvalho de Melo { 81071f566a3SWang Nan /* 81171f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 81271f566a3SWang Nan * Its value is decided by evsel's write_backward. 81371f566a3SWang Nan * So &mp should not be passed through const pointer. 81471f566a3SWang Nan */ 815e440979fSJiri Olsa struct mmap_params mp = { 816e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 817e440979fSJiri Olsa .affinity = affinity, 818e440979fSJiri Olsa .flush = flush, 819e440979fSJiri Olsa .comp_level = comp_level 820e440979fSJiri Olsa }; 821923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = { 8229abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx, 823bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get, 824b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap, 8259abd2ab2SJiri Olsa }; 82650a682ceSArnaldo Carvalho de Melo 8278db6d6b1SWang Nan if (!evlist->mmap) 828d50cf361SJiri Olsa evlist->mmap = evlist__alloc_mmap(evlist, false); 8298db6d6b1SWang Nan if (!evlist->mmap) 830f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 831f8a95309SArnaldo Carvalho de Melo 83231f67fc4SJiri Olsa if (evlist->core.pollfd.entries == NULL && perf_evlist__alloc_pollfd(&evlist->core) < 0) 833f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 834f8a95309SArnaldo Carvalho de Melo 835f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 836f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 837e440979fSJiri Olsa mp.core.mask = evlist->core.mmap_len - page_size - 1; 838f8a95309SArnaldo Carvalho de Melo 839f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 840718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 841718c602dSAdrian Hunter 842923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 843f8a95309SArnaldo Carvalho de Melo } 8447e2ed097SArnaldo Carvalho de Melo 8459521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 846718c602dSAdrian Hunter { 8479521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 848718c602dSAdrian Hunter } 849718c602dSAdrian Hunter 85063503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 8517e2ed097SArnaldo Carvalho de Melo { 852147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 853f854839bSJiri Olsa struct perf_cpu_map *cpus; 8549749b90eSJiri Olsa struct perf_thread_map *threads; 8557e2ed097SArnaldo Carvalho de Melo 856147c508fSJin Yao /* 857147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 858147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 859147c508fSJin Yao * target->system_wide = true. 860147c508fSJin Yao * 861147c508fSJin Yao * If specify '--per-thread' only to perf record, 862147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 863147c508fSJin Yao * 864147c508fSJin Yao * So target->per_thread && target->system_wide is false. 865147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 866147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 867147c508fSJin Yao * current behavior. 868147c508fSJin Yao * 869147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 870147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 871147c508fSJin Yao * per-thread data. thread_map__new_str will call 872147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 873147c508fSJin Yao */ 87473c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 875147c508fSJin Yao all_threads); 87674bfd2b2SAdrian Hunter 87774bfd2b2SAdrian Hunter if (!threads) 8787e2ed097SArnaldo Carvalho de Melo return -1; 8797e2ed097SArnaldo Carvalho de Melo 8809c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 881397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 882879d77d0SNamhyung Kim else 8839c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 8847e2ed097SArnaldo Carvalho de Melo 88574bfd2b2SAdrian Hunter if (!cpus) 8867e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 8877e2ed097SArnaldo Carvalho de Melo 888ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 889ec9a77a7SAdrian Hunter 890453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 891d5bc056eSAdrian Hunter 892d5bc056eSAdrian Hunter return 0; 8937e2ed097SArnaldo Carvalho de Melo 8947e2ed097SArnaldo Carvalho de Melo out_delete_threads: 8957836e52eSJiri Olsa perf_thread_map__put(threads); 8967e2ed097SArnaldo Carvalho de Melo return -1; 8977e2ed097SArnaldo Carvalho de Melo } 8987e2ed097SArnaldo Carvalho de Melo 89963503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist, 90022c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 90122c8a376SArnaldo Carvalho de Melo { 90232dcd021SJiri Olsa struct evsel *evsel; 90322c8a376SArnaldo Carvalho de Melo 904e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 90522c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 90622c8a376SArnaldo Carvalho de Melo } 90722c8a376SArnaldo Carvalho de Melo 90863503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist, 90922c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 91022c8a376SArnaldo Carvalho de Melo { 91132dcd021SJiri Olsa struct evsel *evsel; 91222c8a376SArnaldo Carvalho de Melo 913e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 91422c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 91522c8a376SArnaldo Carvalho de Melo } 91622c8a376SArnaldo Carvalho de Melo 91763503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 9180a102479SFrederic Weisbecker { 91932dcd021SJiri Olsa struct evsel *evsel; 920745cefc5SArnaldo Carvalho de Melo int err = 0; 9210a102479SFrederic Weisbecker 922e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 923745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 9240a102479SFrederic Weisbecker continue; 925745cefc5SArnaldo Carvalho de Melo 926d988d5eeSKan Liang /* 927d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 928d988d5eeSKan Liang * So evlist and evsel should always be same. 929d988d5eeSKan Liang */ 930a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 93123d4aad4SArnaldo Carvalho de Melo if (err) { 93223d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 933745cefc5SArnaldo Carvalho de Melo break; 9340a102479SFrederic Weisbecker } 93523d4aad4SArnaldo Carvalho de Melo } 9360a102479SFrederic Weisbecker 937745cefc5SArnaldo Carvalho de Melo return err; 938745cefc5SArnaldo Carvalho de Melo } 939745cefc5SArnaldo Carvalho de Melo 94063503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 941745cefc5SArnaldo Carvalho de Melo { 94232dcd021SJiri Olsa struct evsel *evsel; 943745cefc5SArnaldo Carvalho de Melo int err = 0; 944745cefc5SArnaldo Carvalho de Melo 94505cea449SArnaldo Carvalho de Melo if (filter == NULL) 94605cea449SArnaldo Carvalho de Melo return -1; 94705cea449SArnaldo Carvalho de Melo 948e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 9491fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 950fdf14720SWang Nan continue; 951fdf14720SWang Nan 95294ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 953745cefc5SArnaldo Carvalho de Melo if (err) 954745cefc5SArnaldo Carvalho de Melo break; 955745cefc5SArnaldo Carvalho de Melo } 956745cefc5SArnaldo Carvalho de Melo 957745cefc5SArnaldo Carvalho de Melo return err; 9580a102479SFrederic Weisbecker } 95974429964SFrederic Weisbecker 96053c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter) 96153c92f73SArnaldo Carvalho de Melo { 96253c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 96353c92f73SArnaldo Carvalho de Melo int err = 0; 96453c92f73SArnaldo Carvalho de Melo 96553c92f73SArnaldo Carvalho de Melo if (filter == NULL) 96653c92f73SArnaldo Carvalho de Melo return -1; 96753c92f73SArnaldo Carvalho de Melo 96853c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 96953c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 97053c92f73SArnaldo Carvalho de Melo continue; 97153c92f73SArnaldo Carvalho de Melo 97253c92f73SArnaldo Carvalho de Melo err = perf_evsel__append_tp_filter(evsel, filter); 97353c92f73SArnaldo Carvalho de Melo if (err) 97453c92f73SArnaldo Carvalho de Melo break; 97553c92f73SArnaldo Carvalho de Melo } 97653c92f73SArnaldo Carvalho de Melo 97753c92f73SArnaldo Carvalho de Melo return err; 97853c92f73SArnaldo Carvalho de Melo } 97953c92f73SArnaldo Carvalho de Melo 98005cea449SArnaldo Carvalho de Melo static char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 981cfd70a26SArnaldo Carvalho de Melo { 982cfd70a26SArnaldo Carvalho de Melo char *filter; 983be199adaSArnaldo Carvalho de Melo size_t i; 984cfd70a26SArnaldo Carvalho de Melo 985be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 986be199adaSArnaldo Carvalho de Melo if (i == 0) { 987be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 98805cea449SArnaldo Carvalho de Melo return NULL; 989be199adaSArnaldo Carvalho de Melo } else { 990be199adaSArnaldo Carvalho de Melo char *tmp; 991be199adaSArnaldo Carvalho de Melo 992be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 993be199adaSArnaldo Carvalho de Melo goto out_free; 994be199adaSArnaldo Carvalho de Melo 995be199adaSArnaldo Carvalho de Melo free(filter); 996be199adaSArnaldo Carvalho de Melo filter = tmp; 997be199adaSArnaldo Carvalho de Melo } 998be199adaSArnaldo Carvalho de Melo } 999cfd70a26SArnaldo Carvalho de Melo 100005cea449SArnaldo Carvalho de Melo return filter; 1001be199adaSArnaldo Carvalho de Melo out_free: 1002cfd70a26SArnaldo Carvalho de Melo free(filter); 100305cea449SArnaldo Carvalho de Melo return NULL; 100405cea449SArnaldo Carvalho de Melo } 100505cea449SArnaldo Carvalho de Melo 100605cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 100705cea449SArnaldo Carvalho de Melo { 100805cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 100905cea449SArnaldo Carvalho de Melo int ret = perf_evlist__set_tp_filter(evlist, filter); 101005cea449SArnaldo Carvalho de Melo 101105cea449SArnaldo Carvalho de Melo free(filter); 1012cfd70a26SArnaldo Carvalho de Melo return ret; 1013cfd70a26SArnaldo Carvalho de Melo } 1014cfd70a26SArnaldo Carvalho de Melo 101563503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1016be199adaSArnaldo Carvalho de Melo { 10177ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1018be199adaSArnaldo Carvalho de Melo } 1019be199adaSArnaldo Carvalho de Melo 10201827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 10211827ab5bSArnaldo Carvalho de Melo { 10221827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 10231827ab5bSArnaldo Carvalho de Melo int ret = perf_evlist__append_tp_filter(evlist, filter); 10241827ab5bSArnaldo Carvalho de Melo 10251827ab5bSArnaldo Carvalho de Melo free(filter); 10261827ab5bSArnaldo Carvalho de Melo return ret; 10271827ab5bSArnaldo Carvalho de Melo } 10281827ab5bSArnaldo Carvalho de Melo 10291827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 10301827ab5bSArnaldo Carvalho de Melo { 10311827ab5bSArnaldo Carvalho de Melo return perf_evlist__append_tp_filter_pids(evlist, 1, &pid); 10321827ab5bSArnaldo Carvalho de Melo } 10331827ab5bSArnaldo Carvalho de Melo 103463503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist) 103574429964SFrederic Weisbecker { 103632dcd021SJiri Olsa struct evsel *pos; 1037c2a70653SArnaldo Carvalho de Melo 10386484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 103975562573SAdrian Hunter return true; 104075562573SAdrian Hunter 104175562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 104275562573SAdrian Hunter return false; 104375562573SAdrian Hunter 1044e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 104575562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 104675562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1047c2a70653SArnaldo Carvalho de Melo return false; 104874429964SFrederic Weisbecker } 104974429964SFrederic Weisbecker 1050c2a70653SArnaldo Carvalho de Melo return true; 1051c2a70653SArnaldo Carvalho de Melo } 1052c2a70653SArnaldo Carvalho de Melo 105363503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist) 1054c2a70653SArnaldo Carvalho de Melo { 105532dcd021SJiri Olsa struct evsel *evsel; 105675562573SAdrian Hunter 105775562573SAdrian Hunter if (evlist->combined_sample_type) 105875562573SAdrian Hunter return evlist->combined_sample_type; 105975562573SAdrian Hunter 1060e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 10611fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 106275562573SAdrian Hunter 106375562573SAdrian Hunter return evlist->combined_sample_type; 106475562573SAdrian Hunter } 106575562573SAdrian Hunter 106663503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist) 106775562573SAdrian Hunter { 106875562573SAdrian Hunter evlist->combined_sample_type = 0; 106975562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1070c2a70653SArnaldo Carvalho de Melo } 1071c2a70653SArnaldo Carvalho de Melo 107263503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist) 107398df858eSAndi Kleen { 107432dcd021SJiri Olsa struct evsel *evsel; 107598df858eSAndi Kleen u64 branch_type = 0; 107698df858eSAndi Kleen 1077e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 10781fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 107998df858eSAndi Kleen return branch_type; 108098df858eSAndi Kleen } 108198df858eSAndi Kleen 108263503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 10839ede473cSJiri Olsa { 1084515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 10851fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 10861fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 10879ede473cSJiri Olsa 1088e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 10891fc632ceSJiri Olsa if (read_format != pos->core.attr.read_format) 10909ede473cSJiri Olsa return false; 10919ede473cSJiri Olsa } 10929ede473cSJiri Olsa 10939ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 10949ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 10959ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 10969ede473cSJiri Olsa return false; 10979ede473cSJiri Olsa } 10989ede473cSJiri Olsa 10999ede473cSJiri Olsa return true; 11009ede473cSJiri Olsa } 11019ede473cSJiri Olsa 110263503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 110381e36bffSArnaldo Carvalho de Melo { 1104515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 110581e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 110681e36bffSArnaldo Carvalho de Melo u64 sample_type; 110781e36bffSArnaldo Carvalho de Melo u16 size = 0; 110881e36bffSArnaldo Carvalho de Melo 11091fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 111081e36bffSArnaldo Carvalho de Melo goto out; 111181e36bffSArnaldo Carvalho de Melo 11121fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 111381e36bffSArnaldo Carvalho de Melo 111481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 111581e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 111681e36bffSArnaldo Carvalho de Melo 111781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 111881e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 111981e36bffSArnaldo Carvalho de Melo 112081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 112181e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 112281e36bffSArnaldo Carvalho de Melo 112381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 112481e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 112581e36bffSArnaldo Carvalho de Melo 112681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 112781e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 112875562573SAdrian Hunter 112975562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 113075562573SAdrian Hunter size += sizeof(data->id); 113181e36bffSArnaldo Carvalho de Melo out: 113281e36bffSArnaldo Carvalho de Melo return size; 113381e36bffSArnaldo Carvalho de Melo } 113481e36bffSArnaldo Carvalho de Melo 113563503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist) 1136c2a70653SArnaldo Carvalho de Melo { 1137515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1138c2a70653SArnaldo Carvalho de Melo 1139e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 11401fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1141c2a70653SArnaldo Carvalho de Melo return false; 1142c2a70653SArnaldo Carvalho de Melo } 1143c2a70653SArnaldo Carvalho de Melo 1144c2a70653SArnaldo Carvalho de Melo return true; 114574429964SFrederic Weisbecker } 114674429964SFrederic Weisbecker 114763503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist) 114874429964SFrederic Weisbecker { 1149515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11501fc632ceSJiri Olsa return first->core.attr.sample_id_all; 115174429964SFrederic Weisbecker } 115281cce8deSArnaldo Carvalho de Melo 115363503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 115432dcd021SJiri Olsa struct evsel *evsel) 115581cce8deSArnaldo Carvalho de Melo { 115681cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 115781cce8deSArnaldo Carvalho de Melo } 1158727ab04eSArnaldo Carvalho de Melo 1159750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1160a74b4b66SNamhyung Kim { 116132dcd021SJiri Olsa struct evsel *evsel; 1162a74b4b66SNamhyung Kim 1163475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 116488761fa1SJiri Olsa evsel__close(evsel); 1165a74b4b66SNamhyung Kim } 1166a74b4b66SNamhyung Kim 116763503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 11684112eb18SArnaldo Carvalho de Melo { 1169f854839bSJiri Olsa struct perf_cpu_map *cpus; 11709749b90eSJiri Olsa struct perf_thread_map *threads; 11714112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 11724112eb18SArnaldo Carvalho de Melo 11734112eb18SArnaldo Carvalho de Melo /* 11744112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 11754112eb18SArnaldo Carvalho de Melo * an all cpus map. 11764112eb18SArnaldo Carvalho de Melo * 11774112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 11784112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 11794112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 11804112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 11814112eb18SArnaldo Carvalho de Melo */ 11829c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 11838c0498b6SAdrian Hunter if (!cpus) 11844112eb18SArnaldo Carvalho de Melo goto out; 11854112eb18SArnaldo Carvalho de Melo 11864b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 11878c0498b6SAdrian Hunter if (!threads) 11888c0498b6SAdrian Hunter goto out_put; 11894112eb18SArnaldo Carvalho de Melo 1190453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 11914112eb18SArnaldo Carvalho de Melo out: 11924112eb18SArnaldo Carvalho de Melo return err; 11938c0498b6SAdrian Hunter out_put: 119438f01d8dSJiri Olsa perf_cpu_map__put(cpus); 11954112eb18SArnaldo Carvalho de Melo goto out; 11964112eb18SArnaldo Carvalho de Melo } 11974112eb18SArnaldo Carvalho de Melo 1198474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1199727ab04eSArnaldo Carvalho de Melo { 120032dcd021SJiri Olsa struct evsel *evsel; 1201a74b4b66SNamhyung Kim int err; 1202727ab04eSArnaldo Carvalho de Melo 12034112eb18SArnaldo Carvalho de Melo /* 12044112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 12054112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 12064112eb18SArnaldo Carvalho de Melo */ 120703617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 12084112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 12094112eb18SArnaldo Carvalho de Melo if (err < 0) 12104112eb18SArnaldo Carvalho de Melo goto out_err; 12114112eb18SArnaldo Carvalho de Melo } 12124112eb18SArnaldo Carvalho de Melo 1213733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1214733cd2feSAdrian Hunter 1215e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1216af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1217727ab04eSArnaldo Carvalho de Melo if (err < 0) 1218727ab04eSArnaldo Carvalho de Melo goto out_err; 1219727ab04eSArnaldo Carvalho de Melo } 1220727ab04eSArnaldo Carvalho de Melo 1221727ab04eSArnaldo Carvalho de Melo return 0; 1222727ab04eSArnaldo Carvalho de Melo out_err: 1223750b4edeSJiri Olsa evlist__close(evlist); 122441c21a68SNamhyung Kim errno = -err; 1225727ab04eSArnaldo Carvalho de Melo return err; 1226727ab04eSArnaldo Carvalho de Melo } 122735b9d88eSArnaldo Carvalho de Melo 122863503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 122955e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1230735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 123135b9d88eSArnaldo Carvalho de Melo { 123235b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 123335b9d88eSArnaldo Carvalho de Melo char bf; 123435b9d88eSArnaldo Carvalho de Melo 123535b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 123635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 123735b9d88eSArnaldo Carvalho de Melo return -1; 123835b9d88eSArnaldo Carvalho de Melo } 123935b9d88eSArnaldo Carvalho de Melo 124035b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 124135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 124235b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 124335b9d88eSArnaldo Carvalho de Melo } 124435b9d88eSArnaldo Carvalho de Melo 124535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 124635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 124735b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 124835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 124935b9d88eSArnaldo Carvalho de Melo } 125035b9d88eSArnaldo Carvalho de Melo 125135b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 12525f1c4225SArnaldo Carvalho de Melo int ret; 12535f1c4225SArnaldo Carvalho de Melo 1254119fa3c9SNamhyung Kim if (pipe_output) 125535b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 125635b9d88eSArnaldo Carvalho de Melo 12570817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 12580817df08SDavid Ahern 125935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 126035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 126135b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 126235b9d88eSArnaldo Carvalho de Melo 126335b9d88eSArnaldo Carvalho de Melo /* 126435b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 126535b9d88eSArnaldo Carvalho de Melo */ 126635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 126735b9d88eSArnaldo Carvalho de Melo 126835b9d88eSArnaldo Carvalho de Melo /* 126935b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 127035b9d88eSArnaldo Carvalho de Melo */ 12715f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 12725f1c4225SArnaldo Carvalho de Melo /* 12735f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 12745f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 12755f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 12765f1c4225SArnaldo Carvalho de Melo * 127720f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 12785f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 12795f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 12805f1c4225SArnaldo Carvalho de Melo * here. 12815f1c4225SArnaldo Carvalho de Melo */ 12825f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 12835f1c4225SArnaldo Carvalho de Melo if (ret == -1) 128435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 12855f1c4225SArnaldo Carvalho de Melo exit(ret); 12865f1c4225SArnaldo Carvalho de Melo } 128735b9d88eSArnaldo Carvalho de Melo 128835b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 128935b9d88eSArnaldo Carvalho de Melo 1290735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1291f33cbe72SArnaldo Carvalho de Melo union sigval val; 1292f33cbe72SArnaldo Carvalho de Melo 1293f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1294f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 129535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1296f33cbe72SArnaldo Carvalho de Melo } else 1297f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 129835b9d88eSArnaldo Carvalho de Melo exit(-1); 129935b9d88eSArnaldo Carvalho de Melo } 130035b9d88eSArnaldo Carvalho de Melo 1301735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1302735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1303735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1304735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1305735f7e0bSArnaldo Carvalho de Melo }; 1306735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1307735f7e0bSArnaldo Carvalho de Melo } 1308735f7e0bSArnaldo Carvalho de Melo 13091aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 131003617c22SJiri Olsa if (evlist->core.threads == NULL) { 13111aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 13121aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 13131aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 13141aaf63b1SArnaldo Carvalho de Melo } 131503617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 13161aaf63b1SArnaldo Carvalho de Melo } 131735b9d88eSArnaldo Carvalho de Melo 131835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 131935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 132035b9d88eSArnaldo Carvalho de Melo /* 132135b9d88eSArnaldo Carvalho de Melo * wait for child to settle 132235b9d88eSArnaldo Carvalho de Melo */ 132335b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 132435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 132535b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 132635b9d88eSArnaldo Carvalho de Melo } 132735b9d88eSArnaldo Carvalho de Melo 1328bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 132935b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 133035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 133135b9d88eSArnaldo Carvalho de Melo return 0; 133235b9d88eSArnaldo Carvalho de Melo 133335b9d88eSArnaldo Carvalho de Melo out_close_pipes: 133435b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 133535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 133635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 133735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 133835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 133935b9d88eSArnaldo Carvalho de Melo return -1; 134035b9d88eSArnaldo Carvalho de Melo } 134135b9d88eSArnaldo Carvalho de Melo 134263503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist) 134335b9d88eSArnaldo Carvalho de Melo { 134435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1345b3824404SDavid Ahern char bf = 0; 1346bcf3145fSNamhyung Kim int ret; 134735b9d88eSArnaldo Carvalho de Melo /* 134835b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 134935b9d88eSArnaldo Carvalho de Melo */ 1350bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1351bcf3145fSNamhyung Kim if (ret < 0) 1352e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1353bcf3145fSNamhyung Kim 1354bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1355bcf3145fSNamhyung Kim return ret; 135635b9d88eSArnaldo Carvalho de Melo } 135735b9d88eSArnaldo Carvalho de Melo 135835b9d88eSArnaldo Carvalho de Melo return 0; 135935b9d88eSArnaldo Carvalho de Melo } 1360cb0b29e0SArnaldo Carvalho de Melo 136163503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 13620807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1363cb0b29e0SArnaldo Carvalho de Melo { 136432dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 136575562573SAdrian Hunter 136675562573SAdrian Hunter if (!evsel) 136775562573SAdrian Hunter return -EFAULT; 13680807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1369cb0b29e0SArnaldo Carvalho de Melo } 137078f067b3SArnaldo Carvalho de Melo 137163503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 137201468120SJiri Olsa union perf_event *event, 137301468120SJiri Olsa u64 *timestamp) 137401468120SJiri Olsa { 137532dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 137601468120SJiri Olsa 137701468120SJiri Olsa if (!evsel) 137801468120SJiri Olsa return -EFAULT; 137901468120SJiri Olsa return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); 138001468120SJiri Olsa } 138101468120SJiri Olsa 138263503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist, 1383a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1384a8f23d8fSArnaldo Carvalho de Melo { 1385a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1386c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1387a8f23d8fSArnaldo Carvalho de Melo 1388a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1389a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1390a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1391a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1392a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1393a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1394a8f23d8fSArnaldo Carvalho de Melo 13951a47245dSAdrian Hunter value = perf_event_paranoid(); 1396a8f23d8fSArnaldo Carvalho de Melo 1397a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1398a8f23d8fSArnaldo Carvalho de Melo 1399a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1400a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1401a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1402a8f23d8fSArnaldo Carvalho de Melo } 1403a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14045229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1405a8f23d8fSArnaldo Carvalho de Melo 1406a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14075229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 14085229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1409a8f23d8fSArnaldo Carvalho de Melo break; 1410d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1411515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1412d9aade7fSArnaldo Carvalho de Melo int max_freq; 1413d9aade7fSArnaldo Carvalho de Melo 1414d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1415d9aade7fSArnaldo Carvalho de Melo goto out_default; 1416d9aade7fSArnaldo Carvalho de Melo 14171fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1418d9aade7fSArnaldo Carvalho de Melo goto out_default; 1419d9aade7fSArnaldo Carvalho de Melo 1420d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1421d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1422d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1423d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 14241fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1425d9aade7fSArnaldo Carvalho de Melo break; 1426d9aade7fSArnaldo Carvalho de Melo } 1427a8f23d8fSArnaldo Carvalho de Melo default: 1428d9aade7fSArnaldo Carvalho de Melo out_default: 1429a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1430a8f23d8fSArnaldo Carvalho de Melo break; 1431a8f23d8fSArnaldo Carvalho de Melo } 1432a8f23d8fSArnaldo Carvalho de Melo 1433a8f23d8fSArnaldo Carvalho de Melo return 0; 1434a8f23d8fSArnaldo Carvalho de Melo } 1435a025e4f0SAdrian Hunter 143663503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1437956fa571SArnaldo Carvalho de Melo { 1438c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1439f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1440956fa571SArnaldo Carvalho de Melo 1441956fa571SArnaldo Carvalho de Melo switch (err) { 1442956fa571SArnaldo Carvalho de Melo case EPERM: 1443e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1444e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1445e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1446956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1447e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1448e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1449e965bea1SArnaldo Carvalho de Melo 1450e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1451e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1452e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1453e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1454e965bea1SArnaldo Carvalho de Melo } 1455e965bea1SArnaldo Carvalho de Melo 1456e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1457e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1458956fa571SArnaldo Carvalho de Melo break; 1459956fa571SArnaldo Carvalho de Melo default: 1460956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1461956fa571SArnaldo Carvalho de Melo break; 1462956fa571SArnaldo Carvalho de Melo } 1463956fa571SArnaldo Carvalho de Melo 1464956fa571SArnaldo Carvalho de Melo return 0; 1465956fa571SArnaldo Carvalho de Melo } 1466956fa571SArnaldo Carvalho de Melo 146763503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 146832dcd021SJiri Olsa struct evsel *move_evsel) 1469a025e4f0SAdrian Hunter { 147032dcd021SJiri Olsa struct evsel *evsel, *n; 1471a025e4f0SAdrian Hunter LIST_HEAD(move); 1472a025e4f0SAdrian Hunter 1473515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1474a025e4f0SAdrian Hunter return; 1475a025e4f0SAdrian Hunter 1476e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1477a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1478b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1479a025e4f0SAdrian Hunter } 1480a025e4f0SAdrian Hunter 1481ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1482a025e4f0SAdrian Hunter } 148360b0896cSAdrian Hunter 148463503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist, 148532dcd021SJiri Olsa struct evsel *tracking_evsel) 148660b0896cSAdrian Hunter { 148732dcd021SJiri Olsa struct evsel *evsel; 148860b0896cSAdrian Hunter 148960b0896cSAdrian Hunter if (tracking_evsel->tracking) 149060b0896cSAdrian Hunter return; 149160b0896cSAdrian Hunter 1492e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 149360b0896cSAdrian Hunter if (evsel != tracking_evsel) 149460b0896cSAdrian Hunter evsel->tracking = false; 149560b0896cSAdrian Hunter } 149660b0896cSAdrian Hunter 149760b0896cSAdrian Hunter tracking_evsel->tracking = true; 149860b0896cSAdrian Hunter } 14997630b3e2SWang Nan 150032dcd021SJiri Olsa struct evsel * 150163503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist, 15027630b3e2SWang Nan const char *str) 15037630b3e2SWang Nan { 150432dcd021SJiri Olsa struct evsel *evsel; 15057630b3e2SWang Nan 1506e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 15077630b3e2SWang Nan if (!evsel->name) 15087630b3e2SWang Nan continue; 15097630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 15107630b3e2SWang Nan return evsel; 15117630b3e2SWang Nan } 15127630b3e2SWang Nan 15137630b3e2SWang Nan return NULL; 15147630b3e2SWang Nan } 151554cc54deSWang Nan 151663503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 151754cc54deSWang Nan enum bkw_mmap_state state) 151854cc54deSWang Nan { 151954cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 152054cc54deSWang Nan enum action { 152154cc54deSWang Nan NONE, 152254cc54deSWang Nan PAUSE, 152354cc54deSWang Nan RESUME, 152454cc54deSWang Nan } action = NONE; 152554cc54deSWang Nan 15260b72d69aSWang Nan if (!evlist->overwrite_mmap) 152754cc54deSWang Nan return; 152854cc54deSWang Nan 152954cc54deSWang Nan switch (old_state) { 153054cc54deSWang Nan case BKW_MMAP_NOTREADY: { 153154cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1532dd8bd53aSLuis de Bethencourt goto state_err; 153354cc54deSWang Nan break; 153454cc54deSWang Nan } 153554cc54deSWang Nan case BKW_MMAP_RUNNING: { 153654cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 153754cc54deSWang Nan goto state_err; 153854cc54deSWang Nan action = PAUSE; 153954cc54deSWang Nan break; 154054cc54deSWang Nan } 154154cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 154254cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 154354cc54deSWang Nan goto state_err; 154454cc54deSWang Nan break; 154554cc54deSWang Nan } 154654cc54deSWang Nan case BKW_MMAP_EMPTY: { 154754cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 154854cc54deSWang Nan goto state_err; 154954cc54deSWang Nan action = RESUME; 155054cc54deSWang Nan break; 155154cc54deSWang Nan } 155254cc54deSWang Nan default: 155354cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 155454cc54deSWang Nan } 155554cc54deSWang Nan 155654cc54deSWang Nan evlist->bkw_mmap_state = state; 155754cc54deSWang Nan 155854cc54deSWang Nan switch (action) { 155954cc54deSWang Nan case PAUSE: 156054cc54deSWang Nan perf_evlist__pause(evlist); 156154cc54deSWang Nan break; 156254cc54deSWang Nan case RESUME: 156354cc54deSWang Nan perf_evlist__resume(evlist); 156454cc54deSWang Nan break; 156554cc54deSWang Nan case NONE: 156654cc54deSWang Nan default: 156754cc54deSWang Nan break; 156854cc54deSWang Nan } 156954cc54deSWang Nan 157054cc54deSWang Nan state_err: 157154cc54deSWang Nan return; 157254cc54deSWang Nan } 157307d6f446SArnaldo Carvalho de Melo 157463503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 157507d6f446SArnaldo Carvalho de Melo { 157632dcd021SJiri Olsa struct evsel *evsel; 157707d6f446SArnaldo Carvalho de Melo 157807d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 15791fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 158007d6f446SArnaldo Carvalho de Melo return false; 158107d6f446SArnaldo Carvalho de Melo } 158207d6f446SArnaldo Carvalho de Melo 158307d6f446SArnaldo Carvalho de Melo return true; 158407d6f446SArnaldo Carvalho de Melo } 1585e2bdbe80SJin Yao 1586e2bdbe80SJin Yao /* 1587e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1588e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1589e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1590e2bdbe80SJin Yao */ 159163503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1592e2bdbe80SJin Yao { 1593e2bdbe80SJin Yao if (!evlist->nr_groups) { 1594515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1595e2bdbe80SJin Yao 1596e2bdbe80SJin Yao perf_evlist__set_leader(evlist); 1597e2bdbe80SJin Yao leader->forced_leader = true; 1598e2bdbe80SJin Yao } 1599e2bdbe80SJin Yao } 1600c3537fc2SAndi Kleen 160163503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 160232dcd021SJiri Olsa struct evsel *evsel) 1603c3537fc2SAndi Kleen { 160432dcd021SJiri Olsa struct evsel *c2, *leader; 1605c3537fc2SAndi Kleen bool is_open = true; 1606c3537fc2SAndi Kleen 1607c3537fc2SAndi Kleen leader = evsel->leader; 1608c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 16095643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1610c3537fc2SAndi Kleen 1611c3537fc2SAndi Kleen /* 1612c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1613c3537fc2SAndi Kleen * include the first entry. 1614c3537fc2SAndi Kleen */ 1615c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1616c3537fc2SAndi Kleen if (c2 == evsel) 1617c3537fc2SAndi Kleen is_open = false; 1618c3537fc2SAndi Kleen if (c2->leader == leader) { 1619c3537fc2SAndi Kleen if (is_open) 16207834fa94SAndi Kleen perf_evsel__close(&evsel->core); 1621c3537fc2SAndi Kleen c2->leader = c2; 16225643b1a5SJiri Olsa c2->core.nr_members = 0; 1623c3537fc2SAndi Kleen } 1624c3537fc2SAndi Kleen } 1625c3537fc2SAndi Kleen return leader; 1626c3537fc2SAndi Kleen } 1627657ee553SSong Liu 162863503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist, 1629657ee553SSong Liu struct perf_event_attr *attr, 1630657ee553SSong Liu perf_evsel__sb_cb_t cb, 1631657ee553SSong Liu void *data) 1632657ee553SSong Liu { 163332dcd021SJiri Olsa struct evsel *evsel; 1634657ee553SSong Liu bool new_evlist = (*evlist) == NULL; 1635657ee553SSong Liu 1636657ee553SSong Liu if (*evlist == NULL) 16370f98b11cSJiri Olsa *evlist = evlist__new(); 1638657ee553SSong Liu if (*evlist == NULL) 1639657ee553SSong Liu return -1; 1640657ee553SSong Liu 1641657ee553SSong Liu if (!attr->sample_id_all) { 1642657ee553SSong Liu pr_warning("enabling sample_id_all for all side band events\n"); 1643657ee553SSong Liu attr->sample_id_all = 1; 1644657ee553SSong Liu } 1645657ee553SSong Liu 16466484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries); 1647657ee553SSong Liu if (!evsel) 1648657ee553SSong Liu goto out_err; 1649657ee553SSong Liu 1650657ee553SSong Liu evsel->side_band.cb = cb; 1651657ee553SSong Liu evsel->side_band.data = data; 1652a1cf3a75SJiri Olsa evlist__add(*evlist, evsel); 1653657ee553SSong Liu return 0; 1654657ee553SSong Liu 1655657ee553SSong Liu out_err: 1656657ee553SSong Liu if (new_evlist) { 1657c12995a5SJiri Olsa evlist__delete(*evlist); 1658657ee553SSong Liu *evlist = NULL; 1659657ee553SSong Liu } 1660657ee553SSong Liu return -1; 1661657ee553SSong Liu } 1662657ee553SSong Liu 1663657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg) 1664657ee553SSong Liu { 166563503dbaSJiri Olsa struct evlist *evlist = arg; 1666657ee553SSong Liu bool draining = false; 1667adc6257cSJiri Olsa int i, done = 0; 1668b397f846SArnaldo Carvalho de Melo /* 1669b397f846SArnaldo Carvalho de Melo * In order to read symbols from other namespaces perf to needs to call 1670b397f846SArnaldo Carvalho de Melo * setns(2). This isn't permitted if the struct_fs has multiple users. 1671b397f846SArnaldo Carvalho de Melo * unshare(2) the fs so that we may continue to setns into namespaces 1672b397f846SArnaldo Carvalho de Melo * that we're observing when, for instance, reading the build-ids at 1673b397f846SArnaldo Carvalho de Melo * the end of a 'perf record' session. 1674b397f846SArnaldo Carvalho de Melo */ 1675b397f846SArnaldo Carvalho de Melo unshare(CLONE_FS); 1676657ee553SSong Liu 1677adc6257cSJiri Olsa while (!done) { 1678adc6257cSJiri Olsa bool got_data = false; 1679adc6257cSJiri Olsa 1680adc6257cSJiri Olsa if (evlist->thread.done) 1681657ee553SSong Liu draining = true; 1682657ee553SSong Liu 1683657ee553SSong Liu if (!draining) 168480ab2987SJiri Olsa evlist__poll(evlist, 1000); 1685657ee553SSong Liu 1686c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 1687a5830532SJiri Olsa struct mmap *map = &evlist->mmap[i]; 1688657ee553SSong Liu union perf_event *event; 1689657ee553SSong Liu 16907c4d4182SJiri Olsa if (perf_mmap__read_init(&map->core)) 1691657ee553SSong Liu continue; 1692151ed5d7SJiri Olsa while ((event = perf_mmap__read_event(&map->core)) != NULL) { 169332dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1694657ee553SSong Liu 1695657ee553SSong Liu if (evsel && evsel->side_band.cb) 1696657ee553SSong Liu evsel->side_band.cb(event, evsel->side_band.data); 1697657ee553SSong Liu else 1698657ee553SSong Liu pr_warning("cannot locate proper evsel for the side band event\n"); 1699657ee553SSong Liu 17007728fa0cSJiri Olsa perf_mmap__consume(&map->core); 1701adc6257cSJiri Olsa got_data = true; 1702657ee553SSong Liu } 170332fdc2caSJiri Olsa perf_mmap__read_done(&map->core); 1704657ee553SSong Liu } 1705adc6257cSJiri Olsa 1706adc6257cSJiri Olsa if (draining && !got_data) 1707adc6257cSJiri Olsa break; 1708657ee553SSong Liu } 1709657ee553SSong Liu return NULL; 1710657ee553SSong Liu } 1711657ee553SSong Liu 171263503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist, 1713657ee553SSong Liu struct target *target) 1714657ee553SSong Liu { 171532dcd021SJiri Olsa struct evsel *counter; 1716657ee553SSong Liu 1717657ee553SSong Liu if (!evlist) 1718657ee553SSong Liu return 0; 1719657ee553SSong Liu 1720657ee553SSong Liu if (perf_evlist__create_maps(evlist, target)) 1721657ee553SSong Liu goto out_delete_evlist; 1722657ee553SSong Liu 1723657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1724f72f901dSJiri Olsa if (evsel__open(counter, evlist->core.cpus, 172503617c22SJiri Olsa evlist->core.threads) < 0) 1726657ee553SSong Liu goto out_delete_evlist; 1727657ee553SSong Liu } 1728657ee553SSong Liu 17299521b5f2SJiri Olsa if (evlist__mmap(evlist, UINT_MAX)) 1730657ee553SSong Liu goto out_delete_evlist; 1731657ee553SSong Liu 1732657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1733ec7f24efSJiri Olsa if (evsel__enable(counter)) 1734657ee553SSong Liu goto out_delete_evlist; 1735657ee553SSong Liu } 1736657ee553SSong Liu 1737657ee553SSong Liu evlist->thread.done = 0; 1738657ee553SSong Liu if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist)) 1739657ee553SSong Liu goto out_delete_evlist; 1740657ee553SSong Liu 1741657ee553SSong Liu return 0; 1742657ee553SSong Liu 1743657ee553SSong Liu out_delete_evlist: 1744c12995a5SJiri Olsa evlist__delete(evlist); 1745657ee553SSong Liu evlist = NULL; 1746657ee553SSong Liu return -1; 1747657ee553SSong Liu } 1748657ee553SSong Liu 174963503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist) 1750657ee553SSong Liu { 1751657ee553SSong Liu if (!evlist) 1752657ee553SSong Liu return; 1753657ee553SSong Liu evlist->thread.done = 1; 1754657ee553SSong Liu pthread_join(evlist->thread.th, NULL); 1755c12995a5SJiri Olsa evlist__delete(evlist); 1756657ee553SSong Liu } 1757