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]; 101ea8f75f9SJiri Olsa 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 /* 127f9a5978aSJiri Olsa * The lc string may be allocated in static storage, 128f9a5978aSJiri Olsa * so get a dynamic copy to make it survive setlocale 129f9a5978aSJiri Olsa * call below. 130f9a5978aSJiri Olsa */ 131f9a5978aSJiri Olsa lc = strdup(lc); 132f9a5978aSJiri Olsa if (!lc) { 133f9a5978aSJiri Olsa ret = -ENOMEM; 134f9a5978aSJiri Olsa goto error; 135f9a5978aSJiri Olsa } 136f9a5978aSJiri Olsa 137f9a5978aSJiri 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 149ea8f75f9SJiri Olsa free(lc); 150f9a5978aSJiri 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 { 605*ac0e2cd5SKan Liang __u64 w = 0; 606*ac0e2cd5SKan Liang int fbit; 6070efe6b67SAdrian Hunter 608*ac0e2cd5SKan Liang for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS) 609*ac0e2cd5SKan Liang w |= (1ULL << fbit); 610*ac0e2cd5SKan Liang 611*ac0e2cd5SKan Liang return w; 6120efe6b67SAdrian Hunter } 6130efe6b67SAdrian Hunter 614cd82a32eSJiri Olsa /* 615688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 616688d4dfcSCody P Schafer * in the remaining terms. 617688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 618688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 619688d4dfcSCody P Schafer * in a config string) later on in the term list. 620688d4dfcSCody P Schafer */ 621688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 622688d4dfcSCody P Schafer struct list_head *head_terms, 623688d4dfcSCody P Schafer __u64 *value) 624688d4dfcSCody P Schafer { 625688d4dfcSCody P Schafer struct parse_events_term *t; 626688d4dfcSCody P Schafer 627688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 628688d4dfcSCody P Schafer if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 629688d4dfcSCody P Schafer if (!strcmp(t->config, term->config)) { 630688d4dfcSCody P Schafer t->used = true; 631688d4dfcSCody P Schafer *value = t->val.num; 632688d4dfcSCody P Schafer return 0; 633688d4dfcSCody P Schafer } 634688d4dfcSCody P Schafer } 635688d4dfcSCody P Schafer } 636688d4dfcSCody P Schafer 637688d4dfcSCody P Schafer if (verbose) 638688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 639688d4dfcSCody P Schafer 640688d4dfcSCody P Schafer return -1; 641688d4dfcSCody P Schafer } 642688d4dfcSCody P Schafer 643ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 644e64b020bSJiri Olsa { 645e64b020bSJiri Olsa struct perf_pmu_format *format; 646ffeb883eSHe Kuang char *str; 647ffeb883eSHe Kuang struct strbuf buf; 648e64b020bSJiri Olsa unsigned i = 0; 649e64b020bSJiri Olsa 650ffeb883eSHe Kuang if (!formats) 651e64b020bSJiri Olsa return NULL; 652e64b020bSJiri Olsa 653ffeb883eSHe Kuang strbuf_init(&buf, 0); 654e64b020bSJiri Olsa /* sysfs exported terms */ 655ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 656ffeb883eSHe Kuang strbuf_addf(&buf, i++ ? ",%s" : "%s", 657ffeb883eSHe Kuang format->name); 658e64b020bSJiri Olsa 659ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 660ffeb883eSHe Kuang strbuf_release(&buf); 661e64b020bSJiri Olsa 662e64b020bSJiri Olsa return str; 663e64b020bSJiri Olsa } 664e64b020bSJiri Olsa 665688d4dfcSCody P Schafer /* 666cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 66788aca8d9SCody P Schafer * user input data - term parameter. 668cd82a32eSJiri Olsa */ 669cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats, 670cd82a32eSJiri Olsa struct perf_event_attr *attr, 671dc0a6202SAdrian Hunter struct parse_events_term *term, 672688d4dfcSCody P Schafer struct list_head *head_terms, 673e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 674cd82a32eSJiri Olsa { 6755c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 676cd82a32eSJiri Olsa __u64 *vp; 6770efe6b67SAdrian Hunter __u64 val, max_val; 678cd82a32eSJiri Olsa 679cd82a32eSJiri Olsa /* 680688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 681688d4dfcSCody P Schafer * skip it in normal eval. 682688d4dfcSCody P Schafer */ 683688d4dfcSCody P Schafer if (term->used) 684688d4dfcSCody P Schafer return 0; 685688d4dfcSCody P Schafer 686688d4dfcSCody P Schafer /* 687cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 688cd82a32eSJiri Olsa * to be done for them. 689cd82a32eSJiri Olsa */ 690cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 691cd82a32eSJiri Olsa return 0; 692cd82a32eSJiri Olsa 693cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 694688d4dfcSCody P Schafer if (!format) { 695688d4dfcSCody P Schafer if (verbose) 696688d4dfcSCody P Schafer printf("Invalid event/parameter '%s'\n", term->config); 697e64b020bSJiri Olsa if (err) { 698ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 699ffeb883eSHe Kuang 700e64b020bSJiri Olsa err->idx = term->err_term; 701e64b020bSJiri Olsa err->str = strdup("unknown term"); 702ffeb883eSHe Kuang err->help = parse_events_formats_error_string(pmu_term); 703ffeb883eSHe Kuang free(pmu_term); 704e64b020bSJiri Olsa } 705cd82a32eSJiri Olsa return -EINVAL; 706688d4dfcSCody P Schafer } 707cd82a32eSJiri Olsa 708cd82a32eSJiri Olsa switch (format->value) { 709cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 710cd82a32eSJiri Olsa vp = &attr->config; 711cd82a32eSJiri Olsa break; 712cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 713cd82a32eSJiri Olsa vp = &attr->config1; 714cd82a32eSJiri Olsa break; 715cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 716cd82a32eSJiri Olsa vp = &attr->config2; 717cd82a32eSJiri Olsa break; 718cd82a32eSJiri Olsa default: 719cd82a32eSJiri Olsa return -EINVAL; 720cd82a32eSJiri Olsa } 721cd82a32eSJiri Olsa 72216fa7e82SJiri Olsa /* 723688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 724688d4dfcSCody P Schafer * using event parameters. 72516fa7e82SJiri Olsa */ 726688d4dfcSCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 727688d4dfcSCody P Schafer val = term->val.num; 728688d4dfcSCody P Schafer else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 729688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 730e64b020bSJiri Olsa if (verbose) { 731688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 732688d4dfcSCody P Schafer term->config, term->val.str); 733e64b020bSJiri Olsa } 734e64b020bSJiri Olsa if (err) { 735e64b020bSJiri Olsa err->idx = term->err_val; 736e64b020bSJiri Olsa err->str = strdup("expected numeric value"); 737e64b020bSJiri Olsa } 738688d4dfcSCody P Schafer return -EINVAL; 739688d4dfcSCody P Schafer } 740688d4dfcSCody P Schafer 741688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 742688d4dfcSCody P Schafer return -EINVAL; 743688d4dfcSCody P Schafer } else 744688d4dfcSCody P Schafer return -EINVAL; 745688d4dfcSCody P Schafer 7460efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 7470efe6b67SAdrian Hunter if (val > max_val) { 7480efe6b67SAdrian Hunter if (err) { 7490efe6b67SAdrian Hunter err->idx = term->err_val; 7500efe6b67SAdrian Hunter if (asprintf(&err->str, 7510efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 7520efe6b67SAdrian Hunter (unsigned long long)max_val) < 0) 7530efe6b67SAdrian Hunter err->str = strdup("value too big for format"); 7540efe6b67SAdrian Hunter return -EINVAL; 7550efe6b67SAdrian Hunter } 7560efe6b67SAdrian Hunter /* 7570efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 7580efe6b67SAdrian Hunter * silently truncated. 7590efe6b67SAdrian Hunter */ 7600efe6b67SAdrian Hunter } 7610efe6b67SAdrian Hunter 762688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 763cd82a32eSJiri Olsa return 0; 764cd82a32eSJiri Olsa } 765cd82a32eSJiri Olsa 766cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats, 767cff7f956SJiri Olsa struct perf_event_attr *attr, 768dc0a6202SAdrian Hunter struct list_head *head_terms, 769e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 770cd82a32eSJiri Olsa { 7716cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 772cd82a32eSJiri Olsa 773688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 774e64b020bSJiri Olsa if (pmu_config_term(formats, attr, term, head_terms, 775e64b020bSJiri Olsa zero, err)) 776cd82a32eSJiri Olsa return -EINVAL; 777688d4dfcSCody P Schafer } 778cd82a32eSJiri Olsa 779cd82a32eSJiri Olsa return 0; 780cd82a32eSJiri Olsa } 781cd82a32eSJiri Olsa 782cd82a32eSJiri Olsa /* 783cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 784cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 785cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 786cd82a32eSJiri Olsa */ 787cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 788e64b020bSJiri Olsa struct list_head *head_terms, 789e64b020bSJiri Olsa struct parse_events_error *err) 790cd82a32eSJiri Olsa { 791dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 792dc0a6202SAdrian Hunter 793cd82a32eSJiri Olsa attr->type = pmu->type; 794e64b020bSJiri Olsa return perf_pmu__config_terms(&pmu->format, attr, head_terms, 795e64b020bSJiri Olsa zero, err); 796cd82a32eSJiri Olsa } 797cd82a32eSJiri Olsa 7985c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 7996cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 800a6146d50SZheng Yan { 8015c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 802a6146d50SZheng Yan char *name; 803a6146d50SZheng Yan 804a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 805a6146d50SZheng Yan return NULL; 806a6146d50SZheng Yan 807a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 808a6146d50SZheng Yan if (term->val.num != 1) 809a6146d50SZheng Yan return NULL; 810a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 811a6146d50SZheng Yan return NULL; 812a6146d50SZheng Yan name = term->config; 813a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 814a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 815a6146d50SZheng Yan return NULL; 816a6146d50SZheng Yan name = term->val.str; 817a6146d50SZheng Yan } else { 818a6146d50SZheng Yan return NULL; 819a6146d50SZheng Yan } 820a6146d50SZheng Yan 821a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 822a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 823a6146d50SZheng Yan return alias; 824a6146d50SZheng Yan } 825a6146d50SZheng Yan return NULL; 826a6146d50SZheng Yan } 827a6146d50SZheng Yan 828410136f5SStephane Eranian 8291d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 8301d9e446bSJiri Olsa struct perf_pmu_info *info) 831410136f5SStephane Eranian { 832410136f5SStephane Eranian /* 833410136f5SStephane Eranian * Only one term in event definition can 8341d9e446bSJiri Olsa * define unit, scale and snapshot, fail 8351d9e446bSJiri Olsa * if there's more than one. 836410136f5SStephane Eranian */ 8371d9e446bSJiri Olsa if ((info->unit && alias->unit) || 8381d9e446bSJiri Olsa (info->scale && alias->scale) || 8391d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 840410136f5SStephane Eranian return -EINVAL; 841410136f5SStephane Eranian 842410136f5SStephane Eranian if (alias->unit) 8431d9e446bSJiri Olsa info->unit = alias->unit; 844410136f5SStephane Eranian 845410136f5SStephane Eranian if (alias->scale) 8461d9e446bSJiri Olsa info->scale = alias->scale; 8471d9e446bSJiri Olsa 8481d9e446bSJiri Olsa if (alias->snapshot) 8491d9e446bSJiri Olsa info->snapshot = alias->snapshot; 850410136f5SStephane Eranian 851410136f5SStephane Eranian return 0; 852410136f5SStephane Eranian } 853410136f5SStephane Eranian 854a6146d50SZheng Yan /* 855a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 856a6146d50SZheng Yan * defined for the alias 857a6146d50SZheng Yan */ 858410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 85946441bdcSMatt Fleming struct perf_pmu_info *info) 860a6146d50SZheng Yan { 8616cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 8625c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 863a6146d50SZheng Yan int ret; 864a6146d50SZheng Yan 865044330c1SMatt Fleming info->per_pkg = false; 866044330c1SMatt Fleming 8678a398897SStephane Eranian /* 8688a398897SStephane Eranian * Mark unit and scale as not set 8698a398897SStephane Eranian * (different from default values, see below) 8708a398897SStephane Eranian */ 87146441bdcSMatt Fleming info->unit = NULL; 87246441bdcSMatt Fleming info->scale = 0.0; 8731d9e446bSJiri Olsa info->snapshot = false; 874410136f5SStephane Eranian 875a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 876a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 877a6146d50SZheng Yan if (!alias) 878a6146d50SZheng Yan continue; 879a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 880a6146d50SZheng Yan if (ret) 881a6146d50SZheng Yan return ret; 882410136f5SStephane Eranian 8831d9e446bSJiri Olsa ret = check_info_data(alias, info); 884410136f5SStephane Eranian if (ret) 885410136f5SStephane Eranian return ret; 886410136f5SStephane Eranian 887044330c1SMatt Fleming if (alias->per_pkg) 888044330c1SMatt Fleming info->per_pkg = true; 889044330c1SMatt Fleming 890a6146d50SZheng Yan list_del(&term->list); 891a6146d50SZheng Yan free(term); 892a6146d50SZheng Yan } 8938a398897SStephane Eranian 8948a398897SStephane Eranian /* 8958a398897SStephane Eranian * if no unit or scale foundin aliases, then 8968a398897SStephane Eranian * set defaults as for evsel 8978a398897SStephane Eranian * unit cannot left to NULL 8988a398897SStephane Eranian */ 89946441bdcSMatt Fleming if (info->unit == NULL) 90046441bdcSMatt Fleming info->unit = ""; 9018a398897SStephane Eranian 90246441bdcSMatt Fleming if (info->scale == 0.0) 90346441bdcSMatt Fleming info->scale = 1.0; 9048a398897SStephane Eranian 905a6146d50SZheng Yan return 0; 906a6146d50SZheng Yan } 907a6146d50SZheng Yan 908cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 909cd82a32eSJiri Olsa int config, unsigned long *bits) 910cd82a32eSJiri Olsa { 9115c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 912cd82a32eSJiri Olsa 913cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 914cd82a32eSJiri Olsa if (!format) 915cd82a32eSJiri Olsa return -ENOMEM; 916cd82a32eSJiri Olsa 917cd82a32eSJiri Olsa format->name = strdup(name); 918cd82a32eSJiri Olsa format->value = config; 919cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 920cd82a32eSJiri Olsa 921cd82a32eSJiri Olsa list_add_tail(&format->list, list); 922cd82a32eSJiri Olsa return 0; 923cd82a32eSJiri Olsa } 924cd82a32eSJiri Olsa 925cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 926cd82a32eSJiri Olsa { 927cd82a32eSJiri Olsa long b; 928cd82a32eSJiri Olsa 929cd82a32eSJiri Olsa if (!to) 930cd82a32eSJiri Olsa to = from; 931cd82a32eSJiri Olsa 93215268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 933cd82a32eSJiri Olsa for (b = from; b <= to; b++) 934cd82a32eSJiri Olsa set_bit(b, bits); 935cd82a32eSJiri Olsa } 936dc098b35SAndi Kleen 937aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 938aaea3617SCody P Schafer { 939aaea3617SCody P Schafer if (b > a) 940aaea3617SCody P Schafer return 0; 941aaea3617SCody P Schafer return a - b; 942aaea3617SCody P Schafer } 943aaea3617SCody P Schafer 944dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 945dc098b35SAndi Kleen struct perf_pmu_alias *alias) 946dc098b35SAndi Kleen { 947aaea3617SCody P Schafer struct parse_events_term *term; 948aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 949aaea3617SCody P Schafer 950aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 951aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 952aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 953aaea3617SCody P Schafer ",%s=%s", term->config, 954aaea3617SCody P Schafer term->val.str); 955aaea3617SCody P Schafer } 956aaea3617SCody P Schafer 957aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 958aaea3617SCody P Schafer buf[used] = '/'; 959aaea3617SCody P Schafer used++; 960aaea3617SCody P Schafer } 961aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 962aaea3617SCody P Schafer buf[used] = '\0'; 963aaea3617SCody P Schafer used++; 964aaea3617SCody P Schafer } else 965aaea3617SCody P Schafer buf[len - 1] = '\0'; 966aaea3617SCody P Schafer 967dc098b35SAndi Kleen return buf; 968dc098b35SAndi Kleen } 969dc098b35SAndi Kleen 970dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 971dc098b35SAndi Kleen struct perf_pmu_alias *alias) 972dc098b35SAndi Kleen { 973dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 974dc098b35SAndi Kleen return buf; 975dc098b35SAndi Kleen } 976dc098b35SAndi Kleen 977dc098b35SAndi Kleen static int cmp_string(const void *a, const void *b) 978dc098b35SAndi Kleen { 979dc098b35SAndi Kleen const char * const *as = a; 980dc098b35SAndi Kleen const char * const *bs = b; 981dc098b35SAndi Kleen return strcmp(*as, *bs); 982dc098b35SAndi Kleen } 983dc098b35SAndi Kleen 984dc098b35SAndi Kleen void print_pmu_events(const char *event_glob, bool name_only) 985dc098b35SAndi Kleen { 986dc098b35SAndi Kleen struct perf_pmu *pmu; 987dc098b35SAndi Kleen struct perf_pmu_alias *alias; 988dc098b35SAndi Kleen char buf[1024]; 989dc098b35SAndi Kleen int printed = 0; 990dc098b35SAndi Kleen int len, j; 991dc098b35SAndi Kleen char **aliases; 992dc098b35SAndi Kleen 993dc098b35SAndi Kleen pmu = NULL; 994dc098b35SAndi Kleen len = 0; 99542634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 996dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 997dc098b35SAndi Kleen len++; 99842634bc7SAdrian Hunter if (pmu->selectable) 99942634bc7SAdrian Hunter len++; 100042634bc7SAdrian Hunter } 10017e4772dcSArnaldo Carvalho de Melo aliases = zalloc(sizeof(char *) * len); 1002dc098b35SAndi Kleen if (!aliases) 10037e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1004dc098b35SAndi Kleen pmu = NULL; 1005dc098b35SAndi Kleen j = 0; 100642634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1007dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 1008dc098b35SAndi Kleen char *name = format_alias(buf, sizeof(buf), pmu, alias); 1009dc098b35SAndi Kleen bool is_cpu = !strcmp(pmu->name, "cpu"); 1010dc098b35SAndi Kleen 1011dc098b35SAndi Kleen if (event_glob != NULL && 1012dc098b35SAndi Kleen !(strglobmatch(name, event_glob) || 1013dc098b35SAndi Kleen (!is_cpu && strglobmatch(alias->name, 1014dc098b35SAndi Kleen event_glob)))) 1015dc098b35SAndi Kleen continue; 10167e4772dcSArnaldo Carvalho de Melo 1017dc098b35SAndi Kleen if (is_cpu && !name_only) 10187e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 10197e4772dcSArnaldo Carvalho de Melo 10207e4772dcSArnaldo Carvalho de Melo aliases[j] = strdup(name); 10217e4772dcSArnaldo Carvalho de Melo if (aliases[j] == NULL) 10227e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1023dc098b35SAndi Kleen j++; 1024dc098b35SAndi Kleen } 1025fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1026fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 10277e4772dcSArnaldo Carvalho de Melo char *s; 10287e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 10297e4772dcSArnaldo Carvalho de Melo goto out_enomem; 10307e4772dcSArnaldo Carvalho de Melo aliases[j] = s; 103142634bc7SAdrian Hunter j++; 103242634bc7SAdrian Hunter } 103342634bc7SAdrian Hunter } 1034dc098b35SAndi Kleen len = j; 1035dc098b35SAndi Kleen qsort(aliases, len, sizeof(char *), cmp_string); 1036dc098b35SAndi Kleen for (j = 0; j < len; j++) { 1037dc098b35SAndi Kleen if (name_only) { 1038dc098b35SAndi Kleen printf("%s ", aliases[j]); 1039dc098b35SAndi Kleen continue; 1040dc098b35SAndi Kleen } 1041dc098b35SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j]); 1042dc098b35SAndi Kleen printed++; 1043dc098b35SAndi Kleen } 1044dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1045dc098b35SAndi Kleen printf("\n"); 10467e4772dcSArnaldo Carvalho de Melo out_free: 10477e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 10487e4772dcSArnaldo Carvalho de Melo zfree(&aliases[j]); 10497e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 10507e4772dcSArnaldo Carvalho de Melo return; 10517e4772dcSArnaldo Carvalho de Melo 10527e4772dcSArnaldo Carvalho de Melo out_enomem: 10537e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 10547e4772dcSArnaldo Carvalho de Melo if (aliases) 10557e4772dcSArnaldo Carvalho de Melo goto out_free; 1056dc098b35SAndi Kleen } 10574cabc3d1SAndi Kleen 10584cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 10594cabc3d1SAndi Kleen { 10604cabc3d1SAndi Kleen struct perf_pmu *pmu; 10614cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 10624cabc3d1SAndi Kleen 10634cabc3d1SAndi Kleen pmu = NULL; 10644cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 10654cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 10664cabc3d1SAndi Kleen continue; 10674cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 10684cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 10694cabc3d1SAndi Kleen return true; 10704cabc3d1SAndi Kleen } 10714cabc3d1SAndi Kleen return false; 10724cabc3d1SAndi Kleen } 10737d4bdab5SAdrian Hunter 10747d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 10757d4bdab5SAdrian Hunter { 10767d4bdab5SAdrian Hunter struct stat st; 10777d4bdab5SAdrian Hunter char path[PATH_MAX]; 10787d4bdab5SAdrian Hunter const char *sysfs; 10797d4bdab5SAdrian Hunter 10807d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 10817d4bdab5SAdrian Hunter if (!sysfs) 10827d4bdab5SAdrian Hunter return NULL; 10837d4bdab5SAdrian Hunter 10847d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 10857d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 10867d4bdab5SAdrian Hunter 10877d4bdab5SAdrian Hunter if (stat(path, &st) < 0) 10887d4bdab5SAdrian Hunter return NULL; 10897d4bdab5SAdrian Hunter 10907d4bdab5SAdrian Hunter return fopen(path, "r"); 10917d4bdab5SAdrian Hunter } 10927d4bdab5SAdrian Hunter 10937d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 10947d4bdab5SAdrian Hunter ...) 10957d4bdab5SAdrian Hunter { 10967d4bdab5SAdrian Hunter va_list args; 10977d4bdab5SAdrian Hunter FILE *file; 10987d4bdab5SAdrian Hunter int ret = EOF; 10997d4bdab5SAdrian Hunter 11007d4bdab5SAdrian Hunter va_start(args, fmt); 11017d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 11027d4bdab5SAdrian Hunter if (file) { 11037d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 11047d4bdab5SAdrian Hunter fclose(file); 11057d4bdab5SAdrian Hunter } 11067d4bdab5SAdrian Hunter va_end(args); 11077d4bdab5SAdrian Hunter return ret; 11087d4bdab5SAdrian Hunter } 1109