parse-events.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. #include <linux/hw_breakpoint.h>
  2. #include "util.h"
  3. #include "../perf.h"
  4. #include "evlist.h"
  5. #include "evsel.h"
  6. #include "parse-options.h"
  7. #include "parse-events.h"
  8. #include "exec_cmd.h"
  9. #include "linux/string.h"
  10. #include "symbol.h"
  11. #include "cache.h"
  12. #include "header.h"
  13. #include <api/fs/debugfs.h>
  14. #include "parse-events-bison.h"
  15. #define YY_EXTRA_TYPE int
  16. #include "parse-events-flex.h"
  17. #include "pmu.h"
  18. #include "thread_map.h"
  19. #define MAX_NAME_LEN 100
  20. struct event_symbol {
  21. const char *symbol;
  22. const char *alias;
  23. };
  24. #ifdef PARSER_DEBUG
  25. extern int parse_events_debug;
  26. #endif
  27. int parse_events_parse(void *data, void *scanner);
  28. static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
  29. [PERF_COUNT_HW_CPU_CYCLES] = {
  30. .symbol = "cpu-cycles",
  31. .alias = "cycles",
  32. },
  33. [PERF_COUNT_HW_INSTRUCTIONS] = {
  34. .symbol = "instructions",
  35. .alias = "",
  36. },
  37. [PERF_COUNT_HW_CACHE_REFERENCES] = {
  38. .symbol = "cache-references",
  39. .alias = "",
  40. },
  41. [PERF_COUNT_HW_CACHE_MISSES] = {
  42. .symbol = "cache-misses",
  43. .alias = "",
  44. },
  45. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
  46. .symbol = "branch-instructions",
  47. .alias = "branches",
  48. },
  49. [PERF_COUNT_HW_BRANCH_MISSES] = {
  50. .symbol = "branch-misses",
  51. .alias = "",
  52. },
  53. [PERF_COUNT_HW_BUS_CYCLES] = {
  54. .symbol = "bus-cycles",
  55. .alias = "",
  56. },
  57. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
  58. .symbol = "stalled-cycles-frontend",
  59. .alias = "idle-cycles-frontend",
  60. },
  61. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
  62. .symbol = "stalled-cycles-backend",
  63. .alias = "idle-cycles-backend",
  64. },
  65. [PERF_COUNT_HW_REF_CPU_CYCLES] = {
  66. .symbol = "ref-cycles",
  67. .alias = "",
  68. },
  69. };
  70. static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
  71. [PERF_COUNT_SW_CPU_CLOCK] = {
  72. .symbol = "cpu-clock",
  73. .alias = "",
  74. },
  75. [PERF_COUNT_SW_TASK_CLOCK] = {
  76. .symbol = "task-clock",
  77. .alias = "",
  78. },
  79. [PERF_COUNT_SW_PAGE_FAULTS] = {
  80. .symbol = "page-faults",
  81. .alias = "faults",
  82. },
  83. [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
  84. .symbol = "context-switches",
  85. .alias = "cs",
  86. },
  87. [PERF_COUNT_SW_CPU_MIGRATIONS] = {
  88. .symbol = "cpu-migrations",
  89. .alias = "migrations",
  90. },
  91. [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
  92. .symbol = "minor-faults",
  93. .alias = "",
  94. },
  95. [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
  96. .symbol = "major-faults",
  97. .alias = "",
  98. },
  99. [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
  100. .symbol = "alignment-faults",
  101. .alias = "",
  102. },
  103. [PERF_COUNT_SW_EMULATION_FAULTS] = {
  104. .symbol = "emulation-faults",
  105. .alias = "",
  106. },
  107. [PERF_COUNT_SW_DUMMY] = {
  108. .symbol = "dummy",
  109. .alias = "",
  110. },
  111. };
  112. #define __PERF_EVENT_FIELD(config, name) \
  113. ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
  114. #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
  115. #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
  116. #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
  117. #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
  118. #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
  119. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  120. if (sys_dirent.d_type == DT_DIR && \
  121. (strcmp(sys_dirent.d_name, ".")) && \
  122. (strcmp(sys_dirent.d_name, "..")))
  123. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  124. {
  125. char evt_path[MAXPATHLEN];
  126. int fd;
  127. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  128. sys_dir->d_name, evt_dir->d_name);
  129. fd = open(evt_path, O_RDONLY);
  130. if (fd < 0)
  131. return -EINVAL;
  132. close(fd);
  133. return 0;
  134. }
  135. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
  136. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  137. if (evt_dirent.d_type == DT_DIR && \
  138. (strcmp(evt_dirent.d_name, ".")) && \
  139. (strcmp(evt_dirent.d_name, "..")) && \
  140. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  141. #define MAX_EVENT_LENGTH 512
  142. struct tracepoint_path *tracepoint_id_to_path(u64 config)
  143. {
  144. struct tracepoint_path *path = NULL;
  145. DIR *sys_dir, *evt_dir;
  146. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  147. char id_buf[24];
  148. int fd;
  149. u64 id;
  150. char evt_path[MAXPATHLEN];
  151. char dir_path[MAXPATHLEN];
  152. if (debugfs_valid_mountpoint(tracing_events_path))
  153. return NULL;
  154. sys_dir = opendir(tracing_events_path);
  155. if (!sys_dir)
  156. return NULL;
  157. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  158. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  159. sys_dirent.d_name);
  160. evt_dir = opendir(dir_path);
  161. if (!evt_dir)
  162. continue;
  163. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  164. snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
  165. evt_dirent.d_name);
  166. fd = open(evt_path, O_RDONLY);
  167. if (fd < 0)
  168. continue;
  169. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  170. close(fd);
  171. continue;
  172. }
  173. close(fd);
  174. id = atoll(id_buf);
  175. if (id == config) {
  176. closedir(evt_dir);
  177. closedir(sys_dir);
  178. path = zalloc(sizeof(*path));
  179. path->system = malloc(MAX_EVENT_LENGTH);
  180. if (!path->system) {
  181. free(path);
  182. return NULL;
  183. }
  184. path->name = malloc(MAX_EVENT_LENGTH);
  185. if (!path->name) {
  186. zfree(&path->system);
  187. free(path);
  188. return NULL;
  189. }
  190. strncpy(path->system, sys_dirent.d_name,
  191. MAX_EVENT_LENGTH);
  192. strncpy(path->name, evt_dirent.d_name,
  193. MAX_EVENT_LENGTH);
  194. return path;
  195. }
  196. }
  197. closedir(evt_dir);
  198. }
  199. closedir(sys_dir);
  200. return NULL;
  201. }
  202. struct tracepoint_path *tracepoint_name_to_path(const char *name)
  203. {
  204. struct tracepoint_path *path = zalloc(sizeof(*path));
  205. char *str = strchr(name, ':');
  206. if (path == NULL || str == NULL) {
  207. free(path);
  208. return NULL;
  209. }
  210. path->system = strndup(name, str - name);
  211. path->name = strdup(str+1);
  212. if (path->system == NULL || path->name == NULL) {
  213. zfree(&path->system);
  214. zfree(&path->name);
  215. free(path);
  216. path = NULL;
  217. }
  218. return path;
  219. }
  220. const char *event_type(int type)
  221. {
  222. switch (type) {
  223. case PERF_TYPE_HARDWARE:
  224. return "hardware";
  225. case PERF_TYPE_SOFTWARE:
  226. return "software";
  227. case PERF_TYPE_TRACEPOINT:
  228. return "tracepoint";
  229. case PERF_TYPE_HW_CACHE:
  230. return "hardware-cache";
  231. default:
  232. break;
  233. }
  234. return "unknown";
  235. }
  236. static struct perf_evsel *
  237. __add_event(struct list_head *list, int *idx,
  238. struct perf_event_attr *attr,
  239. char *name, struct cpu_map *cpus)
  240. {
  241. struct perf_evsel *evsel;
  242. event_attr_init(attr);
  243. evsel = perf_evsel__new_idx(attr, (*idx)++);
  244. if (!evsel)
  245. return NULL;
  246. evsel->cpus = cpus;
  247. if (name)
  248. evsel->name = strdup(name);
  249. list_add_tail(&evsel->node, list);
  250. return evsel;
  251. }
  252. static int add_event(struct list_head *list, int *idx,
  253. struct perf_event_attr *attr, char *name)
  254. {
  255. return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
  256. }
  257. static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
  258. {
  259. int i, j;
  260. int n, longest = -1;
  261. for (i = 0; i < size; i++) {
  262. for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
  263. n = strlen(names[i][j]);
  264. if (n > longest && !strncasecmp(str, names[i][j], n))
  265. longest = n;
  266. }
  267. if (longest > 0)
  268. return i;
  269. }
  270. return -1;
  271. }
  272. int parse_events_add_cache(struct list_head *list, int *idx,
  273. char *type, char *op_result1, char *op_result2)
  274. {
  275. struct perf_event_attr attr;
  276. char name[MAX_NAME_LEN];
  277. int cache_type = -1, cache_op = -1, cache_result = -1;
  278. char *op_result[2] = { op_result1, op_result2 };
  279. int i, n;
  280. /*
  281. * No fallback - if we cannot get a clear cache type
  282. * then bail out:
  283. */
  284. cache_type = parse_aliases(type, perf_evsel__hw_cache,
  285. PERF_COUNT_HW_CACHE_MAX);
  286. if (cache_type == -1)
  287. return -EINVAL;
  288. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  289. for (i = 0; (i < 2) && (op_result[i]); i++) {
  290. char *str = op_result[i];
  291. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
  292. if (cache_op == -1) {
  293. cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
  294. PERF_COUNT_HW_CACHE_OP_MAX);
  295. if (cache_op >= 0) {
  296. if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
  297. return -EINVAL;
  298. continue;
  299. }
  300. }
  301. if (cache_result == -1) {
  302. cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
  303. PERF_COUNT_HW_CACHE_RESULT_MAX);
  304. if (cache_result >= 0)
  305. continue;
  306. }
  307. }
  308. /*
  309. * Fall back to reads:
  310. */
  311. if (cache_op == -1)
  312. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  313. /*
  314. * Fall back to accesses:
  315. */
  316. if (cache_result == -1)
  317. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  318. memset(&attr, 0, sizeof(attr));
  319. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  320. attr.type = PERF_TYPE_HW_CACHE;
  321. return add_event(list, idx, &attr, name);
  322. }
  323. static int add_tracepoint(struct list_head *list, int *idx,
  324. char *sys_name, char *evt_name)
  325. {
  326. struct perf_evsel *evsel;
  327. evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
  328. if (!evsel)
  329. return -ENOMEM;
  330. list_add_tail(&evsel->node, list);
  331. return 0;
  332. }
  333. static int add_tracepoint_multi_event(struct list_head *list, int *idx,
  334. char *sys_name, char *evt_name)
  335. {
  336. char evt_path[MAXPATHLEN];
  337. struct dirent *evt_ent;
  338. DIR *evt_dir;
  339. int ret = 0;
  340. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  341. evt_dir = opendir(evt_path);
  342. if (!evt_dir) {
  343. perror("Can't open event dir");
  344. return -1;
  345. }
  346. while (!ret && (evt_ent = readdir(evt_dir))) {
  347. if (!strcmp(evt_ent->d_name, ".")
  348. || !strcmp(evt_ent->d_name, "..")
  349. || !strcmp(evt_ent->d_name, "enable")
  350. || !strcmp(evt_ent->d_name, "filter"))
  351. continue;
  352. if (!strglobmatch(evt_ent->d_name, evt_name))
  353. continue;
  354. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  355. }
  356. closedir(evt_dir);
  357. return ret;
  358. }
  359. static int add_tracepoint_event(struct list_head *list, int *idx,
  360. char *sys_name, char *evt_name)
  361. {
  362. return strpbrk(evt_name, "*?") ?
  363. add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
  364. add_tracepoint(list, idx, sys_name, evt_name);
  365. }
  366. static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
  367. char *sys_name, char *evt_name)
  368. {
  369. struct dirent *events_ent;
  370. DIR *events_dir;
  371. int ret = 0;
  372. events_dir = opendir(tracing_events_path);
  373. if (!events_dir) {
  374. perror("Can't open event dir");
  375. return -1;
  376. }
  377. while (!ret && (events_ent = readdir(events_dir))) {
  378. if (!strcmp(events_ent->d_name, ".")
  379. || !strcmp(events_ent->d_name, "..")
  380. || !strcmp(events_ent->d_name, "enable")
  381. || !strcmp(events_ent->d_name, "header_event")
  382. || !strcmp(events_ent->d_name, "header_page"))
  383. continue;
  384. if (!strglobmatch(events_ent->d_name, sys_name))
  385. continue;
  386. ret = add_tracepoint_event(list, idx, events_ent->d_name,
  387. evt_name);
  388. }
  389. closedir(events_dir);
  390. return ret;
  391. }
  392. int parse_events_add_tracepoint(struct list_head *list, int *idx,
  393. char *sys, char *event)
  394. {
  395. int ret;
  396. ret = debugfs_valid_mountpoint(tracing_events_path);
  397. if (ret)
  398. return ret;
  399. if (strpbrk(sys, "*?"))
  400. return add_tracepoint_multi_sys(list, idx, sys, event);
  401. else
  402. return add_tracepoint_event(list, idx, sys, event);
  403. }
  404. static int
  405. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  406. {
  407. int i;
  408. for (i = 0; i < 3; i++) {
  409. if (!type || !type[i])
  410. break;
  411. #define CHECK_SET_TYPE(bit) \
  412. do { \
  413. if (attr->bp_type & bit) \
  414. return -EINVAL; \
  415. else \
  416. attr->bp_type |= bit; \
  417. } while (0)
  418. switch (type[i]) {
  419. case 'r':
  420. CHECK_SET_TYPE(HW_BREAKPOINT_R);
  421. break;
  422. case 'w':
  423. CHECK_SET_TYPE(HW_BREAKPOINT_W);
  424. break;
  425. case 'x':
  426. CHECK_SET_TYPE(HW_BREAKPOINT_X);
  427. break;
  428. default:
  429. return -EINVAL;
  430. }
  431. }
  432. #undef CHECK_SET_TYPE
  433. if (!attr->bp_type) /* Default */
  434. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  435. return 0;
  436. }
  437. int parse_events_add_breakpoint(struct list_head *list, int *idx,
  438. void *ptr, char *type)
  439. {
  440. struct perf_event_attr attr;
  441. memset(&attr, 0, sizeof(attr));
  442. attr.bp_addr = (unsigned long) ptr;
  443. if (parse_breakpoint_type(type, &attr))
  444. return -EINVAL;
  445. /*
  446. * We should find a nice way to override the access length
  447. * Provide some defaults for now
  448. */
  449. if (attr.bp_type == HW_BREAKPOINT_X)
  450. attr.bp_len = sizeof(long);
  451. else
  452. attr.bp_len = HW_BREAKPOINT_LEN_4;
  453. attr.type = PERF_TYPE_BREAKPOINT;
  454. attr.sample_period = 1;
  455. return add_event(list, idx, &attr, NULL);
  456. }
  457. static int config_term(struct perf_event_attr *attr,
  458. struct parse_events_term *term)
  459. {
  460. #define CHECK_TYPE_VAL(type) \
  461. do { \
  462. if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
  463. return -EINVAL; \
  464. } while (0)
  465. switch (term->type_term) {
  466. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  467. CHECK_TYPE_VAL(NUM);
  468. attr->config = term->val.num;
  469. break;
  470. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  471. CHECK_TYPE_VAL(NUM);
  472. attr->config1 = term->val.num;
  473. break;
  474. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  475. CHECK_TYPE_VAL(NUM);
  476. attr->config2 = term->val.num;
  477. break;
  478. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  479. CHECK_TYPE_VAL(NUM);
  480. attr->sample_period = term->val.num;
  481. break;
  482. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  483. /*
  484. * TODO uncomment when the field is available
  485. * attr->branch_sample_type = term->val.num;
  486. */
  487. break;
  488. case PARSE_EVENTS__TERM_TYPE_NAME:
  489. CHECK_TYPE_VAL(STR);
  490. break;
  491. default:
  492. return -EINVAL;
  493. }
  494. return 0;
  495. #undef CHECK_TYPE_VAL
  496. }
  497. static int config_attr(struct perf_event_attr *attr,
  498. struct list_head *head, int fail)
  499. {
  500. struct parse_events_term *term;
  501. list_for_each_entry(term, head, list)
  502. if (config_term(attr, term) && fail)
  503. return -EINVAL;
  504. return 0;
  505. }
  506. int parse_events_add_numeric(struct list_head *list, int *idx,
  507. u32 type, u64 config,
  508. struct list_head *head_config)
  509. {
  510. struct perf_event_attr attr;
  511. memset(&attr, 0, sizeof(attr));
  512. attr.type = type;
  513. attr.config = config;
  514. if (head_config &&
  515. config_attr(&attr, head_config, 1))
  516. return -EINVAL;
  517. return add_event(list, idx, &attr, NULL);
  518. }
  519. static int parse_events__is_name_term(struct parse_events_term *term)
  520. {
  521. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  522. }
  523. static char *pmu_event_name(struct list_head *head_terms)
  524. {
  525. struct parse_events_term *term;
  526. list_for_each_entry(term, head_terms, list)
  527. if (parse_events__is_name_term(term))
  528. return term->val.str;
  529. return NULL;
  530. }
  531. int parse_events_add_pmu(struct list_head *list, int *idx,
  532. char *name, struct list_head *head_config)
  533. {
  534. struct perf_event_attr attr;
  535. struct perf_pmu *pmu;
  536. struct perf_evsel *evsel;
  537. const char *unit;
  538. double scale;
  539. pmu = perf_pmu__find(name);
  540. if (!pmu)
  541. return -EINVAL;
  542. memset(&attr, 0, sizeof(attr));
  543. if (perf_pmu__check_alias(pmu, head_config, &unit, &scale))
  544. return -EINVAL;
  545. /*
  546. * Configure hardcoded terms first, no need to check
  547. * return value when called with fail == 0 ;)
  548. */
  549. config_attr(&attr, head_config, 0);
  550. if (perf_pmu__config(pmu, &attr, head_config))
  551. return -EINVAL;
  552. evsel = __add_event(list, idx, &attr, pmu_event_name(head_config),
  553. pmu->cpus);
  554. if (evsel) {
  555. evsel->unit = unit;
  556. evsel->scale = scale;
  557. }
  558. return evsel ? 0 : -ENOMEM;
  559. }
  560. int parse_events__modifier_group(struct list_head *list,
  561. char *event_mod)
  562. {
  563. return parse_events__modifier_event(list, event_mod, true);
  564. }
  565. void parse_events__set_leader(char *name, struct list_head *list)
  566. {
  567. struct perf_evsel *leader;
  568. __perf_evlist__set_leader(list);
  569. leader = list_entry(list->next, struct perf_evsel, node);
  570. leader->group_name = name ? strdup(name) : NULL;
  571. }
  572. /* list_event is assumed to point to malloc'ed memory */
  573. void parse_events_update_lists(struct list_head *list_event,
  574. struct list_head *list_all)
  575. {
  576. /*
  577. * Called for single event definition. Update the
  578. * 'all event' list, and reinit the 'single event'
  579. * list, for next event definition.
  580. */
  581. list_splice_tail(list_event, list_all);
  582. free(list_event);
  583. }
  584. struct event_modifier {
  585. int eu;
  586. int ek;
  587. int eh;
  588. int eH;
  589. int eG;
  590. int precise;
  591. int exclude_GH;
  592. int sample_read;
  593. int pinned;
  594. };
  595. static int get_event_modifier(struct event_modifier *mod, char *str,
  596. struct perf_evsel *evsel)
  597. {
  598. int eu = evsel ? evsel->attr.exclude_user : 0;
  599. int ek = evsel ? evsel->attr.exclude_kernel : 0;
  600. int eh = evsel ? evsel->attr.exclude_hv : 0;
  601. int eH = evsel ? evsel->attr.exclude_host : 0;
  602. int eG = evsel ? evsel->attr.exclude_guest : 0;
  603. int precise = evsel ? evsel->attr.precise_ip : 0;
  604. int sample_read = 0;
  605. int pinned = evsel ? evsel->attr.pinned : 0;
  606. int exclude = eu | ek | eh;
  607. int exclude_GH = evsel ? evsel->exclude_GH : 0;
  608. memset(mod, 0, sizeof(*mod));
  609. while (*str) {
  610. if (*str == 'u') {
  611. if (!exclude)
  612. exclude = eu = ek = eh = 1;
  613. eu = 0;
  614. } else if (*str == 'k') {
  615. if (!exclude)
  616. exclude = eu = ek = eh = 1;
  617. ek = 0;
  618. } else if (*str == 'h') {
  619. if (!exclude)
  620. exclude = eu = ek = eh = 1;
  621. eh = 0;
  622. } else if (*str == 'G') {
  623. if (!exclude_GH)
  624. exclude_GH = eG = eH = 1;
  625. eG = 0;
  626. } else if (*str == 'H') {
  627. if (!exclude_GH)
  628. exclude_GH = eG = eH = 1;
  629. eH = 0;
  630. } else if (*str == 'p') {
  631. precise++;
  632. /* use of precise requires exclude_guest */
  633. if (!exclude_GH)
  634. eG = 1;
  635. } else if (*str == 'S') {
  636. sample_read = 1;
  637. } else if (*str == 'D') {
  638. pinned = 1;
  639. } else
  640. break;
  641. ++str;
  642. }
  643. /*
  644. * precise ip:
  645. *
  646. * 0 - SAMPLE_IP can have arbitrary skid
  647. * 1 - SAMPLE_IP must have constant skid
  648. * 2 - SAMPLE_IP requested to have 0 skid
  649. * 3 - SAMPLE_IP must have 0 skid
  650. *
  651. * See also PERF_RECORD_MISC_EXACT_IP
  652. */
  653. if (precise > 3)
  654. return -EINVAL;
  655. mod->eu = eu;
  656. mod->ek = ek;
  657. mod->eh = eh;
  658. mod->eH = eH;
  659. mod->eG = eG;
  660. mod->precise = precise;
  661. mod->exclude_GH = exclude_GH;
  662. mod->sample_read = sample_read;
  663. mod->pinned = pinned;
  664. return 0;
  665. }
  666. /*
  667. * Basic modifier sanity check to validate it contains only one
  668. * instance of any modifier (apart from 'p') present.
  669. */
  670. static int check_modifier(char *str)
  671. {
  672. char *p = str;
  673. /* The sizeof includes 0 byte as well. */
  674. if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
  675. return -1;
  676. while (*p) {
  677. if (*p != 'p' && strchr(p + 1, *p))
  678. return -1;
  679. p++;
  680. }
  681. return 0;
  682. }
  683. int parse_events__modifier_event(struct list_head *list, char *str, bool add)
  684. {
  685. struct perf_evsel *evsel;
  686. struct event_modifier mod;
  687. if (str == NULL)
  688. return 0;
  689. if (check_modifier(str))
  690. return -EINVAL;
  691. if (!add && get_event_modifier(&mod, str, NULL))
  692. return -EINVAL;
  693. __evlist__for_each(list, evsel) {
  694. if (add && get_event_modifier(&mod, str, evsel))
  695. return -EINVAL;
  696. evsel->attr.exclude_user = mod.eu;
  697. evsel->attr.exclude_kernel = mod.ek;
  698. evsel->attr.exclude_hv = mod.eh;
  699. evsel->attr.precise_ip = mod.precise;
  700. evsel->attr.exclude_host = mod.eH;
  701. evsel->attr.exclude_guest = mod.eG;
  702. evsel->exclude_GH = mod.exclude_GH;
  703. evsel->sample_read = mod.sample_read;
  704. if (perf_evsel__is_group_leader(evsel))
  705. evsel->attr.pinned = mod.pinned;
  706. }
  707. return 0;
  708. }
  709. int parse_events_name(struct list_head *list, char *name)
  710. {
  711. struct perf_evsel *evsel;
  712. __evlist__for_each(list, evsel) {
  713. if (!evsel->name)
  714. evsel->name = strdup(name);
  715. }
  716. return 0;
  717. }
  718. static int parse_events__scanner(const char *str, void *data, int start_token);
  719. static int parse_events_fixup(int ret, const char *str, void *data,
  720. int start_token)
  721. {
  722. char *o = strdup(str);
  723. char *s = NULL;
  724. char *t = o;
  725. char *p;
  726. int len = 0;
  727. if (!o)
  728. return ret;
  729. while ((p = strsep(&t, ",")) != NULL) {
  730. if (s)
  731. str_append(&s, &len, ",");
  732. str_append(&s, &len, "cpu/");
  733. str_append(&s, &len, p);
  734. str_append(&s, &len, "/");
  735. }
  736. free(o);
  737. if (!s)
  738. return -ENOMEM;
  739. return parse_events__scanner(s, data, start_token);
  740. }
  741. static int parse_events__scanner(const char *str, void *data, int start_token)
  742. {
  743. YY_BUFFER_STATE buffer;
  744. void *scanner;
  745. int ret;
  746. ret = parse_events_lex_init_extra(start_token, &scanner);
  747. if (ret)
  748. return ret;
  749. buffer = parse_events__scan_string(str, scanner);
  750. #ifdef PARSER_DEBUG
  751. parse_events_debug = 1;
  752. #endif
  753. ret = parse_events_parse(data, scanner);
  754. parse_events__flush_buffer(buffer, scanner);
  755. parse_events__delete_buffer(buffer, scanner);
  756. parse_events_lex_destroy(scanner);
  757. if (ret && !strchr(str, '/'))
  758. ret = parse_events_fixup(ret, str, data, start_token);
  759. return ret;
  760. }
  761. /*
  762. * parse event config string, return a list of event terms.
  763. */
  764. int parse_events_terms(struct list_head *terms, const char *str)
  765. {
  766. struct parse_events_terms data = {
  767. .terms = NULL,
  768. };
  769. int ret;
  770. ret = parse_events__scanner(str, &data, PE_START_TERMS);
  771. if (!ret) {
  772. list_splice(data.terms, terms);
  773. zfree(&data.terms);
  774. return 0;
  775. }
  776. if (data.terms)
  777. parse_events__free_terms(data.terms);
  778. return ret;
  779. }
  780. int parse_events(struct perf_evlist *evlist, const char *str)
  781. {
  782. struct parse_events_evlist data = {
  783. .list = LIST_HEAD_INIT(data.list),
  784. .idx = evlist->nr_entries,
  785. };
  786. int ret;
  787. ret = parse_events__scanner(str, &data, PE_START_EVENTS);
  788. if (!ret) {
  789. int entries = data.idx - evlist->nr_entries;
  790. perf_evlist__splice_list_tail(evlist, &data.list, entries);
  791. evlist->nr_groups += data.nr_groups;
  792. return 0;
  793. }
  794. /*
  795. * There are 2 users - builtin-record and builtin-test objects.
  796. * Both call perf_evlist__delete in case of error, so we dont
  797. * need to bother.
  798. */
  799. return ret;
  800. }
  801. int parse_events_option(const struct option *opt, const char *str,
  802. int unset __maybe_unused)
  803. {
  804. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  805. int ret = parse_events(evlist, str);
  806. if (ret) {
  807. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  808. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  809. }
  810. return ret;
  811. }
  812. int parse_filter(const struct option *opt, const char *str,
  813. int unset __maybe_unused)
  814. {
  815. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  816. struct perf_evsel *last = NULL;
  817. if (evlist->nr_entries > 0)
  818. last = perf_evlist__last(evlist);
  819. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  820. fprintf(stderr,
  821. "-F option should follow a -e tracepoint option\n");
  822. return -1;
  823. }
  824. last->filter = strdup(str);
  825. if (last->filter == NULL) {
  826. fprintf(stderr, "not enough memory to hold filter string\n");
  827. return -1;
  828. }
  829. return 0;
  830. }
  831. static const char * const event_type_descriptors[] = {
  832. "Hardware event",
  833. "Software event",
  834. "Tracepoint event",
  835. "Hardware cache event",
  836. "Raw hardware event descriptor",
  837. "Hardware breakpoint",
  838. };
  839. /*
  840. * Print the events from <debugfs_mount_point>/tracing/events
  841. */
  842. void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
  843. bool name_only)
  844. {
  845. DIR *sys_dir, *evt_dir;
  846. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  847. char evt_path[MAXPATHLEN];
  848. char dir_path[MAXPATHLEN];
  849. if (debugfs_valid_mountpoint(tracing_events_path)) {
  850. printf(" [ Tracepoints not available: %s ]\n", strerror(errno));
  851. return;
  852. }
  853. sys_dir = opendir(tracing_events_path);
  854. if (!sys_dir)
  855. return;
  856. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  857. if (subsys_glob != NULL &&
  858. !strglobmatch(sys_dirent.d_name, subsys_glob))
  859. continue;
  860. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  861. sys_dirent.d_name);
  862. evt_dir = opendir(dir_path);
  863. if (!evt_dir)
  864. continue;
  865. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  866. if (event_glob != NULL &&
  867. !strglobmatch(evt_dirent.d_name, event_glob))
  868. continue;
  869. if (name_only) {
  870. printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
  871. continue;
  872. }
  873. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  874. sys_dirent.d_name, evt_dirent.d_name);
  875. printf(" %-50s [%s]\n", evt_path,
  876. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  877. }
  878. closedir(evt_dir);
  879. }
  880. closedir(sys_dir);
  881. }
  882. /*
  883. * Check whether event is in <debugfs_mount_point>/tracing/events
  884. */
  885. int is_valid_tracepoint(const char *event_string)
  886. {
  887. DIR *sys_dir, *evt_dir;
  888. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  889. char evt_path[MAXPATHLEN];
  890. char dir_path[MAXPATHLEN];
  891. if (debugfs_valid_mountpoint(tracing_events_path))
  892. return 0;
  893. sys_dir = opendir(tracing_events_path);
  894. if (!sys_dir)
  895. return 0;
  896. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  897. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  898. sys_dirent.d_name);
  899. evt_dir = opendir(dir_path);
  900. if (!evt_dir)
  901. continue;
  902. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  903. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  904. sys_dirent.d_name, evt_dirent.d_name);
  905. if (!strcmp(evt_path, event_string)) {
  906. closedir(evt_dir);
  907. closedir(sys_dir);
  908. return 1;
  909. }
  910. }
  911. closedir(evt_dir);
  912. }
  913. closedir(sys_dir);
  914. return 0;
  915. }
  916. static bool is_event_supported(u8 type, unsigned config)
  917. {
  918. bool ret = true;
  919. struct perf_evsel *evsel;
  920. struct perf_event_attr attr = {
  921. .type = type,
  922. .config = config,
  923. .disabled = 1,
  924. .exclude_kernel = 1,
  925. };
  926. struct {
  927. struct thread_map map;
  928. int threads[1];
  929. } tmap = {
  930. .map.nr = 1,
  931. .threads = { 0 },
  932. };
  933. evsel = perf_evsel__new(&attr);
  934. if (evsel) {
  935. ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
  936. perf_evsel__delete(evsel);
  937. }
  938. return ret;
  939. }
  940. static void __print_events_type(u8 type, struct event_symbol *syms,
  941. unsigned max)
  942. {
  943. char name[64];
  944. unsigned i;
  945. for (i = 0; i < max ; i++, syms++) {
  946. if (!is_event_supported(type, i))
  947. continue;
  948. if (strlen(syms->alias))
  949. snprintf(name, sizeof(name), "%s OR %s",
  950. syms->symbol, syms->alias);
  951. else
  952. snprintf(name, sizeof(name), "%s", syms->symbol);
  953. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  954. }
  955. }
  956. void print_events_type(u8 type)
  957. {
  958. if (type == PERF_TYPE_SOFTWARE)
  959. __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
  960. else
  961. __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
  962. }
  963. int print_hwcache_events(const char *event_glob, bool name_only)
  964. {
  965. unsigned int type, op, i, printed = 0;
  966. char name[64];
  967. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  968. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  969. /* skip invalid cache type */
  970. if (!perf_evsel__is_cache_op_valid(type, op))
  971. continue;
  972. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  973. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  974. name, sizeof(name));
  975. if (event_glob != NULL && !strglobmatch(name, event_glob))
  976. continue;
  977. if (!is_event_supported(PERF_TYPE_HW_CACHE,
  978. type | (op << 8) | (i << 16)))
  979. continue;
  980. if (name_only)
  981. printf("%s ", name);
  982. else
  983. printf(" %-50s [%s]\n", name,
  984. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  985. ++printed;
  986. }
  987. }
  988. }
  989. if (printed)
  990. printf("\n");
  991. return printed;
  992. }
  993. static void print_symbol_events(const char *event_glob, unsigned type,
  994. struct event_symbol *syms, unsigned max,
  995. bool name_only)
  996. {
  997. unsigned i, printed = 0;
  998. char name[MAX_NAME_LEN];
  999. for (i = 0; i < max; i++, syms++) {
  1000. if (event_glob != NULL &&
  1001. !(strglobmatch(syms->symbol, event_glob) ||
  1002. (syms->alias && strglobmatch(syms->alias, event_glob))))
  1003. continue;
  1004. if (!is_event_supported(type, i))
  1005. continue;
  1006. if (name_only) {
  1007. printf("%s ", syms->symbol);
  1008. continue;
  1009. }
  1010. if (strlen(syms->alias))
  1011. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  1012. else
  1013. strncpy(name, syms->symbol, MAX_NAME_LEN);
  1014. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  1015. printed++;
  1016. }
  1017. if (printed)
  1018. printf("\n");
  1019. }
  1020. /*
  1021. * Print the help text for the event symbols:
  1022. */
  1023. void print_events(const char *event_glob, bool name_only)
  1024. {
  1025. if (!name_only) {
  1026. printf("\n");
  1027. printf("List of pre-defined events (to be used in -e):\n");
  1028. }
  1029. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  1030. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  1031. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  1032. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  1033. print_hwcache_events(event_glob, name_only);
  1034. print_pmu_events(event_glob, name_only);
  1035. if (event_glob != NULL)
  1036. return;
  1037. if (!name_only) {
  1038. printf(" %-50s [%s]\n",
  1039. "rNNN",
  1040. event_type_descriptors[PERF_TYPE_RAW]);
  1041. printf(" %-50s [%s]\n",
  1042. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  1043. event_type_descriptors[PERF_TYPE_RAW]);
  1044. printf(" (see 'man perf-list' on how to encode it)\n");
  1045. printf("\n");
  1046. printf(" %-50s [%s]\n",
  1047. "mem:<addr>[:access]",
  1048. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  1049. printf("\n");
  1050. }
  1051. print_tracepoint_events(NULL, NULL, name_only);
  1052. }
  1053. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  1054. {
  1055. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  1056. }
  1057. static int new_term(struct parse_events_term **_term, int type_val,
  1058. int type_term, char *config,
  1059. char *str, u64 num)
  1060. {
  1061. struct parse_events_term *term;
  1062. term = zalloc(sizeof(*term));
  1063. if (!term)
  1064. return -ENOMEM;
  1065. INIT_LIST_HEAD(&term->list);
  1066. term->type_val = type_val;
  1067. term->type_term = type_term;
  1068. term->config = config;
  1069. switch (type_val) {
  1070. case PARSE_EVENTS__TERM_TYPE_NUM:
  1071. term->val.num = num;
  1072. break;
  1073. case PARSE_EVENTS__TERM_TYPE_STR:
  1074. term->val.str = str;
  1075. break;
  1076. default:
  1077. free(term);
  1078. return -EINVAL;
  1079. }
  1080. *_term = term;
  1081. return 0;
  1082. }
  1083. int parse_events_term__num(struct parse_events_term **term,
  1084. int type_term, char *config, u64 num)
  1085. {
  1086. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1087. config, NULL, num);
  1088. }
  1089. int parse_events_term__str(struct parse_events_term **term,
  1090. int type_term, char *config, char *str)
  1091. {
  1092. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1093. config, str, 0);
  1094. }
  1095. int parse_events_term__sym_hw(struct parse_events_term **term,
  1096. char *config, unsigned idx)
  1097. {
  1098. struct event_symbol *sym;
  1099. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1100. sym = &event_symbols_hw[idx];
  1101. if (config)
  1102. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1103. PARSE_EVENTS__TERM_TYPE_USER, config,
  1104. (char *) sym->symbol, 0);
  1105. else
  1106. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1107. PARSE_EVENTS__TERM_TYPE_USER,
  1108. (char *) "event", (char *) sym->symbol, 0);
  1109. }
  1110. int parse_events_term__clone(struct parse_events_term **new,
  1111. struct parse_events_term *term)
  1112. {
  1113. return new_term(new, term->type_val, term->type_term, term->config,
  1114. term->val.str, term->val.num);
  1115. }
  1116. void parse_events__free_terms(struct list_head *terms)
  1117. {
  1118. struct parse_events_term *term, *h;
  1119. list_for_each_entry_safe(term, h, terms, list)
  1120. free(term);
  1121. }