From b26473ff8f4a5d691869a8a35c2fa1e9840e82b9 Mon Sep 17 00:00:00 2001 From: Pablo de Lara Date: Sat, 11 Jul 2015 01:18:51 +0100 Subject: [PATCH] hash: add reset function Added reset function to be able to empty the table, without having to destroy and create it again. Signed-off-by: Pablo de Lara Acked-by: Bruce Richardson --- app/test/test_hash.c | 4 +--- app/test/test_hash_perf.c | 12 +++--------- lib/librte_hash/rte_cuckoo_hash.c | 21 +++++++++++++++++++++ lib/librte_hash/rte_hash.h | 11 +++++++++-- lib/librte_hash/rte_hash_version.map | 7 +++++++ 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/app/test/test_hash.c b/app/test/test_hash.c index e70d859a4d..448586c830 100644 --- a/app/test/test_hash.c +++ b/app/test/test_hash.c @@ -1132,9 +1132,7 @@ static int test_average_table_utilization(void) average_keys_added += added_keys; /* Reset the table */ - rte_hash_free(handle); - handle = rte_hash_create(&ut_params); - RETURN_IF_ERROR(handle == NULL, "hash creation failed"); + rte_hash_reset(handle); /* Print a dot to show progress on operations */ printf("."); diff --git a/app/test/test_hash_perf.c b/app/test/test_hash_perf.c index a54f3c1fd6..b01b040e85 100644 --- a/app/test/test_hash_perf.c +++ b/app/test/test_hash_perf.c @@ -382,14 +382,10 @@ free_table(unsigned table_index) rte_hash_free(h[table_index]); } -static int +static void reset_table(unsigned table_index) { - free_table(table_index); - if (create_table(table_index) != 0) - return -1; - - return 0; + rte_hash_reset(h[table_index]); } static int @@ -421,13 +417,11 @@ run_all_tbl_perf_tests(unsigned with_pushes) if (timed_deletes(with_hash, i) < 0) return -1; - if (reset_table(i) < 0) - return -1; + reset_table(i); /* Print a dot to show progress on operations */ printf("."); fflush(stdout); - } free_table(i); diff --git a/lib/librte_hash/rte_cuckoo_hash.c b/lib/librte_hash/rte_cuckoo_hash.c index 50e3acdff1..15c5da5e29 100644 --- a/lib/librte_hash/rte_cuckoo_hash.c +++ b/lib/librte_hash/rte_cuckoo_hash.c @@ -371,6 +371,27 @@ rte_hash_secondary_hash(const hash_sig_t primary_hash) return (primary_hash ^ ((tag + 1) * alt_bits_xor)); } +void +rte_hash_reset(struct rte_hash *h) +{ + void *ptr; + unsigned i; + + if (h == NULL) + return; + + memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket)); + memset(h->key_store, 0, h->key_entry_size * (h->entries + 1)); + + /* clear the free ring */ + while (rte_ring_dequeue(h->free_slots, &ptr) == 0) + rte_pause(); + + /* Repopulate the free slots ring. Entry zero is reserved for key misses */ + for (i = 1; i < h->entries + 1; i++) + rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i)); +} + /* Search for an entry that can be pushed to its alternative location */ static inline int make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt) diff --git a/lib/librte_hash/rte_hash.h b/lib/librte_hash/rte_hash.h index 936d170f0f..8bbc9f0fc5 100644 --- a/lib/librte_hash/rte_hash.h +++ b/lib/librte_hash/rte_hash.h @@ -128,8 +128,15 @@ void rte_hash_free(struct rte_hash *h); /** - * Add a key to an existing hash table. - * This operation is not multi-thread safe + * Reset all hash structure, by zeroing all entries + * @param h + * Hash table to reset + */ +void +rte_hash_reset(struct rte_hash *h); + +/** + * Add a key to an existing hash table. This operation is not multi-thread safe * and should only be called from one thread. * * @param h diff --git a/lib/librte_hash/rte_hash_version.map b/lib/librte_hash/rte_hash_version.map index 94a0fecc60..db8558ffa1 100644 --- a/lib/librte_hash/rte_hash_version.map +++ b/lib/librte_hash/rte_hash_version.map @@ -18,3 +18,10 @@ DPDK_2.0 { local: *; }; + +DPDK_2.1 { + global: + + rte_hash_reset; + +} DPDK_2.0; -- 2.20.1