parse-events.c 25 KB

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