ArmNN
 24.02
JSONTimelineDecoder.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include <client/src/ProfilingUtils.hpp>
9 
10 #include <string>
11 
12 namespace armnn
13 {
14 namespace timelinedecoder
15 {
16 
17 static const char *const CONNECTION = "connection";
18 static const char *const BACKEND_ID = "backendId";
19 static const char *const NAME = "name";
20 static const char *const TYPE = "type";
21 static const char *const WORKLOAD = "workload";
22 static const char *const WORKLOAD_EXECUTION = "workload_execution";
23 static const char *const INFERENCE = "inference";
24 static const char *const LAYER = "layer";
25 static const char *const ENTITY = "Entity";
26 static const char *const EVENTCLASS = "EventClass";
27 static const char *const EVENT = "Event";
28 
29 JSONTimelineDecoder::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 
37 JSONTimelineDecoder::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 
46 JSONTimelineDecoder::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 
55 JSONTimelineDecoder::TimelineStatus JSONTimelineDecoder::CreateLabel(const Label& label)
56 {
57  this->m_Model.labels.insert({label.m_Guid, label});
58  return TimelineStatus::TimelineStatus_Success;
59 }
60 
61 JSONTimelineDecoder::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  m_Model.relationships.insert({relationship.m_Guid, relationship});
78  }
79 
80  return TimelineStatus::TimelineStatus_Success;
81 }
82 
83 
84 void JSONTimelineDecoder::HandleExecutionLink(const ITimelineDecoder::Relationship& relationship)
85 {
86  uint64_t tailGuid = relationship.m_TailGuid;
87  uint64_t headGuid = relationship.m_HeadGuid;
88 
89  if (m_Model.jsonEntities.count(relationship.m_HeadGuid) != 0)
90  {
91  JSONEntity& tailJSONEntity = m_Model.jsonEntities.at(tailGuid);
92  JSONEntity& headJSONEntity = m_Model.jsonEntities.at(headGuid);
93  tailJSONEntity.SetParent(headJSONEntity);
94  m_Model.jsonEntities.insert({headGuid, headJSONEntity});
95  m_Model.relationships.insert({relationship.m_Guid, relationship});
96  }
97  else
98  {
99  m_Model.relationships.insert({relationship.m_Guid, relationship});
100  }
101 }
102 
103 void JSONTimelineDecoder::HandleLabelLink(const ITimelineDecoder::Relationship& relationship)
104 {
105  if (m_Model.labels.count(relationship.m_TailGuid) != 0)
106  {
107  if (m_Model.labels.at(relationship.m_TailGuid).m_Name == CONNECTION)
108  {
109  HandleConnectionLabel(relationship);
110  }
111  else if (m_Model.labels.at(relationship.m_TailGuid).m_Name == BACKEND_ID)
112  {
113  HandleBackendIdLabel(relationship);
114  }
115  else if (m_Model.labels.at(relationship.m_TailGuid).m_Name == NAME)
116  {
117  HandleNameLabel(relationship);
118  }
119  else if (m_Model.labels.at(relationship.m_TailGuid).m_Name == TYPE)
120  {
121  HandleTypeLabel(relationship);
122  }
123  else
124  {
125  m_Model.relationships.insert({relationship.m_Guid, relationship});
126  }
127  } else
128  {
129  m_Model.relationships.insert({relationship.m_Guid, relationship});
130  }
131 }
132 
133 void JSONTimelineDecoder::HandleTypeLabel(const ITimelineDecoder::Relationship& relationship)
134 {
135  if (m_Model.relationships.count(relationship.m_HeadGuid) != 0)
136  {
137  Relationship labelRelation = m_Model.relationships.at(relationship.m_HeadGuid);
138  if (m_Model.jsonEntities.count(labelRelation.m_HeadGuid) != 0)
139  {
140  JSONEntity& headEntity = m_Model.jsonEntities.at(labelRelation.m_HeadGuid);
141  std::string type = m_Model.labels.at(labelRelation.m_TailGuid).m_Name;
142  headEntity.SetType(type);
143  }
144  }
145  else
146  {
147  m_Model.relationships.insert({relationship.m_Guid, relationship});
148  }
149 }
150 
151 void JSONTimelineDecoder::HandleNameLabel(const ITimelineDecoder::Relationship& relationship)
152 {
153  if (m_Model.relationships.count(relationship.m_HeadGuid) != 0)
154  {
155  Relationship labelRelation = m_Model.relationships.at(relationship.m_HeadGuid);
156  JSONEntity& headEntity = m_Model.jsonEntities.at(labelRelation.m_HeadGuid);
157  std::string name = m_Model.labels.at(labelRelation.m_TailGuid).m_Name;
158  headEntity.SetName(name);
159  }
160  else
161  {
162  m_Model.relationships.insert({relationship.m_Guid, relationship});
163  }
164 }
165 
166 void JSONTimelineDecoder::HandleBackendIdLabel(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 backendName = m_Model.labels.at(labelRelation.m_TailGuid).m_Name;
173  headEntity.extendedData.insert({BACKEND_ID, backendName});
174  }
175  else
176  {
177  m_Model.relationships.insert({relationship.m_Guid, relationship});
178  }
179 }
180 
181 void JSONTimelineDecoder::HandleConnectionLabel(const ITimelineDecoder::Relationship& relationship)
182 {
183  if (m_Model.relationships.count(relationship.m_HeadGuid) != 0)
184  {
185  Relationship retentionRelation = m_Model.relationships.at(relationship.m_HeadGuid);
186  JSONEntity& headEntity = m_Model.jsonEntities.at(retentionRelation.m_HeadGuid);
187  JSONEntity& tailEntity = m_Model.jsonEntities.at(retentionRelation.m_TailGuid);
188  headEntity.AddConnection(headEntity, tailEntity);
189  }
190  else
191  {
192  m_Model.relationships.insert({relationship.m_Guid, relationship});
193  }
194 }
195 
196 void JSONTimelineDecoder::HandleRetentionLink(const ITimelineDecoder::Relationship& relationship)
197 {
198  if (m_Model.jsonEntities.count(relationship.m_TailGuid) != 0 && m_Model.jsonEntities
199  .count(relationship.m_HeadGuid) != 0)
200  {
201  JSONEntity& tailJSONEntity = m_Model.jsonEntities.at(relationship.m_TailGuid);
202  JSONEntity& headJSONEntity = m_Model.jsonEntities.at(relationship.m_HeadGuid);
203  tailJSONEntity.SetParent(headJSONEntity);
204  m_Model.jsonEntities.insert({relationship.m_HeadGuid, headJSONEntity});
205  m_Model.relationships.insert({relationship.m_Guid, relationship});
206  }
207  else
208  {
209  m_Model.relationships.insert({relationship.m_Guid, relationship});
210  }
211 }
212 
214 {
215  parent.childEntities.push_back(GetGuid());
216 }
217 
219 {
220  std::string jsonString = GetJSONString(rootEntity);
221  os << jsonString;
222 }
223 
225 {
226  int counter = 0;
227  std::string json;
228  json.append("{\n");
229  if(rootEntity.GetType() != "")
230  {
231  json.append("\tArmNN");
232  json.append(": {\n");
233 
234  for (uint64_t childEntityId : rootEntity.childEntities)
235  {
236  JSONEntity& childEntity = this->m_Model.jsonEntities.at(childEntityId);
237  json.append(GetJSONEntityString(childEntity, counter));
238  }
239  }
240  json.append("}\n");
241  return json;
242 }
243 
245 {
246  std::string jsonEntityString;
247  if(entity.GetType() == LAYER)
248  {
249  return GetLayerJSONString(entity, counter, jsonEntityString);
250  }
251  else if (entity.GetType() == WORKLOAD)
252  {
253  return GetWorkloadJSONString(entity, counter, jsonEntityString);
254  }
255  else if (entity.GetType() == WORKLOAD_EXECUTION)
256  {
257  return GetWorkloadExecutionJSONString(entity, jsonEntityString);
258  }
259  else if (entity.GetType() == INFERENCE)
260  {
261  return jsonEntityString;
262  }
263  else
264  {
265  for (uint64_t child_entity_id : entity.childEntities)
266  {
267  JSONEntity& childEntity = this->m_Model.jsonEntities.at(child_entity_id);
268  jsonEntityString.append(GetJSONEntityString(childEntity, ++counter));
269  }
270  return jsonEntityString;
271  }
272 }
273 
274 std::string JSONTimelineDecoder::GetWorkloadExecutionJSONString(const JSONTimelineDecoder::JSONEntity& entity,
275  std::string& jsonEntityString) const
276 {
277  if(entity.childEntities.size() < 2)
278  {
279  throw arm::pipe::ProfilingException(
280  "Workload Execution Entity Packet does not have the expected Event packets attached");
281  }
282  JSONEntity jsonEventOne = entity.childEntities[0];
283  JSONEntity jsonEventTwo = entity.childEntities[1];
284 
285  Event event1 = m_Model.events.at(jsonEventOne.GetGuid());
286  Event event2 = m_Model.events.at(jsonEventTwo.GetGuid());
287 
288  uint64_t wall_clock_time = event2.m_TimeStamp - event1.m_TimeStamp;
289  jsonEntityString.append("\t\t\t");
290  jsonEntityString.append("raw : [");
291  jsonEntityString.append(std::to_string(wall_clock_time));
292  jsonEntityString.append("], \n");
293  jsonEntityString.append("\t\t\t");
294  jsonEntityString.append("unit : us,\n");
295  jsonEntityString.append("\t\t\t");
296  jsonEntityString.append("}\n");
297 
298  return jsonEntityString;
299 }
300 
301 std::string JSONTimelineDecoder::GetWorkloadJSONString(const JSONTimelineDecoder::JSONEntity& entity, int& counter,
302  std::string& jsonEntityString)
303 {
304  jsonEntityString.append("\t\t\t");
305  jsonEntityString.append("backendId :");
306  jsonEntityString.append(entity.extendedData.at(BACKEND_ID));
307  jsonEntityString.append(",\n");
308  for (uint64_t child_entity_id : entity.childEntities)
309  {
310  JSONEntity &childEntity = m_Model.jsonEntities.at(child_entity_id);
311  jsonEntityString.append(GetJSONEntityString(childEntity, ++counter));
312  }
313  return jsonEntityString;
314 }
315 
316 std::string JSONTimelineDecoder::GetLayerJSONString(JSONTimelineDecoder::JSONEntity& entity, int& counter,
317  std::string& jsonEntityString)
318 {
319  jsonEntityString.append("\t\t");
320  jsonEntityString.append(entity.GetName());
321  jsonEntityString.append("_");
322  jsonEntityString.append(std::to_string(counter));
323  jsonEntityString.append(": {\n");
324  jsonEntityString.append("\t\t\t");
325  jsonEntityString.append("type: Measurement,\n");
326  for (uint64_t child_entity_id : entity.childEntities)
327  {
328  JSONEntity& childEntity = m_Model.jsonEntities.at(child_entity_id);
329  jsonEntityString.append(GetJSONEntityString(childEntity, ++counter));
330  }
331  return jsonEntityString;
332 }
333 
335 {
336  std::vector<uint64_t>::iterator it = std::find(headEntity.childEntities.begin(),
337  headEntity.childEntities.end(), connectedEntity.GetGuid());
338  headEntity.childEntities.erase(it);
339  headEntity.connected_entities.push_back(connectedEntity.m_Guid);
340 }
341 
343 {
344  return m_Guid;
345 }
346 
348 {
349  return m_Model;
350 }
351 
352 void JSONTimelineDecoder::JSONEntity::SetName(std::string entityName)
353 {
354  this->name = entityName;
355 }
356 
358 {
359  return this->name;
360 }
361 
362 void JSONTimelineDecoder::JSONEntity::SetType(std::string entityType)
363 {
364  this->type = entityType;
365 }
366 
368 {
369  return this->type;
370 }
371 
372 }
373 }
armnn::timelinedecoder::JSONTimelineDecoder::GetJSONString
std::string GetJSONString(JSONEntity &rootEntity)
Definition: JSONTimelineDecoder.cpp:224
armnn::timelinedecoder::JSONTimelineDecoder::GetJSONEntityString
std::string GetJSONEntityString(JSONEntity &entity, int &counter)
Definition: JSONTimelineDecoder.cpp:244
armnn::timelinedecoder::JSONTimelineDecoder::Model::labels
std::map< uint64_t, Label > labels
Definition: JSONTimelineDecoder.hpp:48
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity
Definition: JSONTimelineDecoder.hpp:22
armnn::timelinedecoder::JSONTimelineDecoder::Model::events
std::map< uint64_t, Event > events
Definition: JSONTimelineDecoder.hpp:49
JSONTimelineDecoder.hpp
armnn::timelinedecoder::JSONTimelineDecoder::Model::relationships
std::map< uint64_t, Relationship > relationships
Definition: JSONTimelineDecoder.hpp:47
armnn::timelinedecoder::JSONTimelineDecoder::GetModel
const Model & GetModel()
Definition: JSONTimelineDecoder.cpp:347
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::SetType
void SetType(std::string entityType)
Definition: JSONTimelineDecoder.cpp:362
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::connected_entities
std::vector< uint64_t > connected_entities
Definition: JSONTimelineDecoder.hpp:25
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::GetType
std::string GetType()
Definition: JSONTimelineDecoder.cpp:367
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::GetName
std::string GetName()
Definition: JSONTimelineDecoder.cpp:357
armnn::timelinedecoder::JSONTimelineDecoder::PrintJSON
void PrintJSON(JSONEntity &entity, std::ostream &os)
Definition: JSONTimelineDecoder.cpp:218
armnn::Event
Event class records measurements reported by BeginEvent()/EndEvent() and returns measurements when Ev...
Definition: ProfilingEvent.hpp:27
armnn::timelinedecoder::JSONTimelineDecoder::CreateLabel
virtual TimelineStatus CreateLabel(const Label &) override
Definition: JSONTimelineDecoder.cpp:55
armnn::timelinedecoder::JSONTimelineDecoder::CreateRelationship
virtual TimelineStatus CreateRelationship(const Relationship &) override
Definition: JSONTimelineDecoder.cpp:61
armnn::timelinedecoder::JSONTimelineDecoder::Model
Definition: JSONTimelineDecoder.hpp:44
armnn::timelinedecoder::JSONTimelineDecoder::Model::eventClasses
std::map< uint64_t, EventClass > eventClasses
Definition: JSONTimelineDecoder.hpp:50
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::AddConnection
void AddConnection(JSONEntity &headEntity, JSONEntity &connectedEntity)
Definition: JSONTimelineDecoder.cpp:334
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::childEntities
std::vector< uint64_t > childEntities
Definition: JSONTimelineDecoder.hpp:26
armnn::timelinedecoder::JSONTimelineDecoder::CreateEventClass
virtual TimelineStatus CreateEventClass(const EventClass &) override
Definition: JSONTimelineDecoder.cpp:37
armnn::timelinedecoder::JSONTimelineDecoder::Model::jsonEntities
std::map< uint64_t, JSONEntity > jsonEntities
Definition: JSONTimelineDecoder.hpp:46
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::GetGuid
uint64_t GetGuid()
Definition: JSONTimelineDecoder.cpp:342
armnn::timelinedecoder::JSONTimelineDecoder::CreateEntity
virtual TimelineStatus CreateEntity(const Entity &) override
Definition: JSONTimelineDecoder.cpp:29
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::SetParent
void SetParent(JSONEntity &parent)
Definition: JSONTimelineDecoder.cpp:213
armnn::timelinedecoder::JSONTimelineDecoder::JSONEntity::SetName
void SetName(std::string entityName)
Definition: JSONTimelineDecoder.cpp:352
armnn::timelinedecoder::JSONTimelineDecoder::CreateEvent
virtual TimelineStatus CreateEvent(const Event &) override
Definition: JSONTimelineDecoder.cpp:46