diff --git a/.clang-format b/.clang-format index 376103600..1ac1f5bca 100644 --- a/.clang-format +++ b/.clang-format @@ -19,7 +19,7 @@ ColumnLimit: 100 ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true -DerivePointerAlignment: true +DerivePointerAlignment: false FixNamespaceComments: true IndentWidth: 4 IndentWrappedFunctionNames: true diff --git a/fairmq/FairMQTransportFactory.cxx b/fairmq/FairMQTransportFactory.cxx index b8bdfa7cd..459bc67a9 100644 --- a/fairmq/FairMQTransportFactory.cxx +++ b/fairmq/FairMQTransportFactory.cxx @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2017-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2017-2020 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -22,6 +22,7 @@ using namespace std; FairMQTransportFactory::FairMQTransportFactory(const string& id) : fkId(id) + , fMemoryResource(this) {} auto FairMQTransportFactory::CreateTransportFactory(const string& type, diff --git a/fairmq/FairMQTransportFactory.h b/fairmq/FairMQTransportFactory.h index 16c2da398..e6f9e52bb 100644 --- a/fairmq/FairMQTransportFactory.h +++ b/fairmq/FairMQTransportFactory.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2020 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -34,7 +34,7 @@ class FairMQTransportFactory const std::string fkId; /// The polymorphic memory resource associated with the transport - fair::mq::ChannelResource fMemoryResource{this}; + fair::mq::ChannelResource fMemoryResource; public: /// ctor diff --git a/fairmq/MemoryResourceTools.h b/fairmq/MemoryResourceTools.h index 7ad7ebc90..ffd98aa0b 100644 --- a/fairmq/MemoryResourceTools.h +++ b/fairmq/MemoryResourceTools.h @@ -1,10 +1,10 @@ /******************************************************************************** - * Copyright (C) 2018 CERN and copyright holders of ALICE O2 * - * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2018 CERN and copyright holders of ALICE O2 * + * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * - * This software is distributed under the terms of the * - * GNU Lesser General Public Licence (LGPL) version 3, * - * copied verbatim in the file "LICENSE" * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * ********************************************************************************/ /// @brief Tools for interfacing containers to the transport via polymorphic diff --git a/fairmq/MemoryResources.cxx b/fairmq/MemoryResources.cxx index 1b5713072..600cc5bc8 100644 --- a/fairmq/MemoryResources.cxx +++ b/fairmq/MemoryResources.cxx @@ -1,10 +1,10 @@ /******************************************************************************** - * Copyright (C) 2018 CERN and copyright holders of ALICE O2 * - * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2018 CERN and copyright holders of ALICE O2 * + * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * - * This software is distributed under the terms of the * - * GNU Lesser General Public Licence (LGPL) version 3, * - * copied verbatim in the file "LICENSE" * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * ********************************************************************************/ /// @brief Memory allocators and interfaces related to managing memory via the @@ -15,7 +15,13 @@ #include #include -void *fair::mq::ChannelResource::do_allocate(std::size_t bytes, std::size_t alignment) +namespace fair { +namespace mq { + +auto createMessage(TransportFactory* factory, std::size_t bytes, Alignment alignment) -> MessagePtr { - return setMessage(factory->CreateMessage(bytes, fair::mq::Alignment{alignment})); + return factory->CreateMessage(bytes, alignment); } + +} // namespace mq +} // namespace fair diff --git a/fairmq/MemoryResources.h b/fairmq/MemoryResources.h index 93f8277a4..0e050ff7b 100644 --- a/fairmq/MemoryResources.h +++ b/fairmq/MemoryResources.h @@ -1,10 +1,10 @@ /******************************************************************************** - * Copyright (C) 2018 CERN and copyright holders of ALICE O2 * - * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2018 CERN and copyright holders of ALICE O2 * + * Copyright (C) 2018-2020 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * - * This software is distributed under the terms of the * - * GNU Lesser General Public Licence (LGPL) version 3, * - * copied verbatim in the file "LICENSE" * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * ********************************************************************************/ /// @brief Memory allocators and interfaces related to managing memory via the @@ -30,6 +30,7 @@ namespace mq { using byte = unsigned char; namespace pmr = boost::container::pmr; +using TransportFactory = FairMQTransportFactory; /// All FairMQ related memory resources need to inherit from this interface /// class for the @@ -42,30 +43,33 @@ class FairMQMemoryResource : public pmr::memory_resource /// buffer), e.g. pointer returned by std::vector::data() return nullptr if /// returning /// a message does not make sense! - virtual FairMQMessagePtr getMessage(void *p) = 0; - virtual void *setMessage(FairMQMessagePtr) = 0; - virtual FairMQTransportFactory *getTransportFactory() noexcept = 0; + virtual MessagePtr getMessage(void* p) = 0; + virtual void* setMessage(MessagePtr) = 0; + virtual TransportFactory* getTransportFactory() noexcept = 0; virtual size_t getNumberOfMessages() const noexcept = 0; }; +auto createMessage(TransportFactory* factory, std::size_t bytes, Alignment alignment) -> MessagePtr; + /// This is the allocator that interfaces to FairMQ memory management. All /// allocations are /// delegated to FairMQ so standard (e.g. STL) containers can construct their /// stuff in /// memory regions appropriate for the data channel configuration. -class ChannelResource : public FairMQMemoryResource +template +class AlignedChannelResource : public FairMQMemoryResource { protected: - FairMQTransportFactory *factory{nullptr}; + TransportFactory* factory{nullptr}; // TODO: for now a map to keep track of allocations, something else would // probably be // faster, but for now this does not need to be fast. - boost::container::flat_map messageMap; + boost::container::flat_map messageMap; public: - ChannelResource() = delete; + AlignedChannelResource() = delete; - ChannelResource(FairMQTransportFactory *_factory) + AlignedChannelResource(TransportFactory* _factory) : FairMQMemoryResource() , factory(_factory) , messageMap() @@ -73,39 +77,48 @@ class ChannelResource : public FairMQMemoryResource if (!_factory) { throw std::runtime_error("Tried to construct from a nullptr FairMQTransportFactory"); } - }; + } - FairMQMessagePtr getMessage(void *p) override + MessagePtr getMessage(void* p) override { auto mes = std::move(messageMap[p]); messageMap.erase(p); return mes; } - void *setMessage(FairMQMessagePtr message) override + void* setMessage(MessagePtr message) override { - void *addr = message->GetData(); + void* addr = message->GetData(); messageMap[addr] = std::move(message); return addr; } - FairMQTransportFactory *getTransportFactory() noexcept override { return factory; } + TransportFactory* getTransportFactory() noexcept override { return factory; } size_t getNumberOfMessages() const noexcept override { return messageMap.size(); } protected: - void *do_allocate(std::size_t bytes, std::size_t alignment) override; - void do_deallocate(void *p, std::size_t /*bytes*/, std::size_t /*alignment*/) override + void* do_allocate(std::size_t bytes, std::size_t alignment) override + { + return setMessage(createMessage(factory, + bytes, + alignment < MinAlignment ? Alignment{MinAlignment} + : Alignment{alignment})); + } + + void do_deallocate(void* p, std::size_t /*bytes*/, std::size_t /*alignment*/) override { messageMap.erase(p); - }; + } - bool do_is_equal(const pmr::memory_resource &other) const noexcept override + bool do_is_equal(const pmr::memory_resource& other) const noexcept override { return this == &other; - }; + } }; +using ChannelResource = AlignedChannelResource<0>; + } /* namespace mq */ } /* namespace fair */ diff --git a/fairmq/sdk/DDSEnvironment.cxx b/fairmq/sdk/DDSEnvironment.cxx index 2a2b13de9..bd38f32b6 100644 --- a/fairmq/sdk/DDSEnvironment.cxx +++ b/fairmq/sdk/DDSEnvironment.cxx @@ -66,7 +66,10 @@ struct DDSEnvironment::Impl " mkdir -p \"$HOME/.DDS\"\n" " dds-user-defaults --ignore-default-sid -d -c \"$HOME/.DDS/DDS.cfg\"\n" "fi\n"; - std::system(cmd.str().c_str()); + auto rc(std::system(cmd.str().c_str())); + if (rc != 0) { + LOG(warn) << "DDSEnvironment::SetupConfigHome failed"; + } } auto SetupPath() -> void diff --git a/test/memory_resources/_memory_resources.cxx b/test/memory_resources/_memory_resources.cxx index a370eb982..8faad5d12 100644 --- a/test/memory_resources/_memory_resources.cxx +++ b/test/memory_resources/_memory_resources.cxx @@ -137,4 +137,13 @@ TEST(MemoryResources, getMessage) EXPECT_TRUE(messageArray[0] == 4 && messageArray[1] == 5 && messageArray[2] == 6); } +TEST(MemoryResources, AlignedChannelResource) +{ + auto factory = TransportFactory::CreateTransportFactory("shmem"); + constexpr std::size_t alignment(64); + AlignedChannelResource pmr(factory.get()); + auto ptr = pmr.allocate(100); + ASSERT_EQ(reinterpret_cast(ptr) % 64, 0); +} + } // namespace