Skip to content

Commit 176cda0

Browse files
kjain101mpe
authored andcommitted
powerpc/perf: Add perf interface to expose vpa counters
To support performance measurement for KVM on PowerVM(KoP) feature, PowerVM hypervisor has added couple of new software counters in Virtual Process Area(VPA) of the partition. Commit e1f288d ("KVM: PPC: Book3S HV nestedv2: Add support for reading VPA counters for pseries guests") have updated the paca fields with corresponding changes. Proposed perf interface is to expose these new software counters for monitoring of context switch latencies and runtime aggregate. Perf interface driver is called "vpa_pmu" and it has dependency on KVM and perf, hence added new config called "VPA_PMU" which depends on "CONFIG_KVM_BOOK3S_64_HV" and "CONFIG_HV_PERF_CTRS". Since, kvm and kvm_host are currently compiled as built-in modules, this perf interface takes the same path and registered as a module. vpa_pmu perf interface needs access to some of the kvm functions and structures like kvmhv_get_l2_counters_status(), hence kvm_book3s_64.h and kvm_ppc.h are included. Below are the events added to monitor KoP: vpa_pmu/l1_to_l2_lat/ vpa_pmu/l2_to_l1_lat/ vpa_pmu/l2_runtime_agg/ and vpa_pmu driver supports only per-cpu monitoring with this patch. Example usage: [command]# perf stat -e vpa_pmu/l1_to_l2_lat/ -a -I 1000 1.001017682 727,200 vpa_pmu/l1_to_l2_lat/ 2.003540491 1,118,824 vpa_pmu/l1_to_l2_lat/ 3.005699458 1,919,726 vpa_pmu/l1_to_l2_lat/ 4.007827011 2,364,630 vpa_pmu/l1_to_l2_lat/ Signed-off-by: Kajol Jain <[email protected]> Co-developed-by: Madhavan Srinivasan <[email protected]> Signed-off-by: Madhavan Srinivasan <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent ba6d8ef commit 176cda0

File tree

5 files changed

+232
-0
lines changed

5 files changed

+232
-0
lines changed

arch/powerpc/include/asm/kvm_book3s_64.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ int kvmhv_counters_tracepoint_regfunc(void);
688688
void kvmhv_counters_tracepoint_unregfunc(void);
689689
int kvmhv_get_l2_counters_status(void);
690690
void kvmhv_set_l2_counters_status(int cpu, bool status);
691+
u64 kvmhv_get_l1_to_l2_cs_time(void);
692+
u64 kvmhv_get_l2_to_l1_cs_time(void);
693+
u64 kvmhv_get_l2_runtime_agg(void);
691694

692695
#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
693696

arch/powerpc/kvm/book3s_hv.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4129,6 +4129,7 @@ void kvmhv_set_l2_counters_status(int cpu, bool status)
41294129
else
41304130
lppaca_of(cpu).l2_counters_enable = 0;
41314131
}
4132+
EXPORT_SYMBOL(kvmhv_set_l2_counters_status);
41324133

41334134
int kvmhv_counters_tracepoint_regfunc(void)
41344135
{
@@ -4168,6 +4169,24 @@ static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu)
41684169
*l2_runtime_agg_ptr = l2_runtime_ns;
41694170
}
41704171

4172+
u64 kvmhv_get_l1_to_l2_cs_time(void)
4173+
{
4174+
return tb_to_ns(be64_to_cpu(get_lppaca()->l1_to_l2_cs_tb));
4175+
}
4176+
EXPORT_SYMBOL(kvmhv_get_l1_to_l2_cs_time);
4177+
4178+
u64 kvmhv_get_l2_to_l1_cs_time(void)
4179+
{
4180+
return tb_to_ns(be64_to_cpu(get_lppaca()->l2_to_l1_cs_tb));
4181+
}
4182+
EXPORT_SYMBOL(kvmhv_get_l2_to_l1_cs_time);
4183+
4184+
u64 kvmhv_get_l2_runtime_agg(void)
4185+
{
4186+
return tb_to_ns(be64_to_cpu(get_lppaca()->l2_runtime_tb));
4187+
}
4188+
EXPORT_SYMBOL(kvmhv_get_l2_runtime_agg);
4189+
41714190
#else
41724191
int kvmhv_get_l2_counters_status(void)
41734192
{

arch/powerpc/perf/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ obj-$(CONFIG_FSL_EMB_PERF_EVENT_E500) += e500-pmu.o e6500-pmu.o
1616

1717
obj-$(CONFIG_HV_PERF_CTRS) += hv-24x7.o hv-gpci.o hv-common.o
1818

19+
obj-$(CONFIG_VPA_PMU) += vpa-pmu.o
20+
1921
obj-$(CONFIG_PPC_8xx) += 8xx-pmu.o
2022

2123
obj-$(CONFIG_PPC64) += $(obj64-y)

arch/powerpc/perf/vpa-pmu.c

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Performance monitoring support for Virtual Processor Area(VPA) based counters
4+
*
5+
* Copyright (C) 2024 IBM Corporation
6+
*/
7+
#define pr_fmt(fmt) "vpa_pmu: " fmt
8+
9+
#include <linux/module.h>
10+
#include <linux/perf_event.h>
11+
#include <asm/kvm_ppc.h>
12+
#include <asm/kvm_book3s_64.h>
13+
14+
#define MODULE_VERS "1.0"
15+
#define MODULE_NAME "pseries_vpa_pmu"
16+
17+
#define EVENT(_name, _code) enum{_name = _code}
18+
19+
#define VPA_PMU_EVENT_VAR(_id) event_attr_##_id
20+
#define VPA_PMU_EVENT_PTR(_id) (&event_attr_##_id.attr.attr)
21+
22+
static ssize_t vpa_pmu_events_sysfs_show(struct device *dev,
23+
struct device_attribute *attr, char *page)
24+
{
25+
struct perf_pmu_events_attr *pmu_attr;
26+
27+
pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
28+
29+
return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
30+
}
31+
32+
#define VPA_PMU_EVENT_ATTR(_name, _id) \
33+
PMU_EVENT_ATTR(_name, VPA_PMU_EVENT_VAR(_id), _id, \
34+
vpa_pmu_events_sysfs_show)
35+
36+
EVENT(L1_TO_L2_CS_LAT, 0x1);
37+
EVENT(L2_TO_L1_CS_LAT, 0x2);
38+
EVENT(L2_RUNTIME_AGG, 0x3);
39+
40+
VPA_PMU_EVENT_ATTR(l1_to_l2_lat, L1_TO_L2_CS_LAT);
41+
VPA_PMU_EVENT_ATTR(l2_to_l1_lat, L2_TO_L1_CS_LAT);
42+
VPA_PMU_EVENT_ATTR(l2_runtime_agg, L2_RUNTIME_AGG);
43+
44+
static struct attribute *vpa_pmu_events_attr[] = {
45+
VPA_PMU_EVENT_PTR(L1_TO_L2_CS_LAT),
46+
VPA_PMU_EVENT_PTR(L2_TO_L1_CS_LAT),
47+
VPA_PMU_EVENT_PTR(L2_RUNTIME_AGG),
48+
NULL
49+
};
50+
51+
static const struct attribute_group vpa_pmu_events_group = {
52+
.name = "events",
53+
.attrs = vpa_pmu_events_attr,
54+
};
55+
56+
PMU_FORMAT_ATTR(event, "config:0-31");
57+
static struct attribute *vpa_pmu_format_attr[] = {
58+
&format_attr_event.attr,
59+
NULL,
60+
};
61+
62+
static struct attribute_group vpa_pmu_format_group = {
63+
.name = "format",
64+
.attrs = vpa_pmu_format_attr,
65+
};
66+
67+
static const struct attribute_group *vpa_pmu_attr_groups[] = {
68+
&vpa_pmu_events_group,
69+
&vpa_pmu_format_group,
70+
NULL
71+
};
72+
73+
static int vpa_pmu_event_init(struct perf_event *event)
74+
{
75+
if (event->attr.type != event->pmu->type)
76+
return -ENOENT;
77+
78+
/* it does not support event sampling mode */
79+
if (is_sampling_event(event))
80+
return -EOPNOTSUPP;
81+
82+
/* no branch sampling */
83+
if (has_branch_stack(event))
84+
return -EOPNOTSUPP;
85+
86+
/* Invalid event code */
87+
if ((event->attr.config <= 0) || (event->attr.config > 3))
88+
return -EINVAL;
89+
90+
return 0;
91+
}
92+
93+
static unsigned long get_counter_data(struct perf_event *event)
94+
{
95+
unsigned int config = event->attr.config;
96+
u64 data;
97+
98+
switch (config) {
99+
case L1_TO_L2_CS_LAT:
100+
data = kvmhv_get_l1_to_l2_cs_time();
101+
break;
102+
case L2_TO_L1_CS_LAT:
103+
data = kvmhv_get_l2_to_l1_cs_time();
104+
break;
105+
case L2_RUNTIME_AGG:
106+
data = kvmhv_get_l2_runtime_agg();
107+
break;
108+
default:
109+
data = 0;
110+
break;
111+
}
112+
113+
return data;
114+
}
115+
116+
static int vpa_pmu_add(struct perf_event *event, int flags)
117+
{
118+
u64 data;
119+
120+
kvmhv_set_l2_counters_status(smp_processor_id(), true);
121+
122+
data = get_counter_data(event);
123+
local64_set(&event->hw.prev_count, data);
124+
125+
return 0;
126+
}
127+
128+
static void vpa_pmu_read(struct perf_event *event)
129+
{
130+
u64 prev_data, new_data, final_data;
131+
132+
prev_data = local64_read(&event->hw.prev_count);
133+
new_data = get_counter_data(event);
134+
final_data = new_data - prev_data;
135+
136+
local64_add(final_data, &event->count);
137+
}
138+
139+
static void vpa_pmu_del(struct perf_event *event, int flags)
140+
{
141+
vpa_pmu_read(event);
142+
143+
/*
144+
* Disable vpa counter accumulation
145+
*/
146+
kvmhv_set_l2_counters_status(smp_processor_id(), false);
147+
}
148+
149+
static struct pmu vpa_pmu = {
150+
.task_ctx_nr = perf_sw_context,
151+
.name = "vpa_pmu",
152+
.event_init = vpa_pmu_event_init,
153+
.add = vpa_pmu_add,
154+
.del = vpa_pmu_del,
155+
.read = vpa_pmu_read,
156+
.attr_groups = vpa_pmu_attr_groups,
157+
.capabilities = PERF_PMU_CAP_NO_EXCLUDE | PERF_PMU_CAP_NO_INTERRUPT,
158+
};
159+
160+
static int __init pseries_vpa_pmu_init(void)
161+
{
162+
/*
163+
* List of current Linux on Power platforms and
164+
* this driver is supported only in PowerVM LPAR
165+
* (L1) platform.
166+
*
167+
* Enabled Linux on Power Platforms
168+
* ----------------------------------------
169+
* [X] PowerVM LPAR (L1)
170+
* [ ] KVM Guest On PowerVM KoP(L2)
171+
* [ ] Baremetal(PowerNV)
172+
* [ ] KVM Guest On PowerNV
173+
*/
174+
if (!firmware_has_feature(FW_FEATURE_LPAR) || is_kvm_guest())
175+
return -ENODEV;
176+
177+
perf_pmu_register(&vpa_pmu, vpa_pmu.name, -1);
178+
pr_info("Virtual Processor Area PMU registered.\n");
179+
180+
return 0;
181+
}
182+
183+
static void __exit pseries_vpa_pmu_cleanup(void)
184+
{
185+
perf_pmu_unregister(&vpa_pmu);
186+
pr_info("Virtual Processor Area PMU unregistered.\n");
187+
}
188+
189+
module_init(pseries_vpa_pmu_init);
190+
module_exit(pseries_vpa_pmu_cleanup);
191+
MODULE_DESCRIPTION("Perf Driver for pSeries VPA pmu counter");
192+
MODULE_AUTHOR("Kajol Jain <[email protected]>");
193+
MODULE_AUTHOR("Madhavan Srinivasan <[email protected]>");
194+
MODULE_LICENSE("GPL");

arch/powerpc/platforms/pseries/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ config HV_PERF_CTRS
140140

141141
If unsure, select Y.
142142

143+
config VPA_PMU
144+
tristate "VPA PMU events"
145+
depends on KVM_BOOK3S_64_HV && HV_PERF_CTRS
146+
help
147+
Enable access to the VPA PMU counters via perf. This enables
148+
code that support measurement for KVM on PowerVM(KoP) feature.
149+
PAPR hypervisor has introduced three new counters in the VPA area
150+
of LPAR CPUs for KVM L2 guest observability. Two for context switches
151+
from host to guest and vice versa, and one counter for getting
152+
the total time spent inside the KVM guest. This config enables code
153+
that access these software counters via perf.
154+
155+
If unsure, Select N.
156+
143157
config IBMVIO
144158
depends on PPC_PSERIES
145159
bool

0 commit comments

Comments
 (0)