24 #include <system_error> 28 #include <glog/logging.h> 37 DEFINE_int32(num_threads, 12,
"num concurrent threads to test");
41 template <
typename ParentAlloc>
42 struct ParanoidArenaAlloc {
43 explicit ParanoidArenaAlloc(ParentAlloc& arena) : arena_(arena) {}
44 ParanoidArenaAlloc(ParanoidArenaAlloc
const&) =
delete;
45 ParanoidArenaAlloc(ParanoidArenaAlloc&&) =
delete;
46 ParanoidArenaAlloc& operator=(ParanoidArenaAlloc
const&) =
delete;
47 ParanoidArenaAlloc& operator=(ParanoidArenaAlloc&&) =
delete;
49 void* allocate(
size_t size) {
50 void* result = arena_.get().allocate(size);
51 allocated_.insert(result);
55 void deallocate(
void*
ptr,
size_t n) {
57 arena_.get().deallocate(ptr, n);
61 return allocated_.empty();
64 std::reference_wrapper<ParentAlloc> arena_;
65 std::set<void*> allocated_;
70 template <
typename ParentAlloc>
77 using namespace folly;
80 typedef int ValueType;
83 typedef SkipListType::Accessor SkipListAccessor;
85 typedef std::set<ValueType> SetType;
87 static const int kHeadHeight = 2;
90 static void randomAdding(
92 SkipListAccessor skipList,
94 int maxValue = kMaxValue) {
95 for (
int i = 0;
i <
size; ++
i) {
102 static void randomRemoval(
104 SkipListAccessor skipList,
106 int maxValue = kMaxValue) {
107 for (
int i = 0;
i <
size; ++
i) {
114 static void sumAllValues(SkipListAccessor skipList,
int64_t*
sum) {
116 FOR_EACH (it, skipList) { *sum += *it; }
117 VLOG(20) <<
"sum = " <<
sum;
120 static void concurrentSkip(
122 SkipListAccessor skipList) {
124 SkipListAccessor::Skipper skipper(skipList);
126 if (skipper.to(*it)) {
130 VLOG(20) <<
"sum = " <<
sum;
133 bool verifyEqual(SkipListAccessor skipList,
const SetType& verifier) {
134 EXPECT_EQ(verifier.size(), skipList.size());
136 CHECK(skipList.contains(*it)) << *it;
137 SkipListType::const_iterator iter = skipList.find(*it);
138 CHECK(iter != skipList.end());
141 EXPECT_TRUE(std::equal(verifier.begin(), verifier.end(), skipList.begin()));
147 LOG(
INFO) <<
"nodetype size=" <<
sizeof(SkipListNodeType);
149 auto skipList(SkipListType::create(kHeadHeight));
164 SkipListAccessor::Skipper skipper(skipList);
166 CHECK_EQ(3, *skipper);
176 auto ret = skipList.insert(9);
180 ret = skipList.insert(5);
194 CHECK(skipList.contains(2));
195 CHECK(skipList.contains(3));
196 CHECK(skipList.contains(5));
197 CHECK(skipList.contains(9));
198 CHECK(!skipList.contains(4));
201 auto it = skipList.lower_bound(5);
203 it = skipList.lower_bound(4);
205 it = skipList.lower_bound(9);
207 it = skipList.lower_bound(12);
210 it = skipList.begin();
214 SkipListAccessor::Skipper skipper(skipList);
219 CHECK(!skipper.to(7));
223 CHECK(skipper.to(9));
226 CHECK(!skipList.contains(3));
228 CHECK(skipList.contains(3));
230 for (
auto entry : skipList) {
231 LOG(
INFO) <<
"pos= " << pos++ <<
" value= " << entry;
236 auto skipList(SkipListType::create(kHeadHeight));
239 randomAdding(10000, skipList, &verifier);
240 verifyEqual(skipList, verifier);
243 SkipListAccessor::Skipper skipper(skipList);
244 int num_skips = 1000;
245 for (
int i = 0;
i < num_skips; ++
i) {
246 int n =
i * kMaxValue / num_skips;
247 bool found = skipper.to(n);
248 EXPECT_EQ(found, (verifier.find(n) != verifier.end()));
255 for (
int j = 0; j < len; j++) {
256 s.push_back((rand() % 26) +
'A');
263 std::shared_ptr<SkipListT>
skip = SkipListT::createInstance();
264 SkipListT::Accessor accessor(skip);
266 for (
int i = 0;
i < 100000;
i++) {
271 EXPECT_TRUE(std::is_sorted(accessor.begin(), accessor.end()));
274 struct UniquePtrComp {
275 bool operator()(
const std::unique_ptr<int>&
x,
const std::unique_ptr<int>&
y)
290 auto sl = SkipListT::createInstance();
291 SkipListT::Accessor accessor(sl);
293 static const int N = 10;
294 for (
int i = 0;
i < N; ++
i) {
295 accessor.insert(std::make_unique<int>(
i));
298 for (
int i = 0;
i < N; ++
i) {
300 accessor.find(std::unique_ptr<int>(
new int(
i))) != accessor.end());
303 accessor.find(std::unique_ptr<int>(
new int(N))) == accessor.end());
306 void testConcurrentAdd(
int numThreads) {
307 auto skipList(SkipListType::create(kHeadHeight));
312 for (
int i = 0;
i < numThreads; ++
i) {
314 std::thread(&randomAdding, 100, skipList, &verifiers[
i], kMaxValue));
316 }
catch (
const std::system_error& e) {
318 << threads.size() <<
" threads out of " << numThreads;
320 for (
size_t i = 0;
i < threads.size(); ++
i) {
325 FOR_EACH (
s, verifiers) { all.insert(
s->begin(),
s->end()); }
326 verifyEqual(skipList, all);
331 for (
int numThreads = 10; numThreads < 10000; numThreads += 1000) {
332 testConcurrentAdd(numThreads);
336 void testConcurrentRemoval(
int numThreads,
int maxValue) {
337 auto skipList = SkipListType::create(kHeadHeight);
338 for (
int i = 0;
i < maxValue; ++
i) {
345 for (
int i = 0;
i < numThreads; ++
i) {
347 std::thread(&randomRemoval, 100, skipList, &verifiers[
i], maxValue));
349 }
catch (
const std::system_error& e) {
351 << threads.size() <<
" threads out of " << numThreads;
356 FOR_EACH (
s, verifiers) { all.insert(
s->begin(),
s->end()); }
358 CHECK_EQ(maxValue, all.size() + skipList.size());
359 for (
int i = 0;
i < maxValue; ++
i) {
360 if (all.find(
i) != all.end()) {
361 CHECK(!skipList.contains(
i)) <<
i;
363 CHECK(skipList.contains(
i)) <<
i;
369 for (
int numThreads = 10; numThreads < 1000; numThreads += 100) {
370 testConcurrentRemoval(numThreads, 100 * numThreads);
375 testConcurrentAccess(
int numInsertions,
int numDeletions,
int maxValue) {
376 auto skipList = SkipListType::create(kHeadHeight);
382 for (
int i = 0;
i < FLAGS_num_threads; ++
i) {
383 for (
int j = 0; j < numInsertions; ++j) {
384 skipValues[
i].push_back(rand() % (maxValue + 1));
386 std::sort(skipValues[
i].
begin(), skipValues[
i].
end());
390 for (
int i = 0;
i < FLAGS_num_threads; ++
i) {
394 threads.push_back(std::thread(
395 randomAdding, numInsertions, skipList, &verifiers[
i], maxValue));
398 threads.push_back(std::thread(
399 randomRemoval, numDeletions, skipList, &verifiers[i], maxValue));
403 std::thread(concurrentSkip, &skipValues[i], skipList));
406 threads.push_back(std::thread(sumAllValues, skipList, &sums[i]));
416 testConcurrentAccess(10000, 100, kMaxValue);
417 testConcurrentAccess(100000, 10000, kMaxValue * 10);
418 testConcurrentAccess(1000000, 100000, kMaxValue);
421 struct NonTrivialValue {
422 static std::atomic<int> InstanceCounter;
423 static const int kBadPayLoad;
425 NonTrivialValue() : payload_(kBadPayLoad) {
429 explicit NonTrivialValue(
int payload) : payload_(payload) {
433 NonTrivialValue(
const NonTrivialValue&
rhs) : payload_(rhs.payload_) {
437 NonTrivialValue& operator=(
const NonTrivialValue& rhs) {
438 payload_ = rhs.payload_;
446 bool operator<(
const NonTrivialValue& rhs)
const {
449 return payload_ < rhs.payload_;
456 std::atomic<int> NonTrivialValue::InstanceCounter(0);
457 const int NonTrivialValue::kBadPayLoad = 0xDEADBEEF;
459 template <
typename SkipListPtrType>
460 void TestNonTrivialDeallocation(SkipListPtrType&
list) {
462 auto accessor =
typename SkipListPtrType::element_type::Accessor(list);
463 static const size_t N = 10000;
464 for (
size_t i = 0;
i < N; ++
i) {
465 accessor.add(NonTrivialValue(
i));
469 EXPECT_EQ(0, NonTrivialValue::InstanceCounter);
472 template <
typename ParentAlloc>
473 void NonTrivialDeallocationWithParanoid(ParentAlloc& parentAlloc) {
474 using ParanoidAlloc = ParanoidArenaAlloc<ParentAlloc>;
476 using ParanoidSkipListType =
478 ParanoidAlloc paranoidAlloc(parentAlloc);
479 Alloc alloc(paranoidAlloc);
480 auto list = ParanoidSkipListType::createInstance(10, alloc);
481 TestNonTrivialDeallocation(list);
487 NonTrivialDeallocationWithParanoid(alloc);
493 NonTrivialDeallocationWithParanoid(alloc);
499 std::less<NonTrivialValue>,
502 SysArenaAllocator<void> alloc(arena);
503 auto list = SysArenaSkipListType::createInstance(10, alloc);
504 TestNonTrivialDeallocation(list);
511 google::InitGoogleLogging(argv[0]);
512 gflags::ParseCommandLineFlags(&argc, &argv,
true);
#define TEST(test_case_name, test_name)
std::atomic< int64_t > sum(0)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_EQ(val1, val2)
fbstring exceptionStr(const std::exception &e)
auto begin(TestAdlIterable &instance)
—— Concurrent Priority Queue Implementation ——
detail::Skip skip(size_t count)
FOLLY_PUSH_WARNING RHS rhs
std::vector< std::thread::id > threads
constexpr auto size(C const &c) -> decltype(c.size())
constexpr detail::IsEmpty< true > isEmpty
auto end(TestAdlIterable &instance)
Encoder::MutableCompressedList list
DEFINE_int32(num_threads, 12,"num concurrent threads to test")
#define EXPECT_TRUE(condition)
static const int kMaxValue
#define EXPECT_NE(val1, val2)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
int main(int argc, char *argv[])
#define EXPECT_FALSE(condition)
std::enable_if< IsLessThanComparable< Value >::value, bool >::type operator<(const Expected< Value, Error > &lhs, const Expected< Value, Error > &rhs)
std::vector< int > values(1'000)
Composed all(Predicate pred=Predicate())