Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 1 | // |
Jan Eilers | 1f24944 | 2020-07-01 15:37:50 +0100 | [diff] [blame] | 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "JSONTimelineDecoder.hpp" |
Jim Flynn | 3e9bc19 | 2022-03-23 23:01:26 +0000 | [diff] [blame] | 7 | |
| 8 | #include <client/src/ProfilingUtils.hpp> |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 9 | |
| 10 | #include <string> |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 125 | m_Model.relationships.insert({relationship.m_Guid, relationship}); |
| 126 | } |
| 127 | } else |
| 128 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | { |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 209 | m_Model.relationships.insert({relationship.m_Guid, relationship}); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void JSONTimelineDecoder::JSONEntity::SetParent(JSONEntity& parent) |
| 214 | { |
| 215 | parent.childEntities.push_back(GetGuid()); |
| 216 | } |
| 217 | |
Jan Eilers | 1f24944 | 2020-07-01 15:37:50 +0100 | [diff] [blame] | 218 | void JSONTimelineDecoder::PrintJSON(JSONTimelineDecoder::JSONEntity& rootEntity, std::ostream& os) |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 219 | { |
| 220 | std::string jsonString = GetJSONString(rootEntity); |
Jan Eilers | 1f24944 | 2020-07-01 15:37:50 +0100 | [diff] [blame] | 221 | os << jsonString; |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | std::string JSONTimelineDecoder::GetJSONString(JSONTimelineDecoder::JSONEntity& rootEntity) |
| 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 | |
| 244 | std::string JSONTimelineDecoder::GetJSONEntityString(JSONTimelineDecoder::JSONEntity& entity, int& counter) |
| 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 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 279 | throw arm::pipe::ProfilingException( |
| 280 | "Workload Execution Entity Packet does not have the expected Event packets attached"); |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 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 | |
| 334 | void JSONTimelineDecoder::JSONEntity::AddConnection(JSONEntity& headEntity, JSONEntity& connectedEntity) |
| 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 | |
| 342 | uint64_t JSONTimelineDecoder::JSONEntity::GetGuid() |
| 343 | { |
| 344 | return m_Guid; |
| 345 | } |
| 346 | |
| 347 | const JSONTimelineDecoder::Model &JSONTimelineDecoder::GetModel() |
| 348 | { |
| 349 | return m_Model; |
| 350 | } |
| 351 | |
Éanna Ó Catháin | 0de4712 | 2020-04-01 15:40:12 +0100 | [diff] [blame] | 352 | void JSONTimelineDecoder::JSONEntity::SetName(std::string entityName) |
| 353 | { |
| 354 | this->name = entityName; |
| 355 | } |
| 356 | |
| 357 | std::string JSONTimelineDecoder::JSONEntity::GetName() |
| 358 | { |
| 359 | return this->name; |
| 360 | } |
| 361 | |
| 362 | void JSONTimelineDecoder::JSONEntity::SetType(std::string entityType) |
| 363 | { |
| 364 | this->type = entityType; |
| 365 | } |
| 366 | |
| 367 | std::string JSONTimelineDecoder::JSONEntity::GetType() |
| 368 | { |
| 369 | return this->type; |
| 370 | } |
| 371 | |
| 372 | } |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 373 | } |