From nobody Fri May 17 03:01:06 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 ARC-Seal: i=1; a=rsa-sha256; t=1608550665; cv=none; d=zohomail.com; s=zohoarc; b=FoxuItWudCu0OVhxHZcpPCXsywB7eZ0kscUEOYToRBVRaO5pKmohLtJzff3oCpJE7UPsEIk/emq41bOlK+xsW49W2FKS0ZOrT8PaEYZEc9ZSxS8SdC0I7yAjxpnnMw4P5cOYlbyZh/fwCHFxFEfO15kd3ADNx2aIclDg/FqO66c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1608550665; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=c5XGrMuYuCHnDrriRGNn4q+5xB9MEfhzF7DHZrAl4vU=; b=kU30gfai0XShj70eHjHO3SRjFrWk4wKjdW/1Q2bmyJ8jMRC5kn/N9+MRkOgWfOgFhqRBP0oovmBu+hRFt+paNnIORAMdIGW4w7f1neQkfgtUz7yRvgF6VvFfMIdRi+xnJ7HJeOKocCxanuEdGyvopwjDNK4rxvSyYHRtX2OY9t4= ARC-Authentication-Results: i=1; mx.zohomail.com; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1608550665046965.1506247641295; Mon, 21 Dec 2020 03:37:45 -0800 (PST) Received: from localhost ([::1]:56996 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1krJVX-0006tK-M0 for importer@patchew.org; Mon, 21 Dec 2020 06:37:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58808) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1krJUW-0006Q7-UE for qemu-devel@nongnu.org; Mon, 21 Dec 2020 06:36:40 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2899) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1krJUP-0007wY-Kp for qemu-devel@nongnu.org; Mon, 21 Dec 2020 06:36:38 -0500 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Czy8J6z05z15grZ; Mon, 21 Dec 2020 19:35:44 +0800 (CST) Received: from [10.174.186.6] (10.174.186.6) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.498.0; Mon, 21 Dec 2020 19:36:19 +0800 To: "S. Tsirkin, Michael" , , Paolo Bonzini , , "Eduardo Habkost" From: Zhenyu Ye Subject: [RFC PATCH v2] x86/cpu: initialize the CPU concurrently Message-ID: <90be4860-cbe0-25d4-ccca-75b96ecb4a3c@huawei.com> Date: Mon, 21 Dec 2020 19:36:18 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.186.6] X-CFilter-Loop: Reflected 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=45.249.212.190; envelope-from=yezhenyu2@huawei.com; helo=szxga04-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, 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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yebiaoxiang , yezhenyu2@huawei.com, qemu-devel@nongnu.org, Xiexiangyou , Zhanghailiang Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Providing a optional mechanism to wait for all VCPU threads be created out of qemu_init_vcpu(), then we can initialize the cpu concurrently on the x86 architecture. This reduces the time of creating virtual machines. For example, when the haxm is used as the accelerator, cpus_accel->create_vcpu_thread() will cause at least 200ms for each cpu, extremely prolong the boot time. Signed-off-by: Eduardo Habkost Signed-off-by: eillon --- hw/i386/x86.c | 3 +++ include/hw/core/cpu.h | 13 +++++++++++++ softmmu/cpus.c | 21 +++++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/hw/i386/x86.c b/hw/i386/x86.c index 6329f90ef9..09afff724a 100644 --- a/hw/i386/x86.c +++ b/hw/i386/x86.c @@ -108,6 +108,8 @@ void x86_cpu_new(X86MachineState *x86ms, int64_t apic_i= d, Error **errp) if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) { goto out; } + + CPU(cpu)->async_init =3D true; qdev_realize(DEVICE(cpu), NULL, errp); out: @@ -137,6 +139,7 @@ void x86_cpus_init(X86MachineState *x86ms, int default_= cpu_version) for (i =3D 0; i < ms->smp.cpus; i++) { x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal); } + qemu_wait_all_vcpu_threads_init(); } void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count) diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 8e7552910d..55c2c17d93 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -467,6 +467,12 @@ struct CPUState { /* track IOMMUs whose translations we've cached in the TCG TLB */ GArray *iommu_notifiers; + + /* + * If true, qemu_init_vcpu() will not wait for the VCPU thread to be c= reated + * before returning. + */ + bool async_init; }; typedef QTAILQ_HEAD(CPUTailQ, CPUState) CPUTailQ; @@ -977,6 +983,13 @@ void start_exclusive(void); */ void end_exclusive(void); +/** + * qemu_wait_all_vcpu_threads_init: + * + * Wait for all VCPU threads to be created. + */ +void qemu_wait_all_vcpu_threads_init(void); + /** * qemu_init_vcpu: * @cpu: The vCPU to initialize. diff --git a/softmmu/cpus.c b/softmmu/cpus.c index 1dc20b9dc3..d76853d356 100644 --- a/softmmu/cpus.c +++ b/softmmu/cpus.c @@ -601,6 +601,23 @@ void cpus_register_accel(const CpusAccel *ca) cpus_accel =3D ca; } +static void qemu_wait_vcpu_thread_init(CPUState *cpu) +{ + while (!cpu->created) { + qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); + } +} + +void qemu_wait_all_vcpu_threads_init(void) +{ + CPUState *cpu; + + CPU_FOREACH(cpu) { + printf("***** cpuid: %d\n", cpu->cpu_index); + qemu_wait_vcpu_thread_init(cpu); + } +} + void qemu_init_vcpu(CPUState *cpu) { MachineState *ms =3D MACHINE(qdev_get_machine()); @@ -622,8 +639,8 @@ void qemu_init_vcpu(CPUState *cpu) g_assert(cpus_accel !=3D NULL && cpus_accel->create_vcpu_thread !=3D N= ULL); cpus_accel->create_vcpu_thread(cpu); - while (!cpu->created) { - qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex); + if (!cpu->async_init) { + qemu_wait_vcpu_thread_init(cpu); } } --=20 2.22.0.windows.1