pmu.c 30 KB

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