pmu.c 28 KB

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