parse-events.c 23 KB

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