Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : :
5 : : #include <errno.h>
6 : : #include <stdio.h>
7 : : #include <stdlib.h>
8 : : #include <pthread.h>
9 : : #include <sched.h>
10 : : #include <assert.h>
11 : : #include <string.h>
12 : :
13 : : #include <eal_export.h>
14 : : #include <eal_trace_internal.h>
15 : : #include <rte_errno.h>
16 : : #include <rte_lcore.h>
17 : : #include <rte_log.h>
18 : : #include <rte_memory.h>
19 : : #include <rte_trace_point.h>
20 : :
21 : : #include "eal_internal_cfg.h"
22 : : #include "eal_private.h"
23 : : #include "eal_thread.h"
24 : : #include "eal_trace.h"
25 : :
26 : : RTE_EXPORT_SYMBOL(per_lcore__lcore_id)
27 : : RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
28 : : RTE_EXPORT_SYMBOL(per_lcore__thread_id)
29 : : RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
30 : : static RTE_DEFINE_PER_LCORE(unsigned int, _socket_id) =
31 : : (unsigned int)SOCKET_ID_ANY;
32 : : static RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
33 : :
34 : : RTE_EXPORT_SYMBOL(rte_socket_id)
35 : 109375 : unsigned rte_socket_id(void)
36 : : {
37 : 109375 : return RTE_PER_LCORE(_socket_id);
38 : : }
39 : :
40 : : static int
41 : 802 : eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
42 : : {
43 : : unsigned cpu = 0;
44 : : int socket_id = SOCKET_ID_ANY;
45 : : int sid;
46 : :
47 [ + - ]: 802 : if (cpusetp == NULL)
48 : : return SOCKET_ID_ANY;
49 : :
50 : : do {
51 [ + + ]: 821248 : if (!CPU_ISSET(cpu, cpusetp))
52 : 820446 : continue;
53 : :
54 [ + - ]: 802 : if (socket_id == SOCKET_ID_ANY)
55 : 802 : socket_id = eal_cpu_socket_id(cpu);
56 : :
57 : 802 : sid = eal_cpu_socket_id(cpu);
58 [ + - ]: 802 : if (socket_id != sid) {
59 : : socket_id = SOCKET_ID_ANY;
60 : : break;
61 : : }
62 : :
63 [ + + ]: 821248 : } while (++cpu < CPU_SETSIZE);
64 : :
65 : : return socket_id;
66 : : }
67 : :
68 : : static void
69 : 802 : thread_update_affinity(rte_cpuset_t *cpusetp)
70 : : {
71 : : unsigned int lcore_id = rte_lcore_id();
72 : :
73 : : /* store socket_id in TLS for quick access */
74 : 802 : RTE_PER_LCORE(_socket_id) =
75 [ + + ]: 802 : eal_cpuset_socket_id(cpusetp);
76 : :
77 : : /* store cpuset in TLS for quick access */
78 : : memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
79 : : sizeof(rte_cpuset_t));
80 : :
81 [ + + ]: 802 : if (lcore_id != (unsigned)LCORE_ID_ANY) {
82 : : /* EAL thread will update lcore_config */
83 : 435 : lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
84 : 435 : memmove(&lcore_config[lcore_id].cpuset, cpusetp,
85 : : sizeof(rte_cpuset_t));
86 : : }
87 : 802 : }
88 : :
89 : : RTE_EXPORT_SYMBOL(rte_thread_set_affinity)
90 : : int
91 : 0 : rte_thread_set_affinity(rte_cpuset_t *cpusetp)
92 : : {
93 [ # # ]: 0 : if (rte_thread_set_affinity_by_id(rte_thread_self(), cpusetp) != 0) {
94 : 0 : EAL_LOG(ERR, "rte_thread_set_affinity_by_id failed");
95 : 0 : return -1;
96 : : }
97 : :
98 : 0 : thread_update_affinity(cpusetp);
99 : 0 : return 0;
100 : : }
101 : :
102 : : RTE_EXPORT_SYMBOL(rte_thread_get_affinity)
103 : : void
104 : 308 : rte_thread_get_affinity(rte_cpuset_t *cpusetp)
105 : : {
106 [ - + ]: 308 : assert(cpusetp);
107 : : memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
108 : : sizeof(rte_cpuset_t));
109 : 308 : }
110 : :
111 : : int
112 : 315 : eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size)
113 : : {
114 : : unsigned cpu;
115 : : int ret;
116 : : unsigned int out = 0;
117 : :
118 [ + + ]: 322875 : for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
119 [ + + ]: 322560 : if (!CPU_ISSET(cpu, cpuset))
120 : 322245 : continue;
121 : :
122 : 315 : ret = snprintf(str + out,
123 [ + - ]: 315 : size - out, "%u,", cpu);
124 [ + - - + ]: 315 : if (ret < 0 || (unsigned)ret >= size - out) {
125 : : /* string will be truncated */
126 : : ret = -1;
127 : 0 : goto exit;
128 : : }
129 : :
130 : 315 : out += ret;
131 : : }
132 : :
133 : : ret = 0;
134 : 315 : exit:
135 : : /* remove the last separator */
136 [ + - ]: 315 : if (out > 0)
137 : 315 : str[out - 1] = '\0';
138 : :
139 : 315 : return ret;
140 : : }
141 : :
142 : : int
143 : 308 : eal_thread_dump_current_affinity(char *str, unsigned int size)
144 : : {
145 : : rte_cpuset_t cpuset;
146 : :
147 : 308 : rte_thread_get_affinity(&cpuset);
148 : 308 : return eal_thread_dump_affinity(&cpuset, str, size);
149 : : }
150 : :
151 : : void
152 : 802 : __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
153 : : {
154 : : /* set the lcore ID in per-lcore memory area */
155 [ + + ]: 802 : RTE_PER_LCORE(_lcore_id) = lcore_id;
156 : :
157 : : /* acquire system unique id */
158 : : rte_gettid();
159 : :
160 : 802 : thread_update_affinity(cpuset);
161 : :
162 : 802 : __rte_trace_mem_per_thread_alloc();
163 : 802 : }
164 : :
165 : : void
166 : 129 : __rte_thread_uninit(void)
167 : : {
168 : 129 : trace_mem_per_thread_free();
169 : :
170 : 129 : RTE_PER_LCORE(_lcore_id) = LCORE_ID_ANY;
171 : 129 : }
172 : :
173 : : /* main loop of threads */
174 : : __rte_noreturn uint32_t
175 : 128 : eal_thread_loop(void *arg)
176 : : {
177 : 128 : unsigned int lcore_id = (uintptr_t)arg;
178 : : char cpuset[RTE_CPU_AFFINITY_STR_LEN];
179 : : int ret;
180 : :
181 : 128 : __rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
182 : :
183 : 128 : ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
184 [ - + ]: 128 : EAL_LOG(DEBUG, "lcore %u is ready (tid=%zx;cpuset=[%s%s])",
185 : : lcore_id, rte_thread_self().opaque_id, cpuset,
186 : : ret == 0 ? "" : "...");
187 : :
188 : 128 : rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
189 : :
190 : : /* read on our pipe to get commands */
191 : 194 : while (1) {
192 : : lcore_function_t *f;
193 : : void *fct_arg;
194 : :
195 : 322 : eal_thread_wait_command();
196 : :
197 : : /* Set the state to 'RUNNING'. Use release order
198 : : * since 'state' variable is used as the guard variable.
199 : : */
200 : 194 : rte_atomic_store_explicit(&lcore_config[lcore_id].state, RUNNING,
201 : : rte_memory_order_release);
202 : :
203 : 194 : eal_thread_ack_command();
204 : :
205 : : /* Load 'f' with acquire order to ensure that
206 : : * the memory operations from the main thread
207 : : * are accessed only after update to 'f' is visible.
208 : : * Wait till the update to 'f' is visible to the worker.
209 : : */
210 : 194 : while ((f = rte_atomic_load_explicit(&lcore_config[lcore_id].f,
211 [ - + ]: 194 : rte_memory_order_acquire)) == NULL)
212 : : rte_pause();
213 : :
214 : 194 : rte_eal_trace_thread_lcore_running(lcore_id, f);
215 : :
216 : : /* call the function and store the return value */
217 : 194 : fct_arg = lcore_config[lcore_id].arg;
218 : 194 : ret = f(fct_arg);
219 : 194 : lcore_config[lcore_id].ret = ret;
220 : 194 : lcore_config[lcore_id].f = NULL;
221 : 194 : lcore_config[lcore_id].arg = NULL;
222 : :
223 : : /* Store the state with release order to ensure that
224 : : * the memory operations from the worker thread
225 : : * are completed before the state is updated.
226 : : * Use 'state' as the guard variable.
227 : : */
228 [ + + ]: 194 : rte_atomic_store_explicit(&lcore_config[lcore_id].state, WAIT,
229 : : rte_memory_order_release);
230 : :
231 : 194 : rte_eal_trace_thread_lcore_stopped(lcore_id);
232 : : }
233 : :
234 : : /* never reached */
235 : : /* return 0; */
236 : : }
237 : :
238 : : enum __rte_ctrl_thread_status {
239 : : CTRL_THREAD_LAUNCHING, /* Yet to call pthread_create function */
240 : : CTRL_THREAD_RUNNING, /* Control thread is running successfully */
241 : : CTRL_THREAD_ERROR /* Control thread encountered an error */
242 : : };
243 : :
244 : : struct control_thread_params {
245 : : rte_thread_func start_routine;
246 : : void *arg;
247 : : int ret;
248 : : /* Control thread status.
249 : : * If the status is CTRL_THREAD_ERROR, 'ret' has the error code.
250 : : */
251 : : RTE_ATOMIC(enum __rte_ctrl_thread_status) status;
252 : : };
253 : :
254 : 365 : static int control_thread_init(void *arg)
255 : : {
256 : : struct internal_config *internal_conf =
257 : 365 : eal_get_internal_configuration();
258 : 365 : rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset;
259 : : struct control_thread_params *params = arg;
260 : :
261 : 365 : __rte_thread_init(rte_lcore_id(), cpuset);
262 : : /* Set control thread socket ID to SOCKET_ID_ANY
263 : : * as control threads may be scheduled on any NUMA node.
264 : : */
265 : 365 : RTE_PER_LCORE(_socket_id) = SOCKET_ID_ANY;
266 : 365 : params->ret = rte_thread_set_affinity_by_id(rte_thread_self(), cpuset);
267 [ - + ]: 365 : if (params->ret != 0) {
268 : 0 : rte_atomic_store_explicit(¶ms->status,
269 : : CTRL_THREAD_ERROR, rte_memory_order_release);
270 : 0 : return 1;
271 : : }
272 : :
273 : 365 : rte_atomic_store_explicit(¶ms->status,
274 : : CTRL_THREAD_RUNNING, rte_memory_order_release);
275 : :
276 : 365 : return 0;
277 : : }
278 : :
279 : 365 : static uint32_t control_thread_start(void *arg)
280 : : {
281 : : struct control_thread_params *params = arg;
282 : 365 : void *start_arg = params->arg;
283 : 365 : rte_thread_func start_routine = params->start_routine;
284 : :
285 [ + - ]: 365 : if (control_thread_init(arg) != 0)
286 : : return 0;
287 : :
288 : 365 : return start_routine(start_arg);
289 : : }
290 : :
291 : : RTE_EXPORT_SYMBOL(rte_thread_create_control)
292 : : int
293 : 365 : rte_thread_create_control(rte_thread_t *thread, const char *name,
294 : : rte_thread_func start_routine, void *arg)
295 : : {
296 : : struct control_thread_params *params;
297 : : enum __rte_ctrl_thread_status ctrl_thread_status;
298 : : int ret;
299 : :
300 : 365 : params = malloc(sizeof(*params));
301 [ + - ]: 365 : if (params == NULL)
302 : : return -ENOMEM;
303 : :
304 : 365 : params->start_routine = start_routine;
305 : 365 : params->arg = arg;
306 : 365 : params->ret = 0;
307 : 365 : params->status = CTRL_THREAD_LAUNCHING;
308 : :
309 : 365 : ret = rte_thread_create(thread, NULL, control_thread_start, params);
310 [ - + ]: 365 : if (ret != 0) {
311 : 0 : free(params);
312 : 0 : return -ret;
313 : : }
314 : :
315 [ + - ]: 365 : if (name != NULL)
316 : 365 : rte_thread_set_name(*thread, name);
317 : :
318 : : /* Wait for the control thread to initialize successfully */
319 : : while ((ctrl_thread_status =
320 : 863 : rte_atomic_load_explicit(¶ms->status,
321 [ + + ]: 863 : rte_memory_order_acquire)) == CTRL_THREAD_LAUNCHING) {
322 : 498 : rte_delay_us_sleep(1);
323 : : }
324 : :
325 : : /* Check if the control thread encountered an error */
326 [ - + ]: 365 : if (ctrl_thread_status == CTRL_THREAD_ERROR) {
327 : : /* ctrl thread is exiting */
328 : 0 : rte_thread_join(*thread, NULL);
329 : : }
330 : :
331 : 365 : ret = params->ret;
332 : 365 : free(params);
333 : :
334 : 365 : return ret;
335 : : }
336 : :
337 : : static void
338 : 363 : add_internal_prefix(char *prefixed_name, const char *name, size_t size)
339 : : {
340 : : size_t prefixlen;
341 : :
342 : : /* Check RTE_THREAD_INTERNAL_NAME_SIZE definition. */
343 : : RTE_BUILD_BUG_ON(RTE_THREAD_INTERNAL_NAME_SIZE !=
344 : : RTE_THREAD_NAME_SIZE - sizeof(RTE_THREAD_INTERNAL_PREFIX) + 1);
345 : :
346 : : prefixlen = strlen(RTE_THREAD_INTERNAL_PREFIX);
347 : : strlcpy(prefixed_name, RTE_THREAD_INTERNAL_PREFIX, size);
348 : 363 : strlcpy(prefixed_name + prefixlen, name, size - prefixlen);
349 : 363 : }
350 : :
351 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_thread_create_internal_control)
352 : : int
353 : 363 : rte_thread_create_internal_control(rte_thread_t *id, const char *name,
354 : : rte_thread_func func, void *arg)
355 : : {
356 : : char prefixed_name[RTE_THREAD_NAME_SIZE];
357 : :
358 : 363 : add_internal_prefix(prefixed_name, name, sizeof(prefixed_name));
359 : 363 : return rte_thread_create_control(id, prefixed_name, func, arg);
360 : : }
361 : :
362 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_thread_set_prefixed_name)
363 : : void
364 : 0 : rte_thread_set_prefixed_name(rte_thread_t id, const char *name)
365 : : {
366 : : char prefixed_name[RTE_THREAD_NAME_SIZE];
367 : :
368 : 0 : add_internal_prefix(prefixed_name, name, sizeof(prefixed_name));
369 : 0 : rte_thread_set_name(id, prefixed_name);
370 : 0 : }
371 : :
372 : : RTE_EXPORT_SYMBOL(rte_thread_register)
373 : : int
374 : 129 : rte_thread_register(void)
375 : : {
376 : : unsigned int lcore_id;
377 : : rte_cpuset_t cpuset;
378 : :
379 : : /* EAL init flushes all lcores, we can't register before. */
380 [ - + ]: 129 : if (eal_get_internal_configuration()->init_complete != 1) {
381 : 0 : EAL_LOG(DEBUG, "Called %s before EAL init.", __func__);
382 : 0 : rte_errno = EINVAL;
383 : 0 : return -1;
384 : : }
385 [ - + ]: 129 : if (!rte_mp_disable()) {
386 : 0 : EAL_LOG(ERR, "Multiprocess in use, registering non-EAL threads is not supported.");
387 : 0 : rte_errno = EINVAL;
388 : 0 : return -1;
389 : : }
390 [ - + ]: 129 : if (rte_thread_get_affinity_by_id(rte_thread_self(), &cpuset) != 0)
391 : 0 : CPU_ZERO(&cpuset);
392 : 129 : lcore_id = eal_lcore_non_eal_allocate();
393 [ + + ]: 129 : if (lcore_id >= RTE_MAX_LCORE)
394 : : lcore_id = LCORE_ID_ANY;
395 : 129 : __rte_thread_init(lcore_id, &cpuset);
396 [ + + ]: 129 : if (lcore_id == LCORE_ID_ANY) {
397 : 2 : rte_errno = ENOMEM;
398 : 2 : return -1;
399 : : }
400 : 127 : EAL_LOG(DEBUG, "Registered non-EAL thread as lcore %u.",
401 : : lcore_id);
402 : 127 : return 0;
403 : : }
404 : :
405 : : RTE_EXPORT_SYMBOL(rte_thread_unregister)
406 : : void
407 [ + + ]: 129 : rte_thread_unregister(void)
408 : : {
409 : : unsigned int lcore_id = rte_lcore_id();
410 : :
411 [ + + ]: 129 : if (lcore_id != LCORE_ID_ANY)
412 : 127 : eal_lcore_non_eal_release(lcore_id);
413 : 129 : __rte_thread_uninit();
414 [ + + ]: 129 : if (lcore_id != LCORE_ID_ANY)
415 : 127 : EAL_LOG(DEBUG, "Unregistered non-EAL thread (was lcore %u).",
416 : : lcore_id);
417 : 129 : }
418 : :
419 : : RTE_EXPORT_SYMBOL(rte_thread_attr_init)
420 : : int
421 : 2 : rte_thread_attr_init(rte_thread_attr_t *attr)
422 : : {
423 [ + - ]: 2 : if (attr == NULL)
424 : : return EINVAL;
425 : :
426 : 2 : CPU_ZERO(&attr->cpuset);
427 : 2 : attr->priority = RTE_THREAD_PRIORITY_NORMAL;
428 : :
429 : 2 : return 0;
430 : : }
431 : :
432 : : RTE_EXPORT_SYMBOL(rte_thread_attr_set_priority)
433 : : int
434 : 1 : rte_thread_attr_set_priority(rte_thread_attr_t *thread_attr,
435 : : enum rte_thread_priority priority)
436 : : {
437 [ + - ]: 1 : if (thread_attr == NULL)
438 : : return EINVAL;
439 : :
440 : 1 : thread_attr->priority = priority;
441 : :
442 : 1 : return 0;
443 : : }
444 : :
445 : : RTE_EXPORT_SYMBOL(rte_thread_attr_set_affinity)
446 : : int
447 : 1 : rte_thread_attr_set_affinity(rte_thread_attr_t *thread_attr,
448 : : rte_cpuset_t *cpuset)
449 : : {
450 [ + - ]: 1 : if (thread_attr == NULL)
451 : : return EINVAL;
452 : :
453 [ + - ]: 1 : if (cpuset == NULL)
454 : : return EINVAL;
455 : :
456 : 1 : thread_attr->cpuset = *cpuset;
457 : :
458 : 1 : return 0;
459 : : }
460 : :
461 : : RTE_EXPORT_SYMBOL(rte_thread_attr_get_affinity)
462 : : int
463 : 1 : rte_thread_attr_get_affinity(rte_thread_attr_t *thread_attr,
464 : : rte_cpuset_t *cpuset)
465 : : {
466 [ + - ]: 1 : if (thread_attr == NULL)
467 : : return EINVAL;
468 : :
469 [ + - ]: 1 : if (cpuset == NULL)
470 : : return EINVAL;
471 : :
472 : 1 : *cpuset = thread_attr->cpuset;
473 : :
474 : 1 : return 0;
475 : : }
|