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