parse-events.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #include "../perf.h"
  2. #include "util.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. extern char *strcasestr(const char *haystack, const char *needle);
  9. int nr_counters;
  10. struct perf_counter_attr attrs[MAX_COUNTERS];
  11. struct event_symbol {
  12. u8 type;
  13. u64 config;
  14. char *symbol;
  15. char *alias;
  16. };
  17. char debugfs_path[MAXPATHLEN];
  18. #define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
  19. #define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
  20. static struct event_symbol event_symbols[] = {
  21. { CHW(CPU_CYCLES), "cpu-cycles", "cycles" },
  22. { CHW(INSTRUCTIONS), "instructions", "" },
  23. { CHW(CACHE_REFERENCES), "cache-references", "" },
  24. { CHW(CACHE_MISSES), "cache-misses", "" },
  25. { CHW(BRANCH_INSTRUCTIONS), "branch-instructions", "branches" },
  26. { CHW(BRANCH_MISSES), "branch-misses", "" },
  27. { CHW(BUS_CYCLES), "bus-cycles", "" },
  28. { CSW(CPU_CLOCK), "cpu-clock", "" },
  29. { CSW(TASK_CLOCK), "task-clock", "" },
  30. { CSW(PAGE_FAULTS), "page-faults", "faults" },
  31. { CSW(PAGE_FAULTS_MIN), "minor-faults", "" },
  32. { CSW(PAGE_FAULTS_MAJ), "major-faults", "" },
  33. { CSW(CONTEXT_SWITCHES), "context-switches", "cs" },
  34. { CSW(CPU_MIGRATIONS), "cpu-migrations", "migrations" },
  35. };
  36. #define __PERF_COUNTER_FIELD(config, name) \
  37. ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)
  38. #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW)
  39. #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG)
  40. #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE)
  41. #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT)
  42. static char *hw_event_names[] = {
  43. "cycles",
  44. "instructions",
  45. "cache-references",
  46. "cache-misses",
  47. "branches",
  48. "branch-misses",
  49. "bus-cycles",
  50. };
  51. static char *sw_event_names[] = {
  52. "cpu-clock-msecs",
  53. "task-clock-msecs",
  54. "page-faults",
  55. "context-switches",
  56. "CPU-migrations",
  57. "minor-faults",
  58. "major-faults",
  59. };
  60. #define MAX_ALIASES 8
  61. static char *hw_cache[][MAX_ALIASES] = {
  62. { "L1-dcache", "l1-d", "l1d", "L1-data", },
  63. { "L1-icache", "l1-i", "l1i", "L1-instruction", },
  64. { "LLC", "L2" },
  65. { "dTLB", "d-tlb", "Data-TLB", },
  66. { "iTLB", "i-tlb", "Instruction-TLB", },
  67. { "branch", "branches", "bpu", "btb", "bpc", },
  68. };
  69. static char *hw_cache_op[][MAX_ALIASES] = {
  70. { "load", "loads", "read", },
  71. { "store", "stores", "write", },
  72. { "prefetch", "prefetches", "speculative-read", "speculative-load", },
  73. };
  74. static char *hw_cache_result[][MAX_ALIASES] = {
  75. { "refs", "Reference", "ops", "access", },
  76. { "misses", "miss", },
  77. };
  78. #define C(x) PERF_COUNT_HW_CACHE_##x
  79. #define CACHE_READ (1 << C(OP_READ))
  80. #define CACHE_WRITE (1 << C(OP_WRITE))
  81. #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
  82. #define COP(x) (1 << x)
  83. /*
  84. * cache operartion stat
  85. * L1I : Read and prefetch only
  86. * ITLB and BPU : Read-only
  87. */
  88. static unsigned long hw_cache_stat[C(MAX)] = {
  89. [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  90. [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
  91. [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  92. [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
  93. [C(ITLB)] = (CACHE_READ),
  94. [C(BPU)] = (CACHE_READ),
  95. };
  96. #define for_each_subsystem(sys_dir, sys_dirent, sys_next, file, st) \
  97. while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
  98. if (snprintf(file, MAXPATHLEN, "%s/%s", debugfs_path, \
  99. sys_dirent.d_name) && \
  100. (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \
  101. (strcmp(sys_dirent.d_name, ".")) && \
  102. (strcmp(sys_dirent.d_name, "..")))
  103. static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
  104. {
  105. char evt_path[MAXPATHLEN];
  106. int fd;
  107. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  108. sys_dir->d_name, evt_dir->d_name);
  109. fd = open(evt_path, O_RDONLY);
  110. if (fd < 0)
  111. return -EINVAL;
  112. close(fd);
  113. return 0;
  114. }
  115. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \
  116. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  117. if (snprintf(file, MAXPATHLEN, "%s/%s/%s", debugfs_path, \
  118. sys_dirent.d_name, evt_dirent.d_name) && \
  119. (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \
  120. (strcmp(evt_dirent.d_name, ".")) && \
  121. (strcmp(evt_dirent.d_name, "..")) && \
  122. (!tp_event_has_id(&sys_dirent, &evt_dirent)))
  123. #define MAX_EVENT_LENGTH 30
  124. int valid_debugfs_mount(const char *debugfs)
  125. {
  126. struct statfs st_fs;
  127. if (statfs(debugfs, &st_fs) < 0)
  128. return -ENOENT;
  129. else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
  130. return -ENOENT;
  131. return 0;
  132. }
  133. static char *tracepoint_id_to_name(u64 config)
  134. {
  135. static char tracepoint_name[2 * MAX_EVENT_LENGTH];
  136. DIR *sys_dir, *evt_dir;
  137. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  138. struct stat st;
  139. char id_buf[4];
  140. int fd;
  141. u64 id;
  142. char evt_path[MAXPATHLEN];
  143. if (valid_debugfs_mount(debugfs_path))
  144. return "unkown";
  145. sys_dir = opendir(debugfs_path);
  146. if (!sys_dir)
  147. goto cleanup;
  148. for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
  149. evt_dir = opendir(evt_path);
  150. if (!evt_dir)
  151. goto cleanup;
  152. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
  153. evt_path, st) {
  154. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id",
  155. debugfs_path, sys_dirent.d_name,
  156. evt_dirent.d_name);
  157. fd = open(evt_path, O_RDONLY);
  158. if (fd < 0)
  159. continue;
  160. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  161. close(fd);
  162. continue;
  163. }
  164. close(fd);
  165. id = atoll(id_buf);
  166. if (id == config) {
  167. closedir(evt_dir);
  168. closedir(sys_dir);
  169. snprintf(tracepoint_name, 2 * MAX_EVENT_LENGTH,
  170. "%s:%s", sys_dirent.d_name,
  171. evt_dirent.d_name);
  172. return tracepoint_name;
  173. }
  174. }
  175. closedir(evt_dir);
  176. }
  177. cleanup:
  178. closedir(sys_dir);
  179. return "unkown";
  180. }
  181. static int is_cache_op_valid(u8 cache_type, u8 cache_op)
  182. {
  183. if (hw_cache_stat[cache_type] & COP(cache_op))
  184. return 1; /* valid */
  185. else
  186. return 0; /* invalid */
  187. }
  188. static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
  189. {
  190. static char name[50];
  191. if (cache_result) {
  192. sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
  193. hw_cache_op[cache_op][0],
  194. hw_cache_result[cache_result][0]);
  195. } else {
  196. sprintf(name, "%s-%s", hw_cache[cache_type][0],
  197. hw_cache_op[cache_op][1]);
  198. }
  199. return name;
  200. }
  201. char *event_name(int counter)
  202. {
  203. u64 config = attrs[counter].config;
  204. int type = attrs[counter].type;
  205. return __event_name(type, config);
  206. }
  207. char *__event_name(int type, u64 config)
  208. {
  209. static char buf[32];
  210. if (type == PERF_TYPE_RAW) {
  211. sprintf(buf, "raw 0x%llx", config);
  212. return buf;
  213. }
  214. switch (type) {
  215. case PERF_TYPE_HARDWARE:
  216. if (config < PERF_COUNT_HW_MAX)
  217. return hw_event_names[config];
  218. return "unknown-hardware";
  219. case PERF_TYPE_HW_CACHE: {
  220. u8 cache_type, cache_op, cache_result;
  221. cache_type = (config >> 0) & 0xff;
  222. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  223. return "unknown-ext-hardware-cache-type";
  224. cache_op = (config >> 8) & 0xff;
  225. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  226. return "unknown-ext-hardware-cache-op";
  227. cache_result = (config >> 16) & 0xff;
  228. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  229. return "unknown-ext-hardware-cache-result";
  230. if (!is_cache_op_valid(cache_type, cache_op))
  231. return "invalid-cache";
  232. return event_cache_name(cache_type, cache_op, cache_result);
  233. }
  234. case PERF_TYPE_SOFTWARE:
  235. if (config < PERF_COUNT_SW_MAX)
  236. return sw_event_names[config];
  237. return "unknown-software";
  238. case PERF_TYPE_TRACEPOINT:
  239. return tracepoint_id_to_name(config);
  240. default:
  241. break;
  242. }
  243. return "unknown";
  244. }
  245. static int parse_aliases(const char **str, char *names[][MAX_ALIASES], int size)
  246. {
  247. int i, j;
  248. int n, longest = -1;
  249. for (i = 0; i < size; i++) {
  250. for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
  251. n = strlen(names[i][j]);
  252. if (n > longest && !strncasecmp(*str, names[i][j], n))
  253. longest = n;
  254. }
  255. if (longest > 0) {
  256. *str += longest;
  257. return i;
  258. }
  259. }
  260. return -1;
  261. }
  262. static int
  263. parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
  264. {
  265. const char *s = *str;
  266. int cache_type = -1, cache_op = -1, cache_result = -1;
  267. cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
  268. /*
  269. * No fallback - if we cannot get a clear cache type
  270. * then bail out:
  271. */
  272. if (cache_type == -1)
  273. return 0;
  274. while ((cache_op == -1 || cache_result == -1) && *s == '-') {
  275. ++s;
  276. if (cache_op == -1) {
  277. cache_op = parse_aliases(&s, hw_cache_op,
  278. PERF_COUNT_HW_CACHE_OP_MAX);
  279. if (cache_op >= 0) {
  280. if (!is_cache_op_valid(cache_type, cache_op))
  281. return 0;
  282. continue;
  283. }
  284. }
  285. if (cache_result == -1) {
  286. cache_result = parse_aliases(&s, hw_cache_result,
  287. PERF_COUNT_HW_CACHE_RESULT_MAX);
  288. if (cache_result >= 0)
  289. continue;
  290. }
  291. /*
  292. * Can't parse this as a cache op or result, so back up
  293. * to the '-'.
  294. */
  295. --s;
  296. break;
  297. }
  298. /*
  299. * Fall back to reads:
  300. */
  301. if (cache_op == -1)
  302. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  303. /*
  304. * Fall back to accesses:
  305. */
  306. if (cache_result == -1)
  307. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  308. attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
  309. attr->type = PERF_TYPE_HW_CACHE;
  310. *str = s;
  311. return 1;
  312. }
  313. static int parse_tracepoint_event(const char **strp,
  314. struct perf_counter_attr *attr)
  315. {
  316. const char *evt_name;
  317. char sys_name[MAX_EVENT_LENGTH];
  318. char id_buf[4];
  319. int fd;
  320. unsigned int sys_length, evt_length;
  321. u64 id;
  322. char evt_path[MAXPATHLEN];
  323. if (valid_debugfs_mount(debugfs_path))
  324. return 0;
  325. evt_name = strchr(*strp, ':');
  326. if (!evt_name)
  327. return 0;
  328. sys_length = evt_name - *strp;
  329. if (sys_length >= MAX_EVENT_LENGTH)
  330. return 0;
  331. strncpy(sys_name, *strp, sys_length);
  332. sys_name[sys_length] = '\0';
  333. evt_name = evt_name + 1;
  334. evt_length = strlen(evt_name);
  335. if (evt_length >= MAX_EVENT_LENGTH)
  336. return 0;
  337. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  338. sys_name, evt_name);
  339. fd = open(evt_path, O_RDONLY);
  340. if (fd < 0)
  341. return 0;
  342. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  343. close(fd);
  344. return 0;
  345. }
  346. close(fd);
  347. id = atoll(id_buf);
  348. attr->config = id;
  349. attr->type = PERF_TYPE_TRACEPOINT;
  350. *strp = evt_name + evt_length;
  351. return 1;
  352. }
  353. static int check_events(const char *str, unsigned int i)
  354. {
  355. int n;
  356. n = strlen(event_symbols[i].symbol);
  357. if (!strncmp(str, event_symbols[i].symbol, n))
  358. return n;
  359. n = strlen(event_symbols[i].alias);
  360. if (n)
  361. if (!strncmp(str, event_symbols[i].alias, n))
  362. return n;
  363. return 0;
  364. }
  365. static int
  366. parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
  367. {
  368. const char *str = *strp;
  369. unsigned int i;
  370. int n;
  371. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  372. n = check_events(str, i);
  373. if (n > 0) {
  374. attr->type = event_symbols[i].type;
  375. attr->config = event_symbols[i].config;
  376. *strp = str + n;
  377. return 1;
  378. }
  379. }
  380. return 0;
  381. }
  382. static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
  383. {
  384. const char *str = *strp;
  385. u64 config;
  386. int n;
  387. if (*str != 'r')
  388. return 0;
  389. n = hex2u64(str + 1, &config);
  390. if (n > 0) {
  391. *strp = str + n + 1;
  392. attr->type = PERF_TYPE_RAW;
  393. attr->config = config;
  394. return 1;
  395. }
  396. return 0;
  397. }
  398. static int
  399. parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
  400. {
  401. const char *str = *strp;
  402. char *endp;
  403. unsigned long type;
  404. u64 config;
  405. type = strtoul(str, &endp, 0);
  406. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  407. str = endp + 1;
  408. config = strtoul(str, &endp, 0);
  409. if (endp > str) {
  410. attr->type = type;
  411. attr->config = config;
  412. *strp = endp;
  413. return 1;
  414. }
  415. }
  416. return 0;
  417. }
  418. static int
  419. parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
  420. {
  421. const char *str = *strp;
  422. int eu = 1, ek = 1, eh = 1;
  423. if (*str++ != ':')
  424. return 0;
  425. while (*str) {
  426. if (*str == 'u')
  427. eu = 0;
  428. else if (*str == 'k')
  429. ek = 0;
  430. else if (*str == 'h')
  431. eh = 0;
  432. else
  433. break;
  434. ++str;
  435. }
  436. if (str >= *strp + 2) {
  437. *strp = str;
  438. attr->exclude_user = eu;
  439. attr->exclude_kernel = ek;
  440. attr->exclude_hv = eh;
  441. return 1;
  442. }
  443. return 0;
  444. }
  445. /*
  446. * Each event can have multiple symbolic names.
  447. * Symbolic names are (almost) exactly matched.
  448. */
  449. static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
  450. {
  451. if (!(parse_tracepoint_event(str, attr) ||
  452. parse_raw_event(str, attr) ||
  453. parse_numeric_event(str, attr) ||
  454. parse_symbolic_event(str, attr) ||
  455. parse_generic_hw_event(str, attr)))
  456. return 0;
  457. parse_event_modifier(str, attr);
  458. return 1;
  459. }
  460. int parse_events(const struct option *opt __used, const char *str, int unset __used)
  461. {
  462. struct perf_counter_attr attr;
  463. for (;;) {
  464. if (nr_counters == MAX_COUNTERS)
  465. return -1;
  466. memset(&attr, 0, sizeof(attr));
  467. if (!parse_event_symbols(&str, &attr))
  468. return -1;
  469. if (!(*str == 0 || *str == ',' || isspace(*str)))
  470. return -1;
  471. attrs[nr_counters] = attr;
  472. nr_counters++;
  473. if (*str == 0)
  474. break;
  475. if (*str == ',')
  476. ++str;
  477. while (isspace(*str))
  478. ++str;
  479. }
  480. return 0;
  481. }
  482. static const char * const event_type_descriptors[] = {
  483. "",
  484. "Hardware event",
  485. "Software event",
  486. "Tracepoint event",
  487. "Hardware cache event",
  488. };
  489. /*
  490. * Print the events from <debugfs_mount_point>/tracing/events
  491. */
  492. static void print_tracepoint_events(void)
  493. {
  494. DIR *sys_dir, *evt_dir;
  495. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  496. struct stat st;
  497. char evt_path[MAXPATHLEN];
  498. if (valid_debugfs_mount(debugfs_path))
  499. return;
  500. sys_dir = opendir(debugfs_path);
  501. if (!sys_dir)
  502. goto cleanup;
  503. for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
  504. evt_dir = opendir(evt_path);
  505. if (!evt_dir)
  506. goto cleanup;
  507. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
  508. evt_path, st) {
  509. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  510. sys_dirent.d_name, evt_dirent.d_name);
  511. fprintf(stderr, " %-40s [%s]\n", evt_path,
  512. event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
  513. }
  514. closedir(evt_dir);
  515. }
  516. cleanup:
  517. closedir(sys_dir);
  518. }
  519. /*
  520. * Print the help text for the event symbols:
  521. */
  522. void print_events(void)
  523. {
  524. struct event_symbol *syms = event_symbols;
  525. unsigned int i, type, op, prev_type = -1;
  526. char name[40];
  527. fprintf(stderr, "\n");
  528. fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
  529. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  530. type = syms->type + 1;
  531. if (type >= ARRAY_SIZE(event_type_descriptors))
  532. type = 0;
  533. if (type != prev_type)
  534. fprintf(stderr, "\n");
  535. if (strlen(syms->alias))
  536. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  537. else
  538. strcpy(name, syms->symbol);
  539. fprintf(stderr, " %-40s [%s]\n", name,
  540. event_type_descriptors[type]);
  541. prev_type = type;
  542. }
  543. fprintf(stderr, "\n");
  544. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  545. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  546. /* skip invalid cache type */
  547. if (!is_cache_op_valid(type, op))
  548. continue;
  549. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  550. fprintf(stderr, " %-40s [%s]\n",
  551. event_cache_name(type, op, i),
  552. event_type_descriptors[4]);
  553. }
  554. }
  555. }
  556. fprintf(stderr, "\n");
  557. fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
  558. "rNNN");
  559. fprintf(stderr, "\n");
  560. print_tracepoint_events();
  561. exit(129);
  562. }