parse-events.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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%" PRIx64, 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 enum event_result parse_tracepoint_event(const char **strp,
  407. struct perf_event_attr *attr)
  408. {
  409. const char *evt_name;
  410. char *flags = NULL, *comma_loc;
  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. comma_loc = strchr(evt_name, ',');
  425. if (comma_loc) {
  426. /* take the event name up to the comma */
  427. evt_name = strndup(evt_name, comma_loc - evt_name);
  428. }
  429. flags = strchr(evt_name, ':');
  430. if (flags) {
  431. /* split it out: */
  432. evt_name = strndup(evt_name, flags - evt_name);
  433. flags++;
  434. }
  435. evt_length = strlen(evt_name);
  436. if (evt_length >= MAX_EVENT_LENGTH)
  437. return EVT_FAILED;
  438. if (strpbrk(evt_name, "*?")) {
  439. *strp += strlen(sys_name) + evt_length + 1; /* 1 == the ':' */
  440. return parse_multiple_tracepoint_event(sys_name, evt_name,
  441. flags);
  442. } else {
  443. return parse_single_tracepoint_event(sys_name, evt_name,
  444. evt_length, attr, strp);
  445. }
  446. }
  447. static enum event_result
  448. parse_breakpoint_type(const char *type, const char **strp,
  449. struct perf_event_attr *attr)
  450. {
  451. int i;
  452. for (i = 0; i < 3; i++) {
  453. if (!type[i])
  454. break;
  455. switch (type[i]) {
  456. case 'r':
  457. attr->bp_type |= HW_BREAKPOINT_R;
  458. break;
  459. case 'w':
  460. attr->bp_type |= HW_BREAKPOINT_W;
  461. break;
  462. case 'x':
  463. attr->bp_type |= HW_BREAKPOINT_X;
  464. break;
  465. default:
  466. return EVT_FAILED;
  467. }
  468. }
  469. if (!attr->bp_type) /* Default */
  470. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  471. *strp = type + i;
  472. return EVT_HANDLED;
  473. }
  474. static enum event_result
  475. parse_breakpoint_event(const char **strp, struct perf_event_attr *attr)
  476. {
  477. const char *target;
  478. const char *type;
  479. char *endaddr;
  480. u64 addr;
  481. enum event_result err;
  482. target = strchr(*strp, ':');
  483. if (!target)
  484. return EVT_FAILED;
  485. if (strncmp(*strp, "mem", target - *strp) != 0)
  486. return EVT_FAILED;
  487. target++;
  488. addr = strtoull(target, &endaddr, 0);
  489. if (target == endaddr)
  490. return EVT_FAILED;
  491. attr->bp_addr = addr;
  492. *strp = endaddr;
  493. type = strchr(target, ':');
  494. /* If no type is defined, just rw as default */
  495. if (!type) {
  496. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  497. } else {
  498. err = parse_breakpoint_type(++type, strp, attr);
  499. if (err == EVT_FAILED)
  500. return EVT_FAILED;
  501. }
  502. /*
  503. * We should find a nice way to override the access length
  504. * Provide some defaults for now
  505. */
  506. if (attr->bp_type == HW_BREAKPOINT_X)
  507. attr->bp_len = sizeof(long);
  508. else
  509. attr->bp_len = HW_BREAKPOINT_LEN_4;
  510. attr->type = PERF_TYPE_BREAKPOINT;
  511. return EVT_HANDLED;
  512. }
  513. static int check_events(const char *str, unsigned int i)
  514. {
  515. int n;
  516. n = strlen(event_symbols[i].symbol);
  517. if (!strncmp(str, event_symbols[i].symbol, n))
  518. return n;
  519. n = strlen(event_symbols[i].alias);
  520. if (n)
  521. if (!strncmp(str, event_symbols[i].alias, n))
  522. return n;
  523. return 0;
  524. }
  525. static enum event_result
  526. parse_symbolic_event(const char **strp, struct perf_event_attr *attr)
  527. {
  528. const char *str = *strp;
  529. unsigned int i;
  530. int n;
  531. for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
  532. n = check_events(str, i);
  533. if (n > 0) {
  534. attr->type = event_symbols[i].type;
  535. attr->config = event_symbols[i].config;
  536. *strp = str + n;
  537. return EVT_HANDLED;
  538. }
  539. }
  540. return EVT_FAILED;
  541. }
  542. static enum event_result
  543. parse_raw_event(const char **strp, struct perf_event_attr *attr)
  544. {
  545. const char *str = *strp;
  546. u64 config;
  547. int n;
  548. if (*str != 'r')
  549. return EVT_FAILED;
  550. n = hex2u64(str + 1, &config);
  551. if (n > 0) {
  552. *strp = str + n + 1;
  553. attr->type = PERF_TYPE_RAW;
  554. attr->config = config;
  555. return EVT_HANDLED;
  556. }
  557. return EVT_FAILED;
  558. }
  559. static enum event_result
  560. parse_numeric_event(const char **strp, struct perf_event_attr *attr)
  561. {
  562. const char *str = *strp;
  563. char *endp;
  564. unsigned long type;
  565. u64 config;
  566. type = strtoul(str, &endp, 0);
  567. if (endp > str && type < PERF_TYPE_MAX && *endp == ':') {
  568. str = endp + 1;
  569. config = strtoul(str, &endp, 0);
  570. if (endp > str) {
  571. attr->type = type;
  572. attr->config = config;
  573. *strp = endp;
  574. return EVT_HANDLED;
  575. }
  576. }
  577. return EVT_FAILED;
  578. }
  579. static enum event_result
  580. parse_event_modifier(const char **strp, struct perf_event_attr *attr)
  581. {
  582. const char *str = *strp;
  583. int exclude = 0;
  584. int eu = 0, ek = 0, eh = 0, precise = 0;
  585. if (*str++ != ':')
  586. return 0;
  587. while (*str) {
  588. if (*str == 'u') {
  589. if (!exclude)
  590. exclude = eu = ek = eh = 1;
  591. eu = 0;
  592. } else if (*str == 'k') {
  593. if (!exclude)
  594. exclude = eu = ek = eh = 1;
  595. ek = 0;
  596. } else if (*str == 'h') {
  597. if (!exclude)
  598. exclude = eu = ek = eh = 1;
  599. eh = 0;
  600. } else if (*str == 'p') {
  601. precise++;
  602. } else
  603. break;
  604. ++str;
  605. }
  606. if (str >= *strp + 2) {
  607. *strp = str;
  608. attr->exclude_user = eu;
  609. attr->exclude_kernel = ek;
  610. attr->exclude_hv = eh;
  611. attr->precise_ip = precise;
  612. return 1;
  613. }
  614. return 0;
  615. }
  616. /*
  617. * Each event can have multiple symbolic names.
  618. * Symbolic names are (almost) exactly matched.
  619. */
  620. static enum event_result
  621. parse_event_symbols(const char **str, struct perf_event_attr *attr)
  622. {
  623. enum event_result ret;
  624. ret = parse_tracepoint_event(str, attr);
  625. if (ret != EVT_FAILED)
  626. goto modifier;
  627. ret = parse_raw_event(str, attr);
  628. if (ret != EVT_FAILED)
  629. goto modifier;
  630. ret = parse_numeric_event(str, attr);
  631. if (ret != EVT_FAILED)
  632. goto modifier;
  633. ret = parse_symbolic_event(str, attr);
  634. if (ret != EVT_FAILED)
  635. goto modifier;
  636. ret = parse_generic_hw_event(str, attr);
  637. if (ret != EVT_FAILED)
  638. goto modifier;
  639. ret = parse_breakpoint_event(str, attr);
  640. if (ret != EVT_FAILED)
  641. goto modifier;
  642. fprintf(stderr, "invalid or unsupported event: '%s'\n", *str);
  643. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  644. return EVT_FAILED;
  645. modifier:
  646. parse_event_modifier(str, attr);
  647. return ret;
  648. }
  649. int parse_events(const struct option *opt __used, const char *str, int unset __used)
  650. {
  651. struct perf_event_attr attr;
  652. enum event_result ret;
  653. for (;;) {
  654. memset(&attr, 0, sizeof(attr));
  655. ret = parse_event_symbols(&str, &attr);
  656. if (ret == EVT_FAILED)
  657. return -1;
  658. if (!(*str == 0 || *str == ',' || isspace(*str)))
  659. return -1;
  660. if (ret != EVT_HANDLED_ALL) {
  661. struct perf_evsel *evsel;
  662. evsel = perf_evsel__new(&attr,
  663. nr_counters);
  664. if (evsel == NULL)
  665. return -1;
  666. list_add_tail(&evsel->node, &evsel_list);
  667. ++nr_counters;
  668. }
  669. if (*str == 0)
  670. break;
  671. if (*str == ',')
  672. ++str;
  673. while (isspace(*str))
  674. ++str;
  675. }
  676. return 0;
  677. }
  678. int parse_filter(const struct option *opt __used, const char *str,
  679. int unset __used)
  680. {
  681. struct perf_evsel *last = NULL;
  682. if (!list_empty(&evsel_list))
  683. last = list_entry(evsel_list.prev, struct perf_evsel, node);
  684. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  685. fprintf(stderr,
  686. "-F option should follow a -e tracepoint option\n");
  687. return -1;
  688. }
  689. last->filter = strdup(str);
  690. if (last->filter == NULL) {
  691. fprintf(stderr, "not enough memory to hold filter string\n");
  692. return -1;
  693. }
  694. return 0;
  695. }
  696. static const char * const event_type_descriptors[] = {
  697. "Hardware event",
  698. "Software event",
  699. "Tracepoint event",
  700. "Hardware cache event",
  701. "Raw hardware event descriptor",
  702. "Hardware breakpoint",
  703. };
  704. /*
  705. * Print the events from <debugfs_mount_point>/tracing/events
  706. */
  707. static void print_tracepoint_events(void)
  708. {
  709. DIR *sys_dir, *evt_dir;
  710. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  711. char evt_path[MAXPATHLEN];
  712. char dir_path[MAXPATHLEN];
  713. if (debugfs_valid_mountpoint(debugfs_path))
  714. return;
  715. sys_dir = opendir(debugfs_path);
  716. if (!sys_dir)
  717. return;
  718. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  719. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  720. sys_dirent.d_name);
  721. evt_dir = opendir(dir_path);
  722. if (!evt_dir)
  723. continue;
  724. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  725. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  726. sys_dirent.d_name, evt_dirent.d_name);
  727. printf(" %-42s [%s]\n", evt_path,
  728. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  729. }
  730. closedir(evt_dir);
  731. }
  732. closedir(sys_dir);
  733. }
  734. /*
  735. * Check whether event is in <debugfs_mount_point>/tracing/events
  736. */
  737. int is_valid_tracepoint(const char *event_string)
  738. {
  739. DIR *sys_dir, *evt_dir;
  740. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  741. char evt_path[MAXPATHLEN];
  742. char dir_path[MAXPATHLEN];
  743. if (debugfs_valid_mountpoint(debugfs_path))
  744. return 0;
  745. sys_dir = opendir(debugfs_path);
  746. if (!sys_dir)
  747. return 0;
  748. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  749. snprintf(dir_path, MAXPATHLEN, "%s/%s", debugfs_path,
  750. sys_dirent.d_name);
  751. evt_dir = opendir(dir_path);
  752. if (!evt_dir)
  753. continue;
  754. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  755. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  756. sys_dirent.d_name, evt_dirent.d_name);
  757. if (!strcmp(evt_path, event_string)) {
  758. closedir(evt_dir);
  759. closedir(sys_dir);
  760. return 1;
  761. }
  762. }
  763. closedir(evt_dir);
  764. }
  765. closedir(sys_dir);
  766. return 0;
  767. }
  768. /*
  769. * Print the help text for the event symbols:
  770. */
  771. void print_events(void)
  772. {
  773. struct event_symbol *syms = event_symbols;
  774. unsigned int i, type, op, prev_type = -1;
  775. char name[40];
  776. printf("\n");
  777. printf("List of pre-defined events (to be used in -e):\n");
  778. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  779. type = syms->type;
  780. if (type != prev_type)
  781. printf("\n");
  782. if (strlen(syms->alias))
  783. sprintf(name, "%s OR %s", syms->symbol, syms->alias);
  784. else
  785. strcpy(name, syms->symbol);
  786. printf(" %-42s [%s]\n", name,
  787. event_type_descriptors[type]);
  788. prev_type = type;
  789. }
  790. printf("\n");
  791. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  792. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  793. /* skip invalid cache type */
  794. if (!is_cache_op_valid(type, op))
  795. continue;
  796. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  797. printf(" %-42s [%s]\n",
  798. event_cache_name(type, op, i),
  799. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  800. }
  801. }
  802. }
  803. printf("\n");
  804. printf(" %-42s [%s]\n",
  805. "rNNN (see 'perf list --help' on how to encode it)",
  806. event_type_descriptors[PERF_TYPE_RAW]);
  807. printf("\n");
  808. printf(" %-42s [%s]\n",
  809. "mem:<addr>[:access]",
  810. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  811. printf("\n");
  812. print_tracepoint_events();
  813. exit(129);
  814. }
  815. int perf_evsel_list__create_default(void)
  816. {
  817. struct perf_evsel *evsel;
  818. struct perf_event_attr attr;
  819. memset(&attr, 0, sizeof(attr));
  820. attr.type = PERF_TYPE_HARDWARE;
  821. attr.config = PERF_COUNT_HW_CPU_CYCLES;
  822. evsel = perf_evsel__new(&attr, 0);
  823. if (evsel == NULL)
  824. return -ENOMEM;
  825. list_add(&evsel->node, &evsel_list);
  826. ++nr_counters;
  827. return 0;
  828. }
  829. void perf_evsel_list__delete(void)
  830. {
  831. struct perf_evsel *pos, *n;
  832. list_for_each_entry_safe(pos, n, &evsel_list, node) {
  833. list_del_init(&pos->node);
  834. perf_evsel__delete(pos);
  835. }
  836. nr_counters = 0;
  837. }