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" |
Borislav Petkov | 85c66be | 2013-02-20 16:32:30 +0100 | [diff] [blame] | 10 | #include <lk/debugfs.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" |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 18 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 19 | |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 20 | #include "parse-events.h" |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 21 | #include "parse-options.h" |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 25 | #include <linux/bitops.h> |
| 26 | #include <linux/hash.h> |
| 27 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 28 | #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] | 29 | #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] | 30 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 31 | void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, |
| 32 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 33 | { |
| 34 | int i; |
| 35 | |
| 36 | for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) |
| 37 | INIT_HLIST_HEAD(&evlist->heads[i]); |
| 38 | INIT_LIST_HEAD(&evlist->entries); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 39 | perf_evlist__set_maps(evlist, cpus, threads); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 40 | evlist->workload.pid = -1; |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 41 | } |
| 42 | |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 43 | struct perf_evlist *perf_evlist__new(void) |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 44 | { |
| 45 | struct perf_evlist *evlist = zalloc(sizeof(*evlist)); |
| 46 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 47 | if (evlist != NULL) |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 48 | perf_evlist__init(evlist, NULL, NULL); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 49 | |
| 50 | return evlist; |
| 51 | } |
| 52 | |
Jiri Olsa | b22d54b | 2013-09-01 12:36:14 +0200 | [diff] [blame] | 53 | struct perf_evlist *perf_evlist__new_default(void) |
| 54 | { |
| 55 | struct perf_evlist *evlist = perf_evlist__new(); |
| 56 | |
| 57 | if (evlist && perf_evlist__add_default(evlist)) { |
| 58 | perf_evlist__delete(evlist); |
| 59 | evlist = NULL; |
| 60 | } |
| 61 | |
| 62 | return evlist; |
| 63 | } |
| 64 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 65 | /** |
| 66 | * perf_evlist__set_id_pos - set the positions of event ids. |
| 67 | * @evlist: selected event list |
| 68 | * |
| 69 | * Events with compatible sample types all have the same id_pos |
| 70 | * and is_pos. For convenience, put a copy on evlist. |
| 71 | */ |
| 72 | void perf_evlist__set_id_pos(struct perf_evlist *evlist) |
| 73 | { |
| 74 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 75 | |
| 76 | evlist->id_pos = first->id_pos; |
| 77 | evlist->is_pos = first->is_pos; |
| 78 | } |
| 79 | |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 80 | static void perf_evlist__update_id_pos(struct perf_evlist *evlist) |
| 81 | { |
| 82 | struct perf_evsel *evsel; |
| 83 | |
| 84 | list_for_each_entry(evsel, &evlist->entries, node) |
| 85 | perf_evsel__calc_id_pos(evsel); |
| 86 | |
| 87 | perf_evlist__set_id_pos(evlist); |
| 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 90 | static void perf_evlist__purge(struct perf_evlist *evlist) |
| 91 | { |
| 92 | struct perf_evsel *pos, *n; |
| 93 | |
| 94 | list_for_each_entry_safe(pos, n, &evlist->entries, node) { |
| 95 | list_del_init(&pos->node); |
| 96 | perf_evsel__delete(pos); |
| 97 | } |
| 98 | |
| 99 | evlist->nr_entries = 0; |
| 100 | } |
| 101 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 102 | void perf_evlist__exit(struct perf_evlist *evlist) |
| 103 | { |
| 104 | free(evlist->mmap); |
| 105 | free(evlist->pollfd); |
| 106 | evlist->mmap = NULL; |
| 107 | evlist->pollfd = NULL; |
| 108 | } |
| 109 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 110 | void perf_evlist__delete(struct perf_evlist *evlist) |
| 111 | { |
| 112 | perf_evlist__purge(evlist); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 113 | perf_evlist__exit(evlist); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 114 | free(evlist); |
| 115 | } |
| 116 | |
| 117 | void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) |
| 118 | { |
| 119 | list_add_tail(&entry->node, &evlist->entries); |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 120 | if (!evlist->nr_entries++) |
| 121 | perf_evlist__set_id_pos(evlist); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 122 | } |
| 123 | |
Jiri Olsa | 0529bc1 | 2012-01-27 15:34:20 +0100 | [diff] [blame] | 124 | void perf_evlist__splice_list_tail(struct perf_evlist *evlist, |
| 125 | struct list_head *list, |
| 126 | int nr_entries) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 127 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 128 | bool set_id_pos = !evlist->nr_entries; |
| 129 | |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 130 | list_splice_tail(list, &evlist->entries); |
| 131 | evlist->nr_entries += nr_entries; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 132 | if (set_id_pos) |
| 133 | perf_evlist__set_id_pos(evlist); |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 134 | } |
| 135 | |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 136 | void __perf_evlist__set_leader(struct list_head *list) |
| 137 | { |
| 138 | struct perf_evsel *evsel, *leader; |
| 139 | |
| 140 | leader = list_entry(list->next, struct perf_evsel, node); |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 141 | evsel = list_entry(list->prev, struct perf_evsel, node); |
| 142 | |
| 143 | leader->nr_members = evsel->idx - leader->idx + 1; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 144 | |
| 145 | list_for_each_entry(evsel, list, node) { |
Stephane Eranian | 74b2133 | 2013-01-31 13:54:37 +0100 | [diff] [blame] | 146 | evsel->leader = leader; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | void perf_evlist__set_leader(struct perf_evlist *evlist) |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 151 | { |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 152 | if (evlist->nr_entries) { |
| 153 | evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 154 | __perf_evlist__set_leader(&evlist->entries); |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 155 | } |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 158 | int perf_evlist__add_default(struct perf_evlist *evlist) |
| 159 | { |
| 160 | struct perf_event_attr attr = { |
| 161 | .type = PERF_TYPE_HARDWARE, |
| 162 | .config = PERF_COUNT_HW_CPU_CYCLES, |
| 163 | }; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 164 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 165 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 166 | event_attr_init(&attr); |
| 167 | |
| 168 | evsel = perf_evsel__new(&attr, 0); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 169 | if (evsel == NULL) |
Stephane Eranian | cc2d86b | 2011-06-07 18:19:36 +0200 | [diff] [blame] | 170 | goto error; |
| 171 | |
| 172 | /* use strdup() because free(evsel) assumes name is allocated */ |
| 173 | evsel->name = strdup("cycles"); |
| 174 | if (!evsel->name) |
| 175 | goto error_free; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 176 | |
| 177 | perf_evlist__add(evlist, evsel); |
| 178 | return 0; |
Stephane Eranian | cc2d86b | 2011-06-07 18:19:36 +0200 | [diff] [blame] | 179 | error_free: |
| 180 | perf_evsel__delete(evsel); |
| 181 | error: |
| 182 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 183 | } |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 184 | |
Arnaldo Carvalho de Melo | e60fc84 | 2012-10-03 11:50:55 -0300 | [diff] [blame] | 185 | static int perf_evlist__add_attrs(struct perf_evlist *evlist, |
| 186 | struct perf_event_attr *attrs, size_t nr_attrs) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 187 | { |
| 188 | struct perf_evsel *evsel, *n; |
| 189 | LIST_HEAD(head); |
| 190 | size_t i; |
| 191 | |
| 192 | for (i = 0; i < nr_attrs; i++) { |
| 193 | evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); |
| 194 | if (evsel == NULL) |
| 195 | goto out_delete_partial_list; |
| 196 | list_add_tail(&evsel->node, &head); |
| 197 | } |
| 198 | |
| 199 | perf_evlist__splice_list_tail(evlist, &head, nr_attrs); |
| 200 | |
| 201 | return 0; |
| 202 | |
| 203 | out_delete_partial_list: |
| 204 | list_for_each_entry_safe(evsel, n, &head, node) |
| 205 | perf_evsel__delete(evsel); |
| 206 | return -1; |
| 207 | } |
| 208 | |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 209 | int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, |
| 210 | struct perf_event_attr *attrs, size_t nr_attrs) |
| 211 | { |
| 212 | size_t i; |
| 213 | |
| 214 | for (i = 0; i < nr_attrs; i++) |
| 215 | event_attr_init(attrs + i); |
| 216 | |
| 217 | return perf_evlist__add_attrs(evlist, attrs, nr_attrs); |
| 218 | } |
| 219 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 220 | struct perf_evsel * |
| 221 | 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] | 222 | { |
| 223 | struct perf_evsel *evsel; |
| 224 | |
| 225 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 226 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && |
| 227 | (int)evsel->attr.config == id) |
| 228 | return evsel; |
| 229 | } |
| 230 | |
| 231 | return NULL; |
| 232 | } |
| 233 | |
David Ahern | a2f2804 | 2013-08-28 22:29:51 -0600 | [diff] [blame] | 234 | struct perf_evsel * |
| 235 | perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, |
| 236 | const char *name) |
| 237 | { |
| 238 | struct perf_evsel *evsel; |
| 239 | |
| 240 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 241 | if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && |
| 242 | (strcmp(evsel->name, name) == 0)) |
| 243 | return evsel; |
| 244 | } |
| 245 | |
| 246 | return NULL; |
| 247 | } |
| 248 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 249 | int perf_evlist__add_newtp(struct perf_evlist *evlist, |
| 250 | const char *sys, const char *name, void *handler) |
| 251 | { |
| 252 | struct perf_evsel *evsel; |
| 253 | |
| 254 | evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); |
| 255 | if (evsel == NULL) |
| 256 | return -1; |
| 257 | |
| 258 | evsel->handler.func = handler; |
| 259 | perf_evlist__add(evlist, evsel); |
| 260 | return 0; |
| 261 | } |
| 262 | |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 263 | void perf_evlist__disable(struct perf_evlist *evlist) |
| 264 | { |
| 265 | int cpu, thread; |
| 266 | struct perf_evsel *pos; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 267 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 268 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 269 | |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 270 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 271 | list_for_each_entry(pos, &evlist->entries, node) { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 272 | if (!perf_evsel__is_group_leader(pos) || !pos->fd) |
Jiri Olsa | 3fe4430d | 2012-11-12 18:34:03 +0100 | [diff] [blame] | 273 | continue; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 274 | for (thread = 0; thread < nr_threads; thread++) |
Namhyung Kim | 55da800 | 2012-05-31 14:51:46 +0900 | [diff] [blame] | 275 | ioctl(FD(pos, cpu, thread), |
| 276 | PERF_EVENT_IOC_DISABLE, 0); |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 281 | void perf_evlist__enable(struct perf_evlist *evlist) |
| 282 | { |
| 283 | int cpu, thread; |
| 284 | struct perf_evsel *pos; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 285 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 286 | int nr_threads = thread_map__nr(evlist->threads); |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 287 | |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 288 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 289 | list_for_each_entry(pos, &evlist->entries, node) { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 290 | if (!perf_evsel__is_group_leader(pos) || !pos->fd) |
Jiri Olsa | 3fe4430d | 2012-11-12 18:34:03 +0100 | [diff] [blame] | 291 | continue; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 292 | for (thread = 0; thread < nr_threads; thread++) |
Namhyung Kim | 55da800 | 2012-05-31 14:51:46 +0900 | [diff] [blame] | 293 | ioctl(FD(pos, cpu, thread), |
| 294 | PERF_EVENT_IOC_ENABLE, 0); |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 299 | int perf_evlist__disable_event(struct perf_evlist *evlist, |
| 300 | struct perf_evsel *evsel) |
| 301 | { |
| 302 | int cpu, thread, err; |
| 303 | |
| 304 | if (!evsel->fd) |
| 305 | return 0; |
| 306 | |
| 307 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 308 | for (thread = 0; thread < evlist->threads->nr; thread++) { |
| 309 | err = ioctl(FD(evsel, cpu, thread), |
| 310 | PERF_EVENT_IOC_DISABLE, 0); |
| 311 | if (err) |
| 312 | return err; |
| 313 | } |
| 314 | } |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | int perf_evlist__enable_event(struct perf_evlist *evlist, |
| 319 | struct perf_evsel *evsel) |
| 320 | { |
| 321 | int cpu, thread, err; |
| 322 | |
| 323 | if (!evsel->fd) |
| 324 | return -EINVAL; |
| 325 | |
| 326 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 327 | for (thread = 0; thread < evlist->threads->nr; thread++) { |
| 328 | err = ioctl(FD(evsel, cpu, thread), |
| 329 | PERF_EVENT_IOC_ENABLE, 0); |
| 330 | if (err) |
| 331 | return err; |
| 332 | } |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
Arnaldo Carvalho de Melo | 806fb63 | 2011-11-29 08:05:52 -0200 | [diff] [blame] | 337 | static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 338 | { |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 339 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 340 | int nr_threads = thread_map__nr(evlist->threads); |
| 341 | int nfds = nr_cpus * nr_threads * evlist->nr_entries; |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 342 | evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); |
| 343 | return evlist->pollfd != NULL ? 0 : -ENOMEM; |
| 344 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 345 | |
| 346 | void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) |
| 347 | { |
| 348 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 349 | evlist->pollfd[evlist->nr_fds].fd = fd; |
| 350 | evlist->pollfd[evlist->nr_fds].events = POLLIN; |
| 351 | evlist->nr_fds++; |
| 352 | } |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 353 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 354 | static void perf_evlist__id_hash(struct perf_evlist *evlist, |
| 355 | struct perf_evsel *evsel, |
| 356 | int cpu, int thread, u64 id) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 357 | { |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 358 | int hash; |
| 359 | struct perf_sample_id *sid = SID(evsel, cpu, thread); |
| 360 | |
| 361 | sid->id = id; |
| 362 | sid->evsel = evsel; |
| 363 | hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); |
| 364 | hlist_add_head(&sid->node, &evlist->heads[hash]); |
| 365 | } |
| 366 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 367 | void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, |
| 368 | int cpu, int thread, u64 id) |
| 369 | { |
| 370 | perf_evlist__id_hash(evlist, evsel, cpu, thread, id); |
| 371 | evsel->id[evsel->ids++] = id; |
| 372 | } |
| 373 | |
| 374 | static int perf_evlist__id_add_fd(struct perf_evlist *evlist, |
| 375 | struct perf_evsel *evsel, |
| 376 | int cpu, int thread, int fd) |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 377 | { |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 378 | u64 read_data[4] = { 0, }; |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 379 | int id_idx = 1; /* The first entry is the counter value */ |
Jiri Olsa | e2b5abe | 2012-04-04 19:32:27 +0200 | [diff] [blame] | 380 | u64 id; |
| 381 | int ret; |
| 382 | |
| 383 | ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); |
| 384 | if (!ret) |
| 385 | goto add; |
| 386 | |
| 387 | if (errno != ENOTTY) |
| 388 | return -1; |
| 389 | |
| 390 | /* 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] | 391 | |
Jiri Olsa | c4861af | 2012-10-12 13:02:21 +0200 | [diff] [blame] | 392 | /* |
| 393 | * This way does not work with group format read, so bail |
| 394 | * out in that case. |
| 395 | */ |
| 396 | if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) |
| 397 | return -1; |
| 398 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 399 | if (!(evsel->attr.read_format & PERF_FORMAT_ID) || |
| 400 | read(fd, &read_data, sizeof(read_data)) == -1) |
| 401 | return -1; |
| 402 | |
| 403 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 404 | ++id_idx; |
| 405 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 406 | ++id_idx; |
| 407 | |
Jiri Olsa | e2b5abe | 2012-04-04 19:32:27 +0200 | [diff] [blame] | 408 | id = read_data[id_idx]; |
| 409 | |
| 410 | add: |
| 411 | perf_evlist__id_add(evlist, evsel, cpu, thread, id); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
Jiri Olsa | 932a359 | 2012-10-11 14:10:35 +0200 | [diff] [blame] | 415 | 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] | 416 | { |
| 417 | struct hlist_head *head; |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 418 | struct perf_sample_id *sid; |
| 419 | int hash; |
| 420 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 421 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
| 422 | head = &evlist->heads[hash]; |
| 423 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 424 | hlist_for_each_entry(sid, head, node) |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 425 | if (sid->id == id) |
Jiri Olsa | 932a359 | 2012-10-11 14:10:35 +0200 | [diff] [blame] | 426 | return sid; |
| 427 | |
| 428 | return NULL; |
| 429 | } |
| 430 | |
| 431 | struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) |
| 432 | { |
| 433 | struct perf_sample_id *sid; |
| 434 | |
| 435 | if (evlist->nr_entries == 1) |
| 436 | return perf_evlist__first(evlist); |
| 437 | |
| 438 | sid = perf_evlist__id2sid(evlist, id); |
| 439 | if (sid) |
| 440 | return sid->evsel; |
Namhyung Kim | 30e68bc | 2012-02-20 10:47:26 +0900 | [diff] [blame] | 441 | |
| 442 | if (!perf_evlist__sample_id_all(evlist)) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 443 | return perf_evlist__first(evlist); |
Namhyung Kim | 30e68bc | 2012-02-20 10:47:26 +0900 | [diff] [blame] | 444 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 445 | return NULL; |
| 446 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 447 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 448 | static int perf_evlist__event2id(struct perf_evlist *evlist, |
| 449 | union perf_event *event, u64 *id) |
| 450 | { |
| 451 | const u64 *array = event->sample.array; |
| 452 | ssize_t n; |
| 453 | |
| 454 | n = (event->header.size - sizeof(event->header)) >> 3; |
| 455 | |
| 456 | if (event->header.type == PERF_RECORD_SAMPLE) { |
| 457 | if (evlist->id_pos >= n) |
| 458 | return -1; |
| 459 | *id = array[evlist->id_pos]; |
| 460 | } else { |
| 461 | if (evlist->is_pos > n) |
| 462 | return -1; |
| 463 | n -= evlist->is_pos; |
| 464 | *id = array[n]; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, |
| 470 | union perf_event *event) |
| 471 | { |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 472 | struct perf_evsel *first = perf_evlist__first(evlist); |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 473 | struct hlist_head *head; |
| 474 | struct perf_sample_id *sid; |
| 475 | int hash; |
| 476 | u64 id; |
| 477 | |
| 478 | if (evlist->nr_entries == 1) |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 479 | return first; |
| 480 | |
| 481 | if (!first->attr.sample_id_all && |
| 482 | event->header.type != PERF_RECORD_SAMPLE) |
| 483 | return first; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 484 | |
| 485 | if (perf_evlist__event2id(evlist, event, &id)) |
| 486 | return NULL; |
| 487 | |
| 488 | /* Synthesized events have an id of zero */ |
| 489 | if (!id) |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 490 | return first; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 491 | |
| 492 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
| 493 | head = &evlist->heads[hash]; |
| 494 | |
| 495 | hlist_for_each_entry(sid, head, node) { |
| 496 | if (sid->id == id) |
| 497 | return sid->evsel; |
| 498 | } |
| 499 | return NULL; |
| 500 | } |
| 501 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 502 | union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 503 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 504 | struct perf_mmap *md = &evlist->mmap[idx]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 505 | unsigned int head = perf_mmap__read_head(md); |
| 506 | unsigned int old = md->prev; |
| 507 | unsigned char *data = md->base + page_size; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 508 | union perf_event *event = NULL; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 509 | |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 510 | if (evlist->overwrite) { |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 511 | /* |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 512 | * If we're further behind than half the buffer, there's a chance |
| 513 | * the writer will bite our tail and mess up the samples under us. |
| 514 | * |
| 515 | * If we somehow ended up ahead of the head, we got messed up. |
| 516 | * |
| 517 | * In either case, truncate and restart at head. |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 518 | */ |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 519 | int diff = head - old; |
| 520 | if (diff > md->mask / 2 || diff < 0) { |
| 521 | fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); |
| 522 | |
| 523 | /* |
| 524 | * head points to a known good entry, start there. |
| 525 | */ |
| 526 | old = head; |
| 527 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | if (old != head) { |
| 531 | size_t size; |
| 532 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 533 | event = (union perf_event *)&data[old & md->mask]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 534 | size = event->header.size; |
| 535 | |
| 536 | /* |
| 537 | * Event straddles the mmap boundary -- header should always |
| 538 | * be inside due to u64 alignment of output. |
| 539 | */ |
| 540 | if ((old & md->mask) + size != ((old + size) & md->mask)) { |
| 541 | unsigned int offset = old; |
| 542 | unsigned int len = min(sizeof(*event), size), cpy; |
Jiri Olsa | a65cb4b | 2013-10-02 15:46:39 +0200 | [diff] [blame] | 543 | void *dst = md->event_copy; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 544 | |
| 545 | do { |
| 546 | cpy = min(md->mask + 1 - (offset & md->mask), len); |
| 547 | memcpy(dst, &data[offset & md->mask], cpy); |
| 548 | offset += cpy; |
| 549 | dst += cpy; |
| 550 | len -= cpy; |
| 551 | } while (len); |
| 552 | |
Jiri Olsa | a65cb4b | 2013-10-02 15:46:39 +0200 | [diff] [blame] | 553 | event = (union perf_event *) md->event_copy; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | old += size; |
| 557 | } |
| 558 | |
| 559 | md->prev = old; |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 560 | |
| 561 | if (!evlist->overwrite) |
| 562 | perf_mmap__write_tail(md, old); |
| 563 | |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 564 | return event; |
| 565 | } |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 566 | |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 567 | static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) |
| 568 | { |
| 569 | if (evlist->mmap[idx].base != NULL) { |
| 570 | munmap(evlist->mmap[idx].base, evlist->mmap_len); |
| 571 | evlist->mmap[idx].base = NULL; |
| 572 | } |
| 573 | } |
| 574 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 575 | void perf_evlist__munmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 576 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 577 | int i; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 578 | |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 579 | for (i = 0; i < evlist->nr_mmaps; i++) |
| 580 | __perf_evlist__munmap(evlist, i); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 581 | |
| 582 | free(evlist->mmap); |
| 583 | evlist->mmap = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 584 | } |
| 585 | |
Arnaldo Carvalho de Melo | 806fb63 | 2011-11-29 08:05:52 -0200 | [diff] [blame] | 586 | static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 587 | { |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 588 | evlist->nr_mmaps = cpu_map__nr(evlist->cpus); |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 589 | if (cpu_map__empty(evlist->cpus)) |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 590 | evlist->nr_mmaps = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 591 | evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 592 | return evlist->mmap != NULL ? 0 : -ENOMEM; |
| 593 | } |
| 594 | |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 595 | static int __perf_evlist__mmap(struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 596 | int idx, int prot, int mask, int fd) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 597 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 598 | evlist->mmap[idx].prev = 0; |
| 599 | evlist->mmap[idx].mask = mask; |
| 600 | evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 601 | MAP_SHARED, fd, 0); |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 602 | if (evlist->mmap[idx].base == MAP_FAILED) { |
| 603 | evlist->mmap[idx].base = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 604 | return -1; |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 605 | } |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 606 | |
| 607 | perf_evlist__add_pollfd(evlist, fd); |
| 608 | return 0; |
| 609 | } |
| 610 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 611 | static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) |
| 612 | { |
| 613 | struct perf_evsel *evsel; |
| 614 | int cpu, thread; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 615 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 616 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 617 | |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 618 | pr_debug2("perf event ring buffer mmapped per cpu\n"); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 619 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 620 | int output = -1; |
| 621 | |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 622 | for (thread = 0; thread < nr_threads; thread++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 623 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 624 | int fd = FD(evsel, cpu, thread); |
| 625 | |
| 626 | if (output == -1) { |
| 627 | output = fd; |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 628 | if (__perf_evlist__mmap(evlist, cpu, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 629 | prot, mask, output) < 0) |
| 630 | goto out_unmap; |
| 631 | } else { |
| 632 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) |
| 633 | goto out_unmap; |
| 634 | } |
| 635 | |
| 636 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 637 | perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) |
| 638 | goto out_unmap; |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | |
| 645 | out_unmap: |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 646 | for (cpu = 0; cpu < nr_cpus; cpu++) |
| 647 | __perf_evlist__munmap(evlist, cpu); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 648 | return -1; |
| 649 | } |
| 650 | |
| 651 | static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) |
| 652 | { |
| 653 | struct perf_evsel *evsel; |
| 654 | int thread; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 655 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 656 | |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 657 | pr_debug2("perf event ring buffer mmapped per thread\n"); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 658 | for (thread = 0; thread < nr_threads; thread++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 659 | int output = -1; |
| 660 | |
| 661 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 662 | int fd = FD(evsel, 0, thread); |
| 663 | |
| 664 | if (output == -1) { |
| 665 | output = fd; |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 666 | if (__perf_evlist__mmap(evlist, thread, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 667 | prot, mask, output) < 0) |
| 668 | goto out_unmap; |
| 669 | } else { |
| 670 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) |
| 671 | goto out_unmap; |
| 672 | } |
| 673 | |
| 674 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 675 | perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) |
| 676 | goto out_unmap; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | return 0; |
| 681 | |
| 682 | out_unmap: |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 683 | for (thread = 0; thread < nr_threads; thread++) |
| 684 | __perf_evlist__munmap(evlist, thread); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 685 | return -1; |
| 686 | } |
| 687 | |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 688 | static size_t perf_evlist__mmap_size(unsigned long pages) |
| 689 | { |
| 690 | /* 512 kiB: default amount of unprivileged mlocked memory */ |
| 691 | if (pages == UINT_MAX) |
| 692 | pages = (512 * 1024) / page_size; |
| 693 | else if (!is_power_of_2(pages)) |
| 694 | return 0; |
| 695 | |
| 696 | return (pages + 1) * page_size; |
| 697 | } |
| 698 | |
| 699 | int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, |
| 700 | int unset __maybe_unused) |
| 701 | { |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 702 | unsigned int pages, val, *mmap_pages = opt->value; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 703 | size_t size; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 704 | static struct parse_tag tags[] = { |
| 705 | { .tag = 'B', .mult = 1 }, |
| 706 | { .tag = 'K', .mult = 1 << 10 }, |
| 707 | { .tag = 'M', .mult = 1 << 20 }, |
| 708 | { .tag = 'G', .mult = 1 << 30 }, |
| 709 | { .tag = 0 }, |
| 710 | }; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 711 | |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 712 | val = parse_tag_value(str, tags); |
| 713 | if (val != (unsigned int) -1) { |
| 714 | /* we got file size value */ |
| 715 | pages = PERF_ALIGN(val, page_size) / page_size; |
| 716 | if (!is_power_of_2(pages)) { |
| 717 | pages = next_pow2(pages); |
| 718 | pr_info("rounding mmap pages size to %u (%u pages)\n", |
| 719 | pages * page_size, pages); |
| 720 | } |
| 721 | } else { |
| 722 | /* we got pages count value */ |
| 723 | char *eptr; |
| 724 | pages = strtoul(str, &eptr, 10); |
| 725 | if (*eptr != '\0') { |
| 726 | pr_err("failed to parse --mmap_pages/-m value\n"); |
| 727 | return -1; |
| 728 | } |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | size = perf_evlist__mmap_size(pages); |
| 732 | if (!size) { |
| 733 | pr_err("--mmap_pages/-m value must be a power of two."); |
| 734 | return -1; |
| 735 | } |
| 736 | |
| 737 | *mmap_pages = pages; |
| 738 | return 0; |
| 739 | } |
| 740 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 741 | /** perf_evlist__mmap - Create per cpu maps to receive events |
| 742 | * |
| 743 | * @evlist - list of events |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 744 | * @pages - map length in pages |
| 745 | * @overwrite - overwrite older events? |
| 746 | * |
| 747 | * If overwrite is false the user needs to signal event consuption using: |
| 748 | * |
| 749 | * struct perf_mmap *m = &evlist->mmap[cpu]; |
| 750 | * unsigned int head = perf_mmap__read_head(m); |
| 751 | * |
| 752 | * perf_mmap__write_tail(m, head) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 753 | * |
| 754 | * Using perf_evlist__read_on_cpu does this automatically. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 755 | */ |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 756 | int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, |
| 757 | bool overwrite) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 758 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 759 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 760 | const struct cpu_map *cpus = evlist->cpus; |
| 761 | const struct thread_map *threads = evlist->threads; |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 762 | int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; |
| 763 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 764 | if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 765 | return -ENOMEM; |
| 766 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 767 | if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 768 | return -ENOMEM; |
| 769 | |
| 770 | evlist->overwrite = overwrite; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 771 | evlist->mmap_len = perf_evlist__mmap_size(pages); |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 772 | pr_debug("mmap size %luB\n", evlist->mmap_len); |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 773 | mask = evlist->mmap_len - page_size - 1; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 774 | |
| 775 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 776 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 777 | evsel->sample_id == NULL && |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 778 | 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] | 779 | return -ENOMEM; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 780 | } |
| 781 | |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 782 | if (cpu_map__empty(cpus)) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 783 | return perf_evlist__mmap_per_thread(evlist, prot, mask); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 784 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 785 | return perf_evlist__mmap_per_cpu(evlist, prot, mask); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 786 | } |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 787 | |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 788 | int perf_evlist__create_maps(struct perf_evlist *evlist, |
| 789 | struct perf_target *target) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 790 | { |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 791 | evlist->threads = thread_map__new_str(target->pid, target->tid, |
| 792 | target->uid); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 793 | |
| 794 | if (evlist->threads == NULL) |
| 795 | return -1; |
| 796 | |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 797 | if (perf_target__has_task(target)) |
Namhyung Kim | d67356e | 2012-05-07 14:09:03 +0900 | [diff] [blame] | 798 | evlist->cpus = cpu_map__dummy_new(); |
Namhyung Kim | d1cb9fc | 2012-05-16 18:45:49 +0900 | [diff] [blame] | 799 | else if (!perf_target__has_cpu(target) && !target->uses_mmap) |
| 800 | evlist->cpus = cpu_map__dummy_new(); |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 801 | else |
| 802 | evlist->cpus = cpu_map__new(target->cpu_list); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 803 | |
| 804 | if (evlist->cpus == NULL) |
| 805 | goto out_delete_threads; |
| 806 | |
| 807 | return 0; |
| 808 | |
| 809 | out_delete_threads: |
| 810 | thread_map__delete(evlist->threads); |
| 811 | return -1; |
| 812 | } |
| 813 | |
| 814 | void perf_evlist__delete_maps(struct perf_evlist *evlist) |
| 815 | { |
| 816 | cpu_map__delete(evlist->cpus); |
| 817 | thread_map__delete(evlist->threads); |
| 818 | evlist->cpus = NULL; |
| 819 | evlist->threads = NULL; |
| 820 | } |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 821 | |
Arnaldo Carvalho de Melo | 1491a63 | 2012-09-26 14:43:13 -0300 | [diff] [blame] | 822 | int perf_evlist__apply_filters(struct perf_evlist *evlist) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 823 | { |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 824 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 825 | int err = 0; |
| 826 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 827 | nthreads = thread_map__nr(evlist->threads); |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 828 | |
| 829 | list_for_each_entry(evsel, &evlist->entries, node) { |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 830 | if (evsel->filter == NULL) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 831 | continue; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 832 | |
| 833 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); |
| 834 | if (err) |
| 835 | break; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 838 | return err; |
| 839 | } |
| 840 | |
| 841 | int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) |
| 842 | { |
| 843 | struct perf_evsel *evsel; |
| 844 | int err = 0; |
| 845 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 846 | nthreads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 847 | |
| 848 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 849 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); |
| 850 | if (err) |
| 851 | break; |
| 852 | } |
| 853 | |
| 854 | return err; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 855 | } |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 856 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 857 | bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 858 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 859 | struct perf_evsel *pos; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 860 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 861 | if (evlist->nr_entries == 1) |
| 862 | return true; |
| 863 | |
| 864 | if (evlist->id_pos < 0 || evlist->is_pos < 0) |
| 865 | return false; |
| 866 | |
| 867 | list_for_each_entry(pos, &evlist->entries, node) { |
| 868 | if (pos->id_pos != evlist->id_pos || |
| 869 | pos->is_pos != evlist->is_pos) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 870 | return false; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 871 | } |
| 872 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 873 | return true; |
| 874 | } |
| 875 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 876 | u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 877 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 878 | struct perf_evsel *evsel; |
| 879 | |
| 880 | if (evlist->combined_sample_type) |
| 881 | return evlist->combined_sample_type; |
| 882 | |
| 883 | list_for_each_entry(evsel, &evlist->entries, node) |
| 884 | evlist->combined_sample_type |= evsel->attr.sample_type; |
| 885 | |
| 886 | return evlist->combined_sample_type; |
| 887 | } |
| 888 | |
| 889 | u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
| 890 | { |
| 891 | evlist->combined_sample_type = 0; |
| 892 | return __perf_evlist__combined_sample_type(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 893 | } |
| 894 | |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 895 | bool perf_evlist__valid_read_format(struct perf_evlist *evlist) |
| 896 | { |
| 897 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
| 898 | u64 read_format = first->attr.read_format; |
| 899 | u64 sample_type = first->attr.sample_type; |
| 900 | |
| 901 | list_for_each_entry_continue(pos, &evlist->entries, node) { |
| 902 | if (read_format != pos->attr.read_format) |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ |
| 907 | if ((sample_type & PERF_SAMPLE_READ) && |
| 908 | !(read_format & PERF_FORMAT_ID)) { |
| 909 | return false; |
| 910 | } |
| 911 | |
| 912 | return true; |
| 913 | } |
| 914 | |
| 915 | u64 perf_evlist__read_format(struct perf_evlist *evlist) |
| 916 | { |
| 917 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 918 | return first->attr.read_format; |
| 919 | } |
| 920 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 921 | u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 922 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 923 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 924 | struct perf_sample *data; |
| 925 | u64 sample_type; |
| 926 | u16 size = 0; |
| 927 | |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 928 | if (!first->attr.sample_id_all) |
| 929 | goto out; |
| 930 | |
| 931 | sample_type = first->attr.sample_type; |
| 932 | |
| 933 | if (sample_type & PERF_SAMPLE_TID) |
| 934 | size += sizeof(data->tid) * 2; |
| 935 | |
| 936 | if (sample_type & PERF_SAMPLE_TIME) |
| 937 | size += sizeof(data->time); |
| 938 | |
| 939 | if (sample_type & PERF_SAMPLE_ID) |
| 940 | size += sizeof(data->id); |
| 941 | |
| 942 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 943 | size += sizeof(data->stream_id); |
| 944 | |
| 945 | if (sample_type & PERF_SAMPLE_CPU) |
| 946 | size += sizeof(data->cpu) * 2; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 947 | |
| 948 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 949 | size += sizeof(data->id); |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 950 | out: |
| 951 | return size; |
| 952 | } |
| 953 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 954 | 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] | 955 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 956 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 957 | |
| 958 | list_for_each_entry_continue(pos, &evlist->entries, node) { |
| 959 | if (first->attr.sample_id_all != pos->attr.sample_id_all) |
| 960 | return false; |
| 961 | } |
| 962 | |
| 963 | return true; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 966 | bool perf_evlist__sample_id_all(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 967 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 968 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 969 | return first->attr.sample_id_all; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 970 | } |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 971 | |
| 972 | void perf_evlist__set_selected(struct perf_evlist *evlist, |
| 973 | struct perf_evsel *evsel) |
| 974 | { |
| 975 | evlist->selected = evsel; |
| 976 | } |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 977 | |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 978 | void perf_evlist__close(struct perf_evlist *evlist) |
| 979 | { |
| 980 | struct perf_evsel *evsel; |
| 981 | int ncpus = cpu_map__nr(evlist->cpus); |
| 982 | int nthreads = thread_map__nr(evlist->threads); |
| 983 | |
| 984 | list_for_each_entry_reverse(evsel, &evlist->entries, node) |
| 985 | perf_evsel__close(evsel, ncpus, nthreads); |
| 986 | } |
| 987 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 988 | int perf_evlist__open(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 989 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 990 | struct perf_evsel *evsel; |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 991 | int err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 992 | |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 993 | perf_evlist__update_id_pos(evlist); |
| 994 | |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 995 | list_for_each_entry(evsel, &evlist->entries, node) { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 996 | err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 997 | if (err < 0) |
| 998 | goto out_err; |
| 999 | } |
| 1000 | |
| 1001 | return 0; |
| 1002 | out_err: |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1003 | perf_evlist__close(evlist); |
Namhyung Kim | 41c21a6 | 2012-02-23 12:13:36 +0900 | [diff] [blame] | 1004 | errno = -err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1005 | return err; |
| 1006 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1007 | |
| 1008 | int perf_evlist__prepare_workload(struct perf_evlist *evlist, |
Namhyung Kim | 6ef73ec | 2013-03-11 16:43:15 +0900 | [diff] [blame] | 1009 | struct perf_target *target, |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 1010 | const char *argv[], bool pipe_output, |
| 1011 | bool want_signal) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1012 | { |
| 1013 | int child_ready_pipe[2], go_pipe[2]; |
| 1014 | char bf; |
| 1015 | |
| 1016 | if (pipe(child_ready_pipe) < 0) { |
| 1017 | perror("failed to create 'ready' pipe"); |
| 1018 | return -1; |
| 1019 | } |
| 1020 | |
| 1021 | if (pipe(go_pipe) < 0) { |
| 1022 | perror("failed to create 'go' pipe"); |
| 1023 | goto out_close_ready_pipe; |
| 1024 | } |
| 1025 | |
| 1026 | evlist->workload.pid = fork(); |
| 1027 | if (evlist->workload.pid < 0) { |
| 1028 | perror("failed to fork"); |
| 1029 | goto out_close_pipes; |
| 1030 | } |
| 1031 | |
| 1032 | if (!evlist->workload.pid) { |
Namhyung Kim | 119fa3c | 2013-03-11 16:43:16 +0900 | [diff] [blame] | 1033 | if (pipe_output) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1034 | dup2(2, 1); |
| 1035 | |
David Ahern | 0817df0 | 2013-05-25 17:50:39 -0600 | [diff] [blame] | 1036 | signal(SIGTERM, SIG_DFL); |
| 1037 | |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1038 | close(child_ready_pipe[0]); |
| 1039 | close(go_pipe[1]); |
| 1040 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); |
| 1041 | |
| 1042 | /* |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1043 | * Tell the parent we're ready to go |
| 1044 | */ |
| 1045 | close(child_ready_pipe[1]); |
| 1046 | |
| 1047 | /* |
| 1048 | * Wait until the parent tells us to go. |
| 1049 | */ |
| 1050 | if (read(go_pipe[0], &bf, 1) == -1) |
| 1051 | perror("unable to read pipe"); |
| 1052 | |
| 1053 | execvp(argv[0], (char **)argv); |
| 1054 | |
| 1055 | perror(argv[0]); |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 1056 | if (want_signal) |
| 1057 | kill(getppid(), SIGUSR1); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1058 | exit(-1); |
| 1059 | } |
| 1060 | |
Namhyung Kim | 6ef73ec | 2013-03-11 16:43:15 +0900 | [diff] [blame] | 1061 | if (perf_target__none(target)) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1062 | evlist->threads->map[0] = evlist->workload.pid; |
| 1063 | |
| 1064 | close(child_ready_pipe[1]); |
| 1065 | close(go_pipe[0]); |
| 1066 | /* |
| 1067 | * wait for child to settle |
| 1068 | */ |
| 1069 | if (read(child_ready_pipe[0], &bf, 1) == -1) { |
| 1070 | perror("unable to read pipe"); |
| 1071 | goto out_close_pipes; |
| 1072 | } |
| 1073 | |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1074 | fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1075 | evlist->workload.cork_fd = go_pipe[1]; |
| 1076 | close(child_ready_pipe[0]); |
| 1077 | return 0; |
| 1078 | |
| 1079 | out_close_pipes: |
| 1080 | close(go_pipe[0]); |
| 1081 | close(go_pipe[1]); |
| 1082 | out_close_ready_pipe: |
| 1083 | close(child_ready_pipe[0]); |
| 1084 | close(child_ready_pipe[1]); |
| 1085 | return -1; |
| 1086 | } |
| 1087 | |
| 1088 | int perf_evlist__start_workload(struct perf_evlist *evlist) |
| 1089 | { |
| 1090 | if (evlist->workload.cork_fd > 0) { |
David Ahern | b382440 | 2013-07-02 13:27:21 -0600 | [diff] [blame] | 1091 | char bf = 0; |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1092 | int ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1093 | /* |
| 1094 | * Remove the cork, let it rip! |
| 1095 | */ |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1096 | ret = write(evlist->workload.cork_fd, &bf, 1); |
| 1097 | if (ret < 0) |
| 1098 | perror("enable to write to pipe"); |
| 1099 | |
| 1100 | close(evlist->workload.cork_fd); |
| 1101 | return ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1106 | |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1107 | 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] | 1108 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1109 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1110 | struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); |
| 1111 | |
| 1112 | if (!evsel) |
| 1113 | return -EFAULT; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1114 | return perf_evsel__parse_sample(evsel, event, sample); |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1115 | } |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1116 | |
| 1117 | size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) |
| 1118 | { |
| 1119 | struct perf_evsel *evsel; |
| 1120 | size_t printed = 0; |
| 1121 | |
| 1122 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 1123 | printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", |
| 1124 | perf_evsel__name(evsel)); |
| 1125 | } |
| 1126 | |
| 1127 | return printed + fprintf(fp, "\n");; |
| 1128 | } |
Arnaldo Carvalho de Melo | 6ef068c | 2013-10-17 12:07:58 -0300 | [diff] [blame] | 1129 | |
| 1130 | int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused, |
| 1131 | int err, char *buf, size_t size) |
| 1132 | { |
| 1133 | char sbuf[128]; |
| 1134 | |
| 1135 | switch (err) { |
| 1136 | case ENOENT: |
| 1137 | scnprintf(buf, size, "%s", |
| 1138 | "Error:\tUnable to find debugfs\n" |
| 1139 | "Hint:\tWas your kernel was compiled with debugfs support?\n" |
| 1140 | "Hint:\tIs the debugfs filesystem mounted?\n" |
| 1141 | "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'"); |
| 1142 | break; |
| 1143 | case EACCES: |
| 1144 | scnprintf(buf, size, |
| 1145 | "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n" |
| 1146 | "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", |
| 1147 | debugfs_mountpoint, debugfs_mountpoint); |
| 1148 | break; |
| 1149 | default: |
| 1150 | scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf))); |
| 1151 | break; |
| 1152 | } |
| 1153 | |
| 1154 | return 0; |
| 1155 | } |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1156 | |
| 1157 | int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, |
| 1158 | int err, char *buf, size_t size) |
| 1159 | { |
| 1160 | int printed, value; |
| 1161 | char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); |
| 1162 | |
| 1163 | switch (err) { |
| 1164 | case EACCES: |
| 1165 | case EPERM: |
| 1166 | printed = scnprintf(buf, size, |
| 1167 | "Error:\t%s.\n" |
| 1168 | "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); |
| 1169 | |
| 1170 | if (filename__read_int("/proc/sys/kernel/perf_event_paranoid", &value)) |
| 1171 | break; |
| 1172 | |
| 1173 | printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); |
| 1174 | |
| 1175 | if (value >= 2) { |
| 1176 | printed += scnprintf(buf + printed, size - printed, |
| 1177 | "For your workloads it needs to be <= 1\nHint:\t"); |
| 1178 | } |
| 1179 | printed += scnprintf(buf + printed, size - printed, |
| 1180 | "For system wide tracing it needs to be set to -1"); |
| 1181 | |
| 1182 | printed += scnprintf(buf + printed, size - printed, |
| 1183 | ".\nHint:\tThe current value is %d.", value); |
| 1184 | break; |
| 1185 | default: |
| 1186 | scnprintf(buf, size, "%s", emsg); |
| 1187 | break; |
| 1188 | } |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |