Skip to contentSkip to conversation

examples/timer: fix incorrect time interval

PATCH Superseded Min Hu (Connor) 4 participants 10 messages Apr 15, 2021 – May 12, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 examples/timer: fix incorrect time interval

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

In addition, the rte_rdtsc() is replaced with the more recommended rte_get_timer_cycles function in this patch.

Fixes af75078fece3 ("first public release")
---
 examples/timer/main.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
examples/timer/main.c +12 -10
index 5a57e48..05f4a9f 100644
@@ -18,8 +18,7 @@
 #include <rte_timer.h>
 #include <rte_debug.h>
 
-#define TIMER_RESOLUTION_CYCLES 20000000ULL /* around 10ms at 2 Ghz */
-
+static uint64_t timer_resolution_cycles;
 static struct rte_timer timer0;
 static struct rte_timer timer1;
 
@@ -66,15 +65,16 @@ lcore_mainloop(__rte_unused void *arg)
 
 	while (1) {
 		/*
-		 * Call the timer handler on each core: as we don't
-		 * need a very precise timer, so only call
-		 * rte_timer_manage() every ~10ms (at 2Ghz). In a real
-		 * application, this will enhance performances as
-		 * reading the HPET timer is not efficient.
+		 * Call the timer handler on each core: as we don't need a
+		 * very precise timer, so only call rte_timer_manage()
+		 * every ~10ms. since rte_eal_hpet_init() has not been
+		 * called, the rte_rdtsc() will be used at runtime.
+		 * In a real application, this will enhance performances
+		 * as reading the HPET timer is not efficient.
 		 */
-		cur_tsc = rte_rdtsc();
+		cur_tsc = rte_get_timer_cycles();
 		diff_tsc = cur_tsc - prev_tsc;
-		if (diff_tsc > TIMER_RESOLUTION_CYCLES) {
+		if (diff_tsc > timer_resolution_cycles) {
 			rte_timer_manage();
 			prev_tsc = cur_tsc;
 		}
@@ -100,8 +100,10 @@ main(int argc, char **argv)
 	rte_timer_init(&timer0);
 	rte_timer_init(&timer1);
 
-	/* load timer0, every second, on main lcore, reloaded automatically */
 	hz = rte_get_timer_hz();
+	timer_resolution_cycles = hz * 10 / 1000; /* around 10ms */
+
+	/* load timer0, every second, on main lcore, reloaded automatically */
 	lcore_id = rte_lcore_id();
 	rte_timer_reset(&timer0, hz, PERIODICAL, lcore_id, timer0_cb, NULL);
 
Signature · 1 line
2.7.4
#1 lore raw

15/04/2021 09:12, Min Hu (Connor):

Quoted 6 lines

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

So rte_timer_manage() will be called less often, yes.

Quoted 2 lines

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

I am not sure about making the example more complex. What is the issue with the previous value?

#2 lore raw

Hi

Quoted 8 lines · Thomas Monjalon

15/04/2021 09:12, Min Hu (Connor):

Quoted 6 lines

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

Quoted 8 lines

So rte_timer_manage() will be called less often, yes.

Quoted 2 lines

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

I am not sure about making the example more complex. What is the issue with the previous value?

In my understanding, the example should illustrate the standard usage of related functions. Some of our customers did not know the difference in tsc frequency between arm and x86 when using our SoC. As a result, some misunderstanding are caused. So I think I could explain a more general approach in the example, which will help these new users. When using a timer, we must first know its frequency. So I've added a frequency acquisition process to the example, so that new users can realize that there are differences between different platforms, so that they can design more general programs.

Quoted 5 lines

.

Acked-by on v1 1/1
#3 lore raw
In reply to Min Hu (Connor)
Quoted 18 lines

-----Original Message-----
From: Min Hu (Connor) <[email protected]>
Sent: Thursday, April 15, 2021 2:12 AM
To: [email protected]
Cc: Yigit, Ferruh <[email protected]>; [email protected]; Carrillo, Erik G <[email protected]>
Subject: [PATCH] examples/timer: fix incorrect time interval

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

Quoted 8 lines

In addition, the rte_rdtsc() is replaced with the more recommended rte_get_timer_cycles function in this patch.

Fixes af75078fece3 ("first public release")

This LGTM - thanks.

#4 lore raw
In reply to Min Hu (Connor)

15/04/2021 09:12, Min Hu (Connor):

Quoted 18 lines

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

In addition, the rte_rdtsc() is replaced with the more recommended rte_get_timer_cycles function in this patch.

Fixes af75078fece3 ("first public release")

[...]

examples/timer/main.c lcore_mainloop(__rte_unused void *arg) · line 71

Quoted 10 lines
examples/timer/main.c +4 -5
 		/*
-		 * Call the timer handler on each core: as we don't
-		 * need a very precise timer, so only call
-		 * rte_timer_manage() every ~10ms (at 2Ghz). In a real
-		 * application, this will enhance performances as
-		 * reading the HPET timer is not efficient.
+		 * Call the timer handler on each core: as we don't need a
+		 * very precise timer, so only call rte_timer_manage()
+		 * every ~10ms. since rte_eal_hpet_init() has not been
+		 * called, the rte_rdtsc() will be used at runtime.

I don't understand this last sentence.

Quoted 5 lines
examples/timer/main.c +3 -1
+		 * In a real application, this will enhance performances
+		 * as reading the HPET timer is not efficient.
 		 */
-		cur_tsc = rte_rdtsc();
+		cur_tsc = rte_get_timer_cycles();
#5 lore raw
Quoted 20 lines · Thomas Monjalon

15/04/2021 09:12, Min Hu (Connor):

Quoted 18 lines

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

In addition, the rte_rdtsc() is replaced with the more recommended rte_get_timer_cycles function in this patch.

Fixes af75078fece3 ("first public release")

[...]

Quoted 13 lines

examples/timer/main.c lcore_mainloop(__rte_unused void *arg) · line 71

Quoted 10 lines
examples/timer/main.c +4 -5
 		/*
-		 * Call the timer handler on each core: as we don't
-		 * need a very precise timer, so only call
-		 * rte_timer_manage() every ~10ms (at 2Ghz). In a real
-		 * application, this will enhance performances as
-		 * reading the HPET timer is not efficient.
+		 * Call the timer handler on each core: as we don't need a
+		 * very precise timer, so only call rte_timer_manage()
+		 * every ~10ms. since rte_eal_hpet_init() has not been
+		 * called, the rte_rdtsc() will be used at runtime.

I don't understand this last sentence.

This is explaining why we can use rte_get_timer_cycles() instead of rte_rdtsc(). In this example, we call tsc to improve its performance. So, we invoked rte_rdtsc() here. Now the function rte_get_timer_cycles() encapsulates these counters. It will invoke the corresponding counter according to the user's initialization of the counter.

Quoted 11 lines

examples/timer/main.c lcore_mainloop(__rte_unused void *arg) · line 75

Quoted 5 lines
examples/timer/main.c +3 -1
+		 * In a real application, this will enhance performances
+		 * as reading the HPET timer is not efficient.
 		 */
-		cur_tsc = rte_rdtsc();
+		cur_tsc = rte_get_timer_cycles();

.

#6 lore raw

06/05/2021 04:06, Chengchang Tang:

Quoted 36 lines · Thomas Monjalon
Quoted 20 lines

15/04/2021 09:12, Min Hu (Connor):

Quoted 18 lines

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

In addition, the rte_rdtsc() is replaced with the more recommended rte_get_timer_cycles function in this patch.

Fixes af75078fece3 ("first public release")

[...]

Quoted 13 lines

examples/timer/main.c lcore_mainloop(__rte_unused void *arg) · line 71

Quoted 10 lines
examples/timer/main.c +4 -5
 		/*
-		 * Call the timer handler on each core: as we don't
-		 * need a very precise timer, so only call
-		 * rte_timer_manage() every ~10ms (at 2Ghz). In a real
-		 * application, this will enhance performances as
-		 * reading the HPET timer is not efficient.
+		 * Call the timer handler on each core: as we don't need a
+		 * very precise timer, so only call rte_timer_manage()
+		 * every ~10ms. since rte_eal_hpet_init() has not been
+		 * called, the rte_rdtsc() will be used at runtime.

I don't understand this last sentence.

Quoted 4 lines

This is explaining why we can use rte_get_timer_cycles() instead of rte_rdtsc(). In this example, we call tsc to improve its performance. So, we invoked rte_rdtsc() here. Now the function rte_get_timer_cycles() encapsulates these counters. It will invoke the corresponding counter according to the user's initialization of the counter.

That's very confusing. Better to drop.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
examples/timer/main.c +3 -1
+		 * In a real application, this will enhance performances
+		 * as reading the HPET timer is not efficient.
 		 */
-		cur_tsc = rte_rdtsc();
+		cur_tsc = rte_get_timer_cycles();
#7 lore raw
Quoted 37 lines · Thomas Monjalon

06/05/2021 04:06, Chengchang Tang:

Quoted 36 lines · Thomas Monjalon
Quoted 33 lines

15/04/2021 09:12, Min Hu (Connor):

Quoted 18 lines

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

In addition, the rte_rdtsc() is replaced with the more recommended rte_get_timer_cycles function in this patch.

Fixes af75078fece3 ("first public release")

[...]

examples/timer/main.c lcore_mainloop(__rte_unused void *arg) · line 71

Quoted 10 lines
examples/timer/main.c +4 -5
 		/*
-		 * Call the timer handler on each core: as we don't
-		 * need a very precise timer, so only call
-		 * rte_timer_manage() every ~10ms (at 2Ghz). In a real
-		 * application, this will enhance performances as
-		 * reading the HPET timer is not efficient.
+		 * Call the timer handler on each core: as we don't need a
+		 * very precise timer, so only call rte_timer_manage()
+		 * every ~10ms. since rte_eal_hpet_init() has not been
+		 * called, the rte_rdtsc() will be used at runtime.

I don't understand this last sentence.

Quoted 7 lines
Quoted 4 lines

This is explaining why we can use rte_get_timer_cycles() instead of rte_rdtsc(). In this example, we call tsc to improve its performance. So, we invoked rte_rdtsc() here. Now the function rte_get_timer_cycles() encapsulates these counters. It will invoke the corresponding counter according to the user's initialization of the counter.

That's very confusing. Better to drop.

OK, I will remove this sentence in the next version.

Quoted 12 lines
Quoted 6 lines
Quoted 6 lines

examples/timer/main.c lcore_mainloop(__rte_unused void *arg) · line 75

Quoted 6 lines
examples/timer/main.c +3 -1
+		 * In a real application, this will enhance performances
+		 * as reading the HPET timer is not efficient.
 		 */
-		cur_tsc = rte_rdtsc();
+		cur_tsc = rte_get_timer_cycles();

.

#8 lore raw

PATCH v2 1/1 examples/timer: fix incorrect time interval

In reply to Min Hu (Connor)

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

In addition, the rte_rdtsc() is replaced with the more recommended rte_get_timer_cycles function in this patch.

Fixes af75078fece3 ("first public release")

---
v2:
* delete confusing comments.

---
 examples/timer/main.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
examples/timer/main.c +10 -10
index d67301e..d270ce4 100644
@@ -18,8 +18,7 @@
 #include <rte_timer.h>
 #include <rte_debug.h>
 
-#define TIMER_RESOLUTION_CYCLES 20000000ULL /* around 10ms at 2 Ghz */
-
+static uint64_t timer_resolution_cycles;
 static struct rte_timer timer0;
 static struct rte_timer timer1;
 
@@ -66,15 +65,14 @@ lcore_mainloop(__rte_unused void *arg)
 
 	while (1) {
 		/*
-		 * Call the timer handler on each core: as we don't
-		 * need a very precise timer, so only call
-		 * rte_timer_manage() every ~10ms (at 2Ghz). In a real
-		 * application, this will enhance performances as
-		 * reading the HPET timer is not efficient.
+		 * Call the timer handler on each core: as we don't need a
+		 * very precise timer, so only call rte_timer_manage()
+		 * every ~10ms. In a real application, this will enhance
+		 * performances as reading the HPET timer is not efficient.
 		 */
-		cur_tsc = rte_rdtsc();
+		cur_tsc = rte_get_timer_cycles();
 		diff_tsc = cur_tsc - prev_tsc;
-		if (diff_tsc > TIMER_RESOLUTION_CYCLES) {
+		if (diff_tsc > timer_resolution_cycles) {
 			rte_timer_manage();
 			prev_tsc = cur_tsc;
 		}
@@ -100,8 +98,10 @@ main(int argc, char **argv)
 	rte_timer_init(&timer0);
 	rte_timer_init(&timer1);
 
-	/* load timer0, every second, on main lcore, reloaded automatically */
 	hz = rte_get_timer_hz();
+	timer_resolution_cycles = hz * 10 / 1000; /* around 10ms */
+
+	/* load timer0, every second, on main lcore, reloaded automatically */
 	lcore_id = rte_lcore_id();
 	rte_timer_reset(&timer0, hz, PERIODICAL, lcore_id, timer0_cb, NULL);
 
Signature · 1 line
2.7.4
#9 lore raw

06/05/2021 11:29, Min Hu (Connor):

Quoted 10 lines

From: Chengchang Tang <[email protected]>

Timer sample example assumes that the frequency of the timer is about 2Ghz to control the period of calling rte_timer_manage(). But this assumption is easy to fail. For example. the frequency of tsc on ARM64 is much less than 2Ghz.

This patch uses the frequency of the current timer to calculate the correct time interval to ensure consistent result on all platforms.

Quoted 8 lines

In addition, the rte_rdtsc() is replaced with the more recommended rte_get_timer_cycles function in this patch.

Fixes af75078fece3 ("first public release")

Applied, thanks