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