blob: fac7d59309b83698cf22829936e86e1c511eb31e [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"
Jiri Olsa89812fc2012-03-15 20:09:15 +010014#include "parse-events-flex.h"
Jiri Olsa5f537a22012-03-15 20:09:18 +010015#include "pmu.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010016
17#define MAX_NAME_LEN 100
Ingo Molnar8ad8db32009-05-26 11:10:09 +020018
Ingo Molnar8ad8db32009-05-26 11:10:09 +020019struct event_symbol {
Ingo Molnar83a09442009-08-15 12:26:57 +020020 u8 type;
21 u64 config;
22 const char *symbol;
23 const char *alias;
Ingo Molnar8ad8db32009-05-26 11:10:09 +020024};
25
Jiri Olsa82ba1f22012-05-21 09:12:50 +020026#ifdef PARSER_DEBUG
27extern int parse_events_debug;
28#endif
Jiri Olsab847cbd2012-05-21 09:12:51 +020029int parse_events_parse(struct list_head *list, int *idx);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020030
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053031#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
32#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020033
Ingo Molnara21ca2c2009-06-06 09:58:57 +020034static struct event_symbol event_symbols[] = {
Ingo Molnar129c04c2011-04-29 14:41:28 +020035 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
36 { CHW(STALLED_CYCLES_FRONTEND), "stalled-cycles-frontend", "idle-cycles-frontend" },
37 { CHW(STALLED_CYCLES_BACKEND), "stalled-cycles-backend", "idle-cycles-backend" },
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", "" },
Stephane Eranianf1ac18a2011-12-11 00:28:54 +010044 { CHW(REF_CPU_CYCLES), "ref-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020045
Ingo Molnar129c04c2011-04-29 14:41:28 +020046 { CSW(CPU_CLOCK), "cpu-clock", "" },
47 { CSW(TASK_CLOCK), "task-clock", "" },
48 { CSW(PAGE_FAULTS), "page-faults", "faults" },
49 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
50 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
51 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
52 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
53 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
54 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020055};
56
Ingo Molnarcdd6c482009-09-21 12:02:48 +020057#define __PERF_EVENT_FIELD(config, name) \
58 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020059
Ingo Molnar1fc570a2011-04-27 05:20:22 +020060#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020061#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +020062#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020063#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020064
Ingo Molnard3d1e862011-04-29 13:49:08 +020065static const char *hw_event_names[PERF_COUNT_HW_MAX] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020066 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020067 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020068 "cache-references",
69 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020070 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020071 "branch-misses",
72 "bus-cycles",
Ingo Molnard3d1e862011-04-29 13:49:08 +020073 "stalled-cycles-frontend",
74 "stalled-cycles-backend",
Stephane Eranianf1ac18a2011-12-11 00:28:54 +010075 "ref-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020076};
77
Ingo Molnard3d1e862011-04-29 13:49:08 +020078static const char *sw_event_names[PERF_COUNT_SW_MAX] = {
Ingo Molnar749141d2011-04-27 04:24:57 +020079 "cpu-clock",
80 "task-clock",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020081 "page-faults",
82 "context-switches",
83 "CPU-migrations",
84 "minor-faults",
85 "major-faults",
Anton Blanchardf7d79862009-10-18 01:09:29 +000086 "alignment-faults",
87 "emulation-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020088};
89
Ingo Molnar8326f442009-06-05 20:22:46 +020090#define MAX_ALIASES 8
91
Jiri Olsa01119192011-07-13 22:58:18 +020092static const char *hw_cache[PERF_COUNT_HW_CACHE_MAX][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100093 { "L1-dcache", "l1-d", "l1d", "L1-data", },
94 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jiri Olsa01119192011-07-13 22:58:18 +020095 { "LLC", "L2", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053096 { "dTLB", "d-tlb", "Data-TLB", },
97 { "iTLB", "i-tlb", "Instruction-TLB", },
98 { "branch", "branches", "bpu", "btb", "bpc", },
Jiri Olsa01119192011-07-13 22:58:18 +020099 { "node", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200100};
101
Jiri Olsa01119192011-07-13 22:58:18 +0200102static const char *hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530103 { "load", "loads", "read", },
104 { "store", "stores", "write", },
105 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200106};
107
Jiri Olsa01119192011-07-13 22:58:18 +0200108static const char *hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
109 [MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530110 { "refs", "Reference", "ops", "access", },
111 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200112};
113
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530114#define C(x) PERF_COUNT_HW_CACHE_##x
115#define CACHE_READ (1 << C(OP_READ))
116#define CACHE_WRITE (1 << C(OP_WRITE))
117#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
118#define COP(x) (1 << x)
119
120/*
121 * cache operartion stat
122 * L1I : Read and prefetch only
123 * ITLB and BPU : Read-only
124 */
125static unsigned long hw_cache_stat[C(MAX)] = {
126 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
127 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
128 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
129 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
130 [C(ITLB)] = (CACHE_READ),
131 [C(BPU)] = (CACHE_READ),
Jiri Olsa01119192011-07-13 22:58:18 +0200132 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530133};
134
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300135#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400136 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300137 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400138 (strcmp(sys_dirent.d_name, ".")) && \
139 (strcmp(sys_dirent.d_name, "..")))
140
Peter Zijlstraae07b632009-08-06 16:48:54 +0200141static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
142{
143 char evt_path[MAXPATHLEN];
144 int fd;
145
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200146 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200147 sys_dir->d_name, evt_dir->d_name);
148 fd = open(evt_path, O_RDONLY);
149 if (fd < 0)
150 return -EINVAL;
151 close(fd);
152
153 return 0;
154}
155
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300156#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400157 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300158 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400159 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200160 (strcmp(evt_dirent.d_name, "..")) && \
161 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400162
Li Zefan270bbbe2009-09-17 16:34:51 +0800163#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400164
Jason Baronf6bdafe2009-07-21 12:20:22 -0400165
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200166struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400167{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200168 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400169 DIR *sys_dir, *evt_dir;
170 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100171 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200172 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400173 u64 id;
174 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200175 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400176
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200177 if (debugfs_valid_mountpoint(tracing_events_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200178 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400179
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200180 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400181 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200182 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400183
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300184 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200185
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200186 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200187 sys_dirent.d_name);
188 evt_dir = opendir(dir_path);
189 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300190 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200191
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300192 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200193
194 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400195 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200196 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400197 if (fd < 0)
198 continue;
199 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
200 close(fd);
201 continue;
202 }
203 close(fd);
204 id = atoll(id_buf);
205 if (id == config) {
206 closedir(evt_dir);
207 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100208 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200209 path->system = malloc(MAX_EVENT_LENGTH);
210 if (!path->system) {
211 free(path);
212 return NULL;
213 }
214 path->name = malloc(MAX_EVENT_LENGTH);
215 if (!path->name) {
216 free(path->system);
217 free(path);
218 return NULL;
219 }
220 strncpy(path->system, sys_dirent.d_name,
221 MAX_EVENT_LENGTH);
222 strncpy(path->name, evt_dirent.d_name,
223 MAX_EVENT_LENGTH);
224 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400225 }
226 }
227 closedir(evt_dir);
228 }
229
Jason Baronf6bdafe2009-07-21 12:20:22 -0400230 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200231 return NULL;
232}
233
234#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
235static const char *tracepoint_id_to_name(u64 config)
236{
237 static char buf[TP_PATH_LEN];
238 struct tracepoint_path *path;
239
240 path = tracepoint_id_to_path(config);
241 if (path) {
242 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
243 free(path->name);
244 free(path->system);
245 free(path);
246 } else
247 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
248
249 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400250}
251
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530252static int is_cache_op_valid(u8 cache_type, u8 cache_op)
253{
254 if (hw_cache_stat[cache_type] & COP(cache_op))
255 return 1; /* valid */
256 else
257 return 0; /* invalid */
258}
259
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530260static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
261{
262 static char name[50];
263
264 if (cache_result) {
265 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
266 hw_cache_op[cache_op][0],
267 hw_cache_result[cache_result][0]);
268 } else {
269 sprintf(name, "%s-%s", hw_cache[cache_type][0],
270 hw_cache_op[cache_op][1]);
271 }
272
273 return name;
274}
275
David Ahern1424dc92011-03-09 22:23:28 -0700276const char *event_type(int type)
277{
278 switch (type) {
279 case PERF_TYPE_HARDWARE:
280 return "hardware";
281
282 case PERF_TYPE_SOFTWARE:
283 return "software";
284
285 case PERF_TYPE_TRACEPOINT:
286 return "tracepoint";
287
288 case PERF_TYPE_HW_CACHE:
289 return "hardware-cache";
290
291 default:
292 break;
293 }
294
295 return "unknown";
296}
297
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200298const char *event_name(struct perf_evsel *evsel)
Ingo Molnar52425192009-05-26 09:17:18 +0200299{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200300 u64 config = evsel->attr.config;
301 int type = evsel->attr.type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200302
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200303 if (evsel->name)
304 return evsel->name;
305
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200306 return __event_name(type, config);
307}
308
Ingo Molnar83a09442009-08-15 12:26:57 +0200309const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200310{
Ingo Molnar52425192009-05-26 09:17:18 +0200311 static char buf[32];
312
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200313 if (type == PERF_TYPE_RAW) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200314 sprintf(buf, "raw 0x%" PRIx64, config);
Ingo Molnar52425192009-05-26 09:17:18 +0200315 return buf;
316 }
317
318 switch (type) {
319 case PERF_TYPE_HARDWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200320 if (config < PERF_COUNT_HW_MAX && hw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200321 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200322 return "unknown-hardware";
323
Ingo Molnar8326f442009-06-05 20:22:46 +0200324 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000325 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200326
327 cache_type = (config >> 0) & 0xff;
328 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
329 return "unknown-ext-hardware-cache-type";
330
331 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200332 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
333 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200334
335 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200336 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
337 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200338
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530339 if (!is_cache_op_valid(cache_type, cache_op))
340 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200341
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530342 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200343 }
344
Ingo Molnar52425192009-05-26 09:17:18 +0200345 case PERF_TYPE_SOFTWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200346 if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200347 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200348 return "unknown-software";
349
Jason Baronf6bdafe2009-07-21 12:20:22 -0400350 case PERF_TYPE_TRACEPOINT:
351 return tracepoint_id_to_name(config);
352
Ingo Molnar52425192009-05-26 09:17:18 +0200353 default:
354 break;
355 }
356
357 return "unknown";
358}
359
Jiri Olsab847cbd2012-05-21 09:12:51 +0200360static int add_event(struct list_head **_list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100361 struct perf_event_attr *attr, char *name)
362{
363 struct perf_evsel *evsel;
Jiri Olsab847cbd2012-05-21 09:12:51 +0200364 struct list_head *list = *_list;
365
366 if (!list) {
367 list = malloc(sizeof(*list));
368 if (!list)
369 return -ENOMEM;
370 INIT_LIST_HEAD(list);
371 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100372
373 event_attr_init(attr);
374
375 evsel = perf_evsel__new(attr, (*idx)++);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200376 if (!evsel) {
377 free(list);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100378 return -ENOMEM;
Jiri Olsab847cbd2012-05-21 09:12:51 +0200379 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100380
381 evsel->name = strdup(name);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200382 list_add_tail(&evsel->node, list);
383 *_list = list;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100384 return 0;
385}
386
387static int parse_aliases(char *str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200388{
389 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000390 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200391
392 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000393 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
394 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100395 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000396 longest = n;
397 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100398 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000399 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200400 }
401
Ingo Molnar89536452009-06-06 21:04:17 +0200402 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200403}
404
Jiri Olsab847cbd2012-05-21 09:12:51 +0200405int parse_events_add_cache(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100406 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200407{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100408 struct perf_event_attr attr;
409 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000410 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100411 char *op_result[2] = { op_result1, op_result2 };
412 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200413
Ingo Molnar8326f442009-06-05 20:22:46 +0200414 /*
415 * No fallback - if we cannot get a clear cache type
416 * then bail out:
417 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100418 cache_type = parse_aliases(type, hw_cache,
419 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200420 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100421 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200422
Jiri Olsa89812fc2012-03-15 20:09:15 +0100423 n = snprintf(name, MAX_NAME_LEN, "%s", type);
424
425 for (i = 0; (i < 2) && (op_result[i]); i++) {
426 char *str = op_result[i];
427
428 snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000429
430 if (cache_op == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100431 cache_op = parse_aliases(str, hw_cache_op,
432 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000433 if (cache_op >= 0) {
434 if (!is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100435 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000436 continue;
437 }
438 }
439
440 if (cache_result == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100441 cache_result = parse_aliases(str, hw_cache_result,
Paul Mackerras61c45982009-07-01 13:04:34 +1000442 PERF_COUNT_HW_CACHE_RESULT_MAX);
443 if (cache_result >= 0)
444 continue;
445 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000446 }
447
Ingo Molnar8326f442009-06-05 20:22:46 +0200448 /*
449 * Fall back to reads:
450 */
Ingo Molnar89536452009-06-06 21:04:17 +0200451 if (cache_op == -1)
452 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200453
Ingo Molnar8326f442009-06-05 20:22:46 +0200454 /*
455 * Fall back to accesses:
456 */
457 if (cache_result == -1)
458 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
459
Jiri Olsa89812fc2012-03-15 20:09:15 +0100460 memset(&attr, 0, sizeof(attr));
461 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
462 attr.type = PERF_TYPE_HW_CACHE;
463 return add_event(list, idx, &attr, name);
Ingo Molnar8326f442009-06-05 20:22:46 +0200464}
465
Jiri Olsab847cbd2012-05-21 09:12:51 +0200466static int add_tracepoint(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100467 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200468{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100469 struct perf_event_attr attr;
470 char name[MAX_NAME_LEN];
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200471 char evt_path[MAXPATHLEN];
472 char id_buf[4];
473 u64 id;
474 int fd;
475
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200476 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200477 sys_name, evt_name);
478
479 fd = open(evt_path, O_RDONLY);
480 if (fd < 0)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100481 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200482
483 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
484 close(fd);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100485 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200486 }
487
488 close(fd);
489 id = atoll(id_buf);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200490
Jiri Olsa89812fc2012-03-15 20:09:15 +0100491 memset(&attr, 0, sizeof(attr));
492 attr.config = id;
493 attr.type = PERF_TYPE_TRACEPOINT;
494 attr.sample_type |= PERF_SAMPLE_RAW;
495 attr.sample_type |= PERF_SAMPLE_TIME;
496 attr.sample_type |= PERF_SAMPLE_CPU;
497 attr.sample_period = 1;
Stephane Eranian5710fca2010-04-21 18:06:01 +0200498
Jiri Olsa89812fc2012-03-15 20:09:15 +0100499 snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
500 return add_event(list, idx, &attr, name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200501}
502
Jiri Olsab847cbd2012-05-21 09:12:51 +0200503static int add_tracepoint_multi(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100504 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200505{
506 char evt_path[MAXPATHLEN];
507 struct dirent *evt_ent;
508 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100509 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200510
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200511 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200512 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200513 if (!evt_dir) {
514 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100515 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200516 }
517
Jiri Olsa89812fc2012-03-15 20:09:15 +0100518 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200519 if (!strcmp(evt_ent->d_name, ".")
520 || !strcmp(evt_ent->d_name, "..")
521 || !strcmp(evt_ent->d_name, "enable")
522 || !strcmp(evt_ent->d_name, "filter"))
523 continue;
524
Jiri Olsa89812fc2012-03-15 20:09:15 +0100525 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500526 continue;
527
Jiri Olsa89812fc2012-03-15 20:09:15 +0100528 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200529 }
530
Jiri Olsa89812fc2012-03-15 20:09:15 +0100531 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200532}
533
Jiri Olsab847cbd2012-05-21 09:12:51 +0200534int parse_events_add_tracepoint(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100535 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400536{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100537 int ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400538
Jiri Olsa89812fc2012-03-15 20:09:15 +0100539 ret = debugfs_valid_mountpoint(tracing_events_path);
540 if (ret)
541 return ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400542
Jiri Olsa89812fc2012-03-15 20:09:15 +0100543 return strpbrk(event, "*?") ?
544 add_tracepoint_multi(list, idx, sys, event) :
545 add_tracepoint(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400546}
547
Jiri Olsa89812fc2012-03-15 20:09:15 +0100548static int
549parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100550{
551 int i;
552
553 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100554 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100555 break;
556
557 switch (type[i]) {
558 case 'r':
559 attr->bp_type |= HW_BREAKPOINT_R;
560 break;
561 case 'w':
562 attr->bp_type |= HW_BREAKPOINT_W;
563 break;
564 case 'x':
565 attr->bp_type |= HW_BREAKPOINT_X;
566 break;
567 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100568 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100569 }
570 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100571
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100572 if (!attr->bp_type) /* Default */
573 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
574
Jiri Olsa89812fc2012-03-15 20:09:15 +0100575 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100576}
577
Jiri Olsab847cbd2012-05-21 09:12:51 +0200578int parse_events_add_breakpoint(struct list_head **list, int *idx,
Jiri Olsa89812fc2012-03-15 20:09:15 +0100579 void *ptr, char *type)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100580{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100581 struct perf_event_attr attr;
582 char name[MAX_NAME_LEN];
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100583
Jiri Olsa89812fc2012-03-15 20:09:15 +0100584 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100585 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100586
Jiri Olsa89812fc2012-03-15 20:09:15 +0100587 if (parse_breakpoint_type(type, &attr))
588 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100589
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200590 /*
591 * We should find a nice way to override the access length
592 * Provide some defaults for now
593 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100594 if (attr.bp_type == HW_BREAKPOINT_X)
595 attr.bp_len = sizeof(long);
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200596 else
Jiri Olsa89812fc2012-03-15 20:09:15 +0100597 attr.bp_len = HW_BREAKPOINT_LEN_4;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200598
Jiri Olsa89812fc2012-03-15 20:09:15 +0100599 attr.type = PERF_TYPE_BREAKPOINT;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100600
Jiri Olsa89812fc2012-03-15 20:09:15 +0100601 snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
602 return add_event(list, idx, &attr, name);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100603}
604
Jiri Olsa8f707d82012-03-15 20:09:16 +0100605static int config_term(struct perf_event_attr *attr,
606 struct parse_events__term *term)
607{
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200608#define CHECK_TYPE_VAL(type) \
609do { \
610 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
611 return -EINVAL; \
612} while (0)
613
614 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100615 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200616 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100617 attr->config = term->val.num;
618 break;
619 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200620 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100621 attr->config1 = term->val.num;
622 break;
623 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200624 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100625 attr->config2 = term->val.num;
626 break;
627 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200628 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100629 attr->sample_period = term->val.num;
630 break;
631 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
632 /*
633 * TODO uncomment when the field is available
634 * attr->branch_sample_type = term->val.num;
635 */
636 break;
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200637 case PARSE_EVENTS__TERM_TYPE_NAME:
638 CHECK_TYPE_VAL(STR);
639 break;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100640 default:
641 return -EINVAL;
642 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200643
Jiri Olsa8f707d82012-03-15 20:09:16 +0100644 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200645#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100646}
647
648static int config_attr(struct perf_event_attr *attr,
649 struct list_head *head, int fail)
650{
651 struct parse_events__term *term;
652
653 list_for_each_entry(term, head, list)
654 if (config_term(attr, term) && fail)
655 return -EINVAL;
656
657 return 0;
658}
659
Jiri Olsab847cbd2012-05-21 09:12:51 +0200660int parse_events_add_numeric(struct list_head **list, int *idx,
Jiri Olsa8f707d82012-03-15 20:09:16 +0100661 unsigned long type, unsigned long config,
662 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530663{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100664 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530665
Jiri Olsa89812fc2012-03-15 20:09:15 +0100666 memset(&attr, 0, sizeof(attr));
667 attr.type = type;
668 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100669
670 if (head_config &&
671 config_attr(&attr, head_config, 1))
672 return -EINVAL;
673
Jiri Olsa89812fc2012-03-15 20:09:15 +0100674 return add_event(list, idx, &attr,
675 (char *) __event_name(type, config));
Paul Mackerras61c45982009-07-01 13:04:34 +1000676}
677
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200678static int parse_events__is_name_term(struct parse_events__term *term)
679{
680 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
681}
682
683static char *pmu_event_name(struct perf_event_attr *attr,
684 struct list_head *head_terms)
685{
686 struct parse_events__term *term;
687
688 list_for_each_entry(term, head_terms, list)
689 if (parse_events__is_name_term(term))
690 return term->val.str;
691
692 return (char *) __event_name(PERF_TYPE_RAW, attr->config);
693}
694
Jiri Olsab847cbd2012-05-21 09:12:51 +0200695int parse_events_add_pmu(struct list_head **list, int *idx,
Jiri Olsa5f537a22012-03-15 20:09:18 +0100696 char *name, struct list_head *head_config)
697{
698 struct perf_event_attr attr;
699 struct perf_pmu *pmu;
700
701 pmu = perf_pmu__find(name);
702 if (!pmu)
703 return -EINVAL;
704
705 memset(&attr, 0, sizeof(attr));
706
707 /*
708 * Configure hardcoded terms first, no need to check
709 * return value when called with fail == 0 ;)
710 */
711 config_attr(&attr, head_config, 0);
712
713 if (perf_pmu__config(pmu, &attr, head_config))
714 return -EINVAL;
715
Jiri Olsa6b5fc392012-05-21 09:12:53 +0200716 return add_event(list, idx, &attr,
717 pmu_event_name(&attr, head_config));
Jiri Olsa5f537a22012-03-15 20:09:18 +0100718}
719
Jiri Olsa5d7be902012-03-20 19:15:40 +0100720void parse_events_update_lists(struct list_head *list_event,
721 struct list_head *list_all)
722{
723 /*
724 * Called for single event definition. Update the
725 * 'all event' list, and reinit the 'signle event'
726 * list, for next event definition.
727 */
728 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200729 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100730}
731
Jiri Olsa89812fc2012-03-15 20:09:15 +0100732int parse_events_modifier(struct list_head *list, char *str)
Paul Mackerras61c45982009-07-01 13:04:34 +1000733{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100734 struct perf_evsel *evsel;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100735 int exclude = 0, exclude_GH = 0;
736 int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000737
Jiri Olsa89812fc2012-03-15 20:09:15 +0100738 if (str == NULL)
Paul Mackerras61c45982009-07-01 13:04:34 +1000739 return 0;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200740
Paul Mackerras61c45982009-07-01 13:04:34 +1000741 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200742 if (*str == 'u') {
743 if (!exclude)
744 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000745 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200746 } else if (*str == 'k') {
747 if (!exclude)
748 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000749 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200750 } else if (*str == 'h') {
751 if (!exclude)
752 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000753 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100754 } else if (*str == 'G') {
755 if (!exclude_GH)
756 exclude_GH = eG = eH = 1;
757 eG = 0;
758 } else if (*str == 'H') {
759 if (!exclude_GH)
760 exclude_GH = eG = eH = 1;
761 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200762 } else if (*str == 'p') {
763 precise++;
764 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000765 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200766
Paul Mackerras61c45982009-07-01 13:04:34 +1000767 ++str;
768 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200769
Jiri Olsa89812fc2012-03-15 20:09:15 +0100770 /*
771 * precise ip:
772 *
773 * 0 - SAMPLE_IP can have arbitrary skid
774 * 1 - SAMPLE_IP must have constant skid
775 * 2 - SAMPLE_IP requested to have 0 skid
776 * 3 - SAMPLE_IP must have 0 skid
777 *
778 * See also PERF_RECORD_MISC_EXACT_IP
779 */
780 if (precise > 3)
781 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200782
Jiri Olsa89812fc2012-03-15 20:09:15 +0100783 list_for_each_entry(evsel, list, node) {
784 evsel->attr.exclude_user = eu;
785 evsel->attr.exclude_kernel = ek;
786 evsel->attr.exclude_hv = eh;
787 evsel->attr.precise_ip = precise;
788 evsel->attr.exclude_host = eH;
789 evsel->attr.exclude_guest = eG;
790 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200791
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530792 return 0;
793}
794
Jiri Olsa89812fc2012-03-15 20:09:15 +0100795int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200796{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100797 LIST_HEAD(list);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100798 LIST_HEAD(list_tmp);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100799 YY_BUFFER_STATE buffer;
800 int ret, idx = evlist->nr_entries;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200801
Jiri Olsa89812fc2012-03-15 20:09:15 +0100802 buffer = parse_events__scan_string(str);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200803
Jiri Olsa82ba1f22012-05-21 09:12:50 +0200804#ifdef PARSER_DEBUG
805 parse_events_debug = 1;
806#endif
Jiri Olsab847cbd2012-05-21 09:12:51 +0200807 ret = parse_events_parse(&list, &idx);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200808
Jiri Olsa89812fc2012-03-15 20:09:15 +0100809 parse_events__flush_buffer(buffer);
810 parse_events__delete_buffer(buffer);
Jiri Olsa08d2f762012-05-21 09:12:52 +0200811 parse_events_lex_destroy();
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200812
Jiri Olsa89812fc2012-03-15 20:09:15 +0100813 if (!ret) {
814 int entries = idx - evlist->nr_entries;
815 perf_evlist__splice_list_tail(evlist, &list, entries);
816 return 0;
817 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200818
Jiri Olsa5d7be902012-03-20 19:15:40 +0100819 /*
820 * There are 2 users - builtin-record and builtin-test objects.
821 * Both call perf_evlist__delete in case of error, so we dont
822 * need to bother.
823 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100824 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000825 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200826 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200827}
828
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200829int parse_events_option(const struct option *opt, const char *str,
830 int unset __used)
831{
832 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
833 return parse_events(evlist, str, unset);
834}
835
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200836int parse_filter(const struct option *opt, const char *str,
Li Zefanc171b552009-10-15 11:22:07 +0800837 int unset __used)
838{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200839 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200840 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800841
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200842 if (evlist->nr_entries > 0)
843 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200844
845 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800846 fprintf(stderr,
847 "-F option should follow a -e tracepoint option\n");
848 return -1;
849 }
850
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200851 last->filter = strdup(str);
852 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800853 fprintf(stderr, "not enough memory to hold filter string\n");
854 return -1;
855 }
Li Zefanc171b552009-10-15 11:22:07 +0800856
857 return 0;
858}
859
Thomas Gleixner86847b62009-06-06 12:24:17 +0200860static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200861 "Hardware event",
862 "Software event",
863 "Tracepoint event",
864 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800865 "Raw hardware event descriptor",
866 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200867};
868
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200869/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400870 * Print the events from <debugfs_mount_point>/tracing/events
871 */
872
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200873void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400874{
875 DIR *sys_dir, *evt_dir;
876 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400877 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200878 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400879
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200880 if (debugfs_valid_mountpoint(tracing_events_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400881 return;
882
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200883 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400884 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200885 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400886
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300887 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200888 if (subsys_glob != NULL &&
889 !strglobmatch(sys_dirent.d_name, subsys_glob))
890 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200891
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200892 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200893 sys_dirent.d_name);
894 evt_dir = opendir(dir_path);
895 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300896 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200897
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300898 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200899 if (event_glob != NULL &&
900 !strglobmatch(evt_dirent.d_name, event_glob))
901 continue;
902
Jason Baronf6bdafe2009-07-21 12:20:22 -0400903 snprintf(evt_path, MAXPATHLEN, "%s:%s",
904 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200905 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800906 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400907 }
908 closedir(evt_dir);
909 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400910 closedir(sys_dir);
911}
912
913/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100914 * Check whether event is in <debugfs_mount_point>/tracing/events
915 */
916
917int is_valid_tracepoint(const char *event_string)
918{
919 DIR *sys_dir, *evt_dir;
920 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
921 char evt_path[MAXPATHLEN];
922 char dir_path[MAXPATHLEN];
923
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200924 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +0100925 return 0;
926
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200927 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100928 if (!sys_dir)
929 return 0;
930
931 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
932
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200933 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +0100934 sys_dirent.d_name);
935 evt_dir = opendir(dir_path);
936 if (!evt_dir)
937 continue;
938
939 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
940 snprintf(evt_path, MAXPATHLEN, "%s:%s",
941 sys_dirent.d_name, evt_dirent.d_name);
942 if (!strcmp(evt_path, event_string)) {
943 closedir(evt_dir);
944 closedir(sys_dir);
945 return 1;
946 }
947 }
948 closedir(evt_dir);
949 }
950 closedir(sys_dir);
951 return 0;
952}
953
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200954void print_events_type(u8 type)
955{
956 struct event_symbol *syms = event_symbols;
957 unsigned int i;
958 char name[64];
959
960 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
961 if (type != syms->type)
962 continue;
963
964 if (strlen(syms->alias))
965 snprintf(name, sizeof(name), "%s OR %s",
966 syms->symbol, syms->alias);
967 else
968 snprintf(name, sizeof(name), "%s", syms->symbol);
969
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200970 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200971 event_type_descriptors[type]);
972 }
973}
974
975int print_hwcache_events(const char *event_glob)
976{
977 unsigned int type, op, i, printed = 0;
978
979 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
980 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
981 /* skip invalid cache type */
982 if (!is_cache_op_valid(type, op))
983 continue;
984
985 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
986 char *name = event_cache_name(type, op, i);
987
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200988 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200989 continue;
990
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200991 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200992 event_type_descriptors[PERF_TYPE_HW_CACHE]);
993 ++printed;
994 }
995 }
996 }
997
998 return printed;
999}
1000
Thomas Renninger20c457b2011-01-03 17:50:45 +01001001/*
Thomas Gleixner86847b62009-06-06 12:24:17 +02001002 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001003 */
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001004void print_events(const char *event_glob)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001005{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001006 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001007 struct event_symbol *syms = event_symbols;
1008 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001009
Marti Raudsepp689d3012009-10-27 00:33:05 +00001010 printf("\n");
1011 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001012
Thomas Gleixner86847b62009-06-06 12:24:17 +02001013 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +08001014 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001015
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001016 if (type != prev_type && printed) {
Marti Raudsepp689d3012009-10-27 00:33:05 +00001017 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001018 printed = 0;
1019 ntypes_printed++;
1020 }
1021
1022 if (event_glob != NULL &&
1023 !(strglobmatch(syms->symbol, event_glob) ||
1024 (syms->alias && strglobmatch(syms->alias, event_glob))))
1025 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001026
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301027 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001028 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301029 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001030 strncpy(name, syms->symbol, MAX_NAME_LEN);
1031 printf(" %-50s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +02001032 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001033
Thomas Gleixner86847b62009-06-06 12:24:17 +02001034 prev_type = type;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001035 ++printed;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001036 }
1037
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001038 if (ntypes_printed) {
1039 printed = 0;
1040 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301041 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001042 print_hwcache_events(event_glob);
1043
1044 if (event_glob != NULL)
1045 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301046
Marti Raudsepp689d3012009-10-27 00:33:05 +00001047 printf("\n");
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001048 printf(" %-50s [%s]\n",
Jiri Olsa5f537a22012-03-15 20:09:18 +01001049 "rNNN",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -03001050 event_type_descriptors[PERF_TYPE_RAW]);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001051 printf(" %-50s [%s]\n",
1052 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1053 event_type_descriptors[PERF_TYPE_RAW]);
1054 printf(" (see 'perf list --help' on how to encode it)\n");
Marti Raudsepp689d3012009-10-27 00:33:05 +00001055 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001056
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001057 printf(" %-50s [%s]\n",
Liming Wang41bdcb22009-12-29 16:37:07 +08001058 "mem:<addr>[:access]",
1059 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001060 printf("\n");
1061
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001062 print_tracepoint_events(NULL, NULL);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001063}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001064
1065int parse_events__is_hardcoded_term(struct parse_events__term *term)
1066{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001067 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001068}
1069
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001070static int new_term(struct parse_events__term **_term, int type_val,
1071 int type_term, char *config,
1072 char *str, long num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001073{
1074 struct parse_events__term *term;
1075
1076 term = zalloc(sizeof(*term));
1077 if (!term)
1078 return -ENOMEM;
1079
1080 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001081 term->type_val = type_val;
1082 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001083 term->config = config;
1084
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001085 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001086 case PARSE_EVENTS__TERM_TYPE_NUM:
1087 term->val.num = num;
1088 break;
1089 case PARSE_EVENTS__TERM_TYPE_STR:
1090 term->val.str = str;
1091 break;
1092 default:
1093 return -EINVAL;
1094 }
1095
1096 *_term = term;
1097 return 0;
1098}
1099
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001100int parse_events__term_num(struct parse_events__term **term,
1101 int type_term, char *config, long num)
1102{
1103 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1104 config, NULL, num);
1105}
1106
1107int parse_events__term_str(struct parse_events__term **term,
1108 int type_term, char *config, char *str)
1109{
1110 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1111 config, str, 0);
1112}
1113
Jiri Olsa8f707d82012-03-15 20:09:16 +01001114void parse_events__free_terms(struct list_head *terms)
1115{
1116 struct parse_events__term *term, *h;
1117
1118 list_for_each_entry_safe(term, h, terms, list)
1119 free(term);
1120
1121 free(terms);
1122}