lib/axmap: Optimize __axmap_set()
[fio.git] / lib / axmap.c
1 /*
2  * Bitmap of bitmaps, where each layer is number-of-bits-per-word smaller than
3  * the previous. Hence an 'axmap', since we axe each previous layer into a
4  * much smaller piece. I swear, that is why it's named like that. It has
5  * nothing to do with anything remotely narcissistic.
6  *
7  * A set bit at layer N indicates a full word at layer N-1, and so forth. As
8  * the bitmap becomes progressively more full, checking for existence
9  * becomes cheaper (since fewer layers are walked, making it a lot more
10  * cache friendly) and locating the next free space likewise.
11  *
12  * Axmaps get pretty close to optimal (1 bit per block) space usage, since
13  * layers quickly diminish in size. Doing the size math is straight forward,
14  * since we have log64(blocks) layers of maps. For 20000 blocks, overhead
15  * is roughly 1.9%, or 1.019 bits per block. The number quickly converges
16  * towards 1.0158, or 1.58% of overhead.
17  */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <assert.h>
22
23 #include "../arch/arch.h"
24 #include "axmap.h"
25 #include "../minmax.h"
26
27 #if BITS_PER_LONG == 64
28 #define UNIT_SHIFT              6
29 #elif BITS_PER_LONG == 32
30 #define UNIT_SHIFT              5
31 #else
32 #error "Number of arch bits unknown"
33 #endif
34
35 #define BLOCKS_PER_UNIT         (1U << UNIT_SHIFT)
36 #define BLOCKS_PER_UNIT_MASK    (BLOCKS_PER_UNIT - 1)
37
38 static const unsigned long bit_masks[] = {
39         0x0000000000000000, 0x0000000000000001, 0x0000000000000003, 0x0000000000000007,
40         0x000000000000000f, 0x000000000000001f, 0x000000000000003f, 0x000000000000007f,
41         0x00000000000000ff, 0x00000000000001ff, 0x00000000000003ff, 0x00000000000007ff,
42         0x0000000000000fff, 0x0000000000001fff, 0x0000000000003fff, 0x0000000000007fff,
43         0x000000000000ffff, 0x000000000001ffff, 0x000000000003ffff, 0x000000000007ffff,
44         0x00000000000fffff, 0x00000000001fffff, 0x00000000003fffff, 0x00000000007fffff,
45         0x0000000000ffffff, 0x0000000001ffffff, 0x0000000003ffffff, 0x0000000007ffffff,
46         0x000000000fffffff, 0x000000001fffffff, 0x000000003fffffff, 0x000000007fffffff,
47         0x00000000ffffffff,
48 #if BITS_PER_LONG == 64
49         0x00000001ffffffff, 0x00000003ffffffff, 0x00000007ffffffff, 0x0000000fffffffff,
50         0x0000001fffffffff, 0x0000003fffffffff, 0x0000007fffffffff, 0x000000ffffffffff,
51         0x000001ffffffffff, 0x000003ffffffffff, 0x000007ffffffffff, 0x00000fffffffffff,
52         0x00001fffffffffff, 0x00003fffffffffff, 0x00007fffffffffff, 0x0000ffffffffffff,
53         0x0001ffffffffffff, 0x0003ffffffffffff, 0x0007ffffffffffff, 0x000fffffffffffff,
54         0x001fffffffffffff, 0x003fffffffffffff, 0x007fffffffffffff, 0x00ffffffffffffff,
55         0x01ffffffffffffff, 0x03ffffffffffffff, 0x07ffffffffffffff, 0x0fffffffffffffff,
56         0x1fffffffffffffff, 0x3fffffffffffffff, 0x7fffffffffffffff, 0xffffffffffffffff
57 #endif
58 };
59
60 /**
61  * struct axmap_level - a bitmap used to implement struct axmap
62  * @level: Level index. Each map has at least one level with index zero. The
63  *      higher the level index, the fewer bits a struct axmap_level contains.
64  * @map_size: Number of elements of the @map array.
65  * @map: A bitmap with @map_size elements.
66  */
67 struct axmap_level {
68         int level;
69         unsigned long map_size;
70         unsigned long *map;
71 };
72
73 /**
74  * struct axmap - a set that can store numbers 0 .. @nr_bits - 1
75  * @nr_level: Number of elements of the @levels array.
76  * @levels: struct axmap_level array in which lower levels contain more bits
77  *      than higher levels.
78  * @nr_bits: One more than the highest value stored in the set.
79  */
80 struct axmap {
81         unsigned int nr_levels;
82         struct axmap_level *levels;
83         uint64_t nr_bits;
84 };
85
86 /* Remove all elements from the @axmap set */
87 void axmap_reset(struct axmap *axmap)
88 {
89         int i;
90
91         for (i = 0; i < axmap->nr_levels; i++) {
92                 struct axmap_level *al = &axmap->levels[i];
93
94                 memset(al->map, 0, al->map_size * sizeof(unsigned long));
95         }
96 }
97
98 void axmap_free(struct axmap *axmap)
99 {
100         unsigned int i;
101
102         if (!axmap)
103                 return;
104
105         for (i = 0; i < axmap->nr_levels; i++)
106                 free(axmap->levels[i].map);
107
108         free(axmap->levels);
109         free(axmap);
110 }
111
112 /* Allocate memory for a set that can store the numbers 0 .. @nr_bits - 1. */
113 struct axmap *axmap_new(unsigned long nr_bits)
114 {
115         struct axmap *axmap;
116         unsigned int i, levels;
117
118         axmap = malloc(sizeof(*axmap));
119         if (!axmap)
120                 return NULL;
121
122         levels = 1;
123         i = (nr_bits + BLOCKS_PER_UNIT - 1) >> UNIT_SHIFT;
124         while (i > 1) {
125                 i = (i + BLOCKS_PER_UNIT - 1) >> UNIT_SHIFT;
126                 levels++;
127         }
128
129         axmap->nr_levels = levels;
130         axmap->levels = calloc(axmap->nr_levels, sizeof(struct axmap_level));
131         if (!axmap->levels)
132                 goto free_axmap;
133         axmap->nr_bits = nr_bits;
134
135         for (i = 0; i < axmap->nr_levels; i++) {
136                 struct axmap_level *al = &axmap->levels[i];
137
138                 al->level = i;
139                 al->map_size = (nr_bits + BLOCKS_PER_UNIT - 1) >> UNIT_SHIFT;
140                 al->map = malloc(al->map_size * sizeof(unsigned long));
141                 if (!al->map)
142                         goto free_levels;
143
144                 nr_bits = (nr_bits + BLOCKS_PER_UNIT - 1) >> UNIT_SHIFT;
145         }
146
147         axmap_reset(axmap);
148         return axmap;
149
150 free_levels:
151         for (i = 0; i < axmap->nr_levels; i++)
152                 free(axmap->levels[i].map);
153
154         free(axmap->levels);
155
156 free_axmap:
157         free(axmap);
158         return NULL;
159 }
160
161 /*
162  * Call @func for each level, starting at level zero, until a level is found
163  * for which @func returns true. Return false if none of the @func calls
164  * returns true.
165  */
166 static bool axmap_handler(struct axmap *axmap, uint64_t bit_nr,
167                           bool (*func)(struct axmap_level *, unsigned long, unsigned int,
168                           void *), void *data)
169 {
170         struct axmap_level *al;
171         uint64_t index = bit_nr;
172         int i;
173
174         for (i = 0; i < axmap->nr_levels; i++) {
175                 unsigned long offset = index >> UNIT_SHIFT;
176                 unsigned int bit = index & BLOCKS_PER_UNIT_MASK;
177
178                 al = &axmap->levels[i];
179
180                 if (func(al, offset, bit, data))
181                         return true;
182
183                 if (index)
184                         index >>= UNIT_SHIFT;
185         }
186
187         return false;
188 }
189
190 /*
191  * Call @func for each level, starting at the highest level, until a level is
192  * found for which @func returns true. Return false if none of the @func calls
193  * returns true.
194  */
195 static bool axmap_handler_topdown(struct axmap *axmap, uint64_t bit_nr,
196         bool (*func)(struct axmap_level *, unsigned long, unsigned int, void *))
197 {
198         int i;
199
200         for (i = axmap->nr_levels - 1; i >= 0; i--) {
201                 unsigned long index = bit_nr >> (UNIT_SHIFT * i);
202                 unsigned long offset = index >> UNIT_SHIFT;
203                 unsigned int bit = index & BLOCKS_PER_UNIT_MASK;
204
205                 if (func(&axmap->levels[i], offset, bit, NULL))
206                         return true;
207         }
208
209         return false;
210 }
211
212 struct axmap_set_data {
213         unsigned int nr_bits;
214         unsigned int set_bits;
215 };
216
217 /*
218  * Set at most @__data->nr_bits bits in @al at offset @offset. Do not exceed
219  * the boundary of the element at offset @offset. Return the number of bits
220  * that have been set in @__data->set_bits if @al->level == 0.
221  */
222 static bool axmap_set_fn(struct axmap_level *al, unsigned long offset,
223                          unsigned int bit, void *__data)
224 {
225         struct axmap_set_data *data = __data;
226         unsigned long mask, overlap;
227         unsigned int nr_bits;
228
229         nr_bits = min(data->nr_bits, BLOCKS_PER_UNIT - bit);
230
231         mask = bit_masks[nr_bits] << bit;
232
233         /*
234          * Mask off any potential overlap, only sets contig regions
235          */
236         overlap = al->map[offset] & mask;
237         if (overlap == mask) {
238                 data->set_bits = 0;
239                 return true;
240         }
241
242         if (overlap) {
243                 nr_bits = ffz(~overlap) - bit;
244                 mask = bit_masks[nr_bits] << bit;
245         }
246
247         assert(mask);
248         assert(!(al->map[offset] & mask));
249         al->map[offset] |= mask;
250
251         if (!al->level)
252                 data->set_bits = nr_bits;
253
254         /* For the next level */
255         data->nr_bits = 1;
256
257         return al->map[offset] != -1UL;
258 }
259
260 /*
261  * Set up to @data->nr_bits starting from @bit_nr in @axmap. Start at
262  * @bit_nr. If that bit has not yet been set then set it and continue until
263  * either @data->nr_bits have been set or a 1 bit is found. Store the number
264  * of bits that have been set in @data->set_bits. It is guaranteed that all
265  * bits that have been requested to set fit in the same unsigned long word of
266  * level 0 of @axmap.
267  */
268 static void __axmap_set(struct axmap *axmap, uint64_t bit_nr,
269                          struct axmap_set_data *data)
270 {
271         unsigned int nr_bits = data->nr_bits;
272
273         if (bit_nr > axmap->nr_bits)
274                 return;
275         else if (bit_nr + nr_bits > axmap->nr_bits)
276                 nr_bits = axmap->nr_bits - bit_nr;
277
278         assert(nr_bits <= BLOCKS_PER_UNIT);
279
280         axmap_handler(axmap, bit_nr, axmap_set_fn, data);
281 }
282
283 void axmap_set(struct axmap *axmap, uint64_t bit_nr)
284 {
285         struct axmap_set_data data = { .nr_bits = 1, };
286
287         __axmap_set(axmap, bit_nr, &data);
288 }
289
290 /*
291  * Set up to @nr_bits starting from @bit in @axmap. Start at @bit. If that
292  * bit has not yet been set then set it and continue until either @nr_bits
293  * have been set or a 1 bit is found. Return the number of bits that have been
294  * set.
295  */
296 unsigned int axmap_set_nr(struct axmap *axmap, uint64_t bit_nr,
297                           unsigned int nr_bits)
298 {
299         unsigned int set_bits = 0;
300
301         do {
302                 struct axmap_set_data data = { .nr_bits = nr_bits, };
303                 unsigned int max_bits, this_set;
304
305                 max_bits = BLOCKS_PER_UNIT - (bit_nr & BLOCKS_PER_UNIT_MASK);
306                 if (nr_bits > max_bits)
307                         data.nr_bits = max_bits;
308
309                 this_set = data.nr_bits;
310                 __axmap_set(axmap, bit_nr, &data);
311                 set_bits += data.set_bits;
312                 if (data.set_bits != this_set)
313                         break;
314
315                 nr_bits -= data.set_bits;
316                 bit_nr += data.set_bits;
317         } while (nr_bits);
318
319         return set_bits;
320 }
321
322 static bool axmap_isset_fn(struct axmap_level *al, unsigned long offset,
323                            unsigned int bit, void *unused)
324 {
325         return (al->map[offset] & (1UL << bit)) != 0;
326 }
327
328 bool axmap_isset(struct axmap *axmap, uint64_t bit_nr)
329 {
330         if (bit_nr <= axmap->nr_bits)
331                 return axmap_handler_topdown(axmap, bit_nr, axmap_isset_fn);
332
333         return false;
334 }
335
336 /*
337  * Find the first free bit that is at least as large as bit_nr.  Return
338  * -1 if no free bit is found before the end of the map.
339  */
340 static uint64_t axmap_find_first_free(struct axmap *axmap, uint64_t bit_nr)
341 {
342         int i;
343         unsigned long temp;
344         unsigned int bit;
345         uint64_t offset, base_index, index;
346         struct axmap_level *al;
347
348         index = 0;
349         for (i = axmap->nr_levels - 1; i >= 0; i--) {
350                 al = &axmap->levels[i];
351
352                 /* Shift previously calculated index for next level */
353                 index <<= UNIT_SHIFT;
354
355                 /*
356                  * Start from an index that's at least as large as the
357                  * originally passed in bit number.
358                  */
359                 base_index = bit_nr >> (UNIT_SHIFT * i);
360                 if (index < base_index)
361                         index = base_index;
362
363                 /* Get the offset and bit for this level */
364                 offset = index >> UNIT_SHIFT;
365                 bit = index & BLOCKS_PER_UNIT_MASK;
366
367                 /*
368                  * If the previous level had unused bits in its last
369                  * word, the offset could be bigger than the map at
370                  * this level. That means no free bits exist before the
371                  * end of the map, so return -1.
372                  */
373                 if (offset >= al->map_size)
374                         return -1ULL;
375
376                 /* Check the first word starting with the specific bit */
377                 temp = ~bit_masks[bit] & ~al->map[offset];
378                 if (temp)
379                         goto found;
380
381                 /*
382                  * No free bit in the first word, so iterate
383                  * looking for a word with one or more free bits.
384                  */
385                 for (offset++; offset < al->map_size; offset++) {
386                         temp = ~al->map[offset];
387                         if (temp)
388                                 goto found;
389                 }
390
391                 /* Did not find a free bit */
392                 return -1ULL;
393
394 found:
395                 /* Compute the index of the free bit just found */
396                 index = (offset << UNIT_SHIFT) + ffz(~temp);
397         }
398
399         /* If found an unused bit in the last word of level 0, return -1 */
400         if (index >= axmap->nr_bits)
401                 return -1ULL;
402
403         return index;
404 }
405
406 /*
407  * 'bit_nr' is already set. Find the next free bit after this one.
408  * Return -1 if no free bits found.
409  */
410 uint64_t axmap_next_free(struct axmap *axmap, uint64_t bit_nr)
411 {
412         uint64_t ret;
413         uint64_t next_bit = bit_nr + 1;
414         unsigned long temp;
415         uint64_t offset;
416         unsigned int bit;
417
418         if (bit_nr >= axmap->nr_bits)
419                 return -1ULL;
420
421         /* If at the end of the map, wrap-around */
422         if (next_bit == axmap->nr_bits)
423                 next_bit = 0;
424
425         offset = next_bit >> UNIT_SHIFT;
426         bit = next_bit & BLOCKS_PER_UNIT_MASK;
427
428         /*
429          * As an optimization, do a quick check for a free bit
430          * in the current word at level 0. If not found, do
431          * a topdown search.
432          */
433         temp = ~bit_masks[bit] & ~axmap->levels[0].map[offset];
434         if (temp) {
435                 ret = (offset << UNIT_SHIFT) + ffz(~temp);
436
437                 /* Might have found an unused bit at level 0 */
438                 if (ret >= axmap->nr_bits)
439                         ret = -1ULL;
440         } else
441                 ret = axmap_find_first_free(axmap, next_bit);
442
443         /*
444          * If there are no free bits starting at next_bit and going
445          * to the end of the map, wrap around by searching again
446          * starting at bit 0.
447          */
448         if (ret == -1ULL && next_bit != 0)
449                 ret = axmap_find_first_free(axmap, 0);
450         return ret;
451 }