parse-events.l 8.5 KB

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