xref: /openbmc/linux/tools/perf/arch/x86/util/mem-events.c (revision be2b81b5)
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 
8 static char mem_loads_name[100];
9 static bool mem_loads_name__init;
10 static char mem_stores_name[100];
11 
12 #define MEM_LOADS_AUX		0x8203
13 #define MEM_LOADS_AUX_NAME     "{%s/mem-loads-aux/,%s/mem-loads,ldlat=%u/}:P"
14 
15 #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s }
16 
17 static struct perf_mem_event perf_mem_events_intel[PERF_MEM_EVENTS__MAX] = {
18 	E("ldlat-loads",	"%s/mem-loads,ldlat=%u/P",	"%s/events/mem-loads"),
19 	E("ldlat-stores",	"%s/mem-stores/P",		"%s/events/mem-stores"),
20 	E(NULL,			NULL,				NULL),
21 };
22 
23 static struct perf_mem_event perf_mem_events_amd[PERF_MEM_EVENTS__MAX] = {
24 	E(NULL,		NULL,		NULL),
25 	E(NULL,		NULL,		NULL),
26 	E("mem-ldst",	"ibs_op//",	"ibs_op"),
27 };
28 
29 static int perf_mem_is_amd_cpu(void)
30 {
31 	struct perf_env env = { .total_mem = 0, };
32 
33 	perf_env__cpuid(&env);
34 	if (env.cpuid && strstarts(env.cpuid, "AuthenticAMD"))
35 		return 1;
36 	return -1;
37 }
38 
39 struct perf_mem_event *perf_mem_events__ptr(int i)
40 {
41 	/* 0: Uninitialized, 1: Yes, -1: No */
42 	static int is_amd;
43 
44 	if (i >= PERF_MEM_EVENTS__MAX)
45 		return NULL;
46 
47 	if (!is_amd)
48 		is_amd = perf_mem_is_amd_cpu();
49 
50 	if (is_amd == 1)
51 		return &perf_mem_events_amd[i];
52 
53 	return &perf_mem_events_intel[i];
54 }
55 
56 bool is_mem_loads_aux_event(struct evsel *leader)
57 {
58 	if (perf_pmu__find("cpu")) {
59 		if (!pmu_have_event("cpu", "mem-loads-aux"))
60 			return false;
61 	} else if (perf_pmu__find("cpu_core")) {
62 		if (!pmu_have_event("cpu_core", "mem-loads-aux"))
63 			return false;
64 	}
65 
66 	return leader->core.attr.config == MEM_LOADS_AUX;
67 }
68 
69 char *perf_mem_events__name(int i, char *pmu_name)
70 {
71 	struct perf_mem_event *e = perf_mem_events__ptr(i);
72 
73 	if (!e)
74 		return NULL;
75 
76 	if (i == PERF_MEM_EVENTS__LOAD) {
77 		if (mem_loads_name__init && !pmu_name)
78 			return mem_loads_name;
79 
80 		if (!pmu_name) {
81 			mem_loads_name__init = true;
82 			pmu_name = (char *)"cpu";
83 		}
84 
85 		if (pmu_have_event(pmu_name, "mem-loads-aux")) {
86 			scnprintf(mem_loads_name, sizeof(mem_loads_name),
87 				  MEM_LOADS_AUX_NAME, pmu_name, pmu_name,
88 				  perf_mem_events__loads_ldlat);
89 		} else {
90 			scnprintf(mem_loads_name, sizeof(mem_loads_name),
91 				  e->name, pmu_name,
92 				  perf_mem_events__loads_ldlat);
93 		}
94 		return mem_loads_name;
95 	}
96 
97 	if (i == PERF_MEM_EVENTS__STORE) {
98 		if (!pmu_name)
99 			pmu_name = (char *)"cpu";
100 
101 		scnprintf(mem_stores_name, sizeof(mem_stores_name),
102 			  e->name, pmu_name);
103 		return mem_stores_name;
104 	}
105 
106 	return (char *)e->name;
107 }
108