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