blob: f77407b5832e94011a060f79007c2362d26c933c [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"
Jason Baron5beeded2009-07-21 14:16:29 -04008#include "cache.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02009
Ingo Molnar8326f442009-06-05 20:22:46 +020010extern char *strcasestr(const char *haystack, const char *needle);
11
Ingo Molnara21ca2c2009-06-06 09:58:57 +020012int nr_counters;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020013
Ingo Molnara21ca2c2009-06-06 09:58:57 +020014struct perf_counter_attr attrs[MAX_COUNTERS];
Ingo Molnar8ad8db32009-05-26 11:10:09 +020015
16struct event_symbol {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100017 u8 type;
18 u64 config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +020019 char *symbol;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053020 char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020021};
22
Jason Baron5beeded2009-07-21 14:16:29 -040023char debugfs_path[MAXPATHLEN];
24
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053025#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
26#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020027
Ingo Molnara21ca2c2009-06-06 09:58:57 +020028static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053029 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
30 { CHW(INSTRUCTIONS), "instructions", "" },
31 { CHW(CACHE_REFERENCES), "cache-references", "" },
32 { CHW(CACHE_MISSES), "cache-misses", "" },
33 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
34 { CHW(BRANCH_MISSES), "branch-misses", "" },
35 { CHW(BUS_CYCLES), "bus-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020036
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053037 { CSW(CPU_CLOCK), "cpu-clock", "" },
38 { CSW(TASK_CLOCK), "task-clock", "" },
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053039 { CSW(PAGE_FAULTS), "page-faults", "faults" },
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053040 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
41 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
42 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
43 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020044};
45
Ingo Molnar52425192009-05-26 09:17:18 +020046#define __PERF_COUNTER_FIELD(config, name) \
47 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
48
49#define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
50#define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
51#define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
52#define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
53
54static char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020055 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020056 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020057 "cache-references",
58 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020059 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020060 "branch-misses",
61 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020062};
63
64static char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020065 "cpu-clock-msecs",
66 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020067 "page-faults",
68 "context-switches",
69 "CPU-migrations",
70 "minor-faults",
71 "major-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020072};
73
Ingo Molnar8326f442009-06-05 20:22:46 +020074#define MAX_ALIASES 8
75
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053076static char *hw_cache[][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100077 { "L1-dcache", "l1-d", "l1d", "L1-data", },
78 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053079 { "LLC", "L2" },
80 { "dTLB", "d-tlb", "Data-TLB", },
81 { "iTLB", "i-tlb", "Instruction-TLB", },
82 { "branch", "branches", "bpu", "btb", "bpc", },
Ingo Molnar8326f442009-06-05 20:22:46 +020083};
84
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053085static char *hw_cache_op[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053086 { "load", "loads", "read", },
87 { "store", "stores", "write", },
88 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +020089};
90
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053091static char *hw_cache_result[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053092 { "refs", "Reference", "ops", "access", },
93 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +020094};
95
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +053096#define C(x) PERF_COUNT_HW_CACHE_##x
97#define CACHE_READ (1 << C(OP_READ))
98#define CACHE_WRITE (1 << C(OP_WRITE))
99#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
100#define COP(x) (1 << x)
101
102/*
103 * cache operartion stat
104 * L1I : Read and prefetch only
105 * ITLB and BPU : Read-only
106 */
107static unsigned long hw_cache_stat[C(MAX)] = {
108 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
109 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
110 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
111 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
112 [C(ITLB)] = (CACHE_READ),
113 [C(BPU)] = (CACHE_READ),
114};
115
Jason Baronf6bdafe2009-07-21 12:20:22 -0400116#define for_each_subsystem(sys_dir, sys_dirent, sys_next, file, st) \
117 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Jason Baron5beeded2009-07-21 14:16:29 -0400118 if (snprintf(file, MAXPATHLEN, "%s/%s", debugfs_path, \
119 sys_dirent.d_name) && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400120 (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \
121 (strcmp(sys_dirent.d_name, ".")) && \
122 (strcmp(sys_dirent.d_name, "..")))
123
124#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \
125 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Jason Baron5beeded2009-07-21 14:16:29 -0400126 if (snprintf(file, MAXPATHLEN, "%s/%s/%s", debugfs_path, \
127 sys_dirent.d_name, evt_dirent.d_name) && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400128 (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \
129 (strcmp(evt_dirent.d_name, ".")) && \
130 (strcmp(evt_dirent.d_name, "..")))
131
132#define MAX_EVENT_LENGTH 30
133
Jason Baron5beeded2009-07-21 14:16:29 -0400134int valid_debugfs_mount(const char *debugfs)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400135{
136 struct statfs st_fs;
137
Jason Baron5beeded2009-07-21 14:16:29 -0400138 if (statfs(debugfs, &st_fs) < 0)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400139 return -ENOENT;
140 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
141 return -ENOENT;
142 return 0;
143}
144
145static char *tracepoint_id_to_name(u64 config)
146{
147 static char tracepoint_name[2 * MAX_EVENT_LENGTH];
148 DIR *sys_dir, *evt_dir;
149 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
150 struct stat st;
151 char id_buf[4];
152 int fd;
153 u64 id;
154 char evt_path[MAXPATHLEN];
155
Jason Baron5beeded2009-07-21 14:16:29 -0400156 if (valid_debugfs_mount(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400157 return "unkown";
158
Jason Baron5beeded2009-07-21 14:16:29 -0400159 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160 if (!sys_dir)
161 goto cleanup;
162
163 for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
164 evt_dir = opendir(evt_path);
165 if (!evt_dir)
166 goto cleanup;
167 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
168 evt_path, st) {
169 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id",
Jason Baron5beeded2009-07-21 14:16:29 -0400170 debugfs_path, sys_dirent.d_name,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400171 evt_dirent.d_name);
172 fd = open(evt_path, O_RDONLY);
173 if (fd < 0)
174 continue;
175 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
176 close(fd);
177 continue;
178 }
179 close(fd);
180 id = atoll(id_buf);
181 if (id == config) {
182 closedir(evt_dir);
183 closedir(sys_dir);
184 snprintf(tracepoint_name, 2 * MAX_EVENT_LENGTH,
185 "%s:%s", sys_dirent.d_name,
186 evt_dirent.d_name);
187 return tracepoint_name;
188 }
189 }
190 closedir(evt_dir);
191 }
192
193cleanup:
194 closedir(sys_dir);
195 return "unkown";
196}
197
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530198static int is_cache_op_valid(u8 cache_type, u8 cache_op)
199{
200 if (hw_cache_stat[cache_type] & COP(cache_op))
201 return 1; /* valid */
202 else
203 return 0; /* invalid */
204}
205
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530206static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
207{
208 static char name[50];
209
210 if (cache_result) {
211 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
212 hw_cache_op[cache_op][0],
213 hw_cache_result[cache_result][0]);
214 } else {
215 sprintf(name, "%s-%s", hw_cache[cache_type][0],
216 hw_cache_op[cache_op][1]);
217 }
218
219 return name;
220}
221
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200222char *event_name(int counter)
Ingo Molnar52425192009-05-26 09:17:18 +0200223{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000224 u64 config = attrs[counter].config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200225 int type = attrs[counter].type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200226
227 return __event_name(type, config);
228}
229
230char *__event_name(int type, u64 config)
231{
Ingo Molnar52425192009-05-26 09:17:18 +0200232 static char buf[32];
233
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200234 if (type == PERF_TYPE_RAW) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200235 sprintf(buf, "raw 0x%llx", config);
Ingo Molnar52425192009-05-26 09:17:18 +0200236 return buf;
237 }
238
239 switch (type) {
240 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200241 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200242 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200243 return "unknown-hardware";
244
Ingo Molnar8326f442009-06-05 20:22:46 +0200245 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000246 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200247
248 cache_type = (config >> 0) & 0xff;
249 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
250 return "unknown-ext-hardware-cache-type";
251
252 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200253 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
254 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200255
256 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200257 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
258 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200259
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530260 if (!is_cache_op_valid(cache_type, cache_op))
261 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200262
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530263 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200264 }
265
Ingo Molnar52425192009-05-26 09:17:18 +0200266 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200267 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200268 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200269 return "unknown-software";
270
Jason Baronf6bdafe2009-07-21 12:20:22 -0400271 case PERF_TYPE_TRACEPOINT:
272 return tracepoint_id_to_name(config);
273
Ingo Molnar52425192009-05-26 09:17:18 +0200274 default:
275 break;
276 }
277
278 return "unknown";
279}
280
Paul Mackerras61c45982009-07-01 13:04:34 +1000281static int parse_aliases(const char **str, char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200282{
283 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000284 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200285
286 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000287 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
288 n = strlen(names[i][j]);
289 if (n > longest && !strncasecmp(*str, names[i][j], n))
290 longest = n;
291 }
292 if (longest > 0) {
293 *str += longest;
294 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200295 }
296 }
297
Ingo Molnar89536452009-06-06 21:04:17 +0200298 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200299}
300
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +0530301static int
Paul Mackerras61c45982009-07-01 13:04:34 +1000302parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200303{
Paul Mackerras61c45982009-07-01 13:04:34 +1000304 const char *s = *str;
305 int cache_type = -1, cache_op = -1, cache_result = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200306
Paul Mackerras61c45982009-07-01 13:04:34 +1000307 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200308 /*
309 * No fallback - if we cannot get a clear cache type
310 * then bail out:
311 */
312 if (cache_type == -1)
Paul Mackerras61c45982009-07-01 13:04:34 +1000313 return 0;
Ingo Molnar8326f442009-06-05 20:22:46 +0200314
Paul Mackerras61c45982009-07-01 13:04:34 +1000315 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
316 ++s;
317
318 if (cache_op == -1) {
319 cache_op = parse_aliases(&s, hw_cache_op,
320 PERF_COUNT_HW_CACHE_OP_MAX);
321 if (cache_op >= 0) {
322 if (!is_cache_op_valid(cache_type, cache_op))
323 return 0;
324 continue;
325 }
326 }
327
328 if (cache_result == -1) {
329 cache_result = parse_aliases(&s, hw_cache_result,
330 PERF_COUNT_HW_CACHE_RESULT_MAX);
331 if (cache_result >= 0)
332 continue;
333 }
334
335 /*
336 * Can't parse this as a cache op or result, so back up
337 * to the '-'.
338 */
339 --s;
340 break;
341 }
342
Ingo Molnar8326f442009-06-05 20:22:46 +0200343 /*
344 * Fall back to reads:
345 */
Ingo Molnar89536452009-06-06 21:04:17 +0200346 if (cache_op == -1)
347 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200348
Ingo Molnar8326f442009-06-05 20:22:46 +0200349 /*
350 * Fall back to accesses:
351 */
352 if (cache_result == -1)
353 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
354
355 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
356 attr->type = PERF_TYPE_HW_CACHE;
357
Paul Mackerras61c45982009-07-01 13:04:34 +1000358 *str = s;
359 return 1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200360}
361
Jason Baronf6bdafe2009-07-21 12:20:22 -0400362static int parse_tracepoint_event(const char **strp,
363 struct perf_counter_attr *attr)
364{
365 const char *evt_name;
366 char sys_name[MAX_EVENT_LENGTH];
367 char id_buf[4];
368 int fd;
369 unsigned int sys_length, evt_length;
370 u64 id;
371 char evt_path[MAXPATHLEN];
372
Jason Baron5beeded2009-07-21 14:16:29 -0400373 if (valid_debugfs_mount(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400374 return 0;
375
376 evt_name = strchr(*strp, ':');
377 if (!evt_name)
378 return 0;
379
380 sys_length = evt_name - *strp;
381 if (sys_length >= MAX_EVENT_LENGTH)
382 return 0;
383
384 strncpy(sys_name, *strp, sys_length);
385 sys_name[sys_length] = '\0';
386 evt_name = evt_name + 1;
387 evt_length = strlen(evt_name);
388 if (evt_length >= MAX_EVENT_LENGTH)
389 return 0;
390
Jason Baron5beeded2009-07-21 14:16:29 -0400391 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
392 sys_name, evt_name);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400393 fd = open(evt_path, O_RDONLY);
394 if (fd < 0)
395 return 0;
396
397 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
398 close(fd);
399 return 0;
400 }
401 close(fd);
402 id = atoll(id_buf);
403 attr->config = id;
404 attr->type = PERF_TYPE_TRACEPOINT;
405 *strp = evt_name + evt_length;
406 return 1;
407}
408
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530409static int check_events(const char *str, unsigned int i)
410{
Paul Mackerras61c45982009-07-01 13:04:34 +1000411 int n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530412
Paul Mackerras61c45982009-07-01 13:04:34 +1000413 n = strlen(event_symbols[i].symbol);
414 if (!strncmp(str, event_symbols[i].symbol, n))
415 return n;
416
417 n = strlen(event_symbols[i].alias);
418 if (n)
419 if (!strncmp(str, event_symbols[i].alias, n))
420 return n;
421 return 0;
422}
423
424static int
425parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
426{
427 const char *str = *strp;
428 unsigned int i;
429 int n;
430
431 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
432 n = check_events(str, i);
433 if (n > 0) {
434 attr->type = event_symbols[i].type;
435 attr->config = event_symbols[i].config;
436 *strp = str + n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530437 return 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000438 }
439 }
440 return 0;
441}
442
443static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
444{
445 const char *str = *strp;
446 u64 config;
447 int n;
448
449 if (*str != 'r')
450 return 0;
451 n = hex2u64(str + 1, &config);
452 if (n > 0) {
453 *strp = str + n + 1;
454 attr->type = PERF_TYPE_RAW;
455 attr->config = config;
456 return 1;
457 }
458 return 0;
459}
460
461static int
462parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
463{
464 const char *str = *strp;
465 char *endp;
466 unsigned long type;
467 u64 config;
468
469 type = strtoul(str, &endp, 0);
470 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
471 str = endp + 1;
472 config = strtoul(str, &endp, 0);
473 if (endp > str) {
474 attr->type = type;
475 attr->config = config;
476 *strp = endp;
477 return 1;
478 }
479 }
480 return 0;
481}
482
483static int
484parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
485{
486 const char *str = *strp;
487 int eu = 1, ek = 1, eh = 1;
488
489 if (*str++ != ':')
490 return 0;
491 while (*str) {
492 if (*str == 'u')
493 eu = 0;
494 else if (*str == 'k')
495 ek = 0;
496 else if (*str == 'h')
497 eh = 0;
498 else
499 break;
500 ++str;
501 }
502 if (str >= *strp + 2) {
503 *strp = str;
504 attr->exclude_user = eu;
505 attr->exclude_kernel = ek;
506 attr->exclude_hv = eh;
507 return 1;
508 }
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530509 return 0;
510}
511
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200512/*
513 * Each event can have multiple symbolic names.
514 * Symbolic names are (almost) exactly matched.
515 */
Paul Mackerras61c45982009-07-01 13:04:34 +1000516static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200517{
Jason Baronf6bdafe2009-07-21 12:20:22 -0400518 if (!(parse_tracepoint_event(str, attr) ||
519 parse_raw_event(str, attr) ||
Paul Mackerras61c45982009-07-01 13:04:34 +1000520 parse_numeric_event(str, attr) ||
521 parse_symbolic_event(str, attr) ||
522 parse_generic_hw_event(str, attr)))
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200523 return 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200524
Paul Mackerras61c45982009-07-01 13:04:34 +1000525 parse_event_modifier(str, attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200526
Paul Mackerras61c45982009-07-01 13:04:34 +1000527 return 1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200528}
529
Ingo Molnarf37a2912009-07-01 12:37:06 +0200530int parse_events(const struct option *opt __used, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200531{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200532 struct perf_counter_attr attr;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200533
Paul Mackerras61c45982009-07-01 13:04:34 +1000534 for (;;) {
535 if (nr_counters == MAX_COUNTERS)
536 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200537
Paul Mackerras61c45982009-07-01 13:04:34 +1000538 memset(&attr, 0, sizeof(attr));
539 if (!parse_event_symbols(&str, &attr))
540 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200541
Paul Mackerras61c45982009-07-01 13:04:34 +1000542 if (!(*str == 0 || *str == ',' || isspace(*str)))
543 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200544
Paul Mackerras61c45982009-07-01 13:04:34 +1000545 attrs[nr_counters] = attr;
546 nr_counters++;
547
548 if (*str == 0)
549 break;
550 if (*str == ',')
551 ++str;
552 while (isspace(*str))
553 ++str;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200554 }
555
556 return 0;
557}
558
Thomas Gleixner86847b62009-06-06 12:24:17 +0200559static const char * const event_type_descriptors[] = {
560 "",
561 "Hardware event",
562 "Software event",
563 "Tracepoint event",
564 "Hardware cache event",
565};
566
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200567/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400568 * Print the events from <debugfs_mount_point>/tracing/events
569 */
570
571static void print_tracepoint_events(void)
572{
573 DIR *sys_dir, *evt_dir;
574 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
575 struct stat st;
576 char evt_path[MAXPATHLEN];
577
Jason Baron5beeded2009-07-21 14:16:29 -0400578 if (valid_debugfs_mount(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400579 return;
580
Jason Baron5beeded2009-07-21 14:16:29 -0400581 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400582 if (!sys_dir)
583 goto cleanup;
584
585 for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
586 evt_dir = opendir(evt_path);
587 if (!evt_dir)
588 goto cleanup;
589 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
590 evt_path, st) {
591 snprintf(evt_path, MAXPATHLEN, "%s:%s",
592 sys_dirent.d_name, evt_dirent.d_name);
593 fprintf(stderr, " %-40s [%s]\n", evt_path,
594 event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
595 }
596 closedir(evt_dir);
597 }
598
599cleanup:
600 closedir(sys_dir);
601}
602
603/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200604 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200605 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200606void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200607{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200608 struct event_symbol *syms = event_symbols;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530609 unsigned int i, type, op, prev_type = -1;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530610 char name[40];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200611
Thomas Gleixner86847b62009-06-06 12:24:17 +0200612 fprintf(stderr, "\n");
613 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200614
Thomas Gleixner86847b62009-06-06 12:24:17 +0200615 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
616 type = syms->type + 1;
Roel Kluin23cdb5d2009-07-13 02:25:47 +0200617 if (type >= ARRAY_SIZE(event_type_descriptors))
Thomas Gleixner86847b62009-06-06 12:24:17 +0200618 type = 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200619
Thomas Gleixner86847b62009-06-06 12:24:17 +0200620 if (type != prev_type)
621 fprintf(stderr, "\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200622
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530623 if (strlen(syms->alias))
624 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
625 else
626 strcpy(name, syms->symbol);
627 fprintf(stderr, " %-40s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +0200628 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200629
Thomas Gleixner86847b62009-06-06 12:24:17 +0200630 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200631 }
632
Thomas Gleixner86847b62009-06-06 12:24:17 +0200633 fprintf(stderr, "\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530634 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
635 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
636 /* skip invalid cache type */
637 if (!is_cache_op_valid(type, op))
638 continue;
639
640 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
641 fprintf(stderr, " %-40s [%s]\n",
642 event_cache_name(type, op, i),
643 event_type_descriptors[4]);
644 }
645 }
646 }
647
648 fprintf(stderr, "\n");
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530649 fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200650 "rNNN");
651 fprintf(stderr, "\n");
652
Jason Baronf6bdafe2009-07-21 12:20:22 -0400653 print_tracepoint_events();
654
Thomas Gleixner86847b62009-06-06 12:24:17 +0200655 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200656}