blob: cfc1096b8d111c7c23d545335fee92654fb014e9 [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 Flynn9265a882022-03-10 23:35:26 +000020#include <common/include/IgnoreUnused.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000021#include <common/include/NumericCast.hpp>
Jim Flynndecd08b2022-03-13 22:35:46 +000022#include <common/include/Optional.hpp>
Jim Flynnf9db3ef2022-03-08 21:23:44 +000023#include <common/include/ProfilingException.hpp>
Nikhil Raj5b1bcc92021-06-08 12:31:50 +010024#include <common/include/ProfilingGuidGenerator.hpp>
Nikhil Raj7dcc6972021-04-30 15:44:24 +010025
Jim Flynn64063552020-02-14 10:18:08 +000026#include <atomic>
27#include <condition_variable>
28#include <mutex>
29#include <thread>
30
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000031namespace arm
Jim Flynn64063552020-02-14 10:18:08 +000032{
33
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000034namespace pipe
Jim Flynn64063552020-02-14 10:18:08 +000035{
36
37class MockProfilingConnection : public IProfilingConnection
38{
39public:
40 MockProfilingConnection()
41 : m_IsOpen(true)
42 , m_WrittenData()
43 , m_Packet()
44 {}
45
46 enum class PacketType
47 {
48 StreamMetaData,
49 ConnectionAcknowledge,
50 CounterDirectory,
51 ReqCounterDirectory,
52 PeriodicCounterSelection,
53 PerJobCounterSelection,
54 TimelineMessageDirectory,
55 PeriodicCounterCapture,
Keith Davis33ed2212020-03-30 10:43:41 +010056 ActivateTimelineReporting,
57 DeactivateTimelineReporting,
Jim Flynn64063552020-02-14 10:18:08 +000058 Unknown
59 };
60
61 bool IsOpen() const override
62 {
63 std::lock_guard<std::mutex> lock(m_Mutex);
64
65 return m_IsOpen;
66 }
67
68 void Close() override
69 {
70 std::lock_guard<std::mutex> lock(m_Mutex);
71
72 m_IsOpen = false;
73 }
74
75 bool WritePacket(const unsigned char* buffer, uint32_t length) override
76 {
77 if (buffer == nullptr || length == 0)
78 {
79 return false;
80 }
81
82 uint32_t header = ReadUint32(buffer, 0);
83
84 uint32_t packetFamily = (header >> 26);
85 uint32_t packetId = ((header >> 16) & 1023);
86
87 PacketType packetType;
88
89 switch (packetFamily)
90 {
91 case 0:
Keith Davis33ed2212020-03-30 10:43:41 +010092 packetType = packetId < 8 ? PacketType(packetId) : PacketType::Unknown;
Jim Flynn64063552020-02-14 10:18:08 +000093 break;
94 case 1:
95 packetType = packetId == 0 ? PacketType::TimelineMessageDirectory : PacketType::Unknown;
96 break;
97 case 3:
98 packetType = packetId == 0 ? PacketType::PeriodicCounterCapture : PacketType::Unknown;
99 break;
100 default:
101 packetType = PacketType::Unknown;
102 }
103
104 std::lock_guard<std::mutex> lock(m_Mutex);
105
106 m_WrittenData.push_back({ packetType, length });
107 return true;
108 }
109
110 long CheckForPacket(const std::pair<PacketType, uint32_t> packetInfo)
111 {
112 std::lock_guard<std::mutex> lock(m_Mutex);
113
114 if(packetInfo.second != 0)
115 {
Rob Hughesbb46dde2020-05-20 15:27:37 +0100116 return static_cast<long>(std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo));
Jim Flynn64063552020-02-14 10:18:08 +0000117 }
118 else
119 {
Rob Hughesbb46dde2020-05-20 15:27:37 +0100120 return static_cast<long>(std::count_if(m_WrittenData.begin(), m_WrittenData.end(),
121 [&packetInfo](const std::pair<PacketType, uint32_t> pair) { return packetInfo.first == pair.first; }));
Jim Flynn64063552020-02-14 10:18:08 +0000122 }
123 }
124
Jim Flynnbbfe6032020-07-20 16:57:44 +0100125 bool WritePacket(arm::pipe::Packet&& packet)
Jim Flynn64063552020-02-14 10:18:08 +0000126 {
127 std::lock_guard<std::mutex> lock(m_Mutex);
128
129 m_Packet = std::move(packet);
130 return true;
131 }
132
Jim Flynnbbfe6032020-07-20 16:57:44 +0100133 arm::pipe::Packet ReadPacket(uint32_t timeout) override
Jim Flynn64063552020-02-14 10:18:08 +0000134 {
Jim Flynn9265a882022-03-10 23:35:26 +0000135 arm::pipe::IgnoreUnused(timeout);
Jim Flynn64063552020-02-14 10:18:08 +0000136
137 // Simulate a delay in the reading process. The default timeout is way too long.
138 std::this_thread::sleep_for(std::chrono::milliseconds(5));
139 std::lock_guard<std::mutex> lock(m_Mutex);
140 return std::move(m_Packet);
141 }
142
143 unsigned long GetWrittenDataSize()
144 {
145 std::lock_guard<std::mutex> lock(m_Mutex);
146
Rob Hughesbb46dde2020-05-20 15:27:37 +0100147 return static_cast<unsigned long>(m_WrittenData.size());
Jim Flynn64063552020-02-14 10:18:08 +0000148 }
149
150 void Clear()
151 {
152 std::lock_guard<std::mutex> lock(m_Mutex);
153
154 m_WrittenData.clear();
155 }
156
157private:
158 bool m_IsOpen;
159 std::vector<std::pair<PacketType, uint32_t>> m_WrittenData;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100160 arm::pipe::Packet m_Packet;
Jim Flynn64063552020-02-14 10:18:08 +0000161 mutable std::mutex m_Mutex;
162};
163
164class MockProfilingConnectionFactory : public IProfilingConnectionFactory
165{
166public:
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000167 IProfilingConnectionPtr GetProfilingConnection(const ProfilingOptions& options) const override
Jim Flynn64063552020-02-14 10:18:08 +0000168 {
Jim Flynn9265a882022-03-10 23:35:26 +0000169 arm::pipe::IgnoreUnused(options);
Jim Flynn64063552020-02-14 10:18:08 +0000170 return std::make_unique<MockProfilingConnection>();
171 }
172};
173
174class MockPacketBuffer : public IPacketBuffer
175{
176public:
177 MockPacketBuffer(unsigned int maxSize)
178 : m_MaxSize(maxSize)
179 , m_Size(0)
180 , m_Data(std::make_unique<unsigned char[]>(m_MaxSize))
181 {}
182
183 ~MockPacketBuffer() {}
184
185 const unsigned char* GetReadableData() const override { return m_Data.get(); }
186
187 unsigned int GetSize() const override { return m_Size; }
188
189 void MarkRead() override { m_Size = 0; }
190
191 void Commit(unsigned int size) override { m_Size = size; }
192
193 void Release() override { m_Size = 0; }
194
195 unsigned char* GetWritableData() override { return m_Data.get(); }
196
Jim Flynn0204f092020-06-22 20:41:43 +0100197 void Destroy() override {m_Data.reset(nullptr); m_Size = 0; m_MaxSize =0;}
198
Jim Flynn64063552020-02-14 10:18:08 +0000199private:
200 unsigned int m_MaxSize;
201 unsigned int m_Size;
202 std::unique_ptr<unsigned char[]> m_Data;
203};
204
205class MockBufferManager : public IBufferManager
206{
207public:
208 MockBufferManager(unsigned int size)
209 : m_BufferSize(size),
210 m_Buffer(std::make_unique<MockPacketBuffer>(size)) {}
211
212 ~MockBufferManager() {}
213
214 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
215 {
216 if (requestedSize > m_BufferSize)
217 {
218 reservedSize = m_BufferSize;
219 }
220 else
221 {
222 reservedSize = requestedSize;
223 }
224
225 return std::move(m_Buffer);
226 }
227
228 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override
229 {
230 packetBuffer->Commit(size);
231 m_Buffer = std::move(packetBuffer);
232
233 if (notifyConsumer)
234 {
235 FlushReadList();
236 }
237 }
238
239 IPacketBufferPtr GetReadableBuffer() override
240 {
241 return std::move(m_Buffer);
242 }
243
244 void Release(IPacketBufferPtr& packetBuffer) override
245 {
246 packetBuffer->Release();
247 m_Buffer = std::move(packetBuffer);
248 }
249
250 void MarkRead(IPacketBufferPtr& packetBuffer) override
251 {
252 packetBuffer->MarkRead();
253 m_Buffer = std::move(packetBuffer);
254 }
255
256 void SetConsumer(IConsumer* consumer) override
257 {
258 if (consumer != nullptr)
259 {
260 m_Consumer = consumer;
261 }
262 }
263
264 void FlushReadList() override
265 {
266 // notify consumer that packet is ready to read
267 if (m_Consumer != nullptr)
268 {
269 m_Consumer->SetReadyToRead();
270 }
271 }
272
273private:
274 unsigned int m_BufferSize;
275 IPacketBufferPtr m_Buffer;
276 IConsumer* m_Consumer = nullptr;
277};
278
279class MockStreamCounterBuffer : public IBufferManager
280{
281public:
282 MockStreamCounterBuffer(unsigned int maxBufferSize = 4096)
283 : m_MaxBufferSize(maxBufferSize)
284 , m_BufferList()
285 , m_CommittedSize(0)
286 , m_ReadableSize(0)
287 , m_ReadSize(0)
288 {}
289 ~MockStreamCounterBuffer() {}
290
291 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
292 {
293 std::lock_guard<std::mutex> lock(m_Mutex);
294
295 reservedSize = 0;
296 if (requestedSize > m_MaxBufferSize)
297 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000298 throw arm::pipe::InvalidArgumentException("The maximum buffer size that can be requested is [" +
299 std::to_string(m_MaxBufferSize) + "] bytes");
Jim Flynn64063552020-02-14 10:18:08 +0000300 }
301 reservedSize = requestedSize;
302 return std::make_unique<MockPacketBuffer>(requestedSize);
303 }
304
305 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override
306 {
307 std::lock_guard<std::mutex> lock(m_Mutex);
308
309 packetBuffer->Commit(size);
310 m_BufferList.push_back(std::move(packetBuffer));
311 m_CommittedSize += size;
312
313 if (notifyConsumer)
314 {
315 FlushReadList();
316 }
317 }
318
319 void Release(IPacketBufferPtr& packetBuffer) override
320 {
321 std::lock_guard<std::mutex> lock(m_Mutex);
322
323 packetBuffer->Release();
324 }
325
326 IPacketBufferPtr GetReadableBuffer() override
327 {
328 std::lock_guard<std::mutex> lock(m_Mutex);
329
330 if (m_BufferList.empty())
331 {
332 return nullptr;
333 }
334 IPacketBufferPtr buffer = std::move(m_BufferList.back());
335 m_BufferList.pop_back();
336 m_ReadableSize += buffer->GetSize();
337 return buffer;
338 }
339
340 void MarkRead(IPacketBufferPtr& packetBuffer) override
341 {
342 std::lock_guard<std::mutex> lock(m_Mutex);
343
344 m_ReadSize += packetBuffer->GetSize();
345 packetBuffer->MarkRead();
346 }
347
348 void SetConsumer(IConsumer* consumer) override
349 {
350 if (consumer != nullptr)
351 {
352 m_Consumer = consumer;
353 }
354 }
355
356 void FlushReadList() override
357 {
358 // notify consumer that packet is ready to read
359 if (m_Consumer != nullptr)
360 {
361 m_Consumer->SetReadyToRead();
362 }
363 }
364
365 unsigned int GetCommittedSize() const { return m_CommittedSize; }
366 unsigned int GetReadableSize() const { return m_ReadableSize; }
367 unsigned int GetReadSize() const { return m_ReadSize; }
368
369private:
370 // The maximum buffer size when creating a new buffer
371 unsigned int m_MaxBufferSize;
372
373 // A list of buffers
374 std::vector<IPacketBufferPtr> m_BufferList;
375
376 // The mutex to synchronize this mock's methods
377 std::mutex m_Mutex;
378
379 // The total size of the buffers that has been committed for reading
380 unsigned int m_CommittedSize;
381
382 // The total size of the buffers that can be read
383 unsigned int m_ReadableSize;
384
385 // The total size of the buffers that has already been read
386 unsigned int m_ReadSize;
387
388 // Consumer thread to notify packet is ready to read
389 IConsumer* m_Consumer = nullptr;
390};
391
392class MockSendCounterPacket : public ISendCounterPacket
393{
394public:
395 MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {}
396
397 void SendStreamMetaDataPacket() override
398 {
399 std::string message("SendStreamMetaDataPacket");
400 unsigned int reserved = 0;
401 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
402 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
403 m_BufferManager.Commit(buffer, reserved, false);
404 }
405
406 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override
407 {
Jim Flynn9265a882022-03-10 23:35:26 +0000408 arm::pipe::IgnoreUnused(counterDirectory);
Jim Flynn64063552020-02-14 10:18:08 +0000409
410 std::string message("SendCounterDirectoryPacket");
411 unsigned int reserved = 0;
412 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
413 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
414 m_BufferManager.Commit(buffer, reserved);
415 }
416
417 void SendPeriodicCounterCapturePacket(uint64_t timestamp,
418 const std::vector<CounterValue>& values) override
419 {
Jim Flynn9265a882022-03-10 23:35:26 +0000420 arm::pipe::IgnoreUnused(timestamp, values);
Jim Flynn64063552020-02-14 10:18:08 +0000421
422 std::string message("SendPeriodicCounterCapturePacket");
423 unsigned int reserved = 0;
424 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
425 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
426 m_BufferManager.Commit(buffer, reserved);
427 }
428
429 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
430 const std::vector<uint16_t>& selectedCounterIds) override
431 {
Jim Flynn9265a882022-03-10 23:35:26 +0000432 arm::pipe::IgnoreUnused(capturePeriod, selectedCounterIds);
Jim Flynn64063552020-02-14 10:18:08 +0000433
434 std::string message("SendPeriodicCounterSelectionPacket");
435 unsigned int reserved = 0;
436 IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved);
437 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
438 m_BufferManager.Commit(buffer, reserved);
439 }
440
441private:
442 IBufferManager& m_BufferManager;
443};
444
445class MockCounterDirectory : public ICounterDirectory
446{
447public:
448 MockCounterDirectory() = default;
449 ~MockCounterDirectory() = default;
450
451 // Register profiling objects
Sadik Armagan4c998992020-02-25 12:44:44 +0000452 const Category* RegisterCategory(const std::string& categoryName)
Jim Flynn64063552020-02-14 10:18:08 +0000453 {
Jim Flynn64063552020-02-14 10:18:08 +0000454 // Create the category
Sadik Armagan4c998992020-02-25 12:44:44 +0000455 CategoryPtr category = std::make_unique<Category>(categoryName);
Jim Flynn6730fe92022-03-10 22:57:47 +0000456 ARM_PIPE_ASSERT(category);
Jim Flynn64063552020-02-14 10:18:08 +0000457
458 // Get the raw category pointer
459 const Category* categoryPtr = category.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000460 ARM_PIPE_ASSERT(categoryPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000461
462 // Register the category
463 m_Categories.insert(std::move(category));
464
465 return categoryPtr;
466 }
467
468 const Device* RegisterDevice(const std::string& deviceName,
Sadik Armagan4c998992020-02-25 12:44:44 +0000469 uint16_t cores = 0)
Jim Flynn64063552020-02-14 10:18:08 +0000470 {
471 // Get the device UID
472 uint16_t deviceUid = GetNextUid();
473
474 // Create the device
475 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
Jim Flynn6730fe92022-03-10 22:57:47 +0000476 ARM_PIPE_ASSERT(device);
Jim Flynn64063552020-02-14 10:18:08 +0000477
478 // Get the raw device pointer
479 const Device* devicePtr = device.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000480 ARM_PIPE_ASSERT(devicePtr);
Jim Flynn64063552020-02-14 10:18:08 +0000481
482 // Register the device
483 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
484
Jim Flynn64063552020-02-14 10:18:08 +0000485 return devicePtr;
486 }
487
488 const CounterSet* RegisterCounterSet(
489 const std::string& counterSetName,
Sadik Armagan4c998992020-02-25 12:44:44 +0000490 uint16_t count = 0)
Jim Flynn64063552020-02-14 10:18:08 +0000491 {
492 // Get the counter set UID
493 uint16_t counterSetUid = GetNextUid();
494
495 // Create the counter set
496 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
Jim Flynn6730fe92022-03-10 22:57:47 +0000497 ARM_PIPE_ASSERT(counterSet);
Jim Flynn64063552020-02-14 10:18:08 +0000498
499 // Get the raw counter set pointer
500 const CounterSet* counterSetPtr = counterSet.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000501 ARM_PIPE_ASSERT(counterSetPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000502
503 // Register the counter set
504 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
505
Jim Flynn64063552020-02-14 10:18:08 +0000506 return counterSetPtr;
507 }
508
Cathal Corbett6f073722022-03-04 12:11:09 +0000509 const Counter* RegisterCounter(const std::string& backendId,
Jim Flynn64063552020-02-14 10:18:08 +0000510 const uint16_t uid,
511 const std::string& parentCategoryName,
512 uint16_t counterClass,
513 uint16_t interpolation,
514 double multiplier,
515 const std::string& name,
516 const std::string& description,
Jim Flynndecd08b2022-03-13 22:35:46 +0000517 const arm::pipe::Optional<std::string>& units = arm::pipe::EmptyOptional(),
518 const arm::pipe::Optional<uint16_t>& numberOfCores = arm::pipe::EmptyOptional(),
519 const arm::pipe::Optional<uint16_t>& deviceUid = arm::pipe::EmptyOptional(),
520 const arm::pipe::Optional<uint16_t>& counterSetUid = arm::pipe::EmptyOptional())
Jim Flynn64063552020-02-14 10:18:08 +0000521 {
Jim Flynn9265a882022-03-10 23:35:26 +0000522 arm::pipe::IgnoreUnused(backendId);
Jim Flynn64063552020-02-14 10:18:08 +0000523
524 // Get the number of cores from the argument only
525 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
526
527 // Get the device UID
528 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
529
530 // Get the counter set UID
531 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
532
533 // Get the counter UIDs and calculate the max counter UID
534 std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores);
Jim Flynn6730fe92022-03-10 22:57:47 +0000535 ARM_PIPE_ASSERT(!counterUids.empty());
Jim Flynn64063552020-02-14 10:18:08 +0000536 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
537
538 // Get the counter units
539 const std::string unitsValue = units.has_value() ? units.value() : "";
540
541 // Create the counter
542 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
543 counterUids.front(),
544 maxCounterUid,
545 counterClass,
546 interpolation,
547 multiplier,
548 name,
549 description,
550 unitsValue,
551 deviceUidValue,
552 counterSetUidValue);
Jim Flynn6730fe92022-03-10 22:57:47 +0000553 ARM_PIPE_ASSERT(counter);
Jim Flynn64063552020-02-14 10:18:08 +0000554
555 // Get the raw counter pointer
556 const Counter* counterPtr = counter.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000557 ARM_PIPE_ASSERT(counterPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000558
559 // Process multiple counters if necessary
560 for (uint16_t counterUid : counterUids)
561 {
562 // Connect the counter to the parent category
563 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName));
Jim Flynn6730fe92022-03-10 22:57:47 +0000564 ARM_PIPE_ASSERT(parentCategory);
Jim Flynn64063552020-02-14 10:18:08 +0000565 parentCategory->m_Counters.push_back(counterUid);
566
567 // Register the counter
568 m_Counters.insert(std::make_pair(counterUid, counter));
569 }
570
571 return counterPtr;
572 }
573
574 // Getters for counts
Jim Flynn75c14f42022-03-10 22:05:42 +0000575 uint16_t GetCategoryCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Categories.size()); }
576 uint16_t GetDeviceCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Devices.size()); }
577 uint16_t GetCounterSetCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_CounterSets.size()); }
578 uint16_t GetCounterCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Counters.size()); }
Jim Flynn64063552020-02-14 10:18:08 +0000579
580 // Getters for collections
581 const Categories& GetCategories() const override { return m_Categories; }
582 const Devices& GetDevices() const override { return m_Devices; }
583 const CounterSets& GetCounterSets() const override { return m_CounterSets; }
584 const Counters& GetCounters() const override { return m_Counters; }
585
586 // Getters for profiling objects
587 const Category* GetCategory(const std::string& name) const override
588 {
589 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](const CategoryPtr& category)
590 {
Jim Flynn6730fe92022-03-10 22:57:47 +0000591 ARM_PIPE_ASSERT(category);
Jim Flynn64063552020-02-14 10:18:08 +0000592
593 return category->m_Name == name;
594 });
595
596 if (it == m_Categories.end())
597 {
598 return nullptr;
599 }
600
601 return it->get();
602 }
603
604 const Device* GetDevice(uint16_t uid) const override
605 {
Jim Flynn9265a882022-03-10 23:35:26 +0000606 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000607 return nullptr; // Not used by the unit tests
608 }
609
610 const CounterSet* GetCounterSet(uint16_t uid) const override
611 {
Jim Flynn9265a882022-03-10 23:35:26 +0000612 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000613 return nullptr; // Not used by the unit tests
614 }
615
616 const Counter* GetCounter(uint16_t uid) const override
617 {
Jim Flynn9265a882022-03-10 23:35:26 +0000618 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000619 return nullptr; // Not used by the unit tests
620 }
621
622private:
623 Categories m_Categories;
624 Devices m_Devices;
625 CounterSets m_CounterSets;
626 Counters m_Counters;
627};
628
Sadik Armagan3184c902020-03-18 10:57:30 +0000629class MockProfilingService : public ProfilingService
Sadik Armagancab588a2020-02-17 11:33:31 +0000630{
631public:
Jim Flynn34430252022-03-04 15:03:58 +0000632 MockProfilingService(uint16_t maxGlobalCounterId,
633 IInitialiseProfilingService& initialiser,
634 MockBufferManager& mockBufferManager,
Sadik Armagancab588a2020-02-17 11:33:31 +0000635 bool isProfilingEnabled,
636 const CaptureData& captureData) :
Jim Flynn9c85b412022-03-16 00:27:43 +0000637 ProfilingService(maxGlobalCounterId,
638 initialiser,
639 arm::pipe::ARMNN_SOFTWARE_INFO,
640 arm::pipe::ARMNN_SOFTWARE_VERSION,
641 arm::pipe::ARMNN_HARDWARE_VERSION),
642 m_SendCounterPacket(mockBufferManager,
643 arm::pipe::ARMNN_SOFTWARE_INFO,
644 arm::pipe::ARMNN_SOFTWARE_VERSION,
645 arm::pipe::ARMNN_HARDWARE_VERSION),
Sadik Armagancab588a2020-02-17 11:33:31 +0000646 m_IsProfilingEnabled(isProfilingEnabled),
Keith Davis33ed2212020-03-30 10:43:41 +0100647 m_CaptureData(captureData)
648 {}
Sadik Armagancab588a2020-02-17 11:33:31 +0000649
650 /// Return the next random Guid in the sequence
651 ProfilingDynamicGuid NextGuid() override
652 {
653 return m_GuidGenerator.NextGuid();
654 }
655
656 /// Create a ProfilingStaticGuid based on a hash of the string
657 ProfilingStaticGuid GenerateStaticId(const std::string& str) override
658 {
659 return m_GuidGenerator.GenerateStaticId(str);
660 }
661
662 std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const override
663 {
664 return nullptr;
665 }
666
667 const ICounterMappings& GetCounterMappings() const override
668 {
669 return m_CounterMapping;
670 }
671
672 ISendCounterPacket& GetSendCounterPacket() override
673 {
674 return m_SendCounterPacket;
675 }
676
677 bool IsProfilingEnabled() const override
678 {
679 return m_IsProfilingEnabled;
680 }
681
682 CaptureData GetCaptureData() override
683 {
684 CaptureData copy(m_CaptureData);
685 return copy;
686 }
687
688 void RegisterMapping(uint16_t globalCounterId,
689 uint16_t backendCounterId,
Cathal Corbett6f073722022-03-04 12:11:09 +0000690 const std::string& backendId)
Sadik Armagancab588a2020-02-17 11:33:31 +0000691 {
692 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
693 }
694
Sadik Armagan3184c902020-03-18 10:57:30 +0000695 void Reset()
Sadik Armagancab588a2020-02-17 11:33:31 +0000696 {
697 m_CounterMapping.Reset();
698 }
699
700private:
701 ProfilingGuidGenerator m_GuidGenerator;
Keith Davis33ed2212020-03-30 10:43:41 +0100702 CounterIdMap m_CounterMapping;
703 SendCounterPacket m_SendCounterPacket;
704 bool m_IsProfilingEnabled;
705 CaptureData m_CaptureData;
Sadik Armagancab588a2020-02-17 11:33:31 +0000706};
707
Jim Flynn6398a982020-05-27 17:05:21 +0100708class MockProfilingServiceStatus : public IProfilingServiceStatus
709{
710public:
711 void NotifyProfilingServiceActive() override {}
Jim Flynn9265a882022-03-10 23:35:26 +0000712 void WaitForProfilingServiceActivation(unsigned int timeout) override { arm::pipe::IgnoreUnused(timeout); }
Jim Flynn6398a982020-05-27 17:05:21 +0100713};
714
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000715} // namespace pipe
Jim Flynn64063552020-02-14 10:18:08 +0000716
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000717} // namespace arm