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