24 nbuckets = 1 << (
max_log2 (nbuckets));
26 h->name = (
u8 *) name;
27 h->nbuckets = nbuckets;
28 h->log2_nbuckets =
max_log2 (nbuckets);
38 h->writer_lock[0] = 0;
40 for (i = 0; i < nbuckets; i++)
41 BV (clib_bihash_reset_cache) (h->buckets +
i);
49 memset (h, 0,
sizeof (*h));
59 ASSERT (h->writer_lock[0]);
80 memset (rv, 0xff,
sizeof (*rv) * (1 <<
log2_pages));
88 ASSERT (h->writer_lock[0]);
100 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
104 int log2_working_copy_length;
106 if (thread_index >=
vec_len (h->working_copies))
119 working_copy = h->working_copies[thread_index];
120 log2_working_copy_length = h->working_copy_lengths[thread_index];
122 h->saved_bucket.as_u64 = b->as_u64;
125 if (b->log2_pages > log2_working_copy_length)
131 (
sizeof (working_copy[0]) * (1 << b->log2_pages),
133 h->working_copy_lengths[thread_index] = b->log2_pages;
134 h->working_copies[thread_index] = working_copy;
140 while (BV (clib_bihash_lock_bucket) (b) == 0)
145 clib_memcpy (working_copy, v,
sizeof (*v) * (1 << b->log2_pages));
146 working_bucket.as_u64 = b->as_u64;
149 b->as_u64 = working_bucket.as_u64;
150 h->working_copies[thread_index] = working_copy;
156 (
BVT (clib_bihash) *
h,
161 int i, j, length_in_kvs;
163 new_values = BV (value_alloc) (
h, new_log2_pages);
164 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
166 for (i = 0; i < length_in_kvs; i++)
171 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
175 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[
i]));
176 new_hash >>= h->log2_nbuckets;
177 new_hash &= (1 << new_log2_pages) - 1;
178 new_v = &new_values[new_hash];
184 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
186 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
187 sizeof (new_v->kvp[j]));
192 BV (value_free) (
h, new_values, new_log2_pages);
203 (
BVT (clib_bihash) *
h,
208 int i, j, new_length, old_length;
210 new_values = BV (value_alloc) (
h, new_log2_pages);
211 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
212 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
216 for (i = 0; i < old_length; i++)
219 for (; j < new_length; j++)
222 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
226 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
229 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
230 sizeof (new_values->kvp[j]));
237 BV (value_free) (
h, new_values, new_log2_pages);
246 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add)
249 BVT (clib_bihash_bucket) * b, tmp_b;
254 u32 new_log2_pages, old_log2_pages;
256 int mark_bucket_linear;
259 hash = BV (clib_bihash_hash) (add_v);
261 bucket_index = hash & (h->nbuckets - 1);
262 b = &h->buckets[bucket_index];
264 hash >>= h->log2_nbuckets;
266 tmp_b.linear_search = 0;
268 while (__sync_lock_test_and_set (h->writer_lock, 1))
280 v = BV (value_alloc) (
h, 0);
286 b->as_u64 = tmp_b.as_u64;
296 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
297 if (b->linear_search)
298 limit <<= b->log2_pages;
306 for (i = 0; i < limit; i++)
308 if (!memcmp (&(v->kvp[i]), &add_v->key,
sizeof (add_v->key)))
310 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
313 b->as_u64 = h->saved_bucket.as_u64;
317 for (i = 0; i < limit; i++)
319 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
321 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
323 b->as_u64 = h->saved_bucket.as_u64;
331 for (i = 0; i < limit; i++)
333 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
335 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
337 b->as_u64 = h->saved_bucket.as_u64;
342 b->as_u64 = h->saved_bucket.as_u64;
346 old_log2_pages = h->saved_bucket.log2_pages;
347 new_log2_pages = old_log2_pages + 1;
348 mark_bucket_linear = 0;
350 working_copy = h->working_copies[thread_index];
371 mark_bucket_linear = 1;
377 new_hash = BV (clib_bihash_hash) (add_v);
379 if (mark_bucket_linear)
380 limit <<= new_log2_pages;
381 new_hash >>= h->log2_nbuckets;
382 new_hash &= (1 << new_log2_pages) - 1;
383 new_v += mark_bucket_linear ? 0 : new_hash;
385 for (i = 0; i < limit; i++)
387 if (BV (clib_bihash_is_free) (&(new_v->kvp[
i])))
389 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
395 BV (value_free) (
h, save_new_v, new_log2_pages);
407 if (tmp_b.linear_search ^ mark_bucket_linear)
408 h->linear_buckets += (mark_bucket_linear == 1) ? 1 : -1;
410 tmp_b.log2_pages = new_log2_pages;
412 tmp_b.linear_search = mark_bucket_linear;
415 b->as_u64 = tmp_b.as_u64;
417 BV (value_free) (
h,
v, old_log2_pages);
420 BV (clib_bihash_reset_cache) (b);
421 BV (clib_bihash_unlock_bucket) (b);
423 h->writer_lock[0] = 0;
428 (
BVT (clib_bihash) *
h,
429 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
434 #if BIHASH_KVP_CACHE_SIZE > 0 435 BVT (clib_bihash_kv) * kvp;
437 BVT (clib_bihash_bucket) * b;
442 hash = BV (clib_bihash_hash) (search_key);
444 bucket_index = hash & (h->nbuckets - 1);
445 b = &h->buckets[bucket_index];
450 #if BIHASH_KVP_CACHE_SIZE > 0 456 for (i = 0; i < limit; i++)
458 if (BV (clib_bihash_key_compare) (kvp[
i].key, search_key->key))
468 hash >>= h->log2_nbuckets;
472 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
474 limit <<= b->log2_pages;
476 for (i = 0; i < limit; i++)
478 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, search_key->key))
482 #if BIHASH_KVP_CACHE_SIZE > 0 485 if (BV (clib_bihash_lock_bucket) (b))
487 cache_slot = BV (clib_bihash_get_lru) (b);
488 b->cache[cache_slot] = v->kvp[
i];
489 BV (clib_bihash_update_lru) (b, cache_slot);
492 BV (clib_bihash_unlock_bucket) (b);
502 u8 *BV (format_bihash_lru) (
u8 * s, va_list * args)
504 #if BIHASH_KVP_SIZE > 0 506 BVT (clib_bihash_bucket) * b = va_arg (*args,
BVT (clib_bihash_bucket) *);
507 u16 cache_lru = b->cache_lru;
509 s =
format (s,
"cache %s, order ", cache_lru & (1 << 15) ?
"on" :
"off");
512 s =
format (s,
"[%d] ", ((cache_lru >> (3 * i)) & 7));
516 return format (s,
"cache not configured");
521 BV (clib_bihash_update_lru_not_inline) (
BVT (clib_bihash_bucket) * b,
u8 slot)
523 #if BIHASH_KVP_SIZE > 0 524 BV (clib_bihash_update_lru) (b, slot);
528 u8 *BV (format_bihash) (
u8 * s, va_list * args)
530 BVT (clib_bihash) * h = va_arg (*args,
BVT (clib_bihash) *);
531 int verbose = va_arg (*args,
int);
532 BVT (clib_bihash_bucket) * b;
535 u64 active_elements = 0;
537 s =
format (s,
"Hash table %s\n", h->name ? h->name : (
u8 *)
"(unnamed)");
539 for (i = 0; i < h->nbuckets; i++)
545 s =
format (s,
"[%d]: empty\n", i);
551 s =
format (s,
"[%d]: heap offset %d, len %d, linear %d\n", i,
552 b->offset, (1 << b->log2_pages), b->linear_search);
556 for (j = 0; j < (1 << b->log2_pages); j++)
560 if (BV (clib_bihash_is_free) (&v->kvp[k]))
563 s =
format (s,
" %d: empty\n",
564 j * BIHASH_KVP_PER_PAGE + k);
569 s =
format (s,
" %d: %U\n",
570 j * BIHASH_KVP_PER_PAGE + k,
571 BV (format_bihash_kvp), &(v->kvp[k]));
579 s =
format (s,
" %lld active elements\n", active_elements);
581 s =
format (s,
" %d linear search buckets\n", h->linear_buckets);
582 s =
format (s,
" %lld cache hits, %lld cache misses\n",
583 h->cache_hits, h->cache_misses);
588 (
BVT (clib_bihash) *
h,
void *callback,
void *arg)
591 BVT (clib_bihash_bucket) * b;
593 void (*fp) (
BVT (clib_bihash_kv) *,
void *) = callback;
595 for (i = 0; i < h->nbuckets; i++)
602 for (j = 0; j < (1 << b->log2_pages); j++)
606 if (BV (clib_bihash_is_free) (&v->kvp[k]))
609 (*fp) (&v->kvp[k], arg);
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
sll srl srl sll sra u16x4 i
#define BIHASH_KVP_PER_PAGE
void clib_bihash_free(clib_bihash *h)
Destroy a bounded index extensible hash table.
void * mheap_alloc(void *memory, uword size)
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
int clib_bihash_add_del(clib_bihash *h, clib_bihash_kv *add_v, int is_add)
Add or delete a (key,value) pair from a bi-hash table.
static uword clib_bihash_get_offset(clib_bihash *h, void *v)
Get clib mheap offset given a pointer.
static vnet_classify_entry_t * split_and_rehash_linear(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 old_log2_pages, u32 new_log2_pages)
void clib_bihash_init(clib_bihash *h, char *name, u32 nbuckets, uword memory_size)
initialize a bounded index extensible hash table
void clib_bihash_foreach_key_value_pair(clib_bihash *h, void *callback, void *arg)
Visit active (key,value) pairs in a bi-hash table.
static vnet_classify_entry_t * split_and_rehash(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 old_log2_pages, u32 new_log2_pages)
static void * clib_mem_set_heap(void *heap)
#define clib_warning(format, args...)
#define clib_memcpy(a, b, c)
static void make_working_copy(vnet_classify_table_t *t, vnet_classify_bucket_t *b)
int clib_bihash_search(clib_bihash *h, clib_bihash_kv *search_v, clib_bihash_kv *return_v)
Search a bi-hash table.
static void clib_mem_free(void *p)
template key/value backing page structure
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static uword max_log2(uword x)
static_always_inline uword os_get_thread_index(void)
static void * clib_mem_alloc_aligned(uword size, uword align)
#define CLIB_MEMORY_BARRIER()
static void * clib_bihash_get_value(clib_bihash *h, uword offset)
Get pointer to value page given its clib mheap offset.
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
#define CLIB_CACHE_LINE_BYTES
#define BIHASH_KVP_CACHE_SIZE