blob: 52219d5a507b4ebf1fc61a6c47094dac911d4c10 [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"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02009
Ingo Molnara21ca2c2009-06-06 09:58:57 +020010int nr_counters;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020011
Ingo Molnara21ca2c2009-06-06 09:58:57 +020012struct perf_counter_attr attrs[MAX_COUNTERS];
Ingo Molnar8ad8db32009-05-26 11:10:09 +020013
14struct event_symbol {
Ingo Molnar83a09442009-08-15 12:26:57 +020015 u8 type;
16 u64 config;
17 const char *symbol;
18 const char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020019};
20
Jason Baron5beeded2009-07-21 14:16:29 -040021char debugfs_path[MAXPATHLEN];
22
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053023#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
24#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020025
Ingo Molnara21ca2c2009-06-06 09:58:57 +020026static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053027 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
28 { CHW(INSTRUCTIONS), "instructions", "" },
29 { CHW(CACHE_REFERENCES), "cache-references", "" },
30 { CHW(CACHE_MISSES), "cache-misses", "" },
31 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
32 { CHW(BRANCH_MISSES), "branch-misses", "" },
33 { CHW(BUS_CYCLES), "bus-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020034
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053035 { CSW(CPU_CLOCK), "cpu-clock", "" },
36 { CSW(TASK_CLOCK), "task-clock", "" },
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053037 { CSW(PAGE_FAULTS), "page-faults", "faults" },
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053038 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
39 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
40 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
41 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020042};
43
Ingo Molnar52425192009-05-26 09:17:18 +020044#define __PERF_COUNTER_FIELD(config, name) \
45 ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
46
47#define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
48#define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
49#define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
50#define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
51
Ingo Molnar83a09442009-08-15 12:26:57 +020052static const char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020053 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020054 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020055 "cache-references",
56 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020057 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020058 "branch-misses",
59 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020060};
61
Ingo Molnar83a09442009-08-15 12:26:57 +020062static const char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020063 "cpu-clock-msecs",
64 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020065 "page-faults",
66 "context-switches",
67 "CPU-migrations",
68 "minor-faults",
69 "major-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020070};
71
Ingo Molnar8326f442009-06-05 20:22:46 +020072#define MAX_ALIASES 8
73
Ingo Molnar83a09442009-08-15 12:26:57 +020074static const char *hw_cache[][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100075 { "L1-dcache", "l1-d", "l1d", "L1-data", },
76 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053077 { "LLC", "L2" },
78 { "dTLB", "d-tlb", "Data-TLB", },
79 { "iTLB", "i-tlb", "Instruction-TLB", },
80 { "branch", "branches", "bpu", "btb", "bpc", },
Ingo Molnar8326f442009-06-05 20:22:46 +020081};
82
Ingo Molnar83a09442009-08-15 12:26:57 +020083static const char *hw_cache_op[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053084 { "load", "loads", "read", },
85 { "store", "stores", "write", },
86 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +020087};
88
Ingo Molnar83a09442009-08-15 12:26:57 +020089static const char *hw_cache_result[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053090 { "refs", "Reference", "ops", "access", },
91 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +020092};
93
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +053094#define C(x) PERF_COUNT_HW_CACHE_##x
95#define CACHE_READ (1 << C(OP_READ))
96#define CACHE_WRITE (1 << C(OP_WRITE))
97#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
98#define COP(x) (1 << x)
99
100/*
101 * cache operartion stat
102 * L1I : Read and prefetch only
103 * ITLB and BPU : Read-only
104 */
105static unsigned long hw_cache_stat[C(MAX)] = {
106 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
107 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
108 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
109 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
110 [C(ITLB)] = (CACHE_READ),
111 [C(BPU)] = (CACHE_READ),
112};
113
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300114#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400115 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300116 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400117 (strcmp(sys_dirent.d_name, ".")) && \
118 (strcmp(sys_dirent.d_name, "..")))
119
Peter Zijlstraae07b632009-08-06 16:48:54 +0200120static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
121{
122 char evt_path[MAXPATHLEN];
123 int fd;
124
125 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
126 sys_dir->d_name, evt_dir->d_name);
127 fd = open(evt_path, O_RDONLY);
128 if (fd < 0)
129 return -EINVAL;
130 close(fd);
131
132 return 0;
133}
134
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300135#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400136 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300137 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400138 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200139 (strcmp(evt_dirent.d_name, "..")) && \
140 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400141
142#define MAX_EVENT_LENGTH 30
143
Jason Baron5beeded2009-07-21 14:16:29 -0400144int valid_debugfs_mount(const char *debugfs)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400145{
146 struct statfs st_fs;
147
Jason Baron5beeded2009-07-21 14:16:29 -0400148 if (statfs(debugfs, &st_fs) < 0)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400149 return -ENOENT;
150 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
151 return -ENOENT;
152 return 0;
153}
154
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200155struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400156{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200157 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400158 DIR *sys_dir, *evt_dir;
159 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160 char id_buf[4];
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300161 int sys_dir_fd, fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400162 u64 id;
163 char evt_path[MAXPATHLEN];
164
Jason Baron5beeded2009-07-21 14:16:29 -0400165 if (valid_debugfs_mount(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)
170 goto cleanup;
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300171 sys_dir_fd = dirfd(sys_dir);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400172
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300173 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
174 int dfd = openat(sys_dir_fd, sys_dirent.d_name,
175 O_RDONLY|O_DIRECTORY), evt_dir_fd;
176 if (dfd == -1)
177 continue;
178 evt_dir = fdopendir(dfd);
179 if (!evt_dir) {
180 close(dfd);
181 continue;
182 }
183 evt_dir_fd = dirfd(evt_dir);
184 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
185 snprintf(evt_path, MAXPATHLEN, "%s/id",
Jason Baronf6bdafe2009-07-21 12:20:22 -0400186 evt_dirent.d_name);
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300187 fd = openat(evt_dir_fd, evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400188 if (fd < 0)
189 continue;
190 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
191 close(fd);
192 continue;
193 }
194 close(fd);
195 id = atoll(id_buf);
196 if (id == config) {
197 closedir(evt_dir);
198 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200199 path = calloc(1, sizeof(path));
200 path->system = malloc(MAX_EVENT_LENGTH);
201 if (!path->system) {
202 free(path);
203 return NULL;
204 }
205 path->name = malloc(MAX_EVENT_LENGTH);
206 if (!path->name) {
207 free(path->system);
208 free(path);
209 return NULL;
210 }
211 strncpy(path->system, sys_dirent.d_name,
212 MAX_EVENT_LENGTH);
213 strncpy(path->name, evt_dirent.d_name,
214 MAX_EVENT_LENGTH);
215 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400216 }
217 }
218 closedir(evt_dir);
219 }
220
221cleanup:
222 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200223 return NULL;
224}
225
226#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
227static const char *tracepoint_id_to_name(u64 config)
228{
229 static char buf[TP_PATH_LEN];
230 struct tracepoint_path *path;
231
232 path = tracepoint_id_to_path(config);
233 if (path) {
234 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
235 free(path->name);
236 free(path->system);
237 free(path);
238 } else
239 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
240
241 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400242}
243
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530244static int is_cache_op_valid(u8 cache_type, u8 cache_op)
245{
246 if (hw_cache_stat[cache_type] & COP(cache_op))
247 return 1; /* valid */
248 else
249 return 0; /* invalid */
250}
251
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530252static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
253{
254 static char name[50];
255
256 if (cache_result) {
257 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
258 hw_cache_op[cache_op][0],
259 hw_cache_result[cache_result][0]);
260 } else {
261 sprintf(name, "%s-%s", hw_cache[cache_type][0],
262 hw_cache_op[cache_op][1]);
263 }
264
265 return name;
266}
267
Ingo Molnar83a09442009-08-15 12:26:57 +0200268const char *event_name(int counter)
Ingo Molnar52425192009-05-26 09:17:18 +0200269{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000270 u64 config = attrs[counter].config;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200271 int type = attrs[counter].type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200272
273 return __event_name(type, config);
274}
275
Ingo Molnar83a09442009-08-15 12:26:57 +0200276const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200277{
Ingo Molnar52425192009-05-26 09:17:18 +0200278 static char buf[32];
279
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200280 if (type == PERF_TYPE_RAW) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200281 sprintf(buf, "raw 0x%llx", config);
Ingo Molnar52425192009-05-26 09:17:18 +0200282 return buf;
283 }
284
285 switch (type) {
286 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200287 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200288 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200289 return "unknown-hardware";
290
Ingo Molnar8326f442009-06-05 20:22:46 +0200291 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000292 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200293
294 cache_type = (config >> 0) & 0xff;
295 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
296 return "unknown-ext-hardware-cache-type";
297
298 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200299 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
300 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200301
302 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200303 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
304 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200305
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530306 if (!is_cache_op_valid(cache_type, cache_op))
307 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200308
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530309 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200310 }
311
Ingo Molnar52425192009-05-26 09:17:18 +0200312 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200313 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200314 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200315 return "unknown-software";
316
Jason Baronf6bdafe2009-07-21 12:20:22 -0400317 case PERF_TYPE_TRACEPOINT:
318 return tracepoint_id_to_name(config);
319
Ingo Molnar52425192009-05-26 09:17:18 +0200320 default:
321 break;
322 }
323
324 return "unknown";
325}
326
Ingo Molnar83a09442009-08-15 12:26:57 +0200327static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200328{
329 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000330 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200331
332 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000333 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
334 n = strlen(names[i][j]);
335 if (n > longest && !strncasecmp(*str, names[i][j], n))
336 longest = n;
337 }
338 if (longest > 0) {
339 *str += longest;
340 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200341 }
342 }
343
Ingo Molnar89536452009-06-06 21:04:17 +0200344 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200345}
346
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +0530347static int
Paul Mackerras61c45982009-07-01 13:04:34 +1000348parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200349{
Paul Mackerras61c45982009-07-01 13:04:34 +1000350 const char *s = *str;
351 int cache_type = -1, cache_op = -1, cache_result = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200352
Paul Mackerras61c45982009-07-01 13:04:34 +1000353 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200354 /*
355 * No fallback - if we cannot get a clear cache type
356 * then bail out:
357 */
358 if (cache_type == -1)
Paul Mackerras61c45982009-07-01 13:04:34 +1000359 return 0;
Ingo Molnar8326f442009-06-05 20:22:46 +0200360
Paul Mackerras61c45982009-07-01 13:04:34 +1000361 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
362 ++s;
363
364 if (cache_op == -1) {
365 cache_op = parse_aliases(&s, hw_cache_op,
366 PERF_COUNT_HW_CACHE_OP_MAX);
367 if (cache_op >= 0) {
368 if (!is_cache_op_valid(cache_type, cache_op))
369 return 0;
370 continue;
371 }
372 }
373
374 if (cache_result == -1) {
375 cache_result = parse_aliases(&s, hw_cache_result,
376 PERF_COUNT_HW_CACHE_RESULT_MAX);
377 if (cache_result >= 0)
378 continue;
379 }
380
381 /*
382 * Can't parse this as a cache op or result, so back up
383 * to the '-'.
384 */
385 --s;
386 break;
387 }
388
Ingo Molnar8326f442009-06-05 20:22:46 +0200389 /*
390 * Fall back to reads:
391 */
Ingo Molnar89536452009-06-06 21:04:17 +0200392 if (cache_op == -1)
393 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200394
Ingo Molnar8326f442009-06-05 20:22:46 +0200395 /*
396 * Fall back to accesses:
397 */
398 if (cache_result == -1)
399 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
400
401 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
402 attr->type = PERF_TYPE_HW_CACHE;
403
Paul Mackerras61c45982009-07-01 13:04:34 +1000404 *str = s;
405 return 1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200406}
407
Jason Baronf6bdafe2009-07-21 12:20:22 -0400408static int parse_tracepoint_event(const char **strp,
409 struct perf_counter_attr *attr)
410{
411 const char *evt_name;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200412 char *flags;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400413 char sys_name[MAX_EVENT_LENGTH];
414 char id_buf[4];
415 int fd;
416 unsigned int sys_length, evt_length;
417 u64 id;
418 char evt_path[MAXPATHLEN];
419
Jason Baron5beeded2009-07-21 14:16:29 -0400420 if (valid_debugfs_mount(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400421 return 0;
422
423 evt_name = strchr(*strp, ':');
424 if (!evt_name)
425 return 0;
426
427 sys_length = evt_name - *strp;
428 if (sys_length >= MAX_EVENT_LENGTH)
429 return 0;
430
431 strncpy(sys_name, *strp, sys_length);
432 sys_name[sys_length] = '\0';
433 evt_name = evt_name + 1;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200434
435 flags = strchr(evt_name, ':');
436 if (flags) {
437 *flags = '\0';
438 flags++;
439 if (!strncmp(flags, "record", strlen(flags)))
440 attr->sample_type |= PERF_SAMPLE_RAW;
441 }
442
Jason Baronf6bdafe2009-07-21 12:20:22 -0400443 evt_length = strlen(evt_name);
444 if (evt_length >= MAX_EVENT_LENGTH)
445 return 0;
446
Jason Baron5beeded2009-07-21 14:16:29 -0400447 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
448 sys_name, evt_name);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400449 fd = open(evt_path, O_RDONLY);
450 if (fd < 0)
451 return 0;
452
453 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
454 close(fd);
455 return 0;
456 }
457 close(fd);
458 id = atoll(id_buf);
459 attr->config = id;
460 attr->type = PERF_TYPE_TRACEPOINT;
461 *strp = evt_name + evt_length;
462 return 1;
463}
464
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530465static int check_events(const char *str, unsigned int i)
466{
Paul Mackerras61c45982009-07-01 13:04:34 +1000467 int n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530468
Paul Mackerras61c45982009-07-01 13:04:34 +1000469 n = strlen(event_symbols[i].symbol);
470 if (!strncmp(str, event_symbols[i].symbol, n))
471 return n;
472
473 n = strlen(event_symbols[i].alias);
474 if (n)
475 if (!strncmp(str, event_symbols[i].alias, n))
476 return n;
477 return 0;
478}
479
480static int
481parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
482{
483 const char *str = *strp;
484 unsigned int i;
485 int n;
486
487 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
488 n = check_events(str, i);
489 if (n > 0) {
490 attr->type = event_symbols[i].type;
491 attr->config = event_symbols[i].config;
492 *strp = str + n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530493 return 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000494 }
495 }
496 return 0;
497}
498
499static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
500{
501 const char *str = *strp;
502 u64 config;
503 int n;
504
505 if (*str != 'r')
506 return 0;
507 n = hex2u64(str + 1, &config);
508 if (n > 0) {
509 *strp = str + n + 1;
510 attr->type = PERF_TYPE_RAW;
511 attr->config = config;
512 return 1;
513 }
514 return 0;
515}
516
517static int
518parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
519{
520 const char *str = *strp;
521 char *endp;
522 unsigned long type;
523 u64 config;
524
525 type = strtoul(str, &endp, 0);
526 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
527 str = endp + 1;
528 config = strtoul(str, &endp, 0);
529 if (endp > str) {
530 attr->type = type;
531 attr->config = config;
532 *strp = endp;
533 return 1;
534 }
535 }
536 return 0;
537}
538
539static int
540parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
541{
542 const char *str = *strp;
543 int eu = 1, ek = 1, eh = 1;
544
545 if (*str++ != ':')
546 return 0;
547 while (*str) {
548 if (*str == 'u')
549 eu = 0;
550 else if (*str == 'k')
551 ek = 0;
552 else if (*str == 'h')
553 eh = 0;
554 else
555 break;
556 ++str;
557 }
558 if (str >= *strp + 2) {
559 *strp = str;
560 attr->exclude_user = eu;
561 attr->exclude_kernel = ek;
562 attr->exclude_hv = eh;
563 return 1;
564 }
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530565 return 0;
566}
567
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200568/*
569 * Each event can have multiple symbolic names.
570 * Symbolic names are (almost) exactly matched.
571 */
Paul Mackerras61c45982009-07-01 13:04:34 +1000572static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200573{
Jason Baronf6bdafe2009-07-21 12:20:22 -0400574 if (!(parse_tracepoint_event(str, attr) ||
575 parse_raw_event(str, attr) ||
Paul Mackerras61c45982009-07-01 13:04:34 +1000576 parse_numeric_event(str, attr) ||
577 parse_symbolic_event(str, attr) ||
578 parse_generic_hw_event(str, attr)))
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200579 return 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200580
Paul Mackerras61c45982009-07-01 13:04:34 +1000581 parse_event_modifier(str, attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200582
Paul Mackerras61c45982009-07-01 13:04:34 +1000583 return 1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200584}
585
Ingo Molnarf37a2912009-07-01 12:37:06 +0200586int parse_events(const struct option *opt __used, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200587{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200588 struct perf_counter_attr attr;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200589
Paul Mackerras61c45982009-07-01 13:04:34 +1000590 for (;;) {
591 if (nr_counters == MAX_COUNTERS)
592 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200593
Paul Mackerras61c45982009-07-01 13:04:34 +1000594 memset(&attr, 0, sizeof(attr));
595 if (!parse_event_symbols(&str, &attr))
596 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200597
Paul Mackerras61c45982009-07-01 13:04:34 +1000598 if (!(*str == 0 || *str == ',' || isspace(*str)))
599 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200600
Paul Mackerras61c45982009-07-01 13:04:34 +1000601 attrs[nr_counters] = attr;
602 nr_counters++;
603
604 if (*str == 0)
605 break;
606 if (*str == ',')
607 ++str;
608 while (isspace(*str))
609 ++str;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200610 }
611
612 return 0;
613}
614
Thomas Gleixner86847b62009-06-06 12:24:17 +0200615static const char * const event_type_descriptors[] = {
616 "",
617 "Hardware event",
618 "Software event",
619 "Tracepoint event",
620 "Hardware cache event",
621};
622
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200623/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400624 * Print the events from <debugfs_mount_point>/tracing/events
625 */
626
627static void print_tracepoint_events(void)
628{
629 DIR *sys_dir, *evt_dir;
630 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300631 int sys_dir_fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400632 char evt_path[MAXPATHLEN];
633
Jason Baron5beeded2009-07-21 14:16:29 -0400634 if (valid_debugfs_mount(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400635 return;
636
Jason Baron5beeded2009-07-21 14:16:29 -0400637 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400638 if (!sys_dir)
639 goto cleanup;
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300640 sys_dir_fd = dirfd(sys_dir);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400641
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300642 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
643 int dfd = openat(sys_dir_fd, sys_dirent.d_name,
644 O_RDONLY|O_DIRECTORY), evt_dir_fd;
645 if (dfd == -1)
646 continue;
647 evt_dir = fdopendir(dfd);
648 if (!evt_dir) {
649 close(dfd);
650 continue;
651 }
652 evt_dir_fd = dirfd(evt_dir);
653 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Jason Baronf6bdafe2009-07-21 12:20:22 -0400654 snprintf(evt_path, MAXPATHLEN, "%s:%s",
655 sys_dirent.d_name, evt_dirent.d_name);
656 fprintf(stderr, " %-40s [%s]\n", evt_path,
657 event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
658 }
659 closedir(evt_dir);
660 }
661
662cleanup:
663 closedir(sys_dir);
664}
665
666/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200667 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200668 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200669void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200670{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200671 struct event_symbol *syms = event_symbols;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530672 unsigned int i, type, op, prev_type = -1;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530673 char name[40];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200674
Thomas Gleixner86847b62009-06-06 12:24:17 +0200675 fprintf(stderr, "\n");
676 fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200677
Thomas Gleixner86847b62009-06-06 12:24:17 +0200678 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
679 type = syms->type + 1;
Roel Kluin23cdb5d2009-07-13 02:25:47 +0200680 if (type >= ARRAY_SIZE(event_type_descriptors))
Thomas Gleixner86847b62009-06-06 12:24:17 +0200681 type = 0;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200682
Thomas Gleixner86847b62009-06-06 12:24:17 +0200683 if (type != prev_type)
684 fprintf(stderr, "\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200685
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530686 if (strlen(syms->alias))
687 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
688 else
689 strcpy(name, syms->symbol);
690 fprintf(stderr, " %-40s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +0200691 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200692
Thomas Gleixner86847b62009-06-06 12:24:17 +0200693 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200694 }
695
Thomas Gleixner86847b62009-06-06 12:24:17 +0200696 fprintf(stderr, "\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530697 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
698 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
699 /* skip invalid cache type */
700 if (!is_cache_op_valid(type, op))
701 continue;
702
703 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
704 fprintf(stderr, " %-40s [%s]\n",
705 event_cache_name(type, op, i),
706 event_type_descriptors[4]);
707 }
708 }
709 }
710
711 fprintf(stderr, "\n");
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530712 fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200713 "rNNN");
714 fprintf(stderr, "\n");
715
Jason Baronf6bdafe2009-07-21 12:20:22 -0400716 print_tracepoint_events();
717
Thomas Gleixner86847b62009-06-06 12:24:17 +0200718 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200719}