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 { 434a5830532SJiri Olsa struct mmap *map = fda->priv[fd].ptr; 435e4b356b5SArnaldo Carvalho de Melo 4364876075bSWang Nan if (map) 43780e53d11SJiri Olsa perf_mmap__put(&map->core); 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 static void perf_evlist__set_sid_idx(struct evlist *evlist, 45232dcd021SJiri Olsa struct evsel *evsel, int idx, int cpu, 4533c659eedSAdrian Hunter int thread) 4543c659eedSAdrian Hunter { 4553c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 4563c659eedSAdrian Hunter sid->idx = idx; 457f72f901dSJiri Olsa if (evlist->core.cpus && cpu >= 0) 458f72f901dSJiri Olsa sid->cpu = evlist->core.cpus->map[cpu]; 4593c659eedSAdrian Hunter else 4603c659eedSAdrian Hunter sid->cpu = -1; 461648b5af3SJiri Olsa if (!evsel->core.system_wide && evlist->core.threads && thread >= 0) 462a2f354e3SJiri Olsa sid->tid = perf_thread_map__pid(evlist->core.threads, thread); 4633c659eedSAdrian Hunter else 4643c659eedSAdrian Hunter sid->tid = -1; 4653c659eedSAdrian Hunter } 4663c659eedSAdrian Hunter 46763503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 46870db7533SArnaldo Carvalho de Melo { 46970db7533SArnaldo Carvalho de Melo struct hlist_head *head; 47070db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 47170db7533SArnaldo Carvalho de Melo int hash; 47270db7533SArnaldo Carvalho de Melo 47370db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 4741d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 47570db7533SArnaldo Carvalho de Melo 476b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 47770db7533SArnaldo Carvalho de Melo if (sid->id == id) 478932a3594SJiri Olsa return sid; 479932a3594SJiri Olsa 480932a3594SJiri Olsa return NULL; 481932a3594SJiri Olsa } 482932a3594SJiri Olsa 48363503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 484932a3594SJiri Olsa { 485932a3594SJiri Olsa struct perf_sample_id *sid; 486932a3594SJiri Olsa 4876484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 488515dbe48SJiri Olsa return evlist__first(evlist); 489932a3594SJiri Olsa 490932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 491932a3594SJiri Olsa if (sid) 49270c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 49330e68bccSNamhyung Kim 49430e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 495515dbe48SJiri Olsa return evlist__first(evlist); 49630e68bccSNamhyung Kim 49770db7533SArnaldo Carvalho de Melo return NULL; 49870db7533SArnaldo Carvalho de Melo } 49904391debSArnaldo Carvalho de Melo 50063503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 501dddcf6abSAdrian Hunter u64 id) 502dddcf6abSAdrian Hunter { 503dddcf6abSAdrian Hunter struct perf_sample_id *sid; 504dddcf6abSAdrian Hunter 505dddcf6abSAdrian Hunter if (!id) 506dddcf6abSAdrian Hunter return NULL; 507dddcf6abSAdrian Hunter 508dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 509dddcf6abSAdrian Hunter if (sid) 51070c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 511dddcf6abSAdrian Hunter 512dddcf6abSAdrian Hunter return NULL; 513dddcf6abSAdrian Hunter } 514dddcf6abSAdrian Hunter 51563503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist, 51675562573SAdrian Hunter union perf_event *event, u64 *id) 51775562573SAdrian Hunter { 518b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 51975562573SAdrian Hunter ssize_t n; 52075562573SAdrian Hunter 52175562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 52275562573SAdrian Hunter 52375562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 52475562573SAdrian Hunter if (evlist->id_pos >= n) 52575562573SAdrian Hunter return -1; 52675562573SAdrian Hunter *id = array[evlist->id_pos]; 52775562573SAdrian Hunter } else { 52875562573SAdrian Hunter if (evlist->is_pos > n) 52975562573SAdrian Hunter return -1; 53075562573SAdrian Hunter n -= evlist->is_pos; 53175562573SAdrian Hunter *id = array[n]; 53275562573SAdrian Hunter } 53375562573SAdrian Hunter return 0; 53475562573SAdrian Hunter } 53575562573SAdrian Hunter 53663503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 53775562573SAdrian Hunter union perf_event *event) 53875562573SAdrian Hunter { 539515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 54075562573SAdrian Hunter struct hlist_head *head; 54175562573SAdrian Hunter struct perf_sample_id *sid; 54275562573SAdrian Hunter int hash; 54375562573SAdrian Hunter u64 id; 54475562573SAdrian Hunter 5456484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 54698be6966SAdrian Hunter return first; 54798be6966SAdrian Hunter 5481fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 54998be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 55098be6966SAdrian Hunter return first; 55175562573SAdrian Hunter 55275562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 55375562573SAdrian Hunter return NULL; 55475562573SAdrian Hunter 55575562573SAdrian Hunter /* Synthesized events have an id of zero */ 55675562573SAdrian Hunter if (!id) 55798be6966SAdrian Hunter return first; 55875562573SAdrian Hunter 55975562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5601d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 56175562573SAdrian Hunter 56275562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 56375562573SAdrian Hunter if (sid->id == id) 56470c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 56575562573SAdrian Hunter } 56675562573SAdrian Hunter return NULL; 56775562573SAdrian Hunter } 56875562573SAdrian Hunter 56963503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value) 57065aea233SWang Nan { 57165aea233SWang Nan int i; 57265aea233SWang Nan 5730b72d69aSWang Nan if (!evlist->overwrite_mmap) 574078c3386SWang Nan return 0; 575078c3386SWang Nan 576c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 5772cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 57865aea233SWang Nan int err; 57965aea233SWang Nan 58065aea233SWang Nan if (fd < 0) 58165aea233SWang Nan continue; 58265aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 58365aea233SWang Nan if (err) 58465aea233SWang Nan return err; 58565aea233SWang Nan } 58665aea233SWang Nan return 0; 58765aea233SWang Nan } 58865aea233SWang Nan 58963503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist) 59065aea233SWang Nan { 59165aea233SWang Nan return perf_evlist__set_paused(evlist, true); 59265aea233SWang Nan } 59365aea233SWang Nan 59463503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist) 59565aea233SWang Nan { 59665aea233SWang Nan return perf_evlist__set_paused(evlist, false); 59765aea233SWang Nan } 59865aea233SWang Nan 599db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 600f8a95309SArnaldo Carvalho de Melo { 601aece948fSArnaldo Carvalho de Melo int i; 602f8a95309SArnaldo Carvalho de Melo 603b2cb615dSWang Nan if (evlist->mmap) 604c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 60580e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 606b2cb615dSWang Nan 6070b72d69aSWang Nan if (evlist->overwrite_mmap) 608c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 60980e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 610a1f72618SWang Nan } 611aece948fSArnaldo Carvalho de Melo 612db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 613a1f72618SWang Nan { 614db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 61504662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6160b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 617f8a95309SArnaldo Carvalho de Melo } 618f8a95309SArnaldo Carvalho de Melo 61980e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 62080e53d11SJiri Olsa { 62180e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 62280e53d11SJiri Olsa 62380e53d11SJiri Olsa mmap__munmap(m); 62480e53d11SJiri Olsa } 62580e53d11SJiri Olsa 626d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 6272c5f6d87SKan Liang bool overwrite) 628f8a95309SArnaldo Carvalho de Melo { 629d4c6fb36SWang Nan int i; 630a5830532SJiri Olsa struct mmap *map; 631d4c6fb36SWang Nan 632c976ee11SJiri Olsa evlist->core.nr_mmaps = perf_cpu_map__nr(evlist->core.cpus); 633315c0a1fSJiri Olsa if (perf_cpu_map__empty(evlist->core.cpus)) 634c976ee11SJiri Olsa evlist->core.nr_mmaps = perf_thread_map__nr(evlist->core.threads); 635c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 6368db6d6b1SWang Nan if (!map) 6378db6d6b1SWang Nan return NULL; 638946ae1d4SWang Nan 639c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6404738ca30SArnaldo Carvalho de Melo /* 6414738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 6426afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 6434738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 6444738ca30SArnaldo Carvalho de Melo * dropped. 6454738ca30SArnaldo Carvalho de Melo * 6464738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 6474738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 6484738ca30SArnaldo Carvalho de Melo */ 64980e53d11SJiri Olsa perf_mmap__init(&map[i].core, overwrite, perf_mmap__unmap_cb); 6504738ca30SArnaldo Carvalho de Melo } 651353120b4SJiri Olsa 6528db6d6b1SWang Nan return map; 653f8a95309SArnaldo Carvalho de Melo } 654f8a95309SArnaldo Carvalho de Melo 655f3058a1cSWang Nan static bool 65663503dbaSJiri Olsa perf_evlist__should_poll(struct evlist *evlist __maybe_unused, 65732dcd021SJiri Olsa struct evsel *evsel) 658f3058a1cSWang Nan { 6591fc632ceSJiri Olsa if (evsel->core.attr.write_backward) 660f3058a1cSWang Nan return false; 661f3058a1cSWang Nan return true; 662f3058a1cSWang Nan } 663f3058a1cSWang Nan 6649521b5f2SJiri Olsa static int evlist__mmap_per_evsel(struct evlist *evlist, int idx, 6659f21b815SMark Rutland struct mmap_params *mp, int cpu_idx, 6660b72d69aSWang Nan int thread, int *_output, int *_output_overwrite) 667aece948fSArnaldo Carvalho de Melo { 66832dcd021SJiri Olsa struct evsel *evsel; 669f3058a1cSWang Nan int revent; 670f72f901dSJiri Olsa int evlist_cpu = cpu_map__cpu(evlist->core.cpus, cpu_idx); 67104e21314SAdrian Hunter 672e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 673a5830532SJiri Olsa struct mmap *maps = evlist->mmap; 674078c3386SWang Nan int *output = _output; 675bf8e8f4bSAdrian Hunter int fd; 6769f21b815SMark Rutland int cpu; 677bf8e8f4bSAdrian Hunter 678e440979fSJiri Olsa mp->core.prot = PROT_READ | PROT_WRITE; 6791fc632ceSJiri Olsa if (evsel->core.attr.write_backward) { 6800b72d69aSWang Nan output = _output_overwrite; 6810b72d69aSWang Nan maps = evlist->overwrite_mmap; 682078c3386SWang Nan 683078c3386SWang Nan if (!maps) { 684d50cf361SJiri Olsa maps = evlist__alloc_mmap(evlist, true); 685078c3386SWang Nan if (!maps) 686078c3386SWang Nan return -1; 6870b72d69aSWang Nan evlist->overwrite_mmap = maps; 68854cc54deSWang Nan if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 68954cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 690078c3386SWang Nan } 691e440979fSJiri Olsa mp->core.prot &= ~PROT_WRITE; 692078c3386SWang Nan } 693f3058a1cSWang Nan 694648b5af3SJiri Olsa if (evsel->core.system_wide && thread) 695bf8e8f4bSAdrian Hunter continue; 696bf8e8f4bSAdrian Hunter 697b4df75deSJiri Olsa cpu = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu); 6989f21b815SMark Rutland if (cpu == -1) 6999f21b815SMark Rutland continue; 7009f21b815SMark Rutland 701bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 70204e21314SAdrian Hunter 70304e21314SAdrian Hunter if (*output == -1) { 70404e21314SAdrian Hunter *output = fd; 705078c3386SWang Nan 70632c261c0SJiri Olsa if (mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0) 70704e21314SAdrian Hunter return -1; 70804e21314SAdrian Hunter } else { 70904e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 71004e21314SAdrian Hunter return -1; 71182396986SArnaldo Carvalho de Melo 712e75710f0SJiri Olsa perf_mmap__get(&maps[idx].core); 71304e21314SAdrian Hunter } 71404e21314SAdrian Hunter 715f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 716f3058a1cSWang Nan 717f90d194aSAdrian Hunter /* 718f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 719f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 720f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 721f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 722f90d194aSAdrian Hunter * Therefore don't add it for polling. 723f90d194aSAdrian Hunter */ 724648b5af3SJiri Olsa if (!evsel->core.system_wide && 725f4009e7bSJiri Olsa perf_evlist__add_pollfd(&evlist->core, fd, &maps[idx], revent) < 0) { 72680e53d11SJiri Olsa perf_mmap__put(&maps[idx].core); 727ad6765ddSArnaldo Carvalho de Melo return -1; 72882396986SArnaldo Carvalho de Melo } 729033fa713SArnaldo Carvalho de Melo 7301fc632ceSJiri Olsa if (evsel->core.attr.read_format & PERF_FORMAT_ID) { 731d5a99483SJiri Olsa if (perf_evlist__id_add_fd(&evlist->core, &evsel->core, cpu, thread, 7323c659eedSAdrian Hunter fd) < 0) 73304e21314SAdrian Hunter return -1; 7343c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 7353c659eedSAdrian Hunter thread); 7363c659eedSAdrian Hunter } 73704e21314SAdrian Hunter } 73804e21314SAdrian Hunter 73904e21314SAdrian Hunter return 0; 74004e21314SAdrian Hunter } 74104e21314SAdrian Hunter 7429521b5f2SJiri Olsa static int evlist__mmap_per_cpu(struct evlist *evlist, 743a8a8f3ebSAdrian Hunter struct mmap_params *mp) 74404e21314SAdrian Hunter { 745aece948fSArnaldo Carvalho de Melo int cpu, thread; 7466549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 747a2f354e3SJiri Olsa int nr_threads = perf_thread_map__nr(evlist->core.threads); 748aece948fSArnaldo Carvalho de Melo 749e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 750b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 751aece948fSArnaldo Carvalho de Melo int output = -1; 7520b72d69aSWang Nan int output_overwrite = -1; 753aece948fSArnaldo Carvalho de Melo 754718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 755718c602dSAdrian Hunter true); 756718c602dSAdrian Hunter 757b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 7589521b5f2SJiri Olsa if (evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 7590b72d69aSWang Nan thread, &output, &output_overwrite)) 760aece948fSArnaldo Carvalho de Melo goto out_unmap; 761aece948fSArnaldo Carvalho de Melo } 762aece948fSArnaldo Carvalho de Melo } 763aece948fSArnaldo Carvalho de Melo 764aece948fSArnaldo Carvalho de Melo return 0; 765aece948fSArnaldo Carvalho de Melo 766aece948fSArnaldo Carvalho de Melo out_unmap: 767db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 768aece948fSArnaldo Carvalho de Melo return -1; 769aece948fSArnaldo Carvalho de Melo } 770aece948fSArnaldo Carvalho de Melo 7719521b5f2SJiri Olsa static int evlist__mmap_per_thread(struct evlist *evlist, 772a8a8f3ebSAdrian Hunter struct mmap_params *mp) 773aece948fSArnaldo Carvalho de Melo { 774aece948fSArnaldo Carvalho de Melo int thread; 775a2f354e3SJiri Olsa int nr_threads = perf_thread_map__nr(evlist->core.threads); 776aece948fSArnaldo Carvalho de Melo 777e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 778b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 779aece948fSArnaldo Carvalho de Melo int output = -1; 7800b72d69aSWang Nan int output_overwrite = -1; 781aece948fSArnaldo Carvalho de Melo 782718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 783718c602dSAdrian Hunter false); 784718c602dSAdrian Hunter 7859521b5f2SJiri Olsa if (evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 7860b72d69aSWang Nan &output, &output_overwrite)) 787aece948fSArnaldo Carvalho de Melo goto out_unmap; 788aece948fSArnaldo Carvalho de Melo } 789aece948fSArnaldo Carvalho de Melo 790aece948fSArnaldo Carvalho de Melo return 0; 791aece948fSArnaldo Carvalho de Melo 792aece948fSArnaldo Carvalho de Melo out_unmap: 793db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 794aece948fSArnaldo Carvalho de Melo return -1; 795aece948fSArnaldo Carvalho de Melo } 796aece948fSArnaldo Carvalho de Melo 797f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 798994a1f78SJiri Olsa { 799f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 8008185e881SArnaldo Carvalho de Melo int max; 8018185e881SArnaldo Carvalho de Melo 8028185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 8038185e881SArnaldo Carvalho de Melo /* 8048185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 8058185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 8068185e881SArnaldo Carvalho de Melo * die yet... 8078185e881SArnaldo Carvalho de Melo */ 8088185e881SArnaldo Carvalho de Melo max = 512; 8098185e881SArnaldo Carvalho de Melo } else { 8108185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 8118185e881SArnaldo Carvalho de Melo } 8128185e881SArnaldo Carvalho de Melo 8138185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 8141be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 8151be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 816f5e7150cSArnaldo Carvalho de Melo 817f5e7150cSArnaldo Carvalho de Melo return pages; 818f5e7150cSArnaldo Carvalho de Melo } 819f5e7150cSArnaldo Carvalho de Melo 8209521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 821f5e7150cSArnaldo Carvalho de Melo { 822f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 823f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 824f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 825994a1f78SJiri Olsa return 0; 826994a1f78SJiri Olsa 827994a1f78SJiri Olsa return (pages + 1) * page_size; 828994a1f78SJiri Olsa } 829994a1f78SJiri Olsa 83033c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 83133c2dcfdSDavid Ahern unsigned long max) 832994a1f78SJiri Olsa { 8332fbe4abeSAdrian Hunter unsigned long pages, val; 83427050f53SJiri Olsa static struct parse_tag tags[] = { 83527050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 83627050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 83727050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 83827050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 83927050f53SJiri Olsa { .tag = 0 }, 84027050f53SJiri Olsa }; 841994a1f78SJiri Olsa 8428973504bSDavid Ahern if (str == NULL) 84333c2dcfdSDavid Ahern return -EINVAL; 8448973504bSDavid Ahern 84527050f53SJiri Olsa val = parse_tag_value(str, tags); 8462fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 84727050f53SJiri Olsa /* we got file size value */ 84827050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 84927050f53SJiri Olsa } else { 85027050f53SJiri Olsa /* we got pages count value */ 85127050f53SJiri Olsa char *eptr; 852994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 85333c2dcfdSDavid Ahern if (*eptr != '\0') 85433c2dcfdSDavid Ahern return -EINVAL; 85527050f53SJiri Olsa } 856994a1f78SJiri Olsa 8572bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 85833c2dcfdSDavid Ahern /* leave number of pages at 0 */ 8591dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 8609808143bSJiri Olsa char buf[100]; 8619808143bSJiri Olsa 86233c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 86391529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 8641dbfa938SAdrian Hunter if (!pages) 8651dbfa938SAdrian Hunter return -EINVAL; 8669808143bSJiri Olsa 8679808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 8689808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 8699808143bSJiri Olsa buf, pages); 8702fbe4abeSAdrian Hunter } 8712fbe4abeSAdrian Hunter 87233c2dcfdSDavid Ahern if (pages > max) 87333c2dcfdSDavid Ahern return -EINVAL; 87433c2dcfdSDavid Ahern 87533c2dcfdSDavid Ahern return pages; 87633c2dcfdSDavid Ahern } 87733c2dcfdSDavid Ahern 878e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 87933c2dcfdSDavid Ahern { 88033c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 88133c2dcfdSDavid Ahern long pages; 88233c2dcfdSDavid Ahern 883f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 88433c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 88533c2dcfdSDavid Ahern 88633c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 88733c2dcfdSDavid Ahern if (pages < 0) { 88833c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 889994a1f78SJiri Olsa return -1; 890994a1f78SJiri Olsa } 891994a1f78SJiri Olsa 892994a1f78SJiri Olsa *mmap_pages = pages; 893994a1f78SJiri Olsa return 0; 894994a1f78SJiri Olsa } 895994a1f78SJiri Olsa 896e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 897e9db1310SAdrian Hunter int unset __maybe_unused) 898e9db1310SAdrian Hunter { 899e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 900e9db1310SAdrian Hunter } 901e9db1310SAdrian Hunter 902c83fa7f2SAdrian Hunter /** 9039521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 904c83fa7f2SAdrian Hunter * @evlist: list of events 905c83fa7f2SAdrian Hunter * @pages: map length in pages 906c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 907718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 908718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 909f8a95309SArnaldo Carvalho de Melo * 910c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 9119521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 912c83fa7f2SAdrian Hunter * automatically. 913f8a95309SArnaldo Carvalho de Melo * 914718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 915718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 916718c602dSAdrian Hunter * 917c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 918f8a95309SArnaldo Carvalho de Melo */ 9199521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 9207a276ff6SWang Nan unsigned int auxtrace_pages, 92151255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 92251255a8aSAlexey Budankov int comp_level) 923f8a95309SArnaldo Carvalho de Melo { 92432dcd021SJiri Olsa struct evsel *evsel; 925f72f901dSJiri Olsa const struct perf_cpu_map *cpus = evlist->core.cpus; 92603617c22SJiri Olsa const struct perf_thread_map *threads = evlist->core.threads; 92771f566a3SWang Nan /* 92871f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 92971f566a3SWang Nan * Its value is decided by evsel's write_backward. 93071f566a3SWang Nan * So &mp should not be passed through const pointer. 93171f566a3SWang Nan */ 932e440979fSJiri Olsa struct mmap_params mp = { 933e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 934e440979fSJiri Olsa .affinity = affinity, 935e440979fSJiri Olsa .flush = flush, 936e440979fSJiri Olsa .comp_level = comp_level 937e440979fSJiri Olsa }; 93850a682ceSArnaldo Carvalho de Melo 9398db6d6b1SWang Nan if (!evlist->mmap) 940d50cf361SJiri Olsa evlist->mmap = evlist__alloc_mmap(evlist, false); 9418db6d6b1SWang Nan if (!evlist->mmap) 942f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 943f8a95309SArnaldo Carvalho de Melo 94431f67fc4SJiri Olsa if (evlist->core.pollfd.entries == NULL && perf_evlist__alloc_pollfd(&evlist->core) < 0) 945f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 946f8a95309SArnaldo Carvalho de Melo 947f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 948f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 949e440979fSJiri Olsa mp.core.mask = evlist->core.mmap_len - page_size - 1; 950f8a95309SArnaldo Carvalho de Melo 951f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 952718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 953718c602dSAdrian Hunter 954e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 9551fc632ceSJiri Olsa if ((evsel->core.attr.read_format & PERF_FORMAT_ID) && 9568cd36f3eSJiri Olsa evsel->core.sample_id == NULL && 95770c20369SJiri Olsa perf_evsel__alloc_id(&evsel->core, perf_cpu_map__nr(cpus), threads->nr) < 0) 958f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 959f8a95309SArnaldo Carvalho de Melo } 960f8a95309SArnaldo Carvalho de Melo 961315c0a1fSJiri Olsa if (perf_cpu_map__empty(cpus)) 9629521b5f2SJiri Olsa return evlist__mmap_per_thread(evlist, &mp); 963f8a95309SArnaldo Carvalho de Melo 9649521b5f2SJiri Olsa return evlist__mmap_per_cpu(evlist, &mp); 965f8a95309SArnaldo Carvalho de Melo } 9667e2ed097SArnaldo Carvalho de Melo 9679521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 968718c602dSAdrian Hunter { 9699521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 970718c602dSAdrian Hunter } 971718c602dSAdrian Hunter 97263503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 9737e2ed097SArnaldo Carvalho de Melo { 974147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 975f854839bSJiri Olsa struct perf_cpu_map *cpus; 9769749b90eSJiri Olsa struct perf_thread_map *threads; 9777e2ed097SArnaldo Carvalho de Melo 978147c508fSJin Yao /* 979147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 980147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 981147c508fSJin Yao * target->system_wide = true. 982147c508fSJin Yao * 983147c508fSJin Yao * If specify '--per-thread' only to perf record, 984147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 985147c508fSJin Yao * 986147c508fSJin Yao * So target->per_thread && target->system_wide is false. 987147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 988147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 989147c508fSJin Yao * current behavior. 990147c508fSJin Yao * 991147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 992147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 993147c508fSJin Yao * per-thread data. thread_map__new_str will call 994147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 995147c508fSJin Yao */ 99673c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 997147c508fSJin Yao all_threads); 99874bfd2b2SAdrian Hunter 99974bfd2b2SAdrian Hunter if (!threads) 10007e2ed097SArnaldo Carvalho de Melo return -1; 10017e2ed097SArnaldo Carvalho de Melo 10029c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 1003397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 1004879d77d0SNamhyung Kim else 10059c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 10067e2ed097SArnaldo Carvalho de Melo 100774bfd2b2SAdrian Hunter if (!cpus) 10087e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 10097e2ed097SArnaldo Carvalho de Melo 1010ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 1011ec9a77a7SAdrian Hunter 1012453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1013d5bc056eSAdrian Hunter 1014d5bc056eSAdrian Hunter return 0; 10157e2ed097SArnaldo Carvalho de Melo 10167e2ed097SArnaldo Carvalho de Melo out_delete_threads: 10177836e52eSJiri Olsa perf_thread_map__put(threads); 10187e2ed097SArnaldo Carvalho de Melo return -1; 10197e2ed097SArnaldo Carvalho de Melo } 10207e2ed097SArnaldo Carvalho de Melo 102163503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist, 102222c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 102322c8a376SArnaldo Carvalho de Melo { 102432dcd021SJiri Olsa struct evsel *evsel; 102522c8a376SArnaldo Carvalho de Melo 1026e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 102722c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 102822c8a376SArnaldo Carvalho de Melo } 102922c8a376SArnaldo Carvalho de Melo 103063503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist, 103122c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 103222c8a376SArnaldo Carvalho de Melo { 103332dcd021SJiri Olsa struct evsel *evsel; 103422c8a376SArnaldo Carvalho de Melo 1035e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 103622c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 103722c8a376SArnaldo Carvalho de Melo } 103822c8a376SArnaldo Carvalho de Melo 103963503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 10400a102479SFrederic Weisbecker { 104132dcd021SJiri Olsa struct evsel *evsel; 1042745cefc5SArnaldo Carvalho de Melo int err = 0; 10430a102479SFrederic Weisbecker 1044e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1045745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 10460a102479SFrederic Weisbecker continue; 1047745cefc5SArnaldo Carvalho de Melo 1048d988d5eeSKan Liang /* 1049d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1050d988d5eeSKan Liang * So evlist and evsel should always be same. 1051d988d5eeSKan Liang */ 1052a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 105323d4aad4SArnaldo Carvalho de Melo if (err) { 105423d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1055745cefc5SArnaldo Carvalho de Melo break; 10560a102479SFrederic Weisbecker } 105723d4aad4SArnaldo Carvalho de Melo } 10580a102479SFrederic Weisbecker 1059745cefc5SArnaldo Carvalho de Melo return err; 1060745cefc5SArnaldo Carvalho de Melo } 1061745cefc5SArnaldo Carvalho de Melo 106263503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1063745cefc5SArnaldo Carvalho de Melo { 106432dcd021SJiri Olsa struct evsel *evsel; 1065745cefc5SArnaldo Carvalho de Melo int err = 0; 1066745cefc5SArnaldo Carvalho de Melo 106705cea449SArnaldo Carvalho de Melo if (filter == NULL) 106805cea449SArnaldo Carvalho de Melo return -1; 106905cea449SArnaldo Carvalho de Melo 1070e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 10711fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1072fdf14720SWang Nan continue; 1073fdf14720SWang Nan 107494ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1075745cefc5SArnaldo Carvalho de Melo if (err) 1076745cefc5SArnaldo Carvalho de Melo break; 1077745cefc5SArnaldo Carvalho de Melo } 1078745cefc5SArnaldo Carvalho de Melo 1079745cefc5SArnaldo Carvalho de Melo return err; 10800a102479SFrederic Weisbecker } 108174429964SFrederic Weisbecker 108253c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter) 108353c92f73SArnaldo Carvalho de Melo { 108453c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 108553c92f73SArnaldo Carvalho de Melo int err = 0; 108653c92f73SArnaldo Carvalho de Melo 108753c92f73SArnaldo Carvalho de Melo if (filter == NULL) 108853c92f73SArnaldo Carvalho de Melo return -1; 108953c92f73SArnaldo Carvalho de Melo 109053c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 109153c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 109253c92f73SArnaldo Carvalho de Melo continue; 109353c92f73SArnaldo Carvalho de Melo 109453c92f73SArnaldo Carvalho de Melo err = perf_evsel__append_tp_filter(evsel, filter); 109553c92f73SArnaldo Carvalho de Melo if (err) 109653c92f73SArnaldo Carvalho de Melo break; 109753c92f73SArnaldo Carvalho de Melo } 109853c92f73SArnaldo Carvalho de Melo 109953c92f73SArnaldo Carvalho de Melo return err; 110053c92f73SArnaldo Carvalho de Melo } 110153c92f73SArnaldo Carvalho de Melo 110205cea449SArnaldo Carvalho de Melo static char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1103cfd70a26SArnaldo Carvalho de Melo { 1104cfd70a26SArnaldo Carvalho de Melo char *filter; 1105be199adaSArnaldo Carvalho de Melo size_t i; 1106cfd70a26SArnaldo Carvalho de Melo 1107be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1108be199adaSArnaldo Carvalho de Melo if (i == 0) { 1109be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 111005cea449SArnaldo Carvalho de Melo return NULL; 1111be199adaSArnaldo Carvalho de Melo } else { 1112be199adaSArnaldo Carvalho de Melo char *tmp; 1113be199adaSArnaldo Carvalho de Melo 1114be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1115be199adaSArnaldo Carvalho de Melo goto out_free; 1116be199adaSArnaldo Carvalho de Melo 1117be199adaSArnaldo Carvalho de Melo free(filter); 1118be199adaSArnaldo Carvalho de Melo filter = tmp; 1119be199adaSArnaldo Carvalho de Melo } 1120be199adaSArnaldo Carvalho de Melo } 1121cfd70a26SArnaldo Carvalho de Melo 112205cea449SArnaldo Carvalho de Melo return filter; 1123be199adaSArnaldo Carvalho de Melo out_free: 1124cfd70a26SArnaldo Carvalho de Melo free(filter); 112505cea449SArnaldo Carvalho de Melo return NULL; 112605cea449SArnaldo Carvalho de Melo } 112705cea449SArnaldo Carvalho de Melo 112805cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 112905cea449SArnaldo Carvalho de Melo { 113005cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 113105cea449SArnaldo Carvalho de Melo int ret = perf_evlist__set_tp_filter(evlist, filter); 113205cea449SArnaldo Carvalho de Melo 113305cea449SArnaldo Carvalho de Melo free(filter); 1134cfd70a26SArnaldo Carvalho de Melo return ret; 1135cfd70a26SArnaldo Carvalho de Melo } 1136cfd70a26SArnaldo Carvalho de Melo 113763503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1138be199adaSArnaldo Carvalho de Melo { 11397ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1140be199adaSArnaldo Carvalho de Melo } 1141be199adaSArnaldo Carvalho de Melo 11421827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 11431827ab5bSArnaldo Carvalho de Melo { 11441827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 11451827ab5bSArnaldo Carvalho de Melo int ret = perf_evlist__append_tp_filter(evlist, filter); 11461827ab5bSArnaldo Carvalho de Melo 11471827ab5bSArnaldo Carvalho de Melo free(filter); 11481827ab5bSArnaldo Carvalho de Melo return ret; 11491827ab5bSArnaldo Carvalho de Melo } 11501827ab5bSArnaldo Carvalho de Melo 11511827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 11521827ab5bSArnaldo Carvalho de Melo { 11531827ab5bSArnaldo Carvalho de Melo return perf_evlist__append_tp_filter_pids(evlist, 1, &pid); 11541827ab5bSArnaldo Carvalho de Melo } 11551827ab5bSArnaldo Carvalho de Melo 115663503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist) 115774429964SFrederic Weisbecker { 115832dcd021SJiri Olsa struct evsel *pos; 1159c2a70653SArnaldo Carvalho de Melo 11606484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 116175562573SAdrian Hunter return true; 116275562573SAdrian Hunter 116375562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 116475562573SAdrian Hunter return false; 116575562573SAdrian Hunter 1166e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 116775562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 116875562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1169c2a70653SArnaldo Carvalho de Melo return false; 117074429964SFrederic Weisbecker } 117174429964SFrederic Weisbecker 1172c2a70653SArnaldo Carvalho de Melo return true; 1173c2a70653SArnaldo Carvalho de Melo } 1174c2a70653SArnaldo Carvalho de Melo 117563503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist) 1176c2a70653SArnaldo Carvalho de Melo { 117732dcd021SJiri Olsa struct evsel *evsel; 117875562573SAdrian Hunter 117975562573SAdrian Hunter if (evlist->combined_sample_type) 118075562573SAdrian Hunter return evlist->combined_sample_type; 118175562573SAdrian Hunter 1182e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11831fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 118475562573SAdrian Hunter 118575562573SAdrian Hunter return evlist->combined_sample_type; 118675562573SAdrian Hunter } 118775562573SAdrian Hunter 118863503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist) 118975562573SAdrian Hunter { 119075562573SAdrian Hunter evlist->combined_sample_type = 0; 119175562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1192c2a70653SArnaldo Carvalho de Melo } 1193c2a70653SArnaldo Carvalho de Melo 119463503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist) 119598df858eSAndi Kleen { 119632dcd021SJiri Olsa struct evsel *evsel; 119798df858eSAndi Kleen u64 branch_type = 0; 119898df858eSAndi Kleen 1199e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 12001fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 120198df858eSAndi Kleen return branch_type; 120298df858eSAndi Kleen } 120398df858eSAndi Kleen 120463503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 12059ede473cSJiri Olsa { 1206515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 12071fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 12081fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 12099ede473cSJiri Olsa 1210e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 12111fc632ceSJiri Olsa if (read_format != pos->core.attr.read_format) 12129ede473cSJiri Olsa return false; 12139ede473cSJiri Olsa } 12149ede473cSJiri Olsa 12159ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 12169ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 12179ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 12189ede473cSJiri Olsa return false; 12199ede473cSJiri Olsa } 12209ede473cSJiri Olsa 12219ede473cSJiri Olsa return true; 12229ede473cSJiri Olsa } 12239ede473cSJiri Olsa 122463503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 122581e36bffSArnaldo Carvalho de Melo { 1226515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 122781e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 122881e36bffSArnaldo Carvalho de Melo u64 sample_type; 122981e36bffSArnaldo Carvalho de Melo u16 size = 0; 123081e36bffSArnaldo Carvalho de Melo 12311fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 123281e36bffSArnaldo Carvalho de Melo goto out; 123381e36bffSArnaldo Carvalho de Melo 12341fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 123581e36bffSArnaldo Carvalho de Melo 123681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 123781e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 123881e36bffSArnaldo Carvalho de Melo 123981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 124081e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 124181e36bffSArnaldo Carvalho de Melo 124281e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 124381e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 124481e36bffSArnaldo Carvalho de Melo 124581e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 124681e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 124781e36bffSArnaldo Carvalho de Melo 124881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 124981e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 125075562573SAdrian Hunter 125175562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 125275562573SAdrian Hunter size += sizeof(data->id); 125381e36bffSArnaldo Carvalho de Melo out: 125481e36bffSArnaldo Carvalho de Melo return size; 125581e36bffSArnaldo Carvalho de Melo } 125681e36bffSArnaldo Carvalho de Melo 125763503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist) 1258c2a70653SArnaldo Carvalho de Melo { 1259515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1260c2a70653SArnaldo Carvalho de Melo 1261e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 12621fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1263c2a70653SArnaldo Carvalho de Melo return false; 1264c2a70653SArnaldo Carvalho de Melo } 1265c2a70653SArnaldo Carvalho de Melo 1266c2a70653SArnaldo Carvalho de Melo return true; 126774429964SFrederic Weisbecker } 126874429964SFrederic Weisbecker 126963503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist) 127074429964SFrederic Weisbecker { 1271515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 12721fc632ceSJiri Olsa return first->core.attr.sample_id_all; 127374429964SFrederic Weisbecker } 127481cce8deSArnaldo Carvalho de Melo 127563503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 127632dcd021SJiri Olsa struct evsel *evsel) 127781cce8deSArnaldo Carvalho de Melo { 127881cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 127981cce8deSArnaldo Carvalho de Melo } 1280727ab04eSArnaldo Carvalho de Melo 1281750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1282a74b4b66SNamhyung Kim { 128332dcd021SJiri Olsa struct evsel *evsel; 1284a74b4b66SNamhyung Kim 1285475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 128688761fa1SJiri Olsa evsel__close(evsel); 1287a74b4b66SNamhyung Kim } 1288a74b4b66SNamhyung Kim 128963503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 12904112eb18SArnaldo Carvalho de Melo { 1291f854839bSJiri Olsa struct perf_cpu_map *cpus; 12929749b90eSJiri Olsa struct perf_thread_map *threads; 12934112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 12944112eb18SArnaldo Carvalho de Melo 12954112eb18SArnaldo Carvalho de Melo /* 12964112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 12974112eb18SArnaldo Carvalho de Melo * an all cpus map. 12984112eb18SArnaldo Carvalho de Melo * 12994112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 13004112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 13014112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 13024112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 13034112eb18SArnaldo Carvalho de Melo */ 13049c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 13058c0498b6SAdrian Hunter if (!cpus) 13064112eb18SArnaldo Carvalho de Melo goto out; 13074112eb18SArnaldo Carvalho de Melo 13084b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 13098c0498b6SAdrian Hunter if (!threads) 13108c0498b6SAdrian Hunter goto out_put; 13114112eb18SArnaldo Carvalho de Melo 1312453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 13134112eb18SArnaldo Carvalho de Melo out: 13144112eb18SArnaldo Carvalho de Melo return err; 13158c0498b6SAdrian Hunter out_put: 131638f01d8dSJiri Olsa perf_cpu_map__put(cpus); 13174112eb18SArnaldo Carvalho de Melo goto out; 13184112eb18SArnaldo Carvalho de Melo } 13194112eb18SArnaldo Carvalho de Melo 1320474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1321727ab04eSArnaldo Carvalho de Melo { 132232dcd021SJiri Olsa struct evsel *evsel; 1323a74b4b66SNamhyung Kim int err; 1324727ab04eSArnaldo Carvalho de Melo 13254112eb18SArnaldo Carvalho de Melo /* 13264112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 13274112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 13284112eb18SArnaldo Carvalho de Melo */ 132903617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 13304112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 13314112eb18SArnaldo Carvalho de Melo if (err < 0) 13324112eb18SArnaldo Carvalho de Melo goto out_err; 13334112eb18SArnaldo Carvalho de Melo } 13344112eb18SArnaldo Carvalho de Melo 1335733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1336733cd2feSAdrian Hunter 1337e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1338af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1339727ab04eSArnaldo Carvalho de Melo if (err < 0) 1340727ab04eSArnaldo Carvalho de Melo goto out_err; 1341727ab04eSArnaldo Carvalho de Melo } 1342727ab04eSArnaldo Carvalho de Melo 1343727ab04eSArnaldo Carvalho de Melo return 0; 1344727ab04eSArnaldo Carvalho de Melo out_err: 1345750b4edeSJiri Olsa evlist__close(evlist); 134641c21a68SNamhyung Kim errno = -err; 1347727ab04eSArnaldo Carvalho de Melo return err; 1348727ab04eSArnaldo Carvalho de Melo } 134935b9d88eSArnaldo Carvalho de Melo 135063503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 135155e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1352735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 135335b9d88eSArnaldo Carvalho de Melo { 135435b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 135535b9d88eSArnaldo Carvalho de Melo char bf; 135635b9d88eSArnaldo Carvalho de Melo 135735b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 135835b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 135935b9d88eSArnaldo Carvalho de Melo return -1; 136035b9d88eSArnaldo Carvalho de Melo } 136135b9d88eSArnaldo Carvalho de Melo 136235b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 136335b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 136435b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 136535b9d88eSArnaldo Carvalho de Melo } 136635b9d88eSArnaldo Carvalho de Melo 136735b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 136835b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 136935b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 137035b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 137135b9d88eSArnaldo Carvalho de Melo } 137235b9d88eSArnaldo Carvalho de Melo 137335b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13745f1c4225SArnaldo Carvalho de Melo int ret; 13755f1c4225SArnaldo Carvalho de Melo 1376119fa3c9SNamhyung Kim if (pipe_output) 137735b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 137835b9d88eSArnaldo Carvalho de Melo 13790817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13800817df08SDavid Ahern 138135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 138235b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 138335b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 138435b9d88eSArnaldo Carvalho de Melo 138535b9d88eSArnaldo Carvalho de Melo /* 138635b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 138735b9d88eSArnaldo Carvalho de Melo */ 138835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 138935b9d88eSArnaldo Carvalho de Melo 139035b9d88eSArnaldo Carvalho de Melo /* 139135b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 139235b9d88eSArnaldo Carvalho de Melo */ 13935f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 13945f1c4225SArnaldo Carvalho de Melo /* 13955f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 13965f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 13975f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 13985f1c4225SArnaldo Carvalho de Melo * 139920f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 14005f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 14015f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 14025f1c4225SArnaldo Carvalho de Melo * here. 14035f1c4225SArnaldo Carvalho de Melo */ 14045f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 14055f1c4225SArnaldo Carvalho de Melo if (ret == -1) 140635b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 14075f1c4225SArnaldo Carvalho de Melo exit(ret); 14085f1c4225SArnaldo Carvalho de Melo } 140935b9d88eSArnaldo Carvalho de Melo 141035b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 141135b9d88eSArnaldo Carvalho de Melo 1412735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1413f33cbe72SArnaldo Carvalho de Melo union sigval val; 1414f33cbe72SArnaldo Carvalho de Melo 1415f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1416f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 141735b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1418f33cbe72SArnaldo Carvalho de Melo } else 1419f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 142035b9d88eSArnaldo Carvalho de Melo exit(-1); 142135b9d88eSArnaldo Carvalho de Melo } 142235b9d88eSArnaldo Carvalho de Melo 1423735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1424735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1425735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1426735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1427735f7e0bSArnaldo Carvalho de Melo }; 1428735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1429735f7e0bSArnaldo Carvalho de Melo } 1430735f7e0bSArnaldo Carvalho de Melo 14311aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 143203617c22SJiri Olsa if (evlist->core.threads == NULL) { 14331aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 14341aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 14351aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 14361aaf63b1SArnaldo Carvalho de Melo } 143703617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 14381aaf63b1SArnaldo Carvalho de Melo } 143935b9d88eSArnaldo Carvalho de Melo 144035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 144135b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 144235b9d88eSArnaldo Carvalho de Melo /* 144335b9d88eSArnaldo Carvalho de Melo * wait for child to settle 144435b9d88eSArnaldo Carvalho de Melo */ 144535b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 144635b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 144735b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 144835b9d88eSArnaldo Carvalho de Melo } 144935b9d88eSArnaldo Carvalho de Melo 1450bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 145135b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 145235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 145335b9d88eSArnaldo Carvalho de Melo return 0; 145435b9d88eSArnaldo Carvalho de Melo 145535b9d88eSArnaldo Carvalho de Melo out_close_pipes: 145635b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 145735b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 145835b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 145935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 146035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 146135b9d88eSArnaldo Carvalho de Melo return -1; 146235b9d88eSArnaldo Carvalho de Melo } 146335b9d88eSArnaldo Carvalho de Melo 146463503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist) 146535b9d88eSArnaldo Carvalho de Melo { 146635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1467b3824404SDavid Ahern char bf = 0; 1468bcf3145fSNamhyung Kim int ret; 146935b9d88eSArnaldo Carvalho de Melo /* 147035b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 147135b9d88eSArnaldo Carvalho de Melo */ 1472bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1473bcf3145fSNamhyung Kim if (ret < 0) 1474e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1475bcf3145fSNamhyung Kim 1476bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1477bcf3145fSNamhyung Kim return ret; 147835b9d88eSArnaldo Carvalho de Melo } 147935b9d88eSArnaldo Carvalho de Melo 148035b9d88eSArnaldo Carvalho de Melo return 0; 148135b9d88eSArnaldo Carvalho de Melo } 1482cb0b29e0SArnaldo Carvalho de Melo 148363503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 14840807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1485cb0b29e0SArnaldo Carvalho de Melo { 148632dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 148775562573SAdrian Hunter 148875562573SAdrian Hunter if (!evsel) 148975562573SAdrian Hunter return -EFAULT; 14900807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1491cb0b29e0SArnaldo Carvalho de Melo } 149278f067b3SArnaldo Carvalho de Melo 149363503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 149401468120SJiri Olsa union perf_event *event, 149501468120SJiri Olsa u64 *timestamp) 149601468120SJiri Olsa { 149732dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 149801468120SJiri Olsa 149901468120SJiri Olsa if (!evsel) 150001468120SJiri Olsa return -EFAULT; 150101468120SJiri Olsa return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); 150201468120SJiri Olsa } 150301468120SJiri Olsa 150463503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist, 1505a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1506a8f23d8fSArnaldo Carvalho de Melo { 1507a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1508c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1509a8f23d8fSArnaldo Carvalho de Melo 1510a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1511a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1512a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1513a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1514a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1515a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1516a8f23d8fSArnaldo Carvalho de Melo 15171a47245dSAdrian Hunter value = perf_event_paranoid(); 1518a8f23d8fSArnaldo Carvalho de Melo 1519a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1520a8f23d8fSArnaldo Carvalho de Melo 1521a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1522a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1523a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1524a8f23d8fSArnaldo Carvalho de Melo } 1525a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15265229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1527a8f23d8fSArnaldo Carvalho de Melo 1528a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15295229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 15305229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1531a8f23d8fSArnaldo Carvalho de Melo break; 1532d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1533515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1534d9aade7fSArnaldo Carvalho de Melo int max_freq; 1535d9aade7fSArnaldo Carvalho de Melo 1536d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1537d9aade7fSArnaldo Carvalho de Melo goto out_default; 1538d9aade7fSArnaldo Carvalho de Melo 15391fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1540d9aade7fSArnaldo Carvalho de Melo goto out_default; 1541d9aade7fSArnaldo Carvalho de Melo 1542d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1543d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1544d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1545d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 15461fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1547d9aade7fSArnaldo Carvalho de Melo break; 1548d9aade7fSArnaldo Carvalho de Melo } 1549a8f23d8fSArnaldo Carvalho de Melo default: 1550d9aade7fSArnaldo Carvalho de Melo out_default: 1551a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1552a8f23d8fSArnaldo Carvalho de Melo break; 1553a8f23d8fSArnaldo Carvalho de Melo } 1554a8f23d8fSArnaldo Carvalho de Melo 1555a8f23d8fSArnaldo Carvalho de Melo return 0; 1556a8f23d8fSArnaldo Carvalho de Melo } 1557a025e4f0SAdrian Hunter 155863503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1559956fa571SArnaldo Carvalho de Melo { 1560c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1561f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1562956fa571SArnaldo Carvalho de Melo 1563956fa571SArnaldo Carvalho de Melo switch (err) { 1564956fa571SArnaldo Carvalho de Melo case EPERM: 1565e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1566e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1567e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1568956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1569e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1570e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1571e965bea1SArnaldo Carvalho de Melo 1572e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1573e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1574e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1575e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1576e965bea1SArnaldo Carvalho de Melo } 1577e965bea1SArnaldo Carvalho de Melo 1578e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1579e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1580956fa571SArnaldo Carvalho de Melo break; 1581956fa571SArnaldo Carvalho de Melo default: 1582956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1583956fa571SArnaldo Carvalho de Melo break; 1584956fa571SArnaldo Carvalho de Melo } 1585956fa571SArnaldo Carvalho de Melo 1586956fa571SArnaldo Carvalho de Melo return 0; 1587956fa571SArnaldo Carvalho de Melo } 1588956fa571SArnaldo Carvalho de Melo 158963503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 159032dcd021SJiri Olsa struct evsel *move_evsel) 1591a025e4f0SAdrian Hunter { 159232dcd021SJiri Olsa struct evsel *evsel, *n; 1593a025e4f0SAdrian Hunter LIST_HEAD(move); 1594a025e4f0SAdrian Hunter 1595515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1596a025e4f0SAdrian Hunter return; 1597a025e4f0SAdrian Hunter 1598e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1599a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1600b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1601a025e4f0SAdrian Hunter } 1602a025e4f0SAdrian Hunter 1603ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1604a025e4f0SAdrian Hunter } 160560b0896cSAdrian Hunter 160663503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist, 160732dcd021SJiri Olsa struct evsel *tracking_evsel) 160860b0896cSAdrian Hunter { 160932dcd021SJiri Olsa struct evsel *evsel; 161060b0896cSAdrian Hunter 161160b0896cSAdrian Hunter if (tracking_evsel->tracking) 161260b0896cSAdrian Hunter return; 161360b0896cSAdrian Hunter 1614e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 161560b0896cSAdrian Hunter if (evsel != tracking_evsel) 161660b0896cSAdrian Hunter evsel->tracking = false; 161760b0896cSAdrian Hunter } 161860b0896cSAdrian Hunter 161960b0896cSAdrian Hunter tracking_evsel->tracking = true; 162060b0896cSAdrian Hunter } 16217630b3e2SWang Nan 162232dcd021SJiri Olsa struct evsel * 162363503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist, 16247630b3e2SWang Nan const char *str) 16257630b3e2SWang Nan { 162632dcd021SJiri Olsa struct evsel *evsel; 16277630b3e2SWang Nan 1628e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16297630b3e2SWang Nan if (!evsel->name) 16307630b3e2SWang Nan continue; 16317630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 16327630b3e2SWang Nan return evsel; 16337630b3e2SWang Nan } 16347630b3e2SWang Nan 16357630b3e2SWang Nan return NULL; 16367630b3e2SWang Nan } 163754cc54deSWang Nan 163863503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 163954cc54deSWang Nan enum bkw_mmap_state state) 164054cc54deSWang Nan { 164154cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 164254cc54deSWang Nan enum action { 164354cc54deSWang Nan NONE, 164454cc54deSWang Nan PAUSE, 164554cc54deSWang Nan RESUME, 164654cc54deSWang Nan } action = NONE; 164754cc54deSWang Nan 16480b72d69aSWang Nan if (!evlist->overwrite_mmap) 164954cc54deSWang Nan return; 165054cc54deSWang Nan 165154cc54deSWang Nan switch (old_state) { 165254cc54deSWang Nan case BKW_MMAP_NOTREADY: { 165354cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1654dd8bd53aSLuis de Bethencourt goto state_err; 165554cc54deSWang Nan break; 165654cc54deSWang Nan } 165754cc54deSWang Nan case BKW_MMAP_RUNNING: { 165854cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 165954cc54deSWang Nan goto state_err; 166054cc54deSWang Nan action = PAUSE; 166154cc54deSWang Nan break; 166254cc54deSWang Nan } 166354cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 166454cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 166554cc54deSWang Nan goto state_err; 166654cc54deSWang Nan break; 166754cc54deSWang Nan } 166854cc54deSWang Nan case BKW_MMAP_EMPTY: { 166954cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 167054cc54deSWang Nan goto state_err; 167154cc54deSWang Nan action = RESUME; 167254cc54deSWang Nan break; 167354cc54deSWang Nan } 167454cc54deSWang Nan default: 167554cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 167654cc54deSWang Nan } 167754cc54deSWang Nan 167854cc54deSWang Nan evlist->bkw_mmap_state = state; 167954cc54deSWang Nan 168054cc54deSWang Nan switch (action) { 168154cc54deSWang Nan case PAUSE: 168254cc54deSWang Nan perf_evlist__pause(evlist); 168354cc54deSWang Nan break; 168454cc54deSWang Nan case RESUME: 168554cc54deSWang Nan perf_evlist__resume(evlist); 168654cc54deSWang Nan break; 168754cc54deSWang Nan case NONE: 168854cc54deSWang Nan default: 168954cc54deSWang Nan break; 169054cc54deSWang Nan } 169154cc54deSWang Nan 169254cc54deSWang Nan state_err: 169354cc54deSWang Nan return; 169454cc54deSWang Nan } 169507d6f446SArnaldo Carvalho de Melo 169663503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 169707d6f446SArnaldo Carvalho de Melo { 169832dcd021SJiri Olsa struct evsel *evsel; 169907d6f446SArnaldo Carvalho de Melo 170007d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 17011fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 170207d6f446SArnaldo Carvalho de Melo return false; 170307d6f446SArnaldo Carvalho de Melo } 170407d6f446SArnaldo Carvalho de Melo 170507d6f446SArnaldo Carvalho de Melo return true; 170607d6f446SArnaldo Carvalho de Melo } 1707e2bdbe80SJin Yao 1708e2bdbe80SJin Yao /* 1709e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1710e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1711e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1712e2bdbe80SJin Yao */ 171363503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1714e2bdbe80SJin Yao { 1715e2bdbe80SJin Yao if (!evlist->nr_groups) { 1716515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1717e2bdbe80SJin Yao 1718e2bdbe80SJin Yao perf_evlist__set_leader(evlist); 1719e2bdbe80SJin Yao leader->forced_leader = true; 1720e2bdbe80SJin Yao } 1721e2bdbe80SJin Yao } 1722c3537fc2SAndi Kleen 172363503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 172432dcd021SJiri Olsa struct evsel *evsel) 1725c3537fc2SAndi Kleen { 172632dcd021SJiri Olsa struct evsel *c2, *leader; 1727c3537fc2SAndi Kleen bool is_open = true; 1728c3537fc2SAndi Kleen 1729c3537fc2SAndi Kleen leader = evsel->leader; 1730c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 17315643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1732c3537fc2SAndi Kleen 1733c3537fc2SAndi Kleen /* 1734c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1735c3537fc2SAndi Kleen * include the first entry. 1736c3537fc2SAndi Kleen */ 1737c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1738c3537fc2SAndi Kleen if (c2 == evsel) 1739c3537fc2SAndi Kleen is_open = false; 1740c3537fc2SAndi Kleen if (c2->leader == leader) { 1741c3537fc2SAndi Kleen if (is_open) 17427834fa94SAndi Kleen perf_evsel__close(&evsel->core); 1743c3537fc2SAndi Kleen c2->leader = c2; 17445643b1a5SJiri Olsa c2->core.nr_members = 0; 1745c3537fc2SAndi Kleen } 1746c3537fc2SAndi Kleen } 1747c3537fc2SAndi Kleen return leader; 1748c3537fc2SAndi Kleen } 1749657ee553SSong Liu 175063503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist, 1751657ee553SSong Liu struct perf_event_attr *attr, 1752657ee553SSong Liu perf_evsel__sb_cb_t cb, 1753657ee553SSong Liu void *data) 1754657ee553SSong Liu { 175532dcd021SJiri Olsa struct evsel *evsel; 1756657ee553SSong Liu bool new_evlist = (*evlist) == NULL; 1757657ee553SSong Liu 1758657ee553SSong Liu if (*evlist == NULL) 17590f98b11cSJiri Olsa *evlist = evlist__new(); 1760657ee553SSong Liu if (*evlist == NULL) 1761657ee553SSong Liu return -1; 1762657ee553SSong Liu 1763657ee553SSong Liu if (!attr->sample_id_all) { 1764657ee553SSong Liu pr_warning("enabling sample_id_all for all side band events\n"); 1765657ee553SSong Liu attr->sample_id_all = 1; 1766657ee553SSong Liu } 1767657ee553SSong Liu 17686484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries); 1769657ee553SSong Liu if (!evsel) 1770657ee553SSong Liu goto out_err; 1771657ee553SSong Liu 1772657ee553SSong Liu evsel->side_band.cb = cb; 1773657ee553SSong Liu evsel->side_band.data = data; 1774a1cf3a75SJiri Olsa evlist__add(*evlist, evsel); 1775657ee553SSong Liu return 0; 1776657ee553SSong Liu 1777657ee553SSong Liu out_err: 1778657ee553SSong Liu if (new_evlist) { 1779c12995a5SJiri Olsa evlist__delete(*evlist); 1780657ee553SSong Liu *evlist = NULL; 1781657ee553SSong Liu } 1782657ee553SSong Liu return -1; 1783657ee553SSong Liu } 1784657ee553SSong Liu 1785657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg) 1786657ee553SSong Liu { 178763503dbaSJiri Olsa struct evlist *evlist = arg; 1788657ee553SSong Liu bool draining = false; 1789adc6257cSJiri Olsa int i, done = 0; 1790b397f846SArnaldo Carvalho de Melo /* 1791b397f846SArnaldo Carvalho de Melo * In order to read symbols from other namespaces perf to needs to call 1792b397f846SArnaldo Carvalho de Melo * setns(2). This isn't permitted if the struct_fs has multiple users. 1793b397f846SArnaldo Carvalho de Melo * unshare(2) the fs so that we may continue to setns into namespaces 1794b397f846SArnaldo Carvalho de Melo * that we're observing when, for instance, reading the build-ids at 1795b397f846SArnaldo Carvalho de Melo * the end of a 'perf record' session. 1796b397f846SArnaldo Carvalho de Melo */ 1797b397f846SArnaldo Carvalho de Melo unshare(CLONE_FS); 1798657ee553SSong Liu 1799adc6257cSJiri Olsa while (!done) { 1800adc6257cSJiri Olsa bool got_data = false; 1801adc6257cSJiri Olsa 1802adc6257cSJiri Olsa if (evlist->thread.done) 1803657ee553SSong Liu draining = true; 1804657ee553SSong Liu 1805657ee553SSong Liu if (!draining) 180680ab2987SJiri Olsa evlist__poll(evlist, 1000); 1807657ee553SSong Liu 1808c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 1809a5830532SJiri Olsa struct mmap *map = &evlist->mmap[i]; 1810657ee553SSong Liu union perf_event *event; 1811657ee553SSong Liu 1812657ee553SSong Liu if (perf_mmap__read_init(map)) 1813657ee553SSong Liu continue; 1814657ee553SSong Liu while ((event = perf_mmap__read_event(map)) != NULL) { 181532dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1816657ee553SSong Liu 1817657ee553SSong Liu if (evsel && evsel->side_band.cb) 1818657ee553SSong Liu evsel->side_band.cb(event, evsel->side_band.data); 1819657ee553SSong Liu else 1820657ee553SSong Liu pr_warning("cannot locate proper evsel for the side band event\n"); 1821657ee553SSong Liu 18227728fa0cSJiri Olsa perf_mmap__consume(&map->core); 1823adc6257cSJiri Olsa got_data = true; 1824657ee553SSong Liu } 1825657ee553SSong Liu perf_mmap__read_done(map); 1826657ee553SSong Liu } 1827adc6257cSJiri Olsa 1828adc6257cSJiri Olsa if (draining && !got_data) 1829adc6257cSJiri Olsa break; 1830657ee553SSong Liu } 1831657ee553SSong Liu return NULL; 1832657ee553SSong Liu } 1833657ee553SSong Liu 183463503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist, 1835657ee553SSong Liu struct target *target) 1836657ee553SSong Liu { 183732dcd021SJiri Olsa struct evsel *counter; 1838657ee553SSong Liu 1839657ee553SSong Liu if (!evlist) 1840657ee553SSong Liu return 0; 1841657ee553SSong Liu 1842657ee553SSong Liu if (perf_evlist__create_maps(evlist, target)) 1843657ee553SSong Liu goto out_delete_evlist; 1844657ee553SSong Liu 1845657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1846f72f901dSJiri Olsa if (evsel__open(counter, evlist->core.cpus, 184703617c22SJiri Olsa evlist->core.threads) < 0) 1848657ee553SSong Liu goto out_delete_evlist; 1849657ee553SSong Liu } 1850657ee553SSong Liu 18519521b5f2SJiri Olsa if (evlist__mmap(evlist, UINT_MAX)) 1852657ee553SSong Liu goto out_delete_evlist; 1853657ee553SSong Liu 1854657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1855ec7f24efSJiri Olsa if (evsel__enable(counter)) 1856657ee553SSong Liu goto out_delete_evlist; 1857657ee553SSong Liu } 1858657ee553SSong Liu 1859657ee553SSong Liu evlist->thread.done = 0; 1860657ee553SSong Liu if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist)) 1861657ee553SSong Liu goto out_delete_evlist; 1862657ee553SSong Liu 1863657ee553SSong Liu return 0; 1864657ee553SSong Liu 1865657ee553SSong Liu out_delete_evlist: 1866c12995a5SJiri Olsa evlist__delete(evlist); 1867657ee553SSong Liu evlist = NULL; 1868657ee553SSong Liu return -1; 1869657ee553SSong Liu } 1870657ee553SSong Liu 187163503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist) 1872657ee553SSong Liu { 1873657ee553SSong Liu if (!evlist) 1874657ee553SSong Liu return; 1875657ee553SSong Liu evlist->thread.done = 1; 1876657ee553SSong Liu pthread_join(evlist->thread.th, NULL); 1877c12995a5SJiri Olsa evlist__delete(evlist); 1878657ee553SSong Liu } 1879