From patchwork Wed Mar 17 17:57:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 12146659 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8122CC433E0 for ; Wed, 17 Mar 2021 17:59:23 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 1212964F21 for ; Wed, 17 Mar 2021 17:59:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1212964F21 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kaod.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:33606 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMaS1-00085k-R9 for qemu-devel@archiver.kernel.org; Wed, 17 Mar 2021 13:59:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56556) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMaQ2-0006qi-Mw for qemu-devel@nongnu.org; Wed, 17 Mar 2021 13:57:18 -0400 Received: from us-smtp-delivery-44.mimecast.com ([207.211.30.44]:29279) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lMaQ0-00042o-VD for qemu-devel@nongnu.org; Wed, 17 Mar 2021 13:57:18 -0400 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-595--q9GStCfOUykwSEQwNitNA-1; Wed, 17 Mar 2021 13:57:10 -0400 X-MC-Unique: -q9GStCfOUykwSEQwNitNA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8A92883DBF6; Wed, 17 Mar 2021 17:57:09 +0000 (UTC) Received: from bahia.lan (ovpn-113-236.ams2.redhat.com [10.36.113.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 87752648A2; Wed, 17 Mar 2021 17:57:08 +0000 (UTC) Subject: [PATCH v2] target/ppc/kvm: Cache timebase frequency From: Greg Kurz To: qemu-devel@nongnu.org Date: Wed, 17 Mar 2021 18:57:07 +0100 Message-ID: <161600382766.1780699.6787739229984093959.stgit@bahia.lan> User-Agent: StGit/0.21 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=groug@kaod.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: kaod.org Received-SPF: softfail client-ip=207.211.30.44; envelope-from=groug@kaod.org; helo=us-smtp-delivery-44.mimecast.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665 autolearn=no 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: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , qemu-ppc@nongnu.org, David Gibson Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Each vCPU core exposes its timebase frequency in the DT. When running under KVM, this means parsing /proc/cpuinfo in order to get the timebase frequency of the host CPU. The parsing appears to slow down the boot quite a bit with higher number of cores: # of cores seconds spent in spapr_dt_cpus() 8 0.550122 16 1.342375 32 2.850316 64 5.922505 96 9.109224 128 12.245504 256 24.957236 384 37.389113 The timebase frequency of the host CPU is identical for all cores and it is an invariant for the VM lifetime. Cache it instead of doing the same expensive parsing again and again. Rename kvmppc_get_tbfreq() to kvmppc_get_tbfreq_procfs() and rename the 'retval' variable to make it clear it is used as fallback only. Come up with a new version of kvmppc_get_tbfreq() that calls kvmppc_get_tbfreq_procfs() only once and keep the value in a static. Zero is certainly not a valid value for the timebase frequency. Treat atoi() returning zero as another parsing error and return the fallback value instead. This allows kvmppc_get_tbfreq() to use zero as an indicator that kvmppc_get_tbfreq_procfs() hasn't been called yet. With this patch applied: 384 0.518382 Signed-off-by: Greg Kurz --- v2: - do the caching in a distinct function for clarity (Philippe) - rename 'retval' to 'tbfreq_fallback' - expand the changelog a bit --- target/ppc/kvm.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index 298c1f882c67..104a308abb57 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -1815,24 +1815,37 @@ static int read_cpuinfo(const char *field, char *value, int len) return ret; } -uint32_t kvmppc_get_tbfreq(void) +static uint32_t kvmppc_get_tbfreq_procfs(void) { char line[512]; char *ns; - uint32_t retval = NANOSECONDS_PER_SECOND; + uint32_t tbfreq_fallback = NANOSECONDS_PER_SECOND; + uint32_t tbfreq_procfs; if (read_cpuinfo("timebase", line, sizeof(line))) { - return retval; + return tbfreq_fallback; } ns = strchr(line, ':'); if (!ns) { - return retval; + return tbfreq_fallback; } - ns++; + tbfreq_procfs = atoi(++ns); + + /* 0 is certainly not acceptable by the guest, return fallback value */ + return tbfreq_procfs ? tbfreq_procfs : tbfreq_fallback; +} + +uint32_t kvmppc_get_tbfreq(void) +{ + static uint32_t cached_tbfreq; + + if (!cached_tbfreq) { + cached_tbfreq = kvmppc_get_tbfreq_procfs(); + } - return atoi(ns); + return cached_tbfreq; } bool kvmppc_get_host_serial(char **value)