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