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