When the compression thread count is set to 1, the current implementation is inefficient because of the following reason: 1. Thread synchronization cost; 2. Data copy; 3. No benefit from the separate compression thread;
This patch optimizes the performance for the case of 1 compress thread. In this case, the compression is done in the migration thread, for some fast compression algorithm, it can help to improve the performance. Signed-off-by: Liang Li <[email protected]> --- migration/ram.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index 7f007e6..0cc4f81 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -318,8 +318,13 @@ void migrate_compress_threads_join(void) if (!migrate_use_compression()) { return; } - terminate_compression_threads(); + thread_count = migrate_compress_threads(); + if (thread_count == 1) { + return; + } + + terminate_compression_threads(); for (i = 0; i < thread_count; i++) { qemu_thread_join(compress_threads + i); qemu_fclose(comp_param[i].file); @@ -345,9 +350,12 @@ void migrate_compress_threads_create(void) if (!migrate_use_compression()) { return; } + thread_count = migrate_compress_threads(); + if (thread_count == 1) { + return; + } quit_comp_thread = false; compression_switch = true; - thread_count = migrate_compress_threads(); compress_threads = g_new0(QemuThread, thread_count); comp_param = g_new0(CompressParam, thread_count); comp_done_cond = g_new0(QemuCond, 1); @@ -782,6 +790,9 @@ static void flush_compressed_data(QEMUFile *f) return; } thread_count = migrate_compress_threads(); + if (thread_count == 1) { + return; + } for (idx = 0; idx < thread_count; idx++) { if (!comp_param[idx].done) { qemu_mutex_lock(comp_done_lock); @@ -883,18 +894,16 @@ static int ram_save_compressed_page(QEMUFile *f, RAMBlock *block, * out, keeping this order is important, because the 'cont' flag * is used to avoid resending the block name. */ - if (block != last_sent_block) { + if (block != last_sent_block || migrate_compress_threads() == 1) { flush_compressed_data(f); pages = save_zero_page(f, block, offset, p, bytes_transferred); if (pages == -1) { - set_compress_params(&comp_param[0], block, offset); - /* Use the qemu thread to compress the data to make sure the - * first page is sent out before other pages - */ - bytes_xmit = do_compress_ram_page(&comp_param[0]); - acct_info.norm_pages++; - qemu_put_qemu_file(f, comp_param[0].file); + bytes_xmit = save_page_header(f, block, offset | + RAM_SAVE_FLAG_COMPRESS_PAGE); + bytes_xmit += qemu_put_compression_data(f, p, TARGET_PAGE_SIZE, + migrate_compress_level()); *bytes_transferred += bytes_xmit; + acct_info.norm_pages++; pages = 1; } } else { -- 1.9.1
