From patchwork Thu Nov 20 14:06:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergio Gonzalez Monroy X-Patchwork-Id: 1392 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 15B027F40; Thu, 20 Nov 2014 14:56:34 +0100 (CET) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by dpdk.org (Postfix) with ESMTP id 707177F11 for ; Thu, 20 Nov 2014 14:56:32 +0100 (CET) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga103.fm.intel.com with ESMTP; 20 Nov 2014 06:00:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.97,862,1389772800"; d="scan'208";a="419260740" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by FMSMGA003.fm.intel.com with ESMTP; 20 Nov 2014 05:57:33 -0800 Received: from sivswdev02.ir.intel.com (sivswdev02.ir.intel.com [10.237.217.46]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id sAKE701f008152 for ; Thu, 20 Nov 2014 14:07:00 GMT Received: from sivswdev02.ir.intel.com (localhost [127.0.0.1]) by sivswdev02.ir.intel.com with ESMTP id sAKE6xl9012003 for ; Thu, 20 Nov 2014 14:06:59 GMT Received: (from smonroy@localhost) by sivswdev02.ir.intel.com with id sAKE6xPN011999 for dev@dpdk.org; Thu, 20 Nov 2014 14:06:59 GMT From: Sergio Gonzalez Monroy To: dev@dpdk.org Date: Thu, 20 Nov 2014 14:06:58 +0000 Message-Id: <1416492419-11957-2-git-send-email-sergio.gonzalez.monroy@intel.com> X-Mailer: git-send-email 1.8.5.4 In-Reply-To: <1416492419-11957-1-git-send-email-sergio.gonzalez.monroy@intel.com> References: <1416492419-11957-1-git-send-email-sergio.gonzalez.monroy@intel.com> Subject: [dpdk-dev] [PATCH 1/2] eal: use sysctl in BSD to set TSC freq X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" BSD provides the TSC frequency value through sysctl. Signed-off-by: Sergio Gonzalez Monroy --- lib/librte_eal/bsdapp/eal/eal_timer.c | 46 ++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/librte_eal/bsdapp/eal/eal_timer.c b/lib/librte_eal/bsdapp/eal/eal_timer.c index fd800b9..67da167 100644 --- a/lib/librte_eal/bsdapp/eal/eal_timer.c +++ b/lib/librte_eal/bsdapp/eal/eal_timer.c @@ -41,6 +41,8 @@ #include #include #include +#include +#include #include #include @@ -241,29 +243,34 @@ rte_eal_hpet_init(int make_default) #endif static int -set_tsc_freq_from_clock(void) +set_tsc_freq_from_sysctl(void) { -#ifdef CLOCK_MONOTONIC_RAW -#define NS_PER_SEC 1E9 + size_t sz; + int tmp; - struct timespec sleeptime = {.tv_nsec = 5E8 }; /* 1/2 second */ + sz = sizeof(tmp); + tmp = 0; - struct timespec t_start, t_end; + if (sysctlbyname("kern.timecounter.smp_tsc", &tmp, &sz, NULL, 0)) + RTE_LOG(WARNING, EAL, "%s\n", strerror(errno)); + else if (tmp != 1) + RTE_LOG(WARNING, EAL, "TSC is not safe to use in SMP mode\n"); - if (clock_gettime(CLOCK_MONOTONIC_RAW, &t_start) == 0) { - uint64_t ns, end, start = rte_rdtsc(); - nanosleep(&sleeptime,NULL); - clock_gettime(CLOCK_MONOTONIC_RAW, &t_end); - end = rte_rdtsc(); - ns = ((t_end.tv_sec - t_start.tv_sec) * NS_PER_SEC); - ns += (t_end.tv_nsec - t_start.tv_nsec); + tmp = 0; - double secs = (double)ns/NS_PER_SEC; - eal_tsc_resolution_hz = (uint64_t)((end - start)/secs); - return 0; + if (sysctlbyname("kern.timecounter.invariant_tsc", &tmp, &sz, NULL, 0)) + RTE_LOG(WARNING, EAL, "%s\n", strerror(errno)); + else if (tmp != 1) + RTE_LOG(WARNING, EAL, "TSC is not invariant\n"); + + sz = sizeof(eal_tsc_resolution_hz); + + if (sysctlbyname("machdep.tsc_freq", &eal_tsc_resolution_hz, &sz, NULL, 0)) { + RTE_LOG(WARNING, EAL, "%s\n", strerror(errno)); + return -1; } -#endif - return -1; + + return 0; } static void @@ -277,10 +284,11 @@ set_tsc_freq_fallback(void) sleep(1); eal_tsc_resolution_hz = rte_rdtsc() - start; } + /* * This function measures the TSC frequency. It uses a variety of approaches. * - * 1. If kernel provides CLOCK_MONOTONIC_RAW we use that to tune the TSC value + * 1. Read the TSC frequency value provided by the kernel * 2. If kernel does not provide that, and we have HPET support, tune using HPET * 3. Lastly, if neither of the above can be used, just sleep for 1 second and * tune off that, printing a warning about inaccuracy of timing @@ -288,7 +296,7 @@ set_tsc_freq_fallback(void) static void set_tsc_freq(void) { - if (set_tsc_freq_from_clock() < 0) + if (set_tsc_freq_from_sysctl() < 0) set_tsc_freq_fallback(); RTE_LOG(INFO, EAL, "TSC frequency is ~%"PRIu64" KHz\n",