]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/lib/librte_eal/linux/eal_thread.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / dpdk / lib / librte_eal / linux / eal_thread.c
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 <stdint.h>
9 #include <unistd.h>
10 #include <pthread.h>
11 #include <sched.h>
12 #include <sys/queue.h>
13 #include <sys/syscall.h>
14
15 #include <rte_debug.h>
16 #include <rte_atomic.h>
17 #include <rte_launch.h>
18 #include <rte_log.h>
19 #include <rte_memory.h>
20 #include <rte_per_lcore.h>
21 #include <rte_eal.h>
22 #include <rte_lcore.h>
23 #include <rte_eal_trace.h>
24
25 #include "eal_private.h"
26 #include "eal_thread.h"
27
28 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
29 RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
30 RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
31
32 /*
33 * Send a message to a slave lcore identified by slave_id to call a
34 * function f with argument arg. Once the execution is done, the
35 * remote lcore switch in FINISHED state.
36 */
37 int
38 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
39 {
40 int n;
41 char c = 0;
42 int m2s = lcore_config[slave_id].pipe_master2slave[1];
43 int s2m = lcore_config[slave_id].pipe_slave2master[0];
44 int rc = -EBUSY;
45
46 if (lcore_config[slave_id].state != WAIT)
47 goto finish;
48
49 lcore_config[slave_id].f = f;
50 lcore_config[slave_id].arg = arg;
51
52 /* send message */
53 n = 0;
54 while (n == 0 || (n < 0 && errno == EINTR))
55 n = write(m2s, &c, 1);
56 if (n < 0)
57 rte_panic("cannot write on configuration pipe\n");
58
59 /* wait ack */
60 do {
61 n = read(s2m, &c, 1);
62 } while (n < 0 && errno == EINTR);
63
64 if (n <= 0)
65 rte_panic("cannot read on configuration pipe\n");
66
67 rc = 0;
68 finish:
69 rte_eal_trace_thread_remote_launch(f, arg, slave_id, rc);
70 return rc;
71 }
72
73 /* set affinity for current EAL thread */
74 static int
75 eal_thread_set_affinity(void)
76 {
77 unsigned lcore_id = rte_lcore_id();
78
79 /* acquire system unique id */
80 rte_gettid();
81
82 /* update EAL thread core affinity */
83 return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
84 }
85
86 void eal_thread_init_master(unsigned lcore_id)
87 {
88 /* set the lcore ID in per-lcore memory area */
89 RTE_PER_LCORE(_lcore_id) = lcore_id;
90
91 /* set CPU affinity */
92 if (eal_thread_set_affinity() < 0)
93 rte_panic("cannot set affinity\n");
94 }
95
96 /* main loop of threads */
97 __rte_noreturn void *
98 eal_thread_loop(__rte_unused void *arg)
99 {
100 char c;
101 int n, ret;
102 unsigned lcore_id;
103 pthread_t thread_id;
104 int m2s, s2m;
105 char cpuset[RTE_CPU_AFFINITY_STR_LEN];
106
107 thread_id = pthread_self();
108
109 /* retrieve our lcore_id from the configuration structure */
110 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
111 if (thread_id == lcore_config[lcore_id].thread_id)
112 break;
113 }
114 if (lcore_id == RTE_MAX_LCORE)
115 rte_panic("cannot retrieve lcore id\n");
116
117 m2s = lcore_config[lcore_id].pipe_master2slave[0];
118 s2m = lcore_config[lcore_id].pipe_slave2master[1];
119
120 /* set the lcore ID in per-lcore memory area */
121 RTE_PER_LCORE(_lcore_id) = lcore_id;
122
123 /* set CPU affinity */
124 if (eal_thread_set_affinity() < 0)
125 rte_panic("cannot set affinity\n");
126
127 ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
128
129 RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
130 lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "...");
131
132 __rte_trace_mem_per_thread_alloc();
133 rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
134
135 /* read on our pipe to get commands */
136 while (1) {
137 void *fct_arg;
138
139 /* wait command */
140 do {
141 n = read(m2s, &c, 1);
142 } while (n < 0 && errno == EINTR);
143
144 if (n <= 0)
145 rte_panic("cannot read on configuration pipe\n");
146
147 lcore_config[lcore_id].state = RUNNING;
148
149 /* send ack */
150 n = 0;
151 while (n == 0 || (n < 0 && errno == EINTR))
152 n = write(s2m, &c, 1);
153 if (n < 0)
154 rte_panic("cannot write on configuration pipe\n");
155
156 if (lcore_config[lcore_id].f == NULL)
157 rte_panic("NULL function pointer\n");
158
159 /* call the function and store the return value */
160 fct_arg = lcore_config[lcore_id].arg;
161 ret = lcore_config[lcore_id].f(fct_arg);
162 lcore_config[lcore_id].ret = ret;
163 rte_wmb();
164
165 /* when a service core returns, it should go directly to WAIT
166 * state, because the application will not lcore_wait() for it.
167 */
168 if (lcore_config[lcore_id].core_role == ROLE_SERVICE)
169 lcore_config[lcore_id].state = WAIT;
170 else
171 lcore_config[lcore_id].state = FINISHED;
172 }
173
174 /* never reached */
175 /* pthread_exit(NULL); */
176 /* return NULL; */
177 }
178
179 /* require calling thread tid by gettid() */
180 int rte_sys_gettid(void)
181 {
182 return (int)syscall(SYS_gettid);
183 }
184
185 int rte_thread_setname(pthread_t id, const char *name)
186 {
187 int ret = ENOSYS;
188 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
189 #if __GLIBC_PREREQ(2, 12)
190 ret = pthread_setname_np(id, name);
191 #endif
192 #endif
193 RTE_SET_USED(id);
194 RTE_SET_USED(name);
195 return -ret;
196 }
197
198 int rte_thread_getname(pthread_t id, char *name, size_t len)
199 {
200 int ret = ENOSYS;
201 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
202 #if __GLIBC_PREREQ(2, 12)
203 ret = pthread_getname_np(id, name, len);
204 #endif
205 #endif
206 RTE_SET_USED(id);
207 RTE_SET_USED(name);
208 RTE_SET_USED(len);
209 return -ret;
210
211 }