Use thread local storage for thread index
[vpp.git] / src / vlib / threads.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef included_vlib_threads_h
16 #define included_vlib_threads_h
17
18 #include <vlib/main.h>
19 #include <linux/sched.h>
20
21 extern vlib_main_t **vlib_mains;
22
23 void vlib_set_thread_name (char *name);
24
25 /* arg is actually a vlib__thread_t * */
26 typedef void (vlib_thread_function_t) (void *arg);
27
28 typedef struct vlib_thread_registration_
29 {
30   /* constructor generated list of thread registrations */
31   struct vlib_thread_registration_ *next;
32
33   /* config parameters */
34   char *name;
35   char *short_name;
36   vlib_thread_function_t *function;
37   uword mheap_size;
38   int fixed_count;
39   u32 count;
40   int no_data_structure_clone;
41   u32 frame_queue_nelts;
42
43   /* All threads of this type run on pthreads */
44   int use_pthreads;
45   u32 first_index;
46   uword *coremask;
47 } vlib_thread_registration_t;
48
49 /*
50  * Frames have their cpu / vlib_main_t index in the low-order N bits
51  * Make VLIB_MAX_CPUS a power-of-two, please...
52  */
53
54 #ifndef VLIB_MAX_CPUS
55 #define VLIB_MAX_CPUS 256
56 #endif
57
58 #if VLIB_MAX_CPUS > CLIB_MAX_MHEAPS
59 #error Please increase number of per-cpu mheaps
60 #endif
61
62 #define VLIB_CPU_MASK (VLIB_MAX_CPUS - 1)       /* 0x3f, max */
63 #define VLIB_OFFSET_MASK (~VLIB_CPU_MASK)
64
65 #define VLIB_LOG2_THREAD_STACK_SIZE (20)
66 #define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE)
67
68 typedef enum
69 {
70   VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME,
71 } vlib_frame_queue_msg_type_t;
72
73 typedef struct
74 {
75   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
76   volatile u32 valid;
77   u32 msg_type;
78   u32 n_vectors;
79   u32 last_n_vectors;
80
81   /* 256 * 4 = 1024 bytes, even mult of cache line size */
82   u32 buffer_index[VLIB_FRAME_SIZE];
83 }
84 vlib_frame_queue_elt_t;
85
86 typedef struct
87 {
88   /* First cache line */
89   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
90   volatile u32 *wait_at_barrier;
91   volatile u32 *workers_at_barrier;
92
93   /* Second Cache Line */
94     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
95   void *thread_mheap;
96   u8 *thread_stack;
97   void (*thread_function) (void *);
98   void *thread_function_arg;
99   i64 recursion_level;
100   elog_track_t elog_track;
101   u32 instance_id;
102   vlib_thread_registration_t *registration;
103   u8 *name;
104   u64 barrier_sync_count;
105
106   long lwp;
107   int lcore_id;
108   pthread_t thread_id;
109 } vlib_worker_thread_t;
110
111 extern vlib_worker_thread_t *vlib_worker_threads;
112
113 typedef struct
114 {
115   /* enqueue side */
116   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
117   volatile u64 tail;
118   u64 enqueues;
119   u64 enqueue_ticks;
120   u64 enqueue_vectors;
121   u32 enqueue_full_events;
122
123   /* dequeue side */
124     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
125   volatile u64 head;
126   u64 dequeues;
127   u64 dequeue_ticks;
128   u64 dequeue_vectors;
129   u64 trace;
130   u64 vector_threshold;
131
132   /* dequeue hint to enqueue side */
133     CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
134   volatile u64 head_hint;
135
136   /* read-only, constant, shared */
137     CLIB_CACHE_LINE_ALIGN_MARK (cacheline3);
138   vlib_frame_queue_elt_t *elts;
139   u32 nelts;
140 }
141 vlib_frame_queue_t;
142
143 typedef struct
144 {
145   u32 node_index;
146   vlib_frame_queue_t **vlib_frame_queues;
147
148   /* for frame queue tracing */
149   frame_queue_trace_t *frame_queue_traces;
150   frame_queue_nelt_counter_t *frame_queue_histogram;
151 } vlib_frame_queue_main_t;
152
153 /* Called early, in thread 0's context */
154 clib_error_t *vlib_thread_init (vlib_main_t * vm);
155
156 int vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
157                               u32 frame_queue_index, vlib_frame_t * frame,
158                               vlib_frame_queue_msg_type_t type);
159
160 int
161 vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm);
162
163 void vlib_worker_thread_node_runtime_update (void);
164
165 void vlib_create_worker_threads (vlib_main_t * vm, int n,
166                                  void (*thread_function) (void *));
167
168 void vlib_worker_thread_init (vlib_worker_thread_t * w);
169 u32 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts);
170
171 /* Check for a barrier sync request every 30ms */
172 #define BARRIER_SYNC_DELAY (0.030000)
173
174 #if CLIB_DEBUG > 0
175 /* long barrier timeout, for gdb... */
176 #define BARRIER_SYNC_TIMEOUT (600.1)
177 #else
178 #define BARRIER_SYNC_TIMEOUT (1.0)
179 #endif
180
181 void vlib_worker_thread_barrier_sync (vlib_main_t * vm);
182 void vlib_worker_thread_barrier_release (vlib_main_t * vm);
183
184 extern __thread uword vlib_thread_index;
185 static_always_inline uword
186 vlib_get_thread_index (void)
187 {
188   return vlib_thread_index;
189 }
190
191 always_inline void
192 vlib_smp_unsafe_warning (void)
193 {
194   if (CLIB_DEBUG > 0)
195     {
196       if (vlib_get_thread_index ())
197         fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
198     }
199 }
200
201 typedef enum
202 {
203   VLIB_WORKER_THREAD_FORK_FIXUP_ILLEGAL = 0,
204   VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX,
205 } vlib_fork_fixup_t;
206
207 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which);
208
209 #define foreach_vlib_main(body)                         \
210 do {                                                    \
211   vlib_main_t ** __vlib_mains = 0, *this_vlib_main;     \
212   int ii;                                               \
213                                                         \
214   for (ii = 0; ii < vec_len (vlib_mains); ii++)         \
215     {                                                   \
216       this_vlib_main = vlib_mains[ii];                  \
217       ASSERT (ii == 0 ||                                \
218               this_vlib_main->parked_at_barrier == 1);  \
219       if (this_vlib_main)                               \
220         vec_add1 (__vlib_mains, this_vlib_main);        \
221     }                                                   \
222                                                         \
223   for (ii = 0; ii < vec_len (__vlib_mains); ii++)       \
224     {                                                   \
225       this_vlib_main = __vlib_mains[ii];                \
226       /* body uses this_vlib_main... */                 \
227       (body);                                           \
228     }                                                   \
229   vec_free (__vlib_mains);                              \
230 } while (0);
231
232 #define foreach_sched_policy \
233   _(SCHED_OTHER, OTHER, "other") \
234   _(SCHED_BATCH, BATCH, "batch") \
235   _(SCHED_IDLE, IDLE, "idle")   \
236   _(SCHED_FIFO, FIFO, "fifo")   \
237   _(SCHED_RR, RR, "rr")
238
239 typedef enum
240 {
241 #define _(v,f,s) SCHED_POLICY_##f = v,
242   foreach_sched_policy
243 #undef _
244     SCHED_POLICY_N,
245 } sched_policy_t;
246
247 typedef struct
248 {
249   clib_error_t *(*vlib_launch_thread_cb) (void *fp, vlib_worker_thread_t * w,
250                                           unsigned lcore_id);
251   clib_error_t *(*vlib_thread_set_lcore_cb) (u32 thread, u16 lcore);
252 } vlib_thread_callbacks_t;
253
254 typedef struct
255 {
256   /* Link list of registrations, built by constructors */
257   vlib_thread_registration_t *next;
258
259   /* Vector of registrations, w/ non-data-structure clones at the top */
260   vlib_thread_registration_t **registrations;
261
262   uword *thread_registrations_by_name;
263
264   vlib_worker_thread_t *worker_threads;
265
266   /*
267    * Launch all threads as pthreads,
268    * not eal_rte_launch (strict affinity) threads
269    */
270   int use_pthreads;
271
272   /* Number of vlib_main / vnet_main clones */
273   u32 n_vlib_mains;
274
275   /* Number of thread stacks to create */
276   u32 n_thread_stacks;
277
278   /* Number of pthreads */
279   u32 n_pthreads;
280
281   /* Number of threads */
282   u32 n_threads;
283
284   /* Number of cores to skip, must match the core mask */
285   u32 skip_cores;
286
287   /* Thread prefix name */
288   u8 *thread_prefix;
289
290   /* main thread lcore */
291   u8 main_lcore;
292
293   /* Bitmap of available CPU cores */
294   uword *cpu_core_bitmap;
295
296   /* Bitmap of available CPU sockets (NUMA nodes) */
297   uword *cpu_socket_bitmap;
298
299   /* Worker handoff queues */
300   vlib_frame_queue_main_t *frame_queue_mains;
301
302   /* worker thread initialization barrier */
303   volatile u32 worker_thread_release;
304
305   /* scheduling policy */
306   u32 sched_policy;
307
308   /* scheduling policy priority */
309   u32 sched_priority;
310
311   /* callbacks */
312   vlib_thread_callbacks_t cb;
313   int extern_thread_mgmt;
314 } vlib_thread_main_t;
315
316 extern vlib_thread_main_t vlib_thread_main;
317
318 #include <vlib/global_funcs.h>
319
320 #define VLIB_REGISTER_THREAD(x,...)                     \
321   __VA_ARGS__ vlib_thread_registration_t x;             \
322 static void __vlib_add_thread_registration_##x (void)   \
323   __attribute__((__constructor__)) ;                    \
324 static void __vlib_add_thread_registration_##x (void)   \
325 {                                                       \
326   vlib_thread_main_t * tm = &vlib_thread_main;          \
327   x.next = tm->next;                                    \
328   tm->next = &x;                                        \
329 }                                                       \
330 __VA_ARGS__ vlib_thread_registration_t x
331
332 always_inline u32
333 vlib_num_workers ()
334 {
335   return vlib_thread_main.n_vlib_mains - 1;
336 }
337
338 always_inline u32
339 vlib_get_worker_thread_index (u32 worker_index)
340 {
341   return worker_index + 1;
342 }
343
344 always_inline u32
345 vlib_get_worker_index (u32 thread_index)
346 {
347   return thread_index - 1;
348 }
349
350 always_inline u32
351 vlib_get_current_worker_index ()
352 {
353   return vlib_get_thread_index () - 1;
354 }
355
356 static inline void
357 vlib_worker_thread_barrier_check (void)
358 {
359   if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
360     {
361       vlib_main_t *vm;
362       clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
363       if (CLIB_DEBUG > 0)
364         {
365           vm = vlib_get_main ();
366           vm->parked_at_barrier = 1;
367         }
368       while (*vlib_worker_threads->wait_at_barrier)
369         ;
370       if (CLIB_DEBUG > 0)
371         vm->parked_at_barrier = 0;
372       clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
373     }
374 }
375
376 always_inline vlib_main_t *
377 vlib_get_worker_vlib_main (u32 worker_index)
378 {
379   vlib_main_t *vm;
380   vlib_thread_main_t *tm = &vlib_thread_main;
381   ASSERT (worker_index < tm->n_vlib_mains - 1);
382   vm = vlib_mains[worker_index + 1];
383   ASSERT (vm);
384   return vm;
385 }
386
387 static inline void
388 vlib_put_frame_queue_elt (vlib_frame_queue_elt_t * hf)
389 {
390   CLIB_MEMORY_BARRIER ();
391   hf->valid = 1;
392 }
393
394 static inline vlib_frame_queue_elt_t *
395 vlib_get_frame_queue_elt (u32 frame_queue_index, u32 index)
396 {
397   vlib_frame_queue_t *fq;
398   vlib_frame_queue_elt_t *elt;
399   vlib_thread_main_t *tm = &vlib_thread_main;
400   vlib_frame_queue_main_t *fqm =
401     vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
402   u64 new_tail;
403
404   fq = fqm->vlib_frame_queues[index];
405   ASSERT (fq);
406
407   new_tail = __sync_add_and_fetch (&fq->tail, 1);
408
409   /* Wait until a ring slot is available */
410   while (new_tail >= fq->head_hint + fq->nelts)
411     vlib_worker_thread_barrier_check ();
412
413   elt = fq->elts + (new_tail & (fq->nelts - 1));
414
415   /* this would be very bad... */
416   while (elt->valid)
417     ;
418
419   elt->msg_type = VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME;
420   elt->last_n_vectors = elt->n_vectors = 0;
421
422   return elt;
423 }
424
425 static inline vlib_frame_queue_t *
426 is_vlib_frame_queue_congested (u32 frame_queue_index,
427                                u32 index,
428                                u32 queue_hi_thresh,
429                                vlib_frame_queue_t **
430                                handoff_queue_by_worker_index)
431 {
432   vlib_frame_queue_t *fq;
433   vlib_thread_main_t *tm = &vlib_thread_main;
434   vlib_frame_queue_main_t *fqm =
435     vec_elt_at_index (tm->frame_queue_mains, frame_queue_index);
436
437   fq = handoff_queue_by_worker_index[index];
438   if (fq != (vlib_frame_queue_t *) (~0))
439     return fq;
440
441   fq = fqm->vlib_frame_queues[index];
442   ASSERT (fq);
443
444   if (PREDICT_FALSE (fq->tail >= (fq->head_hint + queue_hi_thresh)))
445     {
446       /* a valid entry in the array will indicate the queue has reached
447        * the specified threshold and is congested
448        */
449       handoff_queue_by_worker_index[index] = fq;
450       fq->enqueue_full_events++;
451       return fq;
452     }
453
454   return NULL;
455 }
456
457 static inline vlib_frame_queue_elt_t *
458 vlib_get_worker_handoff_queue_elt (u32 frame_queue_index,
459                                    u32 vlib_worker_index,
460                                    vlib_frame_queue_elt_t **
461                                    handoff_queue_elt_by_worker_index)
462 {
463   vlib_frame_queue_elt_t *elt;
464
465   if (handoff_queue_elt_by_worker_index[vlib_worker_index])
466     return handoff_queue_elt_by_worker_index[vlib_worker_index];
467
468   elt = vlib_get_frame_queue_elt (frame_queue_index, vlib_worker_index);
469
470   handoff_queue_elt_by_worker_index[vlib_worker_index] = elt;
471
472   return elt;
473 }
474
475 u8 *vlib_thread_stack_init (uword thread_index);
476
477 int vlib_thread_cb_register (struct vlib_main_t *vm,
478                              vlib_thread_callbacks_t * cb);
479
480 #endif /* included_vlib_threads_h */
481
482 /*
483  * fd.io coding-style-patch-verification: ON
484  *
485  * Local Variables:
486  * eval: (c-set-style "gnu")
487  * End:
488  */