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; 473a62a7b8SWang Nan evlist->backward = false; 48361c99a6SArnaldo Carvalho de Melo } 49361c99a6SArnaldo Carvalho de Melo 50334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void) 51ef1d1af2SArnaldo Carvalho de Melo { 52ef1d1af2SArnaldo Carvalho de Melo struct perf_evlist *evlist = zalloc(sizeof(*evlist)); 53ef1d1af2SArnaldo Carvalho de Melo 54ef1d1af2SArnaldo Carvalho de Melo if (evlist != NULL) 55334fe7a3SNamhyung Kim perf_evlist__init(evlist, NULL, NULL); 56ef1d1af2SArnaldo Carvalho de Melo 57361c99a6SArnaldo Carvalho de Melo return evlist; 58361c99a6SArnaldo Carvalho de Melo } 59361c99a6SArnaldo Carvalho de Melo 60b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void) 61b22d54b0SJiri Olsa { 62b22d54b0SJiri Olsa struct perf_evlist *evlist = perf_evlist__new(); 63b22d54b0SJiri Olsa 64b22d54b0SJiri Olsa if (evlist && perf_evlist__add_default(evlist)) { 65b22d54b0SJiri Olsa perf_evlist__delete(evlist); 66b22d54b0SJiri Olsa evlist = NULL; 67b22d54b0SJiri Olsa } 68b22d54b0SJiri Olsa 69b22d54b0SJiri Olsa return evlist; 70b22d54b0SJiri Olsa } 71b22d54b0SJiri Olsa 725bae0250SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new_dummy(void) 735bae0250SArnaldo Carvalho de Melo { 745bae0250SArnaldo Carvalho de Melo struct perf_evlist *evlist = perf_evlist__new(); 755bae0250SArnaldo Carvalho de Melo 765bae0250SArnaldo Carvalho de Melo if (evlist && perf_evlist__add_dummy(evlist)) { 775bae0250SArnaldo Carvalho de Melo perf_evlist__delete(evlist); 785bae0250SArnaldo Carvalho de Melo evlist = NULL; 795bae0250SArnaldo Carvalho de Melo } 805bae0250SArnaldo Carvalho de Melo 815bae0250SArnaldo Carvalho de Melo return evlist; 825bae0250SArnaldo Carvalho de Melo } 835bae0250SArnaldo Carvalho de Melo 8475562573SAdrian Hunter /** 8575562573SAdrian Hunter * perf_evlist__set_id_pos - set the positions of event ids. 8675562573SAdrian Hunter * @evlist: selected event list 8775562573SAdrian Hunter * 8875562573SAdrian Hunter * Events with compatible sample types all have the same id_pos 8975562573SAdrian Hunter * and is_pos. For convenience, put a copy on evlist. 9075562573SAdrian Hunter */ 9175562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist) 9275562573SAdrian Hunter { 9375562573SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 9475562573SAdrian Hunter 9575562573SAdrian Hunter evlist->id_pos = first->id_pos; 9675562573SAdrian Hunter evlist->is_pos = first->is_pos; 9775562573SAdrian Hunter } 9875562573SAdrian Hunter 99733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist) 100733cd2feSAdrian Hunter { 101733cd2feSAdrian Hunter struct perf_evsel *evsel; 102733cd2feSAdrian Hunter 1030050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 104733cd2feSAdrian Hunter perf_evsel__calc_id_pos(evsel); 105733cd2feSAdrian Hunter 106733cd2feSAdrian Hunter perf_evlist__set_id_pos(evlist); 107733cd2feSAdrian Hunter } 108733cd2feSAdrian Hunter 109361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist) 110361c99a6SArnaldo Carvalho de Melo { 111361c99a6SArnaldo Carvalho de Melo struct perf_evsel *pos, *n; 112361c99a6SArnaldo Carvalho de Melo 1130050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, pos) { 114361c99a6SArnaldo Carvalho de Melo list_del_init(&pos->node); 115d49e4695SArnaldo Carvalho de Melo pos->evlist = NULL; 116361c99a6SArnaldo Carvalho de Melo perf_evsel__delete(pos); 117361c99a6SArnaldo Carvalho de Melo } 118361c99a6SArnaldo Carvalho de Melo 119361c99a6SArnaldo Carvalho de Melo evlist->nr_entries = 0; 120361c99a6SArnaldo Carvalho de Melo } 121361c99a6SArnaldo Carvalho de Melo 122ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist) 123ef1d1af2SArnaldo Carvalho de Melo { 12404662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 1251b85337dSArnaldo Carvalho de Melo fdarray__exit(&evlist->pollfd); 126ef1d1af2SArnaldo Carvalho de Melo } 127ef1d1af2SArnaldo Carvalho de Melo 128361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist) 129361c99a6SArnaldo Carvalho de Melo { 130983874d1SArnaldo Carvalho de Melo perf_evlist__munmap(evlist); 131f26e1c7cSArnaldo Carvalho de Melo perf_evlist__close(evlist); 132f30a79b0SJiri Olsa cpu_map__put(evlist->cpus); 133186fbb74SJiri Olsa thread_map__put(evlist->threads); 13403ad9747SArnaldo Carvalho de Melo evlist->cpus = NULL; 13503ad9747SArnaldo Carvalho de Melo evlist->threads = NULL; 136361c99a6SArnaldo Carvalho de Melo perf_evlist__purge(evlist); 137ef1d1af2SArnaldo Carvalho de Melo perf_evlist__exit(evlist); 138361c99a6SArnaldo Carvalho de Melo free(evlist); 139361c99a6SArnaldo Carvalho de Melo } 140361c99a6SArnaldo Carvalho de Melo 141adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, 142adc0c3e8SAdrian Hunter struct perf_evsel *evsel) 143adc0c3e8SAdrian Hunter { 144adc0c3e8SAdrian Hunter /* 145adc0c3e8SAdrian Hunter * We already have cpus for evsel (via PMU sysfs) so 146adc0c3e8SAdrian Hunter * keep it, if there's no target cpu list defined. 147adc0c3e8SAdrian Hunter */ 148adc0c3e8SAdrian Hunter if (!evsel->own_cpus || evlist->has_user_cpus) { 149adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 150adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evlist->cpus); 151adc0c3e8SAdrian Hunter } else if (evsel->cpus != evsel->own_cpus) { 152adc0c3e8SAdrian Hunter cpu_map__put(evsel->cpus); 153adc0c3e8SAdrian Hunter evsel->cpus = cpu_map__get(evsel->own_cpus); 154adc0c3e8SAdrian Hunter } 155adc0c3e8SAdrian Hunter 156adc0c3e8SAdrian Hunter thread_map__put(evsel->threads); 157adc0c3e8SAdrian Hunter evsel->threads = thread_map__get(evlist->threads); 158adc0c3e8SAdrian Hunter } 159adc0c3e8SAdrian Hunter 160adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist) 161adc0c3e8SAdrian Hunter { 162adc0c3e8SAdrian Hunter struct perf_evsel *evsel; 163adc0c3e8SAdrian Hunter 164adc0c3e8SAdrian Hunter evlist__for_each(evlist, evsel) 165adc0c3e8SAdrian Hunter __perf_evlist__propagate_maps(evlist, evsel); 166adc0c3e8SAdrian Hunter } 167adc0c3e8SAdrian Hunter 168361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) 169361c99a6SArnaldo Carvalho de Melo { 170d49e4695SArnaldo Carvalho de Melo entry->evlist = evlist; 171361c99a6SArnaldo Carvalho de Melo list_add_tail(&entry->node, &evlist->entries); 172ef503831SArnaldo Carvalho de Melo entry->idx = evlist->nr_entries; 17360b0896cSAdrian Hunter entry->tracking = !entry->idx; 174ef503831SArnaldo Carvalho de Melo 17575562573SAdrian Hunter if (!evlist->nr_entries++) 17675562573SAdrian Hunter perf_evlist__set_id_pos(evlist); 17744c42d71SAdrian Hunter 17844c42d71SAdrian Hunter __perf_evlist__propagate_maps(evlist, entry); 179361c99a6SArnaldo Carvalho de Melo } 180361c99a6SArnaldo Carvalho de Melo 1814768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel) 1824768230aSAdrian Hunter { 1834768230aSAdrian Hunter evsel->evlist = NULL; 1844768230aSAdrian Hunter list_del_init(&evsel->node); 1854768230aSAdrian Hunter evlist->nr_entries -= 1; 1864768230aSAdrian Hunter } 1874768230aSAdrian Hunter 1880529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist, 189f114d6efSAdrian Hunter struct list_head *list) 19050d08e47SArnaldo Carvalho de Melo { 191f114d6efSAdrian Hunter struct perf_evsel *evsel, *temp; 19275562573SAdrian Hunter 193f114d6efSAdrian Hunter __evlist__for_each_safe(list, temp, evsel) { 194f114d6efSAdrian Hunter list_del_init(&evsel->node); 195f114d6efSAdrian Hunter perf_evlist__add(evlist, evsel); 196f114d6efSAdrian Hunter } 19750d08e47SArnaldo Carvalho de Melo } 19850d08e47SArnaldo Carvalho de Melo 19963dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list) 20063dab225SArnaldo Carvalho de Melo { 20163dab225SArnaldo Carvalho de Melo struct perf_evsel *evsel, *leader; 20263dab225SArnaldo Carvalho de Melo 20363dab225SArnaldo Carvalho de Melo leader = list_entry(list->next, struct perf_evsel, node); 20497f63e4aSNamhyung Kim evsel = list_entry(list->prev, struct perf_evsel, node); 20597f63e4aSNamhyung Kim 20697f63e4aSNamhyung Kim leader->nr_members = evsel->idx - leader->idx + 1; 20763dab225SArnaldo Carvalho de Melo 2080050f7aaSArnaldo Carvalho de Melo __evlist__for_each(list, evsel) { 20963dab225SArnaldo Carvalho de Melo evsel->leader = leader; 21063dab225SArnaldo Carvalho de Melo } 21163dab225SArnaldo Carvalho de Melo } 21263dab225SArnaldo Carvalho de Melo 21363dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist) 2146a4bb04cSJiri Olsa { 21597f63e4aSNamhyung Kim if (evlist->nr_entries) { 21697f63e4aSNamhyung Kim evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; 21763dab225SArnaldo Carvalho de Melo __perf_evlist__set_leader(&evlist->entries); 2186a4bb04cSJiri Olsa } 21997f63e4aSNamhyung Kim } 2206a4bb04cSJiri Olsa 22145cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr) 2227f8d1adeSArnaldo Carvalho de Melo { 2237f8d1adeSArnaldo Carvalho de Melo attr->precise_ip = 3; 2247f8d1adeSArnaldo Carvalho de Melo 2257f8d1adeSArnaldo Carvalho de Melo while (attr->precise_ip != 0) { 2267f8d1adeSArnaldo Carvalho de Melo int fd = sys_perf_event_open(attr, 0, -1, -1, 0); 2277f8d1adeSArnaldo Carvalho de Melo if (fd != -1) { 2287f8d1adeSArnaldo Carvalho de Melo close(fd); 2297f8d1adeSArnaldo Carvalho de Melo break; 2307f8d1adeSArnaldo Carvalho de Melo } 2317f8d1adeSArnaldo Carvalho de Melo --attr->precise_ip; 2327f8d1adeSArnaldo Carvalho de Melo } 2337f8d1adeSArnaldo Carvalho de Melo } 2347f8d1adeSArnaldo Carvalho de Melo 235361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist) 236361c99a6SArnaldo Carvalho de Melo { 237361c99a6SArnaldo Carvalho de Melo struct perf_event_attr attr = { 238361c99a6SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 239361c99a6SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 240361c99a6SArnaldo Carvalho de Melo }; 2411aed2671SJoerg Roedel struct perf_evsel *evsel; 242361c99a6SArnaldo Carvalho de Melo 2431aed2671SJoerg Roedel event_attr_init(&attr); 2441aed2671SJoerg Roedel 2457f8d1adeSArnaldo Carvalho de Melo perf_event_attr__set_max_precise_ip(&attr); 2467f8d1adeSArnaldo Carvalho de Melo 247ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new(&attr); 248361c99a6SArnaldo Carvalho de Melo if (evsel == NULL) 249cc2d86b0SStephane Eranian goto error; 250cc2d86b0SStephane Eranian 2517f8d1adeSArnaldo Carvalho de Melo /* use asprintf() because free(evsel) assumes name is allocated */ 2527f8d1adeSArnaldo Carvalho de Melo if (asprintf(&evsel->name, "cycles%.*s", 2537f8d1adeSArnaldo Carvalho de Melo attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0) 254cc2d86b0SStephane Eranian goto error_free; 255361c99a6SArnaldo Carvalho de Melo 256361c99a6SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 257361c99a6SArnaldo Carvalho de Melo return 0; 258cc2d86b0SStephane Eranian error_free: 259cc2d86b0SStephane Eranian perf_evsel__delete(evsel); 260cc2d86b0SStephane Eranian error: 261cc2d86b0SStephane Eranian return -ENOMEM; 262361c99a6SArnaldo Carvalho de Melo } 2635c581041SArnaldo Carvalho de Melo 2645bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist) 2655bae0250SArnaldo Carvalho de Melo { 2665bae0250SArnaldo Carvalho de Melo struct perf_event_attr attr = { 2675bae0250SArnaldo Carvalho de Melo .type = PERF_TYPE_SOFTWARE, 2685bae0250SArnaldo Carvalho de Melo .config = PERF_COUNT_SW_DUMMY, 2695bae0250SArnaldo Carvalho de Melo .size = sizeof(attr), /* to capture ABI version */ 2705bae0250SArnaldo Carvalho de Melo }; 2715bae0250SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__new(&attr); 2725bae0250SArnaldo Carvalho de Melo 2735bae0250SArnaldo Carvalho de Melo if (evsel == NULL) 2745bae0250SArnaldo Carvalho de Melo return -ENOMEM; 2755bae0250SArnaldo Carvalho de Melo 2765bae0250SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 2775bae0250SArnaldo Carvalho de Melo return 0; 2785bae0250SArnaldo Carvalho de Melo } 2795bae0250SArnaldo Carvalho de Melo 280e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist, 28150d08e47SArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 28250d08e47SArnaldo Carvalho de Melo { 28350d08e47SArnaldo Carvalho de Melo struct perf_evsel *evsel, *n; 28450d08e47SArnaldo Carvalho de Melo LIST_HEAD(head); 28550d08e47SArnaldo Carvalho de Melo size_t i; 28650d08e47SArnaldo Carvalho de Melo 28750d08e47SArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) { 288ef503831SArnaldo Carvalho de Melo evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); 28950d08e47SArnaldo Carvalho de Melo if (evsel == NULL) 29050d08e47SArnaldo Carvalho de Melo goto out_delete_partial_list; 29150d08e47SArnaldo Carvalho de Melo list_add_tail(&evsel->node, &head); 29250d08e47SArnaldo Carvalho de Melo } 29350d08e47SArnaldo Carvalho de Melo 294f114d6efSAdrian Hunter perf_evlist__splice_list_tail(evlist, &head); 29550d08e47SArnaldo Carvalho de Melo 29650d08e47SArnaldo Carvalho de Melo return 0; 29750d08e47SArnaldo Carvalho de Melo 29850d08e47SArnaldo Carvalho de Melo out_delete_partial_list: 2990050f7aaSArnaldo Carvalho de Melo __evlist__for_each_safe(&head, n, evsel) 30050d08e47SArnaldo Carvalho de Melo perf_evsel__delete(evsel); 30150d08e47SArnaldo Carvalho de Melo return -1; 30250d08e47SArnaldo Carvalho de Melo } 30350d08e47SArnaldo Carvalho de Melo 30479695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, 30579695e1bSArnaldo Carvalho de Melo struct perf_event_attr *attrs, size_t nr_attrs) 30679695e1bSArnaldo Carvalho de Melo { 30779695e1bSArnaldo Carvalho de Melo size_t i; 30879695e1bSArnaldo Carvalho de Melo 30979695e1bSArnaldo Carvalho de Melo for (i = 0; i < nr_attrs; i++) 31079695e1bSArnaldo Carvalho de Melo event_attr_init(attrs + i); 31179695e1bSArnaldo Carvalho de Melo 31279695e1bSArnaldo Carvalho de Melo return perf_evlist__add_attrs(evlist, attrs, nr_attrs); 31379695e1bSArnaldo Carvalho de Melo } 31479695e1bSArnaldo Carvalho de Melo 315da378962SArnaldo Carvalho de Melo struct perf_evsel * 316ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) 317ee29be62SArnaldo Carvalho de Melo { 318ee29be62SArnaldo Carvalho de Melo struct perf_evsel *evsel; 319ee29be62SArnaldo Carvalho de Melo 3200050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 321ee29be62SArnaldo Carvalho de Melo if (evsel->attr.type == PERF_TYPE_TRACEPOINT && 322ee29be62SArnaldo Carvalho de Melo (int)evsel->attr.config == id) 323ee29be62SArnaldo Carvalho de Melo return evsel; 324ee29be62SArnaldo Carvalho de Melo } 325ee29be62SArnaldo Carvalho de Melo 326ee29be62SArnaldo Carvalho de Melo return NULL; 327ee29be62SArnaldo Carvalho de Melo } 328ee29be62SArnaldo Carvalho de Melo 329a2f2804aSDavid Ahern struct perf_evsel * 330a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, 331a2f2804aSDavid Ahern const char *name) 332a2f2804aSDavid Ahern { 333a2f2804aSDavid Ahern struct perf_evsel *evsel; 334a2f2804aSDavid Ahern 3350050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 336a2f2804aSDavid Ahern if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && 337a2f2804aSDavid Ahern (strcmp(evsel->name, name) == 0)) 338a2f2804aSDavid Ahern return evsel; 339a2f2804aSDavid Ahern } 340a2f2804aSDavid Ahern 341a2f2804aSDavid Ahern return NULL; 342a2f2804aSDavid Ahern } 343a2f2804aSDavid Ahern 34439876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist, 34539876e7dSArnaldo Carvalho de Melo const char *sys, const char *name, void *handler) 34639876e7dSArnaldo Carvalho de Melo { 347ef503831SArnaldo Carvalho de Melo struct perf_evsel *evsel = perf_evsel__newtp(sys, name); 34839876e7dSArnaldo Carvalho de Melo 3498dd2a131SJiri Olsa if (IS_ERR(evsel)) 35039876e7dSArnaldo Carvalho de Melo return -1; 35139876e7dSArnaldo Carvalho de Melo 352744a9719SArnaldo Carvalho de Melo evsel->handler = handler; 35339876e7dSArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 35439876e7dSArnaldo Carvalho de Melo return 0; 35539876e7dSArnaldo Carvalho de Melo } 35639876e7dSArnaldo Carvalho de Melo 357bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist, 358bf8e8f4bSAdrian Hunter struct perf_evsel *evsel) 359bf8e8f4bSAdrian Hunter { 360bf8e8f4bSAdrian Hunter if (evsel->system_wide) 361bf8e8f4bSAdrian Hunter return 1; 362bf8e8f4bSAdrian Hunter else 363bf8e8f4bSAdrian Hunter return thread_map__nr(evlist->threads); 364bf8e8f4bSAdrian Hunter } 365bf8e8f4bSAdrian Hunter 3664152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist) 3674152ab37SArnaldo Carvalho de Melo { 3684152ab37SArnaldo Carvalho de Melo struct perf_evsel *pos; 3694152ab37SArnaldo Carvalho de Melo 3700050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 371395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3723fe4430dSJiri Olsa continue; 3733e27c920SJiri Olsa perf_evsel__disable(pos); 3744152ab37SArnaldo Carvalho de Melo } 3752b56bcfbSArnaldo Carvalho de Melo 3762b56bcfbSArnaldo Carvalho de Melo evlist->enabled = false; 3774152ab37SArnaldo Carvalho de Melo } 3784152ab37SArnaldo Carvalho de Melo 379764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist) 380764e16a3SDavid Ahern { 381764e16a3SDavid Ahern struct perf_evsel *pos; 382764e16a3SDavid Ahern 3830050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 384395c3070SAdrian Hunter if (!perf_evsel__is_group_leader(pos) || !pos->fd) 3853fe4430dSJiri Olsa continue; 3863e27c920SJiri Olsa perf_evsel__enable(pos); 387764e16a3SDavid Ahern } 3882b56bcfbSArnaldo Carvalho de Melo 3892b56bcfbSArnaldo Carvalho de Melo evlist->enabled = true; 3902b56bcfbSArnaldo Carvalho de Melo } 3912b56bcfbSArnaldo Carvalho de Melo 3922b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist) 3932b56bcfbSArnaldo Carvalho de Melo { 3942b56bcfbSArnaldo Carvalho de Melo (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); 395764e16a3SDavid Ahern } 396764e16a3SDavid Ahern 3971c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, 3981c65056cSAdrian Hunter struct perf_evsel *evsel, int cpu) 3991c65056cSAdrian Hunter { 4001c65056cSAdrian Hunter int thread, err; 4011c65056cSAdrian Hunter int nr_threads = perf_evlist__nr_threads(evlist, evsel); 4021c65056cSAdrian Hunter 4031c65056cSAdrian Hunter if (!evsel->fd) 4041c65056cSAdrian Hunter return -EINVAL; 4051c65056cSAdrian Hunter 4061c65056cSAdrian Hunter for (thread = 0; thread < nr_threads; thread++) { 4071c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), 4081c65056cSAdrian Hunter PERF_EVENT_IOC_ENABLE, 0); 4091c65056cSAdrian Hunter if (err) 4101c65056cSAdrian Hunter return err; 4111c65056cSAdrian Hunter } 4121c65056cSAdrian Hunter return 0; 4131c65056cSAdrian Hunter } 4141c65056cSAdrian Hunter 4151c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, 4161c65056cSAdrian Hunter struct perf_evsel *evsel, 4171c65056cSAdrian Hunter int thread) 4181c65056cSAdrian Hunter { 4191c65056cSAdrian Hunter int cpu, err; 4201c65056cSAdrian Hunter int nr_cpus = cpu_map__nr(evlist->cpus); 4211c65056cSAdrian Hunter 4221c65056cSAdrian Hunter if (!evsel->fd) 4231c65056cSAdrian Hunter return -EINVAL; 4241c65056cSAdrian Hunter 4251c65056cSAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) { 4261c65056cSAdrian Hunter err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); 4271c65056cSAdrian Hunter if (err) 4281c65056cSAdrian Hunter return err; 4291c65056cSAdrian Hunter } 4301c65056cSAdrian Hunter return 0; 4311c65056cSAdrian Hunter } 4321c65056cSAdrian Hunter 4331c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist, 4341c65056cSAdrian Hunter struct perf_evsel *evsel, int idx) 4351c65056cSAdrian Hunter { 4361c65056cSAdrian Hunter bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); 4371c65056cSAdrian Hunter 4381c65056cSAdrian Hunter if (per_cpu_mmaps) 4391c65056cSAdrian Hunter return perf_evlist__enable_event_cpu(evlist, evsel, idx); 4401c65056cSAdrian Hunter else 4411c65056cSAdrian Hunter return perf_evlist__enable_event_thread(evlist, evsel, idx); 4421c65056cSAdrian Hunter } 4431c65056cSAdrian Hunter 444ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) 4455c581041SArnaldo Carvalho de Melo { 446b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 447b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 448bf8e8f4bSAdrian Hunter int nfds = 0; 449bf8e8f4bSAdrian Hunter struct perf_evsel *evsel; 450bf8e8f4bSAdrian Hunter 451cba9b847SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 452bf8e8f4bSAdrian Hunter if (evsel->system_wide) 453bf8e8f4bSAdrian Hunter nfds += nr_cpus; 454bf8e8f4bSAdrian Hunter else 455bf8e8f4bSAdrian Hunter nfds += nr_cpus * nr_threads; 456bf8e8f4bSAdrian Hunter } 457bf8e8f4bSAdrian Hunter 4581b85337dSArnaldo Carvalho de Melo if (fdarray__available_entries(&evlist->pollfd) < nfds && 4591b85337dSArnaldo Carvalho de Melo fdarray__grow(&evlist->pollfd, nfds) < 0) 460ad6765ddSArnaldo Carvalho de Melo return -ENOMEM; 461ad6765ddSArnaldo Carvalho de Melo 462ad6765ddSArnaldo Carvalho de Melo return 0; 4635c581041SArnaldo Carvalho de Melo } 46470082dd9SArnaldo Carvalho de Melo 465f3058a1cSWang Nan static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx, short revent) 466e4b356b5SArnaldo Carvalho de Melo { 467f3058a1cSWang Nan int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); 468e4b356b5SArnaldo Carvalho de Melo /* 469e4b356b5SArnaldo Carvalho de Melo * Save the idx so that when we filter out fds POLLHUP'ed we can 470e4b356b5SArnaldo Carvalho de Melo * close the associated evlist->mmap[] entry. 471e4b356b5SArnaldo Carvalho de Melo */ 472e4b356b5SArnaldo Carvalho de Melo if (pos >= 0) { 473e4b356b5SArnaldo Carvalho de Melo evlist->pollfd.priv[pos].idx = idx; 474e4b356b5SArnaldo Carvalho de Melo 475e4b356b5SArnaldo Carvalho de Melo fcntl(fd, F_SETFL, O_NONBLOCK); 476e4b356b5SArnaldo Carvalho de Melo } 477e4b356b5SArnaldo Carvalho de Melo 478e4b356b5SArnaldo Carvalho de Melo return pos; 479e4b356b5SArnaldo Carvalho de Melo } 480e4b356b5SArnaldo Carvalho de Melo 481ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) 48270082dd9SArnaldo Carvalho de Melo { 483f3058a1cSWang Nan return __perf_evlist__add_pollfd(evlist, fd, -1, POLLIN); 484e4b356b5SArnaldo Carvalho de Melo } 4851b85337dSArnaldo Carvalho de Melo 486e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd) 487e4b356b5SArnaldo Carvalho de Melo { 488e4b356b5SArnaldo Carvalho de Melo struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd); 489e4b356b5SArnaldo Carvalho de Melo 490e4b356b5SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, fda->priv[fd].idx); 49170082dd9SArnaldo Carvalho de Melo } 49270db7533SArnaldo Carvalho de Melo 4931ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4941ddec7f0SArnaldo Carvalho de Melo { 495e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 496e4b356b5SArnaldo Carvalho de Melo perf_evlist__munmap_filtered); 4971ddec7f0SArnaldo Carvalho de Melo } 4981ddec7f0SArnaldo Carvalho de Melo 499f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 500f66a889dSArnaldo Carvalho de Melo { 5011b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 502f66a889dSArnaldo Carvalho de Melo } 503f66a889dSArnaldo Carvalho de Melo 504a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 505a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 5063d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 5073d3b5e95SArnaldo Carvalho de Melo { 5083d3b5e95SArnaldo Carvalho de Melo int hash; 5093d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 5103d3b5e95SArnaldo Carvalho de Melo 5113d3b5e95SArnaldo Carvalho de Melo sid->id = id; 5123d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 5133d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 5143d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 5153d3b5e95SArnaldo Carvalho de Melo } 5163d3b5e95SArnaldo Carvalho de Melo 517a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 518a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 519a91e5431SArnaldo Carvalho de Melo { 520a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 521a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 522a91e5431SArnaldo Carvalho de Melo } 523a91e5431SArnaldo Carvalho de Melo 5241c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist, 5253d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 526f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 527f8a95309SArnaldo Carvalho de Melo { 528f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5293d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 530e2b5abe0SJiri Olsa u64 id; 531e2b5abe0SJiri Olsa int ret; 532e2b5abe0SJiri Olsa 533e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 534e2b5abe0SJiri Olsa if (!ret) 535e2b5abe0SJiri Olsa goto add; 536e2b5abe0SJiri Olsa 537e2b5abe0SJiri Olsa if (errno != ENOTTY) 538e2b5abe0SJiri Olsa return -1; 539e2b5abe0SJiri Olsa 540e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 541f8a95309SArnaldo Carvalho de Melo 542c4861afeSJiri Olsa /* 543c4861afeSJiri Olsa * This way does not work with group format read, so bail 544c4861afeSJiri Olsa * out in that case. 545c4861afeSJiri Olsa */ 546c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 547c4861afeSJiri Olsa return -1; 548c4861afeSJiri Olsa 549f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 550f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 551f8a95309SArnaldo Carvalho de Melo return -1; 552f8a95309SArnaldo Carvalho de Melo 553f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 554f8a95309SArnaldo Carvalho de Melo ++id_idx; 555f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 556f8a95309SArnaldo Carvalho de Melo ++id_idx; 557f8a95309SArnaldo Carvalho de Melo 558e2b5abe0SJiri Olsa id = read_data[id_idx]; 559e2b5abe0SJiri Olsa 560e2b5abe0SJiri Olsa add: 561e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 562f8a95309SArnaldo Carvalho de Melo return 0; 563f8a95309SArnaldo Carvalho de Melo } 564f8a95309SArnaldo Carvalho de Melo 5653c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5663c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5673c659eedSAdrian Hunter int thread) 5683c659eedSAdrian Hunter { 5693c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5703c659eedSAdrian Hunter sid->idx = idx; 5713c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5723c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5733c659eedSAdrian Hunter else 5743c659eedSAdrian Hunter sid->cpu = -1; 5753c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 576e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5773c659eedSAdrian Hunter else 5783c659eedSAdrian Hunter sid->tid = -1; 5793c659eedSAdrian Hunter } 5803c659eedSAdrian Hunter 581932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 58270db7533SArnaldo Carvalho de Melo { 58370db7533SArnaldo Carvalho de Melo struct hlist_head *head; 58470db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 58570db7533SArnaldo Carvalho de Melo int hash; 58670db7533SArnaldo Carvalho de Melo 58770db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 58870db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 58970db7533SArnaldo Carvalho de Melo 590b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 59170db7533SArnaldo Carvalho de Melo if (sid->id == id) 592932a3594SJiri Olsa return sid; 593932a3594SJiri Olsa 594932a3594SJiri Olsa return NULL; 595932a3594SJiri Olsa } 596932a3594SJiri Olsa 597932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 598932a3594SJiri Olsa { 599932a3594SJiri Olsa struct perf_sample_id *sid; 600932a3594SJiri Olsa 60105169df5SAdrian Hunter if (evlist->nr_entries == 1 || !id) 602932a3594SJiri Olsa return perf_evlist__first(evlist); 603932a3594SJiri Olsa 604932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 605932a3594SJiri Olsa if (sid) 60670db7533SArnaldo Carvalho de Melo return sid->evsel; 60730e68bccSNamhyung Kim 60830e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 6090c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 61030e68bccSNamhyung Kim 61170db7533SArnaldo Carvalho de Melo return NULL; 61270db7533SArnaldo Carvalho de Melo } 61304391debSArnaldo Carvalho de Melo 614dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist, 615dddcf6abSAdrian Hunter u64 id) 616dddcf6abSAdrian Hunter { 617dddcf6abSAdrian Hunter struct perf_sample_id *sid; 618dddcf6abSAdrian Hunter 619dddcf6abSAdrian Hunter if (!id) 620dddcf6abSAdrian Hunter return NULL; 621dddcf6abSAdrian Hunter 622dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 623dddcf6abSAdrian Hunter if (sid) 624dddcf6abSAdrian Hunter return sid->evsel; 625dddcf6abSAdrian Hunter 626dddcf6abSAdrian Hunter return NULL; 627dddcf6abSAdrian Hunter } 628dddcf6abSAdrian Hunter 62975562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 63075562573SAdrian Hunter union perf_event *event, u64 *id) 63175562573SAdrian Hunter { 63275562573SAdrian Hunter const u64 *array = event->sample.array; 63375562573SAdrian Hunter ssize_t n; 63475562573SAdrian Hunter 63575562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 63675562573SAdrian Hunter 63775562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 63875562573SAdrian Hunter if (evlist->id_pos >= n) 63975562573SAdrian Hunter return -1; 64075562573SAdrian Hunter *id = array[evlist->id_pos]; 64175562573SAdrian Hunter } else { 64275562573SAdrian Hunter if (evlist->is_pos > n) 64375562573SAdrian Hunter return -1; 64475562573SAdrian Hunter n -= evlist->is_pos; 64575562573SAdrian Hunter *id = array[n]; 64675562573SAdrian Hunter } 64775562573SAdrian Hunter return 0; 64875562573SAdrian Hunter } 64975562573SAdrian Hunter 65075562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 65175562573SAdrian Hunter union perf_event *event) 65275562573SAdrian Hunter { 65398be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 65475562573SAdrian Hunter struct hlist_head *head; 65575562573SAdrian Hunter struct perf_sample_id *sid; 65675562573SAdrian Hunter int hash; 65775562573SAdrian Hunter u64 id; 65875562573SAdrian Hunter 65975562573SAdrian Hunter if (evlist->nr_entries == 1) 66098be6966SAdrian Hunter return first; 66198be6966SAdrian Hunter 66298be6966SAdrian Hunter if (!first->attr.sample_id_all && 66398be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 66498be6966SAdrian Hunter return first; 66575562573SAdrian Hunter 66675562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 66775562573SAdrian Hunter return NULL; 66875562573SAdrian Hunter 66975562573SAdrian Hunter /* Synthesized events have an id of zero */ 67075562573SAdrian Hunter if (!id) 67198be6966SAdrian Hunter return first; 67275562573SAdrian Hunter 67375562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 67475562573SAdrian Hunter head = &evlist->heads[hash]; 67575562573SAdrian Hunter 67675562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 67775562573SAdrian Hunter if (sid->id == id) 67875562573SAdrian Hunter return sid->evsel; 67975562573SAdrian Hunter } 68075562573SAdrian Hunter return NULL; 68175562573SAdrian Hunter } 68275562573SAdrian Hunter 68365aea233SWang Nan static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value) 68465aea233SWang Nan { 68565aea233SWang Nan int i; 68665aea233SWang Nan 68765aea233SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 68865aea233SWang Nan int fd = evlist->mmap[i].fd; 68965aea233SWang Nan int err; 69065aea233SWang Nan 69165aea233SWang Nan if (fd < 0) 69265aea233SWang Nan continue; 69365aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 69465aea233SWang Nan if (err) 69565aea233SWang Nan return err; 69665aea233SWang Nan } 69765aea233SWang Nan return 0; 69865aea233SWang Nan } 69965aea233SWang Nan 70065aea233SWang Nan int perf_evlist__pause(struct perf_evlist *evlist) 70165aea233SWang Nan { 70265aea233SWang Nan return perf_evlist__set_paused(evlist, true); 70365aea233SWang Nan } 70465aea233SWang Nan 70565aea233SWang Nan int perf_evlist__resume(struct perf_evlist *evlist) 70665aea233SWang Nan { 70765aea233SWang Nan return perf_evlist__set_paused(evlist, false); 70865aea233SWang Nan } 70965aea233SWang Nan 710b6b85dadSWang Nan /* When check_messup is true, 'end' must points to a good entry */ 7110f4ccd11SWang Nan static union perf_event * 712b6b85dadSWang Nan perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start, 713b6b85dadSWang Nan u64 end, u64 *prev) 71404391debSArnaldo Carvalho de Melo { 71504391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 7168115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 717b6b85dadSWang Nan int diff = end - start; 71804391debSArnaldo Carvalho de Melo 719b6b85dadSWang Nan if (check_messup) { 72004391debSArnaldo Carvalho de Melo /* 72104391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 72204391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 72304391debSArnaldo Carvalho de Melo * 724b6b85dadSWang Nan * If we somehow ended up ahead of the 'end', we got messed up. 72504391debSArnaldo Carvalho de Melo * 726b6b85dadSWang Nan * In either case, truncate and restart at 'end'. 72704391debSArnaldo Carvalho de Melo */ 72804391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 72904391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 73004391debSArnaldo Carvalho de Melo 73104391debSArnaldo Carvalho de Melo /* 732b6b85dadSWang Nan * 'end' points to a known good entry, start there. 73304391debSArnaldo Carvalho de Melo */ 734b6b85dadSWang Nan start = end; 735b04b7023SWang Nan diff = 0; 73604391debSArnaldo Carvalho de Melo } 7377bb41152SArnaldo Carvalho de Melo } 73804391debSArnaldo Carvalho de Melo 739b04b7023SWang Nan if (diff >= (int)sizeof(event->header)) { 74004391debSArnaldo Carvalho de Melo size_t size; 74104391debSArnaldo Carvalho de Melo 742b6b85dadSWang Nan event = (union perf_event *)&data[start & md->mask]; 74304391debSArnaldo Carvalho de Melo size = event->header.size; 74404391debSArnaldo Carvalho de Melo 745b04b7023SWang Nan if (size < sizeof(event->header) || diff < (int)size) { 746b04b7023SWang Nan event = NULL; 747b04b7023SWang Nan goto broken_event; 748b04b7023SWang Nan } 749b04b7023SWang Nan 75004391debSArnaldo Carvalho de Melo /* 75104391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 75204391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 75304391debSArnaldo Carvalho de Melo */ 754b6b85dadSWang Nan if ((start & md->mask) + size != ((start + size) & md->mask)) { 755b6b85dadSWang Nan unsigned int offset = start; 75604391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 757a65cb4b9SJiri Olsa void *dst = md->event_copy; 75804391debSArnaldo Carvalho de Melo 75904391debSArnaldo Carvalho de Melo do { 76004391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 76104391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 76204391debSArnaldo Carvalho de Melo offset += cpy; 76304391debSArnaldo Carvalho de Melo dst += cpy; 76404391debSArnaldo Carvalho de Melo len -= cpy; 76504391debSArnaldo Carvalho de Melo } while (len); 76604391debSArnaldo Carvalho de Melo 767a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 76804391debSArnaldo Carvalho de Melo } 76904391debSArnaldo Carvalho de Melo 770b6b85dadSWang Nan start += size; 77104391debSArnaldo Carvalho de Melo } 77204391debSArnaldo Carvalho de Melo 773b04b7023SWang Nan broken_event: 7740f4ccd11SWang Nan if (prev) 775b6b85dadSWang Nan *prev = start; 7767bb41152SArnaldo Carvalho de Melo 77704391debSArnaldo Carvalho de Melo return event; 77804391debSArnaldo Carvalho de Melo } 779f8a95309SArnaldo Carvalho de Melo 7800f4ccd11SWang Nan union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 7810f4ccd11SWang Nan { 7820f4ccd11SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 7830f4ccd11SWang Nan u64 head; 7840f4ccd11SWang Nan u64 old = md->prev; 7850f4ccd11SWang Nan 7860f4ccd11SWang Nan /* 7870f4ccd11SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 7880f4ccd11SWang Nan */ 7890f4ccd11SWang Nan if (!atomic_read(&md->refcnt)) 7900f4ccd11SWang Nan return NULL; 7910f4ccd11SWang Nan 7920f4ccd11SWang Nan head = perf_mmap__read_head(md); 7930f4ccd11SWang Nan 794b6b85dadSWang Nan return perf_mmap__read(md, evlist->overwrite, old, head, &md->prev); 7950f4ccd11SWang Nan } 7960f4ccd11SWang Nan 797e24c7520SWang Nan union perf_event * 798e24c7520SWang Nan perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx) 799e24c7520SWang Nan { 800e24c7520SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 801e24c7520SWang Nan u64 head, end; 802e24c7520SWang Nan u64 start = md->prev; 803e24c7520SWang Nan 804e24c7520SWang Nan /* 805e24c7520SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 806e24c7520SWang Nan */ 807e24c7520SWang Nan if (!atomic_read(&md->refcnt)) 808e24c7520SWang Nan return NULL; 809e24c7520SWang Nan 810e24c7520SWang Nan head = perf_mmap__read_head(md); 811e24c7520SWang Nan if (!head) 812e24c7520SWang Nan return NULL; 813e24c7520SWang Nan 814e24c7520SWang Nan /* 815e24c7520SWang Nan * 'head' pointer starts from 0. Kernel minus sizeof(record) form 816e24c7520SWang Nan * it each time when kernel writes to it, so in fact 'head' is 817e24c7520SWang Nan * negative. 'end' pointer is made manually by adding the size of 818e24c7520SWang Nan * the ring buffer to 'head' pointer, means the validate data can 819e24c7520SWang Nan * read is the whole ring buffer. If 'end' is positive, the ring 820e24c7520SWang Nan * buffer has not fully filled, so we must adjust 'end' to 0. 821e24c7520SWang Nan * 822e24c7520SWang Nan * However, since both 'head' and 'end' is unsigned, we can't 823e24c7520SWang Nan * simply compare 'end' against 0. Here we compare '-head' and 824e24c7520SWang Nan * the size of the ring buffer, where -head is the number of bytes 825e24c7520SWang Nan * kernel write to the ring buffer. 826e24c7520SWang Nan */ 827e24c7520SWang Nan if (-head < (u64)(md->mask + 1)) 828e24c7520SWang Nan end = 0; 829e24c7520SWang Nan else 830e24c7520SWang Nan end = head + md->mask + 1; 831e24c7520SWang Nan 832e24c7520SWang Nan return perf_mmap__read(md, false, start, end, &md->prev); 833e24c7520SWang Nan } 834e24c7520SWang Nan 835e24c7520SWang Nan void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx) 836e24c7520SWang Nan { 837e24c7520SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 838e24c7520SWang Nan u64 head; 839e24c7520SWang Nan 840e24c7520SWang Nan if (!atomic_read(&md->refcnt)) 841e24c7520SWang Nan return; 842e24c7520SWang Nan 843e24c7520SWang Nan head = perf_mmap__read_head(md); 844e24c7520SWang Nan md->prev = head; 845e24c7520SWang Nan } 846e24c7520SWang Nan 84782396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 84882396986SArnaldo Carvalho de Melo { 849b72e74d1SAdrian Hunter return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base; 85082396986SArnaldo Carvalho de Melo } 85182396986SArnaldo Carvalho de Melo 85282396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx) 85382396986SArnaldo Carvalho de Melo { 8547143849aSArnaldo Carvalho de Melo atomic_inc(&evlist->mmap[idx].refcnt); 85582396986SArnaldo Carvalho de Melo } 85682396986SArnaldo Carvalho de Melo 85782396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx) 85882396986SArnaldo Carvalho de Melo { 859e10e4ef6SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 86082396986SArnaldo Carvalho de Melo 861e10e4ef6SWang Nan BUG_ON(md->base && atomic_read(&md->refcnt) == 0); 862e10e4ef6SWang Nan 863e10e4ef6SWang Nan if (atomic_dec_and_test(&md->refcnt)) 86482396986SArnaldo Carvalho de Melo __perf_evlist__munmap(evlist, idx); 86582396986SArnaldo Carvalho de Melo } 86682396986SArnaldo Carvalho de Melo 8678e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 8688e50d384SZhouyi Zhou { 8698e50d384SZhouyi Zhou struct perf_mmap *md = &evlist->mmap[idx]; 87082396986SArnaldo Carvalho de Melo 87182396986SArnaldo Carvalho de Melo if (!evlist->overwrite) { 8727b8283b5SDavid Ahern u64 old = md->prev; 8738e50d384SZhouyi Zhou 8748e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 8758e50d384SZhouyi Zhou } 87682396986SArnaldo Carvalho de Melo 8777143849aSArnaldo Carvalho de Melo if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md)) 87882396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 8798e50d384SZhouyi Zhou } 8808e50d384SZhouyi Zhou 881718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, 882718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 883718c602dSAdrian Hunter void *userpg __maybe_unused, 884718c602dSAdrian Hunter int fd __maybe_unused) 885718c602dSAdrian Hunter { 886718c602dSAdrian Hunter return 0; 887718c602dSAdrian Hunter } 888718c602dSAdrian Hunter 889718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) 890718c602dSAdrian Hunter { 891718c602dSAdrian Hunter } 892718c602dSAdrian Hunter 893718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init( 894718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 895718c602dSAdrian Hunter off_t auxtrace_offset __maybe_unused, 896718c602dSAdrian Hunter unsigned int auxtrace_pages __maybe_unused, 897718c602dSAdrian Hunter bool auxtrace_overwrite __maybe_unused) 898718c602dSAdrian Hunter { 899718c602dSAdrian Hunter } 900718c602dSAdrian Hunter 901718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx( 902718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 903718c602dSAdrian Hunter struct perf_evlist *evlist __maybe_unused, 904718c602dSAdrian Hunter int idx __maybe_unused, 905718c602dSAdrian Hunter bool per_cpu __maybe_unused) 906718c602dSAdrian Hunter { 907718c602dSAdrian Hunter } 908718c602dSAdrian Hunter 90993edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 91093edcbd9SAdrian Hunter { 91193edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 91293edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 91393edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 914d4c6fb36SWang Nan evlist->mmap[idx].fd = -1; 9157143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 0); 91693edcbd9SAdrian Hunter } 917718c602dSAdrian Hunter auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap); 91893edcbd9SAdrian Hunter } 91993edcbd9SAdrian Hunter 9207e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 921f8a95309SArnaldo Carvalho de Melo { 922aece948fSArnaldo Carvalho de Melo int i; 923f8a95309SArnaldo Carvalho de Melo 924983874d1SArnaldo Carvalho de Melo if (evlist->mmap == NULL) 925983874d1SArnaldo Carvalho de Melo return; 926983874d1SArnaldo Carvalho de Melo 92793edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 92893edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 929aece948fSArnaldo Carvalho de Melo 93004662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 931f8a95309SArnaldo Carvalho de Melo } 932f8a95309SArnaldo Carvalho de Melo 933806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 934f8a95309SArnaldo Carvalho de Melo { 935d4c6fb36SWang Nan int i; 936d4c6fb36SWang Nan 937a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 938ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 939b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 940aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 941d4c6fb36SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) 942d4c6fb36SWang Nan evlist->mmap[i].fd = -1; 943f8a95309SArnaldo Carvalho de Melo return evlist->mmap != NULL ? 0 : -ENOMEM; 944f8a95309SArnaldo Carvalho de Melo } 945f8a95309SArnaldo Carvalho de Melo 946a8a8f3ebSAdrian Hunter struct mmap_params { 947a8a8f3ebSAdrian Hunter int prot; 948a8a8f3ebSAdrian Hunter int mask; 949718c602dSAdrian Hunter struct auxtrace_mmap_params auxtrace_mp; 950a8a8f3ebSAdrian Hunter }; 951a8a8f3ebSAdrian Hunter 952a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, 953a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 954f8a95309SArnaldo Carvalho de Melo { 95582396986SArnaldo Carvalho de Melo /* 95682396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 95782396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 95882396986SArnaldo Carvalho de Melo * the ring buffer. 95982396986SArnaldo Carvalho de Melo * 96082396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 96182396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 96282396986SArnaldo Carvalho de Melo * waiting to be consumed. 96382396986SArnaldo Carvalho de Melo * 96482396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 96582396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 96682396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 96782396986SArnaldo Carvalho de Melo */ 9687143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 2); 969aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 970a8a8f3ebSAdrian Hunter evlist->mmap[idx].mask = mp->mask; 971a8a8f3ebSAdrian Hunter evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, 972f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 973301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 97402635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 97502635965SAdrian Hunter errno); 976301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 977f8a95309SArnaldo Carvalho de Melo return -1; 978301b195dSNelson Elhage } 979d4c6fb36SWang Nan evlist->mmap[idx].fd = fd; 980ad6765ddSArnaldo Carvalho de Melo 981718c602dSAdrian Hunter if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap, 982718c602dSAdrian Hunter &mp->auxtrace_mp, evlist->mmap[idx].base, fd)) 983718c602dSAdrian Hunter return -1; 984718c602dSAdrian Hunter 985f8a95309SArnaldo Carvalho de Melo return 0; 986f8a95309SArnaldo Carvalho de Melo } 987f8a95309SArnaldo Carvalho de Melo 988f3058a1cSWang Nan static bool 989f3058a1cSWang Nan perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused, 990f3058a1cSWang Nan struct perf_evsel *evsel) 991f3058a1cSWang Nan { 992f3058a1cSWang Nan if (evsel->overwrite) 993f3058a1cSWang Nan return false; 994f3058a1cSWang Nan return true; 995f3058a1cSWang Nan } 996f3058a1cSWang Nan 99704e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 998a8a8f3ebSAdrian Hunter struct mmap_params *mp, int cpu, 999a8a8f3ebSAdrian Hunter int thread, int *output) 1000aece948fSArnaldo Carvalho de Melo { 1001aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 1002f3058a1cSWang Nan int revent; 100304e21314SAdrian Hunter 10040050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1005bf8e8f4bSAdrian Hunter int fd; 1006bf8e8f4bSAdrian Hunter 1007f3058a1cSWang Nan if (evsel->overwrite != (evlist->overwrite && evlist->backward)) 1008f3058a1cSWang Nan continue; 1009f3058a1cSWang Nan 1010bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 1011bf8e8f4bSAdrian Hunter continue; 1012bf8e8f4bSAdrian Hunter 1013bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 101404e21314SAdrian Hunter 101504e21314SAdrian Hunter if (*output == -1) { 101604e21314SAdrian Hunter *output = fd; 1017a8a8f3ebSAdrian Hunter if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) 101804e21314SAdrian Hunter return -1; 101904e21314SAdrian Hunter } else { 102004e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 102104e21314SAdrian Hunter return -1; 102282396986SArnaldo Carvalho de Melo 102382396986SArnaldo Carvalho de Melo perf_evlist__mmap_get(evlist, idx); 102404e21314SAdrian Hunter } 102504e21314SAdrian Hunter 1026f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 1027f3058a1cSWang Nan 1028f90d194aSAdrian Hunter /* 1029f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 1030f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 1031f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 1032f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 1033f90d194aSAdrian Hunter * Therefore don't add it for polling. 1034f90d194aSAdrian Hunter */ 1035f90d194aSAdrian Hunter if (!evsel->system_wide && 1036f3058a1cSWang Nan __perf_evlist__add_pollfd(evlist, fd, idx, revent) < 0) { 103782396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 1038ad6765ddSArnaldo Carvalho de Melo return -1; 103982396986SArnaldo Carvalho de Melo } 1040033fa713SArnaldo Carvalho de Melo 10413c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 10423c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 10433c659eedSAdrian Hunter fd) < 0) 104404e21314SAdrian Hunter return -1; 10453c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 10463c659eedSAdrian Hunter thread); 10473c659eedSAdrian Hunter } 104804e21314SAdrian Hunter } 104904e21314SAdrian Hunter 105004e21314SAdrian Hunter return 0; 105104e21314SAdrian Hunter } 105204e21314SAdrian Hunter 1053a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 1054a8a8f3ebSAdrian Hunter struct mmap_params *mp) 105504e21314SAdrian Hunter { 1056aece948fSArnaldo Carvalho de Melo int cpu, thread; 1057b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 1058b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 1059aece948fSArnaldo Carvalho de Melo 1060e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 1061b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 1062aece948fSArnaldo Carvalho de Melo int output = -1; 1063aece948fSArnaldo Carvalho de Melo 1064718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 1065718c602dSAdrian Hunter true); 1066718c602dSAdrian Hunter 1067b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 1068a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 1069a8a8f3ebSAdrian Hunter thread, &output)) 1070aece948fSArnaldo Carvalho de Melo goto out_unmap; 1071aece948fSArnaldo Carvalho de Melo } 1072aece948fSArnaldo Carvalho de Melo } 1073aece948fSArnaldo Carvalho de Melo 1074aece948fSArnaldo Carvalho de Melo return 0; 1075aece948fSArnaldo Carvalho de Melo 1076aece948fSArnaldo Carvalho de Melo out_unmap: 107793edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 107893edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 1079aece948fSArnaldo Carvalho de Melo return -1; 1080aece948fSArnaldo Carvalho de Melo } 1081aece948fSArnaldo Carvalho de Melo 1082a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 1083a8a8f3ebSAdrian Hunter struct mmap_params *mp) 1084aece948fSArnaldo Carvalho de Melo { 1085aece948fSArnaldo Carvalho de Melo int thread; 1086b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 1087aece948fSArnaldo Carvalho de Melo 1088e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 1089b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 1090aece948fSArnaldo Carvalho de Melo int output = -1; 1091aece948fSArnaldo Carvalho de Melo 1092718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 1093718c602dSAdrian Hunter false); 1094718c602dSAdrian Hunter 1095a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 1096a8a8f3ebSAdrian Hunter &output)) 1097aece948fSArnaldo Carvalho de Melo goto out_unmap; 1098aece948fSArnaldo Carvalho de Melo } 1099aece948fSArnaldo Carvalho de Melo 1100aece948fSArnaldo Carvalho de Melo return 0; 1101aece948fSArnaldo Carvalho de Melo 1102aece948fSArnaldo Carvalho de Melo out_unmap: 110393edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 110493edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 1105aece948fSArnaldo Carvalho de Melo return -1; 1106aece948fSArnaldo Carvalho de Melo } 1107aece948fSArnaldo Carvalho de Melo 1108f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 1109994a1f78SJiri Olsa { 1110f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 11118185e881SArnaldo Carvalho de Melo int max; 11128185e881SArnaldo Carvalho de Melo 11138185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 11148185e881SArnaldo Carvalho de Melo /* 11158185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 11168185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 11178185e881SArnaldo Carvalho de Melo * die yet... 11188185e881SArnaldo Carvalho de Melo */ 11198185e881SArnaldo Carvalho de Melo max = 512; 11208185e881SArnaldo Carvalho de Melo } else { 11218185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 11228185e881SArnaldo Carvalho de Melo } 11238185e881SArnaldo Carvalho de Melo 11248185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 11251be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 11261be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 1127f5e7150cSArnaldo Carvalho de Melo 1128f5e7150cSArnaldo Carvalho de Melo return pages; 1129f5e7150cSArnaldo Carvalho de Melo } 1130f5e7150cSArnaldo Carvalho de Melo 1131f5e7150cSArnaldo Carvalho de Melo static size_t perf_evlist__mmap_size(unsigned long pages) 1132f5e7150cSArnaldo Carvalho de Melo { 1133f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 1134f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 1135f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 1136994a1f78SJiri Olsa return 0; 1137994a1f78SJiri Olsa 1138994a1f78SJiri Olsa return (pages + 1) * page_size; 1139994a1f78SJiri Olsa } 1140994a1f78SJiri Olsa 114133c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 114233c2dcfdSDavid Ahern unsigned long max) 1143994a1f78SJiri Olsa { 11442fbe4abeSAdrian Hunter unsigned long pages, val; 114527050f53SJiri Olsa static struct parse_tag tags[] = { 114627050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 114727050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 114827050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 114927050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 115027050f53SJiri Olsa { .tag = 0 }, 115127050f53SJiri Olsa }; 1152994a1f78SJiri Olsa 11538973504bSDavid Ahern if (str == NULL) 115433c2dcfdSDavid Ahern return -EINVAL; 11558973504bSDavid Ahern 115627050f53SJiri Olsa val = parse_tag_value(str, tags); 11572fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 115827050f53SJiri Olsa /* we got file size value */ 115927050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 116027050f53SJiri Olsa } else { 116127050f53SJiri Olsa /* we got pages count value */ 116227050f53SJiri Olsa char *eptr; 1163994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 116433c2dcfdSDavid Ahern if (*eptr != '\0') 116533c2dcfdSDavid Ahern return -EINVAL; 116627050f53SJiri Olsa } 1167994a1f78SJiri Olsa 11682bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 116933c2dcfdSDavid Ahern /* leave number of pages at 0 */ 11701dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 117133c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 117291529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 11731dbfa938SAdrian Hunter if (!pages) 11741dbfa938SAdrian Hunter return -EINVAL; 11759639837eSDavid Ahern pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 11769639837eSDavid Ahern pages * page_size, pages); 11772fbe4abeSAdrian Hunter } 11782fbe4abeSAdrian Hunter 117933c2dcfdSDavid Ahern if (pages > max) 118033c2dcfdSDavid Ahern return -EINVAL; 118133c2dcfdSDavid Ahern 118233c2dcfdSDavid Ahern return pages; 118333c2dcfdSDavid Ahern } 118433c2dcfdSDavid Ahern 1185e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 118633c2dcfdSDavid Ahern { 118733c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 118833c2dcfdSDavid Ahern long pages; 118933c2dcfdSDavid Ahern 1190f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 119133c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 119233c2dcfdSDavid Ahern 119333c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 119433c2dcfdSDavid Ahern if (pages < 0) { 119533c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 1196994a1f78SJiri Olsa return -1; 1197994a1f78SJiri Olsa } 1198994a1f78SJiri Olsa 1199994a1f78SJiri Olsa *mmap_pages = pages; 1200994a1f78SJiri Olsa return 0; 1201994a1f78SJiri Olsa } 1202994a1f78SJiri Olsa 1203e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 1204e9db1310SAdrian Hunter int unset __maybe_unused) 1205e9db1310SAdrian Hunter { 1206e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 1207e9db1310SAdrian Hunter } 1208e9db1310SAdrian Hunter 1209c83fa7f2SAdrian Hunter /** 1210718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 1211c83fa7f2SAdrian Hunter * @evlist: list of events 1212c83fa7f2SAdrian Hunter * @pages: map length in pages 1213c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 1214718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 1215718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1216f8a95309SArnaldo Carvalho de Melo * 1217c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1218c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1219c83fa7f2SAdrian Hunter * automatically. 1220f8a95309SArnaldo Carvalho de Melo * 1221718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1222718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1223718c602dSAdrian Hunter * 1224c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1225f8a95309SArnaldo Carvalho de Melo */ 1226718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, 1227718c602dSAdrian Hunter bool overwrite, unsigned int auxtrace_pages, 1228718c602dSAdrian Hunter bool auxtrace_overwrite) 1229f8a95309SArnaldo Carvalho de Melo { 1230aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 12317e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 12327e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1233a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1234a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1235a8a8f3ebSAdrian Hunter }; 123650a682ceSArnaldo Carvalho de Melo 12377e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 1238f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1239f8a95309SArnaldo Carvalho de Melo 12401b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1241f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1242f8a95309SArnaldo Carvalho de Melo 1243f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1244994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 12452af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1246a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1247f8a95309SArnaldo Carvalho de Melo 1248718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1249718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1250718c602dSAdrian Hunter 12510050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1252f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1253a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1254a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1255f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1256f8a95309SArnaldo Carvalho de Melo } 1257f8a95309SArnaldo Carvalho de Melo 1258ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1259a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1260f8a95309SArnaldo Carvalho de Melo 1261a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1262f8a95309SArnaldo Carvalho de Melo } 12637e2ed097SArnaldo Carvalho de Melo 1264718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 1265718c602dSAdrian Hunter bool overwrite) 1266718c602dSAdrian Hunter { 1267718c602dSAdrian Hunter return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false); 1268718c602dSAdrian Hunter } 1269718c602dSAdrian Hunter 1270602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 12717e2ed097SArnaldo Carvalho de Melo { 127274bfd2b2SAdrian Hunter struct cpu_map *cpus; 127374bfd2b2SAdrian Hunter struct thread_map *threads; 12747e2ed097SArnaldo Carvalho de Melo 127574bfd2b2SAdrian Hunter threads = thread_map__new_str(target->pid, target->tid, target->uid); 127674bfd2b2SAdrian Hunter 127774bfd2b2SAdrian Hunter if (!threads) 12787e2ed097SArnaldo Carvalho de Melo return -1; 12797e2ed097SArnaldo Carvalho de Melo 12809c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 128174bfd2b2SAdrian Hunter cpus = cpu_map__dummy_new(); 1282879d77d0SNamhyung Kim else 128374bfd2b2SAdrian Hunter cpus = cpu_map__new(target->cpu_list); 12847e2ed097SArnaldo Carvalho de Melo 128574bfd2b2SAdrian Hunter if (!cpus) 12867e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 12877e2ed097SArnaldo Carvalho de Melo 1288ec9a77a7SAdrian Hunter evlist->has_user_cpus = !!target->cpu_list; 1289ec9a77a7SAdrian Hunter 129074bfd2b2SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 1291d5bc056eSAdrian Hunter 1292d5bc056eSAdrian Hunter return 0; 12937e2ed097SArnaldo Carvalho de Melo 12947e2ed097SArnaldo Carvalho de Melo out_delete_threads: 129574bfd2b2SAdrian Hunter thread_map__put(threads); 12967e2ed097SArnaldo Carvalho de Melo return -1; 12977e2ed097SArnaldo Carvalho de Melo } 12987e2ed097SArnaldo Carvalho de Melo 1299d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, 13003de5cfb0SJiri Olsa struct thread_map *threads) 13013de5cfb0SJiri Olsa { 1302934e0f20SAdrian Hunter /* 1303934e0f20SAdrian Hunter * Allow for the possibility that one or another of the maps isn't being 1304934e0f20SAdrian Hunter * changed i.e. don't put it. Note we are assuming the maps that are 1305934e0f20SAdrian Hunter * being applied are brand new and evlist is taking ownership of the 1306934e0f20SAdrian Hunter * original reference count of 1. If that is not the case it is up to 1307934e0f20SAdrian Hunter * the caller to increase the reference count. 1308934e0f20SAdrian Hunter */ 1309934e0f20SAdrian Hunter if (cpus != evlist->cpus) { 13103de5cfb0SJiri Olsa cpu_map__put(evlist->cpus); 1311a55e5663SArnaldo Carvalho de Melo evlist->cpus = cpu_map__get(cpus); 1312934e0f20SAdrian Hunter } 13133de5cfb0SJiri Olsa 1314934e0f20SAdrian Hunter if (threads != evlist->threads) { 13153de5cfb0SJiri Olsa thread_map__put(evlist->threads); 1316a55e5663SArnaldo Carvalho de Melo evlist->threads = thread_map__get(threads); 1317934e0f20SAdrian Hunter } 13183de5cfb0SJiri Olsa 1319ec9a77a7SAdrian Hunter perf_evlist__propagate_maps(evlist); 13203de5cfb0SJiri Olsa } 13213de5cfb0SJiri Olsa 132222c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist, 132322c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 132422c8a376SArnaldo Carvalho de Melo { 132522c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 132622c8a376SArnaldo Carvalho de Melo 132722c8a376SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 132822c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 132922c8a376SArnaldo Carvalho de Melo } 133022c8a376SArnaldo Carvalho de Melo 133122c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist, 133222c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 133322c8a376SArnaldo Carvalho de Melo { 133422c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 133522c8a376SArnaldo Carvalho de Melo 133622c8a376SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 133722c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 133822c8a376SArnaldo Carvalho de Melo } 133922c8a376SArnaldo Carvalho de Melo 134023d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 13410a102479SFrederic Weisbecker { 13420a102479SFrederic Weisbecker struct perf_evsel *evsel; 1343745cefc5SArnaldo Carvalho de Melo int err = 0; 1344745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1345b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 13460a102479SFrederic Weisbecker 13470050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1348745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 13490a102479SFrederic Weisbecker continue; 1350745cefc5SArnaldo Carvalho de Melo 1351d988d5eeSKan Liang /* 1352d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1353d988d5eeSKan Liang * So evlist and evsel should always be same. 1354d988d5eeSKan Liang */ 1355f47805a2SArnaldo Carvalho de Melo err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter); 135623d4aad4SArnaldo Carvalho de Melo if (err) { 135723d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1358745cefc5SArnaldo Carvalho de Melo break; 13590a102479SFrederic Weisbecker } 136023d4aad4SArnaldo Carvalho de Melo } 13610a102479SFrederic Weisbecker 1362745cefc5SArnaldo Carvalho de Melo return err; 1363745cefc5SArnaldo Carvalho de Melo } 1364745cefc5SArnaldo Carvalho de Melo 1365745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1366745cefc5SArnaldo Carvalho de Melo { 1367745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1368745cefc5SArnaldo Carvalho de Melo int err = 0; 1369745cefc5SArnaldo Carvalho de Melo 13700050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1371fdf14720SWang Nan if (evsel->attr.type != PERF_TYPE_TRACEPOINT) 1372fdf14720SWang Nan continue; 1373fdf14720SWang Nan 137494ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1375745cefc5SArnaldo Carvalho de Melo if (err) 1376745cefc5SArnaldo Carvalho de Melo break; 1377745cefc5SArnaldo Carvalho de Melo } 1378745cefc5SArnaldo Carvalho de Melo 1379745cefc5SArnaldo Carvalho de Melo return err; 13800a102479SFrederic Weisbecker } 138174429964SFrederic Weisbecker 1382be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1383cfd70a26SArnaldo Carvalho de Melo { 1384cfd70a26SArnaldo Carvalho de Melo char *filter; 1385be199adaSArnaldo Carvalho de Melo int ret = -1; 1386be199adaSArnaldo Carvalho de Melo size_t i; 1387cfd70a26SArnaldo Carvalho de Melo 1388be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1389be199adaSArnaldo Carvalho de Melo if (i == 0) { 1390be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1391cfd70a26SArnaldo Carvalho de Melo return -1; 1392be199adaSArnaldo Carvalho de Melo } else { 1393be199adaSArnaldo Carvalho de Melo char *tmp; 1394be199adaSArnaldo Carvalho de Melo 1395be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1396be199adaSArnaldo Carvalho de Melo goto out_free; 1397be199adaSArnaldo Carvalho de Melo 1398be199adaSArnaldo Carvalho de Melo free(filter); 1399be199adaSArnaldo Carvalho de Melo filter = tmp; 1400be199adaSArnaldo Carvalho de Melo } 1401be199adaSArnaldo Carvalho de Melo } 1402cfd70a26SArnaldo Carvalho de Melo 1403cfd70a26SArnaldo Carvalho de Melo ret = perf_evlist__set_filter(evlist, filter); 1404be199adaSArnaldo Carvalho de Melo out_free: 1405cfd70a26SArnaldo Carvalho de Melo free(filter); 1406cfd70a26SArnaldo Carvalho de Melo return ret; 1407cfd70a26SArnaldo Carvalho de Melo } 1408cfd70a26SArnaldo Carvalho de Melo 1409be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) 1410be199adaSArnaldo Carvalho de Melo { 1411be199adaSArnaldo Carvalho de Melo return perf_evlist__set_filter_pids(evlist, 1, &pid); 1412be199adaSArnaldo Carvalho de Melo } 1413be199adaSArnaldo Carvalho de Melo 14140c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 141574429964SFrederic Weisbecker { 141675562573SAdrian Hunter struct perf_evsel *pos; 1417c2a70653SArnaldo Carvalho de Melo 141875562573SAdrian Hunter if (evlist->nr_entries == 1) 141975562573SAdrian Hunter return true; 142075562573SAdrian Hunter 142175562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 142275562573SAdrian Hunter return false; 142375562573SAdrian Hunter 14240050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 142575562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 142675562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1427c2a70653SArnaldo Carvalho de Melo return false; 142874429964SFrederic Weisbecker } 142974429964SFrederic Weisbecker 1430c2a70653SArnaldo Carvalho de Melo return true; 1431c2a70653SArnaldo Carvalho de Melo } 1432c2a70653SArnaldo Carvalho de Melo 143375562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1434c2a70653SArnaldo Carvalho de Melo { 143575562573SAdrian Hunter struct perf_evsel *evsel; 143675562573SAdrian Hunter 143775562573SAdrian Hunter if (evlist->combined_sample_type) 143875562573SAdrian Hunter return evlist->combined_sample_type; 143975562573SAdrian Hunter 14400050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 144175562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 144275562573SAdrian Hunter 144375562573SAdrian Hunter return evlist->combined_sample_type; 144475562573SAdrian Hunter } 144575562573SAdrian Hunter 144675562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 144775562573SAdrian Hunter { 144875562573SAdrian Hunter evlist->combined_sample_type = 0; 144975562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1450c2a70653SArnaldo Carvalho de Melo } 1451c2a70653SArnaldo Carvalho de Melo 145298df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) 145398df858eSAndi Kleen { 145498df858eSAndi Kleen struct perf_evsel *evsel; 145598df858eSAndi Kleen u64 branch_type = 0; 145698df858eSAndi Kleen 145798df858eSAndi Kleen evlist__for_each(evlist, evsel) 145898df858eSAndi Kleen branch_type |= evsel->attr.branch_sample_type; 145998df858eSAndi Kleen return branch_type; 146098df858eSAndi Kleen } 146198df858eSAndi Kleen 14629ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 14639ede473cSJiri Olsa { 14649ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 14659ede473cSJiri Olsa u64 read_format = first->attr.read_format; 14669ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 14679ede473cSJiri Olsa 14680050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 14699ede473cSJiri Olsa if (read_format != pos->attr.read_format) 14709ede473cSJiri Olsa return false; 14719ede473cSJiri Olsa } 14729ede473cSJiri Olsa 14739ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 14749ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 14759ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 14769ede473cSJiri Olsa return false; 14779ede473cSJiri Olsa } 14789ede473cSJiri Olsa 14799ede473cSJiri Olsa return true; 14809ede473cSJiri Olsa } 14819ede473cSJiri Olsa 14829ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 14839ede473cSJiri Olsa { 14849ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 14859ede473cSJiri Olsa return first->attr.read_format; 14869ede473cSJiri Olsa } 14879ede473cSJiri Olsa 14880c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 148981e36bffSArnaldo Carvalho de Melo { 14900c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 149181e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 149281e36bffSArnaldo Carvalho de Melo u64 sample_type; 149381e36bffSArnaldo Carvalho de Melo u16 size = 0; 149481e36bffSArnaldo Carvalho de Melo 149581e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 149681e36bffSArnaldo Carvalho de Melo goto out; 149781e36bffSArnaldo Carvalho de Melo 149881e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 149981e36bffSArnaldo Carvalho de Melo 150081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 150181e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 150281e36bffSArnaldo Carvalho de Melo 150381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 150481e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 150581e36bffSArnaldo Carvalho de Melo 150681e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 150781e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 150881e36bffSArnaldo Carvalho de Melo 150981e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 151081e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 151181e36bffSArnaldo Carvalho de Melo 151281e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 151381e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 151475562573SAdrian Hunter 151575562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 151675562573SAdrian Hunter size += sizeof(data->id); 151781e36bffSArnaldo Carvalho de Melo out: 151881e36bffSArnaldo Carvalho de Melo return size; 151981e36bffSArnaldo Carvalho de Melo } 152081e36bffSArnaldo Carvalho de Melo 15210c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1522c2a70653SArnaldo Carvalho de Melo { 15230c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1524c2a70653SArnaldo Carvalho de Melo 15250050f7aaSArnaldo Carvalho de Melo evlist__for_each_continue(evlist, pos) { 1526c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1527c2a70653SArnaldo Carvalho de Melo return false; 1528c2a70653SArnaldo Carvalho de Melo } 1529c2a70653SArnaldo Carvalho de Melo 1530c2a70653SArnaldo Carvalho de Melo return true; 153174429964SFrederic Weisbecker } 153274429964SFrederic Weisbecker 15330c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 153474429964SFrederic Weisbecker { 15350c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1536c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 153774429964SFrederic Weisbecker } 153881cce8deSArnaldo Carvalho de Melo 153981cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 154081cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 154181cce8deSArnaldo Carvalho de Melo { 154281cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 154381cce8deSArnaldo Carvalho de Melo } 1544727ab04eSArnaldo Carvalho de Melo 1545a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1546a74b4b66SNamhyung Kim { 1547a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1548a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1549a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 15508ad9219eSStephane Eranian int n; 1551a74b4b66SNamhyung Kim 15528ad9219eSStephane Eranian evlist__for_each_reverse(evlist, evsel) { 15538ad9219eSStephane Eranian n = evsel->cpus ? evsel->cpus->nr : ncpus; 15548ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 15558ad9219eSStephane Eranian } 1556a74b4b66SNamhyung Kim } 1557a74b4b66SNamhyung Kim 15584112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 15594112eb18SArnaldo Carvalho de Melo { 15608c0498b6SAdrian Hunter struct cpu_map *cpus; 15618c0498b6SAdrian Hunter struct thread_map *threads; 15624112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 15634112eb18SArnaldo Carvalho de Melo 15644112eb18SArnaldo Carvalho de Melo /* 15654112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 15664112eb18SArnaldo Carvalho de Melo * an all cpus map. 15674112eb18SArnaldo Carvalho de Melo * 15684112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 15694112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 15704112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 15714112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 15724112eb18SArnaldo Carvalho de Melo */ 15738c0498b6SAdrian Hunter cpus = cpu_map__new(NULL); 15748c0498b6SAdrian Hunter if (!cpus) 15754112eb18SArnaldo Carvalho de Melo goto out; 15764112eb18SArnaldo Carvalho de Melo 15778c0498b6SAdrian Hunter threads = thread_map__new_dummy(); 15788c0498b6SAdrian Hunter if (!threads) 15798c0498b6SAdrian Hunter goto out_put; 15804112eb18SArnaldo Carvalho de Melo 15818c0498b6SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 15824112eb18SArnaldo Carvalho de Melo out: 15834112eb18SArnaldo Carvalho de Melo return err; 15848c0498b6SAdrian Hunter out_put: 15858c0498b6SAdrian Hunter cpu_map__put(cpus); 15864112eb18SArnaldo Carvalho de Melo goto out; 15874112eb18SArnaldo Carvalho de Melo } 15884112eb18SArnaldo Carvalho de Melo 15896a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1590727ab04eSArnaldo Carvalho de Melo { 15916a4bb04cSJiri Olsa struct perf_evsel *evsel; 1592a74b4b66SNamhyung Kim int err; 1593727ab04eSArnaldo Carvalho de Melo 15944112eb18SArnaldo Carvalho de Melo /* 15954112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 15964112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 15974112eb18SArnaldo Carvalho de Melo */ 15984112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 15994112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 16004112eb18SArnaldo Carvalho de Melo if (err < 0) 16014112eb18SArnaldo Carvalho de Melo goto out_err; 16024112eb18SArnaldo Carvalho de Melo } 16034112eb18SArnaldo Carvalho de Melo 1604733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1605733cd2feSAdrian Hunter 16060050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 160723df7f79SAdrian Hunter err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); 1608727ab04eSArnaldo Carvalho de Melo if (err < 0) 1609727ab04eSArnaldo Carvalho de Melo goto out_err; 1610727ab04eSArnaldo Carvalho de Melo } 1611727ab04eSArnaldo Carvalho de Melo 1612727ab04eSArnaldo Carvalho de Melo return 0; 1613727ab04eSArnaldo Carvalho de Melo out_err: 1614a74b4b66SNamhyung Kim perf_evlist__close(evlist); 161541c21a68SNamhyung Kim errno = -err; 1616727ab04eSArnaldo Carvalho de Melo return err; 1617727ab04eSArnaldo Carvalho de Melo } 161835b9d88eSArnaldo Carvalho de Melo 1619602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 162055e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1621735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 162235b9d88eSArnaldo Carvalho de Melo { 162335b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 162435b9d88eSArnaldo Carvalho de Melo char bf; 162535b9d88eSArnaldo Carvalho de Melo 162635b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 162735b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 162835b9d88eSArnaldo Carvalho de Melo return -1; 162935b9d88eSArnaldo Carvalho de Melo } 163035b9d88eSArnaldo Carvalho de Melo 163135b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 163235b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 163335b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 163435b9d88eSArnaldo Carvalho de Melo } 163535b9d88eSArnaldo Carvalho de Melo 163635b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 163735b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 163835b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 163935b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 164035b9d88eSArnaldo Carvalho de Melo } 164135b9d88eSArnaldo Carvalho de Melo 164235b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 16435f1c4225SArnaldo Carvalho de Melo int ret; 16445f1c4225SArnaldo Carvalho de Melo 1645119fa3c9SNamhyung Kim if (pipe_output) 164635b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 164735b9d88eSArnaldo Carvalho de Melo 16480817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 16490817df08SDavid Ahern 165035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 165135b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 165235b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 165335b9d88eSArnaldo Carvalho de Melo 165435b9d88eSArnaldo Carvalho de Melo /* 165535b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 165635b9d88eSArnaldo Carvalho de Melo */ 165735b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 165835b9d88eSArnaldo Carvalho de Melo 165935b9d88eSArnaldo Carvalho de Melo /* 166035b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 166135b9d88eSArnaldo Carvalho de Melo */ 16625f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 16635f1c4225SArnaldo Carvalho de Melo /* 16645f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 16655f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 16665f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 16675f1c4225SArnaldo Carvalho de Melo * 166820f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 16695f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 16705f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 16715f1c4225SArnaldo Carvalho de Melo * here. 16725f1c4225SArnaldo Carvalho de Melo */ 16735f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 16745f1c4225SArnaldo Carvalho de Melo if (ret == -1) 167535b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 16765f1c4225SArnaldo Carvalho de Melo exit(ret); 16775f1c4225SArnaldo Carvalho de Melo } 167835b9d88eSArnaldo Carvalho de Melo 167935b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 168035b9d88eSArnaldo Carvalho de Melo 1681735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1682f33cbe72SArnaldo Carvalho de Melo union sigval val; 1683f33cbe72SArnaldo Carvalho de Melo 1684f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1685f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 168635b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1687f33cbe72SArnaldo Carvalho de Melo } else 1688f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 168935b9d88eSArnaldo Carvalho de Melo exit(-1); 169035b9d88eSArnaldo Carvalho de Melo } 169135b9d88eSArnaldo Carvalho de Melo 1692735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1693735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1694735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1695735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1696735f7e0bSArnaldo Carvalho de Melo }; 1697735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1698735f7e0bSArnaldo Carvalho de Melo } 1699735f7e0bSArnaldo Carvalho de Melo 17001aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 17011aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 17021aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 17031aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 17041aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 17051aaf63b1SArnaldo Carvalho de Melo } 1706e13798c7SJiri Olsa thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 17071aaf63b1SArnaldo Carvalho de Melo } 170835b9d88eSArnaldo Carvalho de Melo 170935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 171035b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 171135b9d88eSArnaldo Carvalho de Melo /* 171235b9d88eSArnaldo Carvalho de Melo * wait for child to settle 171335b9d88eSArnaldo Carvalho de Melo */ 171435b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 171535b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 171635b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 171735b9d88eSArnaldo Carvalho de Melo } 171835b9d88eSArnaldo Carvalho de Melo 1719bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 172035b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 172135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 172235b9d88eSArnaldo Carvalho de Melo return 0; 172335b9d88eSArnaldo Carvalho de Melo 172435b9d88eSArnaldo Carvalho de Melo out_close_pipes: 172535b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 172635b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 172735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 172835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 172935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 173035b9d88eSArnaldo Carvalho de Melo return -1; 173135b9d88eSArnaldo Carvalho de Melo } 173235b9d88eSArnaldo Carvalho de Melo 173335b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 173435b9d88eSArnaldo Carvalho de Melo { 173535b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1736b3824404SDavid Ahern char bf = 0; 1737bcf3145fSNamhyung Kim int ret; 173835b9d88eSArnaldo Carvalho de Melo /* 173935b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 174035b9d88eSArnaldo Carvalho de Melo */ 1741bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1742bcf3145fSNamhyung Kim if (ret < 0) 1743bcf3145fSNamhyung Kim perror("enable to write to pipe"); 1744bcf3145fSNamhyung Kim 1745bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1746bcf3145fSNamhyung Kim return ret; 174735b9d88eSArnaldo Carvalho de Melo } 174835b9d88eSArnaldo Carvalho de Melo 174935b9d88eSArnaldo Carvalho de Melo return 0; 175035b9d88eSArnaldo Carvalho de Melo } 1751cb0b29e0SArnaldo Carvalho de Melo 1752a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 17530807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1754cb0b29e0SArnaldo Carvalho de Melo { 175575562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 175675562573SAdrian Hunter 175775562573SAdrian Hunter if (!evsel) 175875562573SAdrian Hunter return -EFAULT; 17590807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1760cb0b29e0SArnaldo Carvalho de Melo } 176178f067b3SArnaldo Carvalho de Melo 176278f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 176378f067b3SArnaldo Carvalho de Melo { 176478f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 176578f067b3SArnaldo Carvalho de Melo size_t printed = 0; 176678f067b3SArnaldo Carvalho de Melo 17670050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 176878f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 176978f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 177078f067b3SArnaldo Carvalho de Melo } 177178f067b3SArnaldo Carvalho de Melo 1772b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 177378f067b3SArnaldo Carvalho de Melo } 17746ef068cbSArnaldo Carvalho de Melo 1775d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist, 1776a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1777a8f23d8fSArnaldo Carvalho de Melo { 1778a8f23d8fSArnaldo Carvalho de Melo int printed, value; 17796e81c74cSMasami Hiramatsu char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1780a8f23d8fSArnaldo Carvalho de Melo 1781a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1782a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1783a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1784a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1785a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1786a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1787a8f23d8fSArnaldo Carvalho de Melo 17881a47245dSAdrian Hunter value = perf_event_paranoid(); 1789a8f23d8fSArnaldo Carvalho de Melo 1790a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1791a8f23d8fSArnaldo Carvalho de Melo 1792a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1793a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1794a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1795a8f23d8fSArnaldo Carvalho de Melo } 1796a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 17975229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1798a8f23d8fSArnaldo Carvalho de Melo 1799a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 18005229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 18015229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1802a8f23d8fSArnaldo Carvalho de Melo break; 1803d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1804d9aade7fSArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1805d9aade7fSArnaldo Carvalho de Melo int max_freq; 1806d9aade7fSArnaldo Carvalho de Melo 1807d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1808d9aade7fSArnaldo Carvalho de Melo goto out_default; 1809d9aade7fSArnaldo Carvalho de Melo 1810d9aade7fSArnaldo Carvalho de Melo if (first->attr.sample_freq < (u64)max_freq) 1811d9aade7fSArnaldo Carvalho de Melo goto out_default; 1812d9aade7fSArnaldo Carvalho de Melo 1813d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1814d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1815d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1816d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1817d9aade7fSArnaldo Carvalho de Melo emsg, max_freq, first->attr.sample_freq); 1818d9aade7fSArnaldo Carvalho de Melo break; 1819d9aade7fSArnaldo Carvalho de Melo } 1820a8f23d8fSArnaldo Carvalho de Melo default: 1821d9aade7fSArnaldo Carvalho de Melo out_default: 1822a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1823a8f23d8fSArnaldo Carvalho de Melo break; 1824a8f23d8fSArnaldo Carvalho de Melo } 1825a8f23d8fSArnaldo Carvalho de Melo 1826a8f23d8fSArnaldo Carvalho de Melo return 0; 1827a8f23d8fSArnaldo Carvalho de Melo } 1828a025e4f0SAdrian Hunter 1829956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1830956fa571SArnaldo Carvalho de Melo { 1831956fa571SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1832e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1833956fa571SArnaldo Carvalho de Melo 1834956fa571SArnaldo Carvalho de Melo switch (err) { 1835956fa571SArnaldo Carvalho de Melo case EPERM: 1836e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1837e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1838e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1839956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1840e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1841e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1842e965bea1SArnaldo Carvalho de Melo 1843e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1844e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1845e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1846e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1847e965bea1SArnaldo Carvalho de Melo } 1848e965bea1SArnaldo Carvalho de Melo 1849e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1850e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1851956fa571SArnaldo Carvalho de Melo break; 1852956fa571SArnaldo Carvalho de Melo default: 1853956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1854956fa571SArnaldo Carvalho de Melo break; 1855956fa571SArnaldo Carvalho de Melo } 1856956fa571SArnaldo Carvalho de Melo 1857956fa571SArnaldo Carvalho de Melo return 0; 1858956fa571SArnaldo Carvalho de Melo } 1859956fa571SArnaldo Carvalho de Melo 1860a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1861a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1862a025e4f0SAdrian Hunter { 1863a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1864a025e4f0SAdrian Hunter LIST_HEAD(move); 1865a025e4f0SAdrian Hunter 1866a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1867a025e4f0SAdrian Hunter return; 1868a025e4f0SAdrian Hunter 18690050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, evsel) { 1870a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1871a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1872a025e4f0SAdrian Hunter } 1873a025e4f0SAdrian Hunter 1874a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1875a025e4f0SAdrian Hunter } 187660b0896cSAdrian Hunter 187760b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 187860b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 187960b0896cSAdrian Hunter { 188060b0896cSAdrian Hunter struct perf_evsel *evsel; 188160b0896cSAdrian Hunter 188260b0896cSAdrian Hunter if (tracking_evsel->tracking) 188360b0896cSAdrian Hunter return; 188460b0896cSAdrian Hunter 188560b0896cSAdrian Hunter evlist__for_each(evlist, evsel) { 188660b0896cSAdrian Hunter if (evsel != tracking_evsel) 188760b0896cSAdrian Hunter evsel->tracking = false; 188860b0896cSAdrian Hunter } 188960b0896cSAdrian Hunter 189060b0896cSAdrian Hunter tracking_evsel->tracking = true; 189160b0896cSAdrian Hunter } 18927630b3e2SWang Nan 18937630b3e2SWang Nan struct perf_evsel * 18947630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, 18957630b3e2SWang Nan const char *str) 18967630b3e2SWang Nan { 18977630b3e2SWang Nan struct perf_evsel *evsel; 18987630b3e2SWang Nan 18997630b3e2SWang Nan evlist__for_each(evlist, evsel) { 19007630b3e2SWang Nan if (!evsel->name) 19017630b3e2SWang Nan continue; 19027630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 19037630b3e2SWang Nan return evsel; 19047630b3e2SWang Nan } 19057630b3e2SWang Nan 19067630b3e2SWang Nan return NULL; 19077630b3e2SWang Nan } 1908