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