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