191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f8a95309SArnaldo Carvalho de Melo /* 3f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 4f8a95309SArnaldo Carvalho de Melo * 5f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 6f8a95309SArnaldo Carvalho de Melo * copyright notes. 7f8a95309SArnaldo Carvalho de Melo */ 8956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h> 9a43783aeSArnaldo Carvalho de Melo #include <errno.h> 10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h" 14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1512864b31SNamhyung Kim #include "target.h" 16361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 17361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 18e3e1a54fSAdrian Hunter #include "debug.h" 1958db1d6eSArnaldo Carvalho de Melo #include "units.h" 2020f2be1dSJiri Olsa #include <internal/lib.h> // page_size 217736627bSAndi Kleen #include "affinity.h" 22c1a604dfSArnaldo Carvalho de Melo #include "../perf.h" 2354cc54deSWang Nan #include "asm/bug.h" 24657ee553SSong Liu #include "bpf-event.h" 25da949f50SArnaldo Carvalho de Melo #include "util/string2.h" 2640c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h" 279607ad3aSArnaldo Carvalho de Melo #include <signal.h> 2835b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 29b397f846SArnaldo Carvalho de Melo #include <sched.h> 30f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h> 31361c99a6SArnaldo Carvalho de Melo 3250d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 334b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 3450d08e47SArnaldo Carvalho de Melo 35bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 3686a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 37f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 38f8a95309SArnaldo Carvalho de Melo 3970db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 4070db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 410389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 428dd2a131SJiri Olsa #include <linux/err.h> 438520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 447f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 454562a739SJiri Olsa #include <perf/evlist.h> 4688761fa1SJiri Olsa #include <perf/evsel.h> 479c3516d1SJiri Olsa #include <perf/cpumap.h> 487728fa0cSJiri Olsa #include <perf/mmap.h> 4970db7533SArnaldo Carvalho de Melo 50e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h> 51e14e5497SArnaldo Carvalho de Melo 52748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 53748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 54748fe088SArnaldo Carvalho de Melo #endif 55748fe088SArnaldo Carvalho de Melo 569dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y)) 578cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y) 58f8a95309SArnaldo Carvalho de Melo 5952c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus, 609749b90eSJiri Olsa struct perf_thread_map *threads) 61361c99a6SArnaldo Carvalho de Melo { 624562a739SJiri Olsa perf_evlist__init(&evlist->core); 63453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 6435b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 6554cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 668ab705b5SAlexey Budankov evlist->ctl_fd.fd = -1; 678ab705b5SAlexey Budankov evlist->ctl_fd.ack = -1; 688ab705b5SAlexey Budankov evlist->ctl_fd.pos = -1; 69361c99a6SArnaldo Carvalho de Melo } 70361c99a6SArnaldo Carvalho de Melo 710f98b11cSJiri Olsa struct evlist *evlist__new(void) 72ef1d1af2SArnaldo Carvalho de Melo { 7363503dbaSJiri Olsa struct evlist *evlist = zalloc(sizeof(*evlist)); 74ef1d1af2SArnaldo Carvalho de Melo 75ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 7652c86bcaSJiri Olsa evlist__init(evlist, NULL, NULL); 77ef1d1af2SArnaldo Carvalho de Melo 78361c99a6SArnaldo Carvalho de Melo return evlist; 79361c99a6SArnaldo Carvalho de Melo } 80361c99a6SArnaldo Carvalho de Melo 8163503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void) 82b22d54b0SJiri Olsa { 830f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 84b22d54b0SJiri Olsa 85e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_default(evlist)) { 86c12995a5SJiri Olsa evlist__delete(evlist); 87b22d54b0SJiri Olsa evlist = NULL; 88b22d54b0SJiri Olsa } 89b22d54b0SJiri Olsa 90b22d54b0SJiri Olsa return evlist; 91b22d54b0SJiri Olsa } 92b22d54b0SJiri Olsa 9363503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void) 945bae0250SArnaldo Carvalho de Melo { 950f98b11cSJiri Olsa struct evlist *evlist = evlist__new(); 965bae0250SArnaldo Carvalho de Melo 97e251abeeSArnaldo Carvalho de Melo if (evlist && evlist__add_dummy(evlist)) { 98c12995a5SJiri Olsa evlist__delete(evlist); 995bae0250SArnaldo Carvalho de Melo evlist = NULL; 1005bae0250SArnaldo Carvalho de Melo } 1015bae0250SArnaldo Carvalho de Melo 1025bae0250SArnaldo Carvalho de Melo return evlist; 1035bae0250SArnaldo Carvalho de Melo } 1045bae0250SArnaldo Carvalho de Melo 10575562573SAdrian Hunter /** 10675562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 10775562573SAdrian Hunter * @evlist: selected event list 10875562573SAdrian Hunter * 10975562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 11075562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 11175562573SAdrian Hunter */ 11263503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist) 11375562573SAdrian Hunter { 114515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 11575562573SAdrian Hunter 11675562573SAdrian Hunter evlist->id_pos = first->id_pos; 11775562573SAdrian Hunter evlist->is_pos = first->is_pos; 11875562573SAdrian Hunter } 11975562573SAdrian Hunter 12063503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist) 121733cd2feSAdrian Hunter { 12232dcd021SJiri Olsa struct evsel *evsel; 123733cd2feSAdrian Hunter 124e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 1254b5e87b7SArnaldo Carvalho de Melo evsel__calc_id_pos(evsel); 126733cd2feSAdrian Hunter 127733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 128733cd2feSAdrian Hunter } 129733cd2feSAdrian Hunter 130e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist) 131361c99a6SArnaldo Carvalho de Melo { 13232dcd021SJiri Olsa struct evsel *pos, *n; 133361c99a6SArnaldo Carvalho de Melo 134e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 135b27c4eceSJiri Olsa list_del_init(&pos->core.node); 136d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 1375eb2dd2aSJiri Olsa evsel__delete(pos); 138361c99a6SArnaldo Carvalho de Melo } 139361c99a6SArnaldo Carvalho de Melo 1406484d2f9SJiri Olsa evlist->core.nr_entries = 0; 141361c99a6SArnaldo Carvalho de Melo } 142361c99a6SArnaldo Carvalho de Melo 143470579b0SJiri Olsa void evlist__exit(struct evlist *evlist) 144ef1d1af2SArnaldo Carvalho de Melo { 14504662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1460b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 14793dd6e28SJiri Olsa perf_evlist__exit(&evlist->core); 148ef1d1af2SArnaldo Carvalho de Melo } 149ef1d1af2SArnaldo Carvalho de Melo 150c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist) 151361c99a6SArnaldo Carvalho de Melo { 1520b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1530b04b3dcSArnaldo Carvalho de Melo return; 1540b04b3dcSArnaldo Carvalho de Melo 155db6b7b13SJiri Olsa evlist__munmap(evlist); 156750b4edeSJiri Olsa evlist__close(evlist); 157e6b1878dSJiri Olsa evlist__purge(evlist); 158470579b0SJiri Olsa evlist__exit(evlist); 159361c99a6SArnaldo Carvalho de Melo free(evlist); 160361c99a6SArnaldo Carvalho de Melo } 161361c99a6SArnaldo Carvalho de Melo 162a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry) 163361c99a6SArnaldo Carvalho de Melo { 164d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 1656484d2f9SJiri Olsa entry->idx = evlist->core.nr_entries; 16660b0896cSAdrian Hunter entry->tracking = !entry->idx; 167ef503831SArnaldo Carvalho de Melo 1686484d2f9SJiri Olsa perf_evlist__add(&evlist->core, &entry->core); 1696484d2f9SJiri Olsa 1706484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 17175562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 172361c99a6SArnaldo Carvalho de Melo } 173361c99a6SArnaldo Carvalho de Melo 17416251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel) 1754768230aSAdrian Hunter { 1764768230aSAdrian Hunter evsel->evlist = NULL; 17752e22fb8SJiri Olsa perf_evlist__remove(&evlist->core, &evsel->core); 1784768230aSAdrian Hunter } 1794768230aSAdrian Hunter 18063503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist, 181f114d6efSAdrian Hunter struct list_head *list) 18250d08e47SArnaldo Carvalho de Melo { 18332dcd021SJiri Olsa struct evsel *evsel, *temp; 18475562573SAdrian Hunter 185e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 186b27c4eceSJiri Olsa list_del_init(&evsel->core.node); 187a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 188f114d6efSAdrian Hunter } 18950d08e47SArnaldo Carvalho de Melo } 19050d08e47SArnaldo Carvalho de Melo 191c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist, 192c0e53476SArnaldo Carvalho de Melo const struct evsel_str_handler *assocs, size_t nr_assocs) 193c0e53476SArnaldo Carvalho de Melo { 194c0e53476SArnaldo Carvalho de Melo struct evsel *evsel; 195c0e53476SArnaldo Carvalho de Melo size_t i; 196c0e53476SArnaldo Carvalho de Melo int err; 197c0e53476SArnaldo Carvalho de Melo 198c0e53476SArnaldo Carvalho de Melo for (i = 0; i < nr_assocs; i++) { 199c0e53476SArnaldo Carvalho de Melo // Adding a handler for an event not in this evlist, just ignore it. 200c0e53476SArnaldo Carvalho de Melo evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name); 201c0e53476SArnaldo Carvalho de Melo if (evsel == NULL) 202c0e53476SArnaldo Carvalho de Melo continue; 203c0e53476SArnaldo Carvalho de Melo 204c0e53476SArnaldo Carvalho de Melo err = -EEXIST; 205c0e53476SArnaldo Carvalho de Melo if (evsel->handler != NULL) 206c0e53476SArnaldo Carvalho de Melo goto out; 207c0e53476SArnaldo Carvalho de Melo evsel->handler = assocs[i].handler; 208c0e53476SArnaldo Carvalho de Melo } 209c0e53476SArnaldo Carvalho de Melo 210c0e53476SArnaldo Carvalho de Melo err = 0; 211c0e53476SArnaldo Carvalho de Melo out: 212c0e53476SArnaldo Carvalho de Melo return err; 213c0e53476SArnaldo Carvalho de Melo } 214c0e53476SArnaldo Carvalho de Melo 215*a622eafaSArnaldo Carvalho de Melo void __evlist__set_leader(struct list_head *list) 21663dab225SArnaldo Carvalho de Melo { 21732dcd021SJiri Olsa struct evsel *evsel, *leader; 21863dab225SArnaldo Carvalho de Melo 219b27c4eceSJiri Olsa leader = list_entry(list->next, struct evsel, core.node); 220b27c4eceSJiri Olsa evsel = list_entry(list->prev, struct evsel, core.node); 22197f63e4aSNamhyung Kim 2225643b1a5SJiri Olsa leader->core.nr_members = evsel->idx - leader->idx + 1; 22363dab225SArnaldo Carvalho de Melo 224e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22563dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22663dab225SArnaldo Carvalho de Melo } 22763dab225SArnaldo Carvalho de Melo } 22863dab225SArnaldo Carvalho de Melo 229*a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist) 2306a4bb04cSJiri Olsa { 2316484d2f9SJiri Olsa if (evlist->core.nr_entries) { 2326484d2f9SJiri Olsa evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0; 233*a622eafaSArnaldo Carvalho de Melo __evlist__set_leader(&evlist->core.entries); 2346a4bb04cSJiri Olsa } 23597f63e4aSNamhyung Kim } 2366a4bb04cSJiri Olsa 237e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise) 238361c99a6SArnaldo Carvalho de Melo { 2398f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_cycles(precise); 240361c99a6SArnaldo Carvalho de Melo 241361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2427c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 243361c99a6SArnaldo Carvalho de Melo 244a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 245361c99a6SArnaldo Carvalho de Melo return 0; 246361c99a6SArnaldo Carvalho de Melo } 2475c581041SArnaldo Carvalho de Melo 248e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist) 2495bae0250SArnaldo Carvalho de Melo { 2505bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2515bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2525bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2535bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2545bae0250SArnaldo Carvalho de Melo }; 2558f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries); 2565bae0250SArnaldo Carvalho de Melo 2575bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2585bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2595bae0250SArnaldo Carvalho de Melo 260a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 2615bae0250SArnaldo Carvalho de Melo return 0; 2625bae0250SArnaldo Carvalho de Melo } 2635bae0250SArnaldo Carvalho de Melo 264e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 26550d08e47SArnaldo Carvalho de Melo { 26632dcd021SJiri Olsa struct evsel *evsel, *n; 26750d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 26850d08e47SArnaldo Carvalho de Melo size_t i; 26950d08e47SArnaldo Carvalho de Melo 27050d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 2718f6725a2SArnaldo Carvalho de Melo evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i); 27250d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27350d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 274b27c4eceSJiri Olsa list_add_tail(&evsel->core.node, &head); 27550d08e47SArnaldo Carvalho de Melo } 27650d08e47SArnaldo Carvalho de Melo 277f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 27850d08e47SArnaldo Carvalho de Melo 27950d08e47SArnaldo Carvalho de Melo return 0; 28050d08e47SArnaldo Carvalho de Melo 28150d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 282e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 2835eb2dd2aSJiri Olsa evsel__delete(evsel); 28450d08e47SArnaldo Carvalho de Melo return -1; 28550d08e47SArnaldo Carvalho de Melo } 28650d08e47SArnaldo Carvalho de Melo 287e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 28879695e1bSArnaldo Carvalho de Melo { 28979695e1bSArnaldo Carvalho de Melo size_t i; 29079695e1bSArnaldo Carvalho de Melo 29179695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 29279695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 29379695e1bSArnaldo Carvalho de Melo 294a1cf3a75SJiri Olsa return evlist__add_attrs(evlist, attrs, nr_attrs); 29579695e1bSArnaldo Carvalho de Melo } 29679695e1bSArnaldo Carvalho de Melo 29732dcd021SJiri Olsa struct evsel * 29863503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id) 299ee29be62SArnaldo Carvalho de Melo { 30032dcd021SJiri Olsa struct evsel *evsel; 301ee29be62SArnaldo Carvalho de Melo 302e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3031fc632ceSJiri Olsa if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && 3041fc632ceSJiri Olsa (int)evsel->core.attr.config == id) 305ee29be62SArnaldo Carvalho de Melo return evsel; 306ee29be62SArnaldo Carvalho de Melo } 307ee29be62SArnaldo Carvalho de Melo 308ee29be62SArnaldo Carvalho de Melo return NULL; 309ee29be62SArnaldo Carvalho de Melo } 310ee29be62SArnaldo Carvalho de Melo 31132dcd021SJiri Olsa struct evsel * 31263503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist, 313a2f2804aSDavid Ahern const char *name) 314a2f2804aSDavid Ahern { 31532dcd021SJiri Olsa struct evsel *evsel; 316a2f2804aSDavid Ahern 317e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 3181fc632ceSJiri Olsa if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) && 319a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 320a2f2804aSDavid Ahern return evsel; 321a2f2804aSDavid Ahern } 322a2f2804aSDavid Ahern 323a2f2804aSDavid Ahern return NULL; 324a2f2804aSDavid Ahern } 325a2f2804aSDavid Ahern 326e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler) 32739876e7dSArnaldo Carvalho de Melo { 3288f6725a2SArnaldo Carvalho de Melo struct evsel *evsel = 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 { 341648b5af3SJiri Olsa if (evsel->core.system_wide) 342bf8e8f4bSAdrian Hunter return 1; 343bf8e8f4bSAdrian Hunter else 344a2f354e3SJiri Olsa return perf_thread_map__nr(evlist->core.threads); 345bf8e8f4bSAdrian Hunter } 346bf8e8f4bSAdrian Hunter 347a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist) 348a8cbe40fSAndi Kleen { 349a8cbe40fSAndi Kleen struct evsel *pos; 350a8cbe40fSAndi Kleen 351a8cbe40fSAndi Kleen /* 352a8cbe40fSAndi Kleen * Reset the per evsel cpu_iter. This is needed because 353a8cbe40fSAndi Kleen * each evsel's cpumap may have a different index space, 354a8cbe40fSAndi Kleen * and some operations need the index to modify 355a8cbe40fSAndi Kleen * the FD xyarray (e.g. open, close) 356a8cbe40fSAndi Kleen */ 357a8cbe40fSAndi Kleen evlist__for_each_entry(evlist, pos) 358a8cbe40fSAndi Kleen pos->cpu_iter = 0; 359a8cbe40fSAndi Kleen } 360a8cbe40fSAndi Kleen 361a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu) 362a8cbe40fSAndi Kleen { 363a8cbe40fSAndi Kleen if (ev->cpu_iter >= ev->core.cpus->nr) 364a8cbe40fSAndi Kleen return true; 365a8cbe40fSAndi Kleen if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu) 366a8cbe40fSAndi Kleen return true; 367a8cbe40fSAndi Kleen return false; 368a8cbe40fSAndi Kleen } 369a8cbe40fSAndi Kleen 370a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu) 371a8cbe40fSAndi Kleen { 372a8cbe40fSAndi Kleen if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) { 373a8cbe40fSAndi Kleen ev->cpu_iter++; 374a8cbe40fSAndi Kleen return false; 375a8cbe40fSAndi Kleen } 376a8cbe40fSAndi Kleen return true; 377a8cbe40fSAndi Kleen } 378a8cbe40fSAndi Kleen 379e74676deSJiri Olsa void evlist__disable(struct evlist *evlist) 3804152ab37SArnaldo Carvalho de Melo { 38132dcd021SJiri Olsa struct evsel *pos; 382704e2f5bSAndi Kleen struct affinity affinity; 38387cf8360SAdrian Hunter int cpu, i, imm = 0; 38487cf8360SAdrian Hunter bool has_imm = false; 385704e2f5bSAndi Kleen 386704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 387704e2f5bSAndi Kleen return; 388704e2f5bSAndi Kleen 38987cf8360SAdrian Hunter /* Disable 'immediate' events last */ 39087cf8360SAdrian Hunter for (imm = 0; imm <= 1; imm++) { 391704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 392704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 3934152ab37SArnaldo Carvalho de Melo 394e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 395704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 396704e2f5bSAndi Kleen continue; 397c754c382SArnaldo Carvalho de Melo if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd) 3983fe4430dSJiri Olsa continue; 39987cf8360SAdrian Hunter if (pos->immediate) 40087cf8360SAdrian Hunter has_imm = true; 40187cf8360SAdrian Hunter if (pos->immediate != imm) 40287cf8360SAdrian Hunter continue; 403704e2f5bSAndi Kleen evsel__disable_cpu(pos, pos->cpu_iter - 1); 404704e2f5bSAndi Kleen } 405704e2f5bSAndi Kleen } 40687cf8360SAdrian Hunter if (!has_imm) 40787cf8360SAdrian Hunter break; 40887cf8360SAdrian Hunter } 40987cf8360SAdrian Hunter 410704e2f5bSAndi Kleen affinity__cleanup(&affinity); 411704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 412c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 413704e2f5bSAndi Kleen continue; 414704e2f5bSAndi Kleen pos->disabled = true; 4154152ab37SArnaldo Carvalho de Melo } 4162b56bcfbSArnaldo Carvalho de Melo 4172b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 4184152ab37SArnaldo Carvalho de Melo } 4194152ab37SArnaldo Carvalho de Melo 4201c87f165SJiri Olsa void evlist__enable(struct evlist *evlist) 421764e16a3SDavid Ahern { 42232dcd021SJiri Olsa struct evsel *pos; 423704e2f5bSAndi Kleen struct affinity affinity; 424704e2f5bSAndi Kleen int cpu, i; 425764e16a3SDavid Ahern 426704e2f5bSAndi Kleen if (affinity__setup(&affinity) < 0) 427704e2f5bSAndi Kleen return; 428704e2f5bSAndi Kleen 429704e2f5bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 430704e2f5bSAndi Kleen affinity__set(&affinity, cpu); 431704e2f5bSAndi Kleen 432704e2f5bSAndi Kleen evlist__for_each_entry(evlist, pos) { 433704e2f5bSAndi Kleen if (evsel__cpu_iter_skip(pos, cpu)) 434704e2f5bSAndi Kleen continue; 435c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 436704e2f5bSAndi Kleen continue; 437704e2f5bSAndi Kleen evsel__enable_cpu(pos, pos->cpu_iter - 1); 438704e2f5bSAndi Kleen } 439704e2f5bSAndi Kleen } 440704e2f5bSAndi Kleen affinity__cleanup(&affinity); 441e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 442c754c382SArnaldo Carvalho de Melo if (!evsel__is_group_leader(pos) || !pos->core.fd) 4433fe4430dSJiri Olsa continue; 444704e2f5bSAndi Kleen pos->disabled = false; 445764e16a3SDavid Ahern } 4462b56bcfbSArnaldo Carvalho de Melo 4472b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 4482b56bcfbSArnaldo Carvalho de Melo } 4492b56bcfbSArnaldo Carvalho de Melo 45063503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist) 4512b56bcfbSArnaldo Carvalho de Melo { 452e74676deSJiri Olsa (evlist->enabled ? evlist__disable : evlist__enable)(evlist); 453764e16a3SDavid Ahern } 454764e16a3SDavid Ahern 45563503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist, 45632dcd021SJiri Olsa struct evsel *evsel, int cpu) 4571c65056cSAdrian Hunter { 45818ef15c6SArnaldo Carvalho de Melo int thread; 4591c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 4601c65056cSAdrian Hunter 4619dfcb759SJiri Olsa if (!evsel->core.fd) 4621c65056cSAdrian Hunter return -EINVAL; 4631c65056cSAdrian Hunter 4641c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 46518ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4661c65056cSAdrian Hunter if (err) 4671c65056cSAdrian Hunter return err; 4681c65056cSAdrian Hunter } 4691c65056cSAdrian Hunter return 0; 4701c65056cSAdrian Hunter } 4711c65056cSAdrian Hunter 47263503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist, 47332dcd021SJiri Olsa struct evsel *evsel, 4741c65056cSAdrian Hunter int thread) 4751c65056cSAdrian Hunter { 47618ef15c6SArnaldo Carvalho de Melo int cpu; 4776549cd8fSJiri Olsa int nr_cpus = perf_cpu_map__nr(evlist->core.cpus); 4781c65056cSAdrian Hunter 4799dfcb759SJiri Olsa if (!evsel->core.fd) 4801c65056cSAdrian Hunter return -EINVAL; 4811c65056cSAdrian Hunter 4821c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 48318ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4841c65056cSAdrian Hunter if (err) 4851c65056cSAdrian Hunter return err; 4861c65056cSAdrian Hunter } 4871c65056cSAdrian Hunter return 0; 4881c65056cSAdrian Hunter } 4891c65056cSAdrian Hunter 49063503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist, 49132dcd021SJiri Olsa struct evsel *evsel, int idx) 4921c65056cSAdrian Hunter { 493315c0a1fSJiri Olsa bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus); 4941c65056cSAdrian Hunter 4951c65056cSAdrian Hunter if (per_cpu_mmaps) 4961c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4971c65056cSAdrian Hunter else 4981c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4991c65056cSAdrian Hunter } 5001c65056cSAdrian Hunter 501f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd) 502e4b356b5SArnaldo Carvalho de Melo { 503ab4c1f9fSAlexey Budankov return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default); 504e4b356b5SArnaldo Carvalho de Melo } 5051b85337dSArnaldo Carvalho de Melo 506f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask) 5071ddec7f0SArnaldo Carvalho de Melo { 50884227cb1SJiri Olsa return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask); 5091ddec7f0SArnaldo Carvalho de Melo } 5101ddec7f0SArnaldo Carvalho de Melo 51180ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout) 512f66a889dSArnaldo Carvalho de Melo { 51380ab2987SJiri Olsa return perf_evlist__poll(&evlist->core, timeout); 514f66a889dSArnaldo Carvalho de Melo } 515f66a889dSArnaldo Carvalho de Melo 51663503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id) 51770db7533SArnaldo Carvalho de Melo { 51870db7533SArnaldo Carvalho de Melo struct hlist_head *head; 51970db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 52070db7533SArnaldo Carvalho de Melo int hash; 52170db7533SArnaldo Carvalho de Melo 52270db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 5231d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 52470db7533SArnaldo Carvalho de Melo 525b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 52670db7533SArnaldo Carvalho de Melo if (sid->id == id) 527932a3594SJiri Olsa return sid; 528932a3594SJiri Olsa 529932a3594SJiri Olsa return NULL; 530932a3594SJiri Olsa } 531932a3594SJiri Olsa 53263503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id) 533932a3594SJiri Olsa { 534932a3594SJiri Olsa struct perf_sample_id *sid; 535932a3594SJiri Olsa 5366484d2f9SJiri Olsa if (evlist->core.nr_entries == 1 || !id) 537515dbe48SJiri Olsa return evlist__first(evlist); 538932a3594SJiri Olsa 539932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 540932a3594SJiri Olsa if (sid) 54170c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 54230e68bccSNamhyung Kim 5438cedf3a5SArnaldo Carvalho de Melo if (!evlist__sample_id_all(evlist)) 544515dbe48SJiri Olsa return evlist__first(evlist); 54530e68bccSNamhyung Kim 54670db7533SArnaldo Carvalho de Melo return NULL; 54770db7533SArnaldo Carvalho de Melo } 54804391debSArnaldo Carvalho de Melo 54963503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist, 550dddcf6abSAdrian Hunter u64 id) 551dddcf6abSAdrian Hunter { 552dddcf6abSAdrian Hunter struct perf_sample_id *sid; 553dddcf6abSAdrian Hunter 554dddcf6abSAdrian Hunter if (!id) 555dddcf6abSAdrian Hunter return NULL; 556dddcf6abSAdrian Hunter 557dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 558dddcf6abSAdrian Hunter if (sid) 55970c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 560dddcf6abSAdrian Hunter 561dddcf6abSAdrian Hunter return NULL; 562dddcf6abSAdrian Hunter } 563dddcf6abSAdrian Hunter 56463503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist, 56575562573SAdrian Hunter union perf_event *event, u64 *id) 56675562573SAdrian Hunter { 567b1fcd190SJiri Olsa const __u64 *array = event->sample.array; 56875562573SAdrian Hunter ssize_t n; 56975562573SAdrian Hunter 57075562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 57175562573SAdrian Hunter 57275562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 57375562573SAdrian Hunter if (evlist->id_pos >= n) 57475562573SAdrian Hunter return -1; 57575562573SAdrian Hunter *id = array[evlist->id_pos]; 57675562573SAdrian Hunter } else { 57775562573SAdrian Hunter if (evlist->is_pos > n) 57875562573SAdrian Hunter return -1; 57975562573SAdrian Hunter n -= evlist->is_pos; 58075562573SAdrian Hunter *id = array[n]; 58175562573SAdrian Hunter } 58275562573SAdrian Hunter return 0; 58375562573SAdrian Hunter } 58475562573SAdrian Hunter 58563503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist, 58675562573SAdrian Hunter union perf_event *event) 58775562573SAdrian Hunter { 588515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 58975562573SAdrian Hunter struct hlist_head *head; 59075562573SAdrian Hunter struct perf_sample_id *sid; 59175562573SAdrian Hunter int hash; 59275562573SAdrian Hunter u64 id; 59375562573SAdrian Hunter 5946484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 59598be6966SAdrian Hunter return first; 59698be6966SAdrian Hunter 5971fc632ceSJiri Olsa if (!first->core.attr.sample_id_all && 59898be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 59998be6966SAdrian Hunter return first; 60075562573SAdrian Hunter 60175562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 60275562573SAdrian Hunter return NULL; 60375562573SAdrian Hunter 60475562573SAdrian Hunter /* Synthesized events have an id of zero */ 60575562573SAdrian Hunter if (!id) 60698be6966SAdrian Hunter return first; 60775562573SAdrian Hunter 60875562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 6091d5af02dSJiri Olsa head = &evlist->core.heads[hash]; 61075562573SAdrian Hunter 61175562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 61275562573SAdrian Hunter if (sid->id == id) 61370c20369SJiri Olsa return container_of(sid->evsel, struct evsel, core); 61475562573SAdrian Hunter } 61575562573SAdrian Hunter return NULL; 61675562573SAdrian Hunter } 61775562573SAdrian Hunter 61863503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value) 61965aea233SWang Nan { 62065aea233SWang Nan int i; 62165aea233SWang Nan 6220b72d69aSWang Nan if (!evlist->overwrite_mmap) 623078c3386SWang Nan return 0; 624078c3386SWang Nan 625c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6262cf07b29SJiri Olsa int fd = evlist->overwrite_mmap[i].core.fd; 62765aea233SWang Nan int err; 62865aea233SWang Nan 62965aea233SWang Nan if (fd < 0) 63065aea233SWang Nan continue; 63165aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 63265aea233SWang Nan if (err) 63365aea233SWang Nan return err; 63465aea233SWang Nan } 63565aea233SWang Nan return 0; 63665aea233SWang Nan } 63765aea233SWang Nan 63863503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist) 63965aea233SWang Nan { 64065aea233SWang Nan return perf_evlist__set_paused(evlist, true); 64165aea233SWang Nan } 64265aea233SWang Nan 64363503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist) 64465aea233SWang Nan { 64565aea233SWang Nan return perf_evlist__set_paused(evlist, false); 64665aea233SWang Nan } 64765aea233SWang Nan 648db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist) 649f8a95309SArnaldo Carvalho de Melo { 650aece948fSArnaldo Carvalho de Melo int i; 651f8a95309SArnaldo Carvalho de Melo 652b2cb615dSWang Nan if (evlist->mmap) 653c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 65480e53d11SJiri Olsa perf_mmap__munmap(&evlist->mmap[i].core); 655b2cb615dSWang Nan 6560b72d69aSWang Nan if (evlist->overwrite_mmap) 657c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) 65880e53d11SJiri Olsa perf_mmap__munmap(&evlist->overwrite_mmap[i].core); 659a1f72618SWang Nan } 660aece948fSArnaldo Carvalho de Melo 661db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist) 662a1f72618SWang Nan { 663db6b7b13SJiri Olsa evlist__munmap_nofree(evlist); 66404662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 6650b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 666f8a95309SArnaldo Carvalho de Melo } 667f8a95309SArnaldo Carvalho de Melo 66880e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map) 66980e53d11SJiri Olsa { 67080e53d11SJiri Olsa struct mmap *m = container_of(map, struct mmap, core); 67180e53d11SJiri Olsa 67280e53d11SJiri Olsa mmap__munmap(m); 67380e53d11SJiri Olsa } 67480e53d11SJiri Olsa 675d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist, 6762c5f6d87SKan Liang bool overwrite) 677f8a95309SArnaldo Carvalho de Melo { 678d4c6fb36SWang Nan int i; 679a5830532SJiri Olsa struct mmap *map; 680d4c6fb36SWang Nan 681c976ee11SJiri Olsa map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap)); 6828db6d6b1SWang Nan if (!map) 6838db6d6b1SWang Nan return NULL; 684946ae1d4SWang Nan 685c976ee11SJiri Olsa for (i = 0; i < evlist->core.nr_mmaps; i++) { 6866eb65f7aSJiri Olsa struct perf_mmap *prev = i ? &map[i - 1].core : NULL; 6876eb65f7aSJiri Olsa 6884738ca30SArnaldo Carvalho de Melo /* 6894738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 6906afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 6914738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 6924738ca30SArnaldo Carvalho de Melo * dropped. 6934738ca30SArnaldo Carvalho de Melo * 6944738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 6954738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 6964738ca30SArnaldo Carvalho de Melo */ 6976eb65f7aSJiri Olsa perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb); 6984738ca30SArnaldo Carvalho de Melo } 699353120b4SJiri Olsa 7008db6d6b1SWang Nan return map; 701f8a95309SArnaldo Carvalho de Melo } 702f8a95309SArnaldo Carvalho de Melo 7039abd2ab2SJiri Olsa static void 7049abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist, 7059abd2ab2SJiri Olsa struct perf_mmap_param *_mp, 7069abd2ab2SJiri Olsa int idx, bool per_cpu) 7079abd2ab2SJiri Olsa { 7089abd2ab2SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7099abd2ab2SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 7109abd2ab2SJiri Olsa 7119abd2ab2SJiri Olsa auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu); 7129abd2ab2SJiri Olsa } 7139abd2ab2SJiri Olsa 714bb1b1885SJiri Olsa static struct perf_mmap* 715bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx) 716bb1b1885SJiri Olsa { 717bb1b1885SJiri Olsa struct evlist *evlist = container_of(_evlist, struct evlist, core); 7183805e4f3SJiri Olsa struct mmap *maps; 719bb1b1885SJiri Olsa 7203805e4f3SJiri Olsa maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 721bb1b1885SJiri Olsa 722bb1b1885SJiri Olsa if (!maps) { 7233805e4f3SJiri Olsa maps = evlist__alloc_mmap(evlist, overwrite); 724bb1b1885SJiri Olsa if (!maps) 725bb1b1885SJiri Olsa return NULL; 726bb1b1885SJiri Olsa 7273805e4f3SJiri Olsa if (overwrite) { 728bb1b1885SJiri Olsa evlist->overwrite_mmap = maps; 729bb1b1885SJiri Olsa if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 730bb1b1885SJiri Olsa perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 7313805e4f3SJiri Olsa } else { 7323805e4f3SJiri Olsa evlist->mmap = maps; 733bb1b1885SJiri Olsa } 734bb1b1885SJiri Olsa } 735bb1b1885SJiri Olsa 736bb1b1885SJiri Olsa return &maps[idx].core; 737bb1b1885SJiri Olsa } 738bb1b1885SJiri Olsa 739b80132b1SJiri Olsa static int 740b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp, 741b80132b1SJiri Olsa int output, int cpu) 742b80132b1SJiri Olsa { 743b80132b1SJiri Olsa struct mmap *map = container_of(_map, struct mmap, core); 744b80132b1SJiri Olsa struct mmap_params *mp = container_of(_mp, struct mmap_params, core); 745b80132b1SJiri Olsa 746b80132b1SJiri Olsa return mmap__mmap(map, mp, output, cpu); 747b80132b1SJiri Olsa } 748b80132b1SJiri Olsa 749f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 750994a1f78SJiri Olsa { 751f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 7528185e881SArnaldo Carvalho de Melo int max; 7538185e881SArnaldo Carvalho de Melo 7548185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 7558185e881SArnaldo Carvalho de Melo /* 7568185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 7578185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 7588185e881SArnaldo Carvalho de Melo * die yet... 7598185e881SArnaldo Carvalho de Melo */ 7608185e881SArnaldo Carvalho de Melo max = 512; 7618185e881SArnaldo Carvalho de Melo } else { 7628185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 7638185e881SArnaldo Carvalho de Melo } 7648185e881SArnaldo Carvalho de Melo 7658185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 7661be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 7671be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 768f5e7150cSArnaldo Carvalho de Melo 769f5e7150cSArnaldo Carvalho de Melo return pages; 770f5e7150cSArnaldo Carvalho de Melo } 771f5e7150cSArnaldo Carvalho de Melo 7729521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages) 773f5e7150cSArnaldo Carvalho de Melo { 774f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 775f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 776f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 777994a1f78SJiri Olsa return 0; 778994a1f78SJiri Olsa 779994a1f78SJiri Olsa return (pages + 1) * page_size; 780994a1f78SJiri Olsa } 781994a1f78SJiri Olsa 78233c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 78333c2dcfdSDavid Ahern unsigned long max) 784994a1f78SJiri Olsa { 7852fbe4abeSAdrian Hunter unsigned long pages, val; 78627050f53SJiri Olsa static struct parse_tag tags[] = { 78727050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 78827050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 78927050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 79027050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 79127050f53SJiri Olsa { .tag = 0 }, 79227050f53SJiri Olsa }; 793994a1f78SJiri Olsa 7948973504bSDavid Ahern if (str == NULL) 79533c2dcfdSDavid Ahern return -EINVAL; 7968973504bSDavid Ahern 79727050f53SJiri Olsa val = parse_tag_value(str, tags); 7982fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 79927050f53SJiri Olsa /* we got file size value */ 80027050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 80127050f53SJiri Olsa } else { 80227050f53SJiri Olsa /* we got pages count value */ 80327050f53SJiri Olsa char *eptr; 804994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 80533c2dcfdSDavid Ahern if (*eptr != '\0') 80633c2dcfdSDavid Ahern return -EINVAL; 80727050f53SJiri Olsa } 808994a1f78SJiri Olsa 8092bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 81033c2dcfdSDavid Ahern /* leave number of pages at 0 */ 8111dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 8129808143bSJiri Olsa char buf[100]; 8139808143bSJiri Olsa 81433c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 81591529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 8161dbfa938SAdrian Hunter if (!pages) 8171dbfa938SAdrian Hunter return -EINVAL; 8189808143bSJiri Olsa 8199808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 8209808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 8219808143bSJiri Olsa buf, pages); 8222fbe4abeSAdrian Hunter } 8232fbe4abeSAdrian Hunter 82433c2dcfdSDavid Ahern if (pages > max) 82533c2dcfdSDavid Ahern return -EINVAL; 82633c2dcfdSDavid Ahern 82733c2dcfdSDavid Ahern return pages; 82833c2dcfdSDavid Ahern } 82933c2dcfdSDavid Ahern 830e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 83133c2dcfdSDavid Ahern { 83233c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 83333c2dcfdSDavid Ahern long pages; 83433c2dcfdSDavid Ahern 835f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 83633c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 83733c2dcfdSDavid Ahern 83833c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 83933c2dcfdSDavid Ahern if (pages < 0) { 84033c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 841994a1f78SJiri Olsa return -1; 842994a1f78SJiri Olsa } 843994a1f78SJiri Olsa 844994a1f78SJiri Olsa *mmap_pages = pages; 845994a1f78SJiri Olsa return 0; 846994a1f78SJiri Olsa } 847994a1f78SJiri Olsa 848e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 849e9db1310SAdrian Hunter int unset __maybe_unused) 850e9db1310SAdrian Hunter { 851e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 852e9db1310SAdrian Hunter } 853e9db1310SAdrian Hunter 854c83fa7f2SAdrian Hunter /** 8559521b5f2SJiri Olsa * evlist__mmap_ex - Create mmaps to receive events. 856c83fa7f2SAdrian Hunter * @evlist: list of events 857c83fa7f2SAdrian Hunter * @pages: map length in pages 858c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 859718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 860718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 861f8a95309SArnaldo Carvalho de Melo * 862c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 8639521b5f2SJiri Olsa * perf_mmap__write_tail(). Using evlist__mmap_read() does this 864c83fa7f2SAdrian Hunter * automatically. 865f8a95309SArnaldo Carvalho de Melo * 866718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 867718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 868718c602dSAdrian Hunter * 869c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 870f8a95309SArnaldo Carvalho de Melo */ 8719521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages, 8727a276ff6SWang Nan unsigned int auxtrace_pages, 87351255a8aSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush, 87451255a8aSAlexey Budankov int comp_level) 875f8a95309SArnaldo Carvalho de Melo { 87671f566a3SWang Nan /* 87771f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 87871f566a3SWang Nan * Its value is decided by evsel's write_backward. 87971f566a3SWang Nan * So &mp should not be passed through const pointer. 88071f566a3SWang Nan */ 881e440979fSJiri Olsa struct mmap_params mp = { 882e440979fSJiri Olsa .nr_cblocks = nr_cblocks, 883e440979fSJiri Olsa .affinity = affinity, 884e440979fSJiri Olsa .flush = flush, 885e440979fSJiri Olsa .comp_level = comp_level 886e440979fSJiri Olsa }; 887923d0f18SJiri Olsa struct perf_evlist_mmap_ops ops = { 8889abd2ab2SJiri Olsa .idx = perf_evlist__mmap_cb_idx, 889bb1b1885SJiri Olsa .get = perf_evlist__mmap_cb_get, 890b80132b1SJiri Olsa .mmap = perf_evlist__mmap_cb_mmap, 8919abd2ab2SJiri Olsa }; 89250a682ceSArnaldo Carvalho de Melo 893f6fa4375SJiri Olsa evlist->core.mmap_len = evlist__mmap_size(pages); 894f6fa4375SJiri Olsa pr_debug("mmap size %zuB\n", evlist->core.mmap_len); 895f8a95309SArnaldo Carvalho de Melo 896f6fa4375SJiri Olsa auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len, 897718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 898718c602dSAdrian Hunter 899923d0f18SJiri Olsa return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core); 900f8a95309SArnaldo Carvalho de Melo } 9017e2ed097SArnaldo Carvalho de Melo 9029521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages) 903718c602dSAdrian Hunter { 9049521b5f2SJiri Olsa return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0); 905718c602dSAdrian Hunter } 906718c602dSAdrian Hunter 90763503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target) 9087e2ed097SArnaldo Carvalho de Melo { 909147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 910f854839bSJiri Olsa struct perf_cpu_map *cpus; 9119749b90eSJiri Olsa struct perf_thread_map *threads; 9127e2ed097SArnaldo Carvalho de Melo 913147c508fSJin Yao /* 914147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 915147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 916147c508fSJin Yao * target->system_wide = true. 917147c508fSJin Yao * 918147c508fSJin Yao * If specify '--per-thread' only to perf record, 919147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 920147c508fSJin Yao * 921147c508fSJin Yao * So target->per_thread && target->system_wide is false. 922147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 923147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 924147c508fSJin Yao * current behavior. 925147c508fSJin Yao * 926147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 927147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 928147c508fSJin Yao * per-thread data. thread_map__new_str will call 929147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 930147c508fSJin Yao */ 93173c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 932147c508fSJin Yao all_threads); 93374bfd2b2SAdrian Hunter 93474bfd2b2SAdrian Hunter if (!threads) 9357e2ed097SArnaldo Carvalho de Melo return -1; 9367e2ed097SArnaldo Carvalho de Melo 9379c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 938397721e0SJiri Olsa cpus = perf_cpu_map__dummy_new(); 939879d77d0SNamhyung Kim else 9409c3516d1SJiri Olsa cpus = perf_cpu_map__new(target->cpu_list); 9417e2ed097SArnaldo Carvalho de Melo 94274bfd2b2SAdrian Hunter if (!cpus) 9437e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 9447e2ed097SArnaldo Carvalho de Melo 945ec903f26SJiri Olsa evlist->core.has_user_cpus = !!target->cpu_list; 946ec9a77a7SAdrian Hunter 947453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 948d5bc056eSAdrian Hunter 949bfd1b83dSNamhyung Kim /* as evlist now has references, put count here */ 950bfd1b83dSNamhyung Kim perf_cpu_map__put(cpus); 951bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 952bfd1b83dSNamhyung Kim 953d5bc056eSAdrian Hunter return 0; 9547e2ed097SArnaldo Carvalho de Melo 9557e2ed097SArnaldo Carvalho de Melo out_delete_threads: 9567836e52eSJiri Olsa perf_thread_map__put(threads); 9577e2ed097SArnaldo Carvalho de Melo return -1; 9587e2ed097SArnaldo Carvalho de Melo } 9597e2ed097SArnaldo Carvalho de Melo 96063503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist, 96122c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 96222c8a376SArnaldo Carvalho de Melo { 96332dcd021SJiri Olsa struct evsel *evsel; 96422c8a376SArnaldo Carvalho de Melo 965e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 966862b2f8fSArnaldo Carvalho de Melo __evsel__set_sample_bit(evsel, bit); 96722c8a376SArnaldo Carvalho de Melo } 96822c8a376SArnaldo Carvalho de Melo 96963503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist, 97022c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 97122c8a376SArnaldo Carvalho de Melo { 97232dcd021SJiri Olsa struct evsel *evsel; 97322c8a376SArnaldo Carvalho de Melo 974e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 975862b2f8fSArnaldo Carvalho de Melo __evsel__reset_sample_bit(evsel, bit); 97622c8a376SArnaldo Carvalho de Melo } 97722c8a376SArnaldo Carvalho de Melo 97863503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel) 9790a102479SFrederic Weisbecker { 98032dcd021SJiri Olsa struct evsel *evsel; 981745cefc5SArnaldo Carvalho de Melo int err = 0; 9820a102479SFrederic Weisbecker 983e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 984745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 9850a102479SFrederic Weisbecker continue; 986745cefc5SArnaldo Carvalho de Melo 987d988d5eeSKan Liang /* 988d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 989d988d5eeSKan Liang * So evlist and evsel should always be same. 990d988d5eeSKan Liang */ 991a00571fdSJiri Olsa err = perf_evsel__apply_filter(&evsel->core, evsel->filter); 99223d4aad4SArnaldo Carvalho de Melo if (err) { 99323d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 994745cefc5SArnaldo Carvalho de Melo break; 9950a102479SFrederic Weisbecker } 99623d4aad4SArnaldo Carvalho de Melo } 9970a102479SFrederic Weisbecker 998745cefc5SArnaldo Carvalho de Melo return err; 999745cefc5SArnaldo Carvalho de Melo } 1000745cefc5SArnaldo Carvalho de Melo 100163503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter) 1002745cefc5SArnaldo Carvalho de Melo { 100332dcd021SJiri Olsa struct evsel *evsel; 1004745cefc5SArnaldo Carvalho de Melo int err = 0; 1005745cefc5SArnaldo Carvalho de Melo 100605cea449SArnaldo Carvalho de Melo if (filter == NULL) 100705cea449SArnaldo Carvalho de Melo return -1; 100805cea449SArnaldo Carvalho de Melo 1009e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 10101fc632ceSJiri Olsa if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 1011fdf14720SWang Nan continue; 1012fdf14720SWang Nan 1013ad681adfSArnaldo Carvalho de Melo err = evsel__set_filter(evsel, filter); 1014745cefc5SArnaldo Carvalho de Melo if (err) 1015745cefc5SArnaldo Carvalho de Melo break; 1016745cefc5SArnaldo Carvalho de Melo } 1017745cefc5SArnaldo Carvalho de Melo 1018745cefc5SArnaldo Carvalho de Melo return err; 10190a102479SFrederic Weisbecker } 102074429964SFrederic Weisbecker 102153c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter) 102253c92f73SArnaldo Carvalho de Melo { 102353c92f73SArnaldo Carvalho de Melo struct evsel *evsel; 102453c92f73SArnaldo Carvalho de Melo int err = 0; 102553c92f73SArnaldo Carvalho de Melo 102653c92f73SArnaldo Carvalho de Melo if (filter == NULL) 102753c92f73SArnaldo Carvalho de Melo return -1; 102853c92f73SArnaldo Carvalho de Melo 102953c92f73SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 103053c92f73SArnaldo Carvalho de Melo if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT) 103153c92f73SArnaldo Carvalho de Melo continue; 103253c92f73SArnaldo Carvalho de Melo 1033ad681adfSArnaldo Carvalho de Melo err = evsel__append_tp_filter(evsel, filter); 103453c92f73SArnaldo Carvalho de Melo if (err) 103553c92f73SArnaldo Carvalho de Melo break; 103653c92f73SArnaldo Carvalho de Melo } 103753c92f73SArnaldo Carvalho de Melo 103853c92f73SArnaldo Carvalho de Melo return err; 103953c92f73SArnaldo Carvalho de Melo } 104053c92f73SArnaldo Carvalho de Melo 1041da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids) 1042cfd70a26SArnaldo Carvalho de Melo { 1043cfd70a26SArnaldo Carvalho de Melo char *filter; 1044be199adaSArnaldo Carvalho de Melo size_t i; 1045cfd70a26SArnaldo Carvalho de Melo 1046be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1047be199adaSArnaldo Carvalho de Melo if (i == 0) { 1048be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 104905cea449SArnaldo Carvalho de Melo return NULL; 1050be199adaSArnaldo Carvalho de Melo } else { 1051be199adaSArnaldo Carvalho de Melo char *tmp; 1052be199adaSArnaldo Carvalho de Melo 1053be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1054be199adaSArnaldo Carvalho de Melo goto out_free; 1055be199adaSArnaldo Carvalho de Melo 1056be199adaSArnaldo Carvalho de Melo free(filter); 1057be199adaSArnaldo Carvalho de Melo filter = tmp; 1058be199adaSArnaldo Carvalho de Melo } 1059be199adaSArnaldo Carvalho de Melo } 1060cfd70a26SArnaldo Carvalho de Melo 106105cea449SArnaldo Carvalho de Melo return filter; 1062be199adaSArnaldo Carvalho de Melo out_free: 1063cfd70a26SArnaldo Carvalho de Melo free(filter); 106405cea449SArnaldo Carvalho de Melo return NULL; 106505cea449SArnaldo Carvalho de Melo } 106605cea449SArnaldo Carvalho de Melo 106705cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 106805cea449SArnaldo Carvalho de Melo { 106905cea449SArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 107005cea449SArnaldo Carvalho de Melo int ret = perf_evlist__set_tp_filter(evlist, filter); 107105cea449SArnaldo Carvalho de Melo 107205cea449SArnaldo Carvalho de Melo free(filter); 1073cfd70a26SArnaldo Carvalho de Melo return ret; 1074cfd70a26SArnaldo Carvalho de Melo } 1075cfd70a26SArnaldo Carvalho de Melo 107663503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid) 1077be199adaSArnaldo Carvalho de Melo { 10787ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1079be199adaSArnaldo Carvalho de Melo } 1080be199adaSArnaldo Carvalho de Melo 10811827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids) 10821827ab5bSArnaldo Carvalho de Melo { 10831827ab5bSArnaldo Carvalho de Melo char *filter = asprintf__tp_filter_pids(npids, pids); 10841827ab5bSArnaldo Carvalho de Melo int ret = perf_evlist__append_tp_filter(evlist, filter); 10851827ab5bSArnaldo Carvalho de Melo 10861827ab5bSArnaldo Carvalho de Melo free(filter); 10871827ab5bSArnaldo Carvalho de Melo return ret; 10881827ab5bSArnaldo Carvalho de Melo } 10891827ab5bSArnaldo Carvalho de Melo 10901827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid) 10911827ab5bSArnaldo Carvalho de Melo { 10921827ab5bSArnaldo Carvalho de Melo return perf_evlist__append_tp_filter_pids(evlist, 1, &pid); 10931827ab5bSArnaldo Carvalho de Melo } 10941827ab5bSArnaldo Carvalho de Melo 1095b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist) 109674429964SFrederic Weisbecker { 109732dcd021SJiri Olsa struct evsel *pos; 1098c2a70653SArnaldo Carvalho de Melo 10996484d2f9SJiri Olsa if (evlist->core.nr_entries == 1) 110075562573SAdrian Hunter return true; 110175562573SAdrian Hunter 110275562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 110375562573SAdrian Hunter return false; 110475562573SAdrian Hunter 1105e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 110675562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 110775562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1108c2a70653SArnaldo Carvalho de Melo return false; 110974429964SFrederic Weisbecker } 111074429964SFrederic Weisbecker 1111c2a70653SArnaldo Carvalho de Melo return true; 1112c2a70653SArnaldo Carvalho de Melo } 1113c2a70653SArnaldo Carvalho de Melo 1114b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist) 1115c2a70653SArnaldo Carvalho de Melo { 111632dcd021SJiri Olsa struct evsel *evsel; 111775562573SAdrian Hunter 111875562573SAdrian Hunter if (evlist->combined_sample_type) 111975562573SAdrian Hunter return evlist->combined_sample_type; 112075562573SAdrian Hunter 1121e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11221fc632ceSJiri Olsa evlist->combined_sample_type |= evsel->core.attr.sample_type; 112375562573SAdrian Hunter 112475562573SAdrian Hunter return evlist->combined_sample_type; 112575562573SAdrian Hunter } 112675562573SAdrian Hunter 1127b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist) 112875562573SAdrian Hunter { 112975562573SAdrian Hunter evlist->combined_sample_type = 0; 1130b3c2cc2bSArnaldo Carvalho de Melo return __evlist__combined_sample_type(evlist); 1131c2a70653SArnaldo Carvalho de Melo } 1132c2a70653SArnaldo Carvalho de Melo 113392c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist) 113498df858eSAndi Kleen { 113532dcd021SJiri Olsa struct evsel *evsel; 113698df858eSAndi Kleen u64 branch_type = 0; 113798df858eSAndi Kleen 1138e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 11391fc632ceSJiri Olsa branch_type |= evsel->core.attr.branch_sample_type; 114098df858eSAndi Kleen return branch_type; 114198df858eSAndi Kleen } 114298df858eSAndi Kleen 114363503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist) 11449ede473cSJiri Olsa { 1145515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 11461fc632ceSJiri Olsa u64 read_format = first->core.attr.read_format; 11471fc632ceSJiri Olsa u64 sample_type = first->core.attr.sample_type; 11489ede473cSJiri Olsa 1149e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 115094d3820fSAdrian Hunter if (read_format != pos->core.attr.read_format) { 115194d3820fSAdrian Hunter pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n", 115294d3820fSAdrian Hunter read_format, (u64)pos->core.attr.read_format); 115394d3820fSAdrian Hunter } 11549ede473cSJiri Olsa } 11559ede473cSJiri Olsa 11569ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 11579ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 11589ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 11599ede473cSJiri Olsa return false; 11609ede473cSJiri Olsa } 11619ede473cSJiri Olsa 11629ede473cSJiri Olsa return true; 11639ede473cSJiri Olsa } 11649ede473cSJiri Olsa 116563503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist) 116681e36bffSArnaldo Carvalho de Melo { 1167515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 116881e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 116981e36bffSArnaldo Carvalho de Melo u64 sample_type; 117081e36bffSArnaldo Carvalho de Melo u16 size = 0; 117181e36bffSArnaldo Carvalho de Melo 11721fc632ceSJiri Olsa if (!first->core.attr.sample_id_all) 117381e36bffSArnaldo Carvalho de Melo goto out; 117481e36bffSArnaldo Carvalho de Melo 11751fc632ceSJiri Olsa sample_type = first->core.attr.sample_type; 117681e36bffSArnaldo Carvalho de Melo 117781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 117881e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 117981e36bffSArnaldo Carvalho de Melo 118081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 118181e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 118281e36bffSArnaldo Carvalho de Melo 118381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 118481e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 118581e36bffSArnaldo Carvalho de Melo 118681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 118781e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 118881e36bffSArnaldo Carvalho de Melo 118981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 119081e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 119175562573SAdrian Hunter 119275562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 119375562573SAdrian Hunter size += sizeof(data->id); 119481e36bffSArnaldo Carvalho de Melo out: 119581e36bffSArnaldo Carvalho de Melo return size; 119681e36bffSArnaldo Carvalho de Melo } 119781e36bffSArnaldo Carvalho de Melo 11988cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist) 1199c2a70653SArnaldo Carvalho de Melo { 1200515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist), *pos = first; 1201c2a70653SArnaldo Carvalho de Melo 1202e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 12031fc632ceSJiri Olsa if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all) 1204c2a70653SArnaldo Carvalho de Melo return false; 1205c2a70653SArnaldo Carvalho de Melo } 1206c2a70653SArnaldo Carvalho de Melo 1207c2a70653SArnaldo Carvalho de Melo return true; 120874429964SFrederic Weisbecker } 120974429964SFrederic Weisbecker 12108cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist) 121174429964SFrederic Weisbecker { 1212515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 12131fc632ceSJiri Olsa return first->core.attr.sample_id_all; 121474429964SFrederic Weisbecker } 121581cce8deSArnaldo Carvalho de Melo 121663503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist, 121732dcd021SJiri Olsa struct evsel *evsel) 121881cce8deSArnaldo Carvalho de Melo { 121981cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 122081cce8deSArnaldo Carvalho de Melo } 1221727ab04eSArnaldo Carvalho de Melo 1222750b4edeSJiri Olsa void evlist__close(struct evlist *evlist) 1223a74b4b66SNamhyung Kim { 122432dcd021SJiri Olsa struct evsel *evsel; 12257736627bSAndi Kleen struct affinity affinity; 12267736627bSAndi Kleen int cpu, i; 1227a74b4b66SNamhyung Kim 12287736627bSAndi Kleen /* 12297736627bSAndi Kleen * With perf record core.cpus is usually NULL. 12307736627bSAndi Kleen * Use the old method to handle this for now. 12317736627bSAndi Kleen */ 12327736627bSAndi Kleen if (!evlist->core.cpus) { 1233475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 123488761fa1SJiri Olsa evsel__close(evsel); 12357736627bSAndi Kleen return; 12367736627bSAndi Kleen } 12377736627bSAndi Kleen 12387736627bSAndi Kleen if (affinity__setup(&affinity) < 0) 12397736627bSAndi Kleen return; 12407736627bSAndi Kleen evlist__for_each_cpu(evlist, i, cpu) { 12417736627bSAndi Kleen affinity__set(&affinity, cpu); 12427736627bSAndi Kleen 12437736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12447736627bSAndi Kleen if (evsel__cpu_iter_skip(evsel, cpu)) 12457736627bSAndi Kleen continue; 12467736627bSAndi Kleen perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1); 12477736627bSAndi Kleen } 12487736627bSAndi Kleen } 12497736627bSAndi Kleen affinity__cleanup(&affinity); 12507736627bSAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) { 12517736627bSAndi Kleen perf_evsel__free_fd(&evsel->core); 12527736627bSAndi Kleen perf_evsel__free_id(&evsel->core); 12537736627bSAndi Kleen } 1254a74b4b66SNamhyung Kim } 1255a74b4b66SNamhyung Kim 125663503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist) 12574112eb18SArnaldo Carvalho de Melo { 1258f854839bSJiri Olsa struct perf_cpu_map *cpus; 12599749b90eSJiri Olsa struct perf_thread_map *threads; 12604112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 12614112eb18SArnaldo Carvalho de Melo 12624112eb18SArnaldo Carvalho de Melo /* 12634112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 12644112eb18SArnaldo Carvalho de Melo * an all cpus map. 12654112eb18SArnaldo Carvalho de Melo * 12664112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 12674112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 12684112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 12694112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 12704112eb18SArnaldo Carvalho de Melo */ 12719c3516d1SJiri Olsa cpus = perf_cpu_map__new(NULL); 12728c0498b6SAdrian Hunter if (!cpus) 12734112eb18SArnaldo Carvalho de Melo goto out; 12744112eb18SArnaldo Carvalho de Melo 12754b49cce2SJiri Olsa threads = perf_thread_map__new_dummy(); 12768c0498b6SAdrian Hunter if (!threads) 12778c0498b6SAdrian Hunter goto out_put; 12784112eb18SArnaldo Carvalho de Melo 1279453fa030SJiri Olsa perf_evlist__set_maps(&evlist->core, cpus, threads); 1280bfd1b83dSNamhyung Kim 1281bfd1b83dSNamhyung Kim perf_thread_map__put(threads); 12828c0498b6SAdrian Hunter out_put: 128338f01d8dSJiri Olsa perf_cpu_map__put(cpus); 1284bfd1b83dSNamhyung Kim out: 1285bfd1b83dSNamhyung Kim return err; 12864112eb18SArnaldo Carvalho de Melo } 12874112eb18SArnaldo Carvalho de Melo 1288474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist) 1289727ab04eSArnaldo Carvalho de Melo { 129032dcd021SJiri Olsa struct evsel *evsel; 1291a74b4b66SNamhyung Kim int err; 1292727ab04eSArnaldo Carvalho de Melo 12934112eb18SArnaldo Carvalho de Melo /* 12944112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 12954112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 12964112eb18SArnaldo Carvalho de Melo */ 129703617c22SJiri Olsa if (evlist->core.threads == NULL && evlist->core.cpus == NULL) { 12984112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 12994112eb18SArnaldo Carvalho de Melo if (err < 0) 13004112eb18SArnaldo Carvalho de Melo goto out_err; 13014112eb18SArnaldo Carvalho de Melo } 13024112eb18SArnaldo Carvalho de Melo 1303733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1304733cd2feSAdrian Hunter 1305e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1306af663bd0SJiri Olsa err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads); 1307727ab04eSArnaldo Carvalho de Melo if (err < 0) 1308727ab04eSArnaldo Carvalho de Melo goto out_err; 1309727ab04eSArnaldo Carvalho de Melo } 1310727ab04eSArnaldo Carvalho de Melo 1311727ab04eSArnaldo Carvalho de Melo return 0; 1312727ab04eSArnaldo Carvalho de Melo out_err: 1313750b4edeSJiri Olsa evlist__close(evlist); 131441c21a68SNamhyung Kim errno = -err; 1315727ab04eSArnaldo Carvalho de Melo return err; 1316727ab04eSArnaldo Carvalho de Melo } 131735b9d88eSArnaldo Carvalho de Melo 131863503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target, 131955e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1320735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 132135b9d88eSArnaldo Carvalho de Melo { 132235b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 132335b9d88eSArnaldo Carvalho de Melo char bf; 132435b9d88eSArnaldo Carvalho de Melo 132535b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 132635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 132735b9d88eSArnaldo Carvalho de Melo return -1; 132835b9d88eSArnaldo Carvalho de Melo } 132935b9d88eSArnaldo Carvalho de Melo 133035b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 133135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 133235b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 133335b9d88eSArnaldo Carvalho de Melo } 133435b9d88eSArnaldo Carvalho de Melo 133535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 133635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 133735b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 133835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 133935b9d88eSArnaldo Carvalho de Melo } 134035b9d88eSArnaldo Carvalho de Melo 134135b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13425f1c4225SArnaldo Carvalho de Melo int ret; 13435f1c4225SArnaldo Carvalho de Melo 1344119fa3c9SNamhyung Kim if (pipe_output) 134535b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 134635b9d88eSArnaldo Carvalho de Melo 13470817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13480817df08SDavid Ahern 134935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 135035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 135135b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 135235b9d88eSArnaldo Carvalho de Melo 135335b9d88eSArnaldo Carvalho de Melo /* 135435b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 135535b9d88eSArnaldo Carvalho de Melo */ 135635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 135735b9d88eSArnaldo Carvalho de Melo 135835b9d88eSArnaldo Carvalho de Melo /* 135935b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 136035b9d88eSArnaldo Carvalho de Melo */ 13615f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 13625f1c4225SArnaldo Carvalho de Melo /* 13635f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 13645f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 13655f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 13665f1c4225SArnaldo Carvalho de Melo * 136720f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 13685f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 13695f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 13705f1c4225SArnaldo Carvalho de Melo * here. 13715f1c4225SArnaldo Carvalho de Melo */ 13725f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 13735f1c4225SArnaldo Carvalho de Melo if (ret == -1) 137435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 13755f1c4225SArnaldo Carvalho de Melo exit(ret); 13765f1c4225SArnaldo Carvalho de Melo } 137735b9d88eSArnaldo Carvalho de Melo 137835b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 137935b9d88eSArnaldo Carvalho de Melo 1380735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1381f33cbe72SArnaldo Carvalho de Melo union sigval val; 1382f33cbe72SArnaldo Carvalho de Melo 1383f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1384f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 138535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1386f33cbe72SArnaldo Carvalho de Melo } else 1387f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 138835b9d88eSArnaldo Carvalho de Melo exit(-1); 138935b9d88eSArnaldo Carvalho de Melo } 139035b9d88eSArnaldo Carvalho de Melo 1391735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1392735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1393735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1394735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1395735f7e0bSArnaldo Carvalho de Melo }; 1396735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1397735f7e0bSArnaldo Carvalho de Melo } 1398735f7e0bSArnaldo Carvalho de Melo 13991aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 140003617c22SJiri Olsa if (evlist->core.threads == NULL) { 14011aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 14021aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 14031aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 14041aaf63b1SArnaldo Carvalho de Melo } 140503617c22SJiri Olsa perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid); 14061aaf63b1SArnaldo Carvalho de Melo } 140735b9d88eSArnaldo Carvalho de Melo 140835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 140935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 141035b9d88eSArnaldo Carvalho de Melo /* 141135b9d88eSArnaldo Carvalho de Melo * wait for child to settle 141235b9d88eSArnaldo Carvalho de Melo */ 141335b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 141435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 141535b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 141635b9d88eSArnaldo Carvalho de Melo } 141735b9d88eSArnaldo Carvalho de Melo 1418bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 141935b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 142035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 142135b9d88eSArnaldo Carvalho de Melo return 0; 142235b9d88eSArnaldo Carvalho de Melo 142335b9d88eSArnaldo Carvalho de Melo out_close_pipes: 142435b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 142535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 142635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 142735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 142835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 142935b9d88eSArnaldo Carvalho de Melo return -1; 143035b9d88eSArnaldo Carvalho de Melo } 143135b9d88eSArnaldo Carvalho de Melo 143263503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist) 143335b9d88eSArnaldo Carvalho de Melo { 143435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1435b3824404SDavid Ahern char bf = 0; 1436bcf3145fSNamhyung Kim int ret; 143735b9d88eSArnaldo Carvalho de Melo /* 143835b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 143935b9d88eSArnaldo Carvalho de Melo */ 1440bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1441bcf3145fSNamhyung Kim if (ret < 0) 1442e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1443bcf3145fSNamhyung Kim 1444bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1445bcf3145fSNamhyung Kim return ret; 144635b9d88eSArnaldo Carvalho de Melo } 144735b9d88eSArnaldo Carvalho de Melo 144835b9d88eSArnaldo Carvalho de Melo return 0; 144935b9d88eSArnaldo Carvalho de Melo } 1450cb0b29e0SArnaldo Carvalho de Melo 145163503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event, 14520807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1453cb0b29e0SArnaldo Carvalho de Melo { 145432dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 145575562573SAdrian Hunter 145675562573SAdrian Hunter if (!evsel) 145775562573SAdrian Hunter return -EFAULT; 14586b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample(evsel, event, sample); 1459cb0b29e0SArnaldo Carvalho de Melo } 146078f067b3SArnaldo Carvalho de Melo 146163503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist, 146201468120SJiri Olsa union perf_event *event, 146301468120SJiri Olsa u64 *timestamp) 146401468120SJiri Olsa { 146532dcd021SJiri Olsa struct evsel *evsel = perf_evlist__event2evsel(evlist, event); 146601468120SJiri Olsa 146701468120SJiri Olsa if (!evsel) 146801468120SJiri Olsa return -EFAULT; 14696b6017a2SArnaldo Carvalho de Melo return evsel__parse_sample_timestamp(evsel, event, timestamp); 147001468120SJiri Olsa } 147101468120SJiri Olsa 1472d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size) 1473a8f23d8fSArnaldo Carvalho de Melo { 1474a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1475c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1476a8f23d8fSArnaldo Carvalho de Melo 1477a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1478a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1479a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1480a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1481a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1482a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1483a8f23d8fSArnaldo Carvalho de Melo 14841a47245dSAdrian Hunter value = perf_event_paranoid(); 1485a8f23d8fSArnaldo Carvalho de Melo 1486a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1487a8f23d8fSArnaldo Carvalho de Melo 1488a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1489a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1490a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1491a8f23d8fSArnaldo Carvalho de Melo } 1492a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14935229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1494a8f23d8fSArnaldo Carvalho de Melo 1495a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14965229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 14975229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1498a8f23d8fSArnaldo Carvalho de Melo break; 1499d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1500515dbe48SJiri Olsa struct evsel *first = evlist__first(evlist); 1501d9aade7fSArnaldo Carvalho de Melo int max_freq; 1502d9aade7fSArnaldo Carvalho de Melo 1503d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1504d9aade7fSArnaldo Carvalho de Melo goto out_default; 1505d9aade7fSArnaldo Carvalho de Melo 15061fc632ceSJiri Olsa if (first->core.attr.sample_freq < (u64)max_freq) 1507d9aade7fSArnaldo Carvalho de Melo goto out_default; 1508d9aade7fSArnaldo Carvalho de Melo 1509d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1510d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1511d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1512d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 15131fc632ceSJiri Olsa emsg, max_freq, first->core.attr.sample_freq); 1514d9aade7fSArnaldo Carvalho de Melo break; 1515d9aade7fSArnaldo Carvalho de Melo } 1516a8f23d8fSArnaldo Carvalho de Melo default: 1517d9aade7fSArnaldo Carvalho de Melo out_default: 1518a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1519a8f23d8fSArnaldo Carvalho de Melo break; 1520a8f23d8fSArnaldo Carvalho de Melo } 1521a8f23d8fSArnaldo Carvalho de Melo 1522a8f23d8fSArnaldo Carvalho de Melo return 0; 1523a8f23d8fSArnaldo Carvalho de Melo } 1524a025e4f0SAdrian Hunter 1525d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size) 1526956fa571SArnaldo Carvalho de Melo { 1527c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1528f6fa4375SJiri Olsa int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0; 1529956fa571SArnaldo Carvalho de Melo 1530956fa571SArnaldo Carvalho de Melo switch (err) { 1531956fa571SArnaldo Carvalho de Melo case EPERM: 1532e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1533e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1534e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1535956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1536e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1537e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1538e965bea1SArnaldo Carvalho de Melo 1539e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1540e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1541e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1542e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1543e965bea1SArnaldo Carvalho de Melo } 1544e965bea1SArnaldo Carvalho de Melo 1545e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1546e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1547956fa571SArnaldo Carvalho de Melo break; 1548956fa571SArnaldo Carvalho de Melo default: 1549956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1550956fa571SArnaldo Carvalho de Melo break; 1551956fa571SArnaldo Carvalho de Melo } 1552956fa571SArnaldo Carvalho de Melo 1553956fa571SArnaldo Carvalho de Melo return 0; 1554956fa571SArnaldo Carvalho de Melo } 1555956fa571SArnaldo Carvalho de Melo 155663503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist, 155732dcd021SJiri Olsa struct evsel *move_evsel) 1558a025e4f0SAdrian Hunter { 155932dcd021SJiri Olsa struct evsel *evsel, *n; 1560a025e4f0SAdrian Hunter LIST_HEAD(move); 1561a025e4f0SAdrian Hunter 1562515dbe48SJiri Olsa if (move_evsel == evlist__first(evlist)) 1563a025e4f0SAdrian Hunter return; 1564a025e4f0SAdrian Hunter 1565e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1566a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1567b27c4eceSJiri Olsa list_move_tail(&evsel->core.node, &move); 1568a025e4f0SAdrian Hunter } 1569a025e4f0SAdrian Hunter 1570ce9036a6SJiri Olsa list_splice(&move, &evlist->core.entries); 1571a025e4f0SAdrian Hunter } 157260b0896cSAdrian Hunter 1573442ad225SAdrian Hunter struct evsel *perf_evlist__get_tracking_event(struct evlist *evlist) 1574442ad225SAdrian Hunter { 1575442ad225SAdrian Hunter struct evsel *evsel; 1576442ad225SAdrian Hunter 1577442ad225SAdrian Hunter evlist__for_each_entry(evlist, evsel) { 1578442ad225SAdrian Hunter if (evsel->tracking) 1579442ad225SAdrian Hunter return evsel; 1580442ad225SAdrian Hunter } 1581442ad225SAdrian Hunter 1582442ad225SAdrian Hunter return evlist__first(evlist); 1583442ad225SAdrian Hunter } 1584442ad225SAdrian Hunter 158563503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist, 158632dcd021SJiri Olsa struct evsel *tracking_evsel) 158760b0896cSAdrian Hunter { 158832dcd021SJiri Olsa struct evsel *evsel; 158960b0896cSAdrian Hunter 159060b0896cSAdrian Hunter if (tracking_evsel->tracking) 159160b0896cSAdrian Hunter return; 159260b0896cSAdrian Hunter 1593e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 159460b0896cSAdrian Hunter if (evsel != tracking_evsel) 159560b0896cSAdrian Hunter evsel->tracking = false; 159660b0896cSAdrian Hunter } 159760b0896cSAdrian Hunter 159860b0896cSAdrian Hunter tracking_evsel->tracking = true; 159960b0896cSAdrian Hunter } 16007630b3e2SWang Nan 160132dcd021SJiri Olsa struct evsel * 160263503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist, 16037630b3e2SWang Nan const char *str) 16047630b3e2SWang Nan { 160532dcd021SJiri Olsa struct evsel *evsel; 16067630b3e2SWang Nan 1607e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16087630b3e2SWang Nan if (!evsel->name) 16097630b3e2SWang Nan continue; 16107630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 16117630b3e2SWang Nan return evsel; 16127630b3e2SWang Nan } 16137630b3e2SWang Nan 16147630b3e2SWang Nan return NULL; 16157630b3e2SWang Nan } 161654cc54deSWang Nan 161763503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist, 161854cc54deSWang Nan enum bkw_mmap_state state) 161954cc54deSWang Nan { 162054cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 162154cc54deSWang Nan enum action { 162254cc54deSWang Nan NONE, 162354cc54deSWang Nan PAUSE, 162454cc54deSWang Nan RESUME, 162554cc54deSWang Nan } action = NONE; 162654cc54deSWang Nan 16270b72d69aSWang Nan if (!evlist->overwrite_mmap) 162854cc54deSWang Nan return; 162954cc54deSWang Nan 163054cc54deSWang Nan switch (old_state) { 163154cc54deSWang Nan case BKW_MMAP_NOTREADY: { 163254cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1633dd8bd53aSLuis de Bethencourt goto state_err; 163454cc54deSWang Nan break; 163554cc54deSWang Nan } 163654cc54deSWang Nan case BKW_MMAP_RUNNING: { 163754cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 163854cc54deSWang Nan goto state_err; 163954cc54deSWang Nan action = PAUSE; 164054cc54deSWang Nan break; 164154cc54deSWang Nan } 164254cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 164354cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 164454cc54deSWang Nan goto state_err; 164554cc54deSWang Nan break; 164654cc54deSWang Nan } 164754cc54deSWang Nan case BKW_MMAP_EMPTY: { 164854cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 164954cc54deSWang Nan goto state_err; 165054cc54deSWang Nan action = RESUME; 165154cc54deSWang Nan break; 165254cc54deSWang Nan } 165354cc54deSWang Nan default: 165454cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 165554cc54deSWang Nan } 165654cc54deSWang Nan 165754cc54deSWang Nan evlist->bkw_mmap_state = state; 165854cc54deSWang Nan 165954cc54deSWang Nan switch (action) { 166054cc54deSWang Nan case PAUSE: 166154cc54deSWang Nan perf_evlist__pause(evlist); 166254cc54deSWang Nan break; 166354cc54deSWang Nan case RESUME: 166454cc54deSWang Nan perf_evlist__resume(evlist); 166554cc54deSWang Nan break; 166654cc54deSWang Nan case NONE: 166754cc54deSWang Nan default: 166854cc54deSWang Nan break; 166954cc54deSWang Nan } 167054cc54deSWang Nan 167154cc54deSWang Nan state_err: 167254cc54deSWang Nan return; 167354cc54deSWang Nan } 167407d6f446SArnaldo Carvalho de Melo 167563503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist) 167607d6f446SArnaldo Carvalho de Melo { 167732dcd021SJiri Olsa struct evsel *evsel; 167807d6f446SArnaldo Carvalho de Melo 167907d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 16801fc632ceSJiri Olsa if (!evsel->core.attr.exclude_kernel) 168107d6f446SArnaldo Carvalho de Melo return false; 168207d6f446SArnaldo Carvalho de Melo } 168307d6f446SArnaldo Carvalho de Melo 168407d6f446SArnaldo Carvalho de Melo return true; 168507d6f446SArnaldo Carvalho de Melo } 1686e2bdbe80SJin Yao 1687e2bdbe80SJin Yao /* 1688e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1689e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1690e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1691e2bdbe80SJin Yao */ 169263503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist) 1693e2bdbe80SJin Yao { 1694e2bdbe80SJin Yao if (!evlist->nr_groups) { 1695515dbe48SJiri Olsa struct evsel *leader = evlist__first(evlist); 1696e2bdbe80SJin Yao 1697*a622eafaSArnaldo Carvalho de Melo evlist__set_leader(evlist); 1698e2bdbe80SJin Yao leader->forced_leader = true; 1699e2bdbe80SJin Yao } 1700e2bdbe80SJin Yao } 1701c3537fc2SAndi Kleen 170263503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list, 17034804e011SAndi Kleen struct evsel *evsel, 17044804e011SAndi Kleen bool close) 1705c3537fc2SAndi Kleen { 170632dcd021SJiri Olsa struct evsel *c2, *leader; 1707c3537fc2SAndi Kleen bool is_open = true; 1708c3537fc2SAndi Kleen 1709c3537fc2SAndi Kleen leader = evsel->leader; 1710c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 17115643b1a5SJiri Olsa leader->name, leader->core.nr_members); 1712c3537fc2SAndi Kleen 1713c3537fc2SAndi Kleen /* 1714c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1715c3537fc2SAndi Kleen * include the first entry. 1716c3537fc2SAndi Kleen */ 1717c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1718c3537fc2SAndi Kleen if (c2 == evsel) 1719c3537fc2SAndi Kleen is_open = false; 1720c3537fc2SAndi Kleen if (c2->leader == leader) { 17214804e011SAndi Kleen if (is_open && close) 17225a40e199SAndi Kleen perf_evsel__close(&c2->core); 1723c3537fc2SAndi Kleen c2->leader = c2; 17245643b1a5SJiri Olsa c2->core.nr_members = 0; 17254804e011SAndi Kleen /* 17264804e011SAndi Kleen * Set this for all former members of the group 17274804e011SAndi Kleen * to indicate they get reopened. 17284804e011SAndi Kleen */ 17294804e011SAndi Kleen c2->reset_group = true; 1730c3537fc2SAndi Kleen } 1731c3537fc2SAndi Kleen } 1732c3537fc2SAndi Kleen return leader; 1733c3537fc2SAndi Kleen } 1734ec886bf5SAlexey Budankov 1735a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 1736a8fcbd26SAdrian Hunter { 1737a8fcbd26SAdrian Hunter char *s, *p; 1738a8fcbd26SAdrian Hunter int ret = 0, fd; 1739a8fcbd26SAdrian Hunter 1740a8fcbd26SAdrian Hunter if (strncmp(str, "fifo:", 5)) 1741a8fcbd26SAdrian Hunter return -EINVAL; 1742a8fcbd26SAdrian Hunter 1743a8fcbd26SAdrian Hunter str += 5; 1744a8fcbd26SAdrian Hunter if (!*str || *str == ',') 1745a8fcbd26SAdrian Hunter return -EINVAL; 1746a8fcbd26SAdrian Hunter 1747a8fcbd26SAdrian Hunter s = strdup(str); 1748a8fcbd26SAdrian Hunter if (!s) 1749a8fcbd26SAdrian Hunter return -ENOMEM; 1750a8fcbd26SAdrian Hunter 1751a8fcbd26SAdrian Hunter p = strchr(s, ','); 1752a8fcbd26SAdrian Hunter if (p) 1753a8fcbd26SAdrian Hunter *p = '\0'; 1754a8fcbd26SAdrian Hunter 1755a8fcbd26SAdrian Hunter /* 1756a8fcbd26SAdrian Hunter * O_RDWR avoids POLLHUPs which is necessary to allow the other 1757a8fcbd26SAdrian Hunter * end of a FIFO to be repeatedly opened and closed. 1758a8fcbd26SAdrian Hunter */ 1759a8fcbd26SAdrian Hunter fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1760a8fcbd26SAdrian Hunter if (fd < 0) { 1761a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", s); 1762a8fcbd26SAdrian Hunter ret = -errno; 1763a8fcbd26SAdrian Hunter goto out_free; 1764a8fcbd26SAdrian Hunter } 1765a8fcbd26SAdrian Hunter *ctl_fd = fd; 1766a8fcbd26SAdrian Hunter *ctl_fd_close = true; 1767a8fcbd26SAdrian Hunter 1768a8fcbd26SAdrian Hunter if (p && *++p) { 1769a8fcbd26SAdrian Hunter /* O_RDWR | O_NONBLOCK means the other end need not be open */ 1770a8fcbd26SAdrian Hunter fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC); 1771a8fcbd26SAdrian Hunter if (fd < 0) { 1772a8fcbd26SAdrian Hunter pr_err("Failed to open '%s'\n", p); 1773a8fcbd26SAdrian Hunter ret = -errno; 1774a8fcbd26SAdrian Hunter goto out_free; 1775a8fcbd26SAdrian Hunter } 1776a8fcbd26SAdrian Hunter *ctl_fd_ack = fd; 1777a8fcbd26SAdrian Hunter } 1778a8fcbd26SAdrian Hunter 1779a8fcbd26SAdrian Hunter out_free: 1780a8fcbd26SAdrian Hunter free(s); 1781a8fcbd26SAdrian Hunter return ret; 1782a8fcbd26SAdrian Hunter } 1783a8fcbd26SAdrian Hunter 1784a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close) 17859864a66dSAdrian Hunter { 17869864a66dSAdrian Hunter char *comma = NULL, *endptr = NULL; 17879864a66dSAdrian Hunter 1788a8fcbd26SAdrian Hunter *ctl_fd_close = false; 1789a8fcbd26SAdrian Hunter 17909864a66dSAdrian Hunter if (strncmp(str, "fd:", 3)) 1791a8fcbd26SAdrian Hunter return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close); 17929864a66dSAdrian Hunter 17939864a66dSAdrian Hunter *ctl_fd = strtoul(&str[3], &endptr, 0); 17949864a66dSAdrian Hunter if (endptr == &str[3]) 17959864a66dSAdrian Hunter return -EINVAL; 17969864a66dSAdrian Hunter 17979864a66dSAdrian Hunter comma = strchr(str, ','); 17989864a66dSAdrian Hunter if (comma) { 17999864a66dSAdrian Hunter if (endptr != comma) 18009864a66dSAdrian Hunter return -EINVAL; 18019864a66dSAdrian Hunter 18029864a66dSAdrian Hunter *ctl_fd_ack = strtoul(comma + 1, &endptr, 0); 18039864a66dSAdrian Hunter if (endptr == comma + 1 || *endptr != '\0') 18049864a66dSAdrian Hunter return -EINVAL; 18059864a66dSAdrian Hunter } 18069864a66dSAdrian Hunter 18079864a66dSAdrian Hunter return 0; 18089864a66dSAdrian Hunter } 18099864a66dSAdrian Hunter 1810ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close) 1811ee7fe31eSAdrian Hunter { 1812ee7fe31eSAdrian Hunter if (*ctl_fd_close) { 1813ee7fe31eSAdrian Hunter *ctl_fd_close = false; 1814ee7fe31eSAdrian Hunter close(ctl_fd); 1815ee7fe31eSAdrian Hunter if (ctl_fd_ack >= 0) 1816ee7fe31eSAdrian Hunter close(ctl_fd_ack); 1817ee7fe31eSAdrian Hunter } 1818ee7fe31eSAdrian Hunter } 1819ee7fe31eSAdrian Hunter 1820ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack) 1821ec886bf5SAlexey Budankov { 1822ec886bf5SAlexey Budankov if (fd == -1) { 1823ec886bf5SAlexey Budankov pr_debug("Control descriptor is not initialized\n"); 1824ec886bf5SAlexey Budankov return 0; 1825ec886bf5SAlexey Budankov } 1826ec886bf5SAlexey Budankov 1827ec886bf5SAlexey Budankov evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, 1828ec886bf5SAlexey Budankov fdarray_flag__nonfilterable); 1829ec886bf5SAlexey Budankov if (evlist->ctl_fd.pos < 0) { 1830ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1831ec886bf5SAlexey Budankov pr_err("Failed to add ctl fd entry: %m\n"); 1832ec886bf5SAlexey Budankov return -1; 1833ec886bf5SAlexey Budankov } 1834ec886bf5SAlexey Budankov 1835ec886bf5SAlexey Budankov evlist->ctl_fd.fd = fd; 1836ec886bf5SAlexey Budankov evlist->ctl_fd.ack = ack; 1837ec886bf5SAlexey Budankov 1838ec886bf5SAlexey Budankov return 0; 1839ec886bf5SAlexey Budankov } 1840ec886bf5SAlexey Budankov 1841ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist) 1842ec886bf5SAlexey Budankov { 1843ec886bf5SAlexey Budankov return evlist->ctl_fd.pos >= 0; 1844ec886bf5SAlexey Budankov } 1845ec886bf5SAlexey Budankov 1846ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist) 1847ec886bf5SAlexey Budankov { 1848ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 1849ec886bf5SAlexey Budankov 1850ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist)) 1851ec886bf5SAlexey Budankov return 0; 1852ec886bf5SAlexey Budankov 1853ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].fd = -1; 1854ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].events = 0; 1855ec886bf5SAlexey Budankov entries[evlist->ctl_fd.pos].revents = 0; 1856ec886bf5SAlexey Budankov 1857ec886bf5SAlexey Budankov evlist->ctl_fd.pos = -1; 1858ec886bf5SAlexey Budankov evlist->ctl_fd.ack = -1; 1859ec886bf5SAlexey Budankov evlist->ctl_fd.fd = -1; 1860ec886bf5SAlexey Budankov 1861ec886bf5SAlexey Budankov return 0; 1862ec886bf5SAlexey Budankov } 1863ec886bf5SAlexey Budankov 1864ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd, 1865ec886bf5SAlexey Budankov char *cmd_data, size_t data_size) 1866ec886bf5SAlexey Budankov { 1867ec886bf5SAlexey Budankov int err; 1868ec886bf5SAlexey Budankov char c; 1869ec886bf5SAlexey Budankov size_t bytes_read = 0; 1870ec886bf5SAlexey Budankov 187140db8ff5SAdrian Hunter *cmd = EVLIST_CTL_CMD_UNSUPPORTED; 1872ec886bf5SAlexey Budankov memset(cmd_data, 0, data_size); 1873ec886bf5SAlexey Budankov data_size--; 1874ec886bf5SAlexey Budankov 1875ec886bf5SAlexey Budankov do { 1876ec886bf5SAlexey Budankov err = read(evlist->ctl_fd.fd, &c, 1); 1877ec886bf5SAlexey Budankov if (err > 0) { 1878ec886bf5SAlexey Budankov if (c == '\n' || c == '\0') 1879ec886bf5SAlexey Budankov break; 1880ec886bf5SAlexey Budankov cmd_data[bytes_read++] = c; 1881ec886bf5SAlexey Budankov if (bytes_read == data_size) 1882ec886bf5SAlexey Budankov break; 188340db8ff5SAdrian Hunter continue; 188440db8ff5SAdrian Hunter } else if (err == -1) { 188540db8ff5SAdrian Hunter if (errno == EINTR) 188640db8ff5SAdrian Hunter continue; 188740db8ff5SAdrian Hunter if (errno == EAGAIN || errno == EWOULDBLOCK) 188840db8ff5SAdrian Hunter err = 0; 188940db8ff5SAdrian Hunter else 1890ec886bf5SAlexey Budankov pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd); 1891ec886bf5SAlexey Budankov } 189240db8ff5SAdrian Hunter break; 1893ec886bf5SAlexey Budankov } while (1); 1894ec886bf5SAlexey Budankov 1895ec886bf5SAlexey Budankov pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data, 1896ec886bf5SAlexey Budankov bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0"); 1897ec886bf5SAlexey Budankov 189840db8ff5SAdrian Hunter if (bytes_read > 0) { 1899ec886bf5SAlexey Budankov if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG, 1900ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) { 1901ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_ENABLE; 1902ec886bf5SAlexey Budankov } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG, 1903ec886bf5SAlexey Budankov (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) { 1904ec886bf5SAlexey Budankov *cmd = EVLIST_CTL_CMD_DISABLE; 1905d20aff15SAdrian Hunter } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG, 1906d20aff15SAdrian Hunter (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) { 1907d20aff15SAdrian Hunter *cmd = EVLIST_CTL_CMD_SNAPSHOT; 1908d20aff15SAdrian Hunter pr_debug("is snapshot\n"); 1909ec886bf5SAlexey Budankov } 1910ec886bf5SAlexey Budankov } 1911ec886bf5SAlexey Budankov 191240db8ff5SAdrian Hunter return bytes_read ? (int)bytes_read : err; 1913ec886bf5SAlexey Budankov } 1914ec886bf5SAlexey Budankov 1915d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist) 1916ec886bf5SAlexey Budankov { 1917ec886bf5SAlexey Budankov int err; 1918ec886bf5SAlexey Budankov 1919ec886bf5SAlexey Budankov if (evlist->ctl_fd.ack == -1) 1920ec886bf5SAlexey Budankov return 0; 1921ec886bf5SAlexey Budankov 1922ec886bf5SAlexey Budankov err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG, 1923ec886bf5SAlexey Budankov sizeof(EVLIST_CTL_CMD_ACK_TAG)); 1924ec886bf5SAlexey Budankov if (err == -1) 1925ec886bf5SAlexey Budankov pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack); 1926ec886bf5SAlexey Budankov 1927ec886bf5SAlexey Budankov return err; 1928ec886bf5SAlexey Budankov } 1929ec886bf5SAlexey Budankov 1930ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd) 1931ec886bf5SAlexey Budankov { 1932ec886bf5SAlexey Budankov int err = 0; 1933ec886bf5SAlexey Budankov char cmd_data[EVLIST_CTL_CMD_MAX_LEN]; 1934ec886bf5SAlexey Budankov int ctlfd_pos = evlist->ctl_fd.pos; 1935ec886bf5SAlexey Budankov struct pollfd *entries = evlist->core.pollfd.entries; 1936ec886bf5SAlexey Budankov 1937ec886bf5SAlexey Budankov if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents) 1938ec886bf5SAlexey Budankov return 0; 1939ec886bf5SAlexey Budankov 1940ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & POLLIN) { 1941ec886bf5SAlexey Budankov err = evlist__ctlfd_recv(evlist, cmd, cmd_data, 1942ec886bf5SAlexey Budankov EVLIST_CTL_CMD_MAX_LEN); 1943ec886bf5SAlexey Budankov if (err > 0) { 1944ec886bf5SAlexey Budankov switch (*cmd) { 1945ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ENABLE: 1946ec886bf5SAlexey Budankov evlist__enable(evlist); 1947ec886bf5SAlexey Budankov break; 1948ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_DISABLE: 1949ec886bf5SAlexey Budankov evlist__disable(evlist); 1950ec886bf5SAlexey Budankov break; 1951d20aff15SAdrian Hunter case EVLIST_CTL_CMD_SNAPSHOT: 1952d20aff15SAdrian Hunter break; 1953ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_ACK: 1954ec886bf5SAlexey Budankov case EVLIST_CTL_CMD_UNSUPPORTED: 1955ec886bf5SAlexey Budankov default: 1956ec886bf5SAlexey Budankov pr_debug("ctlfd: unsupported %d\n", *cmd); 1957ec886bf5SAlexey Budankov break; 1958ec886bf5SAlexey Budankov } 1959d20aff15SAdrian Hunter if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED || 1960d20aff15SAdrian Hunter *cmd == EVLIST_CTL_CMD_SNAPSHOT)) 1961ec886bf5SAlexey Budankov evlist__ctlfd_ack(evlist); 1962ec886bf5SAlexey Budankov } 1963ec886bf5SAlexey Budankov } 1964ec886bf5SAlexey Budankov 1965ec886bf5SAlexey Budankov if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR)) 1966ec886bf5SAlexey Budankov evlist__finalize_ctlfd(evlist); 1967ec886bf5SAlexey Budankov else 1968ec886bf5SAlexey Budankov entries[ctlfd_pos].revents = 0; 1969ec886bf5SAlexey Budankov 1970ec886bf5SAlexey Budankov return err; 1971ec886bf5SAlexey Budankov } 1972b214ba8cSNamhyung Kim 1973b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx) 1974b214ba8cSNamhyung Kim { 1975b214ba8cSNamhyung Kim struct evsel *evsel; 1976b214ba8cSNamhyung Kim 1977b214ba8cSNamhyung Kim evlist__for_each_entry(evlist, evsel) { 1978b214ba8cSNamhyung Kim if (evsel->idx == idx) 1979b214ba8cSNamhyung Kim return evsel; 1980b214ba8cSNamhyung Kim } 1981b214ba8cSNamhyung Kim return NULL; 1982b214ba8cSNamhyung Kim } 1983