From nobody Thu Nov 13 17:44:03 2025 Delivered-To: importer@patchew.org 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; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1582050214750923.8948874133305; Tue, 18 Feb 2020 10:23:34 -0800 (PST) Received: from localhost ([::1]:39900 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j47Wv-0004PE-2X for importer@patchew.org; Tue, 18 Feb 2020 13:23:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49225) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j47W3-0003yk-09 for qemu-devel@nongnu.org; Tue, 18 Feb 2020 13:22:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j47W1-0002zV-90 for qemu-devel@nongnu.org; Tue, 18 Feb 2020 13:22:38 -0500 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:31559 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j47W1-0002xY-18 for qemu-devel@nongnu.org; Tue, 18 Feb 2020 13:22:37 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-234-s-h_4HsHOIiZXszZvt1Isw-1; Tue, 18 Feb 2020 13:22:34 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DF5E0107B267 for ; Tue, 18 Feb 2020 18:22:30 +0000 (UTC) Received: from localhost (unknown [10.36.118.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id C59015C13B; Tue, 18 Feb 2020 18:22:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582050156; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=vfE1X1JGFqyKfp7SozvxEPu1YK6Qig1zeUakm0hN9T0=; b=YVKJ6gdlYKcY2ATq2V5HoACglfo7mF1CWyfuybrekHaIRymadk2fabkcnGShhJYo3fobjX rjfJ2v2qlEucvxGgGjjCdanL6Vik9m8jAGOQRpFu0WNefPn3l82NlyH4tlFRgbIBzjlpHt p01hgynjcapVUEttMn5R4L+pSH1Olmk= From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH] memory: batch allocate ioeventfds[] in address_space_update_ioeventfds() Date: Tue, 18 Feb 2020 18:22:26 +0000 Message-Id: <20200218182226.913977-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-MC-Unique: s-h_4HsHOIiZXszZvt1Isw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Reallocing the ioeventfds[] array each time an element is added is very expensive as the number of ioeventfds increases. Batch allocate instead to amortize the cost of realloc. This patch reduces Linux guest boot times from 362s to 140s when there are 2 virtio-blk devices with 1 virtqueue and 99 virtio-blk devices with 32 virtqueues. Signed-off-by: Stefan Hajnoczi Reviewed-by: Stefan Hajnoczi --- memory.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/memory.c b/memory.c index aeaa8dcc9e..2d6f931f8c 100644 --- a/memory.c +++ b/memory.c @@ -794,10 +794,18 @@ static void address_space_update_ioeventfds(AddressSp= ace *as) FlatView *view; FlatRange *fr; unsigned ioeventfd_nb =3D 0; - MemoryRegionIoeventfd *ioeventfds =3D NULL; + unsigned ioeventfd_max; + MemoryRegionIoeventfd *ioeventfds; AddrRange tmp; unsigned i; =20 + /* + * It is likely that the number of ioeventfds hasn't changed much, so = use + * the previous size as the starting value. + */ + ioeventfd_max =3D as->ioeventfd_nb; + ioeventfds =3D g_new(MemoryRegionIoeventfd, ioeventfd_max); + view =3D address_space_get_flatview(as); FOR_EACH_FLAT_RANGE(fr, view) { for (i =3D 0; i < fr->mr->ioeventfd_nb; ++i) { @@ -806,8 +814,11 @@ static void address_space_update_ioeventfds(AddressSpa= ce *as) int128_make64(fr->offset_in_r= egion))); if (addrrange_intersects(fr->addr, tmp)) { ++ioeventfd_nb; - ioeventfds =3D g_realloc(ioeventfds, - ioeventfd_nb * sizeof(*ioeventfd= s)); + if (ioeventfd_nb > ioeventfd_max) { + ioeventfd_max +=3D 64; + ioeventfds =3D g_realloc(ioeventfds, + ioeventfd_max * sizeof(*ioeventfds)); + } ioeventfds[ioeventfd_nb-1] =3D fr->mr->ioeventfds[i]; ioeventfds[ioeventfd_nb-1].addr =3D tmp; } --=20 2.24.1