• 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 <common/include/CommonProfilingUtils.hpp>
7 #include <common/include/Logging.hpp>
8 #include <server/include/timelineDecoder/TimelineCaptureCommandHandler.hpp>
9 
10 #include <string>
11 
12 namespace arm
13 {
14 
15 namespace pipe
16 {
17 
18 //Array of member functions, the array index matches the decl_id
19 const TimelineCaptureCommandHandler::ReadFunction TimelineCaptureCommandHandler::m_ReadFunctions[]
20 {
21     &TimelineCaptureCommandHandler::ReadLabel,              // Label decl_id = 0
22     &TimelineCaptureCommandHandler::ReadEntity,             // Entity decl_id = 1
23     &TimelineCaptureCommandHandler::ReadEventClass,         // EventClass decl_id = 2
24     &TimelineCaptureCommandHandler::ReadRelationship,       // Relationship decl_id = 3
25     &TimelineCaptureCommandHandler::ReadEvent               // Event decl_id = 4
26 };
27 
SetThreadIdSize(uint32_t size)28 void TimelineCaptureCommandHandler::SetThreadIdSize(uint32_t size)
29 {
30     m_ThreadIdSize = size;
31 }
32 
operator ()(const arm::pipe::Packet & packet)33 void TimelineCaptureCommandHandler::operator()(const arm::pipe::Packet& packet)
34 {
35     ParseData(packet);
36 }
37 
ParseData(const arm::pipe::Packet & packet)38 void TimelineCaptureCommandHandler::ParseData(const arm::pipe::Packet& packet)
39 {
40     uint32_t offset = 0;
41     m_PacketLength = packet.GetLength();
42 
43     // We are expecting TimelineDirectoryCaptureCommandHandler to set the thread id size
44     // if it not set in the constructor
45     if (m_ThreadIdSize == 0)
46     {
47         ARM_PIPE_LOG(error) << "TimelineCaptureCommandHandler: m_ThreadIdSize has not been set";
48         return;
49     }
50 
51     if (packet.GetLength() < 8)
52     {
53         return;
54     }
55 
56     const unsigned char* data = reinterpret_cast<const unsigned char*>(packet.GetData());
57 
58     uint32_t declId = 0;
59 
60     while ( offset < m_PacketLength )
61     {
62         declId = arm::pipe::ReadUint32(data, offset);
63         offset += uint32_t_size;
64 
65         ITimelineDecoder::TimelineStatus status = (this->*m_ReadFunctions[declId])(data, offset);
66         if (status == ITimelineDecoder::TimelineStatus::TimelineStatus_Fail)
67         {
68             ARM_PIPE_LOG(error) << "Decode of timeline message type [" << declId <<
69                                 "] at offset [" << offset << "] failed";
70             break;
71         }
72     }
73 }
74 
ReadLabel(const unsigned char * data,uint32_t & offset)75 ITimelineDecoder::TimelineStatus TimelineCaptureCommandHandler::ReadLabel(const unsigned char* data, uint32_t& offset)
76 {
77     ITimelineDecoder::Label label;
78     label.m_Guid = arm::pipe::ReadUint64(data, offset);
79     offset += uint64_t_size;
80 
81     uint32_t nameLength = arm::pipe::ReadUint32(data, offset);
82     offset += uint32_t_size;
83 
84     uint32_t i = 0;
85     // nameLength - 1 to account for null operator \0
86     for ( i = 0; i < nameLength - 1; ++i )
87     {
88         label.m_Name += static_cast<char>(arm::pipe::ReadUint8(data, offset + i));
89     }
90     // Shift offset past nameLength
91     uint32_t uint32WordAmount = (nameLength / uint32_t_size) + (nameLength % uint32_t_size != 0 ? 1 : 0);
92     offset += uint32WordAmount * uint32_t_size;
93 
94     return m_TimelineDecoder.CreateLabel(label);
95 }
96 
ReadEntity(const unsigned char * data,uint32_t & offset)97 ITimelineDecoder::TimelineStatus TimelineCaptureCommandHandler::ReadEntity(
98         const unsigned char* data, uint32_t& offset)
99 {
100     ITimelineDecoder::Entity entity;
101     entity.m_Guid = arm::pipe::ReadUint64(data, offset);
102     offset += uint64_t_size;
103     return m_TimelineDecoder.CreateEntity(entity);
104 }
105 
ReadEventClass(const unsigned char * data,uint32_t & offset)106 ITimelineDecoder::TimelineStatus TimelineCaptureCommandHandler::ReadEventClass(
107     const unsigned char* data, uint32_t& offset)
108 {
109     ITimelineDecoder::EventClass eventClass;
110     eventClass.m_Guid = arm::pipe::ReadUint64(data, offset);
111     offset += uint64_t_size;
112     eventClass.m_NameGuid = arm::pipe::ReadUint64(data, offset);
113     offset += uint64_t_size;
114     return m_TimelineDecoder.CreateEventClass(eventClass);
115 }
116 
ReadRelationship(const unsigned char * data,uint32_t & offset)117 ITimelineDecoder::TimelineStatus TimelineCaptureCommandHandler::ReadRelationship(
118     const unsigned char* data, uint32_t& offset)
119 {
120     ITimelineDecoder::Relationship relationship;
121     relationship.m_RelationshipType =
122         static_cast<ITimelineDecoder::RelationshipType>(arm::pipe::ReadUint32(data, offset));
123     offset += uint32_t_size;
124 
125     relationship.m_Guid = arm::pipe::ReadUint64(data, offset);
126     offset += uint64_t_size;
127 
128     relationship.m_HeadGuid = arm::pipe::ReadUint64(data, offset);
129     offset += uint64_t_size;
130 
131     relationship.m_TailGuid = arm::pipe::ReadUint64(data, offset);
132     offset += uint64_t_size;
133 
134     relationship.m_AttributeGuid = arm::pipe::ReadUint64(data, offset);
135     offset += uint64_t_size;
136 
137     return m_TimelineDecoder.CreateRelationship(relationship);
138 }
139 
ReadEvent(const unsigned char * data,uint32_t & offset)140 ITimelineDecoder::TimelineStatus TimelineCaptureCommandHandler::ReadEvent(
141     const unsigned char* data, uint32_t& offset)
142 {
143     ITimelineDecoder::Event event;
144     event.m_TimeStamp = arm::pipe::ReadUint64(data, offset);
145     offset += uint64_t_size;
146 
147     if ( m_ThreadIdSize == 4 )
148     {
149         event.m_ThreadId = arm::pipe::ReadUint32(data, offset);
150     }
151     else if ( m_ThreadIdSize == 8 )
152     {
153         event.m_ThreadId = arm::pipe::ReadUint64(data, offset);
154     }
155 
156     offset += m_ThreadIdSize;
157 
158     event.m_Guid = arm::pipe::ReadUint64(data, offset);
159     offset += uint64_t_size;
160 
161     return m_TimelineDecoder.CreateEvent(event);
162 }
163 
164 } //namespace pipe
165 
166 } //namespace arm
167