File: enumerable_thread_specific_access.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 (243 lines) | stat: -rw-r--r-- 5,183 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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2019-2020, Intel Corporation */

#include "unittest.hpp"

#include <libpmemobj++/detail/enumerable_thread_specific.hpp>
#include <libpmemobj++/make_persistent.hpp>

#include <set>
#include <vector>

namespace nvobj = pmem::obj;

using test_t = std::size_t;

using container_type = pmem::detail::enumerable_thread_specific<test_t>;

struct root {
	nvobj::persistent_ptr<container_type> pptr;

	nvobj::persistent_ptr<container_type> m_pptr1;
	nvobj::persistent_ptr<container_type> m_pptr2;
};

void
test(nvobj::pool<struct root> &pop)
{
	// Adding more concurrency will increase DRD test time
	const size_t concurrency = 16;

	auto tls = pop.root()->pptr;

	UT_ASSERT(tls != nullptr);

	{
		std::vector<size_t> checker(concurrency, 0);
		parallel_exec(concurrency, [&](size_t thread_index) {
			test_t &ref = tls->local();

			/*
			 * Another thread already wrote some data there
			 * (and exited).
			 */
			if (ref > 0)
				return;

			ref = thread_index;
			for (size_t i = 0; i < 100; ++i) {
				ref = tls->local();
				UT_ASSERTeq(ref, thread_index);

				checker[ref]++;
			}
		});

		UT_ASSERT(tls->size() <= concurrency);

		size_t n_zeros = 0;
		size_t n_100 = 0;
		for (auto &e : checker) {
			if (e == 0)
				n_zeros++;
			else if (e == 100)
				n_100++;
			else
				UT_ASSERTeq(e, 0);
		}

		/* At least one thread should have done its work */
		UT_ASSERT(n_100 > 0);
		UT_ASSERT(n_100 + n_zeros == concurrency);
	}

	std::thread t([&] {
		tls->local() = 99;
		pop.persist(&tls->local(), sizeof(tls->local()));

		UT_ASSERT(tls->size() <= concurrency + 1);
		UT_ASSERT(tls->local() == 99);
	});

	t.join();

	tls->clear();
}

void
test_with_spin(nvobj::pool<struct root> &pop, size_t concurrency)
{
	auto tls = pop.root()->pptr;

	UT_ASSERT(tls != nullptr);
	UT_ASSERT(tls->size() == 0);
	UT_ASSERT(tls->empty());

	parallel_exec_with_sync(concurrency, [&](size_t thread_index) {
		tls->local()++;
		pop.persist(&tls->local(), sizeof(tls->local()));
	});

	/*
	 * tls->size() will be equal to max number of threads that have used
	 * tls at any given time. This test assumes that concurrency is >=
	 * than any previously used number of threads
	 */
	UT_ASSERTeq(tls->size(), concurrency);

	for (auto &e : *tls) {
		UT_ASSERTeq(e, 1);
	}

	tls->clear();
	UT_ASSERT(tls->size() == 0);
	UT_ASSERT(tls->empty());
}

void
test_multiple_tls(nvobj::pool<struct root> &pop)
{
	// Adding more concurrency will increase DRD test time
	const size_t concurrency = 16;

	auto tls1 = pop.root()->m_pptr1;
	auto tls2 = pop.root()->m_pptr2;

	parallel_exec_with_sync(concurrency, [&](size_t thread_index) {
		tls1->local() = thread_index;
		pop.persist(&tls1->local(), sizeof(tls1->local()));
	});

	parallel_exec_with_sync(concurrency, [&](size_t thread_index) {
		tls2->local() = thread_index;
		pop.persist(&tls2->local(), sizeof(tls2->local()));
	});

	UT_ASSERT(tls1->size() == concurrency);
	UT_ASSERT(tls2->size() == concurrency);

	{
		std::set<size_t> tids;
		for (auto &e : *tls1)
			tids.insert(e);

		for (size_t id = 0; id < concurrency; id++)
			UT_ASSERT(tids.count(id) == 1);
	}

	{
		std::set<size_t> tids;
		for (auto &e : *tls2)
			tids.insert(e);

		for (size_t id = 0; id < concurrency; id++)
			UT_ASSERT(tids.count(id) == 1);
	}

	tls1->clear();
	tls2->clear();

	UT_ASSERT(tls1->size() == 0);
	UT_ASSERT(tls2->size() == 0);

	parallel_exec_with_sync(concurrency, [&](size_t thread_index) {
		tls1->local() = thread_index;
		pop.persist(&tls1->local(), sizeof(tls1->local()));

		tls2->local() = thread_index;
		pop.persist(&tls2->local(), sizeof(tls2->local()));
	});

	UT_ASSERT(tls1->size() == concurrency);
	UT_ASSERT(tls2->size() == concurrency);

	{
		std::set<size_t> tids;
		for (auto &e : *tls1)
			tids.insert(e);

		for (size_t id = 0; id < concurrency; id++)
			UT_ASSERT(tids.count(id) == 1);
	}

	{
		std::set<size_t> tids;
		for (auto &e : *tls2)
			tids.insert(e);

		for (size_t id = 0; id < concurrency; id++)
			UT_ASSERT(tids.count(id) == 1);
	}
}

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

	auto path = argv[1];
	auto pop = nvobj::pool<root>::create(
		path, "TLSTest: enumerable_thread_specific_access",
		10 * PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR);

	auto r = pop.root();

	try {
		nvobj::transaction::run(pop, [&] {
			r->pptr = nvobj::make_persistent<container_type>();
			r->m_pptr1 = nvobj::make_persistent<container_type>();
			r->m_pptr2 = nvobj::make_persistent<container_type>();
		});

		test(pop);
		test_multiple_tls(pop);
		test_with_spin(pop, 16);

		if (!On_valgrind) {
			/*
			 * larger that initial size of queue of thread ids,
			 * run this only when not on valgrind due to execution
			 * time.
			 */
			test_with_spin(pop, 2048);
		}

		nvobj::transaction::run(pop, [&] {
			nvobj::delete_persistent<container_type>(r->pptr);
			nvobj::delete_persistent<container_type>(r->m_pptr1);
			nvobj::delete_persistent<container_type>(r->m_pptr2);
		});
	} catch (std::exception &e) {
		UT_FATALexc(e);
	}

	pop.close();
}

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