proxygen
F14InterprocessTest.cpp File Reference
#include <ios>
#include <iostream>
#include <memory>
#include <scoped_allocator>
#include <string>
#include <vector>
#include <boost/interprocess/allocators/adaptive_pool.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <folly/Format.h>
#include <folly/Random.h>
#include <folly/Traits.h>
#include <folly/container/F14Map.h>
#include <folly/container/F14Set.h>
#include <folly/container/test/F14TestUtil.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Typedefs

template<typename T >
using ShmAllocator = adaptive_pool< T, managed_shared_memory::segment_manager >
 
template<typename K , typename M >
using ShmF14ValueMap = folly::F14ValueMap< K, M, folly::f14::DefaultHasher< K >, folly::f14::DefaultKeyEqual< K >, ShmAllocator< std::pair< K const, M >>>
 
template<typename K , typename M >
using ShmF14NodeMap = folly::F14NodeMap< K, M, folly::f14::DefaultHasher< K >, folly::f14::DefaultKeyEqual< K >, ShmAllocator< std::pair< K const, M >>>
 
template<typename K , typename M >
using ShmF14VectorMap = folly::F14VectorMap< K, M, folly::f14::DefaultHasher< K >, folly::f14::DefaultKeyEqual< K >, ShmAllocator< std::pair< K const, M >>>
 
template<typename K >
using ShmF14ValueSet = folly::F14ValueSet< K, folly::f14::DefaultHasher< K >, folly::f14::DefaultKeyEqual< K >, ShmAllocator< K >>
 
template<typename K >
using ShmF14NodeSet = folly::F14NodeSet< K, folly::f14::DefaultHasher< K >, folly::f14::DefaultKeyEqual< K >, ShmAllocator< K >>
 
template<typename K >
using ShmF14VectorSet = folly::F14VectorSet< K, folly::f14::DefaultHasher< K >, folly::f14::DefaultKeyEqual< K >, ShmAllocator< K >>
 
using ShmVI = std::vector< int, ShmAllocator< int >>
 
using ShmVVI = std::vector< ShmVI, std::scoped_allocator_adaptor< ShmAllocator< ShmVI >>>
 
using ShmF14ValueI2VVI = folly::F14ValueMap< int, ShmVVI, folly::f14::DefaultHasher< int >, folly::f14::DefaultKeyEqual< int >, std::scoped_allocator_adaptor< ShmAllocator< std::pair< int const, ShmVVI >>>>
 
using ShmF14NodeI2VVI = folly::F14NodeMap< int, ShmVVI, folly::f14::DefaultHasher< int >, folly::f14::DefaultKeyEqual< int >, std::scoped_allocator_adaptor< ShmAllocator< std::pair< int const, ShmVVI >>>>
 
using ShmF14VectorI2VVI = folly::F14VectorMap< int, ShmVVI, folly::f14::DefaultHasher< int >, folly::f14::DefaultKeyEqual< int >, std::scoped_allocator_adaptor< ShmAllocator< std::pair< int const, ShmVVI >>>>
 

Functions

template<typename M >
void runSimpleMapTest ()
 
template<typename S >
void runSimpleSetTest ()
 
 TEST (ShmF14ValueMap, simple)
 
 TEST (ShmF14NodeMap, simple)
 
 TEST (ShmF14VectorMap, simple)
 
 TEST (ShmF14ValueSet, simple)
 
 TEST (ShmF14NodeSet, simple)
 
 TEST (ShmF14VectorSet, simple)
 
template<typename M >
void runSimultaneousAccessMapTest ()
 
 TEST (ShmF14ValueMap, simultaneous)
 
 TEST (ShmF14NodeMap, simultaneous)
 
 TEST (ShmF14VectorMap, simultaneous)
 
template<typename T >
void checkSingleLocation (std::string name, std::shared_ptr< managed_shared_memory > const &segment, T const &val)
 
void checkLocation (std::string name, std::shared_ptr< managed_shared_memory > const &segment, int const &val)
 
template<typename A , typename B >
void checkLocation (std::string name, std::shared_ptr< managed_shared_memory > const &segment, std::pair< A, B > const &val)
 
template<typename T >
auto checkLocation (std::string name, std::shared_ptr< managed_shared_memory > const &segment, T const &val) -> folly::void_t< decltype(val.begin())>
 
template<typename M >
void runScopedAllocatorTest ()
 
 TEST (ShmF14ValueI2VVI, scopedAllocator)
 
 TEST (ShmF14NodeI2VVI, scopedAllocator)
 
 TEST (ShmF14VectorI2VVI, scopedAllocator)
 
template<typename M >
void runMultiScopeTest ()
 
 TEST (ShmF14ValueI2VVI, multiScope)
 
 TEST (ShmF14NodeI2VVI, multiScope)
 
 TEST (ShmF14VectorI2VVI, multiScope)
 

Typedef Documentation

template<typename T >
using ShmAllocator = adaptive_pool<T, managed_shared_memory::segment_manager>

Definition at line 38 of file F14InterprocessTest.cpp.

using ShmF14NodeI2VVI = folly::F14NodeMap< int, ShmVVI, folly::f14::DefaultHasher<int>, folly::f14::DefaultKeyEqual<int>, std::scoped_allocator_adaptor<ShmAllocator<std::pair<int const, ShmVVI>>>>

Definition at line 101 of file F14InterprocessTest.cpp.

template<typename K , typename M >
using ShmF14NodeMap = folly::F14NodeMap< K, M, folly::f14::DefaultHasher<K>, folly::f14::DefaultKeyEqual<K>, ShmAllocator<std::pair<K const, M>>>

Definition at line 54 of file F14InterprocessTest.cpp.

Definition at line 76 of file F14InterprocessTest.cpp.

using ShmF14ValueI2VVI = folly::F14ValueMap< int, ShmVVI, folly::f14::DefaultHasher<int>, folly::f14::DefaultKeyEqual<int>, std::scoped_allocator_adaptor<ShmAllocator<std::pair<int const, ShmVVI>>>>

Definition at line 94 of file F14InterprocessTest.cpp.

template<typename K , typename M >
using ShmF14ValueMap = folly::F14ValueMap< K, M, folly::f14::DefaultHasher<K>, folly::f14::DefaultKeyEqual<K>, ShmAllocator<std::pair<K const, M>>>

Definition at line 46 of file F14InterprocessTest.cpp.

Definition at line 69 of file F14InterprocessTest.cpp.

using ShmF14VectorI2VVI = folly::F14VectorMap< int, ShmVVI, folly::f14::DefaultHasher<int>, folly::f14::DefaultKeyEqual<int>, std::scoped_allocator_adaptor<ShmAllocator<std::pair<int const, ShmVVI>>>>

Definition at line 108 of file F14InterprocessTest.cpp.

template<typename K , typename M >
using ShmF14VectorMap = folly::F14VectorMap< K, M, folly::f14::DefaultHasher<K>, folly::f14::DefaultKeyEqual<K>, ShmAllocator<std::pair<K const, M>>>

Definition at line 62 of file F14InterprocessTest.cpp.

Definition at line 83 of file F14InterprocessTest.cpp.

using ShmVI = std::vector<int, ShmAllocator<int>>

Definition at line 85 of file F14InterprocessTest.cpp.

using ShmVVI = std::vector<ShmVI, std::scoped_allocator_adaptor<ShmAllocator<ShmVI>>>

Definition at line 87 of file F14InterprocessTest.cpp.

Function Documentation

void checkLocation ( std::string  name,
std::shared_ptr< managed_shared_memory > const &  segment,
int const &  val 
)

Definition at line 249 of file F14InterprocessTest.cpp.

References checkSingleLocation().

Referenced by checkLocation(), checkSingleLocation(), runMultiScopeTest(), and runScopedAllocatorTest().

252  {
253  checkSingleLocation(name, segment, val);
254 }
double val
Definition: String.cpp:273
void checkSingleLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, T const &val)
const char * name
Definition: http_parser.c:437
template<typename A , typename B >
void checkLocation ( std::string  name,
std::shared_ptr< managed_shared_memory > const &  segment,
std::pair< A, B > const &  val 
)

Definition at line 257 of file F14InterprocessTest.cpp.

References checkLocation(), and checkSingleLocation().

260  {
261  checkSingleLocation(name, segment, val);
262  checkLocation(name + ".first", segment, val.first);
263  checkLocation(name + ".second", segment, val.second);
264 }
double val
Definition: String.cpp:273
void checkSingleLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, T const &val)
const char * name
Definition: http_parser.c:437
void checkLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, int const &val)
template<typename T >
auto checkLocation ( std::string  name,
std::shared_ptr< managed_shared_memory > const &  segment,
T const &  val 
) -> folly::void_t<decltype(val.begin())>

Definition at line 267 of file F14InterprocessTest.cpp.

References checkLocation(), checkSingleLocation(), EXPECT_TRUE, name, and val.

270  {
271  typename T::allocator_type alloc{segment->get_segment_manager()};
272  EXPECT_TRUE(alloc == val.get_allocator());
273  checkSingleLocation(name, segment, val);
274  for (auto&& v : val) {
275  checkLocation(name + "[]", segment, v);
276  }
277 }
double val
Definition: String.cpp:273
void checkSingleLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, T const &val)
const char * name
Definition: http_parser.c:437
void checkLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, int const &val)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
template<typename T >
void checkSingleLocation ( std::string  name,
std::shared_ptr< managed_shared_memory > const &  segment,
T const &  val 
)

Definition at line 219 of file F14InterprocessTest.cpp.

References addr, checkLocation(), EXPECT_TRUE, name, string, T, and val.

Referenced by checkLocation().

222  {
223  auto beginAddr = reinterpret_cast<uintptr_t>(segment->get_address());
224  auto endAddr = beginAddr + segment->get_size();
225  auto addr = reinterpret_cast<uintptr_t>(&val);
226  EXPECT_TRUE(beginAddr <= addr && addr + sizeof(T) <= endAddr)
227  << name << ": begin @" << std::hex << beginAddr << ", val @" << std::hex
228  << addr << ", size" << std::hex << sizeof(T) << ", end @" << std::hex
229  << endAddr;
230 }
#define T(v)
Definition: http_parser.c:233
double val
Definition: String.cpp:273
const char * name
Definition: http_parser.c:437
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
ThreadPoolListHook * addr
template<typename M >
void runMultiScopeTest ( )

Definition at line 315 of file F14InterprocessTest.cpp.

References checkLocation(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, folly::gen::move, and folly::f14::swap().

315  {
316  auto segment1 = makeShmSegment(8192);
317  auto mgr1 = segment1->get_segment_manager();
318 
319  auto segment2 = makeShmSegment(8192);
320  auto mgr2 = segment2->get_segment_manager();
321 
322  auto a1 = segment1->construct<M>(anonymous_instance)(
323  typename M::allocator_type{mgr1});
324  (*a1)[1].emplace_back();
325  (*a1)[1][0].push_back(3);
326  auto b1 = segment1->construct<M>(anonymous_instance)(*a1);
327  auto c1 = segment1->construct<M>(anonymous_instance)(std::move(*a1));
328 
329  auto d2 = segment2->construct<M>(anonymous_instance)(
330  typename M::allocator_type{mgr2});
331  (*d2)[10].emplace_back();
332  (*d2)[10][0].push_back(6);
333  auto e2 = segment2->construct<M>(anonymous_instance)(*d2);
334  auto f2 = segment2->construct<M>(anonymous_instance)(
335  *b1, typename M::allocator_type{mgr2});
336 
337  checkLocation("a1", segment1, *a1);
338  checkLocation("b1", segment1, *b1);
339  checkLocation("c1", segment1, *c1);
340  checkLocation("d2", segment2, *d2);
341  checkLocation("e2", segment2, *e2);
342  checkLocation("f2", segment2, *f2);
343 
344  EXPECT_EQ(a1->size(), 0);
345  EXPECT_FALSE(*a1 == *b1);
346  EXPECT_TRUE(*b1 == *c1);
347  EXPECT_TRUE(*b1 == *f2);
348  EXPECT_EQ(d2->size(), 1);
349  EXPECT_EQ(e2->size(), 1);
350  EXPECT_FALSE(*e2 == *f2);
351 
352  checkLocation("d2", segment2, *d2);
353 
354  EXPECT_TRUE(*d2 == *e2);
355 
356  f2->clear();
357  *f2 = std::move(*d2);
358 
359  checkLocation("d2", segment2, *d2);
360  checkLocation("f2", segment2, *f2);
361 
362  EXPECT_TRUE(*f2 == *e2);
363  EXPECT_TRUE(d2->empty());
364 
365  {
366  using std::swap;
367  swap(*a1, *b1);
368  }
369  checkLocation("a1", segment1, *a1);
370  checkLocation("b1", segment1, *b1);
371  EXPECT_TRUE(*a1 == *c1);
372 
373  *a1 = std::move(*e2);
374 
375  EXPECT_TRUE(*f2 == *a1);
376 
377  checkLocation("a1", segment1, *a1);
378  checkLocation("e2", segment2, *e2);
379 
380  auto g2 = segment2->construct<M>(anonymous_instance)(
381  std::move(*a1), typename M::allocator_type{mgr2});
382 
383  EXPECT_TRUE(*f2 == *g2);
384 
385  checkLocation("f2", segment2, *f2);
386  checkLocation("g2", segment2, *g2);
387 
388  segment1->destroy_ptr(a1);
389  segment1->destroy_ptr(b1);
390  segment1->destroy_ptr(c1);
391  segment2->destroy_ptr(d2);
392  segment2->destroy_ptr(e2);
393  segment2->destroy_ptr(f2);
394  segment2->destroy_ptr(g2);
395 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void checkLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, int const &val)
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
Definition: Hazptr.h:104
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
template<typename M >
void runScopedAllocatorTest ( )

Definition at line 280 of file F14InterprocessTest.cpp.

References checkLocation(), and m.

280  {
281  auto segment = makeShmSegment(8192);
282  auto mgr = segment->get_segment_manager();
283 
284  auto vi = segment->construct<ShmVI>(anonymous_instance)(
285  typename ShmVI::allocator_type{mgr});
286  vi->push_back(10);
287  checkLocation("vi", segment, *vi);
288 
289  auto vvi = segment->construct<ShmVVI>(anonymous_instance)(
290  typename ShmVVI::allocator_type{mgr});
291  vvi->resize(1);
292  vvi->at(0).push_back(2);
293  checkLocation("vvi", segment, *vvi);
294 
295  auto m = segment->construct<M>(anonymous_instance)(
296  typename M::allocator_type{mgr});
297  (*m)[1].emplace_back();
298  (*m)[1][0].push_back(3);
299 
300  checkLocation("m", segment, *m);
301  m->clear();
302 }
std::vector< ShmVI, std::scoped_allocator_adaptor< ShmAllocator< ShmVI >>> ShmVVI
static map< string, int > m
void checkLocation(std::string name, std::shared_ptr< managed_shared_memory > const &segment, int const &val)
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
Definition: Hazptr.h:104
std::vector< int, ShmAllocator< int >> ShmVI
template<typename M >
void runSimpleMapTest ( )

Definition at line 129 of file F14InterprocessTest.cpp.

References EXPECT_EQ, i, and m.

129  {
130  auto segment = makeShmSegment(8192);
131  auto mgr = segment->get_segment_manager();
133  M m{alloc};
134  for (int i = 0; i < 20; ++i) {
135  m[i] = i * 10;
136  }
137  EXPECT_EQ(m.size(), 20);
138  for (int i = 0; i < 20; ++i) {
139  EXPECT_EQ(m[i], i * 10);
140  }
141 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static map< string, int > m
adaptive_pool< T, managed_shared_memory::segment_manager > ShmAllocator
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
Definition: Hazptr.h:104
template<typename S >
void runSimpleSetTest ( )

Definition at line 144 of file F14InterprocessTest.cpp.

References EXPECT_EQ, i, and s.

144  {
145  auto segment = makeShmSegment(8192);
146  auto mgr = segment->get_segment_manager();
147  S s{typename S::allocator_type{mgr}};
148  for (int i = 0; i < 20; ++i) {
149  s.insert(i);
150  }
151  EXPECT_EQ(s.size(), 20);
152  for (int i = 0; i < 40; ++i) {
153  EXPECT_EQ(s.count(i), (i < 20 ? 1 : 0));
154  }
155 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static set< string > s
template<typename M >
void runSimultaneousAccessMapTest ( )

Definition at line 177 of file F14InterprocessTest.cpp.

References EXPECT_EQ, EXPECT_FALSE, EXPECT_NE, folly::gen::first, folly::f14::detail::getF14IntrinsicsMode(), and name.

177  {
178  using namespace folly::f14::detail;
179 
180  // fallback std::unordered_map on libstdc++ doesn't pass this test
182  auto name = makeRandomName();
183  auto segment1 = makeShmSegment(8192, name);
184  auto segment2 =
185  std::make_shared<managed_shared_memory>(open_only, name.c_str());
186 
187  auto m1 = segment1->construct<M>("m")(
188  typename M::allocator_type{segment1->get_segment_manager()});
189  auto m2 = segment2->find<M>("m").first;
190 
191  std::cout << "m in segment1 @ " << (uintptr_t)m1 << "\n";
192  std::cout << "m in segment2 @ " << (uintptr_t)m2 << "\n";
193 
194  EXPECT_NE(&*m1, &*m2);
195 
196  (*m1)[1] = 10;
197  EXPECT_EQ(m2->count(0), 0);
198  EXPECT_EQ((*m2)[1], 10);
199  (*m2)[2] = 20;
200  EXPECT_EQ(m1->size(), 2);
201  EXPECT_EQ(m1->find(2)->second, 20);
202  (*m1)[3] = 30;
203  EXPECT_EQ(m2->size(), 3);
204  EXPECT_FALSE(m2->emplace(std::make_pair(3, 33)).second);
205  }
206 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const char * name
Definition: http_parser.c:437
static constexpr F14IntrinsicsMode getF14IntrinsicsMode()
**Optimized Holders **The template hazptr_array< M > provides most of the functionality *of M hazptr_holder s but with faster construction destruction *for M
Definition: Hazptr.h:104
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
constexpr detail::First first
Definition: Base-inl.h:2553
TEST ( ShmF14ValueMap  ,
simple   
)

Definition at line 157 of file F14InterprocessTest.cpp.

157  {
158  runSimpleMapTest<ShmF14ValueMap<int, int>>();
159 }
TEST ( ShmF14NodeMap  ,
simple   
)

Definition at line 160 of file F14InterprocessTest.cpp.

160  {
161  runSimpleMapTest<ShmF14NodeMap<int, int>>();
162 }
TEST ( ShmF14VectorMap  ,
simple   
)

Definition at line 163 of file F14InterprocessTest.cpp.

163  {
164  runSimpleMapTest<ShmF14VectorMap<int, int>>();
165 }
TEST ( ShmF14ValueSet  ,
simple   
)

Definition at line 166 of file F14InterprocessTest.cpp.

166  {
167  runSimpleSetTest<ShmF14ValueSet<int>>();
168 }
TEST ( ShmF14NodeSet  ,
simple   
)

Definition at line 169 of file F14InterprocessTest.cpp.

169  {
170  runSimpleSetTest<ShmF14NodeSet<int>>();
171 }
TEST ( ShmF14VectorSet  ,
simple   
)

Definition at line 172 of file F14InterprocessTest.cpp.

172  {
173  runSimpleSetTest<ShmF14VectorSet<int>>();
174 }
TEST ( ShmF14ValueMap  ,
simultaneous   
)

Definition at line 208 of file F14InterprocessTest.cpp.

208  {
209  runSimultaneousAccessMapTest<ShmF14ValueMap<int, int>>();
210 }
TEST ( ShmF14NodeMap  ,
simultaneous   
)

Definition at line 211 of file F14InterprocessTest.cpp.

211  {
212  runSimultaneousAccessMapTest<ShmF14NodeMap<int, int>>();
213 }
TEST ( ShmF14VectorMap  ,
simultaneous   
)

Definition at line 214 of file F14InterprocessTest.cpp.

214  {
215  runSimultaneousAccessMapTest<ShmF14VectorMap<int, int>>();
216 }
TEST ( ShmF14ValueI2VVI  ,
scopedAllocator   
)

Definition at line 304 of file F14InterprocessTest.cpp.

304  {
305  runScopedAllocatorTest<ShmF14ValueI2VVI>();
306 }
TEST ( ShmF14NodeI2VVI  ,
scopedAllocator   
)

Definition at line 307 of file F14InterprocessTest.cpp.

307  {
308  runScopedAllocatorTest<ShmF14NodeI2VVI>();
309 }
TEST ( ShmF14VectorI2VVI  ,
scopedAllocator   
)

Definition at line 310 of file F14InterprocessTest.cpp.

310  {
311  runScopedAllocatorTest<ShmF14VectorI2VVI>();
312 }
TEST ( ShmF14ValueI2VVI  ,
multiScope   
)

Definition at line 397 of file F14InterprocessTest.cpp.

397  {
398  runMultiScopeTest<ShmF14ValueI2VVI>();
399 }
TEST ( ShmF14NodeI2VVI  ,
multiScope   
)

Definition at line 400 of file F14InterprocessTest.cpp.

400  {
401  runMultiScopeTest<ShmF14NodeI2VVI>();
402 }
TEST ( ShmF14VectorI2VVI  ,
multiScope   
)

Definition at line 403 of file F14InterprocessTest.cpp.

403  {
404  runMultiScopeTest<ShmF14VectorI2VVI>();
405 }