parse-events.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 *flags;
  318. char sys_name[MAX_EVENT_LENGTH];
  319. char id_buf[4];
  320. int fd;
  321. unsigned int sys_length, evt_length;
  322. u64 id;
  323. char evt_path[MAXPATHLEN];
  324. if (valid_debugfs_mount(debugfs_path))
  325. return 0;
  326. evt_name = strchr(*strp, ':');
  327. if (!evt_name)
  328. return 0;
  329. sys_length = evt_name - *strp;
  330. if (sys_length >= MAX_EVENT_LENGTH)
  331. return 0;
  332. strncpy(sys_name, *strp, sys_length);
  333. sys_name[sys_length] = '\0';
  334. evt_name = evt_name + 1;
  335. flags = strchr(evt_name, ':');
  336. if (flags) {
  337. *flags = '\0';
  338. flags++;
  339. if (!strncmp(flags, "record", strlen(flags)))
  340. attr->sample_type |= PERF_SAMPLE_RAW;
  341. }
  342. evt_length = strlen(evt_name);
  343. if (evt_length >= MAX_EVENT_LENGTH)
  344. return 0;
  345. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  346. sys_name, evt_name);
  347. fd = open(evt_path, O_RDONLY);
  348. if (fd < 0)
  349. return 0;
  350. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  351. close(fd);
  352. return 0;
  353. }
  354. close(fd);
  355. id = atoll(id_buf);
  356. attr->config = id;
  357. attr->type = PERF_TYPE_TRACEPOINT;
  358. *strp = evt_name + evt_length;
  359. return 1;
  360. }
  361. static int check_events(const char *str, unsigned int i)
  362. {
  363. int n;
  364. n = strlen(event_symbols[i].symbol);
  365. if (!strncmp(str, event_symbols[i].symbol, n))
  366. return n;
  367. n = strlen(event_symbols[i].alias);
  368. if (n)
  369. if (!strncmp(str, event_symbols[i].alias, n))
  370. return n;
  371. return 0;
  372. }
  373. static int
  374. parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
  375. {
  376. const char *str = *strp;
  377. unsigned int i;
  378. int n;
  379. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  380. n = check_events(str, i);
  381. if (n > 0) {
  382. attr->type = event_symbols[i].type;
  383. attr->config = event_symbols[i].config;
  384. *strp = str + n;
  385. return 1;
  386. }
  387. }
  388. return 0;
  389. }
  390. static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
  391. {
  392. const char *str = *strp;
  393. u64 config;
  394. int n;
  395. if (*str != 'r')
  396. return 0;
  397. n = hex2u64(str + 1, &config);
  398. if (n > 0) {
  399. *strp = str + n + 1;
  400. attr->type = PERF_TYPE_RAW;
  401. attr->config = config;
  402. return 1;
  403. }
  404. return 0;
  405. }
  406. static int
  407. parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
  408. {
  409. const char *str = *strp;
  410. char *endp;
  411. unsigned long type;
  412. u64 config;
  413. type = strtoul(str, &endp, 0);
  414. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  415. str = endp + 1;
  416. config = strtoul(str, &endp, 0);
  417. if (endp > str) {
  418. attr->type = type;
  419. attr->config = config;
  420. *strp = endp;
  421. return 1;
  422. }
  423. }
  424. return 0;
  425. }
  426. static int
  427. parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
  428. {
  429. const char *str = *strp;
  430. int eu = 1, ek = 1, eh = 1;
  431. if (*str++ != ':')
  432. return 0;
  433. while (*str) {
  434. if (*str == 'u')
  435. eu = 0;
  436. else if (*str == 'k')
  437. ek = 0;
  438. else if (*str == 'h')
  439. eh = 0;
  440. else
  441. break;
  442. ++str;
  443. }
  444. if (str >= *strp + 2) {
  445. *strp = str;
  446. attr->exclude_user = eu;
  447. attr->exclude_kernel = ek;
  448. attr->exclude_hv = eh;
  449. return 1;
  450. }
  451. return 0;
  452. }
  453. /*
  454. * Each event can have multiple symbolic names.
  455. * Symbolic names are (almost) exactly matched.
  456. */
  457. static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
  458. {
  459. if (!(parse_tracepoint_event(str, attr) ||
  460. parse_raw_event(str, attr) ||
  461. parse_numeric_event(str, attr) ||
  462. parse_symbolic_event(str, attr) ||
  463. parse_generic_hw_event(str, attr)))
  464. return 0;
  465. parse_event_modifier(str, attr);
  466. return 1;
  467. }
  468. int parse_events(const struct option *opt __used, const char *str, int unset __used)
  469. {
  470. struct perf_counter_attr attr;
  471. for (;;) {
  472. if (nr_counters == MAX_COUNTERS)
  473. return -1;
  474. memset(&attr, 0, sizeof(attr));
  475. if (!parse_event_symbols(&str, &attr))
  476. return -1;
  477. if (!(*str == 0 || *str == ',' || isspace(*str)))
  478. return -1;
  479. attrs[nr_counters] = attr;
  480. nr_counters++;
  481. if (*str == 0)
  482. break;
  483. if (*str == ',')
  484. ++str;
  485. while (isspace(*str))
  486. ++str;
  487. }
  488. return 0;
  489. }
  490. static const char * const event_type_descriptors[] = {
  491. "",
  492. "Hardware event",
  493. "Software event",
  494. "Tracepoint event",
  495. "Hardware cache event",
  496. };
  497. /*
  498. * Print the events from <debugfs_mount_point>/tracing/events
  499. */
  500. static void print_tracepoint_events(void)
  501. {
  502. DIR *sys_dir, *evt_dir;
  503. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  504. struct stat st;
  505. char evt_path[MAXPATHLEN];
  506. if (valid_debugfs_mount(debugfs_path))
  507. return;
  508. sys_dir = opendir(debugfs_path);
  509. if (!sys_dir)
  510. goto cleanup;
  511. for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
  512. evt_dir = opendir(evt_path);
  513. if (!evt_dir)
  514. goto cleanup;
  515. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
  516. evt_path, st) {
  517. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  518. sys_dirent.d_name, evt_dirent.d_name);
  519. fprintf(stderr, " %-40s [%s]\n", evt_path,
  520. event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
  521. }
  522. closedir(evt_dir);
  523. }
  524. cleanup:
  525. closedir(sys_dir);
  526. }
  527. /*
  528. * Print the help text for the event symbols:
  529. */
  530. void print_events(void)
  531. {
  532. struct event_symbol *syms = event_symbols;
  533. unsigned int i, type, op, prev_type = -1;
  534. char name[40];
  535. fprintf(stderr, "\n");
  536. fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
  537. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  538. type = syms->type + 1;
  539. if (type >= ARRAY_SIZE(event_type_descriptors))
  540. type = 0;
  541. if (type != prev_type)
  542. fprintf(stderr, "\n");
  543. if (strlen(syms->alias))
  544. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  545. else
  546. strcpy(name, syms->symbol);
  547. fprintf(stderr, " %-40s [%s]\n", name,
  548. event_type_descriptors[type]);
  549. prev_type = type;
  550. }
  551. fprintf(stderr, "\n");
  552. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  553. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  554. /* skip invalid cache type */
  555. if (!is_cache_op_valid(type, op))
  556. continue;
  557. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  558. fprintf(stderr, " %-40s [%s]\n",
  559. event_cache_name(type, op, i),
  560. event_type_descriptors[4]);
  561. }
  562. }
  563. }
  564. fprintf(stderr, "\n");
  565. fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
  566. "rNNN");
  567. fprintf(stderr, "\n");
  568. print_tracepoint_events();
  569. exit(129);
  570. }