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 vec_validate_aligned (rv, (1 << log2_pages) - 1, CLIB_CACHE_LINE_BYTES);
59 clib_mem_set_heap (oldheap);
62 rv = h->freelists[log2_pages];
63 h->freelists[log2_pages] = rv->next_free;
67 ASSERT (vec_len (rv) == (1 << log2_pages));
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)
82 ASSERT (h->writer_lock[0]);
84 log2_pages = min_log2 (vec_len (v));
86 ASSERT (vec_len (h->freelists) > log2_pages);
88 v->next_free = h->freelists[log2_pages];
89 h->freelists[log2_pages] = v;
93 BV (make_working_copy) (BVT (clib_bihash) * h, clib_bihash_bucket_t * b)
95 BVT (clib_bihash_value) * v;
96 clib_bihash_bucket_t working_bucket __attribute__ ((aligned (8)));
98 BVT (clib_bihash_value) * working_copy;
99 u32 cpu_number = os_get_cpu_number ();
101 if (cpu_number >= vec_len (h->working_copies))
103 oldheap = clib_mem_set_heap (h->mheap);
104 vec_validate (h->working_copies, cpu_number);
105 clib_mem_set_heap (oldheap);
109 * working_copies are per-cpu so that near-simultaneous
110 * updates from multiple threads will not result in sporadic, spurious
113 working_copy = h->working_copies[cpu_number];
115 h->saved_bucket.as_u64 = b->as_u64;
116 oldheap = clib_mem_set_heap (h->mheap);
118 if ((1 << b->log2_pages) > vec_len (working_copy))
120 vec_validate_aligned (working_copy, (1 << b->log2_pages) - 1,
122 h->working_copies[cpu_number] = working_copy;
125 _vec_len (working_copy) = 1 << b->log2_pages;
126 clib_mem_set_heap (oldheap);
128 v = BV (clib_bihash_get_value) (h, b->offset);
130 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
131 working_bucket.as_u64 = b->as_u64;
132 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
133 CLIB_MEMORY_BARRIER ();
134 b->as_u64 = working_bucket.as_u64;
135 h->working_copies[cpu_number] = working_copy;
139 BVT (clib_bihash_value) *
140 BV (split_and_rehash)
141 (BVT (clib_bihash) * h,
142 BVT (clib_bihash_value) * old_values, u32 new_log2_pages)
144 BVT (clib_bihash_value) * new_values, *v, *new_v;
147 new_values = BV (value_alloc) (h, new_log2_pages);
150 for (i = 0; i < vec_len (old_values); i++)
154 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
156 if (BV (clib_bihash_is_free) (&(v->kvp[j])) == 0)
158 new_hash = BV (clib_bihash_hash) (&(v->kvp[j]));
159 new_hash >>= h->log2_nbuckets;
160 new_hash &= (1 << new_log2_pages) - 1;
162 new_v = &new_values[new_hash];
164 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
166 if (BV (clib_bihash_is_free) (&(new_v->kvp[k])))
168 clib_memcpy (&(new_v->kvp[k]), &(v->kvp[j]),
169 sizeof (new_v->kvp[k]));
173 /* Crap. Tell caller to try again */
174 BV (value_free) (h, new_values);
185 int BV (clib_bihash_add_del)
186 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
189 clib_bihash_bucket_t *b, tmp_b;
190 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
196 u32 cpu_number = os_get_cpu_number ();
198 hash = BV (clib_bihash_hash) (add_v);
200 bucket_index = hash & (h->nbuckets - 1);
201 b = &h->buckets[bucket_index];
203 hash >>= h->log2_nbuckets;
205 while (__sync_lock_test_and_set (h->writer_lock, 1))
208 /* First elt in the bucket? */
217 v = BV (value_alloc) (h, 0);
220 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
222 b->as_u64 = tmp_b.as_u64;
226 BV (make_working_copy) (h, b);
228 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
229 value_index = hash & ((1 << h->saved_bucket.log2_pages) - 1);
235 * For obvious (in hindsight) reasons, see if we're supposed to
236 * replace an existing key, then look for an empty slot.
238 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
240 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
242 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
243 CLIB_MEMORY_BARRIER ();
244 /* Restore the previous (k,v) pairs */
245 b->as_u64 = h->saved_bucket.as_u64;
249 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
251 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
253 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
254 CLIB_MEMORY_BARRIER ();
255 b->as_u64 = h->saved_bucket.as_u64;
259 /* no room at the inn... split case... */
263 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
265 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
267 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
268 CLIB_MEMORY_BARRIER ();
269 b->as_u64 = h->saved_bucket.as_u64;
274 b->as_u64 = h->saved_bucket.as_u64;
278 new_log2_pages = h->saved_bucket.log2_pages + 1;
281 working_copy = h->working_copies[cpu_number];
282 new_v = BV (split_and_rehash) (h, working_copy, new_log2_pages);
289 /* Try to add the new entry */
291 new_hash = BV (clib_bihash_hash) (add_v);
292 new_hash >>= h->log2_nbuckets;
293 new_hash &= (1 << min_log2 (vec_len (new_v))) - 1;
296 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
298 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
300 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
304 /* Crap. Try again */
306 BV (value_free) (h, save_new_v);
310 tmp_b.log2_pages = min_log2 (vec_len (save_new_v));
311 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
312 CLIB_MEMORY_BARRIER ();
313 b->as_u64 = tmp_b.as_u64;
314 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
315 BV (value_free) (h, v);
318 CLIB_MEMORY_BARRIER ();
319 h->writer_lock[0] = 0;
323 int BV (clib_bihash_search)
324 (const BVT (clib_bihash) * h,
325 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
330 BVT (clib_bihash_value) * v;
331 clib_bihash_bucket_t *b;
336 hash = BV (clib_bihash_hash) (search_key);
338 bucket_index = hash & (h->nbuckets - 1);
339 b = &h->buckets[bucket_index];
344 hash >>= h->log2_nbuckets;
346 v = BV (clib_bihash_get_value) (h, b->offset);
347 value_index = hash & ((1 << b->log2_pages) - 1);
350 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
352 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
361 u8 *BV (format_bihash) (u8 * s, va_list * args)
363 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
364 int verbose = va_arg (*args, int);
365 clib_bihash_bucket_t *b;
366 BVT (clib_bihash_value) * v;
368 u64 active_elements = 0;
370 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
372 for (i = 0; i < h->nbuckets; i++)
378 s = format (s, "[%d]: empty\n", i);
384 s = format (s, "[%d]: heap offset %d, len %d\n", i,
385 b->offset, (1 << b->log2_pages));
388 v = BV (clib_bihash_get_value) (h, b->offset);
389 for (j = 0; j < (1 << b->log2_pages); j++)
391 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
393 if (BV (clib_bihash_is_free) (&v->kvp[k]))
396 s = format (s, " %d: empty\n",
397 j * BIHASH_KVP_PER_PAGE + k);
402 s = format (s, " %d: %U\n",
403 j * BIHASH_KVP_PER_PAGE + k,
404 BV (format_bihash_kvp), &(v->kvp[k]));
412 s = format (s, " %lld active elements\n", active_elements);
413 s = format (s, " %d free lists\n", vec_len (h->freelists));
418 void BV (clib_bihash_foreach_key_value_pair)
419 (BVT (clib_bihash) * h, void *callback, void *arg)
422 clib_bihash_bucket_t *b;
423 BVT (clib_bihash_value) * v;
424 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
426 for (i = 0; i < h->nbuckets; i++)
432 v = BV (clib_bihash_get_value) (h, b->offset);
433 for (j = 0; j < (1 << b->log2_pages); j++)
435 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
437 if (BV (clib_bihash_is_free) (&v->kvp[k]))
440 (*fp) (&v->kvp[k], arg);
450 * fd.io coding-style-patch-verification: ON
453 * eval: (c-set-style "gnu")