On Mon, Apr 11, 2022 at 2:10 PM Ben Gardon <[email protected]> wrote:
>
> Move the code to read the binary stats descriptors to the KVM selftests
> library. It will be re-used by other tests to check KVM behavior.
>
> No functional change intended.
>
> Signed-off-by: Ben Gardon <[email protected]>
> ---
> .../selftests/kvm/include/kvm_util_base.h | 4 +++
> .../selftests/kvm/kvm_binary_stats_test.c | 9 ++----
> tools/testing/selftests/kvm/lib/kvm_util.c | 29 +++++++++++++++++++
> 3 files changed, 35 insertions(+), 7 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
> index 5ba3132f3110..c5f34551ff76 100644
> --- a/tools/testing/selftests/kvm/include/kvm_util_base.h
> +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
> @@ -401,6 +401,10 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid);
> int vm_get_stats_fd(struct kvm_vm *vm);
> int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid);
> void read_vm_stats_header(int stats_fd, struct kvm_stats_header *header);
> +struct kvm_stats_desc *alloc_vm_stats_desc(int stats_fd,
> + struct kvm_stats_header *header);
> +void read_vm_stats_desc(int stats_fd, struct kvm_stats_header *header,
> + struct kvm_stats_desc *stats_desc);
Same feedback here as the previous patch about getting rid of "vm_" in
the function names.
>
> uint32_t guest_get_vcpuid(void);
>
> diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
> index 22c22a90f15a..e4795bad7db6 100644
> --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c
> +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
> @@ -62,14 +62,9 @@ static void stats_test(int stats_fd)
> header.data_offset),
> "Descriptor block is overlapped with data block");
>
> - /* Allocate memory for stats descriptors */
> - stats_desc = calloc(header.num_desc, size_desc);
> - TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
> /* Read kvm stats descriptors */
> - ret = pread(stats_fd, stats_desc,
> - size_desc * header.num_desc, header.desc_offset);
> - TEST_ASSERT(ret == size_desc * header.num_desc,
> - "Read KVM stats descriptors");
> + stats_desc = alloc_vm_stats_desc(stats_fd, &header);
> + read_vm_stats_desc(stats_fd, &header, stats_desc);
>
> /* Sanity check for fields in descriptors */
> for (i = 0; i < header.num_desc; ++i) {
> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
> index 0caf28e324ed..e3ae26fbef03 100644
> --- a/tools/testing/selftests/kvm/lib/kvm_util.c
> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c
> @@ -2564,3 +2564,32 @@ void read_vm_stats_header(int stats_fd, struct kvm_stats_header *header)
> ret = read(stats_fd, header, sizeof(*header));
> TEST_ASSERT(ret == sizeof(*header), "Read stats header");
> }
> +
> +static ssize_t stats_descs_size(struct kvm_stats_header *header)
> +{
> + return header->num_desc *
> + (sizeof(struct kvm_stats_desc) + header->name_size);
> +}
> +
> +/* Caller is responsible for freeing the returned kvm_stats_desc. */
> +struct kvm_stats_desc *alloc_vm_stats_desc(int stats_fd,
> + struct kvm_stats_header *header)
> +{
> + struct kvm_stats_desc *stats_desc;
> +
> + stats_desc = malloc(stats_descs_size(header));
> + TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
> +
> + return stats_desc;
> +}
> +
> +void read_vm_stats_desc(int stats_fd, struct kvm_stats_header *header,
> + struct kvm_stats_desc *stats_desc)
Reading the descriptors only needs to be done once per fd. So from an
API point of view I don't think there's value in creating separate
functions for the allocation and the read; just combine them in one.
> +{
> + ssize_t ret;
> +
> + ret = pread(stats_fd, stats_desc, stats_descs_size(header),
> + header->desc_offset);
> + TEST_ASSERT(ret == stats_descs_size(header),
> + "Read KVM stats descriptors");
> +}
> --
> 2.35.1.1178.g4f1659d476-goog
>