From nobody Sat May 18 08:35:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1666673419; cv=none; d=zohomail.com; s=zohoarc; b=PUooWdYIYhpc4RRaN/sdQyNZkZodC6NOxl9KFc8WxXpgLDMVzqZ5/qn8IxZg4pwjVxJ4YPm2X9/0N48jejLNgv9xqt9rJTLrZE18tYFYl3cjlCmyOWRNnN3wEy9hGqJH0wY6w1nJJwugCUJezW7JmPInafkzjInPyDX+EH0om2g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666673419; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=voAYcUr5pMqpaT7c3wMSt6rgcPe6U+ek+ImZMgGYY2E=; b=JoZR5eJ31XvEdbUXyKjOFsksNoPjTOOJRfrwMMpKFyvHb4olndxzyM62fLcUNgdo3A0px+TZyxPxAxLEEEiqPh0IwqvYLkKE/tDFKwLc+IFeX/5bodFtb742mHFFT/oy8kZhRDPNOcX0CAa/GiNKxexPXLhRCF5mHa7KAcxKBxA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16666734195471019.5438323108023; Mon, 24 Oct 2022 21:50:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1onBsR-0005gY-S3; Tue, 25 Oct 2022 00:49:23 -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 1onBsP-0005g6-NI for qemu-devel@nongnu.org; Tue, 25 Oct 2022 00:49:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1onBsN-0002tg-4p for qemu-devel@nongnu.org; Tue, 25 Oct 2022 00:49:20 -0400 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-94-vsjwTvffOEeVJzC_qQotCQ-1; Tue, 25 Oct 2022 00:47:52 -0400 Received: by mail-qv1-f69.google.com with SMTP id jn13-20020ad45ded000000b004b1d055fbc7so6361649qvb.2 for ; Mon, 24 Oct 2022 21:47:51 -0700 (PDT) Received: from LeoBras.redhat.com ([2804:1b3:a801:de5e:6447:4a67:eb7c:b690]) by smtp.gmail.com with ESMTPSA id d23-20020a05620a141700b006cbcdc6efedsm1350968qkj.41.2022.10.24.21.47.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Oct 2022 21:47:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666673358; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=voAYcUr5pMqpaT7c3wMSt6rgcPe6U+ek+ImZMgGYY2E=; b=WoaVnlDopyR0FQP90VnytFo7u2c55+NKPJKn/cJteUyp/2K3/t9ZH35P3DnheqGpfEXYCK xz6e8TSBCapk7urKvjiseZ8ar3uPQx09Xjbu1Ju+JkPFFmJFvF1vYYCzbOWBslorBiUYEF 2QP0Kwbatwt7u1AWLCAWOfRc6SPfEXs= X-MC-Unique: vsjwTvffOEeVJzC_qQotCQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=voAYcUr5pMqpaT7c3wMSt6rgcPe6U+ek+ImZMgGYY2E=; b=364A56aLZuDZi4oBCp4/nFDfJIoDgkIgUdBNjb2vvIFiwli4oQ6eM894REPK29yxZj p7G3x+Dip7fe6uhaNpIjTDsgKat69VRubxW/8C3wp6w7xGCz+qR1hWsDzKakqcmlE/b4 3K9++qc4qREs3E5vbaa8B4i2gr5HQF2ekTb5evXDDm97OK8cFKdSw1npvqCJJSXTamVj t32vBCQSoZvHBuzGII4FsW859mGmERNoka7Bynjok7ZNx7cnEeCeCbLhKBiVQgU9iO21 FUE6OaxSzf5Qaccdt+lYkkR4XQFhGPiArnZZOYZahuWqpFfTnld4jBx4qwNs8UBEFg82 21Nw== X-Gm-Message-State: ACrzQf1qRr/8s5hVluNQPhS5ucLO1pP4HxecTNVqZtu5B7wO27LXy9UA fFFu8XVrTKeijO6JyWWm6XFyMM7wk/vkLrl6YqhId9Xu7N2L9YSAATKYH0GCKm7eAhGCHjZytEG k+2UZfJjZ56jdg5M= X-Received: by 2002:a0c:b295:0:b0:4ba:b9b4:5159 with SMTP id r21-20020a0cb295000000b004bab9b45159mr17596102qve.19.1666673271556; Mon, 24 Oct 2022 21:47:51 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6ek3+fDYVl7qUMeg3dhrr83RDNn0rO/xQjI4hpj3KSP4JujHVsqMdB1YbU1p9+NIAV5Awoyg== X-Received: by 2002:a0c:b295:0:b0:4ba:b9b4:5159 with SMTP id r21-20020a0cb295000000b004bab9b45159mr17596096qve.19.1666673271393; Mon, 24 Oct 2022 21:47:51 -0700 (PDT) From: Leonardo Bras To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu Cc: Leonardo Bras , qemu-devel@nongnu.org Subject: [RFC PATCH 1/4] migration/multifd/zero-copy: Create helper function for flushing Date: Tue, 25 Oct 2022 01:47:28 -0300 Message-Id: <20221025044730.319941-2-leobras@redhat.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221025044730.319941-1-leobras@redhat.com> References: <20221025044730.319941-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=leobras@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, DKIMWL_WL_HIGH=-0.503, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1666673420879100001 Content-Type: text/plain; charset="utf-8" Move flushing code from multifd_send_sync_main() to a new helper, and call it in multifd_send_sync_main(). Signed-off-by: Leonardo Bras Reviewed-by: Juan Quintela --- migration/multifd.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/migration/multifd.c b/migration/multifd.c index 586ddc9d65..509bbbe3bf 100644 --- a/migration/multifd.c +++ b/migration/multifd.c @@ -566,6 +566,23 @@ void multifd_save_cleanup(void) multifd_send_state =3D NULL; } =20 +static int multifd_zero_copy_flush(QIOChannel *c) +{ + int ret; + Error *err =3D NULL; + + ret =3D qio_channel_flush(c, &err); + if (ret < 0) { + error_report_err(err); + return -1; + } + if (ret =3D=3D 1) { + dirty_sync_missed_zero_copy(); + } + + return ret; +} + int multifd_send_sync_main(QEMUFile *f) { int i; @@ -616,17 +633,8 @@ int multifd_send_sync_main(QEMUFile *f) qemu_mutex_unlock(&p->mutex); qemu_sem_post(&p->sem); =20 - if (flush_zero_copy && p->c) { - int ret; - Error *err =3D NULL; - - ret =3D qio_channel_flush(p->c, &err); - if (ret < 0) { - error_report_err(err); - return -1; - } else if (ret =3D=3D 1) { - dirty_sync_missed_zero_copy(); - } + if (flush_zero_copy && p->c && (multifd_zero_copy_flush(p->c) < 0)= ) { + return -1; } } for (i =3D 0; i < migrate_multifd_channels(); i++) { --=20 2.38.0 From nobody Sat May 18 08:35:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1666673525; cv=none; d=zohomail.com; s=zohoarc; b=g39UF6ceGuXCWM29JndVOxk6q0ovE5l15gvYgef5JbU0GpY3CLYgwMdhS5eQaMwHxETq8o0udm3kMw3mLJBxQhTMs8y/228yeXU6x9bBTyE8vyujEMWsXgqHLlG3hKpfwzOrGWbo9giWfSyt9lBaUZs4tQBtaJcz6ZsPsvxTxJc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666673525; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ShYCbEPXlE5IcxeK+9rwcqdlJwAqQqfQitYWqDLUAoA=; b=d+UHXoy1np8MLVhC0GQ/Vq6AKorT9xMyRrUkPkciK5/QyhEd+NxamsHB3Ek9W+WzRpLpqs9+6GGY0Gbe2vCunJghK5ZROQZek+d19uIvJOjMJTV8dB0MHUMut64R0/XYgUBpiQ1AGPh+aSgaOQ7xX9ILb8EGjC/FQuNWe77OAPc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666673525284236.36580894540361; Mon, 24 Oct 2022 21:52:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1onBsJ-0005fF-9C; Tue, 25 Oct 2022 00:49:15 -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 1onBsG-0005eP-JD for qemu-devel@nongnu.org; Tue, 25 Oct 2022 00:49:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1onBsE-0002s3-EW for qemu-devel@nongnu.org; Tue, 25 Oct 2022 00:49:12 -0400 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-443-bPBts3VzMxqAp5_zFr4iZg-1; Tue, 25 Oct 2022 00:47:53 -0400 Received: by mail-qt1-f200.google.com with SMTP id cb19-20020a05622a1f9300b0039cc64d84edso8337134qtb.15 for ; Mon, 24 Oct 2022 21:47:53 -0700 (PDT) Received: from LeoBras.redhat.com ([2804:1b3:a801:de5e:6447:4a67:eb7c:b690]) by smtp.gmail.com with ESMTPSA id d23-20020a05620a141700b006cbcdc6efedsm1350968qkj.41.2022.10.24.21.47.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Oct 2022 21:47:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666673349; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ShYCbEPXlE5IcxeK+9rwcqdlJwAqQqfQitYWqDLUAoA=; b=QH4tsPqtFlMuEvg2Q91DC4JEM9XMVaojzjSEnK4fJKf6Zg+OcJcWHxTL/kLpx7s5+AupcN RTokIANDH5cBpQOjicAM/u+nDIkHsNbZuf0yf9sZZz0TxBpp+3il+LdUeby52Wzdr+aWi2 D+1sUG3L7kwNuMg9lCxveXWzzAsfsC8= X-MC-Unique: bPBts3VzMxqAp5_zFr4iZg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ShYCbEPXlE5IcxeK+9rwcqdlJwAqQqfQitYWqDLUAoA=; b=tZZ0++vmWlxW0S5UBXppp7cEmXOsFMWu6cTC26h0hkjdVsWPLY22x805j6DTczME9s NnuWyfzPgARCa+EbVXYWuuIgdHKs0dQB86nHRoYSUpwBU+5R32OBHuAaBrzqiBxdHnAU /3dHymNNSn8IElSagkCiooZv/t+fs88Fo2Bd24IV9uhbkLYTdMjuSUbX8NKR/55TYCyT F17Ue+73dC6ofyEKnk2BiFPU0kxctkfr4cybgWmoo+rxG4XTmtGR64uAJn7EpjcMzPDN ka6GkkiIqelrl3pwRarEnH2uSVXGcrMuB0hJIOL+1bbij/f3iatXY2vQrTCLt7TEULyw 7F6w== X-Gm-Message-State: ACrzQf3WA0LCNfcGYnsRMyzXrZnleu/HOWhLSguwOuoeEI1MKE2TdrOZ E7IT5Tsh+AohdTrfoOTmjC9YbS4yULxEysXfGZ5bWG11FJYQl9lHqzDRuLhMKQtw9eIsyeZyv1s aQ+H9YNQqmvCxclE= X-Received: by 2002:a05:620a:2496:b0:6ee:76ce:4b3e with SMTP id i22-20020a05620a249600b006ee76ce4b3emr25427921qkn.370.1666673273371; Mon, 24 Oct 2022 21:47:53 -0700 (PDT) X-Google-Smtp-Source: AMsMyM62NlvKwNQGH+HIWebXckLNC9TfVyadiRFTJ4yyahZa0ak2o7tX2qpQy+2guswsIJe8xOPUxg== X-Received: by 2002:a05:620a:2496:b0:6ee:76ce:4b3e with SMTP id i22-20020a05620a249600b006ee76ce4b3emr25427916qkn.370.1666673273141; Mon, 24 Oct 2022 21:47:53 -0700 (PDT) From: Leonardo Bras To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu Cc: Leonardo Bras , qemu-devel@nongnu.org Subject: [RFC PATCH 2/4] migration/multifd/zero-copy: Merge header & pages send in a single write Date: Tue, 25 Oct 2022 01:47:29 -0300 Message-Id: <20221025044730.319941-3-leobras@redhat.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221025044730.319941-1-leobras@redhat.com> References: <20221025044730.319941-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=leobras@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, DKIMWL_WL_HIGH=-0.503, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1666673527318100003 Content-Type: text/plain; charset="utf-8" When zero-copy-send is enabled, each loop iteration of the multifd_send_thread will calls for qio_channel_write_*() twice: The first one for sending the header without zero-copy flag and the second one for sending the memory pages, with zero-copy flag enabled. This ends up calling two syscalls per loop iteration, where one should be enough. Also, since the behavior for non-zero-copy write is synchronous, and the behavior for zero-copy write is asynchronous, it ends up interleaving synchronous and asynchronous writes, hurting performance that could otherwise be improved. The choice of sending the header without the zero-copy flag in a separated write happened because the header memory area would be reused in the next write, so it was almost certain to have changed before the kernel could send the packet. To send the packet with zero-copy, create an array of header area instead of a single one, and use a different header area after each write. Also, flush the sending queue after all the headers have been used. To avoid adding a zero-copy conditional in multifd_send_fill_packet(), add a packet parameter (the packet that should be filled). This way it's simpler to pick which element of the array will be used as a header. Suggested-by: Juan Quintela Signed-off-by: Leonardo Bras --- migration/multifd.h | 5 ++++- migration/multifd.c | 52 ++++++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/migration/multifd.h b/migration/multifd.h index 519f498643..7f200c0286 100644 --- a/migration/multifd.h +++ b/migration/multifd.h @@ -90,6 +90,7 @@ typedef struct { =20 /* this mutex protects the following parameters */ QemuMutex mutex; + /* is this channel thread running */ bool running; /* should this thread finish */ @@ -109,8 +110,10 @@ typedef struct { =20 /* thread local variables. No locking required */ =20 - /* pointer to the packet */ + /* pointer to the packet array */ MultiFDPacket_t *packet; + /* index of the packet array */ + uint32_t packet_idx; /* size of the next packet that contains pages */ uint32_t next_packet_size; /* packets sent through this channel */ diff --git a/migration/multifd.c b/migration/multifd.c index 509bbbe3bf..aa18c47141 100644 --- a/migration/multifd.c +++ b/migration/multifd.c @@ -34,6 +34,8 @@ #define MULTIFD_MAGIC 0x11223344U #define MULTIFD_VERSION 1 =20 +#define HEADER_ARR_SZ 1024 + typedef struct { uint32_t magic; uint32_t version; @@ -255,9 +257,9 @@ static void multifd_pages_clear(MultiFDPages_t *pages) g_free(pages); } =20 -static void multifd_send_fill_packet(MultiFDSendParams *p) +static void multifd_send_fill_packet(MultiFDSendParams *p, + MultiFDPacket_t *packet) { - MultiFDPacket_t *packet =3D p->packet; int i; =20 packet->flags =3D cpu_to_be32(p->flags); @@ -547,6 +549,7 @@ void multifd_save_cleanup(void) p->packet_len =3D 0; g_free(p->packet); p->packet =3D NULL; + p->packet_idx =3D 0; g_free(p->iov); p->iov =3D NULL; g_free(p->normal); @@ -651,6 +654,7 @@ int multifd_send_sync_main(QEMUFile *f) static void *multifd_send_thread(void *opaque) { MultiFDSendParams *p =3D opaque; + MultiFDPacket_t *header =3D p->packet; Error *local_err =3D NULL; int ret =3D 0; bool use_zero_copy_send =3D migrate_use_zero_copy_send(); @@ -676,13 +680,9 @@ static void *multifd_send_thread(void *opaque) if (p->pending_job) { uint64_t packet_num =3D p->packet_num; uint32_t flags =3D p->flags; - p->normal_num =3D 0; =20 - if (use_zero_copy_send) { - p->iovs_num =3D 0; - } else { - p->iovs_num =3D 1; - } + p->normal_num =3D 0; + p->iovs_num =3D 1; =20 for (int i =3D 0; i < p->pages->num; i++) { p->normal[p->normal_num] =3D p->pages->offset[i]; @@ -696,7 +696,8 @@ static void *multifd_send_thread(void *opaque) break; } } - multifd_send_fill_packet(p); + + multifd_send_fill_packet(p, header); p->flags =3D 0; p->num_packets++; p->total_normal_pages +=3D p->normal_num; @@ -707,18 +708,8 @@ static void *multifd_send_thread(void *opaque) trace_multifd_send(p->id, packet_num, p->normal_num, flags, p->next_packet_size); =20 - if (use_zero_copy_send) { - /* Send header first, without zerocopy */ - ret =3D qio_channel_write_all(p->c, (void *)p->packet, - p->packet_len, &local_err); - if (ret !=3D 0) { - break; - } - } else { - /* Send header using the same writev call */ - p->iov[0].iov_len =3D p->packet_len; - p->iov[0].iov_base =3D p->packet; - } + p->iov[0].iov_len =3D p->packet_len; + p->iov[0].iov_base =3D header; =20 ret =3D qio_channel_writev_full_all(p->c, p->iov, p->iovs_num,= NULL, 0, p->write_flags, &local_er= r); @@ -726,6 +717,14 @@ static void *multifd_send_thread(void *opaque) break; } =20 + if (use_zero_copy_send) { + p->packet_idx =3D (p->packet_idx + 1) % HEADER_ARR_SZ; + + if (!p->packet_idx && (multifd_zero_copy_flush(p->c) < 0))= { + break; + } + header =3D (void *)p->packet + p->packet_idx * p->packet_l= en; + } qemu_mutex_lock(&p->mutex); p->pending_job--; qemu_mutex_unlock(&p->mutex); @@ -930,6 +929,7 @@ int multifd_save_setup(Error **errp) =20 for (i =3D 0; i < thread_count; i++) { MultiFDSendParams *p =3D &multifd_send_state->params[i]; + int j; =20 qemu_mutex_init(&p->mutex); qemu_sem_init(&p->sem, 0); @@ -940,9 +940,13 @@ int multifd_save_setup(Error **errp) p->pages =3D multifd_pages_init(page_count); p->packet_len =3D sizeof(MultiFDPacket_t) + sizeof(uint64_t) * page_count; - p->packet =3D g_malloc0(p->packet_len); - p->packet->magic =3D cpu_to_be32(MULTIFD_MAGIC); - p->packet->version =3D cpu_to_be32(MULTIFD_VERSION); + p->packet =3D g_malloc0_n(HEADER_ARR_SZ, p->packet_len); + for (j =3D 0; j < HEADER_ARR_SZ ; j++) { + MultiFDPacket_t *packet =3D (void *)p->packet + j * p->packet_= len; + packet->magic =3D cpu_to_be32(MULTIFD_MAGIC); + packet->version =3D cpu_to_be32(MULTIFD_VERSION); + } + p->packet_idx =3D 0; p->name =3D g_strdup_printf("multifdsend_%d", i); /* We need one extra place for the packet header */ p->iov =3D g_new0(struct iovec, page_count + 1); --=20 2.38.0 From nobody Sat May 18 08:35:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1666673492; cv=none; d=zohomail.com; s=zohoarc; b=PbCAA0zMszReypJBc1a2/3NpagXHDPDyUFPPrkwCv/LU8SfFbKR3LmtLR00vBPy7rWAVTFghM3yJ4j+Y7Fs6zaAzpAmQ3jcZWwsmiAWZqCy7AV3LAi9cn8ZE9kOi2JBtT6bcfBM6CVfSnJfWBHiHWOB2q1myfca6qBXYsEojK0Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666673492; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7iPjMq/JvcMVsM+ntRdGNw+Hdegb7Umm62J5du1TI0k=; b=jC4aheg0Ax+o9LXKpfSCG9yUXUjHkvYCL9H79DTVTKXOrOaacFHYcRhfh7q1iF7BDaoynFosXTV5eXNFked+9hNXLvWKHsKW5YzHmEXII+BCXXcx1jjLDpB+Wzre2T6WcprAqk0ZGOvpcLXyYti1WOAX6Pfo2mOeI7Ymf308U44= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666673492380720.1830651430018; Mon, 24 Oct 2022 21:51:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1onBsI-0005ez-Hz; Tue, 25 Oct 2022 00:49:14 -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 1onBsG-0005eN-IK for qemu-devel@nongnu.org; Tue, 25 Oct 2022 00:49:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1onBsE-0002s5-Hk for qemu-devel@nongnu.org; Tue, 25 Oct 2022 00:49:12 -0400 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-665-NGh_VbVSPpekpVwBwqy7Gw-1; Tue, 25 Oct 2022 00:47:55 -0400 Received: by mail-qv1-f69.google.com with SMTP id ln3-20020a0562145a8300b004b8c29a7d50so6413517qvb.15 for ; Mon, 24 Oct 2022 21:47:55 -0700 (PDT) Received: from LeoBras.redhat.com ([2804:1b3:a801:de5e:6447:4a67:eb7c:b690]) by smtp.gmail.com with ESMTPSA id d23-20020a05620a141700b006cbcdc6efedsm1350968qkj.41.2022.10.24.21.47.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Oct 2022 21:47:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666673349; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7iPjMq/JvcMVsM+ntRdGNw+Hdegb7Umm62J5du1TI0k=; b=XNMHB5kdmZAptXgqxYb4jsWYeKslSylFLva8LADD9DMZ651DDUZjirpcSckCI3EDpNIT57 VxnYc2SD60/jqgfLQbNQJsUt6BYRLRqaE4JetAgpb/c8EWRW2hcNQ81qyI/yfUttEw91xL AbhNhCQwGU8kjaY78/wO4DBGYQMO7aE= X-MC-Unique: NGh_VbVSPpekpVwBwqy7Gw-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7iPjMq/JvcMVsM+ntRdGNw+Hdegb7Umm62J5du1TI0k=; b=SKBWt9jD/O/zg1xDyjBUykjFgbxDIeBr2jql20e1kGX4SuXQtWBJJBFq7WQSOnY+DP AAa2UxeEeVfEz9R26XULv0gTIa101/P9RFgrcQB1NnFUnJVBaa7QK8+4+5OzKYqXg+rJ zdLaIR77Qy9md5ytppcVvZnCivKV6dO7ptJ0P3e9g/U3mewJV/0czYODwDl790Ct+9Zp ysinF+w+yCq2RaIlJrXW41dd2Gs/jcZsuN9yq673v+EjniU+XwR/6GifDG5wVpTtq+yY cEiklAjLFRqt4kZ/y+ygxN4mdUWuHrNbLw4kpbqLj1Vxng2zWqoC8+mgXJS5IKEBGwso mHxw== X-Gm-Message-State: ACrzQf3RfYGRCNsejSdczvtGFDUG7KaODSHmM83isqnSKRwm2uNyJtoZ xWnmi54IsuvkzKtDJDx6LCwRCQJind2ggYbvrok8x5TJGFHBi5qw+oU9AmkyLNZtSAMpBqayWHM 5LmoUkYSg4PHlxi8= X-Received: by 2002:ac8:7d8c:0:b0:39c:f4b6:f02f with SMTP id c12-20020ac87d8c000000b0039cf4b6f02fmr29582523qtd.252.1666673275081; Mon, 24 Oct 2022 21:47:55 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7OTW5TYwa9+KPcJWFVJfNUZ31pDFhYVOLaQSYR9l8IM7uEt3+ifp700af46vHpz587vZQIiw== X-Received: by 2002:ac8:7d8c:0:b0:39c:f4b6:f02f with SMTP id c12-20020ac87d8c000000b0039cf4b6f02fmr29582514qtd.252.1666673274879; Mon, 24 Oct 2022 21:47:54 -0700 (PDT) From: Leonardo Bras To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu Cc: Leonardo Bras , qemu-devel@nongnu.org Subject: [RFC PATCH 3/4] QIOChannel: Add max_pending parameter to qio_channel_flush() Date: Tue, 25 Oct 2022 01:47:30 -0300 Message-Id: <20221025044730.319941-4-leobras@redhat.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221025044730.319941-1-leobras@redhat.com> References: <20221025044730.319941-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=leobras@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, DKIMWL_WL_HIGH=-0.503, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1666673493193100001 Content-Type: text/plain; charset="utf-8" Zero-copy write in Linux is an asynchronous type of write, meaning the send process is not finished by the time the function returns. Since it's also zero-copy, it means that incorrect data may be sent if the write buffer gets modified after write returns. To check if a zero-copy write is finished, qio_channel has implemented a flush operation: qio_channel_flush(). As of today, by the time the flush returns, user code knows for sure all previous zero-copy write have finished, and it's safe to modify any write buffer. While this kind of flush is necessary, it may take a while to flush if there has been a write recently, as the OS has to wait for everything to be sent before returning and allowing reuse / free of the write buffers. An option that can improve performance in some scenarios is to modify flush so it guarantees less than N zero-copy writes are pending, allowing some of the write buffers to be reused. This allows flush to return much faster, since it does not need to wait for the more recent writes to complete. If (N =3D=3D 0), then it replicates the previous flushing behavior. Add max_pending parameter to qio_channel_flush() so caller can decide what is the maximum number of pending writes remaining before the function returns. Also, implement this change in qio_channel_socket_flush(). Change current calls of qio_channel_flush() so (max_pending =3D=3D 0), and = the flush-all behavior is maintained. Signed-off-by: Leonardo Bras --- include/io/channel.h | 7 +++++-- io/channel-socket.c | 5 +++-- io/channel.c | 5 +++-- migration/multifd.c | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/io/channel.h b/include/io/channel.h index c680ee7480..9ec1978a26 100644 --- a/include/io/channel.h +++ b/include/io/channel.h @@ -141,6 +141,7 @@ struct QIOChannelClass { IOHandler *io_write, void *opaque); int (*io_flush)(QIOChannel *ioc, + int max_pending, Error **errp); }; =20 @@ -875,11 +876,12 @@ int qio_channel_writev_full_all(QIOChannel *ioc, /** * qio_channel_flush: * @ioc: the channel object + * @max_pending: Maximum remaining writes allowed in queue upon returning * @errp: pointer to a NULL-initialized error object * - * Will block until every packet queued with + * Will block until there are at most max_pending writes called with * qio_channel_writev_full() + QIO_CHANNEL_WRITE_FLAG_ZERO_COPY - * is sent, or return in case of any error. + * pending, or return in case of any error. * * If not implemented, acts as a no-op, and returns 0. * @@ -889,6 +891,7 @@ int qio_channel_writev_full_all(QIOChannel *ioc, */ =20 int qio_channel_flush(QIOChannel *ioc, + int max_pending, Error **errp); =20 #endif /* QIO_CHANNEL_H */ diff --git a/io/channel-socket.c b/io/channel-socket.c index b76dca9cc1..3d0c2b8a14 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -708,6 +708,7 @@ static ssize_t qio_channel_socket_writev(QIOChannel *io= c, =20 #ifdef QEMU_MSG_ZEROCOPY static int qio_channel_socket_flush(QIOChannel *ioc, + int max_pending, Error **errp) { QIOChannelSocket *sioc =3D QIO_CHANNEL_SOCKET(ioc); @@ -718,7 +719,7 @@ static int qio_channel_socket_flush(QIOChannel *ioc, int received; int ret; =20 - if (sioc->zero_copy_queued =3D=3D sioc->zero_copy_sent) { + if (sioc->zero_copy_queued - sioc->zero_copy_sent <=3D max_pending) { return 0; } =20 @@ -728,7 +729,7 @@ static int qio_channel_socket_flush(QIOChannel *ioc, =20 ret =3D 1; =20 - while (sioc->zero_copy_sent < sioc->zero_copy_queued) { + while (sioc->zero_copy_queued - sioc->zero_copy_sent > max_pending) { received =3D recvmsg(sioc->fd, &msg, MSG_ERRQUEUE); if (received < 0) { switch (errno) { diff --git a/io/channel.c b/io/channel.c index 0640941ac5..9d9f15af50 100644 --- a/io/channel.c +++ b/io/channel.c @@ -490,7 +490,8 @@ off_t qio_channel_io_seek(QIOChannel *ioc, } =20 int qio_channel_flush(QIOChannel *ioc, - Error **errp) + int max_pending, + Error **errp) { QIOChannelClass *klass =3D QIO_CHANNEL_GET_CLASS(ioc); =20 @@ -499,7 +500,7 @@ int qio_channel_flush(QIOChannel *ioc, return 0; } =20 - return klass->io_flush(ioc, errp); + return klass->io_flush(ioc, max_pending, errp); } =20 =20 diff --git a/migration/multifd.c b/migration/multifd.c index aa18c47141..c5d1f911a4 100644 --- a/migration/multifd.c +++ b/migration/multifd.c @@ -574,7 +574,7 @@ static int multifd_zero_copy_flush(QIOChannel *c) int ret; Error *err =3D NULL; =20 - ret =3D qio_channel_flush(c, &err); + ret =3D qio_channel_flush(c, 0, &err); if (ret < 0) { error_report_err(err); return -1; --=20 2.38.0 From nobody Sat May 18 08:35:54 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1666673593; cv=none; d=zohomail.com; s=zohoarc; b=Eetowe3uJYtQJuMhF2Jpla3ASErj7XW5amN+H3SsgGZkWU4hrdy4i84rr9VXxtWojvDzNY7+OA4bGro7VQF1QcY5X2asqMyfGROypEnDwinhodeF+gVbdTZd47n1R7NZP3CU+dhdiuH3dIes6QpIKxpc6ym/kjA2cPLTEtOlrRY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1666673593; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=rhDoG+PrbRzibg6I8lz2xrP5yTvQnEEi4Wd2QPyWLnM=; b=aboi/48Jl0EirpJmULfhx3hOtBldnjcLAKOahGQ8lIVGkz6QJ9LjpT+duiJMu5kY9Mfsg6hoxUJplbHJJnAwcRT6u+gYCQdBeUrj88fe8Q95G42DQV3hClYPj3JVA3HhMgk+zHDue/31ZHSXr7k8vHtgqGvkVOVsoxZtKxyshhQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666673593620244.5306767450544; Mon, 24 Oct 2022 21:53:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1onBt8-0006NS-FF; Tue, 25 Oct 2022 00:50:06 -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 1onBt5-0006Gw-Pu for qemu-devel@nongnu.org; Tue, 25 Oct 2022 00:50:04 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1onBt4-0002vE-5y for qemu-devel@nongnu.org; Tue, 25 Oct 2022 00:50:03 -0400 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-380-2truoTIxPEig8hbRCiwOhg-1; Tue, 25 Oct 2022 00:48:12 -0400 Received: by mail-qk1-f197.google.com with SMTP id f12-20020a05620a408c00b006ced53b80e5so10636097qko.17 for ; Mon, 24 Oct 2022 21:48:12 -0700 (PDT) Received: from LeoBras.redhat.com ([2804:1b3:a801:de5e:6447:4a67:eb7c:b690]) by smtp.gmail.com with ESMTPSA id d23-20020a05620a141700b006cbcdc6efedsm1350968qkj.41.2022.10.24.21.47.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Oct 2022 21:47:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666673401; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rhDoG+PrbRzibg6I8lz2xrP5yTvQnEEi4Wd2QPyWLnM=; b=QQrOfy6+2oDKwJSN4EgFJOKZfiDwLpvtbfGK33jZfAiBwdhOr/HRZp9W0yY5YIOHpSmjSL BLJlkpk1/jKLqCstav82TUVclYJ/ToSXO+mnJLFp3FV2PqKRehG+Al6iN0GKvdChxWJOt0 hWWYL83Zn2NS/BH85hE6OdzcUHuLnmU= X-MC-Unique: 2truoTIxPEig8hbRCiwOhg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rhDoG+PrbRzibg6I8lz2xrP5yTvQnEEi4Wd2QPyWLnM=; b=R3XZvRfBNU2f+ifVIGdnSgyWoCgYxghwmoF5G2yt+6ETjkDPnvTqI7dHMk0hQTTJtr TkT8rtnstH5ReUTSQrsTAG0VJg8KIA3jtK9rfBLOQndLXy/3Wz9nfxALw4h2iNgBlmRM avl8TLROkfRa1TMO/a40afsPunbLaLMCFYQ3Sw0cuzHdtW58QlI5CT5VlwCEQ4uqqZOK d/cWcYnz09lpLYhPPQ8N4BRcM4LbKut1SQfsjAJBc5OEmLcAOl4UpDuopckNjeUow6H3 E9KjmxUfZLvqaOpAGIExxk10gaBn71UsNUvuAFIVAMkUzDMSB7vprby9WPeVEgG53Cbc OlLQ== X-Gm-Message-State: ACrzQf1c2R79reQK2UcK3rjJ+dFpo99dFy9VoRkySvZsVHvnyy6DGtWF a9wG+NK9akpg2Ra+sFA5JEjJnupVs/XK623z+tPBKcygHLJscVeLEDAyHhjoAKOqvPbTFNrxjCG CsLwG2OtPbK5lAFE= X-Received: by 2002:ae9:e107:0:b0:6ed:638:930 with SMTP id g7-20020ae9e107000000b006ed06380930mr24680782qkm.211.1666673276930; Mon, 24 Oct 2022 21:47:56 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6l2vfpZCORdHGlrDtwXNc2ykEbJBl5R5EoMNV1NNs7qSblFsrMB9KSuz+x4o3Zah6UE99VwA== X-Received: by 2002:ae9:e107:0:b0:6ed:638:930 with SMTP id g7-20020ae9e107000000b006ed06380930mr24680778qkm.211.1666673276713; Mon, 24 Oct 2022 21:47:56 -0700 (PDT) From: Leonardo Bras To: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Juan Quintela , "Dr. David Alan Gilbert" , Peter Xu Cc: Leonardo Bras , qemu-devel@nongnu.org Subject: [RFC PATCH 4/4] migration/multifd/zero-copy: Flush only the LRU half of the header array Date: Tue, 25 Oct 2022 01:47:31 -0300 Message-Id: <20221025044730.319941-5-leobras@redhat.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221025044730.319941-1-leobras@redhat.com> References: <20221025044730.319941-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=leobras@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, DKIMWL_WL_HIGH=-0.503, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: , Sender: "Qemu-devel" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1666673595755100001 Content-Type: text/plain; charset="utf-8" Zero-copy multifd migration sends both the header and the memory pages in a single syscall. Since it's necessary to flush before reusing the header, a header array was implemented, so each write call uses a different array, and flushing only take place after all headers have been used, meaning 1 flush for each N writes. This method has a bottleneck, though: After the last write, a flush will have to wait for all writes to finish, which will be a lot, meaning the recvmsg() syscall called in qio_channel_socket_flush() will be called a lot. On top of that, it will create a time period when the I/O queue is empty and nothing is getting send: between the flush and the next write. To avoid that, use qio_channel_flush()'s new max_pending parameter to wait until at most half of the array is still in use. (i.e. the LRU half of the array can be reused) Flushing for the LRU half of the array is much faster, since it does not have to wait for the most recent writes to finish, making up for having to flush twice per array. As a main benefit, this approach keeps the I/O queue from being empty while there are still data to be sent, making it easier to keep the I/O maximum throughput while consuming less cpu time. Signed-off-by: Leonardo Bras --- migration/multifd.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/migration/multifd.c b/migration/multifd.c index c5d1f911a4..fe9df460f6 100644 --- a/migration/multifd.c +++ b/migration/multifd.c @@ -569,12 +569,13 @@ void multifd_save_cleanup(void) multifd_send_state =3D NULL; } =20 -static int multifd_zero_copy_flush(QIOChannel *c) +static int multifd_zero_copy_flush(QIOChannel *c, + int max_remaining) { int ret; Error *err =3D NULL; =20 - ret =3D qio_channel_flush(c, 0, &err); + ret =3D qio_channel_flush(c, max_remaining, &err); if (ret < 0) { error_report_err(err); return -1; @@ -636,7 +637,7 @@ int multifd_send_sync_main(QEMUFile *f) qemu_mutex_unlock(&p->mutex); qemu_sem_post(&p->sem); =20 - if (flush_zero_copy && p->c && (multifd_zero_copy_flush(p->c) < 0)= ) { + if (flush_zero_copy && p->c && (multifd_zero_copy_flush(p->c, 0) <= 0)) { return -1; } } @@ -719,12 +720,17 @@ static void *multifd_send_thread(void *opaque) =20 if (use_zero_copy_send) { p->packet_idx =3D (p->packet_idx + 1) % HEADER_ARR_SZ; - - if (!p->packet_idx && (multifd_zero_copy_flush(p->c) < 0))= { + /* + * When half the array have been used, flush to make sure = the + * next half is available + */ + if (!(p->packet_idx % (HEADER_ARR_SZ / 2)) && + (multifd_zero_copy_flush(p->c, HEADER_ARR_SZ / 2) < 0)= ) { break; } header =3D (void *)p->packet + p->packet_idx * p->packet_l= en; } + qemu_mutex_lock(&p->mutex); p->pending_job--; qemu_mutex_unlock(&p->mutex); --=20 2.38.0