summaryrefslogtreecommitdiffstats
path: root/src/test/objectstore/BitAllocator_test.cc
blob: ff946afffab95f0810388f73055a830e707ae9c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Bitmap based in-memory allocator unit test cases.
 * Author: Ramesh Chander, Ramesh.Chander@sandisk.com
 */

#include "include/Context.h"
#include "common/ceph_argparse.h"
#include "os/bluestore/BitAllocator.h"
#include "test/unit.h"
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <sstream>
#include <gtest/gtest.h>


#define bmap_test_assert(x) ASSERT_EQ(true, (x))
#define NUM_THREADS 16
#define MAX_BLOCKS (1024 * 1024 * 1)

TEST(BitAllocator, test_bmap_iter)
{
  int num_items = 5;
  int off = 2;

  class BmapEntityTmp {
      int64_t m_num;
      int64_t m_len;
    public:
      void init(int index) {
        m_num = index;
      }
      BmapEntityTmp() {

      }
      BmapEntityTmp(int num) {
        m_num = num;
        m_len = num;
      }

      int64_t get_index() {
        return m_num;
      }
      bool is_allocated(int64_t s, int64_t num)
      {
        return true;
      }
  };
  BmapEntityTmp *obj = NULL;
  int i = 0;
  mempool::bluestore_alloc::vector<BmapEntityTmp> *arr = new mempool::bluestore_alloc::vector<BmapEntityTmp>(num_items);
  for (i = 0; i < num_items; i++) {
    (*arr)[i].init(i);
  }
  BitMapEntityIter<BmapEntityTmp> iter = BitMapEntityIter<BmapEntityTmp>(arr, off, false);

  i = off;
  int count = 0;
  int64_t last_idx = off;
  while ((obj = iter.next())) {
    bmap_test_assert(obj->get_index() == last_idx);
    bmap_test_assert(obj->get_index() == i);
    bmap_test_assert(obj == &(*arr)[i]);
    last_idx = iter.index();
    i++;
    count++;
  }
  bmap_test_assert(i == num_items);
  bmap_test_assert(count == num_items - off);

  iter = BitMapEntityIter<BmapEntityTmp>(arr, off, true);

  i = off;
  last_idx = off;
  count = 0;
  while ((obj = iter.next())) {
    bmap_test_assert(obj->get_index() == last_idx);
    bmap_test_assert(obj->get_index() == i);
    bmap_test_assert(obj == &(*arr)[i]);
    last_idx = iter.index();

    i = (i + 1) % num_items;
    count++;
  }
  bmap_test_assert(i == off + 1);
  bmap_test_assert(count == num_items + 1);

  delete arr;

  num_items = 4;
  off = num_items - 1;

  arr = new mempool::bluestore_alloc::vector<BmapEntityTmp>(num_items);
  for (i = 0; i < num_items; i++) {
    (*arr)[i].init(i);
  }
  iter = BitMapEntityIter<BmapEntityTmp>(arr, off, true);
  i = off;
  last_idx = off;
  count = 0;
  while ((obj = (BmapEntityTmp*) iter.next())) {
    bmap_test_assert(obj->get_index() == last_idx);
    bmap_test_assert(obj->get_index() == i);
    bmap_test_assert(obj == &(*arr)[i]);
    last_idx = iter.index();
    i = (i + 1) % num_items;
    count++;
  }
  bmap_test_assert(i == (off + 1)%num_items);
  bmap_test_assert(count == num_items + 1);

  delete arr;

  /*
   * BitMapArea Iter tests.
   */
  BitMapArea *area = NULL;
  BitMapArea **children = new BitMapArea*[num_items];
  for (i = 0; i < num_items; i++) {
      children[i] = new BitMapAreaLeaf(BitMapArea::get_span_size(), i, false);
  }

  off = 0;
  BitMapAreaList *area_list = new BitMapAreaList(children, num_items);
  BmapEntityListIter area_iter = BmapEntityListIter(
                                area_list, (int64_t) 0);
  i = off;
  last_idx = off;
  count = 0;
  while ((area = area_iter.next())) {
    bmap_test_assert(area->get_index() == last_idx);
    bmap_test_assert(area->get_index() == i);
    bmap_test_assert(area == children[i]);
    last_idx = area_iter.index();
    i = (i + 1) % num_items;
    count++;
  }
  bmap_test_assert(i == off);
  bmap_test_assert(count == num_items);

  off = 0;
  area_iter = BmapEntityListIter(area_list, off, true);
  i = off;
  last_idx = off;
  count = 0;
  while ((area = area_iter.next())) {
    bmap_test_assert(area->get_index() == last_idx);
    bmap_test_assert(area->get_index() == i);
    bmap_test_assert(area == children[i]);
    last_idx = area_iter.index();
    i = (i + 1) % num_items;
    count++;
  }
  bmap_test_assert(i == (off + 1)%num_items);
  bmap_test_assert(count == num_items + 1);

  for (i = 0; i < num_items; i++)
    delete children[i];

  delete[] children;
  delete area_list;
}

TEST(BitAllocator, test_bmap_entry)
{
  int i = 0;
  int start = 0;
  int64_t scanned = 0;
  int64_t allocated = 0;
  int size = BmapEntry::size();

  BmapEntry *bmap = new BmapEntry(true);

  // Clear bits one by one and check they are cleared
  for (i = 0; i < size; i++) {
    bmap->clear_bit(i);
    bmap_test_assert(!bmap->check_bit(i));
  }

  // Set all bits again using set_bits
  bmap->set_bits(0, size);

  // clear 4 bits at a time and then check allocated
  for (i = 0; i < size/4; i++) {
    bmap->clear_bits(i * 4, 4);
    bmap_test_assert(!bmap->is_allocated(i * 4, 4));
  }

  // set all bits again
  bmap->set_bits(0, size);

  // clear alternate bits, check and set those bits
  for (i = 0; i < size/2; i++) {
    bmap->clear_bit(i * 2 + 1);
    bmap_test_assert(!bmap->check_bit(i * 2 + 1));
    bmap_test_assert(bmap->check_n_set_bit(i * 2 + 1));
  }

  // free 1, 2 and size bits at a time and try to find n cont bits
  for (i = 0; i < size / 4; i++) {
    bmap->clear_bits(i * 2 + 1, i + 1);
    bmap_test_assert(!bmap->check_bit(i * 2 + 1));
    bmap_test_assert(bmap->find_n_cont_bits(i * 2 + 1, i + 1) ==
        i + 1);
  }

  // free 1, 2 and size bits at a time and try to find any cont bits
  for (i = 0; i < size / 4; i++) {
    bmap->clear_bits(i * 2 + 1, i + 1);
    bmap_test_assert(!bmap->is_allocated(i * 2 + 1, i + 1));
  }

  for (i = 0; i < size / 4; i++) {
    bmap->clear_bits(i * 2 + 1, i + 1);
    allocated = bmap->find_first_set_bits(i + 1, 0, &start, &scanned);

    bmap_test_assert(allocated == i + 1);
    bmap_test_assert(scanned == ((i * 2 + 1) + (i + 1)));
    bmap_test_assert(start == i * 2 + 1);
    bmap->set_bits(0, BmapEntry::size());

  }



  // Find few bits at end of bitmap and find those
  bmap->clear_bits(0, 4);
  bmap->clear_bits(BmapEntry::size() - 12, 5);
  bmap->clear_bits(BmapEntry::size() - 6, 6);
  allocated = bmap->find_first_set_bits(6, 0, &start, &scanned);

  bmap_test_assert(allocated == 6);
  bmap_test_assert(scanned == BmapEntry::size() - 6 + 6);
  bmap_test_assert(start == BmapEntry::size() - 6);
  bmap_test_assert(bmap->is_allocated(start, 6));

  delete bmap;

  {

    bmap = new BmapEntry(false);
    start = -1;
    scanned = 0;
    allocated = 0;
    allocated = bmap->find_first_set_bits(1, 1, &start, &scanned);
    bmap_test_assert(allocated == 1);
    bmap_test_assert(start == 1);

    allocated = bmap->find_first_set_bits(1, BmapEntry::size() - 2, &start, &scanned);
    bmap_test_assert(allocated == 1);
    bmap_test_assert(start == BmapEntry::size() - 2);

    bmap->clear_bits(0, BmapEntry::size());
    bmap->set_bits(0, BmapEntry::size() / 4);
    allocated = bmap->find_first_set_bits(4, 2, &start, &scanned);
    bmap_test_assert(allocated == 4);
    bmap_test_assert(start == BmapEntry::size() / 4);
    delete bmap;
  }

  bmap = new BmapEntry(false);
  bmap->set_bits(4, BmapEntry::size() - 4);
  bmap_test_assert(bmap->is_allocated(4, BmapEntry::size() - 4));
  bmap_test_assert(!bmap->is_allocated(0, 4));
  bmap->set_bits(0, 4);
  bmap_test_assert(bmap->is_allocated(0, BmapEntry::size()));
  delete bmap;

}

TEST(BitAllocator, test_zone_alloc)
{
  int total_blocks = 1024;
  int64_t blks = 1;
  int64_t last_blk = -1;
  int64_t start_blk = 0;
  int64_t allocated = 0;

  BitMapZone *zone = new BitMapZone(total_blocks, 0);

  // Allocate all blocks and see that it is allocating in order.
  bool lock = zone->lock_excl_try();
  bmap_test_assert(lock);

  for (int i = 0; i < total_blocks; i++) {
    allocated = zone->alloc_blocks(blks, 0, &start_blk);
    bmap_test_assert(last_blk + 1 == start_blk);
    bmap_test_assert(allocated == blks);
    last_blk = start_blk;
  }
  bmap_test_assert(zone->get_used_blocks() == total_blocks);

  for (int i = 0; i < total_blocks; i++) {
    bmap_test_assert(zone->get_used_blocks() == total_blocks - i);
    zone->free_blocks(i, blks);
  }

  blks = 2;
  last_blk = -2;
  for (int i = 0; i < total_blocks/2; i++) {
    allocated = zone->alloc_blocks(blks, 0, &start_blk);
    bmap_test_assert(last_blk + 2 == start_blk);
    last_blk = start_blk;
  }

  // Free different boundaries and allocate those
  blks = 3;
  bmap_test_assert(zone->is_exhausted());
  zone->free_blocks(BmapEntry::size() - blks, blks);
  zone->free_blocks(BmapEntry::size(), blks);

  allocated = zone->alloc_blocks(blks * 2, 0, &start_blk);
  bmap_test_assert(BmapEntry::size() - blks == start_blk);
  bmap_test_assert(allocated == blks * 2);

  blks = 4;
  zone->free_blocks(BmapEntry::size() * 2 - blks, 2 * blks);
  allocated = zone->alloc_blocks(2 * blks, 0, &start_blk);
  bmap_test_assert(BmapEntry::size() * 2 - blks == start_blk);
  bmap_test_assert(allocated == blks * 2);

  blks = BmapEntry::size() * 2;
  zone->free_blocks(BmapEntry::size() * 6 - blks, blks);
  allocated = zone->alloc_blocks(blks, 0, &start_blk);
  bmap_test_assert(BmapEntry::size() * 6 - blks == start_blk);

  // free blocks at distance 1, 2 up to 63 and allocate all of them
  // together using disc alloc.
  blks = BmapEntry::size() * 2;
  int num_bits = 1;
  for (int i = 0; i < zone->size() / BmapEntry::size() -1; i++) {
    zone->free_blocks(i * BmapEntry::size(), num_bits);
    num_bits++;
  }

  num_bits = 1;
  int64_t start_block = 0;
  for (int i = 0; i < zone->size() / BmapEntry::size() -1; i++) {
    allocated = zone->alloc_blocks(num_bits, 0, &start_block);
    bmap_test_assert(num_bits == allocated);
    bmap_test_assert(start_block == i * BmapEntry::size());
    num_bits++;
  }

  start_block = 0;
  num_bits = 1;
  for (int i = 0; i < zone->size() / BmapEntry::size() -1; i++) {
    zone->free_blocks(i * BmapEntry::size(), num_bits);
    num_bits++;
  }

  delete zone;
  // non-conti allocations test
  zone = new BitMapZone(total_blocks, 0);
  lock = zone->lock_excl_try();
  bmap_test_assert(lock);
  for (int i = 0; i < zone->size(); i++) {
    allocated = zone->alloc_blocks(1, 0, &start_block);
    bmap_test_assert(allocated == 1);
  }
  for (int i = 0; i < zone->size(); i += 2) {
    zone->free_blocks(i, 1);
  }

  int64_t blk_size = 1024;
  AllocExtentVector extents = AllocExtentVector
        (zone->size() / 2, AllocExtent(-1, -1));

  ExtentList *block_list = new ExtentList(&extents, blk_size);
  allocated = zone->alloc_blocks_dis(zone->size() / 2, 0, 0, block_list);
  bmap_test_assert(allocated == zone->size() / 2);

  {
    zone = new BitMapZone(total_blocks, 0);
    lock = zone->lock_excl_try();
    bmap_test_assert(lock);
    for (int i = 0; i < zone->size(); i += 4) {
    allocated = zone->alloc_blocks(1, i, &start_block);
    bmap_test_assert(allocated == 1);
    bmap_test_assert(start_block == i);
    }

    for (int i = 0; i < zone->size(); i += 4) {
      zone->free_blocks(i, 1);
    }
  }

  {
    int64_t blk_size = 1024;
    AllocExtentVector extents = AllocExtentVector
      (zone->size() / 2, AllocExtent(-1, -1));

    ExtentList *block_list = new ExtentList(&extents, blk_size);

    zone = new BitMapZone(total_blocks, 0);
    lock = zone->lock_excl_try();
    bmap_test_assert(lock);
    for (int i = 0; i < zone->size(); i += 4) {
      block_list->reset();
      allocated = zone->alloc_blocks_dis(1, i, 0, block_list);
      bmap_test_assert(allocated == 1);
      EXPECT_EQ(extents[0].offset, (uint64_t) i * blk_size);
    }

    for (int i = 0; i < zone->size(); i += 4) {
      zone->free_blocks(i, 1);
    }
  }
}

TEST(BitAllocator, test_bmap_alloc)
{
  const int max_iter = 2;

  for (int round = 0; round < 3; round++) {
    // Test zone of different sizes: 512, 1024, 2048
    int64_t zone_size = 512ull << round;
    ostringstream val;
    val << zone_size;
    g_conf->set_val("bluestore_bitmapallocator_blocks_per_zone", val.str());

    // choose randomized span_size
    int64_t span_size = 512ull << (rand() % 4);
    val.str("");
    val << span_size;
    g_conf->set_val("bluestore_bitmapallocator_span_size", val.str());
    g_ceph_context->_conf->apply_changes(NULL);

    int64_t total_blocks = zone_size * 4;
    int64_t allocated = 0;
    int64_t start_block = 0;

    BitAllocator *alloc = new BitAllocator(total_blocks, zone_size, CONCURRENT);

    for (int64_t iter = 0; iter < max_iter; iter++) {
      for (int64_t i = 0; i < total_blocks; i++) {
        alloc_assert(alloc->reserve_blocks(1));
        allocated = alloc->alloc_blocks_res(1, 0, &start_block);
        bmap_test_assert(allocated == 1);
        bmap_test_assert(start_block == i);
      }

      for (int64_t i = 0; i < total_blocks; i++) {
        alloc->free_blocks(i, 1);
      }
    }

    for (int64_t iter = 0; iter < max_iter; iter++) {
      for (int64_t i = 0; i < total_blocks / zone_size; i++) {
        alloc_assert(alloc->reserve_blocks(zone_size));
        allocated = alloc->alloc_blocks_res(zone_size, 0, &start_block);
        bmap_test_assert(allocated == zone_size);
        bmap_test_assert(start_block == i * zone_size);
      }

      for (int64_t i = 0; i < total_blocks / zone_size; i++) {
        alloc->free_blocks(i * zone_size, zone_size);
      }
    }

    allocated = alloc->alloc_blocks(1, 0, &start_block);
    bmap_test_assert(allocated == 1);

    allocated = alloc->alloc_blocks(zone_size - 1, 0, &start_block);
    bmap_test_assert(allocated == zone_size - 1);
    bmap_test_assert(start_block == 1);

    allocated = alloc->alloc_blocks(1, 0, &start_block);
    bmap_test_assert(allocated == 1);

    allocated = alloc->alloc_blocks(zone_size, 0, &start_block);
    bmap_test_assert(allocated == zone_size);
    bmap_test_assert(start_block == zone_size * 2);

    // Dis contiguous blocks allocations
    delete alloc;
    alloc = new BitAllocator(total_blocks, zone_size, CONCURRENT);

    for (int64_t i = 0; i < alloc->size(); i++) {
      allocated = alloc->alloc_blocks(1, 0, &start_block);
      bmap_test_assert(allocated == 1);
    }
    for (int i = 0; i < alloc->size(); i += 2) {
      alloc->free_blocks(i, 1);
    }

    int64_t blk_size = 1024;
    auto extents = AllocExtentVector
          (alloc->size(), AllocExtent(-1, -1));

    ExtentList *block_list = new ExtentList(&extents, blk_size);

    allocated = alloc->alloc_blocks_dis(alloc->size()/2, 0, block_list);
    ASSERT_EQ(alloc->size()/2, allocated);

    block_list->reset();
    allocated = alloc->alloc_blocks_dis(1, 0, block_list);
    bmap_test_assert(allocated == 0);

    alloc->free_blocks(alloc->size()/2, 1);

    block_list->reset();
    allocated = alloc->alloc_blocks_dis(1, 0, block_list);
    bmap_test_assert(allocated == 1);

    bmap_test_assert((int64_t) extents[0].offset == alloc->size()/2 * blk_size);

    delete block_list;
    delete alloc;

    // unaligned zones
    total_blocks = zone_size * 2 + 11;
    alloc = new BitAllocator(total_blocks, zone_size, CONCURRENT);

    for (int64_t iter = 0; iter < max_iter; iter++) {
      for (int64_t i = 0; i < total_blocks; i++) {
        allocated = alloc->alloc_blocks(1, 0, &start_block);
        bmap_test_assert(allocated == 1);
        bmap_test_assert(start_block == i);
      }

      for (int64_t i = 0; i < total_blocks; i++) {
        alloc->free_blocks(i, 1);
      }
    }
    delete alloc;

    // Make three > 3 levels tree and check allocations and dealloc
    // in a loop
    int64_t alloc_size = 64ull << round;
    total_blocks = BitMapArea::get_level_factor(2) * 4;
    alloc = new BitAllocator(total_blocks, zone_size, CONCURRENT, false);
    for (int64_t iter = 0; iter < max_iter; iter++) {
      for (int64_t i = 0; i < total_blocks / alloc_size; i++) {
        allocated = alloc->alloc_blocks(alloc_size, 0, &start_block);
        bmap_test_assert(allocated == alloc_size);
        bmap_test_assert(start_block == i * alloc_size);
      }

      for (int64_t i = 0; i < total_blocks / alloc_size; i++) {
        alloc->free_blocks(i * alloc_size, alloc_size);
      }
    }

    delete alloc;
    alloc = new BitAllocator(1024, zone_size, CONCURRENT, true);

    alloc->free_blocks(1, 1023);
    allocated = alloc->alloc_blocks(16, 0, &start_block);
    bmap_test_assert(allocated == 16);
    bmap_test_assert(start_block == 1);
    delete alloc;

    total_blocks = BitMapArea::get_level_factor(2) * 4;
    alloc_size = 64ull << round;
    alloc = new BitAllocator(total_blocks, zone_size, CONCURRENT, false);
    for (int64_t iter = 0; iter < max_iter; iter++) {
      for (int64_t i = 0; i < total_blocks / alloc_size; i++) {
        bmap_test_assert(alloc->reserve_blocks(alloc_size));
        allocated = alloc->alloc_blocks_res(alloc_size, 0, &start_block);
        bmap_test_assert(allocated == alloc_size);
        bmap_test_assert(start_block == i * alloc_size);
      }

      for (int64_t i = 0; i < total_blocks / alloc_size; i++) {
        alloc->free_blocks(i * alloc_size, alloc_size);
      }
    }

  }

  // restore to typical value
  g_conf->set_val("bluestore_bitmapallocator_blocks_per_zone", "1024");
  g_conf->set_val("bluestore_bitmapallocator_span_size", "1024");
  g_ceph_context->_conf->apply_changes(NULL);
}

bool alloc_extents_max_block(BitAllocator *alloc,
           int64_t max_alloc,
           int64_t total_alloc)
{
  int64_t blk_size = 1;
  int64_t allocated = 0;
  int64_t verified = 0;
  int64_t count = 0;
  AllocExtentVector extents = AllocExtentVector
        (total_alloc, AllocExtent(-1, -1));

  ExtentList *block_list = new ExtentList(&extents, blk_size, max_alloc);

  allocated = alloc->alloc_blocks_dis(total_alloc, 0, block_list);
  EXPECT_EQ(allocated, total_alloc);

  max_alloc = total_alloc > max_alloc? max_alloc: total_alloc;

  for (auto &p: extents) {
    count++;
    EXPECT_EQ(p.length,  max_alloc);
    verified += p.length;
    if (verified >= total_alloc) {
      break;
    }
  }

  EXPECT_EQ(total_alloc / max_alloc, count);
  return true;
}

TEST(BitAllocator, test_bmap_alloc2)
{
  int64_t total_blocks = 1024 * 4;
  int64_t zone_size = 1024;
  BitAllocator *alloc = new BitAllocator(total_blocks, zone_size, CONCURRENT);

  alloc_extents_max_block(alloc, 1, 16);
  alloc_extents_max_block(alloc, 4, 16);
  alloc_extents_max_block(alloc, 16, 16);
  alloc_extents_max_block(alloc, 32, 16);
}

void
verify_blocks(int64_t num_blocks, int64_t *blocks)
{
  int64_t i = 0;
  int wraps = 0;
  for (i = 0; i < num_blocks - 1; i++) {
    if (blocks[i] > blocks[i + 1]) {
      wraps++;
      bmap_test_assert(wraps <= 1);
    }
  }
}

__thread int my_tid;

void
do_work(BitAllocator *alloc)
{
  int num_iters = 3;
  int64_t alloced = 0;
  int64_t start_block = -1;
  int64_t num_blocks = alloc->size() / NUM_THREADS;
  int total_alloced = 0;
  int64_t *allocated_blocks = (int64_t *) new int64_t [MAX_BLOCKS];

  while (num_iters--) {
    printf("Allocating in tid %d.\n", my_tid);
    alloc_assert(alloc->reserve_blocks(num_blocks));
    for (int i = 0; i < num_blocks; i++) {
      alloced = alloc->alloc_blocks_res(1, 0, &start_block);
      bmap_test_assert(alloced == 1);
      total_alloced++;
      allocated_blocks[i] = start_block;
    }

    printf("Freeing in tid %d %d blocks.\n", my_tid, total_alloced);
    for (int i = 0; i < num_blocks; i++) {
      alloc->free_blocks(allocated_blocks[i], 1);
    }

    total_alloced = 0;
    printf("Done tid %d iter %d.\n", my_tid, num_iters);
  }
}

void
do_work_dis(BitAllocator *alloc)
{
  int num_iters = 10;
  int64_t alloced = 0;
  int64_t num_blocks = alloc->size() / NUM_THREADS;

  AllocExtentVector extents = AllocExtentVector
        (num_blocks, AllocExtent(-1, -1));
  ExtentList *block_list = new ExtentList(&extents, 4096);

  while (num_iters--) {
      alloc_assert(alloc->reserve_blocks(num_blocks));
      alloced = alloc->alloc_blocks_dis_res(num_blocks, 0, block_list);
      alloc_assert(alloced == num_blocks);

      alloc_assert(alloc->is_allocated_dis(block_list, num_blocks));
      alloc->free_blocks_dis(num_blocks, block_list);
      block_list->reset();
  }
}

int tid = 0;
static bool cont = true;

void *
worker(void *args)
{
  my_tid = __sync_fetch_and_add(&tid, 1);
  BitAllocator *alloc = (BitAllocator *) args;
  printf("Starting thread %d", my_tid);
  if (cont) {
    do_work(alloc);
  } else {
    do_work_dis(alloc);
  }

  return NULL;
}

TEST(BitAllocator, test_bmap_alloc_concurrent)
{
  int64_t total_blocks = MAX_BLOCKS;
  int64_t zone_size = 1024;
  pthread_t pthreads[NUM_THREADS] = {0};

  bmap_test_assert(total_blocks <= MAX_BLOCKS);

  BitAllocator *alloc = new BitAllocator(total_blocks, zone_size, CONCURRENT);

  for (int k = 0; k < 2; k++) {
    cont = k;
    printf("Spawning %d threads for parallel test. Mode Cont = %d.....\n", NUM_THREADS, cont);
    for (int j = 0; j < NUM_THREADS; j++) {
      if (pthread_create(&pthreads[j], NULL, worker, alloc)) {
        printf("Unable to create worker thread.\n");
        exit(0);
      }
    }

    for (int j = 0; j < NUM_THREADS; j++) {
      pthread_join(pthreads[j], NULL);
    }
  }

}