23 #include <common/include/ProfilingGuidGenerator.hpp> 26 #include <condition_variable> 62 std::lock_guard<std::mutex> lock(m_Mutex);
69 std::lock_guard<std::mutex> lock(m_Mutex);
74 bool WritePacket(
const unsigned char* buffer, uint32_t length)
override 76 if (buffer ==
nullptr || length == 0)
83 uint32_t packetFamily = (header >> 26);
84 uint32_t packetId = ((header >> 16) & 1023);
103 std::lock_guard<std::mutex> lock(m_Mutex);
105 m_WrittenData.push_back({ packetType, length });
111 std::lock_guard<std::mutex> lock(m_Mutex);
113 if(packetInfo.second != 0)
115 return static_cast<long>(std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo));
119 return static_cast<long>(std::count_if(m_WrittenData.begin(), m_WrittenData.end(),
120 [&packetInfo](
const std::pair<PacketType, uint32_t> pair) {
return packetInfo.first == pair.first; }));
126 std::lock_guard<std::mutex> lock(m_Mutex);
128 m_Packet = std::move(packet);
137 std::this_thread::sleep_for(std::chrono::milliseconds(5));
138 std::lock_guard<std::mutex> lock(m_Mutex);
139 return std::move(m_Packet);
144 std::lock_guard<std::mutex> lock(m_Mutex);
146 return static_cast<unsigned long>(m_WrittenData.size());
151 std::lock_guard<std::mutex> lock(m_Mutex);
153 m_WrittenData.clear();
158 std::vector<std::pair<PacketType, uint32_t>> m_WrittenData;
159 arm::pipe::Packet m_Packet;
160 mutable std::mutex m_Mutex;
169 return std::make_unique<MockProfilingConnection>();
179 , m_Data(
std::make_unique<unsigned char[]>(m_MaxSize))
186 unsigned int GetSize()
const override {
return m_Size; }
190 void Commit(
unsigned int size)
override { m_Size = size; }
196 void Destroy()
override {m_Data.reset(
nullptr); m_Size = 0; m_MaxSize =0;}
199 unsigned int m_MaxSize;
201 std::unique_ptr<unsigned char[]> m_Data;
208 : m_BufferSize(size),
215 if (requestedSize > m_BufferSize)
217 reservedSize = m_BufferSize;
221 reservedSize = requestedSize;
224 return std::move(m_Buffer);
229 packetBuffer->Commit(size);
230 m_Buffer = std::move(packetBuffer);
240 return std::move(m_Buffer);
245 packetBuffer->Release();
246 m_Buffer = std::move(packetBuffer);
251 packetBuffer->MarkRead();
252 m_Buffer = std::move(packetBuffer);
257 if (consumer !=
nullptr)
259 m_Consumer = consumer;
266 if (m_Consumer !=
nullptr)
268 m_Consumer->SetReadyToRead();
273 unsigned int m_BufferSize;
282 : m_MaxBufferSize(maxBufferSize)
292 std::lock_guard<std::mutex> lock(m_Mutex);
295 if (requestedSize > m_MaxBufferSize)
298 std::to_string(m_MaxBufferSize) +
"] bytes");
300 reservedSize = requestedSize;
301 return std::make_unique<MockPacketBuffer>(requestedSize);
306 std::lock_guard<std::mutex> lock(m_Mutex);
308 packetBuffer->Commit(size);
309 m_BufferList.push_back(std::move(packetBuffer));
310 m_CommittedSize += size;
320 std::lock_guard<std::mutex> lock(m_Mutex);
322 packetBuffer->Release();
327 std::lock_guard<std::mutex> lock(m_Mutex);
329 if (m_BufferList.empty())
334 m_BufferList.pop_back();
335 m_ReadableSize += buffer->GetSize();
341 std::lock_guard<std::mutex> lock(m_Mutex);
343 m_ReadSize += packetBuffer->GetSize();
344 packetBuffer->MarkRead();
349 if (consumer !=
nullptr)
351 m_Consumer = consumer;
358 if (m_Consumer !=
nullptr)
360 m_Consumer->SetReadyToRead();
370 unsigned int m_MaxBufferSize;
373 std::vector<IPacketBufferPtr> m_BufferList;
379 unsigned int m_CommittedSize;
382 unsigned int m_ReadableSize;
385 unsigned int m_ReadSize;
398 std::string message(
"SendStreamMetaDataPacket");
399 unsigned int reserved = 0;
401 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
402 m_BufferManager.Commit(buffer, reserved,
false);
409 std::string message(
"SendCounterDirectoryPacket");
410 unsigned int reserved = 0;
412 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
413 m_BufferManager.Commit(buffer, reserved);
417 const std::vector<CounterValue>& values)
override 421 std::string message(
"SendPeriodicCounterCapturePacket");
422 unsigned int reserved = 0;
424 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
425 m_BufferManager.Commit(buffer, reserved);
429 const std::vector<uint16_t>& selectedCounterIds)
override 433 std::string message(
"SendPeriodicCounterSelectionPacket");
434 unsigned int reserved = 0;
436 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
437 m_BufferManager.Commit(buffer, reserved);
454 CategoryPtr category = std::make_unique<Category>(categoryName);
458 const Category* categoryPtr = category.get();
462 m_Categories.insert(std::move(category));
474 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
478 const Device* devicePtr = device.get();
482 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
488 const std::string& counterSetName,
495 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
499 const CounterSet* counterSetPtr = counterSet.get();
503 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
505 return counterSetPtr;
510 const std::string& parentCategoryName,
511 uint16_t counterClass,
512 uint16_t interpolation,
514 const std::string& name,
515 const std::string& description,
524 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
527 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
530 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
535 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
538 const std::string unitsValue = units.has_value() ? units.value() :
"";
541 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
555 const Counter* counterPtr = counter.get();
559 for (uint16_t counterUid : counterUids)
562 Category* parentCategory =
const_cast<Category*
>(GetCategory(parentCategoryName));
564 parentCategory->
m_Counters.push_back(counterUid);
567 m_Counters.insert(std::make_pair(counterUid, counter));
588 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](
const CategoryPtr& category)
592 return category->m_Name == name;
595 if (it == m_Categories.end())
632 bool isProfilingEnabled,
634 m_SendCounterPacket(mockBufferManager),
635 m_IsProfilingEnabled(isProfilingEnabled),
636 m_CaptureData(captureData)
642 return m_GuidGenerator.NextGuid();
648 return m_GuidGenerator.GenerateStaticId(str);
658 return m_CounterMapping;
663 return m_SendCounterPacket;
668 return m_IsProfilingEnabled;
678 uint16_t backendCounterId,
681 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
686 m_CounterMapping.Reset();
690 ProfilingGuidGenerator m_GuidGenerator;
693 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.
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) 2021 ARM Limited and Contributors.
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
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