diff options
Diffstat (limited to 'tests/profiling/gatordmock')
15 files changed, 0 insertions, 1982 deletions
diff --git a/tests/profiling/gatordmock/CommandFileParser.cpp b/tests/profiling/gatordmock/CommandFileParser.cpp deleted file mode 100644 index 7c746f16e9..0000000000 --- a/tests/profiling/gatordmock/CommandFileParser.cpp +++ /dev/null @@ -1,85 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "CommandFileParser.hpp" - -#include <algorithm> -#include <fstream> -#include <iostream> -#include <iterator> - -namespace armnn -{ - -namespace gatordmock -{ - -void CommandFileParser::ParseFile(std::string CommandFile, GatordMockService& mockService) -{ - std::ifstream infile(CommandFile); - std::string line; - - std::cout << "Parsing command file: " << CommandFile << std::endl; - - while (mockService.ReceiveThreadRunning() && std::getline(infile, line)) - { - std::istringstream iss(line); - - std::vector<std::string> tokens; - - std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), - std::back_inserter(tokens)); - - std::string command = tokens[0]; - - if (command == "LIST") - { - // Expected format for the SET command - // - // LIST - // - - mockService.SendRequestCounterDir(); - } - if (command == "SET") - { - // Expected format for the SET command - // - // SET 500000 1 2 5 10 - // - // This breaks down to: - // SET command - // 500000 polling period in micro seconds - // 1 2 5 10 counter list - - uint32_t period = static_cast<uint32_t>(std::stoul(tokens[1])); - - std::vector<uint16_t> counters; - - std::transform(tokens.begin() + 2, tokens.end(), std::back_inserter(counters), - [](const std::string& str) { return static_cast<uint16_t>(std::stoul(str)); }); - - mockService.SendPeriodicCounterSelectionList(period, counters); - } - else if (command == "WAIT") - { - // Expected format for the SET command - // - // WAIT 11000000 - // - // This breaks down to: - // WAIT command - // 11000000 timeout period in micro seconds - - uint32_t timeout = static_cast<uint32_t>(std::stoul(tokens[1])); - - mockService.WaitCommand(timeout); - } - } -} - -} // namespace gatordmock - -} // namespace armnn diff --git a/tests/profiling/gatordmock/CommandFileParser.hpp b/tests/profiling/gatordmock/CommandFileParser.hpp deleted file mode 100644 index fd4a4fdf38..0000000000 --- a/tests/profiling/gatordmock/CommandFileParser.hpp +++ /dev/null @@ -1,29 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - -#include "GatordMockService.hpp" -#include <string> - -namespace armnn -{ - -namespace gatordmock -{ - -/// This class parses a command file for the GatordMockService. The file contains one command per line. -/// Valid commands are: SET and WAIT. -/// -/// SET: Will construct and send a PeriodicCounterSelection packet to enable a set of counters. -/// WAIT: Will pause for a set period of time to allow for data to be received. -class CommandFileParser -{ -public: - void ParseFile(std::string CommandFile, GatordMockService& mockService); -}; - -} // namespace gatordmock -} // namespace armnn diff --git a/tests/profiling/gatordmock/CommandLineProcessor.cpp b/tests/profiling/gatordmock/CommandLineProcessor.cpp deleted file mode 100644 index 55b51137bc..0000000000 --- a/tests/profiling/gatordmock/CommandLineProcessor.cpp +++ /dev/null @@ -1,72 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "CommandLineProcessor.hpp" - -#include <boost/program_options.hpp> -#include <iostream> - -namespace armnn -{ -namespace gatordmock -{ - -bool CommandLineProcessor::ProcessCommandLine(int argc, char *argv[]) -{ - namespace po = boost::program_options; - po::options_description desc("Options"); - try - { - desc.add_options() - ("help,h", "Display help messages") - ("file,f", po::value<std::string>(&m_File), - "The path to the file that contains instructions for the mock gatord") - ("namespace,n", po::value<std::string>(&m_UdsNamespace)->default_value("gatord_namespace"), - "The Unix domain socket namespace this server will bind to.\n" - "This will always be prepended with \\0 to use the abstract namespace") - ("echo,e", po::bool_switch(&m_Echo)->default_value(false), - "Echo packets sent and received to stdout. Disabled by default.\n"); - } - catch (const std::exception& e) - { - std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl; - return false; - } - - po::variables_map vm; - try - { - po::store(po::parse_command_line(argc, argv, desc), vm); - - if (vm.count("help")) - { - std::cout << "Simulate a Gatord server to interact with ArmNN external profiling." << std::endl; - std::cout << std::endl; - std::cout << desc << std::endl; - return false; - } - // Currently the file parameter is mandatory. - if (!vm.count("file")) - { - std::cout << std::endl << "*** Expected --file or -f parameter." << std::endl; - std::cout << std::endl; - std::cout << desc << std::endl; - return false; - } - po::notify(vm); - } - catch (const po::error& e) - { - std::cerr << e.what() << std::endl << std::endl; - std::cerr << desc << std::endl; - return false; - } - - return true; -} - -} // namespace gatordmock - -} // namespace armnn
\ No newline at end of file diff --git a/tests/profiling/gatordmock/CommandLineProcessor.hpp b/tests/profiling/gatordmock/CommandLineProcessor.hpp deleted file mode 100644 index 532948a5cf..0000000000 --- a/tests/profiling/gatordmock/CommandLineProcessor.hpp +++ /dev/null @@ -1,47 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// -#pragma once - -#include <string> - -namespace armnn -{ - -namespace gatordmock -{ - -/// Use Boost program options to process the command line. -/// -h or --help to print the options. -/// -n or --namespace to specify the UDS namespace that the server will be listening on. -/// -e or --echo print all sent and received packets to stdout. -/// -f or --file The path to the file that contains instructions for the mock gatord. -class CommandLineProcessor -{ -public: - bool ProcessCommandLine(int argc, char* argv[]); - bool IsEchoEnabled() - { - return m_Echo; - } - - std::string GetUdsNamespace() - { - return m_UdsNamespace; - } - std::string GetCommandFile() - { - return m_File; - } - -private: - std::string m_UdsNamespace; - std::string m_File; - - bool m_Echo; -}; - -} // namespace gatordmock - -} // namespace armnn diff --git a/tests/profiling/gatordmock/CounterDirectory.hpp b/tests/profiling/gatordmock/CounterDirectory.hpp deleted file mode 100644 index 7b45e661e0..0000000000 --- a/tests/profiling/gatordmock/CounterDirectory.hpp +++ /dev/null @@ -1,263 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - - -#include "GatordMockService.hpp" -#include "MockUtils.hpp" - -#include "Packet.hpp" -#include "CommandHandlerFunctor.hpp" -#include "SendCounterPacket.hpp" -#include "IPeriodicCounterCapture.hpp" - -#include <vector> -#include <thread> -#include <atomic> -#include <iostream> -#include <functional> - -namespace armnn -{ - -namespace gatordmock -{ - -struct EventRecord -{ - uint16_t m_CounterUid; - uint16_t m_MaxCounterUid; - uint16_t m_DeviceUid; - uint16_t m_CounterSetUid; - uint16_t m_CounterClass; - uint16_t m_CounterInterpolation; - double m_CounterMultiplier; - std::string m_CounterName; - std::string m_CounterDescription; - std::string m_CounterUnits; - - static void printHeader(std::string categoryName) - { - std::string header; - - header.append(gatordmock::CentreAlignFormatting("Counter Name", 20)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Description", 50)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Units", 14)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("UID", 6)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Max UID",10)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Class", 8)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Interpolation", 14)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Multiplier", 20)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Counter set UID", 16)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Device UID", 14)); - header.append("\n"); - - std::cout << "\n" << "\n"; - std::cout << gatordmock::CentreAlignFormatting("EVENTS IN CATEGORY: " + categoryName, - static_cast<int>(header.size())); - std::cout << "\n"; - std::cout << std::string(header.size(), '=') << "\n"; - std::cout << header; - } - - void printContents() const - { - std::string body; - - body.append(gatordmock::CentreAlignFormatting(m_CounterName, 20)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(m_CounterDescription, 50)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(m_CounterUnits, 14)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_CounterUid), 6)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_MaxCounterUid), 10)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_CounterClass), 8)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_CounterInterpolation), 14)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_CounterMultiplier), 20)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_CounterSetUid), 16)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_DeviceUid), 14)); - - body.append("\n"); - - std::cout << std::string(body.size(), '-') << "\n"; - - std::cout << body; - } -}; - -struct CategoryRecord -{ - uint16_t m_DeviceUid; - uint16_t m_CounterSet; - uint16_t m_EventCount; - std::string m_CategoryName; - std::vector<EventRecord> m_EventRecords; - - void print() const - { - std::string body; - std::string header; - - header.append(gatordmock::CentreAlignFormatting("Name", 20)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Device", 12)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Counter set UID:", 16)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Event Count", 14)); - header.append("\n"); - - body.append(gatordmock::CentreAlignFormatting(m_CategoryName, 20)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_DeviceUid), 12)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_CounterSet), 16)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_EventCount), 14)); - - std::cout << "\n" << "\n"; - std::cout << gatordmock::CentreAlignFormatting("CATEGORY", static_cast<int>(header.size())); - std::cout << "\n"; - std::cout << std::string(header.size(), '=') << "\n"; - - std::cout<< header; - - std::cout << std::string(body.size(), '-') << "\n"; - - std::cout<< body; - - if(m_EventRecords.size() > 0) - { - EventRecord::printHeader(m_CategoryName); - - std::for_each(m_EventRecords.begin(), m_EventRecords.end(), std::mem_fun_ref(&EventRecord::printContents)); - } - } -}; - -struct CounterSetRecord -{ - uint16_t m_CounterSetUid; - uint16_t m_CounterSetCount; - std::string m_CounterSetName; - - static void printHeader() - { - std::string header; - - header.append(gatordmock::CentreAlignFormatting("Counter set name", 20)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("UID",13)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Count",10)); - header.append("\n"); - - std::cout << "\n" << "\n"; - std::cout << gatordmock::CentreAlignFormatting("COUNTER SETS", static_cast<int>(header.size())); - std::cout << "\n"; - std::cout << std::string(header.size(), '=') << "\n"; - - std::cout<< header; - } - - void printContents() const - { - std::string body; - - body.append(gatordmock::CentreAlignFormatting(m_CounterSetName, 20)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_CounterSetUid), 13)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_CounterSetCount), 10)); - body.append("\n"); - - std::cout << std::string(body.size(), '-') << "\n"; - - std::cout<< body; - } -}; - -struct DeviceRecord -{ - uint16_t m_DeviceUid; - uint16_t m_DeviceCores; - std::string m_DeviceName; - - static void printHeader() - { - std::string header; - - header.append(gatordmock::CentreAlignFormatting("Device name", 20)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("UID",13)); - header.append(" | "); - header.append(gatordmock::CentreAlignFormatting("Cores",10)); - header.append("\n"); - - std::cout << "\n" << "\n"; - std::cout << gatordmock::CentreAlignFormatting("DEVICES", static_cast<int>(header.size())); - std::cout << "\n"; - std::cout << std::string(header.size(), '=') << "\n"; - std::cout<< header; - } - - void printContents() const - { - std::string body; - - body.append(gatordmock::CentreAlignFormatting(m_DeviceName, 20)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_DeviceUid), 13)); - body.append(" | "); - body.append(gatordmock::CentreAlignFormatting(std::to_string(m_DeviceCores), 10)); - body.append("\n"); - - std::cout << std::string(body.size(), '-') << "\n"; - std::cout<< body; - } -}; - -struct CounterDirectory -{ - std::vector<CategoryRecord> m_Categories; - std::vector<CounterSetRecord> m_CounterSets; - std::vector<DeviceRecord> m_DeviceRecords; - - void print() const - { - DeviceRecord::printHeader(); - std::for_each(m_DeviceRecords.begin(), m_DeviceRecords.end(), - std::mem_fun_ref(&DeviceRecord::printContents)); - - CounterSetRecord::printHeader(); - std::for_each(m_CounterSets.begin(), m_CounterSets.end(), - std::mem_fun_ref(&CounterSetRecord::printContents)); - - std::for_each(m_Categories.begin(), m_Categories.end(), - std::mem_fun_ref(&CategoryRecord::print)); - std::cout << "\n"; - } -}; - -} // namespace gatordmock - -} // namespace armnn
\ No newline at end of file diff --git a/tests/profiling/gatordmock/GatordMockMain.cpp b/tests/profiling/gatordmock/GatordMockMain.cpp deleted file mode 100644 index edad85cffe..0000000000 --- a/tests/profiling/gatordmock/GatordMockMain.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "../../../src/profiling/PacketVersionResolver.hpp" -#include "../../../src/profiling/PeriodicCounterSelectionCommandHandler.hpp" -#include "CommandFileParser.hpp" -#include "CommandLineProcessor.hpp" -#include "DirectoryCaptureCommandHandler.hpp" -#include "GatordMockService.hpp" -#include "PeriodicCounterCaptureCommandHandler.hpp" -#include "PeriodicCounterSelectionResponseHandler.hpp" - -#include <iostream> -#include <string> - -int main(int argc, char* argv[]) -{ - // Process command line arguments - armnn::gatordmock::CommandLineProcessor cmdLine; - if (!cmdLine.ProcessCommandLine(argc, argv)) - { - return EXIT_FAILURE; - } - - armnn::profiling::PacketVersionResolver packetVersionResolver; - // Create the Command Handler Registry - armnn::profiling::CommandHandlerRegistry registry; - - // This functor will receive back the selection response packet. - armnn::gatordmock::PeriodicCounterSelectionResponseHandler periodicCounterSelectionResponseHandler( - 0, 4, packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue()); - // This functor will receive the counter data. - armnn::gatordmock::PeriodicCounterCaptureCommandHandler counterCaptureCommandHandler( - 3, 0, packetVersionResolver.ResolvePacketVersion(3, 0).GetEncodedValue()); - - armnn::profiling::DirectoryCaptureCommandHandler directoryCaptureCommandHandler( - 0, 2, packetVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue(), false); - - // Register different derived functors - registry.RegisterFunctor(&periodicCounterSelectionResponseHandler); - registry.RegisterFunctor(&counterCaptureCommandHandler); - registry.RegisterFunctor(&directoryCaptureCommandHandler); - - armnn::gatordmock::GatordMockService mockService(registry, cmdLine.IsEchoEnabled()); - - if (!mockService.OpenListeningSocket(cmdLine.GetUdsNamespace())) - { - return EXIT_FAILURE; - } - std::cout << "Bound to UDS namespace: \\0" << cmdLine.GetUdsNamespace() << std::endl; - - // Wait for a single connection. - if (-1 == mockService.BlockForOneClient()) - { - return EXIT_FAILURE; - } - std::cout << "Client connection established." << std::endl; - - // Send receive the strweam metadata and send connection ack. - if (!mockService.WaitForStreamMetaData()) - { - return EXIT_FAILURE; - } - mockService.SendConnectionAck(); - - // Prepare to receive data. - mockService.LaunchReceivingThread(); - - // Process the SET and WAIT command from the file. - armnn::gatordmock::CommandFileParser commandLineParser; - commandLineParser.ParseFile(cmdLine.GetCommandFile(), mockService); - - // Once we've finished processing the file wait for the receiving thread to close. - mockService.WaitForReceivingThread(); - - return EXIT_SUCCESS; -} diff --git a/tests/profiling/gatordmock/GatordMockService.cpp b/tests/profiling/gatordmock/GatordMockService.cpp deleted file mode 100644 index c5211962d3..0000000000 --- a/tests/profiling/gatordmock/GatordMockService.cpp +++ /dev/null @@ -1,485 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "GatordMockService.hpp" - -#include <CommandHandlerRegistry.hpp> -#include <PacketVersionResolver.hpp> -#include <ProfilingUtils.hpp> -#include <NetworkSockets.hpp> - -#include <cerrno> -#include <fcntl.h> -#include <iomanip> -#include <iostream> -#include <string> - -using namespace armnnUtils; - -namespace armnn -{ - -namespace gatordmock -{ - -bool GatordMockService::OpenListeningSocket(std::string udsNamespace) -{ - Sockets::Initialize(); - m_ListeningSocket = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); - if (-1 == m_ListeningSocket) - { - std::cerr << ": Socket construction failed: " << strerror(errno) << std::endl; - return false; - } - - sockaddr_un udsAddress; - memset(&udsAddress, 0, sizeof(sockaddr_un)); - // We've set the first element of sun_path to be 0, skip over it and copy the namespace after it. - memcpy(udsAddress.sun_path + 1, udsNamespace.c_str(), strlen(udsNamespace.c_str())); - udsAddress.sun_family = AF_UNIX; - - // Bind the socket to the UDS namespace. - if (-1 == bind(m_ListeningSocket, reinterpret_cast<const sockaddr*>(&udsAddress), sizeof(sockaddr_un))) - { - std::cerr << ": Binding on socket failed: " << strerror(errno) << std::endl; - return false; - } - // Listen for 1 connection. - if (-1 == listen(m_ListeningSocket, 1)) - { - std::cerr << ": Listen call on socket failed: " << strerror(errno) << std::endl; - return false; - } - return true; -} - -Sockets::Socket GatordMockService::BlockForOneClient() -{ - m_ClientConnection = Sockets::Accept(m_ListeningSocket, nullptr, nullptr, SOCK_CLOEXEC); - if (-1 == m_ClientConnection) - { - std::cerr << ": Failure when waiting for a client connection: " << strerror(errno) << std::endl; - return -1; - } - return m_ClientConnection; -} - -bool GatordMockService::WaitForStreamMetaData() -{ - if (m_EchoPackets) - { - std::cout << "Waiting for stream meta data..." << std::endl; - } - // The start of the stream metadata is 2x32bit words, 0 and packet length. - uint8_t header[8]; - if (!ReadFromSocket(header, 8)) - { - return false; - } - EchoPacket(PacketDirection::ReceivedHeader, header, 8); - // The first word, stream_metadata_identifer, should always be 0. - if (ToUint32(&header[0], TargetEndianness::BeWire) != 0) - { - std::cerr << ": Protocol error. The stream_metadata_identifer was not 0." << std::endl; - return false; - } - - uint8_t pipeMagic[4]; - if (!ReadFromSocket(pipeMagic, 4)) - { - return false; - } - 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) == PIPE_MAGIC) - { - m_Endianness = TargetEndianness::BeWire; - } - 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; - return false; - } - // Now we know the endianness we can get the length from the header. - // Remember we already read the pipe magic 4 bytes. - uint32_t metaDataLength = ToUint32(&header[4], m_Endianness) - 4; - // Read the entire packet. - std::vector<uint8_t> packetData(metaDataLength); - if (metaDataLength != - boost::numeric_cast<uint32_t>(Sockets::Read(m_ClientConnection, packetData.data(), metaDataLength))) - { - std::cerr << ": Protocol read error. Data length mismatch." << std::endl; - return false; - } - EchoPacket(PacketDirection::ReceivedData, packetData.data(), metaDataLength); - m_StreamMetaDataVersion = ToUint32(&packetData[0], m_Endianness); - m_StreamMetaDataMaxDataLen = ToUint32(&packetData[4], m_Endianness); - m_StreamMetaDataPid = ToUint32(&packetData[8], m_Endianness); - - return true; -} - -void GatordMockService::SendConnectionAck() -{ - if (m_EchoPackets) - { - std::cout << "Sending connection acknowledgement." << std::endl; - } - // The connection ack packet is an empty data packet with packetId == 1. - SendPacket(0, 1, nullptr, 0); -} - -void GatordMockService::SendRequestCounterDir() -{ - if (m_EchoPackets) - { - std::cout << "Sending connection acknowledgement." << std::endl; - } - // The connection ack packet is an empty data packet with packetId == 1. - SendPacket(0, 3, nullptr, 0); -} - -bool GatordMockService::LaunchReceivingThread() -{ - if (m_EchoPackets) - { - std::cout << "Launching receiving thread." << std::endl; - } - // At this point we want to make the socket non blocking. - if (!Sockets::SetNonBlocking(m_ClientConnection)) - { - Sockets::Close(m_ClientConnection); - std::cerr << "Failed to set socket as non blocking: " << strerror(errno) << std::endl; - return false; - } - m_ListeningThread = std::thread(&GatordMockService::ReceiveLoop, this, std::ref(*this)); - return true; -} - -void GatordMockService::WaitForReceivingThread() -{ - // The receiving thread may already have died. - if (m_CloseReceivingThread != true) - { - m_CloseReceivingThread.store(true); - } - // Check that the receiving thread is running - if (m_ListeningThread.joinable()) - { - // Wait for the receiving thread to complete operations - m_ListeningThread.join(); - } -} - -void GatordMockService::SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters) -{ - // The packet body consists of a UINT32 representing the period following by zero or more - // UINT16's representing counter UID's. If the list is empty it implies all counters are to - // be disabled. - - if (m_EchoPackets) - { - std::cout << "SendPeriodicCounterSelectionList: Period=" << std::dec << period << "uSec" << std::endl; - std::cout << "List length=" << counters.size() << std::endl; - ; - } - // Start by calculating the length of the packet body in bytes. This will be at least 4. - uint32_t dataLength = static_cast<uint32_t>(4 + (counters.size() * 2)); - - std::unique_ptr<unsigned char[]> uniqueData = std::make_unique<unsigned char[]>(dataLength); - unsigned char* data = reinterpret_cast<unsigned char*>(uniqueData.get()); - - uint32_t offset = 0; - profiling::WriteUint32(data, offset, period); - offset += 4; - for (std::vector<uint16_t>::iterator it = counters.begin(); it != counters.end(); ++it) - { - profiling::WriteUint16(data, offset, *it); - offset += 2; - } - - // Send the packet. - SendPacket(0, 4, data, dataLength); - // There will be an echo response packet sitting in the receive thread. PeriodicCounterSelectionResponseHandler - // should deal with it. -} - -void GatordMockService::WaitCommand(uint32_t timeout) -{ - // Wait for a maximum of timeout microseconds or if the receive thread has closed. - // There is a certain level of rounding involved in this timing. - uint32_t iterations = timeout / 1000; - std::cout << std::dec << "Wait command with timeout of " << timeout << " iterations = " << iterations << std::endl; - uint32_t count = 0; - while ((this->ReceiveThreadRunning() && (count < iterations))) - { - std::this_thread::sleep_for(std::chrono::microseconds(1000)); - ++count; - } - if (m_EchoPackets) - { - std::cout << std::dec << "Wait command with timeout of " << timeout << " microseconds completed. " << std::endl; - } -} - -void GatordMockService::ReceiveLoop(GatordMockService& mockService) -{ - m_CloseReceivingThread.store(false); - while (!m_CloseReceivingThread.load()) - { - try - { - armnn::profiling::Packet packet = mockService.WaitForPacket(500); - } - catch (const armnn::TimeoutException&) - { - // In this case we ignore timeouts and and keep trying to receive. - } - catch (const armnn::InvalidArgumentException& e) - { - // We couldn't find a functor to handle the packet? - std::cerr << "Packet received that could not be processed: " << e.what() << std::endl; - } - catch (const armnn::RuntimeException& e) - { - // A runtime exception occurred which means we must exit the loop. - std::cerr << "Receive thread closing: " << e.what() << std::endl; - m_CloseReceivingThread.store(true); - } - } -} - -armnn::profiling::Packet GatordMockService::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); - if (bytes_available > 8) - { - // Yes there is. Read it: - return ReceivePacket(); - } - else - { - // 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<int>(timeoutMs)); - - switch (pollResult) - { - // Error - case -1: - throw armnn::RuntimeException(std::string("File descriptor reported an error during polling: ") + - strerror(errno)); - - // Timeout - case 0: - throw armnn::TimeoutException("Timeout while waiting to receive packet."); - - // Normal poll return. It could still contain an error signal - default: - // Check if the socket reported an error - if (pollingFd[0].revents & (POLLNVAL | POLLERR | POLLHUP)) - { - if (pollingFd[0].revents == POLLNVAL) - { - throw armnn::RuntimeException(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)); - } - if (pollingFd[0].revents == POLLHUP) - { - throw armnn::RuntimeException(std::string("Connection closed by remote client: POLLHUP")); - } - } - - // Check if there is data to read - if (!(pollingFd[0].revents & (POLLIN))) - { - // 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."); - } - return ReceivePacket(); - } - } -} - -armnn::profiling::Packet GatordMockService::ReceivePacket() -{ - uint32_t header[2]; - if (!ReadHeader(header)) - { - return armnn::profiling::Packet(); - } - // Read data_length bytes from the socket. - std::unique_ptr<unsigned char[]> uniquePacketData = std::make_unique<unsigned char[]>(header[1]); - unsigned char* packetData = reinterpret_cast<unsigned char*>(uniquePacketData.get()); - - if (!ReadFromSocket(packetData, header[1])) - { - return armnn::profiling::Packet(); - } - - EchoPacket(PacketDirection::ReceivedData, packetData, header[1]); - - // Construct received packet - armnn::profiling::PacketVersionResolver packetVersionResolver; - armnn::profiling::Packet packetRx = armnn::profiling::Packet(header[0], header[1], uniquePacketData); - if (m_EchoPackets) - { - std::cout << "Processing packet ID= " << packetRx.GetPacketId() << " Length=" << packetRx.GetLength() - << std::endl; - } - - profiling::Version version = - packetVersionResolver.ResolvePacketVersion(packetRx.GetPacketFamily(), packetRx.GetPacketId()); - - profiling::CommandHandlerFunctor* commandHandlerFunctor = - m_HandlerRegistry.GetFunctor(packetRx.GetPacketFamily(), packetRx.GetPacketId(), version.GetEncodedValue()); - BOOST_ASSERT(commandHandlerFunctor); - commandHandlerFunctor->operator()(packetRx); - return packetRx; -} - -bool GatordMockService::SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength) -{ - // Construct a packet from the id and data given and send it to the client. - // Encode the header. - uint32_t header[2]; - header[0] = packetFamily << 26 | packetId << 16; - header[1] = dataLength; - // Add the header to the packet. - std::vector<uint8_t> packet(8 + dataLength); - InsertU32(header[0], packet.data(), m_Endianness); - InsertU32(header[1], packet.data() + 4, m_Endianness); - // And the rest of the data if there is any. - if (dataLength > 0) - { - memcpy((packet.data() + 8), data, dataLength); - } - EchoPacket(PacketDirection::Sending, packet.data(), packet.size()); - if (-1 == Sockets::Write(m_ClientConnection, packet.data(), packet.size())) - { - std::cerr << ": Failure when writing to client socket: " << strerror(errno) << std::endl; - return false; - } - return true; -} - -bool GatordMockService::ReadHeader(uint32_t headerAsWords[2]) -{ - // The header will always be 2x32bit words. - uint8_t header[8]; - if (!ReadFromSocket(header, 8)) - { - return false; - } - EchoPacket(PacketDirection::ReceivedHeader, header, 8); - headerAsWords[0] = ToUint32(&header[0], m_Endianness); - headerAsWords[1] = ToUint32(&header[4], m_Endianness); - return true; -} - -bool GatordMockService::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<uint32_t>(totalBytesRead) < expectedLength) - { - long bytesRead = Sockets::Read(m_ClientConnection, packetData, expectedLength); - if (bytesRead < 0) - { - std::cerr << ": Failure when reading from client socket: " << strerror(errno) << std::endl; - return false; - } - if (bytesRead == 0) - { - std::cerr << ": EOF while reading from client socket." << std::endl; - return false; - } - totalBytesRead += bytesRead; - } - return true; -}; - -void GatordMockService::EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes) -{ - // If enabled print the contents of the data packet to the console. - if (m_EchoPackets) - { - if (direction == PacketDirection::Sending) - { - std::cout << "TX " << std::dec << lengthInBytes << " bytes : "; - } - else if (direction == PacketDirection::ReceivedHeader) - { - std::cout << "RX Header " << std::dec << lengthInBytes << " bytes : "; - } - else - { - std::cout << "RX Data " << std::dec << lengthInBytes << " bytes : "; - } - for (unsigned int i = 0; i < lengthInBytes; i++) - { - if ((i % 10) == 0) - { - std::cout << std::endl; - } - std::cout << "0x" << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned int>(packet[i]) - << " "; - } - std::cout << std::endl; - } -} - -uint32_t GatordMockService::ToUint32(uint8_t* data, TargetEndianness endianness) -{ - // Extract the first 4 bytes starting at data and push them into a 32bit integer based on the - // specified endianness. - if (endianness == TargetEndianness::BeWire) - { - return static_cast<uint32_t>(data[0]) << 24 | static_cast<uint32_t>(data[1]) << 16 | - static_cast<uint32_t>(data[2]) << 8 | static_cast<uint32_t>(data[3]); - } - else - { - return static_cast<uint32_t>(data[3]) << 24 | static_cast<uint32_t>(data[2]) << 16 | - static_cast<uint32_t>(data[1]) << 8 | static_cast<uint32_t>(data[0]); - } -} - -void GatordMockService::InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness) -{ - // Take the bytes of a 32bit integer and copy them into char array starting at data considering - // the endianness value. - if (endianness == TargetEndianness::BeWire) - { - *data = static_cast<uint8_t>((value >> 24) & 0xFF); - *(data + 1) = static_cast<uint8_t>((value >> 16) & 0xFF); - *(data + 2) = static_cast<uint8_t>((value >> 8) & 0xFF); - *(data + 3) = static_cast<uint8_t>(value & 0xFF); - } - else - { - *(data + 3) = static_cast<uint8_t>((value >> 24) & 0xFF); - *(data + 2) = static_cast<uint8_t>((value >> 16) & 0xFF); - *(data + 1) = static_cast<uint8_t>((value >> 8) & 0xFF); - *data = static_cast<uint8_t>(value & 0xFF); - } -} - -} // namespace gatordmock - -} // namespace armnn diff --git a/tests/profiling/gatordmock/GatordMockService.hpp b/tests/profiling/gatordmock/GatordMockService.hpp deleted file mode 100644 index f91e902db8..0000000000 --- a/tests/profiling/gatordmock/GatordMockService.hpp +++ /dev/null @@ -1,158 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - -#include <CommandHandlerRegistry.hpp> -#include <Packet.hpp> -#include <NetworkSockets.hpp> - -#include <atomic> -#include <string> -#include <thread> - -namespace armnn -{ - -namespace gatordmock -{ - -enum class TargetEndianness -{ - BeWire, - LeWire -}; - -enum class PacketDirection -{ - Sending, - ReceivedHeader, - ReceivedData -}; - -/// A class that implements a Mock Gatord server. It will listen on a specified Unix domain socket (UDS) -/// namespace for client connections. It will then allow opertaions to manage coutners while receiving counter data. -class GatordMockService -{ -public: - /// @param registry reference to a command handler registry. - /// @param echoPackets if true the raw packets will be printed to stdout. - GatordMockService(armnn::profiling::CommandHandlerRegistry& registry, bool echoPackets) - : m_HandlerRegistry(registry) - , m_EchoPackets(echoPackets) - , m_CloseReceivingThread(false) - { - m_PacketsReceivedCount.store(0, std::memory_order_relaxed); - } - - ~GatordMockService() - { - // We have set SOCK_CLOEXEC on these sockets but we'll close them to be good citizens. - armnnUtils::Sockets::Close(m_ClientConnection); - armnnUtils::Sockets::Close(m_ListeningSocket); - } - - /// Establish the Unix domain socket and set it to listen for connections. - /// @param udsNamespace the namespace (socket address) associated with the listener. - /// @return true only if the socket has been correctly setup. - bool OpenListeningSocket(std::string udsNamespace); - - /// Block waiting to accept one client to connect to the UDS. - /// @return the file descriptor of the client connection. - armnnUtils::Sockets::Socket BlockForOneClient(); - - /// 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 met data packet has been received. - bool WaitForStreamMetaData(); - - /// Send a connection acknowledged packet back to the client. - void SendConnectionAck(); - - /// Send a request counter directory packet back to the client. - void SendRequestCounterDir(); - - /// Start the thread that will receive all packets and print them nicely to stdout. - bool LaunchReceivingThread(); - - /// Return the total number of periodic counter capture packets received since the receive thread started. - /// @return number of periodic counter capture packets received. - uint32_t GetPacketsReceivedCount() - { - return m_PacketsReceivedCount.load(std::memory_order_acquire); - } - - /// This is a placeholder method to prevent main exiting. It can be removed once the - /// command handling code is added. - void WaitForReceivingThread(); - - // @return true only if the receive thread is closed or closing. - bool ReceiveThreadRunning() - { - return !m_CloseReceivingThread.load(); - } - - /// Send the counter list to ArmNN. - void SendPeriodicCounterSelectionList(uint32_t period, std::vector<uint16_t> counters); - - /// Execute the WAIT command from the comamnd file. - void WaitCommand(uint32_t timeout); - - uint32_t GetStreamMetadataVersion() - { - return m_StreamMetaDataVersion; - } - - uint32_t GetStreamMetadataMaxDataLen() - { - return m_StreamMetaDataMaxDataLen; - } - - uint32_t GetStreamMetadataPid() - { - return m_StreamMetaDataPid; - } - -private: - void ReceiveLoop(GatordMockService& mockService); - - /// Block on the client connection until a complete packet has been received. This is a placeholder function to - /// enable early testing of the tool. - /// @return true if a valid packet has been received. - armnn::profiling::Packet WaitForPacket(uint32_t timeoutMs); - - armnn::profiling::Packet ReceivePacket(); - - bool SendPacket(uint32_t packetFamily, uint32_t packetId, const uint8_t* data, uint32_t dataLength); - - void EchoPacket(PacketDirection direction, uint8_t* packet, size_t lengthInBytes); - - bool ReadHeader(uint32_t headerAsWords[2]); - - bool ReadFromSocket(uint8_t* packetData, uint32_t expectedLength); - - uint32_t ToUint32(uint8_t* data, TargetEndianness endianness); - - void InsertU32(uint32_t value, uint8_t* data, TargetEndianness endianness); - - static const uint32_t PIPE_MAGIC = 0x45495434; - - std::atomic<uint32_t> m_PacketsReceivedCount; - TargetEndianness m_Endianness; - uint32_t m_StreamMetaDataVersion; - uint32_t m_StreamMetaDataMaxDataLen; - uint32_t m_StreamMetaDataPid; - - armnn::profiling::CommandHandlerRegistry& m_HandlerRegistry; - - bool m_EchoPackets; - armnnUtils::Sockets::Socket m_ListeningSocket; - armnnUtils::Sockets::Socket m_ClientConnection; - std::thread m_ListeningThread; - std::atomic<bool> m_CloseReceivingThread; -}; -} // namespace gatordmock - -} // namespace armnn diff --git a/tests/profiling/gatordmock/PeriodicCounterCaptureCommandHandler.cpp b/tests/profiling/gatordmock/PeriodicCounterCaptureCommandHandler.cpp deleted file mode 100644 index 9dd7064c90..0000000000 --- a/tests/profiling/gatordmock/PeriodicCounterCaptureCommandHandler.cpp +++ /dev/null @@ -1,128 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "PeriodicCounterCaptureCommandHandler.hpp" - -#include <ProfilingUtils.hpp> - -#include <boost/numeric/conversion/cast.hpp> - -#include <iostream> - -namespace armnn -{ - -namespace gatordmock -{ - -using boost::numeric_cast; - -void PeriodicCounterCaptureCommandHandler::ParseData(const armnn::profiling::Packet& packet) -{ - std::vector<uint16_t> counterIds; - std::vector<uint32_t> counterValues; - - uint32_t sizeOfUint64 = numeric_cast<uint32_t>(sizeof(uint64_t)); - uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t)); - uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t)); - - uint32_t offset = 0; - - if (packet.GetLength() >= 8) - { - offset = 0; - - uint64_t timestamp = profiling::ReadUint64(reinterpret_cast<const unsigned char*>(packet.GetData()), offset); - - if (m_FirstTimestamp == 0) // detect the first timestamp we receive. - { - m_FirstTimestamp = timestamp; - } - else - { - m_SecondTimestamp = timestamp; - m_CurrentPeriodValue = m_SecondTimestamp - m_FirstTimestamp; - m_FirstTimestamp = m_SecondTimestamp; - } - - // Length minus timestamp and header divided by the length of an indexPair - unsigned int counters = (packet.GetLength() - 8) / 6; - - if (counters > 0) - { - counterIds.reserve(counters); - counterValues.reserve(counters); - // Move offset over timestamp area - offset += sizeOfUint64; - for (unsigned int pos = 0; pos < counters; ++pos) - { - counterIds.emplace_back( - profiling::ReadUint16(reinterpret_cast<const unsigned char*>(packet.GetData()), offset)); - offset += sizeOfUint16; - - counterValues.emplace_back( - profiling::ReadUint32(reinterpret_cast<const unsigned char*>(packet.GetData()), offset)); - offset += sizeOfUint32; - } - } - - m_CounterCaptureValues.m_Timestamp = timestamp; - m_CounterCaptureValues.m_Uids = counterIds; - m_CounterCaptureValues.m_Values = counterValues; - } -} - -void PeriodicCounterCaptureCommandHandler::operator()(const profiling::Packet& packet) -{ - ParseData(packet); - if (!m_QuietOperation) // Are we supposed to print to stdout? - { - std::string header, body, uidString, valueString; - - for (uint16_t uid : m_CounterCaptureValues.m_Uids) - { - uidString.append(std::to_string(uid)); - uidString.append(", "); - } - - for (uint32_t val : m_CounterCaptureValues.m_Values) - { - valueString.append(std::to_string(val)); - valueString.append(", "); - } - - body.append(profiling::CentreAlignFormatting(std::to_string(m_CounterCaptureValues.m_Timestamp), 10)); - body.append(" | "); - body.append(profiling::CentreAlignFormatting(std::to_string(m_CurrentPeriodValue), 13)); - body.append(" | "); - body.append(profiling::CentreAlignFormatting(uidString, 10)); - body.append(" | "); - body.append(profiling::CentreAlignFormatting(valueString, 10)); - body.append("\n"); - - if (!m_HeaderPrinted) - { - header.append(profiling::CentreAlignFormatting(" Timestamp", 11)); - header.append(" | "); - header.append(profiling::CentreAlignFormatting("Period (us)", 13)); - header.append(" | "); - header.append(profiling::CentreAlignFormatting("UID's", static_cast<int>(uidString.size()))); - header.append(" | "); - header.append(profiling::CentreAlignFormatting("Values", 10)); - header.append("\n"); - - std::cout << header; - m_HeaderPrinted = true; - } - - std::cout << std::string(body.size(), '-') << "\n"; - - std::cout << body; - } -} - -} // namespace gatordmock - -} // namespace armnn diff --git a/tests/profiling/gatordmock/PeriodicCounterCaptureCommandHandler.hpp b/tests/profiling/gatordmock/PeriodicCounterCaptureCommandHandler.hpp deleted file mode 100644 index 478d0a6ca9..0000000000 --- a/tests/profiling/gatordmock/PeriodicCounterCaptureCommandHandler.hpp +++ /dev/null @@ -1,61 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - -#include <Packet.hpp> -#include <CommandHandlerFunctor.hpp> - -#include <vector> - -namespace armnn -{ - -namespace gatordmock -{ - -struct CounterCaptureValues -{ - uint64_t m_Timestamp; - std::vector<uint16_t> m_Uids; - std::vector<uint32_t> m_Values; -}; - -class PeriodicCounterCaptureCommandHandler : public profiling::CommandHandlerFunctor -{ - -public: - /** - * @param familyId The family of the packets this handler will service - * @param packetId The id of packets this handler will process. - * @param version The version of that id. - * @param quietOperation Optional parameter to turn off printouts. This is useful for unittests. - */ - PeriodicCounterCaptureCommandHandler(uint32_t familyId, - uint32_t packetId, - uint32_t version, - bool quietOperation = false) - : CommandHandlerFunctor(familyId, packetId, version) - , m_QuietOperation(quietOperation) - {} - - void operator()(const armnn::profiling::Packet& packet) override; - - CounterCaptureValues m_CounterCaptureValues; - - uint64_t m_CurrentPeriodValue = 0; - -private: - void ParseData(const armnn::profiling::Packet& packet); - - uint64_t m_FirstTimestamp = 0, m_SecondTimestamp = 0; - - bool m_HeaderPrinted = false; - bool m_QuietOperation; -}; - -} // namespace gatordmock - -} // namespace armnn diff --git a/tests/profiling/gatordmock/PeriodicCounterSelectionResponseHandler.cpp b/tests/profiling/gatordmock/PeriodicCounterSelectionResponseHandler.cpp deleted file mode 100644 index 645b0b3507..0000000000 --- a/tests/profiling/gatordmock/PeriodicCounterSelectionResponseHandler.cpp +++ /dev/null @@ -1,37 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "PeriodicCounterSelectionResponseHandler.hpp" - -#include "../../../src/profiling/ProfilingUtils.hpp" - -#include <iostream> - -namespace armnn -{ - -namespace gatordmock -{ - -void PeriodicCounterSelectionResponseHandler::operator()(const profiling::Packet& packet) -{ - if (!m_QuietOperation) // Are we supposed to print to stdout? - { - uint32_t period = profiling::ReadUint32(reinterpret_cast<const unsigned char*>(packet.GetData()), 0); - uint32_t numCounters = 0; - // First check if there are any counters mentioned. - if(packet.GetLength() > 4) - { - // Length will be 4 bytes for the period and then a list of 16 bit UIDS. - numCounters = ((packet.GetLength() - 4) / 2); - } - std::cout << "PeriodicCounterSelectionResponse: Collection interval = " << std::dec << period << "uSec" - << " Num counters activated = " << numCounters << std::endl; - } -} - -} // namespace gatordmock - -} // namespace armnn
\ No newline at end of file diff --git a/tests/profiling/gatordmock/PeriodicCounterSelectionResponseHandler.hpp b/tests/profiling/gatordmock/PeriodicCounterSelectionResponseHandler.hpp deleted file mode 100644 index 6b82280e81..0000000000 --- a/tests/profiling/gatordmock/PeriodicCounterSelectionResponseHandler.hpp +++ /dev/null @@ -1,45 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <CommandHandlerFunctor.hpp> -#include <Packet.hpp> - -#include <vector> - -namespace armnn -{ - -namespace gatordmock -{ - -#pragma once - -class PeriodicCounterSelectionResponseHandler : public profiling::CommandHandlerFunctor -{ - -public: - /** - * - * @param packetId The id of packets this handler will process. - * @param version The version of that id. - * @param quietOperation Optional parameter to turn off printouts. This is useful for unittests. - */ - PeriodicCounterSelectionResponseHandler(uint32_t familyId, - uint32_t packetId, - uint32_t version, - bool quietOperation = true) - : CommandHandlerFunctor(familyId, packetId, version) - , m_QuietOperation(quietOperation) - {} - - void operator()(const armnn::profiling::Packet& packet) override; - -private: - bool m_QuietOperation; -}; - -} // namespace gatordmock - -} // namespace armnn
\ No newline at end of file diff --git a/tests/profiling/gatordmock/StreamMetadataCommandHandler.cpp b/tests/profiling/gatordmock/StreamMetadataCommandHandler.cpp deleted file mode 100644 index 09255a57a1..0000000000 --- a/tests/profiling/gatordmock/StreamMetadataCommandHandler.cpp +++ /dev/null @@ -1,127 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include "StreamMetadataCommandHandler.hpp" - -#include <ProfilingUtils.hpp> - -#include <boost/cast.hpp> - -#include <sstream> -#include <iostream> - -using namespace armnn::profiling; - -namespace armnn -{ - -namespace gatordmock -{ - -void StreamMetadataCommandHandler::operator()(const Packet& packet) -{ - ParseData(packet); - - if (m_QuietOperation) - { - return; - } - - std::stringstream ss; - - ss << "Stream metadata packet received" << std::endl << std::endl; - - ss << "Pipe magic: " << m_PipeMagic << std::endl; - ss << "Stream metadata version: " << m_StreamMetadataVersion << std::endl; - ss << "Max data len: " << m_MaxDataLen << std::endl; - ss << "Pid: " << m_Pid << std::endl; - ss << "Software info: " << m_SoftwareInfo << std::endl; - ss << "Hardware version: " << m_HardwareVersion << std::endl; - ss << "Software version: " << m_SoftwareVersion << std::endl; - ss << "Process name: " << m_ProcessName << std::endl; - ss << "Packet versions: " << m_PacketVersionTable.size() << std::endl; - - for (const auto& packetVersion : m_PacketVersionTable) - { - ss << "-----------------------" << std::endl; - ss << "Packet family: " << packetVersion.m_PacketFamily << std::endl; - ss << "Packet id: " << packetVersion.m_PacketId << std::endl; - ss << "Packet version: " << packetVersion.m_PacketVersion << std::endl; - } - - std::cout << ss.str() << std::endl; -} - -std::string ReadString(const unsigned char* buffer, unsigned int &offset) -{ - const char* stringPtr = reinterpret_cast<const char*>(&buffer[offset]); - return stringPtr != nullptr ? std::string(stringPtr) : ""; -} - -void StreamMetadataCommandHandler::ParseData(const Packet &packet) -{ - // Check that at least the packet contains the fixed-length fields - if (packet.GetLength() < 80) - { - return; - } - - // Utils - unsigned int uint16_t_size = sizeof(uint16_t); - unsigned int uint32_t_size = sizeof(uint32_t); - - const unsigned char* buffer = packet.GetData(); - unsigned int offset = 0; - - // Get the fixed-length fields - m_PipeMagic = ReadUint32(buffer, offset); - offset += uint32_t_size; - m_StreamMetadataVersion = ReadUint32(buffer, offset); - offset += uint32_t_size; - m_MaxDataLen = ReadUint32(buffer, offset); - offset += uint32_t_size; - m_Pid = ReadUint32(buffer, offset); - offset += uint32_t_size; - m_OffsetInfo = ReadUint32(buffer, offset); - offset += uint32_t_size; - m_OffsetHwVersion = ReadUint32(buffer, offset); - offset += uint32_t_size; - m_OffsetSwVersion = ReadUint32(buffer, offset); - offset += uint32_t_size; - m_OffsetProcessName = ReadUint32(buffer, offset); - offset += uint32_t_size; - m_OffsetPacketVersionTable = ReadUint32(buffer, offset); - offset += uint32_t_size * 2; // Also skipping the reserved word (all zeros) - - // Get the string fields - m_SoftwareInfo = m_OffsetInfo > 0 ? ReadString(buffer, m_OffsetInfo) : ""; - m_HardwareVersion = m_OffsetHwVersion > 0 ? ReadString(buffer, m_OffsetHwVersion) : ""; - m_SoftwareVersion = m_OffsetSwVersion > 0 ? ReadString(buffer, m_OffsetSwVersion) : ""; - m_ProcessName = m_OffsetProcessName > 0 ? ReadString(buffer, m_OffsetProcessName) : ""; - - // Get the packet versions - m_PacketVersionTable.clear(); - if (m_OffsetPacketVersionTable > 0) - { - offset = m_OffsetPacketVersionTable; - uint16_t packetEntries = ReadUint16(buffer, offset + uint16_t_size); - offset += uint32_t_size; // Also skipping the reserved bytes (all zeros) - for (uint16_t i = 0; i < packetEntries; i++) - { - uint16_t packetFamilyAndId = ReadUint16(buffer, offset + uint16_t_size); - uint16_t packetFamily = (packetFamilyAndId >> 10) & 0x003F; - uint16_t packetId = (packetFamilyAndId >> 0) & 0x03FF; - offset += uint32_t_size; // Also skipping the reserved bytes (all zeros) - uint32_t packetVersion = ReadUint32(buffer, offset); - offset += uint32_t_size; - - m_PacketVersionTable.push_back({ packetFamily, packetId, packetVersion }); - } - } -} - -} // namespace gatordmock - -} // namespace armnn diff --git a/tests/profiling/gatordmock/StreamMetadataCommandHandler.hpp b/tests/profiling/gatordmock/StreamMetadataCommandHandler.hpp deleted file mode 100644 index 4558345e67..0000000000 --- a/tests/profiling/gatordmock/StreamMetadataCommandHandler.hpp +++ /dev/null @@ -1,71 +0,0 @@ -// -// Copyright © 2019 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#pragma once - -#include <Packet.hpp> -#include <CommandHandlerFunctor.hpp> - -#include <vector> - -namespace armnn -{ - -namespace gatordmock -{ - -struct PacketVersion -{ - uint16_t m_PacketFamily; - uint16_t m_PacketId; - uint32_t m_PacketVersion; -}; - -class StreamMetadataCommandHandler : public profiling::CommandHandlerFunctor -{ - -public: - /** - * @param familyId The family of the packets this handler will service - * @param packetId The id of packets this handler will process - * @param version The version of that id - * @param quietOperation Optional parameter to turn off printouts. This is useful for unit tests - */ - StreamMetadataCommandHandler(uint32_t familyId, - uint32_t packetId, - uint32_t version, - bool quietOperation = false) - : CommandHandlerFunctor(familyId, packetId, version) - , m_QuietOperation(quietOperation) - {} - - void operator()(const armnn::profiling::Packet& packet) override; - -private: - void ParseData(const armnn::profiling::Packet& packet); - - uint32_t m_PipeMagic; - uint32_t m_StreamMetadataVersion; - uint32_t m_MaxDataLen; - uint32_t m_Pid; - uint32_t m_OffsetInfo; - uint32_t m_OffsetHwVersion; - uint32_t m_OffsetSwVersion; - uint32_t m_OffsetProcessName; - uint32_t m_OffsetPacketVersionTable; - - std::string m_SoftwareInfo; - std::string m_HardwareVersion; - std::string m_SoftwareVersion; - std::string m_ProcessName; - - std::vector<PacketVersion> m_PacketVersionTable; - - bool m_QuietOperation; -}; - -} // namespace gatordmock - -} // namespace armnn diff --git a/tests/profiling/gatordmock/tests/GatordMockTests.cpp b/tests/profiling/gatordmock/tests/GatordMockTests.cpp deleted file mode 100644 index 02adffb2cc..0000000000 --- a/tests/profiling/gatordmock/tests/GatordMockTests.cpp +++ /dev/null @@ -1,295 +0,0 @@ -// -// Copyright © 2017 Arm Ltd. All rights reserved. -// SPDX-License-Identifier: MIT -// - -#include <CommandHandlerRegistry.hpp> -#include <DirectoryCaptureCommandHandler.hpp> -#include <ProfilingService.hpp> -#include <GatordMockService.hpp> -#include <PeriodicCounterCaptureCommandHandler.hpp> -#include <StreamMetadataCommandHandler.hpp> -#include <TimelineDirectoryCaptureCommandHandler.hpp> - -#include <test/ProfilingMocks.hpp> - -#include <boost/cast.hpp> -#include <boost/test/test_tools.hpp> -#include <boost/test/unit_test_suite.hpp> - -BOOST_AUTO_TEST_SUITE(GatordMockTests) - -using namespace armnn; -using namespace std::this_thread; // sleep_for, sleep_until -using namespace std::chrono_literals; - -BOOST_AUTO_TEST_CASE(CounterCaptureHandlingTest) -{ - using boost::numeric_cast; - - profiling::PacketVersionResolver packetVersionResolver; - - // Data with timestamp, counter idx & counter values - std::vector<std::pair<uint16_t, uint32_t>> indexValuePairs; - indexValuePairs.reserve(5); - indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(0, 100)); - indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(1, 200)); - indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(2, 300)); - indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(3, 400)); - indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(4, 500)); - - // ((uint16_t (2 bytes) + uint32_t (4 bytes)) * 5) + word1 + word2 - uint32_t dataLength = 38; - - // Simulate two different packets incoming 500 ms apart - uint64_t time = static_cast<uint64_t>( - std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()) - .count()); - - uint64_t time2 = time + 5000; - - // UniqueData required for Packet class - std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength); - unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData1.get()); - - std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength); - unsigned char* data2 = reinterpret_cast<unsigned char*>(uniqueData2.get()); - - uint32_t sizeOfUint64 = numeric_cast<uint32_t>(sizeof(uint64_t)); - uint32_t sizeOfUint32 = numeric_cast<uint32_t>(sizeof(uint32_t)); - uint32_t sizeOfUint16 = numeric_cast<uint32_t>(sizeof(uint16_t)); - // Offset index to point to mem address - uint32_t offset = 0; - - profiling::WriteUint64(data1, offset, time); - offset += sizeOfUint64; - for (const auto& pair : indexValuePairs) - { - profiling::WriteUint16(data1, offset, pair.first); - offset += sizeOfUint16; - profiling::WriteUint32(data1, offset, pair.second); - offset += sizeOfUint32; - } - - offset = 0; - - profiling::WriteUint64(data2, offset, time2); - offset += sizeOfUint64; - for (const auto& pair : indexValuePairs) - { - profiling::WriteUint16(data2, offset, pair.first); - offset += sizeOfUint16; - profiling::WriteUint32(data2, offset, pair.second); - offset += sizeOfUint32; - } - - uint32_t headerWord1 = packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(); - // Create packet to send through to the command functor - profiling::Packet packet1(headerWord1, dataLength, uniqueData1); - profiling::Packet packet2(headerWord1, dataLength, uniqueData2); - - gatordmock::PeriodicCounterCaptureCommandHandler commandHandler(0, 4, headerWord1, true); - - // Simulate two separate packets coming in to calculate period - commandHandler(packet1); - commandHandler(packet2); - - BOOST_ASSERT(commandHandler.m_CurrentPeriodValue == 5000); - - for (size_t i = 0; i < commandHandler.m_CounterCaptureValues.m_Uids.size(); ++i) - { - BOOST_ASSERT(commandHandler.m_CounterCaptureValues.m_Uids[i] == i); - } -} - -BOOST_AUTO_TEST_CASE(GatorDMockEndToEnd) -{ - // The purpose of this test is to setup both sides of the profiling service and get to the point of receiving - // performance data. - - //These variables are used to wait for the profiling service - uint32_t timeout = 2000; - uint32_t sleepTime = 50; - uint32_t timeSlept = 0; - - profiling::PacketVersionResolver packetVersionResolver; - - // Create the Command Handler Registry - profiling::CommandHandlerRegistry registry; - - // Update with derived functors - gatordmock::StreamMetadataCommandHandler streamMetadataCommandHandler( - 0, 0, packetVersionResolver.ResolvePacketVersion(0, 0).GetEncodedValue(), true); - - gatordmock::PeriodicCounterCaptureCommandHandler counterCaptureCommandHandler( - 0, 4, packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), true); - - profiling::DirectoryCaptureCommandHandler directoryCaptureCommandHandler( - 0, 2, packetVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue(), true); - - gatordmock::TimelineDirectoryCaptureCommandHandler timelineDirectoryCaptureCommandHandler( - 1, 0, packetVersionResolver.ResolvePacketVersion(1, 0).GetEncodedValue(), true); - - // Register different derived functors - registry.RegisterFunctor(&streamMetadataCommandHandler); - registry.RegisterFunctor(&counterCaptureCommandHandler); - registry.RegisterFunctor(&directoryCaptureCommandHandler); - registry.RegisterFunctor(&timelineDirectoryCaptureCommandHandler); - // Setup the mock service to bind to the UDS. - std::string udsNamespace = "gatord_namespace"; - gatordmock::GatordMockService mockService(registry, false); - mockService.OpenListeningSocket(udsNamespace); - - // Enable the profiling service. - armnn::IRuntime::CreationOptions::ExternalProfilingOptions options; - options.m_EnableProfiling = true; - profiling::ProfilingService& profilingService = profiling::ProfilingService::Instance(); - profilingService.ResetExternalProfilingOptions(options, true); - - // Bring the profiling service to the "WaitingForAck" state - BOOST_CHECK(profilingService.GetCurrentState() == profiling::ProfilingState::Uninitialised); - profilingService.Update(); - BOOST_CHECK(profilingService.GetCurrentState() == profiling::ProfilingState::NotConnected); - profilingService.Update(); - - // Connect the profiling service to the mock Gatord. - int clientFd = mockService.BlockForOneClient(); - if (-1 == clientFd) - { - BOOST_FAIL("Failed to connect client"); - } - - // Give the profiling service sending thread time start executing and send the stream metadata. - while (profilingService.GetCurrentState() != profiling::ProfilingState::WaitingForAck) - { - if (timeSlept >= timeout) - { - BOOST_FAIL("Timeout: Profiling service did not switch to WaitingForAck state"); - } - std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); - timeSlept += sleepTime; - } - - profilingService.Update(); - // Read the stream metadata on the mock side. - if (!mockService.WaitForStreamMetaData()) - { - BOOST_FAIL("Failed to receive StreamMetaData"); - } - // Send Ack from GatorD - mockService.SendConnectionAck(); - - timeSlept = 0; - while (profilingService.GetCurrentState() != profiling::ProfilingState::Active) - { - if (timeSlept >= timeout) - { - BOOST_FAIL("Timeout: Profiling service did not switch to Active state"); - } - std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); - timeSlept += sleepTime; - } - - mockService.LaunchReceivingThread(); - // As part of the default startup of the profiling service a counter directory packet will be sent. - timeSlept = 0; - while (!directoryCaptureCommandHandler.ParsedCounterDirectory()) - { - if (timeSlept >= timeout) - { - BOOST_FAIL("Timeout: MockGatord did not receive counter directory packet"); - } - std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); - timeSlept += sleepTime; - } - - const profiling::ICounterDirectory& serviceCounterDirectory = profilingService.GetCounterDirectory(); - const profiling::ICounterDirectory& receivedCounterDirectory = directoryCaptureCommandHandler.GetCounterDirectory(); - - // Compare thre basics of the counter directory from the service and the one we received over the wire. - BOOST_ASSERT(serviceCounterDirectory.GetDeviceCount() == receivedCounterDirectory.GetDeviceCount()); - BOOST_ASSERT(serviceCounterDirectory.GetCounterSetCount() == receivedCounterDirectory.GetCounterSetCount()); - BOOST_ASSERT(serviceCounterDirectory.GetCategoryCount() == receivedCounterDirectory.GetCategoryCount()); - BOOST_ASSERT(serviceCounterDirectory.GetCounterCount() == receivedCounterDirectory.GetCounterCount()); - - receivedCounterDirectory.GetDeviceCount(); - serviceCounterDirectory.GetDeviceCount(); - - const profiling::Devices& serviceDevices = serviceCounterDirectory.GetDevices(); - for (auto& device : serviceDevices) - { - // Find the same device in the received counter directory. - auto foundDevice = receivedCounterDirectory.GetDevices().find(device.second->m_Uid); - BOOST_CHECK(foundDevice != receivedCounterDirectory.GetDevices().end()); - BOOST_CHECK(device.second->m_Name.compare((*foundDevice).second->m_Name) == 0); - BOOST_CHECK(device.second->m_Cores == (*foundDevice).second->m_Cores); - } - - const profiling::CounterSets& serviceCounterSets = serviceCounterDirectory.GetCounterSets(); - for (auto& counterSet : serviceCounterSets) - { - // Find the same counter set in the received counter directory. - auto foundCounterSet = receivedCounterDirectory.GetCounterSets().find(counterSet.second->m_Uid); - BOOST_CHECK(foundCounterSet != receivedCounterDirectory.GetCounterSets().end()); - BOOST_CHECK(counterSet.second->m_Name.compare((*foundCounterSet).second->m_Name) == 0); - BOOST_CHECK(counterSet.second->m_Count == (*foundCounterSet).second->m_Count); - } - - const profiling::Categories& serviceCategories = serviceCounterDirectory.GetCategories(); - for (auto& category : serviceCategories) - { - for (auto& receivedCategory : receivedCounterDirectory.GetCategories()) - { - if (receivedCategory->m_Name.compare(category->m_Name) == 0) - { - // We've found the matching category. - BOOST_CHECK(category->m_DeviceUid == receivedCategory->m_DeviceUid); - BOOST_CHECK(category->m_CounterSetUid == receivedCategory->m_CounterSetUid); - // Now look at the interiors of the counters. Start by sorting them. - std::sort(category->m_Counters.begin(), category->m_Counters.end()); - std::sort(receivedCategory->m_Counters.begin(), receivedCategory->m_Counters.end()); - // When comparing uid's here we need to translate them. - std::function<bool(const uint16_t&, const uint16_t&)> comparator = - [&directoryCaptureCommandHandler](const uint16_t& first, const uint16_t& second) { - uint16_t translated = directoryCaptureCommandHandler.TranslateUIDCopyToOriginal(second); - if (translated == first) - { - return true; - } - return false; - }; - // Then let vector == do the work. - BOOST_CHECK(std::equal(category->m_Counters.begin(), category->m_Counters.end(), - receivedCategory->m_Counters.begin(), comparator)); - break; - } - } - } - - // Finally check the content of the counters. - const profiling::Counters& receivedCounters = receivedCounterDirectory.GetCounters(); - for (auto& receivedCounter : receivedCounters) - { - // Translate the Uid and find the corresponding counter in the original counter directory. - // Note we can't check m_MaxCounterUid here as it will likely differ between the two counter directories. - uint16_t translated = directoryCaptureCommandHandler.TranslateUIDCopyToOriginal(receivedCounter.first); - const profiling::Counter* serviceCounter = serviceCounterDirectory.GetCounter(translated); - BOOST_CHECK(serviceCounter->m_DeviceUid == receivedCounter.second->m_DeviceUid); - BOOST_CHECK(serviceCounter->m_Name.compare(receivedCounter.second->m_Name) == 0); - BOOST_CHECK(serviceCounter->m_CounterSetUid == receivedCounter.second->m_CounterSetUid); - BOOST_CHECK(serviceCounter->m_Multiplier == receivedCounter.second->m_Multiplier); - BOOST_CHECK(serviceCounter->m_Interpolation == receivedCounter.second->m_Interpolation); - BOOST_CHECK(serviceCounter->m_Class == receivedCounter.second->m_Class); - BOOST_CHECK(serviceCounter->m_Units.compare(receivedCounter.second->m_Units) == 0); - BOOST_CHECK(serviceCounter->m_Description.compare(receivedCounter.second->m_Description) == 0); - } - - mockService.WaitForReceivingThread(); - options.m_EnableProfiling = false; - profilingService.ResetExternalProfilingOptions(options, true); - - // Future tests here will add counters to the ProfilingService, increment values and examine - // PeriodicCounterCapture data received. These are yet to be integrated. -} - -BOOST_AUTO_TEST_SUITE_END() |