parse-events.c 20 KB

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