parse-events.l 9.8 KB

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