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> 11956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h> 125c581041SArnaldo Carvalho de Melo #include <poll.h> 13f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1512864b31SNamhyung Kim #include "target.h" 16361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 17361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 18e3e1a54fSAdrian Hunter #include "debug.h" 1935b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 20361c99a6SArnaldo Carvalho de Melo 2150d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 22994a1f78SJiri Olsa #include "parse-options.h" 2350d08e47SArnaldo Carvalho de Melo 24f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 25f8a95309SArnaldo Carvalho de Melo 2670db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2770db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 280389cd1fSArnaldo Carvalho de Melo #include <linux/log2.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); 102361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 103361c99a6SArnaldo Carvalho de Melo } 104361c99a6SArnaldo Carvalho de Melo 105361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 106361c99a6SArnaldo Carvalho de Melo } 107361c99a6SArnaldo Carvalho de Melo 108ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 109ef1d1af2SArnaldo Carvalho de Melo { 11004662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1111b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 112ef1d1af2SArnaldo Carvalho de Melo } 113ef1d1af2SArnaldo Carvalho de Melo 114361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 115361c99a6SArnaldo Carvalho de Melo { 116983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 117f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 11803ad9747SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 11903ad9747SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 12003ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 12103ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 122361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 123ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 124361c99a6SArnaldo Carvalho de Melo free(evlist); 125361c99a6SArnaldo Carvalho de Melo } 126361c99a6SArnaldo Carvalho de Melo 127361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 128361c99a6SArnaldo Carvalho de Melo { 129361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 130ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 13160b0896cSAdrian Hunter entry->tracking = !entry->idx; 132ef503831SArnaldo Carvalho de Melo 13375562573SAdrian Hunter if (!evlist->nr_entries++) 13475562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 135361c99a6SArnaldo Carvalho de Melo } 136361c99a6SArnaldo Carvalho de Melo 1370529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 13850d08e47SArnaldo Carvalho de Melo struct list_head *list, 13950d08e47SArnaldo Carvalho de Melo int nr_entries) 14050d08e47SArnaldo Carvalho de Melo { 14175562573SAdrian Hunter bool set_id_pos = !evlist->nr_entries; 14275562573SAdrian Hunter 14350d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 14450d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 14575562573SAdrian Hunter if (set_id_pos) 14675562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 14750d08e47SArnaldo Carvalho de Melo } 14850d08e47SArnaldo Carvalho de Melo 14963dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 15063dab225SArnaldo Carvalho de Melo { 15163dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 15263dab225SArnaldo Carvalho de Melo 15363dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 15497f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 15597f63e4aSNamhyung Kim 15697f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 15763dab225SArnaldo Carvalho de Melo 1580050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 15963dab225SArnaldo Carvalho de Melo evsel->leader = leader; 16063dab225SArnaldo Carvalho de Melo } 16163dab225SArnaldo Carvalho de Melo } 16263dab225SArnaldo Carvalho de Melo 16363dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1646a4bb04cSJiri Olsa { 16597f63e4aSNamhyung Kim if (evlist->nr_entries) { 16697f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 16763dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1686a4bb04cSJiri Olsa } 16997f63e4aSNamhyung Kim } 1706a4bb04cSJiri Olsa 171361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 172361c99a6SArnaldo Carvalho de Melo { 173361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 174361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 175361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 176361c99a6SArnaldo Carvalho de Melo }; 1771aed2671SJoerg Roedel struct perf_evsel *evsel; 178361c99a6SArnaldo Carvalho de Melo 1791aed2671SJoerg Roedel event_attr_init(&attr); 1801aed2671SJoerg Roedel 181ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 182361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 183cc2d86b0SStephane Eranian goto error; 184cc2d86b0SStephane Eranian 185cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 186cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 187cc2d86b0SStephane Eranian if (!evsel->name) 188cc2d86b0SStephane Eranian goto error_free; 189361c99a6SArnaldo Carvalho de Melo 190361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 191361c99a6SArnaldo Carvalho de Melo return 0; 192cc2d86b0SStephane Eranian error_free: 193cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 194cc2d86b0SStephane Eranian error: 195cc2d86b0SStephane Eranian return -ENOMEM; 196361c99a6SArnaldo Carvalho de Melo } 1975c581041SArnaldo Carvalho de Melo 198e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 19950d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 20050d08e47SArnaldo Carvalho de Melo { 20150d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 20250d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 20350d08e47SArnaldo Carvalho de Melo size_t i; 20450d08e47SArnaldo Carvalho de Melo 20550d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 206ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 20750d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 20850d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 20950d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 21050d08e47SArnaldo Carvalho de Melo } 21150d08e47SArnaldo Carvalho de Melo 21250d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 21350d08e47SArnaldo Carvalho de Melo 21450d08e47SArnaldo Carvalho de Melo return 0; 21550d08e47SArnaldo Carvalho de Melo 21650d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 2170050f7aaSArnaldo Carvalho de Melo __evlist__for_each_safe(&head, n, evsel) 21850d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 21950d08e47SArnaldo Carvalho de Melo return -1; 22050d08e47SArnaldo Carvalho de Melo } 22150d08e47SArnaldo Carvalho de Melo 22279695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 22379695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 22479695e1bSArnaldo Carvalho de Melo { 22579695e1bSArnaldo Carvalho de Melo size_t i; 22679695e1bSArnaldo Carvalho de Melo 22779695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 22879695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 22979695e1bSArnaldo Carvalho de Melo 23079695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 23179695e1bSArnaldo Carvalho de Melo } 23279695e1bSArnaldo Carvalho de Melo 233da378962SArnaldo Carvalho de Melo struct perf_evsel * 234ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 235ee29be62SArnaldo Carvalho de Melo { 236ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 237ee29be62SArnaldo Carvalho de Melo 2380050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 239ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 240ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 241ee29be62SArnaldo Carvalho de Melo return evsel; 242ee29be62SArnaldo Carvalho de Melo } 243ee29be62SArnaldo Carvalho de Melo 244ee29be62SArnaldo Carvalho de Melo return NULL; 245ee29be62SArnaldo Carvalho de Melo } 246ee29be62SArnaldo Carvalho de Melo 247a2f2804aSDavid Ahern struct perf_evsel * 248a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 249a2f2804aSDavid Ahern const char *name) 250a2f2804aSDavid Ahern { 251a2f2804aSDavid Ahern struct perf_evsel *evsel; 252a2f2804aSDavid Ahern 2530050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 254a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 255a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 256a2f2804aSDavid Ahern return evsel; 257a2f2804aSDavid Ahern } 258a2f2804aSDavid Ahern 259a2f2804aSDavid Ahern return NULL; 260a2f2804aSDavid Ahern } 261a2f2804aSDavid Ahern 26239876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 26339876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 26439876e7dSArnaldo Carvalho de Melo { 265ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 26639876e7dSArnaldo Carvalho de Melo 26739876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 26839876e7dSArnaldo Carvalho de Melo return -1; 26939876e7dSArnaldo Carvalho de Melo 270744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 27139876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 27239876e7dSArnaldo Carvalho de Melo return 0; 27339876e7dSArnaldo Carvalho de Melo } 27439876e7dSArnaldo Carvalho de Melo 275bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 276bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 277bf8e8f4bSAdrian Hunter { 278bf8e8f4bSAdrian Hunter if (evsel->system_wide) 279bf8e8f4bSAdrian Hunter return 1; 280bf8e8f4bSAdrian Hunter else 281bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 282bf8e8f4bSAdrian Hunter } 283bf8e8f4bSAdrian Hunter 2844152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2854152ab37SArnaldo Carvalho de Melo { 2864152ab37SArnaldo Carvalho de Melo int cpu, thread; 2874152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 288b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 289bf8e8f4bSAdrian Hunter int nr_threads; 2904152ab37SArnaldo Carvalho de Melo 291b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 2920050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 293395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 2943fe4430dSJiri Olsa continue; 295bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 296b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 29755da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 29855da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2994152ab37SArnaldo Carvalho de Melo } 3004152ab37SArnaldo Carvalho de Melo } 3014152ab37SArnaldo Carvalho de Melo } 3024152ab37SArnaldo Carvalho de Melo 303764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 304764e16a3SDavid Ahern { 305764e16a3SDavid Ahern int cpu, thread; 306764e16a3SDavid Ahern struct perf_evsel *pos; 307b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 308bf8e8f4bSAdrian Hunter int nr_threads; 309764e16a3SDavid Ahern 310b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 3110050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 312395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3133fe4430dSJiri Olsa continue; 314bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 315b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 31655da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 31755da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 318764e16a3SDavid Ahern } 319764e16a3SDavid Ahern } 320764e16a3SDavid Ahern } 321764e16a3SDavid Ahern 322395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist, 323395c3070SAdrian Hunter struct perf_evsel *evsel) 324395c3070SAdrian Hunter { 325395c3070SAdrian Hunter int cpu, thread, err; 326bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 327bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 328395c3070SAdrian Hunter 329395c3070SAdrian Hunter if (!evsel->fd) 330395c3070SAdrian Hunter return 0; 331395c3070SAdrian Hunter 332bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 333bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 334395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 335395c3070SAdrian Hunter PERF_EVENT_IOC_DISABLE, 0); 336395c3070SAdrian Hunter if (err) 337395c3070SAdrian Hunter return err; 338395c3070SAdrian Hunter } 339395c3070SAdrian Hunter } 340395c3070SAdrian Hunter return 0; 341395c3070SAdrian Hunter } 342395c3070SAdrian Hunter 343395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist, 344395c3070SAdrian Hunter struct perf_evsel *evsel) 345395c3070SAdrian Hunter { 346395c3070SAdrian Hunter int cpu, thread, err; 347bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 348bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 349395c3070SAdrian Hunter 350395c3070SAdrian Hunter if (!evsel->fd) 351395c3070SAdrian Hunter return -EINVAL; 352395c3070SAdrian Hunter 353bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 354bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 355395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 356395c3070SAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 357395c3070SAdrian Hunter if (err) 358395c3070SAdrian Hunter return err; 359395c3070SAdrian Hunter } 360395c3070SAdrian Hunter } 361395c3070SAdrian Hunter return 0; 362395c3070SAdrian Hunter } 363395c3070SAdrian Hunter 3641c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3651c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3661c65056cSAdrian Hunter { 3671c65056cSAdrian Hunter int thread, err; 3681c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3691c65056cSAdrian Hunter 3701c65056cSAdrian Hunter if (!evsel->fd) 3711c65056cSAdrian Hunter return -EINVAL; 3721c65056cSAdrian Hunter 3731c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 3741c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 3751c65056cSAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 3761c65056cSAdrian Hunter if (err) 3771c65056cSAdrian Hunter return err; 3781c65056cSAdrian Hunter } 3791c65056cSAdrian Hunter return 0; 3801c65056cSAdrian Hunter } 3811c65056cSAdrian Hunter 3821c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 3831c65056cSAdrian Hunter struct perf_evsel *evsel, 3841c65056cSAdrian Hunter int thread) 3851c65056cSAdrian Hunter { 3861c65056cSAdrian Hunter int cpu, err; 3871c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 3881c65056cSAdrian Hunter 3891c65056cSAdrian Hunter if (!evsel->fd) 3901c65056cSAdrian Hunter return -EINVAL; 3911c65056cSAdrian Hunter 3921c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 3931c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3941c65056cSAdrian Hunter if (err) 3951c65056cSAdrian Hunter return err; 3961c65056cSAdrian Hunter } 3971c65056cSAdrian Hunter return 0; 3981c65056cSAdrian Hunter } 3991c65056cSAdrian Hunter 4001c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4011c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4021c65056cSAdrian Hunter { 4031c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4041c65056cSAdrian Hunter 4051c65056cSAdrian Hunter if (per_cpu_mmaps) 4061c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4071c65056cSAdrian Hunter else 4081c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4091c65056cSAdrian Hunter } 4101c65056cSAdrian Hunter 411ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4125c581041SArnaldo Carvalho de Melo { 413b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 414b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 415bf8e8f4bSAdrian Hunter int nfds = 0; 416bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 417bf8e8f4bSAdrian Hunter 418cba9b847SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 419bf8e8f4bSAdrian Hunter if (evsel->system_wide) 420bf8e8f4bSAdrian Hunter nfds += nr_cpus; 421bf8e8f4bSAdrian Hunter else 422bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 423bf8e8f4bSAdrian Hunter } 424bf8e8f4bSAdrian Hunter 4251b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4261b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 427ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 428ad6765ddSArnaldo Carvalho de Melo 429ad6765ddSArnaldo Carvalho de Melo return 0; 4305c581041SArnaldo Carvalho de Melo } 43170082dd9SArnaldo Carvalho de Melo 432e4b356b5SArnaldo Carvalho de Melo static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx) 433e4b356b5SArnaldo Carvalho de Melo { 434e4b356b5SArnaldo Carvalho de Melo int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP); 435e4b356b5SArnaldo Carvalho de Melo /* 436e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 437e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 438e4b356b5SArnaldo Carvalho de Melo */ 439e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 440e4b356b5SArnaldo Carvalho de Melo evlist->pollfd.priv[pos].idx = idx; 441e4b356b5SArnaldo Carvalho de Melo 442e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 443e4b356b5SArnaldo Carvalho de Melo } 444e4b356b5SArnaldo Carvalho de Melo 445e4b356b5SArnaldo Carvalho de Melo return pos; 446e4b356b5SArnaldo Carvalho de Melo } 447e4b356b5SArnaldo Carvalho de Melo 448ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 44970082dd9SArnaldo Carvalho de Melo { 450e4b356b5SArnaldo Carvalho de Melo return __perf_evlist__add_pollfd(evlist, fd, -1); 451e4b356b5SArnaldo Carvalho de Melo } 4521b85337dSArnaldo Carvalho de Melo 453e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd) 454e4b356b5SArnaldo Carvalho de Melo { 455e4b356b5SArnaldo Carvalho de Melo struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd); 456e4b356b5SArnaldo Carvalho de Melo 457e4b356b5SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, fda->priv[fd].idx); 45870082dd9SArnaldo Carvalho de Melo } 45970db7533SArnaldo Carvalho de Melo 4601ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4611ddec7f0SArnaldo Carvalho de Melo { 462e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 463e4b356b5SArnaldo Carvalho de Melo perf_evlist__munmap_filtered); 4641ddec7f0SArnaldo Carvalho de Melo } 4651ddec7f0SArnaldo Carvalho de Melo 466f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 467f66a889dSArnaldo Carvalho de Melo { 4681b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 469f66a889dSArnaldo Carvalho de Melo } 470f66a889dSArnaldo Carvalho de Melo 471a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 472a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 4733d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4743d3b5e95SArnaldo Carvalho de Melo { 4753d3b5e95SArnaldo Carvalho de Melo int hash; 4763d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 4773d3b5e95SArnaldo Carvalho de Melo 4783d3b5e95SArnaldo Carvalho de Melo sid->id = id; 4793d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 4803d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 4813d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 4823d3b5e95SArnaldo Carvalho de Melo } 4833d3b5e95SArnaldo Carvalho de Melo 484a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 485a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 486a91e5431SArnaldo Carvalho de Melo { 487a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 488a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 489a91e5431SArnaldo Carvalho de Melo } 490a91e5431SArnaldo Carvalho de Melo 491a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 4923d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 493f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 494f8a95309SArnaldo Carvalho de Melo { 495f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 4963d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 497e2b5abe0SJiri Olsa u64 id; 498e2b5abe0SJiri Olsa int ret; 499e2b5abe0SJiri Olsa 500e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 501e2b5abe0SJiri Olsa if (!ret) 502e2b5abe0SJiri Olsa goto add; 503e2b5abe0SJiri Olsa 504e2b5abe0SJiri Olsa if (errno != ENOTTY) 505e2b5abe0SJiri Olsa return -1; 506e2b5abe0SJiri Olsa 507e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 508f8a95309SArnaldo Carvalho de Melo 509c4861afeSJiri Olsa /* 510c4861afeSJiri Olsa * This way does not work with group format read, so bail 511c4861afeSJiri Olsa * out in that case. 512c4861afeSJiri Olsa */ 513c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 514c4861afeSJiri Olsa return -1; 515c4861afeSJiri Olsa 516f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 517f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 518f8a95309SArnaldo Carvalho de Melo return -1; 519f8a95309SArnaldo Carvalho de Melo 520f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 521f8a95309SArnaldo Carvalho de Melo ++id_idx; 522f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 523f8a95309SArnaldo Carvalho de Melo ++id_idx; 524f8a95309SArnaldo Carvalho de Melo 525e2b5abe0SJiri Olsa id = read_data[id_idx]; 526e2b5abe0SJiri Olsa 527e2b5abe0SJiri Olsa add: 528e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 529f8a95309SArnaldo Carvalho de Melo return 0; 530f8a95309SArnaldo Carvalho de Melo } 531f8a95309SArnaldo Carvalho de Melo 5323c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5333c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5343c659eedSAdrian Hunter int thread) 5353c659eedSAdrian Hunter { 5363c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5373c659eedSAdrian Hunter sid->idx = idx; 5383c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5393c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5403c659eedSAdrian Hunter else 5413c659eedSAdrian Hunter sid->cpu = -1; 5423c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 5433c659eedSAdrian Hunter sid->tid = evlist->threads->map[thread]; 5443c659eedSAdrian Hunter else 5453c659eedSAdrian Hunter sid->tid = -1; 5463c659eedSAdrian Hunter } 5473c659eedSAdrian Hunter 548932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 54970db7533SArnaldo Carvalho de Melo { 55070db7533SArnaldo Carvalho de Melo struct hlist_head *head; 55170db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 55270db7533SArnaldo Carvalho de Melo int hash; 55370db7533SArnaldo Carvalho de Melo 55470db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 55570db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 55670db7533SArnaldo Carvalho de Melo 557b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 55870db7533SArnaldo Carvalho de Melo if (sid->id == id) 559932a3594SJiri Olsa return sid; 560932a3594SJiri Olsa 561932a3594SJiri Olsa return NULL; 562932a3594SJiri Olsa } 563932a3594SJiri Olsa 564932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 565932a3594SJiri Olsa { 566932a3594SJiri Olsa struct perf_sample_id *sid; 567932a3594SJiri Olsa 568932a3594SJiri Olsa if (evlist->nr_entries == 1) 569932a3594SJiri Olsa return perf_evlist__first(evlist); 570932a3594SJiri Olsa 571932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 572932a3594SJiri Olsa if (sid) 57370db7533SArnaldo Carvalho de Melo return sid->evsel; 57430e68bccSNamhyung Kim 57530e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 5760c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 57730e68bccSNamhyung Kim 57870db7533SArnaldo Carvalho de Melo return NULL; 57970db7533SArnaldo Carvalho de Melo } 58004391debSArnaldo Carvalho de Melo 58175562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 58275562573SAdrian Hunter union perf_event *event, u64 *id) 58375562573SAdrian Hunter { 58475562573SAdrian Hunter const u64 *array = event->sample.array; 58575562573SAdrian Hunter ssize_t n; 58675562573SAdrian Hunter 58775562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 58875562573SAdrian Hunter 58975562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 59075562573SAdrian Hunter if (evlist->id_pos >= n) 59175562573SAdrian Hunter return -1; 59275562573SAdrian Hunter *id = array[evlist->id_pos]; 59375562573SAdrian Hunter } else { 59475562573SAdrian Hunter if (evlist->is_pos > n) 59575562573SAdrian Hunter return -1; 59675562573SAdrian Hunter n -= evlist->is_pos; 59775562573SAdrian Hunter *id = array[n]; 59875562573SAdrian Hunter } 59975562573SAdrian Hunter return 0; 60075562573SAdrian Hunter } 60175562573SAdrian Hunter 60275562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 60375562573SAdrian Hunter union perf_event *event) 60475562573SAdrian Hunter { 60598be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 60675562573SAdrian Hunter struct hlist_head *head; 60775562573SAdrian Hunter struct perf_sample_id *sid; 60875562573SAdrian Hunter int hash; 60975562573SAdrian Hunter u64 id; 61075562573SAdrian Hunter 61175562573SAdrian Hunter if (evlist->nr_entries == 1) 61298be6966SAdrian Hunter return first; 61398be6966SAdrian Hunter 61498be6966SAdrian Hunter if (!first->attr.sample_id_all && 61598be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 61698be6966SAdrian Hunter return first; 61775562573SAdrian Hunter 61875562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 61975562573SAdrian Hunter return NULL; 62075562573SAdrian Hunter 62175562573SAdrian Hunter /* Synthesized events have an id of zero */ 62275562573SAdrian Hunter if (!id) 62398be6966SAdrian Hunter return first; 62475562573SAdrian Hunter 62575562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 62675562573SAdrian Hunter head = &evlist->heads[hash]; 62775562573SAdrian Hunter 62875562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 62975562573SAdrian Hunter if (sid->id == id) 63075562573SAdrian Hunter return sid->evsel; 63175562573SAdrian Hunter } 63275562573SAdrian Hunter return NULL; 63375562573SAdrian Hunter } 63475562573SAdrian Hunter 635aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 63604391debSArnaldo Carvalho de Melo { 637aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 63804391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 63904391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 64004391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 6418115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 64204391debSArnaldo Carvalho de Melo 6437bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 64404391debSArnaldo Carvalho de Melo /* 64504391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 64604391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 64704391debSArnaldo Carvalho de Melo * 64804391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 64904391debSArnaldo Carvalho de Melo * 65004391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 65104391debSArnaldo Carvalho de Melo */ 6527bb41152SArnaldo Carvalho de Melo int diff = head - old; 65304391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 65404391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 65504391debSArnaldo Carvalho de Melo 65604391debSArnaldo Carvalho de Melo /* 65704391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 65804391debSArnaldo Carvalho de Melo */ 65904391debSArnaldo Carvalho de Melo old = head; 66004391debSArnaldo Carvalho de Melo } 6617bb41152SArnaldo Carvalho de Melo } 66204391debSArnaldo Carvalho de Melo 66304391debSArnaldo Carvalho de Melo if (old != head) { 66404391debSArnaldo Carvalho de Melo size_t size; 66504391debSArnaldo Carvalho de Melo 6668115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 66704391debSArnaldo Carvalho de Melo size = event->header.size; 66804391debSArnaldo Carvalho de Melo 66904391debSArnaldo Carvalho de Melo /* 67004391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 67104391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 67204391debSArnaldo Carvalho de Melo */ 67304391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 67404391debSArnaldo Carvalho de Melo unsigned int offset = old; 67504391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 676a65cb4b9SJiri Olsa void *dst = md->event_copy; 67704391debSArnaldo Carvalho de Melo 67804391debSArnaldo Carvalho de Melo do { 67904391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 68004391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 68104391debSArnaldo Carvalho de Melo offset += cpy; 68204391debSArnaldo Carvalho de Melo dst += cpy; 68304391debSArnaldo Carvalho de Melo len -= cpy; 68404391debSArnaldo Carvalho de Melo } while (len); 68504391debSArnaldo Carvalho de Melo 686a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 68704391debSArnaldo Carvalho de Melo } 68804391debSArnaldo Carvalho de Melo 68904391debSArnaldo Carvalho de Melo old += size; 69004391debSArnaldo Carvalho de Melo } 69104391debSArnaldo Carvalho de Melo 69204391debSArnaldo Carvalho de Melo md->prev = old; 6937bb41152SArnaldo Carvalho de Melo 69404391debSArnaldo Carvalho de Melo return event; 69504391debSArnaldo Carvalho de Melo } 696f8a95309SArnaldo Carvalho de Melo 69782396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 69882396986SArnaldo Carvalho de Melo { 69982396986SArnaldo Carvalho de Melo return perf_mmap__read_head(md) != md->prev; 70082396986SArnaldo Carvalho de Melo } 70182396986SArnaldo Carvalho de Melo 70282396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx) 70382396986SArnaldo Carvalho de Melo { 70482396986SArnaldo Carvalho de Melo ++evlist->mmap[idx].refcnt; 70582396986SArnaldo Carvalho de Melo } 70682396986SArnaldo Carvalho de Melo 70782396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx) 70882396986SArnaldo Carvalho de Melo { 70982396986SArnaldo Carvalho de Melo BUG_ON(evlist->mmap[idx].refcnt == 0); 71082396986SArnaldo Carvalho de Melo 71182396986SArnaldo Carvalho de Melo if (--evlist->mmap[idx].refcnt == 0) 71282396986SArnaldo Carvalho de Melo __perf_evlist__munmap(evlist, idx); 71382396986SArnaldo Carvalho de Melo } 71482396986SArnaldo Carvalho de Melo 7158e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 7168e50d384SZhouyi Zhou { 7178e50d384SZhouyi Zhou struct perf_mmap *md = &evlist->mmap[idx]; 71882396986SArnaldo Carvalho de Melo 71982396986SArnaldo Carvalho de Melo if (!evlist->overwrite) { 7208e50d384SZhouyi Zhou unsigned int old = md->prev; 7218e50d384SZhouyi Zhou 7228e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 7238e50d384SZhouyi Zhou } 72482396986SArnaldo Carvalho de Melo 72582396986SArnaldo Carvalho de Melo if (md->refcnt == 1 && perf_mmap__empty(md)) 72682396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 7278e50d384SZhouyi Zhou } 7288e50d384SZhouyi Zhou 72993edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 73093edcbd9SAdrian Hunter { 73193edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 73293edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 73393edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 73482396986SArnaldo Carvalho de Melo evlist->mmap[idx].refcnt = 0; 73593edcbd9SAdrian Hunter } 73693edcbd9SAdrian Hunter } 73793edcbd9SAdrian Hunter 7387e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 739f8a95309SArnaldo Carvalho de Melo { 740aece948fSArnaldo Carvalho de Melo int i; 741f8a95309SArnaldo Carvalho de Melo 742983874d1SArnaldo Carvalho de Melo if (evlist->mmap == NULL) 743983874d1SArnaldo Carvalho de Melo return; 744983874d1SArnaldo Carvalho de Melo 74593edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 74693edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 747aece948fSArnaldo Carvalho de Melo 74804662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 749f8a95309SArnaldo Carvalho de Melo } 750f8a95309SArnaldo Carvalho de Melo 751806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 752f8a95309SArnaldo Carvalho de Melo { 753a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 754ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 755b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 756aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 757f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 758f8a95309SArnaldo Carvalho de Melo } 759f8a95309SArnaldo Carvalho de Melo 760a8a8f3ebSAdrian Hunter struct mmap_params { 761a8a8f3ebSAdrian Hunter int prot; 762a8a8f3ebSAdrian Hunter int mask; 763a8a8f3ebSAdrian Hunter }; 764a8a8f3ebSAdrian Hunter 765a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, 766a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 767f8a95309SArnaldo Carvalho de Melo { 76882396986SArnaldo Carvalho de Melo /* 76982396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 77082396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 77182396986SArnaldo Carvalho de Melo * the ring buffer. 77282396986SArnaldo Carvalho de Melo * 77382396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 77482396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 77582396986SArnaldo Carvalho de Melo * waiting to be consumed. 77682396986SArnaldo Carvalho de Melo * 77782396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 77882396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 77982396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 78082396986SArnaldo Carvalho de Melo */ 78182396986SArnaldo Carvalho de Melo evlist->mmap[idx].refcnt = 2; 782aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 783a8a8f3ebSAdrian Hunter evlist->mmap[idx].mask = mp->mask; 784a8a8f3ebSAdrian Hunter evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, 785f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 786301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 78702635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 78802635965SAdrian Hunter errno); 789301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 790f8a95309SArnaldo Carvalho de Melo return -1; 791301b195dSNelson Elhage } 792ad6765ddSArnaldo Carvalho de Melo 793f8a95309SArnaldo Carvalho de Melo return 0; 794f8a95309SArnaldo Carvalho de Melo } 795f8a95309SArnaldo Carvalho de Melo 79604e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 797a8a8f3ebSAdrian Hunter struct mmap_params *mp, int cpu, 798a8a8f3ebSAdrian Hunter int thread, int *output) 799aece948fSArnaldo Carvalho de Melo { 800aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 80104e21314SAdrian Hunter 8020050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 803bf8e8f4bSAdrian Hunter int fd; 804bf8e8f4bSAdrian Hunter 805bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 806bf8e8f4bSAdrian Hunter continue; 807bf8e8f4bSAdrian Hunter 808bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 80904e21314SAdrian Hunter 81004e21314SAdrian Hunter if (*output == -1) { 81104e21314SAdrian Hunter *output = fd; 812a8a8f3ebSAdrian Hunter if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) 81304e21314SAdrian Hunter return -1; 81404e21314SAdrian Hunter } else { 81504e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 81604e21314SAdrian Hunter return -1; 81782396986SArnaldo Carvalho de Melo 81882396986SArnaldo Carvalho de Melo perf_evlist__mmap_get(evlist, idx); 81904e21314SAdrian Hunter } 82004e21314SAdrian Hunter 821f90d194aSAdrian Hunter /* 822f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 823f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 824f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 825f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 826f90d194aSAdrian Hunter * Therefore don't add it for polling. 827f90d194aSAdrian Hunter */ 828f90d194aSAdrian Hunter if (!evsel->system_wide && 829f90d194aSAdrian Hunter __perf_evlist__add_pollfd(evlist, fd, idx) < 0) { 83082396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 831ad6765ddSArnaldo Carvalho de Melo return -1; 83282396986SArnaldo Carvalho de Melo } 833033fa713SArnaldo Carvalho de Melo 8343c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 8353c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 8363c659eedSAdrian Hunter fd) < 0) 83704e21314SAdrian Hunter return -1; 8383c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 8393c659eedSAdrian Hunter thread); 8403c659eedSAdrian Hunter } 84104e21314SAdrian Hunter } 84204e21314SAdrian Hunter 84304e21314SAdrian Hunter return 0; 84404e21314SAdrian Hunter } 84504e21314SAdrian Hunter 846a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 847a8a8f3ebSAdrian Hunter struct mmap_params *mp) 84804e21314SAdrian Hunter { 849aece948fSArnaldo Carvalho de Melo int cpu, thread; 850b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 851b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 852aece948fSArnaldo Carvalho de Melo 853e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 854b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 855aece948fSArnaldo Carvalho de Melo int output = -1; 856aece948fSArnaldo Carvalho de Melo 857b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 858a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 859a8a8f3ebSAdrian Hunter thread, &output)) 860aece948fSArnaldo Carvalho de Melo goto out_unmap; 861aece948fSArnaldo Carvalho de Melo } 862aece948fSArnaldo Carvalho de Melo } 863aece948fSArnaldo Carvalho de Melo 864aece948fSArnaldo Carvalho de Melo return 0; 865aece948fSArnaldo Carvalho de Melo 866aece948fSArnaldo Carvalho de Melo out_unmap: 86793edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 86893edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 869aece948fSArnaldo Carvalho de Melo return -1; 870aece948fSArnaldo Carvalho de Melo } 871aece948fSArnaldo Carvalho de Melo 872a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 873a8a8f3ebSAdrian Hunter struct mmap_params *mp) 874aece948fSArnaldo Carvalho de Melo { 875aece948fSArnaldo Carvalho de Melo int thread; 876b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 877aece948fSArnaldo Carvalho de Melo 878e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 879b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 880aece948fSArnaldo Carvalho de Melo int output = -1; 881aece948fSArnaldo Carvalho de Melo 882a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 883a8a8f3ebSAdrian Hunter &output)) 884aece948fSArnaldo Carvalho de Melo goto out_unmap; 885aece948fSArnaldo Carvalho de Melo } 886aece948fSArnaldo Carvalho de Melo 887aece948fSArnaldo Carvalho de Melo return 0; 888aece948fSArnaldo Carvalho de Melo 889aece948fSArnaldo Carvalho de Melo out_unmap: 89093edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 89193edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 892aece948fSArnaldo Carvalho de Melo return -1; 893aece948fSArnaldo Carvalho de Melo } 894aece948fSArnaldo Carvalho de Melo 895994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages) 896994a1f78SJiri Olsa { 8978185e881SArnaldo Carvalho de Melo if (pages == UINT_MAX) { 8988185e881SArnaldo Carvalho de Melo int max; 8998185e881SArnaldo Carvalho de Melo 9008185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 9018185e881SArnaldo Carvalho de Melo /* 9028185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 9038185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 9048185e881SArnaldo Carvalho de Melo * die yet... 9058185e881SArnaldo Carvalho de Melo */ 9068185e881SArnaldo Carvalho de Melo max = 512; 9078185e881SArnaldo Carvalho de Melo } else { 9088185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 9098185e881SArnaldo Carvalho de Melo } 9108185e881SArnaldo Carvalho de Melo 9118185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 9121be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 9131be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 9148185e881SArnaldo Carvalho de Melo } else if (!is_power_of_2(pages)) 915994a1f78SJiri Olsa return 0; 916994a1f78SJiri Olsa 917994a1f78SJiri Olsa return (pages + 1) * page_size; 918994a1f78SJiri Olsa } 919994a1f78SJiri Olsa 92033c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 92133c2dcfdSDavid Ahern unsigned long max) 922994a1f78SJiri Olsa { 9232fbe4abeSAdrian Hunter unsigned long pages, val; 92427050f53SJiri Olsa static struct parse_tag tags[] = { 92527050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 92627050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 92727050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 92827050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 92927050f53SJiri Olsa { .tag = 0 }, 93027050f53SJiri Olsa }; 931994a1f78SJiri Olsa 9328973504bSDavid Ahern if (str == NULL) 93333c2dcfdSDavid Ahern return -EINVAL; 9348973504bSDavid Ahern 93527050f53SJiri Olsa val = parse_tag_value(str, tags); 9362fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 93727050f53SJiri Olsa /* we got file size value */ 93827050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 93927050f53SJiri Olsa } else { 94027050f53SJiri Olsa /* we got pages count value */ 94127050f53SJiri Olsa char *eptr; 942994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 94333c2dcfdSDavid Ahern if (*eptr != '\0') 94433c2dcfdSDavid Ahern return -EINVAL; 94527050f53SJiri Olsa } 946994a1f78SJiri Olsa 9472bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 94833c2dcfdSDavid Ahern /* leave number of pages at 0 */ 9491dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 95033c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 95191529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 9521dbfa938SAdrian Hunter if (!pages) 9531dbfa938SAdrian Hunter return -EINVAL; 9549639837eSDavid Ahern pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 9559639837eSDavid Ahern pages * page_size, pages); 9562fbe4abeSAdrian Hunter } 9572fbe4abeSAdrian Hunter 95833c2dcfdSDavid Ahern if (pages > max) 95933c2dcfdSDavid Ahern return -EINVAL; 96033c2dcfdSDavid Ahern 96133c2dcfdSDavid Ahern return pages; 96233c2dcfdSDavid Ahern } 96333c2dcfdSDavid Ahern 96433c2dcfdSDavid Ahern int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 96533c2dcfdSDavid Ahern int unset __maybe_unused) 96633c2dcfdSDavid Ahern { 96733c2dcfdSDavid Ahern unsigned int *mmap_pages = opt->value; 96833c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 96933c2dcfdSDavid Ahern long pages; 97033c2dcfdSDavid Ahern 971f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 97233c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 97333c2dcfdSDavid Ahern 97433c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 97533c2dcfdSDavid Ahern if (pages < 0) { 97633c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 977994a1f78SJiri Olsa return -1; 978994a1f78SJiri Olsa } 979994a1f78SJiri Olsa 980994a1f78SJiri Olsa *mmap_pages = pages; 981994a1f78SJiri Olsa return 0; 982994a1f78SJiri Olsa } 983994a1f78SJiri Olsa 984c83fa7f2SAdrian Hunter /** 985c83fa7f2SAdrian Hunter * perf_evlist__mmap - Create mmaps to receive events. 986c83fa7f2SAdrian Hunter * @evlist: list of events 987c83fa7f2SAdrian Hunter * @pages: map length in pages 988c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 989f8a95309SArnaldo Carvalho de Melo * 990c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 991c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 992c83fa7f2SAdrian Hunter * automatically. 993f8a95309SArnaldo Carvalho de Melo * 994c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 995f8a95309SArnaldo Carvalho de Melo */ 99650a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 99750a682ceSArnaldo Carvalho de Melo bool overwrite) 998f8a95309SArnaldo Carvalho de Melo { 999aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 10007e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 10017e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1002a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1003a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1004a8a8f3ebSAdrian Hunter }; 100550a682ceSArnaldo Carvalho de Melo 10067e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 1007f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1008f8a95309SArnaldo Carvalho de Melo 10091b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1010f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1011f8a95309SArnaldo Carvalho de Melo 1012f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1013994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 10142af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1015a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1016f8a95309SArnaldo Carvalho de Melo 10170050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1018f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1019a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1020a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1021f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1022f8a95309SArnaldo Carvalho de Melo } 1023f8a95309SArnaldo Carvalho de Melo 1024ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1025a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1026f8a95309SArnaldo Carvalho de Melo 1027a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1028f8a95309SArnaldo Carvalho de Melo } 10297e2ed097SArnaldo Carvalho de Melo 1030602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 10317e2ed097SArnaldo Carvalho de Melo { 1032b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 1033b809ac10SNamhyung Kim target->uid); 10347e2ed097SArnaldo Carvalho de Melo 10357e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 10367e2ed097SArnaldo Carvalho de Melo return -1; 10377e2ed097SArnaldo Carvalho de Melo 10389c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 1039d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 1040879d77d0SNamhyung Kim else 1041879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 10427e2ed097SArnaldo Carvalho de Melo 10437e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 10447e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 10457e2ed097SArnaldo Carvalho de Melo 10467e2ed097SArnaldo Carvalho de Melo return 0; 10477e2ed097SArnaldo Carvalho de Melo 10487e2ed097SArnaldo Carvalho de Melo out_delete_threads: 10497e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 1050b2e19a93SYasser Shalabi evlist->threads = NULL; 10517e2ed097SArnaldo Carvalho de Melo return -1; 10527e2ed097SArnaldo Carvalho de Melo } 10537e2ed097SArnaldo Carvalho de Melo 10541491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist) 10550a102479SFrederic Weisbecker { 10560a102479SFrederic Weisbecker struct perf_evsel *evsel; 1057745cefc5SArnaldo Carvalho de Melo int err = 0; 1058745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1059b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 10600a102479SFrederic Weisbecker 10610050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1062745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 10630a102479SFrederic Weisbecker continue; 1064745cefc5SArnaldo Carvalho de Melo 1065745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 10660a102479SFrederic Weisbecker if (err) 1067745cefc5SArnaldo Carvalho de Melo break; 10680a102479SFrederic Weisbecker } 10690a102479SFrederic Weisbecker 1070745cefc5SArnaldo Carvalho de Melo return err; 1071745cefc5SArnaldo Carvalho de Melo } 1072745cefc5SArnaldo Carvalho de Melo 1073745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1074745cefc5SArnaldo Carvalho de Melo { 1075745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1076745cefc5SArnaldo Carvalho de Melo int err = 0; 1077745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1078b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 1079745cefc5SArnaldo Carvalho de Melo 10800050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1081745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 1082745cefc5SArnaldo Carvalho de Melo if (err) 1083745cefc5SArnaldo Carvalho de Melo break; 1084745cefc5SArnaldo Carvalho de Melo } 1085745cefc5SArnaldo Carvalho de Melo 1086745cefc5SArnaldo Carvalho de Melo return err; 10870a102479SFrederic Weisbecker } 108874429964SFrederic Weisbecker 10890c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 109074429964SFrederic Weisbecker { 109175562573SAdrian Hunter struct perf_evsel *pos; 1092c2a70653SArnaldo Carvalho de Melo 109375562573SAdrian Hunter if (evlist->nr_entries == 1) 109475562573SAdrian Hunter return true; 109575562573SAdrian Hunter 109675562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 109775562573SAdrian Hunter return false; 109875562573SAdrian Hunter 10990050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 110075562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 110175562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1102c2a70653SArnaldo Carvalho de Melo return false; 110374429964SFrederic Weisbecker } 110474429964SFrederic Weisbecker 1105c2a70653SArnaldo Carvalho de Melo return true; 1106c2a70653SArnaldo Carvalho de Melo } 1107c2a70653SArnaldo Carvalho de Melo 110875562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1109c2a70653SArnaldo Carvalho de Melo { 111075562573SAdrian Hunter struct perf_evsel *evsel; 111175562573SAdrian Hunter 111275562573SAdrian Hunter if (evlist->combined_sample_type) 111375562573SAdrian Hunter return evlist->combined_sample_type; 111475562573SAdrian Hunter 11150050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 111675562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 111775562573SAdrian Hunter 111875562573SAdrian Hunter return evlist->combined_sample_type; 111975562573SAdrian Hunter } 112075562573SAdrian Hunter 112175562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 112275562573SAdrian Hunter { 112375562573SAdrian Hunter evlist->combined_sample_type = 0; 112475562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1125c2a70653SArnaldo Carvalho de Melo } 1126c2a70653SArnaldo Carvalho de Melo 11279ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 11289ede473cSJiri Olsa { 11299ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 11309ede473cSJiri Olsa u64 read_format = first->attr.read_format; 11319ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 11329ede473cSJiri Olsa 11330050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 11349ede473cSJiri Olsa if (read_format != pos->attr.read_format) 11359ede473cSJiri Olsa return false; 11369ede473cSJiri Olsa } 11379ede473cSJiri Olsa 11389ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 11399ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 11409ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 11419ede473cSJiri Olsa return false; 11429ede473cSJiri Olsa } 11439ede473cSJiri Olsa 11449ede473cSJiri Olsa return true; 11459ede473cSJiri Olsa } 11469ede473cSJiri Olsa 11479ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 11489ede473cSJiri Olsa { 11499ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 11509ede473cSJiri Olsa return first->attr.read_format; 11519ede473cSJiri Olsa } 11529ede473cSJiri Olsa 11530c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 115481e36bffSArnaldo Carvalho de Melo { 11550c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 115681e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 115781e36bffSArnaldo Carvalho de Melo u64 sample_type; 115881e36bffSArnaldo Carvalho de Melo u16 size = 0; 115981e36bffSArnaldo Carvalho de Melo 116081e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 116181e36bffSArnaldo Carvalho de Melo goto out; 116281e36bffSArnaldo Carvalho de Melo 116381e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 116481e36bffSArnaldo Carvalho de Melo 116581e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 116681e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 116781e36bffSArnaldo Carvalho de Melo 116881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 116981e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 117081e36bffSArnaldo Carvalho de Melo 117181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 117281e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 117381e36bffSArnaldo Carvalho de Melo 117481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 117581e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 117681e36bffSArnaldo Carvalho de Melo 117781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 117881e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 117975562573SAdrian Hunter 118075562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 118175562573SAdrian Hunter size += sizeof(data->id); 118281e36bffSArnaldo Carvalho de Melo out: 118381e36bffSArnaldo Carvalho de Melo return size; 118481e36bffSArnaldo Carvalho de Melo } 118581e36bffSArnaldo Carvalho de Melo 11860c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1187c2a70653SArnaldo Carvalho de Melo { 11880c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1189c2a70653SArnaldo Carvalho de Melo 11900050f7aaSArnaldo Carvalho de Melo evlist__for_each_continue(evlist, pos) { 1191c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1192c2a70653SArnaldo Carvalho de Melo return false; 1193c2a70653SArnaldo Carvalho de Melo } 1194c2a70653SArnaldo Carvalho de Melo 1195c2a70653SArnaldo Carvalho de Melo return true; 119674429964SFrederic Weisbecker } 119774429964SFrederic Weisbecker 11980c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 119974429964SFrederic Weisbecker { 12000c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1201c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 120274429964SFrederic Weisbecker } 120381cce8deSArnaldo Carvalho de Melo 120481cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 120581cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 120681cce8deSArnaldo Carvalho de Melo { 120781cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 120881cce8deSArnaldo Carvalho de Melo } 1209727ab04eSArnaldo Carvalho de Melo 1210a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1211a74b4b66SNamhyung Kim { 1212a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1213a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1214a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 12158ad9219eSStephane Eranian int n; 1216a74b4b66SNamhyung Kim 12178ad9219eSStephane Eranian evlist__for_each_reverse(evlist, evsel) { 12188ad9219eSStephane Eranian n = evsel->cpus ? evsel->cpus->nr : ncpus; 12198ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 12208ad9219eSStephane Eranian } 1221a74b4b66SNamhyung Kim } 1222a74b4b66SNamhyung Kim 12234112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 12244112eb18SArnaldo Carvalho de Melo { 12254112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 12264112eb18SArnaldo Carvalho de Melo 12274112eb18SArnaldo Carvalho de Melo /* 12284112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 12294112eb18SArnaldo Carvalho de Melo * an all cpus map. 12304112eb18SArnaldo Carvalho de Melo * 12314112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 12324112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 12334112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 12344112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 12354112eb18SArnaldo Carvalho de Melo */ 12364112eb18SArnaldo Carvalho de Melo evlist->cpus = cpu_map__new(NULL); 12374112eb18SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 12384112eb18SArnaldo Carvalho de Melo goto out; 12394112eb18SArnaldo Carvalho de Melo 12404112eb18SArnaldo Carvalho de Melo evlist->threads = thread_map__new_dummy(); 12414112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL) 12424112eb18SArnaldo Carvalho de Melo goto out_free_cpus; 12434112eb18SArnaldo Carvalho de Melo 12444112eb18SArnaldo Carvalho de Melo err = 0; 12454112eb18SArnaldo Carvalho de Melo out: 12464112eb18SArnaldo Carvalho de Melo return err; 12474112eb18SArnaldo Carvalho de Melo out_free_cpus: 12484112eb18SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 12494112eb18SArnaldo Carvalho de Melo evlist->cpus = NULL; 12504112eb18SArnaldo Carvalho de Melo goto out; 12514112eb18SArnaldo Carvalho de Melo } 12524112eb18SArnaldo Carvalho de Melo 12536a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1254727ab04eSArnaldo Carvalho de Melo { 12556a4bb04cSJiri Olsa struct perf_evsel *evsel; 1256a74b4b66SNamhyung Kim int err; 1257727ab04eSArnaldo Carvalho de Melo 12584112eb18SArnaldo Carvalho de Melo /* 12594112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 12604112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 12614112eb18SArnaldo Carvalho de Melo */ 12624112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 12634112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 12644112eb18SArnaldo Carvalho de Melo if (err < 0) 12654112eb18SArnaldo Carvalho de Melo goto out_err; 12664112eb18SArnaldo Carvalho de Melo } 12674112eb18SArnaldo Carvalho de Melo 1268733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1269733cd2feSAdrian Hunter 12700050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 12716a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 1272727ab04eSArnaldo Carvalho de Melo if (err < 0) 1273727ab04eSArnaldo Carvalho de Melo goto out_err; 1274727ab04eSArnaldo Carvalho de Melo } 1275727ab04eSArnaldo Carvalho de Melo 1276727ab04eSArnaldo Carvalho de Melo return 0; 1277727ab04eSArnaldo Carvalho de Melo out_err: 1278a74b4b66SNamhyung Kim perf_evlist__close(evlist); 127941c21a68SNamhyung Kim errno = -err; 1280727ab04eSArnaldo Carvalho de Melo return err; 1281727ab04eSArnaldo Carvalho de Melo } 128235b9d88eSArnaldo Carvalho de Melo 1283602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 128455e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1285735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 128635b9d88eSArnaldo Carvalho de Melo { 128735b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 128835b9d88eSArnaldo Carvalho de Melo char bf; 128935b9d88eSArnaldo Carvalho de Melo 129035b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 129135b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 129235b9d88eSArnaldo Carvalho de Melo return -1; 129335b9d88eSArnaldo Carvalho de Melo } 129435b9d88eSArnaldo Carvalho de Melo 129535b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 129635b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 129735b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 129835b9d88eSArnaldo Carvalho de Melo } 129935b9d88eSArnaldo Carvalho de Melo 130035b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 130135b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 130235b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 130335b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 130435b9d88eSArnaldo Carvalho de Melo } 130535b9d88eSArnaldo Carvalho de Melo 130635b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13075f1c4225SArnaldo Carvalho de Melo int ret; 13085f1c4225SArnaldo Carvalho de Melo 1309119fa3c9SNamhyung Kim if (pipe_output) 131035b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 131135b9d88eSArnaldo Carvalho de Melo 13120817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13130817df08SDavid Ahern 131435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 131535b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 131635b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 131735b9d88eSArnaldo Carvalho de Melo 131835b9d88eSArnaldo Carvalho de Melo /* 131935b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 132035b9d88eSArnaldo Carvalho de Melo */ 132135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 132235b9d88eSArnaldo Carvalho de Melo 132335b9d88eSArnaldo Carvalho de Melo /* 132435b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 132535b9d88eSArnaldo Carvalho de Melo */ 13265f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 13275f1c4225SArnaldo Carvalho de Melo /* 13285f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 13295f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 13305f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 13315f1c4225SArnaldo Carvalho de Melo * 13325f1c4225SArnaldo Carvalho de Melo * For cancelling the workload without actuallin running it, 13335f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 13345f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 13355f1c4225SArnaldo Carvalho de Melo * here. 13365f1c4225SArnaldo Carvalho de Melo */ 13375f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 13385f1c4225SArnaldo Carvalho de Melo if (ret == -1) 133935b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 13405f1c4225SArnaldo Carvalho de Melo exit(ret); 13415f1c4225SArnaldo Carvalho de Melo } 134235b9d88eSArnaldo Carvalho de Melo 134335b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 134435b9d88eSArnaldo Carvalho de Melo 1345735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1346f33cbe72SArnaldo Carvalho de Melo union sigval val; 1347f33cbe72SArnaldo Carvalho de Melo 1348f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1349f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 135035b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1351f33cbe72SArnaldo Carvalho de Melo } else 1352f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 135335b9d88eSArnaldo Carvalho de Melo exit(-1); 135435b9d88eSArnaldo Carvalho de Melo } 135535b9d88eSArnaldo Carvalho de Melo 1356735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1357735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1358735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1359735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1360735f7e0bSArnaldo Carvalho de Melo }; 1361735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1362735f7e0bSArnaldo Carvalho de Melo } 1363735f7e0bSArnaldo Carvalho de Melo 13641aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 13651aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 13661aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 13671aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 13681aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 13691aaf63b1SArnaldo Carvalho de Melo } 137035b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 13711aaf63b1SArnaldo Carvalho de Melo } 137235b9d88eSArnaldo Carvalho de Melo 137335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 137435b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 137535b9d88eSArnaldo Carvalho de Melo /* 137635b9d88eSArnaldo Carvalho de Melo * wait for child to settle 137735b9d88eSArnaldo Carvalho de Melo */ 137835b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 137935b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 138035b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 138135b9d88eSArnaldo Carvalho de Melo } 138235b9d88eSArnaldo Carvalho de Melo 1383bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 138435b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 138535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 138635b9d88eSArnaldo Carvalho de Melo return 0; 138735b9d88eSArnaldo Carvalho de Melo 138835b9d88eSArnaldo Carvalho de Melo out_close_pipes: 138935b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 139035b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 139135b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 139235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 139335b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 139435b9d88eSArnaldo Carvalho de Melo return -1; 139535b9d88eSArnaldo Carvalho de Melo } 139635b9d88eSArnaldo Carvalho de Melo 139735b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 139835b9d88eSArnaldo Carvalho de Melo { 139935b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1400b3824404SDavid Ahern char bf = 0; 1401bcf3145fSNamhyung Kim int ret; 140235b9d88eSArnaldo Carvalho de Melo /* 140335b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 140435b9d88eSArnaldo Carvalho de Melo */ 1405bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1406bcf3145fSNamhyung Kim if (ret < 0) 1407bcf3145fSNamhyung Kim perror("enable to write to pipe"); 1408bcf3145fSNamhyung Kim 1409bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1410bcf3145fSNamhyung Kim return ret; 141135b9d88eSArnaldo Carvalho de Melo } 141235b9d88eSArnaldo Carvalho de Melo 141335b9d88eSArnaldo Carvalho de Melo return 0; 141435b9d88eSArnaldo Carvalho de Melo } 1415cb0b29e0SArnaldo Carvalho de Melo 1416a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 14170807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1418cb0b29e0SArnaldo Carvalho de Melo { 141975562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 142075562573SAdrian Hunter 142175562573SAdrian Hunter if (!evsel) 142275562573SAdrian Hunter return -EFAULT; 14230807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1424cb0b29e0SArnaldo Carvalho de Melo } 142578f067b3SArnaldo Carvalho de Melo 142678f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 142778f067b3SArnaldo Carvalho de Melo { 142878f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 142978f067b3SArnaldo Carvalho de Melo size_t printed = 0; 143078f067b3SArnaldo Carvalho de Melo 14310050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 143278f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 143378f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 143478f067b3SArnaldo Carvalho de Melo } 143578f067b3SArnaldo Carvalho de Melo 1436b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 143778f067b3SArnaldo Carvalho de Melo } 14386ef068cbSArnaldo Carvalho de Melo 14396ef068cbSArnaldo Carvalho de Melo int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused, 14406ef068cbSArnaldo Carvalho de Melo int err, char *buf, size_t size) 14416ef068cbSArnaldo Carvalho de Melo { 14426ef068cbSArnaldo Carvalho de Melo char sbuf[128]; 14436ef068cbSArnaldo Carvalho de Melo 14446ef068cbSArnaldo Carvalho de Melo switch (err) { 14456ef068cbSArnaldo Carvalho de Melo case ENOENT: 14466ef068cbSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", 14476ef068cbSArnaldo Carvalho de Melo "Error:\tUnable to find debugfs\n" 14486ef068cbSArnaldo Carvalho de Melo "Hint:\tWas your kernel was compiled with debugfs support?\n" 14496ef068cbSArnaldo Carvalho de Melo "Hint:\tIs the debugfs filesystem mounted?\n" 14506ef068cbSArnaldo Carvalho de Melo "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'"); 14516ef068cbSArnaldo Carvalho de Melo break; 14526ef068cbSArnaldo Carvalho de Melo case EACCES: 14536ef068cbSArnaldo Carvalho de Melo scnprintf(buf, size, 14546ef068cbSArnaldo Carvalho de Melo "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n" 14556ef068cbSArnaldo Carvalho de Melo "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", 14566ef068cbSArnaldo Carvalho de Melo debugfs_mountpoint, debugfs_mountpoint); 14576ef068cbSArnaldo Carvalho de Melo break; 14586ef068cbSArnaldo Carvalho de Melo default: 14596ef068cbSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf))); 14606ef068cbSArnaldo Carvalho de Melo break; 14616ef068cbSArnaldo Carvalho de Melo } 14626ef068cbSArnaldo Carvalho de Melo 14636ef068cbSArnaldo Carvalho de Melo return 0; 14646ef068cbSArnaldo Carvalho de Melo } 1465a8f23d8fSArnaldo Carvalho de Melo 1466a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, 1467a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1468a8f23d8fSArnaldo Carvalho de Melo { 1469a8f23d8fSArnaldo Carvalho de Melo int printed, value; 14706e81c74cSMasami Hiramatsu char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1471a8f23d8fSArnaldo Carvalho de Melo 1472a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1473a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1474a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1475a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1476a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1477a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1478a8f23d8fSArnaldo Carvalho de Melo 14791a47245dSAdrian Hunter value = perf_event_paranoid(); 1480a8f23d8fSArnaldo Carvalho de Melo 1481a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1482a8f23d8fSArnaldo Carvalho de Melo 1483a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1484a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1485a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1486a8f23d8fSArnaldo Carvalho de Melo } 1487a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14885229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1489a8f23d8fSArnaldo Carvalho de Melo 1490a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14915229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 14925229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1493a8f23d8fSArnaldo Carvalho de Melo break; 1494a8f23d8fSArnaldo Carvalho de Melo default: 1495a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1496a8f23d8fSArnaldo Carvalho de Melo break; 1497a8f23d8fSArnaldo Carvalho de Melo } 1498a8f23d8fSArnaldo Carvalho de Melo 1499a8f23d8fSArnaldo Carvalho de Melo return 0; 1500a8f23d8fSArnaldo Carvalho de Melo } 1501a025e4f0SAdrian Hunter 1502956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1503956fa571SArnaldo Carvalho de Melo { 1504956fa571SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1505e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1506956fa571SArnaldo Carvalho de Melo 1507956fa571SArnaldo Carvalho de Melo switch (err) { 1508956fa571SArnaldo Carvalho de Melo case EPERM: 1509e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1510e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1511e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1512956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1513e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1514e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1515e965bea1SArnaldo Carvalho de Melo 1516e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1517e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1518e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1519e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1520e965bea1SArnaldo Carvalho de Melo } 1521e965bea1SArnaldo Carvalho de Melo 1522e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1523e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1524956fa571SArnaldo Carvalho de Melo break; 1525956fa571SArnaldo Carvalho de Melo default: 1526956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1527956fa571SArnaldo Carvalho de Melo break; 1528956fa571SArnaldo Carvalho de Melo } 1529956fa571SArnaldo Carvalho de Melo 1530956fa571SArnaldo Carvalho de Melo return 0; 1531956fa571SArnaldo Carvalho de Melo } 1532956fa571SArnaldo Carvalho de Melo 1533a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1534a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1535a025e4f0SAdrian Hunter { 1536a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1537a025e4f0SAdrian Hunter LIST_HEAD(move); 1538a025e4f0SAdrian Hunter 1539a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1540a025e4f0SAdrian Hunter return; 1541a025e4f0SAdrian Hunter 15420050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, evsel) { 1543a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1544a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1545a025e4f0SAdrian Hunter } 1546a025e4f0SAdrian Hunter 1547a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1548a025e4f0SAdrian Hunter } 154960b0896cSAdrian Hunter 155060b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 155160b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 155260b0896cSAdrian Hunter { 155360b0896cSAdrian Hunter struct perf_evsel *evsel; 155460b0896cSAdrian Hunter 155560b0896cSAdrian Hunter if (tracking_evsel->tracking) 155660b0896cSAdrian Hunter return; 155760b0896cSAdrian Hunter 155860b0896cSAdrian Hunter evlist__for_each(evlist, evsel) { 155960b0896cSAdrian Hunter if (evsel != tracking_evsel) 156060b0896cSAdrian Hunter evsel->tracking = false; 156160b0896cSAdrian Hunter } 156260b0896cSAdrian Hunter 156360b0896cSAdrian Hunter tracking_evsel->tracking = true; 156460b0896cSAdrian Hunter } 1565