22 #include <boost/numeric/conversion/cast.hpp> 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 std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo);
118 return 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 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;
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; }
196 unsigned int m_MaxSize;
198 std::unique_ptr<unsigned char[]> m_Data;
205 : m_BufferSize(size),
212 if (requestedSize > m_BufferSize)
214 reservedSize = m_BufferSize;
218 reservedSize = requestedSize;
221 return std::move(m_Buffer);
226 packetBuffer->Commit(size);
227 m_Buffer = std::move(packetBuffer);
237 return std::move(m_Buffer);
242 packetBuffer->Release();
243 m_Buffer = std::move(packetBuffer);
248 packetBuffer->MarkRead();
249 m_Buffer = std::move(packetBuffer);
254 if (consumer !=
nullptr)
256 m_Consumer = consumer;
263 if (m_Consumer !=
nullptr)
265 m_Consumer->SetReadyToRead();
270 unsigned int m_BufferSize;
279 : m_MaxBufferSize(maxBufferSize)
289 std::lock_guard<std::mutex> lock(m_Mutex);
292 if (requestedSize > m_MaxBufferSize)
295 std::to_string(m_MaxBufferSize) +
"] bytes");
297 reservedSize = requestedSize;
298 return std::make_unique<MockPacketBuffer>(requestedSize);
303 std::lock_guard<std::mutex> lock(m_Mutex);
305 packetBuffer->Commit(size);
306 m_BufferList.push_back(std::move(packetBuffer));
307 m_CommittedSize += size;
317 std::lock_guard<std::mutex> lock(m_Mutex);
319 packetBuffer->Release();
324 std::lock_guard<std::mutex> lock(m_Mutex);
326 if (m_BufferList.empty())
331 m_BufferList.pop_back();
332 m_ReadableSize += buffer->GetSize();
338 std::lock_guard<std::mutex> lock(m_Mutex);
340 m_ReadSize += packetBuffer->GetSize();
341 packetBuffer->MarkRead();
346 if (consumer !=
nullptr)
348 m_Consumer = consumer;
355 if (m_Consumer !=
nullptr)
357 m_Consumer->SetReadyToRead();
367 unsigned int m_MaxBufferSize;
370 std::vector<IPacketBufferPtr> m_BufferList;
376 unsigned int m_CommittedSize;
379 unsigned int m_ReadableSize;
382 unsigned int m_ReadSize;
395 std::string message(
"SendStreamMetaDataPacket");
396 unsigned int reserved = 0;
398 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
399 m_BufferManager.Commit(buffer, reserved,
false);
406 std::string message(
"SendCounterDirectoryPacket");
407 unsigned int reserved = 0;
409 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
410 m_BufferManager.Commit(buffer, reserved);
414 const std::vector<CounterValue>& values)
override 418 std::string message(
"SendPeriodicCounterCapturePacket");
419 unsigned int reserved = 0;
421 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
422 m_BufferManager.Commit(buffer, reserved);
426 const std::vector<uint16_t>& selectedCounterIds)
override 430 std::string message(
"SendPeriodicCounterSelectionPacket");
431 unsigned int reserved = 0;
433 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
434 m_BufferManager.Commit(buffer, reserved);
451 CategoryPtr category = std::make_unique<Category>(categoryName);
455 const Category* categoryPtr = category.get();
459 m_Categories.insert(std::move(category));
471 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
475 const Device* devicePtr = device.get();
479 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
485 const std::string& counterSetName,
492 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
496 const CounterSet* counterSetPtr = counterSet.get();
500 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
502 return counterSetPtr;
507 const std::string& parentCategoryName,
508 uint16_t counterClass,
509 uint16_t interpolation,
511 const std::string& name,
512 const std::string& description,
521 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
524 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
527 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
532 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
535 const std::string unitsValue = units.has_value() ? units.value() :
"";
538 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
552 const Counter* counterPtr = counter.get();
556 for (uint16_t counterUid : counterUids)
559 Category* parentCategory =
const_cast<Category*
>(GetCategory(parentCategoryName));
561 parentCategory->
m_Counters.push_back(counterUid);
564 m_Counters.insert(std::make_pair(counterUid, counter));
585 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](
const CategoryPtr& category)
589 return category->m_Name == name;
592 if (it == m_Categories.end())
629 bool isProfilingEnabled,
631 m_SendCounterPacket(mockBufferManager),
632 m_IsProfilingEnabled(isProfilingEnabled),
633 m_CaptureData(captureData)
639 return m_GuidGenerator.NextGuid();
645 return m_GuidGenerator.GenerateStaticId(str);
655 return m_CounterMapping;
660 return m_SendCounterPacket;
665 return m_IsProfilingEnabled;
675 uint16_t backendCounterId,
678 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
683 m_CounterMapping.Reset();
690 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
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)
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
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