Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
| 3 | * |
| 4 | * Parts came from builtin-{top,stat,record}.c, see those files for further |
| 5 | * copyright notes. |
| 6 | * |
| 7 | * Released under the GPL v2. (and only v2, not any later version) |
| 8 | */ |
Arnaldo Carvalho de Melo | a8c9ae1 | 2011-11-05 08:41:51 -0200 | [diff] [blame] | 9 | #include "util.h" |
Borislav Petkov | 553873e | 2013-12-09 17:14:23 +0100 | [diff] [blame] | 10 | #include <api/fs/debugfs.h> |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 11 | #include <api/fs/fs.h> |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 12 | #include <poll.h> |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 13 | #include "cpumap.h" |
| 14 | #include "thread_map.h" |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 15 | #include "target.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 16 | #include "evlist.h" |
| 17 | #include "evsel.h" |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 18 | #include "debug.h" |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 19 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 20 | |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 21 | #include "parse-events.h" |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 22 | #include "parse-options.h" |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 24 | #include <sys/mman.h> |
| 25 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 26 | #include <linux/bitops.h> |
| 27 | #include <linux/hash.h> |
| 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 | f66a889d | 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 | f66a889d | 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]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 637 | unsigned int head = perf_mmap__read_head(md); |
| 638 | unsigned int old = md->prev; |
| 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 | { |
| 698 | return perf_mmap__read_head(md) != md->prev; |
| 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) { |
Zhouyi Zhou | 8e50d38 | 2013-10-24 15:43:33 +0800 | [diff] [blame] | 719 | unsigned int old = md->prev; |
| 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 | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 728 | static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx) |
| 729 | { |
| 730 | if (evlist->mmap[idx].base != NULL) { |
| 731 | munmap(evlist->mmap[idx].base, evlist->mmap_len); |
| 732 | evlist->mmap[idx].base = NULL; |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 733 | evlist->mmap[idx].refcnt = 0; |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 737 | void perf_evlist__munmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 738 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 739 | int i; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 740 | |
Arnaldo Carvalho de Melo | 983874d | 2014-01-03 17:25:49 -0300 | [diff] [blame] | 741 | if (evlist->mmap == NULL) |
| 742 | return; |
| 743 | |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 744 | for (i = 0; i < evlist->nr_mmaps; i++) |
| 745 | __perf_evlist__munmap(evlist, i); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 746 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 747 | zfree(&evlist->mmap); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 748 | } |
| 749 | |
Arnaldo Carvalho de Melo | 806fb63 | 2011-11-29 08:05:52 -0200 | [diff] [blame] | 750 | static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 751 | { |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 752 | evlist->nr_mmaps = cpu_map__nr(evlist->cpus); |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 753 | if (cpu_map__empty(evlist->cpus)) |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 754 | evlist->nr_mmaps = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 755 | evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 756 | return evlist->mmap != NULL ? 0 : -ENOMEM; |
| 757 | } |
| 758 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 759 | struct mmap_params { |
| 760 | int prot; |
| 761 | int mask; |
| 762 | }; |
| 763 | |
| 764 | static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx, |
| 765 | struct mmap_params *mp, int fd) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 766 | { |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 767 | /* |
| 768 | * The last one will be done at perf_evlist__mmap_consume(), so that we |
| 769 | * make sure we don't prevent tools from consuming every last event in |
| 770 | * the ring buffer. |
| 771 | * |
| 772 | * I.e. we can get the POLLHUP meaning that the fd doesn't exist |
| 773 | * anymore, but the last events for it are still in the ring buffer, |
| 774 | * waiting to be consumed. |
| 775 | * |
| 776 | * Tools can chose to ignore this at their own discretion, but the |
| 777 | * evlist layer can't just drop it when filtering events in |
| 778 | * perf_evlist__filter_pollfd(). |
| 779 | */ |
| 780 | evlist->mmap[idx].refcnt = 2; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 781 | evlist->mmap[idx].prev = 0; |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 782 | evlist->mmap[idx].mask = mp->mask; |
| 783 | 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] | 784 | MAP_SHARED, fd, 0); |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 785 | if (evlist->mmap[idx].base == MAP_FAILED) { |
Adrian Hunter | 0263596 | 2013-11-01 15:51:33 +0200 | [diff] [blame] | 786 | pr_debug2("failed to mmap perf event ring buffer, error %d\n", |
| 787 | errno); |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 788 | evlist->mmap[idx].base = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 789 | return -1; |
Nelson Elhage | 301b195 | 2011-12-19 08:39:30 -0500 | [diff] [blame] | 790 | } |
Arnaldo Carvalho de Melo | ad6765d | 2014-08-18 16:44:06 -0300 | [diff] [blame] | 791 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 792 | return 0; |
| 793 | } |
| 794 | |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 795 | 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] | 796 | struct mmap_params *mp, int cpu, |
| 797 | int thread, int *output) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 798 | { |
| 799 | struct perf_evsel *evsel; |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 800 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 801 | evlist__for_each(evlist, evsel) { |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 802 | int fd; |
| 803 | |
| 804 | if (evsel->system_wide && thread) |
| 805 | continue; |
| 806 | |
| 807 | fd = FD(evsel, cpu, thread); |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 808 | |
| 809 | if (*output == -1) { |
| 810 | *output = fd; |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 811 | if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 812 | return -1; |
| 813 | } else { |
| 814 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) |
| 815 | return -1; |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 816 | |
| 817 | perf_evlist__mmap_get(evlist, idx); |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 818 | } |
| 819 | |
Adrian Hunter | f90d194 | 2014-11-11 16:16:39 +0200 | [diff] [blame] | 820 | /* |
| 821 | * The system_wide flag causes a selected event to be opened |
| 822 | * always without a pid. Consequently it will never get a |
| 823 | * POLLHUP, but it is used for tracking in combination with |
| 824 | * other events, so it should not need to be polled anyway. |
| 825 | * Therefore don't add it for polling. |
| 826 | */ |
| 827 | if (!evsel->system_wide && |
| 828 | __perf_evlist__add_pollfd(evlist, fd, idx) < 0) { |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 829 | perf_evlist__mmap_put(evlist, idx); |
Arnaldo Carvalho de Melo | ad6765d | 2014-08-18 16:44:06 -0300 | [diff] [blame] | 830 | return -1; |
Arnaldo Carvalho de Melo | 8239698 | 2014-09-08 13:26:35 -0300 | [diff] [blame] | 831 | } |
Arnaldo Carvalho de Melo | 033fa71 | 2014-09-08 12:55:12 -0300 | [diff] [blame] | 832 | |
Adrian Hunter | 3c659ee | 2014-10-27 15:49:22 +0200 | [diff] [blame] | 833 | if (evsel->attr.read_format & PERF_FORMAT_ID) { |
| 834 | if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, |
| 835 | fd) < 0) |
| 836 | return -1; |
| 837 | perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, |
| 838 | thread); |
| 839 | } |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 845 | static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, |
| 846 | struct mmap_params *mp) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 847 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 848 | int cpu, thread; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 849 | int nr_cpus = cpu_map__nr(evlist->cpus); |
| 850 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 851 | |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 852 | pr_debug2("perf event ring buffer mmapped per cpu\n"); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 853 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 854 | int output = -1; |
| 855 | |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 856 | for (thread = 0; thread < nr_threads; thread++) { |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 857 | if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, |
| 858 | thread, &output)) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 859 | goto out_unmap; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | |
| 865 | out_unmap: |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 866 | for (cpu = 0; cpu < nr_cpus; cpu++) |
| 867 | __perf_evlist__munmap(evlist, cpu); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 868 | return -1; |
| 869 | } |
| 870 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 871 | static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, |
| 872 | struct mmap_params *mp) |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 873 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 874 | int thread; |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 875 | int nr_threads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 876 | |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 877 | pr_debug2("perf event ring buffer mmapped per thread\n"); |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 878 | for (thread = 0; thread < nr_threads; thread++) { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 879 | int output = -1; |
| 880 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 881 | if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, |
| 882 | &output)) |
Adrian Hunter | 04e2131 | 2013-10-18 15:29:13 +0300 | [diff] [blame] | 883 | goto out_unmap; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | return 0; |
| 887 | |
| 888 | out_unmap: |
Adrian Hunter | 93edcbd | 2013-07-04 16:20:26 +0300 | [diff] [blame] | 889 | for (thread = 0; thread < nr_threads; thread++) |
| 890 | __perf_evlist__munmap(evlist, thread); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 891 | return -1; |
| 892 | } |
| 893 | |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 894 | static size_t perf_evlist__mmap_size(unsigned long pages) |
| 895 | { |
| 896 | /* 512 kiB: default amount of unprivileged mlocked memory */ |
| 897 | if (pages == UINT_MAX) |
| 898 | pages = (512 * 1024) / page_size; |
| 899 | else if (!is_power_of_2(pages)) |
| 900 | return 0; |
| 901 | |
| 902 | return (pages + 1) * page_size; |
| 903 | } |
| 904 | |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 905 | static long parse_pages_arg(const char *str, unsigned long min, |
| 906 | unsigned long max) |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 907 | { |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 908 | unsigned long pages, val; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 909 | static struct parse_tag tags[] = { |
| 910 | { .tag = 'B', .mult = 1 }, |
| 911 | { .tag = 'K', .mult = 1 << 10 }, |
| 912 | { .tag = 'M', .mult = 1 << 20 }, |
| 913 | { .tag = 'G', .mult = 1 << 30 }, |
| 914 | { .tag = 0 }, |
| 915 | }; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 916 | |
David Ahern | 8973504 | 2013-11-12 07:46:53 -0700 | [diff] [blame] | 917 | if (str == NULL) |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 918 | return -EINVAL; |
David Ahern | 8973504 | 2013-11-12 07:46:53 -0700 | [diff] [blame] | 919 | |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 920 | val = parse_tag_value(str, tags); |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 921 | if (val != (unsigned long) -1) { |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 922 | /* we got file size value */ |
| 923 | pages = PERF_ALIGN(val, page_size) / page_size; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 924 | } else { |
| 925 | /* we got pages count value */ |
| 926 | char *eptr; |
| 927 | pages = strtoul(str, &eptr, 10); |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 928 | if (*eptr != '\0') |
| 929 | return -EINVAL; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 930 | } |
| 931 | |
Adrian Hunter | 2bcab6c | 2013-12-09 15:18:37 +0200 | [diff] [blame] | 932 | if (pages == 0 && min == 0) { |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 933 | /* leave number of pages at 0 */ |
Adrian Hunter | 1dbfa938 | 2013-12-09 15:18:39 +0200 | [diff] [blame] | 934 | } else if (!is_power_of_2(pages)) { |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 935 | /* round pages up to next power of 2 */ |
Adrian Hunter | 1dbfa938 | 2013-12-09 15:18:39 +0200 | [diff] [blame] | 936 | pages = next_pow2_l(pages); |
| 937 | if (!pages) |
| 938 | return -EINVAL; |
David Ahern | 9639837 | 2013-11-12 07:46:54 -0700 | [diff] [blame] | 939 | pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n", |
| 940 | pages * page_size, pages); |
Adrian Hunter | 2fbe4ab | 2013-10-22 10:34:18 +0300 | [diff] [blame] | 941 | } |
| 942 | |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 943 | if (pages > max) |
| 944 | return -EINVAL; |
| 945 | |
| 946 | return pages; |
| 947 | } |
| 948 | |
| 949 | int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, |
| 950 | int unset __maybe_unused) |
| 951 | { |
| 952 | unsigned int *mmap_pages = opt->value; |
| 953 | unsigned long max = UINT_MAX; |
| 954 | long pages; |
| 955 | |
Adrian Hunter | f5ae9c4 | 2013-12-09 15:18:38 +0200 | [diff] [blame] | 956 | if (max > SIZE_MAX / page_size) |
David Ahern | 33c2dcf | 2013-11-12 07:46:55 -0700 | [diff] [blame] | 957 | max = SIZE_MAX / page_size; |
| 958 | |
| 959 | pages = parse_pages_arg(str, 1, max); |
| 960 | if (pages < 0) { |
| 961 | pr_err("Invalid argument for --mmap_pages/-m\n"); |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 962 | return -1; |
| 963 | } |
| 964 | |
| 965 | *mmap_pages = pages; |
| 966 | return 0; |
| 967 | } |
| 968 | |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 969 | /** |
| 970 | * perf_evlist__mmap - Create mmaps to receive events. |
| 971 | * @evlist: list of events |
| 972 | * @pages: map length in pages |
| 973 | * @overwrite: overwrite older events? |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 974 | * |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 975 | * If @overwrite is %false the user needs to signal event consumption using |
| 976 | * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this |
| 977 | * automatically. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 978 | * |
Adrian Hunter | c83fa7f | 2013-10-18 15:29:12 +0300 | [diff] [blame] | 979 | * Return: %0 on success, negative error code otherwise. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 980 | */ |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 981 | int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, |
| 982 | bool overwrite) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 983 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 984 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 985 | const struct cpu_map *cpus = evlist->cpus; |
| 986 | const struct thread_map *threads = evlist->threads; |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 987 | struct mmap_params mp = { |
| 988 | .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), |
| 989 | }; |
Arnaldo Carvalho de Melo | 50a682c | 2011-11-09 09:10:47 -0200 | [diff] [blame] | 990 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 991 | if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 992 | return -ENOMEM; |
| 993 | |
Arnaldo Carvalho de Melo | 1b85337 | 2014-09-03 18:02:59 -0300 | [diff] [blame] | 994 | 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] | 995 | return -ENOMEM; |
| 996 | |
| 997 | evlist->overwrite = overwrite; |
Jiri Olsa | 994a1f7 | 2013-09-01 12:36:12 +0200 | [diff] [blame] | 998 | evlist->mmap_len = perf_evlist__mmap_size(pages); |
Adrian Hunter | 2af68ef | 2013-10-18 15:29:07 +0300 | [diff] [blame] | 999 | pr_debug("mmap size %zuB\n", evlist->mmap_len); |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1000 | mp.mask = evlist->mmap_len - page_size - 1; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1001 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1002 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1003 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1004 | evsel->sample_id == NULL && |
Arnaldo Carvalho de Melo | a14bb7a | 2012-09-26 12:41:14 -0300 | [diff] [blame] | 1005 | 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] | 1006 | return -ENOMEM; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Sukadev Bhattiprolu | ec1e7e4 | 2013-05-22 17:42:38 -0700 | [diff] [blame] | 1009 | if (cpu_map__empty(cpus)) |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1010 | return perf_evlist__mmap_per_thread(evlist, &mp); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1011 | |
Adrian Hunter | a8a8f3e | 2014-07-14 13:02:52 +0300 | [diff] [blame] | 1012 | return perf_evlist__mmap_per_cpu(evlist, &mp); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1013 | } |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1014 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1015 | 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] | 1016 | { |
Namhyung Kim | b809ac1 | 2012-04-26 14:15:19 +0900 | [diff] [blame] | 1017 | evlist->threads = thread_map__new_str(target->pid, target->tid, |
| 1018 | target->uid); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1019 | |
| 1020 | if (evlist->threads == NULL) |
| 1021 | return -1; |
| 1022 | |
Dongsheng Yang | 9c105fb | 2013-12-04 17:56:40 -0500 | [diff] [blame] | 1023 | if (target__uses_dummy_map(target)) |
Namhyung Kim | d1cb9fc | 2012-05-16 18:45:49 +0900 | [diff] [blame] | 1024 | evlist->cpus = cpu_map__dummy_new(); |
Namhyung Kim | 879d77d | 2012-05-16 18:45:48 +0900 | [diff] [blame] | 1025 | else |
| 1026 | evlist->cpus = cpu_map__new(target->cpu_list); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1027 | |
| 1028 | if (evlist->cpus == NULL) |
| 1029 | goto out_delete_threads; |
| 1030 | |
| 1031 | return 0; |
| 1032 | |
| 1033 | out_delete_threads: |
| 1034 | thread_map__delete(evlist->threads); |
Yasser Shalabi | b2e19a9 | 2014-10-04 11:37:57 -0400 | [diff] [blame] | 1035 | evlist->threads = NULL; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 1036 | return -1; |
| 1037 | } |
| 1038 | |
Arnaldo Carvalho de Melo | 1491a63 | 2012-09-26 14:43:13 -0300 | [diff] [blame] | 1039 | int perf_evlist__apply_filters(struct perf_evlist *evlist) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1040 | { |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1041 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1042 | int err = 0; |
| 1043 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 1044 | nthreads = thread_map__nr(evlist->threads); |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1045 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1046 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1047 | if (evsel->filter == NULL) |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1048 | continue; |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1049 | |
| 1050 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter); |
| 1051 | if (err) |
| 1052 | break; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1055 | return err; |
| 1056 | } |
| 1057 | |
| 1058 | int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) |
| 1059 | { |
| 1060 | struct perf_evsel *evsel; |
| 1061 | int err = 0; |
| 1062 | const int ncpus = cpu_map__nr(evlist->cpus), |
Namhyung Kim | b3a319d | 2013-03-11 16:43:14 +0900 | [diff] [blame] | 1063 | nthreads = thread_map__nr(evlist->threads); |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1064 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1065 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1066 | err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter); |
| 1067 | if (err) |
| 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | return err; |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 1072 | } |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1073 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1074 | bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1075 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1076 | struct perf_evsel *pos; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1077 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1078 | if (evlist->nr_entries == 1) |
| 1079 | return true; |
| 1080 | |
| 1081 | if (evlist->id_pos < 0 || evlist->is_pos < 0) |
| 1082 | return false; |
| 1083 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1084 | evlist__for_each(evlist, pos) { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1085 | if (pos->id_pos != evlist->id_pos || |
| 1086 | pos->is_pos != evlist->is_pos) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1087 | return false; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1090 | return true; |
| 1091 | } |
| 1092 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1093 | u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1094 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1095 | struct perf_evsel *evsel; |
| 1096 | |
| 1097 | if (evlist->combined_sample_type) |
| 1098 | return evlist->combined_sample_type; |
| 1099 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1100 | evlist__for_each(evlist, evsel) |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1101 | evlist->combined_sample_type |= evsel->attr.sample_type; |
| 1102 | |
| 1103 | return evlist->combined_sample_type; |
| 1104 | } |
| 1105 | |
| 1106 | u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
| 1107 | { |
| 1108 | evlist->combined_sample_type = 0; |
| 1109 | return __perf_evlist__combined_sample_type(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1110 | } |
| 1111 | |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1112 | bool perf_evlist__valid_read_format(struct perf_evlist *evlist) |
| 1113 | { |
| 1114 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
| 1115 | u64 read_format = first->attr.read_format; |
| 1116 | u64 sample_type = first->attr.sample_type; |
| 1117 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1118 | evlist__for_each(evlist, pos) { |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1119 | if (read_format != pos->attr.read_format) |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
| 1123 | /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ |
| 1124 | if ((sample_type & PERF_SAMPLE_READ) && |
| 1125 | !(read_format & PERF_FORMAT_ID)) { |
| 1126 | return false; |
| 1127 | } |
| 1128 | |
| 1129 | return true; |
| 1130 | } |
| 1131 | |
| 1132 | u64 perf_evlist__read_format(struct perf_evlist *evlist) |
| 1133 | { |
| 1134 | struct perf_evsel *first = perf_evlist__first(evlist); |
| 1135 | return first->attr.read_format; |
| 1136 | } |
| 1137 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1138 | u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 81e36bff | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1139 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1140 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | 81e36bff | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1141 | struct perf_sample *data; |
| 1142 | u64 sample_type; |
| 1143 | u16 size = 0; |
| 1144 | |
Arnaldo Carvalho de Melo | 81e36bff | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1145 | if (!first->attr.sample_id_all) |
| 1146 | goto out; |
| 1147 | |
| 1148 | sample_type = first->attr.sample_type; |
| 1149 | |
| 1150 | if (sample_type & PERF_SAMPLE_TID) |
| 1151 | size += sizeof(data->tid) * 2; |
| 1152 | |
| 1153 | if (sample_type & PERF_SAMPLE_TIME) |
| 1154 | size += sizeof(data->time); |
| 1155 | |
| 1156 | if (sample_type & PERF_SAMPLE_ID) |
| 1157 | size += sizeof(data->id); |
| 1158 | |
| 1159 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 1160 | size += sizeof(data->stream_id); |
| 1161 | |
| 1162 | if (sample_type & PERF_SAMPLE_CPU) |
| 1163 | size += sizeof(data->cpu) * 2; |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1164 | |
| 1165 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 1166 | size += sizeof(data->id); |
Arnaldo Carvalho de Melo | 81e36bff | 2011-11-11 22:28:50 -0200 | [diff] [blame] | 1167 | out: |
| 1168 | return size; |
| 1169 | } |
| 1170 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1171 | 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] | 1172 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1173 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1174 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1175 | evlist__for_each_continue(evlist, pos) { |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1176 | if (first->attr.sample_id_all != pos->attr.sample_id_all) |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | return true; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1183 | bool perf_evlist__sample_id_all(struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1184 | { |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1185 | struct perf_evsel *first = perf_evlist__first(evlist); |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 1186 | return first->attr.sample_id_all; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 1187 | } |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1188 | |
| 1189 | void perf_evlist__set_selected(struct perf_evlist *evlist, |
| 1190 | struct perf_evsel *evsel) |
| 1191 | { |
| 1192 | evlist->selected = evsel; |
| 1193 | } |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1194 | |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1195 | void perf_evlist__close(struct perf_evlist *evlist) |
| 1196 | { |
| 1197 | struct perf_evsel *evsel; |
| 1198 | int ncpus = cpu_map__nr(evlist->cpus); |
| 1199 | int nthreads = thread_map__nr(evlist->threads); |
Stephane Eranian | 8ad9219 | 2014-01-17 16:34:06 +0100 | [diff] [blame] | 1200 | int n; |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1201 | |
Stephane Eranian | 8ad9219 | 2014-01-17 16:34:06 +0100 | [diff] [blame] | 1202 | evlist__for_each_reverse(evlist, evsel) { |
| 1203 | n = evsel->cpus ? evsel->cpus->nr : ncpus; |
| 1204 | perf_evsel__close(evsel, n, nthreads); |
| 1205 | } |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1206 | } |
| 1207 | |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1208 | static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) |
| 1209 | { |
| 1210 | int err = -ENOMEM; |
| 1211 | |
| 1212 | /* |
| 1213 | * Try reading /sys/devices/system/cpu/online to get |
| 1214 | * an all cpus map. |
| 1215 | * |
| 1216 | * FIXME: -ENOMEM is the best we can do here, the cpu_map |
| 1217 | * code needs an overhaul to properly forward the |
| 1218 | * error, and we may not want to do that fallback to a |
| 1219 | * default cpu identity map :-\ |
| 1220 | */ |
| 1221 | evlist->cpus = cpu_map__new(NULL); |
| 1222 | if (evlist->cpus == NULL) |
| 1223 | goto out; |
| 1224 | |
| 1225 | evlist->threads = thread_map__new_dummy(); |
| 1226 | if (evlist->threads == NULL) |
| 1227 | goto out_free_cpus; |
| 1228 | |
| 1229 | err = 0; |
| 1230 | out: |
| 1231 | return err; |
| 1232 | out_free_cpus: |
| 1233 | cpu_map__delete(evlist->cpus); |
| 1234 | evlist->cpus = NULL; |
| 1235 | goto out; |
| 1236 | } |
| 1237 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1238 | int perf_evlist__open(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1239 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1240 | struct perf_evsel *evsel; |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1241 | int err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1242 | |
Arnaldo Carvalho de Melo | 4112eb1 | 2014-10-10 15:55:15 -0300 | [diff] [blame] | 1243 | /* |
| 1244 | * Default: one fd per CPU, all threads, aka systemwide |
| 1245 | * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL |
| 1246 | */ |
| 1247 | if (evlist->threads == NULL && evlist->cpus == NULL) { |
| 1248 | err = perf_evlist__create_syswide_maps(evlist); |
| 1249 | if (err < 0) |
| 1250 | goto out_err; |
| 1251 | } |
| 1252 | |
Adrian Hunter | 733cd2f | 2013-09-06 22:40:11 +0300 | [diff] [blame] | 1253 | perf_evlist__update_id_pos(evlist); |
| 1254 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1255 | evlist__for_each(evlist, evsel) { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1256 | err = perf_evsel__open(evsel, evlist->cpus, evlist->threads); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1257 | if (err < 0) |
| 1258 | goto out_err; |
| 1259 | } |
| 1260 | |
| 1261 | return 0; |
| 1262 | out_err: |
Namhyung Kim | a74b4b6 | 2013-03-15 14:48:48 +0900 | [diff] [blame] | 1263 | perf_evlist__close(evlist); |
Namhyung Kim | 41c21a6 | 2012-02-23 12:13:36 +0900 | [diff] [blame] | 1264 | errno = -err; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1265 | return err; |
| 1266 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1267 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1268 | int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, |
Namhyung Kim | 55e162e | 2013-03-11 16:43:17 +0900 | [diff] [blame] | 1269 | const char *argv[], bool pipe_output, |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1270 | void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1271 | { |
| 1272 | int child_ready_pipe[2], go_pipe[2]; |
| 1273 | char bf; |
| 1274 | |
| 1275 | if (pipe(child_ready_pipe) < 0) { |
| 1276 | perror("failed to create 'ready' pipe"); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | if (pipe(go_pipe) < 0) { |
| 1281 | perror("failed to create 'go' pipe"); |
| 1282 | goto out_close_ready_pipe; |
| 1283 | } |
| 1284 | |
| 1285 | evlist->workload.pid = fork(); |
| 1286 | if (evlist->workload.pid < 0) { |
| 1287 | perror("failed to fork"); |
| 1288 | goto out_close_pipes; |
| 1289 | } |
| 1290 | |
| 1291 | if (!evlist->workload.pid) { |
Arnaldo Carvalho de Melo | 5f1c422 | 2014-07-28 12:39:50 -0300 | [diff] [blame] | 1292 | int ret; |
| 1293 | |
Namhyung Kim | 119fa3c | 2013-03-11 16:43:16 +0900 | [diff] [blame] | 1294 | if (pipe_output) |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1295 | dup2(2, 1); |
| 1296 | |
David Ahern | 0817df0 | 2013-05-25 17:50:39 -0600 | [diff] [blame] | 1297 | signal(SIGTERM, SIG_DFL); |
| 1298 | |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1299 | close(child_ready_pipe[0]); |
| 1300 | close(go_pipe[1]); |
| 1301 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); |
| 1302 | |
| 1303 | /* |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1304 | * Tell the parent we're ready to go |
| 1305 | */ |
| 1306 | close(child_ready_pipe[1]); |
| 1307 | |
| 1308 | /* |
| 1309 | * Wait until the parent tells us to go. |
| 1310 | */ |
Arnaldo Carvalho de Melo | 5f1c422 | 2014-07-28 12:39:50 -0300 | [diff] [blame] | 1311 | ret = read(go_pipe[0], &bf, 1); |
| 1312 | /* |
| 1313 | * The parent will ask for the execvp() to be performed by |
| 1314 | * writing exactly one byte, in workload.cork_fd, usually via |
| 1315 | * perf_evlist__start_workload(). |
| 1316 | * |
| 1317 | * For cancelling the workload without actuallin running it, |
| 1318 | * the parent will just close workload.cork_fd, without writing |
| 1319 | * anything, i.e. read will return zero and we just exit() |
| 1320 | * here. |
| 1321 | */ |
| 1322 | if (ret != 1) { |
| 1323 | if (ret == -1) |
| 1324 | perror("unable to read pipe"); |
| 1325 | exit(ret); |
| 1326 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1327 | |
| 1328 | execvp(argv[0], (char **)argv); |
| 1329 | |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1330 | if (exec_error) { |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 1331 | union sigval val; |
| 1332 | |
| 1333 | val.sival_int = errno; |
| 1334 | if (sigqueue(getppid(), SIGUSR1, val)) |
| 1335 | perror(argv[0]); |
| 1336 | } else |
| 1337 | perror(argv[0]); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1338 | exit(-1); |
| 1339 | } |
| 1340 | |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 1341 | if (exec_error) { |
| 1342 | struct sigaction act = { |
| 1343 | .sa_flags = SA_SIGINFO, |
| 1344 | .sa_sigaction = exec_error, |
| 1345 | }; |
| 1346 | sigaction(SIGUSR1, &act, NULL); |
| 1347 | } |
| 1348 | |
Arnaldo Carvalho de Melo | 1aaf63b | 2014-10-10 14:29:49 -0300 | [diff] [blame] | 1349 | if (target__none(target)) { |
| 1350 | if (evlist->threads == NULL) { |
| 1351 | fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", |
| 1352 | __func__, __LINE__); |
| 1353 | goto out_close_pipes; |
| 1354 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1355 | evlist->threads->map[0] = evlist->workload.pid; |
Arnaldo Carvalho de Melo | 1aaf63b | 2014-10-10 14:29:49 -0300 | [diff] [blame] | 1356 | } |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1357 | |
| 1358 | close(child_ready_pipe[1]); |
| 1359 | close(go_pipe[0]); |
| 1360 | /* |
| 1361 | * wait for child to settle |
| 1362 | */ |
| 1363 | if (read(child_ready_pipe[0], &bf, 1) == -1) { |
| 1364 | perror("unable to read pipe"); |
| 1365 | goto out_close_pipes; |
| 1366 | } |
| 1367 | |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1368 | fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1369 | evlist->workload.cork_fd = go_pipe[1]; |
| 1370 | close(child_ready_pipe[0]); |
| 1371 | return 0; |
| 1372 | |
| 1373 | out_close_pipes: |
| 1374 | close(go_pipe[0]); |
| 1375 | close(go_pipe[1]); |
| 1376 | out_close_ready_pipe: |
| 1377 | close(child_ready_pipe[0]); |
| 1378 | close(child_ready_pipe[1]); |
| 1379 | return -1; |
| 1380 | } |
| 1381 | |
| 1382 | int perf_evlist__start_workload(struct perf_evlist *evlist) |
| 1383 | { |
| 1384 | if (evlist->workload.cork_fd > 0) { |
David Ahern | b382440 | 2013-07-02 13:27:21 -0600 | [diff] [blame] | 1385 | char bf = 0; |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1386 | int ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1387 | /* |
| 1388 | * Remove the cork, let it rip! |
| 1389 | */ |
Namhyung Kim | bcf3145 | 2013-06-26 16:14:15 +0900 | [diff] [blame] | 1390 | ret = write(evlist->workload.cork_fd, &bf, 1); |
| 1391 | if (ret < 0) |
| 1392 | perror("enable to write to pipe"); |
| 1393 | |
| 1394 | close(evlist->workload.cork_fd); |
| 1395 | return ret; |
Arnaldo Carvalho de Melo | 35b9d88 | 2011-11-09 08:47:15 -0200 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1400 | |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1401 | 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] | 1402 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1403 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1404 | struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); |
| 1405 | |
| 1406 | if (!evsel) |
| 1407 | return -EFAULT; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1408 | return perf_evsel__parse_sample(evsel, event, sample); |
Arnaldo Carvalho de Melo | cb0b29e | 2012-08-02 11:42:57 -0300 | [diff] [blame] | 1409 | } |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1410 | |
| 1411 | size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) |
| 1412 | { |
| 1413 | struct perf_evsel *evsel; |
| 1414 | size_t printed = 0; |
| 1415 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1416 | evlist__for_each(evlist, evsel) { |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1417 | printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", |
| 1418 | perf_evsel__name(evsel)); |
| 1419 | } |
| 1420 | |
Davidlohr Bueso | b222213 | 2013-11-12 22:24:24 -0800 | [diff] [blame] | 1421 | return printed + fprintf(fp, "\n"); |
Arnaldo Carvalho de Melo | 78f067b | 2012-09-06 14:54:11 -0300 | [diff] [blame] | 1422 | } |
Arnaldo Carvalho de Melo | 6ef068c | 2013-10-17 12:07:58 -0300 | [diff] [blame] | 1423 | |
| 1424 | int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused, |
| 1425 | int err, char *buf, size_t size) |
| 1426 | { |
| 1427 | char sbuf[128]; |
| 1428 | |
| 1429 | switch (err) { |
| 1430 | case ENOENT: |
| 1431 | scnprintf(buf, size, "%s", |
| 1432 | "Error:\tUnable to find debugfs\n" |
| 1433 | "Hint:\tWas your kernel was compiled with debugfs support?\n" |
| 1434 | "Hint:\tIs the debugfs filesystem mounted?\n" |
| 1435 | "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'"); |
| 1436 | break; |
| 1437 | case EACCES: |
| 1438 | scnprintf(buf, size, |
| 1439 | "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n" |
| 1440 | "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n", |
| 1441 | debugfs_mountpoint, debugfs_mountpoint); |
| 1442 | break; |
| 1443 | default: |
| 1444 | scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf))); |
| 1445 | break; |
| 1446 | } |
| 1447 | |
| 1448 | return 0; |
| 1449 | } |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1450 | |
| 1451 | int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused, |
| 1452 | int err, char *buf, size_t size) |
| 1453 | { |
| 1454 | int printed, value; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 1455 | 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] | 1456 | |
| 1457 | switch (err) { |
| 1458 | case EACCES: |
| 1459 | case EPERM: |
| 1460 | printed = scnprintf(buf, size, |
| 1461 | "Error:\t%s.\n" |
| 1462 | "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); |
| 1463 | |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 1464 | value = perf_event_paranoid(); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1465 | |
| 1466 | printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); |
| 1467 | |
| 1468 | if (value >= 2) { |
| 1469 | printed += scnprintf(buf + printed, size - printed, |
| 1470 | "For your workloads it needs to be <= 1\nHint:\t"); |
| 1471 | } |
| 1472 | printed += scnprintf(buf + printed, size - printed, |
Arnaldo Carvalho de Melo | 5229e36 | 2014-06-10 17:18:54 -0300 | [diff] [blame] | 1473 | "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] | 1474 | |
| 1475 | printed += scnprintf(buf + printed, size - printed, |
Arnaldo Carvalho de Melo | 5229e36 | 2014-06-10 17:18:54 -0300 | [diff] [blame] | 1476 | "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" |
| 1477 | "Hint:\tThe current value is %d.", value); |
Arnaldo Carvalho de Melo | a8f23d8 | 2013-10-17 17:38:29 -0300 | [diff] [blame] | 1478 | break; |
| 1479 | default: |
| 1480 | scnprintf(buf, size, "%s", emsg); |
| 1481 | break; |
| 1482 | } |
| 1483 | |
| 1484 | return 0; |
| 1485 | } |
Adrian Hunter | a025e4f | 2013-12-11 14:36:35 +0200 | [diff] [blame] | 1486 | |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 1487 | int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) |
| 1488 | { |
| 1489 | 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^] | 1490 | 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] | 1491 | |
| 1492 | switch (err) { |
| 1493 | case EPERM: |
Arnaldo Carvalho de Melo | e5d4a29 | 2014-12-12 15:59:51 -0300 | [diff] [blame] | 1494 | 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^] | 1495 | printed += scnprintf(buf + printed, size - printed, |
| 1496 | "Error:\t%s.\n" |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 1497 | "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^] | 1498 | "Hint:\tTried using %zd kB.\n", |
Arnaldo Carvalho de Melo | e5d4a29 | 2014-12-12 15:59:51 -0300 | [diff] [blame] | 1499 | emsg, pages_max_per_user, pages_attempted); |
Arnaldo Carvalho de Melo | e965bea | 2014-12-12 16:25:33 -0300 | [diff] [blame^] | 1500 | |
| 1501 | if (pages_attempted >= pages_max_per_user) { |
| 1502 | printed += scnprintf(buf + printed, size - printed, |
| 1503 | "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", |
| 1504 | pages_max_per_user + pages_attempted); |
| 1505 | } |
| 1506 | |
| 1507 | printed += scnprintf(buf + printed, size - printed, |
| 1508 | "Hint:\tTry using a smaller -m/--mmap-pages value."); |
Arnaldo Carvalho de Melo | 956fa57 | 2014-12-11 18:03:01 -0300 | [diff] [blame] | 1509 | break; |
| 1510 | default: |
| 1511 | scnprintf(buf, size, "%s", emsg); |
| 1512 | break; |
| 1513 | } |
| 1514 | |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
Adrian Hunter | a025e4f | 2013-12-11 14:36:35 +0200 | [diff] [blame] | 1518 | void perf_evlist__to_front(struct perf_evlist *evlist, |
| 1519 | struct perf_evsel *move_evsel) |
| 1520 | { |
| 1521 | struct perf_evsel *evsel, *n; |
| 1522 | LIST_HEAD(move); |
| 1523 | |
| 1524 | if (move_evsel == perf_evlist__first(evlist)) |
| 1525 | return; |
| 1526 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1527 | evlist__for_each_safe(evlist, n, evsel) { |
Adrian Hunter | a025e4f | 2013-12-11 14:36:35 +0200 | [diff] [blame] | 1528 | if (evsel->leader == move_evsel->leader) |
| 1529 | list_move_tail(&evsel->node, &move); |
| 1530 | } |
| 1531 | |
| 1532 | list_splice(&move, &evlist->entries); |
| 1533 | } |
Adrian Hunter | 60b0896 | 2014-07-31 09:00:52 +0300 | [diff] [blame] | 1534 | |
| 1535 | void perf_evlist__set_tracking_event(struct perf_evlist *evlist, |
| 1536 | struct perf_evsel *tracking_evsel) |
| 1537 | { |
| 1538 | struct perf_evsel *evsel; |
| 1539 | |
| 1540 | if (tracking_evsel->tracking) |
| 1541 | return; |
| 1542 | |
| 1543 | evlist__for_each(evlist, evsel) { |
| 1544 | if (evsel != tracking_evsel) |
| 1545 | evsel->tracking = false; |
| 1546 | } |
| 1547 | |
| 1548 | tracking_evsel->tracking = true; |
| 1549 | } |