blob: 135f69baf966d8a2a2525c6f9478958cf2932bf2 [file] [log] [blame]
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001#include "../../../include/linux/hw_breakpoint.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02002#include "util.h"
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -03003#include "../perf.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02004#include "evsel.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02005#include "parse-options.h"
6#include "parse-events.h"
7#include "exec_cmd.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03008#include "string.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03009#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040010#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020011#include "header.h"
Clark Williams549104f2009-11-08 09:03:07 -060012#include "debugfs.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020013
Li Zefanc171b552009-10-15 11:22:07 +080014int nr_counters;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020015
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020016LIST_HEAD(evsel_list);
Ingo Molnar8ad8db32009-05-26 11:10:09 +020017
18struct event_symbol {
Ingo Molnar83a09442009-08-15 12:26:57 +020019 u8 type;
20 u64 config;
21 const char *symbol;
22 const char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020023};
24
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020025enum event_result {
26 EVT_FAILED,
27 EVT_HANDLED,
28 EVT_HANDLED_ALL
29};
30
Jason Baron5beeded2009-07-21 14:16:29 -040031char debugfs_path[MAXPATHLEN];
32
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053033#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
34#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020035
Ingo Molnara21ca2c2009-06-06 09:58:57 +020036static struct event_symbol event_symbols[] = {
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053037 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
38 { CHW(INSTRUCTIONS), "instructions", "" },
39 { CHW(CACHE_REFERENCES), "cache-references", "" },
40 { CHW(CACHE_MISSES), "cache-misses", "" },
41 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
42 { CHW(BRANCH_MISSES), "branch-misses", "" },
43 { CHW(BUS_CYCLES), "bus-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020044
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053045 { CSW(CPU_CLOCK), "cpu-clock", "" },
46 { CSW(TASK_CLOCK), "task-clock", "" },
Jaswinder Singh Rajputc0c22db2009-06-22 20:47:26 +053047 { CSW(PAGE_FAULTS), "page-faults", "faults" },
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +053048 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
49 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
50 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
51 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
Anton Blanchardf7d79862009-10-18 01:09:29 +000052 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
53 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020054};
55
Ingo Molnarcdd6c482009-09-21 12:02:48 +020056#define __PERF_EVENT_FIELD(config, name) \
57 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020058
Ingo Molnarcdd6c482009-09-21 12:02:48 +020059#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
60#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
61#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
62#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020063
Ingo Molnar83a09442009-08-15 12:26:57 +020064static const char *hw_event_names[] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020065 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020066 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020067 "cache-references",
68 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020069 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020070 "branch-misses",
71 "bus-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020072};
73
Ingo Molnar83a09442009-08-15 12:26:57 +020074static const char *sw_event_names[] = {
Ingo Molnar44175b62009-06-13 13:35:00 +020075 "cpu-clock-msecs",
76 "task-clock-msecs",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020077 "page-faults",
78 "context-switches",
79 "CPU-migrations",
80 "minor-faults",
81 "major-faults",
Anton Blanchardf7d79862009-10-18 01:09:29 +000082 "alignment-faults",
83 "emulation-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020084};
85
Ingo Molnar8326f442009-06-05 20:22:46 +020086#define MAX_ALIASES 8
87
Ingo Molnar83a09442009-08-15 12:26:57 +020088static const char *hw_cache[][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100089 { "L1-dcache", "l1-d", "l1d", "L1-data", },
90 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053091 { "LLC", "L2" },
92 { "dTLB", "d-tlb", "Data-TLB", },
93 { "iTLB", "i-tlb", "Instruction-TLB", },
94 { "branch", "branches", "bpu", "btb", "bpc", },
Ingo Molnar8326f442009-06-05 20:22:46 +020095};
96
Ingo Molnar83a09442009-08-15 12:26:57 +020097static const char *hw_cache_op[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053098 { "load", "loads", "read", },
99 { "store", "stores", "write", },
100 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200101};
102
Ingo Molnar83a09442009-08-15 12:26:57 +0200103static const char *hw_cache_result[][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530104 { "refs", "Reference", "ops", "access", },
105 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200106};
107
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530108#define C(x) PERF_COUNT_HW_CACHE_##x
109#define CACHE_READ (1 << C(OP_READ))
110#define CACHE_WRITE (1 << C(OP_WRITE))
111#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
112#define COP(x) (1 << x)
113
114/*
115 * cache operartion stat
116 * L1I : Read and prefetch only
117 * ITLB and BPU : Read-only
118 */
119static unsigned long hw_cache_stat[C(MAX)] = {
120 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
121 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
122 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
123 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
124 [C(ITLB)] = (CACHE_READ),
125 [C(BPU)] = (CACHE_READ),
126};
127
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300128#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400129 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300130 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400131 (strcmp(sys_dirent.d_name, ".")) && \
132 (strcmp(sys_dirent.d_name, "..")))
133
Peter Zijlstraae07b632009-08-06 16:48:54 +0200134static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
135{
136 char evt_path[MAXPATHLEN];
137 int fd;
138
139 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
140 sys_dir->d_name, evt_dir->d_name);
141 fd = open(evt_path, O_RDONLY);
142 if (fd < 0)
143 return -EINVAL;
144 close(fd);
145
146 return 0;
147}
148
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300149#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400150 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300151 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400152 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200153 (strcmp(evt_dirent.d_name, "..")) && \
154 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400155
Li Zefan270bbbe2009-09-17 16:34:51 +0800156#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400157
Jason Baronf6bdafe2009-07-21 12:20:22 -0400158
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200159struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200161 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400162 DIR *sys_dir, *evt_dir;
163 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400164 char id_buf[4];
Eric Dumazet725b1362009-09-24 15:39:09 +0200165 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400166 u64 id;
167 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200168 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400169
Clark Williams549104f2009-11-08 09:03:07 -0600170 if (debugfs_valid_mountpoint(debugfs_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200171 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400172
Jason Baron5beeded2009-07-21 14:16:29 -0400173 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400174 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200175 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400176
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300177 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200178
179 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
180 sys_dirent.d_name);
181 evt_dir = opendir(dir_path);
182 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300183 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200184
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300185 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200186
187 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400188 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200189 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400190 if (fd < 0)
191 continue;
192 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
193 close(fd);
194 continue;
195 }
196 close(fd);
197 id = atoll(id_buf);
198 if (id == config) {
199 closedir(evt_dir);
200 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100201 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200202 path->system = malloc(MAX_EVENT_LENGTH);
203 if (!path->system) {
204 free(path);
205 return NULL;
206 }
207 path->name = malloc(MAX_EVENT_LENGTH);
208 if (!path->name) {
209 free(path->system);
210 free(path);
211 return NULL;
212 }
213 strncpy(path->system, sys_dirent.d_name,
214 MAX_EVENT_LENGTH);
215 strncpy(path->name, evt_dirent.d_name,
216 MAX_EVENT_LENGTH);
217 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400218 }
219 }
220 closedir(evt_dir);
221 }
222
Jason Baronf6bdafe2009-07-21 12:20:22 -0400223 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200224 return NULL;
225}
226
227#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
228static const char *tracepoint_id_to_name(u64 config)
229{
230 static char buf[TP_PATH_LEN];
231 struct tracepoint_path *path;
232
233 path = tracepoint_id_to_path(config);
234 if (path) {
235 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
236 free(path->name);
237 free(path->system);
238 free(path);
239 } else
240 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
241
242 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400243}
244
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530245static int is_cache_op_valid(u8 cache_type, u8 cache_op)
246{
247 if (hw_cache_stat[cache_type] & COP(cache_op))
248 return 1; /* valid */
249 else
250 return 0; /* invalid */
251}
252
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530253static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
254{
255 static char name[50];
256
257 if (cache_result) {
258 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
259 hw_cache_op[cache_op][0],
260 hw_cache_result[cache_result][0]);
261 } else {
262 sprintf(name, "%s-%s", hw_cache[cache_type][0],
263 hw_cache_op[cache_op][1]);
264 }
265
266 return name;
267}
268
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200269const char *event_name(struct perf_evsel *evsel)
Ingo Molnar52425192009-05-26 09:17:18 +0200270{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200271 u64 config = evsel->attr.config;
272 int type = evsel->attr.type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200273
274 return __event_name(type, config);
275}
276
Ingo Molnar83a09442009-08-15 12:26:57 +0200277const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200278{
Ingo Molnar52425192009-05-26 09:17:18 +0200279 static char buf[32];
280
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200281 if (type == PERF_TYPE_RAW) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200282 sprintf(buf, "raw 0x%" PRIx64, config);
Ingo Molnar52425192009-05-26 09:17:18 +0200283 return buf;
284 }
285
286 switch (type) {
287 case PERF_TYPE_HARDWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200288 if (config < PERF_COUNT_HW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200289 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200290 return "unknown-hardware";
291
Ingo Molnar8326f442009-06-05 20:22:46 +0200292 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000293 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200294
295 cache_type = (config >> 0) & 0xff;
296 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
297 return "unknown-ext-hardware-cache-type";
298
299 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200300 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
301 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200302
303 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200304 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
305 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200306
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530307 if (!is_cache_op_valid(cache_type, cache_op))
308 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200309
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530310 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200311 }
312
Ingo Molnar52425192009-05-26 09:17:18 +0200313 case PERF_TYPE_SOFTWARE:
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +0200314 if (config < PERF_COUNT_SW_MAX)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200315 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200316 return "unknown-software";
317
Jason Baronf6bdafe2009-07-21 12:20:22 -0400318 case PERF_TYPE_TRACEPOINT:
319 return tracepoint_id_to_name(config);
320
Ingo Molnar52425192009-05-26 09:17:18 +0200321 default:
322 break;
323 }
324
325 return "unknown";
326}
327
Ingo Molnar83a09442009-08-15 12:26:57 +0200328static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200329{
330 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000331 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200332
333 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000334 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
335 n = strlen(names[i][j]);
336 if (n > longest && !strncasecmp(*str, names[i][j], n))
337 longest = n;
338 }
339 if (longest > 0) {
340 *str += longest;
341 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200342 }
343 }
344
Ingo Molnar89536452009-06-06 21:04:17 +0200345 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200346}
347
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200348static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200349parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200350{
Paul Mackerras61c45982009-07-01 13:04:34 +1000351 const char *s = *str;
352 int cache_type = -1, cache_op = -1, cache_result = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200353
Paul Mackerras61c45982009-07-01 13:04:34 +1000354 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200355 /*
356 * No fallback - if we cannot get a clear cache type
357 * then bail out:
358 */
359 if (cache_type == -1)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200360 return EVT_FAILED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200361
Paul Mackerras61c45982009-07-01 13:04:34 +1000362 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
363 ++s;
364
365 if (cache_op == -1) {
366 cache_op = parse_aliases(&s, hw_cache_op,
367 PERF_COUNT_HW_CACHE_OP_MAX);
368 if (cache_op >= 0) {
369 if (!is_cache_op_valid(cache_type, cache_op))
370 return 0;
371 continue;
372 }
373 }
374
375 if (cache_result == -1) {
376 cache_result = parse_aliases(&s, hw_cache_result,
377 PERF_COUNT_HW_CACHE_RESULT_MAX);
378 if (cache_result >= 0)
379 continue;
380 }
381
382 /*
383 * Can't parse this as a cache op or result, so back up
384 * to the '-'.
385 */
386 --s;
387 break;
388 }
389
Ingo Molnar8326f442009-06-05 20:22:46 +0200390 /*
391 * Fall back to reads:
392 */
Ingo Molnar89536452009-06-06 21:04:17 +0200393 if (cache_op == -1)
394 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200395
Ingo Molnar8326f442009-06-05 20:22:46 +0200396 /*
397 * Fall back to accesses:
398 */
399 if (cache_result == -1)
400 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
401
402 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
403 attr->type = PERF_TYPE_HW_CACHE;
404
Paul Mackerras61c45982009-07-01 13:04:34 +1000405 *str = s;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200406 return EVT_HANDLED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200407}
408
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200409static enum event_result
410parse_single_tracepoint_event(char *sys_name,
411 const char *evt_name,
412 unsigned int evt_length,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200413 struct perf_event_attr *attr,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200414 const char **strp)
415{
416 char evt_path[MAXPATHLEN];
417 char id_buf[4];
418 u64 id;
419 int fd;
420
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200421 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
422 sys_name, evt_name);
423
424 fd = open(evt_path, O_RDONLY);
425 if (fd < 0)
426 return EVT_FAILED;
427
428 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
429 close(fd);
430 return EVT_FAILED;
431 }
432
433 close(fd);
434 id = atoll(id_buf);
435 attr->config = id;
436 attr->type = PERF_TYPE_TRACEPOINT;
Corey Ashford4c635a42010-11-30 14:27:01 -0800437 *strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200438
Stephane Eranian5710fca2010-04-21 18:06:01 +0200439 attr->sample_type |= PERF_SAMPLE_RAW;
440 attr->sample_type |= PERF_SAMPLE_TIME;
441 attr->sample_type |= PERF_SAMPLE_CPU;
442
443 attr->sample_period = 1;
444
445
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200446 return EVT_HANDLED;
447}
448
449/* sys + ':' + event + ':' + flags*/
450#define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
451static enum event_result
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500452parse_multiple_tracepoint_event(char *sys_name, const char *evt_exp,
453 char *flags)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200454{
455 char evt_path[MAXPATHLEN];
456 struct dirent *evt_ent;
457 DIR *evt_dir;
458
459 snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
460 evt_dir = opendir(evt_path);
461
462 if (!evt_dir) {
463 perror("Can't open event dir");
464 return EVT_FAILED;
465 }
466
467 while ((evt_ent = readdir(evt_dir))) {
468 char event_opt[MAX_EVOPT_LEN + 1];
469 int len;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200470
471 if (!strcmp(evt_ent->d_name, ".")
472 || !strcmp(evt_ent->d_name, "..")
473 || !strcmp(evt_ent->d_name, "enable")
474 || !strcmp(evt_ent->d_name, "filter"))
475 continue;
476
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500477 if (!strglobmatch(evt_ent->d_name, evt_exp))
478 continue;
479
Ulrich Drepper180570f2009-12-06 13:25:30 -0500480 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
481 evt_ent->d_name, flags ? ":" : "",
482 flags ?: "");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200483 if (len < 0)
484 return EVT_FAILED;
485
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200486 if (parse_events(NULL, event_opt, 0))
487 return EVT_FAILED;
488 }
489
490 return EVT_HANDLED_ALL;
491}
492
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200493static enum event_result parse_tracepoint_event(const char **strp,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200494 struct perf_event_attr *attr)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400495{
496 const char *evt_name;
Corey Ashford4c635a42010-11-30 14:27:01 -0800497 char *flags = NULL, *comma_loc;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400498 char sys_name[MAX_EVENT_LENGTH];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400499 unsigned int sys_length, evt_length;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400500
Clark Williams549104f2009-11-08 09:03:07 -0600501 if (debugfs_valid_mountpoint(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400502 return 0;
503
504 evt_name = strchr(*strp, ':');
505 if (!evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200506 return EVT_FAILED;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400507
508 sys_length = evt_name - *strp;
509 if (sys_length >= MAX_EVENT_LENGTH)
510 return 0;
511
512 strncpy(sys_name, *strp, sys_length);
513 sys_name[sys_length] = '\0';
514 evt_name = evt_name + 1;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200515
Corey Ashford4c635a42010-11-30 14:27:01 -0800516 comma_loc = strchr(evt_name, ',');
517 if (comma_loc) {
518 /* take the event name up to the comma */
519 evt_name = strndup(evt_name, comma_loc - evt_name);
520 }
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200521 flags = strchr(evt_name, ':');
522 if (flags) {
Ingo Molnar1fc35b22009-09-13 09:44:29 +0200523 /* split it out: */
524 evt_name = strndup(evt_name, flags - evt_name);
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200525 flags++;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200526 }
527
Jason Baronf6bdafe2009-07-21 12:20:22 -0400528 evt_length = strlen(evt_name);
529 if (evt_length >= MAX_EVENT_LENGTH)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200530 return EVT_FAILED;
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500531 if (strpbrk(evt_name, "*?")) {
Arnaldo Carvalho de Melodd9a9ad2011-01-17 14:25:06 -0200532 *strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500533 return parse_multiple_tracepoint_event(sys_name, evt_name,
534 flags);
Han Pingtianf006d252011-01-06 17:39:22 +0800535 } else {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200536 return parse_single_tracepoint_event(sys_name, evt_name,
Frederic Weisbeckerbdef3b02010-04-14 20:05:17 +0200537 evt_length, attr, strp);
Han Pingtianf006d252011-01-06 17:39:22 +0800538 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400539}
540
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100541static enum event_result
542parse_breakpoint_type(const char *type, const char **strp,
543 struct perf_event_attr *attr)
544{
545 int i;
546
547 for (i = 0; i < 3; i++) {
548 if (!type[i])
549 break;
550
551 switch (type[i]) {
552 case 'r':
553 attr->bp_type |= HW_BREAKPOINT_R;
554 break;
555 case 'w':
556 attr->bp_type |= HW_BREAKPOINT_W;
557 break;
558 case 'x':
559 attr->bp_type |= HW_BREAKPOINT_X;
560 break;
561 default:
562 return EVT_FAILED;
563 }
564 }
565 if (!attr->bp_type) /* Default */
566 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
567
568 *strp = type + i;
569
570 return EVT_HANDLED;
571}
572
573static enum event_result
574parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
575{
576 const char *target;
577 const char *type;
578 char *endaddr;
579 u64 addr;
580 enum event_result err;
581
582 target = strchr(*strp, ':');
583 if (!target)
584 return EVT_FAILED;
585
586 if (strncmp(*strp, "mem", target - *strp) != 0)
587 return EVT_FAILED;
588
589 target++;
590
591 addr = strtoull(target, &endaddr, 0);
592 if (target == endaddr)
593 return EVT_FAILED;
594
595 attr->bp_addr = addr;
596 *strp = endaddr;
597
598 type = strchr(target, ':');
599
600 /* If no type is defined, just rw as default */
601 if (!type) {
602 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
603 } else {
604 err = parse_breakpoint_type(++type, strp, attr);
605 if (err == EVT_FAILED)
606 return EVT_FAILED;
607 }
608
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200609 /*
610 * We should find a nice way to override the access length
611 * Provide some defaults for now
612 */
613 if (attr->bp_type == HW_BREAKPOINT_X)
614 attr->bp_len = sizeof(long);
615 else
616 attr->bp_len = HW_BREAKPOINT_LEN_4;
617
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100618 attr->type = PERF_TYPE_BREAKPOINT;
619
620 return EVT_HANDLED;
621}
622
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530623static int check_events(const char *str, unsigned int i)
624{
Paul Mackerras61c45982009-07-01 13:04:34 +1000625 int n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530626
Paul Mackerras61c45982009-07-01 13:04:34 +1000627 n = strlen(event_symbols[i].symbol);
628 if (!strncmp(str, event_symbols[i].symbol, n))
629 return n;
630
631 n = strlen(event_symbols[i].alias);
632 if (n)
633 if (!strncmp(str, event_symbols[i].alias, n))
634 return n;
635 return 0;
636}
637
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200638static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200639parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000640{
641 const char *str = *strp;
642 unsigned int i;
643 int n;
644
645 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
646 n = check_events(str, i);
647 if (n > 0) {
648 attr->type = event_symbols[i].type;
649 attr->config = event_symbols[i].config;
650 *strp = str + n;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200651 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000652 }
653 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200654 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000655}
656
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200657static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200658parse_raw_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000659{
660 const char *str = *strp;
661 u64 config;
662 int n;
663
664 if (*str != 'r')
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200665 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000666 n = hex2u64(str + 1, &config);
667 if (n > 0) {
668 *strp = str + n + 1;
669 attr->type = PERF_TYPE_RAW;
670 attr->config = config;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200671 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000672 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200673 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000674}
675
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200676static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200677parse_numeric_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000678{
679 const char *str = *strp;
680 char *endp;
681 unsigned long type;
682 u64 config;
683
684 type = strtoul(str, &endp, 0);
685 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
686 str = endp + 1;
687 config = strtoul(str, &endp, 0);
688 if (endp > str) {
689 attr->type = type;
690 attr->config = config;
691 *strp = endp;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200692 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000693 }
694 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200695 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000696}
697
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200698static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200699parse_event_modifier(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000700{
701 const char *str = *strp;
Peter Zijlstraab608342010-04-08 23:03:20 +0200702 int exclude = 0;
703 int eu = 0, ek = 0, eh = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000704
705 if (*str++ != ':')
706 return 0;
707 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200708 if (*str == 'u') {
709 if (!exclude)
710 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000711 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200712 } else if (*str == 'k') {
713 if (!exclude)
714 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000715 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200716 } else if (*str == 'h') {
717 if (!exclude)
718 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000719 eh = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200720 } else if (*str == 'p') {
721 precise++;
722 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000723 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200724
Paul Mackerras61c45982009-07-01 13:04:34 +1000725 ++str;
726 }
727 if (str >= *strp + 2) {
728 *strp = str;
729 attr->exclude_user = eu;
730 attr->exclude_kernel = ek;
731 attr->exclude_hv = eh;
Peter Zijlstraab608342010-04-08 23:03:20 +0200732 attr->precise_ip = precise;
Paul Mackerras61c45982009-07-01 13:04:34 +1000733 return 1;
734 }
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530735 return 0;
736}
737
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200738/*
739 * Each event can have multiple symbolic names.
740 * Symbolic names are (almost) exactly matched.
741 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200742static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200743parse_event_symbols(const char **str, struct perf_event_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200744{
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200745 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200746
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200747 ret = parse_tracepoint_event(str, attr);
748 if (ret != EVT_FAILED)
749 goto modifier;
750
751 ret = parse_raw_event(str, attr);
752 if (ret != EVT_FAILED)
753 goto modifier;
754
755 ret = parse_numeric_event(str, attr);
756 if (ret != EVT_FAILED)
757 goto modifier;
758
759 ret = parse_symbolic_event(str, attr);
760 if (ret != EVT_FAILED)
761 goto modifier;
762
763 ret = parse_generic_hw_event(str, attr);
764 if (ret != EVT_FAILED)
765 goto modifier;
766
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100767 ret = parse_breakpoint_event(str, attr);
768 if (ret != EVT_FAILED)
769 goto modifier;
770
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000771 fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
772 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200773 return EVT_FAILED;
774
775modifier:
Paul Mackerras61c45982009-07-01 13:04:34 +1000776 parse_event_modifier(str, attr);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200777
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200778 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200779}
780
Ingo Molnarf37a2912009-07-01 12:37:06 +0200781int parse_events(const struct option *opt __used, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200782{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200783 struct perf_event_attr attr;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200784 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200785
Paul Mackerras61c45982009-07-01 13:04:34 +1000786 for (;;) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000787 memset(&attr, 0, sizeof(attr));
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200788 ret = parse_event_symbols(&str, &attr);
789 if (ret == EVT_FAILED)
Paul Mackerras61c45982009-07-01 13:04:34 +1000790 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200791
Paul Mackerras61c45982009-07-01 13:04:34 +1000792 if (!(*str == 0 || *str == ',' || isspace(*str)))
793 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200794
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200795 if (ret != EVT_HANDLED_ALL) {
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200796 struct perf_evsel *evsel;
Lin Ming23a2f3a2011-01-07 11:11:09 +0800797 evsel = perf_evsel__new(&attr,
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200798 nr_counters);
799 if (evsel == NULL)
800 return -1;
801 list_add_tail(&evsel->node, &evsel_list);
802 ++nr_counters;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200803 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000804
805 if (*str == 0)
806 break;
807 if (*str == ',')
808 ++str;
809 while (isspace(*str))
810 ++str;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200811 }
812
813 return 0;
814}
815
Li Zefanc171b552009-10-15 11:22:07 +0800816int parse_filter(const struct option *opt __used, const char *str,
817 int unset __used)
818{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200819 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800820
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200821 if (!list_empty(&evsel_list))
822 last = list_entry(evsel_list.prev, struct perf_evsel, node);
823
824 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800825 fprintf(stderr,
826 "-F option should follow a -e tracepoint option\n");
827 return -1;
828 }
829
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200830 last->filter = strdup(str);
831 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800832 fprintf(stderr, "not enough memory to hold filter string\n");
833 return -1;
834 }
Li Zefanc171b552009-10-15 11:22:07 +0800835
836 return 0;
837}
838
Thomas Gleixner86847b62009-06-06 12:24:17 +0200839static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200840 "Hardware event",
841 "Software event",
842 "Tracepoint event",
843 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800844 "Raw hardware event descriptor",
845 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200846};
847
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200848/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400849 * Print the events from <debugfs_mount_point>/tracing/events
850 */
851
852static void print_tracepoint_events(void)
853{
854 DIR *sys_dir, *evt_dir;
855 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400856 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200857 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400858
Clark Williams549104f2009-11-08 09:03:07 -0600859 if (debugfs_valid_mountpoint(debugfs_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400860 return;
861
Jason Baron5beeded2009-07-21 14:16:29 -0400862 sys_dir = opendir(debugfs_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400863 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200864 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400865
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300866 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200867
868 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
869 sys_dirent.d_name);
870 evt_dir = opendir(dir_path);
871 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300872 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200873
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300874 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Jason Baronf6bdafe2009-07-21 12:20:22 -0400875 snprintf(evt_path, MAXPATHLEN, "%s:%s",
876 sys_dirent.d_name, evt_dirent.d_name);
Marti Raudsepp689d3012009-10-27 00:33:05 +0000877 printf(" %-42s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800878 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400879 }
880 closedir(evt_dir);
881 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400882 closedir(sys_dir);
883}
884
885/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100886 * Check whether event is in <debugfs_mount_point>/tracing/events
887 */
888
889int is_valid_tracepoint(const char *event_string)
890{
891 DIR *sys_dir, *evt_dir;
892 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
893 char evt_path[MAXPATHLEN];
894 char dir_path[MAXPATHLEN];
895
896 if (debugfs_valid_mountpoint(debugfs_path))
897 return 0;
898
899 sys_dir = opendir(debugfs_path);
900 if (!sys_dir)
901 return 0;
902
903 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
904
905 snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
906 sys_dirent.d_name);
907 evt_dir = opendir(dir_path);
908 if (!evt_dir)
909 continue;
910
911 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
912 snprintf(evt_path, MAXPATHLEN, "%s:%s",
913 sys_dirent.d_name, evt_dirent.d_name);
914 if (!strcmp(evt_path, event_string)) {
915 closedir(evt_dir);
916 closedir(sys_dir);
917 return 1;
918 }
919 }
920 closedir(evt_dir);
921 }
922 closedir(sys_dir);
923 return 0;
924}
925
926/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200927 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200928 */
Thomas Gleixner86847b62009-06-06 12:24:17 +0200929void print_events(void)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200930{
Thomas Gleixner86847b62009-06-06 12:24:17 +0200931 struct event_symbol *syms = event_symbols;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530932 unsigned int i, type, op, prev_type = -1;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530933 char name[40];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200934
Marti Raudsepp689d3012009-10-27 00:33:05 +0000935 printf("\n");
936 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200937
Thomas Gleixner86847b62009-06-06 12:24:17 +0200938 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +0800939 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200940
Thomas Gleixner86847b62009-06-06 12:24:17 +0200941 if (type != prev_type)
Marti Raudsepp689d3012009-10-27 00:33:05 +0000942 printf("\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200943
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530944 if (strlen(syms->alias))
945 sprintf(name, "%s OR %s", syms->symbol, syms->alias);
946 else
947 strcpy(name, syms->symbol);
Marti Raudsepp689d3012009-10-27 00:33:05 +0000948 printf(" %-42s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +0200949 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200950
Thomas Gleixner86847b62009-06-06 12:24:17 +0200951 prev_type = type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200952 }
953
Marti Raudsepp689d3012009-10-27 00:33:05 +0000954 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530955 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
956 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
957 /* skip invalid cache type */
958 if (!is_cache_op_valid(type, op))
959 continue;
960
961 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
Marti Raudsepp689d3012009-10-27 00:33:05 +0000962 printf(" %-42s [%s]\n",
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530963 event_cache_name(type, op, i),
Liming Wang41bdcb22009-12-29 16:37:07 +0800964 event_type_descriptors[PERF_TYPE_HW_CACHE]);
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +0530965 }
966 }
967 }
968
Marti Raudsepp689d3012009-10-27 00:33:05 +0000969 printf("\n");
Liming Wang41bdcb22009-12-29 16:37:07 +0800970 printf(" %-42s [%s]\n",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -0300971 "rNNN (see 'perf list --help' on how to encode it)",
972 event_type_descriptors[PERF_TYPE_RAW]);
Marti Raudsepp689d3012009-10-27 00:33:05 +0000973 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +0200974
Liming Wang41bdcb22009-12-29 16:37:07 +0800975 printf(" %-42s [%s]\n",
976 "mem:<addr>[:access]",
977 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100978 printf("\n");
979
Jason Baronf6bdafe2009-07-21 12:20:22 -0400980 print_tracepoint_events();
981
Thomas Gleixner86847b62009-06-06 12:24:17 +0200982 exit(129);
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200983}
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200984
985int perf_evsel_list__create_default(void)
986{
Lin Ming23a2f3a2011-01-07 11:11:09 +0800987 struct perf_evsel *evsel;
988 struct perf_event_attr attr;
989
990 memset(&attr, 0, sizeof(attr));
991 attr.type = PERF_TYPE_HARDWARE;
992 attr.config = PERF_COUNT_HW_CPU_CYCLES;
993
994 evsel = perf_evsel__new(&attr, 0);
995
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200996 if (evsel == NULL)
997 return -ENOMEM;
998
999 list_add(&evsel->node, &evsel_list);
1000 ++nr_counters;
1001 return 0;
1002}
Arnaldo Carvalho de Melo70d544d2011-01-03 16:51:39 -02001003
1004void perf_evsel_list__delete(void)
1005{
1006 struct perf_evsel *pos, *n;
1007
1008 list_for_each_entry_safe(pos, n, &evsel_list, node) {
1009 list_del_init(&pos->node);
1010 perf_evsel__delete(pos);
1011 }
1012 nr_counters = 0;
1013}