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 486258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, 487258e4bfcSWang Nan void *arg __maybe_unused) 488e4b356b5SArnaldo Carvalho de Melo { 489e4b356b5SArnaldo Carvalho de Melo struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd); 490e4b356b5SArnaldo Carvalho de Melo 491e4b356b5SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, fda->priv[fd].idx); 49270082dd9SArnaldo Carvalho de Melo } 49370db7533SArnaldo Carvalho de Melo 4941ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) 4951ddec7f0SArnaldo Carvalho de Melo { 496e4b356b5SArnaldo Carvalho de Melo return fdarray__filter(&evlist->pollfd, revents_and_mask, 497258e4bfcSWang Nan perf_evlist__munmap_filtered, NULL); 4981ddec7f0SArnaldo Carvalho de Melo } 4991ddec7f0SArnaldo Carvalho de Melo 500f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout) 501f66a889dSArnaldo Carvalho de Melo { 5021b85337dSArnaldo Carvalho de Melo return fdarray__poll(&evlist->pollfd, timeout); 503f66a889dSArnaldo Carvalho de Melo } 504f66a889dSArnaldo Carvalho de Melo 505a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist, 506a91e5431SArnaldo Carvalho de Melo struct perf_evsel *evsel, 5073d3b5e95SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 5083d3b5e95SArnaldo Carvalho de Melo { 5093d3b5e95SArnaldo Carvalho de Melo int hash; 5103d3b5e95SArnaldo Carvalho de Melo struct perf_sample_id *sid = SID(evsel, cpu, thread); 5113d3b5e95SArnaldo Carvalho de Melo 5123d3b5e95SArnaldo Carvalho de Melo sid->id = id; 5133d3b5e95SArnaldo Carvalho de Melo sid->evsel = evsel; 5143d3b5e95SArnaldo Carvalho de Melo hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); 5153d3b5e95SArnaldo Carvalho de Melo hlist_add_head(&sid->node, &evlist->heads[hash]); 5163d3b5e95SArnaldo Carvalho de Melo } 5173d3b5e95SArnaldo Carvalho de Melo 518a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, 519a91e5431SArnaldo Carvalho de Melo int cpu, int thread, u64 id) 520a91e5431SArnaldo Carvalho de Melo { 521a91e5431SArnaldo Carvalho de Melo perf_evlist__id_hash(evlist, evsel, cpu, thread, id); 522a91e5431SArnaldo Carvalho de Melo evsel->id[evsel->ids++] = id; 523a91e5431SArnaldo Carvalho de Melo } 524a91e5431SArnaldo Carvalho de Melo 5251c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist, 5263d3b5e95SArnaldo Carvalho de Melo struct perf_evsel *evsel, 527f8a95309SArnaldo Carvalho de Melo int cpu, int thread, int fd) 528f8a95309SArnaldo Carvalho de Melo { 529f8a95309SArnaldo Carvalho de Melo u64 read_data[4] = { 0, }; 5303d3b5e95SArnaldo Carvalho de Melo int id_idx = 1; /* The first entry is the counter value */ 531e2b5abe0SJiri Olsa u64 id; 532e2b5abe0SJiri Olsa int ret; 533e2b5abe0SJiri Olsa 534e2b5abe0SJiri Olsa ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); 535e2b5abe0SJiri Olsa if (!ret) 536e2b5abe0SJiri Olsa goto add; 537e2b5abe0SJiri Olsa 538e2b5abe0SJiri Olsa if (errno != ENOTTY) 539e2b5abe0SJiri Olsa return -1; 540e2b5abe0SJiri Olsa 541e2b5abe0SJiri Olsa /* Legacy way to get event id.. All hail to old kernels! */ 542f8a95309SArnaldo Carvalho de Melo 543c4861afeSJiri Olsa /* 544c4861afeSJiri Olsa * This way does not work with group format read, so bail 545c4861afeSJiri Olsa * out in that case. 546c4861afeSJiri Olsa */ 547c4861afeSJiri Olsa if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) 548c4861afeSJiri Olsa return -1; 549c4861afeSJiri Olsa 550f8a95309SArnaldo Carvalho de Melo if (!(evsel->attr.read_format & PERF_FORMAT_ID) || 551f8a95309SArnaldo Carvalho de Melo read(fd, &read_data, sizeof(read_data)) == -1) 552f8a95309SArnaldo Carvalho de Melo return -1; 553f8a95309SArnaldo Carvalho de Melo 554f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 555f8a95309SArnaldo Carvalho de Melo ++id_idx; 556f8a95309SArnaldo Carvalho de Melo if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 557f8a95309SArnaldo Carvalho de Melo ++id_idx; 558f8a95309SArnaldo Carvalho de Melo 559e2b5abe0SJiri Olsa id = read_data[id_idx]; 560e2b5abe0SJiri Olsa 561e2b5abe0SJiri Olsa add: 562e2b5abe0SJiri Olsa perf_evlist__id_add(evlist, evsel, cpu, thread, id); 563f8a95309SArnaldo Carvalho de Melo return 0; 564f8a95309SArnaldo Carvalho de Melo } 565f8a95309SArnaldo Carvalho de Melo 5663c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, 5673c659eedSAdrian Hunter struct perf_evsel *evsel, int idx, int cpu, 5683c659eedSAdrian Hunter int thread) 5693c659eedSAdrian Hunter { 5703c659eedSAdrian Hunter struct perf_sample_id *sid = SID(evsel, cpu, thread); 5713c659eedSAdrian Hunter sid->idx = idx; 5723c659eedSAdrian Hunter if (evlist->cpus && cpu >= 0) 5733c659eedSAdrian Hunter sid->cpu = evlist->cpus->map[cpu]; 5743c659eedSAdrian Hunter else 5753c659eedSAdrian Hunter sid->cpu = -1; 5763c659eedSAdrian Hunter if (!evsel->system_wide && evlist->threads && thread >= 0) 577e13798c7SJiri Olsa sid->tid = thread_map__pid(evlist->threads, thread); 5783c659eedSAdrian Hunter else 5793c659eedSAdrian Hunter sid->tid = -1; 5803c659eedSAdrian Hunter } 5813c659eedSAdrian Hunter 582932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) 58370db7533SArnaldo Carvalho de Melo { 58470db7533SArnaldo Carvalho de Melo struct hlist_head *head; 58570db7533SArnaldo Carvalho de Melo struct perf_sample_id *sid; 58670db7533SArnaldo Carvalho de Melo int hash; 58770db7533SArnaldo Carvalho de Melo 58870db7533SArnaldo Carvalho de Melo hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 58970db7533SArnaldo Carvalho de Melo head = &evlist->heads[hash]; 59070db7533SArnaldo Carvalho de Melo 591b67bfe0dSSasha Levin hlist_for_each_entry(sid, head, node) 59270db7533SArnaldo Carvalho de Melo if (sid->id == id) 593932a3594SJiri Olsa return sid; 594932a3594SJiri Olsa 595932a3594SJiri Olsa return NULL; 596932a3594SJiri Olsa } 597932a3594SJiri Olsa 598932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) 599932a3594SJiri Olsa { 600932a3594SJiri Olsa struct perf_sample_id *sid; 601932a3594SJiri Olsa 60205169df5SAdrian Hunter if (evlist->nr_entries == 1 || !id) 603932a3594SJiri Olsa return perf_evlist__first(evlist); 604932a3594SJiri Olsa 605932a3594SJiri Olsa sid = perf_evlist__id2sid(evlist, id); 606932a3594SJiri Olsa if (sid) 60770db7533SArnaldo Carvalho de Melo return sid->evsel; 60830e68bccSNamhyung Kim 60930e68bccSNamhyung Kim if (!perf_evlist__sample_id_all(evlist)) 6100c21f736SArnaldo Carvalho de Melo return perf_evlist__first(evlist); 61130e68bccSNamhyung Kim 61270db7533SArnaldo Carvalho de Melo return NULL; 61370db7533SArnaldo Carvalho de Melo } 61404391debSArnaldo Carvalho de Melo 615dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist, 616dddcf6abSAdrian Hunter u64 id) 617dddcf6abSAdrian Hunter { 618dddcf6abSAdrian Hunter struct perf_sample_id *sid; 619dddcf6abSAdrian Hunter 620dddcf6abSAdrian Hunter if (!id) 621dddcf6abSAdrian Hunter return NULL; 622dddcf6abSAdrian Hunter 623dddcf6abSAdrian Hunter sid = perf_evlist__id2sid(evlist, id); 624dddcf6abSAdrian Hunter if (sid) 625dddcf6abSAdrian Hunter return sid->evsel; 626dddcf6abSAdrian Hunter 627dddcf6abSAdrian Hunter return NULL; 628dddcf6abSAdrian Hunter } 629dddcf6abSAdrian Hunter 63075562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist, 63175562573SAdrian Hunter union perf_event *event, u64 *id) 63275562573SAdrian Hunter { 63375562573SAdrian Hunter const u64 *array = event->sample.array; 63475562573SAdrian Hunter ssize_t n; 63575562573SAdrian Hunter 63675562573SAdrian Hunter n = (event->header.size - sizeof(event->header)) >> 3; 63775562573SAdrian Hunter 63875562573SAdrian Hunter if (event->header.type == PERF_RECORD_SAMPLE) { 63975562573SAdrian Hunter if (evlist->id_pos >= n) 64075562573SAdrian Hunter return -1; 64175562573SAdrian Hunter *id = array[evlist->id_pos]; 64275562573SAdrian Hunter } else { 64375562573SAdrian Hunter if (evlist->is_pos > n) 64475562573SAdrian Hunter return -1; 64575562573SAdrian Hunter n -= evlist->is_pos; 64675562573SAdrian Hunter *id = array[n]; 64775562573SAdrian Hunter } 64875562573SAdrian Hunter return 0; 64975562573SAdrian Hunter } 65075562573SAdrian Hunter 65175562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, 65275562573SAdrian Hunter union perf_event *event) 65375562573SAdrian Hunter { 65498be6966SAdrian Hunter struct perf_evsel *first = perf_evlist__first(evlist); 65575562573SAdrian Hunter struct hlist_head *head; 65675562573SAdrian Hunter struct perf_sample_id *sid; 65775562573SAdrian Hunter int hash; 65875562573SAdrian Hunter u64 id; 65975562573SAdrian Hunter 66075562573SAdrian Hunter if (evlist->nr_entries == 1) 66198be6966SAdrian Hunter return first; 66298be6966SAdrian Hunter 66398be6966SAdrian Hunter if (!first->attr.sample_id_all && 66498be6966SAdrian Hunter event->header.type != PERF_RECORD_SAMPLE) 66598be6966SAdrian Hunter return first; 66675562573SAdrian Hunter 66775562573SAdrian Hunter if (perf_evlist__event2id(evlist, event, &id)) 66875562573SAdrian Hunter return NULL; 66975562573SAdrian Hunter 67075562573SAdrian Hunter /* Synthesized events have an id of zero */ 67175562573SAdrian Hunter if (!id) 67298be6966SAdrian Hunter return first; 67375562573SAdrian Hunter 67475562573SAdrian Hunter hash = hash_64(id, PERF_EVLIST__HLIST_BITS); 67575562573SAdrian Hunter head = &evlist->heads[hash]; 67675562573SAdrian Hunter 67775562573SAdrian Hunter hlist_for_each_entry(sid, head, node) { 67875562573SAdrian Hunter if (sid->id == id) 67975562573SAdrian Hunter return sid->evsel; 68075562573SAdrian Hunter } 68175562573SAdrian Hunter return NULL; 68275562573SAdrian Hunter } 68375562573SAdrian Hunter 68465aea233SWang Nan static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value) 68565aea233SWang Nan { 68665aea233SWang Nan int i; 68765aea233SWang Nan 68865aea233SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 68965aea233SWang Nan int fd = evlist->mmap[i].fd; 69065aea233SWang Nan int err; 69165aea233SWang Nan 69265aea233SWang Nan if (fd < 0) 69365aea233SWang Nan continue; 69465aea233SWang Nan err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); 69565aea233SWang Nan if (err) 69665aea233SWang Nan return err; 69765aea233SWang Nan } 69865aea233SWang Nan return 0; 69965aea233SWang Nan } 70065aea233SWang Nan 70165aea233SWang Nan int perf_evlist__pause(struct perf_evlist *evlist) 70265aea233SWang Nan { 70365aea233SWang Nan return perf_evlist__set_paused(evlist, true); 70465aea233SWang Nan } 70565aea233SWang Nan 70665aea233SWang Nan int perf_evlist__resume(struct perf_evlist *evlist) 70765aea233SWang Nan { 70865aea233SWang Nan return perf_evlist__set_paused(evlist, false); 70965aea233SWang Nan } 71065aea233SWang Nan 711b6b85dadSWang Nan /* When check_messup is true, 'end' must points to a good entry */ 7120f4ccd11SWang Nan static union perf_event * 713b6b85dadSWang Nan perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start, 714b6b85dadSWang Nan u64 end, u64 *prev) 71504391debSArnaldo Carvalho de Melo { 71604391debSArnaldo Carvalho de Melo unsigned char *data = md->base + page_size; 7178115d60cSArnaldo Carvalho de Melo union perf_event *event = NULL; 718b6b85dadSWang Nan int diff = end - start; 71904391debSArnaldo Carvalho de Melo 720b6b85dadSWang Nan if (check_messup) { 72104391debSArnaldo Carvalho de Melo /* 72204391debSArnaldo Carvalho de Melo * If we're further behind than half the buffer, there's a chance 72304391debSArnaldo Carvalho de Melo * the writer will bite our tail and mess up the samples under us. 72404391debSArnaldo Carvalho de Melo * 725b6b85dadSWang Nan * If we somehow ended up ahead of the 'end', we got messed up. 72604391debSArnaldo Carvalho de Melo * 727b6b85dadSWang Nan * In either case, truncate and restart at 'end'. 72804391debSArnaldo Carvalho de Melo */ 72904391debSArnaldo Carvalho de Melo if (diff > md->mask / 2 || diff < 0) { 73004391debSArnaldo Carvalho de Melo fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 73104391debSArnaldo Carvalho de Melo 73204391debSArnaldo Carvalho de Melo /* 733b6b85dadSWang Nan * 'end' points to a known good entry, start there. 73404391debSArnaldo Carvalho de Melo */ 735b6b85dadSWang Nan start = end; 736b04b7023SWang Nan diff = 0; 73704391debSArnaldo Carvalho de Melo } 7387bb41152SArnaldo Carvalho de Melo } 73904391debSArnaldo Carvalho de Melo 740b04b7023SWang Nan if (diff >= (int)sizeof(event->header)) { 74104391debSArnaldo Carvalho de Melo size_t size; 74204391debSArnaldo Carvalho de Melo 743b6b85dadSWang Nan event = (union perf_event *)&data[start & md->mask]; 74404391debSArnaldo Carvalho de Melo size = event->header.size; 74504391debSArnaldo Carvalho de Melo 746b04b7023SWang Nan if (size < sizeof(event->header) || diff < (int)size) { 747b04b7023SWang Nan event = NULL; 748b04b7023SWang Nan goto broken_event; 749b04b7023SWang Nan } 750b04b7023SWang Nan 75104391debSArnaldo Carvalho de Melo /* 75204391debSArnaldo Carvalho de Melo * Event straddles the mmap boundary -- header should always 75304391debSArnaldo Carvalho de Melo * be inside due to u64 alignment of output. 75404391debSArnaldo Carvalho de Melo */ 755b6b85dadSWang Nan if ((start & md->mask) + size != ((start + size) & md->mask)) { 756b6b85dadSWang Nan unsigned int offset = start; 75704391debSArnaldo Carvalho de Melo unsigned int len = min(sizeof(*event), size), cpy; 758a65cb4b9SJiri Olsa void *dst = md->event_copy; 75904391debSArnaldo Carvalho de Melo 76004391debSArnaldo Carvalho de Melo do { 76104391debSArnaldo Carvalho de Melo cpy = min(md->mask + 1 - (offset & md->mask), len); 76204391debSArnaldo Carvalho de Melo memcpy(dst, &data[offset & md->mask], cpy); 76304391debSArnaldo Carvalho de Melo offset += cpy; 76404391debSArnaldo Carvalho de Melo dst += cpy; 76504391debSArnaldo Carvalho de Melo len -= cpy; 76604391debSArnaldo Carvalho de Melo } while (len); 76704391debSArnaldo Carvalho de Melo 768a65cb4b9SJiri Olsa event = (union perf_event *) md->event_copy; 76904391debSArnaldo Carvalho de Melo } 77004391debSArnaldo Carvalho de Melo 771b6b85dadSWang Nan start += size; 77204391debSArnaldo Carvalho de Melo } 77304391debSArnaldo Carvalho de Melo 774b04b7023SWang Nan broken_event: 7750f4ccd11SWang Nan if (prev) 776b6b85dadSWang Nan *prev = start; 7777bb41152SArnaldo Carvalho de Melo 77804391debSArnaldo Carvalho de Melo return event; 77904391debSArnaldo Carvalho de Melo } 780f8a95309SArnaldo Carvalho de Melo 7815a5ddeb6SWang Nan union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx) 7820f4ccd11SWang Nan { 7830f4ccd11SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 7840f4ccd11SWang Nan u64 head; 7850f4ccd11SWang Nan u64 old = md->prev; 7860f4ccd11SWang Nan 7870f4ccd11SWang Nan /* 7880f4ccd11SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 7890f4ccd11SWang Nan */ 7900f4ccd11SWang Nan if (!atomic_read(&md->refcnt)) 7910f4ccd11SWang Nan return NULL; 7920f4ccd11SWang Nan 7930f4ccd11SWang Nan head = perf_mmap__read_head(md); 7940f4ccd11SWang Nan 795b6b85dadSWang Nan return perf_mmap__read(md, evlist->overwrite, old, head, &md->prev); 7960f4ccd11SWang Nan } 7970f4ccd11SWang Nan 798e24c7520SWang Nan union perf_event * 799e24c7520SWang Nan perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx) 800e24c7520SWang Nan { 801e24c7520SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 802e24c7520SWang Nan u64 head, end; 803e24c7520SWang Nan u64 start = md->prev; 804e24c7520SWang Nan 805e24c7520SWang Nan /* 806e24c7520SWang Nan * Check if event was unmapped due to a POLLHUP/POLLERR. 807e24c7520SWang Nan */ 808e24c7520SWang Nan if (!atomic_read(&md->refcnt)) 809e24c7520SWang Nan return NULL; 810e24c7520SWang Nan 811e24c7520SWang Nan head = perf_mmap__read_head(md); 812e24c7520SWang Nan if (!head) 813e24c7520SWang Nan return NULL; 814e24c7520SWang Nan 815e24c7520SWang Nan /* 816e24c7520SWang Nan * 'head' pointer starts from 0. Kernel minus sizeof(record) form 817e24c7520SWang Nan * it each time when kernel writes to it, so in fact 'head' is 818e24c7520SWang Nan * negative. 'end' pointer is made manually by adding the size of 819e24c7520SWang Nan * the ring buffer to 'head' pointer, means the validate data can 820e24c7520SWang Nan * read is the whole ring buffer. If 'end' is positive, the ring 821e24c7520SWang Nan * buffer has not fully filled, so we must adjust 'end' to 0. 822e24c7520SWang Nan * 823e24c7520SWang Nan * However, since both 'head' and 'end' is unsigned, we can't 824e24c7520SWang Nan * simply compare 'end' against 0. Here we compare '-head' and 825e24c7520SWang Nan * the size of the ring buffer, where -head is the number of bytes 826e24c7520SWang Nan * kernel write to the ring buffer. 827e24c7520SWang Nan */ 828e24c7520SWang Nan if (-head < (u64)(md->mask + 1)) 829e24c7520SWang Nan end = 0; 830e24c7520SWang Nan else 831e24c7520SWang Nan end = head + md->mask + 1; 832e24c7520SWang Nan 833e24c7520SWang Nan return perf_mmap__read(md, false, start, end, &md->prev); 834e24c7520SWang Nan } 835e24c7520SWang Nan 8365a5ddeb6SWang Nan union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) 8375a5ddeb6SWang Nan { 8385a5ddeb6SWang Nan if (!evlist->backward) 8395a5ddeb6SWang Nan return perf_evlist__mmap_read_forward(evlist, idx); 8405a5ddeb6SWang Nan return perf_evlist__mmap_read_backward(evlist, idx); 8415a5ddeb6SWang Nan } 8425a5ddeb6SWang Nan 843e24c7520SWang Nan void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx) 844e24c7520SWang Nan { 845e24c7520SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 846e24c7520SWang Nan u64 head; 847e24c7520SWang Nan 848e24c7520SWang Nan if (!atomic_read(&md->refcnt)) 849e24c7520SWang Nan return; 850e24c7520SWang Nan 851e24c7520SWang Nan head = perf_mmap__read_head(md); 852e24c7520SWang Nan md->prev = head; 853e24c7520SWang Nan } 854e24c7520SWang Nan 85582396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md) 85682396986SArnaldo Carvalho de Melo { 857b72e74d1SAdrian Hunter return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base; 85882396986SArnaldo Carvalho de Melo } 85982396986SArnaldo Carvalho de Melo 86082396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx) 86182396986SArnaldo Carvalho de Melo { 8627143849aSArnaldo Carvalho de Melo atomic_inc(&evlist->mmap[idx].refcnt); 86382396986SArnaldo Carvalho de Melo } 86482396986SArnaldo Carvalho de Melo 86582396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx) 86682396986SArnaldo Carvalho de Melo { 867e10e4ef6SWang Nan struct perf_mmap *md = &evlist->mmap[idx]; 86882396986SArnaldo Carvalho de Melo 869e10e4ef6SWang Nan BUG_ON(md->base && atomic_read(&md->refcnt) == 0); 870e10e4ef6SWang Nan 871e10e4ef6SWang Nan if (atomic_dec_and_test(&md->refcnt)) 87282396986SArnaldo Carvalho de Melo __perf_evlist__munmap(evlist, idx); 87382396986SArnaldo Carvalho de Melo } 87482396986SArnaldo Carvalho de Melo 8758e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) 8768e50d384SZhouyi Zhou { 8778e50d384SZhouyi Zhou struct perf_mmap *md = &evlist->mmap[idx]; 87882396986SArnaldo Carvalho de Melo 87982396986SArnaldo Carvalho de Melo if (!evlist->overwrite) { 8807b8283b5SDavid Ahern u64 old = md->prev; 8818e50d384SZhouyi Zhou 8828e50d384SZhouyi Zhou perf_mmap__write_tail(md, old); 8838e50d384SZhouyi Zhou } 88482396986SArnaldo Carvalho de Melo 8857143849aSArnaldo Carvalho de Melo if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md)) 88682396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 8878e50d384SZhouyi Zhou } 8888e50d384SZhouyi Zhou 889718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, 890718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 891718c602dSAdrian Hunter void *userpg __maybe_unused, 892718c602dSAdrian Hunter int fd __maybe_unused) 893718c602dSAdrian Hunter { 894718c602dSAdrian Hunter return 0; 895718c602dSAdrian Hunter } 896718c602dSAdrian Hunter 897718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) 898718c602dSAdrian Hunter { 899718c602dSAdrian Hunter } 900718c602dSAdrian Hunter 901718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init( 902718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 903718c602dSAdrian Hunter off_t auxtrace_offset __maybe_unused, 904718c602dSAdrian Hunter unsigned int auxtrace_pages __maybe_unused, 905718c602dSAdrian Hunter bool auxtrace_overwrite __maybe_unused) 906718c602dSAdrian Hunter { 907718c602dSAdrian Hunter } 908718c602dSAdrian Hunter 909718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx( 910718c602dSAdrian Hunter struct auxtrace_mmap_params *mp __maybe_unused, 911718c602dSAdrian Hunter struct perf_evlist *evlist __maybe_unused, 912718c602dSAdrian Hunter int idx __maybe_unused, 913718c602dSAdrian Hunter bool per_cpu __maybe_unused) 914718c602dSAdrian Hunter { 915718c602dSAdrian Hunter } 916718c602dSAdrian Hunter 91793edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) 91893edcbd9SAdrian Hunter { 91993edcbd9SAdrian Hunter if (evlist->mmap[idx].base != NULL) { 92093edcbd9SAdrian Hunter munmap(evlist->mmap[idx].base, evlist->mmap_len); 92193edcbd9SAdrian Hunter evlist->mmap[idx].base = NULL; 922d4c6fb36SWang Nan evlist->mmap[idx].fd = -1; 9237143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 0); 92493edcbd9SAdrian Hunter } 925718c602dSAdrian Hunter auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap); 92693edcbd9SAdrian Hunter } 92793edcbd9SAdrian Hunter 9287e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist) 929f8a95309SArnaldo Carvalho de Melo { 930aece948fSArnaldo Carvalho de Melo int i; 931f8a95309SArnaldo Carvalho de Melo 932983874d1SArnaldo Carvalho de Melo if (evlist->mmap == NULL) 933983874d1SArnaldo Carvalho de Melo return; 934983874d1SArnaldo Carvalho de Melo 93593edcbd9SAdrian Hunter for (i = 0; i < evlist->nr_mmaps; i++) 93693edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, i); 937aece948fSArnaldo Carvalho de Melo 93804662523SArnaldo Carvalho de Melo zfree(&evlist->mmap); 939f8a95309SArnaldo Carvalho de Melo } 940f8a95309SArnaldo Carvalho de Melo 941806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) 942f8a95309SArnaldo Carvalho de Melo { 943d4c6fb36SWang Nan int i; 944d4c6fb36SWang Nan 945a14bb7a6SArnaldo Carvalho de Melo evlist->nr_mmaps = cpu_map__nr(evlist->cpus); 946ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(evlist->cpus)) 947b3a319d5SNamhyung Kim evlist->nr_mmaps = thread_map__nr(evlist->threads); 948aece948fSArnaldo Carvalho de Melo evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); 949946ae1d4SWang Nan if (!evlist->mmap) 950946ae1d4SWang Nan return -ENOMEM; 951946ae1d4SWang Nan 952d4c6fb36SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) 953d4c6fb36SWang Nan evlist->mmap[i].fd = -1; 954946ae1d4SWang Nan return 0; 955f8a95309SArnaldo Carvalho de Melo } 956f8a95309SArnaldo Carvalho de Melo 957a8a8f3ebSAdrian Hunter struct mmap_params { 958a8a8f3ebSAdrian Hunter int prot; 959a8a8f3ebSAdrian Hunter int mask; 960718c602dSAdrian Hunter struct auxtrace_mmap_params auxtrace_mp; 961a8a8f3ebSAdrian Hunter }; 962a8a8f3ebSAdrian Hunter 963a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, 964a8a8f3ebSAdrian Hunter struct mmap_params *mp, int fd) 965f8a95309SArnaldo Carvalho de Melo { 96682396986SArnaldo Carvalho de Melo /* 96782396986SArnaldo Carvalho de Melo * The last one will be done at perf_evlist__mmap_consume(), so that we 96882396986SArnaldo Carvalho de Melo * make sure we don't prevent tools from consuming every last event in 96982396986SArnaldo Carvalho de Melo * the ring buffer. 97082396986SArnaldo Carvalho de Melo * 97182396986SArnaldo Carvalho de Melo * I.e. we can get the POLLHUP meaning that the fd doesn't exist 97282396986SArnaldo Carvalho de Melo * anymore, but the last events for it are still in the ring buffer, 97382396986SArnaldo Carvalho de Melo * waiting to be consumed. 97482396986SArnaldo Carvalho de Melo * 97582396986SArnaldo Carvalho de Melo * Tools can chose to ignore this at their own discretion, but the 97682396986SArnaldo Carvalho de Melo * evlist layer can't just drop it when filtering events in 97782396986SArnaldo Carvalho de Melo * perf_evlist__filter_pollfd(). 97882396986SArnaldo Carvalho de Melo */ 9797143849aSArnaldo Carvalho de Melo atomic_set(&evlist->mmap[idx].refcnt, 2); 980aece948fSArnaldo Carvalho de Melo evlist->mmap[idx].prev = 0; 981a8a8f3ebSAdrian Hunter evlist->mmap[idx].mask = mp->mask; 982a8a8f3ebSAdrian Hunter evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, 983f8a95309SArnaldo Carvalho de Melo MAP_SHARED, fd, 0); 984301b195dSNelson Elhage if (evlist->mmap[idx].base == MAP_FAILED) { 98502635965SAdrian Hunter pr_debug2("failed to mmap perf event ring buffer, error %d\n", 98602635965SAdrian Hunter errno); 987301b195dSNelson Elhage evlist->mmap[idx].base = NULL; 988f8a95309SArnaldo Carvalho de Melo return -1; 989301b195dSNelson Elhage } 990d4c6fb36SWang Nan evlist->mmap[idx].fd = fd; 991ad6765ddSArnaldo Carvalho de Melo 992718c602dSAdrian Hunter if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap, 993718c602dSAdrian Hunter &mp->auxtrace_mp, evlist->mmap[idx].base, fd)) 994718c602dSAdrian Hunter return -1; 995718c602dSAdrian Hunter 996f8a95309SArnaldo Carvalho de Melo return 0; 997f8a95309SArnaldo Carvalho de Melo } 998f8a95309SArnaldo Carvalho de Melo 999f3058a1cSWang Nan static bool 1000f3058a1cSWang Nan perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused, 1001f3058a1cSWang Nan struct perf_evsel *evsel) 1002f3058a1cSWang Nan { 1003f3058a1cSWang Nan if (evsel->overwrite) 1004f3058a1cSWang Nan return false; 1005f3058a1cSWang Nan return true; 1006f3058a1cSWang Nan } 1007f3058a1cSWang Nan 100804e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, 1009a8a8f3ebSAdrian Hunter struct mmap_params *mp, int cpu, 1010a8a8f3ebSAdrian Hunter int thread, int *output) 1011aece948fSArnaldo Carvalho de Melo { 1012aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 1013f3058a1cSWang Nan int revent; 101404e21314SAdrian Hunter 10150050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1016bf8e8f4bSAdrian Hunter int fd; 1017bf8e8f4bSAdrian Hunter 1018f3058a1cSWang Nan if (evsel->overwrite != (evlist->overwrite && evlist->backward)) 1019f3058a1cSWang Nan continue; 1020f3058a1cSWang Nan 1021bf8e8f4bSAdrian Hunter if (evsel->system_wide && thread) 1022bf8e8f4bSAdrian Hunter continue; 1023bf8e8f4bSAdrian Hunter 1024bf8e8f4bSAdrian Hunter fd = FD(evsel, cpu, thread); 102504e21314SAdrian Hunter 102604e21314SAdrian Hunter if (*output == -1) { 102704e21314SAdrian Hunter *output = fd; 1028a8a8f3ebSAdrian Hunter if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) 102904e21314SAdrian Hunter return -1; 103004e21314SAdrian Hunter } else { 103104e21314SAdrian Hunter if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) 103204e21314SAdrian Hunter return -1; 103382396986SArnaldo Carvalho de Melo 103482396986SArnaldo Carvalho de Melo perf_evlist__mmap_get(evlist, idx); 103504e21314SAdrian Hunter } 103604e21314SAdrian Hunter 1037f3058a1cSWang Nan revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; 1038f3058a1cSWang Nan 1039f90d194aSAdrian Hunter /* 1040f90d194aSAdrian Hunter * The system_wide flag causes a selected event to be opened 1041f90d194aSAdrian Hunter * always without a pid. Consequently it will never get a 1042f90d194aSAdrian Hunter * POLLHUP, but it is used for tracking in combination with 1043f90d194aSAdrian Hunter * other events, so it should not need to be polled anyway. 1044f90d194aSAdrian Hunter * Therefore don't add it for polling. 1045f90d194aSAdrian Hunter */ 1046f90d194aSAdrian Hunter if (!evsel->system_wide && 1047f3058a1cSWang Nan __perf_evlist__add_pollfd(evlist, fd, idx, revent) < 0) { 104882396986SArnaldo Carvalho de Melo perf_evlist__mmap_put(evlist, idx); 1049ad6765ddSArnaldo Carvalho de Melo return -1; 105082396986SArnaldo Carvalho de Melo } 1051033fa713SArnaldo Carvalho de Melo 10523c659eedSAdrian Hunter if (evsel->attr.read_format & PERF_FORMAT_ID) { 10533c659eedSAdrian Hunter if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, 10543c659eedSAdrian Hunter fd) < 0) 105504e21314SAdrian Hunter return -1; 10563c659eedSAdrian Hunter perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, 10573c659eedSAdrian Hunter thread); 10583c659eedSAdrian Hunter } 105904e21314SAdrian Hunter } 106004e21314SAdrian Hunter 106104e21314SAdrian Hunter return 0; 106204e21314SAdrian Hunter } 106304e21314SAdrian Hunter 1064a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, 1065a8a8f3ebSAdrian Hunter struct mmap_params *mp) 106604e21314SAdrian Hunter { 1067aece948fSArnaldo Carvalho de Melo int cpu, thread; 1068b3a319d5SNamhyung Kim int nr_cpus = cpu_map__nr(evlist->cpus); 1069b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 1070aece948fSArnaldo Carvalho de Melo 1071e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per cpu\n"); 1072b3a319d5SNamhyung Kim for (cpu = 0; cpu < nr_cpus; cpu++) { 1073aece948fSArnaldo Carvalho de Melo int output = -1; 1074aece948fSArnaldo Carvalho de Melo 1075718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, 1076718c602dSAdrian Hunter true); 1077718c602dSAdrian Hunter 1078b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 1079a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, 1080a8a8f3ebSAdrian Hunter thread, &output)) 1081aece948fSArnaldo Carvalho de Melo goto out_unmap; 1082aece948fSArnaldo Carvalho de Melo } 1083aece948fSArnaldo Carvalho de Melo } 1084aece948fSArnaldo Carvalho de Melo 1085aece948fSArnaldo Carvalho de Melo return 0; 1086aece948fSArnaldo Carvalho de Melo 1087aece948fSArnaldo Carvalho de Melo out_unmap: 108893edcbd9SAdrian Hunter for (cpu = 0; cpu < nr_cpus; cpu++) 108993edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, cpu); 1090aece948fSArnaldo Carvalho de Melo return -1; 1091aece948fSArnaldo Carvalho de Melo } 1092aece948fSArnaldo Carvalho de Melo 1093a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, 1094a8a8f3ebSAdrian Hunter struct mmap_params *mp) 1095aece948fSArnaldo Carvalho de Melo { 1096aece948fSArnaldo Carvalho de Melo int thread; 1097b3a319d5SNamhyung Kim int nr_threads = thread_map__nr(evlist->threads); 1098aece948fSArnaldo Carvalho de Melo 1099e3e1a54fSAdrian Hunter pr_debug2("perf event ring buffer mmapped per thread\n"); 1100b3a319d5SNamhyung Kim for (thread = 0; thread < nr_threads; thread++) { 1101aece948fSArnaldo Carvalho de Melo int output = -1; 1102aece948fSArnaldo Carvalho de Melo 1103718c602dSAdrian Hunter auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, 1104718c602dSAdrian Hunter false); 1105718c602dSAdrian Hunter 1106a8a8f3ebSAdrian Hunter if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, 1107a8a8f3ebSAdrian Hunter &output)) 1108aece948fSArnaldo Carvalho de Melo goto out_unmap; 1109aece948fSArnaldo Carvalho de Melo } 1110aece948fSArnaldo Carvalho de Melo 1111aece948fSArnaldo Carvalho de Melo return 0; 1112aece948fSArnaldo Carvalho de Melo 1113aece948fSArnaldo Carvalho de Melo out_unmap: 111493edcbd9SAdrian Hunter for (thread = 0; thread < nr_threads; thread++) 111593edcbd9SAdrian Hunter __perf_evlist__munmap(evlist, thread); 1116aece948fSArnaldo Carvalho de Melo return -1; 1117aece948fSArnaldo Carvalho de Melo } 1118aece948fSArnaldo Carvalho de Melo 1119f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void) 1120994a1f78SJiri Olsa { 1121f5e7150cSArnaldo Carvalho de Melo unsigned long pages; 11228185e881SArnaldo Carvalho de Melo int max; 11238185e881SArnaldo Carvalho de Melo 11248185e881SArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { 11258185e881SArnaldo Carvalho de Melo /* 11268185e881SArnaldo Carvalho de Melo * Pick a once upon a time good value, i.e. things look 11278185e881SArnaldo Carvalho de Melo * strange since we can't read a sysctl value, but lets not 11288185e881SArnaldo Carvalho de Melo * die yet... 11298185e881SArnaldo Carvalho de Melo */ 11308185e881SArnaldo Carvalho de Melo max = 512; 11318185e881SArnaldo Carvalho de Melo } else { 11328185e881SArnaldo Carvalho de Melo max -= (page_size / 1024); 11338185e881SArnaldo Carvalho de Melo } 11348185e881SArnaldo Carvalho de Melo 11358185e881SArnaldo Carvalho de Melo pages = (max * 1024) / page_size; 11361be300f4SArnaldo Carvalho de Melo if (!is_power_of_2(pages)) 11371be300f4SArnaldo Carvalho de Melo pages = rounddown_pow_of_two(pages); 1138f5e7150cSArnaldo Carvalho de Melo 1139f5e7150cSArnaldo Carvalho de Melo return pages; 1140f5e7150cSArnaldo Carvalho de Melo } 1141f5e7150cSArnaldo Carvalho de Melo 1142f5e7150cSArnaldo Carvalho de Melo static size_t perf_evlist__mmap_size(unsigned long pages) 1143f5e7150cSArnaldo Carvalho de Melo { 1144f5e7150cSArnaldo Carvalho de Melo if (pages == UINT_MAX) 1145f5e7150cSArnaldo Carvalho de Melo pages = perf_event_mlock_kb_in_pages(); 1146f5e7150cSArnaldo Carvalho de Melo else if (!is_power_of_2(pages)) 1147994a1f78SJiri Olsa return 0; 1148994a1f78SJiri Olsa 1149994a1f78SJiri Olsa return (pages + 1) * page_size; 1150994a1f78SJiri Olsa } 1151994a1f78SJiri Olsa 115233c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min, 115333c2dcfdSDavid Ahern unsigned long max) 1154994a1f78SJiri Olsa { 11552fbe4abeSAdrian Hunter unsigned long pages, val; 115627050f53SJiri Olsa static struct parse_tag tags[] = { 115727050f53SJiri Olsa { .tag = 'B', .mult = 1 }, 115827050f53SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 115927050f53SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 116027050f53SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 116127050f53SJiri Olsa { .tag = 0 }, 116227050f53SJiri Olsa }; 1163994a1f78SJiri Olsa 11648973504bSDavid Ahern if (str == NULL) 116533c2dcfdSDavid Ahern return -EINVAL; 11668973504bSDavid Ahern 116727050f53SJiri Olsa val = parse_tag_value(str, tags); 11682fbe4abeSAdrian Hunter if (val != (unsigned long) -1) { 116927050f53SJiri Olsa /* we got file size value */ 117027050f53SJiri Olsa pages = PERF_ALIGN(val, page_size) / page_size; 117127050f53SJiri Olsa } else { 117227050f53SJiri Olsa /* we got pages count value */ 117327050f53SJiri Olsa char *eptr; 1174994a1f78SJiri Olsa pages = strtoul(str, &eptr, 10); 117533c2dcfdSDavid Ahern if (*eptr != '\0') 117633c2dcfdSDavid Ahern return -EINVAL; 117727050f53SJiri Olsa } 1178994a1f78SJiri Olsa 11792bcab6c1SAdrian Hunter if (pages == 0 && min == 0) { 118033c2dcfdSDavid Ahern /* leave number of pages at 0 */ 11811dbfa938SAdrian Hunter } else if (!is_power_of_2(pages)) { 118233c2dcfdSDavid Ahern /* round pages up to next power of 2 */ 118391529834SArnaldo Carvalho de Melo pages = roundup_pow_of_two(pages); 11841dbfa938SAdrian Hunter if (!pages) 11851dbfa938SAdrian Hunter return -EINVAL; 11869639837eSDavid Ahern pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", 11879639837eSDavid Ahern pages * page_size, pages); 11882fbe4abeSAdrian Hunter } 11892fbe4abeSAdrian Hunter 119033c2dcfdSDavid Ahern if (pages > max) 119133c2dcfdSDavid Ahern return -EINVAL; 119233c2dcfdSDavid Ahern 119333c2dcfdSDavid Ahern return pages; 119433c2dcfdSDavid Ahern } 119533c2dcfdSDavid Ahern 1196e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) 119733c2dcfdSDavid Ahern { 119833c2dcfdSDavid Ahern unsigned long max = UINT_MAX; 119933c2dcfdSDavid Ahern long pages; 120033c2dcfdSDavid Ahern 1201f5ae9c42SAdrian Hunter if (max > SIZE_MAX / page_size) 120233c2dcfdSDavid Ahern max = SIZE_MAX / page_size; 120333c2dcfdSDavid Ahern 120433c2dcfdSDavid Ahern pages = parse_pages_arg(str, 1, max); 120533c2dcfdSDavid Ahern if (pages < 0) { 120633c2dcfdSDavid Ahern pr_err("Invalid argument for --mmap_pages/-m\n"); 1207994a1f78SJiri Olsa return -1; 1208994a1f78SJiri Olsa } 1209994a1f78SJiri Olsa 1210994a1f78SJiri Olsa *mmap_pages = pages; 1211994a1f78SJiri Olsa return 0; 1212994a1f78SJiri Olsa } 1213994a1f78SJiri Olsa 1214e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, 1215e9db1310SAdrian Hunter int unset __maybe_unused) 1216e9db1310SAdrian Hunter { 1217e9db1310SAdrian Hunter return __perf_evlist__parse_mmap_pages(opt->value, str); 1218e9db1310SAdrian Hunter } 1219e9db1310SAdrian Hunter 1220c83fa7f2SAdrian Hunter /** 1221718c602dSAdrian Hunter * perf_evlist__mmap_ex - Create mmaps to receive events. 1222c83fa7f2SAdrian Hunter * @evlist: list of events 1223c83fa7f2SAdrian Hunter * @pages: map length in pages 1224c83fa7f2SAdrian Hunter * @overwrite: overwrite older events? 1225718c602dSAdrian Hunter * @auxtrace_pages - auxtrace map length in pages 1226718c602dSAdrian Hunter * @auxtrace_overwrite - overwrite older auxtrace data? 1227f8a95309SArnaldo Carvalho de Melo * 1228c83fa7f2SAdrian Hunter * If @overwrite is %false the user needs to signal event consumption using 1229c83fa7f2SAdrian Hunter * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this 1230c83fa7f2SAdrian Hunter * automatically. 1231f8a95309SArnaldo Carvalho de Melo * 1232718c602dSAdrian Hunter * Similarly, if @auxtrace_overwrite is %false the user needs to signal data 1233718c602dSAdrian Hunter * consumption using auxtrace_mmap__write_tail(). 1234718c602dSAdrian Hunter * 1235c83fa7f2SAdrian Hunter * Return: %0 on success, negative error code otherwise. 1236f8a95309SArnaldo Carvalho de Melo */ 1237718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, 1238718c602dSAdrian Hunter bool overwrite, unsigned int auxtrace_pages, 1239718c602dSAdrian Hunter bool auxtrace_overwrite) 1240f8a95309SArnaldo Carvalho de Melo { 1241aece948fSArnaldo Carvalho de Melo struct perf_evsel *evsel; 12427e2ed097SArnaldo Carvalho de Melo const struct cpu_map *cpus = evlist->cpus; 12437e2ed097SArnaldo Carvalho de Melo const struct thread_map *threads = evlist->threads; 1244a8a8f3ebSAdrian Hunter struct mmap_params mp = { 1245a8a8f3ebSAdrian Hunter .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), 1246a8a8f3ebSAdrian Hunter }; 124750a682ceSArnaldo Carvalho de Melo 12487e2ed097SArnaldo Carvalho de Melo if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) 1249f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1250f8a95309SArnaldo Carvalho de Melo 12511b85337dSArnaldo Carvalho de Melo if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) 1252f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1253f8a95309SArnaldo Carvalho de Melo 1254f8a95309SArnaldo Carvalho de Melo evlist->overwrite = overwrite; 1255994a1f78SJiri Olsa evlist->mmap_len = perf_evlist__mmap_size(pages); 12562af68ef5SAdrian Hunter pr_debug("mmap size %zuB\n", evlist->mmap_len); 1257a8a8f3ebSAdrian Hunter mp.mask = evlist->mmap_len - page_size - 1; 1258f8a95309SArnaldo Carvalho de Melo 1259718c602dSAdrian Hunter auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, 1260718c602dSAdrian Hunter auxtrace_pages, auxtrace_overwrite); 1261718c602dSAdrian Hunter 12620050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1263f8a95309SArnaldo Carvalho de Melo if ((evsel->attr.read_format & PERF_FORMAT_ID) && 1264a91e5431SArnaldo Carvalho de Melo evsel->sample_id == NULL && 1265a14bb7a6SArnaldo Carvalho de Melo perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) 1266f8a95309SArnaldo Carvalho de Melo return -ENOMEM; 1267f8a95309SArnaldo Carvalho de Melo } 1268f8a95309SArnaldo Carvalho de Melo 1269ec1e7e43SSukadev Bhattiprolu if (cpu_map__empty(cpus)) 1270a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_thread(evlist, &mp); 1271f8a95309SArnaldo Carvalho de Melo 1272a8a8f3ebSAdrian Hunter return perf_evlist__mmap_per_cpu(evlist, &mp); 1273f8a95309SArnaldo Carvalho de Melo } 12747e2ed097SArnaldo Carvalho de Melo 1275718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, 1276718c602dSAdrian Hunter bool overwrite) 1277718c602dSAdrian Hunter { 1278718c602dSAdrian Hunter return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false); 1279718c602dSAdrian Hunter } 1280718c602dSAdrian Hunter 1281602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) 12827e2ed097SArnaldo Carvalho de Melo { 128374bfd2b2SAdrian Hunter struct cpu_map *cpus; 128474bfd2b2SAdrian Hunter struct thread_map *threads; 12857e2ed097SArnaldo Carvalho de Melo 128674bfd2b2SAdrian Hunter threads = thread_map__new_str(target->pid, target->tid, target->uid); 128774bfd2b2SAdrian Hunter 128874bfd2b2SAdrian Hunter if (!threads) 12897e2ed097SArnaldo Carvalho de Melo return -1; 12907e2ed097SArnaldo Carvalho de Melo 12919c105fbcSDongsheng Yang if (target__uses_dummy_map(target)) 129274bfd2b2SAdrian Hunter cpus = cpu_map__dummy_new(); 1293879d77d0SNamhyung Kim else 129474bfd2b2SAdrian Hunter cpus = cpu_map__new(target->cpu_list); 12957e2ed097SArnaldo Carvalho de Melo 129674bfd2b2SAdrian Hunter if (!cpus) 12977e2ed097SArnaldo Carvalho de Melo goto out_delete_threads; 12987e2ed097SArnaldo Carvalho de Melo 1299ec9a77a7SAdrian Hunter evlist->has_user_cpus = !!target->cpu_list; 1300ec9a77a7SAdrian Hunter 130174bfd2b2SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 1302d5bc056eSAdrian Hunter 1303d5bc056eSAdrian Hunter return 0; 13047e2ed097SArnaldo Carvalho de Melo 13057e2ed097SArnaldo Carvalho de Melo out_delete_threads: 130674bfd2b2SAdrian Hunter thread_map__put(threads); 13077e2ed097SArnaldo Carvalho de Melo return -1; 13087e2ed097SArnaldo Carvalho de Melo } 13097e2ed097SArnaldo Carvalho de Melo 1310d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, 13113de5cfb0SJiri Olsa struct thread_map *threads) 13123de5cfb0SJiri Olsa { 1313934e0f20SAdrian Hunter /* 1314934e0f20SAdrian Hunter * Allow for the possibility that one or another of the maps isn't being 1315934e0f20SAdrian Hunter * changed i.e. don't put it. Note we are assuming the maps that are 1316934e0f20SAdrian Hunter * being applied are brand new and evlist is taking ownership of the 1317934e0f20SAdrian Hunter * original reference count of 1. If that is not the case it is up to 1318934e0f20SAdrian Hunter * the caller to increase the reference count. 1319934e0f20SAdrian Hunter */ 1320934e0f20SAdrian Hunter if (cpus != evlist->cpus) { 13213de5cfb0SJiri Olsa cpu_map__put(evlist->cpus); 1322a55e5663SArnaldo Carvalho de Melo evlist->cpus = cpu_map__get(cpus); 1323934e0f20SAdrian Hunter } 13243de5cfb0SJiri Olsa 1325934e0f20SAdrian Hunter if (threads != evlist->threads) { 13263de5cfb0SJiri Olsa thread_map__put(evlist->threads); 1327a55e5663SArnaldo Carvalho de Melo evlist->threads = thread_map__get(threads); 1328934e0f20SAdrian Hunter } 13293de5cfb0SJiri Olsa 1330ec9a77a7SAdrian Hunter perf_evlist__propagate_maps(evlist); 13313de5cfb0SJiri Olsa } 13323de5cfb0SJiri Olsa 133322c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist, 133422c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 133522c8a376SArnaldo Carvalho de Melo { 133622c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 133722c8a376SArnaldo Carvalho de Melo 133822c8a376SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 133922c8a376SArnaldo Carvalho de Melo __perf_evsel__set_sample_bit(evsel, bit); 134022c8a376SArnaldo Carvalho de Melo } 134122c8a376SArnaldo Carvalho de Melo 134222c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist, 134322c8a376SArnaldo Carvalho de Melo enum perf_event_sample_format bit) 134422c8a376SArnaldo Carvalho de Melo { 134522c8a376SArnaldo Carvalho de Melo struct perf_evsel *evsel; 134622c8a376SArnaldo Carvalho de Melo 134722c8a376SArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 134822c8a376SArnaldo Carvalho de Melo __perf_evsel__reset_sample_bit(evsel, bit); 134922c8a376SArnaldo Carvalho de Melo } 135022c8a376SArnaldo Carvalho de Melo 135123d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) 13520a102479SFrederic Weisbecker { 13530a102479SFrederic Weisbecker struct perf_evsel *evsel; 1354745cefc5SArnaldo Carvalho de Melo int err = 0; 1355745cefc5SArnaldo Carvalho de Melo const int ncpus = cpu_map__nr(evlist->cpus), 1356b3a319d5SNamhyung Kim nthreads = thread_map__nr(evlist->threads); 13570a102479SFrederic Weisbecker 13580050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1359745cefc5SArnaldo Carvalho de Melo if (evsel->filter == NULL) 13600a102479SFrederic Weisbecker continue; 1361745cefc5SArnaldo Carvalho de Melo 1362d988d5eeSKan Liang /* 1363d988d5eeSKan Liang * filters only work for tracepoint event, which doesn't have cpu limit. 1364d988d5eeSKan Liang * So evlist and evsel should always be same. 1365d988d5eeSKan Liang */ 1366f47805a2SArnaldo Carvalho de Melo err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter); 136723d4aad4SArnaldo Carvalho de Melo if (err) { 136823d4aad4SArnaldo Carvalho de Melo *err_evsel = evsel; 1369745cefc5SArnaldo Carvalho de Melo break; 13700a102479SFrederic Weisbecker } 137123d4aad4SArnaldo Carvalho de Melo } 13720a102479SFrederic Weisbecker 1373745cefc5SArnaldo Carvalho de Melo return err; 1374745cefc5SArnaldo Carvalho de Melo } 1375745cefc5SArnaldo Carvalho de Melo 1376745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) 1377745cefc5SArnaldo Carvalho de Melo { 1378745cefc5SArnaldo Carvalho de Melo struct perf_evsel *evsel; 1379745cefc5SArnaldo Carvalho de Melo int err = 0; 1380745cefc5SArnaldo Carvalho de Melo 13810050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 1382fdf14720SWang Nan if (evsel->attr.type != PERF_TYPE_TRACEPOINT) 1383fdf14720SWang Nan continue; 1384fdf14720SWang Nan 138594ad89bcSArnaldo Carvalho de Melo err = perf_evsel__set_filter(evsel, filter); 1386745cefc5SArnaldo Carvalho de Melo if (err) 1387745cefc5SArnaldo Carvalho de Melo break; 1388745cefc5SArnaldo Carvalho de Melo } 1389745cefc5SArnaldo Carvalho de Melo 1390745cefc5SArnaldo Carvalho de Melo return err; 13910a102479SFrederic Weisbecker } 139274429964SFrederic Weisbecker 1393be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) 1394cfd70a26SArnaldo Carvalho de Melo { 1395cfd70a26SArnaldo Carvalho de Melo char *filter; 1396be199adaSArnaldo Carvalho de Melo int ret = -1; 1397be199adaSArnaldo Carvalho de Melo size_t i; 1398cfd70a26SArnaldo Carvalho de Melo 1399be199adaSArnaldo Carvalho de Melo for (i = 0; i < npids; ++i) { 1400be199adaSArnaldo Carvalho de Melo if (i == 0) { 1401be199adaSArnaldo Carvalho de Melo if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) 1402cfd70a26SArnaldo Carvalho de Melo return -1; 1403be199adaSArnaldo Carvalho de Melo } else { 1404be199adaSArnaldo Carvalho de Melo char *tmp; 1405be199adaSArnaldo Carvalho de Melo 1406be199adaSArnaldo Carvalho de Melo if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) 1407be199adaSArnaldo Carvalho de Melo goto out_free; 1408be199adaSArnaldo Carvalho de Melo 1409be199adaSArnaldo Carvalho de Melo free(filter); 1410be199adaSArnaldo Carvalho de Melo filter = tmp; 1411be199adaSArnaldo Carvalho de Melo } 1412be199adaSArnaldo Carvalho de Melo } 1413cfd70a26SArnaldo Carvalho de Melo 1414cfd70a26SArnaldo Carvalho de Melo ret = perf_evlist__set_filter(evlist, filter); 1415be199adaSArnaldo Carvalho de Melo out_free: 1416cfd70a26SArnaldo Carvalho de Melo free(filter); 1417cfd70a26SArnaldo Carvalho de Melo return ret; 1418cfd70a26SArnaldo Carvalho de Melo } 1419cfd70a26SArnaldo Carvalho de Melo 1420be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) 1421be199adaSArnaldo Carvalho de Melo { 1422be199adaSArnaldo Carvalho de Melo return perf_evlist__set_filter_pids(evlist, 1, &pid); 1423be199adaSArnaldo Carvalho de Melo } 1424be199adaSArnaldo Carvalho de Melo 14250c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) 142674429964SFrederic Weisbecker { 142775562573SAdrian Hunter struct perf_evsel *pos; 1428c2a70653SArnaldo Carvalho de Melo 142975562573SAdrian Hunter if (evlist->nr_entries == 1) 143075562573SAdrian Hunter return true; 143175562573SAdrian Hunter 143275562573SAdrian Hunter if (evlist->id_pos < 0 || evlist->is_pos < 0) 143375562573SAdrian Hunter return false; 143475562573SAdrian Hunter 14350050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 143675562573SAdrian Hunter if (pos->id_pos != evlist->id_pos || 143775562573SAdrian Hunter pos->is_pos != evlist->is_pos) 1438c2a70653SArnaldo Carvalho de Melo return false; 143974429964SFrederic Weisbecker } 144074429964SFrederic Weisbecker 1441c2a70653SArnaldo Carvalho de Melo return true; 1442c2a70653SArnaldo Carvalho de Melo } 1443c2a70653SArnaldo Carvalho de Melo 144475562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) 1445c2a70653SArnaldo Carvalho de Melo { 144675562573SAdrian Hunter struct perf_evsel *evsel; 144775562573SAdrian Hunter 144875562573SAdrian Hunter if (evlist->combined_sample_type) 144975562573SAdrian Hunter return evlist->combined_sample_type; 145075562573SAdrian Hunter 14510050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) 145275562573SAdrian Hunter evlist->combined_sample_type |= evsel->attr.sample_type; 145375562573SAdrian Hunter 145475562573SAdrian Hunter return evlist->combined_sample_type; 145575562573SAdrian Hunter } 145675562573SAdrian Hunter 145775562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) 145875562573SAdrian Hunter { 145975562573SAdrian Hunter evlist->combined_sample_type = 0; 146075562573SAdrian Hunter return __perf_evlist__combined_sample_type(evlist); 1461c2a70653SArnaldo Carvalho de Melo } 1462c2a70653SArnaldo Carvalho de Melo 146398df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) 146498df858eSAndi Kleen { 146598df858eSAndi Kleen struct perf_evsel *evsel; 146698df858eSAndi Kleen u64 branch_type = 0; 146798df858eSAndi Kleen 146898df858eSAndi Kleen evlist__for_each(evlist, evsel) 146998df858eSAndi Kleen branch_type |= evsel->attr.branch_sample_type; 147098df858eSAndi Kleen return branch_type; 147198df858eSAndi Kleen } 147298df858eSAndi Kleen 14739ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist) 14749ede473cSJiri Olsa { 14759ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 14769ede473cSJiri Olsa u64 read_format = first->attr.read_format; 14779ede473cSJiri Olsa u64 sample_type = first->attr.sample_type; 14789ede473cSJiri Olsa 14790050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, pos) { 14809ede473cSJiri Olsa if (read_format != pos->attr.read_format) 14819ede473cSJiri Olsa return false; 14829ede473cSJiri Olsa } 14839ede473cSJiri Olsa 14849ede473cSJiri Olsa /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ 14859ede473cSJiri Olsa if ((sample_type & PERF_SAMPLE_READ) && 14869ede473cSJiri Olsa !(read_format & PERF_FORMAT_ID)) { 14879ede473cSJiri Olsa return false; 14889ede473cSJiri Olsa } 14899ede473cSJiri Olsa 14909ede473cSJiri Olsa return true; 14919ede473cSJiri Olsa } 14929ede473cSJiri Olsa 14939ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist) 14949ede473cSJiri Olsa { 14959ede473cSJiri Olsa struct perf_evsel *first = perf_evlist__first(evlist); 14969ede473cSJiri Olsa return first->attr.read_format; 14979ede473cSJiri Olsa } 14989ede473cSJiri Olsa 14990c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) 150081e36bffSArnaldo Carvalho de Melo { 15010c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 150281e36bffSArnaldo Carvalho de Melo struct perf_sample *data; 150381e36bffSArnaldo Carvalho de Melo u64 sample_type; 150481e36bffSArnaldo Carvalho de Melo u16 size = 0; 150581e36bffSArnaldo Carvalho de Melo 150681e36bffSArnaldo Carvalho de Melo if (!first->attr.sample_id_all) 150781e36bffSArnaldo Carvalho de Melo goto out; 150881e36bffSArnaldo Carvalho de Melo 150981e36bffSArnaldo Carvalho de Melo sample_type = first->attr.sample_type; 151081e36bffSArnaldo Carvalho de Melo 151181e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TID) 151281e36bffSArnaldo Carvalho de Melo size += sizeof(data->tid) * 2; 151381e36bffSArnaldo Carvalho de Melo 151481e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_TIME) 151581e36bffSArnaldo Carvalho de Melo size += sizeof(data->time); 151681e36bffSArnaldo Carvalho de Melo 151781e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_ID) 151881e36bffSArnaldo Carvalho de Melo size += sizeof(data->id); 151981e36bffSArnaldo Carvalho de Melo 152081e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_STREAM_ID) 152181e36bffSArnaldo Carvalho de Melo size += sizeof(data->stream_id); 152281e36bffSArnaldo Carvalho de Melo 152381e36bffSArnaldo Carvalho de Melo if (sample_type & PERF_SAMPLE_CPU) 152481e36bffSArnaldo Carvalho de Melo size += sizeof(data->cpu) * 2; 152575562573SAdrian Hunter 152675562573SAdrian Hunter if (sample_type & PERF_SAMPLE_IDENTIFIER) 152775562573SAdrian Hunter size += sizeof(data->id); 152881e36bffSArnaldo Carvalho de Melo out: 152981e36bffSArnaldo Carvalho de Melo return size; 153081e36bffSArnaldo Carvalho de Melo } 153181e36bffSArnaldo Carvalho de Melo 15320c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) 1533c2a70653SArnaldo Carvalho de Melo { 15340c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; 1535c2a70653SArnaldo Carvalho de Melo 15360050f7aaSArnaldo Carvalho de Melo evlist__for_each_continue(evlist, pos) { 1537c2a70653SArnaldo Carvalho de Melo if (first->attr.sample_id_all != pos->attr.sample_id_all) 1538c2a70653SArnaldo Carvalho de Melo return false; 1539c2a70653SArnaldo Carvalho de Melo } 1540c2a70653SArnaldo Carvalho de Melo 1541c2a70653SArnaldo Carvalho de Melo return true; 154274429964SFrederic Weisbecker } 154374429964SFrederic Weisbecker 15440c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist) 154574429964SFrederic Weisbecker { 15460c21f736SArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1547c2a70653SArnaldo Carvalho de Melo return first->attr.sample_id_all; 154874429964SFrederic Weisbecker } 154981cce8deSArnaldo Carvalho de Melo 155081cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist, 155181cce8deSArnaldo Carvalho de Melo struct perf_evsel *evsel) 155281cce8deSArnaldo Carvalho de Melo { 155381cce8deSArnaldo Carvalho de Melo evlist->selected = evsel; 155481cce8deSArnaldo Carvalho de Melo } 1555727ab04eSArnaldo Carvalho de Melo 1556a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist) 1557a74b4b66SNamhyung Kim { 1558a74b4b66SNamhyung Kim struct perf_evsel *evsel; 1559a74b4b66SNamhyung Kim int ncpus = cpu_map__nr(evlist->cpus); 1560a74b4b66SNamhyung Kim int nthreads = thread_map__nr(evlist->threads); 15618ad9219eSStephane Eranian int n; 1562a74b4b66SNamhyung Kim 15638ad9219eSStephane Eranian evlist__for_each_reverse(evlist, evsel) { 15648ad9219eSStephane Eranian n = evsel->cpus ? evsel->cpus->nr : ncpus; 15658ad9219eSStephane Eranian perf_evsel__close(evsel, n, nthreads); 15668ad9219eSStephane Eranian } 1567a74b4b66SNamhyung Kim } 1568a74b4b66SNamhyung Kim 15694112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) 15704112eb18SArnaldo Carvalho de Melo { 15718c0498b6SAdrian Hunter struct cpu_map *cpus; 15728c0498b6SAdrian Hunter struct thread_map *threads; 15734112eb18SArnaldo Carvalho de Melo int err = -ENOMEM; 15744112eb18SArnaldo Carvalho de Melo 15754112eb18SArnaldo Carvalho de Melo /* 15764112eb18SArnaldo Carvalho de Melo * Try reading /sys/devices/system/cpu/online to get 15774112eb18SArnaldo Carvalho de Melo * an all cpus map. 15784112eb18SArnaldo Carvalho de Melo * 15794112eb18SArnaldo Carvalho de Melo * FIXME: -ENOMEM is the best we can do here, the cpu_map 15804112eb18SArnaldo Carvalho de Melo * code needs an overhaul to properly forward the 15814112eb18SArnaldo Carvalho de Melo * error, and we may not want to do that fallback to a 15824112eb18SArnaldo Carvalho de Melo * default cpu identity map :-\ 15834112eb18SArnaldo Carvalho de Melo */ 15848c0498b6SAdrian Hunter cpus = cpu_map__new(NULL); 15858c0498b6SAdrian Hunter if (!cpus) 15864112eb18SArnaldo Carvalho de Melo goto out; 15874112eb18SArnaldo Carvalho de Melo 15888c0498b6SAdrian Hunter threads = thread_map__new_dummy(); 15898c0498b6SAdrian Hunter if (!threads) 15908c0498b6SAdrian Hunter goto out_put; 15914112eb18SArnaldo Carvalho de Melo 15928c0498b6SAdrian Hunter perf_evlist__set_maps(evlist, cpus, threads); 15934112eb18SArnaldo Carvalho de Melo out: 15944112eb18SArnaldo Carvalho de Melo return err; 15958c0498b6SAdrian Hunter out_put: 15968c0498b6SAdrian Hunter cpu_map__put(cpus); 15974112eb18SArnaldo Carvalho de Melo goto out; 15984112eb18SArnaldo Carvalho de Melo } 15994112eb18SArnaldo Carvalho de Melo 16006a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist) 1601727ab04eSArnaldo Carvalho de Melo { 16026a4bb04cSJiri Olsa struct perf_evsel *evsel; 1603a74b4b66SNamhyung Kim int err; 1604727ab04eSArnaldo Carvalho de Melo 16054112eb18SArnaldo Carvalho de Melo /* 16064112eb18SArnaldo Carvalho de Melo * Default: one fd per CPU, all threads, aka systemwide 16074112eb18SArnaldo Carvalho de Melo * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL 16084112eb18SArnaldo Carvalho de Melo */ 16094112eb18SArnaldo Carvalho de Melo if (evlist->threads == NULL && evlist->cpus == NULL) { 16104112eb18SArnaldo Carvalho de Melo err = perf_evlist__create_syswide_maps(evlist); 16114112eb18SArnaldo Carvalho de Melo if (err < 0) 16124112eb18SArnaldo Carvalho de Melo goto out_err; 16134112eb18SArnaldo Carvalho de Melo } 16144112eb18SArnaldo Carvalho de Melo 1615733cd2feSAdrian Hunter perf_evlist__update_id_pos(evlist); 1616733cd2feSAdrian Hunter 16170050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 161823df7f79SAdrian Hunter err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); 1619727ab04eSArnaldo Carvalho de Melo if (err < 0) 1620727ab04eSArnaldo Carvalho de Melo goto out_err; 1621727ab04eSArnaldo Carvalho de Melo } 1622727ab04eSArnaldo Carvalho de Melo 1623727ab04eSArnaldo Carvalho de Melo return 0; 1624727ab04eSArnaldo Carvalho de Melo out_err: 1625a74b4b66SNamhyung Kim perf_evlist__close(evlist); 162641c21a68SNamhyung Kim errno = -err; 1627727ab04eSArnaldo Carvalho de Melo return err; 1628727ab04eSArnaldo Carvalho de Melo } 162935b9d88eSArnaldo Carvalho de Melo 1630602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, 163155e162eaSNamhyung Kim const char *argv[], bool pipe_output, 1632735f7e0bSArnaldo Carvalho de Melo void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) 163335b9d88eSArnaldo Carvalho de Melo { 163435b9d88eSArnaldo Carvalho de Melo int child_ready_pipe[2], go_pipe[2]; 163535b9d88eSArnaldo Carvalho de Melo char bf; 163635b9d88eSArnaldo Carvalho de Melo 163735b9d88eSArnaldo Carvalho de Melo if (pipe(child_ready_pipe) < 0) { 163835b9d88eSArnaldo Carvalho de Melo perror("failed to create 'ready' pipe"); 163935b9d88eSArnaldo Carvalho de Melo return -1; 164035b9d88eSArnaldo Carvalho de Melo } 164135b9d88eSArnaldo Carvalho de Melo 164235b9d88eSArnaldo Carvalho de Melo if (pipe(go_pipe) < 0) { 164335b9d88eSArnaldo Carvalho de Melo perror("failed to create 'go' pipe"); 164435b9d88eSArnaldo Carvalho de Melo goto out_close_ready_pipe; 164535b9d88eSArnaldo Carvalho de Melo } 164635b9d88eSArnaldo Carvalho de Melo 164735b9d88eSArnaldo Carvalho de Melo evlist->workload.pid = fork(); 164835b9d88eSArnaldo Carvalho de Melo if (evlist->workload.pid < 0) { 164935b9d88eSArnaldo Carvalho de Melo perror("failed to fork"); 165035b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 165135b9d88eSArnaldo Carvalho de Melo } 165235b9d88eSArnaldo Carvalho de Melo 165335b9d88eSArnaldo Carvalho de Melo if (!evlist->workload.pid) { 16545f1c4225SArnaldo Carvalho de Melo int ret; 16555f1c4225SArnaldo Carvalho de Melo 1656119fa3c9SNamhyung Kim if (pipe_output) 165735b9d88eSArnaldo Carvalho de Melo dup2(2, 1); 165835b9d88eSArnaldo Carvalho de Melo 16590817df08SDavid Ahern signal(SIGTERM, SIG_DFL); 16600817df08SDavid Ahern 166135b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 166235b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 166335b9d88eSArnaldo Carvalho de Melo fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); 166435b9d88eSArnaldo Carvalho de Melo 166535b9d88eSArnaldo Carvalho de Melo /* 166635b9d88eSArnaldo Carvalho de Melo * Tell the parent we're ready to go 166735b9d88eSArnaldo Carvalho de Melo */ 166835b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 166935b9d88eSArnaldo Carvalho de Melo 167035b9d88eSArnaldo Carvalho de Melo /* 167135b9d88eSArnaldo Carvalho de Melo * Wait until the parent tells us to go. 167235b9d88eSArnaldo Carvalho de Melo */ 16735f1c4225SArnaldo Carvalho de Melo ret = read(go_pipe[0], &bf, 1); 16745f1c4225SArnaldo Carvalho de Melo /* 16755f1c4225SArnaldo Carvalho de Melo * The parent will ask for the execvp() to be performed by 16765f1c4225SArnaldo Carvalho de Melo * writing exactly one byte, in workload.cork_fd, usually via 16775f1c4225SArnaldo Carvalho de Melo * perf_evlist__start_workload(). 16785f1c4225SArnaldo Carvalho de Melo * 167920f86fc1SArnaldo Carvalho de Melo * For cancelling the workload without actually running it, 16805f1c4225SArnaldo Carvalho de Melo * the parent will just close workload.cork_fd, without writing 16815f1c4225SArnaldo Carvalho de Melo * anything, i.e. read will return zero and we just exit() 16825f1c4225SArnaldo Carvalho de Melo * here. 16835f1c4225SArnaldo Carvalho de Melo */ 16845f1c4225SArnaldo Carvalho de Melo if (ret != 1) { 16855f1c4225SArnaldo Carvalho de Melo if (ret == -1) 168635b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 16875f1c4225SArnaldo Carvalho de Melo exit(ret); 16885f1c4225SArnaldo Carvalho de Melo } 168935b9d88eSArnaldo Carvalho de Melo 169035b9d88eSArnaldo Carvalho de Melo execvp(argv[0], (char **)argv); 169135b9d88eSArnaldo Carvalho de Melo 1692735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1693f33cbe72SArnaldo Carvalho de Melo union sigval val; 1694f33cbe72SArnaldo Carvalho de Melo 1695f33cbe72SArnaldo Carvalho de Melo val.sival_int = errno; 1696f33cbe72SArnaldo Carvalho de Melo if (sigqueue(getppid(), SIGUSR1, val)) 169735b9d88eSArnaldo Carvalho de Melo perror(argv[0]); 1698f33cbe72SArnaldo Carvalho de Melo } else 1699f33cbe72SArnaldo Carvalho de Melo perror(argv[0]); 170035b9d88eSArnaldo Carvalho de Melo exit(-1); 170135b9d88eSArnaldo Carvalho de Melo } 170235b9d88eSArnaldo Carvalho de Melo 1703735f7e0bSArnaldo Carvalho de Melo if (exec_error) { 1704735f7e0bSArnaldo Carvalho de Melo struct sigaction act = { 1705735f7e0bSArnaldo Carvalho de Melo .sa_flags = SA_SIGINFO, 1706735f7e0bSArnaldo Carvalho de Melo .sa_sigaction = exec_error, 1707735f7e0bSArnaldo Carvalho de Melo }; 1708735f7e0bSArnaldo Carvalho de Melo sigaction(SIGUSR1, &act, NULL); 1709735f7e0bSArnaldo Carvalho de Melo } 1710735f7e0bSArnaldo Carvalho de Melo 17111aaf63b1SArnaldo Carvalho de Melo if (target__none(target)) { 17121aaf63b1SArnaldo Carvalho de Melo if (evlist->threads == NULL) { 17131aaf63b1SArnaldo Carvalho de Melo fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", 17141aaf63b1SArnaldo Carvalho de Melo __func__, __LINE__); 17151aaf63b1SArnaldo Carvalho de Melo goto out_close_pipes; 17161aaf63b1SArnaldo Carvalho de Melo } 1717e13798c7SJiri Olsa thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); 17181aaf63b1SArnaldo Carvalho de Melo } 171935b9d88eSArnaldo Carvalho de Melo 172035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 172135b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 172235b9d88eSArnaldo Carvalho de Melo /* 172335b9d88eSArnaldo Carvalho de Melo * wait for child to settle 172435b9d88eSArnaldo Carvalho de Melo */ 172535b9d88eSArnaldo Carvalho de Melo if (read(child_ready_pipe[0], &bf, 1) == -1) { 172635b9d88eSArnaldo Carvalho de Melo perror("unable to read pipe"); 172735b9d88eSArnaldo Carvalho de Melo goto out_close_pipes; 172835b9d88eSArnaldo Carvalho de Melo } 172935b9d88eSArnaldo Carvalho de Melo 1730bcf3145fSNamhyung Kim fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); 173135b9d88eSArnaldo Carvalho de Melo evlist->workload.cork_fd = go_pipe[1]; 173235b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 173335b9d88eSArnaldo Carvalho de Melo return 0; 173435b9d88eSArnaldo Carvalho de Melo 173535b9d88eSArnaldo Carvalho de Melo out_close_pipes: 173635b9d88eSArnaldo Carvalho de Melo close(go_pipe[0]); 173735b9d88eSArnaldo Carvalho de Melo close(go_pipe[1]); 173835b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe: 173935b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[0]); 174035b9d88eSArnaldo Carvalho de Melo close(child_ready_pipe[1]); 174135b9d88eSArnaldo Carvalho de Melo return -1; 174235b9d88eSArnaldo Carvalho de Melo } 174335b9d88eSArnaldo Carvalho de Melo 174435b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist) 174535b9d88eSArnaldo Carvalho de Melo { 174635b9d88eSArnaldo Carvalho de Melo if (evlist->workload.cork_fd > 0) { 1747b3824404SDavid Ahern char bf = 0; 1748bcf3145fSNamhyung Kim int ret; 174935b9d88eSArnaldo Carvalho de Melo /* 175035b9d88eSArnaldo Carvalho de Melo * Remove the cork, let it rip! 175135b9d88eSArnaldo Carvalho de Melo */ 1752bcf3145fSNamhyung Kim ret = write(evlist->workload.cork_fd, &bf, 1); 1753bcf3145fSNamhyung Kim if (ret < 0) 1754bcf3145fSNamhyung Kim perror("enable to write to pipe"); 1755bcf3145fSNamhyung Kim 1756bcf3145fSNamhyung Kim close(evlist->workload.cork_fd); 1757bcf3145fSNamhyung Kim return ret; 175835b9d88eSArnaldo Carvalho de Melo } 175935b9d88eSArnaldo Carvalho de Melo 176035b9d88eSArnaldo Carvalho de Melo return 0; 176135b9d88eSArnaldo Carvalho de Melo } 1762cb0b29e0SArnaldo Carvalho de Melo 1763a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, 17640807d2d8SArnaldo Carvalho de Melo struct perf_sample *sample) 1765cb0b29e0SArnaldo Carvalho de Melo { 176675562573SAdrian Hunter struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); 176775562573SAdrian Hunter 176875562573SAdrian Hunter if (!evsel) 176975562573SAdrian Hunter return -EFAULT; 17700807d2d8SArnaldo Carvalho de Melo return perf_evsel__parse_sample(evsel, event, sample); 1771cb0b29e0SArnaldo Carvalho de Melo } 177278f067b3SArnaldo Carvalho de Melo 177378f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) 177478f067b3SArnaldo Carvalho de Melo { 177578f067b3SArnaldo Carvalho de Melo struct perf_evsel *evsel; 177678f067b3SArnaldo Carvalho de Melo size_t printed = 0; 177778f067b3SArnaldo Carvalho de Melo 17780050f7aaSArnaldo Carvalho de Melo evlist__for_each(evlist, evsel) { 177978f067b3SArnaldo Carvalho de Melo printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", 178078f067b3SArnaldo Carvalho de Melo perf_evsel__name(evsel)); 178178f067b3SArnaldo Carvalho de Melo } 178278f067b3SArnaldo Carvalho de Melo 1783b2222139SDavidlohr Bueso return printed + fprintf(fp, "\n"); 178478f067b3SArnaldo Carvalho de Melo } 17856ef068cbSArnaldo Carvalho de Melo 1786d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist, 1787a8f23d8fSArnaldo Carvalho de Melo int err, char *buf, size_t size) 1788a8f23d8fSArnaldo Carvalho de Melo { 1789a8f23d8fSArnaldo Carvalho de Melo int printed, value; 17906e81c74cSMasami Hiramatsu char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1791a8f23d8fSArnaldo Carvalho de Melo 1792a8f23d8fSArnaldo Carvalho de Melo switch (err) { 1793a8f23d8fSArnaldo Carvalho de Melo case EACCES: 1794a8f23d8fSArnaldo Carvalho de Melo case EPERM: 1795a8f23d8fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1796a8f23d8fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1797a8f23d8fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); 1798a8f23d8fSArnaldo Carvalho de Melo 17991a47245dSAdrian Hunter value = perf_event_paranoid(); 1800a8f23d8fSArnaldo Carvalho de Melo 1801a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); 1802a8f23d8fSArnaldo Carvalho de Melo 1803a8f23d8fSArnaldo Carvalho de Melo if (value >= 2) { 1804a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1805a8f23d8fSArnaldo Carvalho de Melo "For your workloads it needs to be <= 1\nHint:\t"); 1806a8f23d8fSArnaldo Carvalho de Melo } 1807a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 18085229e366SArnaldo Carvalho de Melo "For system wide tracing it needs to be set to -1.\n"); 1809a8f23d8fSArnaldo Carvalho de Melo 1810a8f23d8fSArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 18115229e366SArnaldo Carvalho de Melo "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" 18125229e366SArnaldo Carvalho de Melo "Hint:\tThe current value is %d.", value); 1813a8f23d8fSArnaldo Carvalho de Melo break; 1814d9aade7fSArnaldo Carvalho de Melo case EINVAL: { 1815d9aade7fSArnaldo Carvalho de Melo struct perf_evsel *first = perf_evlist__first(evlist); 1816d9aade7fSArnaldo Carvalho de Melo int max_freq; 1817d9aade7fSArnaldo Carvalho de Melo 1818d9aade7fSArnaldo Carvalho de Melo if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) 1819d9aade7fSArnaldo Carvalho de Melo goto out_default; 1820d9aade7fSArnaldo Carvalho de Melo 1821d9aade7fSArnaldo Carvalho de Melo if (first->attr.sample_freq < (u64)max_freq) 1822d9aade7fSArnaldo Carvalho de Melo goto out_default; 1823d9aade7fSArnaldo Carvalho de Melo 1824d9aade7fSArnaldo Carvalho de Melo printed = scnprintf(buf, size, 1825d9aade7fSArnaldo Carvalho de Melo "Error:\t%s.\n" 1826d9aade7fSArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" 1827d9aade7fSArnaldo Carvalho de Melo "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", 1828d9aade7fSArnaldo Carvalho de Melo emsg, max_freq, first->attr.sample_freq); 1829d9aade7fSArnaldo Carvalho de Melo break; 1830d9aade7fSArnaldo Carvalho de Melo } 1831a8f23d8fSArnaldo Carvalho de Melo default: 1832d9aade7fSArnaldo Carvalho de Melo out_default: 1833a8f23d8fSArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1834a8f23d8fSArnaldo Carvalho de Melo break; 1835a8f23d8fSArnaldo Carvalho de Melo } 1836a8f23d8fSArnaldo Carvalho de Melo 1837a8f23d8fSArnaldo Carvalho de Melo return 0; 1838a8f23d8fSArnaldo Carvalho de Melo } 1839a025e4f0SAdrian Hunter 1840956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) 1841956fa571SArnaldo Carvalho de Melo { 1842956fa571SArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); 1843e965bea1SArnaldo Carvalho de Melo int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; 1844956fa571SArnaldo Carvalho de Melo 1845956fa571SArnaldo Carvalho de Melo switch (err) { 1846956fa571SArnaldo Carvalho de Melo case EPERM: 1847e5d4a290SArnaldo Carvalho de Melo sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); 1848e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1849e965bea1SArnaldo Carvalho de Melo "Error:\t%s.\n" 1850956fa571SArnaldo Carvalho de Melo "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" 1851e965bea1SArnaldo Carvalho de Melo "Hint:\tTried using %zd kB.\n", 1852e5d4a290SArnaldo Carvalho de Melo emsg, pages_max_per_user, pages_attempted); 1853e965bea1SArnaldo Carvalho de Melo 1854e965bea1SArnaldo Carvalho de Melo if (pages_attempted >= pages_max_per_user) { 1855e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1856e965bea1SArnaldo Carvalho de Melo "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", 1857e965bea1SArnaldo Carvalho de Melo pages_max_per_user + pages_attempted); 1858e965bea1SArnaldo Carvalho de Melo } 1859e965bea1SArnaldo Carvalho de Melo 1860e965bea1SArnaldo Carvalho de Melo printed += scnprintf(buf + printed, size - printed, 1861e965bea1SArnaldo Carvalho de Melo "Hint:\tTry using a smaller -m/--mmap-pages value."); 1862956fa571SArnaldo Carvalho de Melo break; 1863956fa571SArnaldo Carvalho de Melo default: 1864956fa571SArnaldo Carvalho de Melo scnprintf(buf, size, "%s", emsg); 1865956fa571SArnaldo Carvalho de Melo break; 1866956fa571SArnaldo Carvalho de Melo } 1867956fa571SArnaldo Carvalho de Melo 1868956fa571SArnaldo Carvalho de Melo return 0; 1869956fa571SArnaldo Carvalho de Melo } 1870956fa571SArnaldo Carvalho de Melo 1871a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist, 1872a025e4f0SAdrian Hunter struct perf_evsel *move_evsel) 1873a025e4f0SAdrian Hunter { 1874a025e4f0SAdrian Hunter struct perf_evsel *evsel, *n; 1875a025e4f0SAdrian Hunter LIST_HEAD(move); 1876a025e4f0SAdrian Hunter 1877a025e4f0SAdrian Hunter if (move_evsel == perf_evlist__first(evlist)) 1878a025e4f0SAdrian Hunter return; 1879a025e4f0SAdrian Hunter 18800050f7aaSArnaldo Carvalho de Melo evlist__for_each_safe(evlist, n, evsel) { 1881a025e4f0SAdrian Hunter if (evsel->leader == move_evsel->leader) 1882a025e4f0SAdrian Hunter list_move_tail(&evsel->node, &move); 1883a025e4f0SAdrian Hunter } 1884a025e4f0SAdrian Hunter 1885a025e4f0SAdrian Hunter list_splice(&move, &evlist->entries); 1886a025e4f0SAdrian Hunter } 188760b0896cSAdrian Hunter 188860b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist, 188960b0896cSAdrian Hunter struct perf_evsel *tracking_evsel) 189060b0896cSAdrian Hunter { 189160b0896cSAdrian Hunter struct perf_evsel *evsel; 189260b0896cSAdrian Hunter 189360b0896cSAdrian Hunter if (tracking_evsel->tracking) 189460b0896cSAdrian Hunter return; 189560b0896cSAdrian Hunter 189660b0896cSAdrian Hunter evlist__for_each(evlist, evsel) { 189760b0896cSAdrian Hunter if (evsel != tracking_evsel) 189860b0896cSAdrian Hunter evsel->tracking = false; 189960b0896cSAdrian Hunter } 190060b0896cSAdrian Hunter 190160b0896cSAdrian Hunter tracking_evsel->tracking = true; 190260b0896cSAdrian Hunter } 19037630b3e2SWang Nan 19047630b3e2SWang Nan struct perf_evsel * 19057630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, 19067630b3e2SWang Nan const char *str) 19077630b3e2SWang Nan { 19087630b3e2SWang Nan struct perf_evsel *evsel; 19097630b3e2SWang Nan 19107630b3e2SWang Nan evlist__for_each(evlist, evsel) { 19117630b3e2SWang Nan if (!evsel->name) 19127630b3e2SWang Nan continue; 19137630b3e2SWang Nan if (strcmp(str, evsel->name) == 0) 19147630b3e2SWang Nan return evsel; 19157630b3e2SWang Nan } 19167630b3e2SWang Nan 19177630b3e2SWang Nan return NULL; 19187630b3e2SWang Nan } 1919