26 rv = h->alloc_arena_next;
27 h->alloc_arena_next += nbytes;
29 if (rv >= (h->alloc_arena + h->alloc_arena_size))
42 nbuckets = 1 << (
max_log2 (nbuckets));
44 h->name = (
u8 *) name;
45 h->nbuckets = nbuckets;
46 h->log2_nbuckets =
max_log2 (nbuckets);
51 h->alloc_arena_next = h->alloc_arena;
54 bucket_size = nbuckets *
sizeof (h->buckets[0]);
58 h->writer_lock[0] = 0;
60 for (i = 0; i < nbuckets; i++)
61 BV (clib_bihash_reset_cache) (h->buckets +
i);
66 void BV (clib_bihash_set_kvp_format_fn) (
BVT (clib_bihash) *
h,
77 memset (h, 0,
sizeof (*h));
86 ASSERT (h->writer_lock[0]);
103 memset (rv, 0xff,
sizeof (*rv) * (1 <<
log2_pages));
111 ASSERT (h->writer_lock[0]);
123 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
126 int log2_working_copy_length;
128 if (thread_index >=
vec_len (h->working_copies))
139 working_copy = h->working_copies[thread_index];
140 log2_working_copy_length = h->working_copy_lengths[thread_index];
142 h->saved_bucket.as_u64 = b->as_u64;
144 if (b->log2_pages > log2_working_copy_length)
152 (
h,
sizeof (working_copy[0]) * (1 << b->log2_pages));
153 h->working_copy_lengths[thread_index] = b->log2_pages;
154 h->working_copies[thread_index] = working_copy;
158 while (BV (clib_bihash_lock_bucket) (b) == 0)
163 clib_memcpy (working_copy, v,
sizeof (*v) * (1 << b->log2_pages));
164 working_bucket.as_u64 = b->as_u64;
167 b->as_u64 = working_bucket.as_u64;
168 h->working_copies[thread_index] = working_copy;
174 (
BVT (clib_bihash) *
h,
179 int i, j, length_in_kvs;
181 new_values = BV (value_alloc) (
h, new_log2_pages);
182 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
184 for (i = 0; i < length_in_kvs; i++)
189 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
193 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[
i]));
194 new_hash >>= h->log2_nbuckets;
195 new_hash &= (1 << new_log2_pages) - 1;
196 new_v = &new_values[new_hash];
202 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
204 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
205 sizeof (new_v->kvp[j]));
210 BV (value_free) (
h, new_values, new_log2_pages);
221 (
BVT (clib_bihash) *
h,
226 int i, j, new_length, old_length;
228 new_values = BV (value_alloc) (
h, new_log2_pages);
229 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
230 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
234 for (i = 0; i < old_length; i++)
237 for (; j < new_length; j++)
240 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
244 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
247 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
248 sizeof (new_values->kvp[j]));
255 BV (value_free) (
h, new_values, new_log2_pages);
264 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add)
267 BVT (clib_bihash_bucket) * b, tmp_b;
272 u32 new_log2_pages, old_log2_pages;
274 int mark_bucket_linear;
277 hash = BV (clib_bihash_hash) (add_v);
279 bucket_index = hash & (h->nbuckets - 1);
280 b = &h->buckets[bucket_index];
282 hash >>= h->log2_nbuckets;
284 tmp_b.linear_search = 0;
286 while (__sync_lock_test_and_set (h->writer_lock, 1))
298 v = BV (value_alloc) (
h, 0);
305 b->as_u64 = tmp_b.as_u64;
315 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
316 if (b->linear_search)
317 limit <<= b->log2_pages;
325 for (i = 0; i < limit; i++)
327 if (!memcmp (&(v->kvp[i]), &add_v->key,
sizeof (add_v->key)))
329 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
332 b->as_u64 = h->saved_bucket.as_u64;
336 for (i = 0; i < limit; i++)
338 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
340 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
342 b->as_u64 = h->saved_bucket.as_u64;
351 for (i = 0; i < limit; i++)
353 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
355 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
359 h->saved_bucket.refcnt -= 1;
360 b->as_u64 = h->saved_bucket.as_u64;
366 goto free_old_bucket;
371 b->as_u64 = h->saved_bucket.as_u64;
375 old_log2_pages = h->saved_bucket.log2_pages;
376 new_log2_pages = old_log2_pages + 1;
377 mark_bucket_linear = 0;
379 working_copy = h->working_copies[thread_index];
400 mark_bucket_linear = 1;
406 new_hash = BV (clib_bihash_hash) (add_v);
408 if (mark_bucket_linear)
409 limit <<= new_log2_pages;
410 new_hash >>= h->log2_nbuckets;
411 new_hash &= (1 << new_log2_pages) - 1;
412 new_v += mark_bucket_linear ? 0 : new_hash;
414 for (i = 0; i < limit; i++)
416 if (BV (clib_bihash_is_free) (&(new_v->kvp[
i])))
418 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
424 BV (value_free) (
h, save_new_v, new_log2_pages);
435 tmp_b.log2_pages = new_log2_pages;
437 tmp_b.linear_search = mark_bucket_linear;
438 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
443 b->as_u64 = tmp_b.as_u64;
446 BV (value_free) (
h,
v, h->saved_bucket.log2_pages);
449 BV (clib_bihash_reset_cache) (b);
450 BV (clib_bihash_unlock_bucket) (b);
452 h->writer_lock[0] = 0;
456 int BV (clib_bihash_search)
457 (
BVT (clib_bihash) *
h,
458 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
463 #if BIHASH_KVP_CACHE_SIZE > 0 464 BVT (clib_bihash_kv) * kvp;
466 BVT (clib_bihash_bucket) * b;
471 hash = BV (clib_bihash_hash) (search_key);
473 bucket_index = hash & (h->nbuckets - 1);
474 b = &h->buckets[bucket_index];
479 #if BIHASH_KVP_CACHE_SIZE > 0 485 for (i = 0; i < limit; i++)
487 if (BV (clib_bihash_key_compare) (kvp[
i].key, search_key->key))
497 hash >>= h->log2_nbuckets;
501 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
503 limit <<= b->log2_pages;
505 for (i = 0; i < limit; i++)
507 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, search_key->key))
511 #if BIHASH_KVP_CACHE_SIZE > 0 514 if (BV (clib_bihash_lock_bucket) (b))
516 cache_slot = BV (clib_bihash_get_lru) (b);
517 b->cache[cache_slot] = v->kvp[
i];
518 BV (clib_bihash_update_lru) (b, cache_slot);
521 BV (clib_bihash_unlock_bucket) (b);
531 u8 *BV (format_bihash_lru) (
u8 * s, va_list * args)
533 #if BIHASH_KVP_SIZE > 0 535 BVT (clib_bihash_bucket) * b = va_arg (*args,
BVT (clib_bihash_bucket) *);
536 u16 cache_lru = b->cache_lru;
538 s =
format (s,
"cache %s, order ", cache_lru & (1 << 15) ?
"on" :
"off");
541 s =
format (s,
"[%d] ", ((cache_lru >> (3 * i)) & 7));
545 return format (s,
"cache not configured");
550 BV (clib_bihash_update_lru_not_inline) (
BVT (clib_bihash_bucket) * b,
u8 slot)
552 #if BIHASH_KVP_SIZE > 0 553 BV (clib_bihash_update_lru) (b, slot);
557 u8 *BV (format_bihash) (
u8 * s, va_list * args)
559 BVT (clib_bihash) * h = va_arg (*args,
BVT (clib_bihash) *);
560 int verbose = va_arg (*args,
int);
561 BVT (clib_bihash_bucket) * b;
564 u64 active_elements = 0;
565 u64 active_buckets = 0;
566 u64 linear_buckets = 0;
569 s =
format (s,
"Hash table %s\n", h->name ? h->name : (
u8 *)
"(unnamed)");
571 for (i = 0; i < h->nbuckets; i++)
577 s =
format (s,
"[%d]: empty\n", i);
583 if (b->linear_search)
588 s =
format (s,
"[%d]: heap offset %d, len %d, linear %d\n", i,
589 b->offset, (1 << b->log2_pages), b->linear_search);
593 for (j = 0; j < (1 << b->log2_pages); j++)
597 if (BV (clib_bihash_is_free) (&v->kvp[k]))
600 s =
format (s,
" %d: empty\n",
601 j * BIHASH_KVP_PER_PAGE + k);
608 s =
format (s,
" %d: %U\n",
609 j * BIHASH_KVP_PER_PAGE + k,
610 h->fmt_fn, &(v->kvp[k]));
614 s =
format (s,
" %d: %U\n",
615 j * BIHASH_KVP_PER_PAGE + k,
616 BV (format_bihash_kvp), &(v->kvp[k]));
625 s =
format (s,
" %lld active elements %lld active buckets\n",
626 active_elements, active_buckets);
629 for (i = 0; i <
vec_len (h->freelists); i++)
634 free_elt = h->freelists[
i];
638 free_elt = free_elt->next_free;
641 s =
format (s,
" [len %d] %u free elts\n", 1 << i, nfree);
644 s =
format (s,
" %lld linear search buckets\n", linear_buckets);
645 s =
format (s,
" %lld cache hits, %lld cache misses\n",
646 h->cache_hits, h->cache_misses);
647 used_bytes = h->alloc_arena_next - h->alloc_arena;
649 " arena: base %llx, next %llx\n" 650 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
651 h->alloc_arena, h->alloc_arena_next,
652 used_bytes, used_bytes >> 20,
653 h->alloc_arena_size, h->alloc_arena_size >> 20);
658 (
BVT (clib_bihash) *
h,
void *callback,
void *arg)
661 BVT (clib_bihash_bucket) * b;
663 void (*fp) (
BVT (clib_bihash_kv) *,
void *) = callback;
665 for (i = 0; i < h->nbuckets; i++)
672 for (j = 0; j < (1 << b->log2_pages); j++)
676 if (BV (clib_bihash_is_free) (&v->kvp[k]))
679 (*fp) (&v->kvp[k], arg);
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
#define BIHASH_KVP_PER_PAGE
void clib_bihash_free(clib_bihash *h)
Destroy a bounded index extensible hash table.
void os_out_of_memory(void)
for(i=1;i<=collision_buckets;i++)
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)
#define vec_free(V)
Free vector's memory (no header).
#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)
template key/value backing page structure
static void clib_mem_vm_free(void *addr, uword size)
#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)
#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
static void * clib_mem_vm_alloc(uword size)
static void * alloc_aligned(uword size, uword log2_align, void **ptr_to_free)
#define BIHASH_KVP_CACHE_SIZE