From 524b59a49ed582e8c61e08373c1ac6cab16bd3a2 Mon Sep 17 00:00:00 2001 From: Sasha Levin Date: Sun, 16 Oct 2011 21:57:55 +0200 Subject: [PATCH] kvm tools: Move memory stat print to client 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 Signed-off-by: Pekka Enberg --- 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 609a6e517db1..e28eb5b7d51b 100644 --- a/tools/kvm/builtin-stat.c +++ b/tools/kvm/builtin-stat.c @@ -5,10 +5,13 @@ #include #include +#include #include #include #include +#include + 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 991610f99df8..0228686c9df5 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) -- 2.39.5