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);
39 for (i = 0; i < nbuckets; i++)
40 BV (clib_bihash_reset_cache) (h->buckets + i);
42 clib_mem_set_heap (oldheap);
45 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
47 mheap_free (h->mheap);
48 memset (h, 0, sizeof (*h));
52 BVT (clib_bihash_value) *
53 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
55 BVT (clib_bihash_value) * rv = 0;
58 ASSERT (h->writer_lock[0]);
59 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
61 oldheap = clib_mem_set_heap (h->mheap);
63 vec_validate (h->freelists, log2_pages);
64 rv = clib_mem_alloc_aligned ((sizeof (*rv) * (1 << log2_pages)),
65 CLIB_CACHE_LINE_BYTES);
66 clib_mem_set_heap (oldheap);
69 rv = h->freelists[log2_pages];
70 h->freelists[log2_pages] = rv->next_free;
75 * Latest gcc complains that the length arg is zero
76 * if we replace (1<<log2_pages) with vec_len(rv).
79 memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
84 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
87 ASSERT (h->writer_lock[0]);
89 ASSERT (vec_len (h->freelists) > log2_pages);
91 v->next_free = h->freelists[log2_pages];
92 h->freelists[log2_pages] = v;
96 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
98 BVT (clib_bihash_value) * v;
99 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
101 BVT (clib_bihash_value) * working_copy;
102 u32 thread_index = os_get_thread_index ();
103 int log2_working_copy_length;
105 if (thread_index >= vec_len (h->working_copies))
107 oldheap = clib_mem_set_heap (h->mheap);
108 vec_validate (h->working_copies, thread_index);
109 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
110 clib_mem_set_heap (oldheap);
114 * working_copies are per-cpu so that near-simultaneous
115 * updates from multiple threads will not result in sporadic, spurious
118 working_copy = h->working_copies[thread_index];
119 log2_working_copy_length = h->working_copy_lengths[thread_index];
121 h->saved_bucket.as_u64 = b->as_u64;
122 oldheap = clib_mem_set_heap (h->mheap);
124 if (b->log2_pages > log2_working_copy_length)
127 clib_mem_free (working_copy);
129 working_copy = clib_mem_alloc_aligned
130 (sizeof (working_copy[0]) * (1 << b->log2_pages),
131 CLIB_CACHE_LINE_BYTES);
132 h->working_copy_lengths[thread_index] = b->log2_pages;
133 h->working_copies[thread_index] = working_copy;
136 clib_mem_set_heap (oldheap);
138 /* Turn off the cache */
139 BV (clib_bihash_cache_enable_disable) (b, 0);
141 v = BV (clib_bihash_get_value) (h, b->offset);
143 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
144 working_bucket.as_u64 = b->as_u64;
145 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
146 CLIB_MEMORY_BARRIER ();
147 b->as_u64 = working_bucket.as_u64;
148 h->working_copies[thread_index] = working_copy;
152 BVT (clib_bihash_value) *
153 BV (split_and_rehash)
154 (BVT (clib_bihash) * h,
155 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
158 BVT (clib_bihash_value) * new_values, *new_v;
159 int i, j, length_in_kvs;
161 new_values = BV (value_alloc) (h, new_log2_pages);
162 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
164 for (i = 0; i < length_in_kvs; i++)
168 /* Entry not in use? Forget it */
169 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
172 /* rehash the item onto its new home-page */
173 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
174 new_hash >>= h->log2_nbuckets;
175 new_hash &= (1 << new_log2_pages) - 1;
176 new_v = &new_values[new_hash];
178 /* Across the new home-page */
179 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
182 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
184 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
185 sizeof (new_v->kvp[j]));
189 /* Crap. Tell caller to try again */
190 BV (value_free) (h, new_values, new_log2_pages);
199 BVT (clib_bihash_value) *
200 BV (split_and_rehash_linear)
201 (BVT (clib_bihash) * h,
202 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
205 BVT (clib_bihash_value) * new_values;
206 int i, j, new_length, old_length;
208 new_values = BV (value_alloc) (h, new_log2_pages);
209 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
210 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
213 /* Across the old value array */
214 for (i = 0; i < old_length; i++)
216 /* Find a free slot in the new linear scan bucket */
217 for (; j < new_length; j++)
219 /* Old value not in use? Forget it. */
220 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
223 /* New value should never be in use */
224 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
226 /* Copy the old value and move along */
227 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
228 sizeof (new_values->kvp[j]));
233 /* This should never happen... */
234 clib_warning ("BUG: linear rehash failed!");
235 BV (value_free) (h, new_values, new_log2_pages);
243 int BV (clib_bihash_add_del)
244 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
247 BVT (clib_bihash_bucket) * b, tmp_b;
248 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
252 u32 new_log2_pages, old_log2_pages;
253 u32 thread_index = os_get_thread_index ();
254 int mark_bucket_linear;
257 hash = BV (clib_bihash_hash) (add_v);
259 bucket_index = hash & (h->nbuckets - 1);
260 b = &h->buckets[bucket_index];
262 hash >>= h->log2_nbuckets;
264 tmp_b.linear_search = 0;
266 while (__sync_lock_test_and_set (h->writer_lock, 1))
269 /* First elt in the bucket? */
278 v = BV (value_alloc) (h, 0);
282 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
284 b->as_u64 = tmp_b.as_u64;
288 /* Note: this leaves the cache disabled */
289 BV (make_working_copy) (h, b);
291 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
293 limit = BIHASH_KVP_PER_PAGE;
294 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
295 if (b->linear_search)
296 limit <<= b->log2_pages;
301 * For obvious (in hindsight) reasons, see if we're supposed to
302 * replace an existing key, then look for an empty slot.
304 for (i = 0; i < limit; i++)
306 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
308 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
309 CLIB_MEMORY_BARRIER ();
310 /* Restore the previous (k,v) pairs */
311 b->as_u64 = h->saved_bucket.as_u64;
315 for (i = 0; i < limit; i++)
317 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
319 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
320 CLIB_MEMORY_BARRIER ();
321 b->as_u64 = h->saved_bucket.as_u64;
325 /* no room at the inn... split case... */
329 for (i = 0; i < limit; i++)
331 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
333 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
334 CLIB_MEMORY_BARRIER ();
335 b->as_u64 = h->saved_bucket.as_u64;
340 b->as_u64 = h->saved_bucket.as_u64;
344 old_log2_pages = h->saved_bucket.log2_pages;
345 new_log2_pages = old_log2_pages + 1;
346 mark_bucket_linear = 0;
348 working_copy = h->working_copies[thread_index];
351 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
358 /* Try re-splitting. If that fails, fall back to linear search */
359 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
365 /* pinned collisions, use linear search */
367 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
369 mark_bucket_linear = 1;
373 /* Try to add the new entry */
375 new_hash = BV (clib_bihash_hash) (add_v);
376 limit = BIHASH_KVP_PER_PAGE;
377 if (mark_bucket_linear)
378 limit <<= new_log2_pages;
379 new_hash >>= h->log2_nbuckets;
380 new_hash &= (1 << new_log2_pages) - 1;
381 new_v += mark_bucket_linear ? 0 : new_hash;
383 for (i = 0; i < limit; i++)
385 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
387 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
392 /* Crap. Try again */
393 BV (value_free) (h, save_new_v, new_log2_pages);
395 * If we've already doubled the size of the bucket once,
396 * fall back to linear search now.
404 /* Keep track of the number of linear-scan buckets */
405 if (tmp_b.linear_search ^ mark_bucket_linear)
406 h->linear_buckets += (mark_bucket_linear == 1) ? 1 : -1;
408 tmp_b.log2_pages = new_log2_pages;
409 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
410 tmp_b.linear_search = mark_bucket_linear;
412 CLIB_MEMORY_BARRIER ();
413 b->as_u64 = tmp_b.as_u64;
414 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
415 BV (value_free) (h, v, old_log2_pages);
418 BV (clib_bihash_reset_cache) (b);
419 BV (clib_bihash_cache_enable_disable) (b, 1 /* enable */ );
420 CLIB_MEMORY_BARRIER ();
421 h->writer_lock[0] = 0;
425 int BV (clib_bihash_search)
426 (BVT (clib_bihash) * h,
427 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
431 BVT (clib_bihash_value) * v;
432 BVT (clib_bihash_kv) * kvp;
433 BVT (clib_bihash_bucket) * b;
438 hash = BV (clib_bihash_hash) (search_key);
440 bucket_index = hash & (h->nbuckets - 1);
441 b = &h->buckets[bucket_index];
446 /* Check the cache, if currently enabled */
447 if (PREDICT_TRUE (b->cache_lru & (1 << 15)))
449 limit = BIHASH_KVP_CACHE_SIZE;
451 for (i = 0; i < limit; i++)
453 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
462 hash >>= h->log2_nbuckets;
464 v = BV (clib_bihash_get_value) (h, b->offset);
465 limit = BIHASH_KVP_PER_PAGE;
466 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
467 if (PREDICT_FALSE (b->linear_search))
468 limit <<= b->log2_pages;
470 for (i = 0; i < limit; i++)
472 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
477 /* Shut off the cache */
478 BV (clib_bihash_cache_enable_disable) (b, 0);
479 CLIB_MEMORY_BARRIER ();
481 cache_slot = BV (clib_bihash_get_lru) (b);
482 b->cache[cache_slot] = v->kvp[i];
483 BV (clib_bihash_update_lru) (b, cache_slot);
485 /* Reenable the cache */
486 BV (clib_bihash_cache_enable_disable) (b, 1);
494 u8 *BV (format_bihash_lru) (u8 * s, va_list * args)
497 BVT (clib_bihash_bucket) * b = va_arg (*args, BVT (clib_bihash_bucket) *);
498 u16 cache_lru = b->cache_lru;
500 s = format (s, "cache %s, order ", cache_lru & (1 << 15) ? "on" : "off");
502 for (i = 0; i < BIHASH_KVP_CACHE_SIZE; i++)
503 s = format (s, "[%d] ", ((cache_lru >> (3 * i)) & 7));
508 BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b, u8 slot)
510 BV (clib_bihash_update_lru) (b, slot);
513 u8 *BV (format_bihash) (u8 * s, va_list * args)
515 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
516 int verbose = va_arg (*args, int);
517 BVT (clib_bihash_bucket) * b;
518 BVT (clib_bihash_value) * v;
520 u64 active_elements = 0;
522 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
524 for (i = 0; i < h->nbuckets; i++)
530 s = format (s, "[%d]: empty\n", i);
536 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
537 b->offset, (1 << b->log2_pages), b->linear_search);
540 v = BV (clib_bihash_get_value) (h, b->offset);
541 for (j = 0; j < (1 << b->log2_pages); j++)
543 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
545 if (BV (clib_bihash_is_free) (&v->kvp[k]))
548 s = format (s, " %d: empty\n",
549 j * BIHASH_KVP_PER_PAGE + k);
554 s = format (s, " %d: %U\n",
555 j * BIHASH_KVP_PER_PAGE + k,
556 BV (format_bihash_kvp), &(v->kvp[k]));
564 s = format (s, " %lld active elements\n", active_elements);
565 s = format (s, " %d free lists\n", vec_len (h->freelists));
566 s = format (s, " %d linear search buckets\n", h->linear_buckets);
567 s = format (s, " %lld cache hits, %lld cache misses\n",
568 h->cache_hits, h->cache_misses);
572 void BV (clib_bihash_foreach_key_value_pair)
573 (BVT (clib_bihash) * h, void *callback, void *arg)
576 BVT (clib_bihash_bucket) * b;
577 BVT (clib_bihash_value) * v;
578 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
580 for (i = 0; i < h->nbuckets; i++)
586 v = BV (clib_bihash_get_value) (h, b->offset);
587 for (j = 0; j < (1 << b->log2_pages); j++)
589 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
591 if (BV (clib_bihash_is_free) (&v->kvp[k]))
594 (*fp) (&v->kvp[k], arg);
604 * fd.io coding-style-patch-verification: ON
607 * eval: (c-set-style "gnu")