perf tool x86: Consolidate is_amd check into single function
[platform/kernel/linux-starfive.git] / tools / perf / arch / x86 / util / mem-events.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "util/pmu.h"
3 #include "util/env.h"
4 #include "map_symbol.h"
5 #include "mem-events.h"
6 #include "linux/string.h"
7 #include "env.h"
8
9 static char mem_loads_name[100];
10 static bool mem_loads_name__init;
11 static char mem_stores_name[100];
12
13 #define MEM_LOADS_AUX           0x8203
14 #define MEM_LOADS_AUX_NAME     "{%s/mem-loads-aux/,%s/mem-loads,ldlat=%u/}:P"
15
16 #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s }
17
18 static struct perf_mem_event perf_mem_events_intel[PERF_MEM_EVENTS__MAX] = {
19         E("ldlat-loads",        "%s/mem-loads,ldlat=%u/P",      "%s/events/mem-loads"),
20         E("ldlat-stores",       "%s/mem-stores/P",              "%s/events/mem-stores"),
21         E(NULL,                 NULL,                           NULL),
22 };
23
24 static struct perf_mem_event perf_mem_events_amd[PERF_MEM_EVENTS__MAX] = {
25         E(NULL,         NULL,           NULL),
26         E(NULL,         NULL,           NULL),
27         E("mem-ldst",   "ibs_op//",     "ibs_op"),
28 };
29
30 struct perf_mem_event *perf_mem_events__ptr(int i)
31 {
32         if (i >= PERF_MEM_EVENTS__MAX)
33                 return NULL;
34
35         if (x86__is_amd_cpu())
36                 return &perf_mem_events_amd[i];
37
38         return &perf_mem_events_intel[i];
39 }
40
41 bool is_mem_loads_aux_event(struct evsel *leader)
42 {
43         if (perf_pmu__find("cpu")) {
44                 if (!pmu_have_event("cpu", "mem-loads-aux"))
45                         return false;
46         } else if (perf_pmu__find("cpu_core")) {
47                 if (!pmu_have_event("cpu_core", "mem-loads-aux"))
48                         return false;
49         }
50
51         return leader->core.attr.config == MEM_LOADS_AUX;
52 }
53
54 char *perf_mem_events__name(int i, char *pmu_name)
55 {
56         struct perf_mem_event *e = perf_mem_events__ptr(i);
57
58         if (!e)
59                 return NULL;
60
61         if (i == PERF_MEM_EVENTS__LOAD) {
62                 if (mem_loads_name__init && !pmu_name)
63                         return mem_loads_name;
64
65                 if (!pmu_name) {
66                         mem_loads_name__init = true;
67                         pmu_name = (char *)"cpu";
68                 }
69
70                 if (pmu_have_event(pmu_name, "mem-loads-aux")) {
71                         scnprintf(mem_loads_name, sizeof(mem_loads_name),
72                                   MEM_LOADS_AUX_NAME, pmu_name, pmu_name,
73                                   perf_mem_events__loads_ldlat);
74                 } else {
75                         scnprintf(mem_loads_name, sizeof(mem_loads_name),
76                                   e->name, pmu_name,
77                                   perf_mem_events__loads_ldlat);
78                 }
79                 return mem_loads_name;
80         }
81
82         if (i == PERF_MEM_EVENTS__STORE) {
83                 if (!pmu_name)
84                         pmu_name = (char *)"cpu";
85
86                 scnprintf(mem_stores_name, sizeof(mem_stores_name),
87                           e->name, pmu_name);
88                 return mem_stores_name;
89         }
90
91         return (char *)e->name;
92 }