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