blob: 586ab3fe60f87e16acb34426a52177964e0704f0 [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 Melo361c99a2011-01-11 20:56:53 -02004#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02005#include "evsel.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +02006#include "parse-options.h"
7#include "parse-events.h"
8#include "exec_cmd.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03009#include "string.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030010#include "symbol.h"
Jason Baron5beeded2009-07-21 14:16:29 -040011#include "cache.h"
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020012#include "header.h"
Clark Williams549104f2009-11-08 09:03:07 -060013#include "debugfs.h"
Ingo Molnar8ad8db32009-05-26 11:10:09 +020014
Ingo Molnar8ad8db32009-05-26 11:10:09 +020015struct 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
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053028#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
29#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020030
Ingo Molnara21ca2c2009-06-06 09:58:57 +020031static struct event_symbol event_symbols[] = {
Ingo Molnar129c04c2011-04-29 14:41:28 +020032 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
33 { CHW(STALLED_CYCLES_FRONTEND), "stalled-cycles-frontend", "idle-cycles-frontend" },
34 { CHW(STALLED_CYCLES_BACKEND), "stalled-cycles-backend", "idle-cycles-backend" },
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
Ingo Molnar129c04c2011-04-29 14:41:28 +020042 { CSW(CPU_CLOCK), "cpu-clock", "" },
43 { CSW(TASK_CLOCK), "task-clock", "" },
44 { CSW(PAGE_FAULTS), "page-faults", "faults" },
45 { 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" },
49 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
50 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020051};
52
Ingo Molnarcdd6c482009-09-21 12:02:48 +020053#define __PERF_EVENT_FIELD(config, name) \
54 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020055
Ingo Molnar1fc570a2011-04-27 05:20:22 +020056#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020057#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +020058#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020059#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020060
Ingo Molnard3d1e862011-04-29 13:49:08 +020061static const char *hw_event_names[PERF_COUNT_HW_MAX] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020062 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020063 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020064 "cache-references",
65 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020066 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020067 "branch-misses",
68 "bus-cycles",
Ingo Molnard3d1e862011-04-29 13:49:08 +020069 "stalled-cycles-frontend",
70 "stalled-cycles-backend",
Ingo Molnar52425192009-05-26 09:17:18 +020071};
72
Ingo Molnard3d1e862011-04-29 13:49:08 +020073static const char *sw_event_names[PERF_COUNT_SW_MAX] = {
Ingo Molnar749141d2011-04-27 04:24:57 +020074 "cpu-clock",
75 "task-clock",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020076 "page-faults",
77 "context-switches",
78 "CPU-migrations",
79 "minor-faults",
80 "major-faults",
Anton Blanchardf7d79862009-10-18 01:09:29 +000081 "alignment-faults",
82 "emulation-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020083};
84
Ingo Molnar8326f442009-06-05 20:22:46 +020085#define MAX_ALIASES 8
86
Jiri Olsa01119192011-07-13 22:58:18 +020087static const char *hw_cache[PERF_COUNT_HW_CACHE_MAX][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100088 { "L1-dcache", "l1-d", "l1d", "L1-data", },
89 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jiri Olsa01119192011-07-13 22:58:18 +020090 { "LLC", "L2", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053091 { "dTLB", "d-tlb", "Data-TLB", },
92 { "iTLB", "i-tlb", "Instruction-TLB", },
93 { "branch", "branches", "bpu", "btb", "bpc", },
Jiri Olsa01119192011-07-13 22:58:18 +020094 { "node", },
Ingo Molnar8326f442009-06-05 20:22:46 +020095};
96
Jiri Olsa01119192011-07-13 22:58:18 +020097static const char *hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][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
Jiri Olsa01119192011-07-13 22:58:18 +0200103static const char *hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
104 [MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530105 { "refs", "Reference", "ops", "access", },
106 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200107};
108
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530109#define C(x) PERF_COUNT_HW_CACHE_##x
110#define CACHE_READ (1 << C(OP_READ))
111#define CACHE_WRITE (1 << C(OP_WRITE))
112#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
113#define COP(x) (1 << x)
114
115/*
116 * cache operartion stat
117 * L1I : Read and prefetch only
118 * ITLB and BPU : Read-only
119 */
120static unsigned long hw_cache_stat[C(MAX)] = {
121 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
122 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
123 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
124 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
125 [C(ITLB)] = (CACHE_READ),
126 [C(BPU)] = (CACHE_READ),
Jiri Olsa01119192011-07-13 22:58:18 +0200127 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530128};
129
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300130#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400131 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300132 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400133 (strcmp(sys_dirent.d_name, ".")) && \
134 (strcmp(sys_dirent.d_name, "..")))
135
Peter Zijlstraae07b632009-08-06 16:48:54 +0200136static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
137{
138 char evt_path[MAXPATHLEN];
139 int fd;
140
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200141 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200142 sys_dir->d_name, evt_dir->d_name);
143 fd = open(evt_path, O_RDONLY);
144 if (fd < 0)
145 return -EINVAL;
146 close(fd);
147
148 return 0;
149}
150
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300151#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400152 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300153 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400154 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200155 (strcmp(evt_dirent.d_name, "..")) && \
156 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400157
Li Zefan270bbbe2009-09-17 16:34:51 +0800158#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400159
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200161struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400162{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200163 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400164 DIR *sys_dir, *evt_dir;
165 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400166 char id_buf[4];
Eric Dumazet725b1362009-09-24 15:39:09 +0200167 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400168 u64 id;
169 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200170 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400171
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200172 if (debugfs_valid_mountpoint(tracing_events_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200173 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400174
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200175 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400176 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200177 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400178
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300179 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200180
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200181 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200182 sys_dirent.d_name);
183 evt_dir = opendir(dir_path);
184 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300185 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200186
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300187 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200188
189 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400190 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200191 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400192 if (fd < 0)
193 continue;
194 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
195 close(fd);
196 continue;
197 }
198 close(fd);
199 id = atoll(id_buf);
200 if (id == config) {
201 closedir(evt_dir);
202 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100203 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200204 path->system = malloc(MAX_EVENT_LENGTH);
205 if (!path->system) {
206 free(path);
207 return NULL;
208 }
209 path->name = malloc(MAX_EVENT_LENGTH);
210 if (!path->name) {
211 free(path->system);
212 free(path);
213 return NULL;
214 }
215 strncpy(path->system, sys_dirent.d_name,
216 MAX_EVENT_LENGTH);
217 strncpy(path->name, evt_dirent.d_name,
218 MAX_EVENT_LENGTH);
219 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400220 }
221 }
222 closedir(evt_dir);
223 }
224
Jason Baronf6bdafe2009-07-21 12:20:22 -0400225 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200226 return NULL;
227}
228
229#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
230static const char *tracepoint_id_to_name(u64 config)
231{
232 static char buf[TP_PATH_LEN];
233 struct tracepoint_path *path;
234
235 path = tracepoint_id_to_path(config);
236 if (path) {
237 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
238 free(path->name);
239 free(path->system);
240 free(path);
241 } else
242 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
243
244 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400245}
246
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530247static int is_cache_op_valid(u8 cache_type, u8 cache_op)
248{
249 if (hw_cache_stat[cache_type] & COP(cache_op))
250 return 1; /* valid */
251 else
252 return 0; /* invalid */
253}
254
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530255static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
256{
257 static char name[50];
258
259 if (cache_result) {
260 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
261 hw_cache_op[cache_op][0],
262 hw_cache_result[cache_result][0]);
263 } else {
264 sprintf(name, "%s-%s", hw_cache[cache_type][0],
265 hw_cache_op[cache_op][1]);
266 }
267
268 return name;
269}
270
David Ahern1424dc92011-03-09 22:23:28 -0700271const char *event_type(int type)
272{
273 switch (type) {
274 case PERF_TYPE_HARDWARE:
275 return "hardware";
276
277 case PERF_TYPE_SOFTWARE:
278 return "software";
279
280 case PERF_TYPE_TRACEPOINT:
281 return "tracepoint";
282
283 case PERF_TYPE_HW_CACHE:
284 return "hardware-cache";
285
286 default:
287 break;
288 }
289
290 return "unknown";
291}
292
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200293const char *event_name(struct perf_evsel *evsel)
Ingo Molnar52425192009-05-26 09:17:18 +0200294{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200295 u64 config = evsel->attr.config;
296 int type = evsel->attr.type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200297
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200298 if (evsel->name)
299 return evsel->name;
300
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200301 return __event_name(type, config);
302}
303
Ingo Molnar83a09442009-08-15 12:26:57 +0200304const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200305{
Ingo Molnar52425192009-05-26 09:17:18 +0200306 static char buf[32];
307
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200308 if (type == PERF_TYPE_RAW) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200309 sprintf(buf, "raw 0x%" PRIx64, config);
Ingo Molnar52425192009-05-26 09:17:18 +0200310 return buf;
311 }
312
313 switch (type) {
314 case PERF_TYPE_HARDWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200315 if (config < PERF_COUNT_HW_MAX && hw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200316 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200317 return "unknown-hardware";
318
Ingo Molnar8326f442009-06-05 20:22:46 +0200319 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000320 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200321
322 cache_type = (config >> 0) & 0xff;
323 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
324 return "unknown-ext-hardware-cache-type";
325
326 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200327 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
328 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200329
330 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200331 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
332 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200333
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530334 if (!is_cache_op_valid(cache_type, cache_op))
335 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200336
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530337 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200338 }
339
Ingo Molnar52425192009-05-26 09:17:18 +0200340 case PERF_TYPE_SOFTWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200341 if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200342 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200343 return "unknown-software";
344
Jason Baronf6bdafe2009-07-21 12:20:22 -0400345 case PERF_TYPE_TRACEPOINT:
346 return tracepoint_id_to_name(config);
347
Ingo Molnar52425192009-05-26 09:17:18 +0200348 default:
349 break;
350 }
351
352 return "unknown";
353}
354
Ingo Molnar83a09442009-08-15 12:26:57 +0200355static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200356{
357 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000358 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200359
360 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000361 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
362 n = strlen(names[i][j]);
363 if (n > longest && !strncasecmp(*str, names[i][j], n))
364 longest = n;
365 }
366 if (longest > 0) {
367 *str += longest;
368 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200369 }
370 }
371
Ingo Molnar89536452009-06-06 21:04:17 +0200372 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200373}
374
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200375static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200376parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
Ingo Molnar8326f442009-06-05 20:22:46 +0200377{
Paul Mackerras61c45982009-07-01 13:04:34 +1000378 const char *s = *str;
379 int cache_type = -1, cache_op = -1, cache_result = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200380
Paul Mackerras61c45982009-07-01 13:04:34 +1000381 cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200382 /*
383 * No fallback - if we cannot get a clear cache type
384 * then bail out:
385 */
386 if (cache_type == -1)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200387 return EVT_FAILED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200388
Paul Mackerras61c45982009-07-01 13:04:34 +1000389 while ((cache_op == -1 || cache_result == -1) && *s == '-') {
390 ++s;
391
392 if (cache_op == -1) {
393 cache_op = parse_aliases(&s, hw_cache_op,
394 PERF_COUNT_HW_CACHE_OP_MAX);
395 if (cache_op >= 0) {
396 if (!is_cache_op_valid(cache_type, cache_op))
Jiri Olsa01119192011-07-13 22:58:18 +0200397 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000398 continue;
399 }
400 }
401
402 if (cache_result == -1) {
403 cache_result = parse_aliases(&s, hw_cache_result,
404 PERF_COUNT_HW_CACHE_RESULT_MAX);
405 if (cache_result >= 0)
406 continue;
407 }
408
409 /*
410 * Can't parse this as a cache op or result, so back up
411 * to the '-'.
412 */
413 --s;
414 break;
415 }
416
Ingo Molnar8326f442009-06-05 20:22:46 +0200417 /*
418 * Fall back to reads:
419 */
Ingo Molnar89536452009-06-06 21:04:17 +0200420 if (cache_op == -1)
421 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200422
Ingo Molnar8326f442009-06-05 20:22:46 +0200423 /*
424 * Fall back to accesses:
425 */
426 if (cache_result == -1)
427 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
428
429 attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
430 attr->type = PERF_TYPE_HW_CACHE;
431
Paul Mackerras61c45982009-07-01 13:04:34 +1000432 *str = s;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200433 return EVT_HANDLED;
Ingo Molnar8326f442009-06-05 20:22:46 +0200434}
435
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200436static enum event_result
437parse_single_tracepoint_event(char *sys_name,
438 const char *evt_name,
439 unsigned int evt_length,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200440 struct perf_event_attr *attr,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200441 const char **strp)
442{
443 char evt_path[MAXPATHLEN];
444 char id_buf[4];
445 u64 id;
446 int fd;
447
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200448 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200449 sys_name, evt_name);
450
451 fd = open(evt_path, O_RDONLY);
452 if (fd < 0)
453 return EVT_FAILED;
454
455 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
456 close(fd);
457 return EVT_FAILED;
458 }
459
460 close(fd);
461 id = atoll(id_buf);
462 attr->config = id;
463 attr->type = PERF_TYPE_TRACEPOINT;
Corey Ashford4c635a42010-11-30 14:27:01 -0800464 *strp += strlen(sys_name) + evt_length + 1; /* + 1 for the ':' */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200465
Stephane Eranian5710fca2010-04-21 18:06:01 +0200466 attr->sample_type |= PERF_SAMPLE_RAW;
467 attr->sample_type |= PERF_SAMPLE_TIME;
468 attr->sample_type |= PERF_SAMPLE_CPU;
469
470 attr->sample_period = 1;
471
472
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200473 return EVT_HANDLED;
474}
475
476/* sys + ':' + event + ':' + flags*/
477#define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
478static enum event_result
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200479parse_multiple_tracepoint_event(struct perf_evlist *evlist, char *sys_name,
Arnaldo Carvalho de Melo17ea1b72011-01-17 14:40:46 -0200480 const char *evt_exp, char *flags)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200481{
482 char evt_path[MAXPATHLEN];
483 struct dirent *evt_ent;
484 DIR *evt_dir;
485
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200486 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200487 evt_dir = opendir(evt_path);
488
489 if (!evt_dir) {
490 perror("Can't open event dir");
491 return EVT_FAILED;
492 }
493
494 while ((evt_ent = readdir(evt_dir))) {
495 char event_opt[MAX_EVOPT_LEN + 1];
496 int len;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200497
498 if (!strcmp(evt_ent->d_name, ".")
499 || !strcmp(evt_ent->d_name, "..")
500 || !strcmp(evt_ent->d_name, "enable")
501 || !strcmp(evt_ent->d_name, "filter"))
502 continue;
503
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500504 if (!strglobmatch(evt_ent->d_name, evt_exp))
505 continue;
506
Ulrich Drepper180570f2009-12-06 13:25:30 -0500507 len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s%s%s", sys_name,
508 evt_ent->d_name, flags ? ":" : "",
509 flags ?: "");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200510 if (len < 0)
511 return EVT_FAILED;
512
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200513 if (parse_events(evlist, event_opt, 0))
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200514 return EVT_FAILED;
515 }
516
517 return EVT_HANDLED_ALL;
518}
519
Arnaldo Carvalho de Melo17ea1b72011-01-17 14:40:46 -0200520static enum event_result
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200521parse_tracepoint_event(struct perf_evlist *evlist, const char **strp,
Arnaldo Carvalho de Melo17ea1b72011-01-17 14:40:46 -0200522 struct perf_event_attr *attr)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400523{
524 const char *evt_name;
Corey Ashford4c635a42010-11-30 14:27:01 -0800525 char *flags = NULL, *comma_loc;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400526 char sys_name[MAX_EVENT_LENGTH];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400527 unsigned int sys_length, evt_length;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400528
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200529 if (debugfs_valid_mountpoint(tracing_events_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400530 return 0;
531
532 evt_name = strchr(*strp, ':');
533 if (!evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200534 return EVT_FAILED;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400535
536 sys_length = evt_name - *strp;
537 if (sys_length >= MAX_EVENT_LENGTH)
538 return 0;
539
540 strncpy(sys_name, *strp, sys_length);
541 sys_name[sys_length] = '\0';
542 evt_name = evt_name + 1;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200543
Corey Ashford4c635a42010-11-30 14:27:01 -0800544 comma_loc = strchr(evt_name, ',');
545 if (comma_loc) {
546 /* take the event name up to the comma */
547 evt_name = strndup(evt_name, comma_loc - evt_name);
548 }
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200549 flags = strchr(evt_name, ':');
550 if (flags) {
Ingo Molnar1fc35b22009-09-13 09:44:29 +0200551 /* split it out: */
552 evt_name = strndup(evt_name, flags - evt_name);
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200553 flags++;
Frederic Weisbecker3a9f1312009-08-13 10:27:18 +0200554 }
555
Jason Baronf6bdafe2009-07-21 12:20:22 -0400556 evt_length = strlen(evt_name);
557 if (evt_length >= MAX_EVENT_LENGTH)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200558 return EVT_FAILED;
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500559 if (strpbrk(evt_name, "*?")) {
Arnaldo Carvalho de Melodd9a9ad2011-01-17 14:25:06 -0200560 *strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200561 return parse_multiple_tracepoint_event(evlist, sys_name,
562 evt_name, flags);
Han Pingtianf006d252011-01-06 17:39:22 +0800563 } else {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200564 return parse_single_tracepoint_event(sys_name, evt_name,
Frederic Weisbeckerbdef3b02010-04-14 20:05:17 +0200565 evt_length, attr, strp);
Han Pingtianf006d252011-01-06 17:39:22 +0800566 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400567}
568
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100569static enum event_result
570parse_breakpoint_type(const char *type, const char **strp,
571 struct perf_event_attr *attr)
572{
573 int i;
574
575 for (i = 0; i < 3; i++) {
576 if (!type[i])
577 break;
578
579 switch (type[i]) {
580 case 'r':
581 attr->bp_type |= HW_BREAKPOINT_R;
582 break;
583 case 'w':
584 attr->bp_type |= HW_BREAKPOINT_W;
585 break;
586 case 'x':
587 attr->bp_type |= HW_BREAKPOINT_X;
588 break;
589 default:
590 return EVT_FAILED;
591 }
592 }
593 if (!attr->bp_type) /* Default */
594 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
595
596 *strp = type + i;
597
598 return EVT_HANDLED;
599}
600
601static enum event_result
602parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
603{
604 const char *target;
605 const char *type;
606 char *endaddr;
607 u64 addr;
608 enum event_result err;
609
610 target = strchr(*strp, ':');
611 if (!target)
612 return EVT_FAILED;
613
614 if (strncmp(*strp, "mem", target - *strp) != 0)
615 return EVT_FAILED;
616
617 target++;
618
619 addr = strtoull(target, &endaddr, 0);
620 if (target == endaddr)
621 return EVT_FAILED;
622
623 attr->bp_addr = addr;
624 *strp = endaddr;
625
626 type = strchr(target, ':');
627
628 /* If no type is defined, just rw as default */
629 if (!type) {
630 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
631 } else {
632 err = parse_breakpoint_type(++type, strp, attr);
633 if (err == EVT_FAILED)
634 return EVT_FAILED;
635 }
636
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200637 /*
638 * We should find a nice way to override the access length
639 * Provide some defaults for now
640 */
641 if (attr->bp_type == HW_BREAKPOINT_X)
642 attr->bp_len = sizeof(long);
643 else
644 attr->bp_len = HW_BREAKPOINT_LEN_4;
645
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100646 attr->type = PERF_TYPE_BREAKPOINT;
647
648 return EVT_HANDLED;
649}
650
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530651static int check_events(const char *str, unsigned int i)
652{
Paul Mackerras61c45982009-07-01 13:04:34 +1000653 int n;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530654
Paul Mackerras61c45982009-07-01 13:04:34 +1000655 n = strlen(event_symbols[i].symbol);
Ingo Molnarb908deb2011-04-27 03:55:40 +0200656 if (!strncasecmp(str, event_symbols[i].symbol, n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000657 return n;
658
659 n = strlen(event_symbols[i].alias);
Ingo Molnarb908deb2011-04-27 03:55:40 +0200660 if (n) {
661 if (!strncasecmp(str, event_symbols[i].alias, n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000662 return n;
Ingo Molnarb908deb2011-04-27 03:55:40 +0200663 }
664
Paul Mackerras61c45982009-07-01 13:04:34 +1000665 return 0;
666}
667
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200668static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200669parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000670{
671 const char *str = *strp;
672 unsigned int i;
673 int n;
674
675 for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
676 n = check_events(str, i);
677 if (n > 0) {
678 attr->type = event_symbols[i].type;
679 attr->config = event_symbols[i].config;
680 *strp = str + n;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200681 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000682 }
683 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200684 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000685}
686
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200687static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200688parse_raw_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000689{
690 const char *str = *strp;
691 u64 config;
692 int n;
693
694 if (*str != 'r')
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200695 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000696 n = hex2u64(str + 1, &config);
697 if (n > 0) {
Stephane Eranian777d1d72011-07-23 04:10:43 +0200698 const char *end = str + n + 1;
699 if (*end != '\0' && *end != ',' && *end != ':')
700 return EVT_FAILED;
701
702 *strp = end;
Paul Mackerras61c45982009-07-01 13:04:34 +1000703 attr->type = PERF_TYPE_RAW;
704 attr->config = config;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200705 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000706 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200707 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000708}
709
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200710static enum event_result
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200711parse_numeric_event(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000712{
713 const char *str = *strp;
714 char *endp;
715 unsigned long type;
716 u64 config;
717
718 type = strtoul(str, &endp, 0);
719 if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
720 str = endp + 1;
721 config = strtoul(str, &endp, 0);
722 if (endp > str) {
723 attr->type = type;
724 attr->config = config;
725 *strp = endp;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200726 return EVT_HANDLED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000727 }
728 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200729 return EVT_FAILED;
Paul Mackerras61c45982009-07-01 13:04:34 +1000730}
731
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200732static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200733parse_event_modifier(const char **strp, struct perf_event_attr *attr)
Paul Mackerras61c45982009-07-01 13:04:34 +1000734{
735 const char *str = *strp;
Peter Zijlstraab608342010-04-08 23:03:20 +0200736 int exclude = 0;
737 int eu = 0, ek = 0, eh = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000738
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200739 if (!*str)
Paul Mackerras61c45982009-07-01 13:04:34 +1000740 return 0;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200741
Stephane Eranian94692342011-05-17 15:36:19 +0200742 if (*str == ',')
743 return 0;
744
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200745 if (*str++ != ':')
746 return -1;
747
Paul Mackerras61c45982009-07-01 13:04:34 +1000748 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200749 if (*str == 'u') {
750 if (!exclude)
751 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000752 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200753 } else if (*str == 'k') {
754 if (!exclude)
755 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000756 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200757 } else if (*str == 'h') {
758 if (!exclude)
759 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000760 eh = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200761 } else if (*str == 'p') {
762 precise++;
763 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000764 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200765
Paul Mackerras61c45982009-07-01 13:04:34 +1000766 ++str;
767 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200768 if (str < *strp + 2)
769 return -1;
770
771 *strp = str;
772
773 attr->exclude_user = eu;
774 attr->exclude_kernel = ek;
775 attr->exclude_hv = eh;
776 attr->precise_ip = precise;
777
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530778 return 0;
779}
780
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200781/*
782 * Each event can have multiple symbolic names.
783 * Symbolic names are (almost) exactly matched.
784 */
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200785static enum event_result
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200786parse_event_symbols(struct perf_evlist *evlist, const char **str,
Arnaldo Carvalho de Melo17ea1b72011-01-17 14:40:46 -0200787 struct perf_event_attr *attr)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200788{
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200789 enum event_result ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200790
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200791 ret = parse_tracepoint_event(evlist, str, attr);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200792 if (ret != EVT_FAILED)
793 goto modifier;
794
795 ret = parse_raw_event(str, attr);
796 if (ret != EVT_FAILED)
797 goto modifier;
798
799 ret = parse_numeric_event(str, attr);
800 if (ret != EVT_FAILED)
801 goto modifier;
802
803 ret = parse_symbolic_event(str, attr);
804 if (ret != EVT_FAILED)
805 goto modifier;
806
807 ret = parse_generic_hw_event(str, attr);
808 if (ret != EVT_FAILED)
809 goto modifier;
810
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100811 ret = parse_breakpoint_event(str, attr);
812 if (ret != EVT_FAILED)
813 goto modifier;
814
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000815 fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
816 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200817 return EVT_FAILED;
818
819modifier:
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200820 if (parse_event_modifier(str, attr) < 0) {
821 fprintf(stderr, "invalid event modifier: '%s'\n", *str);
822 fprintf(stderr, "Run 'perf list' for a list of valid events and modifiers\n");
823
824 return EVT_FAILED;
825 }
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200826
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200827 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200828}
829
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200830int parse_events(struct perf_evlist *evlist , const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200831{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200832 struct perf_event_attr attr;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200833 enum event_result ret;
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200834 const char *ostr;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200835
Paul Mackerras61c45982009-07-01 13:04:34 +1000836 for (;;) {
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200837 ostr = str;
Paul Mackerras61c45982009-07-01 13:04:34 +1000838 memset(&attr, 0, sizeof(attr));
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200839 ret = parse_event_symbols(evlist, &str, &attr);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200840 if (ret == EVT_FAILED)
Paul Mackerras61c45982009-07-01 13:04:34 +1000841 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200842
Paul Mackerras61c45982009-07-01 13:04:34 +1000843 if (!(*str == 0 || *str == ',' || isspace(*str)))
844 return -1;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200845
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200846 if (ret != EVT_HANDLED_ALL) {
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200847 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200848 evsel = perf_evsel__new(&attr, evlist->nr_entries);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200849 if (evsel == NULL)
850 return -1;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200851 perf_evlist__add(evlist, evsel);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200852
853 evsel->name = calloc(str - ostr + 1, 1);
854 if (!evsel->name)
855 return -1;
856 strncpy(evsel->name, ostr, str - ostr);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200857 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000858
859 if (*str == 0)
860 break;
861 if (*str == ',')
862 ++str;
863 while (isspace(*str))
864 ++str;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200865 }
866
867 return 0;
868}
869
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200870int parse_events_option(const struct option *opt, const char *str,
871 int unset __used)
872{
873 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
874 return parse_events(evlist, str, unset);
875}
876
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200877int parse_filter(const struct option *opt, const char *str,
Li Zefanc171b552009-10-15 11:22:07 +0800878 int unset __used)
879{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200880 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200881 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800882
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200883 if (evlist->nr_entries > 0)
884 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200885
886 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800887 fprintf(stderr,
888 "-F option should follow a -e tracepoint option\n");
889 return -1;
890 }
891
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200892 last->filter = strdup(str);
893 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800894 fprintf(stderr, "not enough memory to hold filter string\n");
895 return -1;
896 }
Li Zefanc171b552009-10-15 11:22:07 +0800897
898 return 0;
899}
900
Thomas Gleixner86847b62009-06-06 12:24:17 +0200901static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200902 "Hardware event",
903 "Software event",
904 "Tracepoint event",
905 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800906 "Raw hardware event descriptor",
907 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200908};
909
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200910/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400911 * Print the events from <debugfs_mount_point>/tracing/events
912 */
913
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200914void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400915{
916 DIR *sys_dir, *evt_dir;
917 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400918 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200919 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400920
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200921 if (debugfs_valid_mountpoint(tracing_events_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400922 return;
923
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200924 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400925 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200926 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400927
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300928 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200929 if (subsys_glob != NULL &&
930 !strglobmatch(sys_dirent.d_name, subsys_glob))
931 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200932
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200933 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200934 sys_dirent.d_name);
935 evt_dir = opendir(dir_path);
936 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300937 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200938
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300939 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200940 if (event_glob != NULL &&
941 !strglobmatch(evt_dirent.d_name, event_glob))
942 continue;
943
Jason Baronf6bdafe2009-07-21 12:20:22 -0400944 snprintf(evt_path, MAXPATHLEN, "%s:%s",
945 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200946 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800947 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400948 }
949 closedir(evt_dir);
950 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400951 closedir(sys_dir);
952}
953
954/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100955 * Check whether event is in <debugfs_mount_point>/tracing/events
956 */
957
958int is_valid_tracepoint(const char *event_string)
959{
960 DIR *sys_dir, *evt_dir;
961 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
962 char evt_path[MAXPATHLEN];
963 char dir_path[MAXPATHLEN];
964
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200965 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +0100966 return 0;
967
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200968 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100969 if (!sys_dir)
970 return 0;
971
972 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
973
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200974 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +0100975 sys_dirent.d_name);
976 evt_dir = opendir(dir_path);
977 if (!evt_dir)
978 continue;
979
980 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
981 snprintf(evt_path, MAXPATHLEN, "%s:%s",
982 sys_dirent.d_name, evt_dirent.d_name);
983 if (!strcmp(evt_path, event_string)) {
984 closedir(evt_dir);
985 closedir(sys_dir);
986 return 1;
987 }
988 }
989 closedir(evt_dir);
990 }
991 closedir(sys_dir);
992 return 0;
993}
994
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200995void print_events_type(u8 type)
996{
997 struct event_symbol *syms = event_symbols;
998 unsigned int i;
999 char name[64];
1000
1001 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
1002 if (type != syms->type)
1003 continue;
1004
1005 if (strlen(syms->alias))
1006 snprintf(name, sizeof(name), "%s OR %s",
1007 syms->symbol, syms->alias);
1008 else
1009 snprintf(name, sizeof(name), "%s", syms->symbol);
1010
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001011 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001012 event_type_descriptors[type]);
1013 }
1014}
1015
1016int print_hwcache_events(const char *event_glob)
1017{
1018 unsigned int type, op, i, printed = 0;
1019
1020 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1021 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1022 /* skip invalid cache type */
1023 if (!is_cache_op_valid(type, op))
1024 continue;
1025
1026 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1027 char *name = event_cache_name(type, op, i);
1028
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001029 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001030 continue;
1031
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001032 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001033 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1034 ++printed;
1035 }
1036 }
1037 }
1038
1039 return printed;
1040}
1041
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001042#define MAX_NAME_LEN 100
1043
Thomas Renninger20c457b2011-01-03 17:50:45 +01001044/*
Thomas Gleixner86847b62009-06-06 12:24:17 +02001045 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001046 */
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001047void print_events(const char *event_glob)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001048{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001049 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001050 struct event_symbol *syms = event_symbols;
1051 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001052
Marti Raudsepp689d3012009-10-27 00:33:05 +00001053 printf("\n");
1054 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001055
Thomas Gleixner86847b62009-06-06 12:24:17 +02001056 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +08001057 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001058
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001059 if (type != prev_type && printed) {
Marti Raudsepp689d3012009-10-27 00:33:05 +00001060 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001061 printed = 0;
1062 ntypes_printed++;
1063 }
1064
1065 if (event_glob != NULL &&
1066 !(strglobmatch(syms->symbol, event_glob) ||
1067 (syms->alias && strglobmatch(syms->alias, event_glob))))
1068 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001069
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301070 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001071 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301072 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001073 strncpy(name, syms->symbol, MAX_NAME_LEN);
1074 printf(" %-50s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +02001075 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001076
Thomas Gleixner86847b62009-06-06 12:24:17 +02001077 prev_type = type;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001078 ++printed;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001079 }
1080
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001081 if (ntypes_printed) {
1082 printed = 0;
1083 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301084 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001085 print_hwcache_events(event_glob);
1086
1087 if (event_glob != NULL)
1088 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301089
Marti Raudsepp689d3012009-10-27 00:33:05 +00001090 printf("\n");
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001091 printf(" %-50s [%s]\n",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -03001092 "rNNN (see 'perf list --help' on how to encode it)",
1093 event_type_descriptors[PERF_TYPE_RAW]);
Marti Raudsepp689d3012009-10-27 00:33:05 +00001094 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001095
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001096 printf(" %-50s [%s]\n",
Liming Wang41bdcb22009-12-29 16:37:07 +08001097 "mem:<addr>[:access]",
1098 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001099 printf("\n");
1100
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001101 print_tracepoint_events(NULL, NULL);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001102}