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