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)
23 nbuckets = 1 << (max_log2 (nbuckets));
25 h->name = (u8 *) name;
26 h->nbuckets = nbuckets;
27 h->log2_nbuckets = max_log2 (nbuckets);
29 h->mheap = mheap_alloc (0 /* use VM */ , memory_size);
31 oldheap = clib_mem_set_heap (h->mheap);
32 vec_validate_aligned (h->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES);
33 h->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
34 CLIB_CACHE_LINE_BYTES);
36 clib_mem_set_heap (oldheap);
39 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
41 mheap_free (h->mheap);
42 memset (h, 0, sizeof (*h));
46 BVT (clib_bihash_value) *
47 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
49 BVT (clib_bihash_value) * rv = 0;
52 ASSERT (h->writer_lock[0]);
53 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
55 oldheap = clib_mem_set_heap (h->mheap);
57 vec_validate (h->freelists, log2_pages);
58 rv = clib_mem_alloc_aligned ((sizeof (*rv) * (1 << log2_pages)),
59 CLIB_CACHE_LINE_BYTES);
60 clib_mem_set_heap (oldheap);
63 rv = h->freelists[log2_pages];
64 h->freelists[log2_pages] = rv->next_free;
69 * Latest gcc complains that the length arg is zero
70 * if we replace (1<<log2_pages) with vec_len(rv).
73 memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
78 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
81 ASSERT (h->writer_lock[0]);
83 ASSERT (vec_len (h->freelists) > log2_pages);
85 v->next_free = h->freelists[log2_pages];
86 h->freelists[log2_pages] = v;
90 BV (make_working_copy) (BVT (clib_bihash) * h, clib_bihash_bucket_t * b)
92 BVT (clib_bihash_value) * v;
93 clib_bihash_bucket_t working_bucket __attribute__ ((aligned (8)));
95 BVT (clib_bihash_value) * working_copy;
96 u32 thread_index = os_get_thread_index ();
97 int log2_working_copy_length;
99 if (thread_index >= vec_len (h->working_copies))
101 oldheap = clib_mem_set_heap (h->mheap);
102 vec_validate (h->working_copies, thread_index);
103 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
104 clib_mem_set_heap (oldheap);
108 * working_copies are per-cpu so that near-simultaneous
109 * updates from multiple threads will not result in sporadic, spurious
112 working_copy = h->working_copies[thread_index];
113 log2_working_copy_length = h->working_copy_lengths[thread_index];
115 h->saved_bucket.as_u64 = b->as_u64;
116 oldheap = clib_mem_set_heap (h->mheap);
118 if (b->log2_pages > log2_working_copy_length)
121 clib_mem_free (working_copy);
123 working_copy = clib_mem_alloc_aligned
124 (sizeof (working_copy[0]) * (1 << b->log2_pages),
125 CLIB_CACHE_LINE_BYTES);
126 h->working_copy_lengths[thread_index] = b->log2_pages;
127 h->working_copies[thread_index] = working_copy;
130 clib_mem_set_heap (oldheap);
132 v = BV (clib_bihash_get_value) (h, b->offset);
134 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
135 working_bucket.as_u64 = b->as_u64;
136 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
137 CLIB_MEMORY_BARRIER ();
138 b->as_u64 = working_bucket.as_u64;
139 h->working_copies[thread_index] = working_copy;
143 BVT (clib_bihash_value) *
144 BV (split_and_rehash)
145 (BVT (clib_bihash) * h,
146 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
149 BVT (clib_bihash_value) * new_values, *new_v;
150 int i, j, length_in_kvs;
152 new_values = BV (value_alloc) (h, new_log2_pages);
153 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
155 for (i = 0; i < length_in_kvs; i++)
159 /* Entry not in use? Forget it */
160 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
163 /* rehash the item onto its new home-page */
164 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
165 new_hash >>= h->log2_nbuckets;
166 new_hash &= (1 << new_log2_pages) - 1;
167 new_v = &new_values[new_hash];
169 /* Across the new home-page */
170 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
173 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
175 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
176 sizeof (new_v->kvp[j]));
180 /* Crap. Tell caller to try again */
181 BV (value_free) (h, new_values, new_log2_pages);
190 BVT (clib_bihash_value) *
191 BV (split_and_rehash_linear)
192 (BVT (clib_bihash) * h,
193 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
196 BVT (clib_bihash_value) * new_values;
197 int i, j, new_length, old_length;
199 new_values = BV (value_alloc) (h, new_log2_pages);
200 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
201 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
204 /* Across the old value array */
205 for (i = 0; i < old_length; i++)
207 /* Find a free slot in the new linear scan bucket */
208 for (; j < new_length; j++)
210 /* Old value not in use? Forget it. */
211 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
214 /* New value should never be in use */
215 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
217 /* Copy the old value and move along */
218 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
219 sizeof (new_values->kvp[j]));
224 /* This should never happen... */
225 clib_warning ("BUG: linear rehash failed!");
226 BV (value_free) (h, new_values, new_log2_pages);
234 int BV (clib_bihash_add_del)
235 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
238 clib_bihash_bucket_t *b, tmp_b;
239 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
243 u32 new_log2_pages, old_log2_pages;
244 u32 thread_index = os_get_thread_index ();
245 int mark_bucket_linear;
248 hash = BV (clib_bihash_hash) (add_v);
250 bucket_index = hash & (h->nbuckets - 1);
251 b = &h->buckets[bucket_index];
253 hash >>= h->log2_nbuckets;
255 tmp_b.linear_search = 0;
257 while (__sync_lock_test_and_set (h->writer_lock, 1))
260 /* First elt in the bucket? */
269 v = BV (value_alloc) (h, 0);
273 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
275 b->as_u64 = tmp_b.as_u64;
279 BV (make_working_copy) (h, b);
281 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
283 limit = BIHASH_KVP_PER_PAGE;
284 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
285 if (b->linear_search)
286 limit <<= b->log2_pages;
291 * For obvious (in hindsight) reasons, see if we're supposed to
292 * replace an existing key, then look for an empty slot.
294 for (i = 0; i < limit; i++)
296 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
298 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
299 CLIB_MEMORY_BARRIER ();
300 /* Restore the previous (k,v) pairs */
301 b->as_u64 = h->saved_bucket.as_u64;
305 for (i = 0; i < limit; i++)
307 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
309 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
310 CLIB_MEMORY_BARRIER ();
311 b->as_u64 = h->saved_bucket.as_u64;
315 /* no room at the inn... split case... */
319 for (i = 0; i < limit; i++)
321 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
323 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
324 CLIB_MEMORY_BARRIER ();
325 b->as_u64 = h->saved_bucket.as_u64;
330 b->as_u64 = h->saved_bucket.as_u64;
334 old_log2_pages = h->saved_bucket.log2_pages;
335 new_log2_pages = old_log2_pages + 1;
336 mark_bucket_linear = 0;
338 working_copy = h->working_copies[thread_index];
341 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
348 /* Try re-splitting. If that fails, fall back to linear search */
349 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
355 /* pinned collisions, use linear search */
357 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
359 mark_bucket_linear = 1;
363 /* Try to add the new entry */
365 new_hash = BV (clib_bihash_hash) (add_v);
366 limit = BIHASH_KVP_PER_PAGE;
367 if (mark_bucket_linear)
368 limit <<= new_log2_pages;
369 new_hash >>= h->log2_nbuckets;
370 new_hash &= (1 << new_log2_pages) - 1;
371 new_v += mark_bucket_linear ? 0 : new_hash;
373 for (i = 0; i < limit; i++)
375 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
377 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
382 /* Crap. Try again */
383 BV (value_free) (h, save_new_v, new_log2_pages);
385 * If we've already doubled the size of the bucket once,
386 * fall back to linear search now.
394 /* Keep track of the number of linear-scan buckets */
395 if (tmp_b.linear_search ^ mark_bucket_linear)
396 h->linear_buckets += (mark_bucket_linear == 1) ? 1 : -1;
398 tmp_b.log2_pages = new_log2_pages;
399 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
400 tmp_b.linear_search = mark_bucket_linear;
402 CLIB_MEMORY_BARRIER ();
403 b->as_u64 = tmp_b.as_u64;
404 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
405 BV (value_free) (h, v, old_log2_pages);
408 CLIB_MEMORY_BARRIER ();
409 h->writer_lock[0] = 0;
413 int BV (clib_bihash_search)
414 (const BVT (clib_bihash) * h,
415 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
419 BVT (clib_bihash_value) * v;
420 clib_bihash_bucket_t *b;
425 hash = BV (clib_bihash_hash) (search_key);
427 bucket_index = hash & (h->nbuckets - 1);
428 b = &h->buckets[bucket_index];
433 hash >>= h->log2_nbuckets;
435 v = BV (clib_bihash_get_value) (h, b->offset);
436 limit = BIHASH_KVP_PER_PAGE;
437 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
438 if (PREDICT_FALSE (b->linear_search))
439 limit <<= b->log2_pages;
441 for (i = 0; i < limit; i++)
443 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
452 u8 *BV (format_bihash) (u8 * s, va_list * args)
454 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
455 int verbose = va_arg (*args, int);
456 clib_bihash_bucket_t *b;
457 BVT (clib_bihash_value) * v;
459 u64 active_elements = 0;
461 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
463 for (i = 0; i < h->nbuckets; i++)
469 s = format (s, "[%d]: empty\n", i);
475 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
476 b->offset, (1 << b->log2_pages), b->linear_search);
479 v = BV (clib_bihash_get_value) (h, b->offset);
480 for (j = 0; j < (1 << b->log2_pages); j++)
482 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
484 if (BV (clib_bihash_is_free) (&v->kvp[k]))
487 s = format (s, " %d: empty\n",
488 j * BIHASH_KVP_PER_PAGE + k);
493 s = format (s, " %d: %U\n",
494 j * BIHASH_KVP_PER_PAGE + k,
495 BV (format_bihash_kvp), &(v->kvp[k]));
503 s = format (s, " %lld active elements\n", active_elements);
504 s = format (s, " %d free lists\n", vec_len (h->freelists));
505 s = format (s, " %d linear search buckets\n", h->linear_buckets);
510 void BV (clib_bihash_foreach_key_value_pair)
511 (BVT (clib_bihash) * h, void *callback, void *arg)
514 clib_bihash_bucket_t *b;
515 BVT (clib_bihash_value) * v;
516 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
518 for (i = 0; i < h->nbuckets; i++)
524 v = BV (clib_bihash_get_value) (h, b->offset);
525 for (j = 0; j < (1 << b->log2_pages); j++)
527 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
529 if (BV (clib_bihash_is_free) (&v->kvp[k]))
532 (*fp) (&v->kvp[k], arg);
542 * fd.io coding-style-patch-verification: ON
545 * eval: (c-set-style "gnu")