parse-events.c 25 KB

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