20 #include <boost/assert.hpp> 21 #include <boost/core/ignore_unused.hpp> 22 #include <boost/numeric/conversion/cast.hpp> 25 #include <condition_variable> 59 std::lock_guard<std::mutex> lock(m_Mutex);
66 std::lock_guard<std::mutex> lock(m_Mutex);
71 bool WritePacket(
const unsigned char* buffer, uint32_t length)
override 73 if (buffer ==
nullptr || length == 0)
80 uint32_t packetFamily = (header >> 26);
81 uint32_t packetId = ((header >> 16) & 1023);
100 std::lock_guard<std::mutex> lock(m_Mutex);
102 m_WrittenData.push_back({ packetType, length });
108 std::lock_guard<std::mutex> lock(m_Mutex);
110 if(packetInfo.second != 0)
112 return std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo);
116 return std::count_if(m_WrittenData.begin(), m_WrittenData.end(),
117 [&packetInfo](
const std::pair<PacketType, uint32_t> pair) {
return packetInfo.first == pair.first; });
123 std::lock_guard<std::mutex> lock(m_Mutex);
125 m_Packet = std::move(packet);
131 boost::ignore_unused(timeout);
134 std::this_thread::sleep_for(std::chrono::milliseconds(5));
135 std::lock_guard<std::mutex> lock(m_Mutex);
136 return std::move(m_Packet);
141 std::lock_guard<std::mutex> lock(m_Mutex);
143 return m_WrittenData.size();
148 std::lock_guard<std::mutex> lock(m_Mutex);
150 m_WrittenData.clear();
155 std::vector<std::pair<PacketType, uint32_t>> m_WrittenData;
157 mutable std::mutex m_Mutex;
165 boost::ignore_unused(options);
166 return std::make_unique<MockProfilingConnection>();
176 , m_Data(
std::make_unique<unsigned char[]>(m_MaxSize))
183 unsigned int GetSize()
const override {
return m_Size; }
187 void Commit(
unsigned int size)
override { m_Size = size; }
194 unsigned int m_MaxSize;
196 std::unique_ptr<unsigned char[]> m_Data;
203 : m_BufferSize(size),
210 if (requestedSize > m_BufferSize)
212 reservedSize = m_BufferSize;
216 reservedSize = requestedSize;
219 return std::move(m_Buffer);
224 packetBuffer->Commit(size);
225 m_Buffer = std::move(packetBuffer);
235 return std::move(m_Buffer);
240 packetBuffer->Release();
241 m_Buffer = std::move(packetBuffer);
246 packetBuffer->MarkRead();
247 m_Buffer = std::move(packetBuffer);
252 if (consumer !=
nullptr)
254 m_Consumer = consumer;
261 if (m_Consumer !=
nullptr)
263 m_Consumer->SetReadyToRead();
268 unsigned int m_BufferSize;
277 : m_MaxBufferSize(maxBufferSize)
287 std::lock_guard<std::mutex> lock(m_Mutex);
290 if (requestedSize > m_MaxBufferSize)
293 std::to_string(m_MaxBufferSize) +
"] bytes");
295 reservedSize = requestedSize;
296 return std::make_unique<MockPacketBuffer>(requestedSize);
301 std::lock_guard<std::mutex> lock(m_Mutex);
303 packetBuffer->Commit(size);
304 m_BufferList.push_back(std::move(packetBuffer));
305 m_CommittedSize += size;
315 std::lock_guard<std::mutex> lock(m_Mutex);
317 packetBuffer->Release();
322 std::lock_guard<std::mutex> lock(m_Mutex);
324 if (m_BufferList.empty())
329 m_BufferList.pop_back();
330 m_ReadableSize += buffer->GetSize();
336 std::lock_guard<std::mutex> lock(m_Mutex);
338 m_ReadSize += packetBuffer->GetSize();
339 packetBuffer->MarkRead();
344 if (consumer !=
nullptr)
346 m_Consumer = consumer;
353 if (m_Consumer !=
nullptr)
355 m_Consumer->SetReadyToRead();
365 unsigned int m_MaxBufferSize;
368 std::vector<IPacketBufferPtr> m_BufferList;
374 unsigned int m_CommittedSize;
377 unsigned int m_ReadableSize;
380 unsigned int m_ReadSize;
393 std::string message(
"SendStreamMetaDataPacket");
394 unsigned int reserved = 0;
396 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
397 m_BufferManager.Commit(buffer, reserved,
false);
402 boost::ignore_unused(counterDirectory);
404 std::string message(
"SendCounterDirectoryPacket");
405 unsigned int reserved = 0;
407 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
408 m_BufferManager.Commit(buffer, reserved);
412 const std::vector<CounterValue>& values)
override 414 boost::ignore_unused(timestamp, values);
416 std::string message(
"SendPeriodicCounterCapturePacket");
417 unsigned int reserved = 0;
419 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
420 m_BufferManager.Commit(buffer, reserved);
424 const std::vector<uint16_t>& selectedCounterIds)
override 426 boost::ignore_unused(capturePeriod, selectedCounterIds);
428 std::string message(
"SendPeriodicCounterSelectionPacket");
429 unsigned int reserved = 0;
431 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
432 m_BufferManager.Commit(buffer, reserved);
451 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
454 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
457 CategoryPtr category = std::make_unique<Category>(categoryName, deviceUidValue, counterSetUidValue);
458 BOOST_ASSERT(category);
461 const Category* categoryPtr = category.get();
462 BOOST_ASSERT(categoryPtr);
465 m_Categories.insert(std::move(category));
478 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
479 BOOST_ASSERT(device);
482 const Device* devicePtr = device.get();
483 BOOST_ASSERT(devicePtr);
486 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
489 if (parentCategoryName.has_value())
492 Category* parentCategory =
const_cast<Category*
>(GetCategory(parentCategoryName.value()));
493 BOOST_ASSERT(parentCategory);
501 const std::string& counterSetName,
509 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
510 BOOST_ASSERT(counterSet);
513 const CounterSet* counterSetPtr = counterSet.get();
514 BOOST_ASSERT(counterSetPtr);
517 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
520 if (parentCategoryName.has_value())
523 Category* parentCategory =
const_cast<Category*
>(GetCategory(parentCategoryName.value()));
524 BOOST_ASSERT(parentCategory);
528 return counterSetPtr;
533 const std::string& parentCategoryName,
534 uint16_t counterClass,
535 uint16_t interpolation,
537 const std::string& name,
538 const std::string& description,
544 boost::ignore_unused(backendId);
547 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
550 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
553 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
557 BOOST_ASSERT(!counterUids.empty());
558 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
561 const std::string unitsValue = units.has_value() ? units.value() :
"";
564 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
575 BOOST_ASSERT(counter);
578 const Counter* counterPtr = counter.get();
579 BOOST_ASSERT(counterPtr);
582 for (uint16_t counterUid : counterUids)
585 Category* parentCategory =
const_cast<Category*
>(GetCategory(parentCategoryName));
586 BOOST_ASSERT(parentCategory);
587 parentCategory->
m_Counters.push_back(counterUid);
590 m_Counters.insert(std::make_pair(counterUid, counter));
597 uint16_t
GetCategoryCount()
const override {
return boost::numeric_cast<uint16_t>(m_Categories.size()); }
598 uint16_t
GetDeviceCount()
const override {
return boost::numeric_cast<uint16_t>(m_Devices.size()); }
599 uint16_t
GetCounterSetCount()
const override {
return boost::numeric_cast<uint16_t>(m_CounterSets.size()); }
600 uint16_t
GetCounterCount()
const override {
return boost::numeric_cast<uint16_t>(m_Counters.size()); }
611 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](
const CategoryPtr& category)
613 BOOST_ASSERT(category);
615 return category->m_Name == name;
618 if (it == m_Categories.end())
628 boost::ignore_unused(uid);
634 boost::ignore_unused(uid);
640 boost::ignore_unused(uid);
655 bool isProfilingEnabled,
657 m_SendCounterPacket(mockBufferManager),
658 m_IsProfilingEnabled(isProfilingEnabled),
659 m_CaptureData(captureData) {}
664 return m_GuidGenerator.NextGuid();
670 return m_GuidGenerator.GenerateStaticId(str);
680 return m_CounterMapping;
685 return m_SendCounterPacket;
690 return m_IsProfilingEnabled;
700 uint16_t backendCounterId,
703 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
708 m_CounterMapping.Reset();
715 bool m_IsProfilingEnabled;
void Release(IPacketBufferPtr &packetBuffer) override
void Commit(IPacketBufferPtr &packetBuffer, unsigned int size, bool notifyConsumer=true) override
std::unordered_set< CategoryPtr > Categories
void Release(IPacketBufferPtr &packetBuffer) override
IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize) override
MockSendCounterPacket(IBufferManager &sendBuffer)
const ICounterMappings & GetCounterMappings() const override
std::unordered_map< uint16_t, DevicePtr > Devices
std::unique_ptr< CounterSet > CounterSetPtr
void SetConsumer(IConsumer *consumer) override
uint16_t GetNextUid(bool peekOnly)
unsigned int GetReadableSize() const
~MockStreamCounterBuffer()
std::unordered_map< uint16_t, CounterSetPtr > CounterSets
MockProfilingService(MockBufferManager &mockBufferManager, bool isProfilingEnabled, const CaptureData &captureData)
void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, const std::vector< uint16_t > &selectedCounterIds) override
Create and write a PeriodicCounterSelectionPacket from the parameters to the buffer.
unsigned int GetReadSize() const
const Counter * GetCounter(uint16_t uid) const override
MockProfilingConnection()
std::unique_ptr< IPacketBuffer > IPacketBufferPtr
IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize) override
const Counters & GetCounters() const override
DataLayout::NCHW DataLayout::NCHW DataLayout::NHWC DataLayout::NHWC true
bool IsProfilingEnabled() const override
const CounterSets & GetCounterSets() const override
void FlushReadList() override
long CheckForPacket(const std::pair< PacketType, uint32_t > packetInfo)
uint16_t GetCounterCount() const override
CaptureData GetCaptureData() override
std::vector< uint16_t > GetNextCounterUids(uint16_t firstUid, uint16_t cores)
bool WritePacket(const unsigned char *buffer, uint32_t length) override
MockPacketBuffer(unsigned int maxSize)
ProfilingDynamicGuid NextGuid() override
Return the next random Guid in the sequence.
unsigned int GetSize() const override
IProfilingConnectionPtr GetProfilingConnection(const ExternalProfilingOptions &options) const override
void MarkRead(IPacketBufferPtr &packetBuffer) override
const CounterSet * GetCounterSet(uint16_t uid) const override
std::shared_ptr< Counter > CounterPtr
const Device * RegisterDevice(const std::string &deviceName, uint16_t cores=0, const armnn::Optional< std::string > &parentCategoryName=armnn::EmptyOptional())
std::vector< uint16_t > m_Counters
std::unique_ptr< Category > CategoryPtr
const Category * GetCategory(const std::string &name) const override
unsigned char * GetWritableData() override
void Commit(unsigned int size) override
unsigned int GetCommittedSize() const
MockStreamCounterBuffer(unsigned int maxBufferSize=4096)
void SetConsumer(IConsumer *consumer) override
std::unique_ptr< Device > DevicePtr
std::unique_ptr< IProfilingConnection > IProfilingConnectionPtr
IPacketBufferPtr GetReadableBuffer() override
void RegisterMapping(uint16_t globalCounterId, uint16_t backendCounterId, const armnn::BackendId &backendId) override
bool WritePacket(Packet &&packet)
uint32_t ReadUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset)
ISendCounterPacket & GetSendCounterPacket() override
uint16_t GetDeviceCount() const override
const Devices & GetDevices() const override
void MarkRead(IPacketBufferPtr &packetBuffer) override
uint16_t GetCounterSetCount() const override
Packet ReadPacket(uint32_t timeout) override
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 SendStreamMetaDataPacket() override
Create and write a StreamMetaDataPacket in the buffer.
bool IsOpen() const override
void SendPeriodicCounterCapturePacket(uint64_t timestamp, const std::vector< CounterValue > &values) override
std::unordered_map< uint16_t, CounterPtr > Counters
IPacketBufferPtr GetReadableBuffer() override
const Categories & GetCategories() const override
Strongly typed guids to distinguish between those generated at runtime, and those that are statically...
std::unique_ptr< ISendTimelinePacket > GetSendTimelinePacket() const override
const unsigned char * GetReadableData() const override
const CounterSet * RegisterCounterSet(const std::string &counterSetName, uint16_t count=0, const armnn::Optional< std::string > &parentCategoryName=armnn::EmptyOptional())
void SendCounterDirectoryPacket(const ICounterDirectory &counterDirectory) override
Create and write a CounterDirectoryPacket from the parameters to the buffer.
void FlushReadList() override
uint16_t GetCategoryCount() const override
unsigned long GetWrittenDataSize()
ProfilingStaticGuid GenerateStaticId(const std::string &str) override
Create a ProfilingStaticGuid based on a hash of the string.
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
void Commit(IPacketBufferPtr &packetBuffer, unsigned int size, bool notifyConsumer=true) override
const Device * GetDevice(uint16_t uid) const override
MockBufferManager(unsigned int size)
const Category * RegisterCategory(const std::string &categoryName, const armnn::Optional< uint16_t > &deviceUid=armnn::EmptyOptional(), const armnn::Optional< uint16_t > &counterSetUid=armnn::EmptyOptional())