blob: 8cfb48cbbea01d8fbd5ba6aee541d07c360b7d3d [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"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020010
Ingo Molnara21ca2c2009-06-06 09:58:57 +020011int nr_counters;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020012
Ingo Molnarcdd6c482009-09-21 12:02:48 +020013struct perf_event_attr attrs[MAX_COUNTERS];
Ingo Molnar8ad8db32009-05-26 11:10:09 +020014
15struct event_symbol {
Ingo Molnar83a09442009-08-15 12:26:57 +020016 u8 type;
17 u64 config;
18 const char *symbol;
19 const char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020020};
21
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020022enum event_result {
23 EVT_FAILED,
24 EVT_HANDLED,
25 EVT_HANDLED_ALL
26};
27
Jason Baron5beeded2009-07-21 14:16:29 -040028char debugfs_path[MAXPATHLEN];
29
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053030#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
31#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020032
Ingo Molnara21ca2c2009-06-06 09:58:57 +020033static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053034 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
35 { CHW(INSTRUCTIONS), "instructions", "" },
36 { CHW(CACHE_REFERENCES), "cache-references", "" },
37 { CHW(CACHE_MISSES), "cache-misses", "" },
38 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
39 { CHW(BRANCH_MISSES), "branch-misses", "" },
40 { CHW(BUS_CYCLES), "bus-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020041
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053042 { CSW(CPU_CLOCK), "cpu-clock", "" },
43 { CSW(TASK_CLOCK), "task-clock", "" },
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053044 { CSW(PAGE_FAULTS), "page-faults", "faults" },
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053045 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
46 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
47 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
48 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020049};
50
Ingo Molnarcdd6c482009-09-21 12:02:48 +020051#define __PERF_EVENT_FIELD(config, name) \
52 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020053
Ingo Molnarcdd6c482009-09-21 12:02:48 +020054#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
55#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
56#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
57#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020058
Ingo Molnar83a09442009-08-15 12:26:57 +020059static const char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020060 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020061 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020062 "cache-references",
63 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020064 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020065 "branch-misses",
66 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020067};
68
Ingo Molnar83a09442009-08-15 12:26:57 +020069static const char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020070 "cpu-clock-msecs",
71 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020072 "page-faults",
73 "context-switches",
74 "CPU-migrations",
75 "minor-faults",
76 "major-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020077};
78
Ingo Molnar8326f442009-06-05 20:22:46 +020079#define MAX_ALIASES 8
80
Ingo Molnar83a09442009-08-15 12:26:57 +020081static const char *hw_cache[][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100082 { "L1-dcache", "l1-d", "l1d", "L1-data", },
83 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053084 { "LLC", "L2" },
85 { "dTLB", "d-tlb", "Data-TLB", },
86 { "iTLB", "i-tlb", "Instruction-TLB", },
87 { "branch", "branches", "bpu", "btb", "bpc", },
Ingo Molnar8326f442009-06-05 20:22:46 +020088};
89
Ingo Molnar83a09442009-08-15 12:26:57 +020090static const char *hw_cache_op[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053091 { "load", "loads", "read", },
92 { "store", "stores", "write", },
93 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +020094};
95
Ingo Molnar83a09442009-08-15 12:26:57 +020096static const char *hw_cache_result[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053097 { "refs", "Reference", "ops", "access", },
98 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +020099};
100
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530101#define C(x) PERF_COUNT_HW_CACHE_##x
102#define CACHE_READ (1 << C(OP_READ))
103#define CACHE_WRITE (1 << C(OP_WRITE))
104#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
105#define COP(x) (1 << x)
106
107/*
108 * cache operartion stat
109 * L1I : Read and prefetch only
110 * ITLB and BPU : Read-only
111 */
112static unsigned long hw_cache_stat[C(MAX)] = {
113 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
114 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
115 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
116 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
117 [C(ITLB)] = (CACHE_READ),
118 [C(BPU)] = (CACHE_READ),
119};
120
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300121#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400122 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300123 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400124 (strcmp(sys_dirent.d_name, ".")) && \
125 (strcmp(sys_dirent.d_name, "..")))
126
Peter Zijlstraae07b632009-08-06 16:48:54 +0200127static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
128{
129 char evt_path[MAXPATHLEN];
130 int fd;
131
132 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
133 sys_dir->d_name, evt_dir->d_name);
134 fd = open(evt_path, O_RDONLY);
135 if (fd < 0)
136 return -EINVAL;
137 close(fd);
138
139 return 0;
140}
141
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300142#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400143 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300144 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400145 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200146 (strcmp(evt_dirent.d_name, "..")) && \
147 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400148
Li Zefan270bbbe2009-09-17 16:34:51 +0800149#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400150
Jason Baron5beeded2009-07-21 14:16:29 -0400151int valid_debugfs_mount(const char *debugfs)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400152{
153 struct statfs st_fs;
154
Jason Baron5beeded2009-07-21 14:16:29 -0400155 if (statfs(debugfs, &st_fs) < 0)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400156 return -ENOENT;
157 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
158 return -ENOENT;
159 return 0;
160}
161
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200162struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400163{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200164 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400165 DIR *sys_dir, *evt_dir;
166 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400167 char id_buf[4];
Eric Dumazet725b1362009-09-24 15:39:09 +0200168 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400169 u64 id;
170 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200171 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400172
Jason Baron5beeded2009-07-21 14:16:29 -0400173 if (valid_debugfs_mount(debugfs_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200174 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400175
Jason Baron5beeded2009-07-21 14:16:29 -0400176 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400177 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200178 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400179
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300180 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200181
182 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
183 sys_dirent.d_name);
184 evt_dir = opendir(dir_path);
185 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300186 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200187
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300188 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200189
190 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400191 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200192 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400193 if (fd < 0)
194 continue;
195 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
196 close(fd);
197 continue;
198 }
199 close(fd);
200 id = atoll(id_buf);
201 if (id == config) {
202 closedir(evt_dir);
203 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200204 path = calloc(1, sizeof(path));
205 path->system = malloc(MAX_EVENT_LENGTH);
206 if (!path->system) {
207 free(path);
208 return NULL;
209 }
210 path->name = malloc(MAX_EVENT_LENGTH);
211 if (!path->name) {
212 free(path->system);
213 free(path);
214 return NULL;
215 }
216 strncpy(path->system, sys_dirent.d_name,
217 MAX_EVENT_LENGTH);
218 strncpy(path->name, evt_dirent.d_name,
219 MAX_EVENT_LENGTH);
220 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400221 }
222 }
223 closedir(evt_dir);
224 }
225
Jason Baronf6bdafe2009-07-21 12:20:22 -0400226 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200227 return NULL;
228}
229
230#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
231static const char *tracepoint_id_to_name(u64 config)
232{
233 static char buf[TP_PATH_LEN];
234 struct tracepoint_path *path;
235
236 path = tracepoint_id_to_path(config);
237 if (path) {
238 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
239 free(path->name);
240 free(path->system);
241 free(path);
242 } else
243 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
244
245 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400246}
247
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530248static int is_cache_op_valid(u8 cache_type, u8 cache_op)
249{
250 if (hw_cache_stat[cache_type] & COP(cache_op))
251 return 1; /* valid */
252 else
253 return 0; /* invalid */
254}
255
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530256static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
257{
258 static char name[50];
259
260 if (cache_result) {
261 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
262 hw_cache_op[cache_op][0],
263 hw_cache_result[cache_result][0]);
264 } else {
265 sprintf(name, "%s-%s", hw_cache[cache_type][0],
266 hw_cache_op[cache_op][1]);
267 }
268
269 return name;
270}
271
Ingo Molnar83a09442009-08-15 12:26:57 +0200272const char *event_name(int counter)
Ingo Molnar52425192009-05-26 09:17:18 +0200273{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000274 u64 config = attrs[counter].config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200275 int type = attrs[counter].type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200276
277 return __event_name(type, config);
278}
279
Ingo Molnar83a09442009-08-15 12:26:57 +0200280const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200281{
Ingo Molnar52425192009-05-26 09:17:18 +0200282 static char buf[32];
283
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200284 if (type == PERF_TYPE_RAW) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200285 sprintf(buf, "raw 0x%llx", config);
Ingo Molnar52425192009-05-26 09:17:18 +0200286 return buf;
287 }
288
289 switch (type) {
290 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200291 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200292 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200293 return "unknown-hardware";
294
Ingo Molnar8326f442009-06-05 20:22:46 +0200295 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000296 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200297
298 cache_type = (config >> 0) & 0xff;
299 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
300 return "unknown-ext-hardware-cache-type";
301
302 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200303 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
304 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200305
306 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200307 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
308 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200309
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530310 if (!is_cache_op_valid(cache_type, cache_op))
311 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200312
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530313 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200314 }
315
Ingo Molnar52425192009-05-26 09:17:18 +0200316 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200317 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200318 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200319 return "unknown-software";
320
Jason Baronf6bdafe2009-07-21 12:20:22 -0400321 case PERF_TYPE_TRACEPOINT:
322 return tracepoint_id_to_name(config);
323
Ingo Molnar52425192009-05-26 09:17:18 +0200324 default:
325 break;
326 }
327
328 return "unknown";
329}
330
Ingo Molnar83a09442009-08-15 12:26:57 +0200331static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200332{
333 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000334 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200335
336 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000337 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
338 n = strlen(names[i][j]);
339 if (n > longest && !strncasecmp(*str, names[i][j], n))
340 longest = n;
341 }
342 if (longest > 0) {
343 *str += longest;
344 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200345 }
346 }
347
Ingo Molnar89536452009-06-06 21:04:17 +0200348 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200349}
350
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200351static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200352parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200353{
Paul Mackerras61c45982009-07-01 13:04:34 +1000354 const char *s = *str;
355 int cache_type = -1, cache_op = -1, cache_result = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200356
Paul Mackerras61c45982009-07-01 13:04:34 +1000357 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200358 /*
359 * No fallback - if we cannot get a clear cache type
360 * then bail out:
361 */
362 if (cache_type == -1)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200363 return EVT_FAILED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200364
Paul Mackerras61c45982009-07-01 13:04:34 +1000365 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
366 ++s;
367
368 if (cache_op == -1) {
369 cache_op = parse_aliases(&s, hw_cache_op,
370 PERF_COUNT_HW_CACHE_OP_MAX);
371 if (cache_op >= 0) {
372 if (!is_cache_op_valid(cache_type, cache_op))
373 return 0;
374 continue;
375 }
376 }
377
378 if (cache_result == -1) {
379 cache_result = parse_aliases(&s, hw_cache_result,
380 PERF_COUNT_HW_CACHE_RESULT_MAX);
381 if (cache_result >= 0)
382 continue;
383 }
384
385 /*
386 * Can't parse this as a cache op or result, so back up
387 * to the '-'.
388 */
389 --s;
390 break;
391 }
392
Ingo Molnar8326f442009-06-05 20:22:46 +0200393 /*
394 * Fall back to reads:
395 */
Ingo Molnar89536452009-06-06 21:04:17 +0200396 if (cache_op == -1)
397 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200398
Ingo Molnar8326f442009-06-05 20:22:46 +0200399 /*
400 * Fall back to accesses:
401 */
402 if (cache_result == -1)
403 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
404
405 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
406 attr->type = PERF_TYPE_HW_CACHE;
407
Paul Mackerras61c45982009-07-01 13:04:34 +1000408 *str = s;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200409 return EVT_HANDLED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200410}
411
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200412static enum event_result
413parse_single_tracepoint_event(char *sys_name,
414 const char *evt_name,
415 unsigned int evt_length,
416 char *flags,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200417 struct perf_event_attr *attr,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200418 const char **strp)
419{
420 char evt_path[MAXPATHLEN];
421 char id_buf[4];
422 u64 id;
423 int fd;
424
425 if (flags) {
Li Zefan1281a492009-09-17 16:49:43 +0800426 if (!strncmp(flags, "record", strlen(flags))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200427 attr->sample_type |= PERF_SAMPLE_RAW;
Li Zefan1281a492009-09-17 16:49:43 +0800428 attr->sample_type |= PERF_SAMPLE_TIME;
429 attr->sample_type |= PERF_SAMPLE_CPU;
430 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200431 }
432
433 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
434 sys_name, evt_name);
435
436 fd = open(evt_path, O_RDONLY);
437 if (fd < 0)
438 return EVT_FAILED;
439
440 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
441 close(fd);
442 return EVT_FAILED;
443 }
444
445 close(fd);
446 id = atoll(id_buf);
447 attr->config = id;
448 attr->type = PERF_TYPE_TRACEPOINT;
449 *strp = evt_name + evt_length;
450
451 return EVT_HANDLED;
452}
453
454/* sys + ':' + event + ':' + flags*/
455#define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
456static enum event_result
457parse_subsystem_tracepoint_event(char *sys_name, char *flags)
458{
459 char evt_path[MAXPATHLEN];
460 struct dirent *evt_ent;
461 DIR *evt_dir;
462
463 snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
464 evt_dir = opendir(evt_path);
465
466 if (!evt_dir) {
467 perror("Can't open event dir");
468 return EVT_FAILED;
469 }
470
471 while ((evt_ent = readdir(evt_dir))) {
472 char event_opt[MAX_EVOPT_LEN + 1];
473 int len;
474 unsigned int rem = MAX_EVOPT_LEN;
475
476 if (!strcmp(evt_ent->d_name, ".")
477 || !strcmp(evt_ent->d_name, "..")
478 || !strcmp(evt_ent->d_name, "enable")
479 || !strcmp(evt_ent->d_name, "filter"))
480 continue;
481
482 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s", sys_name,
483 evt_ent->d_name);
484 if (len < 0)
485 return EVT_FAILED;
486
487 rem -= len;
488 if (flags) {
489 if (rem < strlen(flags) + 1)
490 return EVT_FAILED;
491
492 strcat(event_opt, ":");
493 strcat(event_opt, flags);
494 }
495
496 if (parse_events(NULL, event_opt, 0))
497 return EVT_FAILED;
498 }
499
500 return EVT_HANDLED_ALL;
501}
502
503
504static enum event_result parse_tracepoint_event(const char **strp,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200505 struct perf_event_attr *attr)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400506{
507 const char *evt_name;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200508 char *flags;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400509 char sys_name[MAX_EVENT_LENGTH];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400510 unsigned int sys_length, evt_length;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400511
Jason Baron5beeded2009-07-21 14:16:29 -0400512 if (valid_debugfs_mount(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400513 return 0;
514
515 evt_name = strchr(*strp, ':');
516 if (!evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200517 return EVT_FAILED;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400518
519 sys_length = evt_name - *strp;
520 if (sys_length >= MAX_EVENT_LENGTH)
521 return 0;
522
523 strncpy(sys_name, *strp, sys_length);
524 sys_name[sys_length] = '\0';
525 evt_name = evt_name + 1;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200526
527 flags = strchr(evt_name, ':');
528 if (flags) {
Ingo Molnar1fc35b22009-09-13 09:44:29 +0200529 /* split it out: */
530 evt_name = strndup(evt_name, flags - evt_name);
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200531 flags++;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200532 }
533
Jason Baronf6bdafe2009-07-21 12:20:22 -0400534 evt_length = strlen(evt_name);
535 if (evt_length >= MAX_EVENT_LENGTH)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200536 return EVT_FAILED;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400537
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200538 if (!strcmp(evt_name, "*")) {
539 *strp = evt_name + evt_length;
540 return parse_subsystem_tracepoint_event(sys_name, flags);
541 } else
542 return parse_single_tracepoint_event(sys_name, evt_name,
543 evt_length, flags,
544 attr, strp);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400545}
546
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530547static int check_events(const char *str, unsigned int i)
548{
Paul Mackerras61c45982009-07-01 13:04:34 +1000549 int n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530550
Paul Mackerras61c45982009-07-01 13:04:34 +1000551 n = strlen(event_symbols[i].symbol);
552 if (!strncmp(str, event_symbols[i].symbol, n))
553 return n;
554
555 n = strlen(event_symbols[i].alias);
556 if (n)
557 if (!strncmp(str, event_symbols[i].alias, n))
558 return n;
559 return 0;
560}
561
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200562static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200563parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000564{
565 const char *str = *strp;
566 unsigned int i;
567 int n;
568
569 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
570 n = check_events(str, i);
571 if (n > 0) {
572 attr->type = event_symbols[i].type;
573 attr->config = event_symbols[i].config;
574 *strp = str + n;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200575 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000576 }
577 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200578 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000579}
580
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200581static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200582parse_raw_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000583{
584 const char *str = *strp;
585 u64 config;
586 int n;
587
588 if (*str != 'r')
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200589 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000590 n = hex2u64(str + 1, &config);
591 if (n > 0) {
592 *strp = str + n + 1;
593 attr->type = PERF_TYPE_RAW;
594 attr->config = config;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200595 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000596 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200597 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000598}
599
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200600static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200601parse_numeric_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000602{
603 const char *str = *strp;
604 char *endp;
605 unsigned long type;
606 u64 config;
607
608 type = strtoul(str, &endp, 0);
609 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
610 str = endp + 1;
611 config = strtoul(str, &endp, 0);
612 if (endp > str) {
613 attr->type = type;
614 attr->config = config;
615 *strp = endp;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200616 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000617 }
618 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200619 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000620}
621
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200622static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200623parse_event_modifier(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000624{
625 const char *str = *strp;
626 int eu = 1, ek = 1, eh = 1;
627
628 if (*str++ != ':')
629 return 0;
630 while (*str) {
631 if (*str == 'u')
632 eu = 0;
633 else if (*str == 'k')
634 ek = 0;
635 else if (*str == 'h')
636 eh = 0;
637 else
638 break;
639 ++str;
640 }
641 if (str >= *strp + 2) {
642 *strp = str;
643 attr->exclude_user = eu;
644 attr->exclude_kernel = ek;
645 attr->exclude_hv = eh;
646 return 1;
647 }
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530648 return 0;
649}
650
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200651/*
652 * Each event can have multiple symbolic names.
653 * Symbolic names are (almost) exactly matched.
654 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200655static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200656parse_event_symbols(const char **str, struct perf_event_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200657{
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200658 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200659
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200660 ret = parse_tracepoint_event(str, attr);
661 if (ret != EVT_FAILED)
662 goto modifier;
663
664 ret = parse_raw_event(str, attr);
665 if (ret != EVT_FAILED)
666 goto modifier;
667
668 ret = parse_numeric_event(str, attr);
669 if (ret != EVT_FAILED)
670 goto modifier;
671
672 ret = parse_symbolic_event(str, attr);
673 if (ret != EVT_FAILED)
674 goto modifier;
675
676 ret = parse_generic_hw_event(str, attr);
677 if (ret != EVT_FAILED)
678 goto modifier;
679
680 return EVT_FAILED;
681
682modifier:
Paul Mackerras61c45982009-07-01 13:04:34 +1000683 parse_event_modifier(str, attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200684
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200685 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200686}
687
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200688static void store_event_type(const char *orgname)
689{
690 char filename[PATH_MAX], *c;
691 FILE *file;
692 int id;
693
Ashwin Chaugule63c9e012009-10-04 15:49:34 -0700694 sprintf(filename, "%s/", debugfs_path);
695 strncat(filename, orgname, strlen(orgname));
696 strcat(filename, "/id");
697
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200698 c = strchr(filename, ':');
699 if (c)
700 *c = '/';
701
702 file = fopen(filename, "r");
703 if (!file)
704 return;
705 if (fscanf(file, "%i", &id) < 1)
706 die("cannot store event ID");
707 fclose(file);
708 perf_header__push_event(id, orgname);
709}
710
711
Ingo Molnarf37a2912009-07-01 12:37:06 +0200712int parse_events(const struct option *opt __used, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200713{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200714 struct perf_event_attr attr;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200715 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200716
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200717 if (strchr(str, ':'))
718 store_event_type(str);
719
Paul Mackerras61c45982009-07-01 13:04:34 +1000720 for (;;) {
721 if (nr_counters == MAX_COUNTERS)
722 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200723
Paul Mackerras61c45982009-07-01 13:04:34 +1000724 memset(&attr, 0, sizeof(attr));
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200725 ret = parse_event_symbols(&str, &attr);
726 if (ret == EVT_FAILED)
Paul Mackerras61c45982009-07-01 13:04:34 +1000727 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200728
Paul Mackerras61c45982009-07-01 13:04:34 +1000729 if (!(*str == 0 || *str == ',' || isspace(*str)))
730 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200731
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200732 if (ret != EVT_HANDLED_ALL) {
733 attrs[nr_counters] = attr;
734 nr_counters++;
735 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000736
737 if (*str == 0)
738 break;
739 if (*str == ',')
740 ++str;
741 while (isspace(*str))
742 ++str;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200743 }
744
745 return 0;
746}
747
Thomas Gleixner86847b62009-06-06 12:24:17 +0200748static const char * const event_type_descriptors[] = {
749 "",
750 "Hardware event",
751 "Software event",
752 "Tracepoint event",
753 "Hardware cache event",
754};
755
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200756/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400757 * Print the events from <debugfs_mount_point>/tracing/events
758 */
759
760static void print_tracepoint_events(void)
761{
762 DIR *sys_dir, *evt_dir;
763 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400764 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200765 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400766
Jason Baron5beeded2009-07-21 14:16:29 -0400767 if (valid_debugfs_mount(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400768 return;
769
Jason Baron5beeded2009-07-21 14:16:29 -0400770 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400771 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200772 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400773
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300774 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200775
776 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
777 sys_dirent.d_name);
778 evt_dir = opendir(dir_path);
779 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300780 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200781
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300782 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Jason Baronf6bdafe2009-07-21 12:20:22 -0400783 snprintf(evt_path, MAXPATHLEN, "%s:%s",
784 sys_dirent.d_name, evt_dirent.d_name);
Jason Baron48c2e172009-08-10 16:53:06 -0400785 fprintf(stderr, " %-42s [%s]\n", evt_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400786 event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
787 }
788 closedir(evt_dir);
789 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400790 closedir(sys_dir);
791}
792
793/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200794 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200795 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200796void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200797{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200798 struct event_symbol *syms = event_symbols;
Jaswinder Singh Rajput73c24cb82009-07-01 18:36:18 +0530799 unsigned int i, type, op, prev_type = -1;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530800 char name[40];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200801
Thomas Gleixner86847b62009-06-06 12:24:17 +0200802 fprintf(stderr, "\n");
803 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200804
Thomas Gleixner86847b62009-06-06 12:24:17 +0200805 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
806 type = syms->type + 1;
Roel Kluin23cdb5d2009-07-13 02:25:47 +0200807 if (type >= ARRAY_SIZE(event_type_descriptors))
Thomas Gleixner86847b62009-06-06 12:24:17 +0200808 type = 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200809
Thomas Gleixner86847b62009-06-06 12:24:17 +0200810 if (type != prev_type)
811 fprintf(stderr, "\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200812
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530813 if (strlen(syms->alias))
814 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
815 else
816 strcpy(name, syms->symbol);
Jason Baron48c2e172009-08-10 16:53:06 -0400817 fprintf(stderr, " %-42s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +0200818 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200819
Thomas Gleixner86847b62009-06-06 12:24:17 +0200820 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200821 }
822
Thomas Gleixner86847b62009-06-06 12:24:17 +0200823 fprintf(stderr, "\n");
Jaswinder Singh Rajput73c24cb82009-07-01 18:36:18 +0530824 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
825 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
826 /* skip invalid cache type */
827 if (!is_cache_op_valid(type, op))
828 continue;
829
830 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Jason Baron48c2e172009-08-10 16:53:06 -0400831 fprintf(stderr, " %-42s [%s]\n",
Jaswinder Singh Rajput73c24cb82009-07-01 18:36:18 +0530832 event_cache_name(type, op, i),
833 event_type_descriptors[4]);
834 }
835 }
836 }
837
838 fprintf(stderr, "\n");
Jason Baron48c2e172009-08-10 16:53:06 -0400839 fprintf(stderr, " %-42s [raw hardware event descriptor]\n",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200840 "rNNN");
841 fprintf(stderr, "\n");
842
Jason Baronf6bdafe2009-07-21 12:20:22 -0400843 print_tracepoint_events();
844
Thomas Gleixner86847b62009-06-06 12:24:17 +0200845 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200846}