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