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 | 553873e | 2013-12-09 17:14:23 +0100 | [diff] [blame] | 10 | #include <api/fs/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" |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 21 | #include "parse-options.h" |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 25 | #include <linux/bitops.h> |
| 26 | #include <linux/hash.h> |
| 27 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 28 | #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] | 29 | #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] | 30 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 31 | void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, |
| 32 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 33 | { |
| 34 | int i; |
| 35 | |
| 36 | for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) |
| 37 | INIT_HLIST_HEAD(&evlist->heads[i]); |
| 38 | INIT_LIST_HEAD(&evlist->entries); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 39 | perf_evlist__set_maps(evlist, cpus, threads); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 40 | evlist->workload.pid = -1; |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 41 | } |
| 42 | |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 43 | struct perf_evlist *perf_evlist__new(void) |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 44 | { |
| 45 | struct perf_evlist *evlist = zalloc(sizeof(*evlist)); |
| 46 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 47 | if (evlist != NULL) |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 48 | perf_evlist__init(evlist, NULL, NULL); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 49 | |
| 50 | return evlist; |
| 51 | } |
| 52 | |
Jiri Olsa | b22d54b | 2013-09-01 12:36:14 +0200 | [diff] [blame] | 53 | struct perf_evlist *perf_evlist__new_default(void) |
| 54 | { |
| 55 | struct perf_evlist *evlist = perf_evlist__new(); |
| 56 | |
| 57 | if (evlist && perf_evlist__add_default(evlist)) { |
| 58 | perf_evlist__delete(evlist); |
| 59 | evlist = NULL; |
| 60 | } |
| 61 | |
| 62 | return evlist; |
| 63 | } |
| 64 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 65 | /** |
| 66 | * perf_evlist__set_id_pos - set the positions of event ids. |
| 67 | * @evlist: selected event list |
| 68 | * |
| 69 | * Events with compatible sample types all have the same id_pos |
| 70 | * and is_pos. For convenience, put a copy on evlist. |
| 71 | */ |
| 72 | void perf_evlist__set_id_pos(struct perf_evlist *evlist) |
| 73 | { |
| 74 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 75 | |
| 76 | evlist->id_pos = first->id_pos; |
| 77 | evlist->is_pos = first->is_pos; |
| 78 | } |
| 79 | |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 80 | static void perf_evlist__update_id_pos(struct perf_evlist *evlist) |
| 81 | { |
| 82 | struct perf_evsel *evsel; |
| 83 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 84 | evlist__for_each(evlist, evsel) |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 85 | perf_evsel__calc_id_pos(evsel); |
| 86 | |
| 87 | perf_evlist__set_id_pos(evlist); |
| 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 90 | static void perf_evlist__purge(struct perf_evlist *evlist) |
| 91 | { |
| 92 | struct perf_evsel *pos, *n; |
| 93 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 94 | evlist__for_each_safe(evlist, n, pos) { |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 95 | list_del_init(&pos->node); |
| 96 | perf_evsel__delete(pos); |
| 97 | } |
| 98 | |
| 99 | evlist->nr_entries = 0; |
| 100 | } |
| 101 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 102 | void perf_evlist__exit(struct perf_evlist *evlist) |
| 103 | { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 104 | zfree(&evlist->mmap); |
| 105 | zfree(&evlist->pollfd); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 106 | } |
| 107 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 108 | void perf_evlist__delete(struct perf_evlist *evlist) |
| 109 | { |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 110 | perf_evlist__munmap(evlist); |
Arnaldo Carvalho de Melo | f26e1c7 | 2014-01-03 16:54:12 -0300 | [diff] [blame] | 111 | perf_evlist__close(evlist); |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 112 | cpu_map__delete(evlist->cpus); |
| 113 | thread_map__delete(evlist->threads); |
| 114 | evlist->cpus = NULL; |
| 115 | evlist->threads = NULL; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 116 | perf_evlist__purge(evlist); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 117 | perf_evlist__exit(evlist); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 118 | free(evlist); |
| 119 | } |
| 120 | |
| 121 | void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) |
| 122 | { |
| 123 | list_add_tail(&entry->node, &evlist->entries); |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 124 | entry->idx = evlist->nr_entries; |
Adrian Hunter | 60b0896 | 2014-07-31 09:00:52 +0300 | [diff] [blame] | 125 | entry->tracking = !entry->idx; |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 126 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 127 | if (!evlist->nr_entries++) |
| 128 | perf_evlist__set_id_pos(evlist); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 129 | } |
| 130 | |
Jiri Olsa | 0529bc1 | 2012-01-27 15:34:20 +0100 | [diff] [blame] | 131 | void perf_evlist__splice_list_tail(struct perf_evlist *evlist, |
| 132 | struct list_head *list, |
| 133 | int nr_entries) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 134 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 135 | bool set_id_pos = !evlist->nr_entries; |
| 136 | |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 137 | list_splice_tail(list, &evlist->entries); |
| 138 | evlist->nr_entries += nr_entries; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 139 | if (set_id_pos) |
| 140 | perf_evlist__set_id_pos(evlist); |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 141 | } |
| 142 | |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 143 | void __perf_evlist__set_leader(struct list_head *list) |
| 144 | { |
| 145 | struct perf_evsel *evsel, *leader; |
| 146 | |
| 147 | leader = list_entry(list->next, struct perf_evsel, node); |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 148 | evsel = list_entry(list->prev, struct perf_evsel, node); |
| 149 | |
| 150 | leader->nr_members = evsel->idx - leader->idx + 1; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 151 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 152 | __evlist__for_each(list, evsel) { |
Stephane Eranian | 74b2133 | 2013-01-31 13:54:37 +0100 | [diff] [blame] | 153 | evsel->leader = leader; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | void perf_evlist__set_leader(struct perf_evlist *evlist) |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 158 | { |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 159 | if (evlist->nr_entries) { |
| 160 | evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 161 | __perf_evlist__set_leader(&evlist->entries); |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 162 | } |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 165 | int perf_evlist__add_default(struct perf_evlist *evlist) |
| 166 | { |
| 167 | struct perf_event_attr attr = { |
| 168 | .type = PERF_TYPE_HARDWARE, |
| 169 | .config = PERF_COUNT_HW_CPU_CYCLES, |
| 170 | }; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 171 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 172 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 173 | event_attr_init(&attr); |
| 174 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 175 | evsel = perf_evsel__new(&attr); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 176 | if (evsel == NULL) |
Stephane Eranian | cc2d86b | 2011-06-07 18:19:36 +0200 | [diff] [blame] | 177 | goto error; |
| 178 | |
| 179 | /* use strdup() because free(evsel) assumes name is allocated */ |
| 180 | evsel->name = strdup("cycles"); |
| 181 | if (!evsel->name) |
| 182 | goto error_free; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 183 | |
| 184 | perf_evlist__add(evlist, evsel); |
| 185 | return 0; |
Stephane Eranian | cc2d86b | 2011-06-07 18:19:36 +0200 | [diff] [blame] | 186 | error_free: |
| 187 | perf_evsel__delete(evsel); |
| 188 | error: |
| 189 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 190 | } |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 191 | |
Arnaldo Carvalho de Melo | e60fc84 | 2012-10-03 11:50:55 -0300 | [diff] [blame] | 192 | static int perf_evlist__add_attrs(struct perf_evlist *evlist, |
| 193 | struct perf_event_attr *attrs, size_t nr_attrs) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 194 | { |
| 195 | struct perf_evsel *evsel, *n; |
| 196 | LIST_HEAD(head); |
| 197 | size_t i; |
| 198 | |
| 199 | for (i = 0; i < nr_attrs; i++) { |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 200 | evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 201 | if (evsel == NULL) |
| 202 | goto out_delete_partial_list; |
| 203 | list_add_tail(&evsel->node, &head); |
| 204 | } |
| 205 | |
| 206 | perf_evlist__splice_list_tail(evlist, &head, nr_attrs); |
| 207 | |
| 208 | return 0; |
| 209 | |
| 210 | out_delete_partial_list: |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 211 | __evlist__for_each_safe(&head, n, evsel) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 212 | perf_evsel__delete(evsel); |
| 213 | return -1; |
| 214 | } |
| 215 | |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 216 | int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, |
| 217 | struct perf_event_attr *attrs, size_t nr_attrs) |
| 218 | { |
| 219 | size_t i; |
| 220 | |
| 221 | for (i = 0; i < nr_attrs; i++) |
| 222 | event_attr_init(attrs + i); |
| 223 | |
| 224 | return perf_evlist__add_attrs(evlist, attrs, nr_attrs); |
| 225 | } |
| 226 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 227 | struct perf_evsel * |
| 228 | 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] | 229 | { |
| 230 | struct perf_evsel *evsel; |
| 231 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 232 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | ee29be6 | 2011-11-28 17:57:40 -0200 | [diff] [blame] | 233 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && |
| 234 | (int)evsel->attr.config == id) |
| 235 | return evsel; |
| 236 | } |
| 237 | |
| 238 | return NULL; |
| 239 | } |
| 240 | |
David Ahern | a2f2804 | 2013-08-28 22:29:51 -0600 | [diff] [blame] | 241 | struct perf_evsel * |
| 242 | perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, |
| 243 | const char *name) |
| 244 | { |
| 245 | struct perf_evsel *evsel; |
| 246 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 247 | evlist__for_each(evlist, evsel) { |
David Ahern | a2f2804 | 2013-08-28 22:29:51 -0600 | [diff] [blame] | 248 | if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && |
| 249 | (strcmp(evsel->name, name) == 0)) |
| 250 | return evsel; |
| 251 | } |
| 252 | |
| 253 | return NULL; |
| 254 | } |
| 255 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 256 | int perf_evlist__add_newtp(struct perf_evlist *evlist, |
| 257 | const char *sys, const char *name, void *handler) |
| 258 | { |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 259 | struct perf_evsel *evsel = perf_evsel__newtp(sys, name); |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 260 | |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 261 | if (evsel == NULL) |
| 262 | return -1; |
| 263 | |
Arnaldo Carvalho de Melo | 744a971 | 2013-11-06 10:17:38 -0300 | [diff] [blame] | 264 | evsel->handler = handler; |
Arnaldo Carvalho de Melo | 39876e7 | 2012-10-03 11:40:22 -0300 | [diff] [blame] | 265 | perf_evlist__add(evlist, evsel); |
| 266 | return 0; |
| 267 | } |
| 268 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 269 | static int perf_evlist__nr_threads(struct perf_evlist *evlist, |
| 270 | struct perf_evsel *evsel) |
| 271 | { |
| 272 | if (evsel->system_wide) |
| 273 | return 1; |
| 274 | else |
| 275 | return thread_map__nr(evlist->threads); |
| 276 | } |
| 277 | |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 278 | void perf_evlist__disable(struct perf_evlist *evlist) |
| 279 | { |
| 280 | int cpu, thread; |
| 281 | struct perf_evsel *pos; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 282 | int nr_cpus = cpu_map__nr(evlist->cpus); |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 283 | int nr_threads; |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 284 | |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 285 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 286 | evlist__for_each(evlist, pos) { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 287 | if (!perf_evsel__is_group_leader(pos) || !pos->fd) |
Jiri Olsa | 3fe4430d | 2012-11-12 18:34:03 +0100 | [diff] [blame] | 288 | continue; |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 289 | nr_threads = perf_evlist__nr_threads(evlist, pos); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 290 | for (thread = 0; thread < nr_threads; thread++) |
Namhyung Kim | 55da800 | 2012-05-31 14:51:46 +0900 | [diff] [blame] | 291 | ioctl(FD(pos, cpu, thread), |
| 292 | PERF_EVENT_IOC_DISABLE, 0); |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 297 | void perf_evlist__enable(struct perf_evlist *evlist) |
| 298 | { |
| 299 | int cpu, thread; |
| 300 | struct perf_evsel *pos; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 301 | int nr_cpus = cpu_map__nr(evlist->cpus); |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 302 | int nr_threads; |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 303 | |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 304 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 305 | evlist__for_each(evlist, pos) { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 306 | if (!perf_evsel__is_group_leader(pos) || !pos->fd) |
Jiri Olsa | 3fe4430d | 2012-11-12 18:34:03 +0100 | [diff] [blame] | 307 | continue; |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 308 | nr_threads = perf_evlist__nr_threads(evlist, pos); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 309 | for (thread = 0; thread < nr_threads; thread++) |
Namhyung Kim | 55da800 | 2012-05-31 14:51:46 +0900 | [diff] [blame] | 310 | ioctl(FD(pos, cpu, thread), |
| 311 | PERF_EVENT_IOC_ENABLE, 0); |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 316 | int perf_evlist__disable_event(struct perf_evlist *evlist, |
| 317 | struct perf_evsel *evsel) |
| 318 | { |
| 319 | int cpu, thread, err; |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 320 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 321 | int nr_threads = perf_evlist__nr_threads(evlist, evsel); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 322 | |
| 323 | if (!evsel->fd) |
| 324 | return 0; |
| 325 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 326 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
| 327 | for (thread = 0; thread < nr_threads; thread++) { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 328 | err = ioctl(FD(evsel, cpu, thread), |
| 329 | PERF_EVENT_IOC_DISABLE, 0); |
| 330 | if (err) |
| 331 | return err; |
| 332 | } |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | int perf_evlist__enable_event(struct perf_evlist *evlist, |
| 338 | struct perf_evsel *evsel) |
| 339 | { |
| 340 | int cpu, thread, err; |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 341 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 342 | int nr_threads = perf_evlist__nr_threads(evlist, evsel); |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 343 | |
| 344 | if (!evsel->fd) |
| 345 | return -EINVAL; |
| 346 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 347 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
| 348 | for (thread = 0; thread < nr_threads; thread++) { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 349 | err = ioctl(FD(evsel, cpu, thread), |
| 350 | PERF_EVENT_IOC_ENABLE, 0); |
| 351 | if (err) |
| 352 | return err; |
| 353 | } |
| 354 | } |
| 355 | return 0; |
| 356 | } |
| 357 | |
Adrian Hunter | 1c65056 | 2014-07-31 09:00:56 +0300 | [diff] [blame] | 358 | static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, |
| 359 | struct perf_evsel *evsel, int cpu) |
| 360 | { |
| 361 | int thread, err; |
| 362 | int nr_threads = perf_evlist__nr_threads(evlist, evsel); |
| 363 | |
| 364 | if (!evsel->fd) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | for (thread = 0; thread < nr_threads; thread++) { |
| 368 | err = ioctl(FD(evsel, cpu, thread), |
| 369 | PERF_EVENT_IOC_ENABLE, 0); |
| 370 | if (err) |
| 371 | return err; |
| 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, |
| 377 | struct perf_evsel *evsel, |
| 378 | int thread) |
| 379 | { |
| 380 | int cpu, err; |
| 381 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 382 | |
| 383 | if (!evsel->fd) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
| 387 | err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); |
| 388 | if (err) |
| 389 | return err; |
| 390 | } |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | int perf_evlist__enable_event_idx(struct perf_evlist *evlist, |
| 395 | struct perf_evsel *evsel, int idx) |
| 396 | { |
| 397 | bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); |
| 398 | |
| 399 | if (per_cpu_mmaps) |
| 400 | return perf_evlist__enable_event_cpu(evlist, evsel, idx); |
| 401 | else |
| 402 | return perf_evlist__enable_event_thread(evlist, evsel, idx); |
| 403 | } |
| 404 | |
Arnaldo Carvalho de Melo | 806fb63 | 2011-11-29 08:05:52 -0200 | [diff] [blame] | 405 | static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 406 | { |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 407 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 408 | int nr_threads = thread_map__nr(evlist->threads); |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 409 | int nfds = 0; |
| 410 | struct perf_evsel *evsel; |
| 411 | |
| 412 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 413 | if (evsel->system_wide) |
| 414 | nfds += nr_cpus; |
| 415 | else |
| 416 | nfds += nr_cpus * nr_threads; |
| 417 | } |
| 418 | |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 419 | evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); |
| 420 | return evlist->pollfd != NULL ? 0 : -ENOMEM; |
| 421 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 422 | |
| 423 | void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) |
| 424 | { |
| 425 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 426 | evlist->pollfd[evlist->nr_fds].fd = fd; |
Arnaldo Carvalho de Melo | 8179672 | 2014-08-13 11:26:21 -0300 | [diff] [blame^] | 427 | evlist->pollfd[evlist->nr_fds].events = POLLIN | POLLERR | POLLHUP; |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 428 | evlist->nr_fds++; |
| 429 | } |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 430 | |
Arnaldo Carvalho de Melo | 1ddec7f | 2014-08-12 23:04:11 -0300 | [diff] [blame] | 431 | int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) |
| 432 | { |
| 433 | int fd, nr_fds = 0; |
| 434 | |
| 435 | if (evlist->nr_fds == 0) |
| 436 | return 0; |
| 437 | |
| 438 | for (fd = 0; fd < evlist->nr_fds; ++fd) { |
| 439 | if (evlist->pollfd[fd].revents & revents_and_mask) |
| 440 | continue; |
| 441 | |
| 442 | if (fd != nr_fds) |
| 443 | evlist->pollfd[nr_fds] = evlist->pollfd[fd]; |
| 444 | |
| 445 | ++nr_fds; |
| 446 | } |
| 447 | |
| 448 | evlist->nr_fds = nr_fds; |
| 449 | return nr_fds; |
| 450 | } |
| 451 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 452 | static void perf_evlist__id_hash(struct perf_evlist *evlist, |
| 453 | struct perf_evsel *evsel, |
| 454 | int cpu, int thread, u64 id) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 455 | { |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 456 | int hash; |
| 457 | struct perf_sample_id *sid = SID(evsel, cpu, thread); |
| 458 | |
| 459 | sid->id = id; |
| 460 | sid->evsel = evsel; |
| 461 | hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); |
| 462 | hlist_add_head(&sid->node, &evlist->heads[hash]); |
| 463 | } |
| 464 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 465 | void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, |
| 466 | int cpu, int thread, u64 id) |
| 467 | { |
| 468 | perf_evlist__id_hash(evlist, evsel, cpu, thread, id); |
| 469 | evsel->id[evsel->ids++] = id; |
| 470 | } |
| 471 | |
| 472 | static int perf_evlist__id_add_fd(struct perf_evlist *evlist, |
| 473 | struct perf_evsel *evsel, |
| 474 | int cpu, int thread, int fd) |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 475 | { |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 476 | u64 read_data[4] = { 0, }; |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 477 | int id_idx = 1; /* The first entry is the counter value */ |
Jiri Olsa | e2b5abe | 2012-04-04 19:32:27 +0200 | [diff] [blame] | 478 | u64 id; |
| 479 | int ret; |
| 480 | |
| 481 | ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); |
| 482 | if (!ret) |
| 483 | goto add; |
| 484 | |
| 485 | if (errno != ENOTTY) |
| 486 | return -1; |
| 487 | |
| 488 | /* 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] | 489 | |
Jiri Olsa | c4861af | 2012-10-12 13:02:21 +0200 | [diff] [blame] | 490 | /* |
| 491 | * This way does not work with group format read, so bail |
| 492 | * out in that case. |
| 493 | */ |
| 494 | if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) |
| 495 | return -1; |
| 496 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 497 | if (!(evsel->attr.read_format & PERF_FORMAT_ID) || |
| 498 | read(fd, &read_data, sizeof(read_data)) == -1) |
| 499 | return -1; |
| 500 | |
| 501 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 502 | ++id_idx; |
| 503 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 504 | ++id_idx; |
| 505 | |
Jiri Olsa | e2b5abe | 2012-04-04 19:32:27 +0200 | [diff] [blame] | 506 | id = read_data[id_idx]; |
| 507 | |
| 508 | add: |
| 509 | perf_evlist__id_add(evlist, evsel, cpu, thread, id); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
Jiri Olsa | 932a359 | 2012-10-11 14:10:35 +0200 | [diff] [blame] | 513 | 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] | 514 | { |
| 515 | struct hlist_head *head; |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 516 | struct perf_sample_id *sid; |
| 517 | int hash; |
| 518 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 519 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
| 520 | head = &evlist->heads[hash]; |
| 521 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 522 | hlist_for_each_entry(sid, head, node) |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 523 | if (sid->id == id) |
Jiri Olsa | 932a359 | 2012-10-11 14:10:35 +0200 | [diff] [blame] | 524 | return sid; |
| 525 | |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) |
| 530 | { |
| 531 | struct perf_sample_id *sid; |
| 532 | |
| 533 | if (evlist->nr_entries == 1) |
| 534 | return perf_evlist__first(evlist); |
| 535 | |
| 536 | sid = perf_evlist__id2sid(evlist, id); |
| 537 | if (sid) |
| 538 | return sid->evsel; |
Namhyung Kim | 30e68bc | 2012-02-20 10:47:26 +0900 | [diff] [blame] | 539 | |
| 540 | if (!perf_evlist__sample_id_all(evlist)) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 541 | return perf_evlist__first(evlist); |
Namhyung Kim | 30e68bc | 2012-02-20 10:47:26 +0900 | [diff] [blame] | 542 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 543 | return NULL; |
| 544 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 545 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 546 | static int perf_evlist__event2id(struct perf_evlist *evlist, |
| 547 | union perf_event *event, u64 *id) |
| 548 | { |
| 549 | const u64 *array = event->sample.array; |
| 550 | ssize_t n; |
| 551 | |
| 552 | n = (event->header.size - sizeof(event->header)) >> 3; |
| 553 | |
| 554 | if (event->header.type == PERF_RECORD_SAMPLE) { |
| 555 | if (evlist->id_pos >= n) |
| 556 | return -1; |
| 557 | *id = array[evlist->id_pos]; |
| 558 | } else { |
| 559 | if (evlist->is_pos > n) |
| 560 | return -1; |
| 561 | n -= evlist->is_pos; |
| 562 | *id = array[n]; |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, |
| 568 | union perf_event *event) |
| 569 | { |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 570 | struct perf_evsel *first = perf_evlist__first(evlist); |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 571 | struct hlist_head *head; |
| 572 | struct perf_sample_id *sid; |
| 573 | int hash; |
| 574 | u64 id; |
| 575 | |
| 576 | if (evlist->nr_entries == 1) |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 577 | return first; |
| 578 | |
| 579 | if (!first->attr.sample_id_all && |
| 580 | event->header.type != PERF_RECORD_SAMPLE) |
| 581 | return first; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 582 | |
| 583 | if (perf_evlist__event2id(evlist, event, &id)) |
| 584 | return NULL; |
| 585 | |
| 586 | /* Synthesized events have an id of zero */ |
| 587 | if (!id) |
Adrian Hunter | 98be696 | 2013-09-04 23:18:17 +0300 | [diff] [blame] | 588 | return first; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 589 | |
| 590 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
| 591 | head = &evlist->heads[hash]; |
| 592 | |
| 593 | hlist_for_each_entry(sid, head, node) { |
| 594 | if (sid->id == id) |
| 595 | return sid->evsel; |
| 596 | } |
| 597 | return NULL; |
| 598 | } |
| 599 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 600 | 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] | 601 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 602 | struct perf_mmap *md = &evlist->mmap[idx]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 603 | unsigned int head = perf_mmap__read_head(md); |
| 604 | unsigned int old = md->prev; |
| 605 | unsigned char *data = md->base + page_size; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 606 | union perf_event *event = NULL; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 607 | |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 608 | if (evlist->overwrite) { |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 609 | /* |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 610 | * If we're further behind than half the buffer, there's a chance |
| 611 | * the writer will bite our tail and mess up the samples under us. |
| 612 | * |
| 613 | * If we somehow ended up ahead of the head, we got messed up. |
| 614 | * |
| 615 | * In either case, truncate and restart at head. |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 616 | */ |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 617 | int diff = head - old; |
| 618 | if (diff > md->mask / 2 || diff < 0) { |
| 619 | fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); |
| 620 | |
| 621 | /* |
| 622 | * head points to a known good entry, start there. |
| 623 | */ |
| 624 | old = head; |
| 625 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (old != head) { |
| 629 | size_t size; |
| 630 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 631 | event = (union perf_event *)&data[old & md->mask]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 632 | size = event->header.size; |
| 633 | |
| 634 | /* |
| 635 | * Event straddles the mmap boundary -- header should always |
| 636 | * be inside due to u64 alignment of output. |
| 637 | */ |
| 638 | if ((old & md->mask) + size != ((old + size) & md->mask)) { |
| 639 | unsigned int offset = old; |
| 640 | unsigned int len = min(sizeof(*event), size), cpy; |
Jiri Olsa | a65cb4b | 2013-10-02 15:46:39 +0200 | [diff] [blame] | 641 | void *dst = md->event_copy; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 642 | |
| 643 | do { |
| 644 | cpy = min(md->mask + 1 - (offset & md->mask), len); |
| 645 | memcpy(dst, &data[offset & md->mask], cpy); |
| 646 | offset += cpy; |
| 647 | dst += cpy; |
| 648 | len -= cpy; |
| 649 | } while (len); |
| 650 | |
Jiri Olsa | a65cb4b | 2013-10-02 15:46:39 +0200 | [diff] [blame] | 651 | event = (union perf_event *) md->event_copy; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | old += size; |
| 655 | } |
| 656 | |
| 657 | md->prev = old; |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 658 | |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 659 | return event; |
| 660 | } |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 661 | |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 662 | void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx) |
| 663 | { |
| 664 | if (!evlist->overwrite) { |
| 665 | struct perf_mmap *md = &evlist->mmap[idx]; |
| 666 | unsigned int old = md->prev; |
| 667 | |
| 668 | perf_mmap__write_tail(md, old); |
| 669 | } |
| 670 | } |
| 671 | |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 672 | static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) |
| 673 | { |
| 674 | if (evlist->mmap[idx].base != NULL) { |
| 675 | munmap(evlist->mmap[idx].base, evlist->mmap_len); |
| 676 | evlist->mmap[idx].base = NULL; |
| 677 | } |
| 678 | } |
| 679 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 680 | void perf_evlist__munmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 681 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 682 | int i; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 683 | |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 684 | if (evlist->mmap == NULL) |
| 685 | return; |
| 686 | |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 687 | for (i = 0; i < evlist->nr_mmaps; i++) |
| 688 | __perf_evlist__munmap(evlist, i); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 689 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 690 | zfree(&evlist->mmap); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 691 | } |
| 692 | |
Arnaldo Carvalho de Melo | 806fb63 | 2011-11-29 08:05:52 -0200 | [diff] [blame] | 693 | static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 694 | { |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 695 | evlist->nr_mmaps = cpu_map__nr(evlist->cpus); |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 696 | if (cpu_map__empty(evlist->cpus)) |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 697 | evlist->nr_mmaps = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 698 | evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 699 | return evlist->mmap != NULL ? 0 : -ENOMEM; |
| 700 | } |
| 701 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 702 | struct mmap_params { |
| 703 | int prot; |
| 704 | int mask; |
| 705 | }; |
| 706 | |
| 707 | static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, |
| 708 | struct mmap_params *mp, int fd) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 709 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 710 | evlist->mmap[idx].prev = 0; |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 711 | evlist->mmap[idx].mask = mp->mask; |
| 712 | evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot, |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 713 | MAP_SHARED, fd, 0); |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 714 | if (evlist->mmap[idx].base == MAP_FAILED) { |
Adrian Hunter | 0263596 | 2013-11-01 15:51:33 +0200 | [diff] [blame] | 715 | pr_debug2("failed to mmap perf event ring buffer, error %d\n", |
| 716 | errno); |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 717 | evlist->mmap[idx].base = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 718 | return -1; |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 719 | } |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 720 | |
| 721 | perf_evlist__add_pollfd(evlist, fd); |
| 722 | return 0; |
| 723 | } |
| 724 | |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 725 | static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 726 | struct mmap_params *mp, int cpu, |
| 727 | int thread, int *output) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 728 | { |
| 729 | struct perf_evsel *evsel; |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 730 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 731 | evlist__for_each(evlist, evsel) { |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 732 | int fd; |
| 733 | |
| 734 | if (evsel->system_wide && thread) |
| 735 | continue; |
| 736 | |
| 737 | fd = FD(evsel, cpu, thread); |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 738 | |
| 739 | if (*output == -1) { |
| 740 | *output = fd; |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 741 | if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 742 | return -1; |
| 743 | } else { |
| 744 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) |
| 745 | return -1; |
| 746 | } |
| 747 | |
| 748 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 749 | perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) |
| 750 | return -1; |
| 751 | } |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 756 | static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, |
| 757 | struct mmap_params *mp) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 758 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 759 | int cpu, thread; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 760 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 761 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 762 | |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 763 | pr_debug2("perf event ring buffer mmapped per cpu\n"); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 764 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 765 | int output = -1; |
| 766 | |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 767 | for (thread = 0; thread < nr_threads; thread++) { |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 768 | if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, |
| 769 | thread, &output)) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 770 | goto out_unmap; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
| 774 | return 0; |
| 775 | |
| 776 | out_unmap: |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 777 | for (cpu = 0; cpu < nr_cpus; cpu++) |
| 778 | __perf_evlist__munmap(evlist, cpu); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 779 | return -1; |
| 780 | } |
| 781 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 782 | static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, |
| 783 | struct mmap_params *mp) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 784 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 785 | int thread; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 786 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 787 | |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 788 | pr_debug2("perf event ring buffer mmapped per thread\n"); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 789 | for (thread = 0; thread < nr_threads; thread++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 790 | int output = -1; |
| 791 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 792 | if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, |
| 793 | &output)) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 794 | goto out_unmap; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | return 0; |
| 798 | |
| 799 | out_unmap: |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 800 | for (thread = 0; thread < nr_threads; thread++) |
| 801 | __perf_evlist__munmap(evlist, thread); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 802 | return -1; |
| 803 | } |
| 804 | |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 805 | static size_t perf_evlist__mmap_size(unsigned long pages) |
| 806 | { |
| 807 | /* 512 kiB: default amount of unprivileged mlocked memory */ |
| 808 | if (pages == UINT_MAX) |
| 809 | pages = (512 * 1024) / page_size; |
| 810 | else if (!is_power_of_2(pages)) |
| 811 | return 0; |
| 812 | |
| 813 | return (pages + 1) * page_size; |
| 814 | } |
| 815 | |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 816 | static long parse_pages_arg(const char *str, unsigned long min, |
| 817 | unsigned long max) |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 818 | { |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 819 | unsigned long pages, val; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 820 | static struct parse_tag tags[] = { |
| 821 | { .tag = 'B', .mult = 1 }, |
| 822 | { .tag = 'K', .mult = 1 << 10 }, |
| 823 | { .tag = 'M', .mult = 1 << 20 }, |
| 824 | { .tag = 'G', .mult = 1 << 30 }, |
| 825 | { .tag = 0 }, |
| 826 | }; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 827 | |
David Ahern | 8973504 | 2013-11-12 07:46:53 -0700 | [diff] [blame] | 828 | if (str == NULL) |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 829 | return -EINVAL; |
David Ahern | 8973504 | 2013-11-12 07:46:53 -0700 | [diff] [blame] | 830 | |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 831 | val = parse_tag_value(str, tags); |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 832 | if (val != (unsigned long) -1) { |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 833 | /* we got file size value */ |
| 834 | pages = PERF_ALIGN(val, page_size) / page_size; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 835 | } else { |
| 836 | /* we got pages count value */ |
| 837 | char *eptr; |
| 838 | pages = strtoul(str, &eptr, 10); |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 839 | if (*eptr != '\0') |
| 840 | return -EINVAL; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Adrian Hunter | 2bcab6c | 2013-12-09 15:18:37 +0200 | [diff] [blame] | 843 | if (pages == 0 && min == 0) { |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 844 | /* leave number of pages at 0 */ |
Adrian Hunter | 1dbfa938 | 2013-12-09 15:18:39 +0200 | [diff] [blame] | 845 | } else if (!is_power_of_2(pages)) { |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 846 | /* round pages up to next power of 2 */ |
Adrian Hunter | 1dbfa938 | 2013-12-09 15:18:39 +0200 | [diff] [blame] | 847 | pages = next_pow2_l(pages); |
| 848 | if (!pages) |
| 849 | return -EINVAL; |
David Ahern | 9639837 | 2013-11-12 07:46:54 -0700 | [diff] [blame] | 850 | pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", |
| 851 | pages * page_size, pages); |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 852 | } |
| 853 | |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 854 | if (pages > max) |
| 855 | return -EINVAL; |
| 856 | |
| 857 | return pages; |
| 858 | } |
| 859 | |
| 860 | int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, |
| 861 | int unset __maybe_unused) |
| 862 | { |
| 863 | unsigned int *mmap_pages = opt->value; |
| 864 | unsigned long max = UINT_MAX; |
| 865 | long pages; |
| 866 | |
Adrian Hunter | f5ae9c4 | 2013-12-09 15:18:38 +0200 | [diff] [blame] | 867 | if (max > SIZE_MAX / page_size) |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 868 | max = SIZE_MAX / page_size; |
| 869 | |
| 870 | pages = parse_pages_arg(str, 1, max); |
| 871 | if (pages < 0) { |
| 872 | pr_err("Invalid argument for --mmap_pages/-m\n"); |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 873 | return -1; |
| 874 | } |
| 875 | |
| 876 | *mmap_pages = pages; |
| 877 | return 0; |
| 878 | } |
| 879 | |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 880 | /** |
| 881 | * perf_evlist__mmap - Create mmaps to receive events. |
| 882 | * @evlist: list of events |
| 883 | * @pages: map length in pages |
| 884 | * @overwrite: overwrite older events? |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 885 | * |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 886 | * If @overwrite is %false the user needs to signal event consumption using |
| 887 | * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this |
| 888 | * automatically. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 889 | * |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 890 | * Return: %0 on success, negative error code otherwise. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 891 | */ |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 892 | int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, |
| 893 | bool overwrite) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 894 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 895 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 896 | const struct cpu_map *cpus = evlist->cpus; |
| 897 | const struct thread_map *threads = evlist->threads; |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 898 | struct mmap_params mp = { |
| 899 | .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), |
| 900 | }; |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 901 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 902 | if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 903 | return -ENOMEM; |
| 904 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 905 | if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 906 | return -ENOMEM; |
| 907 | |
| 908 | evlist->overwrite = overwrite; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 909 | evlist->mmap_len = perf_evlist__mmap_size(pages); |
Adrian Hunter | 2af68ef | 2013-10-18 15:29:07 +0300 | [diff] [blame] | 910 | pr_debug("mmap size %zuB\n", evlist->mmap_len); |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 911 | mp.mask = evlist->mmap_len - page_size - 1; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 912 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 913 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 914 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 915 | evsel->sample_id == NULL && |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 916 | 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] | 917 | return -ENOMEM; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 918 | } |
| 919 | |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 920 | if (cpu_map__empty(cpus)) |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 921 | return perf_evlist__mmap_per_thread(evlist, &mp); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 922 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 923 | return perf_evlist__mmap_per_cpu(evlist, &mp); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 924 | } |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 925 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 926 | int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 927 | { |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 928 | evlist->threads = thread_map__new_str(target->pid, target->tid, |
| 929 | target->uid); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 930 | |
| 931 | if (evlist->threads == NULL) |
| 932 | return -1; |
| 933 | |
Dongsheng Yang | 9c105fb | 2013-12-04 17:56:40 -0500 | [diff] [blame] | 934 | if (target__uses_dummy_map(target)) |
Namhyung Kim | d1cb9fc | 2012-05-16 18:45:49 +0900 | [diff] [blame] | 935 | evlist->cpus = cpu_map__dummy_new(); |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 936 | else |
| 937 | evlist->cpus = cpu_map__new(target->cpu_list); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 938 | |
| 939 | if (evlist->cpus == NULL) |
| 940 | goto out_delete_threads; |
| 941 | |
| 942 | return 0; |
| 943 | |
| 944 | out_delete_threads: |
| 945 | thread_map__delete(evlist->threads); |
| 946 | return -1; |
| 947 | } |
| 948 | |
Arnaldo Carvalho de Melo | 1491a63 | 2012-09-26 14:43:13 -0300 | [diff] [blame] | 949 | int perf_evlist__apply_filters(struct perf_evlist *evlist) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 950 | { |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 951 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 952 | int err = 0; |
| 953 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 954 | nthreads = thread_map__nr(evlist->threads); |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 955 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 956 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 957 | if (evsel->filter == NULL) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 958 | continue; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 959 | |
| 960 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); |
| 961 | if (err) |
| 962 | break; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 965 | return err; |
| 966 | } |
| 967 | |
| 968 | int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) |
| 969 | { |
| 970 | struct perf_evsel *evsel; |
| 971 | int err = 0; |
| 972 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 973 | nthreads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 974 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 975 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 976 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); |
| 977 | if (err) |
| 978 | break; |
| 979 | } |
| 980 | |
| 981 | return err; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 982 | } |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 983 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 984 | bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 985 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 986 | struct perf_evsel *pos; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 987 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 988 | if (evlist->nr_entries == 1) |
| 989 | return true; |
| 990 | |
| 991 | if (evlist->id_pos < 0 || evlist->is_pos < 0) |
| 992 | return false; |
| 993 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 994 | evlist__for_each(evlist, pos) { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 995 | if (pos->id_pos != evlist->id_pos || |
| 996 | pos->is_pos != evlist->is_pos) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 997 | return false; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 998 | } |
| 999 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1000 | return true; |
| 1001 | } |
| 1002 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1003 | u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1004 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1005 | struct perf_evsel *evsel; |
| 1006 | |
| 1007 | if (evlist->combined_sample_type) |
| 1008 | return evlist->combined_sample_type; |
| 1009 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1010 | evlist__for_each(evlist, evsel) |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1011 | evlist->combined_sample_type |= evsel->attr.sample_type; |
| 1012 | |
| 1013 | return evlist->combined_sample_type; |
| 1014 | } |
| 1015 | |
| 1016 | u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
| 1017 | { |
| 1018 | evlist->combined_sample_type = 0; |
| 1019 | return __perf_evlist__combined_sample_type(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1020 | } |
| 1021 | |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1022 | bool perf_evlist__valid_read_format(struct perf_evlist *evlist) |
| 1023 | { |
| 1024 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
| 1025 | u64 read_format = first->attr.read_format; |
| 1026 | u64 sample_type = first->attr.sample_type; |
| 1027 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1028 | evlist__for_each(evlist, pos) { |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1029 | if (read_format != pos->attr.read_format) |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ |
| 1034 | if ((sample_type & PERF_SAMPLE_READ) && |
| 1035 | !(read_format & PERF_FORMAT_ID)) { |
| 1036 | return false; |
| 1037 | } |
| 1038 | |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
| 1042 | u64 perf_evlist__read_format(struct perf_evlist *evlist) |
| 1043 | { |
| 1044 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 1045 | return first->attr.read_format; |
| 1046 | } |
| 1047 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1048 | u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1049 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1050 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1051 | struct perf_sample *data; |
| 1052 | u64 sample_type; |
| 1053 | u16 size = 0; |
| 1054 | |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1055 | if (!first->attr.sample_id_all) |
| 1056 | goto out; |
| 1057 | |
| 1058 | sample_type = first->attr.sample_type; |
| 1059 | |
| 1060 | if (sample_type & PERF_SAMPLE_TID) |
| 1061 | size += sizeof(data->tid) * 2; |
| 1062 | |
| 1063 | if (sample_type & PERF_SAMPLE_TIME) |
| 1064 | size += sizeof(data->time); |
| 1065 | |
| 1066 | if (sample_type & PERF_SAMPLE_ID) |
| 1067 | size += sizeof(data->id); |
| 1068 | |
| 1069 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1070 | size += sizeof(data->stream_id); |
| 1071 | |
| 1072 | if (sample_type & PERF_SAMPLE_CPU) |
| 1073 | size += sizeof(data->cpu) * 2; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1074 | |
| 1075 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1076 | size += sizeof(data->id); |
Arnaldo Carvalho de Melo | 81e36bf | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1077 | out: |
| 1078 | return size; |
| 1079 | } |
| 1080 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1081 | 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] | 1082 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1083 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1084 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1085 | evlist__for_each_continue(evlist, pos) { |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1086 | if (first->attr.sample_id_all != pos->attr.sample_id_all) |
| 1087 | return false; |
| 1088 | } |
| 1089 | |
| 1090 | return true; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1093 | bool perf_evlist__sample_id_all(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1094 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1095 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1096 | return first->attr.sample_id_all; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1097 | } |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1098 | |
| 1099 | void perf_evlist__set_selected(struct perf_evlist *evlist, |
| 1100 | struct perf_evsel *evsel) |
| 1101 | { |
| 1102 | evlist->selected = evsel; |
| 1103 | } |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1104 | |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1105 | void perf_evlist__close(struct perf_evlist *evlist) |
| 1106 | { |
| 1107 | struct perf_evsel *evsel; |
| 1108 | int ncpus = cpu_map__nr(evlist->cpus); |
| 1109 | int nthreads = thread_map__nr(evlist->threads); |
Stephane Eranian | 8ad9219 | 2014-01-17 16:34:06 +0100 | [diff] [blame] | 1110 | int n; |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1111 | |
Stephane Eranian | 8ad9219 | 2014-01-17 16:34:06 +0100 | [diff] [blame] | 1112 | evlist__for_each_reverse(evlist, evsel) { |
| 1113 | n = evsel->cpus ? evsel->cpus->nr : ncpus; |
| 1114 | perf_evsel__close(evsel, n, nthreads); |
| 1115 | } |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1116 | } |
| 1117 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1118 | int perf_evlist__open(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1119 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1120 | struct perf_evsel *evsel; |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1121 | int err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1122 | |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 1123 | perf_evlist__update_id_pos(evlist); |
| 1124 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1125 | evlist__for_each(evlist, evsel) { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1126 | err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1127 | if (err < 0) |
| 1128 | goto out_err; |
| 1129 | } |
| 1130 | |
| 1131 | return 0; |
| 1132 | out_err: |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1133 | perf_evlist__close(evlist); |
Namhyung Kim | 41c21a6 | 2012-02-23 12:13:36 +0900 | [diff] [blame] | 1134 | errno = -err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1135 | return err; |
| 1136 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1137 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1138 | int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 1139 | const char *argv[], bool pipe_output, |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1140 | void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1141 | { |
| 1142 | int child_ready_pipe[2], go_pipe[2]; |
| 1143 | char bf; |
| 1144 | |
| 1145 | if (pipe(child_ready_pipe) < 0) { |
| 1146 | perror("failed to create 'ready' pipe"); |
| 1147 | return -1; |
| 1148 | } |
| 1149 | |
| 1150 | if (pipe(go_pipe) < 0) { |
| 1151 | perror("failed to create 'go' pipe"); |
| 1152 | goto out_close_ready_pipe; |
| 1153 | } |
| 1154 | |
| 1155 | evlist->workload.pid = fork(); |
| 1156 | if (evlist->workload.pid < 0) { |
| 1157 | perror("failed to fork"); |
| 1158 | goto out_close_pipes; |
| 1159 | } |
| 1160 | |
| 1161 | if (!evlist->workload.pid) { |
Arnaldo Carvalho de Melo | 5f1c422 | 2014-07-28 12:39:50 -0300 | [diff] [blame] | 1162 | int ret; |
| 1163 | |
Namhyung Kim | 119fa3c | 2013-03-11 16:43:16 +0900 | [diff] [blame] | 1164 | if (pipe_output) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1165 | dup2(2, 1); |
| 1166 | |
David Ahern | 0817df0 | 2013-05-25 17:50:39 -0600 | [diff] [blame] | 1167 | signal(SIGTERM, SIG_DFL); |
| 1168 | |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1169 | close(child_ready_pipe[0]); |
| 1170 | close(go_pipe[1]); |
| 1171 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); |
| 1172 | |
| 1173 | /* |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1174 | * Tell the parent we're ready to go |
| 1175 | */ |
| 1176 | close(child_ready_pipe[1]); |
| 1177 | |
| 1178 | /* |
| 1179 | * Wait until the parent tells us to go. |
| 1180 | */ |
Arnaldo Carvalho de Melo | 5f1c422 | 2014-07-28 12:39:50 -0300 | [diff] [blame] | 1181 | ret = read(go_pipe[0], &bf, 1); |
| 1182 | /* |
| 1183 | * The parent will ask for the execvp() to be performed by |
| 1184 | * writing exactly one byte, in workload.cork_fd, usually via |
| 1185 | * perf_evlist__start_workload(). |
| 1186 | * |
| 1187 | * For cancelling the workload without actuallin running it, |
| 1188 | * the parent will just close workload.cork_fd, without writing |
| 1189 | * anything, i.e. read will return zero and we just exit() |
| 1190 | * here. |
| 1191 | */ |
| 1192 | if (ret != 1) { |
| 1193 | if (ret == -1) |
| 1194 | perror("unable to read pipe"); |
| 1195 | exit(ret); |
| 1196 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1197 | |
| 1198 | execvp(argv[0], (char **)argv); |
| 1199 | |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1200 | if (exec_error) { |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 1201 | union sigval val; |
| 1202 | |
| 1203 | val.sival_int = errno; |
| 1204 | if (sigqueue(getppid(), SIGUSR1, val)) |
| 1205 | perror(argv[0]); |
| 1206 | } else |
| 1207 | perror(argv[0]); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1208 | exit(-1); |
| 1209 | } |
| 1210 | |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1211 | if (exec_error) { |
| 1212 | struct sigaction act = { |
| 1213 | .sa_flags = SA_SIGINFO, |
| 1214 | .sa_sigaction = exec_error, |
| 1215 | }; |
| 1216 | sigaction(SIGUSR1, &act, NULL); |
| 1217 | } |
| 1218 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1219 | if (target__none(target)) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1220 | evlist->threads->map[0] = evlist->workload.pid; |
| 1221 | |
| 1222 | close(child_ready_pipe[1]); |
| 1223 | close(go_pipe[0]); |
| 1224 | /* |
| 1225 | * wait for child to settle |
| 1226 | */ |
| 1227 | if (read(child_ready_pipe[0], &bf, 1) == -1) { |
| 1228 | perror("unable to read pipe"); |
| 1229 | goto out_close_pipes; |
| 1230 | } |
| 1231 | |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1232 | fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1233 | evlist->workload.cork_fd = go_pipe[1]; |
| 1234 | close(child_ready_pipe[0]); |
| 1235 | return 0; |
| 1236 | |
| 1237 | out_close_pipes: |
| 1238 | close(go_pipe[0]); |
| 1239 | close(go_pipe[1]); |
| 1240 | out_close_ready_pipe: |
| 1241 | close(child_ready_pipe[0]); |
| 1242 | close(child_ready_pipe[1]); |
| 1243 | return -1; |
| 1244 | } |
| 1245 | |
| 1246 | int perf_evlist__start_workload(struct perf_evlist *evlist) |
| 1247 | { |
| 1248 | if (evlist->workload.cork_fd > 0) { |
David Ahern | b382440 | 2013-07-02 13:27:21 -0600 | [diff] [blame] | 1249 | char bf = 0; |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1250 | int ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1251 | /* |
| 1252 | * Remove the cork, let it rip! |
| 1253 | */ |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1254 | ret = write(evlist->workload.cork_fd, &bf, 1); |
| 1255 | if (ret < 0) |
| 1256 | perror("enable to write to pipe"); |
| 1257 | |
| 1258 | close(evlist->workload.cork_fd); |
| 1259 | return ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1264 | |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1265 | 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] | 1266 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1267 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1268 | struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); |
| 1269 | |
| 1270 | if (!evsel) |
| 1271 | return -EFAULT; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1272 | return perf_evsel__parse_sample(evsel, event, sample); |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1273 | } |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1274 | |
| 1275 | size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) |
| 1276 | { |
| 1277 | struct perf_evsel *evsel; |
| 1278 | size_t printed = 0; |
| 1279 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1280 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1281 | printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", |
| 1282 | perf_evsel__name(evsel)); |
| 1283 | } |
| 1284 | |
Davidlohr Bueso | b222213 | 2013-11-12 22:24:24 -0800 | [diff] [blame] | 1285 | return printed + fprintf(fp, "\n"); |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1286 | } |
Arnaldo Carvalho de Melo | 6ef068c | 2013-10-17 12:07:58 -0300 | [diff] [blame] | 1287 | |
| 1288 | int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused, |
| 1289 | int err, char *buf, size_t size) |
| 1290 | { |
| 1291 | char sbuf[128]; |
| 1292 | |
| 1293 | switch (err) { |
| 1294 | case ENOENT: |
| 1295 | scnprintf(buf, size, "%s", |
| 1296 | "Error:\tUnable to find debugfs\n" |
| 1297 | "Hint:\tWas your kernel was compiled with debugfs support?\n" |
| 1298 | "Hint:\tIs the debugfs filesystem mounted?\n" |
| 1299 | "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'"); |
| 1300 | break; |
| 1301 | case EACCES: |
| 1302 | scnprintf(buf, size, |
| 1303 | "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n" |
| 1304 | "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", |
| 1305 | debugfs_mountpoint, debugfs_mountpoint); |
| 1306 | break; |
| 1307 | default: |
| 1308 | scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf))); |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | return 0; |
| 1313 | } |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1314 | |
| 1315 | int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, |
| 1316 | int err, char *buf, size_t size) |
| 1317 | { |
| 1318 | int printed, value; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 1319 | char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf)); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1320 | |
| 1321 | switch (err) { |
| 1322 | case EACCES: |
| 1323 | case EPERM: |
| 1324 | printed = scnprintf(buf, size, |
| 1325 | "Error:\t%s.\n" |
| 1326 | "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); |
| 1327 | |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 1328 | value = perf_event_paranoid(); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1329 | |
| 1330 | printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); |
| 1331 | |
| 1332 | if (value >= 2) { |
| 1333 | printed += scnprintf(buf + printed, size - printed, |
| 1334 | "For your workloads it needs to be <= 1\nHint:\t"); |
| 1335 | } |
| 1336 | printed += scnprintf(buf + printed, size - printed, |
Arnaldo Carvalho de Melo | 5229e36 | 2014-06-10 17:18:54 -0300 | [diff] [blame] | 1337 | "For system wide tracing it needs to be set to -1.\n"); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1338 | |
| 1339 | printed += scnprintf(buf + printed, size - printed, |
Arnaldo Carvalho de Melo | 5229e36 | 2014-06-10 17:18:54 -0300 | [diff] [blame] | 1340 | "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" |
| 1341 | "Hint:\tThe current value is %d.", value); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1342 | break; |
| 1343 | default: |
| 1344 | scnprintf(buf, size, "%s", emsg); |
| 1345 | break; |
| 1346 | } |
| 1347 | |
| 1348 | return 0; |
| 1349 | } |
Adrian Hunter | a025e4f | 2013-12-11 14:36:35 +0200 | [diff] [blame] | 1350 | |
| 1351 | void perf_evlist__to_front(struct perf_evlist *evlist, |
| 1352 | struct perf_evsel *move_evsel) |
| 1353 | { |
| 1354 | struct perf_evsel *evsel, *n; |
| 1355 | LIST_HEAD(move); |
| 1356 | |
| 1357 | if (move_evsel == perf_evlist__first(evlist)) |
| 1358 | return; |
| 1359 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1360 | evlist__for_each_safe(evlist, n, evsel) { |
Adrian Hunter | a025e4f | 2013-12-11 14:36:35 +0200 | [diff] [blame] | 1361 | if (evsel->leader == move_evsel->leader) |
| 1362 | list_move_tail(&evsel->node, &move); |
| 1363 | } |
| 1364 | |
| 1365 | list_splice(&move, &evlist->entries); |
| 1366 | } |
Adrian Hunter | 60b0896 | 2014-07-31 09:00:52 +0300 | [diff] [blame] | 1367 | |
| 1368 | void perf_evlist__set_tracking_event(struct perf_evlist *evlist, |
| 1369 | struct perf_evsel *tracking_evsel) |
| 1370 | { |
| 1371 | struct perf_evsel *evsel; |
| 1372 | |
| 1373 | if (tracking_evsel->tracking) |
| 1374 | return; |
| 1375 | |
| 1376 | evlist__for_each(evlist, evsel) { |
| 1377 | if (evsel != tracking_evsel) |
| 1378 | evsel->tracking = false; |
| 1379 | } |
| 1380 | |
| 1381 | tracking_evsel->tracking = true; |
| 1382 | } |