New upstream version 18.02
[deb_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);
123 static void initial_lthread(void *args __attribute__((unused)))
124 {
125         int lcore = (int) rte_lcore_id();
126         /*
127          *
128          * We can now enable pthread API override
129          * and start to use the pthread APIs
130          */
131         pthread_override_set(1);
132
133         uint64_t i;
134         int ret;
135
136         /* initialize mutex for shared counter */
137         print_count = 0;
138         pthread_mutex_init(&print_lock, NULL);
139
140         /* initialize mutex and condition variable controlling thread exit */
141         pthread_mutex_init(&exit_lock, NULL);
142         pthread_cond_init(&exit_cond, NULL);
143
144         /* spawn a number of threads */
145         for (i = 0; i < HELLOW_WORLD_MAX_LTHREADS; i++) {
146
147                 /*
148                  * Not strictly necessary but
149                  * for the sake of this example
150                  * use an attribute to pass the desired lcore
151                  */
152                 pthread_attr_t attr;
153                 rte_cpuset_t cpuset;
154
155                 CPU_ZERO(&cpuset);
156                 CPU_SET(lcore, &cpuset);
157                 pthread_attr_init(&attr);
158                 pthread_attr_setaffinity_np(&attr, sizeof(rte_cpuset_t), &cpuset);
159
160                 /* create the thread */
161                 ret = pthread_create(&tid[i], &attr,
162                                 helloworld_pthread, (void *) i);
163                 if (ret != 0)
164                         rte_exit(EXIT_FAILURE, "Cannot create helloworld thread\n");
165         }
166
167         /* wait for 1s to allow threads
168          * to block on the condition variable
169          * N.B. nanosleep() is resolved to lthread_sleep()
170          * by the shim.
171          */
172         struct timespec time;
173
174         time.tv_sec = 1;
175         time.tv_nsec = 0;
176         nanosleep(&time, NULL);
177
178         /* wake up all the threads */
179         pthread_cond_broadcast(&exit_cond);
180
181         /* wait for them to finish */
182         for (i = 0; i < HELLOW_WORLD_MAX_LTHREADS; i++) {
183
184                 uint64_t thread_no;
185
186                 pthread_join(tid[i], (void *) &thread_no);
187                 if (thread_no != i)
188                         printf("error on thread exit\n");
189         }
190
191         pthread_cond_destroy(&exit_cond);
192         pthread_mutex_destroy(&print_lock);
193         pthread_mutex_destroy(&exit_lock);
194
195         /* shutdown the lthread scheduler */
196         lthread_scheduler_shutdown(rte_lcore_id());
197         lthread_detach();
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);
209 static int
210 lthread_scheduler(void *args __attribute__((unused)))
211 {
212         /* create initial thread  */
213         struct lthread *lt;
214
215         lthread_create(&lt, -1, initial_lthread, (void *) NULL);
216
217         /* run the lthread scheduler */
218         lthread_run();
219
220         /* restore genuine pthread operation */
221         pthread_override_set(0);
222         return 0;
223 }
224
225 int main(int argc, char **argv)
226 {
227         int num_sched = 0;
228
229         /* basic DPDK initialization is all that is necessary to run lthreads*/
230         int ret = rte_eal_init(argc, argv);
231
232         if (ret < 0)
233                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
234
235         /* enable timer subsystem */
236         rte_timer_subsystem_init();
237
238 #if DEBUG_APP
239         lthread_diagnostic_set_mask(LT_DIAG_ALL);
240 #endif
241
242         /* create a scheduler on every core in the core mask
243          * and launch an initial lthread that will spawn many more.
244          */
245         unsigned lcore_id;
246
247         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
248                 if (rte_lcore_is_enabled(lcore_id))
249                         num_sched++;
250         }
251
252         /* set the number of schedulers, this forces all schedulers synchronize
253          * before entering their main loop
254          */
255         lthread_num_schedulers_set(num_sched);
256
257         /* launch all threads */
258         rte_eal_mp_remote_launch(lthread_scheduler, (void *)NULL, CALL_MASTER);
259
260         /* wait for threads to stop */
261         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
262                 rte_eal_wait_lcore(lcore_id);
263         }
264         return 0;
265 }