xref: /openbmc/linux/tools/perf/util/parse-events.l (revision fa0dadde)
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 config3			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG3); }
289 name			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
290 period			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
291 freq			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
292 branch_type		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
293 time			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
294 call-graph		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
295 stack-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
296 max-stack		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
297 nr			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
298 inherit			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
299 no-inherit		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
300 overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
301 no-overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
302 percore			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); }
303 aux-output		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
304 aux-sample-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
305 metric-id		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); }
306 r{num_raw_hex}		{ return raw(yyscanner); }
307 r0x{num_raw_hex}	{ return raw(yyscanner); }
308 ,			{ return ','; }
309 "/"			{ BEGIN(INITIAL); return '/'; }
310 {name_minus}		{ return str(yyscanner, PE_NAME); }
311 \[all\]			{ return PE_ARRAY_ALL; }
312 "["			{ BEGIN(array); return '['; }
313 @{drv_cfg_term}		{ return drv_str(yyscanner, PE_DRV_CFG_TERM); }
314 }
315 
316 <mem>{
317 {modifier_bp}		{ return str(yyscanner, PE_MODIFIER_BP); }
318 :			{ return ':'; }
319 "/"			{ return '/'; }
320 {num_dec}		{ return value(yyscanner, 10); }
321 {num_hex}		{ return value(yyscanner, 16); }
322 	/*
323 	 * We need to separate 'mem:' scanner part, in order to get specific
324 	 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
325 	 * and we'd need to parse it manually. During the escape from <mem>
326 	 * state we need to put the escaping char back, so we dont miss it.
327 	 */
328 .			{ unput(*yytext); BEGIN(INITIAL); }
329 	/*
330 	 * We destroy the scanner after reaching EOF,
331 	 * but anyway just to be sure get back to INIT state.
332 	 */
333 <<EOF>>			{ BEGIN(INITIAL); }
334 }
335 
336 cpu-cycles|cycles				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
337 stalled-cycles-frontend|idle-cycles-frontend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
338 stalled-cycles-backend|idle-cycles-backend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
339 instructions					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
340 cache-references				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
341 cache-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
342 branch-instructions|branches			{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
343 branch-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
344 bus-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
345 ref-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
346 cpu-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
347 task-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
348 page-faults|faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
349 minor-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
350 major-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
351 context-switches|cs				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
352 cpu-migrations|migrations			{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
353 alignment-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
354 emulation-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
355 dummy						{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
356 duration_time					{ return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
357 user_time						{ return tool(yyscanner, PERF_TOOL_USER_TIME); }
358 system_time						{ return tool(yyscanner, PERF_TOOL_SYSTEM_TIME); }
359 bpf-output					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
360 cgroup-switches					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CGROUP_SWITCHES); }
361 
362 	/*
363 	 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
364 	 * Because the prefix cycles is mixed up with cpu-cycles.
365 	 * loads and stores are mixed up with cache event
366 	 */
367 cycles-ct				|
368 cycles-t				|
369 mem-loads				|
370 mem-loads-aux				|
371 mem-stores				|
372 topdown-[a-z-]+				|
373 tx-capacity-[a-z-]+			|
374 el-capacity-[a-z-]+			{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
375 
376 L1-dcache|l1-d|l1d|L1-data		|
377 L1-icache|l1-i|l1i|L1-instruction	|
378 LLC|L2					|
379 dTLB|d-tlb|Data-TLB			|
380 iTLB|i-tlb|Instruction-TLB		|
381 branch|branches|bpu|btb|bpc		|
382 node					{ return str(yyscanner, PE_NAME_CACHE_TYPE); }
383 
384 load|loads|read				|
385 store|stores|write			|
386 prefetch|prefetches			|
387 speculative-read|speculative-load	|
388 refs|Reference|ops|access		|
389 misses|miss				{ return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
390 
391 mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
392 r{num_raw_hex}		{ return raw(yyscanner); }
393 {num_dec}		{ return value(yyscanner, 10); }
394 {num_hex}		{ return value(yyscanner, 16); }
395 
396 {modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
397 {bpf_object}		{ if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
398 {bpf_source}		{ if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
399 {name}			{ return pmu_str_check(yyscanner, _parse_state); }
400 {name_tag}		{ return str(yyscanner, PE_NAME); }
401 "/"			{ BEGIN(config); return '/'; }
402 -			{ return '-'; }
403 ,			{ BEGIN(event); return ','; }
404 :			{ return ':'; }
405 "{"			{ BEGIN(event); return '{'; }
406 "}"			{ return '}'; }
407 =			{ return '='; }
408 \n			{ }
409 .			{ }
410 
411 %%
412 
413 int parse_events_wrap(void *scanner __maybe_unused)
414 {
415 	return 1;
416 }
417