1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <rte_errno.h>
16 #include <rte_lcore.h>
18 #include <rte_memory.h>
19 #include <rte_trace_point.h>
21 #include "eal_internal_cfg.h"
22 #include "eal_private.h"
23 #include "eal_thread.h"
24 #include "eal_trace.h"
26 RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
27 RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
28 static RTE_DEFINE_PER_LCORE(unsigned int, _socket_id) =
29 (unsigned int)SOCKET_ID_ANY;
30 static RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
32 unsigned rte_socket_id(void)
34 return RTE_PER_LCORE(_socket_id);
38 eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
41 int socket_id = SOCKET_ID_ANY;
48 if (!CPU_ISSET(cpu, cpusetp))
51 if (socket_id == SOCKET_ID_ANY)
52 socket_id = eal_cpu_socket_id(cpu);
54 sid = eal_cpu_socket_id(cpu);
55 if (socket_id != sid) {
56 socket_id = SOCKET_ID_ANY;
60 } while (++cpu < CPU_SETSIZE);
66 thread_update_affinity(rte_cpuset_t *cpusetp)
68 unsigned int lcore_id = rte_lcore_id();
70 /* store socket_id in TLS for quick access */
71 RTE_PER_LCORE(_socket_id) =
72 eal_cpuset_socket_id(cpusetp);
74 /* store cpuset in TLS for quick access */
75 memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
76 sizeof(rte_cpuset_t));
78 if (lcore_id != (unsigned)LCORE_ID_ANY) {
79 /* EAL thread will update lcore_config */
80 lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
81 memmove(&lcore_config[lcore_id].cpuset, cpusetp,
82 sizeof(rte_cpuset_t));
87 rte_thread_set_affinity(rte_cpuset_t *cpusetp)
89 if (pthread_setaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
91 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
95 thread_update_affinity(cpusetp);
100 rte_thread_get_affinity(rte_cpuset_t *cpusetp)
103 memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
104 sizeof(rte_cpuset_t));
108 eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size)
112 unsigned int out = 0;
114 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
115 if (!CPU_ISSET(cpu, cpuset))
118 ret = snprintf(str + out,
119 size - out, "%u,", cpu);
120 if (ret < 0 || (unsigned)ret >= size - out) {
121 /* string will be truncated */
131 /* remove the last separator */
139 eal_thread_dump_current_affinity(char *str, unsigned int size)
143 rte_thread_get_affinity(&cpuset);
144 return eal_thread_dump_affinity(&cpuset, str, size);
148 __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
150 /* set the lcore ID in per-lcore memory area */
151 RTE_PER_LCORE(_lcore_id) = lcore_id;
153 /* acquire system unique id */
156 thread_update_affinity(cpuset);
158 __rte_trace_mem_per_thread_alloc();
162 __rte_thread_uninit(void)
164 trace_mem_per_thread_free();
166 RTE_PER_LCORE(_lcore_id) = LCORE_ID_ANY;
169 enum __rte_ctrl_thread_status {
170 CTRL_THREAD_LAUNCHING, /* Yet to call pthread_create function */
171 CTRL_THREAD_RUNNING, /* Control thread is running successfully */
172 CTRL_THREAD_ERROR /* Control thread encountered an error */
175 struct rte_thread_ctrl_params {
176 void *(*start_routine)(void *);
179 /* Control thread status.
180 * If the status is CTRL_THREAD_ERROR, 'ret' has the error code.
182 enum __rte_ctrl_thread_status ctrl_thread_status;
185 static void *ctrl_thread_init(void *arg)
187 struct internal_config *internal_conf =
188 eal_get_internal_configuration();
189 rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset;
190 struct rte_thread_ctrl_params *params = arg;
191 void *(*start_routine)(void *) = params->start_routine;
192 void *routine_arg = params->arg;
194 __rte_thread_init(rte_lcore_id(), cpuset);
195 params->ret = pthread_setaffinity_np(pthread_self(), sizeof(*cpuset),
197 if (params->ret != 0) {
198 __atomic_store_n(¶ms->ctrl_thread_status,
199 CTRL_THREAD_ERROR, __ATOMIC_RELEASE);
203 __atomic_store_n(¶ms->ctrl_thread_status,
204 CTRL_THREAD_RUNNING, __ATOMIC_RELEASE);
206 return start_routine(routine_arg);
210 rte_ctrl_thread_create(pthread_t *thread, const char *name,
211 const pthread_attr_t *attr,
212 void *(*start_routine)(void *), void *arg)
214 struct rte_thread_ctrl_params *params;
215 enum __rte_ctrl_thread_status ctrl_thread_status;
218 params = malloc(sizeof(*params));
222 params->start_routine = start_routine;
225 params->ctrl_thread_status = CTRL_THREAD_LAUNCHING;
227 ret = pthread_create(thread, attr, ctrl_thread_init, (void *)params);
234 ret = rte_thread_setname(*thread, name);
237 "Cannot set name for ctrl thread\n");
240 /* Wait for the control thread to initialize successfully */
241 while ((ctrl_thread_status =
242 __atomic_load_n(¶ms->ctrl_thread_status,
243 __ATOMIC_ACQUIRE)) == CTRL_THREAD_LAUNCHING) {
244 /* Yield the CPU. Using sched_yield call requires maintaining
245 * another implementation for Windows as sched_yield is not
246 * supported on Windows.
248 rte_delay_us_sleep(1);
251 /* Check if the control thread encountered an error */
252 if (ctrl_thread_status == CTRL_THREAD_ERROR) {
253 /* ctrl thread is exiting */
254 pthread_join(*thread, NULL);
264 rte_thread_register(void)
266 unsigned int lcore_id;
269 /* EAL init flushes all lcores, we can't register before. */
270 if (eal_get_internal_configuration()->init_complete != 1) {
271 RTE_LOG(DEBUG, EAL, "Called %s before EAL init.\n", __func__);
275 if (!rte_mp_disable()) {
276 RTE_LOG(ERR, EAL, "Multiprocess in use, registering non-EAL threads is not supported.\n");
280 if (pthread_getaffinity_np(pthread_self(), sizeof(cpuset),
283 lcore_id = eal_lcore_non_eal_allocate();
284 if (lcore_id >= RTE_MAX_LCORE)
285 lcore_id = LCORE_ID_ANY;
286 __rte_thread_init(lcore_id, &cpuset);
287 if (lcore_id == LCORE_ID_ANY) {
291 RTE_LOG(DEBUG, EAL, "Registered non-EAL thread as lcore %u.\n",
297 rte_thread_unregister(void)
299 unsigned int lcore_id = rte_lcore_id();
301 if (lcore_id != LCORE_ID_ANY)
302 eal_lcore_non_eal_release(lcore_id);
303 __rte_thread_uninit();
304 if (lcore_id != LCORE_ID_ANY)
305 RTE_LOG(DEBUG, EAL, "Unregistered non-EAL thread (was lcore %u).\n",