8 #include <client/src/IProfilingConnectionFactory.hpp> 9 #include <client/src/ProfilingService.hpp> 10 #include <client/src/ProfilingUtils.hpp> 11 #include <client/src/SendCounterPacket.hpp> 12 #include <client/src/SendThread.hpp> 17 #include <client/include/Holder.hpp> 18 #include <client/include/IProfilingServiceStatus.hpp> 20 #include <common/include/Assert.hpp> 21 #include <common/include/CommonProfilingUtils.hpp> 22 #include <common/include/IgnoreUnused.hpp> 23 #include <common/include/NumericCast.hpp> 24 #include <common/include/Optional.hpp> 25 #include <common/include/ProfilingException.hpp> 26 #include <common/include/ProfilingGuidGenerator.hpp> 29 #include <condition_variable> 65 std::lock_guard<std::mutex> lock(m_Mutex);
72 std::lock_guard<std::mutex> lock(m_Mutex);
77 bool WritePacket(
const unsigned char* buffer, uint32_t length)
override 79 if (buffer ==
nullptr || length == 0)
84 uint32_t header = ReadUint32(buffer, 0);
86 uint32_t packetFamily = (header >> 26);
87 uint32_t packetId = ((header >> 16) & 1023);
106 std::lock_guard<std::mutex> lock(m_Mutex);
108 m_WrittenData.push_back({ packetType, length });
114 std::lock_guard<std::mutex> lock(m_Mutex);
116 if(packetInfo.second != 0)
118 return static_cast<long>(std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo));
122 return static_cast<long>(std::count_if(m_WrittenData.begin(), m_WrittenData.end(),
123 [&packetInfo](
const std::pair<PacketType, uint32_t> pair) {
return packetInfo.first == pair.first; }));
129 std::lock_guard<std::mutex> lock(m_Mutex);
131 m_Packet = std::move(packet);
140 std::this_thread::sleep_for(std::chrono::milliseconds(5));
141 std::lock_guard<std::mutex> lock(m_Mutex);
142 return std::move(m_Packet);
147 std::lock_guard<std::mutex> lock(m_Mutex);
149 return static_cast<unsigned long>(m_WrittenData.size());
154 std::lock_guard<std::mutex> lock(m_Mutex);
156 m_WrittenData.clear();
161 std::vector<std::pair<PacketType, uint32_t>> m_WrittenData;
162 arm::pipe::Packet m_Packet;
163 mutable std::mutex m_Mutex;
172 return std::make_unique<MockProfilingConnection>();
182 , m_Data(
std::make_unique<unsigned char[]>(m_MaxSize))
189 unsigned int GetSize()
const override {
return m_Size; }
193 void Commit(
unsigned int size)
override { m_Size = size; }
199 void Destroy()
override {m_Data.reset(
nullptr); m_Size = 0; m_MaxSize =0;}
202 unsigned int m_MaxSize;
204 std::unique_ptr<unsigned char[]> m_Data;
211 : m_BufferSize(size),
216 IPacketBufferPtr
Reserve(
unsigned int requestedSize,
unsigned int& reservedSize)
override 218 if (requestedSize > m_BufferSize)
220 reservedSize = m_BufferSize;
224 reservedSize = requestedSize;
227 return std::move(m_Buffer);
230 void Commit(IPacketBufferPtr& packetBuffer,
unsigned int size,
bool notifyConsumer =
true)
override 232 packetBuffer->Commit(size);
233 m_Buffer = std::move(packetBuffer);
243 return std::move(m_Buffer);
246 void Release(IPacketBufferPtr& packetBuffer)
override 248 packetBuffer->Release();
249 m_Buffer = std::move(packetBuffer);
252 void MarkRead(IPacketBufferPtr& packetBuffer)
override 254 packetBuffer->MarkRead();
255 m_Buffer = std::move(packetBuffer);
260 if (consumer !=
nullptr)
262 m_Consumer = consumer;
269 if (m_Consumer !=
nullptr)
271 m_Consumer->SetReadyToRead();
276 unsigned int m_BufferSize;
277 IPacketBufferPtr m_Buffer;
278 IConsumer* m_Consumer =
nullptr;
285 : m_MaxBufferSize(maxBufferSize)
293 IPacketBufferPtr
Reserve(
unsigned int requestedSize,
unsigned int& reservedSize)
override 295 std::lock_guard<std::mutex> lock(m_Mutex);
298 if (requestedSize > m_MaxBufferSize)
300 throw arm::pipe::InvalidArgumentException(
"The maximum buffer size that can be requested is [" +
301 std::to_string(m_MaxBufferSize) +
"] bytes");
303 reservedSize = requestedSize;
304 return std::make_unique<MockPacketBuffer>(requestedSize);
307 void Commit(IPacketBufferPtr& packetBuffer,
unsigned int size,
bool notifyConsumer =
true)
override 309 std::lock_guard<std::mutex> lock(m_Mutex);
311 packetBuffer->Commit(size);
312 m_BufferList.push_back(std::move(packetBuffer));
313 m_CommittedSize += size;
321 void Release(IPacketBufferPtr& packetBuffer)
override 323 std::lock_guard<std::mutex> lock(m_Mutex);
325 packetBuffer->Release();
330 std::lock_guard<std::mutex> lock(m_Mutex);
332 if (m_BufferList.empty())
336 IPacketBufferPtr buffer = std::move(m_BufferList.back());
337 m_BufferList.pop_back();
338 m_ReadableSize += buffer->GetSize();
342 void MarkRead(IPacketBufferPtr& packetBuffer)
override 344 std::lock_guard<std::mutex> lock(m_Mutex);
346 m_ReadSize += packetBuffer->GetSize();
347 packetBuffer->MarkRead();
352 if (consumer !=
nullptr)
354 m_Consumer = consumer;
361 if (m_Consumer !=
nullptr)
363 m_Consumer->SetReadyToRead();
373 unsigned int m_MaxBufferSize;
376 std::vector<IPacketBufferPtr> m_BufferList;
382 unsigned int m_CommittedSize;
385 unsigned int m_ReadableSize;
388 unsigned int m_ReadSize;
391 IConsumer* m_Consumer =
nullptr;
401 std::string message(
"SendStreamMetaDataPacket");
402 unsigned int reserved = 0;
403 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
404 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
405 m_BufferManager.Commit(buffer, reserved,
false);
412 std::string message(
"SendCounterDirectoryPacket");
413 unsigned int reserved = 0;
414 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
415 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
416 m_BufferManager.Commit(buffer, reserved);
420 const std::vector<CounterValue>& values)
override 424 std::string message(
"SendPeriodicCounterCapturePacket");
425 unsigned int reserved = 0;
426 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
427 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
428 m_BufferManager.Commit(buffer, reserved);
432 const std::vector<uint16_t>& selectedCounterIds)
override 436 std::string message(
"SendPeriodicCounterSelectionPacket");
437 unsigned int reserved = 0;
438 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
439 memcpy(buffer->GetWritableData(), message.c_str(),
static_cast<unsigned int>(message.size()) + 1);
440 m_BufferManager.Commit(buffer, reserved);
444 IBufferManager& m_BufferManager;
457 CategoryPtr category = std::make_unique<Category>(categoryName);
458 ARM_PIPE_ASSERT(category);
461 const Category* categoryPtr = category.get();
462 ARM_PIPE_ASSERT(categoryPtr);
465 m_Categories.insert(std::move(category));
474 uint16_t deviceUid = GetNextUid();
477 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
478 ARM_PIPE_ASSERT(device);
481 const Device* devicePtr = device.get();
482 ARM_PIPE_ASSERT(devicePtr);
485 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
491 const std::string& counterSetName,
495 uint16_t counterSetUid = GetNextUid();
498 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
499 ARM_PIPE_ASSERT(counterSet);
502 const CounterSet* counterSetPtr = counterSet.get();
503 ARM_PIPE_ASSERT(counterSetPtr);
506 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
508 return counterSetPtr;
513 const std::string& parentCategoryName,
514 uint16_t counterClass,
515 uint16_t interpolation,
517 const std::string& name,
518 const std::string& description,
519 const arm::pipe::Optional<std::string>& units = arm::pipe::EmptyOptional(),
520 const arm::pipe::Optional<uint16_t>& numberOfCores = arm::pipe::EmptyOptional(),
521 const arm::pipe::Optional<uint16_t>& deviceUid = arm::pipe::EmptyOptional(),
522 const arm::pipe::Optional<uint16_t>& counterSetUid = arm::pipe::EmptyOptional())
527 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
530 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
533 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
536 std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores);
537 ARM_PIPE_ASSERT(!counterUids.empty());
538 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
541 const std::string unitsValue = units.has_value() ? units.value() :
"";
544 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
555 ARM_PIPE_ASSERT(counter);
558 const Counter* counterPtr = counter.get();
559 ARM_PIPE_ASSERT(counterPtr);
562 for (uint16_t counterUid : counterUids)
565 Category* parentCategory =
const_cast<Category*
>(GetCategory(parentCategoryName));
566 ARM_PIPE_ASSERT(parentCategory);
567 parentCategory->m_Counters.push_back(counterUid);
570 m_Counters.insert(std::make_pair(counterUid, counter));
584 const Devices&
GetDevices()
const override {
return m_Devices; }
586 const Counters&
GetCounters()
const override {
return m_Counters; }
589 const Category*
GetCategory(
const std::string& name)
const override 591 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](
const CategoryPtr& category)
593 ARM_PIPE_ASSERT(category);
595 return category->m_Name == name;
598 if (it == m_Categories.end())
625 Categories m_Categories;
627 CounterSets m_CounterSets;
637 bool isProfilingEnabled,
638 const CaptureData& captureData) :
639 ProfilingService(maxGlobalCounterId,
641 arm::pipe::ARMNN_SOFTWARE_INFO,
642 arm::pipe::ARMNN_SOFTWARE_VERSION,
643 arm::pipe::ARMNN_HARDWARE_VERSION),
644 m_SendCounterPacket(mockBufferManager,
645 arm::pipe::ARMNN_SOFTWARE_INFO,
646 arm::pipe::ARMNN_SOFTWARE_VERSION,
647 arm::pipe::ARMNN_HARDWARE_VERSION),
648 m_IsProfilingEnabled(isProfilingEnabled),
649 m_CaptureData(captureData)
655 return m_GuidGenerator.NextGuid();
661 return m_GuidGenerator.GenerateStaticId(str);
671 return m_CounterMapping;
676 return m_SendCounterPacket;
681 return m_IsProfilingEnabled;
686 CaptureData copy(m_CaptureData);
691 uint16_t backendCounterId,
692 const std::string& backendId)
694 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
699 m_CounterMapping.Reset();
703 ProfilingGuidGenerator m_GuidGenerator;
704 CounterIdMap m_CounterMapping;
705 SendCounterPacket m_SendCounterPacket;
706 bool m_IsProfilingEnabled;
707 CaptureData m_CaptureData;
bool WritePacket(arm::pipe::Packet &&packet)
ProfilingDynamicGuid NextGuid() override
Return the next random Guid in the sequence.
const Categories & GetCategories() const override
bool IsOpen() const override
void SendCounterDirectoryPacket(const ICounterDirectory &counterDirectory) override
const Category * RegisterCategory(const std::string &categoryName)
void Release(IPacketBufferPtr &packetBuffer) override
IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize) override
const Devices & GetDevices() const override
const Device * RegisterDevice(const std::string &deviceName, uint16_t cores=0)
uint16_t GetCounterCount() const override
MockSendCounterPacket(IBufferManager &sendBuffer)
std::unique_ptr< ISendTimelinePacket > GetSendTimelinePacket() const override
~MockStreamCounterBuffer()
MockBufferManager(unsigned int size)
void NotifyProfilingServiceActive() override
MockProfilingConnection()
CaptureData GetCaptureData() override
long CheckForPacket(const std::pair< PacketType, uint32_t > packetInfo)
uint16_t GetDeviceCount() const override
void Commit(unsigned int size) override
arm::pipe::IInitialiseProfilingService IInitialiseProfilingService
void IgnoreUnused(Ts &&...)
const Category * GetCategory(const std::string &name) const override
arm::pipe::Packet ReadPacket(uint32_t timeout) override
const ICounterMappings & GetCounterMappings() const override
void MarkRead(IPacketBufferPtr &packetBuffer) override
bool WritePacket(const unsigned char *buffer, uint32_t length) override
MockPacketBuffer(unsigned int maxSize)
const CounterSets & GetCounterSets() const override
bool IsProfilingEnabled() const override
void MarkRead(IPacketBufferPtr &packetBuffer) override
unsigned int GetReadableSize() const
uint16_t GetCounterSetCount() const override
IPacketBufferPtr GetReadableBuffer() override
unsigned int GetReadSize() const
IProfilingConnectionPtr GetProfilingConnection(const ProfilingOptions &options) const override
const Counters & GetCounters() const override
void FlushReadList() override
uint16_t GetCategoryCount() const override
unsigned int GetSize() const override
void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, const std::vector< uint16_t > &selectedCounterIds) override
void FlushReadList() override
void Commit(IPacketBufferPtr &packetBuffer, unsigned int size, bool notifyConsumer=true) override
void SendPeriodicCounterCapturePacket(uint64_t timestamp, const std::vector< CounterValue > &values) override
const Device * GetDevice(uint16_t uid) const override
void SetConsumer(IConsumer *consumer) override
void RegisterMapping(uint16_t globalCounterId, uint16_t backendCounterId, const std::string &backendId)
void Release(IPacketBufferPtr &packetBuffer) override
unsigned long GetWrittenDataSize()
unsigned int GetCommittedSize() const
ProfilingStaticGuid GenerateStaticId(const std::string &str) override
Create a ProfilingStaticGuid based on a hash of the string.
void SendStreamMetaDataPacket() override
const CounterSet * GetCounterSet(uint16_t uid) const override
const unsigned char * GetReadableData() const override
const CounterSet * RegisterCounterSet(const std::string &counterSetName, uint16_t count=0)
IPacketBufferPtr GetReadableBuffer() override
void Commit(IPacketBufferPtr &packetBuffer, unsigned int size, bool notifyConsumer=true) override
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
void SetConsumer(IConsumer *consumer) override
const Counter * GetCounter(uint16_t uid) const override
unsigned char * GetWritableData() override
void WaitForProfilingServiceActivation(unsigned int timeout) override
const Counter * RegisterCounter(const std::string &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 arm::pipe::Optional< std::string > &units=arm::pipe::EmptyOptional(), const arm::pipe::Optional< uint16_t > &numberOfCores=arm::pipe::EmptyOptional(), const arm::pipe::Optional< uint16_t > &deviceUid=arm::pipe::EmptyOptional(), const arm::pipe::Optional< uint16_t > &counterSetUid=arm::pipe::EmptyOptional())
MockProfilingService(uint16_t maxGlobalCounterId, IInitialiseProfilingService &initialiser, MockBufferManager &mockBufferManager, bool isProfilingEnabled, const CaptureData &captureData)
ISendCounterPacket & GetSendCounterPacket() override
MockStreamCounterBuffer(unsigned int maxBufferSize=4096)
IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int &reservedSize) override