[PATCH v4 2/2] timer: allow platform to override cpu TSC frequency

Bruce Richardson bruce.richardson at intel.com
Wed Oct 2 19:11:47 CEST 2024


On Wed, Oct 02, 2024 at 07:56:36PM +0300, Isaac Boukris wrote:
> The CPU value is often not accurate, allow overriding it based
> on info from the host OS.
> 
> On Linux X86, if the tsc_known_freq cpu flag is missing, it means
> the kernel doesn't trust it and calculates its own. We should do
> the same to avoid drift.
> 
> On freebsd we have access to the kernel tsc_hz value, just use it.
> 
> Signed-off-by: Isaac Boukris <iboukris at gmail.com>
> ---

Looks good to me. Hope to test it out soon on my systems to see how it
goes.

One comment inline below.

/Bruce

>  lib/eal/common/eal_common_timer.c |  3 +-
>  lib/eal/common/eal_private.h      |  2 +-
>  lib/eal/freebsd/eal_timer.c       |  8 +++--
>  lib/eal/linux/eal_timer.c         | 53 +++++++++++++++++++++++++++++--
>  lib/eal/windows/eal_timer.c       |  5 ++-
>  5 files changed, 62 insertions(+), 9 deletions(-)
> 
> diff --git a/lib/eal/common/eal_common_timer.c b/lib/eal/common/eal_common_timer.c
> index c5c4703f15..e00be0a5c8 100644
> --- a/lib/eal/common/eal_common_timer.c
> +++ b/lib/eal/common/eal_common_timer.c
> @@ -66,8 +66,7 @@ set_tsc_freq(void)
>  	}
>  
>  	freq = get_tsc_freq_arch();
> -	if (!freq)
> -		freq = get_tsc_freq();
> +	freq = get_tsc_freq(freq);
>  	if (!freq)
>  		freq = estimate_tsc_freq();
>  
> diff --git a/lib/eal/common/eal_private.h b/lib/eal/common/eal_private.h
> index af09620426..bb315dab04 100644
> --- a/lib/eal/common/eal_private.h
> +++ b/lib/eal/common/eal_private.h
> @@ -374,7 +374,7 @@ void set_tsc_freq(void);
>   *
>   * This function is private to the EAL.
>   */
> -uint64_t get_tsc_freq(void);
> +uint64_t get_tsc_freq(uint64_t arch_hz);
>  
>  /**
>   * Get TSC frequency if the architecture supports.
> diff --git a/lib/eal/freebsd/eal_timer.c b/lib/eal/freebsd/eal_timer.c
> index 3dd70e24ba..19fc9a7228 100644
> --- a/lib/eal/freebsd/eal_timer.c
> +++ b/lib/eal/freebsd/eal_timer.c
> @@ -26,7 +26,7 @@
>  enum timer_source eal_timer_source = EAL_TIMER_TSC;
>  
>  uint64_t
> -get_tsc_freq(void)
> +get_tsc_freq(uint64_t arch_hz)
>  {
>  	size_t sz;
>  	int tmp;
> @@ -50,9 +50,13 @@ get_tsc_freq(void)
>  	sz = sizeof(tsc_hz);
>  	if (sysctlbyname("machdep.tsc_freq", &tsc_hz, &sz, NULL, 0)) {
>  		EAL_LOG(WARNING, "%s", strerror(errno));
> -		return 0;
> +		return arch_hz;
>  	}
>  
> +	if (arch_hz && arch_hz - tsc_hz > arch_hz / 100)

Do we not need an "abs()" call on the "arch_hz - tsc_hz" in case tsc_hz is
the bigger value?



More information about the dev mailing list