DPDK patches and discussions
 help / color / mirror / Atom feed
From: Ilya Maximets <i.maximets@samsung.com>
To: dev@dpdk.org
Cc: Jingjing Wu <jingjing.wu@intel.com>,
	Konstantin Ananyev <konstantin.ananyev@intel.com>,
	Wenzhuo Lu <wenzhuo.lu@intel.com>,
	Beilei Xing <beilei.xing@intel.com>,
	Qi Zhang <qi.z.zhang@intel.com>,
	Xiao Wang <xiao.w.wang@intel.com>,
	Bruce Richardson <bruce.richardson@intel.com>,
	Ilya Maximets <i.maximets@samsung.com>
Subject: [dpdk-dev] [RFC 1/2] eal: add nanosleep based delay function
Date: Fri, 31 Aug 2018 15:45:16 +0300	[thread overview]
Message-ID: <20180831124402eucas1p120ba1cdc8c3f6bc6c5bd09b5d3ede771~P_LI1zFBt2959529595eucas1p1H@eucas1p1.samsung.com> (raw)
In-Reply-To: <20180831124517.27619-1-i.maximets@samsung.com>

Add a new rte_delay_us_sleep() function that uses nanosleep().
This function can be used by applications to not implement
their own nanosleep() based callback and by internal DPDK
code if CPU non-blocking delay needed.

Signed-off-by: Ilya Maximets <i.maximets@samsung.com>
---
 lib/librte_eal/common/eal_common_timer.c      | 19 +++++++
 .../common/include/generic/rte_cycles.h       | 11 ++++
 lib/librte_eal/rte_eal_version.map            |  1 +
 test/test/autotest_data.py                    |  6 +++
 test/test/meson.build                         |  1 +
 test/test/test_cycles.c                       | 51 ++++++++++++++-----
 6 files changed, 75 insertions(+), 14 deletions(-)

diff --git a/lib/librte_eal/common/eal_common_timer.c b/lib/librte_eal/common/eal_common_timer.c
index 2e2b770fb..4a00525ca 100644
--- a/lib/librte_eal/common/eal_common_timer.c
+++ b/lib/librte_eal/common/eal_common_timer.c
@@ -7,9 +7,11 @@
 #include <unistd.h>
 #include <inttypes.h>
 #include <sys/types.h>
+#include <time.h>
 #include <errno.h>
 
 #include <rte_common.h>
+#include <rte_compat.h>
 #include <rte_log.h>
 #include <rte_cycles.h>
 #include <rte_pause.h>
@@ -31,6 +33,23 @@ rte_delay_us_block(unsigned int us)
 		rte_pause();
 }
 
+void __rte_experimental
+rte_delay_us_sleep(unsigned int us)
+{
+	struct timespec wait[2];
+	int ind = 0;
+
+	wait[0].tv_sec = 0;
+	if (us >= US_PER_S) {
+		wait[0].tv_sec = us / US_PER_S;
+		us -= wait[0].tv_sec * US_PER_S;
+	}
+	wait[0].tv_nsec = 1000 * us;
+
+	while (nanosleep(&wait[ind], &wait[1 - ind]) == EINTR)
+		ind = 1 - ind;
+}
+
 uint64_t
 rte_get_tsc_hz(void)
 {
diff --git a/lib/librte_eal/common/include/generic/rte_cycles.h b/lib/librte_eal/common/include/generic/rte_cycles.h
index 0ff1af504..ac379e878 100644
--- a/lib/librte_eal/common/include/generic/rte_cycles.h
+++ b/lib/librte_eal/common/include/generic/rte_cycles.h
@@ -13,6 +13,7 @@
  */
 
 #include <stdint.h>
+#include <rte_compat.h>
 #include <rte_debug.h>
 #include <rte_atomic.h>
 
@@ -157,6 +158,16 @@ rte_delay_ms(unsigned ms)
  */
 void rte_delay_us_block(unsigned int us);
 
+/**
+ * Delay function that uses system sleep.
+ * Does not block the CPU core.
+ *
+ * @param us
+ *   Number of microseconds to wait.
+ */
+void __rte_experimental
+rte_delay_us_sleep(unsigned int us);
+
 /**
  * Replace rte_delay_us with user defined function.
  *
diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map
index 344a43d32..d68777ce0 100644
--- a/lib/librte_eal/rte_eal_version.map
+++ b/lib/librte_eal/rte_eal_version.map
@@ -270,6 +270,7 @@ EXPERIMENTAL {
 	rte_class_register;
 	rte_class_unregister;
 	rte_ctrl_thread_create;
+	rte_delay_us_sleep;
 	rte_dev_event_callback_register;
 	rte_dev_event_callback_unregister;
 	rte_dev_event_monitor_start;
diff --git a/test/test/autotest_data.py b/test/test/autotest_data.py
index f68d9b111..874d0cb53 100644
--- a/test/test/autotest_data.py
+++ b/test/test/autotest_data.py
@@ -278,6 +278,12 @@
         "Func":    default_autotest,
         "Report":  None,
     },
+    {
+        "Name":    "Sleep delay",
+        "Command": "delay_us_sleep_autotest",
+        "Func":    default_autotest,
+        "Report":  None,
+    },
     {
         "Name":    "Rawdev autotest",
         "Command": "rawdev_autotest",
diff --git a/test/test/meson.build b/test/test/meson.build
index b1dd6eca2..0078aea30 100644
--- a/test/test/meson.build
+++ b/test/test/meson.build
@@ -143,6 +143,7 @@ test_names = [
 	'cryptodev_dpaa_sec_autotest',
 	'cycles_autotest',
 	'debug_autotest',
+	'delay_us_sleep_autotest',
 	'devargs_autotest',
 	'distributor_autotest',
 	'distributor_perf_autotest',
diff --git a/test/test/test_cycles.c b/test/test/test_cycles.c
index 149f60b07..c78e6a5b1 100644
--- a/test/test/test_cycles.c
+++ b/test/test/test_cycles.c
@@ -23,6 +23,30 @@
  *   of cycles is correct with regard to the frequency of the timer.
  */
 
+static int
+check_wait_one_second(void)
+{
+	uint64_t cycles, prev_cycles;
+	uint64_t hz = rte_get_timer_hz();
+	uint64_t max_inc = (hz / 100); /* 10 ms max between 2 reads */
+
+	/* check that waiting 1 second is precise */
+	prev_cycles = rte_get_timer_cycles();
+	rte_delay_us(1000000);
+	cycles = rte_get_timer_cycles();
+
+	if ((uint64_t)(cycles - prev_cycles) > (hz + max_inc)) {
+		printf("delay_us is not accurate: too long\n");
+		return -1;
+	}
+	if ((uint64_t)(cycles - prev_cycles) < (hz - max_inc)) {
+		printf("delay_us is not accurate: too short\n");
+		return -1;
+	}
+
+	return 0;
+}
+
 static int
 test_cycles(void)
 {
@@ -43,24 +67,23 @@ test_cycles(void)
 		prev_cycles = cycles;
 	}
 
-	/* check that waiting 1 second is precise */
-	prev_cycles = rte_get_timer_cycles();
-	rte_delay_us(1000000);
-	cycles = rte_get_timer_cycles();
+	return check_wait_one_second();
+}
 
-	if ((uint64_t)(cycles - prev_cycles) > (hz + max_inc)) {
-		printf("delay_us is not accurate: too long\n");
-		return -1;
-	}
-	if ((uint64_t)(cycles - prev_cycles) < (hz - max_inc)) {
-		printf("delay_us is not accurate: too short\n");
-		return -1;
-	}
+REGISTER_TEST_COMMAND(cycles_autotest, test_cycles);
 
-	return 0;
+/*
+ * One second precision test with rte_delay_us_sleep.
+ */
+
+static int
+test_delay_us_sleep(void)
+{
+	rte_delay_us_callback_register(rte_delay_us_sleep);
+	return check_wait_one_second();
 }
 
-REGISTER_TEST_COMMAND(cycles_autotest, test_cycles);
+REGISTER_TEST_COMMAND(delay_us_sleep_autotest, test_delay_us_sleep);
 
 /*
  * rte_delay_us_callback test
-- 
2.17.1

       reply	other threads:[~2018-08-31 12:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20180831124517.27619-1-i.maximets@samsung.com>
     [not found] ` <CGME20180831124402eucas1p120ba1cdc8c3f6bc6c5bd09b5d3ede771@eucas1p1.samsung.com>
2018-08-31 12:45   ` Ilya Maximets [this message]
     [not found] ` <CGME20180831124404eucas1p20daff43600dfe450c9106616f886eab4@eucas1p2.samsung.com>
2018-08-31 12:45   ` [dpdk-dev] [RFC 2/2] drivers/net: use sleep delay by default for intel NICs Ilya Maximets
2018-09-03 15:14     ` Wiles, Keith
2018-09-03 15:33       ` Ilya Maximets
2018-09-03 15:39         ` Wiles, Keith
2018-09-03 15:50           ` Ilya Maximets
2018-09-03 16:04             ` Wiles, Keith

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='20180831124402eucas1p120ba1cdc8c3f6bc6c5bd09b5d3ede771~P_LI1zFBt2959529595eucas1p1H@eucas1p1.samsung.com' \
    --to=i.maximets@samsung.com \
    --cc=beilei.xing@intel.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=jingjing.wu@intel.com \
    --cc=konstantin.ananyev@intel.com \
    --cc=qi.z.zhang@intel.com \
    --cc=wenzhuo.lu@intel.com \
    --cc=xiao.w.wang@intel.com \
    /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).