blob: 520995afe948dc6469cd70ffde18eac1f241bb48 [file] [log] [blame]
Éanna Ó Catháin0de47122020-04-01 15:40:12 +01001//
2// Copyright © 2020 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "JSONTimelineDecoder.hpp"
7#include "../profiling/ProfilingUtils.hpp"
8
9#include <string>
10#include <fstream>
11
12namespace armnn
13{
14namespace timelinedecoder
15{
16
17static const char *const CONNECTION = "connection";
18static const char *const BACKEND_ID = "backendId";
19static const char *const NAME = "name";
20static const char *const TYPE = "type";
21static const char *const WORKLOAD = "workload";
22static const char *const WORKLOAD_EXECUTION = "workload_execution";
23static const char *const INFERENCE = "inference";
24static const char *const LAYER = "layer";
25static const char *const ENTITY = "Entity";
26static const char *const EVENTCLASS = "EventClass";
27static const char *const EVENT = "Event";
28
29JSONTimelineDecoder::TimelineStatus JSONTimelineDecoder::CreateEntity(const Entity& entity)
30{
31 JSONEntity jsonEntity(entity.m_Guid);
32 jsonEntity.SetType(ENTITY);
33 this->m_Model.jsonEntities.insert({entity.m_Guid, jsonEntity});
34 return TimelineStatus::TimelineStatus_Success;
35}
36
37JSONTimelineDecoder::TimelineStatus JSONTimelineDecoder::CreateEventClass(const EventClass& eventClass)
38{
39 JSONEntity jsonEntity(eventClass.m_Guid);
40 jsonEntity.SetType(EVENTCLASS);
41 this->m_Model.eventClasses.insert({eventClass.m_Guid, eventClass});
42 this->m_Model.jsonEntities.insert({eventClass.m_Guid, jsonEntity});
43 return TimelineStatus::TimelineStatus_Success;
44}
45
46JSONTimelineDecoder::TimelineStatus JSONTimelineDecoder::CreateEvent(const Event& event)
47{
48 JSONEntity jsonEntity(event.m_Guid);
49 jsonEntity.SetType(EVENT);
50 this->m_Model.events.insert({event.m_Guid, event});
51 this->m_Model.jsonEntities.insert({jsonEntity.GetGuid(), jsonEntity});
52 return TimelineStatus::TimelineStatus_Success;
53}
54
55JSONTimelineDecoder::TimelineStatus JSONTimelineDecoder::CreateLabel(const Label& label)
56{
57 this->m_Model.labels.insert({label.m_Guid, label});
58 return TimelineStatus::TimelineStatus_Success;
59}
60
61JSONTimelineDecoder::TimelineStatus JSONTimelineDecoder::CreateRelationship(const Relationship& relationship)
62{
63 if (relationship.m_RelationshipType == ITimelineDecoder::RelationshipType::RetentionLink)
64 {
65 HandleRetentionLink(relationship);
66 }
67 else if (relationship.m_RelationshipType == ITimelineDecoder::RelationshipType::LabelLink)
68 {
69 HandleLabelLink(relationship);
70 }
71 else if (relationship.m_RelationshipType == ITimelineDecoder::RelationshipType::ExecutionLink)
72 {
73 HandleExecutionLink(relationship);
74 }
75 else
76 {
77 /*
78 * TODO Handle DataLink
79 */
80 m_Model.relationships.insert({relationship.m_Guid, relationship});
81 }
82
83 return TimelineStatus::TimelineStatus_Success;
84}
85
86
87void JSONTimelineDecoder::HandleExecutionLink(const ITimelineDecoder::Relationship& relationship)
88{
89 uint64_t tailGuid = relationship.m_TailGuid;
90 uint64_t headGuid = relationship.m_HeadGuid;
91
92 if (m_Model.jsonEntities.count(relationship.m_HeadGuid) != 0)
93 {
94 JSONEntity& tailJSONEntity = m_Model.jsonEntities.at(tailGuid);
95 JSONEntity& headJSONEntity = m_Model.jsonEntities.at(headGuid);
96 tailJSONEntity.SetParent(headJSONEntity);
97 m_Model.jsonEntities.insert({headGuid, headJSONEntity});
98 m_Model.relationships.insert({relationship.m_Guid, relationship});
99 }
100 else
101 {
102 /*
103 * TODO Add some protection against packet ordering issues
104 */
105 m_Model.relationships.insert({relationship.m_Guid, relationship});
106 }
107}
108
109void JSONTimelineDecoder::HandleLabelLink(const ITimelineDecoder::Relationship& relationship)
110{
111 if (m_Model.labels.count(relationship.m_TailGuid) != 0)
112 {
113 if (m_Model.labels.at(relationship.m_TailGuid).m_Name == CONNECTION)
114 {
115 HandleConnectionLabel(relationship);
116 }
117 else if (m_Model.labels.at(relationship.m_TailGuid).m_Name == BACKEND_ID)
118 {
119 HandleBackendIdLabel(relationship);
120 }
121 else if (m_Model.labels.at(relationship.m_TailGuid).m_Name == NAME)
122 {
123 HandleNameLabel(relationship);
124 }
125 else if (m_Model.labels.at(relationship.m_TailGuid).m_Name == TYPE)
126 {
127 HandleTypeLabel(relationship);
128 }
129 else
130 {
131 /*
132 * TODO Add some protection against packet ordering issues
133 */
134 m_Model.relationships.insert({relationship.m_Guid, relationship});
135 }
136 } else
137 {
138 /*
139 * TODO Add some protection against packet ordering issues
140 */
141 m_Model.relationships.insert({relationship.m_Guid, relationship});
142 }
143}
144
145void JSONTimelineDecoder::HandleTypeLabel(const ITimelineDecoder::Relationship& relationship)
146{
147 if (m_Model.relationships.count(relationship.m_HeadGuid) != 0)
148 {
149 Relationship labelRelation = m_Model.relationships.at(relationship.m_HeadGuid);
150 if (m_Model.jsonEntities.count(labelRelation.m_HeadGuid) != 0)
151 {
152 JSONEntity& headEntity = m_Model.jsonEntities.at(labelRelation.m_HeadGuid);
153 std::string type = m_Model.labels.at(labelRelation.m_TailGuid).m_Name;
154 headEntity.SetType(type);
155 }
156 }
157 else
158 {
159 /*
160 * TODO Add some protection against packet ordering issues
161 */
162 m_Model.relationships.insert({relationship.m_Guid, relationship});
163 }
164}
165
166void JSONTimelineDecoder::HandleNameLabel(const ITimelineDecoder::Relationship& relationship)
167{
168 if (m_Model.relationships.count(relationship.m_HeadGuid) != 0)
169 {
170 Relationship labelRelation = m_Model.relationships.at(relationship.m_HeadGuid);
171 JSONEntity& headEntity = m_Model.jsonEntities.at(labelRelation.m_HeadGuid);
172 std::string name = m_Model.labels.at(labelRelation.m_TailGuid).m_Name;
173 headEntity.SetName(name);
174 }
175 else
176 {
177 /*
178 * TODO Add some protection against packet ordering issues
179 */
180 m_Model.relationships.insert({relationship.m_Guid, relationship});
181 }
182}
183
184void JSONTimelineDecoder::HandleBackendIdLabel(const ITimelineDecoder::Relationship& relationship)
185{
186 if (m_Model.relationships.count(relationship.m_HeadGuid) != 0)
187 {
188 Relationship labelRelation = m_Model.relationships.at(relationship.m_HeadGuid);
189 JSONEntity& headEntity = m_Model.jsonEntities.at(labelRelation.m_HeadGuid);
190 std::string backendName = m_Model.labels.at(labelRelation.m_TailGuid).m_Name;
191 headEntity.extendedData.insert({BACKEND_ID, backendName});
192 }
193 else
194 {
195 /*
196 * TODO Add some protection against packet ordering issues
197 */
198 m_Model.relationships.insert({relationship.m_Guid, relationship});
199 }
200}
201
202void JSONTimelineDecoder::HandleConnectionLabel(const ITimelineDecoder::Relationship& relationship)
203{
204 if (m_Model.relationships.count(relationship.m_HeadGuid) != 0)
205 {
206 Relationship retentionRelation = m_Model.relationships.at(relationship.m_HeadGuid);
207 JSONEntity& headEntity = m_Model.jsonEntities.at(retentionRelation.m_HeadGuid);
208 JSONEntity& tailEntity = m_Model.jsonEntities.at(retentionRelation.m_TailGuid);
209 headEntity.AddConnection(headEntity, tailEntity);
210 }
211 else
212 {
213 /*
214 * TODO Add some protection against packet ordering issues
215 */
216 m_Model.relationships.insert({relationship.m_Guid, relationship});
217 }
218}
219
220void JSONTimelineDecoder::HandleRetentionLink(const ITimelineDecoder::Relationship& relationship)
221{
222 if (m_Model.jsonEntities.count(relationship.m_TailGuid) != 0 && m_Model.jsonEntities
223 .count(relationship.m_HeadGuid) != 0)
224 {
225 JSONEntity& tailJSONEntity = m_Model.jsonEntities.at(relationship.m_TailGuid);
226 JSONEntity& headJSONEntity = m_Model.jsonEntities.at(relationship.m_HeadGuid);
227 tailJSONEntity.SetParent(headJSONEntity);
228 m_Model.jsonEntities.insert({relationship.m_HeadGuid, headJSONEntity});
229 m_Model.relationships.insert({relationship.m_Guid, relationship});
230 }
231 else
232 {
233 /*
234 * TODO Add some protection against packet ordering issues
235 */
236 m_Model.relationships.insert({relationship.m_Guid, relationship});
237 }
238}
239
240void JSONTimelineDecoder::JSONEntity::SetParent(JSONEntity& parent)
241{
242 parent.childEntities.push_back(GetGuid());
243}
244
245void JSONTimelineDecoder::PrintJSON(JSONTimelineDecoder::JSONEntity& rootEntity)
246{
247 std::string jsonString = GetJSONString(rootEntity);
248 std::ofstream outfile;
249 outfile.open(this->outputJSONFile);
250 outfile << jsonString;
251}
252
253std::string JSONTimelineDecoder::GetJSONString(JSONTimelineDecoder::JSONEntity& rootEntity)
254{
255 int counter = 0;
256 std::string json;
257 json.append("{\n");
258 if(rootEntity.GetType() != "")
259 {
260 json.append("\tArmNN");
261 json.append(": {\n");
262
263 for (uint64_t childEntityId : rootEntity.childEntities)
264 {
265 JSONEntity& childEntity = this->m_Model.jsonEntities.at(childEntityId);
266 json.append(GetJSONEntityString(childEntity, counter));
267 }
268 }
269 json.append("}\n");
270 return json;
271}
272
273std::string JSONTimelineDecoder::GetJSONEntityString(JSONTimelineDecoder::JSONEntity& entity, int& counter)
274{
275 std::string jsonEntityString;
276 if(entity.GetType() == LAYER)
277 {
278 return GetLayerJSONString(entity, counter, jsonEntityString);
279 }
280 else if (entity.GetType() == WORKLOAD)
281 {
282 return GetWorkloadJSONString(entity, counter, jsonEntityString);
283 }
284 else if (entity.GetType() == WORKLOAD_EXECUTION)
285 {
286 return GetWorkloadExecutionJSONString(entity, jsonEntityString);
287 }
288 else if (entity.GetType() == INFERENCE)
289 {
290 return jsonEntityString;
291 }
292 else
293 {
294 for (uint64_t child_entity_id : entity.childEntities)
295 {
296 JSONEntity& childEntity = this->m_Model.jsonEntities.at(child_entity_id);
297 jsonEntityString.append(GetJSONEntityString(childEntity, ++counter));
298 }
299 return jsonEntityString;
300 }
301}
302
303std::string JSONTimelineDecoder::GetWorkloadExecutionJSONString(const JSONTimelineDecoder::JSONEntity& entity,
304 std::string& jsonEntityString) const
305{
306 if(entity.childEntities.size() < 2)
307 {
308 throw Exception("Workload Execution Entity Packet does not have the expected Event packets attached");
309 }
310 JSONEntity jsonEventOne = entity.childEntities[0];
311 JSONEntity jsonEventTwo = entity.childEntities[1];
312
313 Event event1 = m_Model.events.at(jsonEventOne.GetGuid());
314 Event event2 = m_Model.events.at(jsonEventTwo.GetGuid());
315
316 uint64_t wall_clock_time = event2.m_TimeStamp - event1.m_TimeStamp;
317 jsonEntityString.append("\t\t\t");
318 jsonEntityString.append("raw : [");
319 jsonEntityString.append(std::to_string(wall_clock_time));
320 jsonEntityString.append("], \n");
321 jsonEntityString.append("\t\t\t");
322 jsonEntityString.append("unit : us,\n");
323 jsonEntityString.append("\t\t\t");
324 jsonEntityString.append("}\n");
325
326 return jsonEntityString;
327}
328
329std::string JSONTimelineDecoder::GetWorkloadJSONString(const JSONTimelineDecoder::JSONEntity& entity, int& counter,
330 std::string& jsonEntityString)
331{
332 jsonEntityString.append("\t\t\t");
333 jsonEntityString.append("backendId :");
334 jsonEntityString.append(entity.extendedData.at(BACKEND_ID));
335 jsonEntityString.append(",\n");
336 for (uint64_t child_entity_id : entity.childEntities)
337 {
338 JSONEntity &childEntity = m_Model.jsonEntities.at(child_entity_id);
339 jsonEntityString.append(GetJSONEntityString(childEntity, ++counter));
340 }
341 return jsonEntityString;
342}
343
344std::string JSONTimelineDecoder::GetLayerJSONString(JSONTimelineDecoder::JSONEntity& entity, int& counter,
345 std::string& jsonEntityString)
346{
347 jsonEntityString.append("\t\t");
348 jsonEntityString.append(entity.GetName());
349 jsonEntityString.append("_");
350 jsonEntityString.append(std::to_string(counter));
351 jsonEntityString.append(": {\n");
352 jsonEntityString.append("\t\t\t");
353 jsonEntityString.append("type: Measurement,\n");
354 for (uint64_t child_entity_id : entity.childEntities)
355 {
356 JSONEntity& childEntity = m_Model.jsonEntities.at(child_entity_id);
357 jsonEntityString.append(GetJSONEntityString(childEntity, ++counter));
358 }
359 return jsonEntityString;
360}
361
362void JSONTimelineDecoder::JSONEntity::AddConnection(JSONEntity& headEntity, JSONEntity& connectedEntity)
363{
364 std::vector<uint64_t>::iterator it = std::find(headEntity.childEntities.begin(),
365 headEntity.childEntities.end(), connectedEntity.GetGuid());
366 headEntity.childEntities.erase(it);
367 headEntity.connected_entities.push_back(connectedEntity.m_Guid);
368}
369
370uint64_t JSONTimelineDecoder::JSONEntity::GetGuid()
371{
372 return m_Guid;
373}
374
375const JSONTimelineDecoder::Model &JSONTimelineDecoder::GetModel()
376{
377 return m_Model;
378}
379
380void JSONTimelineDecoder::SetOutgoingCaptureFile(const std::string& outgoingCaptureFile)
381{
382 this->outputJSONFile = outgoingCaptureFile;
383}
384
385void JSONTimelineDecoder::JSONEntity::SetName(std::string entityName)
386{
387 this->name = entityName;
388}
389
390std::string JSONTimelineDecoder::JSONEntity::GetName()
391{
392 return this->name;
393}
394
395void JSONTimelineDecoder::JSONEntity::SetType(std::string entityType)
396{
397 this->type = entityType;
398}
399
400std::string JSONTimelineDecoder::JSONEntity::GetType()
401{
402 return this->type;
403}
404
405}
406}