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 <server/include/timelineDecoder/TimelineDecoder.hpp>
8
9 #include <iostream>
10 #include <sstream>
11
12 namespace arm
13 {
14 namespace pipe
15 {
16
CreateEntity(const Entity & entity)17 TimelineDecoder::TimelineStatus TimelineDecoder::CreateEntity(const Entity &entity)
18 {
19 if (m_OnNewEntityCallback == nullptr)
20 {
21 return TimelineStatus::TimelineStatus_Fail;
22 }
23 ApplyToModel([&](Model& m){
24 m_OnNewEntityCallback(m, entity);
25 });
26 return TimelineStatus::TimelineStatus_Success;
27 }
28
CreateEventClass(const EventClass & eventClass)29 TimelineDecoder::TimelineStatus TimelineDecoder::CreateEventClass(const EventClass &eventClass)
30 {
31 if (m_OnNewEventClassCallback == nullptr)
32 {
33 return TimelineStatus::TimelineStatus_Fail;
34 }
35 ApplyToModel([&](Model& m){
36 m_OnNewEventClassCallback(m, eventClass);
37 });
38
39 return TimelineStatus::TimelineStatus_Success;
40 }
41
CreateEvent(const Event & event)42 TimelineDecoder::TimelineStatus TimelineDecoder::CreateEvent(const Event &event)
43 {
44 if (m_OnNewEventCallback == nullptr)
45 {
46 return TimelineStatus::TimelineStatus_Fail;
47 }
48 ApplyToModel([&](Model& m){
49 m_OnNewEventCallback(m, event);
50 });
51
52 return TimelineStatus::TimelineStatus_Success;
53 }
54
CreateLabel(const Label & label)55 TimelineDecoder::TimelineStatus TimelineDecoder::CreateLabel(const Label &label)
56 {
57 if (m_OnNewLabelCallback == nullptr)
58 {
59 return TimelineStatus::TimelineStatus_Fail;
60 }
61 ApplyToModel([&](Model& m){
62 m_OnNewLabelCallback(m, label);
63 });
64
65 return TimelineStatus::TimelineStatus_Success;
66 }
67
CreateRelationship(const Relationship & relationship)68 TimelineDecoder::TimelineStatus TimelineDecoder::CreateRelationship(const Relationship &relationship)
69 {
70 if (m_OnNewRelationshipCallback == nullptr)
71 {
72 return TimelineStatus::TimelineStatus_Fail;
73 }
74 ApplyToModel([&](Model& m){
75 m_OnNewRelationshipCallback(m, relationship);
76 });
77 return TimelineStatus::TimelineStatus_Success;
78 }
79
SetEntityCallback(OnNewEntityCallback cb)80 TimelineDecoder::TimelineStatus TimelineDecoder::SetEntityCallback(OnNewEntityCallback cb)
81 {
82 if (cb == nullptr)
83 {
84 return TimelineStatus::TimelineStatus_Fail;
85 }
86 m_OnNewEntityCallback = cb;
87 return TimelineStatus::TimelineStatus_Success;
88 }
89
SetEventClassCallback(OnNewEventClassCallback cb)90 TimelineDecoder::TimelineStatus TimelineDecoder::SetEventClassCallback(OnNewEventClassCallback cb)
91 {
92 if (cb == nullptr)
93 {
94 return TimelineStatus::TimelineStatus_Fail;
95 }
96 m_OnNewEventClassCallback = cb;
97 return TimelineStatus::TimelineStatus_Success;
98 }
99
SetEventCallback(OnNewEventCallback cb)100 TimelineDecoder::TimelineStatus TimelineDecoder::SetEventCallback(OnNewEventCallback cb)
101 {
102 if (cb == nullptr)
103 {
104 return TimelineStatus::TimelineStatus_Fail;
105 }
106 m_OnNewEventCallback = cb;
107 return TimelineStatus::TimelineStatus_Success;
108 }
109
SetLabelCallback(OnNewLabelCallback cb)110 TimelineDecoder::TimelineStatus TimelineDecoder::SetLabelCallback(OnNewLabelCallback cb)
111 {
112 if (cb == nullptr)
113 {
114 return TimelineStatus::TimelineStatus_Fail;
115 }
116 m_OnNewLabelCallback = cb;
117 return TimelineStatus::TimelineStatus_Success;
118 }
119
SetRelationshipCallback(OnNewRelationshipCallback cb)120 TimelineDecoder::TimelineStatus TimelineDecoder::SetRelationshipCallback(OnNewRelationshipCallback cb)
121 {
122 if (cb == nullptr)
123 {
124 return TimelineStatus::TimelineStatus_Fail;
125 }
126 m_OnNewRelationshipCallback = cb;
127 return TimelineStatus::TimelineStatus_Success;
128 }
129
SetDefaultCallbacks()130 void TimelineDecoder::SetDefaultCallbacks()
131 {
132 SetEntityCallback([](Model& model, const ITimelineDecoder::Entity entity)
133 {
134 model.m_Entities.emplace_back(entity);
135 });
136
137 SetEventClassCallback([](Model& model, const ITimelineDecoder::EventClass eventClass)
138 {
139 model.m_EventClasses.emplace_back(eventClass);
140 });
141
142 SetEventCallback([](Model& model, const ITimelineDecoder::Event event)
143 {
144 model.m_Events.emplace_back(event);
145 });
146
147 SetLabelCallback([](Model& model, const ITimelineDecoder::Label label)
148 {
149 model.m_Labels.emplace_back(label);
150 });
151
152 SetRelationshipCallback([](Model& model, const ITimelineDecoder::Relationship relationship)
153 {
154 model.m_Relationships.emplace_back(relationship);
155 });
156 }
157
print()158 void TimelineDecoder::print()
159 {
160 if (m_Model.m_Labels.empty() && m_Model.m_Entities.empty() && m_Model.m_EventClasses.empty() &&
161 m_Model.m_Events.empty() && m_Model.m_Relationships.empty())
162 {
163 std::cout << "No timeline packets received" << std::endl;
164 return;
165 }
166
167 printLabels();
168 printEntities();
169 printEventClasses();
170 printEvents();
171 printRelationships();
172 }
173
printLabels()174 void TimelineDecoder::printLabels()
175 {
176 std::string header;
177
178 header.append(arm::pipe::CentreAlignFormatting("guid", 12));
179 header.append(" | ");
180 header.append(arm::pipe::CentreAlignFormatting("value", 30));
181 header.append("\n");
182
183 std::cout << "\n" << "\n";
184 std::cout << arm::pipe::CentreAlignFormatting("LABELS", static_cast<int>(header.size()));
185 std::cout << "\n";
186 std::cout << std::string(header.size(), '=') << "\n";
187 std::cout << header;
188
189 for (uint32_t i = 0; i < m_Model.m_Labels.size(); ++i)
190 {
191 std::string body;
192
193 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_Model.m_Labels[i].m_Guid), 12));
194 body.append(" | ");
195 body.append(arm::pipe::CentreAlignFormatting(m_Model.m_Labels[i].m_Name, 30));
196 body.append("\n");
197
198 std::cout << std::string(body.size(), '-') << "\n";
199 std::cout << body;
200 }
201 }
202
printEntities()203 void TimelineDecoder::printEntities()
204 {
205 std::string header;
206 header.append(arm::pipe::CentreAlignFormatting("guid", 12));
207 header.append("\n");
208
209 std::cout << "\n" << "\n";
210 std::cout << arm::pipe::CentreAlignFormatting("ENTITIES", static_cast<int>(header.size()));
211 std::cout << "\n";
212 std::cout << std::string(header.size(), '=') << "\n";
213 std::cout << header;
214
215 for (uint32_t i = 0; i < m_Model.m_Entities.size(); ++i)
216 {
217 std::string body;
218
219 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_Model.m_Entities[i].m_Guid), 12));
220 body.append("\n");
221
222 std::cout << std::string(body.size(), '-') << "\n";
223 std::cout << body;
224 }
225 }
226
printEventClasses()227 void TimelineDecoder::printEventClasses()
228 {
229 std::string header;
230 header.append(arm::pipe::CentreAlignFormatting("guid", 12));
231 header.append("\n");
232
233 std::cout << "\n" << "\n";
234 std::cout << arm::pipe::CentreAlignFormatting("EVENT CLASSES", static_cast<int>(header.size()));
235 std::cout << "\n";
236 std::cout << std::string(header.size(), '=') << "\n";
237 std::cout << header;
238
239 for (uint32_t i = 0; i < m_Model.m_EventClasses.size(); ++i)
240 {
241 std::string body;
242
243 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_Model.m_EventClasses[i].m_Guid), 12));
244 body.append("\n");
245
246 std::cout << std::string(body.size(), '-') << "\n";
247 std::cout << body;
248 }
249 }
250
printEvents()251 void TimelineDecoder::printEvents()
252 {
253 std::string header;
254
255 header.append(arm::pipe::CentreAlignFormatting("timestamp", 12));
256 header.append(" | ");
257 header.append(arm::pipe::CentreAlignFormatting("threadId", 12));
258 header.append(" | ");
259 header.append(arm::pipe::CentreAlignFormatting("eventGuid", 12));
260 header.append("\n");
261
262 std::cout << "\n" << "\n";
263 std::cout << arm::pipe::CentreAlignFormatting("EVENTS", static_cast<int>(header.size()));
264 std::cout << "\n";
265 std::cout << std::string(header.size(), '=') << "\n";
266 std::cout << header;
267
268 for (uint32_t i = 0; i < m_Model.m_Events.size(); ++i)
269 {
270 std::string body;
271
272 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_Model.m_Events[i].m_TimeStamp), 12));
273 body.append(" | ");
274
275 std::stringstream ss;
276 ss << m_Model.m_Events[i].m_ThreadId;
277 std::string threadId = ss.str();;
278
279 body.append(arm::pipe::CentreAlignFormatting(threadId, 12));
280 body.append(" | ");
281 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_Model.m_Events[i].m_Guid), 12));
282 body.append("\n");
283
284 std::cout << std::string(body.size(), '-') << "\n";
285 std::cout << body;
286 }
287 }
288
printRelationships()289 void TimelineDecoder::printRelationships()
290 {
291 std::string header;
292 header.append(arm::pipe::CentreAlignFormatting("relationshipType", 20));
293 header.append(" | ");
294 header.append(arm::pipe::CentreAlignFormatting("relationshipGuid", 20));
295 header.append(" | ");
296 header.append(arm::pipe::CentreAlignFormatting("headGuid", 12));
297 header.append(" | ");
298 header.append(arm::pipe::CentreAlignFormatting("tailGuid", 12));
299 header.append("\n");
300
301 std::cout << "\n" << "\n";
302 std::cout << arm::pipe::CentreAlignFormatting("RELATIONSHIPS", static_cast<int>(header.size()));
303 std::cout << "\n";
304 std::cout << std::string(header.size(), '=') << "\n";
305 std::cout << header;
306
307 for (uint32_t i = 0; i < m_Model.m_Relationships.size(); ++i)
308 {
309 std::string body;
310
311 body.append(
312 arm::pipe::CentreAlignFormatting(std::to_string(static_cast<unsigned int>
313 (m_Model.m_Relationships[i].m_RelationshipType)),
314 20));
315 body.append(" | ");
316 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_Model.m_Relationships[i].m_Guid), 20));
317 body.append(" | ");
318 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_Model.m_Relationships[i].m_HeadGuid), 12));
319 body.append(" | ");
320 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_Model.m_Relationships[i].m_TailGuid), 12));
321 body.append(" | ");
322 body.append("\n");
323
324 std::cout << std::string(body.size(), '-') << "\n";
325 std::cout << body;
326 }
327 }
328
329 } // namespace pipe
330 } // namespace arm
331