parse-events.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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. int open_return;
  920. struct perf_evsel *evsel;
  921. struct perf_event_attr attr = {
  922. .type = type,
  923. .config = config,
  924. .disabled = 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. open_return = perf_evsel__open(evsel, NULL, &tmap.map);
  936. ret = open_return >= 0;
  937. if (open_return == -EACCES) {
  938. /*
  939. * This happens if the paranoid value
  940. * /proc/sys/kernel/perf_event_paranoid is set to 2
  941. * Re-run with exclude_kernel set; we don't do that
  942. * by default as some ARM machines do not support it.
  943. *
  944. */
  945. evsel->attr.exclude_kernel = 1;
  946. ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
  947. }
  948. perf_evsel__delete(evsel);
  949. }
  950. return ret;
  951. }
  952. static void __print_events_type(u8 type, struct event_symbol *syms,
  953. unsigned max)
  954. {
  955. char name[64];
  956. unsigned i;
  957. for (i = 0; i < max ; i++, syms++) {
  958. if (!is_event_supported(type, i))
  959. continue;
  960. if (strlen(syms->alias))
  961. snprintf(name, sizeof(name), "%s OR %s",
  962. syms->symbol, syms->alias);
  963. else
  964. snprintf(name, sizeof(name), "%s", syms->symbol);
  965. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  966. }
  967. }
  968. void print_events_type(u8 type)
  969. {
  970. if (type == PERF_TYPE_SOFTWARE)
  971. __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
  972. else
  973. __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
  974. }
  975. int print_hwcache_events(const char *event_glob, bool name_only)
  976. {
  977. unsigned int type, op, i, printed = 0;
  978. char name[64];
  979. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  980. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  981. /* skip invalid cache type */
  982. if (!perf_evsel__is_cache_op_valid(type, op))
  983. continue;
  984. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  985. __perf_evsel__hw_cache_type_op_res_name(type, op, i,
  986. name, sizeof(name));
  987. if (event_glob != NULL && !strglobmatch(name, event_glob))
  988. continue;
  989. if (!is_event_supported(PERF_TYPE_HW_CACHE,
  990. type | (op << 8) | (i << 16)))
  991. continue;
  992. if (name_only)
  993. printf("%s ", name);
  994. else
  995. printf(" %-50s [%s]\n", name,
  996. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  997. ++printed;
  998. }
  999. }
  1000. }
  1001. if (printed)
  1002. printf("\n");
  1003. return printed;
  1004. }
  1005. static void print_symbol_events(const char *event_glob, unsigned type,
  1006. struct event_symbol *syms, unsigned max,
  1007. bool name_only)
  1008. {
  1009. unsigned i, printed = 0;
  1010. char name[MAX_NAME_LEN];
  1011. for (i = 0; i < max; i++, syms++) {
  1012. if (event_glob != NULL &&
  1013. !(strglobmatch(syms->symbol, event_glob) ||
  1014. (syms->alias && strglobmatch(syms->alias, event_glob))))
  1015. continue;
  1016. if (!is_event_supported(type, i))
  1017. continue;
  1018. if (name_only) {
  1019. printf("%s ", syms->symbol);
  1020. continue;
  1021. }
  1022. if (strlen(syms->alias))
  1023. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  1024. else
  1025. strncpy(name, syms->symbol, MAX_NAME_LEN);
  1026. printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
  1027. printed++;
  1028. }
  1029. if (printed)
  1030. printf("\n");
  1031. }
  1032. /*
  1033. * Print the help text for the event symbols:
  1034. */
  1035. void print_events(const char *event_glob, bool name_only)
  1036. {
  1037. if (!name_only) {
  1038. printf("\n");
  1039. printf("List of pre-defined events (to be used in -e):\n");
  1040. }
  1041. print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
  1042. event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
  1043. print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
  1044. event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
  1045. print_hwcache_events(event_glob, name_only);
  1046. print_pmu_events(event_glob, name_only);
  1047. if (event_glob != NULL)
  1048. return;
  1049. if (!name_only) {
  1050. printf(" %-50s [%s]\n",
  1051. "rNNN",
  1052. event_type_descriptors[PERF_TYPE_RAW]);
  1053. printf(" %-50s [%s]\n",
  1054. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  1055. event_type_descriptors[PERF_TYPE_RAW]);
  1056. printf(" (see 'man perf-list' on how to encode it)\n");
  1057. printf("\n");
  1058. printf(" %-50s [%s]\n",
  1059. "mem:<addr>[:access]",
  1060. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  1061. printf("\n");
  1062. }
  1063. print_tracepoint_events(NULL, NULL, name_only);
  1064. }
  1065. int parse_events__is_hardcoded_term(struct parse_events_term *term)
  1066. {
  1067. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  1068. }
  1069. static int new_term(struct parse_events_term **_term, int type_val,
  1070. int type_term, char *config,
  1071. char *str, u64 num)
  1072. {
  1073. struct parse_events_term *term;
  1074. term = zalloc(sizeof(*term));
  1075. if (!term)
  1076. return -ENOMEM;
  1077. INIT_LIST_HEAD(&term->list);
  1078. term->type_val = type_val;
  1079. term->type_term = type_term;
  1080. term->config = config;
  1081. switch (type_val) {
  1082. case PARSE_EVENTS__TERM_TYPE_NUM:
  1083. term->val.num = num;
  1084. break;
  1085. case PARSE_EVENTS__TERM_TYPE_STR:
  1086. term->val.str = str;
  1087. break;
  1088. default:
  1089. free(term);
  1090. return -EINVAL;
  1091. }
  1092. *_term = term;
  1093. return 0;
  1094. }
  1095. int parse_events_term__num(struct parse_events_term **term,
  1096. int type_term, char *config, u64 num)
  1097. {
  1098. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  1099. config, NULL, num);
  1100. }
  1101. int parse_events_term__str(struct parse_events_term **term,
  1102. int type_term, char *config, char *str)
  1103. {
  1104. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  1105. config, str, 0);
  1106. }
  1107. int parse_events_term__sym_hw(struct parse_events_term **term,
  1108. char *config, unsigned idx)
  1109. {
  1110. struct event_symbol *sym;
  1111. BUG_ON(idx >= PERF_COUNT_HW_MAX);
  1112. sym = &event_symbols_hw[idx];
  1113. if (config)
  1114. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1115. PARSE_EVENTS__TERM_TYPE_USER, config,
  1116. (char *) sym->symbol, 0);
  1117. else
  1118. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
  1119. PARSE_EVENTS__TERM_TYPE_USER,
  1120. (char *) "event", (char *) sym->symbol, 0);
  1121. }
  1122. int parse_events_term__clone(struct parse_events_term **new,
  1123. struct parse_events_term *term)
  1124. {
  1125. return new_term(new, term->type_val, term->type_term, term->config,
  1126. term->val.str, term->val.num);
  1127. }
  1128. void parse_events__free_terms(struct list_head *terms)
  1129. {
  1130. struct parse_events_term *term, *h;
  1131. list_for_each_entry_safe(term, h, terms, list)
  1132. free(term);
  1133. }