-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstreamflow.c
1705 lines (1423 loc) · 51.2 KB
/
streamflow.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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Streamflow memory allocator.
*
* Copyright (C) 2007 Scott Schneider, Christos Antonopoulos
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "streamflow.h"
#ifdef PROFILE
unsigned long long malloc_cycles;
unsigned long long malloc_compute_cycles;
unsigned long long garbage_cycles;
unsigned long long get_pgblk_cycles;
unsigned long long free_cycles;
unsigned long long free_compute_cycles;
unsigned long long extract_cycles;
unsigned long long local_free_cycles;
unsigned long long adopt_pgblk_cycles;
unsigned long long remote_free_cycles;
unsigned long long supermap_cycles;
unsigned long long superunmap_cycles;
unsigned long long malloc_calls;
unsigned long long free_calls;
#endif
/* Used for gathering memory stats. */
static volatile unsigned int num_total_small;
static volatile unsigned int num_total_medium;
static volatile unsigned int num_total_large;
static volatile unsigned int num_frees;
static volatile unsigned int num_remote_frees;
static volatile unsigned int num_adoptions;
static volatile unsigned long long size_total_small;
static volatile unsigned long long size_total_medium;
static volatile unsigned long long size_total_large;
#ifdef MEMORY
static int init_flag;
static lock_t init_lock;
#endif
__thread counting_queue_t remote_cache;
__thread unsigned int remote_cache_total;
/* Global thread_id counter. */
unsigned int global_id_counter = 0;
/* Thread ID that is our index in the global object table. */
__thread unsigned int thread_id = 0;
#ifdef NUMA
/* Maps a cpu to the node its on. */
int cpu_to_node[NUM_NUMA_NODES];
#endif
static radix_interior_t* radix_root;
/* All superpage related structures. The lock protects
* the other two structures. */
#ifdef NUMA
static __thread lock_t super_lock;
static __thread double_list_t superpage_list;
static __thread quickieblock_t sph_pageblocks;
#else
static lock_t super_lock;
static double_list_t superpage_list;
static quickieblock_t sph_pageblocks;
#endif
#ifdef BIBOP
/* Each page in virtual memory has an entry in the page vector that records two
* pieces of information: is this a "large" or small object, and the offset to
* the start of the pageblock/object from this page. */
static page_record_t bibop[PAGES_IN_ADDR_SPACE];
#endif
/* The following four arrays are for pageblocks in their various states of use:
*
* local_heap:
* thread local active pageblocks, indexed by object class; the address
* doubles as the thread id
* local_inactive_pageblocks:
* thread local cached inactive pageblocks, indexed by pageblock size
* global_partial_pageblocks:
* global orphaned pageblocks whose owning thread has terminated,
* indexed by object class
* global_free_pageblocks:
* global cached completely free pageblocks, indexed by pageblock size
*/
static __thread heap_t local_heap[OBJECT_SIZE_CLASSES];
static __thread counting_queue_t local_inactive_pageblocks[PAGEBLOCK_SIZE_CLASSES];
static counting_lf_lifo_queue_t global_partial_pageblocks[OBJECT_SIZE_CLASSES];
static counting_lf_lifo_queue_t global_free_pageblocks[PAGEBLOCK_SIZE_CLASSES];
static inline unsigned int quick_log2(unsigned int x);
static inline int max(int a, int b);
static inline int compute_size_class(size_t size);
static inline int malloc_compute_size_class(size_t size);
static inline int free_compute_size_class(size_t size);
static inline int reverse_size_class(size_t size_class);
/* Radix tree operations. */
static void radix_register(void* start, int num_pages, void* ptr, size_t size, short object_type);
static inline void radix_extract(void* object, void** ptr, size_t* size, short* is_large);
static inline radix_interior_t* radix_interior_alloc(void);
static inline radix_leaf_t* radix_leaf_alloc(void);
static inline void radix_interior_free(radix_interior_t* node);
static inline void radix_leaf_free(radix_leaf_t* node);
/* Operations on quickie pageblocks. */
static void* quickie_alloc(quickieblock_t* quickie, size_t object_size);
static inline void quickie_free(quickieblock_t* quickie, void* object);
/* Buddy operations on superpages. */
static inline int find_index(superpage_t* super, page_chunk_t* chunk, int order);
static inline void* find_buddy(superpage_t* super, page_chunk_t* chunk, int order);
static inline int find_bit_index(superpage_t* super, page_chunk_t* chunk, int order);
static void* buddy_alloc(superpage_t* super, size_t size);
static void buddy_free(superpage_t* super, void* start, size_t length);
/* All other superpage operations. */
static void get_free_superpage(superpage_t** sp, size_t size);
static void* supermap(size_t size);
static void superunmap(void* start, size_t length);
/* All virtual page operations. */
static inline void register_pages(void* start, int num_pages, void* ptr, size_t size, short object_type);
static inline void* page_alloc(size_t size);
static inline void page_free(void* start, size_t length);
static void* medium_or_large_alloc(size_t size);
/* All operations on the global free list. */
static void insert_global_free_pageblocks(pageblock_t* pageblock);
static void insert_global_partial_pageblocks(pageblock_t* pageblock, int class_index);
static pageblock_t* remove_global_pageblocks(int class_index, int pageblock_size);
/* All double list operations. */
static void double_list_insert_front(void* new_node, double_list_t* list);
static void double_list_rotate_back(double_list_t* list);
static void double_list_remove(void* node, double_list_t* list);
/* Helper functions for malloc */
static inline void headerize_object(void** object, void* ptr, size_t size, short object_type);
static inline int compute_pageblock_size(int index);
static pageblock_t* get_free_pageblock(heap_t* heap, int index);
/* Helper functions for free. */
static inline void local_free(void* object, pageblock_t* pageblock, heap_t* my_heap);
static void remote_free(void* object, pageblock_t* pageblock, heap_t* my_heap);
static void adopt_pageblock(void* object, pageblock_t* pageblock, heap_t* my_heap);
static const int base[] = { 0, 16, 24, 28, 30, 31, 31, 32, 32, 32,
32, 33, 33, 33, 33, 34, 34, 34, 34, 35,
35, 35, 35, 36, 36, 36, 36, 37, 37, 37,
37, 38, 38, 38, 38, 39, 39, 39, 39, 40,
40, 40, 40, 41, 41, 41, 41, 42, 42, 42,
42, 43, 43, 43, 43, 44, 44, 44, 44, 45,
45, 45, 45, 46, 46, 46, 46, 47, 47, 47,
47, 48, 48, 48, 48, 49, 49, 49, 49, 50,
50, 50, 50, 51, 51, 51, 51, 52, 52, 52,
52, 53, 53, 53, 53, 54, 54, 54, 54, 55,
55, 55, 55, 56, 56, 56, 56, 57, 57, 57,
57, 58, 58, 58, 58, 59, 59, 59, 59, 60,
60, 60, 60, 61, 61, 61, 61, 62, 62, 62,
62, 63, 63, 63, 63, 64, 64, 64, 64, 65,
65, 65, 65, 66, 66, 66, 66, 67, 67, 67,
67, 68, 68, 68, 68, 69, 69, 69, 69, 70,
70, 70, 70, 71, 71, 71, 71, 72, 72, 72,
72, 73, 73, 73, 73, 74, 74, 74, 74, 75,
75, 75, 75, 76, 76, 76, 76, 77, 77, 77,
77, 78, 78, 78, 78, 79, 79, 79, 79, 80,
80, 80, 80, 81, 81, 81, 81, 82, 82, 82,
82, 83, 83, 83, 83, 84, 84, 84, 84, 85,
85, 85, 85, 86, 86, 86, 86, 87, 87, 87,
87, 88, 88, 88, 88, 89, 89, 89, 89, 90,
90, 90, 90, 91, 91, 91, 91, 92, 92, 92,
92, 93, 93, 93, 93, 94, 94, 94, 94 };
static const int factor[] = { 4, 8, 16, 32, 64, 128, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
256, 256, 256, 256, 256, 256, 256, 256, 256 };
static const int reverse[] = { 4, 8, 12, 16, 20, 24, 28, 32, 36, 40,
44, 48, 52, 56, 60, 64, 72, 80, 88, 96,
104, 112, 120, 128, 144, 160, 176, 192, 224, 256,
320, 448, 704, 960, 1216, 1472, 1728, 1984, 2240, 2496,
2752, 3008, 3264, 3520, 3776, 4032, 4288, 4544, 4800, 5056,
5312, 5568, 5824, 6080, 6336, 6592, 6848, 7104, 7360, 7616,
7872, 8128, 8384, 8640, 8896, 9152, 9408, 9664, 9920, 10176,
10432, 10688, 10944, 11200, 11456, 11712, 11968, 12224, 12480, 12736,
12992, 13248, 13504, 13760, 14016, 14272, 14528, 14784, 15040, 15296,
15552, 15808, 16064, 16320, 16576 };
static inline unsigned long get_cycles(void);
#if __INTEL_COMPILER
#include <ia64intrin.h>
inline static unsigned long get_cycles(void)
{
unsigned long long t;
t = __getReg(_IA64_REG_AR_ITC);
return t;
}
#else
inline static unsigned long get_cycles(void)
{
unsigned long tmp;
__asm__ __volatile__("mov %0=ar.itc":"=r"(tmp)::"memory");
return tmp;
}
#endif
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
/* If we're collecting memory stats, does an atomic add. If not, does nothing. */
static inline void memory_add32(volatile unsigned int* address, int value)
{
#ifdef MEMORY
atmc_add32(address, value);
#endif
}
static inline void memory_add64(volatile unsigned long long* address, unsigned long long value)
{
#ifdef MEMORY
atmc_add64(address, value);
#endif
}
/* So, this is embarassingly naive and brittle, but it also noticabley
* outperforms log2(). Returns the base 2 logarithm of x, assuming x is a
* power-of-2. */
static inline unsigned int quick_log2(unsigned int x)
{
switch (x) {
case 1: return 0;
case 2: return 1;
case 4: return 2;
case 8: return 3;
case 16: return 4;
case 32: return 5;
case 64: return 6;
case 128: return 7;
case 256: return 8;
case 512: return 9;
case 1024: return 10;
case 2048: return 11;
case 4096: return 12;
case 8192: return 12;
case 16384: return 13;
}
fprintf(stderr, "quick_log2() unhandled number: %u\n", x);
assert(0);
return -1;
}
static inline int max(int a, int b)
{
return (a > b) ? a: b;
}
static inline int compute_size_class(size_t size)
{
if (size < OBJECT_GRANULARITY) {
size = OBJECT_GRANULARITY;
}
unsigned int bin = size / (CACHE_LINE_SIZE >> 1);
unsigned int position = (size - 1) % (CACHE_LINE_SIZE >> 1);
if (size % (CACHE_LINE_SIZE >> 1) == 0) {
bin = (size - 1) / (CACHE_LINE_SIZE >> 1);
position = (size - 2) % (CACHE_LINE_SIZE >> 1);
}
return base[bin] + (position / factor[bin]);
}
static inline int malloc_compute_size_class(size_t size)
{
#ifdef PROFILE
unsigned long long local_malloc_compute_cycles = get_cycles();
#endif
if (size < OBJECT_GRANULARITY) {
size = OBJECT_GRANULARITY;
}
unsigned int bin = size / (CACHE_LINE_SIZE >> 1);
unsigned int position = (size - 1) % (CACHE_LINE_SIZE >> 1);
if (size % (CACHE_LINE_SIZE >> 1) == 0) {
bin = (size - 1) / (CACHE_LINE_SIZE >> 1);
position = (size - 2) % (CACHE_LINE_SIZE >> 1);
}
int ret = base[bin] + (position / factor[bin]);
#ifdef PROFILE
malloc_compute_cycles += get_cycles() - local_malloc_compute_cycles;
#endif
return ret;
}
static inline int free_compute_size_class(size_t size)
{
#ifdef PROFILE
unsigned long long local_free_compute_cycles = get_cycles();
#endif
if (size < OBJECT_GRANULARITY) {
size = OBJECT_GRANULARITY;
}
unsigned int bin = size / (CACHE_LINE_SIZE >> 1);
unsigned int position = (size - 1) % (CACHE_LINE_SIZE >> 1);
if (size % (CACHE_LINE_SIZE >> 1) == 0) {
bin = (size - 1) / (CACHE_LINE_SIZE >> 1);
position = (size - 2) % (CACHE_LINE_SIZE >> 1);
}
int ret = base[bin] + (position / factor[bin]);
#ifdef PROFILE
free_compute_cycles += get_cycles() - local_free_compute_cycles;
#endif
return ret;
}
static inline int reverse_size_class(size_t size_class)
{
return reverse[size_class];
}
static inline radix_interior_t* radix_interior_alloc(void)
{
void* node = page_alloc(sizeof(radix_interior_t));
return (radix_interior_t*)node;
}
static inline radix_leaf_t* radix_leaf_alloc(void)
{
void* node = page_alloc(sizeof(radix_leaf_t));
return (radix_leaf_t*)node;
}
static inline void radix_interior_free(radix_interior_t* node)
{
page_free(node, sizeof(radix_interior_t));
}
static inline void radix_leaf_free(radix_leaf_t* node)
{
page_free(node, sizeof(radix_leaf_t));
}
static void radix_register(void* start, int num_pages, void* ptr, size_t size, short object_type)
{
/* Ensure in a lock-free manner that we have a root node. */
if (radix_root == NULL) {
radix_interior_t* temp_root = radix_interior_alloc();
if (!compare_and_swap_ptr(&radix_root, NULL, temp_root)) {
radix_interior_free(temp_root);
}
}
int i;
unsigned int log_size = 0;
if (object_type == OBJECT_MEDIUM) {
log_size = quick_log2(size / PAGE_SIZE);
}
unsigned long page = (unsigned long)start >> PAGE_BITS;
for (i = 0; i < num_pages; ++i) {
unsigned long level1 = page >> (RADIX_INTERIOR_BITS + RADIX_LEAF_BITS);
unsigned long level2 = (page >> RADIX_LEAF_BITS) & (RADIX_INTERIOR_SIZE - 1);
unsigned long level3 = page & (RADIX_LEAF_SIZE - 1);
page_record_t record;
if (radix_root->prefixes[level1] == NULL) {
radix_interior_t* temp_interior = radix_interior_alloc();
if (!compare_and_swap_ptr(&radix_root->prefixes[level1], NULL, temp_interior)) {
radix_interior_free(temp_interior);
}
}
if (radix_root->prefixes[level1]->prefixes[level2] == NULL) {
radix_leaf_t* temp_leaf = radix_leaf_alloc();
if (!compare_and_swap_ptr(&radix_root->prefixes[level1]->prefixes[level2], NULL, temp_leaf)) {
radix_leaf_free(temp_leaf);
}
}
/* Accessing the third level does not need any synchronization. Since there is a
* one-to-one correspondence between pages in the system and third level values,
* and we assume the OS will not return the same page multiple times, we know
* that we are the only one accessing this location. */
record.object_type = object_type;
switch (object_type) {
case OBJECT_SMALL: record.pageblock = (unsigned long)ptr >> PAGE_BITS;
break;
case OBJECT_MEDIUM: record.sph = (unsigned long)ptr >> SUPERPAGE_BITS;
record.log_size = log_size;
break;
case OBJECT_LARGE: record.size = (unsigned long)size;
break;
}
((radix_leaf_t*)radix_root->prefixes[level1]->prefixes[level2])->values[level3] = record;
page += 1;
}
}
/* We assume that radix_register has already been called for this object's page. This
* allows us to assume that the nodes are already allocated. */
static inline void radix_extract(void* object, void** ptr, size_t* size, short* object_type)
{
unsigned long page = (unsigned long)object >> PAGE_BITS;
unsigned long level1 = page >> (RADIX_INTERIOR_BITS + RADIX_LEAF_BITS);
unsigned long level2 = (page >> RADIX_LEAF_BITS) & (RADIX_INTERIOR_SIZE - 1);
unsigned long level3 = page & (RADIX_LEAF_SIZE - 1);
page_record_t record;
record = ((radix_leaf_t*)radix_root->prefixes[level1]->prefixes[level2])->values[level3];
*object_type = record.object_type;
switch (*object_type) {
case OBJECT_SMALL: *ptr = (void*)(record.pageblock << PAGE_BITS);
break;
case OBJECT_MEDIUM: *ptr = (void*)(record.sph << SUPERPAGE_BITS);
*size = (size_t)(1 << record.log_size) * PAGE_SIZE;
break;
case OBJECT_LARGE: *size = (size_t)record.size;
break;
}
}
static inline int find_index(superpage_t* super, page_chunk_t* chunk, int order)
{
return ((unsigned long)chunk - (unsigned long)super->page_pool) / (PAGE_SIZE * (1 << order));
}
static inline void* find_buddy(superpage_t* super, page_chunk_t* chunk, int order)
{
void* buddy;
int i = find_index(super, chunk, order);
/* If i is even, buddy is on the right; if odd, buddy
* is on the left. */
if (i % 2 == 0) {
buddy = (superpage_t*)((unsigned long)chunk + ((1 << order) * PAGE_SIZE));
}
else {
buddy = (superpage_t*)((unsigned long)chunk - ((1 << order) * PAGE_SIZE));
}
return buddy;
}
/* When we index the bitmap, each buddy in a pair needs to map to the same
* location. find_bit_index() takes care of this. */
static inline int find_bit_index(superpage_t* super, page_chunk_t* chunk, int order)
{
int i = find_index(super, chunk, order);
/* We'll decide that the even buddy (the one on the right) has the
* correct location, so we need to adjust the odd buddy. */
if (i % 2 != 0) {
--i;
}
return i / 2;
}
/* Allocates size pages from the buddy allocation scheme.
* size is guarenteed to be a multiple of PAGE_SIZE. */
static void* buddy_alloc(superpage_t* super, size_t size)
{
page_chunk_t* chunk = NULL;
unsigned int order = quick_log2(size / PAGE_SIZE);
unsigned int curr_order;
/* Starting at the closest fit, try to find a page chunk to satisfy
* the request. */
for (curr_order = order; curr_order < BUDDY_ORDER_MAX; ++curr_order) {
if (super->buddy[curr_order].free_list.head != NULL) {
chunk = super->buddy[curr_order].free_list.head;
double_list_remove(chunk, &super->buddy[curr_order].free_list);
__change_bit(find_bit_index(super, chunk, curr_order), (unsigned long *)super->buddy[curr_order].bitmap);
break;
}
}
/* If our page chunk is from a higher order, we need to split it
* up. */
size = 1 << curr_order;
page_chunk_t* buddy;
while (curr_order > order) {
--curr_order;
size >>= 1;
/* We don't need to call find_buddy() because we know that chunk is
* on the left. */
buddy = (page_chunk_t*)((unsigned long)chunk + (size * PAGE_SIZE));
double_list_insert_front(chunk, &super->buddy[curr_order].free_list);
__change_bit(find_bit_index(super, chunk, curr_order), (unsigned long *)super->buddy[curr_order].bitmap);
chunk = buddy;
}
/* Figure out what the highest free order is. */
if (super->buddy[super->largest_free_order].free_list.head == NULL) {
int sorder;
for (sorder = super->largest_free_order - 1; sorder >= 0; --sorder) {
if (super->buddy[sorder].free_list.head != NULL) {
super->largest_free_order = sorder;
break;
}
}
if (sorder < 0) {
super->largest_free_order = BUDDY_ORDER_MAX + 1;
}
}
return (void*)chunk;
}
/* Frees pages back to the buddy scheme. */
static void buddy_free(superpage_t* super, void* start, size_t length)
{
page_chunk_t* chunk = (page_chunk_t*)start;
page_chunk_t* buddy;
unsigned int order = quick_log2(length / PAGE_SIZE);
unsigned int curr_order;
length = 1 << order;
for (curr_order = order; curr_order < BUDDY_ORDER_MAX - 1; ++curr_order) {
length <<= 1;
/* If the buddy is still allocated, then no merging can take place. */
if (!__test_and_change_bit(find_bit_index(super, chunk, curr_order), (unsigned long *)super->buddy[curr_order].bitmap)) {
break;
}
buddy = find_buddy(super, chunk, curr_order);
double_list_remove(buddy, &super->buddy[curr_order].free_list);
/* If I am the odd buddy, then I need to change where I am
* for the next pass. */
if (find_index(super, chunk, curr_order) % 2 != 0) {
chunk = buddy;
}
}
/* If there are still used page chunks, add it to the appropriate
* free list. Otherwise, we merged page chunks all the way back up
* to an entire superpage, which means we can return it to the OS. */
if (curr_order < BUDDY_ORDER_MAX - 1) {
double_list_insert_front(chunk, &super->buddy[curr_order].free_list);
if (curr_order > super->largest_free_order || super->largest_free_order > BUDDY_ORDER_MAX) {
super->largest_free_order = curr_order;
}
}
else {
page_free(chunk, SUPERPAGE_SIZE);
double_list_remove(super, super->list);
quickie_free(super->quickie, super);
}
}
static void* quickie_alloc(quickieblock_t* quickie, size_t object_size)
{
void* object;
/* We need to allocate space for a new pageblock in two cases: the first time
* this function is called (in which case unallocated will not point to
* anything), and when there is no more space in the last pageblock we allocated. */
if (quickie->unallocated == NULL || quickie->num_free_objects == 0) {
quickie->unallocated = page_alloc(PAGE_SIZE);
quickie->num_free_objects = PAGE_SIZE / object_size;
}
if (quickie->freed != NULL) {
object = quickie->freed;
quickie->freed = *((void**)(quickie->freed));
}
else {
object = (superpage_t*)quickie->unallocated;
quickie->unallocated += object_size;
}
--(quickie->num_free_objects);
return object;
}
static inline void quickie_free(quickieblock_t* quickie, void* object)
{
*((void**)object) = quickie->freed;
quickie->freed = (void*)object;
}
static void get_free_superpage(superpage_t** super, size_t size)
{
double_list_elem_t* curr;
double_list_elem_t* first;
unsigned int order;
/* Find a superpage with enough space for this allocation. */
*super = NULL;
curr = superpage_list.head;
if (curr != NULL) {
first = curr;
do {
/* First check to see if the superpage has any free pages; a value larger than
* BUDDY_ORDER_MAX in the largest free order indicates this. Then check to see if
* there's enough room for an allocation of size. */
if (((superpage_t*)curr)->largest_free_order < BUDDY_ORDER_MAX &&
(1 << ((superpage_t*)curr)->largest_free_order) >= (size / PAGE_SIZE))
{
*super = (superpage_t*)curr;
break;
}
curr = curr->next;
/*
double_list_rotate_back(&superpage_list);
*/
} while (curr != NULL && curr != first);
}
/* If we couldn't find an existing superpage, get a new one from OS. */
if (*super == NULL) {
*super = (superpage_t*)quickie_alloc(&sph_pageblocks, sizeof(superpage_t));
(*super)->page_pool = page_alloc(SUPERPAGE_SIZE);
/* Initialize bitmaps for buddy allocation */
int byte = 0;
(*super)->buddy[0].bitmap = &(*super)->bitmaps[0];
for (order = 0; order < BUDDY_ORDER_MAX - 1; ++order) {
byte += max(sizeof(unsigned long), (int)ceil(((double)PAGES_PER_SUPERPAGE / ((1 << order) * 8 * 2))));
(*super)->buddy[order + 1].bitmap = &(*super)->bitmaps[byte];
}
memset((*super)->bitmaps, 0, BUDDY_BITMAP_SIZE);
/* The thread local super lock governs all superpages owned by
* this thread. It doubles as a thread ID. */
(*super)->lock = &super_lock;
(*super)->list = &superpage_list;
(*super)->quickie = &sph_pageblocks;
/* Stick the entire superpage into the buddy allocation scheme. */
double_list_insert_front((*super)->page_pool, &((*super)->buddy[BUDDY_ORDER_MAX - 1].free_list));
(*super)->largest_free_order = BUDDY_ORDER_MAX - 1;
double_list_insert_front(*super, &superpage_list);
}
}
static void* supermap(size_t size)
{
superpage_t* super;
void* pages;
#ifdef PROFILE
unsigned long long local_supermap_cycles = get_cycles();
#endif
spin_lock(&super_lock);
get_free_superpage(&super, size);
/* Allocate pages from the superpage. */
pages = buddy_alloc(super, size);
/* UGLY: Write the superpage header pointer into the pageblock header. We
* shouldn't "know" about pageblock at this point, but this is the easiest
* way for superunmap() to know where the superpage header is for the
* freed superpage. */
((pageblock_t*)pages)->sph = super;
spin_unlock(&super_lock);
#ifdef PROFILE
supermap_cycles += get_cycles() - local_supermap_cycles;
#endif
return pages;
}
static void superunmap(void* start, size_t length)
{
superpage_t* super = ((pageblock_t*)start)->sph;
lock_t* lock;
#ifdef PROFILE
unsigned long long local_superunmap_cycles = get_cycles();
#endif
#ifdef NUMA
lock = super->lock;
#else
lock = &super_lock;
#endif
spin_lock(lock);
buddy_free(super, start, length);
spin_unlock(lock);
#ifdef PROFILE
local_superunmap_cycles += get_cycles() - local_superunmap_cycles;
#endif
}
/* If not using headers, registers pages in appropriate data structure.
* We assume that num_pages is a multiple of PAGE_SIZE. */
static inline void register_pages(void* start, int num_pages, void* ptr, size_t size, short object_type)
{
#ifdef RADIX_TREE
radix_register(start, num_pages, ptr, size, object_type);
#elif BIBOP
int i;
unsigned long page = (unsigned long)start;
for (i = 0; i < num_pages; ++i) {
bibop[page / PAGE_SIZE].object_type = object_type;
switch (object_type) {
case OBJECT_SMALL: bibop[page / PAGE_SIZE].pageblock = (unsigned long)ptr >> PAGE_BITS;
break;
case OBJECT_MEDIUM: bibop[page / PAGE_SIZE].sph = (unsigned long)ptr >> SUPERPAGE_BITS;
bibop[page / PAGE_SIZE].log_size = quick_log2(size / PAGE_SIZE);
break;
case OBJECT_LARGE: bibop[page / PAGE_SIZE].size = size;
break;
}
page += PAGE_SIZE;
}
#endif
}
/* Makes a request to the system for size bytes. */
static inline void* page_alloc(size_t size)
{
void* addr;
addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (addr == MAP_FAILED) {
perror("page_alloc");
fprintf(stderr, "page_alloc() mmap of size %zd failed\n", size);
fflush(stderr);
exit(1);
}
return addr;
}
/* Frees length bytes to the system. */
static inline void page_free(void* start, size_t length)
{
munmap(start, length);
}
/* Gets a large amount of memory from the OS and tags it appropriately. */
static void* medium_or_large_alloc(size_t size)
{
void* mem;
if (size <= SUPERPAGE_SIZE) {
/* need to round size up to the nearest power-of-2 pages */
size = ceil((double)size / PAGE_SIZE) * PAGE_SIZE;
unsigned int pow = (size_t)ceil(log2(size));
size = 1 << pow;
mem = supermap(size);
/* Optimization: since we really only care about the first
* page with large objects (that's the only page that free()
* ever gets), we only need to register the first page. */
register_pages(mem, 1, ((pageblock_t*)mem)->sph, size, OBJECT_MEDIUM);
headerize_object(&mem, ((pageblock_t*)mem)->sph, size, OBJECT_MEDIUM);
memory_add32(&num_total_medium, 1);
memory_add64(&size_total_medium, size);
}
else {
mem = page_alloc(size);
register_pages(mem, 1, NULL, size, OBJECT_LARGE);
headerize_object(&mem, NULL, size, OBJECT_LARGE);
memory_add32(&num_total_large, 1);
memory_add64(&size_total_large, size);
}
return mem;
}
/* Adds a pageblock to one of the global lists, or frees it to the OS/page manager. */
static void insert_global_free_pageblocks(pageblock_t* pageblock)
{
int size_index = quick_log2((pageblock->mem_pool_size + (unsigned long)pageblock->mem_pool - (unsigned long)pageblock) / PAGE_SIZE)
- quick_log2(MIN_PAGEBLOCK_SIZE / PAGE_SIZE);
if (global_free_pageblocks[size_index].count >= MAX_GLOBAL_INACTIVE) {
superunmap(pageblock, pageblock->mem_pool_size + CACHE_LINE_SIZE);
}
else {
atmc_add32(&global_free_pageblocks[size_index].count, 1);
lf_lifo_enqueue(&global_free_pageblocks[size_index].queue, pageblock);
}
}
static void insert_global_partial_pageblocks(pageblock_t* pageblock, int class_index)
{
atmc_add32(&global_partial_pageblocks[class_index].count, 1);
lf_lifo_enqueue(&global_partial_pageblocks[class_index].queue, pageblock);
}
/* Attempts to get a global pageblock. */
static pageblock_t* remove_global_pageblocks(int class_index, int pageblock_size)
{
pageblock_t* pageblock = (pageblock_t*)lf_lifo_dequeue(&global_partial_pageblocks[class_index].queue);
if (pageblock) {
atmc_add32(&global_partial_pageblocks[class_index].count, -1);
}
else {
int size_index = quick_log2(pageblock_size / PAGE_SIZE) - quick_log2(MIN_PAGEBLOCK_SIZE / PAGE_SIZE);
pageblock = (pageblock_t*)lf_lifo_dequeue(&global_free_pageblocks[size_index].queue);
if (pageblock) {
atmc_add32(&global_free_pageblocks[size_index].count, -1);
}
}
return pageblock;
}
/* Places new_node at the front of the list. */
static void double_list_insert_front(void* new_node, double_list_t* list)
{
double_list_elem_t* elem_new = (double_list_elem_t*)new_node;
double_list_elem_t* old_head = list->head;
if (old_head == NULL) {
list->tail = elem_new;
}
else {
old_head->prev = elem_new;
}
elem_new->next = old_head;
elem_new->prev = NULL;
list->head = elem_new;
}
/* Moves head to the back. */
static void double_list_rotate_back(double_list_t* list)
{
double_list_elem_t* old_head = list->head;
double_list_elem_t* old_tail = list->tail;
double_list_elem_t* new_head = NULL;
if (old_head == old_tail) {
return;
}
new_head = old_head->next;
new_head->prev = NULL;
old_tail->next = old_head;
old_head->prev = old_tail;
old_head->next = NULL;
list->head = new_head;
list->tail = old_head;
}
/* Removes node from the list. */
static void double_list_remove(void* node, double_list_t* list)
{
double_list_elem_t* elem_node = (double_list_elem_t*)node;
if (elem_node->prev != NULL) {
elem_node->prev->next = elem_node->next;
}
else {
list->head = elem_node->next;
}
if (elem_node->next != NULL) {
elem_node->next->prev = elem_node->prev;
}
else {
list->tail = elem_node->prev;
}
if (list->head != NULL && list->head->next == NULL) {
list->tail = list->head;
}
else if (list->tail != NULL && list->tail->prev == NULL) {
list->head = list->tail;
}
}
/* Garbage collect a single pageblock. */
static void garbage_collect(pageblock_t* collectee)
{
unsigned int chain;
queue_node_t header;
unsigned short index;
#ifdef PROFILE
unsigned long long local_garbage_cycles = get_cycles();
#endif
chain = lf_lifo_chain_dequeue_nABA32((unsigned int *)&(collectee->garbage_head));
header = *((queue_node_t*)&chain);
index = header.next;
collectee->freed = index;
collectee->num_free_objects += ((queue_node_t *)&header)->count;
#ifdef PROFILE
garbage_cycles += get_cycles() - local_garbage_cycles;
#endif
}
void streamflow_thread_finalize(void)
{
int i;
pageblock_t *pageblock, *next_pageblock;
for (i = 0; i < OBJECT_SIZE_CLASSES; ++i) {
heap_t* heap = &local_heap[i];
/* TODO: Optimization. Create a linked list of all pageblocks
* that need to go on the global list. (Both active and
* inactive.) Then atomically link that list into the
* global list. */
pageblock = (pageblock_t*)heap->active_pageblocks.head;
/* If active head is NULL the specific size has never been used */
if (pageblock) {
do {
next_pageblock = pageblock->next;
if (pageblock->num_free_objects == pageblock->mem_pool_size / pageblock->object_size) {
insert_global_free_pageblocks(pageblock);
}
else {
if (pageblock->num_free_objects > 0 || pageblock->garbage_head.next != 0) {
insert_global_partial_pageblocks(pageblock, i);
}
else {
unsigned long long with_id;
unsigned long long no_id;
with_id = pageblock->together;
((unsigned int*)&no_id)[0] = ORPHAN;
((unsigned int*)&no_id)[1] = 0;
if (!compare_and_swap64(&(pageblock->together), with_id, no_id)) {