2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
18 void BV (clib_bihash_init)
19 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
24 nbuckets = 1 << (max_log2 (nbuckets));
26 h->name = (u8 *) name;
27 h->nbuckets = nbuckets;
28 h->log2_nbuckets = max_log2 (nbuckets);
32 h->mheap = mheap_alloc (0 /* use VM */ , memory_size);
34 oldheap = clib_mem_set_heap (h->mheap);
35 vec_validate_aligned (h->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES);
36 h->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
37 CLIB_CACHE_LINE_BYTES);
38 h->writer_lock[0] = 0;
40 for (i = 0; i < nbuckets; i++)
41 BV (clib_bihash_reset_cache) (h->buckets + i);
43 clib_mem_set_heap (oldheap);
48 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
49 format_function_t * fmt_fn)
54 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
56 mheap_free (h->mheap);
57 memset (h, 0, sizeof (*h));
61 BVT (clib_bihash_value) *
62 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
64 BVT (clib_bihash_value) * rv = 0;
67 ASSERT (h->writer_lock[0]);
68 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
70 oldheap = clib_mem_set_heap (h->mheap);
72 vec_validate (h->freelists, log2_pages);
73 rv = clib_mem_alloc_aligned ((sizeof (*rv) * (1 << log2_pages)),
74 CLIB_CACHE_LINE_BYTES);
75 clib_mem_set_heap (oldheap);
78 rv = h->freelists[log2_pages];
79 h->freelists[log2_pages] = rv->next_free;
84 * Latest gcc complains that the length arg is zero
85 * if we replace (1<<log2_pages) with vec_len(rv).
88 memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
93 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
96 ASSERT (h->writer_lock[0]);
98 ASSERT (vec_len (h->freelists) > log2_pages);
100 v->next_free = h->freelists[log2_pages];
101 h->freelists[log2_pages] = v;
105 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
107 BVT (clib_bihash_value) * v;
108 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
110 BVT (clib_bihash_value) * working_copy;
111 u32 thread_index = os_get_thread_index ();
112 int log2_working_copy_length;
114 if (thread_index >= vec_len (h->working_copies))
116 oldheap = clib_mem_set_heap (h->mheap);
117 vec_validate (h->working_copies, thread_index);
118 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
119 clib_mem_set_heap (oldheap);
123 * working_copies are per-cpu so that near-simultaneous
124 * updates from multiple threads will not result in sporadic, spurious
127 working_copy = h->working_copies[thread_index];
128 log2_working_copy_length = h->working_copy_lengths[thread_index];
130 h->saved_bucket.as_u64 = b->as_u64;
131 oldheap = clib_mem_set_heap (h->mheap);
133 if (b->log2_pages > log2_working_copy_length)
136 clib_mem_free (working_copy);
138 working_copy = clib_mem_alloc_aligned
139 (sizeof (working_copy[0]) * (1 << b->log2_pages),
140 CLIB_CACHE_LINE_BYTES);
141 h->working_copy_lengths[thread_index] = b->log2_pages;
142 h->working_copies[thread_index] = working_copy;
145 clib_mem_set_heap (oldheap);
147 /* Lock the bucket... */
148 while (BV (clib_bihash_lock_bucket) (b) == 0)
151 v = BV (clib_bihash_get_value) (h, b->offset);
153 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
154 working_bucket.as_u64 = b->as_u64;
155 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
156 CLIB_MEMORY_BARRIER ();
157 b->as_u64 = working_bucket.as_u64;
158 h->working_copies[thread_index] = working_copy;
162 BVT (clib_bihash_value) *
163 BV (split_and_rehash)
164 (BVT (clib_bihash) * h,
165 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
168 BVT (clib_bihash_value) * new_values, *new_v;
169 int i, j, length_in_kvs;
171 new_values = BV (value_alloc) (h, new_log2_pages);
172 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
174 for (i = 0; i < length_in_kvs; i++)
178 /* Entry not in use? Forget it */
179 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
182 /* rehash the item onto its new home-page */
183 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
184 new_hash >>= h->log2_nbuckets;
185 new_hash &= (1 << new_log2_pages) - 1;
186 new_v = &new_values[new_hash];
188 /* Across the new home-page */
189 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
192 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
194 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
195 sizeof (new_v->kvp[j]));
199 /* Crap. Tell caller to try again */
200 BV (value_free) (h, new_values, new_log2_pages);
209 BVT (clib_bihash_value) *
210 BV (split_and_rehash_linear)
211 (BVT (clib_bihash) * h,
212 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
215 BVT (clib_bihash_value) * new_values;
216 int i, j, new_length, old_length;
218 new_values = BV (value_alloc) (h, new_log2_pages);
219 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
220 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
223 /* Across the old value array */
224 for (i = 0; i < old_length; i++)
226 /* Find a free slot in the new linear scan bucket */
227 for (; j < new_length; j++)
229 /* Old value not in use? Forget it. */
230 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
233 /* New value should never be in use */
234 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
236 /* Copy the old value and move along */
237 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
238 sizeof (new_values->kvp[j]));
243 /* This should never happen... */
244 clib_warning ("BUG: linear rehash failed!");
245 BV (value_free) (h, new_values, new_log2_pages);
253 int BV (clib_bihash_add_del)
254 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
257 BVT (clib_bihash_bucket) * b, tmp_b;
258 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
262 u32 new_log2_pages, old_log2_pages;
263 u32 thread_index = os_get_thread_index ();
264 int mark_bucket_linear;
267 hash = BV (clib_bihash_hash) (add_v);
269 bucket_index = hash & (h->nbuckets - 1);
270 b = &h->buckets[bucket_index];
272 hash >>= h->log2_nbuckets;
274 tmp_b.linear_search = 0;
276 while (__sync_lock_test_and_set (h->writer_lock, 1))
279 /* First elt in the bucket? */
288 v = BV (value_alloc) (h, 0);
292 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
295 b->as_u64 = tmp_b.as_u64;
299 /* Note: this leaves the cache disabled */
300 BV (make_working_copy) (h, b);
302 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
304 limit = BIHASH_KVP_PER_PAGE;
305 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
306 if (b->linear_search)
307 limit <<= b->log2_pages;
312 * For obvious (in hindsight) reasons, see if we're supposed to
313 * replace an existing key, then look for an empty slot.
315 for (i = 0; i < limit; i++)
317 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
319 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
320 CLIB_MEMORY_BARRIER ();
321 /* Restore the previous (k,v) pairs */
322 b->as_u64 = h->saved_bucket.as_u64;
326 for (i = 0; i < limit; i++)
328 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
330 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
331 CLIB_MEMORY_BARRIER ();
332 b->as_u64 = h->saved_bucket.as_u64;
337 /* no room at the inn... split case... */
341 for (i = 0; i < limit; i++)
343 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
345 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
346 CLIB_MEMORY_BARRIER ();
347 if (PREDICT_TRUE (h->saved_bucket.refcnt > 1))
349 h->saved_bucket.refcnt -= 1;
350 b->as_u64 = h->saved_bucket.as_u64;
356 goto free_old_bucket;
361 b->as_u64 = h->saved_bucket.as_u64;
365 old_log2_pages = h->saved_bucket.log2_pages;
366 new_log2_pages = old_log2_pages + 1;
367 mark_bucket_linear = 0;
369 working_copy = h->working_copies[thread_index];
372 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
379 /* Try re-splitting. If that fails, fall back to linear search */
380 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
386 /* pinned collisions, use linear search */
388 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
390 mark_bucket_linear = 1;
394 /* Try to add the new entry */
396 new_hash = BV (clib_bihash_hash) (add_v);
397 limit = BIHASH_KVP_PER_PAGE;
398 if (mark_bucket_linear)
399 limit <<= new_log2_pages;
400 new_hash >>= h->log2_nbuckets;
401 new_hash &= (1 << new_log2_pages) - 1;
402 new_v += mark_bucket_linear ? 0 : new_hash;
404 for (i = 0; i < limit; i++)
406 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
408 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
413 /* Crap. Try again */
414 BV (value_free) (h, save_new_v, new_log2_pages);
416 * If we've already doubled the size of the bucket once,
417 * fall back to linear search now.
425 tmp_b.log2_pages = new_log2_pages;
426 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
427 tmp_b.linear_search = mark_bucket_linear;
428 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
432 CLIB_MEMORY_BARRIER ();
433 b->as_u64 = tmp_b.as_u64;
434 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
436 BV (value_free) (h, v, h->saved_bucket.log2_pages);
439 BV (clib_bihash_reset_cache) (b);
440 BV (clib_bihash_unlock_bucket) (b);
441 CLIB_MEMORY_BARRIER ();
442 h->writer_lock[0] = 0;
446 int BV (clib_bihash_search)
447 (BVT (clib_bihash) * h,
448 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
452 BVT (clib_bihash_value) * v;
453 #if BIHASH_KVP_CACHE_SIZE > 0
454 BVT (clib_bihash_kv) * kvp;
456 BVT (clib_bihash_bucket) * b;
461 hash = BV (clib_bihash_hash) (search_key);
463 bucket_index = hash & (h->nbuckets - 1);
464 b = &h->buckets[bucket_index];
469 #if BIHASH_KVP_CACHE_SIZE > 0
470 /* Check the cache, if currently enabled */
471 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
473 limit = BIHASH_KVP_CACHE_SIZE;
475 for (i = 0; i < limit; i++)
477 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
487 hash >>= h->log2_nbuckets;
489 v = BV (clib_bihash_get_value) (h, b->offset);
490 limit = BIHASH_KVP_PER_PAGE;
491 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
492 if (PREDICT_FALSE (b->linear_search))
493 limit <<= b->log2_pages;
495 for (i = 0; i < limit; i++)
497 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
501 #if BIHASH_KVP_CACHE_SIZE > 0
503 /* Shut off the cache */
504 if (BV (clib_bihash_lock_bucket) (b))
506 cache_slot = BV (clib_bihash_get_lru) (b);
507 b->cache[cache_slot] = v->kvp[i];
508 BV (clib_bihash_update_lru) (b, cache_slot);
510 /* Reenable the cache */
511 BV (clib_bihash_unlock_bucket) (b);
521 u8 *BV (format_bihash_lru) (u8 * s, va_list * args)
523 #if BIHASH_KVP_SIZE > 0
525 BVT (clib_bihash_bucket) * b = va_arg (*args, BVT (clib_bihash_bucket) *);
526 u16 cache_lru = b->cache_lru;
528 s = format (s, "cache %s, order ", cache_lru & (1 << 15) ? "on" : "off");
530 for (i = 0; i < BIHASH_KVP_CACHE_SIZE; i++)
531 s = format (s, "[%d] ", ((cache_lru >> (3 * i)) & 7));
535 return format (s, "cache not configured");
540 BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b, u8 slot)
542 #if BIHASH_KVP_SIZE > 0
543 BV (clib_bihash_update_lru) (b, slot);
547 u8 *BV (format_bihash) (u8 * s, va_list * args)
549 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
550 int verbose = va_arg (*args, int);
551 BVT (clib_bihash_bucket) * b;
552 BVT (clib_bihash_value) * v;
554 u64 active_elements = 0;
555 u64 active_buckets = 0;
556 u64 linear_buckets = 0;
558 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
560 for (i = 0; i < h->nbuckets; i++)
566 s = format (s, "[%d]: empty\n", i);
572 if (b->linear_search)
577 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
578 b->offset, (1 << b->log2_pages), b->linear_search);
581 v = BV (clib_bihash_get_value) (h, b->offset);
582 for (j = 0; j < (1 << b->log2_pages); j++)
584 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
586 if (BV (clib_bihash_is_free) (&v->kvp[k]))
589 s = format (s, " %d: empty\n",
590 j * BIHASH_KVP_PER_PAGE + k);
597 s = format (s, " %d: %U\n",
598 j * BIHASH_KVP_PER_PAGE + k,
599 h->fmt_fn, &(v->kvp[k]));
603 s = format (s, " %d: %U\n",
604 j * BIHASH_KVP_PER_PAGE + k,
605 BV (format_bihash_kvp), &(v->kvp[k]));
614 s = format (s, " %lld active elements %lld active buckets\n",
615 active_elements, active_buckets);
616 s = format (s, " %d free lists\n", vec_len (h->freelists));
618 for (i = 0; i < vec_len (h->freelists); i++)
621 BVT (clib_bihash_value) * free_elt;
623 free_elt = h->freelists[i];
627 free_elt = free_elt->next_free;
630 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
633 s = format (s, " %lld linear search buckets\n", linear_buckets);
634 s = format (s, " %lld cache hits, %lld cache misses\n",
635 h->cache_hits, h->cache_misses);
637 s = format (s, " mheap: %U", format_mheap, h->mheap, 0 /* verbose */ );
641 void BV (clib_bihash_foreach_key_value_pair)
642 (BVT (clib_bihash) * h, void *callback, void *arg)
645 BVT (clib_bihash_bucket) * b;
646 BVT (clib_bihash_value) * v;
647 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
649 for (i = 0; i < h->nbuckets; i++)
655 v = BV (clib_bihash_get_value) (h, b->offset);
656 for (j = 0; j < (1 << b->log2_pages); j++)
658 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
660 if (BV (clib_bihash_is_free) (&v->kvp[k]))
663 (*fp) (&v->kvp[k], arg);
673 * fd.io coding-style-patch-verification: ON
676 * eval: (c-set-style "gnu")