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