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" |
| 10 | #include "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" |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 17 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 18 | |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 19 | #include "parse-events.h" |
| 20 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 21 | #include <sys/mman.h> |
| 22 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 23 | #include <linux/bitops.h> |
| 24 | #include <linux/hash.h> |
| 25 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 26 | #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] | 27 | #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] | 28 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 29 | void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, |
| 30 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 31 | { |
| 32 | int i; |
| 33 | |
| 34 | for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) |
| 35 | INIT_HLIST_HEAD(&evlist->heads[i]); |
| 36 | INIT_LIST_HEAD(&evlist->entries); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 37 | perf_evlist__set_maps(evlist, cpus, threads); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 38 | evlist->workload.pid = -1; |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 39 | } |
| 40 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 41 | struct perf_evlist *perf_evlist__new(struct cpu_map *cpus, |
| 42 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 43 | { |
| 44 | struct perf_evlist *evlist = zalloc(sizeof(*evlist)); |
| 45 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 46 | if (evlist != NULL) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 47 | perf_evlist__init(evlist, cpus, threads); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 48 | |
| 49 | return evlist; |
| 50 | } |
| 51 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 52 | void perf_evlist__config_attrs(struct perf_evlist *evlist, |
| 53 | struct perf_record_opts *opts) |
| 54 | { |
Namhyung Kim | 5090c6a | 2012-03-16 17:42:20 +0900 | [diff] [blame] | 55 | struct perf_evsel *evsel, *first; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 56 | |
| 57 | if (evlist->cpus->map[0] < 0) |
| 58 | opts->no_inherit = true; |
| 59 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 60 | first = perf_evlist__first(evlist); |
Namhyung Kim | 5090c6a | 2012-03-16 17:42:20 +0900 | [diff] [blame] | 61 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 62 | list_for_each_entry(evsel, &evlist->entries, node) { |
Namhyung Kim | 5090c6a | 2012-03-16 17:42:20 +0900 | [diff] [blame] | 63 | perf_evsel__config(evsel, opts, first); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 64 | |
| 65 | if (evlist->nr_entries > 1) |
| 66 | evsel->attr.sample_type |= PERF_SAMPLE_ID; |
| 67 | } |
| 68 | } |
| 69 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 70 | static void perf_evlist__purge(struct perf_evlist *evlist) |
| 71 | { |
| 72 | struct perf_evsel *pos, *n; |
| 73 | |
| 74 | list_for_each_entry_safe(pos, n, &evlist->entries, node) { |
| 75 | list_del_init(&pos->node); |
| 76 | perf_evsel__delete(pos); |
| 77 | } |
| 78 | |
| 79 | evlist->nr_entries = 0; |
| 80 | } |
| 81 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 82 | void perf_evlist__exit(struct perf_evlist *evlist) |
| 83 | { |
| 84 | free(evlist->mmap); |
| 85 | free(evlist->pollfd); |
| 86 | evlist->mmap = NULL; |
| 87 | evlist->pollfd = NULL; |
| 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 90 | void perf_evlist__delete(struct perf_evlist *evlist) |
| 91 | { |
| 92 | perf_evlist__purge(evlist); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 93 | perf_evlist__exit(evlist); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 94 | free(evlist); |
| 95 | } |
| 96 | |
| 97 | void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) |
| 98 | { |
| 99 | list_add_tail(&entry->node, &evlist->entries); |
| 100 | ++evlist->nr_entries; |
| 101 | } |
| 102 | |
Jiri Olsa | 0529bc1 | 2012-01-27 15:34:20 +0100 | [diff] [blame] | 103 | void perf_evlist__splice_list_tail(struct perf_evlist *evlist, |
| 104 | struct list_head *list, |
| 105 | int nr_entries) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 106 | { |
| 107 | list_splice_tail(list, &evlist->entries); |
| 108 | evlist->nr_entries += nr_entries; |
| 109 | } |
| 110 | |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 111 | void __perf_evlist__set_leader(struct list_head *list) |
| 112 | { |
| 113 | struct perf_evsel *evsel, *leader; |
| 114 | |
| 115 | leader = list_entry(list->next, struct perf_evsel, node); |
| 116 | leader->leader = NULL; |
| 117 | |
| 118 | list_for_each_entry(evsel, list, node) { |
| 119 | if (evsel != leader) |
| 120 | evsel->leader = leader; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void perf_evlist__set_leader(struct perf_evlist *evlist) |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 125 | { |
| 126 | if (evlist->nr_entries) |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 127 | __perf_evlist__set_leader(&evlist->entries); |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 130 | int perf_evlist__add_default(struct perf_evlist *evlist) |
| 131 | { |
| 132 | struct perf_event_attr attr = { |
| 133 | .type = PERF_TYPE_HARDWARE, |
| 134 | .config = PERF_COUNT_HW_CPU_CYCLES, |
| 135 | }; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 136 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 137 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 138 | event_attr_init(&attr); |
| 139 | |
| 140 | evsel = perf_evsel__new(&attr, 0); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 141 | if (evsel == NULL) |
Stephane Eranian | cc2d86b | 2011-06-07 18:19:36 +0200 | [diff] [blame] | 142 | goto error; |
| 143 | |
| 144 | /* use strdup() because free(evsel) assumes name is allocated */ |
| 145 | evsel->name = strdup("cycles"); |
| 146 | if (!evsel->name) |
| 147 | goto error_free; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 148 | |
| 149 | perf_evlist__add(evlist, evsel); |
| 150 | return 0; |
Stephane Eranian | cc2d86b | 2011-06-07 18:19:36 +0200 | [diff] [blame] | 151 | error_free: |
| 152 | perf_evsel__delete(evsel); |
| 153 | error: |
| 154 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 155 | } |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 156 | |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 157 | int perf_evlist__add_attrs(struct perf_evlist *evlist, |
| 158 | struct perf_event_attr *attrs, size_t nr_attrs) |
| 159 | { |
| 160 | struct perf_evsel *evsel, *n; |
| 161 | LIST_HEAD(head); |
| 162 | size_t i; |
| 163 | |
| 164 | for (i = 0; i < nr_attrs; i++) { |
| 165 | evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); |
| 166 | if (evsel == NULL) |
| 167 | goto out_delete_partial_list; |
| 168 | list_add_tail(&evsel->node, &head); |
| 169 | } |
| 170 | |
| 171 | perf_evlist__splice_list_tail(evlist, &head, nr_attrs); |
| 172 | |
| 173 | return 0; |
| 174 | |
| 175 | out_delete_partial_list: |
| 176 | list_for_each_entry_safe(evsel, n, &head, node) |
| 177 | perf_evsel__delete(evsel); |
| 178 | return -1; |
| 179 | } |
| 180 | |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 181 | int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, |
| 182 | struct perf_event_attr *attrs, size_t nr_attrs) |
| 183 | { |
| 184 | size_t i; |
| 185 | |
| 186 | for (i = 0; i < nr_attrs; i++) |
| 187 | event_attr_init(attrs + i); |
| 188 | |
| 189 | return perf_evlist__add_attrs(evlist, attrs, nr_attrs); |
| 190 | } |
| 191 | |
Arnaldo Carvalho de Melo | a8c9ae1 | 2011-11-05 08:41:51 -0200 | [diff] [blame] | 192 | static int trace_event__id(const char *evname) |
| 193 | { |
| 194 | char *filename, *colon; |
| 195 | int err = -1, fd; |
| 196 | |
| 197 | if (asprintf(&filename, "%s/%s/id", tracing_events_path, evname) < 0) |
| 198 | return -1; |
| 199 | |
| 200 | colon = strrchr(filename, ':'); |
| 201 | if (colon != NULL) |
| 202 | *colon = '/'; |
| 203 | |
| 204 | fd = open(filename, O_RDONLY); |
| 205 | if (fd >= 0) { |
| 206 | char id[16]; |
| 207 | if (read(fd, id, sizeof(id)) > 0) |
| 208 | err = atoi(id); |
| 209 | close(fd); |
| 210 | } |
| 211 | |
| 212 | free(filename); |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | int perf_evlist__add_tracepoints(struct perf_evlist *evlist, |
| 217 | const char *tracepoints[], |
| 218 | size_t nr_tracepoints) |
| 219 | { |
| 220 | int err; |
| 221 | size_t i; |
| 222 | struct perf_event_attr *attrs = zalloc(nr_tracepoints * sizeof(*attrs)); |
| 223 | |
| 224 | if (attrs == NULL) |
| 225 | return -1; |
| 226 | |
| 227 | for (i = 0; i < nr_tracepoints; i++) { |
| 228 | err = trace_event__id(tracepoints[i]); |
| 229 | |
| 230 | if (err < 0) |
| 231 | goto out_free_attrs; |
| 232 | |
| 233 | attrs[i].type = PERF_TYPE_TRACEPOINT; |
| 234 | attrs[i].config = err; |
| 235 | attrs[i].sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | |
Frederic Weisbecker | 0983cc0 | 2012-07-18 19:10:54 +0200 | [diff] [blame] | 236 | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD); |
Arnaldo Carvalho de Melo | a8c9ae1 | 2011-11-05 08:41:51 -0200 | [diff] [blame] | 237 | attrs[i].sample_period = 1; |
| 238 | } |
| 239 | |
| 240 | err = perf_evlist__add_attrs(evlist, attrs, nr_tracepoints); |
| 241 | out_free_attrs: |
| 242 | free(attrs); |
| 243 | return err; |
| 244 | } |
| 245 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 246 | struct perf_evsel * |
| 247 | 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] | 248 | { |
| 249 | struct perf_evsel *evsel; |
| 250 | |
| 251 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 252 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && |
| 253 | (int)evsel->attr.config == id) |
| 254 | return evsel; |
| 255 | } |
| 256 | |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | int perf_evlist__set_tracepoints_handlers(struct perf_evlist *evlist, |
| 261 | const struct perf_evsel_str_handler *assocs, |
| 262 | size_t nr_assocs) |
| 263 | { |
| 264 | struct perf_evsel *evsel; |
| 265 | int err; |
| 266 | size_t i; |
| 267 | |
| 268 | for (i = 0; i < nr_assocs; i++) { |
| 269 | err = trace_event__id(assocs[i].name); |
| 270 | if (err < 0) |
| 271 | goto out; |
| 272 | |
| 273 | evsel = perf_evlist__find_tracepoint_by_id(evlist, err); |
| 274 | if (evsel == NULL) |
| 275 | continue; |
| 276 | |
| 277 | err = -EEXIST; |
| 278 | if (evsel->handler.func != NULL) |
| 279 | goto out; |
| 280 | evsel->handler.func = assocs[i].handler; |
| 281 | } |
| 282 | |
| 283 | err = 0; |
| 284 | out: |
| 285 | return err; |
| 286 | } |
| 287 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame^] | 288 | int perf_evlist__add_newtp(struct perf_evlist *evlist, |
| 289 | const char *sys, const char *name, void *handler) |
| 290 | { |
| 291 | struct perf_evsel *evsel; |
| 292 | |
| 293 | evsel = perf_evsel__newtp(sys, name, evlist->nr_entries); |
| 294 | if (evsel == NULL) |
| 295 | return -1; |
| 296 | |
| 297 | evsel->handler.func = handler; |
| 298 | perf_evlist__add(evlist, evsel); |
| 299 | return 0; |
| 300 | } |
| 301 | |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 302 | void perf_evlist__disable(struct perf_evlist *evlist) |
| 303 | { |
| 304 | int cpu, thread; |
| 305 | struct perf_evsel *pos; |
| 306 | |
| 307 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 308 | list_for_each_entry(pos, &evlist->entries, node) { |
| 309 | for (thread = 0; thread < evlist->threads->nr; thread++) |
Namhyung Kim | 55da800 | 2012-05-31 14:51:46 +0900 | [diff] [blame] | 310 | ioctl(FD(pos, cpu, thread), |
| 311 | PERF_EVENT_IOC_DISABLE, 0); |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 316 | void perf_evlist__enable(struct perf_evlist *evlist) |
| 317 | { |
| 318 | int cpu, thread; |
| 319 | struct perf_evsel *pos; |
| 320 | |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 321 | for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) { |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 322 | list_for_each_entry(pos, &evlist->entries, node) { |
| 323 | for (thread = 0; thread < evlist->threads->nr; thread++) |
Namhyung Kim | 55da800 | 2012-05-31 14:51:46 +0900 | [diff] [blame] | 324 | ioctl(FD(pos, cpu, thread), |
| 325 | PERF_EVENT_IOC_ENABLE, 0); |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Arnaldo Carvalho de Melo | 806fb63 | 2011-11-29 08:05:52 -0200 | [diff] [blame] | 330 | static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 331 | { |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 332 | int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries; |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 333 | evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); |
| 334 | return evlist->pollfd != NULL ? 0 : -ENOMEM; |
| 335 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 336 | |
| 337 | void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) |
| 338 | { |
| 339 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 340 | evlist->pollfd[evlist->nr_fds].fd = fd; |
| 341 | evlist->pollfd[evlist->nr_fds].events = POLLIN; |
| 342 | evlist->nr_fds++; |
| 343 | } |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 344 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 345 | static void perf_evlist__id_hash(struct perf_evlist *evlist, |
| 346 | struct perf_evsel *evsel, |
| 347 | int cpu, int thread, u64 id) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 348 | { |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 349 | int hash; |
| 350 | struct perf_sample_id *sid = SID(evsel, cpu, thread); |
| 351 | |
| 352 | sid->id = id; |
| 353 | sid->evsel = evsel; |
| 354 | hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); |
| 355 | hlist_add_head(&sid->node, &evlist->heads[hash]); |
| 356 | } |
| 357 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 358 | void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, |
| 359 | int cpu, int thread, u64 id) |
| 360 | { |
| 361 | perf_evlist__id_hash(evlist, evsel, cpu, thread, id); |
| 362 | evsel->id[evsel->ids++] = id; |
| 363 | } |
| 364 | |
| 365 | static int perf_evlist__id_add_fd(struct perf_evlist *evlist, |
| 366 | struct perf_evsel *evsel, |
| 367 | int cpu, int thread, int fd) |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 368 | { |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 369 | u64 read_data[4] = { 0, }; |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 370 | int id_idx = 1; /* The first entry is the counter value */ |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 371 | |
| 372 | if (!(evsel->attr.read_format & PERF_FORMAT_ID) || |
| 373 | read(fd, &read_data, sizeof(read_data)) == -1) |
| 374 | return -1; |
| 375 | |
| 376 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 377 | ++id_idx; |
| 378 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 379 | ++id_idx; |
| 380 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 381 | perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 382 | return 0; |
| 383 | } |
| 384 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 385 | struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) |
| 386 | { |
| 387 | struct hlist_head *head; |
| 388 | struct hlist_node *pos; |
| 389 | struct perf_sample_id *sid; |
| 390 | int hash; |
| 391 | |
| 392 | if (evlist->nr_entries == 1) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 393 | return perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 394 | |
| 395 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
| 396 | head = &evlist->heads[hash]; |
| 397 | |
| 398 | hlist_for_each_entry(sid, pos, head, node) |
| 399 | if (sid->id == id) |
| 400 | return sid->evsel; |
Namhyung Kim | 30e68bc | 2012-02-20 10:47:26 +0900 | [diff] [blame] | 401 | |
| 402 | if (!perf_evlist__sample_id_all(evlist)) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 403 | return perf_evlist__first(evlist); |
Namhyung Kim | 30e68bc | 2012-02-20 10:47:26 +0900 | [diff] [blame] | 404 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 405 | return NULL; |
| 406 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 407 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 408 | 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] | 409 | { |
| 410 | /* XXX Move this to perf.c, making it generally available */ |
| 411 | unsigned int page_size = sysconf(_SC_PAGE_SIZE); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 412 | struct perf_mmap *md = &evlist->mmap[idx]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 413 | unsigned int head = perf_mmap__read_head(md); |
| 414 | unsigned int old = md->prev; |
| 415 | unsigned char *data = md->base + page_size; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 416 | union perf_event *event = NULL; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 417 | |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 418 | if (evlist->overwrite) { |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 419 | /* |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 420 | * If we're further behind than half the buffer, there's a chance |
| 421 | * the writer will bite our tail and mess up the samples under us. |
| 422 | * |
| 423 | * If we somehow ended up ahead of the head, we got messed up. |
| 424 | * |
| 425 | * In either case, truncate and restart at head. |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 426 | */ |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 427 | int diff = head - old; |
| 428 | if (diff > md->mask / 2 || diff < 0) { |
| 429 | fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); |
| 430 | |
| 431 | /* |
| 432 | * head points to a known good entry, start there. |
| 433 | */ |
| 434 | old = head; |
| 435 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if (old != head) { |
| 439 | size_t size; |
| 440 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 441 | event = (union perf_event *)&data[old & md->mask]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 442 | size = event->header.size; |
| 443 | |
| 444 | /* |
| 445 | * Event straddles the mmap boundary -- header should always |
| 446 | * be inside due to u64 alignment of output. |
| 447 | */ |
| 448 | if ((old & md->mask) + size != ((old + size) & md->mask)) { |
| 449 | unsigned int offset = old; |
| 450 | unsigned int len = min(sizeof(*event), size), cpy; |
| 451 | void *dst = &evlist->event_copy; |
| 452 | |
| 453 | do { |
| 454 | cpy = min(md->mask + 1 - (offset & md->mask), len); |
| 455 | memcpy(dst, &data[offset & md->mask], cpy); |
| 456 | offset += cpy; |
| 457 | dst += cpy; |
| 458 | len -= cpy; |
| 459 | } while (len); |
| 460 | |
| 461 | event = &evlist->event_copy; |
| 462 | } |
| 463 | |
| 464 | old += size; |
| 465 | } |
| 466 | |
| 467 | md->prev = old; |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 468 | |
| 469 | if (!evlist->overwrite) |
| 470 | perf_mmap__write_tail(md, old); |
| 471 | |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 472 | return event; |
| 473 | } |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 474 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 475 | void perf_evlist__munmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 476 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 477 | int i; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 478 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 479 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 480 | if (evlist->mmap[i].base != NULL) { |
| 481 | munmap(evlist->mmap[i].base, evlist->mmap_len); |
| 482 | evlist->mmap[i].base = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 483 | } |
| 484 | } |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 485 | |
| 486 | free(evlist->mmap); |
| 487 | evlist->mmap = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 488 | } |
| 489 | |
Arnaldo Carvalho de Melo | 806fb63 | 2011-11-29 08:05:52 -0200 | [diff] [blame] | 490 | static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 491 | { |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 492 | evlist->nr_mmaps = cpu_map__nr(evlist->cpus); |
| 493 | if (cpu_map__all(evlist->cpus)) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 494 | evlist->nr_mmaps = evlist->threads->nr; |
| 495 | evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 496 | return evlist->mmap != NULL ? 0 : -ENOMEM; |
| 497 | } |
| 498 | |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 499 | static int __perf_evlist__mmap(struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 500 | int idx, int prot, int mask, int fd) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 501 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 502 | evlist->mmap[idx].prev = 0; |
| 503 | evlist->mmap[idx].mask = mask; |
| 504 | evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 505 | MAP_SHARED, fd, 0); |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 506 | if (evlist->mmap[idx].base == MAP_FAILED) { |
| 507 | evlist->mmap[idx].base = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 508 | return -1; |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 509 | } |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 510 | |
| 511 | perf_evlist__add_pollfd(evlist, fd); |
| 512 | return 0; |
| 513 | } |
| 514 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 515 | static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) |
| 516 | { |
| 517 | struct perf_evsel *evsel; |
| 518 | int cpu, thread; |
| 519 | |
| 520 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 521 | int output = -1; |
| 522 | |
| 523 | for (thread = 0; thread < evlist->threads->nr; thread++) { |
| 524 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 525 | int fd = FD(evsel, cpu, thread); |
| 526 | |
| 527 | if (output == -1) { |
| 528 | output = fd; |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 529 | if (__perf_evlist__mmap(evlist, cpu, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 530 | prot, mask, output) < 0) |
| 531 | goto out_unmap; |
| 532 | } else { |
| 533 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) |
| 534 | goto out_unmap; |
| 535 | } |
| 536 | |
| 537 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 538 | perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) |
| 539 | goto out_unmap; |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | return 0; |
| 545 | |
| 546 | out_unmap: |
| 547 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 548 | if (evlist->mmap[cpu].base != NULL) { |
| 549 | munmap(evlist->mmap[cpu].base, evlist->mmap_len); |
| 550 | evlist->mmap[cpu].base = NULL; |
| 551 | } |
| 552 | } |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) |
| 557 | { |
| 558 | struct perf_evsel *evsel; |
| 559 | int thread; |
| 560 | |
| 561 | for (thread = 0; thread < evlist->threads->nr; thread++) { |
| 562 | int output = -1; |
| 563 | |
| 564 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 565 | int fd = FD(evsel, 0, thread); |
| 566 | |
| 567 | if (output == -1) { |
| 568 | output = fd; |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 569 | if (__perf_evlist__mmap(evlist, thread, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 570 | prot, mask, output) < 0) |
| 571 | goto out_unmap; |
| 572 | } else { |
| 573 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) |
| 574 | goto out_unmap; |
| 575 | } |
| 576 | |
| 577 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 578 | perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) |
| 579 | goto out_unmap; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | return 0; |
| 584 | |
| 585 | out_unmap: |
| 586 | for (thread = 0; thread < evlist->threads->nr; thread++) { |
| 587 | if (evlist->mmap[thread].base != NULL) { |
| 588 | munmap(evlist->mmap[thread].base, evlist->mmap_len); |
| 589 | evlist->mmap[thread].base = NULL; |
| 590 | } |
| 591 | } |
| 592 | return -1; |
| 593 | } |
| 594 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 595 | /** perf_evlist__mmap - Create per cpu maps to receive events |
| 596 | * |
| 597 | * @evlist - list of events |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 598 | * @pages - map length in pages |
| 599 | * @overwrite - overwrite older events? |
| 600 | * |
| 601 | * If overwrite is false the user needs to signal event consuption using: |
| 602 | * |
| 603 | * struct perf_mmap *m = &evlist->mmap[cpu]; |
| 604 | * unsigned int head = perf_mmap__read_head(m); |
| 605 | * |
| 606 | * perf_mmap__write_tail(m, head) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 607 | * |
| 608 | * Using perf_evlist__read_on_cpu does this automatically. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 609 | */ |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 610 | int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, |
| 611 | bool overwrite) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 612 | { |
| 613 | unsigned int page_size = sysconf(_SC_PAGE_SIZE); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 614 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 615 | const struct cpu_map *cpus = evlist->cpus; |
| 616 | const struct thread_map *threads = evlist->threads; |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 617 | int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; |
| 618 | |
| 619 | /* 512 kiB: default amount of unprivileged mlocked memory */ |
| 620 | if (pages == UINT_MAX) |
| 621 | pages = (512 * 1024) / page_size; |
Nelson Elhage | 41d0d93 | 2011-12-19 08:39:32 -0500 | [diff] [blame] | 622 | else if (!is_power_of_2(pages)) |
| 623 | return -EINVAL; |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 624 | |
| 625 | mask = pages * page_size - 1; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 626 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 627 | if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 628 | return -ENOMEM; |
| 629 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 630 | if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 631 | return -ENOMEM; |
| 632 | |
| 633 | evlist->overwrite = overwrite; |
| 634 | evlist->mmap_len = (pages + 1) * page_size; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 635 | |
| 636 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 637 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 638 | evsel->sample_id == NULL && |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 639 | 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] | 640 | return -ENOMEM; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 641 | } |
| 642 | |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 643 | if (cpu_map__all(cpus)) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 644 | return perf_evlist__mmap_per_thread(evlist, prot, mask); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 645 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 646 | return perf_evlist__mmap_per_cpu(evlist, prot, mask); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 647 | } |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 648 | |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 649 | int perf_evlist__create_maps(struct perf_evlist *evlist, |
| 650 | struct perf_target *target) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 651 | { |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 652 | evlist->threads = thread_map__new_str(target->pid, target->tid, |
| 653 | target->uid); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 654 | |
| 655 | if (evlist->threads == NULL) |
| 656 | return -1; |
| 657 | |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 658 | if (perf_target__has_task(target)) |
Namhyung Kim | d67356e | 2012-05-07 14:09:03 +0900 | [diff] [blame] | 659 | evlist->cpus = cpu_map__dummy_new(); |
Namhyung Kim | d1cb9fc | 2012-05-16 18:45:49 +0900 | [diff] [blame] | 660 | else if (!perf_target__has_cpu(target) && !target->uses_mmap) |
| 661 | evlist->cpus = cpu_map__dummy_new(); |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 662 | else |
| 663 | evlist->cpus = cpu_map__new(target->cpu_list); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 664 | |
| 665 | if (evlist->cpus == NULL) |
| 666 | goto out_delete_threads; |
| 667 | |
| 668 | return 0; |
| 669 | |
| 670 | out_delete_threads: |
| 671 | thread_map__delete(evlist->threads); |
| 672 | return -1; |
| 673 | } |
| 674 | |
| 675 | void perf_evlist__delete_maps(struct perf_evlist *evlist) |
| 676 | { |
| 677 | cpu_map__delete(evlist->cpus); |
| 678 | thread_map__delete(evlist->threads); |
| 679 | evlist->cpus = NULL; |
| 680 | evlist->threads = NULL; |
| 681 | } |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 682 | |
Arnaldo Carvalho de Melo | 1491a63 | 2012-09-26 14:43:13 -0300 | [diff] [blame] | 683 | int perf_evlist__apply_filters(struct perf_evlist *evlist) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 684 | { |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 685 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 686 | int err = 0; |
| 687 | const int ncpus = cpu_map__nr(evlist->cpus), |
| 688 | nthreads = evlist->threads->nr; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 689 | |
| 690 | list_for_each_entry(evsel, &evlist->entries, node) { |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 691 | if (evsel->filter == NULL) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 692 | continue; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 693 | |
| 694 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); |
| 695 | if (err) |
| 696 | break; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 699 | return err; |
| 700 | } |
| 701 | |
| 702 | int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) |
| 703 | { |
| 704 | struct perf_evsel *evsel; |
| 705 | int err = 0; |
| 706 | const int ncpus = cpu_map__nr(evlist->cpus), |
| 707 | nthreads = evlist->threads->nr; |
| 708 | |
| 709 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 710 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); |
| 711 | if (err) |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | return err; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 716 | } |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 717 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 718 | bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 719 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 720 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 721 | |
| 722 | list_for_each_entry_continue(pos, &evlist->entries, node) { |
| 723 | if (first->attr.sample_type != pos->attr.sample_type) |
| 724 | return false; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 725 | } |
| 726 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 727 | return true; |
| 728 | } |
| 729 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 730 | u64 perf_evlist__sample_type(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 731 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 732 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 733 | return first->attr.sample_type; |
| 734 | } |
| 735 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 736 | u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 737 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 738 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 739 | struct perf_sample *data; |
| 740 | u64 sample_type; |
| 741 | u16 size = 0; |
| 742 | |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 743 | if (!first->attr.sample_id_all) |
| 744 | goto out; |
| 745 | |
| 746 | sample_type = first->attr.sample_type; |
| 747 | |
| 748 | if (sample_type & PERF_SAMPLE_TID) |
| 749 | size += sizeof(data->tid) * 2; |
| 750 | |
| 751 | if (sample_type & PERF_SAMPLE_TIME) |
| 752 | size += sizeof(data->time); |
| 753 | |
| 754 | if (sample_type & PERF_SAMPLE_ID) |
| 755 | size += sizeof(data->id); |
| 756 | |
| 757 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 758 | size += sizeof(data->stream_id); |
| 759 | |
| 760 | if (sample_type & PERF_SAMPLE_CPU) |
| 761 | size += sizeof(data->cpu) * 2; |
| 762 | out: |
| 763 | return size; |
| 764 | } |
| 765 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 766 | 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] | 767 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 768 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 769 | |
| 770 | list_for_each_entry_continue(pos, &evlist->entries, node) { |
| 771 | if (first->attr.sample_id_all != pos->attr.sample_id_all) |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | return true; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 778 | bool perf_evlist__sample_id_all(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 779 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 780 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 781 | return first->attr.sample_id_all; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 782 | } |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 783 | |
| 784 | void perf_evlist__set_selected(struct perf_evlist *evlist, |
| 785 | struct perf_evsel *evsel) |
| 786 | { |
| 787 | evlist->selected = evsel; |
| 788 | } |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 789 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 790 | int perf_evlist__open(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 791 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 792 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 793 | int err, ncpus, nthreads; |
| 794 | |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 795 | list_for_each_entry(evsel, &evlist->entries, node) { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 796 | err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 797 | if (err < 0) |
| 798 | goto out_err; |
| 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | out_err: |
| 803 | ncpus = evlist->cpus ? evlist->cpus->nr : 1; |
| 804 | nthreads = evlist->threads ? evlist->threads->nr : 1; |
| 805 | |
| 806 | list_for_each_entry_reverse(evsel, &evlist->entries, node) |
| 807 | perf_evsel__close(evsel, ncpus, nthreads); |
| 808 | |
Namhyung Kim | 41c21a6 | 2012-02-23 12:13:36 +0900 | [diff] [blame] | 809 | errno = -err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 810 | return err; |
| 811 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 812 | |
| 813 | int perf_evlist__prepare_workload(struct perf_evlist *evlist, |
| 814 | struct perf_record_opts *opts, |
| 815 | const char *argv[]) |
| 816 | { |
| 817 | int child_ready_pipe[2], go_pipe[2]; |
| 818 | char bf; |
| 819 | |
| 820 | if (pipe(child_ready_pipe) < 0) { |
| 821 | perror("failed to create 'ready' pipe"); |
| 822 | return -1; |
| 823 | } |
| 824 | |
| 825 | if (pipe(go_pipe) < 0) { |
| 826 | perror("failed to create 'go' pipe"); |
| 827 | goto out_close_ready_pipe; |
| 828 | } |
| 829 | |
| 830 | evlist->workload.pid = fork(); |
| 831 | if (evlist->workload.pid < 0) { |
| 832 | perror("failed to fork"); |
| 833 | goto out_close_pipes; |
| 834 | } |
| 835 | |
| 836 | if (!evlist->workload.pid) { |
| 837 | if (opts->pipe_output) |
| 838 | dup2(2, 1); |
| 839 | |
| 840 | close(child_ready_pipe[0]); |
| 841 | close(go_pipe[1]); |
| 842 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); |
| 843 | |
| 844 | /* |
| 845 | * Do a dummy execvp to get the PLT entry resolved, |
| 846 | * so we avoid the resolver overhead on the real |
| 847 | * execvp call. |
| 848 | */ |
| 849 | execvp("", (char **)argv); |
| 850 | |
| 851 | /* |
| 852 | * Tell the parent we're ready to go |
| 853 | */ |
| 854 | close(child_ready_pipe[1]); |
| 855 | |
| 856 | /* |
| 857 | * Wait until the parent tells us to go. |
| 858 | */ |
| 859 | if (read(go_pipe[0], &bf, 1) == -1) |
| 860 | perror("unable to read pipe"); |
| 861 | |
| 862 | execvp(argv[0], (char **)argv); |
| 863 | |
| 864 | perror(argv[0]); |
| 865 | kill(getppid(), SIGUSR1); |
| 866 | exit(-1); |
| 867 | } |
| 868 | |
Namhyung Kim | d67356e | 2012-05-07 14:09:03 +0900 | [diff] [blame] | 869 | if (perf_target__none(&opts->target)) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 870 | evlist->threads->map[0] = evlist->workload.pid; |
| 871 | |
| 872 | close(child_ready_pipe[1]); |
| 873 | close(go_pipe[0]); |
| 874 | /* |
| 875 | * wait for child to settle |
| 876 | */ |
| 877 | if (read(child_ready_pipe[0], &bf, 1) == -1) { |
| 878 | perror("unable to read pipe"); |
| 879 | goto out_close_pipes; |
| 880 | } |
| 881 | |
| 882 | evlist->workload.cork_fd = go_pipe[1]; |
| 883 | close(child_ready_pipe[0]); |
| 884 | return 0; |
| 885 | |
| 886 | out_close_pipes: |
| 887 | close(go_pipe[0]); |
| 888 | close(go_pipe[1]); |
| 889 | out_close_ready_pipe: |
| 890 | close(child_ready_pipe[0]); |
| 891 | close(child_ready_pipe[1]); |
| 892 | return -1; |
| 893 | } |
| 894 | |
| 895 | int perf_evlist__start_workload(struct perf_evlist *evlist) |
| 896 | { |
| 897 | if (evlist->workload.cork_fd > 0) { |
| 898 | /* |
| 899 | * Remove the cork, let it rip! |
| 900 | */ |
| 901 | return close(evlist->workload.cork_fd); |
| 902 | } |
| 903 | |
| 904 | return 0; |
| 905 | } |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 906 | |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 907 | 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] | 908 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 909 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 910 | struct perf_evsel *evsel = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 911 | return perf_evsel__parse_sample(evsel, event, sample); |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 912 | } |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 913 | |
| 914 | size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) |
| 915 | { |
| 916 | struct perf_evsel *evsel; |
| 917 | size_t printed = 0; |
| 918 | |
| 919 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 920 | printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", |
| 921 | perf_evsel__name(evsel)); |
| 922 | } |
| 923 | |
| 924 | return printed + fprintf(fp, "\n");; |
| 925 | } |