pmu.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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. #include "header.h"
  16. #include "pmu-events/pmu-events.h"
  17. #include "cache.h"
  18. struct perf_pmu_format {
  19. char *name;
  20. int value;
  21. DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS);
  22. struct list_head list;
  23. };
  24. #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/"
  25. int perf_pmu_parse(struct list_head *list, char *name);
  26. extern FILE *perf_pmu_in;
  27. static LIST_HEAD(pmus);
  28. /*
  29. * Parse & process all the sysfs attributes located under
  30. * the directory specified in 'dir' parameter.
  31. */
  32. int perf_pmu__format_parse(char *dir, struct list_head *head)
  33. {
  34. struct dirent *evt_ent;
  35. DIR *format_dir;
  36. int ret = 0;
  37. format_dir = opendir(dir);
  38. if (!format_dir)
  39. return -EINVAL;
  40. while (!ret && (evt_ent = readdir(format_dir))) {
  41. char path[PATH_MAX];
  42. char *name = evt_ent->d_name;
  43. FILE *file;
  44. if (!strcmp(name, ".") || !strcmp(name, ".."))
  45. continue;
  46. snprintf(path, PATH_MAX, "%s/%s", dir, name);
  47. ret = -EINVAL;
  48. file = fopen(path, "r");
  49. if (!file)
  50. break;
  51. perf_pmu_in = file;
  52. ret = perf_pmu_parse(head, name);
  53. fclose(file);
  54. }
  55. closedir(format_dir);
  56. return ret;
  57. }
  58. /*
  59. * Reading/parsing the default pmu format definition, which should be
  60. * located at:
  61. * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes.
  62. */
  63. static int pmu_format(const char *name, struct list_head *format)
  64. {
  65. struct stat st;
  66. char path[PATH_MAX];
  67. const char *sysfs = sysfs__mountpoint();
  68. if (!sysfs)
  69. return -1;
  70. snprintf(path, PATH_MAX,
  71. "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name);
  72. if (stat(path, &st) < 0)
  73. return 0; /* no error if format does not exist */
  74. if (perf_pmu__format_parse(path, format))
  75. return -1;
  76. return 0;
  77. }
  78. static int convert_scale(const char *scale, char **end, double *sval)
  79. {
  80. char *lc;
  81. int ret = 0;
  82. /*
  83. * save current locale
  84. */
  85. lc = setlocale(LC_NUMERIC, NULL);
  86. /*
  87. * The lc string may be allocated in static storage,
  88. * so get a dynamic copy to make it survive setlocale
  89. * call below.
  90. */
  91. lc = strdup(lc);
  92. if (!lc) {
  93. ret = -ENOMEM;
  94. goto out;
  95. }
  96. /*
  97. * force to C locale to ensure kernel
  98. * scale string is converted correctly.
  99. * kernel uses default C locale.
  100. */
  101. setlocale(LC_NUMERIC, "C");
  102. *sval = strtod(scale, end);
  103. out:
  104. /* restore locale */
  105. setlocale(LC_NUMERIC, lc);
  106. free(lc);
  107. return ret;
  108. }
  109. static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name)
  110. {
  111. struct stat st;
  112. ssize_t sret;
  113. char scale[128];
  114. int fd, ret = -1;
  115. char path[PATH_MAX];
  116. snprintf(path, PATH_MAX, "%s/%s.scale", dir, name);
  117. fd = open(path, O_RDONLY);
  118. if (fd == -1)
  119. return -1;
  120. if (fstat(fd, &st) < 0)
  121. goto error;
  122. sret = read(fd, scale, sizeof(scale)-1);
  123. if (sret < 0)
  124. goto error;
  125. if (scale[sret - 1] == '\n')
  126. scale[sret - 1] = '\0';
  127. else
  128. scale[sret] = '\0';
  129. ret = convert_scale(scale, NULL, &alias->scale);
  130. error:
  131. close(fd);
  132. return ret;
  133. }
  134. static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name)
  135. {
  136. char path[PATH_MAX];
  137. ssize_t sret;
  138. int fd;
  139. snprintf(path, PATH_MAX, "%s/%s.unit", dir, name);
  140. fd = open(path, O_RDONLY);
  141. if (fd == -1)
  142. return -1;
  143. sret = read(fd, alias->unit, UNIT_MAX_LEN);
  144. if (sret < 0)
  145. goto error;
  146. close(fd);
  147. if (alias->unit[sret - 1] == '\n')
  148. alias->unit[sret - 1] = '\0';
  149. else
  150. alias->unit[sret] = '\0';
  151. return 0;
  152. error:
  153. close(fd);
  154. alias->unit[0] = '\0';
  155. return -1;
  156. }
  157. static int
  158. perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name)
  159. {
  160. char path[PATH_MAX];
  161. int fd;
  162. snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name);
  163. fd = open(path, O_RDONLY);
  164. if (fd == -1)
  165. return -1;
  166. close(fd);
  167. alias->per_pkg = true;
  168. return 0;
  169. }
  170. static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
  171. char *dir, char *name)
  172. {
  173. char path[PATH_MAX];
  174. int fd;
  175. snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name);
  176. fd = open(path, O_RDONLY);
  177. if (fd == -1)
  178. return -1;
  179. alias->snapshot = true;
  180. close(fd);
  181. return 0;
  182. }
  183. static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
  184. char *desc, char *val,
  185. char *long_desc, char *topic,
  186. char *unit, char *perpkg,
  187. char *metric_expr,
  188. char *metric_name)
  189. {
  190. struct perf_pmu_alias *alias;
  191. int ret;
  192. int num;
  193. alias = malloc(sizeof(*alias));
  194. if (!alias)
  195. return -ENOMEM;
  196. INIT_LIST_HEAD(&alias->terms);
  197. alias->scale = 1.0;
  198. alias->unit[0] = '\0';
  199. alias->per_pkg = false;
  200. alias->snapshot = false;
  201. ret = parse_events_terms(&alias->terms, val);
  202. if (ret) {
  203. pr_err("Cannot parse alias %s: %d\n", val, ret);
  204. free(alias);
  205. return ret;
  206. }
  207. alias->name = strdup(name);
  208. if (dir) {
  209. /*
  210. * load unit name and scale if available
  211. */
  212. perf_pmu__parse_unit(alias, dir, name);
  213. perf_pmu__parse_scale(alias, dir, name);
  214. perf_pmu__parse_per_pkg(alias, dir, name);
  215. perf_pmu__parse_snapshot(alias, dir, name);
  216. }
  217. alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL;
  218. alias->metric_name = metric_name ? strdup(metric_name): NULL;
  219. alias->desc = desc ? strdup(desc) : NULL;
  220. alias->long_desc = long_desc ? strdup(long_desc) :
  221. desc ? strdup(desc) : NULL;
  222. alias->topic = topic ? strdup(topic) : NULL;
  223. if (unit) {
  224. if (convert_scale(unit, &unit, &alias->scale) < 0)
  225. return -1;
  226. snprintf(alias->unit, sizeof(alias->unit), "%s", unit);
  227. }
  228. alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1;
  229. alias->str = strdup(val);
  230. list_add_tail(&alias->list, list);
  231. return 0;
  232. }
  233. static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file)
  234. {
  235. char buf[256];
  236. int ret;
  237. ret = fread(buf, 1, sizeof(buf), file);
  238. if (ret == 0)
  239. return -EINVAL;
  240. buf[ret] = 0;
  241. return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL,
  242. NULL, NULL, NULL);
  243. }
  244. static inline bool pmu_alias_info_file(char *name)
  245. {
  246. size_t len;
  247. len = strlen(name);
  248. if (len > 5 && !strcmp(name + len - 5, ".unit"))
  249. return true;
  250. if (len > 6 && !strcmp(name + len - 6, ".scale"))
  251. return true;
  252. if (len > 8 && !strcmp(name + len - 8, ".per-pkg"))
  253. return true;
  254. if (len > 9 && !strcmp(name + len - 9, ".snapshot"))
  255. return true;
  256. return false;
  257. }
  258. /*
  259. * Process all the sysfs attributes located under the directory
  260. * specified in 'dir' parameter.
  261. */
  262. static int pmu_aliases_parse(char *dir, struct list_head *head)
  263. {
  264. struct dirent *evt_ent;
  265. DIR *event_dir;
  266. event_dir = opendir(dir);
  267. if (!event_dir)
  268. return -EINVAL;
  269. while ((evt_ent = readdir(event_dir))) {
  270. char path[PATH_MAX];
  271. char *name = evt_ent->d_name;
  272. FILE *file;
  273. if (!strcmp(name, ".") || !strcmp(name, ".."))
  274. continue;
  275. /*
  276. * skip info files parsed in perf_pmu__new_alias()
  277. */
  278. if (pmu_alias_info_file(name))
  279. continue;
  280. snprintf(path, PATH_MAX, "%s/%s", dir, name);
  281. file = fopen(path, "r");
  282. if (!file) {
  283. pr_debug("Cannot open %s\n", path);
  284. continue;
  285. }
  286. if (perf_pmu__new_alias(head, dir, name, file) < 0)
  287. pr_debug("Cannot set up %s\n", name);
  288. fclose(file);
  289. }
  290. closedir(event_dir);
  291. return 0;
  292. }
  293. /*
  294. * Reading the pmu event aliases definition, which should be located at:
  295. * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes.
  296. */
  297. static int pmu_aliases(const char *name, struct list_head *head)
  298. {
  299. struct stat st;
  300. char path[PATH_MAX];
  301. const char *sysfs = sysfs__mountpoint();
  302. if (!sysfs)
  303. return -1;
  304. snprintf(path, PATH_MAX,
  305. "%s/bus/event_source/devices/%s/events", sysfs, name);
  306. if (stat(path, &st) < 0)
  307. return 0; /* no error if 'events' does not exist */
  308. if (pmu_aliases_parse(path, head))
  309. return -1;
  310. return 0;
  311. }
  312. static int pmu_alias_terms(struct perf_pmu_alias *alias,
  313. struct list_head *terms)
  314. {
  315. struct parse_events_term *term, *cloned;
  316. LIST_HEAD(list);
  317. int ret;
  318. list_for_each_entry(term, &alias->terms, list) {
  319. ret = parse_events_term__clone(&cloned, term);
  320. if (ret) {
  321. parse_events_terms__purge(&list);
  322. return ret;
  323. }
  324. list_add_tail(&cloned->list, &list);
  325. }
  326. list_splice(&list, terms);
  327. return 0;
  328. }
  329. /*
  330. * Reading/parsing the default pmu type value, which should be
  331. * located at:
  332. * /sys/bus/event_source/devices/<dev>/type as sysfs attribute.
  333. */
  334. static int pmu_type(const char *name, __u32 *type)
  335. {
  336. struct stat st;
  337. char path[PATH_MAX];
  338. FILE *file;
  339. int ret = 0;
  340. const char *sysfs = sysfs__mountpoint();
  341. if (!sysfs)
  342. return -1;
  343. snprintf(path, PATH_MAX,
  344. "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name);
  345. if (stat(path, &st) < 0)
  346. return -1;
  347. file = fopen(path, "r");
  348. if (!file)
  349. return -EINVAL;
  350. if (1 != fscanf(file, "%u", type))
  351. ret = -1;
  352. fclose(file);
  353. return ret;
  354. }
  355. /* Add all pmus in sysfs to pmu list: */
  356. static void pmu_read_sysfs(void)
  357. {
  358. char path[PATH_MAX];
  359. DIR *dir;
  360. struct dirent *dent;
  361. const char *sysfs = sysfs__mountpoint();
  362. if (!sysfs)
  363. return;
  364. snprintf(path, PATH_MAX,
  365. "%s" EVENT_SOURCE_DEVICE_PATH, sysfs);
  366. dir = opendir(path);
  367. if (!dir)
  368. return;
  369. while ((dent = readdir(dir))) {
  370. if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
  371. continue;
  372. /* add to static LIST_HEAD(pmus): */
  373. perf_pmu__find(dent->d_name);
  374. }
  375. closedir(dir);
  376. }
  377. static struct cpu_map *pmu_cpumask(const char *name)
  378. {
  379. struct stat st;
  380. char path[PATH_MAX];
  381. FILE *file;
  382. struct cpu_map *cpus;
  383. const char *sysfs = sysfs__mountpoint();
  384. const char *templates[] = {
  385. "%s/bus/event_source/devices/%s/cpumask",
  386. "%s/bus/event_source/devices/%s/cpus",
  387. NULL
  388. };
  389. const char **template;
  390. if (!sysfs)
  391. return NULL;
  392. for (template = templates; *template; template++) {
  393. snprintf(path, PATH_MAX, *template, sysfs, name);
  394. if (stat(path, &st) == 0)
  395. break;
  396. }
  397. if (!*template)
  398. return NULL;
  399. file = fopen(path, "r");
  400. if (!file)
  401. return NULL;
  402. cpus = cpu_map__read(file);
  403. fclose(file);
  404. return cpus;
  405. }
  406. /*
  407. * Return the CPU id as a raw string.
  408. *
  409. * Each architecture should provide a more precise id string that
  410. * can be use to match the architecture's "mapfile".
  411. */
  412. char * __weak get_cpuid_str(void)
  413. {
  414. return NULL;
  415. }
  416. /*
  417. * From the pmu_events_map, find the table of PMU events that corresponds
  418. * to the current running CPU. Then, add all PMU events from that table
  419. * as aliases.
  420. */
  421. static void pmu_add_cpu_aliases(struct list_head *head, const char *name)
  422. {
  423. int i;
  424. struct pmu_events_map *map;
  425. struct pmu_event *pe;
  426. char *cpuid;
  427. static bool printed;
  428. cpuid = getenv("PERF_CPUID");
  429. if (cpuid)
  430. cpuid = strdup(cpuid);
  431. if (!cpuid)
  432. cpuid = get_cpuid_str();
  433. if (!cpuid)
  434. return;
  435. if (!printed) {
  436. pr_debug("Using CPUID %s\n", cpuid);
  437. printed = true;
  438. }
  439. i = 0;
  440. while (1) {
  441. map = &pmu_events_map[i++];
  442. if (!map->table)
  443. goto out;
  444. if (!strcmp(map->cpuid, cpuid))
  445. break;
  446. }
  447. /*
  448. * Found a matching PMU events table. Create aliases
  449. */
  450. i = 0;
  451. while (1) {
  452. const char *pname;
  453. pe = &map->table[i++];
  454. if (!pe->name)
  455. break;
  456. pname = pe->pmu ? pe->pmu : "cpu";
  457. if (strncmp(pname, name, strlen(pname)))
  458. continue;
  459. /* need type casts to override 'const' */
  460. __perf_pmu__new_alias(head, NULL, (char *)pe->name,
  461. (char *)pe->desc, (char *)pe->event,
  462. (char *)pe->long_desc, (char *)pe->topic,
  463. (char *)pe->unit, (char *)pe->perpkg,
  464. (char *)pe->metric_expr,
  465. (char *)pe->metric_name);
  466. }
  467. out:
  468. free(cpuid);
  469. }
  470. struct perf_event_attr * __weak
  471. perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
  472. {
  473. return NULL;
  474. }
  475. static struct perf_pmu *pmu_lookup(const char *name)
  476. {
  477. struct perf_pmu *pmu;
  478. LIST_HEAD(format);
  479. LIST_HEAD(aliases);
  480. __u32 type;
  481. /*
  482. * The pmu data we store & need consists of the pmu
  483. * type value and format definitions. Load both right
  484. * now.
  485. */
  486. if (pmu_format(name, &format))
  487. return NULL;
  488. /*
  489. * Check the type first to avoid unnecessary work.
  490. */
  491. if (pmu_type(name, &type))
  492. return NULL;
  493. if (pmu_aliases(name, &aliases))
  494. return NULL;
  495. pmu_add_cpu_aliases(&aliases, name);
  496. pmu = zalloc(sizeof(*pmu));
  497. if (!pmu)
  498. return NULL;
  499. pmu->cpus = pmu_cpumask(name);
  500. INIT_LIST_HEAD(&pmu->format);
  501. INIT_LIST_HEAD(&pmu->aliases);
  502. list_splice(&format, &pmu->format);
  503. list_splice(&aliases, &pmu->aliases);
  504. pmu->name = strdup(name);
  505. pmu->type = type;
  506. list_add_tail(&pmu->list, &pmus);
  507. pmu->default_config = perf_pmu__get_default_config(pmu);
  508. return pmu;
  509. }
  510. static struct perf_pmu *pmu_find(const char *name)
  511. {
  512. struct perf_pmu *pmu;
  513. list_for_each_entry(pmu, &pmus, list)
  514. if (!strcmp(pmu->name, name))
  515. return pmu;
  516. return NULL;
  517. }
  518. struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
  519. {
  520. /*
  521. * pmu iterator: If pmu is NULL, we start at the begin,
  522. * otherwise return the next pmu. Returns NULL on end.
  523. */
  524. if (!pmu) {
  525. pmu_read_sysfs();
  526. pmu = list_prepare_entry(pmu, &pmus, list);
  527. }
  528. list_for_each_entry_continue(pmu, &pmus, list)
  529. return pmu;
  530. return NULL;
  531. }
  532. struct perf_pmu *perf_pmu__find(const char *name)
  533. {
  534. struct perf_pmu *pmu;
  535. /*
  536. * Once PMU is loaded it stays in the list,
  537. * so we keep us from multiple reading/parsing
  538. * the pmu format definitions.
  539. */
  540. pmu = pmu_find(name);
  541. if (pmu)
  542. return pmu;
  543. return pmu_lookup(name);
  544. }
  545. static struct perf_pmu_format *
  546. pmu_find_format(struct list_head *formats, const char *name)
  547. {
  548. struct perf_pmu_format *format;
  549. list_for_each_entry(format, formats, list)
  550. if (!strcmp(format->name, name))
  551. return format;
  552. return NULL;
  553. }
  554. __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
  555. {
  556. struct perf_pmu_format *format = pmu_find_format(formats, name);
  557. __u64 bits = 0;
  558. int fbit;
  559. if (!format)
  560. return 0;
  561. for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
  562. bits |= 1ULL << fbit;
  563. return bits;
  564. }
  565. /*
  566. * Sets value based on the format definition (format parameter)
  567. * and unformated value (value parameter).
  568. */
  569. static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
  570. bool zero)
  571. {
  572. unsigned long fbit, vbit;
  573. for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
  574. if (!test_bit(fbit, format))
  575. continue;
  576. if (value & (1llu << vbit++))
  577. *v |= (1llu << fbit);
  578. else if (zero)
  579. *v &= ~(1llu << fbit);
  580. }
  581. }
  582. static __u64 pmu_format_max_value(const unsigned long *format)
  583. {
  584. __u64 w = 0;
  585. int fbit;
  586. for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
  587. w |= (1ULL << fbit);
  588. return w;
  589. }
  590. /*
  591. * Term is a string term, and might be a param-term. Try to look up it's value
  592. * in the remaining terms.
  593. * - We have a term like "base-or-format-term=param-term",
  594. * - We need to find the value supplied for "param-term" (with param-term named
  595. * in a config string) later on in the term list.
  596. */
  597. static int pmu_resolve_param_term(struct parse_events_term *term,
  598. struct list_head *head_terms,
  599. __u64 *value)
  600. {
  601. struct parse_events_term *t;
  602. list_for_each_entry(t, head_terms, list) {
  603. if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  604. if (!strcmp(t->config, term->config)) {
  605. t->used = true;
  606. *value = t->val.num;
  607. return 0;
  608. }
  609. }
  610. }
  611. if (verbose > 0)
  612. printf("Required parameter '%s' not specified\n", term->config);
  613. return -1;
  614. }
  615. static char *pmu_formats_string(struct list_head *formats)
  616. {
  617. struct perf_pmu_format *format;
  618. char *str = NULL;
  619. struct strbuf buf = STRBUF_INIT;
  620. unsigned i = 0;
  621. if (!formats)
  622. return NULL;
  623. /* sysfs exported terms */
  624. list_for_each_entry(format, formats, list)
  625. if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
  626. goto error;
  627. str = strbuf_detach(&buf, NULL);
  628. error:
  629. strbuf_release(&buf);
  630. return str;
  631. }
  632. /*
  633. * Setup one of config[12] attr members based on the
  634. * user input data - term parameter.
  635. */
  636. static int pmu_config_term(struct list_head *formats,
  637. struct perf_event_attr *attr,
  638. struct parse_events_term *term,
  639. struct list_head *head_terms,
  640. bool zero, struct parse_events_error *err)
  641. {
  642. struct perf_pmu_format *format;
  643. __u64 *vp;
  644. __u64 val, max_val;
  645. /*
  646. * If this is a parameter we've already used for parameterized-eval,
  647. * skip it in normal eval.
  648. */
  649. if (term->used)
  650. return 0;
  651. /*
  652. * Hardcoded terms should be already in, so nothing
  653. * to be done for them.
  654. */
  655. if (parse_events__is_hardcoded_term(term))
  656. return 0;
  657. format = pmu_find_format(formats, term->config);
  658. if (!format) {
  659. if (verbose > 0)
  660. printf("Invalid event/parameter '%s'\n", term->config);
  661. if (err) {
  662. char *pmu_term = pmu_formats_string(formats);
  663. err->idx = term->err_term;
  664. err->str = strdup("unknown term");
  665. err->help = parse_events_formats_error_string(pmu_term);
  666. free(pmu_term);
  667. }
  668. return -EINVAL;
  669. }
  670. switch (format->value) {
  671. case PERF_PMU_FORMAT_VALUE_CONFIG:
  672. vp = &attr->config;
  673. break;
  674. case PERF_PMU_FORMAT_VALUE_CONFIG1:
  675. vp = &attr->config1;
  676. break;
  677. case PERF_PMU_FORMAT_VALUE_CONFIG2:
  678. vp = &attr->config2;
  679. break;
  680. default:
  681. return -EINVAL;
  682. }
  683. /*
  684. * Either directly use a numeric term, or try to translate string terms
  685. * using event parameters.
  686. */
  687. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  688. if (term->no_value &&
  689. bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) {
  690. if (err) {
  691. err->idx = term->err_val;
  692. err->str = strdup("no value assigned for term");
  693. }
  694. return -EINVAL;
  695. }
  696. val = term->val.num;
  697. } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  698. if (strcmp(term->val.str, "?")) {
  699. if (verbose > 0) {
  700. pr_info("Invalid sysfs entry %s=%s\n",
  701. term->config, term->val.str);
  702. }
  703. if (err) {
  704. err->idx = term->err_val;
  705. err->str = strdup("expected numeric value");
  706. }
  707. return -EINVAL;
  708. }
  709. if (pmu_resolve_param_term(term, head_terms, &val))
  710. return -EINVAL;
  711. } else
  712. return -EINVAL;
  713. max_val = pmu_format_max_value(format->bits);
  714. if (val > max_val) {
  715. if (err) {
  716. err->idx = term->err_val;
  717. if (asprintf(&err->str,
  718. "value too big for format, maximum is %llu",
  719. (unsigned long long)max_val) < 0)
  720. err->str = strdup("value too big for format");
  721. return -EINVAL;
  722. }
  723. /*
  724. * Assume we don't care if !err, in which case the value will be
  725. * silently truncated.
  726. */
  727. }
  728. pmu_format_value(format->bits, val, vp, zero);
  729. return 0;
  730. }
  731. int perf_pmu__config_terms(struct list_head *formats,
  732. struct perf_event_attr *attr,
  733. struct list_head *head_terms,
  734. bool zero, struct parse_events_error *err)
  735. {
  736. struct parse_events_term *term;
  737. list_for_each_entry(term, head_terms, list) {
  738. if (pmu_config_term(formats, attr, term, head_terms,
  739. zero, err))
  740. return -EINVAL;
  741. }
  742. return 0;
  743. }
  744. /*
  745. * Configures event's 'attr' parameter based on the:
  746. * 1) users input - specified in terms parameter
  747. * 2) pmu format definitions - specified by pmu parameter
  748. */
  749. int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
  750. struct list_head *head_terms,
  751. struct parse_events_error *err)
  752. {
  753. bool zero = !!pmu->default_config;
  754. attr->type = pmu->type;
  755. return perf_pmu__config_terms(&pmu->format, attr, head_terms,
  756. zero, err);
  757. }
  758. static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
  759. struct parse_events_term *term)
  760. {
  761. struct perf_pmu_alias *alias;
  762. char *name;
  763. if (parse_events__is_hardcoded_term(term))
  764. return NULL;
  765. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  766. if (term->val.num != 1)
  767. return NULL;
  768. if (pmu_find_format(&pmu->format, term->config))
  769. return NULL;
  770. name = term->config;
  771. } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  772. if (strcasecmp(term->config, "event"))
  773. return NULL;
  774. name = term->val.str;
  775. } else {
  776. return NULL;
  777. }
  778. list_for_each_entry(alias, &pmu->aliases, list) {
  779. if (!strcasecmp(alias->name, name))
  780. return alias;
  781. }
  782. return NULL;
  783. }
  784. static int check_info_data(struct perf_pmu_alias *alias,
  785. struct perf_pmu_info *info)
  786. {
  787. /*
  788. * Only one term in event definition can
  789. * define unit, scale and snapshot, fail
  790. * if there's more than one.
  791. */
  792. if ((info->unit && alias->unit[0]) ||
  793. (info->scale && alias->scale) ||
  794. (info->snapshot && alias->snapshot))
  795. return -EINVAL;
  796. if (alias->unit[0])
  797. info->unit = alias->unit;
  798. if (alias->scale)
  799. info->scale = alias->scale;
  800. if (alias->snapshot)
  801. info->snapshot = alias->snapshot;
  802. return 0;
  803. }
  804. /*
  805. * Find alias in the terms list and replace it with the terms
  806. * defined for the alias
  807. */
  808. int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
  809. struct perf_pmu_info *info)
  810. {
  811. struct parse_events_term *term, *h;
  812. struct perf_pmu_alias *alias;
  813. int ret;
  814. info->per_pkg = false;
  815. /*
  816. * Mark unit and scale as not set
  817. * (different from default values, see below)
  818. */
  819. info->unit = NULL;
  820. info->scale = 0.0;
  821. info->snapshot = false;
  822. info->metric_expr = NULL;
  823. info->metric_name = NULL;
  824. list_for_each_entry_safe(term, h, head_terms, list) {
  825. alias = pmu_find_alias(pmu, term);
  826. if (!alias)
  827. continue;
  828. ret = pmu_alias_terms(alias, &term->list);
  829. if (ret)
  830. return ret;
  831. ret = check_info_data(alias, info);
  832. if (ret)
  833. return ret;
  834. if (alias->per_pkg)
  835. info->per_pkg = true;
  836. info->metric_expr = alias->metric_expr;
  837. info->metric_name = alias->metric_name;
  838. list_del(&term->list);
  839. free(term);
  840. }
  841. /*
  842. * if no unit or scale foundin aliases, then
  843. * set defaults as for evsel
  844. * unit cannot left to NULL
  845. */
  846. if (info->unit == NULL)
  847. info->unit = "";
  848. if (info->scale == 0.0)
  849. info->scale = 1.0;
  850. return 0;
  851. }
  852. int perf_pmu__new_format(struct list_head *list, char *name,
  853. int config, unsigned long *bits)
  854. {
  855. struct perf_pmu_format *format;
  856. format = zalloc(sizeof(*format));
  857. if (!format)
  858. return -ENOMEM;
  859. format->name = strdup(name);
  860. format->value = config;
  861. memcpy(format->bits, bits, sizeof(format->bits));
  862. list_add_tail(&format->list, list);
  863. return 0;
  864. }
  865. void perf_pmu__set_format(unsigned long *bits, long from, long to)
  866. {
  867. long b;
  868. if (!to)
  869. to = from;
  870. memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
  871. for (b = from; b <= to; b++)
  872. set_bit(b, bits);
  873. }
  874. static int sub_non_neg(int a, int b)
  875. {
  876. if (b > a)
  877. return 0;
  878. return a - b;
  879. }
  880. static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
  881. struct perf_pmu_alias *alias)
  882. {
  883. struct parse_events_term *term;
  884. int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
  885. list_for_each_entry(term, &alias->terms, list) {
  886. if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
  887. used += snprintf(buf + used, sub_non_neg(len, used),
  888. ",%s=%s", term->config,
  889. term->val.str);
  890. }
  891. if (sub_non_neg(len, used) > 0) {
  892. buf[used] = '/';
  893. used++;
  894. }
  895. if (sub_non_neg(len, used) > 0) {
  896. buf[used] = '\0';
  897. used++;
  898. } else
  899. buf[len - 1] = '\0';
  900. return buf;
  901. }
  902. static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
  903. struct perf_pmu_alias *alias)
  904. {
  905. snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
  906. return buf;
  907. }
  908. struct sevent {
  909. char *name;
  910. char *desc;
  911. char *topic;
  912. char *str;
  913. char *pmu;
  914. char *metric_expr;
  915. char *metric_name;
  916. };
  917. static int cmp_sevent(const void *a, const void *b)
  918. {
  919. const struct sevent *as = a;
  920. const struct sevent *bs = b;
  921. /* Put extra events last */
  922. if (!!as->desc != !!bs->desc)
  923. return !!as->desc - !!bs->desc;
  924. if (as->topic && bs->topic) {
  925. int n = strcmp(as->topic, bs->topic);
  926. if (n)
  927. return n;
  928. }
  929. return strcmp(as->name, bs->name);
  930. }
  931. static void wordwrap(char *s, int start, int max, int corr)
  932. {
  933. int column = start;
  934. int n;
  935. while (*s) {
  936. int wlen = strcspn(s, " \t");
  937. if (column + wlen >= max && column > start) {
  938. printf("\n%*s", start, "");
  939. column = start + corr;
  940. }
  941. n = printf("%s%.*s", column > start ? " " : "", wlen, s);
  942. if (n <= 0)
  943. break;
  944. s += wlen;
  945. column += n;
  946. s = ltrim(s);
  947. }
  948. }
  949. void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
  950. bool long_desc, bool details_flag)
  951. {
  952. struct perf_pmu *pmu;
  953. struct perf_pmu_alias *alias;
  954. char buf[1024];
  955. int printed = 0;
  956. int len, j;
  957. struct sevent *aliases;
  958. int numdesc = 0;
  959. int columns = pager_get_columns();
  960. char *topic = NULL;
  961. pmu = NULL;
  962. len = 0;
  963. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  964. list_for_each_entry(alias, &pmu->aliases, list)
  965. len++;
  966. if (pmu->selectable)
  967. len++;
  968. }
  969. aliases = zalloc(sizeof(struct sevent) * len);
  970. if (!aliases)
  971. goto out_enomem;
  972. pmu = NULL;
  973. j = 0;
  974. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  975. list_for_each_entry(alias, &pmu->aliases, list) {
  976. char *name = alias->desc ? alias->name :
  977. format_alias(buf, sizeof(buf), pmu, alias);
  978. bool is_cpu = !strcmp(pmu->name, "cpu");
  979. if (event_glob != NULL &&
  980. !(strglobmatch_nocase(name, event_glob) ||
  981. (!is_cpu && strglobmatch_nocase(alias->name,
  982. event_glob)) ||
  983. (alias->topic &&
  984. strglobmatch_nocase(alias->topic, event_glob))))
  985. continue;
  986. if (is_cpu && !name_only && !alias->desc)
  987. name = format_alias_or(buf, sizeof(buf), pmu, alias);
  988. aliases[j].name = name;
  989. if (is_cpu && !name_only && !alias->desc)
  990. aliases[j].name = format_alias_or(buf,
  991. sizeof(buf),
  992. pmu, alias);
  993. aliases[j].name = strdup(aliases[j].name);
  994. if (!aliases[j].name)
  995. goto out_enomem;
  996. aliases[j].desc = long_desc ? alias->long_desc :
  997. alias->desc;
  998. aliases[j].topic = alias->topic;
  999. aliases[j].str = alias->str;
  1000. aliases[j].pmu = pmu->name;
  1001. aliases[j].metric_expr = alias->metric_expr;
  1002. aliases[j].metric_name = alias->metric_name;
  1003. j++;
  1004. }
  1005. if (pmu->selectable &&
  1006. (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
  1007. char *s;
  1008. if (asprintf(&s, "%s//", pmu->name) < 0)
  1009. goto out_enomem;
  1010. aliases[j].name = s;
  1011. j++;
  1012. }
  1013. }
  1014. len = j;
  1015. qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
  1016. for (j = 0; j < len; j++) {
  1017. /* Skip duplicates */
  1018. if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name))
  1019. continue;
  1020. if (name_only) {
  1021. printf("%s ", aliases[j].name);
  1022. continue;
  1023. }
  1024. if (aliases[j].desc && !quiet_flag) {
  1025. if (numdesc++ == 0)
  1026. printf("\n");
  1027. if (aliases[j].topic && (!topic ||
  1028. strcmp(topic, aliases[j].topic))) {
  1029. printf("%s%s:\n", topic ? "\n" : "",
  1030. aliases[j].topic);
  1031. topic = aliases[j].topic;
  1032. }
  1033. printf(" %-50s\n", aliases[j].name);
  1034. printf("%*s", 8, "[");
  1035. wordwrap(aliases[j].desc, 8, columns, 0);
  1036. printf("]\n");
  1037. if (details_flag) {
  1038. printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str);
  1039. if (aliases[j].metric_name)
  1040. printf(" MetricName: %s", aliases[j].metric_name);
  1041. if (aliases[j].metric_expr)
  1042. printf(" MetricExpr: %s", aliases[j].metric_expr);
  1043. putchar('\n');
  1044. }
  1045. } else
  1046. printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
  1047. printed++;
  1048. }
  1049. if (printed && pager_in_use())
  1050. printf("\n");
  1051. out_free:
  1052. for (j = 0; j < len; j++)
  1053. zfree(&aliases[j].name);
  1054. zfree(&aliases);
  1055. return;
  1056. out_enomem:
  1057. printf("FATAL: not enough memory to print PMU events\n");
  1058. if (aliases)
  1059. goto out_free;
  1060. }
  1061. bool pmu_have_event(const char *pname, const char *name)
  1062. {
  1063. struct perf_pmu *pmu;
  1064. struct perf_pmu_alias *alias;
  1065. pmu = NULL;
  1066. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  1067. if (strcmp(pname, pmu->name))
  1068. continue;
  1069. list_for_each_entry(alias, &pmu->aliases, list)
  1070. if (!strcmp(alias->name, name))
  1071. return true;
  1072. }
  1073. return false;
  1074. }
  1075. static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
  1076. {
  1077. struct stat st;
  1078. char path[PATH_MAX];
  1079. const char *sysfs;
  1080. sysfs = sysfs__mountpoint();
  1081. if (!sysfs)
  1082. return NULL;
  1083. snprintf(path, PATH_MAX,
  1084. "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
  1085. if (stat(path, &st) < 0)
  1086. return NULL;
  1087. return fopen(path, "r");
  1088. }
  1089. int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
  1090. ...)
  1091. {
  1092. va_list args;
  1093. FILE *file;
  1094. int ret = EOF;
  1095. va_start(args, fmt);
  1096. file = perf_pmu__open_file(pmu, name);
  1097. if (file) {
  1098. ret = vfscanf(file, fmt, args);
  1099. fclose(file);
  1100. }
  1101. va_end(args);
  1102. return ret;
  1103. }