parse-events.c 22 KB

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