-
Notifications
You must be signed in to change notification settings - Fork 0
/
masstree_struct.hh
1423 lines (1232 loc) · 40.2 KB
/
masstree_struct.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2016 President and Fellows of Harvard College
* Copyright (c) 2012-2016 Massachusetts Institute of Technology
*
* VLSC Laboratory
* Copyright (c) 2018-2019 Ecole Polytechnique Federale de Lausanne
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef MASSTREE_STRUCT_HH
#define MASSTREE_STRUCT_HH
#include "masstree.hh"
#include "nodeversion.hh"
#include "stringbag.hh"
#include "mtcounters.hh"
#include "timestamp.hh"
#include "incll_configs.hh"
#include "incll_globals.hh"
typedef uint64_t mrcu_epoch_type;
extern volatile mrcu_epoch_type globalepoch;
extern volatile mrcu_epoch_type failedepoch;
#ifdef MTAN
extern std::atomic<size_t> nrecords_lf;
extern std::atomic<size_t> nrecords_in;
extern std::atomic<size_t> ninsert_incll;
extern std::atomic<size_t> nupdate_incll;
#endif //mtan
namespace Masstree {
template <typename P>
struct make_nodeversion {
typedef nodeversion_parameters<typename P::nodeversion_value_type> parameters_type;
typedef typename mass::conditional<P::concurrent,
nodeversion<parameters_type>,
singlethreaded_nodeversion<parameters_type> >::type type;
};
template <typename P>
struct make_prefetcher {
typedef typename mass::conditional<P::prefetch,
value_prefetcher<typename P::value_type>,
do_nothing>::type type;
};
template <typename P>
class node_base : public make_nodeversion<P>::type {
public:
static constexpr bool concurrent = P::concurrent;
static constexpr int nikey = 1;
typedef leaf<P> leaf_type;
typedef internode<P> internode_type;
typedef node_base<P> base_type;
typedef typename P::value_type value_type;
typedef leafvalue<P> leafvalue_type;
typedef typename P::ikey_type ikey_type;
typedef key<ikey_type> key_type;
typedef typename make_nodeversion<P>::type nodeversion_type;
typedef typename P::threadinfo_type threadinfo;
bool not_logged; //1 byte
uint8_t cl0_idx; //1 byte
mrcu_epoch_type loggedepoch;
node_base(bool isleaf):
nodeversion_type(isleaf),
loggedepoch(globalepoch)
{}
inline base_type* parent() const {
// almost always an internode
if (this->isleaf())
return static_cast<const leaf_type*>(this)->parent_;
else
return static_cast<const internode_type*>(this)->parent_;
}
inline bool parent_exists(base_type* p) const {
return p != nullptr;
}
inline bool has_parent() const {
return parent_exists(parent());
}
inline internode_type* locked_parent(threadinfo& ti) const;
inline void set_parent(base_type* p) {
if (this->isleaf())
static_cast<leaf_type*>(this)->parent_ = p;
else
static_cast<internode_type*>(this)->parent_ = p;
}
inline void make_layer_root() {
set_parent(nullptr);
this->mark_root();
}
inline base_type* maybe_parent() const {
base_type* x = parent();
return parent_exists(x) ? x : const_cast<base_type*>(this);
}
inline leaf_type* reach_leaf(const key_type& k, nodeversion_type& version,
threadinfo& ti) const;
void prefetch_full() const {
for (int i = 0; i < std::min(16 * std::min(P::leaf_width, P::internode_width) + 1, 4 * 64); i += 64)
::prefetch((const char *) this + i);
}
void print(FILE* f, const char* prefix, int depth, int kdepth) const;
void print_node() const;
void record_node(){
REC_ASSERT(this->locked())
if(this->isleaf()){
this->to_leaf()->record_node();
}else{
this->to_internode()->record_node();
}
}
int number_of_keys(){
if(this->isleaf()){
return this->to_leaf()->number_of_keys();
}else{
return this->to_internode()->number_of_keys();
}
}
template <typename SF>
nodeversion_type lock_persistent(nodeversion_type expected, SF spin_function){
auto res = nodeversion_type::lock(expected, spin_function);
this->record_node();
return res;
}
nodeversion_type lock_persistent(){
auto res = nodeversion_type::lock();
this->record_node();
return res;
}
#if defined(EXTLOG) && !defined(INCLL)
void log_persistent(){
this->record_node();
}
#endif
#ifdef INCLL
void log_persistent(){
this->record_node();
}
#endif //incll
void fix_lock(){
if(this->locked()){
this->clear_unlock();
}
}
leaf_type* to_leaf(){
return static_cast<leaf_type*>(this);
}
internode_type* to_internode(){
return static_cast<internode_type*>(this);
}
const leaf_type* to_const_leaf() const{
return static_cast<const leaf_type*>(this);
}
const internode_type* to_const_internode() const{
return static_cast<const internode_type*>(this);
}
size_t allocated_size() const {
if(this->isleaf()){
return this->to_const_leaf()->allocated_size();
}else{
return this->to_const_internode()->allocated_size();
}
}
static leaf_type* get_max_leaf(base_type *n){
if(n->isleaf()){
return n->to_leaf();
}else{
internode_type *in = n->to_internode();
auto in_keys = in->nkeys_;
assert(in->child_[in_keys]);
return base_type::get_max_leaf(in->child_[in_keys]);
}
}
static leaf_type* get_min_leaf(base_type *n){
if(n->isleaf()){
return n->to_leaf();
}else{
internode_type *in = n->to_internode();
auto *child_bound = in->child_[0];
auto *child_normal = in->child_[1];
if(child_bound){
return base_type::get_min_leaf(child_bound);
}else if(child_normal){
return base_type::get_min_leaf(child_normal);
}else{
assert(0);
return nullptr;
}
}
}
};
template <typename P>
class internode : public node_base<P> {
public:
static constexpr int width = P::internode_width;
typedef typename node_base<P>::nodeversion_type nodeversion_type;
typedef key<typename P::ikey_type> key_type;
typedef typename P::ikey_type ikey_type;
typedef node_base<P> base_type;
typedef leaf<P> leaf_type;
typedef internode<P> internode_type;
typedef typename key_bound<width, P::bound_method>::type bound_type;
typedef typename P::threadinfo_type threadinfo;
uint8_t nkeys_;
uint32_t height_;
ikey_type ikey0_[width];
node_base<P>* child_[width + 1];
node_base<P>* parent_;
#ifdef MTAN
bool is_recorded;
#endif //mtan
kvtimestamp_t created_at_[P::debug_level > 0];
internode(uint32_t height)
: node_base<P>(false), nkeys_(0), height_(height), parent_() {
#ifdef MTAN
is_recorded=false;
#endif //mtan
}
static internode<P>* make(uint32_t height, threadinfo& ti) {
void* ptr = ti.pool_allocate(sizeof(internode<P>),
memtag_masstree_internode);
internode<P>* n = new(ptr) internode<P>(height);
assert(n);
if (P::debug_level > 0)
n->created_at_[0] = ti.operation_timestamp();
return n;
}
void record_node(){
if(this->loggedepoch != globalepoch){
DBGLOG("record internode ge:%lu", globalepoch)
#ifdef IN_EXTLOG
GH::node_logger->record(this);
#endif //in extlog
this->loggedepoch = globalepoch;
#ifdef MTAN
is_recorded = true;
++nrecords_in;
#endif //mtan
}
}
int number_of_keys(){
return nkeys_;
}
int size() const {
return nkeys_;
}
key_type get_key(int p) const {
return key_type(ikey0_[p]);
}
ikey_type ikey(int p) const {
return ikey0_[p];
}
int compare_key(ikey_type a, int bp) const {
return ::compare(a, ikey(bp));
}
int compare_key(const key_type& a, int bp) const {
return ::compare(a.ikey(), ikey(bp));
}
inline int stable_last_key_compare(const key_type& k, nodeversion_type v,
threadinfo& ti) const;
void prefetch() const {
for (int i = 64; i < std::min(16 * width + 1, 4 * 64); i += 64)
::prefetch((const char *) this + i);
}
void print(FILE* f, const char* prefix, int depth, int kdepth) const;
void print_node() const;
void deallocate(threadinfo& ti) {
ti.pool_deallocate(this, sizeof(*this), memtag_masstree_internode);
}
void deallocate_rcu(threadinfo& ti) {
ti.pool_deallocate_rcu(this, sizeof(*this), memtag_masstree_internode);
}
size_t allocated_size() const {
return sizeof(*this);
}
int find_child_idx(base_type* child){
int idx = -1;
for (int i = 0; i <= this->size(); ++i) {
if(this->child_[i] == child){
idx = i;
break;
}
}
if(idx == -1){
this->print_node();
printf("BOOM child:%p parent:%p %d size %d\n",
(void*)child, (void*)this, idx, this->size());
}
assert(idx != -1);
return idx;
}
private:
void assign(int p, ikey_type ikey, node_base<P>* child) {
child->set_parent(this);
child_[p + 1] = child;
ikey0_[p] = ikey;
}
void shift_from(int p, const internode<P>* x, int xp, int n) {
masstree_precondition(x != this);
if (n) {
memcpy(ikey0_ + p, x->ikey0_ + xp, sizeof(ikey0_[0]) * n);
memcpy(child_ + p + 1, x->child_ + xp + 1, sizeof(child_[0]) * n);
}
}
void shift_up(int p, int xp, int n) {
memmove(ikey0_ + p, ikey0_ + xp, sizeof(ikey0_[0]) * n);
for (node_base<P> **a = child_ + p + n, **b = child_ + xp + n; n; --a, --b, --n)
*a = *b;
}
void shift_down(int p, int xp, int n) {
memmove(ikey0_ + p, ikey0_ + xp, sizeof(ikey0_[0]) * n);
for (node_base<P> **a = child_ + p + 1, **b = child_ + xp + 1; n; ++a, ++b, --n)
*a = *b;
}
int split_into(internode<P>* nr, int p, ikey_type ka, node_base<P>* value,
ikey_type& split_ikey, int split_type);
template <typename PP> friend class tcursor;
};
template <typename P>
class leafvalue {
public:
typedef typename P::value_type value_type;
typedef typename make_prefetcher<P>::type prefetcher_type;
leafvalue() {
}
leafvalue(value_type v) {
u_.v = v;
}
leafvalue(node_base<P>* n) {
u_.x = reinterpret_cast<uintptr_t>(n);
}
static leafvalue<P> make_empty() {
return leafvalue<P>(value_type());
}
typedef bool (leafvalue<P>::*unspecified_bool_type)() const;
operator unspecified_bool_type() const {
return u_.x ? &leafvalue<P>::empty : 0;
}
bool empty() const {
return !u_.x;
}
value_type value() const {
return u_.v;
}
value_type& value() {
return u_.v;
}
node_base<P>* layer() const {
return reinterpret_cast<node_base<P>*>(u_.x);
}
void prefetch(int keylenx) const {
if (!leaf<P>::keylenx_is_layer(keylenx))
prefetcher_type()(u_.v);
else
u_.n->prefetch_full();
}
private:
union {
node_base<P>* n;
value_type v;
uintptr_t x;
} u_;
};
template <typename P>
class leaf : public node_base<P> {
public:
static constexpr int width = P::leaf_width;
typedef typename node_base<P>::nodeversion_type nodeversion_type;
typedef key<typename P::ikey_type> key_type;
typedef node_base<P> base_type;
typedef leaf<P> leaf_type;
typedef internode<P> internode_type;
typedef typename node_base<P>::leafvalue_type leafvalue_type;
typedef kpermuter<P::leaf_width> permuter_type;
typedef typename P::ikey_type ikey_type;
typedef typename key_bound<width, P::bound_method>::type bound_type;
typedef typename P::threadinfo_type threadinfo;
typedef stringbag<uint8_t> internal_ksuf_type;
typedef stringbag<uint16_t> external_ksuf_type;
typedef typename P::phantom_epoch_type phantom_epoch_type;
static constexpr int ksuf_keylenx = 64;
static constexpr int layer_keylenx = 128;
static constexpr const uint8_t invalid_idx = 7;
enum {
modstate_insert = 0, modstate_remove = 1, modstate_deleted_layer = 2
};
#ifdef INCLL
class incll_lv_{
private:
uint64_t data_;
public:
enum {
idx_mask = 7, //bits 210
non_idx_mask = ~idx_mask,
not_logged_bit = (1UL << 63), //bit 63
not_logged_reset = ~not_logged_bit,
lv_mask = 0x00FFFFFFFFFFFFF8UL, //bits 56-3
non_lv_mask = ~lv_mask,
lv_idx_mask = 0x00FFFFFFFFFFFFFFUL,
non_lv_idx_mask = ~lv_idx_mask,
le_mask = 0x7F00000000000000UL, //bit 62-56
ge_mask = 0x000000000000007FUL, //bit 14-0
non_le_mask = ~le_mask,
epoch_shift = 56,
invalid_idx = 7, //111 number is invalid
lv_idx_ge_mask = 0x7FFFFFFFFFFFFFFFUL, //bits 62-0
non_lv_idx_ge_mask = ~lv_idx_ge_mask,
};
incll_lv_(){
data_ &= 0x0;
set_loggedepoch();
invalidate_cl();
}
int get_cl_idx() const{
return data_ & idx_mask;
}
void set_cl_idx(int p){
REC_ASSERT(p < 7)
data_ = (data_ & non_idx_mask) | p;
}
void invalidate_cl(){
data_ |= invalid_idx;
}
mrcu_epoch_type get_loggedepoch() const{
return ((data_ & le_mask) >> epoch_shift);
}
void set_loggedepoch(mrcu_epoch_type e){
data_ = (data_ & non_le_mask) | ((e & ge_mask) << epoch_shift);
}
void set_loggedepoch(){
data_ = (data_ & non_le_mask) | ((globalepoch & ge_mask) << epoch_shift);
}
bool is_not_logged() const{
return data_ & not_logged_bit;
}
void set_not_logged(){
data_ |= not_logged_bit;
}
void reset_not_logged(){
data_ &= not_logged_reset;
}
void set_lv(uint64_t *lv){
REC_ASSERT(!(*lv & non_lv_mask));
data_ = (data_ & non_lv_mask) | *lv;
}
uint64_t get_lv() const{
return data_ & lv_mask;
}
void set_lv_idx(uint64_t *lv, int p){
REC_ASSERT(!(*lv & non_lv_mask));
REC_ASSERT(p < 7)
data_ = (data_ & non_lv_idx_mask) | *lv | p;
}
void set_lv_idx_ge(uint64_t *lv, int p){
REC_ASSERT(!(*lv & non_lv_mask));
REC_ASSERT(p < 7)
data_ = (data_ & non_lv_idx_ge_mask)
| *lv
| p
| ((globalepoch & ge_mask) << epoch_shift);
}
void recover_lv(uint64_t* lv){
int p = data_ & idx_mask;
lv[p] = data_ & lv_mask;
}
void recover_lv2(uint64_t* lv){
int p = (data_ & idx_mask) + KEY_MID;
lv[p] = data_ & lv_mask;
}
bool is_le_diff(){
return ((data_ & le_mask) >> epoch_shift) != (globalepoch & ge_mask);
}
bool is_cl_valid(){
return (data_ & invalid_idx) != invalid_idx;
}
void print() const;
};
#endif //incll
#ifdef INCLL
//version value //4 bytes
//logged epoch //8 bytes
int8_t extrasize64_; //1 byte
uint8_t modstate_; //1 byte
uint8_t keylenx_[width]; //14 bytes
typename permuter_type::storage_type permutation_; //8 bytes
typename permuter_type::storage_type perm_cl0; //8 bytes
external_ksuf_type* ksuf_; //8 bytes
ikey_type ikey0_[width]; //112 bytes
char padding[8];
incll_lv_ lv_cl1; //8 bytes
leafvalue_type lv_[width]; //112 bytes
incll_lv_ lv_cl2; //8 bytes
union {leaf<P>* ptr;uintptr_t x;} next_; //8 bytes
leaf<P>* prev_; //8 bytes
node_base<P>* parent_; //8 bytes
char tmp;
#ifdef MTAN
bool is_recorded;
#endif //mtan
phantom_epoch_type phantom_epoch_[P::need_phantom_epoch];
kvtimestamp_t created_at_[P::debug_level > 0];
internal_ksuf_type iksuf_[0];
#else // incll
int8_t extrasize64_;
uint8_t modstate_;
uint8_t keylenx_[width];
typename permuter_type::storage_type permutation_;
ikey_type ikey0_[width];
leafvalue_type lv_[width];
external_ksuf_type* ksuf_;
union {
leaf<P>* ptr;
uintptr_t x;
} next_;
leaf<P>* prev_;
node_base<P>* parent_;
#ifdef MTAN
bool is_recorded;
#endif //mtan
phantom_epoch_type phantom_epoch_[P::need_phantom_epoch];
kvtimestamp_t created_at_[P::debug_level > 0];
internal_ksuf_type iksuf_[0];
#endif //incll
leaf(size_t sz, phantom_epoch_type phantom_epoch):
#ifdef INCLL
node_base<P>(true), modstate_(modstate_insert),
permutation_(permuter_type::make_empty()),
ksuf_(), parent_(), iksuf_{} {
#else //incll
node_base<P>(true), modstate_(modstate_insert),
permutation_(permuter_type::make_empty()),
ksuf_(), parent_(), iksuf_{} {
#endif //incll
masstree_precondition(sz % 64 == 0 && sz / 64 < 128);
extrasize64_ = (int(sz) >> 6) - ((int(sizeof(*this)) + 63) >> 6);
if (extrasize64_ > 0)
new((void *)&iksuf_[0]) internal_ksuf_type(width, sz - sizeof(*this));
if (P::need_phantom_epoch)
phantom_epoch_[0] = phantom_epoch;
#ifdef INCLL
this->not_logged = false;
this->cl0_idx = invalid_idx;
/*
static_assert(
(uintptr_t)(&((leaf<P>*)0)->lv_cl1) % 64 == 0,
"incll for lv_ is not cache aligned properly");
*/
/*
printf("%lu %lu %lu %lu %lu %lu %lu %lu\n",
((uintptr_t)&this->permutation_)%64, ((uintptr_t)&this->perm_cl0)%64,
((uintptr_t)&this->ikey0_)%64, ((uintptr_t)&this->next_)%64,
((uintptr_t)&this->prev_)%64, ((uintptr_t)&this->parent_)%64,
((uintptr_t)&this->lv_cl1)%64, ((uintptr_t)&this->lv_)%64
);
*/
#endif //incll
#ifdef MTAN
is_recorded = false;
#endif //mtan
}
int number_of_keys(){
permuter_type perm = permutation_;
return perm.size();
}
#ifdef INCLL
void record_node(){
if(this->loggedepoch != globalepoch || this->not_logged){
DBGLOG("record leaf ge:%lu nl:%d keys:%d",
globalepoch, this->not_logged, this->number_of_keys())
this->not_logged=false;
#ifdef LN_EXTLOG
GH::node_logger->record(this);
#endif //ln extlog
this->loggedepoch = globalepoch;
this->invalidate_cls();
#ifdef MTAN
is_recorded = true;
++nrecords_lf;
#endif //mtan
}
}
void recover_cl0(){
permutation_ = perm_cl0;
}
void recover_cl1(){
lv_cl1.recover_lv((uint64_t*)lv_);
}
void recover_cl2(){
lv_cl2.recover_lv2((uint64_t*)lv_);
}
void save_cl0_insert(){
if(this->loggedepoch != globalepoch){
DBGLOG("save incll insert to %p ge:%lu le:%lu keys:%d",
(void*)this, globalepoch, this->loggedepoch, this->number_of_keys())
perm_cl0 = permutation_;
this->cl0_idx = 0;
this->update_epochs(globalepoch);
this->not_logged = true;
#ifdef MTAN
++ninsert_incll;
#endif
}else if(this->not_logged){
DBGLOG("log node insert to %p ge:%lu le:%lu",
(void*)this, globalepoch, this->loggedepoch)
this->not_logged=false;
#ifdef EXTLOG
GH::node_logger->record(this);
#endif //ln extlog incll
this->invalidate_cls();
#ifdef MTAN
is_recorded = true;
++nrecords_lf;
#endif //mtan
}
}
void save_cl1_2_update(int8_t p){
REC_ASSERT(p<width);
if(this->loggedepoch != globalepoch){
DBGLOG("save incll to %p update ge:%lu le:%lu keys:%d",
(void*)this, globalepoch, this->loggedepoch, this->number_of_keys())
if(p < KEY_MID){
lv_cl1.set_lv_idx_ge((uint64_t*)&this->lv_[p].value(), p);
}else{
lv_cl2.set_lv_idx_ge((uint64_t*)&this->lv_[p].value(), p-KEY_MID);
}
this->update_epochs(globalepoch);
this->not_logged = true;
#ifdef MTAN
++nupdate_incll;
#endif
}else if(this->not_logged){
DBGLOG("log node update to %p ge:%lu le:%lu",
(void*)this, globalepoch, this->loggedepoch)
this->not_logged=false;
#ifdef LN_EXTLOG_INCLL
GH::node_logger->record(this);
#endif //ln extlog incll
this->invalidate_cls();
#ifdef MTAN
is_recorded = true;
++nrecords_lf;
#endif //mtan
}
}
void fix_insert(){
if(this->inserting() && modstate_ == modstate_insert){
DBGLOG("fix_insert_state")
modstate_ = modstate_remove;
this->clear_insert();
}
}
void reset_state(){
this->clear_version_counter_bits();
modstate_ = modstate_insert;
this->clear_unlock();
}
void fix_all(){
DBGLOG("fix_state")
//this->fix_insert();
//this->fix_lock();
this->reset_state();
}
void undo_incll(){
undo_incll(failedepoch);
}
void undo_incll(mrcu_epoch_type fe){
//assume only one cacheline is active at a given time
if(this->loggedepoch == fe && this->cl0_idx != invalid_idx){
int i = GH::bucket_locks.lock(this);
recover_cl0();
recover_final();
GH::bucket_locks.unlock(i);
return;
}
if(lv_cl1.get_loggedepoch() == fe && lv_cl1.is_cl_valid()){
int i = GH::bucket_locks.lock(this);
recover_cl1();
recover_final();
GH::bucket_locks.unlock(i);
return;
}
if(lv_cl2.get_loggedepoch() == fe && lv_cl2.is_cl_valid()){
int i = GH::bucket_locks.lock(this);
recover_cl2();
recover_final();
GH::bucket_locks.unlock(i);
return;
}
}
void recover_final(){
DBGLOG("undo_incll")
this->invalidate_cls();
this->update_epochs(globalepoch-1);
}
inline void lazy_recovery(mrcu_epoch_type fe){
if(unlikely(this->loggedepoch <= fe)){
undo_incll(fe);
}
}
void update_epochs(){
this->loggedepoch = globalepoch;
lv_cl1.set_loggedepoch();
lv_cl2.set_loggedepoch();
}
void update_epochs(mrcu_epoch_type e){
this->loggedepoch = e;
lv_cl1.set_loggedepoch(e);
lv_cl2.set_loggedepoch(e);
}
inline void update_loggedepoch(){
this->loggedepoch = globalepoch;
}
inline void update_loggedepoch(mrcu_epoch_type e){
this->loggedepoch = e;
}
void invalidate_cls(){
DBGLOG("invalidate_cls")
this->cl0_idx = invalid_idx;
lv_cl1.invalidate_cl();
lv_cl2.invalidate_cl();
}
void print_cl0() const;
#else //incll
void record_node(){
if(this->loggedepoch != globalepoch){
DBGLOG("record leaf ge:%lu", globalepoch)
#ifdef LN_EXTLOG
GH::node_logger->record(this);
#endif //extlog
this->loggedepoch = globalepoch;
#ifdef MTAN
is_recorded = true;
++nrecords_lf;
#endif //mtan
}
}
#endif //incll
static leaf<P>* make(int ksufsize, phantom_epoch_type phantom_epoch, threadinfo& ti) {
size_t sz = iceil(sizeof(leaf<P>) + std::min(ksufsize, 128), 64);
void* ptr = ti.pool_allocate(sz, memtag_masstree_leaf);
leaf<P>* n = new(ptr) leaf<P>(sz, phantom_epoch);
assert(n);
if (P::debug_level > 0)
n->created_at_[0] = ti.operation_timestamp();
return n;
}
static leaf<P>* make_root(int ksufsize, leaf<P>* parent, threadinfo& ti) {
leaf<P>* n = make(ksufsize, parent ? parent->phantom_epoch() : phantom_epoch_type(), ti);
n->next_.ptr = n->prev_ = 0;
n->make_layer_root();
return n;
}
static size_t min_allocated_size() {
return (sizeof(leaf<P>) + 63) & ~size_t(63);
}
size_t allocated_size() const {
int es = (extrasize64_ >= 0 ? extrasize64_ : -extrasize64_ - 1);
return (sizeof(*this) + es * 64 + 63) & ~size_t(63);
}
phantom_epoch_type phantom_epoch() const {
return P::need_phantom_epoch ? phantom_epoch_[0] : phantom_epoch_type();
}
int size() const {
return permuter_type::size(permutation_);
}
permuter_type permutation() const {
return permuter_type(permutation_);
}
typename nodeversion_type::value_type full_version_value() const {
static_assert(int(nodeversion_type::traits_type::top_stable_bits) >= int(permuter_type::size_bits), "not enough bits to add size to version");
return (this->version_value() << permuter_type::size_bits) + size();
}
typename nodeversion_type::value_type full_unlocked_version_value() const {
static_assert(int(nodeversion_type::traits_type::top_stable_bits) >= int(permuter_type::size_bits), "not enough bits to add size to version");
typename node_base<P>::nodeversion_type v(*this);
if (v.locked())
// subtlely, unlocked_version_value() is different than v.unlock(); v.version_value() because the latter will add a
// split bit if we're doing a split. So we do the latter to get the fully correct version.
v.unlock();
return (v.version_value() << permuter_type::size_bits) + size();
}
using node_base<P>::has_changed;
bool has_changed(nodeversion_type oldv,
typename permuter_type::storage_type oldperm) const {
return this->has_changed(oldv) || oldperm != permutation_;
}
key_type get_key(int p) const {
int keylenx = keylenx_[p];
if (!keylenx_has_ksuf(keylenx))
return key_type(ikey0_[p], keylenx);
else
return key_type(ikey0_[p], ksuf(p));
}
ikey_type ikey(int p) const {
return ikey0_[p];
}
ikey_type ikey_bound() const {
return ikey0_[0];
}
int compare_key(const key_type& a, int bp) const {
return a.compare(ikey(bp), keylenx_[bp]);
}
inline int stable_last_key_compare(const key_type& k, nodeversion_type v,
threadinfo& ti) const;
inline leaf<P>* advance_to_key(const key_type& k, nodeversion_type& version,
threadinfo& ti) const;
leaf<P>* get_prev_safe(){
base_type *cn = this;
while(cn->has_parent()){
base_type *pn = cn->parent();
assert(!pn->isleaf());
internode_type *pin = pn->to_internode();
int idx = pin->find_child_idx(cn);
//case exists a prev node
if(idx>0){
//get the leaf to the most right
base_type *node_for_prev = pin->child_[idx-1];
//make sure the bound has an item, idx-1 = 0
if(node_for_prev){
leaf_type *ln = base_type::get_max_leaf(node_for_prev);
assert(ln != this);
return ln;
}
//if bound does not have an item go up to parent
}
//case no prev in this node
cn = pn;
}
//case no parent
return nullptr;
}
leaf<P>* get_next_safe(){
base_type *cn = this;
while(cn->has_parent()){
base_type *pn = cn->parent();
assert(!pn->isleaf());
internode_type *pin = pn->to_internode();
int idx = pin->find_child_idx(cn);
//case exists a prev node
if(idx<pin->size()){
//get the leaf to the most right
base_type *node_for_next = pin->child_[idx+1];
assert(node_for_next);
leaf_type *ln = base_type::get_min_leaf(node_for_next);
if(ln){
assert(ln != this);
return ln;
}
}
//case no prev in this node
cn = pn;
}
//case no parent
return nullptr;
}
static bool keylenx_is_layer(int keylenx) {