[PATCH v1 4/5] selftests: Add an interface to read the data of named vcpu stat

Manali Shukla
2024-03-07T05:46:51Z

From: Manali Shukla <Manali.Shukla@amd.com>

The interface is used to read the data values of a specified vcpu stat
from the currenly available binary stats interface.

Signed-off-by: Manali Shukla <Manali.Shukla@amd.com>
---
.../selftests/kvm/include/kvm_util_base.h | 11 +++++
tools/testing/selftests/kvm/lib/kvm_util.c | 41 +++++++++++++++++++
2 files changed, 52 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index 9e5afc472c14..294bb42b6940 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -512,6 +512,17 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
struct kvm_stats_desc *desc, uint64_t *data,
size_t max_elements);

+void __vcpu_get_stat(struct kvm_vcpu *vcpu, const char *stat_name, uint64_t *data,
+ size_t max_elements);
+
+static inline uint64_t vcpu_get_stat(struct kvm_vcpu *vcpu, const char *stat_name)
+{
+ uint64_t data;
+
+ __vcpu_get_stat(vcpu, stat_name, &data, 1);
+ return data;
+}
+
void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
size_t max_elements);

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index e066d584c656..3d3a67ea0c7a 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -2166,6 +2166,47 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
desc->name, size, ret);
}

+/*
+ * Read the data of the named vcpu stat
+ *
+ * Input Args:
+ * vcpu - the vcpu for which the stat should be read
+ * stat_name - the name of the stat to read
+ * max_elements - the maximum number of 8-byte values to read into data
+ *
+ * Output Args:
+ * data - the buffer into which stat data should be read
+ *
+ * Read the data values of a specified stat from the binary stats interface.
+ */
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, const char *stat_name, uint64_t *data,
+ size_t max_elements)
+{
+ int vcpu_stats_fd;
+ struct kvm_stats_header header;
+ struct kvm_stats_desc *desc, *t_desc;
+ size_t size_desc;
+ int i;
+
+ vcpu_stats_fd = vcpu_get_stats_fd(vcpu);
+ read_stats_header(vcpu_stats_fd, &header);
+
+ desc = read_stats_descriptors(vcpu_stats_fd, &header);
+ size_desc = get_stats_descriptor_size(&header);
+
+ for (i = 0; i < header.num_desc; ++i) {
+ t_desc = (void *)desc + (i * size_desc);
+
+ if (strcmp(t_desc->name, stat_name))
+ continue;
+
+ read_stat_data(vcpu_stats_fd, &header, t_desc,
+ data, max_elements);
+
+ break;
+ }
+}
+
/*
* Read the data of the named stat
*
--
2.34.1