From: Gaetan Rivet <[email protected]> Add a reference-counted key-value map.
Duplicates take a reference on the original entry within the map, leaving it in place. To be able to execute an entry creation after determining whether it is already present or not in the map, store relevant initialization and de-initialization functions. Signed-off-by: Gaetan Rivet <[email protected]> Co-authored-by: Eli Britstein <[email protected]> Signed-off-by: Eli Britstein <[email protected]> --- lib/automake.mk | 2 + lib/refmap.c | 494 +++++++++++++++ lib/refmap.h | 158 +++++ tests/automake.mk | 1 + tests/library.at | 5 + tests/test-refmap.c | 1107 +++++++++++++++++++++++++++++++++ utilities/checkpatch_dict.txt | 2 + 7 files changed, 1769 insertions(+) create mode 100644 lib/refmap.c create mode 100644 lib/refmap.h create mode 100644 tests/test-refmap.c diff --git a/lib/automake.mk b/lib/automake.mk index c6e988906..cb6458b0d 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -323,6 +323,8 @@ lib_libopenvswitch_la_SOURCES = \ lib/rculist.h \ lib/reconnect.c \ lib/reconnect.h \ + lib/refmap.c \ + lib/refmap.h \ lib/rstp.c \ lib/rstp.h \ lib/rstp-common.h \ diff --git a/lib/refmap.c b/lib/refmap.c new file mode 100644 index 000000000..67c666bf0 --- /dev/null +++ b/lib/refmap.c @@ -0,0 +1,494 @@ +/* + * Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * 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. + */ + +#include <config.h> + +#include "refmap.h" + +#include "cmap.h" +#include "fatal-signal.h" +#include "hash.h" +#include "ovs-atomic.h" +#include "ovs-thread.h" +#include "timeval.h" + +#include "openvswitch/list.h" +#include "openvswitch/vlog.h" + +VLOG_DEFINE_THIS_MODULE(refmap); +static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(600, 600); + +static struct ovs_mutex refmap_destroy_lock = OVS_MUTEX_INITIALIZER; +static struct ovs_list refmap_destroy_list + OVS_GUARDED_BY(refmap_destroy_lock) = + OVS_LIST_INITIALIZER(&refmap_destroy_list); + +struct refmap { + struct cmap map; + struct ovs_mutex map_lock; + size_t key_size; + size_t value_size; + refmap_value_init value_init; + refmap_value_uninit value_uninit; + refmap_value_format value_format; + char *name; + struct ovs_list in_destroy_list; +}; + +struct refmap_node { + /* CMAP related: */ + struct cmap_node map_node; + uint32_t hash; + /* Content: */ + struct ovs_refcount refcount; + /* Key, then Value follows. */ +}; + +static void +refmap_destroy__(struct refmap *, bool global_destroy); + +static void +refmap_destroy_unregister_protected(struct refmap *rfm) + OVS_REQUIRES(refmap_destroy_lock) +{ + ovs_list_remove(&rfm->in_destroy_list); +} + +static void +refmap_destroy_unregister(struct refmap *rfm) + OVS_EXCLUDED(refmap_destroy_lock) +{ + ovs_mutex_lock(&refmap_destroy_lock); + refmap_destroy_unregister_protected(rfm); + ovs_mutex_unlock(&refmap_destroy_lock); +} + +static void +refmap_destroy_register(struct refmap *rfm) + OVS_EXCLUDED(refmap_destroy_lock) +{ + ovs_mutex_lock(&refmap_destroy_lock); + ovs_list_push_back(&refmap_destroy_list, &rfm->in_destroy_list); + ovs_mutex_unlock(&refmap_destroy_lock); +} + +static void +refmap_destroy_all(void *aux OVS_UNUSED) +{ + struct refmap *rfm; + + ovs_mutex_lock(&refmap_destroy_lock); + LIST_FOR_EACH_SAFE (rfm, in_destroy_list, &refmap_destroy_list) { + refmap_destroy_unregister_protected(rfm); + refmap_destroy__(rfm, true); + } + ovs_mutex_unlock(&refmap_destroy_lock); + ovs_mutex_destroy(&refmap_destroy_lock); +} + +static void +refmap_fatal_signal_hook(void *aux OVS_UNUSED) +{ + /* This argument is only for the type check in 'ovsrcu_postpone', + * it is not otherwise used. */ + static int dummy_arg; + + /* Do not run all destroys right in the signal handler. + * Let other modules execute their own cleanup, and then + * iterate over any remaining to warn about leaks. */ + ovsrcu_postpone(refmap_destroy_all, &dummy_arg); +} + +struct refmap * +refmap_create(const char *name, + size_t key_size, + size_t value_size, + refmap_value_init value_init, + refmap_value_uninit value_uninit, + refmap_value_format value_format) +{ + static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; + struct refmap *rfm; + + ovs_assert(value_init && value_uninit); + + if (ovsthread_once_start(&once)) { + fatal_signal_add_hook(refmap_fatal_signal_hook, NULL, NULL, true); + ovsthread_once_done(&once); + } + + rfm = xzalloc(sizeof *rfm); + rfm->name = xstrdup(name); + rfm->key_size = key_size; + rfm->value_size = value_size; + rfm->value_init = value_init; + rfm->value_uninit = value_uninit; + rfm->value_format = value_format; + + ovs_mutex_init(&rfm->map_lock); + cmap_init(&rfm->map); + + refmap_destroy_register(rfm); + + return rfm; +} + +static void +refmap_destroy__(struct refmap *rfm, bool global_destroy) +{ + bool leaks_detected = false; + + if (!rfm) { + return; + } + + VLOG_DBG("%s: destroying the map", rfm->name); + + ovs_mutex_lock(&rfm->map_lock); + if (!cmap_is_empty(&rfm->map)) { + struct refmap_node *node; + + VLOG_WARN("%s: %s called with elements remaining in the map", + rfm->name, __func__); + leaks_detected = true; + CMAP_FOR_EACH (node, map_node, &rfm->map) { + /* No need to remove the node from the CMAP, it will + * be destroyed immediately. */ + ovsrcu_postpone(free, node); + } + } + cmap_destroy(&rfm->map); + ovs_mutex_unlock(&rfm->map_lock); + + ovs_mutex_destroy(&rfm->map_lock); + free(rfm->name); + free(rfm); + + /* During the very last stage of execution of RCU callbacks, + * the VLOG subsystem has been disabled. All logs are thus muted. + * If leaks are detected, abort the process, even though we were + * exiting due to a fatal signal. The SIGABRT generated will still + * be visible. */ + if (global_destroy && leaks_detected) { + ovs_abort(-1, "Refmap values leak detected"); + } +} + +void +refmap_destroy(struct refmap *rfm) +{ + if (!rfm) { + return; + } + + refmap_destroy_unregister(rfm); + refmap_destroy__(rfm, false); +} + +static size_t +refmap_aligned_key_size(struct refmap *rfm) +{ + return ROUND_UP(rfm->key_size, 8); +} + +static void * +refmap_node_key(struct refmap_node *node) +{ + if (!node) { + return NULL; + } + + return node + 1; +} + +static void * +refmap_node_value(struct refmap *rfm, struct refmap_node *node) +{ + if (!node) { + return NULL; + } + + return ((char *) refmap_node_key(node)) + refmap_aligned_key_size(rfm); +} + +static size_t +refmap_node_total_size(struct refmap *rfm) +{ + return sizeof(struct refmap_node) + + refmap_aligned_key_size(rfm) + rfm->value_size; +} + +static struct refmap_node * +refmap_node_from_value(struct refmap *rfm, void *value) +{ + size_t offset = sizeof(struct refmap_node) + refmap_aligned_key_size(rfm); + + if ((uintptr_t) value < offset) { + return NULL; + } + + return (void *) (((char *) value) - offset); +} + +static void +log_node(struct refmap *rfm, const char *prefix, struct refmap_node *node) +{ + void *key, *value; + struct ds s; + + if (OVS_LIKELY(VLOG_DROP_DBG(&rl) || !rfm->value_format)) { + return; + } + + key = refmap_node_key(node); + value = refmap_node_value(rfm, node); + + ds_init(&s); + rfm->value_format(&s, key, value); + /* The ref-count is logged as ~%u to emphasize it might have changed + * between the moment of the read to the actual print. */ + VLOG_DBG("%s[%s]: value=%p, refcnt=~%u, '%s'", rfm->name, prefix, value, + ovs_refcount_read(&node->refcount), ds_cstr(&s)); + ds_destroy(&s); +} + +bool +refmap_iter_next(struct refmap_iter *iter, void **value, void **key) +{ + struct refmap_node *node; + + if (iter->prev_value) { + refmap_unref(iter->rfm, iter->prev_value); + iter->prev_value = NULL; + } + + if (!iter->cursor.impl) { + iter->cursor = cmap_cursor_start(&iter->rfm->map); + } + + CMAP_CURSOR_FOR_EACH_CONTINUE (node, map_node, &iter->cursor) { + if (!ovs_refcount_try_ref_rcu(&node->refcount)) { + continue; + } + + log_node(iter->rfm, "foreach", node); + *value = refmap_node_value(iter->rfm, node); + *key = refmap_node_key(node); + iter->prev_value = *value; + return true; + } + + return false; +} + +static uint32_t +refmap_key_hash(const struct refmap *rfm, const void *key) +{ + return hash_bytes(key, rfm->key_size, 0); +} + +static struct refmap_node * +refmap_lookup_protected(struct refmap *rfm, void *key, uint32_t hash) +{ + struct refmap_node *node; + + CMAP_FOR_EACH_WITH_HASH_PROTECTED (node, map_node, hash, &rfm->map) { + if (!memcmp(key, refmap_node_key(node), rfm->key_size) && + ovs_refcount_read(&node->refcount) > 0) { + return node; + } + } + + return NULL; +} + +static struct refmap_node * +refmap_lookup(struct refmap *rfm, void *key, uint32_t hash) +{ + struct refmap_node *node; + + CMAP_FOR_EACH_WITH_HASH (node, map_node, hash, &rfm->map) { + if (!memcmp(key, refmap_node_key(node), rfm->key_size) && + ovs_refcount_read(&node->refcount) > 0) { + return node; + } + } + + return NULL; +} + +void * +refmap_try_ref(struct refmap *rfm, void *key) +{ + struct refmap_node *node; + + node = refmap_lookup(rfm, key, refmap_key_hash(rfm, key)); + if (!node) { + return NULL; + } + + if (!ovs_refcount_try_ref_rcu(&node->refcount)) { + log_node(rfm, "try_ref-fail", node); + return NULL; + } + + log_node(rfm, "try_ref", node); + return refmap_node_value(rfm, node); +} + +void * +refmap_ref(struct refmap *rfm, void *key, void *arg) +{ + struct refmap_node *node; + bool error = false; + uint32_t hash; + void *value; + + hash = refmap_key_hash(rfm, key); + + node = refmap_lookup(rfm, key, hash); + if (node && ovs_refcount_try_ref_rcu(&node->refcount)) { + value = refmap_node_value(rfm, node); + goto out; + } + + ovs_mutex_lock(&rfm->map_lock); + + node = refmap_lookup_protected(rfm, key, hash); + if (node && ovs_refcount_try_ref_rcu(&node->refcount)) { + ovs_mutex_unlock(&rfm->map_lock); + value = refmap_node_value(rfm, node); + goto out; + } + + node = xzalloc(refmap_node_total_size(rfm)); + node->hash = hash; + ovs_refcount_init(&node->refcount); + memcpy(refmap_node_key(node), key, rfm->key_size); + value = refmap_node_value(rfm, node); + if (rfm->value_init(value, arg) == 0) { + cmap_insert(&rfm->map, &node->map_node, node->hash); + } else { + value = NULL; + error = true; + VLOG_WARN("%s: value_init failed", rfm->name); + } + ovs_mutex_unlock(&rfm->map_lock); + +out: + if (error) { + free(node); + return NULL; + } + + log_node(rfm, "ref", node); + + return value; +} + +bool +refmap_try_ref_value(struct refmap *rfm, void *value) +{ + struct refmap_node *node; + + if (!value) { + return false; + } + + node = refmap_node_from_value(rfm, value); + if (!node || !ovs_refcount_try_ref_rcu(&node->refcount)) { + return false; + } + + log_node(rfm, "try_ref_value", node); + return true; +} + +static inline bool +ovs_refcount_unref_if_not_last(struct ovs_refcount *refcount) +{ + unsigned int count; + + atomic_read_explicit(&refcount->count, &count, + memory_order_acquire); + ovs_assert(count > 0); + while (count > 1) { + if (atomic_compare_exchange_weak_explicit( + &refcount->count, &count, count - 1, + memory_order_release, memory_order_relaxed)) { + return true; + } + } + return false; + } + +bool +refmap_unref(struct refmap *rfm, void *value) +{ + unsigned int old_refcount; + struct refmap_node *node; + + if (!value) { + return false; + } + + node = refmap_node_from_value(rfm, value); + if (!node) { + return false; + } + + if (ovs_refcount_unref_if_not_last(&node->refcount)) { + return false; + } + + ovs_mutex_lock(&rfm->map_lock); + old_refcount = ovs_refcount_unref(&node->refcount); + + if (old_refcount == 1) { + /* We transitioned 1->0 under lock. Safe to cleanup. */ + rfm->value_uninit(refmap_node_value(rfm, node)); + cmap_remove(&rfm->map, &node->map_node, node->hash); + ovs_mutex_unlock(&rfm->map_lock); + ovsrcu_postpone(free, node); + return true; + } + + ovs_mutex_unlock(&rfm->map_lock); + return false; +} + +void * +refmap_key_from_value(struct refmap *rfm, void *value) +{ + return refmap_node_key(refmap_node_from_value(rfm, value)); +} + +unsigned int +refmap_value_refcount_read(struct refmap *rfm, void *value) +{ + struct refmap_node *node; + + if (!value) { + return 0; + } + + node = refmap_node_from_value(rfm, value); + if (node) { + return ovs_refcount_read(&node->refcount); + } + + return 0; +} diff --git a/lib/refmap.h b/lib/refmap.h new file mode 100644 index 000000000..4c99b763e --- /dev/null +++ b/lib/refmap.h @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * + * 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. + */ + +#ifndef REFMAP_H +#define REFMAP_H + +#include <config.h> + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +#include "cmap.h" + +#include "openvswitch/dynamic-string.h" + +/* + * Reference map + * ============= + * + * This key-value store acts like a regular concurrent hashmap, + * except that insertion takes a reference on the value if already + * present. + * The key provided must be fully initialized, including potential pad bytes. + * + * As the value creation is dependent on it being already present + * within the structure and the user cannot predict that, this structure + * requires definitions for value_init and value_uninit functions, + * that will be called only at creation (first reference taken) and + * destruction (last reference released). + * + * Example: + * 1. struct key key; + * 2. memset(&key, 0, sizeof key); + * 3. refmap_create() + * 4. value = refmap_ref(key); + * Since it's the first reference for <key>, value_init is called. + * 5. refmap_ref(key); + * This is not the first reference for <key>. Only ref-count is updated. + * 6. refmap_unref(value); + * This is not the last reference released. Only ref-count is updated. + * 7. refmap_unref(value); + * This is the last reference released. value_uninit is immediately + * called, while the value memory is freed after RCU grace period. + * + * Thread safety + * ============= + * + * MT-unsafe: + * * refmap_create + * * refmap_destroy + * + * MT-safe: + * * REFMAP_FOR_EACH + * * refmap_ref + * * refmap_try_ref + * * refmap_try_ref_value + * * refmap_unref + * + */ + +struct refmap; + +/* Called once on a newly created 'value', i.e. when the first + * reference is taken. */ +typedef int (*refmap_value_init)(void *value, void *arg); + +/* Called once on the last dereference to value. */ +typedef void (*refmap_value_uninit)(void *value); + +/* Format a (key, value) tuple in 's'. This is an optional (can be NULL) + * callback, used for debug log purposes. + */ +typedef struct ds *(*refmap_value_format)(struct ds *s, void *key, + void *value); + +/* Iterator context for REFMAP_FOR_EACH. */ +struct refmap_iter { + struct refmap *rfm; + struct cmap_cursor cursor; + void *prev_value; +}; + +/* Helper for REFMAP_FOR_EACH: advances to the next entry, holding a ref for + * the current value (released on the next call or when iteration ends). + * Returns true if VALUE and KEY were set, false when iteration is done. */ +bool refmap_iter_next(struct refmap_iter *, void **value, void **key); + +/* Iterates KEY/VALUE (void *) over REFMAP. A reference is held for each loop + * body and released after the body runs. If you break out of the loop, call + * refmap_unref(REFMAP, VALUE) on the current VALUE. */ +#define REFMAP_FOR_EACH(VALUE, KEY, REFMAP) \ + for (struct refmap_iter refmap_iter__ = { (REFMAP), {0}, NULL }; \ + refmap_iter_next(&refmap_iter__, &(VALUE), &(KEY)); \ + ) + +/* Allocate and return a map handle. + * + * The user must ensure the 'key' is fully initialized, including potential + * pad bytes. + */ +struct refmap *refmap_create(const char *name, + size_t key_size, + size_t value_size, + refmap_value_init, + refmap_value_uninit, + refmap_value_format); + +/* Frees the map memory. + * + * WARNING: The caller MUST ensure the map is empty before calling + * refmap_destroy(). If the map contains remaining elements, their + * values will NOT have value_uninit() called, which will leak any + * resources managed by those values (file descriptors, allocated + * memory, etc.). The node memory will be freed but resource cleanup + * will not occur. + * + * refmap_destroy() is MT-unsafe. It MUST NOT be called while any other thread + * might be accessing the refmap, even through MT-safe operations like + * REFMAP_FOR_EACH or refmap_try_ref(). + */ +void refmap_destroy(struct refmap *); + +/* The "ref" functions, including refmap_try_ref() take a reference for the + * value upon success. It's the user's responsibility to unref it. + */ +void *refmap_try_ref(struct refmap *, void *key); +void *refmap_ref(struct refmap *, void *key, void *arg); +bool refmap_try_ref_value(struct refmap *, void *value); + +void *refmap_key_from_value(struct refmap *, void *value); + +/* Return 'true' if it was the last 'value' dereference and + * 'value_uninit' has been called. */ +bool refmap_unref(struct refmap *, void *value); + +/* refmap_value_refcount_read() returns the node's ref-count (including the + * reference implied by refmap_ref()) at the moment of the read, but may no + * longer be by the time you receive the value. This makes it unsuitable for + * logic decisions and only useful for debug logging. + */ +unsigned int +refmap_value_refcount_read(struct refmap *, void *value); + +#endif /* REFMAP_H */ diff --git a/tests/automake.mk b/tests/automake.mk index 685d4d2de..c1ea5822e 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -503,6 +503,7 @@ tests_ovstest_SOURCES = \ tests/test-rcu.c \ tests/test-rculist.c \ tests/test-reconnect.c \ + tests/test-refmap.c \ tests/test-rstp.c \ tests/test-sflow.c \ tests/test-sha1.c \ diff --git a/tests/library.at b/tests/library.at index 449f15fd5..3662d488e 100644 --- a/tests/library.at +++ b/tests/library.at @@ -44,6 +44,11 @@ AT_CHECK([ovstest test-ccmap check 1], [0], [... ]) AT_CLEANUP +AT_SETUP([refmap]) +AT_KEYWORDS([refmap]) +AT_CHECK([ovstest test-refmap check], [0], []) +AT_CLEANUP + AT_SETUP([atomic operations]) AT_CHECK([ovstest test-atomic]) AT_CLEANUP diff --git a/tests/test-refmap.c b/tests/test-refmap.c new file mode 100644 index 000000000..0f2b5b4f9 --- /dev/null +++ b/tests/test-refmap.c @@ -0,0 +1,1107 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. + * All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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. + */ + +#include <config.h> + +#undef NDEBUG +#include <assert.h> +#include <errno.h> +#include <getopt.h> +#include <string.h> + +#include "ovs-atomic.h" +#include "ovs-numa.h" +#include "ovs-rcu.h" +#include "ovs-thread.h" +#include "ovstest.h" +#include "random.h" +#include "refmap.h" +#include "timeval.h" +#include "util.h" + +#include "openvswitch/util.h" +#include "openvswitch/vlog.h" + +#define N 100 + +static struct refmap_test_params { + unsigned int n_threads; + unsigned int n_ids; + int step_idx; + bool debug; + bool csv_format; +} params = { + .n_threads = 1, + .n_ids = N, + .debug = false, + .csv_format = false, +}; + +DEFINE_STATIC_PER_THREAD_DATA(unsigned int, thread_id, OVSTHREAD_ID_UNSET); + +static unsigned int +thread_id(void) +{ + static atomic_count next_id = ATOMIC_COUNT_INIT(0); + unsigned int id = *thread_id_get(); + + if (OVS_UNLIKELY(id == OVSTHREAD_ID_UNSET)) { + id = atomic_count_inc(&next_id); + *thread_id_get() = id; + } + + return id; +} + +struct key { + size_t idx; + bool b; + uint8_t pad[7]; +}; + +struct value { + uint32_t *hdl; +}; + +struct arg { + uint32_t *ptr; +}; + +static int +value_init(void *value_, void *arg_) +{ + struct value *value = value_; + struct arg *arg = arg_; + + /* Verify that we don't double-init value. */ + ovs_assert(!value->hdl); + + *arg->ptr = 1; + value->hdl = arg->ptr; + return 0; +} + +/* Counts value_init calls for check_double_value_init_concurrent. */ +static atomic_count double_init_count = ATOMIC_COUNT_INIT(0); + +static int +value_init_count_double_init(void *value_, void *arg_) +{ + atomic_count_inc(&double_init_count); + return value_init(value_, arg_); +} + +static void +value_uninit(void *value_) +{ + struct value *value = value_; + + /* Verify that we don't double-uninit value. */ + ovs_assert(value->hdl); + + *value->hdl = 2; + value->hdl = NULL; +} + +struct value_init_fail_ctx { + bool should_fail; + uint32_t *ptr; +}; + +static int +value_init_maybe_fail(void *value_, void *arg_) +{ + struct value_init_fail_ctx *ctx = arg_; + struct value *value = value_; + + if (ctx->should_fail) { + return -1; + } + + ovs_assert(!value->hdl); + *ctx->ptr = 1; + value->hdl = ctx->ptr; + return 0; +} + +/* Lifecycle for check_value_init_uninit_order: a new value_init for the same + * key must not run until the previous value_uninit has fully completed. */ +enum { + VALUE_LIFECYCLE_IDLE = 0, + VALUE_LIFECYCLE_LIVE = 1, + VALUE_LIFECYCLE_TEARDOWN = 2, +}; + +static atomic_uint value_lifecycle_state; + +static int +tear_down_order_value_init(void *value_, void *arg_) +{ + struct value *value = value_; + struct arg *arg = arg_; + unsigned int state; + + atomic_read(&value_lifecycle_state, &state); + ovs_assert(state == VALUE_LIFECYCLE_IDLE); + + ovs_assert(!value->hdl); + *arg->ptr = 1; + value->hdl = arg->ptr; + atomic_store(&value_lifecycle_state, VALUE_LIFECYCLE_LIVE); + return 0; +} + +static void +tear_down_order_value_uninit(void *value_) +{ + struct value *value = value_; + unsigned int state; + + atomic_read(&value_lifecycle_state, &state); + ovs_assert(state == VALUE_LIFECYCLE_LIVE); + atomic_store(&value_lifecycle_state, VALUE_LIFECYCLE_TEARDOWN); + + /* Widen the race window so a buggy refmap could run replacement + * value_init before this value_uninit finishes. */ + xnanosleep(10 * 1000); + + ovs_assert(value->hdl); + *value->hdl = 2; + value->hdl = NULL; + atomic_store(&value_lifecycle_state, VALUE_LIFECYCLE_IDLE); +} + +struct check_refmap_ctx { + struct value **values; + int count; +}; + +static void +check_refmap(struct refmap *rfm, struct value **values, int n_expected) +{ + struct check_refmap_ctx ctx = { + .values = values, + .count = 0, + }; + void *key, *value; + + REFMAP_FOR_EACH (value, key, rfm) { + struct key *k = key; + + ovs_assert(k->idx < N); + if (ctx.values) { + ovs_assert(ctx.values[k->idx] == value); + } + + ctx.count++; + } + ovs_assert(ctx.count == n_expected); +} + +struct iter_modify_ctx { + struct refmap *rfm; + struct value **extra_refs; + int ref_count; + int unref_count; +}; + +struct try_ref_race_ctx { + struct refmap *rfm; + struct key key; + atomic_bool stop; +}; + +static void * +try_ref_racer(void *arg) +{ + struct try_ref_race_ctx *ctx = arg; + + for (;;) { + void *iter_key, *iter_value; + bool stop_; + + atomic_read(&ctx->stop, &stop_); + if (stop_) { + break; + } + + void *value = refmap_try_ref(ctx->rfm, &ctx->key); + if (value) { + struct value *v = value; + + ovs_assert(v->hdl); + refmap_unref(ctx->rfm, value); + } + + REFMAP_FOR_EACH (iter_value, iter_key, ctx->rfm) { + struct value *v = iter_value; + + ovs_assert(v->hdl); + } + } + + return NULL; +} + +/* Stress-test that try_ref rejects entries at ref-count 1 (the internal + * reference used during value init/uninit synchronization). + * + * Thread A repeatedly creates and destroys the same entry. + * Thread B continuously calls try_ref and for_each. */ +static void +check_try_ref_race(void) +{ + struct try_ref_race_ctx race_ctx; + uint32_t arg_val = 0; + struct refmap *rfm; + pthread_t worker; + struct arg arg = { .ptr = &arg_val }; + + rfm = refmap_create("try-ref-race", sizeof(struct key), + sizeof(struct value), value_init, value_uninit, NULL); + ovs_assert(rfm); + + memset(&race_ctx.key, 0, sizeof race_ctx.key); + race_ctx.key.idx = 0; + race_ctx.rfm = rfm; + atomic_init(&race_ctx.stop, false); + + worker = ovs_thread_create("try-ref-racer", try_ref_racer, &race_ctx); + + for (int i = 0; i < 10000; i++) { + void *value; + + value = refmap_ref(rfm, &race_ctx.key, &arg); + refmap_unref(rfm, value); + } + + atomic_store(&race_ctx.stop, true); + xpthread_join(worker, NULL); + + refmap_destroy(rfm); +} + +/* value_init failure: refmap_ref returns NULL, no entry is inserted, no + * value_uninit; a later successful ref on the same key still works. */ +static void +check_value_init_fail(void) +{ + struct value_init_fail_ctx ctx; + uint32_t status = 0; + struct value *value; + struct refmap *rfm; + struct key key; + + memset(&key, 0, sizeof key); + ctx = (struct value_init_fail_ctx) { + .should_fail = true, + .ptr = &status, + }; + + rfm = refmap_create("value-init-fail", sizeof key, sizeof(struct value), + value_init_maybe_fail, value_uninit, NULL); + ovs_assert(rfm); + + ovs_assert(!refmap_ref(rfm, &key, &ctx)); + ovs_assert(status == 0); + ovs_assert(!refmap_try_ref(rfm, &key)); + check_refmap(rfm, NULL, 0); + + ctx.should_fail = false; + value = refmap_ref(rfm, &key, &ctx); + ovs_assert(value); + ovs_assert(status == 1); + ovs_assert(value->hdl == &status); + check_refmap(rfm, (struct value **) &value, 1); + + refmap_unref(rfm, value); + ovs_assert(status == 2); + ovs_assert(!refmap_try_ref(rfm, &key)); + check_refmap(rfm, NULL, 0); + + refmap_destroy(rfm); +} + +/* If an object for a key is going down and another thread tries to ref the + * same key, a broken implementation could insert a replacement and run + * value_init before value_uninit on the old object has finished. + * value_lifecycle_state and tear_down_order_{value_init,value_uninit} detect + * that overlap. + */ +static void +check_value_init_uninit_order(void) +{ + struct try_ref_race_ctx race_ctx; + uint32_t arg_val = 0; + struct refmap *rfm; + unsigned int state; + pthread_t worker; + struct arg arg = { .ptr = &arg_val }; + + atomic_init(&value_lifecycle_state, VALUE_LIFECYCLE_IDLE); + + rfm = refmap_create("init-uninit-order", sizeof(struct key), + sizeof(struct value), tear_down_order_value_init, + tear_down_order_value_uninit, NULL); + ovs_assert(rfm); + + memset(&race_ctx.key, 0, sizeof race_ctx.key); + race_ctx.key.idx = 0; + race_ctx.rfm = rfm; + atomic_init(&race_ctx.stop, false); + + worker = ovs_thread_create("init-uninit-order", try_ref_racer, &race_ctx); + + for (int i = 0; i < 10000; i++) { + void *value; + + arg_val = 0; + value = refmap_ref(rfm, &race_ctx.key, &arg); + refmap_unref(rfm, value); + } + + atomic_store(&race_ctx.stop, true); + xpthread_join(worker, NULL); + + check_refmap(rfm, NULL, 0); + atomic_read(&value_lifecycle_state, &state); + ovs_assert(state == VALUE_LIFECYCLE_IDLE); + + refmap_destroy(rfm); +} + +/* Concurrent refmap_ref on the same key must run value_init exactly once. + * Without a second lookup under map_lock, two threads could both allocate + * after refmap_try_ref__ returns NULL (another thread may insert before + * refmap_ref takes map_lock for allocation or try_ref_rcu). */ +#define DOUBLE_INIT_THREADS 16 + +struct double_init_worker_ctx { + struct ovs_barrier *barrier_start; + struct ovs_barrier *barrier_after_ref; + struct refmap *rfm; + struct key *key; + struct arg *arg; +}; + +static void * +double_init_worker(void *aux) +{ + struct double_init_worker_ctx *ctx = aux; + void *value; + + /* All threads call refmap_ref together; no refmap_unref until everyone + * has a ref. */ + ovs_barrier_block(ctx->barrier_start); + value = refmap_ref(ctx->rfm, ctx->key, ctx->arg); + ovs_assert(value); + ovs_barrier_block(ctx->barrier_after_ref); + refmap_unref(ctx->rfm, value); + return NULL; +} + +static void +check_double_value_init_concurrent(void) +{ + pthread_t threads[DOUBLE_INIT_THREADS]; + struct ovs_barrier barrier_after_ref; + struct double_init_worker_ctx ctx; + struct ovs_barrier barrier_start; + uint32_t arg_val = 0; + struct refmap *rfm; + struct key key; + struct arg arg = { .ptr = &arg_val }; + + memset(&key, 0, sizeof key); + atomic_count_init(&double_init_count, 0); + + rfm = refmap_create("double-init", sizeof(struct key), + sizeof(struct value), value_init_count_double_init, + value_uninit, NULL); + ovs_assert(rfm); + + ovs_barrier_init(&barrier_start, DOUBLE_INIT_THREADS); + ovs_barrier_init(&barrier_after_ref, DOUBLE_INIT_THREADS); + ctx.barrier_start = &barrier_start; + ctx.barrier_after_ref = &barrier_after_ref; + ctx.rfm = rfm; + ctx.key = &key; + ctx.arg = &arg; + + for (int i = 0; i < DOUBLE_INIT_THREADS; i++) { + threads[i] = ovs_thread_create("double-init", double_init_worker, + &ctx); + } + + for (int i = 0; i < DOUBLE_INIT_THREADS; i++) { + xpthread_join(threads[i], NULL); + } + + ovs_barrier_destroy(&barrier_start); + ovs_barrier_destroy(&barrier_after_ref); + + ovs_assert(atomic_count_get(&double_init_count) == 1); + /* value_init sets arg to 1; value_uninit sets it to 2 when the entry is + * torn down. */ + ovs_assert(arg_val == 2); + check_refmap(rfm, NULL, 0); + + refmap_destroy(rfm); +} + +static void +run_check(struct ovs_cmdl_context *ctx OVS_UNUSED) +{ + struct iter_modify_ctx im_ctx; + struct value *extra_refs[N]; + struct value *values[N]; + struct key keys[N]; + struct refmap *rfm; + uint32_t args[N]; + void *key, *value; + + rfm = refmap_create("check-rfm", sizeof(struct key), sizeof(struct value), + value_init, value_uninit, NULL); + ovs_assert(rfm); + + check_refmap(rfm, NULL, 0); + + memset(keys, 0, sizeof keys); + for (int i = 0; i < N; i++) { + struct arg arg = { + .ptr = &args[i], + }; + struct value *v; + + keys[i].idx = i; + args[i] = i; + ovs_assert(!refmap_try_ref(rfm, &keys[i])); + v = refmap_ref(rfm, &keys[i], &arg); + ovs_assert(v); + ovs_assert(v == refmap_ref(rfm, &keys[i], &arg)); + refmap_unref(rfm, v); + ovs_assert(v == refmap_try_ref(rfm, &keys[i])); + refmap_unref(rfm, v); + values[i] = v; + } + check_refmap(rfm, (struct value **) values, N); + + for (int i = 0; i < N; i++) { + /* Verify that value_init is properly called. */ + ovs_assert(values[i]->hdl == &args[i]); + ovs_assert(args[i] == 1); + } + + /* Verify refmap_value_refcount_read: each value has one user ref. */ + for (int i = 0; i < N; i++) { + ovs_assert(refmap_value_refcount_read(rfm, values[i]) == 1); + } + ovs_assert(refmap_value_refcount_read(rfm, NULL) == 0); + + /* Verify refmap_key_from_value. */ + for (int i = 0; i < N; i++) { + struct key *k = refmap_key_from_value(rfm, values[i]); + ovs_assert(k->idx == keys[i].idx); + } + + /* Verify refmap_try_ref_value and ref-count changes. */ + for (int i = 0; i < N; i++) { + ovs_assert(refmap_try_ref_value(rfm, values[i])); + ovs_assert(refmap_value_refcount_read(rfm, values[i]) == 2); + refmap_unref(rfm, values[i]); + ovs_assert(refmap_value_refcount_read(rfm, values[i]) == 1); + } + ovs_assert(!refmap_try_ref_value(rfm, NULL)); + check_refmap(rfm, (struct value **) values, N); + + /* Take extra refs from within REFMAP_FOR_EACH. */ + memset(&im_ctx, 0, sizeof im_ctx); + im_ctx.rfm = rfm; + im_ctx.extra_refs = (struct value **) extra_refs; + memset(extra_refs, 0, sizeof extra_refs); + REFMAP_FOR_EACH (value, key, rfm) { + struct key *k = key; + + ovs_assert(refmap_try_ref_value(im_ctx.rfm, value)); + im_ctx.extra_refs[k->idx] = value; + im_ctx.ref_count++; + } + ovs_assert(im_ctx.ref_count == N); + + for (int i = 0; i < N; i++) { + ovs_assert(extra_refs[i] == values[i]); + ovs_assert(refmap_value_refcount_read(rfm, values[i]) == 2); + } + check_refmap(rfm, (struct value **) values, N); + + /* Drop extra refs from within REFMAP_FOR_EACH. */ + memset(&im_ctx, 0, sizeof im_ctx); + im_ctx.rfm = rfm; + REFMAP_FOR_EACH (value, key, rfm) { + refmap_unref(im_ctx.rfm, value); + im_ctx.unref_count++; + } + ovs_assert(im_ctx.unref_count == N); + + for (int i = 0; i < N; i++) { + ovs_assert(refmap_value_refcount_read(rfm, values[i]) == 1); + } + check_refmap(rfm, (struct value **) values, N); + + for (int i = 0; i < N; i++) { + refmap_unref(rfm, values[i]); + } + + for (int i = 0; i < N; i++) { + ovs_assert(!refmap_try_ref(rfm, &keys[i])); + } + + for (int i = 0; i < N; i++) { + /* Verify that value_uninit is executed. */ + ovs_assert(args[i] == 2); + } + check_refmap(rfm, NULL, 0); + + refmap_destroy(rfm); + + check_double_value_init_concurrent(); + check_value_init_uninit_order(); + check_try_ref_race(); + check_value_init_fail(); +} + +static uint32_t *ids; +static void **values; +static atomic_uint *thread_working_ms; /* Measured work time. */ + +static struct ovs_barrier barrier_outer; +static struct ovs_barrier barrier_inner; + +static atomic_uint running_time_ms; +static atomic_bool stop; + +static unsigned int +elapsed(unsigned int start) +{ + unsigned int running_time_ms_; + + atomic_read(&running_time_ms, &running_time_ms_); + + return running_time_ms_ - start; +} + +static void * +clock_main(void *arg OVS_UNUSED) +{ + struct timeval start; + struct timeval end; + + xgettimeofday(&start); + for (;;) { + bool stop_; + + atomic_read(&stop, &stop_); + if (stop_) { + break; + } + + xgettimeofday(&end); + atomic_store(&running_time_ms, + timeval_to_msec(&end) - timeval_to_msec(&start)); + xnanosleep(10 * 1000); + } + + return NULL; +} + +enum step_id { + STEP_NONE, + STEP_ALLOC, + STEP_REF, + STEP_UNREF, + STEP_FREE, + STEP_MIXED, + STEP_POS_QUERY, + STEP_NEG_QUERY, +}; + +static const char *step_names[] = { + [STEP_NONE] = "<bug>", + [STEP_ALLOC] = "alloc", + [STEP_REF] = "ref", + [STEP_UNREF] = "unref", + [STEP_FREE] = "free", + [STEP_MIXED] = "mixed", + [STEP_POS_QUERY] = "pos-query", + [STEP_NEG_QUERY] = "neg-query", +}; + +#define MAX_N_STEP 10 + +#define FOREACH_STEP(STEP_VAR, SCHEDULE) \ + for (int __idx = 0, STEP_VAR = (SCHEDULE)[__idx]; \ + (STEP_VAR = (SCHEDULE)[__idx]) != STEP_NONE; \ + __idx++) + +struct test_case { + int idx; + enum step_id schedule[MAX_N_STEP]; +}; + +static void +print_header(void) +{ + if (params.csv_format) { + return; + } + + printf("Benchmarking n=%u on %u thread%s.\n", + params.n_ids, params.n_threads, + params.n_threads > 1 ? "s" : ""); + + printf(" step\\thread: "); + printf(" Avg"); + for (size_t i = 0; i < params.n_threads; i++) { + printf(" %3" PRIuSIZE, i + 1); + } + + printf("\n"); +} + +static void +print_test_header(struct test_case *test) +{ + if (params.csv_format) { + return; + } + + printf("[%d]---------------------------", test->idx); + for (size_t i = 0; i < params.n_threads; i++) { + printf("-------"); + } + + printf("\n"); +} + +static void +print_test_result(struct test_case *test, enum step_id step, int step_idx) +{ + char test_name[50]; + uint32_t *twm; + uint32_t avg; + size_t i; + + twm = xcalloc(params.n_threads, sizeof *twm); + for (i = 0; i < params.n_threads; i++) { + atomic_read(&thread_working_ms[i], &twm[i]); + } + + avg = 0; + for (i = 0; i < params.n_threads; i++) { + avg += twm[i]; + } + + ovs_assert(params.n_threads); + avg /= params.n_threads; + + snprintf(test_name, sizeof test_name, "%d.%d-%s", + test->idx, step_idx, + step_names[step]); + if (params.csv_format) { + printf("%s,%" PRIu32, test_name, avg); + } else { + printf("%*s: ", 18, test_name); + printf(" %6" PRIu32, avg); + for (i = 0; i < params.n_threads; i++) { + printf(" %6" PRIu32, twm[i]); + } + printf(" ms"); + } + + printf("\n"); + + free(twm); +} + +static struct test_case test_cases[] = { + { + .schedule = { + STEP_ALLOC, + STEP_FREE, + }, + }, + { + .schedule = { + STEP_ALLOC, + STEP_REF, + STEP_UNREF, + STEP_FREE, + }, + }, + { + .schedule = { + STEP_MIXED, + STEP_FREE, + }, + }, + { + .schedule = { + STEP_ALLOC, + STEP_POS_QUERY, + /* Test negative query with map full. */ + STEP_NEG_QUERY, + STEP_FREE, + /* Test negative query with map empty. */ + STEP_NEG_QUERY, + }, + }, +}; + +static void +swap_ptr(void **a, void **b) +{ + void *t; + t = *a; + *a = *b; + *b = t; +} + +struct aux { + struct test_case test; + struct refmap *rfm; +}; + +static void * +benchmark_thread_worker(void *aux_) +{ + unsigned int tid = thread_id(); + unsigned int n_ids_per_thread; + unsigned int start_idx; + struct aux *aux = aux_; + struct refmap *rfm; + unsigned int start; + uint32_t *th_ids; + void **th_privs; + void *value; + size_t i; + + n_ids_per_thread = params.n_ids / params.n_threads; + start_idx = tid * n_ids_per_thread; + th_privs = &values[start_idx]; + th_ids = &ids[start_idx]; + + for (;;) { + bool stop_; + + ovs_barrier_block(&barrier_outer); + atomic_read(&stop, &stop_); + if (stop_) { + break; + } + + /* Wait for main thread to finish initializing + * rfm and step schedule. */ + ovs_barrier_block(&barrier_inner); + rfm = aux->rfm; + + FOREACH_STEP(step, aux->test.schedule) { + ovs_barrier_block(&barrier_inner); + atomic_read(&running_time_ms, &start); + switch (step) { + case STEP_ALLOC: + case STEP_REF: + for (i = 0; i < n_ids_per_thread; i++) { + struct key key = { + .idx = start_idx + i, + }; + struct arg arg = { + .ptr = &th_ids[i], + }; + + th_privs[i] = refmap_ref(rfm, &key, &arg); + } + break; + case STEP_POS_QUERY: + for (i = 0; i < n_ids_per_thread; i++) { + struct key key = { + .idx = start_idx + i, + }; + value = refmap_try_ref(rfm, &key); + refmap_unref(rfm, value); + } + break; + case STEP_NEG_QUERY: + for (i = 0; i < n_ids_per_thread; i++) { + struct key key = { + .idx = params.n_ids + 1, + }; + value = refmap_try_ref(rfm, &key); + refmap_unref(rfm, value); + } + break; + case STEP_UNREF: + case STEP_FREE: + for (i = 0; i < n_ids_per_thread; i++) { + refmap_unref(rfm, th_privs[i]); + } + break; + case STEP_MIXED: + for (i = 0; i < n_ids_per_thread; i++) { + struct arg arg; + struct key key; + int shuffled; + + /* Mixed mode is doing: + * 1. Alloc. + * 2. Shuffle two elements. + * 3. Delete shuffled element. + * 4. Alloc again. + * The loop ends with all elements allocated. + */ + + memset(&key, 0, sizeof key); + key.idx = start_idx + i; + shuffled = random_range(i + 1); + + arg.ptr = &th_ids[i]; + th_privs[i] = refmap_ref(rfm, &key, &arg); + swap_ptr(&th_privs[i], &th_privs[shuffled]); + refmap_unref(rfm, th_privs[i]); + arg.ptr = &th_ids[i]; + th_privs[i] = refmap_ref(rfm, &key, &arg); + } + break; + default: + fprintf(stderr, "[%u]: Reached step %d\n", + tid, step); + OVS_NOT_REACHED(); + break; + } + atomic_store(&thread_working_ms[tid], elapsed(start)); + ovs_barrier_block(&barrier_inner); + /* Main thread prints result now. */ + } + } + + return NULL; +} + +static void +benchmark_thread_main(struct aux *aux) +{ + int step_idx; + + memset(ids, 0, params.n_ids * sizeof *ids); + memset(values, 0, params.n_ids * sizeof *values); + + aux->rfm = refmap_create("benchmark-rfm", sizeof(struct key), + sizeof(struct value), value_init, value_uninit, + NULL); + ovs_assert(aux->rfm); + + print_test_header(&aux->test); + ovs_barrier_block(&barrier_inner); + /* Init is done, worker can start preparing to work. */ + step_idx = 0; + FOREACH_STEP(step, aux->test.schedule) { + ovs_barrier_block(&barrier_inner); + /* Workers do the scheduled work now. */ + ovs_barrier_block(&barrier_inner); + print_test_result(&aux->test, step, step_idx++); + } + + refmap_destroy(aux->rfm); +} + +static bool +parse_benchmark_params(int argc, char *argv[]) +{ + long int l_threads = 0; + long int l_ids = 0; + bool valid = true; + long int l; + int i; + + params.step_idx = -1; + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "benchmark") || + !strcmp(argv[i], "debug")) { + continue; + } else if (!strcmp(argv[i], "csv")) { + params.csv_format = true; + } else if (!strncmp(argv[i], "step=", 5)) { + if (!str_to_long(&argv[i][5], 10, &l)) { + fprintf(stderr, + "Invalid parameter '%s', expected positive integer.\n", + argv[i]); + valid = false; + goto out; + } + + params.step_idx = l; + } else { + if (!str_to_long(argv[i], 10, &l)) { + fprintf(stderr, + "Invalid parameter '%s', expected positive integer.\n", + argv[i]); + valid = false; + goto out; + } + + if (l_ids == 0) { + l_ids = l; + } else if (l_threads == 0) { + l_threads = l; + } else { + fprintf(stderr, + "Invalid parameter '%s', too many integer values.\n", + argv[i]); + valid = false; + goto out; + } + } + } + + if (l_ids != 0) { + params.n_ids = l_ids; + } else { + fprintf(stderr, "Invalid parameters: no number of elements given.\n"); + valid = false; + } + + if (l_threads != 0) { + params.n_threads = l_threads; + } else { + fprintf(stderr, "Invalid parameters: no number of threads given.\n"); + valid = false; + } + +out: + return valid; +} + +static void +run_benchmark(struct ovs_cmdl_context *ctx) +{ + pthread_t *threads; + pthread_t clock; + struct aux aux; + size_t i; + + if (!parse_benchmark_params(ctx->argc, ctx->argv)) { + return; + } + + ids = xcalloc(params.n_ids, sizeof *ids); + values = xcalloc(params.n_ids, sizeof *values); + thread_working_ms = xcalloc(params.n_threads, + sizeof *thread_working_ms); + for (i = 0; i < params.n_threads; i++) { + atomic_init(&thread_working_ms[i], 0); + } + + atomic_init(&stop, false); + + clock = ovs_thread_create("clock", clock_main, NULL); + + ovsrcu_quiesce_start(); + ovs_barrier_init(&barrier_outer, params.n_threads + 1); + ovs_barrier_init(&barrier_inner, params.n_threads + 1); + threads = xmalloc(params.n_threads * sizeof *threads); + for (i = 0; i < params.n_threads; i++) { + threads[i] = ovs_thread_create("worker", + benchmark_thread_worker, &aux); + } + + print_header(); + for (i = 0; i < ARRAY_SIZE(test_cases); i++) { + test_cases[i].idx = i; + if (params.step_idx != -1 && + params.step_idx != i) { + continue; + } + /* If we don't block workers from progressing now, + * there would be a race for access to aux.test, + * leading to some workers not respecting the schedule. + */ + ovs_barrier_block(&barrier_outer); + memcpy(&aux.test, &test_cases[i], sizeof aux.test); + benchmark_thread_main(&aux); + } + + atomic_store(&stop, true); + ovs_barrier_block(&barrier_outer); + + for (i = 0; i < params.n_threads; i++) { + xpthread_join(threads[i], NULL); + } + + free(threads); + + ovs_barrier_destroy(&barrier_outer); + ovs_barrier_destroy(&barrier_inner); + free(ids); + free(values); + free(thread_working_ms); + xpthread_join(clock, NULL); +} + +static const struct ovs_cmdl_command commands[] = { + {"check", "[debug]", 0, 1, run_check, OVS_RO}, + {"benchmark", "<nb elem> <nb threads> [step=<uint>] [csv]", 0, 4, + run_benchmark, OVS_RO}, + {NULL, NULL, 0, 0, NULL, OVS_RO}, +}; + +static void +parse_test_params(int argc, char *argv[]) +{ + int i; + + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], "debug")) { + params.debug = true; + } + } +} + +static void +refmap_test_main(int argc, char *argv[]) +{ + struct ovs_cmdl_context ctx = { + .argc = argc - optind, + .argv = argv + optind, + }; + + parse_test_params(argc - optind, argv + optind); + + vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_OFF); + if (params.debug) { + vlog_set_levels_from_string_assert("refmap:console:dbg"); + } + + /* Quiesce to start the RCU. */ + ovsrcu_quiesce(); + + set_program_name(argv[0]); + ovs_cmdl_run_command(&ctx, commands); + + ovsrcu_exit(); +} + +OVSTEST_REGISTER("test-refmap", refmap_test_main); diff --git a/utilities/checkpatch_dict.txt b/utilities/checkpatch_dict.txt index c1f43e5af..5ad599c1d 100644 --- a/utilities/checkpatch_dict.txt +++ b/utilities/checkpatch_dict.txt @@ -107,6 +107,7 @@ icmp icmp4 icmpv6 idl +ie ifdef ifindex initializer @@ -232,6 +233,7 @@ rebased recirc recirculation recirculations +refmap revalidate revalidation revalidator -- 2.34.1 _______________________________________________ dev mailing list [email protected] https://mail.openvswitch.org/mailman/listinfo/ovs-dev
