vlib: improvement to automatic core pinning
[vpp.git] / src / vppinfra / bihash_template.h
1 /*
2   Copyright (c) 2014 Cisco and/or its affiliates.
3
4   * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
18
19 /*
20  * Note: to instantiate the template multiple times in a single file,
21  * #undef __included_bihash_template_h__...
22  */
23 #ifndef __included_bihash_template_h__
24 #define __included_bihash_template_h__
25
26 #include <vppinfra/heap.h>
27 #include <vppinfra/format.h>
28 #include <vppinfra/pool.h>
29 #include <vppinfra/cache.h>
30 #include <vppinfra/lock.h>
31
32 #ifndef BIHASH_TYPE
33 #error BIHASH_TYPE not defined
34 #endif
35
36 #ifdef BIHASH_32_64_SVM
37 #include <vppinfra/linux/syscall.h>
38 #include <fcntl.h>
39 #define F_LINUX_SPECIFIC_BASE 1024
40 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
41 #define F_SEAL_SHRINK (2)
42 /* Max page size 2**16 due to refcount width  */
43 #define BIHASH_FREELIST_LENGTH 17
44 #endif
45
46 /* default is 2MB, use 30 for 1GB */
47 #ifndef BIHASH_LOG2_HUGEPAGE_SIZE
48 #define BIHASH_LOG2_HUGEPAGE_SIZE 21
49 #endif
50
51 #define _bv(a,b) a##b
52 #define __bv(a,b) _bv(a,b)
53 #define BV(a) __bv(a,BIHASH_TYPE)
54
55 #define _bvt(a,b) a##b##_t
56 #define __bvt(a,b) _bvt(a,b)
57 #define BVT(a) __bvt(a,BIHASH_TYPE)
58
59 #define _bvs(a,b) struct a##b
60 #define __bvs(a,b) _bvs(a,b)
61 #define BVS(a) __bvs(a,BIHASH_TYPE)
62
63 #if _LP64 == 0
64 #define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0)
65 #define u64_to_pointer(x) (void *)(u32)((x))
66 #define pointer_to_u64(x) (u64)(u32)((x))
67 #else
68 #define OVERFLOW_ASSERT(x)
69 #define u64_to_pointer(x) (void *)((x))
70 #define pointer_to_u64(x) (u64)((x))
71 #endif
72
73 typedef struct BV (clib_bihash_value)
74 {
75   union
76   {
77     BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
78     u64 next_free_as_u64;
79   };
80 } BVT (clib_bihash_value);
81
82 #define BIHASH_BUCKET_OFFSET_BITS 36
83
84 typedef struct
85 {
86   union
87   {
88     struct
89     {
90       u64 offset:BIHASH_BUCKET_OFFSET_BITS;
91       u64 lock:1;
92       u64 linear_search:1;
93       u64 log2_pages:8;
94       u64 refcnt:16;
95     };
96     u64 as_u64;
97   };
98 } BVT (clib_bihash_bucket);
99
100 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
101
102 typedef CLIB_PACKED (struct {
103   /*
104    * Backing store allocation. Since bihash manages its own
105    * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
106    */
107   u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
108   u64 alloc_arena_size; /* Size of the arena */
109   u64 alloc_arena_mapped;       /* Size of the mapped memory in the arena */
110   /* Two SVM pointers stored as 8-byte integers */
111   u64 alloc_lock_as_u64;
112   u64 buckets_as_u64;
113   /* freelist list-head arrays/vectors */
114   u64 freelists_as_u64;
115   u32 nbuckets; /* Number of buckets */
116   /* Set when header valid */
117   volatile u32 ready;
118   u64 pad[1];
119 }) BVT (clib_bihash_shared_header);
120
121 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
122
123 typedef
124 BVS (clib_bihash_alloc_chunk)
125 {
126   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
127
128   /* chunk size */
129   uword size;
130
131   /* pointer to the next allocation */
132   u8 *next_alloc;
133
134   /* number of bytes left in this chunk */
135   uword bytes_left;
136
137   /* doubly linked list of heap allocated chunks */
138   BVS (clib_bihash_alloc_chunk) * prev, *next;
139
140 } BVT (clib_bihash_alloc_chunk);
141
142 typedef
143 BVS (clib_bihash)
144 {
145   BVT (clib_bihash_bucket) * buckets;
146   volatile u32 *alloc_lock;
147
148   BVT (clib_bihash_value) ** working_copies;
149   int *working_copy_lengths;
150   BVT (clib_bihash_bucket) saved_bucket;
151
152   u32 nbuckets;
153   u32 log2_nbuckets;
154   u64 memory_size;
155   u8 *name;
156   format_function_t *fmt_fn;
157   void *heap;
158   BVT (clib_bihash_alloc_chunk) * chunks;
159
160   u64 *freelists;
161
162 #if BIHASH_32_64_SVM
163   BVT (clib_bihash_shared_header) * sh;
164   int memfd;
165 #else
166   BVT (clib_bihash_shared_header) sh;
167 #endif
168
169   u64 alloc_arena;              /* Base of the allocation arena */
170   volatile u8 instantiated;
171   u8 dont_add_to_all_bihash_list;
172
173   /**
174     * A custom format function to print the Key and Value of bihash_key instead of default hexdump
175     */
176   format_function_t *kvp_fmt_fn;
177
178   /** Optional statistics-gathering callback */
179 #if BIHASH_ENABLE_STATS
180   void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
181
182   /** Statistics callback context (e.g. address of stats data structure) */
183   void *inc_stats_context;
184 #endif
185
186 } BVT (clib_bihash);
187
188 typedef struct
189 {
190   BVT (clib_bihash) * h;
191   char *name;
192   u32 nbuckets;
193   uword memory_size;
194   format_function_t *kvp_fmt_fn;
195   u8 instantiate_immediately;
196   u8 dont_add_to_all_bihash_list;
197 } BVT (clib_bihash_init2_args);
198
199 extern void **clib_all_bihashes;
200
201 #if BIHASH_32_64_SVM
202 #undef alloc_arena_next
203 #undef alloc_arena_size
204 #undef alloc_arena_mapped
205 #undef alloc_arena
206 #undef CLIB_BIHASH_READY_MAGIC
207 #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
208 #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
209 #define alloc_arena_mapped(h) (((h)->sh)->alloc_arena_mapped)
210 #define alloc_arena(h) ((h)->alloc_arena)
211 #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
212 #else
213 #undef alloc_arena_next
214 #undef alloc_arena_size
215 #undef alloc_arena_mapped
216 #undef alloc_arena
217 #undef CLIB_BIHASH_READY_MAGIC
218 #define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
219 #define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
220 #define alloc_arena_mapped(h) ((h)->sh.alloc_arena_mapped)
221 #define alloc_arena(h) ((h)->alloc_arena)
222 #define CLIB_BIHASH_READY_MAGIC 0
223 #endif
224
225 #ifndef BIHASH_STAT_IDS
226 #define BIHASH_STAT_IDS 1
227
228 #define foreach_bihash_stat                     \
229 _(alloc_add)                                    \
230 _(add)                                          \
231 _(split_add)                                    \
232 _(replace)                                      \
233 _(update)                                       \
234 _(del)                                          \
235 _(del_free)                                     \
236 _(linear)                                       \
237 _(resplit)                                      \
238 _(working_copy_lost)                            \
239 _(splits)                       /* must be last */
240
241 typedef enum
242 {
243 #define _(a) BIHASH_STAT_##a,
244   foreach_bihash_stat
245 #undef _
246     BIHASH_STAT_N_STATS,
247 } BVT (clib_bihash_stat_id);
248 #endif /* BIHASH_STAT_IDS */
249
250 static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
251                                                     int stat_id, u64 count)
252 {
253 #if BIHASH_ENABLE_STATS
254   if (PREDICT_FALSE (h->inc_stats_callback != 0))
255     h->inc_stats_callback (h, stat_id, count);
256 #endif
257 }
258
259 #if BIHASH_ENABLE_STATS
260 static inline void BV (clib_bihash_set_stats_callback)
261   (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
262    void *ctx)
263 {
264   h->inc_stats_callback = cb;
265   h->inc_stats_context = ctx;
266 }
267 #endif
268
269
270 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
271 {
272   while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
273     CLIB_PAUSE ();
274 }
275
276 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
277 {
278   __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
279 }
280
281 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
282 {
283   BVT (clib_bihash_bucket) mask = { .lock = 1 };
284   u64 old;
285
286 try_again:
287   old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64);
288
289   if (PREDICT_FALSE (old & mask.as_u64))
290     {
291       /* somebody else flipped the bit, try again */
292       CLIB_PAUSE ();
293       goto try_again;
294     }
295 }
296
297 static inline void BV (clib_bihash_unlock_bucket)
298   (BVT (clib_bihash_bucket) * b)
299 {
300   b->lock = 0;
301 }
302
303 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
304                                                 uword offset)
305 {
306   u8 *hp = (u8 *) (uword) alloc_arena (h);
307   u8 *vp = hp + offset;
308
309   return (void *) vp;
310 }
311
312 static inline int BV (clib_bihash_bucket_is_empty)
313   (BVT (clib_bihash_bucket) * b)
314 {
315   /* Note: applied to locked buckets, test offset */
316   if (BIHASH_KVP_AT_BUCKET_LEVEL == 0)
317     return b->offset == 0;
318   else
319     return (b->log2_pages == 0 && b->refcnt == 1);
320 }
321
322 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
323                                                  void *v)
324 {
325   u8 *hp, *vp;
326
327   hp = (u8 *) (uword) alloc_arena (h);
328   vp = (u8 *) v;
329
330   return vp - hp;
331 }
332
333 #define BIHASH_ADD 1
334 #define BIHASH_DEL 0
335
336 void BV (clib_bihash_init)
337   (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
338
339 void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a);
340
341 #if BIHASH_32_64_SVM
342 void BV (clib_bihash_initiator_init_svm)
343   (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
344 void BV (clib_bihash_responder_init_svm)
345   (BVT (clib_bihash) * h, char *name, int fd);
346 #endif
347
348 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
349                                          format_function_t * kvp_fmt_fn);
350
351 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
352
353 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
354                               BVT (clib_bihash_kv) * add_v, int is_add);
355
356 int BV (clib_bihash_add_del_with_hash) (BVT (clib_bihash) * h,
357                                         BVT (clib_bihash_kv) * add_v, u64 hash,
358                                         int is_add);
359 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
360                                              BVT (clib_bihash_kv) * add_v,
361                                              int (*is_stale_cb) (BVT
362                                                                  (clib_bihash_kv)
363                                                                  *, void *),
364                                              void *arg);
365 int BV (clib_bihash_add_with_overwrite_cb) (
366   BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
367   void (*overwrite_cb) (BVT (clib_bihash_kv) *, void *), void *arg);
368 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
369                              BVT (clib_bihash_kv) * search_v,
370                              BVT (clib_bihash_kv) * return_v);
371
372 int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h);
373
374 #define BIHASH_WALK_STOP 0
375 #define BIHASH_WALK_CONTINUE 1
376
377 typedef
378   int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
379                                                      void *);
380 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
381                                               BV
382                                               (clib_bihash_foreach_key_value_pair_cb)
383                                               cb, void *arg);
384 void *clib_all_bihash_set_heap (void);
385 void clib_bihash_copied (void *dst, void *src);
386
387 format_function_t BV (format_bihash);
388 format_function_t BV (format_bihash_kvp);
389 format_function_t BV (format_bihash_lru);
390
391 static inline
392 BVT (clib_bihash_bucket) *
393 BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
394 {
395 #if BIHASH_KVP_AT_BUCKET_LEVEL
396   uword offset;
397   offset = (hash & (h->nbuckets - 1));
398   offset = offset * (sizeof (BVT (clib_bihash_bucket))
399                      + (BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv))));
400   return ((BVT (clib_bihash_bucket) *) (((u8 *) h->buckets) + offset));
401 #else
402   return h->buckets + (hash & (h->nbuckets - 1));
403 #endif
404 }
405
406 static inline int BV (clib_bihash_search_inline_with_hash)
407   (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
408 {
409   BVT (clib_bihash_kv) rv;
410   BVT (clib_bihash_value) * v;
411   BVT (clib_bihash_bucket) * b;
412   int i, limit;
413
414   static const BVT (clib_bihash_bucket) mask = {
415     .linear_search = 1,
416     .log2_pages = -1
417   };
418
419 #if BIHASH_LAZY_INSTANTIATE
420   if (PREDICT_FALSE (h->instantiated == 0))
421     return -1;
422 #endif
423
424   b = BV (clib_bihash_get_bucket) (h, hash);
425
426   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
427     return -1;
428
429   if (PREDICT_FALSE (b->lock))
430     {
431       volatile BVT (clib_bihash_bucket) * bv = b;
432       while (bv->lock)
433         CLIB_PAUSE ();
434     }
435
436   v = BV (clib_bihash_get_value) (h, b->offset);
437
438   /* If the bucket has unresolvable collisions, use linear search */
439   limit = BIHASH_KVP_PER_PAGE;
440
441   if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
442     {
443       if (PREDICT_FALSE (b->linear_search))
444         limit <<= b->log2_pages;
445       else
446         v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
447     }
448
449   for (i = 0; i < limit; i++)
450     {
451       if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
452         {
453           rv = v->kvp[i];
454           if (BV (clib_bihash_is_free) (&rv))
455             return -1;
456           *key_result = rv;
457           return 0;
458         }
459     }
460   return -1;
461 }
462
463 static inline int BV (clib_bihash_search_inline)
464   (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
465 {
466   u64 hash;
467
468   hash = BV (clib_bihash_hash) (key_result);
469
470   return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
471 }
472
473 static inline void BV (clib_bihash_prefetch_bucket)
474   (BVT (clib_bihash) * h, u64 hash)
475 {
476   CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
477                  BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES,
478                  LOAD);
479 }
480
481 static inline void BV (clib_bihash_prefetch_data)
482   (BVT (clib_bihash) * h, u64 hash)
483 {
484   BVT (clib_bihash_value) * v;
485   BVT (clib_bihash_bucket) * b;
486
487 #if BIHASH_LAZY_INSTANTIATE
488   if (PREDICT_FALSE (h->instantiated == 0))
489     return;
490 #endif
491
492   b = BV (clib_bihash_get_bucket) (h, hash);
493
494   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
495     return;
496
497   v = BV (clib_bihash_get_value) (h, b->offset);
498
499   if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0))
500     v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
501
502   CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)),
503                  LOAD);
504 }
505
506 static inline int BV (clib_bihash_search_inline_2_with_hash)
507   (BVT (clib_bihash) * h,
508    u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
509 {
510   BVT (clib_bihash_kv) rv;
511   BVT (clib_bihash_value) * v;
512   BVT (clib_bihash_bucket) * b;
513   int i, limit;
514
515   static const BVT (clib_bihash_bucket) mask = {
516     .linear_search = 1,
517     .log2_pages = -1
518   };
519
520   ASSERT (valuep);
521
522 #if BIHASH_LAZY_INSTANTIATE
523   if (PREDICT_FALSE (h->instantiated == 0))
524     return -1;
525 #endif
526
527   b = BV (clib_bihash_get_bucket) (h, hash);
528
529   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
530     return -1;
531
532   if (PREDICT_FALSE (b->lock))
533     {
534       volatile BVT (clib_bihash_bucket) * bv = b;
535       while (bv->lock)
536         CLIB_PAUSE ();
537     }
538
539   v = BV (clib_bihash_get_value) (h, b->offset);
540
541   /* If the bucket has unresolvable collisions, use linear search */
542   limit = BIHASH_KVP_PER_PAGE;
543
544   if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
545     {
546       if (PREDICT_FALSE (b->linear_search))
547         limit <<= b->log2_pages;
548       else
549         v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
550     }
551
552   for (i = 0; i < limit; i++)
553     {
554       if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
555         {
556           rv = v->kvp[i];
557           if (BV (clib_bihash_is_free) (&rv))
558             return -1;
559           *valuep = rv;
560           return 0;
561         }
562     }
563   return -1;
564 }
565
566 static inline int BV (clib_bihash_search_inline_2)
567   (BVT (clib_bihash) * h,
568    BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
569 {
570   u64 hash;
571
572   hash = BV (clib_bihash_hash) (search_key);
573
574   return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
575                                                      valuep);
576 }
577
578
579 #endif /* __included_bihash_template_h__ */
580
581 /** @endcond */
582
583 /*
584  * fd.io coding-style-patch-verification: ON
585  *
586  * Local Variables:
587  * eval: (c-set-style "gnu")
588  * End:
589  */