1cd82a32eSJiri Olsa #include <linux/list.h> 2c5de47f2SSukadev Bhattiprolu #include <linux/compiler.h> 3cd82a32eSJiri Olsa #include <sys/types.h> 4cd82a32eSJiri Olsa #include <unistd.h> 5cd82a32eSJiri Olsa #include <stdio.h> 6dc0a6202SAdrian Hunter #include <stdbool.h> 77d4bdab5SAdrian Hunter #include <stdarg.h> 8cd82a32eSJiri Olsa #include <dirent.h> 9cd0cfad7SBorislav Petkov #include <api/fs/fs.h> 10410136f5SStephane Eranian #include <locale.h> 11cd82a32eSJiri Olsa #include "util.h" 12cd82a32eSJiri Olsa #include "pmu.h" 13cd82a32eSJiri Olsa #include "parse-events.h" 147ae92e74SYan, Zheng #include "cpumap.h" 15cd82a32eSJiri Olsa 16ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 17ab1bf653SArnaldo Carvalho de Melo char *name; 18ab1bf653SArnaldo Carvalho de Melo int value; 19ab1bf653SArnaldo Carvalho de Melo DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 20ab1bf653SArnaldo Carvalho de Melo struct list_head list; 21ab1bf653SArnaldo Carvalho de Melo }; 22ab1bf653SArnaldo Carvalho de Melo 2350a9667cSRobert Richter #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/" 2450a9667cSRobert Richter 25cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name); 26cd82a32eSJiri Olsa extern FILE *perf_pmu_in; 27cd82a32eSJiri Olsa 28cd82a32eSJiri Olsa static LIST_HEAD(pmus); 29cd82a32eSJiri Olsa 30cd82a32eSJiri Olsa /* 31cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 32cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 33cd82a32eSJiri Olsa */ 34cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head) 35cd82a32eSJiri Olsa { 36cd82a32eSJiri Olsa struct dirent *evt_ent; 37cd82a32eSJiri Olsa DIR *format_dir; 38cd82a32eSJiri Olsa int ret = 0; 39cd82a32eSJiri Olsa 40cd82a32eSJiri Olsa format_dir = opendir(dir); 41cd82a32eSJiri Olsa if (!format_dir) 42cd82a32eSJiri Olsa return -EINVAL; 43cd82a32eSJiri Olsa 44cd82a32eSJiri Olsa while (!ret && (evt_ent = readdir(format_dir))) { 45cd82a32eSJiri Olsa char path[PATH_MAX]; 46cd82a32eSJiri Olsa char *name = evt_ent->d_name; 47cd82a32eSJiri Olsa FILE *file; 48cd82a32eSJiri Olsa 49cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 50cd82a32eSJiri Olsa continue; 51cd82a32eSJiri Olsa 52cd82a32eSJiri Olsa snprintf(path, PATH_MAX, "%s/%s", dir, name); 53cd82a32eSJiri Olsa 54cd82a32eSJiri Olsa ret = -EINVAL; 55cd82a32eSJiri Olsa file = fopen(path, "r"); 56cd82a32eSJiri Olsa if (!file) 57cd82a32eSJiri Olsa break; 58cd82a32eSJiri Olsa 59cd82a32eSJiri Olsa perf_pmu_in = file; 60cd82a32eSJiri Olsa ret = perf_pmu_parse(head, name); 61cd82a32eSJiri Olsa fclose(file); 62cd82a32eSJiri Olsa } 63cd82a32eSJiri Olsa 64cd82a32eSJiri Olsa closedir(format_dir); 65cd82a32eSJiri Olsa return ret; 66cd82a32eSJiri Olsa } 67cd82a32eSJiri Olsa 68cd82a32eSJiri Olsa /* 69cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 70cd82a32eSJiri Olsa * located at: 71cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 72cd82a32eSJiri Olsa */ 73b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format) 74cd82a32eSJiri Olsa { 75cd82a32eSJiri Olsa struct stat st; 76cd82a32eSJiri Olsa char path[PATH_MAX]; 77cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 78cd82a32eSJiri Olsa 79cd82a32eSJiri Olsa if (!sysfs) 80cd82a32eSJiri Olsa return -1; 81cd82a32eSJiri Olsa 82cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 8350a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); 84cd82a32eSJiri Olsa 85cd82a32eSJiri Olsa if (stat(path, &st) < 0) 869bc8f9feSRobert Richter return 0; /* no error if format does not exist */ 87cd82a32eSJiri Olsa 88cff7f956SJiri Olsa if (perf_pmu__format_parse(path, format)) 89cd82a32eSJiri Olsa return -1; 90cd82a32eSJiri Olsa 91cd82a32eSJiri Olsa return 0; 92cd82a32eSJiri Olsa } 93cd82a32eSJiri Olsa 94410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) 95410136f5SStephane Eranian { 96410136f5SStephane Eranian struct stat st; 97410136f5SStephane Eranian ssize_t sret; 98410136f5SStephane Eranian char scale[128]; 99410136f5SStephane Eranian int fd, ret = -1; 100410136f5SStephane Eranian char path[PATH_MAX]; 1018a398897SStephane Eranian const char *lc; 102410136f5SStephane Eranian 103410136f5SStephane Eranian snprintf(path, PATH_MAX, "%s/%s.scale", dir, name); 104410136f5SStephane Eranian 105410136f5SStephane Eranian fd = open(path, O_RDONLY); 106410136f5SStephane Eranian if (fd == -1) 107410136f5SStephane Eranian return -1; 108410136f5SStephane Eranian 109410136f5SStephane Eranian if (fstat(fd, &st) < 0) 110410136f5SStephane Eranian goto error; 111410136f5SStephane Eranian 112410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 113410136f5SStephane Eranian if (sret < 0) 114410136f5SStephane Eranian goto error; 115410136f5SStephane Eranian 1169ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 1179ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 1189ecae065SMadhavan Srinivasan else 119410136f5SStephane Eranian scale[sret] = '\0'; 1209ecae065SMadhavan Srinivasan 121410136f5SStephane Eranian /* 122410136f5SStephane Eranian * save current locale 123410136f5SStephane Eranian */ 124410136f5SStephane Eranian lc = setlocale(LC_NUMERIC, NULL); 125410136f5SStephane Eranian 126410136f5SStephane Eranian /* 127*f9a5978aSJiri Olsa * The lc string may be allocated in static storage, 128*f9a5978aSJiri Olsa * so get a dynamic copy to make it survive setlocale 129*f9a5978aSJiri Olsa * call below. 130*f9a5978aSJiri Olsa */ 131*f9a5978aSJiri Olsa lc = strdup(lc); 132*f9a5978aSJiri Olsa if (!lc) { 133*f9a5978aSJiri Olsa ret = -ENOMEM; 134*f9a5978aSJiri Olsa goto error; 135*f9a5978aSJiri Olsa } 136*f9a5978aSJiri Olsa 137*f9a5978aSJiri Olsa /* 138410136f5SStephane Eranian * force to C locale to ensure kernel 139410136f5SStephane Eranian * scale string is converted correctly. 140410136f5SStephane Eranian * kernel uses default C locale. 141410136f5SStephane Eranian */ 142410136f5SStephane Eranian setlocale(LC_NUMERIC, "C"); 143410136f5SStephane Eranian 144410136f5SStephane Eranian alias->scale = strtod(scale, NULL); 145410136f5SStephane Eranian 146410136f5SStephane Eranian /* restore locale */ 147410136f5SStephane Eranian setlocale(LC_NUMERIC, lc); 148410136f5SStephane Eranian 149*f9a5978aSJiri Olsa free((char *) lc); 150*f9a5978aSJiri Olsa 151410136f5SStephane Eranian ret = 0; 152410136f5SStephane Eranian error: 153410136f5SStephane Eranian close(fd); 154410136f5SStephane Eranian return ret; 155410136f5SStephane Eranian } 156410136f5SStephane Eranian 157410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) 158410136f5SStephane Eranian { 159410136f5SStephane Eranian char path[PATH_MAX]; 160410136f5SStephane Eranian ssize_t sret; 161410136f5SStephane Eranian int fd; 162410136f5SStephane Eranian 163410136f5SStephane Eranian snprintf(path, PATH_MAX, "%s/%s.unit", dir, name); 164410136f5SStephane Eranian 165410136f5SStephane Eranian fd = open(path, O_RDONLY); 166410136f5SStephane Eranian if (fd == -1) 167410136f5SStephane Eranian return -1; 168410136f5SStephane Eranian 169410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 170410136f5SStephane Eranian if (sret < 0) 171410136f5SStephane Eranian goto error; 172410136f5SStephane Eranian 173410136f5SStephane Eranian close(fd); 174410136f5SStephane Eranian 1759ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 1769ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 1779ecae065SMadhavan Srinivasan else 178410136f5SStephane Eranian alias->unit[sret] = '\0'; 179410136f5SStephane Eranian 180410136f5SStephane Eranian return 0; 181410136f5SStephane Eranian error: 182410136f5SStephane Eranian close(fd); 183410136f5SStephane Eranian alias->unit[0] = '\0'; 184410136f5SStephane Eranian return -1; 185410136f5SStephane Eranian } 186410136f5SStephane Eranian 187044330c1SMatt Fleming static int 188044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) 189044330c1SMatt Fleming { 190044330c1SMatt Fleming char path[PATH_MAX]; 191044330c1SMatt Fleming int fd; 192044330c1SMatt Fleming 193044330c1SMatt Fleming snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); 194044330c1SMatt Fleming 195044330c1SMatt Fleming fd = open(path, O_RDONLY); 196044330c1SMatt Fleming if (fd == -1) 197044330c1SMatt Fleming return -1; 198044330c1SMatt Fleming 199044330c1SMatt Fleming close(fd); 200044330c1SMatt Fleming 201044330c1SMatt Fleming alias->per_pkg = true; 202044330c1SMatt Fleming return 0; 203044330c1SMatt Fleming } 204044330c1SMatt Fleming 2051d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 2061d9e446bSJiri Olsa char *dir, char *name) 2071d9e446bSJiri Olsa { 2081d9e446bSJiri Olsa char path[PATH_MAX]; 2091d9e446bSJiri Olsa int fd; 2101d9e446bSJiri Olsa 2111d9e446bSJiri Olsa snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); 2121d9e446bSJiri Olsa 2131d9e446bSJiri Olsa fd = open(path, O_RDONLY); 2141d9e446bSJiri Olsa if (fd == -1) 2151d9e446bSJiri Olsa return -1; 2161d9e446bSJiri Olsa 2171d9e446bSJiri Olsa alias->snapshot = true; 2181d9e446bSJiri Olsa close(fd); 2191d9e446bSJiri Olsa return 0; 2201d9e446bSJiri Olsa } 2211d9e446bSJiri Olsa 22270c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, 22370c646e0SSukadev Bhattiprolu char *desc __maybe_unused, char *val) 224a6146d50SZheng Yan { 2255c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 226a6146d50SZheng Yan int ret; 227a6146d50SZheng Yan 228a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 229a6146d50SZheng Yan if (!alias) 230a6146d50SZheng Yan return -ENOMEM; 231a6146d50SZheng Yan 232a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 233410136f5SStephane Eranian alias->scale = 1.0; 234410136f5SStephane Eranian alias->unit[0] = '\0'; 235044330c1SMatt Fleming alias->per_pkg = false; 23684530920SStephane Eranian alias->snapshot = false; 237410136f5SStephane Eranian 23870c646e0SSukadev Bhattiprolu ret = parse_events_terms(&alias->terms, val); 239a6146d50SZheng Yan if (ret) { 24070c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 241a6146d50SZheng Yan free(alias); 242a6146d50SZheng Yan return ret; 243a6146d50SZheng Yan } 244a6146d50SZheng Yan 245a6146d50SZheng Yan alias->name = strdup(name); 24670c646e0SSukadev Bhattiprolu if (dir) { 247410136f5SStephane Eranian /* 248410136f5SStephane Eranian * load unit name and scale if available 249410136f5SStephane Eranian */ 250410136f5SStephane Eranian perf_pmu__parse_unit(alias, dir, name); 251410136f5SStephane Eranian perf_pmu__parse_scale(alias, dir, name); 252044330c1SMatt Fleming perf_pmu__parse_per_pkg(alias, dir, name); 2531d9e446bSJiri Olsa perf_pmu__parse_snapshot(alias, dir, name); 25470c646e0SSukadev Bhattiprolu } 255410136f5SStephane Eranian 256a6146d50SZheng Yan list_add_tail(&alias->list, list); 257410136f5SStephane Eranian 258a6146d50SZheng Yan return 0; 259a6146d50SZheng Yan } 260a6146d50SZheng Yan 26170c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) 26270c646e0SSukadev Bhattiprolu { 26370c646e0SSukadev Bhattiprolu char buf[256]; 26470c646e0SSukadev Bhattiprolu int ret; 26570c646e0SSukadev Bhattiprolu 26670c646e0SSukadev Bhattiprolu ret = fread(buf, 1, sizeof(buf), file); 26770c646e0SSukadev Bhattiprolu if (ret == 0) 26870c646e0SSukadev Bhattiprolu return -EINVAL; 26970c646e0SSukadev Bhattiprolu 27070c646e0SSukadev Bhattiprolu buf[ret] = 0; 27170c646e0SSukadev Bhattiprolu 27270c646e0SSukadev Bhattiprolu return __perf_pmu__new_alias(list, dir, name, NULL, buf); 27370c646e0SSukadev Bhattiprolu } 27470c646e0SSukadev Bhattiprolu 27546441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 27646441bdcSMatt Fleming { 27746441bdcSMatt Fleming size_t len; 27846441bdcSMatt Fleming 27946441bdcSMatt Fleming len = strlen(name); 28046441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 28146441bdcSMatt Fleming return true; 28246441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 28346441bdcSMatt Fleming return true; 284044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 285044330c1SMatt Fleming return true; 2861d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 2871d9e446bSJiri Olsa return true; 28846441bdcSMatt Fleming 28946441bdcSMatt Fleming return false; 29046441bdcSMatt Fleming } 29146441bdcSMatt Fleming 292a6146d50SZheng Yan /* 293a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 294a6146d50SZheng Yan * specified in 'dir' parameter. 295a6146d50SZheng Yan */ 296a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head) 297a6146d50SZheng Yan { 298a6146d50SZheng Yan struct dirent *evt_ent; 299a6146d50SZheng Yan DIR *event_dir; 300a6146d50SZheng Yan 301a6146d50SZheng Yan event_dir = opendir(dir); 302a6146d50SZheng Yan if (!event_dir) 303a6146d50SZheng Yan return -EINVAL; 304a6146d50SZheng Yan 305940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 306a6146d50SZheng Yan char path[PATH_MAX]; 307a6146d50SZheng Yan char *name = evt_ent->d_name; 308a6146d50SZheng Yan FILE *file; 309a6146d50SZheng Yan 310a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 311a6146d50SZheng Yan continue; 312a6146d50SZheng Yan 313410136f5SStephane Eranian /* 31446441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 315410136f5SStephane Eranian */ 31646441bdcSMatt Fleming if (pmu_alias_info_file(name)) 317410136f5SStephane Eranian continue; 318410136f5SStephane Eranian 319a6146d50SZheng Yan snprintf(path, PATH_MAX, "%s/%s", dir, name); 320a6146d50SZheng Yan 321a6146d50SZheng Yan file = fopen(path, "r"); 322940db6dcSAndi Kleen if (!file) { 323940db6dcSAndi Kleen pr_debug("Cannot open %s\n", path); 324940db6dcSAndi Kleen continue; 325940db6dcSAndi Kleen } 326410136f5SStephane Eranian 327940db6dcSAndi Kleen if (perf_pmu__new_alias(head, dir, name, file) < 0) 328940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 329a6146d50SZheng Yan fclose(file); 330a6146d50SZheng Yan } 331a6146d50SZheng Yan 332a6146d50SZheng Yan closedir(event_dir); 333940db6dcSAndi Kleen return 0; 334a6146d50SZheng Yan } 335a6146d50SZheng Yan 336a6146d50SZheng Yan /* 337a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 338a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 339a6146d50SZheng Yan */ 340b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head) 341a6146d50SZheng Yan { 342a6146d50SZheng Yan struct stat st; 343a6146d50SZheng Yan char path[PATH_MAX]; 344cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 345a6146d50SZheng Yan 346a6146d50SZheng Yan if (!sysfs) 347a6146d50SZheng Yan return -1; 348a6146d50SZheng Yan 349a6146d50SZheng Yan snprintf(path, PATH_MAX, 350a6146d50SZheng Yan "%s/bus/event_source/devices/%s/events", sysfs, name); 351a6146d50SZheng Yan 352a6146d50SZheng Yan if (stat(path, &st) < 0) 3533fded963SJiri Olsa return 0; /* no error if 'events' does not exist */ 354a6146d50SZheng Yan 355a6146d50SZheng Yan if (pmu_aliases_parse(path, head)) 356a6146d50SZheng Yan return -1; 357a6146d50SZheng Yan 358a6146d50SZheng Yan return 0; 359a6146d50SZheng Yan } 360a6146d50SZheng Yan 3615c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 362a6146d50SZheng Yan struct list_head *terms) 363a6146d50SZheng Yan { 3647c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 365a6146d50SZheng Yan LIST_HEAD(list); 366a6146d50SZheng Yan int ret; 367a6146d50SZheng Yan 368a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 3697c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 370a6146d50SZheng Yan if (ret) { 371682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 372a6146d50SZheng Yan return ret; 373a6146d50SZheng Yan } 3747c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 375a6146d50SZheng Yan } 376a6146d50SZheng Yan list_splice(&list, terms); 377a6146d50SZheng Yan return 0; 378a6146d50SZheng Yan } 379a6146d50SZheng Yan 380cd82a32eSJiri Olsa /* 381cd82a32eSJiri Olsa * Reading/parsing the default pmu type value, which should be 382cd82a32eSJiri Olsa * located at: 383cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 384cd82a32eSJiri Olsa */ 385b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type) 386cd82a32eSJiri Olsa { 387cd82a32eSJiri Olsa struct stat st; 388cd82a32eSJiri Olsa char path[PATH_MAX]; 389cd82a32eSJiri Olsa FILE *file; 390cd82a32eSJiri Olsa int ret = 0; 391cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 392cd82a32eSJiri Olsa 393cd82a32eSJiri Olsa if (!sysfs) 394cd82a32eSJiri Olsa return -1; 395cd82a32eSJiri Olsa 396cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 39750a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); 398cd82a32eSJiri Olsa 399cd82a32eSJiri Olsa if (stat(path, &st) < 0) 400cd82a32eSJiri Olsa return -1; 401cd82a32eSJiri Olsa 402cd82a32eSJiri Olsa file = fopen(path, "r"); 403cd82a32eSJiri Olsa if (!file) 404cd82a32eSJiri Olsa return -EINVAL; 405cd82a32eSJiri Olsa 406cd82a32eSJiri Olsa if (1 != fscanf(file, "%u", type)) 407cd82a32eSJiri Olsa ret = -1; 408cd82a32eSJiri Olsa 409cd82a32eSJiri Olsa fclose(file); 410cd82a32eSJiri Olsa return ret; 411cd82a32eSJiri Olsa } 412cd82a32eSJiri Olsa 41350a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */ 41450a9667cSRobert Richter static void pmu_read_sysfs(void) 41550a9667cSRobert Richter { 41650a9667cSRobert Richter char path[PATH_MAX]; 41750a9667cSRobert Richter DIR *dir; 41850a9667cSRobert Richter struct dirent *dent; 419cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 42050a9667cSRobert Richter 42150a9667cSRobert Richter if (!sysfs) 42250a9667cSRobert Richter return; 42350a9667cSRobert Richter 42450a9667cSRobert Richter snprintf(path, PATH_MAX, 42550a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 42650a9667cSRobert Richter 42750a9667cSRobert Richter dir = opendir(path); 42850a9667cSRobert Richter if (!dir) 42950a9667cSRobert Richter return; 43050a9667cSRobert Richter 43150a9667cSRobert Richter while ((dent = readdir(dir))) { 43250a9667cSRobert Richter if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 43350a9667cSRobert Richter continue; 43450a9667cSRobert Richter /* add to static LIST_HEAD(pmus): */ 43550a9667cSRobert Richter perf_pmu__find(dent->d_name); 43650a9667cSRobert Richter } 43750a9667cSRobert Richter 43850a9667cSRobert Richter closedir(dir); 43950a9667cSRobert Richter } 44050a9667cSRobert Richter 441b6b96fb4SAdrian Hunter static struct cpu_map *pmu_cpumask(const char *name) 4427ae92e74SYan, Zheng { 4437ae92e74SYan, Zheng struct stat st; 4447ae92e74SYan, Zheng char path[PATH_MAX]; 4457ae92e74SYan, Zheng FILE *file; 4467ae92e74SYan, Zheng struct cpu_map *cpus; 447cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 4487ae92e74SYan, Zheng 4497ae92e74SYan, Zheng if (!sysfs) 4507ae92e74SYan, Zheng return NULL; 4517ae92e74SYan, Zheng 4527ae92e74SYan, Zheng snprintf(path, PATH_MAX, 4537ae92e74SYan, Zheng "%s/bus/event_source/devices/%s/cpumask", sysfs, name); 4547ae92e74SYan, Zheng 4557ae92e74SYan, Zheng if (stat(path, &st) < 0) 4567ae92e74SYan, Zheng return NULL; 4577ae92e74SYan, Zheng 4587ae92e74SYan, Zheng file = fopen(path, "r"); 4597ae92e74SYan, Zheng if (!file) 4607ae92e74SYan, Zheng return NULL; 4617ae92e74SYan, Zheng 4627ae92e74SYan, Zheng cpus = cpu_map__read(file); 4637ae92e74SYan, Zheng fclose(file); 4647ae92e74SYan, Zheng return cpus; 4657ae92e74SYan, Zheng } 4667ae92e74SYan, Zheng 467c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 468dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 469dc0a6202SAdrian Hunter { 470dc0a6202SAdrian Hunter return NULL; 471dc0a6202SAdrian Hunter } 472dc0a6202SAdrian Hunter 473b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name) 474cd82a32eSJiri Olsa { 475cd82a32eSJiri Olsa struct perf_pmu *pmu; 476cd82a32eSJiri Olsa LIST_HEAD(format); 477a6146d50SZheng Yan LIST_HEAD(aliases); 478cd82a32eSJiri Olsa __u32 type; 479cd82a32eSJiri Olsa 480cd82a32eSJiri Olsa /* 481cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 482cd82a32eSJiri Olsa * type value and format definitions. Load both right 483cd82a32eSJiri Olsa * now. 484cd82a32eSJiri Olsa */ 485cd82a32eSJiri Olsa if (pmu_format(name, &format)) 486cd82a32eSJiri Olsa return NULL; 487cd82a32eSJiri Olsa 4883fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 4893fded963SJiri Olsa return NULL; 4903fded963SJiri Olsa 491cd82a32eSJiri Olsa if (pmu_type(name, &type)) 492cd82a32eSJiri Olsa return NULL; 493cd82a32eSJiri Olsa 494cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 495cd82a32eSJiri Olsa if (!pmu) 496cd82a32eSJiri Olsa return NULL; 497cd82a32eSJiri Olsa 4987ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 4997ae92e74SYan, Zheng 500cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 501a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 502cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 503a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 504cd82a32eSJiri Olsa pmu->name = strdup(name); 505cd82a32eSJiri Olsa pmu->type = type; 5069bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 507dc0a6202SAdrian Hunter 508dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 509dc0a6202SAdrian Hunter 510cd82a32eSJiri Olsa return pmu; 511cd82a32eSJiri Olsa } 512cd82a32eSJiri Olsa 513b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 514cd82a32eSJiri Olsa { 515cd82a32eSJiri Olsa struct perf_pmu *pmu; 516cd82a32eSJiri Olsa 517cd82a32eSJiri Olsa list_for_each_entry(pmu, &pmus, list) 518cd82a32eSJiri Olsa if (!strcmp(pmu->name, name)) 519cd82a32eSJiri Olsa return pmu; 520cd82a32eSJiri Olsa 521cd82a32eSJiri Olsa return NULL; 522cd82a32eSJiri Olsa } 523cd82a32eSJiri Olsa 52450a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 52550a9667cSRobert Richter { 52650a9667cSRobert Richter /* 52750a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 52850a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 52950a9667cSRobert Richter */ 53050a9667cSRobert Richter if (!pmu) { 53150a9667cSRobert Richter pmu_read_sysfs(); 53250a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 53350a9667cSRobert Richter } 53450a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 53550a9667cSRobert Richter return pmu; 53650a9667cSRobert Richter return NULL; 53750a9667cSRobert Richter } 53850a9667cSRobert Richter 539b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 540cd82a32eSJiri Olsa { 541cd82a32eSJiri Olsa struct perf_pmu *pmu; 542cd82a32eSJiri Olsa 543cd82a32eSJiri Olsa /* 544cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 545cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 546cd82a32eSJiri Olsa * the pmu format definitions. 547cd82a32eSJiri Olsa */ 548cd82a32eSJiri Olsa pmu = pmu_find(name); 549cd82a32eSJiri Olsa if (pmu) 550cd82a32eSJiri Olsa return pmu; 551cd82a32eSJiri Olsa 552cd82a32eSJiri Olsa return pmu_lookup(name); 553cd82a32eSJiri Olsa } 554cd82a32eSJiri Olsa 5555c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 55609ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 557cd82a32eSJiri Olsa { 5585c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 559cd82a32eSJiri Olsa 560cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 561cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 562cd82a32eSJiri Olsa return format; 563cd82a32eSJiri Olsa 564cd82a32eSJiri Olsa return NULL; 565cd82a32eSJiri Olsa } 566cd82a32eSJiri Olsa 56709ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 56809ff6071SAdrian Hunter { 56909ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 57009ff6071SAdrian Hunter __u64 bits = 0; 57109ff6071SAdrian Hunter int fbit; 57209ff6071SAdrian Hunter 57309ff6071SAdrian Hunter if (!format) 57409ff6071SAdrian Hunter return 0; 57509ff6071SAdrian Hunter 57609ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 57709ff6071SAdrian Hunter bits |= 1ULL << fbit; 57809ff6071SAdrian Hunter 57909ff6071SAdrian Hunter return bits; 58009ff6071SAdrian Hunter } 58109ff6071SAdrian Hunter 582cd82a32eSJiri Olsa /* 583dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 584cd82a32eSJiri Olsa * and unformated value (value parameter). 585cd82a32eSJiri Olsa */ 586dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 587dc0a6202SAdrian Hunter bool zero) 588cd82a32eSJiri Olsa { 589cd82a32eSJiri Olsa unsigned long fbit, vbit; 590cd82a32eSJiri Olsa 591cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 592cd82a32eSJiri Olsa 593cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 594cd82a32eSJiri Olsa continue; 595cd82a32eSJiri Olsa 596dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 597dc0a6202SAdrian Hunter *v |= (1llu << fbit); 598dc0a6202SAdrian Hunter else if (zero) 599dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 600cd82a32eSJiri Olsa } 601cd82a32eSJiri Olsa } 602cd82a32eSJiri Olsa 6030efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 6040efe6b67SAdrian Hunter { 6050efe6b67SAdrian Hunter int w; 6060efe6b67SAdrian Hunter 6070efe6b67SAdrian Hunter w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 6080efe6b67SAdrian Hunter if (!w) 6090efe6b67SAdrian Hunter return 0; 6100efe6b67SAdrian Hunter if (w < 64) 6110efe6b67SAdrian Hunter return (1ULL << w) - 1; 6120efe6b67SAdrian Hunter return -1; 6130efe6b67SAdrian Hunter } 6140efe6b67SAdrian Hunter 615cd82a32eSJiri Olsa /* 616688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 617688d4dfcSCody P Schafer * in the remaining terms. 618688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 619688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 620688d4dfcSCody P Schafer * in a config string) later on in the term list. 621688d4dfcSCody P Schafer */ 622688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 623688d4dfcSCody P Schafer struct list_head *head_terms, 624688d4dfcSCody P Schafer __u64 *value) 625688d4dfcSCody P Schafer { 626688d4dfcSCody P Schafer struct parse_events_term *t; 627688d4dfcSCody P Schafer 628688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 629688d4dfcSCody P Schafer if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 630688d4dfcSCody P Schafer if (!strcmp(t->config, term->config)) { 631688d4dfcSCody P Schafer t->used = true; 632688d4dfcSCody P Schafer *value = t->val.num; 633688d4dfcSCody P Schafer return 0; 634688d4dfcSCody P Schafer } 635688d4dfcSCody P Schafer } 636688d4dfcSCody P Schafer } 637688d4dfcSCody P Schafer 638688d4dfcSCody P Schafer if (verbose) 639688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 640688d4dfcSCody P Schafer 641688d4dfcSCody P Schafer return -1; 642688d4dfcSCody P Schafer } 643688d4dfcSCody P Schafer 644ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 645e64b020bSJiri Olsa { 646e64b020bSJiri Olsa struct perf_pmu_format *format; 647ffeb883eSHe Kuang char *str; 648ffeb883eSHe Kuang struct strbuf buf; 649e64b020bSJiri Olsa unsigned i = 0; 650e64b020bSJiri Olsa 651ffeb883eSHe Kuang if (!formats) 652e64b020bSJiri Olsa return NULL; 653e64b020bSJiri Olsa 654ffeb883eSHe Kuang strbuf_init(&buf, 0); 655e64b020bSJiri Olsa /* sysfs exported terms */ 656ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 657ffeb883eSHe Kuang strbuf_addf(&buf, i++ ? ",%s" : "%s", 658ffeb883eSHe Kuang format->name); 659e64b020bSJiri Olsa 660ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 661ffeb883eSHe Kuang strbuf_release(&buf); 662e64b020bSJiri Olsa 663e64b020bSJiri Olsa return str; 664e64b020bSJiri Olsa } 665e64b020bSJiri Olsa 666688d4dfcSCody P Schafer /* 667cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 66888aca8d9SCody P Schafer * user input data - term parameter. 669cd82a32eSJiri Olsa */ 670cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats, 671cd82a32eSJiri Olsa struct perf_event_attr *attr, 672dc0a6202SAdrian Hunter struct parse_events_term *term, 673688d4dfcSCody P Schafer struct list_head *head_terms, 674e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 675cd82a32eSJiri Olsa { 6765c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 677cd82a32eSJiri Olsa __u64 *vp; 6780efe6b67SAdrian Hunter __u64 val, max_val; 679cd82a32eSJiri Olsa 680cd82a32eSJiri Olsa /* 681688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 682688d4dfcSCody P Schafer * skip it in normal eval. 683688d4dfcSCody P Schafer */ 684688d4dfcSCody P Schafer if (term->used) 685688d4dfcSCody P Schafer return 0; 686688d4dfcSCody P Schafer 687688d4dfcSCody P Schafer /* 688cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 689cd82a32eSJiri Olsa * to be done for them. 690cd82a32eSJiri Olsa */ 691cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 692cd82a32eSJiri Olsa return 0; 693cd82a32eSJiri Olsa 694cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 695688d4dfcSCody P Schafer if (!format) { 696688d4dfcSCody P Schafer if (verbose) 697688d4dfcSCody P Schafer printf("Invalid event/parameter '%s'\n", term->config); 698e64b020bSJiri Olsa if (err) { 699ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 700ffeb883eSHe Kuang 701e64b020bSJiri Olsa err->idx = term->err_term; 702e64b020bSJiri Olsa err->str = strdup("unknown term"); 703ffeb883eSHe Kuang err->help = parse_events_formats_error_string(pmu_term); 704ffeb883eSHe Kuang free(pmu_term); 705e64b020bSJiri Olsa } 706cd82a32eSJiri Olsa return -EINVAL; 707688d4dfcSCody P Schafer } 708cd82a32eSJiri Olsa 709cd82a32eSJiri Olsa switch (format->value) { 710cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 711cd82a32eSJiri Olsa vp = &attr->config; 712cd82a32eSJiri Olsa break; 713cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 714cd82a32eSJiri Olsa vp = &attr->config1; 715cd82a32eSJiri Olsa break; 716cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 717cd82a32eSJiri Olsa vp = &attr->config2; 718cd82a32eSJiri Olsa break; 719cd82a32eSJiri Olsa default: 720cd82a32eSJiri Olsa return -EINVAL; 721cd82a32eSJiri Olsa } 722cd82a32eSJiri Olsa 72316fa7e82SJiri Olsa /* 724688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 725688d4dfcSCody P Schafer * using event parameters. 72616fa7e82SJiri Olsa */ 727688d4dfcSCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 728688d4dfcSCody P Schafer val = term->val.num; 729688d4dfcSCody P Schafer else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 730688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 731e64b020bSJiri Olsa if (verbose) { 732688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 733688d4dfcSCody P Schafer term->config, term->val.str); 734e64b020bSJiri Olsa } 735e64b020bSJiri Olsa if (err) { 736e64b020bSJiri Olsa err->idx = term->err_val; 737e64b020bSJiri Olsa err->str = strdup("expected numeric value"); 738e64b020bSJiri Olsa } 739688d4dfcSCody P Schafer return -EINVAL; 740688d4dfcSCody P Schafer } 741688d4dfcSCody P Schafer 742688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 743688d4dfcSCody P Schafer return -EINVAL; 744688d4dfcSCody P Schafer } else 745688d4dfcSCody P Schafer return -EINVAL; 746688d4dfcSCody P Schafer 7470efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 7480efe6b67SAdrian Hunter if (val > max_val) { 7490efe6b67SAdrian Hunter if (err) { 7500efe6b67SAdrian Hunter err->idx = term->err_val; 7510efe6b67SAdrian Hunter if (asprintf(&err->str, 7520efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 7530efe6b67SAdrian Hunter (unsigned long long)max_val) < 0) 7540efe6b67SAdrian Hunter err->str = strdup("value too big for format"); 7550efe6b67SAdrian Hunter return -EINVAL; 7560efe6b67SAdrian Hunter } 7570efe6b67SAdrian Hunter /* 7580efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 7590efe6b67SAdrian Hunter * silently truncated. 7600efe6b67SAdrian Hunter */ 7610efe6b67SAdrian Hunter } 7620efe6b67SAdrian Hunter 763688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 764cd82a32eSJiri Olsa return 0; 765cd82a32eSJiri Olsa } 766cd82a32eSJiri Olsa 767cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats, 768cff7f956SJiri Olsa struct perf_event_attr *attr, 769dc0a6202SAdrian Hunter struct list_head *head_terms, 770e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 771cd82a32eSJiri Olsa { 7726cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 773cd82a32eSJiri Olsa 774688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 775e64b020bSJiri Olsa if (pmu_config_term(formats, attr, term, head_terms, 776e64b020bSJiri Olsa zero, err)) 777cd82a32eSJiri Olsa return -EINVAL; 778688d4dfcSCody P Schafer } 779cd82a32eSJiri Olsa 780cd82a32eSJiri Olsa return 0; 781cd82a32eSJiri Olsa } 782cd82a32eSJiri Olsa 783cd82a32eSJiri Olsa /* 784cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 785cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 786cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 787cd82a32eSJiri Olsa */ 788cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 789e64b020bSJiri Olsa struct list_head *head_terms, 790e64b020bSJiri Olsa struct parse_events_error *err) 791cd82a32eSJiri Olsa { 792dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 793dc0a6202SAdrian Hunter 794cd82a32eSJiri Olsa attr->type = pmu->type; 795e64b020bSJiri Olsa return perf_pmu__config_terms(&pmu->format, attr, head_terms, 796e64b020bSJiri Olsa zero, err); 797cd82a32eSJiri Olsa } 798cd82a32eSJiri Olsa 7995c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 8006cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 801a6146d50SZheng Yan { 8025c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 803a6146d50SZheng Yan char *name; 804a6146d50SZheng Yan 805a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 806a6146d50SZheng Yan return NULL; 807a6146d50SZheng Yan 808a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 809a6146d50SZheng Yan if (term->val.num != 1) 810a6146d50SZheng Yan return NULL; 811a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 812a6146d50SZheng Yan return NULL; 813a6146d50SZheng Yan name = term->config; 814a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 815a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 816a6146d50SZheng Yan return NULL; 817a6146d50SZheng Yan name = term->val.str; 818a6146d50SZheng Yan } else { 819a6146d50SZheng Yan return NULL; 820a6146d50SZheng Yan } 821a6146d50SZheng Yan 822a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 823a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 824a6146d50SZheng Yan return alias; 825a6146d50SZheng Yan } 826a6146d50SZheng Yan return NULL; 827a6146d50SZheng Yan } 828a6146d50SZheng Yan 829410136f5SStephane Eranian 8301d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 8311d9e446bSJiri Olsa struct perf_pmu_info *info) 832410136f5SStephane Eranian { 833410136f5SStephane Eranian /* 834410136f5SStephane Eranian * Only one term in event definition can 8351d9e446bSJiri Olsa * define unit, scale and snapshot, fail 8361d9e446bSJiri Olsa * if there's more than one. 837410136f5SStephane Eranian */ 8381d9e446bSJiri Olsa if ((info->unit && alias->unit) || 8391d9e446bSJiri Olsa (info->scale && alias->scale) || 8401d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 841410136f5SStephane Eranian return -EINVAL; 842410136f5SStephane Eranian 843410136f5SStephane Eranian if (alias->unit) 8441d9e446bSJiri Olsa info->unit = alias->unit; 845410136f5SStephane Eranian 846410136f5SStephane Eranian if (alias->scale) 8471d9e446bSJiri Olsa info->scale = alias->scale; 8481d9e446bSJiri Olsa 8491d9e446bSJiri Olsa if (alias->snapshot) 8501d9e446bSJiri Olsa info->snapshot = alias->snapshot; 851410136f5SStephane Eranian 852410136f5SStephane Eranian return 0; 853410136f5SStephane Eranian } 854410136f5SStephane Eranian 855a6146d50SZheng Yan /* 856a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 857a6146d50SZheng Yan * defined for the alias 858a6146d50SZheng Yan */ 859410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 86046441bdcSMatt Fleming struct perf_pmu_info *info) 861a6146d50SZheng Yan { 8626cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 8635c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 864a6146d50SZheng Yan int ret; 865a6146d50SZheng Yan 866044330c1SMatt Fleming info->per_pkg = false; 867044330c1SMatt Fleming 8688a398897SStephane Eranian /* 8698a398897SStephane Eranian * Mark unit and scale as not set 8708a398897SStephane Eranian * (different from default values, see below) 8718a398897SStephane Eranian */ 87246441bdcSMatt Fleming info->unit = NULL; 87346441bdcSMatt Fleming info->scale = 0.0; 8741d9e446bSJiri Olsa info->snapshot = false; 875410136f5SStephane Eranian 876a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 877a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 878a6146d50SZheng Yan if (!alias) 879a6146d50SZheng Yan continue; 880a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 881a6146d50SZheng Yan if (ret) 882a6146d50SZheng Yan return ret; 883410136f5SStephane Eranian 8841d9e446bSJiri Olsa ret = check_info_data(alias, info); 885410136f5SStephane Eranian if (ret) 886410136f5SStephane Eranian return ret; 887410136f5SStephane Eranian 888044330c1SMatt Fleming if (alias->per_pkg) 889044330c1SMatt Fleming info->per_pkg = true; 890044330c1SMatt Fleming 891a6146d50SZheng Yan list_del(&term->list); 892a6146d50SZheng Yan free(term); 893a6146d50SZheng Yan } 8948a398897SStephane Eranian 8958a398897SStephane Eranian /* 8968a398897SStephane Eranian * if no unit or scale foundin aliases, then 8978a398897SStephane Eranian * set defaults as for evsel 8988a398897SStephane Eranian * unit cannot left to NULL 8998a398897SStephane Eranian */ 90046441bdcSMatt Fleming if (info->unit == NULL) 90146441bdcSMatt Fleming info->unit = ""; 9028a398897SStephane Eranian 90346441bdcSMatt Fleming if (info->scale == 0.0) 90446441bdcSMatt Fleming info->scale = 1.0; 9058a398897SStephane Eranian 906a6146d50SZheng Yan return 0; 907a6146d50SZheng Yan } 908a6146d50SZheng Yan 909cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 910cd82a32eSJiri Olsa int config, unsigned long *bits) 911cd82a32eSJiri Olsa { 9125c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 913cd82a32eSJiri Olsa 914cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 915cd82a32eSJiri Olsa if (!format) 916cd82a32eSJiri Olsa return -ENOMEM; 917cd82a32eSJiri Olsa 918cd82a32eSJiri Olsa format->name = strdup(name); 919cd82a32eSJiri Olsa format->value = config; 920cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 921cd82a32eSJiri Olsa 922cd82a32eSJiri Olsa list_add_tail(&format->list, list); 923cd82a32eSJiri Olsa return 0; 924cd82a32eSJiri Olsa } 925cd82a32eSJiri Olsa 926cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 927cd82a32eSJiri Olsa { 928cd82a32eSJiri Olsa long b; 929cd82a32eSJiri Olsa 930cd82a32eSJiri Olsa if (!to) 931cd82a32eSJiri Olsa to = from; 932cd82a32eSJiri Olsa 93315268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 934cd82a32eSJiri Olsa for (b = from; b <= to; b++) 935cd82a32eSJiri Olsa set_bit(b, bits); 936cd82a32eSJiri Olsa } 937dc098b35SAndi Kleen 938aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 939aaea3617SCody P Schafer { 940aaea3617SCody P Schafer if (b > a) 941aaea3617SCody P Schafer return 0; 942aaea3617SCody P Schafer return a - b; 943aaea3617SCody P Schafer } 944aaea3617SCody P Schafer 945dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 946dc098b35SAndi Kleen struct perf_pmu_alias *alias) 947dc098b35SAndi Kleen { 948aaea3617SCody P Schafer struct parse_events_term *term; 949aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 950aaea3617SCody P Schafer 951aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 952aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 953aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 954aaea3617SCody P Schafer ",%s=%s", term->config, 955aaea3617SCody P Schafer term->val.str); 956aaea3617SCody P Schafer } 957aaea3617SCody P Schafer 958aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 959aaea3617SCody P Schafer buf[used] = '/'; 960aaea3617SCody P Schafer used++; 961aaea3617SCody P Schafer } 962aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 963aaea3617SCody P Schafer buf[used] = '\0'; 964aaea3617SCody P Schafer used++; 965aaea3617SCody P Schafer } else 966aaea3617SCody P Schafer buf[len - 1] = '\0'; 967aaea3617SCody P Schafer 968dc098b35SAndi Kleen return buf; 969dc098b35SAndi Kleen } 970dc098b35SAndi Kleen 971dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 972dc098b35SAndi Kleen struct perf_pmu_alias *alias) 973dc098b35SAndi Kleen { 974dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 975dc098b35SAndi Kleen return buf; 976dc098b35SAndi Kleen } 977dc098b35SAndi Kleen 978dc098b35SAndi Kleen static int cmp_string(const void *a, const void *b) 979dc098b35SAndi Kleen { 980dc098b35SAndi Kleen const char * const *as = a; 981dc098b35SAndi Kleen const char * const *bs = b; 982dc098b35SAndi Kleen return strcmp(*as, *bs); 983dc098b35SAndi Kleen } 984dc098b35SAndi Kleen 985dc098b35SAndi Kleen void print_pmu_events(const char *event_glob, bool name_only) 986dc098b35SAndi Kleen { 987dc098b35SAndi Kleen struct perf_pmu *pmu; 988dc098b35SAndi Kleen struct perf_pmu_alias *alias; 989dc098b35SAndi Kleen char buf[1024]; 990dc098b35SAndi Kleen int printed = 0; 991dc098b35SAndi Kleen int len, j; 992dc098b35SAndi Kleen char **aliases; 993dc098b35SAndi Kleen 994dc098b35SAndi Kleen pmu = NULL; 995dc098b35SAndi Kleen len = 0; 99642634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 997dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 998dc098b35SAndi Kleen len++; 99942634bc7SAdrian Hunter if (pmu->selectable) 100042634bc7SAdrian Hunter len++; 100142634bc7SAdrian Hunter } 10027e4772dcSArnaldo Carvalho de Melo aliases = zalloc(sizeof(char *) * len); 1003dc098b35SAndi Kleen if (!aliases) 10047e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1005dc098b35SAndi Kleen pmu = NULL; 1006dc098b35SAndi Kleen j = 0; 100742634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1008dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 1009dc098b35SAndi Kleen char *name = format_alias(buf, sizeof(buf), pmu, alias); 1010dc098b35SAndi Kleen bool is_cpu = !strcmp(pmu->name, "cpu"); 1011dc098b35SAndi Kleen 1012dc098b35SAndi Kleen if (event_glob != NULL && 1013dc098b35SAndi Kleen !(strglobmatch(name, event_glob) || 1014dc098b35SAndi Kleen (!is_cpu && strglobmatch(alias->name, 1015dc098b35SAndi Kleen event_glob)))) 1016dc098b35SAndi Kleen continue; 10177e4772dcSArnaldo Carvalho de Melo 1018dc098b35SAndi Kleen if (is_cpu && !name_only) 10197e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 10207e4772dcSArnaldo Carvalho de Melo 10217e4772dcSArnaldo Carvalho de Melo aliases[j] = strdup(name); 10227e4772dcSArnaldo Carvalho de Melo if (aliases[j] == NULL) 10237e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1024dc098b35SAndi Kleen j++; 1025dc098b35SAndi Kleen } 1026fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1027fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 10287e4772dcSArnaldo Carvalho de Melo char *s; 10297e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 10307e4772dcSArnaldo Carvalho de Melo goto out_enomem; 10317e4772dcSArnaldo Carvalho de Melo aliases[j] = s; 103242634bc7SAdrian Hunter j++; 103342634bc7SAdrian Hunter } 103442634bc7SAdrian Hunter } 1035dc098b35SAndi Kleen len = j; 1036dc098b35SAndi Kleen qsort(aliases, len, sizeof(char *), cmp_string); 1037dc098b35SAndi Kleen for (j = 0; j < len; j++) { 1038dc098b35SAndi Kleen if (name_only) { 1039dc098b35SAndi Kleen printf("%s ", aliases[j]); 1040dc098b35SAndi Kleen continue; 1041dc098b35SAndi Kleen } 1042dc098b35SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j]); 1043dc098b35SAndi Kleen printed++; 1044dc098b35SAndi Kleen } 1045dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1046dc098b35SAndi Kleen printf("\n"); 10477e4772dcSArnaldo Carvalho de Melo out_free: 10487e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 10497e4772dcSArnaldo Carvalho de Melo zfree(&aliases[j]); 10507e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 10517e4772dcSArnaldo Carvalho de Melo return; 10527e4772dcSArnaldo Carvalho de Melo 10537e4772dcSArnaldo Carvalho de Melo out_enomem: 10547e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 10557e4772dcSArnaldo Carvalho de Melo if (aliases) 10567e4772dcSArnaldo Carvalho de Melo goto out_free; 1057dc098b35SAndi Kleen } 10584cabc3d1SAndi Kleen 10594cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 10604cabc3d1SAndi Kleen { 10614cabc3d1SAndi Kleen struct perf_pmu *pmu; 10624cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 10634cabc3d1SAndi Kleen 10644cabc3d1SAndi Kleen pmu = NULL; 10654cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 10664cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 10674cabc3d1SAndi Kleen continue; 10684cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 10694cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 10704cabc3d1SAndi Kleen return true; 10714cabc3d1SAndi Kleen } 10724cabc3d1SAndi Kleen return false; 10734cabc3d1SAndi Kleen } 10747d4bdab5SAdrian Hunter 10757d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 10767d4bdab5SAdrian Hunter { 10777d4bdab5SAdrian Hunter struct stat st; 10787d4bdab5SAdrian Hunter char path[PATH_MAX]; 10797d4bdab5SAdrian Hunter const char *sysfs; 10807d4bdab5SAdrian Hunter 10817d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 10827d4bdab5SAdrian Hunter if (!sysfs) 10837d4bdab5SAdrian Hunter return NULL; 10847d4bdab5SAdrian Hunter 10857d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 10867d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 10877d4bdab5SAdrian Hunter 10887d4bdab5SAdrian Hunter if (stat(path, &st) < 0) 10897d4bdab5SAdrian Hunter return NULL; 10907d4bdab5SAdrian Hunter 10917d4bdab5SAdrian Hunter return fopen(path, "r"); 10927d4bdab5SAdrian Hunter } 10937d4bdab5SAdrian Hunter 10947d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 10957d4bdab5SAdrian Hunter ...) 10967d4bdab5SAdrian Hunter { 10977d4bdab5SAdrian Hunter va_list args; 10987d4bdab5SAdrian Hunter FILE *file; 10997d4bdab5SAdrian Hunter int ret = EOF; 11007d4bdab5SAdrian Hunter 11017d4bdab5SAdrian Hunter va_start(args, fmt); 11027d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 11037d4bdab5SAdrian Hunter if (file) { 11047d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 11057d4bdab5SAdrian Hunter fclose(file); 11067d4bdab5SAdrian Hunter } 11077d4bdab5SAdrian Hunter va_end(args); 11087d4bdab5SAdrian Hunter return ret; 11097d4bdab5SAdrian Hunter } 1110