@@ -403,6 +403,9 @@ int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid);
void read_stats_header(int stats_fd, struct kvm_stats_header *header);
struct kvm_stats_desc *read_stats_desc(int stats_fd,
struct kvm_stats_header *header);
+int read_stat_data(int stats_fd, struct kvm_stats_header *header,
+ struct kvm_stats_desc *desc, uint64_t *data,
+ ssize_t max_elements);
uint32_t guest_get_vcpuid(void);
@@ -160,11 +160,8 @@ static void stats_test(int stats_fd)
size_data = 0;
for (i = 0; i < header.num_desc; ++i) {
pdesc = (void *)stats_desc + i * size_desc;
- ret = pread(stats_fd, stats_data,
- pdesc->size * sizeof(*stats_data),
- header.data_offset + size_data);
- TEST_ASSERT(ret == pdesc->size * sizeof(*stats_data),
- "Read data of KVM stats: %s", pdesc->name);
+ read_stat_data(stats_fd, &header, pdesc, stats_data,
+ pdesc->size);
size_data += pdesc->size * sizeof(*stats_data);
}
@@ -2615,3 +2615,39 @@ struct kvm_stats_desc *read_stats_desc(int stats_fd,
return stats_desc;
}
+
+/*
+ * Read stat data for a particular stat
+ *
+ * Input Args:
+ * stats_fd - the file descriptor for the binary stats file from which to read
+ * header - the binary stats metadata header corresponding to the given FD
+ * desc - the binary stat metadata for the particular stat to be 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
+ *
+ * Return:
+ * The number of data elements read into data or -ERRNO on error.
+ *
+ * Read the data values of a specified stat from the binary stats interface.
+ */
+int read_stat_data(int stats_fd, struct kvm_stats_header *header,
+ struct kvm_stats_desc *desc, uint64_t *data,
+ ssize_t max_elements)
+{
+ ssize_t size = min((ssize_t)desc->size, max_elements);
+ ssize_t ret;
+
+ ret = pread(stats_fd, data, size * sizeof(*data),
+ header->data_offset + desc->offset);
+
+ /* ret from pread is in bytes. */
+ ret = ret / sizeof(*data);
+
+ TEST_ASSERT(ret == size,
+ "Read data of KVM stats: %s", desc->name);
+
+ return ret;
+}
Move the code to read the binary stats data 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 <bgardon@google.com> --- .../selftests/kvm/include/kvm_util_base.h | 3 ++ .../selftests/kvm/kvm_binary_stats_test.c | 7 ++-- tools/testing/selftests/kvm/lib/kvm_util.c | 36 +++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-)