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
;
41 int eal_cpuset_socket_id(rte_cpuset_t
*cpusetp
)
44 int socket_id
= SOCKET_ID_ANY
;
51 if (!CPU_ISSET(cpu
, cpusetp
))
54 if (socket_id
== SOCKET_ID_ANY
)
55 socket_id
= eal_cpu_socket_id(cpu
);
57 sid
= eal_cpu_socket_id(cpu
);
58 if (socket_id
!= sid
) {
59 socket_id
= SOCKET_ID_ANY
;
63 } while (++cpu
< RTE_MAX_LCORE
);
69 rte_thread_set_affinity(rte_cpuset_t
*cpusetp
)
77 s
= pthread_setaffinity_np(tid
, sizeof(rte_cpuset_t
), cpusetp
);
79 RTE_LOG(ERR
, EAL
, "pthread_setaffinity_np failed\n");
83 /* store socket_id in TLS for quick access */
84 RTE_PER_LCORE(_socket_id
) =
85 eal_cpuset_socket_id(cpusetp
);
87 /* store cpuset in TLS for quick access */
88 memmove(&RTE_PER_LCORE(_cpuset
), cpusetp
,
89 sizeof(rte_cpuset_t
));
91 lcore_id
= rte_lcore_id();
92 if (lcore_id
!= (unsigned)LCORE_ID_ANY
) {
93 /* EAL thread will update lcore_config */
94 lcore_config
[lcore_id
].socket_id
= RTE_PER_LCORE(_socket_id
);
95 memmove(&lcore_config
[lcore_id
].cpuset
, cpusetp
,
96 sizeof(rte_cpuset_t
));
103 rte_thread_get_affinity(rte_cpuset_t
*cpusetp
)
106 memmove(cpusetp
, &RTE_PER_LCORE(_cpuset
),
107 sizeof(rte_cpuset_t
));
111 eal_thread_dump_affinity(char *str
, unsigned size
)
116 unsigned int out
= 0;
118 rte_thread_get_affinity(&cpuset
);
120 for (cpu
= 0; cpu
< RTE_MAX_LCORE
; cpu
++) {
121 if (!CPU_ISSET(cpu
, &cpuset
))
124 ret
= snprintf(str
+ out
,
125 size
- out
, "%u,", cpu
);
126 if (ret
< 0 || (unsigned)ret
>= size
- out
) {
127 /* string will be truncated */
137 /* remove the last separator */
145 struct rte_thread_ctrl_params
{
146 void *(*start_routine
)(void *);
148 pthread_barrier_t configured
;
151 static void *rte_thread_init(void *arg
)
154 struct rte_thread_ctrl_params
*params
= arg
;
155 void *(*start_routine
)(void *) = params
->start_routine
;
156 void *routine_arg
= params
->arg
;
158 ret
= pthread_barrier_wait(¶ms
->configured
);
159 if (ret
== PTHREAD_BARRIER_SERIAL_THREAD
) {
160 pthread_barrier_destroy(¶ms
->configured
);
164 return start_routine(routine_arg
);
168 rte_ctrl_thread_create(pthread_t
*thread
, const char *name
,
169 const pthread_attr_t
*attr
,
170 void *(*start_routine
)(void *), void *arg
)
172 rte_cpuset_t
*cpuset
= &internal_config
.ctrl_cpuset
;
173 struct rte_thread_ctrl_params
*params
;
176 params
= malloc(sizeof(*params
));
180 params
->start_routine
= start_routine
;
183 pthread_barrier_init(¶ms
->configured
, NULL
, 2);
185 ret
= pthread_create(thread
, attr
, rte_thread_init
, (void *)params
);
192 ret
= rte_thread_setname(*thread
, name
);
195 "Cannot set name for ctrl thread\n");
198 ret
= pthread_setaffinity_np(*thread
, sizeof(*cpuset
), cpuset
);
202 ret
= pthread_barrier_wait(¶ms
->configured
);
203 if (ret
== PTHREAD_BARRIER_SERIAL_THREAD
) {
204 pthread_barrier_destroy(¶ms
->configured
);
211 if (PTHREAD_BARRIER_SERIAL_THREAD
==
212 pthread_barrier_wait(¶ms
->configured
)) {
213 pthread_barrier_destroy(¶ms
->configured
);
216 pthread_cancel(*thread
);
217 pthread_join(*thread
, NULL
);