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_private.h"
20 #include "eal_thread.h"
22 RTE_DECLARE_PER_LCORE(unsigned , _socket_id);
24 unsigned rte_socket_id(void)
26 return RTE_PER_LCORE(_socket_id);
30 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
32 struct rte_config *cfg = rte_eal_get_configuration();
34 if (lcore_id >= RTE_MAX_LCORE)
37 if (cfg->lcore_role[lcore_id] == role)
43 int eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
46 int socket_id = SOCKET_ID_ANY;
53 if (!CPU_ISSET(cpu, cpusetp))
56 if (socket_id == SOCKET_ID_ANY)
57 socket_id = eal_cpu_socket_id(cpu);
59 sid = eal_cpu_socket_id(cpu);
60 if (socket_id != sid) {
61 socket_id = SOCKET_ID_ANY;
65 } while (++cpu < RTE_MAX_LCORE);
71 rte_thread_set_affinity(rte_cpuset_t *cpusetp)
79 s = pthread_setaffinity_np(tid, sizeof(rte_cpuset_t), cpusetp);
81 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
85 /* store socket_id in TLS for quick access */
86 RTE_PER_LCORE(_socket_id) =
87 eal_cpuset_socket_id(cpusetp);
89 /* store cpuset in TLS for quick access */
90 memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
91 sizeof(rte_cpuset_t));
93 lcore_id = rte_lcore_id();
94 if (lcore_id != (unsigned)LCORE_ID_ANY) {
95 /* EAL thread will update lcore_config */
96 lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
97 memmove(&lcore_config[lcore_id].cpuset, cpusetp,
98 sizeof(rte_cpuset_t));
105 rte_thread_get_affinity(rte_cpuset_t *cpusetp)
108 memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
109 sizeof(rte_cpuset_t));
113 eal_thread_dump_affinity(char *str, unsigned size)
118 unsigned int out = 0;
120 rte_thread_get_affinity(&cpuset);
122 for (cpu = 0; cpu < RTE_MAX_LCORE; cpu++) {
123 if (!CPU_ISSET(cpu, &cpuset))
126 ret = snprintf(str + out,
127 size - out, "%u,", cpu);
128 if (ret < 0 || (unsigned)ret >= size - out) {
129 /* string will be truncated */
139 /* remove the last separator */
147 struct rte_thread_ctrl_params {
148 void *(*start_routine)(void *);
150 pthread_barrier_t configured;
153 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 pthread_barrier_wait(¶ms->configured);
161 return start_routine(routine_arg);
164 __rte_experimental int
165 rte_ctrl_thread_create(pthread_t *thread, const char *name,
166 const pthread_attr_t *attr,
167 void *(*start_routine)(void *), void *arg)
169 struct rte_thread_ctrl_params params = {
170 .start_routine = start_routine,
173 unsigned int lcore_id;
177 pthread_barrier_init(¶ms.configured, NULL, 2);
179 ret = pthread_create(thread, attr, rte_thread_init, (void *)¶ms);
184 ret = rte_thread_setname(*thread, name);
191 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
192 if (eal_cpu_detected(lcore_id) &&
193 rte_lcore_has_role(lcore_id, ROLE_OFF)) {
194 CPU_SET(lcore_id, &cpuset);
198 /* if no detected cpu is off, use master core */
200 CPU_SET(rte_get_master_lcore(), &cpuset);
202 ret = pthread_setaffinity_np(*thread, sizeof(cpuset), &cpuset);
206 pthread_barrier_wait(¶ms.configured);
211 pthread_cancel(*thread);
212 pthread_join(*thread, NULL);