From patchwork Thu Apr 20 09:48:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukas Straub X-Patchwork-Id: 13218228 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 21129C77B73 for ; Thu, 20 Apr 2023 09:48:34 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ppQtq-00075v-SB; Thu, 20 Apr 2023 05:48:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ppQti-0006nL-4b for qemu-devel@nongnu.org; Thu, 20 Apr 2023 05:48:14 -0400 Received: from mout.web.de ([212.227.15.14]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ppQtg-00083A-DQ for qemu-devel@nongnu.org; Thu, 20 Apr 2023 05:48:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1681984088; i=lukasstraub2@web.de; bh=+fdw9q3h4EKpOAB+Z4itrIGw77JL1zPkXvsYNoHRZyM=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=XiUUrDdl9HOzgHAqAyOVwEUQv+nq0lsuCjE5f97PCX6rMfuEQC8cJ8ygcLtQuSWy4 j6NU7aMS0jcCSbpc1VlmMNjmTCw25J94KccH3pEioy3xLSq3u5nQTavlZU4QOC5UsN lkZ814h33Uuhy1TKzSe4d4EnuHwvjZcP2NhECK31vuoGxYXY/zu0xMFh4uR9azZit1 5GSboTpZQGTVV3kd6YDZsCq0NFAg2RtYpQd6YL1x4Xt8T6rtFtEY5g7QBZDlJXfl6o QWfKJ7Y0KBEHTJ4BpYlv0NyZ2tzj6vHm+0Ic9BoeEihkE8mQ7wsZPASR4A8XLjZk8t qFK80tJVmnz8g== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from gecko.fritz.box ([82.207.254.123]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1M604t-1pwJa84C4J-007I00; Thu, 20 Apr 2023 11:48:08 +0200 Date: Thu, 20 Apr 2023 11:48:06 +0200 From: Lukas Straub To: qemu-devel Cc: Juan Quintela , Peter Xu , Thomas Huth , Laurent Vivier , Paolo Bonzini Subject: [PATCH v2 05/13] ram.c: Do not call save_page_header() from compress threads Message-ID: <11f3eccee34665a352dd2917c5a662dab8ad528f.1681983401.git.lukasstraub2@web.de> In-Reply-To: References: MIME-Version: 1.0 X-Provags-ID: V03:K1:Jco2ACVW/KFDiJbCeHHxX+KeybfArKxTb4WTcfFhfDqEj0UjZ8u xnxIr0QkbCCIqk4HvjQ46TPcuHigd6dQarpJzks3ezmkXun9FsrtXMgACmCLLJIUA+K/M4R RY+xB0YHJqZKE/FeitxEdeLTfFdwkdrYUH4iFRpDkfBaw/Nf29oUrn/r6VuiR2sk/SP8PHN KdQXw3H8VcSt6G6FaPvGQ== UI-OutboundReport: notjunk:1;M01:P0:hrLN5Vi/40M=;NbZc95AqKigOq1hufRt+yPtqHZA qCJ41xDEnoHej1XCMwSesa4sDSWxZp87RPdLOHYV0H2HBZ3LCnerYYXohAGguGsOq28Y+lDVl oIAwdcYHlJSWYHjeEpGFaZjoV+AhCvTInVbyksUfFjaxqv77RkXkkTLIeLWWDS6WkoGffVTs+ nLdCi40Qr7iprHH7McT4Pm/yMyN6IqEZdIpzq9AWdnYtw1xamiqVsGbxlTqRZNRCUHcboEE4+ w8fBvCQCPZJhUsyMOkKKzkNTfX1kyu35xfbTVjNqjLF+cF0IzMCS37SF9zhjFECA1q/WQka3E YMRAj8AAi62FqRYVCzyqXaQ/GjI778zcpVi+6k1tXIJq6NL3LW1vvgi3jKtupn+ElJ/moekgw YndVSqi8JimcEgWOkO99EvQcswf8tJgofAm3ULSbCPPMpKgcYZoF3lkKvb0+Lz4pA3iBz0NcC f4quNjIVsH1kAm2dO3iuh3NBFVbu+y8kC/OtOs2y3SXXS8CxdcwgSuWDZgi5iXe/aYksMQXQS 32TuiWaE1x+xP110M6GnGx5RAIVKiLIVYNUB+HebrdCS/QwT34Vo4gvP6DoHhgW+hncDw7Cax /DFJDM86ZpCM8yuJR9tS1KK/hTIqUFF8DUhPl+yYVwDobQYJax9LpOy7QI0q/6KIZJS/dIQMk U5MZWxFnP1z0GRGJH3kMxNewkj6HdJgBa+hdryR0thwaUy86Hz2RXprnvGFPKQBMxO0xvLLc+ cpObjKhQ7kPZ0PGt3Ffe8AC0FXnCgHE8JoKKSHb9ho9RbtBO0RQSLhSSohbKlG6AcT5bpBOxc DvT/ud++fUr6b4CR2T451QqZ57FwmSpREs+r3VLNQF+RO5Xl+3gDJV1O/oypRsFYwDH8MT0I0 Z9Ij9pi1Sj6NBGxJd6yhFD0mRnPxb1jv7SrcT3+HSfARJSPjKINlIZibPd4T8/AE+PO3zPuen sVfV8mcHnHH6J44PYtHCWUh+Dfk= Received-SPF: pass client-ip=212.227.15.14; envelope-from=lukasstraub2@web.de; helo=mout.web.de X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org save_page_header() accesses several global variables, so calling it from multiple threads is pretty ugly. Instead, call save_page_header() before writing out the compressed data from the compress buffer to the migration stream. This also makes the core compress code more independend from ram.c. Signed-off-by: Lukas Straub Reviewed-by: Juan Quintela --- migration/ram.c | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) -- 2.40.0 diff --git a/migration/ram.c b/migration/ram.c index 5ca0f115cf..d248e1f062 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -1476,17 +1476,13 @@ static CompressResult do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *block, ram_addr_t offset, uint8_t *source_buf) { - RAMState *rs = ram_state; - PageSearchStatus *pss = &rs->pss[RAM_CHANNEL_PRECOPY]; uint8_t *p = block->host + offset; int ret; - if (save_zero_page_to_file(pss, f, block, offset)) { + if (buffer_is_zero(p, TARGET_PAGE_SIZE)) { return RES_ZEROPAGE; } - save_page_header(pss, f, block, offset | RAM_SAVE_FLAG_COMPRESS_PAGE); - /* * copy it to a internal buffer to avoid it being modified by VM * so that we can catch up the error during compression and @@ -1526,9 +1522,40 @@ static inline void compress_reset_result(CompressParam *param) param->offset = 0; } -static void flush_compressed_data(RAMState *rs) +static int send_queued_data(CompressParam *param) { + PageSearchStatus *pss = &ram_state->pss[RAM_CHANNEL_PRECOPY]; MigrationState *ms = migrate_get_current(); + QEMUFile *file = ms->to_dst_file; + int len = 0; + + RAMBlock *block = param->block; + ram_addr_t offset = param->offset; + + if (param->result == RES_NONE) { + return 0; + } + + assert(block == pss->last_sent_block); + + if (param->result == RES_ZEROPAGE) { + len += save_page_header(pss, file, block, offset | RAM_SAVE_FLAG_ZERO); + qemu_put_byte(file, 0); + len += 1; + ram_release_page(block->idstr, offset); + } else if (param->result == RES_COMPRESS) { + len += save_page_header(pss, file, block, + offset | RAM_SAVE_FLAG_COMPRESS_PAGE); + len += qemu_put_qemu_file(file, param->file); + } else { + abort(); + } + + return len; +} + +static void flush_compressed_data(RAMState *rs) +{ int idx, len, thread_count; if (!save_page_use_compression(rs)) { @@ -1548,7 +1575,7 @@ static void flush_compressed_data(RAMState *rs) qemu_mutex_lock(&comp_param[idx].mutex); if (!comp_param[idx].quit) { CompressParam *param = &comp_param[idx]; - len = qemu_put_qemu_file(ms->to_dst_file, param->file); + len = send_queued_data(param); compress_reset_result(param); /* @@ -1574,7 +1601,6 @@ static int compress_page_with_multi_thread(RAMBlock *block, ram_addr_t offset) { int idx, thread_count, bytes_xmit = -1, pages = -1; bool wait = migrate_compress_wait_thread(); - MigrationState *ms = migrate_get_current(); thread_count = migrate_compress_threads(); qemu_mutex_lock(&comp_done_lock); @@ -1584,7 +1610,7 @@ retry: CompressParam *param = &comp_param[idx]; qemu_mutex_lock(¶m->mutex); param->done = false; - bytes_xmit = qemu_put_qemu_file(ms->to_dst_file, param->file); + bytes_xmit = send_queued_data(param); compress_reset_result(param); set_compress_params(param, block, offset);