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 /* 127410136f5SStephane Eranian * force to C locale to ensure kernel 128410136f5SStephane Eranian * scale string is converted correctly. 129410136f5SStephane Eranian * kernel uses default C locale. 130410136f5SStephane Eranian */ 131410136f5SStephane Eranian setlocale(LC_NUMERIC, "C"); 132410136f5SStephane Eranian 133410136f5SStephane Eranian alias->scale = strtod(scale, NULL); 134410136f5SStephane Eranian 135410136f5SStephane Eranian /* restore locale */ 136410136f5SStephane Eranian setlocale(LC_NUMERIC, lc); 137410136f5SStephane Eranian 138410136f5SStephane Eranian ret = 0; 139410136f5SStephane Eranian error: 140410136f5SStephane Eranian close(fd); 141410136f5SStephane Eranian return ret; 142410136f5SStephane Eranian } 143410136f5SStephane Eranian 144410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) 145410136f5SStephane Eranian { 146410136f5SStephane Eranian char path[PATH_MAX]; 147410136f5SStephane Eranian ssize_t sret; 148410136f5SStephane Eranian int fd; 149410136f5SStephane Eranian 150410136f5SStephane Eranian snprintf(path, PATH_MAX, "%s/%s.unit", dir, name); 151410136f5SStephane Eranian 152410136f5SStephane Eranian fd = open(path, O_RDONLY); 153410136f5SStephane Eranian if (fd == -1) 154410136f5SStephane Eranian return -1; 155410136f5SStephane Eranian 156410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 157410136f5SStephane Eranian if (sret < 0) 158410136f5SStephane Eranian goto error; 159410136f5SStephane Eranian 160410136f5SStephane Eranian close(fd); 161410136f5SStephane Eranian 1629ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 1639ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 1649ecae065SMadhavan Srinivasan else 165410136f5SStephane Eranian alias->unit[sret] = '\0'; 166410136f5SStephane Eranian 167410136f5SStephane Eranian return 0; 168410136f5SStephane Eranian error: 169410136f5SStephane Eranian close(fd); 170410136f5SStephane Eranian alias->unit[0] = '\0'; 171410136f5SStephane Eranian return -1; 172410136f5SStephane Eranian } 173410136f5SStephane Eranian 174044330c1SMatt Fleming static int 175044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) 176044330c1SMatt Fleming { 177044330c1SMatt Fleming char path[PATH_MAX]; 178044330c1SMatt Fleming int fd; 179044330c1SMatt Fleming 180044330c1SMatt Fleming snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); 181044330c1SMatt Fleming 182044330c1SMatt Fleming fd = open(path, O_RDONLY); 183044330c1SMatt Fleming if (fd == -1) 184044330c1SMatt Fleming return -1; 185044330c1SMatt Fleming 186044330c1SMatt Fleming close(fd); 187044330c1SMatt Fleming 188044330c1SMatt Fleming alias->per_pkg = true; 189044330c1SMatt Fleming return 0; 190044330c1SMatt Fleming } 191044330c1SMatt Fleming 1921d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 1931d9e446bSJiri Olsa char *dir, char *name) 1941d9e446bSJiri Olsa { 1951d9e446bSJiri Olsa char path[PATH_MAX]; 1961d9e446bSJiri Olsa int fd; 1971d9e446bSJiri Olsa 1981d9e446bSJiri Olsa snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); 1991d9e446bSJiri Olsa 2001d9e446bSJiri Olsa fd = open(path, O_RDONLY); 2011d9e446bSJiri Olsa if (fd == -1) 2021d9e446bSJiri Olsa return -1; 2031d9e446bSJiri Olsa 2041d9e446bSJiri Olsa alias->snapshot = true; 2051d9e446bSJiri Olsa close(fd); 2061d9e446bSJiri Olsa return 0; 2071d9e446bSJiri Olsa } 2081d9e446bSJiri Olsa 20970c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, 21070c646e0SSukadev Bhattiprolu char *desc __maybe_unused, char *val) 211a6146d50SZheng Yan { 2125c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 213a6146d50SZheng Yan int ret; 214a6146d50SZheng Yan 215a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 216a6146d50SZheng Yan if (!alias) 217a6146d50SZheng Yan return -ENOMEM; 218a6146d50SZheng Yan 219a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 220410136f5SStephane Eranian alias->scale = 1.0; 221410136f5SStephane Eranian alias->unit[0] = '\0'; 222044330c1SMatt Fleming alias->per_pkg = false; 223410136f5SStephane Eranian 22470c646e0SSukadev Bhattiprolu ret = parse_events_terms(&alias->terms, val); 225a6146d50SZheng Yan if (ret) { 22670c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 227a6146d50SZheng Yan free(alias); 228a6146d50SZheng Yan return ret; 229a6146d50SZheng Yan } 230a6146d50SZheng Yan 231a6146d50SZheng Yan alias->name = strdup(name); 23270c646e0SSukadev Bhattiprolu if (dir) { 233410136f5SStephane Eranian /* 234410136f5SStephane Eranian * load unit name and scale if available 235410136f5SStephane Eranian */ 236410136f5SStephane Eranian perf_pmu__parse_unit(alias, dir, name); 237410136f5SStephane Eranian perf_pmu__parse_scale(alias, dir, name); 238044330c1SMatt Fleming perf_pmu__parse_per_pkg(alias, dir, name); 2391d9e446bSJiri Olsa perf_pmu__parse_snapshot(alias, dir, name); 24070c646e0SSukadev Bhattiprolu } 241410136f5SStephane Eranian 242a6146d50SZheng Yan list_add_tail(&alias->list, list); 243410136f5SStephane Eranian 244a6146d50SZheng Yan return 0; 245a6146d50SZheng Yan } 246a6146d50SZheng Yan 24770c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) 24870c646e0SSukadev Bhattiprolu { 24970c646e0SSukadev Bhattiprolu char buf[256]; 25070c646e0SSukadev Bhattiprolu int ret; 25170c646e0SSukadev Bhattiprolu 25270c646e0SSukadev Bhattiprolu ret = fread(buf, 1, sizeof(buf), file); 25370c646e0SSukadev Bhattiprolu if (ret == 0) 25470c646e0SSukadev Bhattiprolu return -EINVAL; 25570c646e0SSukadev Bhattiprolu 25670c646e0SSukadev Bhattiprolu buf[ret] = 0; 25770c646e0SSukadev Bhattiprolu 25870c646e0SSukadev Bhattiprolu return __perf_pmu__new_alias(list, dir, name, NULL, buf); 25970c646e0SSukadev Bhattiprolu } 26070c646e0SSukadev Bhattiprolu 26146441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 26246441bdcSMatt Fleming { 26346441bdcSMatt Fleming size_t len; 26446441bdcSMatt Fleming 26546441bdcSMatt Fleming len = strlen(name); 26646441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 26746441bdcSMatt Fleming return true; 26846441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 26946441bdcSMatt Fleming return true; 270044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 271044330c1SMatt Fleming return true; 2721d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 2731d9e446bSJiri Olsa return true; 27446441bdcSMatt Fleming 27546441bdcSMatt Fleming return false; 27646441bdcSMatt Fleming } 27746441bdcSMatt Fleming 278a6146d50SZheng Yan /* 279a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 280a6146d50SZheng Yan * specified in 'dir' parameter. 281a6146d50SZheng Yan */ 282a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head) 283a6146d50SZheng Yan { 284a6146d50SZheng Yan struct dirent *evt_ent; 285a6146d50SZheng Yan DIR *event_dir; 286a6146d50SZheng Yan int ret = 0; 287a6146d50SZheng Yan 288a6146d50SZheng Yan event_dir = opendir(dir); 289a6146d50SZheng Yan if (!event_dir) 290a6146d50SZheng Yan return -EINVAL; 291a6146d50SZheng Yan 292a6146d50SZheng Yan while (!ret && (evt_ent = readdir(event_dir))) { 293a6146d50SZheng Yan char path[PATH_MAX]; 294a6146d50SZheng Yan char *name = evt_ent->d_name; 295a6146d50SZheng Yan FILE *file; 296a6146d50SZheng Yan 297a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 298a6146d50SZheng Yan continue; 299a6146d50SZheng Yan 300410136f5SStephane Eranian /* 30146441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 302410136f5SStephane Eranian */ 30346441bdcSMatt Fleming if (pmu_alias_info_file(name)) 304410136f5SStephane Eranian continue; 305410136f5SStephane Eranian 306a6146d50SZheng Yan snprintf(path, PATH_MAX, "%s/%s", dir, name); 307a6146d50SZheng Yan 308a6146d50SZheng Yan ret = -EINVAL; 309a6146d50SZheng Yan file = fopen(path, "r"); 310a6146d50SZheng Yan if (!file) 311a6146d50SZheng Yan break; 312410136f5SStephane Eranian 313410136f5SStephane Eranian ret = perf_pmu__new_alias(head, dir, name, file); 314a6146d50SZheng Yan fclose(file); 315a6146d50SZheng Yan } 316a6146d50SZheng Yan 317a6146d50SZheng Yan closedir(event_dir); 318a6146d50SZheng Yan return ret; 319a6146d50SZheng Yan } 320a6146d50SZheng Yan 321a6146d50SZheng Yan /* 322a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 323a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 324a6146d50SZheng Yan */ 325b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head) 326a6146d50SZheng Yan { 327a6146d50SZheng Yan struct stat st; 328a6146d50SZheng Yan char path[PATH_MAX]; 329cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 330a6146d50SZheng Yan 331a6146d50SZheng Yan if (!sysfs) 332a6146d50SZheng Yan return -1; 333a6146d50SZheng Yan 334a6146d50SZheng Yan snprintf(path, PATH_MAX, 335a6146d50SZheng Yan "%s/bus/event_source/devices/%s/events", sysfs, name); 336a6146d50SZheng Yan 337a6146d50SZheng Yan if (stat(path, &st) < 0) 3383fded963SJiri Olsa return 0; /* no error if 'events' does not exist */ 339a6146d50SZheng Yan 340a6146d50SZheng Yan if (pmu_aliases_parse(path, head)) 341a6146d50SZheng Yan return -1; 342a6146d50SZheng Yan 343a6146d50SZheng Yan return 0; 344a6146d50SZheng Yan } 345a6146d50SZheng Yan 3465c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 347a6146d50SZheng Yan struct list_head *terms) 348a6146d50SZheng Yan { 3497c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 350a6146d50SZheng Yan LIST_HEAD(list); 351a6146d50SZheng Yan int ret; 352a6146d50SZheng Yan 353a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 3547c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 355a6146d50SZheng Yan if (ret) { 356a6146d50SZheng Yan parse_events__free_terms(&list); 357a6146d50SZheng Yan return ret; 358a6146d50SZheng Yan } 3597c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 360a6146d50SZheng Yan } 361a6146d50SZheng Yan list_splice(&list, terms); 362a6146d50SZheng Yan return 0; 363a6146d50SZheng Yan } 364a6146d50SZheng Yan 365cd82a32eSJiri Olsa /* 366cd82a32eSJiri Olsa * Reading/parsing the default pmu type value, which should be 367cd82a32eSJiri Olsa * located at: 368cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 369cd82a32eSJiri Olsa */ 370b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type) 371cd82a32eSJiri Olsa { 372cd82a32eSJiri Olsa struct stat st; 373cd82a32eSJiri Olsa char path[PATH_MAX]; 374cd82a32eSJiri Olsa FILE *file; 375cd82a32eSJiri Olsa int ret = 0; 376cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 377cd82a32eSJiri Olsa 378cd82a32eSJiri Olsa if (!sysfs) 379cd82a32eSJiri Olsa return -1; 380cd82a32eSJiri Olsa 381cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 38250a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); 383cd82a32eSJiri Olsa 384cd82a32eSJiri Olsa if (stat(path, &st) < 0) 385cd82a32eSJiri Olsa return -1; 386cd82a32eSJiri Olsa 387cd82a32eSJiri Olsa file = fopen(path, "r"); 388cd82a32eSJiri Olsa if (!file) 389cd82a32eSJiri Olsa return -EINVAL; 390cd82a32eSJiri Olsa 391cd82a32eSJiri Olsa if (1 != fscanf(file, "%u", type)) 392cd82a32eSJiri Olsa ret = -1; 393cd82a32eSJiri Olsa 394cd82a32eSJiri Olsa fclose(file); 395cd82a32eSJiri Olsa return ret; 396cd82a32eSJiri Olsa } 397cd82a32eSJiri Olsa 39850a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */ 39950a9667cSRobert Richter static void pmu_read_sysfs(void) 40050a9667cSRobert Richter { 40150a9667cSRobert Richter char path[PATH_MAX]; 40250a9667cSRobert Richter DIR *dir; 40350a9667cSRobert Richter struct dirent *dent; 404cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 40550a9667cSRobert Richter 40650a9667cSRobert Richter if (!sysfs) 40750a9667cSRobert Richter return; 40850a9667cSRobert Richter 40950a9667cSRobert Richter snprintf(path, PATH_MAX, 41050a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 41150a9667cSRobert Richter 41250a9667cSRobert Richter dir = opendir(path); 41350a9667cSRobert Richter if (!dir) 41450a9667cSRobert Richter return; 41550a9667cSRobert Richter 41650a9667cSRobert Richter while ((dent = readdir(dir))) { 41750a9667cSRobert Richter if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 41850a9667cSRobert Richter continue; 41950a9667cSRobert Richter /* add to static LIST_HEAD(pmus): */ 42050a9667cSRobert Richter perf_pmu__find(dent->d_name); 42150a9667cSRobert Richter } 42250a9667cSRobert Richter 42350a9667cSRobert Richter closedir(dir); 42450a9667cSRobert Richter } 42550a9667cSRobert Richter 426b6b96fb4SAdrian Hunter static struct cpu_map *pmu_cpumask(const char *name) 4277ae92e74SYan, Zheng { 4287ae92e74SYan, Zheng struct stat st; 4297ae92e74SYan, Zheng char path[PATH_MAX]; 4307ae92e74SYan, Zheng FILE *file; 4317ae92e74SYan, Zheng struct cpu_map *cpus; 432cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 4337ae92e74SYan, Zheng 4347ae92e74SYan, Zheng if (!sysfs) 4357ae92e74SYan, Zheng return NULL; 4367ae92e74SYan, Zheng 4377ae92e74SYan, Zheng snprintf(path, PATH_MAX, 4387ae92e74SYan, Zheng "%s/bus/event_source/devices/%s/cpumask", sysfs, name); 4397ae92e74SYan, Zheng 4407ae92e74SYan, Zheng if (stat(path, &st) < 0) 4417ae92e74SYan, Zheng return NULL; 4427ae92e74SYan, Zheng 4437ae92e74SYan, Zheng file = fopen(path, "r"); 4447ae92e74SYan, Zheng if (!file) 4457ae92e74SYan, Zheng return NULL; 4467ae92e74SYan, Zheng 4477ae92e74SYan, Zheng cpus = cpu_map__read(file); 4487ae92e74SYan, Zheng fclose(file); 4497ae92e74SYan, Zheng return cpus; 4507ae92e74SYan, Zheng } 4517ae92e74SYan, Zheng 452c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 453dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 454dc0a6202SAdrian Hunter { 455dc0a6202SAdrian Hunter return NULL; 456dc0a6202SAdrian Hunter } 457dc0a6202SAdrian Hunter 458b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name) 459cd82a32eSJiri Olsa { 460cd82a32eSJiri Olsa struct perf_pmu *pmu; 461cd82a32eSJiri Olsa LIST_HEAD(format); 462a6146d50SZheng Yan LIST_HEAD(aliases); 463cd82a32eSJiri Olsa __u32 type; 464cd82a32eSJiri Olsa 4659b5d1c29SAdrian Hunter /* No support for intel_bts or intel_pt so disallow them */ 4669b5d1c29SAdrian Hunter if (!strcmp(name, "intel_bts") || !strcmp(name, "intel_pt")) 4679b5d1c29SAdrian Hunter return NULL; 4689b5d1c29SAdrian Hunter 469cd82a32eSJiri Olsa /* 470cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 471cd82a32eSJiri Olsa * type value and format definitions. Load both right 472cd82a32eSJiri Olsa * now. 473cd82a32eSJiri Olsa */ 474cd82a32eSJiri Olsa if (pmu_format(name, &format)) 475cd82a32eSJiri Olsa return NULL; 476cd82a32eSJiri Olsa 4773fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 4783fded963SJiri Olsa return NULL; 4793fded963SJiri Olsa 480cd82a32eSJiri Olsa if (pmu_type(name, &type)) 481cd82a32eSJiri Olsa return NULL; 482cd82a32eSJiri Olsa 483cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 484cd82a32eSJiri Olsa if (!pmu) 485cd82a32eSJiri Olsa return NULL; 486cd82a32eSJiri Olsa 4877ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 4887ae92e74SYan, Zheng 489cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 490a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 491cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 492a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 493cd82a32eSJiri Olsa pmu->name = strdup(name); 494cd82a32eSJiri Olsa pmu->type = type; 4959bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 496dc0a6202SAdrian Hunter 497dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 498dc0a6202SAdrian Hunter 499cd82a32eSJiri Olsa return pmu; 500cd82a32eSJiri Olsa } 501cd82a32eSJiri Olsa 502b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 503cd82a32eSJiri Olsa { 504cd82a32eSJiri Olsa struct perf_pmu *pmu; 505cd82a32eSJiri Olsa 506cd82a32eSJiri Olsa list_for_each_entry(pmu, &pmus, list) 507cd82a32eSJiri Olsa if (!strcmp(pmu->name, name)) 508cd82a32eSJiri Olsa return pmu; 509cd82a32eSJiri Olsa 510cd82a32eSJiri Olsa return NULL; 511cd82a32eSJiri Olsa } 512cd82a32eSJiri Olsa 51350a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 51450a9667cSRobert Richter { 51550a9667cSRobert Richter /* 51650a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 51750a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 51850a9667cSRobert Richter */ 51950a9667cSRobert Richter if (!pmu) { 52050a9667cSRobert Richter pmu_read_sysfs(); 52150a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 52250a9667cSRobert Richter } 52350a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 52450a9667cSRobert Richter return pmu; 52550a9667cSRobert Richter return NULL; 52650a9667cSRobert Richter } 52750a9667cSRobert Richter 528b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 529cd82a32eSJiri Olsa { 530cd82a32eSJiri Olsa struct perf_pmu *pmu; 531cd82a32eSJiri Olsa 532cd82a32eSJiri Olsa /* 533cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 534cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 535cd82a32eSJiri Olsa * the pmu format definitions. 536cd82a32eSJiri Olsa */ 537cd82a32eSJiri Olsa pmu = pmu_find(name); 538cd82a32eSJiri Olsa if (pmu) 539cd82a32eSJiri Olsa return pmu; 540cd82a32eSJiri Olsa 541cd82a32eSJiri Olsa return pmu_lookup(name); 542cd82a32eSJiri Olsa } 543cd82a32eSJiri Olsa 5445c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 54509ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 546cd82a32eSJiri Olsa { 5475c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 548cd82a32eSJiri Olsa 549cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 550cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 551cd82a32eSJiri Olsa return format; 552cd82a32eSJiri Olsa 553cd82a32eSJiri Olsa return NULL; 554cd82a32eSJiri Olsa } 555cd82a32eSJiri Olsa 55609ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 55709ff6071SAdrian Hunter { 55809ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 55909ff6071SAdrian Hunter __u64 bits = 0; 56009ff6071SAdrian Hunter int fbit; 56109ff6071SAdrian Hunter 56209ff6071SAdrian Hunter if (!format) 56309ff6071SAdrian Hunter return 0; 56409ff6071SAdrian Hunter 56509ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 56609ff6071SAdrian Hunter bits |= 1ULL << fbit; 56709ff6071SAdrian Hunter 56809ff6071SAdrian Hunter return bits; 56909ff6071SAdrian Hunter } 57009ff6071SAdrian Hunter 571cd82a32eSJiri Olsa /* 572dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 573cd82a32eSJiri Olsa * and unformated value (value parameter). 574cd82a32eSJiri Olsa */ 575dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 576dc0a6202SAdrian Hunter bool zero) 577cd82a32eSJiri Olsa { 578cd82a32eSJiri Olsa unsigned long fbit, vbit; 579cd82a32eSJiri Olsa 580cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 581cd82a32eSJiri Olsa 582cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 583cd82a32eSJiri Olsa continue; 584cd82a32eSJiri Olsa 585dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 586dc0a6202SAdrian Hunter *v |= (1llu << fbit); 587dc0a6202SAdrian Hunter else if (zero) 588dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 589cd82a32eSJiri Olsa } 590cd82a32eSJiri Olsa } 591cd82a32eSJiri Olsa 5920efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 5930efe6b67SAdrian Hunter { 5940efe6b67SAdrian Hunter int w; 5950efe6b67SAdrian Hunter 5960efe6b67SAdrian Hunter w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 5970efe6b67SAdrian Hunter if (!w) 5980efe6b67SAdrian Hunter return 0; 5990efe6b67SAdrian Hunter if (w < 64) 6000efe6b67SAdrian Hunter return (1ULL << w) - 1; 6010efe6b67SAdrian Hunter return -1; 6020efe6b67SAdrian Hunter } 6030efe6b67SAdrian Hunter 604cd82a32eSJiri Olsa /* 605688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 606688d4dfcSCody P Schafer * in the remaining terms. 607688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 608688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 609688d4dfcSCody P Schafer * in a config string) later on in the term list. 610688d4dfcSCody P Schafer */ 611688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 612688d4dfcSCody P Schafer struct list_head *head_terms, 613688d4dfcSCody P Schafer __u64 *value) 614688d4dfcSCody P Schafer { 615688d4dfcSCody P Schafer struct parse_events_term *t; 616688d4dfcSCody P Schafer 617688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 618688d4dfcSCody P Schafer if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 619688d4dfcSCody P Schafer if (!strcmp(t->config, term->config)) { 620688d4dfcSCody P Schafer t->used = true; 621688d4dfcSCody P Schafer *value = t->val.num; 622688d4dfcSCody P Schafer return 0; 623688d4dfcSCody P Schafer } 624688d4dfcSCody P Schafer } 625688d4dfcSCody P Schafer } 626688d4dfcSCody P Schafer 627688d4dfcSCody P Schafer if (verbose) 628688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 629688d4dfcSCody P Schafer 630688d4dfcSCody P Schafer return -1; 631688d4dfcSCody P Schafer } 632688d4dfcSCody P Schafer 633e64b020bSJiri Olsa static char *formats_error_string(struct list_head *formats) 634e64b020bSJiri Olsa { 635e64b020bSJiri Olsa struct perf_pmu_format *format; 636e64b020bSJiri Olsa char *err, *str; 63732067712SKan Liang static const char *static_terms = "config,config1,config2,name,period,branch_type,time\n"; 638e64b020bSJiri Olsa unsigned i = 0; 639e64b020bSJiri Olsa 640e64b020bSJiri Olsa if (!asprintf(&str, "valid terms:")) 641e64b020bSJiri Olsa return NULL; 642e64b020bSJiri Olsa 643e64b020bSJiri Olsa /* sysfs exported terms */ 644e64b020bSJiri Olsa list_for_each_entry(format, formats, list) { 645e64b020bSJiri Olsa char c = i++ ? ',' : ' '; 646e64b020bSJiri Olsa 647e64b020bSJiri Olsa err = str; 648e64b020bSJiri Olsa if (!asprintf(&str, "%s%c%s", err, c, format->name)) 649e64b020bSJiri Olsa goto fail; 650e64b020bSJiri Olsa free(err); 651e64b020bSJiri Olsa } 652e64b020bSJiri Olsa 653e64b020bSJiri Olsa /* static terms */ 654e64b020bSJiri Olsa err = str; 655e64b020bSJiri Olsa if (!asprintf(&str, "%s,%s", err, static_terms)) 656e64b020bSJiri Olsa goto fail; 657e64b020bSJiri Olsa 658e64b020bSJiri Olsa free(err); 659e64b020bSJiri Olsa return str; 660e64b020bSJiri Olsa fail: 661e64b020bSJiri Olsa free(err); 662e64b020bSJiri Olsa return NULL; 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) { 698e64b020bSJiri Olsa err->idx = term->err_term; 699e64b020bSJiri Olsa err->str = strdup("unknown term"); 700e64b020bSJiri Olsa err->help = formats_error_string(formats); 701e64b020bSJiri Olsa } 702cd82a32eSJiri Olsa return -EINVAL; 703688d4dfcSCody P Schafer } 704cd82a32eSJiri Olsa 705cd82a32eSJiri Olsa switch (format->value) { 706cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 707cd82a32eSJiri Olsa vp = &attr->config; 708cd82a32eSJiri Olsa break; 709cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 710cd82a32eSJiri Olsa vp = &attr->config1; 711cd82a32eSJiri Olsa break; 712cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 713cd82a32eSJiri Olsa vp = &attr->config2; 714cd82a32eSJiri Olsa break; 715cd82a32eSJiri Olsa default: 716cd82a32eSJiri Olsa return -EINVAL; 717cd82a32eSJiri Olsa } 718cd82a32eSJiri Olsa 71916fa7e82SJiri Olsa /* 720688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 721688d4dfcSCody P Schafer * using event parameters. 72216fa7e82SJiri Olsa */ 723688d4dfcSCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 724688d4dfcSCody P Schafer val = term->val.num; 725688d4dfcSCody P Schafer else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 726688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 727e64b020bSJiri Olsa if (verbose) { 728688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 729688d4dfcSCody P Schafer term->config, term->val.str); 730e64b020bSJiri Olsa } 731e64b020bSJiri Olsa if (err) { 732e64b020bSJiri Olsa err->idx = term->err_val; 733e64b020bSJiri Olsa err->str = strdup("expected numeric value"); 734e64b020bSJiri Olsa } 735688d4dfcSCody P Schafer return -EINVAL; 736688d4dfcSCody P Schafer } 737688d4dfcSCody P Schafer 738688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 739688d4dfcSCody P Schafer return -EINVAL; 740688d4dfcSCody P Schafer } else 741688d4dfcSCody P Schafer return -EINVAL; 742688d4dfcSCody P Schafer 7430efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 7440efe6b67SAdrian Hunter if (val > max_val) { 7450efe6b67SAdrian Hunter if (err) { 7460efe6b67SAdrian Hunter err->idx = term->err_val; 7470efe6b67SAdrian Hunter if (asprintf(&err->str, 7480efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 7490efe6b67SAdrian Hunter (unsigned long long)max_val) < 0) 7500efe6b67SAdrian Hunter err->str = strdup("value too big for format"); 7510efe6b67SAdrian Hunter return -EINVAL; 7520efe6b67SAdrian Hunter } 7530efe6b67SAdrian Hunter /* 7540efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 7550efe6b67SAdrian Hunter * silently truncated. 7560efe6b67SAdrian Hunter */ 7570efe6b67SAdrian Hunter } 7580efe6b67SAdrian Hunter 759688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 760cd82a32eSJiri Olsa return 0; 761cd82a32eSJiri Olsa } 762cd82a32eSJiri Olsa 763cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats, 764cff7f956SJiri Olsa struct perf_event_attr *attr, 765dc0a6202SAdrian Hunter struct list_head *head_terms, 766e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 767cd82a32eSJiri Olsa { 7686cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 769cd82a32eSJiri Olsa 770688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 771e64b020bSJiri Olsa if (pmu_config_term(formats, attr, term, head_terms, 772e64b020bSJiri Olsa zero, err)) 773cd82a32eSJiri Olsa return -EINVAL; 774688d4dfcSCody P Schafer } 775cd82a32eSJiri Olsa 776cd82a32eSJiri Olsa return 0; 777cd82a32eSJiri Olsa } 778cd82a32eSJiri Olsa 779cd82a32eSJiri Olsa /* 780cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 781cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 782cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 783cd82a32eSJiri Olsa */ 784cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 785e64b020bSJiri Olsa struct list_head *head_terms, 786e64b020bSJiri Olsa struct parse_events_error *err) 787cd82a32eSJiri Olsa { 788dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 789dc0a6202SAdrian Hunter 790cd82a32eSJiri Olsa attr->type = pmu->type; 791e64b020bSJiri Olsa return perf_pmu__config_terms(&pmu->format, attr, head_terms, 792e64b020bSJiri Olsa zero, err); 793cd82a32eSJiri Olsa } 794cd82a32eSJiri Olsa 7955c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 7966cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 797a6146d50SZheng Yan { 7985c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 799a6146d50SZheng Yan char *name; 800a6146d50SZheng Yan 801a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 802a6146d50SZheng Yan return NULL; 803a6146d50SZheng Yan 804a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 805a6146d50SZheng Yan if (term->val.num != 1) 806a6146d50SZheng Yan return NULL; 807a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 808a6146d50SZheng Yan return NULL; 809a6146d50SZheng Yan name = term->config; 810a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 811a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 812a6146d50SZheng Yan return NULL; 813a6146d50SZheng Yan name = term->val.str; 814a6146d50SZheng Yan } else { 815a6146d50SZheng Yan return NULL; 816a6146d50SZheng Yan } 817a6146d50SZheng Yan 818a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 819a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 820a6146d50SZheng Yan return alias; 821a6146d50SZheng Yan } 822a6146d50SZheng Yan return NULL; 823a6146d50SZheng Yan } 824a6146d50SZheng Yan 825410136f5SStephane Eranian 8261d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 8271d9e446bSJiri Olsa struct perf_pmu_info *info) 828410136f5SStephane Eranian { 829410136f5SStephane Eranian /* 830410136f5SStephane Eranian * Only one term in event definition can 8311d9e446bSJiri Olsa * define unit, scale and snapshot, fail 8321d9e446bSJiri Olsa * if there's more than one. 833410136f5SStephane Eranian */ 8341d9e446bSJiri Olsa if ((info->unit && alias->unit) || 8351d9e446bSJiri Olsa (info->scale && alias->scale) || 8361d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 837410136f5SStephane Eranian return -EINVAL; 838410136f5SStephane Eranian 839410136f5SStephane Eranian if (alias->unit) 8401d9e446bSJiri Olsa info->unit = alias->unit; 841410136f5SStephane Eranian 842410136f5SStephane Eranian if (alias->scale) 8431d9e446bSJiri Olsa info->scale = alias->scale; 8441d9e446bSJiri Olsa 8451d9e446bSJiri Olsa if (alias->snapshot) 8461d9e446bSJiri Olsa info->snapshot = alias->snapshot; 847410136f5SStephane Eranian 848410136f5SStephane Eranian return 0; 849410136f5SStephane Eranian } 850410136f5SStephane Eranian 851a6146d50SZheng Yan /* 852a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 853a6146d50SZheng Yan * defined for the alias 854a6146d50SZheng Yan */ 855410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 85646441bdcSMatt Fleming struct perf_pmu_info *info) 857a6146d50SZheng Yan { 8586cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 8595c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 860a6146d50SZheng Yan int ret; 861a6146d50SZheng Yan 862044330c1SMatt Fleming info->per_pkg = false; 863044330c1SMatt Fleming 8648a398897SStephane Eranian /* 8658a398897SStephane Eranian * Mark unit and scale as not set 8668a398897SStephane Eranian * (different from default values, see below) 8678a398897SStephane Eranian */ 86846441bdcSMatt Fleming info->unit = NULL; 86946441bdcSMatt Fleming info->scale = 0.0; 8701d9e446bSJiri Olsa info->snapshot = false; 871410136f5SStephane Eranian 872a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 873a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 874a6146d50SZheng Yan if (!alias) 875a6146d50SZheng Yan continue; 876a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 877a6146d50SZheng Yan if (ret) 878a6146d50SZheng Yan return ret; 879410136f5SStephane Eranian 8801d9e446bSJiri Olsa ret = check_info_data(alias, info); 881410136f5SStephane Eranian if (ret) 882410136f5SStephane Eranian return ret; 883410136f5SStephane Eranian 884044330c1SMatt Fleming if (alias->per_pkg) 885044330c1SMatt Fleming info->per_pkg = true; 886044330c1SMatt Fleming 887a6146d50SZheng Yan list_del(&term->list); 888a6146d50SZheng Yan free(term); 889a6146d50SZheng Yan } 8908a398897SStephane Eranian 8918a398897SStephane Eranian /* 8928a398897SStephane Eranian * if no unit or scale foundin aliases, then 8938a398897SStephane Eranian * set defaults as for evsel 8948a398897SStephane Eranian * unit cannot left to NULL 8958a398897SStephane Eranian */ 89646441bdcSMatt Fleming if (info->unit == NULL) 89746441bdcSMatt Fleming info->unit = ""; 8988a398897SStephane Eranian 89946441bdcSMatt Fleming if (info->scale == 0.0) 90046441bdcSMatt Fleming info->scale = 1.0; 9018a398897SStephane Eranian 902a6146d50SZheng Yan return 0; 903a6146d50SZheng Yan } 904a6146d50SZheng Yan 905cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 906cd82a32eSJiri Olsa int config, unsigned long *bits) 907cd82a32eSJiri Olsa { 9085c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 909cd82a32eSJiri Olsa 910cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 911cd82a32eSJiri Olsa if (!format) 912cd82a32eSJiri Olsa return -ENOMEM; 913cd82a32eSJiri Olsa 914cd82a32eSJiri Olsa format->name = strdup(name); 915cd82a32eSJiri Olsa format->value = config; 916cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 917cd82a32eSJiri Olsa 918cd82a32eSJiri Olsa list_add_tail(&format->list, list); 919cd82a32eSJiri Olsa return 0; 920cd82a32eSJiri Olsa } 921cd82a32eSJiri Olsa 922cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 923cd82a32eSJiri Olsa { 924cd82a32eSJiri Olsa long b; 925cd82a32eSJiri Olsa 926cd82a32eSJiri Olsa if (!to) 927cd82a32eSJiri Olsa to = from; 928cd82a32eSJiri Olsa 92915268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 930cd82a32eSJiri Olsa for (b = from; b <= to; b++) 931cd82a32eSJiri Olsa set_bit(b, bits); 932cd82a32eSJiri Olsa } 933dc098b35SAndi Kleen 934aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 935aaea3617SCody P Schafer { 936aaea3617SCody P Schafer if (b > a) 937aaea3617SCody P Schafer return 0; 938aaea3617SCody P Schafer return a - b; 939aaea3617SCody P Schafer } 940aaea3617SCody P Schafer 941dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 942dc098b35SAndi Kleen struct perf_pmu_alias *alias) 943dc098b35SAndi Kleen { 944aaea3617SCody P Schafer struct parse_events_term *term; 945aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 946aaea3617SCody P Schafer 947aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 948aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 949aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 950aaea3617SCody P Schafer ",%s=%s", term->config, 951aaea3617SCody P Schafer term->val.str); 952aaea3617SCody P Schafer } 953aaea3617SCody P Schafer 954aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 955aaea3617SCody P Schafer buf[used] = '/'; 956aaea3617SCody P Schafer used++; 957aaea3617SCody P Schafer } 958aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 959aaea3617SCody P Schafer buf[used] = '\0'; 960aaea3617SCody P Schafer used++; 961aaea3617SCody P Schafer } else 962aaea3617SCody P Schafer buf[len - 1] = '\0'; 963aaea3617SCody P Schafer 964dc098b35SAndi Kleen return buf; 965dc098b35SAndi Kleen } 966dc098b35SAndi Kleen 967dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 968dc098b35SAndi Kleen struct perf_pmu_alias *alias) 969dc098b35SAndi Kleen { 970dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 971dc098b35SAndi Kleen return buf; 972dc098b35SAndi Kleen } 973dc098b35SAndi Kleen 974dc098b35SAndi Kleen static int cmp_string(const void *a, const void *b) 975dc098b35SAndi Kleen { 976dc098b35SAndi Kleen const char * const *as = a; 977dc098b35SAndi Kleen const char * const *bs = b; 978dc098b35SAndi Kleen return strcmp(*as, *bs); 979dc098b35SAndi Kleen } 980dc098b35SAndi Kleen 981dc098b35SAndi Kleen void print_pmu_events(const char *event_glob, bool name_only) 982dc098b35SAndi Kleen { 983dc098b35SAndi Kleen struct perf_pmu *pmu; 984dc098b35SAndi Kleen struct perf_pmu_alias *alias; 985dc098b35SAndi Kleen char buf[1024]; 986dc098b35SAndi Kleen int printed = 0; 987dc098b35SAndi Kleen int len, j; 988dc098b35SAndi Kleen char **aliases; 989dc098b35SAndi Kleen 990dc098b35SAndi Kleen pmu = NULL; 991dc098b35SAndi Kleen len = 0; 99242634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 993dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 994dc098b35SAndi Kleen len++; 99542634bc7SAdrian Hunter if (pmu->selectable) 99642634bc7SAdrian Hunter len++; 99742634bc7SAdrian Hunter } 9987e4772dcSArnaldo Carvalho de Melo aliases = zalloc(sizeof(char *) * len); 999dc098b35SAndi Kleen if (!aliases) 10007e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1001dc098b35SAndi Kleen pmu = NULL; 1002dc098b35SAndi Kleen j = 0; 100342634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1004dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 1005dc098b35SAndi Kleen char *name = format_alias(buf, sizeof(buf), pmu, alias); 1006dc098b35SAndi Kleen bool is_cpu = !strcmp(pmu->name, "cpu"); 1007dc098b35SAndi Kleen 1008dc098b35SAndi Kleen if (event_glob != NULL && 1009dc098b35SAndi Kleen !(strglobmatch(name, event_glob) || 1010dc098b35SAndi Kleen (!is_cpu && strglobmatch(alias->name, 1011dc098b35SAndi Kleen event_glob)))) 1012dc098b35SAndi Kleen continue; 10137e4772dcSArnaldo Carvalho de Melo 1014dc098b35SAndi Kleen if (is_cpu && !name_only) 10157e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 10167e4772dcSArnaldo Carvalho de Melo 10177e4772dcSArnaldo Carvalho de Melo aliases[j] = strdup(name); 10187e4772dcSArnaldo Carvalho de Melo if (aliases[j] == NULL) 10197e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1020dc098b35SAndi Kleen j++; 1021dc098b35SAndi Kleen } 102242634bc7SAdrian Hunter if (pmu->selectable) { 10237e4772dcSArnaldo Carvalho de Melo char *s; 10247e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 10257e4772dcSArnaldo Carvalho de Melo goto out_enomem; 10267e4772dcSArnaldo Carvalho de Melo aliases[j] = s; 102742634bc7SAdrian Hunter j++; 102842634bc7SAdrian Hunter } 102942634bc7SAdrian Hunter } 1030dc098b35SAndi Kleen len = j; 1031dc098b35SAndi Kleen qsort(aliases, len, sizeof(char *), cmp_string); 1032dc098b35SAndi Kleen for (j = 0; j < len; j++) { 1033dc098b35SAndi Kleen if (name_only) { 1034dc098b35SAndi Kleen printf("%s ", aliases[j]); 1035dc098b35SAndi Kleen continue; 1036dc098b35SAndi Kleen } 1037dc098b35SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j]); 1038dc098b35SAndi Kleen printed++; 1039dc098b35SAndi Kleen } 1040dc098b35SAndi Kleen if (printed) 1041dc098b35SAndi Kleen printf("\n"); 10427e4772dcSArnaldo Carvalho de Melo out_free: 10437e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 10447e4772dcSArnaldo Carvalho de Melo zfree(&aliases[j]); 10457e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 10467e4772dcSArnaldo Carvalho de Melo return; 10477e4772dcSArnaldo Carvalho de Melo 10487e4772dcSArnaldo Carvalho de Melo out_enomem: 10497e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 10507e4772dcSArnaldo Carvalho de Melo if (aliases) 10517e4772dcSArnaldo Carvalho de Melo goto out_free; 1052dc098b35SAndi Kleen } 10534cabc3d1SAndi Kleen 10544cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 10554cabc3d1SAndi Kleen { 10564cabc3d1SAndi Kleen struct perf_pmu *pmu; 10574cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 10584cabc3d1SAndi Kleen 10594cabc3d1SAndi Kleen pmu = NULL; 10604cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 10614cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 10624cabc3d1SAndi Kleen continue; 10634cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 10644cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 10654cabc3d1SAndi Kleen return true; 10664cabc3d1SAndi Kleen } 10674cabc3d1SAndi Kleen return false; 10684cabc3d1SAndi Kleen } 10697d4bdab5SAdrian Hunter 10707d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 10717d4bdab5SAdrian Hunter { 10727d4bdab5SAdrian Hunter struct stat st; 10737d4bdab5SAdrian Hunter char path[PATH_MAX]; 10747d4bdab5SAdrian Hunter const char *sysfs; 10757d4bdab5SAdrian Hunter 10767d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 10777d4bdab5SAdrian Hunter if (!sysfs) 10787d4bdab5SAdrian Hunter return NULL; 10797d4bdab5SAdrian Hunter 10807d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 10817d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 10827d4bdab5SAdrian Hunter 10837d4bdab5SAdrian Hunter if (stat(path, &st) < 0) 10847d4bdab5SAdrian Hunter return NULL; 10857d4bdab5SAdrian Hunter 10867d4bdab5SAdrian Hunter return fopen(path, "r"); 10877d4bdab5SAdrian Hunter } 10887d4bdab5SAdrian Hunter 10897d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 10907d4bdab5SAdrian Hunter ...) 10917d4bdab5SAdrian Hunter { 10927d4bdab5SAdrian Hunter va_list args; 10937d4bdab5SAdrian Hunter FILE *file; 10947d4bdab5SAdrian Hunter int ret = EOF; 10957d4bdab5SAdrian Hunter 10967d4bdab5SAdrian Hunter va_start(args, fmt); 10977d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 10987d4bdab5SAdrian Hunter if (file) { 10997d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 11007d4bdab5SAdrian Hunter fclose(file); 11017d4bdab5SAdrian Hunter } 11027d4bdab5SAdrian Hunter va_end(args); 11037d4bdab5SAdrian Hunter return ret; 11047d4bdab5SAdrian Hunter } 1105