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#include <memory_resource>
23
24namespace o2::pmr
25{
26struct testData {
27 int i{1};
28 static int nconstructions;
30 {
32 }
33 testData(const testData& in) : i{in.i}
34 {
36 }
37 testData(const testData&& in) : i{in.i}
38 {
40 }
41 testData(int in) : i{in}
42 {
44 }
45};
46
48
49BOOST_AUTO_TEST_CASE(transportallocatormap_test)
50{
51 size_t session{(size_t)getpid() * 1000};
52 fair::mq::ProgOptions config;
53 config.SetProperty<std::string>("session", std::to_string(session));
54
55 auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
56 auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "transportallocatormap_test", &config);
57 auto allocZMQ = getTransportAllocator(factoryZMQ.get());
58 auto allocSHM = getTransportAllocator(factorySHM.get());
59 BOOST_CHECK(allocZMQ != nullptr && allocSHM != allocZMQ);
60 auto _tmp = getTransportAllocator(factoryZMQ.get());
61 BOOST_CHECK(_tmp == allocZMQ);
62}
63
64BOOST_AUTO_TEST_CASE(allocator_test)
65{
66 size_t session{(size_t)getpid() * 1000 + 1};
67 fair::mq::ProgOptions config;
68 config.SetProperty<std::string>("session", std::to_string(session));
69
70 auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
71 auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "allocator_test", &config);
72 auto allocZMQ = getTransportAllocator(factoryZMQ.get());
73 auto allocSHM = getTransportAllocator(factorySHM.get());
74
76
77 {
78 std::vector<testData, std::pmr::polymorphic_allocator<testData>> v(std::pmr::polymorphic_allocator<testData>{allocZMQ});
79 v.reserve(3);
80 BOOST_CHECK(v.capacity() == 3);
81 BOOST_CHECK(allocZMQ->getNumberOfMessages() == 1);
82 v.emplace_back(1);
83 v.emplace_back(2);
84 v.emplace_back(3);
85 BOOST_CHECK((std::byte*)&(*v.end()) - (std::byte*)&(*v.begin()) == 3 * sizeof(testData));
87 }
88
90 BOOST_CHECK(allocZMQ->getNumberOfMessages() == 0);
91}
92
93BOOST_AUTO_TEST_CASE(getMessage_test)
94{
95 size_t session{(size_t)getpid() * 1000 + 2};
96 fair::mq::ProgOptions config;
97 config.SetProperty<std::string>("session", std::to_string(session));
98
99 auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
100 auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "getMessage_test", &config);
101 auto allocZMQ = getTransportAllocator(factoryZMQ.get());
102 auto allocSHM = getTransportAllocator(factorySHM.get());
103
105
106 fair::mq::MessagePtr message{nullptr};
107
108 int* messageArray{nullptr};
109
110 // test message creation on the same channel it was allocated with
111 {
112 std::vector<testData, std::pmr::polymorphic_allocator<testData>> v(std::pmr::polymorphic_allocator<testData>{allocZMQ});
113 v.emplace_back(1);
114 v.emplace_back(2);
115 v.emplace_back(3);
116 void* vectorBeginPtr = &v[0];
117 message = o2::pmr::getMessage(std::move(v));
118 BOOST_CHECK(message != nullptr);
119 BOOST_CHECK(message->GetData() == vectorBeginPtr);
120 }
121 BOOST_CHECK(message->GetSize() == 3 * sizeof(testData));
122 messageArray = static_cast<int*>(message->GetData());
123 BOOST_CHECK(messageArray[0] == 1 && messageArray[1] == 2 && messageArray[2] == 3);
124
125 // test message creation on a different channel than it was allocated with
126 {
127 std::vector<testData, std::pmr::polymorphic_allocator<testData>> v(std::pmr::polymorphic_allocator<testData>{allocZMQ});
128 v.emplace_back(4);
129 v.emplace_back(5);
130 v.emplace_back(6);
131 void* vectorBeginPtr = &v[0];
132 message = o2::pmr::getMessage(std::move(v), allocSHM);
133 BOOST_CHECK(message != nullptr);
134 BOOST_CHECK(message->GetData() != vectorBeginPtr);
135 }
136 BOOST_CHECK(message->GetSize() == 3 * sizeof(testData));
137 messageArray = static_cast<int*>(message->GetData());
138 BOOST_CHECK(messageArray[0] == 4 && messageArray[1] == 5 && messageArray[2] == 6);
139}
140
141}; // 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, fair::mq::MemoryResource *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)