Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | */ |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 16 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 17 | #include <signal.h> |
| 18 | #include <math.h> |
| 19 | #include <vppinfra/format.h> |
| 20 | #include <vlib/vlib.h> |
| 21 | |
| 22 | #include <vlib/threads.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | #include <vlib/unix/cj.h> |
| 24 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 25 | DECLARE_CJ_GLOBAL_LOG; |
| 26 | |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 27 | #define FRAME_QUEUE_NELTS 64 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 29 | u32 |
| 30 | vl (void *p) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | { |
| 32 | return vec_len (p); |
| 33 | } |
| 34 | |
Damjan Marion | 6a7acc2 | 2016-12-19 16:28:36 +0100 | [diff] [blame] | 35 | vlib_worker_thread_t *vlib_worker_threads; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | vlib_thread_main_t vlib_thread_main; |
| 37 | |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 38 | /* |
| 39 | * Barrier tracing can be enabled on a normal build to collect information |
| 40 | * on barrier use, including timings and call stacks. Deliberately not |
| 41 | * keyed off CLIB_DEBUG, because that can add significant overhead which |
| 42 | * imapacts observed timings. |
| 43 | */ |
| 44 | |
| 45 | #ifdef BARRIER_TRACING |
| 46 | /* |
| 47 | * Output of barrier tracing can be to syslog or elog as suits |
| 48 | */ |
| 49 | #ifdef BARRIER_TRACING_ELOG |
| 50 | static u32 |
| 51 | elog_id_for_msg_name (const char *msg_name) |
| 52 | { |
| 53 | uword *p, r; |
| 54 | static uword *h; |
| 55 | u8 *name_copy; |
| 56 | |
| 57 | if (!h) |
| 58 | h = hash_create_string (0, sizeof (uword)); |
| 59 | |
| 60 | p = hash_get_mem (h, msg_name); |
| 61 | if (p) |
| 62 | return p[0]; |
| 63 | r = elog_string (&vlib_global_main.elog_main, "%s", msg_name); |
| 64 | |
| 65 | name_copy = format (0, "%s%c", msg_name, 0); |
| 66 | |
| 67 | hash_set_mem (h, name_copy, r); |
| 68 | |
| 69 | return r; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * elog Barrier trace functions, which are nulled out if BARRIER_TRACING isn't |
| 74 | * defined |
| 75 | */ |
| 76 | |
| 77 | static inline void |
| 78 | barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed) |
| 79 | { |
| 80 | /* *INDENT-OFF* */ |
| 81 | ELOG_TYPE_DECLARE (e) = |
| 82 | { |
| 83 | .format = "barrier <%d#%s(O:%dus:%dus)(%dus)", |
| 84 | .format_args = "i4T4i4i4i4", |
| 85 | }; |
| 86 | /* *INDENT-ON* */ |
| 87 | struct |
| 88 | { |
| 89 | u32 count, caller, t_entry, t_open, t_closed; |
| 90 | } *ed = 0; |
| 91 | |
| 92 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 93 | ed->count = (int) vlib_worker_threads[0].barrier_sync_count; |
| 94 | ed->caller = elog_id_for_msg_name (vlib_worker_threads[0].barrier_caller); |
| 95 | ed->t_entry = (int) (1000000.0 * t_entry); |
| 96 | ed->t_open = (int) (1000000.0 * t_open); |
| 97 | ed->t_closed = (int) (1000000.0 * t_closed); |
| 98 | } |
| 99 | |
| 100 | static inline void |
| 101 | barrier_trace_sync_rec (f64 t_entry) |
| 102 | { |
| 103 | /* *INDENT-OFF* */ |
| 104 | ELOG_TYPE_DECLARE (e) = |
| 105 | { |
| 106 | .format = "barrier <%d(%dus)%s", |
| 107 | .format_args = "i4i4T4", |
| 108 | }; |
| 109 | /* *INDENT-ON* */ |
| 110 | struct |
| 111 | { |
| 112 | u32 depth, t_entry, caller; |
| 113 | } *ed = 0; |
| 114 | |
| 115 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 116 | ed->depth = (int) vlib_worker_threads[0].recursion_level - 1; |
| 117 | ed->t_entry = (int) (1000000.0 * t_entry); |
| 118 | ed->caller = elog_id_for_msg_name (vlib_worker_threads[0].barrier_caller); |
| 119 | } |
| 120 | |
| 121 | static inline void |
| 122 | barrier_trace_release_rec (f64 t_entry) |
| 123 | { |
| 124 | /* *INDENT-OFF* */ |
| 125 | ELOG_TYPE_DECLARE (e) = |
| 126 | { |
| 127 | .format = "barrier (%dus)%d>", |
| 128 | .format_args = "i4i4", |
| 129 | }; |
| 130 | /* *INDENT-ON* */ |
| 131 | struct |
| 132 | { |
| 133 | u32 t_entry, depth; |
| 134 | } *ed = 0; |
| 135 | |
| 136 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 137 | ed->t_entry = (int) (1000000.0 * t_entry); |
| 138 | ed->depth = (int) vlib_worker_threads[0].recursion_level; |
| 139 | } |
| 140 | |
| 141 | static inline void |
| 142 | barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main) |
| 143 | { |
| 144 | /* *INDENT-OFF* */ |
| 145 | ELOG_TYPE_DECLARE (e) = |
| 146 | { |
| 147 | .format = "barrier (%dus){%d}(C:%dus)#%d>", |
| 148 | .format_args = "i4i4i4i4", |
| 149 | }; |
| 150 | /* *INDENT-ON* */ |
| 151 | struct |
| 152 | { |
| 153 | u32 t_entry, t_update_main, t_closed_total, count; |
| 154 | } *ed = 0; |
| 155 | |
| 156 | ed = ELOG_DATA (&vlib_global_main.elog_main, e); |
| 157 | ed->t_entry = (int) (1000000.0 * t_entry); |
| 158 | ed->t_update_main = (int) (1000000.0 * t_update_main); |
| 159 | ed->t_closed_total = (int) (1000000.0 * t_closed_total); |
| 160 | ed->count = (int) vlib_worker_threads[0].barrier_sync_count; |
| 161 | |
| 162 | /* Reset context for next trace */ |
| 163 | vlib_worker_threads[0].barrier_context = NULL; |
| 164 | } |
| 165 | #else |
| 166 | char barrier_trace[65536]; |
| 167 | char *btp = barrier_trace; |
| 168 | |
| 169 | /* |
| 170 | * syslog Barrier trace functions, which are nulled out if BARRIER_TRACING |
| 171 | * isn't defined |
| 172 | */ |
| 173 | |
| 174 | |
| 175 | static inline void |
| 176 | barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed) |
| 177 | { |
| 178 | btp += sprintf (btp, "<%u#%s", |
| 179 | (unsigned int) vlib_worker_threads[0].barrier_sync_count, |
| 180 | vlib_worker_threads[0].barrier_caller); |
| 181 | |
| 182 | if (vlib_worker_threads[0].barrier_context) |
| 183 | { |
| 184 | btp += sprintf (btp, "[%s]", vlib_worker_threads[0].barrier_context); |
| 185 | |
| 186 | } |
| 187 | |
| 188 | btp += sprintf (btp, "(O:%dus:%dus)(%dus):", |
| 189 | (int) (1000000.0 * t_entry), |
| 190 | (int) (1000000.0 * t_open), (int) (1000000.0 * t_closed)); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | static inline void |
| 195 | barrier_trace_sync_rec (f64 t_entry) |
| 196 | { |
| 197 | btp += sprintf (btp, "<%u(%dus)%s:", |
| 198 | (int) vlib_worker_threads[0].recursion_level - 1, |
| 199 | (int) (1000000.0 * t_entry), |
| 200 | vlib_worker_threads[0].barrier_caller); |
| 201 | } |
| 202 | |
| 203 | static inline void |
| 204 | barrier_trace_release_rec (f64 t_entry) |
| 205 | { |
| 206 | btp += sprintf (btp, ":(%dus)%u>", (int) (1000000.0 * t_entry), |
| 207 | (int) vlib_worker_threads[0].recursion_level); |
| 208 | } |
| 209 | |
| 210 | static inline void |
| 211 | barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main) |
| 212 | { |
| 213 | |
| 214 | btp += sprintf (btp, ":(%dus)", (int) (1000000.0 * t_entry)); |
| 215 | if (t_update_main > 0) |
| 216 | { |
| 217 | btp += sprintf (btp, "{%dus}", (int) (1000000.0 * t_update_main)); |
| 218 | } |
| 219 | |
| 220 | btp += sprintf (btp, "(C:%dus)#%u>", |
| 221 | (int) (1000000.0 * t_closed_total), |
| 222 | (int) vlib_worker_threads[0].barrier_sync_count); |
| 223 | |
| 224 | /* Dump buffer to syslog, and reset for next trace */ |
| 225 | fformat (stderr, "BTRC %s\n", barrier_trace); |
| 226 | btp = barrier_trace; |
| 227 | vlib_worker_threads[0].barrier_context = NULL; |
| 228 | } |
| 229 | #endif |
| 230 | #else |
| 231 | |
| 232 | /* Null functions for default case where barrier tracing isn't used */ |
| 233 | static inline void |
| 234 | barrier_trace_sync (f64 t_entry, f64 t_open, f64 t_closed) |
| 235 | { |
| 236 | } |
| 237 | |
| 238 | static inline void |
| 239 | barrier_trace_sync_rec (f64 t_entry) |
| 240 | { |
| 241 | } |
| 242 | |
| 243 | static inline void |
| 244 | barrier_trace_release_rec (f64 t_entry) |
| 245 | { |
| 246 | } |
| 247 | |
| 248 | static inline void |
| 249 | barrier_trace_release (f64 t_entry, f64 t_closed_total, f64 t_update_main) |
| 250 | { |
| 251 | } |
| 252 | #endif |
| 253 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | uword |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 255 | os_get_nthreads (void) |
Dave Barach | 01d86c7 | 2016-08-08 15:13:42 -0400 | [diff] [blame] | 256 | { |
| 257 | u32 len; |
| 258 | |
| 259 | len = vec_len (vlib_thread_stacks); |
| 260 | if (len == 0) |
| 261 | return 1; |
| 262 | else |
| 263 | return len; |
| 264 | } |
| 265 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | void |
| 267 | vlib_set_thread_name (char *name) |
| 268 | { |
| 269 | int pthread_setname_np (pthread_t __target_thread, const char *__name); |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 270 | int rv; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 271 | pthread_t thread = pthread_self (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 272 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 273 | if (thread) |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 274 | { |
| 275 | rv = pthread_setname_np (thread, name); |
| 276 | if (rv) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 277 | clib_warning ("pthread_setname_np returned %d", rv); |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 278 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 281 | static int |
| 282 | sort_registrations_by_no_clone (void *a0, void *a1) |
| 283 | { |
| 284 | vlib_thread_registration_t **tr0 = a0; |
| 285 | vlib_thread_registration_t **tr1 = a1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 287 | return ((i32) ((*tr0)->no_data_structure_clone) |
| 288 | - ((i32) ((*tr1)->no_data_structure_clone))); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static uword * |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 292 | clib_sysfs_list_to_bitmap (char *filename) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | { |
| 294 | FILE *fp; |
| 295 | uword *r = 0; |
| 296 | |
| 297 | fp = fopen (filename, "r"); |
| 298 | |
| 299 | if (fp != NULL) |
| 300 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 301 | u8 *buffer = 0; |
| 302 | vec_validate (buffer, 256 - 1); |
| 303 | if (fgets ((char *) buffer, 256, fp)) |
| 304 | { |
| 305 | unformat_input_t in; |
| 306 | unformat_init_string (&in, (char *) buffer, |
| 307 | strlen ((char *) buffer)); |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 308 | if (unformat (&in, "%U", unformat_bitmap_list, &r) != 1) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 309 | clib_warning ("unformat_bitmap_list failed"); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 310 | unformat_free (&in); |
| 311 | } |
| 312 | vec_free (buffer); |
| 313 | fclose (fp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 314 | } |
| 315 | return r; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /* Called early in the init sequence */ |
| 320 | |
| 321 | clib_error_t * |
| 322 | vlib_thread_init (vlib_main_t * vm) |
| 323 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 324 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 325 | vlib_worker_thread_t *w; |
| 326 | vlib_thread_registration_t *tr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 327 | u32 n_vlib_mains = 1; |
| 328 | u32 first_index = 1; |
| 329 | u32 i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 330 | uword *avail_cpu; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | |
| 332 | /* get bitmaps of active cpu cores and sockets */ |
| 333 | tm->cpu_core_bitmap = |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 334 | clib_sysfs_list_to_bitmap ("/sys/devices/system/cpu/online"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | tm->cpu_socket_bitmap = |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 336 | clib_sysfs_list_to_bitmap ("/sys/devices/system/node/online"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 338 | avail_cpu = clib_bitmap_dup (tm->cpu_core_bitmap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | |
| 340 | /* skip cores */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 341 | for (i = 0; i < tm->skip_cores; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 343 | uword c = clib_bitmap_first_set (avail_cpu); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | if (c == ~0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 345 | return clib_error_return (0, "no available cpus to skip"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 347 | avail_cpu = clib_bitmap_set (avail_cpu, c, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* grab cpu for main thread */ |
Damjan Marion | 858151f | 2018-07-11 10:51:00 +0200 | [diff] [blame] | 351 | if (tm->main_lcore == ~0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | { |
Damjan Marion | 858151f | 2018-07-11 10:51:00 +0200 | [diff] [blame] | 353 | /* if main-lcore is not set, we try to use lcore 1 */ |
| 354 | if (clib_bitmap_get (avail_cpu, 1)) |
| 355 | tm->main_lcore = 1; |
| 356 | else |
| 357 | tm->main_lcore = clib_bitmap_first_set (avail_cpu); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 358 | if (tm->main_lcore == (u8) ~ 0) |
| 359 | return clib_error_return (0, "no available cpus to be used for the" |
| 360 | " main thread"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | } |
| 362 | else |
| 363 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 364 | if (clib_bitmap_get (avail_cpu, tm->main_lcore) == 0) |
| 365 | return clib_error_return (0, "cpu %u is not available to be used" |
| 366 | " for the main thread", tm->main_lcore); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 368 | avail_cpu = clib_bitmap_set (avail_cpu, tm->main_lcore, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 369 | |
| 370 | /* assume that there is socket 0 only if there is no data from sysfs */ |
| 371 | if (!tm->cpu_socket_bitmap) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 372 | tm->cpu_socket_bitmap = clib_bitmap_set (0, 0, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 374 | /* pin main thread to main_lcore */ |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 375 | if (tm->cb.vlib_thread_set_lcore_cb) |
| 376 | { |
| 377 | tm->cb.vlib_thread_set_lcore_cb (0, tm->main_lcore); |
| 378 | } |
Damjan Marion | 858151f | 2018-07-11 10:51:00 +0200 | [diff] [blame] | 379 | else |
| 380 | { |
| 381 | cpu_set_t cpuset; |
| 382 | CPU_ZERO (&cpuset); |
| 383 | CPU_SET (tm->main_lcore, &cpuset); |
| 384 | pthread_setaffinity_np (pthread_self (), sizeof (cpu_set_t), &cpuset); |
| 385 | } |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 386 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | /* as many threads as stacks... */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 388 | vec_validate_aligned (vlib_worker_threads, vec_len (vlib_thread_stacks) - 1, |
| 389 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
| 391 | /* Preallocate thread 0 */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 392 | _vec_len (vlib_worker_threads) = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | w = vlib_worker_threads; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 394 | w->thread_mheap = clib_mem_get_heap (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | w->thread_stack = vlib_thread_stacks[0]; |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 396 | w->lcore_id = tm->main_lcore; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 397 | w->lwp = syscall (SYS_gettid); |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 398 | w->thread_id = pthread_self (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 399 | tm->n_vlib_mains = 1; |
| 400 | |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 401 | if (tm->sched_policy != ~0) |
| 402 | { |
| 403 | struct sched_param sched_param; |
| 404 | if (!sched_getparam (w->lwp, &sched_param)) |
| 405 | { |
| 406 | if (tm->sched_priority != ~0) |
| 407 | sched_param.sched_priority = tm->sched_priority; |
| 408 | sched_setscheduler (w->lwp, tm->sched_policy, &sched_param); |
| 409 | } |
| 410 | } |
| 411 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | /* assign threads to cores and set n_vlib_mains */ |
| 413 | tr = tm->next; |
| 414 | |
| 415 | while (tr) |
| 416 | { |
| 417 | vec_add1 (tm->registrations, tr); |
| 418 | tr = tr->next; |
| 419 | } |
| 420 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 421 | vec_sort_with_function (tm->registrations, sort_registrations_by_no_clone); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 422 | |
| 423 | for (i = 0; i < vec_len (tm->registrations); i++) |
| 424 | { |
| 425 | int j; |
| 426 | tr = tm->registrations[i]; |
| 427 | tr->first_index = first_index; |
| 428 | first_index += tr->count; |
| 429 | n_vlib_mains += (tr->no_data_structure_clone == 0) ? tr->count : 0; |
| 430 | |
| 431 | /* construct coremask */ |
| 432 | if (tr->use_pthreads || !tr->count) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 433 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | |
| 435 | if (tr->coremask) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 436 | { |
| 437 | uword c; |
| 438 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | clib_bitmap_foreach (c, tr->coremask, ({ |
| 440 | if (clib_bitmap_get(avail_cpu, c) == 0) |
| 441 | return clib_error_return (0, "cpu %u is not available to be used" |
| 442 | " for the '%s' thread",c, tr->name); |
| 443 | |
| 444 | avail_cpu = clib_bitmap_set(avail_cpu, c, 0); |
| 445 | })); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 446 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 447 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 448 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 450 | { |
| 451 | for (j = 0; j < tr->count; j++) |
| 452 | { |
| 453 | uword c = clib_bitmap_first_set (avail_cpu); |
| 454 | if (c == ~0) |
| 455 | return clib_error_return (0, |
| 456 | "no available cpus to be used for" |
| 457 | " the '%s' thread", tr->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 458 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 459 | avail_cpu = clib_bitmap_set (avail_cpu, c, 0); |
| 460 | tr->coremask = clib_bitmap_set (tr->coremask, c, 1); |
| 461 | } |
| 462 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 465 | clib_bitmap_free (avail_cpu); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 466 | |
| 467 | tm->n_vlib_mains = n_vlib_mains; |
| 468 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 469 | vec_validate_aligned (vlib_worker_threads, first_index - 1, |
| 470 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 475 | vlib_frame_queue_t * |
| 476 | vlib_frame_queue_alloc (int nelts) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 478 | vlib_frame_queue_t *fq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 480 | fq = clib_mem_alloc_aligned (sizeof (*fq), CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | memset (fq, 0, sizeof (*fq)); |
| 482 | fq->nelts = nelts; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 483 | fq->vector_threshold = 128; // packets |
| 484 | vec_validate_aligned (fq->elts, nelts - 1, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | |
| 486 | if (1) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 487 | { |
| 488 | if (((uword) & fq->tail) & (CLIB_CACHE_LINE_BYTES - 1)) |
| 489 | fformat (stderr, "WARNING: fq->tail unaligned\n"); |
| 490 | if (((uword) & fq->head) & (CLIB_CACHE_LINE_BYTES - 1)) |
| 491 | fformat (stderr, "WARNING: fq->head unaligned\n"); |
| 492 | if (((uword) fq->elts) & (CLIB_CACHE_LINE_BYTES - 1)) |
| 493 | fformat (stderr, "WARNING: fq->elts unaligned\n"); |
| 494 | |
| 495 | if (sizeof (fq->elts[0]) % CLIB_CACHE_LINE_BYTES) |
| 496 | fformat (stderr, "WARNING: fq->elts[0] size %d\n", |
| 497 | sizeof (fq->elts[0])); |
| 498 | if (nelts & (nelts - 1)) |
| 499 | { |
| 500 | fformat (stderr, "FATAL: nelts MUST be a power of 2\n"); |
| 501 | abort (); |
| 502 | } |
| 503 | } |
| 504 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | return (fq); |
| 506 | } |
| 507 | |
| 508 | void vl_msg_api_handler_no_free (void *) __attribute__ ((weak)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 509 | void |
| 510 | vl_msg_api_handler_no_free (void *v) |
| 511 | { |
| 512 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | |
| 514 | /* Turned off, save as reference material... */ |
| 515 | #if 0 |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 516 | static inline int |
| 517 | vlib_frame_queue_dequeue_internal (int thread_id, |
| 518 | vlib_main_t * vm, vlib_node_main_t * nm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | { |
| 520 | vlib_frame_queue_t *fq = vlib_frame_queues[thread_id]; |
| 521 | vlib_frame_queue_elt_t *elt; |
| 522 | vlib_frame_t *f; |
| 523 | vlib_pending_frame_t *p; |
| 524 | vlib_node_runtime_t *r; |
| 525 | u32 node_runtime_index; |
| 526 | int msg_type; |
| 527 | u64 before; |
| 528 | int processed = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 529 | |
| 530 | ASSERT (vm == vlib_mains[thread_id]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 531 | |
| 532 | while (1) |
| 533 | { |
| 534 | if (fq->head == fq->tail) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 535 | return processed; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 536 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 537 | elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 538 | |
| 539 | if (!elt->valid) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 540 | return processed; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 542 | before = clib_cpu_time_now (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 543 | |
| 544 | f = elt->frame; |
| 545 | node_runtime_index = elt->node_runtime_index; |
| 546 | msg_type = elt->msg_type; |
| 547 | |
| 548 | switch (msg_type) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 549 | { |
| 550 | case VLIB_FRAME_QUEUE_ELT_FREE_BUFFERS: |
| 551 | vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors); |
| 552 | /* note fallthrough... */ |
| 553 | case VLIB_FRAME_QUEUE_ELT_FREE_FRAME: |
| 554 | r = vec_elt_at_index (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL], |
| 555 | node_runtime_index); |
| 556 | vlib_frame_free (vm, r, f); |
| 557 | break; |
| 558 | case VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME: |
| 559 | vec_add2 (vm->node_main.pending_frames, p, 1); |
| 560 | f->flags |= (VLIB_FRAME_PENDING | VLIB_FRAME_FREE_AFTER_DISPATCH); |
| 561 | p->node_runtime_index = elt->node_runtime_index; |
| 562 | p->frame_index = vlib_frame_index (vm, f); |
| 563 | p->next_frame_index = VLIB_PENDING_FRAME_NO_NEXT_FRAME; |
| 564 | fq->dequeue_vectors += (u64) f->n_vectors; |
| 565 | break; |
| 566 | case VLIB_FRAME_QUEUE_ELT_API_MSG: |
| 567 | vl_msg_api_handler_no_free (f); |
| 568 | break; |
| 569 | default: |
| 570 | clib_warning ("bogus frame queue message, type %d", msg_type); |
| 571 | break; |
| 572 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | elt->valid = 0; |
| 574 | fq->dequeues++; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 575 | fq->dequeue_ticks += clib_cpu_time_now () - before; |
| 576 | CLIB_MEMORY_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 577 | fq->head++; |
| 578 | processed++; |
| 579 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 580 | ASSERT (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 581 | return processed; |
| 582 | } |
| 583 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 584 | int |
| 585 | vlib_frame_queue_dequeue (int thread_id, |
| 586 | vlib_main_t * vm, vlib_node_main_t * nm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 587 | { |
| 588 | return vlib_frame_queue_dequeue_internal (thread_id, vm, nm); |
| 589 | } |
| 590 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 591 | int |
| 592 | vlib_frame_queue_enqueue (vlib_main_t * vm, u32 node_runtime_index, |
| 593 | u32 frame_queue_index, vlib_frame_t * frame, |
| 594 | vlib_frame_queue_msg_type_t type) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 595 | { |
| 596 | vlib_frame_queue_t *fq = vlib_frame_queues[frame_queue_index]; |
| 597 | vlib_frame_queue_elt_t *elt; |
| 598 | u32 save_count; |
| 599 | u64 new_tail; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 600 | u64 before = clib_cpu_time_now (); |
| 601 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | ASSERT (fq); |
| 603 | |
| 604 | new_tail = __sync_add_and_fetch (&fq->tail, 1); |
| 605 | |
| 606 | /* Wait until a ring slot is available */ |
| 607 | while (new_tail >= fq->head + fq->nelts) |
| 608 | { |
| 609 | f64 b4 = vlib_time_now_ticks (vm, before); |
| 610 | vlib_worker_thread_barrier_check (vm, b4); |
| 611 | /* Bad idea. Dequeue -> enqueue -> dequeue -> trouble */ |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 612 | // vlib_frame_queue_dequeue (vm->thread_index, vm, nm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 615 | elt = fq->elts + (new_tail & (fq->nelts - 1)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 616 | |
| 617 | /* this would be very bad... */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 618 | while (elt->valid) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 619 | { |
| 620 | } |
| 621 | |
| 622 | /* Once we enqueue the frame, frame->n_vectors is owned elsewhere... */ |
| 623 | save_count = frame->n_vectors; |
| 624 | |
| 625 | elt->frame = frame; |
| 626 | elt->node_runtime_index = node_runtime_index; |
| 627 | elt->msg_type = type; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 628 | CLIB_MEMORY_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 629 | elt->valid = 1; |
| 630 | |
| 631 | return save_count; |
| 632 | } |
| 633 | #endif /* 0 */ |
| 634 | |
| 635 | /* To be called by vlib worker threads upon startup */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 636 | void |
| 637 | vlib_worker_thread_init (vlib_worker_thread_t * w) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 639 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 640 | |
Dave Barach | fdf4944 | 2016-12-20 12:48:14 -0500 | [diff] [blame] | 641 | /* |
| 642 | * Note: disabling signals in worker threads as follows |
| 643 | * prevents the api post-mortem dump scheme from working |
| 644 | * { |
| 645 | * sigset_t s; |
| 646 | * sigfillset (&s); |
| 647 | * pthread_sigmask (SIG_SETMASK, &s, 0); |
| 648 | * } |
| 649 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | |
| 651 | clib_mem_set_heap (w->thread_mheap); |
| 652 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 653 | if (vec_len (tm->thread_prefix) && w->registration->short_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 654 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 655 | w->name = format (0, "%v_%s_%d%c", tm->thread_prefix, |
| 656 | w->registration->short_name, w->instance_id, '\0'); |
| 657 | vlib_set_thread_name ((char *) w->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | if (!w->registration->use_pthreads) |
| 661 | { |
| 662 | |
| 663 | /* Initial barrier sync, for both worker and i/o threads */ |
| 664 | clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, 1); |
| 665 | |
| 666 | while (*vlib_worker_threads->wait_at_barrier) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 667 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 668 | |
| 669 | clib_smp_atomic_add (vlib_worker_threads->workers_at_barrier, -1); |
| 670 | } |
| 671 | } |
| 672 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 673 | void * |
| 674 | vlib_worker_thread_bootstrap_fn (void *arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 675 | { |
| 676 | void *rv; |
| 677 | vlib_worker_thread_t *w = arg; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 678 | |
| 679 | w->lwp = syscall (SYS_gettid); |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 680 | w->thread_id = pthread_self (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 681 | |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 682 | __os_thread_index = w - vlib_worker_threads; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 683 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 684 | rv = (void *) clib_calljmp |
| 685 | ((uword (*)(uword)) w->thread_function, |
| 686 | (uword) arg, w->thread_stack + VLIB_THREAD_STACK_SIZE); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 687 | /* NOTREACHED, we hope */ |
| 688 | return rv; |
| 689 | } |
| 690 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 691 | static clib_error_t * |
| 692 | vlib_launch_thread_int (void *fp, vlib_worker_thread_t * w, unsigned lcore_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 693 | { |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 694 | vlib_thread_main_t *tm = &vlib_thread_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 695 | void *(*fp_arg) (void *) = fp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 696 | |
Pavel Kotucek | 9876520 | 2016-10-07 08:37:28 +0200 | [diff] [blame] | 697 | w->lcore_id = lcore_id; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 698 | if (tm->cb.vlib_launch_thread_cb && !w->registration->use_pthreads) |
| 699 | return tm->cb.vlib_launch_thread_cb (fp, (void *) w, lcore_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 700 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 701 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 702 | pthread_t worker; |
| 703 | cpu_set_t cpuset; |
| 704 | CPU_ZERO (&cpuset); |
| 705 | CPU_SET (lcore_id, &cpuset); |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 706 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 707 | if (pthread_create (&worker, NULL /* attr */ , fp_arg, (void *) w)) |
| 708 | return clib_error_return_unix (0, "pthread_create"); |
| 709 | |
| 710 | if (pthread_setaffinity_np (worker, sizeof (cpu_set_t), &cpuset)) |
| 711 | return clib_error_return_unix (0, "pthread_setaffinity_np"); |
| 712 | |
| 713 | return 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 714 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 717 | static clib_error_t * |
| 718 | start_workers (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 719 | { |
| 720 | int i, j; |
| 721 | vlib_worker_thread_t *w; |
| 722 | vlib_main_t *vm_clone; |
| 723 | void *oldheap; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 724 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 725 | vlib_thread_registration_t *tr; |
| 726 | vlib_node_runtime_t *rt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 727 | u32 n_vlib_mains = tm->n_vlib_mains; |
| 728 | u32 worker_thread_index; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 729 | u8 *main_heap = clib_mem_get_per_cpu_heap (); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 730 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 731 | vec_reset_length (vlib_worker_threads); |
| 732 | |
| 733 | /* Set up the main thread */ |
| 734 | vec_add2_aligned (vlib_worker_threads, w, 1, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 3e7deb1 | 2016-02-05 16:29:53 -0500 | [diff] [blame] | 735 | w->elog_track.name = "main thread"; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 736 | elog_track_register (&vm->elog_main, &w->elog_track); |
| 737 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 738 | if (vec_len (tm->thread_prefix)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 739 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 740 | w->name = format (0, "%v_main%c", tm->thread_prefix, '\0'); |
| 741 | vlib_set_thread_name ((char *) w->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 744 | vm->elog_main.lock = |
| 745 | clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 848191d | 2016-04-28 16:24:15 -0400 | [diff] [blame] | 746 | vm->elog_main.lock[0] = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 747 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 748 | if (n_vlib_mains > 1) |
| 749 | { |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 750 | /* Replace hand-crafted length-1 vector with a real vector */ |
| 751 | vlib_mains = 0; |
| 752 | |
| 753 | vec_validate_aligned (vlib_mains, tm->n_vlib_mains - 1, |
| 754 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | _vec_len (vlib_mains) = 0; |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 756 | vec_add1_aligned (vlib_mains, vm, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 757 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 758 | vlib_worker_threads->wait_at_barrier = |
| 759 | clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 760 | vlib_worker_threads->workers_at_barrier = |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 761 | clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 763 | vlib_worker_threads->node_reforks_required = |
| 764 | clib_mem_alloc_aligned (sizeof (u32), CLIB_CACHE_LINE_BYTES); |
| 765 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 766 | /* Ask for an initial barrier sync */ |
| 767 | *vlib_worker_threads->workers_at_barrier = 0; |
| 768 | *vlib_worker_threads->wait_at_barrier = 1; |
| 769 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 770 | /* Without update or refork */ |
| 771 | *vlib_worker_threads->node_reforks_required = 0; |
| 772 | vm->need_vlib_worker_thread_node_runtime_update = 0; |
| 773 | |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 774 | /* init timing */ |
| 775 | vm->barrier_epoch = 0; |
| 776 | vm->barrier_no_close_before = 0; |
| 777 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 778 | worker_thread_index = 1; |
| 779 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 780 | for (i = 0; i < vec_len (tm->registrations); i++) |
| 781 | { |
| 782 | vlib_node_main_t *nm, *nm_clone; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 783 | vlib_buffer_free_list_t *fl_clone, *fl_orig; |
| 784 | vlib_buffer_free_list_t *orig_freelist_pool; |
| 785 | int k; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 786 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 787 | tr = tm->registrations[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 788 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 789 | if (tr->count == 0) |
| 790 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 791 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 792 | for (k = 0; k < tr->count; k++) |
| 793 | { |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 794 | vlib_node_t *n; |
| 795 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 796 | vec_add2 (vlib_worker_threads, w, 1); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 797 | /* Currently unused, may not really work */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 798 | if (tr->mheap_size) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 799 | { |
| 800 | #if USE_DLMALLOC == 0 |
| 801 | w->thread_mheap = |
| 802 | mheap_alloc (0 /* use VM */ , tr->mheap_size); |
| 803 | #else |
| 804 | w->thread_mheap = create_mspace (tr->mheap_size, |
| 805 | 0 /* unlocked */ ); |
| 806 | #endif |
| 807 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 808 | else |
| 809 | w->thread_mheap = main_heap; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 810 | |
| 811 | w->thread_stack = |
| 812 | vlib_thread_stack_init (w - vlib_worker_threads); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 813 | w->thread_function = tr->function; |
| 814 | w->thread_function_arg = w; |
| 815 | w->instance_id = k; |
| 816 | w->registration = tr; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 817 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 818 | w->elog_track.name = |
| 819 | (char *) format (0, "%s %d", tr->name, k + 1); |
| 820 | vec_add1 (w->elog_track.name, 0); |
| 821 | elog_track_register (&vm->elog_main, &w->elog_track); |
Bud Grise | 68adab9 | 2016-02-12 10:36:11 -0500 | [diff] [blame] | 822 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 823 | if (tr->no_data_structure_clone) |
| 824 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 825 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 826 | /* Fork vlib_global_main et al. Look for bugs here */ |
| 827 | oldheap = clib_mem_set_heap (w->thread_mheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 829 | vm_clone = clib_mem_alloc_aligned (sizeof (*vm_clone), |
| 830 | CLIB_CACHE_LINE_BYTES); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 831 | clib_memcpy (vm_clone, vlib_mains[0], sizeof (*vm_clone)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 832 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 833 | vm_clone->thread_index = worker_thread_index; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 834 | vm_clone->heap_base = w->thread_mheap; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 835 | vm_clone->heap_aligned_base = (void *) |
| 836 | (((uword) w->thread_mheap) & ~(VLIB_FRAME_ALIGN - 1)); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 837 | vm_clone->init_functions_called = |
| 838 | hash_create (0, /* value bytes */ 0); |
Dave Barach | 2877eee | 2017-12-15 12:22:57 -0500 | [diff] [blame] | 839 | vm_clone->pending_rpc_requests = 0; |
| 840 | vec_validate (vm_clone->pending_rpc_requests, 0); |
| 841 | _vec_len (vm_clone->pending_rpc_requests) = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 842 | memset (&vm_clone->random_buffer, 0, |
| 843 | sizeof (vm_clone->random_buffer)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 844 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 845 | nm = &vlib_mains[0]->node_main; |
| 846 | nm_clone = &vm_clone->node_main; |
| 847 | /* fork next frames array, preserving node runtime indices */ |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 848 | nm_clone->next_frames = vec_dup_aligned (nm->next_frames, |
| 849 | CLIB_CACHE_LINE_BYTES); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 850 | for (j = 0; j < vec_len (nm_clone->next_frames); j++) |
| 851 | { |
| 852 | vlib_next_frame_t *nf = &nm_clone->next_frames[j]; |
| 853 | u32 save_node_runtime_index; |
| 854 | u32 save_flags; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 855 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 856 | save_node_runtime_index = nf->node_runtime_index; |
| 857 | save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH; |
| 858 | vlib_next_frame_init (nf); |
| 859 | nf->node_runtime_index = save_node_runtime_index; |
| 860 | nf->flags = save_flags; |
| 861 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 862 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 863 | /* fork the frame dispatch queue */ |
| 864 | nm_clone->pending_frames = 0; |
| 865 | vec_validate (nm_clone->pending_frames, 10); /* $$$$$?????? */ |
| 866 | _vec_len (nm_clone->pending_frames) = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 867 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 868 | /* fork nodes */ |
| 869 | nm_clone->nodes = 0; |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 870 | |
| 871 | /* Allocate all nodes in single block for speed */ |
| 872 | n = clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*n)); |
| 873 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 874 | for (j = 0; j < vec_len (nm->nodes); j++) |
| 875 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 876 | clib_memcpy (n, nm->nodes[j], sizeof (*n)); |
| 877 | /* none of the copied nodes have enqueue rights given out */ |
| 878 | n->owner_node_index = VLIB_INVALID_NODE_INDEX; |
| 879 | memset (&n->stats_total, 0, sizeof (n->stats_total)); |
| 880 | memset (&n->stats_last_clear, 0, |
| 881 | sizeof (n->stats_last_clear)); |
| 882 | vec_add1 (nm_clone->nodes, n); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 883 | n++; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 884 | } |
| 885 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] = |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 886 | vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL], |
| 887 | CLIB_CACHE_LINE_BYTES); |
JingLiuZTE | 30af5da | 2017-07-24 10:53:31 +0800 | [diff] [blame] | 888 | vec_foreach (rt, |
| 889 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 890 | { |
| 891 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 892 | rt->thread_index = vm_clone->thread_index; |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 893 | /* copy initial runtime_data from node */ |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 894 | if (n->runtime_data && n->runtime_data_bytes > 0) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 895 | clib_memcpy (rt->runtime_data, n->runtime_data, |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 896 | clib_min (VLIB_NODE_RUNTIME_DATA_SIZE, |
| 897 | n->runtime_data_bytes)); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 898 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 899 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 900 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] = |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 901 | vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT], |
| 902 | CLIB_CACHE_LINE_BYTES); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 903 | vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 904 | { |
| 905 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 906 | rt->thread_index = vm_clone->thread_index; |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 907 | /* copy initial runtime_data from node */ |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 908 | if (n->runtime_data && n->runtime_data_bytes > 0) |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 909 | clib_memcpy (rt->runtime_data, n->runtime_data, |
Damjan Marion | b6f93a1 | 2017-03-16 17:46:41 +0100 | [diff] [blame] | 910 | clib_min (VLIB_NODE_RUNTIME_DATA_SIZE, |
| 911 | n->runtime_data_bytes)); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 912 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 913 | |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 914 | nm_clone->processes = vec_dup_aligned (nm->processes, |
| 915 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 916 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 917 | /* zap the (per worker) frame freelists, etc */ |
| 918 | nm_clone->frame_sizes = 0; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 919 | nm_clone->frame_size_hash = hash_create (0, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 920 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 921 | /* Packet trace buffers are guaranteed to be empty, nothing to do here */ |
Damjan Marion | bc20bdf | 2015-12-17 14:28:18 +0100 | [diff] [blame] | 922 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 923 | clib_mem_set_heap (oldheap); |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 924 | vec_add1_aligned (vlib_mains, vm_clone, CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 925 | |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 926 | vm_clone->error_main.counters = vec_dup_aligned |
| 927 | (vlib_mains[0]->error_main.counters, CLIB_CACHE_LINE_BYTES); |
| 928 | vm_clone->error_main.counters_last_clear = vec_dup_aligned |
| 929 | (vlib_mains[0]->error_main.counters_last_clear, |
| 930 | CLIB_CACHE_LINE_BYTES); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 931 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 932 | /* Fork the vlib_buffer_main_t free lists, etc. */ |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 933 | orig_freelist_pool = vm_clone->buffer_free_list_pool; |
| 934 | vm_clone->buffer_free_list_pool = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 935 | |
| 936 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 937 | pool_foreach (fl_orig, orig_freelist_pool, |
| 938 | ({ |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 939 | pool_get_aligned (vm_clone->buffer_free_list_pool, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 940 | fl_clone, CLIB_CACHE_LINE_BYTES); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 941 | ASSERT (fl_orig - orig_freelist_pool |
Damjan Marion | d1274cb | 2018-03-13 21:32:17 +0100 | [diff] [blame] | 942 | == fl_clone - vm_clone->buffer_free_list_pool); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 943 | |
| 944 | fl_clone[0] = fl_orig[0]; |
Damjan Marion | bd69a5f | 2017-02-05 23:44:42 +0100 | [diff] [blame] | 945 | fl_clone->buffers = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 946 | fl_clone->n_alloc = 0; |
| 947 | })); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 948 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 949 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 950 | worker_thread_index++; |
| 951 | } |
| 952 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 953 | } |
| 954 | else |
| 955 | { |
| 956 | /* only have non-data-structure copy threads to create... */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 957 | for (i = 0; i < vec_len (tm->registrations); i++) |
| 958 | { |
| 959 | tr = tm->registrations[i]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 960 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 961 | for (j = 0; j < tr->count; j++) |
| 962 | { |
| 963 | vec_add2 (vlib_worker_threads, w, 1); |
| 964 | if (tr->mheap_size) |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 965 | { |
| 966 | #if USE_DLMALLOC == 0 |
| 967 | w->thread_mheap = |
| 968 | mheap_alloc (0 /* use VM */ , tr->mheap_size); |
| 969 | #else |
| 970 | w->thread_mheap = |
| 971 | create_mspace (tr->mheap_size, 0 /* locked */ ); |
| 972 | #endif |
| 973 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 974 | else |
| 975 | w->thread_mheap = main_heap; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 976 | w->thread_stack = |
| 977 | vlib_thread_stack_init (w - vlib_worker_threads); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 978 | w->thread_function = tr->function; |
| 979 | w->thread_function_arg = w; |
| 980 | w->instance_id = j; |
| 981 | w->elog_track.name = |
| 982 | (char *) format (0, "%s %d", tr->name, j + 1); |
| 983 | w->registration = tr; |
| 984 | vec_add1 (w->elog_track.name, 0); |
| 985 | elog_track_register (&vm->elog_main, &w->elog_track); |
| 986 | } |
| 987 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | worker_thread_index = 1; |
| 991 | |
| 992 | for (i = 0; i < vec_len (tm->registrations); i++) |
| 993 | { |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 994 | clib_error_t *err; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 995 | int j; |
| 996 | |
| 997 | tr = tm->registrations[i]; |
| 998 | |
| 999 | if (tr->use_pthreads || tm->use_pthreads) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1000 | { |
| 1001 | for (j = 0; j < tr->count; j++) |
| 1002 | { |
| 1003 | w = vlib_worker_threads + worker_thread_index++; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1004 | err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn, |
| 1005 | w, 0); |
| 1006 | if (err) |
| 1007 | clib_error_report (err); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1008 | } |
| 1009 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1010 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1011 | { |
| 1012 | uword c; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1013 | /* *INDENT-OFF* */ |
| 1014 | clib_bitmap_foreach (c, tr->coremask, ({ |
| 1015 | w = vlib_worker_threads + worker_thread_index++; |
| 1016 | err = vlib_launch_thread_int (vlib_worker_thread_bootstrap_fn, |
| 1017 | w, c); |
| 1018 | if (err) |
| 1019 | clib_error_report (err); |
| 1020 | })); |
| 1021 | /* *INDENT-ON* */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1022 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1023 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1024 | vlib_worker_thread_barrier_sync (vm); |
| 1025 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | VLIB_MAIN_LOOP_ENTER_FUNCTION (start_workers); |
| 1030 | |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1031 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1032 | static inline void |
| 1033 | worker_thread_node_runtime_update_internal (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1034 | { |
| 1035 | int i, j; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1036 | vlib_main_t *vm; |
| 1037 | vlib_node_main_t *nm, *nm_clone; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1038 | vlib_main_t *vm_clone; |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1039 | vlib_node_runtime_t *rt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1040 | never_inline void |
| 1041 | vlib_node_runtime_sync_stats (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1042 | vlib_node_runtime_t * r, |
| 1043 | uword n_calls, |
| 1044 | uword n_vectors, uword n_clocks); |
| 1045 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1046 | ASSERT (vlib_get_thread_index () == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1047 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1048 | vm = vlib_mains[0]; |
| 1049 | nm = &vm->node_main; |
| 1050 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1051 | ASSERT (*vlib_worker_threads->wait_at_barrier == 1); |
| 1052 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1053 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1054 | * Scrape all runtime stats, so we don't lose node runtime(s) with |
| 1055 | * pending counts, or throw away worker / io thread counts. |
| 1056 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1057 | for (j = 0; j < vec_len (nm->nodes); j++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1058 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1059 | vlib_node_t *n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1060 | n = nm->nodes[j]; |
| 1061 | vlib_node_sync_stats (vm, n); |
| 1062 | } |
| 1063 | |
| 1064 | for (i = 1; i < vec_len (vlib_mains); i++) |
| 1065 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1066 | vlib_node_t *n; |
| 1067 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1068 | vm_clone = vlib_mains[i]; |
| 1069 | nm_clone = &vm_clone->node_main; |
| 1070 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1071 | for (j = 0; j < vec_len (nm_clone->nodes); j++) |
| 1072 | { |
| 1073 | n = nm_clone->nodes[j]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1074 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1075 | rt = vlib_node_get_runtime (vm_clone, n->index); |
| 1076 | vlib_node_runtime_sync_stats (vm_clone, rt, 0, 0, 0); |
| 1077 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1080 | /* Per-worker clone rebuilds are now done on each thread */ |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | void |
| 1085 | vlib_worker_thread_node_refork (void) |
| 1086 | { |
| 1087 | vlib_main_t *vm, *vm_clone; |
| 1088 | vlib_node_main_t *nm, *nm_clone; |
| 1089 | vlib_node_t **old_nodes_clone; |
| 1090 | vlib_node_runtime_t *rt, *old_rt; |
| 1091 | |
| 1092 | vlib_node_t *new_n_clone; |
| 1093 | |
| 1094 | int j; |
| 1095 | |
| 1096 | vm = vlib_mains[0]; |
| 1097 | nm = &vm->node_main; |
| 1098 | vm_clone = vlib_get_main (); |
| 1099 | nm_clone = &vm_clone->node_main; |
| 1100 | |
| 1101 | /* Re-clone error heap */ |
| 1102 | u64 *old_counters = vm_clone->error_main.counters; |
| 1103 | u64 *old_counters_all_clear = vm_clone->error_main.counters_last_clear; |
| 1104 | |
| 1105 | clib_memcpy (&vm_clone->error_main, &vm->error_main, |
| 1106 | sizeof (vm->error_main)); |
| 1107 | j = vec_len (vm->error_main.counters) - 1; |
| 1108 | vec_validate_aligned (old_counters, j, CLIB_CACHE_LINE_BYTES); |
| 1109 | vec_validate_aligned (old_counters_all_clear, j, CLIB_CACHE_LINE_BYTES); |
| 1110 | vm_clone->error_main.counters = old_counters; |
| 1111 | vm_clone->error_main.counters_last_clear = old_counters_all_clear; |
| 1112 | |
| 1113 | nm_clone = &vm_clone->node_main; |
| 1114 | vec_free (nm_clone->next_frames); |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 1115 | nm_clone->next_frames = vec_dup_aligned (nm->next_frames, |
| 1116 | CLIB_CACHE_LINE_BYTES); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1117 | |
| 1118 | for (j = 0; j < vec_len (nm_clone->next_frames); j++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1119 | { |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1120 | vlib_next_frame_t *nf = &nm_clone->next_frames[j]; |
| 1121 | u32 save_node_runtime_index; |
| 1122 | u32 save_flags; |
Damjan Marion | bc20bdf | 2015-12-17 14:28:18 +0100 | [diff] [blame] | 1123 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1124 | save_node_runtime_index = nf->node_runtime_index; |
| 1125 | save_flags = nf->flags & VLIB_FRAME_NO_FREE_AFTER_DISPATCH; |
| 1126 | vlib_next_frame_init (nf); |
| 1127 | nf->node_runtime_index = save_node_runtime_index; |
| 1128 | nf->flags = save_flags; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1129 | } |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1130 | |
| 1131 | old_nodes_clone = nm_clone->nodes; |
| 1132 | nm_clone->nodes = 0; |
| 1133 | |
| 1134 | /* re-fork nodes */ |
| 1135 | |
| 1136 | /* Allocate all nodes in single block for speed */ |
| 1137 | new_n_clone = |
| 1138 | clib_mem_alloc_no_fail (vec_len (nm->nodes) * sizeof (*new_n_clone)); |
| 1139 | for (j = 0; j < vec_len (nm->nodes); j++) |
| 1140 | { |
| 1141 | vlib_node_t *old_n_clone; |
| 1142 | vlib_node_t *new_n; |
| 1143 | |
| 1144 | new_n = nm->nodes[j]; |
| 1145 | old_n_clone = old_nodes_clone[j]; |
| 1146 | |
| 1147 | clib_memcpy (new_n_clone, new_n, sizeof (*new_n)); |
| 1148 | /* none of the copied nodes have enqueue rights given out */ |
| 1149 | new_n_clone->owner_node_index = VLIB_INVALID_NODE_INDEX; |
| 1150 | |
| 1151 | if (j >= vec_len (old_nodes_clone)) |
| 1152 | { |
| 1153 | /* new node, set to zero */ |
| 1154 | memset (&new_n_clone->stats_total, 0, |
| 1155 | sizeof (new_n_clone->stats_total)); |
| 1156 | memset (&new_n_clone->stats_last_clear, 0, |
| 1157 | sizeof (new_n_clone->stats_last_clear)); |
| 1158 | } |
| 1159 | else |
| 1160 | { |
| 1161 | /* Copy stats if the old data is valid */ |
| 1162 | clib_memcpy (&new_n_clone->stats_total, |
| 1163 | &old_n_clone->stats_total, |
| 1164 | sizeof (new_n_clone->stats_total)); |
| 1165 | clib_memcpy (&new_n_clone->stats_last_clear, |
| 1166 | &old_n_clone->stats_last_clear, |
| 1167 | sizeof (new_n_clone->stats_last_clear)); |
| 1168 | |
| 1169 | /* keep previous node state */ |
| 1170 | new_n_clone->state = old_n_clone->state; |
| 1171 | } |
| 1172 | vec_add1 (nm_clone->nodes, new_n_clone); |
| 1173 | new_n_clone++; |
| 1174 | } |
| 1175 | /* Free the old node clones */ |
| 1176 | clib_mem_free (old_nodes_clone[0]); |
| 1177 | |
| 1178 | vec_free (old_nodes_clone); |
| 1179 | |
| 1180 | |
| 1181 | /* re-clone internal nodes */ |
| 1182 | old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]; |
| 1183 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL] = |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 1184 | vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL], |
| 1185 | CLIB_CACHE_LINE_BYTES); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1186 | |
| 1187 | vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) |
| 1188 | { |
| 1189 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
| 1190 | rt->thread_index = vm_clone->thread_index; |
| 1191 | /* copy runtime_data, will be overwritten later for existing rt */ |
| 1192 | if (n->runtime_data && n->runtime_data_bytes > 0) |
| 1193 | clib_memcpy (rt->runtime_data, n->runtime_data, |
| 1194 | clib_min (VLIB_NODE_RUNTIME_DATA_SIZE, |
| 1195 | n->runtime_data_bytes)); |
| 1196 | } |
| 1197 | |
| 1198 | for (j = 0; j < vec_len (old_rt); j++) |
| 1199 | { |
| 1200 | rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index); |
| 1201 | rt->state = old_rt[j].state; |
| 1202 | clib_memcpy (rt->runtime_data, old_rt[j].runtime_data, |
| 1203 | VLIB_NODE_RUNTIME_DATA_SIZE); |
| 1204 | } |
| 1205 | |
| 1206 | vec_free (old_rt); |
| 1207 | |
| 1208 | /* re-clone input nodes */ |
| 1209 | old_rt = nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]; |
| 1210 | nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT] = |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 1211 | vec_dup_aligned (nm->nodes_by_type[VLIB_NODE_TYPE_INPUT], |
| 1212 | CLIB_CACHE_LINE_BYTES); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1213 | |
| 1214 | vec_foreach (rt, nm_clone->nodes_by_type[VLIB_NODE_TYPE_INPUT]) |
| 1215 | { |
| 1216 | vlib_node_t *n = vlib_get_node (vm, rt->node_index); |
| 1217 | rt->thread_index = vm_clone->thread_index; |
| 1218 | /* copy runtime_data, will be overwritten later for existing rt */ |
| 1219 | if (n->runtime_data && n->runtime_data_bytes > 0) |
| 1220 | clib_memcpy (rt->runtime_data, n->runtime_data, |
| 1221 | clib_min (VLIB_NODE_RUNTIME_DATA_SIZE, |
| 1222 | n->runtime_data_bytes)); |
| 1223 | } |
| 1224 | |
| 1225 | for (j = 0; j < vec_len (old_rt); j++) |
| 1226 | { |
| 1227 | rt = vlib_node_get_runtime (vm_clone, old_rt[j].node_index); |
| 1228 | rt->state = old_rt[j].state; |
| 1229 | clib_memcpy (rt->runtime_data, old_rt[j].runtime_data, |
| 1230 | VLIB_NODE_RUNTIME_DATA_SIZE); |
| 1231 | } |
| 1232 | |
| 1233 | vec_free (old_rt); |
| 1234 | |
Damjan Marion | be3f4d5 | 2018-03-27 21:06:10 +0200 | [diff] [blame] | 1235 | nm_clone->processes = vec_dup_aligned (nm->processes, |
| 1236 | CLIB_CACHE_LINE_BYTES); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1237 | } |
| 1238 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1239 | void |
| 1240 | vlib_worker_thread_node_runtime_update (void) |
| 1241 | { |
| 1242 | /* |
| 1243 | * Make a note that we need to do a node runtime update |
| 1244 | * prior to releasing the barrier. |
| 1245 | */ |
| 1246 | vlib_global_main.need_vlib_worker_thread_node_runtime_update = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1249 | u32 |
| 1250 | unformat_sched_policy (unformat_input_t * input, va_list * args) |
| 1251 | { |
| 1252 | u32 *r = va_arg (*args, u32 *); |
| 1253 | |
| 1254 | if (0); |
| 1255 | #define _(v,f,s) else if (unformat (input, s)) *r = SCHED_POLICY_##f; |
| 1256 | foreach_sched_policy |
| 1257 | #undef _ |
| 1258 | else |
| 1259 | return 0; |
| 1260 | return 1; |
| 1261 | } |
| 1262 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1263 | static clib_error_t * |
| 1264 | cpu_config (vlib_main_t * vm, unformat_input_t * input) |
| 1265 | { |
| 1266 | vlib_thread_registration_t *tr; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1267 | uword *p; |
| 1268 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 1269 | u8 *name; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1270 | uword *bitmap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1271 | u32 count; |
| 1272 | |
| 1273 | tm->thread_registrations_by_name = hash_create_string (0, sizeof (uword)); |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1274 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1275 | tm->n_thread_stacks = 1; /* account for main thread */ |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1276 | tm->sched_policy = ~0; |
| 1277 | tm->sched_priority = ~0; |
Damjan Marion | 858151f | 2018-07-11 10:51:00 +0200 | [diff] [blame] | 1278 | tm->main_lcore = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1279 | |
| 1280 | tr = tm->next; |
| 1281 | |
| 1282 | while (tr) |
| 1283 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1284 | hash_set_mem (tm->thread_registrations_by_name, tr->name, (uword) tr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1285 | tr = tr->next; |
| 1286 | } |
| 1287 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1288 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1289 | { |
Damjan Marion | bf74147 | 2016-06-13 22:49:44 +0200 | [diff] [blame] | 1290 | if (unformat (input, "use-pthreads")) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1291 | tm->use_pthreads = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1292 | else if (unformat (input, "thread-prefix %v", &tm->thread_prefix)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1293 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1294 | else if (unformat (input, "main-core %u", &tm->main_lcore)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1295 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1296 | else if (unformat (input, "skip-cores %u", &tm->skip_cores)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1297 | ; |
Yi He | e4a9eb7 | 2018-07-17 14:18:41 +0800 | [diff] [blame] | 1298 | else if (unformat (input, "coremask-%s %U", &name, |
| 1299 | unformat_bitmap_mask, &bitmap) || |
| 1300 | unformat (input, "corelist-%s %U", &name, |
| 1301 | unformat_bitmap_list, &bitmap)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1302 | { |
| 1303 | p = hash_get_mem (tm->thread_registrations_by_name, name); |
| 1304 | if (p == 0) |
| 1305 | return clib_error_return (0, "no such thread type '%s'", name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1306 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1307 | tr = (vlib_thread_registration_t *) p[0]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1308 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1309 | if (tr->use_pthreads) |
| 1310 | return clib_error_return (0, |
| 1311 | "corelist cannot be set for '%s' threads", |
| 1312 | name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1313 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1314 | tr->coremask = bitmap; |
| 1315 | tr->count = clib_bitmap_count_set_bits (tr->coremask); |
| 1316 | } |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1317 | else |
| 1318 | if (unformat |
| 1319 | (input, "scheduler-policy %U", unformat_sched_policy, |
| 1320 | &tm->sched_policy)) |
| 1321 | ; |
Pavel Kotucek | c08a1ed | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1322 | else if (unformat (input, "scheduler-priority %u", &tm->sched_priority)) |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1323 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1324 | else if (unformat (input, "%s %u", &name, &count)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1325 | { |
| 1326 | p = hash_get_mem (tm->thread_registrations_by_name, name); |
| 1327 | if (p == 0) |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1328 | return clib_error_return (0, "no such thread type 3 '%s'", name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1329 | |
| 1330 | tr = (vlib_thread_registration_t *) p[0]; |
| 1331 | if (tr->fixed_count) |
| 1332 | return clib_error_return |
| 1333 | (0, "number of %s threads not configurable", tr->name); |
| 1334 | tr->count = count; |
| 1335 | } |
| 1336 | else |
| 1337 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Pavel Kotucek | c08a1ed | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1340 | if (tm->sched_priority != ~0) |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1341 | { |
Pavel Kotucek | c08a1ed | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1342 | if (tm->sched_policy == SCHED_FIFO || tm->sched_policy == SCHED_RR) |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1343 | { |
| 1344 | u32 prio_max = sched_get_priority_max (tm->sched_policy); |
| 1345 | u32 prio_min = sched_get_priority_min (tm->sched_policy); |
| 1346 | if (tm->sched_priority > prio_max) |
| 1347 | tm->sched_priority = prio_max; |
| 1348 | if (tm->sched_priority < prio_min) |
| 1349 | tm->sched_priority = prio_min; |
| 1350 | } |
| 1351 | else |
Pavel Kotucek | c08a1ed | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1352 | { |
| 1353 | return clib_error_return |
| 1354 | (0, |
| 1355 | "scheduling priority (%d) is not allowed for `normal` scheduling policy", |
| 1356 | tm->sched_priority); |
| 1357 | } |
Pavel Kotucek | 1e76583 | 2016-09-23 08:54:14 +0200 | [diff] [blame] | 1358 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1359 | tr = tm->next; |
| 1360 | |
| 1361 | if (!tm->thread_prefix) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1362 | tm->thread_prefix = format (0, "vpp"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1363 | |
| 1364 | while (tr) |
| 1365 | { |
| 1366 | tm->n_thread_stacks += tr->count; |
| 1367 | tm->n_pthreads += tr->count * tr->use_pthreads; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1368 | tm->n_threads += tr->count * (tr->use_pthreads == 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1369 | tr = tr->next; |
| 1370 | } |
| 1371 | |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | VLIB_EARLY_CONFIG_FUNCTION (cpu_config, "cpu"); |
| 1376 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 1377 | #if !defined (__x86_64__) && !defined (__i386__) && !defined (__aarch64__) && !defined (__powerpc64__) && !defined(__arm__) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1378 | void |
| 1379 | __sync_fetch_and_add_8 (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1380 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1381 | fformat (stderr, "%s called\n", __FUNCTION__); |
| 1382 | abort (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1383 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1384 | |
| 1385 | void |
| 1386 | __sync_add_and_fetch_8 (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1387 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1388 | fformat (stderr, "%s called\n", __FUNCTION__); |
| 1389 | abort (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1390 | } |
| 1391 | #endif |
| 1392 | |
| 1393 | void vnet_main_fixup (vlib_fork_fixup_t which) __attribute__ ((weak)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1394 | void |
| 1395 | vnet_main_fixup (vlib_fork_fixup_t which) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1396 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | void |
| 1400 | vlib_worker_thread_fork_fixup (vlib_fork_fixup_t which) |
| 1401 | { |
| 1402 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1403 | |
| 1404 | if (vlib_mains == 0) |
| 1405 | return; |
| 1406 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1407 | ASSERT (vlib_get_thread_index () == 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1408 | vlib_worker_thread_barrier_sync (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1409 | |
| 1410 | switch (which) |
| 1411 | { |
| 1412 | case VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX: |
| 1413 | vnet_main_fixup (VLIB_WORKER_THREAD_FORK_FIXUP_NEW_SW_IF_INDEX); |
| 1414 | break; |
| 1415 | |
| 1416 | default: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1417 | ASSERT (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1418 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1419 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1422 | /* |
| 1423 | * Enforce minimum open time to minimize packet loss due to Rx overflow, |
| 1424 | * based on a test based heuristic that barrier should be open for at least |
| 1425 | * 3 time as long as it is closed (with an upper bound of 1ms because by that |
| 1426 | * point it is probably too late to make a difference) |
| 1427 | */ |
| 1428 | |
| 1429 | #ifndef BARRIER_MINIMUM_OPEN_LIMIT |
| 1430 | #define BARRIER_MINIMUM_OPEN_LIMIT 0.001 |
| 1431 | #endif |
| 1432 | |
| 1433 | #ifndef BARRIER_MINIMUM_OPEN_FACTOR |
| 1434 | #define BARRIER_MINIMUM_OPEN_FACTOR 3 |
| 1435 | #endif |
| 1436 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1437 | void |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1438 | vlib_worker_thread_barrier_sync_int (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1439 | { |
| 1440 | f64 deadline; |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1441 | f64 now; |
| 1442 | f64 t_entry; |
| 1443 | f64 t_open; |
| 1444 | f64 t_closed; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1445 | u32 count; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1446 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 1447 | if (vec_len (vlib_mains) < 2) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1448 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1449 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1450 | ASSERT (vlib_get_thread_index () == 0); |
| 1451 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1452 | count = vec_len (vlib_mains) - 1; |
| 1453 | |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1454 | /* Record entry relative to last close */ |
| 1455 | now = vlib_time_now (vm); |
| 1456 | t_entry = now - vm->barrier_epoch; |
| 1457 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1458 | /* Tolerate recursive calls */ |
| 1459 | if (++vlib_worker_threads[0].recursion_level > 1) |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1460 | { |
| 1461 | barrier_trace_sync_rec (t_entry); |
| 1462 | return; |
| 1463 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1464 | |
Bud Grise | 42f2006 | 2016-03-16 13:09:46 -0400 | [diff] [blame] | 1465 | vlib_worker_threads[0].barrier_sync_count++; |
| 1466 | |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1467 | /* Enforce minimum barrier open time to minimize packet loss */ |
| 1468 | ASSERT (vm->barrier_no_close_before <= (now + BARRIER_MINIMUM_OPEN_LIMIT)); |
| 1469 | while ((now = vlib_time_now (vm)) < vm->barrier_no_close_before) |
| 1470 | ; |
| 1471 | |
| 1472 | /* Record time of closure */ |
| 1473 | t_open = now - vm->barrier_epoch; |
| 1474 | vm->barrier_epoch = now; |
| 1475 | |
| 1476 | deadline = now + BARRIER_SYNC_TIMEOUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1477 | |
| 1478 | *vlib_worker_threads->wait_at_barrier = 1; |
| 1479 | while (*vlib_worker_threads->workers_at_barrier != count) |
| 1480 | { |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1481 | if ((now = vlib_time_now (vm)) > deadline) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1482 | { |
| 1483 | fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__); |
| 1484 | os_panic (); |
| 1485 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1486 | } |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1487 | |
| 1488 | t_closed = now - vm->barrier_epoch; |
| 1489 | |
| 1490 | barrier_trace_sync (t_entry, t_open, t_closed); |
| 1491 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Dave Barach | 1ddbc01 | 2018-06-13 09:26:05 -0400 | [diff] [blame] | 1494 | void vlib_stat_segment_lock (void) __attribute__ ((weak)); |
| 1495 | void |
| 1496 | vlib_stat_segment_lock (void) |
| 1497 | { |
| 1498 | } |
| 1499 | |
| 1500 | void vlib_stat_segment_unlock (void) __attribute__ ((weak)); |
| 1501 | void |
| 1502 | vlib_stat_segment_unlock (void) |
| 1503 | { |
| 1504 | } |
| 1505 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1506 | void |
| 1507 | vlib_worker_thread_barrier_release (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1508 | { |
| 1509 | f64 deadline; |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1510 | f64 now; |
| 1511 | f64 minimum_open; |
| 1512 | f64 t_entry; |
| 1513 | f64 t_closed_total; |
| 1514 | f64 t_update_main = 0.0; |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1515 | int refork_needed = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1516 | |
Dave Barach | 80f54e2 | 2017-03-08 19:08:56 -0500 | [diff] [blame] | 1517 | if (vec_len (vlib_mains) < 2) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1518 | return; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1519 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1520 | ASSERT (vlib_get_thread_index () == 0); |
| 1521 | |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1522 | |
| 1523 | now = vlib_time_now (vm); |
| 1524 | t_entry = now - vm->barrier_epoch; |
| 1525 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1526 | if (--vlib_worker_threads[0].recursion_level > 0) |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1527 | { |
| 1528 | barrier_trace_release_rec (t_entry); |
| 1529 | return; |
| 1530 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1531 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1532 | /* Update (all) node runtimes before releasing the barrier, if needed */ |
| 1533 | if (vm->need_vlib_worker_thread_node_runtime_update) |
| 1534 | { |
Dave Barach | 1ddbc01 | 2018-06-13 09:26:05 -0400 | [diff] [blame] | 1535 | /* |
| 1536 | * Lock stat segment here, so we's safe when |
| 1537 | * rebuilding the stat segment node clones from the |
| 1538 | * stat thread... |
| 1539 | */ |
| 1540 | vlib_stat_segment_lock (); |
| 1541 | |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1542 | /* Do stats elements on main thread */ |
| 1543 | worker_thread_node_runtime_update_internal (); |
| 1544 | vm->need_vlib_worker_thread_node_runtime_update = 0; |
| 1545 | |
| 1546 | /* Do per thread rebuilds in parallel */ |
| 1547 | refork_needed = 1; |
| 1548 | clib_smp_atomic_add (vlib_worker_threads->node_reforks_required, |
| 1549 | (vec_len (vlib_mains) - 1)); |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1550 | now = vlib_time_now (vm); |
| 1551 | t_update_main = now - vm->barrier_epoch; |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1552 | } |
| 1553 | |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1554 | deadline = now + BARRIER_SYNC_TIMEOUT; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1555 | |
| 1556 | *vlib_worker_threads->wait_at_barrier = 0; |
| 1557 | |
| 1558 | while (*vlib_worker_threads->workers_at_barrier > 0) |
| 1559 | { |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1560 | if ((now = vlib_time_now (vm)) > deadline) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1561 | { |
| 1562 | fformat (stderr, "%s: worker thread deadlock\n", __FUNCTION__); |
| 1563 | os_panic (); |
| 1564 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1565 | } |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1566 | |
| 1567 | /* Wait for reforks before continuing */ |
| 1568 | if (refork_needed) |
| 1569 | { |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1570 | now = vlib_time_now (vm); |
| 1571 | |
| 1572 | deadline = now + BARRIER_SYNC_TIMEOUT; |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1573 | |
| 1574 | while (*vlib_worker_threads->node_reforks_required > 0) |
| 1575 | { |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1576 | if ((now = vlib_time_now (vm)) > deadline) |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1577 | { |
| 1578 | fformat (stderr, "%s: worker thread refork deadlock\n", |
| 1579 | __FUNCTION__); |
| 1580 | os_panic (); |
| 1581 | } |
| 1582 | } |
Dave Barach | 1ddbc01 | 2018-06-13 09:26:05 -0400 | [diff] [blame] | 1583 | vlib_stat_segment_unlock (); |
Colin Tregenza Dancer | 2159618 | 2017-09-04 15:27:49 +0100 | [diff] [blame] | 1584 | } |
Colin Tregenza Dancer | eb1ac17 | 2017-09-06 20:23:24 +0100 | [diff] [blame] | 1585 | |
| 1586 | t_closed_total = now - vm->barrier_epoch; |
| 1587 | |
| 1588 | minimum_open = t_closed_total * BARRIER_MINIMUM_OPEN_FACTOR; |
| 1589 | |
| 1590 | if (minimum_open > BARRIER_MINIMUM_OPEN_LIMIT) |
| 1591 | { |
| 1592 | minimum_open = BARRIER_MINIMUM_OPEN_LIMIT; |
| 1593 | } |
| 1594 | |
| 1595 | vm->barrier_no_close_before = now + minimum_open; |
| 1596 | |
| 1597 | /* Record barrier epoch (used to enforce minimum open time) */ |
| 1598 | vm->barrier_epoch = now; |
| 1599 | |
| 1600 | barrier_trace_release (t_entry, t_closed_total, t_update_main); |
| 1601 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1604 | /* |
| 1605 | * Check the frame queue to see if any frames are available. |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1606 | * If so, pull the packets off the frames and put them to |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1607 | * the handoff node. |
| 1608 | */ |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1609 | int |
| 1610 | vlib_frame_queue_dequeue (vlib_main_t * vm, vlib_frame_queue_main_t * fqm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1611 | { |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1612 | u32 thread_id = vm->thread_index; |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1613 | vlib_frame_queue_t *fq = fqm->vlib_frame_queues[thread_id]; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1614 | vlib_frame_queue_elt_t *elt; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1615 | u32 *from, *to; |
| 1616 | vlib_frame_t *f; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1617 | int msg_type; |
| 1618 | int processed = 0; |
| 1619 | u32 n_left_to_node; |
| 1620 | u32 vectors = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1621 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1622 | ASSERT (fq); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1623 | ASSERT (vm == vlib_mains[thread_id]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1624 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1625 | if (PREDICT_FALSE (fqm->node_index == ~0)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1626 | return 0; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1627 | /* |
| 1628 | * Gather trace data for frame queues |
| 1629 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1630 | if (PREDICT_FALSE (fq->trace)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1631 | { |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1632 | frame_queue_trace_t *fqt; |
| 1633 | frame_queue_nelt_counter_t *fqh; |
| 1634 | u32 elix; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1635 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1636 | fqt = &fqm->frame_queue_traces[thread_id]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1637 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1638 | fqt->nelts = fq->nelts; |
| 1639 | fqt->head = fq->head; |
| 1640 | fqt->head_hint = fq->head_hint; |
| 1641 | fqt->tail = fq->tail; |
| 1642 | fqt->threshold = fq->vector_threshold; |
| 1643 | fqt->n_in_use = fqt->tail - fqt->head; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1644 | if (fqt->n_in_use >= fqt->nelts) |
| 1645 | { |
| 1646 | // if beyond max then use max |
| 1647 | fqt->n_in_use = fqt->nelts - 1; |
| 1648 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1649 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1650 | /* Record the number of elements in use in the histogram */ |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1651 | fqh = &fqm->frame_queue_histogram[thread_id]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1652 | fqh->count[fqt->n_in_use]++; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1653 | |
| 1654 | /* Record a snapshot of the elements in use */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1655 | for (elix = 0; elix < fqt->nelts; elix++) |
| 1656 | { |
| 1657 | elt = fq->elts + ((fq->head + 1 + elix) & (fq->nelts - 1)); |
| 1658 | if (1 || elt->valid) |
| 1659 | { |
| 1660 | fqt->n_vectors[elix] = elt->n_vectors; |
| 1661 | } |
| 1662 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1663 | fqt->written = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1664 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1665 | |
| 1666 | while (1) |
| 1667 | { |
| 1668 | if (fq->head == fq->tail) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1669 | { |
| 1670 | fq->head_hint = fq->head; |
| 1671 | return processed; |
| 1672 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1673 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1674 | elt = fq->elts + ((fq->head + 1) & (fq->nelts - 1)); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1675 | |
| 1676 | if (!elt->valid) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1677 | { |
| 1678 | fq->head_hint = fq->head; |
| 1679 | return processed; |
| 1680 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1681 | |
| 1682 | from = elt->buffer_index; |
| 1683 | msg_type = elt->msg_type; |
| 1684 | |
| 1685 | ASSERT (msg_type == VLIB_FRAME_QUEUE_ELT_DISPATCH_FRAME); |
| 1686 | ASSERT (elt->n_vectors <= VLIB_FRAME_SIZE); |
| 1687 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1688 | f = vlib_get_frame_to_node (vm, fqm->node_index); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1689 | |
| 1690 | to = vlib_frame_vector_args (f); |
| 1691 | |
| 1692 | n_left_to_node = elt->n_vectors; |
| 1693 | |
| 1694 | while (n_left_to_node >= 4) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1695 | { |
| 1696 | to[0] = from[0]; |
| 1697 | to[1] = from[1]; |
| 1698 | to[2] = from[2]; |
| 1699 | to[3] = from[3]; |
| 1700 | to += 4; |
| 1701 | from += 4; |
| 1702 | n_left_to_node -= 4; |
| 1703 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1704 | |
| 1705 | while (n_left_to_node > 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1706 | { |
| 1707 | to[0] = from[0]; |
| 1708 | to++; |
| 1709 | from++; |
| 1710 | n_left_to_node--; |
| 1711 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1712 | |
| 1713 | vectors += elt->n_vectors; |
| 1714 | f->n_vectors = elt->n_vectors; |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1715 | vlib_put_frame_to_node (vm, fqm->node_index, f); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1716 | |
| 1717 | elt->valid = 0; |
| 1718 | elt->n_vectors = 0; |
| 1719 | elt->msg_type = 0xfefefefe; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1720 | CLIB_MEMORY_BARRIER (); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1721 | fq->head++; |
| 1722 | processed++; |
| 1723 | |
| 1724 | /* |
| 1725 | * Limit the number of packets pushed into the graph |
| 1726 | */ |
| 1727 | if (vectors >= fq->vector_threshold) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1728 | { |
| 1729 | fq->head_hint = fq->head; |
| 1730 | return processed; |
| 1731 | } |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1732 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1733 | ASSERT (0); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1734 | return processed; |
| 1735 | } |
| 1736 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1737 | void |
| 1738 | vlib_worker_thread_fn (void *arg) |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1739 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1740 | vlib_worker_thread_t *w = (vlib_worker_thread_t *) arg; |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1741 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1742 | vlib_main_t *vm = vlib_get_main (); |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 1743 | clib_error_t *e; |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1744 | |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 1745 | ASSERT (vm->thread_index == vlib_get_thread_index ()); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1746 | |
| 1747 | vlib_worker_thread_init (w); |
| 1748 | clib_time_init (&vm->clib_time); |
| 1749 | clib_mem_set_heap (w->thread_mheap); |
| 1750 | |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 1751 | /* Wait until the dpdk init sequence is complete */ |
| 1752 | while (tm->extern_thread_mgmt && tm->worker_thread_release == 0) |
| 1753 | vlib_worker_thread_barrier_check (); |
| 1754 | |
Damjan Marion | e9f929b | 2017-03-16 11:32:09 +0100 | [diff] [blame] | 1755 | e = vlib_call_init_exit_functions |
| 1756 | (vm, vm->worker_init_function_registrations, 1 /* call_once */ ); |
| 1757 | if (e) |
| 1758 | clib_error_report (e); |
| 1759 | |
Damjan Marion | e9d52d5 | 2017-03-09 15:42:26 +0100 | [diff] [blame] | 1760 | vlib_worker_loop (vm); |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1761 | } |
| 1762 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1763 | /* *INDENT-OFF* */ |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1764 | VLIB_REGISTER_THREAD (worker_thread_reg, static) = { |
| 1765 | .name = "workers", |
| 1766 | .short_name = "wk", |
| 1767 | .function = vlib_worker_thread_fn, |
| 1768 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1769 | /* *INDENT-ON* */ |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1770 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1771 | u32 |
| 1772 | vlib_frame_queue_main_init (u32 node_index, u32 frame_queue_nelts) |
| 1773 | { |
| 1774 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 1775 | vlib_frame_queue_main_t *fqm; |
| 1776 | vlib_frame_queue_t *fq; |
| 1777 | int i; |
| 1778 | |
| 1779 | if (frame_queue_nelts == 0) |
| 1780 | frame_queue_nelts = FRAME_QUEUE_NELTS; |
| 1781 | |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 1782 | ASSERT (frame_queue_nelts >= 8); |
| 1783 | |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1784 | vec_add2 (tm->frame_queue_mains, fqm, 1); |
| 1785 | |
| 1786 | fqm->node_index = node_index; |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 1787 | fqm->frame_queue_nelts = frame_queue_nelts; |
| 1788 | fqm->queue_hi_thresh = frame_queue_nelts - 2; |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1789 | |
| 1790 | vec_validate (fqm->vlib_frame_queues, tm->n_vlib_mains - 1); |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 1791 | vec_validate (fqm->per_thread_data, tm->n_vlib_mains - 1); |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1792 | _vec_len (fqm->vlib_frame_queues) = 0; |
| 1793 | for (i = 0; i < tm->n_vlib_mains; i++) |
| 1794 | { |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 1795 | vlib_frame_queue_per_thread_data_t *ptd; |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1796 | fq = vlib_frame_queue_alloc (frame_queue_nelts); |
| 1797 | vec_add1 (fqm->vlib_frame_queues, fq); |
Damjan Marion | 78fd7e8 | 2018-07-20 18:47:05 +0200 | [diff] [blame] | 1798 | |
| 1799 | ptd = vec_elt_at_index (fqm->per_thread_data, i); |
| 1800 | vec_validate (ptd->handoff_queue_elt_by_thread_index, |
| 1801 | tm->n_vlib_mains - 1); |
| 1802 | vec_validate_init_empty (ptd->congested_handoff_queue_by_thread_index, |
| 1803 | tm->n_vlib_mains - 1, |
| 1804 | (vlib_frame_queue_t *) (~0)); |
Damjan Marion | aaef1eb | 2016-11-08 17:37:01 +0100 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | return (fqm - tm->frame_queue_mains); |
| 1808 | } |
| 1809 | |
Damjan Marion | 878c609 | 2017-01-04 13:19:27 +0100 | [diff] [blame] | 1810 | int |
| 1811 | vlib_thread_cb_register (struct vlib_main_t *vm, vlib_thread_callbacks_t * cb) |
| 1812 | { |
| 1813 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 1814 | |
| 1815 | if (tm->extern_thread_mgmt) |
| 1816 | return -1; |
| 1817 | |
| 1818 | tm->cb.vlib_launch_thread_cb = cb->vlib_launch_thread_cb; |
| 1819 | tm->extern_thread_mgmt = 1; |
| 1820 | return 0; |
| 1821 | } |
| 1822 | |
Dave Barach | 69128d0 | 2017-09-26 10:54:34 -0400 | [diff] [blame] | 1823 | void |
| 1824 | vlib_process_signal_event_mt_helper (vlib_process_signal_event_mt_args_t * |
| 1825 | args) |
| 1826 | { |
| 1827 | ASSERT (vlib_get_thread_index () == 0); |
| 1828 | vlib_process_signal_event (vlib_get_main (), args->node_index, |
| 1829 | args->type_opaque, args->data); |
| 1830 | } |
| 1831 | |
| 1832 | void *rpc_call_main_thread_cb_fn; |
| 1833 | |
| 1834 | void |
| 1835 | vlib_rpc_call_main_thread (void *callback, u8 * args, u32 arg_size) |
| 1836 | { |
| 1837 | if (rpc_call_main_thread_cb_fn) |
| 1838 | { |
| 1839 | void (*fp) (void *, u8 *, u32) = rpc_call_main_thread_cb_fn; |
| 1840 | (*fp) (callback, args, arg_size); |
| 1841 | } |
| 1842 | else |
| 1843 | clib_warning ("BUG: rpc_call_main_thread_cb_fn NULL!"); |
| 1844 | } |
| 1845 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1846 | clib_error_t * |
| 1847 | threads_init (vlib_main_t * vm) |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1848 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1849 | return 0; |
| 1850 | } |
| 1851 | |
Damjan Marion | 0f8ecf0 | 2016-06-27 08:30:30 +0200 | [diff] [blame] | 1852 | VLIB_INIT_FUNCTION (threads_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1853 | |
| 1854 | /* |
| 1855 | * fd.io coding-style-patch-verification: ON |
| 1856 | * |
| 1857 | * Local Variables: |
| 1858 | * eval: (c-set-style "gnu") |
| 1859 | * End: |
| 1860 | */ |