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