blob: a1a01b1ac8b86a5dff96a60042c10b5763a49721 [file] [log] [blame]
Jiri Olsa89812fc2012-03-15 20:09:15 +01001
Zheng Yanac20de6f2012-06-15 14:31:39 +08002%option reentrant
3%option bison-bridge
Jiri Olsa89812fc2012-03-15 20:09:15 +01004%option prefix="parse_events_"
Jiri Olsa08d2f762012-05-21 09:12:52 +02005%option stack
Jiri Olsa6297d422015-04-22 21:10:17 +02006%option bison-locations
7%option yylineno
Jiri Olsab6af53b2017-11-08 16:43:09 +01008%option reject
Jiri Olsa89812fc2012-03-15 20:09:15 +01009
10%{
11#include <errno.h>
Jiri Olsa29479bf2017-10-13 10:37:35 +020012#include <sys/types.h>
13#include <sys/stat.h>
14#include <unistd.h>
Jiri Olsa89812fc2012-03-15 20:09:15 +010015#include "../perf.h"
Jiri Olsa8f707d82012-03-15 20:09:16 +010016#include "parse-events.h"
Wang Nane571e022016-02-22 09:10:35 +000017#include "parse-events-bison.h"
Jiri Olsa89812fc2012-03-15 20:09:15 +010018
Zheng Yanac20de6f2012-06-15 14:31:39 +080019char *parse_events_get_text(yyscan_t yyscanner);
20YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
21
22static int __value(YYSTYPE *yylval, char *str, int base, int token)
Jiri Olsa89812fc2012-03-15 20:09:15 +010023{
Robert Richterb527bab2012-08-07 19:43:13 +020024 u64 num;
Jiri Olsa89812fc2012-03-15 20:09:15 +010025
26 errno = 0;
Robert Richterb527bab2012-08-07 19:43:13 +020027 num = strtoull(str, NULL, base);
Jiri Olsa89812fc2012-03-15 20:09:15 +010028 if (errno)
29 return PE_ERROR;
30
Zheng Yanac20de6f2012-06-15 14:31:39 +080031 yylval->num = num;
Jiri Olsa89812fc2012-03-15 20:09:15 +010032 return token;
33}
34
Zheng Yanac20de6f2012-06-15 14:31:39 +080035static int value(yyscan_t scanner, int base)
Jiri Olsa89812fc2012-03-15 20:09:15 +010036{
Zheng Yanac20de6f2012-06-15 14:31:39 +080037 YYSTYPE *yylval = parse_events_get_lval(scanner);
38 char *text = parse_events_get_text(scanner);
39
40 return __value(yylval, text, base, PE_VALUE);
Jiri Olsa89812fc2012-03-15 20:09:15 +010041}
42
Zheng Yanac20de6f2012-06-15 14:31:39 +080043static int raw(yyscan_t scanner)
Jiri Olsa89812fc2012-03-15 20:09:15 +010044{
Zheng Yanac20de6f2012-06-15 14:31:39 +080045 YYSTYPE *yylval = parse_events_get_lval(scanner);
46 char *text = parse_events_get_text(scanner);
47
48 return __value(yylval, text + 1, 16, PE_RAW);
Jiri Olsa89812fc2012-03-15 20:09:15 +010049}
50
Zheng Yanac20de6f2012-06-15 14:31:39 +080051static int str(yyscan_t scanner, int token)
Jiri Olsa89812fc2012-03-15 20:09:15 +010052{
Zheng Yanac20de6f2012-06-15 14:31:39 +080053 YYSTYPE *yylval = parse_events_get_lval(scanner);
54 char *text = parse_events_get_text(scanner);
55
56 yylval->str = strdup(text);
Jiri Olsa89812fc2012-03-15 20:09:15 +010057 return token;
58}
59
Jiri Olsa29479bf2017-10-13 10:37:35 +020060static bool isbpf_suffix(char *text)
Andi Kleen77d08712017-08-11 16:26:19 -070061{
Andi Kleen77d08712017-08-11 16:26:19 -070062 int len = strlen(text);
63
64 if (len < 2)
65 return false;
66 if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
67 text[len - 2] == '.')
68 return true;
69 if (len > 4 && !strcmp(text + len - 4, ".obj"))
70 return true;
71 return false;
72}
73
Jiri Olsa29479bf2017-10-13 10:37:35 +020074static bool isbpf(yyscan_t scanner)
75{
76 char *text = parse_events_get_text(scanner);
77 struct stat st;
78
79 if (!isbpf_suffix(text))
80 return false;
81
82 return stat(text, &st) == 0;
83}
84
Mathieu Poirierdd60fba2016-09-06 10:37:15 -060085/*
86 * This function is called when the parser gets two kind of input:
87 *
88 * @cfg1 or @cfg2=config
89 *
90 * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
91 * bison. In the latter case it is necessary to keep the string intact so that
92 * the PMU kernel driver can determine what configurable is associated to
93 * 'config'.
94 */
95static int drv_str(yyscan_t scanner, int token)
96{
97 YYSTYPE *yylval = parse_events_get_lval(scanner);
98 char *text = parse_events_get_text(scanner);
99
100 /* Strip off the '@' */
101 yylval->str = strdup(text + 1);
102 return token;
103}
104
Jiri Olsa6297d422015-04-22 21:10:17 +0200105#define REWIND(__alloc) \
106do { \
107 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \
108 char *text = parse_events_get_text(yyscanner); \
109 \
110 if (__alloc) \
111 __yylval->str = strdup(text); \
112 \
113 yycolumn -= strlen(text); \
114 yyless(0); \
115} while (0)
116
Kan Liangba32a452014-10-07 11:08:51 -0400117static int pmu_str_check(yyscan_t scanner)
118{
119 YYSTYPE *yylval = parse_events_get_lval(scanner);
120 char *text = parse_events_get_text(scanner);
121
122 yylval->str = strdup(text);
123 switch (perf_pmu__parse_check(text)) {
124 case PMU_EVENT_SYMBOL_PREFIX:
125 return PE_PMU_EVENT_PRE;
126 case PMU_EVENT_SYMBOL_SUFFIX:
127 return PE_PMU_EVENT_SUF;
128 case PMU_EVENT_SYMBOL:
129 return PE_KERNEL_PMU_EVENT;
130 default:
131 return PE_NAME;
132 }
133}
134
Zheng Yanac20de6f2012-06-15 14:31:39 +0800135static int sym(yyscan_t scanner, int type, int config)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100136{
Zheng Yanac20de6f2012-06-15 14:31:39 +0800137 YYSTYPE *yylval = parse_events_get_lval(scanner);
138
139 yylval->num = (type << 16) + config;
Jiri Olsacf3506d2012-07-04 00:00:43 +0200140 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
Jiri Olsa89812fc2012-03-15 20:09:15 +0100141}
142
Zheng Yanac20de6f2012-06-15 14:31:39 +0800143static int term(yyscan_t scanner, int type)
Jiri Olsa8f707d82012-03-15 20:09:16 +0100144{
Zheng Yanac20de6f2012-06-15 14:31:39 +0800145 YYSTYPE *yylval = parse_events_get_lval(scanner);
146
147 yylval->num = type;
Jiri Olsa8f707d82012-03-15 20:09:16 +0100148 return PE_TERM;
149}
150
Jiri Olsa6297d422015-04-22 21:10:17 +0200151#define YY_USER_ACTION \
152do { \
153 yylloc->last_column = yylloc->first_column; \
154 yylloc->first_column = yycolumn; \
155 yycolumn += yyleng; \
156} while (0);
157
Jiri Olsa94454642017-10-12 17:03:38 +0200158#define USER_REJECT \
159 yycolumn -= yyleng; \
160 REJECT
161
Jiri Olsa89812fc2012-03-15 20:09:15 +0100162%}
163
Jiri Olsa08d2f762012-05-21 09:12:52 +0200164%x mem
Robert Richterac2ba9f2012-08-16 21:10:21 +0200165%s config
166%x event
Wang Nane571e022016-02-22 09:10:35 +0000167%x array
Robert Richterac2ba9f2012-08-16 21:10:21 +0200168
169group [^,{}/]*[{][^}]*[}][^,{}/]*
170event_pmu [^,{}/]+[/][^/]*[/][^,{}/]*
171event [^,{}/]+
Andi Kleen77d08712017-08-11 16:26:19 -0700172bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
173bpf_source [^,{}]+\.c[a-zA-Z0-9._]*
Jiri Olsa08d2f762012-05-21 09:12:52 +0200174
Jiri Olsa89812fc2012-03-15 20:09:15 +0100175num_dec [0-9]+
176num_hex 0x[a-fA-F0-9]+
177num_raw_hex [a-fA-F0-9]+
Agustin Vega-Friasb2b9d3a2018-03-06 09:04:42 -0500178name [a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]]*
Wang Nana34f3be2016-02-22 09:10:31 +0000179name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
Mathieu Poirierdd60fba2016-09-06 10:37:15 -0600180drv_cfg_term [a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
Michael Ellermane9a7c412013-08-06 23:28:05 +1000181/* If you add a modifier you need to update check_modifier() */
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700182modifier_event [ukhpPGHSDIW]+
Jiri Olsa75827322012-06-29 09:22:54 +0200183modifier_bp [rwx]{1,3}
Jiri Olsa89812fc2012-03-15 20:09:15 +0100184
185%%
Jiri Olsa90e2b222012-06-15 14:31:40 +0800186
187%{
188 {
189 int start_token;
190
Robert Richterac2ba9f2012-08-16 21:10:21 +0200191 start_token = parse_events_get_extra(yyscanner);
192
193 if (start_token == PE_START_TERMS)
194 BEGIN(config);
195 else if (start_token == PE_START_EVENTS)
196 BEGIN(event);
197
Jiri Olsa90e2b222012-06-15 14:31:40 +0800198 if (start_token) {
199 parse_events_set_extra(NULL, yyscanner);
Jiri Olsa6297d422015-04-22 21:10:17 +0200200 /*
201 * The flex parser does not init locations variable
202 * via the scan_string interface, so we need do the
203 * init in here.
204 */
205 yycolumn = 0;
Jiri Olsa90e2b222012-06-15 14:31:40 +0800206 return start_token;
207 }
208 }
209%}
210
Robert Richterac2ba9f2012-08-16 21:10:21 +0200211<event>{
212
213{group} {
Jiri Olsa6297d422015-04-22 21:10:17 +0200214 BEGIN(INITIAL);
215 REWIND(0);
Robert Richterac2ba9f2012-08-16 21:10:21 +0200216 }
217
218{event_pmu} |
Wang Nan84c86ca2015-10-14 12:41:14 +0000219{bpf_object} |
Wang Nand509db02015-10-14 12:41:20 +0000220{bpf_source} |
Robert Richterac2ba9f2012-08-16 21:10:21 +0200221{event} {
Jiri Olsa6297d422015-04-22 21:10:17 +0200222 BEGIN(INITIAL);
223 REWIND(1);
Robert Richterac2ba9f2012-08-16 21:10:21 +0200224 return PE_EVENT_NAME;
225 }
226
Robert Richterac2ba9f2012-08-16 21:10:21 +0200227<<EOF>> {
Jiri Olsa6297d422015-04-22 21:10:17 +0200228 BEGIN(INITIAL);
229 REWIND(0);
Robert Richterac2ba9f2012-08-16 21:10:21 +0200230 }
231
232}
233
Wang Nane571e022016-02-22 09:10:35 +0000234<array>{
235"]" { BEGIN(config); return ']'; }
236{num_dec} { return value(yyscanner, 10); }
237{num_hex} { return value(yyscanner, 16); }
238, { return ','; }
239"\.\.\." { return PE_ARRAY_RANGE; }
240}
241
Jiri Olsa82004222013-09-27 18:29:58 +0200242<config>{
Jiri Olsae64b0202015-04-22 21:10:21 +0200243 /*
Wang Nan17cb5f82016-02-19 11:43:57 +0000244 * Please update config_term_names when new static term is added.
Jiri Olsae64b0202015-04-22 21:10:21 +0200245 */
Jiri Olsa82004222013-09-27 18:29:58 +0200246config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
247config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
248config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
249name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
250period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
Namhyung Kim09af2a52015-08-09 15:45:23 +0900251freq { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
Jiri Olsa82004222013-09-27 18:29:58 +0200252branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
Kan Liang32067712015-08-04 04:30:19 -0400253time { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
Kan Liangd457c962015-08-11 06:30:47 -0400254call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
255stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300256max-stack { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
Wang Nan374ce932015-10-28 10:55:02 +0000257inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
258no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
Wang Nan626a6b72016-07-14 08:34:45 +0000259overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
260no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
Jiri Olsa82004222013-09-27 18:29:58 +0200261, { return ','; }
262"/" { BEGIN(INITIAL); return '/'; }
263{name_minus} { return str(yyscanner, PE_NAME); }
Wang Nane571e022016-02-22 09:10:35 +0000264\[all\] { return PE_ARRAY_ALL; }
265"[" { BEGIN(array); return '['; }
Mathieu Poirierdd60fba2016-09-06 10:37:15 -0600266@{drv_cfg_term} { return drv_str(yyscanner, PE_DRV_CFG_TERM); }
Jiri Olsa82004222013-09-27 18:29:58 +0200267}
268
269<mem>{
270{modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); }
271: { return ':'; }
Jacob Shin3741eb92014-05-29 17:26:51 +0200272"/" { return '/'; }
Jiri Olsa82004222013-09-27 18:29:58 +0200273{num_dec} { return value(yyscanner, 10); }
274{num_hex} { return value(yyscanner, 16); }
275 /*
276 * We need to separate 'mem:' scanner part, in order to get specific
277 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
278 * and we'd need to parse it manually. During the escape from <mem>
279 * state we need to put the escaping char back, so we dont miss it.
280 */
281. { unput(*yytext); BEGIN(INITIAL); }
282 /*
283 * We destroy the scanner after reaching EOF,
284 * but anyway just to be sure get back to INIT state.
285 */
286<<EOF>> { BEGIN(INITIAL); }
287}
288
Zheng Yanac20de6f2012-06-15 14:31:39 +0800289cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
290stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
291stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
292instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
293cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
294cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
295branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
296branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
297bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
298ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
299cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
300task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
301page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
302minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
303major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
304context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
305cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
306alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
307emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300308dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
Andi Kleenfd48aad2017-08-31 12:40:34 -0700309duration_time { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
Wang Nan03e0a7d2016-02-22 09:10:37 +0000310bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100311
Kan Liangba32a452014-10-07 11:08:51 -0400312 /*
313 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
314 * Because the prefix cycles is mixed up with cpu-cycles.
315 * loads and stores are mixed up with cache event
316 */
317cycles-ct { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
318cycles-t { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
319mem-loads { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
320mem-stores { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
Andi Kleen44b1e602016-05-30 12:49:42 -0300321topdown-[a-z-]+ { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
Kan Liangba32a452014-10-07 11:08:51 -0400322
Jiri Olsa89812fc2012-03-15 20:09:15 +0100323L1-dcache|l1-d|l1d|L1-data |
324L1-icache|l1-i|l1i|L1-instruction |
325LLC|L2 |
326dTLB|d-tlb|Data-TLB |
327iTLB|i-tlb|Instruction-TLB |
328branch|branches|bpu|btb|bpc |
Zheng Yanac20de6f2012-06-15 14:31:39 +0800329node { return str(yyscanner, PE_NAME_CACHE_TYPE); }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100330
331load|loads|read |
332store|stores|write |
333prefetch|prefetches |
334speculative-read|speculative-load |
335refs|Reference|ops|access |
Zheng Yanac20de6f2012-06-15 14:31:39 +0800336misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100337
Jiri Olsa08d2f762012-05-21 09:12:52 +0200338mem: { BEGIN(mem); return PE_PREFIX_MEM; }
Zheng Yanac20de6f2012-06-15 14:31:39 +0800339r{num_raw_hex} { return raw(yyscanner); }
340{num_dec} { return value(yyscanner, 10); }
341{num_hex} { return value(yyscanner, 16); }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100342
Zheng Yanac20de6f2012-06-15 14:31:39 +0800343{modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); }
Jiri Olsaa271bfa2017-11-09 10:02:10 +0100344{bpf_object} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
345{bpf_source} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
Kan Liangba32a452014-10-07 11:08:51 -0400346{name} { return pmu_str_check(yyscanner); }
Robert Richterac2ba9f2012-08-16 21:10:21 +0200347"/" { BEGIN(config); return '/'; }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100348- { return '-'; }
Robert Richterac2ba9f2012-08-16 21:10:21 +0200349, { BEGIN(event); return ','; }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100350: { return ':'; }
Robert Richterac2ba9f2012-08-16 21:10:21 +0200351"{" { BEGIN(event); return '{'; }
Jiri Olsa89efb022012-08-08 12:14:14 +0200352"}" { return '}'; }
Jiri Olsa89812fc2012-03-15 20:09:15 +0100353= { return '='; }
Jiri Olsa8c5f0a82012-07-04 00:00:39 +0200354\n { }
Jiri Olsa82004222013-09-27 18:29:58 +0200355. { }
Jiri Olsa08d2f762012-05-21 09:12:52 +0200356
Jiri Olsa89812fc2012-03-15 20:09:15 +0100357%%
358
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300359int parse_events_wrap(void *scanner __maybe_unused)
Jiri Olsa89812fc2012-03-15 20:09:15 +0100360{
361 return 1;
362}