[PATCH v2 2/3] kvm tools: Move memory stat print to client

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

 



So far we were printing memory statistics in the terminal of the guest,
this was annoying, confusing and inneficient.

This patch moves the print to the terminal which ran 'kvm stat'

Signed-off-by: Sasha Levin <levinsasha928@xxxxxxxxx>
---
 tools/kvm/builtin-stat.c   |   46 ++++++++++++++++++++++++++++++++++++++++++-
 tools/kvm/virtio/balloon.c |   30 +++------------------------
 2 files changed, 48 insertions(+), 28 deletions(-)

diff --git a/tools/kvm/builtin-stat.c b/tools/kvm/builtin-stat.c
index 609a6e5..e28eb5b 100644
--- a/tools/kvm/builtin-stat.c
+++ b/tools/kvm/builtin-stat.c
@@ -5,10 +5,13 @@
 #include <kvm/parse-options.h>
 #include <kvm/kvm-ipc.h>
 
+#include <sys/select.h>
 #include <stdio.h>
 #include <string.h>
 #include <signal.h>
 
+#include <linux/virtio_balloon.h>
+
 struct stat_cmd {
 	u32 type;
 	u32 len;
@@ -51,14 +54,53 @@ void kvm_stat_help(void)
 static int do_memstat(const char *name, int sock)
 {
 	struct stat_cmd cmd = {KVM_IPC_STAT, 0};
+	struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
+	fd_set fdset;
+	struct timeval t = { .tv_sec = 1 };
 	int r;
+	u8 i;
 
-	printf("Sending memstat command to %s, output should be on the targets' terminal.\n", name);
-
+	FD_ZERO(&fdset);
+	FD_SET(sock, &fdset);
 	r = write(sock, &cmd, sizeof(cmd));
 	if (r < 0)
 		return r;
 
+	r = select(1, &fdset, NULL, NULL, &t);
+	if (r < 0) {
+		pr_error("Could not retrieve mem stats from %s", name);
+		return r;
+	}
+	r = read(sock, &stats, sizeof(stats));
+	if (r < 0)
+		return r;
+
+	printf("\n\n\t*** Guest memory statistics ***\n\n");
+	for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
+		switch (stats[i].tag) {
+		case VIRTIO_BALLOON_S_SWAP_IN:
+			printf("The amount of memory that has been swapped in (in bytes):");
+			break;
+		case VIRTIO_BALLOON_S_SWAP_OUT:
+			printf("The amount of memory that has been swapped out to disk (in bytes):");
+			break;
+		case VIRTIO_BALLOON_S_MAJFLT:
+			printf("The number of major page faults that have occurred:");
+			break;
+		case VIRTIO_BALLOON_S_MINFLT:
+			printf("The number of minor page faults that have occurred:");
+			break;
+		case VIRTIO_BALLOON_S_MEMFREE:
+			printf("The amount of memory not being used for any purpose (in bytes):");
+			break;
+		case VIRTIO_BALLOON_S_MEMTOT:
+			printf("The total amount of memory available (in bytes):");
+			break;
+		}
+		printf("%llu\n", stats[i].val);
+	}
+	printf("\n");
+
 	return 0;
 }
 
diff --git a/tools/kvm/virtio/balloon.c b/tools/kvm/virtio/balloon.c
index 991610f..0228686 100644
--- a/tools/kvm/virtio/balloon.c
+++ b/tools/kvm/virtio/balloon.c
@@ -142,36 +142,14 @@ static int virtio_bln__collect_stats(void)
 
 static void virtio_bln__print_stats(int fd, u32 type, u32 len, u8 *msg)
 {
-	u16 i;
+	int r;
 
 	if (virtio_bln__collect_stats() < 0)
 		return;
 
-	printf("\n\n\t*** Guest memory statistics ***\n\n");
-	for (i = 0; i < bdev.stat_count; i++) {
-		switch (bdev.stats[i].tag) {
-		case VIRTIO_BALLOON_S_SWAP_IN:
-			printf("The amount of memory that has been swapped in (in bytes):");
-			break;
-		case VIRTIO_BALLOON_S_SWAP_OUT:
-			printf("The amount of memory that has been swapped out to disk (in bytes):");
-			break;
-		case VIRTIO_BALLOON_S_MAJFLT:
-			printf("The number of major page faults that have occurred:");
-			break;
-		case VIRTIO_BALLOON_S_MINFLT:
-			printf("The number of minor page faults that have occurred:");
-			break;
-		case VIRTIO_BALLOON_S_MEMFREE:
-			printf("The amount of memory not being used for any purpose (in bytes):");
-			break;
-		case VIRTIO_BALLOON_S_MEMTOT:
-			printf("The total amount of memory available (in bytes):");
-			break;
-		}
-		printf("%llu\n", bdev.stats[i].val);
-	}
-	printf("\n");
+	r = write(fd, bdev.stats, sizeof(bdev.stats));
+	if (r < 0)
+		pr_warning("Failed sending memory stats");
 }
 
 static void handle_mem(int fd, u32 type, u32 len, u8 *msg)
-- 
1.7.7

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[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