parse-events.l 11 KB

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