aboutsummaryrefslogtreecommitdiff
path: root/tests/profiling/gatordmock/StreamMetadataCommandHandler.cpp
blob: 09255a57a1d33b4d7c85b5d81b8801b16f7295a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// 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