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