File: concurrent_map.cpp

package info (click to toggle)
libpmemobj-cpp 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,388 kB
  • sloc: cpp: 136,076; sh: 1,022; perl: 381; ansic: 163; makefile: 13
file content (252 lines) | stat: -rw-r--r-- 5,625 bytes parent folder | download
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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */

/*
 * concurrent_map.cpp -- pmem::obj::experimental::concurrent_map test
 *
 */

#include "thread_helpers.hpp"
#include "unittest.hpp"

#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>

#include <iterator>
#include <thread>
#include <vector>

#include <libpmemobj++/container/string.hpp>
#include <libpmemobj++/experimental/concurrent_map.hpp>

#define LAYOUT "concurrent_map"

namespace nvobj = pmem::obj;

namespace
{

struct hetero_less {
	using is_transparent = void;
	template <typename T1, typename T2>
	bool
	operator()(const T1 &lhs, const T2 &rhs) const
	{
		return lhs < rhs;
	}
};

typedef nvobj::experimental::concurrent_map<nvobj::p<int>, nvobj::p<int>>
	persistent_map_type_int;

typedef nvobj::experimental::concurrent_map<nvobj::string, nvobj::string,
					    hetero_less>
	persistent_map_type_string;

struct root {
	nvobj::persistent_ptr<persistent_map_type_int> cons1;
	nvobj::persistent_ptr<persistent_map_type_string> cons2;
};

std::string
gen_key(persistent_map_type_string &, int i)
{
	return std::to_string(i);
}

int
gen_key(persistent_map_type_int &, int i)
{
	return i;
}

template <typename MapType>
void
check_sorted(MapType *map)
{
	using value_type = typename MapType::value_type;
	UT_ASSERT(std::is_sorted(
		map->begin(), map->end(),
		[](const value_type &lhs, const value_type &rhs) {
			return lhs.first < rhs.first;
		}));
}

/*
 * emplace_and_lookup_test -- (internal) test emplace and lookup operations
 */
template <typename MapType>
void
emplace_and_lookup_test(nvobj::pool<root> &pop, MapType *map)
{
	const size_t NUMBER_ITEMS_INSERT = 50;

	// Adding more concurrency will increase DRD test time
	const size_t concurrency = 8;

	size_t TOTAL_ITEMS = NUMBER_ITEMS_INSERT * concurrency;

	UT_ASSERT(map != nullptr);

	map->runtime_initialize();

	parallel_exec(concurrency, [&](size_t thread_id) {
		int begin = thread_id * NUMBER_ITEMS_INSERT;
		int end = begin + int(NUMBER_ITEMS_INSERT);
		for (int i = begin; i < end; ++i) {
			auto ret = map->emplace(gen_key(*map, i),
						gen_key(*map, i));
			UT_ASSERT(ret.second == true);

			UT_ASSERT(map->count(gen_key(*map, i)) == 1);

			typename MapType::iterator it =
				map->find(gen_key(*map, i));
			UT_ASSERT(it != map->end());
			UT_ASSERT(it->first == gen_key(*map, i));
			UT_ASSERT(it->second == gen_key(*map, i));
		}

		for (int i = begin; i < end; ++i) {
			typename MapType::const_iterator it =
				map->find(gen_key(*map, i));
			UT_ASSERT(it != map->end());
			UT_ASSERT(it->first == gen_key(*map, i));
			UT_ASSERT(it->second == gen_key(*map, i));
		}
	});

	check_sorted(map);

	UT_ASSERT(map->size() == TOTAL_ITEMS);

	UT_ASSERT(std::distance(map->begin(), map->end()) == int(TOTAL_ITEMS));

	check_sorted(map);

	map->runtime_initialize();

	UT_ASSERT(map->size() == TOTAL_ITEMS);

	map->runtime_initialize();

	UT_ASSERT(map->size() == TOTAL_ITEMS);

	map->clear();

	UT_ASSERT(map->size() == 0);

	UT_ASSERT(std::distance(map->begin(), map->end()) == 0);
}
/*
 * emplace_and_lookup_duplicates_test -- (internal) test emplace and lookup
 * operations with duplicates
 */
template <typename MapType>
void
emplace_and_lookup_duplicates_test(nvobj::pool<root> &pop, MapType *map)
{
	const size_t NUMBER_ITEMS_INSERT = 50;

	// Adding more concurrency will increase DRD test time
	const size_t concurrency = 4;

	UT_ASSERT(map != nullptr);

	map->runtime_initialize();

	std::vector<std::thread> threads;
	threads.reserve(concurrency * 2);

	for (size_t i = 0; i < concurrency; ++i) {
		threads.emplace_back([&]() {
			for (int i = 0;
			     i < static_cast<int>(NUMBER_ITEMS_INSERT); ++i) {
				map->emplace(gen_key(*map, i),
					     gen_key(*map, i));
			}
		});
	}

	for (size_t i = 0; i < concurrency; ++i) {
		threads.emplace_back([&]() {
			for (int i = 0;
			     i < static_cast<int>(NUMBER_ITEMS_INSERT); ++i) {
				auto it = map->find(gen_key(*map, i));

				if (it != map->end()) {
					UT_ASSERT(it->first ==
						  gen_key(*map, i));
					UT_ASSERT(it->second ==
						  gen_key(*map, i));
				}
			}
		});
	}

	for (auto &t : threads) {
		t.join();
	}

	check_sorted(map);

	for (auto &e : *map) {
		UT_ASSERT(e.first == e.second);
	}

	UT_ASSERT(map->size() == NUMBER_ITEMS_INSERT);

	UT_ASSERT(std::distance(map->begin(), map->end()) ==
		  static_cast<int>(NUMBER_ITEMS_INSERT));

	check_sorted(map);
}
}

static void
test(int argc, char *argv[])
{
	if (argc < 2) {
		UT_FATAL("usage: %s file-name", argv[0]);
	}

	const char *path = argv[1];

	nvobj::pool<root> pop;

	try {
		pop = nvobj::pool<root>::create(
			path, LAYOUT, PMEMOBJ_MIN_POOL * 20, S_IWUSR | S_IRUSR);
		nvobj::transaction::run(pop, [&] {
			pop.root()->cons1 = nvobj::make_persistent<
				persistent_map_type_int>();
			pop.root()->cons2 = nvobj::make_persistent<
				persistent_map_type_string>();
		});
	} catch (pmem::pool_error &pe) {
		UT_FATAL("!pool::create: %s %s", pe.what(), path);
	}

	emplace_and_lookup_test(pop, pop.root()->cons1.get());
	emplace_and_lookup_duplicates_test(pop, pop.root()->cons1.get());

	emplace_and_lookup_test(pop, pop.root()->cons2.get());
	emplace_and_lookup_duplicates_test(pop, pop.root()->cons2.get());

	nvobj::transaction::run(pop, [&] {
		nvobj::delete_persistent<persistent_map_type_int>(
			pop.root()->cons1);
		nvobj::delete_persistent<persistent_map_type_string>(
			pop.root()->cons2);
	});

	pop.close();
}

int
main(int argc, char *argv[])
{
	return run_test([&] { test(argc, argv); });
}