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