aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/test/TimelineModel.cpp
blob: 2e4fd06013bfe63fef98a15697d87ab68a76d9e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//
// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "TimelineModel.hpp"
#include <LabelsAndEventClasses.hpp>

#include <algorithm>

namespace armnn
{

namespace profiling
{

void TimelineModel::AddLabel(const ITimelineDecoder::Label& label)
{
    m_LabelMap.emplace(label.m_Guid, label);
}

std::string* TimelineModel::FindLabel(uint64_t guid)
{
    auto iter = m_LabelMap.find(guid);
    if (iter != m_LabelMap.end())
    {
        return &iter->second.m_Name;
    }
    else
    {
        return nullptr;
    }
}

void TimelineModel::AddEntity(uint64_t guid)
{
    m_Entities.emplace(guid, guid);
}

Entity* TimelineModel::FindEntity(uint64_t id)
{
    auto iter = m_Entities.find(id);
    if (iter != m_Entities.end())
    {
        return &(iter->second);
    }
    else
    {
        return nullptr;
    }
}

void TimelineModel::AddRelationship(const ITimelineDecoder::Relationship& relationship)
{
    m_Relationships.emplace(relationship.m_Guid, relationship);
    if (relationship.m_RelationshipType == ITimelineDecoder::RelationshipType::LabelLink)
    {
        HandleLabelLink(relationship);
    }
    else if (relationship.m_RelationshipType == ITimelineDecoder::RelationshipType::RetentionLink)
    {
        // Take care of the special case of a connection between layers in ArmNN
        // modelled by a retention link between two layer entities with an attribute GUID
        // of connection
        if (relationship.m_AttributeGuid == armnn::profiling::LabelsAndEventClasses::CONNECTION_GUID)
        {
            HandleConnection(relationship);
        }
        else if (relationship.m_AttributeGuid == armnn::profiling::LabelsAndEventClasses::CHILD_GUID)
        {
            HandleChild(relationship);
        }
        else if (relationship.m_AttributeGuid == armnn::profiling::LabelsAndEventClasses::EXECUTION_OF_GUID)
        {
            HandleExecutionOf(relationship);
        }
        else
        {
            // report unknown relationship type
            std::stringstream ss;
            ss << "Encountered a RetentionLink of unknown type [" << relationship.m_AttributeGuid << "]";
            m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        }
    }
    else if (relationship.m_RelationshipType == ITimelineDecoder::RelationshipType::ExecutionLink)
    {
        HandleExecutionLink(relationship);
    }
}

void TimelineModel::HandleLabelLink(const ITimelineDecoder::Relationship& relationship)
{
    Entity* entity = FindEntity(relationship.m_HeadGuid);
    // we have a label attribute of an entity
    std::string* value = nullptr;
    std::string* attribute = nullptr;
    value = FindLabel(relationship.m_TailGuid);
    if (value == nullptr)
    {
        //report an error
        std::stringstream ss;
        ss << "could not find label link [" << relationship.m_Guid <<
           "] value [" << relationship.m_TailGuid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
    }
    if (relationship.m_AttributeGuid != 0)
    {
        attribute = FindLabel(relationship.m_AttributeGuid);
        if (attribute == nullptr)
        {
            //report an error
            std::stringstream ss;
            ss << "could not find label link [" << relationship.m_Guid <<
               "] attribute [" << relationship.m_AttributeGuid << "]";
            m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        }
    }
    else
    {
        //report an error
        std::stringstream ss;
        ss << "label link [" << relationship.m_Guid << "] has a zero attribute guid";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
    }
    if (entity != nullptr && attribute != nullptr && value != nullptr)
    {
        entity->AddAttribute(*attribute, *value);
        // if the attribute is 'type' and the value is 'inference'
        // we need to cache the entity guid as an inference
        if (armnn::profiling::LabelsAndEventClasses::TYPE_LABEL.compare(*attribute) == 0 &&
            armnn::profiling::LabelsAndEventClasses::INFERENCE.compare(*value) == 0)
        {
            m_InferenceGuids.push_back(relationship.m_HeadGuid);
        }
    }

    if (entity == nullptr)
    {
        //report an error
        std::stringstream ss;
        ss << "could not find label link [" << relationship.m_Guid <<
           "] entity [" << relationship.m_HeadGuid << "] ";
        if (value != nullptr)
        {
            ss << "value [" << *value << "] ";
        }
        if (attribute != nullptr)
        {
            ss << "attribute [" << *attribute << "] ";
        }
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
    }
}

void TimelineModel::HandleConnection(const ITimelineDecoder::Relationship& relationship)
{
    Entity* outputLayer = FindEntity(relationship.m_HeadGuid);
    if (outputLayer == nullptr)
    {
        std::stringstream ss;
        ss << "could not find output entity [" << relationship.m_HeadGuid << "]";
        ss << " of connection [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    Entity* inputLayer = FindEntity(relationship.m_TailGuid);
    if (inputLayer == nullptr)
    {
        std::stringstream ss;
        ss << "could not find input entity [" << relationship.m_TailGuid << "]";
        ss << " of connection [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    Connection connection(relationship.m_Guid, outputLayer, inputLayer);
    outputLayer->AddConnection(connection);
}

void TimelineModel::HandleChild(const ITimelineDecoder::Relationship& relationship)
{
    Entity* parentEntity = FindEntity(relationship.m_HeadGuid);
    if (parentEntity == nullptr)
    {
        std::stringstream ss;
        ss << "could not find parent entity [" << relationship.m_HeadGuid << "]";
        ss << " of child relationship [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    Entity* childEntity = FindEntity(relationship.m_TailGuid);
    if (childEntity == nullptr)
    {
        std::stringstream ss;
        ss << "could not find child entity [" << relationship.m_TailGuid << "]";
        ss << " of child relationship [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    parentEntity->AddChild(childEntity);
}

void TimelineModel::HandleExecutionOf(const ITimelineDecoder::Relationship& relationship)
{
    Entity* parentEntity = FindEntity(relationship.m_HeadGuid);
    if (parentEntity == nullptr)
    {
        std::stringstream ss;
        ss << "could not find parent entity [" << relationship.m_HeadGuid << "]";
        ss << " of execution relationship [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    Entity* executedEntity = FindEntity(relationship.m_TailGuid);
    if (executedEntity == nullptr)
    {
        std::stringstream ss;
        ss << "could not find executed entity [" << relationship.m_TailGuid << "]";
        ss << " of execution relationship [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    parentEntity->AddExecution(executedEntity);
}

void TimelineModel::HandleExecutionLink(const ITimelineDecoder::Relationship& relationship)
{
    // entityGuid,
    Entity* parentEntity = FindEntity(relationship.m_HeadGuid);
    if (parentEntity == nullptr)
    {
        std::stringstream ss;
        ss << "could not find entity [" << relationship.m_HeadGuid << "]";
        ss << " of ExecutionLink [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    // eventGuid,
    EventObj* eventObj = FindEvent(relationship.m_TailGuid);
    if (eventObj == nullptr)
    {
        std::stringstream ss;
        ss << "could not find event [" << relationship.m_TailGuid << "]";
        ss << " of ExecutionLink [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    // eventClassGuid
    EventClassObj* eventClassObj = FindEventClass(relationship.m_AttributeGuid);
    if (eventClassObj == nullptr)
    {
        std::stringstream ss;
        ss << "could not find event class [" << relationship.m_TailGuid << "]";
        ss << " of ExecutionLink [" << relationship.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
        return;
    }
    eventObj->SetEventClass(eventClassObj);
    parentEntity->AddEvent(eventObj);
}

ModelRelationship* TimelineModel::FindRelationship(uint64_t id)
{
    auto iter = m_Relationships.find(id);
    if (iter != m_Relationships.end())
    {
        return &(iter->second);
    }
    else
    {
        return nullptr;
    }
}

bool TimelineModel::IsInferenceGuid(uint64_t guid) const
{
    auto it = std::find(m_InferenceGuids.begin(), m_InferenceGuids.end(), guid);
    return it != m_InferenceGuids.end();
}

void TimelineModel::AddEventClass(const ITimelineDecoder::EventClass& eventClass)
{
    std::string* eventClassName = FindLabel(eventClass.m_NameGuid);
    if (eventClassName != nullptr)
    {
        EventClassObj eventClassObj(eventClass.m_Guid, *eventClassName);
        m_EventClasses.emplace(eventClassObj.GetGuid(), eventClassObj);
    }
    else
    {
        std::stringstream ss;
        ss << "could not find name [" << eventClass.m_NameGuid << "]";
        ss << " of of event class  [" << eventClass.m_Guid << "]";
        m_Errors.push_back(armnnProfiling::ProfilingException(ss.str()));
    }
}

EventClassObj* TimelineModel::FindEventClass(uint64_t id)
{
    auto iter = m_EventClasses.find(id);
    if (iter != m_EventClasses.end())
    {
        return &(iter->second);
    }
    else
    {
        return nullptr;
    }
}

void TimelineModel::AddEvent(const ITimelineDecoder::Event& event)
{
    EventObj evt(event.m_Guid, event.m_TimeStamp, event.m_ThreadId);
    m_Events.emplace(event.m_Guid, evt);
}

EventObj* TimelineModel::FindEvent(uint64_t id)
{
    auto iter = m_Events.find(id);
    if (iter != m_Events.end())
    {
        return &(iter->second);
    }
    else
    {
        return nullptr;
    }
}

std::vector<std::string> GetModelDescription(const TimelineModel& model)
{
    std::vector<std::string> desc;
    for (auto& entry : model.GetEntities())
    {
        auto& entity = entry.second;
        desc.push_back(GetEntityDescription(entity));
        for (auto& connection : entity.GetConnections())
        {
            desc.push_back(GetConnectionDescription(connection));
        }
        for (auto child : entity.GetChildren())
        {
            desc.push_back(GetChildDescription(child));
        }
        for (auto execution : entity.GetExecutions())
        {
            desc.push_back(GetExecutionDescription(execution));
        }
        for (auto event : entity.GetEvents())
        {
            desc.push_back(GetEventDescription(event));
        }
    }
    return desc;
}

std::string GetEntityDescription(const Entity& entity)
{
    std::stringstream ss;
    ss << "Entity [" << entity.GetGuid() << "]";
    for (auto& attributeEntry : entity.GetAttributes())
    {
        if (profiling::LabelsAndEventClasses::PROCESS_ID_LABEL == attributeEntry.second.first)
        {
            ss << " " << attributeEntry.second.first << " = [processId]";
        }
        else {
            ss << " " << attributeEntry.second.first << " = " << attributeEntry.second.second;
        }
    }
    return ss.str();
}

std::string GetChildDescription(Entity* entity)
{
    std::stringstream ss;
    ss << "   child: " << GetEntityDescription(*entity);
    return ss.str();
}

std::string GetConnectionDescription(const Connection& connection)
{
    std::stringstream ss;
    ss << "   connection [" << connection.GetGuid() << "] from entity [";
    ss << connection.GetHead()->GetGuid() << "] to entity [" << connection.GetTail()->GetGuid() << "]";
    return ss.str();
}

std::string GetExecutionDescription(Entity* execution)
{
    std::stringstream ss;
    ss << "   execution: " << GetEntityDescription(*execution);
    return ss.str();
}

std::string GetEventDescription(EventObj* event)
{
    std::stringstream ss;
    ss << "   event: [" << event->GetGuid() << "] class [" << event->GetEventClass() << "]";
    return ss.str();
}

} // namespace profiling

} // namespace armnn