xref: /openbmc/linux/tools/perf/util/parse-events.l (revision 3ddc8b84)
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 lc_str(yyscan_t scanner, const struct parse_events_state *state)
67 {
68 	return str(scanner, state->match_legacy_cache_terms ? PE_LEGACY_CACHE : PE_NAME);
69 }
70 
71 /*
72  * This function is called when the parser gets two kind of input:
73  *
74  * 	@cfg1 or @cfg2=config
75  *
76  * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
77  * bison.  In the latter case it is necessary to keep the string intact so that
78  * the PMU kernel driver can determine what configurable is associated to
79  * 'config'.
80  */
81 static int drv_str(yyscan_t scanner, int token)
82 {
83 	YYSTYPE *yylval = parse_events_get_lval(scanner);
84 	char *text = parse_events_get_text(scanner);
85 
86 	/* Strip off the '@' */
87 	yylval->str = strdup(text + 1);
88 	return token;
89 }
90 
91 #define REWIND(__alloc)				\
92 do {								\
93 	YYSTYPE *__yylval = parse_events_get_lval(yyscanner);	\
94 	char *text = parse_events_get_text(yyscanner);		\
95 								\
96 	if (__alloc)						\
97 		__yylval->str = strdup(text);			\
98 								\
99 	yycolumn -= strlen(text);				\
100 	yyless(0);						\
101 } while (0)
102 
103 static int sym(yyscan_t scanner, int type, int config)
104 {
105 	YYSTYPE *yylval = parse_events_get_lval(scanner);
106 
107 	yylval->num = (type << 16) + config;
108 	return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
109 }
110 
111 static int tool(yyscan_t scanner, enum perf_tool_event event)
112 {
113 	YYSTYPE *yylval = parse_events_get_lval(scanner);
114 
115 	yylval->num = event;
116 	return PE_VALUE_SYM_TOOL;
117 }
118 
119 static int term(yyscan_t scanner, enum parse_events__term_type type)
120 {
121 	YYSTYPE *yylval = parse_events_get_lval(scanner);
122 
123 	yylval->num = type;
124 	return PE_TERM;
125 }
126 
127 static int hw_term(yyscan_t scanner, int config)
128 {
129 	YYSTYPE *yylval = parse_events_get_lval(scanner);
130 	char *text = parse_events_get_text(scanner);
131 
132 	yylval->hardware_term.str = strdup(text);
133 	yylval->hardware_term.num = PERF_TYPE_HARDWARE + config;
134 	return PE_TERM_HW;
135 }
136 
137 #define YY_USER_ACTION					\
138 do {							\
139 	yylloc->last_column  = yylloc->first_column;	\
140 	yylloc->first_column = yycolumn;		\
141 	yycolumn += yyleng;				\
142 } while (0);
143 
144 #define USER_REJECT		\
145 	yycolumn -= yyleng;	\
146 	REJECT
147 
148 %}
149 
150 %x mem
151 %s config
152 %x event
153 
154 group		[^,{}/]*[{][^}]*[}][^,{}/]*
155 event_pmu	[^,{}/]+[/][^/]*[/][^,{}/]*
156 event		[^,{}/]+
157 
158 num_dec		[0-9]+
159 num_hex		0x[a-fA-F0-9]+
160 num_raw_hex	[a-fA-F0-9]+
161 name		[a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]!\-]*
162 name_tag	[\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
163 name_minus	[a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
164 drv_cfg_term	[a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
165 /*
166  * If you add a modifier you need to update check_modifier().
167  * Also, the letters in modifier_event must not be in modifier_bp.
168  */
169 modifier_event	[ukhpPGHSDIWeb]+
170 modifier_bp	[rwx]{1,3}
171 lc_type 	(L1-dcache|l1-d|l1d|L1-data|L1-icache|l1-i|l1i|L1-instruction|LLC|L2|dTLB|d-tlb|Data-TLB|iTLB|i-tlb|Instruction-TLB|branch|branches|bpu|btb|bpc|node)
172 lc_op_result	(load|loads|read|store|stores|write|prefetch|prefetches|speculative-read|speculative-load|refs|Reference|ops|access|misses|miss)
173 digit		[0-9]
174 non_digit	[^0-9]
175 
176 %%
177 
178 %{
179 	struct parse_events_state *_parse_state = parse_events_get_extra(yyscanner);
180 	{
181 		int start_token = _parse_state->stoken;
182 
183 		if (start_token == PE_START_TERMS)
184 			BEGIN(config);
185 		else if (start_token == PE_START_EVENTS)
186 			BEGIN(event);
187 
188 		if (start_token) {
189 			_parse_state->stoken = 0;
190 			/*
191 			 * The flex parser does not init locations variable
192 			 * via the scan_string interface, so we need do the
193 			 * init in here.
194 			 */
195 			yycolumn = 0;
196 			return start_token;
197 		}
198          }
199 %}
200 
201 <event>{
202 
203 {group}		{
204 			BEGIN(INITIAL);
205 			REWIND(0);
206 		}
207 
208 {event_pmu}	|
209 {event}		{
210 			BEGIN(INITIAL);
211 			REWIND(1);
212 			return PE_EVENT_NAME;
213 		}
214 
215 <<EOF>>		{
216 			BEGIN(INITIAL);
217 			REWIND(0);
218 		}
219 ,		{
220 			return ',';
221 		}
222 }
223 
224 <config>{
225 	/*
226 	 * Please update config_term_names when new static term is added.
227 	 */
228 config			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
229 config1			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
230 config2			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
231 config3			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG3); }
232 name			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
233 period			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
234 freq			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
235 branch_type		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
236 time			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
237 call-graph		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
238 stack-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
239 max-stack		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
240 nr			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
241 inherit			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
242 no-inherit		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
243 overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
244 no-overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
245 percore			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); }
246 aux-output		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
247 aux-sample-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
248 metric-id		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); }
249 cpu-cycles|cycles				{ return hw_term(yyscanner, PERF_COUNT_HW_CPU_CYCLES); }
250 stalled-cycles-frontend|idle-cycles-frontend	{ return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
251 stalled-cycles-backend|idle-cycles-backend	{ return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
252 instructions					{ return hw_term(yyscanner, PERF_COUNT_HW_INSTRUCTIONS); }
253 cache-references				{ return hw_term(yyscanner, PERF_COUNT_HW_CACHE_REFERENCES); }
254 cache-misses					{ return hw_term(yyscanner, PERF_COUNT_HW_CACHE_MISSES); }
255 branch-instructions|branches			{ return hw_term(yyscanner, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
256 branch-misses					{ return hw_term(yyscanner, PERF_COUNT_HW_BRANCH_MISSES); }
257 bus-cycles					{ return hw_term(yyscanner, PERF_COUNT_HW_BUS_CYCLES); }
258 ref-cycles					{ return hw_term(yyscanner, PERF_COUNT_HW_REF_CPU_CYCLES); }
259 r{num_raw_hex}		{ return str(yyscanner, PE_RAW); }
260 r0x{num_raw_hex}	{ return str(yyscanner, PE_RAW); }
261 ,			{ return ','; }
262 "/"			{ BEGIN(INITIAL); return '/'; }
263 {lc_type}			{ return lc_str(yyscanner, _parse_state); }
264 {lc_type}-{lc_op_result}	{ return lc_str(yyscanner, _parse_state); }
265 {lc_type}-{lc_op_result}-{lc_op_result}	{ return lc_str(yyscanner, _parse_state); }
266 {name_minus}		{ return str(yyscanner, PE_NAME); }
267 @{drv_cfg_term}		{ return drv_str(yyscanner, PE_DRV_CFG_TERM); }
268 }
269 
270 <mem>{
271 {modifier_bp}		{ return str(yyscanner, PE_MODIFIER_BP); }
272 	/*
273 	 * The colon before memory access modifiers can get mixed up with the
274 	 * colon before event modifiers. Fortunately none of the option letters
275 	 * are the same, so trailing context can be used disambiguate the two
276 	 * cases.
277 	 */
278 ":"/{modifier_bp}	{ return PE_BP_COLON; }
279 	/*
280 	 * The slash before memory length can get mixed up with the slash before
281 	 * config terms. Fortunately config terms do not start with a numeric
282 	 * digit, so trailing context can be used disambiguate the two cases.
283 	 */
284 "/"/{digit}		{ return PE_BP_SLASH; }
285 "/"/{non_digit}		{ BEGIN(config); return '/'; }
286 {num_dec}		{ return value(yyscanner, 10); }
287 {num_hex}		{ return value(yyscanner, 16); }
288 	/*
289 	 * We need to separate 'mem:' scanner part, in order to get specific
290 	 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
291 	 * and we'd need to parse it manually. During the escape from <mem>
292 	 * state we need to put the escaping char back, so we dont miss it.
293 	 */
294 .			{ unput(*yytext); BEGIN(INITIAL); }
295 	/*
296 	 * We destroy the scanner after reaching EOF,
297 	 * but anyway just to be sure get back to INIT state.
298 	 */
299 <<EOF>>			{ BEGIN(INITIAL); }
300 }
301 
302 cpu-cycles|cycles				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
303 stalled-cycles-frontend|idle-cycles-frontend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
304 stalled-cycles-backend|idle-cycles-backend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
305 instructions					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
306 cache-references				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
307 cache-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
308 branch-instructions|branches			{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
309 branch-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
310 bus-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
311 ref-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
312 cpu-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
313 task-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
314 page-faults|faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
315 minor-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
316 major-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
317 context-switches|cs				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
318 cpu-migrations|migrations			{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
319 alignment-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
320 emulation-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
321 dummy						{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
322 duration_time					{ return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
323 user_time						{ return tool(yyscanner, PERF_TOOL_USER_TIME); }
324 system_time						{ return tool(yyscanner, PERF_TOOL_SYSTEM_TIME); }
325 bpf-output					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
326 cgroup-switches					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CGROUP_SWITCHES); }
327 
328 {lc_type}			{ return str(yyscanner, PE_LEGACY_CACHE); }
329 {lc_type}-{lc_op_result}	{ return str(yyscanner, PE_LEGACY_CACHE); }
330 {lc_type}-{lc_op_result}-{lc_op_result}	{ return str(yyscanner, PE_LEGACY_CACHE); }
331 mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
332 r{num_raw_hex}		{ return str(yyscanner, PE_RAW); }
333 {num_dec}		{ return value(yyscanner, 10); }
334 {num_hex}		{ return value(yyscanner, 16); }
335 
336 {modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
337 {name}			{ return str(yyscanner, PE_NAME); }
338 {name_tag}		{ return str(yyscanner, PE_NAME); }
339 "/"			{ BEGIN(config); return '/'; }
340 ,			{ BEGIN(event); return ','; }
341 :			{ return ':'; }
342 "{"			{ BEGIN(event); return '{'; }
343 "}"			{ return '}'; }
344 =			{ return '='; }
345 \n			{ }
346 .			{ }
347 
348 %%
349 
350 int parse_events_wrap(void *scanner __maybe_unused)
351 {
352 	return 1;
353 }
354