Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [kvm-unit-tests PATCH] x86: add HPET counter read micro benchmark and enable/disable torture tests
@ 2025-07-02 14:51 Igor Mammedov
  2025-07-04 15:51 ` Igor Mammedov
  0 siblings, 1 reply; 2+ messages in thread
From: Igor Mammedov @ 2025-07-02 14:51 UTC (permalink / raw)
  To: kvm; +Cc: pbonzini

test is to be used for benchmarking/validating HPET main counter read

how to run:
   QEMU=/foo/qemu-system-x86_64 x86/run x86/hpet_read_test.flat -smp X
where X is max number of logical CPUs on host

it will 1st execute concurrent read benchmark
and after that it will run torture test enabling/disabling HPET counter,
while running readers in parallel. Goal is to verify counter that always
goes up.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
this also will be used for testing upcomming HPET fain-grained
locking QEMU series  

---
 x86/Makefile.common  |  2 ++
 x86/hpet_read_test.c | 66 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 x86/hpet_read_test.c

diff --git a/x86/Makefile.common b/x86/Makefile.common
index 5663a65d..ef0e09a6 100644
--- a/x86/Makefile.common
+++ b/x86/Makefile.common
@@ -101,6 +101,8 @@ tests-common += $(TEST_DIR)/realmode.$(exe) \
 realmode_bits := $(if $(call cc-option,-m16,""),16,32)
 endif
 
+tests-common += $(TEST_DIR)/hpet_read_test.$(exe)
+
 test_cases: $(tests-common) $(tests)
 
 $(TEST_DIR)/%.o: CFLAGS += -std=gnu99 -ffreestanding -I $(SRCDIR)/lib -I $(SRCDIR)/lib/x86 -I lib
diff --git a/x86/hpet_read_test.c b/x86/hpet_read_test.c
new file mode 100644
index 00000000..2f56ab6b
--- /dev/null
+++ b/x86/hpet_read_test.c
@@ -0,0 +1,66 @@
+#include "libcflat.h"
+#include "smp.h"
+
+#define HPET_ADDR         0xFED00000L
+#define HPET_COUNTER_ADDR ((uint8_t *)HPET_ADDR + 0xF0UL)
+#define HPET_CONFIG_ADDR  ((uint8_t *)HPET_ADDR + 0x10UL)
+#define HPET_ENABLE_BIT   0x01UL
+#define HPET_CLK_PERIOD   10
+
+static int fail = 0;
+static void hpet_reader(void *data)
+{
+    long cycles = (long)data;
+
+    while (cycles--) {
+        uint64_t old_counter = 0, new_counter;
+        new_counter = *(volatile uint64_t *)HPET_COUNTER_ADDR;
+        if (new_counter < old_counter) {
+            fail = 1;
+            report_abort("HPET counter jumped back");
+        }
+    }
+}
+
+
+static void hpet_writer(void *data)
+{
+    int i;
+    long cycles = (long)data;
+
+    for (i = 0; i < cycles; ++i)
+        if (i % 2)
+            *(volatile uint64_t *)HPET_CONFIG_ADDR |= HPET_ENABLE_BIT;
+        else
+            *(volatile uint64_t *)HPET_CONFIG_ADDR &= ~HPET_ENABLE_BIT;
+}
+
+int main(void)
+{
+    long cycles = 100000;
+    int i;
+    int ncpus;
+    uint64_t start, end, time_ns;
+
+    ncpus = cpu_count();
+    do {
+        printf("starting concurrent read bench on %d cpus\n", ncpus);
+        *(volatile uint64_t *)HPET_CONFIG_ADDR |= HPET_ENABLE_BIT;
+        start = *(volatile uint64_t *)HPET_COUNTER_ADDR;
+        on_cpus(hpet_reader, (void *)cycles);
+        end = (*(volatile uint64_t *)HPET_COUNTER_ADDR);
+        time_ns = (end - start) * HPET_CLK_PERIOD;
+        report(time_ns, "read test took %lu ms\n", time_ns/1000000);
+    } while (0);
+
+    do {
+        printf("starting enable/disable with concurent readers torture\n");
+        for (i = 2; i < ncpus; i++)
+            on_cpu_async(i, hpet_reader, (void *)cycles);
+
+        on_cpu(1, hpet_writer, (void *)cycles);
+    } while (0);
+
+    report(!fail, "passed torture test\n");
+    return report_summary();
+}
-- 
2.47.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [kvm-unit-tests PATCH] x86: add HPET counter read micro benchmark and enable/disable torture tests
  2025-07-02 14:51 [kvm-unit-tests PATCH] x86: add HPET counter read micro benchmark and enable/disable torture tests Igor Mammedov
@ 2025-07-04 15:51 ` Igor Mammedov
  0 siblings, 0 replies; 2+ messages in thread
From: Igor Mammedov @ 2025-07-04 15:51 UTC (permalink / raw)
  To: kvm; +Cc: pbonzini

On Wed,  2 Jul 2025 16:51:23 +0200
Igor Mammedov <imammedo@redhat.com> wrote:

> test is to be used for benchmarking/validating HPET main counter read
> 
> how to run:
>    QEMU=/foo/qemu-system-x86_64 x86/run x86/hpet_read_test.flat -smp X
> where X is max number of logical CPUs on host
> 
> it will 1st execute concurrent read benchmark
> and after that it will run torture test enabling/disabling HPET counter,
> while running readers in parallel. Goal is to verify counter that always
> goes up.

ignore that, fail check in torture test is broken.
I'll send fixed v2 later on

> 
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> ---
> this also will be used for testing upcomming HPET fain-grained
> locking QEMU series  
> 
> ---
>  x86/Makefile.common  |  2 ++
>  x86/hpet_read_test.c | 66 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 68 insertions(+)
>  create mode 100644 x86/hpet_read_test.c
> 
> diff --git a/x86/Makefile.common b/x86/Makefile.common
> index 5663a65d..ef0e09a6 100644
> --- a/x86/Makefile.common
> +++ b/x86/Makefile.common
> @@ -101,6 +101,8 @@ tests-common += $(TEST_DIR)/realmode.$(exe) \
>  realmode_bits := $(if $(call cc-option,-m16,""),16,32)
>  endif
>  
> +tests-common += $(TEST_DIR)/hpet_read_test.$(exe)
> +
>  test_cases: $(tests-common) $(tests)
>  
>  $(TEST_DIR)/%.o: CFLAGS += -std=gnu99 -ffreestanding -I $(SRCDIR)/lib -I $(SRCDIR)/lib/x86 -I lib
> diff --git a/x86/hpet_read_test.c b/x86/hpet_read_test.c
> new file mode 100644
> index 00000000..2f56ab6b
> --- /dev/null
> +++ b/x86/hpet_read_test.c
> @@ -0,0 +1,66 @@
> +#include "libcflat.h"
> +#include "smp.h"
> +
> +#define HPET_ADDR         0xFED00000L
> +#define HPET_COUNTER_ADDR ((uint8_t *)HPET_ADDR + 0xF0UL)
> +#define HPET_CONFIG_ADDR  ((uint8_t *)HPET_ADDR + 0x10UL)
> +#define HPET_ENABLE_BIT   0x01UL
> +#define HPET_CLK_PERIOD   10
> +
> +static int fail = 0;
> +static void hpet_reader(void *data)
> +{
> +    long cycles = (long)data;
> +
> +    while (cycles--) {
> +        uint64_t old_counter = 0, new_counter;
> +        new_counter = *(volatile uint64_t *)HPET_COUNTER_ADDR;
> +        if (new_counter < old_counter) {
> +            fail = 1;
> +            report_abort("HPET counter jumped back");
> +        }
> +    }
> +}
> +
> +
> +static void hpet_writer(void *data)
> +{
> +    int i;
> +    long cycles = (long)data;
> +
> +    for (i = 0; i < cycles; ++i)
> +        if (i % 2)
> +            *(volatile uint64_t *)HPET_CONFIG_ADDR |= HPET_ENABLE_BIT;
> +        else
> +            *(volatile uint64_t *)HPET_CONFIG_ADDR &= ~HPET_ENABLE_BIT;
> +}
> +
> +int main(void)
> +{
> +    long cycles = 100000;
> +    int i;
> +    int ncpus;
> +    uint64_t start, end, time_ns;
> +
> +    ncpus = cpu_count();
> +    do {
> +        printf("starting concurrent read bench on %d cpus\n", ncpus);
> +        *(volatile uint64_t *)HPET_CONFIG_ADDR |= HPET_ENABLE_BIT;
> +        start = *(volatile uint64_t *)HPET_COUNTER_ADDR;
> +        on_cpus(hpet_reader, (void *)cycles);
> +        end = (*(volatile uint64_t *)HPET_COUNTER_ADDR);
> +        time_ns = (end - start) * HPET_CLK_PERIOD;
> +        report(time_ns, "read test took %lu ms\n", time_ns/1000000);
> +    } while (0);
> +
> +    do {
> +        printf("starting enable/disable with concurent readers torture\n");
> +        for (i = 2; i < ncpus; i++)
> +            on_cpu_async(i, hpet_reader, (void *)cycles);
> +
> +        on_cpu(1, hpet_writer, (void *)cycles);
> +    } while (0);
> +
> +    report(!fail, "passed torture test\n");
> +    return report_summary();
> +}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-07-04 15:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-02 14:51 [kvm-unit-tests PATCH] x86: add HPET counter read micro benchmark and enable/disable torture tests Igor Mammedov
2025-07-04 15:51 ` Igor Mammedov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox