Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[muen/linux.git] / tools / perf / util / parse-events.l
1
2 %option reentrant
3 %option bison-bridge
4 %option prefix="parse_events_"
5 %option stack
6 %option bison-locations
7 %option yylineno
8
9 %{
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include "../perf.h"
15 #include "parse-events.h"
16 #include "parse-events-bison.h"
17
18 char *parse_events_get_text(yyscan_t yyscanner);
19 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
20
21 static int __value(YYSTYPE *yylval, char *str, int base, int token)
22 {
23         u64 num;
24
25         errno = 0;
26         num = strtoull(str, NULL, base);
27         if (errno)
28                 return PE_ERROR;
29
30         yylval->num = num;
31         return token;
32 }
33
34 static int value(yyscan_t scanner, int base)
35 {
36         YYSTYPE *yylval = parse_events_get_lval(scanner);
37         char *text = parse_events_get_text(scanner);
38
39         return __value(yylval, text, base, PE_VALUE);
40 }
41
42 static int raw(yyscan_t scanner)
43 {
44         YYSTYPE *yylval = parse_events_get_lval(scanner);
45         char *text = parse_events_get_text(scanner);
46
47         return __value(yylval, text + 1, 16, PE_RAW);
48 }
49
50 static int str(yyscan_t scanner, int token)
51 {
52         YYSTYPE *yylval = parse_events_get_lval(scanner);
53         char *text = parse_events_get_text(scanner);
54
55         yylval->str = strdup(text);
56         return token;
57 }
58
59 static bool isbpf_suffix(char *text)
60 {
61         int len = strlen(text);
62
63         if (len < 2)
64                 return false;
65         if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
66             text[len - 2] == '.')
67                 return true;
68         if (len > 4 && !strcmp(text + len - 4, ".obj"))
69                 return true;
70         return false;
71 }
72
73 static bool isbpf(yyscan_t scanner)
74 {
75         char *text = parse_events_get_text(scanner);
76         struct stat st;
77
78         if (!isbpf_suffix(text))
79                 return false;
80
81         return stat(text, &st) == 0;
82 }
83
84 /*
85  * This function is called when the parser gets two kind of input:
86  *
87  *      @cfg1 or @cfg2=config
88  *
89  * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
90  * bison.  In the latter case it is necessary to keep the string intact so that
91  * the PMU kernel driver can determine what configurable is associated to
92  * 'config'.
93  */
94 static int drv_str(yyscan_t scanner, int token)
95 {
96         YYSTYPE *yylval = parse_events_get_lval(scanner);
97         char *text = parse_events_get_text(scanner);
98
99         /* Strip off the '@' */
100         yylval->str = strdup(text + 1);
101         return token;
102 }
103
104 #define REWIND(__alloc)                         \
105 do {                                                            \
106         YYSTYPE *__yylval = parse_events_get_lval(yyscanner);   \
107         char *text = parse_events_get_text(yyscanner);          \
108                                                                 \
109         if (__alloc)                                            \
110                 __yylval->str = strdup(text);                   \
111                                                                 \
112         yycolumn -= strlen(text);                               \
113         yyless(0);                                              \
114 } while (0)
115
116 static int pmu_str_check(yyscan_t scanner)
117 {
118         YYSTYPE *yylval = parse_events_get_lval(scanner);
119         char *text = parse_events_get_text(scanner);
120
121         yylval->str = strdup(text);
122         switch (perf_pmu__parse_check(text)) {
123                 case PMU_EVENT_SYMBOL_PREFIX:
124                         return PE_PMU_EVENT_PRE;
125                 case PMU_EVENT_SYMBOL_SUFFIX:
126                         return PE_PMU_EVENT_SUF;
127                 case PMU_EVENT_SYMBOL:
128                         return PE_KERNEL_PMU_EVENT;
129                 default:
130                         return PE_NAME;
131         }
132 }
133
134 static int sym(yyscan_t scanner, int type, int config)
135 {
136         YYSTYPE *yylval = parse_events_get_lval(scanner);
137
138         yylval->num = (type << 16) + config;
139         return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
140 }
141
142 static int term(yyscan_t scanner, int type)
143 {
144         YYSTYPE *yylval = parse_events_get_lval(scanner);
145
146         yylval->num = type;
147         return PE_TERM;
148 }
149
150 #define YY_USER_ACTION                                  \
151 do {                                                    \
152         yylloc->last_column  = yylloc->first_column;    \
153         yylloc->first_column = yycolumn;                \
154         yycolumn += yyleng;                             \
155 } while (0);
156
157 #define USER_REJECT             \
158         yycolumn -= yyleng;     \
159         REJECT
160
161 %}
162
163 %x mem
164 %s config
165 %x event
166 %x array
167
168 group           [^,{}/]*[{][^}]*[}][^,{}/]*
169 event_pmu       [^,{}/]+[/][^/]*[/][^,{}/]*
170 event           [^,{}/]+
171 bpf_object      [^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
172 bpf_source      [^,{}]+\.c[a-zA-Z0-9._]*
173
174 num_dec         [0-9]+
175 num_hex         0x[a-fA-F0-9]+
176 num_raw_hex     [a-fA-F0-9]+
177 name            [a-zA-Z_*?][a-zA-Z0-9_*?.]*
178 name_minus      [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
179 drv_cfg_term    [a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
180 /* If you add a modifier you need to update check_modifier() */
181 modifier_event  [ukhpPGHSDI]+
182 modifier_bp     [rwx]{1,3}
183
184 %%
185
186 %{
187         {
188                 int start_token;
189
190                 start_token = parse_events_get_extra(yyscanner);
191
192                 if (start_token == PE_START_TERMS)
193                         BEGIN(config);
194                 else if (start_token == PE_START_EVENTS)
195                         BEGIN(event);
196
197                 if (start_token) {
198                         parse_events_set_extra(NULL, yyscanner);
199                         /*
200                          * The flex parser does not init locations variable
201                          * via the scan_string interface, so we need do the
202                          * init in here.
203                          */
204                         yycolumn = 0;
205                         return start_token;
206                 }
207          }
208 %}
209
210 <event>{
211
212 {group}         {
213                         BEGIN(INITIAL);
214                         REWIND(0);
215                 }
216
217 {event_pmu}     |
218 {bpf_object}    |
219 {bpf_source}    |
220 {event}         {
221                         BEGIN(INITIAL);
222                         REWIND(1);
223                         return PE_EVENT_NAME;
224                 }
225
226 <<EOF>>         {
227                         BEGIN(INITIAL);
228                         REWIND(0);
229                 }
230
231 }
232
233 <array>{
234 "]"                     { BEGIN(config); return ']'; }
235 {num_dec}               { return value(yyscanner, 10); }
236 {num_hex}               { return value(yyscanner, 16); }
237 ,                       { return ','; }
238 "\.\.\."                { return PE_ARRAY_RANGE; }
239 }
240
241 <config>{
242         /*
243          * Please update config_term_names when new static term is added.
244          */
245 config                  { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
246 config1                 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
247 config2                 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
248 name                    { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
249 period                  { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
250 freq                    { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
251 branch_type             { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
252 time                    { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
253 call-graph              { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
254 stack-size              { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
255 max-stack               { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
256 inherit                 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
257 no-inherit              { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
258 overwrite               { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
259 no-overwrite            { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
260 ,                       { return ','; }
261 "/"                     { BEGIN(INITIAL); return '/'; }
262 {name_minus}            { return str(yyscanner, PE_NAME); }
263 \[all\]                 { return PE_ARRAY_ALL; }
264 "["                     { BEGIN(array); return '['; }
265 @{drv_cfg_term}         { return drv_str(yyscanner, PE_DRV_CFG_TERM); }
266 }
267
268 <mem>{
269 {modifier_bp}           { return str(yyscanner, PE_MODIFIER_BP); }
270 :                       { return ':'; }
271 "/"                     { return '/'; }
272 {num_dec}               { return value(yyscanner, 10); }
273 {num_hex}               { return value(yyscanner, 16); }
274         /*
275          * We need to separate 'mem:' scanner part, in order to get specific
276          * modifier bits parsed out. Otherwise we would need to handle PE_NAME
277          * and we'd need to parse it manually. During the escape from <mem>
278          * state we need to put the escaping char back, so we dont miss it.
279          */
280 .                       { unput(*yytext); BEGIN(INITIAL); }
281         /*
282          * We destroy the scanner after reaching EOF,
283          * but anyway just to be sure get back to INIT state.
284          */
285 <<EOF>>                 { BEGIN(INITIAL); }
286 }
287
288 cpu-cycles|cycles                               { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
289 stalled-cycles-frontend|idle-cycles-frontend    { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
290 stalled-cycles-backend|idle-cycles-backend      { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
291 instructions                                    { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
292 cache-references                                { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
293 cache-misses                                    { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
294 branch-instructions|branches                    { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
295 branch-misses                                   { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
296 bus-cycles                                      { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
297 ref-cycles                                      { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
298 cpu-clock                                       { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
299 task-clock                                      { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
300 page-faults|faults                              { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
301 minor-faults                                    { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
302 major-faults                                    { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
303 context-switches|cs                             { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
304 cpu-migrations|migrations                       { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
305 alignment-faults                                { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
306 emulation-faults                                { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
307 dummy                                           { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
308 bpf-output                                      { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
309
310         /*
311          * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
312          * Because the prefix cycles is mixed up with cpu-cycles.
313          * loads and stores are mixed up with cache event
314          */
315 cycles-ct                                       { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
316 cycles-t                                        { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
317 mem-loads                                       { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
318 mem-stores                                      { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
319 topdown-[a-z-]+                                 { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
320
321 L1-dcache|l1-d|l1d|L1-data              |
322 L1-icache|l1-i|l1i|L1-instruction       |
323 LLC|L2                                  |
324 dTLB|d-tlb|Data-TLB                     |
325 iTLB|i-tlb|Instruction-TLB              |
326 branch|branches|bpu|btb|bpc             |
327 node                                    { return str(yyscanner, PE_NAME_CACHE_TYPE); }
328
329 load|loads|read                         |
330 store|stores|write                      |
331 prefetch|prefetches                     |
332 speculative-read|speculative-load       |
333 refs|Reference|ops|access               |
334 misses|miss                             { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
335
336 mem:                    { BEGIN(mem); return PE_PREFIX_MEM; }
337 r{num_raw_hex}          { return raw(yyscanner); }
338 {num_dec}               { return value(yyscanner, 10); }
339 {num_hex}               { return value(yyscanner, 16); }
340
341 {modifier_event}        { return str(yyscanner, PE_MODIFIER_EVENT); }
342 {bpf_object}            { if (!isbpf(yyscanner)) USER_REJECT; return str(yyscanner, PE_BPF_OBJECT); }
343 {bpf_source}            { if (!isbpf(yyscanner)) USER_REJECT; return str(yyscanner, PE_BPF_SOURCE); }
344 {name}                  { return pmu_str_check(yyscanner); }
345 "/"                     { BEGIN(config); return '/'; }
346 -                       { return '-'; }
347 ,                       { BEGIN(event); return ','; }
348 :                       { return ':'; }
349 "{"                     { BEGIN(event); return '{'; }
350 "}"                     { return '}'; }
351 =                       { return '='; }
352 \n                      { }
353 .                       { }
354
355 %%
356
357 int parse_events_wrap(void *scanner __maybe_unused)
358 {
359         return 1;
360 }