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);
46 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
48 mheap_free (h->mheap);
49 memset (h, 0, sizeof (*h));
53 BVT (clib_bihash_value) *
54 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
56 BVT (clib_bihash_value) * rv = 0;
59 ASSERT (h->writer_lock[0]);
60 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
62 oldheap = clib_mem_set_heap (h->mheap);
64 vec_validate (h->freelists, log2_pages);
65 rv = clib_mem_alloc_aligned ((sizeof (*rv) * (1 << log2_pages)),
66 CLIB_CACHE_LINE_BYTES);
67 clib_mem_set_heap (oldheap);
70 rv = h->freelists[log2_pages];
71 h->freelists[log2_pages] = rv->next_free;
76 * Latest gcc complains that the length arg is zero
77 * if we replace (1<<log2_pages) with vec_len(rv).
80 memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
85 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
88 ASSERT (h->writer_lock[0]);
90 ASSERT (vec_len (h->freelists) > log2_pages);
92 v->next_free = h->freelists[log2_pages];
93 h->freelists[log2_pages] = v;
97 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
99 BVT (clib_bihash_value) * v;
100 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
102 BVT (clib_bihash_value) * working_copy;
103 u32 thread_index = os_get_thread_index ();
104 int log2_working_copy_length;
106 if (thread_index >= vec_len (h->working_copies))
108 oldheap = clib_mem_set_heap (h->mheap);
109 vec_validate (h->working_copies, thread_index);
110 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
111 clib_mem_set_heap (oldheap);
115 * working_copies are per-cpu so that near-simultaneous
116 * updates from multiple threads will not result in sporadic, spurious
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;
123 oldheap = clib_mem_set_heap (h->mheap);
125 if (b->log2_pages > log2_working_copy_length)
128 clib_mem_free (working_copy);
130 working_copy = clib_mem_alloc_aligned
131 (sizeof (working_copy[0]) * (1 << b->log2_pages),
132 CLIB_CACHE_LINE_BYTES);
133 h->working_copy_lengths[thread_index] = b->log2_pages;
134 h->working_copies[thread_index] = working_copy;
137 clib_mem_set_heap (oldheap);
139 /* Lock the bucket... */
140 while (BV (clib_bihash_lock_bucket) (b) == 0)
143 v = BV (clib_bihash_get_value) (h, b->offset);
145 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
146 working_bucket.as_u64 = b->as_u64;
147 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
148 CLIB_MEMORY_BARRIER ();
149 b->as_u64 = working_bucket.as_u64;
150 h->working_copies[thread_index] = working_copy;
154 BVT (clib_bihash_value) *
155 BV (split_and_rehash)
156 (BVT (clib_bihash) * h,
157 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
160 BVT (clib_bihash_value) * new_values, *new_v;
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++)
170 /* Entry not in use? Forget it */
171 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
174 /* rehash the item onto its new home-page */
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];
180 /* Across the new home-page */
181 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
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]));
191 /* Crap. Tell caller to try again */
192 BV (value_free) (h, new_values, new_log2_pages);
201 BVT (clib_bihash_value) *
202 BV (split_and_rehash_linear)
203 (BVT (clib_bihash) * h,
204 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
207 BVT (clib_bihash_value) * new_values;
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;
215 /* Across the old value array */
216 for (i = 0; i < old_length; i++)
218 /* Find a free slot in the new linear scan bucket */
219 for (; j < new_length; j++)
221 /* Old value not in use? Forget it. */
222 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
225 /* New value should never be in use */
226 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
228 /* Copy the old value and move along */
229 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
230 sizeof (new_values->kvp[j]));
235 /* This should never happen... */
236 clib_warning ("BUG: linear rehash failed!");
237 BV (value_free) (h, new_values, new_log2_pages);
245 int BV (clib_bihash_add_del)
246 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
249 BVT (clib_bihash_bucket) * b, tmp_b;
250 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
254 u32 new_log2_pages, old_log2_pages;
255 u32 thread_index = os_get_thread_index ();
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))
271 /* First elt in the bucket? */
280 v = BV (value_alloc) (h, 0);
284 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
286 b->as_u64 = tmp_b.as_u64;
290 /* Note: this leaves the cache disabled */
291 BV (make_working_copy) (h, b);
293 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
295 limit = BIHASH_KVP_PER_PAGE;
296 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
297 if (b->linear_search)
298 limit <<= b->log2_pages;
303 * For obvious (in hindsight) reasons, see if we're supposed to
304 * replace an existing key, then look for an empty slot.
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));
311 CLIB_MEMORY_BARRIER ();
312 /* Restore the previous (k,v) pairs */
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));
322 CLIB_MEMORY_BARRIER ();
323 b->as_u64 = h->saved_bucket.as_u64;
327 /* no room at the inn... split case... */
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)));
336 CLIB_MEMORY_BARRIER ();
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];
353 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
360 /* Try re-splitting. If that fails, fall back to linear search */
361 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
367 /* pinned collisions, use linear search */
369 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
371 mark_bucket_linear = 1;
375 /* Try to add the new entry */
377 new_hash = BV (clib_bihash_hash) (add_v);
378 limit = BIHASH_KVP_PER_PAGE;
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));
394 /* Crap. Try again */
395 BV (value_free) (h, save_new_v, new_log2_pages);
397 * If we've already doubled the size of the bucket once,
398 * fall back to linear search now.
406 /* Keep track of the number of linear-scan buckets */
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;
411 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
412 tmp_b.linear_search = mark_bucket_linear;
414 CLIB_MEMORY_BARRIER ();
415 b->as_u64 = tmp_b.as_u64;
416 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
417 BV (value_free) (h, v, old_log2_pages);
420 BV (clib_bihash_reset_cache) (b);
421 BV (clib_bihash_unlock_bucket) (b);
422 CLIB_MEMORY_BARRIER ();
423 h->writer_lock[0] = 0;
427 int BV (clib_bihash_search)
428 (BVT (clib_bihash) * h,
429 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
433 BVT (clib_bihash_value) * v;
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
451 /* Check the cache, if currently enabled */
452 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
454 limit = BIHASH_KVP_CACHE_SIZE;
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;
470 v = BV (clib_bihash_get_value) (h, b->offset);
471 limit = BIHASH_KVP_PER_PAGE;
472 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
473 if (PREDICT_FALSE (b->linear_search))
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
484 /* Shut off the cache */
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);
491 /* Reenable the cache */
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");
511 for (i = 0; i < BIHASH_KVP_CACHE_SIZE; i++)
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;
533 BVT (clib_bihash_value) * v;
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);
555 v = BV (clib_bihash_get_value) (h, b->offset);
556 for (j = 0; j < (1 << b->log2_pages); j++)
558 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
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);
580 s = format (s, " %d free lists\n", vec_len (h->freelists));
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);
587 void BV (clib_bihash_foreach_key_value_pair)
588 (BVT (clib_bihash) * h, void *callback, void *arg)
591 BVT (clib_bihash_bucket) * b;
592 BVT (clib_bihash_value) * v;
593 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
595 for (i = 0; i < h->nbuckets; i++)
601 v = BV (clib_bihash_get_value) (h, b->offset);
602 for (j = 0; j < (1 << b->log2_pages); j++)
604 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
606 if (BV (clib_bihash_is_free) (&v->kvp[k]))
609 (*fp) (&v->kvp[k], arg);
619 * fd.io coding-style-patch-verification: ON
622 * eval: (c-set-style "gnu")