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> 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1412864b31SNamhyung Kim #include "target.h" 15361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 16361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 17e3e1a54fSAdrian Hunter #include "debug.h" 1835b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 19361c99a6SArnaldo Carvalho de Melo 2050d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 21994a1f78SJiri Olsa #include "parse-options.h" 2250d08e47SArnaldo Carvalho de Melo 23f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 24f8a95309SArnaldo Carvalho de Melo 2570db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2670db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 270389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 2870db7533SArnaldo Carvalho de Melo 29e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx); 30e4b356b5SArnaldo Carvalho de Melo static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx); 31e4b356b5SArnaldo Carvalho de Melo 32f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 33a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 34f8a95309SArnaldo Carvalho de Melo 357e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 367e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 37361c99a6SArnaldo Carvalho de Melo { 3870db7533SArnaldo Carvalho de Melo int i; 3970db7533SArnaldo Carvalho de Melo 4070db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 4170db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 42361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 437e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 441b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 4535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 46361c99a6SArnaldo Carvalho de Melo } 47361c99a6SArnaldo Carvalho de Melo 48334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 49ef1d1af2SArnaldo Carvalho de Melo { 50ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 51ef1d1af2SArnaldo Carvalho de Melo 52ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 53334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 54ef1d1af2SArnaldo Carvalho de Melo 55361c99a6SArnaldo Carvalho de Melo return evlist; 56361c99a6SArnaldo Carvalho de Melo } 57361c99a6SArnaldo Carvalho de Melo 58b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 59b22d54b0SJiri Olsa { 60b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 61b22d54b0SJiri Olsa 62b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 63b22d54b0SJiri Olsa perf_evlist__delete(evlist); 64b22d54b0SJiri Olsa evlist = NULL; 65b22d54b0SJiri Olsa } 66b22d54b0SJiri Olsa 67b22d54b0SJiri Olsa return evlist; 68b22d54b0SJiri Olsa } 69b22d54b0SJiri Olsa 7075562573SAdrian Hunter /** 7175562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 7275562573SAdrian Hunter * @evlist: selected event list 7375562573SAdrian Hunter * 7475562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 7575562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 7675562573SAdrian Hunter */ 7775562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 7875562573SAdrian Hunter { 7975562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 8075562573SAdrian Hunter 8175562573SAdrian Hunter evlist->id_pos = first->id_pos; 8275562573SAdrian Hunter evlist->is_pos = first->is_pos; 8375562573SAdrian Hunter } 8475562573SAdrian Hunter 85733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 86733cd2feSAdrian Hunter { 87733cd2feSAdrian Hunter struct perf_evsel *evsel; 88733cd2feSAdrian Hunter 890050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 90733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 91733cd2feSAdrian Hunter 92733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 93733cd2feSAdrian Hunter } 94733cd2feSAdrian Hunter 95361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 96361c99a6SArnaldo Carvalho de Melo { 97361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 98361c99a6SArnaldo Carvalho de Melo 990050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, pos) { 100361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 101d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 102361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 103361c99a6SArnaldo Carvalho de Melo } 104361c99a6SArnaldo Carvalho de Melo 105361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 106361c99a6SArnaldo Carvalho de Melo } 107361c99a6SArnaldo Carvalho de Melo 108ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 109ef1d1af2SArnaldo Carvalho de Melo { 11004662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1111b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 112ef1d1af2SArnaldo Carvalho de Melo } 113ef1d1af2SArnaldo Carvalho de Melo 114361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 115361c99a6SArnaldo Carvalho de Melo { 116983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 117f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 118f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 119186fbb74SJiri Olsa thread_map__put(evlist->threads); 12003ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 12103ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 122361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 123ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 124361c99a6SArnaldo Carvalho de Melo free(evlist); 125361c99a6SArnaldo Carvalho de Melo } 126361c99a6SArnaldo Carvalho de Melo 127361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 128361c99a6SArnaldo Carvalho de Melo { 129d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 130361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 131ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 13260b0896cSAdrian Hunter entry->tracking = !entry->idx; 133ef503831SArnaldo Carvalho de Melo 13475562573SAdrian Hunter if (!evlist->nr_entries++) 13575562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 136361c99a6SArnaldo Carvalho de Melo } 137361c99a6SArnaldo Carvalho de Melo 1380529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 13950d08e47SArnaldo Carvalho de Melo struct list_head *list, 14050d08e47SArnaldo Carvalho de Melo int nr_entries) 14150d08e47SArnaldo Carvalho de Melo { 14275562573SAdrian Hunter bool set_id_pos = !evlist->nr_entries; 14375562573SAdrian Hunter 14450d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 14550d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 14675562573SAdrian Hunter if (set_id_pos) 14775562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 14850d08e47SArnaldo Carvalho de Melo } 14950d08e47SArnaldo Carvalho de Melo 15063dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 15163dab225SArnaldo Carvalho de Melo { 15263dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 15363dab225SArnaldo Carvalho de Melo 15463dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 15597f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 15697f63e4aSNamhyung Kim 15797f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 15863dab225SArnaldo Carvalho de Melo 1590050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 16063dab225SArnaldo Carvalho de Melo evsel->leader = leader; 16163dab225SArnaldo Carvalho de Melo } 16263dab225SArnaldo Carvalho de Melo } 16363dab225SArnaldo Carvalho de Melo 16463dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1656a4bb04cSJiri Olsa { 16697f63e4aSNamhyung Kim if (evlist->nr_entries) { 16797f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 16863dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1696a4bb04cSJiri Olsa } 17097f63e4aSNamhyung Kim } 1716a4bb04cSJiri Olsa 172361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 173361c99a6SArnaldo Carvalho de Melo { 174361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 175361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 176361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 177361c99a6SArnaldo Carvalho de Melo }; 1781aed2671SJoerg Roedel struct perf_evsel *evsel; 179361c99a6SArnaldo Carvalho de Melo 1801aed2671SJoerg Roedel event_attr_init(&attr); 1811aed2671SJoerg Roedel 182ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 183361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 184cc2d86b0SStephane Eranian goto error; 185cc2d86b0SStephane Eranian 186cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 187cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 188cc2d86b0SStephane Eranian if (!evsel->name) 189cc2d86b0SStephane Eranian goto error_free; 190361c99a6SArnaldo Carvalho de Melo 191361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 192361c99a6SArnaldo Carvalho de Melo return 0; 193cc2d86b0SStephane Eranian error_free: 194cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 195cc2d86b0SStephane Eranian error: 196cc2d86b0SStephane Eranian return -ENOMEM; 197361c99a6SArnaldo Carvalho de Melo } 1985c581041SArnaldo Carvalho de Melo 199e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 20050d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 20150d08e47SArnaldo Carvalho de Melo { 20250d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 20350d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 20450d08e47SArnaldo Carvalho de Melo size_t i; 20550d08e47SArnaldo Carvalho de Melo 20650d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 207ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 20850d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 20950d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 21050d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 21150d08e47SArnaldo Carvalho de Melo } 21250d08e47SArnaldo Carvalho de Melo 21350d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 21450d08e47SArnaldo Carvalho de Melo 21550d08e47SArnaldo Carvalho de Melo return 0; 21650d08e47SArnaldo Carvalho de Melo 21750d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 2180050f7aaSArnaldo Carvalho de Melo __evlist__for_each_safe(&head, n, evsel) 21950d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 22050d08e47SArnaldo Carvalho de Melo return -1; 22150d08e47SArnaldo Carvalho de Melo } 22250d08e47SArnaldo Carvalho de Melo 22379695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 22479695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 22579695e1bSArnaldo Carvalho de Melo { 22679695e1bSArnaldo Carvalho de Melo size_t i; 22779695e1bSArnaldo Carvalho de Melo 22879695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 22979695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 23079695e1bSArnaldo Carvalho de Melo 23179695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 23279695e1bSArnaldo Carvalho de Melo } 23379695e1bSArnaldo Carvalho de Melo 234da378962SArnaldo Carvalho de Melo struct perf_evsel * 235ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 236ee29be62SArnaldo Carvalho de Melo { 237ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 238ee29be62SArnaldo Carvalho de Melo 2390050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 240ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 241ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 242ee29be62SArnaldo Carvalho de Melo return evsel; 243ee29be62SArnaldo Carvalho de Melo } 244ee29be62SArnaldo Carvalho de Melo 245ee29be62SArnaldo Carvalho de Melo return NULL; 246ee29be62SArnaldo Carvalho de Melo } 247ee29be62SArnaldo Carvalho de Melo 248a2f2804aSDavid Ahern struct perf_evsel * 249a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 250a2f2804aSDavid Ahern const char *name) 251a2f2804aSDavid Ahern { 252a2f2804aSDavid Ahern struct perf_evsel *evsel; 253a2f2804aSDavid Ahern 2540050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 255a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 256a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 257a2f2804aSDavid Ahern return evsel; 258a2f2804aSDavid Ahern } 259a2f2804aSDavid Ahern 260a2f2804aSDavid Ahern return NULL; 261a2f2804aSDavid Ahern } 262a2f2804aSDavid Ahern 26339876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 26439876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 26539876e7dSArnaldo Carvalho de Melo { 266ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 26739876e7dSArnaldo Carvalho de Melo 26839876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 26939876e7dSArnaldo Carvalho de Melo return -1; 27039876e7dSArnaldo Carvalho de Melo 271744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 27239876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 27339876e7dSArnaldo Carvalho de Melo return 0; 27439876e7dSArnaldo Carvalho de Melo } 27539876e7dSArnaldo Carvalho de Melo 276bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 277bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 278bf8e8f4bSAdrian Hunter { 279bf8e8f4bSAdrian Hunter if (evsel->system_wide) 280bf8e8f4bSAdrian Hunter return 1; 281bf8e8f4bSAdrian Hunter else 282bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 283bf8e8f4bSAdrian Hunter } 284bf8e8f4bSAdrian Hunter 2854152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2864152ab37SArnaldo Carvalho de Melo { 2874152ab37SArnaldo Carvalho de Melo int cpu, thread; 2884152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 289b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 290bf8e8f4bSAdrian Hunter int nr_threads; 2914152ab37SArnaldo Carvalho de Melo 292b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 2930050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 294395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 2953fe4430dSJiri Olsa continue; 296bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 297b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 29855da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 29955da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 3004152ab37SArnaldo Carvalho de Melo } 3014152ab37SArnaldo Carvalho de Melo } 3022b56bcfbSArnaldo Carvalho de Melo 3032b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3044152ab37SArnaldo Carvalho de Melo } 3054152ab37SArnaldo Carvalho de Melo 306764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 307764e16a3SDavid Ahern { 308764e16a3SDavid Ahern int cpu, thread; 309764e16a3SDavid Ahern struct perf_evsel *pos; 310b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 311bf8e8f4bSAdrian Hunter int nr_threads; 312764e16a3SDavid Ahern 313b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 3140050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 315395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3163fe4430dSJiri Olsa continue; 317bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 318b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 31955da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 32055da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 321764e16a3SDavid Ahern } 322764e16a3SDavid Ahern } 3232b56bcfbSArnaldo Carvalho de Melo 3242b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3252b56bcfbSArnaldo Carvalho de Melo } 3262b56bcfbSArnaldo Carvalho de Melo 3272b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist) 3282b56bcfbSArnaldo Carvalho de Melo { 3292b56bcfbSArnaldo Carvalho de Melo (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); 330764e16a3SDavid Ahern } 331764e16a3SDavid Ahern 332395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist, 333395c3070SAdrian Hunter struct perf_evsel *evsel) 334395c3070SAdrian Hunter { 335395c3070SAdrian Hunter int cpu, thread, err; 336bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 337bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 338395c3070SAdrian Hunter 339395c3070SAdrian Hunter if (!evsel->fd) 340395c3070SAdrian Hunter return 0; 341395c3070SAdrian Hunter 342bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 343bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 344395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 345395c3070SAdrian Hunter PERF_EVENT_IOC_DISABLE, 0); 346395c3070SAdrian Hunter if (err) 347395c3070SAdrian Hunter return err; 348395c3070SAdrian Hunter } 349395c3070SAdrian Hunter } 350395c3070SAdrian Hunter return 0; 351395c3070SAdrian Hunter } 352395c3070SAdrian Hunter 353395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist, 354395c3070SAdrian Hunter struct perf_evsel *evsel) 355395c3070SAdrian Hunter { 356395c3070SAdrian Hunter int cpu, thread, err; 357bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 358bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 359395c3070SAdrian Hunter 360395c3070SAdrian Hunter if (!evsel->fd) 361395c3070SAdrian Hunter return -EINVAL; 362395c3070SAdrian Hunter 363bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 364bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 365395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 366395c3070SAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 367395c3070SAdrian Hunter if (err) 368395c3070SAdrian Hunter return err; 369395c3070SAdrian Hunter } 370395c3070SAdrian Hunter } 371395c3070SAdrian Hunter return 0; 372395c3070SAdrian Hunter } 373395c3070SAdrian Hunter 3741c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3751c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3761c65056cSAdrian Hunter { 3771c65056cSAdrian Hunter int thread, err; 3781c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3791c65056cSAdrian Hunter 3801c65056cSAdrian Hunter if (!evsel->fd) 3811c65056cSAdrian Hunter return -EINVAL; 3821c65056cSAdrian Hunter 3831c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 3841c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 3851c65056cSAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 3861c65056cSAdrian Hunter if (err) 3871c65056cSAdrian Hunter return err; 3881c65056cSAdrian Hunter } 3891c65056cSAdrian Hunter return 0; 3901c65056cSAdrian Hunter } 3911c65056cSAdrian Hunter 3921c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 3931c65056cSAdrian Hunter struct perf_evsel *evsel, 3941c65056cSAdrian Hunter int thread) 3951c65056cSAdrian Hunter { 3961c65056cSAdrian Hunter int cpu, err; 3971c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 3981c65056cSAdrian Hunter 3991c65056cSAdrian Hunter if (!evsel->fd) 4001c65056cSAdrian Hunter return -EINVAL; 4011c65056cSAdrian Hunter 4021c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 4031c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4041c65056cSAdrian Hunter if (err) 4051c65056cSAdrian Hunter return err; 4061c65056cSAdrian Hunter } 4071c65056cSAdrian Hunter return 0; 4081c65056cSAdrian Hunter } 4091c65056cSAdrian Hunter 4101c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4111c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4121c65056cSAdrian Hunter { 4131c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4141c65056cSAdrian Hunter 4151c65056cSAdrian Hunter if (per_cpu_mmaps) 4161c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4171c65056cSAdrian Hunter else 4181c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4191c65056cSAdrian Hunter } 4201c65056cSAdrian Hunter 421ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4225c581041SArnaldo Carvalho de Melo { 423b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 424b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 425bf8e8f4bSAdrian Hunter int nfds = 0; 426bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 427bf8e8f4bSAdrian Hunter 428cba9b847SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 429bf8e8f4bSAdrian Hunter if (evsel->system_wide) 430bf8e8f4bSAdrian Hunter nfds += nr_cpus; 431bf8e8f4bSAdrian Hunter else 432bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 433bf8e8f4bSAdrian Hunter } 434bf8e8f4bSAdrian Hunter 4351b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4361b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 437ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 438ad6765ddSArnaldo Carvalho de Melo 439ad6765ddSArnaldo Carvalho de Melo return 0; 4405c581041SArnaldo Carvalho de Melo } 44170082dd9SArnaldo Carvalho de Melo 442e4b356b5SArnaldo Carvalho de Melo static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx) 443e4b356b5SArnaldo Carvalho de Melo { 444e4b356b5SArnaldo Carvalho de Melo int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP); 445e4b356b5SArnaldo Carvalho de Melo /* 446e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 447e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 448e4b356b5SArnaldo Carvalho de Melo */ 449e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 450e4b356b5SArnaldo Carvalho de Melo evlist->pollfd.priv[pos].idx = idx; 451e4b356b5SArnaldo Carvalho de Melo 452e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 453e4b356b5SArnaldo Carvalho de Melo } 454e4b356b5SArnaldo Carvalho de Melo 455e4b356b5SArnaldo Carvalho de Melo return pos; 456e4b356b5SArnaldo Carvalho de Melo } 457e4b356b5SArnaldo Carvalho de Melo 458ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 45970082dd9SArnaldo Carvalho de Melo { 460e4b356b5SArnaldo Carvalho de Melo return __perf_evlist__add_pollfd(evlist, fd, -1); 461e4b356b5SArnaldo Carvalho de Melo } 4621b85337dSArnaldo Carvalho de Melo 463e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd) 464e4b356b5SArnaldo Carvalho de Melo { 465e4b356b5SArnaldo Carvalho de Melo struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd); 466e4b356b5SArnaldo Carvalho de Melo 467e4b356b5SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, fda->priv[fd].idx); 46870082dd9SArnaldo Carvalho de Melo } 46970db7533SArnaldo Carvalho de Melo 4701ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4711ddec7f0SArnaldo Carvalho de Melo { 472e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 473e4b356b5SArnaldo Carvalho de Melo perf_evlist__munmap_filtered); 4741ddec7f0SArnaldo Carvalho de Melo } 4751ddec7f0SArnaldo Carvalho de Melo 476f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 477f66a889dSArnaldo Carvalho de Melo { 4781b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 479f66a889dSArnaldo Carvalho de Melo } 480f66a889dSArnaldo Carvalho de Melo 481a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 482a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 4833d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4843d3b5e95SArnaldo Carvalho de Melo { 4853d3b5e95SArnaldo Carvalho de Melo int hash; 4863d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 4873d3b5e95SArnaldo Carvalho de Melo 4883d3b5e95SArnaldo Carvalho de Melo sid->id = id; 4893d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 4903d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 4913d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 4923d3b5e95SArnaldo Carvalho de Melo } 4933d3b5e95SArnaldo Carvalho de Melo 494a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 495a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 496a91e5431SArnaldo Carvalho de Melo { 497a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 498a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 499a91e5431SArnaldo Carvalho de Melo } 500a91e5431SArnaldo Carvalho de Melo 501a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 5023d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 503f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 504f8a95309SArnaldo Carvalho de Melo { 505f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5063d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 507e2b5abe0SJiri Olsa u64 id; 508e2b5abe0SJiri Olsa int ret; 509e2b5abe0SJiri Olsa 510e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 511e2b5abe0SJiri Olsa if (!ret) 512e2b5abe0SJiri Olsa goto add; 513e2b5abe0SJiri Olsa 514e2b5abe0SJiri Olsa if (errno != ENOTTY) 515e2b5abe0SJiri Olsa return -1; 516e2b5abe0SJiri Olsa 517e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 518f8a95309SArnaldo Carvalho de Melo 519c4861afeSJiri Olsa /* 520c4861afeSJiri Olsa * This way does not work with group format read, so bail 521c4861afeSJiri Olsa * out in that case. 522c4861afeSJiri Olsa */ 523c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 524c4861afeSJiri Olsa return -1; 525c4861afeSJiri Olsa 526f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 527f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 528f8a95309SArnaldo Carvalho de Melo return -1; 529f8a95309SArnaldo Carvalho de Melo 530f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 531f8a95309SArnaldo Carvalho de Melo ++id_idx; 532f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 533f8a95309SArnaldo Carvalho de Melo ++id_idx; 534f8a95309SArnaldo Carvalho de Melo 535e2b5abe0SJiri Olsa id = read_data[id_idx]; 536e2b5abe0SJiri Olsa 537e2b5abe0SJiri Olsa add: 538e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 539f8a95309SArnaldo Carvalho de Melo return 0; 540f8a95309SArnaldo Carvalho de Melo } 541f8a95309SArnaldo Carvalho de Melo 5423c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5433c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5443c659eedSAdrian Hunter int thread) 5453c659eedSAdrian Hunter { 5463c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5473c659eedSAdrian Hunter sid->idx = idx; 5483c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5493c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5503c659eedSAdrian Hunter else 5513c659eedSAdrian Hunter sid->cpu = -1; 5523c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 553e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5543c659eedSAdrian Hunter else 5553c659eedSAdrian Hunter sid->tid = -1; 5563c659eedSAdrian Hunter } 5573c659eedSAdrian Hunter 558932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 55970db7533SArnaldo Carvalho de Melo { 56070db7533SArnaldo Carvalho de Melo struct hlist_head *head; 56170db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 56270db7533SArnaldo Carvalho de Melo int hash; 56370db7533SArnaldo Carvalho de Melo 56470db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 56570db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 56670db7533SArnaldo Carvalho de Melo 567b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 56870db7533SArnaldo Carvalho de Melo if (sid->id == id) 569932a3594SJiri Olsa return sid; 570932a3594SJiri Olsa 571932a3594SJiri Olsa return NULL; 572932a3594SJiri Olsa } 573932a3594SJiri Olsa 574932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 575932a3594SJiri Olsa { 576932a3594SJiri Olsa struct perf_sample_id *sid; 577932a3594SJiri Olsa 57805169df5SAdrian Hunter if (evlist->nr_entries == 1 || !id) 579932a3594SJiri Olsa return perf_evlist__first(evlist); 580932a3594SJiri Olsa 581932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 582932a3594SJiri Olsa if (sid) 58370db7533SArnaldo Carvalho de Melo return sid->evsel; 58430e68bccSNamhyung Kim 58530e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 5860c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 58730e68bccSNamhyung Kim 58870db7533SArnaldo Carvalho de Melo return NULL; 58970db7533SArnaldo Carvalho de Melo } 59004391debSArnaldo Carvalho de Melo 59175562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 59275562573SAdrian Hunter union perf_event *event, u64 *id) 59375562573SAdrian Hunter { 59475562573SAdrian Hunter const u64 *array = event->sample.array; 59575562573SAdrian Hunter ssize_t n; 59675562573SAdrian Hunter 59775562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 59875562573SAdrian Hunter 59975562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 60075562573SAdrian Hunter if (evlist->id_pos >= n) 60175562573SAdrian Hunter return -1; 60275562573SAdrian Hunter *id = array[evlist->id_pos]; 60375562573SAdrian Hunter } else { 60475562573SAdrian Hunter if (evlist->is_pos > n) 60575562573SAdrian Hunter return -1; 60675562573SAdrian Hunter n -= evlist->is_pos; 60775562573SAdrian Hunter *id = array[n]; 60875562573SAdrian Hunter } 60975562573SAdrian Hunter return 0; 61075562573SAdrian Hunter } 61175562573SAdrian Hunter 61275562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 61375562573SAdrian Hunter union perf_event *event) 61475562573SAdrian Hunter { 61598be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 61675562573SAdrian Hunter struct hlist_head *head; 61775562573SAdrian Hunter struct perf_sample_id *sid; 61875562573SAdrian Hunter int hash; 61975562573SAdrian Hunter u64 id; 62075562573SAdrian Hunter 62175562573SAdrian Hunter if (evlist->nr_entries == 1) 62298be6966SAdrian Hunter return first; 62398be6966SAdrian Hunter 62498be6966SAdrian Hunter if (!first->attr.sample_id_all && 62598be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 62698be6966SAdrian Hunter return first; 62775562573SAdrian Hunter 62875562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 62975562573SAdrian Hunter return NULL; 63075562573SAdrian Hunter 63175562573SAdrian Hunter /* Synthesized events have an id of zero */ 63275562573SAdrian Hunter if (!id) 63398be6966SAdrian Hunter return first; 63475562573SAdrian Hunter 63575562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 63675562573SAdrian Hunter head = &evlist->heads[hash]; 63775562573SAdrian Hunter 63875562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 63975562573SAdrian Hunter if (sid->id == id) 64075562573SAdrian Hunter return sid->evsel; 64175562573SAdrian Hunter } 64275562573SAdrian Hunter return NULL; 64375562573SAdrian Hunter } 64475562573SAdrian Hunter 645aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 64604391debSArnaldo Carvalho de Melo { 647aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 6487951722dSSukadev Bhattiprolu u64 head; 6497b8283b5SDavid Ahern u64 old = md->prev; 65004391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 6518115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 65204391debSArnaldo Carvalho de Melo 6537951722dSSukadev Bhattiprolu /* 6547951722dSSukadev Bhattiprolu * Check if event was unmapped due to a POLLHUP/POLLERR. 6557951722dSSukadev Bhattiprolu */ 6567951722dSSukadev Bhattiprolu if (!atomic_read(&md->refcnt)) 6577951722dSSukadev Bhattiprolu return NULL; 6587951722dSSukadev Bhattiprolu 6597951722dSSukadev Bhattiprolu head = perf_mmap__read_head(md); 6607bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 66104391debSArnaldo Carvalho de Melo /* 66204391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 66304391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 66404391debSArnaldo Carvalho de Melo * 66504391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 66604391debSArnaldo Carvalho de Melo * 66704391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 66804391debSArnaldo Carvalho de Melo */ 6697bb41152SArnaldo Carvalho de Melo int diff = head - old; 67004391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 67104391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 67204391debSArnaldo Carvalho de Melo 67304391debSArnaldo Carvalho de Melo /* 67404391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 67504391debSArnaldo Carvalho de Melo */ 67604391debSArnaldo Carvalho de Melo old = head; 67704391debSArnaldo Carvalho de Melo } 6787bb41152SArnaldo Carvalho de Melo } 67904391debSArnaldo Carvalho de Melo 68004391debSArnaldo Carvalho de Melo if (old != head) { 68104391debSArnaldo Carvalho de Melo size_t size; 68204391debSArnaldo Carvalho de Melo 6838115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 68404391debSArnaldo Carvalho de Melo size = event->header.size; 68504391debSArnaldo Carvalho de Melo 68604391debSArnaldo Carvalho de Melo /* 68704391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 68804391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 68904391debSArnaldo Carvalho de Melo */ 69004391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 69104391debSArnaldo Carvalho de Melo unsigned int offset = old; 69204391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 693a65cb4b9SJiri Olsa void *dst = md->event_copy; 69404391debSArnaldo Carvalho de Melo 69504391debSArnaldo Carvalho de Melo do { 69604391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 69704391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 69804391debSArnaldo Carvalho de Melo offset += cpy; 69904391debSArnaldo Carvalho de Melo dst += cpy; 70004391debSArnaldo Carvalho de Melo len -= cpy; 70104391debSArnaldo Carvalho de Melo } while (len); 70204391debSArnaldo Carvalho de Melo 703a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 70404391debSArnaldo Carvalho de Melo } 70504391debSArnaldo Carvalho de Melo 70604391debSArnaldo Carvalho de Melo old += size; 70704391debSArnaldo Carvalho de Melo } 70804391debSArnaldo Carvalho de Melo 70904391debSArnaldo Carvalho de Melo md->prev = old; 7107bb41152SArnaldo Carvalho de Melo 71104391debSArnaldo Carvalho de Melo return event; 71204391debSArnaldo Carvalho de Melo } 713f8a95309SArnaldo Carvalho de Melo 71482396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 71582396986SArnaldo Carvalho de Melo { 716b72e74d1SAdrian Hunter return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base; 71782396986SArnaldo Carvalho de Melo } 71882396986SArnaldo Carvalho de Melo 71982396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx) 72082396986SArnaldo Carvalho de Melo { 7217143849aSArnaldo Carvalho de Melo atomic_inc(&evlist->mmap[idx].refcnt); 72282396986SArnaldo Carvalho de Melo } 72382396986SArnaldo Carvalho de Melo 72482396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx) 72582396986SArnaldo Carvalho de Melo { 7267143849aSArnaldo Carvalho de Melo BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0); 72782396986SArnaldo Carvalho de Melo 7287143849aSArnaldo Carvalho de Melo if (atomic_dec_and_test(&evlist->mmap[idx].refcnt)) 72982396986SArnaldo Carvalho de Melo __perf_evlist__munmap(evlist, idx); 73082396986SArnaldo Carvalho de Melo } 73182396986SArnaldo Carvalho de Melo 7328e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 7338e50d384SZhouyi Zhou { 7348e50d384SZhouyi Zhou struct perf_mmap *md = &evlist->mmap[idx]; 73582396986SArnaldo Carvalho de Melo 73682396986SArnaldo Carvalho de Melo if (!evlist->overwrite) { 7377b8283b5SDavid Ahern u64 old = md->prev; 7388e50d384SZhouyi Zhou 7398e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 7408e50d384SZhouyi Zhou } 74182396986SArnaldo Carvalho de Melo 7427143849aSArnaldo Carvalho de Melo if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md)) 74382396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 7448e50d384SZhouyi Zhou } 7458e50d384SZhouyi Zhou 746718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, 747718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 748718c602dSAdrian Hunter void *userpg __maybe_unused, 749718c602dSAdrian Hunter int fd __maybe_unused) 750718c602dSAdrian Hunter { 751718c602dSAdrian Hunter return 0; 752718c602dSAdrian Hunter } 753718c602dSAdrian Hunter 754718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) 755718c602dSAdrian Hunter { 756718c602dSAdrian Hunter } 757718c602dSAdrian Hunter 758718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init( 759718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 760718c602dSAdrian Hunter off_t auxtrace_offset __maybe_unused, 761718c602dSAdrian Hunter unsigned int auxtrace_pages __maybe_unused, 762718c602dSAdrian Hunter bool auxtrace_overwrite __maybe_unused) 763718c602dSAdrian Hunter { 764718c602dSAdrian Hunter } 765718c602dSAdrian Hunter 766718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx( 767718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 768718c602dSAdrian Hunter struct perf_evlist *evlist __maybe_unused, 769718c602dSAdrian Hunter int idx __maybe_unused, 770718c602dSAdrian Hunter bool per_cpu __maybe_unused) 771718c602dSAdrian Hunter { 772718c602dSAdrian Hunter } 773718c602dSAdrian Hunter 77493edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 77593edcbd9SAdrian Hunter { 77693edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 77793edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 77893edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 7797143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 0); 78093edcbd9SAdrian Hunter } 781718c602dSAdrian Hunter auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap); 78293edcbd9SAdrian Hunter } 78393edcbd9SAdrian Hunter 7847e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 785f8a95309SArnaldo Carvalho de Melo { 786aece948fSArnaldo Carvalho de Melo int i; 787f8a95309SArnaldo Carvalho de Melo 788983874d1SArnaldo Carvalho de Melo if (evlist->mmap == NULL) 789983874d1SArnaldo Carvalho de Melo return; 790983874d1SArnaldo Carvalho de Melo 79193edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 79293edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 793aece948fSArnaldo Carvalho de Melo 79404662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 795f8a95309SArnaldo Carvalho de Melo } 796f8a95309SArnaldo Carvalho de Melo 797806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 798f8a95309SArnaldo Carvalho de Melo { 799a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 800ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 801b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 802aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 803f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 804f8a95309SArnaldo Carvalho de Melo } 805f8a95309SArnaldo Carvalho de Melo 806a8a8f3ebSAdrian Hunter struct mmap_params { 807a8a8f3ebSAdrian Hunter int prot; 808a8a8f3ebSAdrian Hunter int mask; 809718c602dSAdrian Hunter struct auxtrace_mmap_params auxtrace_mp; 810a8a8f3ebSAdrian Hunter }; 811a8a8f3ebSAdrian Hunter 812a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, 813a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 814f8a95309SArnaldo Carvalho de Melo { 81582396986SArnaldo Carvalho de Melo /* 81682396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 81782396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 81882396986SArnaldo Carvalho de Melo * the ring buffer. 81982396986SArnaldo Carvalho de Melo * 82082396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 82182396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 82282396986SArnaldo Carvalho de Melo * waiting to be consumed. 82382396986SArnaldo Carvalho de Melo * 82482396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 82582396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 82682396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 82782396986SArnaldo Carvalho de Melo */ 8287143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 2); 829aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 830a8a8f3ebSAdrian Hunter evlist->mmap[idx].mask = mp->mask; 831a8a8f3ebSAdrian Hunter evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, 832f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 833301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 83402635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 83502635965SAdrian Hunter errno); 836301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 837f8a95309SArnaldo Carvalho de Melo return -1; 838301b195dSNelson Elhage } 839ad6765ddSArnaldo Carvalho de Melo 840718c602dSAdrian Hunter if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap, 841718c602dSAdrian Hunter &mp->auxtrace_mp, evlist->mmap[idx].base, fd)) 842718c602dSAdrian Hunter return -1; 843718c602dSAdrian Hunter 844f8a95309SArnaldo Carvalho de Melo return 0; 845f8a95309SArnaldo Carvalho de Melo } 846f8a95309SArnaldo Carvalho de Melo 84704e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 848a8a8f3ebSAdrian Hunter struct mmap_params *mp, int cpu, 849a8a8f3ebSAdrian Hunter int thread, int *output) 850aece948fSArnaldo Carvalho de Melo { 851aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 85204e21314SAdrian Hunter 8530050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 854bf8e8f4bSAdrian Hunter int fd; 855bf8e8f4bSAdrian Hunter 856bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 857bf8e8f4bSAdrian Hunter continue; 858bf8e8f4bSAdrian Hunter 859bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 86004e21314SAdrian Hunter 86104e21314SAdrian Hunter if (*output == -1) { 86204e21314SAdrian Hunter *output = fd; 863a8a8f3ebSAdrian Hunter if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) 86404e21314SAdrian Hunter return -1; 86504e21314SAdrian Hunter } else { 86604e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 86704e21314SAdrian Hunter return -1; 86882396986SArnaldo Carvalho de Melo 86982396986SArnaldo Carvalho de Melo perf_evlist__mmap_get(evlist, idx); 87004e21314SAdrian Hunter } 87104e21314SAdrian Hunter 872f90d194aSAdrian Hunter /* 873f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 874f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 875f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 876f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 877f90d194aSAdrian Hunter * Therefore don't add it for polling. 878f90d194aSAdrian Hunter */ 879f90d194aSAdrian Hunter if (!evsel->system_wide && 880f90d194aSAdrian Hunter __perf_evlist__add_pollfd(evlist, fd, idx) < 0) { 88182396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 882ad6765ddSArnaldo Carvalho de Melo return -1; 88382396986SArnaldo Carvalho de Melo } 884033fa713SArnaldo Carvalho de Melo 8853c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 8863c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 8873c659eedSAdrian Hunter fd) < 0) 88804e21314SAdrian Hunter return -1; 8893c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 8903c659eedSAdrian Hunter thread); 8913c659eedSAdrian Hunter } 89204e21314SAdrian Hunter } 89304e21314SAdrian Hunter 89404e21314SAdrian Hunter return 0; 89504e21314SAdrian Hunter } 89604e21314SAdrian Hunter 897a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 898a8a8f3ebSAdrian Hunter struct mmap_params *mp) 89904e21314SAdrian Hunter { 900aece948fSArnaldo Carvalho de Melo int cpu, thread; 901b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 902b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 903aece948fSArnaldo Carvalho de Melo 904e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 905b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 906aece948fSArnaldo Carvalho de Melo int output = -1; 907aece948fSArnaldo Carvalho de Melo 908718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 909718c602dSAdrian Hunter true); 910718c602dSAdrian Hunter 911b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 912a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 913a8a8f3ebSAdrian Hunter thread, &output)) 914aece948fSArnaldo Carvalho de Melo goto out_unmap; 915aece948fSArnaldo Carvalho de Melo } 916aece948fSArnaldo Carvalho de Melo } 917aece948fSArnaldo Carvalho de Melo 918aece948fSArnaldo Carvalho de Melo return 0; 919aece948fSArnaldo Carvalho de Melo 920aece948fSArnaldo Carvalho de Melo out_unmap: 92193edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 92293edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 923aece948fSArnaldo Carvalho de Melo return -1; 924aece948fSArnaldo Carvalho de Melo } 925aece948fSArnaldo Carvalho de Melo 926a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 927a8a8f3ebSAdrian Hunter struct mmap_params *mp) 928aece948fSArnaldo Carvalho de Melo { 929aece948fSArnaldo Carvalho de Melo int thread; 930b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 931aece948fSArnaldo Carvalho de Melo 932e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 933b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 934aece948fSArnaldo Carvalho de Melo int output = -1; 935aece948fSArnaldo Carvalho de Melo 936718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 937718c602dSAdrian Hunter false); 938718c602dSAdrian Hunter 939a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 940a8a8f3ebSAdrian Hunter &output)) 941aece948fSArnaldo Carvalho de Melo goto out_unmap; 942aece948fSArnaldo Carvalho de Melo } 943aece948fSArnaldo Carvalho de Melo 944aece948fSArnaldo Carvalho de Melo return 0; 945aece948fSArnaldo Carvalho de Melo 946aece948fSArnaldo Carvalho de Melo out_unmap: 94793edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 94893edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 949aece948fSArnaldo Carvalho de Melo return -1; 950aece948fSArnaldo Carvalho de Melo } 951aece948fSArnaldo Carvalho de Melo 952994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages) 953994a1f78SJiri Olsa { 9548185e881SArnaldo Carvalho de Melo if (pages == UINT_MAX) { 9558185e881SArnaldo Carvalho de Melo int max; 9568185e881SArnaldo Carvalho de Melo 9578185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 9588185e881SArnaldo Carvalho de Melo /* 9598185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 9608185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 9618185e881SArnaldo Carvalho de Melo * die yet... 9628185e881SArnaldo Carvalho de Melo */ 9638185e881SArnaldo Carvalho de Melo max = 512; 9648185e881SArnaldo Carvalho de Melo } else { 9658185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 9668185e881SArnaldo Carvalho de Melo } 9678185e881SArnaldo Carvalho de Melo 9688185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 9691be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 9701be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 9718185e881SArnaldo Carvalho de Melo } else if (!is_power_of_2(pages)) 972994a1f78SJiri Olsa return 0; 973994a1f78SJiri Olsa 974994a1f78SJiri Olsa return (pages + 1) * page_size; 975994a1f78SJiri Olsa } 976994a1f78SJiri Olsa 97733c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 97833c2dcfdSDavid Ahern unsigned long max) 979994a1f78SJiri Olsa { 9802fbe4abeSAdrian Hunter unsigned long pages, val; 98127050f53SJiri Olsa static struct parse_tag tags[] = { 98227050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 98327050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 98427050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 98527050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 98627050f53SJiri Olsa { .tag = 0 }, 98727050f53SJiri Olsa }; 988994a1f78SJiri Olsa 9898973504bSDavid Ahern if (str == NULL) 99033c2dcfdSDavid Ahern return -EINVAL; 9918973504bSDavid Ahern 99227050f53SJiri Olsa val = parse_tag_value(str, tags); 9932fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 99427050f53SJiri Olsa /* we got file size value */ 99527050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 99627050f53SJiri Olsa } else { 99727050f53SJiri Olsa /* we got pages count value */ 99827050f53SJiri Olsa char *eptr; 999994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 100033c2dcfdSDavid Ahern if (*eptr != '\0') 100133c2dcfdSDavid Ahern return -EINVAL; 100227050f53SJiri Olsa } 1003994a1f78SJiri Olsa 10042bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 100533c2dcfdSDavid Ahern /* leave number of pages at 0 */ 10061dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 100733c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 100891529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 10091dbfa938SAdrian Hunter if (!pages) 10101dbfa938SAdrian Hunter return -EINVAL; 10119639837eSDavid Ahern pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 10129639837eSDavid Ahern pages * page_size, pages); 10132fbe4abeSAdrian Hunter } 10142fbe4abeSAdrian Hunter 101533c2dcfdSDavid Ahern if (pages > max) 101633c2dcfdSDavid Ahern return -EINVAL; 101733c2dcfdSDavid Ahern 101833c2dcfdSDavid Ahern return pages; 101933c2dcfdSDavid Ahern } 102033c2dcfdSDavid Ahern 1021e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 102233c2dcfdSDavid Ahern { 102333c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 102433c2dcfdSDavid Ahern long pages; 102533c2dcfdSDavid Ahern 1026f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 102733c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 102833c2dcfdSDavid Ahern 102933c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 103033c2dcfdSDavid Ahern if (pages < 0) { 103133c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 1032994a1f78SJiri Olsa return -1; 1033994a1f78SJiri Olsa } 1034994a1f78SJiri Olsa 1035994a1f78SJiri Olsa *mmap_pages = pages; 1036994a1f78SJiri Olsa return 0; 1037994a1f78SJiri Olsa } 1038994a1f78SJiri Olsa 1039e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 1040e9db1310SAdrian Hunter int unset __maybe_unused) 1041e9db1310SAdrian Hunter { 1042e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 1043e9db1310SAdrian Hunter } 1044e9db1310SAdrian Hunter 1045c83fa7f2SAdrian Hunter /** 1046718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 1047c83fa7f2SAdrian Hunter * @evlist: list of events 1048c83fa7f2SAdrian Hunter * @pages: map length in pages 1049c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 1050718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 1051718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1052f8a95309SArnaldo Carvalho de Melo * 1053c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1054c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1055c83fa7f2SAdrian Hunter * automatically. 1056f8a95309SArnaldo Carvalho de Melo * 1057718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1058718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1059718c602dSAdrian Hunter * 1060c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1061f8a95309SArnaldo Carvalho de Melo */ 1062718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, 1063718c602dSAdrian Hunter bool overwrite, unsigned int auxtrace_pages, 1064718c602dSAdrian Hunter bool auxtrace_overwrite) 1065f8a95309SArnaldo Carvalho de Melo { 1066aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 10677e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 10687e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1069a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1070a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1071a8a8f3ebSAdrian Hunter }; 107250a682ceSArnaldo Carvalho de Melo 10737e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 1074f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1075f8a95309SArnaldo Carvalho de Melo 10761b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1077f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1078f8a95309SArnaldo Carvalho de Melo 1079f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1080994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 10812af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1082a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1083f8a95309SArnaldo Carvalho de Melo 1084718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1085718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1086718c602dSAdrian Hunter 10870050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1088f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1089a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1090a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1091f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1092f8a95309SArnaldo Carvalho de Melo } 1093f8a95309SArnaldo Carvalho de Melo 1094ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1095a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1096f8a95309SArnaldo Carvalho de Melo 1097a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1098f8a95309SArnaldo Carvalho de Melo } 10997e2ed097SArnaldo Carvalho de Melo 1100718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 1101718c602dSAdrian Hunter bool overwrite) 1102718c602dSAdrian Hunter { 1103718c602dSAdrian Hunter return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false); 1104718c602dSAdrian Hunter } 1105718c602dSAdrian Hunter 1106b7f0c203SJiri Olsa static int perf_evlist__propagate_maps(struct perf_evlist *evlist, 110752361ff0SJiri Olsa bool has_user_cpus) 1108b7f0c203SJiri Olsa { 1109b7f0c203SJiri Olsa struct perf_evsel *evsel; 1110b7f0c203SJiri Olsa 1111b7f0c203SJiri Olsa evlist__for_each(evlist, evsel) { 1112b7f0c203SJiri Olsa /* 1113b7f0c203SJiri Olsa * We already have cpus for evsel (via PMU sysfs) so 1114b7f0c203SJiri Olsa * keep it, if there's no target cpu list defined. 1115b7f0c203SJiri Olsa */ 111652361ff0SJiri Olsa if (evsel->cpus && has_user_cpus) 1117b7f0c203SJiri Olsa cpu_map__put(evsel->cpus); 1118b7f0c203SJiri Olsa 111952361ff0SJiri Olsa if (!evsel->cpus || has_user_cpus) 1120b7f0c203SJiri Olsa evsel->cpus = cpu_map__get(evlist->cpus); 1121b7f0c203SJiri Olsa 1122578e91ecSJiri Olsa evsel->threads = thread_map__get(evlist->threads); 1123578e91ecSJiri Olsa 11240e5ffb31SJiri Olsa if ((evlist->cpus && !evsel->cpus) || 11250e5ffb31SJiri Olsa (evlist->threads && !evsel->threads)) 1126b7f0c203SJiri Olsa return -ENOMEM; 1127b7f0c203SJiri Olsa } 1128b7f0c203SJiri Olsa 1129b7f0c203SJiri Olsa return 0; 1130b7f0c203SJiri Olsa } 1131b7f0c203SJiri Olsa 1132602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 11337e2ed097SArnaldo Carvalho de Melo { 1134b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 1135b809ac10SNamhyung Kim target->uid); 11367e2ed097SArnaldo Carvalho de Melo 11377e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 11387e2ed097SArnaldo Carvalho de Melo return -1; 11397e2ed097SArnaldo Carvalho de Melo 11409c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 1141d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 1142879d77d0SNamhyung Kim else 1143879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 11447e2ed097SArnaldo Carvalho de Melo 11457e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 11467e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 11477e2ed097SArnaldo Carvalho de Melo 114852361ff0SJiri Olsa return perf_evlist__propagate_maps(evlist, !!target->cpu_list); 11497e2ed097SArnaldo Carvalho de Melo 11507e2ed097SArnaldo Carvalho de Melo out_delete_threads: 1151186fbb74SJiri Olsa thread_map__put(evlist->threads); 1152b2e19a93SYasser Shalabi evlist->threads = NULL; 11537e2ed097SArnaldo Carvalho de Melo return -1; 11547e2ed097SArnaldo Carvalho de Melo } 11557e2ed097SArnaldo Carvalho de Melo 11563de5cfb0SJiri Olsa int perf_evlist__set_maps(struct perf_evlist *evlist, 11573de5cfb0SJiri Olsa struct cpu_map *cpus, 11583de5cfb0SJiri Olsa struct thread_map *threads) 11593de5cfb0SJiri Olsa { 11603de5cfb0SJiri Olsa if (evlist->cpus) 11613de5cfb0SJiri Olsa cpu_map__put(evlist->cpus); 11623de5cfb0SJiri Olsa 11633de5cfb0SJiri Olsa evlist->cpus = cpus; 11643de5cfb0SJiri Olsa 11653de5cfb0SJiri Olsa if (evlist->threads) 11663de5cfb0SJiri Olsa thread_map__put(evlist->threads); 11673de5cfb0SJiri Olsa 11683de5cfb0SJiri Olsa evlist->threads = threads; 11693de5cfb0SJiri Olsa 11703de5cfb0SJiri Olsa return perf_evlist__propagate_maps(evlist, false); 11713de5cfb0SJiri Olsa } 11723de5cfb0SJiri Olsa 117323d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 11740a102479SFrederic Weisbecker { 11750a102479SFrederic Weisbecker struct perf_evsel *evsel; 1176745cefc5SArnaldo Carvalho de Melo int err = 0; 1177745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1178b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 11790a102479SFrederic Weisbecker 11800050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1181745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 11820a102479SFrederic Weisbecker continue; 1183745cefc5SArnaldo Carvalho de Melo 1184d988d5eeSKan Liang /* 1185d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1186d988d5eeSKan Liang * So evlist and evsel should always be same. 1187d988d5eeSKan Liang */ 1188f47805a2SArnaldo Carvalho de Melo err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter); 118923d4aad4SArnaldo Carvalho de Melo if (err) { 119023d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1191745cefc5SArnaldo Carvalho de Melo break; 11920a102479SFrederic Weisbecker } 119323d4aad4SArnaldo Carvalho de Melo } 11940a102479SFrederic Weisbecker 1195745cefc5SArnaldo Carvalho de Melo return err; 1196745cefc5SArnaldo Carvalho de Melo } 1197745cefc5SArnaldo Carvalho de Melo 1198745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1199745cefc5SArnaldo Carvalho de Melo { 1200745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1201745cefc5SArnaldo Carvalho de Melo int err = 0; 1202745cefc5SArnaldo Carvalho de Melo 12030050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 120494ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1205745cefc5SArnaldo Carvalho de Melo if (err) 1206745cefc5SArnaldo Carvalho de Melo break; 1207745cefc5SArnaldo Carvalho de Melo } 1208745cefc5SArnaldo Carvalho de Melo 1209745cefc5SArnaldo Carvalho de Melo return err; 12100a102479SFrederic Weisbecker } 121174429964SFrederic Weisbecker 1212be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1213cfd70a26SArnaldo Carvalho de Melo { 1214cfd70a26SArnaldo Carvalho de Melo char *filter; 1215be199adaSArnaldo Carvalho de Melo int ret = -1; 1216be199adaSArnaldo Carvalho de Melo size_t i; 1217cfd70a26SArnaldo Carvalho de Melo 1218be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1219be199adaSArnaldo Carvalho de Melo if (i == 0) { 1220be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1221cfd70a26SArnaldo Carvalho de Melo return -1; 1222be199adaSArnaldo Carvalho de Melo } else { 1223be199adaSArnaldo Carvalho de Melo char *tmp; 1224be199adaSArnaldo Carvalho de Melo 1225be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1226be199adaSArnaldo Carvalho de Melo goto out_free; 1227be199adaSArnaldo Carvalho de Melo 1228be199adaSArnaldo Carvalho de Melo free(filter); 1229be199adaSArnaldo Carvalho de Melo filter = tmp; 1230be199adaSArnaldo Carvalho de Melo } 1231be199adaSArnaldo Carvalho de Melo } 1232cfd70a26SArnaldo Carvalho de Melo 1233cfd70a26SArnaldo Carvalho de Melo ret = perf_evlist__set_filter(evlist, filter); 1234be199adaSArnaldo Carvalho de Melo out_free: 1235cfd70a26SArnaldo Carvalho de Melo free(filter); 1236cfd70a26SArnaldo Carvalho de Melo return ret; 1237cfd70a26SArnaldo Carvalho de Melo } 1238cfd70a26SArnaldo Carvalho de Melo 1239be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) 1240be199adaSArnaldo Carvalho de Melo { 1241be199adaSArnaldo Carvalho de Melo return perf_evlist__set_filter_pids(evlist, 1, &pid); 1242be199adaSArnaldo Carvalho de Melo } 1243be199adaSArnaldo Carvalho de Melo 12440c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 124574429964SFrederic Weisbecker { 124675562573SAdrian Hunter struct perf_evsel *pos; 1247c2a70653SArnaldo Carvalho de Melo 124875562573SAdrian Hunter if (evlist->nr_entries == 1) 124975562573SAdrian Hunter return true; 125075562573SAdrian Hunter 125175562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 125275562573SAdrian Hunter return false; 125375562573SAdrian Hunter 12540050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 125575562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 125675562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1257c2a70653SArnaldo Carvalho de Melo return false; 125874429964SFrederic Weisbecker } 125974429964SFrederic Weisbecker 1260c2a70653SArnaldo Carvalho de Melo return true; 1261c2a70653SArnaldo Carvalho de Melo } 1262c2a70653SArnaldo Carvalho de Melo 126375562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1264c2a70653SArnaldo Carvalho de Melo { 126575562573SAdrian Hunter struct perf_evsel *evsel; 126675562573SAdrian Hunter 126775562573SAdrian Hunter if (evlist->combined_sample_type) 126875562573SAdrian Hunter return evlist->combined_sample_type; 126975562573SAdrian Hunter 12700050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 127175562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 127275562573SAdrian Hunter 127375562573SAdrian Hunter return evlist->combined_sample_type; 127475562573SAdrian Hunter } 127575562573SAdrian Hunter 127675562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 127775562573SAdrian Hunter { 127875562573SAdrian Hunter evlist->combined_sample_type = 0; 127975562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1280c2a70653SArnaldo Carvalho de Melo } 1281c2a70653SArnaldo Carvalho de Melo 128298df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) 128398df858eSAndi Kleen { 128498df858eSAndi Kleen struct perf_evsel *evsel; 128598df858eSAndi Kleen u64 branch_type = 0; 128698df858eSAndi Kleen 128798df858eSAndi Kleen evlist__for_each(evlist, evsel) 128898df858eSAndi Kleen branch_type |= evsel->attr.branch_sample_type; 128998df858eSAndi Kleen return branch_type; 129098df858eSAndi Kleen } 129198df858eSAndi Kleen 12929ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 12939ede473cSJiri Olsa { 12949ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 12959ede473cSJiri Olsa u64 read_format = first->attr.read_format; 12969ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 12979ede473cSJiri Olsa 12980050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 12999ede473cSJiri Olsa if (read_format != pos->attr.read_format) 13009ede473cSJiri Olsa return false; 13019ede473cSJiri Olsa } 13029ede473cSJiri Olsa 13039ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 13049ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 13059ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 13069ede473cSJiri Olsa return false; 13079ede473cSJiri Olsa } 13089ede473cSJiri Olsa 13099ede473cSJiri Olsa return true; 13109ede473cSJiri Olsa } 13119ede473cSJiri Olsa 13129ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 13139ede473cSJiri Olsa { 13149ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 13159ede473cSJiri Olsa return first->attr.read_format; 13169ede473cSJiri Olsa } 13179ede473cSJiri Olsa 13180c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 131981e36bffSArnaldo Carvalho de Melo { 13200c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 132181e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 132281e36bffSArnaldo Carvalho de Melo u64 sample_type; 132381e36bffSArnaldo Carvalho de Melo u16 size = 0; 132481e36bffSArnaldo Carvalho de Melo 132581e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 132681e36bffSArnaldo Carvalho de Melo goto out; 132781e36bffSArnaldo Carvalho de Melo 132881e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 132981e36bffSArnaldo Carvalho de Melo 133081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 133181e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 133281e36bffSArnaldo Carvalho de Melo 133381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 133481e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 133581e36bffSArnaldo Carvalho de Melo 133681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 133781e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 133881e36bffSArnaldo Carvalho de Melo 133981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 134081e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 134181e36bffSArnaldo Carvalho de Melo 134281e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 134381e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 134475562573SAdrian Hunter 134575562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 134675562573SAdrian Hunter size += sizeof(data->id); 134781e36bffSArnaldo Carvalho de Melo out: 134881e36bffSArnaldo Carvalho de Melo return size; 134981e36bffSArnaldo Carvalho de Melo } 135081e36bffSArnaldo Carvalho de Melo 13510c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1352c2a70653SArnaldo Carvalho de Melo { 13530c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1354c2a70653SArnaldo Carvalho de Melo 13550050f7aaSArnaldo Carvalho de Melo evlist__for_each_continue(evlist, pos) { 1356c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1357c2a70653SArnaldo Carvalho de Melo return false; 1358c2a70653SArnaldo Carvalho de Melo } 1359c2a70653SArnaldo Carvalho de Melo 1360c2a70653SArnaldo Carvalho de Melo return true; 136174429964SFrederic Weisbecker } 136274429964SFrederic Weisbecker 13630c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 136474429964SFrederic Weisbecker { 13650c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1366c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 136774429964SFrederic Weisbecker } 136881cce8deSArnaldo Carvalho de Melo 136981cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 137081cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 137181cce8deSArnaldo Carvalho de Melo { 137281cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 137381cce8deSArnaldo Carvalho de Melo } 1374727ab04eSArnaldo Carvalho de Melo 1375a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1376a74b4b66SNamhyung Kim { 1377a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1378a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1379a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 13808ad9219eSStephane Eranian int n; 1381a74b4b66SNamhyung Kim 13828ad9219eSStephane Eranian evlist__for_each_reverse(evlist, evsel) { 13838ad9219eSStephane Eranian n = evsel->cpus ? evsel->cpus->nr : ncpus; 13848ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 13858ad9219eSStephane Eranian } 1386a74b4b66SNamhyung Kim } 1387a74b4b66SNamhyung Kim 13884112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 13894112eb18SArnaldo Carvalho de Melo { 13904112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 13914112eb18SArnaldo Carvalho de Melo 13924112eb18SArnaldo Carvalho de Melo /* 13934112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 13944112eb18SArnaldo Carvalho de Melo * an all cpus map. 13954112eb18SArnaldo Carvalho de Melo * 13964112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 13974112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 13984112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 13994112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 14004112eb18SArnaldo Carvalho de Melo */ 14014112eb18SArnaldo Carvalho de Melo evlist->cpus = cpu_map__new(NULL); 14024112eb18SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 14034112eb18SArnaldo Carvalho de Melo goto out; 14044112eb18SArnaldo Carvalho de Melo 14054112eb18SArnaldo Carvalho de Melo evlist->threads = thread_map__new_dummy(); 14064112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL) 14074112eb18SArnaldo Carvalho de Melo goto out_free_cpus; 14084112eb18SArnaldo Carvalho de Melo 14094112eb18SArnaldo Carvalho de Melo err = 0; 14104112eb18SArnaldo Carvalho de Melo out: 14114112eb18SArnaldo Carvalho de Melo return err; 14124112eb18SArnaldo Carvalho de Melo out_free_cpus: 1413f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 14144112eb18SArnaldo Carvalho de Melo evlist->cpus = NULL; 14154112eb18SArnaldo Carvalho de Melo goto out; 14164112eb18SArnaldo Carvalho de Melo } 14174112eb18SArnaldo Carvalho de Melo 14186a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1419727ab04eSArnaldo Carvalho de Melo { 14206a4bb04cSJiri Olsa struct perf_evsel *evsel; 1421a74b4b66SNamhyung Kim int err; 1422727ab04eSArnaldo Carvalho de Melo 14234112eb18SArnaldo Carvalho de Melo /* 14244112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 14254112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 14264112eb18SArnaldo Carvalho de Melo */ 14274112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 14284112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 14294112eb18SArnaldo Carvalho de Melo if (err < 0) 14304112eb18SArnaldo Carvalho de Melo goto out_err; 14314112eb18SArnaldo Carvalho de Melo } 14324112eb18SArnaldo Carvalho de Melo 1433733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1434733cd2feSAdrian Hunter 14350050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 14366a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 1437727ab04eSArnaldo Carvalho de Melo if (err < 0) 1438727ab04eSArnaldo Carvalho de Melo goto out_err; 1439727ab04eSArnaldo Carvalho de Melo } 1440727ab04eSArnaldo Carvalho de Melo 1441727ab04eSArnaldo Carvalho de Melo return 0; 1442727ab04eSArnaldo Carvalho de Melo out_err: 1443a74b4b66SNamhyung Kim perf_evlist__close(evlist); 144441c21a68SNamhyung Kim errno = -err; 1445727ab04eSArnaldo Carvalho de Melo return err; 1446727ab04eSArnaldo Carvalho de Melo } 144735b9d88eSArnaldo Carvalho de Melo 1448602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 144955e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1450735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 145135b9d88eSArnaldo Carvalho de Melo { 145235b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 145335b9d88eSArnaldo Carvalho de Melo char bf; 145435b9d88eSArnaldo Carvalho de Melo 145535b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 145635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 145735b9d88eSArnaldo Carvalho de Melo return -1; 145835b9d88eSArnaldo Carvalho de Melo } 145935b9d88eSArnaldo Carvalho de Melo 146035b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 146135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 146235b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 146335b9d88eSArnaldo Carvalho de Melo } 146435b9d88eSArnaldo Carvalho de Melo 146535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 146635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 146735b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 146835b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 146935b9d88eSArnaldo Carvalho de Melo } 147035b9d88eSArnaldo Carvalho de Melo 147135b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 14725f1c4225SArnaldo Carvalho de Melo int ret; 14735f1c4225SArnaldo Carvalho de Melo 1474119fa3c9SNamhyung Kim if (pipe_output) 147535b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 147635b9d88eSArnaldo Carvalho de Melo 14770817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 14780817df08SDavid Ahern 147935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 148035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 148135b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 148235b9d88eSArnaldo Carvalho de Melo 148335b9d88eSArnaldo Carvalho de Melo /* 148435b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 148535b9d88eSArnaldo Carvalho de Melo */ 148635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 148735b9d88eSArnaldo Carvalho de Melo 148835b9d88eSArnaldo Carvalho de Melo /* 148935b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 149035b9d88eSArnaldo Carvalho de Melo */ 14915f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 14925f1c4225SArnaldo Carvalho de Melo /* 14935f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 14945f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 14955f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 14965f1c4225SArnaldo Carvalho de Melo * 149720f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 14985f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 14995f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 15005f1c4225SArnaldo Carvalho de Melo * here. 15015f1c4225SArnaldo Carvalho de Melo */ 15025f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 15035f1c4225SArnaldo Carvalho de Melo if (ret == -1) 150435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 15055f1c4225SArnaldo Carvalho de Melo exit(ret); 15065f1c4225SArnaldo Carvalho de Melo } 150735b9d88eSArnaldo Carvalho de Melo 150835b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 150935b9d88eSArnaldo Carvalho de Melo 1510735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1511f33cbe72SArnaldo Carvalho de Melo union sigval val; 1512f33cbe72SArnaldo Carvalho de Melo 1513f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1514f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 151535b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1516f33cbe72SArnaldo Carvalho de Melo } else 1517f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 151835b9d88eSArnaldo Carvalho de Melo exit(-1); 151935b9d88eSArnaldo Carvalho de Melo } 152035b9d88eSArnaldo Carvalho de Melo 1521735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1522735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1523735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1524735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1525735f7e0bSArnaldo Carvalho de Melo }; 1526735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1527735f7e0bSArnaldo Carvalho de Melo } 1528735f7e0bSArnaldo Carvalho de Melo 15291aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 15301aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 15311aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 15321aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 15331aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 15341aaf63b1SArnaldo Carvalho de Melo } 1535e13798c7SJiri Olsa thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 15361aaf63b1SArnaldo Carvalho de Melo } 153735b9d88eSArnaldo Carvalho de Melo 153835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 153935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 154035b9d88eSArnaldo Carvalho de Melo /* 154135b9d88eSArnaldo Carvalho de Melo * wait for child to settle 154235b9d88eSArnaldo Carvalho de Melo */ 154335b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 154435b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 154535b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 154635b9d88eSArnaldo Carvalho de Melo } 154735b9d88eSArnaldo Carvalho de Melo 1548bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 154935b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 155035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 155135b9d88eSArnaldo Carvalho de Melo return 0; 155235b9d88eSArnaldo Carvalho de Melo 155335b9d88eSArnaldo Carvalho de Melo out_close_pipes: 155435b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 155535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 155635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 155735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 155835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 155935b9d88eSArnaldo Carvalho de Melo return -1; 156035b9d88eSArnaldo Carvalho de Melo } 156135b9d88eSArnaldo Carvalho de Melo 156235b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 156335b9d88eSArnaldo Carvalho de Melo { 156435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1565b3824404SDavid Ahern char bf = 0; 1566bcf3145fSNamhyung Kim int ret; 156735b9d88eSArnaldo Carvalho de Melo /* 156835b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 156935b9d88eSArnaldo Carvalho de Melo */ 1570bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1571bcf3145fSNamhyung Kim if (ret < 0) 1572bcf3145fSNamhyung Kim perror("enable to write to pipe"); 1573bcf3145fSNamhyung Kim 1574bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1575bcf3145fSNamhyung Kim return ret; 157635b9d88eSArnaldo Carvalho de Melo } 157735b9d88eSArnaldo Carvalho de Melo 157835b9d88eSArnaldo Carvalho de Melo return 0; 157935b9d88eSArnaldo Carvalho de Melo } 1580cb0b29e0SArnaldo Carvalho de Melo 1581a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 15820807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1583cb0b29e0SArnaldo Carvalho de Melo { 158475562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 158575562573SAdrian Hunter 158675562573SAdrian Hunter if (!evsel) 158775562573SAdrian Hunter return -EFAULT; 15880807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1589cb0b29e0SArnaldo Carvalho de Melo } 159078f067b3SArnaldo Carvalho de Melo 159178f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 159278f067b3SArnaldo Carvalho de Melo { 159378f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 159478f067b3SArnaldo Carvalho de Melo size_t printed = 0; 159578f067b3SArnaldo Carvalho de Melo 15960050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 159778f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 159878f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 159978f067b3SArnaldo Carvalho de Melo } 160078f067b3SArnaldo Carvalho de Melo 1601b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 160278f067b3SArnaldo Carvalho de Melo } 16036ef068cbSArnaldo Carvalho de Melo 1604a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, 1605a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1606a8f23d8fSArnaldo Carvalho de Melo { 1607a8f23d8fSArnaldo Carvalho de Melo int printed, value; 16086e81c74cSMasami Hiramatsu char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1609a8f23d8fSArnaldo Carvalho de Melo 1610a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1611a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1612a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1613a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1614a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1615a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1616a8f23d8fSArnaldo Carvalho de Melo 16171a47245dSAdrian Hunter value = perf_event_paranoid(); 1618a8f23d8fSArnaldo Carvalho de Melo 1619a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1620a8f23d8fSArnaldo Carvalho de Melo 1621a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1622a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1623a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1624a8f23d8fSArnaldo Carvalho de Melo } 1625a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16265229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1627a8f23d8fSArnaldo Carvalho de Melo 1628a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16295229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 16305229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1631a8f23d8fSArnaldo Carvalho de Melo break; 1632a8f23d8fSArnaldo Carvalho de Melo default: 1633a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1634a8f23d8fSArnaldo Carvalho de Melo break; 1635a8f23d8fSArnaldo Carvalho de Melo } 1636a8f23d8fSArnaldo Carvalho de Melo 1637a8f23d8fSArnaldo Carvalho de Melo return 0; 1638a8f23d8fSArnaldo Carvalho de Melo } 1639a025e4f0SAdrian Hunter 1640956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1641956fa571SArnaldo Carvalho de Melo { 1642956fa571SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1643e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1644956fa571SArnaldo Carvalho de Melo 1645956fa571SArnaldo Carvalho de Melo switch (err) { 1646956fa571SArnaldo Carvalho de Melo case EPERM: 1647e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1648e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1649e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1650956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1651e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1652e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1653e965bea1SArnaldo Carvalho de Melo 1654e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1655e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1656e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1657e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1658e965bea1SArnaldo Carvalho de Melo } 1659e965bea1SArnaldo Carvalho de Melo 1660e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1661e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1662956fa571SArnaldo Carvalho de Melo break; 1663956fa571SArnaldo Carvalho de Melo default: 1664956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1665956fa571SArnaldo Carvalho de Melo break; 1666956fa571SArnaldo Carvalho de Melo } 1667956fa571SArnaldo Carvalho de Melo 1668956fa571SArnaldo Carvalho de Melo return 0; 1669956fa571SArnaldo Carvalho de Melo } 1670956fa571SArnaldo Carvalho de Melo 1671a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1672a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1673a025e4f0SAdrian Hunter { 1674a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1675a025e4f0SAdrian Hunter LIST_HEAD(move); 1676a025e4f0SAdrian Hunter 1677a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1678a025e4f0SAdrian Hunter return; 1679a025e4f0SAdrian Hunter 16800050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, evsel) { 1681a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1682a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1683a025e4f0SAdrian Hunter } 1684a025e4f0SAdrian Hunter 1685a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1686a025e4f0SAdrian Hunter } 168760b0896cSAdrian Hunter 168860b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 168960b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 169060b0896cSAdrian Hunter { 169160b0896cSAdrian Hunter struct perf_evsel *evsel; 169260b0896cSAdrian Hunter 169360b0896cSAdrian Hunter if (tracking_evsel->tracking) 169460b0896cSAdrian Hunter return; 169560b0896cSAdrian Hunter 169660b0896cSAdrian Hunter evlist__for_each(evlist, evsel) { 169760b0896cSAdrian Hunter if (evsel != tracking_evsel) 169860b0896cSAdrian Hunter evsel->tracking = false; 169960b0896cSAdrian Hunter } 170060b0896cSAdrian Hunter 170160b0896cSAdrian Hunter tracking_evsel->tracking = true; 170260b0896cSAdrian Hunter } 1703