Skip to contentSkip to conversation

virtio: Add memory statistics reporting to the balloon driver

Qemu-devel Adam Litke 6 participants 16 messages Jan 5, 2010 – Jan 8, 2010

lore
Share & tools
lore

Adam Litke

author
#0 lore raw

This patch has been discussed (and ACKed) in the past, but has not yet been committed due to the congestion surrounding the 0.12 release and other conflicting changes. I would like to rekindle the discussion so that I can make the necessary changes to get this merged.

This patch is ported to 0.12.1 and has one issue that I am aware of:

It still calls monitor_suspend() and monitor_resume() which I understand is no longer allowed. Does anyone have any ideas on how to enable async monitor commands?

balloon.h +3 -3
index 60b4a5d..dc851d3 100644
@@ -16,12 +16,12 @@
 
 #include "cpu-defs.h"
 
-typedef ram_addr_t (QEMUBalloonEvent)(void *opaque, ram_addr_t target);
+typedef QObject *(QEMUBalloonEvent)(void *opaque, ram_addr_t target);
 
 void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque);
 
-void qemu_balloon(ram_addr_t target);
+int qemu_balloon(ram_addr_t target);
 
-ram_addr_t qemu_balloon_status(void);
+QObject *qemu_balloon_status(void);
 
 #endif
hw/virtio-balloon.c +92 -4

158 lines not on this page · raw message

hw/virtio-balloon.h +15 -0
index 9a0d119..e20cf6b 100644
@@ -25,6 +25,7 @@
 
 /* The feature bitmap for virtio balloon */
 #define VIRTIO_BALLOON_F_MUST_TELL_HOST 0 /* Tell before reclaiming pages */
+#define VIRTIO_BALLOON_F_STATS_VQ 1       /* Memory stats virtqueue */
 
 /* Size of a PFN in the balloon interface. */
 #define VIRTIO_BALLOON_PFN_SHIFT 12
@@ -37,4 +38,18 @@ struct virtio_balloon_config
     uint32_t actual;
 };
 
+/* Memory Statistics */
+#define VIRTIO_BALLOON_S_SWAP_IN  0   /* Amount of memory swapped in */
+#define VIRTIO_BALLOON_S_SWAP_OUT 1   /* Amount of memory swapped out */
+#define VIRTIO_BALLOON_S_MAJFLT   2   /* Number of major faults */
+#define VIRTIO_BALLOON_S_MINFLT   3   /* Number of minor faults */
+#define VIRTIO_BALLOON_S_MEMFREE  4   /* Total amount of free memory */
+#define VIRTIO_BALLOON_S_MEMTOT   5   /* Total amount of memory */
+#define VIRTIO_BALLOON_S_NR       6
+
+typedef struct VirtIOBalloonStat {
+    uint16_t tag;
+    uint64_t val;
+} __attribute__((packed)) VirtIOBalloonStat;
+
 #endif
monitor.c +46 -35

110 lines not on this page · raw message

monitor.h +1 -0
index 6ed117a..f7d8780 100644
@@ -32,6 +32,7 @@ void monitor_resume(Monitor *mon);
 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
                                  BlockDriverCompletionFunc *completion_cb,
                                  void *opaque);
+void monitor_print_balloon(Monitor *mon, const QObject *data);
 
 int monitor_get_fd(Monitor *mon, const char *fdname);
 
vl.c +8 -4
index 8be3648..029e572 100644
@@ -362,17 +362,21 @@ void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
     qemu_balloon_event_opaque = opaque;
 }
 
-void qemu_balloon(ram_addr_t target)
+int qemu_balloon(ram_addr_t target)
 {
-    if (qemu_balloon_event)
+    if (qemu_balloon_event) {
         qemu_balloon_event(qemu_balloon_event_opaque, target);
+        return 1;
+    } else {
+        return 0;
+    }
 }
 
-ram_addr_t qemu_balloon_status(void)
+QObject *qemu_balloon_status(void)
 {
     if (qemu_balloon_event)
         return qemu_balloon_event(qemu_balloon_event_opaque, 0);
-    return 0;
+    return NULL;
 }
 
 /***********************************************************/
Signature · 2 lines
Thanks,
Adam
#1 lore raw
Quoted 12 lines · Adam Litke

This patch has been discussed (and ACKed) in the past, but has not yet been committed due to the congestion surrounding the 0.12 release and other conflicting changes. I would like to rekindle the discussion so that I can make the necessary changes to get this merged.

This patch is ported to 0.12.1 and has one issue that I am aware of:

It still calls monitor_suspend() and monitor_resume() which I understand is no longer allowed. Does anyone have any ideas on how to enable async monitor commands?

hw/virtio-balloon.c static void virtio_balloon_set_config(VirtIODevice *vdev, · line 201

Quoted 5 lines
hw/virtio-balloon.c +5 -0
+static void request_stats(VirtIOBalloon *vb)
+{
+    vb->stats_requested = 1;
+    reset_stats(vb);
+    monitor_suspend(cur_mon);

This bit is not going to work reliably anymore. Really, it never worked reliably but this is exacerbated with -qmp since a QMP session will never be the cur_mon.

We have a couple of options:

1) Introduce a query-balloon command that returns immediately, and triggers a request for the guest to update the balloon stats. When the guest does update the balloon stats, trigger an asynchronous message. Asynchronous messages are ignored by the human monitor so they would never be displayed there which is unfortunate.

2) Make info balloon show the last valid stats and have it request new stats. Stats will always be delayed but it avoids async messages.

3) Make qemu request balloon stats regularly (maybe every 10 seconds) and display the latest stats with info balloon. This avoids the problem in #2 but it means that qemu determines the poll rate instead of a management tool.

4) Make info-balloon a proper asynchronous command. We need new infrastructure to allow a qmp handler to take a callback that can be used to delay the completion of the command. This addresses all of the above problems but it introduces a new one. Command completion now depends on the guest. This potentially could trip up a naive management tool that doesn't realize that the info-balloon command may never complete.

I'm on the fence between 3 and 4 myself.

Regards,

Anthony Liguori

#2 lore raw
Quoted 7 lines · Anthony Liguori

3) Make qemu request balloon stats regularly (maybe every 10 seconds) and display the latest stats with info balloon. This avoids the problem in #2 but it means that qemu determines the poll rate instead of a management tool.

4) Make info-balloon a proper asynchronous command. We need new infrastructure to allow a qmp handler to take a callback that can be used to delay the completion of the command. This addresses all of the above problems but it introduces a new one. Command completion now depends on the guest. This potentially could trip up a naive management tool that doesn't realize that the info-balloon command may never complete.

I'm on the fence between 3 and 4 myself.

Can I tip you over to #4? #3 means we have no idea when the stats were generated. With #4, we can't be sure, but it usually be close to when the command returns.

The command should include a timeout so a broken guest won't hang a management tool thread.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw
Quoted 4 lines · Avi Kivity
Quoted 3 lines · Anthony Liguori

3) Make qemu request balloon stats regularly (maybe every 10 seconds) and display the latest stats with info balloon. This avoids the problem in #2 but it means that qemu determines the poll rate instead of a management tool.

Quoted 8 lines
Quoted 4 lines

4) Make info-balloon a proper asynchronous command. We need new infrastructure to allow a qmp handler to take a callback that can be used to delay the completion of the command. This addresses all of the above problems but it introduces a new one. Command completion now depends on the guest. This potentially could trip up a naive management tool that doesn't realize that the info-balloon command may never complete.

I'm on the fence between 3 and 4 myself.

Can I tip you over to #4? #3 means we have no idea when the stats were generated. With #4, we can't be sure, but it usually be close to when the command returns.

The command should include a timeout so a broken guest won't hang a management tool thread.

Generally, timeouts are evil but if we did something like, wait 10 seconds and if we don't hear a response from the guest, return the last data set, I think I would be okay with that. It means we may be reporting stale data, but at the same time, the data is coming from a guest so it can't be considered authoritative anyway.

Regards,

Anthony Liguori

author
#4 lore raw

Thanks for the ideas and feedback. I will play around with #4 and see if I can hack up a prototype.

Quoted 35 lines · Anthony Liguori
Quoted 18 lines · Avi Kivity
Quoted 14 lines · Anthony Liguori

3) Make qemu request balloon stats regularly (maybe every 10 seconds) and display the latest stats with info balloon. This avoids the problem in #2 but it means that qemu determines the poll rate instead of a management tool.

4) Make info-balloon a proper asynchronous command. We need new infrastructure to allow a qmp handler to take a callback that can be used to delay the completion of the command. This addresses all of the above problems but it introduces a new one. Command completion now depends on the guest. This potentially could trip up a naive management tool that doesn't realize that the info-balloon command may never complete.

Quoted 2 lines

I'm on the fence between 3 and 4 myself.

Quoted 6 lines

Can I tip you over to #4? #3 means we have no idea when the stats were generated. With #4, we can't be sure, but it usually be close to when the command returns.

The command should include a timeout so a broken guest won't hang a management tool thread.

Generally, timeouts are evil but if we did something like, wait 10 seconds and if we don't hear a response from the guest, return the last data set, I think I would be okay with that. It means we may be reporting stale data, but at the same time, the data is coming from a guest so it can't be considered authoritative anyway.

Regards,

Anthony Liguori

Signature · 2 lines
Thanks,
Adam
author
#7 lore raw
In reply to Daniel P. Berrange
Quoted 51 lines · Daniel P. Berrange
Quoted 31 lines · Anthony Liguori
Quoted 12 lines · Adam Litke

This patch has been discussed (and ACKed) in the past, but has not yet been committed due to the congestion surrounding the 0.12 release and other conflicting changes. I would like to rekindle the discussion so that I can make the necessary changes to get this merged.

This patch is ported to 0.12.1 and has one issue that I am aware of:

It still calls monitor_suspend() and monitor_resume() which I understand is no longer allowed. Does anyone have any ideas on how to enable async monitor commands?

hw/virtio-balloon.c static void virtio_balloon_set_config(VirtIODevice *vdev, · line 201

Quoted 5 lines
hw/virtio-balloon.c +5 -0
+static void request_stats(VirtIOBalloon *vb)
+{
+    vb->stats_requested = 1;
+    reset_stats(vb);
+    monitor_suspend(cur_mon);

This bit is not going to work reliably anymore. Really, it never worked reliably but this is exacerbated with -qmp since a QMP session will never be the cur_mon.

We have a couple of options:

1) Introduce a query-balloon command that returns immediately, and triggers a request for the guest to update the balloon stats. When the guest does update the balloon stats, trigger an asynchronous message. Asynchronous messages are ignored by the human monitor so they would never be displayed there which is unfortunate.

Quoted 7 lines

2) Make info balloon show the last valid stats and have it request new stats. Stats will always be delayed but it avoids async messages.

3) Make qemu request balloon stats regularly (maybe every 10 seconds) and display the latest stats with info balloon. This avoids the problem in #2 but it means that qemu determines the poll rate instead of a management tool.

I don't like the idea of the anything which has hardcoded polling because it just wastes CPU cycles for the 99% of the time when no one is going to care about these stats.

Quoted 6 lines

4) Make info-balloon a proper asynchronous command. We need new infrastructure to allow a qmp handler to take a callback that can be used to delay the completion of the command. This addresses all of the above problems but it introduces a new one. Command completion now depends on the guest. This potentially could trip up a naive management tool that doesn't realize that the info-balloon command may never complete.

Quoted 5 lines

I think 'info-balloon' should be synchronous and without side-effects. ie return the current stats that QEMU has. We could then add a separate 'refresh-balloon' command + async event notification when that completes. The tool that is requiring the stats could thus refresh as often as it likes.

This would work well for the QMP case, but what about for a traditional monitor? We could include a sequence number or timestamp in the memory stats results so the user could tell that they were updated. This doesn't seem very user friendly though.

Signature · 2 lines
Thanks,
Adam
#8 lore raw
Quoted 11 lines · Adam Litke
Quoted 7 lines

I think 'info-balloon' should be synchronous and without side-effects. ie return the current stats that QEMU has. We could then add a separate 'refresh-balloon' command + async event notification when that completes. The tool that is requiring the stats could thus refresh as often as it likes.

This would work well for the QMP case, but what about for a traditional monitor? We could include a sequence number or timestamp in the memory stats results so the user could tell that they were updated. This doesn't seem very user friendly though

A user would have an easier time logging into the guest and using its native tools.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
In reply to Anthony Liguori
Quoted 31 lines · Anthony Liguori
Quoted 12 lines · Adam Litke

This patch has been discussed (and ACKed) in the past, but has not yet been committed due to the congestion surrounding the 0.12 release and other conflicting changes. I would like to rekindle the discussion so that I can make the necessary changes to get this merged.

This patch is ported to 0.12.1 and has one issue that I am aware of:

It still calls monitor_suspend() and monitor_resume() which I understand is no longer allowed. Does anyone have any ideas on how to enable async monitor commands?

hw/virtio-balloon.c static void virtio_balloon_set_config(VirtIODevice *vdev, · line 201

Quoted 5 lines
hw/virtio-balloon.c +5 -0
+static void request_stats(VirtIOBalloon *vb)
+{
+    vb->stats_requested = 1;
+    reset_stats(vb);
+    monitor_suspend(cur_mon);

This bit is not going to work reliably anymore. Really, it never worked reliably but this is exacerbated with -qmp since a QMP session will never be the cur_mon.

We have a couple of options:

1) Introduce a query-balloon command that returns immediately, and triggers a request for the guest to update the balloon stats. When the guest does update the balloon stats, trigger an asynchronous message. Asynchronous messages are ignored by the human monitor so they would never be displayed there which is unfortunate.

Quoted 7 lines

2) Make info balloon show the last valid stats and have it request new stats. Stats will always be delayed but it avoids async messages.

3) Make qemu request balloon stats regularly (maybe every 10 seconds) and display the latest stats with info balloon. This avoids the problem in #2 but it means that qemu determines the poll rate instead of a management tool.

I don't like the idea of the anything which has hardcoded polling because it just wastes CPU cycles for the 99% of the time when no one is going to care about these stats.

Quoted 6 lines

4) Make info-balloon a proper asynchronous command. We need new infrastructure to allow a qmp handler to take a callback that can be used to delay the completion of the command. This addresses all of the above problems but it introduces a new one. Command completion now depends on the guest. This potentially could trip up a naive management tool that doesn't realize that the info-balloon command may never complete.

I think 'info-balloon' should be synchronous and without side-effects. ie return the current stats that QEMU has. We could then add a separate 'refresh-balloon' command + async event notification when that completes. The tool that is requiring the stats could thus refresh as often as it likes.

Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#9 lore raw
In reply to Avi Kivity
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Adam Litke
Quoted 6 lines

I think 'info-balloon' should be synchronous and without side-effects. ie return the current stats that QEMU has. We could then add a separate 'refresh-balloon' command + async event notification when that completes. The tool that is requiring the stats could thus refresh as often as it likes.

Quoted 6 lines
Quoted 4 lines

This would work well for the QMP case, but what about for a traditional monitor? We could include a sequence number or timestamp in the memory stats results so the user could tell that they were updated. This doesn't seem very user friendly though

A user would have an easier time logging into the guest and using its native tools.

Yeah, without objections, I like having info-balloon attempt to refresh stats, timeout after 10s, and return the last reported stats. I think it also satisfies danpb's criteria of not wasting cpu cycles polling.

Regards,

Anthony Liguori

#10 lore raw
Quoted 9 lines · On Thu, 07 Jan 2010 10:56:19 -0600 Anthony Liguori <[email protected]>…
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Adam Litke
Quoted 7 lines

I think 'info-balloon' should be synchronous and without side-effects. ie return the current stats that QEMU has. We could then add a separate 'refresh-balloon' command + async event notification when that completes.
The tool that is requiring the stats could thus refresh as often as it likes.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

This would work well for the QMP case, but what about for a traditional monitor? We could include a sequence number or timestamp in the memory stats results so the user could tell that they were updated. This doesn't seem very user friendly though

A user would have an easier time logging into the guest and using its native tools.

I'm not sure I agree with that, running 'info balloon' in the Monitor
is more convenient than logging specially if you're testing something related to balloon.

Quoted 3 lines

Yeah, without objections, I like having info-balloon attempt to refresh stats, timeout after 10s, and return the last reported stats. I think it also satisfies danpb's criteria of not wasting cpu cycles polling.

I like Daniel's idea too. In practice 'refresh-balloon' is going to
be Anthony's idea #1 for the QMP case, which seems the right way to do it with QMP.

The only problem though is the user Monitor. Ideally handlers should not try
to know if they are in QMP or not.

I would be ok with that in this case, but if we get more asynchronous
handlers doing this we will really need an better interface for that.

#11 lore raw
Quoted 3 lines · On Thu, 7 Jan 2010 15:58:30 -0200 Luiz Capitulino <[email protected]>…

I like Daniel's idea too. In practice 'refresh-balloon' is going to
be Anthony's idea #1 for the QMP case, which seems the right way to do it with QMP.

Hm, something that has just occurred to me: it's easy to have
async messages in the user Monitor, we could add a new type of user print callback called async_print.

This new callback would be called by the Monitor when the async
message API is called but we are in user mode.

This is really today's user_print, but user data is printed
asynchronously.

#5 lore raw
In reply to Anthony Liguori
Quoted 23 lines · Anthony Liguori
Quoted 22 lines · Avi Kivity
Quoted 14 lines · Anthony Liguori

3) Make qemu request balloon stats regularly (maybe every 10 seconds) and display the latest stats with info balloon. This avoids the problem in #2 but it means that qemu determines the poll rate instead of a management tool.

4) Make info-balloon a proper asynchronous command. We need new infrastructure to allow a qmp handler to take a callback that can be used to delay the completion of the command. This addresses all of the above problems but it introduces a new one. Command completion now depends on the guest. This potentially could trip up a naive management tool that doesn't realize that the info-balloon command may never complete.

Quoted 2 lines

I'm on the fence between 3 and 4 myself.

Can I tip you over to #4? #3 means we have no idea when the stats were generated. With #4, we can't be sure, but it usually be close to when the command returns.

Quoted 8 lines
Quoted 2 lines

The command should include a timeout so a broken guest won't hang a management tool thread.

Generally, timeouts are evil but if we did something like, wait 10 seconds and if we don't hear a response from the guest, return the last data set, I think I would be okay with that. It means we may be reporting stale data, but at the same time, the data is coming from a guest so it can't be considered authoritative anyway.

A flag in the response to say if it's fresh or stale would be good - or simply a timestamp in host time.

The timeout does not need to be as long as 10 seconds. If the guest is so slow to respond, management can poll at whatever rate it wants until it gets a fresh response. For this a timestamp is better, because it may be happy with a response that is sufficiently recent but not due to the most recent request.

-- Jamie

#12 lore raw
In reply to Luiz Capitulino
Quoted 12 lines · Luiz Capitulino
Quoted 4 lines · On Thu, 7 Jan 2010 15:58:30 -0200 Luiz Capitulino<[email protected]>…

I like Daniel's idea too. In practice 'refresh-balloon' is going to
be Anthony's idea #1 for the QMP case, which seems the right way to do it with QMP.

Hm, something that has just occurred to me: it's easy to have
async messages in the user Monitor, we could add a new type of user print callback called async_print.

Quoted 6 lines

This new callback would be called by the Monitor when the async
message API is called but we are in user mode.

This is really today's user_print, but user data is printed
asynchronously.

Even if we did that, it still suffers from the problem of a malicious or broken guest that would not respond thereby hanging the monitor.

Regards,

Anthony Liguori

#13 lore raw
Quoted 16 lines · On Fri, 08 Jan 2010 10:31:28 -0600 Anthony Liguori <[email protected]>…
Quoted 15 lines · Luiz Capitulino
Quoted 4 lines · On Thu, 7 Jan 2010 15:58:30 -0200 Luiz Capitulino<[email protected]>…

I like Daniel's idea too. In practice 'refresh-balloon' is going to
be Anthony's idea #1 for the QMP case, which seems the right way to do it with QMP.

Hm, something that has just occurred to me: it's easy to have
async messages in the user Monitor, we could add a new type of user print callback called async_print.

This new callback would be called by the Monitor when the async
message API is called but we are in user mode.

Quoted 6 lines
Quoted 3 lines

This is really today's user_print, but user data is printed
asynchronously.

Even if we did that, it still suffers from the problem of a malicious or broken guest that would not respond thereby hanging the monitor.

True. We could print the timeout but that's not user friendly.

Btw, I don't remember if I said that already but we could send
the command id with the timeout event.

author
#14 lore raw
In reply to Luiz Capitulino
Quoted 11 lines · Luiz Capitulino
Quoted 3 lines · On Thu, 7 Jan 2010 15:58:30 -0200 Luiz Capitulino <[email protected]>…

I like Daniel's idea too. In practice 'refresh-balloon' is going to
be Anthony's idea #1 for the QMP case, which seems the right way to do it with QMP.

Hm, something that has just occurred to me: it's easy to have
async messages in the user Monitor, we could add a new type of user print callback called async_print.

Quoted 5 lines

This new callback would be called by the Monitor when the async
message API is called but we are in user mode.

This is really today's user_print, but user data is printed
asynchronously.

There is one thing I am struggling with as I try to implement this asynchronous behavior. How can I suspend the monitor thread to wait for the stats update to come through? I see qemu_cond_timedwait(), but that appears to only be used for a VM's cpu threads. I also see the bottom half infrastructure, but I am not sure if that is appropriate either.

Any pointers would be greatly appreciated.

Signature · 2 lines
Thanks,
Adam
#15 lore raw
Quoted 15 lines · On Fri, 08 Jan 2010 11:01:31 -0600 Adam Litke <[email protected]> wrote:
Quoted 14 lines · Luiz Capitulino
Quoted 3 lines · On Thu, 7 Jan 2010 15:58:30 -0200 Luiz Capitulino <[email protected]>…

I like Daniel's idea too. In practice 'refresh-balloon' is going to
be Anthony's idea #1 for the QMP case, which seems the right way to do it with QMP.

Hm, something that has just occurred to me: it's easy to have
async messages in the user Monitor, we could add a new type of user print callback called async_print.

This new callback would be called by the Monitor when the async
message API is called but we are in user mode.

Quoted 8 lines
Quoted 2 lines

This is really today's user_print, but user data is printed
asynchronously.

There is one thing I am struggling with as I try to implement this asynchronous behavior. How can I suspend the monitor thread to wait for the stats update to come through? I see qemu_cond_timedwait(), but that appears to only be used for a VM's cpu threads. I also see the bottom half infrastructure, but I am not sure if that is appropriate either.

IIUC, in QMP you won't wait at all and I believe suspend/resume should
work on the user Monitor.

You can use monitor_ctrl_mode() to know whether you're in QMP or not.