vlib api: move wrkr rpc flushing to vlib
[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 <vppinfra/callback.h>
20 #include <linux/sched.h>
21
22 void vlib_set_thread_name (char *name);
23
24 /* arg is actually a vlib__thread_t * */
25 typedef void (vlib_thread_function_t) (void *arg);
26
27 typedef struct vlib_thread_registration_
28 {
29   /* constructor generated list of thread registrations */
30   struct vlib_thread_registration_ *next;
31
32   /* config parameters */
33   char *name;
34   char *short_name;
35   vlib_thread_function_t *function;
36   uword mheap_size;
37   int fixed_count;
38   u32 count;
39   int no_data_structure_clone;
40   u32 frame_queue_nelts;
41
42   /* All threads of this type run on pthreads */
43   int use_pthreads;
44   u32 first_index;
45   uword *coremask;
46 } vlib_thread_registration_t;
47
48 #define VLIB_LOG2_THREAD_STACK_SIZE (21)
49 #define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE)
50
51 typedef struct
52 {
53   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
54   volatile u32 valid;
55   u32 maybe_trace : 1;
56   u32 n_vectors;
57   u32 offset;
58   STRUCT_MARK (end_of_reset);
59
60   CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
61   u32 buffer_index[VLIB_FRAME_SIZE];
62   u32 aux_data[VLIB_FRAME_SIZE];
63 }
64 vlib_frame_queue_elt_t;
65
66 typedef struct
67 {
68   /* First cache line */
69   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
70   volatile u32 *wait_at_barrier;
71   volatile u32 *workers_at_barrier;
72
73   /* Second Cache Line */
74     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
75   void *thread_mheap;
76   u8 *thread_stack;
77   void (*thread_function) (void *);
78   void *thread_function_arg;
79   i64 recursion_level;
80   elog_track_t elog_track;
81   u32 instance_id;
82   vlib_thread_registration_t *registration;
83   u8 *name;
84   u64 barrier_sync_count;
85   u8 barrier_elog_enabled;
86   const char *barrier_caller;
87   const char *barrier_context;
88   volatile u32 *node_reforks_required;
89   volatile u32 wait_before_barrier;
90   volatile u32 workers_before_barrier;
91   volatile u32 done_work_before_barrier;
92
93   long lwp;
94   int cpu_id;
95   int core_id;
96   int numa_id;
97   pthread_t thread_id;
98 } vlib_worker_thread_t;
99
100 extern vlib_worker_thread_t *vlib_worker_threads;
101
102 typedef struct
103 {
104   /* static data */
105   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
106   vlib_frame_queue_elt_t *elts;
107   u64 vector_threshold;
108   u64 trace;
109   u32 nelts;
110
111   /* modified by enqueue side  */
112   CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
113   volatile u64 tail;
114
115   /* modified by dequeue side  */
116   CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
117   volatile u64 head;
118 }
119 vlib_frame_queue_t;
120
121 struct vlib_frame_queue_main_t_;
122 typedef u32 (vlib_frame_queue_dequeue_fn_t) (
123   vlib_main_t *vm, struct vlib_frame_queue_main_t_ *fqm);
124 typedef struct vlib_frame_queue_main_t_
125 {
126   u32 node_index;
127   u32 frame_queue_nelts;
128
129   vlib_frame_queue_t **vlib_frame_queues;
130
131   /* for frame queue tracing */
132   frame_queue_trace_t *frame_queue_traces;
133   frame_queue_nelt_counter_t *frame_queue_histogram;
134   vlib_frame_queue_dequeue_fn_t *frame_queue_dequeue_fn;
135 } vlib_frame_queue_main_t;
136
137 typedef struct
138 {
139   uword node_index;
140   uword type_opaque;
141   uword data;
142 } vlib_process_signal_event_mt_args_t;
143
144 /* Called early, in thread 0's context */
145 clib_error_t *vlib_thread_init (vlib_main_t * vm);
146
147 void vlib_worker_thread_node_runtime_update (void);
148
149 void vlib_create_worker_threads (vlib_main_t * vm, int n,
150                                  void (*thread_function) (void *));
151
152 void vlib_worker_thread_init (vlib_worker_thread_t * w);
153 u32 vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts);
154
155 /* Check for a barrier sync request every 30ms */
156 #define BARRIER_SYNC_DELAY (0.030000)
157
158 #if CLIB_DEBUG > 0
159 /* long barrier timeout, for gdb... */
160 #define BARRIER_SYNC_TIMEOUT (600.1)
161 #else
162 #define BARRIER_SYNC_TIMEOUT (1.0)
163 #endif
164
165 #define vlib_worker_thread_barrier_sync(X) {vlib_worker_thread_barrier_sync_int(X, __FUNCTION__);}
166
167 void vlib_worker_thread_barrier_sync_int (vlib_main_t * vm,
168                                           const char *func_name);
169 void vlib_worker_thread_barrier_release (vlib_main_t * vm);
170 u8 vlib_worker_thread_barrier_held (void);
171 void vlib_worker_thread_initial_barrier_sync_and_release (vlib_main_t * vm);
172 void vlib_worker_thread_node_refork (void);
173 /**
174  * Wait until each of the workers has been once around the track
175  */
176 void vlib_worker_wait_one_loop (void);
177 /**
178  * Flush worker's pending rpc requests to main thread's rpc queue
179  */
180 void vlib_worker_flush_pending_rpc_requests (vlib_main_t *vm);
181
182 static_always_inline uword
183 vlib_get_thread_index (void)
184 {
185   return __os_thread_index;
186 }
187
188 always_inline void
189 vlib_smp_unsafe_warning (void)
190 {
191   if (CLIB_DEBUG > 0)
192     {
193       if (vlib_get_thread_index ())
194         fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
195     }
196 }
197
198 always_inline int
199 __foreach_vlib_main_helper (vlib_main_t *ii, vlib_main_t **p)
200 {
201   vlib_main_t *vm;
202   u32 index = ii - (vlib_main_t *) 0;
203
204   if (index >= vec_len (vlib_global_main.vlib_mains))
205     return 0;
206
207   *p = vm = vlib_global_main.vlib_mains[index];
208   ASSERT (index == 0 || vm->parked_at_barrier == 1);
209   return 1;
210 }
211
212 #define foreach_vlib_main()                                                   \
213   for (vlib_main_t *ii = 0, *this_vlib_main;                                  \
214        __foreach_vlib_main_helper (ii, &this_vlib_main); ii++)                \
215     if (this_vlib_main)
216
217 #define foreach_sched_policy \
218   _(SCHED_OTHER, OTHER, "other") \
219   _(SCHED_BATCH, BATCH, "batch") \
220   _(SCHED_IDLE, IDLE, "idle")   \
221   _(SCHED_FIFO, FIFO, "fifo")   \
222   _(SCHED_RR, RR, "rr")
223
224 typedef enum
225 {
226 #define _(v,f,s) SCHED_POLICY_##f = v,
227   foreach_sched_policy
228 #undef _
229     SCHED_POLICY_N,
230 } sched_policy_t;
231
232 typedef struct
233 {
234   /* Link list of registrations, built by constructors */
235   vlib_thread_registration_t *next;
236
237   /* Vector of registrations, w/ non-data-structure clones at the top */
238   vlib_thread_registration_t **registrations;
239
240   uword *thread_registrations_by_name;
241
242   vlib_worker_thread_t *worker_threads;
243
244   int use_pthreads;
245
246   /* Number of vlib_main / vnet_main clones */
247   u32 n_vlib_mains;
248
249   /* Number of thread stacks to create */
250   u32 n_thread_stacks;
251
252   /* Number of pthreads */
253   u32 n_pthreads;
254
255   /* Number of threads */
256   u32 n_threads;
257
258   /* Number of cores to skip, must match the core mask */
259   u32 skip_cores;
260
261   /* Thread prefix name */
262   u8 *thread_prefix;
263
264   /* main thread lcore */
265   u32 main_lcore;
266
267   /* Bitmap of available CPU cores */
268   uword *cpu_core_bitmap;
269
270   /* Bitmap of available CPU sockets (NUMA nodes) */
271   uword *cpu_socket_bitmap;
272
273   /* Worker handoff queues */
274   vlib_frame_queue_main_t *frame_queue_mains;
275
276   /* worker thread initialization barrier */
277   volatile u32 worker_thread_release;
278
279   /* scheduling policy */
280   u32 sched_policy;
281
282   /* scheduling policy priority */
283   u32 sched_priority;
284
285   /* NUMA-bound heap size */
286   uword numa_heap_size;
287
288 } vlib_thread_main_t;
289
290 extern vlib_thread_main_t vlib_thread_main;
291
292 #include <vlib/global_funcs.h>
293
294 #define VLIB_REGISTER_THREAD(x,...)                     \
295   __VA_ARGS__ vlib_thread_registration_t x;             \
296 static void __vlib_add_thread_registration_##x (void)   \
297   __attribute__((__constructor__)) ;                    \
298 static void __vlib_add_thread_registration_##x (void)   \
299 {                                                       \
300   vlib_thread_main_t * tm = &vlib_thread_main;          \
301   x.next = tm->next;                                    \
302   tm->next = &x;                                        \
303 }                                                       \
304 static void __vlib_rm_thread_registration_##x (void)    \
305   __attribute__((__destructor__)) ;                     \
306 static void __vlib_rm_thread_registration_##x (void)    \
307 {                                                       \
308   vlib_thread_main_t * tm = &vlib_thread_main;          \
309   VLIB_REMOVE_FROM_LINKED_LIST (tm->next, &x, next);    \
310 }                                                       \
311 __VA_ARGS__ vlib_thread_registration_t x
312
313 always_inline u32
314 vlib_num_workers ()
315 {
316   return vlib_thread_main.n_vlib_mains - 1;
317 }
318
319 always_inline u32
320 vlib_get_worker_thread_index (u32 worker_index)
321 {
322   return worker_index + 1;
323 }
324
325 always_inline u32
326 vlib_get_worker_index (u32 thread_index)
327 {
328   return thread_index - 1;
329 }
330
331 always_inline u32
332 vlib_get_current_worker_index ()
333 {
334   return vlib_get_thread_index () - 1;
335 }
336
337 static inline void
338 vlib_worker_thread_barrier_check (void)
339 {
340   if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
341     {
342       vlib_global_main_t *vgm = vlib_get_global_main ();
343       vlib_main_t *vm = vlib_get_main ();
344       u32 thread_index = vm->thread_index;
345       f64 t = vlib_time_now (vm);
346
347       if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
348         clib_call_callbacks (vm->barrier_perf_callbacks, vm,
349                              vm->clib_time.last_cpu_time, 0 /* enter */ );
350
351       if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
352         {
353           vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
354           /* *INDENT-OFF* */
355           ELOG_TYPE_DECLARE (e) = {
356             .format = "barrier-wait-thread-%d",
357             .format_args = "i4",
358           };
359           /* *INDENT-ON* */
360
361           struct
362           {
363             u32 thread_index;
364           } __clib_packed *ed;
365
366           ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
367           ed->thread_index = thread_index;
368         }
369
370       if (CLIB_DEBUG > 0)
371         {
372           vm = vlib_get_main ();
373           vm->parked_at_barrier = 1;
374         }
375       clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, 1);
376       while (*vlib_worker_threads->wait_at_barrier)
377         ;
378
379       /*
380        * Recompute the offset from thread-0 time.
381        * Note that vlib_time_now adds vm->time_offset, so
382        * clear it first. Save the resulting idea of "now", to
383        * see how well we're doing. See show_clock_command_fn(...)
384        */
385       {
386         f64 now;
387         vm->time_offset = 0.0;
388         now = vlib_time_now (vm);
389         vm->time_offset = vgm->vlib_mains[0]->time_last_barrier_release - now;
390         vm->time_last_barrier_release = vlib_time_now (vm);
391       }
392
393       if (CLIB_DEBUG > 0)
394         vm->parked_at_barrier = 0;
395       clib_atomic_fetch_add (vlib_worker_threads->workers_at_barrier, -1);
396
397       if (PREDICT_FALSE (*vlib_worker_threads->node_reforks_required))
398         {
399           if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
400             {
401               t = vlib_time_now (vm) - t;
402               vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
403               /* *INDENT-OFF* */
404               ELOG_TYPE_DECLARE (e) = {
405                 .format = "barrier-refork-thread-%d",
406                 .format_args = "i4",
407               };
408               /* *INDENT-ON* */
409
410               struct
411               {
412                 u32 thread_index;
413               } __clib_packed *ed;
414
415               ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e,
416                                     w->elog_track);
417               ed->thread_index = thread_index;
418             }
419
420           vlib_worker_thread_node_refork ();
421           clib_atomic_fetch_add (vlib_worker_threads->node_reforks_required,
422                                  -1);
423           while (*vlib_worker_threads->node_reforks_required)
424             ;
425         }
426       if (PREDICT_FALSE (vlib_worker_threads->barrier_elog_enabled))
427         {
428           t = vlib_time_now (vm) - t;
429           vlib_worker_thread_t *w = vlib_worker_threads + thread_index;
430           /* *INDENT-OFF* */
431           ELOG_TYPE_DECLARE (e) = {
432             .format = "barrier-released-thread-%d: %dus",
433             .format_args = "i4i4",
434           };
435           /* *INDENT-ON* */
436
437           struct
438           {
439             u32 thread_index;
440             u32 duration;
441           } __clib_packed *ed;
442
443           ed = ELOG_TRACK_DATA (&vlib_global_main.elog_main, e, w->elog_track);
444           ed->thread_index = thread_index;
445           ed->duration = (int) (1000000.0 * t);
446         }
447
448       if (PREDICT_FALSE (vec_len (vm->barrier_perf_callbacks) != 0))
449         clib_call_callbacks (vm->barrier_perf_callbacks, vm,
450                              vm->clib_time.last_cpu_time, 1 /* leave */ );
451     }
452 }
453
454 always_inline vlib_main_t *
455 vlib_get_worker_vlib_main (u32 worker_index)
456 {
457   vlib_main_t *vm;
458   vlib_thread_main_t *tm = &vlib_thread_main;
459   ASSERT (worker_index < tm->n_vlib_mains - 1);
460   vm = vlib_get_main_by_index (worker_index + 1);
461   ASSERT (vm);
462   return vm;
463 }
464
465 static inline u8
466 vlib_thread_is_main_w_barrier (void)
467 {
468   return (!vlib_num_workers ()
469           || ((vlib_get_thread_index () == 0
470                && vlib_worker_threads->wait_at_barrier[0])));
471 }
472
473 u8 *vlib_thread_stack_init (uword thread_index);
474 extern void *rpc_call_main_thread_cb_fn;
475
476 void
477 vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t *
478                                      args);
479 void vlib_rpc_call_main_thread (void *function, u8 * args, u32 size);
480 void vlib_get_thread_core_numa (vlib_worker_thread_t * w, unsigned cpu_id);
481 vlib_thread_main_t *vlib_get_thread_main_not_inline (void);
482
483 /**
484  * Force workers sync from within worker
485  *
486  * Must be paired with @ref vlib_workers_continue
487  */
488 void vlib_workers_sync (void);
489 /**
490  * Release barrier after workers sync
491  */
492 void vlib_workers_continue (void);
493
494 #endif /* included_vlib_threads_h */
495
496 /*
497  * fd.io coding-style-patch-verification: ON
498  *
499  * Local Variables:
500  * eval: (c-set-style "gnu")
501  * End:
502  */