Skip to contentSkip to conversation

test/lcores: reduce cpu consumption

PATCH Superseded David Marchand 5 participants 9 messages Mar 7, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test/lcores: reduce cpu consumption

Busy looping on RTE_MAX_LCORES threads is too heavy in some CI environments running the fast-test testsuite.
Ask for a reschedule at the threads synchronisation points.

---
Note: this is a quick patch with no validation, except that it runs fine on my laptop.
Luca, can you check and see if it helps in your CI?

---
 app/test/test_lcores.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
app/test/test_lcores.c +6 -5
index 22225a9fd3..7adc03d3da 100644
@@ -2,6 +2,7 @@
  * Copyright (c) 2020 Red Hat, Inc.
  */
 
+#include <sched.h>
 #include <string.h>
 
 #include <rte_common.h>
@@ -43,7 +44,7 @@ static uint32_t thread_loop(void *arg)
 
 	/* Wait for release from the control thread. */
 	while (__atomic_load_n(t->registered_count, __ATOMIC_ACQUIRE) != 0)
-		;
+		sched_yield();
 	rte_thread_unregister();
 	lcore_id = rte_lcore_id();
 	if (lcore_id != LCORE_ID_ANY) {
@@ -85,7 +86,7 @@ test_non_eal_lcores(unsigned int eal_threads_count)
 	/* Wait all non-EAL threads to register. */
 	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
 			non_eal_threads_count)
-		;
+		sched_yield();
 
 	/* We managed to create the max number of threads, let's try to create
 	 * one more. This will allow one more check.
@@ -101,7 +102,7 @@ test_non_eal_lcores(unsigned int eal_threads_count)
 		printf("non-EAL threads count: %u\n", non_eal_threads_count);
 		while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
 				non_eal_threads_count)
-			;
+			sched_yield();
 	}
 
 skip_lcore_any:
@@ -267,7 +268,7 @@ test_non_eal_lcores_callback(unsigned int eal_threads_count)
 	non_eal_threads_count++;
 	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
 			non_eal_threads_count)
-		;
+		sched_yield();
 	if (l[0].init != eal_threads_count + 1 ||
 			l[1].init != eal_threads_count + 1) {
 		printf("Error: incorrect init calls, expected %u, %u, got %u, %u\n",
@@ -290,7 +291,7 @@ test_non_eal_lcores_callback(unsigned int eal_threads_count)
 	non_eal_threads_count++;
 	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
 			non_eal_threads_count)
-		;
+		sched_yield();
 	if (l[0].init != eal_threads_count + 2 ||
 			l[1].init != eal_threads_count + 2) {
 		printf("Error: incorrect init calls, expected %u, %u, got %u, %u\n",
Signature · 1 line
2.44.0
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · David Marchand

Busy looping on RTE_MAX_LCORES threads is too heavy in some CI environments running the fast-test testsuite.
Ask for a reschedule at the threads synchronisation points.

---
Note: this is a quick patch with no validation, except that it runs fine on my laptop.
Luca, can you check and see if it helps in your CI?

I hit problems in the actual build system, rather than in a CI, so I don't have a way to test it before doing a new release. That said, the change looks good to me in principle and we can certainly try it, thanks.

#2 lore raw

PATCH v2 1/1 test/lcores: reduce cpu consumption

In reply to David Marchand

Busy looping on RTE_MAX_LCORES threads is too heavy in some CI or build systems running the fast-test testsuite.
Ask for a reschedule at the threads synchronisation points.

---
Changes since v1:
- fix build with mingw,

---
 app/test/test_lcores.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
app/test/test_lcores.c +15 -5
index 22225a9fd3..08c4e8dfba 100644
@@ -2,7 +2,9 @@
  * Copyright (c) 2020 Red Hat, Inc.
  */
 
+#include <sched.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <rte_common.h>
 #include <rte_errno.h>
@@ -11,6 +13,14 @@
 
 #include "test.h"
 
+#ifndef _POSIX_PRIORITY_SCHEDULING
+/* sched_yield(2):
+ * POSIX  systems on which sched_yield() is available define _POSIX_PRIOR‐
+ * ITY_SCHEDULING in <unistd.h>.
+ */
+#define sched_yield()
+#endif
+
 struct thread_context {
 	enum { Thread_INIT, Thread_ERROR, Thread_DONE } state;
 	bool lcore_id_any;
@@ -43,7 +53,7 @@ static uint32_t thread_loop(void *arg)
 
 	/* Wait for release from the control thread. */
 	while (__atomic_load_n(t->registered_count, __ATOMIC_ACQUIRE) != 0)
-		;
+		sched_yield();
 	rte_thread_unregister();
 	lcore_id = rte_lcore_id();
 	if (lcore_id != LCORE_ID_ANY) {
@@ -85,7 +95,7 @@ test_non_eal_lcores(unsigned int eal_threads_count)
 	/* Wait all non-EAL threads to register. */
 	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
 			non_eal_threads_count)
-		;
+		sched_yield();
 
 	/* We managed to create the max number of threads, let's try to create
 	 * one more. This will allow one more check.
@@ -101,7 +111,7 @@ test_non_eal_lcores(unsigned int eal_threads_count)
 		printf("non-EAL threads count: %u\n", non_eal_threads_count);
 		while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
 				non_eal_threads_count)
-			;
+			sched_yield();
 	}
 
 skip_lcore_any:
@@ -267,7 +277,7 @@ test_non_eal_lcores_callback(unsigned int eal_threads_count)
 	non_eal_threads_count++;
 	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
 			non_eal_threads_count)
-		;
+		sched_yield();
 	if (l[0].init != eal_threads_count + 1 ||
 			l[1].init != eal_threads_count + 1) {
 		printf("Error: incorrect init calls, expected %u, %u, got %u, %u\n",
@@ -290,7 +300,7 @@ test_non_eal_lcores_callback(unsigned int eal_threads_count)
 	non_eal_threads_count++;
 	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
 			non_eal_threads_count)
-		;
+		sched_yield();
 	if (l[0].init != eal_threads_count + 2 ||
 			l[1].init != eal_threads_count + 2) {
 		printf("Error: incorrect init calls, expected %u, %u, got %u, %u\n",
Signature · 1 line
2.44.0
Tested-by on v2 1/1
#3 lore raw
Quoted 7 lines · On Thu, 7 Mar 2024 15:16:06 +0100 David Marchand <[email protected]>…

Busy looping on RTE_MAX_LCORES threads is too heavy in some CI or build systems running the fast-test testsuite.
Ask for a reschedule at the threads synchronisation points.

---

That test was always failing on my little desktop machine, now it works.

#4 lore raw
In reply to David Marchand

app/test/test_lcores.c line 22

Quoted 7 lines · On Thu, 7 Mar 2024 15:16:06 +0100 David Marchand <[email protected]>…
app/test/test_lcores.c +7 -0
+#ifndef _POSIX_PRIORITY_SCHEDULING
+/* sched_yield(2):
+ * POSIX  systems on which sched_yield() is available define _POSIX_PRIOR‐
+ * ITY_SCHEDULING in <unistd.h>.
+ */
+#define sched_yield()
+#endif

Could you fix the awkward line break in that comment before merging :-)

author Applied
#5 lore raw
In reply to David Marchand
Quoted 7 lines · David Marchand

Busy looping on RTE_MAX_LCORES threads is too heavy in some CI or build systems running the fast-test testsuite.
Ask for a reschedule at the threads synchronisation points.

Ideally, this test should be rewritten with some kind of OS-agnostic synchronisation/scheduling API (mutex?).
But I think it will be enough for now.

I updated the code comment as requested by Stephen.

Applied, thanks.

Signature · 1 line
David Marchand
#6 lore raw
Quoted 6 lines · David Marchand
Quoted 5 lines · David Marchand

Busy looping on RTE_MAX_LCORES threads is too heavy in some CI or build systems running the fast-test testsuite.
Ask for a reschedule at the threads synchronisation points.

Quoted 6 lines
Quoted 2 lines

Ideally, this test should be rewritten with some kind of OS-agnostic synchronisation/scheduling API (mutex?).
But I think it will be enough for now.

It's okay, I'll eventually get to this :)

Quoted 7 lines

I updated the code comment as requested by Stephen.

Applied, thanks.

Signature · 1 line
David Marchand
#7 lore raw
Quoted 12 lines

From: Tyler Retzlaff [mailto:[email protected]]
Sent: Thursday, 7 March 2024 19.37

Quoted 1 line · David Marchand

On Thu, Mar 7, 2024 at 3:16 PM David Marchand

Quoted 2 lines · <[email protected]> wrote:
Quoted 2 lines

Busy looping on RTE_MAX_LCORES threads is too heavy in some CI or

build

Quoted 3 lines
Quoted 3 lines

systems running the fast-test testsuite.
Ask for a reschedule at the threads synchronisation points.

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

Ideally, this test should be rewritten with some kind of OS-agnostic synchronisation/scheduling API (mutex?).
But I think it will be enough for now.

It's okay, I'll eventually get to this :)

For future reference, it seems SwitchToThread() [1] resembles sched_yield() [2].

[1]: https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-switchtothread
[2]: https://linux.die.net/man/2/sched_yield

Quoted 7 lines
Quoted 7 lines

I updated the code comment as requested by Stephen.

Applied, thanks.

--
David Marchand

Acked-by on v1 1/1
#8 lore raw
In reply to David Marchand
Quoted 5 lines · On Thu, 7 Mar 2024 12:33:24 +0100 David Marchand <[email protected]>…

Busy looping on RTE_MAX_LCORES threads is too heavy in some CI environments running the fast-test testsuite.
Ask for a reschedule at the threads synchronisation points.

You could use rte_pause here but full yield is better for test cases.