1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "ProfilingMocks.hpp"
7 #include "ProfilingTestUtils.hpp"
8
9 #include <SendTimelinePacket.hpp>
10 #include <TimelineUtilityMethods.hpp>
11 #include <LabelsAndEventClasses.hpp>
12 #include <ProfilingService.hpp>
13
14 #include <memory>
15
16 #include <boost/test/unit_test.hpp>
17
18 using namespace armnn;
19 using namespace armnn::profiling;
20
21 BOOST_AUTO_TEST_SUITE(TimelineUtilityMethodsTests)
22
BOOST_AUTO_TEST_CASE(CreateTypedLabelTest)23 BOOST_AUTO_TEST_CASE(CreateTypedLabelTest)
24 {
25 MockBufferManager mockBufferManager(1024);
26 ProfilingService profilingService;
27
28 std::unique_ptr<ISendTimelinePacket> sendTimelinePacket = std::make_unique<SendTimelinePacket>(mockBufferManager);
29 TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket);
30
31 // Generate first guid to ensure that the named typed entity guid is not 0 on local single test.
32 profilingService.NextGuid();
33
34 ProfilingGuid entityGuid(123);
35 const std::string entityName = "some entity";
36 ProfilingStaticGuid labelTypeGuid(456);
37
38 BOOST_CHECK_NO_THROW(timelineUtilityMethods.MarkEntityWithLabel(entityGuid, entityName, labelTypeGuid));
39
40 // Commit all packets at once
41 timelineUtilityMethods.Commit();
42
43 // Get the readable buffer
44 auto readableBuffer = mockBufferManager.GetReadableBuffer();
45 BOOST_CHECK(readableBuffer != nullptr);
46 unsigned int size = readableBuffer->GetSize();
47 BOOST_CHECK(size == 76);
48 const unsigned char* readableData = readableBuffer->GetReadableData();
49 BOOST_CHECK(readableData != nullptr);
50
51 // Utils
52 unsigned int offset = 0;
53
54 // Verify Header
55 VerifyTimelineHeaderBinary(readableData, offset, 68);
56
57 // First dataset sent: TimelineLabelBinaryPacket
58 VerifyTimelineLabelBinaryPacketData(EmptyOptional(), entityName, readableData, offset);
59
60 // Second dataset sent: TimelineRelationshipBinaryPacket
61 VerifyTimelineRelationshipBinaryPacketData(ProfilingRelationshipType::LabelLink,
62 EmptyOptional(),
63 entityGuid,
64 EmptyOptional(),
65 labelTypeGuid,
66 readableData,
67 offset);
68
69 // Mark the buffer as read
70 mockBufferManager.MarkRead(readableBuffer);
71 }
72
BOOST_AUTO_TEST_CASE(SendWellKnownLabelsAndEventClassesTest)73 BOOST_AUTO_TEST_CASE(SendWellKnownLabelsAndEventClassesTest)
74 {
75 MockBufferManager mockBufferManager(1024);
76 ProfilingService profilingService;
77 SendTimelinePacket sendTimelinePacket(mockBufferManager);
78
79 BOOST_CHECK_NO_THROW(TimelineUtilityMethods::SendWellKnownLabelsAndEventClasses(sendTimelinePacket));
80
81 // Get the readable buffer
82 auto readableBuffer = mockBufferManager.GetReadableBuffer();
83 BOOST_CHECK(readableBuffer != nullptr);
84 unsigned int size = readableBuffer->GetSize();
85 BOOST_TEST(size == 460);
86 const unsigned char* readableData = readableBuffer->GetReadableData();
87 BOOST_CHECK(readableData != nullptr);
88
89 // Utils
90 unsigned int offset = 0;
91
92 // Verify Header
93 VerifyTimelineHeaderBinary(readableData, offset, 452);
94
95 // First "well-known" label: NAME
96 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::NAME_GUID,
97 LabelsAndEventClasses::NAME_LABEL,
98 readableData,
99 offset);
100
101 // Second "well-known" label: TYPE
102 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::TYPE_GUID,
103 LabelsAndEventClasses::TYPE_LABEL,
104 readableData,
105 offset);
106
107 // Third "well-known" label: INDEX
108 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::INDEX_GUID,
109 LabelsAndEventClasses::INDEX_LABEL,
110 readableData,
111 offset);
112
113 // Forth "well-known" label: BACKENDID
114 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::BACKENDID_GUID,
115 LabelsAndEventClasses::BACKENDID_LABEL,
116 readableData,
117 offset);
118
119 // Fifth "well-known" label: CHILD
120 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::CHILD_GUID,
121 LabelsAndEventClasses::CHILD_LABEL,
122 readableData,
123 offset);
124
125 // Sixth "well-known" label: EXECUTION_OF
126 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::EXECUTION_OF_GUID,
127 LabelsAndEventClasses::EXECUTION_OF_LABEL,
128 readableData,
129 offset);
130
131 // Seventh "well-known" label: PROCESS_ID_LABEL
132 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::PROCESS_ID_GUID,
133 LabelsAndEventClasses::PROCESS_ID_LABEL,
134 readableData,
135 offset);
136
137 // Well-known types
138 // Layer
139 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::LAYER_GUID,
140 LabelsAndEventClasses::LAYER,
141 readableData,
142 offset);
143
144 // Workload
145 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::WORKLOAD_GUID,
146 LabelsAndEventClasses::WORKLOAD,
147 readableData,
148 offset);
149
150 // Network
151 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::NETWORK_GUID,
152 LabelsAndEventClasses::NETWORK,
153 readableData,
154 offset);
155
156 // Connection
157 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::CONNECTION_GUID,
158 LabelsAndEventClasses::CONNECTION,
159 readableData,
160 offset);
161
162 // Inference
163 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::INFERENCE_GUID,
164 LabelsAndEventClasses::INFERENCE,
165 readableData,
166 offset);
167
168 // Workload Execution
169 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::WORKLOAD_EXECUTION_GUID,
170 LabelsAndEventClasses::WORKLOAD_EXECUTION,
171 readableData,
172 offset);
173
174 // First "well-known" event class: START OF LIFE
175 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS_NAME_GUID,
176 LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS_NAME,
177 readableData,
178 offset);
179
180 VerifyTimelineEventClassBinaryPacketData(LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS,
181 LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS_NAME_GUID,
182 readableData,
183 offset);
184
185 // Second "well-known" event class: END OF LIFE
186 VerifyTimelineLabelBinaryPacketData(LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS_NAME_GUID,
187 LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS_NAME,
188 readableData,
189 offset);
190
191 VerifyTimelineEventClassBinaryPacketData(LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS,
192 LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS_NAME_GUID,
193 readableData,
194 offset);
195
196 // Mark the buffer as read
197 mockBufferManager.MarkRead(readableBuffer);
198 }
199
BOOST_AUTO_TEST_CASE(CreateNamedTypedChildEntityTest)200 BOOST_AUTO_TEST_CASE(CreateNamedTypedChildEntityTest)
201 {
202 MockBufferManager mockBufferManager(1024);
203 ProfilingService profilingService;
204 std::unique_ptr<ISendTimelinePacket> sendTimelinePacket = std::make_unique<SendTimelinePacket>(mockBufferManager);
205 TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket);
206
207 ProfilingDynamicGuid childEntityGuid(0);
208 ProfilingGuid parentEntityGuid(123);
209 const std::string entityName = "some entity";
210 const std::string entityType = "some type";
211
212 // Generate first guid to ensure that the named typed entity guid is not 0 on local single test.
213 profilingService.NextGuid();
214
215 BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedChildEntity(parentEntityGuid, "", entityType),
216 InvalidArgumentException);
217 BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedChildEntity(parentEntityGuid, entityName, ""),
218 InvalidArgumentException);
219 BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedChildEntity(
220 childEntityGuid, parentEntityGuid, "", entityType), InvalidArgumentException);
221 BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedChildEntity(
222 childEntityGuid, parentEntityGuid, entityName, ""), InvalidArgumentException);
223
224 BOOST_CHECK_NO_THROW(childEntityGuid = timelineUtilityMethods.CreateNamedTypedChildEntity(parentEntityGuid,
225 entityName,
226 entityType));
227 BOOST_CHECK(childEntityGuid != ProfilingGuid(0));
228
229 // Commit all packets at once
230 timelineUtilityMethods.Commit();
231
232 // Get the readable buffer
233 auto readableBuffer = mockBufferManager.GetReadableBuffer();
234 BOOST_CHECK(readableBuffer != nullptr);
235 unsigned int size = readableBuffer->GetSize();
236 BOOST_CHECK(size == 196);
237 const unsigned char* readableData = readableBuffer->GetReadableData();
238 BOOST_CHECK(readableData != nullptr);
239
240 // Utils
241 unsigned int offset = 0;
242
243 // Verify Header
244 VerifyTimelineHeaderBinary(readableData, offset, 188);
245
246 // First dataset sent: TimelineEntityBinaryPacket
247 VerifyTimelineEntityBinaryPacketData(EmptyOptional(), readableData, offset);
248
249 // Second dataset sent: TimelineLabelBinaryPacket
250 VerifyTimelineLabelBinaryPacketData(EmptyOptional(), entityName, readableData, offset);
251
252 // Third dataset sent: TimelineRelationshipBinaryPacket
253 VerifyTimelineRelationshipBinaryPacketData(ProfilingRelationshipType::LabelLink,
254 EmptyOptional(),
255 EmptyOptional(),
256 EmptyOptional(),
257 LabelsAndEventClasses::NAME_GUID,
258 readableData,
259 offset);
260
261 // Fifth dataset sent: TimelineLabelBinaryPacket
262 VerifyTimelineLabelBinaryPacketData(EmptyOptional(), entityType, readableData, offset);
263
264 // Sixth dataset sent: TimelineRelationshipBinaryPacket
265 VerifyTimelineRelationshipBinaryPacketData(ProfilingRelationshipType::LabelLink,
266 EmptyOptional(),
267 EmptyOptional(),
268 EmptyOptional(),
269 LabelsAndEventClasses::TYPE_GUID,
270 readableData,
271 offset);
272
273
274 // Eighth dataset sent: TimelineRelationshipBinaryPacket
275 VerifyTimelineRelationshipBinaryPacketData(ProfilingRelationshipType::RetentionLink,
276 EmptyOptional(),
277 parentEntityGuid,
278 EmptyOptional(),
279 EmptyOptional(),
280 readableData,
281 offset);
282
283 // Mark the buffer as read
284 mockBufferManager.MarkRead(readableBuffer);
285 }
286
BOOST_AUTO_TEST_CASE(DeclareLabelTest)287 BOOST_AUTO_TEST_CASE(DeclareLabelTest)
288 {
289 MockBufferManager mockBufferManager(1024);
290 ProfilingService profilingService;
291 std::unique_ptr<ISendTimelinePacket> sendTimelinePacket = std::make_unique<SendTimelinePacket>(mockBufferManager);
292 TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket);
293
294 // Generate first guid to ensure that the named typed entity guid is not 0 on local single test.
295 profilingService.NextGuid();
296
297 // Try declaring an invalid (empty) label
298 BOOST_CHECK_THROW(timelineUtilityMethods.DeclareLabel(""), InvalidArgumentException);
299
300 // Try declaring an invalid (wrong SWTrace format) label
301 BOOST_CHECK_THROW(timelineUtilityMethods.DeclareLabel("inv@lid lab€l"), RuntimeException);
302
303 // Declare a valid label
304 const std::string labelName = "valid label";
305 ProfilingGuid labelGuid = 0;
306 BOOST_CHECK_NO_THROW(labelGuid = timelineUtilityMethods.DeclareLabel(labelName));
307 BOOST_CHECK(labelGuid != ProfilingGuid(0));
308
309 // Try adding the same label as before
310 ProfilingGuid newLabelGuid = 0;
311 BOOST_CHECK_NO_THROW(newLabelGuid = timelineUtilityMethods.DeclareLabel(labelName));
312 BOOST_CHECK(newLabelGuid != ProfilingGuid(0));
313 BOOST_CHECK(newLabelGuid == labelGuid);
314 }
315
BOOST_AUTO_TEST_CASE(CreateNameTypeEntityInvalidTest)316 BOOST_AUTO_TEST_CASE(CreateNameTypeEntityInvalidTest)
317 {
318 MockBufferManager mockBufferManager(1024);
319 ProfilingService profilingService;
320 std::unique_ptr<ISendTimelinePacket> sendTimelinePacket = std::make_unique<SendTimelinePacket>(mockBufferManager);
321 TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket);
322
323 // Invalid name
324 BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity("", "Type"), InvalidArgumentException);
325
326 // Invalid type
327 BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity("Name", ""), InvalidArgumentException);
328
329 ProfilingDynamicGuid guid = profilingService.NextGuid();
330
331 // CreatedNamedTypedEntity with Guid - Invalid name
332 BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity(guid, "", "Type"),
333 InvalidArgumentException);
334
335 // CreatedNamedTypedEntity with Guid - Invalid type
336 BOOST_CHECK_THROW(timelineUtilityMethods.CreateNamedTypedEntity(guid, "Name", ""),
337 InvalidArgumentException);
338
339 }
340
BOOST_AUTO_TEST_CASE(CreateNameTypeEntityTest)341 BOOST_AUTO_TEST_CASE(CreateNameTypeEntityTest)
342 {
343 MockBufferManager mockBufferManager(1024);
344 ProfilingService profilingService;
345 std::unique_ptr<ISendTimelinePacket> sendTimelinePacket = std::make_unique<SendTimelinePacket>(mockBufferManager);
346 TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket);
347
348 const std::string entityName = "Entity0";
349 const std::string entityType = "Type0";
350
351 // Generate first guid to ensure that the named typed entity guid is not 0 on local single test.
352 profilingService.NextGuid();
353
354 ProfilingDynamicGuid guid = timelineUtilityMethods.CreateNamedTypedEntity(entityName, entityType);
355 BOOST_CHECK(guid != ProfilingGuid(0));
356
357 // Commit all packets at once
358 timelineUtilityMethods.Commit();
359
360 // Get the readable buffer
361 auto readableBuffer = mockBufferManager.GetReadableBuffer();
362 BOOST_CHECK(readableBuffer != nullptr);
363 unsigned int size = readableBuffer->GetSize();
364 BOOST_CHECK(size == 148);
365 const unsigned char* readableData = readableBuffer->GetReadableData();
366 BOOST_CHECK(readableData != nullptr);
367
368 // Utils
369 unsigned int offset = 0;
370
371 // Verify Header
372 VerifyTimelineHeaderBinary(readableData, offset, 140);
373
374 // First dataset sent: TimelineEntityBinaryPacket
375 VerifyTimelineEntityBinaryPacketData(guid, readableData, offset);
376
377 // Packets for Name Entity
378 // First dataset sent: TimelineLabelBinaryPacket
379 VerifyTimelineLabelBinaryPacketData(EmptyOptional(), entityName, readableData, offset);
380
381 // Second dataset sent: TimelineRelationshipBinaryPacket
382 VerifyTimelineRelationshipBinaryPacketData(ProfilingRelationshipType::LabelLink,
383 EmptyOptional(),
384 EmptyOptional(),
385 EmptyOptional(),
386 LabelsAndEventClasses::NAME_GUID,
387 readableData,
388 offset);
389
390 // Packets for Type Entity
391 // First dataset sent: TimelineLabelBinaryPacket
392 VerifyTimelineLabelBinaryPacketData(EmptyOptional(), entityType, readableData, offset);
393
394 // Second dataset sent: TimelineRelationshipBinaryPacket
395 VerifyTimelineRelationshipBinaryPacketData(ProfilingRelationshipType::LabelLink,
396 EmptyOptional(),
397 EmptyOptional(),
398 EmptyOptional(),
399 LabelsAndEventClasses::TYPE_GUID,
400 readableData,
401 offset);
402
403
404 // Mark the buffer as read
405 mockBufferManager.MarkRead(readableBuffer);
406 }
407
BOOST_AUTO_TEST_CASE(RecordEventTest)408 BOOST_AUTO_TEST_CASE(RecordEventTest)
409 {
410 MockBufferManager mockBufferManager(1024);
411 ProfilingService profilingService;
412 std::unique_ptr<ISendTimelinePacket> sendTimelinePacket = std::make_unique<SendTimelinePacket>(mockBufferManager);
413 TimelineUtilityMethods timelineUtilityMethods(sendTimelinePacket);
414 // Generate first guid to ensure that the named typed entity guid is not 0 on local single test.
415 profilingService.NextGuid();
416
417 ProfilingGuid entityGuid(123);
418 ProfilingStaticGuid eventClassGuid(456);
419 ProfilingDynamicGuid eventGuid(0);
420 BOOST_CHECK_NO_THROW(eventGuid = timelineUtilityMethods.RecordEvent(entityGuid, eventClassGuid));
421 BOOST_CHECK(eventGuid != ProfilingGuid(0));
422
423 // Commit all packets at once
424 timelineUtilityMethods.Commit();
425
426 // Get the readable buffer
427 auto readableBuffer = mockBufferManager.GetReadableBuffer();
428 BOOST_CHECK(readableBuffer != nullptr);
429 unsigned int size = readableBuffer->GetSize();
430
431 BOOST_CHECK(size == 68 + ThreadIdSize);
432
433 const unsigned char* readableData = readableBuffer->GetReadableData();
434 BOOST_CHECK(readableData != nullptr);
435
436 // Utils
437 unsigned int offset = 0;
438
439 // Verify Header
440 VerifyTimelineHeaderBinary(readableData, offset, 60 + ThreadIdSize);
441
442 // First dataset sent: TimelineEntityBinaryPacket
443 VerifyTimelineEventBinaryPacket(EmptyOptional(), EmptyOptional(), EmptyOptional(), readableData, offset);
444
445 // Second dataset sent: TimelineRelationshipBinaryPacket
446 VerifyTimelineRelationshipBinaryPacketData(ProfilingRelationshipType::ExecutionLink,
447 EmptyOptional(),
448 entityGuid,
449 eventGuid,
450 eventClassGuid,
451 readableData,
452 offset);
453
454 // Mark the buffer as read
455 mockBufferManager.MarkRead(readableBuffer);
456 }
457
458 BOOST_AUTO_TEST_SUITE_END()
459