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