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> 288dd2a131SJiri Olsa #include <linux/err.h> 2970db7533SArnaldo Carvalho de Melo 30e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx); 31e4b356b5SArnaldo Carvalho de Melo static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx); 32e4b356b5SArnaldo Carvalho de Melo 33f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 34a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 35f8a95309SArnaldo Carvalho de Melo 367e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 377e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 38361c99a6SArnaldo Carvalho de Melo { 3970db7533SArnaldo Carvalho de Melo int i; 4070db7533SArnaldo Carvalho de Melo 4170db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 4270db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 43361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 447e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 451b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 4635b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 47361c99a6SArnaldo Carvalho de Melo } 48361c99a6SArnaldo Carvalho de Melo 49334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 50ef1d1af2SArnaldo Carvalho de Melo { 51ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 52ef1d1af2SArnaldo Carvalho de Melo 53ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 54334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 55ef1d1af2SArnaldo Carvalho de Melo 56361c99a6SArnaldo Carvalho de Melo return evlist; 57361c99a6SArnaldo Carvalho de Melo } 58361c99a6SArnaldo Carvalho de Melo 59b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 60b22d54b0SJiri Olsa { 61b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 62b22d54b0SJiri Olsa 63b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 64b22d54b0SJiri Olsa perf_evlist__delete(evlist); 65b22d54b0SJiri Olsa evlist = NULL; 66b22d54b0SJiri Olsa } 67b22d54b0SJiri Olsa 68b22d54b0SJiri Olsa return evlist; 69b22d54b0SJiri Olsa } 70b22d54b0SJiri Olsa 7175562573SAdrian Hunter /** 7275562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 7375562573SAdrian Hunter * @evlist: selected event list 7475562573SAdrian Hunter * 7575562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 7675562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 7775562573SAdrian Hunter */ 7875562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 7975562573SAdrian Hunter { 8075562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 8175562573SAdrian Hunter 8275562573SAdrian Hunter evlist->id_pos = first->id_pos; 8375562573SAdrian Hunter evlist->is_pos = first->is_pos; 8475562573SAdrian Hunter } 8575562573SAdrian Hunter 86733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 87733cd2feSAdrian Hunter { 88733cd2feSAdrian Hunter struct perf_evsel *evsel; 89733cd2feSAdrian Hunter 900050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 91733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 92733cd2feSAdrian Hunter 93733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 94733cd2feSAdrian Hunter } 95733cd2feSAdrian Hunter 96361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 97361c99a6SArnaldo Carvalho de Melo { 98361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 99361c99a6SArnaldo Carvalho de Melo 1000050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, pos) { 101361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 102d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 103361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 104361c99a6SArnaldo Carvalho de Melo } 105361c99a6SArnaldo Carvalho de Melo 106361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 107361c99a6SArnaldo Carvalho de Melo } 108361c99a6SArnaldo Carvalho de Melo 109ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 110ef1d1af2SArnaldo Carvalho de Melo { 11104662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1121b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 113ef1d1af2SArnaldo Carvalho de Melo } 114ef1d1af2SArnaldo Carvalho de Melo 115361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 116361c99a6SArnaldo Carvalho de Melo { 117983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 118f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 119f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 120186fbb74SJiri Olsa thread_map__put(evlist->threads); 12103ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 12203ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 123361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 124ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 125361c99a6SArnaldo Carvalho de Melo free(evlist); 126361c99a6SArnaldo Carvalho de Melo } 127361c99a6SArnaldo Carvalho de Melo 128361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 129361c99a6SArnaldo Carvalho de Melo { 130d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 131361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 132ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 13360b0896cSAdrian Hunter entry->tracking = !entry->idx; 134ef503831SArnaldo Carvalho de Melo 13575562573SAdrian Hunter if (!evlist->nr_entries++) 13675562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 137361c99a6SArnaldo Carvalho de Melo } 138361c99a6SArnaldo Carvalho de Melo 1390529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 14050d08e47SArnaldo Carvalho de Melo struct list_head *list, 14150d08e47SArnaldo Carvalho de Melo int nr_entries) 14250d08e47SArnaldo Carvalho de Melo { 14375562573SAdrian Hunter bool set_id_pos = !evlist->nr_entries; 14475562573SAdrian Hunter 14550d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 14650d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 14775562573SAdrian Hunter if (set_id_pos) 14875562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 14950d08e47SArnaldo Carvalho de Melo } 15050d08e47SArnaldo Carvalho de Melo 15163dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 15263dab225SArnaldo Carvalho de Melo { 15363dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 15463dab225SArnaldo Carvalho de Melo 15563dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 15697f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 15797f63e4aSNamhyung Kim 15897f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 15963dab225SArnaldo Carvalho de Melo 1600050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 16163dab225SArnaldo Carvalho de Melo evsel->leader = leader; 16263dab225SArnaldo Carvalho de Melo } 16363dab225SArnaldo Carvalho de Melo } 16463dab225SArnaldo Carvalho de Melo 16563dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1666a4bb04cSJiri Olsa { 16797f63e4aSNamhyung Kim if (evlist->nr_entries) { 16897f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 16963dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1706a4bb04cSJiri Olsa } 17197f63e4aSNamhyung Kim } 1726a4bb04cSJiri Olsa 173361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 174361c99a6SArnaldo Carvalho de Melo { 175361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 176361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 177361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 178361c99a6SArnaldo Carvalho de Melo }; 1791aed2671SJoerg Roedel struct perf_evsel *evsel; 180361c99a6SArnaldo Carvalho de Melo 1811aed2671SJoerg Roedel event_attr_init(&attr); 1821aed2671SJoerg Roedel 183ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 184361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 185cc2d86b0SStephane Eranian goto error; 186cc2d86b0SStephane Eranian 187cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 188cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 189cc2d86b0SStephane Eranian if (!evsel->name) 190cc2d86b0SStephane Eranian goto error_free; 191361c99a6SArnaldo Carvalho de Melo 192361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 193361c99a6SArnaldo Carvalho de Melo return 0; 194cc2d86b0SStephane Eranian error_free: 195cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 196cc2d86b0SStephane Eranian error: 197cc2d86b0SStephane Eranian return -ENOMEM; 198361c99a6SArnaldo Carvalho de Melo } 1995c581041SArnaldo Carvalho de Melo 200e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 20150d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 20250d08e47SArnaldo Carvalho de Melo { 20350d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 20450d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 20550d08e47SArnaldo Carvalho de Melo size_t i; 20650d08e47SArnaldo Carvalho de Melo 20750d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 208ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 20950d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 21050d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 21150d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 21250d08e47SArnaldo Carvalho de Melo } 21350d08e47SArnaldo Carvalho de Melo 21450d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 21550d08e47SArnaldo Carvalho de Melo 21650d08e47SArnaldo Carvalho de Melo return 0; 21750d08e47SArnaldo Carvalho de Melo 21850d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 2190050f7aaSArnaldo Carvalho de Melo __evlist__for_each_safe(&head, n, evsel) 22050d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 22150d08e47SArnaldo Carvalho de Melo return -1; 22250d08e47SArnaldo Carvalho de Melo } 22350d08e47SArnaldo Carvalho de Melo 22479695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 22579695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 22679695e1bSArnaldo Carvalho de Melo { 22779695e1bSArnaldo Carvalho de Melo size_t i; 22879695e1bSArnaldo Carvalho de Melo 22979695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 23079695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 23179695e1bSArnaldo Carvalho de Melo 23279695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 23379695e1bSArnaldo Carvalho de Melo } 23479695e1bSArnaldo Carvalho de Melo 235da378962SArnaldo Carvalho de Melo struct perf_evsel * 236ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 237ee29be62SArnaldo Carvalho de Melo { 238ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 239ee29be62SArnaldo Carvalho de Melo 2400050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 241ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 242ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 243ee29be62SArnaldo Carvalho de Melo return evsel; 244ee29be62SArnaldo Carvalho de Melo } 245ee29be62SArnaldo Carvalho de Melo 246ee29be62SArnaldo Carvalho de Melo return NULL; 247ee29be62SArnaldo Carvalho de Melo } 248ee29be62SArnaldo Carvalho de Melo 249a2f2804aSDavid Ahern struct perf_evsel * 250a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 251a2f2804aSDavid Ahern const char *name) 252a2f2804aSDavid Ahern { 253a2f2804aSDavid Ahern struct perf_evsel *evsel; 254a2f2804aSDavid Ahern 2550050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 256a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 257a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 258a2f2804aSDavid Ahern return evsel; 259a2f2804aSDavid Ahern } 260a2f2804aSDavid Ahern 261a2f2804aSDavid Ahern return NULL; 262a2f2804aSDavid Ahern } 263a2f2804aSDavid Ahern 26439876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 26539876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 26639876e7dSArnaldo Carvalho de Melo { 267ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 26839876e7dSArnaldo Carvalho de Melo 2698dd2a131SJiri Olsa if (IS_ERR(evsel)) 27039876e7dSArnaldo Carvalho de Melo return -1; 27139876e7dSArnaldo Carvalho de Melo 272744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 27339876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 27439876e7dSArnaldo Carvalho de Melo return 0; 27539876e7dSArnaldo Carvalho de Melo } 27639876e7dSArnaldo Carvalho de Melo 277bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 278bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 279bf8e8f4bSAdrian Hunter { 280bf8e8f4bSAdrian Hunter if (evsel->system_wide) 281bf8e8f4bSAdrian Hunter return 1; 282bf8e8f4bSAdrian Hunter else 283bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 284bf8e8f4bSAdrian Hunter } 285bf8e8f4bSAdrian Hunter 2864152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2874152ab37SArnaldo Carvalho de Melo { 2884152ab37SArnaldo Carvalho de Melo int cpu, thread; 2894152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 290b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 291bf8e8f4bSAdrian Hunter int nr_threads; 2924152ab37SArnaldo Carvalho de Melo 293b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 2940050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 295395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 2963fe4430dSJiri Olsa continue; 297bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 298b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 29955da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 30055da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 3014152ab37SArnaldo Carvalho de Melo } 3024152ab37SArnaldo Carvalho de Melo } 3032b56bcfbSArnaldo Carvalho de Melo 3042b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3054152ab37SArnaldo Carvalho de Melo } 3064152ab37SArnaldo Carvalho de Melo 307764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 308764e16a3SDavid Ahern { 309764e16a3SDavid Ahern int cpu, thread; 310764e16a3SDavid Ahern struct perf_evsel *pos; 311b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 312bf8e8f4bSAdrian Hunter int nr_threads; 313764e16a3SDavid Ahern 314b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 3150050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 316395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3173fe4430dSJiri Olsa continue; 318bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 319b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 32055da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 32155da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 322764e16a3SDavid Ahern } 323764e16a3SDavid Ahern } 3242b56bcfbSArnaldo Carvalho de Melo 3252b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3262b56bcfbSArnaldo Carvalho de Melo } 3272b56bcfbSArnaldo Carvalho de Melo 3282b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist) 3292b56bcfbSArnaldo Carvalho de Melo { 3302b56bcfbSArnaldo Carvalho de Melo (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); 331764e16a3SDavid Ahern } 332764e16a3SDavid Ahern 333395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist, 334395c3070SAdrian Hunter struct perf_evsel *evsel) 335395c3070SAdrian Hunter { 336395c3070SAdrian Hunter int cpu, thread, err; 337bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 338bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 339395c3070SAdrian Hunter 340395c3070SAdrian Hunter if (!evsel->fd) 341395c3070SAdrian Hunter return 0; 342395c3070SAdrian Hunter 343bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 344bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 345395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 346395c3070SAdrian Hunter PERF_EVENT_IOC_DISABLE, 0); 347395c3070SAdrian Hunter if (err) 348395c3070SAdrian Hunter return err; 349395c3070SAdrian Hunter } 350395c3070SAdrian Hunter } 351395c3070SAdrian Hunter return 0; 352395c3070SAdrian Hunter } 353395c3070SAdrian Hunter 354395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist, 355395c3070SAdrian Hunter struct perf_evsel *evsel) 356395c3070SAdrian Hunter { 357395c3070SAdrian Hunter int cpu, thread, err; 358bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 359bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 360395c3070SAdrian Hunter 361395c3070SAdrian Hunter if (!evsel->fd) 362395c3070SAdrian Hunter return -EINVAL; 363395c3070SAdrian Hunter 364bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 365bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 366395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 367395c3070SAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 368395c3070SAdrian Hunter if (err) 369395c3070SAdrian Hunter return err; 370395c3070SAdrian Hunter } 371395c3070SAdrian Hunter } 372395c3070SAdrian Hunter return 0; 373395c3070SAdrian Hunter } 374395c3070SAdrian Hunter 3751c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3761c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3771c65056cSAdrian Hunter { 3781c65056cSAdrian Hunter int thread, err; 3791c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3801c65056cSAdrian Hunter 3811c65056cSAdrian Hunter if (!evsel->fd) 3821c65056cSAdrian Hunter return -EINVAL; 3831c65056cSAdrian Hunter 3841c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 3851c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 3861c65056cSAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 3871c65056cSAdrian Hunter if (err) 3881c65056cSAdrian Hunter return err; 3891c65056cSAdrian Hunter } 3901c65056cSAdrian Hunter return 0; 3911c65056cSAdrian Hunter } 3921c65056cSAdrian Hunter 3931c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 3941c65056cSAdrian Hunter struct perf_evsel *evsel, 3951c65056cSAdrian Hunter int thread) 3961c65056cSAdrian Hunter { 3971c65056cSAdrian Hunter int cpu, err; 3981c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 3991c65056cSAdrian Hunter 4001c65056cSAdrian Hunter if (!evsel->fd) 4011c65056cSAdrian Hunter return -EINVAL; 4021c65056cSAdrian Hunter 4031c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 4041c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4051c65056cSAdrian Hunter if (err) 4061c65056cSAdrian Hunter return err; 4071c65056cSAdrian Hunter } 4081c65056cSAdrian Hunter return 0; 4091c65056cSAdrian Hunter } 4101c65056cSAdrian Hunter 4111c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4121c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4131c65056cSAdrian Hunter { 4141c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4151c65056cSAdrian Hunter 4161c65056cSAdrian Hunter if (per_cpu_mmaps) 4171c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4181c65056cSAdrian Hunter else 4191c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4201c65056cSAdrian Hunter } 4211c65056cSAdrian Hunter 422ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4235c581041SArnaldo Carvalho de Melo { 424b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 425b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 426bf8e8f4bSAdrian Hunter int nfds = 0; 427bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 428bf8e8f4bSAdrian Hunter 429cba9b847SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 430bf8e8f4bSAdrian Hunter if (evsel->system_wide) 431bf8e8f4bSAdrian Hunter nfds += nr_cpus; 432bf8e8f4bSAdrian Hunter else 433bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 434bf8e8f4bSAdrian Hunter } 435bf8e8f4bSAdrian Hunter 4361b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4371b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 438ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 439ad6765ddSArnaldo Carvalho de Melo 440ad6765ddSArnaldo Carvalho de Melo return 0; 4415c581041SArnaldo Carvalho de Melo } 44270082dd9SArnaldo Carvalho de Melo 443e4b356b5SArnaldo Carvalho de Melo static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx) 444e4b356b5SArnaldo Carvalho de Melo { 445e4b356b5SArnaldo Carvalho de Melo int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP); 446e4b356b5SArnaldo Carvalho de Melo /* 447e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 448e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 449e4b356b5SArnaldo Carvalho de Melo */ 450e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 451e4b356b5SArnaldo Carvalho de Melo evlist->pollfd.priv[pos].idx = idx; 452e4b356b5SArnaldo Carvalho de Melo 453e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 454e4b356b5SArnaldo Carvalho de Melo } 455e4b356b5SArnaldo Carvalho de Melo 456e4b356b5SArnaldo Carvalho de Melo return pos; 457e4b356b5SArnaldo Carvalho de Melo } 458e4b356b5SArnaldo Carvalho de Melo 459ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 46070082dd9SArnaldo Carvalho de Melo { 461e4b356b5SArnaldo Carvalho de Melo return __perf_evlist__add_pollfd(evlist, fd, -1); 462e4b356b5SArnaldo Carvalho de Melo } 4631b85337dSArnaldo Carvalho de Melo 464e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd) 465e4b356b5SArnaldo Carvalho de Melo { 466e4b356b5SArnaldo Carvalho de Melo struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd); 467e4b356b5SArnaldo Carvalho de Melo 468e4b356b5SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, fda->priv[fd].idx); 46970082dd9SArnaldo Carvalho de Melo } 47070db7533SArnaldo Carvalho de Melo 4711ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4721ddec7f0SArnaldo Carvalho de Melo { 473e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 474e4b356b5SArnaldo Carvalho de Melo perf_evlist__munmap_filtered); 4751ddec7f0SArnaldo Carvalho de Melo } 4761ddec7f0SArnaldo Carvalho de Melo 477f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 478f66a889dSArnaldo Carvalho de Melo { 4791b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 480f66a889dSArnaldo Carvalho de Melo } 481f66a889dSArnaldo Carvalho de Melo 482a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 483a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 4843d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4853d3b5e95SArnaldo Carvalho de Melo { 4863d3b5e95SArnaldo Carvalho de Melo int hash; 4873d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 4883d3b5e95SArnaldo Carvalho de Melo 4893d3b5e95SArnaldo Carvalho de Melo sid->id = id; 4903d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 4913d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 4923d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 4933d3b5e95SArnaldo Carvalho de Melo } 4943d3b5e95SArnaldo Carvalho de Melo 495a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 496a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 497a91e5431SArnaldo Carvalho de Melo { 498a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 499a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 500a91e5431SArnaldo Carvalho de Melo } 501a91e5431SArnaldo Carvalho de Melo 502a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 5033d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 504f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 505f8a95309SArnaldo Carvalho de Melo { 506f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5073d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 508e2b5abe0SJiri Olsa u64 id; 509e2b5abe0SJiri Olsa int ret; 510e2b5abe0SJiri Olsa 511e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 512e2b5abe0SJiri Olsa if (!ret) 513e2b5abe0SJiri Olsa goto add; 514e2b5abe0SJiri Olsa 515e2b5abe0SJiri Olsa if (errno != ENOTTY) 516e2b5abe0SJiri Olsa return -1; 517e2b5abe0SJiri Olsa 518e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 519f8a95309SArnaldo Carvalho de Melo 520c4861afeSJiri Olsa /* 521c4861afeSJiri Olsa * This way does not work with group format read, so bail 522c4861afeSJiri Olsa * out in that case. 523c4861afeSJiri Olsa */ 524c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 525c4861afeSJiri Olsa return -1; 526c4861afeSJiri Olsa 527f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 528f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 529f8a95309SArnaldo Carvalho de Melo return -1; 530f8a95309SArnaldo Carvalho de Melo 531f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 532f8a95309SArnaldo Carvalho de Melo ++id_idx; 533f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 534f8a95309SArnaldo Carvalho de Melo ++id_idx; 535f8a95309SArnaldo Carvalho de Melo 536e2b5abe0SJiri Olsa id = read_data[id_idx]; 537e2b5abe0SJiri Olsa 538e2b5abe0SJiri Olsa add: 539e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 540f8a95309SArnaldo Carvalho de Melo return 0; 541f8a95309SArnaldo Carvalho de Melo } 542f8a95309SArnaldo Carvalho de Melo 5433c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5443c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5453c659eedSAdrian Hunter int thread) 5463c659eedSAdrian Hunter { 5473c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5483c659eedSAdrian Hunter sid->idx = idx; 5493c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5503c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5513c659eedSAdrian Hunter else 5523c659eedSAdrian Hunter sid->cpu = -1; 5533c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 554e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5553c659eedSAdrian Hunter else 5563c659eedSAdrian Hunter sid->tid = -1; 5573c659eedSAdrian Hunter } 5583c659eedSAdrian Hunter 559932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 56070db7533SArnaldo Carvalho de Melo { 56170db7533SArnaldo Carvalho de Melo struct hlist_head *head; 56270db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 56370db7533SArnaldo Carvalho de Melo int hash; 56470db7533SArnaldo Carvalho de Melo 56570db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 56670db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 56770db7533SArnaldo Carvalho de Melo 568b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 56970db7533SArnaldo Carvalho de Melo if (sid->id == id) 570932a3594SJiri Olsa return sid; 571932a3594SJiri Olsa 572932a3594SJiri Olsa return NULL; 573932a3594SJiri Olsa } 574932a3594SJiri Olsa 575932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 576932a3594SJiri Olsa { 577932a3594SJiri Olsa struct perf_sample_id *sid; 578932a3594SJiri Olsa 57905169df5SAdrian Hunter if (evlist->nr_entries == 1 || !id) 580932a3594SJiri Olsa return perf_evlist__first(evlist); 581932a3594SJiri Olsa 582932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 583932a3594SJiri Olsa if (sid) 58470db7533SArnaldo Carvalho de Melo return sid->evsel; 58530e68bccSNamhyung Kim 58630e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 5870c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 58830e68bccSNamhyung Kim 58970db7533SArnaldo Carvalho de Melo return NULL; 59070db7533SArnaldo Carvalho de Melo } 59104391debSArnaldo Carvalho de Melo 59275562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 59375562573SAdrian Hunter union perf_event *event, u64 *id) 59475562573SAdrian Hunter { 59575562573SAdrian Hunter const u64 *array = event->sample.array; 59675562573SAdrian Hunter ssize_t n; 59775562573SAdrian Hunter 59875562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 59975562573SAdrian Hunter 60075562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 60175562573SAdrian Hunter if (evlist->id_pos >= n) 60275562573SAdrian Hunter return -1; 60375562573SAdrian Hunter *id = array[evlist->id_pos]; 60475562573SAdrian Hunter } else { 60575562573SAdrian Hunter if (evlist->is_pos > n) 60675562573SAdrian Hunter return -1; 60775562573SAdrian Hunter n -= evlist->is_pos; 60875562573SAdrian Hunter *id = array[n]; 60975562573SAdrian Hunter } 61075562573SAdrian Hunter return 0; 61175562573SAdrian Hunter } 61275562573SAdrian Hunter 61375562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 61475562573SAdrian Hunter union perf_event *event) 61575562573SAdrian Hunter { 61698be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 61775562573SAdrian Hunter struct hlist_head *head; 61875562573SAdrian Hunter struct perf_sample_id *sid; 61975562573SAdrian Hunter int hash; 62075562573SAdrian Hunter u64 id; 62175562573SAdrian Hunter 62275562573SAdrian Hunter if (evlist->nr_entries == 1) 62398be6966SAdrian Hunter return first; 62498be6966SAdrian Hunter 62598be6966SAdrian Hunter if (!first->attr.sample_id_all && 62698be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 62798be6966SAdrian Hunter return first; 62875562573SAdrian Hunter 62975562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 63075562573SAdrian Hunter return NULL; 63175562573SAdrian Hunter 63275562573SAdrian Hunter /* Synthesized events have an id of zero */ 63375562573SAdrian Hunter if (!id) 63498be6966SAdrian Hunter return first; 63575562573SAdrian Hunter 63675562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 63775562573SAdrian Hunter head = &evlist->heads[hash]; 63875562573SAdrian Hunter 63975562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 64075562573SAdrian Hunter if (sid->id == id) 64175562573SAdrian Hunter return sid->evsel; 64275562573SAdrian Hunter } 64375562573SAdrian Hunter return NULL; 64475562573SAdrian Hunter } 64575562573SAdrian Hunter 646aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 64704391debSArnaldo Carvalho de Melo { 648aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 6497951722dSSukadev Bhattiprolu u64 head; 6507b8283b5SDavid Ahern u64 old = md->prev; 65104391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 6528115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 65304391debSArnaldo Carvalho de Melo 6547951722dSSukadev Bhattiprolu /* 6557951722dSSukadev Bhattiprolu * Check if event was unmapped due to a POLLHUP/POLLERR. 6567951722dSSukadev Bhattiprolu */ 6577951722dSSukadev Bhattiprolu if (!atomic_read(&md->refcnt)) 6587951722dSSukadev Bhattiprolu return NULL; 6597951722dSSukadev Bhattiprolu 6607951722dSSukadev Bhattiprolu head = perf_mmap__read_head(md); 6617bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 66204391debSArnaldo Carvalho de Melo /* 66304391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 66404391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 66504391debSArnaldo Carvalho de Melo * 66604391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 66704391debSArnaldo Carvalho de Melo * 66804391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 66904391debSArnaldo Carvalho de Melo */ 6707bb41152SArnaldo Carvalho de Melo int diff = head - old; 67104391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 67204391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 67304391debSArnaldo Carvalho de Melo 67404391debSArnaldo Carvalho de Melo /* 67504391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 67604391debSArnaldo Carvalho de Melo */ 67704391debSArnaldo Carvalho de Melo old = head; 67804391debSArnaldo Carvalho de Melo } 6797bb41152SArnaldo Carvalho de Melo } 68004391debSArnaldo Carvalho de Melo 68104391debSArnaldo Carvalho de Melo if (old != head) { 68204391debSArnaldo Carvalho de Melo size_t size; 68304391debSArnaldo Carvalho de Melo 6848115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 68504391debSArnaldo Carvalho de Melo size = event->header.size; 68604391debSArnaldo Carvalho de Melo 68704391debSArnaldo Carvalho de Melo /* 68804391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 68904391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 69004391debSArnaldo Carvalho de Melo */ 69104391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 69204391debSArnaldo Carvalho de Melo unsigned int offset = old; 69304391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 694a65cb4b9SJiri Olsa void *dst = md->event_copy; 69504391debSArnaldo Carvalho de Melo 69604391debSArnaldo Carvalho de Melo do { 69704391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 69804391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 69904391debSArnaldo Carvalho de Melo offset += cpy; 70004391debSArnaldo Carvalho de Melo dst += cpy; 70104391debSArnaldo Carvalho de Melo len -= cpy; 70204391debSArnaldo Carvalho de Melo } while (len); 70304391debSArnaldo Carvalho de Melo 704a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 70504391debSArnaldo Carvalho de Melo } 70604391debSArnaldo Carvalho de Melo 70704391debSArnaldo Carvalho de Melo old += size; 70804391debSArnaldo Carvalho de Melo } 70904391debSArnaldo Carvalho de Melo 71004391debSArnaldo Carvalho de Melo md->prev = old; 7117bb41152SArnaldo Carvalho de Melo 71204391debSArnaldo Carvalho de Melo return event; 71304391debSArnaldo Carvalho de Melo } 714f8a95309SArnaldo Carvalho de Melo 71582396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 71682396986SArnaldo Carvalho de Melo { 717b72e74d1SAdrian Hunter return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base; 71882396986SArnaldo Carvalho de Melo } 71982396986SArnaldo Carvalho de Melo 72082396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx) 72182396986SArnaldo Carvalho de Melo { 7227143849aSArnaldo Carvalho de Melo atomic_inc(&evlist->mmap[idx].refcnt); 72382396986SArnaldo Carvalho de Melo } 72482396986SArnaldo Carvalho de Melo 72582396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx) 72682396986SArnaldo Carvalho de Melo { 7277143849aSArnaldo Carvalho de Melo BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0); 72882396986SArnaldo Carvalho de Melo 7297143849aSArnaldo Carvalho de Melo if (atomic_dec_and_test(&evlist->mmap[idx].refcnt)) 73082396986SArnaldo Carvalho de Melo __perf_evlist__munmap(evlist, idx); 73182396986SArnaldo Carvalho de Melo } 73282396986SArnaldo Carvalho de Melo 7338e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 7348e50d384SZhouyi Zhou { 7358e50d384SZhouyi Zhou struct perf_mmap *md = &evlist->mmap[idx]; 73682396986SArnaldo Carvalho de Melo 73782396986SArnaldo Carvalho de Melo if (!evlist->overwrite) { 7387b8283b5SDavid Ahern u64 old = md->prev; 7398e50d384SZhouyi Zhou 7408e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 7418e50d384SZhouyi Zhou } 74282396986SArnaldo Carvalho de Melo 7437143849aSArnaldo Carvalho de Melo if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md)) 74482396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 7458e50d384SZhouyi Zhou } 7468e50d384SZhouyi Zhou 747718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, 748718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 749718c602dSAdrian Hunter void *userpg __maybe_unused, 750718c602dSAdrian Hunter int fd __maybe_unused) 751718c602dSAdrian Hunter { 752718c602dSAdrian Hunter return 0; 753718c602dSAdrian Hunter } 754718c602dSAdrian Hunter 755718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) 756718c602dSAdrian Hunter { 757718c602dSAdrian Hunter } 758718c602dSAdrian Hunter 759718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init( 760718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 761718c602dSAdrian Hunter off_t auxtrace_offset __maybe_unused, 762718c602dSAdrian Hunter unsigned int auxtrace_pages __maybe_unused, 763718c602dSAdrian Hunter bool auxtrace_overwrite __maybe_unused) 764718c602dSAdrian Hunter { 765718c602dSAdrian Hunter } 766718c602dSAdrian Hunter 767718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx( 768718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 769718c602dSAdrian Hunter struct perf_evlist *evlist __maybe_unused, 770718c602dSAdrian Hunter int idx __maybe_unused, 771718c602dSAdrian Hunter bool per_cpu __maybe_unused) 772718c602dSAdrian Hunter { 773718c602dSAdrian Hunter } 774718c602dSAdrian Hunter 77593edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 77693edcbd9SAdrian Hunter { 77793edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 77893edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 77993edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 7807143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 0); 78193edcbd9SAdrian Hunter } 782718c602dSAdrian Hunter auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap); 78393edcbd9SAdrian Hunter } 78493edcbd9SAdrian Hunter 7857e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 786f8a95309SArnaldo Carvalho de Melo { 787aece948fSArnaldo Carvalho de Melo int i; 788f8a95309SArnaldo Carvalho de Melo 789983874d1SArnaldo Carvalho de Melo if (evlist->mmap == NULL) 790983874d1SArnaldo Carvalho de Melo return; 791983874d1SArnaldo Carvalho de Melo 79293edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 79393edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 794aece948fSArnaldo Carvalho de Melo 79504662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 796f8a95309SArnaldo Carvalho de Melo } 797f8a95309SArnaldo Carvalho de Melo 798806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 799f8a95309SArnaldo Carvalho de Melo { 800a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 801ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 802b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 803aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 804f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 805f8a95309SArnaldo Carvalho de Melo } 806f8a95309SArnaldo Carvalho de Melo 807a8a8f3ebSAdrian Hunter struct mmap_params { 808a8a8f3ebSAdrian Hunter int prot; 809a8a8f3ebSAdrian Hunter int mask; 810718c602dSAdrian Hunter struct auxtrace_mmap_params auxtrace_mp; 811a8a8f3ebSAdrian Hunter }; 812a8a8f3ebSAdrian Hunter 813a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, 814a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 815f8a95309SArnaldo Carvalho de Melo { 81682396986SArnaldo Carvalho de Melo /* 81782396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 81882396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 81982396986SArnaldo Carvalho de Melo * the ring buffer. 82082396986SArnaldo Carvalho de Melo * 82182396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 82282396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 82382396986SArnaldo Carvalho de Melo * waiting to be consumed. 82482396986SArnaldo Carvalho de Melo * 82582396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 82682396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 82782396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 82882396986SArnaldo Carvalho de Melo */ 8297143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 2); 830aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 831a8a8f3ebSAdrian Hunter evlist->mmap[idx].mask = mp->mask; 832a8a8f3ebSAdrian Hunter evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, 833f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 834301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 83502635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 83602635965SAdrian Hunter errno); 837301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 838f8a95309SArnaldo Carvalho de Melo return -1; 839301b195dSNelson Elhage } 840ad6765ddSArnaldo Carvalho de Melo 841718c602dSAdrian Hunter if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap, 842718c602dSAdrian Hunter &mp->auxtrace_mp, evlist->mmap[idx].base, fd)) 843718c602dSAdrian Hunter return -1; 844718c602dSAdrian Hunter 845f8a95309SArnaldo Carvalho de Melo return 0; 846f8a95309SArnaldo Carvalho de Melo } 847f8a95309SArnaldo Carvalho de Melo 84804e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 849a8a8f3ebSAdrian Hunter struct mmap_params *mp, int cpu, 850a8a8f3ebSAdrian Hunter int thread, int *output) 851aece948fSArnaldo Carvalho de Melo { 852aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 85304e21314SAdrian Hunter 8540050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 855bf8e8f4bSAdrian Hunter int fd; 856bf8e8f4bSAdrian Hunter 857bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 858bf8e8f4bSAdrian Hunter continue; 859bf8e8f4bSAdrian Hunter 860bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 86104e21314SAdrian Hunter 86204e21314SAdrian Hunter if (*output == -1) { 86304e21314SAdrian Hunter *output = fd; 864a8a8f3ebSAdrian Hunter if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) 86504e21314SAdrian Hunter return -1; 86604e21314SAdrian Hunter } else { 86704e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 86804e21314SAdrian Hunter return -1; 86982396986SArnaldo Carvalho de Melo 87082396986SArnaldo Carvalho de Melo perf_evlist__mmap_get(evlist, idx); 87104e21314SAdrian Hunter } 87204e21314SAdrian Hunter 873f90d194aSAdrian Hunter /* 874f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 875f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 876f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 877f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 878f90d194aSAdrian Hunter * Therefore don't add it for polling. 879f90d194aSAdrian Hunter */ 880f90d194aSAdrian Hunter if (!evsel->system_wide && 881f90d194aSAdrian Hunter __perf_evlist__add_pollfd(evlist, fd, idx) < 0) { 88282396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 883ad6765ddSArnaldo Carvalho de Melo return -1; 88482396986SArnaldo Carvalho de Melo } 885033fa713SArnaldo Carvalho de Melo 8863c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 8873c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 8883c659eedSAdrian Hunter fd) < 0) 88904e21314SAdrian Hunter return -1; 8903c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 8913c659eedSAdrian Hunter thread); 8923c659eedSAdrian Hunter } 89304e21314SAdrian Hunter } 89404e21314SAdrian Hunter 89504e21314SAdrian Hunter return 0; 89604e21314SAdrian Hunter } 89704e21314SAdrian Hunter 898a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 899a8a8f3ebSAdrian Hunter struct mmap_params *mp) 90004e21314SAdrian Hunter { 901aece948fSArnaldo Carvalho de Melo int cpu, thread; 902b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 903b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 904aece948fSArnaldo Carvalho de Melo 905e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 906b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 907aece948fSArnaldo Carvalho de Melo int output = -1; 908aece948fSArnaldo Carvalho de Melo 909718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 910718c602dSAdrian Hunter true); 911718c602dSAdrian Hunter 912b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 913a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 914a8a8f3ebSAdrian Hunter thread, &output)) 915aece948fSArnaldo Carvalho de Melo goto out_unmap; 916aece948fSArnaldo Carvalho de Melo } 917aece948fSArnaldo Carvalho de Melo } 918aece948fSArnaldo Carvalho de Melo 919aece948fSArnaldo Carvalho de Melo return 0; 920aece948fSArnaldo Carvalho de Melo 921aece948fSArnaldo Carvalho de Melo out_unmap: 92293edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 92393edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 924aece948fSArnaldo Carvalho de Melo return -1; 925aece948fSArnaldo Carvalho de Melo } 926aece948fSArnaldo Carvalho de Melo 927a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 928a8a8f3ebSAdrian Hunter struct mmap_params *mp) 929aece948fSArnaldo Carvalho de Melo { 930aece948fSArnaldo Carvalho de Melo int thread; 931b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 932aece948fSArnaldo Carvalho de Melo 933e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 934b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 935aece948fSArnaldo Carvalho de Melo int output = -1; 936aece948fSArnaldo Carvalho de Melo 937718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 938718c602dSAdrian Hunter false); 939718c602dSAdrian Hunter 940a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 941a8a8f3ebSAdrian Hunter &output)) 942aece948fSArnaldo Carvalho de Melo goto out_unmap; 943aece948fSArnaldo Carvalho de Melo } 944aece948fSArnaldo Carvalho de Melo 945aece948fSArnaldo Carvalho de Melo return 0; 946aece948fSArnaldo Carvalho de Melo 947aece948fSArnaldo Carvalho de Melo out_unmap: 94893edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 94993edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 950aece948fSArnaldo Carvalho de Melo return -1; 951aece948fSArnaldo Carvalho de Melo } 952aece948fSArnaldo Carvalho de Melo 953994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages) 954994a1f78SJiri Olsa { 9558185e881SArnaldo Carvalho de Melo if (pages == UINT_MAX) { 9568185e881SArnaldo Carvalho de Melo int max; 9578185e881SArnaldo Carvalho de Melo 9588185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 9598185e881SArnaldo Carvalho de Melo /* 9608185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 9618185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 9628185e881SArnaldo Carvalho de Melo * die yet... 9638185e881SArnaldo Carvalho de Melo */ 9648185e881SArnaldo Carvalho de Melo max = 512; 9658185e881SArnaldo Carvalho de Melo } else { 9668185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 9678185e881SArnaldo Carvalho de Melo } 9688185e881SArnaldo Carvalho de Melo 9698185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 9701be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 9711be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 9728185e881SArnaldo Carvalho de Melo } else if (!is_power_of_2(pages)) 973994a1f78SJiri Olsa return 0; 974994a1f78SJiri Olsa 975994a1f78SJiri Olsa return (pages + 1) * page_size; 976994a1f78SJiri Olsa } 977994a1f78SJiri Olsa 97833c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 97933c2dcfdSDavid Ahern unsigned long max) 980994a1f78SJiri Olsa { 9812fbe4abeSAdrian Hunter unsigned long pages, val; 98227050f53SJiri Olsa static struct parse_tag tags[] = { 98327050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 98427050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 98527050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 98627050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 98727050f53SJiri Olsa { .tag = 0 }, 98827050f53SJiri Olsa }; 989994a1f78SJiri Olsa 9908973504bSDavid Ahern if (str == NULL) 99133c2dcfdSDavid Ahern return -EINVAL; 9928973504bSDavid Ahern 99327050f53SJiri Olsa val = parse_tag_value(str, tags); 9942fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 99527050f53SJiri Olsa /* we got file size value */ 99627050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 99727050f53SJiri Olsa } else { 99827050f53SJiri Olsa /* we got pages count value */ 99927050f53SJiri Olsa char *eptr; 1000994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 100133c2dcfdSDavid Ahern if (*eptr != '\0') 100233c2dcfdSDavid Ahern return -EINVAL; 100327050f53SJiri Olsa } 1004994a1f78SJiri Olsa 10052bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 100633c2dcfdSDavid Ahern /* leave number of pages at 0 */ 10071dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 100833c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 100991529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 10101dbfa938SAdrian Hunter if (!pages) 10111dbfa938SAdrian Hunter return -EINVAL; 10129639837eSDavid Ahern pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 10139639837eSDavid Ahern pages * page_size, pages); 10142fbe4abeSAdrian Hunter } 10152fbe4abeSAdrian Hunter 101633c2dcfdSDavid Ahern if (pages > max) 101733c2dcfdSDavid Ahern return -EINVAL; 101833c2dcfdSDavid Ahern 101933c2dcfdSDavid Ahern return pages; 102033c2dcfdSDavid Ahern } 102133c2dcfdSDavid Ahern 1022e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 102333c2dcfdSDavid Ahern { 102433c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 102533c2dcfdSDavid Ahern long pages; 102633c2dcfdSDavid Ahern 1027f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 102833c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 102933c2dcfdSDavid Ahern 103033c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 103133c2dcfdSDavid Ahern if (pages < 0) { 103233c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 1033994a1f78SJiri Olsa return -1; 1034994a1f78SJiri Olsa } 1035994a1f78SJiri Olsa 1036994a1f78SJiri Olsa *mmap_pages = pages; 1037994a1f78SJiri Olsa return 0; 1038994a1f78SJiri Olsa } 1039994a1f78SJiri Olsa 1040e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 1041e9db1310SAdrian Hunter int unset __maybe_unused) 1042e9db1310SAdrian Hunter { 1043e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 1044e9db1310SAdrian Hunter } 1045e9db1310SAdrian Hunter 1046c83fa7f2SAdrian Hunter /** 1047718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 1048c83fa7f2SAdrian Hunter * @evlist: list of events 1049c83fa7f2SAdrian Hunter * @pages: map length in pages 1050c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 1051718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 1052718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1053f8a95309SArnaldo Carvalho de Melo * 1054c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1055c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1056c83fa7f2SAdrian Hunter * automatically. 1057f8a95309SArnaldo Carvalho de Melo * 1058718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1059718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1060718c602dSAdrian Hunter * 1061c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1062f8a95309SArnaldo Carvalho de Melo */ 1063718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, 1064718c602dSAdrian Hunter bool overwrite, unsigned int auxtrace_pages, 1065718c602dSAdrian Hunter bool auxtrace_overwrite) 1066f8a95309SArnaldo Carvalho de Melo { 1067aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 10687e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 10697e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1070a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1071a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1072a8a8f3ebSAdrian Hunter }; 107350a682ceSArnaldo Carvalho de Melo 10747e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 1075f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1076f8a95309SArnaldo Carvalho de Melo 10771b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1078f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1079f8a95309SArnaldo Carvalho de Melo 1080f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1081994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 10822af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1083a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1084f8a95309SArnaldo Carvalho de Melo 1085718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1086718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1087718c602dSAdrian Hunter 10880050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1089f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1090a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1091a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1092f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1093f8a95309SArnaldo Carvalho de Melo } 1094f8a95309SArnaldo Carvalho de Melo 1095ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1096a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1097f8a95309SArnaldo Carvalho de Melo 1098a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1099f8a95309SArnaldo Carvalho de Melo } 11007e2ed097SArnaldo Carvalho de Melo 1101718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 1102718c602dSAdrian Hunter bool overwrite) 1103718c602dSAdrian Hunter { 1104718c602dSAdrian Hunter return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false); 1105718c602dSAdrian Hunter } 1106718c602dSAdrian Hunter 1107b7f0c203SJiri Olsa static int perf_evlist__propagate_maps(struct perf_evlist *evlist, 110852361ff0SJiri Olsa bool has_user_cpus) 1109b7f0c203SJiri Olsa { 1110b7f0c203SJiri Olsa struct perf_evsel *evsel; 1111b7f0c203SJiri Olsa 1112b7f0c203SJiri Olsa evlist__for_each(evlist, evsel) { 1113b7f0c203SJiri Olsa /* 1114b7f0c203SJiri Olsa * We already have cpus for evsel (via PMU sysfs) so 1115b7f0c203SJiri Olsa * keep it, if there's no target cpu list defined. 1116b7f0c203SJiri Olsa */ 111752361ff0SJiri Olsa if (evsel->cpus && has_user_cpus) 1118b7f0c203SJiri Olsa cpu_map__put(evsel->cpus); 1119b7f0c203SJiri Olsa 112052361ff0SJiri Olsa if (!evsel->cpus || has_user_cpus) 1121b7f0c203SJiri Olsa evsel->cpus = cpu_map__get(evlist->cpus); 1122b7f0c203SJiri Olsa 1123578e91ecSJiri Olsa evsel->threads = thread_map__get(evlist->threads); 1124578e91ecSJiri Olsa 11250e5ffb31SJiri Olsa if ((evlist->cpus && !evsel->cpus) || 11260e5ffb31SJiri Olsa (evlist->threads && !evsel->threads)) 1127b7f0c203SJiri Olsa return -ENOMEM; 1128b7f0c203SJiri Olsa } 1129b7f0c203SJiri Olsa 1130b7f0c203SJiri Olsa return 0; 1131b7f0c203SJiri Olsa } 1132b7f0c203SJiri Olsa 1133602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 11347e2ed097SArnaldo Carvalho de Melo { 1135b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 1136b809ac10SNamhyung Kim target->uid); 11377e2ed097SArnaldo Carvalho de Melo 11387e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 11397e2ed097SArnaldo Carvalho de Melo return -1; 11407e2ed097SArnaldo Carvalho de Melo 11419c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 1142d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 1143879d77d0SNamhyung Kim else 1144879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 11457e2ed097SArnaldo Carvalho de Melo 11467e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 11477e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 11487e2ed097SArnaldo Carvalho de Melo 114952361ff0SJiri Olsa return perf_evlist__propagate_maps(evlist, !!target->cpu_list); 11507e2ed097SArnaldo Carvalho de Melo 11517e2ed097SArnaldo Carvalho de Melo out_delete_threads: 1152186fbb74SJiri Olsa thread_map__put(evlist->threads); 1153b2e19a93SYasser Shalabi evlist->threads = NULL; 11547e2ed097SArnaldo Carvalho de Melo return -1; 11557e2ed097SArnaldo Carvalho de Melo } 11567e2ed097SArnaldo Carvalho de Melo 11573de5cfb0SJiri Olsa int perf_evlist__set_maps(struct perf_evlist *evlist, 11583de5cfb0SJiri Olsa struct cpu_map *cpus, 11593de5cfb0SJiri Olsa struct thread_map *threads) 11603de5cfb0SJiri Olsa { 11613de5cfb0SJiri Olsa if (evlist->cpus) 11623de5cfb0SJiri Olsa cpu_map__put(evlist->cpus); 11633de5cfb0SJiri Olsa 11643de5cfb0SJiri Olsa evlist->cpus = cpus; 11653de5cfb0SJiri Olsa 11663de5cfb0SJiri Olsa if (evlist->threads) 11673de5cfb0SJiri Olsa thread_map__put(evlist->threads); 11683de5cfb0SJiri Olsa 11693de5cfb0SJiri Olsa evlist->threads = threads; 11703de5cfb0SJiri Olsa 11713de5cfb0SJiri Olsa return perf_evlist__propagate_maps(evlist, false); 11723de5cfb0SJiri Olsa } 11733de5cfb0SJiri Olsa 117423d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 11750a102479SFrederic Weisbecker { 11760a102479SFrederic Weisbecker struct perf_evsel *evsel; 1177745cefc5SArnaldo Carvalho de Melo int err = 0; 1178745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1179b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 11800a102479SFrederic Weisbecker 11810050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1182745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 11830a102479SFrederic Weisbecker continue; 1184745cefc5SArnaldo Carvalho de Melo 1185d988d5eeSKan Liang /* 1186d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1187d988d5eeSKan Liang * So evlist and evsel should always be same. 1188d988d5eeSKan Liang */ 1189f47805a2SArnaldo Carvalho de Melo err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter); 119023d4aad4SArnaldo Carvalho de Melo if (err) { 119123d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1192745cefc5SArnaldo Carvalho de Melo break; 11930a102479SFrederic Weisbecker } 119423d4aad4SArnaldo Carvalho de Melo } 11950a102479SFrederic Weisbecker 1196745cefc5SArnaldo Carvalho de Melo return err; 1197745cefc5SArnaldo Carvalho de Melo } 1198745cefc5SArnaldo Carvalho de Melo 1199745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1200745cefc5SArnaldo Carvalho de Melo { 1201745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1202745cefc5SArnaldo Carvalho de Melo int err = 0; 1203745cefc5SArnaldo Carvalho de Melo 12040050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 120594ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1206745cefc5SArnaldo Carvalho de Melo if (err) 1207745cefc5SArnaldo Carvalho de Melo break; 1208745cefc5SArnaldo Carvalho de Melo } 1209745cefc5SArnaldo Carvalho de Melo 1210745cefc5SArnaldo Carvalho de Melo return err; 12110a102479SFrederic Weisbecker } 121274429964SFrederic Weisbecker 1213be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1214cfd70a26SArnaldo Carvalho de Melo { 1215cfd70a26SArnaldo Carvalho de Melo char *filter; 1216be199adaSArnaldo Carvalho de Melo int ret = -1; 1217be199adaSArnaldo Carvalho de Melo size_t i; 1218cfd70a26SArnaldo Carvalho de Melo 1219be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1220be199adaSArnaldo Carvalho de Melo if (i == 0) { 1221be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1222cfd70a26SArnaldo Carvalho de Melo return -1; 1223be199adaSArnaldo Carvalho de Melo } else { 1224be199adaSArnaldo Carvalho de Melo char *tmp; 1225be199adaSArnaldo Carvalho de Melo 1226be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1227be199adaSArnaldo Carvalho de Melo goto out_free; 1228be199adaSArnaldo Carvalho de Melo 1229be199adaSArnaldo Carvalho de Melo free(filter); 1230be199adaSArnaldo Carvalho de Melo filter = tmp; 1231be199adaSArnaldo Carvalho de Melo } 1232be199adaSArnaldo Carvalho de Melo } 1233cfd70a26SArnaldo Carvalho de Melo 1234cfd70a26SArnaldo Carvalho de Melo ret = perf_evlist__set_filter(evlist, filter); 1235be199adaSArnaldo Carvalho de Melo out_free: 1236cfd70a26SArnaldo Carvalho de Melo free(filter); 1237cfd70a26SArnaldo Carvalho de Melo return ret; 1238cfd70a26SArnaldo Carvalho de Melo } 1239cfd70a26SArnaldo Carvalho de Melo 1240be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) 1241be199adaSArnaldo Carvalho de Melo { 1242be199adaSArnaldo Carvalho de Melo return perf_evlist__set_filter_pids(evlist, 1, &pid); 1243be199adaSArnaldo Carvalho de Melo } 1244be199adaSArnaldo Carvalho de Melo 12450c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 124674429964SFrederic Weisbecker { 124775562573SAdrian Hunter struct perf_evsel *pos; 1248c2a70653SArnaldo Carvalho de Melo 124975562573SAdrian Hunter if (evlist->nr_entries == 1) 125075562573SAdrian Hunter return true; 125175562573SAdrian Hunter 125275562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 125375562573SAdrian Hunter return false; 125475562573SAdrian Hunter 12550050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 125675562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 125775562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1258c2a70653SArnaldo Carvalho de Melo return false; 125974429964SFrederic Weisbecker } 126074429964SFrederic Weisbecker 1261c2a70653SArnaldo Carvalho de Melo return true; 1262c2a70653SArnaldo Carvalho de Melo } 1263c2a70653SArnaldo Carvalho de Melo 126475562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1265c2a70653SArnaldo Carvalho de Melo { 126675562573SAdrian Hunter struct perf_evsel *evsel; 126775562573SAdrian Hunter 126875562573SAdrian Hunter if (evlist->combined_sample_type) 126975562573SAdrian Hunter return evlist->combined_sample_type; 127075562573SAdrian Hunter 12710050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 127275562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 127375562573SAdrian Hunter 127475562573SAdrian Hunter return evlist->combined_sample_type; 127575562573SAdrian Hunter } 127675562573SAdrian Hunter 127775562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 127875562573SAdrian Hunter { 127975562573SAdrian Hunter evlist->combined_sample_type = 0; 128075562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1281c2a70653SArnaldo Carvalho de Melo } 1282c2a70653SArnaldo Carvalho de Melo 128398df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) 128498df858eSAndi Kleen { 128598df858eSAndi Kleen struct perf_evsel *evsel; 128698df858eSAndi Kleen u64 branch_type = 0; 128798df858eSAndi Kleen 128898df858eSAndi Kleen evlist__for_each(evlist, evsel) 128998df858eSAndi Kleen branch_type |= evsel->attr.branch_sample_type; 129098df858eSAndi Kleen return branch_type; 129198df858eSAndi Kleen } 129298df858eSAndi Kleen 12939ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 12949ede473cSJiri Olsa { 12959ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 12969ede473cSJiri Olsa u64 read_format = first->attr.read_format; 12979ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 12989ede473cSJiri Olsa 12990050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 13009ede473cSJiri Olsa if (read_format != pos->attr.read_format) 13019ede473cSJiri Olsa return false; 13029ede473cSJiri Olsa } 13039ede473cSJiri Olsa 13049ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 13059ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 13069ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 13079ede473cSJiri Olsa return false; 13089ede473cSJiri Olsa } 13099ede473cSJiri Olsa 13109ede473cSJiri Olsa return true; 13119ede473cSJiri Olsa } 13129ede473cSJiri Olsa 13139ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 13149ede473cSJiri Olsa { 13159ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 13169ede473cSJiri Olsa return first->attr.read_format; 13179ede473cSJiri Olsa } 13189ede473cSJiri Olsa 13190c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 132081e36bffSArnaldo Carvalho de Melo { 13210c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 132281e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 132381e36bffSArnaldo Carvalho de Melo u64 sample_type; 132481e36bffSArnaldo Carvalho de Melo u16 size = 0; 132581e36bffSArnaldo Carvalho de Melo 132681e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 132781e36bffSArnaldo Carvalho de Melo goto out; 132881e36bffSArnaldo Carvalho de Melo 132981e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 133081e36bffSArnaldo Carvalho de Melo 133181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 133281e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 133381e36bffSArnaldo Carvalho de Melo 133481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 133581e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 133681e36bffSArnaldo Carvalho de Melo 133781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 133881e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 133981e36bffSArnaldo Carvalho de Melo 134081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 134181e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 134281e36bffSArnaldo Carvalho de Melo 134381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 134481e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 134575562573SAdrian Hunter 134675562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 134775562573SAdrian Hunter size += sizeof(data->id); 134881e36bffSArnaldo Carvalho de Melo out: 134981e36bffSArnaldo Carvalho de Melo return size; 135081e36bffSArnaldo Carvalho de Melo } 135181e36bffSArnaldo Carvalho de Melo 13520c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1353c2a70653SArnaldo Carvalho de Melo { 13540c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1355c2a70653SArnaldo Carvalho de Melo 13560050f7aaSArnaldo Carvalho de Melo evlist__for_each_continue(evlist, pos) { 1357c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1358c2a70653SArnaldo Carvalho de Melo return false; 1359c2a70653SArnaldo Carvalho de Melo } 1360c2a70653SArnaldo Carvalho de Melo 1361c2a70653SArnaldo Carvalho de Melo return true; 136274429964SFrederic Weisbecker } 136374429964SFrederic Weisbecker 13640c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 136574429964SFrederic Weisbecker { 13660c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1367c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 136874429964SFrederic Weisbecker } 136981cce8deSArnaldo Carvalho de Melo 137081cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 137181cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 137281cce8deSArnaldo Carvalho de Melo { 137381cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 137481cce8deSArnaldo Carvalho de Melo } 1375727ab04eSArnaldo Carvalho de Melo 1376a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1377a74b4b66SNamhyung Kim { 1378a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1379a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1380a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 13818ad9219eSStephane Eranian int n; 1382a74b4b66SNamhyung Kim 13838ad9219eSStephane Eranian evlist__for_each_reverse(evlist, evsel) { 13848ad9219eSStephane Eranian n = evsel->cpus ? evsel->cpus->nr : ncpus; 13858ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 13868ad9219eSStephane Eranian } 1387a74b4b66SNamhyung Kim } 1388a74b4b66SNamhyung Kim 13894112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 13904112eb18SArnaldo Carvalho de Melo { 13914112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 13924112eb18SArnaldo Carvalho de Melo 13934112eb18SArnaldo Carvalho de Melo /* 13944112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 13954112eb18SArnaldo Carvalho de Melo * an all cpus map. 13964112eb18SArnaldo Carvalho de Melo * 13974112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 13984112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 13994112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 14004112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 14014112eb18SArnaldo Carvalho de Melo */ 14024112eb18SArnaldo Carvalho de Melo evlist->cpus = cpu_map__new(NULL); 14034112eb18SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 14044112eb18SArnaldo Carvalho de Melo goto out; 14054112eb18SArnaldo Carvalho de Melo 14064112eb18SArnaldo Carvalho de Melo evlist->threads = thread_map__new_dummy(); 14074112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL) 14084112eb18SArnaldo Carvalho de Melo goto out_free_cpus; 14094112eb18SArnaldo Carvalho de Melo 14104112eb18SArnaldo Carvalho de Melo err = 0; 14114112eb18SArnaldo Carvalho de Melo out: 14124112eb18SArnaldo Carvalho de Melo return err; 14134112eb18SArnaldo Carvalho de Melo out_free_cpus: 1414f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 14154112eb18SArnaldo Carvalho de Melo evlist->cpus = NULL; 14164112eb18SArnaldo Carvalho de Melo goto out; 14174112eb18SArnaldo Carvalho de Melo } 14184112eb18SArnaldo Carvalho de Melo 14196a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1420727ab04eSArnaldo Carvalho de Melo { 14216a4bb04cSJiri Olsa struct perf_evsel *evsel; 1422a74b4b66SNamhyung Kim int err; 1423727ab04eSArnaldo Carvalho de Melo 14244112eb18SArnaldo Carvalho de Melo /* 14254112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 14264112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 14274112eb18SArnaldo Carvalho de Melo */ 14284112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 14294112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 14304112eb18SArnaldo Carvalho de Melo if (err < 0) 14314112eb18SArnaldo Carvalho de Melo goto out_err; 14324112eb18SArnaldo Carvalho de Melo } 14334112eb18SArnaldo Carvalho de Melo 1434733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1435733cd2feSAdrian Hunter 14360050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 14376a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 1438727ab04eSArnaldo Carvalho de Melo if (err < 0) 1439727ab04eSArnaldo Carvalho de Melo goto out_err; 1440727ab04eSArnaldo Carvalho de Melo } 1441727ab04eSArnaldo Carvalho de Melo 1442727ab04eSArnaldo Carvalho de Melo return 0; 1443727ab04eSArnaldo Carvalho de Melo out_err: 1444a74b4b66SNamhyung Kim perf_evlist__close(evlist); 144541c21a68SNamhyung Kim errno = -err; 1446727ab04eSArnaldo Carvalho de Melo return err; 1447727ab04eSArnaldo Carvalho de Melo } 144835b9d88eSArnaldo Carvalho de Melo 1449602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 145055e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1451735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 145235b9d88eSArnaldo Carvalho de Melo { 145335b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 145435b9d88eSArnaldo Carvalho de Melo char bf; 145535b9d88eSArnaldo Carvalho de Melo 145635b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 145735b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 145835b9d88eSArnaldo Carvalho de Melo return -1; 145935b9d88eSArnaldo Carvalho de Melo } 146035b9d88eSArnaldo Carvalho de Melo 146135b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 146235b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 146335b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 146435b9d88eSArnaldo Carvalho de Melo } 146535b9d88eSArnaldo Carvalho de Melo 146635b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 146735b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 146835b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 146935b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 147035b9d88eSArnaldo Carvalho de Melo } 147135b9d88eSArnaldo Carvalho de Melo 147235b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 14735f1c4225SArnaldo Carvalho de Melo int ret; 14745f1c4225SArnaldo Carvalho de Melo 1475119fa3c9SNamhyung Kim if (pipe_output) 147635b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 147735b9d88eSArnaldo Carvalho de Melo 14780817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 14790817df08SDavid Ahern 148035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 148135b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 148235b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 148335b9d88eSArnaldo Carvalho de Melo 148435b9d88eSArnaldo Carvalho de Melo /* 148535b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 148635b9d88eSArnaldo Carvalho de Melo */ 148735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 148835b9d88eSArnaldo Carvalho de Melo 148935b9d88eSArnaldo Carvalho de Melo /* 149035b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 149135b9d88eSArnaldo Carvalho de Melo */ 14925f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 14935f1c4225SArnaldo Carvalho de Melo /* 14945f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 14955f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 14965f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 14975f1c4225SArnaldo Carvalho de Melo * 149820f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 14995f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 15005f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 15015f1c4225SArnaldo Carvalho de Melo * here. 15025f1c4225SArnaldo Carvalho de Melo */ 15035f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 15045f1c4225SArnaldo Carvalho de Melo if (ret == -1) 150535b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 15065f1c4225SArnaldo Carvalho de Melo exit(ret); 15075f1c4225SArnaldo Carvalho de Melo } 150835b9d88eSArnaldo Carvalho de Melo 150935b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 151035b9d88eSArnaldo Carvalho de Melo 1511735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1512f33cbe72SArnaldo Carvalho de Melo union sigval val; 1513f33cbe72SArnaldo Carvalho de Melo 1514f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1515f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 151635b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1517f33cbe72SArnaldo Carvalho de Melo } else 1518f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 151935b9d88eSArnaldo Carvalho de Melo exit(-1); 152035b9d88eSArnaldo Carvalho de Melo } 152135b9d88eSArnaldo Carvalho de Melo 1522735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1523735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1524735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1525735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1526735f7e0bSArnaldo Carvalho de Melo }; 1527735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1528735f7e0bSArnaldo Carvalho de Melo } 1529735f7e0bSArnaldo Carvalho de Melo 15301aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 15311aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 15321aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 15331aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 15341aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 15351aaf63b1SArnaldo Carvalho de Melo } 1536e13798c7SJiri Olsa thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 15371aaf63b1SArnaldo Carvalho de Melo } 153835b9d88eSArnaldo Carvalho de Melo 153935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 154035b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 154135b9d88eSArnaldo Carvalho de Melo /* 154235b9d88eSArnaldo Carvalho de Melo * wait for child to settle 154335b9d88eSArnaldo Carvalho de Melo */ 154435b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 154535b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 154635b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 154735b9d88eSArnaldo Carvalho de Melo } 154835b9d88eSArnaldo Carvalho de Melo 1549bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 155035b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 155135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 155235b9d88eSArnaldo Carvalho de Melo return 0; 155335b9d88eSArnaldo Carvalho de Melo 155435b9d88eSArnaldo Carvalho de Melo out_close_pipes: 155535b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 155635b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 155735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 155835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 155935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 156035b9d88eSArnaldo Carvalho de Melo return -1; 156135b9d88eSArnaldo Carvalho de Melo } 156235b9d88eSArnaldo Carvalho de Melo 156335b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 156435b9d88eSArnaldo Carvalho de Melo { 156535b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1566b3824404SDavid Ahern char bf = 0; 1567bcf3145fSNamhyung Kim int ret; 156835b9d88eSArnaldo Carvalho de Melo /* 156935b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 157035b9d88eSArnaldo Carvalho de Melo */ 1571bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1572bcf3145fSNamhyung Kim if (ret < 0) 1573bcf3145fSNamhyung Kim perror("enable to write to pipe"); 1574bcf3145fSNamhyung Kim 1575bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1576bcf3145fSNamhyung Kim return ret; 157735b9d88eSArnaldo Carvalho de Melo } 157835b9d88eSArnaldo Carvalho de Melo 157935b9d88eSArnaldo Carvalho de Melo return 0; 158035b9d88eSArnaldo Carvalho de Melo } 1581cb0b29e0SArnaldo Carvalho de Melo 1582a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 15830807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1584cb0b29e0SArnaldo Carvalho de Melo { 158575562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 158675562573SAdrian Hunter 158775562573SAdrian Hunter if (!evsel) 158875562573SAdrian Hunter return -EFAULT; 15890807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1590cb0b29e0SArnaldo Carvalho de Melo } 159178f067b3SArnaldo Carvalho de Melo 159278f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 159378f067b3SArnaldo Carvalho de Melo { 159478f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 159578f067b3SArnaldo Carvalho de Melo size_t printed = 0; 159678f067b3SArnaldo Carvalho de Melo 15970050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 159878f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 159978f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 160078f067b3SArnaldo Carvalho de Melo } 160178f067b3SArnaldo Carvalho de Melo 1602b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 160378f067b3SArnaldo Carvalho de Melo } 16046ef068cbSArnaldo Carvalho de Melo 1605a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, 1606a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1607a8f23d8fSArnaldo Carvalho de Melo { 1608a8f23d8fSArnaldo Carvalho de Melo int printed, value; 16096e81c74cSMasami Hiramatsu char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1610a8f23d8fSArnaldo Carvalho de Melo 1611a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1612a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1613a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1614a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1615a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1616a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1617a8f23d8fSArnaldo Carvalho de Melo 16181a47245dSAdrian Hunter value = perf_event_paranoid(); 1619a8f23d8fSArnaldo Carvalho de Melo 1620a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1621a8f23d8fSArnaldo Carvalho de Melo 1622a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1623a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1624a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1625a8f23d8fSArnaldo Carvalho de Melo } 1626a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16275229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1628a8f23d8fSArnaldo Carvalho de Melo 1629a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16305229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 16315229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1632a8f23d8fSArnaldo Carvalho de Melo break; 1633a8f23d8fSArnaldo Carvalho de Melo default: 1634a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1635a8f23d8fSArnaldo Carvalho de Melo break; 1636a8f23d8fSArnaldo Carvalho de Melo } 1637a8f23d8fSArnaldo Carvalho de Melo 1638a8f23d8fSArnaldo Carvalho de Melo return 0; 1639a8f23d8fSArnaldo Carvalho de Melo } 1640a025e4f0SAdrian Hunter 1641956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1642956fa571SArnaldo Carvalho de Melo { 1643956fa571SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1644e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1645956fa571SArnaldo Carvalho de Melo 1646956fa571SArnaldo Carvalho de Melo switch (err) { 1647956fa571SArnaldo Carvalho de Melo case EPERM: 1648e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1649e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1650e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1651956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1652e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1653e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1654e965bea1SArnaldo Carvalho de Melo 1655e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1656e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1657e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1658e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1659e965bea1SArnaldo Carvalho de Melo } 1660e965bea1SArnaldo Carvalho de Melo 1661e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1662e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1663956fa571SArnaldo Carvalho de Melo break; 1664956fa571SArnaldo Carvalho de Melo default: 1665956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1666956fa571SArnaldo Carvalho de Melo break; 1667956fa571SArnaldo Carvalho de Melo } 1668956fa571SArnaldo Carvalho de Melo 1669956fa571SArnaldo Carvalho de Melo return 0; 1670956fa571SArnaldo Carvalho de Melo } 1671956fa571SArnaldo Carvalho de Melo 1672a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1673a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1674a025e4f0SAdrian Hunter { 1675a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1676a025e4f0SAdrian Hunter LIST_HEAD(move); 1677a025e4f0SAdrian Hunter 1678a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1679a025e4f0SAdrian Hunter return; 1680a025e4f0SAdrian Hunter 16810050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, evsel) { 1682a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1683a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1684a025e4f0SAdrian Hunter } 1685a025e4f0SAdrian Hunter 1686a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1687a025e4f0SAdrian Hunter } 168860b0896cSAdrian Hunter 168960b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 169060b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 169160b0896cSAdrian Hunter { 169260b0896cSAdrian Hunter struct perf_evsel *evsel; 169360b0896cSAdrian Hunter 169460b0896cSAdrian Hunter if (tracking_evsel->tracking) 169560b0896cSAdrian Hunter return; 169660b0896cSAdrian Hunter 169760b0896cSAdrian Hunter evlist__for_each(evlist, evsel) { 169860b0896cSAdrian Hunter if (evsel != tracking_evsel) 169960b0896cSAdrian Hunter evsel->tracking = false; 170060b0896cSAdrian Hunter } 170160b0896cSAdrian Hunter 170260b0896cSAdrian Hunter tracking_evsel->tracking = true; 170360b0896cSAdrian Hunter } 1704