remove extra parentheses in return statement
[dpdk.git] / lib / librte_sched / rte_bitmap.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __INCLUDE_RTE_BITMAP_H__
35 #define __INCLUDE_RTE_BITMAP_H__
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42  * @file
43  * RTE Bitmap
44  *
45  * The bitmap component provides a mechanism to manage large arrays of bits
46  * through bit get/set/clear and bit array scan operations.
47  *
48  * The bitmap scan operation is optimized for 64-bit CPUs using 64-byte cache
49  * lines. The bitmap is hierarchically organized using two arrays (array1 and
50  * array2), with each bit in array1 being associated with a full cache line
51  * (512 bits) of bitmap bits, which are stored in array2: the bit in array1 is
52  * set only when there is at least one bit set within its associated array2
53  * bits, otherwise the bit in array1 is cleared. The read and write operations
54  * for array1 and array2 are always done in slabs of 64 bits.
55  *
56  * This bitmap is not thread safe. For lock free operation on a specific bitmap
57  * instance, a single writer thread performing bit set/clear operations is
58  * allowed, only the writer thread can do bitmap scan operations, while there
59  * can be several reader threads performing bit get operations in parallel with
60  * the writer thread. When the use of locking primitives is acceptable, the
61  * serialization of the bit set/clear and bitmap scan operations needs to be
62  * enforced by the caller, while the bit get operation does not require locking
63  * the bitmap.
64  *
65  ***/
66
67 #include <rte_common.h>
68 #include <rte_debug.h>
69 #include <rte_memory.h>
70 #include <rte_branch_prediction.h>
71 #include <rte_prefetch.h>
72
73 #ifndef RTE_BITMAP_OPTIMIZATIONS
74 #define RTE_BITMAP_OPTIMIZATIONS                         1
75 #endif
76
77 /* Slab */
78 #define RTE_BITMAP_SLAB_BIT_SIZE                 64
79 #define RTE_BITMAP_SLAB_BIT_SIZE_LOG2            6
80 #define RTE_BITMAP_SLAB_BIT_MASK                 (RTE_BITMAP_SLAB_BIT_SIZE - 1)
81
82 /* Cache line (CL) */
83 #define RTE_BITMAP_CL_BIT_SIZE                   (RTE_CACHE_LINE_SIZE * 8)
84 #define RTE_BITMAP_CL_BIT_SIZE_LOG2              9
85 #define RTE_BITMAP_CL_BIT_MASK                   (RTE_BITMAP_CL_BIT_SIZE - 1)
86
87 #define RTE_BITMAP_CL_SLAB_SIZE                  (RTE_BITMAP_CL_BIT_SIZE / RTE_BITMAP_SLAB_BIT_SIZE)
88 #define RTE_BITMAP_CL_SLAB_SIZE_LOG2             3
89 #define RTE_BITMAP_CL_SLAB_MASK                  (RTE_BITMAP_CL_SLAB_SIZE - 1)
90
91 /** Bitmap data structure */
92 struct rte_bitmap {
93         /* Context for array1 and array2 */
94         uint64_t *array1;                        /**< Bitmap array1 */
95         uint64_t *array2;                        /**< Bitmap array2 */
96         uint32_t array1_size;                    /**< Number of 64-bit slabs in array1 that are actually used */
97         uint32_t array2_size;                    /**< Number of 64-bit slabs in array2 */
98
99         /* Context for the "scan next" operation */
100         uint32_t index1;  /**< Bitmap scan: Index of current array1 slab */
101         uint32_t offset1; /**< Bitmap scan: Offset of current bit within current array1 slab */
102         uint32_t index2;  /**< Bitmap scan: Index of current array2 slab */
103         uint32_t go2;     /**< Bitmap scan: Go/stop condition for current array2 cache line */
104
105         /* Storage space for array1 and array2 */
106         uint8_t memory[0];
107 };
108
109 static inline void
110 __rte_bitmap_index1_inc(struct rte_bitmap *bmp)
111 {
112         bmp->index1 = (bmp->index1 + 1) & (bmp->array1_size - 1);
113 }
114
115 static inline uint64_t
116 __rte_bitmap_mask1_get(struct rte_bitmap *bmp)
117 {
118         return (~1lu) << bmp->offset1;
119 }
120
121 static inline void
122 __rte_bitmap_index2_set(struct rte_bitmap *bmp)
123 {
124         bmp->index2 = (((bmp->index1 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2) + bmp->offset1) << RTE_BITMAP_CL_SLAB_SIZE_LOG2);
125 }
126
127 #if RTE_BITMAP_OPTIMIZATIONS
128
129 static inline int
130 rte_bsf64(uint64_t slab, uint32_t *pos)
131 {
132         if (likely(slab == 0)) {
133                 return 0;
134         }
135
136         *pos = __builtin_ctzll(slab);
137         return 1;
138 }
139
140 #else
141
142 static inline int
143 rte_bsf64(uint64_t slab, uint32_t *pos)
144 {
145         uint64_t mask;
146         uint32_t i;
147
148         if (likely(slab == 0)) {
149                 return 0;
150         }
151
152         for (i = 0, mask = 1; i < RTE_BITMAP_SLAB_BIT_SIZE; i ++, mask <<= 1) {
153                 if (unlikely(slab & mask)) {
154                         *pos = i;
155                         return 1;
156                 }
157         }
158
159         return 0;
160 }
161
162 #endif
163
164 static inline uint32_t
165 __rte_bitmap_get_memory_footprint(uint32_t n_bits,
166         uint32_t *array1_byte_offset, uint32_t *array1_slabs,
167         uint32_t *array2_byte_offset, uint32_t *array2_slabs)
168 {
169         uint32_t n_slabs_context, n_slabs_array1, n_cache_lines_context_and_array1;
170         uint32_t n_cache_lines_array2;
171         uint32_t n_bytes_total;
172
173         n_cache_lines_array2 = (n_bits + RTE_BITMAP_CL_BIT_SIZE - 1) / RTE_BITMAP_CL_BIT_SIZE;
174         n_slabs_array1 = (n_cache_lines_array2 + RTE_BITMAP_SLAB_BIT_SIZE - 1) / RTE_BITMAP_SLAB_BIT_SIZE;
175         n_slabs_array1 = rte_align32pow2(n_slabs_array1);
176         n_slabs_context = (sizeof(struct rte_bitmap) + (RTE_BITMAP_SLAB_BIT_SIZE / 8) - 1) / (RTE_BITMAP_SLAB_BIT_SIZE / 8);
177         n_cache_lines_context_and_array1 = (n_slabs_context + n_slabs_array1 + RTE_BITMAP_CL_SLAB_SIZE - 1) / RTE_BITMAP_CL_SLAB_SIZE;
178         n_bytes_total = (n_cache_lines_context_and_array1 + n_cache_lines_array2) * RTE_CACHE_LINE_SIZE;
179
180         if (array1_byte_offset) {
181                 *array1_byte_offset = n_slabs_context * (RTE_BITMAP_SLAB_BIT_SIZE / 8);
182         }
183         if (array1_slabs) {
184                 *array1_slabs = n_slabs_array1;
185         }
186         if (array2_byte_offset) {
187                 *array2_byte_offset = n_cache_lines_context_and_array1 * RTE_CACHE_LINE_SIZE;
188         }
189         if (array2_slabs) {
190                 *array2_slabs = n_cache_lines_array2 * RTE_BITMAP_CL_SLAB_SIZE;
191         }
192
193         return n_bytes_total;
194 }
195
196 static inline void
197 __rte_bitmap_scan_init(struct rte_bitmap *bmp)
198 {
199         bmp->index1 = bmp->array1_size - 1;
200         bmp->offset1 = RTE_BITMAP_SLAB_BIT_SIZE - 1;
201         __rte_bitmap_index2_set(bmp);
202         bmp->index2 += RTE_BITMAP_CL_SLAB_SIZE;
203
204         bmp->go2 = 0;
205 }
206
207 /**
208  * Bitmap memory footprint calculation
209  *
210  * @param n_bits
211  *   Number of bits in the bitmap
212  * @return
213  *   Bitmap memory footprint measured in bytes on success, 0 on error
214  */
215 static inline uint32_t
216 rte_bitmap_get_memory_footprint(uint32_t n_bits) {
217         /* Check input arguments */
218         if (n_bits == 0) {
219                 return 0;
220         }
221
222         return __rte_bitmap_get_memory_footprint(n_bits, NULL, NULL, NULL, NULL);
223 }
224
225 /**
226  * Bitmap initialization
227  *
228  * @param mem_size
229  *   Minimum expected size of bitmap.
230  * @param mem
231  *   Base address of array1 and array2.
232  * @param n_bits
233  *   Number of pre-allocated bits in array2. Must be non-zero and multiple of 512.
234  * @return
235  *   Handle to bitmap instance.
236  */
237 static inline struct rte_bitmap *
238 rte_bitmap_init(uint32_t n_bits, uint8_t *mem, uint32_t mem_size)
239 {
240         struct rte_bitmap *bmp;
241         uint32_t array1_byte_offset, array1_slabs, array2_byte_offset, array2_slabs;
242         uint32_t size;
243
244         /* Check input arguments */
245         if (n_bits == 0) {
246                 return NULL;
247         }
248
249         if ((mem == NULL) || (((uintptr_t) mem) & RTE_CACHE_LINE_MASK)) {
250                 return NULL;
251         }
252
253         size = __rte_bitmap_get_memory_footprint(n_bits,
254                 &array1_byte_offset, &array1_slabs,
255                 &array2_byte_offset, &array2_slabs);
256         if (size < mem_size) {
257                 return NULL;
258         }
259
260         /* Setup bitmap */
261         memset(mem, 0, size);
262         bmp = (struct rte_bitmap *) mem;
263
264         bmp->array1 = (uint64_t *) &mem[array1_byte_offset];
265         bmp->array1_size = array1_slabs;
266         bmp->array2 = (uint64_t *) &mem[array2_byte_offset];
267         bmp->array2_size = array2_slabs;
268
269         __rte_bitmap_scan_init(bmp);
270
271         return bmp;
272 }
273
274 /**
275  * Bitmap free
276  *
277  * @param bmp
278  *   Handle to bitmap instance
279  * @return
280  *   0 upon success, error code otherwise
281  */
282 static inline int
283 rte_bitmap_free(struct rte_bitmap *bmp)
284 {
285         /* Check input arguments */
286         if (bmp == NULL) {
287                 return -1;
288         }
289
290         return 0;
291 }
292
293 /**
294  * Bitmap reset
295  *
296  * @param bmp
297  *   Handle to bitmap instance
298  */
299 static inline void
300 rte_bitmap_reset(struct rte_bitmap *bmp)
301 {
302         memset(bmp->array1, 0, bmp->array1_size * sizeof(uint64_t));
303         memset(bmp->array2, 0, bmp->array2_size * sizeof(uint64_t));
304         __rte_bitmap_scan_init(bmp);
305 }
306
307 /**
308  * Bitmap location prefetch into CPU L1 cache
309  *
310  * @param bmp
311  *   Handle to bitmap instance
312  * @param pos
313  *   Bit position
314  * @return
315  *   0 upon success, error code otherwise
316  */
317 static inline void
318 rte_bitmap_prefetch0(struct rte_bitmap *bmp, uint32_t pos)
319 {
320         uint64_t *slab2;
321         uint32_t index2;
322
323         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
324         slab2 = bmp->array2 + index2;
325         rte_prefetch0((void *) slab2);
326 }
327
328 /**
329  * Bitmap bit get
330  *
331  * @param bmp
332  *   Handle to bitmap instance
333  * @param pos
334  *   Bit position
335  * @return
336  *   0 when bit is cleared, non-zero when bit is set
337  */
338 static inline uint64_t
339 rte_bitmap_get(struct rte_bitmap *bmp, uint32_t pos)
340 {
341         uint64_t *slab2;
342         uint32_t index2, offset2;
343
344         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
345         offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
346         slab2 = bmp->array2 + index2;
347         return (*slab2) & (1lu << offset2);
348 }
349
350 /**
351  * Bitmap bit set
352  *
353  * @param bmp
354  *   Handle to bitmap instance
355  * @param pos
356  *   Bit position
357  */
358 static inline void
359 rte_bitmap_set(struct rte_bitmap *bmp, uint32_t pos)
360 {
361         uint64_t *slab1, *slab2;
362         uint32_t index1, index2, offset1, offset2;
363
364         /* Set bit in array2 slab and set bit in array1 slab */
365         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
366         offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
367         index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
368         offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
369         slab2 = bmp->array2 + index2;
370         slab1 = bmp->array1 + index1;
371
372         *slab2 |= 1lu << offset2;
373         *slab1 |= 1lu << offset1;
374 }
375
376 /**
377  * Bitmap slab set
378  *
379  * @param bmp
380  *   Handle to bitmap instance
381  * @param pos
382  *   Bit position identifying the array2 slab
383  * @param slab
384  *   Value to be assigned to the 64-bit slab in array2
385  */
386 static inline void
387 rte_bitmap_set_slab(struct rte_bitmap *bmp, uint32_t pos, uint64_t slab)
388 {
389         uint64_t *slab1, *slab2;
390         uint32_t index1, index2, offset1;
391
392         /* Set bits in array2 slab and set bit in array1 slab */
393         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
394         index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
395         offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
396         slab2 = bmp->array2 + index2;
397         slab1 = bmp->array1 + index1;
398
399         *slab2 |= slab;
400         *slab1 |= 1lu << offset1;
401 }
402
403 static inline uint64_t
404 __rte_bitmap_line_not_empty(uint64_t *slab2)
405 {
406         uint64_t v1, v2, v3, v4;
407
408         v1 = slab2[0] | slab2[1];
409         v2 = slab2[2] | slab2[3];
410         v3 = slab2[4] | slab2[5];
411         v4 = slab2[6] | slab2[7];
412         v1 |= v2;
413         v3 |= v4;
414
415         return v1 | v3;
416 }
417
418 /**
419  * Bitmap bit clear
420  *
421  * @param bmp
422  *   Handle to bitmap instance
423  * @param pos
424  *   Bit position
425  */
426 static inline void
427 rte_bitmap_clear(struct rte_bitmap *bmp, uint32_t pos)
428 {
429         uint64_t *slab1, *slab2;
430         uint32_t index1, index2, offset1, offset2;
431
432         /* Clear bit in array2 slab */
433         index2 = pos >> RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
434         offset2 = pos & RTE_BITMAP_SLAB_BIT_MASK;
435         slab2 = bmp->array2 + index2;
436
437         /* Return if array2 slab is not all-zeros */
438         *slab2 &= ~(1lu << offset2);
439         if (*slab2){
440                 return;
441         }
442
443         /* Check the entire cache line of array2 for all-zeros */
444         index2 &= ~ RTE_BITMAP_CL_SLAB_MASK;
445         slab2 = bmp->array2 + index2;
446         if (__rte_bitmap_line_not_empty(slab2)) {
447                 return;
448         }
449
450         /* The array2 cache line is all-zeros, so clear bit in array1 slab */
451         index1 = pos >> (RTE_BITMAP_SLAB_BIT_SIZE_LOG2 + RTE_BITMAP_CL_BIT_SIZE_LOG2);
452         offset1 = (pos >> RTE_BITMAP_CL_BIT_SIZE_LOG2) & RTE_BITMAP_SLAB_BIT_MASK;
453         slab1 = bmp->array1 + index1;
454         *slab1 &= ~(1lu << offset1);
455
456         return;
457 }
458
459 static inline int
460 __rte_bitmap_scan_search(struct rte_bitmap *bmp)
461 {
462         uint64_t value1;
463         uint32_t i;
464
465         /* Check current array1 slab */
466         value1 = bmp->array1[bmp->index1];
467         value1 &= __rte_bitmap_mask1_get(bmp);
468
469         if (rte_bsf64(value1, &bmp->offset1)) {
470                 return 1;
471         }
472
473         __rte_bitmap_index1_inc(bmp);
474         bmp->offset1 = 0;
475
476         /* Look for another array1 slab */
477         for (i = 0; i < bmp->array1_size; i ++, __rte_bitmap_index1_inc(bmp)) {
478                 value1 = bmp->array1[bmp->index1];
479
480                 if (rte_bsf64(value1, &bmp->offset1)) {
481                         return 1;
482                 }
483         }
484
485         return 0;
486 }
487
488 static inline void
489 __rte_bitmap_scan_read_init(struct rte_bitmap *bmp)
490 {
491         __rte_bitmap_index2_set(bmp);
492         bmp->go2 = 1;
493         rte_prefetch1((void *)(bmp->array2 + bmp->index2 + 8));
494 }
495
496 static inline int
497 __rte_bitmap_scan_read(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
498 {
499         uint64_t *slab2;
500
501         slab2 = bmp->array2 + bmp->index2;
502         for ( ; bmp->go2 ; bmp->index2 ++, slab2 ++, bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK) {
503                 if (*slab2) {
504                         *pos = bmp->index2 << RTE_BITMAP_SLAB_BIT_SIZE_LOG2;
505                         *slab = *slab2;
506
507                         bmp->index2 ++;
508                         slab2 ++;
509                         bmp->go2 = bmp->index2 & RTE_BITMAP_CL_SLAB_MASK;
510                         return 1;
511                 }
512         }
513
514         return 0;
515 }
516
517 /**
518  * Bitmap scan (with automatic wrap-around)
519  *
520  * @param bmp
521  *   Handle to bitmap instance
522  * @param pos
523  *   When function call returns 1, pos contains the position of the next set
524  *   bit, otherwise not modified
525  * @param slab
526  *   When function call returns 1, slab contains the value of the entire 64-bit
527  *   slab where the bit indicated by pos is located. Slabs are always 64-bit
528  *   aligned, so the position of the first bit of the slab (this bit is not
529  *   necessarily set) is pos / 64. Once a slab has been returned by the bitmap
530  *   scan operation, the internal pointers of the bitmap are updated to point
531  *   after this slab, so the same slab will not be returned again if it
532  *   contains more than one bit which is set. When function call returns 0,
533  *   slab is not modified.
534  * @return
535  *   0 if there is no bit set in the bitmap, 1 otherwise
536  */
537 static inline int
538 rte_bitmap_scan(struct rte_bitmap *bmp, uint32_t *pos, uint64_t *slab)
539 {
540         /* Return data from current array2 line if available */
541         if (__rte_bitmap_scan_read(bmp, pos, slab)) {
542                 return 1;
543         }
544
545         /* Look for non-empty array2 line */
546         if (__rte_bitmap_scan_search(bmp)) {
547                 __rte_bitmap_scan_read_init(bmp);
548                 __rte_bitmap_scan_read(bmp, pos, slab);
549                 return 1;
550         }
551
552         /* Empty bitmap */
553         return 0;
554 }
555
556 #ifdef __cplusplus
557 }
558 #endif
559
560 #endif /* __INCLUDE_RTE_BITMAP_H__ */