2022-06-05 04:13:53

by Sean Christopherson

[permalink] [raw]
Subject: [PATCH v2 126/144] KVM: selftests: Convert kvm_binary_stats_test away from vCPU IDs

Track vCPUs by their 'struct kvm_vcpu' object in kvm_binary_stats_test,
not by their ID. The per-vCPU helpers will soon take a vCPU instead of a
VM+vcpu_id pair.

Signed-off-by: Sean Christopherson <[email protected]>
---
tools/testing/selftests/kvm/kvm_binary_stats_test.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
index 407e9ea8e6f3..dfc3cf531ced 100644
--- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c
+++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
@@ -172,9 +172,9 @@ static void vm_stats_test(struct kvm_vm *vm)
TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
}

-static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id)
+static void vcpu_stats_test(struct kvm_vcpu *vcpu)
{
- int stats_fd = vcpu_get_stats_fd(vm, vcpu_id);
+ int stats_fd = vcpu_get_stats_fd(vcpu->vm, vcpu->id);

stats_test(stats_fd);
close(stats_fd);
@@ -195,6 +195,7 @@ static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id)
int main(int argc, char *argv[])
{
int i, j;
+ struct kvm_vcpu **vcpus;
struct kvm_vm **vms;
int max_vm = DEFAULT_NUM_VM;
int max_vcpu = DEFAULT_NUM_VCPU;
@@ -220,17 +221,21 @@ int main(int argc, char *argv[])
/* Create VMs and VCPUs */
vms = malloc(sizeof(vms[0]) * max_vm);
TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
+
+ vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);
+ TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");
+
for (i = 0; i < max_vm; ++i) {
vms[i] = vm_create_barebones();
for (j = 0; j < max_vcpu; ++j)
- __vm_vcpu_add(vms[i], j);
+ vcpus[j * max_vcpu + i] = __vm_vcpu_add(vms[i], j);
}

/* Check stats read for every VM and VCPU */
for (i = 0; i < max_vm; ++i) {
vm_stats_test(vms[i]);
for (j = 0; j < max_vcpu; ++j)
- vcpu_stats_test(vms[i], j);
+ vcpu_stats_test(vcpus[j * max_vcpu + i]);
}

for (i = 0; i < max_vm; ++i)
--
2.36.1.255.ge46751e96f-goog


2022-06-10 11:16:27

by Andrew Jones

[permalink] [raw]
Subject: Re: [PATCH v2 126/144] KVM: selftests: Convert kvm_binary_stats_test away from vCPU IDs

On Fri, Jun 03, 2022 at 12:43:13AM +0000, Sean Christopherson wrote:
> Track vCPUs by their 'struct kvm_vcpu' object in kvm_binary_stats_test,
> not by their ID. The per-vCPU helpers will soon take a vCPU instead of a
> VM+vcpu_id pair.
>
> Signed-off-by: Sean Christopherson <[email protected]>
> ---
> tools/testing/selftests/kvm/kvm_binary_stats_test.c | 13 +++++++++----
> 1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
> index 407e9ea8e6f3..dfc3cf531ced 100644
> --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c
> +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
> @@ -172,9 +172,9 @@ static void vm_stats_test(struct kvm_vm *vm)
> TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed");
> }
>
> -static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id)
> +static void vcpu_stats_test(struct kvm_vcpu *vcpu)
> {
> - int stats_fd = vcpu_get_stats_fd(vm, vcpu_id);
> + int stats_fd = vcpu_get_stats_fd(vcpu->vm, vcpu->id);
>
> stats_test(stats_fd);
> close(stats_fd);
> @@ -195,6 +195,7 @@ static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id)
> int main(int argc, char *argv[])
> {
> int i, j;
> + struct kvm_vcpu **vcpus;
> struct kvm_vm **vms;
> int max_vm = DEFAULT_NUM_VM;
> int max_vcpu = DEFAULT_NUM_VCPU;
> @@ -220,17 +221,21 @@ int main(int argc, char *argv[])
> /* Create VMs and VCPUs */
> vms = malloc(sizeof(vms[0]) * max_vm);
> TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
> +
> + vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);
> + TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");
> +
> for (i = 0; i < max_vm; ++i) {
> vms[i] = vm_create_barebones();
> for (j = 0; j < max_vcpu; ++j)
> - __vm_vcpu_add(vms[i], j);
> + vcpus[j * max_vcpu + i] = __vm_vcpu_add(vms[i], j);

The expression for the index should be 'i * max_vcpu + j'. The swapped
i,j usage isn't causing problems now because
DEFAULT_NUM_VM == DEFAULT_NUM_VCPU, but that could change.

> }
>
> /* Check stats read for every VM and VCPU */
> for (i = 0; i < max_vm; ++i) {
> vm_stats_test(vms[i]);
> for (j = 0; j < max_vcpu; ++j)
> - vcpu_stats_test(vms[i], j);
> + vcpu_stats_test(vcpus[j * max_vcpu + i]);

Same comment as above.

Thanks,
drew

> }
>
> for (i = 0; i < max_vm; ++i)
> --
> 2.36.1.255.ge46751e96f-goog
>

2022-06-10 14:48:58

by Sean Christopherson

[permalink] [raw]
Subject: Re: [PATCH v2 126/144] KVM: selftests: Convert kvm_binary_stats_test away from vCPU IDs

On Fri, Jun 10, 2022, Andrew Jones wrote:
> On Fri, Jun 03, 2022 at 12:43:13AM +0000, Sean Christopherson wrote:
> > @@ -220,17 +221,21 @@ int main(int argc, char *argv[])
> > /* Create VMs and VCPUs */
> > vms = malloc(sizeof(vms[0]) * max_vm);
> > TEST_ASSERT(vms, "Allocate memory for storing VM pointers");
> > +
> > + vcpus = malloc(sizeof(struct kvm_vcpu *) * max_vm * max_vcpu);
> > + TEST_ASSERT(vcpus, "Allocate memory for storing vCPU pointers");
> > +
> > for (i = 0; i < max_vm; ++i) {
> > vms[i] = vm_create_barebones();
> > for (j = 0; j < max_vcpu; ++j)
> > - __vm_vcpu_add(vms[i], j);
> > + vcpus[j * max_vcpu + i] = __vm_vcpu_add(vms[i], j);
>
> The expression for the index should be 'i * max_vcpu + j'. The swapped
> i,j usage isn't causing problems now because
> DEFAULT_NUM_VM == DEFAULT_NUM_VCPU, but that could change.

It's better to be lucky than good?

Thanks much, I appreciate the reviews!