eal: roundup tsc frequency when estimating it

Message ID 20181129083138.23029-1-pbhagavatula@caviumnetworks.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series eal: roundup tsc frequency when estimating it |

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/mellanox-Performance-Testing success Performance Testing PASS
ci/intel-Performance-Testing success Performance Testing PASS

Commit Message

Pavan Nikhilesh Nov. 29, 2018, 8:32 a.m. UTC
  When estimating tsc frequency using sleep/gettime round it up to the
nearest multiple of 10Mhz for more accuracy.

Signed-off-by: Pavan Nikhilesh <pbhagavatula@caviumnetworks.com>
---
 lib/librte_eal/common/eal_common_timer.c   |  4 ++--
 lib/librte_eal/common/include/rte_common.h | 10 ++++++++++
 lib/librte_eal/linuxapp/eal/eal_timer.c    |  2 +-
 3 files changed, 13 insertions(+), 3 deletions(-)
  

Comments

Jerin Jacob Nov. 29, 2018, 9:08 a.m. UTC | #1
-----Original Message-----
> Date: Thu, 29 Nov 2018 14:02:03 +0530
> From: "Bhagavatula, Pavan" <Pavan.Bhagavatula@cavium.com>
> To: "Jacob,  Jerin" <Jerin.JacobKollanukkaran@cavium.com>
> CC: "dev@dpdk.org" <dev@dpdk.org>, "Bhagavatula, Pavan"
>  <Pavan.Bhagavatula@cavium.com>
> Subject: [dpdk-dev] [PATCH] eal: roundup tsc frequency when estimating it
> 
> When estimating tsc frequency using sleep/gettime round it up to the
> nearest multiple of 10Mhz for more accuracy.
> 
> Signed-off-by: Pavan Nikhilesh <pbhagavatula@caviumnetworks.com>
> ---
>  lib/librte_eal/common/eal_common_timer.c   |  4 ++--
>  lib/librte_eal/common/include/rte_common.h | 10 ++++++++++
>  lib/librte_eal/linuxapp/eal/eal_timer.c    |  2 +-
>  3 files changed, 13 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/librte_eal/common/eal_common_timer.c b/lib/librte_eal/common/eal_common_timer.c
> index dcf26bfea..1358bbed0 100644
> --- a/lib/librte_eal/common/eal_common_timer.c
> +++ b/lib/librte_eal/common/eal_common_timer.c
> @@ -69,7 +69,7 @@ estimate_tsc_freq(void)
>  	/* assume that the sleep(1) will sleep for 1 second */
>  	uint64_t start = rte_rdtsc();
>  	sleep(1);
> -	return rte_rdtsc() - start;
> +	return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, 1E7);
>  }
>  
>  void
> @@ -83,7 +83,7 @@ set_tsc_freq(void)
>  	if (!freq)
>  		freq = estimate_tsc_freq();
>  
> -	RTE_LOG(DEBUG, EAL, "TSC frequency is ~%" PRIu64 " KHz\n", freq / 1000);
> +	RTE_LOG(INFO, EAL, "TSC frequency is ~%" PRIu64 " Hz\n", freq);
>  	eal_tsc_resolution_hz = freq;
>  }
>  
> diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h
> index 66cdf60b2..e374b16b1 100644
> --- a/lib/librte_eal/common/include/rte_common.h
> +++ b/lib/librte_eal/common/include/rte_common.h
> @@ -248,6 +248,16 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
>  #define RTE_ALIGN_MUL_FLOOR(v, mul) \
>  	((v / ((typeof(v))(mul))) * (typeof(v))(mul))
>  
> +/**
> + * Macro to align a value to the nearest multiple of given value.
> + */
> +#define RTE_ALIGN_MUL_NEAR(v, mul)				\
> +	({							\
> +		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
> +		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
> +		(ceil - v) > (v - floor) ? floor: ceil;		\
> +	})
> +

Probably it is better to have two patches.
First patch to add new API along with unit testcase.
Second patch to roundup tsc frequency when estimating it.


>  /**
>   * Checks if a pointer is aligned to a given power-of-two value
>   *
> diff --git a/lib/librte_eal/linuxapp/eal/eal_timer.c b/lib/librte_eal/linuxapp/eal/eal_timer.c
> index bc8f05199..864d6ef29 100644
> --- a/lib/librte_eal/linuxapp/eal/eal_timer.c
> +++ b/lib/librte_eal/linuxapp/eal/eal_timer.c
> @@ -248,7 +248,7 @@ get_tsc_freq(void)
>  
>  		double secs = (double)ns/NS_PER_SEC;
>  		tsc_hz = (uint64_t)((end - start)/secs);
> -		return tsc_hz;
> +		return RTE_ALIGN_MUL_NEAR(tsc_hz, 1E7);
>  	}
>  #endif
>  	return 0;
> -- 
> 2.19.2
>
  
Stephen Hemminger Nov. 29, 2018, 9:21 p.m. UTC | #2
On Thu, 29 Nov 2018 08:32:03 +0000
Pavan Nikhilesh <pbhagavatula@caviumnetworks.com> wrote:

> When estimating tsc frequency using sleep/gettime round it up to the
> nearest multiple of 10Mhz for more accuracy.
> 
> Signed-off-by: Pavan Nikhilesh <pbhagavatula@caviumnetworks.com>

Rounding reduces accuracy.

Why is this code being used?  Shouldn't get_tsc_freq_arch return a
correct value?

How well does the rdmsr() logic work in VM?
It looks like Hyper-V has special MSR's for TSC frequency determination.
  
Pavan Nikhilesh Nov. 30, 2018, 7:17 a.m. UTC | #3
Hi Stephen,

On Thu, Nov 29, 2018 at 01:21:52PM -0800, Stephen Hemminger wrote:
> On Thu, 29 Nov 2018 08:32:03 +0000
> Pavan Nikhilesh <pbhagavatula@caviumnetworks.com> wrote:
>
> > When estimating tsc frequency using sleep/gettime round it up to the
> > nearest multiple of 10Mhz for more accuracy.
> >
> > Signed-off-by: Pavan Nikhilesh <pbhagavatula@caviumnetworks.com>
>
> Rounding reduces accuracy.
>
> Why is this code being used?  Shouldn't get_tsc_freq_arch return a
> correct value?

This patch doesn't modify get_tsc_freq_arch(), it basically gives a more
accurate freq reading when we rely on sleep(1) i.e. only when
get_tsc_freq_arch() returns 0.

example:

static uint64_t
estimate_tsc_freq(void)
{
        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;
}

This will not give the accurate cyc/sec in most cases, rounding it to 10Mhz wil
do the job.

In case of ARM64 if we enable RTE_ARM_EAL_RDTSC_USE_PMU, get_tsc_freq_arch()
will return 0 as there is no instruction to determine the clk of PMU.

>
> How well does the rdmsr() logic work in VM?
> It looks like Hyper-V has special MSR's for TSC frequency determination.

Maybe bruce can give a more accurate answer to this as it is x86 specific.

Thanks,
Pavan.
  

Patch

diff --git a/lib/librte_eal/common/eal_common_timer.c b/lib/librte_eal/common/eal_common_timer.c
index dcf26bfea..1358bbed0 100644
--- a/lib/librte_eal/common/eal_common_timer.c
+++ b/lib/librte_eal/common/eal_common_timer.c
@@ -69,7 +69,7 @@  estimate_tsc_freq(void)
 	/* assume that the sleep(1) will sleep for 1 second */
 	uint64_t start = rte_rdtsc();
 	sleep(1);
-	return rte_rdtsc() - start;
+	return RTE_ALIGN_MUL_NEAR(rte_rdtsc() - start, 1E7);
 }
 
 void
@@ -83,7 +83,7 @@  set_tsc_freq(void)
 	if (!freq)
 		freq = estimate_tsc_freq();
 
-	RTE_LOG(DEBUG, EAL, "TSC frequency is ~%" PRIu64 " KHz\n", freq / 1000);
+	RTE_LOG(INFO, EAL, "TSC frequency is ~%" PRIu64 " Hz\n", freq);
 	eal_tsc_resolution_hz = freq;
 }
 
diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h
index 66cdf60b2..e374b16b1 100644
--- a/lib/librte_eal/common/include/rte_common.h
+++ b/lib/librte_eal/common/include/rte_common.h
@@ -248,6 +248,16 @@  static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
 	((v / ((typeof(v))(mul))) * (typeof(v))(mul))
 
+/**
+ * Macro to align a value to the nearest multiple of given value.
+ */
+#define RTE_ALIGN_MUL_NEAR(v, mul)				\
+	({							\
+		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
+		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
+		(ceil - v) > (v - floor) ? floor: ceil;		\
+	})
+
 /**
  * Checks if a pointer is aligned to a given power-of-two value
  *
diff --git a/lib/librte_eal/linuxapp/eal/eal_timer.c b/lib/librte_eal/linuxapp/eal/eal_timer.c
index bc8f05199..864d6ef29 100644
--- a/lib/librte_eal/linuxapp/eal/eal_timer.c
+++ b/lib/librte_eal/linuxapp/eal/eal_timer.c
@@ -248,7 +248,7 @@  get_tsc_freq(void)
 
 		double secs = (double)ns/NS_PER_SEC;
 		tsc_hz = (uint64_t)((end - start)/secs);
-		return tsc_hz;
+		return RTE_ALIGN_MUL_NEAR(tsc_hz, 1E7);
 	}
 #endif
 	return 0;