parse-events.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. case PARSE_EVENTS__TERM_TYPE_NAME:
  531. CHECK_TYPE_VAL(STR);
  532. break;
  533. default:
  534. return -EINVAL;
  535. }
  536. return 0;
  537. #undef CHECK_TYPE_VAL
  538. }
  539. static int config_attr(struct perf_event_attr *attr,
  540. struct list_head *head, int fail)
  541. {
  542. struct parse_events__term *term;
  543. list_for_each_entry(term, head, list)
  544. if (config_term(attr, term) && fail)
  545. return -EINVAL;
  546. return 0;
  547. }
  548. int parse_events_add_numeric(struct list_head **list, int *idx,
  549. unsigned long type, unsigned long config,
  550. struct list_head *head_config)
  551. {
  552. struct perf_event_attr attr;
  553. memset(&attr, 0, sizeof(attr));
  554. attr.type = type;
  555. attr.config = config;
  556. if (head_config &&
  557. config_attr(&attr, head_config, 1))
  558. return -EINVAL;
  559. return add_event(list, idx, &attr,
  560. (char *) __event_name(type, config));
  561. }
  562. static int parse_events__is_name_term(struct parse_events__term *term)
  563. {
  564. return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
  565. }
  566. static char *pmu_event_name(struct perf_event_attr *attr,
  567. struct list_head *head_terms)
  568. {
  569. struct parse_events__term *term;
  570. list_for_each_entry(term, head_terms, list)
  571. if (parse_events__is_name_term(term))
  572. return term->val.str;
  573. return (char *) __event_name(PERF_TYPE_RAW, attr->config);
  574. }
  575. int parse_events_add_pmu(struct list_head **list, int *idx,
  576. char *name, struct list_head *head_config)
  577. {
  578. struct perf_event_attr attr;
  579. struct perf_pmu *pmu;
  580. pmu = perf_pmu__find(name);
  581. if (!pmu)
  582. return -EINVAL;
  583. memset(&attr, 0, sizeof(attr));
  584. /*
  585. * Configure hardcoded terms first, no need to check
  586. * return value when called with fail == 0 ;)
  587. */
  588. config_attr(&attr, head_config, 0);
  589. if (perf_pmu__config(pmu, &attr, head_config))
  590. return -EINVAL;
  591. return add_event(list, idx, &attr,
  592. pmu_event_name(&attr, head_config));
  593. }
  594. void parse_events_update_lists(struct list_head *list_event,
  595. struct list_head *list_all)
  596. {
  597. /*
  598. * Called for single event definition. Update the
  599. * 'all event' list, and reinit the 'signle event'
  600. * list, for next event definition.
  601. */
  602. list_splice_tail(list_event, list_all);
  603. free(list_event);
  604. }
  605. int parse_events_modifier(struct list_head *list, char *str)
  606. {
  607. struct perf_evsel *evsel;
  608. int exclude = 0, exclude_GH = 0;
  609. int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
  610. if (str == NULL)
  611. return 0;
  612. while (*str) {
  613. if (*str == 'u') {
  614. if (!exclude)
  615. exclude = eu = ek = eh = 1;
  616. eu = 0;
  617. } else if (*str == 'k') {
  618. if (!exclude)
  619. exclude = eu = ek = eh = 1;
  620. ek = 0;
  621. } else if (*str == 'h') {
  622. if (!exclude)
  623. exclude = eu = ek = eh = 1;
  624. eh = 0;
  625. } else if (*str == 'G') {
  626. if (!exclude_GH)
  627. exclude_GH = eG = eH = 1;
  628. eG = 0;
  629. } else if (*str == 'H') {
  630. if (!exclude_GH)
  631. exclude_GH = eG = eH = 1;
  632. eH = 0;
  633. } else if (*str == 'p') {
  634. precise++;
  635. } else
  636. break;
  637. ++str;
  638. }
  639. /*
  640. * precise ip:
  641. *
  642. * 0 - SAMPLE_IP can have arbitrary skid
  643. * 1 - SAMPLE_IP must have constant skid
  644. * 2 - SAMPLE_IP requested to have 0 skid
  645. * 3 - SAMPLE_IP must have 0 skid
  646. *
  647. * See also PERF_RECORD_MISC_EXACT_IP
  648. */
  649. if (precise > 3)
  650. return -EINVAL;
  651. list_for_each_entry(evsel, list, node) {
  652. evsel->attr.exclude_user = eu;
  653. evsel->attr.exclude_kernel = ek;
  654. evsel->attr.exclude_hv = eh;
  655. evsel->attr.precise_ip = precise;
  656. evsel->attr.exclude_host = eH;
  657. evsel->attr.exclude_guest = eG;
  658. }
  659. return 0;
  660. }
  661. int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
  662. {
  663. LIST_HEAD(list);
  664. LIST_HEAD(list_tmp);
  665. YY_BUFFER_STATE buffer;
  666. int ret, idx = evlist->nr_entries;
  667. buffer = parse_events__scan_string(str);
  668. #ifdef PARSER_DEBUG
  669. parse_events_debug = 1;
  670. #endif
  671. ret = parse_events_parse(&list, &idx);
  672. parse_events__flush_buffer(buffer);
  673. parse_events__delete_buffer(buffer);
  674. parse_events_lex_destroy();
  675. if (!ret) {
  676. int entries = idx - evlist->nr_entries;
  677. perf_evlist__splice_list_tail(evlist, &list, entries);
  678. return 0;
  679. }
  680. /*
  681. * There are 2 users - builtin-record and builtin-test objects.
  682. * Both call perf_evlist__delete in case of error, so we dont
  683. * need to bother.
  684. */
  685. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  686. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  687. return ret;
  688. }
  689. int parse_events_option(const struct option *opt, const char *str,
  690. int unset __used)
  691. {
  692. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  693. return parse_events(evlist, str, unset);
  694. }
  695. int parse_filter(const struct option *opt, const char *str,
  696. int unset __used)
  697. {
  698. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  699. struct perf_evsel *last = NULL;
  700. if (evlist->nr_entries > 0)
  701. last = list_entry(evlist->entries.prev, struct perf_evsel, node);
  702. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  703. fprintf(stderr,
  704. "-F option should follow a -e tracepoint option\n");
  705. return -1;
  706. }
  707. last->filter = strdup(str);
  708. if (last->filter == NULL) {
  709. fprintf(stderr, "not enough memory to hold filter string\n");
  710. return -1;
  711. }
  712. return 0;
  713. }
  714. static const char * const event_type_descriptors[] = {
  715. "Hardware event",
  716. "Software event",
  717. "Tracepoint event",
  718. "Hardware cache event",
  719. "Raw hardware event descriptor",
  720. "Hardware breakpoint",
  721. };
  722. /*
  723. * Print the events from <debugfs_mount_point>/tracing/events
  724. */
  725. void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
  726. {
  727. DIR *sys_dir, *evt_dir;
  728. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  729. char evt_path[MAXPATHLEN];
  730. char dir_path[MAXPATHLEN];
  731. if (debugfs_valid_mountpoint(tracing_events_path))
  732. return;
  733. sys_dir = opendir(tracing_events_path);
  734. if (!sys_dir)
  735. return;
  736. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  737. if (subsys_glob != NULL &&
  738. !strglobmatch(sys_dirent.d_name, subsys_glob))
  739. continue;
  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. if (event_glob != NULL &&
  747. !strglobmatch(evt_dirent.d_name, event_glob))
  748. continue;
  749. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  750. sys_dirent.d_name, evt_dirent.d_name);
  751. printf(" %-50s [%s]\n", evt_path,
  752. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  753. }
  754. closedir(evt_dir);
  755. }
  756. closedir(sys_dir);
  757. }
  758. /*
  759. * Check whether event is in <debugfs_mount_point>/tracing/events
  760. */
  761. int is_valid_tracepoint(const char *event_string)
  762. {
  763. DIR *sys_dir, *evt_dir;
  764. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  765. char evt_path[MAXPATHLEN];
  766. char dir_path[MAXPATHLEN];
  767. if (debugfs_valid_mountpoint(tracing_events_path))
  768. return 0;
  769. sys_dir = opendir(tracing_events_path);
  770. if (!sys_dir)
  771. return 0;
  772. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  773. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  774. sys_dirent.d_name);
  775. evt_dir = opendir(dir_path);
  776. if (!evt_dir)
  777. continue;
  778. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  779. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  780. sys_dirent.d_name, evt_dirent.d_name);
  781. if (!strcmp(evt_path, event_string)) {
  782. closedir(evt_dir);
  783. closedir(sys_dir);
  784. return 1;
  785. }
  786. }
  787. closedir(evt_dir);
  788. }
  789. closedir(sys_dir);
  790. return 0;
  791. }
  792. void print_events_type(u8 type)
  793. {
  794. struct event_symbol *syms = event_symbols;
  795. unsigned int i;
  796. char name[64];
  797. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  798. if (type != syms->type)
  799. continue;
  800. if (strlen(syms->alias))
  801. snprintf(name, sizeof(name), "%s OR %s",
  802. syms->symbol, syms->alias);
  803. else
  804. snprintf(name, sizeof(name), "%s", syms->symbol);
  805. printf(" %-50s [%s]\n", name,
  806. event_type_descriptors[type]);
  807. }
  808. }
  809. int print_hwcache_events(const char *event_glob)
  810. {
  811. unsigned int type, op, i, printed = 0;
  812. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  813. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  814. /* skip invalid cache type */
  815. if (!is_cache_op_valid(type, op))
  816. continue;
  817. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  818. char *name = event_cache_name(type, op, i);
  819. if (event_glob != NULL && !strglobmatch(name, event_glob))
  820. continue;
  821. printf(" %-50s [%s]\n", name,
  822. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  823. ++printed;
  824. }
  825. }
  826. }
  827. return printed;
  828. }
  829. /*
  830. * Print the help text for the event symbols:
  831. */
  832. void print_events(const char *event_glob)
  833. {
  834. unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
  835. struct event_symbol *syms = event_symbols;
  836. char name[MAX_NAME_LEN];
  837. printf("\n");
  838. printf("List of pre-defined events (to be used in -e):\n");
  839. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  840. type = syms->type;
  841. if (type != prev_type && printed) {
  842. printf("\n");
  843. printed = 0;
  844. ntypes_printed++;
  845. }
  846. if (event_glob != NULL &&
  847. !(strglobmatch(syms->symbol, event_glob) ||
  848. (syms->alias && strglobmatch(syms->alias, event_glob))))
  849. continue;
  850. if (strlen(syms->alias))
  851. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  852. else
  853. strncpy(name, syms->symbol, MAX_NAME_LEN);
  854. printf(" %-50s [%s]\n", name,
  855. event_type_descriptors[type]);
  856. prev_type = type;
  857. ++printed;
  858. }
  859. if (ntypes_printed) {
  860. printed = 0;
  861. printf("\n");
  862. }
  863. print_hwcache_events(event_glob);
  864. if (event_glob != NULL)
  865. return;
  866. printf("\n");
  867. printf(" %-50s [%s]\n",
  868. "rNNN",
  869. event_type_descriptors[PERF_TYPE_RAW]);
  870. printf(" %-50s [%s]\n",
  871. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  872. event_type_descriptors[PERF_TYPE_RAW]);
  873. printf(" (see 'perf list --help' on how to encode it)\n");
  874. printf("\n");
  875. printf(" %-50s [%s]\n",
  876. "mem:<addr>[:access]",
  877. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  878. printf("\n");
  879. print_tracepoint_events(NULL, NULL);
  880. }
  881. int parse_events__is_hardcoded_term(struct parse_events__term *term)
  882. {
  883. return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
  884. }
  885. static int new_term(struct parse_events__term **_term, int type_val,
  886. int type_term, char *config,
  887. char *str, long num)
  888. {
  889. struct parse_events__term *term;
  890. term = zalloc(sizeof(*term));
  891. if (!term)
  892. return -ENOMEM;
  893. INIT_LIST_HEAD(&term->list);
  894. term->type_val = type_val;
  895. term->type_term = type_term;
  896. term->config = config;
  897. switch (type_val) {
  898. case PARSE_EVENTS__TERM_TYPE_NUM:
  899. term->val.num = num;
  900. break;
  901. case PARSE_EVENTS__TERM_TYPE_STR:
  902. term->val.str = str;
  903. break;
  904. default:
  905. return -EINVAL;
  906. }
  907. *_term = term;
  908. return 0;
  909. }
  910. int parse_events__term_num(struct parse_events__term **term,
  911. int type_term, char *config, long num)
  912. {
  913. return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
  914. config, NULL, num);
  915. }
  916. int parse_events__term_str(struct parse_events__term **term,
  917. int type_term, char *config, char *str)
  918. {
  919. return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
  920. config, str, 0);
  921. }
  922. void parse_events__free_terms(struct list_head *terms)
  923. {
  924. struct parse_events__term *term, *h;
  925. list_for_each_entry_safe(term, h, terms, list)
  926. free(term);
  927. free(terms);
  928. }