aboutsummaryrefslogtreecommitdiff
path: root/src/timelineDecoder/TimelineCaptureCommandHandler.cpp
blob: 58edd9fc431c5f3b2bc5c43bfe51090b6d2f78dd (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Copyright © 2019 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "TimelineCaptureCommandHandler.hpp"

#include <string>
#include <armnn/Logging.hpp>
namespace armnn
{

namespace timelinedecoder
{

//Array of member functions, the array index matches the decl_id
const TimelineCaptureCommandHandler::ReadFunction TimelineCaptureCommandHandler::m_ReadFunctions[]
{
    &TimelineCaptureCommandHandler::ReadLabel,              // Label decl_id = 0
    &TimelineCaptureCommandHandler::ReadEntity,             // Entity decl_id = 1
    &TimelineCaptureCommandHandler::ReadEventClass,         // EventClass decl_id = 2
    &TimelineCaptureCommandHandler::ReadRelationship,       // Relationship decl_id = 3
    &TimelineCaptureCommandHandler::ReadEvent               // Event decl_id = 4
};

void TimelineCaptureCommandHandler::ParseData(const armnn::profiling::Packet& packet)
{
    uint32_t offset = 0;
    m_PacketLength = packet.GetLength();

    // We are expecting TimelineDirectoryCaptureCommandHandler to set the thread id size
    // if it not set in the constructor
    if (m_ThreadIdSize == 0)
    {
        ARMNN_LOG(error) << "TimelineCaptureCommandHandler: m_ThreadIdSize has not been set";
        return;
    }

    if (packet.GetLength() < 8)
    {
        return;
    }

    const unsigned char* data = reinterpret_cast<const unsigned char*>(packet.GetData());

    uint32_t declId = 0;

    while ( offset < m_PacketLength )
    {
        declId = profiling::ReadUint32(data, offset);
        offset += uint32_t_size;

        (this->*m_ReadFunctions[declId])(data, offset);
    }
}

void TimelineCaptureCommandHandler::ReadLabel(const unsigned char* data, uint32_t& offset)
{
    ITimelineDecoder::Label label;
    label.m_Guid = profiling::ReadUint64(data, offset);
    offset += uint64_t_size;

    uint32_t nameLength = profiling::ReadUint32(data, offset);
    offset += uint32_t_size;

    uint32_t i = 0;
    // nameLength - 1 to account for null operator \0
    for ( i = 0; i < nameLength - 1; ++i )
    {
        label.m_Name += static_cast<char>(profiling::ReadUint8(data, offset + i));
    }
    // Shift offset past nameLength
    uint32_t uint32WordAmount = (nameLength / uint32_t_size) + (nameLength % uint32_t_size != 0 ? 1 : 0);
    offset += uint32WordAmount * uint32_t_size;

    m_TimelineDecoder.CreateLabel(label);
}

void TimelineCaptureCommandHandler::ReadEntity(const unsigned char* data, uint32_t& offset)
{
    ITimelineDecoder::Entity entity;
    entity.m_Guid = profiling::ReadUint64(data, offset);
    offset += uint64_t_size;
    m_TimelineDecoder.CreateEntity(entity);
}

void TimelineCaptureCommandHandler::ReadEventClass(const unsigned char* data, uint32_t& offset)
{
    ITimelineDecoder::EventClass eventClass;
    eventClass.m_Guid = profiling::ReadUint64(data, offset);
    offset += uint64_t_size;
    m_TimelineDecoder.CreateEventClass(eventClass);
}

void TimelineCaptureCommandHandler::ReadRelationship(const unsigned char* data, uint32_t& offset)
{
    ITimelineDecoder::Relationship relationship;
    relationship.m_RelationshipType =
        static_cast<ITimelineDecoder::RelationshipType>(profiling::ReadUint32(data, offset));
    offset += uint32_t_size;

    relationship.m_Guid = profiling::ReadUint64(data, offset);
    offset += uint64_t_size;

    relationship.m_HeadGuid = profiling::ReadUint64(data, offset);
    offset += uint64_t_size;

    relationship.m_TailGuid = profiling::ReadUint64(data, offset);
    offset += uint64_t_size;
    m_TimelineDecoder.CreateRelationship(relationship);
}

void TimelineCaptureCommandHandler::ReadEvent(const unsigned char* data, uint32_t& offset)
{
    ITimelineDecoder::Event event;
    event.m_TimeStamp = profiling::ReadUint64(data, offset);
    offset += uint64_t_size;

    if ( m_ThreadIdSize == 4 )
    {
        event.m_ThreadId = profiling::ReadUint32(data, offset);
    }
    else if ( m_ThreadIdSize == 8 )
    {
        event.m_ThreadId = profiling::ReadUint64(data, offset);
    }

    offset += m_ThreadIdSize;

    event.m_Guid = profiling::ReadUint64(data, offset);
    offset += uint64_t_size;

    m_TimelineDecoder.CreateEvent(event);
}

void TimelineCaptureCommandHandler::SetThreadIdSize(uint32_t size)
{
    m_ThreadIdSize = size;
}

void TimelineCaptureCommandHandler::operator()(const profiling::Packet& packet)
{
    ParseData(packet);
}

} //namespace gatordmock

} //namespace armnn