56c410b5a51eda2da00f3f765beb141fe3fe8c74
[vpp.git] / src / vppinfra / bihash_template.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
17
18 void BV (clib_bihash_init)
19   (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
20 {
21   void *oldheap;
22   int i;
23
24   nbuckets = 1 << (max_log2 (nbuckets));
25
26   h->name = (u8 *) name;
27   h->nbuckets = nbuckets;
28   h->log2_nbuckets = max_log2 (nbuckets);
29   h->cache_hits = 0;
30   h->cache_misses = 0;
31
32   h->mheap = mheap_alloc (0 /* use VM */ , memory_size);
33
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;
39
40   for (i = 0; i < nbuckets; i++)
41     BV (clib_bihash_reset_cache) (h->buckets + i);
42
43   clib_mem_set_heap (oldheap);
44 }
45
46 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
47 {
48   mheap_free (h->mheap);
49   memset (h, 0, sizeof (*h));
50 }
51
52 static
53 BVT (clib_bihash_value) *
54 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
55 {
56   BVT (clib_bihash_value) * rv = 0;
57   void *oldheap;
58
59   ASSERT (h->writer_lock[0]);
60   if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
61     {
62       oldheap = clib_mem_set_heap (h->mheap);
63
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);
68       goto initialize;
69     }
70   rv = h->freelists[log2_pages];
71   h->freelists[log2_pages] = rv->next_free;
72
73 initialize:
74   ASSERT (rv);
75   /*
76    * Latest gcc complains that the length arg is zero
77    * if we replace (1<<log2_pages) with vec_len(rv).
78    * No clue.
79    */
80   memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
81   return rv;
82 }
83
84 static void
85 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
86                  u32 log2_pages)
87 {
88   ASSERT (h->writer_lock[0]);
89
90   ASSERT (vec_len (h->freelists) > log2_pages);
91
92   v->next_free = h->freelists[log2_pages];
93   h->freelists[log2_pages] = v;
94 }
95
96 static inline void
97 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
98 {
99   BVT (clib_bihash_value) * v;
100   BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
101   void *oldheap;
102   BVT (clib_bihash_value) * working_copy;
103   u32 thread_index = os_get_thread_index ();
104   int log2_working_copy_length;
105
106   if (thread_index >= vec_len (h->working_copies))
107     {
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);
112     }
113
114   /*
115    * working_copies are per-cpu so that near-simultaneous
116    * updates from multiple threads will not result in sporadic, spurious
117    * lookup failures.
118    */
119   working_copy = h->working_copies[thread_index];
120   log2_working_copy_length = h->working_copy_lengths[thread_index];
121
122   h->saved_bucket.as_u64 = b->as_u64;
123   oldheap = clib_mem_set_heap (h->mheap);
124
125   if (b->log2_pages > log2_working_copy_length)
126     {
127       if (working_copy)
128         clib_mem_free (working_copy);
129
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;
135     }
136
137   clib_mem_set_heap (oldheap);
138
139   /* Lock the bucket... */
140   while (BV (clib_bihash_lock_bucket) (b) == 0)
141     ;
142
143   v = BV (clib_bihash_get_value) (h, b->offset);
144
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;
151 }
152
153 static
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,
158    u32 new_log2_pages)
159 {
160   BVT (clib_bihash_value) * new_values, *new_v;
161   int i, j, length_in_kvs;
162
163   new_values = BV (value_alloc) (h, new_log2_pages);
164   length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
165
166   for (i = 0; i < length_in_kvs; i++)
167     {
168       u64 new_hash;
169
170       /* Entry not in use? Forget it */
171       if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
172         continue;
173
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];
179
180       /* Across the new home-page */
181       for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
182         {
183           /* Empty slot */
184           if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
185             {
186               clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
187                            sizeof (new_v->kvp[j]));
188               goto doublebreak;
189             }
190         }
191       /* Crap. Tell caller to try again */
192       BV (value_free) (h, new_values, new_log2_pages);
193       return 0;
194     doublebreak:;
195     }
196
197   return new_values;
198 }
199
200 static
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,
205    u32 new_log2_pages)
206 {
207   BVT (clib_bihash_value) * new_values;
208   int i, j, new_length, old_length;
209
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;
213
214   j = 0;
215   /* Across the old value array */
216   for (i = 0; i < old_length; i++)
217     {
218       /* Find a free slot in the new linear scan bucket */
219       for (; j < new_length; j++)
220         {
221           /* Old value not in use? Forget it. */
222           if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
223             goto doublebreak;
224
225           /* New value should never be in use */
226           if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
227             {
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]));
231               j++;
232               goto doublebreak;
233             }
234         }
235       /* This should never happen... */
236       clib_warning ("BUG: linear rehash failed!");
237       BV (value_free) (h, new_values, new_log2_pages);
238       return 0;
239
240     doublebreak:;
241     }
242   return new_values;
243 }
244
245 int BV (clib_bihash_add_del)
246   (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
247 {
248   u32 bucket_index;
249   BVT (clib_bihash_bucket) * b, tmp_b;
250   BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
251   int rv = 0;
252   int i, limit;
253   u64 hash, new_hash;
254   u32 new_log2_pages, old_log2_pages;
255   u32 thread_index = os_get_thread_index ();
256   int mark_bucket_linear;
257   int resplit_once;
258
259   hash = BV (clib_bihash_hash) (add_v);
260
261   bucket_index = hash & (h->nbuckets - 1);
262   b = &h->buckets[bucket_index];
263
264   hash >>= h->log2_nbuckets;
265
266   tmp_b.linear_search = 0;
267
268   while (__sync_lock_test_and_set (h->writer_lock, 1))
269     ;
270
271   /* First elt in the bucket? */
272   if (b->offset == 0)
273     {
274       if (is_add == 0)
275         {
276           rv = -1;
277           goto unlock;
278         }
279
280       v = BV (value_alloc) (h, 0);
281
282       *v->kvp = *add_v;
283       tmp_b.as_u64 = 0;
284       tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
285
286       b->as_u64 = tmp_b.as_u64;
287       goto unlock;
288     }
289
290   /* Note: this leaves the cache disabled */
291   BV (make_working_copy) (h, b);
292
293   v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
294
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;
299
300   if (is_add)
301     {
302       /*
303        * For obvious (in hindsight) reasons, see if we're supposed to
304        * replace an existing key, then look for an empty slot.
305        */
306       for (i = 0; i < limit; i++)
307         {
308           if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
309             {
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;
314               goto unlock;
315             }
316         }
317       for (i = 0; i < limit; i++)
318         {
319           if (BV (clib_bihash_is_free) (&(v->kvp[i])))
320             {
321               clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
322               CLIB_MEMORY_BARRIER ();
323               b->as_u64 = h->saved_bucket.as_u64;
324               goto unlock;
325             }
326         }
327       /* no room at the inn... split case... */
328     }
329   else
330     {
331       for (i = 0; i < limit; i++)
332         {
333           if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
334             {
335               memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
336               CLIB_MEMORY_BARRIER ();
337               b->as_u64 = h->saved_bucket.as_u64;
338               goto unlock;
339             }
340         }
341       rv = -3;
342       b->as_u64 = h->saved_bucket.as_u64;
343       goto unlock;
344     }
345
346   old_log2_pages = h->saved_bucket.log2_pages;
347   new_log2_pages = old_log2_pages + 1;
348   mark_bucket_linear = 0;
349
350   working_copy = h->working_copies[thread_index];
351   resplit_once = 0;
352
353   new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
354                                  new_log2_pages);
355   if (new_v == 0)
356     {
357     try_resplit:
358       resplit_once = 1;
359       new_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,
362                                      new_log2_pages);
363       if (new_v == 0)
364         {
365         mark_linear:
366           new_log2_pages--;
367           /* pinned collisions, use linear search */
368           new_v =
369             BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
370                                           new_log2_pages);
371           mark_bucket_linear = 1;
372         }
373     }
374
375   /* Try to add the new entry */
376   save_new_v = new_v;
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;
384
385   for (i = 0; i < limit; i++)
386     {
387       if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
388         {
389           clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
390           goto expand_ok;
391         }
392     }
393
394   /* Crap. Try again */
395   BV (value_free) (h, save_new_v, new_log2_pages);
396   /*
397    * If we've already doubled the size of the bucket once,
398    * fall back to linear search now.
399    */
400   if (resplit_once)
401     goto mark_linear;
402   else
403     goto try_resplit;
404
405 expand_ok:
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;
409
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;
413
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);
418
419 unlock:
420   BV (clib_bihash_reset_cache) (b);
421   BV (clib_bihash_unlock_bucket) (b);
422   CLIB_MEMORY_BARRIER ();
423   h->writer_lock[0] = 0;
424   return rv;
425 }
426
427 int BV (clib_bihash_search)
428   (BVT (clib_bihash) * h,
429    BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
430 {
431   u64 hash;
432   u32 bucket_index;
433   BVT (clib_bihash_value) * v;
434 #if BIHASH_KVP_CACHE_SIZE > 0
435   BVT (clib_bihash_kv) * kvp;
436 #endif
437   BVT (clib_bihash_bucket) * b;
438   int i, limit;
439
440   ASSERT (valuep);
441
442   hash = BV (clib_bihash_hash) (search_key);
443
444   bucket_index = hash & (h->nbuckets - 1);
445   b = &h->buckets[bucket_index];
446
447   if (b->offset == 0)
448     return -1;
449
450 #if BIHASH_KVP_CACHE_SIZE > 0
451   /* Check the cache, if currently enabled */
452   if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
453     {
454       limit = BIHASH_KVP_CACHE_SIZE;
455       kvp = b->cache;
456       for (i = 0; i < limit; i++)
457         {
458           if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
459             {
460               *valuep = kvp[i];
461               h->cache_hits++;
462               return 0;
463             }
464         }
465     }
466 #endif
467
468   hash >>= h->log2_nbuckets;
469
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;
475
476   for (i = 0; i < limit; i++)
477     {
478       if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
479         {
480           *valuep = v->kvp[i];
481
482 #if BIHASH_KVP_CACHE_SIZE > 0
483           u8 cache_slot;
484           /* Shut off the cache */
485           if (BV (clib_bihash_lock_bucket) (b))
486             {
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);
490
491               /* Reenable the cache */
492               BV (clib_bihash_unlock_bucket) (b);
493               h->cache_misses++;
494             }
495 #endif
496           return 0;
497         }
498     }
499   return -1;
500 }
501
502 u8 *BV (format_bihash_lru) (u8 * s, va_list * args)
503 {
504 #if BIHASH_KVP_SIZE > 0
505   int i;
506   BVT (clib_bihash_bucket) * b = va_arg (*args, BVT (clib_bihash_bucket) *);
507   u16 cache_lru = b->cache_lru;
508
509   s = format (s, "cache %s, order ", cache_lru & (1 << 15) ? "on" : "off");
510
511   for (i = 0; i < BIHASH_KVP_CACHE_SIZE; i++)
512     s = format (s, "[%d] ", ((cache_lru >> (3 * i)) & 7));
513
514   return (s);
515 #else
516   return format (s, "cache not configured");
517 #endif
518 }
519
520 void
521 BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b, u8 slot)
522 {
523 #if BIHASH_KVP_SIZE > 0
524   BV (clib_bihash_update_lru) (b, slot);
525 #endif
526 }
527
528 u8 *BV (format_bihash) (u8 * s, va_list * args)
529 {
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;
534   int i, j, k;
535   u64 active_elements = 0;
536
537   s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
538
539   for (i = 0; i < h->nbuckets; i++)
540     {
541       b = &h->buckets[i];
542       if (b->offset == 0)
543         {
544           if (verbose > 1)
545             s = format (s, "[%d]: empty\n", i);
546           continue;
547         }
548
549       if (verbose)
550         {
551           s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
552                       b->offset, (1 << b->log2_pages), b->linear_search);
553         }
554
555       v = BV (clib_bihash_get_value) (h, b->offset);
556       for (j = 0; j < (1 << b->log2_pages); j++)
557         {
558           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
559             {
560               if (BV (clib_bihash_is_free) (&v->kvp[k]))
561                 {
562                   if (verbose > 1)
563                     s = format (s, "    %d: empty\n",
564                                 j * BIHASH_KVP_PER_PAGE + k);
565                   continue;
566                 }
567               if (verbose)
568                 {
569                   s = format (s, "    %d: %U\n",
570                               j * BIHASH_KVP_PER_PAGE + k,
571                               BV (format_bihash_kvp), &(v->kvp[k]));
572                 }
573               active_elements++;
574             }
575           v++;
576         }
577     }
578
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);
584   return s;
585 }
586
587 void BV (clib_bihash_foreach_key_value_pair)
588   (BVT (clib_bihash) * h, void *callback, void *arg)
589 {
590   int i, j, k;
591   BVT (clib_bihash_bucket) * b;
592   BVT (clib_bihash_value) * v;
593   void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
594
595   for (i = 0; i < h->nbuckets; i++)
596     {
597       b = &h->buckets[i];
598       if (b->offset == 0)
599         continue;
600
601       v = BV (clib_bihash_get_value) (h, b->offset);
602       for (j = 0; j < (1 << b->log2_pages); j++)
603         {
604           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
605             {
606               if (BV (clib_bihash_is_free) (&v->kvp[k]))
607                 continue;
608
609               (*fp) (&v->kvp[k], arg);
610             }
611           v++;
612         }
613     }
614 }
615
616 /** @endcond */
617
618 /*
619  * fd.io coding-style-patch-verification: ON
620  *
621  * Local Variables:
622  * eval: (c-set-style "gnu")
623  * End:
624  */