perf mem: Introduce weak function perf_mem_events__ptr()
authorLeo Yan <leo.yan@linaro.org>
Fri, 6 Nov 2020 09:48:46 +0000 (17:48 +0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 11 Nov 2020 13:29:12 +0000 (10:29 -0300)
Different architectures might use different event or different event
parameters for memory profiling, this patch introduces a weak
perf_mem_events__ptr() function which allows to return back a
architecture specific memory event.

Since the variable 'perf_mem_events' can be only accessed by the
perf_mem_events__ptr() function, mark the variable as 'static', this
allows the architectures to define its own memory event array.

Signed-off-by: Leo Yan <leo.yan@linaro.org>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Link: https://lore.kernel.org/r/20201106094853.21082-3-leo.yan@linaro.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-c2c.c
tools/perf/builtin-mem.c
tools/perf/util/mem-events.c
tools/perf/util/mem-events.h

index d5bea5d..4d1a08e 100644 (file)
@@ -2867,6 +2867,7 @@ static int perf_c2c__record(int argc, const char **argv)
        int ret;
        bool all_user = false, all_kernel = false;
        bool event_set = false;
+       struct perf_mem_event *e;
        struct option options[] = {
        OPT_CALLBACK('e', "event", &event_set, "event",
                     "event selector. Use 'perf c2c record -e list' to list available events",
@@ -2894,11 +2895,15 @@ static int perf_c2c__record(int argc, const char **argv)
        rec_argv[i++] = "record";
 
        if (!event_set) {
-               perf_mem_events[PERF_MEM_EVENTS__LOAD].record  = true;
-               perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
+               e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
+               e->record = true;
+
+               e = perf_mem_events__ptr(PERF_MEM_EVENTS__STORE);
+               e->record = true;
        }
 
-       if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
+       e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
+       if (e->record)
                rec_argv[i++] = "-W";
 
        rec_argv[i++] = "-d";
@@ -2906,12 +2911,13 @@ static int perf_c2c__record(int argc, const char **argv)
        rec_argv[i++] = "--sample-cpu";
 
        for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
-               if (!perf_mem_events[j].record)
+               e = perf_mem_events__ptr(j);
+               if (!e->record)
                        continue;
 
-               if (!perf_mem_events[j].supported) {
+               if (!e->supported) {
                        pr_err("failed: event '%s' not supported\n",
-                              perf_mem_events[j].name);
+                              perf_mem_events__name(j));
                        free(rec_argv);
                        return -1;
                }
index 3523279..9a7df8d 100644 (file)
@@ -64,6 +64,7 @@ static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
        const char **rec_argv;
        int ret;
        bool all_user = false, all_kernel = false;
+       struct perf_mem_event *e;
        struct option options[] = {
        OPT_CALLBACK('e', "event", &mem, "event",
                     "event selector. use 'perf mem record -e list' to list available events",
@@ -86,13 +87,18 @@ static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
 
        rec_argv[i++] = "record";
 
-       if (mem->operation & MEM_OPERATION_LOAD)
-               perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
+       if (mem->operation & MEM_OPERATION_LOAD) {
+               e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
+               e->record = true;
+       }
 
-       if (mem->operation & MEM_OPERATION_STORE)
-               perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
+       if (mem->operation & MEM_OPERATION_STORE) {
+               e = perf_mem_events__ptr(PERF_MEM_EVENTS__STORE);
+               e->record = true;
+       }
 
-       if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
+       e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
+       if (e->record)
                rec_argv[i++] = "-W";
 
        rec_argv[i++] = "-d";
@@ -101,10 +107,11 @@ static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
                rec_argv[i++] = "--phys-data";
 
        for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
-               if (!perf_mem_events[j].record)
+               e = perf_mem_events__ptr(j);
+               if (!e->record)
                        continue;
 
-               if (!perf_mem_events[j].supported) {
+               if (!e->supported) {
                        pr_err("failed: event '%s' not supported\n",
                               perf_mem_events__name(j));
                        free(rec_argv);
index 35c8d17..7a5a0d6 100644 (file)
@@ -17,7 +17,7 @@ unsigned int perf_mem_events__loads_ldlat = 30;
 
 #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s }
 
-struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = {
+static struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = {
        E("ldlat-loads",        "cpu/mem-loads,ldlat=%u/P",     "cpu/events/mem-loads"),
        E("ldlat-stores",       "cpu/mem-stores/P",             "cpu/events/mem-stores"),
 };
@@ -28,19 +28,31 @@ struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = {
 static char mem_loads_name[100];
 static bool mem_loads_name__init;
 
+struct perf_mem_event * __weak perf_mem_events__ptr(int i)
+{
+       if (i >= PERF_MEM_EVENTS__MAX)
+               return NULL;
+
+       return &perf_mem_events[i];
+}
+
 char * __weak perf_mem_events__name(int i)
 {
+       struct perf_mem_event *e = perf_mem_events__ptr(i);
+
+       if (!e)
+               return NULL;
+
        if (i == PERF_MEM_EVENTS__LOAD) {
                if (!mem_loads_name__init) {
                        mem_loads_name__init = true;
                        scnprintf(mem_loads_name, sizeof(mem_loads_name),
-                                 perf_mem_events[i].name,
-                                 perf_mem_events__loads_ldlat);
+                                 e->name, perf_mem_events__loads_ldlat);
                }
                return mem_loads_name;
        }
 
-       return (char *)perf_mem_events[i].name;
+       return (char *)e->name;
 }
 
 int perf_mem_events__parse(const char *str)
@@ -61,7 +73,7 @@ int perf_mem_events__parse(const char *str)
 
        while (tok) {
                for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
-                       struct perf_mem_event *e = &perf_mem_events[j];
+                       struct perf_mem_event *e = perf_mem_events__ptr(j);
 
                        if (strstr(e->tag, tok))
                                e->record = found = true;
@@ -90,7 +102,7 @@ int perf_mem_events__init(void)
 
        for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
                char path[PATH_MAX];
-               struct perf_mem_event *e = &perf_mem_events[j];
+               struct perf_mem_event *e = perf_mem_events__ptr(j);
                struct stat st;
 
                scnprintf(path, PATH_MAX, "%s/devices/%s",
@@ -108,7 +120,7 @@ void perf_mem_events__list(void)
        int j;
 
        for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
-               struct perf_mem_event *e = &perf_mem_events[j];
+               struct perf_mem_event *e = perf_mem_events__ptr(j);
 
                fprintf(stderr, "%-13s%-*s%s\n",
                        e->tag,
index 904dad3..726a9c8 100644 (file)
@@ -31,13 +31,13 @@ enum {
        PERF_MEM_EVENTS__MAX,
 };
 
-extern struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX];
 extern unsigned int perf_mem_events__loads_ldlat;
 
 int perf_mem_events__parse(const char *str);
 int perf_mem_events__init(void);
 
 char *perf_mem_events__name(int i);
+struct perf_mem_event *perf_mem_events__ptr(int i);
 
 void perf_mem_events__list(void);