blob: 6704978736cc1baf18bcbed7ec2192e652bf7b1a [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 Olsa5d7be902012-03-20 19:15:40 +010029int parse_events_parse(struct list_head *list, struct list_head *list_tmp,
30 int *idx);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020031
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053032#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
33#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020034
Ingo Molnara21ca2c2009-06-06 09:58:57 +020035static struct event_symbol event_symbols[] = {
Ingo Molnar129c04c2011-04-29 14:41:28 +020036 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
37 { CHW(STALLED_CYCLES_FRONTEND), "stalled-cycles-frontend", "idle-cycles-frontend" },
38 { CHW(STALLED_CYCLES_BACKEND), "stalled-cycles-backend", "idle-cycles-backend" },
39 { CHW(INSTRUCTIONS), "instructions", "" },
40 { CHW(CACHE_REFERENCES), "cache-references", "" },
41 { CHW(CACHE_MISSES), "cache-misses", "" },
42 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
43 { CHW(BRANCH_MISSES), "branch-misses", "" },
44 { CHW(BUS_CYCLES), "bus-cycles", "" },
Stephane Eranianf1ac18a2011-12-11 00:28:54 +010045 { CHW(REF_CPU_CYCLES), "ref-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020046
Ingo Molnar129c04c2011-04-29 14:41:28 +020047 { CSW(CPU_CLOCK), "cpu-clock", "" },
48 { CSW(TASK_CLOCK), "task-clock", "" },
49 { CSW(PAGE_FAULTS), "page-faults", "faults" },
50 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
51 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
52 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
53 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
54 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
55 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020056};
57
Ingo Molnarcdd6c482009-09-21 12:02:48 +020058#define __PERF_EVENT_FIELD(config, name) \
59 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020060
Ingo Molnar1fc570a2011-04-27 05:20:22 +020061#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020062#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +020063#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020064#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020065
Ingo Molnard3d1e862011-04-29 13:49:08 +020066static const char *hw_event_names[PERF_COUNT_HW_MAX] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020067 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020068 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020069 "cache-references",
70 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020071 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020072 "branch-misses",
73 "bus-cycles",
Ingo Molnard3d1e862011-04-29 13:49:08 +020074 "stalled-cycles-frontend",
75 "stalled-cycles-backend",
Stephane Eranianf1ac18a2011-12-11 00:28:54 +010076 "ref-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020077};
78
Ingo Molnard3d1e862011-04-29 13:49:08 +020079static const char *sw_event_names[PERF_COUNT_SW_MAX] = {
Ingo Molnar749141d2011-04-27 04:24:57 +020080 "cpu-clock",
81 "task-clock",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020082 "page-faults",
83 "context-switches",
84 "CPU-migrations",
85 "minor-faults",
86 "major-faults",
Anton Blanchardf7d79862009-10-18 01:09:29 +000087 "alignment-faults",
88 "emulation-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020089};
90
Ingo Molnar8326f442009-06-05 20:22:46 +020091#define MAX_ALIASES 8
92
Jiri Olsa01119192011-07-13 22:58:18 +020093static const char *hw_cache[PERF_COUNT_HW_CACHE_MAX][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100094 { "L1-dcache", "l1-d", "l1d", "L1-data", },
95 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jiri Olsa01119192011-07-13 22:58:18 +020096 { "LLC", "L2", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053097 { "dTLB", "d-tlb", "Data-TLB", },
98 { "iTLB", "i-tlb", "Instruction-TLB", },
99 { "branch", "branches", "bpu", "btb", "bpc", },
Jiri Olsa01119192011-07-13 22:58:18 +0200100 { "node", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200101};
102
Jiri Olsa01119192011-07-13 22:58:18 +0200103static const char *hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530104 { "load", "loads", "read", },
105 { "store", "stores", "write", },
106 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200107};
108
Jiri Olsa01119192011-07-13 22:58:18 +0200109static const char *hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
110 [MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530111 { "refs", "Reference", "ops", "access", },
112 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200113};
114
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530115#define C(x) PERF_COUNT_HW_CACHE_##x
116#define CACHE_READ (1 << C(OP_READ))
117#define CACHE_WRITE (1 << C(OP_WRITE))
118#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
119#define COP(x) (1 << x)
120
121/*
122 * cache operartion stat
123 * L1I : Read and prefetch only
124 * ITLB and BPU : Read-only
125 */
126static unsigned long hw_cache_stat[C(MAX)] = {
127 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
128 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
129 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
130 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
131 [C(ITLB)] = (CACHE_READ),
132 [C(BPU)] = (CACHE_READ),
Jiri Olsa01119192011-07-13 22:58:18 +0200133 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530134};
135
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300136#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400137 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300138 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400139 (strcmp(sys_dirent.d_name, ".")) && \
140 (strcmp(sys_dirent.d_name, "..")))
141
Peter Zijlstraae07b632009-08-06 16:48:54 +0200142static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
143{
144 char evt_path[MAXPATHLEN];
145 int fd;
146
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200147 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200148 sys_dir->d_name, evt_dir->d_name);
149 fd = open(evt_path, O_RDONLY);
150 if (fd < 0)
151 return -EINVAL;
152 close(fd);
153
154 return 0;
155}
156
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300157#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400158 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300159 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200161 (strcmp(evt_dirent.d_name, "..")) && \
162 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400163
Li Zefan270bbbe2009-09-17 16:34:51 +0800164#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400165
Jason Baronf6bdafe2009-07-21 12:20:22 -0400166
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200167struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400168{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200169 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400170 DIR *sys_dir, *evt_dir;
171 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100172 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200173 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400174 u64 id;
175 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200176 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400177
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200178 if (debugfs_valid_mountpoint(tracing_events_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200179 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400180
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200181 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400182 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200183 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400184
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300185 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200186
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200187 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200188 sys_dirent.d_name);
189 evt_dir = opendir(dir_path);
190 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300191 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200192
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300193 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200194
195 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400196 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200197 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400198 if (fd < 0)
199 continue;
200 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
201 close(fd);
202 continue;
203 }
204 close(fd);
205 id = atoll(id_buf);
206 if (id == config) {
207 closedir(evt_dir);
208 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100209 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200210 path->system = malloc(MAX_EVENT_LENGTH);
211 if (!path->system) {
212 free(path);
213 return NULL;
214 }
215 path->name = malloc(MAX_EVENT_LENGTH);
216 if (!path->name) {
217 free(path->system);
218 free(path);
219 return NULL;
220 }
221 strncpy(path->system, sys_dirent.d_name,
222 MAX_EVENT_LENGTH);
223 strncpy(path->name, evt_dirent.d_name,
224 MAX_EVENT_LENGTH);
225 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400226 }
227 }
228 closedir(evt_dir);
229 }
230
Jason Baronf6bdafe2009-07-21 12:20:22 -0400231 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200232 return NULL;
233}
234
235#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
236static const char *tracepoint_id_to_name(u64 config)
237{
238 static char buf[TP_PATH_LEN];
239 struct tracepoint_path *path;
240
241 path = tracepoint_id_to_path(config);
242 if (path) {
243 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
244 free(path->name);
245 free(path->system);
246 free(path);
247 } else
248 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
249
250 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400251}
252
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530253static int is_cache_op_valid(u8 cache_type, u8 cache_op)
254{
255 if (hw_cache_stat[cache_type] & COP(cache_op))
256 return 1; /* valid */
257 else
258 return 0; /* invalid */
259}
260
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530261static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
262{
263 static char name[50];
264
265 if (cache_result) {
266 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
267 hw_cache_op[cache_op][0],
268 hw_cache_result[cache_result][0]);
269 } else {
270 sprintf(name, "%s-%s", hw_cache[cache_type][0],
271 hw_cache_op[cache_op][1]);
272 }
273
274 return name;
275}
276
David Ahern1424dc92011-03-09 22:23:28 -0700277const char *event_type(int type)
278{
279 switch (type) {
280 case PERF_TYPE_HARDWARE:
281 return "hardware";
282
283 case PERF_TYPE_SOFTWARE:
284 return "software";
285
286 case PERF_TYPE_TRACEPOINT:
287 return "tracepoint";
288
289 case PERF_TYPE_HW_CACHE:
290 return "hardware-cache";
291
292 default:
293 break;
294 }
295
296 return "unknown";
297}
298
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200299const char *event_name(struct perf_evsel *evsel)
Ingo Molnar52425192009-05-26 09:17:18 +0200300{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200301 u64 config = evsel->attr.config;
302 int type = evsel->attr.type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200303
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200304 if (evsel->name)
305 return evsel->name;
306
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200307 return __event_name(type, config);
308}
309
Ingo Molnar83a09442009-08-15 12:26:57 +0200310const char *__event_name(int type, u64 config)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200311{
Ingo Molnar52425192009-05-26 09:17:18 +0200312 static char buf[32];
313
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200314 if (type == PERF_TYPE_RAW) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200315 sprintf(buf, "raw 0x%" PRIx64, config);
Ingo Molnar52425192009-05-26 09:17:18 +0200316 return buf;
317 }
318
319 switch (type) {
320 case PERF_TYPE_HARDWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200321 if (config < PERF_COUNT_HW_MAX && hw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200322 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200323 return "unknown-hardware";
324
Ingo Molnar8326f442009-06-05 20:22:46 +0200325 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000326 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200327
328 cache_type = (config >> 0) & 0xff;
329 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
330 return "unknown-ext-hardware-cache-type";
331
332 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200333 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
334 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200335
336 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200337 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
338 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200339
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530340 if (!is_cache_op_valid(cache_type, cache_op))
341 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200342
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530343 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200344 }
345
Ingo Molnar52425192009-05-26 09:17:18 +0200346 case PERF_TYPE_SOFTWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200347 if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200348 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200349 return "unknown-software";
350
Jason Baronf6bdafe2009-07-21 12:20:22 -0400351 case PERF_TYPE_TRACEPOINT:
352 return tracepoint_id_to_name(config);
353
Ingo Molnar52425192009-05-26 09:17:18 +0200354 default:
355 break;
356 }
357
358 return "unknown";
359}
360
Jiri Olsa89812fc2012-03-15 20:09:15 +0100361static int add_event(struct list_head *list, int *idx,
362 struct perf_event_attr *attr, char *name)
363{
364 struct perf_evsel *evsel;
365
366 event_attr_init(attr);
367
368 evsel = perf_evsel__new(attr, (*idx)++);
369 if (!evsel)
370 return -ENOMEM;
371
372 list_add_tail(&evsel->node, list);
373
374 evsel->name = strdup(name);
375 return 0;
376}
377
378static int parse_aliases(char *str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200379{
380 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000381 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200382
383 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000384 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
385 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100386 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000387 longest = n;
388 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100389 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000390 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200391 }
392
Ingo Molnar89536452009-06-06 21:04:17 +0200393 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200394}
395
Jiri Olsa89812fc2012-03-15 20:09:15 +0100396int parse_events_add_cache(struct list_head *list, int *idx,
397 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200398{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100399 struct perf_event_attr attr;
400 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000401 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100402 char *op_result[2] = { op_result1, op_result2 };
403 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200404
Ingo Molnar8326f442009-06-05 20:22:46 +0200405 /*
406 * No fallback - if we cannot get a clear cache type
407 * then bail out:
408 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100409 cache_type = parse_aliases(type, hw_cache,
410 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200411 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100412 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200413
Jiri Olsa89812fc2012-03-15 20:09:15 +0100414 n = snprintf(name, MAX_NAME_LEN, "%s", type);
415
416 for (i = 0; (i < 2) && (op_result[i]); i++) {
417 char *str = op_result[i];
418
419 snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000420
421 if (cache_op == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100422 cache_op = parse_aliases(str, hw_cache_op,
423 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000424 if (cache_op >= 0) {
425 if (!is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100426 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000427 continue;
428 }
429 }
430
431 if (cache_result == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100432 cache_result = parse_aliases(str, hw_cache_result,
Paul Mackerras61c45982009-07-01 13:04:34 +1000433 PERF_COUNT_HW_CACHE_RESULT_MAX);
434 if (cache_result >= 0)
435 continue;
436 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000437 }
438
Ingo Molnar8326f442009-06-05 20:22:46 +0200439 /*
440 * Fall back to reads:
441 */
Ingo Molnar89536452009-06-06 21:04:17 +0200442 if (cache_op == -1)
443 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200444
Ingo Molnar8326f442009-06-05 20:22:46 +0200445 /*
446 * Fall back to accesses:
447 */
448 if (cache_result == -1)
449 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
450
Jiri Olsa89812fc2012-03-15 20:09:15 +0100451 memset(&attr, 0, sizeof(attr));
452 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
453 attr.type = PERF_TYPE_HW_CACHE;
454 return add_event(list, idx, &attr, name);
Ingo Molnar8326f442009-06-05 20:22:46 +0200455}
456
Jiri Olsa89812fc2012-03-15 20:09:15 +0100457static int add_tracepoint(struct list_head *list, int *idx,
458 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200459{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100460 struct perf_event_attr attr;
461 char name[MAX_NAME_LEN];
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200462 char evt_path[MAXPATHLEN];
463 char id_buf[4];
464 u64 id;
465 int fd;
466
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200467 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200468 sys_name, evt_name);
469
470 fd = open(evt_path, O_RDONLY);
471 if (fd < 0)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100472 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200473
474 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
475 close(fd);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100476 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200477 }
478
479 close(fd);
480 id = atoll(id_buf);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200481
Jiri Olsa89812fc2012-03-15 20:09:15 +0100482 memset(&attr, 0, sizeof(attr));
483 attr.config = id;
484 attr.type = PERF_TYPE_TRACEPOINT;
485 attr.sample_type |= PERF_SAMPLE_RAW;
486 attr.sample_type |= PERF_SAMPLE_TIME;
487 attr.sample_type |= PERF_SAMPLE_CPU;
488 attr.sample_period = 1;
Stephane Eranian5710fca2010-04-21 18:06:01 +0200489
Jiri Olsa89812fc2012-03-15 20:09:15 +0100490 snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
491 return add_event(list, idx, &attr, name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200492}
493
Jiri Olsa89812fc2012-03-15 20:09:15 +0100494static int add_tracepoint_multi(struct list_head *list, int *idx,
495 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200496{
497 char evt_path[MAXPATHLEN];
498 struct dirent *evt_ent;
499 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100500 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200501
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200502 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200503 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200504 if (!evt_dir) {
505 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100506 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200507 }
508
Jiri Olsa89812fc2012-03-15 20:09:15 +0100509 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200510 if (!strcmp(evt_ent->d_name, ".")
511 || !strcmp(evt_ent->d_name, "..")
512 || !strcmp(evt_ent->d_name, "enable")
513 || !strcmp(evt_ent->d_name, "filter"))
514 continue;
515
Jiri Olsa89812fc2012-03-15 20:09:15 +0100516 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500517 continue;
518
Jiri Olsa89812fc2012-03-15 20:09:15 +0100519 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200520 }
521
Jiri Olsa89812fc2012-03-15 20:09:15 +0100522 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200523}
524
Jiri Olsa89812fc2012-03-15 20:09:15 +0100525int parse_events_add_tracepoint(struct list_head *list, int *idx,
526 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400527{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100528 int ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400529
Jiri Olsa89812fc2012-03-15 20:09:15 +0100530 ret = debugfs_valid_mountpoint(tracing_events_path);
531 if (ret)
532 return ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400533
Jiri Olsa89812fc2012-03-15 20:09:15 +0100534 return strpbrk(event, "*?") ?
535 add_tracepoint_multi(list, idx, sys, event) :
536 add_tracepoint(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400537}
538
Jiri Olsa89812fc2012-03-15 20:09:15 +0100539static int
540parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100541{
542 int i;
543
544 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100545 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100546 break;
547
548 switch (type[i]) {
549 case 'r':
550 attr->bp_type |= HW_BREAKPOINT_R;
551 break;
552 case 'w':
553 attr->bp_type |= HW_BREAKPOINT_W;
554 break;
555 case 'x':
556 attr->bp_type |= HW_BREAKPOINT_X;
557 break;
558 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100559 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100560 }
561 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100562
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100563 if (!attr->bp_type) /* Default */
564 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
565
Jiri Olsa89812fc2012-03-15 20:09:15 +0100566 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100567}
568
Jiri Olsa89812fc2012-03-15 20:09:15 +0100569int parse_events_add_breakpoint(struct list_head *list, int *idx,
570 void *ptr, char *type)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100571{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100572 struct perf_event_attr attr;
573 char name[MAX_NAME_LEN];
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100574
Jiri Olsa89812fc2012-03-15 20:09:15 +0100575 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100576 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100577
Jiri Olsa89812fc2012-03-15 20:09:15 +0100578 if (parse_breakpoint_type(type, &attr))
579 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100580
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200581 /*
582 * We should find a nice way to override the access length
583 * Provide some defaults for now
584 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100585 if (attr.bp_type == HW_BREAKPOINT_X)
586 attr.bp_len = sizeof(long);
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200587 else
Jiri Olsa89812fc2012-03-15 20:09:15 +0100588 attr.bp_len = HW_BREAKPOINT_LEN_4;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200589
Jiri Olsa89812fc2012-03-15 20:09:15 +0100590 attr.type = PERF_TYPE_BREAKPOINT;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100591
Jiri Olsa89812fc2012-03-15 20:09:15 +0100592 snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
593 return add_event(list, idx, &attr, name);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100594}
595
Jiri Olsa8f707d82012-03-15 20:09:16 +0100596static int config_term(struct perf_event_attr *attr,
597 struct parse_events__term *term)
598{
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200599#define CHECK_TYPE_VAL(type) \
600do { \
601 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
602 return -EINVAL; \
603} while (0)
604
605 switch (term->type_term) {
Jiri Olsa8f707d82012-03-15 20:09:16 +0100606 case PARSE_EVENTS__TERM_TYPE_CONFIG:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200607 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100608 attr->config = term->val.num;
609 break;
610 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200611 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100612 attr->config1 = term->val.num;
613 break;
614 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200615 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100616 attr->config2 = term->val.num;
617 break;
618 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200619 CHECK_TYPE_VAL(NUM);
Jiri Olsa8f707d82012-03-15 20:09:16 +0100620 attr->sample_period = term->val.num;
621 break;
622 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
623 /*
624 * TODO uncomment when the field is available
625 * attr->branch_sample_type = term->val.num;
626 */
627 break;
628 default:
629 return -EINVAL;
630 }
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200631
Jiri Olsa8f707d82012-03-15 20:09:16 +0100632 return 0;
Jiri Olsa16fa7e82012-04-25 18:24:57 +0200633#undef CHECK_TYPE_VAL
Jiri Olsa8f707d82012-03-15 20:09:16 +0100634}
635
636static int config_attr(struct perf_event_attr *attr,
637 struct list_head *head, int fail)
638{
639 struct parse_events__term *term;
640
641 list_for_each_entry(term, head, list)
642 if (config_term(attr, term) && fail)
643 return -EINVAL;
644
645 return 0;
646}
647
648int parse_events_add_numeric(struct list_head *list, int *idx,
649 unsigned long type, unsigned long config,
650 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530651{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100652 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530653
Jiri Olsa89812fc2012-03-15 20:09:15 +0100654 memset(&attr, 0, sizeof(attr));
655 attr.type = type;
656 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100657
658 if (head_config &&
659 config_attr(&attr, head_config, 1))
660 return -EINVAL;
661
Jiri Olsa89812fc2012-03-15 20:09:15 +0100662 return add_event(list, idx, &attr,
663 (char *) __event_name(type, config));
Paul Mackerras61c45982009-07-01 13:04:34 +1000664}
665
Jiri Olsa5f537a22012-03-15 20:09:18 +0100666int parse_events_add_pmu(struct list_head *list, int *idx,
667 char *name, struct list_head *head_config)
668{
669 struct perf_event_attr attr;
670 struct perf_pmu *pmu;
671
672 pmu = perf_pmu__find(name);
673 if (!pmu)
674 return -EINVAL;
675
676 memset(&attr, 0, sizeof(attr));
677
678 /*
679 * Configure hardcoded terms first, no need to check
680 * return value when called with fail == 0 ;)
681 */
682 config_attr(&attr, head_config, 0);
683
684 if (perf_pmu__config(pmu, &attr, head_config))
685 return -EINVAL;
686
687 return add_event(list, idx, &attr, (char *) "pmu");
688}
689
Jiri Olsa5d7be902012-03-20 19:15:40 +0100690void parse_events_update_lists(struct list_head *list_event,
691 struct list_head *list_all)
692{
693 /*
694 * Called for single event definition. Update the
695 * 'all event' list, and reinit the 'signle event'
696 * list, for next event definition.
697 */
698 list_splice_tail(list_event, list_all);
699 INIT_LIST_HEAD(list_event);
700}
701
Jiri Olsa89812fc2012-03-15 20:09:15 +0100702int parse_events_modifier(struct list_head *list, char *str)
Paul Mackerras61c45982009-07-01 13:04:34 +1000703{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100704 struct perf_evsel *evsel;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100705 int exclude = 0, exclude_GH = 0;
706 int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000707
Jiri Olsa89812fc2012-03-15 20:09:15 +0100708 if (str == NULL)
Paul Mackerras61c45982009-07-01 13:04:34 +1000709 return 0;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200710
Paul Mackerras61c45982009-07-01 13:04:34 +1000711 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200712 if (*str == 'u') {
713 if (!exclude)
714 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000715 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200716 } else if (*str == 'k') {
717 if (!exclude)
718 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000719 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200720 } else if (*str == 'h') {
721 if (!exclude)
722 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000723 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100724 } else if (*str == 'G') {
725 if (!exclude_GH)
726 exclude_GH = eG = eH = 1;
727 eG = 0;
728 } else if (*str == 'H') {
729 if (!exclude_GH)
730 exclude_GH = eG = eH = 1;
731 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200732 } else if (*str == 'p') {
733 precise++;
734 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000735 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200736
Paul Mackerras61c45982009-07-01 13:04:34 +1000737 ++str;
738 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200739
Jiri Olsa89812fc2012-03-15 20:09:15 +0100740 /*
741 * precise ip:
742 *
743 * 0 - SAMPLE_IP can have arbitrary skid
744 * 1 - SAMPLE_IP must have constant skid
745 * 2 - SAMPLE_IP requested to have 0 skid
746 * 3 - SAMPLE_IP must have 0 skid
747 *
748 * See also PERF_RECORD_MISC_EXACT_IP
749 */
750 if (precise > 3)
751 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200752
Jiri Olsa89812fc2012-03-15 20:09:15 +0100753 list_for_each_entry(evsel, list, node) {
754 evsel->attr.exclude_user = eu;
755 evsel->attr.exclude_kernel = ek;
756 evsel->attr.exclude_hv = eh;
757 evsel->attr.precise_ip = precise;
758 evsel->attr.exclude_host = eH;
759 evsel->attr.exclude_guest = eG;
760 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200761
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530762 return 0;
763}
764
Jiri Olsa89812fc2012-03-15 20:09:15 +0100765int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200766{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100767 LIST_HEAD(list);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100768 LIST_HEAD(list_tmp);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100769 YY_BUFFER_STATE buffer;
770 int ret, idx = evlist->nr_entries;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200771
Jiri Olsa89812fc2012-03-15 20:09:15 +0100772 buffer = parse_events__scan_string(str);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200773
Jiri Olsa82ba1f22012-05-21 09:12:50 +0200774#ifdef PARSER_DEBUG
775 parse_events_debug = 1;
776#endif
Jiri Olsa5d7be902012-03-20 19:15:40 +0100777 ret = parse_events_parse(&list, &list_tmp, &idx);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200778
Jiri Olsa89812fc2012-03-15 20:09:15 +0100779 parse_events__flush_buffer(buffer);
780 parse_events__delete_buffer(buffer);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200781
Jiri Olsa89812fc2012-03-15 20:09:15 +0100782 if (!ret) {
783 int entries = idx - evlist->nr_entries;
784 perf_evlist__splice_list_tail(evlist, &list, entries);
785 return 0;
786 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200787
Jiri Olsa5d7be902012-03-20 19:15:40 +0100788 /*
789 * There are 2 users - builtin-record and builtin-test objects.
790 * Both call perf_evlist__delete in case of error, so we dont
791 * need to bother.
792 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100793 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000794 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200795 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200796}
797
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200798int parse_events_option(const struct option *opt, const char *str,
799 int unset __used)
800{
801 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
802 return parse_events(evlist, str, unset);
803}
804
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200805int parse_filter(const struct option *opt, const char *str,
Li Zefanc171b552009-10-15 11:22:07 +0800806 int unset __used)
807{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200808 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200809 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800810
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200811 if (evlist->nr_entries > 0)
812 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200813
814 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800815 fprintf(stderr,
816 "-F option should follow a -e tracepoint option\n");
817 return -1;
818 }
819
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200820 last->filter = strdup(str);
821 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800822 fprintf(stderr, "not enough memory to hold filter string\n");
823 return -1;
824 }
Li Zefanc171b552009-10-15 11:22:07 +0800825
826 return 0;
827}
828
Thomas Gleixner86847b62009-06-06 12:24:17 +0200829static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200830 "Hardware event",
831 "Software event",
832 "Tracepoint event",
833 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800834 "Raw hardware event descriptor",
835 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200836};
837
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200838/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400839 * Print the events from <debugfs_mount_point>/tracing/events
840 */
841
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200842void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400843{
844 DIR *sys_dir, *evt_dir;
845 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400846 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200847 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400848
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200849 if (debugfs_valid_mountpoint(tracing_events_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400850 return;
851
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200852 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400853 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200854 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400855
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300856 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200857 if (subsys_glob != NULL &&
858 !strglobmatch(sys_dirent.d_name, subsys_glob))
859 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200860
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200861 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200862 sys_dirent.d_name);
863 evt_dir = opendir(dir_path);
864 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300865 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200866
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300867 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200868 if (event_glob != NULL &&
869 !strglobmatch(evt_dirent.d_name, event_glob))
870 continue;
871
Jason Baronf6bdafe2009-07-21 12:20:22 -0400872 snprintf(evt_path, MAXPATHLEN, "%s:%s",
873 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200874 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800875 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400876 }
877 closedir(evt_dir);
878 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400879 closedir(sys_dir);
880}
881
882/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100883 * Check whether event is in <debugfs_mount_point>/tracing/events
884 */
885
886int is_valid_tracepoint(const char *event_string)
887{
888 DIR *sys_dir, *evt_dir;
889 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
890 char evt_path[MAXPATHLEN];
891 char dir_path[MAXPATHLEN];
892
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200893 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +0100894 return 0;
895
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200896 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100897 if (!sys_dir)
898 return 0;
899
900 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
901
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200902 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +0100903 sys_dirent.d_name);
904 evt_dir = opendir(dir_path);
905 if (!evt_dir)
906 continue;
907
908 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
909 snprintf(evt_path, MAXPATHLEN, "%s:%s",
910 sys_dirent.d_name, evt_dirent.d_name);
911 if (!strcmp(evt_path, event_string)) {
912 closedir(evt_dir);
913 closedir(sys_dir);
914 return 1;
915 }
916 }
917 closedir(evt_dir);
918 }
919 closedir(sys_dir);
920 return 0;
921}
922
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200923void print_events_type(u8 type)
924{
925 struct event_symbol *syms = event_symbols;
926 unsigned int i;
927 char name[64];
928
929 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
930 if (type != syms->type)
931 continue;
932
933 if (strlen(syms->alias))
934 snprintf(name, sizeof(name), "%s OR %s",
935 syms->symbol, syms->alias);
936 else
937 snprintf(name, sizeof(name), "%s", syms->symbol);
938
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200939 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200940 event_type_descriptors[type]);
941 }
942}
943
944int print_hwcache_events(const char *event_glob)
945{
946 unsigned int type, op, i, printed = 0;
947
948 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
949 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
950 /* skip invalid cache type */
951 if (!is_cache_op_valid(type, op))
952 continue;
953
954 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
955 char *name = event_cache_name(type, op, i);
956
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200957 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200958 continue;
959
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200960 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200961 event_type_descriptors[PERF_TYPE_HW_CACHE]);
962 ++printed;
963 }
964 }
965 }
966
967 return printed;
968}
969
Thomas Renninger20c457b2011-01-03 17:50:45 +0100970/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200971 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200972 */
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200973void print_events(const char *event_glob)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200974{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200975 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200976 struct event_symbol *syms = event_symbols;
977 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200978
Marti Raudsepp689d3012009-10-27 00:33:05 +0000979 printf("\n");
980 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200981
Thomas Gleixner86847b62009-06-06 12:24:17 +0200982 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +0800983 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200984
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200985 if (type != prev_type && printed) {
Marti Raudsepp689d3012009-10-27 00:33:05 +0000986 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200987 printed = 0;
988 ntypes_printed++;
989 }
990
991 if (event_glob != NULL &&
992 !(strglobmatch(syms->symbol, event_glob) ||
993 (syms->alias && strglobmatch(syms->alias, event_glob))))
994 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200995
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530996 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200997 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530998 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200999 strncpy(name, syms->symbol, MAX_NAME_LEN);
1000 printf(" %-50s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +02001001 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001002
Thomas Gleixner86847b62009-06-06 12:24:17 +02001003 prev_type = type;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001004 ++printed;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001005 }
1006
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001007 if (ntypes_printed) {
1008 printed = 0;
1009 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301010 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001011 print_hwcache_events(event_glob);
1012
1013 if (event_glob != NULL)
1014 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301015
Marti Raudsepp689d3012009-10-27 00:33:05 +00001016 printf("\n");
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001017 printf(" %-50s [%s]\n",
Jiri Olsa5f537a22012-03-15 20:09:18 +01001018 "rNNN",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -03001019 event_type_descriptors[PERF_TYPE_RAW]);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001020 printf(" %-50s [%s]\n",
1021 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1022 event_type_descriptors[PERF_TYPE_RAW]);
1023 printf(" (see 'perf list --help' on how to encode it)\n");
Marti Raudsepp689d3012009-10-27 00:33:05 +00001024 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001025
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001026 printf(" %-50s [%s]\n",
Liming Wang41bdcb22009-12-29 16:37:07 +08001027 "mem:<addr>[:access]",
1028 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001029 printf("\n");
1030
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001031 print_tracepoint_events(NULL, NULL);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001032}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001033
1034int parse_events__is_hardcoded_term(struct parse_events__term *term)
1035{
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001036 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001037}
1038
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001039static int new_term(struct parse_events__term **_term, int type_val,
1040 int type_term, char *config,
1041 char *str, long num)
Jiri Olsa8f707d82012-03-15 20:09:16 +01001042{
1043 struct parse_events__term *term;
1044
1045 term = zalloc(sizeof(*term));
1046 if (!term)
1047 return -ENOMEM;
1048
1049 INIT_LIST_HEAD(&term->list);
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001050 term->type_val = type_val;
1051 term->type_term = type_term;
Jiri Olsa8f707d82012-03-15 20:09:16 +01001052 term->config = config;
1053
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001054 switch (type_val) {
Jiri Olsa8f707d82012-03-15 20:09:16 +01001055 case PARSE_EVENTS__TERM_TYPE_NUM:
1056 term->val.num = num;
1057 break;
1058 case PARSE_EVENTS__TERM_TYPE_STR:
1059 term->val.str = str;
1060 break;
1061 default:
1062 return -EINVAL;
1063 }
1064
1065 *_term = term;
1066 return 0;
1067}
1068
Jiri Olsa16fa7e82012-04-25 18:24:57 +02001069int parse_events__term_num(struct parse_events__term **term,
1070 int type_term, char *config, long num)
1071{
1072 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1073 config, NULL, num);
1074}
1075
1076int parse_events__term_str(struct parse_events__term **term,
1077 int type_term, char *config, char *str)
1078{
1079 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1080 config, str, 0);
1081}
1082
Jiri Olsa8f707d82012-03-15 20:09:16 +01001083void parse_events__free_terms(struct list_head *terms)
1084{
1085 struct parse_events__term *term, *h;
1086
1087 list_for_each_entry_safe(term, h, terms, list)
1088 free(term);
1089
1090 free(terms);
1091}