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