bihash: give hint to CPU that we are spinlocking
[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 #define _bv(a,b) a##b
37 #define __bv(a,b) _bv(a,b)
38 #define BV(a) __bv(a,BIHASH_TYPE)
39
40 #define _bvt(a,b) a##b##_t
41 #define __bvt(a,b) _bvt(a,b)
42 #define BVT(a) __bvt(a,BIHASH_TYPE)
43
44 typedef struct BV (clib_bihash_value)
45 {
46   union
47   {
48     BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
49     struct BV (clib_bihash_value) * next_free;
50   };
51 } BVT (clib_bihash_value);
52
53 #define BIHASH_BUCKET_OFFSET_BITS 36
54
55 typedef struct
56 {
57   union
58   {
59     struct
60     {
61       u64 offset:BIHASH_BUCKET_OFFSET_BITS;
62       u64 lock:1;
63       u64 linear_search:1;
64       u64 log2_pages:8;
65       i64 refcnt:16;
66     };
67     u64 as_u64;
68   };
69 } BVT (clib_bihash_bucket);
70
71 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
72
73 typedef struct
74 {
75   BVT (clib_bihash_value) * values;
76   BVT (clib_bihash_bucket) * buckets;
77   volatile u32 *alloc_lock;
78
79     BVT (clib_bihash_value) ** working_copies;
80   int *working_copy_lengths;
81     BVT (clib_bihash_bucket) saved_bucket;
82
83   u32 nbuckets;
84   u32 log2_nbuckets;
85   u8 *name;
86
87   u64 cache_hits;
88   u64 cache_misses;
89
90     BVT (clib_bihash_value) ** freelists;
91
92   /*
93    * Backing store allocation. Since bihash manages its own
94    * freelists, we simple dole out memory at alloc_arena_next.
95    */
96   uword alloc_arena;
97   uword alloc_arena_next;
98   uword alloc_arena_size;
99
100   /**
101     * A custom format function to print the Key and Value of bihash_key instead of default hexdump
102     */
103   format_function_t *fmt_fn;
104
105 } BVT (clib_bihash);
106
107 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
108 {
109   while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
110     CLIB_PAUSE ();
111 }
112
113 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
114 {
115   __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
116 }
117
118 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
119 {
120   BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
121
122   do
123     {
124       locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
125       unlocked_bucket.lock = 0;
126       locked_bucket.lock = 1;
127       CLIB_PAUSE ();
128     }
129   while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
130                                       locked_bucket.as_u64, 1 /* weak */ ,
131                                       __ATOMIC_ACQUIRE,
132                                       __ATOMIC_ACQUIRE) == 0);
133 }
134
135 static inline void BV (clib_bihash_unlock_bucket)
136   (BVT (clib_bihash_bucket) * b)
137 {
138   CLIB_MEMORY_BARRIER ();
139   b->lock = 0;
140 }
141
142 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
143                                                 uword offset)
144 {
145   u8 *hp = (u8 *) h->alloc_arena;
146   u8 *vp = hp + offset;
147
148   return (void *) vp;
149 }
150
151 static inline int BV (clib_bihash_bucket_is_empty)
152   (BVT (clib_bihash_bucket) * b)
153 {
154   /* Note: applied to locked buckets, test offset */
155   return b->offset == 0;
156 }
157
158 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
159                                                  void *v)
160 {
161   u8 *hp, *vp;
162
163   hp = (u8 *) h->alloc_arena;
164   vp = (u8 *) v;
165
166   return vp - hp;
167 }
168
169 void BV (clib_bihash_init)
170   (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
171
172 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
173                                          format_function_t * fmt_fn);
174
175 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
176
177 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
178                               BVT (clib_bihash_kv) * add_v, int is_add);
179 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
180                              BVT (clib_bihash_kv) * search_v,
181                              BVT (clib_bihash_kv) * return_v);
182
183 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
184                                               void *callback, void *arg);
185
186 format_function_t BV (format_bihash);
187 format_function_t BV (format_bihash_kvp);
188 format_function_t BV (format_bihash_lru);
189
190 static inline int BV (clib_bihash_search_inline_with_hash)
191   (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
192 {
193   u32 bucket_index;
194   BVT (clib_bihash_value) * v;
195   BVT (clib_bihash_bucket) * b;
196   int i, limit;
197
198   bucket_index = hash & (h->nbuckets - 1);
199   b = &h->buckets[bucket_index];
200
201   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
202     return -1;
203
204   if (PREDICT_FALSE (b->lock))
205     {
206       volatile BVT (clib_bihash_bucket) * bv = b;
207       while (bv->lock)
208         CLIB_PAUSE ();
209     }
210
211   hash >>= h->log2_nbuckets;
212
213   v = BV (clib_bihash_get_value) (h, b->offset);
214
215   /* If the bucket has unresolvable collisions, use linear search */
216   limit = BIHASH_KVP_PER_PAGE;
217   v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
218   if (PREDICT_FALSE (b->linear_search))
219     limit <<= b->log2_pages;
220
221   for (i = 0; i < limit; i++)
222     {
223       if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
224         {
225           *key_result = v->kvp[i];
226           return 0;
227         }
228     }
229   return -1;
230 }
231
232 static inline int BV (clib_bihash_search_inline)
233   (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
234 {
235   u64 hash;
236
237   hash = BV (clib_bihash_hash) (key_result);
238
239   return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
240 }
241
242 static inline void BV (clib_bihash_prefetch_bucket)
243   (BVT (clib_bihash) * h, u64 hash)
244 {
245   u32 bucket_index;
246   BVT (clib_bihash_bucket) * b;
247
248   bucket_index = hash & (h->nbuckets - 1);
249   b = &h->buckets[bucket_index];
250
251   CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
252 }
253
254 static inline void BV (clib_bihash_prefetch_data)
255   (BVT (clib_bihash) * h, u64 hash)
256 {
257   u32 bucket_index;
258   BVT (clib_bihash_value) * v;
259   BVT (clib_bihash_bucket) * b;
260
261   bucket_index = hash & (h->nbuckets - 1);
262   b = &h->buckets[bucket_index];
263
264   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
265     return;
266
267   hash >>= h->log2_nbuckets;
268   v = BV (clib_bihash_get_value) (h, b->offset);
269
270   v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
271
272   CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
273 }
274
275 static inline int BV (clib_bihash_search_inline_2_with_hash)
276   (BVT (clib_bihash) * h,
277    u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
278 {
279   u32 bucket_index;
280   BVT (clib_bihash_value) * v;
281   BVT (clib_bihash_bucket) * b;
282   int i, limit;
283
284   ASSERT (valuep);
285
286   bucket_index = hash & (h->nbuckets - 1);
287   b = &h->buckets[bucket_index];
288
289   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
290     return -1;
291
292   if (PREDICT_FALSE (b->lock))
293     {
294       volatile BVT (clib_bihash_bucket) * bv = b;
295       while (bv->lock)
296         CLIB_PAUSE ();
297     }
298
299   hash >>= h->log2_nbuckets;
300   v = BV (clib_bihash_get_value) (h, b->offset);
301
302   /* If the bucket has unresolvable collisions, use linear search */
303   limit = BIHASH_KVP_PER_PAGE;
304   v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
305   if (PREDICT_FALSE (b->linear_search))
306     limit <<= b->log2_pages;
307
308   for (i = 0; i < limit; i++)
309     {
310       if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
311         {
312           *valuep = v->kvp[i];
313           return 0;
314         }
315     }
316   return -1;
317 }
318
319 static inline int BV (clib_bihash_search_inline_2)
320   (BVT (clib_bihash) * h,
321    BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
322 {
323   u64 hash;
324
325   hash = BV (clib_bihash_hash) (search_key);
326
327   return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
328                                                      valuep);
329 }
330
331
332 #endif /* __included_bihash_template_h__ */
333
334 /** @endcond */
335
336 /*
337  * fd.io coding-style-patch-verification: ON
338  *
339  * Local Variables:
340  * eval: (c-set-style "gnu")
341  * End:
342  */