Add a bihash prefetchable bucket-level cache
[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
30 #ifndef BIHASH_TYPE
31 #error BIHASH_TYPE not defined
32 #endif
33
34 #define _bv(a,b) a##b
35 #define __bv(a,b) _bv(a,b)
36 #define BV(a) __bv(a,BIHASH_TYPE)
37
38 #define _bvt(a,b) a##b##_t
39 #define __bvt(a,b) _bvt(a,b)
40 #define BVT(a) __bvt(a,BIHASH_TYPE)
41
42 typedef struct BV (clib_bihash_value)
43 {
44   union
45   {
46     BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
47     struct BV (clib_bihash_value) * next_free;
48   };
49 } BVT (clib_bihash_value);
50
51 #if BIHASH_KVP_CACHE_SIZE > 5
52 #error Requested KVP cache LRU data exceeds 16 bits
53 #endif
54
55 typedef struct
56 {
57   union
58   {
59     struct
60     {
61       u32 offset;
62       u8 linear_search;
63       u8 log2_pages;
64       u16 cache_lru;
65     };
66     u64 as_u64;
67   };
68     BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE];
69 } BVT (clib_bihash_bucket);
70
71 typedef struct
72 {
73   BVT (clib_bihash_value) * values;
74   BVT (clib_bihash_bucket) * buckets;
75   volatile u32 *writer_lock;
76
77     BVT (clib_bihash_value) ** working_copies;
78   int *working_copy_lengths;
79     BVT (clib_bihash_bucket) saved_bucket;
80
81   u32 nbuckets;
82   u32 log2_nbuckets;
83   u32 linear_buckets;
84   u8 *name;
85
86   u64 cache_hits;
87   u64 cache_misses;
88
89     BVT (clib_bihash_value) ** freelists;
90   void *mheap;
91
92 } BVT (clib_bihash);
93
94
95 static inline void
96 BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
97 {
98   u16 value, tmp, mask;
99   u8 found_lru_pos;
100   u16 save_hi;
101
102   if (BIHASH_KVP_CACHE_SIZE < 2)
103     return;
104
105   ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
106
107   /* First, find the slot in cache_lru */
108   mask = slot;
109   if (BIHASH_KVP_CACHE_SIZE > 1)
110     mask |= slot << 3;
111   if (BIHASH_KVP_CACHE_SIZE > 2)
112     mask |= slot << 6;
113   if (BIHASH_KVP_CACHE_SIZE > 3)
114     mask |= slot << 9;
115   if (BIHASH_KVP_CACHE_SIZE > 4)
116     mask |= slot << 12;
117
118   value = b->cache_lru;
119   tmp = value ^ mask;
120
121   /* Already the most-recently used? */
122   if ((tmp & 7) == 0)
123     return;
124
125   found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0;
126   if (BIHASH_KVP_CACHE_SIZE > 2)
127     found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos;
128   if (BIHASH_KVP_CACHE_SIZE > 3)
129     found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos;
130   if (BIHASH_KVP_CACHE_SIZE > 4)
131     found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos;
132
133   ASSERT (found_lru_pos);
134
135   /* create a mask to kill bits in or above slot */
136   mask = 0xFFFF << found_lru_pos;
137   mask <<= found_lru_pos;
138   mask <<= found_lru_pos;
139   mask ^= 0xFFFF;
140   tmp = value & mask;
141
142   /* Save bits above slot */
143   mask ^= 0xFFFF;
144   mask <<= 3;
145   save_hi = value & mask;
146
147   value = save_hi | (tmp << 3) | slot;
148
149   b->cache_lru = value;
150 }
151
152 void
153 BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
154                                         u8 slot);
155
156 static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
157 {
158   return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
159 }
160
161 static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
162 {
163   u16 initial_lru_value;
164
165   memset (b->cache, 0xff, sizeof (b->cache));
166
167   /*
168    * We'll want the cache to be loaded from slot 0 -> slot N, so
169    * the initial LRU order is reverse index order.
170    */
171   if (BIHASH_KVP_CACHE_SIZE == 1)
172     initial_lru_value = 0;
173   else if (BIHASH_KVP_CACHE_SIZE == 2)
174     initial_lru_value = (0 << 3) | (1 << 0);
175   else if (BIHASH_KVP_CACHE_SIZE == 3)
176     initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0);
177   else if (BIHASH_KVP_CACHE_SIZE == 4)
178     initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0);
179   else if (BIHASH_KVP_CACHE_SIZE == 5)
180     initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0);
181
182   b->cache_lru = initial_lru_value;
183 }
184
185 static inline void BV (clib_bihash_cache_enable_disable)
186   (BVT (clib_bihash_bucket) * b, u8 enable)
187 {
188   BVT (clib_bihash_bucket) tmp_b;
189   tmp_b.as_u64 = b->as_u64;
190   tmp_b.cache_lru &= 0x7FFF;
191   tmp_b.cache_lru |= enable << 15;
192   b->as_u64 = tmp_b.as_u64;
193 }
194
195 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
196                                                 uword offset)
197 {
198   u8 *hp = h->mheap;
199   u8 *vp = hp + offset;
200
201   return (void *) vp;
202 }
203
204 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
205                                                  void *v)
206 {
207   u8 *hp, *vp;
208
209   hp = (u8 *) h->mheap;
210   vp = (u8 *) v;
211
212   ASSERT ((vp - hp) < 0x100000000ULL);
213   return vp - hp;
214 }
215
216 void BV (clib_bihash_init)
217   (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
218
219 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
220
221 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
222                               BVT (clib_bihash_kv) * add_v, int is_add);
223 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
224                              BVT (clib_bihash_kv) * search_v,
225                              BVT (clib_bihash_kv) * return_v);
226
227 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
228                                               void *callback, void *arg);
229
230 format_function_t BV (format_bihash);
231 format_function_t BV (format_bihash_kvp);
232 format_function_t BV (format_bihash_lru);
233
234 static inline int BV (clib_bihash_search_inline)
235   (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
236 {
237   u64 hash;
238   u32 bucket_index;
239   BVT (clib_bihash_value) * v;
240   BVT (clib_bihash_bucket) * b;
241   BVT (clib_bihash_kv) * kvp;
242   int i, limit;
243
244   hash = BV (clib_bihash_hash) (key_result);
245
246   bucket_index = hash & (h->nbuckets - 1);
247   b = &h->buckets[bucket_index];
248
249   if (b->offset == 0)
250     return -1;
251
252   /* Check the cache, if currently enabled */
253   if (PREDICT_TRUE (b->cache_lru & (1 << 15)))
254     {
255       limit = BIHASH_KVP_CACHE_SIZE;
256       kvp = b->cache;
257       for (i = 0; i < limit; i++)
258         {
259           if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
260             {
261               *key_result = kvp[i];
262               h->cache_hits++;
263               return 0;
264             }
265         }
266     }
267
268   hash >>= h->log2_nbuckets;
269
270   v = BV (clib_bihash_get_value) (h, b->offset);
271
272   /* If the bucket has unresolvable collisions, use linear search */
273   limit = BIHASH_KVP_PER_PAGE;
274   v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
275   if (PREDICT_FALSE (b->linear_search))
276     limit <<= b->log2_pages;
277
278   for (i = 0; i < limit; i++)
279     {
280       if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
281         {
282           u8 cache_slot;
283           *key_result = v->kvp[i];
284
285           /* Shut off the cache */
286           BV (clib_bihash_cache_enable_disable) (b, 0);
287           CLIB_MEMORY_BARRIER ();
288
289           cache_slot = BV (clib_bihash_get_lru) (b);
290           b->cache[cache_slot] = v->kvp[i];
291           BV (clib_bihash_update_lru) (b, cache_slot);
292
293           /* Reenable the cache */
294           BV (clib_bihash_cache_enable_disable) (b, 1);
295           h->cache_misses++;
296           return 0;
297         }
298     }
299   return -1;
300 }
301
302 static inline int BV (clib_bihash_search_inline_2)
303   (BVT (clib_bihash) * h,
304    BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
305 {
306   u64 hash;
307   u32 bucket_index;
308   BVT (clib_bihash_value) * v;
309   BVT (clib_bihash_bucket) * b;
310   BVT (clib_bihash_kv) * kvp;
311   int i, limit;
312
313   ASSERT (valuep);
314
315   hash = BV (clib_bihash_hash) (search_key);
316
317   bucket_index = hash & (h->nbuckets - 1);
318   b = &h->buckets[bucket_index];
319
320   if (b->offset == 0)
321     return -1;
322
323   /* Check the cache, if currently enabled */
324   if (PREDICT_TRUE (b->cache_lru & (1 << 15)))
325     {
326       limit = BIHASH_KVP_CACHE_SIZE;
327       kvp = b->cache;
328       for (i = 0; i < limit; i++)
329         {
330           if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
331             {
332               *valuep = kvp[i];
333               h->cache_hits++;
334               return 0;
335             }
336         }
337     }
338
339   hash >>= h->log2_nbuckets;
340   v = BV (clib_bihash_get_value) (h, b->offset);
341
342   /* If the bucket has unresolvable collisions, use linear search */
343   limit = BIHASH_KVP_PER_PAGE;
344   v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
345   if (PREDICT_FALSE (b->linear_search))
346     limit <<= b->log2_pages;
347
348   for (i = 0; i < limit; i++)
349     {
350       if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
351         {
352           u8 cache_slot;
353           *valuep = v->kvp[i];
354
355           /* Shut off the cache */
356           BV (clib_bihash_cache_enable_disable) (b, 0);
357           CLIB_MEMORY_BARRIER ();
358
359           cache_slot = BV (clib_bihash_get_lru) (b);
360           b->cache[cache_slot] = v->kvp[i];
361           BV (clib_bihash_update_lru) (b, cache_slot);
362
363           /* Reenable the cache */
364           BV (clib_bihash_cache_enable_disable) (b, 1);
365           h->cache_misses++;
366           return 0;
367         }
368     }
369   return -1;
370 }
371
372 #endif /* __included_bihash_template_h__ */
373
374 /** @endcond */
375
376 /*
377  * fd.io coding-style-patch-verification: ON
378  *
379  * Local Variables:
380  * eval: (c-set-style "gnu")
381  * End:
382  */