pmu.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. int w;
  480. w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
  481. if (!w)
  482. return 0;
  483. if (w < 64)
  484. return (1ULL << w) - 1;
  485. return -1;
  486. }
  487. /*
  488. * Term is a string term, and might be a param-term. Try to look up it's value
  489. * in the remaining terms.
  490. * - We have a term like "base-or-format-term=param-term",
  491. * - We need to find the value supplied for "param-term" (with param-term named
  492. * in a config string) later on in the term list.
  493. */
  494. static int pmu_resolve_param_term(struct parse_events_term *term,
  495. struct list_head *head_terms,
  496. __u64 *value)
  497. {
  498. struct parse_events_term *t;
  499. list_for_each_entry(t, head_terms, list) {
  500. if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  501. if (!strcmp(t->config, term->config)) {
  502. t->used = true;
  503. *value = t->val.num;
  504. return 0;
  505. }
  506. }
  507. }
  508. if (verbose)
  509. printf("Required parameter '%s' not specified\n", term->config);
  510. return -1;
  511. }
  512. static char *pmu_formats_string(struct list_head *formats)
  513. {
  514. struct perf_pmu_format *format;
  515. char *str;
  516. struct strbuf buf;
  517. unsigned i = 0;
  518. if (!formats)
  519. return NULL;
  520. strbuf_init(&buf, 0);
  521. /* sysfs exported terms */
  522. list_for_each_entry(format, formats, list)
  523. strbuf_addf(&buf, i++ ? ",%s" : "%s",
  524. format->name);
  525. str = strbuf_detach(&buf, NULL);
  526. strbuf_release(&buf);
  527. return str;
  528. }
  529. /*
  530. * Setup one of config[12] attr members based on the
  531. * user input data - term parameter.
  532. */
  533. static int pmu_config_term(struct list_head *formats,
  534. struct perf_event_attr *attr,
  535. struct parse_events_term *term,
  536. struct list_head *head_terms,
  537. bool zero, struct parse_events_error *err)
  538. {
  539. struct perf_pmu_format *format;
  540. __u64 *vp;
  541. __u64 val, max_val;
  542. /*
  543. * If this is a parameter we've already used for parameterized-eval,
  544. * skip it in normal eval.
  545. */
  546. if (term->used)
  547. return 0;
  548. /*
  549. * Hardcoded terms should be already in, so nothing
  550. * to be done for them.
  551. */
  552. if (parse_events__is_hardcoded_term(term))
  553. return 0;
  554. format = pmu_find_format(formats, term->config);
  555. if (!format) {
  556. if (verbose)
  557. printf("Invalid event/parameter '%s'\n", term->config);
  558. if (err) {
  559. char *pmu_term = pmu_formats_string(formats);
  560. err->idx = term->err_term;
  561. err->str = strdup("unknown term");
  562. err->help = parse_events_formats_error_string(pmu_term);
  563. free(pmu_term);
  564. }
  565. return -EINVAL;
  566. }
  567. switch (format->value) {
  568. case PERF_PMU_FORMAT_VALUE_CONFIG:
  569. vp = &attr->config;
  570. break;
  571. case PERF_PMU_FORMAT_VALUE_CONFIG1:
  572. vp = &attr->config1;
  573. break;
  574. case PERF_PMU_FORMAT_VALUE_CONFIG2:
  575. vp = &attr->config2;
  576. break;
  577. default:
  578. return -EINVAL;
  579. }
  580. /*
  581. * Either directly use a numeric term, or try to translate string terms
  582. * using event parameters.
  583. */
  584. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
  585. val = term->val.num;
  586. else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  587. if (strcmp(term->val.str, "?")) {
  588. if (verbose) {
  589. pr_info("Invalid sysfs entry %s=%s\n",
  590. term->config, term->val.str);
  591. }
  592. if (err) {
  593. err->idx = term->err_val;
  594. err->str = strdup("expected numeric value");
  595. }
  596. return -EINVAL;
  597. }
  598. if (pmu_resolve_param_term(term, head_terms, &val))
  599. return -EINVAL;
  600. } else
  601. return -EINVAL;
  602. max_val = pmu_format_max_value(format->bits);
  603. if (val > max_val) {
  604. if (err) {
  605. err->idx = term->err_val;
  606. if (asprintf(&err->str,
  607. "value too big for format, maximum is %llu",
  608. (unsigned long long)max_val) < 0)
  609. err->str = strdup("value too big for format");
  610. return -EINVAL;
  611. }
  612. /*
  613. * Assume we don't care if !err, in which case the value will be
  614. * silently truncated.
  615. */
  616. }
  617. pmu_format_value(format->bits, val, vp, zero);
  618. return 0;
  619. }
  620. int perf_pmu__config_terms(struct list_head *formats,
  621. struct perf_event_attr *attr,
  622. struct list_head *head_terms,
  623. bool zero, struct parse_events_error *err)
  624. {
  625. struct parse_events_term *term;
  626. list_for_each_entry(term, head_terms, list) {
  627. if (pmu_config_term(formats, attr, term, head_terms,
  628. zero, err))
  629. return -EINVAL;
  630. }
  631. return 0;
  632. }
  633. /*
  634. * Configures event's 'attr' parameter based on the:
  635. * 1) users input - specified in terms parameter
  636. * 2) pmu format definitions - specified by pmu parameter
  637. */
  638. int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
  639. struct list_head *head_terms,
  640. struct parse_events_error *err)
  641. {
  642. bool zero = !!pmu->default_config;
  643. attr->type = pmu->type;
  644. return perf_pmu__config_terms(&pmu->format, attr, head_terms,
  645. zero, err);
  646. }
  647. static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
  648. struct parse_events_term *term)
  649. {
  650. struct perf_pmu_alias *alias;
  651. char *name;
  652. if (parse_events__is_hardcoded_term(term))
  653. return NULL;
  654. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  655. if (term->val.num != 1)
  656. return NULL;
  657. if (pmu_find_format(&pmu->format, term->config))
  658. return NULL;
  659. name = term->config;
  660. } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  661. if (strcasecmp(term->config, "event"))
  662. return NULL;
  663. name = term->val.str;
  664. } else {
  665. return NULL;
  666. }
  667. list_for_each_entry(alias, &pmu->aliases, list) {
  668. if (!strcasecmp(alias->name, name))
  669. return alias;
  670. }
  671. return NULL;
  672. }
  673. static int check_info_data(struct perf_pmu_alias *alias,
  674. struct perf_pmu_info *info)
  675. {
  676. /*
  677. * Only one term in event definition can
  678. * define unit, scale and snapshot, fail
  679. * if there's more than one.
  680. */
  681. if ((info->unit && alias->unit) ||
  682. (info->scale && alias->scale) ||
  683. (info->snapshot && alias->snapshot))
  684. return -EINVAL;
  685. if (alias->unit)
  686. info->unit = alias->unit;
  687. if (alias->scale)
  688. info->scale = alias->scale;
  689. if (alias->snapshot)
  690. info->snapshot = alias->snapshot;
  691. return 0;
  692. }
  693. /*
  694. * Find alias in the terms list and replace it with the terms
  695. * defined for the alias
  696. */
  697. int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
  698. struct perf_pmu_info *info)
  699. {
  700. struct parse_events_term *term, *h;
  701. struct perf_pmu_alias *alias;
  702. int ret;
  703. info->per_pkg = false;
  704. /*
  705. * Mark unit and scale as not set
  706. * (different from default values, see below)
  707. */
  708. info->unit = NULL;
  709. info->scale = 0.0;
  710. info->snapshot = false;
  711. list_for_each_entry_safe(term, h, head_terms, list) {
  712. alias = pmu_find_alias(pmu, term);
  713. if (!alias)
  714. continue;
  715. ret = pmu_alias_terms(alias, &term->list);
  716. if (ret)
  717. return ret;
  718. ret = check_info_data(alias, info);
  719. if (ret)
  720. return ret;
  721. if (alias->per_pkg)
  722. info->per_pkg = true;
  723. list_del(&term->list);
  724. free(term);
  725. }
  726. /*
  727. * if no unit or scale foundin aliases, then
  728. * set defaults as for evsel
  729. * unit cannot left to NULL
  730. */
  731. if (info->unit == NULL)
  732. info->unit = "";
  733. if (info->scale == 0.0)
  734. info->scale = 1.0;
  735. return 0;
  736. }
  737. int perf_pmu__new_format(struct list_head *list, char *name,
  738. int config, unsigned long *bits)
  739. {
  740. struct perf_pmu_format *format;
  741. format = zalloc(sizeof(*format));
  742. if (!format)
  743. return -ENOMEM;
  744. format->name = strdup(name);
  745. format->value = config;
  746. memcpy(format->bits, bits, sizeof(format->bits));
  747. list_add_tail(&format->list, list);
  748. return 0;
  749. }
  750. void perf_pmu__set_format(unsigned long *bits, long from, long to)
  751. {
  752. long b;
  753. if (!to)
  754. to = from;
  755. memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
  756. for (b = from; b <= to; b++)
  757. set_bit(b, bits);
  758. }
  759. static int sub_non_neg(int a, int b)
  760. {
  761. if (b > a)
  762. return 0;
  763. return a - b;
  764. }
  765. static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
  766. struct perf_pmu_alias *alias)
  767. {
  768. struct parse_events_term *term;
  769. int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
  770. list_for_each_entry(term, &alias->terms, list) {
  771. if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
  772. used += snprintf(buf + used, sub_non_neg(len, used),
  773. ",%s=%s", term->config,
  774. term->val.str);
  775. }
  776. if (sub_non_neg(len, used) > 0) {
  777. buf[used] = '/';
  778. used++;
  779. }
  780. if (sub_non_neg(len, used) > 0) {
  781. buf[used] = '\0';
  782. used++;
  783. } else
  784. buf[len - 1] = '\0';
  785. return buf;
  786. }
  787. static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
  788. struct perf_pmu_alias *alias)
  789. {
  790. snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
  791. return buf;
  792. }
  793. static int cmp_string(const void *a, const void *b)
  794. {
  795. const char * const *as = a;
  796. const char * const *bs = b;
  797. return strcmp(*as, *bs);
  798. }
  799. void print_pmu_events(const char *event_glob, bool name_only)
  800. {
  801. struct perf_pmu *pmu;
  802. struct perf_pmu_alias *alias;
  803. char buf[1024];
  804. int printed = 0;
  805. int len, j;
  806. char **aliases;
  807. pmu = NULL;
  808. len = 0;
  809. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  810. list_for_each_entry(alias, &pmu->aliases, list)
  811. len++;
  812. if (pmu->selectable)
  813. len++;
  814. }
  815. aliases = zalloc(sizeof(char *) * len);
  816. if (!aliases)
  817. goto out_enomem;
  818. pmu = NULL;
  819. j = 0;
  820. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  821. list_for_each_entry(alias, &pmu->aliases, list) {
  822. char *name = format_alias(buf, sizeof(buf), pmu, alias);
  823. bool is_cpu = !strcmp(pmu->name, "cpu");
  824. if (event_glob != NULL &&
  825. !(strglobmatch(name, event_glob) ||
  826. (!is_cpu && strglobmatch(alias->name,
  827. event_glob))))
  828. continue;
  829. if (is_cpu && !name_only)
  830. name = format_alias_or(buf, sizeof(buf), pmu, alias);
  831. aliases[j] = strdup(name);
  832. if (aliases[j] == NULL)
  833. goto out_enomem;
  834. j++;
  835. }
  836. if (pmu->selectable &&
  837. (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
  838. char *s;
  839. if (asprintf(&s, "%s//", pmu->name) < 0)
  840. goto out_enomem;
  841. aliases[j] = s;
  842. j++;
  843. }
  844. }
  845. len = j;
  846. qsort(aliases, len, sizeof(char *), cmp_string);
  847. for (j = 0; j < len; j++) {
  848. if (name_only) {
  849. printf("%s ", aliases[j]);
  850. continue;
  851. }
  852. printf(" %-50s [Kernel PMU event]\n", aliases[j]);
  853. printed++;
  854. }
  855. if (printed && pager_in_use())
  856. printf("\n");
  857. out_free:
  858. for (j = 0; j < len; j++)
  859. zfree(&aliases[j]);
  860. zfree(&aliases);
  861. return;
  862. out_enomem:
  863. printf("FATAL: not enough memory to print PMU events\n");
  864. if (aliases)
  865. goto out_free;
  866. }
  867. bool pmu_have_event(const char *pname, const char *name)
  868. {
  869. struct perf_pmu *pmu;
  870. struct perf_pmu_alias *alias;
  871. pmu = NULL;
  872. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  873. if (strcmp(pname, pmu->name))
  874. continue;
  875. list_for_each_entry(alias, &pmu->aliases, list)
  876. if (!strcmp(alias->name, name))
  877. return true;
  878. }
  879. return false;
  880. }
  881. static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
  882. {
  883. struct stat st;
  884. char path[PATH_MAX];
  885. const char *sysfs;
  886. sysfs = sysfs__mountpoint();
  887. if (!sysfs)
  888. return NULL;
  889. snprintf(path, PATH_MAX,
  890. "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
  891. if (stat(path, &st) < 0)
  892. return NULL;
  893. return fopen(path, "r");
  894. }
  895. int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
  896. ...)
  897. {
  898. va_list args;
  899. FILE *file;
  900. int ret = EOF;
  901. va_start(args, fmt);
  902. file = perf_pmu__open_file(pmu, name);
  903. if (file) {
  904. ret = vfscanf(file, fmt, args);
  905. fclose(file);
  906. }
  907. va_end(args);
  908. return ret;
  909. }