blob: f02e5c5770c9d31cfd35f46196123d4ecbb89160 [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;
Ashwin Chaugule9299aad2012-11-17 12:00:58 -0500300 char *buf;
301 size_t buf_sz;
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200302
Ashwin Chaugule9299aad2012-11-17 12:00:58 -0500303 if (evsel->name) {
304 /* Make new space for the modifier bits. */
305 buf_sz = strlen(evsel->name) + 3;
306 buf = malloc(buf_sz);
307 if (!buf)
308 /*
309 * Always return what was already in 'name'.
310 */
311 return evsel->name;
312
313 strlcpy(buf, evsel->name, buf_sz);
314
315 free(evsel->name);
316
317 evsel->name = buf;
318
319 /* User mode profiling. */
320 if (!evsel->attr.exclude_user && evsel->attr.exclude_kernel)
321 strlcpy(&evsel->name[strlen(evsel->name)], ":u",
322 buf_sz);
323 /* Kernel mode profiling. */
324 else if (!evsel->attr.exclude_kernel &&
325 evsel->attr.exclude_user)
326 strlcpy(&evsel->name[strlen(evsel->name)], ":k",
327 buf_sz);
328
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200329 return evsel->name;
Ashwin Chaugule9299aad2012-11-17 12:00:58 -0500330 }
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200331
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400332 return __event_name(type, config, NULL);
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200333}
334
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400335const char *__event_name(int type, u64 config, char *pmu_name)
Peter Zijlstra8f18aec2009-08-06 19:40:28 +0200336{
Ingo Molnar52425192009-05-26 09:17:18 +0200337 static char buf[32];
338
Ashwin Chaugule3d0945d2012-09-20 10:53:42 -0400339 if (!pmu_name && type == PERF_TYPE_RAW) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200340 sprintf(buf, "raw 0x%" PRIx64, config);
Ingo Molnar52425192009-05-26 09:17:18 +0200341 return buf;
342 }
343
344 switch (type) {
345 case PERF_TYPE_HARDWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200346 if (config < PERF_COUNT_HW_MAX && hw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200347 return hw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200348 return "unknown-hardware";
349
Ingo Molnar8326f442009-06-05 20:22:46 +0200350 case PERF_TYPE_HW_CACHE: {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000351 u8 cache_type, cache_op, cache_result;
Ingo Molnar8326f442009-06-05 20:22:46 +0200352
353 cache_type = (config >> 0) & 0xff;
354 if (cache_type > PERF_COUNT_HW_CACHE_MAX)
355 return "unknown-ext-hardware-cache-type";
356
357 cache_op = (config >> 8) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200358 if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
359 return "unknown-ext-hardware-cache-op";
Ingo Molnar8326f442009-06-05 20:22:46 +0200360
361 cache_result = (config >> 16) & 0xff;
Ingo Molnar8faf3b52009-06-06 13:58:12 +0200362 if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
363 return "unknown-ext-hardware-cache-result";
Ingo Molnar8326f442009-06-05 20:22:46 +0200364
Jaswinder Singh Rajput06813f62009-06-25 17:16:07 +0530365 if (!is_cache_op_valid(cache_type, cache_op))
366 return "invalid-cache";
Ingo Molnar8326f442009-06-05 20:22:46 +0200367
Jaswinder Singh Rajpute5c59542009-06-25 18:25:22 +0530368 return event_cache_name(cache_type, cache_op, cache_result);
Ingo Molnar8326f442009-06-05 20:22:46 +0200369 }
370
Ingo Molnar52425192009-05-26 09:17:18 +0200371 case PERF_TYPE_SOFTWARE:
Ingo Molnar1fc570a2011-04-27 05:20:22 +0200372 if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200373 return sw_event_names[config];
Ingo Molnar52425192009-05-26 09:17:18 +0200374 return "unknown-software";
375
Jason Baronf6bdafe2009-07-21 12:20:22 -0400376 case PERF_TYPE_TRACEPOINT:
377 return tracepoint_id_to_name(config);
378
Ingo Molnar52425192009-05-26 09:17:18 +0200379 default:
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400380 if (pmu_name) {
381 snprintf(buf, sizeof(buf), "%s 0x%" PRIx64, pmu_name,
382 config);
383 return buf;
384 } else
385 break;
Ingo Molnar52425192009-05-26 09:17:18 +0200386 }
387
388 return "unknown";
389}
390
Jiri Olsa89812fc2012-03-15 20:09:15 +0100391static int add_event(struct list_head *list, int *idx,
392 struct perf_event_attr *attr, char *name)
393{
394 struct perf_evsel *evsel;
395
396 event_attr_init(attr);
397
398 evsel = perf_evsel__new(attr, (*idx)++);
399 if (!evsel)
400 return -ENOMEM;
401
402 list_add_tail(&evsel->node, list);
403
404 evsel->name = strdup(name);
405 return 0;
406}
407
408static int parse_aliases(char *str, const char *names[][MAX_ALIASES], int size)
Ingo Molnar8326f442009-06-05 20:22:46 +0200409{
410 int i, j;
Paul Mackerras61c45982009-07-01 13:04:34 +1000411 int n, longest = -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200412
413 for (i = 0; i < size; i++) {
Paul Mackerras61c45982009-07-01 13:04:34 +1000414 for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
415 n = strlen(names[i][j]);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100416 if (n > longest && !strncasecmp(str, names[i][j], n))
Paul Mackerras61c45982009-07-01 13:04:34 +1000417 longest = n;
418 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100419 if (longest > 0)
Paul Mackerras61c45982009-07-01 13:04:34 +1000420 return i;
Ingo Molnar8326f442009-06-05 20:22:46 +0200421 }
422
Ingo Molnar89536452009-06-06 21:04:17 +0200423 return -1;
Ingo Molnar8326f442009-06-05 20:22:46 +0200424}
425
Jiri Olsa89812fc2012-03-15 20:09:15 +0100426int parse_events_add_cache(struct list_head *list, int *idx,
427 char *type, char *op_result1, char *op_result2)
Ingo Molnar8326f442009-06-05 20:22:46 +0200428{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100429 struct perf_event_attr attr;
430 char name[MAX_NAME_LEN];
Paul Mackerras61c45982009-07-01 13:04:34 +1000431 int cache_type = -1, cache_op = -1, cache_result = -1;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100432 char *op_result[2] = { op_result1, op_result2 };
433 int i, n;
Ingo Molnar8326f442009-06-05 20:22:46 +0200434
Ingo Molnar8326f442009-06-05 20:22:46 +0200435 /*
436 * No fallback - if we cannot get a clear cache type
437 * then bail out:
438 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100439 cache_type = parse_aliases(type, hw_cache,
440 PERF_COUNT_HW_CACHE_MAX);
Ingo Molnar8326f442009-06-05 20:22:46 +0200441 if (cache_type == -1)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100442 return -EINVAL;
Ingo Molnar8326f442009-06-05 20:22:46 +0200443
Jiri Olsa89812fc2012-03-15 20:09:15 +0100444 n = snprintf(name, MAX_NAME_LEN, "%s", type);
445
446 for (i = 0; (i < 2) && (op_result[i]); i++) {
447 char *str = op_result[i];
448
449 snprintf(name + n, MAX_NAME_LEN - n, "-%s\n", str);
Paul Mackerras61c45982009-07-01 13:04:34 +1000450
451 if (cache_op == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100452 cache_op = parse_aliases(str, hw_cache_op,
453 PERF_COUNT_HW_CACHE_OP_MAX);
Paul Mackerras61c45982009-07-01 13:04:34 +1000454 if (cache_op >= 0) {
455 if (!is_cache_op_valid(cache_type, cache_op))
Jiri Olsa89812fc2012-03-15 20:09:15 +0100456 return -EINVAL;
Paul Mackerras61c45982009-07-01 13:04:34 +1000457 continue;
458 }
459 }
460
461 if (cache_result == -1) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100462 cache_result = parse_aliases(str, hw_cache_result,
Paul Mackerras61c45982009-07-01 13:04:34 +1000463 PERF_COUNT_HW_CACHE_RESULT_MAX);
464 if (cache_result >= 0)
465 continue;
466 }
Paul Mackerras61c45982009-07-01 13:04:34 +1000467 }
468
Ingo Molnar8326f442009-06-05 20:22:46 +0200469 /*
470 * Fall back to reads:
471 */
Ingo Molnar89536452009-06-06 21:04:17 +0200472 if (cache_op == -1)
473 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
Ingo Molnar8326f442009-06-05 20:22:46 +0200474
Ingo Molnar8326f442009-06-05 20:22:46 +0200475 /*
476 * Fall back to accesses:
477 */
478 if (cache_result == -1)
479 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
480
Jiri Olsa89812fc2012-03-15 20:09:15 +0100481 memset(&attr, 0, sizeof(attr));
482 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
483 attr.type = PERF_TYPE_HW_CACHE;
484 return add_event(list, idx, &attr, name);
Ingo Molnar8326f442009-06-05 20:22:46 +0200485}
486
Jiri Olsa89812fc2012-03-15 20:09:15 +0100487static int add_tracepoint(struct list_head *list, int *idx,
488 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200489{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100490 struct perf_event_attr attr;
491 char name[MAX_NAME_LEN];
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200492 char evt_path[MAXPATHLEN];
493 char id_buf[4];
494 u64 id;
495 int fd;
496
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200497 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200498 sys_name, evt_name);
499
500 fd = open(evt_path, O_RDONLY);
501 if (fd < 0)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100502 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200503
504 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
505 close(fd);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100506 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200507 }
508
509 close(fd);
510 id = atoll(id_buf);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200511
Jiri Olsa89812fc2012-03-15 20:09:15 +0100512 memset(&attr, 0, sizeof(attr));
513 attr.config = id;
514 attr.type = PERF_TYPE_TRACEPOINT;
515 attr.sample_type |= PERF_SAMPLE_RAW;
516 attr.sample_type |= PERF_SAMPLE_TIME;
517 attr.sample_type |= PERF_SAMPLE_CPU;
518 attr.sample_period = 1;
Stephane Eranian5710fca2010-04-21 18:06:01 +0200519
Jiri Olsa89812fc2012-03-15 20:09:15 +0100520 snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
521 return add_event(list, idx, &attr, name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200522}
523
Jiri Olsa89812fc2012-03-15 20:09:15 +0100524static int add_tracepoint_multi(struct list_head *list, int *idx,
525 char *sys_name, char *evt_name)
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200526{
527 char evt_path[MAXPATHLEN];
528 struct dirent *evt_ent;
529 DIR *evt_dir;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100530 int ret = 0;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200531
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200532 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200533 evt_dir = opendir(evt_path);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200534 if (!evt_dir) {
535 perror("Can't open event dir");
Jiri Olsa89812fc2012-03-15 20:09:15 +0100536 return -1;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200537 }
538
Jiri Olsa89812fc2012-03-15 20:09:15 +0100539 while (!ret && (evt_ent = readdir(evt_dir))) {
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200540 if (!strcmp(evt_ent->d_name, ".")
541 || !strcmp(evt_ent->d_name, "..")
542 || !strcmp(evt_ent->d_name, "enable")
543 || !strcmp(evt_ent->d_name, "filter"))
544 continue;
545
Jiri Olsa89812fc2012-03-15 20:09:15 +0100546 if (!strglobmatch(evt_ent->d_name, evt_name))
Masami Hiramatsufb1d2ed2010-01-05 17:47:17 -0500547 continue;
548
Jiri Olsa89812fc2012-03-15 20:09:15 +0100549 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200550 }
551
Jiri Olsa89812fc2012-03-15 20:09:15 +0100552 return ret;
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200553}
554
Jiri Olsa89812fc2012-03-15 20:09:15 +0100555int parse_events_add_tracepoint(struct list_head *list, int *idx,
556 char *sys, char *event)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400557{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100558 int ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400559
Jiri Olsa89812fc2012-03-15 20:09:15 +0100560 ret = debugfs_valid_mountpoint(tracing_events_path);
561 if (ret)
562 return ret;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400563
Jiri Olsa89812fc2012-03-15 20:09:15 +0100564 return strpbrk(event, "*?") ?
565 add_tracepoint_multi(list, idx, sys, event) :
566 add_tracepoint(list, idx, sys, event);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400567}
568
Jiri Olsa89812fc2012-03-15 20:09:15 +0100569static int
570parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100571{
572 int i;
573
574 for (i = 0; i < 3; i++) {
Jiri Olsa89812fc2012-03-15 20:09:15 +0100575 if (!type || !type[i])
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100576 break;
577
578 switch (type[i]) {
579 case 'r':
580 attr->bp_type |= HW_BREAKPOINT_R;
581 break;
582 case 'w':
583 attr->bp_type |= HW_BREAKPOINT_W;
584 break;
585 case 'x':
586 attr->bp_type |= HW_BREAKPOINT_X;
587 break;
588 default:
Jiri Olsa89812fc2012-03-15 20:09:15 +0100589 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100590 }
591 }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100592
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100593 if (!attr->bp_type) /* Default */
594 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
595
Jiri Olsa89812fc2012-03-15 20:09:15 +0100596 return 0;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100597}
598
Jiri Olsa89812fc2012-03-15 20:09:15 +0100599int parse_events_add_breakpoint(struct list_head *list, int *idx,
600 void *ptr, char *type)
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100601{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100602 struct perf_event_attr attr;
603 char name[MAX_NAME_LEN];
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100604
Jiri Olsa89812fc2012-03-15 20:09:15 +0100605 memset(&attr, 0, sizeof(attr));
Jiri Olsa9fafd982012-03-20 19:15:39 +0100606 attr.bp_addr = (unsigned long) ptr;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100607
Jiri Olsa89812fc2012-03-15 20:09:15 +0100608 if (parse_breakpoint_type(type, &attr))
609 return -EINVAL;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100610
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200611 /*
612 * We should find a nice way to override the access length
613 * Provide some defaults for now
614 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100615 if (attr.bp_type == HW_BREAKPOINT_X)
616 attr.bp_len = sizeof(long);
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200617 else
Jiri Olsa89812fc2012-03-15 20:09:15 +0100618 attr.bp_len = HW_BREAKPOINT_LEN_4;
Frederic Weisbeckeraa59a482010-06-24 21:36:19 +0200619
Jiri Olsa89812fc2012-03-15 20:09:15 +0100620 attr.type = PERF_TYPE_BREAKPOINT;
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100621
Jiri Olsa89812fc2012-03-15 20:09:15 +0100622 snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
623 return add_event(list, idx, &attr, name);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +0100624}
625
Jiri Olsa8f707d82012-03-15 20:09:16 +0100626static int config_term(struct perf_event_attr *attr,
627 struct parse_events__term *term)
628{
629 switch (term->type) {
630 case PARSE_EVENTS__TERM_TYPE_CONFIG:
631 attr->config = term->val.num;
632 break;
633 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
634 attr->config1 = term->val.num;
635 break;
636 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
637 attr->config2 = term->val.num;
638 break;
639 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
640 attr->sample_period = term->val.num;
641 break;
642 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
643 /*
644 * TODO uncomment when the field is available
645 * attr->branch_sample_type = term->val.num;
646 */
647 break;
648 default:
649 return -EINVAL;
650 }
651 return 0;
652}
653
654static int config_attr(struct perf_event_attr *attr,
655 struct list_head *head, int fail)
656{
657 struct parse_events__term *term;
658
659 list_for_each_entry(term, head, list)
660 if (config_term(attr, term) && fail)
661 return -EINVAL;
662
663 return 0;
664}
665
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400666int parse_events_add_numeric_legacy(struct list_head *list, int *idx,
667 const char *name, unsigned long config,
668 struct list_head *head_config)
669{
670 struct perf_event_attr attr;
671 struct perf_pmu *pmu;
672 char *pmu_name = strdup(name);
673
674 memset(&attr, 0, sizeof(attr));
675
676 pmu = perf_pmu__find(pmu_name);
677
678 if (!pmu)
679 return -EINVAL;
680
681 attr.type = pmu->type;
682 attr.config = config;
683
684 if (head_config &&
685 config_attr(&attr, head_config, 1))
686 return -EINVAL;
687
688 return add_event(list, idx, &attr,
689 (char *) __event_name(pmu->type, config, pmu_name));
690}
691
Jiri Olsa8f707d82012-03-15 20:09:16 +0100692int parse_events_add_numeric(struct list_head *list, int *idx,
693 unsigned long type, unsigned long config,
694 struct list_head *head_config)
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530695{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100696 struct perf_event_attr attr;
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530697
Jiri Olsa89812fc2012-03-15 20:09:15 +0100698 memset(&attr, 0, sizeof(attr));
699 attr.type = type;
700 attr.config = config;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100701
702 if (head_config &&
703 config_attr(&attr, head_config, 1))
704 return -EINVAL;
705
Jiri Olsa89812fc2012-03-15 20:09:15 +0100706 return add_event(list, idx, &attr,
Ashwin Chaugulef4a533d2012-07-19 18:27:03 -0400707 (char *) __event_name(type, config, NULL));
Paul Mackerras61c45982009-07-01 13:04:34 +1000708}
709
Jiri Olsa5f537a22012-03-15 20:09:18 +0100710int parse_events_add_pmu(struct list_head *list, int *idx,
711 char *name, struct list_head *head_config)
712{
713 struct perf_event_attr attr;
714 struct perf_pmu *pmu;
Ashwin Chaugule3d0945d2012-09-20 10:53:42 -0400715 char *ev_name;
Jiri Olsa5f537a22012-03-15 20:09:18 +0100716
717 pmu = perf_pmu__find(name);
718 if (!pmu)
719 return -EINVAL;
720
721 memset(&attr, 0, sizeof(attr));
722
723 /*
724 * Configure hardcoded terms first, no need to check
725 * return value when called with fail == 0 ;)
726 */
727 config_attr(&attr, head_config, 0);
728
729 if (perf_pmu__config(pmu, &attr, head_config))
730 return -EINVAL;
731
Ashwin Chaugule3d0945d2012-09-20 10:53:42 -0400732 ev_name = (char *) __event_name(attr.type, attr.config, pmu->name);
733
734 return add_event(list, idx, &attr, ev_name);
Jiri Olsa5f537a22012-03-15 20:09:18 +0100735}
736
Jiri Olsa5d7be902012-03-20 19:15:40 +0100737void parse_events_update_lists(struct list_head *list_event,
738 struct list_head *list_all)
739{
740 /*
741 * Called for single event definition. Update the
742 * 'all event' list, and reinit the 'signle event'
743 * list, for next event definition.
744 */
745 list_splice_tail(list_event, list_all);
746 INIT_LIST_HEAD(list_event);
747}
748
Jiri Olsa89812fc2012-03-15 20:09:15 +0100749int parse_events_modifier(struct list_head *list, char *str)
Paul Mackerras61c45982009-07-01 13:04:34 +1000750{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100751 struct perf_evsel *evsel;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100752 int exclude = 0, exclude_GH = 0;
753 int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
Paul Mackerras61c45982009-07-01 13:04:34 +1000754
Jiri Olsa89812fc2012-03-15 20:09:15 +0100755 if (str == NULL)
Paul Mackerras61c45982009-07-01 13:04:34 +1000756 return 0;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200757
Paul Mackerras61c45982009-07-01 13:04:34 +1000758 while (*str) {
Peter Zijlstraab608342010-04-08 23:03:20 +0200759 if (*str == 'u') {
760 if (!exclude)
761 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000762 eu = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200763 } else if (*str == 'k') {
764 if (!exclude)
765 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000766 ek = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200767 } else if (*str == 'h') {
768 if (!exclude)
769 exclude = eu = ek = eh = 1;
Paul Mackerras61c45982009-07-01 13:04:34 +1000770 eh = 0;
Joerg Roedel99320cc2012-01-04 17:54:19 +0100771 } else if (*str == 'G') {
772 if (!exclude_GH)
773 exclude_GH = eG = eH = 1;
774 eG = 0;
775 } else if (*str == 'H') {
776 if (!exclude_GH)
777 exclude_GH = eG = eH = 1;
778 eH = 0;
Peter Zijlstraab608342010-04-08 23:03:20 +0200779 } else if (*str == 'p') {
780 precise++;
781 } else
Paul Mackerras61c45982009-07-01 13:04:34 +1000782 break;
Peter Zijlstraab608342010-04-08 23:03:20 +0200783
Paul Mackerras61c45982009-07-01 13:04:34 +1000784 ++str;
785 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200786
Jiri Olsa89812fc2012-03-15 20:09:15 +0100787 /*
788 * precise ip:
789 *
790 * 0 - SAMPLE_IP can have arbitrary skid
791 * 1 - SAMPLE_IP must have constant skid
792 * 2 - SAMPLE_IP requested to have 0 skid
793 * 3 - SAMPLE_IP must have 0 skid
794 *
795 * See also PERF_RECORD_MISC_EXACT_IP
796 */
797 if (precise > 3)
798 return -EINVAL;
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200799
Jiri Olsa89812fc2012-03-15 20:09:15 +0100800 list_for_each_entry(evsel, list, node) {
801 evsel->attr.exclude_user = eu;
802 evsel->attr.exclude_kernel = ek;
803 evsel->attr.exclude_hv = eh;
804 evsel->attr.precise_ip = precise;
805 evsel->attr.exclude_host = eH;
806 evsel->attr.exclude_guest = eG;
807 }
Ingo Molnarceb53fb2011-04-27 04:06:33 +0200808
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +0530809 return 0;
810}
811
Jiri Olsa89812fc2012-03-15 20:09:15 +0100812int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200813{
Jiri Olsa89812fc2012-03-15 20:09:15 +0100814 LIST_HEAD(list);
Jiri Olsa5d7be902012-03-20 19:15:40 +0100815 LIST_HEAD(list_tmp);
Jiri Olsa89812fc2012-03-15 20:09:15 +0100816 YY_BUFFER_STATE buffer;
817 int ret, idx = evlist->nr_entries;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200818
Jiri Olsa89812fc2012-03-15 20:09:15 +0100819 buffer = parse_events__scan_string(str);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200820
Jiri Olsa5d7be902012-03-20 19:15:40 +0100821 ret = parse_events_parse(&list, &list_tmp, &idx);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200822
Jiri Olsa89812fc2012-03-15 20:09:15 +0100823 parse_events__flush_buffer(buffer);
824 parse_events__delete_buffer(buffer);
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200825
Jiri Olsa89812fc2012-03-15 20:09:15 +0100826 if (!ret) {
827 int entries = idx - evlist->nr_entries;
828 perf_evlist__splice_list_tail(evlist, &list, entries);
829 return 0;
830 }
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200831
Jiri Olsa5d7be902012-03-20 19:15:40 +0100832 /*
833 * There are 2 users - builtin-record and builtin-test objects.
834 * Both call perf_evlist__delete in case of error, so we dont
835 * need to bother.
836 */
Jiri Olsa89812fc2012-03-15 20:09:15 +0100837 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
Marti Raudsepp85df6f62009-10-27 00:33:04 +0000838 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
Frederic Weisbeckerbcd32792009-09-11 23:19:45 +0200839 return ret;
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200840}
841
Jiri Olsaf120f9d2011-07-14 11:25:32 +0200842int parse_events_option(const struct option *opt, const char *str,
843 int unset __used)
844{
845 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
846 return parse_events(evlist, str, unset);
847}
848
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200849int parse_filter(const struct option *opt, const char *str,
Li Zefanc171b552009-10-15 11:22:07 +0800850 int unset __used)
851{
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200852 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200853 struct perf_evsel *last = NULL;
Li Zefanc171b552009-10-15 11:22:07 +0800854
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200855 if (evlist->nr_entries > 0)
856 last = list_entry(evlist->entries.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200857
858 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
Li Zefanc171b552009-10-15 11:22:07 +0800859 fprintf(stderr,
860 "-F option should follow a -e tracepoint option\n");
861 return -1;
862 }
863
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200864 last->filter = strdup(str);
865 if (last->filter == NULL) {
Li Zefanc171b552009-10-15 11:22:07 +0800866 fprintf(stderr, "not enough memory to hold filter string\n");
867 return -1;
868 }
Li Zefanc171b552009-10-15 11:22:07 +0800869
870 return 0;
871}
872
Thomas Gleixner86847b62009-06-06 12:24:17 +0200873static const char * const event_type_descriptors[] = {
Thomas Gleixner86847b62009-06-06 12:24:17 +0200874 "Hardware event",
875 "Software event",
876 "Tracepoint event",
877 "Hardware cache event",
Liming Wang41bdcb22009-12-29 16:37:07 +0800878 "Raw hardware event descriptor",
879 "Hardware breakpoint",
Thomas Gleixner86847b62009-06-06 12:24:17 +0200880};
881
Ingo Molnar8ad8db32009-05-26 11:10:09 +0200882/*
Jason Baronf6bdafe2009-07-21 12:20:22 -0400883 * Print the events from <debugfs_mount_point>/tracing/events
884 */
885
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200886void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
Jason Baronf6bdafe2009-07-21 12:20:22 -0400887{
888 DIR *sys_dir, *evt_dir;
889 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400890 char evt_path[MAXPATHLEN];
Eric Dumazet725b1362009-09-24 15:39:09 +0200891 char dir_path[MAXPATHLEN];
Jason Baronf6bdafe2009-07-21 12:20:22 -0400892
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200893 if (debugfs_valid_mountpoint(tracing_events_path))
Jason Baronf6bdafe2009-07-21 12:20:22 -0400894 return;
895
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200896 sys_dir = opendir(tracing_events_path);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400897 if (!sys_dir)
Eric Dumazet725b1362009-09-24 15:39:09 +0200898 return;
Jason Baronf6bdafe2009-07-21 12:20:22 -0400899
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300900 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200901 if (subsys_glob != NULL &&
902 !strglobmatch(sys_dirent.d_name, subsys_glob))
903 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200904
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200905 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Eric Dumazet725b1362009-09-24 15:39:09 +0200906 sys_dirent.d_name);
907 evt_dir = opendir(dir_path);
908 if (!evt_dir)
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300909 continue;
Eric Dumazet725b1362009-09-24 15:39:09 +0200910
Ulrich Drepper6b58e7f2009-09-04 16:39:51 -0300911 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200912 if (event_glob != NULL &&
913 !strglobmatch(evt_dirent.d_name, event_glob))
914 continue;
915
Jason Baronf6bdafe2009-07-21 12:20:22 -0400916 snprintf(evt_path, MAXPATHLEN, "%s:%s",
917 sys_dirent.d_name, evt_dirent.d_name);
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200918 printf(" %-50s [%s]\n", evt_path,
Liming Wang41bdcb22009-12-29 16:37:07 +0800919 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
Jason Baronf6bdafe2009-07-21 12:20:22 -0400920 }
921 closedir(evt_dir);
922 }
Jason Baronf6bdafe2009-07-21 12:20:22 -0400923 closedir(sys_dir);
924}
925
926/*
Thomas Renninger20c457b2011-01-03 17:50:45 +0100927 * Check whether event is in <debugfs_mount_point>/tracing/events
928 */
929
930int is_valid_tracepoint(const char *event_string)
931{
932 DIR *sys_dir, *evt_dir;
933 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
934 char evt_path[MAXPATHLEN];
935 char dir_path[MAXPATHLEN];
936
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200937 if (debugfs_valid_mountpoint(tracing_events_path))
Thomas Renninger20c457b2011-01-03 17:50:45 +0100938 return 0;
939
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200940 sys_dir = opendir(tracing_events_path);
Thomas Renninger20c457b2011-01-03 17:50:45 +0100941 if (!sys_dir)
942 return 0;
943
944 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
945
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200946 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
Thomas Renninger20c457b2011-01-03 17:50:45 +0100947 sys_dirent.d_name);
948 evt_dir = opendir(dir_path);
949 if (!evt_dir)
950 continue;
951
952 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
953 snprintf(evt_path, MAXPATHLEN, "%s:%s",
954 sys_dirent.d_name, evt_dirent.d_name);
955 if (!strcmp(evt_path, event_string)) {
956 closedir(evt_dir);
957 closedir(sys_dir);
958 return 1;
959 }
960 }
961 closedir(evt_dir);
962 }
963 closedir(sys_dir);
964 return 0;
965}
966
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200967void print_events_type(u8 type)
968{
969 struct event_symbol *syms = event_symbols;
970 unsigned int i;
971 char name[64];
972
973 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
974 if (type != syms->type)
975 continue;
976
977 if (strlen(syms->alias))
978 snprintf(name, sizeof(name), "%s OR %s",
979 syms->symbol, syms->alias);
980 else
981 snprintf(name, sizeof(name), "%s", syms->symbol);
982
Ingo Molnar947b4ad2011-04-29 22:52:42 +0200983 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -0200984 event_type_descriptors[type]);
985 }
986}
987
988int print_hwcache_events(const char *event_glob)
989{
990 unsigned int type, op, i, printed = 0;
991
992 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
993 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
994 /* skip invalid cache type */
995 if (!is_cache_op_valid(type, op))
996 continue;
997
998 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
999 char *name = event_cache_name(type, op, i);
1000
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001001 if (event_glob != NULL && !strglobmatch(name, event_glob))
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001002 continue;
1003
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001004 printf(" %-50s [%s]\n", name,
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001005 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1006 ++printed;
1007 }
1008 }
1009 }
1010
1011 return printed;
1012}
1013
Thomas Renninger20c457b2011-01-03 17:50:45 +01001014/*
Thomas Gleixner86847b62009-06-06 12:24:17 +02001015 * Print the help text for the event symbols:
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001016 */
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001017void print_events(const char *event_glob)
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001018{
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001019 unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001020 struct event_symbol *syms = event_symbols;
1021 char name[MAX_NAME_LEN];
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001022
Marti Raudsepp689d3012009-10-27 00:33:05 +00001023 printf("\n");
1024 printf("List of pre-defined events (to be used in -e):\n");
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001025
Thomas Gleixner86847b62009-06-06 12:24:17 +02001026 for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
Liming Wang41bdcb22009-12-29 16:37:07 +08001027 type = syms->type;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001028
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001029 if (type != prev_type && printed) {
Marti Raudsepp689d3012009-10-27 00:33:05 +00001030 printf("\n");
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001031 printed = 0;
1032 ntypes_printed++;
1033 }
1034
1035 if (event_glob != NULL &&
1036 !(strglobmatch(syms->symbol, event_glob) ||
1037 (syms->alias && strglobmatch(syms->alias, event_glob))))
1038 continue;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001039
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301040 if (strlen(syms->alias))
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001041 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
Jaswinder Singh Rajput74d5b582009-06-22 16:44:28 +05301042 else
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001043 strncpy(name, syms->symbol, MAX_NAME_LEN);
1044 printf(" %-50s [%s]\n", name,
Thomas Gleixner86847b62009-06-06 12:24:17 +02001045 event_type_descriptors[type]);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001046
Thomas Gleixner86847b62009-06-06 12:24:17 +02001047 prev_type = type;
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001048 ++printed;
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001049 }
1050
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001051 if (ntypes_printed) {
1052 printed = 0;
1053 printf("\n");
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301054 }
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001055 print_hwcache_events(event_glob);
1056
1057 if (event_glob != NULL)
1058 return;
Jaswinder Singh Rajput73c24cb2009-07-01 18:36:18 +05301059
Marti Raudsepp689d3012009-10-27 00:33:05 +00001060 printf("\n");
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001061 printf(" %-50s [%s]\n",
Jiri Olsa5f537a22012-03-15 20:09:18 +01001062 "rNNN",
Arnaldo Carvalho de Melo1cf4a062010-05-07 14:07:05 -03001063 event_type_descriptors[PERF_TYPE_RAW]);
Jiri Olsa5f537a22012-03-15 20:09:18 +01001064 printf(" %-50s [%s]\n",
1065 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1066 event_type_descriptors[PERF_TYPE_RAW]);
1067 printf(" (see 'perf list --help' on how to encode it)\n");
Marti Raudsepp689d3012009-10-27 00:33:05 +00001068 printf("\n");
Thomas Gleixner86847b62009-06-06 12:24:17 +02001069
Ingo Molnar947b4ad2011-04-29 22:52:42 +02001070 printf(" %-50s [%s]\n",
Liming Wang41bdcb22009-12-29 16:37:07 +08001071 "mem:<addr>[:access]",
1072 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
Frederic Weisbecker1b290d62009-11-23 15:42:35 +01001073 printf("\n");
1074
Arnaldo Carvalho de Melo668b8782011-02-17 15:38:58 -02001075 print_tracepoint_events(NULL, NULL);
Ingo Molnar8ad8db32009-05-26 11:10:09 +02001076}
Jiri Olsa8f707d82012-03-15 20:09:16 +01001077
1078int parse_events__is_hardcoded_term(struct parse_events__term *term)
1079{
1080 return term->type <= PARSE_EVENTS__TERM_TYPE_HARDCODED_MAX;
1081}
1082
1083int parse_events__new_term(struct parse_events__term **_term, int type,
1084 char *config, char *str, long num)
1085{
1086 struct parse_events__term *term;
1087
1088 term = zalloc(sizeof(*term));
1089 if (!term)
1090 return -ENOMEM;
1091
1092 INIT_LIST_HEAD(&term->list);
1093 term->type = type;
1094 term->config = config;
1095
1096 switch (type) {
1097 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1098 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1099 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1100 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1101 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1102 case PARSE_EVENTS__TERM_TYPE_NUM:
1103 term->val.num = num;
1104 break;
1105 case PARSE_EVENTS__TERM_TYPE_STR:
1106 term->val.str = str;
1107 break;
1108 default:
1109 return -EINVAL;
1110 }
1111
1112 *_term = term;
1113 return 0;
1114}
1115
1116void parse_events__free_terms(struct list_head *terms)
1117{
1118 struct parse_events__term *term, *h;
1119
1120 list_for_each_entry_safe(term, h, terms, list)
1121 free(term);
1122
1123 free(terms);
1124}