13 #include <common/include/LabelsAndEventClasses.hpp> 14 #include <common/include/CommandHandlerRegistry.hpp> 19 #include <server/include/timelineDecoder/TimelineDirectoryCaptureCommandHandler.hpp> 20 #include <server/include/timelineDecoder/TimelineDecoder.hpp> 21 #include <server/include/basePipeServer/ConnectionHandler.hpp> 23 #include <doctest/doctest.h> 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);
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 =
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 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::NAME_GUID);
190 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::NAME_LABEL);
192 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::TYPE_GUID);
193 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::TYPE_LABEL);
195 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::INDEX_GUID);
196 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::INDEX_LABEL);
198 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::BACKENDID_GUID);
199 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::BACKENDID_LABEL);
201 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::CHILD_GUID);
202 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::CHILD_LABEL);
204 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::EXECUTION_OF_GUID);
205 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name ==
206 profiling::LabelsAndEventClasses::EXECUTION_OF_LABEL);
208 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::PROCESS_ID_GUID);
209 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name ==
210 profiling::LabelsAndEventClasses::PROCESS_ID_LABEL);
212 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::LAYER_GUID);
213 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::LAYER);
215 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::WORKLOAD_GUID);
216 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::WORKLOAD);
218 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::NETWORK_GUID);
219 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::NETWORK);
221 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::CONNECTION_GUID);
222 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::CONNECTION);
224 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid == profiling::LabelsAndEventClasses::INFERENCE_GUID);
225 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name == profiling::LabelsAndEventClasses::INFERENCE);
227 CHECK(timelineDecoder.GetModel().m_Labels[i].m_Guid ==
228 profiling::LabelsAndEventClasses::WORKLOAD_EXECUTION_GUID);
229 CHECK(timelineDecoder.GetModel().m_Labels[i++].m_Name ==
230 profiling::LabelsAndEventClasses::WORKLOAD_EXECUTION);
232 CHECK(timelineDecoder.GetModel().m_EventClasses[0].m_Guid ==
233 profiling::LabelsAndEventClasses::ARMNN_PROFILING_SOL_EVENT_CLASS);
234 CHECK(timelineDecoder.GetModel().m_EventClasses[1].m_Guid ==
235 profiling::LabelsAndEventClasses::ARMNN_PROFILING_EOL_EVENT_CLASS);
238 TEST_CASE(
"GatorDMockEndToEnd")
244 std::string udsNamespace =
"gatord_namespace";
246 CHECK_NOTHROW(arm::pipe::ConnectionHandler connectionHandler(udsNamespace,
false));
248 arm::pipe::ConnectionHandler connectionHandler(udsNamespace,
false);
260 profilingService.
Update();
262 profilingService.
Update();
265 auto basePipeServer = connectionHandler.GetNewBasePipeServer(
false);
276 "Profiling service did not switch to WaitingForAck state");
278 profilingService.
Update();
282 FAIL(
"Failed to receive StreamMetaData");
290 "Profiling service did not switch to Active state");
294 "MockGatord did not receive counter directory packet");
297 WaitFor([&](){
return timelineDecoder.GetModel().m_EventClasses.size() >= 2;},
298 "MockGatord did not receive well known timeline labels and event classes");
302 CheckTimelinePackets(timelineDecoder);
317 for (
auto& device : serviceDevices)
320 auto foundDevice = receivedCounterDirectory.
GetDevices().find(device.second->m_Uid);
321 CHECK(foundDevice != receivedCounterDirectory.
GetDevices().end());
322 CHECK(device.second->m_Name.compare((*foundDevice).second->m_Name) == 0);
323 CHECK(device.second->m_Cores == (*foundDevice).second->m_Cores);
327 for (
auto& counterSet : serviceCounterSets)
330 auto foundCounterSet = receivedCounterDirectory.
GetCounterSets().find(counterSet.second->m_Uid);
331 CHECK(foundCounterSet != receivedCounterDirectory.
GetCounterSets().end());
332 CHECK(counterSet.second->m_Name.compare((*foundCounterSet).second->m_Name) == 0);
333 CHECK(counterSet.second->m_Count == (*foundCounterSet).second->m_Count);
337 for (
auto& category : serviceCategories)
339 for (
auto& receivedCategory : receivedCounterDirectory.
GetCategories())
341 if (receivedCategory->m_Name.compare(category->m_Name) == 0)
345 std::sort(category->m_Counters.begin(), category->m_Counters.end());
346 std::sort(receivedCategory->m_Counters.begin(), receivedCategory->m_Counters.end());
348 std::function<bool(const uint16_t&, const uint16_t&)> comparator =
349 [&directoryCaptureCommandHandler](
const uint16_t& first,
const uint16_t& second) {
351 if (translated == first)
358 CHECK(std::equal(category->m_Counters.begin(), category->m_Counters.end(),
359 receivedCategory->m_Counters.begin(), comparator));
367 for (
auto& receivedCounter : receivedCounters)
373 CHECK(serviceCounter->
m_DeviceUid == receivedCounter.second->m_DeviceUid);
374 CHECK(serviceCounter->
m_Name.compare(receivedCounter.second->m_Name) == 0);
375 CHECK(serviceCounter->
m_CounterSetUid == receivedCounter.second->m_CounterSetUid);
376 CHECK(serviceCounter->
m_Multiplier == receivedCounter.second->m_Multiplier);
377 CHECK(serviceCounter->
m_Interpolation == receivedCounter.second->m_Interpolation);
378 CHECK(serviceCounter->
m_Class == receivedCounter.second->m_Class);
379 CHECK(serviceCounter->
m_Units.compare(receivedCounter.second->m_Units) == 0);
380 CHECK(serviceCounter->
m_Description.compare(receivedCounter.second->m_Description) == 0);
390 TEST_CASE(
"GatorDMockTimeLineActivation")
399 std::string udsNamespace =
"gatord_namespace";
401 arm::pipe::ConnectionHandler connectionHandler(udsNamespace,
false);
408 auto basePipeServer = connectionHandler.GetNewBasePipeServer(
false);
414 FAIL(
"Failed to receive StreamMetaData");
431 IConnectableLayer* normalize = net->AddNormalizationLayer(descriptor,
"normalization");
445 "MockGatord did not receive counter directory packet");
449 WaitFor([&](){
return timelineDecoder.GetModel().m_EventClasses.size() >= 2;},
450 "MockGatord did not receive well known timeline labels");
452 WaitFor([&](){
return timelineDecoder.GetModel().m_Entities.size() >= 1;},
453 "MockGatord did not receive mock backend test entity");
456 CHECK(timelineDecoder.GetModel().m_Entities.size() == 1);
457 CHECK(timelineDecoder.GetModel().m_EventClasses.size() == 2);
458 CHECK(timelineDecoder.GetModel().m_Labels.size() == 15);
459 CHECK(timelineDecoder.GetModel().m_Relationships.size() == 0);
460 CHECK(timelineDecoder.GetModel().m_Events.size() == 0);
465 "Timeline packets were not deactivated");
475 "Timeline packets were not activated");
483 WaitFor([&](){
return timelineDecoder.GetModel().m_Labels.size() >= 24;},
484 "MockGatord did not receive well known timeline labels");
487 CHECK(timelineDecoder.GetModel().m_Entities.size() == 6);
488 CHECK(timelineDecoder.GetModel().m_EventClasses.size() == 4);
489 CHECK(timelineDecoder.GetModel().m_Labels.size() == 34);
490 CHECK(timelineDecoder.GetModel().m_Relationships.size() == 15);
491 CHECK(timelineDecoder.GetModel().m_Events.size() == 0);
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.
arm::pipe::TimelineDirectoryCaptureCommandHandler & GetTimelineDirectoryCaptureCommandHandler()
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()
Status LoadNetwork(NetworkId &networkIdOut, IOptimizedNetworkPtr network)
Loads a complete network into the Runtime.
uint8_t ReadUint8(const IPacketBufferPtr &packetBuffer, unsigned int offset)
Copyright (c) 2021 ARM Limited and Contributors.
void SendDeactivateTimelinePacket()
Send a deactivate timeline packet back to the client.
virtual const CounterSets & GetCounterSets() const =0
virtual uint16_t GetCategoryCount() const =0
bool m_EnableProfiling
Indicates whether external profiling is enabled or not.
void SendActivateTimelinePacket()
Send a activate timeline packet back to the client.
bool ParsedCounterDirectory()
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
std::unordered_map< uint16_t, CounterSetPtr > CounterSets
virtual const Categories & GetCategories() const =0
bool WaitForStreamMetaData()
Once the connection is open wait to receive the stream meta data packet from the client.
virtual const Devices & GetDevices() const =0
arm::pipe::TimelineDecoder & GetTimelineDecoder()
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.
const IDeviceSpec & GetDeviceSpec() const
TEST_SUITE("GatordMockTests")
uint64_t m_CurrentPeriodValue
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)
bool LaunchReceivingThread()
Start the thread that will receive all packets and print them nicely to stdout.
IPacketBufferPtr GetReadableBuffer() override
static MockBackendProfilingService & Instance()
CounterCaptureValues m_CounterCaptureValues
virtual const Counter * GetCounter(uint16_t uid) const =0
std::vector< uint16_t > m_Uids
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
profiling::DirectoryCaptureCommandHandler & GetDirectoryCaptureCommandHandler()
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
void WaitForReceivingThread()
This is a placeholder method to prevent main exiting.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
virtual int Connect(IInputSlot &destination)=0
std::unique_ptr< ISendTimelinePacket > GetSendTimelinePacket() const
const ICounterDirectory & GetCounterDirectory() const
A NormalizationDescriptor for the NormalizationLayer.
ExternalProfilingOptions m_ProfilingOptions
const ICounterDirectory & GetCounterDirectory() const
static INetworkPtr Create(NetworkOptions networkOptions={})
void SendConnectionAck()
Send a connection acknowledged packet back to the client.