-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathmmap_tests.cpp
More file actions
342 lines (264 loc) · 9.4 KB
/
mmap_tests.cpp
File metadata and controls
342 lines (264 loc) · 9.4 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
// Copyright 2023 Christian Mazakas.
// Copyright 2023-2024 Joaquin M Lopez Munoz.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#if defined(BOOST_CLANG_VERSION) && BOOST_CLANG_VERSION < 30900
BOOST_PRAGMA_MESSAGE(
"This version of clang is incompatible with Boost.Process");
int main() {}
#elif defined(__MSVC_RUNTIME_CHECKS)
BOOST_PRAGMA_MESSAGE(
"Test skipped because of /RTCc, which is incompatible with Boost.Interprocess");
int main() {}
#else
#include "../helpers/test.hpp"
#include <boost/unordered/unordered_flat_map.hpp>
#include <boost/unordered/unordered_map.hpp>
#include <boost/unordered/unordered_node_map.hpp>
#include <boost/unordered/unordered_flat_set.hpp>
#include <boost/unordered/unordered_node_set.hpp>
#include <boost/unordered/unordered_set.hpp>
#include <boost/unordered/concurrent_flat_map.hpp>
#include <boost/unordered/concurrent_flat_set.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/asio.hpp>
#include <boost/process/process.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <algorithm>
#include <iostream>
#include <type_traits>
#include <vector>
#ifndef BOOST_UNORDERED_FOA_MMAP_MAP_TYPE
#error "this requires a class template be passed as a macro"
#endif
using char_allocator = boost::interprocess::allocator<char,
boost::interprocess::managed_shared_memory::segment_manager>;
using string_type = boost::interprocess::basic_string<char,
std::char_traits<char>, char_allocator>;
using pair_type = std::pair<const string_type, string_type>;
using string_pair_type = std::pair<string_type, string_type>;
using string_pair_allocator = boost::interprocess::allocator<string_pair_type,
boost::interprocess::managed_shared_memory::segment_manager>;
using pair_allocator = boost::interprocess::allocator<pair_type,
boost::interprocess::managed_shared_memory::segment_manager>;
template <template <class, class, class, class, class> class Map,
class MapType = Map<string_type, string_type, boost::hash<string_type>,
std::equal_to<string_type>, pair_allocator> >
typename std::enable_if<
!std::is_same<typename MapType::value_type, string_pair_type>::value,
MapType>::type
get_container_type()
{
return {};
}
template <template <class, class, class, class> class Set,
class SetType = Set<string_pair_type, boost::hash<string_pair_type>,
std::equal_to<string_pair_type>, string_pair_allocator> >
typename std::enable_if<
std::is_same<typename SetType::value_type, string_pair_type>::value,
SetType>::type
get_container_type()
{
return {};
}
using concurrent_map = decltype(
get_container_type<boost::concurrent_flat_map>());
using concurrent_set = decltype(
get_container_type<boost::concurrent_flat_set>());
template <class C>
struct is_concurrent_container: std::false_type {};
template <typename... Args>
struct is_concurrent_container<boost::concurrent_flat_map<Args...> >:
std::true_type {};
template <typename... Args>
struct is_concurrent_container<boost::concurrent_flat_set<Args...> >:
std::true_type {};
static char const* shm_map_name = "shared_map";
template <class C>
typename std::enable_if<!is_concurrent_container<C>::value, void>::type
parent(std::string const& shm_name_, char const* exe_name, C*)
{
struct shm_remove
{
char const* shm_name;
shm_remove(char const* shm_name_) : shm_name(shm_name_)
{
boost::interprocess::shared_memory_object::remove(shm_name);
}
~shm_remove()
{
boost::interprocess::shared_memory_object::remove(shm_name);
}
} remover{shm_name_.c_str()};
using container_type = C;
std::size_t const shm_size = 64 * 1024;
auto shm_name = remover.shm_name;
boost::interprocess::managed_shared_memory segment(
boost::interprocess::create_only, shm_name, shm_size);
auto segment_mngr = segment.get_segment_manager();
char_allocator char_alloc(segment_mngr);
pair_allocator pair_alloc(segment_mngr);
using iterator_type = typename C::iterator;
container_type* c =
segment.construct<container_type>(shm_map_name)(pair_alloc);
iterator_type* it = segment.construct<iterator_type>("shared_iterator")();
auto const old_bc = c->bucket_count();
BOOST_TEST(c->empty());
boost::asio::io_context ctx;
boost::process::process child(ctx.get_executor(), exe_name, {shm_name});
child.wait();
int ret = child.exit_code();
BOOST_TEST(*it == c->begin());
BOOST_TEST((**it) == *(c->begin()));
c->erase(*it);
auto inputs =
std::vector<std::pair<std::string, std::string> >{{"hello", "world"}};
for (const auto& kvp : inputs) {
auto const& key = kvp.first;
auto const& value = kvp.second;
c->emplace(string_type(key.data(), char_alloc),
string_type(value.data(), char_alloc));
}
BOOST_TEST_EQ(ret, 0);
BOOST_TEST_GT(c->size(), inputs.size());
BOOST_TEST_GT(c->bucket_count(), old_bc);
segment.destroy<iterator_type>("shared_iterator");
segment.destroy<container_type>(shm_map_name);
}
template <class C>
typename std::enable_if<!is_concurrent_container<C>::value, void>::type
child(std::string const& shm_name, C*)
{
using container_type = C;
using iterator = typename container_type::iterator;
boost::interprocess::managed_shared_memory segment(
boost::interprocess::open_only, shm_name.c_str());
container_type* c = segment.find<container_type>(shm_map_name).first;
iterator* it = segment.find<iterator>("shared_iterator").first;
BOOST_TEST(c->empty());
std::vector<std::pair<std::string, std::string> > inputs = {
{"aaa", "AAA"}, {"bbb", "BBB"}, {"ccc", "CCCC"}};
if (BOOST_TEST_NE(c, nullptr)) {
auto a = segment.get_segment_manager();
for (const auto& input : inputs) {
c->emplace(string_type(input.first.c_str(), a),
string_type(input.second.c_str(), a));
}
c->rehash(c->bucket_count() + 1);
if (BOOST_TEST_NE(it, nullptr)) {
*it = c->begin();
}
}
}
template <class C>
typename std::enable_if<is_concurrent_container<C>::value, void>::type
parent(std::string const& shm_name_, char const* exe_name, C*)
{
struct shm_remove
{
char const* shm_name;
shm_remove(char const* shm_name_) : shm_name(shm_name_)
{
boost::interprocess::shared_memory_object::remove(shm_name);
}
~shm_remove()
{
boost::interprocess::shared_memory_object::remove(shm_name);
}
} remover{shm_name_.c_str()};
using container_type = C;
std::size_t const shm_size = 64 * 1024;
auto shm_name = remover.shm_name;
boost::interprocess::managed_shared_memory segment(
boost::interprocess::create_only, shm_name, shm_size);
auto segment_mngr = segment.get_segment_manager();
char_allocator char_alloc(segment_mngr);
pair_allocator pair_alloc(segment_mngr);
container_type* c =
segment.construct<container_type>(shm_map_name)(pair_alloc);
auto const old_bc = c->bucket_count();
BOOST_TEST(c->empty());
boost::asio::io_context ctx;
boost::process::process child(ctx.get_executor(), exe_name, {shm_name});
child.wait();
int ret = child.exit_code();
auto inputs =
std::vector<std::pair<std::string, std::string> >{{"hello", "world"}};
for (const auto& kvp : inputs) {
auto const& key = kvp.first;
auto const& value = kvp.second;
c->emplace(string_type(key.data(), char_alloc),
string_type(value.data(), char_alloc));
}
BOOST_TEST_EQ(ret, 0);
BOOST_TEST_GT(c->size(), inputs.size());
BOOST_TEST_GT(c->bucket_count(), old_bc);
segment.destroy<container_type>(shm_map_name);
}
template <class C>
typename std::enable_if<is_concurrent_container<C>::value, void>::type
child(std::string const& shm_name, C*)
{
using container_type = C;
boost::interprocess::managed_shared_memory segment(
boost::interprocess::open_only, shm_name.c_str());
container_type* c = segment.find<container_type>(shm_map_name).first;
BOOST_TEST(c->empty());
std::vector<std::pair<std::string, std::string> > inputs = {
{"aaa", "AAA"}, {"bbb", "BBB"}, {"ccc", "CCCC"}};
if (BOOST_TEST_NE(c, nullptr)) {
auto a = segment.get_segment_manager();
for (const auto& input : inputs) {
c->emplace(string_type(input.first.c_str(), a),
string_type(input.second.c_str(), a));
}
c->rehash(c->bucket_count() + 1);
}
}
std::string shm_name_sanitize(std::string const& exe_name)
{
std::string s(exe_name);
auto pos = std::remove_if(s.begin(), s.end(), [](char const c) {
switch (c) {
case '/':
case '.':
case '\\':
case '-':
case '_':
case '+':
return true;
default:
return false;
}
});
s.erase(pos, s.end());
s = "/" + s;
return s.substr(0, 255);
}
void mmap_test(int argc, char const** argv)
{
using container_type =
decltype(get_container_type<BOOST_UNORDERED_FOA_MMAP_MAP_TYPE>());
if (argc == 1) {
auto uuid = to_string(boost::uuids::random_generator()());
auto exe_name = argv[0];
auto shm_name = shm_name_sanitize(std::string(exe_name) + uuid);
container_type* p = nullptr;
parent(shm_name, exe_name, p);
} else {
auto shm_name = std::string(argv[1]);
container_type* p = nullptr;
child(shm_name, p);
}
}
int main(int argc, char const** argv)
{
mmap_test(argc, argv);
return boost::report_errors();
}
#endif