blob: 5a3cd3a34af1176c66f7303c194eb1c6af4c9851 [file] [log] [blame]
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001
2#include "../perf.h"
3#include "util.h"
4#include "parse-options.h"
5#include "parse-events.h"
6#include "exec_cmd.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03007#include "string.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02008
Ingo Molnar8326f442009-06-05 20:22:46 +02009extern char *strcasestr(const char *haystack, const char *needle);
10
Ingo Molnara21ca2c2009-06-06 09:58:57 +020011int nr_counters;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020012
Ingo Molnara21ca2c2009-06-06 09:58:57 +020013struct perf_counter_attr attrs[MAX_COUNTERS];
Ingo Molnar8ad8db32009-05-26 11:10:09 +020014
Jason Baronf6bdafe2009-07-21 12:20:22 -040015static char default_debugfs_path[] = "/sys/kernel/debug/tracing/events";
16
Ingo Molnar8ad8db32009-05-26 11:10:09 +020017struct event_symbol {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100018 u8 type;
19 u64 config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020020 char *symbol;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053021 char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020022};
23
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053024#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
25#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020026
Ingo Molnara21ca2c2009-06-06 09:58:57 +020027static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053028 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
29 { CHW(INSTRUCTIONS), "instructions", "" },
30 { CHW(CACHE_REFERENCES), "cache-references", "" },
31 { CHW(CACHE_MISSES), "cache-misses", "" },
32 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
33 { CHW(BRANCH_MISSES), "branch-misses", "" },
34 { CHW(BUS_CYCLES), "bus-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020035
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053036 { CSW(CPU_CLOCK), "cpu-clock", "" },
37 { CSW(TASK_CLOCK), "task-clock", "" },
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053038 { CSW(PAGE_FAULTS), "page-faults", "faults" },
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053039 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
40 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
41 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
42 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020043};
44
Ingo Molnar52425192009-05-26 09:17:18 +020045#define __PERF_COUNTER_FIELD(config, name) \
46 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
47
48#define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
49#define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
50#define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
51#define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
52
53static char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020054 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020055 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020056 "cache-references",
57 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020058 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020059 "branch-misses",
60 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020061};
62
63static char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020064 "cpu-clock-msecs",
65 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020066 "page-faults",
67 "context-switches",
68 "CPU-migrations",
69 "minor-faults",
70 "major-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020071};
72
Ingo Molnar8326f442009-06-05 20:22:46 +020073#define MAX_ALIASES 8
74
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053075static char *hw_cache[][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100076 { "L1-dcache", "l1-d", "l1d", "L1-data", },
77 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053078 { "LLC", "L2" },
79 { "dTLB", "d-tlb", "Data-TLB", },
80 { "iTLB", "i-tlb", "Instruction-TLB", },
81 { "branch", "branches", "bpu", "btb", "bpc", },
Ingo Molnar8326f442009-06-05 20:22:46 +020082};
83
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053084static char *hw_cache_op[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053085 { "load", "loads", "read", },
86 { "store", "stores", "write", },
87 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +020088};
89
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053090static char *hw_cache_result[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053091 { "refs", "Reference", "ops", "access", },
92 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +020093};
94
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +053095#define C(x) PERF_COUNT_HW_CACHE_##x
96#define CACHE_READ (1 << C(OP_READ))
97#define CACHE_WRITE (1 << C(OP_WRITE))
98#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
99#define COP(x) (1 << x)
100
101/*
102 * cache operartion stat
103 * L1I : Read and prefetch only
104 * ITLB and BPU : Read-only
105 */
106static unsigned long hw_cache_stat[C(MAX)] = {
107 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
108 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
109 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
110 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
111 [C(ITLB)] = (CACHE_READ),
112 [C(BPU)] = (CACHE_READ),
113};
114
Jason Baronf6bdafe2009-07-21 12:20:22 -0400115#define for_each_subsystem(sys_dir, sys_dirent, sys_next, file, st) \
116 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
117 if (snprintf(file, MAXPATHLEN, "%s/%s", default_debugfs_path, \
118 sys_dirent.d_name) && \
119 (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \
120 (strcmp(sys_dirent.d_name, ".")) && \
121 (strcmp(sys_dirent.d_name, "..")))
122
123#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \
124 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
125 if (snprintf(file, MAXPATHLEN, "%s/%s/%s", default_debugfs_path, \
126 sys_dirent.d_name, evt_dirent.d_name) && \
127 (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \
128 (strcmp(evt_dirent.d_name, ".")) && \
129 (strcmp(evt_dirent.d_name, "..")))
130
131#define MAX_EVENT_LENGTH 30
132
133static int valid_debugfs_mount(void)
134{
135 struct statfs st_fs;
136
137 if (statfs(default_debugfs_path, &st_fs) < 0)
138 return -ENOENT;
139 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
140 return -ENOENT;
141 return 0;
142}
143
144static char *tracepoint_id_to_name(u64 config)
145{
146 static char tracepoint_name[2 * MAX_EVENT_LENGTH];
147 DIR *sys_dir, *evt_dir;
148 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
149 struct stat st;
150 char id_buf[4];
151 int fd;
152 u64 id;
153 char evt_path[MAXPATHLEN];
154
155 if (valid_debugfs_mount())
156 return "unkown";
157
158 sys_dir = opendir(default_debugfs_path);
159 if (!sys_dir)
160 goto cleanup;
161
162 for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
163 evt_dir = opendir(evt_path);
164 if (!evt_dir)
165 goto cleanup;
166 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
167 evt_path, st) {
168 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id",
169 default_debugfs_path, sys_dirent.d_name,
170 evt_dirent.d_name);
171 fd = open(evt_path, O_RDONLY);
172 if (fd < 0)
173 continue;
174 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
175 close(fd);
176 continue;
177 }
178 close(fd);
179 id = atoll(id_buf);
180 if (id == config) {
181 closedir(evt_dir);
182 closedir(sys_dir);
183 snprintf(tracepoint_name, 2 * MAX_EVENT_LENGTH,
184 "%s:%s", sys_dirent.d_name,
185 evt_dirent.d_name);
186 return tracepoint_name;
187 }
188 }
189 closedir(evt_dir);
190 }
191
192cleanup:
193 closedir(sys_dir);
194 return "unkown";
195}
196
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530197static int is_cache_op_valid(u8 cache_type, u8 cache_op)
198{
199 if (hw_cache_stat[cache_type] & COP(cache_op))
200 return 1; /* valid */
201 else
202 return 0; /* invalid */
203}
204
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530205static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
206{
207 static char name[50];
208
209 if (cache_result) {
210 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
211 hw_cache_op[cache_op][0],
212 hw_cache_result[cache_result][0]);
213 } else {
214 sprintf(name, "%s-%s", hw_cache[cache_type][0],
215 hw_cache_op[cache_op][1]);
216 }
217
218 return name;
219}
220
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200221char *event_name(int counter)
Ingo Molnar52425192009-05-26 09:17:18 +0200222{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000223 u64 config = attrs[counter].config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200224 int type = attrs[counter].type;
Ingo Molnar52425192009-05-26 09:17:18 +0200225 static char buf[32];
226
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200227 if (attrs[counter].type == PERF_TYPE_RAW) {
228 sprintf(buf, "raw 0x%llx", config);
Ingo Molnar52425192009-05-26 09:17:18 +0200229 return buf;
230 }
231
232 switch (type) {
233 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200234 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200235 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200236 return "unknown-hardware";
237
Ingo Molnar8326f442009-06-05 20:22:46 +0200238 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000239 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200240
241 cache_type = (config >> 0) & 0xff;
242 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
243 return "unknown-ext-hardware-cache-type";
244
245 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200246 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
247 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200248
249 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200250 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
251 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200252
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530253 if (!is_cache_op_valid(cache_type, cache_op))
254 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200255
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530256 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200257 }
258
Ingo Molnar52425192009-05-26 09:17:18 +0200259 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200260 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200261 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200262 return "unknown-software";
263
Jason Baronf6bdafe2009-07-21 12:20:22 -0400264 case PERF_TYPE_TRACEPOINT:
265 return tracepoint_id_to_name(config);
266
Ingo Molnar52425192009-05-26 09:17:18 +0200267 default:
268 break;
269 }
270
271 return "unknown";
272}
273
Paul Mackerras61c45982009-07-01 13:04:34 +1000274static int parse_aliases(const char **str, char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200275{
276 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000277 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200278
279 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000280 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
281 n = strlen(names[i][j]);
282 if (n > longest && !strncasecmp(*str, names[i][j], n))
283 longest = n;
284 }
285 if (longest > 0) {
286 *str += longest;
287 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200288 }
289 }
290
Ingo Molnar89536452009-06-06 21:04:17 +0200291 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200292}
293
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +0530294static int
Paul Mackerras61c45982009-07-01 13:04:34 +1000295parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200296{
Paul Mackerras61c45982009-07-01 13:04:34 +1000297 const char *s = *str;
298 int cache_type = -1, cache_op = -1, cache_result = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200299
Paul Mackerras61c45982009-07-01 13:04:34 +1000300 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200301 /*
302 * No fallback - if we cannot get a clear cache type
303 * then bail out:
304 */
305 if (cache_type == -1)
Paul Mackerras61c45982009-07-01 13:04:34 +1000306 return 0;
Ingo Molnar8326f442009-06-05 20:22:46 +0200307
Paul Mackerras61c45982009-07-01 13:04:34 +1000308 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
309 ++s;
310
311 if (cache_op == -1) {
312 cache_op = parse_aliases(&s, hw_cache_op,
313 PERF_COUNT_HW_CACHE_OP_MAX);
314 if (cache_op >= 0) {
315 if (!is_cache_op_valid(cache_type, cache_op))
316 return 0;
317 continue;
318 }
319 }
320
321 if (cache_result == -1) {
322 cache_result = parse_aliases(&s, hw_cache_result,
323 PERF_COUNT_HW_CACHE_RESULT_MAX);
324 if (cache_result >= 0)
325 continue;
326 }
327
328 /*
329 * Can't parse this as a cache op or result, so back up
330 * to the '-'.
331 */
332 --s;
333 break;
334 }
335
Ingo Molnar8326f442009-06-05 20:22:46 +0200336 /*
337 * Fall back to reads:
338 */
Ingo Molnar89536452009-06-06 21:04:17 +0200339 if (cache_op == -1)
340 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200341
Ingo Molnar8326f442009-06-05 20:22:46 +0200342 /*
343 * Fall back to accesses:
344 */
345 if (cache_result == -1)
346 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
347
348 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
349 attr->type = PERF_TYPE_HW_CACHE;
350
Paul Mackerras61c45982009-07-01 13:04:34 +1000351 *str = s;
352 return 1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200353}
354
Jason Baronf6bdafe2009-07-21 12:20:22 -0400355static int parse_tracepoint_event(const char **strp,
356 struct perf_counter_attr *attr)
357{
358 const char *evt_name;
359 char sys_name[MAX_EVENT_LENGTH];
360 char id_buf[4];
361 int fd;
362 unsigned int sys_length, evt_length;
363 u64 id;
364 char evt_path[MAXPATHLEN];
365
366 if (valid_debugfs_mount())
367 return 0;
368
369 evt_name = strchr(*strp, ':');
370 if (!evt_name)
371 return 0;
372
373 sys_length = evt_name - *strp;
374 if (sys_length >= MAX_EVENT_LENGTH)
375 return 0;
376
377 strncpy(sys_name, *strp, sys_length);
378 sys_name[sys_length] = '\0';
379 evt_name = evt_name + 1;
380 evt_length = strlen(evt_name);
381 if (evt_length >= MAX_EVENT_LENGTH)
382 return 0;
383
384 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", default_debugfs_path,
385 sys_name, evt_name);
386 fd = open(evt_path, O_RDONLY);
387 if (fd < 0)
388 return 0;
389
390 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
391 close(fd);
392 return 0;
393 }
394 close(fd);
395 id = atoll(id_buf);
396 attr->config = id;
397 attr->type = PERF_TYPE_TRACEPOINT;
398 *strp = evt_name + evt_length;
399 return 1;
400}
401
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530402static int check_events(const char *str, unsigned int i)
403{
Paul Mackerras61c45982009-07-01 13:04:34 +1000404 int n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530405
Paul Mackerras61c45982009-07-01 13:04:34 +1000406 n = strlen(event_symbols[i].symbol);
407 if (!strncmp(str, event_symbols[i].symbol, n))
408 return n;
409
410 n = strlen(event_symbols[i].alias);
411 if (n)
412 if (!strncmp(str, event_symbols[i].alias, n))
413 return n;
414 return 0;
415}
416
417static int
418parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
419{
420 const char *str = *strp;
421 unsigned int i;
422 int n;
423
424 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
425 n = check_events(str, i);
426 if (n > 0) {
427 attr->type = event_symbols[i].type;
428 attr->config = event_symbols[i].config;
429 *strp = str + n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530430 return 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000431 }
432 }
433 return 0;
434}
435
436static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
437{
438 const char *str = *strp;
439 u64 config;
440 int n;
441
442 if (*str != 'r')
443 return 0;
444 n = hex2u64(str + 1, &config);
445 if (n > 0) {
446 *strp = str + n + 1;
447 attr->type = PERF_TYPE_RAW;
448 attr->config = config;
449 return 1;
450 }
451 return 0;
452}
453
454static int
455parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
456{
457 const char *str = *strp;
458 char *endp;
459 unsigned long type;
460 u64 config;
461
462 type = strtoul(str, &endp, 0);
463 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
464 str = endp + 1;
465 config = strtoul(str, &endp, 0);
466 if (endp > str) {
467 attr->type = type;
468 attr->config = config;
469 *strp = endp;
470 return 1;
471 }
472 }
473 return 0;
474}
475
476static int
477parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
478{
479 const char *str = *strp;
480 int eu = 1, ek = 1, eh = 1;
481
482 if (*str++ != ':')
483 return 0;
484 while (*str) {
485 if (*str == 'u')
486 eu = 0;
487 else if (*str == 'k')
488 ek = 0;
489 else if (*str == 'h')
490 eh = 0;
491 else
492 break;
493 ++str;
494 }
495 if (str >= *strp + 2) {
496 *strp = str;
497 attr->exclude_user = eu;
498 attr->exclude_kernel = ek;
499 attr->exclude_hv = eh;
500 return 1;
501 }
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530502 return 0;
503}
504
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200505/*
506 * Each event can have multiple symbolic names.
507 * Symbolic names are (almost) exactly matched.
508 */
Paul Mackerras61c45982009-07-01 13:04:34 +1000509static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200510{
Jason Baronf6bdafe2009-07-21 12:20:22 -0400511 if (!(parse_tracepoint_event(str, attr) ||
512 parse_raw_event(str, attr) ||
Paul Mackerras61c45982009-07-01 13:04:34 +1000513 parse_numeric_event(str, attr) ||
514 parse_symbolic_event(str, attr) ||
515 parse_generic_hw_event(str, attr)))
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200516 return 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200517
Paul Mackerras61c45982009-07-01 13:04:34 +1000518 parse_event_modifier(str, attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200519
Paul Mackerras61c45982009-07-01 13:04:34 +1000520 return 1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200521}
522
Ingo Molnarf37a2912009-07-01 12:37:06 +0200523int parse_events(const struct option *opt __used, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200524{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200525 struct perf_counter_attr attr;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200526
Paul Mackerras61c45982009-07-01 13:04:34 +1000527 for (;;) {
528 if (nr_counters == MAX_COUNTERS)
529 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200530
Paul Mackerras61c45982009-07-01 13:04:34 +1000531 memset(&attr, 0, sizeof(attr));
532 if (!parse_event_symbols(&str, &attr))
533 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200534
Paul Mackerras61c45982009-07-01 13:04:34 +1000535 if (!(*str == 0 || *str == ',' || isspace(*str)))
536 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200537
Paul Mackerras61c45982009-07-01 13:04:34 +1000538 attrs[nr_counters] = attr;
539 nr_counters++;
540
541 if (*str == 0)
542 break;
543 if (*str == ',')
544 ++str;
545 while (isspace(*str))
546 ++str;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200547 }
548
549 return 0;
550}
551
Thomas Gleixner86847b62009-06-06 12:24:17 +0200552static const char * const event_type_descriptors[] = {
553 "",
554 "Hardware event",
555 "Software event",
556 "Tracepoint event",
557 "Hardware cache event",
558};
559
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200560/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400561 * Print the events from <debugfs_mount_point>/tracing/events
562 */
563
564static void print_tracepoint_events(void)
565{
566 DIR *sys_dir, *evt_dir;
567 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
568 struct stat st;
569 char evt_path[MAXPATHLEN];
570
571 if (valid_debugfs_mount())
572 return;
573
574 sys_dir = opendir(default_debugfs_path);
575 if (!sys_dir)
576 goto cleanup;
577
578 for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
579 evt_dir = opendir(evt_path);
580 if (!evt_dir)
581 goto cleanup;
582 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
583 evt_path, st) {
584 snprintf(evt_path, MAXPATHLEN, "%s:%s",
585 sys_dirent.d_name, evt_dirent.d_name);
586 fprintf(stderr, " %-40s [%s]\n", evt_path,
587 event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
588 }
589 closedir(evt_dir);
590 }
591
592cleanup:
593 closedir(sys_dir);
594}
595
596/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200597 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200598 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200599void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200600{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200601 struct event_symbol *syms = event_symbols;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530602 unsigned int i, type, op, prev_type = -1;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530603 char name[40];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200604
Thomas Gleixner86847b62009-06-06 12:24:17 +0200605 fprintf(stderr, "\n");
606 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200607
Thomas Gleixner86847b62009-06-06 12:24:17 +0200608 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
609 type = syms->type + 1;
Roel Kluin23cdb5d2009-07-13 02:25:47 +0200610 if (type >= ARRAY_SIZE(event_type_descriptors))
Thomas Gleixner86847b62009-06-06 12:24:17 +0200611 type = 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200612
Thomas Gleixner86847b62009-06-06 12:24:17 +0200613 if (type != prev_type)
614 fprintf(stderr, "\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200615
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530616 if (strlen(syms->alias))
617 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
618 else
619 strcpy(name, syms->symbol);
620 fprintf(stderr, " %-40s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +0200621 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200622
Thomas Gleixner86847b62009-06-06 12:24:17 +0200623 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200624 }
625
Thomas Gleixner86847b62009-06-06 12:24:17 +0200626 fprintf(stderr, "\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530627 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
628 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
629 /* skip invalid cache type */
630 if (!is_cache_op_valid(type, op))
631 continue;
632
633 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
634 fprintf(stderr, " %-40s [%s]\n",
635 event_cache_name(type, op, i),
636 event_type_descriptors[4]);
637 }
638 }
639 }
640
641 fprintf(stderr, "\n");
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530642 fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200643 "rNNN");
644 fprintf(stderr, "\n");
645
Jason Baronf6bdafe2009-07-21 12:20:22 -0400646 print_tracepoint_events();
647
Thomas Gleixner86847b62009-06-06 12:24:17 +0200648 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200649}