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