pmu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. #include <linux/list.h>
  2. #include <linux/compiler.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdio.h>
  6. #include <stdbool.h>
  7. #include <stdarg.h>
  8. #include <dirent.h>
  9. #include <api/fs/fs.h>
  10. #include <locale.h>
  11. #include "util.h"
  12. #include "pmu.h"
  13. #include "parse-events.h"
  14. #include "cpumap.h"
  15. struct perf_pmu_format {
  16. char *name;
  17. int value;
  18. DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
  19. struct list_head list;
  20. };
  21. #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
  22. int perf_pmu_parse(struct list_head *list, char *name);
  23. extern FILE *perf_pmu_in;
  24. static LIST_HEAD(pmus);
  25. /*
  26. * Parse & process all the sysfs attributes located under
  27. * the directory specified in 'dir' parameter.
  28. */
  29. int perf_pmu__format_parse(char *dir, struct list_head *head)
  30. {
  31. struct dirent *evt_ent;
  32. DIR *format_dir;
  33. int ret = 0;
  34. format_dir = opendir(dir);
  35. if (!format_dir)
  36. return -EINVAL;
  37. while (!ret && (evt_ent = readdir(format_dir))) {
  38. char path[PATH_MAX];
  39. char *name = evt_ent->d_name;
  40. FILE *file;
  41. if (!strcmp(name, ".") || !strcmp(name, ".."))
  42. continue;
  43. snprintf(path, PATH_MAX, "%s/%s", dir, name);
  44. ret = -EINVAL;
  45. file = fopen(path, "r");
  46. if (!file)
  47. break;
  48. perf_pmu_in = file;
  49. ret = perf_pmu_parse(head, name);
  50. fclose(file);
  51. }
  52. closedir(format_dir);
  53. return ret;
  54. }
  55. /*
  56. * Reading/parsing the default pmu format definition, which should be
  57. * located at:
  58. * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
  59. */
  60. static int pmu_format(const char *name, struct list_head *format)
  61. {
  62. struct stat st;
  63. char path[PATH_MAX];
  64. const char *sysfs = sysfs__mountpoint();
  65. if (!sysfs)
  66. return -1;
  67. snprintf(path, PATH_MAX,
  68. "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
  69. if (stat(path, &st) < 0)
  70. return 0; /* no error if format does not exist */
  71. if (perf_pmu__format_parse(path, format))
  72. return -1;
  73. return 0;
  74. }
  75. static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
  76. {
  77. struct stat st;
  78. ssize_t sret;
  79. char scale[128];
  80. int fd, ret = -1;
  81. char path[PATH_MAX];
  82. const char *lc;
  83. scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
  84. fd = open(path, O_RDONLY);
  85. if (fd == -1)
  86. return -1;
  87. if (fstat(fd, &st) < 0)
  88. goto error;
  89. sret = read(fd, scale, sizeof(scale)-1);
  90. if (sret < 0)
  91. goto error;
  92. if (scale[sret - 1] == '\n')
  93. scale[sret - 1] = '\0';
  94. else
  95. scale[sret] = '\0';
  96. /*
  97. * save current locale
  98. */
  99. lc = setlocale(LC_NUMERIC, NULL);
  100. /*
  101. * force to C locale to ensure kernel
  102. * scale string is converted correctly.
  103. * kernel uses default C locale.
  104. */
  105. setlocale(LC_NUMERIC, "C");
  106. alias->scale = strtod(scale, NULL);
  107. /* restore locale */
  108. setlocale(LC_NUMERIC, lc);
  109. ret = 0;
  110. error:
  111. close(fd);
  112. return ret;
  113. }
  114. static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
  115. {
  116. char path[PATH_MAX];
  117. ssize_t sret;
  118. int fd;
  119. scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
  120. fd = open(path, O_RDONLY);
  121. if (fd == -1)
  122. return -1;
  123. sret = read(fd, alias->unit, UNIT_MAX_LEN);
  124. if (sret < 0)
  125. goto error;
  126. close(fd);
  127. if (alias->unit[sret - 1] == '\n')
  128. alias->unit[sret - 1] = '\0';
  129. else
  130. alias->unit[sret] = '\0';
  131. return 0;
  132. error:
  133. close(fd);
  134. alias->unit[0] = '\0';
  135. return -1;
  136. }
  137. static int
  138. perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
  139. {
  140. char path[PATH_MAX];
  141. int fd;
  142. scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
  143. fd = open(path, O_RDONLY);
  144. if (fd == -1)
  145. return -1;
  146. close(fd);
  147. alias->per_pkg = true;
  148. return 0;
  149. }
  150. static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
  151. char *dir, char *name)
  152. {
  153. char path[PATH_MAX];
  154. int fd;
  155. scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
  156. fd = open(path, O_RDONLY);
  157. if (fd == -1)
  158. return -1;
  159. alias->snapshot = true;
  160. close(fd);
  161. return 0;
  162. }
  163. static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
  164. char *desc __maybe_unused, char *val)
  165. {
  166. struct perf_pmu_alias *alias;
  167. int ret;
  168. alias = malloc(sizeof(*alias));
  169. if (!alias)
  170. return -ENOMEM;
  171. INIT_LIST_HEAD(&alias->terms);
  172. alias->scale = 1.0;
  173. alias->unit[0] = '\0';
  174. alias->per_pkg = false;
  175. ret = parse_events_terms(&alias->terms, val);
  176. if (ret) {
  177. pr_err("Cannot parse alias %s: %d\n", val, ret);
  178. free(alias);
  179. return ret;
  180. }
  181. alias->name = strdup(name);
  182. if (dir) {
  183. /*
  184. * load unit name and scale if available
  185. */
  186. perf_pmu__parse_unit(alias, dir, name);
  187. perf_pmu__parse_scale(alias, dir, name);
  188. perf_pmu__parse_per_pkg(alias, dir, name);
  189. perf_pmu__parse_snapshot(alias, dir, name);
  190. }
  191. list_add_tail(&alias->list, list);
  192. return 0;
  193. }
  194. static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
  195. {
  196. char buf[256];
  197. int ret;
  198. ret = fread(buf, 1, sizeof(buf), file);
  199. if (ret == 0)
  200. return -EINVAL;
  201. buf[ret] = 0;
  202. return __perf_pmu__new_alias(list, dir, name, NULL, buf);
  203. }
  204. static inline bool pmu_alias_info_file(char *name)
  205. {
  206. size_t len;
  207. len = strlen(name);
  208. if (len > 5 && !strcmp(name + len - 5, ".unit"))
  209. return true;
  210. if (len > 6 && !strcmp(name + len - 6, ".scale"))
  211. return true;
  212. if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
  213. return true;
  214. if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
  215. return true;
  216. return false;
  217. }
  218. /*
  219. * Process all the sysfs attributes located under the directory
  220. * specified in 'dir' parameter.
  221. */
  222. static int pmu_aliases_parse(char *dir, struct list_head *head)
  223. {
  224. struct dirent *evt_ent;
  225. DIR *event_dir;
  226. event_dir = opendir(dir);
  227. if (!event_dir)
  228. return -EINVAL;
  229. while ((evt_ent = readdir(event_dir))) {
  230. char path[PATH_MAX];
  231. char *name = evt_ent->d_name;
  232. FILE *file;
  233. if (!strcmp(name, ".") || !strcmp(name, ".."))
  234. continue;
  235. /*
  236. * skip info files parsed in perf_pmu__new_alias()
  237. */
  238. if (pmu_alias_info_file(name))
  239. continue;
  240. snprintf(path, PATH_MAX, "%s/%s", dir, name);
  241. file = fopen(path, "r");
  242. if (!file) {
  243. pr_debug("Cannot open %s\n", path);
  244. continue;
  245. }
  246. if (perf_pmu__new_alias(head, dir, name, file) < 0)
  247. pr_debug("Cannot set up %s\n", name);
  248. fclose(file);
  249. }
  250. closedir(event_dir);
  251. return 0;
  252. }
  253. /*
  254. * Reading the pmu event aliases definition, which should be located at:
  255. * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
  256. */
  257. static int pmu_aliases(const char *name, struct list_head *head)
  258. {
  259. struct stat st;
  260. char path[PATH_MAX];
  261. const char *sysfs = sysfs__mountpoint();
  262. if (!sysfs)
  263. return -1;
  264. snprintf(path, PATH_MAX,
  265. "%s/bus/event_source/devices/%s/events", sysfs, name);
  266. if (stat(path, &st) < 0)
  267. return 0; /* no error if 'events' does not exist */
  268. if (pmu_aliases_parse(path, head))
  269. return -1;
  270. return 0;
  271. }
  272. static int pmu_alias_terms(struct perf_pmu_alias *alias,
  273. struct list_head *terms)
  274. {
  275. struct parse_events_term *term, *cloned;
  276. LIST_HEAD(list);
  277. int ret;
  278. list_for_each_entry(term, &alias->terms, list) {
  279. ret = parse_events_term__clone(&cloned, term);
  280. if (ret) {
  281. parse_events__free_terms(&list);
  282. return ret;
  283. }
  284. list_add_tail(&cloned->list, &list);
  285. }
  286. list_splice(&list, terms);
  287. return 0;
  288. }
  289. /*
  290. * Reading/parsing the default pmu type value, which should be
  291. * located at:
  292. * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
  293. */
  294. static int pmu_type(const char *name, __u32 *type)
  295. {
  296. struct stat st;
  297. char path[PATH_MAX];
  298. FILE *file;
  299. int ret = 0;
  300. const char *sysfs = sysfs__mountpoint();
  301. if (!sysfs)
  302. return -1;
  303. snprintf(path, PATH_MAX,
  304. "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
  305. if (stat(path, &st) < 0)
  306. return -1;
  307. file = fopen(path, "r");
  308. if (!file)
  309. return -EINVAL;
  310. if (1 != fscanf(file, "%u", type))
  311. ret = -1;
  312. fclose(file);
  313. return ret;
  314. }
  315. /* Add all pmus in sysfs to pmu list: */
  316. static void pmu_read_sysfs(void)
  317. {
  318. char path[PATH_MAX];
  319. DIR *dir;
  320. struct dirent *dent;
  321. const char *sysfs = sysfs__mountpoint();
  322. if (!sysfs)
  323. return;
  324. snprintf(path, PATH_MAX,
  325. "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
  326. dir = opendir(path);
  327. if (!dir)
  328. return;
  329. while ((dent = readdir(dir))) {
  330. if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
  331. continue;
  332. /* add to static LIST_HEAD(pmus): */
  333. perf_pmu__find(dent->d_name);
  334. }
  335. closedir(dir);
  336. }
  337. static struct cpu_map *pmu_cpumask(const char *name)
  338. {
  339. struct stat st;
  340. char path[PATH_MAX];
  341. FILE *file;
  342. struct cpu_map *cpus;
  343. const char *sysfs = sysfs__mountpoint();
  344. if (!sysfs)
  345. return NULL;
  346. snprintf(path, PATH_MAX,
  347. "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
  348. if (stat(path, &st) < 0)
  349. return NULL;
  350. file = fopen(path, "r");
  351. if (!file)
  352. return NULL;
  353. cpus = cpu_map__read(file);
  354. fclose(file);
  355. return cpus;
  356. }
  357. struct perf_event_attr * __weak
  358. perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
  359. {
  360. return NULL;
  361. }
  362. static struct perf_pmu *pmu_lookup(const char *name)
  363. {
  364. struct perf_pmu *pmu;
  365. LIST_HEAD(format);
  366. LIST_HEAD(aliases);
  367. __u32 type;
  368. /*
  369. * The pmu data we store & need consists of the pmu
  370. * type value and format definitions. Load both right
  371. * now.
  372. */
  373. if (pmu_format(name, &format))
  374. return NULL;
  375. if (pmu_aliases(name, &aliases))
  376. return NULL;
  377. if (pmu_type(name, &type))
  378. return NULL;
  379. pmu = zalloc(sizeof(*pmu));
  380. if (!pmu)
  381. return NULL;
  382. pmu->cpus = pmu_cpumask(name);
  383. INIT_LIST_HEAD(&pmu->format);
  384. INIT_LIST_HEAD(&pmu->aliases);
  385. list_splice(&format, &pmu->format);
  386. list_splice(&aliases, &pmu->aliases);
  387. pmu->name = strdup(name);
  388. pmu->type = type;
  389. list_add_tail(&pmu->list, &pmus);
  390. pmu->default_config = perf_pmu__get_default_config(pmu);
  391. return pmu;
  392. }
  393. static struct perf_pmu *pmu_find(const char *name)
  394. {
  395. struct perf_pmu *pmu;
  396. list_for_each_entry(pmu, &pmus, list)
  397. if (!strcmp(pmu->name, name))
  398. return pmu;
  399. return NULL;
  400. }
  401. struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
  402. {
  403. /*
  404. * pmu iterator: If pmu is NULL, we start at the begin,
  405. * otherwise return the next pmu. Returns NULL on end.
  406. */
  407. if (!pmu) {
  408. pmu_read_sysfs();
  409. pmu = list_prepare_entry(pmu, &pmus, list);
  410. }
  411. list_for_each_entry_continue(pmu, &pmus, list)
  412. return pmu;
  413. return NULL;
  414. }
  415. struct perf_pmu *perf_pmu__find(const char *name)
  416. {
  417. struct perf_pmu *pmu;
  418. /*
  419. * Once PMU is loaded it stays in the list,
  420. * so we keep us from multiple reading/parsing
  421. * the pmu format definitions.
  422. */
  423. pmu = pmu_find(name);
  424. if (pmu)
  425. return pmu;
  426. return pmu_lookup(name);
  427. }
  428. static struct perf_pmu_format *
  429. pmu_find_format(struct list_head *formats, const char *name)
  430. {
  431. struct perf_pmu_format *format;
  432. list_for_each_entry(format, formats, list)
  433. if (!strcmp(format->name, name))
  434. return format;
  435. return NULL;
  436. }
  437. __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
  438. {
  439. struct perf_pmu_format *format = pmu_find_format(formats, name);
  440. __u64 bits = 0;
  441. int fbit;
  442. if (!format)
  443. return 0;
  444. for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
  445. bits |= 1ULL << fbit;
  446. return bits;
  447. }
  448. /*
  449. * Sets value based on the format definition (format parameter)
  450. * and unformated value (value parameter).
  451. */
  452. static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
  453. bool zero)
  454. {
  455. unsigned long fbit, vbit;
  456. for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
  457. if (!test_bit(fbit, format))
  458. continue;
  459. if (value & (1llu << vbit++))
  460. *v |= (1llu << fbit);
  461. else if (zero)
  462. *v &= ~(1llu << fbit);
  463. }
  464. }
  465. static __u64 pmu_format_max_value(const unsigned long *format)
  466. {
  467. int w;
  468. w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
  469. if (!w)
  470. return 0;
  471. if (w < 64)
  472. return (1ULL << w) - 1;
  473. return -1;
  474. }
  475. /*
  476. * Term is a string term, and might be a param-term. Try to look up it's value
  477. * in the remaining terms.
  478. * - We have a term like "base-or-format-term=param-term",
  479. * - We need to find the value supplied for "param-term" (with param-term named
  480. * in a config string) later on in the term list.
  481. */
  482. static int pmu_resolve_param_term(struct parse_events_term *term,
  483. struct list_head *head_terms,
  484. __u64 *value)
  485. {
  486. struct parse_events_term *t;
  487. list_for_each_entry(t, head_terms, list) {
  488. if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  489. if (!strcmp(t->config, term->config)) {
  490. t->used = true;
  491. *value = t->val.num;
  492. return 0;
  493. }
  494. }
  495. }
  496. if (verbose)
  497. printf("Required parameter '%s' not specified\n", term->config);
  498. return -1;
  499. }
  500. static char *pmu_formats_string(struct list_head *formats)
  501. {
  502. struct perf_pmu_format *format;
  503. char *str;
  504. struct strbuf buf;
  505. unsigned i = 0;
  506. if (!formats)
  507. return NULL;
  508. strbuf_init(&buf, 0);
  509. /* sysfs exported terms */
  510. list_for_each_entry(format, formats, list)
  511. strbuf_addf(&buf, i++ ? ",%s" : "%s",
  512. format->name);
  513. str = strbuf_detach(&buf, NULL);
  514. strbuf_release(&buf);
  515. return str;
  516. }
  517. /*
  518. * Setup one of config[12] attr members based on the
  519. * user input data - term parameter.
  520. */
  521. static int pmu_config_term(struct list_head *formats,
  522. struct perf_event_attr *attr,
  523. struct parse_events_term *term,
  524. struct list_head *head_terms,
  525. bool zero, struct parse_events_error *err)
  526. {
  527. struct perf_pmu_format *format;
  528. __u64 *vp;
  529. __u64 val, max_val;
  530. /*
  531. * If this is a parameter we've already used for parameterized-eval,
  532. * skip it in normal eval.
  533. */
  534. if (term->used)
  535. return 0;
  536. /*
  537. * Hardcoded terms should be already in, so nothing
  538. * to be done for them.
  539. */
  540. if (parse_events__is_hardcoded_term(term))
  541. return 0;
  542. format = pmu_find_format(formats, term->config);
  543. if (!format) {
  544. if (verbose)
  545. printf("Invalid event/parameter '%s'\n", term->config);
  546. if (err) {
  547. char *pmu_term = pmu_formats_string(formats);
  548. err->idx = term->err_term;
  549. err->str = strdup("unknown term");
  550. err->help = parse_events_formats_error_string(pmu_term);
  551. free(pmu_term);
  552. }
  553. return -EINVAL;
  554. }
  555. switch (format->value) {
  556. case PERF_PMU_FORMAT_VALUE_CONFIG:
  557. vp = &attr->config;
  558. break;
  559. case PERF_PMU_FORMAT_VALUE_CONFIG1:
  560. vp = &attr->config1;
  561. break;
  562. case PERF_PMU_FORMAT_VALUE_CONFIG2:
  563. vp = &attr->config2;
  564. break;
  565. default:
  566. return -EINVAL;
  567. }
  568. /*
  569. * Either directly use a numeric term, or try to translate string terms
  570. * using event parameters.
  571. */
  572. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
  573. val = term->val.num;
  574. else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  575. if (strcmp(term->val.str, "?")) {
  576. if (verbose) {
  577. pr_info("Invalid sysfs entry %s=%s\n",
  578. term->config, term->val.str);
  579. }
  580. if (err) {
  581. err->idx = term->err_val;
  582. err->str = strdup("expected numeric value");
  583. }
  584. return -EINVAL;
  585. }
  586. if (pmu_resolve_param_term(term, head_terms, &val))
  587. return -EINVAL;
  588. } else
  589. return -EINVAL;
  590. max_val = pmu_format_max_value(format->bits);
  591. if (val > max_val) {
  592. if (err) {
  593. err->idx = term->err_val;
  594. if (asprintf(&err->str,
  595. "value too big for format, maximum is %llu",
  596. (unsigned long long)max_val) < 0)
  597. err->str = strdup("value too big for format");
  598. return -EINVAL;
  599. }
  600. /*
  601. * Assume we don't care if !err, in which case the value will be
  602. * silently truncated.
  603. */
  604. }
  605. pmu_format_value(format->bits, val, vp, zero);
  606. return 0;
  607. }
  608. int perf_pmu__config_terms(struct list_head *formats,
  609. struct perf_event_attr *attr,
  610. struct list_head *head_terms,
  611. bool zero, struct parse_events_error *err)
  612. {
  613. struct parse_events_term *term;
  614. list_for_each_entry(term, head_terms, list) {
  615. if (pmu_config_term(formats, attr, term, head_terms,
  616. zero, err))
  617. return -EINVAL;
  618. }
  619. return 0;
  620. }
  621. /*
  622. * Configures event's 'attr' parameter based on the:
  623. * 1) users input - specified in terms parameter
  624. * 2) pmu format definitions - specified by pmu parameter
  625. */
  626. int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
  627. struct list_head *head_terms,
  628. struct parse_events_error *err)
  629. {
  630. bool zero = !!pmu->default_config;
  631. attr->type = pmu->type;
  632. return perf_pmu__config_terms(&pmu->format, attr, head_terms,
  633. zero, err);
  634. }
  635. static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
  636. struct parse_events_term *term)
  637. {
  638. struct perf_pmu_alias *alias;
  639. char *name;
  640. if (parse_events__is_hardcoded_term(term))
  641. return NULL;
  642. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  643. if (term->val.num != 1)
  644. return NULL;
  645. if (pmu_find_format(&pmu->format, term->config))
  646. return NULL;
  647. name = term->config;
  648. } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  649. if (strcasecmp(term->config, "event"))
  650. return NULL;
  651. name = term->val.str;
  652. } else {
  653. return NULL;
  654. }
  655. list_for_each_entry(alias, &pmu->aliases, list) {
  656. if (!strcasecmp(alias->name, name))
  657. return alias;
  658. }
  659. return NULL;
  660. }
  661. static int check_info_data(struct perf_pmu_alias *alias,
  662. struct perf_pmu_info *info)
  663. {
  664. /*
  665. * Only one term in event definition can
  666. * define unit, scale and snapshot, fail
  667. * if there's more than one.
  668. */
  669. if ((info->unit && alias->unit) ||
  670. (info->scale && alias->scale) ||
  671. (info->snapshot && alias->snapshot))
  672. return -EINVAL;
  673. if (alias->unit)
  674. info->unit = alias->unit;
  675. if (alias->scale)
  676. info->scale = alias->scale;
  677. if (alias->snapshot)
  678. info->snapshot = alias->snapshot;
  679. return 0;
  680. }
  681. /*
  682. * Find alias in the terms list and replace it with the terms
  683. * defined for the alias
  684. */
  685. int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
  686. struct perf_pmu_info *info)
  687. {
  688. struct parse_events_term *term, *h;
  689. struct perf_pmu_alias *alias;
  690. int ret;
  691. info->per_pkg = false;
  692. /*
  693. * Mark unit and scale as not set
  694. * (different from default values, see below)
  695. */
  696. info->unit = NULL;
  697. info->scale = 0.0;
  698. info->snapshot = false;
  699. list_for_each_entry_safe(term, h, head_terms, list) {
  700. alias = pmu_find_alias(pmu, term);
  701. if (!alias)
  702. continue;
  703. ret = pmu_alias_terms(alias, &term->list);
  704. if (ret)
  705. return ret;
  706. ret = check_info_data(alias, info);
  707. if (ret)
  708. return ret;
  709. if (alias->per_pkg)
  710. info->per_pkg = true;
  711. list_del(&term->list);
  712. free(term);
  713. }
  714. /*
  715. * if no unit or scale foundin aliases, then
  716. * set defaults as for evsel
  717. * unit cannot left to NULL
  718. */
  719. if (info->unit == NULL)
  720. info->unit = "";
  721. if (info->scale == 0.0)
  722. info->scale = 1.0;
  723. return 0;
  724. }
  725. int perf_pmu__new_format(struct list_head *list, char *name,
  726. int config, unsigned long *bits)
  727. {
  728. struct perf_pmu_format *format;
  729. format = zalloc(sizeof(*format));
  730. if (!format)
  731. return -ENOMEM;
  732. format->name = strdup(name);
  733. format->value = config;
  734. memcpy(format->bits, bits, sizeof(format->bits));
  735. list_add_tail(&format->list, list);
  736. return 0;
  737. }
  738. void perf_pmu__set_format(unsigned long *bits, long from, long to)
  739. {
  740. long b;
  741. if (!to)
  742. to = from;
  743. memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
  744. for (b = from; b <= to; b++)
  745. set_bit(b, bits);
  746. }
  747. static int sub_non_neg(int a, int b)
  748. {
  749. if (b > a)
  750. return 0;
  751. return a - b;
  752. }
  753. static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
  754. struct perf_pmu_alias *alias)
  755. {
  756. struct parse_events_term *term;
  757. int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
  758. list_for_each_entry(term, &alias->terms, list) {
  759. if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
  760. used += snprintf(buf + used, sub_non_neg(len, used),
  761. ",%s=%s", term->config,
  762. term->val.str);
  763. }
  764. if (sub_non_neg(len, used) > 0) {
  765. buf[used] = '/';
  766. used++;
  767. }
  768. if (sub_non_neg(len, used) > 0) {
  769. buf[used] = '\0';
  770. used++;
  771. } else
  772. buf[len - 1] = '\0';
  773. return buf;
  774. }
  775. static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
  776. struct perf_pmu_alias *alias)
  777. {
  778. snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
  779. return buf;
  780. }
  781. static int cmp_string(const void *a, const void *b)
  782. {
  783. const char * const *as = a;
  784. const char * const *bs = b;
  785. return strcmp(*as, *bs);
  786. }
  787. void print_pmu_events(const char *event_glob, bool name_only)
  788. {
  789. struct perf_pmu *pmu;
  790. struct perf_pmu_alias *alias;
  791. char buf[1024];
  792. int printed = 0;
  793. int len, j;
  794. char **aliases;
  795. pmu = NULL;
  796. len = 0;
  797. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  798. list_for_each_entry(alias, &pmu->aliases, list)
  799. len++;
  800. if (pmu->selectable)
  801. len++;
  802. }
  803. aliases = zalloc(sizeof(char *) * len);
  804. if (!aliases)
  805. goto out_enomem;
  806. pmu = NULL;
  807. j = 0;
  808. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  809. list_for_each_entry(alias, &pmu->aliases, list) {
  810. char *name = format_alias(buf, sizeof(buf), pmu, alias);
  811. bool is_cpu = !strcmp(pmu->name, "cpu");
  812. if (event_glob != NULL &&
  813. !(strglobmatch(name, event_glob) ||
  814. (!is_cpu && strglobmatch(alias->name,
  815. event_glob))))
  816. continue;
  817. if (is_cpu && !name_only)
  818. name = format_alias_or(buf, sizeof(buf), pmu, alias);
  819. aliases[j] = strdup(name);
  820. if (aliases[j] == NULL)
  821. goto out_enomem;
  822. j++;
  823. }
  824. if (pmu->selectable &&
  825. (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
  826. char *s;
  827. if (asprintf(&s, "%s//", pmu->name) < 0)
  828. goto out_enomem;
  829. aliases[j] = s;
  830. j++;
  831. }
  832. }
  833. len = j;
  834. qsort(aliases, len, sizeof(char *), cmp_string);
  835. for (j = 0; j < len; j++) {
  836. if (name_only) {
  837. printf("%s ", aliases[j]);
  838. continue;
  839. }
  840. printf(" %-50s [Kernel PMU event]\n", aliases[j]);
  841. printed++;
  842. }
  843. if (printed && pager_in_use())
  844. printf("\n");
  845. out_free:
  846. for (j = 0; j < len; j++)
  847. zfree(&aliases[j]);
  848. zfree(&aliases);
  849. return;
  850. out_enomem:
  851. printf("FATAL: not enough memory to print PMU events\n");
  852. if (aliases)
  853. goto out_free;
  854. }
  855. bool pmu_have_event(const char *pname, const char *name)
  856. {
  857. struct perf_pmu *pmu;
  858. struct perf_pmu_alias *alias;
  859. pmu = NULL;
  860. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  861. if (strcmp(pname, pmu->name))
  862. continue;
  863. list_for_each_entry(alias, &pmu->aliases, list)
  864. if (!strcmp(alias->name, name))
  865. return true;
  866. }
  867. return false;
  868. }
  869. static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
  870. {
  871. struct stat st;
  872. char path[PATH_MAX];
  873. const char *sysfs;
  874. sysfs = sysfs__mountpoint();
  875. if (!sysfs)
  876. return NULL;
  877. snprintf(path, PATH_MAX,
  878. "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
  879. if (stat(path, &st) < 0)
  880. return NULL;
  881. return fopen(path, "r");
  882. }
  883. int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
  884. ...)
  885. {
  886. va_list args;
  887. FILE *file;
  888. int ret = EOF;
  889. va_start(args, fmt);
  890. file = perf_pmu__open_file(pmu, name);
  891. if (file) {
  892. ret = vfscanf(file, fmt, args);
  893. fclose(file);
  894. }
  895. va_end(args);
  896. return ret;
  897. }