pmu.c 37 KB

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