f1798cebcb0bb115d2ab85f98caf1ea7ee80fab1
[dpdk.git] / lib / librte_eal / common / eal_common_thread.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <pthread.h>
39 #include <signal.h>
40 #include <sched.h>
41 #include <assert.h>
42 #include <string.h>
43
44 #include <rte_lcore.h>
45 #include <rte_memory.h>
46 #include <rte_log.h>
47
48 #include "eal_private.h"
49 #include "eal_thread.h"
50
51 RTE_DECLARE_PER_LCORE(unsigned , _socket_id);
52
53 unsigned rte_socket_id(void)
54 {
55         return RTE_PER_LCORE(_socket_id);
56 }
57
58 int
59 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
60 {
61         struct rte_config *cfg = rte_eal_get_configuration();
62
63         if (lcore_id >= RTE_MAX_LCORE)
64                 return -EINVAL;
65
66         if (cfg->lcore_role[lcore_id] == role)
67                 return 0;
68
69         return -EINVAL;
70 }
71
72 int eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
73 {
74         unsigned cpu = 0;
75         int socket_id = SOCKET_ID_ANY;
76         int sid;
77
78         if (cpusetp == NULL)
79                 return SOCKET_ID_ANY;
80
81         do {
82                 if (!CPU_ISSET(cpu, cpusetp))
83                         continue;
84
85                 if (socket_id == SOCKET_ID_ANY)
86                         socket_id = eal_cpu_socket_id(cpu);
87
88                 sid = eal_cpu_socket_id(cpu);
89                 if (socket_id != sid) {
90                         socket_id = SOCKET_ID_ANY;
91                         break;
92                 }
93
94         } while (++cpu < RTE_MAX_LCORE);
95
96         return socket_id;
97 }
98
99 int
100 rte_thread_set_affinity(rte_cpuset_t *cpusetp)
101 {
102         int s;
103         unsigned lcore_id;
104         pthread_t tid;
105
106         tid = pthread_self();
107
108         s = pthread_setaffinity_np(tid, sizeof(rte_cpuset_t), cpusetp);
109         if (s != 0) {
110                 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
111                 return -1;
112         }
113
114         /* store socket_id in TLS for quick access */
115         RTE_PER_LCORE(_socket_id) =
116                 eal_cpuset_socket_id(cpusetp);
117
118         /* store cpuset in TLS for quick access */
119         memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
120                 sizeof(rte_cpuset_t));
121
122         lcore_id = rte_lcore_id();
123         if (lcore_id != (unsigned)LCORE_ID_ANY) {
124                 /* EAL thread will update lcore_config */
125                 lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
126                 memmove(&lcore_config[lcore_id].cpuset, cpusetp,
127                         sizeof(rte_cpuset_t));
128         }
129
130         return 0;
131 }
132
133 void
134 rte_thread_get_affinity(rte_cpuset_t *cpusetp)
135 {
136         assert(cpusetp);
137         memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
138                 sizeof(rte_cpuset_t));
139 }
140
141 int
142 eal_thread_dump_affinity(char *str, unsigned size)
143 {
144         rte_cpuset_t cpuset;
145         unsigned cpu;
146         int ret;
147         unsigned int out = 0;
148
149         rte_thread_get_affinity(&cpuset);
150
151         for (cpu = 0; cpu < RTE_MAX_LCORE; cpu++) {
152                 if (!CPU_ISSET(cpu, &cpuset))
153                         continue;
154
155                 ret = snprintf(str + out,
156                                size - out, "%u,", cpu);
157                 if (ret < 0 || (unsigned)ret >= size - out) {
158                         /* string will be truncated */
159                         ret = -1;
160                         goto exit;
161                 }
162
163                 out += ret;
164         }
165
166         ret = 0;
167 exit:
168         /* remove the last separator */
169         if (out > 0)
170                 str[out - 1] = '\0';
171
172         return ret;
173 }
174
175
176 struct rte_thread_ctrl_params {
177         void *(*start_routine)(void *);
178         void *arg;
179         pthread_barrier_t launched;
180         pthread_barrier_t configured;
181 };
182
183 static void *rte_thread_init(void *arg)
184 {
185         struct rte_thread_ctrl_params *params = arg;
186         void *(*start_routine)(void *) = params->start_routine;
187         void *routine_arg = params->arg;
188
189         pthread_barrier_wait(&params->launched);
190         pthread_barrier_wait(&params->configured);
191
192         return start_routine(routine_arg);
193 }
194
195 int rte_ctrl_thread_create(pthread_t *thread, const char *name,
196                         const pthread_attr_t *attr,
197                         void *(*start_routine)(void *), void *arg)
198 {
199         struct rte_thread_ctrl_params params = {
200                 .start_routine = start_routine,
201                 .arg = arg,
202         };
203         unsigned int lcore_id;
204         rte_cpuset_t cpuset;
205         int set_affinity, ret;
206
207         pthread_barrier_init(&params.launched, NULL, 2);
208         pthread_barrier_init(&params.configured, NULL, 2);
209
210         ret = pthread_create(thread, attr, rte_thread_init, (void *)&params);
211         if (ret != 0)
212                 return ret;
213
214         pthread_barrier_wait(&params.launched);
215
216         if (name != NULL) {
217                 ret = rte_thread_setname(*thread, name);
218                 if (ret < 0)
219                         goto fail;
220         }
221
222         set_affinity = 0;
223         CPU_ZERO(&cpuset);
224         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
225                 if (eal_cpu_detected(lcore_id) &&
226                                 rte_lcore_has_role(lcore_id, ROLE_OFF)) {
227                         CPU_SET(lcore_id, &cpuset);
228                         set_affinity = 1;
229                 }
230         }
231         if (set_affinity) {
232                 ret = pthread_setaffinity_np(*thread, sizeof(cpuset), &cpuset);
233                 if (ret < 0)
234                         goto fail;
235         }
236
237         pthread_barrier_wait(&params.configured);
238
239         return 0;
240
241 fail:
242         pthread_kill(*thread, SIGTERM);
243         pthread_join(*thread, NULL);
244         return ret;
245 }