From 5f2cfb21fdf6eb4a32346809c8cee2550d775b19 Mon Sep 17 00:00:00 2001 From: Dave Barach Date: Mon, 20 May 2019 10:28:57 -0400 Subject: Add callback multiplex support Change-Id: Iddeb3a1b0e20706e72ec8f74dabc60b342f003ba Signed-off-by: Dave Barach --- src/plugins/perfmon/perfmon_periodic.c | 62 +++++++++++++----- src/vlib/main.c | 11 ++-- src/vlib/main.h | 18 ++++-- src/vlib/vlib.h | 1 + src/vppinfra/CMakeLists.txt | 1 + src/vppinfra/callback.h | 111 +++++++++++++++++++++++++++++++++ 6 files changed, 177 insertions(+), 27 deletions(-) create mode 100644 src/vppinfra/callback.h (limited to 'src') diff --git a/src/plugins/perfmon/perfmon_periodic.c b/src/plugins/perfmon/perfmon_periodic.c index d21b464b07b..ac68c42c219 100644 --- a/src/plugins/perfmon/perfmon_periodic.c +++ b/src/plugins/perfmon/perfmon_periodic.c @@ -52,12 +52,18 @@ read_current_perf_counters (vlib_main_t * vm, u64 * c0, u64 * c1, else { u64 sw_value; - if (read (pm->pm_fds[i][my_thread_index], &sw_value, - sizeof (sw_value)) != sizeof (sw_value)) + int read_result; + if ((read_result = read (pm->pm_fds[i][my_thread_index], &sw_value, + sizeof (sw_value)) != sizeof (sw_value))) { clib_unix_warning - ("counter read failed, disable collection..."); - vm->vlib_node_runtime_perf_counter_cb = 0; + ("counter read returned %d, expected %d", + read_result, sizeof (sw_value)); + clib_callback_enable_disable + (vm->vlib_node_runtime_perf_counter_cbs, + vm->vlib_node_runtime_perf_counter_cb_tmp, + vm->worker_thread_main_loop_callback_lock, + read_current_perf_counters, 0 /* enable */ ); return; } *cc = sw_value; @@ -202,7 +208,11 @@ enable_current_events (perfmon_main_t * pm) pm->n_active = i; /* Enable the main loop counter snapshot mechanism */ - vm->vlib_node_runtime_perf_counter_cb = read_current_perf_counters; + clib_callback_enable_disable + (vm->vlib_node_runtime_perf_counter_cbs, + vm->vlib_node_runtime_perf_counter_cb_tmp, + vm->worker_thread_main_loop_callback_lock, + read_current_perf_counters, 1 /* enable */ ); } static void @@ -213,7 +223,11 @@ disable_events (perfmon_main_t * pm) int i; /* Stop main loop collection */ - vm->vlib_node_runtime_perf_counter_cb = 0; + clib_callback_enable_disable + (vm->vlib_node_runtime_perf_counter_cbs, + vm->vlib_node_runtime_perf_counter_cb_tmp, + vm->worker_thread_main_loop_callback_lock, + read_current_perf_counters, 0 /* enable */ ); for (i = 0; i < pm->n_active; i++) { @@ -239,16 +253,22 @@ worker_thread_start_event (vlib_main_t * vm) { perfmon_main_t *pm = &perfmon_main; + clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks, + vm->worker_thread_main_loop_callback_tmp, + vm->worker_thread_main_loop_callback_lock, + worker_thread_start_event, 0 /* enable */ ); enable_current_events (pm); - vm->worker_thread_main_loop_callback = 0; } static void worker_thread_stop_event (vlib_main_t * vm) { perfmon_main_t *pm = &perfmon_main; + clib_callback_enable_disable (vm->worker_thread_main_loop_callbacks, + vm->worker_thread_main_loop_callback_tmp, + vm->worker_thread_main_loop_callback_lock, + worker_thread_stop_event, 0 /* enable */ ); disable_events (pm); - vm->worker_thread_main_loop_callback = 0; } static void @@ -285,8 +305,11 @@ start_event (perfmon_main_t * pm, f64 now, uword event_data) continue; if (all || clib_bitmap_get (pm->thread_bitmap, i)) - vlib_mains[i]->worker_thread_main_loop_callback = (void *) - worker_thread_start_event; + clib_callback_enable_disable + (vlib_mains[i]->worker_thread_main_loop_callbacks, + vlib_mains[i]->worker_thread_main_loop_callback_tmp, + vlib_mains[i]->worker_thread_main_loop_callback_lock, + (void *) worker_thread_start_event, 1 /* enable */ ); } } @@ -445,8 +468,11 @@ handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now) if (vlib_mains[i] == 0) continue; if (all || clib_bitmap_get (pm->thread_bitmap, i)) - vlib_mains[i]->worker_thread_main_loop_callback = (void *) - worker_thread_stop_event; + clib_callback_enable_disable + (vlib_mains[i]->worker_thread_main_loop_callbacks, + vlib_mains[i]->worker_thread_main_loop_callback_tmp, + vlib_mains[i]->worker_thread_main_loop_callback_lock, + (void *) worker_thread_stop_event, 1 /* enable */ ); } /* Make sure workers have stopped collection */ @@ -457,7 +483,10 @@ handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now) for (i = 1; i < vec_len (vlib_mains); i++) { /* Has the worker actually stopped collecting data? */ - while (vlib_mains[i]->worker_thread_main_loop_callback) + while (clib_callback_is_set + (vlib_mains[i]->worker_thread_main_loop_callbacks, + vlib_mains[i]->worker_thread_main_loop_callback_lock, + read_current_perf_counters)) { if (vlib_time_now (vm) > deadman) { @@ -486,8 +515,11 @@ handle_timeout (vlib_main_t * vm, perfmon_main_t * pm, f64 now) if (vlib_mains[i] == 0) continue; if (all || clib_bitmap_get (pm->thread_bitmap, i)) - vlib_mains[i]->worker_thread_main_loop_callback = (void *) - worker_thread_start_event; + clib_callback_enable_disable + (vlib_mains[i]->worker_thread_main_loop_callbacks, + vlib_mains[i]->worker_thread_main_loop_callback_tmp, + vlib_mains[i]->worker_thread_main_loop_callback_lock, + worker_thread_start_event, 1 /* enable */ ); } } diff --git a/src/vlib/main.c b/src/vlib/main.c index 759c1d0ec0d..b6006e8bab0 100644 --- a/src/vlib/main.c +++ b/src/vlib/main.c @@ -687,9 +687,9 @@ vlib_node_runtime_perf_counter (vlib_main_t * vm, u64 * pmc0, u64 * pmc1, { *pmc0 = 0; *pmc1 = 0; - if (PREDICT_FALSE (vm->vlib_node_runtime_perf_counter_cb != 0)) - (*vm->vlib_node_runtime_perf_counter_cb) (vm, pmc0, pmc1, node, - frame, before_or_after); + if (PREDICT_FALSE (vec_len (vm->vlib_node_runtime_perf_counter_cbs) != 0)) + clib_call_callbacks (vm->vlib_node_runtime_perf_counter_cbs, vm, pmc0, + pmc1, node, frame, before_or_after); } always_inline void @@ -1760,9 +1760,8 @@ vlib_main_or_worker_loop (vlib_main_t * vm, int is_main) else frame_queue_check_counter--; } - if (PREDICT_FALSE (vm->worker_thread_main_loop_callback != 0)) - ((void (*)(vlib_main_t *)) vm->worker_thread_main_loop_callback) - (vm); + if (PREDICT_FALSE (vec_len (vm->worker_thread_main_loop_callbacks))) + clib_call_callbacks (vm->worker_thread_main_loop_callbacks, vm); } /* Process pre-input nodes. */ diff --git a/src/vlib/main.h b/src/vlib/main.h index 42f0c5196da..9b6311601d3 100644 --- a/src/vlib/main.h +++ b/src/vlib/main.h @@ -94,11 +94,14 @@ typedef struct vlib_main_t u32 node_counts_per_main_loop[2]; /* Main loop hw / sw performance counters */ - void (*vlib_node_runtime_perf_counter_cb) (struct vlib_main_t *, - u64 *, u64 *, - vlib_node_runtime_t *, - vlib_frame_t *, int); - + void (**vlib_node_runtime_perf_counter_cbs) (struct vlib_main_t *, + u64 *, u64 *, + vlib_node_runtime_t *, + vlib_frame_t *, int); + void (**vlib_node_runtime_perf_counter_cb_tmp) (struct vlib_main_t *, + u64 *, u64 *, + vlib_node_runtime_t *, + vlib_frame_t *, int); /* Every so often we switch to the next counter. */ #define VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE 7 @@ -215,7 +218,10 @@ typedef struct vlib_main_t u8 **argv; /* Top of (worker) dispatch loop callback */ - volatile void (*worker_thread_main_loop_callback) (struct vlib_main_t *); + void (**volatile worker_thread_main_loop_callbacks) (struct vlib_main_t *); + void (**volatile worker_thread_main_loop_callback_tmp) + (struct vlib_main_t *); + clib_spinlock_t worker_thread_main_loop_callback_lock; /* debugging */ volatile int parked_at_barrier; diff --git a/src/vlib/vlib.h b/src/vlib/vlib.h index b5fe47b3729..8f59cae2cee 100644 --- a/src/vlib/vlib.h +++ b/src/vlib/vlib.h @@ -42,6 +42,7 @@ #include #include +#include /* Generic definitions. */ #include diff --git a/src/vppinfra/CMakeLists.txt b/src/vppinfra/CMakeLists.txt index 02e5322e1fc..a42bcb818a5 100644 --- a/src/vppinfra/CMakeLists.txt +++ b/src/vppinfra/CMakeLists.txt @@ -99,6 +99,7 @@ set(VPPINFRA_HEADERS bitops.h byte_order.h cache.h + callback.h clib_error.h clib.h cpu.h diff --git a/src/vppinfra/callback.h b/src/vppinfra/callback.h new file mode 100644 index 00000000000..595d69d72ab --- /dev/null +++ b/src/vppinfra/callback.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** @file + * @brief Callback multiplex scheme + * For a fully worked-out example, see .../src/vlib/main.[ch] and + * .../src/plugins/perfmon.c + */ + +#ifndef included_callback_h +#define included_callback_h +#include + +/** @brief Add or remove a callback to the specified callback set + * @param h head of the callback vector + * @param tmp vector to build result + * @param l clib_spinlock_t lock to protect the vector, may be 0 + * @param f function to add or remove + * @param enable 1 adds f to the vector, 0 removes f from the vector + * + * Add or remove a callback from the indicated callback vector. + * Caller must provide locking to prevent > 1 concurrent writer + * Swaps the head of the callback vector and a tmp vector in one + * motion, after a write barrier to ensure that the write is atomic. + */ +#define clib_callback_enable_disable(h,tmp,l,f,enable) \ +do { \ + void *tmp2; \ + clib_spinlock_lock_if_init(&l); \ + vec_reset_length(tmp); \ + vec_append(tmp, h); \ + if (enable) \ + vec_add1 (tmp,(void *)f); \ + else \ + { \ + int i; \ + for (i = 0; i < vec_len (tmp); i++) \ + if (((void *)tmp[i]) == (void *)f) \ + { \ + vec_delete (tmp, 1, i); \ + break; \ + } \ + } \ + tmp2 = h; \ + CLIB_MEMORY_STORE_BARRIER(); \ + h = tmp; \ + tmp = tmp2; \ + clib_spinlock_unlock_if_init(&l); \ +} while(0); + +/** @brief call the specified callback set + * @param h the callback set + * @param varargs additional callback parameters + */ +#define clib_call_callbacks(h, ... ) \ +do { \ + /* \ + * Note: fp exists to shut up gcc-6, which \ + * produces a warning not seen with gcc-7 or 8 \ + */ \ + void (*fp)(void *a1, ...); \ + int i; \ + for (i = 0; i < vec_len (h); i++) \ + { \ + fp = (void *)(h[i]); \ + (*fp) (__VA_ARGS__); \ + } \ + } while (0); + +/** @brief predicate function says whether the specified function is enabled + * @param h the callback set + * @param l clib_spinlock_t lock to protect the vector, may be 0 + * @param f the function to search for + * @return 1 if the function is enabled, 0 if not + */ +#define clib_callback_is_set(h,l,f) \ +({ \ + int _i; \ + int _found = 0; \ + clib_spinlock_lock_if_init(&l); \ + for (_i = 0; _i < vec_len (h); _i++) \ + if (((void *)h[_i]) == (void *) f) \ + { \ + _found=1; \ + break; \ + } \ + clib_spinlock_unlock_if_init(&l); \ + _found; \ + }) + +#endif /* included_callback_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ -- cgit 1.2.3-korg