parse-events.c 19 KB

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