pmu.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. }