1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <rte_lcore.h>
16 #include <rte_memory.h>
18 #include <rte_trace_point.h>
20 #include "eal_internal_cfg.h"
21 #include "eal_private.h"
22 #include "eal_thread.h"
24 RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
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 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
37 struct rte_config *cfg = rte_eal_get_configuration();
39 if (lcore_id >= RTE_MAX_LCORE)
42 return cfg->lcore_role[lcore_id] == role;
46 eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
49 int socket_id = SOCKET_ID_ANY;
56 if (!CPU_ISSET(cpu, cpusetp))
59 if (socket_id == SOCKET_ID_ANY)
60 socket_id = eal_cpu_socket_id(cpu);
62 sid = eal_cpu_socket_id(cpu);
63 if (socket_id != sid) {
64 socket_id = SOCKET_ID_ANY;
68 } while (++cpu < CPU_SETSIZE);
74 rte_thread_set_affinity(rte_cpuset_t *cpusetp)
82 s = pthread_setaffinity_np(tid, sizeof(rte_cpuset_t), cpusetp);
84 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
88 /* store socket_id in TLS for quick access */
89 RTE_PER_LCORE(_socket_id) =
90 eal_cpuset_socket_id(cpusetp);
92 /* store cpuset in TLS for quick access */
93 memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
94 sizeof(rte_cpuset_t));
96 lcore_id = rte_lcore_id();
97 if (lcore_id != (unsigned)LCORE_ID_ANY) {
98 /* EAL thread will update lcore_config */
99 lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
100 memmove(&lcore_config[lcore_id].cpuset, cpusetp,
101 sizeof(rte_cpuset_t));
108 rte_thread_get_affinity(rte_cpuset_t *cpusetp)
111 memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
112 sizeof(rte_cpuset_t));
116 eal_thread_dump_affinity(char *str, unsigned size)
121 unsigned int out = 0;
123 rte_thread_get_affinity(&cpuset);
125 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
126 if (!CPU_ISSET(cpu, &cpuset))
129 ret = snprintf(str + out,
130 size - out, "%u,", cpu);
131 if (ret < 0 || (unsigned)ret >= size - out) {
132 /* string will be truncated */
142 /* remove the last separator */
150 struct rte_thread_ctrl_params {
151 void *(*start_routine)(void *);
153 pthread_barrier_t configured;
156 static void *rte_thread_init(void *arg)
159 struct internal_config *internal_conf =
160 eal_get_internal_configuration();
161 rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset;
162 struct rte_thread_ctrl_params *params = arg;
163 void *(*start_routine)(void *) = params->start_routine;
164 void *routine_arg = params->arg;
166 /* Store cpuset in TLS for quick access */
167 memmove(&RTE_PER_LCORE(_cpuset), cpuset, sizeof(rte_cpuset_t));
169 ret = pthread_barrier_wait(¶ms->configured);
170 if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
171 pthread_barrier_destroy(¶ms->configured);
175 __rte_trace_mem_per_thread_alloc();
177 return start_routine(routine_arg);
181 rte_ctrl_thread_create(pthread_t *thread, const char *name,
182 const pthread_attr_t *attr,
183 void *(*start_routine)(void *), void *arg)
185 struct internal_config *internal_conf =
186 eal_get_internal_configuration();
187 rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset;
188 struct rte_thread_ctrl_params *params;
191 params = malloc(sizeof(*params));
195 params->start_routine = start_routine;
198 pthread_barrier_init(¶ms->configured, NULL, 2);
200 ret = pthread_create(thread, attr, rte_thread_init, (void *)params);
207 ret = rte_thread_setname(*thread, name);
210 "Cannot set name for ctrl thread\n");
213 ret = pthread_setaffinity_np(*thread, sizeof(*cpuset), cpuset);
217 ret = pthread_barrier_wait(¶ms->configured);
218 if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
219 pthread_barrier_destroy(¶ms->configured);
226 if (PTHREAD_BARRIER_SERIAL_THREAD ==
227 pthread_barrier_wait(¶ms->configured)) {
228 pthread_barrier_destroy(¶ms->configured);
231 pthread_cancel(*thread);
232 pthread_join(*thread, NULL);