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