[PATCH v4 04/10] KVM: selftests: Read binary stat data in lib

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
---
 .../selftests/kvm/include/kvm_util_base.h     |  3 +++
 .../selftests/kvm/kvm_binary_stats_test.c     | 20 +++++-------------
 tools/testing/selftests/kvm/lib/kvm_util.c    | 21 +++++++++++++++++++
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index c5f34551ff76..b2684cfc2cb1 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -405,6 +405,9 @@ 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);
+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);
 
diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
index e4795bad7db6..97b180249ba0 100644
--- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c
+++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
@@ -20,6 +20,8 @@
 #include "asm/kvm.h"
 #include "linux/kvm.h"
 
+#define STAT_MAX_ELEMENTS 1000
+
 static void stats_test(int stats_fd)
 {
 	ssize_t ret;
@@ -29,7 +31,7 @@ static void stats_test(int stats_fd)
 	struct kvm_stats_header header;
 	char *id;
 	struct kvm_stats_desc *stats_desc;
-	u64 *stats_data;
+	u64 stats_data[STAT_MAX_ELEMENTS];
 	struct kvm_stats_desc *pdesc;
 
 	/* Read kvm stats header */
@@ -130,25 +132,13 @@ static void stats_test(int stats_fd)
 			pdesc->offset, pdesc->name);
 	}
 
-	/* Allocate memory for stats data */
-	stats_data = malloc(size_data);
-	TEST_ASSERT(stats_data, "Allocate memory for stats data");
-	/* Read kvm stats data as a bulk */
-	ret = pread(stats_fd, stats_data, size_data, header.data_offset);
-	TEST_ASSERT(ret == size_data, "Read KVM stats data");
 	/* Read kvm stats data one by one */
-	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);
-		size_data += pdesc->size * sizeof(*stats_data);
+		read_stat_data(stats_fd, &header, pdesc, stats_data,
+			       ARRAY_SIZE(stats_data));
 	}
 
-	free(stats_data);
 	free(stats_desc);
 	free(id);
 }
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index e3ae26fbef03..64e2085f1129 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -2593,3 +2593,24 @@ void read_vm_stats_desc(int stats_fd, struct kvm_stats_header *header,
 	TEST_ASSERT(ret == stats_descs_size(header),
 		    "Read KVM stats descriptors");
 }
+
+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 ret;
+
+	TEST_ASSERT(desc->size <= max_elements,
+		    "Max data elements should be at least as large as stat data");
+
+	ret = pread(stats_fd, data, desc->size * sizeof(*data),
+		    header->data_offset + desc->offset);
+
+	/* ret from pread is in bytes. */
+	ret = ret / sizeof(*data);
+
+	TEST_ASSERT(ret == desc->size,
+		    "Read data of KVM stats: %s", desc->name);
+
+	return ret;
+}
-- 
2.35.1.1178.g4f1659d476-goog




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux