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