parse-events.c 19 KB

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