27 using namespace armnn;
28 using namespace std::this_thread;
31 TEST_CASE(
"CounterCaptureHandlingTest")
33 arm::pipe::PacketVersionResolver packetVersionResolver;
36 std::vector<std::pair<uint16_t, uint32_t>> indexValuePairs;
37 indexValuePairs.reserve(5);
38 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(0, 100));
39 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(1, 200));
40 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(2, 300));
41 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(3, 400));
42 indexValuePairs.emplace_back(std::make_pair<uint16_t, uint32_t>(4, 500));
45 uint32_t dataLength = 38;
48 uint64_t time =
static_cast<uint64_t
>(
49 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch())
52 uint64_t time2 = time + 5000;
55 std::unique_ptr<unsigned char[]> uniqueData1 = std::make_unique<unsigned char[]>(dataLength);
56 unsigned char* data1 =
reinterpret_cast<unsigned char*
>(uniqueData1.get());
58 std::unique_ptr<unsigned char[]> uniqueData2 = std::make_unique<unsigned char[]>(dataLength);
59 unsigned char* data2 =
reinterpret_cast<unsigned char*
>(uniqueData2.get());
68 offset += sizeOfUint64;
69 for (
const auto& pair : indexValuePairs)
72 offset += sizeOfUint16;
74 offset += sizeOfUint32;
80 offset += sizeOfUint64;
81 for (
const auto& pair : indexValuePairs)
84 offset += sizeOfUint16;
86 offset += sizeOfUint32;
89 uint32_t headerWord1 = packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue();
91 arm::pipe::Packet packet1(headerWord1, dataLength, uniqueData1);
92 arm::pipe::Packet packet2(headerWord1, dataLength, uniqueData2);
97 commandHandler(packet1);
98 commandHandler(packet2);
100 ARMNN_ASSERT(commandHandler.m_CurrentPeriodValue == 5000);
102 for (
size_t i = 0; i < commandHandler.m_CounterCaptureValues.m_Uids.size(); ++i)
104 ARMNN_ASSERT(commandHandler.m_CounterCaptureValues.m_Uids[i] == i);
108 void WaitFor(std::function<
bool()> predicate, std::string errorMsg, uint32_t timeout = 2000, uint32_t sleepTime = 50)
110 uint32_t timeSlept = 0;
113 if (timeSlept >= timeout)
115 FAIL(
"Timeout: " << errorMsg);
117 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
118 timeSlept += sleepTime;
122 void CheckTimelineDirectory(arm::pipe::TimelineDirectoryCaptureCommandHandler& commandHandler)
124 uint32_t uint8_t_size =
sizeof(uint8_t);
126 uint32_t uint64_t_size =
sizeof(uint64_t);
127 uint32_t threadId_size =
sizeof(int);
132 std::unique_ptr<profiling::ISendTimelinePacket> sendTimelinePacket =
133 timelinePacketWriterFactory.GetSendTimelinePacket();
135 sendTimelinePacket->SendTimelineMessageDirectoryPackage();
136 sendTimelinePacket->Commit();
138 std::vector<arm::pipe::SwTraceMessage> swTraceBufferMessages;
140 unsigned int offset = uint32_t_size * 2;
142 std::unique_ptr<profiling::IPacketBuffer> packetBuffer = bufferManager.GetReadableBuffer();
144 uint8_t readStreamVersion =
ReadUint8(packetBuffer, offset);
145 CHECK(readStreamVersion == 4);
146 offset += uint8_t_size;
147 uint8_t readPointerBytes =
ReadUint8(packetBuffer, offset);
148 CHECK(readPointerBytes == uint64_t_size);
149 offset += uint8_t_size;
150 uint8_t readThreadIdBytes =
ReadUint8(packetBuffer, offset);
151 CHECK(readThreadIdBytes == threadId_size);
152 offset += uint8_t_size;
156 for(uint32_t i = 0; i < declarationSize; ++i)
158 swTraceBufferMessages.push_back(arm::pipe::ReadSwTraceMessage(packetBuffer->GetReadableData(),
160 packetBuffer->GetSize()));
163 for(uint32_t index = 0; index < declarationSize; ++index)
165 arm::pipe::SwTraceMessage& bufferMessage = swTraceBufferMessages[index];
166 arm::pipe::SwTraceMessage& handlerMessage = commandHandler.m_SwTraceMessages[index];
168 CHECK(bufferMessage.m_Name == handlerMessage.m_Name);
169 CHECK(bufferMessage.m_UiName == handlerMessage.m_UiName);
170 CHECK(bufferMessage.m_Id == handlerMessage.m_Id);
172 CHECK(bufferMessage.m_ArgTypes.size() == handlerMessage.m_ArgTypes.size());
173 for(uint32_t i = 0; i < bufferMessage.m_ArgTypes.size(); ++i)
175 CHECK(bufferMessage.m_ArgTypes[i] == handlerMessage.m_ArgTypes[i]);
178 CHECK(bufferMessage.m_ArgNames.size() == handlerMessage.m_ArgNames.size());
179 for(uint32_t i = 0; i < bufferMessage.m_ArgNames.size(); ++i)
181 CHECK(bufferMessage.m_ArgNames[i] == handlerMessage.m_ArgNames[i]);
186 void CheckTimelinePackets(arm::pipe::TimelineDecoder& timelineDecoder)
189 timelineDecoder.ApplyToModel([&](arm::pipe::TimelineDecoder::Model& m) {
190 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::NAME_GUID);
191 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::NAME_LABEL);
193 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::TYPE_GUID);
194 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::TYPE_LABEL);
196 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::INDEX_GUID);
197 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::INDEX_LABEL);
199 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::BACKENDID_GUID);
200 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::BACKENDID_LABEL);
202 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::CHILD_GUID);
203 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::CHILD_LABEL);
205 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::EXECUTION_OF_GUID);
206 CHECK(m.m_Labels[i++].m_Name ==
207 profiling::LabelsAndEventClasses::EXECUTION_OF_LABEL);
209 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::PROCESS_ID_GUID);
210 CHECK(m.m_Labels[i++].m_Name ==
211 profiling::LabelsAndEventClasses::PROCESS_ID_LABEL);
213 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::LAYER_GUID);
214 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::LAYER);
216 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::WORKLOAD_GUID);
217 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::WORKLOAD);
219 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::NETWORK_GUID);
220 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::NETWORK);
222 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::CONNECTION_GUID);
223 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::CONNECTION);
225 CHECK(m.m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::INFERENCE_GUID);
226 CHECK(m.m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::INFERENCE);
228 CHECK(m.m_Labels[i].m_Guid ==
229 profiling::LabelsAndEventClasses::WORKLOAD_EXECUTION_GUID);
230 CHECK(m.m_Labels[i++].m_Name ==
231 profiling::LabelsAndEventClasses::WORKLOAD_EXECUTION);
233 CHECK(m.m_EventClasses[0].m_Guid ==
234 profiling::LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS);
235 CHECK(m.m_EventClasses[1].m_Guid ==
236 profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS);
240 TEST_CASE(
"GatorDMockEndToEnd")
246 std::string udsNamespace =
"gatord_namespace";
248 CHECK_NOTHROW(arm::pipe::ConnectionHandler connectionHandler(udsNamespace,
false));
250 arm::pipe::ConnectionHandler connectionHandler(udsNamespace,
false);
262 profilingService.
Update();
264 profilingService.
Update();
267 auto basePipeServer = connectionHandler.GetNewBasePipeServer(
false);
272 arm::pipe::TimelineDecoder& timelineDecoder = mockService.GetTimelineDecoder();
274 mockService.GetDirectoryCaptureCommandHandler();
278 "Profiling service did not switch to WaitingForAck state");
280 profilingService.
Update();
282 if (!mockService.WaitForStreamMetaData())
284 FAIL(
"Failed to receive StreamMetaData");
287 mockService.SendConnectionAck();
289 mockService.LaunchReceivingThread();
292 "Profiling service did not switch to Active state");
296 "MockGatord did not receive counter directory packet");
299 WaitFor([&](){
return timelineDecoder.ApplyToModel([&](arm::pipe::TimelineDecoder::Model& m){
300 return m.m_EventClasses.size() >= 2;});},
301 "MockGatord did not receive well known timeline labels and event classes");
303 CheckTimelineDirectory(mockService.GetTimelineDirectoryCaptureCommandHandler());
305 CheckTimelinePackets(timelineDecoder);
320 for (
auto& device : serviceDevices)
323 auto foundDevice = receivedCounterDirectory.
GetDevices().find(device.second->m_Uid);
324 CHECK(foundDevice != receivedCounterDirectory.
GetDevices().end());
325 CHECK(device.second->m_Name.compare((*foundDevice).second->m_Name) == 0);
326 CHECK(device.second->m_Cores == (*foundDevice).second->m_Cores);
330 for (
auto& counterSet : serviceCounterSets)
333 auto foundCounterSet = receivedCounterDirectory.
GetCounterSets().find(counterSet.second->m_Uid);
334 CHECK(foundCounterSet != receivedCounterDirectory.
GetCounterSets().end());
335 CHECK(counterSet.second->m_Name.compare((*foundCounterSet).second->m_Name) == 0);
336 CHECK(counterSet.second->m_Count == (*foundCounterSet).second->m_Count);
340 for (
auto& category : serviceCategories)
342 for (
auto& receivedCategory : receivedCounterDirectory.
GetCategories())
344 if (receivedCategory->m_Name.compare(category->m_Name) == 0)
348 std::sort(category->m_Counters.begin(), category->m_Counters.end());
349 std::sort(receivedCategory->m_Counters.begin(), receivedCategory->m_Counters.end());
351 std::function<bool(const uint16_t&, const uint16_t&)> comparator =
352 [&directoryCaptureCommandHandler](
const uint16_t& first,
const uint16_t& second) {
354 if (translated == first)
361 CHECK(std::equal(category->m_Counters.begin(), category->m_Counters.end(),
362 receivedCategory->m_Counters.begin(), comparator));
370 for (
auto& receivedCounter : receivedCounters)
376 CHECK(serviceCounter->
m_DeviceUid == receivedCounter.second->m_DeviceUid);
377 CHECK(serviceCounter->
m_Name.compare(receivedCounter.second->m_Name) == 0);
378 CHECK(serviceCounter->
m_CounterSetUid == receivedCounter.second->m_CounterSetUid);
379 CHECK(serviceCounter->
m_Multiplier == receivedCounter.second->m_Multiplier);
380 CHECK(serviceCounter->
m_Interpolation == receivedCounter.second->m_Interpolation);
381 CHECK(serviceCounter->
m_Class == receivedCounter.second->m_Class);
382 CHECK(serviceCounter->
m_Units.compare(receivedCounter.second->m_Units) == 0);
383 CHECK(serviceCounter->
m_Description.compare(receivedCounter.second->m_Description) == 0);
386 mockService.WaitForReceivingThread();
393 TEST_CASE(
"GatorDMockTimeLineActivation")
402 std::string udsNamespace =
"gatord_namespace";
404 arm::pipe::ConnectionHandler connectionHandler(udsNamespace,
false);
411 auto basePipeServer = connectionHandler.GetNewBasePipeServer(
false);
415 if (!mockService.WaitForStreamMetaData())
417 FAIL(
"Failed to receive StreamMetaData");
424 mockService.SendConnectionAck();
426 mockService.LaunchReceivingThread();
434 IConnectableLayer* normalize = net->AddNormalizationLayer(descriptor,
"normalization");
447 WaitFor([&](){
return mockService.GetDirectoryCaptureCommandHandler().ParsedCounterDirectory();},
448 "MockGatord did not receive counter directory packet");
450 arm::pipe::TimelineDecoder& timelineDecoder = mockService.GetTimelineDecoder();
452 WaitFor([&](){
return timelineDecoder.ApplyToModel([&](arm::pipe::TimelineDecoder::Model& m){
453 return m.m_EventClasses.size() >= 2;});},
454 "MockGatord did not receive well known timeline labels");
456 WaitFor([&](){
return timelineDecoder.ApplyToModel([&](arm::pipe::TimelineDecoder::Model& m){
457 return m.m_Entities.size() >= 1;});},
458 "MockGatord did not receive mock backend test entity");
461 timelineDecoder.ApplyToModel([&](
const arm::pipe::TimelineDecoder::Model& m){
462 CHECK(m.m_Entities.size() == 1);
463 CHECK(m.m_EventClasses.size() == 2);
464 CHECK(m.m_Labels.size() == 15);
465 CHECK(m.m_Relationships.size() == 0);
466 CHECK(m.m_Events.size() == 0);
469 mockService.SendDeactivateTimelinePacket();
472 "Timeline packets were not deactivated");
476 runtime.LoadNetwork(netId, std::move(optNet));
479 mockService.SendActivateTimelinePacket();
482 "Timeline packets were not activated");
485 mockService.SendActivateTimelinePacket();
486 mockService.SendActivateTimelinePacket();
490 WaitFor([&](){
return timelineDecoder.ApplyToModel([&](arm::pipe::TimelineDecoder::Model& m){
491 return m.m_Labels.size() >= 24;});},
492 "MockGatord did not receive well known timeline labels");
495 timelineDecoder.ApplyToModel([&](
const arm::pipe::TimelineDecoder::Model& m){
496 CHECK(m.m_Entities.size() == 6);
497 CHECK(m.m_EventClasses.size() == 4);
498 CHECK(m.m_Labels.size() == 34);
499 CHECK(m.m_Relationships.size() == 15);
500 CHECK(m.m_Events.size() == 0);
503 mockService.WaitForReceivingThread();
profiling::ProfilingService & GetProfilingService(armnn::RuntimeImpl *runtime)
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
CPU Execution: Reference C++ kernels.
ProfilingState GetCurrentState() const
uint16_t TranslateUIDCopyToOriginal(uint16_t copyUid)
Given a Uid that came from a copy of the counter directory translate it to the original.
void WriteUint16(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint16_t value)
void WriteUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset, uint32_t value)
std::unordered_map< uint16_t, CounterPtr > Counters
virtual uint16_t GetCounterCount() const =0
BackendRegistry & BackendRegistryInstance()
uint8_t ReadUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset)
Copyright (c) 2021 ARM Limited and Contributors.
virtual const CounterSets & GetCounterSets() const =0
virtual uint16_t GetCategoryCount() const =0
bool m_EnableProfiling
Indicates whether external profiling is enabled or not.
bool ParsedCounterDirectory()
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
std::unordered_map< uint16_t, CounterSetPtr > CounterSets
virtual const Categories & GetCategories() const =0
virtual const Devices & GetDevices() const =0
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
void ResetExternalProfilingOptions(const ExternalProfilingOptions &options, bool resetProfilingService=false)
virtual uint16_t GetDeviceCount() const =0
#define ARMNN_ASSERT(COND)
static MockBackendProfilingService & Instance()
virtual const Counter * GetCounter(uint16_t uid) const =0
void WriteUint64(const std::unique_ptr< IPacketBuffer > &packetBuffer, unsigned int offset, uint64_t value)
uint32_t ReadUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset)
std::unordered_set< CategoryPtr > Categories
virtual uint16_t GetCounterSetCount() const =0
A class that implements a Mock Gatord server.
MockBackendProfilingContext * GetContext()
std::string m_Description
bool TimelineReportingEnabled()
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
bool m_TimelineEnabled
Indicates whether external timeline profiling is enabled or not.
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
virtual const Counters & GetCounters() const =0
std::unordered_map< uint16_t, DevicePtr > Devices
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
virtual int Connect(IInputSlot &destination)=0
const ICounterDirectory & GetCounterDirectory() const
A NormalizationDescriptor for the NormalizationLayer.
ExternalProfilingOptions m_ProfilingOptions
const ICounterDirectory & GetCounterDirectory() const
static INetworkPtr Create(NetworkOptions networkOptions={})