]>
Commit | Line | Data |
---|---|---|
1b290d67 | 1 | #include "../../../include/linux/hw_breakpoint.h" |
8ad8db37 | 2 | #include "util.h" |
6b58e7f1 | 3 | #include "../perf.h" |
8ad8db37 IM |
4 | #include "parse-options.h" |
5 | #include "parse-events.h" | |
6 | #include "exec_cmd.h" | |
a0055ae2 | 7 | #include "string.h" |
5aab621b | 8 | #include "symbol.h" |
5beeded1 | 9 | #include "cache.h" |
8755a8f2 | 10 | #include "header.h" |
549104f2 | 11 | #include "debugfs.h" |
8ad8db37 | 12 | |
c171b552 | 13 | int nr_counters; |
8ad8db37 | 14 | |
cdd6c482 | 15 | struct perf_event_attr attrs[MAX_COUNTERS]; |
c171b552 | 16 | char *filters[MAX_COUNTERS]; |
8ad8db37 IM |
17 | |
18 | struct event_symbol { | |
83a0944f IM |
19 | u8 type; |
20 | u64 config; | |
21 | const char *symbol; | |
22 | const char *alias; | |
8ad8db37 IM |
23 | }; |
24 | ||
bcd3279f FW |
25 | enum event_result { |
26 | EVT_FAILED, | |
27 | EVT_HANDLED, | |
28 | EVT_HANDLED_ALL | |
29 | }; | |
30 | ||
5beeded1 JB |
31 | char debugfs_path[MAXPATHLEN]; |
32 | ||
51e26842 JSR |
33 | #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x |
34 | #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x | |
a21ca2ca | 35 | |
8ad8db37 | 36 | static struct event_symbol event_symbols[] = { |
74d5b588 JSR |
37 | { CHW(CPU_CYCLES), "cpu-cycles", "cycles" }, |
38 | { CHW(INSTRUCTIONS), "instructions", "" }, | |
39 | { CHW(CACHE_REFERENCES), "cache-references", "" }, | |
40 | { CHW(CACHE_MISSES), "cache-misses", "" }, | |
41 | { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" }, | |
42 | { CHW(BRANCH_MISSES), "branch-misses", "" }, | |
43 | { CHW(BUS_CYCLES), "bus-cycles", "" }, | |
44 | ||
45 | { CSW(CPU_CLOCK), "cpu-clock", "" }, | |
46 | { CSW(TASK_CLOCK), "task-clock", "" }, | |
c0c22dbf | 47 | { CSW(PAGE_FAULTS), "page-faults", "faults" }, |
74d5b588 JSR |
48 | { CSW(PAGE_FAULTS_MIN), "minor-faults", "" }, |
49 | { CSW(PAGE_FAULTS_MAJ), "major-faults", "" }, | |
50 | { CSW(CONTEXT_SWITCHES), "context-switches", "cs" }, | |
51 | { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" }, | |
f7d79860 AB |
52 | { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" }, |
53 | { CSW(EMULATION_FAULTS), "emulation-faults", "" }, | |
8ad8db37 IM |
54 | }; |
55 | ||
cdd6c482 IM |
56 | #define __PERF_EVENT_FIELD(config, name) \ |
57 | ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) | |
5242519b | 58 | |
cdd6c482 IM |
59 | #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) |
60 | #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) | |
61 | #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) | |
62 | #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) | |
5242519b | 63 | |
83a0944f | 64 | static const char *hw_event_names[] = { |
8faf3b54 | 65 | "cycles", |
5242519b | 66 | "instructions", |
8faf3b54 IM |
67 | "cache-references", |
68 | "cache-misses", | |
5242519b | 69 | "branches", |
8faf3b54 IM |
70 | "branch-misses", |
71 | "bus-cycles", | |
5242519b IM |
72 | }; |
73 | ||
83a0944f | 74 | static const char *sw_event_names[] = { |
44175b6f IM |
75 | "cpu-clock-msecs", |
76 | "task-clock-msecs", | |
8faf3b54 IM |
77 | "page-faults", |
78 | "context-switches", | |
79 | "CPU-migrations", | |
80 | "minor-faults", | |
81 | "major-faults", | |
f7d79860 AB |
82 | "alignment-faults", |
83 | "emulation-faults", | |
5242519b IM |
84 | }; |
85 | ||
8326f44d IM |
86 | #define MAX_ALIASES 8 |
87 | ||
83a0944f | 88 | static const char *hw_cache[][MAX_ALIASES] = { |
9590b7ba AB |
89 | { "L1-dcache", "l1-d", "l1d", "L1-data", }, |
90 | { "L1-icache", "l1-i", "l1i", "L1-instruction", }, | |
e5c59547 JSR |
91 | { "LLC", "L2" }, |
92 | { "dTLB", "d-tlb", "Data-TLB", }, | |
93 | { "iTLB", "i-tlb", "Instruction-TLB", }, | |
94 | { "branch", "branches", "bpu", "btb", "bpc", }, | |
8326f44d IM |
95 | }; |
96 | ||
83a0944f | 97 | static const char *hw_cache_op[][MAX_ALIASES] = { |
e5c59547 JSR |
98 | { "load", "loads", "read", }, |
99 | { "store", "stores", "write", }, | |
100 | { "prefetch", "prefetches", "speculative-read", "speculative-load", }, | |
8326f44d IM |
101 | }; |
102 | ||
83a0944f | 103 | static const char *hw_cache_result[][MAX_ALIASES] = { |
e5c59547 JSR |
104 | { "refs", "Reference", "ops", "access", }, |
105 | { "misses", "miss", }, | |
8326f44d IM |
106 | }; |
107 | ||
06813f6c JSR |
108 | #define C(x) PERF_COUNT_HW_CACHE_##x |
109 | #define CACHE_READ (1 << C(OP_READ)) | |
110 | #define CACHE_WRITE (1 << C(OP_WRITE)) | |
111 | #define CACHE_PREFETCH (1 << C(OP_PREFETCH)) | |
112 | #define COP(x) (1 << x) | |
113 | ||
114 | /* | |
115 | * cache operartion stat | |
116 | * L1I : Read and prefetch only | |
117 | * ITLB and BPU : Read-only | |
118 | */ | |
119 | static unsigned long hw_cache_stat[C(MAX)] = { | |
120 | [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
121 | [C(L1I)] = (CACHE_READ | CACHE_PREFETCH), | |
122 | [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
123 | [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH), | |
124 | [C(ITLB)] = (CACHE_READ), | |
125 | [C(BPU)] = (CACHE_READ), | |
126 | }; | |
127 | ||
6b58e7f1 | 128 | #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \ |
f6bdafef | 129 | while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ |
6b58e7f1 | 130 | if (sys_dirent.d_type == DT_DIR && \ |
f6bdafef JB |
131 | (strcmp(sys_dirent.d_name, ".")) && \ |
132 | (strcmp(sys_dirent.d_name, ".."))) | |
133 | ||
ae07b63f PZ |
134 | static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) |
135 | { | |
136 | char evt_path[MAXPATHLEN]; | |
137 | int fd; | |
138 | ||
139 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path, | |
140 | sys_dir->d_name, evt_dir->d_name); | |
141 | fd = open(evt_path, O_RDONLY); | |
142 | if (fd < 0) | |
143 | return -EINVAL; | |
144 | close(fd); | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
6b58e7f1 | 149 | #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \ |
f6bdafef | 150 | while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ |
6b58e7f1 | 151 | if (evt_dirent.d_type == DT_DIR && \ |
f6bdafef | 152 | (strcmp(evt_dirent.d_name, ".")) && \ |
ae07b63f PZ |
153 | (strcmp(evt_dirent.d_name, "..")) && \ |
154 | (!tp_event_has_id(&sys_dirent, &evt_dirent))) | |
f6bdafef | 155 | |
270bbbe8 | 156 | #define MAX_EVENT_LENGTH 512 |
f6bdafef | 157 | |
f6bdafef | 158 | |
1ef2ed10 | 159 | struct tracepoint_path *tracepoint_id_to_path(u64 config) |
f6bdafef | 160 | { |
1ef2ed10 | 161 | struct tracepoint_path *path = NULL; |
f6bdafef JB |
162 | DIR *sys_dir, *evt_dir; |
163 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | |
f6bdafef | 164 | char id_buf[4]; |
725b1368 | 165 | int fd; |
f6bdafef JB |
166 | u64 id; |
167 | char evt_path[MAXPATHLEN]; | |
725b1368 | 168 | char dir_path[MAXPATHLEN]; |
f6bdafef | 169 | |
549104f2 | 170 | if (debugfs_valid_mountpoint(debugfs_path)) |
1ef2ed10 | 171 | return NULL; |
f6bdafef | 172 | |
5beeded1 | 173 | sys_dir = opendir(debugfs_path); |
f6bdafef | 174 | if (!sys_dir) |
725b1368 | 175 | return NULL; |
6b58e7f1 UD |
176 | |
177 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | |
725b1368 ED |
178 | |
179 | snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path, | |
180 | sys_dirent.d_name); | |
181 | evt_dir = opendir(dir_path); | |
182 | if (!evt_dir) | |
6b58e7f1 | 183 | continue; |
725b1368 | 184 | |
6b58e7f1 | 185 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
725b1368 ED |
186 | |
187 | snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, | |
f6bdafef | 188 | evt_dirent.d_name); |
725b1368 | 189 | fd = open(evt_path, O_RDONLY); |
f6bdafef JB |
190 | if (fd < 0) |
191 | continue; | |
192 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { | |
193 | close(fd); | |
194 | continue; | |
195 | } | |
196 | close(fd); | |
197 | id = atoll(id_buf); | |
198 | if (id == config) { | |
199 | closedir(evt_dir); | |
200 | closedir(sys_dir); | |
59b4caeb | 201 | path = zalloc(sizeof(*path)); |
1ef2ed10 FW |
202 | path->system = malloc(MAX_EVENT_LENGTH); |
203 | if (!path->system) { | |
204 | free(path); | |
205 | return NULL; | |
206 | } | |
207 | path->name = malloc(MAX_EVENT_LENGTH); | |
208 | if (!path->name) { | |
209 | free(path->system); | |
210 | free(path); | |
211 | return NULL; | |
212 | } | |
213 | strncpy(path->system, sys_dirent.d_name, | |
214 | MAX_EVENT_LENGTH); | |
215 | strncpy(path->name, evt_dirent.d_name, | |
216 | MAX_EVENT_LENGTH); | |
217 | return path; | |
f6bdafef JB |
218 | } |
219 | } | |
220 | closedir(evt_dir); | |
221 | } | |
222 | ||
f6bdafef | 223 | closedir(sys_dir); |
1ef2ed10 FW |
224 | return NULL; |
225 | } | |
226 | ||
227 | #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1) | |
228 | static const char *tracepoint_id_to_name(u64 config) | |
229 | { | |
230 | static char buf[TP_PATH_LEN]; | |
231 | struct tracepoint_path *path; | |
232 | ||
233 | path = tracepoint_id_to_path(config); | |
234 | if (path) { | |
235 | snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name); | |
236 | free(path->name); | |
237 | free(path->system); | |
238 | free(path); | |
239 | } else | |
240 | snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown"); | |
241 | ||
242 | return buf; | |
f6bdafef JB |
243 | } |
244 | ||
06813f6c JSR |
245 | static int is_cache_op_valid(u8 cache_type, u8 cache_op) |
246 | { | |
247 | if (hw_cache_stat[cache_type] & COP(cache_op)) | |
248 | return 1; /* valid */ | |
249 | else | |
250 | return 0; /* invalid */ | |
251 | } | |
252 | ||
e5c59547 JSR |
253 | static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result) |
254 | { | |
255 | static char name[50]; | |
256 | ||
257 | if (cache_result) { | |
258 | sprintf(name, "%s-%s-%s", hw_cache[cache_type][0], | |
259 | hw_cache_op[cache_op][0], | |
260 | hw_cache_result[cache_result][0]); | |
261 | } else { | |
262 | sprintf(name, "%s-%s", hw_cache[cache_type][0], | |
263 | hw_cache_op[cache_op][1]); | |
264 | } | |
265 | ||
266 | return name; | |
267 | } | |
268 | ||
83a0944f | 269 | const char *event_name(int counter) |
5242519b | 270 | { |
9cffa8d5 | 271 | u64 config = attrs[counter].config; |
a21ca2ca | 272 | int type = attrs[counter].type; |
8f18aec5 PZ |
273 | |
274 | return __event_name(type, config); | |
275 | } | |
276 | ||
83a0944f | 277 | const char *__event_name(int type, u64 config) |
8f18aec5 | 278 | { |
5242519b IM |
279 | static char buf[32]; |
280 | ||
8f18aec5 | 281 | if (type == PERF_TYPE_RAW) { |
a21ca2ca | 282 | sprintf(buf, "raw 0x%llx", config); |
5242519b IM |
283 | return buf; |
284 | } | |
285 | ||
286 | switch (type) { | |
287 | case PERF_TYPE_HARDWARE: | |
f4dbfa8f | 288 | if (config < PERF_COUNT_HW_MAX) |
a21ca2ca | 289 | return hw_event_names[config]; |
5242519b IM |
290 | return "unknown-hardware"; |
291 | ||
8326f44d | 292 | case PERF_TYPE_HW_CACHE: { |
9cffa8d5 | 293 | u8 cache_type, cache_op, cache_result; |
8326f44d IM |
294 | |
295 | cache_type = (config >> 0) & 0xff; | |
296 | if (cache_type > PERF_COUNT_HW_CACHE_MAX) | |
297 | return "unknown-ext-hardware-cache-type"; | |
298 | ||
299 | cache_op = (config >> 8) & 0xff; | |
8faf3b54 IM |
300 | if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX) |
301 | return "unknown-ext-hardware-cache-op"; | |
8326f44d IM |
302 | |
303 | cache_result = (config >> 16) & 0xff; | |
8faf3b54 IM |
304 | if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX) |
305 | return "unknown-ext-hardware-cache-result"; | |
8326f44d | 306 | |
06813f6c JSR |
307 | if (!is_cache_op_valid(cache_type, cache_op)) |
308 | return "invalid-cache"; | |
8326f44d | 309 | |
e5c59547 | 310 | return event_cache_name(cache_type, cache_op, cache_result); |
8326f44d IM |
311 | } |
312 | ||
5242519b | 313 | case PERF_TYPE_SOFTWARE: |
f4dbfa8f | 314 | if (config < PERF_COUNT_SW_MAX) |
a21ca2ca | 315 | return sw_event_names[config]; |
5242519b IM |
316 | return "unknown-software"; |
317 | ||
f6bdafef JB |
318 | case PERF_TYPE_TRACEPOINT: |
319 | return tracepoint_id_to_name(config); | |
320 | ||
5242519b IM |
321 | default: |
322 | break; | |
323 | } | |
324 | ||
325 | return "unknown"; | |
326 | } | |
327 | ||
83a0944f | 328 | static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size) |
8326f44d IM |
329 | { |
330 | int i, j; | |
61c45981 | 331 | int n, longest = -1; |
8326f44d IM |
332 | |
333 | for (i = 0; i < size; i++) { | |
61c45981 PM |
334 | for (j = 0; j < MAX_ALIASES && names[i][j]; j++) { |
335 | n = strlen(names[i][j]); | |
336 | if (n > longest && !strncasecmp(*str, names[i][j], n)) | |
337 | longest = n; | |
338 | } | |
339 | if (longest > 0) { | |
340 | *str += longest; | |
341 | return i; | |
8326f44d IM |
342 | } |
343 | } | |
344 | ||
8953645f | 345 | return -1; |
8326f44d IM |
346 | } |
347 | ||
bcd3279f | 348 | static enum event_result |
cdd6c482 | 349 | parse_generic_hw_event(const char **str, struct perf_event_attr *attr) |
8326f44d | 350 | { |
61c45981 PM |
351 | const char *s = *str; |
352 | int cache_type = -1, cache_op = -1, cache_result = -1; | |
8326f44d | 353 | |
61c45981 | 354 | cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX); |
8326f44d IM |
355 | /* |
356 | * No fallback - if we cannot get a clear cache type | |
357 | * then bail out: | |
358 | */ | |
359 | if (cache_type == -1) | |
bcd3279f | 360 | return EVT_FAILED; |
61c45981 PM |
361 | |
362 | while ((cache_op == -1 || cache_result == -1) && *s == '-') { | |
363 | ++s; | |
364 | ||
365 | if (cache_op == -1) { | |
366 | cache_op = parse_aliases(&s, hw_cache_op, | |
367 | PERF_COUNT_HW_CACHE_OP_MAX); | |
368 | if (cache_op >= 0) { | |
369 | if (!is_cache_op_valid(cache_type, cache_op)) | |
370 | return 0; | |
371 | continue; | |
372 | } | |
373 | } | |
374 | ||
375 | if (cache_result == -1) { | |
376 | cache_result = parse_aliases(&s, hw_cache_result, | |
377 | PERF_COUNT_HW_CACHE_RESULT_MAX); | |
378 | if (cache_result >= 0) | |
379 | continue; | |
380 | } | |
381 | ||
382 | /* | |
383 | * Can't parse this as a cache op or result, so back up | |
384 | * to the '-'. | |
385 | */ | |
386 | --s; | |
387 | break; | |
388 | } | |
8326f44d | 389 | |
8326f44d IM |
390 | /* |
391 | * Fall back to reads: | |
392 | */ | |
8953645f IM |
393 | if (cache_op == -1) |
394 | cache_op = PERF_COUNT_HW_CACHE_OP_READ; | |
8326f44d | 395 | |
8326f44d IM |
396 | /* |
397 | * Fall back to accesses: | |
398 | */ | |
399 | if (cache_result == -1) | |
400 | cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; | |
401 | ||
402 | attr->config = cache_type | (cache_op << 8) | (cache_result << 16); | |
403 | attr->type = PERF_TYPE_HW_CACHE; | |
404 | ||
61c45981 | 405 | *str = s; |
bcd3279f FW |
406 | return EVT_HANDLED; |
407 | } | |
408 | ||
409 | static enum event_result | |
410 | parse_single_tracepoint_event(char *sys_name, | |
411 | const char *evt_name, | |
412 | unsigned int evt_length, | |
cdd6c482 | 413 | struct perf_event_attr *attr, |
bcd3279f FW |
414 | const char **strp) |
415 | { | |
416 | char evt_path[MAXPATHLEN]; | |
417 | char id_buf[4]; | |
418 | u64 id; | |
419 | int fd; | |
420 | ||
bdef3b02 FW |
421 | attr->sample_type |= PERF_SAMPLE_RAW; |
422 | attr->sample_type |= PERF_SAMPLE_TIME; | |
423 | attr->sample_type |= PERF_SAMPLE_CPU; | |
bcd3279f | 424 | |
f9212819 FW |
425 | attr->sample_period = 1; |
426 | ||
bcd3279f FW |
427 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path, |
428 | sys_name, evt_name); | |
429 | ||
430 | fd = open(evt_path, O_RDONLY); | |
431 | if (fd < 0) | |
432 | return EVT_FAILED; | |
433 | ||
434 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { | |
435 | close(fd); | |
436 | return EVT_FAILED; | |
437 | } | |
438 | ||
439 | close(fd); | |
440 | id = atoll(id_buf); | |
441 | attr->config = id; | |
442 | attr->type = PERF_TYPE_TRACEPOINT; | |
443 | *strp = evt_name + evt_length; | |
444 | ||
445 | return EVT_HANDLED; | |
8326f44d IM |
446 | } |
447 | ||
bcd3279f FW |
448 | /* sys + ':' + event + ':' + flags*/ |
449 | #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128) | |
450 | static enum event_result | |
fb1d2edf MH |
451 | parse_multiple_tracepoint_event(char *sys_name, const char *evt_exp, |
452 | char *flags) | |
bcd3279f FW |
453 | { |
454 | char evt_path[MAXPATHLEN]; | |
455 | struct dirent *evt_ent; | |
456 | DIR *evt_dir; | |
457 | ||
458 | snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name); | |
459 | evt_dir = opendir(evt_path); | |
460 | ||
461 | if (!evt_dir) { | |
462 | perror("Can't open event dir"); | |
463 | return EVT_FAILED; | |
464 | } | |
465 | ||
466 | while ((evt_ent = readdir(evt_dir))) { | |
467 | char event_opt[MAX_EVOPT_LEN + 1]; | |
468 | int len; | |
bcd3279f FW |
469 | |
470 | if (!strcmp(evt_ent->d_name, ".") | |
471 | || !strcmp(evt_ent->d_name, "..") | |
472 | || !strcmp(evt_ent->d_name, "enable") | |
473 | || !strcmp(evt_ent->d_name, "filter")) | |
474 | continue; | |
475 | ||
fb1d2edf MH |
476 | if (!strglobmatch(evt_ent->d_name, evt_exp)) |
477 | continue; | |
478 | ||
180570fd UD |
479 | len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name, |
480 | evt_ent->d_name, flags ? ":" : "", | |
481 | flags ?: ""); | |
bcd3279f FW |
482 | if (len < 0) |
483 | return EVT_FAILED; | |
484 | ||
bcd3279f FW |
485 | if (parse_events(NULL, event_opt, 0)) |
486 | return EVT_FAILED; | |
487 | } | |
488 | ||
489 | return EVT_HANDLED_ALL; | |
490 | } | |
491 | ||
492 | ||
493 | static enum event_result parse_tracepoint_event(const char **strp, | |
cdd6c482 | 494 | struct perf_event_attr *attr) |
f6bdafef JB |
495 | { |
496 | const char *evt_name; | |
3a9f131f | 497 | char *flags; |
f6bdafef | 498 | char sys_name[MAX_EVENT_LENGTH]; |
f6bdafef | 499 | unsigned int sys_length, evt_length; |
f6bdafef | 500 | |
549104f2 | 501 | if (debugfs_valid_mountpoint(debugfs_path)) |
f6bdafef JB |
502 | return 0; |
503 | ||
504 | evt_name = strchr(*strp, ':'); | |
505 | if (!evt_name) | |
bcd3279f | 506 | return EVT_FAILED; |
f6bdafef JB |
507 | |
508 | sys_length = evt_name - *strp; | |
509 | if (sys_length >= MAX_EVENT_LENGTH) | |
510 | return 0; | |
511 | ||
512 | strncpy(sys_name, *strp, sys_length); | |
513 | sys_name[sys_length] = '\0'; | |
514 | evt_name = evt_name + 1; | |
3a9f131f FW |
515 | |
516 | flags = strchr(evt_name, ':'); | |
517 | if (flags) { | |
1fc35b29 IM |
518 | /* split it out: */ |
519 | evt_name = strndup(evt_name, flags - evt_name); | |
3a9f131f | 520 | flags++; |
3a9f131f FW |
521 | } |
522 | ||
f6bdafef JB |
523 | evt_length = strlen(evt_name); |
524 | if (evt_length >= MAX_EVENT_LENGTH) | |
bcd3279f | 525 | return EVT_FAILED; |
f6bdafef | 526 | |
fb1d2edf | 527 | if (strpbrk(evt_name, "*?")) { |
bcd3279f | 528 | *strp = evt_name + evt_length; |
fb1d2edf MH |
529 | return parse_multiple_tracepoint_event(sys_name, evt_name, |
530 | flags); | |
bcd3279f FW |
531 | } else |
532 | return parse_single_tracepoint_event(sys_name, evt_name, | |
bdef3b02 | 533 | evt_length, attr, strp); |
f6bdafef JB |
534 | } |
535 | ||
1b290d67 FW |
536 | static enum event_result |
537 | parse_breakpoint_type(const char *type, const char **strp, | |
538 | struct perf_event_attr *attr) | |
539 | { | |
540 | int i; | |
541 | ||
542 | for (i = 0; i < 3; i++) { | |
543 | if (!type[i]) | |
544 | break; | |
545 | ||
546 | switch (type[i]) { | |
547 | case 'r': | |
548 | attr->bp_type |= HW_BREAKPOINT_R; | |
549 | break; | |
550 | case 'w': | |
551 | attr->bp_type |= HW_BREAKPOINT_W; | |
552 | break; | |
553 | case 'x': | |
554 | attr->bp_type |= HW_BREAKPOINT_X; | |
555 | break; | |
556 | default: | |
557 | return EVT_FAILED; | |
558 | } | |
559 | } | |
560 | if (!attr->bp_type) /* Default */ | |
561 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; | |
562 | ||
563 | *strp = type + i; | |
564 | ||
565 | return EVT_HANDLED; | |
566 | } | |
567 | ||
568 | static enum event_result | |
569 | parse_breakpoint_event(const char **strp, struct perf_event_attr *attr) | |
570 | { | |
571 | const char *target; | |
572 | const char *type; | |
573 | char *endaddr; | |
574 | u64 addr; | |
575 | enum event_result err; | |
576 | ||
577 | target = strchr(*strp, ':'); | |
578 | if (!target) | |
579 | return EVT_FAILED; | |
580 | ||
581 | if (strncmp(*strp, "mem", target - *strp) != 0) | |
582 | return EVT_FAILED; | |
583 | ||
584 | target++; | |
585 | ||
586 | addr = strtoull(target, &endaddr, 0); | |
587 | if (target == endaddr) | |
588 | return EVT_FAILED; | |
589 | ||
590 | attr->bp_addr = addr; | |
591 | *strp = endaddr; | |
592 | ||
593 | type = strchr(target, ':'); | |
594 | ||
595 | /* If no type is defined, just rw as default */ | |
596 | if (!type) { | |
597 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; | |
598 | } else { | |
599 | err = parse_breakpoint_type(++type, strp, attr); | |
600 | if (err == EVT_FAILED) | |
601 | return EVT_FAILED; | |
602 | } | |
603 | ||
604 | /* We should find a nice way to override the access type */ | |
605 | attr->bp_len = HW_BREAKPOINT_LEN_4; | |
606 | attr->type = PERF_TYPE_BREAKPOINT; | |
607 | ||
608 | return EVT_HANDLED; | |
609 | } | |
610 | ||
74d5b588 JSR |
611 | static int check_events(const char *str, unsigned int i) |
612 | { | |
61c45981 | 613 | int n; |
74d5b588 | 614 | |
61c45981 PM |
615 | n = strlen(event_symbols[i].symbol); |
616 | if (!strncmp(str, event_symbols[i].symbol, n)) | |
617 | return n; | |
618 | ||
619 | n = strlen(event_symbols[i].alias); | |
620 | if (n) | |
621 | if (!strncmp(str, event_symbols[i].alias, n)) | |
622 | return n; | |
74d5b588 JSR |
623 | return 0; |
624 | } | |
625 | ||
bcd3279f | 626 | static enum event_result |
cdd6c482 | 627 | parse_symbolic_event(const char **strp, struct perf_event_attr *attr) |
8ad8db37 | 628 | { |
61c45981 | 629 | const char *str = *strp; |
8ad8db37 | 630 | unsigned int i; |
61c45981 | 631 | int n; |
8ad8db37 | 632 | |
61c45981 PM |
633 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { |
634 | n = check_events(str, i); | |
635 | if (n > 0) { | |
636 | attr->type = event_symbols[i].type; | |
637 | attr->config = event_symbols[i].config; | |
638 | *strp = str + n; | |
bcd3279f | 639 | return EVT_HANDLED; |
61c45981 PM |
640 | } |
641 | } | |
bcd3279f | 642 | return EVT_FAILED; |
61c45981 PM |
643 | } |
644 | ||
bcd3279f | 645 | static enum event_result |
cdd6c482 | 646 | parse_raw_event(const char **strp, struct perf_event_attr *attr) |
61c45981 PM |
647 | { |
648 | const char *str = *strp; | |
649 | u64 config; | |
650 | int n; | |
a21ca2ca | 651 | |
61c45981 | 652 | if (*str != 'r') |
bcd3279f | 653 | return EVT_FAILED; |
61c45981 PM |
654 | n = hex2u64(str + 1, &config); |
655 | if (n > 0) { | |
69fef0d2 PZ |
656 | if (str[n+1] == 'p') { |
657 | attr->precise = 1; | |
658 | n++; | |
659 | } | |
61c45981 PM |
660 | *strp = str + n + 1; |
661 | attr->type = PERF_TYPE_RAW; | |
662 | attr->config = config; | |
bcd3279f | 663 | return EVT_HANDLED; |
a21ca2ca | 664 | } |
bcd3279f | 665 | return EVT_FAILED; |
61c45981 | 666 | } |
8ad8db37 | 667 | |
bcd3279f | 668 | static enum event_result |
cdd6c482 | 669 | parse_numeric_event(const char **strp, struct perf_event_attr *attr) |
61c45981 PM |
670 | { |
671 | const char *str = *strp; | |
672 | char *endp; | |
673 | unsigned long type; | |
674 | u64 config; | |
675 | ||
676 | type = strtoul(str, &endp, 0); | |
677 | if (endp > str && type < PERF_TYPE_MAX && *endp == ':') { | |
678 | str = endp + 1; | |
679 | config = strtoul(str, &endp, 0); | |
680 | if (endp > str) { | |
681 | attr->type = type; | |
682 | attr->config = config; | |
683 | *strp = endp; | |
bcd3279f | 684 | return EVT_HANDLED; |
a0055ae2 | 685 | } |
61c45981 | 686 | } |
bcd3279f | 687 | return EVT_FAILED; |
61c45981 PM |
688 | } |
689 | ||
bcd3279f | 690 | static enum event_result |
cdd6c482 | 691 | parse_event_modifier(const char **strp, struct perf_event_attr *attr) |
61c45981 PM |
692 | { |
693 | const char *str = *strp; | |
694 | int eu = 1, ek = 1, eh = 1; | |
a21ca2ca | 695 | |
61c45981 | 696 | if (*str++ != ':') |
a21ca2ca | 697 | return 0; |
61c45981 PM |
698 | while (*str) { |
699 | if (*str == 'u') | |
700 | eu = 0; | |
701 | else if (*str == 'k') | |
702 | ek = 0; | |
703 | else if (*str == 'h') | |
704 | eh = 0; | |
705 | else | |
706 | break; | |
707 | ++str; | |
5242519b | 708 | } |
61c45981 PM |
709 | if (str >= *strp + 2) { |
710 | *strp = str; | |
711 | attr->exclude_user = eu; | |
712 | attr->exclude_kernel = ek; | |
713 | attr->exclude_hv = eh; | |
714 | return 1; | |
715 | } | |
716 | return 0; | |
717 | } | |
8ad8db37 | 718 | |
61c45981 PM |
719 | /* |
720 | * Each event can have multiple symbolic names. | |
721 | * Symbolic names are (almost) exactly matched. | |
722 | */ | |
bcd3279f | 723 | static enum event_result |
cdd6c482 | 724 | parse_event_symbols(const char **str, struct perf_event_attr *attr) |
61c45981 | 725 | { |
bcd3279f FW |
726 | enum event_result ret; |
727 | ||
728 | ret = parse_tracepoint_event(str, attr); | |
729 | if (ret != EVT_FAILED) | |
730 | goto modifier; | |
731 | ||
732 | ret = parse_raw_event(str, attr); | |
733 | if (ret != EVT_FAILED) | |
734 | goto modifier; | |
a21ca2ca | 735 | |
bcd3279f FW |
736 | ret = parse_numeric_event(str, attr); |
737 | if (ret != EVT_FAILED) | |
738 | goto modifier; | |
739 | ||
740 | ret = parse_symbolic_event(str, attr); | |
741 | if (ret != EVT_FAILED) | |
742 | goto modifier; | |
743 | ||
744 | ret = parse_generic_hw_event(str, attr); | |
745 | if (ret != EVT_FAILED) | |
746 | goto modifier; | |
747 | ||
1b290d67 FW |
748 | ret = parse_breakpoint_event(str, attr); |
749 | if (ret != EVT_FAILED) | |
750 | goto modifier; | |
751 | ||
85df6f68 MR |
752 | fprintf(stderr, "invalid or unsupported event: '%s'\n", *str); |
753 | fprintf(stderr, "Run 'perf list' for a list of valid events\n"); | |
bcd3279f FW |
754 | return EVT_FAILED; |
755 | ||
756 | modifier: | |
61c45981 | 757 | parse_event_modifier(str, attr); |
8ad8db37 | 758 | |
bcd3279f | 759 | return ret; |
8ad8db37 IM |
760 | } |
761 | ||
ae99fb2c | 762 | static int store_event_type(const char *orgname) |
8755a8f2 AV |
763 | { |
764 | char filename[PATH_MAX], *c; | |
765 | FILE *file; | |
ae99fb2c | 766 | int id, n; |
8755a8f2 | 767 | |
63c9e01e AC |
768 | sprintf(filename, "%s/", debugfs_path); |
769 | strncat(filename, orgname, strlen(orgname)); | |
770 | strcat(filename, "/id"); | |
771 | ||
8755a8f2 AV |
772 | c = strchr(filename, ':'); |
773 | if (c) | |
774 | *c = '/'; | |
775 | ||
776 | file = fopen(filename, "r"); | |
777 | if (!file) | |
ae99fb2c ACM |
778 | return 0; |
779 | n = fscanf(file, "%i", &id); | |
8755a8f2 | 780 | fclose(file); |
ae99fb2c ACM |
781 | if (n < 1) { |
782 | pr_err("cannot store event ID\n"); | |
783 | return -EINVAL; | |
784 | } | |
785 | return perf_header__push_event(id, orgname); | |
8755a8f2 AV |
786 | } |
787 | ||
f37a291c | 788 | int parse_events(const struct option *opt __used, const char *str, int unset __used) |
8ad8db37 | 789 | { |
cdd6c482 | 790 | struct perf_event_attr attr; |
bcd3279f | 791 | enum event_result ret; |
8ad8db37 | 792 | |
8755a8f2 | 793 | if (strchr(str, ':')) |
ae99fb2c ACM |
794 | if (store_event_type(str) < 0) |
795 | return -1; | |
8755a8f2 | 796 | |
61c45981 PM |
797 | for (;;) { |
798 | if (nr_counters == MAX_COUNTERS) | |
799 | return -1; | |
800 | ||
801 | memset(&attr, 0, sizeof(attr)); | |
bcd3279f FW |
802 | ret = parse_event_symbols(&str, &attr); |
803 | if (ret == EVT_FAILED) | |
61c45981 | 804 | return -1; |
8ad8db37 | 805 | |
61c45981 PM |
806 | if (!(*str == 0 || *str == ',' || isspace(*str))) |
807 | return -1; | |
8ad8db37 | 808 | |
bcd3279f FW |
809 | if (ret != EVT_HANDLED_ALL) { |
810 | attrs[nr_counters] = attr; | |
811 | nr_counters++; | |
812 | } | |
8ad8db37 | 813 | |
61c45981 PM |
814 | if (*str == 0) |
815 | break; | |
816 | if (*str == ',') | |
817 | ++str; | |
818 | while (isspace(*str)) | |
819 | ++str; | |
8ad8db37 IM |
820 | } |
821 | ||
822 | return 0; | |
823 | } | |
824 | ||
c171b552 LZ |
825 | int parse_filter(const struct option *opt __used, const char *str, |
826 | int unset __used) | |
827 | { | |
828 | int i = nr_counters - 1; | |
829 | int len = strlen(str); | |
830 | ||
831 | if (i < 0 || attrs[i].type != PERF_TYPE_TRACEPOINT) { | |
832 | fprintf(stderr, | |
833 | "-F option should follow a -e tracepoint option\n"); | |
834 | return -1; | |
835 | } | |
836 | ||
837 | filters[i] = malloc(len + 1); | |
838 | if (!filters[i]) { | |
839 | fprintf(stderr, "not enough memory to hold filter string\n"); | |
840 | return -1; | |
841 | } | |
842 | strcpy(filters[i], str); | |
843 | ||
844 | return 0; | |
845 | } | |
846 | ||
86847b62 | 847 | static const char * const event_type_descriptors[] = { |
86847b62 TG |
848 | "Hardware event", |
849 | "Software event", | |
850 | "Tracepoint event", | |
851 | "Hardware cache event", | |
41bdcb23 LW |
852 | "Raw hardware event descriptor", |
853 | "Hardware breakpoint", | |
86847b62 TG |
854 | }; |
855 | ||
f6bdafef JB |
856 | /* |
857 | * Print the events from <debugfs_mount_point>/tracing/events | |
858 | */ | |
859 | ||
860 | static void print_tracepoint_events(void) | |
861 | { | |
862 | DIR *sys_dir, *evt_dir; | |
863 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; | |
f6bdafef | 864 | char evt_path[MAXPATHLEN]; |
725b1368 | 865 | char dir_path[MAXPATHLEN]; |
f6bdafef | 866 | |
549104f2 | 867 | if (debugfs_valid_mountpoint(debugfs_path)) |
f6bdafef JB |
868 | return; |
869 | ||
5beeded1 | 870 | sys_dir = opendir(debugfs_path); |
f6bdafef | 871 | if (!sys_dir) |
725b1368 | 872 | return; |
6b58e7f1 UD |
873 | |
874 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { | |
725b1368 ED |
875 | |
876 | snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path, | |
877 | sys_dirent.d_name); | |
878 | evt_dir = opendir(dir_path); | |
879 | if (!evt_dir) | |
6b58e7f1 | 880 | continue; |
725b1368 | 881 | |
6b58e7f1 | 882 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
f6bdafef JB |
883 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
884 | sys_dirent.d_name, evt_dirent.d_name); | |
689d3018 | 885 | printf(" %-42s [%s]\n", evt_path, |
41bdcb23 | 886 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); |
f6bdafef JB |
887 | } |
888 | closedir(evt_dir); | |
889 | } | |
f6bdafef JB |
890 | closedir(sys_dir); |
891 | } | |
892 | ||
8ad8db37 | 893 | /* |
86847b62 | 894 | * Print the help text for the event symbols: |
8ad8db37 | 895 | */ |
86847b62 | 896 | void print_events(void) |
8ad8db37 | 897 | { |
86847b62 | 898 | struct event_symbol *syms = event_symbols; |
73c24cb8 | 899 | unsigned int i, type, op, prev_type = -1; |
74d5b588 | 900 | char name[40]; |
8ad8db37 | 901 | |
689d3018 MR |
902 | printf("\n"); |
903 | printf("List of pre-defined events (to be used in -e):\n"); | |
8ad8db37 | 904 | |
86847b62 | 905 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) { |
41bdcb23 | 906 | type = syms->type; |
8ad8db37 | 907 | |
86847b62 | 908 | if (type != prev_type) |
689d3018 | 909 | printf("\n"); |
8ad8db37 | 910 | |
74d5b588 JSR |
911 | if (strlen(syms->alias)) |
912 | sprintf(name, "%s OR %s", syms->symbol, syms->alias); | |
913 | else | |
914 | strcpy(name, syms->symbol); | |
689d3018 | 915 | printf(" %-42s [%s]\n", name, |
86847b62 | 916 | event_type_descriptors[type]); |
8ad8db37 | 917 | |
86847b62 | 918 | prev_type = type; |
8ad8db37 IM |
919 | } |
920 | ||
689d3018 | 921 | printf("\n"); |
73c24cb8 JSR |
922 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { |
923 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { | |
924 | /* skip invalid cache type */ | |
925 | if (!is_cache_op_valid(type, op)) | |
926 | continue; | |
927 | ||
928 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { | |
689d3018 | 929 | printf(" %-42s [%s]\n", |
73c24cb8 | 930 | event_cache_name(type, op, i), |
41bdcb23 | 931 | event_type_descriptors[PERF_TYPE_HW_CACHE]); |
73c24cb8 JSR |
932 | } |
933 | } | |
934 | } | |
935 | ||
689d3018 | 936 | printf("\n"); |
41bdcb23 LW |
937 | printf(" %-42s [%s]\n", |
938 | "rNNN", event_type_descriptors[PERF_TYPE_RAW]); | |
689d3018 | 939 | printf("\n"); |
86847b62 | 940 | |
41bdcb23 LW |
941 | printf(" %-42s [%s]\n", |
942 | "mem:<addr>[:access]", | |
943 | event_type_descriptors[PERF_TYPE_BREAKPOINT]); | |
1b290d67 FW |
944 | printf("\n"); |
945 | ||
f6bdafef JB |
946 | print_tracepoint_events(); |
947 | ||
86847b62 | 948 | exit(129); |
8ad8db37 | 949 | } |