blob: 097938a96d74b5813f040394d7f178ac5f153438 [file] [log] [blame]
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002#include "util.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03003#include "../perf.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02004#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"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02009#include "header.h"
Clark Williams549104f2009-11-08 09:03:07 -060010#include "debugfs.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020011
Li Zefanc171b552009-10-15 11:22:07 +080012int nr_counters;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020013
Ingo Molnarcdd6c482009-09-21 12:02:48 +020014struct perf_event_attr attrs[MAX_COUNTERS];
Li Zefanc171b552009-10-15 11:22:07 +080015char *filters[MAX_COUNTERS];
Ingo Molnar8ad8db32009-05-26 11:10:09 +020016
17struct event_symbol {
Ingo Molnar83a09442009-08-15 12:26:57 +020018 u8 type;
19 u64 config;
20 const char *symbol;
21 const char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020022};
23
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020024enum event_result {
25 EVT_FAILED,
26 EVT_HANDLED,
27 EVT_HANDLED_ALL
28};
29
Jason Baron5beeded2009-07-21 14:16:29 -040030char debugfs_path[MAXPATHLEN];
31
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053032#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
33#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020034
Ingo Molnara21ca2c2009-06-06 09:58:57 +020035static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053036 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
37 { CHW(INSTRUCTIONS), "instructions", "" },
38 { CHW(CACHE_REFERENCES), "cache-references", "" },
39 { CHW(CACHE_MISSES), "cache-misses", "" },
40 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
41 { CHW(BRANCH_MISSES), "branch-misses", "" },
42 { CHW(BUS_CYCLES), "bus-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020043
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053044 { CSW(CPU_CLOCK), "cpu-clock", "" },
45 { CSW(TASK_CLOCK), "task-clock", "" },
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053046 { CSW(PAGE_FAULTS), "page-faults", "faults" },
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053047 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
48 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
49 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
50 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020051};
52
Ingo Molnarcdd6c482009-09-21 12:02:48 +020053#define __PERF_EVENT_FIELD(config, name) \
54 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020055
Ingo Molnarcdd6c482009-09-21 12:02:48 +020056#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
57#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
58#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
59#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020060
Ingo Molnar83a09442009-08-15 12:26:57 +020061static const char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020062 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020063 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020064 "cache-references",
65 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020066 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020067 "branch-misses",
68 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020069};
70
Ingo Molnar83a09442009-08-15 12:26:57 +020071static const char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020072 "cpu-clock-msecs",
73 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020074 "page-faults",
75 "context-switches",
76 "CPU-migrations",
77 "minor-faults",
78 "major-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020079};
80
Ingo Molnar8326f442009-06-05 20:22:46 +020081#define MAX_ALIASES 8
82
Ingo Molnar83a09442009-08-15 12:26:57 +020083static const char *hw_cache[][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100084 { "L1-dcache", "l1-d", "l1d", "L1-data", },
85 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053086 { "LLC", "L2" },
87 { "dTLB", "d-tlb", "Data-TLB", },
88 { "iTLB", "i-tlb", "Instruction-TLB", },
89 { "branch", "branches", "bpu", "btb", "bpc", },
Ingo Molnar8326f442009-06-05 20:22:46 +020090};
91
Ingo Molnar83a09442009-08-15 12:26:57 +020092static const char *hw_cache_op[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053093 { "load", "loads", "read", },
94 { "store", "stores", "write", },
95 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +020096};
97
Ingo Molnar83a09442009-08-15 12:26:57 +020098static const char *hw_cache_result[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053099 { "refs", "Reference", "ops", "access", },
100 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200101};
102
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530103#define C(x) PERF_COUNT_HW_CACHE_##x
104#define CACHE_READ (1 << C(OP_READ))
105#define CACHE_WRITE (1 << C(OP_WRITE))
106#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
107#define COP(x) (1 << x)
108
109/*
110 * cache operartion stat
111 * L1I : Read and prefetch only
112 * ITLB and BPU : Read-only
113 */
114static unsigned long hw_cache_stat[C(MAX)] = {
115 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
116 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
117 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
118 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
119 [C(ITLB)] = (CACHE_READ),
120 [C(BPU)] = (CACHE_READ),
121};
122
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300123#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400124 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300125 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400126 (strcmp(sys_dirent.d_name, ".")) && \
127 (strcmp(sys_dirent.d_name, "..")))
128
Peter Zijlstraae07b632009-08-06 16:48:54 +0200129static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
130{
131 char evt_path[MAXPATHLEN];
132 int fd;
133
134 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
135 sys_dir->d_name, evt_dir->d_name);
136 fd = open(evt_path, O_RDONLY);
137 if (fd < 0)
138 return -EINVAL;
139 close(fd);
140
141 return 0;
142}
143
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300144#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400145 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300146 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400147 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200148 (strcmp(evt_dirent.d_name, "..")) && \
149 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400150
Li Zefan270bbbe2009-09-17 16:34:51 +0800151#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400152
Jason Baronf6bdafe2009-07-21 12:20:22 -0400153
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200154struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400155{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200156 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400157 DIR *sys_dir, *evt_dir;
158 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400159 char id_buf[4];
Eric Dumazet725b1362009-09-24 15:39:09 +0200160 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400161 u64 id;
162 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200163 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400164
Clark Williams549104f2009-11-08 09:03:07 -0600165 if (debugfs_valid_mountpoint(debugfs_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200166 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400167
Jason Baron5beeded2009-07-21 14:16:29 -0400168 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400169 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200170 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400171
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300172 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200173
174 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
175 sys_dirent.d_name);
176 evt_dir = opendir(dir_path);
177 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300178 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200179
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300180 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200181
182 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400183 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200184 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400185 if (fd < 0)
186 continue;
187 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
188 close(fd);
189 continue;
190 }
191 close(fd);
192 id = atoll(id_buf);
193 if (id == config) {
194 closedir(evt_dir);
195 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200196 path = calloc(1, sizeof(path));
197 path->system = malloc(MAX_EVENT_LENGTH);
198 if (!path->system) {
199 free(path);
200 return NULL;
201 }
202 path->name = malloc(MAX_EVENT_LENGTH);
203 if (!path->name) {
204 free(path->system);
205 free(path);
206 return NULL;
207 }
208 strncpy(path->system, sys_dirent.d_name,
209 MAX_EVENT_LENGTH);
210 strncpy(path->name, evt_dirent.d_name,
211 MAX_EVENT_LENGTH);
212 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400213 }
214 }
215 closedir(evt_dir);
216 }
217
Jason Baronf6bdafe2009-07-21 12:20:22 -0400218 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200219 return NULL;
220}
221
222#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
223static const char *tracepoint_id_to_name(u64 config)
224{
225 static char buf[TP_PATH_LEN];
226 struct tracepoint_path *path;
227
228 path = tracepoint_id_to_path(config);
229 if (path) {
230 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
231 free(path->name);
232 free(path->system);
233 free(path);
234 } else
235 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
236
237 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400238}
239
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530240static int is_cache_op_valid(u8 cache_type, u8 cache_op)
241{
242 if (hw_cache_stat[cache_type] & COP(cache_op))
243 return 1; /* valid */
244 else
245 return 0; /* invalid */
246}
247
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530248static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
249{
250 static char name[50];
251
252 if (cache_result) {
253 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
254 hw_cache_op[cache_op][0],
255 hw_cache_result[cache_result][0]);
256 } else {
257 sprintf(name, "%s-%s", hw_cache[cache_type][0],
258 hw_cache_op[cache_op][1]);
259 }
260
261 return name;
262}
263
Ingo Molnar83a09442009-08-15 12:26:57 +0200264const char *event_name(int counter)
Ingo Molnar52425192009-05-26 09:17:18 +0200265{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000266 u64 config = attrs[counter].config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200267 int type = attrs[counter].type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200268
269 return __event_name(type, config);
270}
271
Ingo Molnar83a09442009-08-15 12:26:57 +0200272const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200273{
Ingo Molnar52425192009-05-26 09:17:18 +0200274 static char buf[32];
275
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200276 if (type == PERF_TYPE_RAW) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200277 sprintf(buf, "raw 0x%llx", config);
Ingo Molnar52425192009-05-26 09:17:18 +0200278 return buf;
279 }
280
281 switch (type) {
282 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200283 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200284 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200285 return "unknown-hardware";
286
Ingo Molnar8326f442009-06-05 20:22:46 +0200287 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000288 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200289
290 cache_type = (config >> 0) & 0xff;
291 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
292 return "unknown-ext-hardware-cache-type";
293
294 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200295 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
296 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200297
298 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200299 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
300 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200301
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530302 if (!is_cache_op_valid(cache_type, cache_op))
303 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200304
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530305 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200306 }
307
Ingo Molnar52425192009-05-26 09:17:18 +0200308 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200309 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200310 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200311 return "unknown-software";
312
Jason Baronf6bdafe2009-07-21 12:20:22 -0400313 case PERF_TYPE_TRACEPOINT:
314 return tracepoint_id_to_name(config);
315
Ingo Molnar52425192009-05-26 09:17:18 +0200316 default:
317 break;
318 }
319
320 return "unknown";
321}
322
Ingo Molnar83a09442009-08-15 12:26:57 +0200323static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200324{
325 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000326 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200327
328 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000329 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
330 n = strlen(names[i][j]);
331 if (n > longest && !strncasecmp(*str, names[i][j], n))
332 longest = n;
333 }
334 if (longest > 0) {
335 *str += longest;
336 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200337 }
338 }
339
Ingo Molnar89536452009-06-06 21:04:17 +0200340 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200341}
342
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200343static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200344parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200345{
Paul Mackerras61c45982009-07-01 13:04:34 +1000346 const char *s = *str;
347 int cache_type = -1, cache_op = -1, cache_result = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200348
Paul Mackerras61c45982009-07-01 13:04:34 +1000349 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200350 /*
351 * No fallback - if we cannot get a clear cache type
352 * then bail out:
353 */
354 if (cache_type == -1)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200355 return EVT_FAILED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200356
Paul Mackerras61c45982009-07-01 13:04:34 +1000357 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
358 ++s;
359
360 if (cache_op == -1) {
361 cache_op = parse_aliases(&s, hw_cache_op,
362 PERF_COUNT_HW_CACHE_OP_MAX);
363 if (cache_op >= 0) {
364 if (!is_cache_op_valid(cache_type, cache_op))
365 return 0;
366 continue;
367 }
368 }
369
370 if (cache_result == -1) {
371 cache_result = parse_aliases(&s, hw_cache_result,
372 PERF_COUNT_HW_CACHE_RESULT_MAX);
373 if (cache_result >= 0)
374 continue;
375 }
376
377 /*
378 * Can't parse this as a cache op or result, so back up
379 * to the '-'.
380 */
381 --s;
382 break;
383 }
384
Ingo Molnar8326f442009-06-05 20:22:46 +0200385 /*
386 * Fall back to reads:
387 */
Ingo Molnar89536452009-06-06 21:04:17 +0200388 if (cache_op == -1)
389 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200390
Ingo Molnar8326f442009-06-05 20:22:46 +0200391 /*
392 * Fall back to accesses:
393 */
394 if (cache_result == -1)
395 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
396
397 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
398 attr->type = PERF_TYPE_HW_CACHE;
399
Paul Mackerras61c45982009-07-01 13:04:34 +1000400 *str = s;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200401 return EVT_HANDLED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200402}
403
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200404static enum event_result
405parse_single_tracepoint_event(char *sys_name,
406 const char *evt_name,
407 unsigned int evt_length,
408 char *flags,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200409 struct perf_event_attr *attr,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200410 const char **strp)
411{
412 char evt_path[MAXPATHLEN];
413 char id_buf[4];
414 u64 id;
415 int fd;
416
417 if (flags) {
Li Zefan1281a492009-09-17 16:49:43 +0800418 if (!strncmp(flags, "record", strlen(flags))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200419 attr->sample_type |= PERF_SAMPLE_RAW;
Li Zefan1281a492009-09-17 16:49:43 +0800420 attr->sample_type |= PERF_SAMPLE_TIME;
421 attr->sample_type |= PERF_SAMPLE_CPU;
422 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200423 }
424
425 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
426 sys_name, evt_name);
427
428 fd = open(evt_path, O_RDONLY);
429 if (fd < 0)
430 return EVT_FAILED;
431
432 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
433 close(fd);
434 return EVT_FAILED;
435 }
436
437 close(fd);
438 id = atoll(id_buf);
439 attr->config = id;
440 attr->type = PERF_TYPE_TRACEPOINT;
441 *strp = evt_name + evt_length;
442
443 return EVT_HANDLED;
444}
445
446/* sys + ':' + event + ':' + flags*/
447#define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
448static enum event_result
449parse_subsystem_tracepoint_event(char *sys_name, char *flags)
450{
451 char evt_path[MAXPATHLEN];
452 struct dirent *evt_ent;
453 DIR *evt_dir;
454
455 snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
456 evt_dir = opendir(evt_path);
457
458 if (!evt_dir) {
459 perror("Can't open event dir");
460 return EVT_FAILED;
461 }
462
463 while ((evt_ent = readdir(evt_dir))) {
464 char event_opt[MAX_EVOPT_LEN + 1];
465 int len;
466 unsigned int rem = MAX_EVOPT_LEN;
467
468 if (!strcmp(evt_ent->d_name, ".")
469 || !strcmp(evt_ent->d_name, "..")
470 || !strcmp(evt_ent->d_name, "enable")
471 || !strcmp(evt_ent->d_name, "filter"))
472 continue;
473
474 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s", sys_name,
475 evt_ent->d_name);
476 if (len < 0)
477 return EVT_FAILED;
478
479 rem -= len;
480 if (flags) {
481 if (rem < strlen(flags) + 1)
482 return EVT_FAILED;
483
484 strcat(event_opt, ":");
485 strcat(event_opt, flags);
486 }
487
488 if (parse_events(NULL, event_opt, 0))
489 return EVT_FAILED;
490 }
491
492 return EVT_HANDLED_ALL;
493}
494
495
496static enum event_result parse_tracepoint_event(const char **strp,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200497 struct perf_event_attr *attr)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400498{
499 const char *evt_name;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200500 char *flags;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400501 char sys_name[MAX_EVENT_LENGTH];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400502 unsigned int sys_length, evt_length;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400503
Clark Williams549104f2009-11-08 09:03:07 -0600504 if (debugfs_valid_mountpoint(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400505 return 0;
506
507 evt_name = strchr(*strp, ':');
508 if (!evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200509 return EVT_FAILED;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400510
511 sys_length = evt_name - *strp;
512 if (sys_length >= MAX_EVENT_LENGTH)
513 return 0;
514
515 strncpy(sys_name, *strp, sys_length);
516 sys_name[sys_length] = '\0';
517 evt_name = evt_name + 1;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200518
519 flags = strchr(evt_name, ':');
520 if (flags) {
Ingo Molnar1fc35b22009-09-13 09:44:29 +0200521 /* split it out: */
522 evt_name = strndup(evt_name, flags - evt_name);
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200523 flags++;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200524 }
525
Jason Baronf6bdafe2009-07-21 12:20:22 -0400526 evt_length = strlen(evt_name);
527 if (evt_length >= MAX_EVENT_LENGTH)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200528 return EVT_FAILED;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400529
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200530 if (!strcmp(evt_name, "*")) {
531 *strp = evt_name + evt_length;
532 return parse_subsystem_tracepoint_event(sys_name, flags);
533 } else
534 return parse_single_tracepoint_event(sys_name, evt_name,
535 evt_length, flags,
536 attr, strp);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400537}
538
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530539static int check_events(const char *str, unsigned int i)
540{
Paul Mackerras61c45982009-07-01 13:04:34 +1000541 int n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530542
Paul Mackerras61c45982009-07-01 13:04:34 +1000543 n = strlen(event_symbols[i].symbol);
544 if (!strncmp(str, event_symbols[i].symbol, n))
545 return n;
546
547 n = strlen(event_symbols[i].alias);
548 if (n)
549 if (!strncmp(str, event_symbols[i].alias, n))
550 return n;
551 return 0;
552}
553
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200554static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200555parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000556{
557 const char *str = *strp;
558 unsigned int i;
559 int n;
560
561 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
562 n = check_events(str, i);
563 if (n > 0) {
564 attr->type = event_symbols[i].type;
565 attr->config = event_symbols[i].config;
566 *strp = str + n;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200567 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000568 }
569 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200570 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000571}
572
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200573static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200574parse_raw_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000575{
576 const char *str = *strp;
577 u64 config;
578 int n;
579
580 if (*str != 'r')
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200581 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000582 n = hex2u64(str + 1, &config);
583 if (n > 0) {
584 *strp = str + n + 1;
585 attr->type = PERF_TYPE_RAW;
586 attr->config = config;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200587 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000588 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200589 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000590}
591
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200592static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200593parse_numeric_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000594{
595 const char *str = *strp;
596 char *endp;
597 unsigned long type;
598 u64 config;
599
600 type = strtoul(str, &endp, 0);
601 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
602 str = endp + 1;
603 config = strtoul(str, &endp, 0);
604 if (endp > str) {
605 attr->type = type;
606 attr->config = config;
607 *strp = endp;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200608 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000609 }
610 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200611 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000612}
613
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200614static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200615parse_event_modifier(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000616{
617 const char *str = *strp;
618 int eu = 1, ek = 1, eh = 1;
619
620 if (*str++ != ':')
621 return 0;
622 while (*str) {
623 if (*str == 'u')
624 eu = 0;
625 else if (*str == 'k')
626 ek = 0;
627 else if (*str == 'h')
628 eh = 0;
629 else
630 break;
631 ++str;
632 }
633 if (str >= *strp + 2) {
634 *strp = str;
635 attr->exclude_user = eu;
636 attr->exclude_kernel = ek;
637 attr->exclude_hv = eh;
638 return 1;
639 }
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530640 return 0;
641}
642
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200643/*
644 * Each event can have multiple symbolic names.
645 * Symbolic names are (almost) exactly matched.
646 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200647static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200648parse_event_symbols(const char **str, struct perf_event_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200649{
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200650 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200651
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200652 ret = parse_tracepoint_event(str, attr);
653 if (ret != EVT_FAILED)
654 goto modifier;
655
656 ret = parse_raw_event(str, attr);
657 if (ret != EVT_FAILED)
658 goto modifier;
659
660 ret = parse_numeric_event(str, attr);
661 if (ret != EVT_FAILED)
662 goto modifier;
663
664 ret = parse_symbolic_event(str, attr);
665 if (ret != EVT_FAILED)
666 goto modifier;
667
668 ret = parse_generic_hw_event(str, attr);
669 if (ret != EVT_FAILED)
670 goto modifier;
671
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000672 fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
673 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200674 return EVT_FAILED;
675
676modifier:
Paul Mackerras61c45982009-07-01 13:04:34 +1000677 parse_event_modifier(str, attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200678
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200679 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200680}
681
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200682static void store_event_type(const char *orgname)
683{
684 char filename[PATH_MAX], *c;
685 FILE *file;
686 int id;
687
Ashwin Chaugule63c9e012009-10-04 15:49:34 -0700688 sprintf(filename, "%s/", debugfs_path);
689 strncat(filename, orgname, strlen(orgname));
690 strcat(filename, "/id");
691
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200692 c = strchr(filename, ':');
693 if (c)
694 *c = '/';
695
696 file = fopen(filename, "r");
697 if (!file)
698 return;
699 if (fscanf(file, "%i", &id) < 1)
700 die("cannot store event ID");
701 fclose(file);
702 perf_header__push_event(id, orgname);
703}
704
Ingo Molnarf37a2912009-07-01 12:37:06 +0200705int parse_events(const struct option *opt __used, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200706{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200707 struct perf_event_attr attr;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200708 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200709
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200710 if (strchr(str, ':'))
711 store_event_type(str);
712
Paul Mackerras61c45982009-07-01 13:04:34 +1000713 for (;;) {
714 if (nr_counters == MAX_COUNTERS)
715 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200716
Paul Mackerras61c45982009-07-01 13:04:34 +1000717 memset(&attr, 0, sizeof(attr));
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200718 ret = parse_event_symbols(&str, &attr);
719 if (ret == EVT_FAILED)
Paul Mackerras61c45982009-07-01 13:04:34 +1000720 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200721
Paul Mackerras61c45982009-07-01 13:04:34 +1000722 if (!(*str == 0 || *str == ',' || isspace(*str)))
723 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200724
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200725 if (ret != EVT_HANDLED_ALL) {
726 attrs[nr_counters] = attr;
727 nr_counters++;
728 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000729
730 if (*str == 0)
731 break;
732 if (*str == ',')
733 ++str;
734 while (isspace(*str))
735 ++str;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200736 }
737
738 return 0;
739}
740
Li Zefanc171b552009-10-15 11:22:07 +0800741int parse_filter(const struct option *opt __used, const char *str,
742 int unset __used)
743{
744 int i = nr_counters - 1;
745 int len = strlen(str);
746
747 if (i < 0 || attrs[i].type != PERF_TYPE_TRACEPOINT) {
748 fprintf(stderr,
749 "-F option should follow a -e tracepoint option\n");
750 return -1;
751 }
752
753 filters[i] = malloc(len + 1);
754 if (!filters[i]) {
755 fprintf(stderr, "not enough memory to hold filter string\n");
756 return -1;
757 }
758 strcpy(filters[i], str);
759
760 return 0;
761}
762
Thomas Gleixner86847b62009-06-06 12:24:17 +0200763static const char * const event_type_descriptors[] = {
764 "",
765 "Hardware event",
766 "Software event",
767 "Tracepoint event",
768 "Hardware cache event",
769};
770
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200771/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400772 * Print the events from <debugfs_mount_point>/tracing/events
773 */
774
775static void print_tracepoint_events(void)
776{
777 DIR *sys_dir, *evt_dir;
778 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400779 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200780 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400781
Clark Williams549104f2009-11-08 09:03:07 -0600782 if (debugfs_valid_mountpoint(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400783 return;
784
Jason Baron5beeded2009-07-21 14:16:29 -0400785 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400786 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200787 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400788
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300789 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200790
791 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
792 sys_dirent.d_name);
793 evt_dir = opendir(dir_path);
794 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300795 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200796
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300797 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Jason Baronf6bdafe2009-07-21 12:20:22 -0400798 snprintf(evt_path, MAXPATHLEN, "%s:%s",
799 sys_dirent.d_name, evt_dirent.d_name);
Marti Raudsepp689d3012009-10-27 00:33:05 +0000800 printf(" %-42s [%s]\n", evt_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400801 event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
802 }
803 closedir(evt_dir);
804 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400805 closedir(sys_dir);
806}
807
808/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200809 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200810 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200811void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200812{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200813 struct event_symbol *syms = event_symbols;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530814 unsigned int i, type, op, prev_type = -1;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530815 char name[40];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200816
Marti Raudsepp689d3012009-10-27 00:33:05 +0000817 printf("\n");
818 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200819
Thomas Gleixner86847b62009-06-06 12:24:17 +0200820 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
821 type = syms->type + 1;
Roel Kluin23cdb5d2009-07-13 02:25:47 +0200822 if (type >= ARRAY_SIZE(event_type_descriptors))
Thomas Gleixner86847b62009-06-06 12:24:17 +0200823 type = 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200824
Thomas Gleixner86847b62009-06-06 12:24:17 +0200825 if (type != prev_type)
Marti Raudsepp689d3012009-10-27 00:33:05 +0000826 printf("\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200827
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530828 if (strlen(syms->alias))
829 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
830 else
831 strcpy(name, syms->symbol);
Marti Raudsepp689d3012009-10-27 00:33:05 +0000832 printf(" %-42s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +0200833 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200834
Thomas Gleixner86847b62009-06-06 12:24:17 +0200835 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200836 }
837
Marti Raudsepp689d3012009-10-27 00:33:05 +0000838 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530839 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
840 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
841 /* skip invalid cache type */
842 if (!is_cache_op_valid(type, op))
843 continue;
844
845 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Marti Raudsepp689d3012009-10-27 00:33:05 +0000846 printf(" %-42s [%s]\n",
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530847 event_cache_name(type, op, i),
848 event_type_descriptors[4]);
849 }
850 }
851 }
852
Marti Raudsepp689d3012009-10-27 00:33:05 +0000853 printf("\n");
854 printf(" %-42s [raw hardware event descriptor]\n",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200855 "rNNN");
Marti Raudsepp689d3012009-10-27 00:33:05 +0000856 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +0200857
Jason Baronf6bdafe2009-07-21 12:20:22 -0400858 print_tracepoint_events();
859
Thomas Gleixner86847b62009-06-06 12:24:17 +0200860 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200861}