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 void BV(clib_bihash_init)
17 (BVT(clib_bihash) * h, char * name, u32 nbuckets,
22 nbuckets = 1 << (max_log2 (nbuckets));
25 h->nbuckets = nbuckets;
26 h->log2_nbuckets = max_log2 (nbuckets);
28 h->mheap = mheap_alloc (0 /* use VM */, memory_size);
30 oldheap = clib_mem_set_heap (h->mheap);
31 vec_validate_aligned (h->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES);
32 h->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
33 CLIB_CACHE_LINE_BYTES);
35 clib_mem_set_heap (oldheap);
38 void BV(clib_bihash_free) (BVT(clib_bihash) * h)
40 mheap_free (h->mheap);
41 memset (h, 0, sizeof (*h));
44 static BVT(clib_bihash_value) *
45 BV(value_alloc) (BVT(clib_bihash) * h, u32 log2_pages)
47 BVT(clib_bihash_value) * rv = 0;
50 ASSERT (h->writer_lock[0]);
51 if (log2_pages >= vec_len (h->freelists)
52 || h->freelists [log2_pages] == 0)
54 oldheap = clib_mem_set_heap (h->mheap);
56 vec_validate (h->freelists, log2_pages);
57 vec_validate_aligned (rv, (1<<log2_pages) - 1, CLIB_CACHE_LINE_BYTES);
58 clib_mem_set_heap (oldheap);
61 rv = h->freelists[log2_pages];
62 h->freelists[log2_pages] = rv->next_free;
66 ASSERT (vec_len(rv) == (1<<log2_pages));
68 * Latest gcc complains that the length arg is zero
69 * if we replace (1<<log2_pages) with vec_len(rv).
72 memset (rv, 0xff, sizeof (*rv) * (1<<log2_pages));
78 (BVT(clib_bihash) * h,
79 BVT(clib_bihash_value) * v)
83 ASSERT (h->writer_lock[0]);
85 log2_pages = min_log2(vec_len(v));
87 ASSERT(vec_len (h->freelists) > log2_pages);
89 v->next_free = h->freelists[log2_pages];
90 h->freelists[log2_pages] = v;
95 (BVT(clib_bihash) * h, clib_bihash_bucket_t * b)
97 BVT(clib_bihash_value) * v;
98 clib_bihash_bucket_t working_bucket __attribute__((aligned (8)));
100 BVT(clib_bihash_value) * working_copy;
101 u32 cpu_number = os_get_cpu_number();
103 if (cpu_number >= vec_len (h->working_copies))
105 oldheap = clib_mem_set_heap (h->mheap);
106 vec_validate (h->working_copies, cpu_number);
107 clib_mem_set_heap (oldheap);
111 * working_copies are per-cpu so that near-simultaneous
112 * updates from multiple threads will not result in sporadic, spurious
115 working_copy = h->working_copies[cpu_number];
117 h->saved_bucket.as_u64 = b->as_u64;
118 oldheap = clib_mem_set_heap (h->mheap);
120 if ((1<<b->log2_pages) > vec_len (working_copy))
122 vec_validate_aligned (working_copy, (1<<b->log2_pages)-1,
124 h->working_copies[cpu_number] = working_copy;
127 _vec_len(working_copy) = 1<<b->log2_pages;
128 clib_mem_set_heap (oldheap);
130 v = BV(clib_bihash_get_value) (h, b->offset);
132 memcpy (working_copy, v, sizeof (*v)*(1<<b->log2_pages));
133 working_bucket.as_u64 = b->as_u64;
134 working_bucket.offset = BV(clib_bihash_get_offset) (h, working_copy);
135 CLIB_MEMORY_BARRIER();
136 b->as_u64 = working_bucket.as_u64;
137 h->working_copies[cpu_number] = working_copy;
140 static BVT(clib_bihash_value) *
142 (BVT(clib_bihash) * h,
143 BVT(clib_bihash_value) * old_values,
146 BVT(clib_bihash_value) * new_values, * v, * new_v;
149 new_values = BV(value_alloc) (h, new_log2_pages);
152 for (i = 0; i < vec_len (old_values); i++)
156 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
158 if (BV(clib_bihash_is_free)(&(v->kvp[j])) == 0)
160 new_hash = BV(clib_bihash_hash) (&(v->kvp[j]));
161 new_hash >>= h->log2_nbuckets;
162 new_hash &= (1<<new_log2_pages) - 1;
164 new_v = &new_values [new_hash];
166 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
168 if (BV(clib_bihash_is_free)(&(new_v->kvp[k])))
170 memcpy (&(new_v->kvp[k]), &(v->kvp[j]),
171 sizeof (new_v->kvp[k]));
175 /* Crap. Tell caller to try again */
176 BV(value_free) (h, new_values);
187 int BV(clib_bihash_add_del)
188 (BVT(clib_bihash) * h,
189 BVT(clib_bihash_kv) * add_v,
193 clib_bihash_bucket_t * b, tmp_b;
194 BVT(clib_bihash_value) * v, * new_v, * save_new_v, * working_copy;
200 u32 cpu_number = os_get_cpu_number();
202 hash = BV(clib_bihash_hash) (add_v);
204 bucket_index = hash & (h->nbuckets-1);
205 b = &h->buckets[bucket_index];
207 hash >>= h->log2_nbuckets;
209 while (__sync_lock_test_and_set (h->writer_lock, 1))
212 /* First elt in the bucket? */
221 v = BV(value_alloc) (h, 0);
224 tmp_b.offset = BV(clib_bihash_get_offset) (h, v);
226 b->as_u64 = tmp_b.as_u64;
230 BV(make_working_copy) (h, b);
232 v = BV(clib_bihash_get_value) (h, h->saved_bucket.offset);
233 value_index = hash & ((1<<h->saved_bucket.log2_pages)-1);
239 * For obvious (in hindsight) reasons, see if we're supposed to
240 * replace an existing key, then look for an empty slot.
242 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
244 if (!memcmp(&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
246 memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
247 CLIB_MEMORY_BARRIER();
248 /* Restore the previous (k,v) pairs */
249 b->as_u64 = h->saved_bucket.as_u64;
253 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
255 if (BV(clib_bihash_is_free)(&(v->kvp[i])))
257 memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
258 CLIB_MEMORY_BARRIER();
259 b->as_u64 = h->saved_bucket.as_u64;
263 /* no room at the inn... split case... */
267 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
269 if (!memcmp(&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
271 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
272 CLIB_MEMORY_BARRIER();
273 b->as_u64 = h->saved_bucket.as_u64;
278 b->as_u64 = h->saved_bucket.as_u64;
282 new_log2_pages = h->saved_bucket.log2_pages + 1;
285 working_copy = h->working_copies[cpu_number];
286 new_v = BV(split_and_rehash) (h, working_copy, new_log2_pages);
293 /* Try to add the new entry */
295 new_hash = BV(clib_bihash_hash) (add_v);
296 new_hash >>= h->log2_nbuckets;
297 new_hash &= (1<<min_log2(vec_len(new_v))) - 1;
300 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
302 if (BV(clib_bihash_is_free)(&(new_v->kvp[i])))
304 memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
308 /* Crap. Try again */
310 BV(value_free) (h, save_new_v);
314 tmp_b.log2_pages = min_log2 (vec_len (save_new_v));
315 tmp_b.offset = BV(clib_bihash_get_offset) (h, save_new_v);
316 CLIB_MEMORY_BARRIER();
317 b->as_u64 = tmp_b.as_u64;
318 v = BV(clib_bihash_get_value) (h, h->saved_bucket.offset);
319 BV(value_free) (h, v);
322 CLIB_MEMORY_BARRIER();
323 h->writer_lock[0] = 0;
327 int BV(clib_bihash_search)
328 (BVT(clib_bihash) * h,
329 BVT(clib_bihash_kv) *search_key,
330 BVT(clib_bihash_kv) *valuep)
335 BVT(clib_bihash_value) * v;
336 clib_bihash_bucket_t * b;
341 hash = BV(clib_bihash_hash) (search_key);
343 bucket_index = hash & (h->nbuckets-1);
344 b = &h->buckets[bucket_index];
349 hash >>= h->log2_nbuckets;
351 v = BV(clib_bihash_get_value) (h, b->offset);
352 value_index = hash & ((1<<b->log2_pages)-1);
355 for (i = 0; i < BIHASH_KVP_PER_PAGE; i++)
357 if (BV(clib_bihash_key_compare)(v->kvp[i].key, search_key->key))
366 u8 * BV(format_bihash) (u8 * s, va_list * args)
369 = va_arg (*args, BVT(clib_bihash) *);
370 int verbose = va_arg (*args, int);
371 clib_bihash_bucket_t * b;
372 BVT(clib_bihash_value) * v;
374 u64 active_elements = 0;
376 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
378 for (i = 0; i < h->nbuckets; i++)
384 s = format (s, "[%d]: empty\n", i);
390 s = format (s, "[%d]: heap offset %d, len %d\n", i,
391 b->offset, (1<<b->log2_pages));
394 v = BV(clib_bihash_get_value) (h, b->offset);
395 for (j = 0; j < (1<<b->log2_pages); j++)
397 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
399 if (BV(clib_bihash_is_free)(&v->kvp[k]))
402 s = format (s, " %d: empty\n",
403 j * BIHASH_KVP_PER_PAGE + k);
408 s = format (s, " %d: %U\n",
409 j * BIHASH_KVP_PER_PAGE + k,
410 BV(format_bihash_kvp),
419 s = format (s, " %lld active elements\n", active_elements);
420 s = format (s, " %d free lists\n", vec_len (h->freelists));
421 s = format (s, " %U\n", format_mheap, h->mheap, 1 /* verbose */);
426 void BV(clib_bihash_foreach_key_value_pair)
427 (BVT(clib_bihash) * h,
432 clib_bihash_bucket_t * b;
433 BVT(clib_bihash_value) * v;
434 void (*fp)(BVT(clib_bihash_kv) *, void *) = callback;
436 for (i = 0; i < h->nbuckets; i++)
442 v = BV(clib_bihash_get_value) (h, b->offset);
443 for (j = 0; j < (1<<b->log2_pages); j++)
445 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
447 if (BV(clib_bihash_is_free)(&v->kvp[k]))
450 (*fp)(&v->kvp[k], arg);