parse-events.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next, file, st) \
  104. while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
  105. if (snprintf(file, MAXPATHLEN, "%s/%s/%s", debugfs_path, \
  106. sys_dirent.d_name, evt_dirent.d_name) && \
  107. (!stat(file, &st)) && (S_ISDIR(st.st_mode)) && \
  108. (strcmp(evt_dirent.d_name, ".")) && \
  109. (strcmp(evt_dirent.d_name, "..")))
  110. #define MAX_EVENT_LENGTH 30
  111. int valid_debugfs_mount(const char *debugfs)
  112. {
  113. struct statfs st_fs;
  114. if (statfs(debugfs, &st_fs) < 0)
  115. return -ENOENT;
  116. else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
  117. return -ENOENT;
  118. return 0;
  119. }
  120. static char *tracepoint_id_to_name(u64 config)
  121. {
  122. static char tracepoint_name[2 * MAX_EVENT_LENGTH];
  123. DIR *sys_dir, *evt_dir;
  124. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  125. struct stat st;
  126. char id_buf[4];
  127. int fd;
  128. u64 id;
  129. char evt_path[MAXPATHLEN];
  130. if (valid_debugfs_mount(debugfs_path))
  131. return "unkown";
  132. sys_dir = opendir(debugfs_path);
  133. if (!sys_dir)
  134. goto cleanup;
  135. for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
  136. evt_dir = opendir(evt_path);
  137. if (!evt_dir)
  138. goto cleanup;
  139. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
  140. evt_path, st) {
  141. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id",
  142. debugfs_path, sys_dirent.d_name,
  143. evt_dirent.d_name);
  144. fd = open(evt_path, O_RDONLY);
  145. if (fd < 0)
  146. continue;
  147. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  148. close(fd);
  149. continue;
  150. }
  151. close(fd);
  152. id = atoll(id_buf);
  153. if (id == config) {
  154. closedir(evt_dir);
  155. closedir(sys_dir);
  156. snprintf(tracepoint_name, 2 * MAX_EVENT_LENGTH,
  157. "%s:%s", sys_dirent.d_name,
  158. evt_dirent.d_name);
  159. return tracepoint_name;
  160. }
  161. }
  162. closedir(evt_dir);
  163. }
  164. cleanup:
  165. closedir(sys_dir);
  166. return "unkown";
  167. }
  168. static int is_cache_op_valid(u8 cache_type, u8 cache_op)
  169. {
  170. if (hw_cache_stat[cache_type] & COP(cache_op))
  171. return 1; /* valid */
  172. else
  173. return 0; /* invalid */
  174. }
  175. static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
  176. {
  177. static char name[50];
  178. if (cache_result) {
  179. sprintf(name, "%s-%s-%s", hw_cache[cache_type][0],
  180. hw_cache_op[cache_op][0],
  181. hw_cache_result[cache_result][0]);
  182. } else {
  183. sprintf(name, "%s-%s", hw_cache[cache_type][0],
  184. hw_cache_op[cache_op][1]);
  185. }
  186. return name;
  187. }
  188. char *event_name(int counter)
  189. {
  190. u64 config = attrs[counter].config;
  191. int type = attrs[counter].type;
  192. static char buf[32];
  193. if (attrs[counter].type == PERF_TYPE_RAW) {
  194. sprintf(buf, "raw 0x%llx", config);
  195. return buf;
  196. }
  197. switch (type) {
  198. case PERF_TYPE_HARDWARE:
  199. if (config < PERF_COUNT_HW_MAX)
  200. return hw_event_names[config];
  201. return "unknown-hardware";
  202. case PERF_TYPE_HW_CACHE: {
  203. u8 cache_type, cache_op, cache_result;
  204. cache_type = (config >> 0) & 0xff;
  205. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  206. return "unknown-ext-hardware-cache-type";
  207. cache_op = (config >> 8) & 0xff;
  208. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  209. return "unknown-ext-hardware-cache-op";
  210. cache_result = (config >> 16) & 0xff;
  211. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  212. return "unknown-ext-hardware-cache-result";
  213. if (!is_cache_op_valid(cache_type, cache_op))
  214. return "invalid-cache";
  215. return event_cache_name(cache_type, cache_op, cache_result);
  216. }
  217. case PERF_TYPE_SOFTWARE:
  218. if (config < PERF_COUNT_SW_MAX)
  219. return sw_event_names[config];
  220. return "unknown-software";
  221. case PERF_TYPE_TRACEPOINT:
  222. return tracepoint_id_to_name(config);
  223. default:
  224. break;
  225. }
  226. return "unknown";
  227. }
  228. static int parse_aliases(const char **str, char *names[][MAX_ALIASES], int size)
  229. {
  230. int i, j;
  231. int n, longest = -1;
  232. for (i = 0; i < size; i++) {
  233. for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
  234. n = strlen(names[i][j]);
  235. if (n > longest && !strncasecmp(*str, names[i][j], n))
  236. longest = n;
  237. }
  238. if (longest > 0) {
  239. *str += longest;
  240. return i;
  241. }
  242. }
  243. return -1;
  244. }
  245. static int
  246. parse_generic_hw_event(const char **str, struct perf_counter_attr *attr)
  247. {
  248. const char *s = *str;
  249. int cache_type = -1, cache_op = -1, cache_result = -1;
  250. cache_type = parse_aliases(&s, hw_cache, PERF_COUNT_HW_CACHE_MAX);
  251. /*
  252. * No fallback - if we cannot get a clear cache type
  253. * then bail out:
  254. */
  255. if (cache_type == -1)
  256. return 0;
  257. while ((cache_op == -1 || cache_result == -1) && *s == '-') {
  258. ++s;
  259. if (cache_op == -1) {
  260. cache_op = parse_aliases(&s, hw_cache_op,
  261. PERF_COUNT_HW_CACHE_OP_MAX);
  262. if (cache_op >= 0) {
  263. if (!is_cache_op_valid(cache_type, cache_op))
  264. return 0;
  265. continue;
  266. }
  267. }
  268. if (cache_result == -1) {
  269. cache_result = parse_aliases(&s, hw_cache_result,
  270. PERF_COUNT_HW_CACHE_RESULT_MAX);
  271. if (cache_result >= 0)
  272. continue;
  273. }
  274. /*
  275. * Can't parse this as a cache op or result, so back up
  276. * to the '-'.
  277. */
  278. --s;
  279. break;
  280. }
  281. /*
  282. * Fall back to reads:
  283. */
  284. if (cache_op == -1)
  285. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  286. /*
  287. * Fall back to accesses:
  288. */
  289. if (cache_result == -1)
  290. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  291. attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
  292. attr->type = PERF_TYPE_HW_CACHE;
  293. *str = s;
  294. return 1;
  295. }
  296. static int parse_tracepoint_event(const char **strp,
  297. struct perf_counter_attr *attr)
  298. {
  299. const char *evt_name;
  300. char sys_name[MAX_EVENT_LENGTH];
  301. char id_buf[4];
  302. int fd;
  303. unsigned int sys_length, evt_length;
  304. u64 id;
  305. char evt_path[MAXPATHLEN];
  306. if (valid_debugfs_mount(debugfs_path))
  307. return 0;
  308. evt_name = strchr(*strp, ':');
  309. if (!evt_name)
  310. return 0;
  311. sys_length = evt_name - *strp;
  312. if (sys_length >= MAX_EVENT_LENGTH)
  313. return 0;
  314. strncpy(sys_name, *strp, sys_length);
  315. sys_name[sys_length] = '\0';
  316. evt_name = evt_name + 1;
  317. evt_length = strlen(evt_name);
  318. if (evt_length >= MAX_EVENT_LENGTH)
  319. return 0;
  320. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", debugfs_path,
  321. sys_name, evt_name);
  322. fd = open(evt_path, O_RDONLY);
  323. if (fd < 0)
  324. return 0;
  325. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  326. close(fd);
  327. return 0;
  328. }
  329. close(fd);
  330. id = atoll(id_buf);
  331. attr->config = id;
  332. attr->type = PERF_TYPE_TRACEPOINT;
  333. *strp = evt_name + evt_length;
  334. return 1;
  335. }
  336. static int check_events(const char *str, unsigned int i)
  337. {
  338. int n;
  339. n = strlen(event_symbols[i].symbol);
  340. if (!strncmp(str, event_symbols[i].symbol, n))
  341. return n;
  342. n = strlen(event_symbols[i].alias);
  343. if (n)
  344. if (!strncmp(str, event_symbols[i].alias, n))
  345. return n;
  346. return 0;
  347. }
  348. static int
  349. parse_symbolic_event(const char **strp, struct perf_counter_attr *attr)
  350. {
  351. const char *str = *strp;
  352. unsigned int i;
  353. int n;
  354. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  355. n = check_events(str, i);
  356. if (n > 0) {
  357. attr->type = event_symbols[i].type;
  358. attr->config = event_symbols[i].config;
  359. *strp = str + n;
  360. return 1;
  361. }
  362. }
  363. return 0;
  364. }
  365. static int parse_raw_event(const char **strp, struct perf_counter_attr *attr)
  366. {
  367. const char *str = *strp;
  368. u64 config;
  369. int n;
  370. if (*str != 'r')
  371. return 0;
  372. n = hex2u64(str + 1, &config);
  373. if (n > 0) {
  374. *strp = str + n + 1;
  375. attr->type = PERF_TYPE_RAW;
  376. attr->config = config;
  377. return 1;
  378. }
  379. return 0;
  380. }
  381. static int
  382. parse_numeric_event(const char **strp, struct perf_counter_attr *attr)
  383. {
  384. const char *str = *strp;
  385. char *endp;
  386. unsigned long type;
  387. u64 config;
  388. type = strtoul(str, &endp, 0);
  389. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  390. str = endp + 1;
  391. config = strtoul(str, &endp, 0);
  392. if (endp > str) {
  393. attr->type = type;
  394. attr->config = config;
  395. *strp = endp;
  396. return 1;
  397. }
  398. }
  399. return 0;
  400. }
  401. static int
  402. parse_event_modifier(const char **strp, struct perf_counter_attr *attr)
  403. {
  404. const char *str = *strp;
  405. int eu = 1, ek = 1, eh = 1;
  406. if (*str++ != ':')
  407. return 0;
  408. while (*str) {
  409. if (*str == 'u')
  410. eu = 0;
  411. else if (*str == 'k')
  412. ek = 0;
  413. else if (*str == 'h')
  414. eh = 0;
  415. else
  416. break;
  417. ++str;
  418. }
  419. if (str >= *strp + 2) {
  420. *strp = str;
  421. attr->exclude_user = eu;
  422. attr->exclude_kernel = ek;
  423. attr->exclude_hv = eh;
  424. return 1;
  425. }
  426. return 0;
  427. }
  428. /*
  429. * Each event can have multiple symbolic names.
  430. * Symbolic names are (almost) exactly matched.
  431. */
  432. static int parse_event_symbols(const char **str, struct perf_counter_attr *attr)
  433. {
  434. if (!(parse_tracepoint_event(str, attr) ||
  435. parse_raw_event(str, attr) ||
  436. parse_numeric_event(str, attr) ||
  437. parse_symbolic_event(str, attr) ||
  438. parse_generic_hw_event(str, attr)))
  439. return 0;
  440. parse_event_modifier(str, attr);
  441. return 1;
  442. }
  443. int parse_events(const struct option *opt __used, const char *str, int unset __used)
  444. {
  445. struct perf_counter_attr attr;
  446. for (;;) {
  447. if (nr_counters == MAX_COUNTERS)
  448. return -1;
  449. memset(&attr, 0, sizeof(attr));
  450. if (!parse_event_symbols(&str, &attr))
  451. return -1;
  452. if (!(*str == 0 || *str == ',' || isspace(*str)))
  453. return -1;
  454. attrs[nr_counters] = attr;
  455. nr_counters++;
  456. if (*str == 0)
  457. break;
  458. if (*str == ',')
  459. ++str;
  460. while (isspace(*str))
  461. ++str;
  462. }
  463. return 0;
  464. }
  465. static const char * const event_type_descriptors[] = {
  466. "",
  467. "Hardware event",
  468. "Software event",
  469. "Tracepoint event",
  470. "Hardware cache event",
  471. };
  472. /*
  473. * Print the events from <debugfs_mount_point>/tracing/events
  474. */
  475. static void print_tracepoint_events(void)
  476. {
  477. DIR *sys_dir, *evt_dir;
  478. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  479. struct stat st;
  480. char evt_path[MAXPATHLEN];
  481. if (valid_debugfs_mount(debugfs_path))
  482. return;
  483. sys_dir = opendir(debugfs_path);
  484. if (!sys_dir)
  485. goto cleanup;
  486. for_each_subsystem(sys_dir, sys_dirent, sys_next, evt_path, st) {
  487. evt_dir = opendir(evt_path);
  488. if (!evt_dir)
  489. goto cleanup;
  490. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next,
  491. evt_path, st) {
  492. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  493. sys_dirent.d_name, evt_dirent.d_name);
  494. fprintf(stderr, " %-40s [%s]\n", evt_path,
  495. event_type_descriptors[PERF_TYPE_TRACEPOINT+1]);
  496. }
  497. closedir(evt_dir);
  498. }
  499. cleanup:
  500. closedir(sys_dir);
  501. }
  502. /*
  503. * Print the help text for the event symbols:
  504. */
  505. void print_events(void)
  506. {
  507. struct event_symbol *syms = event_symbols;
  508. unsigned int i, type, op, prev_type = -1;
  509. char name[40];
  510. fprintf(stderr, "\n");
  511. fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
  512. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  513. type = syms->type + 1;
  514. if (type >= ARRAY_SIZE(event_type_descriptors))
  515. type = 0;
  516. if (type != prev_type)
  517. fprintf(stderr, "\n");
  518. if (strlen(syms->alias))
  519. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  520. else
  521. strcpy(name, syms->symbol);
  522. fprintf(stderr, " %-40s [%s]\n", name,
  523. event_type_descriptors[type]);
  524. prev_type = type;
  525. }
  526. fprintf(stderr, "\n");
  527. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  528. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  529. /* skip invalid cache type */
  530. if (!is_cache_op_valid(type, op))
  531. continue;
  532. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  533. fprintf(stderr, " %-40s [%s]\n",
  534. event_cache_name(type, op, i),
  535. event_type_descriptors[4]);
  536. }
  537. }
  538. }
  539. fprintf(stderr, "\n");
  540. fprintf(stderr, " %-40s [raw hardware event descriptor]\n",
  541. "rNNN");
  542. fprintf(stderr, "\n");
  543. print_tracepoint_events();
  544. exit(129);
  545. }