1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
15 #include <rte_lcore.h>
16 #include <rte_memory.h>
19 #include "eal_internal_cfg.h"
20 #include "eal_private.h"
21 #include "eal_thread.h"
23 RTE_DECLARE_PER_LCORE(unsigned , _socket_id);
25 unsigned rte_socket_id(void)
27 return RTE_PER_LCORE(_socket_id);
31 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
33 struct rte_config *cfg = rte_eal_get_configuration();
35 if (lcore_id >= RTE_MAX_LCORE)
38 return cfg->lcore_role[lcore_id] == role;
42 eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
45 int socket_id = SOCKET_ID_ANY;
52 if (!CPU_ISSET(cpu, cpusetp))
55 if (socket_id == SOCKET_ID_ANY)
56 socket_id = eal_cpu_socket_id(cpu);
58 sid = eal_cpu_socket_id(cpu);
59 if (socket_id != sid) {
60 socket_id = SOCKET_ID_ANY;
64 } while (++cpu < RTE_MAX_LCORE);
70 rte_thread_set_affinity(rte_cpuset_t *cpusetp)
78 s = pthread_setaffinity_np(tid, sizeof(rte_cpuset_t), cpusetp);
80 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
84 /* store socket_id in TLS for quick access */
85 RTE_PER_LCORE(_socket_id) =
86 eal_cpuset_socket_id(cpusetp);
88 /* store cpuset in TLS for quick access */
89 memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
90 sizeof(rte_cpuset_t));
92 lcore_id = rte_lcore_id();
93 if (lcore_id != (unsigned)LCORE_ID_ANY) {
94 /* EAL thread will update lcore_config */
95 lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
96 memmove(&lcore_config[lcore_id].cpuset, cpusetp,
97 sizeof(rte_cpuset_t));
104 rte_thread_get_affinity(rte_cpuset_t *cpusetp)
107 memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
108 sizeof(rte_cpuset_t));
112 eal_thread_dump_affinity(char *str, unsigned size)
117 unsigned int out = 0;
119 rte_thread_get_affinity(&cpuset);
121 for (cpu = 0; cpu < RTE_MAX_LCORE; cpu++) {
122 if (!CPU_ISSET(cpu, &cpuset))
125 ret = snprintf(str + out,
126 size - out, "%u,", cpu);
127 if (ret < 0 || (unsigned)ret >= size - out) {
128 /* string will be truncated */
138 /* remove the last separator */
146 struct rte_thread_ctrl_params {
147 void *(*start_routine)(void *);
149 pthread_barrier_t configured;
152 static void *rte_thread_init(void *arg)
155 struct rte_thread_ctrl_params *params = arg;
156 void *(*start_routine)(void *) = params->start_routine;
157 void *routine_arg = params->arg;
159 ret = pthread_barrier_wait(¶ms->configured);
160 if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
161 pthread_barrier_destroy(¶ms->configured);
165 return start_routine(routine_arg);
169 rte_ctrl_thread_create(pthread_t *thread, const char *name,
170 const pthread_attr_t *attr,
171 void *(*start_routine)(void *), void *arg)
173 rte_cpuset_t *cpuset = &internal_config.ctrl_cpuset;
174 struct rte_thread_ctrl_params *params;
177 params = malloc(sizeof(*params));
181 params->start_routine = start_routine;
184 pthread_barrier_init(¶ms->configured, NULL, 2);
186 ret = pthread_create(thread, attr, rte_thread_init, (void *)params);
193 ret = rte_thread_setname(*thread, name);
196 "Cannot set name for ctrl thread\n");
199 ret = pthread_setaffinity_np(*thread, sizeof(*cpuset), cpuset);
203 ret = pthread_barrier_wait(¶ms->configured);
204 if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
205 pthread_barrier_destroy(¶ms->configured);
212 if (PTHREAD_BARRIER_SERIAL_THREAD ==
213 pthread_barrier_wait(¶ms->configured)) {
214 pthread_barrier_destroy(¶ms->configured);
217 pthread_cancel(*thread);
218 pthread_join(*thread, NULL);