pmu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. /* No support for intel_bts or intel_pt so disallow them */
  368. if (!strcmp(name, "intel_bts") || !strcmp(name, "intel_pt"))
  369. return NULL;
  370. /*
  371. * The pmu data we store & need consists of the pmu
  372. * type value and format definitions. Load both right
  373. * now.
  374. */
  375. if (pmu_format(name, &format))
  376. return NULL;
  377. if (pmu_aliases(name, &aliases))
  378. return NULL;
  379. if (pmu_type(name, &type))
  380. return NULL;
  381. pmu = zalloc(sizeof(*pmu));
  382. if (!pmu)
  383. return NULL;
  384. pmu->cpus = pmu_cpumask(name);
  385. INIT_LIST_HEAD(&pmu->format);
  386. INIT_LIST_HEAD(&pmu->aliases);
  387. list_splice(&format, &pmu->format);
  388. list_splice(&aliases, &pmu->aliases);
  389. pmu->name = strdup(name);
  390. pmu->type = type;
  391. list_add_tail(&pmu->list, &pmus);
  392. pmu->default_config = perf_pmu__get_default_config(pmu);
  393. return pmu;
  394. }
  395. static struct perf_pmu *pmu_find(const char *name)
  396. {
  397. struct perf_pmu *pmu;
  398. list_for_each_entry(pmu, &pmus, list)
  399. if (!strcmp(pmu->name, name))
  400. return pmu;
  401. return NULL;
  402. }
  403. struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
  404. {
  405. /*
  406. * pmu iterator: If pmu is NULL, we start at the begin,
  407. * otherwise return the next pmu. Returns NULL on end.
  408. */
  409. if (!pmu) {
  410. pmu_read_sysfs();
  411. pmu = list_prepare_entry(pmu, &pmus, list);
  412. }
  413. list_for_each_entry_continue(pmu, &pmus, list)
  414. return pmu;
  415. return NULL;
  416. }
  417. struct perf_pmu *perf_pmu__find(const char *name)
  418. {
  419. struct perf_pmu *pmu;
  420. /*
  421. * Once PMU is loaded it stays in the list,
  422. * so we keep us from multiple reading/parsing
  423. * the pmu format definitions.
  424. */
  425. pmu = pmu_find(name);
  426. if (pmu)
  427. return pmu;
  428. return pmu_lookup(name);
  429. }
  430. static struct perf_pmu_format *
  431. pmu_find_format(struct list_head *formats, char *name)
  432. {
  433. struct perf_pmu_format *format;
  434. list_for_each_entry(format, formats, list)
  435. if (!strcmp(format->name, name))
  436. return format;
  437. return NULL;
  438. }
  439. /*
  440. * Sets value based on the format definition (format parameter)
  441. * and unformated value (value parameter).
  442. */
  443. static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
  444. bool zero)
  445. {
  446. unsigned long fbit, vbit;
  447. for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
  448. if (!test_bit(fbit, format))
  449. continue;
  450. if (value & (1llu << vbit++))
  451. *v |= (1llu << fbit);
  452. else if (zero)
  453. *v &= ~(1llu << fbit);
  454. }
  455. }
  456. /*
  457. * Term is a string term, and might be a param-term. Try to look up it's value
  458. * in the remaining terms.
  459. * - We have a term like "base-or-format-term=param-term",
  460. * - We need to find the value supplied for "param-term" (with param-term named
  461. * in a config string) later on in the term list.
  462. */
  463. static int pmu_resolve_param_term(struct parse_events_term *term,
  464. struct list_head *head_terms,
  465. __u64 *value)
  466. {
  467. struct parse_events_term *t;
  468. list_for_each_entry(t, head_terms, list) {
  469. if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  470. if (!strcmp(t->config, term->config)) {
  471. t->used = true;
  472. *value = t->val.num;
  473. return 0;
  474. }
  475. }
  476. }
  477. if (verbose)
  478. printf("Required parameter '%s' not specified\n", term->config);
  479. return -1;
  480. }
  481. static char *formats_error_string(struct list_head *formats)
  482. {
  483. struct perf_pmu_format *format;
  484. char *err, *str;
  485. static const char *static_terms = "config,config1,config2,name,period,branch_type\n";
  486. unsigned i = 0;
  487. if (!asprintf(&str, "valid terms:"))
  488. return NULL;
  489. /* sysfs exported terms */
  490. list_for_each_entry(format, formats, list) {
  491. char c = i++ ? ',' : ' ';
  492. err = str;
  493. if (!asprintf(&str, "%s%c%s", err, c, format->name))
  494. goto fail;
  495. free(err);
  496. }
  497. /* static terms */
  498. err = str;
  499. if (!asprintf(&str, "%s,%s", err, static_terms))
  500. goto fail;
  501. free(err);
  502. return str;
  503. fail:
  504. free(err);
  505. return NULL;
  506. }
  507. /*
  508. * Setup one of config[12] attr members based on the
  509. * user input data - term parameter.
  510. */
  511. static int pmu_config_term(struct list_head *formats,
  512. struct perf_event_attr *attr,
  513. struct parse_events_term *term,
  514. struct list_head *head_terms,
  515. bool zero, struct parse_events_error *err)
  516. {
  517. struct perf_pmu_format *format;
  518. __u64 *vp;
  519. __u64 val;
  520. /*
  521. * If this is a parameter we've already used for parameterized-eval,
  522. * skip it in normal eval.
  523. */
  524. if (term->used)
  525. return 0;
  526. /*
  527. * Hardcoded terms should be already in, so nothing
  528. * to be done for them.
  529. */
  530. if (parse_events__is_hardcoded_term(term))
  531. return 0;
  532. format = pmu_find_format(formats, term->config);
  533. if (!format) {
  534. if (verbose)
  535. printf("Invalid event/parameter '%s'\n", term->config);
  536. if (err) {
  537. err->idx = term->err_term;
  538. err->str = strdup("unknown term");
  539. err->help = formats_error_string(formats);
  540. }
  541. return -EINVAL;
  542. }
  543. switch (format->value) {
  544. case PERF_PMU_FORMAT_VALUE_CONFIG:
  545. vp = &attr->config;
  546. break;
  547. case PERF_PMU_FORMAT_VALUE_CONFIG1:
  548. vp = &attr->config1;
  549. break;
  550. case PERF_PMU_FORMAT_VALUE_CONFIG2:
  551. vp = &attr->config2;
  552. break;
  553. default:
  554. return -EINVAL;
  555. }
  556. /*
  557. * Either directly use a numeric term, or try to translate string terms
  558. * using event parameters.
  559. */
  560. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
  561. val = term->val.num;
  562. else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  563. if (strcmp(term->val.str, "?")) {
  564. if (verbose) {
  565. pr_info("Invalid sysfs entry %s=%s\n",
  566. term->config, term->val.str);
  567. }
  568. if (err) {
  569. err->idx = term->err_val;
  570. err->str = strdup("expected numeric value");
  571. }
  572. return -EINVAL;
  573. }
  574. if (pmu_resolve_param_term(term, head_terms, &val))
  575. return -EINVAL;
  576. } else
  577. return -EINVAL;
  578. pmu_format_value(format->bits, val, vp, zero);
  579. return 0;
  580. }
  581. int perf_pmu__config_terms(struct list_head *formats,
  582. struct perf_event_attr *attr,
  583. struct list_head *head_terms,
  584. bool zero, struct parse_events_error *err)
  585. {
  586. struct parse_events_term *term;
  587. list_for_each_entry(term, head_terms, list) {
  588. if (pmu_config_term(formats, attr, term, head_terms,
  589. zero, err))
  590. return -EINVAL;
  591. }
  592. return 0;
  593. }
  594. /*
  595. * Configures event's 'attr' parameter based on the:
  596. * 1) users input - specified in terms parameter
  597. * 2) pmu format definitions - specified by pmu parameter
  598. */
  599. int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
  600. struct list_head *head_terms,
  601. struct parse_events_error *err)
  602. {
  603. bool zero = !!pmu->default_config;
  604. attr->type = pmu->type;
  605. return perf_pmu__config_terms(&pmu->format, attr, head_terms,
  606. zero, err);
  607. }
  608. static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
  609. struct parse_events_term *term)
  610. {
  611. struct perf_pmu_alias *alias;
  612. char *name;
  613. if (parse_events__is_hardcoded_term(term))
  614. return NULL;
  615. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  616. if (term->val.num != 1)
  617. return NULL;
  618. if (pmu_find_format(&pmu->format, term->config))
  619. return NULL;
  620. name = term->config;
  621. } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  622. if (strcasecmp(term->config, "event"))
  623. return NULL;
  624. name = term->val.str;
  625. } else {
  626. return NULL;
  627. }
  628. list_for_each_entry(alias, &pmu->aliases, list) {
  629. if (!strcasecmp(alias->name, name))
  630. return alias;
  631. }
  632. return NULL;
  633. }
  634. static int check_info_data(struct perf_pmu_alias *alias,
  635. struct perf_pmu_info *info)
  636. {
  637. /*
  638. * Only one term in event definition can
  639. * define unit, scale and snapshot, fail
  640. * if there's more than one.
  641. */
  642. if ((info->unit && alias->unit) ||
  643. (info->scale && alias->scale) ||
  644. (info->snapshot && alias->snapshot))
  645. return -EINVAL;
  646. if (alias->unit)
  647. info->unit = alias->unit;
  648. if (alias->scale)
  649. info->scale = alias->scale;
  650. if (alias->snapshot)
  651. info->snapshot = alias->snapshot;
  652. return 0;
  653. }
  654. /*
  655. * Find alias in the terms list and replace it with the terms
  656. * defined for the alias
  657. */
  658. int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
  659. struct perf_pmu_info *info)
  660. {
  661. struct parse_events_term *term, *h;
  662. struct perf_pmu_alias *alias;
  663. int ret;
  664. info->per_pkg = false;
  665. /*
  666. * Mark unit and scale as not set
  667. * (different from default values, see below)
  668. */
  669. info->unit = NULL;
  670. info->scale = 0.0;
  671. info->snapshot = false;
  672. list_for_each_entry_safe(term, h, head_terms, list) {
  673. alias = pmu_find_alias(pmu, term);
  674. if (!alias)
  675. continue;
  676. ret = pmu_alias_terms(alias, &term->list);
  677. if (ret)
  678. return ret;
  679. ret = check_info_data(alias, info);
  680. if (ret)
  681. return ret;
  682. if (alias->per_pkg)
  683. info->per_pkg = true;
  684. list_del(&term->list);
  685. free(term);
  686. }
  687. /*
  688. * if no unit or scale foundin aliases, then
  689. * set defaults as for evsel
  690. * unit cannot left to NULL
  691. */
  692. if (info->unit == NULL)
  693. info->unit = "";
  694. if (info->scale == 0.0)
  695. info->scale = 1.0;
  696. return 0;
  697. }
  698. int perf_pmu__new_format(struct list_head *list, char *name,
  699. int config, unsigned long *bits)
  700. {
  701. struct perf_pmu_format *format;
  702. format = zalloc(sizeof(*format));
  703. if (!format)
  704. return -ENOMEM;
  705. format->name = strdup(name);
  706. format->value = config;
  707. memcpy(format->bits, bits, sizeof(format->bits));
  708. list_add_tail(&format->list, list);
  709. return 0;
  710. }
  711. void perf_pmu__set_format(unsigned long *bits, long from, long to)
  712. {
  713. long b;
  714. if (!to)
  715. to = from;
  716. memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
  717. for (b = from; b <= to; b++)
  718. set_bit(b, bits);
  719. }
  720. static int sub_non_neg(int a, int b)
  721. {
  722. if (b > a)
  723. return 0;
  724. return a - b;
  725. }
  726. static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
  727. struct perf_pmu_alias *alias)
  728. {
  729. struct parse_events_term *term;
  730. int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
  731. list_for_each_entry(term, &alias->terms, list) {
  732. if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
  733. used += snprintf(buf + used, sub_non_neg(len, used),
  734. ",%s=%s", term->config,
  735. term->val.str);
  736. }
  737. if (sub_non_neg(len, used) > 0) {
  738. buf[used] = '/';
  739. used++;
  740. }
  741. if (sub_non_neg(len, used) > 0) {
  742. buf[used] = '\0';
  743. used++;
  744. } else
  745. buf[len - 1] = '\0';
  746. return buf;
  747. }
  748. static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
  749. struct perf_pmu_alias *alias)
  750. {
  751. snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
  752. return buf;
  753. }
  754. static int cmp_string(const void *a, const void *b)
  755. {
  756. const char * const *as = a;
  757. const char * const *bs = b;
  758. return strcmp(*as, *bs);
  759. }
  760. void print_pmu_events(const char *event_glob, bool name_only)
  761. {
  762. struct perf_pmu *pmu;
  763. struct perf_pmu_alias *alias;
  764. char buf[1024];
  765. int printed = 0;
  766. int len, j;
  767. char **aliases;
  768. pmu = NULL;
  769. len = 0;
  770. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  771. list_for_each_entry(alias, &pmu->aliases, list)
  772. len++;
  773. if (pmu->selectable)
  774. len++;
  775. }
  776. aliases = zalloc(sizeof(char *) * len);
  777. if (!aliases)
  778. goto out_enomem;
  779. pmu = NULL;
  780. j = 0;
  781. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  782. list_for_each_entry(alias, &pmu->aliases, list) {
  783. char *name = format_alias(buf, sizeof(buf), pmu, alias);
  784. bool is_cpu = !strcmp(pmu->name, "cpu");
  785. if (event_glob != NULL &&
  786. !(strglobmatch(name, event_glob) ||
  787. (!is_cpu && strglobmatch(alias->name,
  788. event_glob))))
  789. continue;
  790. if (is_cpu && !name_only)
  791. name = format_alias_or(buf, sizeof(buf), pmu, alias);
  792. aliases[j] = strdup(name);
  793. if (aliases[j] == NULL)
  794. goto out_enomem;
  795. j++;
  796. }
  797. if (pmu->selectable) {
  798. char *s;
  799. if (asprintf(&s, "%s//", pmu->name) < 0)
  800. goto out_enomem;
  801. aliases[j] = s;
  802. j++;
  803. }
  804. }
  805. len = j;
  806. qsort(aliases, len, sizeof(char *), cmp_string);
  807. for (j = 0; j < len; j++) {
  808. if (name_only) {
  809. printf("%s ", aliases[j]);
  810. continue;
  811. }
  812. printf(" %-50s [Kernel PMU event]\n", aliases[j]);
  813. printed++;
  814. }
  815. if (printed)
  816. printf("\n");
  817. out_free:
  818. for (j = 0; j < len; j++)
  819. zfree(&aliases[j]);
  820. zfree(&aliases);
  821. return;
  822. out_enomem:
  823. printf("FATAL: not enough memory to print PMU events\n");
  824. if (aliases)
  825. goto out_free;
  826. }
  827. bool pmu_have_event(const char *pname, const char *name)
  828. {
  829. struct perf_pmu *pmu;
  830. struct perf_pmu_alias *alias;
  831. pmu = NULL;
  832. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  833. if (strcmp(pname, pmu->name))
  834. continue;
  835. list_for_each_entry(alias, &pmu->aliases, list)
  836. if (!strcmp(alias->name, name))
  837. return true;
  838. }
  839. return false;
  840. }
  841. static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
  842. {
  843. struct stat st;
  844. char path[PATH_MAX];
  845. const char *sysfs;
  846. sysfs = sysfs__mountpoint();
  847. if (!sysfs)
  848. return NULL;
  849. snprintf(path, PATH_MAX,
  850. "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
  851. if (stat(path, &st) < 0)
  852. return NULL;
  853. return fopen(path, "r");
  854. }
  855. int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
  856. ...)
  857. {
  858. va_list args;
  859. FILE *file;
  860. int ret = EOF;
  861. va_start(args, fmt);
  862. file = perf_pmu__open_file(pmu, name);
  863. if (file) {
  864. ret = vfscanf(file, fmt, args);
  865. fclose(file);
  866. }
  867. va_end(args);
  868. return ret;
  869. }