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 | */ |
| 9 | |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 10 | #include <byteswap.h> |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Jiri Olsa | 4605eab | 2015-09-02 09:56:43 +0200 | [diff] [blame] | 12 | #include <api/fs/tracing_path.h> |
Robert Richter | 4e31902 | 2013-06-11 17:29:18 +0200 | [diff] [blame] | 13 | #include <traceevent/event-parse.h> |
| 14 | #include <linux/hw_breakpoint.h> |
| 15 | #include <linux/perf_event.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 16 | #include <linux/err.h> |
Andi Kleen | bec1967 | 2013-08-04 19:41:26 -0700 | [diff] [blame] | 17 | #include <sys/resource.h> |
Robert Richter | 4e31902 | 2013-06-11 17:29:18 +0200 | [diff] [blame] | 18 | #include "asm/bug.h" |
Arnaldo Carvalho de Melo | 8f651ea | 2014-10-09 16:12:24 -0300 | [diff] [blame] | 19 | #include "callchain.h" |
Arnaldo Carvalho de Melo | f14d570 | 2014-10-17 12:17:40 -0300 | [diff] [blame] | 20 | #include "cgroup.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 21 | #include "evsel.h" |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 22 | #include "evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 23 | #include "util.h" |
Arnaldo Carvalho de Melo | 86bd5e8 | 2011-01-03 23:09:46 -0200 | [diff] [blame] | 24 | #include "cpumap.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 25 | #include "thread_map.h" |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 26 | #include "target.h" |
Jiri Olsa | 26d3302 | 2012-08-07 15:20:47 +0200 | [diff] [blame] | 27 | #include "perf_regs.h" |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 28 | #include "debug.h" |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 29 | #include "trace-event.h" |
Jiri Olsa | a9a3a4d | 2015-06-14 10:19:26 +0200 | [diff] [blame] | 30 | #include "stat.h" |
Andi Kleen | ac12f67 | 2016-10-12 14:02:06 -0700 | [diff] [blame] | 31 | #include "util/parse-branch-options.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 32 | |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 33 | static struct { |
| 34 | bool sample_id_all; |
| 35 | bool exclude_guest; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 36 | bool mmap2; |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 37 | bool cloexec; |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 38 | bool clockid; |
| 39 | bool clockid_wrong; |
Andi Kleen | bd0f889 | 2015-12-11 16:12:24 -0800 | [diff] [blame] | 40 | bool lbr_flags; |
Wang Nan | b90dc17 | 2016-05-20 16:38:23 +0000 | [diff] [blame] | 41 | bool write_backward; |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 42 | } perf_missing_features; |
| 43 | |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 44 | static clockid_t clockid; |
| 45 | |
Arnaldo Carvalho de Melo | ce8ccff | 2014-10-09 15:29:51 -0300 | [diff] [blame] | 46 | static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused) |
| 47 | { |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | static struct { |
| 56 | size_t size; |
| 57 | int (*init)(struct perf_evsel *evsel); |
| 58 | void (*fini)(struct perf_evsel *evsel); |
| 59 | } perf_evsel__object = { |
| 60 | .size = sizeof(struct perf_evsel), |
| 61 | .init = perf_evsel__no_extra_init, |
| 62 | .fini = perf_evsel__no_extra_fini, |
| 63 | }; |
| 64 | |
| 65 | int perf_evsel__object_config(size_t object_size, |
| 66 | int (*init)(struct perf_evsel *evsel), |
| 67 | void (*fini)(struct perf_evsel *evsel)) |
| 68 | { |
| 69 | |
| 70 | if (object_size == 0) |
| 71 | goto set_methods; |
| 72 | |
| 73 | if (perf_evsel__object.size > object_size) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | perf_evsel__object.size = object_size; |
| 77 | |
| 78 | set_methods: |
| 79 | if (init != NULL) |
| 80 | perf_evsel__object.init = init; |
| 81 | |
| 82 | if (fini != NULL) |
| 83 | perf_evsel__object.fini = fini; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 88 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
| 89 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 90 | int __perf_evsel__sample_size(u64 sample_type) |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 91 | { |
| 92 | u64 mask = sample_type & PERF_SAMPLE_MASK; |
| 93 | int size = 0; |
| 94 | int i; |
| 95 | |
| 96 | for (i = 0; i < 64; i++) { |
| 97 | if (mask & (1ULL << i)) |
| 98 | size++; |
| 99 | } |
| 100 | |
| 101 | size *= sizeof(u64); |
| 102 | |
| 103 | return size; |
| 104 | } |
| 105 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 106 | /** |
| 107 | * __perf_evsel__calc_id_pos - calculate id_pos. |
| 108 | * @sample_type: sample type |
| 109 | * |
| 110 | * This function returns the position of the event id (PERF_SAMPLE_ID or |
| 111 | * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct |
| 112 | * sample_event. |
| 113 | */ |
| 114 | static int __perf_evsel__calc_id_pos(u64 sample_type) |
| 115 | { |
| 116 | int idx = 0; |
| 117 | |
| 118 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 119 | return 0; |
| 120 | |
| 121 | if (!(sample_type & PERF_SAMPLE_ID)) |
| 122 | return -1; |
| 123 | |
| 124 | if (sample_type & PERF_SAMPLE_IP) |
| 125 | idx += 1; |
| 126 | |
| 127 | if (sample_type & PERF_SAMPLE_TID) |
| 128 | idx += 1; |
| 129 | |
| 130 | if (sample_type & PERF_SAMPLE_TIME) |
| 131 | idx += 1; |
| 132 | |
| 133 | if (sample_type & PERF_SAMPLE_ADDR) |
| 134 | idx += 1; |
| 135 | |
| 136 | return idx; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * __perf_evsel__calc_is_pos - calculate is_pos. |
| 141 | * @sample_type: sample type |
| 142 | * |
| 143 | * This function returns the position (counting backwards) of the event id |
| 144 | * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if |
| 145 | * sample_id_all is used there is an id sample appended to non-sample events. |
| 146 | */ |
| 147 | static int __perf_evsel__calc_is_pos(u64 sample_type) |
| 148 | { |
| 149 | int idx = 1; |
| 150 | |
| 151 | if (sample_type & PERF_SAMPLE_IDENTIFIER) |
| 152 | return 1; |
| 153 | |
| 154 | if (!(sample_type & PERF_SAMPLE_ID)) |
| 155 | return -1; |
| 156 | |
| 157 | if (sample_type & PERF_SAMPLE_CPU) |
| 158 | idx += 1; |
| 159 | |
| 160 | if (sample_type & PERF_SAMPLE_STREAM_ID) |
| 161 | idx += 1; |
| 162 | |
| 163 | return idx; |
| 164 | } |
| 165 | |
| 166 | void perf_evsel__calc_id_pos(struct perf_evsel *evsel) |
| 167 | { |
| 168 | evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type); |
| 169 | evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type); |
| 170 | } |
| 171 | |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 172 | void __perf_evsel__set_sample_bit(struct perf_evsel *evsel, |
| 173 | enum perf_event_sample_format bit) |
| 174 | { |
| 175 | if (!(evsel->attr.sample_type & bit)) { |
| 176 | evsel->attr.sample_type |= bit; |
| 177 | evsel->sample_size += sizeof(u64); |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 178 | perf_evsel__calc_id_pos(evsel); |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel, |
| 183 | enum perf_event_sample_format bit) |
| 184 | { |
| 185 | if (evsel->attr.sample_type & bit) { |
| 186 | evsel->attr.sample_type &= ~bit; |
| 187 | evsel->sample_size -= sizeof(u64); |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 188 | perf_evsel__calc_id_pos(evsel); |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 192 | void perf_evsel__set_sample_id(struct perf_evsel *evsel, |
| 193 | bool can_sample_identifier) |
Arnaldo Carvalho de Melo | 7a5a5ca | 2012-12-10 15:21:30 -0300 | [diff] [blame] | 194 | { |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 195 | if (can_sample_identifier) { |
| 196 | perf_evsel__reset_sample_bit(evsel, ID); |
| 197 | perf_evsel__set_sample_bit(evsel, IDENTIFIER); |
| 198 | } else { |
| 199 | perf_evsel__set_sample_bit(evsel, ID); |
| 200 | } |
Arnaldo Carvalho de Melo | 7a5a5ca | 2012-12-10 15:21:30 -0300 | [diff] [blame] | 201 | evsel->attr.read_format |= PERF_FORMAT_ID; |
| 202 | } |
| 203 | |
Arnaldo Carvalho de Melo | 5496bc0 | 2016-07-07 11:51:47 -0300 | [diff] [blame] | 204 | /** |
| 205 | * perf_evsel__is_function_event - Return whether given evsel is a function |
| 206 | * trace event |
| 207 | * |
| 208 | * @evsel - evsel selector to be tested |
| 209 | * |
| 210 | * Return %true if event is function trace event |
| 211 | */ |
| 212 | bool perf_evsel__is_function_event(struct perf_evsel *evsel) |
| 213 | { |
| 214 | #define FUNCTION_EVENT "ftrace:function" |
| 215 | |
| 216 | return evsel->name && |
| 217 | !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT)); |
| 218 | |
| 219 | #undef FUNCTION_EVENT |
| 220 | } |
| 221 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 222 | void perf_evsel__init(struct perf_evsel *evsel, |
| 223 | struct perf_event_attr *attr, int idx) |
| 224 | { |
| 225 | evsel->idx = idx; |
Adrian Hunter | 60b0896 | 2014-07-31 09:00:52 +0300 | [diff] [blame] | 226 | evsel->tracking = !idx; |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 227 | evsel->attr = *attr; |
Namhyung Kim | 2cfda56 | 2012-11-29 15:38:29 +0900 | [diff] [blame] | 228 | evsel->leader = evsel; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 229 | evsel->unit = ""; |
| 230 | evsel->scale = 1.0; |
Arnaldo Carvalho de Melo | d49e469 | 2015-08-27 08:07:40 -0400 | [diff] [blame] | 231 | evsel->evlist = NULL; |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 232 | evsel->bpf_fd = -1; |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 233 | INIT_LIST_HEAD(&evsel->node); |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 234 | INIT_LIST_HEAD(&evsel->config_terms); |
Arnaldo Carvalho de Melo | ce8ccff | 2014-10-09 15:29:51 -0300 | [diff] [blame] | 235 | perf_evsel__object.init(evsel); |
Arnaldo Carvalho de Melo | bde0946 | 2012-08-01 18:53:11 -0300 | [diff] [blame] | 236 | evsel->sample_size = __perf_evsel__sample_size(attr->sample_type); |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 237 | perf_evsel__calc_id_pos(evsel); |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 238 | evsel->cmdline_group_boundary = false; |
Andi Kleen | 37932c1 | 2017-03-20 13:17:08 -0700 | [diff] [blame^] | 239 | evsel->metric_expr = NULL; |
| 240 | evsel->metric_events = NULL; |
| 241 | evsel->collect_stat = false; |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 242 | } |
| 243 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 244 | struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx) |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 245 | { |
Arnaldo Carvalho de Melo | ce8ccff | 2014-10-09 15:29:51 -0300 | [diff] [blame] | 246 | struct perf_evsel *evsel = zalloc(perf_evsel__object.size); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 247 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 248 | if (evsel != NULL) |
| 249 | perf_evsel__init(evsel, attr, idx); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 250 | |
Wang Nan | 03e0a7d | 2016-02-22 09:10:37 +0000 | [diff] [blame] | 251 | if (perf_evsel__is_bpf_output(evsel)) { |
Wang Nan | d37ba88 | 2016-04-01 13:26:42 +0000 | [diff] [blame] | 252 | evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | |
| 253 | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), |
Wang Nan | 03e0a7d | 2016-02-22 09:10:37 +0000 | [diff] [blame] | 254 | evsel->attr.sample_period = 1; |
| 255 | } |
| 256 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 257 | return evsel; |
| 258 | } |
| 259 | |
Arnaldo Carvalho de Melo | 7c48dcf | 2016-07-28 18:33:20 -0300 | [diff] [blame] | 260 | struct perf_evsel *perf_evsel__new_cycles(void) |
| 261 | { |
| 262 | struct perf_event_attr attr = { |
| 263 | .type = PERF_TYPE_HARDWARE, |
| 264 | .config = PERF_COUNT_HW_CPU_CYCLES, |
| 265 | }; |
| 266 | struct perf_evsel *evsel; |
| 267 | |
| 268 | event_attr_init(&attr); |
| 269 | |
| 270 | perf_event_attr__set_max_precise_ip(&attr); |
| 271 | |
| 272 | evsel = perf_evsel__new(&attr); |
| 273 | if (evsel == NULL) |
| 274 | goto out; |
| 275 | |
| 276 | /* use asprintf() because free(evsel) assumes name is allocated */ |
| 277 | if (asprintf(&evsel->name, "cycles%.*s", |
| 278 | attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0) |
| 279 | goto error_free; |
| 280 | out: |
| 281 | return evsel; |
| 282 | error_free: |
| 283 | perf_evsel__delete(evsel); |
| 284 | evsel = NULL; |
| 285 | goto out; |
| 286 | } |
| 287 | |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 288 | /* |
| 289 | * Returns pointer with encoded error via <linux/err.h> interface. |
| 290 | */ |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 291 | struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx) |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 292 | { |
Arnaldo Carvalho de Melo | ce8ccff | 2014-10-09 15:29:51 -0300 | [diff] [blame] | 293 | struct perf_evsel *evsel = zalloc(perf_evsel__object.size); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 294 | int err = -ENOMEM; |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 295 | |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 296 | if (evsel == NULL) { |
| 297 | goto out_err; |
| 298 | } else { |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 299 | struct perf_event_attr attr = { |
Arnaldo Carvalho de Melo | 0b80f8b3 | 2012-09-26 12:28:26 -0300 | [diff] [blame] | 300 | .type = PERF_TYPE_TRACEPOINT, |
| 301 | .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | |
| 302 | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD), |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 303 | }; |
| 304 | |
Arnaldo Carvalho de Melo | e48ffe2 | 2012-09-26 17:11:38 -0300 | [diff] [blame] | 305 | if (asprintf(&evsel->name, "%s:%s", sys, name) < 0) |
| 306 | goto out_free; |
| 307 | |
Jiri Olsa | 97978b3 | 2013-12-03 14:09:24 +0100 | [diff] [blame] | 308 | evsel->tp_format = trace_event__tp_format(sys, name); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 309 | if (IS_ERR(evsel->tp_format)) { |
| 310 | err = PTR_ERR(evsel->tp_format); |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 311 | goto out_free; |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 312 | } |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 313 | |
Arnaldo Carvalho de Melo | 0b80f8b3 | 2012-09-26 12:28:26 -0300 | [diff] [blame] | 314 | event_attr_init(&attr); |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 315 | attr.config = evsel->tp_format->id; |
Arnaldo Carvalho de Melo | 0b80f8b3 | 2012-09-26 12:28:26 -0300 | [diff] [blame] | 316 | attr.sample_period = 1; |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 317 | perf_evsel__init(evsel, &attr, idx); |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | return evsel; |
| 321 | |
| 322 | out_free: |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 323 | zfree(&evsel->name); |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 324 | free(evsel); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 325 | out_err: |
| 326 | return ERR_PTR(err); |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 327 | } |
| 328 | |
Arnaldo Carvalho de Melo | 8ad7013 | 2012-09-06 13:11:18 -0300 | [diff] [blame] | 329 | const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = { |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 330 | "cycles", |
| 331 | "instructions", |
| 332 | "cache-references", |
| 333 | "cache-misses", |
| 334 | "branches", |
| 335 | "branch-misses", |
| 336 | "bus-cycles", |
| 337 | "stalled-cycles-frontend", |
| 338 | "stalled-cycles-backend", |
| 339 | "ref-cycles", |
| 340 | }; |
| 341 | |
Arnaldo Carvalho de Melo | dd4f522 | 2012-06-13 15:52:42 -0300 | [diff] [blame] | 342 | static const char *__perf_evsel__hw_name(u64 config) |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 343 | { |
| 344 | if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config]) |
| 345 | return perf_evsel__hw_names[config]; |
| 346 | |
| 347 | return "unknown-hardware"; |
| 348 | } |
| 349 | |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 350 | static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size) |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 351 | { |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 352 | int colon = 0, r = 0; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 353 | struct perf_event_attr *attr = &evsel->attr; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 354 | bool exclude_guest_default = false; |
| 355 | |
| 356 | #define MOD_PRINT(context, mod) do { \ |
| 357 | if (!attr->exclude_##context) { \ |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 358 | if (!colon) colon = ++r; \ |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 359 | r += scnprintf(bf + r, size - r, "%c", mod); \ |
| 360 | } } while(0) |
| 361 | |
| 362 | if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) { |
| 363 | MOD_PRINT(kernel, 'k'); |
| 364 | MOD_PRINT(user, 'u'); |
| 365 | MOD_PRINT(hv, 'h'); |
| 366 | exclude_guest_default = true; |
| 367 | } |
| 368 | |
| 369 | if (attr->precise_ip) { |
| 370 | if (!colon) |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 371 | colon = ++r; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 372 | r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp"); |
| 373 | exclude_guest_default = true; |
| 374 | } |
| 375 | |
| 376 | if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) { |
| 377 | MOD_PRINT(host, 'H'); |
| 378 | MOD_PRINT(guest, 'G'); |
| 379 | } |
| 380 | #undef MOD_PRINT |
| 381 | if (colon) |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 382 | bf[colon - 1] = ':'; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 383 | return r; |
| 384 | } |
| 385 | |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 386 | static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 387 | { |
| 388 | int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config)); |
| 389 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); |
| 390 | } |
| 391 | |
Arnaldo Carvalho de Melo | 8ad7013 | 2012-09-06 13:11:18 -0300 | [diff] [blame] | 392 | const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = { |
Arnaldo Carvalho de Melo | 335c2f5 | 2012-06-11 14:36:20 -0300 | [diff] [blame] | 393 | "cpu-clock", |
| 394 | "task-clock", |
| 395 | "page-faults", |
| 396 | "context-switches", |
Arnaldo Carvalho de Melo | 8ad7013 | 2012-09-06 13:11:18 -0300 | [diff] [blame] | 397 | "cpu-migrations", |
Arnaldo Carvalho de Melo | 335c2f5 | 2012-06-11 14:36:20 -0300 | [diff] [blame] | 398 | "minor-faults", |
| 399 | "major-faults", |
| 400 | "alignment-faults", |
| 401 | "emulation-faults", |
Adrian Hunter | d22d1a2 | 2013-08-31 21:50:52 +0300 | [diff] [blame] | 402 | "dummy", |
Arnaldo Carvalho de Melo | 335c2f5 | 2012-06-11 14:36:20 -0300 | [diff] [blame] | 403 | }; |
| 404 | |
Arnaldo Carvalho de Melo | dd4f522 | 2012-06-13 15:52:42 -0300 | [diff] [blame] | 405 | static const char *__perf_evsel__sw_name(u64 config) |
Arnaldo Carvalho de Melo | 335c2f5 | 2012-06-11 14:36:20 -0300 | [diff] [blame] | 406 | { |
| 407 | if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config]) |
| 408 | return perf_evsel__sw_names[config]; |
| 409 | return "unknown-software"; |
| 410 | } |
| 411 | |
| 412 | static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 413 | { |
| 414 | int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config)); |
| 415 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); |
| 416 | } |
| 417 | |
Jiri Olsa | 287e74a | 2012-06-28 23:18:49 +0200 | [diff] [blame] | 418 | static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type) |
| 419 | { |
| 420 | int r; |
| 421 | |
| 422 | r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr); |
| 423 | |
| 424 | if (type & HW_BREAKPOINT_R) |
| 425 | r += scnprintf(bf + r, size - r, "r"); |
| 426 | |
| 427 | if (type & HW_BREAKPOINT_W) |
| 428 | r += scnprintf(bf + r, size - r, "w"); |
| 429 | |
| 430 | if (type & HW_BREAKPOINT_X) |
| 431 | r += scnprintf(bf + r, size - r, "x"); |
| 432 | |
| 433 | return r; |
| 434 | } |
| 435 | |
| 436 | static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 437 | { |
| 438 | struct perf_event_attr *attr = &evsel->attr; |
| 439 | int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type); |
| 440 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); |
| 441 | } |
| 442 | |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 443 | const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] |
| 444 | [PERF_EVSEL__MAX_ALIASES] = { |
| 445 | { "L1-dcache", "l1-d", "l1d", "L1-data", }, |
| 446 | { "L1-icache", "l1-i", "l1i", "L1-instruction", }, |
| 447 | { "LLC", "L2", }, |
| 448 | { "dTLB", "d-tlb", "Data-TLB", }, |
| 449 | { "iTLB", "i-tlb", "Instruction-TLB", }, |
| 450 | { "branch", "branches", "bpu", "btb", "bpc", }, |
| 451 | { "node", }, |
| 452 | }; |
| 453 | |
| 454 | const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] |
| 455 | [PERF_EVSEL__MAX_ALIASES] = { |
| 456 | { "load", "loads", "read", }, |
| 457 | { "store", "stores", "write", }, |
| 458 | { "prefetch", "prefetches", "speculative-read", "speculative-load", }, |
| 459 | }; |
| 460 | |
| 461 | const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] |
| 462 | [PERF_EVSEL__MAX_ALIASES] = { |
| 463 | { "refs", "Reference", "ops", "access", }, |
| 464 | { "misses", "miss", }, |
| 465 | }; |
| 466 | |
| 467 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 468 | #define CACHE_READ (1 << C(OP_READ)) |
| 469 | #define CACHE_WRITE (1 << C(OP_WRITE)) |
| 470 | #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) |
| 471 | #define COP(x) (1 << x) |
| 472 | |
| 473 | /* |
| 474 | * cache operartion stat |
| 475 | * L1I : Read and prefetch only |
| 476 | * ITLB and BPU : Read-only |
| 477 | */ |
| 478 | static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = { |
| 479 | [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 480 | [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), |
| 481 | [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 482 | [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 483 | [C(ITLB)] = (CACHE_READ), |
| 484 | [C(BPU)] = (CACHE_READ), |
| 485 | [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 486 | }; |
| 487 | |
| 488 | bool perf_evsel__is_cache_op_valid(u8 type, u8 op) |
| 489 | { |
| 490 | if (perf_evsel__hw_cache_stat[type] & COP(op)) |
| 491 | return true; /* valid */ |
| 492 | else |
| 493 | return false; /* invalid */ |
| 494 | } |
| 495 | |
| 496 | int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, |
| 497 | char *bf, size_t size) |
| 498 | { |
| 499 | if (result) { |
| 500 | return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0], |
| 501 | perf_evsel__hw_cache_op[op][0], |
| 502 | perf_evsel__hw_cache_result[result][0]); |
| 503 | } |
| 504 | |
| 505 | return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0], |
| 506 | perf_evsel__hw_cache_op[op][1]); |
| 507 | } |
| 508 | |
Arnaldo Carvalho de Melo | dd4f522 | 2012-06-13 15:52:42 -0300 | [diff] [blame] | 509 | static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size) |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 510 | { |
| 511 | u8 op, result, type = (config >> 0) & 0xff; |
| 512 | const char *err = "unknown-ext-hardware-cache-type"; |
| 513 | |
Arnaldo Carvalho de Melo | c53412e | 2016-08-18 16:30:28 -0300 | [diff] [blame] | 514 | if (type >= PERF_COUNT_HW_CACHE_MAX) |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 515 | goto out_err; |
| 516 | |
| 517 | op = (config >> 8) & 0xff; |
| 518 | err = "unknown-ext-hardware-cache-op"; |
Arnaldo Carvalho de Melo | c53412e | 2016-08-18 16:30:28 -0300 | [diff] [blame] | 519 | if (op >= PERF_COUNT_HW_CACHE_OP_MAX) |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 520 | goto out_err; |
| 521 | |
| 522 | result = (config >> 16) & 0xff; |
| 523 | err = "unknown-ext-hardware-cache-result"; |
Arnaldo Carvalho de Melo | c53412e | 2016-08-18 16:30:28 -0300 | [diff] [blame] | 524 | if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX) |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 525 | goto out_err; |
| 526 | |
| 527 | err = "invalid-cache"; |
| 528 | if (!perf_evsel__is_cache_op_valid(type, op)) |
| 529 | goto out_err; |
| 530 | |
| 531 | return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size); |
| 532 | out_err: |
| 533 | return scnprintf(bf, size, "%s", err); |
| 534 | } |
| 535 | |
| 536 | static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 537 | { |
| 538 | int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size); |
| 539 | return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret); |
| 540 | } |
| 541 | |
Arnaldo Carvalho de Melo | 6eef3d9 | 2012-06-13 11:53:37 -0300 | [diff] [blame] | 542 | static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 543 | { |
| 544 | int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config); |
| 545 | return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret); |
| 546 | } |
| 547 | |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 548 | const char *perf_evsel__name(struct perf_evsel *evsel) |
Arnaldo Carvalho de Melo | a446083 | 2012-06-12 10:29:12 -0300 | [diff] [blame] | 549 | { |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 550 | char bf[128]; |
Arnaldo Carvalho de Melo | a446083 | 2012-06-12 10:29:12 -0300 | [diff] [blame] | 551 | |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 552 | if (evsel->name) |
| 553 | return evsel->name; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 554 | |
| 555 | switch (evsel->attr.type) { |
| 556 | case PERF_TYPE_RAW: |
Arnaldo Carvalho de Melo | 6eef3d9 | 2012-06-13 11:53:37 -0300 | [diff] [blame] | 557 | perf_evsel__raw_name(evsel, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 558 | break; |
| 559 | |
| 560 | case PERF_TYPE_HARDWARE: |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 561 | perf_evsel__hw_name(evsel, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 562 | break; |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 563 | |
| 564 | case PERF_TYPE_HW_CACHE: |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 565 | perf_evsel__hw_cache_name(evsel, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 566 | break; |
| 567 | |
Arnaldo Carvalho de Melo | 335c2f5 | 2012-06-11 14:36:20 -0300 | [diff] [blame] | 568 | case PERF_TYPE_SOFTWARE: |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 569 | perf_evsel__sw_name(evsel, bf, sizeof(bf)); |
Arnaldo Carvalho de Melo | 335c2f5 | 2012-06-11 14:36:20 -0300 | [diff] [blame] | 570 | break; |
| 571 | |
Arnaldo Carvalho de Melo | a446083 | 2012-06-12 10:29:12 -0300 | [diff] [blame] | 572 | case PERF_TYPE_TRACEPOINT: |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 573 | scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint"); |
Arnaldo Carvalho de Melo | a446083 | 2012-06-12 10:29:12 -0300 | [diff] [blame] | 574 | break; |
| 575 | |
Jiri Olsa | 287e74a | 2012-06-28 23:18:49 +0200 | [diff] [blame] | 576 | case PERF_TYPE_BREAKPOINT: |
| 577 | perf_evsel__bp_name(evsel, bf, sizeof(bf)); |
| 578 | break; |
| 579 | |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 580 | default: |
Robert Richter | ca1b145 | 2012-08-16 21:10:18 +0200 | [diff] [blame] | 581 | scnprintf(bf, sizeof(bf), "unknown attr type: %d", |
| 582 | evsel->attr.type); |
Arnaldo Carvalho de Melo | a446083 | 2012-06-12 10:29:12 -0300 | [diff] [blame] | 583 | break; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 584 | } |
| 585 | |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 586 | evsel->name = strdup(bf); |
| 587 | |
| 588 | return evsel->name ?: "unknown"; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 589 | } |
| 590 | |
Namhyung Kim | 717e263 | 2013-01-22 18:09:44 +0900 | [diff] [blame] | 591 | const char *perf_evsel__group_name(struct perf_evsel *evsel) |
| 592 | { |
| 593 | return evsel->group_name ?: "anon group"; |
| 594 | } |
| 595 | |
| 596 | int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size) |
| 597 | { |
| 598 | int ret; |
| 599 | struct perf_evsel *pos; |
| 600 | const char *group_name = perf_evsel__group_name(evsel); |
| 601 | |
| 602 | ret = scnprintf(buf, size, "%s", group_name); |
| 603 | |
| 604 | ret += scnprintf(buf + ret, size - ret, " { %s", |
| 605 | perf_evsel__name(evsel)); |
| 606 | |
| 607 | for_each_group_member(pos, evsel) |
| 608 | ret += scnprintf(buf + ret, size - ret, ", %s", |
| 609 | perf_evsel__name(pos)); |
| 610 | |
| 611 | ret += scnprintf(buf + ret, size - ret, " }"); |
| 612 | |
| 613 | return ret; |
| 614 | } |
| 615 | |
Arnaldo Carvalho de Melo | 01e0d50 | 2016-04-11 18:39:37 -0300 | [diff] [blame] | 616 | void perf_evsel__config_callchain(struct perf_evsel *evsel, |
| 617 | struct record_opts *opts, |
| 618 | struct callchain_param *param) |
Jiri Olsa | 6bedfab | 2014-03-02 16:56:40 +0100 | [diff] [blame] | 619 | { |
| 620 | bool function = perf_evsel__is_function_event(evsel); |
| 621 | struct perf_event_attr *attr = &evsel->attr; |
| 622 | |
| 623 | perf_evsel__set_sample_bit(evsel, CALLCHAIN); |
| 624 | |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 625 | attr->sample_max_stack = param->max_stack; |
| 626 | |
Kan Liang | c3a6a8c | 2015-08-04 04:30:20 -0400 | [diff] [blame] | 627 | if (param->record_mode == CALLCHAIN_LBR) { |
Kan Liang | aad2b21 | 2015-01-05 13:23:04 -0500 | [diff] [blame] | 628 | if (!opts->branch_stack) { |
| 629 | if (attr->exclude_user) { |
| 630 | pr_warning("LBR callstack option is only available " |
| 631 | "to get user callchain information. " |
| 632 | "Falling back to framepointers.\n"); |
| 633 | } else { |
| 634 | perf_evsel__set_sample_bit(evsel, BRANCH_STACK); |
| 635 | attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER | |
Andi Kleen | bd0f889 | 2015-12-11 16:12:24 -0800 | [diff] [blame] | 636 | PERF_SAMPLE_BRANCH_CALL_STACK | |
| 637 | PERF_SAMPLE_BRANCH_NO_CYCLES | |
| 638 | PERF_SAMPLE_BRANCH_NO_FLAGS; |
Kan Liang | aad2b21 | 2015-01-05 13:23:04 -0500 | [diff] [blame] | 639 | } |
| 640 | } else |
| 641 | pr_warning("Cannot use LBR callstack with branch stack. " |
| 642 | "Falling back to framepointers.\n"); |
| 643 | } |
| 644 | |
Kan Liang | c3a6a8c | 2015-08-04 04:30:20 -0400 | [diff] [blame] | 645 | if (param->record_mode == CALLCHAIN_DWARF) { |
Jiri Olsa | 6bedfab | 2014-03-02 16:56:40 +0100 | [diff] [blame] | 646 | if (!function) { |
| 647 | perf_evsel__set_sample_bit(evsel, REGS_USER); |
| 648 | perf_evsel__set_sample_bit(evsel, STACK_USER); |
| 649 | attr->sample_regs_user = PERF_REGS_MASK; |
Kan Liang | c3a6a8c | 2015-08-04 04:30:20 -0400 | [diff] [blame] | 650 | attr->sample_stack_user = param->dump_size; |
Jiri Olsa | 6bedfab | 2014-03-02 16:56:40 +0100 | [diff] [blame] | 651 | attr->exclude_callchain_user = 1; |
| 652 | } else { |
| 653 | pr_info("Cannot use DWARF unwind for function trace event," |
| 654 | " falling back to framepointers.\n"); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | if (function) { |
| 659 | pr_info("Disabling user space callchains for function trace event.\n"); |
| 660 | attr->exclude_callchain_user = 1; |
| 661 | } |
| 662 | } |
| 663 | |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 664 | static void |
| 665 | perf_evsel__reset_callgraph(struct perf_evsel *evsel, |
| 666 | struct callchain_param *param) |
| 667 | { |
| 668 | struct perf_event_attr *attr = &evsel->attr; |
| 669 | |
| 670 | perf_evsel__reset_sample_bit(evsel, CALLCHAIN); |
| 671 | if (param->record_mode == CALLCHAIN_LBR) { |
| 672 | perf_evsel__reset_sample_bit(evsel, BRANCH_STACK); |
| 673 | attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER | |
| 674 | PERF_SAMPLE_BRANCH_CALL_STACK); |
| 675 | } |
| 676 | if (param->record_mode == CALLCHAIN_DWARF) { |
| 677 | perf_evsel__reset_sample_bit(evsel, REGS_USER); |
| 678 | perf_evsel__reset_sample_bit(evsel, STACK_USER); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | static void apply_config_terms(struct perf_evsel *evsel, |
| 683 | struct record_opts *opts) |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 684 | { |
| 685 | struct perf_evsel_config_term *term; |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 686 | struct list_head *config_terms = &evsel->config_terms; |
| 687 | struct perf_event_attr *attr = &evsel->attr; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 688 | struct callchain_param param; |
| 689 | u32 dump_size = 0; |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 690 | int max_stack = 0; |
| 691 | const char *callgraph_buf = NULL; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 692 | |
| 693 | /* callgraph default */ |
| 694 | param.record_mode = callchain_param.record_mode; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 695 | |
| 696 | list_for_each_entry(term, config_terms, list) { |
| 697 | switch (term->type) { |
Jiri Olsa | ee4c758 | 2015-07-29 05:42:11 -0400 | [diff] [blame] | 698 | case PERF_EVSEL__CONFIG_TERM_PERIOD: |
| 699 | attr->sample_period = term->val.period; |
Jiri Olsa | ab35a7d | 2015-08-08 19:12:10 +0200 | [diff] [blame] | 700 | attr->freq = 0; |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 701 | break; |
Namhyung Kim | 09af2a5 | 2015-08-09 15:45:23 +0900 | [diff] [blame] | 702 | case PERF_EVSEL__CONFIG_TERM_FREQ: |
| 703 | attr->sample_freq = term->val.freq; |
| 704 | attr->freq = 1; |
| 705 | break; |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 706 | case PERF_EVSEL__CONFIG_TERM_TIME: |
| 707 | if (term->val.time) |
| 708 | perf_evsel__set_sample_bit(evsel, TIME); |
| 709 | else |
| 710 | perf_evsel__reset_sample_bit(evsel, TIME); |
| 711 | break; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 712 | case PERF_EVSEL__CONFIG_TERM_CALLGRAPH: |
| 713 | callgraph_buf = term->val.callgraph; |
| 714 | break; |
Andi Kleen | ac12f67 | 2016-10-12 14:02:06 -0700 | [diff] [blame] | 715 | case PERF_EVSEL__CONFIG_TERM_BRANCH: |
| 716 | if (term->val.branch && strcmp(term->val.branch, "no")) { |
| 717 | perf_evsel__set_sample_bit(evsel, BRANCH_STACK); |
| 718 | parse_branch_str(term->val.branch, |
| 719 | &attr->branch_sample_type); |
| 720 | } else |
| 721 | perf_evsel__reset_sample_bit(evsel, BRANCH_STACK); |
| 722 | break; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 723 | case PERF_EVSEL__CONFIG_TERM_STACK_USER: |
| 724 | dump_size = term->val.stack_user; |
| 725 | break; |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 726 | case PERF_EVSEL__CONFIG_TERM_MAX_STACK: |
| 727 | max_stack = term->val.max_stack; |
| 728 | break; |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 729 | case PERF_EVSEL__CONFIG_TERM_INHERIT: |
| 730 | /* |
| 731 | * attr->inherit should has already been set by |
| 732 | * perf_evsel__config. If user explicitly set |
| 733 | * inherit using config terms, override global |
| 734 | * opt->no_inherit setting. |
| 735 | */ |
| 736 | attr->inherit = term->val.inherit ? 1 : 0; |
| 737 | break; |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 738 | case PERF_EVSEL__CONFIG_TERM_OVERWRITE: |
| 739 | attr->write_backward = term->val.overwrite ? 1 : 0; |
| 740 | break; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 741 | default: |
| 742 | break; |
| 743 | } |
| 744 | } |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 745 | |
| 746 | /* User explicitly set per-event callgraph, clear the old setting and reset. */ |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 747 | if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) { |
| 748 | if (max_stack) { |
| 749 | param.max_stack = max_stack; |
| 750 | if (callgraph_buf == NULL) |
| 751 | callgraph_buf = "fp"; |
| 752 | } |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 753 | |
| 754 | /* parse callgraph parameters */ |
| 755 | if (callgraph_buf != NULL) { |
Kan Liang | f9db0d0 | 2015-08-11 06:30:48 -0400 | [diff] [blame] | 756 | if (!strcmp(callgraph_buf, "no")) { |
| 757 | param.enabled = false; |
| 758 | param.record_mode = CALLCHAIN_NONE; |
| 759 | } else { |
| 760 | param.enabled = true; |
| 761 | if (parse_callchain_record(callgraph_buf, ¶m)) { |
| 762 | pr_err("per-event callgraph setting for %s failed. " |
| 763 | "Apply callgraph global setting for it\n", |
| 764 | evsel->name); |
| 765 | return; |
| 766 | } |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | if (dump_size > 0) { |
| 770 | dump_size = round_up(dump_size, sizeof(u64)); |
| 771 | param.dump_size = dump_size; |
| 772 | } |
| 773 | |
| 774 | /* If global callgraph set, clear it */ |
| 775 | if (callchain_param.enabled) |
| 776 | perf_evsel__reset_callgraph(evsel, &callchain_param); |
| 777 | |
| 778 | /* set perf-event callgraph */ |
| 779 | if (param.enabled) |
Arnaldo Carvalho de Melo | 01e0d50 | 2016-04-11 18:39:37 -0300 | [diff] [blame] | 780 | perf_evsel__config_callchain(evsel, opts, ¶m); |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 781 | } |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Jiri Olsa | 774cb49 | 2012-11-12 18:34:01 +0100 | [diff] [blame] | 784 | /* |
| 785 | * The enable_on_exec/disabled value strategy: |
| 786 | * |
| 787 | * 1) For any type of traced program: |
| 788 | * - all independent events and group leaders are disabled |
| 789 | * - all group members are enabled |
| 790 | * |
| 791 | * Group members are ruled by group leaders. They need to |
| 792 | * be enabled, because the group scheduling relies on that. |
| 793 | * |
| 794 | * 2) For traced programs executed by perf: |
| 795 | * - all independent events and group leaders have |
| 796 | * enable_on_exec set |
| 797 | * - we don't specifically enable or disable any event during |
| 798 | * the record command |
| 799 | * |
| 800 | * Independent events and group leaders are initially disabled |
| 801 | * and get enabled by exec. Group members are ruled by group |
| 802 | * leaders as stated in 1). |
| 803 | * |
| 804 | * 3) For traced programs attached by perf (pid/tid): |
| 805 | * - we specifically enable or disable all events during |
| 806 | * the record command |
| 807 | * |
| 808 | * When attaching events to already running traced we |
| 809 | * enable/disable events specifically, as there's no |
| 810 | * initial traced exec call. |
| 811 | */ |
Arnaldo Carvalho de Melo | e68ae9c | 2016-04-11 18:15:29 -0300 | [diff] [blame] | 812 | void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts, |
| 813 | struct callchain_param *callchain) |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 814 | { |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 815 | struct perf_evsel *leader = evsel->leader; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 816 | struct perf_event_attr *attr = &evsel->attr; |
Adrian Hunter | 60b0896 | 2014-07-31 09:00:52 +0300 | [diff] [blame] | 817 | int track = evsel->tracking; |
Adrian Hunter | 3aa5939 | 2013-11-15 15:52:29 +0200 | [diff] [blame] | 818 | bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 819 | |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 820 | attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 821 | attr->inherit = !opts->no_inherit; |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 822 | attr->write_backward = opts->overwrite ? 1 : 0; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 823 | |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 824 | perf_evsel__set_sample_bit(evsel, IP); |
| 825 | perf_evsel__set_sample_bit(evsel, TID); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 826 | |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 827 | if (evsel->sample_read) { |
| 828 | perf_evsel__set_sample_bit(evsel, READ); |
| 829 | |
| 830 | /* |
| 831 | * We need ID even in case of single event, because |
| 832 | * PERF_SAMPLE_READ process ID specific data. |
| 833 | */ |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 834 | perf_evsel__set_sample_id(evsel, false); |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 835 | |
| 836 | /* |
| 837 | * Apply group format only if we belong to group |
| 838 | * with more than one members. |
| 839 | */ |
| 840 | if (leader->nr_members > 1) { |
| 841 | attr->read_format |= PERF_FORMAT_GROUP; |
| 842 | attr->inherit = 0; |
| 843 | } |
| 844 | } |
| 845 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 846 | /* |
Namhyung Kim | 17314e2 | 2014-06-09 14:43:37 +0900 | [diff] [blame] | 847 | * We default some events to have a default interval. But keep |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 848 | * it a weak assumption overridable by the user. |
| 849 | */ |
Namhyung Kim | 17314e2 | 2014-06-09 14:43:37 +0900 | [diff] [blame] | 850 | if (!attr->sample_period || (opts->user_freq != UINT_MAX || |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 851 | opts->user_interval != ULLONG_MAX)) { |
| 852 | if (opts->freq) { |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 853 | perf_evsel__set_sample_bit(evsel, PERIOD); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 854 | attr->freq = 1; |
| 855 | attr->sample_freq = opts->freq; |
| 856 | } else { |
| 857 | attr->sample_period = opts->default_interval; |
| 858 | } |
| 859 | } |
| 860 | |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 861 | /* |
| 862 | * Disable sampling for all group members other |
| 863 | * than leader in case leader 'leads' the sampling. |
| 864 | */ |
| 865 | if ((leader != evsel) && leader->sample_read) { |
| 866 | attr->sample_freq = 0; |
| 867 | attr->sample_period = 0; |
| 868 | } |
| 869 | |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 870 | if (opts->no_samples) |
| 871 | attr->sample_freq = 0; |
| 872 | |
| 873 | if (opts->inherit_stat) |
| 874 | attr->inherit_stat = 1; |
| 875 | |
| 876 | if (opts->sample_address) { |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 877 | perf_evsel__set_sample_bit(evsel, ADDR); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 878 | attr->mmap_data = track; |
| 879 | } |
| 880 | |
Jiri Olsa | f140373 | 2014-11-13 18:21:03 +0100 | [diff] [blame] | 881 | /* |
| 882 | * We don't allow user space callchains for function trace |
| 883 | * event, due to issues with page faults while tracing page |
| 884 | * fault handler and its overall trickiness nature. |
| 885 | */ |
| 886 | if (perf_evsel__is_function_event(evsel)) |
| 887 | evsel->attr.exclude_callchain_user = 1; |
| 888 | |
Arnaldo Carvalho de Melo | e68ae9c | 2016-04-11 18:15:29 -0300 | [diff] [blame] | 889 | if (callchain && callchain->enabled && !evsel->no_aux_samples) |
Arnaldo Carvalho de Melo | 01e0d50 | 2016-04-11 18:39:37 -0300 | [diff] [blame] | 890 | perf_evsel__config_callchain(evsel, opts, callchain); |
Jiri Olsa | 26d3302 | 2012-08-07 15:20:47 +0200 | [diff] [blame] | 891 | |
Stephane Eranian | 6a21c0b | 2014-09-24 13:48:39 +0200 | [diff] [blame] | 892 | if (opts->sample_intr_regs) { |
Stephane Eranian | bcc84ec | 2015-08-31 18:41:12 +0200 | [diff] [blame] | 893 | attr->sample_regs_intr = opts->sample_intr_regs; |
Stephane Eranian | 6a21c0b | 2014-09-24 13:48:39 +0200 | [diff] [blame] | 894 | perf_evsel__set_sample_bit(evsel, REGS_INTR); |
| 895 | } |
| 896 | |
Jiri Olsa | b6f35ed | 2016-08-01 20:02:35 +0200 | [diff] [blame] | 897 | if (target__has_cpu(&opts->target) || opts->sample_cpu) |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 898 | perf_evsel__set_sample_bit(evsel, CPU); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 899 | |
Andrew Vagin | 3e76ac7 | 2011-12-20 17:32:45 +0300 | [diff] [blame] | 900 | if (opts->period) |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 901 | perf_evsel__set_sample_bit(evsel, PERIOD); |
Andrew Vagin | 3e76ac7 | 2011-12-20 17:32:45 +0300 | [diff] [blame] | 902 | |
Andi Kleen | 8affc2b | 2014-07-31 14:45:04 +0800 | [diff] [blame] | 903 | /* |
Adam Buchbinder | bd1a0be5 | 2016-02-24 10:02:25 -0800 | [diff] [blame] | 904 | * When the user explicitly disabled time don't force it here. |
Andi Kleen | 8affc2b | 2014-07-31 14:45:04 +0800 | [diff] [blame] | 905 | */ |
| 906 | if (opts->sample_time && |
| 907 | (!perf_missing_features.sample_id_all && |
Adrian Hunter | 3abebc5 | 2015-07-06 14:51:01 +0300 | [diff] [blame] | 908 | (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu || |
| 909 | opts->sample_time_set))) |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 910 | perf_evsel__set_sample_bit(evsel, TIME); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 911 | |
Adrian Hunter | 6ff1ce7 | 2014-07-14 13:02:56 +0300 | [diff] [blame] | 912 | if (opts->raw_samples && !evsel->no_aux_samples) { |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 913 | perf_evsel__set_sample_bit(evsel, TIME); |
| 914 | perf_evsel__set_sample_bit(evsel, RAW); |
| 915 | perf_evsel__set_sample_bit(evsel, CPU); |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 916 | } |
| 917 | |
Stephane Eranian | ccf49bf | 2013-01-24 16:10:37 +0100 | [diff] [blame] | 918 | if (opts->sample_address) |
Adrian Hunter | 1e7ed5e | 2013-11-01 15:51:35 +0200 | [diff] [blame] | 919 | perf_evsel__set_sample_bit(evsel, DATA_SRC); |
Stephane Eranian | ccf49bf | 2013-01-24 16:10:37 +0100 | [diff] [blame] | 920 | |
Arnaldo Carvalho de Melo | 509051e | 2014-01-14 17:52:14 -0300 | [diff] [blame] | 921 | if (opts->no_buffering) { |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 922 | attr->watermark = 0; |
| 923 | attr->wakeup_events = 1; |
| 924 | } |
Adrian Hunter | 6ff1ce7 | 2014-07-14 13:02:56 +0300 | [diff] [blame] | 925 | if (opts->branch_stack && !evsel->no_aux_samples) { |
Arnaldo Carvalho de Melo | 7be5ebe | 2012-12-10 14:53:43 -0300 | [diff] [blame] | 926 | perf_evsel__set_sample_bit(evsel, BRANCH_STACK); |
Roberto Agostino Vitillo | bdfebd8 | 2012-02-09 23:21:02 +0100 | [diff] [blame] | 927 | attr->branch_sample_type = opts->branch_stack; |
| 928 | } |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 929 | |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 930 | if (opts->sample_weight) |
Adrian Hunter | 1e7ed5e | 2013-11-01 15:51:35 +0200 | [diff] [blame] | 931 | perf_evsel__set_sample_bit(evsel, WEIGHT); |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 932 | |
Namhyung Kim | 62e503b | 2015-01-29 17:06:46 +0900 | [diff] [blame] | 933 | attr->task = track; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 934 | attr->mmap = track; |
Don Zickus | a5a5ba7 | 2014-05-30 10:49:42 -0400 | [diff] [blame] | 935 | attr->mmap2 = track && !perf_missing_features.mmap2; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 936 | attr->comm = track; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 937 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 938 | if (opts->record_namespaces) |
| 939 | attr->namespaces = track; |
| 940 | |
Adrian Hunter | b757bb0 | 2015-07-21 12:44:04 +0300 | [diff] [blame] | 941 | if (opts->record_switch_events) |
| 942 | attr->context_switch = track; |
| 943 | |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 944 | if (opts->sample_transaction) |
Adrian Hunter | 1e7ed5e | 2013-11-01 15:51:35 +0200 | [diff] [blame] | 945 | perf_evsel__set_sample_bit(evsel, TRANSACTION); |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 946 | |
Andi Kleen | 85c273d | 2015-02-24 15:13:40 -0800 | [diff] [blame] | 947 | if (opts->running_time) { |
| 948 | evsel->attr.read_format |= |
| 949 | PERF_FORMAT_TOTAL_TIME_ENABLED | |
| 950 | PERF_FORMAT_TOTAL_TIME_RUNNING; |
| 951 | } |
| 952 | |
Jiri Olsa | 774cb49 | 2012-11-12 18:34:01 +0100 | [diff] [blame] | 953 | /* |
| 954 | * XXX see the function comment above |
| 955 | * |
| 956 | * Disabling only independent events or group leaders, |
| 957 | * keeping group members enabled. |
| 958 | */ |
Namhyung Kim | 823254e | 2012-11-29 15:38:30 +0900 | [diff] [blame] | 959 | if (perf_evsel__is_group_leader(evsel)) |
Jiri Olsa | 774cb49 | 2012-11-12 18:34:01 +0100 | [diff] [blame] | 960 | attr->disabled = 1; |
| 961 | |
| 962 | /* |
| 963 | * Setting enable_on_exec for independent events and |
| 964 | * group leaders for traced executed by perf. |
| 965 | */ |
Andi Kleen | 6619a53 | 2014-01-11 13:38:27 -0800 | [diff] [blame] | 966 | if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) && |
| 967 | !opts->initial_delay) |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 968 | attr->enable_on_exec = 1; |
Adrian Hunter | 2afd2bc | 2014-07-14 13:02:57 +0300 | [diff] [blame] | 969 | |
| 970 | if (evsel->immediate) { |
| 971 | attr->disabled = 0; |
| 972 | attr->enable_on_exec = 0; |
| 973 | } |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 974 | |
| 975 | clockid = opts->clockid; |
| 976 | if (opts->use_clockid) { |
| 977 | attr->use_clockid = 1; |
| 978 | attr->clockid = opts->clockid; |
| 979 | } |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 980 | |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 981 | if (evsel->precise_max) |
| 982 | perf_event_attr__set_max_precise_ip(attr); |
| 983 | |
Jiri Olsa | 8572388 | 2016-02-15 09:34:31 +0100 | [diff] [blame] | 984 | if (opts->all_user) { |
| 985 | attr->exclude_kernel = 1; |
| 986 | attr->exclude_user = 0; |
| 987 | } |
| 988 | |
| 989 | if (opts->all_kernel) { |
| 990 | attr->exclude_kernel = 0; |
| 991 | attr->exclude_user = 1; |
| 992 | } |
| 993 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 994 | /* |
| 995 | * Apply event specific term settings, |
| 996 | * it overloads any global configuration. |
| 997 | */ |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 998 | apply_config_terms(evsel, opts); |
Jiri Olsa | a359c17 | 2016-12-13 08:46:22 +0100 | [diff] [blame] | 999 | |
| 1000 | evsel->ignore_missing_thread = opts->ignore_missing_thread; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 1001 | } |
| 1002 | |
Arnaldo Carvalho de Melo | 8885846 | 2014-10-13 13:30:27 -0300 | [diff] [blame] | 1003 | static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1004 | { |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1005 | if (evsel->system_wide) |
| 1006 | nthreads = 1; |
| 1007 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1008 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); |
David Ahern | 4af4c95 | 2011-05-27 09:58:34 -0600 | [diff] [blame] | 1009 | |
| 1010 | if (evsel->fd) { |
Arnaldo Carvalho de Melo | 18ef15c | 2016-10-03 11:07:24 -0300 | [diff] [blame] | 1011 | int cpu, thread; |
David Ahern | 4af4c95 | 2011-05-27 09:58:34 -0600 | [diff] [blame] | 1012 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 1013 | for (thread = 0; thread < nthreads; thread++) { |
| 1014 | FD(evsel, cpu, thread) = -1; |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1019 | return evsel->fd != NULL ? 0 : -ENOMEM; |
| 1020 | } |
| 1021 | |
Andi Kleen | e2407be | 2013-08-02 17:41:10 -0700 | [diff] [blame] | 1022 | static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads, |
| 1023 | int ioc, void *arg) |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1024 | { |
| 1025 | int cpu, thread; |
| 1026 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1027 | if (evsel->system_wide) |
| 1028 | nthreads = 1; |
| 1029 | |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1030 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 1031 | for (thread = 0; thread < nthreads; thread++) { |
| 1032 | int fd = FD(evsel, cpu, thread), |
Andi Kleen | e2407be | 2013-08-02 17:41:10 -0700 | [diff] [blame] | 1033 | err = ioctl(fd, ioc, arg); |
Arnaldo Carvalho de Melo | 745cefc | 2012-09-26 15:07:39 -0300 | [diff] [blame] | 1034 | |
| 1035 | if (err) |
| 1036 | return err; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Arnaldo Carvalho de Melo | f47805a | 2015-07-03 15:53:49 -0300 | [diff] [blame] | 1043 | int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads, |
| 1044 | const char *filter) |
Andi Kleen | e2407be | 2013-08-02 17:41:10 -0700 | [diff] [blame] | 1045 | { |
| 1046 | return perf_evsel__run_ioctl(evsel, ncpus, nthreads, |
| 1047 | PERF_EVENT_IOC_SET_FILTER, |
| 1048 | (void *)filter); |
| 1049 | } |
| 1050 | |
Arnaldo Carvalho de Melo | 12467ae | 2015-07-03 17:05:50 -0300 | [diff] [blame] | 1051 | int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter) |
| 1052 | { |
| 1053 | char *new_filter = strdup(filter); |
| 1054 | |
| 1055 | if (new_filter != NULL) { |
| 1056 | free(evsel->filter); |
| 1057 | evsel->filter = new_filter; |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
Mathieu Poirier | 3541c03 | 2016-09-16 08:44:04 -0600 | [diff] [blame] | 1064 | static int perf_evsel__append_filter(struct perf_evsel *evsel, |
| 1065 | const char *fmt, const char *filter) |
Arnaldo Carvalho de Melo | 64ec84f | 2015-07-04 12:19:13 -0300 | [diff] [blame] | 1066 | { |
| 1067 | char *new_filter; |
| 1068 | |
| 1069 | if (evsel->filter == NULL) |
| 1070 | return perf_evsel__set_filter(evsel, filter); |
| 1071 | |
Mathieu Poirier | b15d0a4 | 2016-09-16 08:44:03 -0600 | [diff] [blame] | 1072 | if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) { |
Arnaldo Carvalho de Melo | 64ec84f | 2015-07-04 12:19:13 -0300 | [diff] [blame] | 1073 | free(evsel->filter); |
| 1074 | evsel->filter = new_filter; |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | return -1; |
| 1079 | } |
| 1080 | |
Mathieu Poirier | 3541c03 | 2016-09-16 08:44:04 -0600 | [diff] [blame] | 1081 | int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter) |
| 1082 | { |
| 1083 | return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter); |
| 1084 | } |
| 1085 | |
Mathieu Poirier | 1e85748 | 2016-09-16 08:44:05 -0600 | [diff] [blame] | 1086 | int perf_evsel__append_addr_filter(struct perf_evsel *evsel, const char *filter) |
| 1087 | { |
| 1088 | return perf_evsel__append_filter(evsel, "%s,%s", filter); |
| 1089 | } |
| 1090 | |
Jiri Olsa | 5cd95fc | 2015-12-03 10:06:40 +0100 | [diff] [blame] | 1091 | int perf_evsel__enable(struct perf_evsel *evsel) |
Andi Kleen | e2407be | 2013-08-02 17:41:10 -0700 | [diff] [blame] | 1092 | { |
Jiri Olsa | 5cd95fc | 2015-12-03 10:06:40 +0100 | [diff] [blame] | 1093 | int nthreads = thread_map__nr(evsel->threads); |
| 1094 | int ncpus = cpu_map__nr(evsel->cpus); |
| 1095 | |
Andi Kleen | e2407be | 2013-08-02 17:41:10 -0700 | [diff] [blame] | 1096 | return perf_evsel__run_ioctl(evsel, ncpus, nthreads, |
| 1097 | PERF_EVENT_IOC_ENABLE, |
| 1098 | 0); |
| 1099 | } |
| 1100 | |
Jiri Olsa | e98a4cb | 2015-12-03 10:06:41 +0100 | [diff] [blame] | 1101 | int perf_evsel__disable(struct perf_evsel *evsel) |
| 1102 | { |
| 1103 | int nthreads = thread_map__nr(evsel->threads); |
| 1104 | int ncpus = cpu_map__nr(evsel->cpus); |
| 1105 | |
| 1106 | return perf_evsel__run_ioctl(evsel, ncpus, nthreads, |
| 1107 | PERF_EVENT_IOC_DISABLE, |
| 1108 | 0); |
| 1109 | } |
| 1110 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 1111 | int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 1112 | { |
Vineet Gupta | 8d9cbd8 | 2015-01-13 19:13:23 +0530 | [diff] [blame] | 1113 | if (ncpus == 0 || nthreads == 0) |
| 1114 | return 0; |
| 1115 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1116 | if (evsel->system_wide) |
| 1117 | nthreads = 1; |
| 1118 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1119 | evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); |
| 1120 | if (evsel->sample_id == NULL) |
| 1121 | return -ENOMEM; |
| 1122 | |
| 1123 | evsel->id = zalloc(ncpus * nthreads * sizeof(u64)); |
| 1124 | if (evsel->id == NULL) { |
| 1125 | xyarray__delete(evsel->sample_id); |
| 1126 | evsel->sample_id = NULL; |
| 1127 | return -ENOMEM; |
| 1128 | } |
| 1129 | |
| 1130 | return 0; |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Arnaldo Carvalho de Melo | 8885846 | 2014-10-13 13:30:27 -0300 | [diff] [blame] | 1133 | static void perf_evsel__free_fd(struct perf_evsel *evsel) |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1134 | { |
| 1135 | xyarray__delete(evsel->fd); |
| 1136 | evsel->fd = NULL; |
| 1137 | } |
| 1138 | |
Arnaldo Carvalho de Melo | 8885846 | 2014-10-13 13:30:27 -0300 | [diff] [blame] | 1139 | static void perf_evsel__free_id(struct perf_evsel *evsel) |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 1140 | { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1141 | xyarray__delete(evsel->sample_id); |
| 1142 | evsel->sample_id = NULL; |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1143 | zfree(&evsel->id); |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1146 | static void perf_evsel__free_config_terms(struct perf_evsel *evsel) |
| 1147 | { |
| 1148 | struct perf_evsel_config_term *term, *h; |
| 1149 | |
| 1150 | list_for_each_entry_safe(term, h, &evsel->config_terms, list) { |
| 1151 | list_del(&term->list); |
| 1152 | free(term); |
| 1153 | } |
| 1154 | } |
| 1155 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 1156 | void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 1157 | { |
| 1158 | int cpu, thread; |
| 1159 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1160 | if (evsel->system_wide) |
| 1161 | nthreads = 1; |
| 1162 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 1163 | for (cpu = 0; cpu < ncpus; cpu++) |
| 1164 | for (thread = 0; thread < nthreads; ++thread) { |
| 1165 | close(FD(evsel, cpu, thread)); |
| 1166 | FD(evsel, cpu, thread) = -1; |
| 1167 | } |
| 1168 | } |
| 1169 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 1170 | void perf_evsel__exit(struct perf_evsel *evsel) |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1171 | { |
| 1172 | assert(list_empty(&evsel->node)); |
Arnaldo Carvalho de Melo | d49e469 | 2015-08-27 08:07:40 -0400 | [diff] [blame] | 1173 | assert(evsel->evlist == NULL); |
Namhyung Kim | 736b05a | 2013-03-15 14:48:49 +0900 | [diff] [blame] | 1174 | perf_evsel__free_fd(evsel); |
| 1175 | perf_evsel__free_id(evsel); |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1176 | perf_evsel__free_config_terms(evsel); |
Arnaldo Carvalho de Melo | 597e48c | 2014-10-16 13:25:01 -0300 | [diff] [blame] | 1177 | close_cgroup(evsel->cgrp); |
Jiri Olsa | f30a79b | 2015-06-23 00:36:04 +0200 | [diff] [blame] | 1178 | cpu_map__put(evsel->cpus); |
Adrian Hunter | fce4d29 | 2015-09-08 10:58:55 +0300 | [diff] [blame] | 1179 | cpu_map__put(evsel->own_cpus); |
Jiri Olsa | 578e91e | 2015-06-23 00:36:07 +0200 | [diff] [blame] | 1180 | thread_map__put(evsel->threads); |
Arnaldo Carvalho de Melo | 597e48c | 2014-10-16 13:25:01 -0300 | [diff] [blame] | 1181 | zfree(&evsel->group_name); |
Arnaldo Carvalho de Melo | 597e48c | 2014-10-16 13:25:01 -0300 | [diff] [blame] | 1182 | zfree(&evsel->name); |
Arnaldo Carvalho de Melo | ce8ccff | 2014-10-09 15:29:51 -0300 | [diff] [blame] | 1183 | perf_evsel__object.fini(evsel); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | void perf_evsel__delete(struct perf_evsel *evsel) |
| 1187 | { |
| 1188 | perf_evsel__exit(evsel); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1189 | free(evsel); |
| 1190 | } |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 1191 | |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 1192 | void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread, |
Jiri Olsa | 857a94a | 2014-11-21 10:31:05 +0100 | [diff] [blame] | 1193 | struct perf_counts_values *count) |
Stephane Eranian | c7a79c4 | 2013-01-29 12:47:43 +0100 | [diff] [blame] | 1194 | { |
| 1195 | struct perf_counts_values tmp; |
| 1196 | |
| 1197 | if (!evsel->prev_raw_counts) |
| 1198 | return; |
| 1199 | |
| 1200 | if (cpu == -1) { |
| 1201 | tmp = evsel->prev_raw_counts->aggr; |
| 1202 | evsel->prev_raw_counts->aggr = *count; |
| 1203 | } else { |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 1204 | tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread); |
| 1205 | *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count; |
Stephane Eranian | c7a79c4 | 2013-01-29 12:47:43 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | count->val = count->val - tmp.val; |
| 1209 | count->ena = count->ena - tmp.ena; |
| 1210 | count->run = count->run - tmp.run; |
| 1211 | } |
| 1212 | |
Jiri Olsa | 13112bb | 2014-11-21 10:31:06 +0100 | [diff] [blame] | 1213 | void perf_counts_values__scale(struct perf_counts_values *count, |
| 1214 | bool scale, s8 *pscaled) |
| 1215 | { |
| 1216 | s8 scaled = 0; |
| 1217 | |
| 1218 | if (scale) { |
| 1219 | if (count->run == 0) { |
| 1220 | scaled = -1; |
| 1221 | count->val = 0; |
| 1222 | } else if (count->run < count->ena) { |
| 1223 | scaled = 1; |
| 1224 | count->val = (u64)((double) count->val * count->ena / count->run + 0.5); |
| 1225 | } |
| 1226 | } else |
| 1227 | count->ena = count->run = 0; |
| 1228 | |
| 1229 | if (pscaled) |
| 1230 | *pscaled = scaled; |
| 1231 | } |
| 1232 | |
Jiri Olsa | f99f4719 | 2015-06-26 11:29:18 +0200 | [diff] [blame] | 1233 | int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread, |
| 1234 | struct perf_counts_values *count) |
| 1235 | { |
| 1236 | memset(count, 0, sizeof(*count)); |
| 1237 | |
| 1238 | if (FD(evsel, cpu, thread) < 0) |
| 1239 | return -EINVAL; |
| 1240 | |
| 1241 | if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0) |
| 1242 | return -errno; |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 1247 | int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, |
| 1248 | int cpu, int thread, bool scale) |
| 1249 | { |
| 1250 | struct perf_counts_values count; |
| 1251 | size_t nv = scale ? 3 : 1; |
| 1252 | |
| 1253 | if (FD(evsel, cpu, thread) < 0) |
| 1254 | return -EINVAL; |
| 1255 | |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 1256 | if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0) |
Arnaldo Carvalho de Melo | 4eed11d | 2011-01-04 00:13:17 -0200 | [diff] [blame] | 1257 | return -ENOMEM; |
| 1258 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 1259 | if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0) |
| 1260 | return -errno; |
| 1261 | |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 1262 | perf_evsel__compute_deltas(evsel, cpu, thread, &count); |
Jiri Olsa | 13112bb | 2014-11-21 10:31:06 +0100 | [diff] [blame] | 1263 | perf_counts_values__scale(&count, scale, NULL); |
Jiri Olsa | a6fa003 | 2015-06-26 11:29:11 +0200 | [diff] [blame] | 1264 | *perf_counts(evsel->counts, cpu, thread) = count; |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 1265 | return 0; |
| 1266 | } |
| 1267 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1268 | static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread) |
| 1269 | { |
| 1270 | struct perf_evsel *leader = evsel->leader; |
| 1271 | int fd; |
| 1272 | |
Namhyung Kim | 823254e | 2012-11-29 15:38:30 +0900 | [diff] [blame] | 1273 | if (perf_evsel__is_group_leader(evsel)) |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1274 | return -1; |
| 1275 | |
| 1276 | /* |
| 1277 | * Leader must be already processed/open, |
| 1278 | * if not it's a bug. |
| 1279 | */ |
| 1280 | BUG_ON(!leader->fd); |
| 1281 | |
| 1282 | fd = FD(leader, cpu, thread); |
| 1283 | BUG_ON(fd == -1); |
| 1284 | |
| 1285 | return fd; |
| 1286 | } |
| 1287 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1288 | struct bit_names { |
| 1289 | int bit; |
| 1290 | const char *name; |
| 1291 | }; |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1292 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1293 | static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits) |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1294 | { |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1295 | bool first_bit = true; |
| 1296 | int i = 0; |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1297 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1298 | do { |
| 1299 | if (value & bits[i].bit) { |
| 1300 | buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name); |
| 1301 | first_bit = false; |
| 1302 | } |
| 1303 | } while (bits[++i].name != NULL); |
| 1304 | } |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1305 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1306 | static void __p_sample_type(char *buf, size_t size, u64 value) |
| 1307 | { |
| 1308 | #define bit_name(n) { PERF_SAMPLE_##n, #n } |
| 1309 | struct bit_names bits[] = { |
| 1310 | bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR), |
| 1311 | bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU), |
| 1312 | bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW), |
| 1313 | bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER), |
Jiri Olsa | 84422592 | 2015-10-05 20:06:02 +0200 | [diff] [blame] | 1314 | bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC), |
Jiri Olsa | dcdd184 | 2015-11-25 16:36:55 +0100 | [diff] [blame] | 1315 | bit_name(WEIGHT), |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1316 | { .name = NULL, } |
| 1317 | }; |
| 1318 | #undef bit_name |
| 1319 | __p_bits(buf, size, value, bits); |
| 1320 | } |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1321 | |
Arnaldo Carvalho de Melo | a213b92 | 2016-04-25 16:45:29 -0300 | [diff] [blame] | 1322 | static void __p_branch_sample_type(char *buf, size_t size, u64 value) |
| 1323 | { |
| 1324 | #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n } |
| 1325 | struct bit_names bits[] = { |
| 1326 | bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY), |
| 1327 | bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL), |
| 1328 | bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX), |
| 1329 | bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP), |
| 1330 | bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES), |
| 1331 | { .name = NULL, } |
| 1332 | }; |
| 1333 | #undef bit_name |
| 1334 | __p_bits(buf, size, value, bits); |
| 1335 | } |
| 1336 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1337 | static void __p_read_format(char *buf, size_t size, u64 value) |
| 1338 | { |
| 1339 | #define bit_name(n) { PERF_FORMAT_##n, #n } |
| 1340 | struct bit_names bits[] = { |
| 1341 | bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING), |
| 1342 | bit_name(ID), bit_name(GROUP), |
| 1343 | { .name = NULL, } |
| 1344 | }; |
| 1345 | #undef bit_name |
| 1346 | __p_bits(buf, size, value, bits); |
| 1347 | } |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 1348 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1349 | #define BUF_SIZE 1024 |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1350 | |
Adrian Hunter | 7310aed | 2015-06-11 15:51:04 +0300 | [diff] [blame] | 1351 | #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val)) |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1352 | #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val)) |
| 1353 | #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val)) |
| 1354 | #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val) |
Arnaldo Carvalho de Melo | a213b92 | 2016-04-25 16:45:29 -0300 | [diff] [blame] | 1355 | #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val) |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1356 | #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val) |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1357 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1358 | #define PRINT_ATTRn(_n, _f, _p) \ |
| 1359 | do { \ |
| 1360 | if (attr->_f) { \ |
| 1361 | _p(attr->_f); \ |
| 1362 | ret += attr__fprintf(fp, _n, buf, priv);\ |
| 1363 | } \ |
| 1364 | } while (0) |
| 1365 | |
| 1366 | #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p) |
| 1367 | |
| 1368 | int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr, |
| 1369 | attr__fprintf_f attr__fprintf, void *priv) |
| 1370 | { |
| 1371 | char buf[BUF_SIZE]; |
| 1372 | int ret = 0; |
| 1373 | |
| 1374 | PRINT_ATTRf(type, p_unsigned); |
| 1375 | PRINT_ATTRf(size, p_unsigned); |
| 1376 | PRINT_ATTRf(config, p_hex); |
| 1377 | PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned); |
| 1378 | PRINT_ATTRf(sample_type, p_sample_type); |
| 1379 | PRINT_ATTRf(read_format, p_read_format); |
| 1380 | |
| 1381 | PRINT_ATTRf(disabled, p_unsigned); |
| 1382 | PRINT_ATTRf(inherit, p_unsigned); |
| 1383 | PRINT_ATTRf(pinned, p_unsigned); |
| 1384 | PRINT_ATTRf(exclusive, p_unsigned); |
| 1385 | PRINT_ATTRf(exclude_user, p_unsigned); |
| 1386 | PRINT_ATTRf(exclude_kernel, p_unsigned); |
| 1387 | PRINT_ATTRf(exclude_hv, p_unsigned); |
| 1388 | PRINT_ATTRf(exclude_idle, p_unsigned); |
| 1389 | PRINT_ATTRf(mmap, p_unsigned); |
| 1390 | PRINT_ATTRf(comm, p_unsigned); |
| 1391 | PRINT_ATTRf(freq, p_unsigned); |
| 1392 | PRINT_ATTRf(inherit_stat, p_unsigned); |
| 1393 | PRINT_ATTRf(enable_on_exec, p_unsigned); |
| 1394 | PRINT_ATTRf(task, p_unsigned); |
| 1395 | PRINT_ATTRf(watermark, p_unsigned); |
| 1396 | PRINT_ATTRf(precise_ip, p_unsigned); |
| 1397 | PRINT_ATTRf(mmap_data, p_unsigned); |
| 1398 | PRINT_ATTRf(sample_id_all, p_unsigned); |
| 1399 | PRINT_ATTRf(exclude_host, p_unsigned); |
| 1400 | PRINT_ATTRf(exclude_guest, p_unsigned); |
| 1401 | PRINT_ATTRf(exclude_callchain_kernel, p_unsigned); |
| 1402 | PRINT_ATTRf(exclude_callchain_user, p_unsigned); |
| 1403 | PRINT_ATTRf(mmap2, p_unsigned); |
| 1404 | PRINT_ATTRf(comm_exec, p_unsigned); |
| 1405 | PRINT_ATTRf(use_clockid, p_unsigned); |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1406 | PRINT_ATTRf(context_switch, p_unsigned); |
Arnaldo Carvalho de Melo | 0a241ef | 2016-05-09 18:08:33 -0300 | [diff] [blame] | 1407 | PRINT_ATTRf(write_backward, p_unsigned); |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1408 | |
| 1409 | PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned); |
| 1410 | PRINT_ATTRf(bp_type, p_unsigned); |
| 1411 | PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex); |
| 1412 | PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex); |
Arnaldo Carvalho de Melo | a213b92 | 2016-04-25 16:45:29 -0300 | [diff] [blame] | 1413 | PRINT_ATTRf(branch_sample_type, p_branch_sample_type); |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1414 | PRINT_ATTRf(sample_regs_user, p_hex); |
| 1415 | PRINT_ATTRf(sample_stack_user, p_unsigned); |
| 1416 | PRINT_ATTRf(clockid, p_signed); |
| 1417 | PRINT_ATTRf(sample_regs_intr, p_hex); |
Adrian Hunter | 70d73de | 2015-04-09 18:54:06 +0300 | [diff] [blame] | 1418 | PRINT_ATTRf(aux_watermark, p_unsigned); |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 1419 | PRINT_ATTRf(sample_max_stack, p_unsigned); |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1420 | |
| 1421 | return ret; |
| 1422 | } |
| 1423 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1424 | static int __open_attr__fprintf(FILE *fp, const char *name, const char *val, |
| 1425 | void *priv __attribute__((unused))) |
| 1426 | { |
| 1427 | return fprintf(fp, " %-32s %s\n", name, val); |
| 1428 | } |
| 1429 | |
Jiri Olsa | a359c17 | 2016-12-13 08:46:22 +0100 | [diff] [blame] | 1430 | static bool ignore_missing_thread(struct perf_evsel *evsel, |
| 1431 | struct thread_map *threads, |
| 1432 | int thread, int err) |
| 1433 | { |
| 1434 | if (!evsel->ignore_missing_thread) |
| 1435 | return false; |
| 1436 | |
| 1437 | /* The system wide setup does not work with threads. */ |
| 1438 | if (evsel->system_wide) |
| 1439 | return false; |
| 1440 | |
| 1441 | /* The -ESRCH is perf event syscall errno for pid's not found. */ |
| 1442 | if (err != -ESRCH) |
| 1443 | return false; |
| 1444 | |
| 1445 | /* If there's only one thread, let it fail. */ |
| 1446 | if (threads->nr == 1) |
| 1447 | return false; |
| 1448 | |
| 1449 | if (thread_map__remove(threads, thread)) |
| 1450 | return false; |
| 1451 | |
| 1452 | pr_warning("WARNING: Ignored open failure for pid %d\n", |
| 1453 | thread_map__pid(threads, thread)); |
| 1454 | return true; |
| 1455 | } |
| 1456 | |
Arnaldo Carvalho de Melo | c24ae6d | 2017-02-14 10:59:04 -0300 | [diff] [blame] | 1457 | int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
| 1458 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 1459 | { |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1460 | int cpu, thread, nthreads; |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 1461 | unsigned long flags = PERF_FLAG_FD_CLOEXEC; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1462 | int pid = -1, err; |
Andi Kleen | bec1967 | 2013-08-04 19:41:26 -0700 | [diff] [blame] | 1463 | enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE; |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 1464 | |
Arnaldo Carvalho de Melo | 32a951b | 2016-07-14 08:34:33 +0000 | [diff] [blame] | 1465 | if (perf_missing_features.write_backward && evsel->attr.write_backward) |
| 1466 | return -EINVAL; |
| 1467 | |
Arnaldo Carvalho de Melo | c24ae6d | 2017-02-14 10:59:04 -0300 | [diff] [blame] | 1468 | if (cpus == NULL) { |
| 1469 | static struct cpu_map *empty_cpu_map; |
| 1470 | |
| 1471 | if (empty_cpu_map == NULL) { |
| 1472 | empty_cpu_map = cpu_map__dummy_new(); |
| 1473 | if (empty_cpu_map == NULL) |
| 1474 | return -ENOMEM; |
| 1475 | } |
| 1476 | |
| 1477 | cpus = empty_cpu_map; |
| 1478 | } |
| 1479 | |
| 1480 | if (threads == NULL) { |
| 1481 | static struct thread_map *empty_thread_map; |
| 1482 | |
| 1483 | if (empty_thread_map == NULL) { |
| 1484 | empty_thread_map = thread_map__new_by_tid(-1); |
| 1485 | if (empty_thread_map == NULL) |
| 1486 | return -ENOMEM; |
| 1487 | } |
| 1488 | |
| 1489 | threads = empty_thread_map; |
| 1490 | } |
| 1491 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1492 | if (evsel->system_wide) |
| 1493 | nthreads = 1; |
| 1494 | else |
| 1495 | nthreads = threads->nr; |
| 1496 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 1497 | if (evsel->fd == NULL && |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1498 | perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1499 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 4eed11d | 2011-01-04 00:13:17 -0200 | [diff] [blame] | 1500 | |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1501 | if (evsel->cgrp) { |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 1502 | flags |= PERF_FLAG_PID_CGROUP; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1503 | pid = evsel->cgrp->fd; |
| 1504 | } |
| 1505 | |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 1506 | fallback_missing_features: |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 1507 | if (perf_missing_features.clockid_wrong) |
| 1508 | evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */ |
| 1509 | if (perf_missing_features.clockid) { |
| 1510 | evsel->attr.use_clockid = 0; |
| 1511 | evsel->attr.clockid = 0; |
| 1512 | } |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 1513 | if (perf_missing_features.cloexec) |
| 1514 | flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1515 | if (perf_missing_features.mmap2) |
| 1516 | evsel->attr.mmap2 = 0; |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 1517 | if (perf_missing_features.exclude_guest) |
| 1518 | evsel->attr.exclude_guest = evsel->attr.exclude_host = 0; |
Andi Kleen | bd0f889 | 2015-12-11 16:12:24 -0800 | [diff] [blame] | 1519 | if (perf_missing_features.lbr_flags) |
| 1520 | evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS | |
| 1521 | PERF_SAMPLE_BRANCH_NO_CYCLES); |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 1522 | retry_sample_id: |
| 1523 | if (perf_missing_features.sample_id_all) |
| 1524 | evsel->attr.sample_id_all = 0; |
| 1525 | |
Peter Zijlstra | 2c5e8c5 | 2015-04-07 11:09:54 +0200 | [diff] [blame] | 1526 | if (verbose >= 2) { |
| 1527 | fprintf(stderr, "%.60s\n", graph_dotted_line); |
| 1528 | fprintf(stderr, "perf_event_attr:\n"); |
| 1529 | perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL); |
| 1530 | fprintf(stderr, "%.60s\n", graph_dotted_line); |
| 1531 | } |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1532 | |
Arnaldo Carvalho de Melo | 86bd5e8 | 2011-01-03 23:09:46 -0200 | [diff] [blame] | 1533 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 1534 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1535 | for (thread = 0; thread < nthreads; thread++) { |
Jiri Olsa | 83c2e4f3 | 2016-12-12 11:35:40 +0100 | [diff] [blame] | 1536 | int fd, group_fd; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1537 | |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1538 | if (!evsel->cgrp && !evsel->system_wide) |
Jiri Olsa | e13798c | 2015-06-23 00:36:02 +0200 | [diff] [blame] | 1539 | pid = thread_map__pid(threads, thread); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1540 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1541 | group_fd = get_group_fd(evsel, cpu, thread); |
Andi Kleen | bec1967 | 2013-08-04 19:41:26 -0700 | [diff] [blame] | 1542 | retry_open: |
Jiri Olsa | 7b4b82bc | 2016-11-21 22:33:26 +0100 | [diff] [blame] | 1543 | pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx", |
Adrian Hunter | e3e1a54 | 2013-08-14 15:48:24 +0300 | [diff] [blame] | 1544 | pid, cpus->map[cpu], group_fd, flags); |
| 1545 | |
Jiri Olsa | 83c2e4f3 | 2016-12-12 11:35:40 +0100 | [diff] [blame] | 1546 | fd = sys_perf_event_open(&evsel->attr, pid, cpus->map[cpu], |
| 1547 | group_fd, flags); |
| 1548 | |
| 1549 | FD(evsel, cpu, thread) = fd; |
| 1550 | |
| 1551 | if (fd < 0) { |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1552 | err = -errno; |
Jiri Olsa | a359c17 | 2016-12-13 08:46:22 +0100 | [diff] [blame] | 1553 | |
| 1554 | if (ignore_missing_thread(evsel, threads, thread, err)) { |
| 1555 | /* |
| 1556 | * We just removed 1 thread, so take a step |
| 1557 | * back on thread index and lower the upper |
| 1558 | * nthreads limit. |
| 1559 | */ |
| 1560 | nthreads--; |
| 1561 | thread--; |
| 1562 | |
| 1563 | /* ... and pretend like nothing have happened. */ |
| 1564 | err = 0; |
| 1565 | continue; |
| 1566 | } |
| 1567 | |
Jiri Olsa | 7b4b82bc | 2016-11-21 22:33:26 +0100 | [diff] [blame] | 1568 | pr_debug2("\nsys_perf_event_open failed, error %d\n", |
Adrian Hunter | f852fd6 | 2013-11-01 15:51:29 +0200 | [diff] [blame] | 1569 | err); |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 1570 | goto try_fallback; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1571 | } |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 1572 | |
Jiri Olsa | 83c2e4f3 | 2016-12-12 11:35:40 +0100 | [diff] [blame] | 1573 | pr_debug2(" = %d\n", fd); |
Jiri Olsa | 7b4b82bc | 2016-11-21 22:33:26 +0100 | [diff] [blame] | 1574 | |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 1575 | if (evsel->bpf_fd >= 0) { |
Jiri Olsa | 83c2e4f3 | 2016-12-12 11:35:40 +0100 | [diff] [blame] | 1576 | int evt_fd = fd; |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 1577 | int bpf_fd = evsel->bpf_fd; |
| 1578 | |
| 1579 | err = ioctl(evt_fd, |
| 1580 | PERF_EVENT_IOC_SET_BPF, |
| 1581 | bpf_fd); |
| 1582 | if (err && errno != EEXIST) { |
| 1583 | pr_err("failed to attach bpf fd %d: %s\n", |
| 1584 | bpf_fd, strerror(errno)); |
| 1585 | err = -EINVAL; |
| 1586 | goto out_close; |
| 1587 | } |
| 1588 | } |
| 1589 | |
Andi Kleen | bec1967 | 2013-08-04 19:41:26 -0700 | [diff] [blame] | 1590 | set_rlimit = NO_CHANGE; |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 1591 | |
| 1592 | /* |
| 1593 | * If we succeeded but had to kill clockid, fail and |
| 1594 | * have perf_evsel__open_strerror() print us a nice |
| 1595 | * error. |
| 1596 | */ |
| 1597 | if (perf_missing_features.clockid || |
| 1598 | perf_missing_features.clockid_wrong) { |
| 1599 | err = -EINVAL; |
| 1600 | goto out_close; |
| 1601 | } |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 1602 | } |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | return 0; |
| 1606 | |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 1607 | try_fallback: |
Andi Kleen | bec1967 | 2013-08-04 19:41:26 -0700 | [diff] [blame] | 1608 | /* |
| 1609 | * perf stat needs between 5 and 22 fds per CPU. When we run out |
| 1610 | * of them try to increase the limits. |
| 1611 | */ |
| 1612 | if (err == -EMFILE && set_rlimit < INCREASED_MAX) { |
| 1613 | struct rlimit l; |
| 1614 | int old_errno = errno; |
| 1615 | |
| 1616 | if (getrlimit(RLIMIT_NOFILE, &l) == 0) { |
| 1617 | if (set_rlimit == NO_CHANGE) |
| 1618 | l.rlim_cur = l.rlim_max; |
| 1619 | else { |
| 1620 | l.rlim_cur = l.rlim_max + 1000; |
| 1621 | l.rlim_max = l.rlim_cur; |
| 1622 | } |
| 1623 | if (setrlimit(RLIMIT_NOFILE, &l) == 0) { |
| 1624 | set_rlimit++; |
| 1625 | errno = old_errno; |
| 1626 | goto retry_open; |
| 1627 | } |
| 1628 | } |
| 1629 | errno = old_errno; |
| 1630 | } |
| 1631 | |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 1632 | if (err != -EINVAL || cpu > 0 || thread > 0) |
| 1633 | goto out_close; |
| 1634 | |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 1635 | /* |
| 1636 | * Must probe features in the order they were added to the |
| 1637 | * perf_event_attr interface. |
| 1638 | */ |
Arnaldo Carvalho de Melo | 7da36e9 | 2016-06-20 10:47:18 +0000 | [diff] [blame] | 1639 | if (!perf_missing_features.write_backward && evsel->attr.write_backward) { |
| 1640 | perf_missing_features.write_backward = true; |
Arnaldo Carvalho de Melo | 32a951b | 2016-07-14 08:34:33 +0000 | [diff] [blame] | 1641 | goto out_close; |
Arnaldo Carvalho de Melo | 7da36e9 | 2016-06-20 10:47:18 +0000 | [diff] [blame] | 1642 | } else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) { |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 1643 | perf_missing_features.clockid_wrong = true; |
| 1644 | goto fallback_missing_features; |
| 1645 | } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) { |
| 1646 | perf_missing_features.clockid = true; |
| 1647 | goto fallback_missing_features; |
| 1648 | } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) { |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 1649 | perf_missing_features.cloexec = true; |
| 1650 | goto fallback_missing_features; |
| 1651 | } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) { |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1652 | perf_missing_features.mmap2 = true; |
| 1653 | goto fallback_missing_features; |
| 1654 | } else if (!perf_missing_features.exclude_guest && |
| 1655 | (evsel->attr.exclude_guest || evsel->attr.exclude_host)) { |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 1656 | perf_missing_features.exclude_guest = true; |
| 1657 | goto fallback_missing_features; |
| 1658 | } else if (!perf_missing_features.sample_id_all) { |
| 1659 | perf_missing_features.sample_id_all = true; |
| 1660 | goto retry_sample_id; |
Andi Kleen | bd0f889 | 2015-12-11 16:12:24 -0800 | [diff] [blame] | 1661 | } else if (!perf_missing_features.lbr_flags && |
| 1662 | (evsel->attr.branch_sample_type & |
| 1663 | (PERF_SAMPLE_BRANCH_NO_CYCLES | |
| 1664 | PERF_SAMPLE_BRANCH_NO_FLAGS))) { |
| 1665 | perf_missing_features.lbr_flags = true; |
| 1666 | goto fallback_missing_features; |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 1667 | } |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 1668 | out_close: |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 1669 | do { |
| 1670 | while (--thread >= 0) { |
| 1671 | close(FD(evsel, cpu, thread)); |
| 1672 | FD(evsel, cpu, thread) = -1; |
| 1673 | } |
Adrian Hunter | bf8e8f4 | 2014-07-31 09:00:51 +0300 | [diff] [blame] | 1674 | thread = nthreads; |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 1675 | } while (--cpu >= 0); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1676 | return err; |
| 1677 | } |
| 1678 | |
| 1679 | void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 1680 | { |
| 1681 | if (evsel->fd == NULL) |
| 1682 | return; |
| 1683 | |
| 1684 | perf_evsel__close_fd(evsel, ncpus, nthreads); |
| 1685 | perf_evsel__free_fd(evsel); |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 1686 | } |
| 1687 | |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 1688 | int perf_evsel__open_per_cpu(struct perf_evsel *evsel, |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1689 | struct cpu_map *cpus) |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 1690 | { |
Arnaldo Carvalho de Melo | c24ae6d | 2017-02-14 10:59:04 -0300 | [diff] [blame] | 1691 | return perf_evsel__open(evsel, cpus, NULL); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 1692 | } |
| 1693 | |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 1694 | int perf_evsel__open_per_thread(struct perf_evsel *evsel, |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1695 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 1696 | { |
Arnaldo Carvalho de Melo | c24ae6d | 2017-02-14 10:59:04 -0300 | [diff] [blame] | 1697 | return perf_evsel__open(evsel, NULL, threads); |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 1698 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 1699 | |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1700 | static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel, |
| 1701 | const union perf_event *event, |
| 1702 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1703 | { |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1704 | u64 type = evsel->attr.sample_type; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1705 | const u64 *array = event->sample.array; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1706 | bool swapped = evsel->needs_swap; |
Jiri Olsa | 37073f9 | 2012-05-30 14:23:44 +0200 | [diff] [blame] | 1707 | union u64_swap u; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1708 | |
| 1709 | array += ((event->header.size - |
| 1710 | sizeof(event->header)) / sizeof(u64)) - 1; |
| 1711 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1712 | if (type & PERF_SAMPLE_IDENTIFIER) { |
| 1713 | sample->id = *array; |
| 1714 | array--; |
| 1715 | } |
| 1716 | |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1717 | if (type & PERF_SAMPLE_CPU) { |
Jiri Olsa | 37073f9 | 2012-05-30 14:23:44 +0200 | [diff] [blame] | 1718 | u.val64 = *array; |
| 1719 | if (swapped) { |
| 1720 | /* undo swap of u64, then swap on individual u32s */ |
| 1721 | u.val64 = bswap_64(u.val64); |
| 1722 | u.val32[0] = bswap_32(u.val32[0]); |
| 1723 | } |
| 1724 | |
| 1725 | sample->cpu = u.val32[0]; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1726 | array--; |
| 1727 | } |
| 1728 | |
| 1729 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 1730 | sample->stream_id = *array; |
| 1731 | array--; |
| 1732 | } |
| 1733 | |
| 1734 | if (type & PERF_SAMPLE_ID) { |
| 1735 | sample->id = *array; |
| 1736 | array--; |
| 1737 | } |
| 1738 | |
| 1739 | if (type & PERF_SAMPLE_TIME) { |
| 1740 | sample->time = *array; |
| 1741 | array--; |
| 1742 | } |
| 1743 | |
| 1744 | if (type & PERF_SAMPLE_TID) { |
Jiri Olsa | 37073f9 | 2012-05-30 14:23:44 +0200 | [diff] [blame] | 1745 | u.val64 = *array; |
| 1746 | if (swapped) { |
| 1747 | /* undo swap of u64, then swap on individual u32s */ |
| 1748 | u.val64 = bswap_64(u.val64); |
| 1749 | u.val32[0] = bswap_32(u.val32[0]); |
| 1750 | u.val32[1] = bswap_32(u.val32[1]); |
| 1751 | } |
| 1752 | |
| 1753 | sample->pid = u.val32[0]; |
| 1754 | sample->tid = u.val32[1]; |
Adrian Hunter | dd44bc6 | 2013-10-18 15:29:01 +0300 | [diff] [blame] | 1755 | array--; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1761 | static inline bool overflow(const void *endp, u16 max_size, const void *offset, |
| 1762 | u64 size) |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 1763 | { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1764 | return size > max_size || offset + size > endp; |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 1765 | } |
| 1766 | |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1767 | #define OVERFLOW_CHECK(offset, size, max_size) \ |
| 1768 | do { \ |
| 1769 | if (overflow(endp, (max_size), (offset), (size))) \ |
| 1770 | return -EFAULT; \ |
| 1771 | } while (0) |
| 1772 | |
| 1773 | #define OVERFLOW_CHECK_u64(offset) \ |
| 1774 | OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64)) |
| 1775 | |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1776 | int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event, |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1777 | struct perf_sample *data) |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1778 | { |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1779 | u64 type = evsel->attr.sample_type; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1780 | bool swapped = evsel->needs_swap; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1781 | const u64 *array; |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1782 | u16 max_size = event->header.size; |
| 1783 | const void *endp = (void *)event + max_size; |
| 1784 | u64 sz; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1785 | |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 1786 | /* |
| 1787 | * used for cross-endian analysis. See git commit 65014ab3 |
| 1788 | * for why this goofiness is needed. |
| 1789 | */ |
Jiri Olsa | 6a11f92 | 2012-05-16 08:59:04 +0200 | [diff] [blame] | 1790 | union u64_swap u; |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 1791 | |
Robert Richter | f3bda2c | 2011-12-15 17:32:39 +0100 | [diff] [blame] | 1792 | memset(data, 0, sizeof(*data)); |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1793 | data->cpu = data->pid = data->tid = -1; |
| 1794 | data->stream_id = data->id = data->time = -1ULL; |
Jiri Olsa | bc52908 | 2014-02-03 12:44:41 +0100 | [diff] [blame] | 1795 | data->period = evsel->attr.sample_period; |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1796 | data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1797 | |
| 1798 | if (event->header.type != PERF_RECORD_SAMPLE) { |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1799 | if (!evsel->attr.sample_id_all) |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1800 | return 0; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1801 | return perf_evsel__parse_id_sample(evsel, event, data); |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | array = event->sample.array; |
| 1805 | |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1806 | /* |
| 1807 | * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes |
| 1808 | * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to |
| 1809 | * check the format does not go past the end of the event. |
| 1810 | */ |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 1811 | if (evsel->sample_size + sizeof(event->header) > event->header.size) |
Frederic Weisbecker | a285412 | 2011-05-21 19:33:04 +0200 | [diff] [blame] | 1812 | return -EFAULT; |
| 1813 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 1814 | data->id = -1ULL; |
| 1815 | if (type & PERF_SAMPLE_IDENTIFIER) { |
| 1816 | data->id = *array; |
| 1817 | array++; |
| 1818 | } |
| 1819 | |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1820 | if (type & PERF_SAMPLE_IP) { |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 1821 | data->ip = *array; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1822 | array++; |
| 1823 | } |
| 1824 | |
| 1825 | if (type & PERF_SAMPLE_TID) { |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 1826 | u.val64 = *array; |
| 1827 | if (swapped) { |
| 1828 | /* undo swap of u64, then swap on individual u32s */ |
| 1829 | u.val64 = bswap_64(u.val64); |
| 1830 | u.val32[0] = bswap_32(u.val32[0]); |
| 1831 | u.val32[1] = bswap_32(u.val32[1]); |
| 1832 | } |
| 1833 | |
| 1834 | data->pid = u.val32[0]; |
| 1835 | data->tid = u.val32[1]; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1836 | array++; |
| 1837 | } |
| 1838 | |
| 1839 | if (type & PERF_SAMPLE_TIME) { |
| 1840 | data->time = *array; |
| 1841 | array++; |
| 1842 | } |
| 1843 | |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 1844 | data->addr = 0; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1845 | if (type & PERF_SAMPLE_ADDR) { |
| 1846 | data->addr = *array; |
| 1847 | array++; |
| 1848 | } |
| 1849 | |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1850 | if (type & PERF_SAMPLE_ID) { |
| 1851 | data->id = *array; |
| 1852 | array++; |
| 1853 | } |
| 1854 | |
| 1855 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 1856 | data->stream_id = *array; |
| 1857 | array++; |
| 1858 | } |
| 1859 | |
| 1860 | if (type & PERF_SAMPLE_CPU) { |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 1861 | |
| 1862 | u.val64 = *array; |
| 1863 | if (swapped) { |
| 1864 | /* undo swap of u64, then swap on individual u32s */ |
| 1865 | u.val64 = bswap_64(u.val64); |
| 1866 | u.val32[0] = bswap_32(u.val32[0]); |
| 1867 | } |
| 1868 | |
| 1869 | data->cpu = u.val32[0]; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1870 | array++; |
| 1871 | } |
| 1872 | |
| 1873 | if (type & PERF_SAMPLE_PERIOD) { |
| 1874 | data->period = *array; |
| 1875 | array++; |
| 1876 | } |
| 1877 | |
| 1878 | if (type & PERF_SAMPLE_READ) { |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1879 | u64 read_format = evsel->attr.read_format; |
| 1880 | |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1881 | OVERFLOW_CHECK_u64(array); |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1882 | if (read_format & PERF_FORMAT_GROUP) |
| 1883 | data->read.group.nr = *array; |
| 1884 | else |
| 1885 | data->read.one.value = *array; |
| 1886 | |
| 1887 | array++; |
| 1888 | |
| 1889 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1890 | OVERFLOW_CHECK_u64(array); |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1891 | data->read.time_enabled = *array; |
| 1892 | array++; |
| 1893 | } |
| 1894 | |
| 1895 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1896 | OVERFLOW_CHECK_u64(array); |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1897 | data->read.time_running = *array; |
| 1898 | array++; |
| 1899 | } |
| 1900 | |
| 1901 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ |
| 1902 | if (read_format & PERF_FORMAT_GROUP) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1903 | const u64 max_group_nr = UINT64_MAX / |
| 1904 | sizeof(struct sample_read_value); |
| 1905 | |
| 1906 | if (data->read.group.nr > max_group_nr) |
| 1907 | return -EFAULT; |
| 1908 | sz = data->read.group.nr * |
| 1909 | sizeof(struct sample_read_value); |
| 1910 | OVERFLOW_CHECK(array, sz, max_size); |
| 1911 | data->read.group.values = |
| 1912 | (struct sample_read_value *)array; |
| 1913 | array = (void *)array + sz; |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1914 | } else { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1915 | OVERFLOW_CHECK_u64(array); |
Jiri Olsa | 9ede473 | 2012-10-10 17:38:13 +0200 | [diff] [blame] | 1916 | data->read.one.id = *array; |
| 1917 | array++; |
| 1918 | } |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | if (type & PERF_SAMPLE_CALLCHAIN) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1922 | const u64 max_callchain_nr = UINT64_MAX / sizeof(u64); |
| 1923 | |
| 1924 | OVERFLOW_CHECK_u64(array); |
| 1925 | data->callchain = (struct ip_callchain *)array++; |
| 1926 | if (data->callchain->nr > max_callchain_nr) |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 1927 | return -EFAULT; |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1928 | sz = data->callchain->nr * sizeof(u64); |
| 1929 | OVERFLOW_CHECK(array, sz, max_size); |
| 1930 | array = (void *)array + sz; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | if (type & PERF_SAMPLE_RAW) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1934 | OVERFLOW_CHECK_u64(array); |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 1935 | u.val64 = *array; |
| 1936 | if (WARN_ONCE(swapped, |
| 1937 | "Endianness of raw data not corrected!\n")) { |
| 1938 | /* undo swap of u64, then swap on individual u32s */ |
| 1939 | u.val64 = bswap_64(u.val64); |
| 1940 | u.val32[0] = bswap_32(u.val32[0]); |
| 1941 | u.val32[1] = bswap_32(u.val32[1]); |
| 1942 | } |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 1943 | data->raw_size = u.val32[0]; |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1944 | array = (void *)array + sizeof(u32); |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 1945 | |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1946 | OVERFLOW_CHECK(array, data->raw_size, max_size); |
| 1947 | data->raw_data = (void *)array; |
| 1948 | array = (void *)array + data->raw_size; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 1949 | } |
| 1950 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1951 | if (type & PERF_SAMPLE_BRANCH_STACK) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1952 | const u64 max_branch_nr = UINT64_MAX / |
| 1953 | sizeof(struct branch_entry); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1954 | |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1955 | OVERFLOW_CHECK_u64(array); |
| 1956 | data->branch_stack = (struct branch_stack *)array++; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1957 | |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1958 | if (data->branch_stack->nr > max_branch_nr) |
| 1959 | return -EFAULT; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1960 | sz = data->branch_stack->nr * sizeof(struct branch_entry); |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1961 | OVERFLOW_CHECK(array, sz, max_size); |
| 1962 | array = (void *)array + sz; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 1963 | } |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1964 | |
| 1965 | if (type & PERF_SAMPLE_REGS_USER) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1966 | OVERFLOW_CHECK_u64(array); |
Adrian Hunter | 5b95a4a3 | 2013-08-27 11:23:10 +0300 | [diff] [blame] | 1967 | data->user_regs.abi = *array; |
| 1968 | array++; |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1969 | |
Adrian Hunter | 5b95a4a3 | 2013-08-27 11:23:10 +0300 | [diff] [blame] | 1970 | if (data->user_regs.abi) { |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 1971 | u64 mask = evsel->attr.sample_regs_user; |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1972 | |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 1973 | sz = hweight_long(mask) * sizeof(u64); |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1974 | OVERFLOW_CHECK(array, sz, max_size); |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 1975 | data->user_regs.mask = mask; |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1976 | data->user_regs.regs = (u64 *)array; |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1977 | array = (void *)array + sz; |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | if (type & PERF_SAMPLE_STACK_USER) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1982 | OVERFLOW_CHECK_u64(array); |
| 1983 | sz = *array++; |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1984 | |
| 1985 | data->user_stack.offset = ((char *)(array - 1) |
| 1986 | - (char *) event); |
| 1987 | |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1988 | if (!sz) { |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1989 | data->user_stack.size = 0; |
| 1990 | } else { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1991 | OVERFLOW_CHECK(array, sz, max_size); |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1992 | data->user_stack.data = (char *)array; |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 1993 | array = (void *)array + sz; |
| 1994 | OVERFLOW_CHECK_u64(array); |
Adrian Hunter | 54bd269 | 2013-07-04 16:20:34 +0300 | [diff] [blame] | 1995 | data->user_stack.size = *array++; |
Jiri Olsa | a65cb4b | 2013-10-02 15:46:39 +0200 | [diff] [blame] | 1996 | if (WARN_ONCE(data->user_stack.size > sz, |
| 1997 | "user stack dump failure\n")) |
| 1998 | return -EFAULT; |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 2002 | if (type & PERF_SAMPLE_WEIGHT) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 2003 | OVERFLOW_CHECK_u64(array); |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 2004 | data->weight = *array; |
| 2005 | array++; |
| 2006 | } |
| 2007 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 2008 | data->data_src = PERF_MEM_DATA_SRC_NONE; |
| 2009 | if (type & PERF_SAMPLE_DATA_SRC) { |
Adrian Hunter | 03b6ea9 | 2013-08-27 11:23:04 +0300 | [diff] [blame] | 2010 | OVERFLOW_CHECK_u64(array); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 2011 | data->data_src = *array; |
| 2012 | array++; |
| 2013 | } |
| 2014 | |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 2015 | data->transaction = 0; |
| 2016 | if (type & PERF_SAMPLE_TRANSACTION) { |
Adrian Hunter | 87b9552 | 2013-11-01 15:51:36 +0200 | [diff] [blame] | 2017 | OVERFLOW_CHECK_u64(array); |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 2018 | data->transaction = *array; |
| 2019 | array++; |
| 2020 | } |
| 2021 | |
Stephane Eranian | 6a21c0b | 2014-09-24 13:48:39 +0200 | [diff] [blame] | 2022 | data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 2023 | if (type & PERF_SAMPLE_REGS_INTR) { |
| 2024 | OVERFLOW_CHECK_u64(array); |
| 2025 | data->intr_regs.abi = *array; |
| 2026 | array++; |
| 2027 | |
| 2028 | if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) { |
| 2029 | u64 mask = evsel->attr.sample_regs_intr; |
| 2030 | |
| 2031 | sz = hweight_long(mask) * sizeof(u64); |
| 2032 | OVERFLOW_CHECK(array, sz, max_size); |
| 2033 | data->intr_regs.mask = mask; |
| 2034 | data->intr_regs.regs = (u64 *)array; |
| 2035 | array = (void *)array + sz; |
| 2036 | } |
| 2037 | } |
| 2038 | |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 2039 | return 0; |
| 2040 | } |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2041 | |
Adrian Hunter | b1cf6f6 | 2013-08-27 11:23:12 +0300 | [diff] [blame] | 2042 | size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 2043 | u64 read_format) |
Adrian Hunter | b1cf6f6 | 2013-08-27 11:23:12 +0300 | [diff] [blame] | 2044 | { |
| 2045 | size_t sz, result = sizeof(struct sample_event); |
| 2046 | |
| 2047 | if (type & PERF_SAMPLE_IDENTIFIER) |
| 2048 | result += sizeof(u64); |
| 2049 | |
| 2050 | if (type & PERF_SAMPLE_IP) |
| 2051 | result += sizeof(u64); |
| 2052 | |
| 2053 | if (type & PERF_SAMPLE_TID) |
| 2054 | result += sizeof(u64); |
| 2055 | |
| 2056 | if (type & PERF_SAMPLE_TIME) |
| 2057 | result += sizeof(u64); |
| 2058 | |
| 2059 | if (type & PERF_SAMPLE_ADDR) |
| 2060 | result += sizeof(u64); |
| 2061 | |
| 2062 | if (type & PERF_SAMPLE_ID) |
| 2063 | result += sizeof(u64); |
| 2064 | |
| 2065 | if (type & PERF_SAMPLE_STREAM_ID) |
| 2066 | result += sizeof(u64); |
| 2067 | |
| 2068 | if (type & PERF_SAMPLE_CPU) |
| 2069 | result += sizeof(u64); |
| 2070 | |
| 2071 | if (type & PERF_SAMPLE_PERIOD) |
| 2072 | result += sizeof(u64); |
| 2073 | |
| 2074 | if (type & PERF_SAMPLE_READ) { |
| 2075 | result += sizeof(u64); |
| 2076 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 2077 | result += sizeof(u64); |
| 2078 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 2079 | result += sizeof(u64); |
| 2080 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ |
| 2081 | if (read_format & PERF_FORMAT_GROUP) { |
| 2082 | sz = sample->read.group.nr * |
| 2083 | sizeof(struct sample_read_value); |
| 2084 | result += sz; |
| 2085 | } else { |
| 2086 | result += sizeof(u64); |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | if (type & PERF_SAMPLE_CALLCHAIN) { |
| 2091 | sz = (sample->callchain->nr + 1) * sizeof(u64); |
| 2092 | result += sz; |
| 2093 | } |
| 2094 | |
| 2095 | if (type & PERF_SAMPLE_RAW) { |
| 2096 | result += sizeof(u32); |
| 2097 | result += sample->raw_size; |
| 2098 | } |
| 2099 | |
| 2100 | if (type & PERF_SAMPLE_BRANCH_STACK) { |
| 2101 | sz = sample->branch_stack->nr * sizeof(struct branch_entry); |
| 2102 | sz += sizeof(u64); |
| 2103 | result += sz; |
| 2104 | } |
| 2105 | |
| 2106 | if (type & PERF_SAMPLE_REGS_USER) { |
| 2107 | if (sample->user_regs.abi) { |
| 2108 | result += sizeof(u64); |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 2109 | sz = hweight_long(sample->user_regs.mask) * sizeof(u64); |
Adrian Hunter | b1cf6f6 | 2013-08-27 11:23:12 +0300 | [diff] [blame] | 2110 | result += sz; |
| 2111 | } else { |
| 2112 | result += sizeof(u64); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | if (type & PERF_SAMPLE_STACK_USER) { |
| 2117 | sz = sample->user_stack.size; |
| 2118 | result += sizeof(u64); |
| 2119 | if (sz) { |
| 2120 | result += sz; |
| 2121 | result += sizeof(u64); |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | if (type & PERF_SAMPLE_WEIGHT) |
| 2126 | result += sizeof(u64); |
| 2127 | |
| 2128 | if (type & PERF_SAMPLE_DATA_SRC) |
| 2129 | result += sizeof(u64); |
| 2130 | |
Adrian Hunter | 42d8891 | 2013-11-01 15:51:38 +0200 | [diff] [blame] | 2131 | if (type & PERF_SAMPLE_TRANSACTION) |
| 2132 | result += sizeof(u64); |
| 2133 | |
Stephane Eranian | 6a21c0b | 2014-09-24 13:48:39 +0200 | [diff] [blame] | 2134 | if (type & PERF_SAMPLE_REGS_INTR) { |
| 2135 | if (sample->intr_regs.abi) { |
| 2136 | result += sizeof(u64); |
| 2137 | sz = hweight_long(sample->intr_regs.mask) * sizeof(u64); |
| 2138 | result += sz; |
| 2139 | } else { |
| 2140 | result += sizeof(u64); |
| 2141 | } |
| 2142 | } |
| 2143 | |
Adrian Hunter | b1cf6f6 | 2013-08-27 11:23:12 +0300 | [diff] [blame] | 2144 | return result; |
| 2145 | } |
| 2146 | |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2147 | int perf_event__synthesize_sample(union perf_event *event, u64 type, |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 2148 | u64 read_format, |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2149 | const struct perf_sample *sample, |
| 2150 | bool swapped) |
| 2151 | { |
| 2152 | u64 *array; |
Adrian Hunter | d03f217 | 2013-08-27 11:23:11 +0300 | [diff] [blame] | 2153 | size_t sz; |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2154 | /* |
| 2155 | * used for cross-endian analysis. See git commit 65014ab3 |
| 2156 | * for why this goofiness is needed. |
| 2157 | */ |
Jiri Olsa | 6a11f92 | 2012-05-16 08:59:04 +0200 | [diff] [blame] | 2158 | union u64_swap u; |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2159 | |
| 2160 | array = event->sample.array; |
| 2161 | |
Adrian Hunter | 7556257 | 2013-08-27 11:23:09 +0300 | [diff] [blame] | 2162 | if (type & PERF_SAMPLE_IDENTIFIER) { |
| 2163 | *array = sample->id; |
| 2164 | array++; |
| 2165 | } |
| 2166 | |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2167 | if (type & PERF_SAMPLE_IP) { |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 2168 | *array = sample->ip; |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2169 | array++; |
| 2170 | } |
| 2171 | |
| 2172 | if (type & PERF_SAMPLE_TID) { |
| 2173 | u.val32[0] = sample->pid; |
| 2174 | u.val32[1] = sample->tid; |
| 2175 | if (swapped) { |
| 2176 | /* |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 2177 | * Inverse of what is done in perf_evsel__parse_sample |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2178 | */ |
| 2179 | u.val32[0] = bswap_32(u.val32[0]); |
| 2180 | u.val32[1] = bswap_32(u.val32[1]); |
| 2181 | u.val64 = bswap_64(u.val64); |
| 2182 | } |
| 2183 | |
| 2184 | *array = u.val64; |
| 2185 | array++; |
| 2186 | } |
| 2187 | |
| 2188 | if (type & PERF_SAMPLE_TIME) { |
| 2189 | *array = sample->time; |
| 2190 | array++; |
| 2191 | } |
| 2192 | |
| 2193 | if (type & PERF_SAMPLE_ADDR) { |
| 2194 | *array = sample->addr; |
| 2195 | array++; |
| 2196 | } |
| 2197 | |
| 2198 | if (type & PERF_SAMPLE_ID) { |
| 2199 | *array = sample->id; |
| 2200 | array++; |
| 2201 | } |
| 2202 | |
| 2203 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 2204 | *array = sample->stream_id; |
| 2205 | array++; |
| 2206 | } |
| 2207 | |
| 2208 | if (type & PERF_SAMPLE_CPU) { |
| 2209 | u.val32[0] = sample->cpu; |
| 2210 | if (swapped) { |
| 2211 | /* |
Arnaldo Carvalho de Melo | a3f698f | 2012-08-02 12:23:46 -0300 | [diff] [blame] | 2212 | * Inverse of what is done in perf_evsel__parse_sample |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2213 | */ |
| 2214 | u.val32[0] = bswap_32(u.val32[0]); |
| 2215 | u.val64 = bswap_64(u.val64); |
| 2216 | } |
| 2217 | *array = u.val64; |
| 2218 | array++; |
| 2219 | } |
| 2220 | |
| 2221 | if (type & PERF_SAMPLE_PERIOD) { |
| 2222 | *array = sample->period; |
| 2223 | array++; |
| 2224 | } |
| 2225 | |
Adrian Hunter | d03f217 | 2013-08-27 11:23:11 +0300 | [diff] [blame] | 2226 | if (type & PERF_SAMPLE_READ) { |
| 2227 | if (read_format & PERF_FORMAT_GROUP) |
| 2228 | *array = sample->read.group.nr; |
| 2229 | else |
| 2230 | *array = sample->read.one.value; |
| 2231 | array++; |
| 2232 | |
| 2233 | if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { |
| 2234 | *array = sample->read.time_enabled; |
| 2235 | array++; |
| 2236 | } |
| 2237 | |
| 2238 | if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { |
| 2239 | *array = sample->read.time_running; |
| 2240 | array++; |
| 2241 | } |
| 2242 | |
| 2243 | /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ |
| 2244 | if (read_format & PERF_FORMAT_GROUP) { |
| 2245 | sz = sample->read.group.nr * |
| 2246 | sizeof(struct sample_read_value); |
| 2247 | memcpy(array, sample->read.group.values, sz); |
| 2248 | array = (void *)array + sz; |
| 2249 | } else { |
| 2250 | *array = sample->read.one.id; |
| 2251 | array++; |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | if (type & PERF_SAMPLE_CALLCHAIN) { |
| 2256 | sz = (sample->callchain->nr + 1) * sizeof(u64); |
| 2257 | memcpy(array, sample->callchain, sz); |
| 2258 | array = (void *)array + sz; |
| 2259 | } |
| 2260 | |
| 2261 | if (type & PERF_SAMPLE_RAW) { |
| 2262 | u.val32[0] = sample->raw_size; |
| 2263 | if (WARN_ONCE(swapped, |
| 2264 | "Endianness of raw data not corrected!\n")) { |
| 2265 | /* |
| 2266 | * Inverse of what is done in perf_evsel__parse_sample |
| 2267 | */ |
| 2268 | u.val32[0] = bswap_32(u.val32[0]); |
| 2269 | u.val32[1] = bswap_32(u.val32[1]); |
| 2270 | u.val64 = bswap_64(u.val64); |
| 2271 | } |
| 2272 | *array = u.val64; |
| 2273 | array = (void *)array + sizeof(u32); |
| 2274 | |
| 2275 | memcpy(array, sample->raw_data, sample->raw_size); |
| 2276 | array = (void *)array + sample->raw_size; |
| 2277 | } |
| 2278 | |
| 2279 | if (type & PERF_SAMPLE_BRANCH_STACK) { |
| 2280 | sz = sample->branch_stack->nr * sizeof(struct branch_entry); |
| 2281 | sz += sizeof(u64); |
| 2282 | memcpy(array, sample->branch_stack, sz); |
| 2283 | array = (void *)array + sz; |
| 2284 | } |
| 2285 | |
| 2286 | if (type & PERF_SAMPLE_REGS_USER) { |
| 2287 | if (sample->user_regs.abi) { |
| 2288 | *array++ = sample->user_regs.abi; |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 2289 | sz = hweight_long(sample->user_regs.mask) * sizeof(u64); |
Adrian Hunter | d03f217 | 2013-08-27 11:23:11 +0300 | [diff] [blame] | 2290 | memcpy(array, sample->user_regs.regs, sz); |
| 2291 | array = (void *)array + sz; |
| 2292 | } else { |
| 2293 | *array++ = 0; |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | if (type & PERF_SAMPLE_STACK_USER) { |
| 2298 | sz = sample->user_stack.size; |
| 2299 | *array++ = sz; |
| 2300 | if (sz) { |
| 2301 | memcpy(array, sample->user_stack.data, sz); |
| 2302 | array = (void *)array + sz; |
| 2303 | *array++ = sz; |
| 2304 | } |
| 2305 | } |
| 2306 | |
| 2307 | if (type & PERF_SAMPLE_WEIGHT) { |
| 2308 | *array = sample->weight; |
| 2309 | array++; |
| 2310 | } |
| 2311 | |
| 2312 | if (type & PERF_SAMPLE_DATA_SRC) { |
| 2313 | *array = sample->data_src; |
| 2314 | array++; |
| 2315 | } |
| 2316 | |
Adrian Hunter | 42d8891 | 2013-11-01 15:51:38 +0200 | [diff] [blame] | 2317 | if (type & PERF_SAMPLE_TRANSACTION) { |
| 2318 | *array = sample->transaction; |
| 2319 | array++; |
| 2320 | } |
| 2321 | |
Stephane Eranian | 6a21c0b | 2014-09-24 13:48:39 +0200 | [diff] [blame] | 2322 | if (type & PERF_SAMPLE_REGS_INTR) { |
| 2323 | if (sample->intr_regs.abi) { |
| 2324 | *array++ = sample->intr_regs.abi; |
| 2325 | sz = hweight_long(sample->intr_regs.mask) * sizeof(u64); |
| 2326 | memcpy(array, sample->intr_regs.regs, sz); |
| 2327 | array = (void *)array + sz; |
| 2328 | } else { |
| 2329 | *array++ = 0; |
| 2330 | } |
| 2331 | } |
| 2332 | |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 2333 | return 0; |
| 2334 | } |
Arnaldo Carvalho de Melo | 5555ded | 2012-09-11 19:24:23 -0300 | [diff] [blame] | 2335 | |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 2336 | struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name) |
| 2337 | { |
| 2338 | return pevent_find_field(evsel->tp_format, name); |
| 2339 | } |
| 2340 | |
Arnaldo Carvalho de Melo | 5d2074e | 2012-09-26 20:22:00 -0300 | [diff] [blame] | 2341 | void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 5555ded | 2012-09-11 19:24:23 -0300 | [diff] [blame] | 2342 | const char *name) |
| 2343 | { |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 2344 | struct format_field *field = perf_evsel__field(evsel, name); |
Arnaldo Carvalho de Melo | 5555ded | 2012-09-11 19:24:23 -0300 | [diff] [blame] | 2345 | int offset; |
| 2346 | |
Arnaldo Carvalho de Melo | efd2b92 | 2012-09-18 11:21:50 -0300 | [diff] [blame] | 2347 | if (!field) |
| 2348 | return NULL; |
Arnaldo Carvalho de Melo | 5555ded | 2012-09-11 19:24:23 -0300 | [diff] [blame] | 2349 | |
| 2350 | offset = field->offset; |
| 2351 | |
| 2352 | if (field->flags & FIELD_IS_DYNAMIC) { |
| 2353 | offset = *(int *)(sample->raw_data + field->offset); |
| 2354 | offset &= 0xffff; |
| 2355 | } |
| 2356 | |
| 2357 | return sample->raw_data + offset; |
| 2358 | } |
| 2359 | |
Arnaldo Carvalho de Melo | 9052517 | 2016-05-31 12:47:46 -0300 | [diff] [blame] | 2360 | u64 format_field__intval(struct format_field *field, struct perf_sample *sample, |
| 2361 | bool needs_swap) |
Arnaldo Carvalho de Melo | 5555ded | 2012-09-11 19:24:23 -0300 | [diff] [blame] | 2362 | { |
Arnaldo Carvalho de Melo | e6b6f67 | 2012-09-26 13:13:04 -0300 | [diff] [blame] | 2363 | u64 value; |
Arnaldo Carvalho de Melo | 9052517 | 2016-05-31 12:47:46 -0300 | [diff] [blame] | 2364 | void *ptr = sample->raw_data + field->offset; |
Arnaldo Carvalho de Melo | 5555ded | 2012-09-11 19:24:23 -0300 | [diff] [blame] | 2365 | |
Arnaldo Carvalho de Melo | e6b6f67 | 2012-09-26 13:13:04 -0300 | [diff] [blame] | 2366 | switch (field->size) { |
| 2367 | case 1: |
| 2368 | return *(u8 *)ptr; |
| 2369 | case 2: |
| 2370 | value = *(u16 *)ptr; |
| 2371 | break; |
| 2372 | case 4: |
| 2373 | value = *(u32 *)ptr; |
| 2374 | break; |
| 2375 | case 8: |
David Ahern | e94eeda | 2015-03-24 16:14:09 -0400 | [diff] [blame] | 2376 | memcpy(&value, ptr, sizeof(u64)); |
Arnaldo Carvalho de Melo | e6b6f67 | 2012-09-26 13:13:04 -0300 | [diff] [blame] | 2377 | break; |
| 2378 | default: |
| 2379 | return 0; |
| 2380 | } |
| 2381 | |
Arnaldo Carvalho de Melo | 9052517 | 2016-05-31 12:47:46 -0300 | [diff] [blame] | 2382 | if (!needs_swap) |
Arnaldo Carvalho de Melo | e6b6f67 | 2012-09-26 13:13:04 -0300 | [diff] [blame] | 2383 | return value; |
| 2384 | |
| 2385 | switch (field->size) { |
| 2386 | case 2: |
| 2387 | return bswap_16(value); |
| 2388 | case 4: |
| 2389 | return bswap_32(value); |
| 2390 | case 8: |
| 2391 | return bswap_64(value); |
| 2392 | default: |
| 2393 | return 0; |
| 2394 | } |
| 2395 | |
| 2396 | return 0; |
Arnaldo Carvalho de Melo | 5555ded | 2012-09-11 19:24:23 -0300 | [diff] [blame] | 2397 | } |
Arnaldo Carvalho de Melo | 0698aed | 2012-12-10 18:17:08 -0300 | [diff] [blame] | 2398 | |
Arnaldo Carvalho de Melo | 9052517 | 2016-05-31 12:47:46 -0300 | [diff] [blame] | 2399 | u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample, |
| 2400 | const char *name) |
| 2401 | { |
| 2402 | struct format_field *field = perf_evsel__field(evsel, name); |
| 2403 | |
| 2404 | if (!field) |
| 2405 | return 0; |
| 2406 | |
| 2407 | return field ? format_field__intval(field, sample, evsel->needs_swap) : 0; |
| 2408 | } |
| 2409 | |
Arnaldo Carvalho de Melo | c0a5434 | 2012-12-13 14:16:30 -0300 | [diff] [blame] | 2410 | bool perf_evsel__fallback(struct perf_evsel *evsel, int err, |
| 2411 | char *msg, size_t msgsize) |
| 2412 | { |
Arnaldo Carvalho de Melo | 0809482 | 2016-05-12 16:07:47 -0300 | [diff] [blame] | 2413 | int paranoid; |
| 2414 | |
David Ahern | 2b821cc | 2013-07-18 17:27:59 -0600 | [diff] [blame] | 2415 | if ((err == ENOENT || err == ENXIO || err == ENODEV) && |
Arnaldo Carvalho de Melo | c0a5434 | 2012-12-13 14:16:30 -0300 | [diff] [blame] | 2416 | evsel->attr.type == PERF_TYPE_HARDWARE && |
| 2417 | evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) { |
| 2418 | /* |
| 2419 | * If it's cycles then fall back to hrtimer based |
| 2420 | * cpu-clock-tick sw counter, which is always available even if |
| 2421 | * no PMU support. |
| 2422 | * |
| 2423 | * PPC returns ENXIO until 2.6.37 (behavior changed with commit |
| 2424 | * b0a873e). |
| 2425 | */ |
| 2426 | scnprintf(msg, msgsize, "%s", |
| 2427 | "The cycles event is not supported, trying to fall back to cpu-clock-ticks"); |
| 2428 | |
| 2429 | evsel->attr.type = PERF_TYPE_SOFTWARE; |
| 2430 | evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK; |
| 2431 | |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 2432 | zfree(&evsel->name); |
Arnaldo Carvalho de Melo | c0a5434 | 2012-12-13 14:16:30 -0300 | [diff] [blame] | 2433 | return true; |
Arnaldo Carvalho de Melo | 0809482 | 2016-05-12 16:07:47 -0300 | [diff] [blame] | 2434 | } else if (err == EACCES && !evsel->attr.exclude_kernel && |
| 2435 | (paranoid = perf_event_paranoid()) > 1) { |
| 2436 | const char *name = perf_evsel__name(evsel); |
| 2437 | char *new_name; |
| 2438 | |
| 2439 | if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0) |
| 2440 | return false; |
| 2441 | |
| 2442 | if (evsel->name) |
| 2443 | free(evsel->name); |
| 2444 | evsel->name = new_name; |
| 2445 | scnprintf(msg, msgsize, |
| 2446 | "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid); |
| 2447 | evsel->attr.exclude_kernel = 1; |
| 2448 | |
| 2449 | return true; |
Arnaldo Carvalho de Melo | c0a5434 | 2012-12-13 14:16:30 -0300 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | return false; |
| 2453 | } |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2454 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2455 | int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target, |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2456 | int err, char *msg, size_t size) |
| 2457 | { |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 2458 | char sbuf[STRERR_BUFSIZE]; |
| 2459 | |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2460 | switch (err) { |
| 2461 | case EPERM: |
| 2462 | case EACCES: |
David Ahern | b69e63a | 2013-05-25 17:54:00 -0600 | [diff] [blame] | 2463 | return scnprintf(msg, size, |
Ben Hutchings | 3379e0c | 2016-01-19 21:35:15 +0000 | [diff] [blame] | 2464 | "You may not have permission to collect %sstats.\n\n" |
| 2465 | "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n" |
| 2466 | "which controls use of the performance events system by\n" |
| 2467 | "unprivileged users (without CAP_SYS_ADMIN).\n\n" |
Arnaldo Carvalho de Melo | 7d17391 | 2016-05-12 15:44:55 -0300 | [diff] [blame] | 2468 | "The current value is %d:\n\n" |
Ben Hutchings | 3379e0c | 2016-01-19 21:35:15 +0000 | [diff] [blame] | 2469 | " -1: Allow use of (almost) all events by all users\n" |
| 2470 | ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n" |
| 2471 | ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n" |
Arnaldo Carvalho de Melo | d6195a6 | 2017-02-13 16:45:24 -0300 | [diff] [blame] | 2472 | ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n" |
| 2473 | "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n" |
| 2474 | " kernel.perf_event_paranoid = -1\n" , |
Arnaldo Carvalho de Melo | 7d17391 | 2016-05-12 15:44:55 -0300 | [diff] [blame] | 2475 | target->system_wide ? "system-wide " : "", |
| 2476 | perf_event_paranoid()); |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2477 | case ENOENT: |
| 2478 | return scnprintf(msg, size, "The %s event is not supported.", |
| 2479 | perf_evsel__name(evsel)); |
| 2480 | case EMFILE: |
| 2481 | return scnprintf(msg, size, "%s", |
| 2482 | "Too many events are opened.\n" |
Jiri Olsa | 18ffdfe | 2015-05-25 22:51:54 +0200 | [diff] [blame] | 2483 | "Probably the maximum number of open file descriptors has been reached.\n" |
| 2484 | "Hint: Try again after reducing the number of events.\n" |
| 2485 | "Hint: Try increasing the limit with 'ulimit -n <limit>'"); |
Arnaldo Carvalho de Melo | de46d52 | 2016-04-27 17:51:45 -0300 | [diff] [blame] | 2486 | case ENOMEM: |
| 2487 | if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 && |
| 2488 | access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0) |
| 2489 | return scnprintf(msg, size, |
| 2490 | "Not enough memory to setup event with callchain.\n" |
| 2491 | "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n" |
| 2492 | "Hint: Current value: %d", sysctl_perf_event_max_stack); |
| 2493 | break; |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2494 | case ENODEV: |
| 2495 | if (target->cpu_list) |
| 2496 | return scnprintf(msg, size, "%s", |
Arnaldo Carvalho de Melo | 81d64f4 | 2016-04-27 17:56:53 -0300 | [diff] [blame] | 2497 | "No such device - did you specify an out-of-range profile CPU?"); |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2498 | break; |
| 2499 | case EOPNOTSUPP: |
Vineet Gupta | dc89e75 | 2016-05-09 15:07:39 +0530 | [diff] [blame] | 2500 | if (evsel->attr.sample_period != 0) |
| 2501 | return scnprintf(msg, size, "%s", |
| 2502 | "PMU Hardware doesn't support sampling/overflow-interrupts."); |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2503 | if (evsel->attr.precise_ip) |
| 2504 | return scnprintf(msg, size, "%s", |
| 2505 | "\'precise\' request may not be supported. Try removing 'p' modifier."); |
| 2506 | #if defined(__i386__) || defined(__x86_64__) |
| 2507 | if (evsel->attr.type == PERF_TYPE_HARDWARE) |
| 2508 | return scnprintf(msg, size, "%s", |
| 2509 | "No hardware sampling interrupt available.\n" |
| 2510 | "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it."); |
| 2511 | #endif |
| 2512 | break; |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 2513 | case EBUSY: |
| 2514 | if (find_process("oprofiled")) |
| 2515 | return scnprintf(msg, size, |
| 2516 | "The PMU counters are busy/taken by another profiler.\n" |
| 2517 | "We found oprofile daemon running, please stop it and try again."); |
| 2518 | break; |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 2519 | case EINVAL: |
Arnaldo Carvalho de Melo | 32a951b | 2016-07-14 08:34:33 +0000 | [diff] [blame] | 2520 | if (evsel->attr.write_backward && perf_missing_features.write_backward) |
Arnaldo Carvalho de Melo | 7da36e9 | 2016-06-20 10:47:18 +0000 | [diff] [blame] | 2521 | return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel."); |
Peter Zijlstra | 814c8c3 | 2015-03-31 00:19:31 +0200 | [diff] [blame] | 2522 | if (perf_missing_features.clockid) |
| 2523 | return scnprintf(msg, size, "clockid feature not supported."); |
| 2524 | if (perf_missing_features.clockid_wrong) |
| 2525 | return scnprintf(msg, size, "wrong clockid (%d).", clockid); |
| 2526 | break; |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2527 | default: |
| 2528 | break; |
| 2529 | } |
| 2530 | |
| 2531 | return scnprintf(msg, size, |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 2532 | "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n" |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2533 | "/bin/dmesg may provide additional information.\n" |
Arnaldo Carvalho de Melo | 81d64f4 | 2016-04-27 17:56:53 -0300 | [diff] [blame] | 2534 | "No CONFIG_PERF_EVENTS=y kernel support configured?", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 2535 | err, str_error_r(err, sbuf, sizeof(sbuf)), |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 2536 | perf_evsel__name(evsel)); |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 2537 | } |
Ravi Bangoria | f4e47f9 | 2016-06-30 11:44:19 +0530 | [diff] [blame] | 2538 | |
| 2539 | char *perf_evsel__env_arch(struct perf_evsel *evsel) |
| 2540 | { |
| 2541 | if (evsel && evsel->evlist && evsel->evlist->env) |
| 2542 | return evsel->evlist->env->arch; |
| 2543 | return NULL; |
| 2544 | } |