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> 11fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 125c581041SArnaldo Carvalho de Melo #include <poll.h> 13f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1512864b31SNamhyung Kim #include "target.h" 16361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 17361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 18e3e1a54fSAdrian Hunter #include "debug.h" 1954cc54deSWang Nan #include "asm/bug.h" 2035b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 21361c99a6SArnaldo Carvalho de Melo 2250d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 234b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 2450d08e47SArnaldo Carvalho de Melo 25f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 26f8a95309SArnaldo Carvalho de Melo 2770db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2870db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 290389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 308dd2a131SJiri Olsa #include <linux/err.h> 3170db7533SArnaldo Carvalho de Melo 328db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map); 334876075bSWang Nan static void perf_mmap__put(struct perf_mmap *map); 34e4b356b5SArnaldo Carvalho de Melo 35f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 36a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 37f8a95309SArnaldo Carvalho de Melo 387e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 397e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 40361c99a6SArnaldo Carvalho de Melo { 4170db7533SArnaldo Carvalho de Melo int i; 4270db7533SArnaldo Carvalho de Melo 4370db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 4470db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 45361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 467e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 471b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 4835b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 4954cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 50361c99a6SArnaldo Carvalho de Melo } 51361c99a6SArnaldo Carvalho de Melo 52334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 53ef1d1af2SArnaldo Carvalho de Melo { 54ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 55ef1d1af2SArnaldo Carvalho de Melo 56ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 57334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 58ef1d1af2SArnaldo Carvalho de Melo 59361c99a6SArnaldo Carvalho de Melo return evlist; 60361c99a6SArnaldo Carvalho de Melo } 61361c99a6SArnaldo Carvalho de Melo 62b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 63b22d54b0SJiri Olsa { 64b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 65b22d54b0SJiri Olsa 66b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 67b22d54b0SJiri Olsa perf_evlist__delete(evlist); 68b22d54b0SJiri Olsa evlist = NULL; 69b22d54b0SJiri Olsa } 70b22d54b0SJiri Olsa 71b22d54b0SJiri Olsa return evlist; 72b22d54b0SJiri Olsa } 73b22d54b0SJiri Olsa 745bae0250SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new_dummy(void) 755bae0250SArnaldo Carvalho de Melo { 765bae0250SArnaldo Carvalho de Melo struct perf_evlist *evlist = perf_evlist__new(); 775bae0250SArnaldo Carvalho de Melo 785bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 795bae0250SArnaldo Carvalho de Melo perf_evlist__delete(evlist); 805bae0250SArnaldo Carvalho de Melo evlist = NULL; 815bae0250SArnaldo Carvalho de Melo } 825bae0250SArnaldo Carvalho de Melo 835bae0250SArnaldo Carvalho de Melo return evlist; 845bae0250SArnaldo Carvalho de Melo } 855bae0250SArnaldo Carvalho de Melo 8675562573SAdrian Hunter /** 8775562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 8875562573SAdrian Hunter * @evlist: selected event list 8975562573SAdrian Hunter * 9075562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 9175562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 9275562573SAdrian Hunter */ 9375562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 9475562573SAdrian Hunter { 9575562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 9675562573SAdrian Hunter 9775562573SAdrian Hunter evlist->id_pos = first->id_pos; 9875562573SAdrian Hunter evlist->is_pos = first->is_pos; 9975562573SAdrian Hunter } 10075562573SAdrian Hunter 101733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 102733cd2feSAdrian Hunter { 103733cd2feSAdrian Hunter struct perf_evsel *evsel; 104733cd2feSAdrian Hunter 105e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 106733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 107733cd2feSAdrian Hunter 108733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 109733cd2feSAdrian Hunter } 110733cd2feSAdrian Hunter 111361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 112361c99a6SArnaldo Carvalho de Melo { 113361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 114361c99a6SArnaldo Carvalho de Melo 115e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 116361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 117d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 118361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 119361c99a6SArnaldo Carvalho de Melo } 120361c99a6SArnaldo Carvalho de Melo 121361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 122361c99a6SArnaldo Carvalho de Melo } 123361c99a6SArnaldo Carvalho de Melo 124ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 125ef1d1af2SArnaldo Carvalho de Melo { 12604662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 127b2cb615dSWang Nan zfree(&evlist->backward_mmap); 1281b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 129ef1d1af2SArnaldo Carvalho de Melo } 130ef1d1af2SArnaldo Carvalho de Melo 131361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 132361c99a6SArnaldo Carvalho de Melo { 1330b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1340b04b3dcSArnaldo Carvalho de Melo return; 1350b04b3dcSArnaldo Carvalho de Melo 136983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 137f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 138f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 139186fbb74SJiri Olsa thread_map__put(evlist->threads); 14003ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 14103ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 142361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 143ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 144361c99a6SArnaldo Carvalho de Melo free(evlist); 145361c99a6SArnaldo Carvalho de Melo } 146361c99a6SArnaldo Carvalho de Melo 147adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, 148adc0c3e8SAdrian Hunter struct perf_evsel *evsel) 149adc0c3e8SAdrian Hunter { 150adc0c3e8SAdrian Hunter /* 151adc0c3e8SAdrian Hunter * We already have cpus for evsel (via PMU sysfs) so 152adc0c3e8SAdrian Hunter * keep it, if there's no target cpu list defined. 153adc0c3e8SAdrian Hunter */ 154adc0c3e8SAdrian Hunter if (!evsel->own_cpus || evlist->has_user_cpus) { 155adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 156adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evlist->cpus); 157adc0c3e8SAdrian Hunter } else if (evsel->cpus != evsel->own_cpus) { 158adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 159adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evsel->own_cpus); 160adc0c3e8SAdrian Hunter } 161adc0c3e8SAdrian Hunter 162adc0c3e8SAdrian Hunter thread_map__put(evsel->threads); 163adc0c3e8SAdrian Hunter evsel->threads = thread_map__get(evlist->threads); 164adc0c3e8SAdrian Hunter } 165adc0c3e8SAdrian Hunter 166adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist) 167adc0c3e8SAdrian Hunter { 168adc0c3e8SAdrian Hunter struct perf_evsel *evsel; 169adc0c3e8SAdrian Hunter 170e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 171adc0c3e8SAdrian Hunter __perf_evlist__propagate_maps(evlist, evsel); 172adc0c3e8SAdrian Hunter } 173adc0c3e8SAdrian Hunter 174361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 175361c99a6SArnaldo Carvalho de Melo { 176d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 177361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 178ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 17960b0896cSAdrian Hunter entry->tracking = !entry->idx; 180ef503831SArnaldo Carvalho de Melo 18175562573SAdrian Hunter if (!evlist->nr_entries++) 18275562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 18344c42d71SAdrian Hunter 18444c42d71SAdrian Hunter __perf_evlist__propagate_maps(evlist, entry); 185361c99a6SArnaldo Carvalho de Melo } 186361c99a6SArnaldo Carvalho de Melo 1874768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel) 1884768230aSAdrian Hunter { 1894768230aSAdrian Hunter evsel->evlist = NULL; 1904768230aSAdrian Hunter list_del_init(&evsel->node); 1914768230aSAdrian Hunter evlist->nr_entries -= 1; 1924768230aSAdrian Hunter } 1934768230aSAdrian Hunter 1940529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 195f114d6efSAdrian Hunter struct list_head *list) 19650d08e47SArnaldo Carvalho de Melo { 197f114d6efSAdrian Hunter struct perf_evsel *evsel, *temp; 19875562573SAdrian Hunter 199e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 200f114d6efSAdrian Hunter list_del_init(&evsel->node); 201f114d6efSAdrian Hunter perf_evlist__add(evlist, evsel); 202f114d6efSAdrian Hunter } 20350d08e47SArnaldo Carvalho de Melo } 20450d08e47SArnaldo Carvalho de Melo 20563dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 20663dab225SArnaldo Carvalho de Melo { 20763dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 20863dab225SArnaldo Carvalho de Melo 20963dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 21097f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 21197f63e4aSNamhyung Kim 21297f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 21363dab225SArnaldo Carvalho de Melo 214e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 21563dab225SArnaldo Carvalho de Melo evsel->leader = leader; 21663dab225SArnaldo Carvalho de Melo } 21763dab225SArnaldo Carvalho de Melo } 21863dab225SArnaldo Carvalho de Melo 21963dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 2206a4bb04cSJiri Olsa { 22197f63e4aSNamhyung Kim if (evlist->nr_entries) { 22297f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 22363dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 2246a4bb04cSJiri Olsa } 22597f63e4aSNamhyung Kim } 2266a4bb04cSJiri Olsa 22745cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr) 2287f8d1adeSArnaldo Carvalho de Melo { 2297f8d1adeSArnaldo Carvalho de Melo attr->precise_ip = 3; 2307f8d1adeSArnaldo Carvalho de Melo 2317f8d1adeSArnaldo Carvalho de Melo while (attr->precise_ip != 0) { 2327f8d1adeSArnaldo Carvalho de Melo int fd = sys_perf_event_open(attr, 0, -1, -1, 0); 2337f8d1adeSArnaldo Carvalho de Melo if (fd != -1) { 2347f8d1adeSArnaldo Carvalho de Melo close(fd); 2357f8d1adeSArnaldo Carvalho de Melo break; 2367f8d1adeSArnaldo Carvalho de Melo } 2377f8d1adeSArnaldo Carvalho de Melo --attr->precise_ip; 2387f8d1adeSArnaldo Carvalho de Melo } 2397f8d1adeSArnaldo Carvalho de Melo } 2407f8d1adeSArnaldo Carvalho de Melo 241361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 242361c99a6SArnaldo Carvalho de Melo { 2437c48dcfdSArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new_cycles(); 244361c99a6SArnaldo Carvalho de Melo 245361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2467c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 247361c99a6SArnaldo Carvalho de Melo 248361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 249361c99a6SArnaldo Carvalho de Melo return 0; 250361c99a6SArnaldo Carvalho de Melo } 2515c581041SArnaldo Carvalho de Melo 2525bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist) 2535bae0250SArnaldo Carvalho de Melo { 2545bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2555bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2565bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2575bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2585bae0250SArnaldo Carvalho de Melo }; 2595bae0250SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new(&attr); 2605bae0250SArnaldo Carvalho de Melo 2615bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2625bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2635bae0250SArnaldo Carvalho de Melo 2645bae0250SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 2655bae0250SArnaldo Carvalho de Melo return 0; 2665bae0250SArnaldo Carvalho de Melo } 2675bae0250SArnaldo Carvalho de Melo 268e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 26950d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 27050d08e47SArnaldo Carvalho de Melo { 27150d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 27250d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 27350d08e47SArnaldo Carvalho de Melo size_t i; 27450d08e47SArnaldo Carvalho de Melo 27550d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 276ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 27750d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27850d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 27950d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 28050d08e47SArnaldo Carvalho de Melo } 28150d08e47SArnaldo Carvalho de Melo 282f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 28350d08e47SArnaldo Carvalho de Melo 28450d08e47SArnaldo Carvalho de Melo return 0; 28550d08e47SArnaldo Carvalho de Melo 28650d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 287e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 28850d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 28950d08e47SArnaldo Carvalho de Melo return -1; 29050d08e47SArnaldo Carvalho de Melo } 29150d08e47SArnaldo Carvalho de Melo 29279695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 29379695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 29479695e1bSArnaldo Carvalho de Melo { 29579695e1bSArnaldo Carvalho de Melo size_t i; 29679695e1bSArnaldo Carvalho de Melo 29779695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 29879695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 29979695e1bSArnaldo Carvalho de Melo 30079695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 30179695e1bSArnaldo Carvalho de Melo } 30279695e1bSArnaldo Carvalho de Melo 303da378962SArnaldo Carvalho de Melo struct perf_evsel * 304ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 305ee29be62SArnaldo Carvalho de Melo { 306ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 307ee29be62SArnaldo Carvalho de Melo 308e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 309ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 310ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 311ee29be62SArnaldo Carvalho de Melo return evsel; 312ee29be62SArnaldo Carvalho de Melo } 313ee29be62SArnaldo Carvalho de Melo 314ee29be62SArnaldo Carvalho de Melo return NULL; 315ee29be62SArnaldo Carvalho de Melo } 316ee29be62SArnaldo Carvalho de Melo 317a2f2804aSDavid Ahern struct perf_evsel * 318a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 319a2f2804aSDavid Ahern const char *name) 320a2f2804aSDavid Ahern { 321a2f2804aSDavid Ahern struct perf_evsel *evsel; 322a2f2804aSDavid Ahern 323e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 324a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 325a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 326a2f2804aSDavid Ahern return evsel; 327a2f2804aSDavid Ahern } 328a2f2804aSDavid Ahern 329a2f2804aSDavid Ahern return NULL; 330a2f2804aSDavid Ahern } 331a2f2804aSDavid Ahern 33239876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 33339876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 33439876e7dSArnaldo Carvalho de Melo { 335ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 33639876e7dSArnaldo Carvalho de Melo 3378dd2a131SJiri Olsa if (IS_ERR(evsel)) 33839876e7dSArnaldo Carvalho de Melo return -1; 33939876e7dSArnaldo Carvalho de Melo 340744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 34139876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 34239876e7dSArnaldo Carvalho de Melo return 0; 34339876e7dSArnaldo Carvalho de Melo } 34439876e7dSArnaldo Carvalho de Melo 345bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 346bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 347bf8e8f4bSAdrian Hunter { 348bf8e8f4bSAdrian Hunter if (evsel->system_wide) 349bf8e8f4bSAdrian Hunter return 1; 350bf8e8f4bSAdrian Hunter else 351bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 352bf8e8f4bSAdrian Hunter } 353bf8e8f4bSAdrian Hunter 3544152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 3554152ab37SArnaldo Carvalho de Melo { 3564152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 3574152ab37SArnaldo Carvalho de Melo 358e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 359395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3603fe4430dSJiri Olsa continue; 3613e27c920SJiri Olsa perf_evsel__disable(pos); 3624152ab37SArnaldo Carvalho de Melo } 3632b56bcfbSArnaldo Carvalho de Melo 3642b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3654152ab37SArnaldo Carvalho de Melo } 3664152ab37SArnaldo Carvalho de Melo 367764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 368764e16a3SDavid Ahern { 369764e16a3SDavid Ahern struct perf_evsel *pos; 370764e16a3SDavid Ahern 371e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 372395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3733fe4430dSJiri Olsa continue; 3743e27c920SJiri Olsa perf_evsel__enable(pos); 375764e16a3SDavid Ahern } 3762b56bcfbSArnaldo Carvalho de Melo 3772b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3782b56bcfbSArnaldo Carvalho de Melo } 3792b56bcfbSArnaldo Carvalho de Melo 3802b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist) 3812b56bcfbSArnaldo Carvalho de Melo { 3822b56bcfbSArnaldo Carvalho de Melo (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); 383764e16a3SDavid Ahern } 384764e16a3SDavid Ahern 3851c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3861c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3871c65056cSAdrian Hunter { 38818ef15c6SArnaldo Carvalho de Melo int thread; 3891c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3901c65056cSAdrian Hunter 3911c65056cSAdrian Hunter if (!evsel->fd) 3921c65056cSAdrian Hunter return -EINVAL; 3931c65056cSAdrian Hunter 3941c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 39518ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3961c65056cSAdrian Hunter if (err) 3971c65056cSAdrian Hunter return err; 3981c65056cSAdrian Hunter } 3991c65056cSAdrian Hunter return 0; 4001c65056cSAdrian Hunter } 4011c65056cSAdrian Hunter 4021c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 4031c65056cSAdrian Hunter struct perf_evsel *evsel, 4041c65056cSAdrian Hunter int thread) 4051c65056cSAdrian Hunter { 40618ef15c6SArnaldo Carvalho de Melo int cpu; 4071c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 4081c65056cSAdrian Hunter 4091c65056cSAdrian Hunter if (!evsel->fd) 4101c65056cSAdrian Hunter return -EINVAL; 4111c65056cSAdrian Hunter 4121c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 41318ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4141c65056cSAdrian Hunter if (err) 4151c65056cSAdrian Hunter return err; 4161c65056cSAdrian Hunter } 4171c65056cSAdrian Hunter return 0; 4181c65056cSAdrian Hunter } 4191c65056cSAdrian Hunter 4201c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4211c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4221c65056cSAdrian Hunter { 4231c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4241c65056cSAdrian Hunter 4251c65056cSAdrian Hunter if (per_cpu_mmaps) 4261c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4271c65056cSAdrian Hunter else 4281c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4291c65056cSAdrian Hunter } 4301c65056cSAdrian Hunter 431ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4325c581041SArnaldo Carvalho de Melo { 433b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 434b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 435bf8e8f4bSAdrian Hunter int nfds = 0; 436bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 437bf8e8f4bSAdrian Hunter 438e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 439bf8e8f4bSAdrian Hunter if (evsel->system_wide) 440bf8e8f4bSAdrian Hunter nfds += nr_cpus; 441bf8e8f4bSAdrian Hunter else 442bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 443bf8e8f4bSAdrian Hunter } 444bf8e8f4bSAdrian Hunter 4451b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4461b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 447ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 448ad6765ddSArnaldo Carvalho de Melo 449ad6765ddSArnaldo Carvalho de Melo return 0; 4505c581041SArnaldo Carvalho de Melo } 45170082dd9SArnaldo Carvalho de Melo 4524876075bSWang Nan static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, 4534876075bSWang Nan struct perf_mmap *map, short revent) 454e4b356b5SArnaldo Carvalho de Melo { 455f3058a1cSWang Nan int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); 456e4b356b5SArnaldo Carvalho de Melo /* 457e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 458e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 459e4b356b5SArnaldo Carvalho de Melo */ 460e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 4614876075bSWang Nan evlist->pollfd.priv[pos].ptr = map; 462e4b356b5SArnaldo Carvalho de Melo 463e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 464e4b356b5SArnaldo Carvalho de Melo } 465e4b356b5SArnaldo Carvalho de Melo 466e4b356b5SArnaldo Carvalho de Melo return pos; 467e4b356b5SArnaldo Carvalho de Melo } 468e4b356b5SArnaldo Carvalho de Melo 469ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 47070082dd9SArnaldo Carvalho de Melo { 4714876075bSWang Nan return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN); 472e4b356b5SArnaldo Carvalho de Melo } 4731b85337dSArnaldo Carvalho de Melo 474258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 475258e4bfcSWang Nan void *arg __maybe_unused) 476e4b356b5SArnaldo Carvalho de Melo { 4774876075bSWang Nan struct perf_mmap *map = fda->priv[fd].ptr; 478e4b356b5SArnaldo Carvalho de Melo 4794876075bSWang Nan if (map) 4804876075bSWang Nan perf_mmap__put(map); 48170082dd9SArnaldo Carvalho de Melo } 48270db7533SArnaldo Carvalho de Melo 4831ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4841ddec7f0SArnaldo Carvalho de Melo { 485e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 486258e4bfcSWang Nan perf_evlist__munmap_filtered, NULL); 4871ddec7f0SArnaldo Carvalho de Melo } 4881ddec7f0SArnaldo Carvalho de Melo 489f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 490f66a889dSArnaldo Carvalho de Melo { 4911b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 492f66a889dSArnaldo Carvalho de Melo } 493f66a889dSArnaldo Carvalho de Melo 494a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 495a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 4963d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4973d3b5e95SArnaldo Carvalho de Melo { 4983d3b5e95SArnaldo Carvalho de Melo int hash; 4993d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 5003d3b5e95SArnaldo Carvalho de Melo 5013d3b5e95SArnaldo Carvalho de Melo sid->id = id; 5023d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 5033d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 5043d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 5053d3b5e95SArnaldo Carvalho de Melo } 5063d3b5e95SArnaldo Carvalho de Melo 507a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 508a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 509a91e5431SArnaldo Carvalho de Melo { 510a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 511a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 512a91e5431SArnaldo Carvalho de Melo } 513a91e5431SArnaldo Carvalho de Melo 5141c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist, 5153d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 516f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 517f8a95309SArnaldo Carvalho de Melo { 518f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5193d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 520e2b5abe0SJiri Olsa u64 id; 521e2b5abe0SJiri Olsa int ret; 522e2b5abe0SJiri Olsa 523e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 524e2b5abe0SJiri Olsa if (!ret) 525e2b5abe0SJiri Olsa goto add; 526e2b5abe0SJiri Olsa 527e2b5abe0SJiri Olsa if (errno != ENOTTY) 528e2b5abe0SJiri Olsa return -1; 529e2b5abe0SJiri Olsa 530e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 531f8a95309SArnaldo Carvalho de Melo 532c4861afeSJiri Olsa /* 533c4861afeSJiri Olsa * This way does not work with group format read, so bail 534c4861afeSJiri Olsa * out in that case. 535c4861afeSJiri Olsa */ 536c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 537c4861afeSJiri Olsa return -1; 538c4861afeSJiri Olsa 539f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 540f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 541f8a95309SArnaldo Carvalho de Melo return -1; 542f8a95309SArnaldo Carvalho de Melo 543f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 544f8a95309SArnaldo Carvalho de Melo ++id_idx; 545f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 546f8a95309SArnaldo Carvalho de Melo ++id_idx; 547f8a95309SArnaldo Carvalho de Melo 548e2b5abe0SJiri Olsa id = read_data[id_idx]; 549e2b5abe0SJiri Olsa 550e2b5abe0SJiri Olsa add: 551e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 552f8a95309SArnaldo Carvalho de Melo return 0; 553f8a95309SArnaldo Carvalho de Melo } 554f8a95309SArnaldo Carvalho de Melo 5553c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5563c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5573c659eedSAdrian Hunter int thread) 5583c659eedSAdrian Hunter { 5593c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5603c659eedSAdrian Hunter sid->idx = idx; 5613c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5623c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5633c659eedSAdrian Hunter else 5643c659eedSAdrian Hunter sid->cpu = -1; 5653c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 566e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5673c659eedSAdrian Hunter else 5683c659eedSAdrian Hunter sid->tid = -1; 5693c659eedSAdrian Hunter } 5703c659eedSAdrian Hunter 571932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 57270db7533SArnaldo Carvalho de Melo { 57370db7533SArnaldo Carvalho de Melo struct hlist_head *head; 57470db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 57570db7533SArnaldo Carvalho de Melo int hash; 57670db7533SArnaldo Carvalho de Melo 57770db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 57870db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 57970db7533SArnaldo Carvalho de Melo 580b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 58170db7533SArnaldo Carvalho de Melo if (sid->id == id) 582932a3594SJiri Olsa return sid; 583932a3594SJiri Olsa 584932a3594SJiri Olsa return NULL; 585932a3594SJiri Olsa } 586932a3594SJiri Olsa 587932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 588932a3594SJiri Olsa { 589932a3594SJiri Olsa struct perf_sample_id *sid; 590932a3594SJiri Olsa 59105169df5SAdrian Hunter if (evlist->nr_entries == 1 || !id) 592932a3594SJiri Olsa return perf_evlist__first(evlist); 593932a3594SJiri Olsa 594932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 595932a3594SJiri Olsa if (sid) 59670db7533SArnaldo Carvalho de Melo return sid->evsel; 59730e68bccSNamhyung Kim 59830e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 5990c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 60030e68bccSNamhyung Kim 60170db7533SArnaldo Carvalho de Melo return NULL; 60270db7533SArnaldo Carvalho de Melo } 60304391debSArnaldo Carvalho de Melo 604dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist, 605dddcf6abSAdrian Hunter u64 id) 606dddcf6abSAdrian Hunter { 607dddcf6abSAdrian Hunter struct perf_sample_id *sid; 608dddcf6abSAdrian Hunter 609dddcf6abSAdrian Hunter if (!id) 610dddcf6abSAdrian Hunter return NULL; 611dddcf6abSAdrian Hunter 612dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 613dddcf6abSAdrian Hunter if (sid) 614dddcf6abSAdrian Hunter return sid->evsel; 615dddcf6abSAdrian Hunter 616dddcf6abSAdrian Hunter return NULL; 617dddcf6abSAdrian Hunter } 618dddcf6abSAdrian Hunter 61975562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 62075562573SAdrian Hunter union perf_event *event, u64 *id) 62175562573SAdrian Hunter { 62275562573SAdrian Hunter const u64 *array = event->sample.array; 62375562573SAdrian Hunter ssize_t n; 62475562573SAdrian Hunter 62575562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 62675562573SAdrian Hunter 62775562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 62875562573SAdrian Hunter if (evlist->id_pos >= n) 62975562573SAdrian Hunter return -1; 63075562573SAdrian Hunter *id = array[evlist->id_pos]; 63175562573SAdrian Hunter } else { 63275562573SAdrian Hunter if (evlist->is_pos > n) 63375562573SAdrian Hunter return -1; 63475562573SAdrian Hunter n -= evlist->is_pos; 63575562573SAdrian Hunter *id = array[n]; 63675562573SAdrian Hunter } 63775562573SAdrian Hunter return 0; 63875562573SAdrian Hunter } 63975562573SAdrian Hunter 6407cb5c5acSJiri Olsa struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 64175562573SAdrian Hunter union perf_event *event) 64275562573SAdrian Hunter { 64398be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 64475562573SAdrian Hunter struct hlist_head *head; 64575562573SAdrian Hunter struct perf_sample_id *sid; 64675562573SAdrian Hunter int hash; 64775562573SAdrian Hunter u64 id; 64875562573SAdrian Hunter 64975562573SAdrian Hunter if (evlist->nr_entries == 1) 65098be6966SAdrian Hunter return first; 65198be6966SAdrian Hunter 65298be6966SAdrian Hunter if (!first->attr.sample_id_all && 65398be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 65498be6966SAdrian Hunter return first; 65575562573SAdrian Hunter 65675562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 65775562573SAdrian Hunter return NULL; 65875562573SAdrian Hunter 65975562573SAdrian Hunter /* Synthesized events have an id of zero */ 66075562573SAdrian Hunter if (!id) 66198be6966SAdrian Hunter return first; 66275562573SAdrian Hunter 66375562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 66475562573SAdrian Hunter head = &evlist->heads[hash]; 66575562573SAdrian Hunter 66675562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 66775562573SAdrian Hunter if (sid->id == id) 66875562573SAdrian Hunter return sid->evsel; 66975562573SAdrian Hunter } 67075562573SAdrian Hunter return NULL; 67175562573SAdrian Hunter } 67275562573SAdrian Hunter 67365aea233SWang Nan static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value) 67465aea233SWang Nan { 67565aea233SWang Nan int i; 67665aea233SWang Nan 677078c3386SWang Nan if (!evlist->backward_mmap) 678078c3386SWang Nan return 0; 679078c3386SWang Nan 68065aea233SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 681078c3386SWang Nan int fd = evlist->backward_mmap[i].fd; 68265aea233SWang Nan int err; 68365aea233SWang Nan 68465aea233SWang Nan if (fd < 0) 68565aea233SWang Nan continue; 68665aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 68765aea233SWang Nan if (err) 68865aea233SWang Nan return err; 68965aea233SWang Nan } 69065aea233SWang Nan return 0; 69165aea233SWang Nan } 69265aea233SWang Nan 693f6cdff83SWang Nan static int perf_evlist__pause(struct perf_evlist *evlist) 69465aea233SWang Nan { 69565aea233SWang Nan return perf_evlist__set_paused(evlist, true); 69665aea233SWang Nan } 69765aea233SWang Nan 698f6cdff83SWang Nan static int perf_evlist__resume(struct perf_evlist *evlist) 69965aea233SWang Nan { 70065aea233SWang Nan return perf_evlist__set_paused(evlist, false); 70165aea233SWang Nan } 70265aea233SWang Nan 703b6b85dadSWang Nan /* When check_messup is true, 'end' must points to a good entry */ 7040f4ccd11SWang Nan static union perf_event * 705b6b85dadSWang Nan perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start, 706b6b85dadSWang Nan u64 end, u64 *prev) 70704391debSArnaldo Carvalho de Melo { 70804391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 7098115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 710b6b85dadSWang Nan int diff = end - start; 71104391debSArnaldo Carvalho de Melo 712b6b85dadSWang Nan if (check_messup) { 71304391debSArnaldo Carvalho de Melo /* 71404391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 71504391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 71604391debSArnaldo Carvalho de Melo * 717b6b85dadSWang Nan * If we somehow ended up ahead of the 'end', we got messed up. 71804391debSArnaldo Carvalho de Melo * 719b6b85dadSWang Nan * In either case, truncate and restart at 'end'. 72004391debSArnaldo Carvalho de Melo */ 72104391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 72204391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 72304391debSArnaldo Carvalho de Melo 72404391debSArnaldo Carvalho de Melo /* 725b6b85dadSWang Nan * 'end' points to a known good entry, start there. 72604391debSArnaldo Carvalho de Melo */ 727b6b85dadSWang Nan start = end; 728b04b7023SWang Nan diff = 0; 72904391debSArnaldo Carvalho de Melo } 7307bb41152SArnaldo Carvalho de Melo } 73104391debSArnaldo Carvalho de Melo 732b04b7023SWang Nan if (diff >= (int)sizeof(event->header)) { 73304391debSArnaldo Carvalho de Melo size_t size; 73404391debSArnaldo Carvalho de Melo 735b6b85dadSWang Nan event = (union perf_event *)&data[start & md->mask]; 73604391debSArnaldo Carvalho de Melo size = event->header.size; 73704391debSArnaldo Carvalho de Melo 738b04b7023SWang Nan if (size < sizeof(event->header) || diff < (int)size) { 739b04b7023SWang Nan event = NULL; 740b04b7023SWang Nan goto broken_event; 741b04b7023SWang Nan } 742b04b7023SWang Nan 74304391debSArnaldo Carvalho de Melo /* 74404391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 74504391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 74604391debSArnaldo Carvalho de Melo */ 747b6b85dadSWang Nan if ((start & md->mask) + size != ((start + size) & md->mask)) { 748b6b85dadSWang Nan unsigned int offset = start; 74904391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 750a65cb4b9SJiri Olsa void *dst = md->event_copy; 75104391debSArnaldo Carvalho de Melo 75204391debSArnaldo Carvalho de Melo do { 75304391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 75404391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 75504391debSArnaldo Carvalho de Melo offset += cpy; 75604391debSArnaldo Carvalho de Melo dst += cpy; 75704391debSArnaldo Carvalho de Melo len -= cpy; 75804391debSArnaldo Carvalho de Melo } while (len); 75904391debSArnaldo Carvalho de Melo 760a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 76104391debSArnaldo Carvalho de Melo } 76204391debSArnaldo Carvalho de Melo 763b6b85dadSWang Nan start += size; 76404391debSArnaldo Carvalho de Melo } 76504391debSArnaldo Carvalho de Melo 766b04b7023SWang Nan broken_event: 7670f4ccd11SWang Nan if (prev) 768b6b85dadSWang Nan *prev = start; 7697bb41152SArnaldo Carvalho de Melo 77004391debSArnaldo Carvalho de Melo return event; 77104391debSArnaldo Carvalho de Melo } 772f8a95309SArnaldo Carvalho de Melo 7738db6d6b1SWang Nan union perf_event *perf_mmap__read_forward(struct perf_mmap *md, bool check_messup) 7740f4ccd11SWang Nan { 7750f4ccd11SWang Nan u64 head; 7760f4ccd11SWang Nan u64 old = md->prev; 7770f4ccd11SWang Nan 7780f4ccd11SWang Nan /* 7790f4ccd11SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 7800f4ccd11SWang Nan */ 78125a3720cSElena Reshetova if (!refcount_read(&md->refcnt)) 7820f4ccd11SWang Nan return NULL; 7830f4ccd11SWang Nan 7840f4ccd11SWang Nan head = perf_mmap__read_head(md); 7850f4ccd11SWang Nan 7868db6d6b1SWang Nan return perf_mmap__read(md, check_messup, old, head, &md->prev); 7870f4ccd11SWang Nan } 7880f4ccd11SWang Nan 789e24c7520SWang Nan union perf_event * 7908db6d6b1SWang Nan perf_mmap__read_backward(struct perf_mmap *md) 791e24c7520SWang Nan { 792e24c7520SWang Nan u64 head, end; 793e24c7520SWang Nan u64 start = md->prev; 794e24c7520SWang Nan 795e24c7520SWang Nan /* 796e24c7520SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 797e24c7520SWang Nan */ 79825a3720cSElena Reshetova if (!refcount_read(&md->refcnt)) 799e24c7520SWang Nan return NULL; 800e24c7520SWang Nan 801e24c7520SWang Nan head = perf_mmap__read_head(md); 802e24c7520SWang Nan if (!head) 803e24c7520SWang Nan return NULL; 804e24c7520SWang Nan 805e24c7520SWang Nan /* 806e24c7520SWang Nan * 'head' pointer starts from 0. Kernel minus sizeof(record) form 807e24c7520SWang Nan * it each time when kernel writes to it, so in fact 'head' is 808e24c7520SWang Nan * negative. 'end' pointer is made manually by adding the size of 809e24c7520SWang Nan * the ring buffer to 'head' pointer, means the validate data can 810e24c7520SWang Nan * read is the whole ring buffer. If 'end' is positive, the ring 811e24c7520SWang Nan * buffer has not fully filled, so we must adjust 'end' to 0. 812e24c7520SWang Nan * 813e24c7520SWang Nan * However, since both 'head' and 'end' is unsigned, we can't 814e24c7520SWang Nan * simply compare 'end' against 0. Here we compare '-head' and 815e24c7520SWang Nan * the size of the ring buffer, where -head is the number of bytes 816e24c7520SWang Nan * kernel write to the ring buffer. 817e24c7520SWang Nan */ 818e24c7520SWang Nan if (-head < (u64)(md->mask + 1)) 819e24c7520SWang Nan end = 0; 820e24c7520SWang Nan else 821e24c7520SWang Nan end = head + md->mask + 1; 822e24c7520SWang Nan 823e24c7520SWang Nan return perf_mmap__read(md, false, start, end, &md->prev); 824e24c7520SWang Nan } 825e24c7520SWang Nan 8268db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx) 8278db6d6b1SWang Nan { 8288db6d6b1SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 8298db6d6b1SWang Nan 8308db6d6b1SWang Nan /* 8318db6d6b1SWang Nan * Check messup is required for forward overwritable ring buffer: 8328db6d6b1SWang Nan * memory pointed by md->prev can be overwritten in this case. 8338db6d6b1SWang Nan * No need for read-write ring buffer: kernel stop outputting when 8348db6d6b1SWang Nan * it hit md->prev (perf_mmap__consume()). 8358db6d6b1SWang Nan */ 8368db6d6b1SWang Nan return perf_mmap__read_forward(md, evlist->overwrite); 8378db6d6b1SWang Nan } 8388db6d6b1SWang Nan 8398db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx) 8408db6d6b1SWang Nan { 8418db6d6b1SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 8428db6d6b1SWang Nan 8438db6d6b1SWang Nan /* 8448db6d6b1SWang Nan * No need to check messup for backward ring buffer: 8458db6d6b1SWang Nan * We can always read arbitrary long data from a backward 8468db6d6b1SWang Nan * ring buffer unless we forget to pause it before reading. 8478db6d6b1SWang Nan */ 8488db6d6b1SWang Nan return perf_mmap__read_backward(md); 8498db6d6b1SWang Nan } 8508db6d6b1SWang Nan 8515a5ddeb6SWang Nan union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 8525a5ddeb6SWang Nan { 8535a5ddeb6SWang Nan return perf_evlist__mmap_read_forward(evlist, idx); 8545a5ddeb6SWang Nan } 8555a5ddeb6SWang Nan 8568db6d6b1SWang Nan void perf_mmap__read_catchup(struct perf_mmap *md) 857e24c7520SWang Nan { 858e24c7520SWang Nan u64 head; 859e24c7520SWang Nan 86025a3720cSElena Reshetova if (!refcount_read(&md->refcnt)) 861e24c7520SWang Nan return; 862e24c7520SWang Nan 863e24c7520SWang Nan head = perf_mmap__read_head(md); 864e24c7520SWang Nan md->prev = head; 865e24c7520SWang Nan } 866e24c7520SWang Nan 8678db6d6b1SWang Nan void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx) 8688db6d6b1SWang Nan { 8698db6d6b1SWang Nan perf_mmap__read_catchup(&evlist->mmap[idx]); 8708db6d6b1SWang Nan } 8718db6d6b1SWang Nan 87282396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 87382396986SArnaldo Carvalho de Melo { 874b72e74d1SAdrian Hunter return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base; 87582396986SArnaldo Carvalho de Melo } 87682396986SArnaldo Carvalho de Melo 8778db6d6b1SWang Nan static void perf_mmap__get(struct perf_mmap *map) 8788db6d6b1SWang Nan { 87925a3720cSElena Reshetova refcount_inc(&map->refcnt); 8808db6d6b1SWang Nan } 8818db6d6b1SWang Nan 8828db6d6b1SWang Nan static void perf_mmap__put(struct perf_mmap *md) 8838db6d6b1SWang Nan { 88425a3720cSElena Reshetova BUG_ON(md->base && refcount_read(&md->refcnt) == 0); 8858db6d6b1SWang Nan 88625a3720cSElena Reshetova if (refcount_dec_and_test(&md->refcnt)) 8878db6d6b1SWang Nan perf_mmap__munmap(md); 8888db6d6b1SWang Nan } 8898db6d6b1SWang Nan 8908db6d6b1SWang Nan void perf_mmap__consume(struct perf_mmap *md, bool overwrite) 8918e50d384SZhouyi Zhou { 8928db6d6b1SWang Nan if (!overwrite) { 8937b8283b5SDavid Ahern u64 old = md->prev; 8948e50d384SZhouyi Zhou 8958e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 8968e50d384SZhouyi Zhou } 89782396986SArnaldo Carvalho de Melo 89825a3720cSElena Reshetova if (refcount_read(&md->refcnt) == 1 && perf_mmap__empty(md)) 8998db6d6b1SWang Nan perf_mmap__put(md); 9008db6d6b1SWang Nan } 9018db6d6b1SWang Nan 9028db6d6b1SWang Nan void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 9038db6d6b1SWang Nan { 9048db6d6b1SWang Nan perf_mmap__consume(&evlist->mmap[idx], evlist->overwrite); 9058e50d384SZhouyi Zhou } 9068e50d384SZhouyi Zhou 907718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, 908718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 909718c602dSAdrian Hunter void *userpg __maybe_unused, 910718c602dSAdrian Hunter int fd __maybe_unused) 911718c602dSAdrian Hunter { 912718c602dSAdrian Hunter return 0; 913718c602dSAdrian Hunter } 914718c602dSAdrian Hunter 915718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) 916718c602dSAdrian Hunter { 917718c602dSAdrian Hunter } 918718c602dSAdrian Hunter 919718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init( 920718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 921718c602dSAdrian Hunter off_t auxtrace_offset __maybe_unused, 922718c602dSAdrian Hunter unsigned int auxtrace_pages __maybe_unused, 923718c602dSAdrian Hunter bool auxtrace_overwrite __maybe_unused) 924718c602dSAdrian Hunter { 925718c602dSAdrian Hunter } 926718c602dSAdrian Hunter 927718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx( 928718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 929718c602dSAdrian Hunter struct perf_evlist *evlist __maybe_unused, 930718c602dSAdrian Hunter int idx __maybe_unused, 931718c602dSAdrian Hunter bool per_cpu __maybe_unused) 932718c602dSAdrian Hunter { 933718c602dSAdrian Hunter } 934718c602dSAdrian Hunter 9358db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map) 9368db6d6b1SWang Nan { 9378db6d6b1SWang Nan if (map->base != NULL) { 9388db6d6b1SWang Nan munmap(map->base, perf_mmap__mmap_len(map)); 9398db6d6b1SWang Nan map->base = NULL; 9408db6d6b1SWang Nan map->fd = -1; 94125a3720cSElena Reshetova refcount_set(&map->refcnt, 0); 9428db6d6b1SWang Nan } 9438db6d6b1SWang Nan auxtrace_mmap__munmap(&map->auxtrace_mmap); 9448db6d6b1SWang Nan } 9458db6d6b1SWang Nan 946a1f72618SWang Nan static void perf_evlist__munmap_nofree(struct perf_evlist *evlist) 947f8a95309SArnaldo Carvalho de Melo { 948aece948fSArnaldo Carvalho de Melo int i; 949f8a95309SArnaldo Carvalho de Melo 950b2cb615dSWang Nan if (evlist->mmap) 95193edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 952a1f72618SWang Nan perf_mmap__munmap(&evlist->mmap[i]); 953b2cb615dSWang Nan 954b2cb615dSWang Nan if (evlist->backward_mmap) 955b2cb615dSWang Nan for (i = 0; i < evlist->nr_mmaps; i++) 956b2cb615dSWang Nan perf_mmap__munmap(&evlist->backward_mmap[i]); 957a1f72618SWang Nan } 958aece948fSArnaldo Carvalho de Melo 959a1f72618SWang Nan void perf_evlist__munmap(struct perf_evlist *evlist) 960a1f72618SWang Nan { 961a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 96204662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 963b2cb615dSWang Nan zfree(&evlist->backward_mmap); 964f8a95309SArnaldo Carvalho de Melo } 965f8a95309SArnaldo Carvalho de Melo 9668db6d6b1SWang Nan static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist) 967f8a95309SArnaldo Carvalho de Melo { 968d4c6fb36SWang Nan int i; 9698db6d6b1SWang Nan struct perf_mmap *map; 970d4c6fb36SWang Nan 971a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 972ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 973b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 9748db6d6b1SWang Nan map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 9758db6d6b1SWang Nan if (!map) 9768db6d6b1SWang Nan return NULL; 977946ae1d4SWang Nan 9784738ca30SArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 9798db6d6b1SWang Nan map[i].fd = -1; 9804738ca30SArnaldo Carvalho de Melo /* 9814738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 9824738ca30SArnaldo Carvalho de Melo * one extra to let perf_evlist__mmap_consume() get the last 9834738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 9844738ca30SArnaldo Carvalho de Melo * dropped. 9854738ca30SArnaldo Carvalho de Melo * 9864738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 9874738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 9884738ca30SArnaldo Carvalho de Melo */ 9894738ca30SArnaldo Carvalho de Melo refcount_set(&map[i].refcnt, 0); 9904738ca30SArnaldo Carvalho de Melo } 9918db6d6b1SWang Nan return map; 992f8a95309SArnaldo Carvalho de Melo } 993f8a95309SArnaldo Carvalho de Melo 994a8a8f3ebSAdrian Hunter struct mmap_params { 995a8a8f3ebSAdrian Hunter int prot; 996a8a8f3ebSAdrian Hunter int mask; 997718c602dSAdrian Hunter struct auxtrace_mmap_params auxtrace_mp; 998a8a8f3ebSAdrian Hunter }; 999a8a8f3ebSAdrian Hunter 10008db6d6b1SWang Nan static int perf_mmap__mmap(struct perf_mmap *map, 1001a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 1002f8a95309SArnaldo Carvalho de Melo { 100382396986SArnaldo Carvalho de Melo /* 100482396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 100582396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 100682396986SArnaldo Carvalho de Melo * the ring buffer. 100782396986SArnaldo Carvalho de Melo * 100882396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 100982396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 101082396986SArnaldo Carvalho de Melo * waiting to be consumed. 101182396986SArnaldo Carvalho de Melo * 101282396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 101382396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 101482396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 101582396986SArnaldo Carvalho de Melo */ 101625a3720cSElena Reshetova refcount_set(&map->refcnt, 2); 10178db6d6b1SWang Nan map->prev = 0; 10188db6d6b1SWang Nan map->mask = mp->mask; 10198db6d6b1SWang Nan map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot, 1020f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 10218db6d6b1SWang Nan if (map->base == MAP_FAILED) { 102202635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 102302635965SAdrian Hunter errno); 10248db6d6b1SWang Nan map->base = NULL; 1025f8a95309SArnaldo Carvalho de Melo return -1; 1026301b195dSNelson Elhage } 10278db6d6b1SWang Nan map->fd = fd; 1028ad6765ddSArnaldo Carvalho de Melo 10298db6d6b1SWang Nan if (auxtrace_mmap__mmap(&map->auxtrace_mmap, 10308db6d6b1SWang Nan &mp->auxtrace_mp, map->base, fd)) 1031718c602dSAdrian Hunter return -1; 1032718c602dSAdrian Hunter 1033f8a95309SArnaldo Carvalho de Melo return 0; 1034f8a95309SArnaldo Carvalho de Melo } 1035f8a95309SArnaldo Carvalho de Melo 1036f3058a1cSWang Nan static bool 1037f3058a1cSWang Nan perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused, 1038f3058a1cSWang Nan struct perf_evsel *evsel) 1039f3058a1cSWang Nan { 104032a951b4SArnaldo Carvalho de Melo if (evsel->attr.write_backward) 1041f3058a1cSWang Nan return false; 1042f3058a1cSWang Nan return true; 1043f3058a1cSWang Nan } 1044f3058a1cSWang Nan 104504e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 10469f21b815SMark Rutland struct mmap_params *mp, int cpu_idx, 1047078c3386SWang Nan int thread, int *_output, int *_output_backward) 1048aece948fSArnaldo Carvalho de Melo { 1049aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 1050f3058a1cSWang Nan int revent; 10519f21b815SMark Rutland int evlist_cpu = cpu_map__cpu(evlist->cpus, cpu_idx); 105204e21314SAdrian Hunter 1053e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1054078c3386SWang Nan struct perf_mmap *maps = evlist->mmap; 1055078c3386SWang Nan int *output = _output; 1056bf8e8f4bSAdrian Hunter int fd; 10579f21b815SMark Rutland int cpu; 1058bf8e8f4bSAdrian Hunter 1059078c3386SWang Nan if (evsel->attr.write_backward) { 1060078c3386SWang Nan output = _output_backward; 1061078c3386SWang Nan maps = evlist->backward_mmap; 1062078c3386SWang Nan 1063078c3386SWang Nan if (!maps) { 1064078c3386SWang Nan maps = perf_evlist__alloc_mmap(evlist); 1065078c3386SWang Nan if (!maps) 1066078c3386SWang Nan return -1; 1067078c3386SWang Nan evlist->backward_mmap = maps; 106854cc54deSWang Nan if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 106954cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 1070078c3386SWang Nan } 1071078c3386SWang Nan } 1072f3058a1cSWang Nan 1073bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 1074bf8e8f4bSAdrian Hunter continue; 1075bf8e8f4bSAdrian Hunter 10769f21b815SMark Rutland cpu = cpu_map__idx(evsel->cpus, evlist_cpu); 10779f21b815SMark Rutland if (cpu == -1) 10789f21b815SMark Rutland continue; 10799f21b815SMark Rutland 1080bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 108104e21314SAdrian Hunter 108204e21314SAdrian Hunter if (*output == -1) { 108304e21314SAdrian Hunter *output = fd; 1084078c3386SWang Nan 1085078c3386SWang Nan if (perf_mmap__mmap(&maps[idx], mp, *output) < 0) 108604e21314SAdrian Hunter return -1; 108704e21314SAdrian Hunter } else { 108804e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 108904e21314SAdrian Hunter return -1; 109082396986SArnaldo Carvalho de Melo 1091078c3386SWang Nan perf_mmap__get(&maps[idx]); 109204e21314SAdrian Hunter } 109304e21314SAdrian Hunter 1094f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 1095f3058a1cSWang Nan 1096f90d194aSAdrian Hunter /* 1097f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 1098f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 1099f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 1100f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 1101f90d194aSAdrian Hunter * Therefore don't add it for polling. 1102f90d194aSAdrian Hunter */ 1103f90d194aSAdrian Hunter if (!evsel->system_wide && 1104078c3386SWang Nan __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) { 1105078c3386SWang Nan perf_mmap__put(&maps[idx]); 1106ad6765ddSArnaldo Carvalho de Melo return -1; 110782396986SArnaldo Carvalho de Melo } 1108033fa713SArnaldo Carvalho de Melo 11093c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 11103c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 11113c659eedSAdrian Hunter fd) < 0) 111204e21314SAdrian Hunter return -1; 11133c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 11143c659eedSAdrian Hunter thread); 11153c659eedSAdrian Hunter } 111604e21314SAdrian Hunter } 111704e21314SAdrian Hunter 111804e21314SAdrian Hunter return 0; 111904e21314SAdrian Hunter } 112004e21314SAdrian Hunter 1121a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 1122a8a8f3ebSAdrian Hunter struct mmap_params *mp) 112304e21314SAdrian Hunter { 1124aece948fSArnaldo Carvalho de Melo int cpu, thread; 1125b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 1126b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 1127aece948fSArnaldo Carvalho de Melo 1128e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 1129b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 1130aece948fSArnaldo Carvalho de Melo int output = -1; 1131078c3386SWang Nan int output_backward = -1; 1132aece948fSArnaldo Carvalho de Melo 1133718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 1134718c602dSAdrian Hunter true); 1135718c602dSAdrian Hunter 1136b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 1137a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 1138078c3386SWang Nan thread, &output, &output_backward)) 1139aece948fSArnaldo Carvalho de Melo goto out_unmap; 1140aece948fSArnaldo Carvalho de Melo } 1141aece948fSArnaldo Carvalho de Melo } 1142aece948fSArnaldo Carvalho de Melo 1143aece948fSArnaldo Carvalho de Melo return 0; 1144aece948fSArnaldo Carvalho de Melo 1145aece948fSArnaldo Carvalho de Melo out_unmap: 1146a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 1147aece948fSArnaldo Carvalho de Melo return -1; 1148aece948fSArnaldo Carvalho de Melo } 1149aece948fSArnaldo Carvalho de Melo 1150a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 1151a8a8f3ebSAdrian Hunter struct mmap_params *mp) 1152aece948fSArnaldo Carvalho de Melo { 1153aece948fSArnaldo Carvalho de Melo int thread; 1154b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 1155aece948fSArnaldo Carvalho de Melo 1156e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 1157b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 1158aece948fSArnaldo Carvalho de Melo int output = -1; 1159078c3386SWang Nan int output_backward = -1; 1160aece948fSArnaldo Carvalho de Melo 1161718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 1162718c602dSAdrian Hunter false); 1163718c602dSAdrian Hunter 1164a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 1165078c3386SWang Nan &output, &output_backward)) 1166aece948fSArnaldo Carvalho de Melo goto out_unmap; 1167aece948fSArnaldo Carvalho de Melo } 1168aece948fSArnaldo Carvalho de Melo 1169aece948fSArnaldo Carvalho de Melo return 0; 1170aece948fSArnaldo Carvalho de Melo 1171aece948fSArnaldo Carvalho de Melo out_unmap: 1172a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 1173aece948fSArnaldo Carvalho de Melo return -1; 1174aece948fSArnaldo Carvalho de Melo } 1175aece948fSArnaldo Carvalho de Melo 1176f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 1177994a1f78SJiri Olsa { 1178f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 11798185e881SArnaldo Carvalho de Melo int max; 11808185e881SArnaldo Carvalho de Melo 11818185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 11828185e881SArnaldo Carvalho de Melo /* 11838185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 11848185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 11858185e881SArnaldo Carvalho de Melo * die yet... 11868185e881SArnaldo Carvalho de Melo */ 11878185e881SArnaldo Carvalho de Melo max = 512; 11888185e881SArnaldo Carvalho de Melo } else { 11898185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 11908185e881SArnaldo Carvalho de Melo } 11918185e881SArnaldo Carvalho de Melo 11928185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 11931be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 11941be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 1195f5e7150cSArnaldo Carvalho de Melo 1196f5e7150cSArnaldo Carvalho de Melo return pages; 1197f5e7150cSArnaldo Carvalho de Melo } 1198f5e7150cSArnaldo Carvalho de Melo 11990c582449SJiri Olsa size_t perf_evlist__mmap_size(unsigned long pages) 1200f5e7150cSArnaldo Carvalho de Melo { 1201f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 1202f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 1203f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 1204994a1f78SJiri Olsa return 0; 1205994a1f78SJiri Olsa 1206994a1f78SJiri Olsa return (pages + 1) * page_size; 1207994a1f78SJiri Olsa } 1208994a1f78SJiri Olsa 120933c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 121033c2dcfdSDavid Ahern unsigned long max) 1211994a1f78SJiri Olsa { 12122fbe4abeSAdrian Hunter unsigned long pages, val; 121327050f53SJiri Olsa static struct parse_tag tags[] = { 121427050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 121527050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 121627050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 121727050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 121827050f53SJiri Olsa { .tag = 0 }, 121927050f53SJiri Olsa }; 1220994a1f78SJiri Olsa 12218973504bSDavid Ahern if (str == NULL) 122233c2dcfdSDavid Ahern return -EINVAL; 12238973504bSDavid Ahern 122427050f53SJiri Olsa val = parse_tag_value(str, tags); 12252fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 122627050f53SJiri Olsa /* we got file size value */ 122727050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 122827050f53SJiri Olsa } else { 122927050f53SJiri Olsa /* we got pages count value */ 123027050f53SJiri Olsa char *eptr; 1231994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 123233c2dcfdSDavid Ahern if (*eptr != '\0') 123333c2dcfdSDavid Ahern return -EINVAL; 123427050f53SJiri Olsa } 1235994a1f78SJiri Olsa 12362bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 123733c2dcfdSDavid Ahern /* leave number of pages at 0 */ 12381dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 12399808143bSJiri Olsa char buf[100]; 12409808143bSJiri Olsa 124133c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 124291529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 12431dbfa938SAdrian Hunter if (!pages) 12441dbfa938SAdrian Hunter return -EINVAL; 12459808143bSJiri Olsa 12469808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 12479808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 12489808143bSJiri Olsa buf, pages); 12492fbe4abeSAdrian Hunter } 12502fbe4abeSAdrian Hunter 125133c2dcfdSDavid Ahern if (pages > max) 125233c2dcfdSDavid Ahern return -EINVAL; 125333c2dcfdSDavid Ahern 125433c2dcfdSDavid Ahern return pages; 125533c2dcfdSDavid Ahern } 125633c2dcfdSDavid Ahern 1257e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 125833c2dcfdSDavid Ahern { 125933c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 126033c2dcfdSDavid Ahern long pages; 126133c2dcfdSDavid Ahern 1262f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 126333c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 126433c2dcfdSDavid Ahern 126533c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 126633c2dcfdSDavid Ahern if (pages < 0) { 126733c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 1268994a1f78SJiri Olsa return -1; 1269994a1f78SJiri Olsa } 1270994a1f78SJiri Olsa 1271994a1f78SJiri Olsa *mmap_pages = pages; 1272994a1f78SJiri Olsa return 0; 1273994a1f78SJiri Olsa } 1274994a1f78SJiri Olsa 1275e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 1276e9db1310SAdrian Hunter int unset __maybe_unused) 1277e9db1310SAdrian Hunter { 1278e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 1279e9db1310SAdrian Hunter } 1280e9db1310SAdrian Hunter 1281c83fa7f2SAdrian Hunter /** 1282718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 1283c83fa7f2SAdrian Hunter * @evlist: list of events 1284c83fa7f2SAdrian Hunter * @pages: map length in pages 1285c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 1286718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 1287718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1288f8a95309SArnaldo Carvalho de Melo * 1289c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1290c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1291c83fa7f2SAdrian Hunter * automatically. 1292f8a95309SArnaldo Carvalho de Melo * 1293718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1294718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1295718c602dSAdrian Hunter * 1296c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1297f8a95309SArnaldo Carvalho de Melo */ 1298718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, 1299718c602dSAdrian Hunter bool overwrite, unsigned int auxtrace_pages, 1300718c602dSAdrian Hunter bool auxtrace_overwrite) 1301f8a95309SArnaldo Carvalho de Melo { 1302aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 13037e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 13047e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1305a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1306a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1307a8a8f3ebSAdrian Hunter }; 130850a682ceSArnaldo Carvalho de Melo 13098db6d6b1SWang Nan if (!evlist->mmap) 13108db6d6b1SWang Nan evlist->mmap = perf_evlist__alloc_mmap(evlist); 13118db6d6b1SWang Nan if (!evlist->mmap) 1312f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1313f8a95309SArnaldo Carvalho de Melo 13141b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1315f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1316f8a95309SArnaldo Carvalho de Melo 1317f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1318994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 13192af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1320a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1321f8a95309SArnaldo Carvalho de Melo 1322718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1323718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1324718c602dSAdrian Hunter 1325e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1326f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1327a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1328a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1329f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1330f8a95309SArnaldo Carvalho de Melo } 1331f8a95309SArnaldo Carvalho de Melo 1332ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1333a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1334f8a95309SArnaldo Carvalho de Melo 1335a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1336f8a95309SArnaldo Carvalho de Melo } 13377e2ed097SArnaldo Carvalho de Melo 1338718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 1339718c602dSAdrian Hunter bool overwrite) 1340718c602dSAdrian Hunter { 1341718c602dSAdrian Hunter return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false); 1342718c602dSAdrian Hunter } 1343718c602dSAdrian Hunter 1344602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 13457e2ed097SArnaldo Carvalho de Melo { 134674bfd2b2SAdrian Hunter struct cpu_map *cpus; 134774bfd2b2SAdrian Hunter struct thread_map *threads; 13487e2ed097SArnaldo Carvalho de Melo 134974bfd2b2SAdrian Hunter threads = thread_map__new_str(target->pid, target->tid, target->uid); 135074bfd2b2SAdrian Hunter 135174bfd2b2SAdrian Hunter if (!threads) 13527e2ed097SArnaldo Carvalho de Melo return -1; 13537e2ed097SArnaldo Carvalho de Melo 13549c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 135574bfd2b2SAdrian Hunter cpus = cpu_map__dummy_new(); 1356879d77d0SNamhyung Kim else 135774bfd2b2SAdrian Hunter cpus = cpu_map__new(target->cpu_list); 13587e2ed097SArnaldo Carvalho de Melo 135974bfd2b2SAdrian Hunter if (!cpus) 13607e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 13617e2ed097SArnaldo Carvalho de Melo 1362ec9a77a7SAdrian Hunter evlist->has_user_cpus = !!target->cpu_list; 1363ec9a77a7SAdrian Hunter 136474bfd2b2SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 1365d5bc056eSAdrian Hunter 1366d5bc056eSAdrian Hunter return 0; 13677e2ed097SArnaldo Carvalho de Melo 13687e2ed097SArnaldo Carvalho de Melo out_delete_threads: 136974bfd2b2SAdrian Hunter thread_map__put(threads); 13707e2ed097SArnaldo Carvalho de Melo return -1; 13717e2ed097SArnaldo Carvalho de Melo } 13727e2ed097SArnaldo Carvalho de Melo 1373d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, 13743de5cfb0SJiri Olsa struct thread_map *threads) 13753de5cfb0SJiri Olsa { 1376934e0f20SAdrian Hunter /* 1377934e0f20SAdrian Hunter * Allow for the possibility that one or another of the maps isn't being 1378934e0f20SAdrian Hunter * changed i.e. don't put it. Note we are assuming the maps that are 1379934e0f20SAdrian Hunter * being applied are brand new and evlist is taking ownership of the 1380934e0f20SAdrian Hunter * original reference count of 1. If that is not the case it is up to 1381934e0f20SAdrian Hunter * the caller to increase the reference count. 1382934e0f20SAdrian Hunter */ 1383934e0f20SAdrian Hunter if (cpus != evlist->cpus) { 13843de5cfb0SJiri Olsa cpu_map__put(evlist->cpus); 1385a55e5663SArnaldo Carvalho de Melo evlist->cpus = cpu_map__get(cpus); 1386934e0f20SAdrian Hunter } 13873de5cfb0SJiri Olsa 1388934e0f20SAdrian Hunter if (threads != evlist->threads) { 13893de5cfb0SJiri Olsa thread_map__put(evlist->threads); 1390a55e5663SArnaldo Carvalho de Melo evlist->threads = thread_map__get(threads); 1391934e0f20SAdrian Hunter } 13923de5cfb0SJiri Olsa 1393ec9a77a7SAdrian Hunter perf_evlist__propagate_maps(evlist); 13943de5cfb0SJiri Olsa } 13953de5cfb0SJiri Olsa 139622c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist, 139722c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 139822c8a376SArnaldo Carvalho de Melo { 139922c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 140022c8a376SArnaldo Carvalho de Melo 1401e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 140222c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 140322c8a376SArnaldo Carvalho de Melo } 140422c8a376SArnaldo Carvalho de Melo 140522c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist, 140622c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 140722c8a376SArnaldo Carvalho de Melo { 140822c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 140922c8a376SArnaldo Carvalho de Melo 1410e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 141122c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 141222c8a376SArnaldo Carvalho de Melo } 141322c8a376SArnaldo Carvalho de Melo 141423d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 14150a102479SFrederic Weisbecker { 14160a102479SFrederic Weisbecker struct perf_evsel *evsel; 1417745cefc5SArnaldo Carvalho de Melo int err = 0; 1418745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1419b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 14200a102479SFrederic Weisbecker 1421e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1422745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 14230a102479SFrederic Weisbecker continue; 1424745cefc5SArnaldo Carvalho de Melo 1425d988d5eeSKan Liang /* 1426d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1427d988d5eeSKan Liang * So evlist and evsel should always be same. 1428d988d5eeSKan Liang */ 1429f47805a2SArnaldo Carvalho de Melo err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter); 143023d4aad4SArnaldo Carvalho de Melo if (err) { 143123d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1432745cefc5SArnaldo Carvalho de Melo break; 14330a102479SFrederic Weisbecker } 143423d4aad4SArnaldo Carvalho de Melo } 14350a102479SFrederic Weisbecker 1436745cefc5SArnaldo Carvalho de Melo return err; 1437745cefc5SArnaldo Carvalho de Melo } 1438745cefc5SArnaldo Carvalho de Melo 1439745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1440745cefc5SArnaldo Carvalho de Melo { 1441745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1442745cefc5SArnaldo Carvalho de Melo int err = 0; 1443745cefc5SArnaldo Carvalho de Melo 1444e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1445fdf14720SWang Nan if (evsel->attr.type != PERF_TYPE_TRACEPOINT) 1446fdf14720SWang Nan continue; 1447fdf14720SWang Nan 144894ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1449745cefc5SArnaldo Carvalho de Melo if (err) 1450745cefc5SArnaldo Carvalho de Melo break; 1451745cefc5SArnaldo Carvalho de Melo } 1452745cefc5SArnaldo Carvalho de Melo 1453745cefc5SArnaldo Carvalho de Melo return err; 14540a102479SFrederic Weisbecker } 145574429964SFrederic Weisbecker 1456be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1457cfd70a26SArnaldo Carvalho de Melo { 1458cfd70a26SArnaldo Carvalho de Melo char *filter; 1459be199adaSArnaldo Carvalho de Melo int ret = -1; 1460be199adaSArnaldo Carvalho de Melo size_t i; 1461cfd70a26SArnaldo Carvalho de Melo 1462be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1463be199adaSArnaldo Carvalho de Melo if (i == 0) { 1464be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1465cfd70a26SArnaldo Carvalho de Melo return -1; 1466be199adaSArnaldo Carvalho de Melo } else { 1467be199adaSArnaldo Carvalho de Melo char *tmp; 1468be199adaSArnaldo Carvalho de Melo 1469be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1470be199adaSArnaldo Carvalho de Melo goto out_free; 1471be199adaSArnaldo Carvalho de Melo 1472be199adaSArnaldo Carvalho de Melo free(filter); 1473be199adaSArnaldo Carvalho de Melo filter = tmp; 1474be199adaSArnaldo Carvalho de Melo } 1475be199adaSArnaldo Carvalho de Melo } 1476cfd70a26SArnaldo Carvalho de Melo 1477cfd70a26SArnaldo Carvalho de Melo ret = perf_evlist__set_filter(evlist, filter); 1478be199adaSArnaldo Carvalho de Melo out_free: 1479cfd70a26SArnaldo Carvalho de Melo free(filter); 1480cfd70a26SArnaldo Carvalho de Melo return ret; 1481cfd70a26SArnaldo Carvalho de Melo } 1482cfd70a26SArnaldo Carvalho de Melo 1483be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) 1484be199adaSArnaldo Carvalho de Melo { 1485be199adaSArnaldo Carvalho de Melo return perf_evlist__set_filter_pids(evlist, 1, &pid); 1486be199adaSArnaldo Carvalho de Melo } 1487be199adaSArnaldo Carvalho de Melo 14880c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 148974429964SFrederic Weisbecker { 149075562573SAdrian Hunter struct perf_evsel *pos; 1491c2a70653SArnaldo Carvalho de Melo 149275562573SAdrian Hunter if (evlist->nr_entries == 1) 149375562573SAdrian Hunter return true; 149475562573SAdrian Hunter 149575562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 149675562573SAdrian Hunter return false; 149775562573SAdrian Hunter 1498e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 149975562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 150075562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1501c2a70653SArnaldo Carvalho de Melo return false; 150274429964SFrederic Weisbecker } 150374429964SFrederic Weisbecker 1504c2a70653SArnaldo Carvalho de Melo return true; 1505c2a70653SArnaldo Carvalho de Melo } 1506c2a70653SArnaldo Carvalho de Melo 150775562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1508c2a70653SArnaldo Carvalho de Melo { 150975562573SAdrian Hunter struct perf_evsel *evsel; 151075562573SAdrian Hunter 151175562573SAdrian Hunter if (evlist->combined_sample_type) 151275562573SAdrian Hunter return evlist->combined_sample_type; 151375562573SAdrian Hunter 1514e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 151575562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 151675562573SAdrian Hunter 151775562573SAdrian Hunter return evlist->combined_sample_type; 151875562573SAdrian Hunter } 151975562573SAdrian Hunter 152075562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 152175562573SAdrian Hunter { 152275562573SAdrian Hunter evlist->combined_sample_type = 0; 152375562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1524c2a70653SArnaldo Carvalho de Melo } 1525c2a70653SArnaldo Carvalho de Melo 152698df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) 152798df858eSAndi Kleen { 152898df858eSAndi Kleen struct perf_evsel *evsel; 152998df858eSAndi Kleen u64 branch_type = 0; 153098df858eSAndi Kleen 1531e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 153298df858eSAndi Kleen branch_type |= evsel->attr.branch_sample_type; 153398df858eSAndi Kleen return branch_type; 153498df858eSAndi Kleen } 153598df858eSAndi Kleen 15369ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 15379ede473cSJiri Olsa { 15389ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 15399ede473cSJiri Olsa u64 read_format = first->attr.read_format; 15409ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 15419ede473cSJiri Olsa 1542e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 15439ede473cSJiri Olsa if (read_format != pos->attr.read_format) 15449ede473cSJiri Olsa return false; 15459ede473cSJiri Olsa } 15469ede473cSJiri Olsa 15479ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 15489ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 15499ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 15509ede473cSJiri Olsa return false; 15519ede473cSJiri Olsa } 15529ede473cSJiri Olsa 15539ede473cSJiri Olsa return true; 15549ede473cSJiri Olsa } 15559ede473cSJiri Olsa 15569ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 15579ede473cSJiri Olsa { 15589ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 15599ede473cSJiri Olsa return first->attr.read_format; 15609ede473cSJiri Olsa } 15619ede473cSJiri Olsa 15620c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 156381e36bffSArnaldo Carvalho de Melo { 15640c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 156581e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 156681e36bffSArnaldo Carvalho de Melo u64 sample_type; 156781e36bffSArnaldo Carvalho de Melo u16 size = 0; 156881e36bffSArnaldo Carvalho de Melo 156981e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 157081e36bffSArnaldo Carvalho de Melo goto out; 157181e36bffSArnaldo Carvalho de Melo 157281e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 157381e36bffSArnaldo Carvalho de Melo 157481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 157581e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 157681e36bffSArnaldo Carvalho de Melo 157781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 157881e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 157981e36bffSArnaldo Carvalho de Melo 158081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 158181e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 158281e36bffSArnaldo Carvalho de Melo 158381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 158481e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 158581e36bffSArnaldo Carvalho de Melo 158681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 158781e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 158875562573SAdrian Hunter 158975562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 159075562573SAdrian Hunter size += sizeof(data->id); 159181e36bffSArnaldo Carvalho de Melo out: 159281e36bffSArnaldo Carvalho de Melo return size; 159381e36bffSArnaldo Carvalho de Melo } 159481e36bffSArnaldo Carvalho de Melo 15950c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1596c2a70653SArnaldo Carvalho de Melo { 15970c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1598c2a70653SArnaldo Carvalho de Melo 1599e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 1600c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1601c2a70653SArnaldo Carvalho de Melo return false; 1602c2a70653SArnaldo Carvalho de Melo } 1603c2a70653SArnaldo Carvalho de Melo 1604c2a70653SArnaldo Carvalho de Melo return true; 160574429964SFrederic Weisbecker } 160674429964SFrederic Weisbecker 16070c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 160874429964SFrederic Weisbecker { 16090c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1610c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 161174429964SFrederic Weisbecker } 161281cce8deSArnaldo Carvalho de Melo 161381cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 161481cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 161581cce8deSArnaldo Carvalho de Melo { 161681cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 161781cce8deSArnaldo Carvalho de Melo } 1618727ab04eSArnaldo Carvalho de Melo 1619a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1620a74b4b66SNamhyung Kim { 1621a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1622a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1623a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 1624a74b4b66SNamhyung Kim 1625e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_reverse(evlist, evsel) { 162618ef15c6SArnaldo Carvalho de Melo int n = evsel->cpus ? evsel->cpus->nr : ncpus; 16278ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 16288ad9219eSStephane Eranian } 1629a74b4b66SNamhyung Kim } 1630a74b4b66SNamhyung Kim 16314112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 16324112eb18SArnaldo Carvalho de Melo { 16338c0498b6SAdrian Hunter struct cpu_map *cpus; 16348c0498b6SAdrian Hunter struct thread_map *threads; 16354112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 16364112eb18SArnaldo Carvalho de Melo 16374112eb18SArnaldo Carvalho de Melo /* 16384112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 16394112eb18SArnaldo Carvalho de Melo * an all cpus map. 16404112eb18SArnaldo Carvalho de Melo * 16414112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 16424112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 16434112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 16444112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 16454112eb18SArnaldo Carvalho de Melo */ 16468c0498b6SAdrian Hunter cpus = cpu_map__new(NULL); 16478c0498b6SAdrian Hunter if (!cpus) 16484112eb18SArnaldo Carvalho de Melo goto out; 16494112eb18SArnaldo Carvalho de Melo 16508c0498b6SAdrian Hunter threads = thread_map__new_dummy(); 16518c0498b6SAdrian Hunter if (!threads) 16528c0498b6SAdrian Hunter goto out_put; 16534112eb18SArnaldo Carvalho de Melo 16548c0498b6SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 16554112eb18SArnaldo Carvalho de Melo out: 16564112eb18SArnaldo Carvalho de Melo return err; 16578c0498b6SAdrian Hunter out_put: 16588c0498b6SAdrian Hunter cpu_map__put(cpus); 16594112eb18SArnaldo Carvalho de Melo goto out; 16604112eb18SArnaldo Carvalho de Melo } 16614112eb18SArnaldo Carvalho de Melo 16626a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1663727ab04eSArnaldo Carvalho de Melo { 16646a4bb04cSJiri Olsa struct perf_evsel *evsel; 1665a74b4b66SNamhyung Kim int err; 1666727ab04eSArnaldo Carvalho de Melo 16674112eb18SArnaldo Carvalho de Melo /* 16684112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 16694112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 16704112eb18SArnaldo Carvalho de Melo */ 16714112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 16724112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 16734112eb18SArnaldo Carvalho de Melo if (err < 0) 16744112eb18SArnaldo Carvalho de Melo goto out_err; 16754112eb18SArnaldo Carvalho de Melo } 16764112eb18SArnaldo Carvalho de Melo 1677733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1678733cd2feSAdrian Hunter 1679e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 168023df7f79SAdrian Hunter err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); 1681727ab04eSArnaldo Carvalho de Melo if (err < 0) 1682727ab04eSArnaldo Carvalho de Melo goto out_err; 1683727ab04eSArnaldo Carvalho de Melo } 1684727ab04eSArnaldo Carvalho de Melo 1685727ab04eSArnaldo Carvalho de Melo return 0; 1686727ab04eSArnaldo Carvalho de Melo out_err: 1687a74b4b66SNamhyung Kim perf_evlist__close(evlist); 168841c21a68SNamhyung Kim errno = -err; 1689727ab04eSArnaldo Carvalho de Melo return err; 1690727ab04eSArnaldo Carvalho de Melo } 169135b9d88eSArnaldo Carvalho de Melo 1692602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 169355e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1694735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 169535b9d88eSArnaldo Carvalho de Melo { 169635b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 169735b9d88eSArnaldo Carvalho de Melo char bf; 169835b9d88eSArnaldo Carvalho de Melo 169935b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 170035b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 170135b9d88eSArnaldo Carvalho de Melo return -1; 170235b9d88eSArnaldo Carvalho de Melo } 170335b9d88eSArnaldo Carvalho de Melo 170435b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 170535b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 170635b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 170735b9d88eSArnaldo Carvalho de Melo } 170835b9d88eSArnaldo Carvalho de Melo 170935b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 171035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 171135b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 171235b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 171335b9d88eSArnaldo Carvalho de Melo } 171435b9d88eSArnaldo Carvalho de Melo 171535b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 17165f1c4225SArnaldo Carvalho de Melo int ret; 17175f1c4225SArnaldo Carvalho de Melo 1718119fa3c9SNamhyung Kim if (pipe_output) 171935b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 172035b9d88eSArnaldo Carvalho de Melo 17210817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 17220817df08SDavid Ahern 172335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 172435b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 172535b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 172635b9d88eSArnaldo Carvalho de Melo 172735b9d88eSArnaldo Carvalho de Melo /* 172835b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 172935b9d88eSArnaldo Carvalho de Melo */ 173035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 173135b9d88eSArnaldo Carvalho de Melo 173235b9d88eSArnaldo Carvalho de Melo /* 173335b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 173435b9d88eSArnaldo Carvalho de Melo */ 17355f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 17365f1c4225SArnaldo Carvalho de Melo /* 17375f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 17385f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 17395f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 17405f1c4225SArnaldo Carvalho de Melo * 174120f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 17425f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 17435f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 17445f1c4225SArnaldo Carvalho de Melo * here. 17455f1c4225SArnaldo Carvalho de Melo */ 17465f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 17475f1c4225SArnaldo Carvalho de Melo if (ret == -1) 174835b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 17495f1c4225SArnaldo Carvalho de Melo exit(ret); 17505f1c4225SArnaldo Carvalho de Melo } 175135b9d88eSArnaldo Carvalho de Melo 175235b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 175335b9d88eSArnaldo Carvalho de Melo 1754735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1755f33cbe72SArnaldo Carvalho de Melo union sigval val; 1756f33cbe72SArnaldo Carvalho de Melo 1757f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1758f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 175935b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1760f33cbe72SArnaldo Carvalho de Melo } else 1761f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 176235b9d88eSArnaldo Carvalho de Melo exit(-1); 176335b9d88eSArnaldo Carvalho de Melo } 176435b9d88eSArnaldo Carvalho de Melo 1765735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1766735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1767735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1768735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1769735f7e0bSArnaldo Carvalho de Melo }; 1770735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1771735f7e0bSArnaldo Carvalho de Melo } 1772735f7e0bSArnaldo Carvalho de Melo 17731aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 17741aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 17751aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 17761aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 17771aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 17781aaf63b1SArnaldo Carvalho de Melo } 1779e13798c7SJiri Olsa thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 17801aaf63b1SArnaldo Carvalho de Melo } 178135b9d88eSArnaldo Carvalho de Melo 178235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 178335b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 178435b9d88eSArnaldo Carvalho de Melo /* 178535b9d88eSArnaldo Carvalho de Melo * wait for child to settle 178635b9d88eSArnaldo Carvalho de Melo */ 178735b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 178835b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 178935b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 179035b9d88eSArnaldo Carvalho de Melo } 179135b9d88eSArnaldo Carvalho de Melo 1792bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 179335b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 179435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 179535b9d88eSArnaldo Carvalho de Melo return 0; 179635b9d88eSArnaldo Carvalho de Melo 179735b9d88eSArnaldo Carvalho de Melo out_close_pipes: 179835b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 179935b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 180035b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 180135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 180235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 180335b9d88eSArnaldo Carvalho de Melo return -1; 180435b9d88eSArnaldo Carvalho de Melo } 180535b9d88eSArnaldo Carvalho de Melo 180635b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 180735b9d88eSArnaldo Carvalho de Melo { 180835b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1809b3824404SDavid Ahern char bf = 0; 1810bcf3145fSNamhyung Kim int ret; 181135b9d88eSArnaldo Carvalho de Melo /* 181235b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 181335b9d88eSArnaldo Carvalho de Melo */ 1814bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1815bcf3145fSNamhyung Kim if (ret < 0) 1816e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1817bcf3145fSNamhyung Kim 1818bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1819bcf3145fSNamhyung Kim return ret; 182035b9d88eSArnaldo Carvalho de Melo } 182135b9d88eSArnaldo Carvalho de Melo 182235b9d88eSArnaldo Carvalho de Melo return 0; 182335b9d88eSArnaldo Carvalho de Melo } 1824cb0b29e0SArnaldo Carvalho de Melo 1825a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 18260807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1827cb0b29e0SArnaldo Carvalho de Melo { 182875562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 182975562573SAdrian Hunter 183075562573SAdrian Hunter if (!evsel) 183175562573SAdrian Hunter return -EFAULT; 18320807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1833cb0b29e0SArnaldo Carvalho de Melo } 183478f067b3SArnaldo Carvalho de Melo 183578f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 183678f067b3SArnaldo Carvalho de Melo { 183778f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 183878f067b3SArnaldo Carvalho de Melo size_t printed = 0; 183978f067b3SArnaldo Carvalho de Melo 1840e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 184178f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 184278f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 184378f067b3SArnaldo Carvalho de Melo } 184478f067b3SArnaldo Carvalho de Melo 1845b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 184678f067b3SArnaldo Carvalho de Melo } 18476ef068cbSArnaldo Carvalho de Melo 1848d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist, 1849a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1850a8f23d8fSArnaldo Carvalho de Melo { 1851a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1852c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1853a8f23d8fSArnaldo Carvalho de Melo 1854a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1855a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1856a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1857a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1858a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1859a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1860a8f23d8fSArnaldo Carvalho de Melo 18611a47245dSAdrian Hunter value = perf_event_paranoid(); 1862a8f23d8fSArnaldo Carvalho de Melo 1863a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1864a8f23d8fSArnaldo Carvalho de Melo 1865a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1866a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1867a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1868a8f23d8fSArnaldo Carvalho de Melo } 1869a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 18705229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1871a8f23d8fSArnaldo Carvalho de Melo 1872a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 18735229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 18745229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1875a8f23d8fSArnaldo Carvalho de Melo break; 1876d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1877d9aade7fSArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1878d9aade7fSArnaldo Carvalho de Melo int max_freq; 1879d9aade7fSArnaldo Carvalho de Melo 1880d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1881d9aade7fSArnaldo Carvalho de Melo goto out_default; 1882d9aade7fSArnaldo Carvalho de Melo 1883d9aade7fSArnaldo Carvalho de Melo if (first->attr.sample_freq < (u64)max_freq) 1884d9aade7fSArnaldo Carvalho de Melo goto out_default; 1885d9aade7fSArnaldo Carvalho de Melo 1886d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1887d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1888d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1889d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1890d9aade7fSArnaldo Carvalho de Melo emsg, max_freq, first->attr.sample_freq); 1891d9aade7fSArnaldo Carvalho de Melo break; 1892d9aade7fSArnaldo Carvalho de Melo } 1893a8f23d8fSArnaldo Carvalho de Melo default: 1894d9aade7fSArnaldo Carvalho de Melo out_default: 1895a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1896a8f23d8fSArnaldo Carvalho de Melo break; 1897a8f23d8fSArnaldo Carvalho de Melo } 1898a8f23d8fSArnaldo Carvalho de Melo 1899a8f23d8fSArnaldo Carvalho de Melo return 0; 1900a8f23d8fSArnaldo Carvalho de Melo } 1901a025e4f0SAdrian Hunter 1902956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1903956fa571SArnaldo Carvalho de Melo { 1904c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1905e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1906956fa571SArnaldo Carvalho de Melo 1907956fa571SArnaldo Carvalho de Melo switch (err) { 1908956fa571SArnaldo Carvalho de Melo case EPERM: 1909e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1910e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1911e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1912956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1913e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1914e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1915e965bea1SArnaldo Carvalho de Melo 1916e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1917e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1918e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1919e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1920e965bea1SArnaldo Carvalho de Melo } 1921e965bea1SArnaldo Carvalho de Melo 1922e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1923e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1924956fa571SArnaldo Carvalho de Melo break; 1925956fa571SArnaldo Carvalho de Melo default: 1926956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1927956fa571SArnaldo Carvalho de Melo break; 1928956fa571SArnaldo Carvalho de Melo } 1929956fa571SArnaldo Carvalho de Melo 1930956fa571SArnaldo Carvalho de Melo return 0; 1931956fa571SArnaldo Carvalho de Melo } 1932956fa571SArnaldo Carvalho de Melo 1933a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1934a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1935a025e4f0SAdrian Hunter { 1936a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1937a025e4f0SAdrian Hunter LIST_HEAD(move); 1938a025e4f0SAdrian Hunter 1939a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1940a025e4f0SAdrian Hunter return; 1941a025e4f0SAdrian Hunter 1942e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1943a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1944a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1945a025e4f0SAdrian Hunter } 1946a025e4f0SAdrian Hunter 1947a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1948a025e4f0SAdrian Hunter } 194960b0896cSAdrian Hunter 195060b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 195160b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 195260b0896cSAdrian Hunter { 195360b0896cSAdrian Hunter struct perf_evsel *evsel; 195460b0896cSAdrian Hunter 195560b0896cSAdrian Hunter if (tracking_evsel->tracking) 195660b0896cSAdrian Hunter return; 195760b0896cSAdrian Hunter 1958e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 195960b0896cSAdrian Hunter if (evsel != tracking_evsel) 196060b0896cSAdrian Hunter evsel->tracking = false; 196160b0896cSAdrian Hunter } 196260b0896cSAdrian Hunter 196360b0896cSAdrian Hunter tracking_evsel->tracking = true; 196460b0896cSAdrian Hunter } 19657630b3e2SWang Nan 19667630b3e2SWang Nan struct perf_evsel * 19677630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, 19687630b3e2SWang Nan const char *str) 19697630b3e2SWang Nan { 19707630b3e2SWang Nan struct perf_evsel *evsel; 19717630b3e2SWang Nan 1972e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 19737630b3e2SWang Nan if (!evsel->name) 19747630b3e2SWang Nan continue; 19757630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 19767630b3e2SWang Nan return evsel; 19777630b3e2SWang Nan } 19787630b3e2SWang Nan 19797630b3e2SWang Nan return NULL; 19807630b3e2SWang Nan } 198154cc54deSWang Nan 198254cc54deSWang Nan void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, 198354cc54deSWang Nan enum bkw_mmap_state state) 198454cc54deSWang Nan { 198554cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 198654cc54deSWang Nan enum action { 198754cc54deSWang Nan NONE, 198854cc54deSWang Nan PAUSE, 198954cc54deSWang Nan RESUME, 199054cc54deSWang Nan } action = NONE; 199154cc54deSWang Nan 199254cc54deSWang Nan if (!evlist->backward_mmap) 199354cc54deSWang Nan return; 199454cc54deSWang Nan 199554cc54deSWang Nan switch (old_state) { 199654cc54deSWang Nan case BKW_MMAP_NOTREADY: { 199754cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 199854cc54deSWang Nan goto state_err;; 199954cc54deSWang Nan break; 200054cc54deSWang Nan } 200154cc54deSWang Nan case BKW_MMAP_RUNNING: { 200254cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 200354cc54deSWang Nan goto state_err; 200454cc54deSWang Nan action = PAUSE; 200554cc54deSWang Nan break; 200654cc54deSWang Nan } 200754cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 200854cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 200954cc54deSWang Nan goto state_err; 201054cc54deSWang Nan break; 201154cc54deSWang Nan } 201254cc54deSWang Nan case BKW_MMAP_EMPTY: { 201354cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 201454cc54deSWang Nan goto state_err; 201554cc54deSWang Nan action = RESUME; 201654cc54deSWang Nan break; 201754cc54deSWang Nan } 201854cc54deSWang Nan default: 201954cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 202054cc54deSWang Nan } 202154cc54deSWang Nan 202254cc54deSWang Nan evlist->bkw_mmap_state = state; 202354cc54deSWang Nan 202454cc54deSWang Nan switch (action) { 202554cc54deSWang Nan case PAUSE: 202654cc54deSWang Nan perf_evlist__pause(evlist); 202754cc54deSWang Nan break; 202854cc54deSWang Nan case RESUME: 202954cc54deSWang Nan perf_evlist__resume(evlist); 203054cc54deSWang Nan break; 203154cc54deSWang Nan case NONE: 203254cc54deSWang Nan default: 203354cc54deSWang Nan break; 203454cc54deSWang Nan } 203554cc54deSWang Nan 203654cc54deSWang Nan state_err: 203754cc54deSWang Nan return; 203854cc54deSWang Nan } 2039