blob: 4025e18765c40e6a9e99e7e5b9924b76616191ad [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;
637 default:
638 return -EINVAL;
639 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200640
Jiri Olsa8f707d82012-03-15 20:09:16 +0100641 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200642#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100643}
644
645static int config_attr(struct perf_event_attr *attr,
646 struct list_head *head, int fail)
647{
648 struct parse_events__term *term;
649
650 list_for_each_entry(term, head, list)
651 if (config_term(attr, term) && fail)
652 return -EINVAL;
653
654 return 0;
655}
656
Jiri Olsab847cbd2012-05-21 09:12:51 +0200657int parse_events_add_numeric(struct list_head **list, int *idx,
Jiri Olsa8f707d82012-03-15 20:09:16 +0100658 unsigned long type, unsigned long config,
659 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530660{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100661 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530662
Jiri Olsa89812fc2012-03-15 20:09:15 +0100663 memset(&attr, 0, sizeof(attr));
664 attr.type = type;
665 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100666
667 if (head_config &&
668 config_attr(&attr, head_config, 1))
669 return -EINVAL;
670
Jiri Olsa89812fc2012-03-15 20:09:15 +0100671 return add_event(list, idx, &attr,
672 (char *) __event_name(type, config));
Paul Mackerras61c45982009-07-01 13:04:34 +1000673}
674
Jiri Olsab847cbd2012-05-21 09:12:51 +0200675int parse_events_add_pmu(struct list_head **list, int *idx,
Jiri Olsa5f537a22012-03-15 20:09:18 +0100676 char *name, struct list_head *head_config)
677{
678 struct perf_event_attr attr;
679 struct perf_pmu *pmu;
680
681 pmu = perf_pmu__find(name);
682 if (!pmu)
683 return -EINVAL;
684
685 memset(&attr, 0, sizeof(attr));
686
687 /*
688 * Configure hardcoded terms first, no need to check
689 * return value when called with fail == 0 ;)
690 */
691 config_attr(&attr, head_config, 0);
692
693 if (perf_pmu__config(pmu, &attr, head_config))
694 return -EINVAL;
695
696 return add_event(list, idx, &attr, (char *) "pmu");
697}
698
Jiri Olsa5d7be902012-03-20 19:15:40 +0100699void parse_events_update_lists(struct list_head *list_event,
700 struct list_head *list_all)
701{
702 /*
703 * Called for single event definition. Update the
704 * 'all event' list, and reinit the 'signle event'
705 * list, for next event definition.
706 */
707 list_splice_tail(list_event, list_all);
Jiri Olsab847cbd2012-05-21 09:12:51 +0200708 free(list_event);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100709}
710
Jiri Olsa89812fc2012-03-15 20:09:15 +0100711int parse_events_modifier(struct list_head *list, char *str)
Paul Mackerras61c45982009-07-01 13:04:34 +1000712{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100713 struct perf_evsel *evsel;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100714 int exclude = 0, exclude_GH = 0;
715 int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000716
Jiri Olsa89812fc2012-03-15 20:09:15 +0100717 if (str == NULL)
Paul Mackerras61c45982009-07-01 13:04:34 +1000718 return 0;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200719
Paul Mackerras61c45982009-07-01 13:04:34 +1000720 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200721 if (*str == 'u') {
722 if (!exclude)
723 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000724 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200725 } else if (*str == 'k') {
726 if (!exclude)
727 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000728 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200729 } else if (*str == 'h') {
730 if (!exclude)
731 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000732 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100733 } else if (*str == 'G') {
734 if (!exclude_GH)
735 exclude_GH = eG = eH = 1;
736 eG = 0;
737 } else if (*str == 'H') {
738 if (!exclude_GH)
739 exclude_GH = eG = eH = 1;
740 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200741 } else if (*str == 'p') {
742 precise++;
743 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000744 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200745
Paul Mackerras61c45982009-07-01 13:04:34 +1000746 ++str;
747 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200748
Jiri Olsa89812fc2012-03-15 20:09:15 +0100749 /*
750 * precise ip:
751 *
752 * 0 - SAMPLE_IP can have arbitrary skid
753 * 1 - SAMPLE_IP must have constant skid
754 * 2 - SAMPLE_IP requested to have 0 skid
755 * 3 - SAMPLE_IP must have 0 skid
756 *
757 * See also PERF_RECORD_MISC_EXACT_IP
758 */
759 if (precise > 3)
760 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200761
Jiri Olsa89812fc2012-03-15 20:09:15 +0100762 list_for_each_entry(evsel, list, node) {
763 evsel->attr.exclude_user = eu;
764 evsel->attr.exclude_kernel = ek;
765 evsel->attr.exclude_hv = eh;
766 evsel->attr.precise_ip = precise;
767 evsel->attr.exclude_host = eH;
768 evsel->attr.exclude_guest = eG;
769 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200770
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530771 return 0;
772}
773
Jiri Olsa89812fc2012-03-15 20:09:15 +0100774int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200775{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100776 LIST_HEAD(list);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100777 LIST_HEAD(list_tmp);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100778 YY_BUFFER_STATE buffer;
779 int ret, idx = evlist->nr_entries;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200780
Jiri Olsa89812fc2012-03-15 20:09:15 +0100781 buffer = parse_events__scan_string(str);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200782
Jiri Olsa82ba1f22012-05-21 09:12:50 +0200783#ifdef PARSER_DEBUG
784 parse_events_debug = 1;
785#endif
Jiri Olsab847cbd2012-05-21 09:12:51 +0200786 ret = parse_events_parse(&list, &idx);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200787
Jiri Olsa89812fc2012-03-15 20:09:15 +0100788 parse_events__flush_buffer(buffer);
789 parse_events__delete_buffer(buffer);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200790
Jiri Olsa89812fc2012-03-15 20:09:15 +0100791 if (!ret) {
792 int entries = idx - evlist->nr_entries;
793 perf_evlist__splice_list_tail(evlist, &list, entries);
794 return 0;
795 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200796
Jiri Olsa5d7be902012-03-20 19:15:40 +0100797 /*
798 * There are 2 users - builtin-record and builtin-test objects.
799 * Both call perf_evlist__delete in case of error, so we dont
800 * need to bother.
801 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100802 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000803 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200804 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200805}
806
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200807int parse_events_option(const struct option *opt, const char *str,
808 int unset __used)
809{
810 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
811 return parse_events(evlist, str, unset);
812}
813
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200814int parse_filter(const struct option *opt, const char *str,
Li Zefanc171b552009-10-15 11:22:07 +0800815 int unset __used)
816{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200817 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200818 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800819
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200820 if (evlist->nr_entries > 0)
821 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200822
823 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800824 fprintf(stderr,
825 "-F option should follow a -e tracepoint option\n");
826 return -1;
827 }
828
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200829 last->filter = strdup(str);
830 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800831 fprintf(stderr, "not enough memory to hold filter string\n");
832 return -1;
833 }
Li Zefanc171b552009-10-15 11:22:07 +0800834
835 return 0;
836}
837
Thomas Gleixner86847b62009-06-06 12:24:17 +0200838static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200839 "Hardware event",
840 "Software event",
841 "Tracepoint event",
842 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800843 "Raw hardware event descriptor",
844 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200845};
846
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200847/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400848 * Print the events from <debugfs_mount_point>/tracing/events
849 */
850
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200851void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400852{
853 DIR *sys_dir, *evt_dir;
854 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400855 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200856 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400857
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200858 if (debugfs_valid_mountpoint(tracing_events_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400859 return;
860
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200861 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400862 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200863 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400864
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300865 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200866 if (subsys_glob != NULL &&
867 !strglobmatch(sys_dirent.d_name, subsys_glob))
868 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200869
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200870 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200871 sys_dirent.d_name);
872 evt_dir = opendir(dir_path);
873 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300874 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200875
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300876 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200877 if (event_glob != NULL &&
878 !strglobmatch(evt_dirent.d_name, event_glob))
879 continue;
880
Jason Baronf6bdafe2009-07-21 12:20:22 -0400881 snprintf(evt_path, MAXPATHLEN, "%s:%s",
882 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200883 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800884 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400885 }
886 closedir(evt_dir);
887 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400888 closedir(sys_dir);
889}
890
891/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100892 * Check whether event is in <debugfs_mount_point>/tracing/events
893 */
894
895int is_valid_tracepoint(const char *event_string)
896{
897 DIR *sys_dir, *evt_dir;
898 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
899 char evt_path[MAXPATHLEN];
900 char dir_path[MAXPATHLEN];
901
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200902 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +0100903 return 0;
904
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200905 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100906 if (!sys_dir)
907 return 0;
908
909 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
910
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200911 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +0100912 sys_dirent.d_name);
913 evt_dir = opendir(dir_path);
914 if (!evt_dir)
915 continue;
916
917 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
918 snprintf(evt_path, MAXPATHLEN, "%s:%s",
919 sys_dirent.d_name, evt_dirent.d_name);
920 if (!strcmp(evt_path, event_string)) {
921 closedir(evt_dir);
922 closedir(sys_dir);
923 return 1;
924 }
925 }
926 closedir(evt_dir);
927 }
928 closedir(sys_dir);
929 return 0;
930}
931
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200932void print_events_type(u8 type)
933{
934 struct event_symbol *syms = event_symbols;
935 unsigned int i;
936 char name[64];
937
938 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
939 if (type != syms->type)
940 continue;
941
942 if (strlen(syms->alias))
943 snprintf(name, sizeof(name), "%s OR %s",
944 syms->symbol, syms->alias);
945 else
946 snprintf(name, sizeof(name), "%s", syms->symbol);
947
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200948 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200949 event_type_descriptors[type]);
950 }
951}
952
953int print_hwcache_events(const char *event_glob)
954{
955 unsigned int type, op, i, printed = 0;
956
957 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
958 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
959 /* skip invalid cache type */
960 if (!is_cache_op_valid(type, op))
961 continue;
962
963 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
964 char *name = event_cache_name(type, op, i);
965
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200966 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200967 continue;
968
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200969 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200970 event_type_descriptors[PERF_TYPE_HW_CACHE]);
971 ++printed;
972 }
973 }
974 }
975
976 return printed;
977}
978
Thomas Renninger20c457b2011-01-03 17:50:45 +0100979/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200980 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200981 */
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200982void print_events(const char *event_glob)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200983{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200984 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200985 struct event_symbol *syms = event_symbols;
986 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200987
Marti Raudsepp689d3012009-10-27 00:33:05 +0000988 printf("\n");
989 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200990
Thomas Gleixner86847b62009-06-06 12:24:17 +0200991 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +0800992 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200993
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200994 if (type != prev_type && printed) {
Marti Raudsepp689d3012009-10-27 00:33:05 +0000995 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200996 printed = 0;
997 ntypes_printed++;
998 }
999
1000 if (event_glob != NULL &&
1001 !(strglobmatch(syms->symbol, event_glob) ||
1002 (syms->alias && strglobmatch(syms->alias, event_glob))))
1003 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001004
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301005 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001006 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301007 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001008 strncpy(name, syms->symbol, MAX_NAME_LEN);
1009 printf(" %-50s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +02001010 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001011
Thomas Gleixner86847b62009-06-06 12:24:17 +02001012 prev_type = type;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001013 ++printed;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001014 }
1015
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001016 if (ntypes_printed) {
1017 printed = 0;
1018 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301019 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001020 print_hwcache_events(event_glob);
1021
1022 if (event_glob != NULL)
1023 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301024
Marti Raudsepp689d3012009-10-27 00:33:05 +00001025 printf("\n");
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001026 printf(" %-50s [%s]\n",
Jiri Olsa5f537a22012-03-15 20:09:18 +01001027 "rNNN",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -03001028 event_type_descriptors[PERF_TYPE_RAW]);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001029 printf(" %-50s [%s]\n",
1030 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1031 event_type_descriptors[PERF_TYPE_RAW]);
1032 printf(" (see 'perf list --help' on how to encode it)\n");
Marti Raudsepp689d3012009-10-27 00:33:05 +00001033 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001034
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001035 printf(" %-50s [%s]\n",
Liming Wang41bdcb22009-12-29 16:37:07 +08001036 "mem:<addr>[:access]",
1037 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001038 printf("\n");
1039
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001040 print_tracepoint_events(NULL, NULL);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001041}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001042
1043int parse_events__is_hardcoded_term(struct parse_events__term *term)
1044{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001045 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001046}
1047
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001048static int new_term(struct parse_events__term **_term, int type_val,
1049 int type_term, char *config,
1050 char *str, long num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001051{
1052 struct parse_events__term *term;
1053
1054 term = zalloc(sizeof(*term));
1055 if (!term)
1056 return -ENOMEM;
1057
1058 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001059 term->type_val = type_val;
1060 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001061 term->config = config;
1062
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001063 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001064 case PARSE_EVENTS__TERM_TYPE_NUM:
1065 term->val.num = num;
1066 break;
1067 case PARSE_EVENTS__TERM_TYPE_STR:
1068 term->val.str = str;
1069 break;
1070 default:
1071 return -EINVAL;
1072 }
1073
1074 *_term = term;
1075 return 0;
1076}
1077
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001078int parse_events__term_num(struct parse_events__term **term,
1079 int type_term, char *config, long num)
1080{
1081 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1082 config, NULL, num);
1083}
1084
1085int parse_events__term_str(struct parse_events__term **term,
1086 int type_term, char *config, char *str)
1087{
1088 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1089 config, str, 0);
1090}
1091
Jiri Olsa8f707d82012-03-15 20:09:16 +01001092void parse_events__free_terms(struct list_head *terms)
1093{
1094 struct parse_events__term *term, *h;
1095
1096 list_for_each_entry_safe(term, h, terms, list)
1097 free(term);
1098
1099 free(terms);
1100}