blob: 80ac1fed0e3e080465a1f2de86dba6e852e42600 [file] [log] [blame]
Jim Flynn64063552020-02-14 10:18:08 +00001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
Jim Flynn64063552020-02-14 10:18:08 +00003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Sadik Armagancab588a2020-02-17 11:33:31 +00008#include <Holder.hpp>
Jim Flynn64063552020-02-14 10:18:08 +00009#include <IProfilingConnectionFactory.hpp>
Jim Flynn6398a982020-05-27 17:05:21 +010010#include <IProfilingServiceStatus.hpp>
Sadik Armagan3184c902020-03-18 10:57:30 +000011#include <ProfilingService.hpp>
Jim Flynn64063552020-02-14 10:18:08 +000012#include <ProfilingUtils.hpp>
13#include <SendCounterPacket.hpp>
14#include <SendThread.hpp>
15
Jim Flynn9c85b412022-03-16 00:27:43 +000016#include <armnn/BackendId.hpp>
17#include <armnn/profiling/ArmNNProfiling.hpp>
18
Jim Flynn6730fe92022-03-10 22:57:47 +000019#include <common/include/Assert.hpp>
Jim Flynnc454ac92022-03-16 18:43:18 +000020#include <common/include/CommonProfilingUtils.hpp>
Jim Flynn9265a882022-03-10 23:35:26 +000021#include <common/include/IgnoreUnused.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000022#include <common/include/NumericCast.hpp>
Jim Flynndecd08b2022-03-13 22:35:46 +000023#include <common/include/Optional.hpp>
Jim Flynnf9db3ef2022-03-08 21:23:44 +000024#include <common/include/ProfilingException.hpp>
Nikhil Raj5b1bcc92021-06-08 12:31:50 +010025#include <common/include/ProfilingGuidGenerator.hpp>
Nikhil Raj7dcc6972021-04-30 15:44:24 +010026
Jim Flynn64063552020-02-14 10:18:08 +000027#include <atomic>
28#include <condition_variable>
29#include <mutex>
30#include <thread>
31
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000032namespace arm
Jim Flynn64063552020-02-14 10:18:08 +000033{
34
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000035namespace pipe
Jim Flynn64063552020-02-14 10:18:08 +000036{
37
38class MockProfilingConnection : public IProfilingConnection
39{
40public:
41 MockProfilingConnection()
42 : m_IsOpen(true)
43 , m_WrittenData()
44 , m_Packet()
45 {}
46
47 enum class PacketType
48 {
49 StreamMetaData,
50 ConnectionAcknowledge,
51 CounterDirectory,
52 ReqCounterDirectory,
53 PeriodicCounterSelection,
54 PerJobCounterSelection,
55 TimelineMessageDirectory,
56 PeriodicCounterCapture,
Keith Davis33ed2212020-03-30 10:43:41 +010057 ActivateTimelineReporting,
58 DeactivateTimelineReporting,
Jim Flynn64063552020-02-14 10:18:08 +000059 Unknown
60 };
61
62 bool IsOpen() const override
63 {
64 std::lock_guard<std::mutex> lock(m_Mutex);
65
66 return m_IsOpen;
67 }
68
69 void Close() override
70 {
71 std::lock_guard<std::mutex> lock(m_Mutex);
72
73 m_IsOpen = false;
74 }
75
76 bool WritePacket(const unsigned char* buffer, uint32_t length) override
77 {
78 if (buffer == nullptr || length == 0)
79 {
80 return false;
81 }
82
83 uint32_t header = ReadUint32(buffer, 0);
84
85 uint32_t packetFamily = (header >> 26);
86 uint32_t packetId = ((header >> 16) & 1023);
87
88 PacketType packetType;
89
90 switch (packetFamily)
91 {
92 case 0:
Keith Davis33ed2212020-03-30 10:43:41 +010093 packetType = packetId < 8 ? PacketType(packetId) : PacketType::Unknown;
Jim Flynn64063552020-02-14 10:18:08 +000094 break;
95 case 1:
96 packetType = packetId == 0 ? PacketType::TimelineMessageDirectory : PacketType::Unknown;
97 break;
98 case 3:
99 packetType = packetId == 0 ? PacketType::PeriodicCounterCapture : PacketType::Unknown;
100 break;
101 default:
102 packetType = PacketType::Unknown;
103 }
104
105 std::lock_guard<std::mutex> lock(m_Mutex);
106
107 m_WrittenData.push_back({ packetType, length });
108 return true;
109 }
110
111 long CheckForPacket(const std::pair<PacketType, uint32_t> packetInfo)
112 {
113 std::lock_guard<std::mutex> lock(m_Mutex);
114
115 if(packetInfo.second != 0)
116 {
Rob Hughesbb46dde2020-05-20 15:27:37 +0100117 return static_cast<long>(std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo));
Jim Flynn64063552020-02-14 10:18:08 +0000118 }
119 else
120 {
Rob Hughesbb46dde2020-05-20 15:27:37 +0100121 return static_cast<long>(std::count_if(m_WrittenData.begin(), m_WrittenData.end(),
122 [&packetInfo](const std::pair<PacketType, uint32_t> pair) { return packetInfo.first == pair.first; }));
Jim Flynn64063552020-02-14 10:18:08 +0000123 }
124 }
125
Jim Flynnbbfe6032020-07-20 16:57:44 +0100126 bool WritePacket(arm::pipe::Packet&& packet)
Jim Flynn64063552020-02-14 10:18:08 +0000127 {
128 std::lock_guard<std::mutex> lock(m_Mutex);
129
130 m_Packet = std::move(packet);
131 return true;
132 }
133
Jim Flynnbbfe6032020-07-20 16:57:44 +0100134 arm::pipe::Packet ReadPacket(uint32_t timeout) override
Jim Flynn64063552020-02-14 10:18:08 +0000135 {
Jim Flynn9265a882022-03-10 23:35:26 +0000136 arm::pipe::IgnoreUnused(timeout);
Jim Flynn64063552020-02-14 10:18:08 +0000137
138 // Simulate a delay in the reading process. The default timeout is way too long.
139 std::this_thread::sleep_for(std::chrono::milliseconds(5));
140 std::lock_guard<std::mutex> lock(m_Mutex);
141 return std::move(m_Packet);
142 }
143
144 unsigned long GetWrittenDataSize()
145 {
146 std::lock_guard<std::mutex> lock(m_Mutex);
147
Rob Hughesbb46dde2020-05-20 15:27:37 +0100148 return static_cast<unsigned long>(m_WrittenData.size());
Jim Flynn64063552020-02-14 10:18:08 +0000149 }
150
151 void Clear()
152 {
153 std::lock_guard<std::mutex> lock(m_Mutex);
154
155 m_WrittenData.clear();
156 }
157
158private:
159 bool m_IsOpen;
160 std::vector<std::pair<PacketType, uint32_t>> m_WrittenData;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100161 arm::pipe::Packet m_Packet;
Jim Flynn64063552020-02-14 10:18:08 +0000162 mutable std::mutex m_Mutex;
163};
164
165class MockProfilingConnectionFactory : public IProfilingConnectionFactory
166{
167public:
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000168 IProfilingConnectionPtr GetProfilingConnection(const ProfilingOptions& options) const override
Jim Flynn64063552020-02-14 10:18:08 +0000169 {
Jim Flynn9265a882022-03-10 23:35:26 +0000170 arm::pipe::IgnoreUnused(options);
Jim Flynn64063552020-02-14 10:18:08 +0000171 return std::make_unique<MockProfilingConnection>();
172 }
173};
174
175class MockPacketBuffer : public IPacketBuffer
176{
177public:
178 MockPacketBuffer(unsigned int maxSize)
179 : m_MaxSize(maxSize)
180 , m_Size(0)
181 , m_Data(std::make_unique<unsigned char[]>(m_MaxSize))
182 {}
183
184 ~MockPacketBuffer() {}
185
186 const unsigned char* GetReadableData() const override { return m_Data.get(); }
187
188 unsigned int GetSize() const override { return m_Size; }
189
190 void MarkRead() override { m_Size = 0; }
191
192 void Commit(unsigned int size) override { m_Size = size; }
193
194 void Release() override { m_Size = 0; }
195
196 unsigned char* GetWritableData() override { return m_Data.get(); }
197
Jim Flynn0204f092020-06-22 20:41:43 +0100198 void Destroy() override {m_Data.reset(nullptr); m_Size = 0; m_MaxSize =0;}
199
Jim Flynn64063552020-02-14 10:18:08 +0000200private:
201 unsigned int m_MaxSize;
202 unsigned int m_Size;
203 std::unique_ptr<unsigned char[]> m_Data;
204};
205
206class MockBufferManager : public IBufferManager
207{
208public:
209 MockBufferManager(unsigned int size)
210 : m_BufferSize(size),
211 m_Buffer(std::make_unique<MockPacketBuffer>(size)) {}
212
213 ~MockBufferManager() {}
214
215 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
216 {
217 if (requestedSize > m_BufferSize)
218 {
219 reservedSize = m_BufferSize;
220 }
221 else
222 {
223 reservedSize = requestedSize;
224 }
225
226 return std::move(m_Buffer);
227 }
228
229 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override
230 {
231 packetBuffer->Commit(size);
232 m_Buffer = std::move(packetBuffer);
233
234 if (notifyConsumer)
235 {
236 FlushReadList();
237 }
238 }
239
240 IPacketBufferPtr GetReadableBuffer() override
241 {
242 return std::move(m_Buffer);
243 }
244
245 void Release(IPacketBufferPtr& packetBuffer) override
246 {
247 packetBuffer->Release();
248 m_Buffer = std::move(packetBuffer);
249 }
250
251 void MarkRead(IPacketBufferPtr& packetBuffer) override
252 {
253 packetBuffer->MarkRead();
254 m_Buffer = std::move(packetBuffer);
255 }
256
257 void SetConsumer(IConsumer* consumer) override
258 {
259 if (consumer != nullptr)
260 {
261 m_Consumer = consumer;
262 }
263 }
264
265 void FlushReadList() override
266 {
267 // notify consumer that packet is ready to read
268 if (m_Consumer != nullptr)
269 {
270 m_Consumer->SetReadyToRead();
271 }
272 }
273
274private:
275 unsigned int m_BufferSize;
276 IPacketBufferPtr m_Buffer;
277 IConsumer* m_Consumer = nullptr;
278};
279
280class MockStreamCounterBuffer : public IBufferManager
281{
282public:
283 MockStreamCounterBuffer(unsigned int maxBufferSize = 4096)
284 : m_MaxBufferSize(maxBufferSize)
285 , m_BufferList()
286 , m_CommittedSize(0)
287 , m_ReadableSize(0)
288 , m_ReadSize(0)
289 {}
290 ~MockStreamCounterBuffer() {}
291
292 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
293 {
294 std::lock_guard<std::mutex> lock(m_Mutex);
295
296 reservedSize = 0;
297 if (requestedSize > m_MaxBufferSize)
298 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000299 throw arm::pipe::InvalidArgumentException("The maximum buffer size that can be requested is [" +
300 std::to_string(m_MaxBufferSize) + "] bytes");
Jim Flynn64063552020-02-14 10:18:08 +0000301 }
302 reservedSize = requestedSize;
303 return std::make_unique<MockPacketBuffer>(requestedSize);
304 }
305
306 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override
307 {
308 std::lock_guard<std::mutex> lock(m_Mutex);
309
310 packetBuffer->Commit(size);
311 m_BufferList.push_back(std::move(packetBuffer));
312 m_CommittedSize += size;
313
314 if (notifyConsumer)
315 {
316 FlushReadList();
317 }
318 }
319
320 void Release(IPacketBufferPtr& packetBuffer) override
321 {
322 std::lock_guard<std::mutex> lock(m_Mutex);
323
324 packetBuffer->Release();
325 }
326
327 IPacketBufferPtr GetReadableBuffer() override
328 {
329 std::lock_guard<std::mutex> lock(m_Mutex);
330
331 if (m_BufferList.empty())
332 {
333 return nullptr;
334 }
335 IPacketBufferPtr buffer = std::move(m_BufferList.back());
336 m_BufferList.pop_back();
337 m_ReadableSize += buffer->GetSize();
338 return buffer;
339 }
340
341 void MarkRead(IPacketBufferPtr& packetBuffer) override
342 {
343 std::lock_guard<std::mutex> lock(m_Mutex);
344
345 m_ReadSize += packetBuffer->GetSize();
346 packetBuffer->MarkRead();
347 }
348
349 void SetConsumer(IConsumer* consumer) override
350 {
351 if (consumer != nullptr)
352 {
353 m_Consumer = consumer;
354 }
355 }
356
357 void FlushReadList() override
358 {
359 // notify consumer that packet is ready to read
360 if (m_Consumer != nullptr)
361 {
362 m_Consumer->SetReadyToRead();
363 }
364 }
365
366 unsigned int GetCommittedSize() const { return m_CommittedSize; }
367 unsigned int GetReadableSize() const { return m_ReadableSize; }
368 unsigned int GetReadSize() const { return m_ReadSize; }
369
370private:
371 // The maximum buffer size when creating a new buffer
372 unsigned int m_MaxBufferSize;
373
374 // A list of buffers
375 std::vector<IPacketBufferPtr> m_BufferList;
376
377 // The mutex to synchronize this mock's methods
378 std::mutex m_Mutex;
379
380 // The total size of the buffers that has been committed for reading
381 unsigned int m_CommittedSize;
382
383 // The total size of the buffers that can be read
384 unsigned int m_ReadableSize;
385
386 // The total size of the buffers that has already been read
387 unsigned int m_ReadSize;
388
389 // Consumer thread to notify packet is ready to read
390 IConsumer* m_Consumer = nullptr;
391};
392
393class MockSendCounterPacket : public ISendCounterPacket
394{
395public:
396 MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {}
397
398 void SendStreamMetaDataPacket() override
399 {
400 std::string message("SendStreamMetaDataPacket");
401 unsigned int reserved = 0;
402 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
403 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
404 m_BufferManager.Commit(buffer, reserved, false);
405 }
406
407 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override
408 {
Jim Flynn9265a882022-03-10 23:35:26 +0000409 arm::pipe::IgnoreUnused(counterDirectory);
Jim Flynn64063552020-02-14 10:18:08 +0000410
411 std::string message("SendCounterDirectoryPacket");
412 unsigned int reserved = 0;
413 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
414 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
415 m_BufferManager.Commit(buffer, reserved);
416 }
417
418 void SendPeriodicCounterCapturePacket(uint64_t timestamp,
419 const std::vector<CounterValue>& values) override
420 {
Jim Flynn9265a882022-03-10 23:35:26 +0000421 arm::pipe::IgnoreUnused(timestamp, values);
Jim Flynn64063552020-02-14 10:18:08 +0000422
423 std::string message("SendPeriodicCounterCapturePacket");
424 unsigned int reserved = 0;
425 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
426 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
427 m_BufferManager.Commit(buffer, reserved);
428 }
429
430 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
431 const std::vector<uint16_t>& selectedCounterIds) override
432 {
Jim Flynn9265a882022-03-10 23:35:26 +0000433 arm::pipe::IgnoreUnused(capturePeriod, selectedCounterIds);
Jim Flynn64063552020-02-14 10:18:08 +0000434
435 std::string message("SendPeriodicCounterSelectionPacket");
436 unsigned int reserved = 0;
437 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
438 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
439 m_BufferManager.Commit(buffer, reserved);
440 }
441
442private:
443 IBufferManager& m_BufferManager;
444};
445
446class MockCounterDirectory : public ICounterDirectory
447{
448public:
449 MockCounterDirectory() = default;
450 ~MockCounterDirectory() = default;
451
452 // Register profiling objects
Sadik Armagan4c998992020-02-25 12:44:44 +0000453 const Category* RegisterCategory(const std::string& categoryName)
Jim Flynn64063552020-02-14 10:18:08 +0000454 {
Jim Flynn64063552020-02-14 10:18:08 +0000455 // Create the category
Sadik Armagan4c998992020-02-25 12:44:44 +0000456 CategoryPtr category = std::make_unique<Category>(categoryName);
Jim Flynn6730fe92022-03-10 22:57:47 +0000457 ARM_PIPE_ASSERT(category);
Jim Flynn64063552020-02-14 10:18:08 +0000458
459 // Get the raw category pointer
460 const Category* categoryPtr = category.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000461 ARM_PIPE_ASSERT(categoryPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000462
463 // Register the category
464 m_Categories.insert(std::move(category));
465
466 return categoryPtr;
467 }
468
469 const Device* RegisterDevice(const std::string& deviceName,
Sadik Armagan4c998992020-02-25 12:44:44 +0000470 uint16_t cores = 0)
Jim Flynn64063552020-02-14 10:18:08 +0000471 {
472 // Get the device UID
473 uint16_t deviceUid = GetNextUid();
474
475 // Create the device
476 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
Jim Flynn6730fe92022-03-10 22:57:47 +0000477 ARM_PIPE_ASSERT(device);
Jim Flynn64063552020-02-14 10:18:08 +0000478
479 // Get the raw device pointer
480 const Device* devicePtr = device.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000481 ARM_PIPE_ASSERT(devicePtr);
Jim Flynn64063552020-02-14 10:18:08 +0000482
483 // Register the device
484 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
485
Jim Flynn64063552020-02-14 10:18:08 +0000486 return devicePtr;
487 }
488
489 const CounterSet* RegisterCounterSet(
490 const std::string& counterSetName,
Sadik Armagan4c998992020-02-25 12:44:44 +0000491 uint16_t count = 0)
Jim Flynn64063552020-02-14 10:18:08 +0000492 {
493 // Get the counter set UID
494 uint16_t counterSetUid = GetNextUid();
495
496 // Create the counter set
497 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
Jim Flynn6730fe92022-03-10 22:57:47 +0000498 ARM_PIPE_ASSERT(counterSet);
Jim Flynn64063552020-02-14 10:18:08 +0000499
500 // Get the raw counter set pointer
501 const CounterSet* counterSetPtr = counterSet.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000502 ARM_PIPE_ASSERT(counterSetPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000503
504 // Register the counter set
505 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
506
Jim Flynn64063552020-02-14 10:18:08 +0000507 return counterSetPtr;
508 }
509
Cathal Corbett6f073722022-03-04 12:11:09 +0000510 const Counter* RegisterCounter(const std::string& backendId,
Jim Flynn64063552020-02-14 10:18:08 +0000511 const uint16_t uid,
512 const std::string& parentCategoryName,
513 uint16_t counterClass,
514 uint16_t interpolation,
515 double multiplier,
516 const std::string& name,
517 const std::string& description,
Jim Flynndecd08b2022-03-13 22:35:46 +0000518 const arm::pipe::Optional<std::string>& units = arm::pipe::EmptyOptional(),
519 const arm::pipe::Optional<uint16_t>& numberOfCores = arm::pipe::EmptyOptional(),
520 const arm::pipe::Optional<uint16_t>& deviceUid = arm::pipe::EmptyOptional(),
521 const arm::pipe::Optional<uint16_t>& counterSetUid = arm::pipe::EmptyOptional())
Jim Flynn64063552020-02-14 10:18:08 +0000522 {
Jim Flynn9265a882022-03-10 23:35:26 +0000523 arm::pipe::IgnoreUnused(backendId);
Jim Flynn64063552020-02-14 10:18:08 +0000524
525 // Get the number of cores from the argument only
526 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
527
528 // Get the device UID
529 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
530
531 // Get the counter set UID
532 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
533
534 // Get the counter UIDs and calculate the max counter UID
535 std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores);
Jim Flynn6730fe92022-03-10 22:57:47 +0000536 ARM_PIPE_ASSERT(!counterUids.empty());
Jim Flynn64063552020-02-14 10:18:08 +0000537 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
538
539 // Get the counter units
540 const std::string unitsValue = units.has_value() ? units.value() : "";
541
542 // Create the counter
543 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
544 counterUids.front(),
545 maxCounterUid,
546 counterClass,
547 interpolation,
548 multiplier,
549 name,
550 description,
551 unitsValue,
552 deviceUidValue,
553 counterSetUidValue);
Jim Flynn6730fe92022-03-10 22:57:47 +0000554 ARM_PIPE_ASSERT(counter);
Jim Flynn64063552020-02-14 10:18:08 +0000555
556 // Get the raw counter pointer
557 const Counter* counterPtr = counter.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000558 ARM_PIPE_ASSERT(counterPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000559
560 // Process multiple counters if necessary
561 for (uint16_t counterUid : counterUids)
562 {
563 // Connect the counter to the parent category
564 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName));
Jim Flynn6730fe92022-03-10 22:57:47 +0000565 ARM_PIPE_ASSERT(parentCategory);
Jim Flynn64063552020-02-14 10:18:08 +0000566 parentCategory->m_Counters.push_back(counterUid);
567
568 // Register the counter
569 m_Counters.insert(std::make_pair(counterUid, counter));
570 }
571
572 return counterPtr;
573 }
574
575 // Getters for counts
Jim Flynn75c14f42022-03-10 22:05:42 +0000576 uint16_t GetCategoryCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Categories.size()); }
577 uint16_t GetDeviceCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Devices.size()); }
578 uint16_t GetCounterSetCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_CounterSets.size()); }
579 uint16_t GetCounterCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Counters.size()); }
Jim Flynn64063552020-02-14 10:18:08 +0000580
581 // Getters for collections
582 const Categories& GetCategories() const override { return m_Categories; }
583 const Devices& GetDevices() const override { return m_Devices; }
584 const CounterSets& GetCounterSets() const override { return m_CounterSets; }
585 const Counters& GetCounters() const override { return m_Counters; }
586
587 // Getters for profiling objects
588 const Category* GetCategory(const std::string& name) const override
589 {
590 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](const CategoryPtr& category)
591 {
Jim Flynn6730fe92022-03-10 22:57:47 +0000592 ARM_PIPE_ASSERT(category);
Jim Flynn64063552020-02-14 10:18:08 +0000593
594 return category->m_Name == name;
595 });
596
597 if (it == m_Categories.end())
598 {
599 return nullptr;
600 }
601
602 return it->get();
603 }
604
605 const Device* GetDevice(uint16_t uid) const override
606 {
Jim Flynn9265a882022-03-10 23:35:26 +0000607 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000608 return nullptr; // Not used by the unit tests
609 }
610
611 const CounterSet* GetCounterSet(uint16_t uid) const override
612 {
Jim Flynn9265a882022-03-10 23:35:26 +0000613 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000614 return nullptr; // Not used by the unit tests
615 }
616
617 const Counter* GetCounter(uint16_t uid) const override
618 {
Jim Flynn9265a882022-03-10 23:35:26 +0000619 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000620 return nullptr; // Not used by the unit tests
621 }
622
623private:
624 Categories m_Categories;
625 Devices m_Devices;
626 CounterSets m_CounterSets;
627 Counters m_Counters;
628};
629
Sadik Armagan3184c902020-03-18 10:57:30 +0000630class MockProfilingService : public ProfilingService
Sadik Armagancab588a2020-02-17 11:33:31 +0000631{
632public:
Jim Flynn34430252022-03-04 15:03:58 +0000633 MockProfilingService(uint16_t maxGlobalCounterId,
634 IInitialiseProfilingService& initialiser,
635 MockBufferManager& mockBufferManager,
Sadik Armagancab588a2020-02-17 11:33:31 +0000636 bool isProfilingEnabled,
637 const CaptureData& captureData) :
Jim Flynn9c85b412022-03-16 00:27:43 +0000638 ProfilingService(maxGlobalCounterId,
639 initialiser,
640 arm::pipe::ARMNN_SOFTWARE_INFO,
641 arm::pipe::ARMNN_SOFTWARE_VERSION,
642 arm::pipe::ARMNN_HARDWARE_VERSION),
643 m_SendCounterPacket(mockBufferManager,
644 arm::pipe::ARMNN_SOFTWARE_INFO,
645 arm::pipe::ARMNN_SOFTWARE_VERSION,
646 arm::pipe::ARMNN_HARDWARE_VERSION),
Sadik Armagancab588a2020-02-17 11:33:31 +0000647 m_IsProfilingEnabled(isProfilingEnabled),
Keith Davis33ed2212020-03-30 10:43:41 +0100648 m_CaptureData(captureData)
649 {}
Sadik Armagancab588a2020-02-17 11:33:31 +0000650
651 /// Return the next random Guid in the sequence
652 ProfilingDynamicGuid NextGuid() override
653 {
654 return m_GuidGenerator.NextGuid();
655 }
656
657 /// Create a ProfilingStaticGuid based on a hash of the string
658 ProfilingStaticGuid GenerateStaticId(const std::string& str) override
659 {
660 return m_GuidGenerator.GenerateStaticId(str);
661 }
662
663 std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const override
664 {
665 return nullptr;
666 }
667
668 const ICounterMappings& GetCounterMappings() const override
669 {
670 return m_CounterMapping;
671 }
672
673 ISendCounterPacket& GetSendCounterPacket() override
674 {
675 return m_SendCounterPacket;
676 }
677
678 bool IsProfilingEnabled() const override
679 {
680 return m_IsProfilingEnabled;
681 }
682
683 CaptureData GetCaptureData() override
684 {
685 CaptureData copy(m_CaptureData);
686 return copy;
687 }
688
689 void RegisterMapping(uint16_t globalCounterId,
690 uint16_t backendCounterId,
Cathal Corbett6f073722022-03-04 12:11:09 +0000691 const std::string& backendId)
Sadik Armagancab588a2020-02-17 11:33:31 +0000692 {
693 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
694 }
695
Sadik Armagan3184c902020-03-18 10:57:30 +0000696 void Reset()
Sadik Armagancab588a2020-02-17 11:33:31 +0000697 {
698 m_CounterMapping.Reset();
699 }
700
701private:
702 ProfilingGuidGenerator m_GuidGenerator;
Keith Davis33ed2212020-03-30 10:43:41 +0100703 CounterIdMap m_CounterMapping;
704 SendCounterPacket m_SendCounterPacket;
705 bool m_IsProfilingEnabled;
706 CaptureData m_CaptureData;
Sadik Armagancab588a2020-02-17 11:33:31 +0000707};
708
Jim Flynn6398a982020-05-27 17:05:21 +0100709class MockProfilingServiceStatus : public IProfilingServiceStatus
710{
711public:
712 void NotifyProfilingServiceActive() override {}
Jim Flynn9265a882022-03-10 23:35:26 +0000713 void WaitForProfilingServiceActivation(unsigned int timeout) override { arm::pipe::IgnoreUnused(timeout); }
Jim Flynn6398a982020-05-27 17:05:21 +0100714};
715
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000716} // namespace pipe
Jim Flynn64063552020-02-14 10:18:08 +0000717
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000718} // namespace arm