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