]> git.droids-corp.org - dpdk.git/commitdiff
eal: cleanup lcore ID hand-over
authorDavid Marchand <david.marchand@redhat.com>
Tue, 5 Apr 2022 16:34:46 +0000 (18:34 +0200)
committerDavid Marchand <david.marchand@redhat.com>
Thu, 14 Apr 2022 11:59:50 +0000 (13:59 +0200)
So far, a worker thread has been using its thread_id to discover which
lcore has been assigned to it.

On the other hand, as noted by Tyler, the pthread API does not strictly
guarantee that a new thread won't start running eal_thread_loop before
pthread_create writes to &lcore_config[xx].thread_id.

Though all OS implementations supported in DPDK (recently) ensure this
property, it is more robust to have the main thread directly pass
the worker thread lcore.

Signed-off-by: David Marchand <david.marchand@redhat.com>
lib/eal/common/eal_thread.h
lib/eal/freebsd/eal.c
lib/eal/freebsd/eal_thread.c
lib/eal/linux/eal.c
lib/eal/linux/eal_thread.c
lib/eal/windows/eal.c
lib/eal/windows/eal_thread.c
lib/eal/windows/eal_windows.h

index 4a49117be894a4ce9667d104969f6260ed63c3fb..3c84efd553745aee60d3a83d6eb241098addf5c3 100644 (file)
@@ -8,10 +8,10 @@
 #include <rte_lcore.h>
 
 /**
- * basic loop of thread, called for each thread by eal_init().
+ * Basic loop of EAL thread, called for each worker thread by rte_eal_init().
  *
  * @param arg
- *   opaque pointer
+ *   The lcore_id (passed as an integer) of this worker thread.
  */
 __rte_noreturn void *eal_thread_loop(void *arg);
 
index 71993fe25b6cb90ed2958e63ca378b86fc738da5..e233e57184c58710ee43e935bc468fa7a4b6e5ec 100644 (file)
@@ -813,7 +813,7 @@ rte_eal_init(int argc, char **argv)
 
                /* create a thread for each lcore */
                ret = pthread_create(&lcore_config[i].thread_id, NULL,
-                                    eal_thread_loop, NULL);
+                                    eal_thread_loop, (void *)(uintptr_t)i);
                if (ret != 0)
                        rte_panic("Cannot create thread\n");
 
index 3b18030d735bf5df340d6ddc01494b3ffd01a8be..9044d70ef77ea219f881c1c7c8d2807580ed98e4 100644 (file)
@@ -76,25 +76,15 @@ finish:
 
 /* main loop of threads */
 __rte_noreturn void *
-eal_thread_loop(__rte_unused void *arg)
+eal_thread_loop(void *arg)
 {
+       unsigned int lcore_id = (uintptr_t)arg;
        char c;
        int n, ret;
        unsigned lcore_id;
-       pthread_t thread_id;
        int m2w, w2m;
        char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 
-       thread_id = pthread_self();
-
-       /* retrieve our lcore_id from the configuration structure */
-       RTE_LCORE_FOREACH_WORKER(lcore_id) {
-               if (thread_id == lcore_config[lcore_id].thread_id)
-                       break;
-       }
-       if (lcore_id == RTE_MAX_LCORE)
-               rte_panic("cannot retrieve lcore id\n");
-
        m2w = lcore_config[lcore_id].pipe_main2worker[0];
        w2m = lcore_config[lcore_id].pipe_worker2main[1];
 
@@ -102,7 +92,7 @@ eal_thread_loop(__rte_unused void *arg)
 
        ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
        RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
-               lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
+               lcore_id, pthread_self(), cpuset, ret == 0 ? "" : "...");
 
        rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
 
index 025e5cc10d3d624e34e0fda6a008155f6cef08a7..98c6838026827e81182016ddf1aa29f820044480 100644 (file)
@@ -1147,7 +1147,7 @@ rte_eal_init(int argc, char **argv)
 
                /* create a thread for each lcore */
                ret = pthread_create(&lcore_config[i].thread_id, NULL,
-                                    eal_thread_loop, NULL);
+                                    eal_thread_loop, (void *)(uintptr_t)i);
                if (ret != 0)
                        rte_panic("Cannot create thread\n");
 
index fa6cd7e2c404fd983d429c38fa0bfb28d880f626..26b0a7d48aa5d22b65c11f06c7c0297cbcaa7596 100644 (file)
@@ -70,24 +70,14 @@ finish:
 
 /* main loop of threads */
 __rte_noreturn void *
-eal_thread_loop(__rte_unused void *arg)
+eal_thread_loop(void *arg)
 {
+       unsigned int lcore_id = (uintptr_t)arg;
        char c;
        int n, ret;
-       unsigned lcore_id;
-       pthread_t thread_id;
        int m2w, w2m;
        char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 
-       thread_id = pthread_self();
-
-       /* retrieve our lcore_id from the configuration structure */
-       RTE_LCORE_FOREACH_WORKER(lcore_id) {
-               if (thread_id == lcore_config[lcore_id].thread_id)
-                       break;
-       }
-       if (lcore_id == RTE_MAX_LCORE)
-               rte_panic("cannot retrieve lcore id\n");
 
        m2w = lcore_config[lcore_id].pipe_main2worker[0];
        w2m = lcore_config[lcore_id].pipe_worker2main[1];
@@ -96,7 +86,8 @@ eal_thread_loop(__rte_unused void *arg)
 
        ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
        RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
-               lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "...");
+               lcore_id, (uintptr_t)pthread_self(), cpuset,
+               ret == 0 ? "" : "...");
 
        rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
 
index ca3c41aaa7f145ca9da6b81e67bffc2aeca8a7d5..1874f9f6d7cd77b820e062714664a96f6f0d64d7 100644 (file)
@@ -420,7 +420,7 @@ rte_eal_init(int argc, char **argv)
                lcore_config[i].state = WAIT;
 
                /* create a thread for each lcore */
-               if (eal_thread_create(&lcore_config[i].thread_id) != 0)
+               if (eal_thread_create(&lcore_config[i].thread_id, i) != 0)
                        rte_panic("Cannot create thread\n");
                ret = pthread_setaffinity_np(lcore_config[i].thread_id,
                        sizeof(rte_cpuset_t), &lcore_config[i].cpuset);
index ff84cb42afd31f74bdd536d4a46ce235bd01fb15..95e96548dea81d13089d781729a4dc2e98188672 100644 (file)
@@ -63,32 +63,21 @@ rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned int worker_id)
 
 /* main loop of threads */
 void *
-eal_thread_loop(void *arg __rte_unused)
+eal_thread_loop(void *arg)
 {
+       unsigned int lcore_id = (uintptr_t)arg;
        char c;
        int n, ret;
-       unsigned int lcore_id;
-       pthread_t thread_id;
        int m2w, w2m;
        char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 
-       thread_id = pthread_self();
-
-       /* retrieve our lcore_id from the configuration structure */
-       RTE_LCORE_FOREACH_WORKER(lcore_id) {
-               if (thread_id == lcore_config[lcore_id].thread_id)
-                       break;
-       }
-       if (lcore_id == RTE_MAX_LCORE)
-               rte_panic("cannot retrieve lcore id\n");
-
        m2w = lcore_config[lcore_id].pipe_main2worker[0];
        w2m = lcore_config[lcore_id].pipe_worker2main[1];
 
        __rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
 
        RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s])\n",
-               lcore_id, (uintptr_t)thread_id, cpuset);
+               lcore_id, (uintptr_t)pthread_self(), cpuset);
 
        /* read on our pipe to get commands */
        while (1) {
@@ -144,13 +133,15 @@ eal_thread_loop(void *arg __rte_unused)
 
 /* function to create threads */
 int
-eal_thread_create(pthread_t *thread)
+eal_thread_create(pthread_t *thread, unsigned int lcore_id)
 {
        HANDLE th;
 
        th = CreateThread(NULL, 0,
                (LPTHREAD_START_ROUTINE)(ULONG_PTR)eal_thread_loop,
-                                               NULL, CREATE_SUSPENDED, (LPDWORD)thread);
+                                               (LPVOID)(uintptr_t)lcore_id,
+                                               CREATE_SUSPENDED,
+                                               (LPDWORD)thread);
        if (!th)
                return -1;
 
index 245aa6034487a5537001ad83662553b2ae8b21ab..e4c46705c65213834e6bb00a5403770a3580e73e 100644 (file)
@@ -40,10 +40,12 @@ int eal_create_cpu_map(void);
  *
  * @param thread
  *   The location to store the thread id if successful.
+ * @param lcore_id
+ *   The lcore_id of this worker thread.
  * @return
  *   0 for success, -1 if the thread is not created.
  */
-int eal_thread_create(pthread_t *thread);
+int eal_thread_create(pthread_t *thread, unsigned int lcore_id);
 
 /**
  * Get system NUMA node number for a socket ID.