eal: set affinity for control threads
[dpdk.git] / lib / librte_eal / common / eal_common_thread.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <unistd.h>
9 #include <pthread.h>
10 #include <signal.h>
11 #include <sched.h>
12 #include <assert.h>
13 #include <string.h>
14
15 #include <rte_lcore.h>
16 #include <rte_memory.h>
17 #include <rte_log.h>
18
19 #include "eal_private.h"
20 #include "eal_thread.h"
21
22 RTE_DECLARE_PER_LCORE(unsigned , _socket_id);
23
24 unsigned rte_socket_id(void)
25 {
26         return RTE_PER_LCORE(_socket_id);
27 }
28
29 int
30 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
31 {
32         struct rte_config *cfg = rte_eal_get_configuration();
33
34         if (lcore_id >= RTE_MAX_LCORE)
35                 return -EINVAL;
36
37         if (cfg->lcore_role[lcore_id] == role)
38                 return 0;
39
40         return -EINVAL;
41 }
42
43 int eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
44 {
45         unsigned cpu = 0;
46         int socket_id = SOCKET_ID_ANY;
47         int sid;
48
49         if (cpusetp == NULL)
50                 return SOCKET_ID_ANY;
51
52         do {
53                 if (!CPU_ISSET(cpu, cpusetp))
54                         continue;
55
56                 if (socket_id == SOCKET_ID_ANY)
57                         socket_id = eal_cpu_socket_id(cpu);
58
59                 sid = eal_cpu_socket_id(cpu);
60                 if (socket_id != sid) {
61                         socket_id = SOCKET_ID_ANY;
62                         break;
63                 }
64
65         } while (++cpu < RTE_MAX_LCORE);
66
67         return socket_id;
68 }
69
70 int
71 rte_thread_set_affinity(rte_cpuset_t *cpusetp)
72 {
73         int s;
74         unsigned lcore_id;
75         pthread_t tid;
76
77         tid = pthread_self();
78
79         s = pthread_setaffinity_np(tid, sizeof(rte_cpuset_t), cpusetp);
80         if (s != 0) {
81                 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
82                 return -1;
83         }
84
85         /* store socket_id in TLS for quick access */
86         RTE_PER_LCORE(_socket_id) =
87                 eal_cpuset_socket_id(cpusetp);
88
89         /* store cpuset in TLS for quick access */
90         memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
91                 sizeof(rte_cpuset_t));
92
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));
99         }
100
101         return 0;
102 }
103
104 void
105 rte_thread_get_affinity(rte_cpuset_t *cpusetp)
106 {
107         assert(cpusetp);
108         memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
109                 sizeof(rte_cpuset_t));
110 }
111
112 int
113 eal_thread_dump_affinity(char *str, unsigned size)
114 {
115         rte_cpuset_t cpuset;
116         unsigned cpu;
117         int ret;
118         unsigned int out = 0;
119
120         rte_thread_get_affinity(&cpuset);
121
122         for (cpu = 0; cpu < RTE_MAX_LCORE; cpu++) {
123                 if (!CPU_ISSET(cpu, &cpuset))
124                         continue;
125
126                 ret = snprintf(str + out,
127                                size - out, "%u,", cpu);
128                 if (ret < 0 || (unsigned)ret >= size - out) {
129                         /* string will be truncated */
130                         ret = -1;
131                         goto exit;
132                 }
133
134                 out += ret;
135         }
136
137         ret = 0;
138 exit:
139         /* remove the last separator */
140         if (out > 0)
141                 str[out - 1] = '\0';
142
143         return ret;
144 }
145
146
147 struct rte_thread_ctrl_params {
148         void *(*start_routine)(void *);
149         void *arg;
150         pthread_barrier_t configured;
151 };
152
153 static void *rte_thread_init(void *arg)
154 {
155         struct rte_thread_ctrl_params *params = arg;
156         void *(*start_routine)(void *) = params->start_routine;
157         void *routine_arg = params->arg;
158
159         pthread_barrier_wait(&params->configured);
160
161         return start_routine(routine_arg);
162 }
163
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)
168 {
169         struct rte_thread_ctrl_params params = {
170                 .start_routine = start_routine,
171                 .arg = arg,
172         };
173         unsigned int lcore_id;
174         rte_cpuset_t cpuset;
175         int cpu_found, ret;
176
177         pthread_barrier_init(&params.configured, NULL, 2);
178
179         ret = pthread_create(thread, attr, rte_thread_init, (void *)&params);
180         if (ret != 0)
181                 return ret;
182
183         if (name != NULL) {
184                 ret = rte_thread_setname(*thread, name);
185                 if (ret < 0)
186                         goto fail;
187         }
188
189         cpu_found = 0;
190         CPU_ZERO(&cpuset);
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);
195                         cpu_found = 1;
196                 }
197         }
198         /* if no detected cpu is off, use master core */
199         if (!cpu_found)
200                 CPU_SET(rte_get_master_lcore(), &cpuset);
201
202         ret = pthread_setaffinity_np(*thread, sizeof(cpuset), &cpuset);
203         if (ret < 0)
204                 goto fail;
205
206         pthread_barrier_wait(&params.configured);
207
208         return 0;
209
210 fail:
211         pthread_cancel(*thread);
212         pthread_join(*thread, NULL);
213         return ret;
214 }