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