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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef included_vlib_threads_h
16 #define included_vlib_threads_h
18 #include <vlib/main.h>
20 vlib_main_t **vlib_mains;
22 void vlib_set_thread_name (char *name);
24 /* arg is actually a vlib__thread_t * */
25 typedef void (vlib_thread_function_t) (void *arg);
27 typedef struct vlib_thread_registration_
29 /* constructor generated list of thread registrations */
30 struct vlib_thread_registration_ *next;
32 /* config parameters */
35 vlib_thread_function_t *function;
39 int no_data_structure_clone;
40 u32 frame_queue_nelts;
42 /* All threads of this type run on pthreads */
46 } vlib_thread_registration_t;
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...
54 #define VLIB_MAX_CPUS 256
57 #if VLIB_MAX_CPUS > CLIB_MAX_MHEAPS
58 #error Please increase number of per-cpu mheaps
61 #define VLIB_CPU_MASK (VLIB_MAX_CPUS - 1) /* 0x3f, max */
62 #define VLIB_OFFSET_MASK (~VLIB_CPU_MASK)
64 #define VLIB_LOG2_THREAD_STACK_SIZE (20)
65 #define VLIB_THREAD_STACK_SIZE (1<<VLIB_LOG2_THREAD_STACK_SIZE)
69 VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME,
70 } vlib_frame_queue_msg_type_t;
79 /* 256 * 4 = 1024 bytes, even mult of cache line size */
80 u32 buffer_index[VLIB_FRAME_SIZE];
82 /* Pad to a cache line boundary */
83 u8 pad[CLIB_CACHE_LINE_BYTES - 4 * sizeof (u32)];
85 vlib_frame_queue_elt_t;
89 /* First cache line */
90 volatile u32 *wait_at_barrier;
91 volatile u32 *workers_at_barrier;
92 u8 pad0[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32 *))];
94 /* Second Cache Line */
97 void (*thread_function) (void *);
98 void *thread_function_arg;
100 elog_track_t elog_track;
102 vlib_thread_registration_t *registration;
104 u64 barrier_sync_count;
108 } vlib_worker_thread_t;
110 vlib_worker_thread_t *vlib_worker_threads;
119 u32 enqueue_full_events;
120 u32 enqueue_efd_discards;
121 u8 pad2[CLIB_CACHE_LINE_BYTES - (2 * sizeof (u32)) - (4 * sizeof (u64))];
129 u64 vector_threshold;
130 u8 pad4[CLIB_CACHE_LINE_BYTES - (6 * sizeof (u64))];
132 /* dequeue hint to enqueue side */
133 volatile u64 head_hint;
134 u8 pad5[CLIB_CACHE_LINE_BYTES - sizeof (u64)];
136 /* read-only, constant, shared */
137 vlib_frame_queue_elt_t *elts;
142 vlib_frame_queue_t **vlib_frame_queues;
144 /* Called early, in thread 0's context */
145 clib_error_t *vlib_thread_init (vlib_main_t * vm);
147 vlib_worker_thread_t *vlib_alloc_thread (vlib_main_t * vm);
149 int vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index,
150 u32 frame_queue_index, vlib_frame_t * frame,
151 vlib_frame_queue_msg_type_t type);
153 int vlib_frame_queue_dequeue (int thread_id,
154 vlib_main_t * vm, vlib_node_main_t * nm);
156 u64 dispatch_node (vlib_main_t * vm,
157 vlib_node_runtime_t * node,
158 vlib_node_type_t type,
159 vlib_node_state_t dispatch_state,
160 vlib_frame_t * frame, u64 last_time_stamp);
162 u64 dispatch_pending_node (vlib_main_t * vm,
163 vlib_pending_frame_t * p, u64 last_time_stamp);
165 void vlib_worker_thread_node_runtime_update (void);
167 void vlib_create_worker_threads (vlib_main_t * vm, int n,
168 void (*thread_function) (void *));
170 void vlib_worker_thread_init (vlib_worker_thread_t * w);
172 /* Check for a barrier sync request every 30ms */
173 #define BARRIER_SYNC_DELAY (0.030000)
176 /* long barrier timeout, for gdb... */
177 #define BARRIER_SYNC_TIMEOUT (600.1)
179 #define BARRIER_SYNC_TIMEOUT (1.0)
182 void vlib_worker_thread_barrier_sync (vlib_main_t * vm);
183 void vlib_worker_thread_barrier_release (vlib_main_t * vm);
186 vlib_smp_unsafe_warning (void)
190 if (os_get_cpu_number ())
191 fformat (stderr, "%s: SMP unsafe warning...\n", __FUNCTION__);
197 VLIB_WORKER_THREAD_FORK_FIXUP_ILLEGAL = 0,
198 VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX,
201 void vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which);
204 vlib_worker_thread_barrier_check (void)
206 if (PREDICT_FALSE (*vlib_worker_threads->wait_at_barrier))
208 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1);
209 while (*vlib_worker_threads->wait_at_barrier)
211 clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1);
215 #define foreach_vlib_main(body) \
217 vlib_main_t ** __vlib_mains = 0, *this_vlib_main; \
220 if (vec_len (vlib_mains) == 0) \
221 vec_add1 (__vlib_mains, &vlib_global_main); \
224 for (ii = 0; ii < vec_len (vlib_mains); ii++) \
226 this_vlib_main = vlib_mains[ii]; \
227 if (this_vlib_main) \
228 vec_add1 (__vlib_mains, this_vlib_main); \
232 for (ii = 0; ii < vec_len (__vlib_mains); ii++) \
234 this_vlib_main = __vlib_mains[ii]; \
235 /* body uses this_vlib_main... */ \
238 vec_free (__vlib_mains); \
242 /* Early-Fast-Discard (EFD) */
243 #define VLIB_EFD_DISABLED 0
244 #define VLIB_EFD_DISCARD_ENABLED (1 << 0)
245 #define VLIB_EFD_MONITOR_ENABLED (1 << 1)
247 #define VLIB_EFD_DEF_WORKER_HI_THRESH_PCT 90
249 /* EFD worker thread settings */
250 typedef struct vlib_efd_t
262 /* Link list of registrations, built by constructors */
263 vlib_thread_registration_t *next;
265 /* Vector of registrations, w/ non-data-structure clones at the top */
266 vlib_thread_registration_t **registrations;
268 uword *thread_registrations_by_name;
270 vlib_worker_thread_t *worker_threads;
273 * Launch all threads as pthreads,
274 * not eal_rte_launch (strict affinity) threads
278 /* Number of vlib_main / vnet_main clones */
281 /* Number of thread stacks to create */
284 /* Number of pthreads */
287 /* Number of DPDK eal threads */
290 /* Number of cores to skip, must match the core mask */
293 /* Thread prefix name */
296 /* main thread lcore */
299 /* Bitmap of available CPU cores */
300 uword *cpu_core_bitmap;
302 /* Bitmap of available CPU sockets (NUMA nodes) */
303 uword *cpu_socket_bitmap;
307 /* handoff node index */
308 u32 handoff_dispatch_node_index;
310 /* for frame queue tracing */
311 frame_queue_trace_t *frame_queue_traces;
312 frame_queue_nelt_counter_t *frame_queue_histogram;
314 /* worker thread initialization barrier */
315 volatile u32 worker_thread_release;
317 } vlib_thread_main_t;
319 vlib_thread_main_t vlib_thread_main;
321 #define VLIB_REGISTER_THREAD(x,...) \
322 __VA_ARGS__ vlib_thread_registration_t x; \
323 static void __vlib_add_thread_registration_##x (void) \
324 __attribute__((__constructor__)) ; \
325 static void __vlib_add_thread_registration_##x (void) \
327 vlib_thread_main_t * tm = &vlib_thread_main; \
331 __VA_ARGS__ vlib_thread_registration_t x
333 #endif /* included_vlib_threads_h */
336 * fd.io coding-style-patch-verification: ON
339 * eval: (c-set-style "gnu")