Skip to contentSkip to conversation

Calculate memory access latency stats

PATCH Superseded Colton Lewis 5 participants 17 messages Mar 16, 2023 – Mar 28, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Calculate memory access latency stats

Sample the latency of memory accesses in dirty_log_perf_test and report summary stats to give a picture of the latency distribution. Specifically, focus on the right tail with the 50th, 90th, and 99th percentile reported in ns.

v2:
* rebase to v6.3-rc2
* move counter measurement to common code helpers
* fix page allocation math for only the exact number needed
* add command line argument to control number of samples

v1: https://lore.kernel.org/kvm/[email protected]/

Colton Lewis (2):
KVM: selftests: Provide generic way to read system counter
KVM: selftests: Print summary stats of memory latency distribution

 .../selftests/kvm/access_tracking_perf_test.c |  3 +-
 .../selftests/kvm/demand_paging_test.c        |  2 +-
 .../selftests/kvm/dirty_log_perf_test.c       | 10 ++-
 .../testing/selftests/kvm/include/kvm_util.h  | 15 ++++
 .../testing/selftests/kvm/include/memstress.h | 10 ++-
 tools/testing/selftests/kvm/lib/kvm_util.c    | 30 ++++++++
 tools/testing/selftests/kvm/lib/memstress.c   | 68 ++++++++++++++++---
 .../kvm/memslot_modification_stress_test.c    |  2 +-
 .../kvm/system_counter_offset_test.c          | 10 +--
 9 files changed, 128 insertions(+), 22 deletions(-)

--
2.40.0.rc1.284.g88254d51c5-goog

#1 lore raw

PATCH v2 1/2 KVM: selftests: Provide generic way to read system counter

Provide a generic function to read the system counter from the guest for timing purposes. A common and important way to measure guest performance is to measure the amount of time different actions take in the guest. Provide also a mathematical conversion from cycles to nanoseconds and a macro for timing individual statements.

Substitute the previous custom implementation of a similar function in system_counter_offset_test with this new implementation.

---
 .../testing/selftests/kvm/include/kvm_util.h  | 15 ++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 30 +++++++++++++++++++
 .../kvm/system_counter_offset_test.c          | 10 ++-----
 3 files changed, 47 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +15 -0
index c9286811a4cb..8b478eabee4c 100644
@@ -10,4 +10,19 @@
 #include "kvm_util_base.h"
 #include "ucall_common.h"
 
+#if defined(__aarch64__) || defined(__x86_64__)
+
+uint64_t cycles_read(void);
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles);
+
+#define MEASURE_CYCLES(x)			\
+	({					\
+		uint64_t start;			\
+		start = cycles_read();		\
+		x;				\
+		cycles_read() - start;		\
+	})
+
+#endif
+
 #endif /* SELFTEST_KVM_UTIL_H */
tools/testing/selftests/kvm/lib/kvm_util.c +31 -0
index 3ea24a5f4c43..780481a92efe 100644
@@ -2135,3 +2135,34 @@ void __attribute((constructor)) kvm_selftest_init(void)
 
 	kvm_selftest_arch_init();
 }
+
+#if defined(__aarch64__)
+
+#include "arch_timer.h"
+
+uint64_t cycles_read(void)
+{
+	return timer_get_cntct(VIRTUAL);
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
+	return cycles * (1e9 / timer_get_cntfrq());
+}
+
+#elif defined(__x86_64__)
+
+#include "processor.h"
+
+uint64_t cycles_read(void)
+{
+	return rdtsc();
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
+	uint64_t tsc_khz = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL);
+
+	return cycles * (1e9 / (tsc_khz * 1000));
+}
+#endif
tools/testing/selftests/kvm/system_counter_offset_test.c +2 -8
index 7f5b330b6a1b..44101d0fcb48 100644
@@ -39,14 +39,9 @@ static void setup_system_counter(struct kvm_vcpu *vcpu, struct test_case *test)
 			     &test->tsc_offset);
 }
 
-static uint64_t guest_read_system_counter(struct test_case *test)
-{
-	return rdtsc();
-}
-
 static uint64_t host_read_guest_system_counter(struct test_case *test)
 {
-	return rdtsc() + test->tsc_offset;
+	return cycles_read() + test->tsc_offset;
 }
 
 #else /* __x86_64__ */
@@ -63,9 +58,8 @@ static void guest_main(void)
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
-		struct test_case *test = &test_cases[i];
 
-		GUEST_SYNC_CLOCK(i, guest_read_system_counter(test));
+		GUEST_SYNC_CLOCK(i, cycles_read());
 	}
 }
 

--
2.40.0.rc1.284.g88254d51c5-goog

#2 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

Quoted 69 lines · Colton Lewis

Provide a generic function to read the system counter from the guest for timing purposes. A common and important way to measure guest performance is to measure the amount of time different actions take in the guest. Provide also a mathematical conversion from cycles to nanoseconds and a macro for timing individual statements.

Substitute the previous custom implementation of a similar function in system_counter_offset_test with this new implementation.

---
 .../testing/selftests/kvm/include/kvm_util.h  | 15 ++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 30 +++++++++++++++++++
 .../kvm/system_counter_offset_test.c          | 10 ++-----
 3 files changed, 47 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +15 -0
index c9286811a4cb..8b478eabee4c 100644
@@ -10,4 +10,19 @@
 #include "kvm_util_base.h"
 #include "ucall_common.h"
 
+#if defined(__aarch64__) || defined(__x86_64__)
+
+uint64_t cycles_read(void);
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles);
+
+#define MEASURE_CYCLES(x)			\
+	({					\
+		uint64_t start;			\
+		start = cycles_read();		\
+		x;				\
+		cycles_read() - start;		\
+	})
+
+#endif
+
 #endif /* SELFTEST_KVM_UTIL_H */
tools/testing/selftests/kvm/lib/kvm_util.c +21 -0
index 3ea24a5f4c43..780481a92efe 100644
@@ -2135,3 +2135,34 @@ void __attribute((constructor)) kvm_selftest_init(void)
 
 	kvm_selftest_arch_init();
 }
+
+#if defined(__aarch64__)
+
+#include "arch_timer.h"
+
+uint64_t cycles_read(void)
+{
+	return timer_get_cntct(VIRTUAL);
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
+	return cycles * (1e9 / timer_get_cntfrq());
+}
+
+#elif defined(__x86_64__)
+
+#include "processor.h"
+
+uint64_t cycles_read(void)
+{

tools/testing/selftests/kvm/lib/kvm_util.c void __attribute((constructor)) kvm_selftest_init(void) · line 2168

Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10 -0
+	return rdtsc();
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
+	uint64_t tsc_khz = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL);
+
+	return cycles * (1e9 / (tsc_khz * 1000));
+}
+#endif

Instead of the #ifdef's why not must put these functions in lib/ARCH/processor.c?

Thanks,
drew

#3 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

Quoted 25 lines · Andrew Jones
Quoted 25 lines
tools/testing/selftests/kvm/lib/kvm_util.c +25 -0
+#if defined(__aarch64__)
+
+#include "arch_timer.h"
+
+uint64_t cycles_read(void)
+{
+	return timer_get_cntct(VIRTUAL);
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
+	return cycles * (1e9 / timer_get_cntfrq());
+}
+
+#elif defined(__x86_64__)
+
+#include "processor.h"
+
+uint64_t cycles_read(void)
+{
+	return rdtsc();
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
Quoted 8 lines

tools/testing/selftests/kvm/lib/kvm_util.c void __attribute((constructor)) kvm_selftest_init(void) · line 2168

Quoted 5 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5 -0
+	uint64_t tsc_khz = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL);
+
+	return cycles * (1e9 / (tsc_khz * 1000));
+}
+#endif

Instead of the #ifdef's why not must put these functions in lib/ARCH/processor.c?

Seems like a good suggestion.

#4 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

In reply to Colton Lewis
Quoted 8 lines · Colton Lewis

Provide a generic function to read the system counter from the guest for timing purposes. A common and important way to measure guest performance is to measure the amount of time different actions take in the guest. Provide also a mathematical conversion from cycles to nanoseconds and a macro for timing individual statements.

Substitute the previous custom implementation of a similar function in

May be specify specific name: guest_read_system_counter()

Quoted 20 lines

system_counter_offset_test with this new implementation.

---
 .../testing/selftests/kvm/include/kvm_util.h  | 15 ++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 30 +++++++++++++++++++
 .../kvm/system_counter_offset_test.c          | 10 ++-----
 3 files changed, 47 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +3 -0
index c9286811a4cb..8b478eabee4c 100644
@@ -10,4 +10,19 @@
 #include "kvm_util_base.h"
 #include "ucall_common.h"
 
+#if defined(__aarch64__) || defined(__x86_64__)
+
+uint64_t cycles_read(void);

tools/testing/selftests/kvm/include/kvm_util.h line 25

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util.h +10 -0
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles);
+
+#define MEASURE_CYCLES(x)                      \
+       ({                                      \
+               uint64_t start;                 \
+               start = cycles_read();          \
+               x;                              \
+               cycles_read() - start;          \
+       })
+

MEASURE_CYCLES should be moved to the next patch where it is getting used. Does it have to be macro or can it be replaced with a function?

Quoted 32 lines
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
+#endif
+
 #endif /* SELFTEST_KVM_UTIL_H */
tools/testing/selftests/kvm/lib/kvm_util.c +21 -0
index 3ea24a5f4c43..780481a92efe 100644
@@ -2135,3 +2135,34 @@ void __attribute((constructor)) kvm_selftest_init(void)
 
        kvm_selftest_arch_init();
 }
+
+#if defined(__aarch64__)
+
+#include "arch_timer.h"
+
+uint64_t cycles_read(void)
+{
+       return timer_get_cntct(VIRTUAL);
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
+       return cycles * (1e9 / timer_get_cntfrq());
+}
+
+#elif defined(__x86_64__)
+
+#include "processor.h"
+
+uint64_t cycles_read(void)
+{

tools/testing/selftests/kvm/lib/kvm_util.c void __attribute((constructor)) kvm_selftest_init(void) · line 2168

Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10 -0
+       return rdtsc();
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
+       uint64_t tsc_khz = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL);
+
+       return cycles * (1e9 / (tsc_khz * 1000));
+}
+#endif

As Andrew noted, these should be in the respective processor files.

#5 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

Quoted 11 lines · Vipin Sharma
Quoted 7 lines · Colton Lewis

Provide a generic function to read the system counter from the guest for timing purposes. A common and important way to measure guest performance is to measure the amount of time different actions take in the guest. Provide also a mathematical conversion from cycles to nanoseconds and a macro for timing individual statements.

Substitute the previous custom implementation of a similar function in

May be specify specific name: guest_read_system_counter()

Will do.

Quoted 11 lines

tools/testing/selftests/kvm/include/kvm_util.h line 25

Quoted 8 lines
tools/testing/selftests/kvm/include/kvm_util.h +8 -0
+#define MEASURE_CYCLES(x)                      \
+       ({                                      \
+               uint64_t start;                 \
+               start = cycles_read();          \
+               x;                              \
+               cycles_read() - start;          \
+       })
+

MEASURE_CYCLES should be moved to the next patch where it is getting used. Does it have to be macro or can it be replaced with a function?

Will move to the next patch. It can't be replaced by a function because then 'x' would be executed before the function begins and there would be nothing left to measure.

#6 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

In reply to Colton Lewis
Quoted 25 lines · Colton Lewis

Provide a generic function to read the system counter from the guest for timing purposes. A common and important way to measure guest performance is to measure the amount of time different actions take in the guest. Provide also a mathematical conversion from cycles to nanoseconds and a macro for timing individual statements.

Substitute the previous custom implementation of a similar function in system_counter_offset_test with this new implementation.

---
 .../testing/selftests/kvm/include/kvm_util.h  | 15 ++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 30 +++++++++++++++++++
 .../kvm/system_counter_offset_test.c          | 10 ++-----
 3 files changed, 47 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +0 -0
index c9286811a4cb..8b478eabee4c 100644
@@ -10,4 +10,19 @@
 #include "kvm_util_base.h"
 #include "ucall_common.h"
 

tools/testing/selftests/kvm/include/kvm_util.h line 22

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util.h +10 -0
+#if defined(__aarch64__) || defined(__x86_64__)
+
+uint64_t cycles_read(void);
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles);
+
+#define MEASURE_CYCLES(x)			\
+	({					\
+		uint64_t start;			\
+		start = cycles_read();		\
+		x;				\

You insert memory accesses inside a sequence that has no dependency with it. On a weakly ordered memory system, there is absolutely no reason why the memory access shouldn't be moved around. What do you exactly measure in that case?

tools/testing/selftests/kvm/include/kvm_util.h line 23

Quoted 1 line
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
+		cycles_read() - start;		\

I also question the usefulness of this exercise. You're comparing the time it takes for a multi-GHz system to put a write in a store buffer (assuming it didn't miss in the TLBs) vs a counter that gets updated at a frequency of a few tens of MHz.

My guts feeling is that this results in a big fat zero most of the time, but I'm happy to be explained otherwise.

Quoted 16 lines
tools/testing/selftests/kvm/include/kvm_util.h +4 -0
+	})
+
+#endif
+
 #endif /* SELFTEST_KVM_UTIL_H */
tools/testing/selftests/kvm/lib/kvm_util.c +3 -0
index 3ea24a5f4c43..780481a92efe 100644
@@ -2135,3 +2135,34 @@ void __attribute((constructor)) kvm_selftest_init(void)
 
 	kvm_selftest_arch_init();
 }
+
+#if defined(__aarch64__)
+

tools/testing/selftests/kvm/lib/kvm_util.c void __attribute((constructor)) kvm_selftest_init(void) · line 2150

Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10 -0
+#include "arch_timer.h"
+
+uint64_t cycles_read(void)
+{
+	return timer_get_cntct(VIRTUAL);
+}
+
+double cycles_to_ns(struct kvm_vcpu *vcpu, double cycles)
+{
+	return cycles * (1e9 / timer_get_cntfrq());

We already have all the required code to deal with ns conversions using a multiplier and a shift, avoiding floating point like the plague it is. Please reuse the kernel code for this, as you're quite likely to only measure the time it takes for KVM to trap the FP registers and perform a FP/SIMD switch...

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#7 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

Quoted 10 lines · Marc Zyngier

tools/testing/selftests/kvm/include/kvm_util.h line 22

Quoted 5 lines
tools/testing/selftests/kvm/include/kvm_util.h +5 -0
+#define MEASURE_CYCLES(x)			\
+	({					\
+		uint64_t start;			\
+		start = cycles_read();		\
+		x;				\

You insert memory accesses inside a sequence that has no dependency with it. On a weakly ordered memory system, there is absolutely no reason why the memory access shouldn't be moved around. What do you exactly measure in that case?

cycles_read is built on another function timer_get_cntct which includes its own barriers. Stripped of some abstraction, the sequence is:

timer_get_cntct (isb+read timer)
whatever is being measured
timer_get_cntct

I hadn't looked at it too closely before but on review of the manual I think you are correct. Borrowing from example D7-2 in the manual, it should be:

timer_get_cntct
isb
whatever is being measured
dsb
timer_get_cntct

Quoted 9 lines

tools/testing/selftests/kvm/include/kvm_util.h line 23

Quoted 1 line
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
+		cycles_read() - start;		\

I also question the usefulness of this exercise. You're comparing the time it takes for a multi-GHz system to put a write in a store buffer (assuming it didn't miss in the TLBs) vs a counter that gets updated at a frequency of a few tens of MHz.

My guts feeling is that this results in a big fat zero most of the time, but I'm happy to be explained otherwise.

In context, I'm trying to measure the time it takes to write to a buffer *with dirty memory logging enabled*. What do you mean by zero? I can confirm from running this code I am not measuring zero time.

Quoted 5 lines

We already have all the required code to deal with ns conversions using a multiplier and a shift, avoiding floating point like the plague it is. Please reuse the kernel code for this, as you're quite likely to only measure the time it takes for KVM to trap the FP registers and perform a FP/SIMD switch...

Will do.

#8 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

Quoted 25 lines · Colton Lewis
Quoted 10 lines · Marc Zyngier

tools/testing/selftests/kvm/include/kvm_util.h line 22

Quoted 5 lines
tools/testing/selftests/kvm/include/kvm_util.h +5 -0
+#define MEASURE_CYCLES(x)			\
+	({					\
+		uint64_t start;			\
+		start = cycles_read();		\
+		x;				\

You insert memory accesses inside a sequence that has no dependency with it. On a weakly ordered memory system, there is absolutely no reason why the memory access shouldn't be moved around. What do you exactly measure in that case?

cycles_read is built on another function timer_get_cntct which includes its own barriers. Stripped of some abstraction, the sequence is:

timer_get_cntct (isb+read timer)
whatever is being measured
timer_get_cntct

I hadn't looked at it too closely before but on review of the manual I think you are correct. Borrowing from example D7-2 in the manual, it should be:

Quoted 5 lines

timer_get_cntct
isb
whatever is being measured
dsb
timer_get_cntct

That's better, but also very heavy handed. You'd be better off constructing an address dependency from the timer value, and feed that into a load-acquire/store-release pair wrapping your payload.

Quoted 8 lines
Quoted 7 lines

tools/testing/selftests/kvm/include/kvm_util.h line 23

Quoted 2 lines
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
+		cycles_read() - start;		\

I also question the usefulness of this exercise. You're comparing the time it takes for a multi-GHz system to put a write in a store buffer (assuming it didn't miss in the TLBs) vs a counter that gets updated at a frequency of a few tens of MHz.

Quoted 7 lines
Quoted 2 lines

My guts feeling is that this results in a big fat zero most of the time, but I'm happy to be explained otherwise.

In context, I'm trying to measure the time it takes to write to a buffer *with dirty memory logging enabled*. What do you mean by zero? I can confirm from running this code I am not measuring zero time.

See my earlier point: the counter tick is a few MHz, and the CPU multiple GHz. So unless "whatever" is something that takes a significant time (several thousands of CPU cycles), you'll measure nothing using the counter. Page faults will probably show, but not a normal access.

The right tool for this job is to use PMU events, as they count at the CPU frequency.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#9 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

Quoted 8 lines · Marc Zyngier
Quoted 3 lines · Colton Lewis

In context, I'm trying to measure the time it takes to write to a buffer *with dirty memory logging enabled*. What do you mean by zero? I can confirm from running this code I am not measuring zero time.

See my earlier point: the counter tick is a few MHz, and the CPU multiple GHz.

On x86, the system counter (TSC) counts at multiple GHz, so we should be able to continue with that approach for x86.

Quoted 6 lines

So unless "whatever" is something that takes a significant time (several thousands of CPU cycles), you'll measure nothing using the counter. Page faults will probably show, but not a normal access.

The right tool for this job is to use PMU events, as they count at the CPU frequency.

Out of curiosity, what does the kernel end up using for things like ndelay()? I tried to follow the breadcrumbs for ARM and got as far as arm_arch_timer.c, but after that I'm more than a bit lost.

#10 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

Quoted 18 lines · Sean Christopherson
Quoted 8 lines · Marc Zyngier
Quoted 3 lines · Colton Lewis

In context, I'm trying to measure the time it takes to write to a buffer *with dirty memory logging enabled*. What do you mean by zero? I can confirm from running this code I am not measuring zero time.

See my earlier point: the counter tick is a few MHz, and the CPU multiple GHz.

On x86, the system counter (TSC) counts at multiple GHz, so we should be able to continue with that approach for x86.

Quoted 4 lines

So unless "whatever" is something that takes a significant time (several thousands of CPU cycles), you'll measure nothing using the counter. Page faults will probably show, but not a normal access.

Quoted 6 lines
Quoted 2 lines

The right tool for this job is to use PMU events, as they count at the CPU frequency.

Out of curiosity, what does the kernel end up using for things like ndelay()? I tried to follow the breadcrumbs for ARM and got as far as arm_arch_timer.c, but after that I'm more than a bit lost.

That's where it ends. We use the counter for everything. Even on ARMv8.6+ HW that is supposed to give you a 1GHz counter, implementations are allowed to perform "lumpy" increments (50MHz counter with increments of 20 per tick, for example).

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#11 lore raw

Re: [PATCH v2 1/2] KVM: selftests: Provide generic way to read system counter

In reply to Marc Zyngier
Quoted 33 lines · Marc Zyngier
Quoted 24 lines · Colton Lewis
Quoted 10 lines · Marc Zyngier

tools/testing/selftests/kvm/include/kvm_util.h line 22

Quoted 5 lines
tools/testing/selftests/kvm/include/kvm_util.h +5 -0
+#define MEASURE_CYCLES(x)			\
+	({					\
+		uint64_t start;			\
+		start = cycles_read();		\
+		x;				\

You insert memory accesses inside a sequence that has no dependency with it. On a weakly ordered memory system, there is absolutely no reason why the memory access shouldn't be moved around. What do you exactly measure in that case?

cycles_read is built on another function timer_get_cntct which includes its own barriers. Stripped of some abstraction, the sequence is:

timer_get_cntct (isb+read timer)
whatever is being measured
timer_get_cntct

I hadn't looked at it too closely before but on review of the manual I think you are correct. Borrowing from example D7-2 in the manual, it should be:

Quoted 5 lines

timer_get_cntct
isb
whatever is being measured
dsb
timer_get_cntct

Quoted 3 lines

That's better, but also very heavy handed. You'd be better off constructing an address dependency from the timer value, and feed that into a load-acquire/store-release pair wrapping your payload.

I can do something like that.

Quoted 15 lines
Quoted 7 lines
Quoted 6 lines

tools/testing/selftests/kvm/include/kvm_util.h line 23

Quoted 1 line
tools/testing/selftests/kvm/include/kvm_util.h +1 -0
+		cycles_read() - start;		\

I also question the usefulness of this exercise. You're comparing the time it takes for a multi-GHz system to put a write in a store buffer (assuming it didn't miss in the TLBs) vs a counter that gets updated at a frequency of a few tens of MHz.

Quoted 7 lines
Quoted 2 lines

My guts feeling is that this results in a big fat zero most of the time, but I'm happy to be explained otherwise.

In context, I'm trying to measure the time it takes to write to a buffer *with dirty memory logging enabled*. What do you mean by zero? I can confirm from running this code I am not measuring zero time.

Quoted 8 lines

See my earlier point: the counter tick is a few MHz, and the CPU multiple GHz. So unless "whatever" is something that takes a significant time (several thousands of CPU cycles), you'll measure nothing using the counter. Page faults will probably show, but not a normal access.

The right tool for this job is to use PMU events, as they count at the CPU frequency.

Thanks. I understand you clearly now. I think it works out to tens of cpu cycles, not thousands to observe a timer tick in the usual case (2 GHz / 25 MHz = 80, of course slower timers exist), but I agree with you a more precise tool is called for.

#12 lore raw

PATCH v2 2/2 KVM: selftests: Print summary stats of memory latency distribution

In reply to Colton Lewis

Print summary stats of the memory latency distribution in nanoseconds for dirty_log_perf_test. For every iteration, this prints the minimum, the maximum, and the 50th, 90th, and 99th percentiles.

Stats are calculated by sorting the samples taken from all vcpus and picking from the index corresponding with each percentile.

Because keeping all samples is impractical due to the space required in some cases (pooled memory w/ 64 vcpus would be 64 GB/vcpu * 64 vcpus * 250,000 samples/GB * 8 bytes/sample ~ 8 GB extra memory just for samples), reservoir sampling [1] is used to only keep a small number of samples per vcpu (settable via command argument). There is a tradeoff between stat accuracy and memory usage. More samples means more accuracy but also more memory use.

Because other selftests use memstress.c, those tests were hardcoded to take 0 samples at this time.

[1] https://en.wikipedia.org/wiki/Reservoir_sampling#Simple:_Algorithm_R

---
 .../selftests/kvm/access_tracking_perf_test.c |  3 +-
 .../selftests/kvm/demand_paging_test.c        |  2 +-
 .../selftests/kvm/dirty_log_perf_test.c       | 10 ++-
 .../testing/selftests/kvm/include/memstress.h | 10 ++-
 tools/testing/selftests/kvm/lib/memstress.c   | 68 ++++++++++++++++---
 .../kvm/memslot_modification_stress_test.c    |  2 +-
 6 files changed, 81 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/access_tracking_perf_test.c +2 -1
index 3c7defd34f56..89947aa4ff3a 100644
@@ -306,7 +306,8 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	struct kvm_vm *vm;
 	int nr_vcpus = params->nr_vcpus;
 
-	vm = memstress_create_vm(mode, nr_vcpus, params->vcpu_memory_bytes, 1,
+	vm = memstress_create_vm(mode, nr_vcpus, params->vcpu_memory_bytes,
+				 0, 1,
 				 params->backing_src, !overlap_memory_access);
 
 	memstress_start_vcpu_threads(nr_vcpus, vcpu_thread_main);
tools/testing/selftests/kvm/demand_paging_test.c +1 -1
index b0e1fc4de9e2..bc5c171eca9e 100644
@@ -135,7 +135,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	struct kvm_vm *vm;
 	int i;
 
-	vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
+	vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 0, 1,
 				 p->src_type, p->partition_vcpu_memory_access);
 
 	demand_paging_size = get_backing_src_pagesz(p->src_type);
tools/testing/selftests/kvm/dirty_log_perf_test.c +9 -1
index e9d6d1aecf89..40058fe2b58f 100644
@@ -133,6 +133,7 @@ struct test_params {
 	int slots;
 	uint32_t write_percent;
 	uint32_t random_seed;
+	uint64_t samples_per_vcpu;
 	bool random_access;
 };
 
@@ -224,6 +225,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	int i;
 
 	vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size,
+				 p->samples_per_vcpu,
 				 p->slots, p->backing_src,
 				 p->partition_vcpu_memory_access);
 
@@ -274,6 +276,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	ts_diff = timespec_elapsed(start);
 	pr_info("Populate memory time: %ld.%.9lds\n",
 		ts_diff.tv_sec, ts_diff.tv_nsec);
+	memstress_print_percentiles(vm, nr_vcpus);
 
 	/* Enable dirty logging */
 	clock_gettime(CLOCK_MONOTONIC, &start);
@@ -304,6 +307,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 		vcpu_dirty_total = timespec_add(vcpu_dirty_total, ts_diff);
 		pr_info("Iteration %d dirty memory time: %ld.%.9lds\n",
 			iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
+		memstress_print_percentiles(vm, nr_vcpus);
 
 		clock_gettime(CLOCK_MONOTONIC, &start);
 		get_dirty_log(vm, bitmaps, p->slots);
@@ -428,6 +432,7 @@ int main(int argc, char *argv[])
 		.slots = 1,
 		.random_seed = 1,
 		.write_percent = 100,
+		.samples_per_vcpu = 1000,
 	};
 	int opt;
 
@@ -438,7 +443,7 @@ int main(int argc, char *argv[])
 
 	guest_modes_append_default();
 
-	while ((opt = getopt(argc, argv, "ab:c:eghi:m:nop:r:s:v:x:w:")) != -1) {
+	while ((opt = getopt(argc, argv, "ab:c:eghi:m:nop:r:s:S:v:x:w:")) != -1) {
 		switch (opt) {
 		case 'a':
 			p.random_access = true;
@@ -480,6 +485,9 @@ int main(int argc, char *argv[])
 		case 's':
 			p.backing_src = parse_backing_src_type(optarg);
 			break;
+		case 'S':
+			p.samples_per_vcpu = atoi_positive("Number of samples/vcpu", optarg);
+			break;
 		case 'v':
 			nr_vcpus = atoi_positive("Number of vCPUs", optarg);
 			TEST_ASSERT(nr_vcpus <= max_vcpus,
tools/testing/selftests/kvm/include/memstress.h +9 -1
index 72e3e358ef7b..25a3b5e308e9 100644
@@ -37,6 +37,12 @@ struct memstress_args {
 	uint64_t guest_page_size;
 	uint32_t random_seed;
 	uint32_t write_percent;
+	uint64_t samples_per_vcpu;
+	/* Store all samples in a flat array so they can be easily
+	 * sorted later.
+	 */
+	vm_vaddr_t latency_samples;
+
 
 	/* Run vCPUs in L2 instead of L1, if the architecture supports it. */
 	bool nested;
@@ -56,7 +62,8 @@ struct memstress_args {
 extern struct memstress_args memstress_args;
 
 struct kvm_vm *memstress_create_vm(enum vm_guest_mode mode, int nr_vcpus,
-				   uint64_t vcpu_memory_bytes, int slots,
+				   uint64_t vcpu_memory_bytes,
+				   uint64_t samples_per_vcpu, int slots,
 				   enum vm_mem_backing_src_type backing_src,
 				   bool partition_vcpu_memory_access);
 void memstress_destroy_vm(struct kvm_vm *vm);
@@ -72,4 +79,5 @@ void memstress_guest_code(uint32_t vcpu_id);
 uint64_t memstress_nested_pages(int nr_vcpus);
 void memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vcpus[]);
 
+void memstress_print_percentiles(struct kvm_vm *vm, int nr_vcpus);
 #endif /* SELFTEST_KVM_MEMSTRESS_H */
tools/testing/selftests/kvm/lib/memstress.c +59 -9

134 lines not on this page · raw message

tools/testing/selftests/kvm/memslot_modification_stress_test.c +1 -1
index 9855c41ca811..0a14f7d16e0c 100644
@@ -95,7 +95,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 	struct test_params *p = arg;
 	struct kvm_vm *vm;
 
-	vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 1,
+	vm = memstress_create_vm(mode, nr_vcpus, guest_percpu_mem_size, 0, 1,
 				 VM_MEM_SRC_ANONYMOUS,
 				 p->partition_vcpu_memory_access);
 

--
2.40.0.rc1.284.g88254d51c5-goog

#13 lore raw

Re: [PATCH v2 2/2] KVM: selftests: Print summary stats of memory latency distribution

Quoted 5 lines · Colton Lewis

Print summary stats of the memory latency distribution in nanoseconds for dirty_log_perf_test. For every iteration, this prints the minimum, the maximum, and the 50th, 90th, and 99th percentiles.

Can you also write how this is useful and why these 5 specific percentiles are valuable for testers? Generally, 5 number summaries are 0, 25, 50, 75, 100 %ile.
It might also be too much data to display with each iteration.

Nit: minimum, 50th, 90th, 99th and maximum since this is the order you are printing.

tools/testing/selftests/kvm/dirty_log_perf_test.c int main(int argc, char *argv[]) · line 435

Quoted 5 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +1 -0
@@ -428,6 +432,7 @@ int main(int argc, char *argv[])
                .slots = 1,
                .random_seed = 1,
                .write_percent = 100,
+               .samples_per_vcpu = 1000,

Why is 1000 the right default choice? Maybe the default should be 0 and if anyone wants to use it then they can use the command line option to set it?

tools/testing/selftests/kvm/dirty_log_perf_test.c int main(int argc, char *argv[]) · line 446

Quoted 6 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +1 -1
@@ -438,7 +443,7 @@ int main(int argc, char *argv[])
 
        guest_modes_append_default();
 
-       while ((opt = getopt(argc, argv, "ab:c:eghi:m:nop:r:s:v:x:w:")) != -1) {
+       while ((opt = getopt(argc, argv, "ab:c:eghi:m:nop:r:s:S:v:x:w:")) != -1) {

1. Please add the help section to tell about the new command line option.
2. Instead of having s and S, it may be better to use a different lower case letter, like "l" for latency. Giving this option will print memory latency and users need to provide the number of samples they prefer per vCPU.

tools/testing/selftests/kvm/dirty_log_perf_test.c int main(int argc, char *argv[]) · line 490

Quoted 7 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +3 -0
@@ -480,6 +485,9 @@ int main(int argc, char *argv[])
                case 's':
                        p.backing_src = parse_backing_src_type(optarg);
                        break;
+               case 'S':
+                       p.samples_per_vcpu = atoi_positive("Number of samples/vcpu", optarg);
+                       break;

This will force users to always see latency stat when they do not want to see it. I think this patch should be modified in a way to easily disable this feature.
I might be wrong here and it is actually a useful metric to see with each run. If this is true then maybe the commit should mention why it is good for each run.

tools/testing/selftests/kvm/lib/memstress.c void memstress_guest_code(uint32_t vcpu_idx) · line 112

Quoted 9 lines
tools/testing/selftests/kvm/lib/memstress.c +9 -0
+void memstress_print_percentiles(struct kvm_vm *vm, int nr_vcpus)
+{
+       uint64_t n_samples = nr_vcpus * memstress_args.samples_per_vcpu;
+       uint64_t *host_latency_samples = addr_gva2hva(vm, memstress_args.latency_samples);
+
+       qsort(host_latency_samples, n_samples, sizeof(uint64_t), &memstress_qcmp);
+
+       pr_info("Latency distribution (ns) = min:%6.0lf, 50th:%6.0lf, 90th:%6.0lf, 99th:%6.0lf, max:%6.0lf\n",
+               cycles_to_ns(vcpus[0], (double)host_latency_samples[0]),

I am not much aware of how tsc is set up and used. Will all vCPUs have the same tsc value? Can this change if vCPU gets scheduled to different pCPU on the host?

tools/testing/selftests/kvm/lib/memstress.c void memstress_guest_code(uint32_t vcpu_idx) · line 117

Quoted 5 lines
tools/testing/selftests/kvm/lib/memstress.c +5 -0
+               cycles_to_ns(vcpus[0], (double)host_latency_samples[n_samples / 2]),
+               cycles_to_ns(vcpus[0], (double)host_latency_samples[n_samples * 9 / 10]),
+               cycles_to_ns(vcpus[0], (double)host_latency_samples[n_samples * 99 / 100]),
+               cycles_to_ns(vcpus[0], (double)host_latency_samples[n_samples - 1]));
+}

All of the dirty_log_perf_tests print data in seconds followed by nanoseconds. I will recommend keeping it the same. Also, 9 digits for nanoseconds instead of 6 as in other formats.

#14 lore raw

Re: [PATCH v2 2/2] KVM: selftests: Print summary stats of memory latency distribution

Quoted 11 lines · Vipin Sharma
Quoted 3 lines · Colton Lewis

Print summary stats of the memory latency distribution in nanoseconds for dirty_log_perf_test. For every iteration, this prints the minimum, the maximum, and the 50th, 90th, and 99th percentiles.

Can you also write how this is useful and why these 5 specific percentiles are valuable for testers? Generally, 5 number summaries are 0, 25, 50, 75, 100 %ile.
It might also be too much data to display with each iteration.

Generally, when measuring latency you care more about the right half of the distribution. The left half of the distribution is assumed to be bunched close together at some low value, and I do see in my measurements there is not much difference between the minimum and the 50th percentile. The right half of the distribution is where things might be going wrong. 50th, 90th, or 99th are common measures to take in other contexts, specifically networking.

One example:
https://cloud.google.com/spanner/docs/latency-metrics#overview

Quoted 2 lines

Nit: minimum, 50th, 90th, 99th and maximum since this is the order you are printing.

Ok.

Quoted 9 lines

tools/testing/selftests/kvm/dirty_log_perf_test.c int main(int argc, char *argv[]) · line 435

Quoted 5 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +1 -0
@@ -428,6 +432,7 @@ int main(int argc, char *argv[])
                 .slots = 1,
                 .random_seed = 1,
                 .write_percent = 100,
+               .samples_per_vcpu = 1000,

Why is 1000 the right default choice? Maybe the default should be 0 and if anyone wants to use it then they can use the command line option to set it?

Good point.

Quoted 10 lines

Review int main(int argc, char *argv[]) · line 448

Quoted 8 lines
(quoted diff) +1 -1
@@ -438,7 +443,7 @@ int main(int argc, char *argv[])
 
         guest_modes_append_default();
 
-       while ((opt = getopt(argc,  
 argv, "ab:c:eghi:m:nop:r:s:v:x:w:")) != -1) {
+       while ((opt = getopt(argc,  
 argv, "ab:c:eghi:m:nop:r:s:S:v:x:w:")) != -1) {

1. Please add the help section to tell about the new command line option.

Thanks for the reminder.

Quoted 4 lines

2. Instead of having s and S, it may be better to use a different lower case letter, like "l" for latency. Giving this option will print memory latency and users need to provide the number of samples they prefer per vCPU.

Probably a good idea.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/kvm/dirty_log_perf_test.c +2 -0
@@ -480,6 +485,9 @@ int main(int argc, char *argv[])
                 case 's':
                         p.backing_src = parse_backing_src_type(optarg);
                         break;
+               case 'S':
+                       p.samples_per_vcpu = atoi_positive("Number of  
 samples/vcpu", optarg);
Quoted 8 lines

tools/testing/selftests/kvm/dirty_log_perf_test.c int main(int argc, char *argv[]) · line 490

Quoted 1 line
tools/testing/selftests/kvm/dirty_log_perf_test.c +1 -0
+                       break;

This will force users to always see latency stat when they do not want to see it. I think this patch should be modified in a way to easily disable this feature.
I might be wrong here and it is actually a useful metric to see with each run. If this is true then maybe the commit should mention why it is good for each run.

It gives an extra line of information per run. This would help people distinguish between cases where the average is high because most accesses are high or because a a small percentage of accesses are *really* high.

But in general I agree it should be silent unless requested.

Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/kvm/lib/memstress.c +4 -0
+void memstress_print_percentiles(struct kvm_vm *vm, int nr_vcpus)
+{
+       uint64_t n_samples = nr_vcpus * memstress_args.samples_per_vcpu;
+       uint64_t *host_latency_samples = addr_gva2hva(vm,  

memstress_args.latency_samples);

(quoted diff) +2 -0
+
+       qsort(host_latency_samples, n_samples, sizeof(uint64_t),  

&memstress_qcmp);

Quoted 8 lines
Quoted 4 lines
(quoted diff) +2 -0
+
+       pr_info("Latency distribution (ns) = min:%6.0lf, 50th:%6.0lf,  

90th:%6.0lf, 99th:%6.0lf, max:%6.0lf\n",
+ cycles_to_ns(vcpus[0], (double)host_latency_samples[0]),

I am not much aware of how tsc is set up and used. Will all vCPUs have the same tsc value? Can this change if vCPU gets scheduled to different pCPU on the host?

All vCPUs *in one VM* should have the same frequency. The alternative is probably possible but so weird I can't imagine a reason for doing it.

Quoted 10 lines
Quoted 9 lines

+ cycles_to_ns(vcpus[0],
(double)host_latency_samples[n_samples / 2]),
+ cycles_to_ns(vcpus[0],
(double)host_latency_samples[n_samples * 9 / 10]),
+ cycles_to_ns(vcpus[0],
(double)host_latency_samples[n_samples * 99 / 100]),
+ cycles_to_ns(vcpus[0],
(double)host_latency_samples[n_samples - 1])); +}

Quoted 3 lines

All of the dirty_log_perf_tests print data in seconds followed by nanoseconds. I will recommend keeping it the same. Also, 9 digits for nanoseconds instead of 6 as in other formats.

Consistency here is a good thing. Thanks for pointing out.

#15 lore raw

Re: [PATCH v2 2/2] KVM: selftests: Print summary stats of memory latency distribution

Quoted 11 lines · Colton Lewis
Quoted 9 lines · Vipin Sharma
Quoted 3 lines · Colton Lewis

+ pr_info("Latency distribution (ns) = min:%6.0lf,
50th:%6.0lf, 90th:%6.0lf, 99th:%6.0lf, max:%6.0lf\n",
+ cycles_to_ns(vcpus[0], (double)host_latency_samples[0]),

I am not much aware of how tsc is set up and used. Will all vCPUs have the same tsc value? Can this change if vCPU gets scheduled to different pCPU on the host?

FWIW, if this test were run on older CPUs, there would be potential divergence across pCPUs that would then bleed into vCPUs to some extent. Older CPUs tied the TSC frequency to the core frequency, e.g. would change frequency depending on the power/turbo state, and the TSC would even stop counting altogether at certain C-states. KVM does its best to adjust the guest's perception of the TSC, but it can't be hidden completely.

But for what this test is trying to do, IMO there's zero reason to worry about that.

Quoted 2 lines

All vCPUs *in one VM* should have the same frequency. The alternative is probably possible but so weird I can't imagine a reason for doing it.

Somewhat related to Vipin's question, "host_latency_samples" is a confusing name. It's easy to miss that "host_latency_samples" are actually samples collected in the guest, and thus to think that this code will depend on which pCPU it runs on.

I don't see any reason for such a verbose name, e.g. can't it just be "samples"?

#16 lore raw

Re: [PATCH v2 2/2] KVM: selftests: Print summary stats of memory latency distribution

Quoted 21 lines · Sean Christopherson
Quoted 12 lines · Colton Lewis
Quoted 5 lines · Vipin Sharma
Quoted 3 lines · Colton Lewis

+ pr_info("Latency distribution (ns) = min:%6.0lf,
50th:%6.0lf, 90th:%6.0lf, 99th:%6.0lf, max:%6.0lf\n",
+ cycles_to_ns(vcpus[0],

(double)host_latency_samples[0]),

Quoted 3 lines

I am not much aware of how tsc is set up and used. Will all vCPUs have the same tsc value? Can this change if vCPU gets scheduled to different pCPU on the host?

FWIW, if this test were run on older CPUs, there would be potential divergence across pCPUs that would then bleed into vCPUs to some extent. Older CPUs tied the TSC frequency to the core frequency, e.g. would change frequency depending on the power/turbo state, and the TSC would even stop counting altogether at certain C-states. KVM does its best to adjust the guest's perception of the TSC, but it can't be hidden completely.

Quoted 2 lines

But for what this test is trying to do, IMO there's zero reason to worry about that.

Thanks for the confirmation.

Quoted 8 lines
Quoted 2 lines

All vCPUs *in one VM* should have the same frequency. The alternative is probably possible but so weird I can't imagine a reason for doing it.

Somewhat related to Vipin's question, "host_latency_samples" is a confusing name. It's easy to miss that "host_latency_samples" are actually samples collected in the guest, and thus to think that this code will depend on which pCPU it runs on.

I don't see any reason for such a verbose name, e.g. can't it just be "samples"?

Yes it can.