1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <rte_errno.h>
13 #include <rte_lcore.h>
15 #include <rte_memory.h>
16 #include <rte_trace_point.h>
18 #include "eal_internal_cfg.h"
19 #include "eal_private.h"
20 #include "eal_thread.h"
21 #include "eal_trace.h"
23 RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
24 RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
25 static RTE_DEFINE_PER_LCORE(unsigned int, _socket_id) =
26 (unsigned int)SOCKET_ID_ANY;
27 static RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
29 unsigned rte_socket_id(void)
31 return RTE_PER_LCORE(_socket_id);
35 eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
38 int socket_id = SOCKET_ID_ANY;
45 if (!CPU_ISSET(cpu, cpusetp))
48 if (socket_id == SOCKET_ID_ANY)
49 socket_id = eal_cpu_socket_id(cpu);
51 sid = eal_cpu_socket_id(cpu);
52 if (socket_id != sid) {
53 socket_id = SOCKET_ID_ANY;
57 } while (++cpu < CPU_SETSIZE);
63 thread_update_affinity(rte_cpuset_t *cpusetp)
65 unsigned int lcore_id = rte_lcore_id();
67 /* store socket_id in TLS for quick access */
68 RTE_PER_LCORE(_socket_id) =
69 eal_cpuset_socket_id(cpusetp);
71 /* store cpuset in TLS for quick access */
72 memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
73 sizeof(rte_cpuset_t));
75 if (lcore_id != (unsigned)LCORE_ID_ANY) {
76 /* EAL thread will update lcore_config */
77 lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
78 memmove(&lcore_config[lcore_id].cpuset, cpusetp,
79 sizeof(rte_cpuset_t));
84 rte_thread_set_affinity(rte_cpuset_t *cpusetp)
86 if (pthread_setaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
88 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
92 thread_update_affinity(cpusetp);
97 rte_thread_get_affinity(rte_cpuset_t *cpusetp)
100 memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
101 sizeof(rte_cpuset_t));
105 eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size)
109 unsigned int out = 0;
111 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
112 if (!CPU_ISSET(cpu, cpuset))
115 ret = snprintf(str + out,
116 size - out, "%u,", cpu);
117 if (ret < 0 || (unsigned)ret >= size - out) {
118 /* string will be truncated */
128 /* remove the last separator */
136 eal_thread_dump_current_affinity(char *str, unsigned int size)
140 rte_thread_get_affinity(&cpuset);
141 return eal_thread_dump_affinity(&cpuset, str, size);
145 __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
147 /* set the lcore ID in per-lcore memory area */
148 RTE_PER_LCORE(_lcore_id) = lcore_id;
150 /* acquire system unique id */
153 thread_update_affinity(cpuset);
155 __rte_trace_mem_per_thread_alloc();
159 __rte_thread_uninit(void)
161 trace_mem_per_thread_free();
163 RTE_PER_LCORE(_lcore_id) = LCORE_ID_ANY;
166 enum __rte_ctrl_thread_status {
167 CTRL_THREAD_LAUNCHING, /* Yet to call pthread_create function */
168 CTRL_THREAD_RUNNING, /* Control thread is running successfully */
169 CTRL_THREAD_ERROR /* Control thread encountered an error */
172 struct rte_thread_ctrl_params {
173 void *(*start_routine)(void *);
176 /* Control thread status.
177 * If the status is CTRL_THREAD_ERROR, 'ret' has the error code.
179 enum __rte_ctrl_thread_status ctrl_thread_status;
182 static void *ctrl_thread_init(void *arg)
184 struct internal_config *internal_conf =
185 eal_get_internal_configuration();
186 rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset;
187 struct rte_thread_ctrl_params *params = arg;
188 void *(*start_routine)(void *) = params->start_routine;
189 void *routine_arg = params->arg;
191 __rte_thread_init(rte_lcore_id(), cpuset);
192 params->ret = pthread_setaffinity_np(pthread_self(), sizeof(*cpuset),
194 if (params->ret != 0) {
195 __atomic_store_n(¶ms->ctrl_thread_status,
196 CTRL_THREAD_ERROR, __ATOMIC_RELEASE);
200 __atomic_store_n(¶ms->ctrl_thread_status,
201 CTRL_THREAD_RUNNING, __ATOMIC_RELEASE);
203 return start_routine(routine_arg);
207 rte_ctrl_thread_create(pthread_t *thread, const char *name,
208 const pthread_attr_t *attr,
209 void *(*start_routine)(void *), void *arg)
211 struct rte_thread_ctrl_params *params;
212 enum __rte_ctrl_thread_status ctrl_thread_status;
215 params = malloc(sizeof(*params));
219 params->start_routine = start_routine;
222 params->ctrl_thread_status = CTRL_THREAD_LAUNCHING;
224 ret = pthread_create(thread, attr, ctrl_thread_init, (void *)params);
231 ret = rte_thread_setname(*thread, name);
234 "Cannot set name for ctrl thread\n");
237 /* Wait for the control thread to initialize successfully */
238 while ((ctrl_thread_status =
239 __atomic_load_n(¶ms->ctrl_thread_status,
240 __ATOMIC_ACQUIRE)) == CTRL_THREAD_LAUNCHING) {
241 /* Yield the CPU. Using sched_yield call requires maintaining
242 * another implementation for Windows as sched_yield is not
243 * supported on Windows.
245 rte_delay_us_sleep(1);
248 /* Check if the control thread encountered an error */
249 if (ctrl_thread_status == CTRL_THREAD_ERROR) {
250 /* ctrl thread is exiting */
251 pthread_join(*thread, NULL);
261 rte_thread_register(void)
263 unsigned int lcore_id;
266 /* EAL init flushes all lcores, we can't register before. */
267 if (eal_get_internal_configuration()->init_complete != 1) {
268 RTE_LOG(DEBUG, EAL, "Called %s before EAL init.\n", __func__);
272 if (!rte_mp_disable()) {
273 RTE_LOG(ERR, EAL, "Multiprocess in use, registering non-EAL threads is not supported.\n");
277 if (pthread_getaffinity_np(pthread_self(), sizeof(cpuset),
280 lcore_id = eal_lcore_non_eal_allocate();
281 if (lcore_id >= RTE_MAX_LCORE)
282 lcore_id = LCORE_ID_ANY;
283 __rte_thread_init(lcore_id, &cpuset);
284 if (lcore_id == LCORE_ID_ANY) {
288 RTE_LOG(DEBUG, EAL, "Registered non-EAL thread as lcore %u.\n",
294 rte_thread_unregister(void)
296 unsigned int lcore_id = rte_lcore_id();
298 if (lcore_id != LCORE_ID_ANY)
299 eal_lcore_non_eal_release(lcore_id);
300 __rte_thread_uninit();
301 if (lcore_id != LCORE_ID_ANY)
302 RTE_LOG(DEBUG, EAL, "Unregistered non-EAL thread (was lcore %u).\n",