forked from vmware/splinterdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc_allocator.c
798 lines (688 loc) · 23.3 KB
/
rc_allocator.c
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// Copyright 2018-2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
/*
* rc_allocator.c --
*
* This file contains the implementation of the ref count allocator.
*/
#include "platform.h"
#include "rc_allocator.h"
#include "io.h"
#include "poison.h"
#define RC_ALLOCATOR_META_PAGE_CSUM_SEED (2718281828)
/*
* Base offset from where the allocator starts. Currently hard coded to 0.
*/
#define RC_ALLOCATOR_BASE_OFFSET (0)
/* A predicate defining whether to trace allocations/ref-count changes
* on a given address.
*
* Examples:
* #define SHOULD_TRACE(addr) (1) // trace all addresses
* #define SHOULD_TRACE(addr) ((addr) / (4096 * 32) == 339ULL) // trace extent
* 339
*
*/
#define SHOULD_TRACE(addr) (0) // Do not trace anything
/*
*------------------------------------------------------------------------------
*
* function declarations
*
*------------------------------------------------------------------------------
*/
// allocator.h functions
platform_status
rc_allocator_alloc(rc_allocator *al, uint64 *addr, page_type type);
platform_status
rc_allocator_alloc_virtual(allocator *a, uint64 *addr, page_type type)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_alloc(al, addr, type);
}
uint8
rc_allocator_inc_ref(rc_allocator *al, uint64 addr);
uint8
rc_allocator_inc_ref_virtual(allocator *a, uint64 addr)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_inc_ref(al, addr);
}
uint8
rc_allocator_dec_ref(rc_allocator *al, uint64 addr, page_type type);
uint8
rc_allocator_dec_ref_virtual(allocator *a, uint64 addr, page_type type)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_dec_ref(al, addr, type);
}
uint8
rc_allocator_get_ref(rc_allocator *al, uint64 addr);
uint8
rc_allocator_get_ref_virtual(allocator *a, uint64 addr)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_get_ref(al, addr);
}
platform_status
rc_allocator_get_super_addr(rc_allocator * al,
allocator_root_id spl_id,
uint64 * addr);
platform_status
rc_allocator_get_super_addr_virtual(allocator * a,
allocator_root_id spl_id,
uint64 * addr)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_get_super_addr(al, spl_id, addr);
}
platform_status
rc_allocator_alloc_super_addr(rc_allocator * al,
allocator_root_id spl_id,
uint64 * addr);
platform_status
rc_allocator_alloc_super_addr_virtual(allocator * a,
allocator_root_id spl_id,
uint64 * addr)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_alloc_super_addr(al, spl_id, addr);
}
void
rc_allocator_remove_super_addr(rc_allocator *al, allocator_root_id spl_id);
void
rc_allocator_remove_super_addr_virtual(allocator *a, allocator_root_id spl_id)
{
rc_allocator *al = (rc_allocator *)a;
rc_allocator_remove_super_addr(al, spl_id);
}
uint64
rc_allocator_in_use(rc_allocator *al);
uint64
rc_allocator_in_use_virtual(allocator *a)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_in_use(al);
}
uint64
rc_allocator_get_capacity(rc_allocator *al);
uint64
rc_allocator_get_capacity_virtual(allocator *a)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_get_capacity(al);
}
uint64
rc_allocator_extent_size(rc_allocator *al);
uint64
rc_allocator_extent_size_virtual(allocator *a)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_extent_size(al);
}
uint64
rc_allocator_page_size(rc_allocator *al);
uint64
rc_allocator_page_size_virtual(allocator *a)
{
rc_allocator *al = (rc_allocator *)a;
return rc_allocator_page_size(al);
}
void
rc_allocator_assert_noleaks(rc_allocator *al);
void
rc_allocator_assert_noleaks_virtual(allocator *a)
{
rc_allocator *al = (rc_allocator *)a;
rc_allocator_assert_noleaks(al);
}
void
rc_allocator_print_stats(rc_allocator *al);
void
rc_allocator_print_stats_virtual(allocator *a)
{
rc_allocator *al = (rc_allocator *)a;
rc_allocator_print_stats(al);
}
void
rc_allocator_debug_print(rc_allocator *al);
void
rc_allocator_debug_print_virtual(allocator *a)
{
rc_allocator *al = (rc_allocator *)a;
rc_allocator_debug_print(al);
}
const static allocator_ops rc_allocator_ops = {
.alloc = rc_allocator_alloc_virtual,
.inc_ref = rc_allocator_inc_ref_virtual,
.dec_ref = rc_allocator_dec_ref_virtual,
.get_ref = rc_allocator_get_ref_virtual,
.get_super_addr = rc_allocator_get_super_addr_virtual,
.alloc_super_addr = rc_allocator_alloc_super_addr_virtual,
.remove_super_addr = rc_allocator_remove_super_addr_virtual,
.get_capacity = rc_allocator_get_capacity_virtual,
.get_extent_size = rc_allocator_extent_size_virtual,
.get_page_size = rc_allocator_page_size_virtual,
.assert_noleaks = rc_allocator_assert_noleaks_virtual,
.print_stats = rc_allocator_print_stats_virtual,
.debug_print = rc_allocator_debug_print_virtual,
};
static platform_status
rc_allocator_init_meta_page(rc_allocator *al)
{
_Static_assert(offsetof(rc_allocator_meta_page, splinters) == 0,
"splinter array should be first field in meta_page struct");
/*
* To make it easier to do aligned i/o's we allocate the meta page to
* always be page size. In the future we can use the remaining space
* for some other reserved information we may want to persist as part
* of the meta page.
*/
platform_assert(sizeof(rc_allocator_meta_page) <= al->cfg->page_size);
/*
* Ensure that the meta page and all the super blocks will fit in one
* extent.
*/
platform_assert((1 + RC_ALLOCATOR_MAX_ALLOCATOR_ROOT_IDS) * al->cfg->page_size
<= al->cfg->extent_size);
al->meta_page =
platform_aligned_malloc(al->heap_id, al->cfg->page_size,
al->cfg->page_size);
if (al->meta_page == NULL) {
return STATUS_NO_MEMORY;
}
memset(al->meta_page, 0, al->cfg->page_size);
memset(al->meta_page->splinters, INVALID_ALLOCATOR_ROOT_ID,
sizeof(al->meta_page->splinters));
return STATUS_OK;
}
/*
*-----------------------------------------------------------------------------
*
* rc_allocator_config_init --
*
* Initialize rc_allocator config values
*
*-----------------------------------------------------------------------------
*/
void
rc_allocator_config_init(rc_allocator_config *allocator_cfg,
uint64 page_size,
uint64 extent_size,
uint64 capacity)
{
ZERO_CONTENTS(allocator_cfg);
allocator_cfg->page_size = page_size;
allocator_cfg->extent_size = extent_size;
allocator_cfg->capacity = capacity;
allocator_cfg->page_capacity = capacity / page_size;
allocator_cfg->extent_capacity = capacity / extent_size;
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_[de]init --
*
* [de]initialize an allocator
*
*----------------------------------------------------------------------
*/
platform_status
rc_allocator_init(rc_allocator *al,
rc_allocator_config *cfg,
io_handle *io,
platform_heap_handle hh,
platform_heap_id hid,
platform_module_id mid)
{
uint64 rc_extent_count;
uint64 addr;
platform_status rc;
platform_assert(al != NULL);
ZERO_CONTENTS(al);
al->super.ops = &rc_allocator_ops;
al->cfg = cfg;
al->io = io;
al->heap_handle = hh;
al->heap_id = hid;
platform_assert(cfg->page_size % 4096 == 0);
platform_assert(cfg->capacity == cfg->extent_size * cfg->extent_capacity);
platform_assert(cfg->capacity == cfg->page_size * cfg->page_capacity);
rc = platform_mutex_init(&al->lock, mid, al->heap_id);
if (!SUCCESS(rc)) {
platform_error_log("Failed to init mutex for the allocator\n");
return rc;
}
rc = rc_allocator_init_meta_page(al);
if (!SUCCESS(rc)) {
platform_error_log("Failed to init meta page for rc allocator\n");
platform_mutex_destroy(&al->lock);
return rc;
}
// To ensure alignment always allocate in multiples of page size.
uint32 buffer_size = cfg->extent_capacity * sizeof(uint8);
buffer_size = ROUNDUP(buffer_size, cfg->page_size);
al->bh = platform_buffer_create(buffer_size, al->heap_handle, mid);
if (al->bh == NULL) {
platform_error_log("Failed to create buffer for ref counts\n");
platform_mutex_destroy(&al->lock);
platform_free(al->heap_id, al->meta_page);
return STATUS_NO_MEMORY;
}
al->ref_count = platform_buffer_getaddr(al->bh);
memset(al->ref_count, 0, buffer_size);
// allocate the super block
allocator_alloc(&al->super, &addr, PAGE_TYPE_MISC);
// super block extent should always start from address 0.
platform_assert(addr == RC_ALLOCATOR_BASE_OFFSET);
/*
* Allocate room for the ref counts, use same rounded up size used in buffer
* creation.
*/
rc_extent_count = (buffer_size + al->cfg->extent_size - 1)
/ al->cfg->extent_size;
for (uint64 i = 0; i < rc_extent_count; i++) {
allocator_alloc(&al->super, &addr, PAGE_TYPE_MISC);
platform_assert(addr == cfg->extent_size * (i + 1));
}
return STATUS_OK;
}
void rc_allocator_deinit(rc_allocator *al)
{
platform_buffer_destroy(al->bh);
al->ref_count = NULL;
platform_mutex_destroy(&al->lock);
platform_free(al->heap_id, al->meta_page);
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_[dis]mount --
*
* Loads the file system from disk
* Write the file system to disk
*
*----------------------------------------------------------------------
*/
platform_status
rc_allocator_mount(rc_allocator *al,
rc_allocator_config *cfg,
io_handle *io,
platform_heap_handle hh,
platform_heap_id hid,
platform_module_id mid)
{
platform_status status;
platform_assert(al != NULL);
ZERO_CONTENTS(al);
al->super.ops = &rc_allocator_ops;
al->cfg = cfg;
al->io = io;
al->heap_handle = hh;
al->heap_id = hid;
status = platform_mutex_init(&al->lock, mid, al->heap_id);
if (!SUCCESS(status)) {
platform_error_log("Failed to init mutex for the allocator\n");
return status;
}
status = rc_allocator_init_meta_page(al);
if (!SUCCESS(status)) {
platform_error_log("Failed to init meta page for rc allocator\n");
platform_mutex_destroy(&al->lock);
return status;
}
platform_assert(cfg->page_size % 4096 == 0);
platform_assert(cfg->capacity == cfg->extent_size * cfg->extent_capacity);
platform_assert(cfg->capacity == cfg->page_size * cfg->page_capacity);
uint32 buffer_size = cfg->extent_capacity * sizeof(uint8);
buffer_size = ROUNDUP(buffer_size, cfg->page_size);
al->bh = platform_buffer_create(buffer_size, al->heap_handle, mid);
platform_assert(al->bh != NULL);
al->ref_count = platform_buffer_getaddr(al->bh);
// load the meta page from disk.
status = io_read(io, al->meta_page, al->cfg->page_size,
RC_ALLOCATOR_BASE_OFFSET);
platform_assert_status_ok(status);
// validate the checksum of the meta page.
checksum128 currChecksum =
platform_checksum128(al->meta_page, sizeof(al->meta_page->splinters),
RC_ALLOCATOR_META_PAGE_CSUM_SEED);
if (!platform_checksum_is_equal(al->meta_page->checksum, currChecksum)) {
platform_assert(0 == "Corrupt Meta Page upon mount");
}
// load the ref counts from disk.
uint32 io_size = ROUNDUP(al->cfg->extent_capacity, al->cfg->page_size);
status = io_read(io, al->ref_count, io_size, cfg->extent_size);
platform_assert_status_ok(status);
for (uint64 i = 0; i < al->cfg->extent_capacity; i++) {
if (al->ref_count[i] != 0) {
al->stats.curr_allocated++;
}
}
platform_log("Allocated at mount: %lu MiB\n",
B_TO_MiB(al->stats.curr_allocated * cfg->extent_size));
return STATUS_OK;
}
void
rc_allocator_dismount(rc_allocator *al)
{
platform_status status;
platform_log("Allocated at dismount: %lu MiB\n",
B_TO_MiB(al->stats.curr_allocated * al->cfg->extent_size));
// persist the ref counts upon dismount.
uint32 io_size = ROUNDUP(al->cfg->extent_capacity, al->cfg->page_size);
status = io_write(al->io, al->ref_count, io_size, al->cfg->extent_size);
platform_assert_status_ok(status);
rc_allocator_deinit(al);
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_[inc,dec,get]_ref --
*
* Increments/decrements/fetches the ref count of the given address and
* returns the new one. If the ref_count goes to 0, then the extent is
* freed.
*
*----------------------------------------------------------------------
*/
uint8
rc_allocator_inc_ref(rc_allocator *al, uint64 addr)
{
debug_assert(addr % al->cfg->extent_size == 0);
uint64 extent_no = addr / al->cfg->extent_size;
debug_assert(extent_no < al->cfg->extent_capacity);
uint8 ref_count = __sync_add_and_fetch(&al->ref_count[extent_no], 1);
platform_assert(ref_count != 1 && ref_count != 0);
if (SHOULD_TRACE(addr)) {
platform_log("rc_allocator_inc_ref(%lu): %d -> %d\n",
addr,
ref_count,
ref_count + 1);
}
return ref_count;
}
uint8
rc_allocator_dec_ref(rc_allocator *al, uint64 addr, page_type type)
{
debug_assert(addr % al->cfg->extent_size == 0);
uint64 extent_no = addr / al->cfg->extent_size;
debug_assert(extent_no < al->cfg->extent_capacity);
uint8 ref_count = __sync_sub_and_fetch(&al->ref_count[extent_no], 1);
platform_assert(ref_count != UINT8_MAX);
if (ref_count == 0) {
platform_assert(type != PAGE_TYPE_INVALID);
__sync_sub_and_fetch(&al->stats.curr_allocated, 1);
__sync_add_and_fetch(&al->stats.extent_deallocs[type], 1);
}
if (SHOULD_TRACE(addr)) {
platform_log("rc_allocator_dec_ref(%lu): %d -> %d\n",
addr,
ref_count,
ref_count - 1);
}
return ref_count;
}
uint8
rc_allocator_get_ref(rc_allocator *al, uint64 addr)
{
uint64 extent_no;
debug_assert(addr % al->cfg->extent_size == 0);
extent_no = addr / al->cfg->extent_size;
debug_assert(extent_no < al->cfg->extent_capacity);
return al->ref_count[extent_no];
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_get_[capacity,super_addr] --
*
* returns the struct/parameter
*
*----------------------------------------------------------------------
*/
uint64
rc_allocator_get_capacity(rc_allocator *al)
{
return al->cfg->capacity;
}
platform_status
rc_allocator_get_super_addr(rc_allocator *al,
allocator_root_id allocator_root_id,
uint64 *addr)
{
platform_status status = STATUS_NOT_FOUND;
platform_mutex_lock(&al->lock);
for (uint8 idx = 0; idx < RC_ALLOCATOR_MAX_ALLOCATOR_ROOT_IDS; idx++) {
if (al->meta_page->splinters[idx] == allocator_root_id) {
// have already seen this table before, return existing addr.
*addr = (1 + idx) * al->cfg->page_size;
status = STATUS_OK;
break;
}
}
platform_mutex_unlock(&al->lock);
return status;
}
platform_status
rc_allocator_alloc_super_addr(rc_allocator *al,
allocator_root_id allocator_root_id,
uint64 *addr)
{
platform_status status = STATUS_NOT_FOUND;
platform_mutex_lock(&al->lock);
for (uint8 idx = 0; idx < RC_ALLOCATOR_MAX_ALLOCATOR_ROOT_IDS; idx++) {
if (al->meta_page->splinters[idx] == INVALID_ALLOCATOR_ROOT_ID) {
// assign the first available slot and update the on disk metadata.
al->meta_page->splinters[idx] = allocator_root_id;
*addr = (1 + idx) * al->cfg->page_size;
al->meta_page->checksum =
platform_checksum128(al->meta_page,
sizeof(al->meta_page->splinters),
RC_ALLOCATOR_META_PAGE_CSUM_SEED);
platform_status io_status =
io_write(al->io, al->meta_page, al->cfg->page_size,
RC_ALLOCATOR_BASE_OFFSET);
platform_assert_status_ok(io_status);
status = STATUS_OK;
break;
}
}
platform_mutex_unlock(&al->lock);
return status;
}
void
rc_allocator_remove_super_addr(rc_allocator *al,
allocator_root_id allocator_root_id)
{
platform_mutex_lock(&al->lock);
for (uint8 idx = 0; idx < RC_ALLOCATOR_MAX_ALLOCATOR_ROOT_IDS; idx++) {
/*
* clear out the mapping for this splinter table and update on disk
* metadata.
*/
if (al->meta_page->splinters[idx] == allocator_root_id) {
al->meta_page->splinters[idx] = INVALID_ALLOCATOR_ROOT_ID;
al->meta_page->checksum =
platform_checksum128(al->meta_page,
sizeof(al->meta_page->splinters),
RC_ALLOCATOR_META_PAGE_CSUM_SEED);
platform_status status =
io_write(al->io, al->meta_page, al->cfg->page_size,
RC_ALLOCATOR_BASE_OFFSET);
platform_assert_status_ok(status);
platform_mutex_unlock(&al->lock);
return;
}
}
platform_mutex_unlock(&al->lock);
// Couldnt find the splinter id in the meta page.
platform_assert(0 == "Couldnt find existing splinter table in meta page");
}
uint64
rc_allocator_extent_size(rc_allocator *al)
{
return al->cfg->extent_size;
}
uint64
rc_allocator_page_size(rc_allocator *al)
{
return al->cfg->page_size;
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_alloc--
*
* Allocate an extent
*
*----------------------------------------------------------------------
*/
platform_status
rc_allocator_alloc(rc_allocator *al, // IN
uint64 * addr, // OUT
page_type type) // IN
{
uint64 first_hand = al->hand % al->cfg->extent_capacity;
uint64 hand;
bool extent_is_free = FALSE;
do {
hand = __sync_fetch_and_add(&al->hand, 1) % al->cfg->extent_capacity;
if (al->ref_count[hand] == 0)
extent_is_free = __sync_bool_compare_and_swap(&al->ref_count[hand], 0, 2);
} while (!extent_is_free && (hand + 1) % al->cfg->extent_capacity != first_hand);
if (!extent_is_free) {
platform_log("Out of Space: allocated %lu out of %lu addrs.\n",
al->stats.curr_allocated,
al->cfg->extent_capacity);
return STATUS_NO_SPACE;
}
int64 curr_allocated = __sync_add_and_fetch(&al->stats.curr_allocated, 1);
int64 max_allocated = al->stats.max_allocated;
while (curr_allocated > max_allocated) {
__sync_bool_compare_and_swap(
&al->stats.max_allocated, max_allocated, curr_allocated);
max_allocated = al->stats.max_allocated;
}
__sync_add_and_fetch(&al->stats.extent_allocs[type], 1);
*addr = hand * al->cfg->extent_size;
if (SHOULD_TRACE(*addr)) {
platform_log(
"rc_allocator_alloc_extent %12lu (%s)\n", *addr, page_type_str[type]);
}
return STATUS_OK;
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_in_use --
*
* Returns the number of extents currently allocated
*
*----------------------------------------------------------------------
*/
uint64
rc_allocator_in_use(rc_allocator *al)
{
return al->stats.curr_allocated;
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_assert_noleaks --
*
* Asserts that the allocations of each type are completely matched by
* deallocations.
*
*----------------------------------------------------------------------
*/
void
rc_allocator_assert_noleaks(rc_allocator *al)
{
for (page_type type = PAGE_TYPE_TRUNK; type != NUM_PAGE_TYPES; type++) {
if (type == PAGE_TYPE_LOG || type == PAGE_TYPE_MISC) {
continue;
}
if (al->stats.extent_allocs[type] != al->stats.extent_deallocs[type]) {
platform_default_log("assert_noleaks: leak found\n");
platform_default_log("\n");
rc_allocator_print_stats(al);
rc_allocator_debug_print(al);
platform_assert(0);
}
}
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_print_stats --
*
* Prints basic statistics about the allocator state.
*
* Max allocations, and page type stats are since last mount.
*
*----------------------------------------------------------------------
*/
void
rc_allocator_print_stats(rc_allocator *al)
{
int64 divider = GiB / al->cfg->extent_size;
platform_default_log(
"----------------------------------------------------------------\n");
platform_default_log(
"| Allocator Stats |\n");
platform_default_log(
"|--------------------------------------------------------------|\n");
uint64 curr_gib = al->stats.curr_allocated / divider;
platform_default_log(
"| Currently Allocated: %12lu extents (%4luGiB) |\n",
al->stats.curr_allocated,
curr_gib);
uint64 max_gib = al->stats.max_allocated / divider;
platform_default_log(
"| Max Allocated: %12lu extents (%4luGiB) |\n",
al->stats.max_allocated,
max_gib);
platform_default_log(
"|--------------------------------------------------------------|\n");
platform_default_log(
"| Page Type | Allocations | Deallocations | Footprint (bytes) |\n");
platform_default_log(
"|--------------------------------------------------------------|\n");
for (page_type type = PAGE_TYPE_TRUNK; type != NUM_PAGE_TYPES; type++) {
const char *str = page_type_str[type];
int64 allocs = al->stats.extent_allocs[type];
int64 deallocs = al->stats.extent_deallocs[type];
int64 footprint = allocs - deallocs;
int64 footprint_gib = footprint / divider;
platform_default_log("| %9s | %11ld | %13ld | %8ld (%4ldGiB) |\n",
str,
allocs,
deallocs,
footprint,
footprint_gib);
}
platform_default_log(
"----------------------------------------------------------------\n");
}
/*
*----------------------------------------------------------------------
*
* rc_allocator_debug_print --
*
* Prints the base addrs of all allocated extents.
*
*----------------------------------------------------------------------
*/
void
rc_allocator_debug_print(rc_allocator *al)
{
uint64 i;
uint8 ref;
platform_default_log("Allocated: %lu\n", al->stats.curr_allocated);
for (i = 0; i < al->cfg->extent_capacity; i++) {
ref = al->ref_count[i];
if (ref != 0)
platform_default_log("%lu -- %u\n", i * al->cfg->extent_size, ref);
}
}