parse-events.l 11.6 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
%option reject
9 10 11

%{
#include <errno.h>
12 13 14
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
15
#include "../perf.h"
16
#include "parse-events.h"
17
#include "parse-events-bison.h"
18
#include "evsel.h"
19

20 21 22 23
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)
24
{
25
	u64 num;
26 27

	errno = 0;
28
	num = strtoull(str, NULL, base);
29 30 31
	if (errno)
		return PE_ERROR;

32
	yylval->num = num;
33 34 35
	return token;
}

36
static int value(yyscan_t scanner, int base)
37
{
38 39 40 41
	YYSTYPE *yylval = parse_events_get_lval(scanner);
	char *text = parse_events_get_text(scanner);

	return __value(yylval, text, base, PE_VALUE);
42 43
}

44
static int raw(yyscan_t scanner)
45
{
46 47 48 49
	YYSTYPE *yylval = parse_events_get_lval(scanner);
	char *text = parse_events_get_text(scanner);

	return __value(yylval, text + 1, 16, PE_RAW);
50 51
}

52
static int str(yyscan_t scanner, int token)
53
{
54 55 56
	YYSTYPE *yylval = parse_events_get_lval(scanner);
	char *text = parse_events_get_text(scanner);

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	if (text[0] != '\'') {
		yylval->str = strdup(text);
	} else {
		/*
		 * If a text tag specified on the command line
		 * contains opening single quite ' then it is
		 * expected that the tag ends with single quote
		 * as well, like this:
		 *     name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\'
		 * quotes need to be escaped to bypass shell
		 * processing.
		 */
		yylval->str = strndup(&text[1], strlen(text) - 2);
	}

72 73 74
	return token;
}

75
static bool isbpf_suffix(char *text)
76 77 78 79 80 81 82 83 84 85 86 87 88
{
	int len = strlen(text);

	if (len < 2)
		return false;
	if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
	    text[len - 2] == '.')
		return true;
	if (len > 4 && !strcmp(text + len - 4, ".obj"))
		return true;
	return false;
}

89 90 91 92 93 94 95 96 97 98 99
static bool isbpf(yyscan_t scanner)
{
	char *text = parse_events_get_text(scanner);
	struct stat st;

	if (!isbpf_suffix(text))
		return false;

	return stat(text, &st) == 0;
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/*
 * This function is called when the parser gets two kind of input:
 *
 * 	@cfg1 or @cfg2=config
 *
 * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
 * bison.  In the latter case it is necessary to keep the string intact so that
 * the PMU kernel driver can determine what configurable is associated to
 * 'config'.
 */
static int drv_str(yyscan_t scanner, int token)
{
	YYSTYPE *yylval = parse_events_get_lval(scanner);
	char *text = parse_events_get_text(scanner);

	/* Strip off the '@' */
	yylval->str = strdup(text + 1);
	return token;
}

120 121 122 123 124 125 126 127 128 129 130 131
#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)

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
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;
	}
}

150
static int sym(yyscan_t scanner, int type, int config)
151
{
152 153 154
	YYSTYPE *yylval = parse_events_get_lval(scanner);

	yylval->num = (type << 16) + config;
155
	return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
156 157
}

158 159 160 161 162 163 164 165
static int tool(yyscan_t scanner, enum perf_tool_event event)
{
	YYSTYPE *yylval = parse_events_get_lval(scanner);

	yylval->num = event;
	return PE_VALUE_SYM_TOOL;
}

166
static int term(yyscan_t scanner, int type)
167
{
168 169 170
	YYSTYPE *yylval = parse_events_get_lval(scanner);

	yylval->num = type;
171 172 173
	return PE_TERM;
}

174 175 176 177 178 179 180
#define YY_USER_ACTION					\
do {							\
	yylloc->last_column  = yylloc->first_column;	\
	yylloc->first_column = yycolumn;		\
	yycolumn += yyleng;				\
} while (0);

181 182 183 184
#define USER_REJECT		\
	yycolumn -= yyleng;	\
	REJECT

185 186
%}

187
%x mem
188 189
%s config
%x event
190
%x array
191 192 193 194

group		[^,{}/]*[{][^}]*[}][^,{}/]*
event_pmu	[^,{}/]+[/][^/]*[/][^,{}/]*
event		[^,{}/]+
195 196
bpf_object	[^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
bpf_source	[^,{}]+\.c[a-zA-Z0-9._]*
197

198 199 200
num_dec		[0-9]+
num_hex		0x[a-fA-F0-9]+
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 213 214

%{
	{
		int start_token;

215 216 217 218 219 220 221
		start_token = parse_events_get_extra(yyscanner);

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

222 223
		if (start_token) {
			parse_events_set_extra(NULL, yyscanner);
224 225 226 227 228 229
			/*
			 * The flex parser does not init locations variable
			 * via the scan_string interface, so we need do the
			 * init in here.
			 */
			yycolumn = 0;
230 231 232 233 234
			return start_token;
		}
         }
%}

235 236 237
<event>{

{group}		{
238 239
			BEGIN(INITIAL);
			REWIND(0);
240 241 242
		}

{event_pmu}	|
243
{bpf_object}	|
244
{bpf_source}	|
245
{event}		{
246 247
			BEGIN(INITIAL);
			REWIND(1);
248 249 250 251
			return PE_EVENT_NAME;
		}

<<EOF>>		{
252 253
			BEGIN(INITIAL);
			REWIND(0);
254 255 256 257
		}

}

258 259 260 261 262 263 264 265
<array>{
"]"			{ BEGIN(config); return ']'; }
{num_dec}		{ return value(yyscanner, 10); }
{num_hex}		{ return value(yyscanner, 16); }
,			{ return ','; }
"\.\.\."		{ return PE_ARRAY_RANGE; }
}

266
<config>{
267
	/*
268
	 * Please update config_term_names when new static term is added.
269
	 */
270 271 272 273 274
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); }
275
freq			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
276
branch_type		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
K
Kan Liang 已提交
277
time			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
278 279
call-graph		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
stack-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
280
max-stack		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
281
nr			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
282 283
inherit			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
no-inherit		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
W
Wang Nan 已提交
284 285
overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
no-overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
286
percore			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); }
287
aux-output		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
288
aux-sample-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
289 290 291
,			{ return ','; }
"/"			{ BEGIN(INITIAL); return '/'; }
{name_minus}		{ return str(yyscanner, PE_NAME); }
292 293
\[all\]			{ return PE_ARRAY_ALL; }
"["			{ BEGIN(array); return '['; }
294
@{drv_cfg_term}		{ return drv_str(yyscanner, PE_DRV_CFG_TERM); }
295 296 297 298 299
}

<mem>{
{modifier_bp}		{ return str(yyscanner, PE_MODIFIER_BP); }
:			{ return ':'; }
300
"/"			{ return '/'; }
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
{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); }
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
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); }
336
dummy						{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
337
duration_time					{ return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
338
bpf-output					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
339

340 341 342 343 344 345 346 347 348
	/*
	 * 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); }
349
topdown-[a-z-]+					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
350

351 352 353 354 355 356
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		|
357
node					{ return str(yyscanner, PE_NAME_CACHE_TYPE); }
358 359 360 361 362 363

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

366
mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
367 368 369
r{num_raw_hex}		{ return raw(yyscanner); }
{num_dec}		{ return value(yyscanner, 10); }
{num_hex}		{ return value(yyscanner, 16); }
370

371
{modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
372 373
{bpf_object}		{ if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
{bpf_source}		{ if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
374
{name}			{ return pmu_str_check(yyscanner); }
375
{name_tag}		{ return str(yyscanner, PE_NAME); }
376
"/"			{ BEGIN(config); return '/'; }
377
-			{ return '-'; }
378
,			{ BEGIN(event); return ','; }
379
:			{ return ':'; }
380
"{"			{ BEGIN(event); return '{'; }
381
"}"			{ return '}'; }
382
=			{ return '='; }
383
\n			{ }
384
.			{ }
385

386 387
%%

388
int parse_events_wrap(void *scanner __maybe_unused)
389 390 391
{
	return 1;
}