Matteo Martincigh | cdfb941 | 2019-11-08 11:23:06 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "StreamMetadataCommandHandler.hpp" |
| 7 | |
| 8 | #include <ProfilingUtils.hpp> |
| 9 | |
| 10 | #include <boost/cast.hpp> |
| 11 | |
| 12 | #include <sstream> |
| 13 | #include <iostream> |
| 14 | |
| 15 | using namespace armnn::profiling; |
| 16 | |
| 17 | namespace armnn |
| 18 | { |
| 19 | |
| 20 | namespace gatordmock |
| 21 | { |
| 22 | |
| 23 | void StreamMetadataCommandHandler::operator()(const Packet& packet) |
| 24 | { |
| 25 | ParseData(packet); |
| 26 | |
| 27 | if (m_QuietOperation) |
| 28 | { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | std::stringstream ss; |
| 33 | |
| 34 | ss << "Stream metadata packet received" << std::endl << std::endl; |
| 35 | |
| 36 | ss << "Pipe magic: " << m_PipeMagic << std::endl; |
| 37 | ss << "Stream metadata version: " << m_StreamMetadataVersion << std::endl; |
| 38 | ss << "Max data len: " << m_MaxDataLen << std::endl; |
| 39 | ss << "Pid: " << m_Pid << std::endl; |
| 40 | ss << "Software info: " << m_SoftwareInfo << std::endl; |
| 41 | ss << "Hardware version: " << m_HardwareVersion << std::endl; |
| 42 | ss << "Software version: " << m_SoftwareVersion << std::endl; |
| 43 | ss << "Process name: " << m_ProcessName << std::endl; |
| 44 | ss << "Packet versions: " << m_PacketVersionTable.size() << std::endl; |
| 45 | |
| 46 | for (const auto& packetVersion : m_PacketVersionTable) |
| 47 | { |
| 48 | ss << "-----------------------" << std::endl; |
| 49 | ss << "Packet family: " << packetVersion.m_PacketFamily << std::endl; |
| 50 | ss << "Packet id: " << packetVersion.m_PacketId << std::endl; |
| 51 | ss << "Packet version: " << packetVersion.m_PacketVersion << std::endl; |
| 52 | } |
| 53 | |
| 54 | std::cout << ss.str() << std::endl; |
| 55 | } |
| 56 | |
| 57 | std::string ReadString(const unsigned char* buffer, unsigned int &offset) |
| 58 | { |
| 59 | const char* stringPtr = reinterpret_cast<const char*>(&buffer[offset]); |
| 60 | return stringPtr != nullptr ? std::string(stringPtr) : ""; |
| 61 | } |
| 62 | |
| 63 | void StreamMetadataCommandHandler::ParseData(const Packet &packet) |
| 64 | { |
| 65 | // Check that at least the packet contains the fixed-length fields |
| 66 | if (packet.GetLength() < 80) |
| 67 | { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | // Utils |
| 72 | unsigned int uint16_t_size = sizeof(uint16_t); |
| 73 | unsigned int uint32_t_size = sizeof(uint32_t); |
| 74 | |
| 75 | const unsigned char* buffer = packet.GetData(); |
| 76 | unsigned int offset = 0; |
| 77 | |
| 78 | // Get the fixed-length fields |
| 79 | m_PipeMagic = ReadUint32(buffer, offset); |
| 80 | offset += uint32_t_size; |
| 81 | m_StreamMetadataVersion = ReadUint32(buffer, offset); |
| 82 | offset += uint32_t_size; |
| 83 | m_MaxDataLen = ReadUint32(buffer, offset); |
| 84 | offset += uint32_t_size; |
| 85 | m_Pid = ReadUint32(buffer, offset); |
| 86 | offset += uint32_t_size; |
| 87 | m_OffsetInfo = ReadUint32(buffer, offset); |
| 88 | offset += uint32_t_size; |
| 89 | m_OffsetHwVersion = ReadUint32(buffer, offset); |
| 90 | offset += uint32_t_size; |
| 91 | m_OffsetSwVersion = ReadUint32(buffer, offset); |
| 92 | offset += uint32_t_size; |
| 93 | m_OffsetProcessName = ReadUint32(buffer, offset); |
| 94 | offset += uint32_t_size; |
| 95 | m_OffsetPacketVersionTable = ReadUint32(buffer, offset); |
| 96 | offset += uint32_t_size * 2; // Also skipping the reserved word (all zeros) |
| 97 | |
| 98 | // Get the string fields |
| 99 | m_SoftwareInfo = m_OffsetInfo > 0 ? ReadString(buffer, m_OffsetInfo) : ""; |
| 100 | m_HardwareVersion = m_OffsetHwVersion > 0 ? ReadString(buffer, m_OffsetHwVersion) : ""; |
| 101 | m_SoftwareVersion = m_OffsetSwVersion > 0 ? ReadString(buffer, m_OffsetSwVersion) : ""; |
| 102 | m_ProcessName = m_OffsetProcessName > 0 ? ReadString(buffer, m_OffsetProcessName) : ""; |
| 103 | |
| 104 | // Get the packet versions |
| 105 | m_PacketVersionTable.clear(); |
| 106 | if (m_OffsetPacketVersionTable > 0) |
| 107 | { |
| 108 | offset = m_OffsetPacketVersionTable; |
| 109 | uint16_t packetEntries = ReadUint16(buffer, offset + uint16_t_size); |
| 110 | offset += uint32_t_size; // Also skipping the reserved bytes (all zeros) |
| 111 | for (uint16_t i = 0; i < packetEntries; i++) |
| 112 | { |
| 113 | uint16_t packetFamilyAndId = ReadUint16(buffer, offset + uint16_t_size); |
| 114 | uint16_t packetFamily = (packetFamilyAndId >> 10) & 0x003F; |
| 115 | uint16_t packetId = (packetFamilyAndId >> 0) & 0x03FF; |
| 116 | offset += uint32_t_size; // Also skipping the reserved bytes (all zeros) |
| 117 | uint32_t packetVersion = ReadUint32(buffer, offset); |
| 118 | offset += uint32_t_size; |
| 119 | |
| 120 | m_PacketVersionTable.push_back({ packetFamily, packetId, packetVersion }); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | } // namespace gatordmock |
| 126 | |
| 127 | } // namespace armnn |