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> 4570db7533SArnaldo Carvalho de Melo 46e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h> 47e14e5497SArnaldo Carvalho de Melo 48748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 49748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 50748fe088SArnaldo Carvalho de Melo #endif 51748fe088SArnaldo Carvalho de Melo 529dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 538cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 54f8a95309SArnaldo Carvalho de Melo 5552c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 569749b90eSJiri Olsa struct perf_thread_map *threads) 57361c99a6SArnaldo Carvalho de Melo { 584562a739SJiri Olsa perf_evlist__init(&evlist->core); 59453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 6040cb2d51SJiri Olsa fdarray__init(&evlist->core.pollfd, 64); 6135b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 6254cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 63361c99a6SArnaldo Carvalho de Melo } 64361c99a6SArnaldo Carvalho de Melo 650f98b11cSJiri Olsa struct evlist *evlist__new(void) 66ef1d1af2SArnaldo Carvalho de Melo { 6763503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist)); 68ef1d1af2SArnaldo Carvalho de Melo 69ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 7052c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL); 71ef1d1af2SArnaldo Carvalho de Melo 72361c99a6SArnaldo Carvalho de Melo return evlist; 73361c99a6SArnaldo Carvalho de Melo } 74361c99a6SArnaldo Carvalho de Melo 7563503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void) 76b22d54b0SJiri Olsa { 770f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 78b22d54b0SJiri Olsa 79b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 80c12995a5SJiri Olsa evlist__delete(evlist); 81b22d54b0SJiri Olsa evlist = NULL; 82b22d54b0SJiri Olsa } 83b22d54b0SJiri Olsa 84b22d54b0SJiri Olsa return evlist; 85b22d54b0SJiri Olsa } 86b22d54b0SJiri Olsa 8763503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void) 885bae0250SArnaldo Carvalho de Melo { 890f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 905bae0250SArnaldo Carvalho de Melo 915bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 92c12995a5SJiri Olsa evlist__delete(evlist); 935bae0250SArnaldo Carvalho de Melo evlist = NULL; 945bae0250SArnaldo Carvalho de Melo } 955bae0250SArnaldo Carvalho de Melo 965bae0250SArnaldo Carvalho de Melo return evlist; 975bae0250SArnaldo Carvalho de Melo } 985bae0250SArnaldo Carvalho de Melo 9975562573SAdrian Hunter /** 10075562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 10175562573SAdrian Hunter * @evlist: selected event list 10275562573SAdrian Hunter * 10375562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 10475562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 10575562573SAdrian Hunter */ 10663503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist) 10775562573SAdrian Hunter { 108515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 10975562573SAdrian Hunter 11075562573SAdrian Hunter evlist->id_pos = first->id_pos; 11175562573SAdrian Hunter evlist->is_pos = first->is_pos; 11275562573SAdrian Hunter } 11375562573SAdrian Hunter 11463503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist) 115733cd2feSAdrian Hunter { 11632dcd021SJiri Olsa struct evsel *evsel; 117733cd2feSAdrian Hunter 118e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 119733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 120733cd2feSAdrian Hunter 121733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 122733cd2feSAdrian Hunter } 123733cd2feSAdrian Hunter 124e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist) 125361c99a6SArnaldo Carvalho de Melo { 12632dcd021SJiri Olsa struct evsel *pos, *n; 127361c99a6SArnaldo Carvalho de Melo 128e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 129b27c4eceSJiri Olsa list_del_init(&pos->core.node); 130d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 1315eb2dd2aSJiri Olsa evsel__delete(pos); 132361c99a6SArnaldo Carvalho de Melo } 133361c99a6SArnaldo Carvalho de Melo 1346484d2f9SJiri Olsa evlist->core.nr_entries = 0; 135361c99a6SArnaldo Carvalho de Melo } 136361c99a6SArnaldo Carvalho de Melo 137470579b0SJiri Olsa void evlist__exit(struct evlist *evlist) 138ef1d1af2SArnaldo Carvalho de Melo { 13904662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1400b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 14140cb2d51SJiri Olsa fdarray__exit(&evlist->core.pollfd); 142ef1d1af2SArnaldo Carvalho de Melo } 143ef1d1af2SArnaldo Carvalho de Melo 144c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist) 145361c99a6SArnaldo Carvalho de Melo { 1460b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1470b04b3dcSArnaldo Carvalho de Melo return; 1480b04b3dcSArnaldo Carvalho de Melo 149db6b7b13SJiri Olsa evlist__munmap(evlist); 150750b4edeSJiri Olsa evlist__close(evlist); 151f72f901dSJiri Olsa perf_cpu_map__put(evlist->core.cpus); 15203617c22SJiri Olsa perf_thread_map__put(evlist->core.threads); 153f72f901dSJiri Olsa evlist->core.cpus = NULL; 15403617c22SJiri Olsa evlist->core.threads = NULL; 155e6b1878dSJiri Olsa evlist__purge(evlist); 156470579b0SJiri Olsa evlist__exit(evlist); 157361c99a6SArnaldo Carvalho de Melo free(evlist); 158361c99a6SArnaldo Carvalho de Melo } 159361c99a6SArnaldo Carvalho de Melo 160a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 161361c99a6SArnaldo Carvalho de Melo { 162d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1636484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 16460b0896cSAdrian Hunter entry->tracking = !entry->idx; 165ef503831SArnaldo Carvalho de Melo 1666484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1676484d2f9SJiri Olsa 1686484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 16975562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 170361c99a6SArnaldo Carvalho de Melo } 171361c99a6SArnaldo Carvalho de Melo 17216251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1734768230aSAdrian Hunter { 1744768230aSAdrian Hunter evsel->evlist = NULL; 17552e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1764768230aSAdrian Hunter } 1774768230aSAdrian Hunter 17863503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist, 179f114d6efSAdrian Hunter struct list_head *list) 18050d08e47SArnaldo Carvalho de Melo { 18132dcd021SJiri Olsa struct evsel *evsel, *temp; 18275562573SAdrian Hunter 183e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 184b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 185a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 186f114d6efSAdrian Hunter } 18750d08e47SArnaldo Carvalho de Melo } 18850d08e47SArnaldo Carvalho de Melo 189c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist, 190c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs) 191c0e53476SArnaldo Carvalho de Melo { 192c0e53476SArnaldo Carvalho de Melo struct evsel *evsel; 193c0e53476SArnaldo Carvalho de Melo size_t i; 194c0e53476SArnaldo Carvalho de Melo int err; 195c0e53476SArnaldo Carvalho de Melo 196c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 197c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it. 198c0e53476SArnaldo Carvalho de Melo evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name); 199c0e53476SArnaldo Carvalho de Melo if (evsel == NULL) 200c0e53476SArnaldo Carvalho de Melo continue; 201c0e53476SArnaldo Carvalho de Melo 202c0e53476SArnaldo Carvalho de Melo err = -EEXIST; 203c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL) 204c0e53476SArnaldo Carvalho de Melo goto out; 205c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler; 206c0e53476SArnaldo Carvalho de Melo } 207c0e53476SArnaldo Carvalho de Melo 208c0e53476SArnaldo Carvalho de Melo err = 0; 209c0e53476SArnaldo Carvalho de Melo out: 210c0e53476SArnaldo Carvalho de Melo return err; 211c0e53476SArnaldo Carvalho de Melo } 212c0e53476SArnaldo Carvalho de Melo 21363dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 21463dab225SArnaldo Carvalho de Melo { 21532dcd021SJiri Olsa struct evsel *evsel, *leader; 21663dab225SArnaldo Carvalho de Melo 217b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 218b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 21997f63e4aSNamhyung Kim 2205643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 22163dab225SArnaldo Carvalho de Melo 222e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22363dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22463dab225SArnaldo Carvalho de Melo } 22563dab225SArnaldo Carvalho de Melo } 22663dab225SArnaldo Carvalho de Melo 22763503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist) 2286a4bb04cSJiri Olsa { 2296484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2306484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 231ce9036a6SJiri Olsa __perf_evlist__set_leader(&evlist->core.entries); 2326a4bb04cSJiri Olsa } 23397f63e4aSNamhyung Kim } 2346a4bb04cSJiri Olsa 23563503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise) 236361c99a6SArnaldo Carvalho de Melo { 23732dcd021SJiri Olsa struct evsel *evsel = perf_evsel__new_cycles(precise); 238361c99a6SArnaldo Carvalho de Melo 239361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2407c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 241361c99a6SArnaldo Carvalho de Melo 242a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 243361c99a6SArnaldo Carvalho de Melo return 0; 244361c99a6SArnaldo Carvalho de Melo } 2455c581041SArnaldo Carvalho de Melo 24663503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist) 2475bae0250SArnaldo Carvalho de Melo { 2485bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2495bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2505bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2515bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2525bae0250SArnaldo Carvalho de Melo }; 2536484d2f9SJiri Olsa struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries); 2545bae0250SArnaldo Carvalho de Melo 2555bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2565bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2575bae0250SArnaldo Carvalho de Melo 258a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2595bae0250SArnaldo Carvalho de Melo return 0; 2605bae0250SArnaldo Carvalho de Melo } 2615bae0250SArnaldo Carvalho de Melo 262a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist, 26350d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 26450d08e47SArnaldo Carvalho de Melo { 26532dcd021SJiri Olsa struct evsel *evsel, *n; 26650d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 26750d08e47SArnaldo Carvalho de Melo size_t i; 26850d08e47SArnaldo Carvalho de Melo 26950d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2706484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 27150d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27250d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 273b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 27450d08e47SArnaldo Carvalho de Melo } 27550d08e47SArnaldo Carvalho de Melo 276f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 27750d08e47SArnaldo Carvalho de Melo 27850d08e47SArnaldo Carvalho de Melo return 0; 27950d08e47SArnaldo Carvalho de Melo 28050d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 281e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2825eb2dd2aSJiri Olsa evsel__delete(evsel); 28350d08e47SArnaldo Carvalho de Melo return -1; 28450d08e47SArnaldo Carvalho de Melo } 28550d08e47SArnaldo Carvalho de Melo 28663503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist, 28779695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 28879695e1bSArnaldo Carvalho de Melo { 28979695e1bSArnaldo Carvalho de Melo size_t i; 29079695e1bSArnaldo Carvalho de Melo 29179695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 29279695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 29379695e1bSArnaldo Carvalho de Melo 294a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 29579695e1bSArnaldo Carvalho de Melo } 29679695e1bSArnaldo Carvalho de Melo 29732dcd021SJiri Olsa struct evsel * 29863503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 299ee29be62SArnaldo Carvalho de Melo { 30032dcd021SJiri Olsa struct evsel *evsel; 301ee29be62SArnaldo Carvalho de Melo 302e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3031fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3041fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 305ee29be62SArnaldo Carvalho de Melo return evsel; 306ee29be62SArnaldo Carvalho de Melo } 307ee29be62SArnaldo Carvalho de Melo 308ee29be62SArnaldo Carvalho de Melo return NULL; 309ee29be62SArnaldo Carvalho de Melo } 310ee29be62SArnaldo Carvalho de Melo 31132dcd021SJiri Olsa struct evsel * 31263503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist, 313a2f2804aSDavid Ahern const char *name) 314a2f2804aSDavid Ahern { 31532dcd021SJiri Olsa struct evsel *evsel; 316a2f2804aSDavid Ahern 317e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3181fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 319a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 320a2f2804aSDavid Ahern return evsel; 321a2f2804aSDavid Ahern } 322a2f2804aSDavid Ahern 323a2f2804aSDavid Ahern return NULL; 324a2f2804aSDavid Ahern } 325a2f2804aSDavid Ahern 32663503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist, 32739876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 32839876e7dSArnaldo Carvalho de Melo { 32932dcd021SJiri Olsa struct evsel *evsel = perf_evsel__newtp(sys, name); 33039876e7dSArnaldo Carvalho de Melo 3318dd2a131SJiri Olsa if (IS_ERR(evsel)) 33239876e7dSArnaldo Carvalho de Melo return -1; 33339876e7dSArnaldo Carvalho de Melo 334744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 335a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 33639876e7dSArnaldo Carvalho de Melo return 0; 33739876e7dSArnaldo Carvalho de Melo } 33839876e7dSArnaldo Carvalho de Melo 33963503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist, 34032dcd021SJiri Olsa struct evsel *evsel) 341bf8e8f4bSAdrian Hunter { 342648b5af3SJiri Olsa if (evsel->core.system_wide) 343bf8e8f4bSAdrian Hunter return 1; 344bf8e8f4bSAdrian Hunter else 345a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 346bf8e8f4bSAdrian Hunter } 347bf8e8f4bSAdrian Hunter 348e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3494152ab37SArnaldo Carvalho de Melo { 35032dcd021SJiri Olsa struct evsel *pos; 3514152ab37SArnaldo Carvalho de Melo 352e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3539dfcb759SJiri Olsa if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd) 3543fe4430dSJiri Olsa continue; 3559a10bb22SJiri Olsa evsel__disable(pos); 3564152ab37SArnaldo Carvalho de Melo } 3572b56bcfbSArnaldo Carvalho de Melo 3582b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3594152ab37SArnaldo Carvalho de Melo } 3604152ab37SArnaldo Carvalho de Melo 3611c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 362764e16a3SDavid Ahern { 36332dcd021SJiri Olsa struct evsel *pos; 364764e16a3SDavid Ahern 365e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3669dfcb759SJiri Olsa if (!perf_evsel__is_group_leader(pos) || !pos->core.fd) 3673fe4430dSJiri Olsa continue; 368ec7f24efSJiri Olsa evsel__enable(pos); 369764e16a3SDavid Ahern } 3702b56bcfbSArnaldo Carvalho de Melo 3712b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3722b56bcfbSArnaldo Carvalho de Melo } 3732b56bcfbSArnaldo Carvalho de Melo 37463503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist) 3752b56bcfbSArnaldo Carvalho de Melo { 376e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 377764e16a3SDavid Ahern } 378764e16a3SDavid Ahern 37963503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist, 38032dcd021SJiri Olsa struct evsel *evsel, int cpu) 3811c65056cSAdrian Hunter { 38218ef15c6SArnaldo Carvalho de Melo int thread; 3831c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3841c65056cSAdrian Hunter 3859dfcb759SJiri Olsa if (!evsel->core.fd) 3861c65056cSAdrian Hunter return -EINVAL; 3871c65056cSAdrian Hunter 3881c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 38918ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3901c65056cSAdrian Hunter if (err) 3911c65056cSAdrian Hunter return err; 3921c65056cSAdrian Hunter } 3931c65056cSAdrian Hunter return 0; 3941c65056cSAdrian Hunter } 3951c65056cSAdrian Hunter 39663503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist, 39732dcd021SJiri Olsa struct evsel *evsel, 3981c65056cSAdrian Hunter int thread) 3991c65056cSAdrian Hunter { 40018ef15c6SArnaldo Carvalho de Melo int cpu; 4016549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 4021c65056cSAdrian Hunter 4039dfcb759SJiri Olsa if (!evsel->core.fd) 4041c65056cSAdrian Hunter return -EINVAL; 4051c65056cSAdrian Hunter 4061c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 40718ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4081c65056cSAdrian Hunter if (err) 4091c65056cSAdrian Hunter return err; 4101c65056cSAdrian Hunter } 4111c65056cSAdrian Hunter return 0; 4121c65056cSAdrian Hunter } 4131c65056cSAdrian Hunter 41463503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist, 41532dcd021SJiri Olsa struct evsel *evsel, int idx) 4161c65056cSAdrian Hunter { 417315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 4181c65056cSAdrian Hunter 4191c65056cSAdrian Hunter if (per_cpu_mmaps) 4201c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4211c65056cSAdrian Hunter else 4221c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4231c65056cSAdrian Hunter } 4241c65056cSAdrian Hunter 425f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 426e4b356b5SArnaldo Carvalho de Melo { 427f4009e7bSJiri Olsa return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN); 428e4b356b5SArnaldo Carvalho de Melo } 4291b85337dSArnaldo Carvalho de Melo 430258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 431258e4bfcSWang Nan void *arg __maybe_unused) 432e4b356b5SArnaldo Carvalho de Melo { 433a5830532SJiri Olsa struct mmap *map = fda->priv[fd].ptr; 434e4b356b5SArnaldo Carvalho de Melo 4354876075bSWang Nan if (map) 4364876075bSWang Nan perf_mmap__put(map); 43770082dd9SArnaldo Carvalho de Melo } 43870db7533SArnaldo Carvalho de Melo 439f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 4401ddec7f0SArnaldo Carvalho de Melo { 44140cb2d51SJiri Olsa return fdarray__filter(&evlist->core.pollfd, revents_and_mask, 442258e4bfcSWang Nan perf_evlist__munmap_filtered, NULL); 4431ddec7f0SArnaldo Carvalho de Melo } 4441ddec7f0SArnaldo Carvalho de Melo 44580ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 446f66a889dSArnaldo Carvalho de Melo { 44780ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 448f66a889dSArnaldo Carvalho de Melo } 449f66a889dSArnaldo Carvalho de Melo 45063503dbaSJiri Olsa static void perf_evlist__set_sid_idx(struct evlist *evlist, 45132dcd021SJiri Olsa struct evsel *evsel, int idx, int cpu, 4523c659eedSAdrian Hunter int thread) 4533c659eedSAdrian Hunter { 4543c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 4553c659eedSAdrian Hunter sid->idx = idx; 456f72f901dSJiri Olsa if (evlist->core.cpus && cpu >= 0) 457f72f901dSJiri Olsa sid->cpu = evlist->core.cpus->map[cpu]; 4583c659eedSAdrian Hunter else 4593c659eedSAdrian Hunter sid->cpu = -1; 460648b5af3SJiri Olsa if (!evsel->core.system_wide && evlist->core.threads && thread >= 0) 461a2f354e3SJiri Olsa sid->tid = perf_thread_map__pid(evlist->core.threads, thread); 4623c659eedSAdrian Hunter else 4633c659eedSAdrian Hunter sid->tid = -1; 4643c659eedSAdrian Hunter } 4653c659eedSAdrian Hunter 46663503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 46770db7533SArnaldo Carvalho de Melo { 46870db7533SArnaldo Carvalho de Melo struct hlist_head *head; 46970db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 47070db7533SArnaldo Carvalho de Melo int hash; 47170db7533SArnaldo Carvalho de Melo 47270db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 4731d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 47470db7533SArnaldo Carvalho de Melo 475b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 47670db7533SArnaldo Carvalho de Melo if (sid->id == id) 477932a3594SJiri Olsa return sid; 478932a3594SJiri Olsa 479932a3594SJiri Olsa return NULL; 480932a3594SJiri Olsa } 481932a3594SJiri Olsa 48263503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 483932a3594SJiri Olsa { 484932a3594SJiri Olsa struct perf_sample_id *sid; 485932a3594SJiri Olsa 4866484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 487515dbe48SJiri Olsa return evlist__first(evlist); 488932a3594SJiri Olsa 489932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 490932a3594SJiri Olsa if (sid) 49170c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 49230e68bccSNamhyung Kim 49330e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 494515dbe48SJiri Olsa return evlist__first(evlist); 49530e68bccSNamhyung Kim 49670db7533SArnaldo Carvalho de Melo return NULL; 49770db7533SArnaldo Carvalho de Melo } 49804391debSArnaldo Carvalho de Melo 49963503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 500dddcf6abSAdrian Hunter u64 id) 501dddcf6abSAdrian Hunter { 502dddcf6abSAdrian Hunter struct perf_sample_id *sid; 503dddcf6abSAdrian Hunter 504dddcf6abSAdrian Hunter if (!id) 505dddcf6abSAdrian Hunter return NULL; 506dddcf6abSAdrian Hunter 507dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 508dddcf6abSAdrian Hunter if (sid) 50970c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 510dddcf6abSAdrian Hunter 511dddcf6abSAdrian Hunter return NULL; 512dddcf6abSAdrian Hunter } 513dddcf6abSAdrian Hunter 51463503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist, 51575562573SAdrian Hunter union perf_event *event, u64 *id) 51675562573SAdrian Hunter { 517b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 51875562573SAdrian Hunter ssize_t n; 51975562573SAdrian Hunter 52075562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 52175562573SAdrian Hunter 52275562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 52375562573SAdrian Hunter if (evlist->id_pos >= n) 52475562573SAdrian Hunter return -1; 52575562573SAdrian Hunter *id = array[evlist->id_pos]; 52675562573SAdrian Hunter } else { 52775562573SAdrian Hunter if (evlist->is_pos > n) 52875562573SAdrian Hunter return -1; 52975562573SAdrian Hunter n -= evlist->is_pos; 53075562573SAdrian Hunter *id = array[n]; 53175562573SAdrian Hunter } 53275562573SAdrian Hunter return 0; 53375562573SAdrian Hunter } 53475562573SAdrian Hunter 53563503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 53675562573SAdrian Hunter union perf_event *event) 53775562573SAdrian Hunter { 538515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 53975562573SAdrian Hunter struct hlist_head *head; 54075562573SAdrian Hunter struct perf_sample_id *sid; 54175562573SAdrian Hunter int hash; 54275562573SAdrian Hunter u64 id; 54375562573SAdrian Hunter 5446484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 54598be6966SAdrian Hunter return first; 54698be6966SAdrian Hunter 5471fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 54898be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 54998be6966SAdrian Hunter return first; 55075562573SAdrian Hunter 55175562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 55275562573SAdrian Hunter return NULL; 55375562573SAdrian Hunter 55475562573SAdrian Hunter /* Synthesized events have an id of zero */ 55575562573SAdrian Hunter if (!id) 55698be6966SAdrian Hunter return first; 55775562573SAdrian Hunter 55875562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5591d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 56075562573SAdrian Hunter 56175562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 56275562573SAdrian Hunter if (sid->id == id) 56370c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 56475562573SAdrian Hunter } 56575562573SAdrian Hunter return NULL; 56675562573SAdrian Hunter } 56775562573SAdrian Hunter 56863503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value) 56965aea233SWang Nan { 57065aea233SWang Nan int i; 57165aea233SWang Nan 5720b72d69aSWang Nan if (!evlist->overwrite_mmap) 573078c3386SWang Nan return 0; 574078c3386SWang Nan 575c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 5762cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 57765aea233SWang Nan int err; 57865aea233SWang Nan 57965aea233SWang Nan if (fd < 0) 58065aea233SWang Nan continue; 58165aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 58265aea233SWang Nan if (err) 58365aea233SWang Nan return err; 58465aea233SWang Nan } 58565aea233SWang Nan return 0; 58665aea233SWang Nan } 58765aea233SWang Nan 58863503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist) 58965aea233SWang Nan { 59065aea233SWang Nan return perf_evlist__set_paused(evlist, true); 59165aea233SWang Nan } 59265aea233SWang Nan 59363503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist) 59465aea233SWang Nan { 59565aea233SWang Nan return perf_evlist__set_paused(evlist, false); 59665aea233SWang Nan } 59765aea233SWang Nan 598db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 599f8a95309SArnaldo Carvalho de Melo { 600aece948fSArnaldo Carvalho de Melo int i; 601f8a95309SArnaldo Carvalho de Melo 602b2cb615dSWang Nan if (evlist->mmap) 603c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 604a1f72618SWang Nan perf_mmap__munmap(&evlist->mmap[i]); 605b2cb615dSWang Nan 6060b72d69aSWang Nan if (evlist->overwrite_mmap) 607c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 6080b72d69aSWang Nan perf_mmap__munmap(&evlist->overwrite_mmap[i]); 609a1f72618SWang Nan } 610aece948fSArnaldo Carvalho de Melo 611db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 612a1f72618SWang Nan { 613db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 61404662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6150b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 616f8a95309SArnaldo Carvalho de Melo } 617f8a95309SArnaldo Carvalho de Melo 618d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 6192c5f6d87SKan Liang bool overwrite) 620f8a95309SArnaldo Carvalho de Melo { 621d4c6fb36SWang Nan int i; 622a5830532SJiri Olsa struct mmap *map; 623d4c6fb36SWang Nan 624c976ee11SJiri Olsa evlist->core.nr_mmaps = perf_cpu_map__nr(evlist->core.cpus); 625315c0a1fSJiri Olsa if (perf_cpu_map__empty(evlist->core.cpus)) 626c976ee11SJiri Olsa evlist->core.nr_mmaps = perf_thread_map__nr(evlist->core.threads); 627c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 6288db6d6b1SWang Nan if (!map) 6298db6d6b1SWang Nan return NULL; 630946ae1d4SWang Nan 631c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6324738ca30SArnaldo Carvalho de Melo /* 6334738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 6346afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 6354738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 6364738ca30SArnaldo Carvalho de Melo * dropped. 6374738ca30SArnaldo Carvalho de Melo * 6384738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 6394738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 6404738ca30SArnaldo Carvalho de Melo */ 641353120b4SJiri Olsa perf_mmap__init(&map[i].core, overwrite); 6424738ca30SArnaldo Carvalho de Melo } 643353120b4SJiri Olsa 6448db6d6b1SWang Nan return map; 645f8a95309SArnaldo Carvalho de Melo } 646f8a95309SArnaldo Carvalho de Melo 647f3058a1cSWang Nan static bool 64863503dbaSJiri Olsa perf_evlist__should_poll(struct evlist *evlist __maybe_unused, 64932dcd021SJiri Olsa struct evsel *evsel) 650f3058a1cSWang Nan { 6511fc632ceSJiri Olsa if (evsel->core.attr.write_backward) 652f3058a1cSWang Nan return false; 653f3058a1cSWang Nan return true; 654f3058a1cSWang Nan } 655f3058a1cSWang Nan 6569521b5f2SJiri Olsa static int evlist__mmap_per_evsel(struct evlist *evlist, int idx, 6579f21b815SMark Rutland struct mmap_params *mp, int cpu_idx, 6580b72d69aSWang Nan int thread, int *_output, int *_output_overwrite) 659aece948fSArnaldo Carvalho de Melo { 66032dcd021SJiri Olsa struct evsel *evsel; 661f3058a1cSWang Nan int revent; 662f72f901dSJiri Olsa int evlist_cpu = cpu_map__cpu(evlist->core.cpus, cpu_idx); 66304e21314SAdrian Hunter 664e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 665a5830532SJiri Olsa struct mmap *maps = evlist->mmap; 666078c3386SWang Nan int *output = _output; 667bf8e8f4bSAdrian Hunter int fd; 6689f21b815SMark Rutland int cpu; 669bf8e8f4bSAdrian Hunter 67071f566a3SWang Nan mp->prot = PROT_READ | PROT_WRITE; 6711fc632ceSJiri Olsa if (evsel->core.attr.write_backward) { 6720b72d69aSWang Nan output = _output_overwrite; 6730b72d69aSWang Nan maps = evlist->overwrite_mmap; 674078c3386SWang Nan 675078c3386SWang Nan if (!maps) { 676d50cf361SJiri Olsa maps = evlist__alloc_mmap(evlist, true); 677078c3386SWang Nan if (!maps) 678078c3386SWang Nan return -1; 6790b72d69aSWang Nan evlist->overwrite_mmap = maps; 68054cc54deSWang Nan if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 68154cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 682078c3386SWang Nan } 68371f566a3SWang Nan mp->prot &= ~PROT_WRITE; 684078c3386SWang Nan } 685f3058a1cSWang Nan 686648b5af3SJiri Olsa if (evsel->core.system_wide && thread) 687bf8e8f4bSAdrian Hunter continue; 688bf8e8f4bSAdrian Hunter 689b4df75deSJiri Olsa cpu = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu); 6909f21b815SMark Rutland if (cpu == -1) 6919f21b815SMark Rutland continue; 6929f21b815SMark Rutland 693bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 69404e21314SAdrian Hunter 69504e21314SAdrian Hunter if (*output == -1) { 69604e21314SAdrian Hunter *output = fd; 697078c3386SWang Nan 69831fb4c0dSJiri Olsa if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0) 69904e21314SAdrian Hunter return -1; 70004e21314SAdrian Hunter } else { 70104e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 70204e21314SAdrian Hunter return -1; 70382396986SArnaldo Carvalho de Melo 704078c3386SWang Nan perf_mmap__get(&maps[idx]); 70504e21314SAdrian Hunter } 70604e21314SAdrian Hunter 707f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 708f3058a1cSWang Nan 709f90d194aSAdrian Hunter /* 710f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 711f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 712f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 713f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 714f90d194aSAdrian Hunter * Therefore don't add it for polling. 715f90d194aSAdrian Hunter */ 716648b5af3SJiri Olsa if (!evsel->core.system_wide && 717f4009e7bSJiri Olsa perf_evlist__add_pollfd(&evlist->core, fd, &maps[idx], revent) < 0) { 718078c3386SWang Nan perf_mmap__put(&maps[idx]); 719ad6765ddSArnaldo Carvalho de Melo return -1; 72082396986SArnaldo Carvalho de Melo } 721033fa713SArnaldo Carvalho de Melo 7221fc632ceSJiri Olsa if (evsel->core.attr.read_format & PERF_FORMAT_ID) { 723d5a99483SJiri Olsa if (perf_evlist__id_add_fd(&evlist->core, &evsel->core, cpu, thread, 7243c659eedSAdrian Hunter fd) < 0) 72504e21314SAdrian Hunter return -1; 7263c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 7273c659eedSAdrian Hunter thread); 7283c659eedSAdrian Hunter } 72904e21314SAdrian Hunter } 73004e21314SAdrian Hunter 73104e21314SAdrian Hunter return 0; 73204e21314SAdrian Hunter } 73304e21314SAdrian Hunter 7349521b5f2SJiri Olsa static int evlist__mmap_per_cpu(struct evlist *evlist, 735a8a8f3ebSAdrian Hunter struct mmap_params *mp) 73604e21314SAdrian Hunter { 737aece948fSArnaldo Carvalho de Melo int cpu, thread; 7386549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 739a2f354e3SJiri Olsa int nr_threads = perf_thread_map__nr(evlist->core.threads); 740aece948fSArnaldo Carvalho de Melo 741e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 742b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 743aece948fSArnaldo Carvalho de Melo int output = -1; 7440b72d69aSWang Nan int output_overwrite = -1; 745aece948fSArnaldo Carvalho de Melo 746718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 747718c602dSAdrian Hunter true); 748718c602dSAdrian Hunter 749b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 7509521b5f2SJiri Olsa if (evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 7510b72d69aSWang Nan thread, &output, &output_overwrite)) 752aece948fSArnaldo Carvalho de Melo goto out_unmap; 753aece948fSArnaldo Carvalho de Melo } 754aece948fSArnaldo Carvalho de Melo } 755aece948fSArnaldo Carvalho de Melo 756aece948fSArnaldo Carvalho de Melo return 0; 757aece948fSArnaldo Carvalho de Melo 758aece948fSArnaldo Carvalho de Melo out_unmap: 759db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 760aece948fSArnaldo Carvalho de Melo return -1; 761aece948fSArnaldo Carvalho de Melo } 762aece948fSArnaldo Carvalho de Melo 7639521b5f2SJiri Olsa static int evlist__mmap_per_thread(struct evlist *evlist, 764a8a8f3ebSAdrian Hunter struct mmap_params *mp) 765aece948fSArnaldo Carvalho de Melo { 766aece948fSArnaldo Carvalho de Melo int thread; 767a2f354e3SJiri Olsa int nr_threads = perf_thread_map__nr(evlist->core.threads); 768aece948fSArnaldo Carvalho de Melo 769e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 770b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 771aece948fSArnaldo Carvalho de Melo int output = -1; 7720b72d69aSWang Nan int output_overwrite = -1; 773aece948fSArnaldo Carvalho de Melo 774718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 775718c602dSAdrian Hunter false); 776718c602dSAdrian Hunter 7779521b5f2SJiri Olsa if (evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 7780b72d69aSWang Nan &output, &output_overwrite)) 779aece948fSArnaldo Carvalho de Melo goto out_unmap; 780aece948fSArnaldo Carvalho de Melo } 781aece948fSArnaldo Carvalho de Melo 782aece948fSArnaldo Carvalho de Melo return 0; 783aece948fSArnaldo Carvalho de Melo 784aece948fSArnaldo Carvalho de Melo out_unmap: 785db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 786aece948fSArnaldo Carvalho de Melo return -1; 787aece948fSArnaldo Carvalho de Melo } 788aece948fSArnaldo Carvalho de Melo 789f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 790994a1f78SJiri Olsa { 791f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 7928185e881SArnaldo Carvalho de Melo int max; 7938185e881SArnaldo Carvalho de Melo 7948185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 7958185e881SArnaldo Carvalho de Melo /* 7968185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 7978185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 7988185e881SArnaldo Carvalho de Melo * die yet... 7998185e881SArnaldo Carvalho de Melo */ 8008185e881SArnaldo Carvalho de Melo max = 512; 8018185e881SArnaldo Carvalho de Melo } else { 8028185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 8038185e881SArnaldo Carvalho de Melo } 8048185e881SArnaldo Carvalho de Melo 8058185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 8061be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 8071be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 808f5e7150cSArnaldo Carvalho de Melo 809f5e7150cSArnaldo Carvalho de Melo return pages; 810f5e7150cSArnaldo Carvalho de Melo } 811f5e7150cSArnaldo Carvalho de Melo 8129521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 813f5e7150cSArnaldo Carvalho de Melo { 814f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 815f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 816f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 817994a1f78SJiri Olsa return 0; 818994a1f78SJiri Olsa 819994a1f78SJiri Olsa return (pages + 1) * page_size; 820994a1f78SJiri Olsa } 821994a1f78SJiri Olsa 82233c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 82333c2dcfdSDavid Ahern unsigned long max) 824994a1f78SJiri Olsa { 8252fbe4abeSAdrian Hunter unsigned long pages, val; 82627050f53SJiri Olsa static struct parse_tag tags[] = { 82727050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 82827050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 82927050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 83027050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 83127050f53SJiri Olsa { .tag = 0 }, 83227050f53SJiri Olsa }; 833994a1f78SJiri Olsa 8348973504bSDavid Ahern if (str == NULL) 83533c2dcfdSDavid Ahern return -EINVAL; 8368973504bSDavid Ahern 83727050f53SJiri Olsa val = parse_tag_value(str, tags); 8382fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 83927050f53SJiri Olsa /* we got file size value */ 84027050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 84127050f53SJiri Olsa } else { 84227050f53SJiri Olsa /* we got pages count value */ 84327050f53SJiri Olsa char *eptr; 844994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 84533c2dcfdSDavid Ahern if (*eptr != '\0') 84633c2dcfdSDavid Ahern return -EINVAL; 84727050f53SJiri Olsa } 848994a1f78SJiri Olsa 8492bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 85033c2dcfdSDavid Ahern /* leave number of pages at 0 */ 8511dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 8529808143bSJiri Olsa char buf[100]; 8539808143bSJiri Olsa 85433c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 85591529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 8561dbfa938SAdrian Hunter if (!pages) 8571dbfa938SAdrian Hunter return -EINVAL; 8589808143bSJiri Olsa 8599808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 8609808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 8619808143bSJiri Olsa buf, pages); 8622fbe4abeSAdrian Hunter } 8632fbe4abeSAdrian Hunter 86433c2dcfdSDavid Ahern if (pages > max) 86533c2dcfdSDavid Ahern return -EINVAL; 86633c2dcfdSDavid Ahern 86733c2dcfdSDavid Ahern return pages; 86833c2dcfdSDavid Ahern } 86933c2dcfdSDavid Ahern 870e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 87133c2dcfdSDavid Ahern { 87233c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 87333c2dcfdSDavid Ahern long pages; 87433c2dcfdSDavid Ahern 875f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 87633c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 87733c2dcfdSDavid Ahern 87833c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 87933c2dcfdSDavid Ahern if (pages < 0) { 88033c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 881994a1f78SJiri Olsa return -1; 882994a1f78SJiri Olsa } 883994a1f78SJiri Olsa 884994a1f78SJiri Olsa *mmap_pages = pages; 885994a1f78SJiri Olsa return 0; 886994a1f78SJiri Olsa } 887994a1f78SJiri Olsa 888e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 889e9db1310SAdrian Hunter int unset __maybe_unused) 890e9db1310SAdrian Hunter { 891e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 892e9db1310SAdrian Hunter } 893e9db1310SAdrian Hunter 894c83fa7f2SAdrian Hunter /** 8959521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 896c83fa7f2SAdrian Hunter * @evlist: list of events 897c83fa7f2SAdrian Hunter * @pages: map length in pages 898c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 899718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 900718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 901f8a95309SArnaldo Carvalho de Melo * 902c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 9039521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 904c83fa7f2SAdrian Hunter * automatically. 905f8a95309SArnaldo Carvalho de Melo * 906718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 907718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 908718c602dSAdrian Hunter * 909c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 910f8a95309SArnaldo Carvalho de Melo */ 9119521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 9127a276ff6SWang Nan unsigned int auxtrace_pages, 91351255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 91451255a8aSAlexey Budankov int comp_level) 915f8a95309SArnaldo Carvalho de Melo { 91632dcd021SJiri Olsa struct evsel *evsel; 917f72f901dSJiri Olsa const struct perf_cpu_map *cpus = evlist->core.cpus; 91803617c22SJiri Olsa const struct perf_thread_map *threads = evlist->core.threads; 91971f566a3SWang Nan /* 92071f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 92171f566a3SWang Nan * Its value is decided by evsel's write_backward. 92271f566a3SWang Nan * So &mp should not be passed through const pointer. 92371f566a3SWang Nan */ 92451255a8aSAlexey Budankov struct mmap_params mp = { .nr_cblocks = nr_cblocks, .affinity = affinity, .flush = flush, 92551255a8aSAlexey Budankov .comp_level = comp_level }; 92650a682ceSArnaldo Carvalho de Melo 9278db6d6b1SWang Nan if (!evlist->mmap) 928d50cf361SJiri Olsa evlist->mmap = evlist__alloc_mmap(evlist, false); 9298db6d6b1SWang Nan if (!evlist->mmap) 930f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 931f8a95309SArnaldo Carvalho de Melo 93231f67fc4SJiri Olsa if (evlist->core.pollfd.entries == NULL && perf_evlist__alloc_pollfd(&evlist->core) < 0) 933f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 934f8a95309SArnaldo Carvalho de Melo 935f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 936f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 937f6fa4375SJiri Olsa mp.mask = evlist->core.mmap_len - page_size - 1; 938f8a95309SArnaldo Carvalho de Melo 939f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 940718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 941718c602dSAdrian Hunter 942e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 9431fc632ceSJiri Olsa if ((evsel->core.attr.read_format & PERF_FORMAT_ID) && 9448cd36f3eSJiri Olsa evsel->core.sample_id == NULL && 94570c20369SJiri Olsa perf_evsel__alloc_id(&evsel->core, perf_cpu_map__nr(cpus), threads->nr) < 0) 946f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 947f8a95309SArnaldo Carvalho de Melo } 948f8a95309SArnaldo Carvalho de Melo 949315c0a1fSJiri Olsa if (perf_cpu_map__empty(cpus)) 9509521b5f2SJiri Olsa return evlist__mmap_per_thread(evlist, &mp); 951f8a95309SArnaldo Carvalho de Melo 9529521b5f2SJiri Olsa return evlist__mmap_per_cpu(evlist, &mp); 953f8a95309SArnaldo Carvalho de Melo } 9547e2ed097SArnaldo Carvalho de Melo 9559521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 956718c602dSAdrian Hunter { 9579521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 958718c602dSAdrian Hunter } 959718c602dSAdrian Hunter 96063503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 9617e2ed097SArnaldo Carvalho de Melo { 962147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 963f854839bSJiri Olsa struct perf_cpu_map *cpus; 9649749b90eSJiri Olsa struct perf_thread_map *threads; 9657e2ed097SArnaldo Carvalho de Melo 966147c508fSJin Yao /* 967147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 968147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 969147c508fSJin Yao * target->system_wide = true. 970147c508fSJin Yao * 971147c508fSJin Yao * If specify '--per-thread' only to perf record, 972147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 973147c508fSJin Yao * 974147c508fSJin Yao * So target->per_thread && target->system_wide is false. 975147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 976147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 977147c508fSJin Yao * current behavior. 978147c508fSJin Yao * 979147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 980147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 981147c508fSJin Yao * per-thread data. thread_map__new_str will call 982147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 983147c508fSJin Yao */ 98473c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 985147c508fSJin Yao all_threads); 98674bfd2b2SAdrian Hunter 98774bfd2b2SAdrian Hunter if (!threads) 9887e2ed097SArnaldo Carvalho de Melo return -1; 9897e2ed097SArnaldo Carvalho de Melo 9909c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 991397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 992879d77d0SNamhyung Kim else 9939c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 9947e2ed097SArnaldo Carvalho de Melo 99574bfd2b2SAdrian Hunter if (!cpus) 9967e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 9977e2ed097SArnaldo Carvalho de Melo 998ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 999ec9a77a7SAdrian Hunter 1000453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1001d5bc056eSAdrian Hunter 1002d5bc056eSAdrian Hunter return 0; 10037e2ed097SArnaldo Carvalho de Melo 10047e2ed097SArnaldo Carvalho de Melo out_delete_threads: 10057836e52eSJiri Olsa perf_thread_map__put(threads); 10067e2ed097SArnaldo Carvalho de Melo return -1; 10077e2ed097SArnaldo Carvalho de Melo } 10087e2ed097SArnaldo Carvalho de Melo 100963503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist, 101022c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 101122c8a376SArnaldo Carvalho de Melo { 101232dcd021SJiri Olsa struct evsel *evsel; 101322c8a376SArnaldo Carvalho de Melo 1014e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 101522c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 101622c8a376SArnaldo Carvalho de Melo } 101722c8a376SArnaldo Carvalho de Melo 101863503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist, 101922c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 102022c8a376SArnaldo Carvalho de Melo { 102132dcd021SJiri Olsa struct evsel *evsel; 102222c8a376SArnaldo Carvalho de Melo 1023e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 102422c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 102522c8a376SArnaldo Carvalho de Melo } 102622c8a376SArnaldo Carvalho de Melo 102763503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 10280a102479SFrederic Weisbecker { 102932dcd021SJiri Olsa struct evsel *evsel; 1030745cefc5SArnaldo Carvalho de Melo int err = 0; 10310a102479SFrederic Weisbecker 1032e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1033745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 10340a102479SFrederic Weisbecker continue; 1035745cefc5SArnaldo Carvalho de Melo 1036d988d5eeSKan Liang /* 1037d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1038d988d5eeSKan Liang * So evlist and evsel should always be same. 1039d988d5eeSKan Liang */ 1040a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 104123d4aad4SArnaldo Carvalho de Melo if (err) { 104223d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1043745cefc5SArnaldo Carvalho de Melo break; 10440a102479SFrederic Weisbecker } 104523d4aad4SArnaldo Carvalho de Melo } 10460a102479SFrederic Weisbecker 1047745cefc5SArnaldo Carvalho de Melo return err; 1048745cefc5SArnaldo Carvalho de Melo } 1049745cefc5SArnaldo Carvalho de Melo 105063503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1051745cefc5SArnaldo Carvalho de Melo { 105232dcd021SJiri Olsa struct evsel *evsel; 1053745cefc5SArnaldo Carvalho de Melo int err = 0; 1054745cefc5SArnaldo Carvalho de Melo 105505cea449SArnaldo Carvalho de Melo if (filter == NULL) 105605cea449SArnaldo Carvalho de Melo return -1; 105705cea449SArnaldo Carvalho de Melo 1058e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 10591fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1060fdf14720SWang Nan continue; 1061fdf14720SWang Nan 106294ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1063745cefc5SArnaldo Carvalho de Melo if (err) 1064745cefc5SArnaldo Carvalho de Melo break; 1065745cefc5SArnaldo Carvalho de Melo } 1066745cefc5SArnaldo Carvalho de Melo 1067745cefc5SArnaldo Carvalho de Melo return err; 10680a102479SFrederic Weisbecker } 106974429964SFrederic Weisbecker 107053c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter) 107153c92f73SArnaldo Carvalho de Melo { 107253c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 107353c92f73SArnaldo Carvalho de Melo int err = 0; 107453c92f73SArnaldo Carvalho de Melo 107553c92f73SArnaldo Carvalho de Melo if (filter == NULL) 107653c92f73SArnaldo Carvalho de Melo return -1; 107753c92f73SArnaldo Carvalho de Melo 107853c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 107953c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 108053c92f73SArnaldo Carvalho de Melo continue; 108153c92f73SArnaldo Carvalho de Melo 108253c92f73SArnaldo Carvalho de Melo err = perf_evsel__append_tp_filter(evsel, filter); 108353c92f73SArnaldo Carvalho de Melo if (err) 108453c92f73SArnaldo Carvalho de Melo break; 108553c92f73SArnaldo Carvalho de Melo } 108653c92f73SArnaldo Carvalho de Melo 108753c92f73SArnaldo Carvalho de Melo return err; 108853c92f73SArnaldo Carvalho de Melo } 108953c92f73SArnaldo Carvalho de Melo 109005cea449SArnaldo Carvalho de Melo static char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1091cfd70a26SArnaldo Carvalho de Melo { 1092cfd70a26SArnaldo Carvalho de Melo char *filter; 1093be199adaSArnaldo Carvalho de Melo size_t i; 1094cfd70a26SArnaldo Carvalho de Melo 1095be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1096be199adaSArnaldo Carvalho de Melo if (i == 0) { 1097be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 109805cea449SArnaldo Carvalho de Melo return NULL; 1099be199adaSArnaldo Carvalho de Melo } else { 1100be199adaSArnaldo Carvalho de Melo char *tmp; 1101be199adaSArnaldo Carvalho de Melo 1102be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1103be199adaSArnaldo Carvalho de Melo goto out_free; 1104be199adaSArnaldo Carvalho de Melo 1105be199adaSArnaldo Carvalho de Melo free(filter); 1106be199adaSArnaldo Carvalho de Melo filter = tmp; 1107be199adaSArnaldo Carvalho de Melo } 1108be199adaSArnaldo Carvalho de Melo } 1109cfd70a26SArnaldo Carvalho de Melo 111005cea449SArnaldo Carvalho de Melo return filter; 1111be199adaSArnaldo Carvalho de Melo out_free: 1112cfd70a26SArnaldo Carvalho de Melo free(filter); 111305cea449SArnaldo Carvalho de Melo return NULL; 111405cea449SArnaldo Carvalho de Melo } 111505cea449SArnaldo Carvalho de Melo 111605cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 111705cea449SArnaldo Carvalho de Melo { 111805cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 111905cea449SArnaldo Carvalho de Melo int ret = perf_evlist__set_tp_filter(evlist, filter); 112005cea449SArnaldo Carvalho de Melo 112105cea449SArnaldo Carvalho de Melo free(filter); 1122cfd70a26SArnaldo Carvalho de Melo return ret; 1123cfd70a26SArnaldo Carvalho de Melo } 1124cfd70a26SArnaldo Carvalho de Melo 112563503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1126be199adaSArnaldo Carvalho de Melo { 11277ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1128be199adaSArnaldo Carvalho de Melo } 1129be199adaSArnaldo Carvalho de Melo 11301827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 11311827ab5bSArnaldo Carvalho de Melo { 11321827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 11331827ab5bSArnaldo Carvalho de Melo int ret = perf_evlist__append_tp_filter(evlist, filter); 11341827ab5bSArnaldo Carvalho de Melo 11351827ab5bSArnaldo Carvalho de Melo free(filter); 11361827ab5bSArnaldo Carvalho de Melo return ret; 11371827ab5bSArnaldo Carvalho de Melo } 11381827ab5bSArnaldo Carvalho de Melo 11391827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 11401827ab5bSArnaldo Carvalho de Melo { 11411827ab5bSArnaldo Carvalho de Melo return perf_evlist__append_tp_filter_pids(evlist, 1, &pid); 11421827ab5bSArnaldo Carvalho de Melo } 11431827ab5bSArnaldo Carvalho de Melo 114463503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist) 114574429964SFrederic Weisbecker { 114632dcd021SJiri Olsa struct evsel *pos; 1147c2a70653SArnaldo Carvalho de Melo 11486484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 114975562573SAdrian Hunter return true; 115075562573SAdrian Hunter 115175562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 115275562573SAdrian Hunter return false; 115375562573SAdrian Hunter 1154e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 115575562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 115675562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1157c2a70653SArnaldo Carvalho de Melo return false; 115874429964SFrederic Weisbecker } 115974429964SFrederic Weisbecker 1160c2a70653SArnaldo Carvalho de Melo return true; 1161c2a70653SArnaldo Carvalho de Melo } 1162c2a70653SArnaldo Carvalho de Melo 116363503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist) 1164c2a70653SArnaldo Carvalho de Melo { 116532dcd021SJiri Olsa struct evsel *evsel; 116675562573SAdrian Hunter 116775562573SAdrian Hunter if (evlist->combined_sample_type) 116875562573SAdrian Hunter return evlist->combined_sample_type; 116975562573SAdrian Hunter 1170e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11711fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 117275562573SAdrian Hunter 117375562573SAdrian Hunter return evlist->combined_sample_type; 117475562573SAdrian Hunter } 117575562573SAdrian Hunter 117663503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist) 117775562573SAdrian Hunter { 117875562573SAdrian Hunter evlist->combined_sample_type = 0; 117975562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1180c2a70653SArnaldo Carvalho de Melo } 1181c2a70653SArnaldo Carvalho de Melo 118263503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist) 118398df858eSAndi Kleen { 118432dcd021SJiri Olsa struct evsel *evsel; 118598df858eSAndi Kleen u64 branch_type = 0; 118698df858eSAndi Kleen 1187e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11881fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 118998df858eSAndi Kleen return branch_type; 119098df858eSAndi Kleen } 119198df858eSAndi Kleen 119263503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 11939ede473cSJiri Olsa { 1194515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 11951fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 11961fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 11979ede473cSJiri Olsa 1198e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 11991fc632ceSJiri Olsa if (read_format != pos->core.attr.read_format) 12009ede473cSJiri Olsa return false; 12019ede473cSJiri Olsa } 12029ede473cSJiri Olsa 12039ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 12049ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 12059ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 12069ede473cSJiri Olsa return false; 12079ede473cSJiri Olsa } 12089ede473cSJiri Olsa 12099ede473cSJiri Olsa return true; 12109ede473cSJiri Olsa } 12119ede473cSJiri Olsa 121263503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 121381e36bffSArnaldo Carvalho de Melo { 1214515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 121581e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 121681e36bffSArnaldo Carvalho de Melo u64 sample_type; 121781e36bffSArnaldo Carvalho de Melo u16 size = 0; 121881e36bffSArnaldo Carvalho de Melo 12191fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 122081e36bffSArnaldo Carvalho de Melo goto out; 122181e36bffSArnaldo Carvalho de Melo 12221fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 122381e36bffSArnaldo Carvalho de Melo 122481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 122581e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 122681e36bffSArnaldo Carvalho de Melo 122781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 122881e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 122981e36bffSArnaldo Carvalho de Melo 123081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 123181e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 123281e36bffSArnaldo Carvalho de Melo 123381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 123481e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 123581e36bffSArnaldo Carvalho de Melo 123681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 123781e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 123875562573SAdrian Hunter 123975562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 124075562573SAdrian Hunter size += sizeof(data->id); 124181e36bffSArnaldo Carvalho de Melo out: 124281e36bffSArnaldo Carvalho de Melo return size; 124381e36bffSArnaldo Carvalho de Melo } 124481e36bffSArnaldo Carvalho de Melo 124563503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist) 1246c2a70653SArnaldo Carvalho de Melo { 1247515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1248c2a70653SArnaldo Carvalho de Melo 1249e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 12501fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1251c2a70653SArnaldo Carvalho de Melo return false; 1252c2a70653SArnaldo Carvalho de Melo } 1253c2a70653SArnaldo Carvalho de Melo 1254c2a70653SArnaldo Carvalho de Melo return true; 125574429964SFrederic Weisbecker } 125674429964SFrederic Weisbecker 125763503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist) 125874429964SFrederic Weisbecker { 1259515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 12601fc632ceSJiri Olsa return first->core.attr.sample_id_all; 126174429964SFrederic Weisbecker } 126281cce8deSArnaldo Carvalho de Melo 126363503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 126432dcd021SJiri Olsa struct evsel *evsel) 126581cce8deSArnaldo Carvalho de Melo { 126681cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 126781cce8deSArnaldo Carvalho de Melo } 1268727ab04eSArnaldo Carvalho de Melo 1269750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1270a74b4b66SNamhyung Kim { 127132dcd021SJiri Olsa struct evsel *evsel; 1272a74b4b66SNamhyung Kim 1273475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 127488761fa1SJiri Olsa evsel__close(evsel); 1275a74b4b66SNamhyung Kim } 1276a74b4b66SNamhyung Kim 127763503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 12784112eb18SArnaldo Carvalho de Melo { 1279f854839bSJiri Olsa struct perf_cpu_map *cpus; 12809749b90eSJiri Olsa struct perf_thread_map *threads; 12814112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 12824112eb18SArnaldo Carvalho de Melo 12834112eb18SArnaldo Carvalho de Melo /* 12844112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 12854112eb18SArnaldo Carvalho de Melo * an all cpus map. 12864112eb18SArnaldo Carvalho de Melo * 12874112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 12884112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 12894112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 12904112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 12914112eb18SArnaldo Carvalho de Melo */ 12929c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 12938c0498b6SAdrian Hunter if (!cpus) 12944112eb18SArnaldo Carvalho de Melo goto out; 12954112eb18SArnaldo Carvalho de Melo 12964b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 12978c0498b6SAdrian Hunter if (!threads) 12988c0498b6SAdrian Hunter goto out_put; 12994112eb18SArnaldo Carvalho de Melo 1300453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 13014112eb18SArnaldo Carvalho de Melo out: 13024112eb18SArnaldo Carvalho de Melo return err; 13038c0498b6SAdrian Hunter out_put: 130438f01d8dSJiri Olsa perf_cpu_map__put(cpus); 13054112eb18SArnaldo Carvalho de Melo goto out; 13064112eb18SArnaldo Carvalho de Melo } 13074112eb18SArnaldo Carvalho de Melo 1308474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1309727ab04eSArnaldo Carvalho de Melo { 131032dcd021SJiri Olsa struct evsel *evsel; 1311a74b4b66SNamhyung Kim int err; 1312727ab04eSArnaldo Carvalho de Melo 13134112eb18SArnaldo Carvalho de Melo /* 13144112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 13154112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 13164112eb18SArnaldo Carvalho de Melo */ 131703617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 13184112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 13194112eb18SArnaldo Carvalho de Melo if (err < 0) 13204112eb18SArnaldo Carvalho de Melo goto out_err; 13214112eb18SArnaldo Carvalho de Melo } 13224112eb18SArnaldo Carvalho de Melo 1323733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1324733cd2feSAdrian Hunter 1325e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1326af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1327727ab04eSArnaldo Carvalho de Melo if (err < 0) 1328727ab04eSArnaldo Carvalho de Melo goto out_err; 1329727ab04eSArnaldo Carvalho de Melo } 1330727ab04eSArnaldo Carvalho de Melo 1331727ab04eSArnaldo Carvalho de Melo return 0; 1332727ab04eSArnaldo Carvalho de Melo out_err: 1333750b4edeSJiri Olsa evlist__close(evlist); 133441c21a68SNamhyung Kim errno = -err; 1335727ab04eSArnaldo Carvalho de Melo return err; 1336727ab04eSArnaldo Carvalho de Melo } 133735b9d88eSArnaldo Carvalho de Melo 133863503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 133955e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1340735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 134135b9d88eSArnaldo Carvalho de Melo { 134235b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 134335b9d88eSArnaldo Carvalho de Melo char bf; 134435b9d88eSArnaldo Carvalho de Melo 134535b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 134635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 134735b9d88eSArnaldo Carvalho de Melo return -1; 134835b9d88eSArnaldo Carvalho de Melo } 134935b9d88eSArnaldo Carvalho de Melo 135035b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 135135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 135235b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 135335b9d88eSArnaldo Carvalho de Melo } 135435b9d88eSArnaldo Carvalho de Melo 135535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 135635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 135735b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 135835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 135935b9d88eSArnaldo Carvalho de Melo } 136035b9d88eSArnaldo Carvalho de Melo 136135b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13625f1c4225SArnaldo Carvalho de Melo int ret; 13635f1c4225SArnaldo Carvalho de Melo 1364119fa3c9SNamhyung Kim if (pipe_output) 136535b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 136635b9d88eSArnaldo Carvalho de Melo 13670817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13680817df08SDavid Ahern 136935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 137035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 137135b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 137235b9d88eSArnaldo Carvalho de Melo 137335b9d88eSArnaldo Carvalho de Melo /* 137435b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 137535b9d88eSArnaldo Carvalho de Melo */ 137635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 137735b9d88eSArnaldo Carvalho de Melo 137835b9d88eSArnaldo Carvalho de Melo /* 137935b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 138035b9d88eSArnaldo Carvalho de Melo */ 13815f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 13825f1c4225SArnaldo Carvalho de Melo /* 13835f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 13845f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 13855f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 13865f1c4225SArnaldo Carvalho de Melo * 138720f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 13885f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 13895f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 13905f1c4225SArnaldo Carvalho de Melo * here. 13915f1c4225SArnaldo Carvalho de Melo */ 13925f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 13935f1c4225SArnaldo Carvalho de Melo if (ret == -1) 139435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 13955f1c4225SArnaldo Carvalho de Melo exit(ret); 13965f1c4225SArnaldo Carvalho de Melo } 139735b9d88eSArnaldo Carvalho de Melo 139835b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 139935b9d88eSArnaldo Carvalho de Melo 1400735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1401f33cbe72SArnaldo Carvalho de Melo union sigval val; 1402f33cbe72SArnaldo Carvalho de Melo 1403f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1404f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 140535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1406f33cbe72SArnaldo Carvalho de Melo } else 1407f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 140835b9d88eSArnaldo Carvalho de Melo exit(-1); 140935b9d88eSArnaldo Carvalho de Melo } 141035b9d88eSArnaldo Carvalho de Melo 1411735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1412735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1413735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1414735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1415735f7e0bSArnaldo Carvalho de Melo }; 1416735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1417735f7e0bSArnaldo Carvalho de Melo } 1418735f7e0bSArnaldo Carvalho de Melo 14191aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 142003617c22SJiri Olsa if (evlist->core.threads == NULL) { 14211aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 14221aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 14231aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 14241aaf63b1SArnaldo Carvalho de Melo } 142503617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 14261aaf63b1SArnaldo Carvalho de Melo } 142735b9d88eSArnaldo Carvalho de Melo 142835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 142935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 143035b9d88eSArnaldo Carvalho de Melo /* 143135b9d88eSArnaldo Carvalho de Melo * wait for child to settle 143235b9d88eSArnaldo Carvalho de Melo */ 143335b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 143435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 143535b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 143635b9d88eSArnaldo Carvalho de Melo } 143735b9d88eSArnaldo Carvalho de Melo 1438bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 143935b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 144035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 144135b9d88eSArnaldo Carvalho de Melo return 0; 144235b9d88eSArnaldo Carvalho de Melo 144335b9d88eSArnaldo Carvalho de Melo out_close_pipes: 144435b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 144535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 144635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 144735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 144835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 144935b9d88eSArnaldo Carvalho de Melo return -1; 145035b9d88eSArnaldo Carvalho de Melo } 145135b9d88eSArnaldo Carvalho de Melo 145263503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist) 145335b9d88eSArnaldo Carvalho de Melo { 145435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1455b3824404SDavid Ahern char bf = 0; 1456bcf3145fSNamhyung Kim int ret; 145735b9d88eSArnaldo Carvalho de Melo /* 145835b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 145935b9d88eSArnaldo Carvalho de Melo */ 1460bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1461bcf3145fSNamhyung Kim if (ret < 0) 1462e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1463bcf3145fSNamhyung Kim 1464bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1465bcf3145fSNamhyung Kim return ret; 146635b9d88eSArnaldo Carvalho de Melo } 146735b9d88eSArnaldo Carvalho de Melo 146835b9d88eSArnaldo Carvalho de Melo return 0; 146935b9d88eSArnaldo Carvalho de Melo } 1470cb0b29e0SArnaldo Carvalho de Melo 147163503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 14720807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1473cb0b29e0SArnaldo Carvalho de Melo { 147432dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 147575562573SAdrian Hunter 147675562573SAdrian Hunter if (!evsel) 147775562573SAdrian Hunter return -EFAULT; 14780807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1479cb0b29e0SArnaldo Carvalho de Melo } 148078f067b3SArnaldo Carvalho de Melo 148163503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 148201468120SJiri Olsa union perf_event *event, 148301468120SJiri Olsa u64 *timestamp) 148401468120SJiri Olsa { 148532dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 148601468120SJiri Olsa 148701468120SJiri Olsa if (!evsel) 148801468120SJiri Olsa return -EFAULT; 148901468120SJiri Olsa return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); 149001468120SJiri Olsa } 149101468120SJiri Olsa 149263503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist, 1493a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1494a8f23d8fSArnaldo Carvalho de Melo { 1495a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1496c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1497a8f23d8fSArnaldo Carvalho de Melo 1498a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1499a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1500a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1501a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1502a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1503a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1504a8f23d8fSArnaldo Carvalho de Melo 15051a47245dSAdrian Hunter value = perf_event_paranoid(); 1506a8f23d8fSArnaldo Carvalho de Melo 1507a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1508a8f23d8fSArnaldo Carvalho de Melo 1509a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1510a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1511a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1512a8f23d8fSArnaldo Carvalho de Melo } 1513a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15145229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1515a8f23d8fSArnaldo Carvalho de Melo 1516a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15175229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 15185229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1519a8f23d8fSArnaldo Carvalho de Melo break; 1520d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1521515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1522d9aade7fSArnaldo Carvalho de Melo int max_freq; 1523d9aade7fSArnaldo Carvalho de Melo 1524d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1525d9aade7fSArnaldo Carvalho de Melo goto out_default; 1526d9aade7fSArnaldo Carvalho de Melo 15271fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1528d9aade7fSArnaldo Carvalho de Melo goto out_default; 1529d9aade7fSArnaldo Carvalho de Melo 1530d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1531d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1532d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1533d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 15341fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1535d9aade7fSArnaldo Carvalho de Melo break; 1536d9aade7fSArnaldo Carvalho de Melo } 1537a8f23d8fSArnaldo Carvalho de Melo default: 1538d9aade7fSArnaldo Carvalho de Melo out_default: 1539a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1540a8f23d8fSArnaldo Carvalho de Melo break; 1541a8f23d8fSArnaldo Carvalho de Melo } 1542a8f23d8fSArnaldo Carvalho de Melo 1543a8f23d8fSArnaldo Carvalho de Melo return 0; 1544a8f23d8fSArnaldo Carvalho de Melo } 1545a025e4f0SAdrian Hunter 154663503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1547956fa571SArnaldo Carvalho de Melo { 1548c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1549f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1550956fa571SArnaldo Carvalho de Melo 1551956fa571SArnaldo Carvalho de Melo switch (err) { 1552956fa571SArnaldo Carvalho de Melo case EPERM: 1553e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1554e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1555e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1556956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1557e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1558e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1559e965bea1SArnaldo Carvalho de Melo 1560e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1561e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1562e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1563e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1564e965bea1SArnaldo Carvalho de Melo } 1565e965bea1SArnaldo Carvalho de Melo 1566e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1567e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1568956fa571SArnaldo Carvalho de Melo break; 1569956fa571SArnaldo Carvalho de Melo default: 1570956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1571956fa571SArnaldo Carvalho de Melo break; 1572956fa571SArnaldo Carvalho de Melo } 1573956fa571SArnaldo Carvalho de Melo 1574956fa571SArnaldo Carvalho de Melo return 0; 1575956fa571SArnaldo Carvalho de Melo } 1576956fa571SArnaldo Carvalho de Melo 157763503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 157832dcd021SJiri Olsa struct evsel *move_evsel) 1579a025e4f0SAdrian Hunter { 158032dcd021SJiri Olsa struct evsel *evsel, *n; 1581a025e4f0SAdrian Hunter LIST_HEAD(move); 1582a025e4f0SAdrian Hunter 1583515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1584a025e4f0SAdrian Hunter return; 1585a025e4f0SAdrian Hunter 1586e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1587a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1588b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1589a025e4f0SAdrian Hunter } 1590a025e4f0SAdrian Hunter 1591ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1592a025e4f0SAdrian Hunter } 159360b0896cSAdrian Hunter 159463503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist, 159532dcd021SJiri Olsa struct evsel *tracking_evsel) 159660b0896cSAdrian Hunter { 159732dcd021SJiri Olsa struct evsel *evsel; 159860b0896cSAdrian Hunter 159960b0896cSAdrian Hunter if (tracking_evsel->tracking) 160060b0896cSAdrian Hunter return; 160160b0896cSAdrian Hunter 1602e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 160360b0896cSAdrian Hunter if (evsel != tracking_evsel) 160460b0896cSAdrian Hunter evsel->tracking = false; 160560b0896cSAdrian Hunter } 160660b0896cSAdrian Hunter 160760b0896cSAdrian Hunter tracking_evsel->tracking = true; 160860b0896cSAdrian Hunter } 16097630b3e2SWang Nan 161032dcd021SJiri Olsa struct evsel * 161163503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist, 16127630b3e2SWang Nan const char *str) 16137630b3e2SWang Nan { 161432dcd021SJiri Olsa struct evsel *evsel; 16157630b3e2SWang Nan 1616e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16177630b3e2SWang Nan if (!evsel->name) 16187630b3e2SWang Nan continue; 16197630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 16207630b3e2SWang Nan return evsel; 16217630b3e2SWang Nan } 16227630b3e2SWang Nan 16237630b3e2SWang Nan return NULL; 16247630b3e2SWang Nan } 162554cc54deSWang Nan 162663503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 162754cc54deSWang Nan enum bkw_mmap_state state) 162854cc54deSWang Nan { 162954cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 163054cc54deSWang Nan enum action { 163154cc54deSWang Nan NONE, 163254cc54deSWang Nan PAUSE, 163354cc54deSWang Nan RESUME, 163454cc54deSWang Nan } action = NONE; 163554cc54deSWang Nan 16360b72d69aSWang Nan if (!evlist->overwrite_mmap) 163754cc54deSWang Nan return; 163854cc54deSWang Nan 163954cc54deSWang Nan switch (old_state) { 164054cc54deSWang Nan case BKW_MMAP_NOTREADY: { 164154cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1642dd8bd53aSLuis de Bethencourt goto state_err; 164354cc54deSWang Nan break; 164454cc54deSWang Nan } 164554cc54deSWang Nan case BKW_MMAP_RUNNING: { 164654cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 164754cc54deSWang Nan goto state_err; 164854cc54deSWang Nan action = PAUSE; 164954cc54deSWang Nan break; 165054cc54deSWang Nan } 165154cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 165254cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 165354cc54deSWang Nan goto state_err; 165454cc54deSWang Nan break; 165554cc54deSWang Nan } 165654cc54deSWang Nan case BKW_MMAP_EMPTY: { 165754cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 165854cc54deSWang Nan goto state_err; 165954cc54deSWang Nan action = RESUME; 166054cc54deSWang Nan break; 166154cc54deSWang Nan } 166254cc54deSWang Nan default: 166354cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 166454cc54deSWang Nan } 166554cc54deSWang Nan 166654cc54deSWang Nan evlist->bkw_mmap_state = state; 166754cc54deSWang Nan 166854cc54deSWang Nan switch (action) { 166954cc54deSWang Nan case PAUSE: 167054cc54deSWang Nan perf_evlist__pause(evlist); 167154cc54deSWang Nan break; 167254cc54deSWang Nan case RESUME: 167354cc54deSWang Nan perf_evlist__resume(evlist); 167454cc54deSWang Nan break; 167554cc54deSWang Nan case NONE: 167654cc54deSWang Nan default: 167754cc54deSWang Nan break; 167854cc54deSWang Nan } 167954cc54deSWang Nan 168054cc54deSWang Nan state_err: 168154cc54deSWang Nan return; 168254cc54deSWang Nan } 168307d6f446SArnaldo Carvalho de Melo 168463503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 168507d6f446SArnaldo Carvalho de Melo { 168632dcd021SJiri Olsa struct evsel *evsel; 168707d6f446SArnaldo Carvalho de Melo 168807d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16891fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 169007d6f446SArnaldo Carvalho de Melo return false; 169107d6f446SArnaldo Carvalho de Melo } 169207d6f446SArnaldo Carvalho de Melo 169307d6f446SArnaldo Carvalho de Melo return true; 169407d6f446SArnaldo Carvalho de Melo } 1695e2bdbe80SJin Yao 1696e2bdbe80SJin Yao /* 1697e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1698e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1699e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1700e2bdbe80SJin Yao */ 170163503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1702e2bdbe80SJin Yao { 1703e2bdbe80SJin Yao if (!evlist->nr_groups) { 1704515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1705e2bdbe80SJin Yao 1706e2bdbe80SJin Yao perf_evlist__set_leader(evlist); 1707e2bdbe80SJin Yao leader->forced_leader = true; 1708e2bdbe80SJin Yao } 1709e2bdbe80SJin Yao } 1710c3537fc2SAndi Kleen 171163503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 171232dcd021SJiri Olsa struct evsel *evsel) 1713c3537fc2SAndi Kleen { 171432dcd021SJiri Olsa struct evsel *c2, *leader; 1715c3537fc2SAndi Kleen bool is_open = true; 1716c3537fc2SAndi Kleen 1717c3537fc2SAndi Kleen leader = evsel->leader; 1718c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 17195643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1720c3537fc2SAndi Kleen 1721c3537fc2SAndi Kleen /* 1722c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1723c3537fc2SAndi Kleen * include the first entry. 1724c3537fc2SAndi Kleen */ 1725c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1726c3537fc2SAndi Kleen if (c2 == evsel) 1727c3537fc2SAndi Kleen is_open = false; 1728c3537fc2SAndi Kleen if (c2->leader == leader) { 1729c3537fc2SAndi Kleen if (is_open) 17307834fa94SAndi Kleen perf_evsel__close(&evsel->core); 1731c3537fc2SAndi Kleen c2->leader = c2; 17325643b1a5SJiri Olsa c2->core.nr_members = 0; 1733c3537fc2SAndi Kleen } 1734c3537fc2SAndi Kleen } 1735c3537fc2SAndi Kleen return leader; 1736c3537fc2SAndi Kleen } 1737657ee553SSong Liu 173863503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist, 1739657ee553SSong Liu struct perf_event_attr *attr, 1740657ee553SSong Liu perf_evsel__sb_cb_t cb, 1741657ee553SSong Liu void *data) 1742657ee553SSong Liu { 174332dcd021SJiri Olsa struct evsel *evsel; 1744657ee553SSong Liu bool new_evlist = (*evlist) == NULL; 1745657ee553SSong Liu 1746657ee553SSong Liu if (*evlist == NULL) 17470f98b11cSJiri Olsa *evlist = evlist__new(); 1748657ee553SSong Liu if (*evlist == NULL) 1749657ee553SSong Liu return -1; 1750657ee553SSong Liu 1751657ee553SSong Liu if (!attr->sample_id_all) { 1752657ee553SSong Liu pr_warning("enabling sample_id_all for all side band events\n"); 1753657ee553SSong Liu attr->sample_id_all = 1; 1754657ee553SSong Liu } 1755657ee553SSong Liu 17566484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries); 1757657ee553SSong Liu if (!evsel) 1758657ee553SSong Liu goto out_err; 1759657ee553SSong Liu 1760657ee553SSong Liu evsel->side_band.cb = cb; 1761657ee553SSong Liu evsel->side_band.data = data; 1762a1cf3a75SJiri Olsa evlist__add(*evlist, evsel); 1763657ee553SSong Liu return 0; 1764657ee553SSong Liu 1765657ee553SSong Liu out_err: 1766657ee553SSong Liu if (new_evlist) { 1767c12995a5SJiri Olsa evlist__delete(*evlist); 1768657ee553SSong Liu *evlist = NULL; 1769657ee553SSong Liu } 1770657ee553SSong Liu return -1; 1771657ee553SSong Liu } 1772657ee553SSong Liu 1773657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg) 1774657ee553SSong Liu { 177563503dbaSJiri Olsa struct evlist *evlist = arg; 1776657ee553SSong Liu bool draining = false; 1777adc6257cSJiri Olsa int i, done = 0; 1778b397f846SArnaldo Carvalho de Melo /* 1779b397f846SArnaldo Carvalho de Melo * In order to read symbols from other namespaces perf to needs to call 1780b397f846SArnaldo Carvalho de Melo * setns(2). This isn't permitted if the struct_fs has multiple users. 1781b397f846SArnaldo Carvalho de Melo * unshare(2) the fs so that we may continue to setns into namespaces 1782b397f846SArnaldo Carvalho de Melo * that we're observing when, for instance, reading the build-ids at 1783b397f846SArnaldo Carvalho de Melo * the end of a 'perf record' session. 1784b397f846SArnaldo Carvalho de Melo */ 1785b397f846SArnaldo Carvalho de Melo unshare(CLONE_FS); 1786657ee553SSong Liu 1787adc6257cSJiri Olsa while (!done) { 1788adc6257cSJiri Olsa bool got_data = false; 1789adc6257cSJiri Olsa 1790adc6257cSJiri Olsa if (evlist->thread.done) 1791657ee553SSong Liu draining = true; 1792657ee553SSong Liu 1793657ee553SSong Liu if (!draining) 179480ab2987SJiri Olsa evlist__poll(evlist, 1000); 1795657ee553SSong Liu 1796c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 1797a5830532SJiri Olsa struct mmap *map = &evlist->mmap[i]; 1798657ee553SSong Liu union perf_event *event; 1799657ee553SSong Liu 1800657ee553SSong Liu if (perf_mmap__read_init(map)) 1801657ee553SSong Liu continue; 1802657ee553SSong Liu while ((event = perf_mmap__read_event(map)) != NULL) { 180332dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1804657ee553SSong Liu 1805657ee553SSong Liu if (evsel && evsel->side_band.cb) 1806657ee553SSong Liu evsel->side_band.cb(event, evsel->side_band.data); 1807657ee553SSong Liu else 1808657ee553SSong Liu pr_warning("cannot locate proper evsel for the side band event\n"); 1809657ee553SSong Liu 1810657ee553SSong Liu perf_mmap__consume(map); 1811adc6257cSJiri Olsa got_data = true; 1812657ee553SSong Liu } 1813657ee553SSong Liu perf_mmap__read_done(map); 1814657ee553SSong Liu } 1815adc6257cSJiri Olsa 1816adc6257cSJiri Olsa if (draining && !got_data) 1817adc6257cSJiri Olsa break; 1818657ee553SSong Liu } 1819657ee553SSong Liu return NULL; 1820657ee553SSong Liu } 1821657ee553SSong Liu 182263503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist, 1823657ee553SSong Liu struct target *target) 1824657ee553SSong Liu { 182532dcd021SJiri Olsa struct evsel *counter; 1826657ee553SSong Liu 1827657ee553SSong Liu if (!evlist) 1828657ee553SSong Liu return 0; 1829657ee553SSong Liu 1830657ee553SSong Liu if (perf_evlist__create_maps(evlist, target)) 1831657ee553SSong Liu goto out_delete_evlist; 1832657ee553SSong Liu 1833657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1834f72f901dSJiri Olsa if (evsel__open(counter, evlist->core.cpus, 183503617c22SJiri Olsa evlist->core.threads) < 0) 1836657ee553SSong Liu goto out_delete_evlist; 1837657ee553SSong Liu } 1838657ee553SSong Liu 18399521b5f2SJiri Olsa if (evlist__mmap(evlist, UINT_MAX)) 1840657ee553SSong Liu goto out_delete_evlist; 1841657ee553SSong Liu 1842657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1843ec7f24efSJiri Olsa if (evsel__enable(counter)) 1844657ee553SSong Liu goto out_delete_evlist; 1845657ee553SSong Liu } 1846657ee553SSong Liu 1847657ee553SSong Liu evlist->thread.done = 0; 1848657ee553SSong Liu if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist)) 1849657ee553SSong Liu goto out_delete_evlist; 1850657ee553SSong Liu 1851657ee553SSong Liu return 0; 1852657ee553SSong Liu 1853657ee553SSong Liu out_delete_evlist: 1854c12995a5SJiri Olsa evlist__delete(evlist); 1855657ee553SSong Liu evlist = NULL; 1856657ee553SSong Liu return -1; 1857657ee553SSong Liu } 1858657ee553SSong Liu 185963503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist) 1860657ee553SSong Liu { 1861657ee553SSong Liu if (!evlist) 1862657ee553SSong Liu return; 1863657ee553SSong Liu evlist->thread.done = 1; 1864657ee553SSong Liu pthread_join(evlist->thread.th, NULL); 1865c12995a5SJiri Olsa evlist__delete(evlist); 1866657ee553SSong Liu } 1867