1f8a95309SArnaldo Carvalho de Melo /* 2f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3f8a95309SArnaldo Carvalho de Melo * 4f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 5f8a95309SArnaldo Carvalho de Melo * copyright notes. 6f8a95309SArnaldo Carvalho de Melo * 7f8a95309SArnaldo Carvalho de Melo * Released under the GPL v2. (and only v2, not any later version) 8f8a95309SArnaldo Carvalho de Melo */ 9a8c9ae18SArnaldo Carvalho de Melo #include "util.h" 10956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h> 11a43783aeSArnaldo Carvalho de Melo #include <errno.h> 12fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 135c581041SArnaldo Carvalho de Melo #include <poll.h> 14f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 15f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1612864b31SNamhyung Kim #include "target.h" 17361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 18361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 19e3e1a54fSAdrian Hunter #include "debug.h" 2058db1d6eSArnaldo Carvalho de Melo #include "units.h" 2154cc54deSWang Nan #include "asm/bug.h" 229607ad3aSArnaldo Carvalho de Melo #include <signal.h> 2335b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 24361c99a6SArnaldo Carvalho de Melo 2550d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 264b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 2750d08e47SArnaldo Carvalho de Melo 28bafae98eSArnaldo Carvalho de Melo #include <fcntl.h> 2986a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h> 30f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 31f8a95309SArnaldo Carvalho de Melo 3270db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 3370db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 340389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 358dd2a131SJiri Olsa #include <linux/err.h> 3670db7533SArnaldo Carvalho de Melo 37748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE 38748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value); 39748fe088SArnaldo Carvalho de Melo #endif 40748fe088SArnaldo Carvalho de Melo 41f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 42a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 43f8a95309SArnaldo Carvalho de Melo 447e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 457e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 46361c99a6SArnaldo Carvalho de Melo { 4770db7533SArnaldo Carvalho de Melo int i; 4870db7533SArnaldo Carvalho de Melo 4970db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 5070db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 51361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 527e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 531b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 5435b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 5554cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 56361c99a6SArnaldo Carvalho de Melo } 57361c99a6SArnaldo Carvalho de Melo 58334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 59ef1d1af2SArnaldo Carvalho de Melo { 60ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 61ef1d1af2SArnaldo Carvalho de Melo 62ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 63334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 64ef1d1af2SArnaldo Carvalho de Melo 65361c99a6SArnaldo Carvalho de Melo return evlist; 66361c99a6SArnaldo Carvalho de Melo } 67361c99a6SArnaldo Carvalho de Melo 68b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 69b22d54b0SJiri Olsa { 70b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 71b22d54b0SJiri Olsa 72b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 73b22d54b0SJiri Olsa perf_evlist__delete(evlist); 74b22d54b0SJiri Olsa evlist = NULL; 75b22d54b0SJiri Olsa } 76b22d54b0SJiri Olsa 77b22d54b0SJiri Olsa return evlist; 78b22d54b0SJiri Olsa } 79b22d54b0SJiri Olsa 805bae0250SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new_dummy(void) 815bae0250SArnaldo Carvalho de Melo { 825bae0250SArnaldo Carvalho de Melo struct perf_evlist *evlist = perf_evlist__new(); 835bae0250SArnaldo Carvalho de Melo 845bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 855bae0250SArnaldo Carvalho de Melo perf_evlist__delete(evlist); 865bae0250SArnaldo Carvalho de Melo evlist = NULL; 875bae0250SArnaldo Carvalho de Melo } 885bae0250SArnaldo Carvalho de Melo 895bae0250SArnaldo Carvalho de Melo return evlist; 905bae0250SArnaldo Carvalho de Melo } 915bae0250SArnaldo Carvalho de Melo 9275562573SAdrian Hunter /** 9375562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 9475562573SAdrian Hunter * @evlist: selected event list 9575562573SAdrian Hunter * 9675562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 9775562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 9875562573SAdrian Hunter */ 9975562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 10075562573SAdrian Hunter { 10175562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 10275562573SAdrian Hunter 10375562573SAdrian Hunter evlist->id_pos = first->id_pos; 10475562573SAdrian Hunter evlist->is_pos = first->is_pos; 10575562573SAdrian Hunter } 10675562573SAdrian Hunter 107733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 108733cd2feSAdrian Hunter { 109733cd2feSAdrian Hunter struct perf_evsel *evsel; 110733cd2feSAdrian Hunter 111e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 112733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 113733cd2feSAdrian Hunter 114733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 115733cd2feSAdrian Hunter } 116733cd2feSAdrian Hunter 117361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 118361c99a6SArnaldo Carvalho de Melo { 119361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 120361c99a6SArnaldo Carvalho de Melo 121e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 122361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 123d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 124361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 125361c99a6SArnaldo Carvalho de Melo } 126361c99a6SArnaldo Carvalho de Melo 127361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 128361c99a6SArnaldo Carvalho de Melo } 129361c99a6SArnaldo Carvalho de Melo 130ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 131ef1d1af2SArnaldo Carvalho de Melo { 13204662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1330b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 1341b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 135ef1d1af2SArnaldo Carvalho de Melo } 136ef1d1af2SArnaldo Carvalho de Melo 137361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 138361c99a6SArnaldo Carvalho de Melo { 1390b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1400b04b3dcSArnaldo Carvalho de Melo return; 1410b04b3dcSArnaldo Carvalho de Melo 142983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 143f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 144f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 145186fbb74SJiri Olsa thread_map__put(evlist->threads); 14603ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 14703ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 148361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 149ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 150361c99a6SArnaldo Carvalho de Melo free(evlist); 151361c99a6SArnaldo Carvalho de Melo } 152361c99a6SArnaldo Carvalho de Melo 153adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, 154adc0c3e8SAdrian Hunter struct perf_evsel *evsel) 155adc0c3e8SAdrian Hunter { 156adc0c3e8SAdrian Hunter /* 157adc0c3e8SAdrian Hunter * We already have cpus for evsel (via PMU sysfs) so 158adc0c3e8SAdrian Hunter * keep it, if there's no target cpu list defined. 159adc0c3e8SAdrian Hunter */ 160adc0c3e8SAdrian Hunter if (!evsel->own_cpus || evlist->has_user_cpus) { 161adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 162adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evlist->cpus); 163adc0c3e8SAdrian Hunter } else if (evsel->cpus != evsel->own_cpus) { 164adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 165adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evsel->own_cpus); 166adc0c3e8SAdrian Hunter } 167adc0c3e8SAdrian Hunter 168adc0c3e8SAdrian Hunter thread_map__put(evsel->threads); 169adc0c3e8SAdrian Hunter evsel->threads = thread_map__get(evlist->threads); 170adc0c3e8SAdrian Hunter } 171adc0c3e8SAdrian Hunter 172adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist) 173adc0c3e8SAdrian Hunter { 174adc0c3e8SAdrian Hunter struct perf_evsel *evsel; 175adc0c3e8SAdrian Hunter 176e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 177adc0c3e8SAdrian Hunter __perf_evlist__propagate_maps(evlist, evsel); 178adc0c3e8SAdrian Hunter } 179adc0c3e8SAdrian Hunter 180361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 181361c99a6SArnaldo Carvalho de Melo { 182d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 183361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 184ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 18560b0896cSAdrian Hunter entry->tracking = !entry->idx; 186ef503831SArnaldo Carvalho de Melo 18775562573SAdrian Hunter if (!evlist->nr_entries++) 18875562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 18944c42d71SAdrian Hunter 19044c42d71SAdrian Hunter __perf_evlist__propagate_maps(evlist, entry); 191361c99a6SArnaldo Carvalho de Melo } 192361c99a6SArnaldo Carvalho de Melo 1934768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel) 1944768230aSAdrian Hunter { 1954768230aSAdrian Hunter evsel->evlist = NULL; 1964768230aSAdrian Hunter list_del_init(&evsel->node); 1974768230aSAdrian Hunter evlist->nr_entries -= 1; 1984768230aSAdrian Hunter } 1994768230aSAdrian Hunter 2000529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 201f114d6efSAdrian Hunter struct list_head *list) 20250d08e47SArnaldo Carvalho de Melo { 203f114d6efSAdrian Hunter struct perf_evsel *evsel, *temp; 20475562573SAdrian Hunter 205e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 206f114d6efSAdrian Hunter list_del_init(&evsel->node); 207f114d6efSAdrian Hunter perf_evlist__add(evlist, evsel); 208f114d6efSAdrian Hunter } 20950d08e47SArnaldo Carvalho de Melo } 21050d08e47SArnaldo Carvalho de Melo 21163dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 21263dab225SArnaldo Carvalho de Melo { 21363dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 21463dab225SArnaldo Carvalho de Melo 21563dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 21697f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 21797f63e4aSNamhyung Kim 21897f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 21963dab225SArnaldo Carvalho de Melo 220e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 22163dab225SArnaldo Carvalho de Melo evsel->leader = leader; 22263dab225SArnaldo Carvalho de Melo } 22363dab225SArnaldo Carvalho de Melo } 22463dab225SArnaldo Carvalho de Melo 22563dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 2266a4bb04cSJiri Olsa { 22797f63e4aSNamhyung Kim if (evlist->nr_entries) { 22897f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 22963dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 2306a4bb04cSJiri Olsa } 23197f63e4aSNamhyung Kim } 2326a4bb04cSJiri Olsa 23345cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr) 2347f8d1adeSArnaldo Carvalho de Melo { 2357f8d1adeSArnaldo Carvalho de Melo attr->precise_ip = 3; 2367f8d1adeSArnaldo Carvalho de Melo 2377f8d1adeSArnaldo Carvalho de Melo while (attr->precise_ip != 0) { 2387f8d1adeSArnaldo Carvalho de Melo int fd = sys_perf_event_open(attr, 0, -1, -1, 0); 2397f8d1adeSArnaldo Carvalho de Melo if (fd != -1) { 2407f8d1adeSArnaldo Carvalho de Melo close(fd); 2417f8d1adeSArnaldo Carvalho de Melo break; 2427f8d1adeSArnaldo Carvalho de Melo } 2437f8d1adeSArnaldo Carvalho de Melo --attr->precise_ip; 2447f8d1adeSArnaldo Carvalho de Melo } 2457f8d1adeSArnaldo Carvalho de Melo } 2467f8d1adeSArnaldo Carvalho de Melo 247db918acbSArnaldo Carvalho de Melo int __perf_evlist__add_default(struct perf_evlist *evlist, bool precise) 248361c99a6SArnaldo Carvalho de Melo { 249db918acbSArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new_cycles(precise); 250361c99a6SArnaldo Carvalho de Melo 251361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2527c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 253361c99a6SArnaldo Carvalho de Melo 254361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 255361c99a6SArnaldo Carvalho de Melo return 0; 256361c99a6SArnaldo Carvalho de Melo } 2575c581041SArnaldo Carvalho de Melo 2585bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist) 2595bae0250SArnaldo Carvalho de Melo { 2605bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2615bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2625bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2635bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2645bae0250SArnaldo Carvalho de Melo }; 265640d5175SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new_idx(&attr, evlist->nr_entries); 2665bae0250SArnaldo Carvalho de Melo 2675bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2685bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2695bae0250SArnaldo Carvalho de Melo 2705bae0250SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 2715bae0250SArnaldo Carvalho de Melo return 0; 2725bae0250SArnaldo Carvalho de Melo } 2735bae0250SArnaldo Carvalho de Melo 274e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 27550d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 27650d08e47SArnaldo Carvalho de Melo { 27750d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 27850d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 27950d08e47SArnaldo Carvalho de Melo size_t i; 28050d08e47SArnaldo Carvalho de Melo 28150d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 282ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 28350d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 28450d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 28550d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 28650d08e47SArnaldo Carvalho de Melo } 28750d08e47SArnaldo Carvalho de Melo 288f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 28950d08e47SArnaldo Carvalho de Melo 29050d08e47SArnaldo Carvalho de Melo return 0; 29150d08e47SArnaldo Carvalho de Melo 29250d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 293e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 29450d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 29550d08e47SArnaldo Carvalho de Melo return -1; 29650d08e47SArnaldo Carvalho de Melo } 29750d08e47SArnaldo Carvalho de Melo 29879695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 29979695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 30079695e1bSArnaldo Carvalho de Melo { 30179695e1bSArnaldo Carvalho de Melo size_t i; 30279695e1bSArnaldo Carvalho de Melo 30379695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 30479695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 30579695e1bSArnaldo Carvalho de Melo 30679695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 30779695e1bSArnaldo Carvalho de Melo } 30879695e1bSArnaldo Carvalho de Melo 309da378962SArnaldo Carvalho de Melo struct perf_evsel * 310ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 311ee29be62SArnaldo Carvalho de Melo { 312ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 313ee29be62SArnaldo Carvalho de Melo 314e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 315ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 316ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 317ee29be62SArnaldo Carvalho de Melo return evsel; 318ee29be62SArnaldo Carvalho de Melo } 319ee29be62SArnaldo Carvalho de Melo 320ee29be62SArnaldo Carvalho de Melo return NULL; 321ee29be62SArnaldo Carvalho de Melo } 322ee29be62SArnaldo Carvalho de Melo 323a2f2804aSDavid Ahern struct perf_evsel * 324a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 325a2f2804aSDavid Ahern const char *name) 326a2f2804aSDavid Ahern { 327a2f2804aSDavid Ahern struct perf_evsel *evsel; 328a2f2804aSDavid Ahern 329e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 330a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 331a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 332a2f2804aSDavid Ahern return evsel; 333a2f2804aSDavid Ahern } 334a2f2804aSDavid Ahern 335a2f2804aSDavid Ahern return NULL; 336a2f2804aSDavid Ahern } 337a2f2804aSDavid Ahern 33839876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 33939876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 34039876e7dSArnaldo Carvalho de Melo { 341ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 34239876e7dSArnaldo Carvalho de Melo 3438dd2a131SJiri Olsa if (IS_ERR(evsel)) 34439876e7dSArnaldo Carvalho de Melo return -1; 34539876e7dSArnaldo Carvalho de Melo 346744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 34739876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 34839876e7dSArnaldo Carvalho de Melo return 0; 34939876e7dSArnaldo Carvalho de Melo } 35039876e7dSArnaldo Carvalho de Melo 351bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 352bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 353bf8e8f4bSAdrian Hunter { 354bf8e8f4bSAdrian Hunter if (evsel->system_wide) 355bf8e8f4bSAdrian Hunter return 1; 356bf8e8f4bSAdrian Hunter else 357bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 358bf8e8f4bSAdrian Hunter } 359bf8e8f4bSAdrian Hunter 3604152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 3614152ab37SArnaldo Carvalho de Melo { 3624152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 3634152ab37SArnaldo Carvalho de Melo 364e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 365b7e8452bSArnaldo Carvalho de Melo if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->fd) 3663fe4430dSJiri Olsa continue; 3673e27c920SJiri Olsa perf_evsel__disable(pos); 3684152ab37SArnaldo Carvalho de Melo } 3692b56bcfbSArnaldo Carvalho de Melo 3702b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3714152ab37SArnaldo Carvalho de Melo } 3724152ab37SArnaldo Carvalho de Melo 373764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 374764e16a3SDavid Ahern { 375764e16a3SDavid Ahern struct perf_evsel *pos; 376764e16a3SDavid Ahern 377e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 378395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3793fe4430dSJiri Olsa continue; 3803e27c920SJiri Olsa perf_evsel__enable(pos); 381764e16a3SDavid Ahern } 3822b56bcfbSArnaldo Carvalho de Melo 3832b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3842b56bcfbSArnaldo Carvalho de Melo } 3852b56bcfbSArnaldo Carvalho de Melo 3862b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist) 3872b56bcfbSArnaldo Carvalho de Melo { 3882b56bcfbSArnaldo Carvalho de Melo (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); 389764e16a3SDavid Ahern } 390764e16a3SDavid Ahern 3911c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3921c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3931c65056cSAdrian Hunter { 39418ef15c6SArnaldo Carvalho de Melo int thread; 3951c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3961c65056cSAdrian Hunter 3971c65056cSAdrian Hunter if (!evsel->fd) 3981c65056cSAdrian Hunter return -EINVAL; 3991c65056cSAdrian Hunter 4001c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 40118ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4021c65056cSAdrian Hunter if (err) 4031c65056cSAdrian Hunter return err; 4041c65056cSAdrian Hunter } 4051c65056cSAdrian Hunter return 0; 4061c65056cSAdrian Hunter } 4071c65056cSAdrian Hunter 4081c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 4091c65056cSAdrian Hunter struct perf_evsel *evsel, 4101c65056cSAdrian Hunter int thread) 4111c65056cSAdrian Hunter { 41218ef15c6SArnaldo Carvalho de Melo int cpu; 4131c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 4141c65056cSAdrian Hunter 4151c65056cSAdrian Hunter if (!evsel->fd) 4161c65056cSAdrian Hunter return -EINVAL; 4171c65056cSAdrian Hunter 4181c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 41918ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4201c65056cSAdrian Hunter if (err) 4211c65056cSAdrian Hunter return err; 4221c65056cSAdrian Hunter } 4231c65056cSAdrian Hunter return 0; 4241c65056cSAdrian Hunter } 4251c65056cSAdrian Hunter 4261c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4271c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4281c65056cSAdrian Hunter { 4291c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4301c65056cSAdrian Hunter 4311c65056cSAdrian Hunter if (per_cpu_mmaps) 4321c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4331c65056cSAdrian Hunter else 4341c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4351c65056cSAdrian Hunter } 4361c65056cSAdrian Hunter 437ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4385c581041SArnaldo Carvalho de Melo { 439b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 440b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 441bf8e8f4bSAdrian Hunter int nfds = 0; 442bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 443bf8e8f4bSAdrian Hunter 444e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 445bf8e8f4bSAdrian Hunter if (evsel->system_wide) 446bf8e8f4bSAdrian Hunter nfds += nr_cpus; 447bf8e8f4bSAdrian Hunter else 448bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 449bf8e8f4bSAdrian Hunter } 450bf8e8f4bSAdrian Hunter 4511b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4521b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 453ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 454ad6765ddSArnaldo Carvalho de Melo 455ad6765ddSArnaldo Carvalho de Melo return 0; 4565c581041SArnaldo Carvalho de Melo } 45770082dd9SArnaldo Carvalho de Melo 4584876075bSWang Nan static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, 4594876075bSWang Nan struct perf_mmap *map, short revent) 460e4b356b5SArnaldo Carvalho de Melo { 461f3058a1cSWang Nan int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); 462e4b356b5SArnaldo Carvalho de Melo /* 463e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 464e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 465e4b356b5SArnaldo Carvalho de Melo */ 466e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 4674876075bSWang Nan evlist->pollfd.priv[pos].ptr = map; 468e4b356b5SArnaldo Carvalho de Melo 469e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 470e4b356b5SArnaldo Carvalho de Melo } 471e4b356b5SArnaldo Carvalho de Melo 472e4b356b5SArnaldo Carvalho de Melo return pos; 473e4b356b5SArnaldo Carvalho de Melo } 474e4b356b5SArnaldo Carvalho de Melo 475ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 47670082dd9SArnaldo Carvalho de Melo { 4774876075bSWang Nan return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN); 478e4b356b5SArnaldo Carvalho de Melo } 4791b85337dSArnaldo Carvalho de Melo 480258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 481258e4bfcSWang Nan void *arg __maybe_unused) 482e4b356b5SArnaldo Carvalho de Melo { 4834876075bSWang Nan struct perf_mmap *map = fda->priv[fd].ptr; 484e4b356b5SArnaldo Carvalho de Melo 4854876075bSWang Nan if (map) 4864876075bSWang Nan perf_mmap__put(map); 48770082dd9SArnaldo Carvalho de Melo } 48870db7533SArnaldo Carvalho de Melo 4891ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4901ddec7f0SArnaldo Carvalho de Melo { 491e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 492258e4bfcSWang Nan perf_evlist__munmap_filtered, NULL); 4931ddec7f0SArnaldo Carvalho de Melo } 4941ddec7f0SArnaldo Carvalho de Melo 495f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 496f66a889dSArnaldo Carvalho de Melo { 4971b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 498f66a889dSArnaldo Carvalho de Melo } 499f66a889dSArnaldo Carvalho de Melo 500a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 501a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 5023d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 5033d3b5e95SArnaldo Carvalho de Melo { 5043d3b5e95SArnaldo Carvalho de Melo int hash; 5053d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 5063d3b5e95SArnaldo Carvalho de Melo 5073d3b5e95SArnaldo Carvalho de Melo sid->id = id; 5083d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 5093d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 5103d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 5113d3b5e95SArnaldo Carvalho de Melo } 5123d3b5e95SArnaldo Carvalho de Melo 513a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 514a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 515a91e5431SArnaldo Carvalho de Melo { 516a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 517a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 518a91e5431SArnaldo Carvalho de Melo } 519a91e5431SArnaldo Carvalho de Melo 5201c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist, 5213d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 522f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 523f8a95309SArnaldo Carvalho de Melo { 524f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5253d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 526e2b5abe0SJiri Olsa u64 id; 527e2b5abe0SJiri Olsa int ret; 528e2b5abe0SJiri Olsa 529e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 530e2b5abe0SJiri Olsa if (!ret) 531e2b5abe0SJiri Olsa goto add; 532e2b5abe0SJiri Olsa 533e2b5abe0SJiri Olsa if (errno != ENOTTY) 534e2b5abe0SJiri Olsa return -1; 535e2b5abe0SJiri Olsa 536e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 537f8a95309SArnaldo Carvalho de Melo 538c4861afeSJiri Olsa /* 539c4861afeSJiri Olsa * This way does not work with group format read, so bail 540c4861afeSJiri Olsa * out in that case. 541c4861afeSJiri Olsa */ 542c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 543c4861afeSJiri Olsa return -1; 544c4861afeSJiri Olsa 545f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 546f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 547f8a95309SArnaldo Carvalho de Melo return -1; 548f8a95309SArnaldo Carvalho de Melo 549f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 550f8a95309SArnaldo Carvalho de Melo ++id_idx; 551f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 552f8a95309SArnaldo Carvalho de Melo ++id_idx; 553f8a95309SArnaldo Carvalho de Melo 554e2b5abe0SJiri Olsa id = read_data[id_idx]; 555e2b5abe0SJiri Olsa 556e2b5abe0SJiri Olsa add: 557e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 558f8a95309SArnaldo Carvalho de Melo return 0; 559f8a95309SArnaldo Carvalho de Melo } 560f8a95309SArnaldo Carvalho de Melo 5613c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5623c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5633c659eedSAdrian Hunter int thread) 5643c659eedSAdrian Hunter { 5653c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5663c659eedSAdrian Hunter sid->idx = idx; 5673c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5683c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5693c659eedSAdrian Hunter else 5703c659eedSAdrian Hunter sid->cpu = -1; 5713c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 572e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5733c659eedSAdrian Hunter else 5743c659eedSAdrian Hunter sid->tid = -1; 5753c659eedSAdrian Hunter } 5763c659eedSAdrian Hunter 577932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 57870db7533SArnaldo Carvalho de Melo { 57970db7533SArnaldo Carvalho de Melo struct hlist_head *head; 58070db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 58170db7533SArnaldo Carvalho de Melo int hash; 58270db7533SArnaldo Carvalho de Melo 58370db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 58470db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 58570db7533SArnaldo Carvalho de Melo 586b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 58770db7533SArnaldo Carvalho de Melo if (sid->id == id) 588932a3594SJiri Olsa return sid; 589932a3594SJiri Olsa 590932a3594SJiri Olsa return NULL; 591932a3594SJiri Olsa } 592932a3594SJiri Olsa 593932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 594932a3594SJiri Olsa { 595932a3594SJiri Olsa struct perf_sample_id *sid; 596932a3594SJiri Olsa 59705169df5SAdrian Hunter if (evlist->nr_entries == 1 || !id) 598932a3594SJiri Olsa return perf_evlist__first(evlist); 599932a3594SJiri Olsa 600932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 601932a3594SJiri Olsa if (sid) 60270db7533SArnaldo Carvalho de Melo return sid->evsel; 60330e68bccSNamhyung Kim 60430e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 6050c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 60630e68bccSNamhyung Kim 60770db7533SArnaldo Carvalho de Melo return NULL; 60870db7533SArnaldo Carvalho de Melo } 60904391debSArnaldo Carvalho de Melo 610dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist, 611dddcf6abSAdrian Hunter u64 id) 612dddcf6abSAdrian Hunter { 613dddcf6abSAdrian Hunter struct perf_sample_id *sid; 614dddcf6abSAdrian Hunter 615dddcf6abSAdrian Hunter if (!id) 616dddcf6abSAdrian Hunter return NULL; 617dddcf6abSAdrian Hunter 618dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 619dddcf6abSAdrian Hunter if (sid) 620dddcf6abSAdrian Hunter return sid->evsel; 621dddcf6abSAdrian Hunter 622dddcf6abSAdrian Hunter return NULL; 623dddcf6abSAdrian Hunter } 624dddcf6abSAdrian Hunter 62575562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 62675562573SAdrian Hunter union perf_event *event, u64 *id) 62775562573SAdrian Hunter { 62875562573SAdrian Hunter const u64 *array = event->sample.array; 62975562573SAdrian Hunter ssize_t n; 63075562573SAdrian Hunter 63175562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 63275562573SAdrian Hunter 63375562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 63475562573SAdrian Hunter if (evlist->id_pos >= n) 63575562573SAdrian Hunter return -1; 63675562573SAdrian Hunter *id = array[evlist->id_pos]; 63775562573SAdrian Hunter } else { 63875562573SAdrian Hunter if (evlist->is_pos > n) 63975562573SAdrian Hunter return -1; 64075562573SAdrian Hunter n -= evlist->is_pos; 64175562573SAdrian Hunter *id = array[n]; 64275562573SAdrian Hunter } 64375562573SAdrian Hunter return 0; 64475562573SAdrian Hunter } 64575562573SAdrian Hunter 6467cb5c5acSJiri Olsa struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 64775562573SAdrian Hunter union perf_event *event) 64875562573SAdrian Hunter { 64998be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 65075562573SAdrian Hunter struct hlist_head *head; 65175562573SAdrian Hunter struct perf_sample_id *sid; 65275562573SAdrian Hunter int hash; 65375562573SAdrian Hunter u64 id; 65475562573SAdrian Hunter 65575562573SAdrian Hunter if (evlist->nr_entries == 1) 65698be6966SAdrian Hunter return first; 65798be6966SAdrian Hunter 65898be6966SAdrian Hunter if (!first->attr.sample_id_all && 65998be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 66098be6966SAdrian Hunter return first; 66175562573SAdrian Hunter 66275562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 66375562573SAdrian Hunter return NULL; 66475562573SAdrian Hunter 66575562573SAdrian Hunter /* Synthesized events have an id of zero */ 66675562573SAdrian Hunter if (!id) 66798be6966SAdrian Hunter return first; 66875562573SAdrian Hunter 66975562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 67075562573SAdrian Hunter head = &evlist->heads[hash]; 67175562573SAdrian Hunter 67275562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 67375562573SAdrian Hunter if (sid->id == id) 67475562573SAdrian Hunter return sid->evsel; 67575562573SAdrian Hunter } 67675562573SAdrian Hunter return NULL; 67775562573SAdrian Hunter } 67875562573SAdrian Hunter 67965aea233SWang Nan static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value) 68065aea233SWang Nan { 68165aea233SWang Nan int i; 68265aea233SWang Nan 6830b72d69aSWang Nan if (!evlist->overwrite_mmap) 684078c3386SWang Nan return 0; 685078c3386SWang Nan 68665aea233SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 6870b72d69aSWang Nan int fd = evlist->overwrite_mmap[i].fd; 68865aea233SWang Nan int err; 68965aea233SWang Nan 69065aea233SWang Nan if (fd < 0) 69165aea233SWang Nan continue; 69265aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 69365aea233SWang Nan if (err) 69465aea233SWang Nan return err; 69565aea233SWang Nan } 69665aea233SWang Nan return 0; 69765aea233SWang Nan } 69865aea233SWang Nan 699f6cdff83SWang Nan static int perf_evlist__pause(struct perf_evlist *evlist) 70065aea233SWang Nan { 70165aea233SWang Nan return perf_evlist__set_paused(evlist, true); 70265aea233SWang Nan } 70365aea233SWang Nan 704f6cdff83SWang Nan static int perf_evlist__resume(struct perf_evlist *evlist) 70565aea233SWang Nan { 70665aea233SWang Nan return perf_evlist__set_paused(evlist, false); 70765aea233SWang Nan } 70865aea233SWang Nan 709a1f72618SWang Nan static void perf_evlist__munmap_nofree(struct perf_evlist *evlist) 710f8a95309SArnaldo Carvalho de Melo { 711aece948fSArnaldo Carvalho de Melo int i; 712f8a95309SArnaldo Carvalho de Melo 713b2cb615dSWang Nan if (evlist->mmap) 71493edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 715a1f72618SWang Nan perf_mmap__munmap(&evlist->mmap[i]); 716b2cb615dSWang Nan 7170b72d69aSWang Nan if (evlist->overwrite_mmap) 718b2cb615dSWang Nan for (i = 0; i < evlist->nr_mmaps; i++) 7190b72d69aSWang Nan perf_mmap__munmap(&evlist->overwrite_mmap[i]); 720a1f72618SWang Nan } 721aece948fSArnaldo Carvalho de Melo 722a1f72618SWang Nan void perf_evlist__munmap(struct perf_evlist *evlist) 723a1f72618SWang Nan { 724a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 72504662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 7260b72d69aSWang Nan zfree(&evlist->overwrite_mmap); 727f8a95309SArnaldo Carvalho de Melo } 728f8a95309SArnaldo Carvalho de Melo 7292c5f6d87SKan Liang static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist, 7302c5f6d87SKan Liang bool overwrite) 731f8a95309SArnaldo Carvalho de Melo { 732d4c6fb36SWang Nan int i; 7338db6d6b1SWang Nan struct perf_mmap *map; 734d4c6fb36SWang Nan 735a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 736ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 737b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 7388db6d6b1SWang Nan map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 7398db6d6b1SWang Nan if (!map) 7408db6d6b1SWang Nan return NULL; 741946ae1d4SWang Nan 7424738ca30SArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 7438db6d6b1SWang Nan map[i].fd = -1; 7442c5f6d87SKan Liang map[i].overwrite = overwrite; 7454738ca30SArnaldo Carvalho de Melo /* 7464738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 7476afad54dSKan Liang * one extra to let perf_mmap__consume() get the last 7484738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 7494738ca30SArnaldo Carvalho de Melo * dropped. 7504738ca30SArnaldo Carvalho de Melo * 7514738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 7524738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 7534738ca30SArnaldo Carvalho de Melo */ 7544738ca30SArnaldo Carvalho de Melo refcount_set(&map[i].refcnt, 0); 7554738ca30SArnaldo Carvalho de Melo } 7568db6d6b1SWang Nan return map; 757f8a95309SArnaldo Carvalho de Melo } 758f8a95309SArnaldo Carvalho de Melo 759f3058a1cSWang Nan static bool 760f3058a1cSWang Nan perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused, 761f3058a1cSWang Nan struct perf_evsel *evsel) 762f3058a1cSWang Nan { 76332a951b4SArnaldo Carvalho de Melo if (evsel->attr.write_backward) 764f3058a1cSWang Nan return false; 765f3058a1cSWang Nan return true; 766f3058a1cSWang Nan } 767f3058a1cSWang Nan 76804e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 7699f21b815SMark Rutland struct mmap_params *mp, int cpu_idx, 7700b72d69aSWang Nan int thread, int *_output, int *_output_overwrite) 771aece948fSArnaldo Carvalho de Melo { 772aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 773f3058a1cSWang Nan int revent; 7749f21b815SMark Rutland int evlist_cpu = cpu_map__cpu(evlist->cpus, cpu_idx); 77504e21314SAdrian Hunter 776e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 777078c3386SWang Nan struct perf_mmap *maps = evlist->mmap; 778078c3386SWang Nan int *output = _output; 779bf8e8f4bSAdrian Hunter int fd; 7809f21b815SMark Rutland int cpu; 781bf8e8f4bSAdrian Hunter 78271f566a3SWang Nan mp->prot = PROT_READ | PROT_WRITE; 783078c3386SWang Nan if (evsel->attr.write_backward) { 7840b72d69aSWang Nan output = _output_overwrite; 7850b72d69aSWang Nan maps = evlist->overwrite_mmap; 786078c3386SWang Nan 787078c3386SWang Nan if (!maps) { 7882c5f6d87SKan Liang maps = perf_evlist__alloc_mmap(evlist, true); 789078c3386SWang Nan if (!maps) 790078c3386SWang Nan return -1; 7910b72d69aSWang Nan evlist->overwrite_mmap = maps; 79254cc54deSWang Nan if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 79354cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 794078c3386SWang Nan } 79571f566a3SWang Nan mp->prot &= ~PROT_WRITE; 796078c3386SWang Nan } 797f3058a1cSWang Nan 798bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 799bf8e8f4bSAdrian Hunter continue; 800bf8e8f4bSAdrian Hunter 8019f21b815SMark Rutland cpu = cpu_map__idx(evsel->cpus, evlist_cpu); 8029f21b815SMark Rutland if (cpu == -1) 8039f21b815SMark Rutland continue; 8049f21b815SMark Rutland 805bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 80604e21314SAdrian Hunter 80704e21314SAdrian Hunter if (*output == -1) { 80804e21314SAdrian Hunter *output = fd; 809078c3386SWang Nan 81031fb4c0dSJiri Olsa if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0) 81104e21314SAdrian Hunter return -1; 81204e21314SAdrian Hunter } else { 81304e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 81404e21314SAdrian Hunter return -1; 81582396986SArnaldo Carvalho de Melo 816078c3386SWang Nan perf_mmap__get(&maps[idx]); 81704e21314SAdrian Hunter } 81804e21314SAdrian Hunter 819f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 820f3058a1cSWang Nan 821f90d194aSAdrian Hunter /* 822f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 823f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 824f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 825f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 826f90d194aSAdrian Hunter * Therefore don't add it for polling. 827f90d194aSAdrian Hunter */ 828f90d194aSAdrian Hunter if (!evsel->system_wide && 829078c3386SWang Nan __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) { 830078c3386SWang Nan perf_mmap__put(&maps[idx]); 831ad6765ddSArnaldo Carvalho de Melo return -1; 83282396986SArnaldo Carvalho de Melo } 833033fa713SArnaldo Carvalho de Melo 8343c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 8353c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 8363c659eedSAdrian Hunter fd) < 0) 83704e21314SAdrian Hunter return -1; 8383c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 8393c659eedSAdrian Hunter thread); 8403c659eedSAdrian Hunter } 84104e21314SAdrian Hunter } 84204e21314SAdrian Hunter 84304e21314SAdrian Hunter return 0; 84404e21314SAdrian Hunter } 84504e21314SAdrian Hunter 846a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 847a8a8f3ebSAdrian Hunter struct mmap_params *mp) 84804e21314SAdrian Hunter { 849aece948fSArnaldo Carvalho de Melo int cpu, thread; 850b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 851b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 852aece948fSArnaldo Carvalho de Melo 853e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 854b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 855aece948fSArnaldo Carvalho de Melo int output = -1; 8560b72d69aSWang Nan int output_overwrite = -1; 857aece948fSArnaldo Carvalho de Melo 858718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 859718c602dSAdrian Hunter true); 860718c602dSAdrian Hunter 861b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 862a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 8630b72d69aSWang Nan thread, &output, &output_overwrite)) 864aece948fSArnaldo Carvalho de Melo goto out_unmap; 865aece948fSArnaldo Carvalho de Melo } 866aece948fSArnaldo Carvalho de Melo } 867aece948fSArnaldo Carvalho de Melo 868aece948fSArnaldo Carvalho de Melo return 0; 869aece948fSArnaldo Carvalho de Melo 870aece948fSArnaldo Carvalho de Melo out_unmap: 871a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 872aece948fSArnaldo Carvalho de Melo return -1; 873aece948fSArnaldo Carvalho de Melo } 874aece948fSArnaldo Carvalho de Melo 875a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 876a8a8f3ebSAdrian Hunter struct mmap_params *mp) 877aece948fSArnaldo Carvalho de Melo { 878aece948fSArnaldo Carvalho de Melo int thread; 879b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 880aece948fSArnaldo Carvalho de Melo 881e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 882b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 883aece948fSArnaldo Carvalho de Melo int output = -1; 8840b72d69aSWang Nan int output_overwrite = -1; 885aece948fSArnaldo Carvalho de Melo 886718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 887718c602dSAdrian Hunter false); 888718c602dSAdrian Hunter 889a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 8900b72d69aSWang Nan &output, &output_overwrite)) 891aece948fSArnaldo Carvalho de Melo goto out_unmap; 892aece948fSArnaldo Carvalho de Melo } 893aece948fSArnaldo Carvalho de Melo 894aece948fSArnaldo Carvalho de Melo return 0; 895aece948fSArnaldo Carvalho de Melo 896aece948fSArnaldo Carvalho de Melo out_unmap: 897a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 898aece948fSArnaldo Carvalho de Melo return -1; 899aece948fSArnaldo Carvalho de Melo } 900aece948fSArnaldo Carvalho de Melo 901f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 902994a1f78SJiri Olsa { 903f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 9048185e881SArnaldo Carvalho de Melo int max; 9058185e881SArnaldo Carvalho de Melo 9068185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 9078185e881SArnaldo Carvalho de Melo /* 9088185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 9098185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 9108185e881SArnaldo Carvalho de Melo * die yet... 9118185e881SArnaldo Carvalho de Melo */ 9128185e881SArnaldo Carvalho de Melo max = 512; 9138185e881SArnaldo Carvalho de Melo } else { 9148185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 9158185e881SArnaldo Carvalho de Melo } 9168185e881SArnaldo Carvalho de Melo 9178185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 9181be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 9191be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 920f5e7150cSArnaldo Carvalho de Melo 921f5e7150cSArnaldo Carvalho de Melo return pages; 922f5e7150cSArnaldo Carvalho de Melo } 923f5e7150cSArnaldo Carvalho de Melo 9240c582449SJiri Olsa size_t perf_evlist__mmap_size(unsigned long pages) 925f5e7150cSArnaldo Carvalho de Melo { 926f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 927f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 928f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 929994a1f78SJiri Olsa return 0; 930994a1f78SJiri Olsa 931994a1f78SJiri Olsa return (pages + 1) * page_size; 932994a1f78SJiri Olsa } 933994a1f78SJiri Olsa 93433c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 93533c2dcfdSDavid Ahern unsigned long max) 936994a1f78SJiri Olsa { 9372fbe4abeSAdrian Hunter unsigned long pages, val; 93827050f53SJiri Olsa static struct parse_tag tags[] = { 93927050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 94027050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 94127050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 94227050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 94327050f53SJiri Olsa { .tag = 0 }, 94427050f53SJiri Olsa }; 945994a1f78SJiri Olsa 9468973504bSDavid Ahern if (str == NULL) 94733c2dcfdSDavid Ahern return -EINVAL; 9488973504bSDavid Ahern 94927050f53SJiri Olsa val = parse_tag_value(str, tags); 9502fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 95127050f53SJiri Olsa /* we got file size value */ 95227050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 95327050f53SJiri Olsa } else { 95427050f53SJiri Olsa /* we got pages count value */ 95527050f53SJiri Olsa char *eptr; 956994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 95733c2dcfdSDavid Ahern if (*eptr != '\0') 95833c2dcfdSDavid Ahern return -EINVAL; 95927050f53SJiri Olsa } 960994a1f78SJiri Olsa 9612bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 96233c2dcfdSDavid Ahern /* leave number of pages at 0 */ 9631dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 9649808143bSJiri Olsa char buf[100]; 9659808143bSJiri Olsa 96633c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 96791529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 9681dbfa938SAdrian Hunter if (!pages) 9691dbfa938SAdrian Hunter return -EINVAL; 9709808143bSJiri Olsa 9719808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 9729808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 9739808143bSJiri Olsa buf, pages); 9742fbe4abeSAdrian Hunter } 9752fbe4abeSAdrian Hunter 97633c2dcfdSDavid Ahern if (pages > max) 97733c2dcfdSDavid Ahern return -EINVAL; 97833c2dcfdSDavid Ahern 97933c2dcfdSDavid Ahern return pages; 98033c2dcfdSDavid Ahern } 98133c2dcfdSDavid Ahern 982e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 98333c2dcfdSDavid Ahern { 98433c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 98533c2dcfdSDavid Ahern long pages; 98633c2dcfdSDavid Ahern 987f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 98833c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 98933c2dcfdSDavid Ahern 99033c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 99133c2dcfdSDavid Ahern if (pages < 0) { 99233c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 993994a1f78SJiri Olsa return -1; 994994a1f78SJiri Olsa } 995994a1f78SJiri Olsa 996994a1f78SJiri Olsa *mmap_pages = pages; 997994a1f78SJiri Olsa return 0; 998994a1f78SJiri Olsa } 999994a1f78SJiri Olsa 1000e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 1001e9db1310SAdrian Hunter int unset __maybe_unused) 1002e9db1310SAdrian Hunter { 1003e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 1004e9db1310SAdrian Hunter } 1005e9db1310SAdrian Hunter 1006c83fa7f2SAdrian Hunter /** 1007718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 1008c83fa7f2SAdrian Hunter * @evlist: list of events 1009c83fa7f2SAdrian Hunter * @pages: map length in pages 1010c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 1011718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 1012718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1013f8a95309SArnaldo Carvalho de Melo * 1014c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1015c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1016c83fa7f2SAdrian Hunter * automatically. 1017f8a95309SArnaldo Carvalho de Melo * 1018718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1019718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1020718c602dSAdrian Hunter * 1021c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1022f8a95309SArnaldo Carvalho de Melo */ 1023718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, 10247a276ff6SWang Nan unsigned int auxtrace_pages, 1025d3d1af6fSAlexey Budankov bool auxtrace_overwrite, int nr_cblocks) 1026f8a95309SArnaldo Carvalho de Melo { 1027aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 10287e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 10297e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 103071f566a3SWang Nan /* 103171f566a3SWang Nan * Delay setting mp.prot: set it before calling perf_mmap__mmap. 103271f566a3SWang Nan * Its value is decided by evsel's write_backward. 103371f566a3SWang Nan * So &mp should not be passed through const pointer. 103471f566a3SWang Nan */ 1035d3d1af6fSAlexey Budankov struct mmap_params mp = { .nr_cblocks = nr_cblocks }; 103650a682ceSArnaldo Carvalho de Melo 10378db6d6b1SWang Nan if (!evlist->mmap) 10382c5f6d87SKan Liang evlist->mmap = perf_evlist__alloc_mmap(evlist, false); 10398db6d6b1SWang Nan if (!evlist->mmap) 1040f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1041f8a95309SArnaldo Carvalho de Melo 10421b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1043f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1044f8a95309SArnaldo Carvalho de Melo 1045994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 10462af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1047a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1048f8a95309SArnaldo Carvalho de Melo 1049718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1050718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1051718c602dSAdrian Hunter 1052e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1053f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1054a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1055a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1056f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1057f8a95309SArnaldo Carvalho de Melo } 1058f8a95309SArnaldo Carvalho de Melo 1059ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1060a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1061f8a95309SArnaldo Carvalho de Melo 1062a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1063f8a95309SArnaldo Carvalho de Melo } 10647e2ed097SArnaldo Carvalho de Melo 1065f74b9d3aSWang Nan int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages) 1066718c602dSAdrian Hunter { 1067d3d1af6fSAlexey Budankov return perf_evlist__mmap_ex(evlist, pages, 0, false, 0); 1068718c602dSAdrian Hunter } 1069718c602dSAdrian Hunter 1070602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 10717e2ed097SArnaldo Carvalho de Melo { 1072147c508fSJin Yao bool all_threads = (target->per_thread && target->system_wide); 107374bfd2b2SAdrian Hunter struct cpu_map *cpus; 107474bfd2b2SAdrian Hunter struct thread_map *threads; 10757e2ed097SArnaldo Carvalho de Melo 1076147c508fSJin Yao /* 1077147c508fSJin Yao * If specify '-a' and '--per-thread' to perf record, perf record 1078147c508fSJin Yao * will override '--per-thread'. target->per_thread = false and 1079147c508fSJin Yao * target->system_wide = true. 1080147c508fSJin Yao * 1081147c508fSJin Yao * If specify '--per-thread' only to perf record, 1082147c508fSJin Yao * target->per_thread = true and target->system_wide = false. 1083147c508fSJin Yao * 1084147c508fSJin Yao * So target->per_thread && target->system_wide is false. 1085147c508fSJin Yao * For perf record, thread_map__new_str doesn't call 1086147c508fSJin Yao * thread_map__new_all_cpus. That will keep perf record's 1087147c508fSJin Yao * current behavior. 1088147c508fSJin Yao * 1089147c508fSJin Yao * For perf stat, it allows the case that target->per_thread and 1090147c508fSJin Yao * target->system_wide are all true. It means to collect system-wide 1091147c508fSJin Yao * per-thread data. thread_map__new_str will call 1092147c508fSJin Yao * thread_map__new_all_cpus to enumerate all threads. 1093147c508fSJin Yao */ 109473c0ca1eSJin Yao threads = thread_map__new_str(target->pid, target->tid, target->uid, 1095147c508fSJin Yao all_threads); 109674bfd2b2SAdrian Hunter 109774bfd2b2SAdrian Hunter if (!threads) 10987e2ed097SArnaldo Carvalho de Melo return -1; 10997e2ed097SArnaldo Carvalho de Melo 11009c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 110174bfd2b2SAdrian Hunter cpus = cpu_map__dummy_new(); 1102879d77d0SNamhyung Kim else 110374bfd2b2SAdrian Hunter cpus = cpu_map__new(target->cpu_list); 11047e2ed097SArnaldo Carvalho de Melo 110574bfd2b2SAdrian Hunter if (!cpus) 11067e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 11077e2ed097SArnaldo Carvalho de Melo 1108ec9a77a7SAdrian Hunter evlist->has_user_cpus = !!target->cpu_list; 1109ec9a77a7SAdrian Hunter 111074bfd2b2SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 1111d5bc056eSAdrian Hunter 1112d5bc056eSAdrian Hunter return 0; 11137e2ed097SArnaldo Carvalho de Melo 11147e2ed097SArnaldo Carvalho de Melo out_delete_threads: 111574bfd2b2SAdrian Hunter thread_map__put(threads); 11167e2ed097SArnaldo Carvalho de Melo return -1; 11177e2ed097SArnaldo Carvalho de Melo } 11187e2ed097SArnaldo Carvalho de Melo 1119d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, 11203de5cfb0SJiri Olsa struct thread_map *threads) 11213de5cfb0SJiri Olsa { 1122934e0f20SAdrian Hunter /* 1123934e0f20SAdrian Hunter * Allow for the possibility that one or another of the maps isn't being 1124934e0f20SAdrian Hunter * changed i.e. don't put it. Note we are assuming the maps that are 1125934e0f20SAdrian Hunter * being applied are brand new and evlist is taking ownership of the 1126934e0f20SAdrian Hunter * original reference count of 1. If that is not the case it is up to 1127934e0f20SAdrian Hunter * the caller to increase the reference count. 1128934e0f20SAdrian Hunter */ 1129934e0f20SAdrian Hunter if (cpus != evlist->cpus) { 11303de5cfb0SJiri Olsa cpu_map__put(evlist->cpus); 1131a55e5663SArnaldo Carvalho de Melo evlist->cpus = cpu_map__get(cpus); 1132934e0f20SAdrian Hunter } 11333de5cfb0SJiri Olsa 1134934e0f20SAdrian Hunter if (threads != evlist->threads) { 11353de5cfb0SJiri Olsa thread_map__put(evlist->threads); 1136a55e5663SArnaldo Carvalho de Melo evlist->threads = thread_map__get(threads); 1137934e0f20SAdrian Hunter } 11383de5cfb0SJiri Olsa 1139ec9a77a7SAdrian Hunter perf_evlist__propagate_maps(evlist); 11403de5cfb0SJiri Olsa } 11413de5cfb0SJiri Olsa 114222c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist, 114322c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 114422c8a376SArnaldo Carvalho de Melo { 114522c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 114622c8a376SArnaldo Carvalho de Melo 1147e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 114822c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 114922c8a376SArnaldo Carvalho de Melo } 115022c8a376SArnaldo Carvalho de Melo 115122c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist, 115222c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 115322c8a376SArnaldo Carvalho de Melo { 115422c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 115522c8a376SArnaldo Carvalho de Melo 1156e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 115722c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 115822c8a376SArnaldo Carvalho de Melo } 115922c8a376SArnaldo Carvalho de Melo 116023d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 11610a102479SFrederic Weisbecker { 11620a102479SFrederic Weisbecker struct perf_evsel *evsel; 1163745cefc5SArnaldo Carvalho de Melo int err = 0; 11640a102479SFrederic Weisbecker 1165e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1166745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 11670a102479SFrederic Weisbecker continue; 1168745cefc5SArnaldo Carvalho de Melo 1169d988d5eeSKan Liang /* 1170d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1171d988d5eeSKan Liang * So evlist and evsel should always be same. 1172d988d5eeSKan Liang */ 1173475fb533SAndi Kleen err = perf_evsel__apply_filter(evsel, evsel->filter); 117423d4aad4SArnaldo Carvalho de Melo if (err) { 117523d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1176745cefc5SArnaldo Carvalho de Melo break; 11770a102479SFrederic Weisbecker } 117823d4aad4SArnaldo Carvalho de Melo } 11790a102479SFrederic Weisbecker 1180745cefc5SArnaldo Carvalho de Melo return err; 1181745cefc5SArnaldo Carvalho de Melo } 1182745cefc5SArnaldo Carvalho de Melo 11837ad92a33SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter(struct perf_evlist *evlist, const char *filter) 1184745cefc5SArnaldo Carvalho de Melo { 1185745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1186745cefc5SArnaldo Carvalho de Melo int err = 0; 1187745cefc5SArnaldo Carvalho de Melo 1188e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1189fdf14720SWang Nan if (evsel->attr.type != PERF_TYPE_TRACEPOINT) 1190fdf14720SWang Nan continue; 1191fdf14720SWang Nan 119294ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1193745cefc5SArnaldo Carvalho de Melo if (err) 1194745cefc5SArnaldo Carvalho de Melo break; 1195745cefc5SArnaldo Carvalho de Melo } 1196745cefc5SArnaldo Carvalho de Melo 1197745cefc5SArnaldo Carvalho de Melo return err; 11980a102479SFrederic Weisbecker } 119974429964SFrederic Weisbecker 12007ad92a33SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1201cfd70a26SArnaldo Carvalho de Melo { 1202cfd70a26SArnaldo Carvalho de Melo char *filter; 1203be199adaSArnaldo Carvalho de Melo int ret = -1; 1204be199adaSArnaldo Carvalho de Melo size_t i; 1205cfd70a26SArnaldo Carvalho de Melo 1206be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1207be199adaSArnaldo Carvalho de Melo if (i == 0) { 1208be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1209cfd70a26SArnaldo Carvalho de Melo return -1; 1210be199adaSArnaldo Carvalho de Melo } else { 1211be199adaSArnaldo Carvalho de Melo char *tmp; 1212be199adaSArnaldo Carvalho de Melo 1213be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1214be199adaSArnaldo Carvalho de Melo goto out_free; 1215be199adaSArnaldo Carvalho de Melo 1216be199adaSArnaldo Carvalho de Melo free(filter); 1217be199adaSArnaldo Carvalho de Melo filter = tmp; 1218be199adaSArnaldo Carvalho de Melo } 1219be199adaSArnaldo Carvalho de Melo } 1220cfd70a26SArnaldo Carvalho de Melo 12217ad92a33SArnaldo Carvalho de Melo ret = perf_evlist__set_tp_filter(evlist, filter); 1222be199adaSArnaldo Carvalho de Melo out_free: 1223cfd70a26SArnaldo Carvalho de Melo free(filter); 1224cfd70a26SArnaldo Carvalho de Melo return ret; 1225cfd70a26SArnaldo Carvalho de Melo } 1226cfd70a26SArnaldo Carvalho de Melo 12277ad92a33SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pid(struct perf_evlist *evlist, pid_t pid) 1228be199adaSArnaldo Carvalho de Melo { 12297ad92a33SArnaldo Carvalho de Melo return perf_evlist__set_tp_filter_pids(evlist, 1, &pid); 1230be199adaSArnaldo Carvalho de Melo } 1231be199adaSArnaldo Carvalho de Melo 12320c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 123374429964SFrederic Weisbecker { 123475562573SAdrian Hunter struct perf_evsel *pos; 1235c2a70653SArnaldo Carvalho de Melo 123675562573SAdrian Hunter if (evlist->nr_entries == 1) 123775562573SAdrian Hunter return true; 123875562573SAdrian Hunter 123975562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 124075562573SAdrian Hunter return false; 124175562573SAdrian Hunter 1242e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 124375562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 124475562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1245c2a70653SArnaldo Carvalho de Melo return false; 124674429964SFrederic Weisbecker } 124774429964SFrederic Weisbecker 1248c2a70653SArnaldo Carvalho de Melo return true; 1249c2a70653SArnaldo Carvalho de Melo } 1250c2a70653SArnaldo Carvalho de Melo 125175562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1252c2a70653SArnaldo Carvalho de Melo { 125375562573SAdrian Hunter struct perf_evsel *evsel; 125475562573SAdrian Hunter 125575562573SAdrian Hunter if (evlist->combined_sample_type) 125675562573SAdrian Hunter return evlist->combined_sample_type; 125775562573SAdrian Hunter 1258e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 125975562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 126075562573SAdrian Hunter 126175562573SAdrian Hunter return evlist->combined_sample_type; 126275562573SAdrian Hunter } 126375562573SAdrian Hunter 126475562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 126575562573SAdrian Hunter { 126675562573SAdrian Hunter evlist->combined_sample_type = 0; 126775562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1268c2a70653SArnaldo Carvalho de Melo } 1269c2a70653SArnaldo Carvalho de Melo 127098df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) 127198df858eSAndi Kleen { 127298df858eSAndi Kleen struct perf_evsel *evsel; 127398df858eSAndi Kleen u64 branch_type = 0; 127498df858eSAndi Kleen 1275e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 127698df858eSAndi Kleen branch_type |= evsel->attr.branch_sample_type; 127798df858eSAndi Kleen return branch_type; 127898df858eSAndi Kleen } 127998df858eSAndi Kleen 12809ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 12819ede473cSJiri Olsa { 12829ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 12839ede473cSJiri Olsa u64 read_format = first->attr.read_format; 12849ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 12859ede473cSJiri Olsa 1286e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 12879ede473cSJiri Olsa if (read_format != pos->attr.read_format) 12889ede473cSJiri Olsa return false; 12899ede473cSJiri Olsa } 12909ede473cSJiri Olsa 12919ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 12929ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 12939ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 12949ede473cSJiri Olsa return false; 12959ede473cSJiri Olsa } 12969ede473cSJiri Olsa 12979ede473cSJiri Olsa return true; 12989ede473cSJiri Olsa } 12999ede473cSJiri Olsa 13009ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 13019ede473cSJiri Olsa { 13029ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 13039ede473cSJiri Olsa return first->attr.read_format; 13049ede473cSJiri Olsa } 13059ede473cSJiri Olsa 13060c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 130781e36bffSArnaldo Carvalho de Melo { 13080c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 130981e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 131081e36bffSArnaldo Carvalho de Melo u64 sample_type; 131181e36bffSArnaldo Carvalho de Melo u16 size = 0; 131281e36bffSArnaldo Carvalho de Melo 131381e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 131481e36bffSArnaldo Carvalho de Melo goto out; 131581e36bffSArnaldo Carvalho de Melo 131681e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 131781e36bffSArnaldo Carvalho de Melo 131881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 131981e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 132081e36bffSArnaldo Carvalho de Melo 132181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 132281e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 132381e36bffSArnaldo Carvalho de Melo 132481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 132581e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 132681e36bffSArnaldo Carvalho de Melo 132781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 132881e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 132981e36bffSArnaldo Carvalho de Melo 133081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 133181e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 133275562573SAdrian Hunter 133375562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 133475562573SAdrian Hunter size += sizeof(data->id); 133581e36bffSArnaldo Carvalho de Melo out: 133681e36bffSArnaldo Carvalho de Melo return size; 133781e36bffSArnaldo Carvalho de Melo } 133881e36bffSArnaldo Carvalho de Melo 13390c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1340c2a70653SArnaldo Carvalho de Melo { 13410c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1342c2a70653SArnaldo Carvalho de Melo 1343e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 1344c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1345c2a70653SArnaldo Carvalho de Melo return false; 1346c2a70653SArnaldo Carvalho de Melo } 1347c2a70653SArnaldo Carvalho de Melo 1348c2a70653SArnaldo Carvalho de Melo return true; 134974429964SFrederic Weisbecker } 135074429964SFrederic Weisbecker 13510c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 135274429964SFrederic Weisbecker { 13530c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1354c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 135574429964SFrederic Weisbecker } 135681cce8deSArnaldo Carvalho de Melo 135781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 135881cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 135981cce8deSArnaldo Carvalho de Melo { 136081cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 136181cce8deSArnaldo Carvalho de Melo } 1362727ab04eSArnaldo Carvalho de Melo 1363a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1364a74b4b66SNamhyung Kim { 1365a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1366a74b4b66SNamhyung Kim 1367475fb533SAndi Kleen evlist__for_each_entry_reverse(evlist, evsel) 1368475fb533SAndi Kleen perf_evsel__close(evsel); 1369a74b4b66SNamhyung Kim } 1370a74b4b66SNamhyung Kim 13714112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 13724112eb18SArnaldo Carvalho de Melo { 13738c0498b6SAdrian Hunter struct cpu_map *cpus; 13748c0498b6SAdrian Hunter struct thread_map *threads; 13754112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 13764112eb18SArnaldo Carvalho de Melo 13774112eb18SArnaldo Carvalho de Melo /* 13784112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 13794112eb18SArnaldo Carvalho de Melo * an all cpus map. 13804112eb18SArnaldo Carvalho de Melo * 13814112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 13824112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 13834112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 13844112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 13854112eb18SArnaldo Carvalho de Melo */ 13868c0498b6SAdrian Hunter cpus = cpu_map__new(NULL); 13878c0498b6SAdrian Hunter if (!cpus) 13884112eb18SArnaldo Carvalho de Melo goto out; 13894112eb18SArnaldo Carvalho de Melo 13908c0498b6SAdrian Hunter threads = thread_map__new_dummy(); 13918c0498b6SAdrian Hunter if (!threads) 13928c0498b6SAdrian Hunter goto out_put; 13934112eb18SArnaldo Carvalho de Melo 13948c0498b6SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 13954112eb18SArnaldo Carvalho de Melo out: 13964112eb18SArnaldo Carvalho de Melo return err; 13978c0498b6SAdrian Hunter out_put: 13988c0498b6SAdrian Hunter cpu_map__put(cpus); 13994112eb18SArnaldo Carvalho de Melo goto out; 14004112eb18SArnaldo Carvalho de Melo } 14014112eb18SArnaldo Carvalho de Melo 14026a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1403727ab04eSArnaldo Carvalho de Melo { 14046a4bb04cSJiri Olsa struct perf_evsel *evsel; 1405a74b4b66SNamhyung Kim int err; 1406727ab04eSArnaldo Carvalho de Melo 14074112eb18SArnaldo Carvalho de Melo /* 14084112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 14094112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 14104112eb18SArnaldo Carvalho de Melo */ 14114112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 14124112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 14134112eb18SArnaldo Carvalho de Melo if (err < 0) 14144112eb18SArnaldo Carvalho de Melo goto out_err; 14154112eb18SArnaldo Carvalho de Melo } 14164112eb18SArnaldo Carvalho de Melo 1417733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1418733cd2feSAdrian Hunter 1419e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 142023df7f79SAdrian Hunter err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); 1421727ab04eSArnaldo Carvalho de Melo if (err < 0) 1422727ab04eSArnaldo Carvalho de Melo goto out_err; 1423727ab04eSArnaldo Carvalho de Melo } 1424727ab04eSArnaldo Carvalho de Melo 1425727ab04eSArnaldo Carvalho de Melo return 0; 1426727ab04eSArnaldo Carvalho de Melo out_err: 1427a74b4b66SNamhyung Kim perf_evlist__close(evlist); 142841c21a68SNamhyung Kim errno = -err; 1429727ab04eSArnaldo Carvalho de Melo return err; 1430727ab04eSArnaldo Carvalho de Melo } 143135b9d88eSArnaldo Carvalho de Melo 1432602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 143355e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1434735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 143535b9d88eSArnaldo Carvalho de Melo { 143635b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 143735b9d88eSArnaldo Carvalho de Melo char bf; 143835b9d88eSArnaldo Carvalho de Melo 143935b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 144035b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 144135b9d88eSArnaldo Carvalho de Melo return -1; 144235b9d88eSArnaldo Carvalho de Melo } 144335b9d88eSArnaldo Carvalho de Melo 144435b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 144535b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 144635b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 144735b9d88eSArnaldo Carvalho de Melo } 144835b9d88eSArnaldo Carvalho de Melo 144935b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 145035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 145135b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 145235b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 145335b9d88eSArnaldo Carvalho de Melo } 145435b9d88eSArnaldo Carvalho de Melo 145535b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 14565f1c4225SArnaldo Carvalho de Melo int ret; 14575f1c4225SArnaldo Carvalho de Melo 1458119fa3c9SNamhyung Kim if (pipe_output) 145935b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 146035b9d88eSArnaldo Carvalho de Melo 14610817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 14620817df08SDavid Ahern 146335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 146435b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 146535b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 146635b9d88eSArnaldo Carvalho de Melo 146735b9d88eSArnaldo Carvalho de Melo /* 146835b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 146935b9d88eSArnaldo Carvalho de Melo */ 147035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 147135b9d88eSArnaldo Carvalho de Melo 147235b9d88eSArnaldo Carvalho de Melo /* 147335b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 147435b9d88eSArnaldo Carvalho de Melo */ 14755f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 14765f1c4225SArnaldo Carvalho de Melo /* 14775f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 14785f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 14795f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 14805f1c4225SArnaldo Carvalho de Melo * 148120f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 14825f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 14835f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 14845f1c4225SArnaldo Carvalho de Melo * here. 14855f1c4225SArnaldo Carvalho de Melo */ 14865f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 14875f1c4225SArnaldo Carvalho de Melo if (ret == -1) 148835b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 14895f1c4225SArnaldo Carvalho de Melo exit(ret); 14905f1c4225SArnaldo Carvalho de Melo } 149135b9d88eSArnaldo Carvalho de Melo 149235b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 149335b9d88eSArnaldo Carvalho de Melo 1494735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1495f33cbe72SArnaldo Carvalho de Melo union sigval val; 1496f33cbe72SArnaldo Carvalho de Melo 1497f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1498f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 149935b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1500f33cbe72SArnaldo Carvalho de Melo } else 1501f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 150235b9d88eSArnaldo Carvalho de Melo exit(-1); 150335b9d88eSArnaldo Carvalho de Melo } 150435b9d88eSArnaldo Carvalho de Melo 1505735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1506735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1507735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1508735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1509735f7e0bSArnaldo Carvalho de Melo }; 1510735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1511735f7e0bSArnaldo Carvalho de Melo } 1512735f7e0bSArnaldo Carvalho de Melo 15131aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 15141aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 15151aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 15161aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 15171aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 15181aaf63b1SArnaldo Carvalho de Melo } 1519e13798c7SJiri Olsa thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 15201aaf63b1SArnaldo Carvalho de Melo } 152135b9d88eSArnaldo Carvalho de Melo 152235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 152335b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 152435b9d88eSArnaldo Carvalho de Melo /* 152535b9d88eSArnaldo Carvalho de Melo * wait for child to settle 152635b9d88eSArnaldo Carvalho de Melo */ 152735b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 152835b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 152935b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 153035b9d88eSArnaldo Carvalho de Melo } 153135b9d88eSArnaldo Carvalho de Melo 1532bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 153335b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 153435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 153535b9d88eSArnaldo Carvalho de Melo return 0; 153635b9d88eSArnaldo Carvalho de Melo 153735b9d88eSArnaldo Carvalho de Melo out_close_pipes: 153835b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 153935b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 154035b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 154135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 154235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 154335b9d88eSArnaldo Carvalho de Melo return -1; 154435b9d88eSArnaldo Carvalho de Melo } 154535b9d88eSArnaldo Carvalho de Melo 154635b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 154735b9d88eSArnaldo Carvalho de Melo { 154835b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1549b3824404SDavid Ahern char bf = 0; 1550bcf3145fSNamhyung Kim int ret; 155135b9d88eSArnaldo Carvalho de Melo /* 155235b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 155335b9d88eSArnaldo Carvalho de Melo */ 1554bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1555bcf3145fSNamhyung Kim if (ret < 0) 1556e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1557bcf3145fSNamhyung Kim 1558bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1559bcf3145fSNamhyung Kim return ret; 156035b9d88eSArnaldo Carvalho de Melo } 156135b9d88eSArnaldo Carvalho de Melo 156235b9d88eSArnaldo Carvalho de Melo return 0; 156335b9d88eSArnaldo Carvalho de Melo } 1564cb0b29e0SArnaldo Carvalho de Melo 1565a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 15660807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1567cb0b29e0SArnaldo Carvalho de Melo { 156875562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 156975562573SAdrian Hunter 157075562573SAdrian Hunter if (!evsel) 157175562573SAdrian Hunter return -EFAULT; 15720807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1573cb0b29e0SArnaldo Carvalho de Melo } 157478f067b3SArnaldo Carvalho de Melo 157501468120SJiri Olsa int perf_evlist__parse_sample_timestamp(struct perf_evlist *evlist, 157601468120SJiri Olsa union perf_event *event, 157701468120SJiri Olsa u64 *timestamp) 157801468120SJiri Olsa { 157901468120SJiri Olsa struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 158001468120SJiri Olsa 158101468120SJiri Olsa if (!evsel) 158201468120SJiri Olsa return -EFAULT; 158301468120SJiri Olsa return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); 158401468120SJiri Olsa } 158501468120SJiri Olsa 158678f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 158778f067b3SArnaldo Carvalho de Melo { 158878f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 158978f067b3SArnaldo Carvalho de Melo size_t printed = 0; 159078f067b3SArnaldo Carvalho de Melo 1591e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 159278f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 159378f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 159478f067b3SArnaldo Carvalho de Melo } 159578f067b3SArnaldo Carvalho de Melo 1596b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 159778f067b3SArnaldo Carvalho de Melo } 15986ef068cbSArnaldo Carvalho de Melo 1599d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist, 1600a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1601a8f23d8fSArnaldo Carvalho de Melo { 1602a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1603c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1604a8f23d8fSArnaldo Carvalho de Melo 1605a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1606a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1607a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1608a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1609a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1610a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1611a8f23d8fSArnaldo Carvalho de Melo 16121a47245dSAdrian Hunter value = perf_event_paranoid(); 1613a8f23d8fSArnaldo Carvalho de Melo 1614a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1615a8f23d8fSArnaldo Carvalho de Melo 1616a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1617a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1618a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1619a8f23d8fSArnaldo Carvalho de Melo } 1620a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16215229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1622a8f23d8fSArnaldo Carvalho de Melo 1623a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16245229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 16255229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1626a8f23d8fSArnaldo Carvalho de Melo break; 1627d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1628d9aade7fSArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1629d9aade7fSArnaldo Carvalho de Melo int max_freq; 1630d9aade7fSArnaldo Carvalho de Melo 1631d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1632d9aade7fSArnaldo Carvalho de Melo goto out_default; 1633d9aade7fSArnaldo Carvalho de Melo 1634d9aade7fSArnaldo Carvalho de Melo if (first->attr.sample_freq < (u64)max_freq) 1635d9aade7fSArnaldo Carvalho de Melo goto out_default; 1636d9aade7fSArnaldo Carvalho de Melo 1637d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1638d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1639d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1640d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1641d9aade7fSArnaldo Carvalho de Melo emsg, max_freq, first->attr.sample_freq); 1642d9aade7fSArnaldo Carvalho de Melo break; 1643d9aade7fSArnaldo Carvalho de Melo } 1644a8f23d8fSArnaldo Carvalho de Melo default: 1645d9aade7fSArnaldo Carvalho de Melo out_default: 1646a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1647a8f23d8fSArnaldo Carvalho de Melo break; 1648a8f23d8fSArnaldo Carvalho de Melo } 1649a8f23d8fSArnaldo Carvalho de Melo 1650a8f23d8fSArnaldo Carvalho de Melo return 0; 1651a8f23d8fSArnaldo Carvalho de Melo } 1652a025e4f0SAdrian Hunter 1653956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1654956fa571SArnaldo Carvalho de Melo { 1655c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1656e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1657956fa571SArnaldo Carvalho de Melo 1658956fa571SArnaldo Carvalho de Melo switch (err) { 1659956fa571SArnaldo Carvalho de Melo case EPERM: 1660e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1661e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1662e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1663956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1664e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1665e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1666e965bea1SArnaldo Carvalho de Melo 1667e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1668e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1669e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1670e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1671e965bea1SArnaldo Carvalho de Melo } 1672e965bea1SArnaldo Carvalho de Melo 1673e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1674e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1675956fa571SArnaldo Carvalho de Melo break; 1676956fa571SArnaldo Carvalho de Melo default: 1677956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1678956fa571SArnaldo Carvalho de Melo break; 1679956fa571SArnaldo Carvalho de Melo } 1680956fa571SArnaldo Carvalho de Melo 1681956fa571SArnaldo Carvalho de Melo return 0; 1682956fa571SArnaldo Carvalho de Melo } 1683956fa571SArnaldo Carvalho de Melo 1684a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1685a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1686a025e4f0SAdrian Hunter { 1687a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1688a025e4f0SAdrian Hunter LIST_HEAD(move); 1689a025e4f0SAdrian Hunter 1690a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1691a025e4f0SAdrian Hunter return; 1692a025e4f0SAdrian Hunter 1693e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1694a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1695a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1696a025e4f0SAdrian Hunter } 1697a025e4f0SAdrian Hunter 1698a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1699a025e4f0SAdrian Hunter } 170060b0896cSAdrian Hunter 170160b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 170260b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 170360b0896cSAdrian Hunter { 170460b0896cSAdrian Hunter struct perf_evsel *evsel; 170560b0896cSAdrian Hunter 170660b0896cSAdrian Hunter if (tracking_evsel->tracking) 170760b0896cSAdrian Hunter return; 170860b0896cSAdrian Hunter 1709e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 171060b0896cSAdrian Hunter if (evsel != tracking_evsel) 171160b0896cSAdrian Hunter evsel->tracking = false; 171260b0896cSAdrian Hunter } 171360b0896cSAdrian Hunter 171460b0896cSAdrian Hunter tracking_evsel->tracking = true; 171560b0896cSAdrian Hunter } 17167630b3e2SWang Nan 17177630b3e2SWang Nan struct perf_evsel * 17187630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, 17197630b3e2SWang Nan const char *str) 17207630b3e2SWang Nan { 17217630b3e2SWang Nan struct perf_evsel *evsel; 17227630b3e2SWang Nan 1723e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 17247630b3e2SWang Nan if (!evsel->name) 17257630b3e2SWang Nan continue; 17267630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 17277630b3e2SWang Nan return evsel; 17287630b3e2SWang Nan } 17297630b3e2SWang Nan 17307630b3e2SWang Nan return NULL; 17317630b3e2SWang Nan } 173254cc54deSWang Nan 173354cc54deSWang Nan void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, 173454cc54deSWang Nan enum bkw_mmap_state state) 173554cc54deSWang Nan { 173654cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 173754cc54deSWang Nan enum action { 173854cc54deSWang Nan NONE, 173954cc54deSWang Nan PAUSE, 174054cc54deSWang Nan RESUME, 174154cc54deSWang Nan } action = NONE; 174254cc54deSWang Nan 17430b72d69aSWang Nan if (!evlist->overwrite_mmap) 174454cc54deSWang Nan return; 174554cc54deSWang Nan 174654cc54deSWang Nan switch (old_state) { 174754cc54deSWang Nan case BKW_MMAP_NOTREADY: { 174854cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 1749dd8bd53aSLuis de Bethencourt goto state_err; 175054cc54deSWang Nan break; 175154cc54deSWang Nan } 175254cc54deSWang Nan case BKW_MMAP_RUNNING: { 175354cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 175454cc54deSWang Nan goto state_err; 175554cc54deSWang Nan action = PAUSE; 175654cc54deSWang Nan break; 175754cc54deSWang Nan } 175854cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 175954cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 176054cc54deSWang Nan goto state_err; 176154cc54deSWang Nan break; 176254cc54deSWang Nan } 176354cc54deSWang Nan case BKW_MMAP_EMPTY: { 176454cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 176554cc54deSWang Nan goto state_err; 176654cc54deSWang Nan action = RESUME; 176754cc54deSWang Nan break; 176854cc54deSWang Nan } 176954cc54deSWang Nan default: 177054cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 177154cc54deSWang Nan } 177254cc54deSWang Nan 177354cc54deSWang Nan evlist->bkw_mmap_state = state; 177454cc54deSWang Nan 177554cc54deSWang Nan switch (action) { 177654cc54deSWang Nan case PAUSE: 177754cc54deSWang Nan perf_evlist__pause(evlist); 177854cc54deSWang Nan break; 177954cc54deSWang Nan case RESUME: 178054cc54deSWang Nan perf_evlist__resume(evlist); 178154cc54deSWang Nan break; 178254cc54deSWang Nan case NONE: 178354cc54deSWang Nan default: 178454cc54deSWang Nan break; 178554cc54deSWang Nan } 178654cc54deSWang Nan 178754cc54deSWang Nan state_err: 178854cc54deSWang Nan return; 178954cc54deSWang Nan } 179007d6f446SArnaldo Carvalho de Melo 179107d6f446SArnaldo Carvalho de Melo bool perf_evlist__exclude_kernel(struct perf_evlist *evlist) 179207d6f446SArnaldo Carvalho de Melo { 179307d6f446SArnaldo Carvalho de Melo struct perf_evsel *evsel; 179407d6f446SArnaldo Carvalho de Melo 179507d6f446SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 179607d6f446SArnaldo Carvalho de Melo if (!evsel->attr.exclude_kernel) 179707d6f446SArnaldo Carvalho de Melo return false; 179807d6f446SArnaldo Carvalho de Melo } 179907d6f446SArnaldo Carvalho de Melo 180007d6f446SArnaldo Carvalho de Melo return true; 180107d6f446SArnaldo Carvalho de Melo } 1802e2bdbe80SJin Yao 1803e2bdbe80SJin Yao /* 1804e2bdbe80SJin Yao * Events in data file are not collect in groups, but we still want 1805e2bdbe80SJin Yao * the group display. Set the artificial group and set the leader's 1806e2bdbe80SJin Yao * forced_leader flag to notify the display code. 1807e2bdbe80SJin Yao */ 1808e2bdbe80SJin Yao void perf_evlist__force_leader(struct perf_evlist *evlist) 1809e2bdbe80SJin Yao { 1810e2bdbe80SJin Yao if (!evlist->nr_groups) { 1811e2bdbe80SJin Yao struct perf_evsel *leader = perf_evlist__first(evlist); 1812e2bdbe80SJin Yao 1813e2bdbe80SJin Yao perf_evlist__set_leader(evlist); 1814e2bdbe80SJin Yao leader->forced_leader = true; 1815e2bdbe80SJin Yao } 1816e2bdbe80SJin Yao } 1817c3537fc2SAndi Kleen 1818c3537fc2SAndi Kleen struct perf_evsel *perf_evlist__reset_weak_group(struct perf_evlist *evsel_list, 1819c3537fc2SAndi Kleen struct perf_evsel *evsel) 1820c3537fc2SAndi Kleen { 1821c3537fc2SAndi Kleen struct perf_evsel *c2, *leader; 1822c3537fc2SAndi Kleen bool is_open = true; 1823c3537fc2SAndi Kleen 1824c3537fc2SAndi Kleen leader = evsel->leader; 1825c3537fc2SAndi Kleen pr_debug("Weak group for %s/%d failed\n", 1826c3537fc2SAndi Kleen leader->name, leader->nr_members); 1827c3537fc2SAndi Kleen 1828c3537fc2SAndi Kleen /* 1829c3537fc2SAndi Kleen * for_each_group_member doesn't work here because it doesn't 1830c3537fc2SAndi Kleen * include the first entry. 1831c3537fc2SAndi Kleen */ 1832c3537fc2SAndi Kleen evlist__for_each_entry(evsel_list, c2) { 1833c3537fc2SAndi Kleen if (c2 == evsel) 1834c3537fc2SAndi Kleen is_open = false; 1835c3537fc2SAndi Kleen if (c2->leader == leader) { 1836c3537fc2SAndi Kleen if (is_open) 1837c3537fc2SAndi Kleen perf_evsel__close(c2); 1838c3537fc2SAndi Kleen c2->leader = c2; 1839c3537fc2SAndi Kleen c2->nr_members = 0; 1840c3537fc2SAndi Kleen } 1841c3537fc2SAndi Kleen } 1842c3537fc2SAndi Kleen return leader; 1843c3537fc2SAndi Kleen } 1844