IVGCVSW-4065 Add a RecordEvent function
[platform/upstream/armnn.git] / src / profiling / TimelineUtilityMethods.cpp
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "TimelineUtilityMethods.hpp"
7 #include "ProfilingService.hpp"
8 #include "LabelsAndEventClasses.hpp"
9
10 namespace armnn
11 {
12
13 namespace profiling
14 {
15
16 void TimelineUtilityMethods::SendWellKnownLabelsAndEventClasses()
17 {
18     // Send the "name" label, this call throws in case of error
19     m_SendTimelinePacket.SendTimelineLabelBinaryPacket(LabelsAndEventClasses::NAME_GUID,
20                                                        LabelsAndEventClasses::NAME_LABEL);
21
22     // Send the "type" label, this call throws in case of error
23     m_SendTimelinePacket.SendTimelineLabelBinaryPacket(LabelsAndEventClasses::TYPE_GUID,
24                                                        LabelsAndEventClasses::TYPE_LABEL);
25
26     // Send the "index" label, this call throws in case of error
27     m_SendTimelinePacket.SendTimelineLabelBinaryPacket(LabelsAndEventClasses::INDEX_GUID,
28                                                        LabelsAndEventClasses::INDEX_LABEL);
29
30     // Send the "start of life" event class, this call throws in case of error
31     m_SendTimelinePacket.SendTimelineEventClassBinaryPacket(LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS);
32
33     // Send the "end of life" event class, this call throws in case of error
34     m_SendTimelinePacket.SendTimelineEventClassBinaryPacket(LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS);
35 }
36
37 ProfilingDynamicGuid TimelineUtilityMethods::CreateNamedTypedEntity(const std::string& name, const std::string& type)
38 {
39     // Check that the entity name is valid
40     if (name.empty())
41     {
42         throw InvalidArgumentException("Invalid entity name, the entity name cannot be empty");
43     }
44
45     // Check that the entity type is valid
46     if (type.empty())
47     {
48         throw InvalidArgumentException("Invalid entity type, the entity type cannot be empty");
49     }
50
51     // Generate dynamic GUID of the entity
52     ProfilingDynamicGuid entityGuid = ProfilingService::Instance().NextGuid();
53
54     // Send Entity Binary Packet of the entity to the external profiling service
55     m_SendTimelinePacket.SendTimelineEntityBinaryPacket(entityGuid);
56
57     // Create name entity and send the relationship of the entity with the given name
58     NameEntity(entityGuid, name);
59
60     // Create type entity and send the relationship of the entity with the given type
61     TypeEntity(entityGuid, type);
62
63     return entityGuid;
64 }
65
66 ProfilingStaticGuid TimelineUtilityMethods::DeclareLabel(const std::string& labelName)
67 {
68     // Check that the label name is valid
69     if (labelName.empty())
70     {
71         // The label name is invalid
72         throw InvalidArgumentException("Invalid label name, the label name cannot be empty");
73     }
74
75     // Generate a static GUID for the given label name
76     ProfilingStaticGuid labelGuid = ProfilingService::Instance().GenerateStaticId(labelName);
77
78     // Send the new label to the external profiling service, this call throws in case of error
79     m_SendTimelinePacket.SendTimelineLabelBinaryPacket(labelGuid, labelName);
80
81     return labelGuid;
82 }
83
84 void TimelineUtilityMethods::CreateTypedLabel(ProfilingGuid entityGuid,
85                                               const std::string& entityName,
86                                               ProfilingStaticGuid labelTypeGuid)
87 {
88     // Check that the entity name is valid
89     if (entityName.empty())
90     {
91         // The entity name is invalid
92         throw InvalidArgumentException("Invalid entity name, the entity name cannot be empty");
93     }
94
95     // Declare a label with the entity's name, this call throws in case of error
96     ProfilingStaticGuid labelGuid = DeclareLabel(entityName);
97
98     // Generate a GUID for the label relationship
99     ProfilingDynamicGuid relationshipGuid = ProfilingService::Instance().NextGuid();
100
101     // Send the new label link to the external profiling service, this call throws in case of error
102     m_SendTimelinePacket.SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink,
103                                                               relationshipGuid,
104                                                               entityGuid,
105                                                               labelGuid);
106
107     // Generate a GUID for the label relationship
108     ProfilingDynamicGuid relationshipLabelGuid = ProfilingService::Instance().NextGuid();
109
110     // Send the new label link to the external profiling service, this call throws in case of error
111     m_SendTimelinePacket.SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::LabelLink,
112                                                               relationshipLabelGuid,
113                                                               relationshipGuid,
114                                                               labelTypeGuid);
115 }
116
117 void TimelineUtilityMethods::NameEntity(ProfilingGuid entityGuid, const std::string& name)
118 {
119     CreateTypedLabel(entityGuid, name, LabelsAndEventClasses::NAME_GUID);
120 }
121
122 void TimelineUtilityMethods::TypeEntity(ProfilingGuid entityGuid, const std::string& type)
123 {
124     CreateTypedLabel(entityGuid, type, LabelsAndEventClasses::TYPE_GUID);
125 }
126
127 ProfilingDynamicGuid TimelineUtilityMethods::CreateNamedTypedChildEntity(ProfilingGuid parentEntityGuid,
128                                                                          const std::string& entityName,
129                                                                          const std::string& entityType)
130 {
131     // Check that the entity name is valid
132     if (entityName.empty())
133     {
134         // The entity name is invalid
135         throw InvalidArgumentException("Invalid entity name, the entity name cannot be empty");
136     }
137
138     // Check that the entity type is valid
139     if (entityType.empty())
140     {
141         // The entity type is invalid
142         throw InvalidArgumentException("Invalid entity type, the entity type cannot be empty");
143     }
144
145     // Create a named type entity from the given name and type, this call throws in case of error
146     ProfilingDynamicGuid childEntityGuid = CreateNamedTypedEntity(entityName, entityType);
147
148     // Generate a GUID for the retention link relationship
149     ProfilingDynamicGuid retentionLinkGuid = ProfilingService::Instance().NextGuid();
150
151     // Send the new retention link to the external profiling service, this call throws in case of error
152     m_SendTimelinePacket.SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::RetentionLink,
153                                                               retentionLinkGuid,
154                                                               parentEntityGuid,
155                                                               childEntityGuid);
156
157     return childEntityGuid;
158 }
159
160 ProfilingDynamicGuid TimelineUtilityMethods::RecordEvent(ProfilingGuid entityGuid, ProfilingStaticGuid eventClassGuid)
161 {
162     // Take a timestamp
163     uint64_t timestamp = GetTimestamp();
164
165     // Get the thread id
166     std::thread::id threadId = std::this_thread::get_id();
167
168     // Generate a GUID for the event
169     ProfilingDynamicGuid eventGuid = ProfilingService::Instance().NextGuid();
170
171     // Send the new timeline event to the external profiling service, this call throws in case of error
172     m_SendTimelinePacket.SendTimelineEventBinaryPacket(timestamp, threadId, eventGuid);
173
174     // Generate a GUID for the execution link
175     ProfilingDynamicGuid executionLinkId = ProfilingService::Instance().NextGuid();
176
177     // Send the new execution link to the external profiling service, this call throws in case of error
178     m_SendTimelinePacket.SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::ExecutionLink,
179                                                               executionLinkId,
180                                                               entityGuid,
181                                                               eventGuid);
182
183     // Generate a GUID for the data relationship link
184     ProfilingDynamicGuid eventClassLinkId = ProfilingService::Instance().NextGuid();
185
186     // Send the new data relationship link to the external profiling service, this call throws in case of error
187     m_SendTimelinePacket.SendTimelineRelationshipBinaryPacket(ProfilingRelationshipType::DataLink,
188                                                               eventClassLinkId,
189                                                               entityGuid,
190                                                               eventClassGuid);
191
192     return eventGuid;
193 }
194
195 } // namespace profiling
196
197 } // namespace armnn