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