pmu.c 25 KB

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