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