1f8a95309SArnaldo Carvalho de Melo /* 2f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3f8a95309SArnaldo Carvalho de Melo * 4f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 5f8a95309SArnaldo Carvalho de Melo * copyright notes. 6f8a95309SArnaldo Carvalho de Melo * 7f8a95309SArnaldo Carvalho de Melo * Released under the GPL v2. (and only v2, not any later version) 8f8a95309SArnaldo Carvalho de Melo */ 9a8c9ae18SArnaldo Carvalho de Melo #include "util.h" 10956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h> 11a43783aeSArnaldo Carvalho de Melo #include <errno.h> 12fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 135c581041SArnaldo Carvalho de Melo #include <poll.h> 14f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 15f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1612864b31SNamhyung Kim #include "target.h" 17361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 18361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 19e3e1a54fSAdrian Hunter #include "debug.h" 2054cc54deSWang Nan #include "asm/bug.h" 2135b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 22361c99a6SArnaldo Carvalho de Melo 2350d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 244b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 2550d08e47SArnaldo Carvalho de Melo 26f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 27f8a95309SArnaldo Carvalho de Melo 2870db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2970db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 300389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 318dd2a131SJiri Olsa #include <linux/err.h> 3270db7533SArnaldo Carvalho de Melo 338db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map); 344876075bSWang Nan static void perf_mmap__put(struct perf_mmap *map); 35e4b356b5SArnaldo Carvalho de Melo 36f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 37a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 38f8a95309SArnaldo Carvalho de Melo 397e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 407e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 41361c99a6SArnaldo Carvalho de Melo { 4270db7533SArnaldo Carvalho de Melo int i; 4370db7533SArnaldo Carvalho de Melo 4470db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 4570db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 46361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 477e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 481b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 4935b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 5054cc54deSWang Nan evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; 51361c99a6SArnaldo Carvalho de Melo } 52361c99a6SArnaldo Carvalho de Melo 53334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 54ef1d1af2SArnaldo Carvalho de Melo { 55ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 56ef1d1af2SArnaldo Carvalho de Melo 57ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 58334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 59ef1d1af2SArnaldo Carvalho de Melo 60361c99a6SArnaldo Carvalho de Melo return evlist; 61361c99a6SArnaldo Carvalho de Melo } 62361c99a6SArnaldo Carvalho de Melo 63b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 64b22d54b0SJiri Olsa { 65b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 66b22d54b0SJiri Olsa 67b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 68b22d54b0SJiri Olsa perf_evlist__delete(evlist); 69b22d54b0SJiri Olsa evlist = NULL; 70b22d54b0SJiri Olsa } 71b22d54b0SJiri Olsa 72b22d54b0SJiri Olsa return evlist; 73b22d54b0SJiri Olsa } 74b22d54b0SJiri Olsa 755bae0250SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new_dummy(void) 765bae0250SArnaldo Carvalho de Melo { 775bae0250SArnaldo Carvalho de Melo struct perf_evlist *evlist = perf_evlist__new(); 785bae0250SArnaldo Carvalho de Melo 795bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 805bae0250SArnaldo Carvalho de Melo perf_evlist__delete(evlist); 815bae0250SArnaldo Carvalho de Melo evlist = NULL; 825bae0250SArnaldo Carvalho de Melo } 835bae0250SArnaldo Carvalho de Melo 845bae0250SArnaldo Carvalho de Melo return evlist; 855bae0250SArnaldo Carvalho de Melo } 865bae0250SArnaldo Carvalho de Melo 8775562573SAdrian Hunter /** 8875562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 8975562573SAdrian Hunter * @evlist: selected event list 9075562573SAdrian Hunter * 9175562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 9275562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 9375562573SAdrian Hunter */ 9475562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 9575562573SAdrian Hunter { 9675562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 9775562573SAdrian Hunter 9875562573SAdrian Hunter evlist->id_pos = first->id_pos; 9975562573SAdrian Hunter evlist->is_pos = first->is_pos; 10075562573SAdrian Hunter } 10175562573SAdrian Hunter 102733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 103733cd2feSAdrian Hunter { 104733cd2feSAdrian Hunter struct perf_evsel *evsel; 105733cd2feSAdrian Hunter 106e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 107733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 108733cd2feSAdrian Hunter 109733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 110733cd2feSAdrian Hunter } 111733cd2feSAdrian Hunter 112361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 113361c99a6SArnaldo Carvalho de Melo { 114361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 115361c99a6SArnaldo Carvalho de Melo 116e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, pos) { 117361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 118d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 119361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 120361c99a6SArnaldo Carvalho de Melo } 121361c99a6SArnaldo Carvalho de Melo 122361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 123361c99a6SArnaldo Carvalho de Melo } 124361c99a6SArnaldo Carvalho de Melo 125ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 126ef1d1af2SArnaldo Carvalho de Melo { 12704662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 128b2cb615dSWang Nan zfree(&evlist->backward_mmap); 1291b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 130ef1d1af2SArnaldo Carvalho de Melo } 131ef1d1af2SArnaldo Carvalho de Melo 132361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 133361c99a6SArnaldo Carvalho de Melo { 1340b04b3dcSArnaldo Carvalho de Melo if (evlist == NULL) 1350b04b3dcSArnaldo Carvalho de Melo return; 1360b04b3dcSArnaldo Carvalho de Melo 137983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 138f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 139f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 140186fbb74SJiri Olsa thread_map__put(evlist->threads); 14103ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 14203ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 143361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 144ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 145361c99a6SArnaldo Carvalho de Melo free(evlist); 146361c99a6SArnaldo Carvalho de Melo } 147361c99a6SArnaldo Carvalho de Melo 148adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, 149adc0c3e8SAdrian Hunter struct perf_evsel *evsel) 150adc0c3e8SAdrian Hunter { 151adc0c3e8SAdrian Hunter /* 152adc0c3e8SAdrian Hunter * We already have cpus for evsel (via PMU sysfs) so 153adc0c3e8SAdrian Hunter * keep it, if there's no target cpu list defined. 154adc0c3e8SAdrian Hunter */ 155adc0c3e8SAdrian Hunter if (!evsel->own_cpus || evlist->has_user_cpus) { 156adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 157adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evlist->cpus); 158adc0c3e8SAdrian Hunter } else if (evsel->cpus != evsel->own_cpus) { 159adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 160adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evsel->own_cpus); 161adc0c3e8SAdrian Hunter } 162adc0c3e8SAdrian Hunter 163adc0c3e8SAdrian Hunter thread_map__put(evsel->threads); 164adc0c3e8SAdrian Hunter evsel->threads = thread_map__get(evlist->threads); 165adc0c3e8SAdrian Hunter } 166adc0c3e8SAdrian Hunter 167adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist) 168adc0c3e8SAdrian Hunter { 169adc0c3e8SAdrian Hunter struct perf_evsel *evsel; 170adc0c3e8SAdrian Hunter 171e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 172adc0c3e8SAdrian Hunter __perf_evlist__propagate_maps(evlist, evsel); 173adc0c3e8SAdrian Hunter } 174adc0c3e8SAdrian Hunter 175361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 176361c99a6SArnaldo Carvalho de Melo { 177d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 178361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 179ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 18060b0896cSAdrian Hunter entry->tracking = !entry->idx; 181ef503831SArnaldo Carvalho de Melo 18275562573SAdrian Hunter if (!evlist->nr_entries++) 18375562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 18444c42d71SAdrian Hunter 18544c42d71SAdrian Hunter __perf_evlist__propagate_maps(evlist, entry); 186361c99a6SArnaldo Carvalho de Melo } 187361c99a6SArnaldo Carvalho de Melo 1884768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel) 1894768230aSAdrian Hunter { 1904768230aSAdrian Hunter evsel->evlist = NULL; 1914768230aSAdrian Hunter list_del_init(&evsel->node); 1924768230aSAdrian Hunter evlist->nr_entries -= 1; 1934768230aSAdrian Hunter } 1944768230aSAdrian Hunter 1950529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 196f114d6efSAdrian Hunter struct list_head *list) 19750d08e47SArnaldo Carvalho de Melo { 198f114d6efSAdrian Hunter struct perf_evsel *evsel, *temp; 19975562573SAdrian Hunter 200e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(list, temp, evsel) { 201f114d6efSAdrian Hunter list_del_init(&evsel->node); 202f114d6efSAdrian Hunter perf_evlist__add(evlist, evsel); 203f114d6efSAdrian Hunter } 20450d08e47SArnaldo Carvalho de Melo } 20550d08e47SArnaldo Carvalho de Melo 20663dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 20763dab225SArnaldo Carvalho de Melo { 20863dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 20963dab225SArnaldo Carvalho de Melo 21063dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 21197f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 21297f63e4aSNamhyung Kim 21397f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 21463dab225SArnaldo Carvalho de Melo 215e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry(list, evsel) { 21663dab225SArnaldo Carvalho de Melo evsel->leader = leader; 21763dab225SArnaldo Carvalho de Melo } 21863dab225SArnaldo Carvalho de Melo } 21963dab225SArnaldo Carvalho de Melo 22063dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 2216a4bb04cSJiri Olsa { 22297f63e4aSNamhyung Kim if (evlist->nr_entries) { 22397f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 22463dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 2256a4bb04cSJiri Olsa } 22697f63e4aSNamhyung Kim } 2276a4bb04cSJiri Olsa 22845cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr) 2297f8d1adeSArnaldo Carvalho de Melo { 2307f8d1adeSArnaldo Carvalho de Melo attr->precise_ip = 3; 2317f8d1adeSArnaldo Carvalho de Melo 2327f8d1adeSArnaldo Carvalho de Melo while (attr->precise_ip != 0) { 2337f8d1adeSArnaldo Carvalho de Melo int fd = sys_perf_event_open(attr, 0, -1, -1, 0); 2347f8d1adeSArnaldo Carvalho de Melo if (fd != -1) { 2357f8d1adeSArnaldo Carvalho de Melo close(fd); 2367f8d1adeSArnaldo Carvalho de Melo break; 2377f8d1adeSArnaldo Carvalho de Melo } 2387f8d1adeSArnaldo Carvalho de Melo --attr->precise_ip; 2397f8d1adeSArnaldo Carvalho de Melo } 2407f8d1adeSArnaldo Carvalho de Melo } 2417f8d1adeSArnaldo Carvalho de Melo 242361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 243361c99a6SArnaldo Carvalho de Melo { 2447c48dcfdSArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new_cycles(); 245361c99a6SArnaldo Carvalho de Melo 246361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 2477c48dcfdSArnaldo Carvalho de Melo return -ENOMEM; 248361c99a6SArnaldo Carvalho de Melo 249361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 250361c99a6SArnaldo Carvalho de Melo return 0; 251361c99a6SArnaldo Carvalho de Melo } 2525c581041SArnaldo Carvalho de Melo 2535bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist) 2545bae0250SArnaldo Carvalho de Melo { 2555bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2565bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2575bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2585bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2595bae0250SArnaldo Carvalho de Melo }; 2605bae0250SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new(&attr); 2615bae0250SArnaldo Carvalho de Melo 2625bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2635bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2645bae0250SArnaldo Carvalho de Melo 2655bae0250SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 2665bae0250SArnaldo Carvalho de Melo return 0; 2675bae0250SArnaldo Carvalho de Melo } 2685bae0250SArnaldo Carvalho de Melo 269e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 27050d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 27150d08e47SArnaldo Carvalho de Melo { 27250d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 27350d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 27450d08e47SArnaldo Carvalho de Melo size_t i; 27550d08e47SArnaldo Carvalho de Melo 27650d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 277ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 27850d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 27950d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 28050d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 28150d08e47SArnaldo Carvalho de Melo } 28250d08e47SArnaldo Carvalho de Melo 283f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 28450d08e47SArnaldo Carvalho de Melo 28550d08e47SArnaldo Carvalho de Melo return 0; 28650d08e47SArnaldo Carvalho de Melo 28750d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 288e5cadb93SArnaldo Carvalho de Melo __evlist__for_each_entry_safe(&head, n, evsel) 28950d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 29050d08e47SArnaldo Carvalho de Melo return -1; 29150d08e47SArnaldo Carvalho de Melo } 29250d08e47SArnaldo Carvalho de Melo 29379695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 29479695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 29579695e1bSArnaldo Carvalho de Melo { 29679695e1bSArnaldo Carvalho de Melo size_t i; 29779695e1bSArnaldo Carvalho de Melo 29879695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 29979695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 30079695e1bSArnaldo Carvalho de Melo 30179695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 30279695e1bSArnaldo Carvalho de Melo } 30379695e1bSArnaldo Carvalho de Melo 304da378962SArnaldo Carvalho de Melo struct perf_evsel * 305ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 306ee29be62SArnaldo Carvalho de Melo { 307ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 308ee29be62SArnaldo Carvalho de Melo 309e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 310ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 311ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 312ee29be62SArnaldo Carvalho de Melo return evsel; 313ee29be62SArnaldo Carvalho de Melo } 314ee29be62SArnaldo Carvalho de Melo 315ee29be62SArnaldo Carvalho de Melo return NULL; 316ee29be62SArnaldo Carvalho de Melo } 317ee29be62SArnaldo Carvalho de Melo 318a2f2804aSDavid Ahern struct perf_evsel * 319a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 320a2f2804aSDavid Ahern const char *name) 321a2f2804aSDavid Ahern { 322a2f2804aSDavid Ahern struct perf_evsel *evsel; 323a2f2804aSDavid Ahern 324e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 325a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 326a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 327a2f2804aSDavid Ahern return evsel; 328a2f2804aSDavid Ahern } 329a2f2804aSDavid Ahern 330a2f2804aSDavid Ahern return NULL; 331a2f2804aSDavid Ahern } 332a2f2804aSDavid Ahern 33339876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 33439876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 33539876e7dSArnaldo Carvalho de Melo { 336ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 33739876e7dSArnaldo Carvalho de Melo 3388dd2a131SJiri Olsa if (IS_ERR(evsel)) 33939876e7dSArnaldo Carvalho de Melo return -1; 34039876e7dSArnaldo Carvalho de Melo 341744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 34239876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 34339876e7dSArnaldo Carvalho de Melo return 0; 34439876e7dSArnaldo Carvalho de Melo } 34539876e7dSArnaldo Carvalho de Melo 346bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 347bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 348bf8e8f4bSAdrian Hunter { 349bf8e8f4bSAdrian Hunter if (evsel->system_wide) 350bf8e8f4bSAdrian Hunter return 1; 351bf8e8f4bSAdrian Hunter else 352bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 353bf8e8f4bSAdrian Hunter } 354bf8e8f4bSAdrian Hunter 3554152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 3564152ab37SArnaldo Carvalho de Melo { 3574152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 3584152ab37SArnaldo Carvalho de Melo 359e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 360395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3613fe4430dSJiri Olsa continue; 3623e27c920SJiri Olsa perf_evsel__disable(pos); 3634152ab37SArnaldo Carvalho de Melo } 3642b56bcfbSArnaldo Carvalho de Melo 3652b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3664152ab37SArnaldo Carvalho de Melo } 3674152ab37SArnaldo Carvalho de Melo 368764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 369764e16a3SDavid Ahern { 370764e16a3SDavid Ahern struct perf_evsel *pos; 371764e16a3SDavid Ahern 372e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 373395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3743fe4430dSJiri Olsa continue; 3753e27c920SJiri Olsa perf_evsel__enable(pos); 376764e16a3SDavid Ahern } 3772b56bcfbSArnaldo Carvalho de Melo 3782b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3792b56bcfbSArnaldo Carvalho de Melo } 3802b56bcfbSArnaldo Carvalho de Melo 3812b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist) 3822b56bcfbSArnaldo Carvalho de Melo { 3832b56bcfbSArnaldo Carvalho de Melo (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); 384764e16a3SDavid Ahern } 385764e16a3SDavid Ahern 3861c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3871c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3881c65056cSAdrian Hunter { 38918ef15c6SArnaldo Carvalho de Melo int thread; 3901c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3911c65056cSAdrian Hunter 3921c65056cSAdrian Hunter if (!evsel->fd) 3931c65056cSAdrian Hunter return -EINVAL; 3941c65056cSAdrian Hunter 3951c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 39618ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3971c65056cSAdrian Hunter if (err) 3981c65056cSAdrian Hunter return err; 3991c65056cSAdrian Hunter } 4001c65056cSAdrian Hunter return 0; 4011c65056cSAdrian Hunter } 4021c65056cSAdrian Hunter 4031c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 4041c65056cSAdrian Hunter struct perf_evsel *evsel, 4051c65056cSAdrian Hunter int thread) 4061c65056cSAdrian Hunter { 40718ef15c6SArnaldo Carvalho de Melo int cpu; 4081c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 4091c65056cSAdrian Hunter 4101c65056cSAdrian Hunter if (!evsel->fd) 4111c65056cSAdrian Hunter return -EINVAL; 4121c65056cSAdrian Hunter 4131c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 41418ef15c6SArnaldo Carvalho de Melo int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4151c65056cSAdrian Hunter if (err) 4161c65056cSAdrian Hunter return err; 4171c65056cSAdrian Hunter } 4181c65056cSAdrian Hunter return 0; 4191c65056cSAdrian Hunter } 4201c65056cSAdrian Hunter 4211c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4221c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4231c65056cSAdrian Hunter { 4241c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4251c65056cSAdrian Hunter 4261c65056cSAdrian Hunter if (per_cpu_mmaps) 4271c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4281c65056cSAdrian Hunter else 4291c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4301c65056cSAdrian Hunter } 4311c65056cSAdrian Hunter 432ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4335c581041SArnaldo Carvalho de Melo { 434b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 435b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 436bf8e8f4bSAdrian Hunter int nfds = 0; 437bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 438bf8e8f4bSAdrian Hunter 439e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 440bf8e8f4bSAdrian Hunter if (evsel->system_wide) 441bf8e8f4bSAdrian Hunter nfds += nr_cpus; 442bf8e8f4bSAdrian Hunter else 443bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 444bf8e8f4bSAdrian Hunter } 445bf8e8f4bSAdrian Hunter 4461b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4471b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 448ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 449ad6765ddSArnaldo Carvalho de Melo 450ad6765ddSArnaldo Carvalho de Melo return 0; 4515c581041SArnaldo Carvalho de Melo } 45270082dd9SArnaldo Carvalho de Melo 4534876075bSWang Nan static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, 4544876075bSWang Nan struct perf_mmap *map, short revent) 455e4b356b5SArnaldo Carvalho de Melo { 456f3058a1cSWang Nan int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); 457e4b356b5SArnaldo Carvalho de Melo /* 458e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 459e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 460e4b356b5SArnaldo Carvalho de Melo */ 461e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 4624876075bSWang Nan evlist->pollfd.priv[pos].ptr = map; 463e4b356b5SArnaldo Carvalho de Melo 464e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 465e4b356b5SArnaldo Carvalho de Melo } 466e4b356b5SArnaldo Carvalho de Melo 467e4b356b5SArnaldo Carvalho de Melo return pos; 468e4b356b5SArnaldo Carvalho de Melo } 469e4b356b5SArnaldo Carvalho de Melo 470ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 47170082dd9SArnaldo Carvalho de Melo { 4724876075bSWang Nan return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN); 473e4b356b5SArnaldo Carvalho de Melo } 4741b85337dSArnaldo Carvalho de Melo 475258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 476258e4bfcSWang Nan void *arg __maybe_unused) 477e4b356b5SArnaldo Carvalho de Melo { 4784876075bSWang Nan struct perf_mmap *map = fda->priv[fd].ptr; 479e4b356b5SArnaldo Carvalho de Melo 4804876075bSWang Nan if (map) 4814876075bSWang Nan perf_mmap__put(map); 48270082dd9SArnaldo Carvalho de Melo } 48370db7533SArnaldo Carvalho de Melo 4841ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4851ddec7f0SArnaldo Carvalho de Melo { 486e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 487258e4bfcSWang Nan perf_evlist__munmap_filtered, NULL); 4881ddec7f0SArnaldo Carvalho de Melo } 4891ddec7f0SArnaldo Carvalho de Melo 490f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 491f66a889dSArnaldo Carvalho de Melo { 4921b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 493f66a889dSArnaldo Carvalho de Melo } 494f66a889dSArnaldo Carvalho de Melo 495a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 496a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 4973d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4983d3b5e95SArnaldo Carvalho de Melo { 4993d3b5e95SArnaldo Carvalho de Melo int hash; 5003d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 5013d3b5e95SArnaldo Carvalho de Melo 5023d3b5e95SArnaldo Carvalho de Melo sid->id = id; 5033d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 5043d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 5053d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 5063d3b5e95SArnaldo Carvalho de Melo } 5073d3b5e95SArnaldo Carvalho de Melo 508a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 509a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 510a91e5431SArnaldo Carvalho de Melo { 511a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 512a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 513a91e5431SArnaldo Carvalho de Melo } 514a91e5431SArnaldo Carvalho de Melo 5151c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist, 5163d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 517f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 518f8a95309SArnaldo Carvalho de Melo { 519f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5203d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 521e2b5abe0SJiri Olsa u64 id; 522e2b5abe0SJiri Olsa int ret; 523e2b5abe0SJiri Olsa 524e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 525e2b5abe0SJiri Olsa if (!ret) 526e2b5abe0SJiri Olsa goto add; 527e2b5abe0SJiri Olsa 528e2b5abe0SJiri Olsa if (errno != ENOTTY) 529e2b5abe0SJiri Olsa return -1; 530e2b5abe0SJiri Olsa 531e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 532f8a95309SArnaldo Carvalho de Melo 533c4861afeSJiri Olsa /* 534c4861afeSJiri Olsa * This way does not work with group format read, so bail 535c4861afeSJiri Olsa * out in that case. 536c4861afeSJiri Olsa */ 537c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 538c4861afeSJiri Olsa return -1; 539c4861afeSJiri Olsa 540f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 541f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 542f8a95309SArnaldo Carvalho de Melo return -1; 543f8a95309SArnaldo Carvalho de Melo 544f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 545f8a95309SArnaldo Carvalho de Melo ++id_idx; 546f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 547f8a95309SArnaldo Carvalho de Melo ++id_idx; 548f8a95309SArnaldo Carvalho de Melo 549e2b5abe0SJiri Olsa id = read_data[id_idx]; 550e2b5abe0SJiri Olsa 551e2b5abe0SJiri Olsa add: 552e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 553f8a95309SArnaldo Carvalho de Melo return 0; 554f8a95309SArnaldo Carvalho de Melo } 555f8a95309SArnaldo Carvalho de Melo 5563c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5573c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5583c659eedSAdrian Hunter int thread) 5593c659eedSAdrian Hunter { 5603c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5613c659eedSAdrian Hunter sid->idx = idx; 5623c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5633c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5643c659eedSAdrian Hunter else 5653c659eedSAdrian Hunter sid->cpu = -1; 5663c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 567e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5683c659eedSAdrian Hunter else 5693c659eedSAdrian Hunter sid->tid = -1; 5703c659eedSAdrian Hunter } 5713c659eedSAdrian Hunter 572932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 57370db7533SArnaldo Carvalho de Melo { 57470db7533SArnaldo Carvalho de Melo struct hlist_head *head; 57570db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 57670db7533SArnaldo Carvalho de Melo int hash; 57770db7533SArnaldo Carvalho de Melo 57870db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 57970db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 58070db7533SArnaldo Carvalho de Melo 581b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 58270db7533SArnaldo Carvalho de Melo if (sid->id == id) 583932a3594SJiri Olsa return sid; 584932a3594SJiri Olsa 585932a3594SJiri Olsa return NULL; 586932a3594SJiri Olsa } 587932a3594SJiri Olsa 588932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 589932a3594SJiri Olsa { 590932a3594SJiri Olsa struct perf_sample_id *sid; 591932a3594SJiri Olsa 59205169df5SAdrian Hunter if (evlist->nr_entries == 1 || !id) 593932a3594SJiri Olsa return perf_evlist__first(evlist); 594932a3594SJiri Olsa 595932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 596932a3594SJiri Olsa if (sid) 59770db7533SArnaldo Carvalho de Melo return sid->evsel; 59830e68bccSNamhyung Kim 59930e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 6000c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 60130e68bccSNamhyung Kim 60270db7533SArnaldo Carvalho de Melo return NULL; 60370db7533SArnaldo Carvalho de Melo } 60404391debSArnaldo Carvalho de Melo 605dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist, 606dddcf6abSAdrian Hunter u64 id) 607dddcf6abSAdrian Hunter { 608dddcf6abSAdrian Hunter struct perf_sample_id *sid; 609dddcf6abSAdrian Hunter 610dddcf6abSAdrian Hunter if (!id) 611dddcf6abSAdrian Hunter return NULL; 612dddcf6abSAdrian Hunter 613dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 614dddcf6abSAdrian Hunter if (sid) 615dddcf6abSAdrian Hunter return sid->evsel; 616dddcf6abSAdrian Hunter 617dddcf6abSAdrian Hunter return NULL; 618dddcf6abSAdrian Hunter } 619dddcf6abSAdrian Hunter 62075562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 62175562573SAdrian Hunter union perf_event *event, u64 *id) 62275562573SAdrian Hunter { 62375562573SAdrian Hunter const u64 *array = event->sample.array; 62475562573SAdrian Hunter ssize_t n; 62575562573SAdrian Hunter 62675562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 62775562573SAdrian Hunter 62875562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 62975562573SAdrian Hunter if (evlist->id_pos >= n) 63075562573SAdrian Hunter return -1; 63175562573SAdrian Hunter *id = array[evlist->id_pos]; 63275562573SAdrian Hunter } else { 63375562573SAdrian Hunter if (evlist->is_pos > n) 63475562573SAdrian Hunter return -1; 63575562573SAdrian Hunter n -= evlist->is_pos; 63675562573SAdrian Hunter *id = array[n]; 63775562573SAdrian Hunter } 63875562573SAdrian Hunter return 0; 63975562573SAdrian Hunter } 64075562573SAdrian Hunter 6417cb5c5acSJiri Olsa struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 64275562573SAdrian Hunter union perf_event *event) 64375562573SAdrian Hunter { 64498be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 64575562573SAdrian Hunter struct hlist_head *head; 64675562573SAdrian Hunter struct perf_sample_id *sid; 64775562573SAdrian Hunter int hash; 64875562573SAdrian Hunter u64 id; 64975562573SAdrian Hunter 65075562573SAdrian Hunter if (evlist->nr_entries == 1) 65198be6966SAdrian Hunter return first; 65298be6966SAdrian Hunter 65398be6966SAdrian Hunter if (!first->attr.sample_id_all && 65498be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 65598be6966SAdrian Hunter return first; 65675562573SAdrian Hunter 65775562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 65875562573SAdrian Hunter return NULL; 65975562573SAdrian Hunter 66075562573SAdrian Hunter /* Synthesized events have an id of zero */ 66175562573SAdrian Hunter if (!id) 66298be6966SAdrian Hunter return first; 66375562573SAdrian Hunter 66475562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 66575562573SAdrian Hunter head = &evlist->heads[hash]; 66675562573SAdrian Hunter 66775562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 66875562573SAdrian Hunter if (sid->id == id) 66975562573SAdrian Hunter return sid->evsel; 67075562573SAdrian Hunter } 67175562573SAdrian Hunter return NULL; 67275562573SAdrian Hunter } 67375562573SAdrian Hunter 67465aea233SWang Nan static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value) 67565aea233SWang Nan { 67665aea233SWang Nan int i; 67765aea233SWang Nan 678078c3386SWang Nan if (!evlist->backward_mmap) 679078c3386SWang Nan return 0; 680078c3386SWang Nan 68165aea233SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 682078c3386SWang Nan int fd = evlist->backward_mmap[i].fd; 68365aea233SWang Nan int err; 68465aea233SWang Nan 68565aea233SWang Nan if (fd < 0) 68665aea233SWang Nan continue; 68765aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 68865aea233SWang Nan if (err) 68965aea233SWang Nan return err; 69065aea233SWang Nan } 69165aea233SWang Nan return 0; 69265aea233SWang Nan } 69365aea233SWang Nan 694f6cdff83SWang Nan static int perf_evlist__pause(struct perf_evlist *evlist) 69565aea233SWang Nan { 69665aea233SWang Nan return perf_evlist__set_paused(evlist, true); 69765aea233SWang Nan } 69865aea233SWang Nan 699f6cdff83SWang Nan static int perf_evlist__resume(struct perf_evlist *evlist) 70065aea233SWang Nan { 70165aea233SWang Nan return perf_evlist__set_paused(evlist, false); 70265aea233SWang Nan } 70365aea233SWang Nan 704b6b85dadSWang Nan /* When check_messup is true, 'end' must points to a good entry */ 7050f4ccd11SWang Nan static union perf_event * 706b6b85dadSWang Nan perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start, 707b6b85dadSWang Nan u64 end, u64 *prev) 70804391debSArnaldo Carvalho de Melo { 70904391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 7108115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 711b6b85dadSWang Nan int diff = end - start; 71204391debSArnaldo Carvalho de Melo 713b6b85dadSWang Nan if (check_messup) { 71404391debSArnaldo Carvalho de Melo /* 71504391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 71604391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 71704391debSArnaldo Carvalho de Melo * 718b6b85dadSWang Nan * If we somehow ended up ahead of the 'end', we got messed up. 71904391debSArnaldo Carvalho de Melo * 720b6b85dadSWang Nan * In either case, truncate and restart at 'end'. 72104391debSArnaldo Carvalho de Melo */ 72204391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 72304391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 72404391debSArnaldo Carvalho de Melo 72504391debSArnaldo Carvalho de Melo /* 726b6b85dadSWang Nan * 'end' points to a known good entry, start there. 72704391debSArnaldo Carvalho de Melo */ 728b6b85dadSWang Nan start = end; 729b04b7023SWang Nan diff = 0; 73004391debSArnaldo Carvalho de Melo } 7317bb41152SArnaldo Carvalho de Melo } 73204391debSArnaldo Carvalho de Melo 733b04b7023SWang Nan if (diff >= (int)sizeof(event->header)) { 73404391debSArnaldo Carvalho de Melo size_t size; 73504391debSArnaldo Carvalho de Melo 736b6b85dadSWang Nan event = (union perf_event *)&data[start & md->mask]; 73704391debSArnaldo Carvalho de Melo size = event->header.size; 73804391debSArnaldo Carvalho de Melo 739b04b7023SWang Nan if (size < sizeof(event->header) || diff < (int)size) { 740b04b7023SWang Nan event = NULL; 741b04b7023SWang Nan goto broken_event; 742b04b7023SWang Nan } 743b04b7023SWang Nan 74404391debSArnaldo Carvalho de Melo /* 74504391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 74604391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 74704391debSArnaldo Carvalho de Melo */ 748b6b85dadSWang Nan if ((start & md->mask) + size != ((start + size) & md->mask)) { 749b6b85dadSWang Nan unsigned int offset = start; 75004391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 751a65cb4b9SJiri Olsa void *dst = md->event_copy; 75204391debSArnaldo Carvalho de Melo 75304391debSArnaldo Carvalho de Melo do { 75404391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 75504391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 75604391debSArnaldo Carvalho de Melo offset += cpy; 75704391debSArnaldo Carvalho de Melo dst += cpy; 75804391debSArnaldo Carvalho de Melo len -= cpy; 75904391debSArnaldo Carvalho de Melo } while (len); 76004391debSArnaldo Carvalho de Melo 761a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 76204391debSArnaldo Carvalho de Melo } 76304391debSArnaldo Carvalho de Melo 764b6b85dadSWang Nan start += size; 76504391debSArnaldo Carvalho de Melo } 76604391debSArnaldo Carvalho de Melo 767b04b7023SWang Nan broken_event: 7680f4ccd11SWang Nan if (prev) 769b6b85dadSWang Nan *prev = start; 7707bb41152SArnaldo Carvalho de Melo 77104391debSArnaldo Carvalho de Melo return event; 77204391debSArnaldo Carvalho de Melo } 773f8a95309SArnaldo Carvalho de Melo 7748db6d6b1SWang Nan union perf_event *perf_mmap__read_forward(struct perf_mmap *md, bool check_messup) 7750f4ccd11SWang Nan { 7760f4ccd11SWang Nan u64 head; 7770f4ccd11SWang Nan u64 old = md->prev; 7780f4ccd11SWang Nan 7790f4ccd11SWang Nan /* 7800f4ccd11SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 7810f4ccd11SWang Nan */ 78225a3720cSElena Reshetova if (!refcount_read(&md->refcnt)) 7830f4ccd11SWang Nan return NULL; 7840f4ccd11SWang Nan 7850f4ccd11SWang Nan head = perf_mmap__read_head(md); 7860f4ccd11SWang Nan 7878db6d6b1SWang Nan return perf_mmap__read(md, check_messup, old, head, &md->prev); 7880f4ccd11SWang Nan } 7890f4ccd11SWang Nan 790e24c7520SWang Nan union perf_event * 7918db6d6b1SWang Nan perf_mmap__read_backward(struct perf_mmap *md) 792e24c7520SWang Nan { 793e24c7520SWang Nan u64 head, end; 794e24c7520SWang Nan u64 start = md->prev; 795e24c7520SWang Nan 796e24c7520SWang Nan /* 797e24c7520SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 798e24c7520SWang Nan */ 79925a3720cSElena Reshetova if (!refcount_read(&md->refcnt)) 800e24c7520SWang Nan return NULL; 801e24c7520SWang Nan 802e24c7520SWang Nan head = perf_mmap__read_head(md); 803e24c7520SWang Nan if (!head) 804e24c7520SWang Nan return NULL; 805e24c7520SWang Nan 806e24c7520SWang Nan /* 807e24c7520SWang Nan * 'head' pointer starts from 0. Kernel minus sizeof(record) form 808e24c7520SWang Nan * it each time when kernel writes to it, so in fact 'head' is 809e24c7520SWang Nan * negative. 'end' pointer is made manually by adding the size of 810e24c7520SWang Nan * the ring buffer to 'head' pointer, means the validate data can 811e24c7520SWang Nan * read is the whole ring buffer. If 'end' is positive, the ring 812e24c7520SWang Nan * buffer has not fully filled, so we must adjust 'end' to 0. 813e24c7520SWang Nan * 814e24c7520SWang Nan * However, since both 'head' and 'end' is unsigned, we can't 815e24c7520SWang Nan * simply compare 'end' against 0. Here we compare '-head' and 816e24c7520SWang Nan * the size of the ring buffer, where -head is the number of bytes 817e24c7520SWang Nan * kernel write to the ring buffer. 818e24c7520SWang Nan */ 819e24c7520SWang Nan if (-head < (u64)(md->mask + 1)) 820e24c7520SWang Nan end = 0; 821e24c7520SWang Nan else 822e24c7520SWang Nan end = head + md->mask + 1; 823e24c7520SWang Nan 824e24c7520SWang Nan return perf_mmap__read(md, false, start, end, &md->prev); 825e24c7520SWang Nan } 826e24c7520SWang Nan 8278db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx) 8288db6d6b1SWang Nan { 8298db6d6b1SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 8308db6d6b1SWang Nan 8318db6d6b1SWang Nan /* 8328db6d6b1SWang Nan * Check messup is required for forward overwritable ring buffer: 8338db6d6b1SWang Nan * memory pointed by md->prev can be overwritten in this case. 8348db6d6b1SWang Nan * No need for read-write ring buffer: kernel stop outputting when 8358db6d6b1SWang Nan * it hit md->prev (perf_mmap__consume()). 8368db6d6b1SWang Nan */ 8378db6d6b1SWang Nan return perf_mmap__read_forward(md, evlist->overwrite); 8388db6d6b1SWang Nan } 8398db6d6b1SWang Nan 8408db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx) 8418db6d6b1SWang Nan { 8428db6d6b1SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 8438db6d6b1SWang Nan 8448db6d6b1SWang Nan /* 8458db6d6b1SWang Nan * No need to check messup for backward ring buffer: 8468db6d6b1SWang Nan * We can always read arbitrary long data from a backward 8478db6d6b1SWang Nan * ring buffer unless we forget to pause it before reading. 8488db6d6b1SWang Nan */ 8498db6d6b1SWang Nan return perf_mmap__read_backward(md); 8508db6d6b1SWang Nan } 8518db6d6b1SWang Nan 8525a5ddeb6SWang Nan union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 8535a5ddeb6SWang Nan { 8545a5ddeb6SWang Nan return perf_evlist__mmap_read_forward(evlist, idx); 8555a5ddeb6SWang Nan } 8565a5ddeb6SWang Nan 8578db6d6b1SWang Nan void perf_mmap__read_catchup(struct perf_mmap *md) 858e24c7520SWang Nan { 859e24c7520SWang Nan u64 head; 860e24c7520SWang Nan 86125a3720cSElena Reshetova if (!refcount_read(&md->refcnt)) 862e24c7520SWang Nan return; 863e24c7520SWang Nan 864e24c7520SWang Nan head = perf_mmap__read_head(md); 865e24c7520SWang Nan md->prev = head; 866e24c7520SWang Nan } 867e24c7520SWang Nan 8688db6d6b1SWang Nan void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx) 8698db6d6b1SWang Nan { 8708db6d6b1SWang Nan perf_mmap__read_catchup(&evlist->mmap[idx]); 8718db6d6b1SWang Nan } 8728db6d6b1SWang Nan 87382396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 87482396986SArnaldo Carvalho de Melo { 875b72e74d1SAdrian Hunter return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base; 87682396986SArnaldo Carvalho de Melo } 87782396986SArnaldo Carvalho de Melo 8788db6d6b1SWang Nan static void perf_mmap__get(struct perf_mmap *map) 8798db6d6b1SWang Nan { 88025a3720cSElena Reshetova refcount_inc(&map->refcnt); 8818db6d6b1SWang Nan } 8828db6d6b1SWang Nan 8838db6d6b1SWang Nan static void perf_mmap__put(struct perf_mmap *md) 8848db6d6b1SWang Nan { 88525a3720cSElena Reshetova BUG_ON(md->base && refcount_read(&md->refcnt) == 0); 8868db6d6b1SWang Nan 88725a3720cSElena Reshetova if (refcount_dec_and_test(&md->refcnt)) 8888db6d6b1SWang Nan perf_mmap__munmap(md); 8898db6d6b1SWang Nan } 8908db6d6b1SWang Nan 8918db6d6b1SWang Nan void perf_mmap__consume(struct perf_mmap *md, bool overwrite) 8928e50d384SZhouyi Zhou { 8938db6d6b1SWang Nan if (!overwrite) { 8947b8283b5SDavid Ahern u64 old = md->prev; 8958e50d384SZhouyi Zhou 8968e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 8978e50d384SZhouyi Zhou } 89882396986SArnaldo Carvalho de Melo 89925a3720cSElena Reshetova if (refcount_read(&md->refcnt) == 1 && perf_mmap__empty(md)) 9008db6d6b1SWang Nan perf_mmap__put(md); 9018db6d6b1SWang Nan } 9028db6d6b1SWang Nan 9038db6d6b1SWang Nan void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 9048db6d6b1SWang Nan { 9058db6d6b1SWang Nan perf_mmap__consume(&evlist->mmap[idx], evlist->overwrite); 9068e50d384SZhouyi Zhou } 9078e50d384SZhouyi Zhou 908718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, 909718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 910718c602dSAdrian Hunter void *userpg __maybe_unused, 911718c602dSAdrian Hunter int fd __maybe_unused) 912718c602dSAdrian Hunter { 913718c602dSAdrian Hunter return 0; 914718c602dSAdrian Hunter } 915718c602dSAdrian Hunter 916718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) 917718c602dSAdrian Hunter { 918718c602dSAdrian Hunter } 919718c602dSAdrian Hunter 920718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init( 921718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 922718c602dSAdrian Hunter off_t auxtrace_offset __maybe_unused, 923718c602dSAdrian Hunter unsigned int auxtrace_pages __maybe_unused, 924718c602dSAdrian Hunter bool auxtrace_overwrite __maybe_unused) 925718c602dSAdrian Hunter { 926718c602dSAdrian Hunter } 927718c602dSAdrian Hunter 928718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx( 929718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 930718c602dSAdrian Hunter struct perf_evlist *evlist __maybe_unused, 931718c602dSAdrian Hunter int idx __maybe_unused, 932718c602dSAdrian Hunter bool per_cpu __maybe_unused) 933718c602dSAdrian Hunter { 934718c602dSAdrian Hunter } 935718c602dSAdrian Hunter 9368db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map) 9378db6d6b1SWang Nan { 9388db6d6b1SWang Nan if (map->base != NULL) { 9398db6d6b1SWang Nan munmap(map->base, perf_mmap__mmap_len(map)); 9408db6d6b1SWang Nan map->base = NULL; 9418db6d6b1SWang Nan map->fd = -1; 94225a3720cSElena Reshetova refcount_set(&map->refcnt, 0); 9438db6d6b1SWang Nan } 9448db6d6b1SWang Nan auxtrace_mmap__munmap(&map->auxtrace_mmap); 9458db6d6b1SWang Nan } 9468db6d6b1SWang Nan 947a1f72618SWang Nan static void perf_evlist__munmap_nofree(struct perf_evlist *evlist) 948f8a95309SArnaldo Carvalho de Melo { 949aece948fSArnaldo Carvalho de Melo int i; 950f8a95309SArnaldo Carvalho de Melo 951b2cb615dSWang Nan if (evlist->mmap) 95293edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 953a1f72618SWang Nan perf_mmap__munmap(&evlist->mmap[i]); 954b2cb615dSWang Nan 955b2cb615dSWang Nan if (evlist->backward_mmap) 956b2cb615dSWang Nan for (i = 0; i < evlist->nr_mmaps; i++) 957b2cb615dSWang Nan perf_mmap__munmap(&evlist->backward_mmap[i]); 958a1f72618SWang Nan } 959aece948fSArnaldo Carvalho de Melo 960a1f72618SWang Nan void perf_evlist__munmap(struct perf_evlist *evlist) 961a1f72618SWang Nan { 962a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 96304662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 964b2cb615dSWang Nan zfree(&evlist->backward_mmap); 965f8a95309SArnaldo Carvalho de Melo } 966f8a95309SArnaldo Carvalho de Melo 9678db6d6b1SWang Nan static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist) 968f8a95309SArnaldo Carvalho de Melo { 969d4c6fb36SWang Nan int i; 9708db6d6b1SWang Nan struct perf_mmap *map; 971d4c6fb36SWang Nan 972a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 973ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 974b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 9758db6d6b1SWang Nan map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 9768db6d6b1SWang Nan if (!map) 9778db6d6b1SWang Nan return NULL; 978946ae1d4SWang Nan 9794738ca30SArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_mmaps; i++) { 9808db6d6b1SWang Nan map[i].fd = -1; 9814738ca30SArnaldo Carvalho de Melo /* 9824738ca30SArnaldo Carvalho de Melo * When the perf_mmap() call is made we grab one refcount, plus 9834738ca30SArnaldo Carvalho de Melo * one extra to let perf_evlist__mmap_consume() get the last 9844738ca30SArnaldo Carvalho de Melo * events after all real references (perf_mmap__get()) are 9854738ca30SArnaldo Carvalho de Melo * dropped. 9864738ca30SArnaldo Carvalho de Melo * 9874738ca30SArnaldo Carvalho de Melo * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and 9884738ca30SArnaldo Carvalho de Melo * thus does perf_mmap__get() on it. 9894738ca30SArnaldo Carvalho de Melo */ 9904738ca30SArnaldo Carvalho de Melo refcount_set(&map[i].refcnt, 0); 9914738ca30SArnaldo Carvalho de Melo } 9928db6d6b1SWang Nan return map; 993f8a95309SArnaldo Carvalho de Melo } 994f8a95309SArnaldo Carvalho de Melo 995a8a8f3ebSAdrian Hunter struct mmap_params { 996a8a8f3ebSAdrian Hunter int prot; 997a8a8f3ebSAdrian Hunter int mask; 998718c602dSAdrian Hunter struct auxtrace_mmap_params auxtrace_mp; 999a8a8f3ebSAdrian Hunter }; 1000a8a8f3ebSAdrian Hunter 10018db6d6b1SWang Nan static int perf_mmap__mmap(struct perf_mmap *map, 1002a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 1003f8a95309SArnaldo Carvalho de Melo { 100482396986SArnaldo Carvalho de Melo /* 100582396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 100682396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 100782396986SArnaldo Carvalho de Melo * the ring buffer. 100882396986SArnaldo Carvalho de Melo * 100982396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 101082396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 101182396986SArnaldo Carvalho de Melo * waiting to be consumed. 101282396986SArnaldo Carvalho de Melo * 101382396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 101482396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 101582396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 101682396986SArnaldo Carvalho de Melo */ 101725a3720cSElena Reshetova refcount_set(&map->refcnt, 2); 10188db6d6b1SWang Nan map->prev = 0; 10198db6d6b1SWang Nan map->mask = mp->mask; 10208db6d6b1SWang Nan map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot, 1021f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 10228db6d6b1SWang Nan if (map->base == MAP_FAILED) { 102302635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 102402635965SAdrian Hunter errno); 10258db6d6b1SWang Nan map->base = NULL; 1026f8a95309SArnaldo Carvalho de Melo return -1; 1027301b195dSNelson Elhage } 10288db6d6b1SWang Nan map->fd = fd; 1029ad6765ddSArnaldo Carvalho de Melo 10308db6d6b1SWang Nan if (auxtrace_mmap__mmap(&map->auxtrace_mmap, 10318db6d6b1SWang Nan &mp->auxtrace_mp, map->base, fd)) 1032718c602dSAdrian Hunter return -1; 1033718c602dSAdrian Hunter 1034f8a95309SArnaldo Carvalho de Melo return 0; 1035f8a95309SArnaldo Carvalho de Melo } 1036f8a95309SArnaldo Carvalho de Melo 1037f3058a1cSWang Nan static bool 1038f3058a1cSWang Nan perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused, 1039f3058a1cSWang Nan struct perf_evsel *evsel) 1040f3058a1cSWang Nan { 104132a951b4SArnaldo Carvalho de Melo if (evsel->attr.write_backward) 1042f3058a1cSWang Nan return false; 1043f3058a1cSWang Nan return true; 1044f3058a1cSWang Nan } 1045f3058a1cSWang Nan 104604e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 10479f21b815SMark Rutland struct mmap_params *mp, int cpu_idx, 1048078c3386SWang Nan int thread, int *_output, int *_output_backward) 1049aece948fSArnaldo Carvalho de Melo { 1050aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 1051f3058a1cSWang Nan int revent; 10529f21b815SMark Rutland int evlist_cpu = cpu_map__cpu(evlist->cpus, cpu_idx); 105304e21314SAdrian Hunter 1054e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1055078c3386SWang Nan struct perf_mmap *maps = evlist->mmap; 1056078c3386SWang Nan int *output = _output; 1057bf8e8f4bSAdrian Hunter int fd; 10589f21b815SMark Rutland int cpu; 1059bf8e8f4bSAdrian Hunter 1060078c3386SWang Nan if (evsel->attr.write_backward) { 1061078c3386SWang Nan output = _output_backward; 1062078c3386SWang Nan maps = evlist->backward_mmap; 1063078c3386SWang Nan 1064078c3386SWang Nan if (!maps) { 1065078c3386SWang Nan maps = perf_evlist__alloc_mmap(evlist); 1066078c3386SWang Nan if (!maps) 1067078c3386SWang Nan return -1; 1068078c3386SWang Nan evlist->backward_mmap = maps; 106954cc54deSWang Nan if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) 107054cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); 1071078c3386SWang Nan } 1072078c3386SWang Nan } 1073f3058a1cSWang Nan 1074bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 1075bf8e8f4bSAdrian Hunter continue; 1076bf8e8f4bSAdrian Hunter 10779f21b815SMark Rutland cpu = cpu_map__idx(evsel->cpus, evlist_cpu); 10789f21b815SMark Rutland if (cpu == -1) 10799f21b815SMark Rutland continue; 10809f21b815SMark Rutland 1081bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 108204e21314SAdrian Hunter 108304e21314SAdrian Hunter if (*output == -1) { 108404e21314SAdrian Hunter *output = fd; 1085078c3386SWang Nan 1086078c3386SWang Nan if (perf_mmap__mmap(&maps[idx], mp, *output) < 0) 108704e21314SAdrian Hunter return -1; 108804e21314SAdrian Hunter } else { 108904e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 109004e21314SAdrian Hunter return -1; 109182396986SArnaldo Carvalho de Melo 1092078c3386SWang Nan perf_mmap__get(&maps[idx]); 109304e21314SAdrian Hunter } 109404e21314SAdrian Hunter 1095f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 1096f3058a1cSWang Nan 1097f90d194aSAdrian Hunter /* 1098f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 1099f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 1100f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 1101f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 1102f90d194aSAdrian Hunter * Therefore don't add it for polling. 1103f90d194aSAdrian Hunter */ 1104f90d194aSAdrian Hunter if (!evsel->system_wide && 1105078c3386SWang Nan __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) { 1106078c3386SWang Nan perf_mmap__put(&maps[idx]); 1107ad6765ddSArnaldo Carvalho de Melo return -1; 110882396986SArnaldo Carvalho de Melo } 1109033fa713SArnaldo Carvalho de Melo 11103c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 11113c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 11123c659eedSAdrian Hunter fd) < 0) 111304e21314SAdrian Hunter return -1; 11143c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 11153c659eedSAdrian Hunter thread); 11163c659eedSAdrian Hunter } 111704e21314SAdrian Hunter } 111804e21314SAdrian Hunter 111904e21314SAdrian Hunter return 0; 112004e21314SAdrian Hunter } 112104e21314SAdrian Hunter 1122a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 1123a8a8f3ebSAdrian Hunter struct mmap_params *mp) 112404e21314SAdrian Hunter { 1125aece948fSArnaldo Carvalho de Melo int cpu, thread; 1126b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 1127b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 1128aece948fSArnaldo Carvalho de Melo 1129e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 1130b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 1131aece948fSArnaldo Carvalho de Melo int output = -1; 1132078c3386SWang Nan int output_backward = -1; 1133aece948fSArnaldo Carvalho de Melo 1134718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 1135718c602dSAdrian Hunter true); 1136718c602dSAdrian Hunter 1137b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 1138a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 1139078c3386SWang Nan thread, &output, &output_backward)) 1140aece948fSArnaldo Carvalho de Melo goto out_unmap; 1141aece948fSArnaldo Carvalho de Melo } 1142aece948fSArnaldo Carvalho de Melo } 1143aece948fSArnaldo Carvalho de Melo 1144aece948fSArnaldo Carvalho de Melo return 0; 1145aece948fSArnaldo Carvalho de Melo 1146aece948fSArnaldo Carvalho de Melo out_unmap: 1147a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 1148aece948fSArnaldo Carvalho de Melo return -1; 1149aece948fSArnaldo Carvalho de Melo } 1150aece948fSArnaldo Carvalho de Melo 1151a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 1152a8a8f3ebSAdrian Hunter struct mmap_params *mp) 1153aece948fSArnaldo Carvalho de Melo { 1154aece948fSArnaldo Carvalho de Melo int thread; 1155b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 1156aece948fSArnaldo Carvalho de Melo 1157e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 1158b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 1159aece948fSArnaldo Carvalho de Melo int output = -1; 1160078c3386SWang Nan int output_backward = -1; 1161aece948fSArnaldo Carvalho de Melo 1162718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 1163718c602dSAdrian Hunter false); 1164718c602dSAdrian Hunter 1165a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 1166078c3386SWang Nan &output, &output_backward)) 1167aece948fSArnaldo Carvalho de Melo goto out_unmap; 1168aece948fSArnaldo Carvalho de Melo } 1169aece948fSArnaldo Carvalho de Melo 1170aece948fSArnaldo Carvalho de Melo return 0; 1171aece948fSArnaldo Carvalho de Melo 1172aece948fSArnaldo Carvalho de Melo out_unmap: 1173a1f72618SWang Nan perf_evlist__munmap_nofree(evlist); 1174aece948fSArnaldo Carvalho de Melo return -1; 1175aece948fSArnaldo Carvalho de Melo } 1176aece948fSArnaldo Carvalho de Melo 1177f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 1178994a1f78SJiri Olsa { 1179f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 11808185e881SArnaldo Carvalho de Melo int max; 11818185e881SArnaldo Carvalho de Melo 11828185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 11838185e881SArnaldo Carvalho de Melo /* 11848185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 11858185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 11868185e881SArnaldo Carvalho de Melo * die yet... 11878185e881SArnaldo Carvalho de Melo */ 11888185e881SArnaldo Carvalho de Melo max = 512; 11898185e881SArnaldo Carvalho de Melo } else { 11908185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 11918185e881SArnaldo Carvalho de Melo } 11928185e881SArnaldo Carvalho de Melo 11938185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 11941be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 11951be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 1196f5e7150cSArnaldo Carvalho de Melo 1197f5e7150cSArnaldo Carvalho de Melo return pages; 1198f5e7150cSArnaldo Carvalho de Melo } 1199f5e7150cSArnaldo Carvalho de Melo 12000c582449SJiri Olsa size_t perf_evlist__mmap_size(unsigned long pages) 1201f5e7150cSArnaldo Carvalho de Melo { 1202f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 1203f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 1204f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 1205994a1f78SJiri Olsa return 0; 1206994a1f78SJiri Olsa 1207994a1f78SJiri Olsa return (pages + 1) * page_size; 1208994a1f78SJiri Olsa } 1209994a1f78SJiri Olsa 121033c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 121133c2dcfdSDavid Ahern unsigned long max) 1212994a1f78SJiri Olsa { 12132fbe4abeSAdrian Hunter unsigned long pages, val; 121427050f53SJiri Olsa static struct parse_tag tags[] = { 121527050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 121627050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 121727050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 121827050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 121927050f53SJiri Olsa { .tag = 0 }, 122027050f53SJiri Olsa }; 1221994a1f78SJiri Olsa 12228973504bSDavid Ahern if (str == NULL) 122333c2dcfdSDavid Ahern return -EINVAL; 12248973504bSDavid Ahern 122527050f53SJiri Olsa val = parse_tag_value(str, tags); 12262fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 122727050f53SJiri Olsa /* we got file size value */ 122827050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 122927050f53SJiri Olsa } else { 123027050f53SJiri Olsa /* we got pages count value */ 123127050f53SJiri Olsa char *eptr; 1232994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 123333c2dcfdSDavid Ahern if (*eptr != '\0') 123433c2dcfdSDavid Ahern return -EINVAL; 123527050f53SJiri Olsa } 1236994a1f78SJiri Olsa 12372bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 123833c2dcfdSDavid Ahern /* leave number of pages at 0 */ 12391dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 12409808143bSJiri Olsa char buf[100]; 12419808143bSJiri Olsa 124233c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 124391529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 12441dbfa938SAdrian Hunter if (!pages) 12451dbfa938SAdrian Hunter return -EINVAL; 12469808143bSJiri Olsa 12479808143bSJiri Olsa unit_number__scnprintf(buf, sizeof(buf), pages * page_size); 12489808143bSJiri Olsa pr_info("rounding mmap pages size to %s (%lu pages)\n", 12499808143bSJiri Olsa buf, pages); 12502fbe4abeSAdrian Hunter } 12512fbe4abeSAdrian Hunter 125233c2dcfdSDavid Ahern if (pages > max) 125333c2dcfdSDavid Ahern return -EINVAL; 125433c2dcfdSDavid Ahern 125533c2dcfdSDavid Ahern return pages; 125633c2dcfdSDavid Ahern } 125733c2dcfdSDavid Ahern 1258e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 125933c2dcfdSDavid Ahern { 126033c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 126133c2dcfdSDavid Ahern long pages; 126233c2dcfdSDavid Ahern 1263f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 126433c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 126533c2dcfdSDavid Ahern 126633c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 126733c2dcfdSDavid Ahern if (pages < 0) { 126833c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 1269994a1f78SJiri Olsa return -1; 1270994a1f78SJiri Olsa } 1271994a1f78SJiri Olsa 1272994a1f78SJiri Olsa *mmap_pages = pages; 1273994a1f78SJiri Olsa return 0; 1274994a1f78SJiri Olsa } 1275994a1f78SJiri Olsa 1276e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 1277e9db1310SAdrian Hunter int unset __maybe_unused) 1278e9db1310SAdrian Hunter { 1279e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 1280e9db1310SAdrian Hunter } 1281e9db1310SAdrian Hunter 1282c83fa7f2SAdrian Hunter /** 1283718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 1284c83fa7f2SAdrian Hunter * @evlist: list of events 1285c83fa7f2SAdrian Hunter * @pages: map length in pages 1286c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 1287718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 1288718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1289f8a95309SArnaldo Carvalho de Melo * 1290c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1291c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1292c83fa7f2SAdrian Hunter * automatically. 1293f8a95309SArnaldo Carvalho de Melo * 1294718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1295718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1296718c602dSAdrian Hunter * 1297c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1298f8a95309SArnaldo Carvalho de Melo */ 1299718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, 1300718c602dSAdrian Hunter bool overwrite, unsigned int auxtrace_pages, 1301718c602dSAdrian Hunter bool auxtrace_overwrite) 1302f8a95309SArnaldo Carvalho de Melo { 1303aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 13047e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 13057e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1306a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1307a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1308a8a8f3ebSAdrian Hunter }; 130950a682ceSArnaldo Carvalho de Melo 13108db6d6b1SWang Nan if (!evlist->mmap) 13118db6d6b1SWang Nan evlist->mmap = perf_evlist__alloc_mmap(evlist); 13128db6d6b1SWang Nan if (!evlist->mmap) 1313f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1314f8a95309SArnaldo Carvalho de Melo 13151b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1316f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1317f8a95309SArnaldo Carvalho de Melo 1318f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1319994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 13202af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1321a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1322f8a95309SArnaldo Carvalho de Melo 1323718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1324718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1325718c602dSAdrian Hunter 1326e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1327f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1328a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1329a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1330f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1331f8a95309SArnaldo Carvalho de Melo } 1332f8a95309SArnaldo Carvalho de Melo 1333ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1334a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1335f8a95309SArnaldo Carvalho de Melo 1336a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1337f8a95309SArnaldo Carvalho de Melo } 13387e2ed097SArnaldo Carvalho de Melo 1339718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 1340718c602dSAdrian Hunter bool overwrite) 1341718c602dSAdrian Hunter { 1342718c602dSAdrian Hunter return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false); 1343718c602dSAdrian Hunter } 1344718c602dSAdrian Hunter 1345602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 13467e2ed097SArnaldo Carvalho de Melo { 134774bfd2b2SAdrian Hunter struct cpu_map *cpus; 134874bfd2b2SAdrian Hunter struct thread_map *threads; 13497e2ed097SArnaldo Carvalho de Melo 135074bfd2b2SAdrian Hunter threads = thread_map__new_str(target->pid, target->tid, target->uid); 135174bfd2b2SAdrian Hunter 135274bfd2b2SAdrian Hunter if (!threads) 13537e2ed097SArnaldo Carvalho de Melo return -1; 13547e2ed097SArnaldo Carvalho de Melo 13559c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 135674bfd2b2SAdrian Hunter cpus = cpu_map__dummy_new(); 1357879d77d0SNamhyung Kim else 135874bfd2b2SAdrian Hunter cpus = cpu_map__new(target->cpu_list); 13597e2ed097SArnaldo Carvalho de Melo 136074bfd2b2SAdrian Hunter if (!cpus) 13617e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 13627e2ed097SArnaldo Carvalho de Melo 1363ec9a77a7SAdrian Hunter evlist->has_user_cpus = !!target->cpu_list; 1364ec9a77a7SAdrian Hunter 136574bfd2b2SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 1366d5bc056eSAdrian Hunter 1367d5bc056eSAdrian Hunter return 0; 13687e2ed097SArnaldo Carvalho de Melo 13697e2ed097SArnaldo Carvalho de Melo out_delete_threads: 137074bfd2b2SAdrian Hunter thread_map__put(threads); 13717e2ed097SArnaldo Carvalho de Melo return -1; 13727e2ed097SArnaldo Carvalho de Melo } 13737e2ed097SArnaldo Carvalho de Melo 1374d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, 13753de5cfb0SJiri Olsa struct thread_map *threads) 13763de5cfb0SJiri Olsa { 1377934e0f20SAdrian Hunter /* 1378934e0f20SAdrian Hunter * Allow for the possibility that one or another of the maps isn't being 1379934e0f20SAdrian Hunter * changed i.e. don't put it. Note we are assuming the maps that are 1380934e0f20SAdrian Hunter * being applied are brand new and evlist is taking ownership of the 1381934e0f20SAdrian Hunter * original reference count of 1. If that is not the case it is up to 1382934e0f20SAdrian Hunter * the caller to increase the reference count. 1383934e0f20SAdrian Hunter */ 1384934e0f20SAdrian Hunter if (cpus != evlist->cpus) { 13853de5cfb0SJiri Olsa cpu_map__put(evlist->cpus); 1386a55e5663SArnaldo Carvalho de Melo evlist->cpus = cpu_map__get(cpus); 1387934e0f20SAdrian Hunter } 13883de5cfb0SJiri Olsa 1389934e0f20SAdrian Hunter if (threads != evlist->threads) { 13903de5cfb0SJiri Olsa thread_map__put(evlist->threads); 1391a55e5663SArnaldo Carvalho de Melo evlist->threads = thread_map__get(threads); 1392934e0f20SAdrian Hunter } 13933de5cfb0SJiri Olsa 1394ec9a77a7SAdrian Hunter perf_evlist__propagate_maps(evlist); 13953de5cfb0SJiri Olsa } 13963de5cfb0SJiri Olsa 139722c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist, 139822c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 139922c8a376SArnaldo Carvalho de Melo { 140022c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 140122c8a376SArnaldo Carvalho de Melo 1402e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 140322c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 140422c8a376SArnaldo Carvalho de Melo } 140522c8a376SArnaldo Carvalho de Melo 140622c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist, 140722c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 140822c8a376SArnaldo Carvalho de Melo { 140922c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 141022c8a376SArnaldo Carvalho de Melo 1411e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 141222c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 141322c8a376SArnaldo Carvalho de Melo } 141422c8a376SArnaldo Carvalho de Melo 141523d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 14160a102479SFrederic Weisbecker { 14170a102479SFrederic Weisbecker struct perf_evsel *evsel; 1418745cefc5SArnaldo Carvalho de Melo int err = 0; 1419745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1420b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 14210a102479SFrederic Weisbecker 1422e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1423745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 14240a102479SFrederic Weisbecker continue; 1425745cefc5SArnaldo Carvalho de Melo 1426d988d5eeSKan Liang /* 1427d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1428d988d5eeSKan Liang * So evlist and evsel should always be same. 1429d988d5eeSKan Liang */ 1430f47805a2SArnaldo Carvalho de Melo err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter); 143123d4aad4SArnaldo Carvalho de Melo if (err) { 143223d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1433745cefc5SArnaldo Carvalho de Melo break; 14340a102479SFrederic Weisbecker } 143523d4aad4SArnaldo Carvalho de Melo } 14360a102479SFrederic Weisbecker 1437745cefc5SArnaldo Carvalho de Melo return err; 1438745cefc5SArnaldo Carvalho de Melo } 1439745cefc5SArnaldo Carvalho de Melo 1440745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1441745cefc5SArnaldo Carvalho de Melo { 1442745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1443745cefc5SArnaldo Carvalho de Melo int err = 0; 1444745cefc5SArnaldo Carvalho de Melo 1445e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 1446fdf14720SWang Nan if (evsel->attr.type != PERF_TYPE_TRACEPOINT) 1447fdf14720SWang Nan continue; 1448fdf14720SWang Nan 144994ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1450745cefc5SArnaldo Carvalho de Melo if (err) 1451745cefc5SArnaldo Carvalho de Melo break; 1452745cefc5SArnaldo Carvalho de Melo } 1453745cefc5SArnaldo Carvalho de Melo 1454745cefc5SArnaldo Carvalho de Melo return err; 14550a102479SFrederic Weisbecker } 145674429964SFrederic Weisbecker 1457be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1458cfd70a26SArnaldo Carvalho de Melo { 1459cfd70a26SArnaldo Carvalho de Melo char *filter; 1460be199adaSArnaldo Carvalho de Melo int ret = -1; 1461be199adaSArnaldo Carvalho de Melo size_t i; 1462cfd70a26SArnaldo Carvalho de Melo 1463be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1464be199adaSArnaldo Carvalho de Melo if (i == 0) { 1465be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1466cfd70a26SArnaldo Carvalho de Melo return -1; 1467be199adaSArnaldo Carvalho de Melo } else { 1468be199adaSArnaldo Carvalho de Melo char *tmp; 1469be199adaSArnaldo Carvalho de Melo 1470be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1471be199adaSArnaldo Carvalho de Melo goto out_free; 1472be199adaSArnaldo Carvalho de Melo 1473be199adaSArnaldo Carvalho de Melo free(filter); 1474be199adaSArnaldo Carvalho de Melo filter = tmp; 1475be199adaSArnaldo Carvalho de Melo } 1476be199adaSArnaldo Carvalho de Melo } 1477cfd70a26SArnaldo Carvalho de Melo 1478cfd70a26SArnaldo Carvalho de Melo ret = perf_evlist__set_filter(evlist, filter); 1479be199adaSArnaldo Carvalho de Melo out_free: 1480cfd70a26SArnaldo Carvalho de Melo free(filter); 1481cfd70a26SArnaldo Carvalho de Melo return ret; 1482cfd70a26SArnaldo Carvalho de Melo } 1483cfd70a26SArnaldo Carvalho de Melo 1484be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) 1485be199adaSArnaldo Carvalho de Melo { 1486be199adaSArnaldo Carvalho de Melo return perf_evlist__set_filter_pids(evlist, 1, &pid); 1487be199adaSArnaldo Carvalho de Melo } 1488be199adaSArnaldo Carvalho de Melo 14890c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 149074429964SFrederic Weisbecker { 149175562573SAdrian Hunter struct perf_evsel *pos; 1492c2a70653SArnaldo Carvalho de Melo 149375562573SAdrian Hunter if (evlist->nr_entries == 1) 149475562573SAdrian Hunter return true; 149575562573SAdrian Hunter 149675562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 149775562573SAdrian Hunter return false; 149875562573SAdrian Hunter 1499e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 150075562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 150175562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1502c2a70653SArnaldo Carvalho de Melo return false; 150374429964SFrederic Weisbecker } 150474429964SFrederic Weisbecker 1505c2a70653SArnaldo Carvalho de Melo return true; 1506c2a70653SArnaldo Carvalho de Melo } 1507c2a70653SArnaldo Carvalho de Melo 150875562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1509c2a70653SArnaldo Carvalho de Melo { 151075562573SAdrian Hunter struct perf_evsel *evsel; 151175562573SAdrian Hunter 151275562573SAdrian Hunter if (evlist->combined_sample_type) 151375562573SAdrian Hunter return evlist->combined_sample_type; 151475562573SAdrian Hunter 1515e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 151675562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 151775562573SAdrian Hunter 151875562573SAdrian Hunter return evlist->combined_sample_type; 151975562573SAdrian Hunter } 152075562573SAdrian Hunter 152175562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 152275562573SAdrian Hunter { 152375562573SAdrian Hunter evlist->combined_sample_type = 0; 152475562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1525c2a70653SArnaldo Carvalho de Melo } 1526c2a70653SArnaldo Carvalho de Melo 152798df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) 152898df858eSAndi Kleen { 152998df858eSAndi Kleen struct perf_evsel *evsel; 153098df858eSAndi Kleen u64 branch_type = 0; 153198df858eSAndi Kleen 1532e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) 153398df858eSAndi Kleen branch_type |= evsel->attr.branch_sample_type; 153498df858eSAndi Kleen return branch_type; 153598df858eSAndi Kleen } 153698df858eSAndi Kleen 15379ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 15389ede473cSJiri Olsa { 15399ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 15409ede473cSJiri Olsa u64 read_format = first->attr.read_format; 15419ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 15429ede473cSJiri Olsa 1543e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 15449ede473cSJiri Olsa if (read_format != pos->attr.read_format) 15459ede473cSJiri Olsa return false; 15469ede473cSJiri Olsa } 15479ede473cSJiri Olsa 15489ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 15499ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 15509ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 15519ede473cSJiri Olsa return false; 15529ede473cSJiri Olsa } 15539ede473cSJiri Olsa 15549ede473cSJiri Olsa return true; 15559ede473cSJiri Olsa } 15569ede473cSJiri Olsa 15579ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 15589ede473cSJiri Olsa { 15599ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 15609ede473cSJiri Olsa return first->attr.read_format; 15619ede473cSJiri Olsa } 15629ede473cSJiri Olsa 15630c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 156481e36bffSArnaldo Carvalho de Melo { 15650c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 156681e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 156781e36bffSArnaldo Carvalho de Melo u64 sample_type; 156881e36bffSArnaldo Carvalho de Melo u16 size = 0; 156981e36bffSArnaldo Carvalho de Melo 157081e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 157181e36bffSArnaldo Carvalho de Melo goto out; 157281e36bffSArnaldo Carvalho de Melo 157381e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 157481e36bffSArnaldo Carvalho de Melo 157581e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 157681e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 157781e36bffSArnaldo Carvalho de Melo 157881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 157981e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 158081e36bffSArnaldo Carvalho de Melo 158181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 158281e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 158381e36bffSArnaldo Carvalho de Melo 158481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 158581e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 158681e36bffSArnaldo Carvalho de Melo 158781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 158881e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 158975562573SAdrian Hunter 159075562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 159175562573SAdrian Hunter size += sizeof(data->id); 159281e36bffSArnaldo Carvalho de Melo out: 159381e36bffSArnaldo Carvalho de Melo return size; 159481e36bffSArnaldo Carvalho de Melo } 159581e36bffSArnaldo Carvalho de Melo 15960c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1597c2a70653SArnaldo Carvalho de Melo { 15980c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1599c2a70653SArnaldo Carvalho de Melo 1600e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_continue(evlist, pos) { 1601c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1602c2a70653SArnaldo Carvalho de Melo return false; 1603c2a70653SArnaldo Carvalho de Melo } 1604c2a70653SArnaldo Carvalho de Melo 1605c2a70653SArnaldo Carvalho de Melo return true; 160674429964SFrederic Weisbecker } 160774429964SFrederic Weisbecker 16080c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 160974429964SFrederic Weisbecker { 16100c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1611c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 161274429964SFrederic Weisbecker } 161381cce8deSArnaldo Carvalho de Melo 161481cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 161581cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 161681cce8deSArnaldo Carvalho de Melo { 161781cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 161881cce8deSArnaldo Carvalho de Melo } 1619727ab04eSArnaldo Carvalho de Melo 1620a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1621a74b4b66SNamhyung Kim { 1622a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1623a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1624a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 1625a74b4b66SNamhyung Kim 1626e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_reverse(evlist, evsel) { 162718ef15c6SArnaldo Carvalho de Melo int n = evsel->cpus ? evsel->cpus->nr : ncpus; 16288ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 16298ad9219eSStephane Eranian } 1630a74b4b66SNamhyung Kim } 1631a74b4b66SNamhyung Kim 16324112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 16334112eb18SArnaldo Carvalho de Melo { 16348c0498b6SAdrian Hunter struct cpu_map *cpus; 16358c0498b6SAdrian Hunter struct thread_map *threads; 16364112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 16374112eb18SArnaldo Carvalho de Melo 16384112eb18SArnaldo Carvalho de Melo /* 16394112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 16404112eb18SArnaldo Carvalho de Melo * an all cpus map. 16414112eb18SArnaldo Carvalho de Melo * 16424112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 16434112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 16444112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 16454112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 16464112eb18SArnaldo Carvalho de Melo */ 16478c0498b6SAdrian Hunter cpus = cpu_map__new(NULL); 16488c0498b6SAdrian Hunter if (!cpus) 16494112eb18SArnaldo Carvalho de Melo goto out; 16504112eb18SArnaldo Carvalho de Melo 16518c0498b6SAdrian Hunter threads = thread_map__new_dummy(); 16528c0498b6SAdrian Hunter if (!threads) 16538c0498b6SAdrian Hunter goto out_put; 16544112eb18SArnaldo Carvalho de Melo 16558c0498b6SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 16564112eb18SArnaldo Carvalho de Melo out: 16574112eb18SArnaldo Carvalho de Melo return err; 16588c0498b6SAdrian Hunter out_put: 16598c0498b6SAdrian Hunter cpu_map__put(cpus); 16604112eb18SArnaldo Carvalho de Melo goto out; 16614112eb18SArnaldo Carvalho de Melo } 16624112eb18SArnaldo Carvalho de Melo 16636a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1664727ab04eSArnaldo Carvalho de Melo { 16656a4bb04cSJiri Olsa struct perf_evsel *evsel; 1666a74b4b66SNamhyung Kim int err; 1667727ab04eSArnaldo Carvalho de Melo 16684112eb18SArnaldo Carvalho de Melo /* 16694112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 16704112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 16714112eb18SArnaldo Carvalho de Melo */ 16724112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 16734112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 16744112eb18SArnaldo Carvalho de Melo if (err < 0) 16754112eb18SArnaldo Carvalho de Melo goto out_err; 16764112eb18SArnaldo Carvalho de Melo } 16774112eb18SArnaldo Carvalho de Melo 1678733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1679733cd2feSAdrian Hunter 1680e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 168123df7f79SAdrian Hunter err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); 1682727ab04eSArnaldo Carvalho de Melo if (err < 0) 1683727ab04eSArnaldo Carvalho de Melo goto out_err; 1684727ab04eSArnaldo Carvalho de Melo } 1685727ab04eSArnaldo Carvalho de Melo 1686727ab04eSArnaldo Carvalho de Melo return 0; 1687727ab04eSArnaldo Carvalho de Melo out_err: 1688a74b4b66SNamhyung Kim perf_evlist__close(evlist); 168941c21a68SNamhyung Kim errno = -err; 1690727ab04eSArnaldo Carvalho de Melo return err; 1691727ab04eSArnaldo Carvalho de Melo } 169235b9d88eSArnaldo Carvalho de Melo 1693602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 169455e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1695735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 169635b9d88eSArnaldo Carvalho de Melo { 169735b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 169835b9d88eSArnaldo Carvalho de Melo char bf; 169935b9d88eSArnaldo Carvalho de Melo 170035b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 170135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 170235b9d88eSArnaldo Carvalho de Melo return -1; 170335b9d88eSArnaldo Carvalho de Melo } 170435b9d88eSArnaldo Carvalho de Melo 170535b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 170635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 170735b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 170835b9d88eSArnaldo Carvalho de Melo } 170935b9d88eSArnaldo Carvalho de Melo 171035b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 171135b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 171235b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 171335b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 171435b9d88eSArnaldo Carvalho de Melo } 171535b9d88eSArnaldo Carvalho de Melo 171635b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 17175f1c4225SArnaldo Carvalho de Melo int ret; 17185f1c4225SArnaldo Carvalho de Melo 1719119fa3c9SNamhyung Kim if (pipe_output) 172035b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 172135b9d88eSArnaldo Carvalho de Melo 17220817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 17230817df08SDavid Ahern 172435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 172535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 172635b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 172735b9d88eSArnaldo Carvalho de Melo 172835b9d88eSArnaldo Carvalho de Melo /* 172935b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 173035b9d88eSArnaldo Carvalho de Melo */ 173135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 173235b9d88eSArnaldo Carvalho de Melo 173335b9d88eSArnaldo Carvalho de Melo /* 173435b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 173535b9d88eSArnaldo Carvalho de Melo */ 17365f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 17375f1c4225SArnaldo Carvalho de Melo /* 17385f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 17395f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 17405f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 17415f1c4225SArnaldo Carvalho de Melo * 174220f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 17435f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 17445f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 17455f1c4225SArnaldo Carvalho de Melo * here. 17465f1c4225SArnaldo Carvalho de Melo */ 17475f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 17485f1c4225SArnaldo Carvalho de Melo if (ret == -1) 174935b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 17505f1c4225SArnaldo Carvalho de Melo exit(ret); 17515f1c4225SArnaldo Carvalho de Melo } 175235b9d88eSArnaldo Carvalho de Melo 175335b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 175435b9d88eSArnaldo Carvalho de Melo 1755735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1756f33cbe72SArnaldo Carvalho de Melo union sigval val; 1757f33cbe72SArnaldo Carvalho de Melo 1758f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1759f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 176035b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1761f33cbe72SArnaldo Carvalho de Melo } else 1762f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 176335b9d88eSArnaldo Carvalho de Melo exit(-1); 176435b9d88eSArnaldo Carvalho de Melo } 176535b9d88eSArnaldo Carvalho de Melo 1766735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1767735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1768735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1769735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1770735f7e0bSArnaldo Carvalho de Melo }; 1771735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1772735f7e0bSArnaldo Carvalho de Melo } 1773735f7e0bSArnaldo Carvalho de Melo 17741aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 17751aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 17761aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 17771aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 17781aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 17791aaf63b1SArnaldo Carvalho de Melo } 1780e13798c7SJiri Olsa thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 17811aaf63b1SArnaldo Carvalho de Melo } 178235b9d88eSArnaldo Carvalho de Melo 178335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 178435b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 178535b9d88eSArnaldo Carvalho de Melo /* 178635b9d88eSArnaldo Carvalho de Melo * wait for child to settle 178735b9d88eSArnaldo Carvalho de Melo */ 178835b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 178935b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 179035b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 179135b9d88eSArnaldo Carvalho de Melo } 179235b9d88eSArnaldo Carvalho de Melo 1793bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 179435b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 179535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 179635b9d88eSArnaldo Carvalho de Melo return 0; 179735b9d88eSArnaldo Carvalho de Melo 179835b9d88eSArnaldo Carvalho de Melo out_close_pipes: 179935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 180035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 180135b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 180235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 180335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 180435b9d88eSArnaldo Carvalho de Melo return -1; 180535b9d88eSArnaldo Carvalho de Melo } 180635b9d88eSArnaldo Carvalho de Melo 180735b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 180835b9d88eSArnaldo Carvalho de Melo { 180935b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1810b3824404SDavid Ahern char bf = 0; 1811bcf3145fSNamhyung Kim int ret; 181235b9d88eSArnaldo Carvalho de Melo /* 181335b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 181435b9d88eSArnaldo Carvalho de Melo */ 1815bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1816bcf3145fSNamhyung Kim if (ret < 0) 1817e978be9eSSoramichi Akiyama perror("unable to write to pipe"); 1818bcf3145fSNamhyung Kim 1819bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1820bcf3145fSNamhyung Kim return ret; 182135b9d88eSArnaldo Carvalho de Melo } 182235b9d88eSArnaldo Carvalho de Melo 182335b9d88eSArnaldo Carvalho de Melo return 0; 182435b9d88eSArnaldo Carvalho de Melo } 1825cb0b29e0SArnaldo Carvalho de Melo 1826a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 18270807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1828cb0b29e0SArnaldo Carvalho de Melo { 182975562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 183075562573SAdrian Hunter 183175562573SAdrian Hunter if (!evsel) 183275562573SAdrian Hunter return -EFAULT; 18330807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1834cb0b29e0SArnaldo Carvalho de Melo } 183578f067b3SArnaldo Carvalho de Melo 183678f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 183778f067b3SArnaldo Carvalho de Melo { 183878f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 183978f067b3SArnaldo Carvalho de Melo size_t printed = 0; 184078f067b3SArnaldo Carvalho de Melo 1841e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 184278f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 184378f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 184478f067b3SArnaldo Carvalho de Melo } 184578f067b3SArnaldo Carvalho de Melo 1846b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 184778f067b3SArnaldo Carvalho de Melo } 18486ef068cbSArnaldo Carvalho de Melo 1849d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist, 1850a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1851a8f23d8fSArnaldo Carvalho de Melo { 1852a8f23d8fSArnaldo Carvalho de Melo int printed, value; 1853c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1854a8f23d8fSArnaldo Carvalho de Melo 1855a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1856a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1857a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1858a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1859a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1860a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1861a8f23d8fSArnaldo Carvalho de Melo 18621a47245dSAdrian Hunter value = perf_event_paranoid(); 1863a8f23d8fSArnaldo Carvalho de Melo 1864a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1865a8f23d8fSArnaldo Carvalho de Melo 1866a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1867a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1868a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1869a8f23d8fSArnaldo Carvalho de Melo } 1870a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 18715229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1872a8f23d8fSArnaldo Carvalho de Melo 1873a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 18745229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 18755229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1876a8f23d8fSArnaldo Carvalho de Melo break; 1877d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1878d9aade7fSArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1879d9aade7fSArnaldo Carvalho de Melo int max_freq; 1880d9aade7fSArnaldo Carvalho de Melo 1881d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1882d9aade7fSArnaldo Carvalho de Melo goto out_default; 1883d9aade7fSArnaldo Carvalho de Melo 1884d9aade7fSArnaldo Carvalho de Melo if (first->attr.sample_freq < (u64)max_freq) 1885d9aade7fSArnaldo Carvalho de Melo goto out_default; 1886d9aade7fSArnaldo Carvalho de Melo 1887d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1888d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1889d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1890d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1891d9aade7fSArnaldo Carvalho de Melo emsg, max_freq, first->attr.sample_freq); 1892d9aade7fSArnaldo Carvalho de Melo break; 1893d9aade7fSArnaldo Carvalho de Melo } 1894a8f23d8fSArnaldo Carvalho de Melo default: 1895d9aade7fSArnaldo Carvalho de Melo out_default: 1896a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1897a8f23d8fSArnaldo Carvalho de Melo break; 1898a8f23d8fSArnaldo Carvalho de Melo } 1899a8f23d8fSArnaldo Carvalho de Melo 1900a8f23d8fSArnaldo Carvalho de Melo return 0; 1901a8f23d8fSArnaldo Carvalho de Melo } 1902a025e4f0SAdrian Hunter 1903956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1904956fa571SArnaldo Carvalho de Melo { 1905c8b5f2c9SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); 1906e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1907956fa571SArnaldo Carvalho de Melo 1908956fa571SArnaldo Carvalho de Melo switch (err) { 1909956fa571SArnaldo Carvalho de Melo case EPERM: 1910e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1911e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1912e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1913956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1914e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1915e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1916e965bea1SArnaldo Carvalho de Melo 1917e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1918e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1919e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1920e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1921e965bea1SArnaldo Carvalho de Melo } 1922e965bea1SArnaldo Carvalho de Melo 1923e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1924e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1925956fa571SArnaldo Carvalho de Melo break; 1926956fa571SArnaldo Carvalho de Melo default: 1927956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1928956fa571SArnaldo Carvalho de Melo break; 1929956fa571SArnaldo Carvalho de Melo } 1930956fa571SArnaldo Carvalho de Melo 1931956fa571SArnaldo Carvalho de Melo return 0; 1932956fa571SArnaldo Carvalho de Melo } 1933956fa571SArnaldo Carvalho de Melo 1934a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1935a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1936a025e4f0SAdrian Hunter { 1937a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1938a025e4f0SAdrian Hunter LIST_HEAD(move); 1939a025e4f0SAdrian Hunter 1940a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1941a025e4f0SAdrian Hunter return; 1942a025e4f0SAdrian Hunter 1943e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry_safe(evlist, n, evsel) { 1944a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1945a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1946a025e4f0SAdrian Hunter } 1947a025e4f0SAdrian Hunter 1948a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1949a025e4f0SAdrian Hunter } 195060b0896cSAdrian Hunter 195160b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 195260b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 195360b0896cSAdrian Hunter { 195460b0896cSAdrian Hunter struct perf_evsel *evsel; 195560b0896cSAdrian Hunter 195660b0896cSAdrian Hunter if (tracking_evsel->tracking) 195760b0896cSAdrian Hunter return; 195860b0896cSAdrian Hunter 1959e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 196060b0896cSAdrian Hunter if (evsel != tracking_evsel) 196160b0896cSAdrian Hunter evsel->tracking = false; 196260b0896cSAdrian Hunter } 196360b0896cSAdrian Hunter 196460b0896cSAdrian Hunter tracking_evsel->tracking = true; 196560b0896cSAdrian Hunter } 19667630b3e2SWang Nan 19677630b3e2SWang Nan struct perf_evsel * 19687630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, 19697630b3e2SWang Nan const char *str) 19707630b3e2SWang Nan { 19717630b3e2SWang Nan struct perf_evsel *evsel; 19727630b3e2SWang Nan 1973e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, evsel) { 19747630b3e2SWang Nan if (!evsel->name) 19757630b3e2SWang Nan continue; 19767630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 19777630b3e2SWang Nan return evsel; 19787630b3e2SWang Nan } 19797630b3e2SWang Nan 19807630b3e2SWang Nan return NULL; 19817630b3e2SWang Nan } 198254cc54deSWang Nan 198354cc54deSWang Nan void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, 198454cc54deSWang Nan enum bkw_mmap_state state) 198554cc54deSWang Nan { 198654cc54deSWang Nan enum bkw_mmap_state old_state = evlist->bkw_mmap_state; 198754cc54deSWang Nan enum action { 198854cc54deSWang Nan NONE, 198954cc54deSWang Nan PAUSE, 199054cc54deSWang Nan RESUME, 199154cc54deSWang Nan } action = NONE; 199254cc54deSWang Nan 199354cc54deSWang Nan if (!evlist->backward_mmap) 199454cc54deSWang Nan return; 199554cc54deSWang Nan 199654cc54deSWang Nan switch (old_state) { 199754cc54deSWang Nan case BKW_MMAP_NOTREADY: { 199854cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 199954cc54deSWang Nan goto state_err;; 200054cc54deSWang Nan break; 200154cc54deSWang Nan } 200254cc54deSWang Nan case BKW_MMAP_RUNNING: { 200354cc54deSWang Nan if (state != BKW_MMAP_DATA_PENDING) 200454cc54deSWang Nan goto state_err; 200554cc54deSWang Nan action = PAUSE; 200654cc54deSWang Nan break; 200754cc54deSWang Nan } 200854cc54deSWang Nan case BKW_MMAP_DATA_PENDING: { 200954cc54deSWang Nan if (state != BKW_MMAP_EMPTY) 201054cc54deSWang Nan goto state_err; 201154cc54deSWang Nan break; 201254cc54deSWang Nan } 201354cc54deSWang Nan case BKW_MMAP_EMPTY: { 201454cc54deSWang Nan if (state != BKW_MMAP_RUNNING) 201554cc54deSWang Nan goto state_err; 201654cc54deSWang Nan action = RESUME; 201754cc54deSWang Nan break; 201854cc54deSWang Nan } 201954cc54deSWang Nan default: 202054cc54deSWang Nan WARN_ONCE(1, "Shouldn't get there\n"); 202154cc54deSWang Nan } 202254cc54deSWang Nan 202354cc54deSWang Nan evlist->bkw_mmap_state = state; 202454cc54deSWang Nan 202554cc54deSWang Nan switch (action) { 202654cc54deSWang Nan case PAUSE: 202754cc54deSWang Nan perf_evlist__pause(evlist); 202854cc54deSWang Nan break; 202954cc54deSWang Nan case RESUME: 203054cc54deSWang Nan perf_evlist__resume(evlist); 203154cc54deSWang Nan break; 203254cc54deSWang Nan case NONE: 203354cc54deSWang Nan default: 203454cc54deSWang Nan break; 203554cc54deSWang Nan } 203654cc54deSWang Nan 203754cc54deSWang Nan state_err: 203854cc54deSWang Nan return; 203954cc54deSWang Nan } 2040