pmu.c 22 KB

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