summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Monjalon <thomas.monjalon@6wind.com>2012-05-31 19:00:31 +0200
committerThomas Monjalon <thomas.monjalon@6wind.com>2013-07-25 16:08:01 +0200
commit54d60229a1c35c2a008f9d1b0513002f874dc42a (patch)
tree0eba30a63fa52fb6cf46abf7adc25cd1ba0bb774
parentac3589e96ae6b526e8bec35b52c40481c996d55c (diff)
downloaddpdk-54d60229a1c35c2a008f9d1b0513002f874dc42a.zip
dpdk-54d60229a1c35c2a008f9d1b0513002f874dc42a.tar.gz
dpdk-54d60229a1c35c2a008f9d1b0513002f874dc42a.tar.xz
timer: get TSC frequency from /proc/cpuinfo
Frequency was guessed by sleeping 1 sec. Now, read frequency from cpuinfo first. Keep sleep method as fallback. Signed-off-by: Thomas Monjalon <thomas.monjalon@6wind.com> Acked-by: Ivan Boule <ivan.boule@6wind.com>
-rw-r--r--lib/librte_eal/linuxapp/eal/eal_hpet.c47
1 files changed, 42 insertions, 5 deletions
diff --git a/lib/librte_eal/linuxapp/eal/eal_hpet.c b/lib/librte_eal/linuxapp/eal/eal_hpet.c
index 0de2c35..0b8b1a0 100644
--- a/lib/librte_eal/linuxapp/eal/eal_hpet.c
+++ b/lib/librte_eal/linuxapp/eal/eal_hpet.c
@@ -128,14 +128,51 @@ hpet_msb_inc(__attribute__((unused)) void *arg)
}
}
-static inline void
-set_rdtsc_freq(void)
+static uint64_t
+get_tsc_freq_from_cpuinfo(void)
{
- uint64_t start;
+ char line[256];
+ FILE *stream;
+ double dmhz;
+ uint64_t freq = 0;
+
+ stream = fopen("/proc/cpuinfo", "r");
+ if (!stream) {
+ RTE_LOG(WARNING, EAL, "WARNING: Unable to open /proc/cpuinfo\n");
+ return 0;
+ }
+
+ while (fgets(line, sizeof line, stream)) {
+ if (sscanf(line, "cpu MHz\t: %lf", &dmhz) == 1) {
+ freq = (uint64_t)(dmhz * 1000000UL);
+ break;
+ }
+ }
+
+ fclose(stream);
+ return freq;
+}
- start = rte_rdtsc();
+static uint64_t
+get_tsc_freq_from_sleep(void)
+{
+ uint64_t start = rte_rdtsc();
sleep(1);
- eal_hpet_resolution_hz = rte_rdtsc() - start;
+ return rte_rdtsc() - start;
+}
+
+static inline void
+set_rdtsc_freq(void)
+{
+ uint64_t freq;
+ freq = get_tsc_freq_from_cpuinfo();
+ if (!freq) {
+ RTE_LOG(WARNING, EAL, "WARNING: Cannot read CPU clock\n");
+ freq = get_tsc_freq_from_sleep();
+ }
+ RTE_LOG(INFO, EAL, "TSC clock @ %lu MHz\n",
+ (unsigned long) (freq / (1000000UL)));
+ eal_hpet_resolution_hz = freq;
eal_hpet_resolution_fs = (uint32_t)
((1.0 / eal_hpet_resolution_hz) / 1e-15);
}