[PATCH v1 10/18] KVM: selftests/tsc_scaling_sync: vcpu related code consolidation

From: Wei Wang
Date: Mon Oct 24 2022 - 07:38:39 EST


Remove the unnecessary definition of the vcpu_threads[] array, as
it has beend included in the kvm_vcpu struct. Use the helper functions
to create and join the vcpu threads.

Signed-off-by: Wei Wang <wei.w.wang@xxxxxxxxx>
---
.../selftests/kvm/x86_64/tsc_scaling_sync.c | 25 ++++---------------
1 file changed, 5 insertions(+), 20 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c b/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c
index 47139aab7408..34a8beef42b6 100644
--- a/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c
+++ b/tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c
@@ -15,7 +15,6 @@
#include <time.h>
#include <sched.h>
#include <signal.h>
-#include <pthread.h>

#define NR_TEST_VCPUS 20

@@ -44,18 +43,15 @@ static void guest_code(void)
}


-static void *run_vcpu(void *_cpu_nr)
+static void *run_vcpu(void *data)
{
- unsigned long vcpu_id = (unsigned long)_cpu_nr;
+ struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
unsigned long failures = 0;
static bool first_cpu_done;
- struct kvm_vcpu *vcpu;

/* The kernel is fine, but vm_vcpu_add() needs locking */
pthread_spin_lock(&create_lock);

- vcpu = vm_vcpu_add(vm, vcpu_id, guest_code);
-
if (!first_cpu_done) {
first_cpu_done = true;
vcpu_set_msr(vcpu, MSR_IA32_TSC, TEST_TSC_OFFSET);
@@ -95,23 +91,12 @@ int main(int argc, char *argv[])
{
TEST_REQUIRE(kvm_has_cap(KVM_CAP_VM_TSC_CONTROL));

- vm = vm_create(NR_TEST_VCPUS);
+ vm = vm_create_with_vcpus(NR_TEST_VCPUS, guest_code, NULL);
vm_ioctl(vm, KVM_SET_TSC_KHZ, (void *) TEST_TSC_KHZ);

pthread_spin_init(&create_lock, PTHREAD_PROCESS_PRIVATE);
- pthread_t cpu_threads[NR_TEST_VCPUS];
- unsigned long cpu;
- for (cpu = 0; cpu < NR_TEST_VCPUS; cpu++)
- pthread_create(&cpu_threads[cpu], NULL, run_vcpu, (void *)cpu);
-
- unsigned long failures = 0;
- for (cpu = 0; cpu < NR_TEST_VCPUS; cpu++) {
- void *this_cpu_failures;
- pthread_join(cpu_threads[cpu], &this_cpu_failures);
- failures += (unsigned long)this_cpu_failures;
- }
-
- TEST_ASSERT(!failures, "TSC sync failed");
+ vm_vcpu_threads_create(vm, run_vcpu, 0);
+ vm_vcpu_threads_join(vm);
pthread_spin_destroy(&create_lock);
kvm_vm_free(vm);
return 0;
--
2.27.0