Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
| 3 | * |
| 4 | * Parts came from builtin-{top,stat,record}.c, see those files for further |
| 5 | * copyright notes. |
| 6 | * |
| 7 | * Released under the GPL v2. (and only v2, not any later version) |
| 8 | */ |
Arnaldo Carvalho de Melo | a8c9ae1 | 2011-11-05 08:41:51 -0200 | [diff] [blame] | 9 | #include "util.h" |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 10 | #include <api/fs/fs.h> |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 11 | #include <poll.h> |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 12 | #include "cpumap.h" |
| 13 | #include "thread_map.h" |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 14 | #include "target.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 15 | #include "evlist.h" |
| 16 | #include "evsel.h" |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 17 | #include "debug.h" |
Wang Nan | 54cc54d | 2016-07-14 08:34:42 +0000 | [diff] [blame] | 18 | #include "asm/bug.h" |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 19 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 20 | |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 21 | #include "parse-events.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 22 | #include <subcmd/parse-options.h> |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 24 | #include <sys/mman.h> |
| 25 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 26 | #include <linux/bitops.h> |
| 27 | #include <linux/hash.h> |
Arnaldo Carvalho de Melo | 0389cd1 | 2014-12-15 16:04:11 -0300 | [diff] [blame] | 28 | #include <linux/log2.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 29 | #include <linux/err.h> |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 30 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 31 | static void perf_mmap__munmap(struct perf_mmap *map); |
Wang Nan | 4876075 | 2016-07-14 08:34:37 +0000 | [diff] [blame] | 32 | static void perf_mmap__put(struct perf_mmap *map); |
Arnaldo Carvalho de Melo | e4b356b | 2014-09-08 11:27:49 -0300 | [diff] [blame] | 33 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 34 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 35 | #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 36 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 37 | void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, |
| 38 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 39 | { |
| 40 | int i; |
| 41 | |
| 42 | for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) |
| 43 | INIT_HLIST_HEAD(&evlist->heads[i]); |
| 44 | INIT_LIST_HEAD(&evlist->entries); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 45 | perf_evlist__set_maps(evlist, cpus, threads); |
Arnaldo Carvalho de Melo | 1b85337 | 2014-09-03 18:02:59 -0300 | [diff] [blame] | 46 | fdarray__init(&evlist->pollfd, 64); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 47 | evlist->workload.pid = -1; |
Wang Nan | 54cc54d | 2016-07-14 08:34:42 +0000 | [diff] [blame] | 48 | evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 49 | } |
| 50 | |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 51 | struct perf_evlist *perf_evlist__new(void) |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 52 | { |
| 53 | struct perf_evlist *evlist = zalloc(sizeof(*evlist)); |
| 54 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 55 | if (evlist != NULL) |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 56 | perf_evlist__init(evlist, NULL, NULL); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 57 | |
| 58 | return evlist; |
| 59 | } |
| 60 | |
Jiri Olsa | b22d54b | 2013-09-01 12:36:14 +0200 | [diff] [blame] | 61 | struct perf_evlist *perf_evlist__new_default(void) |
| 62 | { |
| 63 | struct perf_evlist *evlist = perf_evlist__new(); |
| 64 | |
| 65 | if (evlist && perf_evlist__add_default(evlist)) { |
| 66 | perf_evlist__delete(evlist); |
| 67 | evlist = NULL; |
| 68 | } |
| 69 | |
| 70 | return evlist; |
| 71 | } |
| 72 | |
Arnaldo Carvalho de Melo | 5bae025 | 2016-01-07 13:14:56 -0300 | [diff] [blame] | 73 | struct perf_evlist *perf_evlist__new_dummy(void) |
| 74 | { |
| 75 | struct perf_evlist *evlist = perf_evlist__new(); |
| 76 | |
| 77 | if (evlist && perf_evlist__add_dummy(evlist)) { |
| 78 | perf_evlist__delete(evlist); |
| 79 | evlist = NULL; |
| 80 | } |
| 81 | |
| 82 | return evlist; |
| 83 | } |
| 84 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 85 | /** |
| 86 | * perf_evlist__set_id_pos - set the positions of event ids. |
| 87 | * @evlist: selected event list |
| 88 | * |
| 89 | * Events with compatible sample types all have the same id_pos |
| 90 | * and is_pos. For convenience, put a copy on evlist. |
| 91 | */ |
| 92 | void perf_evlist__set_id_pos(struct perf_evlist *evlist) |
| 93 | { |
| 94 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 95 | |
| 96 | evlist->id_pos = first->id_pos; |
| 97 | evlist->is_pos = first->is_pos; |
| 98 | } |
| 99 | |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 100 | static void perf_evlist__update_id_pos(struct perf_evlist *evlist) |
| 101 | { |
| 102 | struct perf_evsel *evsel; |
| 103 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 104 | evlist__for_each_entry(evlist, evsel) |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 105 | perf_evsel__calc_id_pos(evsel); |
| 106 | |
| 107 | perf_evlist__set_id_pos(evlist); |
| 108 | } |
| 109 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 110 | static void perf_evlist__purge(struct perf_evlist *evlist) |
| 111 | { |
| 112 | struct perf_evsel *pos, *n; |
| 113 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 114 | evlist__for_each_entry_safe(evlist, n, pos) { |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 115 | list_del_init(&pos->node); |
Arnaldo Carvalho de Melo | d49e469 | 2015-08-27 08:07:40 -0400 | [diff] [blame] | 116 | pos->evlist = NULL; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 117 | perf_evsel__delete(pos); |
| 118 | } |
| 119 | |
| 120 | evlist->nr_entries = 0; |
| 121 | } |
| 122 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 123 | void perf_evlist__exit(struct perf_evlist *evlist) |
| 124 | { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 125 | zfree(&evlist->mmap); |
Wang Nan | b2cb615 | 2016-07-14 08:34:39 +0000 | [diff] [blame] | 126 | zfree(&evlist->backward_mmap); |
Arnaldo Carvalho de Melo | 1b85337 | 2014-09-03 18:02:59 -0300 | [diff] [blame] | 127 | fdarray__exit(&evlist->pollfd); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 128 | } |
| 129 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 130 | void perf_evlist__delete(struct perf_evlist *evlist) |
| 131 | { |
Arnaldo Carvalho de Melo | 0b04b3d | 2016-06-21 18:15:45 -0300 | [diff] [blame] | 132 | if (evlist == NULL) |
| 133 | return; |
| 134 | |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 135 | perf_evlist__munmap(evlist); |
Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 136 | perf_evlist__close(evlist); |
Jiri Olsa | f30a79b | 2015-06-23 00:36:04 +0200 | [diff] [blame] | 137 | cpu_map__put(evlist->cpus); |
Jiri Olsa | 186fbb7 | 2015-06-23 00:36:05 +0200 | [diff] [blame] | 138 | thread_map__put(evlist->threads); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 139 | evlist->cpus = NULL; |
| 140 | evlist->threads = NULL; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 141 | perf_evlist__purge(evlist); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 142 | perf_evlist__exit(evlist); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 143 | free(evlist); |
| 144 | } |
| 145 | |
Adrian Hunter | adc0c3e | 2015-09-08 10:58:58 +0300 | [diff] [blame] | 146 | static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, |
| 147 | struct perf_evsel *evsel) |
| 148 | { |
| 149 | /* |
| 150 | * We already have cpus for evsel (via PMU sysfs) so |
| 151 | * keep it, if there's no target cpu list defined. |
| 152 | */ |
| 153 | if (!evsel->own_cpus || evlist->has_user_cpus) { |
| 154 | cpu_map__put(evsel->cpus); |
| 155 | evsel->cpus = cpu_map__get(evlist->cpus); |
| 156 | } else if (evsel->cpus != evsel->own_cpus) { |
| 157 | cpu_map__put(evsel->cpus); |
| 158 | evsel->cpus = cpu_map__get(evsel->own_cpus); |
| 159 | } |
| 160 | |
| 161 | thread_map__put(evsel->threads); |
| 162 | evsel->threads = thread_map__get(evlist->threads); |
| 163 | } |
| 164 | |
| 165 | static void perf_evlist__propagate_maps(struct perf_evlist *evlist) |
| 166 | { |
| 167 | struct perf_evsel *evsel; |
| 168 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 169 | evlist__for_each_entry(evlist, evsel) |
Adrian Hunter | adc0c3e | 2015-09-08 10:58:58 +0300 | [diff] [blame] | 170 | __perf_evlist__propagate_maps(evlist, evsel); |
| 171 | } |
| 172 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 173 | void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) |
| 174 | { |
Arnaldo Carvalho de Melo | d49e469 | 2015-08-27 08:07:40 -0400 | [diff] [blame] | 175 | entry->evlist = evlist; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 176 | list_add_tail(&entry->node, &evlist->entries); |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 177 | entry->idx = evlist->nr_entries; |
Adrian Hunter | 60b0896 | 2014-07-31 09:00:52 +0300 | [diff] [blame] | 178 | entry->tracking = !entry->idx; |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 179 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 180 | if (!evlist->nr_entries++) |
| 181 | perf_evlist__set_id_pos(evlist); |
Adrian Hunter | 44c42d7 | 2015-09-08 10:58:59 +0300 | [diff] [blame] | 182 | |
| 183 | __perf_evlist__propagate_maps(evlist, entry); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 184 | } |
| 185 | |
Adrian Hunter | 4768230 | 2015-09-25 16:15:53 +0300 | [diff] [blame] | 186 | void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel) |
| 187 | { |
| 188 | evsel->evlist = NULL; |
| 189 | list_del_init(&evsel->node); |
| 190 | evlist->nr_entries -= 1; |
| 191 | } |
| 192 | |
Jiri Olsa | 0529bc1 | 2012-01-27 15:34:20 +0100 | [diff] [blame] | 193 | void perf_evlist__splice_list_tail(struct perf_evlist *evlist, |
Adrian Hunter | f114d6e | 2015-09-08 10:58:53 +0300 | [diff] [blame] | 194 | struct list_head *list) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 195 | { |
Adrian Hunter | f114d6e | 2015-09-08 10:58:53 +0300 | [diff] [blame] | 196 | struct perf_evsel *evsel, *temp; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 197 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 198 | __evlist__for_each_entry_safe(list, temp, evsel) { |
Adrian Hunter | f114d6e | 2015-09-08 10:58:53 +0300 | [diff] [blame] | 199 | list_del_init(&evsel->node); |
| 200 | perf_evlist__add(evlist, evsel); |
| 201 | } |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 202 | } |
| 203 | |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 204 | void __perf_evlist__set_leader(struct list_head *list) |
| 205 | { |
| 206 | struct perf_evsel *evsel, *leader; |
| 207 | |
| 208 | leader = list_entry(list->next, struct perf_evsel, node); |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 209 | evsel = list_entry(list->prev, struct perf_evsel, node); |
| 210 | |
| 211 | leader->nr_members = evsel->idx - leader->idx + 1; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 212 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 213 | __evlist__for_each_entry(list, evsel) { |
Stephane Eranian | 74b2133 | 2013-01-31 13:54:37 +0100 | [diff] [blame] | 214 | evsel->leader = leader; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | void perf_evlist__set_leader(struct perf_evlist *evlist) |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 219 | { |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 220 | if (evlist->nr_entries) { |
| 221 | evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 222 | __perf_evlist__set_leader(&evlist->entries); |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 223 | } |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Jiri Olsa | 45cf6c3 | 2015-10-05 20:06:04 +0200 | [diff] [blame] | 226 | void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr) |
Arnaldo Carvalho de Melo | 7f8d1ad | 2015-09-30 17:49:49 -0300 | [diff] [blame] | 227 | { |
| 228 | attr->precise_ip = 3; |
| 229 | |
| 230 | while (attr->precise_ip != 0) { |
| 231 | int fd = sys_perf_event_open(attr, 0, -1, -1, 0); |
| 232 | if (fd != -1) { |
| 233 | close(fd); |
| 234 | break; |
| 235 | } |
| 236 | --attr->precise_ip; |
| 237 | } |
| 238 | } |
| 239 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 240 | int perf_evlist__add_default(struct perf_evlist *evlist) |
| 241 | { |
Arnaldo Carvalho de Melo | 7c48dcf | 2016-07-28 18:33:20 -0300 | [diff] [blame] | 242 | struct perf_evsel *evsel = perf_evsel__new_cycles(); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 243 | |
| 244 | if (evsel == NULL) |
Arnaldo Carvalho de Melo | 7c48dcf | 2016-07-28 18:33:20 -0300 | [diff] [blame] | 245 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 246 | |
| 247 | perf_evlist__add(evlist, evsel); |
| 248 | return 0; |
| 249 | } |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 250 | |
Arnaldo Carvalho de Melo | 5bae025 | 2016-01-07 13:14:56 -0300 | [diff] [blame] | 251 | int perf_evlist__add_dummy(struct perf_evlist *evlist) |
| 252 | { |
| 253 | struct perf_event_attr attr = { |
| 254 | .type = PERF_TYPE_SOFTWARE, |
| 255 | .config = PERF_COUNT_SW_DUMMY, |
| 256 | .size = sizeof(attr), /* to capture ABI version */ |
| 257 | }; |
| 258 | struct perf_evsel *evsel = perf_evsel__new(&attr); |
| 259 | |
| 260 | if (evsel == NULL) |
| 261 | return -ENOMEM; |
| 262 | |
| 263 | perf_evlist__add(evlist, evsel); |
| 264 | return 0; |
| 265 | } |
| 266 | |
Arnaldo Carvalho de Melo | e60fc84 | 2012-10-03 11:50:55 -0300 | [diff] [blame] | 267 | static int perf_evlist__add_attrs(struct perf_evlist *evlist, |
| 268 | struct perf_event_attr *attrs, size_t nr_attrs) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 269 | { |
| 270 | struct perf_evsel *evsel, *n; |
| 271 | LIST_HEAD(head); |
| 272 | size_t i; |
| 273 | |
| 274 | for (i = 0; i < nr_attrs; i++) { |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 275 | evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 276 | if (evsel == NULL) |
| 277 | goto out_delete_partial_list; |
| 278 | list_add_tail(&evsel->node, &head); |
| 279 | } |
| 280 | |
Adrian Hunter | f114d6e | 2015-09-08 10:58:53 +0300 | [diff] [blame] | 281 | perf_evlist__splice_list_tail(evlist, &head); |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | |
| 285 | out_delete_partial_list: |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 286 | __evlist__for_each_entry_safe(&head, n, evsel) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 287 | perf_evsel__delete(evsel); |
| 288 | return -1; |
| 289 | } |
| 290 | |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 291 | int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, |
| 292 | struct perf_event_attr *attrs, size_t nr_attrs) |
| 293 | { |
| 294 | size_t i; |
| 295 | |
| 296 | for (i = 0; i < nr_attrs; i++) |
| 297 | event_attr_init(attrs + i); |
| 298 | |
| 299 | return perf_evlist__add_attrs(evlist, attrs, nr_attrs); |
| 300 | } |
| 301 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 302 | struct perf_evsel * |
| 303 | perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 304 | { |
| 305 | struct perf_evsel *evsel; |
| 306 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 307 | evlist__for_each_entry(evlist, evsel) { |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 308 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && |
| 309 | (int)evsel->attr.config == id) |
| 310 | return evsel; |
| 311 | } |
| 312 | |
| 313 | return NULL; |
| 314 | } |
| 315 | |
David Ahern | a2f2804 | 2013-08-28 22:29:51 -0600 | [diff] [blame] | 316 | struct perf_evsel * |
| 317 | perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, |
| 318 | const char *name) |
| 319 | { |
| 320 | struct perf_evsel *evsel; |
| 321 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 322 | evlist__for_each_entry(evlist, evsel) { |
David Ahern | a2f2804 | 2013-08-28 22:29:51 -0600 | [diff] [blame] | 323 | if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && |
| 324 | (strcmp(evsel->name, name) == 0)) |
| 325 | return evsel; |
| 326 | } |
| 327 | |
| 328 | return NULL; |
| 329 | } |
| 330 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 331 | int perf_evlist__add_newtp(struct perf_evlist *evlist, |
| 332 | const char *sys, const char *name, void *handler) |
| 333 | { |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 334 | struct perf_evsel *evsel = perf_evsel__newtp(sys, name); |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 335 | |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 336 | if (IS_ERR(evsel)) |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 337 | return -1; |
| 338 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 339 | evsel->handler = handler; |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 340 | perf_evlist__add(evlist, evsel); |
| 341 | return 0; |
| 342 | } |
| 343 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 344 | static int perf_evlist__nr_threads(struct perf_evlist *evlist, |
| 345 | struct perf_evsel *evsel) |
| 346 | { |
| 347 | if (evsel->system_wide) |
| 348 | return 1; |
| 349 | else |
| 350 | return thread_map__nr(evlist->threads); |
| 351 | } |
| 352 | |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 353 | void perf_evlist__disable(struct perf_evlist *evlist) |
| 354 | { |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 355 | struct perf_evsel *pos; |
| 356 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 357 | evlist__for_each_entry(evlist, pos) { |
Jiri Olsa | 3e27c92 | 2015-12-03 10:06:42 +0100 | [diff] [blame] | 358 | if (!perf_evsel__is_group_leader(pos) || !pos->fd) |
| 359 | continue; |
| 360 | perf_evsel__disable(pos); |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 361 | } |
Arnaldo Carvalho de Melo | 2b56bcf | 2015-06-17 16:40:26 -0300 | [diff] [blame] | 362 | |
| 363 | evlist->enabled = false; |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 364 | } |
| 365 | |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 366 | void perf_evlist__enable(struct perf_evlist *evlist) |
| 367 | { |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 368 | struct perf_evsel *pos; |
| 369 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 370 | evlist__for_each_entry(evlist, pos) { |
Jiri Olsa | 3e27c92 | 2015-12-03 10:06:42 +0100 | [diff] [blame] | 371 | if (!perf_evsel__is_group_leader(pos) || !pos->fd) |
| 372 | continue; |
| 373 | perf_evsel__enable(pos); |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 374 | } |
Arnaldo Carvalho de Melo | 2b56bcf | 2015-06-17 16:40:26 -0300 | [diff] [blame] | 375 | |
| 376 | evlist->enabled = true; |
| 377 | } |
| 378 | |
| 379 | void perf_evlist__toggle_enable(struct perf_evlist *evlist) |
| 380 | { |
| 381 | (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 382 | } |
| 383 | |
Adrian Hunter | 1c65056 | 2014-07-31 09:00:56 +0300 | [diff] [blame] | 384 | static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, |
| 385 | struct perf_evsel *evsel, int cpu) |
| 386 | { |
| 387 | int thread, err; |
| 388 | int nr_threads = perf_evlist__nr_threads(evlist, evsel); |
| 389 | |
| 390 | if (!evsel->fd) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | for (thread = 0; thread < nr_threads; thread++) { |
| 394 | err = ioctl(FD(evsel, cpu, thread), |
| 395 | PERF_EVENT_IOC_ENABLE, 0); |
| 396 | if (err) |
| 397 | return err; |
| 398 | } |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, |
| 403 | struct perf_evsel *evsel, |
| 404 | int thread) |
| 405 | { |
| 406 | int cpu, err; |
| 407 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 408 | |
| 409 | if (!evsel->fd) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
| 413 | err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); |
| 414 | if (err) |
| 415 | return err; |
| 416 | } |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | int perf_evlist__enable_event_idx(struct perf_evlist *evlist, |
| 421 | struct perf_evsel *evsel, int idx) |
| 422 | { |
| 423 | bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); |
| 424 | |
| 425 | if (per_cpu_mmaps) |
| 426 | return perf_evlist__enable_event_cpu(evlist, evsel, idx); |
| 427 | else |
| 428 | return perf_evlist__enable_event_thread(evlist, evsel, idx); |
| 429 | } |
| 430 | |
Arnaldo Carvalho de Melo | ad6765d | 2014-08-18 16:44:06 -0300 | [diff] [blame] | 431 | int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 432 | { |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 433 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 434 | int nr_threads = thread_map__nr(evlist->threads); |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 435 | int nfds = 0; |
| 436 | struct perf_evsel *evsel; |
| 437 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 438 | evlist__for_each_entry(evlist, evsel) { |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 439 | if (evsel->system_wide) |
| 440 | nfds += nr_cpus; |
| 441 | else |
| 442 | nfds += nr_cpus * nr_threads; |
| 443 | } |
| 444 | |
Arnaldo Carvalho de Melo | 1b85337 | 2014-09-03 18:02:59 -0300 | [diff] [blame] | 445 | if (fdarray__available_entries(&evlist->pollfd) < nfds && |
| 446 | fdarray__grow(&evlist->pollfd, nfds) < 0) |
Arnaldo Carvalho de Melo | ad6765d | 2014-08-18 16:44:06 -0300 | [diff] [blame] | 447 | return -ENOMEM; |
| 448 | |
| 449 | return 0; |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 450 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 451 | |
Wang Nan | 4876075 | 2016-07-14 08:34:37 +0000 | [diff] [blame] | 452 | static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, |
| 453 | struct perf_mmap *map, short revent) |
Arnaldo Carvalho de Melo | e4b356b | 2014-09-08 11:27:49 -0300 | [diff] [blame] | 454 | { |
Wang Nan | f3058a1 | 2016-05-24 02:28:59 +0000 | [diff] [blame] | 455 | int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); |
Arnaldo Carvalho de Melo | e4b356b | 2014-09-08 11:27:49 -0300 | [diff] [blame] | 456 | /* |
| 457 | * Save the idx so that when we filter out fds POLLHUP'ed we can |
| 458 | * close the associated evlist->mmap[] entry. |
| 459 | */ |
| 460 | if (pos >= 0) { |
Wang Nan | 4876075 | 2016-07-14 08:34:37 +0000 | [diff] [blame] | 461 | evlist->pollfd.priv[pos].ptr = map; |
Arnaldo Carvalho de Melo | e4b356b | 2014-09-08 11:27:49 -0300 | [diff] [blame] | 462 | |
| 463 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 464 | } |
| 465 | |
| 466 | return pos; |
| 467 | } |
| 468 | |
Arnaldo Carvalho de Melo | ad6765d | 2014-08-18 16:44:06 -0300 | [diff] [blame] | 469 | int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 470 | { |
Wang Nan | 4876075 | 2016-07-14 08:34:37 +0000 | [diff] [blame] | 471 | return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN); |
Arnaldo Carvalho de Melo | e4b356b | 2014-09-08 11:27:49 -0300 | [diff] [blame] | 472 | } |
Arnaldo Carvalho de Melo | 1b85337 | 2014-09-03 18:02:59 -0300 | [diff] [blame] | 473 | |
Wang Nan | 258e4bf | 2016-05-25 13:44:57 +0000 | [diff] [blame] | 474 | static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, |
| 475 | void *arg __maybe_unused) |
Arnaldo Carvalho de Melo | e4b356b | 2014-09-08 11:27:49 -0300 | [diff] [blame] | 476 | { |
Wang Nan | 4876075 | 2016-07-14 08:34:37 +0000 | [diff] [blame] | 477 | struct perf_mmap *map = fda->priv[fd].ptr; |
Arnaldo Carvalho de Melo | e4b356b | 2014-09-08 11:27:49 -0300 | [diff] [blame] | 478 | |
Wang Nan | 4876075 | 2016-07-14 08:34:37 +0000 | [diff] [blame] | 479 | if (map) |
| 480 | perf_mmap__put(map); |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 481 | } |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 482 | |
Arnaldo Carvalho de Melo | 1ddec7f | 2014-08-12 23:04:11 -0300 | [diff] [blame] | 483 | int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) |
| 484 | { |
Arnaldo Carvalho de Melo | e4b356b | 2014-09-08 11:27:49 -0300 | [diff] [blame] | 485 | return fdarray__filter(&evlist->pollfd, revents_and_mask, |
Wang Nan | 258e4bf | 2016-05-25 13:44:57 +0000 | [diff] [blame] | 486 | perf_evlist__munmap_filtered, NULL); |
Arnaldo Carvalho de Melo | 1ddec7f | 2014-08-12 23:04:11 -0300 | [diff] [blame] | 487 | } |
| 488 | |
Arnaldo Carvalho de Melo | f66a889 | 2014-08-18 17:25:59 -0300 | [diff] [blame] | 489 | int perf_evlist__poll(struct perf_evlist *evlist, int timeout) |
| 490 | { |
Arnaldo Carvalho de Melo | 1b85337 | 2014-09-03 18:02:59 -0300 | [diff] [blame] | 491 | return fdarray__poll(&evlist->pollfd, timeout); |
Arnaldo Carvalho de Melo | f66a889 | 2014-08-18 17:25:59 -0300 | [diff] [blame] | 492 | } |
| 493 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 494 | static void perf_evlist__id_hash(struct perf_evlist *evlist, |
| 495 | struct perf_evsel *evsel, |
| 496 | int cpu, int thread, u64 id) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 497 | { |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 498 | int hash; |
| 499 | struct perf_sample_id *sid = SID(evsel, cpu, thread); |
| 500 | |
| 501 | sid->id = id; |
| 502 | sid->evsel = evsel; |
| 503 | hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); |
| 504 | hlist_add_head(&sid->node, &evlist->heads[hash]); |
| 505 | } |
| 506 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 507 | void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, |
| 508 | int cpu, int thread, u64 id) |
| 509 | { |
| 510 | perf_evlist__id_hash(evlist, evsel, cpu, thread, id); |
| 511 | evsel->id[evsel->ids++] = id; |
| 512 | } |
| 513 | |
Jiri Olsa | 1c59612 | 2015-11-05 15:40:49 +0100 | [diff] [blame] | 514 | int perf_evlist__id_add_fd(struct perf_evlist *evlist, |
| 515 | struct perf_evsel *evsel, |
| 516 | int cpu, int thread, int fd) |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 517 | { |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 518 | u64 read_data[4] = { 0, }; |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 519 | int id_idx = 1; /* The first entry is the counter value */ |
Jiri Olsa | e2b5abe | 2012-04-04 19:32:27 +0200 | [diff] [blame] | 520 | u64 id; |
| 521 | int ret; |
| 522 | |
| 523 | ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); |
| 524 | if (!ret) |
| 525 | goto add; |
| 526 | |
| 527 | if (errno != ENOTTY) |
| 528 | return -1; |
| 529 | |
| 530 | /* Legacy way to get event id.. All hail to old kernels! */ |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 531 | |
Jiri Olsa | c4861af | 2012-10-12 13:02:21 +0200 | [diff] [blame] | 532 | /* |
| 533 | * This way does not work with group format read, so bail |
| 534 | * out in that case. |
| 535 | */ |
| 536 | if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) |
| 537 | return -1; |
| 538 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 539 | if (!(evsel->attr.read_format & PERF_FORMAT_ID) || |
| 540 | read(fd, &read_data, sizeof(read_data)) == -1) |
| 541 | return -1; |
| 542 | |
| 543 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 544 | ++id_idx; |
| 545 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 546 | ++id_idx; |
| 547 | |
Jiri Olsa | e2b5abe | 2012-04-04 19:32:27 +0200 | [diff] [blame] | 548 | id = read_data[id_idx]; |
| 549 | |
| 550 | add: |
| 551 | perf_evlist__id_add(evlist, evsel, cpu, thread, id); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 555 | static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, |
| 556 | struct perf_evsel *evsel, int idx, int cpu, |
| 557 | int thread) |
| 558 | { |
| 559 | struct perf_sample_id *sid = SID(evsel, cpu, thread); |
| 560 | sid->idx = idx; |
| 561 | if (evlist->cpus && cpu >= 0) |
| 562 | sid->cpu = evlist->cpus->map[cpu]; |
| 563 | else |
| 564 | sid->cpu = -1; |
| 565 | if (!evsel->system_wide && evlist->threads && thread >= 0) |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 566 | sid->tid = thread_map__pid(evlist->threads, thread); |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 567 | else |
| 568 | sid->tid = -1; |
| 569 | } |
| 570 | |
Jiri Olsa | 932a359 | 2012-10-11 14:10:35 +0200 | [diff] [blame] | 571 | struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 572 | { |
| 573 | struct hlist_head *head; |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 574 | struct perf_sample_id *sid; |
| 575 | int hash; |
| 576 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 577 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
| 578 | head = &evlist->heads[hash]; |
| 579 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 580 | hlist_for_each_entry(sid, head, node) |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 581 | if (sid->id == id) |
Jiri Olsa | 932a359 | 2012-10-11 14:10:35 +0200 | [diff] [blame] | 582 | return sid; |
| 583 | |
| 584 | return NULL; |
| 585 | } |
| 586 | |
| 587 | struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) |
| 588 | { |
| 589 | struct perf_sample_id *sid; |
| 590 | |
Adrian Hunter | 05169df | 2015-08-20 11:26:45 +0300 | [diff] [blame] | 591 | if (evlist->nr_entries == 1 || !id) |
Jiri Olsa | 932a359 | 2012-10-11 14:10:35 +0200 | [diff] [blame] | 592 | return perf_evlist__first(evlist); |
| 593 | |
| 594 | sid = perf_evlist__id2sid(evlist, id); |
| 595 | if (sid) |
| 596 | return sid->evsel; |
Namhyung Kim | 30e68bc | 2012-02-20 10:47:26 +0900 | [diff] [blame] | 597 | |
| 598 | if (!perf_evlist__sample_id_all(evlist)) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 599 | return perf_evlist__first(evlist); |
Namhyung Kim | 30e68bc | 2012-02-20 10:47:26 +0900 | [diff] [blame] | 600 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 601 | return NULL; |
| 602 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 603 | |
Adrian Hunter | dddcf6a | 2015-09-25 16:15:52 +0300 | [diff] [blame] | 604 | struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist, |
| 605 | u64 id) |
| 606 | { |
| 607 | struct perf_sample_id *sid; |
| 608 | |
| 609 | if (!id) |
| 610 | return NULL; |
| 611 | |
| 612 | sid = perf_evlist__id2sid(evlist, id); |
| 613 | if (sid) |
| 614 | return sid->evsel; |
| 615 | |
| 616 | return NULL; |
| 617 | } |
| 618 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 619 | static int perf_evlist__event2id(struct perf_evlist *evlist, |
| 620 | union perf_event *event, u64 *id) |
| 621 | { |
| 622 | const u64 *array = event->sample.array; |
| 623 | ssize_t n; |
| 624 | |
| 625 | n = (event->header.size - sizeof(event->header)) >> 3; |
| 626 | |
| 627 | if (event->header.type == PERF_RECORD_SAMPLE) { |
| 628 | if (evlist->id_pos >= n) |
| 629 | return -1; |
| 630 | *id = array[evlist->id_pos]; |
| 631 | } else { |
| 632 | if (evlist->is_pos > n) |
| 633 | return -1; |
| 634 | n -= evlist->is_pos; |
| 635 | *id = array[n]; |
| 636 | } |
| 637 | return 0; |
| 638 | } |
| 639 | |
Jiri Olsa | 7cb5c5a | 2016-07-10 13:07:53 +0200 | [diff] [blame] | 640 | struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, |
| 641 | union perf_event *event) |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 642 | { |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 643 | struct perf_evsel *first = perf_evlist__first(evlist); |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 644 | struct hlist_head *head; |
| 645 | struct perf_sample_id *sid; |
| 646 | int hash; |
| 647 | u64 id; |
| 648 | |
| 649 | if (evlist->nr_entries == 1) |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 650 | return first; |
| 651 | |
| 652 | if (!first->attr.sample_id_all && |
| 653 | event->header.type != PERF_RECORD_SAMPLE) |
| 654 | return first; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 655 | |
| 656 | if (perf_evlist__event2id(evlist, event, &id)) |
| 657 | return NULL; |
| 658 | |
| 659 | /* Synthesized events have an id of zero */ |
| 660 | if (!id) |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 661 | return first; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 662 | |
| 663 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
| 664 | head = &evlist->heads[hash]; |
| 665 | |
| 666 | hlist_for_each_entry(sid, head, node) { |
| 667 | if (sid->id == id) |
| 668 | return sid->evsel; |
| 669 | } |
| 670 | return NULL; |
| 671 | } |
| 672 | |
Wang Nan | 65aea23 | 2016-05-23 07:13:38 +0000 | [diff] [blame] | 673 | static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value) |
| 674 | { |
| 675 | int i; |
| 676 | |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 677 | if (!evlist->backward_mmap) |
| 678 | return 0; |
| 679 | |
Wang Nan | 65aea23 | 2016-05-23 07:13:38 +0000 | [diff] [blame] | 680 | for (i = 0; i < evlist->nr_mmaps; i++) { |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 681 | int fd = evlist->backward_mmap[i].fd; |
Wang Nan | 65aea23 | 2016-05-23 07:13:38 +0000 | [diff] [blame] | 682 | int err; |
| 683 | |
| 684 | if (fd < 0) |
| 685 | continue; |
| 686 | err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); |
| 687 | if (err) |
| 688 | return err; |
| 689 | } |
| 690 | return 0; |
| 691 | } |
| 692 | |
Wang Nan | f6cdff8 | 2016-07-14 08:34:44 +0000 | [diff] [blame] | 693 | static int perf_evlist__pause(struct perf_evlist *evlist) |
Wang Nan | 65aea23 | 2016-05-23 07:13:38 +0000 | [diff] [blame] | 694 | { |
| 695 | return perf_evlist__set_paused(evlist, true); |
| 696 | } |
| 697 | |
Wang Nan | f6cdff8 | 2016-07-14 08:34:44 +0000 | [diff] [blame] | 698 | static int perf_evlist__resume(struct perf_evlist *evlist) |
Wang Nan | 65aea23 | 2016-05-23 07:13:38 +0000 | [diff] [blame] | 699 | { |
| 700 | return perf_evlist__set_paused(evlist, false); |
| 701 | } |
| 702 | |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 703 | /* When check_messup is true, 'end' must points to a good entry */ |
Wang Nan | 0f4ccd1 | 2016-04-27 02:19:20 +0000 | [diff] [blame] | 704 | static union perf_event * |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 705 | perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start, |
| 706 | u64 end, u64 *prev) |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 707 | { |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 708 | unsigned char *data = md->base + page_size; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 709 | union perf_event *event = NULL; |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 710 | int diff = end - start; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 711 | |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 712 | if (check_messup) { |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 713 | /* |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 714 | * If we're further behind than half the buffer, there's a chance |
| 715 | * the writer will bite our tail and mess up the samples under us. |
| 716 | * |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 717 | * If we somehow ended up ahead of the 'end', we got messed up. |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 718 | * |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 719 | * In either case, truncate and restart at 'end'. |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 720 | */ |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 721 | if (diff > md->mask / 2 || diff < 0) { |
| 722 | fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); |
| 723 | |
| 724 | /* |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 725 | * 'end' points to a known good entry, start there. |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 726 | */ |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 727 | start = end; |
Wang Nan | b04b702 | 2016-04-26 02:28:54 +0000 | [diff] [blame] | 728 | diff = 0; |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 729 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 730 | } |
| 731 | |
Wang Nan | b04b702 | 2016-04-26 02:28:54 +0000 | [diff] [blame] | 732 | if (diff >= (int)sizeof(event->header)) { |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 733 | size_t size; |
| 734 | |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 735 | event = (union perf_event *)&data[start & md->mask]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 736 | size = event->header.size; |
| 737 | |
Wang Nan | b04b702 | 2016-04-26 02:28:54 +0000 | [diff] [blame] | 738 | if (size < sizeof(event->header) || diff < (int)size) { |
| 739 | event = NULL; |
| 740 | goto broken_event; |
| 741 | } |
| 742 | |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 743 | /* |
| 744 | * Event straddles the mmap boundary -- header should always |
| 745 | * be inside due to u64 alignment of output. |
| 746 | */ |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 747 | if ((start & md->mask) + size != ((start + size) & md->mask)) { |
| 748 | unsigned int offset = start; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 749 | unsigned int len = min(sizeof(*event), size), cpy; |
Jiri Olsa | a65cb4b | 2013-10-02 15:46:39 +0200 | [diff] [blame] | 750 | void *dst = md->event_copy; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 751 | |
| 752 | do { |
| 753 | cpy = min(md->mask + 1 - (offset & md->mask), len); |
| 754 | memcpy(dst, &data[offset & md->mask], cpy); |
| 755 | offset += cpy; |
| 756 | dst += cpy; |
| 757 | len -= cpy; |
| 758 | } while (len); |
| 759 | |
Jiri Olsa | a65cb4b | 2013-10-02 15:46:39 +0200 | [diff] [blame] | 760 | event = (union perf_event *) md->event_copy; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 761 | } |
| 762 | |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 763 | start += size; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 764 | } |
| 765 | |
Wang Nan | b04b702 | 2016-04-26 02:28:54 +0000 | [diff] [blame] | 766 | broken_event: |
Wang Nan | 0f4ccd1 | 2016-04-27 02:19:20 +0000 | [diff] [blame] | 767 | if (prev) |
Wang Nan | b6b85da | 2016-04-27 02:19:21 +0000 | [diff] [blame] | 768 | *prev = start; |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 769 | |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 770 | return event; |
| 771 | } |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 772 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 773 | union perf_event *perf_mmap__read_forward(struct perf_mmap *md, bool check_messup) |
Wang Nan | 0f4ccd1 | 2016-04-27 02:19:20 +0000 | [diff] [blame] | 774 | { |
Wang Nan | 0f4ccd1 | 2016-04-27 02:19:20 +0000 | [diff] [blame] | 775 | u64 head; |
| 776 | u64 old = md->prev; |
| 777 | |
| 778 | /* |
| 779 | * Check if event was unmapped due to a POLLHUP/POLLERR. |
| 780 | */ |
| 781 | if (!atomic_read(&md->refcnt)) |
| 782 | return NULL; |
| 783 | |
| 784 | head = perf_mmap__read_head(md); |
| 785 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 786 | return perf_mmap__read(md, check_messup, old, head, &md->prev); |
Wang Nan | 0f4ccd1 | 2016-04-27 02:19:20 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Wang Nan | e24c752 | 2016-05-09 01:47:50 +0000 | [diff] [blame] | 789 | union perf_event * |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 790 | perf_mmap__read_backward(struct perf_mmap *md) |
Wang Nan | e24c752 | 2016-05-09 01:47:50 +0000 | [diff] [blame] | 791 | { |
Wang Nan | e24c752 | 2016-05-09 01:47:50 +0000 | [diff] [blame] | 792 | u64 head, end; |
| 793 | u64 start = md->prev; |
| 794 | |
| 795 | /* |
| 796 | * Check if event was unmapped due to a POLLHUP/POLLERR. |
| 797 | */ |
| 798 | if (!atomic_read(&md->refcnt)) |
| 799 | return NULL; |
| 800 | |
| 801 | head = perf_mmap__read_head(md); |
| 802 | if (!head) |
| 803 | return NULL; |
| 804 | |
| 805 | /* |
| 806 | * 'head' pointer starts from 0. Kernel minus sizeof(record) form |
| 807 | * it each time when kernel writes to it, so in fact 'head' is |
| 808 | * negative. 'end' pointer is made manually by adding the size of |
| 809 | * the ring buffer to 'head' pointer, means the validate data can |
| 810 | * read is the whole ring buffer. If 'end' is positive, the ring |
| 811 | * buffer has not fully filled, so we must adjust 'end' to 0. |
| 812 | * |
| 813 | * However, since both 'head' and 'end' is unsigned, we can't |
| 814 | * simply compare 'end' against 0. Here we compare '-head' and |
| 815 | * the size of the ring buffer, where -head is the number of bytes |
| 816 | * kernel write to the ring buffer. |
| 817 | */ |
| 818 | if (-head < (u64)(md->mask + 1)) |
| 819 | end = 0; |
| 820 | else |
| 821 | end = head + md->mask + 1; |
| 822 | |
| 823 | return perf_mmap__read(md, false, start, end, &md->prev); |
| 824 | } |
| 825 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 826 | union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx) |
| 827 | { |
| 828 | struct perf_mmap *md = &evlist->mmap[idx]; |
| 829 | |
| 830 | /* |
| 831 | * Check messup is required for forward overwritable ring buffer: |
| 832 | * memory pointed by md->prev can be overwritten in this case. |
| 833 | * No need for read-write ring buffer: kernel stop outputting when |
| 834 | * it hit md->prev (perf_mmap__consume()). |
| 835 | */ |
| 836 | return perf_mmap__read_forward(md, evlist->overwrite); |
| 837 | } |
| 838 | |
| 839 | union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx) |
| 840 | { |
| 841 | struct perf_mmap *md = &evlist->mmap[idx]; |
| 842 | |
| 843 | /* |
| 844 | * No need to check messup for backward ring buffer: |
| 845 | * We can always read arbitrary long data from a backward |
| 846 | * ring buffer unless we forget to pause it before reading. |
| 847 | */ |
| 848 | return perf_mmap__read_backward(md); |
| 849 | } |
| 850 | |
Wang Nan | 5a5ddeb | 2016-05-25 13:44:50 +0000 | [diff] [blame] | 851 | union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) |
| 852 | { |
Wang Nan | a0c6f45 | 2016-07-14 08:34:41 +0000 | [diff] [blame] | 853 | return perf_evlist__mmap_read_forward(evlist, idx); |
Wang Nan | 5a5ddeb | 2016-05-25 13:44:50 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 856 | void perf_mmap__read_catchup(struct perf_mmap *md) |
Wang Nan | e24c752 | 2016-05-09 01:47:50 +0000 | [diff] [blame] | 857 | { |
Wang Nan | e24c752 | 2016-05-09 01:47:50 +0000 | [diff] [blame] | 858 | u64 head; |
| 859 | |
| 860 | if (!atomic_read(&md->refcnt)) |
| 861 | return; |
| 862 | |
| 863 | head = perf_mmap__read_head(md); |
| 864 | md->prev = head; |
| 865 | } |
| 866 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 867 | void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx) |
| 868 | { |
| 869 | perf_mmap__read_catchup(&evlist->mmap[idx]); |
| 870 | } |
| 871 | |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 872 | static bool perf_mmap__empty(struct perf_mmap *md) |
| 873 | { |
Adrian Hunter | b72e74d | 2015-04-24 22:29:43 +0300 | [diff] [blame] | 874 | return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base; |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 875 | } |
| 876 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 877 | static void perf_mmap__get(struct perf_mmap *map) |
| 878 | { |
| 879 | atomic_inc(&map->refcnt); |
| 880 | } |
| 881 | |
| 882 | static void perf_mmap__put(struct perf_mmap *md) |
| 883 | { |
| 884 | BUG_ON(md->base && atomic_read(&md->refcnt) == 0); |
| 885 | |
| 886 | if (atomic_dec_and_test(&md->refcnt)) |
| 887 | perf_mmap__munmap(md); |
| 888 | } |
| 889 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 890 | void perf_mmap__consume(struct perf_mmap *md, bool overwrite) |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 891 | { |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 892 | if (!overwrite) { |
David Ahern | 7b8283b5 | 2015-04-07 09:20:37 -0600 | [diff] [blame] | 893 | u64 old = md->prev; |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 894 | |
| 895 | perf_mmap__write_tail(md, old); |
| 896 | } |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 897 | |
Arnaldo Carvalho de Melo | 7143849 | 2015-05-15 15:45:16 -0300 | [diff] [blame] | 898 | if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md)) |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 899 | perf_mmap__put(md); |
| 900 | } |
| 901 | |
| 902 | void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) |
| 903 | { |
| 904 | perf_mmap__consume(&evlist->mmap[idx], evlist->overwrite); |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 905 | } |
| 906 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 907 | int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused, |
| 908 | struct auxtrace_mmap_params *mp __maybe_unused, |
| 909 | void *userpg __maybe_unused, |
| 910 | int fd __maybe_unused) |
| 911 | { |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused) |
| 916 | { |
| 917 | } |
| 918 | |
| 919 | void __weak auxtrace_mmap_params__init( |
| 920 | struct auxtrace_mmap_params *mp __maybe_unused, |
| 921 | off_t auxtrace_offset __maybe_unused, |
| 922 | unsigned int auxtrace_pages __maybe_unused, |
| 923 | bool auxtrace_overwrite __maybe_unused) |
| 924 | { |
| 925 | } |
| 926 | |
| 927 | void __weak auxtrace_mmap_params__set_idx( |
| 928 | struct auxtrace_mmap_params *mp __maybe_unused, |
| 929 | struct perf_evlist *evlist __maybe_unused, |
| 930 | int idx __maybe_unused, |
| 931 | bool per_cpu __maybe_unused) |
| 932 | { |
| 933 | } |
| 934 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 935 | static void perf_mmap__munmap(struct perf_mmap *map) |
| 936 | { |
| 937 | if (map->base != NULL) { |
| 938 | munmap(map->base, perf_mmap__mmap_len(map)); |
| 939 | map->base = NULL; |
| 940 | map->fd = -1; |
| 941 | atomic_set(&map->refcnt, 0); |
| 942 | } |
| 943 | auxtrace_mmap__munmap(&map->auxtrace_mmap); |
| 944 | } |
| 945 | |
Wang Nan | a1f7261 | 2016-07-14 08:34:38 +0000 | [diff] [blame] | 946 | static void perf_evlist__munmap_nofree(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 947 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 948 | int i; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 949 | |
Wang Nan | b2cb615 | 2016-07-14 08:34:39 +0000 | [diff] [blame] | 950 | if (evlist->mmap) |
| 951 | for (i = 0; i < evlist->nr_mmaps; i++) |
| 952 | perf_mmap__munmap(&evlist->mmap[i]); |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 953 | |
Wang Nan | b2cb615 | 2016-07-14 08:34:39 +0000 | [diff] [blame] | 954 | if (evlist->backward_mmap) |
| 955 | for (i = 0; i < evlist->nr_mmaps; i++) |
| 956 | perf_mmap__munmap(&evlist->backward_mmap[i]); |
Wang Nan | a1f7261 | 2016-07-14 08:34:38 +0000 | [diff] [blame] | 957 | } |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 958 | |
Wang Nan | a1f7261 | 2016-07-14 08:34:38 +0000 | [diff] [blame] | 959 | void perf_evlist__munmap(struct perf_evlist *evlist) |
| 960 | { |
| 961 | perf_evlist__munmap_nofree(evlist); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 962 | zfree(&evlist->mmap); |
Wang Nan | b2cb615 | 2016-07-14 08:34:39 +0000 | [diff] [blame] | 963 | zfree(&evlist->backward_mmap); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 964 | } |
| 965 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 966 | static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 967 | { |
Wang Nan | d4c6fb3 | 2016-05-20 16:38:24 +0000 | [diff] [blame] | 968 | int i; |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 969 | struct perf_mmap *map; |
Wang Nan | d4c6fb3 | 2016-05-20 16:38:24 +0000 | [diff] [blame] | 970 | |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 971 | evlist->nr_mmaps = cpu_map__nr(evlist->cpus); |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 972 | if (cpu_map__empty(evlist->cpus)) |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 973 | evlist->nr_mmaps = thread_map__nr(evlist->threads); |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 974 | map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); |
| 975 | if (!map) |
| 976 | return NULL; |
Wang Nan | 946ae1d | 2016-05-31 13:06:15 +0000 | [diff] [blame] | 977 | |
Wang Nan | d4c6fb3 | 2016-05-20 16:38:24 +0000 | [diff] [blame] | 978 | for (i = 0; i < evlist->nr_mmaps; i++) |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 979 | map[i].fd = -1; |
| 980 | return map; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 981 | } |
| 982 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 983 | struct mmap_params { |
| 984 | int prot; |
| 985 | int mask; |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 986 | struct auxtrace_mmap_params auxtrace_mp; |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 987 | }; |
| 988 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 989 | static int perf_mmap__mmap(struct perf_mmap *map, |
| 990 | struct mmap_params *mp, int fd) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 991 | { |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 992 | /* |
| 993 | * The last one will be done at perf_evlist__mmap_consume(), so that we |
| 994 | * make sure we don't prevent tools from consuming every last event in |
| 995 | * the ring buffer. |
| 996 | * |
| 997 | * I.e. we can get the POLLHUP meaning that the fd doesn't exist |
| 998 | * anymore, but the last events for it are still in the ring buffer, |
| 999 | * waiting to be consumed. |
| 1000 | * |
| 1001 | * Tools can chose to ignore this at their own discretion, but the |
| 1002 | * evlist layer can't just drop it when filtering events in |
| 1003 | * perf_evlist__filter_pollfd(). |
| 1004 | */ |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 1005 | atomic_set(&map->refcnt, 2); |
| 1006 | map->prev = 0; |
| 1007 | map->mask = mp->mask; |
| 1008 | map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot, |
| 1009 | MAP_SHARED, fd, 0); |
| 1010 | if (map->base == MAP_FAILED) { |
Adrian Hunter | 0263596 | 2013-11-01 15:51:33 +0200 | [diff] [blame] | 1011 | pr_debug2("failed to mmap perf event ring buffer, error %d\n", |
| 1012 | errno); |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 1013 | map->base = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1014 | return -1; |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 1015 | } |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 1016 | map->fd = fd; |
Arnaldo Carvalho de Melo | ad6765d | 2014-08-18 16:44:06 -0300 | [diff] [blame] | 1017 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 1018 | if (auxtrace_mmap__mmap(&map->auxtrace_mmap, |
| 1019 | &mp->auxtrace_mp, map->base, fd)) |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1020 | return -1; |
| 1021 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
| 1024 | |
Wang Nan | f3058a1 | 2016-05-24 02:28:59 +0000 | [diff] [blame] | 1025 | static bool |
| 1026 | perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused, |
| 1027 | struct perf_evsel *evsel) |
| 1028 | { |
Arnaldo Carvalho de Melo | 32a951b | 2016-07-14 08:34:33 +0000 | [diff] [blame] | 1029 | if (evsel->attr.write_backward) |
Wang Nan | f3058a1 | 2016-05-24 02:28:59 +0000 | [diff] [blame] | 1030 | return false; |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1034 | static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1035 | struct mmap_params *mp, int cpu, |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1036 | int thread, int *_output, int *_output_backward) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1037 | { |
| 1038 | struct perf_evsel *evsel; |
Wang Nan | f3058a1 | 2016-05-24 02:28:59 +0000 | [diff] [blame] | 1039 | int revent; |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1040 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1041 | evlist__for_each_entry(evlist, evsel) { |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1042 | struct perf_mmap *maps = evlist->mmap; |
| 1043 | int *output = _output; |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1044 | int fd; |
| 1045 | |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1046 | if (evsel->attr.write_backward) { |
| 1047 | output = _output_backward; |
| 1048 | maps = evlist->backward_mmap; |
| 1049 | |
| 1050 | if (!maps) { |
| 1051 | maps = perf_evlist__alloc_mmap(evlist); |
| 1052 | if (!maps) |
| 1053 | return -1; |
| 1054 | evlist->backward_mmap = maps; |
Wang Nan | 54cc54d | 2016-07-14 08:34:42 +0000 | [diff] [blame] | 1055 | if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) |
| 1056 | perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
Wang Nan | f3058a1 | 2016-05-24 02:28:59 +0000 | [diff] [blame] | 1059 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1060 | if (evsel->system_wide && thread) |
| 1061 | continue; |
| 1062 | |
| 1063 | fd = FD(evsel, cpu, thread); |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1064 | |
| 1065 | if (*output == -1) { |
| 1066 | *output = fd; |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (perf_mmap__mmap(&maps[idx], mp, *output) < 0) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1069 | return -1; |
| 1070 | } else { |
| 1071 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) |
| 1072 | return -1; |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 1073 | |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1074 | perf_mmap__get(&maps[idx]); |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1075 | } |
| 1076 | |
Wang Nan | f3058a1 | 2016-05-24 02:28:59 +0000 | [diff] [blame] | 1077 | revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; |
| 1078 | |
Adrian Hunter | f90d194 | 2014-11-11 16:16:39 +0200 | [diff] [blame] | 1079 | /* |
| 1080 | * The system_wide flag causes a selected event to be opened |
| 1081 | * always without a pid. Consequently it will never get a |
| 1082 | * POLLHUP, but it is used for tracking in combination with |
| 1083 | * other events, so it should not need to be polled anyway. |
| 1084 | * Therefore don't add it for polling. |
| 1085 | */ |
| 1086 | if (!evsel->system_wide && |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1087 | __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) { |
| 1088 | perf_mmap__put(&maps[idx]); |
Arnaldo Carvalho de Melo | ad6765d | 2014-08-18 16:44:06 -0300 | [diff] [blame] | 1089 | return -1; |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 1090 | } |
Arnaldo Carvalho de Melo | 033fa71 | 2014-09-08 12:55:12 -0300 | [diff] [blame] | 1091 | |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 1092 | if (evsel->attr.read_format & PERF_FORMAT_ID) { |
| 1093 | if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, |
| 1094 | fd) < 0) |
| 1095 | return -1; |
| 1096 | perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, |
| 1097 | thread); |
| 1098 | } |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1104 | static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, |
| 1105 | struct mmap_params *mp) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1106 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1107 | int cpu, thread; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 1108 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 1109 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1110 | |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1111 | pr_debug2("perf event ring buffer mmapped per cpu\n"); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 1112 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1113 | int output = -1; |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1114 | int output_backward = -1; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1115 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1116 | auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, |
| 1117 | true); |
| 1118 | |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 1119 | for (thread = 0; thread < nr_threads; thread++) { |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1120 | if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1121 | thread, &output, &output_backward)) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1122 | goto out_unmap; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | return 0; |
| 1127 | |
| 1128 | out_unmap: |
Wang Nan | a1f7261 | 2016-07-14 08:34:38 +0000 | [diff] [blame] | 1129 | perf_evlist__munmap_nofree(evlist); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1130 | return -1; |
| 1131 | } |
| 1132 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1133 | static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, |
| 1134 | struct mmap_params *mp) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1135 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1136 | int thread; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 1137 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1138 | |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1139 | pr_debug2("perf event ring buffer mmapped per thread\n"); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 1140 | for (thread = 0; thread < nr_threads; thread++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1141 | int output = -1; |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1142 | int output_backward = -1; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1143 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1144 | auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, |
| 1145 | false); |
| 1146 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1147 | if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, |
Wang Nan | 078c338 | 2016-07-14 08:34:40 +0000 | [diff] [blame] | 1148 | &output, &output_backward)) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 1149 | goto out_unmap; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | return 0; |
| 1153 | |
| 1154 | out_unmap: |
Wang Nan | a1f7261 | 2016-07-14 08:34:38 +0000 | [diff] [blame] | 1155 | perf_evlist__munmap_nofree(evlist); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1156 | return -1; |
| 1157 | } |
| 1158 | |
Arnaldo Carvalho de Melo | f5e7150 | 2016-04-15 17:46:31 -0300 | [diff] [blame] | 1159 | unsigned long perf_event_mlock_kb_in_pages(void) |
| 1160 | { |
| 1161 | unsigned long pages; |
| 1162 | int max; |
| 1163 | |
| 1164 | if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { |
| 1165 | /* |
| 1166 | * Pick a once upon a time good value, i.e. things look |
| 1167 | * strange since we can't read a sysctl value, but lets not |
| 1168 | * die yet... |
| 1169 | */ |
| 1170 | max = 512; |
| 1171 | } else { |
| 1172 | max -= (page_size / 1024); |
| 1173 | } |
| 1174 | |
| 1175 | pages = (max * 1024) / page_size; |
| 1176 | if (!is_power_of_2(pages)) |
| 1177 | pages = rounddown_pow_of_two(pages); |
| 1178 | |
| 1179 | return pages; |
| 1180 | } |
| 1181 | |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 1182 | static size_t perf_evlist__mmap_size(unsigned long pages) |
| 1183 | { |
Arnaldo Carvalho de Melo | f5e7150 | 2016-04-15 17:46:31 -0300 | [diff] [blame] | 1184 | if (pages == UINT_MAX) |
| 1185 | pages = perf_event_mlock_kb_in_pages(); |
| 1186 | else if (!is_power_of_2(pages)) |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 1187 | return 0; |
| 1188 | |
| 1189 | return (pages + 1) * page_size; |
| 1190 | } |
| 1191 | |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1192 | static long parse_pages_arg(const char *str, unsigned long min, |
| 1193 | unsigned long max) |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 1194 | { |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 1195 | unsigned long pages, val; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 1196 | static struct parse_tag tags[] = { |
| 1197 | { .tag = 'B', .mult = 1 }, |
| 1198 | { .tag = 'K', .mult = 1 << 10 }, |
| 1199 | { .tag = 'M', .mult = 1 << 20 }, |
| 1200 | { .tag = 'G', .mult = 1 << 30 }, |
| 1201 | { .tag = 0 }, |
| 1202 | }; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 1203 | |
David Ahern | 8973504 | 2013-11-12 07:46:53 -0700 | [diff] [blame] | 1204 | if (str == NULL) |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1205 | return -EINVAL; |
David Ahern | 8973504 | 2013-11-12 07:46:53 -0700 | [diff] [blame] | 1206 | |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 1207 | val = parse_tag_value(str, tags); |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 1208 | if (val != (unsigned long) -1) { |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 1209 | /* we got file size value */ |
| 1210 | pages = PERF_ALIGN(val, page_size) / page_size; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 1211 | } else { |
| 1212 | /* we got pages count value */ |
| 1213 | char *eptr; |
| 1214 | pages = strtoul(str, &eptr, 10); |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1215 | if (*eptr != '\0') |
| 1216 | return -EINVAL; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 1217 | } |
| 1218 | |
Adrian Hunter | 2bcab6c | 2013-12-09 15:18:37 +0200 | [diff] [blame] | 1219 | if (pages == 0 && min == 0) { |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1220 | /* leave number of pages at 0 */ |
Adrian Hunter | 1dbfa938 | 2013-12-09 15:18:39 +0200 | [diff] [blame] | 1221 | } else if (!is_power_of_2(pages)) { |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1222 | /* round pages up to next power of 2 */ |
Arnaldo Carvalho de Melo | 9152983 | 2014-12-16 13:24:41 -0300 | [diff] [blame] | 1223 | pages = roundup_pow_of_two(pages); |
Adrian Hunter | 1dbfa938 | 2013-12-09 15:18:39 +0200 | [diff] [blame] | 1224 | if (!pages) |
| 1225 | return -EINVAL; |
David Ahern | 9639837 | 2013-11-12 07:46:54 -0700 | [diff] [blame] | 1226 | pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", |
| 1227 | pages * page_size, pages); |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 1228 | } |
| 1229 | |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1230 | if (pages > max) |
| 1231 | return -EINVAL; |
| 1232 | |
| 1233 | return pages; |
| 1234 | } |
| 1235 | |
Adrian Hunter | e9db131 | 2015-04-09 18:53:46 +0300 | [diff] [blame] | 1236 | int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1237 | { |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1238 | unsigned long max = UINT_MAX; |
| 1239 | long pages; |
| 1240 | |
Adrian Hunter | f5ae9c4 | 2013-12-09 15:18:38 +0200 | [diff] [blame] | 1241 | if (max > SIZE_MAX / page_size) |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 1242 | max = SIZE_MAX / page_size; |
| 1243 | |
| 1244 | pages = parse_pages_arg(str, 1, max); |
| 1245 | if (pages < 0) { |
| 1246 | pr_err("Invalid argument for --mmap_pages/-m\n"); |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 1247 | return -1; |
| 1248 | } |
| 1249 | |
| 1250 | *mmap_pages = pages; |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Adrian Hunter | e9db131 | 2015-04-09 18:53:46 +0300 | [diff] [blame] | 1254 | int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, |
| 1255 | int unset __maybe_unused) |
| 1256 | { |
| 1257 | return __perf_evlist__parse_mmap_pages(opt->value, str); |
| 1258 | } |
| 1259 | |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 1260 | /** |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1261 | * perf_evlist__mmap_ex - Create mmaps to receive events. |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 1262 | * @evlist: list of events |
| 1263 | * @pages: map length in pages |
| 1264 | * @overwrite: overwrite older events? |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1265 | * @auxtrace_pages - auxtrace map length in pages |
| 1266 | * @auxtrace_overwrite - overwrite older auxtrace data? |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1267 | * |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 1268 | * If @overwrite is %false the user needs to signal event consumption using |
| 1269 | * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this |
| 1270 | * automatically. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1271 | * |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1272 | * Similarly, if @auxtrace_overwrite is %false the user needs to signal data |
| 1273 | * consumption using auxtrace_mmap__write_tail(). |
| 1274 | * |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 1275 | * Return: %0 on success, negative error code otherwise. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1276 | */ |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1277 | int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, |
| 1278 | bool overwrite, unsigned int auxtrace_pages, |
| 1279 | bool auxtrace_overwrite) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1280 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 1281 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1282 | const struct cpu_map *cpus = evlist->cpus; |
| 1283 | const struct thread_map *threads = evlist->threads; |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1284 | struct mmap_params mp = { |
| 1285 | .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), |
| 1286 | }; |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 1287 | |
Wang Nan | 8db6d6b | 2016-07-14 08:34:35 +0000 | [diff] [blame] | 1288 | if (!evlist->mmap) |
| 1289 | evlist->mmap = perf_evlist__alloc_mmap(evlist); |
| 1290 | if (!evlist->mmap) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1291 | return -ENOMEM; |
| 1292 | |
Arnaldo Carvalho de Melo | 1b85337 | 2014-09-03 18:02:59 -0300 | [diff] [blame] | 1293 | if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1294 | return -ENOMEM; |
| 1295 | |
| 1296 | evlist->overwrite = overwrite; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 1297 | evlist->mmap_len = perf_evlist__mmap_size(pages); |
Adrian Hunter | 2af68ef | 2013-10-18 15:29:07 +0300 | [diff] [blame] | 1298 | pr_debug("mmap size %zuB\n", evlist->mmap_len); |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1299 | mp.mask = evlist->mmap_len - page_size - 1; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1300 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1301 | auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, |
| 1302 | auxtrace_pages, auxtrace_overwrite); |
| 1303 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1304 | evlist__for_each_entry(evlist, evsel) { |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1305 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1306 | evsel->sample_id == NULL && |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 1307 | perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1308 | return -ENOMEM; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 1311 | if (cpu_map__empty(cpus)) |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1312 | return perf_evlist__mmap_per_thread(evlist, &mp); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1313 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1314 | return perf_evlist__mmap_per_cpu(evlist, &mp); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1315 | } |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1316 | |
Adrian Hunter | 718c602 | 2015-04-09 18:53:42 +0300 | [diff] [blame] | 1317 | int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, |
| 1318 | bool overwrite) |
| 1319 | { |
| 1320 | return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false); |
| 1321 | } |
| 1322 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1323 | int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1324 | { |
Adrian Hunter | 74bfd2b | 2015-09-08 10:58:57 +0300 | [diff] [blame] | 1325 | struct cpu_map *cpus; |
| 1326 | struct thread_map *threads; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1327 | |
Adrian Hunter | 74bfd2b | 2015-09-08 10:58:57 +0300 | [diff] [blame] | 1328 | threads = thread_map__new_str(target->pid, target->tid, target->uid); |
| 1329 | |
| 1330 | if (!threads) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1331 | return -1; |
| 1332 | |
Dongsheng Yang | 9c105fb | 2013-12-04 17:56:40 -0500 | [diff] [blame] | 1333 | if (target__uses_dummy_map(target)) |
Adrian Hunter | 74bfd2b | 2015-09-08 10:58:57 +0300 | [diff] [blame] | 1334 | cpus = cpu_map__dummy_new(); |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 1335 | else |
Adrian Hunter | 74bfd2b | 2015-09-08 10:58:57 +0300 | [diff] [blame] | 1336 | cpus = cpu_map__new(target->cpu_list); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1337 | |
Adrian Hunter | 74bfd2b | 2015-09-08 10:58:57 +0300 | [diff] [blame] | 1338 | if (!cpus) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1339 | goto out_delete_threads; |
| 1340 | |
Adrian Hunter | ec9a77a | 2015-09-08 10:58:52 +0300 | [diff] [blame] | 1341 | evlist->has_user_cpus = !!target->cpu_list; |
| 1342 | |
Adrian Hunter | 74bfd2b | 2015-09-08 10:58:57 +0300 | [diff] [blame] | 1343 | perf_evlist__set_maps(evlist, cpus, threads); |
Adrian Hunter | d5bc056 | 2015-09-08 10:58:51 +0300 | [diff] [blame] | 1344 | |
| 1345 | return 0; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1346 | |
| 1347 | out_delete_threads: |
Adrian Hunter | 74bfd2b | 2015-09-08 10:58:57 +0300 | [diff] [blame] | 1348 | thread_map__put(threads); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1349 | return -1; |
| 1350 | } |
| 1351 | |
Adrian Hunter | d5bc056 | 2015-09-08 10:58:51 +0300 | [diff] [blame] | 1352 | void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, |
| 1353 | struct thread_map *threads) |
Jiri Olsa | 3de5cfb | 2015-07-21 14:31:30 +0200 | [diff] [blame] | 1354 | { |
Adrian Hunter | 934e0f2 | 2015-09-08 10:58:56 +0300 | [diff] [blame] | 1355 | /* |
| 1356 | * Allow for the possibility that one or another of the maps isn't being |
| 1357 | * changed i.e. don't put it. Note we are assuming the maps that are |
| 1358 | * being applied are brand new and evlist is taking ownership of the |
| 1359 | * original reference count of 1. If that is not the case it is up to |
| 1360 | * the caller to increase the reference count. |
| 1361 | */ |
| 1362 | if (cpus != evlist->cpus) { |
Jiri Olsa | 3de5cfb | 2015-07-21 14:31:30 +0200 | [diff] [blame] | 1363 | cpu_map__put(evlist->cpus); |
Arnaldo Carvalho de Melo | a55e566 | 2016-02-17 10:57:19 -0300 | [diff] [blame] | 1364 | evlist->cpus = cpu_map__get(cpus); |
Adrian Hunter | 934e0f2 | 2015-09-08 10:58:56 +0300 | [diff] [blame] | 1365 | } |
Jiri Olsa | 3de5cfb | 2015-07-21 14:31:30 +0200 | [diff] [blame] | 1366 | |
Adrian Hunter | 934e0f2 | 2015-09-08 10:58:56 +0300 | [diff] [blame] | 1367 | if (threads != evlist->threads) { |
Jiri Olsa | 3de5cfb | 2015-07-21 14:31:30 +0200 | [diff] [blame] | 1368 | thread_map__put(evlist->threads); |
Arnaldo Carvalho de Melo | a55e566 | 2016-02-17 10:57:19 -0300 | [diff] [blame] | 1369 | evlist->threads = thread_map__get(threads); |
Adrian Hunter | 934e0f2 | 2015-09-08 10:58:56 +0300 | [diff] [blame] | 1370 | } |
Jiri Olsa | 3de5cfb | 2015-07-21 14:31:30 +0200 | [diff] [blame] | 1371 | |
Adrian Hunter | ec9a77a | 2015-09-08 10:58:52 +0300 | [diff] [blame] | 1372 | perf_evlist__propagate_maps(evlist); |
Jiri Olsa | 3de5cfb | 2015-07-21 14:31:30 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
Arnaldo Carvalho de Melo | 22c8a37 | 2016-04-11 18:37:45 -0300 | [diff] [blame] | 1375 | void __perf_evlist__set_sample_bit(struct perf_evlist *evlist, |
| 1376 | enum perf_event_sample_format bit) |
| 1377 | { |
| 1378 | struct perf_evsel *evsel; |
| 1379 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1380 | evlist__for_each_entry(evlist, evsel) |
Arnaldo Carvalho de Melo | 22c8a37 | 2016-04-11 18:37:45 -0300 | [diff] [blame] | 1381 | __perf_evsel__set_sample_bit(evsel, bit); |
| 1382 | } |
| 1383 | |
| 1384 | void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist, |
| 1385 | enum perf_event_sample_format bit) |
| 1386 | { |
| 1387 | struct perf_evsel *evsel; |
| 1388 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1389 | evlist__for_each_entry(evlist, evsel) |
Arnaldo Carvalho de Melo | 22c8a37 | 2016-04-11 18:37:45 -0300 | [diff] [blame] | 1390 | __perf_evsel__reset_sample_bit(evsel, bit); |
| 1391 | } |
| 1392 | |
Arnaldo Carvalho de Melo | 23d4aad | 2015-03-24 19:23:47 -0300 | [diff] [blame] | 1393 | int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1394 | { |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1395 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1396 | int err = 0; |
| 1397 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 1398 | nthreads = thread_map__nr(evlist->threads); |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1399 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1400 | evlist__for_each_entry(evlist, evsel) { |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1401 | if (evsel->filter == NULL) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1402 | continue; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1403 | |
Kan Liang | d988d5e | 2015-08-21 02:23:14 -0400 | [diff] [blame] | 1404 | /* |
| 1405 | * filters only work for tracepoint event, which doesn't have cpu limit. |
| 1406 | * So evlist and evsel should always be same. |
| 1407 | */ |
Arnaldo Carvalho de Melo | f47805a | 2015-07-03 15:53:49 -0300 | [diff] [blame] | 1408 | err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter); |
Arnaldo Carvalho de Melo | 23d4aad | 2015-03-24 19:23:47 -0300 | [diff] [blame] | 1409 | if (err) { |
| 1410 | *err_evsel = evsel; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1411 | break; |
Arnaldo Carvalho de Melo | 23d4aad | 2015-03-24 19:23:47 -0300 | [diff] [blame] | 1412 | } |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1413 | } |
| 1414 | |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1415 | return err; |
| 1416 | } |
| 1417 | |
| 1418 | int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) |
| 1419 | { |
| 1420 | struct perf_evsel *evsel; |
| 1421 | int err = 0; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1422 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1423 | evlist__for_each_entry(evlist, evsel) { |
Wang Nan | fdf1472 | 2016-02-26 09:31:53 +0000 | [diff] [blame] | 1424 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) |
| 1425 | continue; |
| 1426 | |
Arnaldo Carvalho de Melo | 94ad89b | 2015-07-03 17:42:03 -0300 | [diff] [blame] | 1427 | err = perf_evsel__set_filter(evsel, filter); |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1428 | if (err) |
| 1429 | break; |
| 1430 | } |
| 1431 | |
| 1432 | return err; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1433 | } |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1434 | |
Arnaldo Carvalho de Melo | be199ad | 2015-02-21 11:33:47 -0800 | [diff] [blame] | 1435 | int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) |
Arnaldo Carvalho de Melo | cfd70a2 | 2015-02-21 10:09:55 -0800 | [diff] [blame] | 1436 | { |
| 1437 | char *filter; |
Arnaldo Carvalho de Melo | be199ad | 2015-02-21 11:33:47 -0800 | [diff] [blame] | 1438 | int ret = -1; |
| 1439 | size_t i; |
Arnaldo Carvalho de Melo | cfd70a2 | 2015-02-21 10:09:55 -0800 | [diff] [blame] | 1440 | |
Arnaldo Carvalho de Melo | be199ad | 2015-02-21 11:33:47 -0800 | [diff] [blame] | 1441 | for (i = 0; i < npids; ++i) { |
| 1442 | if (i == 0) { |
| 1443 | if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) |
| 1444 | return -1; |
| 1445 | } else { |
| 1446 | char *tmp; |
| 1447 | |
| 1448 | if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) |
| 1449 | goto out_free; |
| 1450 | |
| 1451 | free(filter); |
| 1452 | filter = tmp; |
| 1453 | } |
| 1454 | } |
Arnaldo Carvalho de Melo | cfd70a2 | 2015-02-21 10:09:55 -0800 | [diff] [blame] | 1455 | |
| 1456 | ret = perf_evlist__set_filter(evlist, filter); |
Arnaldo Carvalho de Melo | be199ad | 2015-02-21 11:33:47 -0800 | [diff] [blame] | 1457 | out_free: |
Arnaldo Carvalho de Melo | cfd70a2 | 2015-02-21 10:09:55 -0800 | [diff] [blame] | 1458 | free(filter); |
| 1459 | return ret; |
| 1460 | } |
| 1461 | |
Arnaldo Carvalho de Melo | be199ad | 2015-02-21 11:33:47 -0800 | [diff] [blame] | 1462 | int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) |
| 1463 | { |
| 1464 | return perf_evlist__set_filter_pids(evlist, 1, &pid); |
| 1465 | } |
| 1466 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1467 | bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1468 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1469 | struct perf_evsel *pos; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1470 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1471 | if (evlist->nr_entries == 1) |
| 1472 | return true; |
| 1473 | |
| 1474 | if (evlist->id_pos < 0 || evlist->is_pos < 0) |
| 1475 | return false; |
| 1476 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1477 | evlist__for_each_entry(evlist, pos) { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1478 | if (pos->id_pos != evlist->id_pos || |
| 1479 | pos->is_pos != evlist->is_pos) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1480 | return false; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1483 | return true; |
| 1484 | } |
| 1485 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1486 | u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1487 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1488 | struct perf_evsel *evsel; |
| 1489 | |
| 1490 | if (evlist->combined_sample_type) |
| 1491 | return evlist->combined_sample_type; |
| 1492 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1493 | evlist__for_each_entry(evlist, evsel) |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1494 | evlist->combined_sample_type |= evsel->attr.sample_type; |
| 1495 | |
| 1496 | return evlist->combined_sample_type; |
| 1497 | } |
| 1498 | |
| 1499 | u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
| 1500 | { |
| 1501 | evlist->combined_sample_type = 0; |
| 1502 | return __perf_evlist__combined_sample_type(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1503 | } |
| 1504 | |
Andi Kleen | 98df858 | 2015-07-18 08:24:47 -0700 | [diff] [blame] | 1505 | u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) |
| 1506 | { |
| 1507 | struct perf_evsel *evsel; |
| 1508 | u64 branch_type = 0; |
| 1509 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1510 | evlist__for_each_entry(evlist, evsel) |
Andi Kleen | 98df858 | 2015-07-18 08:24:47 -0700 | [diff] [blame] | 1511 | branch_type |= evsel->attr.branch_sample_type; |
| 1512 | return branch_type; |
| 1513 | } |
| 1514 | |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1515 | bool perf_evlist__valid_read_format(struct perf_evlist *evlist) |
| 1516 | { |
| 1517 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
| 1518 | u64 read_format = first->attr.read_format; |
| 1519 | u64 sample_type = first->attr.sample_type; |
| 1520 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1521 | evlist__for_each_entry(evlist, pos) { |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1522 | if (read_format != pos->attr.read_format) |
| 1523 | return false; |
| 1524 | } |
| 1525 | |
| 1526 | /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ |
| 1527 | if ((sample_type & PERF_SAMPLE_READ) && |
| 1528 | !(read_format & PERF_FORMAT_ID)) { |
| 1529 | return false; |
| 1530 | } |
| 1531 | |
| 1532 | return true; |
| 1533 | } |
| 1534 | |
| 1535 | u64 perf_evlist__read_format(struct perf_evlist *evlist) |
| 1536 | { |
| 1537 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 1538 | return first->attr.read_format; |
| 1539 | } |
| 1540 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1541 | u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 81e36bff | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1542 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1543 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 81e36bff | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1544 | struct perf_sample *data; |
| 1545 | u64 sample_type; |
| 1546 | u16 size = 0; |
| 1547 | |
Arnaldo Carvalho de Melo | 81e36bff | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1548 | if (!first->attr.sample_id_all) |
| 1549 | goto out; |
| 1550 | |
| 1551 | sample_type = first->attr.sample_type; |
| 1552 | |
| 1553 | if (sample_type & PERF_SAMPLE_TID) |
| 1554 | size += sizeof(data->tid) * 2; |
| 1555 | |
| 1556 | if (sample_type & PERF_SAMPLE_TIME) |
| 1557 | size += sizeof(data->time); |
| 1558 | |
| 1559 | if (sample_type & PERF_SAMPLE_ID) |
| 1560 | size += sizeof(data->id); |
| 1561 | |
| 1562 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1563 | size += sizeof(data->stream_id); |
| 1564 | |
| 1565 | if (sample_type & PERF_SAMPLE_CPU) |
| 1566 | size += sizeof(data->cpu) * 2; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1567 | |
| 1568 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1569 | size += sizeof(data->id); |
Arnaldo Carvalho de Melo | 81e36bff | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1570 | out: |
| 1571 | return size; |
| 1572 | } |
| 1573 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1574 | bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1575 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1576 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1577 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1578 | evlist__for_each_entry_continue(evlist, pos) { |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1579 | if (first->attr.sample_id_all != pos->attr.sample_id_all) |
| 1580 | return false; |
| 1581 | } |
| 1582 | |
| 1583 | return true; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1586 | bool perf_evlist__sample_id_all(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1587 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1588 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1589 | return first->attr.sample_id_all; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1590 | } |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1591 | |
| 1592 | void perf_evlist__set_selected(struct perf_evlist *evlist, |
| 1593 | struct perf_evsel *evsel) |
| 1594 | { |
| 1595 | evlist->selected = evsel; |
| 1596 | } |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1597 | |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1598 | void perf_evlist__close(struct perf_evlist *evlist) |
| 1599 | { |
| 1600 | struct perf_evsel *evsel; |
| 1601 | int ncpus = cpu_map__nr(evlist->cpus); |
| 1602 | int nthreads = thread_map__nr(evlist->threads); |
Stephane Eranian | 8ad9219 | 2014-01-17 16:34:06 +0100 | [diff] [blame] | 1603 | int n; |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1604 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1605 | evlist__for_each_entry_reverse(evlist, evsel) { |
Stephane Eranian | 8ad9219 | 2014-01-17 16:34:06 +0100 | [diff] [blame] | 1606 | n = evsel->cpus ? evsel->cpus->nr : ncpus; |
| 1607 | perf_evsel__close(evsel, n, nthreads); |
| 1608 | } |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1609 | } |
| 1610 | |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1611 | static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) |
| 1612 | { |
Adrian Hunter | 8c0498b | 2015-09-08 10:59:00 +0300 | [diff] [blame] | 1613 | struct cpu_map *cpus; |
| 1614 | struct thread_map *threads; |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1615 | int err = -ENOMEM; |
| 1616 | |
| 1617 | /* |
| 1618 | * Try reading /sys/devices/system/cpu/online to get |
| 1619 | * an all cpus map. |
| 1620 | * |
| 1621 | * FIXME: -ENOMEM is the best we can do here, the cpu_map |
| 1622 | * code needs an overhaul to properly forward the |
| 1623 | * error, and we may not want to do that fallback to a |
| 1624 | * default cpu identity map :-\ |
| 1625 | */ |
Adrian Hunter | 8c0498b | 2015-09-08 10:59:00 +0300 | [diff] [blame] | 1626 | cpus = cpu_map__new(NULL); |
| 1627 | if (!cpus) |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1628 | goto out; |
| 1629 | |
Adrian Hunter | 8c0498b | 2015-09-08 10:59:00 +0300 | [diff] [blame] | 1630 | threads = thread_map__new_dummy(); |
| 1631 | if (!threads) |
| 1632 | goto out_put; |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1633 | |
Adrian Hunter | 8c0498b | 2015-09-08 10:59:00 +0300 | [diff] [blame] | 1634 | perf_evlist__set_maps(evlist, cpus, threads); |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1635 | out: |
| 1636 | return err; |
Adrian Hunter | 8c0498b | 2015-09-08 10:59:00 +0300 | [diff] [blame] | 1637 | out_put: |
| 1638 | cpu_map__put(cpus); |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1639 | goto out; |
| 1640 | } |
| 1641 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1642 | int perf_evlist__open(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1643 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1644 | struct perf_evsel *evsel; |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1645 | int err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1646 | |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1647 | /* |
| 1648 | * Default: one fd per CPU, all threads, aka systemwide |
| 1649 | * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL |
| 1650 | */ |
| 1651 | if (evlist->threads == NULL && evlist->cpus == NULL) { |
| 1652 | err = perf_evlist__create_syswide_maps(evlist); |
| 1653 | if (err < 0) |
| 1654 | goto out_err; |
| 1655 | } |
| 1656 | |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 1657 | perf_evlist__update_id_pos(evlist); |
| 1658 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1659 | evlist__for_each_entry(evlist, evsel) { |
Adrian Hunter | 23df7f7 | 2016-01-07 10:13:59 +0100 | [diff] [blame] | 1660 | err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1661 | if (err < 0) |
| 1662 | goto out_err; |
| 1663 | } |
| 1664 | |
| 1665 | return 0; |
| 1666 | out_err: |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1667 | perf_evlist__close(evlist); |
Namhyung Kim | 41c21a6 | 2012-02-23 12:13:36 +0900 | [diff] [blame] | 1668 | errno = -err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1669 | return err; |
| 1670 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1671 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1672 | int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 1673 | const char *argv[], bool pipe_output, |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1674 | void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1675 | { |
| 1676 | int child_ready_pipe[2], go_pipe[2]; |
| 1677 | char bf; |
| 1678 | |
| 1679 | if (pipe(child_ready_pipe) < 0) { |
| 1680 | perror("failed to create 'ready' pipe"); |
| 1681 | return -1; |
| 1682 | } |
| 1683 | |
| 1684 | if (pipe(go_pipe) < 0) { |
| 1685 | perror("failed to create 'go' pipe"); |
| 1686 | goto out_close_ready_pipe; |
| 1687 | } |
| 1688 | |
| 1689 | evlist->workload.pid = fork(); |
| 1690 | if (evlist->workload.pid < 0) { |
| 1691 | perror("failed to fork"); |
| 1692 | goto out_close_pipes; |
| 1693 | } |
| 1694 | |
| 1695 | if (!evlist->workload.pid) { |
Arnaldo Carvalho de Melo | 5f1c422 | 2014-07-28 12:39:50 -0300 | [diff] [blame] | 1696 | int ret; |
| 1697 | |
Namhyung Kim | 119fa3c | 2013-03-11 16:43:16 +0900 | [diff] [blame] | 1698 | if (pipe_output) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1699 | dup2(2, 1); |
| 1700 | |
David Ahern | 0817df0 | 2013-05-25 17:50:39 -0600 | [diff] [blame] | 1701 | signal(SIGTERM, SIG_DFL); |
| 1702 | |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1703 | close(child_ready_pipe[0]); |
| 1704 | close(go_pipe[1]); |
| 1705 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); |
| 1706 | |
| 1707 | /* |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1708 | * Tell the parent we're ready to go |
| 1709 | */ |
| 1710 | close(child_ready_pipe[1]); |
| 1711 | |
| 1712 | /* |
| 1713 | * Wait until the parent tells us to go. |
| 1714 | */ |
Arnaldo Carvalho de Melo | 5f1c422 | 2014-07-28 12:39:50 -0300 | [diff] [blame] | 1715 | ret = read(go_pipe[0], &bf, 1); |
| 1716 | /* |
| 1717 | * The parent will ask for the execvp() to be performed by |
| 1718 | * writing exactly one byte, in workload.cork_fd, usually via |
| 1719 | * perf_evlist__start_workload(). |
| 1720 | * |
Arnaldo Carvalho de Melo | 20f86fc | 2015-02-03 13:29:05 -0300 | [diff] [blame] | 1721 | * For cancelling the workload without actually running it, |
Arnaldo Carvalho de Melo | 5f1c422 | 2014-07-28 12:39:50 -0300 | [diff] [blame] | 1722 | * the parent will just close workload.cork_fd, without writing |
| 1723 | * anything, i.e. read will return zero and we just exit() |
| 1724 | * here. |
| 1725 | */ |
| 1726 | if (ret != 1) { |
| 1727 | if (ret == -1) |
| 1728 | perror("unable to read pipe"); |
| 1729 | exit(ret); |
| 1730 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1731 | |
| 1732 | execvp(argv[0], (char **)argv); |
| 1733 | |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1734 | if (exec_error) { |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 1735 | union sigval val; |
| 1736 | |
| 1737 | val.sival_int = errno; |
| 1738 | if (sigqueue(getppid(), SIGUSR1, val)) |
| 1739 | perror(argv[0]); |
| 1740 | } else |
| 1741 | perror(argv[0]); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1742 | exit(-1); |
| 1743 | } |
| 1744 | |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1745 | if (exec_error) { |
| 1746 | struct sigaction act = { |
| 1747 | .sa_flags = SA_SIGINFO, |
| 1748 | .sa_sigaction = exec_error, |
| 1749 | }; |
| 1750 | sigaction(SIGUSR1, &act, NULL); |
| 1751 | } |
| 1752 | |
Arnaldo Carvalho de Melo | 1aaf63b | 2014-10-10 14:29:49 -0300 | [diff] [blame] | 1753 | if (target__none(target)) { |
| 1754 | if (evlist->threads == NULL) { |
| 1755 | fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", |
| 1756 | __func__, __LINE__); |
| 1757 | goto out_close_pipes; |
| 1758 | } |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 1759 | thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); |
Arnaldo Carvalho de Melo | 1aaf63b | 2014-10-10 14:29:49 -0300 | [diff] [blame] | 1760 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1761 | |
| 1762 | close(child_ready_pipe[1]); |
| 1763 | close(go_pipe[0]); |
| 1764 | /* |
| 1765 | * wait for child to settle |
| 1766 | */ |
| 1767 | if (read(child_ready_pipe[0], &bf, 1) == -1) { |
| 1768 | perror("unable to read pipe"); |
| 1769 | goto out_close_pipes; |
| 1770 | } |
| 1771 | |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1772 | fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1773 | evlist->workload.cork_fd = go_pipe[1]; |
| 1774 | close(child_ready_pipe[0]); |
| 1775 | return 0; |
| 1776 | |
| 1777 | out_close_pipes: |
| 1778 | close(go_pipe[0]); |
| 1779 | close(go_pipe[1]); |
| 1780 | out_close_ready_pipe: |
| 1781 | close(child_ready_pipe[0]); |
| 1782 | close(child_ready_pipe[1]); |
| 1783 | return -1; |
| 1784 | } |
| 1785 | |
| 1786 | int perf_evlist__start_workload(struct perf_evlist *evlist) |
| 1787 | { |
| 1788 | if (evlist->workload.cork_fd > 0) { |
David Ahern | b382440 | 2013-07-02 13:27:21 -0600 | [diff] [blame] | 1789 | char bf = 0; |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1790 | int ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1791 | /* |
| 1792 | * Remove the cork, let it rip! |
| 1793 | */ |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1794 | ret = write(evlist->workload.cork_fd, &bf, 1); |
| 1795 | if (ret < 0) |
| 1796 | perror("enable to write to pipe"); |
| 1797 | |
| 1798 | close(evlist->workload.cork_fd); |
| 1799 | return ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | return 0; |
| 1803 | } |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1804 | |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1805 | int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1806 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1807 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1808 | struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); |
| 1809 | |
| 1810 | if (!evsel) |
| 1811 | return -EFAULT; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1812 | return perf_evsel__parse_sample(evsel, event, sample); |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1813 | } |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1814 | |
| 1815 | size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) |
| 1816 | { |
| 1817 | struct perf_evsel *evsel; |
| 1818 | size_t printed = 0; |
| 1819 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1820 | evlist__for_each_entry(evlist, evsel) { |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1821 | printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", |
| 1822 | perf_evsel__name(evsel)); |
| 1823 | } |
| 1824 | |
Davidlohr Bueso | b222213 | 2013-11-12 22:24:24 -0800 | [diff] [blame] | 1825 | return printed + fprintf(fp, "\n"); |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1826 | } |
Arnaldo Carvalho de Melo | 6ef068c | 2013-10-17 12:07:58 -0300 | [diff] [blame] | 1827 | |
Arnaldo Carvalho de Melo | d9aade7f | 2016-02-18 13:34:09 -0300 | [diff] [blame] | 1828 | int perf_evlist__strerror_open(struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1829 | int err, char *buf, size_t size) |
| 1830 | { |
| 1831 | int printed, value; |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 1832 | char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1833 | |
| 1834 | switch (err) { |
| 1835 | case EACCES: |
| 1836 | case EPERM: |
| 1837 | printed = scnprintf(buf, size, |
| 1838 | "Error:\t%s.\n" |
| 1839 | "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); |
| 1840 | |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 1841 | value = perf_event_paranoid(); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1842 | |
| 1843 | printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); |
| 1844 | |
| 1845 | if (value >= 2) { |
| 1846 | printed += scnprintf(buf + printed, size - printed, |
| 1847 | "For your workloads it needs to be <= 1\nHint:\t"); |
| 1848 | } |
| 1849 | printed += scnprintf(buf + printed, size - printed, |
Arnaldo Carvalho de Melo | 5229e36 | 2014-06-10 17:18:54 -0300 | [diff] [blame] | 1850 | "For system wide tracing it needs to be set to -1.\n"); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1851 | |
| 1852 | printed += scnprintf(buf + printed, size - printed, |
Arnaldo Carvalho de Melo | 5229e36 | 2014-06-10 17:18:54 -0300 | [diff] [blame] | 1853 | "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" |
| 1854 | "Hint:\tThe current value is %d.", value); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1855 | break; |
Arnaldo Carvalho de Melo | d9aade7f | 2016-02-18 13:34:09 -0300 | [diff] [blame] | 1856 | case EINVAL: { |
| 1857 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 1858 | int max_freq; |
| 1859 | |
| 1860 | if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) |
| 1861 | goto out_default; |
| 1862 | |
| 1863 | if (first->attr.sample_freq < (u64)max_freq) |
| 1864 | goto out_default; |
| 1865 | |
| 1866 | printed = scnprintf(buf, size, |
| 1867 | "Error:\t%s.\n" |
| 1868 | "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" |
| 1869 | "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", |
| 1870 | emsg, max_freq, first->attr.sample_freq); |
| 1871 | break; |
| 1872 | } |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1873 | default: |
Arnaldo Carvalho de Melo | d9aade7f | 2016-02-18 13:34:09 -0300 | [diff] [blame] | 1874 | out_default: |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1875 | scnprintf(buf, size, "%s", emsg); |
| 1876 | break; |
| 1877 | } |
| 1878 | |
| 1879 | return 0; |
| 1880 | } |
Adrian Hunter | a025e4f | 2013-12-11 14:36:35 +0200 | [diff] [blame] | 1881 | |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 1882 | int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) |
| 1883 | { |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 1884 | char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); |
Arnaldo Carvalho de Melo | e965bea | 2014-12-12 16:25:33 -0300 | [diff] [blame] | 1885 | int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 1886 | |
| 1887 | switch (err) { |
| 1888 | case EPERM: |
Arnaldo Carvalho de Melo | e5d4a29 | 2014-12-12 15:59:51 -0300 | [diff] [blame] | 1889 | sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); |
Arnaldo Carvalho de Melo | e965bea | 2014-12-12 16:25:33 -0300 | [diff] [blame] | 1890 | printed += scnprintf(buf + printed, size - printed, |
| 1891 | "Error:\t%s.\n" |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 1892 | "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" |
Arnaldo Carvalho de Melo | e965bea | 2014-12-12 16:25:33 -0300 | [diff] [blame] | 1893 | "Hint:\tTried using %zd kB.\n", |
Arnaldo Carvalho de Melo | e5d4a29 | 2014-12-12 15:59:51 -0300 | [diff] [blame] | 1894 | emsg, pages_max_per_user, pages_attempted); |
Arnaldo Carvalho de Melo | e965bea | 2014-12-12 16:25:33 -0300 | [diff] [blame] | 1895 | |
| 1896 | if (pages_attempted >= pages_max_per_user) { |
| 1897 | printed += scnprintf(buf + printed, size - printed, |
| 1898 | "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", |
| 1899 | pages_max_per_user + pages_attempted); |
| 1900 | } |
| 1901 | |
| 1902 | printed += scnprintf(buf + printed, size - printed, |
| 1903 | "Hint:\tTry using a smaller -m/--mmap-pages value."); |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 1904 | break; |
| 1905 | default: |
| 1906 | scnprintf(buf, size, "%s", emsg); |
| 1907 | break; |
| 1908 | } |
| 1909 | |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
Adrian Hunter | a025e4f | 2013-12-11 14:36:35 +0200 | [diff] [blame] | 1913 | void perf_evlist__to_front(struct perf_evlist *evlist, |
| 1914 | struct perf_evsel *move_evsel) |
| 1915 | { |
| 1916 | struct perf_evsel *evsel, *n; |
| 1917 | LIST_HEAD(move); |
| 1918 | |
| 1919 | if (move_evsel == perf_evlist__first(evlist)) |
| 1920 | return; |
| 1921 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1922 | evlist__for_each_entry_safe(evlist, n, evsel) { |
Adrian Hunter | a025e4f | 2013-12-11 14:36:35 +0200 | [diff] [blame] | 1923 | if (evsel->leader == move_evsel->leader) |
| 1924 | list_move_tail(&evsel->node, &move); |
| 1925 | } |
| 1926 | |
| 1927 | list_splice(&move, &evlist->entries); |
| 1928 | } |
Adrian Hunter | 60b0896 | 2014-07-31 09:00:52 +0300 | [diff] [blame] | 1929 | |
| 1930 | void perf_evlist__set_tracking_event(struct perf_evlist *evlist, |
| 1931 | struct perf_evsel *tracking_evsel) |
| 1932 | { |
| 1933 | struct perf_evsel *evsel; |
| 1934 | |
| 1935 | if (tracking_evsel->tracking) |
| 1936 | return; |
| 1937 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1938 | evlist__for_each_entry(evlist, evsel) { |
Adrian Hunter | 60b0896 | 2014-07-31 09:00:52 +0300 | [diff] [blame] | 1939 | if (evsel != tracking_evsel) |
| 1940 | evsel->tracking = false; |
| 1941 | } |
| 1942 | |
| 1943 | tracking_evsel->tracking = true; |
| 1944 | } |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1945 | |
| 1946 | struct perf_evsel * |
| 1947 | perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, |
| 1948 | const char *str) |
| 1949 | { |
| 1950 | struct perf_evsel *evsel; |
| 1951 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1952 | evlist__for_each_entry(evlist, evsel) { |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1953 | if (!evsel->name) |
| 1954 | continue; |
| 1955 | if (strcmp(str, evsel->name) == 0) |
| 1956 | return evsel; |
| 1957 | } |
| 1958 | |
| 1959 | return NULL; |
| 1960 | } |
Wang Nan | 54cc54d | 2016-07-14 08:34:42 +0000 | [diff] [blame] | 1961 | |
| 1962 | void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, |
| 1963 | enum bkw_mmap_state state) |
| 1964 | { |
| 1965 | enum bkw_mmap_state old_state = evlist->bkw_mmap_state; |
| 1966 | enum action { |
| 1967 | NONE, |
| 1968 | PAUSE, |
| 1969 | RESUME, |
| 1970 | } action = NONE; |
| 1971 | |
| 1972 | if (!evlist->backward_mmap) |
| 1973 | return; |
| 1974 | |
| 1975 | switch (old_state) { |
| 1976 | case BKW_MMAP_NOTREADY: { |
| 1977 | if (state != BKW_MMAP_RUNNING) |
| 1978 | goto state_err;; |
| 1979 | break; |
| 1980 | } |
| 1981 | case BKW_MMAP_RUNNING: { |
| 1982 | if (state != BKW_MMAP_DATA_PENDING) |
| 1983 | goto state_err; |
| 1984 | action = PAUSE; |
| 1985 | break; |
| 1986 | } |
| 1987 | case BKW_MMAP_DATA_PENDING: { |
| 1988 | if (state != BKW_MMAP_EMPTY) |
| 1989 | goto state_err; |
| 1990 | break; |
| 1991 | } |
| 1992 | case BKW_MMAP_EMPTY: { |
| 1993 | if (state != BKW_MMAP_RUNNING) |
| 1994 | goto state_err; |
| 1995 | action = RESUME; |
| 1996 | break; |
| 1997 | } |
| 1998 | default: |
| 1999 | WARN_ONCE(1, "Shouldn't get there\n"); |
| 2000 | } |
| 2001 | |
| 2002 | evlist->bkw_mmap_state = state; |
| 2003 | |
| 2004 | switch (action) { |
| 2005 | case PAUSE: |
| 2006 | perf_evlist__pause(evlist); |
| 2007 | break; |
| 2008 | case RESUME: |
| 2009 | perf_evlist__resume(evlist); |
| 2010 | break; |
| 2011 | case NONE: |
| 2012 | default: |
| 2013 | break; |
| 2014 | } |
| 2015 | |
| 2016 | state_err: |
| 2017 | return; |
| 2018 | } |