-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlt.cc
More file actions
5782 lines (4702 loc) · 166 KB
/
lt.cc
File metadata and controls
5782 lines (4702 loc) · 166 KB
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
// Alex Horn, University of Oxford
//
// The source code is structured into three main parts:
//
// 1) Data structures and algorithm of the linearizability tester,
// including a new optional partitioning algorithm;
//
// 2) Immutable data types for sets, registers and stacks with
// efficient equality checks;
//
// 3) Unit tests and experiments with TBB, EMBB, and etcd.
#include <thread>
#include <atomic>
#include <mutex>
#include <tuple>
#include <memory>
#include <vector>
#include <climits>
#include <cstddef>
#include <utility>
#include <cassert>
#include <type_traits>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <string>
#include <stdexcept>
#include <list>
// functional testing and experiments
#include <random>
#include <functional>
#include <iostream>
/// Allow users to print out counterexamples
#define _LT_DEBUG_
/// Built-in timeout feature
#define _LT_TIMEOUT_
#ifdef _LT_DEBUG_
#include <string>
#include <ostream>
#include <sstream>
#include <string>
#include <algorithm>
#endif
#ifdef _ENABLE_EMBB_
#include <embb/base/thread.h>
#include <embb/containers/lock_free_stack.h>
#endif
#ifdef _ENABLE_TBB_
#include "tbb/concurrent_unordered_set.h"
#endif
#ifdef __linux__
#define _LT_MEM_USAGE_
#endif
#ifdef _LT_TIMEOUT_
#include <chrono>
#endif
#ifdef _LT_MEM_USAGE_
#include <unistd.h>
#include <ios>
#include <iostream>
#include <algorithm>
#endif
#if __cplusplus <= 201103L
// since C++14 in std, see Herb Sutter's blog
template<class T, class ...Args>
std::unique_ptr<T> make_unique(Args&& ...args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
#else
using std::make_unique;
#endif
/// Linearizability tester
namespace lt
{
/************* Core data structures and algorithms *************/
template<class S>
class Entry;
/// Doubly-linked list of log entries
/// S - sequential data type
template<class S>
using EntryPtr = Entry<S>*;
/// Bounded stack of call entries that have been linearized
/// S - sequential data type
template<class S>
class Stack
{
private:
typedef std::tuple<EntryPtr<S>, S> Pair;
typedef std::vector<Pair> Pairs;
typedef typename Pairs::size_type SizeType;
// A constant-size vector
Pairs m_vector;
SizeType m_top;
public:
/// Create a new stack of bounded height
/// \post: if capacity is positive, then not is_full()
Stack(SizeType capacity)
: m_vector(capacity), m_top{0U}
{
assert(capacity == 0U or not is_full());
}
/// History length in the stack
SizeType size() const noexcept
{
return m_top;
}
/// Is size() zero?
bool is_empty() const noexcept
{
return 0U == size();
}
/// Is size() equal to the stack's capacity?
bool is_full() const noexcept
{
return m_top == m_vector.size();
}
/// \pre: not is_empty()
const Pair& top() const noexcept
{
assert(not is_empty());
return m_vector[m_top - 1U];
}
/// Add an entry to the top() of the stack
/// \pre: not is_full()
/// \pre: ptr->is_call()
void push(EntryPtr<S>, S&&);
/// Remove count entries from the stack
/// \pre: 0 < count <= size()
void pop(unsigned count = 1U)
{
assert(0U < count);
assert(count <= size());
m_top -= count;
}
/// \internal
EntryPtr<S> entry_ptr(std::size_t pos)
{
assert(pos < m_top);
return std::get<0>(m_vector[pos]);
}
};
enum class Option : unsigned char
{
NEVER_CACHE,
LRU_CACHE,
ALWAYS_CACHE,
};
template<class S> class Entry;
template<class S> class Log;
template<class S> class ConcurrentLog;
template<class S> class Slicer;
template<class S, Option> class LinearizabilityTester;
/// A kind of "functor" in C++ terminology
/// S - sequential data type
template<class S>
class Op
{
private:
friend class Entry<S>;
// Is m_partition defined?
bool m_is_partitionable;
unsigned m_partition;
// modified by Entry
unsigned ref_counter;
#ifdef _LT_DEBUG_
virtual std::ostream& print(std::ostream&) const = 0;
#endif
virtual std::pair<bool, S> internal_apply(const S&, const Op<S>&)
{
return {};
}
public:
Op()
: m_is_partitionable{false},
m_partition{0U},
ref_counter{0U} {}
Op(unsigned partition)
: m_is_partitionable{true},
m_partition{partition},
ref_counter{0U} {}
Op(bool is_partitionable, unsigned partition)
: m_is_partitionable{is_partitionable},
m_partition{partition},
ref_counter{0U} {}
virtual ~Op()
{
assert(ref_counter == 0);
}
/// Is partition() defined?
bool is_partitionable() const noexcept
{
return m_is_partitionable;
}
/// \pre: is_partitionable()
unsigned partition() const
{
assert(m_is_partitionable);
return m_partition;
}
/// Returns true exactly if the operation could be applied
std::pair<bool, S> apply(const S& s, const Op<S>& op)
{
return internal_apply(s, op);
}
#ifdef _LT_DEBUG_
friend std::ostream& operator<<(std::ostream& os, const Op& op)
{
return op.print(os);
}
#endif
};
/// Fixed-size set of bits with persistence features
class Bitset
{
public:
typedef std::size_t Pos;
private:
friend struct BitsetHash;
friend class FlexibleBitset;
typedef unsigned long Block;
typedef std::vector<Block> Blocks;
typedef Blocks::size_type BlockIndex;
/// Accessible bits in a Block
typedef unsigned BlockWidth;
static constexpr BlockWidth s_bits_per_block =
static_cast<BlockWidth>(sizeof(Block) * CHAR_BIT);
static BlockIndex block_index(Pos pos) noexcept
{
return pos / s_bits_per_block;
}
static BlockIndex blocks_size(Pos max_pos) noexcept
{
return block_index(max_pos) + 1U;
}
static BlockWidth bit_index(Pos pos) noexcept
{
return static_cast<BlockWidth>(pos % s_bits_per_block);
}
static Block bit_mask(Pos pos) noexcept
{
return Block(1U) << bit_index(pos);
}
/// only resized by FlexibleBitset
Blocks m_blocks;
std::size_t m_hash;
unsigned m_number_of_set_bits;
Block& find_block(Pos pos)
{
BlockIndex i{block_index(pos)};
assert(i < m_blocks.size());
return m_blocks[i];
}
// We exploit the fact that XOR forms an abelian group:
// first, clear hash of old block; then, hash new block.
void update_hash(Block old_block, Block new_block)
{
m_hash ^= old_block;
m_hash ^= new_block;
}
public:
Bitset(Pos max_pos)
: m_blocks(blocks_size(max_pos)),
m_hash{0U},
m_number_of_set_bits{0U} {}
bool is_empty() const noexcept
{
return m_number_of_set_bits == 0U;
}
bool set(Pos pos)
{
Block& block = find_block(pos);
const Block copy_block{block};
block |= bit_mask(pos);
update_hash(copy_block, block);
bool ok{block != copy_block};
m_number_of_set_bits += ok;
return ok;
}
Bitset immutable_set(Pos pos) const
{
Bitset copy{*this};
copy.set(pos);
return copy;
}
bool is_set(Pos pos) const
{
BlockIndex i{block_index(pos)};
if (i < m_blocks.size())
return (m_blocks[i] & bit_mask(pos)) != 0U;
return false;
}
bool reset(Pos pos)
{
Block& block = find_block(pos);
const Block copy_block{block};
block &= ~bit_mask(pos);
update_hash(copy_block, block);
bool ok{block != copy_block};
m_number_of_set_bits -= ok;
return ok;
}
Bitset immutable_reset(Pos pos) const
{
Bitset copy{*this};
copy.reset(pos);
return copy;
}
// Same number of blocks and identical bits in all those blocks?
bool operator==(const Bitset& other) const noexcept
{
return m_number_of_set_bits == other.m_number_of_set_bits and
m_blocks == other.m_blocks;
}
bool operator!=(const Bitset& other) const noexcept
{
return m_number_of_set_bits != other.m_number_of_set_bits or
m_blocks != other.m_blocks;
}
};
/// Constant-time, O(1), hash function
struct BitsetHash
{
std::size_t operator()(const Bitset& bitset) const noexcept
{
return bitset.m_hash;
}
};
/// States of abstract data types
namespace state
{
template<class T>
struct Hash
{
std::size_t operator()(const T&) const noexcept;
};
}
template<class S>
using OpPtr = std::unique_ptr<Op<S>>;
/// Call/ret log entry
/// S - sequential data type
template<class S>
class Entry
{
private:
friend class Log<S>;
friend class Slicer<S>;
friend class LinearizabilityTester<S, Option::NEVER_CACHE>;
friend class LinearizabilityTester<S, Option::LRU_CACHE>;
friend class LinearizabilityTester<S, Option::ALWAYS_CACHE>;
// Ref counted pointer because we need to copy logs so that we
// can experimentally compare different linearizability testers
//
// However, this is an implementation detail and the strict type
// of OpPtr<S> enforces at compile-time that we manage the
// ownership of these kind of pointers on the user's behalf.
Op<S>* m_op_ptr;
unsigned m_entry_id;
std::thread::id m_thread_id;
EntryPtr<S> m_match;
bool m_is_call;
void inc_ref_counter() const noexcept
{
if (m_op_ptr != nullptr)
++m_op_ptr->ref_counter;
}
void dec_ref_counter() const
{
assert(m_op_ptr == nullptr or 0 < m_op_ptr->ref_counter);
if (m_op_ptr != nullptr and --m_op_ptr->ref_counter == 0)
delete m_op_ptr;
}
/// Log head
/// \post: if _next is not nullptr, then _next->prev == this
Entry(EntryPtr<S> _next)
: m_op_ptr{nullptr},
m_entry_id{},
m_thread_id{},
m_match{nullptr},
m_is_call{false},
prev{nullptr},
next{_next}
{
if (_next != nullptr)
_next->prev = this;
}
public:
~Entry()
{
dec_ref_counter();
}
EntryPtr<S> prev;
EntryPtr<S> next;
Entry()
: m_op_ptr{nullptr},
m_entry_id{},
m_thread_id{},
m_match{nullptr},
m_is_call{false},
prev{nullptr},
next{nullptr} {}
Entry(const Entry& entry)
: m_op_ptr{entry.m_op_ptr},
m_entry_id{entry.m_entry_id},
m_thread_id{entry.m_thread_id},
m_match{entry.m_match},
m_is_call{entry.m_is_call},
prev{entry.prev},
next{entry.next}
{
inc_ref_counter();
}
Entry& operator=(const Entry& entry)
{
entry.inc_ref_counter();
dec_ref_counter();
m_op_ptr = entry.m_op_ptr;
m_entry_id = entry.m_entry_id;
m_thread_id = entry.m_thread_id;
m_match = entry.m_match;
m_is_call = entry.m_is_call;
prev = entry.prev;
next = entry.next;
return *this;
}
Entry& operator=(Entry&& entry)
{
// only decrement required (due to move semantics)
dec_ref_counter();
m_op_ptr = entry.m_op_ptr;
m_entry_id = entry.m_entry_id;
m_thread_id = entry.m_thread_id;
m_match = entry.m_match;
m_is_call = entry.m_is_call;
prev = entry.prev;
next = entry.next;
entry.m_op_ptr = nullptr;
entry.m_entry_id = 0;
entry.m_thread_id = 0;
entry.m_match = nullptr;
entry.m_is_call = false;
entry.prev = nullptr;
entry.next = nullptr;
return *this;
}
/// \pre: set_match and set_op have been called with non-null arguments
bool is_partitionable() const
{
assert(m_match != nullptr);
assert(m_match->m_op_ptr != nullptr);
assert(m_op_ptr->m_is_partitionable == m_match->m_op_ptr->m_is_partitionable);
assert(m_op_ptr->m_partition == m_match->m_op_ptr->m_partition);
return m_op_ptr->m_is_partitionable;
}
void set_op(OpPtr<S>&& op_ptr) noexcept
{
m_op_ptr = op_ptr.release();
inc_ref_counter();
}
Op<S>& op() const
{
assert(m_op_ptr != nullptr);
return *m_op_ptr;
}
const Op<S>* const op_ptr() const noexcept
{
return m_op_ptr;
}
void set_entry_id(unsigned entry_id) noexcept
{
m_entry_id = entry_id;
}
unsigned entry_id() const noexcept
{
return m_entry_id;
}
void set_thread_id(std::thread::id thread_id) noexcept
{
m_thread_id = thread_id;
}
std::thread::id thread_id() const noexcept
{
return m_thread_id;
}
/// \pre: ret_entry_ptr->match() == nullptr
/// \pre: not ret_entry_ptr->is_call()
///
/// \post: this->is_call()
/// \post: this == ret_entry_ptr->match()
/// \post: this->match() == ret_entry_ptr
/// \post: this->entry_id() == ret_entry_ptr->entry_id()
/// \post: if this->is_partitionable() or ret_entry_ptr->is_partitionable(),
/// then this->op().partition() == ret_entry_ptr->op().partition()
void set_match(EntryPtr<S> ret_entry_ptr) noexcept
{
assert(ret_entry_ptr->m_match == nullptr);
assert(not ret_entry_ptr->is_call());
ret_entry_ptr->m_match = this;
ret_entry_ptr->set_entry_id(m_entry_id);
if (ret_entry_ptr->op().m_is_partitionable)
{
op().m_is_partitionable = ret_entry_ptr->op().m_is_partitionable;
op().m_partition = ret_entry_ptr->op().m_partition;
}
else
{
ret_entry_ptr->op().m_is_partitionable = op().m_is_partitionable;
ret_entry_ptr->op().m_partition = op().m_partition;
}
m_match = ret_entry_ptr;
m_is_call = true;
assert(is_call());
assert(this == ret_entry_ptr->match());
assert(match() == ret_entry_ptr);
assert(entry_id() == ret_entry_ptr->entry_id());
assert(op().m_is_partitionable == ret_entry_ptr->op().m_is_partitionable);
assert(op().m_partition == ret_entry_ptr->op().m_partition);
}
EntryPtr<S> match() const noexcept
{
return m_match;
}
bool is_call() const noexcept
{
return m_is_call;
}
};
#ifdef _LT_DEBUG_
/// S - sequential data type
template<class S>
std::ostream& operator<<(std::ostream& os, EntryPtr<S> entry_ptr)
{
if (entry_ptr == nullptr)
return os << "entry id: none, thread id: none [nullptr]";
const Entry<S>& entry = *entry_ptr;
return os <<
"entry id: " << entry.entry_id() <<
", thread id: " << entry.thread_id() <<
", " << (entry.is_call() ? "call: " : "return: ") <<
entry.op();
}
#endif
template<class S>
void Stack<S>::push(EntryPtr<S> ptr, S&& s)
{
assert(not is_full());
assert(ptr != nullptr);
assert(ptr->is_call());
// no overflow
m_vector[m_top++] = std::make_pair(ptr, std::move(s));
assert(0U != m_top);
}
/// Input to linearizabilty testers
/// S - sequential data type
template<class S>
class LogInfo
{
private:
friend class Slicer<S>;
EntryPtr<S> m_log_head_ptr;
std::size_t m_number_of_entries;
public:
/// \post: is_empty()
LogInfo() : m_log_head_ptr{nullptr}, m_number_of_entries{0U} {}
/// \pre: number_of_entries is positive and even
/// \pre: log_head_ptr is not nullptr
/// \post: not is_empty()
LogInfo(EntryPtr<S> log_head_ptr, std::size_t number_of_entries)
: m_log_head_ptr{log_head_ptr}, m_number_of_entries{number_of_entries}
{
assert(log_head_ptr != nullptr);
assert(0U < m_number_of_entries);
assert((m_number_of_entries & 1U) == 0U);
}
/// Ptr to the first entry in the log
EntryPtr<S> log_head_ptr() const noexcept
{
return m_log_head_ptr;
}
/// Total number of call entries plus return entries.
/// Returns even number since every call is paired with a return
std::size_t number_of_entries() const noexcept
{
return m_number_of_entries;
}
bool is_empty() const noexcept
{
return m_log_head_ptr == nullptr and m_number_of_entries == 0U;
}
};
#ifdef _LT_DEBUG_
/// S - sequential data type
template<class S>
std::ostream& operator<<(std::ostream& os, const LogInfo<S>& log_info)
{
EntryPtr<S> entry_ptr{log_info.log_head_ptr()};
os << "log info, number of entries: " << log_info.number_of_entries() << std::endl;
for (; entry_ptr != nullptr; entry_ptr = entry_ptr->next)
os << entry_ptr << std::endl;
return os;
}
#endif
/// Bounded history log
/// If you need thread-safety, use ConcurrentLog<S> instead.
///
/// S - sequential data type
template<class S>
class Log
{
private:
// fixed-size vector
typedef std::vector<Entry<S>> Entries;
public:
typedef typename Entries::size_type Size;
private:
// we never resize the vector and so pointers into it are stable
Size m_entry_id, m_index;
Entries m_entries;
EntryPtr<S> m_last_entry_ptr;
void link(Entry<S>& entry) noexcept
{
if (m_last_entry_ptr != nullptr)
m_last_entry_ptr->next = &entry;
entry.prev = m_last_entry_ptr;
}
public:
Log(const Log&) = delete;
/// A history with at most capacity entries
Log(Size capacity)
: m_entry_id{0U},
m_index{0U},
m_entries(capacity),
m_last_entry_ptr{nullptr} {}
/// Copy entries
Log(LogInfo<S> log_info)
: m_entry_id{0U},
m_index{0U},
m_entries(log_info.number_of_entries()),
m_last_entry_ptr{nullptr}
{
EntryPtr<S> entry_ptr{log_info.log_head_ptr()};
std::vector<unsigned> matches(log_info.number_of_entries() >> 1);
while (entry_ptr != nullptr)
{
assert(m_index < m_entries.size());
Entry<S>& new_entry = m_entries[m_index];
new_entry = *entry_ptr;
new_entry.m_match = nullptr;
link(new_entry);
if (new_entry.is_call())
{
matches[new_entry.entry_id()] = m_index;
}
else
{
Entry<S>& call_entry = m_entries[matches[new_entry.entry_id()]];
call_entry.set_match(&new_entry);
}
m_last_entry_ptr = &new_entry;
entry_ptr = entry_ptr->next;
++m_index;
}
assert(m_index == m_entries.size());
assert(entry_ptr == nullptr);
}
EntryPtr<S> add_call(OpPtr<S>&& op_ptr)
{
assert(m_index < m_entries.size());
Entry<S>& entry = m_entries[m_index++];
entry.set_op(std::move(op_ptr));
entry.set_entry_id(m_entry_id++);
link(entry);
m_last_entry_ptr = &entry;
return m_last_entry_ptr;
}
/// \post: call_entry_ptr->is_call()
EntryPtr<S> add_ret(EntryPtr<S> call_entry_ptr, OpPtr<S>&& op_ptr)
{
assert(m_index < m_entries.size());
Entry<S>& entry = m_entries[m_index++];
entry.set_op(std::move(op_ptr));
link(entry);
m_last_entry_ptr = &entry;
call_entry_ptr->set_match(m_last_entry_ptr);
assert(call_entry_ptr->is_call());
assert(m_entry_id <= m_index);
return m_last_entry_ptr;
}
EntryPtr<S> log_head_ptr()
{
return &m_entries.front();
}
/// Total number of call entries plus return entries.
/// Returns even number since every call is paired with a return
std::size_t number_of_entries() const noexcept
{
return m_index;
}
LogInfo<S> info()
{
return {log_head_ptr(), number_of_entries()};
}
};
/// Output of linearizability tester
/// S - sequential data type
template<class S>
class Result
{
private:
friend class LinearizabilityTester<S, Option::NEVER_CACHE>;
friend class LinearizabilityTester<S, Option::LRU_CACHE>;
friend class LinearizabilityTester<S, Option::ALWAYS_CACHE>;
typedef std::vector<EntryPtr<S>> EntryPtrs;
bool m_is_linearizable;
EntryPtrs m_entry_ptrs;
#ifdef _LT_DEBUG_
unsigned m_cutoff_entry_id;
EntryPtr<S> m_log_head_ptr;
#endif
bool m_is_timeout;
double m_virtual_memory_usage;
double m_resident_set_size;
void reset()
{
m_is_linearizable = true;
m_entry_ptrs.clear();
#ifdef _LT_DEBUG_
m_cutoff_entry_id = 0U;
m_log_head_ptr = nullptr;
#endif
m_is_timeout = false;
m_virtual_memory_usage = 0.0;
m_resident_set_size = 0.0;
}
public:
/// Initially linearizable
Result()
: m_is_linearizable{true},
m_entry_ptrs{},
#ifdef _LT_DEBUG_
m_cutoff_entry_id{0U},
m_log_head_ptr{nullptr},
#endif
m_is_timeout{false},
m_virtual_memory_usage{0.0},
m_resident_set_size{0.0} {}
/// \pre: not is_timeout()
bool is_linearizable() const noexcept
{
assert(not is_timeout());
return m_is_linearizable;
}
bool is_timeout() const noexcept
{
return m_is_timeout;
}
/// Zero if unknown, unit: MiB
double virtual_memory_usage() const noexcept
{
return m_virtual_memory_usage;
}
/// Zero if unknown, unit: MiB
double resident_set_size() const noexcept
{
return m_resident_set_size;
}
#ifdef _LT_DEBUG_
void debug(std::ostream& os, bool verbose = false)
{
os << "Linearizable: ";
if (m_is_linearizable)
{
os << "Yes" << std::endl;
for (EntryPtr<S> entry_ptr : m_entry_ptrs)
os << entry_ptr << " : " << entry_ptr->match() << std::endl;
return;
}
os << "No" << std::endl;
EntryPtr<S> entry_ptr{m_log_head_ptr};
for (; entry_ptr != nullptr; entry_ptr = entry_ptr->next)
{
os << entry_ptr << std::endl;
if (entry_ptr->entry_id() == m_cutoff_entry_id)
{
os << "^ previous entries cannot be linearized" << std::endl;
if (not (verbose or entry_ptr->is_call()))
return;
}
}
}
#endif
};
#ifdef _LT_TIMEOUT_
template <typename Clock = std::chrono::steady_clock>
struct Timeout
{
const typename Clock::time_point start_time;
const typename Clock::duration max_duration;
Timeout()
: start_time{Clock::now()},
max_duration{Clock::duration::max()} {}
Timeout(typename Clock::duration duration)
: start_time{Clock::now()},
max_duration{duration} {}
bool is_expired() const
{
return max_duration < (Clock::now() - start_time);
}
};
#endif
/// Least-recently used cache eviction
template<class Key, class Hash = std::hash<Key>>
class LruCache
{
private:
typedef std::list<Key> List;
typedef typename List::iterator ListIterator;
typedef std::unordered_map<Key, ListIterator, Hash> UnorderedMap;
typedef typename UnorderedMap::size_type Capacity;
const Capacity m_capacity;
UnorderedMap m_unordered_map;
List m_list;