From patchwork Thu May 5 07:32:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Liang Z" X-Patchwork-Id: 618860 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r0mxL6l1sz9ssM for ; Thu, 5 May 2016 17:38:46 +1000 (AEST) Received: from localhost ([::1]:52117 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ayDrt-0002vR-PO for incoming@patchwork.ozlabs.org; Thu, 05 May 2016 03:38:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58379) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ayDr5-00015j-HG for qemu-devel@nongnu.org; Thu, 05 May 2016 03:37:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ayDqk-0001Iw-IW for qemu-devel@nongnu.org; Thu, 05 May 2016 03:37:46 -0400 Received: from mga14.intel.com ([192.55.52.115]:61079) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ayDqk-0001Am-AE for qemu-devel@nongnu.org; Thu, 05 May 2016 03:37:30 -0400 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga103.fm.intel.com with ESMTP; 05 May 2016 00:37:07 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,581,1455004800"; d="scan'208";a="799336516" Received: from ll.sh.intel.com (HELO localhost) ([10.239.13.27]) by orsmga003.jf.intel.com with ESMTP; 05 May 2016 00:37:03 -0700 From: Liang Li To: qemu-devel@nongnu.org Date: Thu, 5 May 2016 15:32:58 +0800 Message-Id: <1462433579-13691-9-git-send-email-liang.z.li@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1462433579-13691-1-git-send-email-liang.z.li@intel.com> References: <1462433579-13691-1-git-send-email-liang.z.li@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.115 Subject: [Qemu-devel] [PATCH v2 8/9] migration: refine the decompression code 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: amit.shah@redhat.com, Liang Li , dgilbert@redhat.com, quintela@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" The current code for multi-thread decompression is not clear, especially in the aspect of using lock. Refine the code to make it clear. Signed-off-by: Liang Li --- migration/ram.c | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/migration/ram.c b/migration/ram.c index f44f833..51de958 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -262,7 +262,6 @@ struct CompressParam { typedef struct CompressParam CompressParam; struct DecompressParam { - bool start; bool done; bool quit; QemuMutex mutex; @@ -830,15 +829,6 @@ static int do_compress_ram_page(QEMUFile *f, RAMBlock *block, return bytes_sent; } -static inline void start_decompression(DecompressParam *param) -{ - param->done = false; - qemu_mutex_lock(¶m->mutex); - param->start = true; - qemu_cond_signal(¶m->cond); - qemu_mutex_unlock(¶m->mutex); -} - static uint64_t bytes_transferred; static void flush_compressed_data(QEMUFile *f) @@ -2195,30 +2185,37 @@ static void *do_data_decompress(void *opaque) { DecompressParam *param = opaque; unsigned long pagesize; + uint8_t *des; + int len; + qemu_mutex_lock(¶m->mutex); while (!param->quit) { - qemu_mutex_lock(¶m->mutex); - while (!param->start && !param->quit) { - qemu_cond_wait(¶m->cond, ¶m->mutex); - } - if (!param->quit) { + if (param->des) { + des = param->des; + len = param->len; + param->des = 0; + qemu_mutex_unlock(¶m->mutex); + pagesize = TARGET_PAGE_SIZE; /* uncompress() will return failed in some case, especially * when the page is dirted when doing the compression, it's * not a problem because the dirty page will be retransferred * and uncompress() won't break the data in other pages. */ - uncompress((Bytef *)param->des, &pagesize, - (const Bytef *)param->compbuf, param->len); - } - param->start = false; - qemu_mutex_unlock(¶m->mutex); + uncompress((Bytef *)des, &pagesize, + (const Bytef *)param->compbuf, len); - qemu_mutex_lock(&decomp_done_lock); - param->done = true; - qemu_cond_signal(&decomp_done_cond); - qemu_mutex_unlock(&decomp_done_lock); + qemu_mutex_lock(&decomp_done_lock); + param->done = true; + qemu_cond_signal(&decomp_done_cond); + qemu_mutex_unlock(&decomp_done_lock); + + qemu_mutex_lock(¶m->mutex); + } else { + qemu_cond_wait(¶m->cond, ¶m->mutex); + } } + qemu_mutex_unlock(¶m->mutex); return NULL; } @@ -2295,10 +2292,13 @@ static void decompress_data_with_multi_threads(QEMUFile *f, while (true) { for (idx = 0; idx < thread_count; idx++) { if (decomp_param[idx].done) { + decomp_param[idx].done = false; + qemu_mutex_lock(&decomp_param[idx].mutex); qemu_get_buffer(f, decomp_param[idx].compbuf, len); decomp_param[idx].des = host; decomp_param[idx].len = len; - start_decompression(&decomp_param[idx]); + qemu_cond_signal(&decomp_param[idx].cond); + qemu_mutex_unlock(&decomp_param[idx].mutex); break; } }