blob: be2e0c555e01065e8f839d147f80175bc9f8411a [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 Olsa5d7be902012-03-20 19:15:40 +010026int parse_events_parse(struct list_head *list, struct list_head *list_tmp,
27 int *idx);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +020028
Jaswinder Singh Rajput51e26842009-06-22 16:43:14 +053029#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
30#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
Ingo Molnar8ad8db32009-05-26 11:10:09 +020031
Ingo Molnara21ca2c2009-06-06 09:58:57 +020032static struct event_symbol event_symbols[] = {
Ingo Molnar129c04c2011-04-29 14:41:28 +020033 { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
34 { CHW(STALLED_CYCLES_FRONTEND), "stalled-cycles-frontend", "idle-cycles-frontend" },
35 { CHW(STALLED_CYCLES_BACKEND), "stalled-cycles-backend", "idle-cycles-backend" },
36 { CHW(INSTRUCTIONS), "instructions", "" },
37 { CHW(CACHE_REFERENCES), "cache-references", "" },
38 { CHW(CACHE_MISSES), "cache-misses", "" },
39 { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
40 { CHW(BRANCH_MISSES), "branch-misses", "" },
41 { CHW(BUS_CYCLES), "bus-cycles", "" },
Stephane Eranianf1ac18a2011-12-11 00:28:54 +010042 { CHW(REF_CPU_CYCLES), "ref-cycles", "" },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020043
Ingo Molnar129c04c2011-04-29 14:41:28 +020044 { CSW(CPU_CLOCK), "cpu-clock", "" },
45 { CSW(TASK_CLOCK), "task-clock", "" },
46 { CSW(PAGE_FAULTS), "page-faults", "faults" },
47 { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
48 { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
49 { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
50 { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
51 { CSW(ALIGNMENT_FAULTS), "alignment-faults", "" },
52 { CSW(EMULATION_FAULTS), "emulation-faults", "" },
Ingo Molnar8ad8db32009-05-26 11:10:09 +020053};
54
Ingo Molnarcdd6c482009-09-21 12:02:48 +020055#define __PERF_EVENT_FIELD(config, name) \
56 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
Ingo Molnar52425192009-05-26 09:17:18 +020057
Ingo Molnar1fc570a2011-04-27 05:20:22 +020058#define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020059#define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
Ingo Molnar1fc570a2011-04-27 05:20:22 +020060#define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020061#define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
Ingo Molnar52425192009-05-26 09:17:18 +020062
Ingo Molnard3d1e862011-04-29 13:49:08 +020063static const char *hw_event_names[PERF_COUNT_HW_MAX] = {
Ingo Molnar8faf3b52009-06-06 13:58:12 +020064 "cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020065 "instructions",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020066 "cache-references",
67 "cache-misses",
Ingo Molnar52425192009-05-26 09:17:18 +020068 "branches",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020069 "branch-misses",
70 "bus-cycles",
Ingo Molnard3d1e862011-04-29 13:49:08 +020071 "stalled-cycles-frontend",
72 "stalled-cycles-backend",
Stephane Eranianf1ac18a2011-12-11 00:28:54 +010073 "ref-cycles",
Ingo Molnar52425192009-05-26 09:17:18 +020074};
75
Ingo Molnard3d1e862011-04-29 13:49:08 +020076static const char *sw_event_names[PERF_COUNT_SW_MAX] = {
Ingo Molnar749141d2011-04-27 04:24:57 +020077 "cpu-clock",
78 "task-clock",
Ingo Molnar8faf3b52009-06-06 13:58:12 +020079 "page-faults",
80 "context-switches",
81 "CPU-migrations",
82 "minor-faults",
83 "major-faults",
Anton Blanchardf7d79862009-10-18 01:09:29 +000084 "alignment-faults",
85 "emulation-faults",
Ingo Molnar52425192009-05-26 09:17:18 +020086};
87
Ingo Molnar8326f442009-06-05 20:22:46 +020088#define MAX_ALIASES 8
89
Jiri Olsa01119192011-07-13 22:58:18 +020090static const char *hw_cache[PERF_COUNT_HW_CACHE_MAX][MAX_ALIASES] = {
Anton Blanchard9590b7b2009-07-06 22:01:31 +100091 { "L1-dcache", "l1-d", "l1d", "L1-data", },
92 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
Jiri Olsa01119192011-07-13 22:58:18 +020093 { "LLC", "L2", },
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +053094 { "dTLB", "d-tlb", "Data-TLB", },
95 { "iTLB", "i-tlb", "Instruction-TLB", },
96 { "branch", "branches", "bpu", "btb", "bpc", },
Jiri Olsa01119192011-07-13 22:58:18 +020097 { "node", },
Ingo Molnar8326f442009-06-05 20:22:46 +020098};
99
Jiri Olsa01119192011-07-13 22:58:18 +0200100static const char *hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530101 { "load", "loads", "read", },
102 { "store", "stores", "write", },
103 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200104};
105
Jiri Olsa01119192011-07-13 22:58:18 +0200106static const char *hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
107 [MAX_ALIASES] = {
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530108 { "refs", "Reference", "ops", "access", },
109 { "misses", "miss", },
Ingo Molnar8326f442009-06-05 20:22:46 +0200110};
111
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530112#define C(x) PERF_COUNT_HW_CACHE_##x
113#define CACHE_READ (1 << C(OP_READ))
114#define CACHE_WRITE (1 << C(OP_WRITE))
115#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
116#define COP(x) (1 << x)
117
118/*
119 * cache operartion stat
120 * L1I : Read and prefetch only
121 * ITLB and BPU : Read-only
122 */
123static unsigned long hw_cache_stat[C(MAX)] = {
124 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
125 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
126 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
127 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
128 [C(ITLB)] = (CACHE_READ),
129 [C(BPU)] = (CACHE_READ),
Jiri Olsa01119192011-07-13 22:58:18 +0200130 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530131};
132
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300133#define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400134 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300135 if (sys_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400136 (strcmp(sys_dirent.d_name, ".")) && \
137 (strcmp(sys_dirent.d_name, "..")))
138
Peter Zijlstraae07b632009-08-06 16:48:54 +0200139static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
140{
141 char evt_path[MAXPATHLEN];
142 int fd;
143
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200144 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Peter Zijlstraae07b632009-08-06 16:48:54 +0200145 sys_dir->d_name, evt_dir->d_name);
146 fd = open(evt_path, O_RDONLY);
147 if (fd < 0)
148 return -EINVAL;
149 close(fd);
150
151 return 0;
152}
153
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300154#define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400155 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300156 if (evt_dirent.d_type == DT_DIR && \
Jason Baronf6bdafe2009-07-21 12:20:22 -0400157 (strcmp(evt_dirent.d_name, ".")) && \
Peter Zijlstraae07b632009-08-06 16:48:54 +0200158 (strcmp(evt_dirent.d_name, "..")) && \
159 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400160
Li Zefan270bbbe2009-09-17 16:34:51 +0800161#define MAX_EVENT_LENGTH 512
Jason Baronf6bdafe2009-07-21 12:20:22 -0400162
Jason Baronf6bdafe2009-07-21 12:20:22 -0400163
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200164struct tracepoint_path *tracepoint_id_to_path(u64 config)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400165{
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200166 struct tracepoint_path *path = NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400167 DIR *sys_dir, *evt_dir;
168 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Stephane Eranian8aa8a7c2012-03-13 16:51:02 +0100169 char id_buf[24];
Eric Dumazet725b1362009-09-24 15:39:09 +0200170 int fd;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400171 u64 id;
172 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200173 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400174
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200175 if (debugfs_valid_mountpoint(tracing_events_path))
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200176 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400177
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200178 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400179 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200180 return NULL;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400181
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300182 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200183
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200184 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200185 sys_dirent.d_name);
186 evt_dir = opendir(dir_path);
187 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300188 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200189
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300190 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Eric Dumazet725b1362009-09-24 15:39:09 +0200191
192 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
Jason Baronf6bdafe2009-07-21 12:20:22 -0400193 evt_dirent.d_name);
Eric Dumazet725b1362009-09-24 15:39:09 +0200194 fd = open(evt_path, O_RDONLY);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400195 if (fd < 0)
196 continue;
197 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
198 close(fd);
199 continue;
200 }
201 close(fd);
202 id = atoll(id_buf);
203 if (id == config) {
204 closedir(evt_dir);
205 closedir(sys_dir);
Julia Lawall59b4cae2009-12-06 10:16:30 +0100206 path = zalloc(sizeof(*path));
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200207 path->system = malloc(MAX_EVENT_LENGTH);
208 if (!path->system) {
209 free(path);
210 return NULL;
211 }
212 path->name = malloc(MAX_EVENT_LENGTH);
213 if (!path->name) {
214 free(path->system);
215 free(path);
216 return NULL;
217 }
218 strncpy(path->system, sys_dirent.d_name,
219 MAX_EVENT_LENGTH);
220 strncpy(path->name, evt_dirent.d_name,
221 MAX_EVENT_LENGTH);
222 return path;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400223 }
224 }
225 closedir(evt_dir);
226 }
227
Jason Baronf6bdafe2009-07-21 12:20:22 -0400228 closedir(sys_dir);
Frederic Weisbecker1ef2ed12009-08-28 03:09:58 +0200229 return NULL;
230}
231
232#define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
233static const char *tracepoint_id_to_name(u64 config)
234{
235 static char buf[TP_PATH_LEN];
236 struct tracepoint_path *path;
237
238 path = tracepoint_id_to_path(config);
239 if (path) {
240 snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
241 free(path->name);
242 free(path->system);
243 free(path);
244 } else
245 snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
246
247 return buf;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400248}
249
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530250static int is_cache_op_valid(u8 cache_type, u8 cache_op)
251{
252 if (hw_cache_stat[cache_type] & COP(cache_op))
253 return 1; /* valid */
254 else
255 return 0; /* invalid */
256}
257
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530258static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
259{
260 static char name[50];
261
262 if (cache_result) {
263 sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
264 hw_cache_op[cache_op][0],
265 hw_cache_result[cache_result][0]);
266 } else {
267 sprintf(name, "%s-%s", hw_cache[cache_type][0],
268 hw_cache_op[cache_op][1]);
269 }
270
271 return name;
272}
273
David Ahern1424dc92011-03-09 22:23:28 -0700274const char *event_type(int type)
275{
276 switch (type) {
277 case PERF_TYPE_HARDWARE:
278 return "hardware";
279
280 case PERF_TYPE_SOFTWARE:
281 return "software";
282
283 case PERF_TYPE_TRACEPOINT:
284 return "tracepoint";
285
286 case PERF_TYPE_HW_CACHE:
287 return "hardware-cache";
288
289 default:
290 break;
291 }
292
293 return "unknown";
294}
295
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200296const char *event_name(struct perf_evsel *evsel)
Ingo Molnar52425192009-05-26 09:17:18 +0200297{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200298 u64 config = evsel->attr.config;
299 int type = evsel->attr.type;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200300
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200301 if (evsel->name)
302 return evsel->name;
303
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400304 return __event_name(type, config, NULL);
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200305}
306
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400307const char *__event_name(int type, u64 config, char *pmu_name)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200308{
Ingo Molnar52425192009-05-26 09:17:18 +0200309 static char buf[32];
310
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200311 if (type == PERF_TYPE_RAW) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200312 sprintf(buf, "raw 0x%" PRIx64, config);
Ingo Molnar52425192009-05-26 09:17:18 +0200313 return buf;
314 }
315
316 switch (type) {
317 case PERF_TYPE_HARDWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200318 if (config < PERF_COUNT_HW_MAX && hw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200319 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200320 return "unknown-hardware";
321
Ingo Molnar8326f442009-06-05 20:22:46 +0200322 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000323 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200324
325 cache_type = (config >> 0) & 0xff;
326 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
327 return "unknown-ext-hardware-cache-type";
328
329 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200330 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
331 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200332
333 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200334 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
335 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200336
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530337 if (!is_cache_op_valid(cache_type, cache_op))
338 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200339
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530340 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200341 }
342
Ingo Molnar52425192009-05-26 09:17:18 +0200343 case PERF_TYPE_SOFTWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200344 if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200345 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200346 return "unknown-software";
347
Jason Baronf6bdafe2009-07-21 12:20:22 -0400348 case PERF_TYPE_TRACEPOINT:
349 return tracepoint_id_to_name(config);
350
Ingo Molnar52425192009-05-26 09:17:18 +0200351 default:
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400352 if (pmu_name) {
353 snprintf(buf, sizeof(buf), "%s 0x%" PRIx64, pmu_name,
354 config);
355 return buf;
356 } else
357 break;
Ingo Molnar52425192009-05-26 09:17:18 +0200358 }
359
360 return "unknown";
361}
362
Jiri Olsa89812fc2012-03-15 20:09:15 +0100363static int add_event(struct list_head *list, int *idx,
364 struct perf_event_attr *attr, char *name)
365{
366 struct perf_evsel *evsel;
367
368 event_attr_init(attr);
369
370 evsel = perf_evsel__new(attr, (*idx)++);
371 if (!evsel)
372 return -ENOMEM;
373
374 list_add_tail(&evsel->node, list);
375
376 evsel->name = strdup(name);
377 return 0;
378}
379
380static int parse_aliases(char *str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200381{
382 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000383 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200384
385 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000386 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
387 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100388 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000389 longest = n;
390 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100391 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000392 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200393 }
394
Ingo Molnar89536452009-06-06 21:04:17 +0200395 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200396}
397
Jiri Olsa89812fc2012-03-15 20:09:15 +0100398int parse_events_add_cache(struct list_head *list, int *idx,
399 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200400{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100401 struct perf_event_attr attr;
402 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000403 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100404 char *op_result[2] = { op_result1, op_result2 };
405 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200406
Ingo Molnar8326f442009-06-05 20:22:46 +0200407 /*
408 * No fallback - if we cannot get a clear cache type
409 * then bail out:
410 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100411 cache_type = parse_aliases(type, hw_cache,
412 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200413 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100414 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200415
Jiri Olsa89812fc2012-03-15 20:09:15 +0100416 n = snprintf(name, MAX_NAME_LEN, "%s", type);
417
418 for (i = 0; (i < 2) && (op_result[i]); i++) {
419 char *str = op_result[i];
420
421 snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000422
423 if (cache_op == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100424 cache_op = parse_aliases(str, hw_cache_op,
425 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000426 if (cache_op >= 0) {
427 if (!is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100428 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000429 continue;
430 }
431 }
432
433 if (cache_result == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100434 cache_result = parse_aliases(str, hw_cache_result,
Paul Mackerras61c45982009-07-01 13:04:34 +1000435 PERF_COUNT_HW_CACHE_RESULT_MAX);
436 if (cache_result >= 0)
437 continue;
438 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000439 }
440
Ingo Molnar8326f442009-06-05 20:22:46 +0200441 /*
442 * Fall back to reads:
443 */
Ingo Molnar89536452009-06-06 21:04:17 +0200444 if (cache_op == -1)
445 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200446
Ingo Molnar8326f442009-06-05 20:22:46 +0200447 /*
448 * Fall back to accesses:
449 */
450 if (cache_result == -1)
451 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
452
Jiri Olsa89812fc2012-03-15 20:09:15 +0100453 memset(&attr, 0, sizeof(attr));
454 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
455 attr.type = PERF_TYPE_HW_CACHE;
456 return add_event(list, idx, &attr, name);
Ingo Molnar8326f442009-06-05 20:22:46 +0200457}
458
Jiri Olsa89812fc2012-03-15 20:09:15 +0100459static int add_tracepoint(struct list_head *list, int *idx,
460 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200461{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100462 struct perf_event_attr attr;
463 char name[MAX_NAME_LEN];
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200464 char evt_path[MAXPATHLEN];
465 char id_buf[4];
466 u64 id;
467 int fd;
468
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200469 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200470 sys_name, evt_name);
471
472 fd = open(evt_path, O_RDONLY);
473 if (fd < 0)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100474 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200475
476 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
477 close(fd);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100478 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200479 }
480
481 close(fd);
482 id = atoll(id_buf);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200483
Jiri Olsa89812fc2012-03-15 20:09:15 +0100484 memset(&attr, 0, sizeof(attr));
485 attr.config = id;
486 attr.type = PERF_TYPE_TRACEPOINT;
487 attr.sample_type |= PERF_SAMPLE_RAW;
488 attr.sample_type |= PERF_SAMPLE_TIME;
489 attr.sample_type |= PERF_SAMPLE_CPU;
490 attr.sample_period = 1;
Stephane Eranian5710fca2010-04-21 18:06:01 +0200491
Jiri Olsa89812fc2012-03-15 20:09:15 +0100492 snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
493 return add_event(list, idx, &attr, name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200494}
495
Jiri Olsa89812fc2012-03-15 20:09:15 +0100496static int add_tracepoint_multi(struct list_head *list, int *idx,
497 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200498{
499 char evt_path[MAXPATHLEN];
500 struct dirent *evt_ent;
501 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100502 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200503
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200504 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200505 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200506 if (!evt_dir) {
507 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100508 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200509 }
510
Jiri Olsa89812fc2012-03-15 20:09:15 +0100511 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200512 if (!strcmp(evt_ent->d_name, ".")
513 || !strcmp(evt_ent->d_name, "..")
514 || !strcmp(evt_ent->d_name, "enable")
515 || !strcmp(evt_ent->d_name, "filter"))
516 continue;
517
Jiri Olsa89812fc2012-03-15 20:09:15 +0100518 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500519 continue;
520
Jiri Olsa89812fc2012-03-15 20:09:15 +0100521 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200522 }
523
Jiri Olsa89812fc2012-03-15 20:09:15 +0100524 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200525}
526
Jiri Olsa89812fc2012-03-15 20:09:15 +0100527int parse_events_add_tracepoint(struct list_head *list, int *idx,
528 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400529{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100530 int ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400531
Jiri Olsa89812fc2012-03-15 20:09:15 +0100532 ret = debugfs_valid_mountpoint(tracing_events_path);
533 if (ret)
534 return ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400535
Jiri Olsa89812fc2012-03-15 20:09:15 +0100536 return strpbrk(event, "*?") ?
537 add_tracepoint_multi(list, idx, sys, event) :
538 add_tracepoint(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400539}
540
Jiri Olsa89812fc2012-03-15 20:09:15 +0100541static int
542parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100543{
544 int i;
545
546 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100547 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100548 break;
549
550 switch (type[i]) {
551 case 'r':
552 attr->bp_type |= HW_BREAKPOINT_R;
553 break;
554 case 'w':
555 attr->bp_type |= HW_BREAKPOINT_W;
556 break;
557 case 'x':
558 attr->bp_type |= HW_BREAKPOINT_X;
559 break;
560 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100561 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100562 }
563 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100564
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100565 if (!attr->bp_type) /* Default */
566 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
567
Jiri Olsa89812fc2012-03-15 20:09:15 +0100568 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100569}
570
Jiri Olsa89812fc2012-03-15 20:09:15 +0100571int parse_events_add_breakpoint(struct list_head *list, int *idx,
572 void *ptr, char *type)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100573{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100574 struct perf_event_attr attr;
575 char name[MAX_NAME_LEN];
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100576
Jiri Olsa89812fc2012-03-15 20:09:15 +0100577 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100578 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100579
Jiri Olsa89812fc2012-03-15 20:09:15 +0100580 if (parse_breakpoint_type(type, &attr))
581 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100582
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200583 /*
584 * We should find a nice way to override the access length
585 * Provide some defaults for now
586 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100587 if (attr.bp_type == HW_BREAKPOINT_X)
588 attr.bp_len = sizeof(long);
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200589 else
Jiri Olsa89812fc2012-03-15 20:09:15 +0100590 attr.bp_len = HW_BREAKPOINT_LEN_4;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200591
Jiri Olsa89812fc2012-03-15 20:09:15 +0100592 attr.type = PERF_TYPE_BREAKPOINT;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100593
Jiri Olsa89812fc2012-03-15 20:09:15 +0100594 snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
595 return add_event(list, idx, &attr, name);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100596}
597
Jiri Olsa8f707d82012-03-15 20:09:16 +0100598static int config_term(struct perf_event_attr *attr,
599 struct parse_events__term *term)
600{
601 switch (term->type) {
602 case PARSE_EVENTS__TERM_TYPE_CONFIG:
603 attr->config = term->val.num;
604 break;
605 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
606 attr->config1 = term->val.num;
607 break;
608 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
609 attr->config2 = term->val.num;
610 break;
611 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
612 attr->sample_period = term->val.num;
613 break;
614 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
615 /*
616 * TODO uncomment when the field is available
617 * attr->branch_sample_type = term->val.num;
618 */
619 break;
620 default:
621 return -EINVAL;
622 }
623 return 0;
624}
625
626static int config_attr(struct perf_event_attr *attr,
627 struct list_head *head, int fail)
628{
629 struct parse_events__term *term;
630
631 list_for_each_entry(term, head, list)
632 if (config_term(attr, term) && fail)
633 return -EINVAL;
634
635 return 0;
636}
637
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400638int parse_events_add_numeric_legacy(struct list_head *list, int *idx,
639 const char *name, unsigned long config,
640 struct list_head *head_config)
641{
642 struct perf_event_attr attr;
643 struct perf_pmu *pmu;
644 char *pmu_name = strdup(name);
645
646 memset(&attr, 0, sizeof(attr));
647
648 pmu = perf_pmu__find(pmu_name);
649
650 if (!pmu)
651 return -EINVAL;
652
653 attr.type = pmu->type;
654 attr.config = config;
655
656 if (head_config &&
657 config_attr(&attr, head_config, 1))
658 return -EINVAL;
659
660 return add_event(list, idx, &attr,
661 (char *) __event_name(pmu->type, config, pmu_name));
662}
663
Jiri Olsa8f707d82012-03-15 20:09:16 +0100664int parse_events_add_numeric(struct list_head *list, int *idx,
665 unsigned long type, unsigned long config,
666 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530667{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100668 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530669
Jiri Olsa89812fc2012-03-15 20:09:15 +0100670 memset(&attr, 0, sizeof(attr));
671 attr.type = type;
672 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100673
674 if (head_config &&
675 config_attr(&attr, head_config, 1))
676 return -EINVAL;
677
Jiri Olsa89812fc2012-03-15 20:09:15 +0100678 return add_event(list, idx, &attr,
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400679 (char *) __event_name(type, config, NULL));
Paul Mackerras61c45982009-07-01 13:04:34 +1000680}
681
Jiri Olsa5f537a22012-03-15 20:09:18 +0100682int parse_events_add_pmu(struct list_head *list, int *idx,
683 char *name, struct list_head *head_config)
684{
685 struct perf_event_attr attr;
686 struct perf_pmu *pmu;
687
688 pmu = perf_pmu__find(name);
689 if (!pmu)
690 return -EINVAL;
691
692 memset(&attr, 0, sizeof(attr));
693
694 /*
695 * Configure hardcoded terms first, no need to check
696 * return value when called with fail == 0 ;)
697 */
698 config_attr(&attr, head_config, 0);
699
700 if (perf_pmu__config(pmu, &attr, head_config))
701 return -EINVAL;
702
703 return add_event(list, idx, &attr, (char *) "pmu");
704}
705
Jiri Olsa5d7be902012-03-20 19:15:40 +0100706void parse_events_update_lists(struct list_head *list_event,
707 struct list_head *list_all)
708{
709 /*
710 * Called for single event definition. Update the
711 * 'all event' list, and reinit the 'signle event'
712 * list, for next event definition.
713 */
714 list_splice_tail(list_event, list_all);
715 INIT_LIST_HEAD(list_event);
716}
717
Jiri Olsa89812fc2012-03-15 20:09:15 +0100718int parse_events_modifier(struct list_head *list, char *str)
Paul Mackerras61c45982009-07-01 13:04:34 +1000719{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100720 struct perf_evsel *evsel;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100721 int exclude = 0, exclude_GH = 0;
722 int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000723
Jiri Olsa89812fc2012-03-15 20:09:15 +0100724 if (str == NULL)
Paul Mackerras61c45982009-07-01 13:04:34 +1000725 return 0;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200726
Paul Mackerras61c45982009-07-01 13:04:34 +1000727 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200728 if (*str == 'u') {
729 if (!exclude)
730 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000731 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200732 } else if (*str == 'k') {
733 if (!exclude)
734 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000735 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200736 } else if (*str == 'h') {
737 if (!exclude)
738 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000739 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100740 } else if (*str == 'G') {
741 if (!exclude_GH)
742 exclude_GH = eG = eH = 1;
743 eG = 0;
744 } else if (*str == 'H') {
745 if (!exclude_GH)
746 exclude_GH = eG = eH = 1;
747 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200748 } else if (*str == 'p') {
749 precise++;
750 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000751 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200752
Paul Mackerras61c45982009-07-01 13:04:34 +1000753 ++str;
754 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200755
Jiri Olsa89812fc2012-03-15 20:09:15 +0100756 /*
757 * precise ip:
758 *
759 * 0 - SAMPLE_IP can have arbitrary skid
760 * 1 - SAMPLE_IP must have constant skid
761 * 2 - SAMPLE_IP requested to have 0 skid
762 * 3 - SAMPLE_IP must have 0 skid
763 *
764 * See also PERF_RECORD_MISC_EXACT_IP
765 */
766 if (precise > 3)
767 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200768
Jiri Olsa89812fc2012-03-15 20:09:15 +0100769 list_for_each_entry(evsel, list, node) {
770 evsel->attr.exclude_user = eu;
771 evsel->attr.exclude_kernel = ek;
772 evsel->attr.exclude_hv = eh;
773 evsel->attr.precise_ip = precise;
774 evsel->attr.exclude_host = eH;
775 evsel->attr.exclude_guest = eG;
776 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200777
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530778 return 0;
779}
780
Jiri Olsa89812fc2012-03-15 20:09:15 +0100781int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200782{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100783 LIST_HEAD(list);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100784 LIST_HEAD(list_tmp);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100785 YY_BUFFER_STATE buffer;
786 int ret, idx = evlist->nr_entries;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200787
Jiri Olsa89812fc2012-03-15 20:09:15 +0100788 buffer = parse_events__scan_string(str);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200789
Jiri Olsa5d7be902012-03-20 19:15:40 +0100790 ret = parse_events_parse(&list, &list_tmp, &idx);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200791
Jiri Olsa89812fc2012-03-15 20:09:15 +0100792 parse_events__flush_buffer(buffer);
793 parse_events__delete_buffer(buffer);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200794
Jiri Olsa89812fc2012-03-15 20:09:15 +0100795 if (!ret) {
796 int entries = idx - evlist->nr_entries;
797 perf_evlist__splice_list_tail(evlist, &list, entries);
798 return 0;
799 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200800
Jiri Olsa5d7be902012-03-20 19:15:40 +0100801 /*
802 * There are 2 users - builtin-record and builtin-test objects.
803 * Both call perf_evlist__delete in case of error, so we dont
804 * need to bother.
805 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100806 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000807 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200808 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200809}
810
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200811int parse_events_option(const struct option *opt, const char *str,
812 int unset __used)
813{
814 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
815 return parse_events(evlist, str, unset);
816}
817
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200818int parse_filter(const struct option *opt, const char *str,
Li Zefanc171b552009-10-15 11:22:07 +0800819 int unset __used)
820{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200821 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200822 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800823
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200824 if (evlist->nr_entries > 0)
825 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200826
827 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800828 fprintf(stderr,
829 "-F option should follow a -e tracepoint option\n");
830 return -1;
831 }
832
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200833 last->filter = strdup(str);
834 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800835 fprintf(stderr, "not enough memory to hold filter string\n");
836 return -1;
837 }
Li Zefanc171b552009-10-15 11:22:07 +0800838
839 return 0;
840}
841
Thomas Gleixner86847b62009-06-06 12:24:17 +0200842static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200843 "Hardware event",
844 "Software event",
845 "Tracepoint event",
846 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800847 "Raw hardware event descriptor",
848 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200849};
850
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200851/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400852 * Print the events from <debugfs_mount_point>/tracing/events
853 */
854
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200855void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400856{
857 DIR *sys_dir, *evt_dir;
858 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400859 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200860 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400861
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200862 if (debugfs_valid_mountpoint(tracing_events_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400863 return;
864
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200865 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400866 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200867 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400868
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300869 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200870 if (subsys_glob != NULL &&
871 !strglobmatch(sys_dirent.d_name, subsys_glob))
872 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200873
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200874 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200875 sys_dirent.d_name);
876 evt_dir = opendir(dir_path);
877 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300878 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200879
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300880 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200881 if (event_glob != NULL &&
882 !strglobmatch(evt_dirent.d_name, event_glob))
883 continue;
884
Jason Baronf6bdafe2009-07-21 12:20:22 -0400885 snprintf(evt_path, MAXPATHLEN, "%s:%s",
886 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200887 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800888 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400889 }
890 closedir(evt_dir);
891 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400892 closedir(sys_dir);
893}
894
895/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100896 * Check whether event is in <debugfs_mount_point>/tracing/events
897 */
898
899int is_valid_tracepoint(const char *event_string)
900{
901 DIR *sys_dir, *evt_dir;
902 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
903 char evt_path[MAXPATHLEN];
904 char dir_path[MAXPATHLEN];
905
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200906 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +0100907 return 0;
908
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200909 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100910 if (!sys_dir)
911 return 0;
912
913 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
914
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200915 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +0100916 sys_dirent.d_name);
917 evt_dir = opendir(dir_path);
918 if (!evt_dir)
919 continue;
920
921 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
922 snprintf(evt_path, MAXPATHLEN, "%s:%s",
923 sys_dirent.d_name, evt_dirent.d_name);
924 if (!strcmp(evt_path, event_string)) {
925 closedir(evt_dir);
926 closedir(sys_dir);
927 return 1;
928 }
929 }
930 closedir(evt_dir);
931 }
932 closedir(sys_dir);
933 return 0;
934}
935
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200936void print_events_type(u8 type)
937{
938 struct event_symbol *syms = event_symbols;
939 unsigned int i;
940 char name[64];
941
942 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
943 if (type != syms->type)
944 continue;
945
946 if (strlen(syms->alias))
947 snprintf(name, sizeof(name), "%s OR %s",
948 syms->symbol, syms->alias);
949 else
950 snprintf(name, sizeof(name), "%s", syms->symbol);
951
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200952 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200953 event_type_descriptors[type]);
954 }
955}
956
957int print_hwcache_events(const char *event_glob)
958{
959 unsigned int type, op, i, printed = 0;
960
961 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
962 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
963 /* skip invalid cache type */
964 if (!is_cache_op_valid(type, op))
965 continue;
966
967 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
968 char *name = event_cache_name(type, op, i);
969
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200970 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200971 continue;
972
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200973 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200974 event_type_descriptors[PERF_TYPE_HW_CACHE]);
975 ++printed;
976 }
977 }
978 }
979
980 return printed;
981}
982
Thomas Renninger20c457b2011-01-03 17:50:45 +0100983/*
Thomas Gleixner86847b62009-06-06 12:24:17 +0200984 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200985 */
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200986void print_events(const char *event_glob)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200987{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200988 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200989 struct event_symbol *syms = event_symbols;
990 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200991
Marti Raudsepp689d3012009-10-27 00:33:05 +0000992 printf("\n");
993 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200994
Thomas Gleixner86847b62009-06-06 12:24:17 +0200995 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +0800996 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200997
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200998 if (type != prev_type && printed) {
Marti Raudsepp689d3012009-10-27 00:33:05 +0000999 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001000 printed = 0;
1001 ntypes_printed++;
1002 }
1003
1004 if (event_glob != NULL &&
1005 !(strglobmatch(syms->symbol, event_glob) ||
1006 (syms->alias && strglobmatch(syms->alias, event_glob))))
1007 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001008
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301009 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001010 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301011 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001012 strncpy(name, syms->symbol, MAX_NAME_LEN);
1013 printf(" %-50s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +02001014 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001015
Thomas Gleixner86847b62009-06-06 12:24:17 +02001016 prev_type = type;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001017 ++printed;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001018 }
1019
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001020 if (ntypes_printed) {
1021 printed = 0;
1022 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301023 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001024 print_hwcache_events(event_glob);
1025
1026 if (event_glob != NULL)
1027 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301028
Marti Raudsepp689d3012009-10-27 00:33:05 +00001029 printf("\n");
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001030 printf(" %-50s [%s]\n",
Jiri Olsa5f537a22012-03-15 20:09:18 +01001031 "rNNN",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -03001032 event_type_descriptors[PERF_TYPE_RAW]);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001033 printf(" %-50s [%s]\n",
1034 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1035 event_type_descriptors[PERF_TYPE_RAW]);
1036 printf(" (see 'perf list --help' on how to encode it)\n");
Marti Raudsepp689d3012009-10-27 00:33:05 +00001037 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001038
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001039 printf(" %-50s [%s]\n",
Liming Wang41bdcb22009-12-29 16:37:07 +08001040 "mem:<addr>[:access]",
1041 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001042 printf("\n");
1043
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001044 print_tracepoint_events(NULL, NULL);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001045}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001046
1047int parse_events__is_hardcoded_term(struct parse_events__term *term)
1048{
1049 return term->type <= PARSE_EVENTS__TERM_TYPE_HARDCODED_MAX;
1050}
1051
1052int parse_events__new_term(struct parse_events__term **_term, int type,
1053 char *config, char *str, long num)
1054{
1055 struct parse_events__term *term;
1056
1057 term = zalloc(sizeof(*term));
1058 if (!term)
1059 return -ENOMEM;
1060
1061 INIT_LIST_HEAD(&term->list);
1062 term->type = type;
1063 term->config = config;
1064
1065 switch (type) {
1066 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1067 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1068 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1069 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1070 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1071 case PARSE_EVENTS__TERM_TYPE_NUM:
1072 term->val.num = num;
1073 break;
1074 case PARSE_EVENTS__TERM_TYPE_STR:
1075 term->val.str = str;
1076 break;
1077 default:
1078 return -EINVAL;
1079 }
1080
1081 *_term = term;
1082 return 0;
1083}
1084
1085void parse_events__free_terms(struct list_head *terms)
1086{
1087 struct parse_events__term *term, *h;
1088
1089 list_for_each_entry_safe(term, h, terms, list)
1090 free(term);
1091
1092 free(terms);
1093}