virtio-balloon: free page hint reporting support
v6 cover virtio-balloon: free page hint reporting support
This is the deivce part implementation to add a new feature, VIRTIO_BALLOON_F_FREE_PAGE_HINT to the virtio-balloon device. The device receives the guest free page hints from the driver and clears the corresponding bits in the dirty bitmap, so that those free pages are not transferred by the migration thread to the destination.
- Test Environment
Host: Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
Guest: 8G RAM, 4 vCPU
Migration setup: migrate_set_speed 100G, migrate_set_downtime 2 second
- Test Results
- Idle Guest Live Migration Time (results are averaged over 10 runs):
- Optimization v.s. Legacy = 271ms vs 1769ms --> ~86% reduction
- Guest with Linux Compilation Workload (make bzImage -j4):
- Live Migration Time (average)
Optimization v.s. Legacy = 1265ms v.s. 2634ms --> ~51% reduction
- Linux Compilation Time
Optimization v.s. Legacy = 4min56s v.s. 5min3s
--> no obvious difference
- Source Code
- QEMU: https://github.com/wei-w-wang/qemu-free-page-lm.git
- Linux: https://github.com/wei-w-wang/linux-free-page-lm.git
ChangeLog:v5->v6:
virtio-balloon:
- use an iothread to get free page hint;
- block the iothread when the vm is stoppedm and unblock it when it is woken up;
- remove FREE_PAGE_REPORT_S_EXIT which is not needed now.v4->v5:
1) migration:
- bitmap_clear_dirty: update the dirty bitmap and dirty page count under the bitmap mutex as what other functions are doing;
- qemu_guest_free_page_hint:
- add comments for this function;
- check the !block case;
- check "offset > block->used_length" before proceed;
- assign used_len inside the for{} body;
- update the dirty bitmap and dirty page counter under the bitmap mutex;
- ram_state_reset:
- rs->free_page_support: && with use "migrate_postcopy" instead of migration_in_postcopy;
- clear the ram_bulk_stage flag if free_page_support is true;
2) balloon:
- add the usage documentation of balloon_free_page_start and balloon_free_page_stop in code;
- the optimization thread is named "balloon_fpo" to meet the requirement of "less than 14 characters";
- virtio_balloon_poll_free_page_hints:
- run on condition when runstate_is_running() is true;
- add a qemu spin lock to synchronize accesses to the free page reporting related fields shared among the migration thread and the optimization thread;
- virtio_balloon_free_page_start: just return if runstate_is_running is false;
- virtio_balloon_free_page_stop: access to the free page reporting related fields under a qemu spin lock;
- virtio_balloon_device_unrealize/reset: call virtio_balloon_free_page_stop is the free page hint feature is used;
- virtio_balloon_set_status: call irtio_balloon_free_page_stop in case the guest is stopped by qmp when the optimization is running;v3->v4:
1) bitmap: add a new API to count 1s starting from an offset of a bitmap
2) migration:
- qemu_guest_free_page_hint: calculate ram_state->migration_dirty_pages by counting how many bits of free pages are truely cleared. If some of the bits were already 0, they shouldn't be deducted by ram_state->migration_dirty_pages. This wasn't needed for previous versions since we optimized bulk stage only, where all bits are guaranteed to be set. It's needed now because we extened the usage of this optimizaton to all stages except the last stop© stage. From 2nd stage onward, there are possibilities that some bits of free pages are already 0.
3) virtio-balloon:
- virtio_balloon_free_page_report_status: introduce a new status, FREE_PAGE_REPORT_S_EXIT. This status indicates that the optimization thread has exited. FREE_PAGE_REPORT_S_STOP means the reporting is stopped, but the optimization thread still needs to be joined by the migration thread.v2->v3:
1) virtio-balloon
- virtio_balloon_free_page_start: poll the hints using a new thread;
- use cmd id between [0x80000000, UINT_MAX];
- virtio_balloon_poll_free_page_hints:
- stop the optimization only when it has started;
- don't skip free pages when !poison_val;
- add poison_val to vmsd to migrate;
- virtio_balloon_get_features: add the F_PAGE_POISON feature when host has F_FREE_PAGE_HINT;
- remove the timer patch which is not needed now.
2) migration
- new api, qemu_guest_free_page_hint;
- rs->free_page_support set only in the precopy case;
- use the new balloon APIs.v1->v2:
1) virtio-balloon
- use subsections to save free_page_report_cmd_id;
- poll the free page vq after sending a cmd id to the driver;
- change the free page vq size to VIRTQUEUE_MAX_SIZE;
- virtio_balloon_poll_free_page_hints: handle the corner case that the free page block reported from the driver may cross the RAMBlock boundary.
2) migration/ram.c
- use balloon_free_page_poll to start the optimization
Wei Wang (5):
bitmap: bitmap_count_one_with_offset
migration: use bitmap_mutex in migration_bitmap_clear_dirty
migration: API to clear bits of guest free pages from the dirty bitmap
virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
migration: use the free page hint feature from balloon
balloon.c | 58 +++++- hw/virtio/virtio-balloon.c | 240 ++++++++++++++++++++++-- include/hw/virtio/virtio-balloon.h | 27 ++- include/migration/misc.h | 2 + include/qemu/bitmap.h | 13 ++ include/standard-headers/linux/virtio_balloon.h | 7 + include/sysemu/balloon.h | 15 +- migration/ram.c | 73 ++++++- 8 files changed, 405 insertions(+), 30 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v6 1/5 bitmap: bitmap_count_one_with_offset
Count the number of 1s in a bitmap starting from an offset.
--- include/qemu/bitmap.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/qemu/bitmap.h +13 -0
@@ -228,6 +228,19 @@ static inline long bitmap_count_one(const unsigned long *bitmap, long nbits) } } +static inline long bitmap_count_one_with_offset (const unsigned long *bitmap, +long offset,long nbits) +{ +long aligned_offset =QEMU_ALIGN_DOWN (offset,BITS_PER_LONG ); +long redundant_bits = offset - aligned_offset; +long bits_to_count = nbits + redundant_bits; +const unsigned long *bitmap_start = bitmap + + aligned_offset /BITS_PER_LONG ; + +return bitmap_count_one (bitmap_start, bits_to_count) - +bitmap_count_one (bitmap_start, redundant_bits); +} +void bitmap_set (unsigned long *map,long i,long len);void bitmap_set_atomic (unsigned long *map,long i,long len);void bitmap_clear (unsigned long *map,long start,long nr);
Signature · 1 line
1.8.3.1
PATCH v6 2/5 migration: use bitmap_mutex in migration_bitmap_clear_dirty
In reply to Wei WangThe bitmap mutex is used to synchronize threads to update the dirty bitmap and the migration_dirty_pages counter. This patch makes migration_bitmap_clear_dirty update the bitmap and counter under the mutex.
--- migration/ram.c | 3 +++ 1 file changed, 3 insertions(+)
migration/ram.c +3 -0
@@ -795,11 +795,14 @@ static inline bool migration_bitmap_clear_dirty(RAMState *rs, {bool ret; +qemu_mutex_lock (&rs->bitmap_mutex); ret =test_and_clear_bit (page, rb->bmap);if (ret) { rs->migration_dirty_pages--; } +qemu_mutex_unlock (&rs->bitmap_mutex); +return ret; }
Signature · 6 lines
1.8.3.1 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
PATCH v6 3/5 migration: API to clear bits of guest free pages from the dirty bitmap
In reply to Wei WangThis patch adds an API to clear bits corresponding to guest free pages from the dirty bitmap. Spilt the free page block if it crosses the QEMU RAMBlock boundary.
--- include/migration/misc.h | 2 ++ migration/ram.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+)
include/migration/misc.h +2 -0
@@ -14,11 +14,13 @@#ifndef MIGRATION_MISC_H #define MIGRATION_MISC_H +#include "exec/cpu-common.h" #include "qemu/notify.h" /* migration/ram.c */ void ram_mig_init (void ); +void qemu_guest_free_page_hint (void *addr,size_t len);/* migration/block.c */
migration/ram.c +44 -0
50 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v6 4/5 virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
In reply to Wei WangThe new feature enables the virtio-balloon device to receive hints of guest free pages from the free page vq.
balloon_free_page_start - start guest free page hint reporting. balloon_free_page_stop - stop guest free page hint reporting.
Note: balloon will report pages which were free at the time of this call. As the reporting happens asynchronously, dirty bit logging must be enabled before this call is made. Guest reporting must be disabled before the migration dirty bitmap is synchronized.
TODO:
- handle the case when page poisoning is in use
--- balloon.c | 58 +++++- hw/virtio/virtio-balloon.c | 240 ++++++++++++++++++++++-- include/hw/virtio/virtio-balloon.h | 27 ++- include/standard-headers/linux/virtio_balloon.h | 7 + include/sysemu/balloon.h | 15 +- 5 files changed, 318 insertions(+), 29 deletions(-)
balloon.c +48 -10
@@ -36,6 +36,9 @@static QEMUBalloonEvent *balloon_event_fn;static QEMUBalloonStatus *balloon_stat_fn; +static QEMUBalloonFreePageSupport *balloon_free_page_support_fn; +static QEMUBalloonFreePageStart *balloon_free_page_start_fn; +static QEMUBalloonFreePageStop *balloon_free_page_stop_fn;static void *balloon_opaque;static bool balloon_inhibited;
@@ -64,19 +67,51 @@ static bool have_balloon(Error **errp)return true; } -int qemu_add_balloon_handler (QEMUBalloonEvent *event_func, -QEMUBalloonStatus *stat_func,void *opaque) +bool balloon_free_page_support (void ) { -if (balloon_event_fn || balloon_stat_fn || balloon_opaque) { -/* We're already registered one balloon handler. How many can -* a guest really have? -*/ -return -1 ; +return balloon_free_page_support_fn && +balloon_free_page_support_fn (balloon_opaque); +} + +/* +* Balloon will report pages which were free at the time of this call. As the +* reporting happens asynchronously, dirty bit logging must be enabled before +* this call is made. +*/ +void balloon_free_page_start (void ) +{ +balloon_free_page_start_fn (balloon_opaque); +} + +/* +* Guest reporting must be disabled before the migration dirty bitmap is +* synchronized. +*/ +void balloon_free_page_stop (void ) +{ +balloon_free_page_stop_fn (balloon_opaque); +} + +void qemu_add_balloon_handler (QEMUBalloonEvent *event_fn, +QEMUBalloonStatus *stat_fn, +QEMUBalloonFreePageSupport *free_page_support_fn, +QEMUBalloonFreePageStart *free_page_start_fn, +QEMUBalloonFreePageStop *free_page_stop_fn, +void *opaque) +{ +if (balloon_event_fn || balloon_stat_fn || balloon_free_page_support_fn || + balloon_free_page_start_fn || balloon_free_page_stop_fn || + balloon_opaque) { +/* We already registered one balloon handler. */ +return ; } - balloon_event_fn = event_func; - balloon_stat_fn = stat_func; + + balloon_event_fn = event_fn; + balloon_stat_fn = stat_fn; + balloon_free_page_support_fn = free_page_support_fn; + balloon_free_page_start_fn = free_page_start_fn; + balloon_free_page_stop_fn = free_page_stop_fn; balloon_opaque = opaque; -return 0 ; }void qemu_remove_balloon_handler (void *opaque)
@@ -86,6 +121,9 @@ void qemu_remove_balloon_handler(void *opaque) } balloon_event_fn =NULL ; balloon_stat_fn =NULL ; + balloon_free_page_support_fn =NULL ; + balloon_free_page_start_fn =NULL ; + balloon_free_page_stop_fn =NULL ; balloon_opaque =NULL ; }
hw/virtio/virtio-balloon.c +224 -16
319 lines not on this page · raw message
include/hw/virtio/virtio-balloon.h +26 -1
@@ -18,11 +18,14 @@#include "standard-headers/linux/virtio_balloon.h" #include "hw/virtio/virtio.h" #include "hw/pci/pci.h" +#include "sysemu/iothread.h" #define TYPE_VIRTIO_BALLOON "virtio-balloon-device"#define VIRTIO_BALLOON (obj) \ OBJECT_CHECK(VirtIOBalloon, (obj), TYPE_VIRTIO_BALLOON) +#define VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN 0x80000000 +typedef struct virtio_balloon_stat VirtIOBalloonStat ; typedef struct virtio_balloon_stat_modern {
@@ -31,15 +34,37 @@ typedef struct virtio_balloon_stat_modern {uint64_t val; } VirtIOBalloonStatModern; +enum virtio_balloon_free_page_report_status { +FREE_PAGE_REPORT_S_STOP =0 , +FREE_PAGE_REPORT_S_REQUESTED =1 , +FREE_PAGE_REPORT_S_START =2 , +}; + typedef struct VirtIOBalloon { VirtIODevice parent_obj; - VirtQueue *ivq, *dvq, *svq; + VirtQueue *ivq, *dvq, *svq, *free_page_vq; + uint32_t free_page_report_status; uint32_t num_pages; uint32_t actual; + uint32_t free_page_report_cmd_id; + uint32_t poison_val; uint64_t stats[VIRTIO_BALLOON_S_NR]; VirtQueueElement *stats_vq_elem; size_t stats_vq_offset; QEMUTimer *stats_timer; + IOThread *iothread; + QEMUBH *free_page_bh; + /* + * Lock to synchronize threads to access the free page reporting related + * fields (e.g. free_page_report_status). + */ + QemuMutex free_page_lock; + QemuCond free_page_cond; + /* + * Set to block iothread to continue reading free page hints as the VM is + * stopped. + */ + bool block_iothread; int64_t stats_last_update; int64_t stats_poll_interval; uint32_t host_features;
include/standard-headers/linux/virtio_balloon.h +7 -0
@@ -34,15 +34,22 @@#define VIRTIO_BALLOON_F_MUST_TELL_HOST 0/* Tell before reclaiming pages */ #define VIRTIO_BALLOON_F_STATS_VQ 1/* Memory Stats virtqueue */ #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM 2/* Deflate balloon on OOM */ +#define VIRTIO_BALLOON_F_FREE_PAGE_HINT 3/* VQ to report free pages */ +#define VIRTIO_BALLOON_F_PAGE_POISON 4/* Guest is using page poisoning */ /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12 +#define VIRTIO_BALLOON_FREE_PAGE_REPORT_STOP_ID 0struct virtio_balloon_config {/* Number of pages host wants Guest to give up. */ uint32_t num_pages;/* Number of pages we've actually got in balloon. */ uint32_t actual; +/* Free page report command id, readonly by guest */ +uint32_t free_page_report_cmd_id; +/* Stores PAGE_POISON if page poisoning is in use */ +uint32_t poison_val; };#define VIRTIO_BALLOON_S_SWAP_IN 0/* Amount of memory swapped in */
include/sysemu/balloon.h +13 -2
@@ -18,11 +18,22 @@typedef void (QEMUBalloonEvent )(void *opaque,ram_addr_t target);typedef void (QEMUBalloonStatus )(void *opaque,BalloonInfo *info); +typedef bool (QEMUBalloonFreePageSupport )(void *opaque); +typedef void (QEMUBalloonFreePageStart )(void *opaque); +typedef void (QEMUBalloonFreePageStop )(void *opaque); -int qemu_add_balloon_handler (QEMUBalloonEvent *event_func, -QEMUBalloonStatus *stat_func,void *opaque);void qemu_remove_balloon_handler (void *opaque);bool qemu_balloon_is_inhibited (void );void qemu_balloon_inhibit (bool state); +bool balloon_free_page_support (void ); +void balloon_free_page_start (void ); +void balloon_free_page_stop (void ); + +void qemu_add_balloon_handler (QEMUBalloonEvent *event_fn, +QEMUBalloonStatus *stat_fn, +QEMUBalloonFreePageSupport *free_page_support_fn, +QEMUBalloonFreePageStart *free_page_start_fn, +QEMUBalloonFreePageStop *free_page_stop_fn, +void *opaque);#endif
Signature · 1 line
1.8.3.1
PATCH v6 5/5 migration: use the free page hint feature from balloon
In reply to Wei WangStart the free page optimization after the migration bitmap is synchronized. This can't be used in the stop© phase since the guest is paused. Make sure the guest reporting has stopped before synchronizing the migration dirty bitmap. Currently, the optimization is added to precopy only.
--- migration/ram.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
migration/ram.c +25 -1
@@ -51,6 +51,7 @@#include "qemu/rcu_queue.h" #include "migration/colo.h" #include "migration/block.h" +#include "sysemu/balloon.h" /***********************************************************/ /* ram save/restore */
@@ -213,6 +214,8 @@ struct RAMState {uint32_t last_version;/* We are in the first round */ bool ram_bulk_stage; +/* The free pages optimization feature is supported */ +bool free_page_support;/* How many times we have dirty too many pages */ int dirty_rate_high_cnt;/* these variables are used for bitmap sync */
@@ -841,6 +844,10 @@ static void migration_bitmap_sync(RAMState *rs)int64_t end_time;uint64_t bytes_xfer_now; +if (rs->free_page_support) { +balloon_free_page_stop (); + } + ram_counters.dirty_sync_count++;if (!rs->time_last_bitmap_sync) {
@@ -907,6 +914,10 @@ static void migration_bitmap_sync(RAMState *rs)if (migrate_use_events ()) {qapi_event_send_migration_pass (ram_counters.dirty_sync_count,NULL ); } + +if (rs->free_page_support) { +balloon_free_page_start (); + } }/**
@@ -1663,7 +1674,17 @@ static void ram_state_reset(RAMState *rs) rs->last_sent_block =NULL ; rs->last_page =0 ; rs->last_version = ram_list.version; - rs->ram_bulk_stage = true; + rs->free_page_support =balloon_free_page_support () && !migrate_postcopy (); +if (rs->free_page_support) { +/* +* When the free page optimization is used, not all the pages are +* treated as dirty pages (via migration_bitmap_find_dirty), which need +* to be sent. So disable ram_bulk_stage in this case. +*/ + rs->ram_bulk_stage = false; + }else { + rs->ram_bulk_stage = true; + } }#define MAX_WAIT 50/* ms, half buffered_file limit */
@@ -2369,6 +2390,9 @@ out: ret =qemu_file_get_error (f);if (ret <0 ) { +if (rs->free_page_support) { +balloon_free_page_stop (); + }return ret; }
Signature · 1 line
1.8.3.1