parse-events.c 19 KB

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