Skip to contentSkip to conversation

x86/pmu: Add AMD vPMU testing code

RFC In review Wei Huang 2 participants 9 messages Aug 4, 2017 KVM

lore
Share & tools
lore

Wei Huang

author
#0 lore raw

cover x86/pmu: Add AMD vPMU testing code

This patchset adds the PMU testing case for AMD CPU. The original pmu test code is re-named to intel_pmu. And common PMU event and counter definitions are moved to pmu.h, which is shared between intel_pmu and amd_pmc.

Thanks,
-Wei

Wei Huang (3):
pmu: move common x86 PMU definitions to pmu.h
pmu: Rename pmu.c code to intel_pmu.c
pmu: Create AMD PMU test code

 x86/Makefile.common |   3 +-
 x86/amd_pmu.c       | 265 +++++++++++++++++++++++++++++++++
 x86/intel_pmu.c     | 379 +++++++++++++++++++++++++++++++++++++++++++++++
 x86/pmu.c           | 413 ----------------------------------------------------
 x86/pmu.h           |  42 ++++++
 x86/unittests.cfg   |   8 +-
 6 files changed, 694 insertions(+), 416 deletions(-)
 create mode 100644 x86/amd_pmu.c
 create mode 100644 x86/intel_pmu.c
 delete mode 100644 x86/pmu.c
 create mode 100644 x86/pmu.h
Signature · 1 line
2.7.5
author
#1 lore raw

RFC 1/3 x86/pmu: Move common x86 PMU definitions to pmu.h

This patch moves common x86 PMU counter and event defintions to pmu.h file. This header file will be used by both Intel and AMD PMU test code.

---
 x86/pmu.c | 38 ++------------------------------------
 x86/pmu.h | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 36 deletions(-)
 create mode 100644 x86/pmu.h
x86/pmu.c +2 -36
index a0238dc..1b818c2 100644
@@ -8,38 +8,9 @@
 #include "x86/vm.h"
 
 #include "libcflat.h"
+#include "pmu.h"
 #include <stdint.h>
 
-#define FIXED_CNT_INDEX 32
-#define PC_VECTOR	32
-
-#define EVNSEL_EVENT_SHIFT	0
-#define EVNTSEL_UMASK_SHIFT	8
-#define EVNTSEL_USR_SHIFT	16
-#define EVNTSEL_OS_SHIFT	17
-#define EVNTSEL_EDGE_SHIFT	18
-#define EVNTSEL_PC_SHIFT	19
-#define EVNTSEL_INT_SHIFT	20
-#define EVNTSEL_EN_SHIF		22
-#define EVNTSEL_INV_SHIF	23
-#define EVNTSEL_CMASK_SHIFT	24
-
-#define EVNTSEL_EN	(1 << EVNTSEL_EN_SHIF)
-#define EVNTSEL_USR	(1 << EVNTSEL_USR_SHIFT)
-#define EVNTSEL_OS	(1 << EVNTSEL_OS_SHIFT)
-#define EVNTSEL_PC	(1 << EVNTSEL_PC_SHIFT)
-#define EVNTSEL_INT	(1 << EVNTSEL_INT_SHIFT)
-#define EVNTSEL_INV	(1 << EVNTSEL_INV_SHIF)
-
-#define N 1000000
-
-typedef struct {
-	uint32_t ctr;
-	uint32_t config;
-	uint64_t count;
-	int idx;
-} pmu_counter_t;
-
 union cpuid10_eax {
 	struct {
 		unsigned int version_id:8;
@@ -72,12 +43,7 @@ union cpuid10_edx {
 	unsigned int full;
 } edx;
 
-struct pmu_event {
-	const char *name;
-	uint32_t unit_sel;
-	int min;
-	int max;
-} gp_events[] = {
+struct pmu_event gp_events[] = {
 	{"core cycles", 0x003c, 1*N, 50*N},
 	{"instructions", 0x00c0, 10*N, 10.2*N},
 	{"ref cycles", 0x013c, 0.1*N, 30*N},
x86/pmu.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.7.5
author
#3 lore raw

RFC 2/3 x86/pmu: Rename pmu.c code to intel_pmu.c

In reply to Wei Huang

Many code in pmu.c is Intel-specific (e.g. fixed counters, global ctrl). This patch renames pmu.c to intel_pmu.c. The makefile and unit-test config file are also changed acccordingly.

---
 x86/Makefile.common |   2 +-
 x86/intel_pmu.c     | 379 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/pmu.c           | 379 ----------------------------------------------------
 x86/unittests.cfg   |   4 +-
 4 files changed, 382 insertions(+), 382 deletions(-)
 create mode 100644 x86/intel_pmu.c
 delete mode 100644 x86/pmu.c
x86/Makefile.common +1 -1
index e96812b..d0f4ed1 100644
@@ -45,7 +45,7 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \
                $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \
                $(TEST_DIR)/kvmclock_test.flat  $(TEST_DIR)/eventinj.flat \
-               $(TEST_DIR)/s3.flat $(TEST_DIR)/pmu.flat $(TEST_DIR)/setjmp.flat \
+               $(TEST_DIR)/s3.flat $(TEST_DIR)/intel_pmu.flat $(TEST_DIR)/setjmp.flat \
                $(TEST_DIR)/tsc_adjust.flat $(TEST_DIR)/asyncpf.flat \
                $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \
                $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \
x86/intel_pmu.c new file +379 -0

379 lines not on this page · raw message

x86/pmu.c deleted +0 -379

379 lines not on this page · raw message

x86/unittests.cfg +2 -2
index b4e5172..74156fa 100644
@@ -140,8 +140,8 @@ arch = x86_64
 [msr]
 file = msr.flat
 
-[pmu]
-file = pmu.flat
+[intel_pmu]
+file = intel_pmu.flat
 extra_params = -cpu host
 check = /proc/sys/kernel/nmi_watchdog=0
 
Signature · 1 line
2.7.5
author
#6 lore raw

RFC 3/3 x86/pmu: Create AMD PMU test code

In reply to Wei Huang

AMD PMU implementation is very different from Intel. This patch adds a new test case, called amd_pmu, to x86 architecture. The implementation of amd_pmu.c is based on intel_pmu.c, with focus on AMD's general-purpose registers. To avoid running on Intel CPUs, we check the CPU's vendor name before executing the tests.

---
 x86/Makefile.common |   3 +-
 x86/amd_pmu.c       | 265 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |   4 +
 3 files changed, 271 insertions(+), 1 deletion(-)
 create mode 100644 x86/amd_pmu.c
x86/Makefile.common +2 -1
index d0f4ed1..36bcf90 100644
@@ -45,7 +45,8 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \
                $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \
                $(TEST_DIR)/kvmclock_test.flat  $(TEST_DIR)/eventinj.flat \
-               $(TEST_DIR)/s3.flat $(TEST_DIR)/intel_pmu.flat $(TEST_DIR)/setjmp.flat \
+               $(TEST_DIR)/s3.flat $(TEST_DIR)/intel_pmu.flat \
+	       $(TEST_DIR)/amd_pmu.flat $(TEST_DIR)/setjmp.flat \
                $(TEST_DIR)/tsc_adjust.flat $(TEST_DIR)/asyncpf.flat \
                $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \
                $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \
x86/amd_pmu.c new file +265 -0

265 lines not on this page · raw message

x86/unittests.cfg +4 -0
index 74156fa..98fb3e9 100644
@@ -145,6 +145,10 @@ file = intel_pmu.flat
 extra_params = -cpu host
 check = /proc/sys/kernel/nmi_watchdog=0
 
+[amd_pmu]
+file = amd_pmu.flat
+extra_params = -cpu host
+
 [port80]
 file = port80.flat
 
Signature · 1 line
2.7.5
Reviewed-by on v1 1/3
#2 lore raw

Re: [kvm-unit-tests RFC 1/3] x86/pmu: Move common x86 PMU definitions to pmu.h

In reply to Wei Huang
Quoted 75 lines · Wei Huang

This patch moves common x86 PMU counter and event defintions to pmu.h file. This header file will be used by both Intel and AMD PMU test code.

---
 x86/pmu.c | 38 ++------------------------------------
 x86/pmu.h | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 36 deletions(-)
 create mode 100644 x86/pmu.h
x86/pmu.c +2 -36
index a0238dc..1b818c2 100644
@@ -8,38 +8,9 @@
 #include "x86/vm.h"
 
 #include "libcflat.h"
+#include "pmu.h"
 #include <stdint.h>
 
-#define FIXED_CNT_INDEX 32
-#define PC_VECTOR	32
-
-#define EVNSEL_EVENT_SHIFT	0
-#define EVNTSEL_UMASK_SHIFT	8
-#define EVNTSEL_USR_SHIFT	16
-#define EVNTSEL_OS_SHIFT	17
-#define EVNTSEL_EDGE_SHIFT	18
-#define EVNTSEL_PC_SHIFT	19
-#define EVNTSEL_INT_SHIFT	20
-#define EVNTSEL_EN_SHIF		22
-#define EVNTSEL_INV_SHIF	23
-#define EVNTSEL_CMASK_SHIFT	24
-
-#define EVNTSEL_EN	(1 << EVNTSEL_EN_SHIF)
-#define EVNTSEL_USR	(1 << EVNTSEL_USR_SHIFT)
-#define EVNTSEL_OS	(1 << EVNTSEL_OS_SHIFT)
-#define EVNTSEL_PC	(1 << EVNTSEL_PC_SHIFT)
-#define EVNTSEL_INT	(1 << EVNTSEL_INT_SHIFT)
-#define EVNTSEL_INV	(1 << EVNTSEL_INV_SHIF)
-
-#define N 1000000
-
-typedef struct {
-	uint32_t ctr;
-	uint32_t config;
-	uint64_t count;
-	int idx;
-} pmu_counter_t;
-
 union cpuid10_eax {
 	struct {
 		unsigned int version_id:8;
@@ -72,12 +43,7 @@ union cpuid10_edx {
 	unsigned int full;
 } edx;
 
-struct pmu_event {
-	const char *name;
-	uint32_t unit_sel;
-	int min;
-	int max;
-} gp_events[] = {
+struct pmu_event gp_events[] = {
 	{"core cycles", 0x003c, 1*N, 50*N},
 	{"instructions", 0x00c0, 10*N, 10.2*N},
 	{"ref cycles", 0x013c, 0.1*N, 30*N},
x86/pmu.h new file +0 -0
new file mode 100644
index 0000000..b1e8e51

x86/pmu.h line 3

Quoted 3 lines
x86/pmu.h new file +3 -0
@@ -0,0 +1,42 @@
+/* vPMU unit testing for x86 */
+#ifndef __PMU_H
+#define __PMU_H

New files should get GPL headers.

Quoted 42 lines
x86/pmu.h new file +39 -0
+
+#define N 1000000
+
+#define FIXED_CNT_INDEX 32
+#define PC_VECTOR	32
+
+#define EVNTSEL_EVENT_SHIFT	0
+#define EVNTSEL_UMASK_SHIFT	8
+#define EVNTSEL_USR_SHIFT	16
+#define EVNTSEL_OS_SHIFT	17
+#define EVNTSEL_EDGE_SHIFT	18
+#define EVNTSEL_PC_SHIFT	19
+#define EVNTSEL_INT_SHIFT	20
+#define EVNTSEL_EN_SHIF		22
+#define EVNTSEL_INV_SHIF	23
+#define EVNTSEL_CMASK_SHIFT	24
+
+#define EVNTSEL_EN	(1 << EVNTSEL_EN_SHIF)
+#define EVNTSEL_USR	(1 << EVNTSEL_USR_SHIFT)
+#define EVNTSEL_OS	(1 << EVNTSEL_OS_SHIFT)
+#define EVNTSEL_PC	(1 << EVNTSEL_PC_SHIFT)
+#define EVNTSEL_INT	(1 << EVNTSEL_INT_SHIFT)
+#define EVNTSEL_INV	(1 << EVNTSEL_INV_SHIF)
+
+typedef struct {
+	uint32_t ctr;
+	uint32_t config;
+	uint64_t count;
+	int idx;
+} pmu_counter_t;
+
+struct pmu_event {
+	const char *name;
+	uint32_t unit_sel;
+	int min;
+	int max;
+};
+
+#endif /* __PMU_H */
Signature · 1 line
2.7.5

Otherwise

#4 lore raw

Re: [kvm-unit-tests RFC 2/3] x86/pmu: Rename pmu.c code to intel_pmu.c

In reply to Wei Huang
Quoted 5 lines · Wei Huang

Many code in pmu.c is Intel-specific (e.g. fixed counters, global ctrl). This patch renames pmu.c to intel_pmu.c. The makefile and unit-test config file are also changed acccordingly.

Quoted 8 lines
---
 x86/Makefile.common |   2 +-
 x86/intel_pmu.c     | 379 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/pmu.c           | 379 ----------------------------------------------------
 x86/unittests.cfg   |   4 +-
 4 files changed, 382 insertions(+), 382 deletions(-)
 create mode 100644 x86/intel_pmu.c
 delete mode 100644 x86/pmu.c

It'd be best to create this patch with 'format-patch -M'

Thanks,
drew

#7 lore raw

Re: [kvm-unit-tests RFC 3/3] x86/pmu: Create AMD PMU test code

In reply to Wei Huang

Hi Wei,

I didn't review the important part of this test, i.e. what it's testing and how, so below are just some nits.

Quoted 19 lines · Wei Huang

AMD PMU implementation is very different from Intel. This patch adds a new test case, called amd_pmu, to x86 architecture. The implementation of amd_pmu.c is based on intel_pmu.c, with focus on AMD's general-purpose registers. To avoid running on Intel CPUs, we check the CPU's vendor name before executing the tests.

---
 x86/Makefile.common |   3 +-
 x86/amd_pmu.c       | 265 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |   4 +
 3 files changed, 271 insertions(+), 1 deletion(-)
 create mode 100644 x86/amd_pmu.c
x86/Makefile.common +0 -0
index d0f4ed1..36bcf90 100644

x86/Makefile.common tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \ · line 49

Quoted 6 lines
x86/Makefile.common +2 -1
@@ -45,7 +45,8 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \
                $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \
                $(TEST_DIR)/kvmclock_test.flat  $(TEST_DIR)/eventinj.flat \
-               $(TEST_DIR)/s3.flat $(TEST_DIR)/intel_pmu.flat $(TEST_DIR)/setjmp.flat \
+               $(TEST_DIR)/s3.flat $(TEST_DIR)/intel_pmu.flat \
+	       $(TEST_DIR)/amd_pmu.flat $(TEST_DIR)/setjmp.flat \

mixing tabs and spaces here

Quoted 11 lines
x86/Makefile.common +0 -0
                $(TEST_DIR)/tsc_adjust.flat $(TEST_DIR)/asyncpf.flat \
                $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \
                $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \
x86/amd_pmu.c new file +2 -0
new file mode 100644
index 0000000..006eccf
@@ -0,0 +1,265 @@
+/*
+ * vPMU testing for AMD CPUs

x86/amd_pmu.c line 12

Quoted 10 lines
x86/amd_pmu.c new file +10 -0
+ *
+ * Copyright (c) 2017 Red Hat Inc
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.
+ */
+
+#include "x86/msr.h"
+#include "x86/processor.h"
+#include "x86/apic-defs.h"
+#include "x86/apic.h"

apic-defs is included by apic

x86/amd_pmu.c line 19

Quoted 7 lines
x86/amd_pmu.c new file +7 -0
+#include "x86/desc.h"
+#include "x86/isr.h"
+#include "x86/vm.h"
+
+#include "libcflat.h"
+#include "pmu.h"
+#include <stdint.h>

stdint is included by libcflat

Quoted 11 lines
x86/amd_pmu.c new file +11 -0
+
+#define AMD64_NUM_COUNTERS		4
+
+#define CPUID_VENDOR_AMD_1   0x68747541 /* "Auth" */
+#define CPUID_VENDOR_AMD_2   0x69746e65 /* "enti" */
+#define CPUID_VENDOR_AMD_3   0x444d4163 /* "cAMD" */
+
+struct pmu_event gp_events[] = {
+	{"core cycles", 0x0076, 1*N, 50*N},
+	{"instructions", 0x00c0, 10*N, 10.2*N},
+	{"cache reference", 0x077d, 1, 2*N},

x86/amd_pmu.c line 40

Quoted 10 lines
x86/amd_pmu.c new file +10 -0
+	{"cache misses", 0x077e, 0, 1*N},
+	{"branches", 0x00c2, 1*N, 1.1*N},
+	{"branch misses", 0x00c3, 0, 0.1*N},
+	{"stalls frontend", 0x00d0, 0, 0.1*N},
+	{"stalls backend", 0x00d1, 0, 30*N},
+};
+
+char *buf;
+static int num_counters;
+volatile uint64_t irq_received;

all above globals can be static

Quoted 31 lines
x86/amd_pmu.c new file +31 -0
+
+static bool check_irq(void)
+{
+	int i;
+	irq_received = 0;
+
+	irq_enable();
+	for (i = 0; i < 100000 && !irq_received; i++)
+		asm volatile("pause");
+	irq_disable();
+
+	return irq_received;
+}
+
+static inline void loop()
+{
+	unsigned long tmp, tmp2, tmp3;
+
+	asm volatile("1: mov (%1), %2\n\t"
+		     "   add $64, %1\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   loop 1b"
+		     : "=c"(tmp), "=r"(tmp2), "=r"(tmp3)
+		     : "0"(N), "1"(buf));
+}

x86/amd_pmu.c line 81

Quoted 10 lines
x86/amd_pmu.c new file +10 -0
+
+static struct pmu_event* get_counter_event(pmu_counter_t *cnt)
+{
+	int i;
+
+	for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++)
+		if (gp_events[i].unit_sel == (cnt->config & 0xffff))
+			return &gp_events[i];
+
+	return (void*)0;

NULL

Quoted 139 lines
x86/amd_pmu.c new file +139 -0
+}
+
+static void cnt_overflow(isr_regs_t *regs)
+{
+	irq_received++;
+	apic_write(APIC_EOI, 0);
+}
+
+static int event_to_global_idx(pmu_counter_t *cnt)
+{
+	return cnt->ctr - MSR_K7_PERFCTR0;
+}
+
+static bool verify_event(uint64_t count, struct pmu_event *e)
+{
+	return count >= e->min && count <= e->max;
+}
+
+static bool verify_counter(pmu_counter_t *cnt)
+{
+	return verify_event(cnt->count, get_counter_event(cnt));
+}
+
+static void start_event(pmu_counter_t *evt)
+{
+	wrmsr(evt->ctr, evt->count);
+	wrmsr(MSR_K7_EVNTSEL0 + event_to_global_idx(evt),
+	      evt->config | EVNTSEL_EN);
+}
+
+static void stop_event(pmu_counter_t *evt)
+{
+	wrmsr(MSR_K7_EVNTSEL0 + event_to_global_idx(evt),
+	      evt->config & ~EVNTSEL_EN);
+	evt->count = rdmsr(evt->ctr);
+}
+
+static void measure(pmu_counter_t *evt, int count)
+{
+	int i;
+
+	for (i = 0; i < count; i++)
+		start_event(&evt[i]);
+
+	loop();
+
+	for (i = 0; i < count; i++)
+		stop_event(&evt[i]);
+}
+
+static void check_gp_counter(struct pmu_event *evt)
+{
+	int i;
+
+	pmu_counter_t cnt = {
+		.ctr = MSR_K7_PERFCTR0,
+		.config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel,
+	};
+
+	for (i = 0; i < num_counters; i++, cnt.ctr++) {
+		cnt.count = 0;
+		measure(&cnt, 1);
+		report("%s-%d", verify_event(cnt.count, evt), evt->name, i);
+	}
+}
+
+static void check_gp_counters(void)
+{
+	int i;
+
+	for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++) {
+		check_gp_counter(&gp_events[i]);
+	}
+}
+
+static void check_counter_overflow(void)
+{
+	uint64_t count;
+	int i;
+	pmu_counter_t cnt = {
+		.ctr = MSR_K7_PERFCTR0,
+		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
+		.count = 0,
+	};
+
+	measure(&cnt, 1);
+	count = cnt.count;
+
+	report_prefix_push("overflow");
+
+	for (i = 0; i < num_counters; i++, cnt.ctr++) {
+		if (i % 2)
+			cnt.config |= EVNTSEL_INT;
+		else
+			cnt.config &= ~EVNTSEL_INT;
+		cnt.count = 1 - count;
+		measure(&cnt, 1);
+		report("cntr-%d", cnt.count < 20, i);
+		report("irq-%d", check_irq() == (i % 2), i);
+	}
+
+	report_prefix_pop();
+}
+
+static void check_gp_counter_cmask(void)
+{
+	pmu_counter_t cnt = {
+		.ctr = MSR_K7_PERFCTR0,
+		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
+		.count = 0,
+	};
+
+	cnt.config |= (0x2 << EVNTSEL_CMASK_SHIFT);
+	measure(&cnt, 1);
+	report("cmask", cnt.count < gp_events[1].min);
+}
+
+static void check_rdpmc(void)
+{
+	uint64_t val = 0x1f3456789ull;
+	int i;
+
+	report_prefix_push("rdpmc");
+
+	for (i = 0; i < num_counters; i++) {
+		uint64_t x = val;
+		wrmsr(MSR_K7_PERFCTR0 + i, val);
+		report("cntr-%d", rdpmc(i) == x, i);
+	}
+
+	report_prefix_pop();
+}
+
+static void check_counters_many(void)
+{
+	pmu_counter_t cnt[10];
+	int i, n;
+
+	for (n = 0; n < num_counters; n++) {

x86/amd_pmu.c line 230

Quoted 10 lines
x86/amd_pmu.c new file +10 -0
+		cnt[n].count = 0;
+		cnt[n].ctr = MSR_K7_PERFCTR0 + n;
+		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | gp_events[n].unit_sel;
+	}
+
+	measure(cnt, n);
+
+	for (i = 0; i < n; i++)
+		if (!verify_counter(&cnt[i]))
+			break;

{} for the for loop, might be nice here

x86/amd_pmu.c line 237

Quoted 7 lines
x86/amd_pmu.c new file +7 -0
+
+	report("all counters", i == n);
+}
+
+#define IS_AMD_CPU(cpuid)  ((cpuid).b == CPUID_VENDOR_AMD_1 && \
+			    (cpuid).d == CPUID_VENDOR_AMD_2 && \
+			    (cpuid).c == CPUID_VENDOR_AMD_3)

should add a blank line here

Quoted 33 lines
x86/amd_pmu.c new file +28 -0
+int main(int ac, char **av)
+{
+	struct cpuid id = cpuid(0);
+
+	setup_vm();
+	setup_idt();
+	handle_irq(PC_VECTOR, cnt_overflow);
+	buf = vmalloc(N * 64);
+
+	if (!IS_AMD_CPU(id)) {
+		printf("No AMD PMU detected!\n");
+		return report_summary();
+	}
+
+	num_counters = AMD64_NUM_COUNTERS;
+	if (num_counters > ARRAY_SIZE(gp_events))
+		num_counters = ARRAY_SIZE(gp_events);
+
+	apic_write(APIC_LVTPC, PC_VECTOR);
+
+	check_gp_counters();
+	check_rdpmc();
+	check_counters_many();
+	check_counter_overflow();
+	check_gp_counter_cmask();
+
+	return report_summary();
+}
x86/unittests.cfg +0 -0
index 74156fa..98fb3e9 100644

x86/unittests.cfg file = intel_pmu.flat · line 150

Quoted 6 lines
x86/unittests.cfg +3 -0
@@ -145,6 +145,10 @@ file = intel_pmu.flat
 extra_params = -cpu host
 check = /proc/sys/kernel/nmi_watchdog=0
 
+[amd_pmu]
+file = amd_pmu.flat
+extra_params = -cpu host

maybe add 'arch = x86_64', since AMD only supports 64 bit.

Quoted 7 lines
x86/unittests.cfg +1 -0
+
 [port80]
 file = port80.flat
 
Signature · 1 line
2.7.5

Thanks,
drew

author
#5 lore raw

Re: [kvm-unit-tests RFC 2/3] x86/pmu: Rename pmu.c code to intel_pmu.c

In reply to Andrew Jones
Quoted 15 lines · Andrew Jones
Quoted 5 lines · Wei Huang

Many code in pmu.c is Intel-specific (e.g. fixed counters, global ctrl). This patch renames pmu.c to intel_pmu.c. The makefile and unit-test config file are also changed acccordingly.

Quoted 8 lines
---
 x86/Makefile.common |   2 +-
 x86/intel_pmu.c     | 379 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/pmu.c           | 379 ----------------------------------------------------
 x86/unittests.cfg   |   4 +-
 4 files changed, 382 insertions(+), 382 deletions(-)
 create mode 100644 x86/intel_pmu.c
 delete mode 100644 x86/pmu.c
Quoted 2 lines

It'd be best to create this patch with 'format-patch -M'

Thanks for pointing it out. I will do it in next re-spin.

Quoted 3 lines

Thanks,
drew

author
#8 lore raw

Re: [kvm-unit-tests RFC 3/3] x86/pmu: Create AMD PMU test code

In reply to Andrew Jones
Quoted 4 lines · Andrew Jones

Hi Wei,

I didn't review the important part of this test, i.e. what it's testing and how, so below are just some nits.

Thanks for reviewing it. I agree with most nits below, and will fix them in the next respin.

Quoted 347 lines · Wei Huang
Quoted 19 lines

AMD PMU implementation is very different from Intel. This patch adds a new test case, called amd_pmu, to x86 architecture. The implementation of amd_pmu.c is based on intel_pmu.c, with focus on AMD's general-purpose registers. To avoid running on Intel CPUs, we check the CPU's vendor name before executing the tests.

---
 x86/Makefile.common |   3 +-
 x86/amd_pmu.c       | 265 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 x86/unittests.cfg   |   4 +
 3 files changed, 271 insertions(+), 1 deletion(-)
 create mode 100644 x86/amd_pmu.c
x86/Makefile.common +0 -0
index d0f4ed1..36bcf90 100644

x86/Makefile.common tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \ · line 49

Quoted 6 lines
x86/Makefile.common +2 -1
@@ -45,7 +45,8 @@ tests-common = $(TEST_DIR)/vmexit.flat $(TEST_DIR)/tsc.flat \
                $(TEST_DIR)/realmode.flat $(TEST_DIR)/msr.flat \
                $(TEST_DIR)/hypercall.flat $(TEST_DIR)/sieve.flat \
                $(TEST_DIR)/kvmclock_test.flat  $(TEST_DIR)/eventinj.flat \
-               $(TEST_DIR)/s3.flat $(TEST_DIR)/intel_pmu.flat $(TEST_DIR)/setjmp.flat \
+               $(TEST_DIR)/s3.flat $(TEST_DIR)/intel_pmu.flat \
+	       $(TEST_DIR)/amd_pmu.flat $(TEST_DIR)/setjmp.flat \

mixing tabs and spaces here

Quoted 11 lines
x86/Makefile.common +0 -0
                $(TEST_DIR)/tsc_adjust.flat $(TEST_DIR)/asyncpf.flat \
                $(TEST_DIR)/init.flat $(TEST_DIR)/smap.flat \
                $(TEST_DIR)/hyperv_synic.flat $(TEST_DIR)/hyperv_stimer.flat \
x86/amd_pmu.c new file +2 -0
new file mode 100644
index 0000000..006eccf
@@ -0,0 +1,265 @@
+/*
+ * vPMU testing for AMD CPUs

x86/amd_pmu.c line 12

Quoted 10 lines
x86/amd_pmu.c new file +10 -0
+ *
+ * Copyright (c) 2017 Red Hat Inc
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.
+ */
+
+#include "x86/msr.h"
+#include "x86/processor.h"
+#include "x86/apic-defs.h"
+#include "x86/apic.h"

apic-defs is included by apic

x86/amd_pmu.c line 19

Quoted 7 lines
x86/amd_pmu.c new file +7 -0
+#include "x86/desc.h"
+#include "x86/isr.h"
+#include "x86/vm.h"
+
+#include "libcflat.h"
+#include "pmu.h"
+#include <stdint.h>

stdint is included by libcflat

Quoted 11 lines
x86/amd_pmu.c new file +11 -0
+
+#define AMD64_NUM_COUNTERS		4
+
+#define CPUID_VENDOR_AMD_1   0x68747541 /* "Auth" */
+#define CPUID_VENDOR_AMD_2   0x69746e65 /* "enti" */
+#define CPUID_VENDOR_AMD_3   0x444d4163 /* "cAMD" */
+
+struct pmu_event gp_events[] = {
+	{"core cycles", 0x0076, 1*N, 50*N},
+	{"instructions", 0x00c0, 10*N, 10.2*N},
+	{"cache reference", 0x077d, 1, 2*N},

x86/amd_pmu.c line 40

Quoted 10 lines
x86/amd_pmu.c new file +10 -0
+	{"cache misses", 0x077e, 0, 1*N},
+	{"branches", 0x00c2, 1*N, 1.1*N},
+	{"branch misses", 0x00c3, 0, 0.1*N},
+	{"stalls frontend", 0x00d0, 0, 0.1*N},
+	{"stalls backend", 0x00d1, 0, 30*N},
+};
+
+char *buf;
+static int num_counters;
+volatile uint64_t irq_received;

all above globals can be static

Quoted 31 lines
x86/amd_pmu.c new file +31 -0
+
+static bool check_irq(void)
+{
+	int i;
+	irq_received = 0;
+
+	irq_enable();
+	for (i = 0; i < 100000 && !irq_received; i++)
+		asm volatile("pause");
+	irq_disable();
+
+	return irq_received;
+}
+
+static inline void loop()
+{
+	unsigned long tmp, tmp2, tmp3;
+
+	asm volatile("1: mov (%1), %2\n\t"
+		     "   add $64, %1\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   nop\n\t"
+		     "   loop 1b"
+		     : "=c"(tmp), "=r"(tmp2), "=r"(tmp3)
+		     : "0"(N), "1"(buf));
+}

x86/amd_pmu.c line 81

Quoted 10 lines
x86/amd_pmu.c new file +10 -0
+
+static struct pmu_event* get_counter_event(pmu_counter_t *cnt)
+{
+	int i;
+
+	for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++)
+		if (gp_events[i].unit_sel == (cnt->config & 0xffff))
+			return &gp_events[i];
+
+	return (void*)0;

NULL

Quoted 139 lines
x86/amd_pmu.c new file +139 -0
+}
+
+static void cnt_overflow(isr_regs_t *regs)
+{
+	irq_received++;
+	apic_write(APIC_EOI, 0);
+}
+
+static int event_to_global_idx(pmu_counter_t *cnt)
+{
+	return cnt->ctr - MSR_K7_PERFCTR0;
+}
+
+static bool verify_event(uint64_t count, struct pmu_event *e)
+{
+	return count >= e->min && count <= e->max;
+}
+
+static bool verify_counter(pmu_counter_t *cnt)
+{
+	return verify_event(cnt->count, get_counter_event(cnt));
+}
+
+static void start_event(pmu_counter_t *evt)
+{
+	wrmsr(evt->ctr, evt->count);
+	wrmsr(MSR_K7_EVNTSEL0 + event_to_global_idx(evt),
+	      evt->config | EVNTSEL_EN);
+}
+
+static void stop_event(pmu_counter_t *evt)
+{
+	wrmsr(MSR_K7_EVNTSEL0 + event_to_global_idx(evt),
+	      evt->config & ~EVNTSEL_EN);
+	evt->count = rdmsr(evt->ctr);
+}
+
+static void measure(pmu_counter_t *evt, int count)
+{
+	int i;
+
+	for (i = 0; i < count; i++)
+		start_event(&evt[i]);
+
+	loop();
+
+	for (i = 0; i < count; i++)
+		stop_event(&evt[i]);
+}
+
+static void check_gp_counter(struct pmu_event *evt)
+{
+	int i;
+
+	pmu_counter_t cnt = {
+		.ctr = MSR_K7_PERFCTR0,
+		.config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel,
+	};
+
+	for (i = 0; i < num_counters; i++, cnt.ctr++) {
+		cnt.count = 0;
+		measure(&cnt, 1);
+		report("%s-%d", verify_event(cnt.count, evt), evt->name, i);
+	}
+}
+
+static void check_gp_counters(void)
+{
+	int i;
+
+	for (i = 0; i < sizeof(gp_events)/sizeof(gp_events[0]); i++) {
+		check_gp_counter(&gp_events[i]);
+	}
+}
+
+static void check_counter_overflow(void)
+{
+	uint64_t count;
+	int i;
+	pmu_counter_t cnt = {
+		.ctr = MSR_K7_PERFCTR0,
+		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
+		.count = 0,
+	};
+
+	measure(&cnt, 1);
+	count = cnt.count;
+
+	report_prefix_push("overflow");
+
+	for (i = 0; i < num_counters; i++, cnt.ctr++) {
+		if (i % 2)
+			cnt.config |= EVNTSEL_INT;
+		else
+			cnt.config &= ~EVNTSEL_INT;
+		cnt.count = 1 - count;
+		measure(&cnt, 1);
+		report("cntr-%d", cnt.count < 20, i);
+		report("irq-%d", check_irq() == (i % 2), i);
+	}
+
+	report_prefix_pop();
+}
+
+static void check_gp_counter_cmask(void)
+{
+	pmu_counter_t cnt = {
+		.ctr = MSR_K7_PERFCTR0,
+		.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[1].unit_sel,
+		.count = 0,
+	};
+
+	cnt.config |= (0x2 << EVNTSEL_CMASK_SHIFT);
+	measure(&cnt, 1);
+	report("cmask", cnt.count < gp_events[1].min);
+}
+
+static void check_rdpmc(void)
+{
+	uint64_t val = 0x1f3456789ull;
+	int i;
+
+	report_prefix_push("rdpmc");
+
+	for (i = 0; i < num_counters; i++) {
+		uint64_t x = val;
+		wrmsr(MSR_K7_PERFCTR0 + i, val);
+		report("cntr-%d", rdpmc(i) == x, i);
+	}
+
+	report_prefix_pop();
+}
+
+static void check_counters_many(void)
+{
+	pmu_counter_t cnt[10];
+	int i, n;
+
+	for (n = 0; n < num_counters; n++) {

x86/amd_pmu.c line 230

Quoted 10 lines
x86/amd_pmu.c new file +10 -0
+		cnt[n].count = 0;
+		cnt[n].ctr = MSR_K7_PERFCTR0 + n;
+		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | gp_events[n].unit_sel;
+	}
+
+	measure(cnt, n);
+
+	for (i = 0; i < n; i++)
+		if (!verify_counter(&cnt[i]))
+			break;

{} for the for loop, might be nice here

x86/amd_pmu.c line 237

Quoted 7 lines
x86/amd_pmu.c new file +7 -0
+
+	report("all counters", i == n);
+}
+
+#define IS_AMD_CPU(cpuid)  ((cpuid).b == CPUID_VENDOR_AMD_1 && \
+			    (cpuid).d == CPUID_VENDOR_AMD_2 && \
+			    (cpuid).c == CPUID_VENDOR_AMD_3)

should add a blank line here

Quoted 33 lines
x86/amd_pmu.c new file +28 -0
+int main(int ac, char **av)
+{
+	struct cpuid id = cpuid(0);
+
+	setup_vm();
+	setup_idt();
+	handle_irq(PC_VECTOR, cnt_overflow);
+	buf = vmalloc(N * 64);
+
+	if (!IS_AMD_CPU(id)) {
+		printf("No AMD PMU detected!\n");
+		return report_summary();
+	}
+
+	num_counters = AMD64_NUM_COUNTERS;
+	if (num_counters > ARRAY_SIZE(gp_events))
+		num_counters = ARRAY_SIZE(gp_events);
+
+	apic_write(APIC_LVTPC, PC_VECTOR);
+
+	check_gp_counters();
+	check_rdpmc();
+	check_counters_many();
+	check_counter_overflow();
+	check_gp_counter_cmask();
+
+	return report_summary();
+}
x86/unittests.cfg +0 -0
index 74156fa..98fb3e9 100644

x86/unittests.cfg file = intel_pmu.flat · line 150

Quoted 6 lines
x86/unittests.cfg +3 -0
@@ -145,6 +145,10 @@ file = intel_pmu.flat
 extra_params = -cpu host
 check = /proc/sys/kernel/nmi_watchdog=0
 
+[amd_pmu]
+file = amd_pmu.flat
+extra_params = -cpu host

maybe add 'arch = x86_64', since AMD only supports 64 bit.

Quoted 7 lines
x86/unittests.cfg +1 -0
+
 [port80]
 file = port80.flat
 
Signature · 1 line
2.7.5

Thanks,
drew