parse-events.c 24 KB

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