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 | |
| 708 | int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, |
| 709 | int unset __maybe_unused) |
| 710 | { |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 711 | unsigned int *mmap_pages = opt->value; |
| 712 | unsigned long pages, val; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 713 | size_t size; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 714 | static struct parse_tag tags[] = { |
| 715 | { .tag = 'B', .mult = 1 }, |
| 716 | { .tag = 'K', .mult = 1 << 10 }, |
| 717 | { .tag = 'M', .mult = 1 << 20 }, |
| 718 | { .tag = 'G', .mult = 1 << 30 }, |
| 719 | { .tag = 0 }, |
| 720 | }; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 721 | |
David Ahern | 8973504 | 2013-11-12 07:46:53 -0700 | [diff] [blame^] | 722 | if (str == NULL) |
| 723 | return -1; |
| 724 | |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 725 | val = parse_tag_value(str, tags); |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 726 | if (val != (unsigned long) -1) { |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 727 | /* we got file size value */ |
| 728 | pages = PERF_ALIGN(val, page_size) / page_size; |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 729 | if (pages < (1UL << 31) && !is_power_of_2(pages)) { |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 730 | pages = next_pow2(pages); |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 731 | pr_info("rounding mmap pages size to %lu (%lu pages)\n", |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 732 | pages * page_size, pages); |
| 733 | } |
| 734 | } else { |
| 735 | /* we got pages count value */ |
| 736 | char *eptr; |
| 737 | pages = strtoul(str, &eptr, 10); |
| 738 | if (*eptr != '\0') { |
| 739 | pr_err("failed to parse --mmap_pages/-m value\n"); |
| 740 | return -1; |
| 741 | } |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 742 | } |
| 743 | |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 744 | if (pages > UINT_MAX || pages > SIZE_MAX / page_size) { |
| 745 | pr_err("--mmap_pages/-m value too big\n"); |
| 746 | return -1; |
| 747 | } |
| 748 | |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 749 | size = perf_evlist__mmap_size(pages); |
| 750 | if (!size) { |
| 751 | pr_err("--mmap_pages/-m value must be a power of two."); |
| 752 | return -1; |
| 753 | } |
| 754 | |
| 755 | *mmap_pages = pages; |
| 756 | return 0; |
| 757 | } |
| 758 | |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 759 | /** |
| 760 | * perf_evlist__mmap - Create mmaps to receive events. |
| 761 | * @evlist: list of events |
| 762 | * @pages: map length in pages |
| 763 | * @overwrite: overwrite older events? |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 764 | * |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 765 | * If @overwrite is %false the user needs to signal event consumption using |
| 766 | * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this |
| 767 | * automatically. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 768 | * |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 769 | * Return: %0 on success, negative error code otherwise. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 770 | */ |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 771 | int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, |
| 772 | bool overwrite) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 773 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 774 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 775 | const struct cpu_map *cpus = evlist->cpus; |
| 776 | const struct thread_map *threads = evlist->threads; |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 777 | int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; |
| 778 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 779 | if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 780 | return -ENOMEM; |
| 781 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 782 | if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 783 | return -ENOMEM; |
| 784 | |
| 785 | evlist->overwrite = overwrite; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 786 | evlist->mmap_len = perf_evlist__mmap_size(pages); |
Adrian Hunter | 2af68ef | 2013-10-18 15:29:07 +0300 | [diff] [blame] | 787 | pr_debug("mmap size %zuB\n", evlist->mmap_len); |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 788 | mask = evlist->mmap_len - page_size - 1; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 789 | |
| 790 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 791 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 792 | evsel->sample_id == NULL && |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 793 | 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] | 794 | return -ENOMEM; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 795 | } |
| 796 | |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 797 | if (cpu_map__empty(cpus)) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 798 | return perf_evlist__mmap_per_thread(evlist, prot, mask); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 799 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 800 | return perf_evlist__mmap_per_cpu(evlist, prot, mask); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 801 | } |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 802 | |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 803 | int perf_evlist__create_maps(struct perf_evlist *evlist, |
| 804 | struct perf_target *target) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 805 | { |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 806 | evlist->threads = thread_map__new_str(target->pid, target->tid, |
| 807 | target->uid); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 808 | |
| 809 | if (evlist->threads == NULL) |
| 810 | return -1; |
| 811 | |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 812 | if (perf_target__has_task(target)) |
Namhyung Kim | d67356e | 2012-05-07 14:09:03 +0900 | [diff] [blame] | 813 | evlist->cpus = cpu_map__dummy_new(); |
Namhyung Kim | d1cb9fc | 2012-05-16 18:45:49 +0900 | [diff] [blame] | 814 | else if (!perf_target__has_cpu(target) && !target->uses_mmap) |
| 815 | evlist->cpus = cpu_map__dummy_new(); |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 816 | else |
| 817 | evlist->cpus = cpu_map__new(target->cpu_list); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 818 | |
| 819 | if (evlist->cpus == NULL) |
| 820 | goto out_delete_threads; |
| 821 | |
| 822 | return 0; |
| 823 | |
| 824 | out_delete_threads: |
| 825 | thread_map__delete(evlist->threads); |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | void perf_evlist__delete_maps(struct perf_evlist *evlist) |
| 830 | { |
| 831 | cpu_map__delete(evlist->cpus); |
| 832 | thread_map__delete(evlist->threads); |
| 833 | evlist->cpus = NULL; |
| 834 | evlist->threads = NULL; |
| 835 | } |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 836 | |
Arnaldo Carvalho de Melo | 1491a63 | 2012-09-26 14:43:13 -0300 | [diff] [blame] | 837 | int perf_evlist__apply_filters(struct perf_evlist *evlist) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 838 | { |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 839 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 840 | int err = 0; |
| 841 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 842 | nthreads = thread_map__nr(evlist->threads); |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 843 | |
| 844 | list_for_each_entry(evsel, &evlist->entries, node) { |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 845 | if (evsel->filter == NULL) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 846 | continue; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 847 | |
| 848 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); |
| 849 | if (err) |
| 850 | break; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 853 | return err; |
| 854 | } |
| 855 | |
| 856 | int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) |
| 857 | { |
| 858 | struct perf_evsel *evsel; |
| 859 | int err = 0; |
| 860 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 861 | nthreads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 862 | |
| 863 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 864 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); |
| 865 | if (err) |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | return err; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 870 | } |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 871 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 872 | bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 873 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 874 | struct perf_evsel *pos; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 875 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 876 | if (evlist->nr_entries == 1) |
| 877 | return true; |
| 878 | |
| 879 | if (evlist->id_pos < 0 || evlist->is_pos < 0) |
| 880 | return false; |
| 881 | |
| 882 | list_for_each_entry(pos, &evlist->entries, node) { |
| 883 | if (pos->id_pos != evlist->id_pos || |
| 884 | pos->is_pos != evlist->is_pos) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 885 | return false; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 886 | } |
| 887 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 888 | return true; |
| 889 | } |
| 890 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 891 | u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 892 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 893 | struct perf_evsel *evsel; |
| 894 | |
| 895 | if (evlist->combined_sample_type) |
| 896 | return evlist->combined_sample_type; |
| 897 | |
| 898 | list_for_each_entry(evsel, &evlist->entries, node) |
| 899 | evlist->combined_sample_type |= evsel->attr.sample_type; |
| 900 | |
| 901 | return evlist->combined_sample_type; |
| 902 | } |
| 903 | |
| 904 | u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
| 905 | { |
| 906 | evlist->combined_sample_type = 0; |
| 907 | return __perf_evlist__combined_sample_type(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 908 | } |
| 909 | |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 910 | bool perf_evlist__valid_read_format(struct perf_evlist *evlist) |
| 911 | { |
| 912 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
| 913 | u64 read_format = first->attr.read_format; |
| 914 | u64 sample_type = first->attr.sample_type; |
| 915 | |
| 916 | list_for_each_entry_continue(pos, &evlist->entries, node) { |
| 917 | if (read_format != pos->attr.read_format) |
| 918 | return false; |
| 919 | } |
| 920 | |
| 921 | /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ |
| 922 | if ((sample_type & PERF_SAMPLE_READ) && |
| 923 | !(read_format & PERF_FORMAT_ID)) { |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | return true; |
| 928 | } |
| 929 | |
| 930 | u64 perf_evlist__read_format(struct perf_evlist *evlist) |
| 931 | { |
| 932 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 933 | return first->attr.read_format; |
| 934 | } |
| 935 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 936 | u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 937 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 938 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 939 | struct perf_sample *data; |
| 940 | u64 sample_type; |
| 941 | u16 size = 0; |
| 942 | |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 943 | if (!first->attr.sample_id_all) |
| 944 | goto out; |
| 945 | |
| 946 | sample_type = first->attr.sample_type; |
| 947 | |
| 948 | if (sample_type & PERF_SAMPLE_TID) |
| 949 | size += sizeof(data->tid) * 2; |
| 950 | |
| 951 | if (sample_type & PERF_SAMPLE_TIME) |
| 952 | size += sizeof(data->time); |
| 953 | |
| 954 | if (sample_type & PERF_SAMPLE_ID) |
| 955 | size += sizeof(data->id); |
| 956 | |
| 957 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 958 | size += sizeof(data->stream_id); |
| 959 | |
| 960 | if (sample_type & PERF_SAMPLE_CPU) |
| 961 | size += sizeof(data->cpu) * 2; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 962 | |
| 963 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 964 | size += sizeof(data->id); |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 965 | out: |
| 966 | return size; |
| 967 | } |
| 968 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 969 | 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] | 970 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 971 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 972 | |
| 973 | list_for_each_entry_continue(pos, &evlist->entries, node) { |
| 974 | if (first->attr.sample_id_all != pos->attr.sample_id_all) |
| 975 | return false; |
| 976 | } |
| 977 | |
| 978 | return true; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 979 | } |
| 980 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 981 | bool perf_evlist__sample_id_all(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 982 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 983 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 984 | return first->attr.sample_id_all; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 985 | } |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 986 | |
| 987 | void perf_evlist__set_selected(struct perf_evlist *evlist, |
| 988 | struct perf_evsel *evsel) |
| 989 | { |
| 990 | evlist->selected = evsel; |
| 991 | } |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 992 | |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 993 | void perf_evlist__close(struct perf_evlist *evlist) |
| 994 | { |
| 995 | struct perf_evsel *evsel; |
| 996 | int ncpus = cpu_map__nr(evlist->cpus); |
| 997 | int nthreads = thread_map__nr(evlist->threads); |
| 998 | |
| 999 | list_for_each_entry_reverse(evsel, &evlist->entries, node) |
| 1000 | perf_evsel__close(evsel, ncpus, nthreads); |
| 1001 | } |
| 1002 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1003 | int perf_evlist__open(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1004 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1005 | struct perf_evsel *evsel; |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1006 | int err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1007 | |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 1008 | perf_evlist__update_id_pos(evlist); |
| 1009 | |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1010 | list_for_each_entry(evsel, &evlist->entries, node) { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1011 | err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1012 | if (err < 0) |
| 1013 | goto out_err; |
| 1014 | } |
| 1015 | |
| 1016 | return 0; |
| 1017 | out_err: |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1018 | perf_evlist__close(evlist); |
Namhyung Kim | 41c21a6 | 2012-02-23 12:13:36 +0900 | [diff] [blame] | 1019 | errno = -err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1020 | return err; |
| 1021 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1022 | |
| 1023 | int perf_evlist__prepare_workload(struct perf_evlist *evlist, |
Namhyung Kim | 6ef73ec | 2013-03-11 16:43:15 +0900 | [diff] [blame] | 1024 | struct perf_target *target, |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 1025 | const char *argv[], bool pipe_output, |
| 1026 | bool want_signal) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1027 | { |
| 1028 | int child_ready_pipe[2], go_pipe[2]; |
| 1029 | char bf; |
| 1030 | |
| 1031 | if (pipe(child_ready_pipe) < 0) { |
| 1032 | perror("failed to create 'ready' pipe"); |
| 1033 | return -1; |
| 1034 | } |
| 1035 | |
| 1036 | if (pipe(go_pipe) < 0) { |
| 1037 | perror("failed to create 'go' pipe"); |
| 1038 | goto out_close_ready_pipe; |
| 1039 | } |
| 1040 | |
| 1041 | evlist->workload.pid = fork(); |
| 1042 | if (evlist->workload.pid < 0) { |
| 1043 | perror("failed to fork"); |
| 1044 | goto out_close_pipes; |
| 1045 | } |
| 1046 | |
| 1047 | if (!evlist->workload.pid) { |
Namhyung Kim | 119fa3c | 2013-03-11 16:43:16 +0900 | [diff] [blame] | 1048 | if (pipe_output) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1049 | dup2(2, 1); |
| 1050 | |
David Ahern | 0817df0 | 2013-05-25 17:50:39 -0600 | [diff] [blame] | 1051 | signal(SIGTERM, SIG_DFL); |
| 1052 | |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1053 | close(child_ready_pipe[0]); |
| 1054 | close(go_pipe[1]); |
| 1055 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); |
| 1056 | |
| 1057 | /* |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1058 | * Tell the parent we're ready to go |
| 1059 | */ |
| 1060 | close(child_ready_pipe[1]); |
| 1061 | |
| 1062 | /* |
| 1063 | * Wait until the parent tells us to go. |
| 1064 | */ |
| 1065 | if (read(go_pipe[0], &bf, 1) == -1) |
| 1066 | perror("unable to read pipe"); |
| 1067 | |
| 1068 | execvp(argv[0], (char **)argv); |
| 1069 | |
| 1070 | perror(argv[0]); |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 1071 | if (want_signal) |
| 1072 | kill(getppid(), SIGUSR1); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1073 | exit(-1); |
| 1074 | } |
| 1075 | |
Namhyung Kim | 6ef73ec | 2013-03-11 16:43:15 +0900 | [diff] [blame] | 1076 | if (perf_target__none(target)) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1077 | evlist->threads->map[0] = evlist->workload.pid; |
| 1078 | |
| 1079 | close(child_ready_pipe[1]); |
| 1080 | close(go_pipe[0]); |
| 1081 | /* |
| 1082 | * wait for child to settle |
| 1083 | */ |
| 1084 | if (read(child_ready_pipe[0], &bf, 1) == -1) { |
| 1085 | perror("unable to read pipe"); |
| 1086 | goto out_close_pipes; |
| 1087 | } |
| 1088 | |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1089 | fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1090 | evlist->workload.cork_fd = go_pipe[1]; |
| 1091 | close(child_ready_pipe[0]); |
| 1092 | return 0; |
| 1093 | |
| 1094 | out_close_pipes: |
| 1095 | close(go_pipe[0]); |
| 1096 | close(go_pipe[1]); |
| 1097 | out_close_ready_pipe: |
| 1098 | close(child_ready_pipe[0]); |
| 1099 | close(child_ready_pipe[1]); |
| 1100 | return -1; |
| 1101 | } |
| 1102 | |
| 1103 | int perf_evlist__start_workload(struct perf_evlist *evlist) |
| 1104 | { |
| 1105 | if (evlist->workload.cork_fd > 0) { |
David Ahern | b382440 | 2013-07-02 13:27:21 -0600 | [diff] [blame] | 1106 | char bf = 0; |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1107 | int ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1108 | /* |
| 1109 | * Remove the cork, let it rip! |
| 1110 | */ |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1111 | ret = write(evlist->workload.cork_fd, &bf, 1); |
| 1112 | if (ret < 0) |
| 1113 | perror("enable to write to pipe"); |
| 1114 | |
| 1115 | close(evlist->workload.cork_fd); |
| 1116 | return ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | return 0; |
| 1120 | } |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1121 | |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1122 | 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] | 1123 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1124 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1125 | struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); |
| 1126 | |
| 1127 | if (!evsel) |
| 1128 | return -EFAULT; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1129 | return perf_evsel__parse_sample(evsel, event, sample); |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1130 | } |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1131 | |
| 1132 | size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) |
| 1133 | { |
| 1134 | struct perf_evsel *evsel; |
| 1135 | size_t printed = 0; |
| 1136 | |
| 1137 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 1138 | printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", |
| 1139 | perf_evsel__name(evsel)); |
| 1140 | } |
| 1141 | |
| 1142 | return printed + fprintf(fp, "\n");; |
| 1143 | } |
Arnaldo Carvalho de Melo | 6ef068c | 2013-10-17 12:07:58 -0300 | [diff] [blame] | 1144 | |
| 1145 | int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused, |
| 1146 | int err, char *buf, size_t size) |
| 1147 | { |
| 1148 | char sbuf[128]; |
| 1149 | |
| 1150 | switch (err) { |
| 1151 | case ENOENT: |
| 1152 | scnprintf(buf, size, "%s", |
| 1153 | "Error:\tUnable to find debugfs\n" |
| 1154 | "Hint:\tWas your kernel was compiled with debugfs support?\n" |
| 1155 | "Hint:\tIs the debugfs filesystem mounted?\n" |
| 1156 | "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'"); |
| 1157 | break; |
| 1158 | case EACCES: |
| 1159 | scnprintf(buf, size, |
| 1160 | "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n" |
| 1161 | "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", |
| 1162 | debugfs_mountpoint, debugfs_mountpoint); |
| 1163 | break; |
| 1164 | default: |
| 1165 | scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf))); |
| 1166 | break; |
| 1167 | } |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1171 | |
| 1172 | int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, |
| 1173 | int err, char *buf, size_t size) |
| 1174 | { |
| 1175 | int printed, value; |
| 1176 | char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); |
| 1177 | |
| 1178 | switch (err) { |
| 1179 | case EACCES: |
| 1180 | case EPERM: |
| 1181 | printed = scnprintf(buf, size, |
| 1182 | "Error:\t%s.\n" |
| 1183 | "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); |
| 1184 | |
| 1185 | if (filename__read_int("/proc/sys/kernel/perf_event_paranoid", &value)) |
| 1186 | break; |
| 1187 | |
| 1188 | printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); |
| 1189 | |
| 1190 | if (value >= 2) { |
| 1191 | printed += scnprintf(buf + printed, size - printed, |
| 1192 | "For your workloads it needs to be <= 1\nHint:\t"); |
| 1193 | } |
| 1194 | printed += scnprintf(buf + printed, size - printed, |
| 1195 | "For system wide tracing it needs to be set to -1"); |
| 1196 | |
| 1197 | printed += scnprintf(buf + printed, size - printed, |
| 1198 | ".\nHint:\tThe current value is %d.", value); |
| 1199 | break; |
| 1200 | default: |
| 1201 | scnprintf(buf, size, "%s", emsg); |
| 1202 | break; |
| 1203 | } |
| 1204 | |
| 1205 | return 0; |
| 1206 | } |