1f8a95309SArnaldo Carvalho de Melo /* 2f8a95309SArnaldo Carvalho de Melo * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3f8a95309SArnaldo Carvalho de Melo * 4f8a95309SArnaldo Carvalho de Melo * Parts came from builtin-{top,stat,record}.c, see those files for further 5f8a95309SArnaldo Carvalho de Melo * copyright notes. 6f8a95309SArnaldo Carvalho de Melo * 7f8a95309SArnaldo Carvalho de Melo * Released under the GPL v2. (and only v2, not any later version) 8f8a95309SArnaldo Carvalho de Melo */ 9a8c9ae18SArnaldo Carvalho de Melo #include "util.h" 10956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h> 115c581041SArnaldo Carvalho de Melo #include <poll.h> 12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h" 13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h" 1412864b31SNamhyung Kim #include "target.h" 15361c99a6SArnaldo Carvalho de Melo #include "evlist.h" 16361c99a6SArnaldo Carvalho de Melo #include "evsel.h" 17e3e1a54fSAdrian Hunter #include "debug.h" 1835b9d88eSArnaldo Carvalho de Melo #include <unistd.h> 19361c99a6SArnaldo Carvalho de Melo 2050d08e47SArnaldo Carvalho de Melo #include "parse-events.h" 21994a1f78SJiri Olsa #include "parse-options.h" 2250d08e47SArnaldo Carvalho de Melo 23f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 24f8a95309SArnaldo Carvalho de Melo 2570db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2670db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 270389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 2870db7533SArnaldo Carvalho de Melo 29e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx); 30e4b356b5SArnaldo Carvalho de Melo static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx); 31e4b356b5SArnaldo Carvalho de Melo 32f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 33a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 34f8a95309SArnaldo Carvalho de Melo 357e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 367e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 37361c99a6SArnaldo Carvalho de Melo { 3870db7533SArnaldo Carvalho de Melo int i; 3970db7533SArnaldo Carvalho de Melo 4070db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 4170db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 42361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 437e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 441b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 4535b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 46361c99a6SArnaldo Carvalho de Melo } 47361c99a6SArnaldo Carvalho de Melo 48334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 49ef1d1af2SArnaldo Carvalho de Melo { 50ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 51ef1d1af2SArnaldo Carvalho de Melo 52ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 53334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 54ef1d1af2SArnaldo Carvalho de Melo 55361c99a6SArnaldo Carvalho de Melo return evlist; 56361c99a6SArnaldo Carvalho de Melo } 57361c99a6SArnaldo Carvalho de Melo 58b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 59b22d54b0SJiri Olsa { 60b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 61b22d54b0SJiri Olsa 62b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 63b22d54b0SJiri Olsa perf_evlist__delete(evlist); 64b22d54b0SJiri Olsa evlist = NULL; 65b22d54b0SJiri Olsa } 66b22d54b0SJiri Olsa 67b22d54b0SJiri Olsa return evlist; 68b22d54b0SJiri Olsa } 69b22d54b0SJiri Olsa 7075562573SAdrian Hunter /** 7175562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 7275562573SAdrian Hunter * @evlist: selected event list 7375562573SAdrian Hunter * 7475562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 7575562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 7675562573SAdrian Hunter */ 7775562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 7875562573SAdrian Hunter { 7975562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 8075562573SAdrian Hunter 8175562573SAdrian Hunter evlist->id_pos = first->id_pos; 8275562573SAdrian Hunter evlist->is_pos = first->is_pos; 8375562573SAdrian Hunter } 8475562573SAdrian Hunter 85733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 86733cd2feSAdrian Hunter { 87733cd2feSAdrian Hunter struct perf_evsel *evsel; 88733cd2feSAdrian Hunter 890050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 90733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 91733cd2feSAdrian Hunter 92733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 93733cd2feSAdrian Hunter } 94733cd2feSAdrian Hunter 95361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 96361c99a6SArnaldo Carvalho de Melo { 97361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 98361c99a6SArnaldo Carvalho de Melo 990050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, pos) { 100361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 101361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 102361c99a6SArnaldo Carvalho de Melo } 103361c99a6SArnaldo Carvalho de Melo 104361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 105361c99a6SArnaldo Carvalho de Melo } 106361c99a6SArnaldo Carvalho de Melo 107ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 108ef1d1af2SArnaldo Carvalho de Melo { 10904662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1101b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 111ef1d1af2SArnaldo Carvalho de Melo } 112ef1d1af2SArnaldo Carvalho de Melo 113361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 114361c99a6SArnaldo Carvalho de Melo { 115983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 116f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 11703ad9747SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 11803ad9747SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 11903ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 12003ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 121361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 122ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 123361c99a6SArnaldo Carvalho de Melo free(evlist); 124361c99a6SArnaldo Carvalho de Melo } 125361c99a6SArnaldo Carvalho de Melo 126361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 127361c99a6SArnaldo Carvalho de Melo { 128361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 129ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 13060b0896cSAdrian Hunter entry->tracking = !entry->idx; 131ef503831SArnaldo Carvalho de Melo 13275562573SAdrian Hunter if (!evlist->nr_entries++) 13375562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 134361c99a6SArnaldo Carvalho de Melo } 135361c99a6SArnaldo Carvalho de Melo 1360529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 13750d08e47SArnaldo Carvalho de Melo struct list_head *list, 13850d08e47SArnaldo Carvalho de Melo int nr_entries) 13950d08e47SArnaldo Carvalho de Melo { 14075562573SAdrian Hunter bool set_id_pos = !evlist->nr_entries; 14175562573SAdrian Hunter 14250d08e47SArnaldo Carvalho de Melo list_splice_tail(list, &evlist->entries); 14350d08e47SArnaldo Carvalho de Melo evlist->nr_entries += nr_entries; 14475562573SAdrian Hunter if (set_id_pos) 14575562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 14650d08e47SArnaldo Carvalho de Melo } 14750d08e47SArnaldo Carvalho de Melo 14863dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 14963dab225SArnaldo Carvalho de Melo { 15063dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 15163dab225SArnaldo Carvalho de Melo 15263dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 15397f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 15497f63e4aSNamhyung Kim 15597f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 15663dab225SArnaldo Carvalho de Melo 1570050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 15863dab225SArnaldo Carvalho de Melo evsel->leader = leader; 15963dab225SArnaldo Carvalho de Melo } 16063dab225SArnaldo Carvalho de Melo } 16163dab225SArnaldo Carvalho de Melo 16263dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 1636a4bb04cSJiri Olsa { 16497f63e4aSNamhyung Kim if (evlist->nr_entries) { 16597f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 16663dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 1676a4bb04cSJiri Olsa } 16897f63e4aSNamhyung Kim } 1696a4bb04cSJiri Olsa 170361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 171361c99a6SArnaldo Carvalho de Melo { 172361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 173361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 174361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 175361c99a6SArnaldo Carvalho de Melo }; 1761aed2671SJoerg Roedel struct perf_evsel *evsel; 177361c99a6SArnaldo Carvalho de Melo 1781aed2671SJoerg Roedel event_attr_init(&attr); 1791aed2671SJoerg Roedel 180ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 181361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 182cc2d86b0SStephane Eranian goto error; 183cc2d86b0SStephane Eranian 184cc2d86b0SStephane Eranian /* use strdup() because free(evsel) assumes name is allocated */ 185cc2d86b0SStephane Eranian evsel->name = strdup("cycles"); 186cc2d86b0SStephane Eranian if (!evsel->name) 187cc2d86b0SStephane Eranian goto error_free; 188361c99a6SArnaldo Carvalho de Melo 189361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 190361c99a6SArnaldo Carvalho de Melo return 0; 191cc2d86b0SStephane Eranian error_free: 192cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 193cc2d86b0SStephane Eranian error: 194cc2d86b0SStephane Eranian return -ENOMEM; 195361c99a6SArnaldo Carvalho de Melo } 1965c581041SArnaldo Carvalho de Melo 197e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 19850d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 19950d08e47SArnaldo Carvalho de Melo { 20050d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 20150d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 20250d08e47SArnaldo Carvalho de Melo size_t i; 20350d08e47SArnaldo Carvalho de Melo 20450d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 205ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 20650d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 20750d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 20850d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 20950d08e47SArnaldo Carvalho de Melo } 21050d08e47SArnaldo Carvalho de Melo 21150d08e47SArnaldo Carvalho de Melo perf_evlist__splice_list_tail(evlist, &head, nr_attrs); 21250d08e47SArnaldo Carvalho de Melo 21350d08e47SArnaldo Carvalho de Melo return 0; 21450d08e47SArnaldo Carvalho de Melo 21550d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 2160050f7aaSArnaldo Carvalho de Melo __evlist__for_each_safe(&head, n, evsel) 21750d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 21850d08e47SArnaldo Carvalho de Melo return -1; 21950d08e47SArnaldo Carvalho de Melo } 22050d08e47SArnaldo Carvalho de Melo 22179695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 22279695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 22379695e1bSArnaldo Carvalho de Melo { 22479695e1bSArnaldo Carvalho de Melo size_t i; 22579695e1bSArnaldo Carvalho de Melo 22679695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 22779695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 22879695e1bSArnaldo Carvalho de Melo 22979695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 23079695e1bSArnaldo Carvalho de Melo } 23179695e1bSArnaldo Carvalho de Melo 232da378962SArnaldo Carvalho de Melo struct perf_evsel * 233ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 234ee29be62SArnaldo Carvalho de Melo { 235ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 236ee29be62SArnaldo Carvalho de Melo 2370050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 238ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 239ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 240ee29be62SArnaldo Carvalho de Melo return evsel; 241ee29be62SArnaldo Carvalho de Melo } 242ee29be62SArnaldo Carvalho de Melo 243ee29be62SArnaldo Carvalho de Melo return NULL; 244ee29be62SArnaldo Carvalho de Melo } 245ee29be62SArnaldo Carvalho de Melo 246a2f2804aSDavid Ahern struct perf_evsel * 247a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 248a2f2804aSDavid Ahern const char *name) 249a2f2804aSDavid Ahern { 250a2f2804aSDavid Ahern struct perf_evsel *evsel; 251a2f2804aSDavid Ahern 2520050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 253a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 254a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 255a2f2804aSDavid Ahern return evsel; 256a2f2804aSDavid Ahern } 257a2f2804aSDavid Ahern 258a2f2804aSDavid Ahern return NULL; 259a2f2804aSDavid Ahern } 260a2f2804aSDavid Ahern 26139876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 26239876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 26339876e7dSArnaldo Carvalho de Melo { 264ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 26539876e7dSArnaldo Carvalho de Melo 26639876e7dSArnaldo Carvalho de Melo if (evsel == NULL) 26739876e7dSArnaldo Carvalho de Melo return -1; 26839876e7dSArnaldo Carvalho de Melo 269744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 27039876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 27139876e7dSArnaldo Carvalho de Melo return 0; 27239876e7dSArnaldo Carvalho de Melo } 27339876e7dSArnaldo Carvalho de Melo 274bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 275bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 276bf8e8f4bSAdrian Hunter { 277bf8e8f4bSAdrian Hunter if (evsel->system_wide) 278bf8e8f4bSAdrian Hunter return 1; 279bf8e8f4bSAdrian Hunter else 280bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 281bf8e8f4bSAdrian Hunter } 282bf8e8f4bSAdrian Hunter 2834152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 2844152ab37SArnaldo Carvalho de Melo { 2854152ab37SArnaldo Carvalho de Melo int cpu, thread; 2864152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 287b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 288bf8e8f4bSAdrian Hunter int nr_threads; 2894152ab37SArnaldo Carvalho de Melo 290b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 2910050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 292395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 2933fe4430dSJiri Olsa continue; 294bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 295b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 29655da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 29755da8005SNamhyung Kim PERF_EVENT_IOC_DISABLE, 0); 2984152ab37SArnaldo Carvalho de Melo } 2994152ab37SArnaldo Carvalho de Melo } 3004152ab37SArnaldo Carvalho de Melo } 3014152ab37SArnaldo Carvalho de Melo 302764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 303764e16a3SDavid Ahern { 304764e16a3SDavid Ahern int cpu, thread; 305764e16a3SDavid Ahern struct perf_evsel *pos; 306b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 307bf8e8f4bSAdrian Hunter int nr_threads; 308764e16a3SDavid Ahern 309b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 3100050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 311395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3123fe4430dSJiri Olsa continue; 313bf8e8f4bSAdrian Hunter nr_threads = perf_evlist__nr_threads(evlist, pos); 314b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) 31555da8005SNamhyung Kim ioctl(FD(pos, cpu, thread), 31655da8005SNamhyung Kim PERF_EVENT_IOC_ENABLE, 0); 317764e16a3SDavid Ahern } 318764e16a3SDavid Ahern } 319764e16a3SDavid Ahern } 320764e16a3SDavid Ahern 321395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist, 322395c3070SAdrian Hunter struct perf_evsel *evsel) 323395c3070SAdrian Hunter { 324395c3070SAdrian Hunter int cpu, thread, err; 325bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 326bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 327395c3070SAdrian Hunter 328395c3070SAdrian Hunter if (!evsel->fd) 329395c3070SAdrian Hunter return 0; 330395c3070SAdrian Hunter 331bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 332bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 333395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 334395c3070SAdrian Hunter PERF_EVENT_IOC_DISABLE, 0); 335395c3070SAdrian Hunter if (err) 336395c3070SAdrian Hunter return err; 337395c3070SAdrian Hunter } 338395c3070SAdrian Hunter } 339395c3070SAdrian Hunter return 0; 340395c3070SAdrian Hunter } 341395c3070SAdrian Hunter 342395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist, 343395c3070SAdrian Hunter struct perf_evsel *evsel) 344395c3070SAdrian Hunter { 345395c3070SAdrian Hunter int cpu, thread, err; 346bf8e8f4bSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 347bf8e8f4bSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 348395c3070SAdrian Hunter 349395c3070SAdrian Hunter if (!evsel->fd) 350395c3070SAdrian Hunter return -EINVAL; 351395c3070SAdrian Hunter 352bf8e8f4bSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 353bf8e8f4bSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 354395c3070SAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 355395c3070SAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 356395c3070SAdrian Hunter if (err) 357395c3070SAdrian Hunter return err; 358395c3070SAdrian Hunter } 359395c3070SAdrian Hunter } 360395c3070SAdrian Hunter return 0; 361395c3070SAdrian Hunter } 362395c3070SAdrian Hunter 3631c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3641c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3651c65056cSAdrian Hunter { 3661c65056cSAdrian Hunter int thread, err; 3671c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 3681c65056cSAdrian Hunter 3691c65056cSAdrian Hunter if (!evsel->fd) 3701c65056cSAdrian Hunter return -EINVAL; 3711c65056cSAdrian Hunter 3721c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 3731c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 3741c65056cSAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 3751c65056cSAdrian Hunter if (err) 3761c65056cSAdrian Hunter return err; 3771c65056cSAdrian Hunter } 3781c65056cSAdrian Hunter return 0; 3791c65056cSAdrian Hunter } 3801c65056cSAdrian Hunter 3811c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 3821c65056cSAdrian Hunter struct perf_evsel *evsel, 3831c65056cSAdrian Hunter int thread) 3841c65056cSAdrian Hunter { 3851c65056cSAdrian Hunter int cpu, err; 3861c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 3871c65056cSAdrian Hunter 3881c65056cSAdrian Hunter if (!evsel->fd) 3891c65056cSAdrian Hunter return -EINVAL; 3901c65056cSAdrian Hunter 3911c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 3921c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 3931c65056cSAdrian Hunter if (err) 3941c65056cSAdrian Hunter return err; 3951c65056cSAdrian Hunter } 3961c65056cSAdrian Hunter return 0; 3971c65056cSAdrian Hunter } 3981c65056cSAdrian Hunter 3991c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4001c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4011c65056cSAdrian Hunter { 4021c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4031c65056cSAdrian Hunter 4041c65056cSAdrian Hunter if (per_cpu_mmaps) 4051c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4061c65056cSAdrian Hunter else 4071c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4081c65056cSAdrian Hunter } 4091c65056cSAdrian Hunter 410ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4115c581041SArnaldo Carvalho de Melo { 412b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 413b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 414bf8e8f4bSAdrian Hunter int nfds = 0; 415bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 416bf8e8f4bSAdrian Hunter 417cba9b847SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 418bf8e8f4bSAdrian Hunter if (evsel->system_wide) 419bf8e8f4bSAdrian Hunter nfds += nr_cpus; 420bf8e8f4bSAdrian Hunter else 421bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 422bf8e8f4bSAdrian Hunter } 423bf8e8f4bSAdrian Hunter 4241b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4251b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 426ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 427ad6765ddSArnaldo Carvalho de Melo 428ad6765ddSArnaldo Carvalho de Melo return 0; 4295c581041SArnaldo Carvalho de Melo } 43070082dd9SArnaldo Carvalho de Melo 431e4b356b5SArnaldo Carvalho de Melo static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx) 432e4b356b5SArnaldo Carvalho de Melo { 433e4b356b5SArnaldo Carvalho de Melo int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP); 434e4b356b5SArnaldo Carvalho de Melo /* 435e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 436e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 437e4b356b5SArnaldo Carvalho de Melo */ 438e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 439e4b356b5SArnaldo Carvalho de Melo evlist->pollfd.priv[pos].idx = idx; 440e4b356b5SArnaldo Carvalho de Melo 441e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 442e4b356b5SArnaldo Carvalho de Melo } 443e4b356b5SArnaldo Carvalho de Melo 444e4b356b5SArnaldo Carvalho de Melo return pos; 445e4b356b5SArnaldo Carvalho de Melo } 446e4b356b5SArnaldo Carvalho de Melo 447ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 44870082dd9SArnaldo Carvalho de Melo { 449e4b356b5SArnaldo Carvalho de Melo return __perf_evlist__add_pollfd(evlist, fd, -1); 450e4b356b5SArnaldo Carvalho de Melo } 4511b85337dSArnaldo Carvalho de Melo 452e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd) 453e4b356b5SArnaldo Carvalho de Melo { 454e4b356b5SArnaldo Carvalho de Melo struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd); 455e4b356b5SArnaldo Carvalho de Melo 456e4b356b5SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, fda->priv[fd].idx); 45770082dd9SArnaldo Carvalho de Melo } 45870db7533SArnaldo Carvalho de Melo 4591ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4601ddec7f0SArnaldo Carvalho de Melo { 461e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 462e4b356b5SArnaldo Carvalho de Melo perf_evlist__munmap_filtered); 4631ddec7f0SArnaldo Carvalho de Melo } 4641ddec7f0SArnaldo Carvalho de Melo 465f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 466f66a889dSArnaldo Carvalho de Melo { 4671b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 468f66a889dSArnaldo Carvalho de Melo } 469f66a889dSArnaldo Carvalho de Melo 470a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 471a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 4723d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 4733d3b5e95SArnaldo Carvalho de Melo { 4743d3b5e95SArnaldo Carvalho de Melo int hash; 4753d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 4763d3b5e95SArnaldo Carvalho de Melo 4773d3b5e95SArnaldo Carvalho de Melo sid->id = id; 4783d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 4793d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 4803d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 4813d3b5e95SArnaldo Carvalho de Melo } 4823d3b5e95SArnaldo Carvalho de Melo 483a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 484a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 485a91e5431SArnaldo Carvalho de Melo { 486a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 487a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 488a91e5431SArnaldo Carvalho de Melo } 489a91e5431SArnaldo Carvalho de Melo 490a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist, 4913d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 492f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 493f8a95309SArnaldo Carvalho de Melo { 494f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 4953d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 496e2b5abe0SJiri Olsa u64 id; 497e2b5abe0SJiri Olsa int ret; 498e2b5abe0SJiri Olsa 499e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 500e2b5abe0SJiri Olsa if (!ret) 501e2b5abe0SJiri Olsa goto add; 502e2b5abe0SJiri Olsa 503e2b5abe0SJiri Olsa if (errno != ENOTTY) 504e2b5abe0SJiri Olsa return -1; 505e2b5abe0SJiri Olsa 506e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 507f8a95309SArnaldo Carvalho de Melo 508c4861afeSJiri Olsa /* 509c4861afeSJiri Olsa * This way does not work with group format read, so bail 510c4861afeSJiri Olsa * out in that case. 511c4861afeSJiri Olsa */ 512c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 513c4861afeSJiri Olsa return -1; 514c4861afeSJiri Olsa 515f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 516f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 517f8a95309SArnaldo Carvalho de Melo return -1; 518f8a95309SArnaldo Carvalho de Melo 519f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 520f8a95309SArnaldo Carvalho de Melo ++id_idx; 521f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 522f8a95309SArnaldo Carvalho de Melo ++id_idx; 523f8a95309SArnaldo Carvalho de Melo 524e2b5abe0SJiri Olsa id = read_data[id_idx]; 525e2b5abe0SJiri Olsa 526e2b5abe0SJiri Olsa add: 527e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 528f8a95309SArnaldo Carvalho de Melo return 0; 529f8a95309SArnaldo Carvalho de Melo } 530f8a95309SArnaldo Carvalho de Melo 5313c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5323c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5333c659eedSAdrian Hunter int thread) 5343c659eedSAdrian Hunter { 5353c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5363c659eedSAdrian Hunter sid->idx = idx; 5373c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5383c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5393c659eedSAdrian Hunter else 5403c659eedSAdrian Hunter sid->cpu = -1; 5413c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 5423c659eedSAdrian Hunter sid->tid = evlist->threads->map[thread]; 5433c659eedSAdrian Hunter else 5443c659eedSAdrian Hunter sid->tid = -1; 5453c659eedSAdrian Hunter } 5463c659eedSAdrian Hunter 547932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 54870db7533SArnaldo Carvalho de Melo { 54970db7533SArnaldo Carvalho de Melo struct hlist_head *head; 55070db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 55170db7533SArnaldo Carvalho de Melo int hash; 55270db7533SArnaldo Carvalho de Melo 55370db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 55470db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 55570db7533SArnaldo Carvalho de Melo 556b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 55770db7533SArnaldo Carvalho de Melo if (sid->id == id) 558932a3594SJiri Olsa return sid; 559932a3594SJiri Olsa 560932a3594SJiri Olsa return NULL; 561932a3594SJiri Olsa } 562932a3594SJiri Olsa 563932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 564932a3594SJiri Olsa { 565932a3594SJiri Olsa struct perf_sample_id *sid; 566932a3594SJiri Olsa 567932a3594SJiri Olsa if (evlist->nr_entries == 1) 568932a3594SJiri Olsa return perf_evlist__first(evlist); 569932a3594SJiri Olsa 570932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 571932a3594SJiri Olsa if (sid) 57270db7533SArnaldo Carvalho de Melo return sid->evsel; 57330e68bccSNamhyung Kim 57430e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 5750c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 57630e68bccSNamhyung Kim 57770db7533SArnaldo Carvalho de Melo return NULL; 57870db7533SArnaldo Carvalho de Melo } 57904391debSArnaldo Carvalho de Melo 58075562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 58175562573SAdrian Hunter union perf_event *event, u64 *id) 58275562573SAdrian Hunter { 58375562573SAdrian Hunter const u64 *array = event->sample.array; 58475562573SAdrian Hunter ssize_t n; 58575562573SAdrian Hunter 58675562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 58775562573SAdrian Hunter 58875562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 58975562573SAdrian Hunter if (evlist->id_pos >= n) 59075562573SAdrian Hunter return -1; 59175562573SAdrian Hunter *id = array[evlist->id_pos]; 59275562573SAdrian Hunter } else { 59375562573SAdrian Hunter if (evlist->is_pos > n) 59475562573SAdrian Hunter return -1; 59575562573SAdrian Hunter n -= evlist->is_pos; 59675562573SAdrian Hunter *id = array[n]; 59775562573SAdrian Hunter } 59875562573SAdrian Hunter return 0; 59975562573SAdrian Hunter } 60075562573SAdrian Hunter 60175562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 60275562573SAdrian Hunter union perf_event *event) 60375562573SAdrian Hunter { 60498be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 60575562573SAdrian Hunter struct hlist_head *head; 60675562573SAdrian Hunter struct perf_sample_id *sid; 60775562573SAdrian Hunter int hash; 60875562573SAdrian Hunter u64 id; 60975562573SAdrian Hunter 61075562573SAdrian Hunter if (evlist->nr_entries == 1) 61198be6966SAdrian Hunter return first; 61298be6966SAdrian Hunter 61398be6966SAdrian Hunter if (!first->attr.sample_id_all && 61498be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 61598be6966SAdrian Hunter return first; 61675562573SAdrian Hunter 61775562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 61875562573SAdrian Hunter return NULL; 61975562573SAdrian Hunter 62075562573SAdrian Hunter /* Synthesized events have an id of zero */ 62175562573SAdrian Hunter if (!id) 62298be6966SAdrian Hunter return first; 62375562573SAdrian Hunter 62475562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 62575562573SAdrian Hunter head = &evlist->heads[hash]; 62675562573SAdrian Hunter 62775562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 62875562573SAdrian Hunter if (sid->id == id) 62975562573SAdrian Hunter return sid->evsel; 63075562573SAdrian Hunter } 63175562573SAdrian Hunter return NULL; 63275562573SAdrian Hunter } 63375562573SAdrian Hunter 634aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 63504391debSArnaldo Carvalho de Melo { 636aece948fSArnaldo Carvalho de Melo struct perf_mmap *md = &evlist->mmap[idx]; 63704391debSArnaldo Carvalho de Melo unsigned int head = perf_mmap__read_head(md); 63804391debSArnaldo Carvalho de Melo unsigned int old = md->prev; 63904391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 6408115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 64104391debSArnaldo Carvalho de Melo 6427bb41152SArnaldo Carvalho de Melo if (evlist->overwrite) { 64304391debSArnaldo Carvalho de Melo /* 64404391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 64504391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 64604391debSArnaldo Carvalho de Melo * 64704391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 64804391debSArnaldo Carvalho de Melo * 64904391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 65004391debSArnaldo Carvalho de Melo */ 6517bb41152SArnaldo Carvalho de Melo int diff = head - old; 65204391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 65304391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 65404391debSArnaldo Carvalho de Melo 65504391debSArnaldo Carvalho de Melo /* 65604391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 65704391debSArnaldo Carvalho de Melo */ 65804391debSArnaldo Carvalho de Melo old = head; 65904391debSArnaldo Carvalho de Melo } 6607bb41152SArnaldo Carvalho de Melo } 66104391debSArnaldo Carvalho de Melo 66204391debSArnaldo Carvalho de Melo if (old != head) { 66304391debSArnaldo Carvalho de Melo size_t size; 66404391debSArnaldo Carvalho de Melo 6658115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 66604391debSArnaldo Carvalho de Melo size = event->header.size; 66704391debSArnaldo Carvalho de Melo 66804391debSArnaldo Carvalho de Melo /* 66904391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 67004391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 67104391debSArnaldo Carvalho de Melo */ 67204391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 67304391debSArnaldo Carvalho de Melo unsigned int offset = old; 67404391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 675a65cb4b9SJiri Olsa void *dst = md->event_copy; 67604391debSArnaldo Carvalho de Melo 67704391debSArnaldo Carvalho de Melo do { 67804391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 67904391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 68004391debSArnaldo Carvalho de Melo offset += cpy; 68104391debSArnaldo Carvalho de Melo dst += cpy; 68204391debSArnaldo Carvalho de Melo len -= cpy; 68304391debSArnaldo Carvalho de Melo } while (len); 68404391debSArnaldo Carvalho de Melo 685a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 68604391debSArnaldo Carvalho de Melo } 68704391debSArnaldo Carvalho de Melo 68804391debSArnaldo Carvalho de Melo old += size; 68904391debSArnaldo Carvalho de Melo } 69004391debSArnaldo Carvalho de Melo 69104391debSArnaldo Carvalho de Melo md->prev = old; 6927bb41152SArnaldo Carvalho de Melo 69304391debSArnaldo Carvalho de Melo return event; 69404391debSArnaldo Carvalho de Melo } 695f8a95309SArnaldo Carvalho de Melo 69682396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 69782396986SArnaldo Carvalho de Melo { 69882396986SArnaldo Carvalho de Melo return perf_mmap__read_head(md) != md->prev; 69982396986SArnaldo Carvalho de Melo } 70082396986SArnaldo Carvalho de Melo 70182396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx) 70282396986SArnaldo Carvalho de Melo { 70382396986SArnaldo Carvalho de Melo ++evlist->mmap[idx].refcnt; 70482396986SArnaldo Carvalho de Melo } 70582396986SArnaldo Carvalho de Melo 70682396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx) 70782396986SArnaldo Carvalho de Melo { 70882396986SArnaldo Carvalho de Melo BUG_ON(evlist->mmap[idx].refcnt == 0); 70982396986SArnaldo Carvalho de Melo 71082396986SArnaldo Carvalho de Melo if (--evlist->mmap[idx].refcnt == 0) 71182396986SArnaldo Carvalho de Melo __perf_evlist__munmap(evlist, idx); 71282396986SArnaldo Carvalho de Melo } 71382396986SArnaldo Carvalho de Melo 7148e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 7158e50d384SZhouyi Zhou { 7168e50d384SZhouyi Zhou struct perf_mmap *md = &evlist->mmap[idx]; 71782396986SArnaldo Carvalho de Melo 71882396986SArnaldo Carvalho de Melo if (!evlist->overwrite) { 7198e50d384SZhouyi Zhou unsigned int old = md->prev; 7208e50d384SZhouyi Zhou 7218e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 7228e50d384SZhouyi Zhou } 72382396986SArnaldo Carvalho de Melo 72482396986SArnaldo Carvalho de Melo if (md->refcnt == 1 && perf_mmap__empty(md)) 72582396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 7268e50d384SZhouyi Zhou } 7278e50d384SZhouyi Zhou 72893edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 72993edcbd9SAdrian Hunter { 73093edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 73193edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 73293edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 73382396986SArnaldo Carvalho de Melo evlist->mmap[idx].refcnt = 0; 73493edcbd9SAdrian Hunter } 73593edcbd9SAdrian Hunter } 73693edcbd9SAdrian Hunter 7377e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 738f8a95309SArnaldo Carvalho de Melo { 739aece948fSArnaldo Carvalho de Melo int i; 740f8a95309SArnaldo Carvalho de Melo 741983874d1SArnaldo Carvalho de Melo if (evlist->mmap == NULL) 742983874d1SArnaldo Carvalho de Melo return; 743983874d1SArnaldo Carvalho de Melo 74493edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 74593edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 746aece948fSArnaldo Carvalho de Melo 74704662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 748f8a95309SArnaldo Carvalho de Melo } 749f8a95309SArnaldo Carvalho de Melo 750806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 751f8a95309SArnaldo Carvalho de Melo { 752a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 753ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 754b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 755aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 756f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 757f8a95309SArnaldo Carvalho de Melo } 758f8a95309SArnaldo Carvalho de Melo 759a8a8f3ebSAdrian Hunter struct mmap_params { 760a8a8f3ebSAdrian Hunter int prot; 761a8a8f3ebSAdrian Hunter int mask; 762a8a8f3ebSAdrian Hunter }; 763a8a8f3ebSAdrian Hunter 764a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, 765a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 766f8a95309SArnaldo Carvalho de Melo { 76782396986SArnaldo Carvalho de Melo /* 76882396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 76982396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 77082396986SArnaldo Carvalho de Melo * the ring buffer. 77182396986SArnaldo Carvalho de Melo * 77282396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 77382396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 77482396986SArnaldo Carvalho de Melo * waiting to be consumed. 77582396986SArnaldo Carvalho de Melo * 77682396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 77782396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 77882396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 77982396986SArnaldo Carvalho de Melo */ 78082396986SArnaldo Carvalho de Melo evlist->mmap[idx].refcnt = 2; 781aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 782a8a8f3ebSAdrian Hunter evlist->mmap[idx].mask = mp->mask; 783a8a8f3ebSAdrian Hunter evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, 784f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 785301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 78602635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 78702635965SAdrian Hunter errno); 788301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 789f8a95309SArnaldo Carvalho de Melo return -1; 790301b195dSNelson Elhage } 791ad6765ddSArnaldo Carvalho de Melo 792f8a95309SArnaldo Carvalho de Melo return 0; 793f8a95309SArnaldo Carvalho de Melo } 794f8a95309SArnaldo Carvalho de Melo 79504e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 796a8a8f3ebSAdrian Hunter struct mmap_params *mp, int cpu, 797a8a8f3ebSAdrian Hunter int thread, int *output) 798aece948fSArnaldo Carvalho de Melo { 799aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 80004e21314SAdrian Hunter 8010050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 802bf8e8f4bSAdrian Hunter int fd; 803bf8e8f4bSAdrian Hunter 804bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 805bf8e8f4bSAdrian Hunter continue; 806bf8e8f4bSAdrian Hunter 807bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 80804e21314SAdrian Hunter 80904e21314SAdrian Hunter if (*output == -1) { 81004e21314SAdrian Hunter *output = fd; 811a8a8f3ebSAdrian Hunter if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) 81204e21314SAdrian Hunter return -1; 81304e21314SAdrian Hunter } else { 81404e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 81504e21314SAdrian Hunter return -1; 81682396986SArnaldo Carvalho de Melo 81782396986SArnaldo Carvalho de Melo perf_evlist__mmap_get(evlist, idx); 81804e21314SAdrian Hunter } 81904e21314SAdrian Hunter 820f90d194aSAdrian Hunter /* 821f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 822f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 823f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 824f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 825f90d194aSAdrian Hunter * Therefore don't add it for polling. 826f90d194aSAdrian Hunter */ 827f90d194aSAdrian Hunter if (!evsel->system_wide && 828f90d194aSAdrian Hunter __perf_evlist__add_pollfd(evlist, fd, idx) < 0) { 82982396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 830ad6765ddSArnaldo Carvalho de Melo return -1; 83182396986SArnaldo Carvalho de Melo } 832033fa713SArnaldo Carvalho de Melo 8333c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 8343c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 8353c659eedSAdrian Hunter fd) < 0) 83604e21314SAdrian Hunter return -1; 8373c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 8383c659eedSAdrian Hunter thread); 8393c659eedSAdrian Hunter } 84004e21314SAdrian Hunter } 84104e21314SAdrian Hunter 84204e21314SAdrian Hunter return 0; 84304e21314SAdrian Hunter } 84404e21314SAdrian Hunter 845a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 846a8a8f3ebSAdrian Hunter struct mmap_params *mp) 84704e21314SAdrian Hunter { 848aece948fSArnaldo Carvalho de Melo int cpu, thread; 849b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 850b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 851aece948fSArnaldo Carvalho de Melo 852e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 853b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 854aece948fSArnaldo Carvalho de Melo int output = -1; 855aece948fSArnaldo Carvalho de Melo 856b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 857a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 858a8a8f3ebSAdrian Hunter thread, &output)) 859aece948fSArnaldo Carvalho de Melo goto out_unmap; 860aece948fSArnaldo Carvalho de Melo } 861aece948fSArnaldo Carvalho de Melo } 862aece948fSArnaldo Carvalho de Melo 863aece948fSArnaldo Carvalho de Melo return 0; 864aece948fSArnaldo Carvalho de Melo 865aece948fSArnaldo Carvalho de Melo out_unmap: 86693edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 86793edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 868aece948fSArnaldo Carvalho de Melo return -1; 869aece948fSArnaldo Carvalho de Melo } 870aece948fSArnaldo Carvalho de Melo 871a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 872a8a8f3ebSAdrian Hunter struct mmap_params *mp) 873aece948fSArnaldo Carvalho de Melo { 874aece948fSArnaldo Carvalho de Melo int thread; 875b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 876aece948fSArnaldo Carvalho de Melo 877e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 878b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 879aece948fSArnaldo Carvalho de Melo int output = -1; 880aece948fSArnaldo Carvalho de Melo 881a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 882a8a8f3ebSAdrian Hunter &output)) 883aece948fSArnaldo Carvalho de Melo goto out_unmap; 884aece948fSArnaldo Carvalho de Melo } 885aece948fSArnaldo Carvalho de Melo 886aece948fSArnaldo Carvalho de Melo return 0; 887aece948fSArnaldo Carvalho de Melo 888aece948fSArnaldo Carvalho de Melo out_unmap: 88993edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 89093edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 891aece948fSArnaldo Carvalho de Melo return -1; 892aece948fSArnaldo Carvalho de Melo } 893aece948fSArnaldo Carvalho de Melo 894994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages) 895994a1f78SJiri Olsa { 8968185e881SArnaldo Carvalho de Melo if (pages == UINT_MAX) { 8978185e881SArnaldo Carvalho de Melo int max; 8988185e881SArnaldo Carvalho de Melo 8998185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 9008185e881SArnaldo Carvalho de Melo /* 9018185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 9028185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 9038185e881SArnaldo Carvalho de Melo * die yet... 9048185e881SArnaldo Carvalho de Melo */ 9058185e881SArnaldo Carvalho de Melo max = 512; 9068185e881SArnaldo Carvalho de Melo } else { 9078185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 9088185e881SArnaldo Carvalho de Melo } 9098185e881SArnaldo Carvalho de Melo 9108185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 9111be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 9121be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 9138185e881SArnaldo Carvalho de Melo } else if (!is_power_of_2(pages)) 914994a1f78SJiri Olsa return 0; 915994a1f78SJiri Olsa 916994a1f78SJiri Olsa return (pages + 1) * page_size; 917994a1f78SJiri Olsa } 918994a1f78SJiri Olsa 91933c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 92033c2dcfdSDavid Ahern unsigned long max) 921994a1f78SJiri Olsa { 9222fbe4abeSAdrian Hunter unsigned long pages, val; 92327050f53SJiri Olsa static struct parse_tag tags[] = { 92427050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 92527050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 92627050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 92727050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 92827050f53SJiri Olsa { .tag = 0 }, 92927050f53SJiri Olsa }; 930994a1f78SJiri Olsa 9318973504bSDavid Ahern if (str == NULL) 93233c2dcfdSDavid Ahern return -EINVAL; 9338973504bSDavid Ahern 93427050f53SJiri Olsa val = parse_tag_value(str, tags); 9352fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 93627050f53SJiri Olsa /* we got file size value */ 93727050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 93827050f53SJiri Olsa } else { 93927050f53SJiri Olsa /* we got pages count value */ 94027050f53SJiri Olsa char *eptr; 941994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 94233c2dcfdSDavid Ahern if (*eptr != '\0') 94333c2dcfdSDavid Ahern return -EINVAL; 94427050f53SJiri Olsa } 945994a1f78SJiri Olsa 9462bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 94733c2dcfdSDavid Ahern /* leave number of pages at 0 */ 9481dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 94933c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 95091529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 9511dbfa938SAdrian Hunter if (!pages) 9521dbfa938SAdrian Hunter return -EINVAL; 9539639837eSDavid Ahern pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 9549639837eSDavid Ahern pages * page_size, pages); 9552fbe4abeSAdrian Hunter } 9562fbe4abeSAdrian Hunter 95733c2dcfdSDavid Ahern if (pages > max) 95833c2dcfdSDavid Ahern return -EINVAL; 95933c2dcfdSDavid Ahern 96033c2dcfdSDavid Ahern return pages; 96133c2dcfdSDavid Ahern } 96233c2dcfdSDavid Ahern 96333c2dcfdSDavid Ahern int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 96433c2dcfdSDavid Ahern int unset __maybe_unused) 96533c2dcfdSDavid Ahern { 96633c2dcfdSDavid Ahern unsigned int *mmap_pages = opt->value; 96733c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 96833c2dcfdSDavid Ahern long pages; 96933c2dcfdSDavid Ahern 970f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 97133c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 97233c2dcfdSDavid Ahern 97333c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 97433c2dcfdSDavid Ahern if (pages < 0) { 97533c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 976994a1f78SJiri Olsa return -1; 977994a1f78SJiri Olsa } 978994a1f78SJiri Olsa 979994a1f78SJiri Olsa *mmap_pages = pages; 980994a1f78SJiri Olsa return 0; 981994a1f78SJiri Olsa } 982994a1f78SJiri Olsa 983c83fa7f2SAdrian Hunter /** 984c83fa7f2SAdrian Hunter * perf_evlist__mmap - Create mmaps to receive events. 985c83fa7f2SAdrian Hunter * @evlist: list of events 986c83fa7f2SAdrian Hunter * @pages: map length in pages 987c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 988f8a95309SArnaldo Carvalho de Melo * 989c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 990c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 991c83fa7f2SAdrian Hunter * automatically. 992f8a95309SArnaldo Carvalho de Melo * 993c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 994f8a95309SArnaldo Carvalho de Melo */ 99550a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 99650a682ceSArnaldo Carvalho de Melo bool overwrite) 997f8a95309SArnaldo Carvalho de Melo { 998aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 9997e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 10007e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1001a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1002a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1003a8a8f3ebSAdrian Hunter }; 100450a682ceSArnaldo Carvalho de Melo 10057e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 1006f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1007f8a95309SArnaldo Carvalho de Melo 10081b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1009f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1010f8a95309SArnaldo Carvalho de Melo 1011f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1012994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 10132af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1014a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1015f8a95309SArnaldo Carvalho de Melo 10160050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1017f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1018a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1019a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1020f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1021f8a95309SArnaldo Carvalho de Melo } 1022f8a95309SArnaldo Carvalho de Melo 1023ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1024a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1025f8a95309SArnaldo Carvalho de Melo 1026a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1027f8a95309SArnaldo Carvalho de Melo } 10287e2ed097SArnaldo Carvalho de Melo 1029602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 10307e2ed097SArnaldo Carvalho de Melo { 1031b809ac10SNamhyung Kim evlist->threads = thread_map__new_str(target->pid, target->tid, 1032b809ac10SNamhyung Kim target->uid); 10337e2ed097SArnaldo Carvalho de Melo 10347e2ed097SArnaldo Carvalho de Melo if (evlist->threads == NULL) 10357e2ed097SArnaldo Carvalho de Melo return -1; 10367e2ed097SArnaldo Carvalho de Melo 10379c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 1038d1cb9fceSNamhyung Kim evlist->cpus = cpu_map__dummy_new(); 1039879d77d0SNamhyung Kim else 1040879d77d0SNamhyung Kim evlist->cpus = cpu_map__new(target->cpu_list); 10417e2ed097SArnaldo Carvalho de Melo 10427e2ed097SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 10437e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 10447e2ed097SArnaldo Carvalho de Melo 10457e2ed097SArnaldo Carvalho de Melo return 0; 10467e2ed097SArnaldo Carvalho de Melo 10477e2ed097SArnaldo Carvalho de Melo out_delete_threads: 10487e2ed097SArnaldo Carvalho de Melo thread_map__delete(evlist->threads); 1049b2e19a93SYasser Shalabi evlist->threads = NULL; 10507e2ed097SArnaldo Carvalho de Melo return -1; 10517e2ed097SArnaldo Carvalho de Melo } 10527e2ed097SArnaldo Carvalho de Melo 105323d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 10540a102479SFrederic Weisbecker { 10550a102479SFrederic Weisbecker struct perf_evsel *evsel; 1056745cefc5SArnaldo Carvalho de Melo int err = 0; 1057745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1058b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 10590a102479SFrederic Weisbecker 10600050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1061745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 10620a102479SFrederic Weisbecker continue; 1063745cefc5SArnaldo Carvalho de Melo 1064745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); 106523d4aad4SArnaldo Carvalho de Melo if (err) { 106623d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1067745cefc5SArnaldo Carvalho de Melo break; 10680a102479SFrederic Weisbecker } 106923d4aad4SArnaldo Carvalho de Melo } 10700a102479SFrederic Weisbecker 1071745cefc5SArnaldo Carvalho de Melo return err; 1072745cefc5SArnaldo Carvalho de Melo } 1073745cefc5SArnaldo Carvalho de Melo 1074745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1075745cefc5SArnaldo Carvalho de Melo { 1076745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1077745cefc5SArnaldo Carvalho de Melo int err = 0; 1078745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1079b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 1080745cefc5SArnaldo Carvalho de Melo 10810050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1082745cefc5SArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); 1083745cefc5SArnaldo Carvalho de Melo if (err) 1084745cefc5SArnaldo Carvalho de Melo break; 1085745cefc5SArnaldo Carvalho de Melo } 1086745cefc5SArnaldo Carvalho de Melo 1087745cefc5SArnaldo Carvalho de Melo return err; 10880a102479SFrederic Weisbecker } 108974429964SFrederic Weisbecker 1090be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1091cfd70a26SArnaldo Carvalho de Melo { 1092cfd70a26SArnaldo Carvalho de Melo char *filter; 1093be199adaSArnaldo Carvalho de Melo int ret = -1; 1094be199adaSArnaldo Carvalho de Melo size_t i; 1095cfd70a26SArnaldo Carvalho de Melo 1096be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1097be199adaSArnaldo Carvalho de Melo if (i == 0) { 1098be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1099cfd70a26SArnaldo Carvalho de Melo return -1; 1100be199adaSArnaldo Carvalho de Melo } else { 1101be199adaSArnaldo Carvalho de Melo char *tmp; 1102be199adaSArnaldo Carvalho de Melo 1103be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1104be199adaSArnaldo Carvalho de Melo goto out_free; 1105be199adaSArnaldo Carvalho de Melo 1106be199adaSArnaldo Carvalho de Melo free(filter); 1107be199adaSArnaldo Carvalho de Melo filter = tmp; 1108be199adaSArnaldo Carvalho de Melo } 1109be199adaSArnaldo Carvalho de Melo } 1110cfd70a26SArnaldo Carvalho de Melo 1111cfd70a26SArnaldo Carvalho de Melo ret = perf_evlist__set_filter(evlist, filter); 1112be199adaSArnaldo Carvalho de Melo out_free: 1113cfd70a26SArnaldo Carvalho de Melo free(filter); 1114cfd70a26SArnaldo Carvalho de Melo return ret; 1115cfd70a26SArnaldo Carvalho de Melo } 1116cfd70a26SArnaldo Carvalho de Melo 1117be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) 1118be199adaSArnaldo Carvalho de Melo { 1119be199adaSArnaldo Carvalho de Melo return perf_evlist__set_filter_pids(evlist, 1, &pid); 1120be199adaSArnaldo Carvalho de Melo } 1121be199adaSArnaldo Carvalho de Melo 11220c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 112374429964SFrederic Weisbecker { 112475562573SAdrian Hunter struct perf_evsel *pos; 1125c2a70653SArnaldo Carvalho de Melo 112675562573SAdrian Hunter if (evlist->nr_entries == 1) 112775562573SAdrian Hunter return true; 112875562573SAdrian Hunter 112975562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 113075562573SAdrian Hunter return false; 113175562573SAdrian Hunter 11320050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 113375562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 113475562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1135c2a70653SArnaldo Carvalho de Melo return false; 113674429964SFrederic Weisbecker } 113774429964SFrederic Weisbecker 1138c2a70653SArnaldo Carvalho de Melo return true; 1139c2a70653SArnaldo Carvalho de Melo } 1140c2a70653SArnaldo Carvalho de Melo 114175562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1142c2a70653SArnaldo Carvalho de Melo { 114375562573SAdrian Hunter struct perf_evsel *evsel; 114475562573SAdrian Hunter 114575562573SAdrian Hunter if (evlist->combined_sample_type) 114675562573SAdrian Hunter return evlist->combined_sample_type; 114775562573SAdrian Hunter 11480050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 114975562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 115075562573SAdrian Hunter 115175562573SAdrian Hunter return evlist->combined_sample_type; 115275562573SAdrian Hunter } 115375562573SAdrian Hunter 115475562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 115575562573SAdrian Hunter { 115675562573SAdrian Hunter evlist->combined_sample_type = 0; 115775562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1158c2a70653SArnaldo Carvalho de Melo } 1159c2a70653SArnaldo Carvalho de Melo 11609ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 11619ede473cSJiri Olsa { 11629ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 11639ede473cSJiri Olsa u64 read_format = first->attr.read_format; 11649ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 11659ede473cSJiri Olsa 11660050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 11679ede473cSJiri Olsa if (read_format != pos->attr.read_format) 11689ede473cSJiri Olsa return false; 11699ede473cSJiri Olsa } 11709ede473cSJiri Olsa 11719ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 11729ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 11739ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 11749ede473cSJiri Olsa return false; 11759ede473cSJiri Olsa } 11769ede473cSJiri Olsa 11779ede473cSJiri Olsa return true; 11789ede473cSJiri Olsa } 11799ede473cSJiri Olsa 11809ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 11819ede473cSJiri Olsa { 11829ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 11839ede473cSJiri Olsa return first->attr.read_format; 11849ede473cSJiri Olsa } 11859ede473cSJiri Olsa 11860c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 118781e36bffSArnaldo Carvalho de Melo { 11880c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 118981e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 119081e36bffSArnaldo Carvalho de Melo u64 sample_type; 119181e36bffSArnaldo Carvalho de Melo u16 size = 0; 119281e36bffSArnaldo Carvalho de Melo 119381e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 119481e36bffSArnaldo Carvalho de Melo goto out; 119581e36bffSArnaldo Carvalho de Melo 119681e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 119781e36bffSArnaldo Carvalho de Melo 119881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 119981e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 120081e36bffSArnaldo Carvalho de Melo 120181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 120281e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 120381e36bffSArnaldo Carvalho de Melo 120481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 120581e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 120681e36bffSArnaldo Carvalho de Melo 120781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 120881e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 120981e36bffSArnaldo Carvalho de Melo 121081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 121181e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 121275562573SAdrian Hunter 121375562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 121475562573SAdrian Hunter size += sizeof(data->id); 121581e36bffSArnaldo Carvalho de Melo out: 121681e36bffSArnaldo Carvalho de Melo return size; 121781e36bffSArnaldo Carvalho de Melo } 121881e36bffSArnaldo Carvalho de Melo 12190c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1220c2a70653SArnaldo Carvalho de Melo { 12210c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1222c2a70653SArnaldo Carvalho de Melo 12230050f7aaSArnaldo Carvalho de Melo evlist__for_each_continue(evlist, pos) { 1224c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1225c2a70653SArnaldo Carvalho de Melo return false; 1226c2a70653SArnaldo Carvalho de Melo } 1227c2a70653SArnaldo Carvalho de Melo 1228c2a70653SArnaldo Carvalho de Melo return true; 122974429964SFrederic Weisbecker } 123074429964SFrederic Weisbecker 12310c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 123274429964SFrederic Weisbecker { 12330c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1234c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 123574429964SFrederic Weisbecker } 123681cce8deSArnaldo Carvalho de Melo 123781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 123881cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 123981cce8deSArnaldo Carvalho de Melo { 124081cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 124181cce8deSArnaldo Carvalho de Melo } 1242727ab04eSArnaldo Carvalho de Melo 1243a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1244a74b4b66SNamhyung Kim { 1245a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1246a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1247a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 12488ad9219eSStephane Eranian int n; 1249a74b4b66SNamhyung Kim 12508ad9219eSStephane Eranian evlist__for_each_reverse(evlist, evsel) { 12518ad9219eSStephane Eranian n = evsel->cpus ? evsel->cpus->nr : ncpus; 12528ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 12538ad9219eSStephane Eranian } 1254a74b4b66SNamhyung Kim } 1255a74b4b66SNamhyung Kim 12564112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 12574112eb18SArnaldo Carvalho de Melo { 12584112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 12594112eb18SArnaldo Carvalho de Melo 12604112eb18SArnaldo Carvalho de Melo /* 12614112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 12624112eb18SArnaldo Carvalho de Melo * an all cpus map. 12634112eb18SArnaldo Carvalho de Melo * 12644112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 12654112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 12664112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 12674112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 12684112eb18SArnaldo Carvalho de Melo */ 12694112eb18SArnaldo Carvalho de Melo evlist->cpus = cpu_map__new(NULL); 12704112eb18SArnaldo Carvalho de Melo if (evlist->cpus == NULL) 12714112eb18SArnaldo Carvalho de Melo goto out; 12724112eb18SArnaldo Carvalho de Melo 12734112eb18SArnaldo Carvalho de Melo evlist->threads = thread_map__new_dummy(); 12744112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL) 12754112eb18SArnaldo Carvalho de Melo goto out_free_cpus; 12764112eb18SArnaldo Carvalho de Melo 12774112eb18SArnaldo Carvalho de Melo err = 0; 12784112eb18SArnaldo Carvalho de Melo out: 12794112eb18SArnaldo Carvalho de Melo return err; 12804112eb18SArnaldo Carvalho de Melo out_free_cpus: 12814112eb18SArnaldo Carvalho de Melo cpu_map__delete(evlist->cpus); 12824112eb18SArnaldo Carvalho de Melo evlist->cpus = NULL; 12834112eb18SArnaldo Carvalho de Melo goto out; 12844112eb18SArnaldo Carvalho de Melo } 12854112eb18SArnaldo Carvalho de Melo 12866a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1287727ab04eSArnaldo Carvalho de Melo { 12886a4bb04cSJiri Olsa struct perf_evsel *evsel; 1289a74b4b66SNamhyung Kim int err; 1290727ab04eSArnaldo Carvalho de Melo 12914112eb18SArnaldo Carvalho de Melo /* 12924112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 12934112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 12944112eb18SArnaldo Carvalho de Melo */ 12954112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 12964112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 12974112eb18SArnaldo Carvalho de Melo if (err < 0) 12984112eb18SArnaldo Carvalho de Melo goto out_err; 12994112eb18SArnaldo Carvalho de Melo } 13004112eb18SArnaldo Carvalho de Melo 1301733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1302733cd2feSAdrian Hunter 13030050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 13046a4bb04cSJiri Olsa err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); 1305727ab04eSArnaldo Carvalho de Melo if (err < 0) 1306727ab04eSArnaldo Carvalho de Melo goto out_err; 1307727ab04eSArnaldo Carvalho de Melo } 1308727ab04eSArnaldo Carvalho de Melo 1309727ab04eSArnaldo Carvalho de Melo return 0; 1310727ab04eSArnaldo Carvalho de Melo out_err: 1311a74b4b66SNamhyung Kim perf_evlist__close(evlist); 131241c21a68SNamhyung Kim errno = -err; 1313727ab04eSArnaldo Carvalho de Melo return err; 1314727ab04eSArnaldo Carvalho de Melo } 131535b9d88eSArnaldo Carvalho de Melo 1316602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 131755e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1318735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 131935b9d88eSArnaldo Carvalho de Melo { 132035b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 132135b9d88eSArnaldo Carvalho de Melo char bf; 132235b9d88eSArnaldo Carvalho de Melo 132335b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 132435b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 132535b9d88eSArnaldo Carvalho de Melo return -1; 132635b9d88eSArnaldo Carvalho de Melo } 132735b9d88eSArnaldo Carvalho de Melo 132835b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 132935b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 133035b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 133135b9d88eSArnaldo Carvalho de Melo } 133235b9d88eSArnaldo Carvalho de Melo 133335b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 133435b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 133535b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 133635b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 133735b9d88eSArnaldo Carvalho de Melo } 133835b9d88eSArnaldo Carvalho de Melo 133935b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 13405f1c4225SArnaldo Carvalho de Melo int ret; 13415f1c4225SArnaldo Carvalho de Melo 1342119fa3c9SNamhyung Kim if (pipe_output) 134335b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 134435b9d88eSArnaldo Carvalho de Melo 13450817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 13460817df08SDavid Ahern 134735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 134835b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 134935b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 135035b9d88eSArnaldo Carvalho de Melo 135135b9d88eSArnaldo Carvalho de Melo /* 135235b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 135335b9d88eSArnaldo Carvalho de Melo */ 135435b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 135535b9d88eSArnaldo Carvalho de Melo 135635b9d88eSArnaldo Carvalho de Melo /* 135735b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 135835b9d88eSArnaldo Carvalho de Melo */ 13595f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 13605f1c4225SArnaldo Carvalho de Melo /* 13615f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 13625f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 13635f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 13645f1c4225SArnaldo Carvalho de Melo * 136520f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 13665f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 13675f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 13685f1c4225SArnaldo Carvalho de Melo * here. 13695f1c4225SArnaldo Carvalho de Melo */ 13705f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 13715f1c4225SArnaldo Carvalho de Melo if (ret == -1) 137235b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 13735f1c4225SArnaldo Carvalho de Melo exit(ret); 13745f1c4225SArnaldo Carvalho de Melo } 137535b9d88eSArnaldo Carvalho de Melo 137635b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 137735b9d88eSArnaldo Carvalho de Melo 1378735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1379f33cbe72SArnaldo Carvalho de Melo union sigval val; 1380f33cbe72SArnaldo Carvalho de Melo 1381f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1382f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 138335b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1384f33cbe72SArnaldo Carvalho de Melo } else 1385f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 138635b9d88eSArnaldo Carvalho de Melo exit(-1); 138735b9d88eSArnaldo Carvalho de Melo } 138835b9d88eSArnaldo Carvalho de Melo 1389735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1390735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1391735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1392735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1393735f7e0bSArnaldo Carvalho de Melo }; 1394735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1395735f7e0bSArnaldo Carvalho de Melo } 1396735f7e0bSArnaldo Carvalho de Melo 13971aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 13981aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 13991aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 14001aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 14011aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 14021aaf63b1SArnaldo Carvalho de Melo } 140335b9d88eSArnaldo Carvalho de Melo evlist->threads->map[0] = evlist->workload.pid; 14041aaf63b1SArnaldo Carvalho de Melo } 140535b9d88eSArnaldo Carvalho de Melo 140635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 140735b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 140835b9d88eSArnaldo Carvalho de Melo /* 140935b9d88eSArnaldo Carvalho de Melo * wait for child to settle 141035b9d88eSArnaldo Carvalho de Melo */ 141135b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 141235b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 141335b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 141435b9d88eSArnaldo Carvalho de Melo } 141535b9d88eSArnaldo Carvalho de Melo 1416bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 141735b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 141835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 141935b9d88eSArnaldo Carvalho de Melo return 0; 142035b9d88eSArnaldo Carvalho de Melo 142135b9d88eSArnaldo Carvalho de Melo out_close_pipes: 142235b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 142335b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 142435b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 142535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 142635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 142735b9d88eSArnaldo Carvalho de Melo return -1; 142835b9d88eSArnaldo Carvalho de Melo } 142935b9d88eSArnaldo Carvalho de Melo 143035b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 143135b9d88eSArnaldo Carvalho de Melo { 143235b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1433b3824404SDavid Ahern char bf = 0; 1434bcf3145fSNamhyung Kim int ret; 143535b9d88eSArnaldo Carvalho de Melo /* 143635b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 143735b9d88eSArnaldo Carvalho de Melo */ 1438bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1439bcf3145fSNamhyung Kim if (ret < 0) 1440bcf3145fSNamhyung Kim perror("enable to write to pipe"); 1441bcf3145fSNamhyung Kim 1442bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1443bcf3145fSNamhyung Kim return ret; 144435b9d88eSArnaldo Carvalho de Melo } 144535b9d88eSArnaldo Carvalho de Melo 144635b9d88eSArnaldo Carvalho de Melo return 0; 144735b9d88eSArnaldo Carvalho de Melo } 1448cb0b29e0SArnaldo Carvalho de Melo 1449a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 14500807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1451cb0b29e0SArnaldo Carvalho de Melo { 145275562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 145375562573SAdrian Hunter 145475562573SAdrian Hunter if (!evsel) 145575562573SAdrian Hunter return -EFAULT; 14560807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1457cb0b29e0SArnaldo Carvalho de Melo } 145878f067b3SArnaldo Carvalho de Melo 145978f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 146078f067b3SArnaldo Carvalho de Melo { 146178f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 146278f067b3SArnaldo Carvalho de Melo size_t printed = 0; 146378f067b3SArnaldo Carvalho de Melo 14640050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 146578f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 146678f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 146778f067b3SArnaldo Carvalho de Melo } 146878f067b3SArnaldo Carvalho de Melo 1469b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 147078f067b3SArnaldo Carvalho de Melo } 14716ef068cbSArnaldo Carvalho de Melo 1472a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, 1473a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1474a8f23d8fSArnaldo Carvalho de Melo { 1475a8f23d8fSArnaldo Carvalho de Melo int printed, value; 14766e81c74cSMasami Hiramatsu char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1477a8f23d8fSArnaldo Carvalho de Melo 1478a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1479a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1480a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1481a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1482a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1483a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1484a8f23d8fSArnaldo Carvalho de Melo 14851a47245dSAdrian Hunter value = perf_event_paranoid(); 1486a8f23d8fSArnaldo Carvalho de Melo 1487a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1488a8f23d8fSArnaldo Carvalho de Melo 1489a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1490a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1491a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1492a8f23d8fSArnaldo Carvalho de Melo } 1493a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14945229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1495a8f23d8fSArnaldo Carvalho de Melo 1496a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 14975229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 14985229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1499a8f23d8fSArnaldo Carvalho de Melo break; 1500a8f23d8fSArnaldo Carvalho de Melo default: 1501a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1502a8f23d8fSArnaldo Carvalho de Melo break; 1503a8f23d8fSArnaldo Carvalho de Melo } 1504a8f23d8fSArnaldo Carvalho de Melo 1505a8f23d8fSArnaldo Carvalho de Melo return 0; 1506a8f23d8fSArnaldo Carvalho de Melo } 1507a025e4f0SAdrian Hunter 1508956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1509956fa571SArnaldo Carvalho de Melo { 1510956fa571SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1511e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1512956fa571SArnaldo Carvalho de Melo 1513956fa571SArnaldo Carvalho de Melo switch (err) { 1514956fa571SArnaldo Carvalho de Melo case EPERM: 1515e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1516e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1517e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1518956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1519e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1520e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1521e965bea1SArnaldo Carvalho de Melo 1522e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1523e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1524e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1525e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1526e965bea1SArnaldo Carvalho de Melo } 1527e965bea1SArnaldo Carvalho de Melo 1528e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1529e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1530956fa571SArnaldo Carvalho de Melo break; 1531956fa571SArnaldo Carvalho de Melo default: 1532956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1533956fa571SArnaldo Carvalho de Melo break; 1534956fa571SArnaldo Carvalho de Melo } 1535956fa571SArnaldo Carvalho de Melo 1536956fa571SArnaldo Carvalho de Melo return 0; 1537956fa571SArnaldo Carvalho de Melo } 1538956fa571SArnaldo Carvalho de Melo 1539a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1540a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1541a025e4f0SAdrian Hunter { 1542a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1543a025e4f0SAdrian Hunter LIST_HEAD(move); 1544a025e4f0SAdrian Hunter 1545a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1546a025e4f0SAdrian Hunter return; 1547a025e4f0SAdrian Hunter 15480050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, evsel) { 1549a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1550a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1551a025e4f0SAdrian Hunter } 1552a025e4f0SAdrian Hunter 1553a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1554a025e4f0SAdrian Hunter } 155560b0896cSAdrian Hunter 155660b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 155760b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 155860b0896cSAdrian Hunter { 155960b0896cSAdrian Hunter struct perf_evsel *evsel; 156060b0896cSAdrian Hunter 156160b0896cSAdrian Hunter if (tracking_evsel->tracking) 156260b0896cSAdrian Hunter return; 156360b0896cSAdrian Hunter 156460b0896cSAdrian Hunter evlist__for_each(evlist, evsel) { 156560b0896cSAdrian Hunter if (evsel != tracking_evsel) 156660b0896cSAdrian Hunter evsel->tracking = false; 156760b0896cSAdrian Hunter } 156860b0896cSAdrian Hunter 156960b0896cSAdrian Hunter tracking_evsel->tracking = true; 157060b0896cSAdrian Hunter } 1571