parse-events.c 19 KB

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