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