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> 3770db7533SArnaldo Carvalho de Melo 38748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 39748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 40748fe088SArnaldo Carvalho de Melo #endif 41748fe088SArnaldo Carvalho de Melo 42f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 43a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 44f8a95309SArnaldo Carvalho de Melo 4552c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 469749b90eSJiri Olsa struct perf_thread_map *threads) 47361c99a6SArnaldo Carvalho de Melo { 4870db7533SArnaldo Carvalho de Melo int i; 4970db7533SArnaldo Carvalho de Melo 5070db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 5170db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 524562a739SJiri Olsa perf_evlist__init(&evlist->core); 537e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 541b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 5535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 5654cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 57361c99a6SArnaldo Carvalho de Melo } 58361c99a6SArnaldo Carvalho de Melo 590f98b11cSJiri Olsa struct evlist *evlist__new(void) 60ef1d1af2SArnaldo Carvalho de Melo { 6163503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist)); 62ef1d1af2SArnaldo Carvalho de Melo 63ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 6452c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL); 65ef1d1af2SArnaldo Carvalho de Melo 66361c99a6SArnaldo Carvalho de Melo return evlist; 67361c99a6SArnaldo Carvalho de Melo } 68361c99a6SArnaldo Carvalho de Melo 6963503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void) 70b22d54b0SJiri Olsa { 710f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 72b22d54b0SJiri Olsa 73b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 74c12995a5SJiri Olsa evlist__delete(evlist); 75b22d54b0SJiri Olsa evlist = NULL; 76b22d54b0SJiri Olsa } 77b22d54b0SJiri Olsa 78b22d54b0SJiri Olsa return evlist; 79b22d54b0SJiri Olsa } 80b22d54b0SJiri Olsa 8163503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void) 825bae0250SArnaldo Carvalho de Melo { 830f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 845bae0250SArnaldo Carvalho de Melo 855bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 86c12995a5SJiri Olsa evlist__delete(evlist); 875bae0250SArnaldo Carvalho de Melo evlist = NULL; 885bae0250SArnaldo Carvalho de Melo } 895bae0250SArnaldo Carvalho de Melo 905bae0250SArnaldo Carvalho de Melo return evlist; 915bae0250SArnaldo Carvalho de Melo } 925bae0250SArnaldo Carvalho de Melo 9375562573SAdrian Hunter /** 9475562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 9575562573SAdrian Hunter * @evlist: selected event list 9675562573SAdrian Hunter * 9775562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 9875562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 9975562573SAdrian Hunter */ 10063503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist) 10175562573SAdrian Hunter { 10232dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 10375562573SAdrian Hunter 10475562573SAdrian Hunter evlist->id_pos = first->id_pos; 10575562573SAdrian Hunter evlist->is_pos = first->is_pos; 10675562573SAdrian Hunter } 10775562573SAdrian Hunter 10863503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist) 109733cd2feSAdrian Hunter { 11032dcd021SJiri Olsa struct evsel *evsel; 111733cd2feSAdrian Hunter 112e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 113733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 114733cd2feSAdrian Hunter 115733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 116733cd2feSAdrian Hunter } 117733cd2feSAdrian Hunter 11863503dbaSJiri Olsa static void perf_evlist__purge(struct evlist *evlist) 119361c99a6SArnaldo Carvalho de Melo { 12032dcd021SJiri Olsa struct evsel *pos, *n; 121361c99a6SArnaldo Carvalho de Melo 122e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 123b27c4eceSJiri Olsa list_del_init(&pos->core.node); 124d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 1255eb2dd2aSJiri Olsa evsel__delete(pos); 126361c99a6SArnaldo Carvalho de Melo } 127361c99a6SArnaldo Carvalho de Melo 1286484d2f9SJiri Olsa evlist->core.nr_entries = 0; 129361c99a6SArnaldo Carvalho de Melo } 130361c99a6SArnaldo Carvalho de Melo 13163503dbaSJiri Olsa void perf_evlist__exit(struct evlist *evlist) 132ef1d1af2SArnaldo Carvalho de Melo { 13304662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1340b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 1351b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 136ef1d1af2SArnaldo Carvalho de Melo } 137ef1d1af2SArnaldo Carvalho de Melo 138c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist) 139361c99a6SArnaldo Carvalho de Melo { 1400b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1410b04b3dcSArnaldo Carvalho de Melo return; 1420b04b3dcSArnaldo Carvalho de Melo 143983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 144750b4edeSJiri Olsa evlist__close(evlist); 14538f01d8dSJiri Olsa perf_cpu_map__put(evlist->cpus); 1467836e52eSJiri Olsa perf_thread_map__put(evlist->threads); 14703ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 14803ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 149361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 150ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 151361c99a6SArnaldo Carvalho de Melo free(evlist); 152361c99a6SArnaldo Carvalho de Melo } 153361c99a6SArnaldo Carvalho de Melo 15463503dbaSJiri Olsa static void __perf_evlist__propagate_maps(struct evlist *evlist, 15532dcd021SJiri Olsa struct evsel *evsel) 156adc0c3e8SAdrian Hunter { 157adc0c3e8SAdrian Hunter /* 158adc0c3e8SAdrian Hunter * We already have cpus for evsel (via PMU sysfs) so 159adc0c3e8SAdrian Hunter * keep it, if there's no target cpu list defined. 160adc0c3e8SAdrian Hunter */ 161adc0c3e8SAdrian Hunter if (!evsel->own_cpus || evlist->has_user_cpus) { 16238f01d8dSJiri Olsa perf_cpu_map__put(evsel->cpus); 16338f01d8dSJiri Olsa evsel->cpus = perf_cpu_map__get(evlist->cpus); 164adc0c3e8SAdrian Hunter } else if (evsel->cpus != evsel->own_cpus) { 16538f01d8dSJiri Olsa perf_cpu_map__put(evsel->cpus); 16638f01d8dSJiri Olsa evsel->cpus = perf_cpu_map__get(evsel->own_cpus); 167adc0c3e8SAdrian Hunter } 168adc0c3e8SAdrian Hunter 1697836e52eSJiri Olsa perf_thread_map__put(evsel->threads); 1707836e52eSJiri Olsa evsel->threads = perf_thread_map__get(evlist->threads); 171adc0c3e8SAdrian Hunter } 172adc0c3e8SAdrian Hunter 17363503dbaSJiri Olsa static void perf_evlist__propagate_maps(struct evlist *evlist) 174adc0c3e8SAdrian Hunter { 17532dcd021SJiri Olsa struct evsel *evsel; 176adc0c3e8SAdrian Hunter 177e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 178adc0c3e8SAdrian Hunter __perf_evlist__propagate_maps(evlist, evsel); 179adc0c3e8SAdrian Hunter } 180adc0c3e8SAdrian Hunter 181a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 182361c99a6SArnaldo Carvalho de Melo { 183d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1846484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 18560b0896cSAdrian Hunter entry->tracking = !entry->idx; 186ef503831SArnaldo Carvalho de Melo 1876484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1886484d2f9SJiri Olsa 1896484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 19075562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 19144c42d71SAdrian Hunter 19244c42d71SAdrian Hunter __perf_evlist__propagate_maps(evlist, entry); 193361c99a6SArnaldo Carvalho de Melo } 194361c99a6SArnaldo Carvalho de Melo 19516251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1964768230aSAdrian Hunter { 1974768230aSAdrian Hunter evsel->evlist = NULL; 19852e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1994768230aSAdrian Hunter } 2004768230aSAdrian Hunter 20163503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist, 202f114d6efSAdrian Hunter struct list_head *list) 20350d08e47SArnaldo Carvalho de Melo { 20432dcd021SJiri Olsa struct evsel *evsel, *temp; 20575562573SAdrian Hunter 206e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 207b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 208a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 209f114d6efSAdrian Hunter } 21050d08e47SArnaldo Carvalho de Melo } 21150d08e47SArnaldo Carvalho de Melo 21263dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 21363dab225SArnaldo Carvalho de Melo { 21432dcd021SJiri Olsa struct evsel *evsel, *leader; 21563dab225SArnaldo Carvalho de Melo 216b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 217b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 21897f63e4aSNamhyung Kim 21997f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 22063dab225SArnaldo Carvalho de Melo 221e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22263dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22363dab225SArnaldo Carvalho de Melo } 22463dab225SArnaldo Carvalho de Melo } 22563dab225SArnaldo Carvalho de Melo 22663503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist) 2276a4bb04cSJiri Olsa { 2286484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2296484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 230ce9036a6SJiri Olsa __perf_evlist__set_leader(&evlist->core.entries); 2316a4bb04cSJiri Olsa } 23297f63e4aSNamhyung Kim } 2336a4bb04cSJiri Olsa 23463503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise) 235361c99a6SArnaldo Carvalho de Melo { 23632dcd021SJiri Olsa struct evsel *evsel = perf_evsel__new_cycles(precise); 237361c99a6SArnaldo Carvalho de Melo 238361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2397c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 240361c99a6SArnaldo Carvalho de Melo 241a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 242361c99a6SArnaldo Carvalho de Melo return 0; 243361c99a6SArnaldo Carvalho de Melo } 2445c581041SArnaldo Carvalho de Melo 24563503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist) 2465bae0250SArnaldo Carvalho de Melo { 2475bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2485bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2495bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2505bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2515bae0250SArnaldo Carvalho de Melo }; 2526484d2f9SJiri Olsa struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries); 2535bae0250SArnaldo Carvalho de Melo 2545bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2555bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2565bae0250SArnaldo Carvalho de Melo 257a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2585bae0250SArnaldo Carvalho de Melo return 0; 2595bae0250SArnaldo Carvalho de Melo } 2605bae0250SArnaldo Carvalho de Melo 261a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist, 26250d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 26350d08e47SArnaldo Carvalho de Melo { 26432dcd021SJiri Olsa struct evsel *evsel, *n; 26550d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 26650d08e47SArnaldo Carvalho de Melo size_t i; 26750d08e47SArnaldo Carvalho de Melo 26850d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2696484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 27050d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27150d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 272b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 27350d08e47SArnaldo Carvalho de Melo } 27450d08e47SArnaldo Carvalho de Melo 275f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 27650d08e47SArnaldo Carvalho de Melo 27750d08e47SArnaldo Carvalho de Melo return 0; 27850d08e47SArnaldo Carvalho de Melo 27950d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 280e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2815eb2dd2aSJiri Olsa evsel__delete(evsel); 28250d08e47SArnaldo Carvalho de Melo return -1; 28350d08e47SArnaldo Carvalho de Melo } 28450d08e47SArnaldo Carvalho de Melo 28563503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist, 28679695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 28779695e1bSArnaldo Carvalho de Melo { 28879695e1bSArnaldo Carvalho de Melo size_t i; 28979695e1bSArnaldo Carvalho de Melo 29079695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 29179695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 29279695e1bSArnaldo Carvalho de Melo 293a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 29479695e1bSArnaldo Carvalho de Melo } 29579695e1bSArnaldo Carvalho de Melo 29632dcd021SJiri Olsa struct evsel * 29763503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 298ee29be62SArnaldo Carvalho de Melo { 29932dcd021SJiri Olsa struct evsel *evsel; 300ee29be62SArnaldo Carvalho de Melo 301e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3021fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3031fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 304ee29be62SArnaldo Carvalho de Melo return evsel; 305ee29be62SArnaldo Carvalho de Melo } 306ee29be62SArnaldo Carvalho de Melo 307ee29be62SArnaldo Carvalho de Melo return NULL; 308ee29be62SArnaldo Carvalho de Melo } 309ee29be62SArnaldo Carvalho de Melo 31032dcd021SJiri Olsa struct evsel * 31163503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist, 312a2f2804aSDavid Ahern const char *name) 313a2f2804aSDavid Ahern { 31432dcd021SJiri Olsa struct evsel *evsel; 315a2f2804aSDavid Ahern 316e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3171fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 318a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 319a2f2804aSDavid Ahern return evsel; 320a2f2804aSDavid Ahern } 321a2f2804aSDavid Ahern 322a2f2804aSDavid Ahern return NULL; 323a2f2804aSDavid Ahern } 324a2f2804aSDavid Ahern 32563503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist, 32639876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 32739876e7dSArnaldo Carvalho de Melo { 32832dcd021SJiri Olsa struct evsel *evsel = perf_evsel__newtp(sys, name); 32939876e7dSArnaldo Carvalho de Melo 3308dd2a131SJiri Olsa if (IS_ERR(evsel)) 33139876e7dSArnaldo Carvalho de Melo return -1; 33239876e7dSArnaldo Carvalho de Melo 333744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 334a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 33539876e7dSArnaldo Carvalho de Melo return 0; 33639876e7dSArnaldo Carvalho de Melo } 33739876e7dSArnaldo Carvalho de Melo 33863503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist, 33932dcd021SJiri Olsa struct evsel *evsel) 340bf8e8f4bSAdrian Hunter { 341bf8e8f4bSAdrian Hunter if (evsel->system_wide) 342bf8e8f4bSAdrian Hunter return 1; 343bf8e8f4bSAdrian Hunter else 344bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 345bf8e8f4bSAdrian Hunter } 346bf8e8f4bSAdrian Hunter 347e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3484152ab37SArnaldo Carvalho de Melo { 34932dcd021SJiri Olsa struct evsel *pos; 3504152ab37SArnaldo Carvalho de Melo 351e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 352b7e8452bSArnaldo Carvalho de Melo if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->fd) 3533fe4430dSJiri Olsa continue; 3549a10bb22SJiri Olsa evsel__disable(pos); 3554152ab37SArnaldo Carvalho de Melo } 3562b56bcfbSArnaldo Carvalho de Melo 3572b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3584152ab37SArnaldo Carvalho de Melo } 3594152ab37SArnaldo Carvalho de Melo 3601c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 361764e16a3SDavid Ahern { 36232dcd021SJiri Olsa struct evsel *pos; 363764e16a3SDavid Ahern 364e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 365395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3663fe4430dSJiri Olsa continue; 367ec7f24efSJiri Olsa evsel__enable(pos); 368764e16a3SDavid Ahern } 3692b56bcfbSArnaldo Carvalho de Melo 3702b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3712b56bcfbSArnaldo Carvalho de Melo } 3722b56bcfbSArnaldo Carvalho de Melo 37363503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist) 3742b56bcfbSArnaldo Carvalho de Melo { 375e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 376764e16a3SDavid Ahern } 377764e16a3SDavid Ahern 37863503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist, 37932dcd021SJiri Olsa struct evsel *evsel, int cpu) 3801c65056cSAdrian Hunter { 38118ef15c6SArnaldo Carvalho de Melo int thread; 3821c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3831c65056cSAdrian Hunter 3841c65056cSAdrian Hunter if (!evsel->fd) 3851c65056cSAdrian Hunter return -EINVAL; 3861c65056cSAdrian Hunter 3871c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 38818ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3891c65056cSAdrian Hunter if (err) 3901c65056cSAdrian Hunter return err; 3911c65056cSAdrian Hunter } 3921c65056cSAdrian Hunter return 0; 3931c65056cSAdrian Hunter } 3941c65056cSAdrian Hunter 39563503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist, 39632dcd021SJiri Olsa struct evsel *evsel, 3971c65056cSAdrian Hunter int thread) 3981c65056cSAdrian Hunter { 39918ef15c6SArnaldo Carvalho de Melo int cpu; 4001c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 4011c65056cSAdrian Hunter 4021c65056cSAdrian Hunter if (!evsel->fd) 4031c65056cSAdrian Hunter return -EINVAL; 4041c65056cSAdrian Hunter 4051c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 40618ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4071c65056cSAdrian Hunter if (err) 4081c65056cSAdrian Hunter return err; 4091c65056cSAdrian Hunter } 4101c65056cSAdrian Hunter return 0; 4111c65056cSAdrian Hunter } 4121c65056cSAdrian Hunter 41363503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist, 41432dcd021SJiri Olsa struct evsel *evsel, int idx) 4151c65056cSAdrian Hunter { 4161c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4171c65056cSAdrian Hunter 4181c65056cSAdrian Hunter if (per_cpu_mmaps) 4191c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4201c65056cSAdrian Hunter else 4211c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4221c65056cSAdrian Hunter } 4231c65056cSAdrian Hunter 42463503dbaSJiri Olsa int perf_evlist__alloc_pollfd(struct evlist *evlist) 4255c581041SArnaldo Carvalho de Melo { 426b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 427b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 428bf8e8f4bSAdrian Hunter int nfds = 0; 42932dcd021SJiri Olsa struct evsel *evsel; 430bf8e8f4bSAdrian Hunter 431e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 432bf8e8f4bSAdrian Hunter if (evsel->system_wide) 433bf8e8f4bSAdrian Hunter nfds += nr_cpus; 434bf8e8f4bSAdrian Hunter else 435bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 436bf8e8f4bSAdrian Hunter } 437bf8e8f4bSAdrian Hunter 4381b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4391b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 440ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 441ad6765ddSArnaldo Carvalho de Melo 442ad6765ddSArnaldo Carvalho de Melo return 0; 4435c581041SArnaldo Carvalho de Melo } 44470082dd9SArnaldo Carvalho de Melo 44563503dbaSJiri Olsa static int __perf_evlist__add_pollfd(struct evlist *evlist, int fd, 4464876075bSWang Nan struct perf_mmap *map, short revent) 447e4b356b5SArnaldo Carvalho de Melo { 448f3058a1cSWang Nan int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); 449e4b356b5SArnaldo Carvalho de Melo /* 450e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 451e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 452e4b356b5SArnaldo Carvalho de Melo */ 453e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 4544876075bSWang Nan evlist->pollfd.priv[pos].ptr = map; 455e4b356b5SArnaldo Carvalho de Melo 456e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 457e4b356b5SArnaldo Carvalho de Melo } 458e4b356b5SArnaldo Carvalho de Melo 459e4b356b5SArnaldo Carvalho de Melo return pos; 460e4b356b5SArnaldo Carvalho de Melo } 461e4b356b5SArnaldo Carvalho de Melo 46263503dbaSJiri Olsa int perf_evlist__add_pollfd(struct evlist *evlist, int fd) 46370082dd9SArnaldo Carvalho de Melo { 4644876075bSWang Nan return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN); 465e4b356b5SArnaldo Carvalho de Melo } 4661b85337dSArnaldo Carvalho de Melo 467258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 468258e4bfcSWang Nan void *arg __maybe_unused) 469e4b356b5SArnaldo Carvalho de Melo { 4704876075bSWang Nan struct perf_mmap *map = fda->priv[fd].ptr; 471e4b356b5SArnaldo Carvalho de Melo 4724876075bSWang Nan if (map) 4734876075bSWang Nan perf_mmap__put(map); 47470082dd9SArnaldo Carvalho de Melo } 47570db7533SArnaldo Carvalho de Melo 47663503dbaSJiri Olsa int perf_evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 4771ddec7f0SArnaldo Carvalho de Melo { 478e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 479258e4bfcSWang Nan perf_evlist__munmap_filtered, NULL); 4801ddec7f0SArnaldo Carvalho de Melo } 4811ddec7f0SArnaldo Carvalho de Melo 48263503dbaSJiri Olsa int perf_evlist__poll(struct evlist *evlist, int timeout) 483f66a889dSArnaldo Carvalho de Melo { 4841b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 485f66a889dSArnaldo Carvalho de Melo } 486f66a889dSArnaldo Carvalho de Melo 48763503dbaSJiri Olsa static void perf_evlist__id_hash(struct evlist *evlist, 48832dcd021SJiri Olsa struct evsel *evsel, 4893d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4903d3b5e95SArnaldo Carvalho de Melo { 4913d3b5e95SArnaldo Carvalho de Melo int hash; 4923d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 4933d3b5e95SArnaldo Carvalho de Melo 4943d3b5e95SArnaldo Carvalho de Melo sid->id = id; 4953d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 4963d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 4973d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 4983d3b5e95SArnaldo Carvalho de Melo } 4993d3b5e95SArnaldo Carvalho de Melo 50063503dbaSJiri Olsa void perf_evlist__id_add(struct evlist *evlist, struct evsel *evsel, 501a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 502a91e5431SArnaldo Carvalho de Melo { 503a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 504a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 505a91e5431SArnaldo Carvalho de Melo } 506a91e5431SArnaldo Carvalho de Melo 50763503dbaSJiri Olsa int perf_evlist__id_add_fd(struct evlist *evlist, 50832dcd021SJiri Olsa struct evsel *evsel, 509f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 510f8a95309SArnaldo Carvalho de Melo { 511f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5123d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 513e2b5abe0SJiri Olsa u64 id; 514e2b5abe0SJiri Olsa int ret; 515e2b5abe0SJiri Olsa 516e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 517e2b5abe0SJiri Olsa if (!ret) 518e2b5abe0SJiri Olsa goto add; 519e2b5abe0SJiri Olsa 520e2b5abe0SJiri Olsa if (errno != ENOTTY) 521e2b5abe0SJiri Olsa return -1; 522e2b5abe0SJiri Olsa 523e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 524f8a95309SArnaldo Carvalho de Melo 525c4861afeSJiri Olsa /* 526c4861afeSJiri Olsa * This way does not work with group format read, so bail 527c4861afeSJiri Olsa * out in that case. 528c4861afeSJiri Olsa */ 529c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 530c4861afeSJiri Olsa return -1; 531c4861afeSJiri Olsa 5321fc632ceSJiri Olsa if (!(evsel->core.attr.read_format & PERF_FORMAT_ID) || 533f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 534f8a95309SArnaldo Carvalho de Melo return -1; 535f8a95309SArnaldo Carvalho de Melo 5361fc632ceSJiri Olsa if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 537f8a95309SArnaldo Carvalho de Melo ++id_idx; 5381fc632ceSJiri Olsa if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 539f8a95309SArnaldo Carvalho de Melo ++id_idx; 540f8a95309SArnaldo Carvalho de Melo 541e2b5abe0SJiri Olsa id = read_data[id_idx]; 542e2b5abe0SJiri Olsa 543e2b5abe0SJiri Olsa add: 544e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 545f8a95309SArnaldo Carvalho de Melo return 0; 546f8a95309SArnaldo Carvalho de Melo } 547f8a95309SArnaldo Carvalho de Melo 54863503dbaSJiri Olsa static void perf_evlist__set_sid_idx(struct evlist *evlist, 54932dcd021SJiri Olsa struct evsel *evsel, int idx, int cpu, 5503c659eedSAdrian Hunter int thread) 5513c659eedSAdrian Hunter { 5523c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5533c659eedSAdrian Hunter sid->idx = idx; 5543c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5553c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5563c659eedSAdrian Hunter else 5573c659eedSAdrian Hunter sid->cpu = -1; 5583c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 559e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5603c659eedSAdrian Hunter else 5613c659eedSAdrian Hunter sid->tid = -1; 5623c659eedSAdrian Hunter } 5633c659eedSAdrian Hunter 56463503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 56570db7533SArnaldo Carvalho de Melo { 56670db7533SArnaldo Carvalho de Melo struct hlist_head *head; 56770db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 56870db7533SArnaldo Carvalho de Melo int hash; 56970db7533SArnaldo Carvalho de Melo 57070db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 57170db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 57270db7533SArnaldo Carvalho de Melo 573b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 57470db7533SArnaldo Carvalho de Melo if (sid->id == id) 575932a3594SJiri Olsa return sid; 576932a3594SJiri Olsa 577932a3594SJiri Olsa return NULL; 578932a3594SJiri Olsa } 579932a3594SJiri Olsa 58063503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 581932a3594SJiri Olsa { 582932a3594SJiri Olsa struct perf_sample_id *sid; 583932a3594SJiri Olsa 5846484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 585932a3594SJiri Olsa return perf_evlist__first(evlist); 586932a3594SJiri Olsa 587932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 588932a3594SJiri Olsa if (sid) 58970db7533SArnaldo Carvalho de Melo return sid->evsel; 59030e68bccSNamhyung Kim 59130e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 5920c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 59330e68bccSNamhyung Kim 59470db7533SArnaldo Carvalho de Melo return NULL; 59570db7533SArnaldo Carvalho de Melo } 59604391debSArnaldo Carvalho de Melo 59763503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 598dddcf6abSAdrian Hunter u64 id) 599dddcf6abSAdrian Hunter { 600dddcf6abSAdrian Hunter struct perf_sample_id *sid; 601dddcf6abSAdrian Hunter 602dddcf6abSAdrian Hunter if (!id) 603dddcf6abSAdrian Hunter return NULL; 604dddcf6abSAdrian Hunter 605dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 606dddcf6abSAdrian Hunter if (sid) 607dddcf6abSAdrian Hunter return sid->evsel; 608dddcf6abSAdrian Hunter 609dddcf6abSAdrian Hunter return NULL; 610dddcf6abSAdrian Hunter } 611dddcf6abSAdrian Hunter 61263503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist, 61375562573SAdrian Hunter union perf_event *event, u64 *id) 61475562573SAdrian Hunter { 61575562573SAdrian Hunter const u64 *array = event->sample.array; 61675562573SAdrian Hunter ssize_t n; 61775562573SAdrian Hunter 61875562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 61975562573SAdrian Hunter 62075562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 62175562573SAdrian Hunter if (evlist->id_pos >= n) 62275562573SAdrian Hunter return -1; 62375562573SAdrian Hunter *id = array[evlist->id_pos]; 62475562573SAdrian Hunter } else { 62575562573SAdrian Hunter if (evlist->is_pos > n) 62675562573SAdrian Hunter return -1; 62775562573SAdrian Hunter n -= evlist->is_pos; 62875562573SAdrian Hunter *id = array[n]; 62975562573SAdrian Hunter } 63075562573SAdrian Hunter return 0; 63175562573SAdrian Hunter } 63275562573SAdrian Hunter 63363503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 63475562573SAdrian Hunter union perf_event *event) 63575562573SAdrian Hunter { 63632dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 63775562573SAdrian Hunter struct hlist_head *head; 63875562573SAdrian Hunter struct perf_sample_id *sid; 63975562573SAdrian Hunter int hash; 64075562573SAdrian Hunter u64 id; 64175562573SAdrian Hunter 6426484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 64398be6966SAdrian Hunter return first; 64498be6966SAdrian Hunter 6451fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 64698be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 64798be6966SAdrian Hunter return first; 64875562573SAdrian Hunter 64975562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 65075562573SAdrian Hunter return NULL; 65175562573SAdrian Hunter 65275562573SAdrian Hunter /* Synthesized events have an id of zero */ 65375562573SAdrian Hunter if (!id) 65498be6966SAdrian Hunter return first; 65575562573SAdrian Hunter 65675562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 65775562573SAdrian Hunter head = &evlist->heads[hash]; 65875562573SAdrian Hunter 65975562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 66075562573SAdrian Hunter if (sid->id == id) 66175562573SAdrian Hunter return sid->evsel; 66275562573SAdrian Hunter } 66375562573SAdrian Hunter return NULL; 66475562573SAdrian Hunter } 66575562573SAdrian Hunter 66663503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value) 66765aea233SWang Nan { 66865aea233SWang Nan int i; 66965aea233SWang Nan 6700b72d69aSWang Nan if (!evlist->overwrite_mmap) 671078c3386SWang Nan return 0; 672078c3386SWang Nan 67365aea233SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 6740b72d69aSWang Nan int fd = evlist->overwrite_mmap[i].fd; 67565aea233SWang Nan int err; 67665aea233SWang Nan 67765aea233SWang Nan if (fd < 0) 67865aea233SWang Nan continue; 67965aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 68065aea233SWang Nan if (err) 68165aea233SWang Nan return err; 68265aea233SWang Nan } 68365aea233SWang Nan return 0; 68465aea233SWang Nan } 68565aea233SWang Nan 68663503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist) 68765aea233SWang Nan { 68865aea233SWang Nan return perf_evlist__set_paused(evlist, true); 68965aea233SWang Nan } 69065aea233SWang Nan 69163503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist) 69265aea233SWang Nan { 69365aea233SWang Nan return perf_evlist__set_paused(evlist, false); 69465aea233SWang Nan } 69565aea233SWang Nan 69663503dbaSJiri Olsa static void perf_evlist__munmap_nofree(struct evlist *evlist) 697f8a95309SArnaldo Carvalho de Melo { 698aece948fSArnaldo Carvalho de Melo int i; 699f8a95309SArnaldo Carvalho de Melo 700b2cb615dSWang Nan if (evlist->mmap) 70193edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 702a1f72618SWang Nan perf_mmap__munmap(&evlist->mmap[i]); 703b2cb615dSWang Nan 7040b72d69aSWang Nan if (evlist->overwrite_mmap) 705b2cb615dSWang Nan for (i = 0; i < evlist->nr_mmaps; i++) 7060b72d69aSWang Nan perf_mmap__munmap(&evlist->overwrite_mmap[i]); 707a1f72618SWang Nan } 708aece948fSArnaldo Carvalho de Melo 70963503dbaSJiri Olsa void perf_evlist__munmap(struct evlist *evlist) 710a1f72618SWang Nan { 711a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 71204662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 7130b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 714f8a95309SArnaldo Carvalho de Melo } 715f8a95309SArnaldo Carvalho de Melo 71663503dbaSJiri Olsa static struct perf_mmap *perf_evlist__alloc_mmap(struct evlist *evlist, 7172c5f6d87SKan Liang bool overwrite) 718f8a95309SArnaldo Carvalho de Melo { 719d4c6fb36SWang Nan int i; 7208db6d6b1SWang Nan struct perf_mmap *map; 721d4c6fb36SWang Nan 722a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 723ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 724b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 7258db6d6b1SWang Nan map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 7268db6d6b1SWang Nan if (!map) 7278db6d6b1SWang Nan return NULL; 728946ae1d4SWang Nan 7294738ca30SArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 7308db6d6b1SWang Nan map[i].fd = -1; 7312c5f6d87SKan Liang map[i].overwrite = overwrite; 7324738ca30SArnaldo Carvalho de Melo /* 7334738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 7346afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 7354738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 7364738ca30SArnaldo Carvalho de Melo * dropped. 7374738ca30SArnaldo Carvalho de Melo * 7384738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 7394738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 7404738ca30SArnaldo Carvalho de Melo */ 7414738ca30SArnaldo Carvalho de Melo refcount_set(&map[i].refcnt, 0); 7424738ca30SArnaldo Carvalho de Melo } 7438db6d6b1SWang Nan return map; 744f8a95309SArnaldo Carvalho de Melo } 745f8a95309SArnaldo Carvalho de Melo 746f3058a1cSWang Nan static bool 74763503dbaSJiri Olsa perf_evlist__should_poll(struct evlist *evlist __maybe_unused, 74832dcd021SJiri Olsa struct evsel *evsel) 749f3058a1cSWang Nan { 7501fc632ceSJiri Olsa if (evsel->core.attr.write_backward) 751f3058a1cSWang Nan return false; 752f3058a1cSWang Nan return true; 753f3058a1cSWang Nan } 754f3058a1cSWang Nan 75563503dbaSJiri Olsa static int perf_evlist__mmap_per_evsel(struct evlist *evlist, int idx, 7569f21b815SMark Rutland struct mmap_params *mp, int cpu_idx, 7570b72d69aSWang Nan int thread, int *_output, int *_output_overwrite) 758aece948fSArnaldo Carvalho de Melo { 75932dcd021SJiri Olsa struct evsel *evsel; 760f3058a1cSWang Nan int revent; 7619f21b815SMark Rutland int evlist_cpu = cpu_map__cpu(evlist->cpus, cpu_idx); 76204e21314SAdrian Hunter 763e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 764078c3386SWang Nan struct perf_mmap *maps = evlist->mmap; 765078c3386SWang Nan int *output = _output; 766bf8e8f4bSAdrian Hunter int fd; 7679f21b815SMark Rutland int cpu; 768bf8e8f4bSAdrian Hunter 76971f566a3SWang Nan mp->prot = PROT_READ | PROT_WRITE; 7701fc632ceSJiri Olsa if (evsel->core.attr.write_backward) { 7710b72d69aSWang Nan output = _output_overwrite; 7720b72d69aSWang Nan maps = evlist->overwrite_mmap; 773078c3386SWang Nan 774078c3386SWang Nan if (!maps) { 7752c5f6d87SKan Liang maps = perf_evlist__alloc_mmap(evlist, true); 776078c3386SWang Nan if (!maps) 777078c3386SWang Nan return -1; 7780b72d69aSWang Nan evlist->overwrite_mmap = maps; 77954cc54deSWang Nan if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 78054cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 781078c3386SWang Nan } 78271f566a3SWang Nan mp->prot &= ~PROT_WRITE; 783078c3386SWang Nan } 784f3058a1cSWang Nan 785bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 786bf8e8f4bSAdrian Hunter continue; 787bf8e8f4bSAdrian Hunter 7889f21b815SMark Rutland cpu = cpu_map__idx(evsel->cpus, evlist_cpu); 7899f21b815SMark Rutland if (cpu == -1) 7909f21b815SMark Rutland continue; 7919f21b815SMark Rutland 792bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 79304e21314SAdrian Hunter 79404e21314SAdrian Hunter if (*output == -1) { 79504e21314SAdrian Hunter *output = fd; 796078c3386SWang Nan 79731fb4c0dSJiri Olsa if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0) 79804e21314SAdrian Hunter return -1; 79904e21314SAdrian Hunter } else { 80004e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 80104e21314SAdrian Hunter return -1; 80282396986SArnaldo Carvalho de Melo 803078c3386SWang Nan perf_mmap__get(&maps[idx]); 80404e21314SAdrian Hunter } 80504e21314SAdrian Hunter 806f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 807f3058a1cSWang Nan 808f90d194aSAdrian Hunter /* 809f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 810f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 811f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 812f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 813f90d194aSAdrian Hunter * Therefore don't add it for polling. 814f90d194aSAdrian Hunter */ 815f90d194aSAdrian Hunter if (!evsel->system_wide && 816078c3386SWang Nan __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) { 817078c3386SWang Nan perf_mmap__put(&maps[idx]); 818ad6765ddSArnaldo Carvalho de Melo return -1; 81982396986SArnaldo Carvalho de Melo } 820033fa713SArnaldo Carvalho de Melo 8211fc632ceSJiri Olsa if (evsel->core.attr.read_format & PERF_FORMAT_ID) { 8223c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 8233c659eedSAdrian Hunter fd) < 0) 82404e21314SAdrian Hunter return -1; 8253c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 8263c659eedSAdrian Hunter thread); 8273c659eedSAdrian Hunter } 82804e21314SAdrian Hunter } 82904e21314SAdrian Hunter 83004e21314SAdrian Hunter return 0; 83104e21314SAdrian Hunter } 83204e21314SAdrian Hunter 83363503dbaSJiri Olsa static int perf_evlist__mmap_per_cpu(struct evlist *evlist, 834a8a8f3ebSAdrian Hunter struct mmap_params *mp) 83504e21314SAdrian Hunter { 836aece948fSArnaldo Carvalho de Melo int cpu, thread; 837b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 838b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 839aece948fSArnaldo Carvalho de Melo 840e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 841b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 842aece948fSArnaldo Carvalho de Melo int output = -1; 8430b72d69aSWang Nan int output_overwrite = -1; 844aece948fSArnaldo Carvalho de Melo 845718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 846718c602dSAdrian Hunter true); 847718c602dSAdrian Hunter 848b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 849a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 8500b72d69aSWang Nan thread, &output, &output_overwrite)) 851aece948fSArnaldo Carvalho de Melo goto out_unmap; 852aece948fSArnaldo Carvalho de Melo } 853aece948fSArnaldo Carvalho de Melo } 854aece948fSArnaldo Carvalho de Melo 855aece948fSArnaldo Carvalho de Melo return 0; 856aece948fSArnaldo Carvalho de Melo 857aece948fSArnaldo Carvalho de Melo out_unmap: 858a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 859aece948fSArnaldo Carvalho de Melo return -1; 860aece948fSArnaldo Carvalho de Melo } 861aece948fSArnaldo Carvalho de Melo 86263503dbaSJiri Olsa static int perf_evlist__mmap_per_thread(struct evlist *evlist, 863a8a8f3ebSAdrian Hunter struct mmap_params *mp) 864aece948fSArnaldo Carvalho de Melo { 865aece948fSArnaldo Carvalho de Melo int thread; 866b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 867aece948fSArnaldo Carvalho de Melo 868e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 869b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 870aece948fSArnaldo Carvalho de Melo int output = -1; 8710b72d69aSWang Nan int output_overwrite = -1; 872aece948fSArnaldo Carvalho de Melo 873718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 874718c602dSAdrian Hunter false); 875718c602dSAdrian Hunter 876a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 8770b72d69aSWang Nan &output, &output_overwrite)) 878aece948fSArnaldo Carvalho de Melo goto out_unmap; 879aece948fSArnaldo Carvalho de Melo } 880aece948fSArnaldo Carvalho de Melo 881aece948fSArnaldo Carvalho de Melo return 0; 882aece948fSArnaldo Carvalho de Melo 883aece948fSArnaldo Carvalho de Melo out_unmap: 884a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 885aece948fSArnaldo Carvalho de Melo return -1; 886aece948fSArnaldo Carvalho de Melo } 887aece948fSArnaldo Carvalho de Melo 888f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 889994a1f78SJiri Olsa { 890f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 8918185e881SArnaldo Carvalho de Melo int max; 8928185e881SArnaldo Carvalho de Melo 8938185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 8948185e881SArnaldo Carvalho de Melo /* 8958185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 8968185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 8978185e881SArnaldo Carvalho de Melo * die yet... 8988185e881SArnaldo Carvalho de Melo */ 8998185e881SArnaldo Carvalho de Melo max = 512; 9008185e881SArnaldo Carvalho de Melo } else { 9018185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 9028185e881SArnaldo Carvalho de Melo } 9038185e881SArnaldo Carvalho de Melo 9048185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 9051be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 9061be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 907f5e7150cSArnaldo Carvalho de Melo 908f5e7150cSArnaldo Carvalho de Melo return pages; 909f5e7150cSArnaldo Carvalho de Melo } 910f5e7150cSArnaldo Carvalho de Melo 9110c582449SJiri Olsa size_t perf_evlist__mmap_size(unsigned long pages) 912f5e7150cSArnaldo Carvalho de Melo { 913f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 914f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 915f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 916994a1f78SJiri Olsa return 0; 917994a1f78SJiri Olsa 918994a1f78SJiri Olsa return (pages + 1) * page_size; 919994a1f78SJiri Olsa } 920994a1f78SJiri Olsa 92133c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 92233c2dcfdSDavid Ahern unsigned long max) 923994a1f78SJiri Olsa { 9242fbe4abeSAdrian Hunter unsigned long pages, val; 92527050f53SJiri Olsa static struct parse_tag tags[] = { 92627050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 92727050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 92827050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 92927050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 93027050f53SJiri Olsa { .tag = 0 }, 93127050f53SJiri Olsa }; 932994a1f78SJiri Olsa 9338973504bSDavid Ahern if (str == NULL) 93433c2dcfdSDavid Ahern return -EINVAL; 9358973504bSDavid Ahern 93627050f53SJiri Olsa val = parse_tag_value(str, tags); 9372fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 93827050f53SJiri Olsa /* we got file size value */ 93927050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 94027050f53SJiri Olsa } else { 94127050f53SJiri Olsa /* we got pages count value */ 94227050f53SJiri Olsa char *eptr; 943994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 94433c2dcfdSDavid Ahern if (*eptr != '\0') 94533c2dcfdSDavid Ahern return -EINVAL; 94627050f53SJiri Olsa } 947994a1f78SJiri Olsa 9482bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 94933c2dcfdSDavid Ahern /* leave number of pages at 0 */ 9501dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 9519808143bSJiri Olsa char buf[100]; 9529808143bSJiri Olsa 95333c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 95491529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 9551dbfa938SAdrian Hunter if (!pages) 9561dbfa938SAdrian Hunter return -EINVAL; 9579808143bSJiri Olsa 9589808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 9599808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 9609808143bSJiri Olsa buf, pages); 9612fbe4abeSAdrian Hunter } 9622fbe4abeSAdrian Hunter 96333c2dcfdSDavid Ahern if (pages > max) 96433c2dcfdSDavid Ahern return -EINVAL; 96533c2dcfdSDavid Ahern 96633c2dcfdSDavid Ahern return pages; 96733c2dcfdSDavid Ahern } 96833c2dcfdSDavid Ahern 969e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 97033c2dcfdSDavid Ahern { 97133c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 97233c2dcfdSDavid Ahern long pages; 97333c2dcfdSDavid Ahern 974f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 97533c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 97633c2dcfdSDavid Ahern 97733c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 97833c2dcfdSDavid Ahern if (pages < 0) { 97933c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 980994a1f78SJiri Olsa return -1; 981994a1f78SJiri Olsa } 982994a1f78SJiri Olsa 983994a1f78SJiri Olsa *mmap_pages = pages; 984994a1f78SJiri Olsa return 0; 985994a1f78SJiri Olsa } 986994a1f78SJiri Olsa 987e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 988e9db1310SAdrian Hunter int unset __maybe_unused) 989e9db1310SAdrian Hunter { 990e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 991e9db1310SAdrian Hunter } 992e9db1310SAdrian Hunter 993c83fa7f2SAdrian Hunter /** 994718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 995c83fa7f2SAdrian Hunter * @evlist: list of events 996c83fa7f2SAdrian Hunter * @pages: map length in pages 997c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 998718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 999718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1000f8a95309SArnaldo Carvalho de Melo * 1001c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1002c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1003c83fa7f2SAdrian Hunter * automatically. 1004f8a95309SArnaldo Carvalho de Melo * 1005718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1006718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1007718c602dSAdrian Hunter * 1008c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1009f8a95309SArnaldo Carvalho de Melo */ 101063503dbaSJiri Olsa int perf_evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 10117a276ff6SWang Nan unsigned int auxtrace_pages, 101251255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 101351255a8aSAlexey Budankov int comp_level) 1014f8a95309SArnaldo Carvalho de Melo { 101532dcd021SJiri Olsa struct evsel *evsel; 1016f854839bSJiri Olsa const struct perf_cpu_map *cpus = evlist->cpus; 10179749b90eSJiri Olsa const struct perf_thread_map *threads = evlist->threads; 101871f566a3SWang Nan /* 101971f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 102071f566a3SWang Nan * Its value is decided by evsel's write_backward. 102171f566a3SWang Nan * So &mp should not be passed through const pointer. 102271f566a3SWang Nan */ 102351255a8aSAlexey Budankov struct mmap_params mp = { .nr_cblocks = nr_cblocks, .affinity = affinity, .flush = flush, 102451255a8aSAlexey Budankov .comp_level = comp_level }; 102550a682ceSArnaldo Carvalho de Melo 10268db6d6b1SWang Nan if (!evlist->mmap) 10272c5f6d87SKan Liang evlist->mmap = perf_evlist__alloc_mmap(evlist, false); 10288db6d6b1SWang Nan if (!evlist->mmap) 1029f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1030f8a95309SArnaldo Carvalho de Melo 10311b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1032f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1033f8a95309SArnaldo Carvalho de Melo 1034994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 10352af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1036a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1037f8a95309SArnaldo Carvalho de Melo 1038718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1039718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1040718c602dSAdrian Hunter 1041e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 10421fc632ceSJiri Olsa if ((evsel->core.attr.read_format & PERF_FORMAT_ID) && 1043a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1044a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1045f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1046f8a95309SArnaldo Carvalho de Melo } 1047f8a95309SArnaldo Carvalho de Melo 1048ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1049a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1050f8a95309SArnaldo Carvalho de Melo 1051a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1052f8a95309SArnaldo Carvalho de Melo } 10537e2ed097SArnaldo Carvalho de Melo 105463503dbaSJiri Olsa int perf_evlist__mmap(struct evlist *evlist, unsigned int pages) 1055718c602dSAdrian Hunter { 105651255a8aSAlexey Budankov return perf_evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 1057718c602dSAdrian Hunter } 1058718c602dSAdrian Hunter 105963503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 10607e2ed097SArnaldo Carvalho de Melo { 1061147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 1062f854839bSJiri Olsa struct perf_cpu_map *cpus; 10639749b90eSJiri Olsa struct perf_thread_map *threads; 10647e2ed097SArnaldo Carvalho de Melo 1065147c508fSJin Yao /* 1066147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 1067147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 1068147c508fSJin Yao * target->system_wide = true. 1069147c508fSJin Yao * 1070147c508fSJin Yao * If specify '--per-thread' only to perf record, 1071147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 1072147c508fSJin Yao * 1073147c508fSJin Yao * So target->per_thread && target->system_wide is false. 1074147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 1075147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 1076147c508fSJin Yao * current behavior. 1077147c508fSJin Yao * 1078147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 1079147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 1080147c508fSJin Yao * per-thread data. thread_map__new_str will call 1081147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 1082147c508fSJin Yao */ 108373c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 1084147c508fSJin Yao all_threads); 108574bfd2b2SAdrian Hunter 108674bfd2b2SAdrian Hunter if (!threads) 10877e2ed097SArnaldo Carvalho de Melo return -1; 10887e2ed097SArnaldo Carvalho de Melo 10899c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 1090397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 1091879d77d0SNamhyung Kim else 109274bfd2b2SAdrian Hunter cpus = cpu_map__new(target->cpu_list); 10937e2ed097SArnaldo Carvalho de Melo 109474bfd2b2SAdrian Hunter if (!cpus) 10957e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 10967e2ed097SArnaldo Carvalho de Melo 1097ec9a77a7SAdrian Hunter evlist->has_user_cpus = !!target->cpu_list; 1098ec9a77a7SAdrian Hunter 109974bfd2b2SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 1100d5bc056eSAdrian Hunter 1101d5bc056eSAdrian Hunter return 0; 11027e2ed097SArnaldo Carvalho de Melo 11037e2ed097SArnaldo Carvalho de Melo out_delete_threads: 11047836e52eSJiri Olsa perf_thread_map__put(threads); 11057e2ed097SArnaldo Carvalho de Melo return -1; 11067e2ed097SArnaldo Carvalho de Melo } 11077e2ed097SArnaldo Carvalho de Melo 110863503dbaSJiri Olsa void perf_evlist__set_maps(struct evlist *evlist, struct perf_cpu_map *cpus, 11099749b90eSJiri Olsa struct perf_thread_map *threads) 11103de5cfb0SJiri Olsa { 1111934e0f20SAdrian Hunter /* 1112934e0f20SAdrian Hunter * Allow for the possibility that one or another of the maps isn't being 1113934e0f20SAdrian Hunter * changed i.e. don't put it. Note we are assuming the maps that are 1114934e0f20SAdrian Hunter * being applied are brand new and evlist is taking ownership of the 1115934e0f20SAdrian Hunter * original reference count of 1. If that is not the case it is up to 1116934e0f20SAdrian Hunter * the caller to increase the reference count. 1117934e0f20SAdrian Hunter */ 1118934e0f20SAdrian Hunter if (cpus != evlist->cpus) { 111938f01d8dSJiri Olsa perf_cpu_map__put(evlist->cpus); 112038f01d8dSJiri Olsa evlist->cpus = perf_cpu_map__get(cpus); 1121934e0f20SAdrian Hunter } 11223de5cfb0SJiri Olsa 1123934e0f20SAdrian Hunter if (threads != evlist->threads) { 11247836e52eSJiri Olsa perf_thread_map__put(evlist->threads); 11257836e52eSJiri Olsa evlist->threads = perf_thread_map__get(threads); 1126934e0f20SAdrian Hunter } 11273de5cfb0SJiri Olsa 1128ec9a77a7SAdrian Hunter perf_evlist__propagate_maps(evlist); 11293de5cfb0SJiri Olsa } 11303de5cfb0SJiri Olsa 113163503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist, 113222c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 113322c8a376SArnaldo Carvalho de Melo { 113432dcd021SJiri Olsa struct evsel *evsel; 113522c8a376SArnaldo Carvalho de Melo 1136e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 113722c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 113822c8a376SArnaldo Carvalho de Melo } 113922c8a376SArnaldo Carvalho de Melo 114063503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist, 114122c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 114222c8a376SArnaldo Carvalho de Melo { 114332dcd021SJiri Olsa struct evsel *evsel; 114422c8a376SArnaldo Carvalho de Melo 1145e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 114622c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 114722c8a376SArnaldo Carvalho de Melo } 114822c8a376SArnaldo Carvalho de Melo 114963503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 11500a102479SFrederic Weisbecker { 115132dcd021SJiri Olsa struct evsel *evsel; 1152745cefc5SArnaldo Carvalho de Melo int err = 0; 11530a102479SFrederic Weisbecker 1154e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1155745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 11560a102479SFrederic Weisbecker continue; 1157745cefc5SArnaldo Carvalho de Melo 1158d988d5eeSKan Liang /* 1159d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1160d988d5eeSKan Liang * So evlist and evsel should always be same. 1161d988d5eeSKan Liang */ 116224e376b2SJiri Olsa err = evsel__apply_filter(evsel, evsel->filter); 116323d4aad4SArnaldo Carvalho de Melo if (err) { 116423d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1165745cefc5SArnaldo Carvalho de Melo break; 11660a102479SFrederic Weisbecker } 116723d4aad4SArnaldo Carvalho de Melo } 11680a102479SFrederic Weisbecker 1169745cefc5SArnaldo Carvalho de Melo return err; 1170745cefc5SArnaldo Carvalho de Melo } 1171745cefc5SArnaldo Carvalho de Melo 117263503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1173745cefc5SArnaldo Carvalho de Melo { 117432dcd021SJiri Olsa struct evsel *evsel; 1175745cefc5SArnaldo Carvalho de Melo int err = 0; 1176745cefc5SArnaldo Carvalho de Melo 1177e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 11781fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1179fdf14720SWang Nan continue; 1180fdf14720SWang Nan 118194ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1182745cefc5SArnaldo Carvalho de Melo if (err) 1183745cefc5SArnaldo Carvalho de Melo break; 1184745cefc5SArnaldo Carvalho de Melo } 1185745cefc5SArnaldo Carvalho de Melo 1186745cefc5SArnaldo Carvalho de Melo return err; 11870a102479SFrederic Weisbecker } 118874429964SFrederic Weisbecker 118963503dbaSJiri Olsa int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 1190cfd70a26SArnaldo Carvalho de Melo { 1191cfd70a26SArnaldo Carvalho de Melo char *filter; 1192be199adaSArnaldo Carvalho de Melo int ret = -1; 1193be199adaSArnaldo Carvalho de Melo size_t i; 1194cfd70a26SArnaldo Carvalho de Melo 1195be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1196be199adaSArnaldo Carvalho de Melo if (i == 0) { 1197be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1198cfd70a26SArnaldo Carvalho de Melo return -1; 1199be199adaSArnaldo Carvalho de Melo } else { 1200be199adaSArnaldo Carvalho de Melo char *tmp; 1201be199adaSArnaldo Carvalho de Melo 1202be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1203be199adaSArnaldo Carvalho de Melo goto out_free; 1204be199adaSArnaldo Carvalho de Melo 1205be199adaSArnaldo Carvalho de Melo free(filter); 1206be199adaSArnaldo Carvalho de Melo filter = tmp; 1207be199adaSArnaldo Carvalho de Melo } 1208be199adaSArnaldo Carvalho de Melo } 1209cfd70a26SArnaldo Carvalho de Melo 12107ad92a33SArnaldo Carvalho de Melo ret = perf_evlist__set_tp_filter(evlist, filter); 1211be199adaSArnaldo Carvalho de Melo out_free: 1212cfd70a26SArnaldo Carvalho de Melo free(filter); 1213cfd70a26SArnaldo Carvalho de Melo return ret; 1214cfd70a26SArnaldo Carvalho de Melo } 1215cfd70a26SArnaldo Carvalho de Melo 121663503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1217be199adaSArnaldo Carvalho de Melo { 12187ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1219be199adaSArnaldo Carvalho de Melo } 1220be199adaSArnaldo Carvalho de Melo 122163503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist) 122274429964SFrederic Weisbecker { 122332dcd021SJiri Olsa struct evsel *pos; 1224c2a70653SArnaldo Carvalho de Melo 12256484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 122675562573SAdrian Hunter return true; 122775562573SAdrian Hunter 122875562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 122975562573SAdrian Hunter return false; 123075562573SAdrian Hunter 1231e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 123275562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 123375562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1234c2a70653SArnaldo Carvalho de Melo return false; 123574429964SFrederic Weisbecker } 123674429964SFrederic Weisbecker 1237c2a70653SArnaldo Carvalho de Melo return true; 1238c2a70653SArnaldo Carvalho de Melo } 1239c2a70653SArnaldo Carvalho de Melo 124063503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist) 1241c2a70653SArnaldo Carvalho de Melo { 124232dcd021SJiri Olsa struct evsel *evsel; 124375562573SAdrian Hunter 124475562573SAdrian Hunter if (evlist->combined_sample_type) 124575562573SAdrian Hunter return evlist->combined_sample_type; 124675562573SAdrian Hunter 1247e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 12481fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 124975562573SAdrian Hunter 125075562573SAdrian Hunter return evlist->combined_sample_type; 125175562573SAdrian Hunter } 125275562573SAdrian Hunter 125363503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist) 125475562573SAdrian Hunter { 125575562573SAdrian Hunter evlist->combined_sample_type = 0; 125675562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1257c2a70653SArnaldo Carvalho de Melo } 1258c2a70653SArnaldo Carvalho de Melo 125963503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist) 126098df858eSAndi Kleen { 126132dcd021SJiri Olsa struct evsel *evsel; 126298df858eSAndi Kleen u64 branch_type = 0; 126398df858eSAndi Kleen 1264e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 12651fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 126698df858eSAndi Kleen return branch_type; 126798df858eSAndi Kleen } 126898df858eSAndi Kleen 126963503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 12709ede473cSJiri Olsa { 127132dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist), *pos = first; 12721fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 12731fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 12749ede473cSJiri Olsa 1275e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 12761fc632ceSJiri Olsa if (read_format != pos->core.attr.read_format) 12779ede473cSJiri Olsa return false; 12789ede473cSJiri Olsa } 12799ede473cSJiri Olsa 12809ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 12819ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 12829ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 12839ede473cSJiri Olsa return false; 12849ede473cSJiri Olsa } 12859ede473cSJiri Olsa 12869ede473cSJiri Olsa return true; 12879ede473cSJiri Olsa } 12889ede473cSJiri Olsa 128963503dbaSJiri Olsa u64 perf_evlist__read_format(struct evlist *evlist) 12909ede473cSJiri Olsa { 129132dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 12921fc632ceSJiri Olsa return first->core.attr.read_format; 12939ede473cSJiri Olsa } 12949ede473cSJiri Olsa 129563503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 129681e36bffSArnaldo Carvalho de Melo { 129732dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 129881e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 129981e36bffSArnaldo Carvalho de Melo u64 sample_type; 130081e36bffSArnaldo Carvalho de Melo u16 size = 0; 130181e36bffSArnaldo Carvalho de Melo 13021fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 130381e36bffSArnaldo Carvalho de Melo goto out; 130481e36bffSArnaldo Carvalho de Melo 13051fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 130681e36bffSArnaldo Carvalho de Melo 130781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 130881e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 130981e36bffSArnaldo Carvalho de Melo 131081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 131181e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 131281e36bffSArnaldo Carvalho de Melo 131381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 131481e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 131581e36bffSArnaldo Carvalho de Melo 131681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 131781e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 131881e36bffSArnaldo Carvalho de Melo 131981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 132081e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 132175562573SAdrian Hunter 132275562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 132375562573SAdrian Hunter size += sizeof(data->id); 132481e36bffSArnaldo Carvalho de Melo out: 132581e36bffSArnaldo Carvalho de Melo return size; 132681e36bffSArnaldo Carvalho de Melo } 132781e36bffSArnaldo Carvalho de Melo 132863503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist) 1329c2a70653SArnaldo Carvalho de Melo { 133032dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist), *pos = first; 1331c2a70653SArnaldo Carvalho de Melo 1332e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 13331fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1334c2a70653SArnaldo Carvalho de Melo return false; 1335c2a70653SArnaldo Carvalho de Melo } 1336c2a70653SArnaldo Carvalho de Melo 1337c2a70653SArnaldo Carvalho de Melo return true; 133874429964SFrederic Weisbecker } 133974429964SFrederic Weisbecker 134063503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist) 134174429964SFrederic Weisbecker { 134232dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 13431fc632ceSJiri Olsa return first->core.attr.sample_id_all; 134474429964SFrederic Weisbecker } 134581cce8deSArnaldo Carvalho de Melo 134663503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 134732dcd021SJiri Olsa struct evsel *evsel) 134881cce8deSArnaldo Carvalho de Melo { 134981cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 135081cce8deSArnaldo Carvalho de Melo } 1351727ab04eSArnaldo Carvalho de Melo 1352750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1353a74b4b66SNamhyung Kim { 135432dcd021SJiri Olsa struct evsel *evsel; 1355a74b4b66SNamhyung Kim 1356475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 1357475fb533SAndi Kleen perf_evsel__close(evsel); 1358a74b4b66SNamhyung Kim } 1359a74b4b66SNamhyung Kim 136063503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 13614112eb18SArnaldo Carvalho de Melo { 1362f854839bSJiri Olsa struct perf_cpu_map *cpus; 13639749b90eSJiri Olsa struct perf_thread_map *threads; 13644112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 13654112eb18SArnaldo Carvalho de Melo 13664112eb18SArnaldo Carvalho de Melo /* 13674112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 13684112eb18SArnaldo Carvalho de Melo * an all cpus map. 13694112eb18SArnaldo Carvalho de Melo * 13704112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 13714112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 13724112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 13734112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 13744112eb18SArnaldo Carvalho de Melo */ 13758c0498b6SAdrian Hunter cpus = cpu_map__new(NULL); 13768c0498b6SAdrian Hunter if (!cpus) 13774112eb18SArnaldo Carvalho de Melo goto out; 13784112eb18SArnaldo Carvalho de Melo 13794b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 13808c0498b6SAdrian Hunter if (!threads) 13818c0498b6SAdrian Hunter goto out_put; 13824112eb18SArnaldo Carvalho de Melo 13838c0498b6SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 13844112eb18SArnaldo Carvalho de Melo out: 13854112eb18SArnaldo Carvalho de Melo return err; 13868c0498b6SAdrian Hunter out_put: 138738f01d8dSJiri Olsa perf_cpu_map__put(cpus); 13884112eb18SArnaldo Carvalho de Melo goto out; 13894112eb18SArnaldo Carvalho de Melo } 13904112eb18SArnaldo Carvalho de Melo 1391474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1392727ab04eSArnaldo Carvalho de Melo { 139332dcd021SJiri Olsa struct evsel *evsel; 1394a74b4b66SNamhyung Kim int err; 1395727ab04eSArnaldo Carvalho de Melo 13964112eb18SArnaldo Carvalho de Melo /* 13974112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 13984112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 13994112eb18SArnaldo Carvalho de Melo */ 14004112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 14014112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 14024112eb18SArnaldo Carvalho de Melo if (err < 0) 14034112eb18SArnaldo Carvalho de Melo goto out_err; 14044112eb18SArnaldo Carvalho de Melo } 14054112eb18SArnaldo Carvalho de Melo 1406733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1407733cd2feSAdrian Hunter 1408e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 14095972d1e0SJiri Olsa err = evsel__open(evsel, evsel->cpus, evsel->threads); 1410727ab04eSArnaldo Carvalho de Melo if (err < 0) 1411727ab04eSArnaldo Carvalho de Melo goto out_err; 1412727ab04eSArnaldo Carvalho de Melo } 1413727ab04eSArnaldo Carvalho de Melo 1414727ab04eSArnaldo Carvalho de Melo return 0; 1415727ab04eSArnaldo Carvalho de Melo out_err: 1416750b4edeSJiri Olsa evlist__close(evlist); 141741c21a68SNamhyung Kim errno = -err; 1418727ab04eSArnaldo Carvalho de Melo return err; 1419727ab04eSArnaldo Carvalho de Melo } 142035b9d88eSArnaldo Carvalho de Melo 142163503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 142255e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1423735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 142435b9d88eSArnaldo Carvalho de Melo { 142535b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 142635b9d88eSArnaldo Carvalho de Melo char bf; 142735b9d88eSArnaldo Carvalho de Melo 142835b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 142935b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 143035b9d88eSArnaldo Carvalho de Melo return -1; 143135b9d88eSArnaldo Carvalho de Melo } 143235b9d88eSArnaldo Carvalho de Melo 143335b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 143435b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 143535b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 143635b9d88eSArnaldo Carvalho de Melo } 143735b9d88eSArnaldo Carvalho de Melo 143835b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 143935b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 144035b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 144135b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 144235b9d88eSArnaldo Carvalho de Melo } 144335b9d88eSArnaldo Carvalho de Melo 144435b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 14455f1c4225SArnaldo Carvalho de Melo int ret; 14465f1c4225SArnaldo Carvalho de Melo 1447119fa3c9SNamhyung Kim if (pipe_output) 144835b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 144935b9d88eSArnaldo Carvalho de Melo 14500817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 14510817df08SDavid Ahern 145235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 145335b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 145435b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 145535b9d88eSArnaldo Carvalho de Melo 145635b9d88eSArnaldo Carvalho de Melo /* 145735b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 145835b9d88eSArnaldo Carvalho de Melo */ 145935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 146035b9d88eSArnaldo Carvalho de Melo 146135b9d88eSArnaldo Carvalho de Melo /* 146235b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 146335b9d88eSArnaldo Carvalho de Melo */ 14645f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 14655f1c4225SArnaldo Carvalho de Melo /* 14665f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 14675f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 14685f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 14695f1c4225SArnaldo Carvalho de Melo * 147020f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 14715f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 14725f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 14735f1c4225SArnaldo Carvalho de Melo * here. 14745f1c4225SArnaldo Carvalho de Melo */ 14755f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 14765f1c4225SArnaldo Carvalho de Melo if (ret == -1) 147735b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 14785f1c4225SArnaldo Carvalho de Melo exit(ret); 14795f1c4225SArnaldo Carvalho de Melo } 148035b9d88eSArnaldo Carvalho de Melo 148135b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 148235b9d88eSArnaldo Carvalho de Melo 1483735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1484f33cbe72SArnaldo Carvalho de Melo union sigval val; 1485f33cbe72SArnaldo Carvalho de Melo 1486f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1487f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 148835b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1489f33cbe72SArnaldo Carvalho de Melo } else 1490f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 149135b9d88eSArnaldo Carvalho de Melo exit(-1); 149235b9d88eSArnaldo Carvalho de Melo } 149335b9d88eSArnaldo Carvalho de Melo 1494735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1495735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1496735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1497735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1498735f7e0bSArnaldo Carvalho de Melo }; 1499735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1500735f7e0bSArnaldo Carvalho de Melo } 1501735f7e0bSArnaldo Carvalho de Melo 15021aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 15031aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 15041aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 15051aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 15061aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 15071aaf63b1SArnaldo Carvalho de Melo } 15084b49cce2SJiri Olsa perf_thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 15091aaf63b1SArnaldo Carvalho de Melo } 151035b9d88eSArnaldo Carvalho de Melo 151135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 151235b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 151335b9d88eSArnaldo Carvalho de Melo /* 151435b9d88eSArnaldo Carvalho de Melo * wait for child to settle 151535b9d88eSArnaldo Carvalho de Melo */ 151635b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 151735b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 151835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 151935b9d88eSArnaldo Carvalho de Melo } 152035b9d88eSArnaldo Carvalho de Melo 1521bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 152235b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 152335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 152435b9d88eSArnaldo Carvalho de Melo return 0; 152535b9d88eSArnaldo Carvalho de Melo 152635b9d88eSArnaldo Carvalho de Melo out_close_pipes: 152735b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 152835b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 152935b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 153035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 153135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 153235b9d88eSArnaldo Carvalho de Melo return -1; 153335b9d88eSArnaldo Carvalho de Melo } 153435b9d88eSArnaldo Carvalho de Melo 153563503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist) 153635b9d88eSArnaldo Carvalho de Melo { 153735b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1538b3824404SDavid Ahern char bf = 0; 1539bcf3145fSNamhyung Kim int ret; 154035b9d88eSArnaldo Carvalho de Melo /* 154135b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 154235b9d88eSArnaldo Carvalho de Melo */ 1543bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1544bcf3145fSNamhyung Kim if (ret < 0) 1545e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1546bcf3145fSNamhyung Kim 1547bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1548bcf3145fSNamhyung Kim return ret; 154935b9d88eSArnaldo Carvalho de Melo } 155035b9d88eSArnaldo Carvalho de Melo 155135b9d88eSArnaldo Carvalho de Melo return 0; 155235b9d88eSArnaldo Carvalho de Melo } 1553cb0b29e0SArnaldo Carvalho de Melo 155463503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 15550807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1556cb0b29e0SArnaldo Carvalho de Melo { 155732dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 155875562573SAdrian Hunter 155975562573SAdrian Hunter if (!evsel) 156075562573SAdrian Hunter return -EFAULT; 15610807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1562cb0b29e0SArnaldo Carvalho de Melo } 156378f067b3SArnaldo Carvalho de Melo 156463503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 156501468120SJiri Olsa union perf_event *event, 156601468120SJiri Olsa u64 *timestamp) 156701468120SJiri Olsa { 156832dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 156901468120SJiri Olsa 157001468120SJiri Olsa if (!evsel) 157101468120SJiri Olsa return -EFAULT; 157201468120SJiri Olsa return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); 157301468120SJiri Olsa } 157401468120SJiri Olsa 157563503dbaSJiri Olsa size_t perf_evlist__fprintf(struct evlist *evlist, FILE *fp) 157678f067b3SArnaldo Carvalho de Melo { 157732dcd021SJiri Olsa struct evsel *evsel; 157878f067b3SArnaldo Carvalho de Melo size_t printed = 0; 157978f067b3SArnaldo Carvalho de Melo 1580e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 158178f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 158278f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 158378f067b3SArnaldo Carvalho de Melo } 158478f067b3SArnaldo Carvalho de Melo 1585b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 158678f067b3SArnaldo Carvalho de Melo } 15876ef068cbSArnaldo Carvalho de Melo 158863503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist, 1589a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1590a8f23d8fSArnaldo Carvalho de Melo { 1591a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1592c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1593a8f23d8fSArnaldo Carvalho de Melo 1594a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1595a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1596a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1597a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1598a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1599a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1600a8f23d8fSArnaldo Carvalho de Melo 16011a47245dSAdrian Hunter value = perf_event_paranoid(); 1602a8f23d8fSArnaldo Carvalho de Melo 1603a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1604a8f23d8fSArnaldo Carvalho de Melo 1605a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1606a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1607a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1608a8f23d8fSArnaldo Carvalho de Melo } 1609a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16105229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1611a8f23d8fSArnaldo Carvalho de Melo 1612a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16135229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 16145229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1615a8f23d8fSArnaldo Carvalho de Melo break; 1616d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 161732dcd021SJiri Olsa struct evsel *first = perf_evlist__first(evlist); 1618d9aade7fSArnaldo Carvalho de Melo int max_freq; 1619d9aade7fSArnaldo Carvalho de Melo 1620d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1621d9aade7fSArnaldo Carvalho de Melo goto out_default; 1622d9aade7fSArnaldo Carvalho de Melo 16231fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1624d9aade7fSArnaldo Carvalho de Melo goto out_default; 1625d9aade7fSArnaldo Carvalho de Melo 1626d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1627d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1628d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1629d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 16301fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1631d9aade7fSArnaldo Carvalho de Melo break; 1632d9aade7fSArnaldo Carvalho de Melo } 1633a8f23d8fSArnaldo Carvalho de Melo default: 1634d9aade7fSArnaldo Carvalho de Melo out_default: 1635a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1636a8f23d8fSArnaldo Carvalho de Melo break; 1637a8f23d8fSArnaldo Carvalho de Melo } 1638a8f23d8fSArnaldo Carvalho de Melo 1639a8f23d8fSArnaldo Carvalho de Melo return 0; 1640a8f23d8fSArnaldo Carvalho de Melo } 1641a025e4f0SAdrian Hunter 164263503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1643956fa571SArnaldo Carvalho de Melo { 1644c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1645e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1646956fa571SArnaldo Carvalho de Melo 1647956fa571SArnaldo Carvalho de Melo switch (err) { 1648956fa571SArnaldo Carvalho de Melo case EPERM: 1649e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1650e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1651e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1652956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1653e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1654e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1655e965bea1SArnaldo Carvalho de Melo 1656e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1657e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1658e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1659e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1660e965bea1SArnaldo Carvalho de Melo } 1661e965bea1SArnaldo Carvalho de Melo 1662e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1663e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1664956fa571SArnaldo Carvalho de Melo break; 1665956fa571SArnaldo Carvalho de Melo default: 1666956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1667956fa571SArnaldo Carvalho de Melo break; 1668956fa571SArnaldo Carvalho de Melo } 1669956fa571SArnaldo Carvalho de Melo 1670956fa571SArnaldo Carvalho de Melo return 0; 1671956fa571SArnaldo Carvalho de Melo } 1672956fa571SArnaldo Carvalho de Melo 167363503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 167432dcd021SJiri Olsa struct evsel *move_evsel) 1675a025e4f0SAdrian Hunter { 167632dcd021SJiri Olsa struct evsel *evsel, *n; 1677a025e4f0SAdrian Hunter LIST_HEAD(move); 1678a025e4f0SAdrian Hunter 1679a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1680a025e4f0SAdrian Hunter return; 1681a025e4f0SAdrian Hunter 1682e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1683a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1684b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1685a025e4f0SAdrian Hunter } 1686a025e4f0SAdrian Hunter 1687ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1688a025e4f0SAdrian Hunter } 168960b0896cSAdrian Hunter 169063503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist, 169132dcd021SJiri Olsa struct evsel *tracking_evsel) 169260b0896cSAdrian Hunter { 169332dcd021SJiri Olsa struct evsel *evsel; 169460b0896cSAdrian Hunter 169560b0896cSAdrian Hunter if (tracking_evsel->tracking) 169660b0896cSAdrian Hunter return; 169760b0896cSAdrian Hunter 1698e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 169960b0896cSAdrian Hunter if (evsel != tracking_evsel) 170060b0896cSAdrian Hunter evsel->tracking = false; 170160b0896cSAdrian Hunter } 170260b0896cSAdrian Hunter 170360b0896cSAdrian Hunter tracking_evsel->tracking = true; 170460b0896cSAdrian Hunter } 17057630b3e2SWang Nan 170632dcd021SJiri Olsa struct evsel * 170763503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist, 17087630b3e2SWang Nan const char *str) 17097630b3e2SWang Nan { 171032dcd021SJiri Olsa struct evsel *evsel; 17117630b3e2SWang Nan 1712e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 17137630b3e2SWang Nan if (!evsel->name) 17147630b3e2SWang Nan continue; 17157630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 17167630b3e2SWang Nan return evsel; 17177630b3e2SWang Nan } 17187630b3e2SWang Nan 17197630b3e2SWang Nan return NULL; 17207630b3e2SWang Nan } 172154cc54deSWang Nan 172263503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 172354cc54deSWang Nan enum bkw_mmap_state state) 172454cc54deSWang Nan { 172554cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 172654cc54deSWang Nan enum action { 172754cc54deSWang Nan NONE, 172854cc54deSWang Nan PAUSE, 172954cc54deSWang Nan RESUME, 173054cc54deSWang Nan } action = NONE; 173154cc54deSWang Nan 17320b72d69aSWang Nan if (!evlist->overwrite_mmap) 173354cc54deSWang Nan return; 173454cc54deSWang Nan 173554cc54deSWang Nan switch (old_state) { 173654cc54deSWang Nan case BKW_MMAP_NOTREADY: { 173754cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1738dd8bd53aSLuis de Bethencourt goto state_err; 173954cc54deSWang Nan break; 174054cc54deSWang Nan } 174154cc54deSWang Nan case BKW_MMAP_RUNNING: { 174254cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 174354cc54deSWang Nan goto state_err; 174454cc54deSWang Nan action = PAUSE; 174554cc54deSWang Nan break; 174654cc54deSWang Nan } 174754cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 174854cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 174954cc54deSWang Nan goto state_err; 175054cc54deSWang Nan break; 175154cc54deSWang Nan } 175254cc54deSWang Nan case BKW_MMAP_EMPTY: { 175354cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 175454cc54deSWang Nan goto state_err; 175554cc54deSWang Nan action = RESUME; 175654cc54deSWang Nan break; 175754cc54deSWang Nan } 175854cc54deSWang Nan default: 175954cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 176054cc54deSWang Nan } 176154cc54deSWang Nan 176254cc54deSWang Nan evlist->bkw_mmap_state = state; 176354cc54deSWang Nan 176454cc54deSWang Nan switch (action) { 176554cc54deSWang Nan case PAUSE: 176654cc54deSWang Nan perf_evlist__pause(evlist); 176754cc54deSWang Nan break; 176854cc54deSWang Nan case RESUME: 176954cc54deSWang Nan perf_evlist__resume(evlist); 177054cc54deSWang Nan break; 177154cc54deSWang Nan case NONE: 177254cc54deSWang Nan default: 177354cc54deSWang Nan break; 177454cc54deSWang Nan } 177554cc54deSWang Nan 177654cc54deSWang Nan state_err: 177754cc54deSWang Nan return; 177854cc54deSWang Nan } 177907d6f446SArnaldo Carvalho de Melo 178063503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 178107d6f446SArnaldo Carvalho de Melo { 178232dcd021SJiri Olsa struct evsel *evsel; 178307d6f446SArnaldo Carvalho de Melo 178407d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 17851fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 178607d6f446SArnaldo Carvalho de Melo return false; 178707d6f446SArnaldo Carvalho de Melo } 178807d6f446SArnaldo Carvalho de Melo 178907d6f446SArnaldo Carvalho de Melo return true; 179007d6f446SArnaldo Carvalho de Melo } 1791e2bdbe80SJin Yao 1792e2bdbe80SJin Yao /* 1793e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1794e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1795e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1796e2bdbe80SJin Yao */ 179763503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1798e2bdbe80SJin Yao { 1799e2bdbe80SJin Yao if (!evlist->nr_groups) { 180032dcd021SJiri Olsa struct evsel *leader = perf_evlist__first(evlist); 1801e2bdbe80SJin Yao 1802e2bdbe80SJin Yao perf_evlist__set_leader(evlist); 1803e2bdbe80SJin Yao leader->forced_leader = true; 1804e2bdbe80SJin Yao } 1805e2bdbe80SJin Yao } 1806c3537fc2SAndi Kleen 180763503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 180832dcd021SJiri Olsa struct evsel *evsel) 1809c3537fc2SAndi Kleen { 181032dcd021SJiri Olsa struct evsel *c2, *leader; 1811c3537fc2SAndi Kleen bool is_open = true; 1812c3537fc2SAndi Kleen 1813c3537fc2SAndi Kleen leader = evsel->leader; 1814c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 1815c3537fc2SAndi Kleen leader->name, leader->nr_members); 1816c3537fc2SAndi Kleen 1817c3537fc2SAndi Kleen /* 1818c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1819c3537fc2SAndi Kleen * include the first entry. 1820c3537fc2SAndi Kleen */ 1821c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1822c3537fc2SAndi Kleen if (c2 == evsel) 1823c3537fc2SAndi Kleen is_open = false; 1824c3537fc2SAndi Kleen if (c2->leader == leader) { 1825c3537fc2SAndi Kleen if (is_open) 1826c3537fc2SAndi Kleen perf_evsel__close(c2); 1827c3537fc2SAndi Kleen c2->leader = c2; 1828c3537fc2SAndi Kleen c2->nr_members = 0; 1829c3537fc2SAndi Kleen } 1830c3537fc2SAndi Kleen } 1831c3537fc2SAndi Kleen return leader; 1832c3537fc2SAndi Kleen } 1833657ee553SSong Liu 183463503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist, 1835657ee553SSong Liu struct perf_event_attr *attr, 1836657ee553SSong Liu perf_evsel__sb_cb_t cb, 1837657ee553SSong Liu void *data) 1838657ee553SSong Liu { 183932dcd021SJiri Olsa struct evsel *evsel; 1840657ee553SSong Liu bool new_evlist = (*evlist) == NULL; 1841657ee553SSong Liu 1842657ee553SSong Liu if (*evlist == NULL) 18430f98b11cSJiri Olsa *evlist = evlist__new(); 1844657ee553SSong Liu if (*evlist == NULL) 1845657ee553SSong Liu return -1; 1846657ee553SSong Liu 1847657ee553SSong Liu if (!attr->sample_id_all) { 1848657ee553SSong Liu pr_warning("enabling sample_id_all for all side band events\n"); 1849657ee553SSong Liu attr->sample_id_all = 1; 1850657ee553SSong Liu } 1851657ee553SSong Liu 18526484d2f9SJiri Olsa evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries); 1853657ee553SSong Liu if (!evsel) 1854657ee553SSong Liu goto out_err; 1855657ee553SSong Liu 1856657ee553SSong Liu evsel->side_band.cb = cb; 1857657ee553SSong Liu evsel->side_band.data = data; 1858a1cf3a75SJiri Olsa evlist__add(*evlist, evsel); 1859657ee553SSong Liu return 0; 1860657ee553SSong Liu 1861657ee553SSong Liu out_err: 1862657ee553SSong Liu if (new_evlist) { 1863c12995a5SJiri Olsa evlist__delete(*evlist); 1864657ee553SSong Liu *evlist = NULL; 1865657ee553SSong Liu } 1866657ee553SSong Liu return -1; 1867657ee553SSong Liu } 1868657ee553SSong Liu 1869657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg) 1870657ee553SSong Liu { 187163503dbaSJiri Olsa struct evlist *evlist = arg; 1872657ee553SSong Liu bool draining = false; 1873adc6257cSJiri Olsa int i, done = 0; 1874657ee553SSong Liu 1875adc6257cSJiri Olsa while (!done) { 1876adc6257cSJiri Olsa bool got_data = false; 1877adc6257cSJiri Olsa 1878adc6257cSJiri Olsa if (evlist->thread.done) 1879657ee553SSong Liu draining = true; 1880657ee553SSong Liu 1881657ee553SSong Liu if (!draining) 1882657ee553SSong Liu perf_evlist__poll(evlist, 1000); 1883657ee553SSong Liu 1884657ee553SSong Liu for (i = 0; i < evlist->nr_mmaps; i++) { 1885657ee553SSong Liu struct perf_mmap *map = &evlist->mmap[i]; 1886657ee553SSong Liu union perf_event *event; 1887657ee553SSong Liu 1888657ee553SSong Liu if (perf_mmap__read_init(map)) 1889657ee553SSong Liu continue; 1890657ee553SSong Liu while ((event = perf_mmap__read_event(map)) != NULL) { 189132dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 1892657ee553SSong Liu 1893657ee553SSong Liu if (evsel && evsel->side_band.cb) 1894657ee553SSong Liu evsel->side_band.cb(event, evsel->side_band.data); 1895657ee553SSong Liu else 1896657ee553SSong Liu pr_warning("cannot locate proper evsel for the side band event\n"); 1897657ee553SSong Liu 1898657ee553SSong Liu perf_mmap__consume(map); 1899adc6257cSJiri Olsa got_data = true; 1900657ee553SSong Liu } 1901657ee553SSong Liu perf_mmap__read_done(map); 1902657ee553SSong Liu } 1903adc6257cSJiri Olsa 1904adc6257cSJiri Olsa if (draining && !got_data) 1905adc6257cSJiri Olsa break; 1906657ee553SSong Liu } 1907657ee553SSong Liu return NULL; 1908657ee553SSong Liu } 1909657ee553SSong Liu 191063503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist, 1911657ee553SSong Liu struct target *target) 1912657ee553SSong Liu { 191332dcd021SJiri Olsa struct evsel *counter; 1914657ee553SSong Liu 1915657ee553SSong Liu if (!evlist) 1916657ee553SSong Liu return 0; 1917657ee553SSong Liu 1918657ee553SSong Liu if (perf_evlist__create_maps(evlist, target)) 1919657ee553SSong Liu goto out_delete_evlist; 1920657ee553SSong Liu 1921657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 19225972d1e0SJiri Olsa if (evsel__open(counter, evlist->cpus, 1923657ee553SSong Liu evlist->threads) < 0) 1924657ee553SSong Liu goto out_delete_evlist; 1925657ee553SSong Liu } 1926657ee553SSong Liu 1927657ee553SSong Liu if (perf_evlist__mmap(evlist, UINT_MAX)) 1928657ee553SSong Liu goto out_delete_evlist; 1929657ee553SSong Liu 1930657ee553SSong Liu evlist__for_each_entry(evlist, counter) { 1931ec7f24efSJiri Olsa if (evsel__enable(counter)) 1932657ee553SSong Liu goto out_delete_evlist; 1933657ee553SSong Liu } 1934657ee553SSong Liu 1935657ee553SSong Liu evlist->thread.done = 0; 1936657ee553SSong Liu if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist)) 1937657ee553SSong Liu goto out_delete_evlist; 1938657ee553SSong Liu 1939657ee553SSong Liu return 0; 1940657ee553SSong Liu 1941657ee553SSong Liu out_delete_evlist: 1942c12995a5SJiri Olsa evlist__delete(evlist); 1943657ee553SSong Liu evlist = NULL; 1944657ee553SSong Liu return -1; 1945657ee553SSong Liu } 1946657ee553SSong Liu 194763503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist) 1948657ee553SSong Liu { 1949657ee553SSong Liu if (!evlist) 1950657ee553SSong Liu return; 1951657ee553SSong Liu evlist->thread.done = 1; 1952657ee553SSong Liu pthread_join(evlist->thread.th, NULL); 1953c12995a5SJiri Olsa evlist__delete(evlist); 1954657ee553SSong Liu } 1955