blob: fb829e5b31bf41421293917c493a1888372f26cb [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
Jim Flynn3e9bc192022-03-23 23:01:26 +00008#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>
Jim Flynn64063552020-02-14 10:18:08 +000013
Jim Flynn9c85b412022-03-16 00:27:43 +000014#include <armnn/BackendId.hpp>
15#include <armnn/profiling/ArmNNProfiling.hpp>
16
Jim Flynn27761832022-03-20 21:52:17 +000017#include <client/include/Holder.hpp>
18#include <client/include/IProfilingServiceStatus.hpp>
19
Jim Flynn6730fe92022-03-10 22:57:47 +000020#include <common/include/Assert.hpp>
Jim Flynnc454ac92022-03-16 18:43:18 +000021#include <common/include/CommonProfilingUtils.hpp>
Jim Flynn9265a882022-03-10 23:35:26 +000022#include <common/include/IgnoreUnused.hpp>
Jim Flynn75c14f42022-03-10 22:05:42 +000023#include <common/include/NumericCast.hpp>
Jim Flynndecd08b2022-03-13 22:35:46 +000024#include <common/include/Optional.hpp>
Jim Flynnf9db3ef2022-03-08 21:23:44 +000025#include <common/include/ProfilingException.hpp>
Nikhil Raj5b1bcc92021-06-08 12:31:50 +010026#include <common/include/ProfilingGuidGenerator.hpp>
Nikhil Raj7dcc6972021-04-30 15:44:24 +010027
Jim Flynn64063552020-02-14 10:18:08 +000028#include <atomic>
29#include <condition_variable>
30#include <mutex>
31#include <thread>
32
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000033namespace arm
Jim Flynn64063552020-02-14 10:18:08 +000034{
35
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000036namespace pipe
Jim Flynn64063552020-02-14 10:18:08 +000037{
38
39class MockProfilingConnection : public IProfilingConnection
40{
41public:
42 MockProfilingConnection()
43 : m_IsOpen(true)
44 , m_WrittenData()
45 , m_Packet()
46 {}
47
48 enum class PacketType
49 {
50 StreamMetaData,
51 ConnectionAcknowledge,
52 CounterDirectory,
53 ReqCounterDirectory,
54 PeriodicCounterSelection,
55 PerJobCounterSelection,
56 TimelineMessageDirectory,
57 PeriodicCounterCapture,
Keith Davis33ed2212020-03-30 10:43:41 +010058 ActivateTimelineReporting,
59 DeactivateTimelineReporting,
Jim Flynn64063552020-02-14 10:18:08 +000060 Unknown
61 };
62
63 bool IsOpen() const override
64 {
65 std::lock_guard<std::mutex> lock(m_Mutex);
66
67 return m_IsOpen;
68 }
69
70 void Close() override
71 {
72 std::lock_guard<std::mutex> lock(m_Mutex);
73
74 m_IsOpen = false;
75 }
76
77 bool WritePacket(const unsigned char* buffer, uint32_t length) override
78 {
79 if (buffer == nullptr || length == 0)
80 {
81 return false;
82 }
83
84 uint32_t header = ReadUint32(buffer, 0);
85
86 uint32_t packetFamily = (header >> 26);
87 uint32_t packetId = ((header >> 16) & 1023);
88
89 PacketType packetType;
90
91 switch (packetFamily)
92 {
93 case 0:
Keith Davis33ed2212020-03-30 10:43:41 +010094 packetType = packetId < 8 ? PacketType(packetId) : PacketType::Unknown;
Jim Flynn64063552020-02-14 10:18:08 +000095 break;
96 case 1:
97 packetType = packetId == 0 ? PacketType::TimelineMessageDirectory : PacketType::Unknown;
98 break;
99 case 3:
100 packetType = packetId == 0 ? PacketType::PeriodicCounterCapture : PacketType::Unknown;
101 break;
102 default:
103 packetType = PacketType::Unknown;
104 }
105
106 std::lock_guard<std::mutex> lock(m_Mutex);
107
108 m_WrittenData.push_back({ packetType, length });
109 return true;
110 }
111
112 long CheckForPacket(const std::pair<PacketType, uint32_t> packetInfo)
113 {
114 std::lock_guard<std::mutex> lock(m_Mutex);
115
116 if(packetInfo.second != 0)
117 {
Rob Hughesbb46dde2020-05-20 15:27:37 +0100118 return static_cast<long>(std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo));
Jim Flynn64063552020-02-14 10:18:08 +0000119 }
120 else
121 {
Rob Hughesbb46dde2020-05-20 15:27:37 +0100122 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; }));
Jim Flynn64063552020-02-14 10:18:08 +0000124 }
125 }
126
Jim Flynnbbfe6032020-07-20 16:57:44 +0100127 bool WritePacket(arm::pipe::Packet&& packet)
Jim Flynn64063552020-02-14 10:18:08 +0000128 {
129 std::lock_guard<std::mutex> lock(m_Mutex);
130
131 m_Packet = std::move(packet);
132 return true;
133 }
134
Jim Flynnbbfe6032020-07-20 16:57:44 +0100135 arm::pipe::Packet ReadPacket(uint32_t timeout) override
Jim Flynn64063552020-02-14 10:18:08 +0000136 {
Jim Flynn9265a882022-03-10 23:35:26 +0000137 arm::pipe::IgnoreUnused(timeout);
Jim Flynn64063552020-02-14 10:18:08 +0000138
139 // Simulate a delay in the reading process. The default timeout is way too long.
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);
143 }
144
145 unsigned long GetWrittenDataSize()
146 {
147 std::lock_guard<std::mutex> lock(m_Mutex);
148
Rob Hughesbb46dde2020-05-20 15:27:37 +0100149 return static_cast<unsigned long>(m_WrittenData.size());
Jim Flynn64063552020-02-14 10:18:08 +0000150 }
151
152 void Clear()
153 {
154 std::lock_guard<std::mutex> lock(m_Mutex);
155
156 m_WrittenData.clear();
157 }
158
159private:
160 bool m_IsOpen;
161 std::vector<std::pair<PacketType, uint32_t>> m_WrittenData;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100162 arm::pipe::Packet m_Packet;
Jim Flynn64063552020-02-14 10:18:08 +0000163 mutable std::mutex m_Mutex;
164};
165
166class MockProfilingConnectionFactory : public IProfilingConnectionFactory
167{
168public:
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000169 IProfilingConnectionPtr GetProfilingConnection(const ProfilingOptions& options) const override
Jim Flynn64063552020-02-14 10:18:08 +0000170 {
Jim Flynn9265a882022-03-10 23:35:26 +0000171 arm::pipe::IgnoreUnused(options);
Jim Flynn64063552020-02-14 10:18:08 +0000172 return std::make_unique<MockProfilingConnection>();
173 }
174};
175
176class MockPacketBuffer : public IPacketBuffer
177{
178public:
179 MockPacketBuffer(unsigned int maxSize)
180 : m_MaxSize(maxSize)
181 , m_Size(0)
182 , m_Data(std::make_unique<unsigned char[]>(m_MaxSize))
183 {}
184
185 ~MockPacketBuffer() {}
186
187 const unsigned char* GetReadableData() const override { return m_Data.get(); }
188
189 unsigned int GetSize() const override { return m_Size; }
190
191 void MarkRead() override { m_Size = 0; }
192
193 void Commit(unsigned int size) override { m_Size = size; }
194
195 void Release() override { m_Size = 0; }
196
197 unsigned char* GetWritableData() override { return m_Data.get(); }
198
Jim Flynn0204f092020-06-22 20:41:43 +0100199 void Destroy() override {m_Data.reset(nullptr); m_Size = 0; m_MaxSize =0;}
200
Jim Flynn64063552020-02-14 10:18:08 +0000201private:
202 unsigned int m_MaxSize;
203 unsigned int m_Size;
204 std::unique_ptr<unsigned char[]> m_Data;
205};
206
207class MockBufferManager : public IBufferManager
208{
209public:
210 MockBufferManager(unsigned int size)
211 : m_BufferSize(size),
212 m_Buffer(std::make_unique<MockPacketBuffer>(size)) {}
213
214 ~MockBufferManager() {}
215
216 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
217 {
218 if (requestedSize > m_BufferSize)
219 {
220 reservedSize = m_BufferSize;
221 }
222 else
223 {
224 reservedSize = requestedSize;
225 }
226
227 return std::move(m_Buffer);
228 }
229
230 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override
231 {
232 packetBuffer->Commit(size);
233 m_Buffer = std::move(packetBuffer);
234
235 if (notifyConsumer)
236 {
237 FlushReadList();
238 }
239 }
240
241 IPacketBufferPtr GetReadableBuffer() override
242 {
243 return std::move(m_Buffer);
244 }
245
246 void Release(IPacketBufferPtr& packetBuffer) override
247 {
248 packetBuffer->Release();
249 m_Buffer = std::move(packetBuffer);
250 }
251
252 void MarkRead(IPacketBufferPtr& packetBuffer) override
253 {
254 packetBuffer->MarkRead();
255 m_Buffer = std::move(packetBuffer);
256 }
257
258 void SetConsumer(IConsumer* consumer) override
259 {
260 if (consumer != nullptr)
261 {
262 m_Consumer = consumer;
263 }
264 }
265
266 void FlushReadList() override
267 {
268 // notify consumer that packet is ready to read
269 if (m_Consumer != nullptr)
270 {
271 m_Consumer->SetReadyToRead();
272 }
273 }
274
275private:
276 unsigned int m_BufferSize;
277 IPacketBufferPtr m_Buffer;
278 IConsumer* m_Consumer = nullptr;
279};
280
281class MockStreamCounterBuffer : public IBufferManager
282{
283public:
284 MockStreamCounterBuffer(unsigned int maxBufferSize = 4096)
285 : m_MaxBufferSize(maxBufferSize)
286 , m_BufferList()
287 , m_CommittedSize(0)
288 , m_ReadableSize(0)
289 , m_ReadSize(0)
290 {}
291 ~MockStreamCounterBuffer() {}
292
293 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
294 {
295 std::lock_guard<std::mutex> lock(m_Mutex);
296
297 reservedSize = 0;
298 if (requestedSize > m_MaxBufferSize)
299 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000300 throw arm::pipe::InvalidArgumentException("The maximum buffer size that can be requested is [" +
301 std::to_string(m_MaxBufferSize) + "] bytes");
Jim Flynn64063552020-02-14 10:18:08 +0000302 }
303 reservedSize = requestedSize;
304 return std::make_unique<MockPacketBuffer>(requestedSize);
305 }
306
307 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override
308 {
309 std::lock_guard<std::mutex> lock(m_Mutex);
310
311 packetBuffer->Commit(size);
312 m_BufferList.push_back(std::move(packetBuffer));
313 m_CommittedSize += size;
314
315 if (notifyConsumer)
316 {
317 FlushReadList();
318 }
319 }
320
321 void Release(IPacketBufferPtr& packetBuffer) override
322 {
323 std::lock_guard<std::mutex> lock(m_Mutex);
324
325 packetBuffer->Release();
326 }
327
328 IPacketBufferPtr GetReadableBuffer() override
329 {
330 std::lock_guard<std::mutex> lock(m_Mutex);
331
332 if (m_BufferList.empty())
333 {
334 return nullptr;
335 }
336 IPacketBufferPtr buffer = std::move(m_BufferList.back());
337 m_BufferList.pop_back();
338 m_ReadableSize += buffer->GetSize();
339 return buffer;
340 }
341
342 void MarkRead(IPacketBufferPtr& packetBuffer) override
343 {
344 std::lock_guard<std::mutex> lock(m_Mutex);
345
346 m_ReadSize += packetBuffer->GetSize();
347 packetBuffer->MarkRead();
348 }
349
350 void SetConsumer(IConsumer* consumer) override
351 {
352 if (consumer != nullptr)
353 {
354 m_Consumer = consumer;
355 }
356 }
357
358 void FlushReadList() override
359 {
360 // notify consumer that packet is ready to read
361 if (m_Consumer != nullptr)
362 {
363 m_Consumer->SetReadyToRead();
364 }
365 }
366
367 unsigned int GetCommittedSize() const { return m_CommittedSize; }
368 unsigned int GetReadableSize() const { return m_ReadableSize; }
369 unsigned int GetReadSize() const { return m_ReadSize; }
370
371private:
372 // The maximum buffer size when creating a new buffer
373 unsigned int m_MaxBufferSize;
374
375 // A list of buffers
376 std::vector<IPacketBufferPtr> m_BufferList;
377
378 // The mutex to synchronize this mock's methods
379 std::mutex m_Mutex;
380
381 // The total size of the buffers that has been committed for reading
382 unsigned int m_CommittedSize;
383
384 // The total size of the buffers that can be read
385 unsigned int m_ReadableSize;
386
387 // The total size of the buffers that has already been read
388 unsigned int m_ReadSize;
389
390 // Consumer thread to notify packet is ready to read
391 IConsumer* m_Consumer = nullptr;
392};
393
394class MockSendCounterPacket : public ISendCounterPacket
395{
396public:
397 MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {}
398
399 void SendStreamMetaDataPacket() override
400 {
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);
406 }
407
408 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override
409 {
Jim Flynn9265a882022-03-10 23:35:26 +0000410 arm::pipe::IgnoreUnused(counterDirectory);
Jim Flynn64063552020-02-14 10:18:08 +0000411
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);
417 }
418
419 void SendPeriodicCounterCapturePacket(uint64_t timestamp,
420 const std::vector<CounterValue>& values) override
421 {
Jim Flynn9265a882022-03-10 23:35:26 +0000422 arm::pipe::IgnoreUnused(timestamp, values);
Jim Flynn64063552020-02-14 10:18:08 +0000423
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);
429 }
430
431 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
432 const std::vector<uint16_t>& selectedCounterIds) override
433 {
Jim Flynn9265a882022-03-10 23:35:26 +0000434 arm::pipe::IgnoreUnused(capturePeriod, selectedCounterIds);
Jim Flynn64063552020-02-14 10:18:08 +0000435
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);
441 }
442
443private:
444 IBufferManager& m_BufferManager;
445};
446
447class MockCounterDirectory : public ICounterDirectory
448{
449public:
450 MockCounterDirectory() = default;
451 ~MockCounterDirectory() = default;
452
453 // Register profiling objects
Sadik Armagan4c998992020-02-25 12:44:44 +0000454 const Category* RegisterCategory(const std::string& categoryName)
Jim Flynn64063552020-02-14 10:18:08 +0000455 {
Jim Flynn64063552020-02-14 10:18:08 +0000456 // Create the category
Sadik Armagan4c998992020-02-25 12:44:44 +0000457 CategoryPtr category = std::make_unique<Category>(categoryName);
Jim Flynn6730fe92022-03-10 22:57:47 +0000458 ARM_PIPE_ASSERT(category);
Jim Flynn64063552020-02-14 10:18:08 +0000459
460 // Get the raw category pointer
461 const Category* categoryPtr = category.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000462 ARM_PIPE_ASSERT(categoryPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000463
464 // Register the category
465 m_Categories.insert(std::move(category));
466
467 return categoryPtr;
468 }
469
470 const Device* RegisterDevice(const std::string& deviceName,
Sadik Armagan4c998992020-02-25 12:44:44 +0000471 uint16_t cores = 0)
Jim Flynn64063552020-02-14 10:18:08 +0000472 {
473 // Get the device UID
474 uint16_t deviceUid = GetNextUid();
475
476 // Create the device
477 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
Jim Flynn6730fe92022-03-10 22:57:47 +0000478 ARM_PIPE_ASSERT(device);
Jim Flynn64063552020-02-14 10:18:08 +0000479
480 // Get the raw device pointer
481 const Device* devicePtr = device.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000482 ARM_PIPE_ASSERT(devicePtr);
Jim Flynn64063552020-02-14 10:18:08 +0000483
484 // Register the device
485 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
486
Jim Flynn64063552020-02-14 10:18:08 +0000487 return devicePtr;
488 }
489
490 const CounterSet* RegisterCounterSet(
491 const std::string& counterSetName,
Sadik Armagan4c998992020-02-25 12:44:44 +0000492 uint16_t count = 0)
Jim Flynn64063552020-02-14 10:18:08 +0000493 {
494 // Get the counter set UID
495 uint16_t counterSetUid = GetNextUid();
496
497 // Create the counter set
498 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
Jim Flynn6730fe92022-03-10 22:57:47 +0000499 ARM_PIPE_ASSERT(counterSet);
Jim Flynn64063552020-02-14 10:18:08 +0000500
501 // Get the raw counter set pointer
502 const CounterSet* counterSetPtr = counterSet.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000503 ARM_PIPE_ASSERT(counterSetPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000504
505 // Register the counter set
506 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
507
Jim Flynn64063552020-02-14 10:18:08 +0000508 return counterSetPtr;
509 }
510
Cathal Corbett6f073722022-03-04 12:11:09 +0000511 const Counter* RegisterCounter(const std::string& backendId,
Jim Flynn64063552020-02-14 10:18:08 +0000512 const uint16_t uid,
513 const std::string& parentCategoryName,
514 uint16_t counterClass,
515 uint16_t interpolation,
516 double multiplier,
517 const std::string& name,
518 const std::string& description,
Jim Flynndecd08b2022-03-13 22:35:46 +0000519 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())
Jim Flynn64063552020-02-14 10:18:08 +0000523 {
Jim Flynn9265a882022-03-10 23:35:26 +0000524 arm::pipe::IgnoreUnused(backendId);
Jim Flynn64063552020-02-14 10:18:08 +0000525
526 // Get the number of cores from the argument only
527 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
528
529 // Get the device UID
530 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
531
532 // Get the counter set UID
533 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
534
535 // Get the counter UIDs and calculate the max counter UID
536 std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores);
Jim Flynn6730fe92022-03-10 22:57:47 +0000537 ARM_PIPE_ASSERT(!counterUids.empty());
Jim Flynn64063552020-02-14 10:18:08 +0000538 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
539
540 // Get the counter units
541 const std::string unitsValue = units.has_value() ? units.value() : "";
542
543 // Create the counter
544 CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
545 counterUids.front(),
546 maxCounterUid,
547 counterClass,
548 interpolation,
549 multiplier,
550 name,
551 description,
552 unitsValue,
553 deviceUidValue,
554 counterSetUidValue);
Jim Flynn6730fe92022-03-10 22:57:47 +0000555 ARM_PIPE_ASSERT(counter);
Jim Flynn64063552020-02-14 10:18:08 +0000556
557 // Get the raw counter pointer
558 const Counter* counterPtr = counter.get();
Jim Flynn6730fe92022-03-10 22:57:47 +0000559 ARM_PIPE_ASSERT(counterPtr);
Jim Flynn64063552020-02-14 10:18:08 +0000560
561 // Process multiple counters if necessary
562 for (uint16_t counterUid : counterUids)
563 {
564 // Connect the counter to the parent category
565 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName));
Jim Flynn6730fe92022-03-10 22:57:47 +0000566 ARM_PIPE_ASSERT(parentCategory);
Jim Flynn64063552020-02-14 10:18:08 +0000567 parentCategory->m_Counters.push_back(counterUid);
568
569 // Register the counter
570 m_Counters.insert(std::make_pair(counterUid, counter));
571 }
572
573 return counterPtr;
574 }
575
576 // Getters for counts
Jim Flynn75c14f42022-03-10 22:05:42 +0000577 uint16_t GetCategoryCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Categories.size()); }
578 uint16_t GetDeviceCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Devices.size()); }
579 uint16_t GetCounterSetCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_CounterSets.size()); }
580 uint16_t GetCounterCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Counters.size()); }
Jim Flynn64063552020-02-14 10:18:08 +0000581
582 // Getters for collections
583 const Categories& GetCategories() const override { return m_Categories; }
584 const Devices& GetDevices() const override { return m_Devices; }
585 const CounterSets& GetCounterSets() const override { return m_CounterSets; }
586 const Counters& GetCounters() const override { return m_Counters; }
587
588 // Getters for profiling objects
589 const Category* GetCategory(const std::string& name) const override
590 {
591 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](const CategoryPtr& category)
592 {
Jim Flynn6730fe92022-03-10 22:57:47 +0000593 ARM_PIPE_ASSERT(category);
Jim Flynn64063552020-02-14 10:18:08 +0000594
595 return category->m_Name == name;
596 });
597
598 if (it == m_Categories.end())
599 {
600 return nullptr;
601 }
602
603 return it->get();
604 }
605
606 const Device* GetDevice(uint16_t uid) const override
607 {
Jim Flynn9265a882022-03-10 23:35:26 +0000608 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000609 return nullptr; // Not used by the unit tests
610 }
611
612 const CounterSet* GetCounterSet(uint16_t uid) const override
613 {
Jim Flynn9265a882022-03-10 23:35:26 +0000614 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000615 return nullptr; // Not used by the unit tests
616 }
617
618 const Counter* GetCounter(uint16_t uid) const override
619 {
Jim Flynn9265a882022-03-10 23:35:26 +0000620 arm::pipe::IgnoreUnused(uid);
Jim Flynn64063552020-02-14 10:18:08 +0000621 return nullptr; // Not used by the unit tests
622 }
623
624private:
625 Categories m_Categories;
626 Devices m_Devices;
627 CounterSets m_CounterSets;
628 Counters m_Counters;
629};
630
Sadik Armagan3184c902020-03-18 10:57:30 +0000631class MockProfilingService : public ProfilingService
Sadik Armagancab588a2020-02-17 11:33:31 +0000632{
633public:
Jim Flynn34430252022-03-04 15:03:58 +0000634 MockProfilingService(uint16_t maxGlobalCounterId,
635 IInitialiseProfilingService& initialiser,
636 MockBufferManager& mockBufferManager,
Sadik Armagancab588a2020-02-17 11:33:31 +0000637 bool isProfilingEnabled,
638 const CaptureData& captureData) :
Jim Flynn9c85b412022-03-16 00:27:43 +0000639 ProfilingService(maxGlobalCounterId,
640 initialiser,
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),
Sadik Armagancab588a2020-02-17 11:33:31 +0000648 m_IsProfilingEnabled(isProfilingEnabled),
Keith Davis33ed2212020-03-30 10:43:41 +0100649 m_CaptureData(captureData)
650 {}
Sadik Armagancab588a2020-02-17 11:33:31 +0000651
652 /// Return the next random Guid in the sequence
653 ProfilingDynamicGuid NextGuid() override
654 {
655 return m_GuidGenerator.NextGuid();
656 }
657
658 /// Create a ProfilingStaticGuid based on a hash of the string
659 ProfilingStaticGuid GenerateStaticId(const std::string& str) override
660 {
661 return m_GuidGenerator.GenerateStaticId(str);
662 }
663
664 std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const override
665 {
666 return nullptr;
667 }
668
669 const ICounterMappings& GetCounterMappings() const override
670 {
671 return m_CounterMapping;
672 }
673
674 ISendCounterPacket& GetSendCounterPacket() override
675 {
676 return m_SendCounterPacket;
677 }
678
679 bool IsProfilingEnabled() const override
680 {
681 return m_IsProfilingEnabled;
682 }
683
684 CaptureData GetCaptureData() override
685 {
686 CaptureData copy(m_CaptureData);
687 return copy;
688 }
689
690 void RegisterMapping(uint16_t globalCounterId,
691 uint16_t backendCounterId,
Cathal Corbett6f073722022-03-04 12:11:09 +0000692 const std::string& backendId)
Sadik Armagancab588a2020-02-17 11:33:31 +0000693 {
694 m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId);
695 }
696
Sadik Armagan3184c902020-03-18 10:57:30 +0000697 void Reset()
Sadik Armagancab588a2020-02-17 11:33:31 +0000698 {
699 m_CounterMapping.Reset();
700 }
701
702private:
703 ProfilingGuidGenerator m_GuidGenerator;
Keith Davis33ed2212020-03-30 10:43:41 +0100704 CounterIdMap m_CounterMapping;
705 SendCounterPacket m_SendCounterPacket;
706 bool m_IsProfilingEnabled;
707 CaptureData m_CaptureData;
Sadik Armagancab588a2020-02-17 11:33:31 +0000708};
709
Jim Flynn6398a982020-05-27 17:05:21 +0100710class MockProfilingServiceStatus : public IProfilingServiceStatus
711{
712public:
713 void NotifyProfilingServiceActive() override {}
Jim Flynn9265a882022-03-10 23:35:26 +0000714 void WaitForProfilingServiceActivation(unsigned int timeout) override { arm::pipe::IgnoreUnused(timeout); }
Jim Flynn6398a982020-05-27 17:05:21 +0100715};
716
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000717} // namespace pipe
Jim Flynn64063552020-02-14 10:18:08 +0000718
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000719} // namespace arm