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> |
| 11 | #include "asm/bug.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 12 | #include "evsel.h" |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 13 | #include "evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 14 | #include "util.h" |
Arnaldo Carvalho de Melo | 86bd5e8 | 2011-01-03 23:09:46 -0200 | [diff] [blame] | 15 | #include "cpumap.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 16 | #include "thread_map.h" |
Namhyung Kim | 12864b3 | 2012-04-26 14:15:22 +0900 | [diff] [blame] | 17 | #include "target.h" |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 18 | #include "../../include/linux/perf_event.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 19 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 20 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 21 | #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0)) |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 23 | int __perf_evsel__sample_size(u64 sample_type) |
| 24 | { |
| 25 | u64 mask = sample_type & PERF_SAMPLE_MASK; |
| 26 | int size = 0; |
| 27 | int i; |
| 28 | |
| 29 | for (i = 0; i < 64; i++) { |
| 30 | if (mask & (1ULL << i)) |
| 31 | size++; |
| 32 | } |
| 33 | |
| 34 | size *= sizeof(u64); |
| 35 | |
| 36 | return size; |
| 37 | } |
| 38 | |
Jiri Olsa | 4bf9ce1 | 2012-03-22 14:37:26 +0100 | [diff] [blame] | 39 | void hists__init(struct hists *hists) |
Arnaldo Carvalho de Melo | 0e2a5f1 | 2011-11-04 08:16:58 -0200 | [diff] [blame] | 40 | { |
| 41 | memset(hists, 0, sizeof(*hists)); |
| 42 | hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT; |
| 43 | hists->entries_in = &hists->entries_in_array[0]; |
| 44 | hists->entries_collapsed = RB_ROOT; |
| 45 | hists->entries = RB_ROOT; |
| 46 | pthread_mutex_init(&hists->lock, NULL); |
| 47 | } |
| 48 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 49 | void perf_evsel__init(struct perf_evsel *evsel, |
| 50 | struct perf_event_attr *attr, int idx) |
| 51 | { |
| 52 | evsel->idx = idx; |
| 53 | evsel->attr = *attr; |
| 54 | INIT_LIST_HEAD(&evsel->node); |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 55 | hists__init(&evsel->hists); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 56 | } |
| 57 | |
Lin Ming | 23a2f3a | 2011-01-07 11:11:09 +0800 | [diff] [blame] | 58 | struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx) |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 59 | { |
| 60 | struct perf_evsel *evsel = zalloc(sizeof(*evsel)); |
| 61 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 62 | if (evsel != NULL) |
| 63 | perf_evsel__init(evsel, attr, idx); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 64 | |
| 65 | return evsel; |
| 66 | } |
| 67 | |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 68 | static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = { |
| 69 | "cycles", |
| 70 | "instructions", |
| 71 | "cache-references", |
| 72 | "cache-misses", |
| 73 | "branches", |
| 74 | "branch-misses", |
| 75 | "bus-cycles", |
| 76 | "stalled-cycles-frontend", |
| 77 | "stalled-cycles-backend", |
| 78 | "ref-cycles", |
| 79 | }; |
| 80 | |
| 81 | const char *__perf_evsel__hw_name(u64 config) |
| 82 | { |
| 83 | if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config]) |
| 84 | return perf_evsel__hw_names[config]; |
| 85 | |
| 86 | return "unknown-hardware"; |
| 87 | } |
| 88 | |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 89 | 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] | 90 | { |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 91 | int colon = 0, r = 0; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 92 | struct perf_event_attr *attr = &evsel->attr; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 93 | bool exclude_guest_default = false; |
| 94 | |
| 95 | #define MOD_PRINT(context, mod) do { \ |
| 96 | if (!attr->exclude_##context) { \ |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 97 | if (!colon) colon = ++r; \ |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 98 | r += scnprintf(bf + r, size - r, "%c", mod); \ |
| 99 | } } while(0) |
| 100 | |
| 101 | if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) { |
| 102 | MOD_PRINT(kernel, 'k'); |
| 103 | MOD_PRINT(user, 'u'); |
| 104 | MOD_PRINT(hv, 'h'); |
| 105 | exclude_guest_default = true; |
| 106 | } |
| 107 | |
| 108 | if (attr->precise_ip) { |
| 109 | if (!colon) |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 110 | colon = ++r; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 111 | r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp"); |
| 112 | exclude_guest_default = true; |
| 113 | } |
| 114 | |
| 115 | if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) { |
| 116 | MOD_PRINT(host, 'H'); |
| 117 | MOD_PRINT(guest, 'G'); |
| 118 | } |
| 119 | #undef MOD_PRINT |
| 120 | if (colon) |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 121 | bf[colon - 1] = ':'; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 122 | return r; |
| 123 | } |
| 124 | |
Arnaldo Carvalho de Melo | 27f1861 | 2012-06-11 13:33:09 -0300 | [diff] [blame] | 125 | static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 126 | { |
| 127 | int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config)); |
| 128 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); |
| 129 | } |
| 130 | |
Arnaldo Carvalho de Melo | 335c2f5 | 2012-06-11 14:36:20 -0300 | [diff] [blame] | 131 | static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = { |
| 132 | "cpu-clock", |
| 133 | "task-clock", |
| 134 | "page-faults", |
| 135 | "context-switches", |
| 136 | "CPU-migrations", |
| 137 | "minor-faults", |
| 138 | "major-faults", |
| 139 | "alignment-faults", |
| 140 | "emulation-faults", |
| 141 | }; |
| 142 | |
| 143 | const char *__perf_evsel__sw_name(u64 config) |
| 144 | { |
| 145 | if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config]) |
| 146 | return perf_evsel__sw_names[config]; |
| 147 | return "unknown-software"; |
| 148 | } |
| 149 | |
| 150 | static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 151 | { |
| 152 | int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config)); |
| 153 | return r + perf_evsel__add_modifiers(evsel, bf + r, size - r); |
| 154 | } |
| 155 | |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 156 | const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX] |
| 157 | [PERF_EVSEL__MAX_ALIASES] = { |
| 158 | { "L1-dcache", "l1-d", "l1d", "L1-data", }, |
| 159 | { "L1-icache", "l1-i", "l1i", "L1-instruction", }, |
| 160 | { "LLC", "L2", }, |
| 161 | { "dTLB", "d-tlb", "Data-TLB", }, |
| 162 | { "iTLB", "i-tlb", "Instruction-TLB", }, |
| 163 | { "branch", "branches", "bpu", "btb", "bpc", }, |
| 164 | { "node", }, |
| 165 | }; |
| 166 | |
| 167 | const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX] |
| 168 | [PERF_EVSEL__MAX_ALIASES] = { |
| 169 | { "load", "loads", "read", }, |
| 170 | { "store", "stores", "write", }, |
| 171 | { "prefetch", "prefetches", "speculative-read", "speculative-load", }, |
| 172 | }; |
| 173 | |
| 174 | const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX] |
| 175 | [PERF_EVSEL__MAX_ALIASES] = { |
| 176 | { "refs", "Reference", "ops", "access", }, |
| 177 | { "misses", "miss", }, |
| 178 | }; |
| 179 | |
| 180 | #define C(x) PERF_COUNT_HW_CACHE_##x |
| 181 | #define CACHE_READ (1 << C(OP_READ)) |
| 182 | #define CACHE_WRITE (1 << C(OP_WRITE)) |
| 183 | #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) |
| 184 | #define COP(x) (1 << x) |
| 185 | |
| 186 | /* |
| 187 | * cache operartion stat |
| 188 | * L1I : Read and prefetch only |
| 189 | * ITLB and BPU : Read-only |
| 190 | */ |
| 191 | static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = { |
| 192 | [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 193 | [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), |
| 194 | [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 195 | [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 196 | [C(ITLB)] = (CACHE_READ), |
| 197 | [C(BPU)] = (CACHE_READ), |
| 198 | [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), |
| 199 | }; |
| 200 | |
| 201 | bool perf_evsel__is_cache_op_valid(u8 type, u8 op) |
| 202 | { |
| 203 | if (perf_evsel__hw_cache_stat[type] & COP(op)) |
| 204 | return true; /* valid */ |
| 205 | else |
| 206 | return false; /* invalid */ |
| 207 | } |
| 208 | |
| 209 | int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, |
| 210 | char *bf, size_t size) |
| 211 | { |
| 212 | if (result) { |
| 213 | return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0], |
| 214 | perf_evsel__hw_cache_op[op][0], |
| 215 | perf_evsel__hw_cache_result[result][0]); |
| 216 | } |
| 217 | |
| 218 | return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0], |
| 219 | perf_evsel__hw_cache_op[op][1]); |
| 220 | } |
| 221 | |
| 222 | int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size) |
| 223 | { |
| 224 | u8 op, result, type = (config >> 0) & 0xff; |
| 225 | const char *err = "unknown-ext-hardware-cache-type"; |
| 226 | |
| 227 | if (type > PERF_COUNT_HW_CACHE_MAX) |
| 228 | goto out_err; |
| 229 | |
| 230 | op = (config >> 8) & 0xff; |
| 231 | err = "unknown-ext-hardware-cache-op"; |
| 232 | if (op > PERF_COUNT_HW_CACHE_OP_MAX) |
| 233 | goto out_err; |
| 234 | |
| 235 | result = (config >> 16) & 0xff; |
| 236 | err = "unknown-ext-hardware-cache-result"; |
| 237 | if (result > PERF_COUNT_HW_CACHE_RESULT_MAX) |
| 238 | goto out_err; |
| 239 | |
| 240 | err = "invalid-cache"; |
| 241 | if (!perf_evsel__is_cache_op_valid(type, op)) |
| 242 | goto out_err; |
| 243 | |
| 244 | return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size); |
| 245 | out_err: |
| 246 | return scnprintf(bf, size, "%s", err); |
| 247 | } |
| 248 | |
| 249 | static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 250 | { |
| 251 | int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size); |
| 252 | return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret); |
| 253 | } |
| 254 | |
Arnaldo Carvalho de Melo | a446083 | 2012-06-12 10:29:12 -0300 | [diff] [blame^] | 255 | static int perf_evsel__tracepoint_name(struct perf_evsel *evsel, char *bf, size_t size) |
| 256 | { |
| 257 | return scnprintf(bf, size, "%s", evsel->name ?: "unknown tracepoint"); |
| 258 | } |
| 259 | |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 260 | int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size) |
| 261 | { |
| 262 | int ret; |
| 263 | |
| 264 | switch (evsel->attr.type) { |
| 265 | case PERF_TYPE_RAW: |
| 266 | ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config); |
| 267 | break; |
| 268 | |
| 269 | case PERF_TYPE_HARDWARE: |
| 270 | ret = perf_evsel__hw_name(evsel, bf, size); |
| 271 | break; |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 272 | |
| 273 | case PERF_TYPE_HW_CACHE: |
| 274 | ret = perf_evsel__hw_cache_name(evsel, bf, size); |
| 275 | break; |
| 276 | |
Arnaldo Carvalho de Melo | 335c2f5 | 2012-06-11 14:36:20 -0300 | [diff] [blame] | 277 | case PERF_TYPE_SOFTWARE: |
| 278 | ret = perf_evsel__sw_name(evsel, bf, size); |
| 279 | break; |
| 280 | |
Arnaldo Carvalho de Melo | a446083 | 2012-06-12 10:29:12 -0300 | [diff] [blame^] | 281 | case PERF_TYPE_TRACEPOINT: |
| 282 | ret = perf_evsel__tracepoint_name(evsel, bf, size); |
| 283 | break; |
| 284 | |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 285 | default: |
Arnaldo Carvalho de Melo | a446083 | 2012-06-12 10:29:12 -0300 | [diff] [blame^] | 286 | ret = scnprintf(bf, size, "%s", "unknown attr type"); |
| 287 | break; |
Arnaldo Carvalho de Melo | c410431 | 2012-05-25 16:38:11 -0300 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | |
Namhyung Kim | 5090c6a | 2012-03-16 17:42:20 +0900 | [diff] [blame] | 293 | void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts, |
| 294 | struct perf_evsel *first) |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 295 | { |
| 296 | struct perf_event_attr *attr = &evsel->attr; |
| 297 | int track = !evsel->idx; /* only the first counter needs these */ |
| 298 | |
David Ahern | 5e1c81d | 2012-05-13 22:01:28 -0600 | [diff] [blame] | 299 | attr->disabled = 1; |
Arnaldo Carvalho de Melo | 808e122 | 2012-02-14 14:18:57 -0200 | [diff] [blame] | 300 | attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1; |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 301 | attr->inherit = !opts->no_inherit; |
| 302 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
| 303 | PERF_FORMAT_TOTAL_TIME_RUNNING | |
| 304 | PERF_FORMAT_ID; |
| 305 | |
| 306 | attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
| 307 | |
| 308 | /* |
| 309 | * We default some events to a 1 default interval. But keep |
| 310 | * it a weak assumption overridable by the user. |
| 311 | */ |
| 312 | if (!attr->sample_period || (opts->user_freq != UINT_MAX && |
| 313 | opts->user_interval != ULLONG_MAX)) { |
| 314 | if (opts->freq) { |
| 315 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
| 316 | attr->freq = 1; |
| 317 | attr->sample_freq = opts->freq; |
| 318 | } else { |
| 319 | attr->sample_period = opts->default_interval; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (opts->no_samples) |
| 324 | attr->sample_freq = 0; |
| 325 | |
| 326 | if (opts->inherit_stat) |
| 327 | attr->inherit_stat = 1; |
| 328 | |
| 329 | if (opts->sample_address) { |
| 330 | attr->sample_type |= PERF_SAMPLE_ADDR; |
| 331 | attr->mmap_data = track; |
| 332 | } |
| 333 | |
| 334 | if (opts->call_graph) |
| 335 | attr->sample_type |= PERF_SAMPLE_CALLCHAIN; |
| 336 | |
Namhyung Kim | e40ee74 | 2012-05-21 10:42:07 +0900 | [diff] [blame] | 337 | if (perf_target__has_cpu(&opts->target)) |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 338 | attr->sample_type |= PERF_SAMPLE_CPU; |
| 339 | |
Andrew Vagin | 3e76ac7 | 2011-12-20 17:32:45 +0300 | [diff] [blame] | 340 | if (opts->period) |
| 341 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
| 342 | |
Arnaldo Carvalho de Melo | 808e122 | 2012-02-14 14:18:57 -0200 | [diff] [blame] | 343 | if (!opts->sample_id_all_missing && |
Namhyung Kim | d67356e | 2012-05-07 14:09:03 +0900 | [diff] [blame] | 344 | (opts->sample_time || !opts->no_inherit || |
Namhyung Kim | aa22dd4 | 2012-05-16 18:45:47 +0900 | [diff] [blame] | 345 | perf_target__has_cpu(&opts->target))) |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 346 | attr->sample_type |= PERF_SAMPLE_TIME; |
| 347 | |
| 348 | if (opts->raw_samples) { |
| 349 | attr->sample_type |= PERF_SAMPLE_TIME; |
| 350 | attr->sample_type |= PERF_SAMPLE_RAW; |
| 351 | attr->sample_type |= PERF_SAMPLE_CPU; |
| 352 | } |
| 353 | |
| 354 | if (opts->no_delay) { |
| 355 | attr->watermark = 0; |
| 356 | attr->wakeup_events = 1; |
| 357 | } |
Roberto Agostino Vitillo | bdfebd8 | 2012-02-09 23:21:02 +0100 | [diff] [blame] | 358 | if (opts->branch_stack) { |
| 359 | attr->sample_type |= PERF_SAMPLE_BRANCH_STACK; |
| 360 | attr->branch_sample_type = opts->branch_stack; |
| 361 | } |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 362 | |
| 363 | attr->mmap = track; |
| 364 | attr->comm = track; |
| 365 | |
Namhyung Kim | d67356e | 2012-05-07 14:09:03 +0900 | [diff] [blame] | 366 | if (perf_target__none(&opts->target) && |
| 367 | (!opts->group || evsel == first)) { |
Arnaldo Carvalho de Melo | 0f82ebc | 2011-11-08 14:41:57 -0200 | [diff] [blame] | 368 | attr->enable_on_exec = 1; |
| 369 | } |
| 370 | } |
| 371 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 372 | int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 373 | { |
David Ahern | 4af4c95 | 2011-05-27 09:58:34 -0600 | [diff] [blame] | 374 | int cpu, thread; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 375 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); |
David Ahern | 4af4c95 | 2011-05-27 09:58:34 -0600 | [diff] [blame] | 376 | |
| 377 | if (evsel->fd) { |
| 378 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 379 | for (thread = 0; thread < nthreads; thread++) { |
| 380 | FD(evsel, cpu, thread) = -1; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 385 | return evsel->fd != NULL ? 0 : -ENOMEM; |
| 386 | } |
| 387 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 388 | int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 389 | { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 390 | evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); |
| 391 | if (evsel->sample_id == NULL) |
| 392 | return -ENOMEM; |
| 393 | |
| 394 | evsel->id = zalloc(ncpus * nthreads * sizeof(u64)); |
| 395 | if (evsel->id == NULL) { |
| 396 | xyarray__delete(evsel->sample_id); |
| 397 | evsel->sample_id = NULL; |
| 398 | return -ENOMEM; |
| 399 | } |
| 400 | |
| 401 | return 0; |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 402 | } |
| 403 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 404 | int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus) |
| 405 | { |
| 406 | evsel->counts = zalloc((sizeof(*evsel->counts) + |
| 407 | (ncpus * sizeof(struct perf_counts_values)))); |
| 408 | return evsel->counts != NULL ? 0 : -ENOMEM; |
| 409 | } |
| 410 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 411 | void perf_evsel__free_fd(struct perf_evsel *evsel) |
| 412 | { |
| 413 | xyarray__delete(evsel->fd); |
| 414 | evsel->fd = NULL; |
| 415 | } |
| 416 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 417 | void perf_evsel__free_id(struct perf_evsel *evsel) |
| 418 | { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 419 | xyarray__delete(evsel->sample_id); |
| 420 | evsel->sample_id = NULL; |
| 421 | free(evsel->id); |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 422 | evsel->id = NULL; |
| 423 | } |
| 424 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 425 | void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 426 | { |
| 427 | int cpu, thread; |
| 428 | |
| 429 | for (cpu = 0; cpu < ncpus; cpu++) |
| 430 | for (thread = 0; thread < nthreads; ++thread) { |
| 431 | close(FD(evsel, cpu, thread)); |
| 432 | FD(evsel, cpu, thread) = -1; |
| 433 | } |
| 434 | } |
| 435 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 436 | void perf_evsel__exit(struct perf_evsel *evsel) |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 437 | { |
| 438 | assert(list_empty(&evsel->node)); |
| 439 | xyarray__delete(evsel->fd); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 440 | xyarray__delete(evsel->sample_id); |
| 441 | free(evsel->id); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void perf_evsel__delete(struct perf_evsel *evsel) |
| 445 | { |
| 446 | perf_evsel__exit(evsel); |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 447 | close_cgroup(evsel->cgrp); |
Stephane Eranian | f0c55bc | 2011-02-16 15:10:01 +0200 | [diff] [blame] | 448 | free(evsel->name); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 449 | free(evsel); |
| 450 | } |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 451 | |
| 452 | int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, |
| 453 | int cpu, int thread, bool scale) |
| 454 | { |
| 455 | struct perf_counts_values count; |
| 456 | size_t nv = scale ? 3 : 1; |
| 457 | |
| 458 | if (FD(evsel, cpu, thread) < 0) |
| 459 | return -EINVAL; |
| 460 | |
Arnaldo Carvalho de Melo | 4eed11d | 2011-01-04 00:13:17 -0200 | [diff] [blame] | 461 | if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0) |
| 462 | return -ENOMEM; |
| 463 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 464 | if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0) |
| 465 | return -errno; |
| 466 | |
| 467 | if (scale) { |
| 468 | if (count.run == 0) |
| 469 | count.val = 0; |
| 470 | else if (count.run < count.ena) |
| 471 | count.val = (u64)((double)count.val * count.ena / count.run + 0.5); |
| 472 | } else |
| 473 | count.ena = count.run = 0; |
| 474 | |
| 475 | evsel->counts->cpu[cpu] = count; |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | int __perf_evsel__read(struct perf_evsel *evsel, |
| 480 | int ncpus, int nthreads, bool scale) |
| 481 | { |
| 482 | size_t nv = scale ? 3 : 1; |
| 483 | int cpu, thread; |
| 484 | struct perf_counts_values *aggr = &evsel->counts->aggr, count; |
| 485 | |
Arnaldo Carvalho de Melo | 52bcd994 | 2011-02-03 17:26:06 -0200 | [diff] [blame] | 486 | aggr->val = aggr->ena = aggr->run = 0; |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 487 | |
| 488 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 489 | for (thread = 0; thread < nthreads; thread++) { |
| 490 | if (FD(evsel, cpu, thread) < 0) |
| 491 | continue; |
| 492 | |
| 493 | if (readn(FD(evsel, cpu, thread), |
| 494 | &count, nv * sizeof(u64)) < 0) |
| 495 | return -errno; |
| 496 | |
| 497 | aggr->val += count.val; |
| 498 | if (scale) { |
| 499 | aggr->ena += count.ena; |
| 500 | aggr->run += count.run; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | evsel->counts->scaled = 0; |
| 506 | if (scale) { |
| 507 | if (aggr->run == 0) { |
| 508 | evsel->counts->scaled = -1; |
| 509 | aggr->val = 0; |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | if (aggr->run < aggr->ena) { |
| 514 | evsel->counts->scaled = 1; |
| 515 | aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5); |
| 516 | } |
| 517 | } else |
| 518 | aggr->ena = aggr->run = 0; |
| 519 | |
| 520 | return 0; |
| 521 | } |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 522 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 523 | static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 524 | struct thread_map *threads, bool group, |
| 525 | struct xyarray *group_fds) |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 526 | { |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 527 | int cpu, thread; |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 528 | unsigned long flags = 0; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 529 | int pid = -1, err; |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 530 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 531 | if (evsel->fd == NULL && |
| 532 | perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0) |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 533 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 4eed11d | 2011-01-04 00:13:17 -0200 | [diff] [blame] | 534 | |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 535 | if (evsel->cgrp) { |
| 536 | flags = PERF_FLAG_PID_CGROUP; |
| 537 | pid = evsel->cgrp->fd; |
| 538 | } |
| 539 | |
Arnaldo Carvalho de Melo | 86bd5e8 | 2011-01-03 23:09:46 -0200 | [diff] [blame] | 540 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 541 | int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1; |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 542 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 543 | for (thread = 0; thread < threads->nr; thread++) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 544 | |
| 545 | if (!evsel->cgrp) |
| 546 | pid = threads->map[thread]; |
| 547 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 548 | FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr, |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 549 | pid, |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 550 | cpus->map[cpu], |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 551 | group_fd, flags); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 552 | if (FD(evsel, cpu, thread) < 0) { |
| 553 | err = -errno; |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 554 | goto out_close; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 555 | } |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 556 | |
| 557 | if (group && group_fd == -1) |
| 558 | group_fd = FD(evsel, cpu, thread); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 559 | } |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | return 0; |
| 563 | |
| 564 | out_close: |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 565 | do { |
| 566 | while (--thread >= 0) { |
| 567 | close(FD(evsel, cpu, thread)); |
| 568 | FD(evsel, cpu, thread) = -1; |
| 569 | } |
| 570 | thread = threads->nr; |
| 571 | } while (--cpu >= 0); |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 572 | return err; |
| 573 | } |
| 574 | |
| 575 | void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 576 | { |
| 577 | if (evsel->fd == NULL) |
| 578 | return; |
| 579 | |
| 580 | perf_evsel__close_fd(evsel, ncpus, nthreads); |
| 581 | perf_evsel__free_fd(evsel); |
| 582 | evsel->fd = NULL; |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 583 | } |
| 584 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 585 | static struct { |
| 586 | struct cpu_map map; |
| 587 | int cpus[1]; |
| 588 | } empty_cpu_map = { |
| 589 | .map.nr = 1, |
| 590 | .cpus = { -1, }, |
| 591 | }; |
| 592 | |
| 593 | static struct { |
| 594 | struct thread_map map; |
| 595 | int threads[1]; |
| 596 | } empty_thread_map = { |
| 597 | .map.nr = 1, |
| 598 | .threads = { -1, }, |
| 599 | }; |
| 600 | |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 601 | int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 602 | struct thread_map *threads, bool group, |
| 603 | struct xyarray *group_fd) |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 604 | { |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 605 | if (cpus == NULL) { |
| 606 | /* Work around old compiler warnings about strict aliasing */ |
| 607 | cpus = &empty_cpu_map.map; |
| 608 | } |
| 609 | |
| 610 | if (threads == NULL) |
| 611 | threads = &empty_thread_map.map; |
| 612 | |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 613 | return __perf_evsel__open(evsel, cpus, threads, group, group_fd); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 614 | } |
| 615 | |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 616 | int perf_evsel__open_per_cpu(struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 617 | struct cpu_map *cpus, bool group, |
| 618 | struct xyarray *group_fd) |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 619 | { |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 620 | return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, |
| 621 | group_fd); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 622 | } |
| 623 | |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 624 | int perf_evsel__open_per_thread(struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 625 | struct thread_map *threads, bool group, |
| 626 | struct xyarray *group_fd) |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 627 | { |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 628 | return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, |
| 629 | group_fd); |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 630 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 631 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 632 | static int perf_event__parse_id_sample(const union perf_event *event, u64 type, |
Jiri Olsa | 37073f9 | 2012-05-30 14:23:44 +0200 | [diff] [blame] | 633 | struct perf_sample *sample, |
| 634 | bool swapped) |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 635 | { |
| 636 | const u64 *array = event->sample.array; |
Jiri Olsa | 37073f9 | 2012-05-30 14:23:44 +0200 | [diff] [blame] | 637 | union u64_swap u; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 638 | |
| 639 | array += ((event->header.size - |
| 640 | sizeof(event->header)) / sizeof(u64)) - 1; |
| 641 | |
| 642 | if (type & PERF_SAMPLE_CPU) { |
Jiri Olsa | 37073f9 | 2012-05-30 14:23:44 +0200 | [diff] [blame] | 643 | u.val64 = *array; |
| 644 | if (swapped) { |
| 645 | /* undo swap of u64, then swap on individual u32s */ |
| 646 | u.val64 = bswap_64(u.val64); |
| 647 | u.val32[0] = bswap_32(u.val32[0]); |
| 648 | } |
| 649 | |
| 650 | sample->cpu = u.val32[0]; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 651 | array--; |
| 652 | } |
| 653 | |
| 654 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 655 | sample->stream_id = *array; |
| 656 | array--; |
| 657 | } |
| 658 | |
| 659 | if (type & PERF_SAMPLE_ID) { |
| 660 | sample->id = *array; |
| 661 | array--; |
| 662 | } |
| 663 | |
| 664 | if (type & PERF_SAMPLE_TIME) { |
| 665 | sample->time = *array; |
| 666 | array--; |
| 667 | } |
| 668 | |
| 669 | if (type & PERF_SAMPLE_TID) { |
Jiri Olsa | 37073f9 | 2012-05-30 14:23:44 +0200 | [diff] [blame] | 670 | u.val64 = *array; |
| 671 | if (swapped) { |
| 672 | /* undo swap of u64, then swap on individual u32s */ |
| 673 | u.val64 = bswap_64(u.val64); |
| 674 | u.val32[0] = bswap_32(u.val32[0]); |
| 675 | u.val32[1] = bswap_32(u.val32[1]); |
| 676 | } |
| 677 | |
| 678 | sample->pid = u.val32[0]; |
| 679 | sample->tid = u.val32[1]; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 685 | static bool sample_overlap(const union perf_event *event, |
| 686 | const void *offset, u64 size) |
| 687 | { |
| 688 | const void *base = event; |
| 689 | |
| 690 | if (offset + size > base + event->header.size) |
| 691 | return true; |
| 692 | |
| 693 | return false; |
| 694 | } |
| 695 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 696 | int perf_event__parse_sample(const union perf_event *event, u64 type, |
Frederic Weisbecker | a285412 | 2011-05-21 19:33:04 +0200 | [diff] [blame] | 697 | int sample_size, bool sample_id_all, |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 698 | struct perf_sample *data, bool swapped) |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 699 | { |
| 700 | const u64 *array; |
| 701 | |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 702 | /* |
| 703 | * used for cross-endian analysis. See git commit 65014ab3 |
| 704 | * for why this goofiness is needed. |
| 705 | */ |
Jiri Olsa | 6a11f92 | 2012-05-16 08:59:04 +0200 | [diff] [blame] | 706 | union u64_swap u; |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 707 | |
Robert Richter | f3bda2c | 2011-12-15 17:32:39 +0100 | [diff] [blame] | 708 | memset(data, 0, sizeof(*data)); |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 709 | data->cpu = data->pid = data->tid = -1; |
| 710 | data->stream_id = data->id = data->time = -1ULL; |
Naveen N. Rao | a4a03fc | 2012-02-03 22:31:13 +0530 | [diff] [blame] | 711 | data->period = 1; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 712 | |
| 713 | if (event->header.type != PERF_RECORD_SAMPLE) { |
| 714 | if (!sample_id_all) |
| 715 | return 0; |
Jiri Olsa | 37073f9 | 2012-05-30 14:23:44 +0200 | [diff] [blame] | 716 | return perf_event__parse_id_sample(event, type, data, swapped); |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | array = event->sample.array; |
| 720 | |
Frederic Weisbecker | a285412 | 2011-05-21 19:33:04 +0200 | [diff] [blame] | 721 | if (sample_size + sizeof(event->header) > event->header.size) |
| 722 | return -EFAULT; |
| 723 | |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 724 | if (type & PERF_SAMPLE_IP) { |
| 725 | data->ip = event->ip.ip; |
| 726 | array++; |
| 727 | } |
| 728 | |
| 729 | if (type & PERF_SAMPLE_TID) { |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 730 | u.val64 = *array; |
| 731 | if (swapped) { |
| 732 | /* undo swap of u64, then swap on individual u32s */ |
| 733 | u.val64 = bswap_64(u.val64); |
| 734 | u.val32[0] = bswap_32(u.val32[0]); |
| 735 | u.val32[1] = bswap_32(u.val32[1]); |
| 736 | } |
| 737 | |
| 738 | data->pid = u.val32[0]; |
| 739 | data->tid = u.val32[1]; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 740 | array++; |
| 741 | } |
| 742 | |
| 743 | if (type & PERF_SAMPLE_TIME) { |
| 744 | data->time = *array; |
| 745 | array++; |
| 746 | } |
| 747 | |
David Ahern | 7cec092 | 2011-05-30 13:08:23 -0600 | [diff] [blame] | 748 | data->addr = 0; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 749 | if (type & PERF_SAMPLE_ADDR) { |
| 750 | data->addr = *array; |
| 751 | array++; |
| 752 | } |
| 753 | |
| 754 | data->id = -1ULL; |
| 755 | if (type & PERF_SAMPLE_ID) { |
| 756 | data->id = *array; |
| 757 | array++; |
| 758 | } |
| 759 | |
| 760 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 761 | data->stream_id = *array; |
| 762 | array++; |
| 763 | } |
| 764 | |
| 765 | if (type & PERF_SAMPLE_CPU) { |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 766 | |
| 767 | u.val64 = *array; |
| 768 | if (swapped) { |
| 769 | /* undo swap of u64, then swap on individual u32s */ |
| 770 | u.val64 = bswap_64(u.val64); |
| 771 | u.val32[0] = bswap_32(u.val32[0]); |
| 772 | } |
| 773 | |
| 774 | data->cpu = u.val32[0]; |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 775 | array++; |
| 776 | } |
| 777 | |
| 778 | if (type & PERF_SAMPLE_PERIOD) { |
| 779 | data->period = *array; |
| 780 | array++; |
| 781 | } |
| 782 | |
| 783 | if (type & PERF_SAMPLE_READ) { |
Masanari Iida | f9d3699 | 2012-01-25 15:20:40 +0100 | [diff] [blame] | 784 | fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n"); |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 785 | return -1; |
| 786 | } |
| 787 | |
| 788 | if (type & PERF_SAMPLE_CALLCHAIN) { |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 789 | if (sample_overlap(event, array, sizeof(data->callchain->nr))) |
| 790 | return -EFAULT; |
| 791 | |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 792 | data->callchain = (struct ip_callchain *)array; |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 793 | |
| 794 | if (sample_overlap(event, array, data->callchain->nr)) |
| 795 | return -EFAULT; |
| 796 | |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 797 | array += 1 + data->callchain->nr; |
| 798 | } |
| 799 | |
| 800 | if (type & PERF_SAMPLE_RAW) { |
Jiri Olsa | 8e303f2 | 2011-09-29 17:05:08 +0200 | [diff] [blame] | 801 | const u64 *pdata; |
| 802 | |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 803 | u.val64 = *array; |
| 804 | if (WARN_ONCE(swapped, |
| 805 | "Endianness of raw data not corrected!\n")) { |
| 806 | /* undo swap of u64, then swap on individual u32s */ |
| 807 | u.val64 = bswap_64(u.val64); |
| 808 | u.val32[0] = bswap_32(u.val32[0]); |
| 809 | u.val32[1] = bswap_32(u.val32[1]); |
| 810 | } |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 811 | |
| 812 | if (sample_overlap(event, array, sizeof(u32))) |
| 813 | return -EFAULT; |
| 814 | |
David Ahern | 936be50 | 2011-09-06 09:12:26 -0600 | [diff] [blame] | 815 | data->raw_size = u.val32[0]; |
Jiri Olsa | 8e303f2 | 2011-09-29 17:05:08 +0200 | [diff] [blame] | 816 | pdata = (void *) array + sizeof(u32); |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 817 | |
Jiri Olsa | 8e303f2 | 2011-09-29 17:05:08 +0200 | [diff] [blame] | 818 | if (sample_overlap(event, pdata, data->raw_size)) |
Frederic Weisbecker | 98e1da9 | 2011-05-21 20:08:15 +0200 | [diff] [blame] | 819 | return -EFAULT; |
| 820 | |
Jiri Olsa | 8e303f2 | 2011-09-29 17:05:08 +0200 | [diff] [blame] | 821 | data->raw_data = (void *) pdata; |
Stephane Eranian | fa30c96 | 2012-03-17 23:23:18 +0100 | [diff] [blame] | 822 | |
| 823 | array = (void *)array + data->raw_size + sizeof(u32); |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 824 | } |
| 825 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 826 | if (type & PERF_SAMPLE_BRANCH_STACK) { |
| 827 | u64 sz; |
| 828 | |
| 829 | data->branch_stack = (struct branch_stack *)array; |
| 830 | array++; /* nr */ |
| 831 | |
| 832 | sz = data->branch_stack->nr * sizeof(struct branch_entry); |
| 833 | sz /= sizeof(u64); |
| 834 | array += sz; |
| 835 | } |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame] | 836 | return 0; |
| 837 | } |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 838 | |
| 839 | int perf_event__synthesize_sample(union perf_event *event, u64 type, |
| 840 | const struct perf_sample *sample, |
| 841 | bool swapped) |
| 842 | { |
| 843 | u64 *array; |
| 844 | |
| 845 | /* |
| 846 | * used for cross-endian analysis. See git commit 65014ab3 |
| 847 | * for why this goofiness is needed. |
| 848 | */ |
Jiri Olsa | 6a11f92 | 2012-05-16 08:59:04 +0200 | [diff] [blame] | 849 | union u64_swap u; |
Andrew Vagin | 74eec26 | 2011-11-28 12:03:31 +0300 | [diff] [blame] | 850 | |
| 851 | array = event->sample.array; |
| 852 | |
| 853 | if (type & PERF_SAMPLE_IP) { |
| 854 | event->ip.ip = sample->ip; |
| 855 | array++; |
| 856 | } |
| 857 | |
| 858 | if (type & PERF_SAMPLE_TID) { |
| 859 | u.val32[0] = sample->pid; |
| 860 | u.val32[1] = sample->tid; |
| 861 | if (swapped) { |
| 862 | /* |
| 863 | * Inverse of what is done in perf_event__parse_sample |
| 864 | */ |
| 865 | u.val32[0] = bswap_32(u.val32[0]); |
| 866 | u.val32[1] = bswap_32(u.val32[1]); |
| 867 | u.val64 = bswap_64(u.val64); |
| 868 | } |
| 869 | |
| 870 | *array = u.val64; |
| 871 | array++; |
| 872 | } |
| 873 | |
| 874 | if (type & PERF_SAMPLE_TIME) { |
| 875 | *array = sample->time; |
| 876 | array++; |
| 877 | } |
| 878 | |
| 879 | if (type & PERF_SAMPLE_ADDR) { |
| 880 | *array = sample->addr; |
| 881 | array++; |
| 882 | } |
| 883 | |
| 884 | if (type & PERF_SAMPLE_ID) { |
| 885 | *array = sample->id; |
| 886 | array++; |
| 887 | } |
| 888 | |
| 889 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 890 | *array = sample->stream_id; |
| 891 | array++; |
| 892 | } |
| 893 | |
| 894 | if (type & PERF_SAMPLE_CPU) { |
| 895 | u.val32[0] = sample->cpu; |
| 896 | if (swapped) { |
| 897 | /* |
| 898 | * Inverse of what is done in perf_event__parse_sample |
| 899 | */ |
| 900 | u.val32[0] = bswap_32(u.val32[0]); |
| 901 | u.val64 = bswap_64(u.val64); |
| 902 | } |
| 903 | *array = u.val64; |
| 904 | array++; |
| 905 | } |
| 906 | |
| 907 | if (type & PERF_SAMPLE_PERIOD) { |
| 908 | *array = sample->period; |
| 909 | array++; |
| 910 | } |
| 911 | |
| 912 | return 0; |
| 913 | } |