parse-events.l 8.2 KB
Newer Older
1

2 3
%option reentrant
%option bison-bridge
4
%option prefix="parse_events_"
5
%option stack
6 7
%option bison-locations
%option yylineno
8 9 10 11 12

%{
#include <errno.h>
#include "../perf.h"
#include "parse-events-bison.h"
13
#include "parse-events.h"
14

15 16 17 18
char *parse_events_get_text(yyscan_t yyscanner);
YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);

static int __value(YYSTYPE *yylval, char *str, int base, int token)
19
{
20
	u64 num;
21 22

	errno = 0;
23
	num = strtoull(str, NULL, base);
24 25 26
	if (errno)
		return PE_ERROR;

27
	yylval->num = num;
28 29 30
	return token;
}

31
static int value(yyscan_t scanner, int base)
32
{
33 34 35 36
	YYSTYPE *yylval = parse_events_get_lval(scanner);
	char *text = parse_events_get_text(scanner);

	return __value(yylval, text, base, PE_VALUE);
37 38
}

39
static int raw(yyscan_t scanner)
40
{
41 42 43 44
	YYSTYPE *yylval = parse_events_get_lval(scanner);
	char *text = parse_events_get_text(scanner);

	return __value(yylval, text + 1, 16, PE_RAW);
45 46
}

47
static int str(yyscan_t scanner, int token)
48
{
49 50 51 52
	YYSTYPE *yylval = parse_events_get_lval(scanner);
	char *text = parse_events_get_text(scanner);

	yylval->str = strdup(text);
53 54 55
	return token;
}

56 57 58 59 60 61 62 63 64 65 66 67
#define REWIND(__alloc)				\
do {								\
	YYSTYPE *__yylval = parse_events_get_lval(yyscanner);	\
	char *text = parse_events_get_text(yyscanner);		\
								\
	if (__alloc)						\
		__yylval->str = strdup(text);			\
								\
	yycolumn -= strlen(text);				\
	yyless(0);						\
} while (0)

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
static int pmu_str_check(yyscan_t scanner)
{
	YYSTYPE *yylval = parse_events_get_lval(scanner);
	char *text = parse_events_get_text(scanner);

	yylval->str = strdup(text);
	switch (perf_pmu__parse_check(text)) {
		case PMU_EVENT_SYMBOL_PREFIX:
			return PE_PMU_EVENT_PRE;
		case PMU_EVENT_SYMBOL_SUFFIX:
			return PE_PMU_EVENT_SUF;
		case PMU_EVENT_SYMBOL:
			return PE_KERNEL_PMU_EVENT;
		default:
			return PE_NAME;
	}
}

86
static int sym(yyscan_t scanner, int type, int config)
87
{
88 89 90
	YYSTYPE *yylval = parse_events_get_lval(scanner);

	yylval->num = (type << 16) + config;
91
	return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
92 93
}

94
static int term(yyscan_t scanner, int type)
95
{
96 97 98
	YYSTYPE *yylval = parse_events_get_lval(scanner);

	yylval->num = type;
99 100 101
	return PE_TERM;
}

102 103 104 105 106 107 108
#define YY_USER_ACTION					\
do {							\
	yylloc->last_column  = yylloc->first_column;	\
	yylloc->first_column = yycolumn;		\
	yycolumn += yyleng;				\
} while (0);

109 110
%}

111
%x mem
112 113 114 115 116 117
%s config
%x event

group		[^,{}/]*[{][^}]*[}][^,{}/]*
event_pmu	[^,{}/]+[/][^/]*[/][^,{}/]*
event		[^,{}/]+
118

119 120 121
num_dec		[0-9]+
num_hex		0x[a-fA-F0-9]+
num_raw_hex	[a-fA-F0-9]+
A
Andi Kleen 已提交
122 123
name		[a-zA-Z_*?][a-zA-Z0-9_*?.]*
name_minus	[a-zA-Z_*?][a-zA-Z0-9\-_*?.]*
124
/* If you add a modifier you need to update check_modifier() */
125
modifier_event	[ukhpPGHSDI]+
126
modifier_bp	[rwx]{1,3}
127 128

%%
129 130 131 132 133

%{
	{
		int start_token;

134 135 136 137 138 139 140
		start_token = parse_events_get_extra(yyscanner);

		if (start_token == PE_START_TERMS)
			BEGIN(config);
		else if (start_token == PE_START_EVENTS)
			BEGIN(event);

141 142
		if (start_token) {
			parse_events_set_extra(NULL, yyscanner);
143 144 145 146 147 148
			/*
			 * The flex parser does not init locations variable
			 * via the scan_string interface, so we need do the
			 * init in here.
			 */
			yycolumn = 0;
149 150 151 152 153
			return start_token;
		}
         }
%}

154 155 156
<event>{

{group}		{
157 158
			BEGIN(INITIAL);
			REWIND(0);
159 160 161 162
		}

{event_pmu}	|
{event}		{
163 164
			BEGIN(INITIAL);
			REWIND(1);
165 166 167 168
			return PE_EVENT_NAME;
		}

<<EOF>>		{
169 170
			BEGIN(INITIAL);
			REWIND(0);
171 172 173 174
		}

}

175
<config>{
176
	/*
177
	 * Please update parse_events_formats_error_string any time
178 179
	 * new static term is added.
	 */
180 181 182 183 184
config			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
config1			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
config2			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
name			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
period			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
185
freq			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
186
branch_type		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
K
Kan Liang 已提交
187
time			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
188 189
call-graph		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
stack-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
190 191 192 193 194 195 196 197
,			{ return ','; }
"/"			{ BEGIN(INITIAL); return '/'; }
{name_minus}		{ return str(yyscanner, PE_NAME); }
}

<mem>{
{modifier_bp}		{ return str(yyscanner, PE_MODIFIER_BP); }
:			{ return ':'; }
198
"/"			{ return '/'; }
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
{num_dec}		{ return value(yyscanner, 10); }
{num_hex}		{ return value(yyscanner, 16); }
	/*
	 * We need to separate 'mem:' scanner part, in order to get specific
	 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
	 * and we'd need to parse it manually. During the escape from <mem>
	 * state we need to put the escaping char back, so we dont miss it.
	 */
.			{ unput(*yytext); BEGIN(INITIAL); }
	/*
	 * We destroy the scanner after reaching EOF,
	 * but anyway just to be sure get back to INIT state.
	 */
<<EOF>>			{ BEGIN(INITIAL); }
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
cpu-cycles|cycles				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
stalled-cycles-frontend|idle-cycles-frontend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
stalled-cycles-backend|idle-cycles-backend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
instructions					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
cache-references				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
cache-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
branch-instructions|branches			{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
branch-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
bus-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
ref-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
cpu-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
task-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
page-faults|faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
minor-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
major-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
context-switches|cs				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
cpu-migrations|migrations			{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
alignment-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
emulation-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
234
dummy						{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
235

236 237 238 239 240 241 242 243 244 245
	/*
	 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
	 * Because the prefix cycles is mixed up with cpu-cycles.
	 * loads and stores are mixed up with cache event
	 */
cycles-ct					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
cycles-t					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
mem-loads					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
mem-stores					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }

246 247 248 249 250 251
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		|
252
node					{ return str(yyscanner, PE_NAME_CACHE_TYPE); }
253 254 255 256 257 258

load|loads|read				|
store|stores|write			|
prefetch|prefetches			|
speculative-read|speculative-load	|
refs|Reference|ops|access		|
259
misses|miss				{ return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
260

261
mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
262 263 264
r{num_raw_hex}		{ return raw(yyscanner); }
{num_dec}		{ return value(yyscanner, 10); }
{num_hex}		{ return value(yyscanner, 16); }
265

266
{modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
267
{name}			{ return pmu_str_check(yyscanner); }
268
"/"			{ BEGIN(config); return '/'; }
269
-			{ return '-'; }
270
,			{ BEGIN(event); return ','; }
271
:			{ return ':'; }
272
"{"			{ BEGIN(event); return '{'; }
273
"}"			{ return '}'; }
274
=			{ return '='; }
275
\n			{ }
276
.			{ }
277

278 279
%%

280
int parse_events_wrap(void *scanner __maybe_unused)
281 282 283
{
	return 1;
}