pmu.c 22 KB

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