7d0d581169537d0658954cd3686751010a554237
[dpdk.git] / examples / performance-thread / pthread_shim / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015 Intel Corporation
3  */
4
5 #define _GNU_SOURCE
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <inttypes.h>
10 #include <sys/types.h>
11 #include <string.h>
12 #include <sys/queue.h>
13 #include <stdarg.h>
14 #include <errno.h>
15 #include <getopt.h>
16 #include <unistd.h>
17 #include <sched.h>
18 #include <pthread.h>
19
20 #include <rte_common.h>
21 #include <rte_lcore.h>
22 #include <rte_per_lcore.h>
23 #include <rte_timer.h>
24
25 #include "lthread_api.h"
26 #include "lthread_diag_api.h"
27 #include "pthread_shim.h"
28
29 #define DEBUG_APP 0
30 #define HELLOW_WORLD_MAX_LTHREADS 10
31
32 #ifndef __GLIBC__ /* sched_getcpu() is glibc-specific */
33 #define sched_getcpu() rte_lcore_id()
34 #endif
35
36 __thread int print_count;
37 __thread pthread_mutex_t print_lock;
38
39 __thread pthread_mutex_t exit_lock;
40 __thread pthread_cond_t exit_cond;
41
42 /*
43  * A simple thread that demonstrates use of a mutex, a condition
44  * variable, thread local storage, explicit yield, and thread exit.
45  *
46  * The thread uses a mutex to protect a shared counter which is incremented
47  * and then it waits on condition variable before exiting.
48  *
49  * The thread argument is stored in and retrieved from TLS, using
50  * the pthread key create, get and set specific APIs.
51  *
52  * The thread yields while holding the mutex, to provide opportunity
53  * for other threads to contend.
54  *
55  * All of the pthread API functions used by this thread are actually
56  * resolved to corresponding lthread functions by the pthread shim
57  * implemented in pthread_shim.c
58  */
59 void *helloworld_pthread(void *arg);
60 void *helloworld_pthread(void *arg)
61 {
62         pthread_key_t key;
63
64         /* create a key for TLS */
65         pthread_key_create(&key, NULL);
66
67         /* store the arg in TLS */
68         pthread_setspecific(key, arg);
69
70         /* grab lock and increment shared counter */
71         pthread_mutex_lock(&print_lock);
72         print_count++;
73
74         /* yield thread to give opportunity for lock contention */
75         pthread_yield();
76
77         /* retrieve arg from TLS */
78         uint64_t thread_no = (uint64_t) pthread_getspecific(key);
79
80         printf("Hello - lcore = %d count = %d thread_no = %d thread_id = %p\n",
81                         sched_getcpu(),
82                         print_count,
83                         (int) thread_no,
84                         (void *)pthread_self());
85
86         /* release the lock */
87         pthread_mutex_unlock(&print_lock);
88
89         /*
90          * wait on condition variable
91          * before exiting
92          */
93         pthread_mutex_lock(&exit_lock);
94         pthread_cond_wait(&exit_cond, &exit_lock);
95         pthread_mutex_unlock(&exit_lock);
96
97         /* exit */
98         pthread_exit((void *) thread_no);
99 }
100
101
102 /*
103  * This is the initial thread
104  *
105  * It demonstrates pthread, mutex and condition variable creation,
106  * broadcast and pthread join APIs.
107  *
108  * This initial thread must always start life as an lthread.
109  *
110  * This thread creates many more threads then waits a short time
111  * before signalling them to exit using a broadcast.
112  *
113  * All of the pthread API functions used by this thread are actually
114  * resolved to corresponding lthread functions by the pthread shim
115  * implemented in pthread_shim.c
116  *
117  * After all threads have finished the lthread scheduler is shutdown
118  * and normal pthread operation is restored
119  */
120 __thread pthread_t tid[HELLOW_WORLD_MAX_LTHREADS];
121
122 static void *initial_lthread(void *args __attribute__((unused)))
123 {
124         int lcore = (int) rte_lcore_id();
125         /*
126          *
127          * We can now enable pthread API override
128          * and start to use the pthread APIs
129          */
130         pthread_override_set(1);
131
132         uint64_t i;
133         int ret;
134
135         /* initialize mutex for shared counter */
136         print_count = 0;
137         pthread_mutex_init(&print_lock, NULL);
138
139         /* initialize mutex and condition variable controlling thread exit */
140         pthread_mutex_init(&exit_lock, NULL);
141         pthread_cond_init(&exit_cond, NULL);
142
143         /* spawn a number of threads */
144         for (i = 0; i < HELLOW_WORLD_MAX_LTHREADS; i++) {
145
146                 /*
147                  * Not strictly necessary but
148                  * for the sake of this example
149                  * use an attribute to pass the desired lcore
150                  */
151                 pthread_attr_t attr;
152                 rte_cpuset_t cpuset;
153
154                 CPU_ZERO(&cpuset);
155                 CPU_SET(lcore, &cpuset);
156                 pthread_attr_init(&attr);
157                 pthread_attr_setaffinity_np(&attr, sizeof(rte_cpuset_t), &cpuset);
158
159                 /* create the thread */
160                 ret = pthread_create(&tid[i], &attr,
161                                 helloworld_pthread, (void *) i);
162                 if (ret != 0)
163                         rte_exit(EXIT_FAILURE, "Cannot create helloworld thread\n");
164         }
165
166         /* wait for 1s to allow threads
167          * to block on the condition variable
168          * N.B. nanosleep() is resolved to lthread_sleep()
169          * by the shim.
170          */
171         struct timespec time;
172
173         time.tv_sec = 1;
174         time.tv_nsec = 0;
175         nanosleep(&time, NULL);
176
177         /* wake up all the threads */
178         pthread_cond_broadcast(&exit_cond);
179
180         /* wait for them to finish */
181         for (i = 0; i < HELLOW_WORLD_MAX_LTHREADS; i++) {
182
183                 uint64_t thread_no;
184
185                 pthread_join(tid[i], (void *) &thread_no);
186                 if (thread_no != i)
187                         printf("error on thread exit\n");
188         }
189
190         pthread_cond_destroy(&exit_cond);
191         pthread_mutex_destroy(&print_lock);
192         pthread_mutex_destroy(&exit_lock);
193
194         /* shutdown the lthread scheduler */
195         lthread_scheduler_shutdown(rte_lcore_id());
196         lthread_detach();
197         return NULL;
198 }
199
200
201
202 /* This thread creates a single initial lthread
203  * and then runs the scheduler
204  * An instance of this thread is created on each thread
205  * in the core mask
206  */
207 static int
208 lthread_scheduler(void *args __attribute__((unused)))
209 {
210         /* create initial thread  */
211         struct lthread *lt;
212
213         lthread_create(&lt, -1, initial_lthread, (void *) NULL);
214
215         /* run the lthread scheduler */
216         lthread_run();
217
218         /* restore genuine pthread operation */
219         pthread_override_set(0);
220         return 0;
221 }
222
223 int main(int argc, char **argv)
224 {
225         int num_sched = 0;
226
227         /* basic DPDK initialization is all that is necessary to run lthreads*/
228         int ret = rte_eal_init(argc, argv);
229
230         if (ret < 0)
231                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
232
233         /* enable timer subsystem */
234         rte_timer_subsystem_init();
235
236 #if DEBUG_APP
237         lthread_diagnostic_set_mask(LT_DIAG_ALL);
238 #endif
239
240         /* create a scheduler on every core in the core mask
241          * and launch an initial lthread that will spawn many more.
242          */
243         unsigned lcore_id;
244
245         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
246                 if (rte_lcore_is_enabled(lcore_id))
247                         num_sched++;
248         }
249
250         /* set the number of schedulers, this forces all schedulers synchronize
251          * before entering their main loop
252          */
253         lthread_num_schedulers_set(num_sched);
254
255         /* launch all threads */
256         rte_eal_mp_remote_launch(lthread_scheduler, (void *)NULL, CALL_MASTER);
257
258         /* wait for threads to stop */
259         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
260                 rte_eal_wait_lcore(lcore_id);
261         }
262         return 0;
263 }