Project
Loading...
Searching...
No Matches
testMemoryResources.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#define BOOST_TEST_MODULE Test MemoryResources
13#define BOOST_TEST_MAIN
14#define BOOST_TEST_DYN_LINK
15#include <boost/test/unit_test.hpp>
17#include <fairmq/TransportFactory.h>
18#include <fairmq/Tools.h>
19#include <fairmq/ProgOptions.h>
20#include <vector>
21#include <cstring>
22
23namespace o2::pmr
24{
25struct testData {
26 int i{1};
27 static int nconstructions;
29 {
31 }
32 testData(const testData& in) : i{in.i}
33 {
35 }
36 testData(const testData&& in) : i{in.i}
37 {
39 }
40 testData(int in) : i{in}
41 {
43 }
44};
45
47
48BOOST_AUTO_TEST_CASE(transportallocatormap_test)
49{
50 size_t session{(size_t)getpid() * 1000};
51 fair::mq::ProgOptions config;
52 config.SetProperty<std::string>("session", std::to_string(session));
53
54 auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
55 auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "transportallocatormap_test", &config);
56 auto allocZMQ = getTransportAllocator(factoryZMQ.get());
57 auto allocSHM = getTransportAllocator(factorySHM.get());
58 BOOST_CHECK(allocZMQ != nullptr && allocSHM != allocZMQ);
59 auto _tmp = getTransportAllocator(factoryZMQ.get());
60 BOOST_CHECK(_tmp == allocZMQ);
61}
62
63using namespace fair::mq::pmr;
64
65BOOST_AUTO_TEST_CASE(allocator_test)
66{
67 size_t session{(size_t)getpid() * 1000 + 1};
68 fair::mq::ProgOptions config;
69 config.SetProperty<std::string>("session", std::to_string(session));
70
71 auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
72 auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "allocator_test", &config);
73 auto allocZMQ = getTransportAllocator(factoryZMQ.get());
74 auto allocSHM = getTransportAllocator(factorySHM.get());
75
77
78 {
79 std::vector<testData, polymorphic_allocator<testData>> v(polymorphic_allocator<testData>{allocZMQ});
80 v.reserve(3);
81 BOOST_CHECK(v.capacity() == 3);
82 BOOST_CHECK(allocZMQ->getNumberOfMessages() == 1);
83 v.emplace_back(1);
84 v.emplace_back(2);
85 v.emplace_back(3);
86 BOOST_CHECK((std::byte*)&(*v.end()) - (std::byte*)&(*v.begin()) == 3 * sizeof(testData));
88 }
89
91 BOOST_CHECK(allocZMQ->getNumberOfMessages() == 0);
92}
93
94BOOST_AUTO_TEST_CASE(getMessage_test)
95{
96 size_t session{(size_t)getpid() * 1000 + 2};
97 fair::mq::ProgOptions config;
98 config.SetProperty<std::string>("session", std::to_string(session));
99
100 auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
101 auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "getMessage_test", &config);
102 auto allocZMQ = getTransportAllocator(factoryZMQ.get());
103 auto allocSHM = getTransportAllocator(factorySHM.get());
104
106
107 fair::mq::MessagePtr message{nullptr};
108
109 int* messageArray{nullptr};
110
111 // test message creation on the same channel it was allocated with
112 {
113 std::vector<testData, polymorphic_allocator<testData>> v(polymorphic_allocator<testData>{allocZMQ});
114 v.emplace_back(1);
115 v.emplace_back(2);
116 v.emplace_back(3);
117 void* vectorBeginPtr = &v[0];
118 message = o2::pmr::getMessage(std::move(v));
119 BOOST_CHECK(message != nullptr);
120 BOOST_CHECK(message->GetData() == vectorBeginPtr);
121 }
122 BOOST_CHECK(message->GetSize() == 3 * sizeof(testData));
123 messageArray = static_cast<int*>(message->GetData());
124 BOOST_CHECK(messageArray[0] == 1 && messageArray[1] == 2 && messageArray[2] == 3);
125
126 // test message creation on a different channel than it was allocated with
127 {
128 std::vector<testData, polymorphic_allocator<testData>> v(polymorphic_allocator<testData>{allocZMQ});
129 v.emplace_back(4);
130 v.emplace_back(5);
131 v.emplace_back(6);
132 void* vectorBeginPtr = &v[0];
133 message = o2::pmr::getMessage(std::move(v), allocSHM);
134 BOOST_CHECK(message != nullptr);
135 BOOST_CHECK(message->GetData() != vectorBeginPtr);
136 }
137 BOOST_CHECK(message->GetSize() == 3 * sizeof(testData));
138 messageArray = static_cast<int*>(message->GetData());
139 BOOST_CHECK(messageArray[0] == 4 && messageArray[1] == 5 && messageArray[2] == 6);
140
141}
142
143}; // namespace o2::pmr
const GLdouble * v
Definition glcorearb.h:832
GLuint GLsizei const GLchar * message
Definition glcorearb.h:2517
O2 memory allocators and interfaces related to managing memory via the trasport layer.
BOOST_AUTO_TEST_CASE(transportallocatormap_test)
fair::mq::MessagePtr getMessage(ContainerT &&container, FairMQMemoryResource *targetResource=nullptr)
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
testData(const testData &in)
testData(const testData &&in)
BOOST_CHECK(tree)