parse-events.c 15 KB

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