25 #include <condition_variable> 61 std::lock_guard<std::mutex> lock(m_Mutex);
68 std::lock_guard<std::mutex> lock(m_Mutex);
73 bool WritePacket(
const unsigned char* buffer, uint32_t length)
override 75 if (buffer ==
nullptr || length == 0)
82 uint32_t packetFamily = (header >> 26);
83 uint32_t packetId = ((header >> 16) & 1023);
102 std::lock_guard<std::mutex> lock(m_Mutex);
104 m_WrittenData.push_back({ packetType, length });
110 std::lock_guard<std::mutex> lock(m_Mutex);
112 if(packetInfo.second != 0)
114 return static_cast<long>(std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo));
118 return static_cast<long>(std::count_if(m_WrittenData.begin(), m_WrittenData.end(),
119 [&packetInfo](
const std::pair<PacketType, uint32_t> pair) {
return packetInfo.first == pair.first; }));
125 std::lock_guard<std::mutex> lock(m_Mutex);
127 m_Packet = std::move(packet);
136 std::this_thread::sleep_for(std::chrono::milliseconds(5));
137 std::lock_guard<std::mutex> lock(m_Mutex);
138 return std::move(m_Packet);
143 std::lock_guard<std::mutex> lock(m_Mutex);
145 return static_cast<unsigned long>(m_WrittenData.size());
150 std::lock_guard<std::mutex> lock(m_Mutex);
152 m_WrittenData.clear();
157 std::vector<std::pair<PacketType, uint32_t>> m_WrittenData;
158 arm::pipe::Packet m_Packet;
159 mutable std::mutex m_Mutex;
168 return std::make_unique<MockProfilingConnection>();
178 , m_Data(
std::make_unique<unsigned char[]>(m_MaxSize))
185 unsigned int GetSize()
const override {
return m_Size; }
189 void Commit(
unsigned int size)
override { m_Size = size; }
195 void Destroy()
override {m_Data.reset(
nullptr); m_Size = 0; m_MaxSize =0;}
198 unsigned int m_MaxSize;
200 std::unique_ptr<unsigned char[]> m_Data;
207 : m_BufferSize(size),
214 if (requestedSize > m_BufferSize)
216 reservedSize = m_BufferSize;
220 reservedSize = requestedSize;
223 return std::move(m_Buffer);
228 packetBuffer->Commit(size);
229 m_Buffer = std::move(packetBuffer);
239 return std::move(m_Buffer);
244 packetBuffer->Release();
245 m_Buffer = std::move(packetBuffer);
250 packetBuffer->MarkRead();
251 m_Buffer = std::move(packetBuffer);
256 if (consumer !=
nullptr)
258 m_Consumer = consumer;
265 if (m_Consumer !=
nullptr)
267 m_Consumer->SetReadyToRead();
272 unsigned int m_BufferSize;
281 : m_MaxBufferSize(maxBufferSize)
291 std::lock_guard<std::mutex> lock(m_Mutex);
294 if (requestedSize > m_MaxBufferSize)
297 std::to_string(m_MaxBufferSize) +
"] bytes");
299 reservedSize = requestedSize;
300 return std::make_unique<MockPacketBuffer>(requestedSize);
305 std::lock_guard<std::mutex> lock(m_Mutex);
307 packetBuffer->Commit(size);
308 m_BufferList.push_back(std::move(packetBuffer));
309 m_CommittedSize += size;
319 std::lock_guard<std::mutex> lock(m_Mutex);
321 packetBuffer->Release();
326 std::lock_guard<std::mutex> lock(m_Mutex);
328 if (m_BufferList.empty())
333 m_BufferList.pop_back();
334 m_ReadableSize += buffer->GetSize();
340 std::lock_guard<std::mutex> lock(m_Mutex);
342 m_ReadSize += packetBuffer->GetSize();
343 packetBuffer->MarkRead();
348 if (consumer !=
nullptr)
350 m_Consumer = consumer;
357 if (m_Consumer !=
nullptr)
359 m_Consumer->SetReadyToRead();
369 unsigned int m_MaxBufferSize;
372 std::vector<IPacketBufferPtr> m_BufferList;
378 unsigned int m_CommittedSize;
381 unsigned int m_ReadableSize;
384 unsigned int m_ReadSize;
397 std::string message(
"SendStreamMetaDataPacket");
398 unsigned int reserved = 0;
400 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
401 m_BufferManager.Commit(buffer, reserved,
false);
408 std::string message(
"SendCounterDirectoryPacket");
409 unsigned int reserved = 0;
411 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
412 m_BufferManager.Commit(buffer, reserved);
416 const std::vector<CounterValue>& values)
override 420 std::string message(
"SendPeriodicCounterCapturePacket");
421 unsigned int reserved = 0;
423 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
424 m_BufferManager.Commit(buffer, reserved);
428 const std::vector<uint16_t>& selectedCounterIds)
override 432 std::string message(
"SendPeriodicCounterSelectionPacket");
433 unsigned int reserved = 0;
435 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
436 m_BufferManager.Commit(buffer, reserved);
453 CategoryPtr category = std::make_unique<Category>(categoryName);
457 const Category* categoryPtr = category.get();
461 m_Categories.insert(std::move(category));
473 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
477 const Device* devicePtr = device.get();
481 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
487 const std::string& counterSetName,
494 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
498 const CounterSet* counterSetPtr = counterSet.get();
502 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
504 return counterSetPtr;
509 const std::string& parentCategoryName,
510 uint16_t counterClass,
511 uint16_t interpolation,
513 const std::string& name,
514 const std::string& description,
523 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
526 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
529 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
534 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
537 const std::string unitsValue = units.has_value() ? units.value() :
"";
540 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
554 const Counter* counterPtr = counter.get();
558 for (uint16_t counterUid : counterUids)
561 Category* parentCategory =
const_cast<Category*
>(GetCategory(parentCategoryName));
563 parentCategory->
m_Counters.push_back(counterUid);
566 m_Counters.insert(std::make_pair(counterUid, counter));
587 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](
const CategoryPtr& category)
591 return category->m_Name == name;
594 if (it == m_Categories.end())
631 bool isProfilingEnabled,
633 m_SendCounterPacket(mockBufferManager),
634 m_IsProfilingEnabled(isProfilingEnabled),
635 m_CaptureData(captureData)
641 return m_GuidGenerator.NextGuid();
647 return m_GuidGenerator.GenerateStaticId(str);
657 return m_CounterMapping;
662 return m_SendCounterPacket;
667 return m_IsProfilingEnabled;
677 uint16_t backendCounterId,
680 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
685 m_CounterMapping.Reset();
692 bool m_IsProfilingEnabled;
const CounterSet * GetCounterSet(uint16_t uid) const override
IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize) override
MockStreamCounterBuffer(unsigned int maxBufferSize=4096)
ISendCounterPacket & GetSendCounterPacket() override
const Categories & GetCategories() const override
std::unique_ptr< ISendTimelinePacket > GetSendTimelinePacket() const override
const Devices & GetDevices() const override
void FlushReadList() override
void SetConsumer(IConsumer *consumer) override
IPacketBufferPtr GetReadableBuffer() override
std::unordered_map< uint16_t, CounterPtr > Counters
void SendStreamMetaDataPacket() override
Create and write a StreamMetaDataPacket in the buffer.
void Commit(IPacketBufferPtr &packetBuffer, unsigned int size, bool notifyConsumer=true) override
bool IsProfilingEnabled() const override
void SendCounterDirectoryPacket(const ICounterDirectory &counterDirectory) override
Create and write a CounterDirectoryPacket from the parameters to the buffer.
Strongly typed guids to distinguish between those generated at runtime, and those that are statically...
std::unique_ptr< Device > DevicePtr
unsigned int GetCommittedSize() const
std::unique_ptr< CounterSet > CounterSetPtr
std::unique_ptr< IProfilingConnection > IProfilingConnectionPtr
MockProfilingConnection()
unsigned long GetWrittenDataSize()
Copyright (c) 2020 ARM Limited.
void IgnoreUnused(Ts &&...)
ProfilingDynamicGuid NextGuid() override
Return the next random Guid in the sequence.
void Commit(IPacketBufferPtr &packetBuffer, unsigned int size, bool notifyConsumer=true) override
void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, const std::vector< uint16_t > &selectedCounterIds) override
Create and write a PeriodicCounterSelectionPacket from the parameters to the buffer.
void Release(IPacketBufferPtr &packetBuffer) override
std::unordered_map< uint16_t, CounterSetPtr > CounterSets
uint16_t GetNextUid(bool peekOnly)
const Counter * RegisterCounter(const BackendId &backendId, const uint16_t uid, const std::string &parentCategoryName, uint16_t counterClass, uint16_t interpolation, double multiplier, const std::string &name, const std::string &description, const armnn::Optional< std::string > &units=armnn::EmptyOptional(), const armnn::Optional< uint16_t > &numberOfCores=armnn::EmptyOptional(), const armnn::Optional< uint16_t > &deviceUid=armnn::EmptyOptional(), const armnn::Optional< uint16_t > &counterSetUid=armnn::EmptyOptional())
void MarkRead(IPacketBufferPtr &packetBuffer) override
uint16_t GetCategoryCount() const override
unsigned char * GetWritableData() override
arm::pipe::Packet ReadPacket(uint32_t timeout) override
void MarkRead(IPacketBufferPtr &packetBuffer) override
const unsigned char * GetReadableData() const override
std::shared_ptr< Counter > CounterPtr
const CounterSets & GetCounterSets() const override
const ICounterMappings & GetCounterMappings() const override
const Device * RegisterDevice(const std::string &deviceName, uint16_t cores=0)
unsigned int GetReadableSize() const
std::vector< uint16_t > GetNextCounterUids(uint16_t firstUid, uint16_t cores)
IPacketBufferPtr GetReadableBuffer() override
const Category * GetCategory(const std::string &name) const override
unsigned int GetSize() const override
ProfilingStaticGuid GenerateStaticId(const std::string &str) override
Create a ProfilingStaticGuid based on a hash of the string.
#define ARMNN_ASSERT(COND)
const Category * RegisterCategory(const std::string &categoryName)
const Counter * GetCounter(uint16_t uid) const override
void SendPeriodicCounterCapturePacket(uint64_t timestamp, const std::vector< CounterValue > &values) override
uint16_t GetCounterCount() const override
bool WritePacket(arm::pipe::Packet &&packet)
~MockStreamCounterBuffer()
uint32_t ReadUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset)
uint16_t GetCounterSetCount() const override
std::unordered_set< CategoryPtr > Categories
std::vector< uint16_t > m_Counters
void FlushReadList() override
MockBufferManager(unsigned int size)
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
const Counters & GetCounters() const override
CaptureData GetCaptureData() override
void Destroy() override
release the memory held and reset internal point to null.
MockPacketBuffer(unsigned int maxSize)
std::unique_ptr< Category > CategoryPtr
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
bool WritePacket(const unsigned char *buffer, uint32_t length) override
const CounterSet * RegisterCounterSet(const std::string &counterSetName, uint16_t count=0)
long CheckForPacket(const std::pair< PacketType, uint32_t > packetInfo)
const Device * GetDevice(uint16_t uid) const override
void NotifyProfilingServiceActive() override
std::unordered_map< uint16_t, DevicePtr > Devices
bool IsOpen() const override
void SetConsumer(IConsumer *consumer) override
IProfilingConnectionPtr GetProfilingConnection(const ExternalProfilingOptions &options) const override
MockProfilingService(MockBufferManager &mockBufferManager, bool isProfilingEnabled, const CaptureData &captureData)
void WaitForProfilingServiceActivation(unsigned int timeout) override
void Release(IPacketBufferPtr &packetBuffer) override
unsigned int GetReadSize() const
std::unique_ptr< IPacketBuffer > IPacketBufferPtr
DataLayout::NCHW DataLayout::NCHW DataLayout::NHWC DataLayout::NHWC true
IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize) override
MockSendCounterPacket(IBufferManager &sendBuffer)
uint16_t GetDeviceCount() const override
void RegisterMapping(uint16_t globalCounterId, uint16_t backendCounterId, const armnn::BackendId &backendId)
void Commit(unsigned int size) override