ArmNN
 20.02
TimelineTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
8 #include <TimelineDecoder.hpp>
9 
11 #include <ProfilingService.hpp>
12 #include <PacketBuffer.hpp>
14 
15 #include <boost/test/test_tools.hpp>
16 #include <boost/test/unit_test_suite.hpp>
17 
18 BOOST_AUTO_TEST_SUITE(TimelineDecoderTests)
19 
20 using namespace armnn;
21 using namespace timelinedecoder;
22 
23 void SendTimelinePacketToCommandHandler(const unsigned char* packetBuffer,
25 {
26  uint32_t uint32_t_size = sizeof(uint32_t);
27  unsigned int offset = 0;
28 
29  uint32_t header[2];
30  header[0] = profiling::ReadUint32(packetBuffer, offset);
31  offset += uint32_t_size;
32  header[1] = profiling::ReadUint32(packetBuffer, offset);
33  offset += uint32_t_size;
34  uint32_t PacketDataLength = header[1] & 0x00FFFFFF;
35 
36  auto uniquePacketData = std::make_unique<unsigned char[]>(PacketDataLength);
37  std::memcpy(uniquePacketData.get(), packetBuffer + offset, PacketDataLength);
38 
39  armnn::profiling::Packet packet(header[0], PacketDataLength, uniquePacketData);
40 
41  BOOST_CHECK(std::memcmp(packetBuffer + offset, packet.GetData(), packet.GetLength()) == 0);
42 
43  CommandHandler(packet);
44 }
45 
47 {
48  model.m_Entities.emplace_back(entity);
49 }
50 
52 {
53  model.m_EventClasses.emplace_back(eventClass);
54 }
55 
57 {
58  model.m_Events.emplace_back(event);
59 }
60 
62 {
63  model.m_Labels.emplace_back(label);
64 }
65 
67 {
68  model.m_Relationships.emplace_back(relationship);
69 }
70 
71 BOOST_AUTO_TEST_CASE(TimelineDirectoryTest)
72 {
73  uint32_t uint8_t_size = sizeof(uint8_t);
74  uint32_t uint32_t_size = sizeof(uint32_t);
75  uint32_t uint64_t_size = sizeof(uint64_t);
76  uint32_t threadId_size = sizeof(std::thread::id);
77 
78  profiling::BufferManager bufferManager(5);
79  profiling::TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager);
80 
81  std::unique_ptr<profiling::ISendTimelinePacket> sendTimelinePacket =
82  timelinePacketWriterFactory.GetSendTimelinePacket();
83 
84  profiling::PacketVersionResolver packetVersionResolver;
85 
86  TimelineDirectoryCaptureCommandHandler timelineDirectoryCaptureCommandHandler(
87  1, 0, packetVersionResolver.ResolvePacketVersion(1, 0).GetEncodedValue(), true);
88 
89  sendTimelinePacket->SendTimelineMessageDirectoryPackage();
90  sendTimelinePacket->Commit();
91 
92  std::vector<profiling::SwTraceMessage> swTraceBufferMessages;
93 
94  unsigned int offset = uint32_t_size * 2;
95 
96  std::unique_ptr<profiling::IPacketBuffer> packetBuffer = bufferManager.GetReadableBuffer();
97 
98  uint8_t readStreamVersion = ReadUint8(packetBuffer, offset);
99  BOOST_CHECK(readStreamVersion == 4);
100  offset += uint8_t_size;
101  uint8_t readPointerBytes = ReadUint8(packetBuffer, offset);
102  BOOST_CHECK(readPointerBytes == uint64_t_size);
103  offset += uint8_t_size;
104  uint8_t readThreadIdBytes = ReadUint8(packetBuffer, offset);
105  BOOST_CHECK(readThreadIdBytes == threadId_size);
106  offset += uint8_t_size;
107 
108  uint32_t declarationSize = profiling::ReadUint32(packetBuffer, offset);
109  offset += uint32_t_size;
110  for(uint32_t i = 0; i < declarationSize; ++i)
111  {
112  swTraceBufferMessages.push_back(profiling::ReadSwTraceMessage(packetBuffer->GetReadableData(), offset));
113  }
114 
115  SendTimelinePacketToCommandHandler(packetBuffer->GetReadableData(), timelineDirectoryCaptureCommandHandler);
116 
117  for(uint32_t index = 0; index < declarationSize; ++index)
118  {
119  profiling::SwTraceMessage& bufferMessage = swTraceBufferMessages[index];
120  profiling::SwTraceMessage& handlerMessage = timelineDirectoryCaptureCommandHandler.m_SwTraceMessages[index];
121 
122  BOOST_CHECK(bufferMessage.m_Name == handlerMessage.m_Name);
123  BOOST_CHECK(bufferMessage.m_UiName == handlerMessage.m_UiName);
124  BOOST_CHECK(bufferMessage.m_Id == handlerMessage.m_Id);
125 
126  BOOST_CHECK(bufferMessage.m_ArgTypes.size() == handlerMessage.m_ArgTypes.size());
127  for(uint32_t i = 0; i < bufferMessage.m_ArgTypes.size(); ++i)
128  {
129  BOOST_CHECK(bufferMessage.m_ArgTypes[i] == handlerMessage.m_ArgTypes[i]);
130  }
131 
132  BOOST_CHECK(bufferMessage.m_ArgNames.size() == handlerMessage.m_ArgNames.size());
133  for(uint32_t i = 0; i < bufferMessage.m_ArgNames.size(); ++i)
134  {
135  BOOST_CHECK(bufferMessage.m_ArgNames[i] == handlerMessage.m_ArgNames[i]);
136  }
137  }
138 }
139 
140 BOOST_AUTO_TEST_CASE(TimelineCaptureTest)
141 {
142  unsigned int threadIdSize = sizeof(std::thread::id);
143  profiling::BufferManager bufferManager(50);
144  profiling::TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager);
145 
146  std::unique_ptr<profiling::ISendTimelinePacket> sendTimelinePacket =
147  timelinePacketWriterFactory.GetSendTimelinePacket();
148 
149  profiling::PacketVersionResolver packetVersionResolver;
150 
151  TimelineDecoder timelineDecoder;
152  const TimelineDecoder::Model& model = timelineDecoder.GetModel();
153 
154  TimelineCaptureCommandHandler timelineCaptureCommandHandler(
155  1, 1, packetVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), timelineDecoder, threadIdSize);
156 
158  BOOST_CHECK(timelineDecoder.SetEntityCallback(PushEntity) == Status::TimelineStatus_Success);
159  BOOST_CHECK(timelineDecoder.SetEventClassCallback(PushEventClass) == Status::TimelineStatus_Success);
160  BOOST_CHECK(timelineDecoder.SetEventCallback(PushEvent) == Status::TimelineStatus_Success);
161  BOOST_CHECK(timelineDecoder.SetLabelCallback(PushLabel) == Status::TimelineStatus_Success);
162  BOOST_CHECK(timelineDecoder.SetRelationshipCallback(PushRelationship) == Status::TimelineStatus_Success);
163 
164  const uint64_t entityGuid = 111111u;
165  const uint64_t eventClassGuid = 22222u;
166  const uint64_t timestamp = 33333u;
167  const uint64_t eventGuid = 44444u;
168 
169  const std::thread::id threadId = std::this_thread::get_id();
170 
171  // need to do a bit of work here to extract the value from threadId
172  unsigned char* uCharThreadId = new unsigned char[threadIdSize]();;
173  uint64_t uint64ThreadId;
174 
175  profiling::WriteBytes(uCharThreadId, 0, &threadId, threadIdSize);
176 
177  if (threadIdSize == 4)
178  {
179  uint64ThreadId = profiling::ReadUint32(uCharThreadId, 0);
180  }
181  else if (threadIdSize == 8)
182  {
183  uint64ThreadId = profiling::ReadUint64(uCharThreadId, 0);
184  }
185  delete[] uCharThreadId;
186 
187  const uint64_t labelGuid = 66666u;
188  std::string labelName = "test_label";
189 
190  const uint64_t relationshipGuid = 77777u;
191  const uint64_t headGuid = 888888u;
192  const uint64_t tailGuid = 999999u;
193 
194  for (int i = 0; i < 10; ++i)
195  {
196  // Send entity
197  sendTimelinePacket->SendTimelineEntityBinaryPacket(entityGuid);
198  sendTimelinePacket->Commit();
199  SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
200  timelineCaptureCommandHandler);
201 
202  // Send event class
203  sendTimelinePacket->SendTimelineEventClassBinaryPacket(eventClassGuid);
204  sendTimelinePacket->Commit();
205  SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
206  timelineCaptureCommandHandler);
207 
208  // Send event
209  sendTimelinePacket->SendTimelineEventBinaryPacket(timestamp, threadId, eventGuid);
210  sendTimelinePacket->Commit();
211  SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
212  timelineCaptureCommandHandler);
213 
214  // Send label
215  sendTimelinePacket->SendTimelineLabelBinaryPacket(labelGuid, labelName);
216  sendTimelinePacket->Commit();
217  SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
218  timelineCaptureCommandHandler);
219 
220  // Send relationship
222  sendTimelinePacket->SendTimelineRelationshipBinaryPacket(relationshipType,
223  relationshipGuid,
224  headGuid,
225  tailGuid);
226  sendTimelinePacket->Commit();
227  SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
228  timelineCaptureCommandHandler);
229  }
230 
231  for (unsigned long i = 0; i < 10; ++i)
232  {
233  BOOST_CHECK(model.m_Entities[i].m_Guid == entityGuid);
234 
235  BOOST_CHECK(model.m_EventClasses[i].m_Guid == eventClassGuid);
236 
237  BOOST_CHECK(model.m_Events[i].m_TimeStamp == timestamp);
238  BOOST_CHECK(model.m_Events[i].m_ThreadId == uint64ThreadId);
239  BOOST_CHECK(model.m_Events[i].m_Guid == eventGuid);
240 
241  BOOST_CHECK(model.m_Labels[i].m_Guid == labelGuid);
242  BOOST_CHECK(model.m_Labels[i].m_Name == labelName);
243 
245  BOOST_CHECK(model.m_Relationships[i].m_Guid == relationshipGuid);
246  BOOST_CHECK(model.m_Relationships[i].m_HeadGuid == headGuid);
247  BOOST_CHECK(model.m_Relationships[i].m_TailGuid == tailGuid);
248  }
249 }
250 
251 BOOST_AUTO_TEST_CASE(TimelineCaptureTestMultipleStringsInBuffer)
252 {
253  unsigned int threadIdSize = sizeof(std::thread::id);
254  profiling::BufferManager bufferManager(50);
255  profiling::TimelinePacketWriterFactory timelinePacketWriterFactory(bufferManager);
256 
257  std::unique_ptr<profiling::ISendTimelinePacket> sendTimelinePacket =
258  timelinePacketWriterFactory.GetSendTimelinePacket();
259 
260  profiling::PacketVersionResolver packetVersionResolver;
261 
262  TimelineDecoder timelineDecoder;
263  const TimelineDecoder::Model& model = timelineDecoder.GetModel();
264 
265  TimelineCaptureCommandHandler timelineCaptureCommandHandler(
266  1, 1, packetVersionResolver.ResolvePacketVersion(1, 1).GetEncodedValue(), timelineDecoder, threadIdSize);
267 
269  BOOST_CHECK(timelineDecoder.SetEntityCallback(PushEntity) == Status::TimelineStatus_Success);
270  BOOST_CHECK(timelineDecoder.SetEventClassCallback(PushEventClass) == Status::TimelineStatus_Success);
271  BOOST_CHECK(timelineDecoder.SetEventCallback(PushEvent) == Status::TimelineStatus_Success);
272  BOOST_CHECK(timelineDecoder.SetLabelCallback(PushLabel) == Status::TimelineStatus_Success);
273  BOOST_CHECK(timelineDecoder.SetRelationshipCallback(PushRelationship) == Status::TimelineStatus_Success);
274 
275  const uint64_t entityGuid = 111111u;
276  const uint64_t eventClassGuid = 22222u;
277  const uint64_t timestamp = 33333u;
278  const uint64_t eventGuid = 44444u;
279 
280  const std::thread::id threadId = std::this_thread::get_id();
281 
282  // need to do a bit of work here to extract the value from threadId
283  unsigned char* uCharThreadId = new unsigned char[threadIdSize]();;
284  uint64_t uint64ThreadId;
285 
286  profiling::WriteBytes(uCharThreadId, 0, &threadId, threadIdSize);
287 
288  if ( threadIdSize == 4 )
289  {
290  uint64ThreadId = profiling::ReadUint32(uCharThreadId, 0);
291  } else if ( threadIdSize == 8 )
292  {
293  uint64ThreadId = profiling::ReadUint64(uCharThreadId, 0);
294  }
295  delete[] uCharThreadId;
296 
297  const uint64_t labelGuid = 66666u;
298  std::string labelName = "test_label";
299  std::string labelName2 = "test_label2";
300  std::string labelName3 = "test_label32";
301 
302  const uint64_t relationshipGuid = 77777u;
303  const uint64_t headGuid = 888888u;
304  const uint64_t tailGuid = 999999u;
305 
306  // Check with multiple messages in the same buffer
307  for ( int i = 0; i < 9; ++i )
308  {
309  // Send entity
310  sendTimelinePacket->SendTimelineEntityBinaryPacket(entityGuid);
311  // Send event class
312  sendTimelinePacket->SendTimelineEventClassBinaryPacket(eventClassGuid);
313  // Send event
314  sendTimelinePacket->SendTimelineEventBinaryPacket(timestamp, threadId, eventGuid);
315  // Send label
316  sendTimelinePacket->SendTimelineLabelBinaryPacket(labelGuid, labelName);
317  sendTimelinePacket->SendTimelineLabelBinaryPacket(labelGuid, labelName2);
318  sendTimelinePacket->SendTimelineLabelBinaryPacket(labelGuid, labelName3);
319  // Send relationship
321  sendTimelinePacket->SendTimelineRelationshipBinaryPacket(relationshipType,
322  relationshipGuid,
323  headGuid,
324  tailGuid);
325  }
326 
327  sendTimelinePacket->Commit();
328  SendTimelinePacketToCommandHandler(bufferManager.GetReadableBuffer()->GetReadableData(),
329  timelineCaptureCommandHandler);
330 
331  for ( unsigned long i = 0; i < 9; ++i )
332  {
333  BOOST_CHECK(model.m_Entities[i].m_Guid == entityGuid);
334 
335  BOOST_CHECK(model.m_EventClasses[i].m_Guid == eventClassGuid);
336 
337  BOOST_CHECK(model.m_Labels[i].m_Guid == labelGuid);
338 
339  BOOST_CHECK(model.m_Events[i].m_TimeStamp == timestamp);
340  BOOST_CHECK(model.m_Events[i].m_ThreadId == uint64ThreadId);
341  BOOST_CHECK(model.m_Events[i].m_Guid == eventGuid);
342 
344  BOOST_CHECK(model.m_Relationships[i].m_Guid == relationshipGuid);
345  BOOST_CHECK(model.m_Relationships[i].m_HeadGuid == headGuid);
346  BOOST_CHECK(model.m_Relationships[i].m_TailGuid == tailGuid);
347  }
348  for ( unsigned long i = 0; i < 9; i += 3 )
349  {
350  BOOST_CHECK(model.m_Labels[i].m_Name == labelName);
351  BOOST_CHECK(model.m_Labels[i+1].m_Name == labelName2);
352  BOOST_CHECK(model.m_Labels[i+2].m_Name == labelName3);
353  }
354 }
355 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
void SendTimelinePacketToCommandHandler(const unsigned char *packetBuffer, profiling::CommandHandlerFunctor &CommandHandler)
Head execution start depends on Tail execution completion.
TimelineStatus SetRelationshipCallback(const OnNewRelationshipCallback)
void PushEventClass(TimelineDecoder::Model &model, const ITimelineDecoder::EventClass eventClass)
uint64_t ReadUint64(const IPacketBufferPtr &packetBuffer, unsigned int offset)
Version ResolvePacketVersion(uint32_t familyId, uint32_t packetId) const
uint8_t ReadUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset)
Copyright (c) 2020 ARM Limited.
Head execution start depends on Tail execution completion.
BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::WaitingForAck)
SwTraceMessage ReadSwTraceMessage(const unsigned char *packetBuffer, unsigned int &offset)
std::vector< std::string > m_ArgNames
void WriteBytes(const IPacketBufferPtr &packetBuffer, unsigned int offset, const void *value, unsigned int valueSize)
void PushRelationship(TimelineDecoder::Model &model, const ITimelineDecoder::Relationship relationship)
void PushEntity(TimelineDecoder::Model &model, const ITimelineDecoder::Entity entity)
TimelineStatus SetEventClassCallback(const OnNewEventClassCallback)
Status
enumeration
Definition: Types.hpp:26
BOOST_AUTO_TEST_CASE(CheckConvolution2dLayer)
IPacketBufferPtr GetReadableBuffer() override
void PushEvent(TimelineDecoder::Model &model, const ITimelineDecoder::Event event)
uint32_t ReadUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset)
void PushLabel(TimelineDecoder::Model &model, const ITimelineDecoder::Label label)
BOOST_AUTO_TEST_SUITE_END()
TimelineStatus SetLabelCallback(const OnNewLabelCallback)
std::unique_ptr< ISendTimelinePacket > GetSendTimelinePacket() const
TimelineStatus SetEntityCallback(const OnNewEntityCallback)
TimelineStatus SetEventCallback(const OnNewEventCallback)