parse-events.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. char *buf;
  256. size_t buf_sz;
  257. if (evsel->name) {
  258. /* Make new space for the modifier bits. */
  259. buf_sz = strlen(evsel->name) + 3;
  260. buf = malloc(buf_sz);
  261. if (!buf)
  262. /*
  263. * Always return what was already in 'name'.
  264. */
  265. return evsel->name;
  266. strlcpy(buf, evsel->name, buf_sz);
  267. free(evsel->name);
  268. evsel->name = buf;
  269. /* User mode profiling. */
  270. if (!evsel->attr.exclude_user && evsel->attr.exclude_kernel)
  271. strlcpy(&evsel->name[strlen(evsel->name)], ":u",
  272. buf_sz);
  273. /* Kernel mode profiling. */
  274. else if (!evsel->attr.exclude_kernel &&
  275. evsel->attr.exclude_user)
  276. strlcpy(&evsel->name[strlen(evsel->name)], ":k",
  277. buf_sz);
  278. return evsel->name;
  279. }
  280. return __event_name(type, config, NULL);
  281. }
  282. const char *__event_name(int type, u64 config, char *pmu_name)
  283. {
  284. static char buf[32];
  285. if (!pmu_name && type == PERF_TYPE_RAW) {
  286. sprintf(buf, "raw 0x%" PRIx64, config);
  287. return buf;
  288. }
  289. switch (type) {
  290. case PERF_TYPE_HARDWARE:
  291. if (config < PERF_COUNT_HW_MAX && hw_event_names[config])
  292. return hw_event_names[config];
  293. return "unknown-hardware";
  294. case PERF_TYPE_HW_CACHE: {
  295. u8 cache_type, cache_op, cache_result;
  296. cache_type = (config >> 0) & 0xff;
  297. if (cache_type > PERF_COUNT_HW_CACHE_MAX)
  298. return "unknown-ext-hardware-cache-type";
  299. cache_op = (config >> 8) & 0xff;
  300. if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
  301. return "unknown-ext-hardware-cache-op";
  302. cache_result = (config >> 16) & 0xff;
  303. if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
  304. return "unknown-ext-hardware-cache-result";
  305. if (!is_cache_op_valid(cache_type, cache_op))
  306. return "invalid-cache";
  307. return event_cache_name(cache_type, cache_op, cache_result);
  308. }
  309. case PERF_TYPE_SOFTWARE:
  310. if (config < PERF_COUNT_SW_MAX && sw_event_names[config])
  311. return sw_event_names[config];
  312. return "unknown-software";
  313. case PERF_TYPE_TRACEPOINT:
  314. return tracepoint_id_to_name(config);
  315. default:
  316. if (pmu_name) {
  317. snprintf(buf, sizeof(buf), "%s 0x%" PRIx64, pmu_name,
  318. config);
  319. return buf;
  320. } else
  321. break;
  322. }
  323. return "unknown";
  324. }
  325. static int add_event(struct list_head *list, int *idx,
  326. struct perf_event_attr *attr, char *name)
  327. {
  328. struct perf_evsel *evsel;
  329. event_attr_init(attr);
  330. evsel = perf_evsel__new(attr, (*idx)++);
  331. if (!evsel)
  332. return -ENOMEM;
  333. list_add_tail(&evsel->node, list);
  334. evsel->name = strdup(name);
  335. return 0;
  336. }
  337. static int parse_aliases(char *str, const char *names[][MAX_ALIASES], int size)
  338. {
  339. int i, j;
  340. int n, longest = -1;
  341. for (i = 0; i < size; i++) {
  342. for (j = 0; j < MAX_ALIASES && names[i][j]; j++) {
  343. n = strlen(names[i][j]);
  344. if (n > longest && !strncasecmp(str, names[i][j], n))
  345. longest = n;
  346. }
  347. if (longest > 0)
  348. return i;
  349. }
  350. return -1;
  351. }
  352. int parse_events_add_cache(struct list_head *list, int *idx,
  353. char *type, char *op_result1, char *op_result2)
  354. {
  355. struct perf_event_attr attr;
  356. char name[MAX_NAME_LEN];
  357. int cache_type = -1, cache_op = -1, cache_result = -1;
  358. char *op_result[2] = { op_result1, op_result2 };
  359. int i, n;
  360. /*
  361. * No fallback - if we cannot get a clear cache type
  362. * then bail out:
  363. */
  364. cache_type = parse_aliases(type, hw_cache,
  365. PERF_COUNT_HW_CACHE_MAX);
  366. if (cache_type == -1)
  367. return -EINVAL;
  368. n = snprintf(name, MAX_NAME_LEN, "%s", type);
  369. for (i = 0; (i < 2) && (op_result[i]); i++) {
  370. char *str = op_result[i];
  371. n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
  372. if (cache_op == -1) {
  373. cache_op = parse_aliases(str, hw_cache_op,
  374. PERF_COUNT_HW_CACHE_OP_MAX);
  375. if (cache_op >= 0) {
  376. if (!is_cache_op_valid(cache_type, cache_op))
  377. return -EINVAL;
  378. continue;
  379. }
  380. }
  381. if (cache_result == -1) {
  382. cache_result = parse_aliases(str, hw_cache_result,
  383. PERF_COUNT_HW_CACHE_RESULT_MAX);
  384. if (cache_result >= 0)
  385. continue;
  386. }
  387. }
  388. /*
  389. * Fall back to reads:
  390. */
  391. if (cache_op == -1)
  392. cache_op = PERF_COUNT_HW_CACHE_OP_READ;
  393. /*
  394. * Fall back to accesses:
  395. */
  396. if (cache_result == -1)
  397. cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
  398. memset(&attr, 0, sizeof(attr));
  399. attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
  400. attr.type = PERF_TYPE_HW_CACHE;
  401. return add_event(list, idx, &attr, name);
  402. }
  403. static int add_tracepoint(struct list_head *list, int *idx,
  404. char *sys_name, char *evt_name)
  405. {
  406. struct perf_event_attr attr;
  407. char name[MAX_NAME_LEN];
  408. char evt_path[MAXPATHLEN];
  409. char id_buf[4];
  410. u64 id;
  411. int fd;
  412. snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
  413. sys_name, evt_name);
  414. fd = open(evt_path, O_RDONLY);
  415. if (fd < 0)
  416. return -1;
  417. if (read(fd, id_buf, sizeof(id_buf)) < 0) {
  418. close(fd);
  419. return -1;
  420. }
  421. close(fd);
  422. id = atoll(id_buf);
  423. memset(&attr, 0, sizeof(attr));
  424. attr.config = id;
  425. attr.type = PERF_TYPE_TRACEPOINT;
  426. attr.sample_type |= PERF_SAMPLE_RAW;
  427. attr.sample_type |= PERF_SAMPLE_TIME;
  428. attr.sample_type |= PERF_SAMPLE_CPU;
  429. attr.sample_period = 1;
  430. snprintf(name, MAX_NAME_LEN, "%s:%s", sys_name, evt_name);
  431. return add_event(list, idx, &attr, name);
  432. }
  433. static int add_tracepoint_multi(struct list_head *list, int *idx,
  434. char *sys_name, char *evt_name)
  435. {
  436. char evt_path[MAXPATHLEN];
  437. struct dirent *evt_ent;
  438. DIR *evt_dir;
  439. int ret = 0;
  440. snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
  441. evt_dir = opendir(evt_path);
  442. if (!evt_dir) {
  443. perror("Can't open event dir");
  444. return -1;
  445. }
  446. while (!ret && (evt_ent = readdir(evt_dir))) {
  447. if (!strcmp(evt_ent->d_name, ".")
  448. || !strcmp(evt_ent->d_name, "..")
  449. || !strcmp(evt_ent->d_name, "enable")
  450. || !strcmp(evt_ent->d_name, "filter"))
  451. continue;
  452. if (!strglobmatch(evt_ent->d_name, evt_name))
  453. continue;
  454. ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
  455. }
  456. return ret;
  457. }
  458. int parse_events_add_tracepoint(struct list_head *list, int *idx,
  459. char *sys, char *event)
  460. {
  461. int ret;
  462. ret = debugfs_valid_mountpoint(tracing_events_path);
  463. if (ret)
  464. return ret;
  465. return strpbrk(event, "*?") ?
  466. add_tracepoint_multi(list, idx, sys, event) :
  467. add_tracepoint(list, idx, sys, event);
  468. }
  469. static int
  470. parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
  471. {
  472. int i;
  473. for (i = 0; i < 3; i++) {
  474. if (!type || !type[i])
  475. break;
  476. switch (type[i]) {
  477. case 'r':
  478. attr->bp_type |= HW_BREAKPOINT_R;
  479. break;
  480. case 'w':
  481. attr->bp_type |= HW_BREAKPOINT_W;
  482. break;
  483. case 'x':
  484. attr->bp_type |= HW_BREAKPOINT_X;
  485. break;
  486. default:
  487. return -EINVAL;
  488. }
  489. }
  490. if (!attr->bp_type) /* Default */
  491. attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
  492. return 0;
  493. }
  494. int parse_events_add_breakpoint(struct list_head *list, int *idx,
  495. void *ptr, char *type)
  496. {
  497. struct perf_event_attr attr;
  498. char name[MAX_NAME_LEN];
  499. memset(&attr, 0, sizeof(attr));
  500. attr.bp_addr = (unsigned long) ptr;
  501. if (parse_breakpoint_type(type, &attr))
  502. return -EINVAL;
  503. /*
  504. * We should find a nice way to override the access length
  505. * Provide some defaults for now
  506. */
  507. if (attr.bp_type == HW_BREAKPOINT_X)
  508. attr.bp_len = sizeof(long);
  509. else
  510. attr.bp_len = HW_BREAKPOINT_LEN_4;
  511. attr.type = PERF_TYPE_BREAKPOINT;
  512. snprintf(name, MAX_NAME_LEN, "mem:%p:%s", ptr, type ? type : "rw");
  513. return add_event(list, idx, &attr, name);
  514. }
  515. static int config_term(struct perf_event_attr *attr,
  516. struct parse_events__term *term)
  517. {
  518. switch (term->type) {
  519. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  520. attr->config = term->val.num;
  521. break;
  522. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  523. attr->config1 = term->val.num;
  524. break;
  525. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  526. attr->config2 = term->val.num;
  527. break;
  528. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  529. attr->sample_period = term->val.num;
  530. break;
  531. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  532. /*
  533. * TODO uncomment when the field is available
  534. * attr->branch_sample_type = term->val.num;
  535. */
  536. break;
  537. default:
  538. return -EINVAL;
  539. }
  540. return 0;
  541. }
  542. static int config_attr(struct perf_event_attr *attr,
  543. struct list_head *head, int fail)
  544. {
  545. struct parse_events__term *term;
  546. list_for_each_entry(term, head, list)
  547. if (config_term(attr, term) && fail)
  548. return -EINVAL;
  549. return 0;
  550. }
  551. int parse_events_add_numeric_legacy(struct list_head *list, int *idx,
  552. const char *name, unsigned long config,
  553. struct list_head *head_config)
  554. {
  555. struct perf_event_attr attr;
  556. struct perf_pmu *pmu;
  557. char *pmu_name = strdup(name);
  558. memset(&attr, 0, sizeof(attr));
  559. pmu = perf_pmu__find(pmu_name);
  560. if (!pmu)
  561. return -EINVAL;
  562. attr.type = pmu->type;
  563. attr.config = config;
  564. if (head_config &&
  565. config_attr(&attr, head_config, 1))
  566. return -EINVAL;
  567. return add_event(list, idx, &attr,
  568. (char *) __event_name(pmu->type, config, pmu_name));
  569. }
  570. int parse_events_add_numeric(struct list_head *list, int *idx,
  571. unsigned long type, unsigned long config,
  572. struct list_head *head_config)
  573. {
  574. struct perf_event_attr attr;
  575. memset(&attr, 0, sizeof(attr));
  576. attr.type = type;
  577. attr.config = config;
  578. if (head_config &&
  579. config_attr(&attr, head_config, 1))
  580. return -EINVAL;
  581. return add_event(list, idx, &attr,
  582. (char *) __event_name(type, config, NULL));
  583. }
  584. int parse_events_add_pmu(struct list_head *list, int *idx,
  585. char *name, struct list_head *head_config)
  586. {
  587. struct perf_event_attr attr;
  588. struct perf_pmu *pmu;
  589. char *ev_name;
  590. pmu = perf_pmu__find(name);
  591. if (!pmu)
  592. return -EINVAL;
  593. memset(&attr, 0, sizeof(attr));
  594. /*
  595. * Configure hardcoded terms first, no need to check
  596. * return value when called with fail == 0 ;)
  597. */
  598. config_attr(&attr, head_config, 0);
  599. if (perf_pmu__config(pmu, &attr, head_config))
  600. return -EINVAL;
  601. ev_name = (char *) __event_name(attr.type, attr.config, pmu->name);
  602. return add_event(list, idx, &attr, ev_name);
  603. }
  604. void parse_events_update_lists(struct list_head *list_event,
  605. struct list_head *list_all)
  606. {
  607. /*
  608. * Called for single event definition. Update the
  609. * 'all event' list, and reinit the 'signle event'
  610. * list, for next event definition.
  611. */
  612. list_splice_tail(list_event, list_all);
  613. INIT_LIST_HEAD(list_event);
  614. }
  615. int parse_events_modifier(struct list_head *list, char *str)
  616. {
  617. struct perf_evsel *evsel;
  618. int exclude = 0, exclude_GH = 0;
  619. int eu = 0, ek = 0, eh = 0, eH = 0, eG = 0, precise = 0;
  620. if (str == NULL)
  621. return 0;
  622. while (*str) {
  623. if (*str == 'u') {
  624. if (!exclude)
  625. exclude = eu = ek = eh = 1;
  626. eu = 0;
  627. } else if (*str == 'k') {
  628. if (!exclude)
  629. exclude = eu = ek = eh = 1;
  630. ek = 0;
  631. } else if (*str == 'h') {
  632. if (!exclude)
  633. exclude = eu = ek = eh = 1;
  634. eh = 0;
  635. } else if (*str == 'G') {
  636. if (!exclude_GH)
  637. exclude_GH = eG = eH = 1;
  638. eG = 0;
  639. } else if (*str == 'H') {
  640. if (!exclude_GH)
  641. exclude_GH = eG = eH = 1;
  642. eH = 0;
  643. } else if (*str == 'p') {
  644. precise++;
  645. } else
  646. break;
  647. ++str;
  648. }
  649. /*
  650. * precise ip:
  651. *
  652. * 0 - SAMPLE_IP can have arbitrary skid
  653. * 1 - SAMPLE_IP must have constant skid
  654. * 2 - SAMPLE_IP requested to have 0 skid
  655. * 3 - SAMPLE_IP must have 0 skid
  656. *
  657. * See also PERF_RECORD_MISC_EXACT_IP
  658. */
  659. if (precise > 3)
  660. return -EINVAL;
  661. list_for_each_entry(evsel, list, node) {
  662. evsel->attr.exclude_user = eu;
  663. evsel->attr.exclude_kernel = ek;
  664. evsel->attr.exclude_hv = eh;
  665. evsel->attr.precise_ip = precise;
  666. evsel->attr.exclude_host = eH;
  667. evsel->attr.exclude_guest = eG;
  668. }
  669. return 0;
  670. }
  671. int parse_events(struct perf_evlist *evlist, const char *str, int unset __used)
  672. {
  673. LIST_HEAD(list);
  674. LIST_HEAD(list_tmp);
  675. YY_BUFFER_STATE buffer;
  676. int ret, idx = evlist->nr_entries;
  677. buffer = parse_events__scan_string(str);
  678. ret = parse_events_parse(&list, &list_tmp, &idx);
  679. parse_events__flush_buffer(buffer);
  680. parse_events__delete_buffer(buffer);
  681. if (!ret) {
  682. int entries = idx - evlist->nr_entries;
  683. perf_evlist__splice_list_tail(evlist, &list, entries);
  684. return 0;
  685. }
  686. /*
  687. * There are 2 users - builtin-record and builtin-test objects.
  688. * Both call perf_evlist__delete in case of error, so we dont
  689. * need to bother.
  690. */
  691. fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
  692. fprintf(stderr, "Run 'perf list' for a list of valid events\n");
  693. return ret;
  694. }
  695. int parse_events_option(const struct option *opt, const char *str,
  696. int unset __used)
  697. {
  698. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  699. return parse_events(evlist, str, unset);
  700. }
  701. int parse_filter(const struct option *opt, const char *str,
  702. int unset __used)
  703. {
  704. struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
  705. struct perf_evsel *last = NULL;
  706. if (evlist->nr_entries > 0)
  707. last = list_entry(evlist->entries.prev, struct perf_evsel, node);
  708. if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
  709. fprintf(stderr,
  710. "-F option should follow a -e tracepoint option\n");
  711. return -1;
  712. }
  713. last->filter = strdup(str);
  714. if (last->filter == NULL) {
  715. fprintf(stderr, "not enough memory to hold filter string\n");
  716. return -1;
  717. }
  718. return 0;
  719. }
  720. static const char * const event_type_descriptors[] = {
  721. "Hardware event",
  722. "Software event",
  723. "Tracepoint event",
  724. "Hardware cache event",
  725. "Raw hardware event descriptor",
  726. "Hardware breakpoint",
  727. };
  728. /*
  729. * Print the events from <debugfs_mount_point>/tracing/events
  730. */
  731. void print_tracepoint_events(const char *subsys_glob, const char *event_glob)
  732. {
  733. DIR *sys_dir, *evt_dir;
  734. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  735. char evt_path[MAXPATHLEN];
  736. char dir_path[MAXPATHLEN];
  737. if (debugfs_valid_mountpoint(tracing_events_path))
  738. return;
  739. sys_dir = opendir(tracing_events_path);
  740. if (!sys_dir)
  741. return;
  742. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  743. if (subsys_glob != NULL &&
  744. !strglobmatch(sys_dirent.d_name, subsys_glob))
  745. continue;
  746. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  747. sys_dirent.d_name);
  748. evt_dir = opendir(dir_path);
  749. if (!evt_dir)
  750. continue;
  751. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  752. if (event_glob != NULL &&
  753. !strglobmatch(evt_dirent.d_name, event_glob))
  754. continue;
  755. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  756. sys_dirent.d_name, evt_dirent.d_name);
  757. printf(" %-50s [%s]\n", evt_path,
  758. event_type_descriptors[PERF_TYPE_TRACEPOINT]);
  759. }
  760. closedir(evt_dir);
  761. }
  762. closedir(sys_dir);
  763. }
  764. /*
  765. * Check whether event is in <debugfs_mount_point>/tracing/events
  766. */
  767. int is_valid_tracepoint(const char *event_string)
  768. {
  769. DIR *sys_dir, *evt_dir;
  770. struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
  771. char evt_path[MAXPATHLEN];
  772. char dir_path[MAXPATHLEN];
  773. if (debugfs_valid_mountpoint(tracing_events_path))
  774. return 0;
  775. sys_dir = opendir(tracing_events_path);
  776. if (!sys_dir)
  777. return 0;
  778. for_each_subsystem(sys_dir, sys_dirent, sys_next) {
  779. snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
  780. sys_dirent.d_name);
  781. evt_dir = opendir(dir_path);
  782. if (!evt_dir)
  783. continue;
  784. for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
  785. snprintf(evt_path, MAXPATHLEN, "%s:%s",
  786. sys_dirent.d_name, evt_dirent.d_name);
  787. if (!strcmp(evt_path, event_string)) {
  788. closedir(evt_dir);
  789. closedir(sys_dir);
  790. return 1;
  791. }
  792. }
  793. closedir(evt_dir);
  794. }
  795. closedir(sys_dir);
  796. return 0;
  797. }
  798. void print_events_type(u8 type)
  799. {
  800. struct event_symbol *syms = event_symbols;
  801. unsigned int i;
  802. char name[64];
  803. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  804. if (type != syms->type)
  805. continue;
  806. if (strlen(syms->alias))
  807. snprintf(name, sizeof(name), "%s OR %s",
  808. syms->symbol, syms->alias);
  809. else
  810. snprintf(name, sizeof(name), "%s", syms->symbol);
  811. printf(" %-50s [%s]\n", name,
  812. event_type_descriptors[type]);
  813. }
  814. }
  815. int print_hwcache_events(const char *event_glob)
  816. {
  817. unsigned int type, op, i, printed = 0;
  818. for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
  819. for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
  820. /* skip invalid cache type */
  821. if (!is_cache_op_valid(type, op))
  822. continue;
  823. for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
  824. char *name = event_cache_name(type, op, i);
  825. if (event_glob != NULL && !strglobmatch(name, event_glob))
  826. continue;
  827. printf(" %-50s [%s]\n", name,
  828. event_type_descriptors[PERF_TYPE_HW_CACHE]);
  829. ++printed;
  830. }
  831. }
  832. }
  833. return printed;
  834. }
  835. /*
  836. * Print the help text for the event symbols:
  837. */
  838. void print_events(const char *event_glob)
  839. {
  840. unsigned int i, type, prev_type = -1, printed = 0, ntypes_printed = 0;
  841. struct event_symbol *syms = event_symbols;
  842. char name[MAX_NAME_LEN];
  843. printf("\n");
  844. printf("List of pre-defined events (to be used in -e):\n");
  845. for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
  846. type = syms->type;
  847. if (type != prev_type && printed) {
  848. printf("\n");
  849. printed = 0;
  850. ntypes_printed++;
  851. }
  852. if (event_glob != NULL &&
  853. !(strglobmatch(syms->symbol, event_glob) ||
  854. (syms->alias && strglobmatch(syms->alias, event_glob))))
  855. continue;
  856. if (strlen(syms->alias))
  857. snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
  858. else
  859. strncpy(name, syms->symbol, MAX_NAME_LEN);
  860. printf(" %-50s [%s]\n", name,
  861. event_type_descriptors[type]);
  862. prev_type = type;
  863. ++printed;
  864. }
  865. if (ntypes_printed) {
  866. printed = 0;
  867. printf("\n");
  868. }
  869. print_hwcache_events(event_glob);
  870. if (event_glob != NULL)
  871. return;
  872. printf("\n");
  873. printf(" %-50s [%s]\n",
  874. "rNNN",
  875. event_type_descriptors[PERF_TYPE_RAW]);
  876. printf(" %-50s [%s]\n",
  877. "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
  878. event_type_descriptors[PERF_TYPE_RAW]);
  879. printf(" (see 'perf list --help' on how to encode it)\n");
  880. printf("\n");
  881. printf(" %-50s [%s]\n",
  882. "mem:<addr>[:access]",
  883. event_type_descriptors[PERF_TYPE_BREAKPOINT]);
  884. printf("\n");
  885. print_tracepoint_events(NULL, NULL);
  886. }
  887. int parse_events__is_hardcoded_term(struct parse_events__term *term)
  888. {
  889. return term->type <= PARSE_EVENTS__TERM_TYPE_HARDCODED_MAX;
  890. }
  891. int parse_events__new_term(struct parse_events__term **_term, int type,
  892. char *config, char *str, long num)
  893. {
  894. struct parse_events__term *term;
  895. term = zalloc(sizeof(*term));
  896. if (!term)
  897. return -ENOMEM;
  898. INIT_LIST_HEAD(&term->list);
  899. term->type = type;
  900. term->config = config;
  901. switch (type) {
  902. case PARSE_EVENTS__TERM_TYPE_CONFIG:
  903. case PARSE_EVENTS__TERM_TYPE_CONFIG1:
  904. case PARSE_EVENTS__TERM_TYPE_CONFIG2:
  905. case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
  906. case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
  907. case PARSE_EVENTS__TERM_TYPE_NUM:
  908. term->val.num = num;
  909. break;
  910. case PARSE_EVENTS__TERM_TYPE_STR:
  911. term->val.str = str;
  912. break;
  913. default:
  914. return -EINVAL;
  915. }
  916. *_term = term;
  917. return 0;
  918. }
  919. void parse_events__free_terms(struct list_head *terms)
  920. {
  921. struct parse_events__term *term, *h;
  922. list_for_each_entry_safe(term, h, terms, list)
  923. free(term);
  924. free(terms);
  925. }