pmu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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. static bool printed;
  408. cpuid = getenv("PERF_CPUID");
  409. if (cpuid)
  410. cpuid = strdup(cpuid);
  411. if (!cpuid)
  412. cpuid = get_cpuid_str();
  413. if (!cpuid)
  414. return;
  415. if (!printed) {
  416. pr_debug("Using CPUID %s\n", cpuid);
  417. printed = true;
  418. }
  419. i = 0;
  420. while (1) {
  421. map = &pmu_events_map[i++];
  422. if (!map->table)
  423. goto out;
  424. if (!strcmp(map->cpuid, cpuid))
  425. break;
  426. }
  427. /*
  428. * Found a matching PMU events table. Create aliases
  429. */
  430. i = 0;
  431. while (1) {
  432. pe = &map->table[i++];
  433. if (!pe->name)
  434. break;
  435. /* need type casts to override 'const' */
  436. __perf_pmu__new_alias(head, NULL, (char *)pe->name,
  437. (char *)pe->desc, (char *)pe->event,
  438. (char *)pe->long_desc, (char *)pe->topic);
  439. }
  440. out:
  441. free(cpuid);
  442. }
  443. struct perf_event_attr * __weak
  444. perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
  445. {
  446. return NULL;
  447. }
  448. static struct perf_pmu *pmu_lookup(const char *name)
  449. {
  450. struct perf_pmu *pmu;
  451. LIST_HEAD(format);
  452. LIST_HEAD(aliases);
  453. __u32 type;
  454. /*
  455. * The pmu data we store & need consists of the pmu
  456. * type value and format definitions. Load both right
  457. * now.
  458. */
  459. if (pmu_format(name, &format))
  460. return NULL;
  461. if (pmu_aliases(name, &aliases))
  462. return NULL;
  463. if (!strcmp(name, "cpu"))
  464. pmu_add_cpu_aliases(&aliases);
  465. if (pmu_type(name, &type))
  466. return NULL;
  467. pmu = zalloc(sizeof(*pmu));
  468. if (!pmu)
  469. return NULL;
  470. pmu->cpus = pmu_cpumask(name);
  471. INIT_LIST_HEAD(&pmu->format);
  472. INIT_LIST_HEAD(&pmu->aliases);
  473. list_splice(&format, &pmu->format);
  474. list_splice(&aliases, &pmu->aliases);
  475. pmu->name = strdup(name);
  476. pmu->type = type;
  477. list_add_tail(&pmu->list, &pmus);
  478. pmu->default_config = perf_pmu__get_default_config(pmu);
  479. return pmu;
  480. }
  481. static struct perf_pmu *pmu_find(const char *name)
  482. {
  483. struct perf_pmu *pmu;
  484. list_for_each_entry(pmu, &pmus, list)
  485. if (!strcmp(pmu->name, name))
  486. return pmu;
  487. return NULL;
  488. }
  489. struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
  490. {
  491. /*
  492. * pmu iterator: If pmu is NULL, we start at the begin,
  493. * otherwise return the next pmu. Returns NULL on end.
  494. */
  495. if (!pmu) {
  496. pmu_read_sysfs();
  497. pmu = list_prepare_entry(pmu, &pmus, list);
  498. }
  499. list_for_each_entry_continue(pmu, &pmus, list)
  500. return pmu;
  501. return NULL;
  502. }
  503. struct perf_pmu *perf_pmu__find(const char *name)
  504. {
  505. struct perf_pmu *pmu;
  506. /*
  507. * Once PMU is loaded it stays in the list,
  508. * so we keep us from multiple reading/parsing
  509. * the pmu format definitions.
  510. */
  511. pmu = pmu_find(name);
  512. if (pmu)
  513. return pmu;
  514. return pmu_lookup(name);
  515. }
  516. static struct perf_pmu_format *
  517. pmu_find_format(struct list_head *formats, const char *name)
  518. {
  519. struct perf_pmu_format *format;
  520. list_for_each_entry(format, formats, list)
  521. if (!strcmp(format->name, name))
  522. return format;
  523. return NULL;
  524. }
  525. __u64 perf_pmu__format_bits(struct list_head *formats, const char *name)
  526. {
  527. struct perf_pmu_format *format = pmu_find_format(formats, name);
  528. __u64 bits = 0;
  529. int fbit;
  530. if (!format)
  531. return 0;
  532. for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS)
  533. bits |= 1ULL << fbit;
  534. return bits;
  535. }
  536. /*
  537. * Sets value based on the format definition (format parameter)
  538. * and unformated value (value parameter).
  539. */
  540. static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
  541. bool zero)
  542. {
  543. unsigned long fbit, vbit;
  544. for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) {
  545. if (!test_bit(fbit, format))
  546. continue;
  547. if (value & (1llu << vbit++))
  548. *v |= (1llu << fbit);
  549. else if (zero)
  550. *v &= ~(1llu << fbit);
  551. }
  552. }
  553. static __u64 pmu_format_max_value(const unsigned long *format)
  554. {
  555. __u64 w = 0;
  556. int fbit;
  557. for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
  558. w |= (1ULL << fbit);
  559. return w;
  560. }
  561. /*
  562. * Term is a string term, and might be a param-term. Try to look up it's value
  563. * in the remaining terms.
  564. * - We have a term like "base-or-format-term=param-term",
  565. * - We need to find the value supplied for "param-term" (with param-term named
  566. * in a config string) later on in the term list.
  567. */
  568. static int pmu_resolve_param_term(struct parse_events_term *term,
  569. struct list_head *head_terms,
  570. __u64 *value)
  571. {
  572. struct parse_events_term *t;
  573. list_for_each_entry(t, head_terms, list) {
  574. if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  575. if (!strcmp(t->config, term->config)) {
  576. t->used = true;
  577. *value = t->val.num;
  578. return 0;
  579. }
  580. }
  581. }
  582. if (verbose)
  583. printf("Required parameter '%s' not specified\n", term->config);
  584. return -1;
  585. }
  586. static char *pmu_formats_string(struct list_head *formats)
  587. {
  588. struct perf_pmu_format *format;
  589. char *str = NULL;
  590. struct strbuf buf = STRBUF_INIT;
  591. unsigned i = 0;
  592. if (!formats)
  593. return NULL;
  594. /* sysfs exported terms */
  595. list_for_each_entry(format, formats, list)
  596. if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
  597. goto error;
  598. str = strbuf_detach(&buf, NULL);
  599. error:
  600. strbuf_release(&buf);
  601. return str;
  602. }
  603. /*
  604. * Setup one of config[12] attr members based on the
  605. * user input data - term parameter.
  606. */
  607. static int pmu_config_term(struct list_head *formats,
  608. struct perf_event_attr *attr,
  609. struct parse_events_term *term,
  610. struct list_head *head_terms,
  611. bool zero, struct parse_events_error *err)
  612. {
  613. struct perf_pmu_format *format;
  614. __u64 *vp;
  615. __u64 val, max_val;
  616. /*
  617. * If this is a parameter we've already used for parameterized-eval,
  618. * skip it in normal eval.
  619. */
  620. if (term->used)
  621. return 0;
  622. /*
  623. * Hardcoded terms should be already in, so nothing
  624. * to be done for them.
  625. */
  626. if (parse_events__is_hardcoded_term(term))
  627. return 0;
  628. format = pmu_find_format(formats, term->config);
  629. if (!format) {
  630. if (verbose)
  631. printf("Invalid event/parameter '%s'\n", term->config);
  632. if (err) {
  633. char *pmu_term = pmu_formats_string(formats);
  634. err->idx = term->err_term;
  635. err->str = strdup("unknown term");
  636. err->help = parse_events_formats_error_string(pmu_term);
  637. free(pmu_term);
  638. }
  639. return -EINVAL;
  640. }
  641. switch (format->value) {
  642. case PERF_PMU_FORMAT_VALUE_CONFIG:
  643. vp = &attr->config;
  644. break;
  645. case PERF_PMU_FORMAT_VALUE_CONFIG1:
  646. vp = &attr->config1;
  647. break;
  648. case PERF_PMU_FORMAT_VALUE_CONFIG2:
  649. vp = &attr->config2;
  650. break;
  651. default:
  652. return -EINVAL;
  653. }
  654. /*
  655. * Either directly use a numeric term, or try to translate string terms
  656. * using event parameters.
  657. */
  658. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
  659. val = term->val.num;
  660. else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  661. if (strcmp(term->val.str, "?")) {
  662. if (verbose) {
  663. pr_info("Invalid sysfs entry %s=%s\n",
  664. term->config, term->val.str);
  665. }
  666. if (err) {
  667. err->idx = term->err_val;
  668. err->str = strdup("expected numeric value");
  669. }
  670. return -EINVAL;
  671. }
  672. if (pmu_resolve_param_term(term, head_terms, &val))
  673. return -EINVAL;
  674. } else
  675. return -EINVAL;
  676. max_val = pmu_format_max_value(format->bits);
  677. if (val > max_val) {
  678. if (err) {
  679. err->idx = term->err_val;
  680. if (asprintf(&err->str,
  681. "value too big for format, maximum is %llu",
  682. (unsigned long long)max_val) < 0)
  683. err->str = strdup("value too big for format");
  684. return -EINVAL;
  685. }
  686. /*
  687. * Assume we don't care if !err, in which case the value will be
  688. * silently truncated.
  689. */
  690. }
  691. pmu_format_value(format->bits, val, vp, zero);
  692. return 0;
  693. }
  694. int perf_pmu__config_terms(struct list_head *formats,
  695. struct perf_event_attr *attr,
  696. struct list_head *head_terms,
  697. bool zero, struct parse_events_error *err)
  698. {
  699. struct parse_events_term *term;
  700. list_for_each_entry(term, head_terms, list) {
  701. if (pmu_config_term(formats, attr, term, head_terms,
  702. zero, err))
  703. return -EINVAL;
  704. }
  705. return 0;
  706. }
  707. /*
  708. * Configures event's 'attr' parameter based on the:
  709. * 1) users input - specified in terms parameter
  710. * 2) pmu format definitions - specified by pmu parameter
  711. */
  712. int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
  713. struct list_head *head_terms,
  714. struct parse_events_error *err)
  715. {
  716. bool zero = !!pmu->default_config;
  717. attr->type = pmu->type;
  718. return perf_pmu__config_terms(&pmu->format, attr, head_terms,
  719. zero, err);
  720. }
  721. static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
  722. struct parse_events_term *term)
  723. {
  724. struct perf_pmu_alias *alias;
  725. char *name;
  726. if (parse_events__is_hardcoded_term(term))
  727. return NULL;
  728. if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) {
  729. if (term->val.num != 1)
  730. return NULL;
  731. if (pmu_find_format(&pmu->format, term->config))
  732. return NULL;
  733. name = term->config;
  734. } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
  735. if (strcasecmp(term->config, "event"))
  736. return NULL;
  737. name = term->val.str;
  738. } else {
  739. return NULL;
  740. }
  741. list_for_each_entry(alias, &pmu->aliases, list) {
  742. if (!strcasecmp(alias->name, name))
  743. return alias;
  744. }
  745. return NULL;
  746. }
  747. static int check_info_data(struct perf_pmu_alias *alias,
  748. struct perf_pmu_info *info)
  749. {
  750. /*
  751. * Only one term in event definition can
  752. * define unit, scale and snapshot, fail
  753. * if there's more than one.
  754. */
  755. if ((info->unit && alias->unit) ||
  756. (info->scale && alias->scale) ||
  757. (info->snapshot && alias->snapshot))
  758. return -EINVAL;
  759. if (alias->unit)
  760. info->unit = alias->unit;
  761. if (alias->scale)
  762. info->scale = alias->scale;
  763. if (alias->snapshot)
  764. info->snapshot = alias->snapshot;
  765. return 0;
  766. }
  767. /*
  768. * Find alias in the terms list and replace it with the terms
  769. * defined for the alias
  770. */
  771. int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
  772. struct perf_pmu_info *info)
  773. {
  774. struct parse_events_term *term, *h;
  775. struct perf_pmu_alias *alias;
  776. int ret;
  777. info->per_pkg = false;
  778. /*
  779. * Mark unit and scale as not set
  780. * (different from default values, see below)
  781. */
  782. info->unit = NULL;
  783. info->scale = 0.0;
  784. info->snapshot = false;
  785. list_for_each_entry_safe(term, h, head_terms, list) {
  786. alias = pmu_find_alias(pmu, term);
  787. if (!alias)
  788. continue;
  789. ret = pmu_alias_terms(alias, &term->list);
  790. if (ret)
  791. return ret;
  792. ret = check_info_data(alias, info);
  793. if (ret)
  794. return ret;
  795. if (alias->per_pkg)
  796. info->per_pkg = true;
  797. list_del(&term->list);
  798. free(term);
  799. }
  800. /*
  801. * if no unit or scale foundin aliases, then
  802. * set defaults as for evsel
  803. * unit cannot left to NULL
  804. */
  805. if (info->unit == NULL)
  806. info->unit = "";
  807. if (info->scale == 0.0)
  808. info->scale = 1.0;
  809. return 0;
  810. }
  811. int perf_pmu__new_format(struct list_head *list, char *name,
  812. int config, unsigned long *bits)
  813. {
  814. struct perf_pmu_format *format;
  815. format = zalloc(sizeof(*format));
  816. if (!format)
  817. return -ENOMEM;
  818. format->name = strdup(name);
  819. format->value = config;
  820. memcpy(format->bits, bits, sizeof(format->bits));
  821. list_add_tail(&format->list, list);
  822. return 0;
  823. }
  824. void perf_pmu__set_format(unsigned long *bits, long from, long to)
  825. {
  826. long b;
  827. if (!to)
  828. to = from;
  829. memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS));
  830. for (b = from; b <= to; b++)
  831. set_bit(b, bits);
  832. }
  833. static int sub_non_neg(int a, int b)
  834. {
  835. if (b > a)
  836. return 0;
  837. return a - b;
  838. }
  839. static char *format_alias(char *buf, int len, struct perf_pmu *pmu,
  840. struct perf_pmu_alias *alias)
  841. {
  842. struct parse_events_term *term;
  843. int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
  844. list_for_each_entry(term, &alias->terms, list) {
  845. if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
  846. used += snprintf(buf + used, sub_non_neg(len, used),
  847. ",%s=%s", term->config,
  848. term->val.str);
  849. }
  850. if (sub_non_neg(len, used) > 0) {
  851. buf[used] = '/';
  852. used++;
  853. }
  854. if (sub_non_neg(len, used) > 0) {
  855. buf[used] = '\0';
  856. used++;
  857. } else
  858. buf[len - 1] = '\0';
  859. return buf;
  860. }
  861. static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
  862. struct perf_pmu_alias *alias)
  863. {
  864. snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name);
  865. return buf;
  866. }
  867. struct sevent {
  868. char *name;
  869. char *desc;
  870. char *topic;
  871. };
  872. static int cmp_sevent(const void *a, const void *b)
  873. {
  874. const struct sevent *as = a;
  875. const struct sevent *bs = b;
  876. /* Put extra events last */
  877. if (!!as->desc != !!bs->desc)
  878. return !!as->desc - !!bs->desc;
  879. if (as->topic && bs->topic) {
  880. int n = strcmp(as->topic, bs->topic);
  881. if (n)
  882. return n;
  883. }
  884. return strcmp(as->name, bs->name);
  885. }
  886. static void wordwrap(char *s, int start, int max, int corr)
  887. {
  888. int column = start;
  889. int n;
  890. while (*s) {
  891. int wlen = strcspn(s, " \t");
  892. if (column + wlen >= max && column > start) {
  893. printf("\n%*s", start, "");
  894. column = start + corr;
  895. }
  896. n = printf("%s%.*s", column > start ? " " : "", wlen, s);
  897. if (n <= 0)
  898. break;
  899. s += wlen;
  900. column += n;
  901. while (isspace(*s))
  902. s++;
  903. }
  904. }
  905. void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag,
  906. bool long_desc)
  907. {
  908. struct perf_pmu *pmu;
  909. struct perf_pmu_alias *alias;
  910. char buf[1024];
  911. int printed = 0;
  912. int len, j;
  913. struct sevent *aliases;
  914. int numdesc = 0;
  915. int columns = pager_get_columns();
  916. char *topic = NULL;
  917. pmu = NULL;
  918. len = 0;
  919. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  920. list_for_each_entry(alias, &pmu->aliases, list)
  921. len++;
  922. if (pmu->selectable)
  923. len++;
  924. }
  925. aliases = zalloc(sizeof(struct sevent) * len);
  926. if (!aliases)
  927. goto out_enomem;
  928. pmu = NULL;
  929. j = 0;
  930. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  931. list_for_each_entry(alias, &pmu->aliases, list) {
  932. char *name = alias->desc ? alias->name :
  933. format_alias(buf, sizeof(buf), pmu, alias);
  934. bool is_cpu = !strcmp(pmu->name, "cpu");
  935. if (event_glob != NULL &&
  936. !(strglobmatch_nocase(name, event_glob) ||
  937. (!is_cpu && strglobmatch_nocase(alias->name,
  938. event_glob)) ||
  939. (alias->topic &&
  940. strglobmatch_nocase(alias->topic, event_glob))))
  941. continue;
  942. if (is_cpu && !name_only && !alias->desc)
  943. name = format_alias_or(buf, sizeof(buf), pmu, alias);
  944. aliases[j].name = name;
  945. if (is_cpu && !name_only && !alias->desc)
  946. aliases[j].name = format_alias_or(buf,
  947. sizeof(buf),
  948. pmu, alias);
  949. aliases[j].name = strdup(aliases[j].name);
  950. if (!aliases[j].name)
  951. goto out_enomem;
  952. aliases[j].desc = long_desc ? alias->long_desc :
  953. alias->desc;
  954. aliases[j].topic = alias->topic;
  955. j++;
  956. }
  957. if (pmu->selectable &&
  958. (event_glob == NULL || strglobmatch(pmu->name, event_glob))) {
  959. char *s;
  960. if (asprintf(&s, "%s//", pmu->name) < 0)
  961. goto out_enomem;
  962. aliases[j].name = s;
  963. j++;
  964. }
  965. }
  966. len = j;
  967. qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
  968. for (j = 0; j < len; j++) {
  969. if (name_only) {
  970. printf("%s ", aliases[j].name);
  971. continue;
  972. }
  973. if (aliases[j].desc && !quiet_flag) {
  974. if (numdesc++ == 0)
  975. printf("\n");
  976. if (aliases[j].topic && (!topic ||
  977. strcmp(topic, aliases[j].topic))) {
  978. printf("%s%s:\n", topic ? "\n" : "",
  979. aliases[j].topic);
  980. topic = aliases[j].topic;
  981. }
  982. printf(" %-50s\n", aliases[j].name);
  983. printf("%*s", 8, "[");
  984. wordwrap(aliases[j].desc, 8, columns, 0);
  985. printf("]\n");
  986. } else
  987. printf(" %-50s [Kernel PMU event]\n", aliases[j].name);
  988. printed++;
  989. }
  990. if (printed && pager_in_use())
  991. printf("\n");
  992. out_free:
  993. for (j = 0; j < len; j++)
  994. zfree(&aliases[j].name);
  995. zfree(&aliases);
  996. return;
  997. out_enomem:
  998. printf("FATAL: not enough memory to print PMU events\n");
  999. if (aliases)
  1000. goto out_free;
  1001. }
  1002. bool pmu_have_event(const char *pname, const char *name)
  1003. {
  1004. struct perf_pmu *pmu;
  1005. struct perf_pmu_alias *alias;
  1006. pmu = NULL;
  1007. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  1008. if (strcmp(pname, pmu->name))
  1009. continue;
  1010. list_for_each_entry(alias, &pmu->aliases, list)
  1011. if (!strcmp(alias->name, name))
  1012. return true;
  1013. }
  1014. return false;
  1015. }
  1016. static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
  1017. {
  1018. struct stat st;
  1019. char path[PATH_MAX];
  1020. const char *sysfs;
  1021. sysfs = sysfs__mountpoint();
  1022. if (!sysfs)
  1023. return NULL;
  1024. snprintf(path, PATH_MAX,
  1025. "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
  1026. if (stat(path, &st) < 0)
  1027. return NULL;
  1028. return fopen(path, "r");
  1029. }
  1030. int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
  1031. ...)
  1032. {
  1033. va_list args;
  1034. FILE *file;
  1035. int ret = EOF;
  1036. va_start(args, fmt);
  1037. file = perf_pmu__open_file(pmu, name);
  1038. if (file) {
  1039. ret = vfscanf(file, fmt, args);
  1040. fclose(file);
  1041. }
  1042. va_end(args);
  1043. return ret;
  1044. }