qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
Cc: qemu-ppc@nongnu.org, qemu-devel@nongnu.org,
	David Gibson <david@gibson.dropbear.id.au>
Subject: Re: [PATCH] target/ppc/kvm: Cache timebase frequency
Date: Wed, 17 Mar 2021 17:32:56 +0100	[thread overview]
Message-ID: <20210317173256.6c87aca1@bahia.lan> (raw)
In-Reply-To: <b10f7706-204b-6bb0-895a-c6e680acd2c3@amsat.org>

On Wed, 17 Mar 2021 16:39:04 +0100
Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:

> On 3/17/21 4:24 PM, Greg Kurz wrote:
> > 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.
> > 
> > With this patch applied:
> > 
> >     384                 0.518382
> > 
> > Signed-off-by: Greg Kurz <groug@kaod.org>
> > ---
> >  target/ppc/kvm.c |   11 +++++++++--
> >  1 file changed, 9 insertions(+), 2 deletions(-)
> > 
> > diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
> > index 298c1f882c67..9ad3dae29132 100644
> > --- a/target/ppc/kvm.c
> > +++ b/target/ppc/kvm.c
> > @@ -1819,7 +1819,13 @@ uint32_t kvmppc_get_tbfreq(void)
> >  {
> >      char line[512];
> >      char *ns;
> > -    uint32_t retval = NANOSECONDS_PER_SECOND;
> > +    static uint32_t retval = -1;
> 
> Please document why in the code ...
> 

Yeah, I've been lazy :)

> > +
> > +    if (retval != -1) {
> > +        return retval;
> > +    }
> > +
> > +    retval = NANOSECONDS_PER_SECOND;
> >  
> >      if (read_cpuinfo("timebase", line, sizeof(line))) {
> >          return retval;
> > @@ -1832,7 +1838,8 @@ uint32_t kvmppc_get_tbfreq(void)
> >  
> >      ns++;
> >  
> > -    return atoi(ns);
> > +    retval = atoi(ns);
> > +    return retval;
> >  }
> 
> ... or alternatively use self-documented code:
> 
> -- >8 --
> diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c
> index 298c1f882c6..2b2fe5d8148 100644
> --- a/target/ppc/kvm.c
> +++ b/target/ppc/kvm.c
> @@ -1815,7 +1815,7 @@ 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;
> @@ -1835,6 +1835,17 @@ uint32_t kvmppc_get_tbfreq(void)
>      return atoi(ns);
>  }
> 
> +uint32_t kvmppc_get_tbfreq(void)
> +{
> +    static uint32_t cached_tbfreq;
> +
> +    if (!cached_tbfreq) {
> +        cached_tbfreq = kvmppc_get_tbfreq_procfs();
> +    }
> +
> +    return cached_tbfreq;
> +}
> +

This is much nicer indeed. I'll do just that in v2.

Thanks ! :)

>  bool kvmppc_get_host_serial(char **value)
>  {
>      return g_file_get_contents("/proc/device-tree/system-id", value, NULL,
> ---



      reply	other threads:[~2021-03-17 16:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-17 15:24 [PATCH] target/ppc/kvm: Cache timebase frequency Greg Kurz
2021-03-17 15:39 ` Philippe Mathieu-Daudé
2021-03-17 16:32   ` Greg Kurz [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210317173256.6c87aca1@bahia.lan \
    --to=groug@kaod.org \
    --cc=david@gibson.dropbear.id.au \
    --cc=f4bug@amsat.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).