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 boost::container::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 {
92 std::vector<testData, SpectatorAllocator<testData>> v(SpectatorAllocator<testData>{allocZMQ});
93 v.reserve(3);
94 BOOST_CHECK(allocZMQ->getNumberOfMessages() == 1);
95 v.emplace_back(1);
96 v.emplace_back(2);
97 v.emplace_back(3);
99 }
100 BOOST_CHECK(allocZMQ->getNumberOfMessages() == 0);
101}
102
103BOOST_AUTO_TEST_CASE(getMessage_test)
104{
105 size_t session{(size_t)getpid() * 1000 + 2};
106 fair::mq::ProgOptions config;
107 config.SetProperty<std::string>("session", std::to_string(session));
108
109 auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
110 auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "getMessage_test", &config);
111 auto allocZMQ = getTransportAllocator(factoryZMQ.get());
112 auto allocSHM = getTransportAllocator(factorySHM.get());
113
115
116 fair::mq::MessagePtr message{nullptr};
117
118 int* messageArray{nullptr};
119
120 // test message creation on the same channel it was allocated with
121 {
122 std::vector<testData, polymorphic_allocator<testData>> v(polymorphic_allocator<testData>{allocZMQ});
123 v.emplace_back(1);
124 v.emplace_back(2);
125 v.emplace_back(3);
126 void* vectorBeginPtr = &v[0];
127 message = o2::pmr::getMessage(std::move(v));
128 BOOST_CHECK(message != nullptr);
129 BOOST_CHECK(message->GetData() == vectorBeginPtr);
130 }
131 BOOST_CHECK(message->GetSize() == 3 * sizeof(testData));
132 messageArray = static_cast<int*>(message->GetData());
133 BOOST_CHECK(messageArray[0] == 1 && messageArray[1] == 2 && messageArray[2] == 3);
134
135 // test message creation on a different channel than it was allocated with
136 {
137 std::vector<testData, polymorphic_allocator<testData>> v(polymorphic_allocator<testData>{allocZMQ});
138 v.emplace_back(4);
139 v.emplace_back(5);
140 v.emplace_back(6);
141 void* vectorBeginPtr = &v[0];
142 message = o2::pmr::getMessage(std::move(v), allocSHM);
143 BOOST_CHECK(message != nullptr);
144 BOOST_CHECK(message->GetData() != vectorBeginPtr);
145 }
146 BOOST_CHECK(message->GetSize() == 3 * sizeof(testData));
147 messageArray = static_cast<int*>(message->GetData());
148 BOOST_CHECK(messageArray[0] == 4 && messageArray[1] == 5 && messageArray[2] == 6);
149
150 {
151 std::vector<testData, SpectatorAllocator<testData>> v(SpectatorAllocator<testData>{allocSHM});
152 }
153}
154
155BOOST_AUTO_TEST_CASE(adoptVector_test)
156{
157 size_t session{(size_t)getpid() * 1000 + 3};
158 fair::mq::ProgOptions config;
159 config.SetProperty<std::string>("session", std::to_string(session));
160
161 auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
162 auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "adoptVector_test", &config);
163 auto allocZMQ = getTransportAllocator(factoryZMQ.get());
164 auto allocSHM = getTransportAllocator(factorySHM.get());
165
167
168 // Create a bogus message
169 auto message = factoryZMQ->CreateMessage(3 * sizeof(testData));
170 auto messageAddr = message.get();
171 testData tmpBuf[3] = {3, 2, 1};
172 std::memcpy(message->GetData(), tmpBuf, 3 * sizeof(testData));
173
174 auto adoptedOwner = adoptVector<testData>(3, std::move(message));
175 BOOST_CHECK(adoptedOwner[0].i == 3);
176 BOOST_CHECK(adoptedOwner[1].i == 2);
177 BOOST_CHECK(adoptedOwner[2].i == 1);
178
179 auto reclaimedMessage = o2::pmr::getMessage(std::move(adoptedOwner));
180 BOOST_CHECK(reclaimedMessage.get() == messageAddr);
181 BOOST_CHECK(adoptedOwner.size() == 0);
182
183 auto modified = adoptVector<testData>(3, std::move(reclaimedMessage));
184 modified.emplace_back(9);
185 BOOST_CHECK(modified[3].i == 9);
186 BOOST_CHECK(modified.size() == 4);
188 auto modifiedMessage = getMessage(std::move(modified));
189 BOOST_CHECK(modifiedMessage != nullptr);
190 BOOST_CHECK(modifiedMessage.get() != messageAddr);
191}
192
193BOOST_AUTO_TEST_CASE(test_SpectatorMemoryResource)
194{
195 constexpr int size = 5;
196 auto buffer = std::make_unique<int[]>(size);
197 auto const* bufferdata = buffer.get();
198 SpectatorMemoryResource<decltype(buffer)> resource(std::move(buffer), size * sizeof(int));
199 std::vector<int, o2::pmr::SpectatorAllocator<int>> bufferclone(size, o2::pmr::SpectatorAllocator<int>(&resource));
200 BOOST_CHECK(bufferclone.data() == bufferdata);
201 BOOST_CHECK(bufferclone.size() == size);
202 BOOST_CHECK_THROW(bufferclone.resize(2 * size), std::runtime_error);
203
204 auto vecbuf = std::make_unique<std::vector<int>>(size);
205 auto const* vectordata = vecbuf->data();
206 SpectatorMemoryResource<decltype(vecbuf)> vecresource(std::move(vecbuf));
207 std::vector<int, o2::pmr::SpectatorAllocator<int>> vecclone(size, o2::pmr::SpectatorAllocator<int>(&vecresource));
208 BOOST_CHECK(vecclone.data() == vectordata);
209 BOOST_CHECK(vecclone.size() == size);
210 BOOST_CHECK_THROW(vecclone.resize(2 * size), std::runtime_error);
211
212 std::vector<int, o2::pmr::SpectatorAllocator<int>> vecmove;
213 vecmove = std::move(vecclone);
214 BOOST_CHECK(vecclone.size() == 0);
215 BOOST_CHECK(vecmove.data() == vectordata);
216 BOOST_CHECK(vecmove.size() == size);
217}
218
219}; // namespace o2::pmr
int32_t i
const auto & getMessage()
GLuint buffer
Definition glcorearb.h:655
GLsizeiptr size
Definition glcorearb.h:659
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)