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" 10553873e1SBorislav Petkov #include <api/fs/debugfs.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> 2770db7533SArnaldo Carvalho de Melo 28f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 29a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 30f8a95309SArnaldo Carvalho de Melo 317e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 327e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 33361c99a6SArnaldo Carvalho de Melo { 3470db7533SArnaldo Carvalho de Melo int i; 3570db7533SArnaldo Carvalho de Melo 3670db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 3770db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 38361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 397e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 401b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 4135b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 42361c99a6SArnaldo Carvalho de Melo } 43361c99a6SArnaldo Carvalho de Melo 44334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 45ef1d1af2SArnaldo Carvalho de Melo { 46ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 47ef1d1af2SArnaldo Carvalho de Melo 48ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 49334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 50ef1d1af2SArnaldo Carvalho de Melo 51361c99a6SArnaldo Carvalho de Melo return evlist; 52361c99a6SArnaldo Carvalho de Melo } 53361c99a6SArnaldo Carvalho de Melo 54b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 55b22d54b0SJiri Olsa { 56b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 57b22d54b0SJiri Olsa 58b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 59b22d54b0SJiri Olsa perf_evlist__delete(evlist); 60b22d54b0SJiri Olsa evlist = NULL; 61b22d54b0SJiri Olsa } 62b22d54b0SJiri Olsa 63b22d54b0SJiri Olsa return evlist; 64b22d54b0SJiri Olsa } 65b22d54b0SJiri Olsa 6675562573SAdrian Hunter /** 6775562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 6875562573SAdrian Hunter * @evlist: selected event list 6975562573SAdrian Hunter * 7075562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 7175562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 7275562573SAdrian Hunter */ 7375562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 7475562573SAdrian Hunter { 7575562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 7675562573SAdrian Hunter 7775562573SAdrian Hunter evlist->id_pos = first->id_pos; 7875562573SAdrian Hunter evlist->is_pos = first->is_pos; 7975562573SAdrian Hunter } 8075562573SAdrian Hunter 81733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 82733cd2feSAdrian Hunter { 83733cd2feSAdrian Hunter struct perf_evsel *evsel; 84733cd2feSAdrian Hunter 850050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 86733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 87733cd2feSAdrian Hunter 88733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 89733cd2feSAdrian Hunter } 90733cd2feSAdrian Hunter 91361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 92361c99a6SArnaldo Carvalho de Melo { 93361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 94361c99a6SArnaldo Carvalho de Melo 950050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, pos) { 96361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 97361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 98361c99a6SArnaldo Carvalho de Melo } 99361c99a6SArnaldo Carvalho de Melo 100361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 101361c99a6SArnaldo Carvalho de Melo } 102361c99a6SArnaldo Carvalho de Melo 103ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 104ef1d1af2SArnaldo Carvalho de Melo { 10504662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1061b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 107ef1d1af2SArnaldo Carvalho de Melo } 108ef1d1af2SArnaldo Carvalho de Melo 109361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 110361c99a6SArnaldo Carvalho de Melo { 111983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 112f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 11303ad9747SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 11403ad9747SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 11503ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 11603ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 117361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 118ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 119361c99a6SArnaldo Carvalho de Melo free(evlist); 120361c99a6SArnaldo Carvalho de Melo } 121361c99a6SArnaldo Carvalho de Melo 122361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 123361c99a6SArnaldo Carvalho de Melo { 124361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 125ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 12660b0896cSAdrian Hunter entry->tracking = !entry->idx; 127ef503831SArnaldo Carvalho de Melo 12875562573SAdrian Hunter if (!evlist->nr_entries++) 12975562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 130361c99a6SArnaldo Carvalho de Melo } 131361c99a6SArnaldo Carvalho de Melo 1320529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 13350d08e47SArnaldo Carvalho de Melo struct list_head *list, 13450d08e47SArnaldo Carvalho de Melo int nr_entries) 13550d08e47SArnaldo Carvalho de Melo { 13675562573SAdrian Hunter bool set_id_pos = !evlist->nr_entries; 13775562573SAdrian Hunter 13850d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 13950d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 14075562573SAdrian Hunter if (set_id_pos) 14175562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 14250d08e47SArnaldo Carvalho de Melo } 14350d08e47SArnaldo Carvalho de Melo 14463dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 14563dab225SArnaldo Carvalho de Melo { 14663dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 14763dab225SArnaldo Carvalho de Melo 14863dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 14997f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 15097f63e4aSNamhyung Kim 15197f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 15263dab225SArnaldo Carvalho de Melo 1530050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 15463dab225SArnaldo Carvalho de Melo evsel->leader = leader; 15563dab225SArnaldo Carvalho de Melo } 15663dab225SArnaldo Carvalho de Melo } 15763dab225SArnaldo Carvalho de Melo 15863dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1596a4bb04cSJiri Olsa { 16097f63e4aSNamhyung Kim if (evlist->nr_entries) { 16197f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 16263dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1636a4bb04cSJiri Olsa } 16497f63e4aSNamhyung Kim } 1656a4bb04cSJiri Olsa 166361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 167361c99a6SArnaldo Carvalho de Melo { 168361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 169361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 170361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 171361c99a6SArnaldo Carvalho de Melo }; 1721aed2671SJoerg Roedel struct perf_evsel *evsel; 173361c99a6SArnaldo Carvalho de Melo 1741aed2671SJoerg Roedel event_attr_init(&attr); 1751aed2671SJoerg Roedel 176ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 177361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 178cc2d86b0SStephane Eranian goto error; 179cc2d86b0SStephane Eranian 180cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 181cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 182cc2d86b0SStephane Eranian if (!evsel->name) 183cc2d86b0SStephane Eranian goto error_free; 184361c99a6SArnaldo Carvalho de Melo 185361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 186361c99a6SArnaldo Carvalho de Melo return 0; 187cc2d86b0SStephane Eranian error_free: 188cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 189cc2d86b0SStephane Eranian error: 190cc2d86b0SStephane Eranian return -ENOMEM; 191361c99a6SArnaldo Carvalho de Melo } 1925c581041SArnaldo Carvalho de Melo 193e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 19450d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 19550d08e47SArnaldo Carvalho de Melo { 19650d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 19750d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 19850d08e47SArnaldo Carvalho de Melo size_t i; 19950d08e47SArnaldo Carvalho de Melo 20050d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 201ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 20250d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 20350d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 20450d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 20550d08e47SArnaldo Carvalho de Melo } 20650d08e47SArnaldo Carvalho de Melo 20750d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 20850d08e47SArnaldo Carvalho de Melo 20950d08e47SArnaldo Carvalho de Melo return 0; 21050d08e47SArnaldo Carvalho de Melo 21150d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 2120050f7aaSArnaldo Carvalho de Melo __evlist__for_each_safe(&head, n, evsel) 21350d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 21450d08e47SArnaldo Carvalho de Melo return -1; 21550d08e47SArnaldo Carvalho de Melo } 21650d08e47SArnaldo Carvalho de Melo 21779695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 21879695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 21979695e1bSArnaldo Carvalho de Melo { 22079695e1bSArnaldo Carvalho de Melo size_t i; 22179695e1bSArnaldo Carvalho de Melo 22279695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 22379695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 22479695e1bSArnaldo Carvalho de Melo 22579695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 22679695e1bSArnaldo Carvalho de Melo } 22779695e1bSArnaldo Carvalho de Melo 228da378962SArnaldo Carvalho de Melo struct perf_evsel * 229ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 230ee29be62SArnaldo Carvalho de Melo { 231ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 232ee29be62SArnaldo Carvalho de Melo 2330050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 234ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 235ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 236ee29be62SArnaldo Carvalho de Melo return evsel; 237ee29be62SArnaldo Carvalho de Melo } 238ee29be62SArnaldo Carvalho de Melo 239ee29be62SArnaldo Carvalho de Melo return NULL; 240ee29be62SArnaldo Carvalho de Melo } 241ee29be62SArnaldo Carvalho de Melo 242a2f2804aSDavid Ahern struct perf_evsel * 243a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 244a2f2804aSDavid Ahern const char *name) 245a2f2804aSDavid Ahern { 246a2f2804aSDavid Ahern struct perf_evsel *evsel; 247a2f2804aSDavid Ahern 2480050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 249a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 250a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 251a2f2804aSDavid Ahern return evsel; 252a2f2804aSDavid Ahern } 253a2f2804aSDavid Ahern 254a2f2804aSDavid Ahern return NULL; 255a2f2804aSDavid Ahern } 256a2f2804aSDavid Ahern 25739876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 25839876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 25939876e7dSArnaldo Carvalho de Melo { 260ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 26139876e7dSArnaldo Carvalho de Melo 26239876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 26339876e7dSArnaldo Carvalho de Melo return -1; 26439876e7dSArnaldo Carvalho de Melo 265744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 26639876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 26739876e7dSArnaldo Carvalho de Melo return 0; 26839876e7dSArnaldo Carvalho de Melo } 26939876e7dSArnaldo Carvalho de Melo 270bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 271bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 272bf8e8f4bSAdrian Hunter { 273bf8e8f4bSAdrian Hunter if (evsel->system_wide) 274bf8e8f4bSAdrian Hunter return 1; 275bf8e8f4bSAdrian Hunter else 276bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 277bf8e8f4bSAdrian Hunter } 278bf8e8f4bSAdrian Hunter 2794152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2804152ab37SArnaldo Carvalho de Melo { 2814152ab37SArnaldo Carvalho de Melo int cpu, thread; 2824152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 283b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 284bf8e8f4bSAdrian Hunter int nr_threads; 2854152ab37SArnaldo Carvalho de Melo 286b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 2870050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 288395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 2893fe4430dSJiri Olsa continue; 290bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 291b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 29255da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 29355da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2944152ab37SArnaldo Carvalho de Melo } 2954152ab37SArnaldo Carvalho de Melo } 2964152ab37SArnaldo Carvalho de Melo } 2974152ab37SArnaldo Carvalho de Melo 298764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 299764e16a3SDavid Ahern { 300764e16a3SDavid Ahern int cpu, thread; 301764e16a3SDavid Ahern struct perf_evsel *pos; 302b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 303bf8e8f4bSAdrian Hunter int nr_threads; 304764e16a3SDavid Ahern 305b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 3060050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 307395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3083fe4430dSJiri Olsa continue; 309bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 310b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 31155da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 31255da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 313764e16a3SDavid Ahern } 314764e16a3SDavid Ahern } 315764e16a3SDavid Ahern } 316764e16a3SDavid Ahern 317395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist, 318395c3070SAdrian Hunter struct perf_evsel *evsel) 319395c3070SAdrian Hunter { 320395c3070SAdrian Hunter int cpu, thread, err; 321bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 322bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 323395c3070SAdrian Hunter 324395c3070SAdrian Hunter if (!evsel->fd) 325395c3070SAdrian Hunter return 0; 326395c3070SAdrian Hunter 327bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 328bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 329395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 330395c3070SAdrian Hunter PERF_EVENT_IOC_DISABLE, 0); 331395c3070SAdrian Hunter if (err) 332395c3070SAdrian Hunter return err; 333395c3070SAdrian Hunter } 334395c3070SAdrian Hunter } 335395c3070SAdrian Hunter return 0; 336395c3070SAdrian Hunter } 337395c3070SAdrian Hunter 338395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist, 339395c3070SAdrian Hunter struct perf_evsel *evsel) 340395c3070SAdrian Hunter { 341395c3070SAdrian Hunter int cpu, thread, err; 342bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 343bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 344395c3070SAdrian Hunter 345395c3070SAdrian Hunter if (!evsel->fd) 346395c3070SAdrian Hunter return -EINVAL; 347395c3070SAdrian Hunter 348bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 349bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 350395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 351395c3070SAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 352395c3070SAdrian Hunter if (err) 353395c3070SAdrian Hunter return err; 354395c3070SAdrian Hunter } 355395c3070SAdrian Hunter } 356395c3070SAdrian Hunter return 0; 357395c3070SAdrian Hunter } 358395c3070SAdrian Hunter 3591c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3601c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3611c65056cSAdrian Hunter { 3621c65056cSAdrian Hunter int thread, err; 3631c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3641c65056cSAdrian Hunter 3651c65056cSAdrian Hunter if (!evsel->fd) 3661c65056cSAdrian Hunter return -EINVAL; 3671c65056cSAdrian Hunter 3681c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 3691c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 3701c65056cSAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 3711c65056cSAdrian Hunter if (err) 3721c65056cSAdrian Hunter return err; 3731c65056cSAdrian Hunter } 3741c65056cSAdrian Hunter return 0; 3751c65056cSAdrian Hunter } 3761c65056cSAdrian Hunter 3771c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 3781c65056cSAdrian Hunter struct perf_evsel *evsel, 3791c65056cSAdrian Hunter int thread) 3801c65056cSAdrian Hunter { 3811c65056cSAdrian Hunter int cpu, err; 3821c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 3831c65056cSAdrian Hunter 3841c65056cSAdrian Hunter if (!evsel->fd) 3851c65056cSAdrian Hunter return -EINVAL; 3861c65056cSAdrian Hunter 3871c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 3881c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3891c65056cSAdrian Hunter if (err) 3901c65056cSAdrian Hunter return err; 3911c65056cSAdrian Hunter } 3921c65056cSAdrian Hunter return 0; 3931c65056cSAdrian Hunter } 3941c65056cSAdrian Hunter 3951c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 3961c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 3971c65056cSAdrian Hunter { 3981c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 3991c65056cSAdrian Hunter 4001c65056cSAdrian Hunter if (per_cpu_mmaps) 4011c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4021c65056cSAdrian Hunter else 4031c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4041c65056cSAdrian Hunter } 4051c65056cSAdrian Hunter 406ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4075c581041SArnaldo Carvalho de Melo { 408b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 409b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 410bf8e8f4bSAdrian Hunter int nfds = 0; 411bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 412bf8e8f4bSAdrian Hunter 413bf8e8f4bSAdrian Hunter list_for_each_entry(evsel, &evlist->entries, node) { 414bf8e8f4bSAdrian Hunter if (evsel->system_wide) 415bf8e8f4bSAdrian Hunter nfds += nr_cpus; 416bf8e8f4bSAdrian Hunter else 417bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 418bf8e8f4bSAdrian Hunter } 419bf8e8f4bSAdrian Hunter 4201b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4211b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 422ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 423ad6765ddSArnaldo Carvalho de Melo 424ad6765ddSArnaldo Carvalho de Melo return 0; 4255c581041SArnaldo Carvalho de Melo } 42670082dd9SArnaldo Carvalho de Melo 427ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 42870082dd9SArnaldo Carvalho de Melo { 42970082dd9SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 4301b85337dSArnaldo Carvalho de Melo 4311b85337dSArnaldo Carvalho de Melo return fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP); 43270082dd9SArnaldo Carvalho de Melo } 43370db7533SArnaldo Carvalho de Melo 4341ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4351ddec7f0SArnaldo Carvalho de Melo { 4361b85337dSArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask); 4371ddec7f0SArnaldo Carvalho de Melo } 4381ddec7f0SArnaldo Carvalho de Melo 439f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 440f66a889dSArnaldo Carvalho de Melo { 4411b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 442f66a889dSArnaldo Carvalho de Melo } 443f66a889dSArnaldo Carvalho de Melo 444a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 445a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 4463d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4473d3b5e95SArnaldo Carvalho de Melo { 4483d3b5e95SArnaldo Carvalho de Melo int hash; 4493d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 4503d3b5e95SArnaldo Carvalho de Melo 4513d3b5e95SArnaldo Carvalho de Melo sid->id = id; 4523d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 4533d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 4543d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 4553d3b5e95SArnaldo Carvalho de Melo } 4563d3b5e95SArnaldo Carvalho de Melo 457a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 458a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 459a91e5431SArnaldo Carvalho de Melo { 460a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 461a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 462a91e5431SArnaldo Carvalho de Melo } 463a91e5431SArnaldo Carvalho de Melo 464a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 4653d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 466f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 467f8a95309SArnaldo Carvalho de Melo { 468f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 4693d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 470e2b5abe0SJiri Olsa u64 id; 471e2b5abe0SJiri Olsa int ret; 472e2b5abe0SJiri Olsa 473e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 474e2b5abe0SJiri Olsa if (!ret) 475e2b5abe0SJiri Olsa goto add; 476e2b5abe0SJiri Olsa 477e2b5abe0SJiri Olsa if (errno != ENOTTY) 478e2b5abe0SJiri Olsa return -1; 479e2b5abe0SJiri Olsa 480e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 481f8a95309SArnaldo Carvalho de Melo 482c4861afeSJiri Olsa /* 483c4861afeSJiri Olsa * This way does not work with group format read, so bail 484c4861afeSJiri Olsa * out in that case. 485c4861afeSJiri Olsa */ 486c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 487c4861afeSJiri Olsa return -1; 488c4861afeSJiri Olsa 489f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 490f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 491f8a95309SArnaldo Carvalho de Melo return -1; 492f8a95309SArnaldo Carvalho de Melo 493f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 494f8a95309SArnaldo Carvalho de Melo ++id_idx; 495f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 496f8a95309SArnaldo Carvalho de Melo ++id_idx; 497f8a95309SArnaldo Carvalho de Melo 498e2b5abe0SJiri Olsa id = read_data[id_idx]; 499e2b5abe0SJiri Olsa 500e2b5abe0SJiri Olsa add: 501e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 502f8a95309SArnaldo Carvalho de Melo return 0; 503f8a95309SArnaldo Carvalho de Melo } 504f8a95309SArnaldo Carvalho de Melo 505932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 50670db7533SArnaldo Carvalho de Melo { 50770db7533SArnaldo Carvalho de Melo struct hlist_head *head; 50870db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 50970db7533SArnaldo Carvalho de Melo int hash; 51070db7533SArnaldo Carvalho de Melo 51170db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 51270db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 51370db7533SArnaldo Carvalho de Melo 514b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 51570db7533SArnaldo Carvalho de Melo if (sid->id == id) 516932a3594SJiri Olsa return sid; 517932a3594SJiri Olsa 518932a3594SJiri Olsa return NULL; 519932a3594SJiri Olsa } 520932a3594SJiri Olsa 521932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 522932a3594SJiri Olsa { 523932a3594SJiri Olsa struct perf_sample_id *sid; 524932a3594SJiri Olsa 525932a3594SJiri Olsa if (evlist->nr_entries == 1) 526932a3594SJiri Olsa return perf_evlist__first(evlist); 527932a3594SJiri Olsa 528932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 529932a3594SJiri Olsa if (sid) 53070db7533SArnaldo Carvalho de Melo return sid->evsel; 53130e68bccSNamhyung Kim 53230e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 5330c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 53430e68bccSNamhyung Kim 53570db7533SArnaldo Carvalho de Melo return NULL; 53670db7533SArnaldo Carvalho de Melo } 53704391debSArnaldo Carvalho de Melo 53875562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 53975562573SAdrian Hunter union perf_event *event, u64 *id) 54075562573SAdrian Hunter { 54175562573SAdrian Hunter const u64 *array = event->sample.array; 54275562573SAdrian Hunter ssize_t n; 54375562573SAdrian Hunter 54475562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 54575562573SAdrian Hunter 54675562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 54775562573SAdrian Hunter if (evlist->id_pos >= n) 54875562573SAdrian Hunter return -1; 54975562573SAdrian Hunter *id = array[evlist->id_pos]; 55075562573SAdrian Hunter } else { 55175562573SAdrian Hunter if (evlist->is_pos > n) 55275562573SAdrian Hunter return -1; 55375562573SAdrian Hunter n -= evlist->is_pos; 55475562573SAdrian Hunter *id = array[n]; 55575562573SAdrian Hunter } 55675562573SAdrian Hunter return 0; 55775562573SAdrian Hunter } 55875562573SAdrian Hunter 55975562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 56075562573SAdrian Hunter union perf_event *event) 56175562573SAdrian Hunter { 56298be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 56375562573SAdrian Hunter struct hlist_head *head; 56475562573SAdrian Hunter struct perf_sample_id *sid; 56575562573SAdrian Hunter int hash; 56675562573SAdrian Hunter u64 id; 56775562573SAdrian Hunter 56875562573SAdrian Hunter if (evlist->nr_entries == 1) 56998be6966SAdrian Hunter return first; 57098be6966SAdrian Hunter 57198be6966SAdrian Hunter if (!first->attr.sample_id_all && 57298be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 57398be6966SAdrian Hunter return first; 57475562573SAdrian Hunter 57575562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 57675562573SAdrian Hunter return NULL; 57775562573SAdrian Hunter 57875562573SAdrian Hunter /* Synthesized events have an id of zero */ 57975562573SAdrian Hunter if (!id) 58098be6966SAdrian Hunter return first; 58175562573SAdrian Hunter 58275562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 58375562573SAdrian Hunter head = &evlist->heads[hash]; 58475562573SAdrian Hunter 58575562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 58675562573SAdrian Hunter if (sid->id == id) 58775562573SAdrian Hunter return sid->evsel; 58875562573SAdrian Hunter } 58975562573SAdrian Hunter return NULL; 59075562573SAdrian Hunter } 59175562573SAdrian Hunter 592aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 59304391debSArnaldo Carvalho de Melo { 594aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 59504391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 59604391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 59704391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 5988115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 59904391debSArnaldo Carvalho de Melo 6007bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 60104391debSArnaldo Carvalho de Melo /* 60204391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 60304391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 60404391debSArnaldo Carvalho de Melo * 60504391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 60604391debSArnaldo Carvalho de Melo * 60704391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 60804391debSArnaldo Carvalho de Melo */ 6097bb41152SArnaldo Carvalho de Melo int diff = head - old; 61004391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 61104391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 61204391debSArnaldo Carvalho de Melo 61304391debSArnaldo Carvalho de Melo /* 61404391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 61504391debSArnaldo Carvalho de Melo */ 61604391debSArnaldo Carvalho de Melo old = head; 61704391debSArnaldo Carvalho de Melo } 6187bb41152SArnaldo Carvalho de Melo } 61904391debSArnaldo Carvalho de Melo 62004391debSArnaldo Carvalho de Melo if (old != head) { 62104391debSArnaldo Carvalho de Melo size_t size; 62204391debSArnaldo Carvalho de Melo 6238115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 62404391debSArnaldo Carvalho de Melo size = event->header.size; 62504391debSArnaldo Carvalho de Melo 62604391debSArnaldo Carvalho de Melo /* 62704391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 62804391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 62904391debSArnaldo Carvalho de Melo */ 63004391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 63104391debSArnaldo Carvalho de Melo unsigned int offset = old; 63204391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 633a65cb4b9SJiri Olsa void *dst = md->event_copy; 63404391debSArnaldo Carvalho de Melo 63504391debSArnaldo Carvalho de Melo do { 63604391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 63704391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 63804391debSArnaldo Carvalho de Melo offset += cpy; 63904391debSArnaldo Carvalho de Melo dst += cpy; 64004391debSArnaldo Carvalho de Melo len -= cpy; 64104391debSArnaldo Carvalho de Melo } while (len); 64204391debSArnaldo Carvalho de Melo 643a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 64404391debSArnaldo Carvalho de Melo } 64504391debSArnaldo Carvalho de Melo 64604391debSArnaldo Carvalho de Melo old += size; 64704391debSArnaldo Carvalho de Melo } 64804391debSArnaldo Carvalho de Melo 64904391debSArnaldo Carvalho de Melo md->prev = old; 6507bb41152SArnaldo Carvalho de Melo 65104391debSArnaldo Carvalho de Melo return event; 65204391debSArnaldo Carvalho de Melo } 653f8a95309SArnaldo Carvalho de Melo 6548e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 6558e50d384SZhouyi Zhou { 6568e50d384SZhouyi Zhou if (!evlist->overwrite) { 6578e50d384SZhouyi Zhou struct perf_mmap *md = &evlist->mmap[idx]; 6588e50d384SZhouyi Zhou unsigned int old = md->prev; 6598e50d384SZhouyi Zhou 6608e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 6618e50d384SZhouyi Zhou } 6628e50d384SZhouyi Zhou } 6638e50d384SZhouyi Zhou 66493edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 66593edcbd9SAdrian Hunter { 66693edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 66793edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 66893edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 66993edcbd9SAdrian Hunter } 67093edcbd9SAdrian Hunter } 67193edcbd9SAdrian Hunter 6727e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 673f8a95309SArnaldo Carvalho de Melo { 674aece948fSArnaldo Carvalho de Melo int i; 675f8a95309SArnaldo Carvalho de Melo 676983874d1SArnaldo Carvalho de Melo if (evlist->mmap == NULL) 677983874d1SArnaldo Carvalho de Melo return; 678983874d1SArnaldo Carvalho de Melo 67993edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 68093edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 681aece948fSArnaldo Carvalho de Melo 68204662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 683f8a95309SArnaldo Carvalho de Melo } 684f8a95309SArnaldo Carvalho de Melo 685806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 686f8a95309SArnaldo Carvalho de Melo { 687a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 688ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 689b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 690aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 691f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 692f8a95309SArnaldo Carvalho de Melo } 693f8a95309SArnaldo Carvalho de Melo 694a8a8f3ebSAdrian Hunter struct mmap_params { 695a8a8f3ebSAdrian Hunter int prot; 696a8a8f3ebSAdrian Hunter int mask; 697a8a8f3ebSAdrian Hunter }; 698a8a8f3ebSAdrian Hunter 699a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, 700a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 701f8a95309SArnaldo Carvalho de Melo { 702aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 703a8a8f3ebSAdrian Hunter evlist->mmap[idx].mask = mp->mask; 704a8a8f3ebSAdrian Hunter evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, 705f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 706301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 70702635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 70802635965SAdrian Hunter errno); 709301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 710f8a95309SArnaldo Carvalho de Melo return -1; 711301b195dSNelson Elhage } 712ad6765ddSArnaldo Carvalho de Melo 713f8a95309SArnaldo Carvalho de Melo return 0; 714f8a95309SArnaldo Carvalho de Melo } 715f8a95309SArnaldo Carvalho de Melo 71604e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 717a8a8f3ebSAdrian Hunter struct mmap_params *mp, int cpu, 718a8a8f3ebSAdrian Hunter int thread, int *output) 719aece948fSArnaldo Carvalho de Melo { 720aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 72104e21314SAdrian Hunter 7220050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 723bf8e8f4bSAdrian Hunter int fd; 724bf8e8f4bSAdrian Hunter 725bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 726bf8e8f4bSAdrian Hunter continue; 727bf8e8f4bSAdrian Hunter 728bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 72904e21314SAdrian Hunter 73004e21314SAdrian Hunter if (*output == -1) { 73104e21314SAdrian Hunter *output = fd; 732a8a8f3ebSAdrian Hunter if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) 73304e21314SAdrian Hunter return -1; 73404e21314SAdrian Hunter } else { 73504e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 73604e21314SAdrian Hunter return -1; 73704e21314SAdrian Hunter } 73804e21314SAdrian Hunter 739ad6765ddSArnaldo Carvalho de Melo if (perf_evlist__add_pollfd(evlist, fd) < 0) 740ad6765ddSArnaldo Carvalho de Melo return -1; 741033fa713SArnaldo Carvalho de Melo 74204e21314SAdrian Hunter if ((evsel->attr.read_format & PERF_FORMAT_ID) && 74304e21314SAdrian Hunter perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) 74404e21314SAdrian Hunter return -1; 74504e21314SAdrian Hunter } 74604e21314SAdrian Hunter 74704e21314SAdrian Hunter return 0; 74804e21314SAdrian Hunter } 74904e21314SAdrian Hunter 750a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 751a8a8f3ebSAdrian Hunter struct mmap_params *mp) 75204e21314SAdrian Hunter { 753aece948fSArnaldo Carvalho de Melo int cpu, thread; 754b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 755b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 756aece948fSArnaldo Carvalho de Melo 757e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 758b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 759aece948fSArnaldo Carvalho de Melo int output = -1; 760aece948fSArnaldo Carvalho de Melo 761b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 762a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 763a8a8f3ebSAdrian Hunter thread, &output)) 764aece948fSArnaldo Carvalho de Melo goto out_unmap; 765aece948fSArnaldo Carvalho de Melo } 766aece948fSArnaldo Carvalho de Melo } 767aece948fSArnaldo Carvalho de Melo 768aece948fSArnaldo Carvalho de Melo return 0; 769aece948fSArnaldo Carvalho de Melo 770aece948fSArnaldo Carvalho de Melo out_unmap: 77193edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 77293edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 773aece948fSArnaldo Carvalho de Melo return -1; 774aece948fSArnaldo Carvalho de Melo } 775aece948fSArnaldo Carvalho de Melo 776a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 777a8a8f3ebSAdrian Hunter struct mmap_params *mp) 778aece948fSArnaldo Carvalho de Melo { 779aece948fSArnaldo Carvalho de Melo int thread; 780b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 781aece948fSArnaldo Carvalho de Melo 782e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 783b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 784aece948fSArnaldo Carvalho de Melo int output = -1; 785aece948fSArnaldo Carvalho de Melo 786a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 787a8a8f3ebSAdrian Hunter &output)) 788aece948fSArnaldo Carvalho de Melo goto out_unmap; 789aece948fSArnaldo Carvalho de Melo } 790aece948fSArnaldo Carvalho de Melo 791aece948fSArnaldo Carvalho de Melo return 0; 792aece948fSArnaldo Carvalho de Melo 793aece948fSArnaldo Carvalho de Melo out_unmap: 79493edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 79593edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 796aece948fSArnaldo Carvalho de Melo return -1; 797aece948fSArnaldo Carvalho de Melo } 798aece948fSArnaldo Carvalho de Melo 799994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages) 800994a1f78SJiri Olsa { 801994a1f78SJiri Olsa /* 512 kiB: default amount of unprivileged mlocked memory */ 802994a1f78SJiri Olsa if (pages == UINT_MAX) 803994a1f78SJiri Olsa pages = (512 * 1024) / page_size; 804994a1f78SJiri Olsa else if (!is_power_of_2(pages)) 805994a1f78SJiri Olsa return 0; 806994a1f78SJiri Olsa 807994a1f78SJiri Olsa return (pages + 1) * page_size; 808994a1f78SJiri Olsa } 809994a1f78SJiri Olsa 81033c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 81133c2dcfdSDavid Ahern unsigned long max) 812994a1f78SJiri Olsa { 8132fbe4abeSAdrian Hunter unsigned long pages, val; 81427050f53SJiri Olsa static struct parse_tag tags[] = { 81527050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 81627050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 81727050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 81827050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 81927050f53SJiri Olsa { .tag = 0 }, 82027050f53SJiri Olsa }; 821994a1f78SJiri Olsa 8228973504bSDavid Ahern if (str == NULL) 82333c2dcfdSDavid Ahern return -EINVAL; 8248973504bSDavid Ahern 82527050f53SJiri Olsa val = parse_tag_value(str, tags); 8262fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 82727050f53SJiri Olsa /* we got file size value */ 82827050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 82927050f53SJiri Olsa } else { 83027050f53SJiri Olsa /* we got pages count value */ 83127050f53SJiri Olsa char *eptr; 832994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 83333c2dcfdSDavid Ahern if (*eptr != '\0') 83433c2dcfdSDavid Ahern return -EINVAL; 83527050f53SJiri Olsa } 836994a1f78SJiri Olsa 8372bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 83833c2dcfdSDavid Ahern /* leave number of pages at 0 */ 8391dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 84033c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 8411dbfa938SAdrian Hunter pages = next_pow2_l(pages); 8421dbfa938SAdrian Hunter if (!pages) 8431dbfa938SAdrian Hunter return -EINVAL; 8449639837eSDavid Ahern pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 8459639837eSDavid Ahern pages * page_size, pages); 8462fbe4abeSAdrian Hunter } 8472fbe4abeSAdrian Hunter 84833c2dcfdSDavid Ahern if (pages > max) 84933c2dcfdSDavid Ahern return -EINVAL; 85033c2dcfdSDavid Ahern 85133c2dcfdSDavid Ahern return pages; 85233c2dcfdSDavid Ahern } 85333c2dcfdSDavid Ahern 85433c2dcfdSDavid Ahern int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 85533c2dcfdSDavid Ahern int unset __maybe_unused) 85633c2dcfdSDavid Ahern { 85733c2dcfdSDavid Ahern unsigned int *mmap_pages = opt->value; 85833c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 85933c2dcfdSDavid Ahern long pages; 86033c2dcfdSDavid Ahern 861f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 86233c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 86333c2dcfdSDavid Ahern 86433c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 86533c2dcfdSDavid Ahern if (pages < 0) { 86633c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 867994a1f78SJiri Olsa return -1; 868994a1f78SJiri Olsa } 869994a1f78SJiri Olsa 870994a1f78SJiri Olsa *mmap_pages = pages; 871994a1f78SJiri Olsa return 0; 872994a1f78SJiri Olsa } 873994a1f78SJiri Olsa 874c83fa7f2SAdrian Hunter /** 875c83fa7f2SAdrian Hunter * perf_evlist__mmap - Create mmaps to receive events. 876c83fa7f2SAdrian Hunter * @evlist: list of events 877c83fa7f2SAdrian Hunter * @pages: map length in pages 878c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 879f8a95309SArnaldo Carvalho de Melo * 880c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 881c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 882c83fa7f2SAdrian Hunter * automatically. 883f8a95309SArnaldo Carvalho de Melo * 884c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 885f8a95309SArnaldo Carvalho de Melo */ 88650a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 88750a682ceSArnaldo Carvalho de Melo bool overwrite) 888f8a95309SArnaldo Carvalho de Melo { 889aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 8907e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 8917e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 892a8a8f3ebSAdrian Hunter struct mmap_params mp = { 893a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 894a8a8f3ebSAdrian Hunter }; 89550a682ceSArnaldo Carvalho de Melo 8967e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 897f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 898f8a95309SArnaldo Carvalho de Melo 8991b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 900f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 901f8a95309SArnaldo Carvalho de Melo 902f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 903994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 9042af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 905a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 906f8a95309SArnaldo Carvalho de Melo 9070050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 908f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 909a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 910a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 911f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 912f8a95309SArnaldo Carvalho de Melo } 913f8a95309SArnaldo Carvalho de Melo 914ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 915a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 916f8a95309SArnaldo Carvalho de Melo 917a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 918f8a95309SArnaldo Carvalho de Melo } 9197e2ed097SArnaldo Carvalho de Melo 920602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 9217e2ed097SArnaldo Carvalho de Melo { 922b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 923b809ac10SNamhyung Kim target->uid); 9247e2ed097SArnaldo Carvalho de Melo 9257e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 9267e2ed097SArnaldo Carvalho de Melo return -1; 9277e2ed097SArnaldo Carvalho de Melo 9289c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 929d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 930879d77d0SNamhyung Kim else 931879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 9327e2ed097SArnaldo Carvalho de Melo 9337e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 9347e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 9357e2ed097SArnaldo Carvalho de Melo 9367e2ed097SArnaldo Carvalho de Melo return 0; 9377e2ed097SArnaldo Carvalho de Melo 9387e2ed097SArnaldo Carvalho de Melo out_delete_threads: 9397e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 9407e2ed097SArnaldo Carvalho de Melo return -1; 9417e2ed097SArnaldo Carvalho de Melo } 9427e2ed097SArnaldo Carvalho de Melo 9431491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 9440a102479SFrederic Weisbecker { 9450a102479SFrederic Weisbecker struct perf_evsel *evsel; 946745cefc5SArnaldo Carvalho de Melo int err = 0; 947745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 948b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 9490a102479SFrederic Weisbecker 9500050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 951745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 9520a102479SFrederic Weisbecker continue; 953745cefc5SArnaldo Carvalho de Melo 954745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 9550a102479SFrederic Weisbecker if (err) 956745cefc5SArnaldo Carvalho de Melo break; 9570a102479SFrederic Weisbecker } 9580a102479SFrederic Weisbecker 959745cefc5SArnaldo Carvalho de Melo return err; 960745cefc5SArnaldo Carvalho de Melo } 961745cefc5SArnaldo Carvalho de Melo 962745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 963745cefc5SArnaldo Carvalho de Melo { 964745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 965745cefc5SArnaldo Carvalho de Melo int err = 0; 966745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 967b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 968745cefc5SArnaldo Carvalho de Melo 9690050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 970745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 971745cefc5SArnaldo Carvalho de Melo if (err) 972745cefc5SArnaldo Carvalho de Melo break; 973745cefc5SArnaldo Carvalho de Melo } 974745cefc5SArnaldo Carvalho de Melo 975745cefc5SArnaldo Carvalho de Melo return err; 9760a102479SFrederic Weisbecker } 97774429964SFrederic Weisbecker 9780c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 97974429964SFrederic Weisbecker { 98075562573SAdrian Hunter struct perf_evsel *pos; 981c2a70653SArnaldo Carvalho de Melo 98275562573SAdrian Hunter if (evlist->nr_entries == 1) 98375562573SAdrian Hunter return true; 98475562573SAdrian Hunter 98575562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 98675562573SAdrian Hunter return false; 98775562573SAdrian Hunter 9880050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 98975562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 99075562573SAdrian Hunter pos->is_pos != evlist->is_pos) 991c2a70653SArnaldo Carvalho de Melo return false; 99274429964SFrederic Weisbecker } 99374429964SFrederic Weisbecker 994c2a70653SArnaldo Carvalho de Melo return true; 995c2a70653SArnaldo Carvalho de Melo } 996c2a70653SArnaldo Carvalho de Melo 99775562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 998c2a70653SArnaldo Carvalho de Melo { 99975562573SAdrian Hunter struct perf_evsel *evsel; 100075562573SAdrian Hunter 100175562573SAdrian Hunter if (evlist->combined_sample_type) 100275562573SAdrian Hunter return evlist->combined_sample_type; 100375562573SAdrian Hunter 10040050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 100575562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 100675562573SAdrian Hunter 100775562573SAdrian Hunter return evlist->combined_sample_type; 100875562573SAdrian Hunter } 100975562573SAdrian Hunter 101075562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 101175562573SAdrian Hunter { 101275562573SAdrian Hunter evlist->combined_sample_type = 0; 101375562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1014c2a70653SArnaldo Carvalho de Melo } 1015c2a70653SArnaldo Carvalho de Melo 10169ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 10179ede473cSJiri Olsa { 10189ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 10199ede473cSJiri Olsa u64 read_format = first->attr.read_format; 10209ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 10219ede473cSJiri Olsa 10220050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 10239ede473cSJiri Olsa if (read_format != pos->attr.read_format) 10249ede473cSJiri Olsa return false; 10259ede473cSJiri Olsa } 10269ede473cSJiri Olsa 10279ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 10289ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 10299ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 10309ede473cSJiri Olsa return false; 10319ede473cSJiri Olsa } 10329ede473cSJiri Olsa 10339ede473cSJiri Olsa return true; 10349ede473cSJiri Olsa } 10359ede473cSJiri Olsa 10369ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 10379ede473cSJiri Olsa { 10389ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 10399ede473cSJiri Olsa return first->attr.read_format; 10409ede473cSJiri Olsa } 10419ede473cSJiri Olsa 10420c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 104381e36bffSArnaldo Carvalho de Melo { 10440c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 104581e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 104681e36bffSArnaldo Carvalho de Melo u64 sample_type; 104781e36bffSArnaldo Carvalho de Melo u16 size = 0; 104881e36bffSArnaldo Carvalho de Melo 104981e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 105081e36bffSArnaldo Carvalho de Melo goto out; 105181e36bffSArnaldo Carvalho de Melo 105281e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 105381e36bffSArnaldo Carvalho de Melo 105481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 105581e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 105681e36bffSArnaldo Carvalho de Melo 105781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 105881e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 105981e36bffSArnaldo Carvalho de Melo 106081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 106181e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 106281e36bffSArnaldo Carvalho de Melo 106381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 106481e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 106581e36bffSArnaldo Carvalho de Melo 106681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 106781e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 106875562573SAdrian Hunter 106975562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 107075562573SAdrian Hunter size += sizeof(data->id); 107181e36bffSArnaldo Carvalho de Melo out: 107281e36bffSArnaldo Carvalho de Melo return size; 107381e36bffSArnaldo Carvalho de Melo } 107481e36bffSArnaldo Carvalho de Melo 10750c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1076c2a70653SArnaldo Carvalho de Melo { 10770c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1078c2a70653SArnaldo Carvalho de Melo 10790050f7aaSArnaldo Carvalho de Melo evlist__for_each_continue(evlist, pos) { 1080c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1081c2a70653SArnaldo Carvalho de Melo return false; 1082c2a70653SArnaldo Carvalho de Melo } 1083c2a70653SArnaldo Carvalho de Melo 1084c2a70653SArnaldo Carvalho de Melo return true; 108574429964SFrederic Weisbecker } 108674429964SFrederic Weisbecker 10870c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 108874429964SFrederic Weisbecker { 10890c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1090c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 109174429964SFrederic Weisbecker } 109281cce8deSArnaldo Carvalho de Melo 109381cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 109481cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 109581cce8deSArnaldo Carvalho de Melo { 109681cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 109781cce8deSArnaldo Carvalho de Melo } 1098727ab04eSArnaldo Carvalho de Melo 1099a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1100a74b4b66SNamhyung Kim { 1101a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1102a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1103a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 11048ad9219eSStephane Eranian int n; 1105a74b4b66SNamhyung Kim 11068ad9219eSStephane Eranian evlist__for_each_reverse(evlist, evsel) { 11078ad9219eSStephane Eranian n = evsel->cpus ? evsel->cpus->nr : ncpus; 11088ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 11098ad9219eSStephane Eranian } 1110a74b4b66SNamhyung Kim } 1111a74b4b66SNamhyung Kim 11126a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1113727ab04eSArnaldo Carvalho de Melo { 11146a4bb04cSJiri Olsa struct perf_evsel *evsel; 1115a74b4b66SNamhyung Kim int err; 1116727ab04eSArnaldo Carvalho de Melo 1117733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1118733cd2feSAdrian Hunter 11190050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 11206a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 1121727ab04eSArnaldo Carvalho de Melo if (err < 0) 1122727ab04eSArnaldo Carvalho de Melo goto out_err; 1123727ab04eSArnaldo Carvalho de Melo } 1124727ab04eSArnaldo Carvalho de Melo 1125727ab04eSArnaldo Carvalho de Melo return 0; 1126727ab04eSArnaldo Carvalho de Melo out_err: 1127a74b4b66SNamhyung Kim perf_evlist__close(evlist); 112841c21a68SNamhyung Kim errno = -err; 1129727ab04eSArnaldo Carvalho de Melo return err; 1130727ab04eSArnaldo Carvalho de Melo } 113135b9d88eSArnaldo Carvalho de Melo 1132602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 113355e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1134735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 113535b9d88eSArnaldo Carvalho de Melo { 113635b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 113735b9d88eSArnaldo Carvalho de Melo char bf; 113835b9d88eSArnaldo Carvalho de Melo 113935b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 114035b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 114135b9d88eSArnaldo Carvalho de Melo return -1; 114235b9d88eSArnaldo Carvalho de Melo } 114335b9d88eSArnaldo Carvalho de Melo 114435b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 114535b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 114635b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 114735b9d88eSArnaldo Carvalho de Melo } 114835b9d88eSArnaldo Carvalho de Melo 114935b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 115035b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 115135b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 115235b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 115335b9d88eSArnaldo Carvalho de Melo } 115435b9d88eSArnaldo Carvalho de Melo 115535b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 11565f1c4225SArnaldo Carvalho de Melo int ret; 11575f1c4225SArnaldo Carvalho de Melo 1158119fa3c9SNamhyung Kim if (pipe_output) 115935b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 116035b9d88eSArnaldo Carvalho de Melo 11610817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 11620817df08SDavid Ahern 116335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 116435b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 116535b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 116635b9d88eSArnaldo Carvalho de Melo 116735b9d88eSArnaldo Carvalho de Melo /* 116835b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 116935b9d88eSArnaldo Carvalho de Melo */ 117035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 117135b9d88eSArnaldo Carvalho de Melo 117235b9d88eSArnaldo Carvalho de Melo /* 117335b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 117435b9d88eSArnaldo Carvalho de Melo */ 11755f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 11765f1c4225SArnaldo Carvalho de Melo /* 11775f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 11785f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 11795f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 11805f1c4225SArnaldo Carvalho de Melo * 11815f1c4225SArnaldo Carvalho de Melo * For cancelling the workload without actuallin running it, 11825f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 11835f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 11845f1c4225SArnaldo Carvalho de Melo * here. 11855f1c4225SArnaldo Carvalho de Melo */ 11865f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 11875f1c4225SArnaldo Carvalho de Melo if (ret == -1) 118835b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 11895f1c4225SArnaldo Carvalho de Melo exit(ret); 11905f1c4225SArnaldo Carvalho de Melo } 119135b9d88eSArnaldo Carvalho de Melo 119235b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 119335b9d88eSArnaldo Carvalho de Melo 1194735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1195f33cbe72SArnaldo Carvalho de Melo union sigval val; 1196f33cbe72SArnaldo Carvalho de Melo 1197f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1198f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 119935b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1200f33cbe72SArnaldo Carvalho de Melo } else 1201f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 120235b9d88eSArnaldo Carvalho de Melo exit(-1); 120335b9d88eSArnaldo Carvalho de Melo } 120435b9d88eSArnaldo Carvalho de Melo 1205735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1206735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1207735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1208735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1209735f7e0bSArnaldo Carvalho de Melo }; 1210735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1211735f7e0bSArnaldo Carvalho de Melo } 1212735f7e0bSArnaldo Carvalho de Melo 1213602ad878SArnaldo Carvalho de Melo if (target__none(target)) 121435b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 121535b9d88eSArnaldo Carvalho de Melo 121635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 121735b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 121835b9d88eSArnaldo Carvalho de Melo /* 121935b9d88eSArnaldo Carvalho de Melo * wait for child to settle 122035b9d88eSArnaldo Carvalho de Melo */ 122135b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 122235b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 122335b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 122435b9d88eSArnaldo Carvalho de Melo } 122535b9d88eSArnaldo Carvalho de Melo 1226bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 122735b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 122835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 122935b9d88eSArnaldo Carvalho de Melo return 0; 123035b9d88eSArnaldo Carvalho de Melo 123135b9d88eSArnaldo Carvalho de Melo out_close_pipes: 123235b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 123335b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 123435b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 123535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 123635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 123735b9d88eSArnaldo Carvalho de Melo return -1; 123835b9d88eSArnaldo Carvalho de Melo } 123935b9d88eSArnaldo Carvalho de Melo 124035b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 124135b9d88eSArnaldo Carvalho de Melo { 124235b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1243b3824404SDavid Ahern char bf = 0; 1244bcf3145fSNamhyung Kim int ret; 124535b9d88eSArnaldo Carvalho de Melo /* 124635b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 124735b9d88eSArnaldo Carvalho de Melo */ 1248bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1249bcf3145fSNamhyung Kim if (ret < 0) 1250bcf3145fSNamhyung Kim perror("enable to write to pipe"); 1251bcf3145fSNamhyung Kim 1252bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1253bcf3145fSNamhyung Kim return ret; 125435b9d88eSArnaldo Carvalho de Melo } 125535b9d88eSArnaldo Carvalho de Melo 125635b9d88eSArnaldo Carvalho de Melo return 0; 125735b9d88eSArnaldo Carvalho de Melo } 1258cb0b29e0SArnaldo Carvalho de Melo 1259a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 12600807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1261cb0b29e0SArnaldo Carvalho de Melo { 126275562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 126375562573SAdrian Hunter 126475562573SAdrian Hunter if (!evsel) 126575562573SAdrian Hunter return -EFAULT; 12660807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1267cb0b29e0SArnaldo Carvalho de Melo } 126878f067b3SArnaldo Carvalho de Melo 126978f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 127078f067b3SArnaldo Carvalho de Melo { 127178f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 127278f067b3SArnaldo Carvalho de Melo size_t printed = 0; 127378f067b3SArnaldo Carvalho de Melo 12740050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 127578f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 127678f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 127778f067b3SArnaldo Carvalho de Melo } 127878f067b3SArnaldo Carvalho de Melo 1279b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 128078f067b3SArnaldo Carvalho de Melo } 12816ef068cbSArnaldo Carvalho de Melo 12826ef068cbSArnaldo Carvalho de Melo int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused, 12836ef068cbSArnaldo Carvalho de Melo int err, char *buf, size_t size) 12846ef068cbSArnaldo Carvalho de Melo { 12856ef068cbSArnaldo Carvalho de Melo char sbuf[128]; 12866ef068cbSArnaldo Carvalho de Melo 12876ef068cbSArnaldo Carvalho de Melo switch (err) { 12886ef068cbSArnaldo Carvalho de Melo case ENOENT: 12896ef068cbSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", 12906ef068cbSArnaldo Carvalho de Melo "Error:\tUnable to find debugfs\n" 12916ef068cbSArnaldo Carvalho de Melo "Hint:\tWas your kernel was compiled with debugfs support?\n" 12926ef068cbSArnaldo Carvalho de Melo "Hint:\tIs the debugfs filesystem mounted?\n" 12936ef068cbSArnaldo Carvalho de Melo "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'"); 12946ef068cbSArnaldo Carvalho de Melo break; 12956ef068cbSArnaldo Carvalho de Melo case EACCES: 12966ef068cbSArnaldo Carvalho de Melo scnprintf(buf, size, 12976ef068cbSArnaldo Carvalho de Melo "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n" 12986ef068cbSArnaldo Carvalho de Melo "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", 12996ef068cbSArnaldo Carvalho de Melo debugfs_mountpoint, debugfs_mountpoint); 13006ef068cbSArnaldo Carvalho de Melo break; 13016ef068cbSArnaldo Carvalho de Melo default: 13026ef068cbSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf))); 13036ef068cbSArnaldo Carvalho de Melo break; 13046ef068cbSArnaldo Carvalho de Melo } 13056ef068cbSArnaldo Carvalho de Melo 13066ef068cbSArnaldo Carvalho de Melo return 0; 13076ef068cbSArnaldo Carvalho de Melo } 1308a8f23d8fSArnaldo Carvalho de Melo 1309a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, 1310a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1311a8f23d8fSArnaldo Carvalho de Melo { 1312a8f23d8fSArnaldo Carvalho de Melo int printed, value; 13136e81c74cSMasami Hiramatsu char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1314a8f23d8fSArnaldo Carvalho de Melo 1315a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1316a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1317a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1318a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1319a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1320a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1321a8f23d8fSArnaldo Carvalho de Melo 13221a47245dSAdrian Hunter value = perf_event_paranoid(); 1323a8f23d8fSArnaldo Carvalho de Melo 1324a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1325a8f23d8fSArnaldo Carvalho de Melo 1326a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1327a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1328a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1329a8f23d8fSArnaldo Carvalho de Melo } 1330a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 13315229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1332a8f23d8fSArnaldo Carvalho de Melo 1333a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 13345229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 13355229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1336a8f23d8fSArnaldo Carvalho de Melo break; 1337a8f23d8fSArnaldo Carvalho de Melo default: 1338a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1339a8f23d8fSArnaldo Carvalho de Melo break; 1340a8f23d8fSArnaldo Carvalho de Melo } 1341a8f23d8fSArnaldo Carvalho de Melo 1342a8f23d8fSArnaldo Carvalho de Melo return 0; 1343a8f23d8fSArnaldo Carvalho de Melo } 1344a025e4f0SAdrian Hunter 1345a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1346a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1347a025e4f0SAdrian Hunter { 1348a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1349a025e4f0SAdrian Hunter LIST_HEAD(move); 1350a025e4f0SAdrian Hunter 1351a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1352a025e4f0SAdrian Hunter return; 1353a025e4f0SAdrian Hunter 13540050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, evsel) { 1355a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1356a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1357a025e4f0SAdrian Hunter } 1358a025e4f0SAdrian Hunter 1359a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1360a025e4f0SAdrian Hunter } 136160b0896cSAdrian Hunter 136260b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 136360b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 136460b0896cSAdrian Hunter { 136560b0896cSAdrian Hunter struct perf_evsel *evsel; 136660b0896cSAdrian Hunter 136760b0896cSAdrian Hunter if (tracking_evsel->tracking) 136860b0896cSAdrian Hunter return; 136960b0896cSAdrian Hunter 137060b0896cSAdrian Hunter evlist__for_each(evlist, evsel) { 137160b0896cSAdrian Hunter if (evsel != tracking_evsel) 137260b0896cSAdrian Hunter evsel->tracking = false; 137360b0896cSAdrian Hunter } 137460b0896cSAdrian Hunter 137560b0896cSAdrian Hunter tracking_evsel->tracking = true; 137660b0896cSAdrian Hunter } 1377