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