parse-events.l 11 KB

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