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" 214b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 2250d08e47SArnaldo Carvalho de Melo 23f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h> 24f8a95309SArnaldo Carvalho de Melo 2570db7533SArnaldo Carvalho de Melo #include <linux/bitops.h> 2670db7533SArnaldo Carvalho de Melo #include <linux/hash.h> 270389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h> 288dd2a131SJiri Olsa #include <linux/err.h> 2970db7533SArnaldo Carvalho de Melo 30e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx); 31e4b356b5SArnaldo Carvalho de Melo static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx); 32e4b356b5SArnaldo Carvalho de Melo 33f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) 34a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) 35f8a95309SArnaldo Carvalho de Melo 367e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, 377e2ed097SArnaldo Carvalho de Melo struct thread_map *threads) 38361c99a6SArnaldo Carvalho de Melo { 3970db7533SArnaldo Carvalho de Melo int i; 4070db7533SArnaldo Carvalho de Melo 4170db7533SArnaldo Carvalho de Melo for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) 4270db7533SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&evlist->heads[i]); 43361c99a6SArnaldo Carvalho de Melo INIT_LIST_HEAD(&evlist->entries); 447e2ed097SArnaldo Carvalho de Melo perf_evlist__set_maps(evlist, cpus, threads); 451b85337dSArnaldo Carvalho de Melo fdarray__init(&evlist->pollfd, 64); 4635b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = -1; 47361c99a6SArnaldo Carvalho de Melo } 48361c99a6SArnaldo Carvalho de Melo 49334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 50ef1d1af2SArnaldo Carvalho de Melo { 51ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 52ef1d1af2SArnaldo Carvalho de Melo 53ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 54334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 55ef1d1af2SArnaldo Carvalho de Melo 56361c99a6SArnaldo Carvalho de Melo return evlist; 57361c99a6SArnaldo Carvalho de Melo } 58361c99a6SArnaldo Carvalho de Melo 59b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 60b22d54b0SJiri Olsa { 61b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 62b22d54b0SJiri Olsa 63b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 64b22d54b0SJiri Olsa perf_evlist__delete(evlist); 65b22d54b0SJiri Olsa evlist = NULL; 66b22d54b0SJiri Olsa } 67b22d54b0SJiri Olsa 68b22d54b0SJiri Olsa return evlist; 69b22d54b0SJiri Olsa } 70b22d54b0SJiri Olsa 715bae0250SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new_dummy(void) 725bae0250SArnaldo Carvalho de Melo { 735bae0250SArnaldo Carvalho de Melo struct perf_evlist *evlist = perf_evlist__new(); 745bae0250SArnaldo Carvalho de Melo 755bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 765bae0250SArnaldo Carvalho de Melo perf_evlist__delete(evlist); 775bae0250SArnaldo Carvalho de Melo evlist = NULL; 785bae0250SArnaldo Carvalho de Melo } 795bae0250SArnaldo Carvalho de Melo 805bae0250SArnaldo Carvalho de Melo return evlist; 815bae0250SArnaldo Carvalho de Melo } 825bae0250SArnaldo Carvalho de Melo 8375562573SAdrian Hunter /** 8475562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 8575562573SAdrian Hunter * @evlist: selected event list 8675562573SAdrian Hunter * 8775562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 8875562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 8975562573SAdrian Hunter */ 9075562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 9175562573SAdrian Hunter { 9275562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 9375562573SAdrian Hunter 9475562573SAdrian Hunter evlist->id_pos = first->id_pos; 9575562573SAdrian Hunter evlist->is_pos = first->is_pos; 9675562573SAdrian Hunter } 9775562573SAdrian Hunter 98733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 99733cd2feSAdrian Hunter { 100733cd2feSAdrian Hunter struct perf_evsel *evsel; 101733cd2feSAdrian Hunter 1020050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 103733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 104733cd2feSAdrian Hunter 105733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 106733cd2feSAdrian Hunter } 107733cd2feSAdrian Hunter 108361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 109361c99a6SArnaldo Carvalho de Melo { 110361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 111361c99a6SArnaldo Carvalho de Melo 1120050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, pos) { 113361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 114d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 115361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 116361c99a6SArnaldo Carvalho de Melo } 117361c99a6SArnaldo Carvalho de Melo 118361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 119361c99a6SArnaldo Carvalho de Melo } 120361c99a6SArnaldo Carvalho de Melo 121ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 122ef1d1af2SArnaldo Carvalho de Melo { 12304662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1241b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 125ef1d1af2SArnaldo Carvalho de Melo } 126ef1d1af2SArnaldo Carvalho de Melo 127361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 128361c99a6SArnaldo Carvalho de Melo { 129983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 130f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 131f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 132186fbb74SJiri Olsa thread_map__put(evlist->threads); 13303ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 13403ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 135361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 136ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 137361c99a6SArnaldo Carvalho de Melo free(evlist); 138361c99a6SArnaldo Carvalho de Melo } 139361c99a6SArnaldo Carvalho de Melo 140adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, 141adc0c3e8SAdrian Hunter struct perf_evsel *evsel) 142adc0c3e8SAdrian Hunter { 143adc0c3e8SAdrian Hunter /* 144adc0c3e8SAdrian Hunter * We already have cpus for evsel (via PMU sysfs) so 145adc0c3e8SAdrian Hunter * keep it, if there's no target cpu list defined. 146adc0c3e8SAdrian Hunter */ 147adc0c3e8SAdrian Hunter if (!evsel->own_cpus || evlist->has_user_cpus) { 148adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 149adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evlist->cpus); 150adc0c3e8SAdrian Hunter } else if (evsel->cpus != evsel->own_cpus) { 151adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 152adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evsel->own_cpus); 153adc0c3e8SAdrian Hunter } 154adc0c3e8SAdrian Hunter 155adc0c3e8SAdrian Hunter thread_map__put(evsel->threads); 156adc0c3e8SAdrian Hunter evsel->threads = thread_map__get(evlist->threads); 157adc0c3e8SAdrian Hunter } 158adc0c3e8SAdrian Hunter 159adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist) 160adc0c3e8SAdrian Hunter { 161adc0c3e8SAdrian Hunter struct perf_evsel *evsel; 162adc0c3e8SAdrian Hunter 163adc0c3e8SAdrian Hunter evlist__for_each(evlist, evsel) 164adc0c3e8SAdrian Hunter __perf_evlist__propagate_maps(evlist, evsel); 165adc0c3e8SAdrian Hunter } 166adc0c3e8SAdrian Hunter 167361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 168361c99a6SArnaldo Carvalho de Melo { 169d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 170361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 171ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 17260b0896cSAdrian Hunter entry->tracking = !entry->idx; 173ef503831SArnaldo Carvalho de Melo 17475562573SAdrian Hunter if (!evlist->nr_entries++) 17575562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 17644c42d71SAdrian Hunter 17744c42d71SAdrian Hunter __perf_evlist__propagate_maps(evlist, entry); 178361c99a6SArnaldo Carvalho de Melo } 179361c99a6SArnaldo Carvalho de Melo 1804768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel) 1814768230aSAdrian Hunter { 1824768230aSAdrian Hunter evsel->evlist = NULL; 1834768230aSAdrian Hunter list_del_init(&evsel->node); 1844768230aSAdrian Hunter evlist->nr_entries -= 1; 1854768230aSAdrian Hunter } 1864768230aSAdrian Hunter 1870529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 188f114d6efSAdrian Hunter struct list_head *list) 18950d08e47SArnaldo Carvalho de Melo { 190f114d6efSAdrian Hunter struct perf_evsel *evsel, *temp; 19175562573SAdrian Hunter 192f114d6efSAdrian Hunter __evlist__for_each_safe(list, temp, evsel) { 193f114d6efSAdrian Hunter list_del_init(&evsel->node); 194f114d6efSAdrian Hunter perf_evlist__add(evlist, evsel); 195f114d6efSAdrian Hunter } 19650d08e47SArnaldo Carvalho de Melo } 19750d08e47SArnaldo Carvalho de Melo 19863dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 19963dab225SArnaldo Carvalho de Melo { 20063dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 20163dab225SArnaldo Carvalho de Melo 20263dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 20397f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 20497f63e4aSNamhyung Kim 20597f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 20663dab225SArnaldo Carvalho de Melo 2070050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 20863dab225SArnaldo Carvalho de Melo evsel->leader = leader; 20963dab225SArnaldo Carvalho de Melo } 21063dab225SArnaldo Carvalho de Melo } 21163dab225SArnaldo Carvalho de Melo 21263dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 2136a4bb04cSJiri Olsa { 21497f63e4aSNamhyung Kim if (evlist->nr_entries) { 21597f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 21663dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 2176a4bb04cSJiri Olsa } 21897f63e4aSNamhyung Kim } 2196a4bb04cSJiri Olsa 22045cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr) 2217f8d1adeSArnaldo Carvalho de Melo { 2227f8d1adeSArnaldo Carvalho de Melo attr->precise_ip = 3; 2237f8d1adeSArnaldo Carvalho de Melo 2247f8d1adeSArnaldo Carvalho de Melo while (attr->precise_ip != 0) { 2257f8d1adeSArnaldo Carvalho de Melo int fd = sys_perf_event_open(attr, 0, -1, -1, 0); 2267f8d1adeSArnaldo Carvalho de Melo if (fd != -1) { 2277f8d1adeSArnaldo Carvalho de Melo close(fd); 2287f8d1adeSArnaldo Carvalho de Melo break; 2297f8d1adeSArnaldo Carvalho de Melo } 2307f8d1adeSArnaldo Carvalho de Melo --attr->precise_ip; 2317f8d1adeSArnaldo Carvalho de Melo } 2327f8d1adeSArnaldo Carvalho de Melo } 2337f8d1adeSArnaldo Carvalho de Melo 234361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 235361c99a6SArnaldo Carvalho de Melo { 236361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 237361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 238361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 239361c99a6SArnaldo Carvalho de Melo }; 2401aed2671SJoerg Roedel struct perf_evsel *evsel; 241361c99a6SArnaldo Carvalho de Melo 2421aed2671SJoerg Roedel event_attr_init(&attr); 2431aed2671SJoerg Roedel 2447f8d1adeSArnaldo Carvalho de Melo perf_event_attr__set_max_precise_ip(&attr); 2457f8d1adeSArnaldo Carvalho de Melo 246ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 247361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 248cc2d86b0SStephane Eranian goto error; 249cc2d86b0SStephane Eranian 2507f8d1adeSArnaldo Carvalho de Melo /* use asprintf() because free(evsel) assumes name is allocated */ 2517f8d1adeSArnaldo Carvalho de Melo if (asprintf(&evsel->name, "cycles%.*s", 2527f8d1adeSArnaldo Carvalho de Melo attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0) 253cc2d86b0SStephane Eranian goto error_free; 254361c99a6SArnaldo Carvalho de Melo 255361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 256361c99a6SArnaldo Carvalho de Melo return 0; 257cc2d86b0SStephane Eranian error_free: 258cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 259cc2d86b0SStephane Eranian error: 260cc2d86b0SStephane Eranian return -ENOMEM; 261361c99a6SArnaldo Carvalho de Melo } 2625c581041SArnaldo Carvalho de Melo 2635bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist) 2645bae0250SArnaldo Carvalho de Melo { 2655bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2665bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2675bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2685bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2695bae0250SArnaldo Carvalho de Melo }; 2705bae0250SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new(&attr); 2715bae0250SArnaldo Carvalho de Melo 2725bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2735bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2745bae0250SArnaldo Carvalho de Melo 2755bae0250SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 2765bae0250SArnaldo Carvalho de Melo return 0; 2775bae0250SArnaldo Carvalho de Melo } 2785bae0250SArnaldo Carvalho de Melo 279e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 28050d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 28150d08e47SArnaldo Carvalho de Melo { 28250d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 28350d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 28450d08e47SArnaldo Carvalho de Melo size_t i; 28550d08e47SArnaldo Carvalho de Melo 28650d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 287ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 28850d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 28950d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 29050d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 29150d08e47SArnaldo Carvalho de Melo } 29250d08e47SArnaldo Carvalho de Melo 293f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 29450d08e47SArnaldo Carvalho de Melo 29550d08e47SArnaldo Carvalho de Melo return 0; 29650d08e47SArnaldo Carvalho de Melo 29750d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 2980050f7aaSArnaldo Carvalho de Melo __evlist__for_each_safe(&head, n, evsel) 29950d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 30050d08e47SArnaldo Carvalho de Melo return -1; 30150d08e47SArnaldo Carvalho de Melo } 30250d08e47SArnaldo Carvalho de Melo 30379695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 30479695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 30579695e1bSArnaldo Carvalho de Melo { 30679695e1bSArnaldo Carvalho de Melo size_t i; 30779695e1bSArnaldo Carvalho de Melo 30879695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 30979695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 31079695e1bSArnaldo Carvalho de Melo 31179695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 31279695e1bSArnaldo Carvalho de Melo } 31379695e1bSArnaldo Carvalho de Melo 314da378962SArnaldo Carvalho de Melo struct perf_evsel * 315ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 316ee29be62SArnaldo Carvalho de Melo { 317ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 318ee29be62SArnaldo Carvalho de Melo 3190050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 320ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 321ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 322ee29be62SArnaldo Carvalho de Melo return evsel; 323ee29be62SArnaldo Carvalho de Melo } 324ee29be62SArnaldo Carvalho de Melo 325ee29be62SArnaldo Carvalho de Melo return NULL; 326ee29be62SArnaldo Carvalho de Melo } 327ee29be62SArnaldo Carvalho de Melo 328a2f2804aSDavid Ahern struct perf_evsel * 329a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 330a2f2804aSDavid Ahern const char *name) 331a2f2804aSDavid Ahern { 332a2f2804aSDavid Ahern struct perf_evsel *evsel; 333a2f2804aSDavid Ahern 3340050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 335a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 336a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 337a2f2804aSDavid Ahern return evsel; 338a2f2804aSDavid Ahern } 339a2f2804aSDavid Ahern 340a2f2804aSDavid Ahern return NULL; 341a2f2804aSDavid Ahern } 342a2f2804aSDavid Ahern 34339876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 34439876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 34539876e7dSArnaldo Carvalho de Melo { 346ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 34739876e7dSArnaldo Carvalho de Melo 3488dd2a131SJiri Olsa if (IS_ERR(evsel)) 34939876e7dSArnaldo Carvalho de Melo return -1; 35039876e7dSArnaldo Carvalho de Melo 351744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 35239876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 35339876e7dSArnaldo Carvalho de Melo return 0; 35439876e7dSArnaldo Carvalho de Melo } 35539876e7dSArnaldo Carvalho de Melo 356bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 357bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 358bf8e8f4bSAdrian Hunter { 359bf8e8f4bSAdrian Hunter if (evsel->system_wide) 360bf8e8f4bSAdrian Hunter return 1; 361bf8e8f4bSAdrian Hunter else 362bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 363bf8e8f4bSAdrian Hunter } 364bf8e8f4bSAdrian Hunter 3654152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 3664152ab37SArnaldo Carvalho de Melo { 3674152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 3684152ab37SArnaldo Carvalho de Melo 3690050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 370395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3713fe4430dSJiri Olsa continue; 3723e27c920SJiri Olsa perf_evsel__disable(pos); 3734152ab37SArnaldo Carvalho de Melo } 3742b56bcfbSArnaldo Carvalho de Melo 3752b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3764152ab37SArnaldo Carvalho de Melo } 3774152ab37SArnaldo Carvalho de Melo 378764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 379764e16a3SDavid Ahern { 380764e16a3SDavid Ahern struct perf_evsel *pos; 381764e16a3SDavid Ahern 3820050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 383395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3843fe4430dSJiri Olsa continue; 3853e27c920SJiri Olsa perf_evsel__enable(pos); 386764e16a3SDavid Ahern } 3872b56bcfbSArnaldo Carvalho de Melo 3882b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3892b56bcfbSArnaldo Carvalho de Melo } 3902b56bcfbSArnaldo Carvalho de Melo 3912b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist) 3922b56bcfbSArnaldo Carvalho de Melo { 3932b56bcfbSArnaldo Carvalho de Melo (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); 394764e16a3SDavid Ahern } 395764e16a3SDavid Ahern 3961c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3971c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3981c65056cSAdrian Hunter { 3991c65056cSAdrian Hunter int thread, err; 4001c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 4011c65056cSAdrian Hunter 4021c65056cSAdrian Hunter if (!evsel->fd) 4031c65056cSAdrian Hunter return -EINVAL; 4041c65056cSAdrian Hunter 4051c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 4061c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 4071c65056cSAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 4081c65056cSAdrian Hunter if (err) 4091c65056cSAdrian Hunter return err; 4101c65056cSAdrian Hunter } 4111c65056cSAdrian Hunter return 0; 4121c65056cSAdrian Hunter } 4131c65056cSAdrian Hunter 4141c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 4151c65056cSAdrian Hunter struct perf_evsel *evsel, 4161c65056cSAdrian Hunter int thread) 4171c65056cSAdrian Hunter { 4181c65056cSAdrian Hunter int cpu, err; 4191c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 4201c65056cSAdrian Hunter 4211c65056cSAdrian Hunter if (!evsel->fd) 4221c65056cSAdrian Hunter return -EINVAL; 4231c65056cSAdrian Hunter 4241c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 4251c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4261c65056cSAdrian Hunter if (err) 4271c65056cSAdrian Hunter return err; 4281c65056cSAdrian Hunter } 4291c65056cSAdrian Hunter return 0; 4301c65056cSAdrian Hunter } 4311c65056cSAdrian Hunter 4321c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4331c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4341c65056cSAdrian Hunter { 4351c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4361c65056cSAdrian Hunter 4371c65056cSAdrian Hunter if (per_cpu_mmaps) 4381c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4391c65056cSAdrian Hunter else 4401c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4411c65056cSAdrian Hunter } 4421c65056cSAdrian Hunter 443ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4445c581041SArnaldo Carvalho de Melo { 445b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 446b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 447bf8e8f4bSAdrian Hunter int nfds = 0; 448bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 449bf8e8f4bSAdrian Hunter 450cba9b847SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 451bf8e8f4bSAdrian Hunter if (evsel->system_wide) 452bf8e8f4bSAdrian Hunter nfds += nr_cpus; 453bf8e8f4bSAdrian Hunter else 454bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 455bf8e8f4bSAdrian Hunter } 456bf8e8f4bSAdrian Hunter 4571b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4581b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 459ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 460ad6765ddSArnaldo Carvalho de Melo 461ad6765ddSArnaldo Carvalho de Melo return 0; 4625c581041SArnaldo Carvalho de Melo } 46370082dd9SArnaldo Carvalho de Melo 464e4b356b5SArnaldo Carvalho de Melo static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx) 465e4b356b5SArnaldo Carvalho de Melo { 466e4b356b5SArnaldo Carvalho de Melo int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP); 467e4b356b5SArnaldo Carvalho de Melo /* 468e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 469e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 470e4b356b5SArnaldo Carvalho de Melo */ 471e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 472e4b356b5SArnaldo Carvalho de Melo evlist->pollfd.priv[pos].idx = idx; 473e4b356b5SArnaldo Carvalho de Melo 474e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 475e4b356b5SArnaldo Carvalho de Melo } 476e4b356b5SArnaldo Carvalho de Melo 477e4b356b5SArnaldo Carvalho de Melo return pos; 478e4b356b5SArnaldo Carvalho de Melo } 479e4b356b5SArnaldo Carvalho de Melo 480ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 48170082dd9SArnaldo Carvalho de Melo { 482e4b356b5SArnaldo Carvalho de Melo return __perf_evlist__add_pollfd(evlist, fd, -1); 483e4b356b5SArnaldo Carvalho de Melo } 4841b85337dSArnaldo Carvalho de Melo 485e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd) 486e4b356b5SArnaldo Carvalho de Melo { 487e4b356b5SArnaldo Carvalho de Melo struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd); 488e4b356b5SArnaldo Carvalho de Melo 489e4b356b5SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, fda->priv[fd].idx); 49070082dd9SArnaldo Carvalho de Melo } 49170db7533SArnaldo Carvalho de Melo 4921ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4931ddec7f0SArnaldo Carvalho de Melo { 494e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 495e4b356b5SArnaldo Carvalho de Melo perf_evlist__munmap_filtered); 4961ddec7f0SArnaldo Carvalho de Melo } 4971ddec7f0SArnaldo Carvalho de Melo 498f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 499f66a889dSArnaldo Carvalho de Melo { 5001b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 501f66a889dSArnaldo Carvalho de Melo } 502f66a889dSArnaldo Carvalho de Melo 503a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 504a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 5053d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 5063d3b5e95SArnaldo Carvalho de Melo { 5073d3b5e95SArnaldo Carvalho de Melo int hash; 5083d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 5093d3b5e95SArnaldo Carvalho de Melo 5103d3b5e95SArnaldo Carvalho de Melo sid->id = id; 5113d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 5123d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 5133d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 5143d3b5e95SArnaldo Carvalho de Melo } 5153d3b5e95SArnaldo Carvalho de Melo 516a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 517a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 518a91e5431SArnaldo Carvalho de Melo { 519a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 520a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 521a91e5431SArnaldo Carvalho de Melo } 522a91e5431SArnaldo Carvalho de Melo 5231c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist, 5243d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 525f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 526f8a95309SArnaldo Carvalho de Melo { 527f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5283d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 529e2b5abe0SJiri Olsa u64 id; 530e2b5abe0SJiri Olsa int ret; 531e2b5abe0SJiri Olsa 532e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 533e2b5abe0SJiri Olsa if (!ret) 534e2b5abe0SJiri Olsa goto add; 535e2b5abe0SJiri Olsa 536e2b5abe0SJiri Olsa if (errno != ENOTTY) 537e2b5abe0SJiri Olsa return -1; 538e2b5abe0SJiri Olsa 539e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 540f8a95309SArnaldo Carvalho de Melo 541c4861afeSJiri Olsa /* 542c4861afeSJiri Olsa * This way does not work with group format read, so bail 543c4861afeSJiri Olsa * out in that case. 544c4861afeSJiri Olsa */ 545c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 546c4861afeSJiri Olsa return -1; 547c4861afeSJiri Olsa 548f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 549f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 550f8a95309SArnaldo Carvalho de Melo return -1; 551f8a95309SArnaldo Carvalho de Melo 552f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 553f8a95309SArnaldo Carvalho de Melo ++id_idx; 554f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 555f8a95309SArnaldo Carvalho de Melo ++id_idx; 556f8a95309SArnaldo Carvalho de Melo 557e2b5abe0SJiri Olsa id = read_data[id_idx]; 558e2b5abe0SJiri Olsa 559e2b5abe0SJiri Olsa add: 560e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 561f8a95309SArnaldo Carvalho de Melo return 0; 562f8a95309SArnaldo Carvalho de Melo } 563f8a95309SArnaldo Carvalho de Melo 5643c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5653c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5663c659eedSAdrian Hunter int thread) 5673c659eedSAdrian Hunter { 5683c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5693c659eedSAdrian Hunter sid->idx = idx; 5703c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5713c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5723c659eedSAdrian Hunter else 5733c659eedSAdrian Hunter sid->cpu = -1; 5743c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 575e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5763c659eedSAdrian Hunter else 5773c659eedSAdrian Hunter sid->tid = -1; 5783c659eedSAdrian Hunter } 5793c659eedSAdrian Hunter 580932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 58170db7533SArnaldo Carvalho de Melo { 58270db7533SArnaldo Carvalho de Melo struct hlist_head *head; 58370db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 58470db7533SArnaldo Carvalho de Melo int hash; 58570db7533SArnaldo Carvalho de Melo 58670db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 58770db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 58870db7533SArnaldo Carvalho de Melo 589b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 59070db7533SArnaldo Carvalho de Melo if (sid->id == id) 591932a3594SJiri Olsa return sid; 592932a3594SJiri Olsa 593932a3594SJiri Olsa return NULL; 594932a3594SJiri Olsa } 595932a3594SJiri Olsa 596932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 597932a3594SJiri Olsa { 598932a3594SJiri Olsa struct perf_sample_id *sid; 599932a3594SJiri Olsa 60005169df5SAdrian Hunter if (evlist->nr_entries == 1 || !id) 601932a3594SJiri Olsa return perf_evlist__first(evlist); 602932a3594SJiri Olsa 603932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 604932a3594SJiri Olsa if (sid) 60570db7533SArnaldo Carvalho de Melo return sid->evsel; 60630e68bccSNamhyung Kim 60730e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 6080c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 60930e68bccSNamhyung Kim 61070db7533SArnaldo Carvalho de Melo return NULL; 61170db7533SArnaldo Carvalho de Melo } 61204391debSArnaldo Carvalho de Melo 613dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist, 614dddcf6abSAdrian Hunter u64 id) 615dddcf6abSAdrian Hunter { 616dddcf6abSAdrian Hunter struct perf_sample_id *sid; 617dddcf6abSAdrian Hunter 618dddcf6abSAdrian Hunter if (!id) 619dddcf6abSAdrian Hunter return NULL; 620dddcf6abSAdrian Hunter 621dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 622dddcf6abSAdrian Hunter if (sid) 623dddcf6abSAdrian Hunter return sid->evsel; 624dddcf6abSAdrian Hunter 625dddcf6abSAdrian Hunter return NULL; 626dddcf6abSAdrian Hunter } 627dddcf6abSAdrian Hunter 62875562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 62975562573SAdrian Hunter union perf_event *event, u64 *id) 63075562573SAdrian Hunter { 63175562573SAdrian Hunter const u64 *array = event->sample.array; 63275562573SAdrian Hunter ssize_t n; 63375562573SAdrian Hunter 63475562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 63575562573SAdrian Hunter 63675562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 63775562573SAdrian Hunter if (evlist->id_pos >= n) 63875562573SAdrian Hunter return -1; 63975562573SAdrian Hunter *id = array[evlist->id_pos]; 64075562573SAdrian Hunter } else { 64175562573SAdrian Hunter if (evlist->is_pos > n) 64275562573SAdrian Hunter return -1; 64375562573SAdrian Hunter n -= evlist->is_pos; 64475562573SAdrian Hunter *id = array[n]; 64575562573SAdrian Hunter } 64675562573SAdrian Hunter return 0; 64775562573SAdrian Hunter } 64875562573SAdrian Hunter 64975562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 65075562573SAdrian Hunter union perf_event *event) 65175562573SAdrian Hunter { 65298be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 65375562573SAdrian Hunter struct hlist_head *head; 65475562573SAdrian Hunter struct perf_sample_id *sid; 65575562573SAdrian Hunter int hash; 65675562573SAdrian Hunter u64 id; 65775562573SAdrian Hunter 65875562573SAdrian Hunter if (evlist->nr_entries == 1) 65998be6966SAdrian Hunter return first; 66098be6966SAdrian Hunter 66198be6966SAdrian Hunter if (!first->attr.sample_id_all && 66298be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 66398be6966SAdrian Hunter return first; 66475562573SAdrian Hunter 66575562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 66675562573SAdrian Hunter return NULL; 66775562573SAdrian Hunter 66875562573SAdrian Hunter /* Synthesized events have an id of zero */ 66975562573SAdrian Hunter if (!id) 67098be6966SAdrian Hunter return first; 67175562573SAdrian Hunter 67275562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 67375562573SAdrian Hunter head = &evlist->heads[hash]; 67475562573SAdrian Hunter 67575562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 67675562573SAdrian Hunter if (sid->id == id) 67775562573SAdrian Hunter return sid->evsel; 67875562573SAdrian Hunter } 67975562573SAdrian Hunter return NULL; 68075562573SAdrian Hunter } 68175562573SAdrian Hunter 6820f4ccd11SWang Nan static union perf_event * 6830f4ccd11SWang Nan perf_mmap__read(struct perf_mmap *md, bool overwrite, u64 head, 6840f4ccd11SWang Nan u64 old, u64 *prev) 68504391debSArnaldo Carvalho de Melo { 68604391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 6878115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 6880f4ccd11SWang Nan int diff = head - old; 68904391debSArnaldo Carvalho de Melo 6900f4ccd11SWang Nan if (overwrite) { 69104391debSArnaldo Carvalho de Melo /* 69204391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 69304391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 69404391debSArnaldo Carvalho de Melo * 69504391debSArnaldo Carvalho de Melo * If we somehow ended up ahead of the head, we got messed up. 69604391debSArnaldo Carvalho de Melo * 69704391debSArnaldo Carvalho de Melo * In either case, truncate and restart at head. 69804391debSArnaldo Carvalho de Melo */ 69904391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 70004391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 70104391debSArnaldo Carvalho de Melo 70204391debSArnaldo Carvalho de Melo /* 70304391debSArnaldo Carvalho de Melo * head points to a known good entry, start there. 70404391debSArnaldo Carvalho de Melo */ 70504391debSArnaldo Carvalho de Melo old = head; 706b04b7023SWang Nan diff = 0; 70704391debSArnaldo Carvalho de Melo } 7087bb41152SArnaldo Carvalho de Melo } 70904391debSArnaldo Carvalho de Melo 710b04b7023SWang Nan if (diff >= (int)sizeof(event->header)) { 71104391debSArnaldo Carvalho de Melo size_t size; 71204391debSArnaldo Carvalho de Melo 7138115d60cSArnaldo Carvalho de Melo event = (union perf_event *)&data[old & md->mask]; 71404391debSArnaldo Carvalho de Melo size = event->header.size; 71504391debSArnaldo Carvalho de Melo 716b04b7023SWang Nan if (size < sizeof(event->header) || diff < (int)size) { 717b04b7023SWang Nan event = NULL; 718b04b7023SWang Nan goto broken_event; 719b04b7023SWang Nan } 720b04b7023SWang Nan 72104391debSArnaldo Carvalho de Melo /* 72204391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 72304391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 72404391debSArnaldo Carvalho de Melo */ 72504391debSArnaldo Carvalho de Melo if ((old & md->mask) + size != ((old + size) & md->mask)) { 72604391debSArnaldo Carvalho de Melo unsigned int offset = old; 72704391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 728a65cb4b9SJiri Olsa void *dst = md->event_copy; 72904391debSArnaldo Carvalho de Melo 73004391debSArnaldo Carvalho de Melo do { 73104391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 73204391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 73304391debSArnaldo Carvalho de Melo offset += cpy; 73404391debSArnaldo Carvalho de Melo dst += cpy; 73504391debSArnaldo Carvalho de Melo len -= cpy; 73604391debSArnaldo Carvalho de Melo } while (len); 73704391debSArnaldo Carvalho de Melo 738a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 73904391debSArnaldo Carvalho de Melo } 74004391debSArnaldo Carvalho de Melo 74104391debSArnaldo Carvalho de Melo old += size; 74204391debSArnaldo Carvalho de Melo } 74304391debSArnaldo Carvalho de Melo 744b04b7023SWang Nan broken_event: 7450f4ccd11SWang Nan if (prev) 7460f4ccd11SWang Nan *prev = old; 7477bb41152SArnaldo Carvalho de Melo 74804391debSArnaldo Carvalho de Melo return event; 74904391debSArnaldo Carvalho de Melo } 750f8a95309SArnaldo Carvalho de Melo 7510f4ccd11SWang Nan union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 7520f4ccd11SWang Nan { 7530f4ccd11SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 7540f4ccd11SWang Nan u64 head; 7550f4ccd11SWang Nan u64 old = md->prev; 7560f4ccd11SWang Nan 7570f4ccd11SWang Nan /* 7580f4ccd11SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 7590f4ccd11SWang Nan */ 7600f4ccd11SWang Nan if (!atomic_read(&md->refcnt)) 7610f4ccd11SWang Nan return NULL; 7620f4ccd11SWang Nan 7630f4ccd11SWang Nan head = perf_mmap__read_head(md); 7640f4ccd11SWang Nan 7650f4ccd11SWang Nan return perf_mmap__read(md, evlist->overwrite, head, old, &md->prev); 7660f4ccd11SWang Nan } 7670f4ccd11SWang Nan 76882396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 76982396986SArnaldo Carvalho de Melo { 770b72e74d1SAdrian Hunter return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base; 77182396986SArnaldo Carvalho de Melo } 77282396986SArnaldo Carvalho de Melo 77382396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx) 77482396986SArnaldo Carvalho de Melo { 7757143849aSArnaldo Carvalho de Melo atomic_inc(&evlist->mmap[idx].refcnt); 77682396986SArnaldo Carvalho de Melo } 77782396986SArnaldo Carvalho de Melo 77882396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx) 77982396986SArnaldo Carvalho de Melo { 7807143849aSArnaldo Carvalho de Melo BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0); 78182396986SArnaldo Carvalho de Melo 7827143849aSArnaldo Carvalho de Melo if (atomic_dec_and_test(&evlist->mmap[idx].refcnt)) 78382396986SArnaldo Carvalho de Melo __perf_evlist__munmap(evlist, idx); 78482396986SArnaldo Carvalho de Melo } 78582396986SArnaldo Carvalho de Melo 7868e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 7878e50d384SZhouyi Zhou { 7888e50d384SZhouyi Zhou struct perf_mmap *md = &evlist->mmap[idx]; 78982396986SArnaldo Carvalho de Melo 79082396986SArnaldo Carvalho de Melo if (!evlist->overwrite) { 7917b8283b5SDavid Ahern u64 old = md->prev; 7928e50d384SZhouyi Zhou 7938e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 7948e50d384SZhouyi Zhou } 79582396986SArnaldo Carvalho de Melo 7967143849aSArnaldo Carvalho de Melo if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md)) 79782396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 7988e50d384SZhouyi Zhou } 7998e50d384SZhouyi Zhou 800718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, 801718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 802718c602dSAdrian Hunter void *userpg __maybe_unused, 803718c602dSAdrian Hunter int fd __maybe_unused) 804718c602dSAdrian Hunter { 805718c602dSAdrian Hunter return 0; 806718c602dSAdrian Hunter } 807718c602dSAdrian Hunter 808718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) 809718c602dSAdrian Hunter { 810718c602dSAdrian Hunter } 811718c602dSAdrian Hunter 812718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init( 813718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 814718c602dSAdrian Hunter off_t auxtrace_offset __maybe_unused, 815718c602dSAdrian Hunter unsigned int auxtrace_pages __maybe_unused, 816718c602dSAdrian Hunter bool auxtrace_overwrite __maybe_unused) 817718c602dSAdrian Hunter { 818718c602dSAdrian Hunter } 819718c602dSAdrian Hunter 820718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx( 821718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 822718c602dSAdrian Hunter struct perf_evlist *evlist __maybe_unused, 823718c602dSAdrian Hunter int idx __maybe_unused, 824718c602dSAdrian Hunter bool per_cpu __maybe_unused) 825718c602dSAdrian Hunter { 826718c602dSAdrian Hunter } 827718c602dSAdrian Hunter 82893edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 82993edcbd9SAdrian Hunter { 83093edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 83193edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 83293edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 8337143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 0); 83493edcbd9SAdrian Hunter } 835718c602dSAdrian Hunter auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap); 83693edcbd9SAdrian Hunter } 83793edcbd9SAdrian Hunter 8387e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 839f8a95309SArnaldo Carvalho de Melo { 840aece948fSArnaldo Carvalho de Melo int i; 841f8a95309SArnaldo Carvalho de Melo 842983874d1SArnaldo Carvalho de Melo if (evlist->mmap == NULL) 843983874d1SArnaldo Carvalho de Melo return; 844983874d1SArnaldo Carvalho de Melo 84593edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 84693edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 847aece948fSArnaldo Carvalho de Melo 84804662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 849f8a95309SArnaldo Carvalho de Melo } 850f8a95309SArnaldo Carvalho de Melo 851806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 852f8a95309SArnaldo Carvalho de Melo { 853a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 854ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 855b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 856aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 857f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 858f8a95309SArnaldo Carvalho de Melo } 859f8a95309SArnaldo Carvalho de Melo 860a8a8f3ebSAdrian Hunter struct mmap_params { 861a8a8f3ebSAdrian Hunter int prot; 862a8a8f3ebSAdrian Hunter int mask; 863718c602dSAdrian Hunter struct auxtrace_mmap_params auxtrace_mp; 864a8a8f3ebSAdrian Hunter }; 865a8a8f3ebSAdrian Hunter 866a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, 867a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 868f8a95309SArnaldo Carvalho de Melo { 86982396986SArnaldo Carvalho de Melo /* 87082396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 87182396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 87282396986SArnaldo Carvalho de Melo * the ring buffer. 87382396986SArnaldo Carvalho de Melo * 87482396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 87582396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 87682396986SArnaldo Carvalho de Melo * waiting to be consumed. 87782396986SArnaldo Carvalho de Melo * 87882396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 87982396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 88082396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 88182396986SArnaldo Carvalho de Melo */ 8827143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 2); 883aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 884a8a8f3ebSAdrian Hunter evlist->mmap[idx].mask = mp->mask; 885a8a8f3ebSAdrian Hunter evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, 886f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 887301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 88802635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 88902635965SAdrian Hunter errno); 890301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 891f8a95309SArnaldo Carvalho de Melo return -1; 892301b195dSNelson Elhage } 893ad6765ddSArnaldo Carvalho de Melo 894718c602dSAdrian Hunter if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap, 895718c602dSAdrian Hunter &mp->auxtrace_mp, evlist->mmap[idx].base, fd)) 896718c602dSAdrian Hunter return -1; 897718c602dSAdrian Hunter 898f8a95309SArnaldo Carvalho de Melo return 0; 899f8a95309SArnaldo Carvalho de Melo } 900f8a95309SArnaldo Carvalho de Melo 90104e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 902a8a8f3ebSAdrian Hunter struct mmap_params *mp, int cpu, 903a8a8f3ebSAdrian Hunter int thread, int *output) 904aece948fSArnaldo Carvalho de Melo { 905aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 90604e21314SAdrian Hunter 9070050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 908bf8e8f4bSAdrian Hunter int fd; 909bf8e8f4bSAdrian Hunter 910bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 911bf8e8f4bSAdrian Hunter continue; 912bf8e8f4bSAdrian Hunter 913bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 91404e21314SAdrian Hunter 91504e21314SAdrian Hunter if (*output == -1) { 91604e21314SAdrian Hunter *output = fd; 917a8a8f3ebSAdrian Hunter if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) 91804e21314SAdrian Hunter return -1; 91904e21314SAdrian Hunter } else { 92004e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 92104e21314SAdrian Hunter return -1; 92282396986SArnaldo Carvalho de Melo 92382396986SArnaldo Carvalho de Melo perf_evlist__mmap_get(evlist, idx); 92404e21314SAdrian Hunter } 92504e21314SAdrian Hunter 926f90d194aSAdrian Hunter /* 927f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 928f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 929f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 930f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 931f90d194aSAdrian Hunter * Therefore don't add it for polling. 932f90d194aSAdrian Hunter */ 933f90d194aSAdrian Hunter if (!evsel->system_wide && 934f90d194aSAdrian Hunter __perf_evlist__add_pollfd(evlist, fd, idx) < 0) { 93582396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 936ad6765ddSArnaldo Carvalho de Melo return -1; 93782396986SArnaldo Carvalho de Melo } 938033fa713SArnaldo Carvalho de Melo 9393c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 9403c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 9413c659eedSAdrian Hunter fd) < 0) 94204e21314SAdrian Hunter return -1; 9433c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 9443c659eedSAdrian Hunter thread); 9453c659eedSAdrian Hunter } 94604e21314SAdrian Hunter } 94704e21314SAdrian Hunter 94804e21314SAdrian Hunter return 0; 94904e21314SAdrian Hunter } 95004e21314SAdrian Hunter 951a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 952a8a8f3ebSAdrian Hunter struct mmap_params *mp) 95304e21314SAdrian Hunter { 954aece948fSArnaldo Carvalho de Melo int cpu, thread; 955b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 956b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 957aece948fSArnaldo Carvalho de Melo 958e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 959b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 960aece948fSArnaldo Carvalho de Melo int output = -1; 961aece948fSArnaldo Carvalho de Melo 962718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 963718c602dSAdrian Hunter true); 964718c602dSAdrian Hunter 965b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 966a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 967a8a8f3ebSAdrian Hunter thread, &output)) 968aece948fSArnaldo Carvalho de Melo goto out_unmap; 969aece948fSArnaldo Carvalho de Melo } 970aece948fSArnaldo Carvalho de Melo } 971aece948fSArnaldo Carvalho de Melo 972aece948fSArnaldo Carvalho de Melo return 0; 973aece948fSArnaldo Carvalho de Melo 974aece948fSArnaldo Carvalho de Melo out_unmap: 97593edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 97693edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 977aece948fSArnaldo Carvalho de Melo return -1; 978aece948fSArnaldo Carvalho de Melo } 979aece948fSArnaldo Carvalho de Melo 980a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 981a8a8f3ebSAdrian Hunter struct mmap_params *mp) 982aece948fSArnaldo Carvalho de Melo { 983aece948fSArnaldo Carvalho de Melo int thread; 984b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 985aece948fSArnaldo Carvalho de Melo 986e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 987b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 988aece948fSArnaldo Carvalho de Melo int output = -1; 989aece948fSArnaldo Carvalho de Melo 990718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 991718c602dSAdrian Hunter false); 992718c602dSAdrian Hunter 993a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 994a8a8f3ebSAdrian Hunter &output)) 995aece948fSArnaldo Carvalho de Melo goto out_unmap; 996aece948fSArnaldo Carvalho de Melo } 997aece948fSArnaldo Carvalho de Melo 998aece948fSArnaldo Carvalho de Melo return 0; 999aece948fSArnaldo Carvalho de Melo 1000aece948fSArnaldo Carvalho de Melo out_unmap: 100193edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 100293edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 1003aece948fSArnaldo Carvalho de Melo return -1; 1004aece948fSArnaldo Carvalho de Melo } 1005aece948fSArnaldo Carvalho de Melo 1006f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 1007994a1f78SJiri Olsa { 1008f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 10098185e881SArnaldo Carvalho de Melo int max; 10108185e881SArnaldo Carvalho de Melo 10118185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 10128185e881SArnaldo Carvalho de Melo /* 10138185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 10148185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 10158185e881SArnaldo Carvalho de Melo * die yet... 10168185e881SArnaldo Carvalho de Melo */ 10178185e881SArnaldo Carvalho de Melo max = 512; 10188185e881SArnaldo Carvalho de Melo } else { 10198185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 10208185e881SArnaldo Carvalho de Melo } 10218185e881SArnaldo Carvalho de Melo 10228185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 10231be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 10241be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 1025f5e7150cSArnaldo Carvalho de Melo 1026f5e7150cSArnaldo Carvalho de Melo return pages; 1027f5e7150cSArnaldo Carvalho de Melo } 1028f5e7150cSArnaldo Carvalho de Melo 1029f5e7150cSArnaldo Carvalho de Melo static size_t perf_evlist__mmap_size(unsigned long pages) 1030f5e7150cSArnaldo Carvalho de Melo { 1031f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 1032f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 1033f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 1034994a1f78SJiri Olsa return 0; 1035994a1f78SJiri Olsa 1036994a1f78SJiri Olsa return (pages + 1) * page_size; 1037994a1f78SJiri Olsa } 1038994a1f78SJiri Olsa 103933c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 104033c2dcfdSDavid Ahern unsigned long max) 1041994a1f78SJiri Olsa { 10422fbe4abeSAdrian Hunter unsigned long pages, val; 104327050f53SJiri Olsa static struct parse_tag tags[] = { 104427050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 104527050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 104627050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 104727050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 104827050f53SJiri Olsa { .tag = 0 }, 104927050f53SJiri Olsa }; 1050994a1f78SJiri Olsa 10518973504bSDavid Ahern if (str == NULL) 105233c2dcfdSDavid Ahern return -EINVAL; 10538973504bSDavid Ahern 105427050f53SJiri Olsa val = parse_tag_value(str, tags); 10552fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 105627050f53SJiri Olsa /* we got file size value */ 105727050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 105827050f53SJiri Olsa } else { 105927050f53SJiri Olsa /* we got pages count value */ 106027050f53SJiri Olsa char *eptr; 1061994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 106233c2dcfdSDavid Ahern if (*eptr != '\0') 106333c2dcfdSDavid Ahern return -EINVAL; 106427050f53SJiri Olsa } 1065994a1f78SJiri Olsa 10662bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 106733c2dcfdSDavid Ahern /* leave number of pages at 0 */ 10681dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 106933c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 107091529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 10711dbfa938SAdrian Hunter if (!pages) 10721dbfa938SAdrian Hunter return -EINVAL; 10739639837eSDavid Ahern pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 10749639837eSDavid Ahern pages * page_size, pages); 10752fbe4abeSAdrian Hunter } 10762fbe4abeSAdrian Hunter 107733c2dcfdSDavid Ahern if (pages > max) 107833c2dcfdSDavid Ahern return -EINVAL; 107933c2dcfdSDavid Ahern 108033c2dcfdSDavid Ahern return pages; 108133c2dcfdSDavid Ahern } 108233c2dcfdSDavid Ahern 1083e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 108433c2dcfdSDavid Ahern { 108533c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 108633c2dcfdSDavid Ahern long pages; 108733c2dcfdSDavid Ahern 1088f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 108933c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 109033c2dcfdSDavid Ahern 109133c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 109233c2dcfdSDavid Ahern if (pages < 0) { 109333c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 1094994a1f78SJiri Olsa return -1; 1095994a1f78SJiri Olsa } 1096994a1f78SJiri Olsa 1097994a1f78SJiri Olsa *mmap_pages = pages; 1098994a1f78SJiri Olsa return 0; 1099994a1f78SJiri Olsa } 1100994a1f78SJiri Olsa 1101e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 1102e9db1310SAdrian Hunter int unset __maybe_unused) 1103e9db1310SAdrian Hunter { 1104e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 1105e9db1310SAdrian Hunter } 1106e9db1310SAdrian Hunter 1107c83fa7f2SAdrian Hunter /** 1108718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 1109c83fa7f2SAdrian Hunter * @evlist: list of events 1110c83fa7f2SAdrian Hunter * @pages: map length in pages 1111c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 1112718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 1113718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1114f8a95309SArnaldo Carvalho de Melo * 1115c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1116c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1117c83fa7f2SAdrian Hunter * automatically. 1118f8a95309SArnaldo Carvalho de Melo * 1119718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1120718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1121718c602dSAdrian Hunter * 1122c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1123f8a95309SArnaldo Carvalho de Melo */ 1124718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, 1125718c602dSAdrian Hunter bool overwrite, unsigned int auxtrace_pages, 1126718c602dSAdrian Hunter bool auxtrace_overwrite) 1127f8a95309SArnaldo Carvalho de Melo { 1128aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 11297e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 11307e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1131a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1132a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1133a8a8f3ebSAdrian Hunter }; 113450a682ceSArnaldo Carvalho de Melo 11357e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 1136f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1137f8a95309SArnaldo Carvalho de Melo 11381b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1139f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1140f8a95309SArnaldo Carvalho de Melo 1141f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1142994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 11432af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1144a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1145f8a95309SArnaldo Carvalho de Melo 1146718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1147718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1148718c602dSAdrian Hunter 11490050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1150f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1151a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1152a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1153f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1154f8a95309SArnaldo Carvalho de Melo } 1155f8a95309SArnaldo Carvalho de Melo 1156ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1157a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1158f8a95309SArnaldo Carvalho de Melo 1159a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1160f8a95309SArnaldo Carvalho de Melo } 11617e2ed097SArnaldo Carvalho de Melo 1162718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 1163718c602dSAdrian Hunter bool overwrite) 1164718c602dSAdrian Hunter { 1165718c602dSAdrian Hunter return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false); 1166718c602dSAdrian Hunter } 1167718c602dSAdrian Hunter 1168602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 11697e2ed097SArnaldo Carvalho de Melo { 117074bfd2b2SAdrian Hunter struct cpu_map *cpus; 117174bfd2b2SAdrian Hunter struct thread_map *threads; 11727e2ed097SArnaldo Carvalho de Melo 117374bfd2b2SAdrian Hunter threads = thread_map__new_str(target->pid, target->tid, target->uid); 117474bfd2b2SAdrian Hunter 117574bfd2b2SAdrian Hunter if (!threads) 11767e2ed097SArnaldo Carvalho de Melo return -1; 11777e2ed097SArnaldo Carvalho de Melo 11789c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 117974bfd2b2SAdrian Hunter cpus = cpu_map__dummy_new(); 1180879d77d0SNamhyung Kim else 118174bfd2b2SAdrian Hunter cpus = cpu_map__new(target->cpu_list); 11827e2ed097SArnaldo Carvalho de Melo 118374bfd2b2SAdrian Hunter if (!cpus) 11847e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 11857e2ed097SArnaldo Carvalho de Melo 1186ec9a77a7SAdrian Hunter evlist->has_user_cpus = !!target->cpu_list; 1187ec9a77a7SAdrian Hunter 118874bfd2b2SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 1189d5bc056eSAdrian Hunter 1190d5bc056eSAdrian Hunter return 0; 11917e2ed097SArnaldo Carvalho de Melo 11927e2ed097SArnaldo Carvalho de Melo out_delete_threads: 119374bfd2b2SAdrian Hunter thread_map__put(threads); 11947e2ed097SArnaldo Carvalho de Melo return -1; 11957e2ed097SArnaldo Carvalho de Melo } 11967e2ed097SArnaldo Carvalho de Melo 1197d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, 11983de5cfb0SJiri Olsa struct thread_map *threads) 11993de5cfb0SJiri Olsa { 1200934e0f20SAdrian Hunter /* 1201934e0f20SAdrian Hunter * Allow for the possibility that one or another of the maps isn't being 1202934e0f20SAdrian Hunter * changed i.e. don't put it. Note we are assuming the maps that are 1203934e0f20SAdrian Hunter * being applied are brand new and evlist is taking ownership of the 1204934e0f20SAdrian Hunter * original reference count of 1. If that is not the case it is up to 1205934e0f20SAdrian Hunter * the caller to increase the reference count. 1206934e0f20SAdrian Hunter */ 1207934e0f20SAdrian Hunter if (cpus != evlist->cpus) { 12083de5cfb0SJiri Olsa cpu_map__put(evlist->cpus); 1209a55e5663SArnaldo Carvalho de Melo evlist->cpus = cpu_map__get(cpus); 1210934e0f20SAdrian Hunter } 12113de5cfb0SJiri Olsa 1212934e0f20SAdrian Hunter if (threads != evlist->threads) { 12133de5cfb0SJiri Olsa thread_map__put(evlist->threads); 1214a55e5663SArnaldo Carvalho de Melo evlist->threads = thread_map__get(threads); 1215934e0f20SAdrian Hunter } 12163de5cfb0SJiri Olsa 1217ec9a77a7SAdrian Hunter perf_evlist__propagate_maps(evlist); 12183de5cfb0SJiri Olsa } 12193de5cfb0SJiri Olsa 122022c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist, 122122c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 122222c8a376SArnaldo Carvalho de Melo { 122322c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 122422c8a376SArnaldo Carvalho de Melo 122522c8a376SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 122622c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 122722c8a376SArnaldo Carvalho de Melo } 122822c8a376SArnaldo Carvalho de Melo 122922c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist, 123022c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 123122c8a376SArnaldo Carvalho de Melo { 123222c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 123322c8a376SArnaldo Carvalho de Melo 123422c8a376SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 123522c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 123622c8a376SArnaldo Carvalho de Melo } 123722c8a376SArnaldo Carvalho de Melo 123823d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 12390a102479SFrederic Weisbecker { 12400a102479SFrederic Weisbecker struct perf_evsel *evsel; 1241745cefc5SArnaldo Carvalho de Melo int err = 0; 1242745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1243b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 12440a102479SFrederic Weisbecker 12450050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1246745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 12470a102479SFrederic Weisbecker continue; 1248745cefc5SArnaldo Carvalho de Melo 1249d988d5eeSKan Liang /* 1250d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1251d988d5eeSKan Liang * So evlist and evsel should always be same. 1252d988d5eeSKan Liang */ 1253f47805a2SArnaldo Carvalho de Melo err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter); 125423d4aad4SArnaldo Carvalho de Melo if (err) { 125523d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1256745cefc5SArnaldo Carvalho de Melo break; 12570a102479SFrederic Weisbecker } 125823d4aad4SArnaldo Carvalho de Melo } 12590a102479SFrederic Weisbecker 1260745cefc5SArnaldo Carvalho de Melo return err; 1261745cefc5SArnaldo Carvalho de Melo } 1262745cefc5SArnaldo Carvalho de Melo 1263745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1264745cefc5SArnaldo Carvalho de Melo { 1265745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1266745cefc5SArnaldo Carvalho de Melo int err = 0; 1267745cefc5SArnaldo Carvalho de Melo 12680050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1269fdf14720SWang Nan if (evsel->attr.type != PERF_TYPE_TRACEPOINT) 1270fdf14720SWang Nan continue; 1271fdf14720SWang Nan 127294ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1273745cefc5SArnaldo Carvalho de Melo if (err) 1274745cefc5SArnaldo Carvalho de Melo break; 1275745cefc5SArnaldo Carvalho de Melo } 1276745cefc5SArnaldo Carvalho de Melo 1277745cefc5SArnaldo Carvalho de Melo return err; 12780a102479SFrederic Weisbecker } 127974429964SFrederic Weisbecker 1280be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1281cfd70a26SArnaldo Carvalho de Melo { 1282cfd70a26SArnaldo Carvalho de Melo char *filter; 1283be199adaSArnaldo Carvalho de Melo int ret = -1; 1284be199adaSArnaldo Carvalho de Melo size_t i; 1285cfd70a26SArnaldo Carvalho de Melo 1286be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1287be199adaSArnaldo Carvalho de Melo if (i == 0) { 1288be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1289cfd70a26SArnaldo Carvalho de Melo return -1; 1290be199adaSArnaldo Carvalho de Melo } else { 1291be199adaSArnaldo Carvalho de Melo char *tmp; 1292be199adaSArnaldo Carvalho de Melo 1293be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1294be199adaSArnaldo Carvalho de Melo goto out_free; 1295be199adaSArnaldo Carvalho de Melo 1296be199adaSArnaldo Carvalho de Melo free(filter); 1297be199adaSArnaldo Carvalho de Melo filter = tmp; 1298be199adaSArnaldo Carvalho de Melo } 1299be199adaSArnaldo Carvalho de Melo } 1300cfd70a26SArnaldo Carvalho de Melo 1301cfd70a26SArnaldo Carvalho de Melo ret = perf_evlist__set_filter(evlist, filter); 1302be199adaSArnaldo Carvalho de Melo out_free: 1303cfd70a26SArnaldo Carvalho de Melo free(filter); 1304cfd70a26SArnaldo Carvalho de Melo return ret; 1305cfd70a26SArnaldo Carvalho de Melo } 1306cfd70a26SArnaldo Carvalho de Melo 1307be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) 1308be199adaSArnaldo Carvalho de Melo { 1309be199adaSArnaldo Carvalho de Melo return perf_evlist__set_filter_pids(evlist, 1, &pid); 1310be199adaSArnaldo Carvalho de Melo } 1311be199adaSArnaldo Carvalho de Melo 13120c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 131374429964SFrederic Weisbecker { 131475562573SAdrian Hunter struct perf_evsel *pos; 1315c2a70653SArnaldo Carvalho de Melo 131675562573SAdrian Hunter if (evlist->nr_entries == 1) 131775562573SAdrian Hunter return true; 131875562573SAdrian Hunter 131975562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 132075562573SAdrian Hunter return false; 132175562573SAdrian Hunter 13220050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 132375562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 132475562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1325c2a70653SArnaldo Carvalho de Melo return false; 132674429964SFrederic Weisbecker } 132774429964SFrederic Weisbecker 1328c2a70653SArnaldo Carvalho de Melo return true; 1329c2a70653SArnaldo Carvalho de Melo } 1330c2a70653SArnaldo Carvalho de Melo 133175562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1332c2a70653SArnaldo Carvalho de Melo { 133375562573SAdrian Hunter struct perf_evsel *evsel; 133475562573SAdrian Hunter 133575562573SAdrian Hunter if (evlist->combined_sample_type) 133675562573SAdrian Hunter return evlist->combined_sample_type; 133775562573SAdrian Hunter 13380050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 133975562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 134075562573SAdrian Hunter 134175562573SAdrian Hunter return evlist->combined_sample_type; 134275562573SAdrian Hunter } 134375562573SAdrian Hunter 134475562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 134575562573SAdrian Hunter { 134675562573SAdrian Hunter evlist->combined_sample_type = 0; 134775562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1348c2a70653SArnaldo Carvalho de Melo } 1349c2a70653SArnaldo Carvalho de Melo 135098df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) 135198df858eSAndi Kleen { 135298df858eSAndi Kleen struct perf_evsel *evsel; 135398df858eSAndi Kleen u64 branch_type = 0; 135498df858eSAndi Kleen 135598df858eSAndi Kleen evlist__for_each(evlist, evsel) 135698df858eSAndi Kleen branch_type |= evsel->attr.branch_sample_type; 135798df858eSAndi Kleen return branch_type; 135898df858eSAndi Kleen } 135998df858eSAndi Kleen 13609ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 13619ede473cSJiri Olsa { 13629ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 13639ede473cSJiri Olsa u64 read_format = first->attr.read_format; 13649ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 13659ede473cSJiri Olsa 13660050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 13679ede473cSJiri Olsa if (read_format != pos->attr.read_format) 13689ede473cSJiri Olsa return false; 13699ede473cSJiri Olsa } 13709ede473cSJiri Olsa 13719ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 13729ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 13739ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 13749ede473cSJiri Olsa return false; 13759ede473cSJiri Olsa } 13769ede473cSJiri Olsa 13779ede473cSJiri Olsa return true; 13789ede473cSJiri Olsa } 13799ede473cSJiri Olsa 13809ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 13819ede473cSJiri Olsa { 13829ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 13839ede473cSJiri Olsa return first->attr.read_format; 13849ede473cSJiri Olsa } 13859ede473cSJiri Olsa 13860c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 138781e36bffSArnaldo Carvalho de Melo { 13880c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 138981e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 139081e36bffSArnaldo Carvalho de Melo u64 sample_type; 139181e36bffSArnaldo Carvalho de Melo u16 size = 0; 139281e36bffSArnaldo Carvalho de Melo 139381e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 139481e36bffSArnaldo Carvalho de Melo goto out; 139581e36bffSArnaldo Carvalho de Melo 139681e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 139781e36bffSArnaldo Carvalho de Melo 139881e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 139981e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 140081e36bffSArnaldo Carvalho de Melo 140181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 140281e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 140381e36bffSArnaldo Carvalho de Melo 140481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 140581e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 140681e36bffSArnaldo Carvalho de Melo 140781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 140881e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 140981e36bffSArnaldo Carvalho de Melo 141081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 141181e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 141275562573SAdrian Hunter 141375562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 141475562573SAdrian Hunter size += sizeof(data->id); 141581e36bffSArnaldo Carvalho de Melo out: 141681e36bffSArnaldo Carvalho de Melo return size; 141781e36bffSArnaldo Carvalho de Melo } 141881e36bffSArnaldo Carvalho de Melo 14190c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1420c2a70653SArnaldo Carvalho de Melo { 14210c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1422c2a70653SArnaldo Carvalho de Melo 14230050f7aaSArnaldo Carvalho de Melo evlist__for_each_continue(evlist, pos) { 1424c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1425c2a70653SArnaldo Carvalho de Melo return false; 1426c2a70653SArnaldo Carvalho de Melo } 1427c2a70653SArnaldo Carvalho de Melo 1428c2a70653SArnaldo Carvalho de Melo return true; 142974429964SFrederic Weisbecker } 143074429964SFrederic Weisbecker 14310c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 143274429964SFrederic Weisbecker { 14330c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1434c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 143574429964SFrederic Weisbecker } 143681cce8deSArnaldo Carvalho de Melo 143781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 143881cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 143981cce8deSArnaldo Carvalho de Melo { 144081cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 144181cce8deSArnaldo Carvalho de Melo } 1442727ab04eSArnaldo Carvalho de Melo 1443a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1444a74b4b66SNamhyung Kim { 1445a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1446a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1447a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 14488ad9219eSStephane Eranian int n; 1449a74b4b66SNamhyung Kim 14508ad9219eSStephane Eranian evlist__for_each_reverse(evlist, evsel) { 14518ad9219eSStephane Eranian n = evsel->cpus ? evsel->cpus->nr : ncpus; 14528ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 14538ad9219eSStephane Eranian } 1454a74b4b66SNamhyung Kim } 1455a74b4b66SNamhyung Kim 14564112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 14574112eb18SArnaldo Carvalho de Melo { 14588c0498b6SAdrian Hunter struct cpu_map *cpus; 14598c0498b6SAdrian Hunter struct thread_map *threads; 14604112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 14614112eb18SArnaldo Carvalho de Melo 14624112eb18SArnaldo Carvalho de Melo /* 14634112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 14644112eb18SArnaldo Carvalho de Melo * an all cpus map. 14654112eb18SArnaldo Carvalho de Melo * 14664112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 14674112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 14684112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 14694112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 14704112eb18SArnaldo Carvalho de Melo */ 14718c0498b6SAdrian Hunter cpus = cpu_map__new(NULL); 14728c0498b6SAdrian Hunter if (!cpus) 14734112eb18SArnaldo Carvalho de Melo goto out; 14744112eb18SArnaldo Carvalho de Melo 14758c0498b6SAdrian Hunter threads = thread_map__new_dummy(); 14768c0498b6SAdrian Hunter if (!threads) 14778c0498b6SAdrian Hunter goto out_put; 14784112eb18SArnaldo Carvalho de Melo 14798c0498b6SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 14804112eb18SArnaldo Carvalho de Melo out: 14814112eb18SArnaldo Carvalho de Melo return err; 14828c0498b6SAdrian Hunter out_put: 14838c0498b6SAdrian Hunter cpu_map__put(cpus); 14844112eb18SArnaldo Carvalho de Melo goto out; 14854112eb18SArnaldo Carvalho de Melo } 14864112eb18SArnaldo Carvalho de Melo 14876a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1488727ab04eSArnaldo Carvalho de Melo { 14896a4bb04cSJiri Olsa struct perf_evsel *evsel; 1490a74b4b66SNamhyung Kim int err; 1491727ab04eSArnaldo Carvalho de Melo 14924112eb18SArnaldo Carvalho de Melo /* 14934112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 14944112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 14954112eb18SArnaldo Carvalho de Melo */ 14964112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 14974112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 14984112eb18SArnaldo Carvalho de Melo if (err < 0) 14994112eb18SArnaldo Carvalho de Melo goto out_err; 15004112eb18SArnaldo Carvalho de Melo } 15014112eb18SArnaldo Carvalho de Melo 1502733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1503733cd2feSAdrian Hunter 15040050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 150523df7f79SAdrian Hunter err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); 1506727ab04eSArnaldo Carvalho de Melo if (err < 0) 1507727ab04eSArnaldo Carvalho de Melo goto out_err; 1508727ab04eSArnaldo Carvalho de Melo } 1509727ab04eSArnaldo Carvalho de Melo 1510727ab04eSArnaldo Carvalho de Melo return 0; 1511727ab04eSArnaldo Carvalho de Melo out_err: 1512a74b4b66SNamhyung Kim perf_evlist__close(evlist); 151341c21a68SNamhyung Kim errno = -err; 1514727ab04eSArnaldo Carvalho de Melo return err; 1515727ab04eSArnaldo Carvalho de Melo } 151635b9d88eSArnaldo Carvalho de Melo 1517602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 151855e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1519735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 152035b9d88eSArnaldo Carvalho de Melo { 152135b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 152235b9d88eSArnaldo Carvalho de Melo char bf; 152335b9d88eSArnaldo Carvalho de Melo 152435b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 152535b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 152635b9d88eSArnaldo Carvalho de Melo return -1; 152735b9d88eSArnaldo Carvalho de Melo } 152835b9d88eSArnaldo Carvalho de Melo 152935b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 153035b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 153135b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 153235b9d88eSArnaldo Carvalho de Melo } 153335b9d88eSArnaldo Carvalho de Melo 153435b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 153535b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 153635b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 153735b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 153835b9d88eSArnaldo Carvalho de Melo } 153935b9d88eSArnaldo Carvalho de Melo 154035b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 15415f1c4225SArnaldo Carvalho de Melo int ret; 15425f1c4225SArnaldo Carvalho de Melo 1543119fa3c9SNamhyung Kim if (pipe_output) 154435b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 154535b9d88eSArnaldo Carvalho de Melo 15460817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 15470817df08SDavid Ahern 154835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 154935b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 155035b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 155135b9d88eSArnaldo Carvalho de Melo 155235b9d88eSArnaldo Carvalho de Melo /* 155335b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 155435b9d88eSArnaldo Carvalho de Melo */ 155535b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 155635b9d88eSArnaldo Carvalho de Melo 155735b9d88eSArnaldo Carvalho de Melo /* 155835b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 155935b9d88eSArnaldo Carvalho de Melo */ 15605f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 15615f1c4225SArnaldo Carvalho de Melo /* 15625f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 15635f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 15645f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 15655f1c4225SArnaldo Carvalho de Melo * 156620f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 15675f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 15685f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 15695f1c4225SArnaldo Carvalho de Melo * here. 15705f1c4225SArnaldo Carvalho de Melo */ 15715f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 15725f1c4225SArnaldo Carvalho de Melo if (ret == -1) 157335b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 15745f1c4225SArnaldo Carvalho de Melo exit(ret); 15755f1c4225SArnaldo Carvalho de Melo } 157635b9d88eSArnaldo Carvalho de Melo 157735b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 157835b9d88eSArnaldo Carvalho de Melo 1579735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1580f33cbe72SArnaldo Carvalho de Melo union sigval val; 1581f33cbe72SArnaldo Carvalho de Melo 1582f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1583f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 158435b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1585f33cbe72SArnaldo Carvalho de Melo } else 1586f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 158735b9d88eSArnaldo Carvalho de Melo exit(-1); 158835b9d88eSArnaldo Carvalho de Melo } 158935b9d88eSArnaldo Carvalho de Melo 1590735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1591735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1592735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1593735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1594735f7e0bSArnaldo Carvalho de Melo }; 1595735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1596735f7e0bSArnaldo Carvalho de Melo } 1597735f7e0bSArnaldo Carvalho de Melo 15981aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 15991aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 16001aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 16011aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 16021aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 16031aaf63b1SArnaldo Carvalho de Melo } 1604e13798c7SJiri Olsa thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 16051aaf63b1SArnaldo Carvalho de Melo } 160635b9d88eSArnaldo Carvalho de Melo 160735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 160835b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 160935b9d88eSArnaldo Carvalho de Melo /* 161035b9d88eSArnaldo Carvalho de Melo * wait for child to settle 161135b9d88eSArnaldo Carvalho de Melo */ 161235b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 161335b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 161435b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 161535b9d88eSArnaldo Carvalho de Melo } 161635b9d88eSArnaldo Carvalho de Melo 1617bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 161835b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 161935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 162035b9d88eSArnaldo Carvalho de Melo return 0; 162135b9d88eSArnaldo Carvalho de Melo 162235b9d88eSArnaldo Carvalho de Melo out_close_pipes: 162335b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 162435b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 162535b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 162635b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 162735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 162835b9d88eSArnaldo Carvalho de Melo return -1; 162935b9d88eSArnaldo Carvalho de Melo } 163035b9d88eSArnaldo Carvalho de Melo 163135b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 163235b9d88eSArnaldo Carvalho de Melo { 163335b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1634b3824404SDavid Ahern char bf = 0; 1635bcf3145fSNamhyung Kim int ret; 163635b9d88eSArnaldo Carvalho de Melo /* 163735b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 163835b9d88eSArnaldo Carvalho de Melo */ 1639bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1640bcf3145fSNamhyung Kim if (ret < 0) 1641bcf3145fSNamhyung Kim perror("enable to write to pipe"); 1642bcf3145fSNamhyung Kim 1643bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1644bcf3145fSNamhyung Kim return ret; 164535b9d88eSArnaldo Carvalho de Melo } 164635b9d88eSArnaldo Carvalho de Melo 164735b9d88eSArnaldo Carvalho de Melo return 0; 164835b9d88eSArnaldo Carvalho de Melo } 1649cb0b29e0SArnaldo Carvalho de Melo 1650a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 16510807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1652cb0b29e0SArnaldo Carvalho de Melo { 165375562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 165475562573SAdrian Hunter 165575562573SAdrian Hunter if (!evsel) 165675562573SAdrian Hunter return -EFAULT; 16570807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1658cb0b29e0SArnaldo Carvalho de Melo } 165978f067b3SArnaldo Carvalho de Melo 166078f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 166178f067b3SArnaldo Carvalho de Melo { 166278f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 166378f067b3SArnaldo Carvalho de Melo size_t printed = 0; 166478f067b3SArnaldo Carvalho de Melo 16650050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 166678f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 166778f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 166878f067b3SArnaldo Carvalho de Melo } 166978f067b3SArnaldo Carvalho de Melo 1670b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 167178f067b3SArnaldo Carvalho de Melo } 16726ef068cbSArnaldo Carvalho de Melo 1673d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist, 1674a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1675a8f23d8fSArnaldo Carvalho de Melo { 1676a8f23d8fSArnaldo Carvalho de Melo int printed, value; 16776e81c74cSMasami Hiramatsu char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1678a8f23d8fSArnaldo Carvalho de Melo 1679a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1680a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1681a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1682a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1683a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1684a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1685a8f23d8fSArnaldo Carvalho de Melo 16861a47245dSAdrian Hunter value = perf_event_paranoid(); 1687a8f23d8fSArnaldo Carvalho de Melo 1688a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1689a8f23d8fSArnaldo Carvalho de Melo 1690a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1691a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1692a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1693a8f23d8fSArnaldo Carvalho de Melo } 1694a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16955229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1696a8f23d8fSArnaldo Carvalho de Melo 1697a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 16985229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 16995229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1700a8f23d8fSArnaldo Carvalho de Melo break; 1701d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1702d9aade7fSArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1703d9aade7fSArnaldo Carvalho de Melo int max_freq; 1704d9aade7fSArnaldo Carvalho de Melo 1705d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1706d9aade7fSArnaldo Carvalho de Melo goto out_default; 1707d9aade7fSArnaldo Carvalho de Melo 1708d9aade7fSArnaldo Carvalho de Melo if (first->attr.sample_freq < (u64)max_freq) 1709d9aade7fSArnaldo Carvalho de Melo goto out_default; 1710d9aade7fSArnaldo Carvalho de Melo 1711d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1712d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1713d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1714d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1715d9aade7fSArnaldo Carvalho de Melo emsg, max_freq, first->attr.sample_freq); 1716d9aade7fSArnaldo Carvalho de Melo break; 1717d9aade7fSArnaldo Carvalho de Melo } 1718a8f23d8fSArnaldo Carvalho de Melo default: 1719d9aade7fSArnaldo Carvalho de Melo out_default: 1720a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1721a8f23d8fSArnaldo Carvalho de Melo break; 1722a8f23d8fSArnaldo Carvalho de Melo } 1723a8f23d8fSArnaldo Carvalho de Melo 1724a8f23d8fSArnaldo Carvalho de Melo return 0; 1725a8f23d8fSArnaldo Carvalho de Melo } 1726a025e4f0SAdrian Hunter 1727956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1728956fa571SArnaldo Carvalho de Melo { 1729956fa571SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1730e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1731956fa571SArnaldo Carvalho de Melo 1732956fa571SArnaldo Carvalho de Melo switch (err) { 1733956fa571SArnaldo Carvalho de Melo case EPERM: 1734e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1735e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1736e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1737956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1738e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1739e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1740e965bea1SArnaldo Carvalho de Melo 1741e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1742e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1743e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1744e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1745e965bea1SArnaldo Carvalho de Melo } 1746e965bea1SArnaldo Carvalho de Melo 1747e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1748e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1749956fa571SArnaldo Carvalho de Melo break; 1750956fa571SArnaldo Carvalho de Melo default: 1751956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1752956fa571SArnaldo Carvalho de Melo break; 1753956fa571SArnaldo Carvalho de Melo } 1754956fa571SArnaldo Carvalho de Melo 1755956fa571SArnaldo Carvalho de Melo return 0; 1756956fa571SArnaldo Carvalho de Melo } 1757956fa571SArnaldo Carvalho de Melo 1758a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1759a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1760a025e4f0SAdrian Hunter { 1761a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1762a025e4f0SAdrian Hunter LIST_HEAD(move); 1763a025e4f0SAdrian Hunter 1764a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1765a025e4f0SAdrian Hunter return; 1766a025e4f0SAdrian Hunter 17670050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, evsel) { 1768a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1769a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1770a025e4f0SAdrian Hunter } 1771a025e4f0SAdrian Hunter 1772a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1773a025e4f0SAdrian Hunter } 177460b0896cSAdrian Hunter 177560b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 177660b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 177760b0896cSAdrian Hunter { 177860b0896cSAdrian Hunter struct perf_evsel *evsel; 177960b0896cSAdrian Hunter 178060b0896cSAdrian Hunter if (tracking_evsel->tracking) 178160b0896cSAdrian Hunter return; 178260b0896cSAdrian Hunter 178360b0896cSAdrian Hunter evlist__for_each(evlist, evsel) { 178460b0896cSAdrian Hunter if (evsel != tracking_evsel) 178560b0896cSAdrian Hunter evsel->tracking = false; 178660b0896cSAdrian Hunter } 178760b0896cSAdrian Hunter 178860b0896cSAdrian Hunter tracking_evsel->tracking = true; 178960b0896cSAdrian Hunter } 17907630b3e2SWang Nan 17917630b3e2SWang Nan struct perf_evsel * 17927630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, 17937630b3e2SWang Nan const char *str) 17947630b3e2SWang Nan { 17957630b3e2SWang Nan struct perf_evsel *evsel; 17967630b3e2SWang Nan 17977630b3e2SWang Nan evlist__for_each(evlist, evsel) { 17987630b3e2SWang Nan if (!evsel->name) 17997630b3e2SWang Nan continue; 18007630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 18017630b3e2SWang Nan return evsel; 18027630b3e2SWang Nan } 18037630b3e2SWang Nan 18047630b3e2SWang Nan return NULL; 18057630b3e2SWang Nan } 1806