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