parse-events.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. #include "util.h"
  2. #include "../perf.h"
  3. #include "parse-options.h"
  4. #include "parse-events.h"
  5. #include "exec_cmd.h"
  6. #include "string.h"
  7. #include "cache.h"
  8. #include "header.h"
  9. int nr_counters;
  10. struct perf_event_attr attrs[MAX_COUNTERS];
  11. struct event_symbol {
  12. u8 type;
  13. u64 config;
  14. const char *symbol;
  15. const char *alias;
  16. };
  17. enum event_result {
  18. EVT_FAILED,
  19. EVT_HANDLED,
  20. EVT_HANDLED_ALL
  21. };
  22. char debugfs_path[MAXPATHLEN];
  23. #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
  24. #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
  25. static struct event_symbol event_symbols[] = {
  26. { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
  27. { CHW(INSTRUCTIONS), "instructions", "" },
  28. { CHW(CACHE_REFERENCES), "cache-references", "" },
  29. { CHW(CACHE_MISSES), "cache-misses", "" },
  30. { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
  31. { CHW(BRANCH_MISSES), "branch-misses", "" },
  32. { CHW(BUS_CYCLES), "bus-cycles", "" },
  33. { CSW(CPU_CLOCK), "cpu-clock", "" },
  34. { CSW(TASK_CLOCK), "task-clock", "" },
  35. { CSW(PAGE_FAULTS), "page-faults", "faults" },
  36. { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
  37. { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
  38. { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
  39. { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
  40. };
  41. #define __PERF_EVENT_FIELD(config, name) \
  42. ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
  43. #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
  44. #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
  45. #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
  46. #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
  47. static const char *hw_event_names[] = {
  48. "cycles",
  49. "instructions",
  50. "cache-references",
  51. "cache-misses",
  52. "branches",
  53. "branch-misses",
  54. "bus-cycles",
  55. };
  56. static const char *sw_event_names[] = {
  57. "cpu-clock-msecs",
  58. "task-clock-msecs",
  59. "page-faults",
  60. "context-switches",
  61. "CPU-migrations",
  62. "minor-faults",
  63. "major-faults",
  64. };
  65. #define MAX_ALIASES 8
  66. static const char *hw_cache[][MAX_ALIASES] = {
  67. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  68. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  69. { "LLC", "L2" },
  70. { "dTLB", "d-tlb", "Data-TLB", },
  71. { "iTLB", "i-tlb", "Instruction-TLB", },
  72. { "branch", "branches", "bpu", "btb", "bpc", },
  73. };
  74. static const char *hw_cache_op[][MAX_ALIASES] = {
  75. { "load", "loads", "read", },
  76. { "store", "stores", "write", },
  77. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  78. };
  79. static const char *hw_cache_result[][MAX_ALIASES] = {
  80. { "refs", "Reference", "ops", "access", },
  81. { "misses", "miss", },
  82. };
  83. #define C(x) PERF_COUNT_HW_CACHE_##x
  84. #define CACHE_READ (1 << C(OP_READ))
  85. #define CACHE_WRITE (1 << C(OP_WRITE))
  86. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  87. #define COP(x) (1 << x)
  88. /*
  89. * cache operartion stat
  90. * L1I : Read and prefetch only
  91. * ITLB and BPU : Read-only
  92. */
  93. static unsigned long hw_cache_stat[C(MAX)] = {
  94. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  95. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  96. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  97. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  98. [C(ITLB)] = (CACHE_READ),
  99. [C(BPU)] = (CACHE_READ),
  100. };
  101. #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
  102. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  103. if (sys_dirent.d_type == DT_DIR && \
  104. (strcmp(sys_dirent.d_name, ".")) && \
  105. (strcmp(sys_dirent.d_name, "..")))
  106. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  107. {
  108. char evt_path[MAXPATHLEN];
  109. int fd;
  110. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  111. sys_dir->d_name, evt_dir->d_name);
  112. fd = open(evt_path, O_RDONLY);
  113. if (fd < 0)
  114. return -EINVAL;
  115. close(fd);
  116. return 0;
  117. }
  118. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
  119. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  120. if (evt_dirent.d_type == DT_DIR && \
  121. (strcmp(evt_dirent.d_name, ".")) && \
  122. (strcmp(evt_dirent.d_name, "..")) && \
  123. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  124. #define MAX_EVENT_LENGTH 512
  125. int valid_debugfs_mount(const char *debugfs)
  126. {
  127. struct statfs st_fs;
  128. if (statfs(debugfs, &st_fs) < 0)
  129. return -ENOENT;
  130. else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
  131. return -ENOENT;
  132. return 0;
  133. }
  134. struct tracepoint_path *tracepoint_id_to_path(u64 config)
  135. {
  136. struct tracepoint_path *path = NULL;
  137. DIR *sys_dir, *evt_dir;
  138. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  139. char id_buf[4];
  140. int sys_dir_fd, fd;
  141. u64 id;
  142. char evt_path[MAXPATHLEN];
  143. if (valid_debugfs_mount(debugfs_path))
  144. return NULL;
  145. sys_dir = opendir(debugfs_path);
  146. if (!sys_dir)
  147. goto cleanup;
  148. sys_dir_fd = dirfd(sys_dir);
  149. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  150. int dfd = openat(sys_dir_fd, sys_dirent.d_name,
  151. O_RDONLY|O_DIRECTORY), evt_dir_fd;
  152. if (dfd == -1)
  153. continue;
  154. evt_dir = fdopendir(dfd);
  155. if (!evt_dir) {
  156. close(dfd);
  157. continue;
  158. }
  159. evt_dir_fd = dirfd(evt_dir);
  160. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  161. snprintf(evt_path, MAXPATHLEN, "%s/id",
  162. evt_dirent.d_name);
  163. fd = openat(evt_dir_fd, evt_path, O_RDONLY);
  164. if (fd < 0)
  165. continue;
  166. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  167. close(fd);
  168. continue;
  169. }
  170. close(fd);
  171. id = atoll(id_buf);
  172. if (id == config) {
  173. closedir(evt_dir);
  174. closedir(sys_dir);
  175. path = calloc(1, sizeof(path));
  176. path->system = malloc(MAX_EVENT_LENGTH);
  177. if (!path->system) {
  178. free(path);
  179. return NULL;
  180. }
  181. path->name = malloc(MAX_EVENT_LENGTH);
  182. if (!path->name) {
  183. free(path->system);
  184. free(path);
  185. return NULL;
  186. }
  187. strncpy(path->system, sys_dirent.d_name,
  188. MAX_EVENT_LENGTH);
  189. strncpy(path->name, evt_dirent.d_name,
  190. MAX_EVENT_LENGTH);
  191. return path;
  192. }
  193. }
  194. closedir(evt_dir);
  195. }
  196. cleanup:
  197. closedir(sys_dir);
  198. return NULL;
  199. }
  200. #define TP_PATH_LEN (MAX_EVENT_LENGTH * 2 + 1)
  201. static const char *tracepoint_id_to_name(u64 config)
  202. {
  203. static char buf[TP_PATH_LEN];
  204. struct tracepoint_path *path;
  205. path = tracepoint_id_to_path(config);
  206. if (path) {
  207. snprintf(buf, TP_PATH_LEN, "%s:%s", path->system, path->name);
  208. free(path->name);
  209. free(path->system);
  210. free(path);
  211. } else
  212. snprintf(buf, TP_PATH_LEN, "%s:%s", "unknown", "unknown");
  213. return buf;
  214. }
  215. static int is_cache_op_valid(u8 cache_type, u8 cache_op)
  216. {
  217. if (hw_cache_stat[cache_type] & COP(cache_op))
  218. return 1; /* valid */
  219. else
  220. return 0; /* invalid */
  221. }
  222. static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
  223. {
  224. static char name[50];
  225. if (cache_result) {
  226. sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
  227. hw_cache_op[cache_op][0],
  228. hw_cache_result[cache_result][0]);
  229. } else {
  230. sprintf(name, "%s-%s", hw_cache[cache_type][0],
  231. hw_cache_op[cache_op][1]);
  232. }
  233. return name;
  234. }
  235. const char *event_name(int counter)
  236. {
  237. u64 config = attrs[counter].config;
  238. int type = attrs[counter].type;
  239. return __event_name(type, config);
  240. }
  241. const char *__event_name(int type, u64 config)
  242. {
  243. static char buf[32];
  244. if (type == PERF_TYPE_RAW) {
  245. sprintf(buf, "raw 0x%llx", config);
  246. return buf;
  247. }
  248. switch (type) {
  249. case PERF_TYPE_HARDWARE:
  250. if (config < PERF_COUNT_HW_MAX)
  251. return hw_event_names[config];
  252. return "unknown-hardware";
  253. case PERF_TYPE_HW_CACHE: {
  254. u8 cache_type, cache_op, cache_result;
  255. cache_type = (config >> 0) & 0xff;
  256. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  257. return "unknown-ext-hardware-cache-type";
  258. cache_op = (config >> 8) & 0xff;
  259. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  260. return "unknown-ext-hardware-cache-op";
  261. cache_result = (config >> 16) & 0xff;
  262. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  263. return "unknown-ext-hardware-cache-result";
  264. if (!is_cache_op_valid(cache_type, cache_op))
  265. return "invalid-cache";
  266. return event_cache_name(cache_type, cache_op, cache_result);
  267. }
  268. case PERF_TYPE_SOFTWARE:
  269. if (config < PERF_COUNT_SW_MAX)
  270. return sw_event_names[config];
  271. return "unknown-software";
  272. case PERF_TYPE_TRACEPOINT:
  273. return tracepoint_id_to_name(config);
  274. default:
  275. break;
  276. }
  277. return "unknown";
  278. }
  279. static int parse_aliases(const char **str, const char *names[][MAX_ALIASES], int size)
  280. {
  281. int i, j;
  282. int n, longest = -1;
  283. for (i = 0; i < size; i++) {
  284. for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
  285. n = strlen(names[i][j]);
  286. if (n > longest && !strncasecmp(*str, names[i][j], n))
  287. longest = n;
  288. }
  289. if (longest > 0) {
  290. *str += longest;
  291. return i;
  292. }
  293. }
  294. return -1;
  295. }
  296. static enum event_result
  297. parse_generic_hw_event(const char **str, struct perf_event_attr *attr)
  298. {
  299. const char *s = *str;
  300. int cache_type = -1, cache_op = -1, cache_result = -1;
  301. cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
  302. /*
  303. * No fallback - if we cannot get a clear cache type
  304. * then bail out:
  305. */
  306. if (cache_type == -1)
  307. return EVT_FAILED;
  308. while ((cache_op == -1 || cache_result == -1) && *s == '-') {
  309. ++s;
  310. if (cache_op == -1) {
  311. cache_op = parse_aliases(&s, hw_cache_op,
  312. PERF_COUNT_HW_CACHE_OP_MAX);
  313. if (cache_op >= 0) {
  314. if (!is_cache_op_valid(cache_type, cache_op))
  315. return 0;
  316. continue;
  317. }
  318. }
  319. if (cache_result == -1) {
  320. cache_result = parse_aliases(&s, hw_cache_result,
  321. PERF_COUNT_HW_CACHE_RESULT_MAX);
  322. if (cache_result >= 0)
  323. continue;
  324. }
  325. /*
  326. * Can't parse this as a cache op or result, so back up
  327. * to the '-'.
  328. */
  329. --s;
  330. break;
  331. }
  332. /*
  333. * Fall back to reads:
  334. */
  335. if (cache_op == -1)
  336. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  337. /*
  338. * Fall back to accesses:
  339. */
  340. if (cache_result == -1)
  341. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  342. attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
  343. attr->type = PERF_TYPE_HW_CACHE;
  344. *str = s;
  345. return EVT_HANDLED;
  346. }
  347. static enum event_result
  348. parse_single_tracepoint_event(char *sys_name,
  349. const char *evt_name,
  350. unsigned int evt_length,
  351. char *flags,
  352. struct perf_event_attr *attr,
  353. const char **strp)
  354. {
  355. char evt_path[MAXPATHLEN];
  356. char id_buf[4];
  357. u64 id;
  358. int fd;
  359. if (flags) {
  360. if (!strncmp(flags, "record", strlen(flags))) {
  361. attr->sample_type |= PERF_SAMPLE_RAW;
  362. attr->sample_type |= PERF_SAMPLE_TIME;
  363. attr->sample_type |= PERF_SAMPLE_CPU;
  364. }
  365. }
  366. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  367. sys_name, evt_name);
  368. fd = open(evt_path, O_RDONLY);
  369. if (fd < 0)
  370. return EVT_FAILED;
  371. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  372. close(fd);
  373. return EVT_FAILED;
  374. }
  375. close(fd);
  376. id = atoll(id_buf);
  377. attr->config = id;
  378. attr->type = PERF_TYPE_TRACEPOINT;
  379. *strp = evt_name + evt_length;
  380. return EVT_HANDLED;
  381. }
  382. /* sys + ':' + event + ':' + flags*/
  383. #define MAX_EVOPT_LEN (MAX_EVENT_LENGTH * 2 + 2 + 128)
  384. static enum event_result
  385. parse_subsystem_tracepoint_event(char *sys_name, char *flags)
  386. {
  387. char evt_path[MAXPATHLEN];
  388. struct dirent *evt_ent;
  389. DIR *evt_dir;
  390. snprintf(evt_path, MAXPATHLEN, "%s/%s", debugfs_path, sys_name);
  391. evt_dir = opendir(evt_path);
  392. if (!evt_dir) {
  393. perror("Can't open event dir");
  394. return EVT_FAILED;
  395. }
  396. while ((evt_ent = readdir(evt_dir))) {
  397. char event_opt[MAX_EVOPT_LEN + 1];
  398. int len;
  399. unsigned int rem = MAX_EVOPT_LEN;
  400. if (!strcmp(evt_ent->d_name, ".")
  401. || !strcmp(evt_ent->d_name, "..")
  402. || !strcmp(evt_ent->d_name, "enable")
  403. || !strcmp(evt_ent->d_name, "filter"))
  404. continue;
  405. len = snprintf(event_opt, MAX_EVOPT_LEN, "%s:%s", sys_name,
  406. evt_ent->d_name);
  407. if (len < 0)
  408. return EVT_FAILED;
  409. rem -= len;
  410. if (flags) {
  411. if (rem < strlen(flags) + 1)
  412. return EVT_FAILED;
  413. strcat(event_opt, ":");
  414. strcat(event_opt, flags);
  415. }
  416. if (parse_events(NULL, event_opt, 0))
  417. return EVT_FAILED;
  418. }
  419. return EVT_HANDLED_ALL;
  420. }
  421. static enum event_result parse_tracepoint_event(const char **strp,
  422. struct perf_event_attr *attr)
  423. {
  424. const char *evt_name;
  425. char *flags;
  426. char sys_name[MAX_EVENT_LENGTH];
  427. unsigned int sys_length, evt_length;
  428. if (valid_debugfs_mount(debugfs_path))
  429. return 0;
  430. evt_name = strchr(*strp, ':');
  431. if (!evt_name)
  432. return EVT_FAILED;
  433. sys_length = evt_name - *strp;
  434. if (sys_length >= MAX_EVENT_LENGTH)
  435. return 0;
  436. strncpy(sys_name, *strp, sys_length);
  437. sys_name[sys_length] = '\0';
  438. evt_name = evt_name + 1;
  439. flags = strchr(evt_name, ':');
  440. if (flags) {
  441. /* split it out: */
  442. evt_name = strndup(evt_name, flags - evt_name);
  443. flags++;
  444. }
  445. evt_length = strlen(evt_name);
  446. if (evt_length >= MAX_EVENT_LENGTH)
  447. return EVT_FAILED;
  448. if (!strcmp(evt_name, "*")) {
  449. *strp = evt_name + evt_length;
  450. return parse_subsystem_tracepoint_event(sys_name, flags);
  451. } else
  452. return parse_single_tracepoint_event(sys_name, evt_name,
  453. evt_length, flags,
  454. attr, strp);
  455. }
  456. static int check_events(const char *str, unsigned int i)
  457. {
  458. int n;
  459. n = strlen(event_symbols[i].symbol);
  460. if (!strncmp(str, event_symbols[i].symbol, n))
  461. return n;
  462. n = strlen(event_symbols[i].alias);
  463. if (n)
  464. if (!strncmp(str, event_symbols[i].alias, n))
  465. return n;
  466. return 0;
  467. }
  468. static enum event_result
  469. parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
  470. {
  471. const char *str = *strp;
  472. unsigned int i;
  473. int n;
  474. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  475. n = check_events(str, i);
  476. if (n > 0) {
  477. attr->type = event_symbols[i].type;
  478. attr->config = event_symbols[i].config;
  479. *strp = str + n;
  480. return EVT_HANDLED;
  481. }
  482. }
  483. return EVT_FAILED;
  484. }
  485. static enum event_result
  486. parse_raw_event(const char **strp, struct perf_event_attr *attr)
  487. {
  488. const char *str = *strp;
  489. u64 config;
  490. int n;
  491. if (*str != 'r')
  492. return EVT_FAILED;
  493. n = hex2u64(str + 1, &config);
  494. if (n > 0) {
  495. *strp = str + n + 1;
  496. attr->type = PERF_TYPE_RAW;
  497. attr->config = config;
  498. return EVT_HANDLED;
  499. }
  500. return EVT_FAILED;
  501. }
  502. static enum event_result
  503. parse_numeric_event(const char **strp, struct perf_event_attr *attr)
  504. {
  505. const char *str = *strp;
  506. char *endp;
  507. unsigned long type;
  508. u64 config;
  509. type = strtoul(str, &endp, 0);
  510. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  511. str = endp + 1;
  512. config = strtoul(str, &endp, 0);
  513. if (endp > str) {
  514. attr->type = type;
  515. attr->config = config;
  516. *strp = endp;
  517. return EVT_HANDLED;
  518. }
  519. }
  520. return EVT_FAILED;
  521. }
  522. static enum event_result
  523. parse_event_modifier(const char **strp, struct perf_event_attr *attr)
  524. {
  525. const char *str = *strp;
  526. int eu = 1, ek = 1, eh = 1;
  527. if (*str++ != ':')
  528. return 0;
  529. while (*str) {
  530. if (*str == 'u')
  531. eu = 0;
  532. else if (*str == 'k')
  533. ek = 0;
  534. else if (*str == 'h')
  535. eh = 0;
  536. else
  537. break;
  538. ++str;
  539. }
  540. if (str >= *strp + 2) {
  541. *strp = str;
  542. attr->exclude_user = eu;
  543. attr->exclude_kernel = ek;
  544. attr->exclude_hv = eh;
  545. return 1;
  546. }
  547. return 0;
  548. }
  549. /*
  550. * Each event can have multiple symbolic names.
  551. * Symbolic names are (almost) exactly matched.
  552. */
  553. static enum event_result
  554. parse_event_symbols(const char **str, struct perf_event_attr *attr)
  555. {
  556. enum event_result ret;
  557. ret = parse_tracepoint_event(str, attr);
  558. if (ret != EVT_FAILED)
  559. goto modifier;
  560. ret = parse_raw_event(str, attr);
  561. if (ret != EVT_FAILED)
  562. goto modifier;
  563. ret = parse_numeric_event(str, attr);
  564. if (ret != EVT_FAILED)
  565. goto modifier;
  566. ret = parse_symbolic_event(str, attr);
  567. if (ret != EVT_FAILED)
  568. goto modifier;
  569. ret = parse_generic_hw_event(str, attr);
  570. if (ret != EVT_FAILED)
  571. goto modifier;
  572. return EVT_FAILED;
  573. modifier:
  574. parse_event_modifier(str, attr);
  575. return ret;
  576. }
  577. static void store_event_type(const char *orgname)
  578. {
  579. char filename[PATH_MAX], *c;
  580. FILE *file;
  581. int id;
  582. sprintf(filename, "/sys/kernel/debug/tracing/events/%s/id", orgname);
  583. c = strchr(filename, ':');
  584. if (c)
  585. *c = '/';
  586. file = fopen(filename, "r");
  587. if (!file)
  588. return;
  589. if (fscanf(file, "%i", &id) < 1)
  590. die("cannot store event ID");
  591. fclose(file);
  592. perf_header__push_event(id, orgname);
  593. }
  594. int parse_events(const struct option *opt __used, const char *str, int unset __used)
  595. {
  596. struct perf_event_attr attr;
  597. enum event_result ret;
  598. if (strchr(str, ':'))
  599. store_event_type(str);
  600. for (;;) {
  601. if (nr_counters == MAX_COUNTERS)
  602. return -1;
  603. memset(&attr, 0, sizeof(attr));
  604. ret = parse_event_symbols(&str, &attr);
  605. if (ret == EVT_FAILED)
  606. return -1;
  607. if (!(*str == 0 || *str == ',' || isspace(*str)))
  608. return -1;
  609. if (ret != EVT_HANDLED_ALL) {
  610. attrs[nr_counters] = attr;
  611. nr_counters++;
  612. }
  613. if (*str == 0)
  614. break;
  615. if (*str == ',')
  616. ++str;
  617. while (isspace(*str))
  618. ++str;
  619. }
  620. return 0;
  621. }
  622. static const char * const event_type_descriptors[] = {
  623. "",
  624. "Hardware event",
  625. "Software event",
  626. "Tracepoint event",
  627. "Hardware cache event",
  628. };
  629. /*
  630. * Print the events from <debugfs_mount_point>/tracing/events
  631. */
  632. static void print_tracepoint_events(void)
  633. {
  634. DIR *sys_dir, *evt_dir;
  635. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  636. int sys_dir_fd;
  637. char evt_path[MAXPATHLEN];
  638. if (valid_debugfs_mount(debugfs_path))
  639. return;
  640. sys_dir = opendir(debugfs_path);
  641. if (!sys_dir)
  642. goto cleanup;
  643. sys_dir_fd = dirfd(sys_dir);
  644. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  645. int dfd = openat(sys_dir_fd, sys_dirent.d_name,
  646. O_RDONLY|O_DIRECTORY), evt_dir_fd;
  647. if (dfd == -1)
  648. continue;
  649. evt_dir = fdopendir(dfd);
  650. if (!evt_dir) {
  651. close(dfd);
  652. continue;
  653. }
  654. evt_dir_fd = dirfd(evt_dir);
  655. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  656. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  657. sys_dirent.d_name, evt_dirent.d_name);
  658. fprintf(stderr, " %-42s [%s]\n", evt_path,
  659. event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
  660. }
  661. closedir(evt_dir);
  662. }
  663. cleanup:
  664. closedir(sys_dir);
  665. }
  666. /*
  667. * Print the help text for the event symbols:
  668. */
  669. void print_events(void)
  670. {
  671. struct event_symbol *syms = event_symbols;
  672. unsigned int i, type, op, prev_type = -1;
  673. char name[40];
  674. fprintf(stderr, "\n");
  675. fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
  676. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  677. type = syms->type + 1;
  678. if (type >= ARRAY_SIZE(event_type_descriptors))
  679. type = 0;
  680. if (type != prev_type)
  681. fprintf(stderr, "\n");
  682. if (strlen(syms->alias))
  683. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  684. else
  685. strcpy(name, syms->symbol);
  686. fprintf(stderr, " %-42s [%s]\n", name,
  687. event_type_descriptors[type]);
  688. prev_type = type;
  689. }
  690. fprintf(stderr, "\n");
  691. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  692. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  693. /* skip invalid cache type */
  694. if (!is_cache_op_valid(type, op))
  695. continue;
  696. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  697. fprintf(stderr, " %-42s [%s]\n",
  698. event_cache_name(type, op, i),
  699. event_type_descriptors[4]);
  700. }
  701. }
  702. }
  703. fprintf(stderr, "\n");
  704. fprintf(stderr, " %-42s [raw hardware event descriptor]\n",
  705. "rNNN");
  706. fprintf(stderr, "\n");
  707. print_tracepoint_events();
  708. exit(129);
  709. }