parse-events.c 25 KB

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