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