xref: /linux/tools/perf/util/parse-events.l (revision dd0a11815a339d6deeea8357574f8126a8404c92)
1 
2 %option reentrant
3 %option bison-bridge
4 %option prefix="parse_events_"
5 %option stack
6 
7 %{
8 #include <errno.h>
9 #include "../perf.h"
10 #include "parse-events-bison.h"
11 #include "parse-events.h"
12 
13 char *parse_events_get_text(yyscan_t yyscanner);
14 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
15 
16 static int __value(YYSTYPE *yylval, char *str, int base, int token)
17 {
18 	u64 num;
19 
20 	errno = 0;
21 	num = strtoull(str, NULL, base);
22 	if (errno)
23 		return PE_ERROR;
24 
25 	yylval->num = num;
26 	return token;
27 }
28 
29 static int value(yyscan_t scanner, int base)
30 {
31 	YYSTYPE *yylval = parse_events_get_lval(scanner);
32 	char *text = parse_events_get_text(scanner);
33 
34 	return __value(yylval, text, base, PE_VALUE);
35 }
36 
37 static int raw(yyscan_t scanner)
38 {
39 	YYSTYPE *yylval = parse_events_get_lval(scanner);
40 	char *text = parse_events_get_text(scanner);
41 
42 	return __value(yylval, text + 1, 16, PE_RAW);
43 }
44 
45 static int str(yyscan_t scanner, int token)
46 {
47 	YYSTYPE *yylval = parse_events_get_lval(scanner);
48 	char *text = parse_events_get_text(scanner);
49 
50 	yylval->str = strdup(text);
51 	return token;
52 }
53 
54 static int sym(yyscan_t scanner, int type, int config)
55 {
56 	YYSTYPE *yylval = parse_events_get_lval(scanner);
57 
58 	yylval->num = (type << 16) + config;
59 	return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
60 }
61 
62 static int term(yyscan_t scanner, int type)
63 {
64 	YYSTYPE *yylval = parse_events_get_lval(scanner);
65 
66 	yylval->num = type;
67 	return PE_TERM;
68 }
69 
70 %}
71 
72 %x mem
73 %s config
74 %x event
75 
76 group		[^,{}/]*[{][^}]*[}][^,{}/]*
77 event_pmu	[^,{}/]+[/][^/]*[/][^,{}/]*
78 event		[^,{}/]+
79 
80 num_dec		[0-9]+
81 num_hex		0x[a-fA-F0-9]+
82 num_raw_hex	[a-fA-F0-9]+
83 name		[a-zA-Z_*?][a-zA-Z0-9_*?]*
84 name_minus	[a-zA-Z_*?][a-zA-Z0-9\-_*?]*
85 /* If you add a modifier you need to update check_modifier() */
86 modifier_event	[ukhpGHSD]+
87 modifier_bp	[rwx]{1,3}
88 
89 %%
90 
91 %{
92 	{
93 		int start_token;
94 
95 		start_token = parse_events_get_extra(yyscanner);
96 
97 		if (start_token == PE_START_TERMS)
98 			BEGIN(config);
99 		else if (start_token == PE_START_EVENTS)
100 			BEGIN(event);
101 
102 		if (start_token) {
103 			parse_events_set_extra(NULL, yyscanner);
104 			return start_token;
105 		}
106          }
107 %}
108 
109 <event>{
110 
111 {group}		{
112 			BEGIN(INITIAL); yyless(0);
113 		}
114 
115 {event_pmu}	|
116 {event}		{
117 			str(yyscanner, PE_EVENT_NAME);
118 			BEGIN(INITIAL); yyless(0);
119 			return PE_EVENT_NAME;
120 		}
121 
122 .		|
123 <<EOF>>		{
124 			BEGIN(INITIAL); yyless(0);
125 		}
126 
127 }
128 
129 cpu-cycles|cycles				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
130 stalled-cycles-frontend|idle-cycles-frontend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
131 stalled-cycles-backend|idle-cycles-backend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
132 instructions					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
133 cache-references				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
134 cache-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
135 branch-instructions|branches			{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
136 branch-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
137 bus-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
138 ref-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
139 cpu-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
140 task-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
141 page-faults|faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
142 minor-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
143 major-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
144 context-switches|cs				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
145 cpu-migrations|migrations			{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
146 alignment-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
147 emulation-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
148 dummy						{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
149 
150 L1-dcache|l1-d|l1d|L1-data		|
151 L1-icache|l1-i|l1i|L1-instruction	|
152 LLC|L2					|
153 dTLB|d-tlb|Data-TLB			|
154 iTLB|i-tlb|Instruction-TLB		|
155 branch|branches|bpu|btb|bpc		|
156 node					{ return str(yyscanner, PE_NAME_CACHE_TYPE); }
157 
158 load|loads|read				|
159 store|stores|write			|
160 prefetch|prefetches			|
161 speculative-read|speculative-load	|
162 refs|Reference|ops|access		|
163 misses|miss				{ return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
164 
165 <config>{
166 config			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
167 config1			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
168 config2			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
169 name			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
170 period			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
171 branch_type		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
172 ,			{ return ','; }
173 "/"			{ BEGIN(INITIAL); return '/'; }
174 {name_minus}		{ return str(yyscanner, PE_NAME); }
175 }
176 
177 mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
178 r{num_raw_hex}		{ return raw(yyscanner); }
179 {num_dec}		{ return value(yyscanner, 10); }
180 {num_hex}		{ return value(yyscanner, 16); }
181 
182 {modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
183 {name}			{ return str(yyscanner, PE_NAME); }
184 "/"			{ BEGIN(config); return '/'; }
185 -			{ return '-'; }
186 ,			{ BEGIN(event); return ','; }
187 :			{ return ':'; }
188 "{"			{ BEGIN(event); return '{'; }
189 "}"			{ return '}'; }
190 =			{ return '='; }
191 \n			{ }
192 
193 <mem>{
194 {modifier_bp}		{ return str(yyscanner, PE_MODIFIER_BP); }
195 :			{ return ':'; }
196 {num_dec}		{ return value(yyscanner, 10); }
197 {num_hex}		{ return value(yyscanner, 16); }
198 	/*
199 	 * We need to separate 'mem:' scanner part, in order to get specific
200 	 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
201 	 * and we'd need to parse it manually. During the escape from <mem>
202 	 * state we need to put the escaping char back, so we dont miss it.
203 	 */
204 .			{ unput(*yytext); BEGIN(INITIAL); }
205 	/*
206 	 * We destroy the scanner after reaching EOF,
207 	 * but anyway just to be sure get back to INIT state.
208 	 */
209 <<EOF>>			{ BEGIN(INITIAL); }
210 }
211 
212 %%
213 
214 int parse_events_wrap(void *scanner __maybe_unused)
215 {
216 	return 1;
217 }
218