21 #include <boost/assert.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);
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;
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);
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 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 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);
449 CategoryPtr category = std::make_unique<Category>(categoryName);
450 BOOST_ASSERT(category);
453 const Category* categoryPtr = category.get();
454 BOOST_ASSERT(categoryPtr);
457 m_Categories.insert(std::move(category));
469 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
470 BOOST_ASSERT(device);
473 const Device* devicePtr = device.get();
474 BOOST_ASSERT(devicePtr);
477 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
483 const std::string& counterSetName,
490 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
491 BOOST_ASSERT(counterSet);
494 const CounterSet* counterSetPtr = counterSet.get();
495 BOOST_ASSERT(counterSetPtr);
498 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
500 return counterSetPtr;
505 const std::string& parentCategoryName,
506 uint16_t counterClass,
507 uint16_t interpolation,
509 const std::string& name,
510 const std::string& description,
519 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
522 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
525 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
529 BOOST_ASSERT(!counterUids.empty());
530 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
533 const std::string unitsValue = units.has_value() ? units.value() :
"";
536 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
547 BOOST_ASSERT(counter);
550 const Counter* counterPtr = counter.get();
551 BOOST_ASSERT(counterPtr);
554 for (uint16_t counterUid : counterUids)
557 Category* parentCategory =
const_cast<Category*
>(GetCategory(parentCategoryName));
558 BOOST_ASSERT(parentCategory);
559 parentCategory->
m_Counters.push_back(counterUid);
562 m_Counters.insert(std::make_pair(counterUid, counter));
583 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](
const CategoryPtr& category)
585 BOOST_ASSERT(category);
587 return category->m_Name == name;
590 if (it == m_Categories.end())
627 bool isProfilingEnabled,
629 m_SendCounterPacket(mockBufferManager),
630 m_IsProfilingEnabled(isProfilingEnabled),
631 m_CaptureData(captureData) {}
636 return m_GuidGenerator.NextGuid();
642 return m_GuidGenerator.GenerateStaticId(str);
652 return m_CounterMapping;
657 return m_SendCounterPacket;
662 return m_IsProfilingEnabled;
672 uint16_t backendCounterId,
675 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
680 m_CounterMapping.Reset();
687 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.
DataLayout::NCHW DataLayout::NCHW DataLayout::NHWC DataLayout::NHWC true
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
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.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
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
~MockStreamCounterBuffer()
uint32_t ReadUint32(const IPacketBufferPtr &packetBuffer, unsigned int offset)
uint16_t GetCounterSetCount() const override
std::unordered_set< CategoryPtr > Categories
bool WritePacket(Packet &&packet)
std::vector< uint16_t > m_Counters
void FlushReadList() override
Packet ReadPacket(uint32_t timeout) 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
MockPacketBuffer(unsigned int maxSize)
std::unique_ptr< Category > CategoryPtr
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
std::unordered_map< uint16_t, DevicePtr > Devices
bool IsOpen() const override
void SetConsumer(IConsumer *consumer) override
armnn::Runtime::CreationOptions::ExternalProfilingOptions options
IProfilingConnectionPtr GetProfilingConnection(const ExternalProfilingOptions &options) const override
MockProfilingService(MockBufferManager &mockBufferManager, bool isProfilingEnabled, const CaptureData &captureData)
void Release(IPacketBufferPtr &packetBuffer) override
void RegisterMapping(uint16_t globalCounterId, uint16_t backendCounterId, const armnn::BackendId &backendId) override
unsigned int GetReadSize() const
std::unique_ptr< IPacketBuffer > IPacketBufferPtr
IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize) override
MockSendCounterPacket(IBufferManager &sendBuffer)
uint16_t GetDeviceCount() const override
void Commit(unsigned int size) override