]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - tools/perf/util/pmu.c
perf pmu: Add override support for event list CPUID
[linux.git] / tools / perf / util / pmu.c
index adef23b1352e836fec9f0f9a5290c578bb25cc43..79242cf9bb79937a2a0ff172e23923443e111b9e 100644 (file)
@@ -12,6 +12,9 @@
 #include "pmu.h"
 #include "parse-events.h"
 #include "cpumap.h"
+#include "header.h"
+#include "pmu-events/pmu-events.h"
+#include "cache.h"
 
 struct perf_pmu_format {
        char *name;
@@ -220,7 +223,7 @@ static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias,
 }
 
 static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
-                                char *desc __maybe_unused, char *val)
+                                char *desc, char *val)
 {
        struct perf_pmu_alias *alias;
        int ret;
@@ -253,6 +256,8 @@ static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name,
                perf_pmu__parse_snapshot(alias, dir, name);
        }
 
+       alias->desc = desc ? strdup(desc) : NULL;
+
        list_add_tail(&alias->list, list);
 
        return 0;
@@ -445,14 +450,23 @@ static struct cpu_map *pmu_cpumask(const char *name)
        FILE *file;
        struct cpu_map *cpus;
        const char *sysfs = sysfs__mountpoint();
+       const char *templates[] = {
+                "%s/bus/event_source/devices/%s/cpumask",
+                "%s/bus/event_source/devices/%s/cpus",
+                NULL
+       };
+       const char **template;
 
        if (!sysfs)
                return NULL;
 
-       snprintf(path, PATH_MAX,
-                "%s/bus/event_source/devices/%s/cpumask", sysfs, name);
+       for (template = templates; *template; template++) {
+               snprintf(path, PATH_MAX, *template, sysfs, name);
+               if (stat(path, &st) == 0)
+                       break;
+       }
 
-       if (stat(path, &st) < 0)
+       if (!*template)
                return NULL;
 
        file = fopen(path, "r");
@@ -464,6 +478,67 @@ static struct cpu_map *pmu_cpumask(const char *name)
        return cpus;
 }
 
+/*
+ * Return the CPU id as a raw string.
+ *
+ * Each architecture should provide a more precise id string that
+ * can be use to match the architecture's "mapfile".
+ */
+char * __weak get_cpuid_str(void)
+{
+       return NULL;
+}
+
+/*
+ * From the pmu_events_map, find the table of PMU events that corresponds
+ * to the current running CPU. Then, add all PMU events from that table
+ * as aliases.
+ */
+static void pmu_add_cpu_aliases(struct list_head *head)
+{
+       int i;
+       struct pmu_events_map *map;
+       struct pmu_event *pe;
+       char *cpuid;
+
+       cpuid = getenv("PERF_CPUID");
+       if (cpuid)
+               cpuid = strdup(cpuid);
+       if (!cpuid)
+               cpuid = get_cpuid_str();
+       if (!cpuid)
+               return;
+
+       pr_debug("Using CPUID %s\n", cpuid);
+
+       i = 0;
+       while (1) {
+               map = &pmu_events_map[i++];
+               if (!map->table)
+                       goto out;
+
+               if (!strcmp(map->cpuid, cpuid))
+                       break;
+       }
+
+       /*
+        * Found a matching PMU events table. Create aliases
+        */
+       i = 0;
+       while (1) {
+               pe = &map->table[i++];
+               if (!pe->name)
+                       break;
+
+               /* need type casts to override 'const' */
+               __perf_pmu__new_alias(head, NULL, (char *)pe->name,
+                               (char *)pe->desc, (char *)pe->event);
+       }
+
+out:
+       free(cpuid);
+}
+
 struct perf_event_attr * __weak
 perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused)
 {
@@ -488,6 +563,9 @@ static struct perf_pmu *pmu_lookup(const char *name)
        if (pmu_aliases(name, &aliases))
                return NULL;
 
+       if (!strcmp(name, "cpu"))
+               pmu_add_cpu_aliases(&aliases);
+
        if (pmu_type(name, &type))
                return NULL;
 
@@ -602,14 +680,13 @@ static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v,
 
 static __u64 pmu_format_max_value(const unsigned long *format)
 {
-       int w;
+       __u64 w = 0;
+       int fbit;
 
-       w = bitmap_weight(format, PERF_PMU_FORMAT_BITS);
-       if (!w)
-               return 0;
-       if (w < 64)
-               return (1ULL << w) - 1;
-       return -1;
+       for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS)
+               w |= (1ULL << fbit);
+
+       return w;
 }
 
 /*
@@ -644,20 +721,20 @@ static int pmu_resolve_param_term(struct parse_events_term *term,
 static char *pmu_formats_string(struct list_head *formats)
 {
        struct perf_pmu_format *format;
-       char *str;
-       struct strbuf buf;
+       char *str = NULL;
+       struct strbuf buf = STRBUF_INIT;
        unsigned i = 0;
 
        if (!formats)
                return NULL;
 
-       strbuf_init(&buf, 0);
        /* sysfs exported terms */
        list_for_each_entry(format, formats, list)
-               strbuf_addf(&buf, i++ ? ",%s" : "%s",
-                           format->name);
+               if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0)
+                       goto error;
 
        str = strbuf_detach(&buf, NULL);
+error:
        strbuf_release(&buf);
 
        return str;
@@ -975,21 +1052,54 @@ static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu,
        return buf;
 }
 
-static int cmp_string(const void *a, const void *b)
+struct pair {
+       char *name;
+       char *desc;
+};
+
+static int cmp_pair(const void *a, const void *b)
+{
+       const struct pair *as = a;
+       const struct pair *bs = b;
+
+       /* Put extra events last */
+       if (!!as->desc != !!bs->desc)
+               return !!as->desc - !!bs->desc;
+       return strcmp(as->name, bs->name);
+}
+
+static void wordwrap(char *s, int start, int max, int corr)
 {
-       const char * const *as = a;
-       const char * const *bs = b;
-       return strcmp(*as, *bs);
+       int column = start;
+       int n;
+
+       while (*s) {
+               int wlen = strcspn(s, " \t");
+
+               if (column + wlen >= max && column > start) {
+                       printf("\n%*s", start, "");
+                       column = start + corr;
+               }
+               n = printf("%s%.*s", column > start ? " " : "", wlen, s);
+               if (n <= 0)
+                       break;
+               s += wlen;
+               column += n;
+               while (isspace(*s))
+                       s++;
+       }
 }
 
-void print_pmu_events(const char *event_glob, bool name_only)
+void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag)
 {
        struct perf_pmu *pmu;
        struct perf_pmu_alias *alias;
        char buf[1024];
        int printed = 0;
        int len, j;
-       char **aliases;
+       struct pair *aliases;
+       int numdesc = 0;
+       int columns = pager_get_columns();
 
        pmu = NULL;
        len = 0;
@@ -999,14 +1109,15 @@ void print_pmu_events(const char *event_glob, bool name_only)
                if (pmu->selectable)
                        len++;
        }
-       aliases = zalloc(sizeof(char *) * len);
+       aliases = zalloc(sizeof(struct pair) * len);
        if (!aliases)
                goto out_enomem;
        pmu = NULL;
        j = 0;
        while ((pmu = perf_pmu__scan(pmu)) != NULL) {
                list_for_each_entry(alias, &pmu->aliases, list) {
-                       char *name = format_alias(buf, sizeof(buf), pmu, alias);
+                       char *name = alias->desc ? alias->name :
+                               format_alias(buf, sizeof(buf), pmu, alias);
                        bool is_cpu = !strcmp(pmu->name, "cpu");
 
                        if (event_glob != NULL &&
@@ -1015,12 +1126,19 @@ void print_pmu_events(const char *event_glob, bool name_only)
                                                       event_glob))))
                                continue;
 
-                       if (is_cpu && !name_only)
+                       if (is_cpu && !name_only && !alias->desc)
                                name = format_alias_or(buf, sizeof(buf), pmu, alias);
 
-                       aliases[j] = strdup(name);
-                       if (aliases[j] == NULL)
+                       aliases[j].name = name;
+                       if (is_cpu && !name_only && !alias->desc)
+                               aliases[j].name = format_alias_or(buf,
+                                                                 sizeof(buf),
+                                                                 pmu, alias);
+                       aliases[j].name = strdup(aliases[j].name);
+                       if (!aliases[j].name)
                                goto out_enomem;
+
+                       aliases[j].desc = alias->desc;
                        j++;
                }
                if (pmu->selectable &&
@@ -1028,25 +1146,33 @@ void print_pmu_events(const char *event_glob, bool name_only)
                        char *s;
                        if (asprintf(&s, "%s//", pmu->name) < 0)
                                goto out_enomem;
-                       aliases[j] = s;
+                       aliases[j].name = s;
                        j++;
                }
        }
        len = j;
-       qsort(aliases, len, sizeof(char *), cmp_string);
+       qsort(aliases, len, sizeof(struct pair), cmp_pair);
        for (j = 0; j < len; j++) {
                if (name_only) {
-                       printf("%s ", aliases[j]);
+                       printf("%s ", aliases[j].name);
                        continue;
                }
-               printf("  %-50s [Kernel PMU event]\n", aliases[j]);
+               if (aliases[j].desc && !quiet_flag) {
+                       if (numdesc++ == 0)
+                               printf("\n");
+                       printf("  %-50s\n", aliases[j].name);
+                       printf("%*s", 8, "[");
+                       wordwrap(aliases[j].desc, 8, columns, 0);
+                       printf("]\n");
+               } else
+                       printf("  %-50s [Kernel PMU event]\n", aliases[j].name);
                printed++;
        }
        if (printed && pager_in_use())
                printf("\n");
 out_free:
        for (j = 0; j < len; j++)
-               zfree(&aliases[j]);
+               zfree(&aliases[j].name);
        zfree(&aliases);
        return;