aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/vppinfra/bihash_16_8.h2
-rw-r--r--src/vppinfra/bihash_24_8.h2
-rw-r--r--src/vppinfra/bihash_40_8.h2
-rw-r--r--src/vppinfra/bihash_48_8.h2
-rw-r--r--src/vppinfra/bihash_8_8.h2
-rw-r--r--src/vppinfra/bihash_template.c214
-rw-r--r--src/vppinfra/bihash_template.h220
-rw-r--r--src/vppinfra/bihash_vec8_8.h2
-rw-r--r--src/vppinfra/test_bihash_template.c142
9 files changed, 234 insertions, 354 deletions
diff --git a/src/vppinfra/bihash_16_8.h b/src/vppinfra/bihash_16_8.h
index 6076487934d..5949e2b3549 100644
--- a/src/vppinfra/bihash_16_8.h
+++ b/src/vppinfra/bihash_16_8.h
@@ -13,12 +13,10 @@
* limitations under the License.
*/
#undef BIHASH_TYPE
-#undef BIHASH_KVP_CACHE_SIZE
#undef BIHASH_KVP_PER_PAGE
#define BIHASH_TYPE _16_8
#define BIHASH_KVP_PER_PAGE 4
-#define BIHASH_KVP_CACHE_SIZE 0
#ifndef __included_bihash_16_8_h__
#define __included_bihash_16_8_h__
diff --git a/src/vppinfra/bihash_24_8.h b/src/vppinfra/bihash_24_8.h
index dcce2a21259..c028352d0e9 100644
--- a/src/vppinfra/bihash_24_8.h
+++ b/src/vppinfra/bihash_24_8.h
@@ -13,12 +13,10 @@
* limitations under the License.
*/
#undef BIHASH_TYPE
-#undef BIHASH_KVP_CACHE_SIZE
#undef BIHASH_KVP_PER_PAGE
#define BIHASH_TYPE _24_8
#define BIHASH_KVP_PER_PAGE 4
-#define BIHASH_KVP_CACHE_SIZE 0
#ifndef __included_bihash_24_8_h__
#define __included_bihash_24_8_h__
diff --git a/src/vppinfra/bihash_40_8.h b/src/vppinfra/bihash_40_8.h
index 90adb8f5b10..fe57ba03966 100644
--- a/src/vppinfra/bihash_40_8.h
+++ b/src/vppinfra/bihash_40_8.h
@@ -14,12 +14,10 @@
*/
#undef BIHASH_TYPE
-#undef BIHASH_KVP_CACHE_SIZE
#undef BIHASH_KVP_PER_PAGE
#define BIHASH_TYPE _40_8
#define BIHASH_KVP_PER_PAGE 4
-#define BIHASH_KVP_CACHE_SIZE 0
#ifndef __included_bihash_40_8_h__
#define __included_bihash_40_8_h__
diff --git a/src/vppinfra/bihash_48_8.h b/src/vppinfra/bihash_48_8.h
index e981950b1bd..9aebe66d264 100644
--- a/src/vppinfra/bihash_48_8.h
+++ b/src/vppinfra/bihash_48_8.h
@@ -14,12 +14,10 @@
*/
#undef BIHASH_TYPE
-#undef BIHASH_KVP_CACHE_SIZE
#undef BIHASH_KVP_PER_PAGE
#define BIHASH_TYPE _48_8
#define BIHASH_KVP_PER_PAGE 4
-#define BIHASH_KVP_CACHE_SIZE 0
#ifndef __included_bihash_48_8_h__
#define __included_bihash_48_8_h__
diff --git a/src/vppinfra/bihash_8_8.h b/src/vppinfra/bihash_8_8.h
index 2deb64ef438..604cf30808f 100644
--- a/src/vppinfra/bihash_8_8.h
+++ b/src/vppinfra/bihash_8_8.h
@@ -13,12 +13,10 @@
* limitations under the License.
*/
#undef BIHASH_TYPE
-#undef BIHASH_KVP_CACHE_SIZE
#undef BIHASH_KVP_PER_PAGE
#define BIHASH_TYPE _8_8
#define BIHASH_KVP_PER_PAGE 4
-#define BIHASH_KVP_CACHE_SIZE 0
#ifndef __included_bihash_8_8_h__
#define __included_bihash_8_8_h__
diff --git a/src/vppinfra/bihash_template.c b/src/vppinfra/bihash_template.c
index 53ffbf156e6..af624fee070 100644
--- a/src/vppinfra/bihash_template.c
+++ b/src/vppinfra/bihash_template.c
@@ -37,7 +37,6 @@ void BV (clib_bihash_init)
(BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
{
uword bucket_size;
- int i;
nbuckets = 1 << (max_log2 (nbuckets));
@@ -47,6 +46,14 @@ void BV (clib_bihash_init)
h->cache_hits = 0;
h->cache_misses = 0;
+ /*
+ * Make sure the requested size is rational. The max table
+ * size without playing the alignment card is 64 Gbytes.
+ * If someone starts complaining that's not enough, we can shift
+ * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
+ */
+ ASSERT (memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
+
h->alloc_arena = (uword) clib_mem_vm_alloc (memory_size);
h->alloc_arena_next = h->alloc_arena;
h->alloc_arena_size = memory_size;
@@ -54,11 +61,8 @@ void BV (clib_bihash_init)
bucket_size = nbuckets * sizeof (h->buckets[0]);
h->buckets = BV (alloc_aligned) (h, bucket_size);
- h->writer_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
- h->writer_lock[0] = 0;
-
- for (i = 0; i < nbuckets; i++)
- BV (clib_bihash_reset_cache) (h->buckets + i);
+ h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
+ h->alloc_lock[0] = 0;
h->fmt_fn = NULL;
}
@@ -83,7 +87,7 @@ BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
{
BVT (clib_bihash_value) * rv = 0;
- ASSERT (h->writer_lock[0]);
+ ASSERT (h->alloc_lock[0]);
if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
{
vec_validate_init_empty (h->freelists, log2_pages, 0);
@@ -108,10 +112,13 @@ static void
BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
u32 log2_pages)
{
- ASSERT (h->writer_lock[0]);
+ ASSERT (h->alloc_lock[0]);
ASSERT (vec_len (h->freelists) > log2_pages);
+ if (CLIB_DEBUG > 0)
+ memset (v, 0xFE, sizeof (*v) * (1 << log2_pages));
+
v->next_free = h->freelists[log2_pages];
h->freelists[log2_pages] = v;
}
@@ -125,6 +132,8 @@ BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
u32 thread_index = os_get_thread_index ();
int log2_working_copy_length;
+ ASSERT (h->alloc_lock[0]);
+
if (thread_index >= vec_len (h->working_copies))
{
vec_validate (h->working_copies, thread_index);
@@ -154,10 +163,6 @@ BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
h->working_copies[thread_index] = working_copy;
}
- /* Lock the bucket... */
- while (BV (clib_bihash_lock_bucket) (b) == 0)
- ;
-
v = BV (clib_bihash_get_value) (h, b->offset);
clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
@@ -178,6 +183,8 @@ BV (split_and_rehash)
BVT (clib_bihash_value) * new_values, *new_v;
int i, j, length_in_kvs;
+ ASSERT (h->alloc_lock[0]);
+
new_values = BV (value_alloc) (h, new_log2_pages);
length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
@@ -225,6 +232,8 @@ BV (split_and_rehash_linear)
BVT (clib_bihash_value) * new_values;
int i, j, new_length, old_length;
+ ASSERT (h->alloc_lock[0]);
+
new_values = BV (value_alloc) (h, new_log2_pages);
new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
@@ -266,7 +275,6 @@ int BV (clib_bihash_add_del)
u32 bucket_index;
BVT (clib_bihash_bucket) * b, tmp_b;
BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
- int rv = 0;
int i, limit;
u64 hash, new_hash;
u32 new_log2_pages, old_log2_pages;
@@ -281,37 +289,36 @@ int BV (clib_bihash_add_del)
hash >>= h->log2_nbuckets;
- tmp_b.linear_search = 0;
-
- while (__sync_lock_test_and_set (h->writer_lock, 1))
- ;
+ BV (clib_bihash_lock_bucket) (b);
/* First elt in the bucket? */
if (BV (clib_bihash_bucket_is_empty) (b))
{
if (is_add == 0)
{
- rv = -1;
- goto unlock;
+ BV (clib_bihash_unlock_bucket) (b);
+ return (-1);
}
+ BV (clib_bihash_alloc_lock) (h);
v = BV (value_alloc) (h, 0);
+ BV (clib_bihash_alloc_unlock) (h);
*v->kvp = *add_v;
- tmp_b.as_u64 = 0;
+ tmp_b.as_u64 = 0; /* clears bucket lock */
tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
tmp_b.refcnt = 1;
+ CLIB_MEMORY_BARRIER ();
b->as_u64 = tmp_b.as_u64;
- goto unlock;
+ BV (clib_bihash_unlock_bucket) (b);
+ return (0);
}
- /* Note: this leaves the cache disabled */
- BV (make_working_copy) (h, b);
-
- v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
-
+ /* WARNING: we're still looking at the live copy... */
limit = BIHASH_KVP_PER_PAGE;
+ v = BV (clib_bihash_get_value) (h, b->offset);
+
v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
if (b->linear_search)
limit <<= b->log2_pages;
@@ -319,6 +326,13 @@ int BV (clib_bihash_add_del)
if (is_add)
{
/*
+ * Because reader threads are looking at live data,
+ * we have to be extra careful. Readers do NOT hold the
+ * bucket lock. We need to be SLOWER than a search, past the
+ * point where readers CHECK the bucket lock.
+ */
+
+ /*
* For obvious (in hindsight) reasons, see if we're supposed to
* replace an existing key, then look for an empty slot.
*/
@@ -326,52 +340,79 @@ int BV (clib_bihash_add_del)
{
if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
{
+ CLIB_MEMORY_BARRIER (); /* Add a delay */
clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
- CLIB_MEMORY_BARRIER ();
- /* Restore the previous (k,v) pairs */
- b->as_u64 = h->saved_bucket.as_u64;
- goto unlock;
+ BV (clib_bihash_unlock_bucket) (b);
+ return (0);
}
}
+ /*
+ * Look for an empty slot. If found, use it
+ */
for (i = 0; i < limit; i++)
{
if (BV (clib_bihash_is_free) (&(v->kvp[i])))
{
- clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
- CLIB_MEMORY_BARRIER ();
- b->as_u64 = h->saved_bucket.as_u64;
+ /*
+ * Copy the value first, so that if a reader manages
+ * to match the new key, the value will be right...
+ */
+ clib_memcpy (&(v->kvp[i].value),
+ &add_v->value, sizeof (add_v->value));
+ CLIB_MEMORY_BARRIER (); /* Make sure the value has settled */
+ clib_memcpy (&(v->kvp[i]), &add_v->key, sizeof (add_v->key));
b->refcnt++;
- goto unlock;
+ BV (clib_bihash_unlock_bucket) (b);
+ return (0);
}
}
- /* no room at the inn... split case... */
+ /* Out of space in this bucket, split the bucket... */
}
- else
+ else /* delete case */
{
for (i = 0; i < limit; i++)
{
+ /* Found the key? Kill it... */
if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
{
memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
- CLIB_MEMORY_BARRIER ();
- if (PREDICT_TRUE (h->saved_bucket.refcnt > 1))
+ /* Is the bucket empty? */
+ if (PREDICT_TRUE (b->refcnt > 1))
{
- h->saved_bucket.refcnt -= 1;
- b->as_u64 = h->saved_bucket.as_u64;
- goto unlock;
+ b->refcnt--;
+ BV (clib_bihash_unlock_bucket) (b);
+ return (0);
}
- else
+ else /* yes, free it */
{
- tmp_b.as_u64 = 0;
- goto free_old_bucket;
+ /* Save old bucket value, need log2_pages to free it */
+ tmp_b.as_u64 = b->as_u64;
+ CLIB_MEMORY_BARRIER ();
+
+ /* Kill and unlock the bucket */
+ b->as_u64 = 0;
+
+ /* And free the backing storage */
+ BV (clib_bihash_alloc_lock) (h);
+ /* Note: v currently points into the middle of the bucket */
+ v = BV (clib_bihash_get_value) (h, tmp_b.offset);
+ BV (value_free) (h, v, tmp_b.log2_pages);
+ BV (clib_bihash_alloc_unlock) (h);
+ return (0);
}
}
}
- rv = -3;
- b->as_u64 = h->saved_bucket.as_u64;
- goto unlock;
+ /* Not found... */
+ BV (clib_bihash_unlock_bucket) (b);
+ return (-3);
}
+ /* Move readers to a (locked) temp copy of the bucket */
+ BV (clib_bihash_alloc_lock) (h);
+ BV (make_working_copy) (h, b);
+
+ v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
+
old_log2_pages = h->saved_bucket.log2_pages;
new_log2_pages = old_log2_pages + 1;
mark_bucket_linear = 0;
@@ -436,21 +477,11 @@ expand_ok:
tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
tmp_b.linear_search = mark_bucket_linear;
tmp_b.refcnt = h->saved_bucket.refcnt + 1;
-
-free_old_bucket:
-
+ tmp_b.lock = 0;
CLIB_MEMORY_BARRIER ();
b->as_u64 = tmp_b.as_u64;
- v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
-
- BV (value_free) (h, v, h->saved_bucket.log2_pages);
-
-unlock:
- BV (clib_bihash_reset_cache) (b);
- BV (clib_bihash_unlock_bucket) (b);
- CLIB_MEMORY_BARRIER ();
- h->writer_lock[0] = 0;
- return rv;
+ BV (clib_bihash_alloc_unlock) (h);
+ return (0);
}
int BV (clib_bihash_search)
@@ -460,9 +491,6 @@ int BV (clib_bihash_search)
u64 hash;
u32 bucket_index;
BVT (clib_bihash_value) * v;
-#if BIHASH_KVP_CACHE_SIZE > 0
- BVT (clib_bihash_kv) * kvp;
-#endif
BVT (clib_bihash_bucket) * b;
int i, limit;
@@ -476,23 +504,12 @@ int BV (clib_bihash_search)
if (BV (clib_bihash_bucket_is_empty) (b))
return -1;
-#if BIHASH_KVP_CACHE_SIZE > 0
- /* Check the cache, if currently enabled */
- if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
+ if (PREDICT_FALSE (b->lock))
{
- limit = BIHASH_KVP_CACHE_SIZE;
- kvp = b->cache;
- for (i = 0; i < limit; i++)
- {
- if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
- {
- *valuep = kvp[i];
- h->cache_hits++;
- return 0;
- }
- }
+ volatile BVT (clib_bihash_bucket) * bv = b;
+ while (bv->lock)
+ ;
}
-#endif
hash >>= h->log2_nbuckets;
@@ -507,53 +524,12 @@ int BV (clib_bihash_search)
if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
{
*valuep = v->kvp[i];
-
-#if BIHASH_KVP_CACHE_SIZE > 0
- u8 cache_slot;
- /* Shut off the cache */
- if (BV (clib_bihash_lock_bucket) (b))
- {
- cache_slot = BV (clib_bihash_get_lru) (b);
- b->cache[cache_slot] = v->kvp[i];
- BV (clib_bihash_update_lru) (b, cache_slot);
-
- /* Reenable the cache */
- BV (clib_bihash_unlock_bucket) (b);
- h->cache_misses++;
- }
-#endif
return 0;
}
}
return -1;
}
-u8 *BV (format_bihash_lru) (u8 * s, va_list * args)
-{
-#if BIHASH_KVP_SIZE > 0
- int i;
- BVT (clib_bihash_bucket) * b = va_arg (*args, BVT (clib_bihash_bucket) *);
- u16 cache_lru = b->cache_lru;
-
- s = format (s, "cache %s, order ", cache_lru & (1 << 15) ? "on" : "off");
-
- for (i = 0; i < BIHASH_KVP_CACHE_SIZE; i++)
- s = format (s, "[%d] ", ((cache_lru >> (3 * i)) & 7));
-
- return (s);
-#else
- return format (s, "cache not configured");
-#endif
-}
-
-void
-BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b, u8 slot)
-{
-#if BIHASH_KVP_SIZE > 0
- BV (clib_bihash_update_lru) (b, slot);
-#endif
-}
-
u8 *BV (format_bihash) (u8 * s, va_list * args)
{
BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
diff --git a/src/vppinfra/bihash_template.h b/src/vppinfra/bihash_template.h
index eb50d32d521..6303992bc57 100644
--- a/src/vppinfra/bihash_template.h
+++ b/src/vppinfra/bihash_template.h
@@ -49,9 +49,7 @@ typedef struct BV (clib_bihash_value)
};
} BVT (clib_bihash_value);
-#if BIHASH_KVP_CACHE_SIZE > 5
-#error Requested KVP cache LRU data exceeds 16 bits
-#endif
+#define BIHASH_BUCKET_OFFSET_BITS 36
typedef struct
{
@@ -59,28 +57,23 @@ typedef struct
{
struct
{
- u64 offset:37;
+ u64 offset:BIHASH_BUCKET_OFFSET_BITS;
+ u64 lock:1;
u64 linear_search:1;
u64 log2_pages:8;
i64 refcnt:16;
};
u64 as_u64;
};
-#if BIHASH_KVP_CACHE_SIZE > 0
- u16 cache_lru;
- BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE];
-#endif
} BVT (clib_bihash_bucket);
-#if BIHASH_KVP_CACHE_SIZE == 0
STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
-#endif
typedef struct
{
BVT (clib_bihash_value) * values;
BVT (clib_bihash_bucket) * buckets;
- volatile u32 *writer_lock;
+ volatile u32 *alloc_lock;
BVT (clib_bihash_value) ** working_copies;
int *working_copy_lengths;
@@ -110,127 +103,38 @@ typedef struct
} BVT (clib_bihash);
-
-static inline void
-BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
+static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
{
-#if BIHASH_KVP_CACHE_SIZE > 1
- u16 value, tmp, mask;
- u8 found_lru_pos;
- u16 save_hi;
-
- ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
-
- /* First, find the slot in cache_lru */
- mask = slot;
- if (BIHASH_KVP_CACHE_SIZE > 1)
- mask |= slot << 3;
- if (BIHASH_KVP_CACHE_SIZE > 2)
- mask |= slot << 6;
- if (BIHASH_KVP_CACHE_SIZE > 3)
- mask |= slot << 9;
- if (BIHASH_KVP_CACHE_SIZE > 4)
- mask |= slot << 12;
-
- value = b->cache_lru;
- tmp = value ^ mask;
-
- /* Already the most-recently used? */
- if ((tmp & 7) == 0)
- return;
-
- found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0;
- if (BIHASH_KVP_CACHE_SIZE > 2)
- found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos;
- if (BIHASH_KVP_CACHE_SIZE > 3)
- found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos;
- if (BIHASH_KVP_CACHE_SIZE > 4)
- found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos;
-
- ASSERT (found_lru_pos);
-
- /* create a mask to kill bits in or above slot */
- mask = 0xFFFF << found_lru_pos;
- mask <<= found_lru_pos;
- mask <<= found_lru_pos;
- mask ^= 0xFFFF;
- tmp = value & mask;
-
- /* Save bits above slot */
- mask ^= 0xFFFF;
- mask <<= 3;
- save_hi = value & mask;
-
- value = save_hi | (tmp << 3) | slot;
-
- b->cache_lru = value;
-#endif
-}
-
-void
-BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
- u8 slot);
-
-static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
-{
-#if BIHASH_KVP_CACHE_SIZE > 0
- return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
-#else
- return 0;
-#endif
+ while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
+ ;
}
-static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
+static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
{
-#if BIHASH_KVP_CACHE_SIZE > 0
- u16 initial_lru_value;
-
- memset (b->cache, 0xff, sizeof (b->cache));
-
- /*
- * We'll want the cache to be loaded from slot 0 -> slot N, so
- * the initial LRU order is reverse index order.
- */
- if (BIHASH_KVP_CACHE_SIZE == 1)
- initial_lru_value = 0;
- else if (BIHASH_KVP_CACHE_SIZE == 2)
- initial_lru_value = (0 << 3) | (1 << 0);
- else if (BIHASH_KVP_CACHE_SIZE == 3)
- initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0);
- else if (BIHASH_KVP_CACHE_SIZE == 4)
- initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0);
- else if (BIHASH_KVP_CACHE_SIZE == 5)
- initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0);
-
- b->cache_lru = initial_lru_value;
-#endif
+ __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
}
-static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
+static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
{
-#if BIHASH_KVP_CACHE_SIZE > 0
- u16 cache_lru_bit;
- u16 rv;
-
- cache_lru_bit = 1 << 15;
+ BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
- rv = __sync_fetch_and_or (&b->cache_lru, cache_lru_bit);
- /* Was already locked? */
- if (rv & (1 << 15))
- return 0;
-#endif
- return 1;
+ do
+ {
+ locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
+ unlocked_bucket.lock = 0;
+ locked_bucket.lock = 1;
+ }
+ while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
+ locked_bucket.as_u64, 1 /* weak */ ,
+ __ATOMIC_ACQUIRE,
+ __ATOMIC_ACQUIRE) == 0);
}
static inline void BV (clib_bihash_unlock_bucket)
(BVT (clib_bihash_bucket) * b)
{
-#if BIHASH_KVP_CACHE_SIZE > 0
- u16 cache_lru;
-
- cache_lru = b->cache_lru & ~(1 << 15);
- b->cache_lru = cache_lru;
-#endif
+ CLIB_MEMORY_BARRIER ();
+ b->lock = 0;
}
static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
@@ -245,7 +149,8 @@ static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
static inline int BV (clib_bihash_bucket_is_empty)
(BVT (clib_bihash_bucket) * b)
{
- return b->as_u64 == 0;
+ /* Note: applied to locked buckets, test offset */
+ return b->offset == 0;
}
static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
@@ -286,9 +191,6 @@ static inline int BV (clib_bihash_search_inline_with_hash)
u32 bucket_index;
BVT (clib_bihash_value) * v;
BVT (clib_bihash_bucket) * b;
-#if BIHASH_KVP_CACHE_SIZE > 0
- BVT (clib_bihash_kv) * kvp;
-#endif
int i, limit;
bucket_index = hash & (h->nbuckets - 1);
@@ -297,23 +199,12 @@ static inline int BV (clib_bihash_search_inline_with_hash)
if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
return -1;
-#if BIHASH_KVP_CACHE_SIZE > 0
- /* Check the cache, if not currently locked */
- if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
+ if (PREDICT_FALSE (b->lock))
{
- limit = BIHASH_KVP_CACHE_SIZE;
- kvp = b->cache;
- for (i = 0; i < limit; i++)
- {
- if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
- {
- *key_result = kvp[i];
- h->cache_hits++;
- return 0;
- }
- }
+ volatile BVT (clib_bihash_bucket) * bv = b;
+ while (bv->lock)
+ ;
}
-#endif
hash >>= h->log2_nbuckets;
@@ -330,21 +221,6 @@ static inline int BV (clib_bihash_search_inline_with_hash)
if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
{
*key_result = v->kvp[i];
-
-#if BIHASH_KVP_CACHE_SIZE > 0
- u8 cache_slot;
- /* Try to lock the bucket */
- if (BV (clib_bihash_lock_bucket) (b))
- {
- cache_slot = BV (clib_bihash_get_lru) (b);
- b->cache[cache_slot] = v->kvp[i];
- BV (clib_bihash_update_lru) (b, cache_slot);
-
- /* Unlock the bucket */
- BV (clib_bihash_unlock_bucket) (b);
- h->cache_misses++;
- }
-#endif
return 0;
}
}
@@ -401,9 +277,6 @@ static inline int BV (clib_bihash_search_inline_2_with_hash)
u32 bucket_index;
BVT (clib_bihash_value) * v;
BVT (clib_bihash_bucket) * b;
-#if BIHASH_KVP_CACHE_SIZE > 0
- BVT (clib_bihash_kv) * kvp;
-#endif
int i, limit;
ASSERT (valuep);
@@ -414,23 +287,12 @@ static inline int BV (clib_bihash_search_inline_2_with_hash)
if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
return -1;
- /* Check the cache, if currently unlocked */
-#if BIHASH_KVP_CACHE_SIZE > 0
- if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
+ if (PREDICT_FALSE (b->lock))
{
- limit = BIHASH_KVP_CACHE_SIZE;
- kvp = b->cache;
- for (i = 0; i < limit; i++)
- {
- if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
- {
- *valuep = kvp[i];
- h->cache_hits++;
- return 0;
- }
- }
+ volatile BVT (clib_bihash_bucket) * bv = b;
+ while (bv->lock)
+ ;
}
-#endif
hash >>= h->log2_nbuckets;
v = BV (clib_bihash_get_value) (h, b->offset);
@@ -446,22 +308,6 @@ static inline int BV (clib_bihash_search_inline_2_with_hash)
if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
{
*valuep = v->kvp[i];
-
-#if BIHASH_KVP_CACHE_SIZE > 0
- u8 cache_slot;
-
- /* Try to lock the bucket */
- if (BV (clib_bihash_lock_bucket) (b))
- {
- cache_slot = BV (clib_bihash_get_lru) (b);
- b->cache[cache_slot] = v->kvp[i];
- BV (clib_bihash_update_lru) (b, cache_slot);
-
- /* Reenable the cache */
- BV (clib_bihash_unlock_bucket) (b);
- h->cache_misses++;
- }
-#endif
return 0;
}
}
diff --git a/src/vppinfra/bihash_vec8_8.h b/src/vppinfra/bihash_vec8_8.h
index cc78f1d51d7..2394600ba7c 100644
--- a/src/vppinfra/bihash_vec8_8.h
+++ b/src/vppinfra/bihash_vec8_8.h
@@ -13,12 +13,10 @@
* limitations under the License.
*/
#undef BIHASH_TYPE
-#undef BIHASH_KVP_CACHE_SIZE
#undef BIHASH_KVP_PER_PAGE
#define BIHASH_TYPE _vec8_8
#define BIHASH_KVP_PER_PAGE 4
-#define BIHASH_KVP_CACHE_SIZE 0
#ifndef __included_bihash_vec8_8_h__
#define __included_bihash_vec8_8_h__
diff --git a/src/vppinfra/test_bihash_template.c b/src/vppinfra/test_bihash_template.c
index bdcf2cd6a81..80e11511f62 100644
--- a/src/vppinfra/test_bihash_template.c
+++ b/src/vppinfra/test_bihash_template.c
@@ -17,6 +17,7 @@
#include <vppinfra/error.h>
#include <sys/resource.h>
#include <stdio.h>
+#include <pthread.h>
#include <vppinfra/bihash_8_8.h>
#include <vppinfra/bihash_template.h>
@@ -25,6 +26,9 @@
typedef struct
{
+ volatile u32 thread_barrier;
+ volatile u32 threads_running;
+ volatile u64 sequence_number;
u64 seed;
u32 nbuckets;
u32 nitems;
@@ -34,11 +38,13 @@ typedef struct
int careful_delete_tests;
int verbose;
int non_random_keys;
+ u32 nthreads;
uword *key_hash;
u64 *keys;
uword hash_memory_size;
BVT (clib_bihash) hash;
clib_time_t clib_time;
+ void *global_heap;
unformat_input_t *input;
@@ -89,6 +95,102 @@ test_bihash_vec64 (test_main_t * tm)
return 0;
}
+void *
+test_bihash_thread_fn (void *arg)
+{
+ BVT (clib_bihash) * h;
+ BVT (clib_bihash_kv) kv;
+ test_main_t *tm = &test_main;
+
+ int i, j;
+
+ u32 my_thread_index = (u32) (u64) arg;
+ __os_thread_index = my_thread_index;
+ clib_mem_set_per_cpu_heap (tm->global_heap);
+
+ while (tm->thread_barrier)
+ ;
+
+ h = &tm->hash;
+
+ for (i = 0; i < tm->ncycles; i++)
+ {
+ for (j = 0; j < tm->nitems; j++)
+ {
+ kv.key = ((u64) my_thread_index << 32) | (u64) j;
+ kv.value = ((u64) my_thread_index << 32) | (u64) j;
+ (void) __atomic_add_fetch (&tm->sequence_number, 1,
+ __ATOMIC_ACQUIRE);
+ BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
+ }
+ for (j = 0; j < tm->nitems; j++)
+ {
+ kv.key = ((u64) my_thread_index << 32) | (u64) j;
+ kv.value = ((u64) my_thread_index << 32) | (u64) j;
+ (void) __atomic_add_fetch (&tm->sequence_number, 1,
+ __ATOMIC_ACQUIRE);
+ BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
+ }
+ }
+
+ (void) __atomic_sub_fetch (&tm->threads_running, 1, __ATOMIC_ACQUIRE);
+ while (1)
+ {
+ struct timespec ts, tsrem;
+ ts.tv_sec = 1;
+ ts.tv_nsec = 0;
+
+ while (nanosleep (&ts, &tsrem) < 0)
+ ts = tsrem;
+ }
+ return (0); /* not so much */
+}
+
+static clib_error_t *
+test_bihash_threads (test_main_t * tm)
+{
+ int i;
+ pthread_t handle;
+ BVT (clib_bihash) * h;
+ int rv;
+
+ h = &tm->hash;
+
+ BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
+
+ tm->thread_barrier = 1;
+
+ /* Start the worker threads */
+ for (i = 0; i < tm->nthreads; i++)
+ {
+ rv = pthread_create (&handle, NULL, test_bihash_thread_fn,
+ (void *) (u64) i);
+ if (rv)
+ {
+ clib_unix_warning ("pthread_create returned %d", rv);
+ }
+ }
+ tm->threads_running = i;
+ tm->sequence_number = 0;
+ CLIB_MEMORY_BARRIER ();
+
+ /* start the workers */
+ tm->thread_barrier = 0;
+
+ while (tm->threads_running)
+ {
+ struct timespec ts, tsrem;
+ ts.tv_sec = 0;
+ ts.tv_nsec = 20 * 1000 * 1000; /* sleep for 20ms at a time */
+
+ while (nanosleep (&ts, &tsrem) < 0)
+ ts = tsrem;
+ }
+
+ return 0;
+}
+
+
static clib_error_t *
test_bihash (test_main_t * tm)
{
@@ -283,39 +385,6 @@ test_bihash (test_main_t * tm)
}
clib_error_t *
-test_bihash_cache (test_main_t * tm)
-{
- u32 lru;
- BVT (clib_bihash_bucket) _b, *b = &_b;
-
- BV (clib_bihash_reset_cache) (b);
-
- fformat (stdout, "Initial LRU config: %U\n", BV (format_bihash_lru), b);
-
- BV (clib_bihash_update_lru_not_inline) (b, 3);
-
- fformat (stdout, "use slot 3, LRU config: %U\n", BV (format_bihash_lru), b);
-
- BV (clib_bihash_update_lru) (b, 1);
-
- fformat (stdout, "use slot 1 LRU config: %U\n", BV (format_bihash_lru), b);
-
- lru = BV (clib_bihash_get_lru) (b);
-
- fformat (stdout, "least-recently-used is %d\n", lru);
-
- BV (clib_bihash_update_lru) (b, 4);
-
- fformat (stdout, "use slot 4 LRU config: %U\n", BV (format_bihash_lru), b);
-
- lru = BV (clib_bihash_get_lru) (b);
-
- fformat (stdout, "least-recently-used is %d\n", lru);
-
- return 0;
-}
-
-clib_error_t *
test_bihash_main (test_main_t * tm)
{
unformat_input_t *i = tm->input;
@@ -351,9 +420,8 @@ test_bihash_main (test_main_t * tm)
;
else if (unformat (i, "vec64"))
which = 1;
- else if (unformat (i, "cache"))
+ else if (unformat (i, "threads %u", &tm->nthreads))
which = 2;
-
else if (unformat (i, "verbose"))
tm->verbose = 1;
else
@@ -378,7 +446,7 @@ test_bihash_main (test_main_t * tm)
break;
case 2:
- error = test_bihash_cache (tm);
+ error = test_bihash_threads (tm);
break;
default:
@@ -398,6 +466,8 @@ main (int argc, char *argv[])
clib_mem_init (0, 4095ULL << 20);
+ tm->global_heap = clib_mem_get_per_cpu_heap ();
+
tm->input = &i;
tm->seed = 0xdeaddabe;