pmu.c 22 KB

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