virtio: Add memory statistics reporting to the balloon driver (V7)
Subsystems (2)
This version improves support for multiple monitors and has been ported up to HEAD as of 01/14.
Changes since V6:
- Integrated with virtio qdev feature bit changes
(specifically: Use VirtIODevice 'guest_features' to check if memory stats is a negotiated feature)
- Track which monitor initiated the most recent stats request. Now it does the Right Thing(tm) with multiple monitors making parallel requests.
Changes since V5:
- Asynchronous query-balloon mode for QMP
- Add timeout to prevent hanging the user monitor in synchronous mode
Changes since V4:
- Virtio spec updated: http://ozlabs.org/~rusty/virtio-spec/virtio-spec-0.8.2.pdf
- Guest-side Linux implementation applied by Rusty
- Start using the QObject infrastructure
- All endian conversions done in the host
- Report stats that reference a quantity of memory in bytes
Changes since V3:
- Increase stat field size to 64 bits
- Report all sizes in kb (not pages)
- Drop anon_pages stat
Changes since V2:
- Use a virtqueue for communication instead of the device config space
Changes since V1:
- In the monitor, print all stats on one line with less abbreviated names
- Coding style changes
When using ballooning to manage overcommitted memory on a host, a system for guests to communicate their memory usage to the host can provide information that will minimize the impact of ballooning on the guests. The current method employs a daemon running in each guest that communicates memory statistics to a host daemon at a specified time interval. The host daemon aggregates this information and inflates and/or deflates balloons according to the level of host memory pressure. This approach is effective but overly complex since a daemon must be installed inside each guest and coordinated to communicate with the host. A simpler approach is to collect memory statistics in the virtio balloon driver and communicate them directly to the hypervisor.
To: Anthony Liguori <[email protected]>
Cc: Avi Kivity <[email protected]>
Cc: Luiz Capitulino <[email protected]>
Cc: [email protected]
balloon.h +6 -3
@@ -16,12 +16,15 @@#include "cpu-defs.h" -typedef ram_addr_t (QEMUBalloonEvent )(void *opaque,ram_addr_t target); +/* Timeout for synchronous stats requests (in seconds) */ +#define QEMU_BALLOON_SYNC_TIMEOUT 5 + +typedef void (QEMUBalloonEvent )(void *opaque,ram_addr_t target,Monitor *mon);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 ); +int qemu_balloon_status (Monitor *mon);#endif
hw/virtio-balloon.c +185 -4
250 lines not on this page · raw message
hw/virtio-balloon.h +15 -0
@@ -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 +54 -36
131 lines not on this page · raw message
monitor.h +4 -0
@@ -20,6 +20,7 @@ typedef enum MonitorEvent {QEVENT_RESET ,QEVENT_POWERDOWN ,QEVENT_STOP , +QEVENT_BALLOON ,QEVENT_MAX , } MonitorEvent;
@@ -32,6 +33,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);
@@ -41,4 +43,6 @@ void monitor_printf(Monitor *mon, const char *fmt, ...)void monitor_print_filename (Monitor *mon,const char *filename);void monitor_flush (Monitor *mon); +extern int monitor_ctrl_mode (const Monitor *mon); +#endif /* !MONITOR_H */
vl.c +14 -7
@@ -362,17 +362,24 @@ 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) -qemu_balloon_event (qemu_balloon_event_opaque, target); +if (qemu_balloon_event) { +qemu_balloon_event (qemu_balloon_event_opaque, target,NULL ); +return 1 ; + }else { +return 0 ; + } } -ram_addr_t qemu_balloon_status (void ) +int qemu_balloon_status (Monitor *mon) { -if (qemu_balloon_event) -return qemu_balloon_event (qemu_balloon_event_opaque,0 ); -return 0 ; +if (qemu_balloon_event) { +qemu_balloon_event (qemu_balloon_event_opaque,0 , mon); +return 1 ; + }else { +return 0 ; + } }/***********************************************************/
Signature · 2 lines
Thanks, Adam
Quoted 2 lines · On Fri, 15 Jan 2010 13:54:29 -0600 Adam Litke <[email protected]> wrote:
This version improves support for multiple monitors and has been ported up to HEAD as of 01/14.
Overall review on the Monitor related changes seems ok, but I'm not sure
how I should enable it.
Quoted 8 lines · Luiz Capitulino
Quoted 2 lines · On Fri, 15 Jan 2010 13:54:29 -0600 Adam Litke <[email protected]> wrote:
This version improves support for multiple monitors and has been ported up to HEAD as of 01/14.
Overall review on the Monitor related changes seems ok, but I'm not sure
how I should enable it.
You must run a Linux guest with the virtio balloon enabled (-balloon virtio) and the guest kernel must support the stats API. Rusty Russell has accepted my patch into his tree and will be pushing it upstream. In the meantime, you could build a 2.6.33-rc3 kernel with the following patch applied...
commit b9bb81c2f7ce82d636d47089d6aa279d9be704f2
Author: [email protected] <[email protected]> Date: Fri Jan 8 14:17:45 2010 -0800
balloon memory stats patch
drivers/virtio/virtio_balloon.c +100 -8
@@ -28,7 +28,7 @@struct virtio_balloon {struct virtio_device *vdev; -struct virtqueue *inflate_vq, *deflate_vq; +struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;/* Where the ballooning thread waits for config to change. */ wait_queue_head_t config_change;
@@ -49,6 +49,10 @@ struct virtio_balloon/* The array of pfns we tell the Host about. */ unsigned int num_pfns;u32 pfns[256 ]; + +/* Memory statistics */ +int need_stats_update; +struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR ]; };static struct virtio_device_id id_table[] = {
@@ -154,6 +158,72 @@ static void leak_balloon(struct virtio_balloon *vb, size_t num) } } +static inline void update_stat (struct virtio_balloon *vb,int idx, +u16 tag,u64 val) +{ +BUG_ON (idx >=VIRTIO_BALLOON_S_NR ); + vb->stats[idx].tag = tag; + vb->stats[idx].val = val; +} + +#define pages_to_bytes (x) ((u64)(x) << PAGE_SHIFT) + +static void update_balloon_stats (struct virtio_balloon *vb) +{ +unsigned long events[NR_VM_EVENT_ITEMS ]; +struct sysinfo i; +int idx =0 ; + +all_vm_events (events); +si_meminfo (&i); + +update_stat (vb, idx++,VIRTIO_BALLOON_S_SWAP_IN , +pages_to_bytes (events[PSWPIN ])); +update_stat (vb, idx++,VIRTIO_BALLOON_S_SWAP_OUT , +pages_to_bytes (events[PSWPOUT ])); +update_stat (vb, idx++,VIRTIO_BALLOON_S_MAJFLT , events[PGMAJFAULT ]); +update_stat (vb, idx++,VIRTIO_BALLOON_S_MINFLT , events[PGFAULT ]); +update_stat (vb, idx++,VIRTIO_BALLOON_S_MEMFREE , +pages_to_bytes (i.freeram)); +update_stat (vb, idx++,VIRTIO_BALLOON_S_MEMTOT , +pages_to_bytes (i.totalram)); +} + +/* +* While most virtqueues communicate guest-initiated requests to the hypervisor, +* the stats queue operates in reverse. The driver initializes the virtqueue +* with a single buffer. From that point forward, all conversations consist of +* a hypervisor request (a call to this function) which directs us to refill +* the virtqueue with a fresh stats buffer. Since stats collection can sleep, +* we notify our kthread which does the actual work via stats_handle_request(). +*/ +static void stats_request (struct virtqueue *vq) +{ +struct virtio_balloon *vb; +unsigned int len; + + vb = vq->vq_ops->get_buf (vq, &len); +if (!vb) +return ; + vb->need_stats_update =1 ; +wake_up (&vb->config_change); +} + +static void stats_handle_request (struct virtio_balloon *vb) +{ +struct virtqueue *vq; +struct scatterlist sg; + + vb->need_stats_update =0 ; +update_balloon_stats (vb); + + vq = vb->stats_vq; +sg_init_one (&sg, vb->stats,sizeof (vb->stats)); +if (vq->vq_ops->add_buf (vq, &sg,1 ,0 , vb) <0 ) +BUG (); + vq->vq_ops->kick (vq); +} +static void virtballoon_changed (struct virtio_device *vdev) {struct virtio_balloon *vb = vdev->priv;
@@ -190,8 +260,11 @@ static int balloon(void *_vballoon)try_to_freeze ();wait_event_interruptible (vb->config_change, (diff =towards_target (vb)) !=0 + || vb->need_stats_update ||kthread_should_stop () ||freezing (current)); +if (vb->need_stats_update) +stats_handle_request (vb);if (diff >0 )fill_balloon (vb, diff);else if (diff <0 )
@@ -204,10 +277,10 @@ static int balloon(void *_vballoon)static int virtballoon_probe (struct virtio_device *vdev) {struct virtio_balloon *vb; -struct virtqueue *vqs[2 ]; -vq_callback_t *callbacks[] = { balloon_ack, balloon_ack }; -const char *names[] = {"inflate" ,"deflate" }; -int err; +struct virtqueue *vqs[3 ]; +vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request }; +const char *names[] = {"inflate" ,"deflate" ,"stats" }; +int err, nvqs; vdev->priv = vb =kmalloc (sizeof (*vb),GFP_KERNEL );if (!vb) {
@@ -220,13 +293,29 @@ static int virtballoon_probe(struct virtio_device *vdev)init_waitqueue_head (&vb->config_change); vb->vdev = vdev; -/* We expect two virtqueues. */ - err = vdev->config->find_vqs (vdev,2 , vqs, callbacks, names); +/* We expect two virtqueues: inflate and deflate, +* and optionally stat. */ + nvqs =virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_STATS_VQ ) ?3 :2 ; + err = vdev->config->find_vqs (vdev, nvqs, vqs, callbacks, names);if (err) goto out_free_vb; vb->inflate_vq = vqs[0 ]; vb->deflate_vq = vqs[1 ]; +if (virtio_has_feature (vb->vdev,VIRTIO_BALLOON_F_STATS_VQ )) { +struct scatterlist sg; + vb->stats_vq = vqs[2 ]; + +/* +* Prime this virtqueue with one buffer so the hypervisor can +* use it to signal us later. +*/ +sg_init_one (&sg, vb->stats,sizeof vb->stats); +if (vb->stats_vq->vq_ops->add_buf (vb->stats_vq, + &sg,1 ,0 , vb) <0 ) +BUG (); + vb->stats_vq->vq_ops->kick (vb->stats_vq); + } vb->thread =kthread_run (balloon, vb,"vballoon" );if (IS_ERR (vb->thread)) {
@@ -264,7 +353,10 @@ static void __devexit virtballoon_remove(struct virtio_device *vdev)kfree (vb); } -static unsigned int features[] = {VIRTIO_BALLOON_F_MUST_TELL_HOST }; +static unsigned int features[] = { +VIRTIO_BALLOON_F_MUST_TELL_HOST , +VIRTIO_BALLOON_F_STATS_VQ , +};static struct virtio_driver virtio_balloon = { .feature_table = features,
include/linux/virtio_balloon.h +15 -0
@@ -7,6 +7,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
@@ -18,4 +19,18 @@ struct virtio_balloon_config/* Number of pages we've actually got in balloon. */ __le32 actual; }; + +#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 + +struct virtio_balloon_stat { +u16 tag; +u64 val; +} __attribute__((packed)); +#endif /* _LINUX_VIRTIO_BALLOON_H */
Signature · 2 lines
Thanks, Adam