From bbfe603e5ae42317a2b67d713d00882bea341c88 Mon Sep 17 00:00:00 2001 From: Jim Flynn Date: Mon, 20 Jul 2020 16:57:44 +0100 Subject: IVGCVSW-5166 Pull out the common and server side code into standalone libraries Change-Id: I180f84c493a9b2be4b93b25d312ebdd9e71b1735 Signed-off-by: Jim Flynn --- .../server/src/basePipeServer/BasePipeServer.cpp | 63 +++++------ .../server/src/basePipeServer/BasePipeServer.hpp | 116 --------------------- profiling/server/src/basePipeServer/CMakeLists.txt | 48 +++++---- .../src/basePipeServer/ConnectionHandler.cpp | 23 ++-- .../src/basePipeServer/ConnectionHandler.hpp | 44 -------- .../basePipeServer/tests/BasePipeServerTests.cpp | 8 +- 6 files changed, 81 insertions(+), 221 deletions(-) delete mode 100644 profiling/server/src/basePipeServer/BasePipeServer.hpp delete mode 100644 profiling/server/src/basePipeServer/ConnectionHandler.hpp (limited to 'profiling/server/src/basePipeServer') diff --git a/profiling/server/src/basePipeServer/BasePipeServer.cpp b/profiling/server/src/basePipeServer/BasePipeServer.cpp index ed5c442492..81f58a5ee9 100644 --- a/profiling/server/src/basePipeServer/BasePipeServer.cpp +++ b/profiling/server/src/basePipeServer/BasePipeServer.cpp @@ -1,30 +1,31 @@ // -// Copyright © 2020 Arm Ltd. All rights reserved. +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // -#include "BasePipeServer.hpp" +#include -#include "common/include/Constants.hpp" +#include +#include #include -#include #include #include #include -using namespace armnnUtils; +namespace arm +{ -namespace armnnProfiling +namespace pipe { bool BasePipeServer::ReadFromSocket(uint8_t* packetData, uint32_t expectedLength) { // This is a blocking read until either expectedLength has been received or an error is detected. long totalBytesRead = 0; - while (boost::numeric_cast(totalBytesRead) < expectedLength) + while (arm::pipe::numeric_cast(totalBytesRead) < expectedLength) { - long bytesRead = Sockets::Read(m_ClientConnection, packetData, expectedLength); + long bytesRead = arm::pipe::Read(m_ClientConnection, packetData, expectedLength); if (bytesRead < 0) { std::cerr << ": Failure when reading from client socket: " << strerror(errno) << std::endl; @@ -68,17 +69,17 @@ bool BasePipeServer::WaitForStreamMetaData() EchoPacket(PacketDirection::ReceivedData, pipeMagic, 4); // Before we interpret the length we need to read the pipe_magic word to determine endianness. - if (ToUint32(&pipeMagic[0], TargetEndianness::BeWire) == armnnProfiling::PIPE_MAGIC) + if (ToUint32(&pipeMagic[0], TargetEndianness::BeWire) == PIPE_MAGIC) { m_Endianness = TargetEndianness::BeWire; } - else if (ToUint32(&pipeMagic[0], TargetEndianness::LeWire) == armnnProfiling::PIPE_MAGIC) + else if (ToUint32(&pipeMagic[0], TargetEndianness::LeWire) == PIPE_MAGIC) { m_Endianness = TargetEndianness::LeWire; } else { - std::cerr << ": Protocol read error. Unable to read PIPE_MAGIC value." << std::endl; + std::cerr << ": Protocol read error. Unable to read the PIPE_MAGIC value." << std::endl; return false; } // Now we know the endianness we can get the length from the header. @@ -87,7 +88,7 @@ bool BasePipeServer::WaitForStreamMetaData() // Read the entire packet. std::vector packetData(metaDataLength); if (metaDataLength != - boost::numeric_cast(Sockets::Read(m_ClientConnection, packetData.data(), metaDataLength))) + arm::pipe::numeric_cast(arm::pipe::Read(m_ClientConnection, packetData.data(), metaDataLength))) { std::cerr << ": Protocol read error. Data length mismatch." << std::endl; return false; @@ -100,11 +101,11 @@ bool BasePipeServer::WaitForStreamMetaData() return true; } -armnn::profiling::Packet BasePipeServer::WaitForPacket(uint32_t timeoutMs) +arm::pipe::Packet BasePipeServer::WaitForPacket(uint32_t timeoutMs) { // Is there currently more than a headers worth of data waiting to be read? int bytes_available; - Sockets::Ioctl(m_ClientConnection, FIONREAD, &bytes_available); + arm::pipe::Ioctl(m_ClientConnection, FIONREAD, &bytes_available); if (bytes_available > 8) { // Yes there is. Read it: @@ -115,18 +116,18 @@ armnn::profiling::Packet BasePipeServer::WaitForPacket(uint32_t timeoutMs) // No there's not. Poll for more data. struct pollfd pollingFd[1]{}; pollingFd[0].fd = m_ClientConnection; - int pollResult = Sockets::Poll(pollingFd, 1, static_cast(timeoutMs)); + int pollResult = arm::pipe::Poll(pollingFd, 1, static_cast(timeoutMs)); switch (pollResult) { // Error case -1: - throw armnn::RuntimeException(std::string("File descriptor reported an error during polling: ") + - strerror(errno)); + throw ProfilingException(std::string("File descriptor reported an error during polling: ") + + strerror(errno)); // Timeout case 0: - throw armnn::TimeoutException("Timeout while waiting to receive packet."); + throw arm::pipe::TimeoutException("Timeout while waiting to receive packet."); // Normal poll return. It could still contain an error signal default: @@ -135,16 +136,18 @@ armnn::profiling::Packet BasePipeServer::WaitForPacket(uint32_t timeoutMs) { if (pollingFd[0].revents == POLLNVAL) { - throw armnn::RuntimeException(std::string("Error while polling receiving socket: POLLNVAL")); + throw arm::pipe::ProfilingException( + std::string("Error while polling receiving socket: POLLNVAL")); } if (pollingFd[0].revents == POLLERR) { - throw armnn::RuntimeException(std::string("Error while polling receiving socket: POLLERR: ") + - strerror(errno)); + throw arm::pipe::ProfilingException( + std::string("Error while polling receiving socket: POLLERR: ") + strerror(errno)); } if (pollingFd[0].revents == POLLHUP) { - throw armnn::RuntimeException(std::string("Connection closed by remote client: POLLHUP")); + throw arm::pipe::ProfilingException( + std::string("Connection closed by remote client: POLLHUP")); } } @@ -153,19 +156,20 @@ armnn::profiling::Packet BasePipeServer::WaitForPacket(uint32_t timeoutMs) { // This is a corner case. The socket as been woken up but not with any data. // We'll throw a timeout exception to loop around again. - throw armnn::TimeoutException("File descriptor was polled but no data was available to receive."); + throw arm::pipe::TimeoutException( + "File descriptor was polled but no data was available to receive."); } return ReceivePacket(); } } } -armnn::profiling::Packet BasePipeServer::ReceivePacket() +arm::pipe::Packet BasePipeServer::ReceivePacket() { uint32_t header[2]; if (!ReadHeader(header)) { - return armnn::profiling::Packet(); + return arm::pipe::Packet(); } // Read data_length bytes from the socket. std::unique_ptr uniquePacketData = std::make_unique(header[1]); @@ -173,13 +177,13 @@ armnn::profiling::Packet BasePipeServer::ReceivePacket() if (!ReadFromSocket(packetData, header[1])) { - return armnn::profiling::Packet(); + return arm::pipe::Packet(); } EchoPacket(PacketDirection::ReceivedData, packetData, header[1]); // Construct received packet - armnn::profiling::Packet packetRx = armnn::profiling::Packet(header[0], header[1], uniquePacketData); + arm::pipe::Packet packetRx = arm::pipe::Packet(header[0], header[1], uniquePacketData); if (m_EchoPackets) { std::cout << "Processing packet ID= " << packetRx.GetPacketId() << " Length=" << packetRx.GetLength() @@ -206,7 +210,7 @@ bool BasePipeServer::SendPacket(uint32_t packetFamily, uint32_t packetId, const memcpy((packet.data() + 8), data, dataLength); } EchoPacket(PacketDirection::Sending, packet.data(), packet.size()); - if (-1 == armnnUtils::Sockets::Write(m_ClientConnection, packet.data(), packet.size())) + if (-1 == arm::pipe::Write(m_ClientConnection, packet.data(), packet.size())) { std::cerr << ": Failure when writing to client socket: " << strerror(errno) << std::endl; return false; @@ -294,4 +298,5 @@ void BasePipeServer::InsertU32(uint32_t value, uint8_t* data, TargetEndianness e } } -} // namespace armnnProfiling \ No newline at end of file +} // namespace pipe +} // namespace arm diff --git a/profiling/server/src/basePipeServer/BasePipeServer.hpp b/profiling/server/src/basePipeServer/BasePipeServer.hpp deleted file mode 100644 index bef9d29f44..0000000000 --- a/profiling/server/src/basePipeServer/BasePipeServer.hpp +++ /dev/null @@ -1,116 +0,0 @@ -// -// Copyright © 2020 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - -#include -#include -#include - -#include -#include - -namespace armnnProfiling -{ - -enum class TargetEndianness -{ - BeWire, - LeWire -}; - -enum class PacketDirection -{ - Sending, - ReceivedHeader, - ReceivedData -}; -class ConnectionHandler; - -class BasePipeServer -{ - -public: - - BasePipeServer(armnnUtils::Sockets::Socket clientConnection, bool echoPackets) - : m_ClientConnection(clientConnection) - , m_EchoPackets(echoPackets) - {} - - ~BasePipeServer() - { - // We have set SOCK_CLOEXEC on this socket but we'll close it to be good citizens. - armnnUtils::Sockets::Close(m_ClientConnection); - } - - BasePipeServer(const BasePipeServer&) = delete; - BasePipeServer& operator=(const BasePipeServer&) = delete; - - BasePipeServer(BasePipeServer&&) = delete; - BasePipeServer& operator=(BasePipeServer&&) = delete; - - /// Close the client connection - /// @return 0 if successful - int Close() - { - return armnnUtils::Sockets::Close(m_ClientConnection); - } - - /// Send a packet to the client - /// @return true if a valid packet has been sent. - bool SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength); - - /// Set the client socket to nonblocking - /// @return true if successful. - bool SetNonBlocking() - { - return armnnUtils::Sockets::SetNonBlocking(m_ClientConnection); - } - - /// Block on the client connection until a complete packet has been received. - /// @return true if a valid packet has been received. - armnn::profiling::Packet WaitForPacket(uint32_t timeoutMs); - - /// Once the connection is open wait to receive the stream meta data packet from the client. Reading this - /// packet differs from others as we need to determine endianness. - /// @return true only if a valid stream meta data packet has been received. - bool WaitForStreamMetaData(); - - uint32_t GetStreamMetadataVersion() - { - return m_StreamMetaDataVersion; - } - - uint32_t GetStreamMetadataMaxDataLen() - { - return m_StreamMetaDataMaxDataLen; - } - - uint32_t GetStreamMetadataPid() - { - return m_StreamMetaDataPid; - } - -private: - - void EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes); - bool ReadFromSocket(uint8_t* packetData, uint32_t expectedLength); - bool ReadHeader(uint32_t headerAsWords[2]); - - armnn::profiling::Packet ReceivePacket(); - - uint32_t ToUint32(uint8_t* data, TargetEndianness endianness); - void InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness); - - armnnUtils::Sockets::Socket m_ClientConnection; - bool m_EchoPackets; - TargetEndianness m_Endianness; - - uint32_t m_StreamMetaDataVersion; - uint32_t m_StreamMetaDataMaxDataLen; - uint32_t m_StreamMetaDataPid; -}; - -} // namespace armnnProfiling \ No newline at end of file diff --git a/profiling/server/src/basePipeServer/CMakeLists.txt b/profiling/server/src/basePipeServer/CMakeLists.txt index 56ef4548ec..b5dc68c2c4 100644 --- a/profiling/server/src/basePipeServer/CMakeLists.txt +++ b/profiling/server/src/basePipeServer/CMakeLists.txt @@ -1,37 +1,49 @@ # -# Copyright © 2020 Arm Ltd. All rights reserved. +# Copyright © 2020 Arm Ltd and Contributors. All rights reserved. # SPDX-License-Identifier: MIT # if(BUILD_BASE_PIPE_SERVER) set(BasePipeServer_sources) list(APPEND BasePipeServer_sources - BasePipeServer.cpp - BasePipeServer.hpp - ConnectionHandler.cpp - ConnectionHandler.hpp) + BasePipeServer.cpp) + + if(NOT "${TOOLCHAIN_PREFIX}" STREQUAL x86_64-w64-mingw32) + list(APPEND BasePipeServer_sources + ConnectionHandler.cpp) + endif() include_directories(${PROJECT_SOURCE_DIR}/profiling/common/include) + include_directories(${PROJECT_SOURCE_DIR}/profiling/server/include/basePipeServer) if (BUILD_UNIT_TESTS) target_include_directories(UnitTests PRIVATE ${PROJECT_SOURCE_DIR}/profiling/server/src/basePipeServer) target_include_directories(UnitTests PUBLIC ${PROJECT_SOURCE_DIR}/profiling/common/include) endif() - add_library_ex(armnnBasePipeServer SHARED ${BasePipeServer_sources}) + if (BUILD_STATIC_PIPE_LIBS) + add_library_ex(armnnBasePipeServer STATIC ${BasePipeServer_sources}) + target_link_libraries(armnnBasePipeServer pipeCommon) - set_target_properties(armnnBasePipeServer PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) - set_target_properties(armnnBasePipeServer PROPERTIES VERSION ${GENERIC_LIB_VERSION} - SOVERSION ${GENERIC_LIB_SOVERSION}) + if ("${CMAKE_SYSTEM_NAME}" STREQUAL Windows) + target_link_libraries(armnnBasePipeServer ws2_32.lib) + endif() + else() + add_library_ex(armnnBasePipeServer SHARED ${BasePipeServer_sources}) + target_link_libraries(armnnBasePipeServer pipeCommon) - target_include_directories(armnnBasePipeServer PRIVATE ${PROJECT_SOURCE_DIR}/src/armnnUtils) + if ("${CMAKE_SYSTEM_NAME}" STREQUAL Windows) + target_link_libraries(armnnBasePipeServer ws2_32.lib) + endif() - target_link_libraries(armnnBasePipeServer armnn) - if ("${CMAKE_SYSTEM_NAME}" STREQUAL Windows) - target_link_libraries(armnnBasePipeServer Ws2_32.lib) - endif() + set_target_properties(armnnBasePipeServer PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) + set_target_properties(armnnBasePipeServer PROPERTIES VERSION ${GENERIC_LIB_VERSION} + SOVERSION ${GENERIC_LIB_SOVERSION}) + + target_include_directories(armnnBasePipeServer PRIVATE ${PROJECT_SOURCE_DIR}/src/armnnUtils) - install(TARGETS armnnBasePipeServer - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) -endif() \ No newline at end of file + install(TARGETS armnnBasePipeServer + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + endif() +endif() diff --git a/profiling/server/src/basePipeServer/ConnectionHandler.cpp b/profiling/server/src/basePipeServer/ConnectionHandler.cpp index 1485ab8620..1c9ffa95c2 100644 --- a/profiling/server/src/basePipeServer/ConnectionHandler.cpp +++ b/profiling/server/src/basePipeServer/ConnectionHandler.cpp @@ -1,18 +1,21 @@ // -// Copyright © 2020 Arm Ltd. All rights reserved. +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // -#include "ConnectionHandler.hpp" -#include +#include -using namespace armnnUtils; +#include -namespace armnnProfiling +namespace arm { + +namespace pipe +{ + ConnectionHandler::ConnectionHandler(const std::string& udsNamespace, const bool setNonBlocking) { - Sockets::Initialize(); + arm::pipe::Initialize(); m_ListeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); if (-1 == m_ListeningSocket) @@ -39,14 +42,13 @@ ConnectionHandler::ConnectionHandler(const std::string& udsNamespace, const bool if (setNonBlocking) { - Sockets::SetNonBlocking(m_ListeningSocket); + arm::pipe::SetNonBlocking(m_ListeningSocket); } } std::unique_ptr ConnectionHandler::GetNewBasePipeServer(const bool echoPackets) { - armnnUtils::Sockets::Socket clientConnection = armnnUtils::Sockets::Accept(m_ListeningSocket, nullptr, nullptr, - SOCK_CLOEXEC); + arm::pipe::Socket clientConnection = arm::pipe::Accept(m_ListeningSocket, nullptr, nullptr, SOCK_CLOEXEC); if (clientConnection < 1) { return nullptr; @@ -54,4 +56,5 @@ std::unique_ptr ConnectionHandler::GetNewBasePipeServer(const bo return std::make_unique(clientConnection, echoPackets); } -} // namespace armnnProfiling \ No newline at end of file +} // namespace pipe +} // namespace arm diff --git a/profiling/server/src/basePipeServer/ConnectionHandler.hpp b/profiling/server/src/basePipeServer/ConnectionHandler.hpp deleted file mode 100644 index 661935b885..0000000000 --- a/profiling/server/src/basePipeServer/ConnectionHandler.hpp +++ /dev/null @@ -1,44 +0,0 @@ -// -// Copyright © 2020 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - -#include "BasePipeServer.hpp" -#include - -namespace armnnProfiling -{ - -class ConnectionHandler -{ -public: - /// Constructor establishes the Unix domain socket and sets it to listen for connections. - /// @param udsNamespace the namespace (socket address) associated with the listener. - /// @throws SocketConnectionException if the socket has been incorrectly setup. - ConnectionHandler(const std::string& udsNamespace, const bool setNonBlocking); - - ~ConnectionHandler() - { - // We have set SOCK_CLOEXEC on this socket but we'll close it to be good citizens. - armnnUtils::Sockets::Close(m_ListeningSocket); - } - - ConnectionHandler(const ConnectionHandler&) = delete; - ConnectionHandler& operator=(const ConnectionHandler&) = delete; - - ConnectionHandler(ConnectionHandler&&) = delete; - ConnectionHandler& operator=(ConnectionHandler&&) = delete; - - /// Attempt to open a new socket to the client and use it to construct a new basePipeServer - /// @param echoPackets if true the raw packets will be printed to stdout. - /// @return if successful a unique_ptr to a basePipeServer otherwise a nullptr - std::unique_ptr GetNewBasePipeServer(const bool echoPackets); - -private: - - armnnUtils::Sockets::Socket m_ListeningSocket; -}; - -} // namespace armnnProfiling \ No newline at end of file diff --git a/profiling/server/src/basePipeServer/tests/BasePipeServerTests.cpp b/profiling/server/src/basePipeServer/tests/BasePipeServerTests.cpp index 57fc9e9da6..091792782f 100644 --- a/profiling/server/src/basePipeServer/tests/BasePipeServerTests.cpp +++ b/profiling/server/src/basePipeServer/tests/BasePipeServerTests.cpp @@ -1,9 +1,9 @@ // -// Copyright © 2020 Arm Ltd. All rights reserved. +// Copyright © 2020 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // -#include +#include #include #include @@ -15,7 +15,7 @@ BOOST_AUTO_TEST_SUITE(BasePipeServerTests) using namespace armnn; -using namespace armnnProfiling; +using namespace arm::pipe; BOOST_AUTO_TEST_CASE(BasePipeServerTest) { @@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(BasePipeServerTest) // The socket should close once we leave the scope of BOOST_CHECK_NO_THROW // and socketProfilingConnection should fail to connect BOOST_CHECK_THROW(profiling::SocketProfilingConnection socketProfilingConnection, - armnnProfiling::SocketConnectionException); + arm::pipe::SocketConnectionException); // Try to initialize a listening socket through the ConnectionHandler again ConnectionHandler connectionHandler(udsNamespace, true); -- cgit v1.2.1