forked from HNUSystemsLab/ZBTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbtree.cpp
2162 lines (2012 loc) · 71.2 KB
/
zbtree.cpp
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
#include "zbtree.h"
// #define RECOVERY
// #define USE_PMDK
#define IGNORE
#ifdef BREAKDOWN_FIND
thread_local uint64_t breakdown_search=0;
thread_local uint64_t breakdown_check=0;
thread_local uint64_t breakdown_write=0;
thread_local uint64_t breakdown_split=0;
thread_local uint64_t breakdown_split2=0;
thread_local uint64_t breakdown_split2_write=0;
thread_local uint64_t breakdown_split2_split=0;
#endif
#define BINARY_HOLD 7
#define POOL_SIZE 64*1024*1024*1024ULL
#define TESTINLINE NOINLINE
#define THREAD_INIT 996996
// #define CXL
//#define SPACE
// #define BREAKDOWN
extern size_t pm_pool_size;
extern int run_thread;
extern int read_threshold;
extern int write_threshold;
std::atomic<size_t> countshit(0);
size_t count_dram = 0;
std::mutex thread_fence;
std::unordered_map<int, DSeg*> workers;
std::unordered_map<int, N_alc*> mmanagers;
thread_local int tid=114514;
#ifdef USE_PMDK
PMEMobjpool* pop;
#else
//thread_local N_alc tree_alloc(pm_pool_size);
#endif
thread_local Cursor root_path[MAX_TREE_LEVEL];
//thread_local DSeg sl;
static const uint64_t kFNVPrime64 = 1099511628211;
#ifdef _MSC_VER
#define FORCEINLINE __forceinline
#define NOINLINE __declspec(noinline)
#define ALIGN(n) __declspec(align(n))
FORCEINLINE uint32_t bsr(uint32_t x) {
unsigned long res;
_BitScanReverse(&res, x);
return res;
}
FORCEINLINE uint32_t bsf(uint32_t x) {
unsigned long res;
_BitScanForward(&res, x);
return res;
}
#else
#define FORCEINLINE __attribute__((always_inline)) inline
#define NOINLINE __attribute__((noinline))
#define ALIGN(n) __attribute__((aligned(n)))
#endif
#ifdef DALC
thread_local D_alc tree_dlc;
#endif
thread_local adaptive_slot finger_buffer[MAX_LEAF_KEY];
void* pmdk_alloc(size_t size, PMEMobjpool* ppop){
PMEMoid paddr;
pmemobj_alloc(ppop, &paddr, size, 0, nullptr, nullptr);
void* return_ptr = pmemobj_direct(paddr);
return return_ptr;
}
void pmdk_free(void* ptr){
PMEMoid pmdk_ptr = pmemobj_oid(ptr);
pmemobj_free(&pmdk_ptr);
}
static int ceiling(int key_num, int node_key){
return (key_num + node_key - 1) / node_key;
}
void finger_differ(adaptive_slot* array, int* results, int count_result){
int i=0;
int temp[count_result];//1 for fake duplicate, 2 for real duplicate, 0 for uncheck
memset(temp, 0, sizeof(int) * count_result);
//divided conquer
//both left
for(; i<count_result-1 && results[i] < LEFT_KEY - 1; i++)
{
if(temp[i] != 0 )
continue;
for(int j= i + 1; j<count_result && results[j] < LEFT_KEY; j++)
{
if(array[results[j]].sub_slot.hash_byte == array[results[i]].sub_slot.hash_byte)
{
temp[j] = 1;
temp[i] = 1;
}
}
}
//both right
for(; i < count_result-1; i++)
{
for(int j = i +1; j<count_result; j++)
{
if(array[results[j]].sub_slot.hash_byte == array[results[i]].sub_slot.hash_byte)
{
temp[j] = temp[i] = 1;
}
}
}
for(int j=0; j<count_result; j++)
{
if(temp[j] != 1)
{
array[results[j]].sub_slot.access_bit = 0;
}
}
}
void qsort(int start, int end, Node_entry* temp, adaptive_slot* temp2){
if(start >= end)
return;
Key_t sentinal = temp[start].one_key;
void* sentinal_add;
uint8_t hash_temp;
int start_rem = start;
int end_rem = end;
while(start < end)
{
while(temp[end].one_key >= sentinal && start < end)
{
end--;
}
std::swap(temp[end], temp[start]);
std::swap(temp2[end], temp2[start]);
while(temp[start].one_key <= sentinal && start < end)
{
start++;
}
std::swap(temp[end], temp[start]);
std::swap(temp2[end], temp2[start]);
}
qsort(start_rem, start, temp, temp2);
qsort(start+1, end_rem, temp, temp2);
}
unsigned char hashfunc(uint64_t val)
{
unsigned char hash = 123;
int i;
for (i = 0; i < sizeof(uint64_t); i++)
{
uint64_t octet = val & 0x00ff;
val = val >> 8;
hash = hash ^ octet;
hash = hash * kFNVPrime64;
}
return hash;
}
DSeg::DSeg(){
//split_log = (Log_buffer*) tree_alloc.n_allocate(sizeof(Log_buffer));
}
DSeg::~DSeg(){
// std::cout<<"destruct"<<std::endl;
// std::cout<<"log seg is "<<seg_count<<std::endl;
}
size_t timer_write=0;
size_t timer_split=0;
void DSeg::resize(size_t size){
#ifdef USE_PMDK
void* log_start = pmdk_alloc(CHUNK_SIZE, pop);
#else
if(tid == 114514)
{
tid = gettid() % 56;
}
#ifdef CXL
void* log_start = malloc(size);
#else
if(mmanagers[tid] == nullptr)
{
N_alc* new_manager = new N_alc(pm_pool_size);
mmanagers[tid] = new_manager;
}
void* log_start = mmanagers[tid]->n_allocate(size, 1);
#endif
//void* log_start = tree_alloc.n_allocate(size, 1);
#endif
// if(log_vec!=nullptr)//append log end,link different segment
// {
// log_vec[log_end].pptr = (Leaf_Node*)log_start;
// log_vec[log_end].op_kind = LOG_SEGMENT;
// persist(&log_vec[log_end]);
// }
log_vec = (Log_Node*)log_start;
batch_count = 0;
temp_offset = 0;
seg_count++;
}
void* DSeg::append_one(size_t out_key, size_t out_val, void* out_pptr, char out_op, uint32_t version){
// #ifdef USE_PMDK
// Log_Node* new_node = (Log_Node*)pmdk_alloc(sizeof(Log_Node), pop);
// new_node->version_number = version;
// new_node->pptr = out_pptr;
// new_node->op_kind = out_op;
// new_node->key = out_key;
// new_node->val = out_val;
// pmem_persist(new_node, sizeof(Log_Node));
// return &new_node->key;
// #endif
if(temp_offset + sizeof(Log_Node) + version >= log_end || log_vec == nullptr)
{
resize(CHUNK_SIZE - CACHELINE*2);
vec_start = (size_t)log_vec;
temp_offset = 0;
}
#ifdef LONGVAL
Log_Node* one_node = (Log_Node*)vec_start;
one_node->version_number = version;
one_node->pptr = out_pptr;
one_node->op_kind = out_op;
one_node->key = out_key;
one_node->val = out_val;
vec_start += sizeof(Log_Node);
temp_offset += sizeof(Log_Node);
//value
char* pm_value = (char*)vec_start;
memcpy(pm_value, (char*)out_val, version);
vec_start += version;
temp_offset += version;
persist_range(one_node, sizeof(Log_Node) + version);
#else
Log_Node* one_node = (Log_Node*)vec_start;
//std::cout<<"one node is "<<one_node<<std::endl;
//one_node->timestamp = out_timestamp;
one_node->version_number = version;
one_node->pptr = out_pptr;
one_node->op_kind = out_op;
one_node->key = out_key;
one_node->val = out_val;
vec_start += sizeof(Log_Node);
temp_offset += sizeof(Log_Node);
persist_range(one_node, sizeof(Log_Node));
//persist(&log_vec[temp_offset]);
// temp_offset++;
// batch_count++;
// if(batch_count == 8)
// {
// persist_range(&log_vec[temp_offset-batch_count], 256);
// batch_count = 0;
// }
// if(log_end == temp_offset)
// {
// if(batch_count != 0)
// {
// persist_range(&log_vec[temp_offset - batch_count], sizeof(Log_Node) * batch_count);
// batch_count = 0;
// }
// resize(CHUNK_SIZE - CACHELINE * 2);
// temp_offset = 0;
// }
#endif
return &one_node->key;
}
//collect valid log and reuse the log space if possible
void DSeg::recycle(){
//collect valied ptr field
//update index ptr field
//move to new block
}
void DSeg::replay(std::vector<finger_array> &finger_vec, std::vector<Key_t> &key_vec){
int this_tid = gettid() % 56;
for(auto iter=mmanagers[this_tid]->nlog_list.begin(); iter!=mmanagers[this_tid]->nlog_list.end(); iter++)
{
Base_Meta* one_page = iter->second;
if(one_page->bitmap == 0)
{
continue;
}
size_t start_address = (size_t)iter->second->start_address;
for(int i=0; i<log_end; i++)
{
Log_Node* kv_log = (Log_Node*)start_address;
//D_Leaf* insert_leaf = (D_Leaf*)clht_get(elysia->ht, (size_t)kv_log->pptr);
// if(insert_leaf->max_key >= kv_log->key && insert_leaf->min_key <= kv_log->key)//the right place to insert
// {
// int insert_pos = __sync_fetch_and_add(&insert_leaf->max_use, 1);
// insert_leaf->finger_print[insert_pos].hash_byte = hashfunc(kv_log->key);
// insert_leaf->finger_print[insert_pos].ptr_field = (uint64_t)&kv_log->key;
// }
// else
if(kv_log->version_number== 0)
break;
else
{
//insert into somewhere else
//TODO add else place exactly
finger_array new_finger;
new_finger.hash_byte = hashfunc(kv_log->key);
new_finger.ptr_field = (uint64_t)&kv_log->key;
finger_vec.push_back(new_finger);
key_vec.push_back(kv_log->key);
}
start_address += sizeof(Log_Node);
}
}
}
bool test_exist(const char* file_name){
std::ifstream f1(file_name);
if(f1.good())
return true;
return false;
}
void* inner_alloc(size_t sz){
void* ptr;
posix_memalign(&ptr, 64, sz);
return ptr;
}
Tree::Tree(void* start_address){
#ifdef USE_PMDK
if(test_exist("/mnt/pmem/zb_pmdk"))
{
std::cout<<"pmem file exist!"<<std::endl;
exit(1);
}
pop = pmemobj_create("/mnt/pmem/zb_pmdk", "zbtree", POOL_SIZE, 0666);
#endif
#ifdef RECOVERY
if(test_exist("/mnt/pmem/zbtree/nyx0"))
{
recover();
return;
}
#endif
root = nullptr;
std::cout<<"Inner node size is "<<sizeof(Inner_Node)<<std::endl;
std::cout<<"DLeaf node size is "<<sizeof(D_Leaf)<<std::endl;
std::cout<<"Log node size is "<<sizeof(Log_Node)<<std::endl;
std::cout<<"hash fp "<<(int) fp_hash(0)<<std::endl;
std::cout<<"Log buffer size is "<<sizeof(Log_buffer)<<std::endl;
std::cout<<"PLeaf size is "<<sizeof(P_Leaf)<<std::endl;
std::cout<<"finger array is "<<sizeof(finger_array)<<std::endl;
#ifdef DALC
D_Leaf* new_meta = (D_Leaf*)tree_dlc.huge_malloc(sizeof(D_Leaf));
#else
D_Leaf* new_meta = (D_Leaf*)malloc(sizeof(D_Leaf));
#ifdef SPACE
count_dram += sizeof(D_Leaf);
#endif
#endif
#ifdef USE_PMDK
//pmemobj_alloc(pop, &pmdk_ptr, sizeof(Leaf_Node), 0, nullptr, nullptr);
P_Leaf* new_root = (P_Leaf*)pmdk_alloc(sizeof(P_Leaf), pop);
#else
//Leaf_Node* new_root = (Leaf_Node*)tree_alloc.n_allocate(sizeof(Leaf_Node));
if(tid == 114514)
tid = gettid() % run_thread;
#ifdef CXL
P_Leaf* new_root = (P_Leaf*) malloc(sizeof(P_Leaf));
#else
if(mmanagers[tid] == nullptr)
{
N_alc* new_alloc = new N_alc(pm_pool_size);
mmanagers[tid] = new_alloc;
}
P_Leaf* new_root = (P_Leaf*)mmanagers[tid]->n_allocate(sizeof(P_Leaf));
#endif
//P_Leaf* new_root = (P_Leaf*)tree_alloc.n_allocate(sizeof(P_Leaf));
#endif
new_root->next = nullptr;
new_root->version_field = 0;
//new_root->leaf_id = 114514;
//insert into log
//new_root->slot_all[0].one_key = key;
//new_root->slot_all[0].val = (char*)val;
//new_root->bitmap = 1;
leaf_start = new_root;
dstart = new_meta;
new_meta->alt_ptr = new_root;
new_meta->max_use = 0;
new_meta->temp_insert = 0;
//new_meta->min_key = ~0ULL;
new_meta->max_key = 0;
memset(new_meta->finger_print, 0, MAX_LEAF_KEY * 8);
//new_meta->version_field = 0;
//new_meta->key_max = key;
root = (char*) new_meta;
#ifndef CXL
// size_t* anchor = (size_t*)(mmanagers[tid]->start_address + 64);
// anchor[0] = (size_t)new_root;
// persist(anchor);
#endif
persist_fence();
//init log_buffer space
std::cout<<"Panchor start is "<<leaf_start<<std::endl;
std::cout<<"Mmanager id is "<<tid<<std::endl;
}
//find key from left most leaf, used for debug
void Tree::liner_clear(){
// D_Leaf* start = dstart;
// while(start!= nullptr)
// {
// start->version_write = 0;
// start->version_read = 0;
// start = start->next;
// }
}
void Tree::liner_find(Key_t key){
D_Leaf* start = dstart;
// Leaf_Node* start2 = (Leaf_Node*)start->alt_ptr;
size_t count=0;
size_t count2=0;
size_t count3=0;
size_t count4=0;
while(start != nullptr)
{
// for(int i=0; i<start->max_use; i++)
// {
// if(start->finger_print[i].sub_slot.control_bit > start->max_use && start->finger_print[i].sub_slot.access_bit == 0)
// {
// count++;
// }
// else
// count2++;
// if(start->finger_print[i].sub_slot.access_bit == 1)
// count3++;
// }
count2++;
// if(start->version_write > write_threshold && start->version_read > read_threshold)
// count++;
// if(start->version_write > write_threshold)
// count3++;
// if(start->version_read > read_threshold)
// count4++;
start = start->next;
}
std::cout<<"count is "<<count<<" "<<"count2 is "<< count2<<" version write "<<count3 <<
" version read "<<count4<<std::endl;
}
size_t Tree::find(Key_t key, int val_sz){
tbb::speculative_spin_rw_mutex::scoped_lock lock_find;
// if(tid == 114514)
// {
// tid = gettid() % run_thread;
// if(workers[tid] == nullptr)
// {
// workers[tid] = new DSeg();
// }
// }
Find_begin:
int search_level = 0;
if(temp_level == 0)//only have root
{
D_Leaf* root_meta = reinterpret_cast<D_Leaf*>(root);
uint8_t fingerprint = hashfunc(key);
for(int i=0; i<MAX_LEAF_KEY; i++)
{
if(root_meta->finger_print[i].sub_slot.hash_byte == fingerprint)
{
size_t* kv_ptr = (size_t*) root_meta->finger_print[i].sub_slot.ptr_field;
if(kv_ptr[0] == key)
return kv_ptr[1];
}
}
std::cout<<"key not existed"<<std::endl;
return 0;
}
// lock_find.acquire(function_lock, false);
Inner_Node* start_node = (Inner_Node*)root;
//inner
#ifdef BREAKDOWN_FIND
auto global_start = read_rdtsc();
#endif
while(search_level <= temp_level-1)
{
prefetch0(start_node, sizeof(Inner_Node));
{
#ifdef BREAKDOWN_FIND
auto query_start = read_rdtsc();
#endif
int start=0;
int end = start_node->max_size - 1;
#ifdef BIN_SEARCH
binary_search(key, start_node, start, end);
if(end == -1)
{
start_node = reinterpret_cast<Inner_Node*>(start_node->node_key[1][start]);
}
#endif
for(int i=start; i<=end; i++)
{
if(start_node->node_key[0][i] >= key && i == 0)//left most case
{
#ifdef BREAKDOWN_FIND
auto query_end = read_rdtsc();
breakdown_search += query_end - query_start;
//next_value = start_node->left_most_ptr;
#endif
start_node = reinterpret_cast<Inner_Node*>(start_node->left_most_ptr);
//if(start_node == nullptr)
// std::cout<<"find null in if 2"<<std::endl;
break;
}
else if(i == end || start_node->node_key[0][i] < key && start_node->node_key[0][i+1] >= key)
{
#ifdef BREAKDOWN_FIND
auto query_end = read_rdtsc();
breakdown_search += query_end - query_start;
#endif
start_node = reinterpret_cast<Inner_Node*>(start_node->node_key[1][i]);
break;
}
}
}
search_level++;
}
#ifdef BREAKDOWN_FIND
auto global_end = read_rdtsc();
//breakdown_alc += global_end - global_start;
#endif
//leaf
//some way to reduce query overhead
D_Leaf* leaf_meta = reinterpret_cast<D_Leaf*>(start_node);
uint8_t version_before = leaf_meta->temp_insert;
char* find_val = NULL;
uint8_t fingerprint = hashfunc(key);
//slow path search, caused by split process
// lock_find.release();
if(leaf_meta->temp_insert > 1)//is being split, find value from the alt buffer
{
// goto Find_begin;
#ifndef LONGVAL
Node_entry* cow_buffer = (Node_entry*)leaf_meta->alt_ptr;
for(int i=0; i<MAX_LEAF_KEY; i++)
{
if(cow_buffer[i].one_key == key)
{
return cow_buffer[i].val;
}
}
return 0;
#endif
}
//fast path search, using fingerprint
prefetch0(leaf_meta, sizeof(D_Leaf));
for(int i=0; i<leaf_meta->max_use; i++)
{
if(leaf_meta->finger_print[i].sub_slot.hash_byte == fingerprint)
{
//size_t* kv_ptr = (size_t*)leaf_meta->finger_print[i].ptr_field;
// if(kv_ptr[0] == key)
// return (char*)kv_ptr[1];
#ifdef NO_COMPACT
size_t* kv_ptr = (size_t*)leaf_meta->finger_print[i].sub_slot.ptr_field;
if(kv_ptr[0] == key)
{
//size_t v_ptr = (size_t)leaf_meta->finger_print[i].sub_slot.ptr_field + 16;
return (char*)kv_ptr[1];
}
#else
if(leaf_meta->finger_print[i].sub_slot.control_bit >= leaf_meta->max_use)
{
// return (char*)leaf_meta->finger_print[i].val;
size_t lodging_key = leaf_meta->finger_print[leaf_meta->finger_print[i].sub_slot.control_bit].val;
if(lodging_key == key)
{
if(leaf_meta->finger_print[i].sub_slot.access_bit < 3)
leaf_meta->finger_print[i].sub_slot.access_bit++;
// workers[tid]->hit_count++;
// leaf_meta->version_read++;
return leaf_meta->finger_print[leaf_meta->finger_print[i].sub_slot.control_bit + 1].val;
}
}
else
{
size_t* kv_ptr = (size_t*)leaf_meta->finger_print[i].sub_slot.ptr_field;
if(kv_ptr[0] == key)
{
if(leaf_meta->finger_print[i].sub_slot.access_bit < 3)
leaf_meta->finger_print[i].sub_slot.access_bit++;
// leaf_meta->version_read++;
// countshit++;
return kv_ptr[1];
}
}
//return (char*)leaf_meta->finger_print[i].ptr_field;
#endif
}
}
// abort();
return 0;
#ifdef DEBUG
if(find_val == nullptr)
{
liner_find(key);
std::cout<<"nothing find!"<<std::endl;
}
#endif
//shit_mutex.unlock();
}
uint64_t Tree::find_insert(Key_t key, Cursor* path_array){
if(temp_level == 0)
{
return (uint64_t)root;
}
int search_level = 0;
Inner_Node* start = (Inner_Node*) root;
uint64_t next_value;//used for address->offset transfer
int i;
while(search_level <= temp_level - 1)//search non-leaf node,return leaf node
{
prefetch0(start, sizeof(Inner_Node));
path_array[search_level].node = start;
path_array[search_level].node_max = start->max_size;
path_array[search_level].node_version = start->version_field;
#ifdef AVX_512
{
#ifdef BREAKDOWN_FIND
auto query_start = read_rdtsc();
#endif
int size_max = start->max_size;
int find_pos = linear_search_avx_512ul(start->node_key[0], size_max, key);
#ifdef BREAKDOWN_FIND
auto query_end = read_rdtsc();
breakdown_split += query_end - query_start;
#endif
if(find_pos == -1)
{
start = reinterpret_cast<Inner_Node*>(start->left_most_ptr);
path_array[search_level].node_pos = -1;
}
else
{
start = reinterpret_cast<Inner_Node*>(start->node_key[1][find_pos]);
path_array[search_level].node_pos = find_pos;
}
}
#else
{
int start_pos = 0;
int end_pos = path_array[search_level].node_max - 1;
#ifdef BIN_SEARCH_INSERT
binary_search(key, start, start_pos, end_pos);
#endif
#ifdef BREAKDOWN_FIND
auto query_start = read_rdtsc();
#endif
for(i=start_pos; i<=end_pos; i++)
{
if(start->node_key[0][i] >= key && i == 0)//left most case
{
#ifdef BREAKDOWN_FIND
auto query_end = read_rdtsc();
breakdown_split += query_end - query_start;
#endif
start = reinterpret_cast<Inner_Node*>(start->left_most_ptr);
path_array[search_level].node_pos = -1;
//if(start == nullptr)
// std::cout<<"find null in if 2"<<std::endl;
break;
}
else if(i == path_array[search_level].node_max - 1 || start->node_key[0][i] < key && start->node_key[0][i+1] >= key)
{
#ifdef BREAKDOWN_FIND
auto query_end = read_rdtsc();
breakdown_split += query_end - query_start;
#endif
start = reinterpret_cast<Inner_Node*>(start->node_key[1][i]);
path_array[search_level].node_pos = i;
//if(start == nullptr)
// std::cout<<"find null in if 1"<<std::endl;
break;
}
}
}
#endif
search_level++;
}
//end of inner, return leaf node
#ifdef DEBUG
if(start == nullptr){
std::cout<<"duplicate key outside loop"<<std::endl;
}
#endif
return (uint64_t)start;
}
bool Tree::insert(Key_t key, void* val, int value_len){
tbb::speculative_spin_rw_mutex::scoped_lock lock_insert;
memset(root_path, 0, temp_level);
restart:
lock_insert.acquire(function_lock, false);
#ifdef BREAKDOWN_FIND
auto search_start = read_rdtsc();
#endif
uint64_t leaf_offset = find_insert(key, root_path);
#ifdef BREAKDOWN_FIND
auto search_end = read_rdtsc();
breakdown_search+=(search_end - search_start);
auto except_search_start = read_rdtsc();
#endif
D_Leaf* leaf_meta = (D_Leaf*) leaf_offset;
#ifdef DEBUG
if(leaf_meta->pleaf == nullptr)//debug, output error
{
std::cout<<"error key, nullptr detacted"<<std::endl;
abort();
}
#endif
bool lock_state = __sync_bool_compare_and_swap(&leaf_meta->temp_insert, 0, 1);
if(!lock_state)//check than insert
{
lock_insert.release();
goto restart;
}
lock_insert.release();
//now get real leaf
#ifdef BREAKDOWN_FIND
auto check_start = read_rdtsc();
#endif
//enter critical section
prefetch0(leaf_meta, sizeof(D_Leaf));
bool duplicate = false;
uint8_t fingerprint = hashfunc(key);
int compact_flag=0;
for(int i=0; i<leaf_meta->max_use; i++)
{
if(leaf_meta->finger_print[i].sub_slot.hash_byte == fingerprint)
{
//countshit++;
if(leaf_meta->finger_print[i].sub_slot.control_bit > leaf_meta->max_use)
{
size_t lodging_key = leaf_meta->finger_print[leaf_meta->finger_print[i].sub_slot.control_bit].val;
if(lodging_key == key)
return false;
}
else
{
size_t* kv_ptr = (size_t*)leaf_meta->finger_print[i].sub_slot.ptr_field;
if(kv_ptr[0] == key)
return false;
}
// else
// {
// leaf_meta->finger_print[i].sub_slot.access_bit = 1;
// duplicate = true;
// }
}
}
#ifdef BREAKDOWN_FIND
auto check_end = read_rdtsc();
breakdown_check += check_end - check_start;
#endif
if(leaf_meta->max_use < MAX_LEAF_KEY)
{
#ifdef BREAKDOWN
auto write_start = read_rdtsc();
#endif
//first meta
//uint32_t version_now = leaf_meta->version_field;
if(tid == 114514)
tid = gettid() % run_thread;
if(workers[tid] == nullptr)
{
DSeg *sl = new DSeg();
workers[tid] = sl;
}
void* kv_addr = workers[tid]->append_one(key, (size_t)val, leaf_meta->alt_ptr, LOG_INSERT, value_len);
//void* kv_addr = sl.append_one(key, (size_t)val, (Leaf_Node*)leaf_meta->alt_ptr, LOG_INSERT, 1);
int insert_pos = leaf_meta->max_use;
leaf_meta->max_use++;
if(leaf_meta->max_key < key)
leaf_meta->max_key = key;
//write inline data
leaf_meta->finger_print[insert_pos].sub_slot.hash_byte = fingerprint;
leaf_meta->finger_print[insert_pos].sub_slot.ptr_field = (uint64_t)kv_addr;
leaf_meta->finger_print[insert_pos].sub_slot.control_bit = 0;
leaf_meta->finger_print[insert_pos].sub_slot.access_bit = 0;
leaf_meta->temp_insert = 0;
//write log
#ifdef BREAKDOWN
auto write_end = read_rdtsc();
timer_write += write_end - write_start;
#endif
}
else
{
//leaf is full, need split
P_Leaf* insert_leaf = (P_Leaf*)leaf_meta->alt_ptr;
//prefetch0(insert_leaf, sizeof(Leaf_Node));
//store kv to dram
Node_entry dram_buffer[MAX_LEAF_KEY];
int results[MAX_LEAF_KEY];
int count_result = 0;
for(int i=0; i<MAX_LEAF_KEY; i++)
{
uint64_t* kv_ptr = (uint64_t*)leaf_meta->finger_print[i].sub_slot.ptr_field;
dram_buffer[i].one_key = kv_ptr[0];
dram_buffer[i].val = kv_ptr[1];
finger_buffer[i] = leaf_meta->finger_print[i];
finger_buffer[i].sub_slot.control_bit = 0;
}
//sort
qsort(0, MAX_LEAF_KEY - 1, dram_buffer, finger_buffer);
leaf_meta->alt_ptr = dram_buffer;
persist_fence();
leaf_meta->temp_insert++;
// for(int i=0; i<MAX_LEAF_KEY; i++)
// {
// if(finger_buffer[i].sub_slot.access_bit == 1)
// {
// results[count_result] = i;//position
// count_result++;
// }
// }
#ifdef BREAKDOWN
auto start_split = read_rdtsc();
#endif
// if(count_result != 0)//has duplicate fingerprint, need filter
// finger_differ(finger_buffer, results, count_result);
//cow kv pair to log buffer
//nt_64((char*)sl.split_log->buffer, (char*)dram_buffer, sizeof(Node_entry) * MAX_LEAF_KEY);
//sl.split_log->alt = leaf_meta->version_field;
//sl.split_log->pleaf = insert_leaf;
//execute split process
D_Leaf* leaf_meta2 = (D_Leaf*)split_leaf(insert_leaf, leaf_meta, finger_buffer, dram_buffer);
persist(insert_leaf);
persist(leaf_meta2->alt_ptr);
#ifdef BREAKDOWN
auto end_split = read_rdtsc();
timer_split += end_split - start_split;
#endif
uint64_t split_key = dram_buffer[SPLIT_POS].one_key;
if(temp_level == 0)//root case split
{
//shit_mutex.lock();
lock_insert.acquire(function_lock);
//shit_mutex.lock();
//std::unique_lock<std::shared_mutex> smo_lock(mut_);
Inner_Node* new_root = (Inner_Node*) malloc(sizeof(Inner_Node));
#ifdef SPACE
count_dram += sizeof(Inner_Node);
#endif
//Inner_Node* new_root = (Inner_Node*) inner_alloc(sizeof(Inner_Node));
memset(new_root->node_key[0], 0xff, (MAX_LEAF_KEY + 1) * sizeof(size_t));
new_root->node_key[0][0] = dram_buffer[SPLIT_POS].one_key;
new_root->left_most_ptr = leaf_offset;
new_root->node_key[1][0] = (uint64_t)leaf_meta2;
new_root->max_size = 1;
root = (char*) new_root;
temp_level++;
//temp_leaf++;
new_root->version_field = 0;
//lock_insert.release();
leaf_meta->temp_insert = 0;
leaf_meta->alt_ptr = insert_leaf;
lock_insert.release();
#ifdef BREAKDOWN_FIND
auto except_search_end = read_rdtsc();
breakdown_split2+=(except_search_end - except_search_start);
#endif
goto restart;
//return true;
}
//std::unique_lock<std::shared_mutex> smo_lock22(mut_);
memset(root_path, 0, temp_level);
lock_insert.acquire(function_lock);
find_insert(key, root_path);
splitInnerNode(root_path, split_key, (uint64_t)leaf_meta, (uint64_t)leaf_meta2);
leaf_meta->temp_insert = 0;
leaf_meta->alt_ptr = insert_leaf;
lock_insert.release();
goto restart;
}
#ifdef BREAKDOWN_FIND
auto except_search_end = read_rdtsc();
breakdown_split2+=(except_search_end - except_search_start);
#endif
return true;
}
bool Tree::update(Key_t key, void* val, bool is_upsert){
//update val ptr
tbb::speculative_spin_rw_mutex::scoped_lock lock_update;
uint8_t fingerprint = hashfunc(key);
Update_begin:
lock_update.acquire(function_lock, false);
Inner_Node* start_node = (Inner_Node*)root;
int search_level = 0;
while(search_level <= temp_level-1)
{
prefetch0(start_node, sizeof(Inner_Node));
int start=0;
int end = start_node->max_size - 1;
for(int i=start; i<=end; i++)
{
if(start_node->node_key[0][i] >= key && i == 0)
{
start_node = reinterpret_cast<Inner_Node*>(start_node->left_most_ptr);
break;
}
else if(i == end || start_node->node_key[0][i] < key && start_node->node_key[0][i+1] >= key)
{
start_node = reinterpret_cast<Inner_Node*>(start_node->node_key[1][i]);
break;
}
}
search_level++;
}
D_Leaf* leaf_meta = (D_Leaf*) start_node;
//check lock state of leaf
if(leaf_meta->temp_insert > 1)
{
lock_update.release();
goto Update_begin;
}
else
{
lock_update.release();
}
bool is_updated = false;
for(int i=0; i<leaf_meta->max_use; i++)