From patchwork Fri Jan 29 13:37:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Ivanov X-Patchwork-Id: 1433319 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=hemlock.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DRz1L5p4hz9sSC for ; Sat, 30 Jan 2021 00:37:58 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id 3A55D8746A; Fri, 29 Jan 2021 13:37:57 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VtwHJyh-gXV9; Fri, 29 Jan 2021 13:37:55 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id E901D87338; Fri, 29 Jan 2021 13:37:54 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id CB8B5C08A1; Fri, 29 Jan 2021 13:37:54 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 009E2C013A for ; Fri, 29 Jan 2021 13:37:54 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id E031087004 for ; Fri, 29 Jan 2021 13:37:53 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Z3KZuWjDVp-t for ; Fri, 29 Jan 2021 13:37:51 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.kot-begemot.co.uk (ivanoab7.miniserver.com [37.128.132.42]) by whitealder.osuosl.org (Postfix) with ESMTPS id 7A13186FFC for ; Fri, 29 Jan 2021 13:37:51 +0000 (UTC) Received: from tun252.jain.kot-begemot.co.uk ([192.168.18.6] helo=jain.kot-begemot.co.uk) by www.kot-begemot.co.uk with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l5Ty8-0006qE-Lx; Fri, 29 Jan 2021 13:37:49 +0000 Received: from jain.kot-begemot.co.uk ([192.168.3.3]) by jain.kot-begemot.co.uk with esmtp (Exim 4.92) (envelope-from ) id 1l5Ty3-00075k-MP; Fri, 29 Jan 2021 13:37:46 +0000 From: anton.ivanov@cambridgegreys.com To: ovs-dev@openvswitch.org Date: Fri, 29 Jan 2021 13:37:40 +0000 Message-Id: <20210129133741.27211-1-anton.ivanov@cambridgegreys.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett Cc: dceara@redhat.com, Anton Ivanov Subject: [ovs-dev] [PATCH 1/2] Add support for parallel processing of hashes X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Anton Ivanov Adds functionality needed to walk a hash in parallel where thread ID N out of a pool sized M is responsible for processing all elements in buckets N, N+M, N+M*2, etc Signed-off-by: Anton Ivanov --- include/openvswitch/hmap.h | 2 + lib/automake.mk | 2 + lib/parallel-hmap.c | 410 +++++++++++++++++++++++++++++++++++++ lib/parallel-hmap.h | 238 +++++++++++++++++++++ 4 files changed, 652 insertions(+) create mode 100644 lib/parallel-hmap.c create mode 100644 lib/parallel-hmap.h diff --git a/include/openvswitch/hmap.h b/include/openvswitch/hmap.h index 4e001cc69..6aed568e1 100644 --- a/include/openvswitch/hmap.h +++ b/include/openvswitch/hmap.h @@ -17,6 +17,8 @@ #ifndef HMAP_H #define HMAP_H 1 +#define OVS_HAS_PARALLEL_HMAP 1 + #include #include #include "openvswitch/util.h" diff --git a/lib/automake.mk b/lib/automake.mk index 39afbff9d..99f7bb8d4 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -122,6 +122,8 @@ lib_libopenvswitch_la_SOURCES = \ lib/dynamic-string.c \ lib/entropy.c \ lib/entropy.h \ + lib/parallel-hmap.h \ + lib/parallel-hmap.c \ lib/fat-rwlock.c \ lib/fat-rwlock.h \ lib/fatal-signal.c \ diff --git a/lib/parallel-hmap.c b/lib/parallel-hmap.c new file mode 100644 index 000000000..a923a6142 --- /dev/null +++ b/lib/parallel-hmap.c @@ -0,0 +1,410 @@ +/* + * Copyright (c) 2020 Red Hat, Inc. + * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2015, 2019 Nicira, Inc. + * + * 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 +#include +#include +#include +#include +#include +#include +#include "fatal-signal.h" +#include "util.h" +#include "openvswitch/vlog.h" +#include "openvswitch/hmap.h" +#include "openvswitch/thread.h" +#include "hmapx.h" +#include "ovs-atomic.h" +#include "ovs-thread.h" +#include "ovs-numa.h" +#include "random.h" +#include "parallel-hmap.h" + +VLOG_DEFINE_THIS_MODULE(parallel_hmap); + +#define WORKER_SEM_NAME "%x-%p-%x" +#define MAIN_SEM_NAME "%x-%p-main" + + +/* These are accessed under mutex inside add_worker_pool(). + * They do not need to be atomic. + */ + +static atomic_bool initial_pool_setup = ATOMIC_VAR_INIT(false); +static bool can_parallelize = false; + +/* This is set only in the process of exit and the set is + * accompanied by a fence. It does not need to be atomic or be + * accessed under a lock. + */ + +static bool workers_must_exit = false; + +static struct ovs_list worker_pools = OVS_LIST_INITIALIZER(&worker_pools); + +static struct ovs_mutex init_mutex = OVS_MUTEX_INITIALIZER; + +static int pool_size; + +static int sembase; + +static void worker_pool_hook(void *aux OVS_UNUSED) { + int i; + static struct worker_pool *pool; + char sem_name[256]; + + workers_must_exit = true; + + /* All workers must honour the must_exit flag and check for it regularly. + * We can make it atomic and check it via atomics in workers, but that + * is not really necessary as it is set just once - when the program + * terminates. So we use a fence which is invoked before exiting instead. + */ + atomic_thread_fence(memory_order_acq_rel); + + /* Wake up the workers after the must_exit flag has been set */ + + LIST_FOR_EACH (pool, list_node, &worker_pools) { + for (i = 0; i < pool->size ; i++) { + sem_post(pool->controls[i].fire); + } + for (i = 0; i < pool->size ; i++) { + sem_close(pool->controls[i].fire); + sprintf(sem_name, WORKER_SEM_NAME, sembase, pool, i); + sem_unlink(sem_name); + } + sem_close(pool->done); + sprintf(sem_name, MAIN_SEM_NAME, sembase, pool); + sem_unlink(sem_name); + } +} + +static void setup_worker_pools(void) { + int cores, nodes; + + nodes = ovs_numa_get_n_numas(); + if (nodes == OVS_NUMA_UNSPEC || nodes <= 0) { + nodes = 1; + } + cores = ovs_numa_get_n_cores(); + + /* If there is no NUMA config, use 4 cores. + * If there is NUMA config use half the cores on + * one node so that the OS does not start pushing + * threads to other nodes. + */ + if (cores == OVS_CORE_UNSPEC || cores <= 0) { + /* If there is no NUMA we can try the ovs-threads routine. + * It falls back to sysconf and/or affinity mask. + */ + cores = count_cpu_cores(); + pool_size = cores; + } else { + pool_size = cores / nodes; + } + if (pool_size > 16) { + pool_size = 16; + } + can_parallelize = (pool_size >= 3); + fatal_signal_add_hook(worker_pool_hook, NULL, NULL, true); + sembase = random_uint32(); +} + +bool stop_parallel_processing(void) +{ + return workers_must_exit; +} + +bool can_parallelize_hashes(void) +{ + bool test = false; + + if (atomic_compare_exchange_strong( + &initial_pool_setup, + &test, + true)) { + ovs_mutex_lock(&init_mutex); + setup_worker_pools(); + ovs_mutex_unlock(&init_mutex); + } + return can_parallelize; +} + +struct worker_pool *add_worker_pool(void *(*start)(void *)){ + + struct worker_pool *new_pool = NULL; + struct worker_control *new_control; + bool test = false; + int i; + char sem_name[256]; + + + if (atomic_compare_exchange_strong( + &initial_pool_setup, + &test, + true)) { + ovs_mutex_lock(&init_mutex); + setup_worker_pools(); + ovs_mutex_unlock(&init_mutex); + } + + ovs_mutex_lock(&init_mutex); + if (can_parallelize) { + new_pool = xmalloc(sizeof(struct worker_pool)); + new_pool->size = pool_size; + sprintf(sem_name, MAIN_SEM_NAME, sembase, new_pool); + new_pool->done = sem_open(sem_name, O_CREAT, S_IRWXU, 0); + + ovs_list_push_back(&worker_pools, &new_pool->list_node); + + new_pool->controls = + xmalloc(sizeof(struct worker_control) * new_pool->size); + + for (i = 0; i < new_pool->size; i++) { + new_control = &new_pool->controls[i]; + sprintf(sem_name, WORKER_SEM_NAME, sembase, new_pool, i); + new_control->fire = sem_open(sem_name, O_CREAT, S_IRWXU, 0); + new_control->id = i; + new_control->done = new_pool->done; + new_control->data = NULL; + ovs_mutex_init(&new_control->mutex); + new_control->finished = ATOMIC_VAR_INIT(false); + } + + for (i = 0; i < pool_size; i++) { + ovs_thread_create("worker pool helper", start, &new_pool->controls[i]); + } + } + ovs_mutex_unlock(&init_mutex); + return new_pool; +} + + +/* Initializes 'hmap' as an empty hash table with mask N. */ +void +fast_hmap_init(struct hmap *hmap, ssize_t mask) +{ + size_t i; + + hmap->buckets = xmalloc(sizeof (struct hmap_node *) * (mask + 1)); + hmap->one = NULL; + hmap->mask = mask; + hmap->n = 0; + for (i = 0; i <= hmap->mask; i++) { + hmap->buckets[i] = NULL; + } +} + +/* Initializes 'hmap' as an empty hash table of size X. + * Intended for use in parallel processing so that all + * fragments used to store results in a parallel job + * are the same size. + */ +void +fast_hmap_size_for(struct hmap *hmap, int size) +{ + size_t mask; + mask = size / 2; + mask |= mask >> 1; + mask |= mask >> 2; + mask |= mask >> 4; + mask |= mask >> 8; + mask |= mask >> 16; +#if SIZE_MAX > UINT32_MAX + mask |= mask >> 32; +#endif + + /* If we need to dynamically allocate buckets we might as well allocate at + * least 4 of them. */ + mask |= (mask & 1) << 1; + + fast_hmap_init(hmap, mask); +} + +/* Run a thread pool which uses a callback function to process results + */ + +void run_pool_callback(struct worker_pool *pool, + void *fin_result, void *result_frags, + void (*helper_func)(struct worker_pool *pool, + void *fin_result, + void *result_frags, int index)) +{ + int index, completed; + + /* Ensure that all worker threads see the same data as the + * main thread. + */ + + atomic_thread_fence(memory_order_acq_rel); + + /* Start workers */ + + for (index = 0; index < pool->size; index++) { + sem_post(pool->controls[index].fire); + } + + completed = 0; + + do { + bool test; + /* Note - we do not loop on semaphore until it reaches + * zero, but on pool size/remaining workers. + * This is by design. If the inner loop can handle + * completion for more than one worker within an iteration + * it will do so to ensure no additional iterations and + * waits once all of them are done. + * + * This may result in us having an initial positive value + * of the semaphore when the pool is invoked the next time. + * This is harmless - the loop will spin up a couple of times + * doing nothing while the workers are processing their data + * slices. + */ + sem_wait(pool->done); + for (index = 0; index < pool->size; index++) { + test = true; + /* If the worker has marked its data chunk as complete, + * invoke the helper function to combine the results of + * this worker into the main result. + * + * The worker must invoke an appropriate memory fence + * (most likely acq_rel) to ensure that the main thread + * sees all of the results produced by the worker. + */ + if (atomic_compare_exchange_weak( + &pool->controls[index].finished, + &test, + false)) { + if (helper_func) { + (helper_func)(pool, fin_result, result_frags, index); + } + completed++; + pool->controls[index].data = NULL; + } + } + } while (completed < pool->size); +} + +/* Run a thread pool - basic, does not do results processing. + */ + +void run_pool(struct worker_pool *pool) +{ + run_pool_callback(pool, NULL, NULL, NULL); +} + +/* Brute force merge of a hashmap into another hashmap. + * Intended for use in parallel processing. The destination + * hashmap MUST be the same size as the one being merged. + * + * This can be achieved by pre-allocating them to correct size + * and using hmap_insert_fast() instead of hmap_insert() + */ + +void fast_hmap_merge(struct hmap *dest, struct hmap *inc) +{ + size_t i; + + ovs_assert(inc->mask == dest->mask); + + if (!inc->n) { + /* Request to merge an empty frag, nothing to do */ + return; + } + + for (i = 0; i <= dest->mask; i++) { + struct hmap_node **dest_bucket = &dest->buckets[i]; + struct hmap_node **inc_bucket = &inc->buckets[i]; + if (*inc_bucket != NULL) { + struct hmap_node *last_node = *inc_bucket; + while (last_node->next != NULL) { + last_node = last_node->next; + } + last_node->next = *dest_bucket; + *dest_bucket = *inc_bucket; + *inc_bucket = NULL; + } + } + dest->n += inc->n; + inc->n = 0; +} + +/* Run a thread pool which gathers results in an array + * of hashes. Merge results. + */ + +static void merge_hash_results(struct worker_pool *pool OVS_UNUSED, + void *fin_result, void *result_frags, + int index) +{ + struct hmap *result = (struct hmap *)fin_result; + struct hmap *res_frags = (struct hmap *)result_frags; + + fast_hmap_merge(result, &res_frags[index]); + hmap_destroy(&res_frags[index]); +} + + +void run_pool_hash( + struct worker_pool *pool, + struct hmap *result, + struct hmap *result_frags) +{ + run_pool_callback(pool, result, result_frags, merge_hash_results); +} + +/* Run a thread pool which gathers results in an array of lists. + * Merge results. + */ + +static void merge_list_results(struct worker_pool *pool OVS_UNUSED, + void *fin_result, void *result_frags, + int index) +{ + struct ovs_list *result = (struct ovs_list *)fin_result; + struct ovs_list *res_frags = (struct ovs_list *)result_frags; + + if (!ovs_list_is_empty(&res_frags[index])) { + ovs_list_splice(result->next, + ovs_list_front(&res_frags[index]), &res_frags[index]); + } +} + + +void run_pool_list( + struct worker_pool *pool, + struct ovs_list *result, + struct ovs_list *result_frags) +{ + run_pool_callback(pool, result, result_frags, merge_list_results); +} + +void update_hashrow_locks(struct hmap *lflows, struct hashrow_locks *hrl) +{ + int i; + if (hrl->mask != lflows->mask) { + if (hrl->row_locks) { + free(hrl->row_locks); + } + hrl->row_locks = xcalloc(sizeof(struct ovs_mutex), lflows->mask + 1); + hrl->mask = lflows->mask; + for (i = 0; i <= lflows->mask; i++) { + ovs_mutex_init(&hrl->row_locks[i]); + } + } +} diff --git a/lib/parallel-hmap.h b/lib/parallel-hmap.h new file mode 100644 index 000000000..771bf583a --- /dev/null +++ b/lib/parallel-hmap.h @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2020 Red Hat, Inc. + * + * 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 OVS_PARALLEL_HMAP +#define OVS_PARALLEL_HMAP 1 + +/* if the parallel macros are defined by hmap.h or any other ovs define + * we skip over the ovn specific definitions. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include "openvswitch/util.h" +#include "openvswitch/hmap.h" +#include "openvswitch/thread.h" +#include "ovs-atomic.h" + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wthread-safety" +#endif +/* A version of the HMAP_FOR_EACH macro intended for iterating as part + * of parallel processing. + * Each worker thread has a different ThreadID in the range of 0..POOL_SIZE + * and will iterate hash buckets ThreadID, ThreadID + step, + * ThreadID + step * 2, etc. The actual macro accepts + * ThreadID + step * i as the JOBID parameter. + */ + +#define HMAP_FOR_EACH_IN_PARALLEL(NODE, MEMBER, JOBID, HMAP) \ + for (INIT_CONTAINER(NODE, hmap_first_in_bucket_num(HMAP, JOBID), MEMBER); \ + (NODE != OBJECT_CONTAINING(NULL, NODE, MEMBER)) \ + || ((NODE = NULL), false); \ + ASSIGN_CONTAINER(NODE, hmap_next_in_bucket(&(NODE)->MEMBER), MEMBER)) + +/* We do not have a SAFE version of the macro, because the hash size is not + * atomic and hash removal operations would need to be wrapped with + * locks. This will defeat most of the benefits from doing anything in + * parallel. + * If the code block inside FOR_EACH_IN_PARALLEL needs to remove elements, + * each thread should store them in a temporary list result instead, merging + * the lists into a combined result at the end */ + +/* Work "Handle" */ + +struct worker_control { + int id; /* Used as a modulo when iterating over a hash. */ + atomic_bool finished; /* Set to true after achunk of work is complete. */ + sem_t *fire; /* Work start semaphore - sem_post starts the worker. */ + sem_t *done; /* Work completion semaphore - sem_post on completion. */ + struct ovs_mutex mutex; /* Guards the data. */ + void *data; /* Pointer to data to be processed. */ + void *workload; /* back-pointer to the worker pool structure. */ +}; + +struct worker_pool { + int size; /* Number of threads in the pool. */ + struct ovs_list list_node; /* List of pools - used in cleanup/exit. */ + struct worker_control *controls; /* "Handles" in this pool. */ + sem_t *done; /* Work completion semaphorew. */ +}; + +/* Add a worker pool for thread function start() which expects a pointer to + * a worker_control structure as an argument. */ + +struct worker_pool *add_worker_pool(void *(*start)(void *)); + +/* Setting this to true will make all processing threads exit */ + +bool stop_parallel_processing(void); + +/* Build a hmap pre-sized for size elements */ + +void fast_hmap_size_for(struct hmap *hmap, int size); + +/* Build a hmap with a mask equals to size */ + +void fast_hmap_init(struct hmap *hmap, ssize_t size); + +/* Brute-force merge a hmap into hmap. + * Dest and inc have to have the same mask. The merge is performed + * by extending the element list for bucket N in the dest hmap with the list + * from bucket N in inc. + */ + +void fast_hmap_merge(struct hmap *dest, struct hmap *inc); + +/* Run a pool, without any default processing of results. + */ + +void run_pool(struct worker_pool *pool); + +/* Run a pool, merge results from hash frags into a final hash result. + * The hash frags must be pre-sized to the same size. + */ + +void run_pool_hash(struct worker_pool *pool, + struct hmap *result, struct hmap *result_frags); + +/* Run a pool, merge results from list frags into a final list result. + */ + +void run_pool_list(struct worker_pool *pool, + struct ovs_list *result, struct ovs_list *result_frags); + +/* Run a pool, call a callback function to perform processing of results. + */ + +void run_pool_callback(struct worker_pool *pool, void *fin_result, + void *result_frags, + void (*helper_func)(struct worker_pool *pool, + void *fin_result, void *result_frags, + int index)); + + +/* Returns the first node in 'hmap' in the bucket in which the given 'hash' + * would land, or a null pointer if that bucket is empty. */ + +static inline struct hmap_node * +hmap_first_in_bucket_num(const struct hmap *hmap, size_t num) +{ + return hmap->buckets[num]; +} + +static inline struct hmap_node * +parallel_hmap_next__(const struct hmap *hmap, size_t start, size_t pool_size) +{ + size_t i; + for (i = start; i <= hmap->mask; i+= pool_size) { + struct hmap_node *node = hmap->buckets[i]; + if (node) { + return node; + } + } + return NULL; +} + +/* Returns the first node in 'hmap', as expected by thread with job_id + * for parallel processing in arbitrary order, or a null pointer if + * the slice of 'hmap' for that job_id is empty. */ +static inline struct hmap_node * +parallel_hmap_first(const struct hmap *hmap, size_t job_id, size_t pool_size) +{ + return parallel_hmap_next__(hmap, job_id, pool_size); +} + +/* Returns the next node in the slice of 'hmap' following 'node', + * in arbitrary order, or a * null pointer if 'node' is the last node in + * the 'hmap' slice. + * + */ +static inline struct hmap_node * +parallel_hmap_next(const struct hmap *hmap, + const struct hmap_node *node, ssize_t pool_size) +{ + return (node->next + ? node->next + : parallel_hmap_next__(hmap, + (node->hash & hmap->mask) + pool_size, pool_size)); +} + +static inline void post_completed_work(struct worker_control *control) +{ + atomic_thread_fence(memory_order_acq_rel); + atomic_store_relaxed(&control->finished, true); + sem_post(control->done); +} + +static inline void wait_for_work(struct worker_control *control) +{ + sem_wait(control->fire); +} + +/* Hash per-row locking support - to be used only in conjunction + * with fast hash inserts. Normal hash inserts may resize the hash + * rendering the locking invalid. + */ + +struct hashrow_locks { + ssize_t mask; + struct ovs_mutex *row_locks; +}; + +/* Update an hash row locks structure to match the current hash size */ + +void update_hashrow_locks(struct hmap *lflows, struct hashrow_locks *hrl); + +/* Lock a hash row */ + +static inline void lock_hash_row(struct hashrow_locks *hrl, uint32_t hash) +{ + ovs_mutex_lock(&hrl->row_locks[hash % hrl->mask]); +} + +/* Unlock a hash row */ + +static inline void unlock_hash_row(struct hashrow_locks *hrl, uint32_t hash) +{ + ovs_mutex_unlock(&hrl->row_locks[hash % hrl->mask]); +} +/* Init the row locks structure */ + +static inline void init_hash_row_locks(struct hashrow_locks *hrl) +{ + hrl->mask = 0; + hrl->row_locks = NULL; +} + +bool can_parallelize_hashes(void); + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#ifdef __cplusplus +} +#endif + + +#endif /* lib/ovs-fasthmap.h */ From patchwork Fri Jan 29 13:37:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Ivanov X-Patchwork-Id: 1433320 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=hemlock.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DRz1R1b3dz9sSC for ; Sat, 30 Jan 2021 00:38:03 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id CD279874B8; Fri, 29 Jan 2021 13:38:01 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id VRF0sRYGClsb; Fri, 29 Jan 2021 13:37:58 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id 1FF3187340; Fri, 29 Jan 2021 13:37:58 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 153D0C08A1; Fri, 29 Jan 2021 13:37:58 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 27CC0C013A for ; Fri, 29 Jan 2021 13:37:57 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 15E7422E20 for ; Fri, 29 Jan 2021 13:37:57 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id v2P39Lzo+e0f for ; Fri, 29 Jan 2021 13:37:53 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.kot-begemot.co.uk (ivanoab7.miniserver.com [37.128.132.42]) by silver.osuosl.org (Postfix) with ESMTPS id E2AD920390 for ; Fri, 29 Jan 2021 13:37:52 +0000 (UTC) Received: from tun252.jain.kot-begemot.co.uk ([192.168.18.6] helo=jain.kot-begemot.co.uk) by www.kot-begemot.co.uk with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l5TyA-0006qJ-CF; Fri, 29 Jan 2021 13:37:51 +0000 Received: from jain.kot-begemot.co.uk ([192.168.3.3]) by jain.kot-begemot.co.uk with esmtp (Exim 4.92) (envelope-from ) id 1l5Ty6-00075k-1F; Fri, 29 Jan 2021 13:37:48 +0000 From: anton.ivanov@cambridgegreys.com To: ovs-dev@openvswitch.org Date: Fri, 29 Jan 2021 13:37:41 +0000 Message-Id: <20210129133741.27211-2-anton.ivanov@cambridgegreys.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210129133741.27211-1-anton.ivanov@cambridgegreys.com> References: <20210129133741.27211-1-anton.ivanov@cambridgegreys.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett Cc: dceara@redhat.com, Anton Ivanov Subject: [ovs-dev] [PATCH 2/2] ovsdb: Run monitor processing in parallel X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Anton Ivanov Monitor processing performs a full walk of all rows in all tables referenced in a monitor. The rows are internally represented as a hash map. This operation can be run in parallel where thread M out N is walking hash buckets M, N+M, etc. Running inter-thread IPC for only a handful of datum values is not likely to be efficient, hence there is a cut-off. Tables smaller than the cut-off value are run in the main thread. Larger tables are run via multiple threads. The cut-off value of 8 has been chosen based on experimentation on machines with between 4 and 12 cores. Signed-off-by: Anton Ivanov --- lib/ovsdb-idl.c | 30 ++++ lib/ovsdb-idl.h | 6 + ovsdb/monitor.c | 387 ++++++++++++++++++++++++++++++++++++++++---- ovsdb/ovsdb-idlc.in | 26 +++ 4 files changed, 414 insertions(+), 35 deletions(-) diff --git a/lib/ovsdb-idl.c b/lib/ovsdb-idl.c index 2c8a0c9cf..a9aaa7d66 100644 --- a/lib/ovsdb-idl.c +++ b/lib/ovsdb-idl.c @@ -46,6 +46,7 @@ #include "svec.h" #include "util.h" #include "uuid.h" +#include "parallel-hmap.h" #include "openvswitch/vlog.h" VLOG_DEFINE_THIS_MODULE(ovsdb_idl); @@ -2298,6 +2299,24 @@ ovsdb_idl_first_row(const struct ovsdb_idl *idl, return next_real_row(table, hmap_first(&table->rows)); } +/* Returns a row in 'table_class''s table slice in 'idl', or a null pointer + * if that table slice is empty. + * + * Database tables are internally maintained as hash tables, so adding or + * removing rows while traversing the same table can cause some rows to be + * visited twice or not at apply. */ +const struct ovsdb_idl_row * +parallel_ovsdb_idl_first_row(const struct ovsdb_idl *idl, + const struct ovsdb_idl_table_class *table_class, + ssize_t job_id, + ssize_t pool_size) +{ + struct ovsdb_idl_table *table = ovsdb_idl_table_from_class(idl, + table_class); + return next_real_row( + table, parallel_hmap_first(&table->rows, job_id, pool_size)); +} + /* Returns a row following 'row' within its table, or a null pointer if 'row' * is the last row in its table. */ const struct ovsdb_idl_row * @@ -2308,6 +2327,17 @@ ovsdb_idl_next_row(const struct ovsdb_idl_row *row) return next_real_row(table, hmap_next(&table->rows, &row->hmap_node)); } +/* Returns a row following 'row' within its table slice, or a null pointer + * if 'row' is the last row in its table slice. */ +const struct ovsdb_idl_row * +parallel_ovsdb_idl_next_row(const struct ovsdb_idl_row *row, ssize_t pool_size) +{ + struct ovsdb_idl_table *table = row->table; + + return next_real_row(table, + parallel_hmap_next(&table->rows, &row->hmap_node, pool_size)); +} + /* Reads and returns the value of 'column' within 'row'. If an ongoing * transaction has changed 'column''s value, the modified value is returned. * diff --git a/lib/ovsdb-idl.h b/lib/ovsdb-idl.h index 05bb48d66..dcc1ad531 100644 --- a/lib/ovsdb-idl.h +++ b/lib/ovsdb-idl.h @@ -234,6 +234,12 @@ const struct ovsdb_idl_row *ovsdb_idl_first_row( const struct ovsdb_idl *, const struct ovsdb_idl_table_class *); const struct ovsdb_idl_row *ovsdb_idl_next_row(const struct ovsdb_idl_row *); +const struct ovsdb_idl_row *parallel_ovsdb_idl_first_row( + const struct ovsdb_idl *, const struct ovsdb_idl_table_class *, + ssize_t job_id, ssize_t pool); +const struct ovsdb_idl_row *parallel_ovsdb_idl_next_row( + const struct ovsdb_idl_row *, ssize_t pool); + const struct ovsdb_datum *ovsdb_idl_read(const struct ovsdb_idl_row *, const struct ovsdb_idl_column *); const struct ovsdb_datum *ovsdb_idl_get(const struct ovsdb_idl_row *, diff --git a/ovsdb/monitor.c b/ovsdb/monitor.c index 532dedcb6..2bde7772a 100644 --- a/ovsdb/monitor.c +++ b/ovsdb/monitor.c @@ -36,6 +36,7 @@ #include "jsonrpc-server.h" #include "monitor.h" #include "util.h" +#include "parallel-hmap.h" #include "openvswitch/vlog.h" VLOG_DEFINE_THIS_MODULE(ovsdb_monitor); @@ -1084,7 +1085,6 @@ ovsdb_monitor_add_json_row(struct json **json, const char *table_name, const struct uuid *row_uuid) { char uuid[UUID_LEN + 1]; - /* Create JSON object for transaction overall. */ if (!*json) { *json = json_object_create(); @@ -1101,6 +1101,142 @@ ovsdb_monitor_add_json_row(struct json **json, const char *table_name, json_object_put(*table_json, uuid, row_json); } +/* We cannot reuse the same multi-threaded conventions as for the cond + * update (further down) as the calling conventions and data supplied + * to update generation are quite different + * + * In addition to this, key parts of the code are dependent on container + * macros making it impossible to reuse processing code between the two. + * + * The only part which we can reuse is json result processing. + */ + +/* The "cut-off" point determined by using debug timings seems to be + * somewhere ~ processing hashesh with 4-8 elements. That is surprisingly + * low. JSON serialization looks like the primary suspect. If that improves + * this should be increased accordingly. + */ + +#define PARALLEL_CUT_OFF_A 8 + +struct mon_json_result { + struct ovs_list list_node; + const char *table_name; + const struct uuid *row_uuid; + struct json *row_json; +}; + +struct monitor_compose_update_info { + struct ovsdb_monitor_change_set_for_table *mcst; + const struct ovsdb_monitor_session_condition *condition; + unsigned long int *changed; + struct ovs_list *results; + compose_row_update_cb_func row_update; + bool initial; +}; + +struct monitor_compose_update_pool { + void (*row_helper_func)(struct ovsdb_monitor_row *row, + struct monitor_compose_update_info *mci); + struct worker_pool *pool; +}; + + +static void ovsdb_monitor_compose_update_process_one_row ( + struct ovsdb_monitor_row *row, + struct monitor_compose_update_info *mci) +{ + struct json *row_json; + struct mon_json_result *temp; + + row_json = (*mci->row_update)(mci->mcst->mt, mci->condition, OVSDB_MONITOR_ROW, + row, mci->initial, mci->changed, + mci->mcst->n_columns); + if (row_json) { + temp = xmalloc(sizeof(struct mon_json_result)); + temp->table_name = mci->mcst->mt->table->schema->name; + temp->row_uuid = &row->uuid; + temp->row_json = row_json; + ovs_list_push_back(mci->results, &temp->list_node); + } +} + + +static void *monitor_compose_update_thread(void *arg) +{ + struct worker_control *control = (struct worker_control *) arg; + struct monitor_compose_update_pool *workload; + struct monitor_compose_update_info *mci; + struct ovsdb_monitor_row *row; + struct hmap *table_rows; + int bnum; + + + while (!stop_parallel_processing()) { + wait_for_work(control); + if (stop_parallel_processing()) { + return NULL; + } + workload = (struct monitor_compose_update_pool *) control->workload; + mci = (struct monitor_compose_update_info *) control->data; + if (mci && workload) { + table_rows = (struct hmap *) &mci->mcst->rows; + for (bnum = control->id; + bnum <= table_rows->mask; + bnum += workload->pool->size) + { + HMAP_FOR_EACH_IN_PARALLEL (row, hmap_node, bnum, table_rows) { + if (stop_parallel_processing()) { + return NULL; + } + (workload->row_helper_func)(row, control->data); + } + } + } + post_completed_work(control); + } + return NULL; +} + + +static void ovsdb_monitor_compose_cond_change_update_generate_json( + struct json **json, struct json **table_json, + struct ovs_list *temp_result) +{ + struct mon_json_result * temp; + + LIST_FOR_EACH_POP (temp, list_node, temp_result) { + ovsdb_monitor_add_json_row(json, + temp->table_name, + table_json, + temp->row_json, + temp->row_uuid); + free(temp); + } +} + + +static struct monitor_compose_update_pool *monitor_compose_pool = NULL; + +static void init_compose_pool(void) { + + int index; + + if (can_parallelize_hashes() && (!monitor_compose_pool)) { + monitor_compose_pool = + xmalloc(sizeof(*monitor_compose_pool)); + monitor_compose_pool->pool = + add_worker_pool(monitor_compose_update_thread); + monitor_compose_pool->row_helper_func = + ovsdb_monitor_compose_update_process_one_row; + + for (index = 0; index < monitor_compose_pool->pool->size; index++) { + monitor_compose_pool->pool->controls[index].workload = + monitor_compose_pool; + } + } +} + /* Constructs and returns JSON for a object (as described in * RFC 7047) for all the outstanding changes within 'monitor', starting from * 'transaction'. */ @@ -1113,31 +1249,169 @@ ovsdb_monitor_compose_update( { struct json *json; size_t max_columns = ovsdb_monitor_max_columns(dbmon); - unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns)); json = NULL; struct ovsdb_monitor_change_set_for_table *mcst; LIST_FOR_EACH (mcst, list_in_change_set, &mcs->change_set_for_tables) { struct ovsdb_monitor_row *row, *next; struct json *table_json = NULL; - struct ovsdb_monitor_table *mt = mcst->mt; - HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mcst->rows) { - struct json *row_json; - row_json = (*row_update)(mt, condition, OVSDB_MONITOR_ROW, row, - initial, changed, mcst->n_columns); - if (row_json) { - ovsdb_monitor_add_json_row(&json, mt->table->schema->name, - &table_json, row_json, - &row->uuid); + if (hmap_count(&mcst->rows) > PARALLEL_CUT_OFF_A && + can_parallelize_hashes()) { + + struct monitor_compose_update_info *mci; + struct ovs_list combined_result = OVS_LIST_INITIALIZER(&combined_result); + struct ovs_list *results = NULL; + int index; + + init_compose_pool(); + mci = xcalloc(sizeof(*mci), monitor_compose_pool->pool->size); + results = xcalloc(sizeof(*results), monitor_compose_pool->pool->size); + + for (index = 0; + index < monitor_compose_pool->pool->size; index++) { + + mci[index].mcst = mcst; + mci[index].condition = condition; + mci[index].initial = initial; + mci[index].changed = xmalloc(bitmap_n_bytes(max_columns)); + mci[index].row_update = row_update; + ovs_list_init(&results[index]); + mci[index].results = &results[index]; + monitor_compose_pool->pool->controls[index].data = &mci[index]; + } + + run_pool_list(monitor_compose_pool->pool, &combined_result, results); + + ovsdb_monitor_compose_cond_change_update_generate_json( + &json, &table_json, &combined_result); + + free(results); + for (index = 0; index < monitor_compose_pool->pool->size; index++) { + free(mci[index].changed); } + free(mci); + } else { + struct monitor_compose_update_info mci; + struct ovs_list results = OVS_LIST_INITIALIZER(&results); + mci.mcst = mcst; + mci.condition = condition; + mci.initial = initial; + mci.changed = xmalloc(bitmap_n_bytes(max_columns)); + mci.row_update = row_update; + mci.results = &results; + + HMAP_FOR_EACH_SAFE (row, next, hmap_node, &mcst->rows) { + ovsdb_monitor_compose_update_process_one_row( + row, &mci); + } + if (!ovs_list_is_empty(mci.results)) { + ovsdb_monitor_compose_cond_change_update_generate_json( + &json, &table_json, mci.results); + } + free(mci.changed); } } - free(changed); return json; } +#define PARALLEL_CUT_OFF_B 8 + +struct monitor_cond_change_info { + struct ovsdb_monitor_table *mt; + struct ovsdb_monitor_session_condition *condition; + unsigned long int *changed; + struct ovs_list *results; +}; + +struct monitor_cond_change_pool { + void (*row_helper_func)(struct ovsdb_row *row, + struct monitor_cond_change_info *mi); + struct worker_pool *pool; +}; + +static void *monitor_cond_change_thread(void *arg) { + struct worker_control *control = (struct worker_control *) arg; + struct monitor_cond_change_pool *workload; + struct monitor_cond_change_info *mi; + struct ovsdb_row *row; + struct hmap *table_rows; + int bnum; + + + while (!stop_parallel_processing()) { + wait_for_work(control); + if (stop_parallel_processing()) { + return NULL; + } + workload = (struct monitor_cond_change_pool *) control->workload; + mi = (struct monitor_cond_change_info *) control->data; + if (mi && workload) { + table_rows = (struct hmap *) &mi->mt->table->rows; + for (bnum = control->id; + bnum <= mi->mt->table->rows.mask; + bnum += workload->pool->size) + { + HMAP_FOR_EACH_IN_PARALLEL (row, hmap_node, bnum, table_rows) { + if (stop_parallel_processing()) { + return NULL; + } + (workload->row_helper_func)(row, control->data); + } + } + } + post_completed_work(control); + } + return NULL; +} + +static void ovsdb_monitor_cond_change_process_one_row ( + struct ovsdb_row *row, + struct monitor_cond_change_info *mi) +{ + struct json *row_json; + struct mon_json_result *temp; + + row_json = ovsdb_monitor_compose_row_update2( + mi->mt, + mi->condition, + OVSDB_ROW, + row, + false, + mi->changed, + mi->mt->n_columns); + if (row_json) { + temp = xmalloc(sizeof(struct mon_json_result)); + temp->table_name = mi->mt->table->schema->name; + temp->row_uuid = ovsdb_row_get_uuid(row); + temp->row_json = row_json; + ovs_list_push_back(mi->results, &temp->list_node); + } +} + +static struct monitor_cond_change_pool *monitor_cond_pool = NULL; + +static void init_cond_pool(void) { + + int index; + + if (can_parallelize_hashes() && (!monitor_cond_pool)) { + monitor_cond_pool = + xmalloc(sizeof(*monitor_cond_pool)); + monitor_cond_pool->pool = + add_worker_pool(monitor_cond_change_thread); + monitor_cond_pool->row_helper_func = + ovsdb_monitor_cond_change_process_one_row; + + for (index = 0; index < monitor_cond_pool->pool->size; index++) { + monitor_cond_pool->pool->controls[index].workload = + monitor_cond_pool; + } + } +} + + static struct json* ovsdb_monitor_compose_cond_change_update( struct ovsdb_monitor *dbmon, @@ -1146,40 +1420,83 @@ ovsdb_monitor_compose_cond_change_update( struct shash_node *node; struct json *json = NULL; size_t max_columns = ovsdb_monitor_max_columns(dbmon); - unsigned long int *changed = xmalloc(bitmap_n_bytes(max_columns)); + SHASH_FOR_EACH (node, &dbmon->tables) { struct ovsdb_monitor_table *mt = node->data; - struct ovsdb_row *row; struct json *table_json = NULL; struct ovsdb_condition *old_condition, *new_condition; - if (!ovsdb_monitor_get_table_conditions(mt, - condition, - &old_condition, - &new_condition) || - !ovsdb_condition_cmp_3way(old_condition, new_condition)) { - /* Nothing to update on this table */ - continue; - } + if (can_parallelize_hashes() && + (hmap_count(&mt->table->rows) > PARALLEL_CUT_OFF_B)) { + + struct monitor_cond_change_info *mi; + struct ovs_list combined_result = OVS_LIST_INITIALIZER(&combined_result); + struct ovs_list *results = NULL; + int index, count; + + init_cond_pool(); + mi = xcalloc(sizeof(*mi), monitor_cond_pool->pool->size); + results = xcalloc(sizeof(*results), monitor_cond_pool->pool->size); - /* Iterate over all rows in table */ - HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) { - struct json *row_json; - - row_json = ovsdb_monitor_compose_row_update2(mt, condition, - OVSDB_ROW, row, - false, changed, - mt->n_columns); - if (row_json) { - ovsdb_monitor_add_json_row(&json, mt->table->schema->name, - &table_json, row_json, - ovsdb_row_get_uuid(row)); + count = monitor_cond_pool->pool->size; + + for (index = 0; index < count; index++) { + mi[index].changed = xmalloc(bitmap_n_bytes(max_columns)); + mi[index].condition = condition; + mi[index].mt = mt; + ovs_list_init(&results[index]); + mi[index].results = &results[index]; + monitor_cond_pool->pool->controls[index].data = &mi[index]; + } + + if (!ovsdb_monitor_get_table_conditions(mt, + condition, + &old_condition, + &new_condition) || + !ovsdb_condition_cmp_3way(old_condition, new_condition)) { + /* Nothing to update on this table */ + goto cleanup_scratchpad; } + + run_pool_list( + monitor_cond_pool->pool, + &combined_result, + results); + ovsdb_monitor_compose_cond_change_update_generate_json( + &json, &table_json, &combined_result); +cleanup_scratchpad: + for (index = 0; index < count; index++) { + free(mi[index].changed); + } + free(results); + free(mi); + } else { + /* Iterate over all rows in table - single threaded */ + + struct ovsdb_row *row; + struct monitor_cond_change_info mi; + struct ovs_list results = OVS_LIST_INITIALIZER(&results); + + init_cond_pool(); + + mi.changed = xmalloc(bitmap_n_bytes(max_columns)); + mi.condition = condition; + mi.mt = mt; + mi.results = &results; + + HMAP_FOR_EACH (row, hmap_node, &mt->table->rows) { + ovsdb_monitor_cond_change_process_one_row(row, &mi); + } + if (!ovs_list_is_empty(mi.results)) { + ovsdb_monitor_compose_cond_change_update_generate_json( + &json, &table_json, mi.results); + } + free(mi.changed); } ovsdb_monitor_table_condition_updated(mt, condition); } - free(changed); + return json; } diff --git a/ovsdb/ovsdb-idlc.in b/ovsdb/ovsdb-idlc.in index 5914e0878..b3245b415 100755 --- a/ovsdb/ovsdb-idlc.in +++ b/ovsdb/ovsdb-idlc.in @@ -254,6 +254,8 @@ const struct %(s)s *%(s)s_get_for_uuid(const struct ovsdb_idl *, const struct uu const struct %(s)s *%(s)s_table_get_for_uuid(const struct %(s)s_table *, const struct uuid *); const struct %(s)s *%(s)s_first(const struct ovsdb_idl *); const struct %(s)s *%(s)s_next(const struct %(s)s *); +const struct %(s)s *%(s)s_parallel_first(const struct ovsdb_idl *, ssize_t job_id, ssize_t pool); +const struct %(s)s *%(s)s_parallel_next(const struct %(s)s *, ssize_t pool); #define %(S)s_FOR_EACH(ROW, IDL) \\ for ((ROW) = %(s)s_first(IDL); \\ (ROW); \\ @@ -262,6 +264,10 @@ const struct %(s)s *%(s)s_next(const struct %(s)s *); for ((ROW) = %(s)s_first(IDL); \\ (ROW) ? ((NEXT) = %(s)s_next(ROW), 1) : 0; \\ (ROW) = (NEXT)) +#define %(S)s_PARALLEL_FOR_EACH(ROW, IDL, JOBID, POOL) \\ + for ((ROW) = %(s)s_parallel_first(IDL, JOBID, POOL); \\ + (ROW); \\ + (ROW) = %(s)s_parallel_next(ROW, POOL)) unsigned int %(s)s_get_seqno(const struct ovsdb_idl *); unsigned int %(s)s_row_get_seqno(const struct %(s)s *row, enum ovsdb_idl_change change); @@ -708,6 +714,18 @@ const struct %(s)s * return %(s)s_cast(ovsdb_idl_first_row(idl, &%(p)stable_%(tl)s)); } +/* Returns a row in table "%(t)s" in 'idl', or a null pointer if that + * table is empty. + * + * Database tables are internally maintained as hash tables, so adding or + * removing rows while traversing the same table can cause some rows to be + * visited twice or not at apply. */ +const struct %(s)s * +%(s)s_parallel_first(const struct ovsdb_idl *idl, ssize_t job_id, ssize_t pool) +{ + return %(s)s_cast(parallel_ovsdb_idl_first_row(idl, &%(p)stable_%(tl)s, job_id, pool)); +} + /* Returns a row following 'row' within its table, or a null pointer if 'row' * is the last row in its table. */ const struct %(s)s * @@ -716,6 +734,14 @@ const struct %(s)s * return %(s)s_cast(ovsdb_idl_next_row(&row->header_)); } +/* Returns a row following 'row' within its table slice, or a null pointer if 'row' + * is the last row in its table slice. */ +const struct %(s)s * +%(s)s_parallel_next(const struct %(s)s *row, ssize_t pool) +{ + return %(s)s_cast(parallel_ovsdb_idl_next_row(&row->header_, pool)); +} + unsigned int %(s)s_get_seqno(const struct ovsdb_idl *idl) { return ovsdb_idl_table_get_seqno(idl, &%(p)stable_%(tl)s);