DPDK patches and discussions
 help / color / mirror / Atom feed
From: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH 1/2] eal: use sysctl in BSD to set TSC freq
Date: Thu, 20 Nov 2014 14:06:58 +0000	[thread overview]
Message-ID: <1416492419-11957-2-git-send-email-sergio.gonzalez.monroy@intel.com> (raw)
In-Reply-To: <1416492419-11957-1-git-send-email-sergio.gonzalez.monroy@intel.com>

BSD provides the TSC frequency value through sysctl.

Signed-off-by: Sergio Gonzalez Monroy <sergio.gonzalez.monroy@intel.com>
---
 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 <sys/mman.h>
 #include <sys/queue.h>
 #include <pthread.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
 #include <errno.h>
 
 #include <rte_common.h>
@@ -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",
-- 
2.1.0

  reply	other threads:[~2014-11-20 13:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-20 14:06 [dpdk-dev] [PATCH 0/2] BSD timer cleanup/update Sergio Gonzalez Monroy
2014-11-20 14:06 ` Sergio Gonzalez Monroy [this message]
2014-11-20 14:06 ` [dpdk-dev] [PATCH 2/2] eal: Remove unused HPET support from FreeBSD Sergio Gonzalez Monroy
2014-11-27 11:57 ` [dpdk-dev] [PATCH 0/2] BSD timer cleanup/update Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1416492419-11957-2-git-send-email-sergio.gonzalez.monroy@intel.com \
    --to=sergio.gonzalez.monroy@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).