From nobody Thu Nov 6 06:20:08 2025 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539688371510396.2024127841064; Tue, 16 Oct 2018 04:12:51 -0700 (PDT) Received: from localhost ([::1]:57266 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gCNH9-0001QD-Bg for importer@patchew.org; Tue, 16 Oct 2018 07:12:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36763) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gCNFI-0000X0-1R for qemu-devel@nongnu.org; Tue, 16 Oct 2018 07:10:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gCNFD-000314-Uf for qemu-devel@nongnu.org; Tue, 16 Oct 2018 07:10:38 -0400 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]:44471) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gCNFD-0002ln-En for qemu-devel@nongnu.org; Tue, 16 Oct 2018 07:10:35 -0400 Received: by mail-pg1-x541.google.com with SMTP id g2-v6so10685614pgu.11 for ; Tue, 16 Oct 2018 04:10:28 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.40]) by smtp.gmail.com with ESMTPSA id p24-v6sm18054927pgm.70.2018.10.16.04.10.23 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 16 Oct 2018 04:10:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=CxhcfrwFXA5qte1Agh1ZynNl8udg4bU7bkK+GQOcbrY=; b=GsW0rSDPBXajeebi6w3iRNw6jYjk5cuXrEGQJpP4H+kuUV5bD5gYhdtnqVjBR/vy9t 64wgZ65jsH1ZChYj5LeRnwXzX9m292aZxwxVtpGiGO/bcXnMN01P+k+SDcLcnrpJakz2 /z2sAOTNxFEkvCVwHF3DPibEviuYIMMUZPp5ug6B9jRpAfy1gN9NhbS9qoarLZLx/Yw7 rniCXNZOWunhUKBWr2VSDpRHtQfAK9pZjxGutqobcBVKFFnSgtXVzsheIXfp/zASlqNl 5NaDZxvReGou0zYvsPsCNaHWrA06Mu6GGy4vVVLbgjFLAtD7dK17vhh0HXxxKRYOk8Vp BC9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=CxhcfrwFXA5qte1Agh1ZynNl8udg4bU7bkK+GQOcbrY=; b=UXhQpx2tGqNVTC1KaNS1MiajftrfvejO7byKyV3zHbSH9mD+uyp7snSXmWAFroUiOh Y0Tn8bAl1clwP048n7j8IsEbvhe3bh3+hWUs/ycpukPveYCLSPwwp176JHPE1H/X/cXu IqKTQwmrtaIYz2Zdbj1VsLP23/fJZVMkG77zEb88yAPmXdUfnarz3TrGLNmApJmiKpQf vNTxOWZ3j4EANEjOU4rLVSZ6T8t5NteCZfrR5tVIPk+hYHQyBBXtvJ+xNyFtiwQAsr0o SfgpgHLtQq5Fx75rmgYACie2auY9cSWLT49QTDGvLAlVY2DfFXEbgXdeBONWru3K/TL4 JJJQ== X-Gm-Message-State: ABuFfohksK57yaQvw/ztyZJ6H5ibZXfXufQWqETn2MTXuovL2gnkKGYO 4Fm17aJtaJb8f+u7VJI66AA= X-Google-Smtp-Source: ACcGV63ux71vY6asKBcnub0BY80ur9U6E5+vqf7YV/enomkA0u/vp+X5uS5Znvvvd7aM3c04LFWWNw== X-Received: by 2002:a63:c14a:: with SMTP id p10-v6mr19790957pgi.424.1539688227427; Tue, 16 Oct 2018 04:10:27 -0700 (PDT) From: guangrong.xiao@gmail.com X-Google-Original-From: xiaoguangrong@tencent.com To: pbonzini@redhat.com, mst@redhat.com, mtosatti@redhat.com Date: Tue, 16 Oct 2018 19:10:05 +0800 Message-Id: <20181016111006.629-4-xiaoguangrong@tencent.com> X-Mailer: git-send-email 2.14.4 In-Reply-To: <20181016111006.629-1-xiaoguangrong@tencent.com> References: <20181016111006.629-1-xiaoguangrong@tencent.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::541 Subject: [Qemu-devel] [PATCH 3/4] migration: use lockless Multithread model for compression X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org, quintela@redhat.com, Xiao Guangrong , qemu-devel@nongnu.org, peterx@redhat.com, dgilbert@redhat.com, wei.w.wang@intel.com, jiang.biao2@zte.com.cn Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Xiao Guangrong Adapt the compression code to the lockless multithread model Signed-off-by: Xiao Guangrong --- migration/ram.c | 312 +++++++++++++++++++++-------------------------------= ---- 1 file changed, 115 insertions(+), 197 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index bc38d98cc3..2356bc255c 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -57,6 +57,7 @@ #include "qemu/uuid.h" #include "savevm.h" #include "qemu/iov.h" +#include "qemu/lockless-threads.h" =20 /***********************************************************/ /* ram save/restore */ @@ -349,22 +350,6 @@ typedef struct PageSearchStatus PageSearchStatus; =20 CompressionStats compression_counters; =20 -struct CompressParam { - bool done; - bool quit; - bool zero_page; - QEMUFile *file; - QemuMutex mutex; - QemuCond cond; - RAMBlock *block; - ram_addr_t offset; - - /* internally used fields */ - z_stream stream; - uint8_t *originbuf; -}; -typedef struct CompressParam CompressParam; - struct DecompressParam { bool done; bool quit; @@ -377,15 +362,6 @@ struct DecompressParam { }; typedef struct DecompressParam DecompressParam; =20 -static CompressParam *comp_param; -static QemuThread *compress_threads; -/* comp_done_cond is used to wake up the migration thread when - * one of the compression threads has finished the compression. - * comp_done_lock is used to co-work with comp_done_cond. - */ -static QemuMutex comp_done_lock; -static QemuCond comp_done_cond; -/* The empty QEMUFileOps will be used by file in CompressParam */ static const QEMUFileOps empty_ops =3D { }; =20 static QEMUFile *decomp_file; @@ -394,125 +370,6 @@ static QemuThread *decompress_threads; static QemuMutex decomp_done_lock; static QemuCond decomp_done_cond; =20 -static bool do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *= block, - ram_addr_t offset, uint8_t *source_buf); - -static void *do_data_compress(void *opaque) -{ - CompressParam *param =3D opaque; - RAMBlock *block; - ram_addr_t offset; - bool zero_page; - - qemu_mutex_lock(¶m->mutex); - while (!param->quit) { - if (param->block) { - block =3D param->block; - offset =3D param->offset; - param->block =3D NULL; - qemu_mutex_unlock(¶m->mutex); - - zero_page =3D do_compress_ram_page(param->file, ¶m->stream, - block, offset, param->originb= uf); - - qemu_mutex_lock(&comp_done_lock); - param->done =3D true; - param->zero_page =3D zero_page; - qemu_cond_signal(&comp_done_cond); - qemu_mutex_unlock(&comp_done_lock); - - qemu_mutex_lock(¶m->mutex); - } else { - qemu_cond_wait(¶m->cond, ¶m->mutex); - } - } - qemu_mutex_unlock(¶m->mutex); - - return NULL; -} - -static void compress_threads_save_cleanup(void) -{ - int i, thread_count; - - if (!migrate_use_compression() || !comp_param) { - return; - } - - thread_count =3D migrate_compress_threads(); - for (i =3D 0; i < thread_count; i++) { - /* - * we use it as a indicator which shows if the thread is - * properly init'd or not - */ - if (!comp_param[i].file) { - break; - } - - qemu_mutex_lock(&comp_param[i].mutex); - comp_param[i].quit =3D true; - qemu_cond_signal(&comp_param[i].cond); - qemu_mutex_unlock(&comp_param[i].mutex); - - qemu_thread_join(compress_threads + i); - qemu_mutex_destroy(&comp_param[i].mutex); - qemu_cond_destroy(&comp_param[i].cond); - deflateEnd(&comp_param[i].stream); - g_free(comp_param[i].originbuf); - qemu_fclose(comp_param[i].file); - comp_param[i].file =3D NULL; - } - qemu_mutex_destroy(&comp_done_lock); - qemu_cond_destroy(&comp_done_cond); - g_free(compress_threads); - g_free(comp_param); - compress_threads =3D NULL; - comp_param =3D NULL; -} - -static int compress_threads_save_setup(void) -{ - int i, thread_count; - - if (!migrate_use_compression()) { - return 0; - } - thread_count =3D migrate_compress_threads(); - compress_threads =3D g_new0(QemuThread, thread_count); - comp_param =3D g_new0(CompressParam, thread_count); - qemu_cond_init(&comp_done_cond); - qemu_mutex_init(&comp_done_lock); - for (i =3D 0; i < thread_count; i++) { - comp_param[i].originbuf =3D g_try_malloc(TARGET_PAGE_SIZE); - if (!comp_param[i].originbuf) { - goto exit; - } - - if (deflateInit(&comp_param[i].stream, - migrate_compress_level()) !=3D Z_OK) { - g_free(comp_param[i].originbuf); - goto exit; - } - - /* comp_param[i].file is just used as a dummy buffer to save data, - * set its ops to empty. - */ - comp_param[i].file =3D qemu_fopen_ops(NULL, &empty_ops); - comp_param[i].done =3D true; - comp_param[i].quit =3D false; - qemu_mutex_init(&comp_param[i].mutex); - qemu_cond_init(&comp_param[i].cond); - qemu_thread_create(compress_threads + i, "compress", - do_data_compress, comp_param + i, - QEMU_THREAD_JOINABLE); - } - return 0; - -exit: - compress_threads_save_cleanup(); - return -1; -} - /* Multiple fd's */ =20 #define MULTIFD_MAGIC 0x11223344U @@ -1909,12 +1766,27 @@ exit: return zero_page; } =20 +struct CompressData { + /* filled by migration thread.*/ + RAMBlock *block; + ram_addr_t offset; + + /* filled by compress thread. */ + QEMUFile *file; + z_stream stream; + uint8_t *originbuf; + bool zero_page; + + ThreadRequest request; +}; +typedef struct CompressData CompressData; + static void -update_compress_thread_counts(const CompressParam *param, int bytes_xmit) +update_compress_thread_counts(CompressData *cd, int bytes_xmit) { ram_counters.transferred +=3D bytes_xmit; =20 - if (param->zero_page) { + if (cd->zero_page) { ram_counters.duplicate++; return; } @@ -1924,81 +1796,127 @@ update_compress_thread_counts(const CompressParam = *param, int bytes_xmit) compression_counters.pages++; } =20 +static ThreadRequest *compress_thread_data_init(void) +{ + CompressData *cd =3D g_new0(CompressData, 1); + + cd->originbuf =3D g_try_malloc(TARGET_PAGE_SIZE); + if (!cd->originbuf) { + goto exit; + } + + if (deflateInit(&cd->stream, migrate_compress_level()) !=3D Z_OK) { + g_free(cd->originbuf); + goto exit; + } + + cd->file =3D qemu_fopen_ops(NULL, &empty_ops); + return &cd->request; + +exit: + g_free(cd); + return NULL; +} + +static void compress_thread_data_fini(ThreadRequest *request) +{ + CompressData *cd =3D container_of(request, CompressData, request); + + qemu_fclose(cd->file); + deflateEnd(&cd->stream); + g_free(cd->originbuf); + g_free(cd); +} + +static void compress_thread_data_handler(ThreadRequest *request) +{ + CompressData *cd =3D container_of(request, CompressData, request); + + /* + * if compression fails, it will be indicated by + * migrate_get_current()->to_dst_file. + */ + cd->zero_page =3D do_compress_ram_page(cd->file, &cd->stream, cd->bloc= k, + cd->offset, cd->originbuf); +} + +static void compress_thread_data_done(ThreadRequest *request) +{ + CompressData *cd =3D container_of(request, CompressData, request); + RAMState *rs =3D ram_state; + int bytes_xmit; + + bytes_xmit =3D qemu_put_qemu_file(rs->f, cd->file); + update_compress_thread_counts(cd, bytes_xmit); +} + +static Threads *compress_threads; + static bool save_page_use_compression(RAMState *rs); =20 static void flush_compressed_data(RAMState *rs) { - int idx, len, thread_count; - if (!save_page_use_compression(rs)) { return; } - thread_count =3D migrate_compress_threads(); =20 - qemu_mutex_lock(&comp_done_lock); - for (idx =3D 0; idx < thread_count; idx++) { - while (!comp_param[idx].done) { - qemu_cond_wait(&comp_done_cond, &comp_done_lock); - } - } - qemu_mutex_unlock(&comp_done_lock); + threads_wait_done(compress_threads); +} =20 - for (idx =3D 0; idx < thread_count; idx++) { - qemu_mutex_lock(&comp_param[idx].mutex); - if (!comp_param[idx].quit) { - len =3D qemu_put_qemu_file(rs->f, comp_param[idx].file); - /* - * it's safe to fetch zero_page without holding comp_done_lock - * as there is no further request submitted to the thread, - * i.e, the thread should be waiting for a request at this poi= nt. - */ - update_compress_thread_counts(&comp_param[idx], len); - } - qemu_mutex_unlock(&comp_param[idx].mutex); +static void compress_threads_save_cleanup(void) +{ + if (!compress_threads) { + return; } + + threads_destroy(compress_threads); + compress_threads =3D NULL; } =20 -static inline void set_compress_params(CompressParam *param, RAMBlock *blo= ck, - ram_addr_t offset) +static int compress_threads_save_setup(void) { - param->block =3D block; - param->offset =3D offset; + if (!migrate_use_compression()) { + return 0; + } + + compress_threads =3D threads_create(migrate_compress_threads(), + "compress", + DEFAULT_THREAD_RING_SIZE, + compress_thread_data_init, + compress_thread_data_fini, + compress_thread_data_handler, + compress_thread_data_done); + return compress_threads ? 0 : -1; } =20 static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block, ram_addr_t offset) { - int idx, thread_count, bytes_xmit =3D -1, pages =3D -1; + CompressData *cd; + ThreadRequest *request; bool wait =3D migrate_compress_wait_thread(); =20 - thread_count =3D migrate_compress_threads(); - qemu_mutex_lock(&comp_done_lock); retry: - for (idx =3D 0; idx < thread_count; idx++) { - if (comp_param[idx].done) { - comp_param[idx].done =3D false; - bytes_xmit =3D qemu_put_qemu_file(rs->f, comp_param[idx].file); - qemu_mutex_lock(&comp_param[idx].mutex); - set_compress_params(&comp_param[idx], block, offset); - qemu_cond_signal(&comp_param[idx].cond); - qemu_mutex_unlock(&comp_param[idx].mutex); - pages =3D 1; - update_compress_thread_counts(&comp_param[idx], bytes_xmit); - break; + request =3D threads_submit_request_prepare(compress_threads); + if (!request) { + /* + * wait for the free thread if the user specifies + * 'compress-wait-thread', otherwise we will post + * the page out in the main thread as normal page. + */ + if (wait) { + cpu_relax(); + goto retry; } - } =20 - /* - * wait for the free thread if the user specifies 'compress-wait-threa= d', - * otherwise we will post the page out in the main thread as normal pa= ge. - */ - if (pages < 0 && wait) { - qemu_cond_wait(&comp_done_cond, &comp_done_lock); - goto retry; - } - qemu_mutex_unlock(&comp_done_lock); + return -1; + } =20 - return pages; + cd =3D container_of(request, CompressData, request); + cd->block =3D block; + cd->offset =3D offset; + threads_submit_request_commit(compress_threads, request); + return 1; } =20 /** --=20 2.14.4