From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 11B9445A72; Tue, 1 Oct 2024 02:26:08 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 125B74060B; Tue, 1 Oct 2024 02:26:06 +0200 (CEST) Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) by mails.dpdk.org (Postfix) with ESMTP id 2E1C5402CB for ; Tue, 1 Oct 2024 02:26:04 +0200 (CEST) Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5c46c2bf490so2887375a12.3 for ; Mon, 30 Sep 2024 17:26:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727742363; x=1728347163; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ds/D+e3U5gACBvCquWEyfEw83KL2mei6Mj5Nyl7q1es=; b=lLHw5DC1VmLuwsc/Dp5KD3PJ4u3ZMdggAwTVimClhVNIcwmaA7nJ1UZzGUUv3c4Tbn Gl4z2LxmxyZOl6QqVK6m2FkI0Hw6v/+m4Il94kTA7uWU/UnHdLTRL30Bf7Fcq+SISe+A Y0dAfKXGb27Al2gCPSg+YTSDaXhVZ4LNeOboQwSwHNArDiZ8I7ycAtYal1lPZKNjLYkZ IT94Gt0DT5LfxQrY9Ctb0G6l8iBNDIugn5rwFBJkfttn1ABH+4oRPsYNfuDnzpmZCA6d A0jM8rAYqAVj3FCxNEoQJZCfejcZBvoENd4miqCFthJAGLf9SwpGp3RW2ESx6MWORhcD y3pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727742363; x=1728347163; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ds/D+e3U5gACBvCquWEyfEw83KL2mei6Mj5Nyl7q1es=; b=E05GAg4Hpf/MaSSw/Ws+ZlCY4JWI4zB/3HZIAWhGKKFWpyTiIs5aweRxWsE15IiJx/ M+C9LDROnx6VRwyE6i+6GmzjehdYHKL+pupyzosI020tHveJBOWH2tdHaMuoK31FVPGG uuL1Se5kXfT40gGSiZJwPXwXfaHv3mkeXVZYc/dHB0v9lpb9yu8iDEIaecfW8UZbb5CM aoLoNHeBDj3A8C18vy3DVJU55oUstKzPwm6aF68em3xjtMfyZzBmNZ/IyihUtzKz587P 0lWYcQmRl8y9XJ1S00YkQEnm6c6rVzLcfMwO+fhas4GZyiy84PwI7WL6XZK/v4kVqvLr apBQ== X-Gm-Message-State: AOJu0YxSfVLuc0G70bNpofTBQDwEbmBP4CA3qa6tuyubmb8ILomigI9L +nYXsMMdUQgAz0+7fuekuwMKppPMA2doLyDAoOGbPEGrPNwx4XHOpiom+ZbO X-Google-Smtp-Source: AGHT+IHmWTIboKzpJXrHJFZUx9XkvzOHX0X4GrXlLgXbde7vDMu+OZM45ixD06tV9rzVg6WHKgBcow== X-Received: by 2002:a05:6402:34c4:b0:5c8:8d5e:14e1 with SMTP id 4fb4d7f45d1cf-5c88d5e1598mr12293777a12.15.1727742362817; Mon, 30 Sep 2024 17:26:02 -0700 (PDT) Received: from localhost.localdomain ([176.12.188.220]) by smtp.googlemail.com with ESMTPSA id 4fb4d7f45d1cf-5c8824056adsm5179335a12.17.2024.09.30.17.25.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Sep 2024 17:26:02 -0700 (PDT) From: Isaac Boukris To: dev@dpdk.org Cc: stephen@networkplumber.org, bruce.richardson@intel.com, roretzla@linux.microsoft.com, dmitry.kozliuk@gmail.com, david.marchand@redhat.com, Isaac Boukris Subject: [PATCH v3 2/2] timer/linux/x86: override TSC freq if no tsc_known_freq Date: Tue, 1 Oct 2024 03:22:51 +0300 Message-ID: <20241001002527.277838-3-iboukris@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20241001002527.277838-1-iboukris@gmail.com> References: <20240921140022.107239-1-iboukris@gmail.com> <20241001002527.277838-1-iboukris@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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. Signed-off-by: Isaac Boukris --- lib/eal/common/eal_common_timer.c | 3 +- lib/eal/common/eal_private.h | 2 +- lib/eal/freebsd/eal_timer.c | 5 ++- lib/eal/linux/eal_timer.c | 53 +++++++++++++++++++++++++++++-- lib/eal/windows/eal_timer.c | 5 ++- 5 files changed, 60 insertions(+), 8 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..5a8aea03e1 100644 --- a/lib/eal/freebsd/eal_timer.c +++ b/lib/eal/freebsd/eal_timer.c @@ -26,12 +26,15 @@ 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; uint64_t tsc_hz; + if (arch_hz) + return arch_hz; + sz = sizeof(tmp); tmp = 0; diff --git a/lib/eal/linux/eal_timer.c b/lib/eal/linux/eal_timer.c index f56a7ae15b..2c5fc9ff3e 100644 --- a/lib/eal/linux/eal_timer.c +++ b/lib/eal/linux/eal_timer.c @@ -5,9 +5,9 @@ #include #include +#include #ifdef RTE_LIBEAL_USE_HPET #include -#include #include #include #endif @@ -187,8 +187,41 @@ rte_eal_hpet_init(int make_default) } #endif +/* Check if the kernel deems the arch provided TSC frequency trustworthy. */ + +static bool +is_tsc_known_freq(void) +{ + bool ret = true; /* Assume tsc_known_freq */ + +#if defined(RTE_ARCH_X86) + char line[2048]; + FILE *stream; + + stream = fopen("/proc/cpuinfo", "r"); + if (!stream) { + EAL_LOG(WARNING, "Unable to open /proc/cpuinfo"); + return ret; + } + + while (fgets(line, sizeof(line), stream)) { + if (strncmp(line, "flags", 5) != 0) + continue; + + if (!strstr(line, "tsc_known_freq")) + ret = false; + + break; + } + + fclose(stream); +#endif + + return ret; +} + uint64_t -get_tsc_freq(void) +get_tsc_freq(uint64_t arch_hz) { #ifdef CLOCK_MONOTONIC_RAW #define NS_PER_SEC 1E9 @@ -199,6 +232,9 @@ get_tsc_freq(void) struct timespec t_start, t_end; uint64_t tsc_hz; + if (arch_hz && is_tsc_known_freq()) + return arch_hz; + if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) { uint64_t ns, end, start = rte_rdtsc(); nanosleep(&sleeptime,NULL); @@ -209,11 +245,22 @@ get_tsc_freq(void) double secs = (double)ns/NS_PER_SEC; tsc_hz = (uint64_t)((end - start)/secs); + + if (arch_hz) { + /* Make sure we're within 1% for sanity check */ + if (arch_hz - tsc_hz > arch_hz / 100) + return arch_hz; + + EAL_LOG(DEBUG, + "Refined arch frequency %"PRIu64" to measured frequency %"PRIu64, + arch_hz, tsc_hz); + } + /* Round up to 100Khz. 1E5 ~ 100Khz */ return RTE_ALIGN_MUL_NEAR(tsc_hz, CYC_PER_100KHZ); } #endif - return 0; + return arch_hz; } int diff --git a/lib/eal/windows/eal_timer.c b/lib/eal/windows/eal_timer.c index b070cb7751..cfd6c267ac 100644 --- a/lib/eal/windows/eal_timer.c +++ b/lib/eal/windows/eal_timer.c @@ -49,13 +49,16 @@ rte_delay_us_sleep(unsigned int us) } uint64_t -get_tsc_freq(void) +get_tsc_freq(uint64_t arch_hz) { LARGE_INTEGER t_start, t_end, elapsed_us; LARGE_INTEGER frequency; uint64_t tsc_hz; uint64_t end, start; + if (arch_hz) + return arch_hz; + QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&t_start); -- 2.45.0