aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/kvm/lib/perf_test_util.c
diff options
context:
space:
mode:
authorDavid Matlack <dmatlack@google.com>2021-11-11 00:12:55 +0000
committerPaolo Bonzini <pbonzini@redhat.com>2021-11-16 07:43:28 -0500
commit81bcb26172a8f00840e0ca44277272dcb673887a (patch)
tree0e947a01873939ed8db43983f877914ceaf7a866 /tools/testing/selftests/kvm/lib/perf_test_util.c
parentKVM: selftests: Start at iteration 0 instead of -1 (diff)
downloadlinux-dev-81bcb26172a8f00840e0ca44277272dcb673887a.tar.xz
linux-dev-81bcb26172a8f00840e0ca44277272dcb673887a.zip
KVM: selftests: Move vCPU thread creation and joining to common helpers
Move vCPU thread creation and joining to common helper functions. This is in preparation for the next commit which ensures that all vCPU threads are fully created before entering guest mode on any one vCPU. No functional change intended. Signed-off-by: David Matlack <dmatlack@google.com> Reviewed-by: Ben Gardon <bgardon@google.com> Message-Id: <20211111001257.1446428-3-dmatlack@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tools/testing/selftests/kvm/lib/perf_test_util.c')
-rw-r--r--tools/testing/selftests/kvm/lib/perf_test_util.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/tools/testing/selftests/kvm/lib/perf_test_util.c b/tools/testing/selftests/kvm/lib/perf_test_util.c
index 77f9eb5667c9..d646477ed16a 100644
--- a/tools/testing/selftests/kvm/lib/perf_test_util.c
+++ b/tools/testing/selftests/kvm/lib/perf_test_util.c
@@ -16,6 +16,20 @@ struct perf_test_args perf_test_args;
*/
static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
+struct vcpu_thread {
+ /* The id of the vCPU. */
+ int vcpu_id;
+
+ /* The pthread backing the vCPU. */
+ pthread_t thread;
+};
+
+/* The vCPU threads involved in this test. */
+static struct vcpu_thread vcpu_threads[KVM_MAX_VCPUS];
+
+/* The function run by each vCPU thread, as provided by the test. */
+static void (*vcpu_thread_fn)(struct perf_test_vcpu_args *);
+
/*
* Continuously write to the first 8 bytes of each page in the
* specified region.
@@ -177,3 +191,35 @@ void perf_test_set_wr_fract(struct kvm_vm *vm, int wr_fract)
perf_test_args.wr_fract = wr_fract;
sync_global_to_guest(vm, perf_test_args);
}
+
+static void *vcpu_thread_main(void *data)
+{
+ struct vcpu_thread *vcpu = data;
+
+ vcpu_thread_fn(&perf_test_args.vcpu_args[vcpu->vcpu_id]);
+
+ return NULL;
+}
+
+void perf_test_start_vcpu_threads(int vcpus, void (*vcpu_fn)(struct perf_test_vcpu_args *))
+{
+ int vcpu_id;
+
+ vcpu_thread_fn = vcpu_fn;
+
+ for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++) {
+ struct vcpu_thread *vcpu = &vcpu_threads[vcpu_id];
+
+ vcpu->vcpu_id = vcpu_id;
+
+ pthread_create(&vcpu->thread, NULL, vcpu_thread_main, vcpu);
+ }
+}
+
+void perf_test_join_vcpu_threads(int vcpus)
+{
+ int vcpu_id;
+
+ for (vcpu_id = 0; vcpu_id < vcpus; vcpu_id++)
+ pthread_join(vcpu_threads[vcpu_id].thread, NULL);
+}