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