From 5cbd14b3e5f91819d4e1924a5118b32754e0d87e Mon Sep 17 00:00:00 2001 From: Pavan Nikhilesh Date: Sat, 16 Mar 2019 19:01:54 +0000 Subject: [PATCH] eal: roundup TSC frequency when estimating When estimating tsc frequency using sleep/gettime round it up to the nearest multiple of 10Mhz for more accuracy. Signed-off-by: Pavan Nikhilesh Reviewed-by: Keith Wiles --- lib/librte_eal/common/eal_common_timer.c | 4 +++- lib/librte_eal/linux/eal/eal_timer.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/librte_eal/common/eal_common_timer.c b/lib/librte_eal/common/eal_common_timer.c index dcf26bfead..68d67e684b 100644 --- a/lib/librte_eal/common/eal_common_timer.c +++ b/lib/librte_eal/common/eal_common_timer.c @@ -64,12 +64,14 @@ rte_get_tsc_hz(void) static uint64_t estimate_tsc_freq(void) { +#define CYC_PER_10MHZ 1E7 RTE_LOG(WARNING, EAL, "WARNING: TSC frequency estimated roughly" " - clock timings may be less accurate.\n"); /* assume that the sleep(1) will sleep for 1 second */ uint64_t start = rte_rdtsc(); sleep(1); - return rte_rdtsc() - start; + /* Round up to 10Mhz. 1E7 ~ 10Mhz */ + return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, CYC_PER_10MHZ); } void diff --git a/lib/librte_eal/linux/eal/eal_timer.c b/lib/librte_eal/linux/eal/eal_timer.c index bc8f051990..76ec17034b 100644 --- a/lib/librte_eal/linux/eal/eal_timer.c +++ b/lib/librte_eal/linux/eal/eal_timer.c @@ -232,6 +232,7 @@ get_tsc_freq(void) { #ifdef CLOCK_MONOTONIC_RAW #define NS_PER_SEC 1E9 +#define CYC_PER_10MHZ 1E7 struct timespec sleeptime = {.tv_nsec = NS_PER_SEC / 10 }; /* 1/10 second */ @@ -248,7 +249,8 @@ get_tsc_freq(void) double secs = (double)ns/NS_PER_SEC; tsc_hz = (uint64_t)((end - start)/secs); - return tsc_hz; + /* Round up to 10Mhz. 1E7 ~ 10Mhz */ + return RTE_ALIGN_MUL_NEAR(tsc_hz, CYC_PER_10MHZ); } #endif return 0; -- 2.20.1