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