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" 13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1412864b31SNamhyung Kim #include "target.h" 15361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 16361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 17e3e1a54fSAdrian Hunter #include "debug.h" 1858db1d6eSArnaldo Carvalho de Melo #include "units.h" 1954cc54deSWang Nan #include "asm/bug.h" 20657ee553SSong Liu #include "bpf-event.h" 219607ad3aSArnaldo Carvalho de Melo #include <signal.h> 2235b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 23361c99a6SArnaldo Carvalho de Melo 2450d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 254b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 2650d08e47SArnaldo Carvalho de Melo 27bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 2886a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 29f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 30f8a95309SArnaldo Carvalho de Melo 3170db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 3270db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 330389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 348dd2a131SJiri Olsa #include <linux/err.h> 357f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 364562a739SJiri Olsa #include <perf/evlist.h> 3788761fa1SJiri Olsa #include <perf/evsel.h> 389c3516d1SJiri Olsa #include <perf/cpumap.h> 3970db7533SArnaldo Carvalho de Melo 40748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 41748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 42748fe088SArnaldo Carvalho de Melo #endif 43748fe088SArnaldo Carvalho de Melo 449dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 45a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 46f8a95309SArnaldo Carvalho de Melo 4752c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 489749b90eSJiri Olsa struct perf_thread_map *threads) 49361c99a6SArnaldo Carvalho de Melo { 5070db7533SArnaldo Carvalho de Melo int i; 5170db7533SArnaldo Carvalho de Melo 5270db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 5370db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 544562a739SJiri Olsa perf_evlist__init(&evlist->core); 55453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 561b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 5735b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 5854cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 59361c99a6SArnaldo Carvalho de Melo } 60361c99a6SArnaldo Carvalho de Melo 610f98b11cSJiri Olsa struct evlist *evlist__new(void) 62ef1d1af2SArnaldo Carvalho de Melo { 6363503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist)); 64ef1d1af2SArnaldo Carvalho de Melo 65ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 6652c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL); 67ef1d1af2SArnaldo Carvalho de Melo 68361c99a6SArnaldo Carvalho de Melo return evlist; 69361c99a6SArnaldo Carvalho de Melo } 70361c99a6SArnaldo Carvalho de Melo 7163503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void) 72b22d54b0SJiri Olsa { 730f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 74b22d54b0SJiri Olsa 75b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 76c12995a5SJiri Olsa evlist__delete(evlist); 77b22d54b0SJiri Olsa evlist = NULL; 78b22d54b0SJiri Olsa } 79b22d54b0SJiri Olsa 80b22d54b0SJiri Olsa return evlist; 81b22d54b0SJiri Olsa } 82b22d54b0SJiri Olsa 8363503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void) 845bae0250SArnaldo Carvalho de Melo { 850f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 865bae0250SArnaldo Carvalho de Melo 875bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 88c12995a5SJiri Olsa evlist__delete(evlist); 895bae0250SArnaldo Carvalho de Melo evlist = NULL; 905bae0250SArnaldo Carvalho de Melo } 915bae0250SArnaldo Carvalho de Melo 925bae0250SArnaldo Carvalho de Melo return evlist; 935bae0250SArnaldo Carvalho de Melo } 945bae0250SArnaldo Carvalho de Melo 9575562573SAdrian Hunter /** 9675562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 9775562573SAdrian Hunter * @evlist: selected event list 9875562573SAdrian Hunter * 9975562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 10075562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 10175562573SAdrian Hunter */ 10263503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist) 10375562573SAdrian Hunter { 10432dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 10575562573SAdrian Hunter 10675562573SAdrian Hunter evlist->id_pos = first->id_pos; 10775562573SAdrian Hunter evlist->is_pos = first->is_pos; 10875562573SAdrian Hunter } 10975562573SAdrian Hunter 11063503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist) 111733cd2feSAdrian Hunter { 11232dcd021SJiri Olsa struct evsel *evsel; 113733cd2feSAdrian Hunter 114e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 115733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 116733cd2feSAdrian Hunter 117733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 118733cd2feSAdrian Hunter } 119733cd2feSAdrian Hunter 12063503dbaSJiri Olsa static void perf_evlist__purge(struct evlist *evlist) 121361c99a6SArnaldo Carvalho de Melo { 12232dcd021SJiri Olsa struct evsel *pos, *n; 123361c99a6SArnaldo Carvalho de Melo 124e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 125b27c4eceSJiri Olsa list_del_init(&pos->core.node); 126d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 1275eb2dd2aSJiri Olsa evsel__delete(pos); 128361c99a6SArnaldo Carvalho de Melo } 129361c99a6SArnaldo Carvalho de Melo 1306484d2f9SJiri Olsa evlist->core.nr_entries = 0; 131361c99a6SArnaldo Carvalho de Melo } 132361c99a6SArnaldo Carvalho de Melo 13363503dbaSJiri Olsa void perf_evlist__exit(struct evlist *evlist) 134ef1d1af2SArnaldo Carvalho de Melo { 13504662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1360b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 1371b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 138ef1d1af2SArnaldo Carvalho de Melo } 139ef1d1af2SArnaldo Carvalho de Melo 140c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist) 141361c99a6SArnaldo Carvalho de Melo { 1420b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1430b04b3dcSArnaldo Carvalho de Melo return; 1440b04b3dcSArnaldo Carvalho de Melo 145983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 146750b4edeSJiri Olsa evlist__close(evlist); 147f72f901dSJiri Olsa perf_cpu_map__put(evlist->core.cpus); 14803617c22SJiri Olsa perf_thread_map__put(evlist->core.threads); 149f72f901dSJiri Olsa evlist->core.cpus = NULL; 15003617c22SJiri Olsa evlist->core.threads = NULL; 151361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 152ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 153361c99a6SArnaldo Carvalho de Melo free(evlist); 154361c99a6SArnaldo Carvalho de Melo } 155361c99a6SArnaldo Carvalho de Melo 156a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 157361c99a6SArnaldo Carvalho de Melo { 158d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1596484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 16060b0896cSAdrian Hunter entry->tracking = !entry->idx; 161ef503831SArnaldo Carvalho de Melo 1626484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1636484d2f9SJiri Olsa 1646484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 16575562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 166361c99a6SArnaldo Carvalho de Melo } 167361c99a6SArnaldo Carvalho de Melo 16816251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1694768230aSAdrian Hunter { 1704768230aSAdrian Hunter evsel->evlist = NULL; 17152e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1724768230aSAdrian Hunter } 1734768230aSAdrian Hunter 17463503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist, 175f114d6efSAdrian Hunter struct list_head *list) 17650d08e47SArnaldo Carvalho de Melo { 17732dcd021SJiri Olsa struct evsel *evsel, *temp; 17875562573SAdrian Hunter 179e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 180b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 181a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 182f114d6efSAdrian Hunter } 18350d08e47SArnaldo Carvalho de Melo } 18450d08e47SArnaldo Carvalho de Melo 18563dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 18663dab225SArnaldo Carvalho de Melo { 18732dcd021SJiri Olsa struct evsel *evsel, *leader; 18863dab225SArnaldo Carvalho de Melo 189b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 190b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 19197f63e4aSNamhyung Kim 1925643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 19363dab225SArnaldo Carvalho de Melo 194e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 19563dab225SArnaldo Carvalho de Melo evsel->leader = leader; 19663dab225SArnaldo Carvalho de Melo } 19763dab225SArnaldo Carvalho de Melo } 19863dab225SArnaldo Carvalho de Melo 19963503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist) 2006a4bb04cSJiri Olsa { 2016484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2026484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 203ce9036a6SJiri Olsa __perf_evlist__set_leader(&evlist->core.entries); 2046a4bb04cSJiri Olsa } 20597f63e4aSNamhyung Kim } 2066a4bb04cSJiri Olsa 20763503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise) 208361c99a6SArnaldo Carvalho de Melo { 20932dcd021SJiri Olsa struct evsel *evsel = perf_evsel__new_cycles(precise); 210361c99a6SArnaldo Carvalho de Melo 211361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2127c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 213361c99a6SArnaldo Carvalho de Melo 214a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 215361c99a6SArnaldo Carvalho de Melo return 0; 216361c99a6SArnaldo Carvalho de Melo } 2175c581041SArnaldo Carvalho de Melo 21863503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist) 2195bae0250SArnaldo Carvalho de Melo { 2205bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2215bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2225bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2235bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2245bae0250SArnaldo Carvalho de Melo }; 2256484d2f9SJiri Olsa struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries); 2265bae0250SArnaldo Carvalho de Melo 2275bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2285bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2295bae0250SArnaldo Carvalho de Melo 230a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2315bae0250SArnaldo Carvalho de Melo return 0; 2325bae0250SArnaldo Carvalho de Melo } 2335bae0250SArnaldo Carvalho de Melo 234a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist, 23550d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 23650d08e47SArnaldo Carvalho de Melo { 23732dcd021SJiri Olsa struct evsel *evsel, *n; 23850d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 23950d08e47SArnaldo Carvalho de Melo size_t i; 24050d08e47SArnaldo Carvalho de Melo 24150d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2426484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 24350d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 24450d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 245b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 24650d08e47SArnaldo Carvalho de Melo } 24750d08e47SArnaldo Carvalho de Melo 248f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 24950d08e47SArnaldo Carvalho de Melo 25050d08e47SArnaldo Carvalho de Melo return 0; 25150d08e47SArnaldo Carvalho de Melo 25250d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 253e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2545eb2dd2aSJiri Olsa evsel__delete(evsel); 25550d08e47SArnaldo Carvalho de Melo return -1; 25650d08e47SArnaldo Carvalho de Melo } 25750d08e47SArnaldo Carvalho de Melo 25863503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist, 25979695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 26079695e1bSArnaldo Carvalho de Melo { 26179695e1bSArnaldo Carvalho de Melo size_t i; 26279695e1bSArnaldo Carvalho de Melo 26379695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 26479695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 26579695e1bSArnaldo Carvalho de Melo 266a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 26779695e1bSArnaldo Carvalho de Melo } 26879695e1bSArnaldo Carvalho de Melo 26932dcd021SJiri Olsa struct evsel * 27063503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 271ee29be62SArnaldo Carvalho de Melo { 27232dcd021SJiri Olsa struct evsel *evsel; 273ee29be62SArnaldo Carvalho de Melo 274e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 2751fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 2761fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 277ee29be62SArnaldo Carvalho de Melo return evsel; 278ee29be62SArnaldo Carvalho de Melo } 279ee29be62SArnaldo Carvalho de Melo 280ee29be62SArnaldo Carvalho de Melo return NULL; 281ee29be62SArnaldo Carvalho de Melo } 282ee29be62SArnaldo Carvalho de Melo 28332dcd021SJiri Olsa struct evsel * 28463503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist, 285a2f2804aSDavid Ahern const char *name) 286a2f2804aSDavid Ahern { 28732dcd021SJiri Olsa struct evsel *evsel; 288a2f2804aSDavid Ahern 289e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 2901fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 291a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 292a2f2804aSDavid Ahern return evsel; 293a2f2804aSDavid Ahern } 294a2f2804aSDavid Ahern 295a2f2804aSDavid Ahern return NULL; 296a2f2804aSDavid Ahern } 297a2f2804aSDavid Ahern 29863503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist, 29939876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 30039876e7dSArnaldo Carvalho de Melo { 30132dcd021SJiri Olsa struct evsel *evsel = perf_evsel__newtp(sys, name); 30239876e7dSArnaldo Carvalho de Melo 3038dd2a131SJiri Olsa if (IS_ERR(evsel)) 30439876e7dSArnaldo Carvalho de Melo return -1; 30539876e7dSArnaldo Carvalho de Melo 306744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 307a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 30839876e7dSArnaldo Carvalho de Melo return 0; 30939876e7dSArnaldo Carvalho de Melo } 31039876e7dSArnaldo Carvalho de Melo 31163503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist, 31232dcd021SJiri Olsa struct evsel *evsel) 313bf8e8f4bSAdrian Hunter { 314bf8e8f4bSAdrian Hunter if (evsel->system_wide) 315bf8e8f4bSAdrian Hunter return 1; 316bf8e8f4bSAdrian Hunter else 31703617c22SJiri Olsa return thread_map__nr(evlist->core.threads); 318bf8e8f4bSAdrian Hunter } 319bf8e8f4bSAdrian Hunter 320e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3214152ab37SArnaldo Carvalho de Melo { 32232dcd021SJiri Olsa struct evsel *pos; 3234152ab37SArnaldo Carvalho de Melo 324e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3259dfcb759SJiri Olsa if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd) 3263fe4430dSJiri Olsa continue; 3279a10bb22SJiri Olsa evsel__disable(pos); 3284152ab37SArnaldo Carvalho de Melo } 3292b56bcfbSArnaldo Carvalho de Melo 3302b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3314152ab37SArnaldo Carvalho de Melo } 3324152ab37SArnaldo Carvalho de Melo 3331c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 334764e16a3SDavid Ahern { 33532dcd021SJiri Olsa struct evsel *pos; 336764e16a3SDavid Ahern 337e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3389dfcb759SJiri Olsa if (!perf_evsel__is_group_leader(pos) || !pos->core.fd) 3393fe4430dSJiri Olsa continue; 340ec7f24efSJiri Olsa evsel__enable(pos); 341764e16a3SDavid Ahern } 3422b56bcfbSArnaldo Carvalho de Melo 3432b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3442b56bcfbSArnaldo Carvalho de Melo } 3452b56bcfbSArnaldo Carvalho de Melo 34663503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist) 3472b56bcfbSArnaldo Carvalho de Melo { 348e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 349764e16a3SDavid Ahern } 350764e16a3SDavid Ahern 35163503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist, 35232dcd021SJiri Olsa struct evsel *evsel, int cpu) 3531c65056cSAdrian Hunter { 35418ef15c6SArnaldo Carvalho de Melo int thread; 3551c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3561c65056cSAdrian Hunter 3579dfcb759SJiri Olsa if (!evsel->core.fd) 3581c65056cSAdrian Hunter return -EINVAL; 3591c65056cSAdrian Hunter 3601c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 36118ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3621c65056cSAdrian Hunter if (err) 3631c65056cSAdrian Hunter return err; 3641c65056cSAdrian Hunter } 3651c65056cSAdrian Hunter return 0; 3661c65056cSAdrian Hunter } 3671c65056cSAdrian Hunter 36863503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist, 36932dcd021SJiri Olsa struct evsel *evsel, 3701c65056cSAdrian Hunter int thread) 3711c65056cSAdrian Hunter { 37218ef15c6SArnaldo Carvalho de Melo int cpu; 3736549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 3741c65056cSAdrian Hunter 3759dfcb759SJiri Olsa if (!evsel->core.fd) 3761c65056cSAdrian Hunter return -EINVAL; 3771c65056cSAdrian Hunter 3781c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 37918ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3801c65056cSAdrian Hunter if (err) 3811c65056cSAdrian Hunter return err; 3821c65056cSAdrian Hunter } 3831c65056cSAdrian Hunter return 0; 3841c65056cSAdrian Hunter } 3851c65056cSAdrian Hunter 38663503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist, 38732dcd021SJiri Olsa struct evsel *evsel, int idx) 3881c65056cSAdrian Hunter { 389315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 3901c65056cSAdrian Hunter 3911c65056cSAdrian Hunter if (per_cpu_mmaps) 3921c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 3931c65056cSAdrian Hunter else 3941c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 3951c65056cSAdrian Hunter } 3961c65056cSAdrian Hunter 39763503dbaSJiri Olsa int perf_evlist__alloc_pollfd(struct evlist *evlist) 3985c581041SArnaldo Carvalho de Melo { 3996549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 40003617c22SJiri Olsa int nr_threads = thread_map__nr(evlist->core.threads); 401bf8e8f4bSAdrian Hunter int nfds = 0; 40232dcd021SJiri Olsa struct evsel *evsel; 403bf8e8f4bSAdrian Hunter 404e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 405bf8e8f4bSAdrian Hunter if (evsel->system_wide) 406bf8e8f4bSAdrian Hunter nfds += nr_cpus; 407bf8e8f4bSAdrian Hunter else 408bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 409bf8e8f4bSAdrian Hunter } 410bf8e8f4bSAdrian Hunter 4111b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4121b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 413ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 414ad6765ddSArnaldo Carvalho de Melo 415ad6765ddSArnaldo Carvalho de Melo return 0; 4165c581041SArnaldo Carvalho de Melo } 41770082dd9SArnaldo Carvalho de Melo 41863503dbaSJiri Olsa static int __perf_evlist__add_pollfd(struct evlist *evlist, int fd, 4194876075bSWang Nan struct perf_mmap *map, short revent) 420e4b356b5SArnaldo Carvalho de Melo { 421f3058a1cSWang Nan int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); 422e4b356b5SArnaldo Carvalho de Melo /* 423e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 424e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 425e4b356b5SArnaldo Carvalho de Melo */ 426e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 4274876075bSWang Nan evlist->pollfd.priv[pos].ptr = map; 428e4b356b5SArnaldo Carvalho de Melo 429e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 430e4b356b5SArnaldo Carvalho de Melo } 431e4b356b5SArnaldo Carvalho de Melo 432e4b356b5SArnaldo Carvalho de Melo return pos; 433e4b356b5SArnaldo Carvalho de Melo } 434e4b356b5SArnaldo Carvalho de Melo 43563503dbaSJiri Olsa int perf_evlist__add_pollfd(struct evlist *evlist, int fd) 43670082dd9SArnaldo Carvalho de Melo { 4374876075bSWang Nan return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN); 438e4b356b5SArnaldo Carvalho de Melo } 4391b85337dSArnaldo Carvalho de Melo 440258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 441258e4bfcSWang Nan void *arg __maybe_unused) 442e4b356b5SArnaldo Carvalho de Melo { 4434876075bSWang Nan struct perf_mmap *map = fda->priv[fd].ptr; 444e4b356b5SArnaldo Carvalho de Melo 4454876075bSWang Nan if (map) 4464876075bSWang Nan perf_mmap__put(map); 44770082dd9SArnaldo Carvalho de Melo } 44870db7533SArnaldo Carvalho de Melo 44963503dbaSJiri Olsa int perf_evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 4501ddec7f0SArnaldo Carvalho de Melo { 451e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 452258e4bfcSWang Nan perf_evlist__munmap_filtered, NULL); 4531ddec7f0SArnaldo Carvalho de Melo } 4541ddec7f0SArnaldo Carvalho de Melo 45563503dbaSJiri Olsa int perf_evlist__poll(struct evlist *evlist, int timeout) 456f66a889dSArnaldo Carvalho de Melo { 4571b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 458f66a889dSArnaldo Carvalho de Melo } 459f66a889dSArnaldo Carvalho de Melo 46063503dbaSJiri Olsa static void perf_evlist__id_hash(struct evlist *evlist, 46132dcd021SJiri Olsa struct evsel *evsel, 4623d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4633d3b5e95SArnaldo Carvalho de Melo { 4643d3b5e95SArnaldo Carvalho de Melo int hash; 4653d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 4663d3b5e95SArnaldo Carvalho de Melo 4673d3b5e95SArnaldo Carvalho de Melo sid->id = id; 4683d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 4693d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 4703d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 4713d3b5e95SArnaldo Carvalho de Melo } 4723d3b5e95SArnaldo Carvalho de Melo 47363503dbaSJiri Olsa void perf_evlist__id_add(struct evlist *evlist, struct evsel *evsel, 474a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 475a91e5431SArnaldo Carvalho de Melo { 476a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 477a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 478a91e5431SArnaldo Carvalho de Melo } 479a91e5431SArnaldo Carvalho de Melo 48063503dbaSJiri Olsa int perf_evlist__id_add_fd(struct evlist *evlist, 48132dcd021SJiri Olsa struct evsel *evsel, 482f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 483f8a95309SArnaldo Carvalho de Melo { 484f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 4853d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 486e2b5abe0SJiri Olsa u64 id; 487e2b5abe0SJiri Olsa int ret; 488e2b5abe0SJiri Olsa 489e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 490e2b5abe0SJiri Olsa if (!ret) 491e2b5abe0SJiri Olsa goto add; 492e2b5abe0SJiri Olsa 493e2b5abe0SJiri Olsa if (errno != ENOTTY) 494e2b5abe0SJiri Olsa return -1; 495e2b5abe0SJiri Olsa 496e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 497f8a95309SArnaldo Carvalho de Melo 498c4861afeSJiri Olsa /* 499c4861afeSJiri Olsa * This way does not work with group format read, so bail 500c4861afeSJiri Olsa * out in that case. 501c4861afeSJiri Olsa */ 502c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 503c4861afeSJiri Olsa return -1; 504c4861afeSJiri Olsa 5051fc632ceSJiri Olsa if (!(evsel->core.attr.read_format & PERF_FORMAT_ID) || 506f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 507f8a95309SArnaldo Carvalho de Melo return -1; 508f8a95309SArnaldo Carvalho de Melo 5091fc632ceSJiri Olsa if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 510f8a95309SArnaldo Carvalho de Melo ++id_idx; 5111fc632ceSJiri Olsa if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 512f8a95309SArnaldo Carvalho de Melo ++id_idx; 513f8a95309SArnaldo Carvalho de Melo 514e2b5abe0SJiri Olsa id = read_data[id_idx]; 515e2b5abe0SJiri Olsa 516e2b5abe0SJiri Olsa add: 517e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 518f8a95309SArnaldo Carvalho de Melo return 0; 519f8a95309SArnaldo Carvalho de Melo } 520f8a95309SArnaldo Carvalho de Melo 52163503dbaSJiri Olsa static void perf_evlist__set_sid_idx(struct evlist *evlist, 52232dcd021SJiri Olsa struct evsel *evsel, int idx, int cpu, 5233c659eedSAdrian Hunter int thread) 5243c659eedSAdrian Hunter { 5253c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5263c659eedSAdrian Hunter sid->idx = idx; 527f72f901dSJiri Olsa if (evlist->core.cpus && cpu >= 0) 528f72f901dSJiri Olsa sid->cpu = evlist->core.cpus->map[cpu]; 5293c659eedSAdrian Hunter else 5303c659eedSAdrian Hunter sid->cpu = -1; 53103617c22SJiri Olsa if (!evsel->system_wide && evlist->core.threads && thread >= 0) 53203617c22SJiri Olsa sid->tid = thread_map__pid(evlist->core.threads, thread); 5333c659eedSAdrian Hunter else 5343c659eedSAdrian Hunter sid->tid = -1; 5353c659eedSAdrian Hunter } 5363c659eedSAdrian Hunter 53763503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 53870db7533SArnaldo Carvalho de Melo { 53970db7533SArnaldo Carvalho de Melo struct hlist_head *head; 54070db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 54170db7533SArnaldo Carvalho de Melo int hash; 54270db7533SArnaldo Carvalho de Melo 54370db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 54470db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 54570db7533SArnaldo Carvalho de Melo 546b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 54770db7533SArnaldo Carvalho de Melo if (sid->id == id) 548932a3594SJiri Olsa return sid; 549932a3594SJiri Olsa 550932a3594SJiri Olsa return NULL; 551932a3594SJiri Olsa } 552932a3594SJiri Olsa 55363503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 554932a3594SJiri Olsa { 555932a3594SJiri Olsa struct perf_sample_id *sid; 556932a3594SJiri Olsa 5576484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 558932a3594SJiri Olsa return perf_evlist__first(evlist); 559932a3594SJiri Olsa 560932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 561932a3594SJiri Olsa if (sid) 56270db7533SArnaldo Carvalho de Melo return sid->evsel; 56330e68bccSNamhyung Kim 56430e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 5650c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 56630e68bccSNamhyung Kim 56770db7533SArnaldo Carvalho de Melo return NULL; 56870db7533SArnaldo Carvalho de Melo } 56904391debSArnaldo Carvalho de Melo 57063503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 571dddcf6abSAdrian Hunter u64 id) 572dddcf6abSAdrian Hunter { 573dddcf6abSAdrian Hunter struct perf_sample_id *sid; 574dddcf6abSAdrian Hunter 575dddcf6abSAdrian Hunter if (!id) 576dddcf6abSAdrian Hunter return NULL; 577dddcf6abSAdrian Hunter 578dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 579dddcf6abSAdrian Hunter if (sid) 580dddcf6abSAdrian Hunter return sid->evsel; 581dddcf6abSAdrian Hunter 582dddcf6abSAdrian Hunter return NULL; 583dddcf6abSAdrian Hunter } 584dddcf6abSAdrian Hunter 58563503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist, 58675562573SAdrian Hunter union perf_event *event, u64 *id) 58775562573SAdrian Hunter { 58875562573SAdrian Hunter const u64 *array = event->sample.array; 58975562573SAdrian Hunter ssize_t n; 59075562573SAdrian Hunter 59175562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 59275562573SAdrian Hunter 59375562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 59475562573SAdrian Hunter if (evlist->id_pos >= n) 59575562573SAdrian Hunter return -1; 59675562573SAdrian Hunter *id = array[evlist->id_pos]; 59775562573SAdrian Hunter } else { 59875562573SAdrian Hunter if (evlist->is_pos > n) 59975562573SAdrian Hunter return -1; 60075562573SAdrian Hunter n -= evlist->is_pos; 60175562573SAdrian Hunter *id = array[n]; 60275562573SAdrian Hunter } 60375562573SAdrian Hunter return 0; 60475562573SAdrian Hunter } 60575562573SAdrian Hunter 60663503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 60775562573SAdrian Hunter union perf_event *event) 60875562573SAdrian Hunter { 60932dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 61075562573SAdrian Hunter struct hlist_head *head; 61175562573SAdrian Hunter struct perf_sample_id *sid; 61275562573SAdrian Hunter int hash; 61375562573SAdrian Hunter u64 id; 61475562573SAdrian Hunter 6156484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 61698be6966SAdrian Hunter return first; 61798be6966SAdrian Hunter 6181fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 61998be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 62098be6966SAdrian Hunter return first; 62175562573SAdrian Hunter 62275562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 62375562573SAdrian Hunter return NULL; 62475562573SAdrian Hunter 62575562573SAdrian Hunter /* Synthesized events have an id of zero */ 62675562573SAdrian Hunter if (!id) 62798be6966SAdrian Hunter return first; 62875562573SAdrian Hunter 62975562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 63075562573SAdrian Hunter head = &evlist->heads[hash]; 63175562573SAdrian Hunter 63275562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 63375562573SAdrian Hunter if (sid->id == id) 63475562573SAdrian Hunter return sid->evsel; 63575562573SAdrian Hunter } 63675562573SAdrian Hunter return NULL; 63775562573SAdrian Hunter } 63875562573SAdrian Hunter 63963503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value) 64065aea233SWang Nan { 64165aea233SWang Nan int i; 64265aea233SWang Nan 6430b72d69aSWang Nan if (!evlist->overwrite_mmap) 644078c3386SWang Nan return 0; 645078c3386SWang Nan 64665aea233SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 6470b72d69aSWang Nan int fd = evlist->overwrite_mmap[i].fd; 64865aea233SWang Nan int err; 64965aea233SWang Nan 65065aea233SWang Nan if (fd < 0) 65165aea233SWang Nan continue; 65265aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 65365aea233SWang Nan if (err) 65465aea233SWang Nan return err; 65565aea233SWang Nan } 65665aea233SWang Nan return 0; 65765aea233SWang Nan } 65865aea233SWang Nan 65963503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist) 66065aea233SWang Nan { 66165aea233SWang Nan return perf_evlist__set_paused(evlist, true); 66265aea233SWang Nan } 66365aea233SWang Nan 66463503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist) 66565aea233SWang Nan { 66665aea233SWang Nan return perf_evlist__set_paused(evlist, false); 66765aea233SWang Nan } 66865aea233SWang Nan 66963503dbaSJiri Olsa static void perf_evlist__munmap_nofree(struct evlist *evlist) 670f8a95309SArnaldo Carvalho de Melo { 671aece948fSArnaldo Carvalho de Melo int i; 672f8a95309SArnaldo Carvalho de Melo 673b2cb615dSWang Nan if (evlist->mmap) 67493edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 675a1f72618SWang Nan perf_mmap__munmap(&evlist->mmap[i]); 676b2cb615dSWang Nan 6770b72d69aSWang Nan if (evlist->overwrite_mmap) 678b2cb615dSWang Nan for (i = 0; i < evlist->nr_mmaps; i++) 6790b72d69aSWang Nan perf_mmap__munmap(&evlist->overwrite_mmap[i]); 680a1f72618SWang Nan } 681aece948fSArnaldo Carvalho de Melo 68263503dbaSJiri Olsa void perf_evlist__munmap(struct evlist *evlist) 683a1f72618SWang Nan { 684a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 68504662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6860b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 687f8a95309SArnaldo Carvalho de Melo } 688f8a95309SArnaldo Carvalho de Melo 68963503dbaSJiri Olsa static struct perf_mmap *perf_evlist__alloc_mmap(struct evlist *evlist, 6902c5f6d87SKan Liang bool overwrite) 691f8a95309SArnaldo Carvalho de Melo { 692d4c6fb36SWang Nan int i; 6938db6d6b1SWang Nan struct perf_mmap *map; 694d4c6fb36SWang Nan 6956549cd8fSJiri Olsa evlist->nr_mmaps = perf_cpu_map__nr(evlist->core.cpus); 696315c0a1fSJiri Olsa if (perf_cpu_map__empty(evlist->core.cpus)) 69703617c22SJiri Olsa evlist->nr_mmaps = thread_map__nr(evlist->core.threads); 6988db6d6b1SWang Nan map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 6998db6d6b1SWang Nan if (!map) 7008db6d6b1SWang Nan return NULL; 701946ae1d4SWang Nan 7024738ca30SArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 7038db6d6b1SWang Nan map[i].fd = -1; 7042c5f6d87SKan Liang map[i].overwrite = overwrite; 7054738ca30SArnaldo Carvalho de Melo /* 7064738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 7076afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 7084738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 7094738ca30SArnaldo Carvalho de Melo * dropped. 7104738ca30SArnaldo Carvalho de Melo * 7114738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 7124738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 7134738ca30SArnaldo Carvalho de Melo */ 7144738ca30SArnaldo Carvalho de Melo refcount_set(&map[i].refcnt, 0); 7154738ca30SArnaldo Carvalho de Melo } 7168db6d6b1SWang Nan return map; 717f8a95309SArnaldo Carvalho de Melo } 718f8a95309SArnaldo Carvalho de Melo 719f3058a1cSWang Nan static bool 72063503dbaSJiri Olsa perf_evlist__should_poll(struct evlist *evlist __maybe_unused, 72132dcd021SJiri Olsa struct evsel *evsel) 722f3058a1cSWang Nan { 7231fc632ceSJiri Olsa if (evsel->core.attr.write_backward) 724f3058a1cSWang Nan return false; 725f3058a1cSWang Nan return true; 726f3058a1cSWang Nan } 727f3058a1cSWang Nan 72863503dbaSJiri Olsa static int perf_evlist__mmap_per_evsel(struct evlist *evlist, int idx, 7299f21b815SMark Rutland struct mmap_params *mp, int cpu_idx, 7300b72d69aSWang Nan int thread, int *_output, int *_output_overwrite) 731aece948fSArnaldo Carvalho de Melo { 73232dcd021SJiri Olsa struct evsel *evsel; 733f3058a1cSWang Nan int revent; 734f72f901dSJiri Olsa int evlist_cpu = cpu_map__cpu(evlist->core.cpus, cpu_idx); 73504e21314SAdrian Hunter 736e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 737078c3386SWang Nan struct perf_mmap *maps = evlist->mmap; 738078c3386SWang Nan int *output = _output; 739bf8e8f4bSAdrian Hunter int fd; 7409f21b815SMark Rutland int cpu; 741bf8e8f4bSAdrian Hunter 74271f566a3SWang Nan mp->prot = PROT_READ | PROT_WRITE; 7431fc632ceSJiri Olsa if (evsel->core.attr.write_backward) { 7440b72d69aSWang Nan output = _output_overwrite; 7450b72d69aSWang Nan maps = evlist->overwrite_mmap; 746078c3386SWang Nan 747078c3386SWang Nan if (!maps) { 7482c5f6d87SKan Liang maps = perf_evlist__alloc_mmap(evlist, true); 749078c3386SWang Nan if (!maps) 750078c3386SWang Nan return -1; 7510b72d69aSWang Nan evlist->overwrite_mmap = maps; 75254cc54deSWang Nan if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 75354cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 754078c3386SWang Nan } 75571f566a3SWang Nan mp->prot &= ~PROT_WRITE; 756078c3386SWang Nan } 757f3058a1cSWang Nan 758bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 759bf8e8f4bSAdrian Hunter continue; 760bf8e8f4bSAdrian Hunter 761d400bd3aSJiri Olsa cpu = cpu_map__idx(evsel->core.cpus, evlist_cpu); 7629f21b815SMark Rutland if (cpu == -1) 7639f21b815SMark Rutland continue; 7649f21b815SMark Rutland 765bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 76604e21314SAdrian Hunter 76704e21314SAdrian Hunter if (*output == -1) { 76804e21314SAdrian Hunter *output = fd; 769078c3386SWang Nan 77031fb4c0dSJiri Olsa if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0) 77104e21314SAdrian Hunter return -1; 77204e21314SAdrian Hunter } else { 77304e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 77404e21314SAdrian Hunter return -1; 77582396986SArnaldo Carvalho de Melo 776078c3386SWang Nan perf_mmap__get(&maps[idx]); 77704e21314SAdrian Hunter } 77804e21314SAdrian Hunter 779f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 780f3058a1cSWang Nan 781f90d194aSAdrian Hunter /* 782f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 783f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 784f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 785f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 786f90d194aSAdrian Hunter * Therefore don't add it for polling. 787f90d194aSAdrian Hunter */ 788f90d194aSAdrian Hunter if (!evsel->system_wide && 789078c3386SWang Nan __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) { 790078c3386SWang Nan perf_mmap__put(&maps[idx]); 791ad6765ddSArnaldo Carvalho de Melo return -1; 79282396986SArnaldo Carvalho de Melo } 793033fa713SArnaldo Carvalho de Melo 7941fc632ceSJiri Olsa if (evsel->core.attr.read_format & PERF_FORMAT_ID) { 7953c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 7963c659eedSAdrian Hunter fd) < 0) 79704e21314SAdrian Hunter return -1; 7983c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 7993c659eedSAdrian Hunter thread); 8003c659eedSAdrian Hunter } 80104e21314SAdrian Hunter } 80204e21314SAdrian Hunter 80304e21314SAdrian Hunter return 0; 80404e21314SAdrian Hunter } 80504e21314SAdrian Hunter 80663503dbaSJiri Olsa static int perf_evlist__mmap_per_cpu(struct evlist *evlist, 807a8a8f3ebSAdrian Hunter struct mmap_params *mp) 80804e21314SAdrian Hunter { 809aece948fSArnaldo Carvalho de Melo int cpu, thread; 8106549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 81103617c22SJiri Olsa int nr_threads = thread_map__nr(evlist->core.threads); 812aece948fSArnaldo Carvalho de Melo 813e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 814b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 815aece948fSArnaldo Carvalho de Melo int output = -1; 8160b72d69aSWang Nan int output_overwrite = -1; 817aece948fSArnaldo Carvalho de Melo 818718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 819718c602dSAdrian Hunter true); 820718c602dSAdrian Hunter 821b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 822a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 8230b72d69aSWang Nan thread, &output, &output_overwrite)) 824aece948fSArnaldo Carvalho de Melo goto out_unmap; 825aece948fSArnaldo Carvalho de Melo } 826aece948fSArnaldo Carvalho de Melo } 827aece948fSArnaldo Carvalho de Melo 828aece948fSArnaldo Carvalho de Melo return 0; 829aece948fSArnaldo Carvalho de Melo 830aece948fSArnaldo Carvalho de Melo out_unmap: 831a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 832aece948fSArnaldo Carvalho de Melo return -1; 833aece948fSArnaldo Carvalho de Melo } 834aece948fSArnaldo Carvalho de Melo 83563503dbaSJiri Olsa static int perf_evlist__mmap_per_thread(struct evlist *evlist, 836a8a8f3ebSAdrian Hunter struct mmap_params *mp) 837aece948fSArnaldo Carvalho de Melo { 838aece948fSArnaldo Carvalho de Melo int thread; 83903617c22SJiri Olsa int nr_threads = thread_map__nr(evlist->core.threads); 840aece948fSArnaldo Carvalho de Melo 841e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 842b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 843aece948fSArnaldo Carvalho de Melo int output = -1; 8440b72d69aSWang Nan int output_overwrite = -1; 845aece948fSArnaldo Carvalho de Melo 846718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 847718c602dSAdrian Hunter false); 848718c602dSAdrian Hunter 849a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 8500b72d69aSWang Nan &output, &output_overwrite)) 851aece948fSArnaldo Carvalho de Melo goto out_unmap; 852aece948fSArnaldo Carvalho de Melo } 853aece948fSArnaldo Carvalho de Melo 854aece948fSArnaldo Carvalho de Melo return 0; 855aece948fSArnaldo Carvalho de Melo 856aece948fSArnaldo Carvalho de Melo out_unmap: 857a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 858aece948fSArnaldo Carvalho de Melo return -1; 859aece948fSArnaldo Carvalho de Melo } 860aece948fSArnaldo Carvalho de Melo 861f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 862994a1f78SJiri Olsa { 863f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 8648185e881SArnaldo Carvalho de Melo int max; 8658185e881SArnaldo Carvalho de Melo 8668185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 8678185e881SArnaldo Carvalho de Melo /* 8688185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 8698185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 8708185e881SArnaldo Carvalho de Melo * die yet... 8718185e881SArnaldo Carvalho de Melo */ 8728185e881SArnaldo Carvalho de Melo max = 512; 8738185e881SArnaldo Carvalho de Melo } else { 8748185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 8758185e881SArnaldo Carvalho de Melo } 8768185e881SArnaldo Carvalho de Melo 8778185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 8781be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 8791be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 880f5e7150cSArnaldo Carvalho de Melo 881f5e7150cSArnaldo Carvalho de Melo return pages; 882f5e7150cSArnaldo Carvalho de Melo } 883f5e7150cSArnaldo Carvalho de Melo 8840c582449SJiri Olsa size_t perf_evlist__mmap_size(unsigned long pages) 885f5e7150cSArnaldo Carvalho de Melo { 886f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 887f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 888f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 889994a1f78SJiri Olsa return 0; 890994a1f78SJiri Olsa 891994a1f78SJiri Olsa return (pages + 1) * page_size; 892994a1f78SJiri Olsa } 893994a1f78SJiri Olsa 89433c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 89533c2dcfdSDavid Ahern unsigned long max) 896994a1f78SJiri Olsa { 8972fbe4abeSAdrian Hunter unsigned long pages, val; 89827050f53SJiri Olsa static struct parse_tag tags[] = { 89927050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 90027050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 90127050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 90227050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 90327050f53SJiri Olsa { .tag = 0 }, 90427050f53SJiri Olsa }; 905994a1f78SJiri Olsa 9068973504bSDavid Ahern if (str == NULL) 90733c2dcfdSDavid Ahern return -EINVAL; 9088973504bSDavid Ahern 90927050f53SJiri Olsa val = parse_tag_value(str, tags); 9102fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 91127050f53SJiri Olsa /* we got file size value */ 91227050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 91327050f53SJiri Olsa } else { 91427050f53SJiri Olsa /* we got pages count value */ 91527050f53SJiri Olsa char *eptr; 916994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 91733c2dcfdSDavid Ahern if (*eptr != '\0') 91833c2dcfdSDavid Ahern return -EINVAL; 91927050f53SJiri Olsa } 920994a1f78SJiri Olsa 9212bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 92233c2dcfdSDavid Ahern /* leave number of pages at 0 */ 9231dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 9249808143bSJiri Olsa char buf[100]; 9259808143bSJiri Olsa 92633c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 92791529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 9281dbfa938SAdrian Hunter if (!pages) 9291dbfa938SAdrian Hunter return -EINVAL; 9309808143bSJiri Olsa 9319808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 9329808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 9339808143bSJiri Olsa buf, pages); 9342fbe4abeSAdrian Hunter } 9352fbe4abeSAdrian Hunter 93633c2dcfdSDavid Ahern if (pages > max) 93733c2dcfdSDavid Ahern return -EINVAL; 93833c2dcfdSDavid Ahern 93933c2dcfdSDavid Ahern return pages; 94033c2dcfdSDavid Ahern } 94133c2dcfdSDavid Ahern 942e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 94333c2dcfdSDavid Ahern { 94433c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 94533c2dcfdSDavid Ahern long pages; 94633c2dcfdSDavid Ahern 947f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 94833c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 94933c2dcfdSDavid Ahern 95033c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 95133c2dcfdSDavid Ahern if (pages < 0) { 95233c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 953994a1f78SJiri Olsa return -1; 954994a1f78SJiri Olsa } 955994a1f78SJiri Olsa 956994a1f78SJiri Olsa *mmap_pages = pages; 957994a1f78SJiri Olsa return 0; 958994a1f78SJiri Olsa } 959994a1f78SJiri Olsa 960e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 961e9db1310SAdrian Hunter int unset __maybe_unused) 962e9db1310SAdrian Hunter { 963e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 964e9db1310SAdrian Hunter } 965e9db1310SAdrian Hunter 966c83fa7f2SAdrian Hunter /** 967718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 968c83fa7f2SAdrian Hunter * @evlist: list of events 969c83fa7f2SAdrian Hunter * @pages: map length in pages 970c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 971718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 972718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 973f8a95309SArnaldo Carvalho de Melo * 974c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 975c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 976c83fa7f2SAdrian Hunter * automatically. 977f8a95309SArnaldo Carvalho de Melo * 978718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 979718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 980718c602dSAdrian Hunter * 981c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 982f8a95309SArnaldo Carvalho de Melo */ 98363503dbaSJiri Olsa int perf_evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 9847a276ff6SWang Nan unsigned int auxtrace_pages, 98551255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 98651255a8aSAlexey Budankov int comp_level) 987f8a95309SArnaldo Carvalho de Melo { 98832dcd021SJiri Olsa struct evsel *evsel; 989f72f901dSJiri Olsa const struct perf_cpu_map *cpus = evlist->core.cpus; 99003617c22SJiri Olsa const struct perf_thread_map *threads = evlist->core.threads; 99171f566a3SWang Nan /* 99271f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 99371f566a3SWang Nan * Its value is decided by evsel's write_backward. 99471f566a3SWang Nan * So &mp should not be passed through const pointer. 99571f566a3SWang Nan */ 99651255a8aSAlexey Budankov struct mmap_params mp = { .nr_cblocks = nr_cblocks, .affinity = affinity, .flush = flush, 99751255a8aSAlexey Budankov .comp_level = comp_level }; 99850a682ceSArnaldo Carvalho de Melo 9998db6d6b1SWang Nan if (!evlist->mmap) 10002c5f6d87SKan Liang evlist->mmap = perf_evlist__alloc_mmap(evlist, false); 10018db6d6b1SWang Nan if (!evlist->mmap) 1002f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1003f8a95309SArnaldo Carvalho de Melo 10041b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1005f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1006f8a95309SArnaldo Carvalho de Melo 1007994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 10082af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1009a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1010f8a95309SArnaldo Carvalho de Melo 1011718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1012718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1013718c602dSAdrian Hunter 1014e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 10151fc632ceSJiri Olsa if ((evsel->core.attr.read_format & PERF_FORMAT_ID) && 1016a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 10176549cd8fSJiri Olsa perf_evsel__alloc_id(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0) 1018f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1019f8a95309SArnaldo Carvalho de Melo } 1020f8a95309SArnaldo Carvalho de Melo 1021315c0a1fSJiri Olsa if (perf_cpu_map__empty(cpus)) 1022a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1023f8a95309SArnaldo Carvalho de Melo 1024a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1025f8a95309SArnaldo Carvalho de Melo } 10267e2ed097SArnaldo Carvalho de Melo 102763503dbaSJiri Olsa int perf_evlist__mmap(struct evlist *evlist, unsigned int pages) 1028718c602dSAdrian Hunter { 102951255a8aSAlexey Budankov return perf_evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 1030718c602dSAdrian Hunter } 1031718c602dSAdrian Hunter 103263503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 10337e2ed097SArnaldo Carvalho de Melo { 1034147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 1035f854839bSJiri Olsa struct perf_cpu_map *cpus; 10369749b90eSJiri Olsa struct perf_thread_map *threads; 10377e2ed097SArnaldo Carvalho de Melo 1038147c508fSJin Yao /* 1039147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 1040147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 1041147c508fSJin Yao * target->system_wide = true. 1042147c508fSJin Yao * 1043147c508fSJin Yao * If specify '--per-thread' only to perf record, 1044147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 1045147c508fSJin Yao * 1046147c508fSJin Yao * So target->per_thread && target->system_wide is false. 1047147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 1048147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 1049147c508fSJin Yao * current behavior. 1050147c508fSJin Yao * 1051147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 1052147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 1053147c508fSJin Yao * per-thread data. thread_map__new_str will call 1054147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 1055147c508fSJin Yao */ 105673c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 1057147c508fSJin Yao all_threads); 105874bfd2b2SAdrian Hunter 105974bfd2b2SAdrian Hunter if (!threads) 10607e2ed097SArnaldo Carvalho de Melo return -1; 10617e2ed097SArnaldo Carvalho de Melo 10629c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 1063397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 1064879d77d0SNamhyung Kim else 10659c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 10667e2ed097SArnaldo Carvalho de Melo 106774bfd2b2SAdrian Hunter if (!cpus) 10687e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 10697e2ed097SArnaldo Carvalho de Melo 1070ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 1071ec9a77a7SAdrian Hunter 1072453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1073d5bc056eSAdrian Hunter 1074d5bc056eSAdrian Hunter return 0; 10757e2ed097SArnaldo Carvalho de Melo 10767e2ed097SArnaldo Carvalho de Melo out_delete_threads: 10777836e52eSJiri Olsa perf_thread_map__put(threads); 10787e2ed097SArnaldo Carvalho de Melo return -1; 10797e2ed097SArnaldo Carvalho de Melo } 10807e2ed097SArnaldo Carvalho de Melo 108163503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist, 108222c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 108322c8a376SArnaldo Carvalho de Melo { 108432dcd021SJiri Olsa struct evsel *evsel; 108522c8a376SArnaldo Carvalho de Melo 1086e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 108722c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 108822c8a376SArnaldo Carvalho de Melo } 108922c8a376SArnaldo Carvalho de Melo 109063503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist, 109122c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 109222c8a376SArnaldo Carvalho de Melo { 109332dcd021SJiri Olsa struct evsel *evsel; 109422c8a376SArnaldo Carvalho de Melo 1095e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 109622c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 109722c8a376SArnaldo Carvalho de Melo } 109822c8a376SArnaldo Carvalho de Melo 109963503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 11000a102479SFrederic Weisbecker { 110132dcd021SJiri Olsa struct evsel *evsel; 1102745cefc5SArnaldo Carvalho de Melo int err = 0; 11030a102479SFrederic Weisbecker 1104e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1105745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 11060a102479SFrederic Weisbecker continue; 1107745cefc5SArnaldo Carvalho de Melo 1108d988d5eeSKan Liang /* 1109d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1110d988d5eeSKan Liang * So evlist and evsel should always be same. 1111d988d5eeSKan Liang */ 1112a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 111323d4aad4SArnaldo Carvalho de Melo if (err) { 111423d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1115745cefc5SArnaldo Carvalho de Melo break; 11160a102479SFrederic Weisbecker } 111723d4aad4SArnaldo Carvalho de Melo } 11180a102479SFrederic Weisbecker 1119745cefc5SArnaldo Carvalho de Melo return err; 1120745cefc5SArnaldo Carvalho de Melo } 1121745cefc5SArnaldo Carvalho de Melo 112263503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1123745cefc5SArnaldo Carvalho de Melo { 112432dcd021SJiri Olsa struct evsel *evsel; 1125745cefc5SArnaldo Carvalho de Melo int err = 0; 1126745cefc5SArnaldo Carvalho de Melo 1127e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 11281fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1129fdf14720SWang Nan continue; 1130fdf14720SWang Nan 113194ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1132745cefc5SArnaldo Carvalho de Melo if (err) 1133745cefc5SArnaldo Carvalho de Melo break; 1134745cefc5SArnaldo Carvalho de Melo } 1135745cefc5SArnaldo Carvalho de Melo 1136745cefc5SArnaldo Carvalho de Melo return err; 11370a102479SFrederic Weisbecker } 113874429964SFrederic Weisbecker 113963503dbaSJiri Olsa int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 1140cfd70a26SArnaldo Carvalho de Melo { 1141cfd70a26SArnaldo Carvalho de Melo char *filter; 1142be199adaSArnaldo Carvalho de Melo int ret = -1; 1143be199adaSArnaldo Carvalho de Melo size_t i; 1144cfd70a26SArnaldo Carvalho de Melo 1145be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1146be199adaSArnaldo Carvalho de Melo if (i == 0) { 1147be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1148cfd70a26SArnaldo Carvalho de Melo return -1; 1149be199adaSArnaldo Carvalho de Melo } else { 1150be199adaSArnaldo Carvalho de Melo char *tmp; 1151be199adaSArnaldo Carvalho de Melo 1152be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1153be199adaSArnaldo Carvalho de Melo goto out_free; 1154be199adaSArnaldo Carvalho de Melo 1155be199adaSArnaldo Carvalho de Melo free(filter); 1156be199adaSArnaldo Carvalho de Melo filter = tmp; 1157be199adaSArnaldo Carvalho de Melo } 1158be199adaSArnaldo Carvalho de Melo } 1159cfd70a26SArnaldo Carvalho de Melo 11607ad92a33SArnaldo Carvalho de Melo ret = perf_evlist__set_tp_filter(evlist, filter); 1161be199adaSArnaldo Carvalho de Melo out_free: 1162cfd70a26SArnaldo Carvalho de Melo free(filter); 1163cfd70a26SArnaldo Carvalho de Melo return ret; 1164cfd70a26SArnaldo Carvalho de Melo } 1165cfd70a26SArnaldo Carvalho de Melo 116663503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1167be199adaSArnaldo Carvalho de Melo { 11687ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1169be199adaSArnaldo Carvalho de Melo } 1170be199adaSArnaldo Carvalho de Melo 117163503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist) 117274429964SFrederic Weisbecker { 117332dcd021SJiri Olsa struct evsel *pos; 1174c2a70653SArnaldo Carvalho de Melo 11756484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 117675562573SAdrian Hunter return true; 117775562573SAdrian Hunter 117875562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 117975562573SAdrian Hunter return false; 118075562573SAdrian Hunter 1181e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 118275562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 118375562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1184c2a70653SArnaldo Carvalho de Melo return false; 118574429964SFrederic Weisbecker } 118674429964SFrederic Weisbecker 1187c2a70653SArnaldo Carvalho de Melo return true; 1188c2a70653SArnaldo Carvalho de Melo } 1189c2a70653SArnaldo Carvalho de Melo 119063503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist) 1191c2a70653SArnaldo Carvalho de Melo { 119232dcd021SJiri Olsa struct evsel *evsel; 119375562573SAdrian Hunter 119475562573SAdrian Hunter if (evlist->combined_sample_type) 119575562573SAdrian Hunter return evlist->combined_sample_type; 119675562573SAdrian Hunter 1197e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11981fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 119975562573SAdrian Hunter 120075562573SAdrian Hunter return evlist->combined_sample_type; 120175562573SAdrian Hunter } 120275562573SAdrian Hunter 120363503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist) 120475562573SAdrian Hunter { 120575562573SAdrian Hunter evlist->combined_sample_type = 0; 120675562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1207c2a70653SArnaldo Carvalho de Melo } 1208c2a70653SArnaldo Carvalho de Melo 120963503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist) 121098df858eSAndi Kleen { 121132dcd021SJiri Olsa struct evsel *evsel; 121298df858eSAndi Kleen u64 branch_type = 0; 121398df858eSAndi Kleen 1214e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 12151fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 121698df858eSAndi Kleen return branch_type; 121798df858eSAndi Kleen } 121898df858eSAndi Kleen 121963503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 12209ede473cSJiri Olsa { 122132dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist), *pos = first; 12221fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 12231fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 12249ede473cSJiri Olsa 1225e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 12261fc632ceSJiri Olsa if (read_format != pos->core.attr.read_format) 12279ede473cSJiri Olsa return false; 12289ede473cSJiri Olsa } 12299ede473cSJiri Olsa 12309ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 12319ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 12329ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 12339ede473cSJiri Olsa return false; 12349ede473cSJiri Olsa } 12359ede473cSJiri Olsa 12369ede473cSJiri Olsa return true; 12379ede473cSJiri Olsa } 12389ede473cSJiri Olsa 123963503dbaSJiri Olsa u64 perf_evlist__read_format(struct evlist *evlist) 12409ede473cSJiri Olsa { 124132dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 12421fc632ceSJiri Olsa return first->core.attr.read_format; 12439ede473cSJiri Olsa } 12449ede473cSJiri Olsa 124563503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 124681e36bffSArnaldo Carvalho de Melo { 124732dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 124881e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 124981e36bffSArnaldo Carvalho de Melo u64 sample_type; 125081e36bffSArnaldo Carvalho de Melo u16 size = 0; 125181e36bffSArnaldo Carvalho de Melo 12521fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 125381e36bffSArnaldo Carvalho de Melo goto out; 125481e36bffSArnaldo Carvalho de Melo 12551fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 125681e36bffSArnaldo Carvalho de Melo 125781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 125881e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 125981e36bffSArnaldo Carvalho de Melo 126081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 126181e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 126281e36bffSArnaldo Carvalho de Melo 126381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 126481e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 126581e36bffSArnaldo Carvalho de Melo 126681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 126781e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 126881e36bffSArnaldo Carvalho de Melo 126981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 127081e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 127175562573SAdrian Hunter 127275562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 127375562573SAdrian Hunter size += sizeof(data->id); 127481e36bffSArnaldo Carvalho de Melo out: 127581e36bffSArnaldo Carvalho de Melo return size; 127681e36bffSArnaldo Carvalho de Melo } 127781e36bffSArnaldo Carvalho de Melo 127863503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist) 1279c2a70653SArnaldo Carvalho de Melo { 128032dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist), *pos = first; 1281c2a70653SArnaldo Carvalho de Melo 1282e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 12831fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1284c2a70653SArnaldo Carvalho de Melo return false; 1285c2a70653SArnaldo Carvalho de Melo } 1286c2a70653SArnaldo Carvalho de Melo 1287c2a70653SArnaldo Carvalho de Melo return true; 128874429964SFrederic Weisbecker } 128974429964SFrederic Weisbecker 129063503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist) 129174429964SFrederic Weisbecker { 129232dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 12931fc632ceSJiri Olsa return first->core.attr.sample_id_all; 129474429964SFrederic Weisbecker } 129581cce8deSArnaldo Carvalho de Melo 129663503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 129732dcd021SJiri Olsa struct evsel *evsel) 129881cce8deSArnaldo Carvalho de Melo { 129981cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 130081cce8deSArnaldo Carvalho de Melo } 1301727ab04eSArnaldo Carvalho de Melo 1302750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1303a74b4b66SNamhyung Kim { 130432dcd021SJiri Olsa struct evsel *evsel; 1305a74b4b66SNamhyung Kim 1306475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 130788761fa1SJiri Olsa evsel__close(evsel); 1308a74b4b66SNamhyung Kim } 1309a74b4b66SNamhyung Kim 131063503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 13114112eb18SArnaldo Carvalho de Melo { 1312f854839bSJiri Olsa struct perf_cpu_map *cpus; 13139749b90eSJiri Olsa struct perf_thread_map *threads; 13144112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 13154112eb18SArnaldo Carvalho de Melo 13164112eb18SArnaldo Carvalho de Melo /* 13174112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 13184112eb18SArnaldo Carvalho de Melo * an all cpus map. 13194112eb18SArnaldo Carvalho de Melo * 13204112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 13214112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 13224112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 13234112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 13244112eb18SArnaldo Carvalho de Melo */ 13259c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 13268c0498b6SAdrian Hunter if (!cpus) 13274112eb18SArnaldo Carvalho de Melo goto out; 13284112eb18SArnaldo Carvalho de Melo 13294b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 13308c0498b6SAdrian Hunter if (!threads) 13318c0498b6SAdrian Hunter goto out_put; 13324112eb18SArnaldo Carvalho de Melo 1333453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 13344112eb18SArnaldo Carvalho de Melo out: 13354112eb18SArnaldo Carvalho de Melo return err; 13368c0498b6SAdrian Hunter out_put: 133738f01d8dSJiri Olsa perf_cpu_map__put(cpus); 13384112eb18SArnaldo Carvalho de Melo goto out; 13394112eb18SArnaldo Carvalho de Melo } 13404112eb18SArnaldo Carvalho de Melo 1341474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1342727ab04eSArnaldo Carvalho de Melo { 134332dcd021SJiri Olsa struct evsel *evsel; 1344a74b4b66SNamhyung Kim int err; 1345727ab04eSArnaldo Carvalho de Melo 13464112eb18SArnaldo Carvalho de Melo /* 13474112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 13484112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 13494112eb18SArnaldo Carvalho de Melo */ 135003617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 13514112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 13524112eb18SArnaldo Carvalho de Melo if (err < 0) 13534112eb18SArnaldo Carvalho de Melo goto out_err; 13544112eb18SArnaldo Carvalho de Melo } 13554112eb18SArnaldo Carvalho de Melo 1356733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1357733cd2feSAdrian Hunter 1358e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1359af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1360727ab04eSArnaldo Carvalho de Melo if (err < 0) 1361727ab04eSArnaldo Carvalho de Melo goto out_err; 1362727ab04eSArnaldo Carvalho de Melo } 1363727ab04eSArnaldo Carvalho de Melo 1364727ab04eSArnaldo Carvalho de Melo return 0; 1365727ab04eSArnaldo Carvalho de Melo out_err: 1366750b4edeSJiri Olsa evlist__close(evlist); 136741c21a68SNamhyung Kim errno = -err; 1368727ab04eSArnaldo Carvalho de Melo return err; 1369727ab04eSArnaldo Carvalho de Melo } 137035b9d88eSArnaldo Carvalho de Melo 137163503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 137255e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1373735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 137435b9d88eSArnaldo Carvalho de Melo { 137535b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 137635b9d88eSArnaldo Carvalho de Melo char bf; 137735b9d88eSArnaldo Carvalho de Melo 137835b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 137935b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 138035b9d88eSArnaldo Carvalho de Melo return -1; 138135b9d88eSArnaldo Carvalho de Melo } 138235b9d88eSArnaldo Carvalho de Melo 138335b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 138435b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 138535b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 138635b9d88eSArnaldo Carvalho de Melo } 138735b9d88eSArnaldo Carvalho de Melo 138835b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 138935b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 139035b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 139135b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 139235b9d88eSArnaldo Carvalho de Melo } 139335b9d88eSArnaldo Carvalho de Melo 139435b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13955f1c4225SArnaldo Carvalho de Melo int ret; 13965f1c4225SArnaldo Carvalho de Melo 1397119fa3c9SNamhyung Kim if (pipe_output) 139835b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 139935b9d88eSArnaldo Carvalho de Melo 14000817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 14010817df08SDavid Ahern 140235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 140335b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 140435b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 140535b9d88eSArnaldo Carvalho de Melo 140635b9d88eSArnaldo Carvalho de Melo /* 140735b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 140835b9d88eSArnaldo Carvalho de Melo */ 140935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 141035b9d88eSArnaldo Carvalho de Melo 141135b9d88eSArnaldo Carvalho de Melo /* 141235b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 141335b9d88eSArnaldo Carvalho de Melo */ 14145f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 14155f1c4225SArnaldo Carvalho de Melo /* 14165f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 14175f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 14185f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 14195f1c4225SArnaldo Carvalho de Melo * 142020f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 14215f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 14225f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 14235f1c4225SArnaldo Carvalho de Melo * here. 14245f1c4225SArnaldo Carvalho de Melo */ 14255f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 14265f1c4225SArnaldo Carvalho de Melo if (ret == -1) 142735b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 14285f1c4225SArnaldo Carvalho de Melo exit(ret); 14295f1c4225SArnaldo Carvalho de Melo } 143035b9d88eSArnaldo Carvalho de Melo 143135b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 143235b9d88eSArnaldo Carvalho de Melo 1433735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1434f33cbe72SArnaldo Carvalho de Melo union sigval val; 1435f33cbe72SArnaldo Carvalho de Melo 1436f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1437f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 143835b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1439f33cbe72SArnaldo Carvalho de Melo } else 1440f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 144135b9d88eSArnaldo Carvalho de Melo exit(-1); 144235b9d88eSArnaldo Carvalho de Melo } 144335b9d88eSArnaldo Carvalho de Melo 1444735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1445735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1446735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1447735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1448735f7e0bSArnaldo Carvalho de Melo }; 1449735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1450735f7e0bSArnaldo Carvalho de Melo } 1451735f7e0bSArnaldo Carvalho de Melo 14521aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 145303617c22SJiri Olsa if (evlist->core.threads == NULL) { 14541aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 14551aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 14561aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 14571aaf63b1SArnaldo Carvalho de Melo } 145803617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 14591aaf63b1SArnaldo Carvalho de Melo } 146035b9d88eSArnaldo Carvalho de Melo 146135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 146235b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 146335b9d88eSArnaldo Carvalho de Melo /* 146435b9d88eSArnaldo Carvalho de Melo * wait for child to settle 146535b9d88eSArnaldo Carvalho de Melo */ 146635b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 146735b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 146835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 146935b9d88eSArnaldo Carvalho de Melo } 147035b9d88eSArnaldo Carvalho de Melo 1471bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 147235b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 147335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 147435b9d88eSArnaldo Carvalho de Melo return 0; 147535b9d88eSArnaldo Carvalho de Melo 147635b9d88eSArnaldo Carvalho de Melo out_close_pipes: 147735b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 147835b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 147935b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 148035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 148135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 148235b9d88eSArnaldo Carvalho de Melo return -1; 148335b9d88eSArnaldo Carvalho de Melo } 148435b9d88eSArnaldo Carvalho de Melo 148563503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist) 148635b9d88eSArnaldo Carvalho de Melo { 148735b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1488b3824404SDavid Ahern char bf = 0; 1489bcf3145fSNamhyung Kim int ret; 149035b9d88eSArnaldo Carvalho de Melo /* 149135b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 149235b9d88eSArnaldo Carvalho de Melo */ 1493bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1494bcf3145fSNamhyung Kim if (ret < 0) 1495e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1496bcf3145fSNamhyung Kim 1497bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1498bcf3145fSNamhyung Kim return ret; 149935b9d88eSArnaldo Carvalho de Melo } 150035b9d88eSArnaldo Carvalho de Melo 150135b9d88eSArnaldo Carvalho de Melo return 0; 150235b9d88eSArnaldo Carvalho de Melo } 1503cb0b29e0SArnaldo Carvalho de Melo 150463503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 15050807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1506cb0b29e0SArnaldo Carvalho de Melo { 150732dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 150875562573SAdrian Hunter 150975562573SAdrian Hunter if (!evsel) 151075562573SAdrian Hunter return -EFAULT; 15110807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1512cb0b29e0SArnaldo Carvalho de Melo } 151378f067b3SArnaldo Carvalho de Melo 151463503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 151501468120SJiri Olsa union perf_event *event, 151601468120SJiri Olsa u64 *timestamp) 151701468120SJiri Olsa { 151832dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 151901468120SJiri Olsa 152001468120SJiri Olsa if (!evsel) 152101468120SJiri Olsa return -EFAULT; 152201468120SJiri Olsa return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); 152301468120SJiri Olsa } 152401468120SJiri Olsa 152563503dbaSJiri Olsa size_t perf_evlist__fprintf(struct evlist *evlist, FILE *fp) 152678f067b3SArnaldo Carvalho de Melo { 152732dcd021SJiri Olsa struct evsel *evsel; 152878f067b3SArnaldo Carvalho de Melo size_t printed = 0; 152978f067b3SArnaldo Carvalho de Melo 1530e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 153178f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 153278f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 153378f067b3SArnaldo Carvalho de Melo } 153478f067b3SArnaldo Carvalho de Melo 1535b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 153678f067b3SArnaldo Carvalho de Melo } 15376ef068cbSArnaldo Carvalho de Melo 153863503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist, 1539a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1540a8f23d8fSArnaldo Carvalho de Melo { 1541a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1542c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1543a8f23d8fSArnaldo Carvalho de Melo 1544a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1545a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1546a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1547a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1548a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1549a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1550a8f23d8fSArnaldo Carvalho de Melo 15511a47245dSAdrian Hunter value = perf_event_paranoid(); 1552a8f23d8fSArnaldo Carvalho de Melo 1553a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1554a8f23d8fSArnaldo Carvalho de Melo 1555a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1556a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1557a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1558a8f23d8fSArnaldo Carvalho de Melo } 1559a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15605229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1561a8f23d8fSArnaldo Carvalho de Melo 1562a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 15635229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 15645229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1565a8f23d8fSArnaldo Carvalho de Melo break; 1566d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 156732dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 1568d9aade7fSArnaldo Carvalho de Melo int max_freq; 1569d9aade7fSArnaldo Carvalho de Melo 1570d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1571d9aade7fSArnaldo Carvalho de Melo goto out_default; 1572d9aade7fSArnaldo Carvalho de Melo 15731fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1574d9aade7fSArnaldo Carvalho de Melo goto out_default; 1575d9aade7fSArnaldo Carvalho de Melo 1576d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1577d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1578d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1579d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 15801fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1581d9aade7fSArnaldo Carvalho de Melo break; 1582d9aade7fSArnaldo Carvalho de Melo } 1583a8f23d8fSArnaldo Carvalho de Melo default: 1584d9aade7fSArnaldo Carvalho de Melo out_default: 1585a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1586a8f23d8fSArnaldo Carvalho de Melo break; 1587a8f23d8fSArnaldo Carvalho de Melo } 1588a8f23d8fSArnaldo Carvalho de Melo 1589a8f23d8fSArnaldo Carvalho de Melo return 0; 1590a8f23d8fSArnaldo Carvalho de Melo } 1591a025e4f0SAdrian Hunter 159263503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1593956fa571SArnaldo Carvalho de Melo { 1594c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1595e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1596956fa571SArnaldo Carvalho de Melo 1597956fa571SArnaldo Carvalho de Melo switch (err) { 1598956fa571SArnaldo Carvalho de Melo case EPERM: 1599e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1600e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1601e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1602956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1603e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1604e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1605e965bea1SArnaldo Carvalho de Melo 1606e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1607e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1608e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1609e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1610e965bea1SArnaldo Carvalho de Melo } 1611e965bea1SArnaldo Carvalho de Melo 1612e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1613e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1614956fa571SArnaldo Carvalho de Melo break; 1615956fa571SArnaldo Carvalho de Melo default: 1616956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1617956fa571SArnaldo Carvalho de Melo break; 1618956fa571SArnaldo Carvalho de Melo } 1619956fa571SArnaldo Carvalho de Melo 1620956fa571SArnaldo Carvalho de Melo return 0; 1621956fa571SArnaldo Carvalho de Melo } 1622956fa571SArnaldo Carvalho de Melo 162363503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 162432dcd021SJiri Olsa struct evsel *move_evsel) 1625a025e4f0SAdrian Hunter { 162632dcd021SJiri Olsa struct evsel *evsel, *n; 1627a025e4f0SAdrian Hunter LIST_HEAD(move); 1628a025e4f0SAdrian Hunter 1629a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1630a025e4f0SAdrian Hunter return; 1631a025e4f0SAdrian Hunter 1632e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1633a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1634b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1635a025e4f0SAdrian Hunter } 1636a025e4f0SAdrian Hunter 1637ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1638a025e4f0SAdrian Hunter } 163960b0896cSAdrian Hunter 164063503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist, 164132dcd021SJiri Olsa struct evsel *tracking_evsel) 164260b0896cSAdrian Hunter { 164332dcd021SJiri Olsa struct evsel *evsel; 164460b0896cSAdrian Hunter 164560b0896cSAdrian Hunter if (tracking_evsel->tracking) 164660b0896cSAdrian Hunter return; 164760b0896cSAdrian Hunter 1648e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 164960b0896cSAdrian Hunter if (evsel != tracking_evsel) 165060b0896cSAdrian Hunter evsel->tracking = false; 165160b0896cSAdrian Hunter } 165260b0896cSAdrian Hunter 165360b0896cSAdrian Hunter tracking_evsel->tracking = true; 165460b0896cSAdrian Hunter } 16557630b3e2SWang Nan 165632dcd021SJiri Olsa struct evsel * 165763503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist, 16587630b3e2SWang Nan const char *str) 16597630b3e2SWang Nan { 166032dcd021SJiri Olsa struct evsel *evsel; 16617630b3e2SWang Nan 1662e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16637630b3e2SWang Nan if (!evsel->name) 16647630b3e2SWang Nan continue; 16657630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 16667630b3e2SWang Nan return evsel; 16677630b3e2SWang Nan } 16687630b3e2SWang Nan 16697630b3e2SWang Nan return NULL; 16707630b3e2SWang Nan } 167154cc54deSWang Nan 167263503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 167354cc54deSWang Nan enum bkw_mmap_state state) 167454cc54deSWang Nan { 167554cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 167654cc54deSWang Nan enum action { 167754cc54deSWang Nan NONE, 167854cc54deSWang Nan PAUSE, 167954cc54deSWang Nan RESUME, 168054cc54deSWang Nan } action = NONE; 168154cc54deSWang Nan 16820b72d69aSWang Nan if (!evlist->overwrite_mmap) 168354cc54deSWang Nan return; 168454cc54deSWang Nan 168554cc54deSWang Nan switch (old_state) { 168654cc54deSWang Nan case BKW_MMAP_NOTREADY: { 168754cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1688dd8bd53aSLuis de Bethencourt goto state_err; 168954cc54deSWang Nan break; 169054cc54deSWang Nan } 169154cc54deSWang Nan case BKW_MMAP_RUNNING: { 169254cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 169354cc54deSWang Nan goto state_err; 169454cc54deSWang Nan action = PAUSE; 169554cc54deSWang Nan break; 169654cc54deSWang Nan } 169754cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 169854cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 169954cc54deSWang Nan goto state_err; 170054cc54deSWang Nan break; 170154cc54deSWang Nan } 170254cc54deSWang Nan case BKW_MMAP_EMPTY: { 170354cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 170454cc54deSWang Nan goto state_err; 170554cc54deSWang Nan action = RESUME; 170654cc54deSWang Nan break; 170754cc54deSWang Nan } 170854cc54deSWang Nan default: 170954cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 171054cc54deSWang Nan } 171154cc54deSWang Nan 171254cc54deSWang Nan evlist->bkw_mmap_state = state; 171354cc54deSWang Nan 171454cc54deSWang Nan switch (action) { 171554cc54deSWang Nan case PAUSE: 171654cc54deSWang Nan perf_evlist__pause(evlist); 171754cc54deSWang Nan break; 171854cc54deSWang Nan case RESUME: 171954cc54deSWang Nan perf_evlist__resume(evlist); 172054cc54deSWang Nan break; 172154cc54deSWang Nan case NONE: 172254cc54deSWang Nan default: 172354cc54deSWang Nan break; 172454cc54deSWang Nan } 172554cc54deSWang Nan 172654cc54deSWang Nan state_err: 172754cc54deSWang Nan return; 172854cc54deSWang Nan } 172907d6f446SArnaldo Carvalho de Melo 173063503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 173107d6f446SArnaldo Carvalho de Melo { 173232dcd021SJiri Olsa struct evsel *evsel; 173307d6f446SArnaldo Carvalho de Melo 173407d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 17351fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 173607d6f446SArnaldo Carvalho de Melo return false; 173707d6f446SArnaldo Carvalho de Melo } 173807d6f446SArnaldo Carvalho de Melo 173907d6f446SArnaldo Carvalho de Melo return true; 174007d6f446SArnaldo Carvalho de Melo } 1741e2bdbe80SJin Yao 1742e2bdbe80SJin Yao /* 1743e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1744e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1745e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1746e2bdbe80SJin Yao */ 174763503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1748e2bdbe80SJin Yao { 1749e2bdbe80SJin Yao if (!evlist->nr_groups) { 175032dcd021SJiri Olsa struct evsel *leader = perf_evlist__first(evlist); 1751e2bdbe80SJin Yao 1752e2bdbe80SJin Yao perf_evlist__set_leader(evlist); 1753e2bdbe80SJin Yao leader->forced_leader = true; 1754e2bdbe80SJin Yao } 1755e2bdbe80SJin Yao } 1756c3537fc2SAndi Kleen 175763503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 175832dcd021SJiri Olsa struct evsel *evsel) 1759c3537fc2SAndi Kleen { 176032dcd021SJiri Olsa struct evsel *c2, *leader; 1761c3537fc2SAndi Kleen bool is_open = true; 1762c3537fc2SAndi Kleen 1763c3537fc2SAndi Kleen leader = evsel->leader; 1764c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 17655643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1766c3537fc2SAndi Kleen 1767c3537fc2SAndi Kleen /* 1768c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1769c3537fc2SAndi Kleen * include the first entry. 1770c3537fc2SAndi Kleen */ 1771c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1772c3537fc2SAndi Kleen if (c2 == evsel) 1773c3537fc2SAndi Kleen is_open = false; 1774c3537fc2SAndi Kleen if (c2->leader == leader) { 1775c3537fc2SAndi Kleen if (is_open) 177688761fa1SJiri Olsa evsel__close(c2); 1777c3537fc2SAndi Kleen c2->leader = c2; 17785643b1a5SJiri Olsa c2->core.nr_members = 0; 1779c3537fc2SAndi Kleen } 1780c3537fc2SAndi Kleen } 1781c3537fc2SAndi Kleen return leader; 1782c3537fc2SAndi Kleen } 1783657ee553SSong Liu 178463503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist, 1785657ee553SSong Liu struct perf_event_attr *attr, 1786657ee553SSong Liu perf_evsel__sb_cb_t cb, 1787657ee553SSong Liu void *data) 1788657ee553SSong Liu { 178932dcd021SJiri Olsa struct evsel *evsel; 1790657ee553SSong Liu bool new_evlist = (*evlist) == NULL; 1791657ee553SSong Liu 1792657ee553SSong Liu if (*evlist == NULL) 17930f98b11cSJiri Olsa *evlist = evlist__new(); 1794657ee553SSong Liu if (*evlist == NULL) 1795657ee553SSong Liu return -1; 1796657ee553SSong Liu 1797657ee553SSong Liu if (!attr->sample_id_all) { 1798657ee553SSong Liu pr_warning("enabling sample_id_all for all side band events\n"); 1799657ee553SSong Liu attr->sample_id_all = 1; 1800657ee553SSong Liu } 1801657ee553SSong Liu 18026484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries); 1803657ee553SSong Liu if (!evsel) 1804657ee553SSong Liu goto out_err; 1805657ee553SSong Liu 1806657ee553SSong Liu evsel->side_band.cb = cb; 1807657ee553SSong Liu evsel->side_band.data = data; 1808a1cf3a75SJiri Olsa evlist__add(*evlist, evsel); 1809657ee553SSong Liu return 0; 1810657ee553SSong Liu 1811657ee553SSong Liu out_err: 1812657ee553SSong Liu if (new_evlist) { 1813c12995a5SJiri Olsa evlist__delete(*evlist); 1814657ee553SSong Liu *evlist = NULL; 1815657ee553SSong Liu } 1816657ee553SSong Liu return -1; 1817657ee553SSong Liu } 1818657ee553SSong Liu 1819657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg) 1820657ee553SSong Liu { 182163503dbaSJiri Olsa struct evlist *evlist = arg; 1822657ee553SSong Liu bool draining = false; 1823adc6257cSJiri Olsa int i, done = 0; 1824657ee553SSong Liu 1825adc6257cSJiri Olsa while (!done) { 1826adc6257cSJiri Olsa bool got_data = false; 1827adc6257cSJiri Olsa 1828adc6257cSJiri Olsa if (evlist->thread.done) 1829657ee553SSong Liu draining = true; 1830657ee553SSong Liu 1831657ee553SSong Liu if (!draining) 1832657ee553SSong Liu perf_evlist__poll(evlist, 1000); 1833657ee553SSong Liu 1834657ee553SSong Liu for (i = 0; i < evlist->nr_mmaps; i++) { 1835657ee553SSong Liu struct perf_mmap *map = &evlist->mmap[i]; 1836657ee553SSong Liu union perf_event *event; 1837657ee553SSong Liu 1838657ee553SSong Liu if (perf_mmap__read_init(map)) 1839657ee553SSong Liu continue; 1840657ee553SSong Liu while ((event = perf_mmap__read_event(map)) != NULL) { 184132dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1842657ee553SSong Liu 1843657ee553SSong Liu if (evsel && evsel->side_band.cb) 1844657ee553SSong Liu evsel->side_band.cb(event, evsel->side_band.data); 1845657ee553SSong Liu else 1846657ee553SSong Liu pr_warning("cannot locate proper evsel for the side band event\n"); 1847657ee553SSong Liu 1848657ee553SSong Liu perf_mmap__consume(map); 1849adc6257cSJiri Olsa got_data = true; 1850657ee553SSong Liu } 1851657ee553SSong Liu perf_mmap__read_done(map); 1852657ee553SSong Liu } 1853adc6257cSJiri Olsa 1854adc6257cSJiri Olsa if (draining && !got_data) 1855adc6257cSJiri Olsa break; 1856657ee553SSong Liu } 1857657ee553SSong Liu return NULL; 1858657ee553SSong Liu } 1859657ee553SSong Liu 186063503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist, 1861657ee553SSong Liu struct target *target) 1862657ee553SSong Liu { 186332dcd021SJiri Olsa struct evsel *counter; 1864657ee553SSong Liu 1865657ee553SSong Liu if (!evlist) 1866657ee553SSong Liu return 0; 1867657ee553SSong Liu 1868657ee553SSong Liu if (perf_evlist__create_maps(evlist, target)) 1869657ee553SSong Liu goto out_delete_evlist; 1870657ee553SSong Liu 1871657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1872f72f901dSJiri Olsa if (evsel__open(counter, evlist->core.cpus, 187303617c22SJiri Olsa evlist->core.threads) < 0) 1874657ee553SSong Liu goto out_delete_evlist; 1875657ee553SSong Liu } 1876657ee553SSong Liu 1877657ee553SSong Liu if (perf_evlist__mmap(evlist, UINT_MAX)) 1878657ee553SSong Liu goto out_delete_evlist; 1879657ee553SSong Liu 1880657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1881ec7f24efSJiri Olsa if (evsel__enable(counter)) 1882657ee553SSong Liu goto out_delete_evlist; 1883657ee553SSong Liu } 1884657ee553SSong Liu 1885657ee553SSong Liu evlist->thread.done = 0; 1886657ee553SSong Liu if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist)) 1887657ee553SSong Liu goto out_delete_evlist; 1888657ee553SSong Liu 1889657ee553SSong Liu return 0; 1890657ee553SSong Liu 1891657ee553SSong Liu out_delete_evlist: 1892c12995a5SJiri Olsa evlist__delete(evlist); 1893657ee553SSong Liu evlist = NULL; 1894657ee553SSong Liu return -1; 1895657ee553SSong Liu } 1896657ee553SSong Liu 189763503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist) 1898657ee553SSong Liu { 1899657ee553SSong Liu if (!evlist) 1900657ee553SSong Liu return; 1901657ee553SSong Liu evlist->thread.done = 1; 1902657ee553SSong Liu pthread_join(evlist->thread.th, NULL); 1903c12995a5SJiri Olsa evlist__delete(evlist); 1904657ee553SSong Liu } 1905