Skip to content

Commit 9cbcfb5

Browse files
gkurzdgibson
authored andcommitted
target/ppc/kvm: Cache timebase frequency
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 <groug@kaod.org> Message-Id: <161600382766.1780699.6787739229984093959.stgit@bahia.lan> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
1 parent 0932567 commit 9cbcfb5

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

target/ppc/kvm.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,24 +1815,37 @@ static int read_cpuinfo(const char *field, char *value, int len)
18151815
return ret;
18161816
}
18171817

1818-
uint32_t kvmppc_get_tbfreq(void)
1818+
static uint32_t kvmppc_get_tbfreq_procfs(void)
18191819
{
18201820
char line[512];
18211821
char *ns;
1822-
uint32_t retval = NANOSECONDS_PER_SECOND;
1822+
uint32_t tbfreq_fallback = NANOSECONDS_PER_SECOND;
1823+
uint32_t tbfreq_procfs;
18231824

18241825
if (read_cpuinfo("timebase", line, sizeof(line))) {
1825-
return retval;
1826+
return tbfreq_fallback;
18261827
}
18271828

18281829
ns = strchr(line, ':');
18291830
if (!ns) {
1830-
return retval;
1831+
return tbfreq_fallback;
18311832
}
18321833

1833-
ns++;
1834+
tbfreq_procfs = atoi(++ns);
1835+
1836+
/* 0 is certainly not acceptable by the guest, return fallback value */
1837+
return tbfreq_procfs ? tbfreq_procfs : tbfreq_fallback;
1838+
}
1839+
1840+
uint32_t kvmppc_get_tbfreq(void)
1841+
{
1842+
static uint32_t cached_tbfreq;
1843+
1844+
if (!cached_tbfreq) {
1845+
cached_tbfreq = kvmppc_get_tbfreq_procfs();
1846+
}
18341847

1835-
return atoi(ns);
1848+
return cached_tbfreq;
18361849
}
18371850

18381851
bool kvmppc_get_host_serial(char **value)

0 commit comments

Comments
 (0)