blob: 73fc39b437a5549124b7ba78d3912e9b556762a2 [file] [log] [blame]
Ferran Balaguer1b941722019-08-28 16:57:18 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Matteo Martincigh24e8f922019-09-19 11:57:46 +01008#include <SendCounterPacket.hpp>
9#include <ProfilingUtils.hpp>
Matteo Martincighe8485382019-10-10 14:08:21 +010010#include <IProfilingConnectionFactory.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010011
12#include <armnn/Exceptions.hpp>
Matteo Martincigh24e8f922019-09-19 11:57:46 +010013#include <armnn/Optional.hpp>
14#include <armnn/Conversion.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010015
Matteo Martincighd0613b52019-10-09 16:47:04 +010016#include <boost/assert.hpp>
Matteo Martincigh24e8f922019-09-19 11:57:46 +010017#include <boost/numeric/conversion/cast.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010018
Matteo Martincigh24e8f922019-09-19 11:57:46 +010019namespace armnn
20{
Ferran Balaguer1b941722019-08-28 16:57:18 +010021
Matteo Martincigh24e8f922019-09-19 11:57:46 +010022namespace profiling
23{
Matteo Martincighd0613b52019-10-09 16:47:04 +010024
Matteo Martincigh24e8f922019-09-19 11:57:46 +010025class MockProfilingConnection : public IProfilingConnection
26{
27public:
28 MockProfilingConnection()
29 : m_IsOpen(true)
Matteo Martincigh54fb9572019-10-02 12:50:57 +010030 , m_WrittenData()
31 , m_Packet()
Matteo Martincigh24e8f922019-09-19 11:57:46 +010032 {}
33
Matteo Martincighd0613b52019-10-09 16:47:04 +010034 bool IsOpen() const override
35 {
36 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh24e8f922019-09-19 11:57:46 +010037
Matteo Martincighd0613b52019-10-09 16:47:04 +010038 return m_IsOpen;
39 }
40
41 void Close() override
42 {
43 std::lock_guard<std::mutex> lock(m_Mutex);
44
45 m_IsOpen = false;
46 }
Matteo Martincigh24e8f922019-09-19 11:57:46 +010047
48 bool WritePacket(const unsigned char* buffer, uint32_t length) override
49 {
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010050 if (buffer == nullptr || length == 0)
51 {
52 return false;
53 }
Matteo Martincigh24e8f922019-09-19 11:57:46 +010054
Matteo Martincighd0613b52019-10-09 16:47:04 +010055 std::lock_guard<std::mutex> lock(m_Mutex);
56
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010057 m_WrittenData.push_back(length);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010058 return true;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010059 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +010060 bool WritePacket(Packet&& packet)
61 {
Matteo Martincighd0613b52019-10-09 16:47:04 +010062 std::lock_guard<std::mutex> lock(m_Mutex);
63
Matteo Martincigh54fb9572019-10-02 12:50:57 +010064 m_Packet = std::move(packet);
65 return true;
66 }
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010067
Matteo Martincigh54fb9572019-10-02 12:50:57 +010068 Packet ReadPacket(uint32_t timeout) override
69 {
70 // Simulate a delay in the reading process
Matteo Martincighd0613b52019-10-09 16:47:04 +010071 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
72
73 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh54fb9572019-10-02 12:50:57 +010074
75 return std::move(m_Packet);
76 }
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010077
Matteo Martincighe8485382019-10-10 14:08:21 +010078 const std::vector<uint32_t> GetWrittenData()
Matteo Martincighd0613b52019-10-09 16:47:04 +010079 {
80 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010081
Matteo Martincighe8485382019-10-10 14:08:21 +010082 std::vector<uint32_t> writtenData = m_WrittenData;
83 m_WrittenData.clear();
84 return writtenData;
Matteo Martincighd0613b52019-10-09 16:47:04 +010085 }
86
87 void Clear()
88 {
89 std::lock_guard<std::mutex> lock(m_Mutex);
90
91 m_WrittenData.clear();
92 }
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010093
94private:
95 bool m_IsOpen;
96 std::vector<uint32_t> m_WrittenData;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010097 Packet m_Packet;
Matteo Martincighd0613b52019-10-09 16:47:04 +010098 mutable std::mutex m_Mutex;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010099};
100
Matteo Martincighe8485382019-10-10 14:08:21 +0100101class MockProfilingConnectionFactory : public IProfilingConnectionFactory
102{
103public:
104 IProfilingConnectionPtr GetProfilingConnection(const ExternalProfilingOptions& options) const override
105 {
106 return std::make_unique<MockProfilingConnection>();
107 }
108};
109
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100110class MockPacketBuffer : public IPacketBuffer
Ferran Balaguer1b941722019-08-28 16:57:18 +0100111{
112public:
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100113 MockPacketBuffer(unsigned int maxSize)
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100114 : m_MaxSize(maxSize)
115 , m_Size(0)
116 , m_Data(std::make_unique<unsigned char[]>(m_MaxSize))
117 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +0100118
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100119 ~MockPacketBuffer() {}
120
121 const unsigned char* const GetReadableData() const override { return m_Data.get(); }
122
123 unsigned int GetSize() const override { return m_Size; }
124
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100125 void MarkRead() override { m_Size = 0; }
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100126
127 void Commit(unsigned int size) override { m_Size = size; }
128
129 void Release() override { m_Size = 0; }
130
131 unsigned char* GetWritableData() override { return m_Data.get(); }
132
133private:
134 unsigned int m_MaxSize;
135 unsigned int m_Size;
136 std::unique_ptr<unsigned char[]> m_Data;
137};
138
139class MockBufferManager : public IBufferManager
140{
141public:
142 MockBufferManager(unsigned int size)
143 : m_BufferSize(size),
144 m_Buffer(std::make_unique<MockPacketBuffer>(size)) {}
145
146 ~MockBufferManager() {}
147
148 std::unique_ptr<IPacketBuffer> Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
Ferran Balaguer1b941722019-08-28 16:57:18 +0100149 {
150 if (requestedSize > m_BufferSize)
151 {
152 reservedSize = m_BufferSize;
153 }
154 else
155 {
156 reservedSize = requestedSize;
157 }
158
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100159 return std::move(m_Buffer);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100160 }
161
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100162 void Commit(std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int size) override
Ferran Balaguer1b941722019-08-28 16:57:18 +0100163 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100164 packetBuffer->Commit(size);
165 m_Buffer = std::move(packetBuffer);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100166 }
167
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100168 std::unique_ptr<IPacketBuffer> GetReadableBuffer() override
169 {
170 return std::move(m_Buffer);
171 }
172
173 void Release(std::unique_ptr<IPacketBuffer>& packetBuffer) override
174 {
175 packetBuffer->Release();
176 m_Buffer = std::move(packetBuffer);
177 }
178
179 void MarkRead(std::unique_ptr<IPacketBuffer>& packetBuffer) override
180 {
181 packetBuffer->MarkRead();
182 m_Buffer = std::move(packetBuffer);
183 }
Ferran Balaguer1b941722019-08-28 16:57:18 +0100184
185private:
186 unsigned int m_BufferSize;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100187 std::unique_ptr<IPacketBuffer> m_Buffer;
Ferran Balaguer1b941722019-08-28 16:57:18 +0100188};
189
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100190class MockStreamCounterBuffer : public IBufferManager
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100191{
192public:
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100193 using IPacketBufferPtr = std::unique_ptr<IPacketBuffer>;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100194
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100195 MockStreamCounterBuffer(unsigned int maxBufferSize = 4096)
196 : m_MaxBufferSize(maxBufferSize)
197 , m_BufferList()
198 , m_CommittedSize(0)
199 , m_ReadableSize(0)
200 , m_ReadSize(0)
201 {}
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100202 ~MockStreamCounterBuffer() {}
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100203
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100204 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100205 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100206 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100207
208 reservedSize = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100209 if (requestedSize > m_MaxBufferSize)
Narumol Prangnawarat7be47ef2019-09-27 18:00:11 +0100210 {
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100211 throw armnn::InvalidArgumentException("The maximum buffer size that can be requested is [" +
212 std::to_string(m_MaxBufferSize) + "] bytes");
Narumol Prangnawarat7be47ef2019-09-27 18:00:11 +0100213 }
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100214 reservedSize = requestedSize;
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100215 return std::make_unique<MockPacketBuffer>(requestedSize);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100216 }
217
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100218 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size) override
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100219 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100220 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100221
222 packetBuffer->Commit(size);
223 m_BufferList.push_back(std::move(packetBuffer));
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100224 m_CommittedSize += size;
225 }
226
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100227 void Release(IPacketBufferPtr& packetBuffer) override
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100228 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100229 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100230
231 packetBuffer->Release();
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100232 }
233
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100234 IPacketBufferPtr GetReadableBuffer() override
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100235 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100236 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100237
238 if (m_BufferList.empty())
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100239 {
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100240 return nullptr;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100241 }
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100242 IPacketBufferPtr buffer = std::move(m_BufferList.back());
243 m_BufferList.pop_back();
244 m_ReadableSize += buffer->GetSize();
245 return buffer;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100246 }
247
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100248 void MarkRead(IPacketBufferPtr& packetBuffer) override
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100249 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100250 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100251
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100252 m_ReadSize += packetBuffer->GetSize();
253 packetBuffer->MarkRead();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100254 }
255
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100256 unsigned int GetCommittedSize() const { return m_CommittedSize; }
257 unsigned int GetReadableSize() const { return m_ReadableSize; }
258 unsigned int GetReadSize() const { return m_ReadSize; }
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100259
260private:
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100261 // The maximum buffer size when creating a new buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100262 unsigned int m_MaxBufferSize;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100263
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100264 // A list of buffers
265 std::vector<IPacketBufferPtr> m_BufferList;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100266
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100267 // The mutex to synchronize this mock's methods
268 std::mutex m_Mutex;
269
270 // The total size of the buffers that has been committed for reading
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100271 unsigned int m_CommittedSize;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100272
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100273 // The total size of the buffers that can be read
274 unsigned int m_ReadableSize;
275
276 // The total size of the buffers that has already been read
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100277 unsigned int m_ReadSize;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100278};
279
Ferran Balaguer1b941722019-08-28 16:57:18 +0100280class MockSendCounterPacket : public ISendCounterPacket
281{
282public:
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100283 MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {}
Ferran Balaguer1b941722019-08-28 16:57:18 +0100284
285 void SendStreamMetaDataPacket() override
286 {
287 std::string message("SendStreamMetaDataPacket");
288 unsigned int reserved = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100289 std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
290 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
291 m_BufferManager.Commit(buffer, reserved);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100292 }
293
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100294 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override
Ferran Balaguer1b941722019-08-28 16:57:18 +0100295 {
296 std::string message("SendCounterDirectoryPacket");
297 unsigned int reserved = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100298 std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
299 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
300 m_BufferManager.Commit(buffer, reserved);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100301 }
302
303 void SendPeriodicCounterCapturePacket(uint64_t timestamp,
304 const std::vector<std::pair<uint16_t, uint32_t>>& values) override
305 {
306 std::string message("SendPeriodicCounterCapturePacket");
307 unsigned int reserved = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100308 std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
309 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
310 m_BufferManager.Commit(buffer, reserved);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100311 }
312
313 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
314 const std::vector<uint16_t>& selectedCounterIds) override
315 {
316 std::string message("SendPeriodicCounterSelectionPacket");
317 unsigned int reserved = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100318 std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
319 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
320 m_BufferManager.Commit(buffer, reserved);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100321 }
322
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100323 void SetReadyToRead() override {}
Ferran Balaguer1b941722019-08-28 16:57:18 +0100324
325private:
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100326 IBufferManager& m_BufferManager;
Ferran Balaguer1b941722019-08-28 16:57:18 +0100327};
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100328
329class MockCounterDirectory : public ICounterDirectory
330{
331public:
332 MockCounterDirectory() = default;
333 ~MockCounterDirectory() = default;
334
335 // Register profiling objects
336 const Category* RegisterCategory(const std::string& categoryName,
337 const armnn::Optional<uint16_t>& deviceUid = armnn::EmptyOptional(),
338 const armnn::Optional<uint16_t>& counterSetUid = armnn::EmptyOptional())
339 {
340 // Get the device UID
341 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
342
343 // Get the counter set UID
344 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
345
346 // Create the category
347 CategoryPtr category = std::make_unique<Category>(categoryName, deviceUidValue, counterSetUidValue);
348 BOOST_ASSERT(category);
349
350 // Get the raw category pointer
351 const Category* categoryPtr = category.get();
352 BOOST_ASSERT(categoryPtr);
353
354 // Register the category
355 m_Categories.insert(std::move(category));
356
357 return categoryPtr;
358 }
359
360 const Device* RegisterDevice(const std::string& deviceName,
361 uint16_t cores = 0,
362 const armnn::Optional<std::string>& parentCategoryName = armnn::EmptyOptional())
363 {
364 // Get the device UID
365 uint16_t deviceUid = GetNextUid();
366
367 // Create the device
368 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
369 BOOST_ASSERT(device);
370
371 // Get the raw device pointer
372 const Device* devicePtr = device.get();
373 BOOST_ASSERT(devicePtr);
374
375 // Register the device
376 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
377
378 // Connect the counter set to the parent category, if required
379 if (parentCategoryName.has_value())
380 {
381 // Set the counter set UID in the parent category
382 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName.value()));
383 BOOST_ASSERT(parentCategory);
384 parentCategory->m_DeviceUid = deviceUid;
385 }
386
387 return devicePtr;
388 }
389
390 const CounterSet* RegisterCounterSet(
391 const std::string& counterSetName,
392 uint16_t count = 0,
393 const armnn::Optional<std::string>& parentCategoryName = armnn::EmptyOptional())
394 {
395 // Get the counter set UID
396 uint16_t counterSetUid = GetNextUid();
397
398 // Create the counter set
399 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
400 BOOST_ASSERT(counterSet);
401
402 // Get the raw counter set pointer
403 const CounterSet* counterSetPtr = counterSet.get();
404 BOOST_ASSERT(counterSetPtr);
405
406 // Register the counter set
407 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
408
409 // Connect the counter set to the parent category, if required
410 if (parentCategoryName.has_value())
411 {
412 // Set the counter set UID in the parent category
413 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName.value()));
414 BOOST_ASSERT(parentCategory);
415 parentCategory->m_CounterSetUid = counterSetUid;
416 }
417
418 return counterSetPtr;
419 }
420
421 const Counter* RegisterCounter(const std::string& parentCategoryName,
422 uint16_t counterClass,
423 uint16_t interpolation,
424 double multiplier,
425 const std::string& name,
426 const std::string& description,
427 const armnn::Optional<std::string>& units = armnn::EmptyOptional(),
428 const armnn::Optional<uint16_t>& numberOfCores = armnn::EmptyOptional(),
429 const armnn::Optional<uint16_t>& deviceUid = armnn::EmptyOptional(),
430 const armnn::Optional<uint16_t>& counterSetUid = armnn::EmptyOptional())
431 {
432 // Get the number of cores from the argument only
433 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
434
435 // Get the device UID
436 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
437
438 // Get the counter set UID
439 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
440
441 // Get the counter UIDs and calculate the max counter UID
442 std::vector<uint16_t> counterUids = GetNextCounterUids(deviceCores);
443 BOOST_ASSERT(!counterUids.empty());
444 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
445
446 // Get the counter units
447 const std::string unitsValue = units.has_value() ? units.value() : "";
448
449 // Create the counter
450 CounterPtr counter = std::make_shared<Counter>(counterUids.front(),
451 maxCounterUid,
452 counterClass,
453 interpolation,
454 multiplier,
455 name,
456 description,
457 unitsValue,
458 deviceUidValue,
459 counterSetUidValue);
460 BOOST_ASSERT(counter);
461
462 // Get the raw counter pointer
463 const Counter* counterPtr = counter.get();
464 BOOST_ASSERT(counterPtr);
465
466 // Process multiple counters if necessary
467 for (uint16_t counterUid : counterUids)
468 {
469 // Connect the counter to the parent category
470 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName));
471 BOOST_ASSERT(parentCategory);
472 parentCategory->m_Counters.push_back(counterUid);
473
474 // Register the counter
475 m_Counters.insert(std::make_pair(counterUid, counter));
476 }
477
478 return counterPtr;
479 }
480
481 // Getters for counts
482 uint16_t GetCategoryCount() const override { return boost::numeric_cast<uint16_t>(m_Categories.size()); }
483 uint16_t GetDeviceCount() const override { return boost::numeric_cast<uint16_t>(m_Devices.size()); }
484 uint16_t GetCounterSetCount() const override { return boost::numeric_cast<uint16_t>(m_CounterSets.size()); }
485 uint16_t GetCounterCount() const override { return boost::numeric_cast<uint16_t>(m_Counters.size()); }
486
487 // Getters for collections
488 const Categories& GetCategories() const override { return m_Categories; }
489 const Devices& GetDevices() const override { return m_Devices; }
490 const CounterSets& GetCounterSets() const override { return m_CounterSets; }
491 const Counters& GetCounters() const override { return m_Counters; }
492
493 // Getters for profiling objects
494 const Category* GetCategory(const std::string& name) const override
495 {
496 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](const CategoryPtr& category)
497 {
498 BOOST_ASSERT(category);
499
500 return category->m_Name == name;
501 });
502
503 if (it == m_Categories.end())
504 {
505 return nullptr;
506 }
507
508 return it->get();
509 }
510
511 const Device* GetDevice(uint16_t uid) const override
512 {
513 return nullptr; // Not used by the unit tests
514 }
515
516 const CounterSet* GetCounterSet(uint16_t uid) const override
517 {
518 return nullptr; // Not used by the unit tests
519 }
520
521 const Counter* GetCounter(uint16_t uid) const override
522 {
523 return nullptr; // Not used by the unit tests
524 }
525
526private:
527 Categories m_Categories;
528 Devices m_Devices;
529 CounterSets m_CounterSets;
530 Counters m_Counters;
531};
532
533class SendCounterPacketTest : public SendCounterPacket
534{
535public:
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100536 SendCounterPacketTest(ProfilingStateMachine& profilingStateMachine, IBufferManager& buffer)
537 : SendCounterPacket(profilingStateMachine, buffer)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100538 {}
539
540 bool CreateDeviceRecordTest(const DevicePtr& device,
541 DeviceRecord& deviceRecord,
542 std::string& errorMessage)
543 {
544 return CreateDeviceRecord(device, deviceRecord, errorMessage);
545 }
546
547 bool CreateCounterSetRecordTest(const CounterSetPtr& counterSet,
548 CounterSetRecord& counterSetRecord,
549 std::string& errorMessage)
550 {
551 return CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage);
552 }
553
554 bool CreateEventRecordTest(const CounterPtr& counter,
555 EventRecord& eventRecord,
556 std::string& errorMessage)
557 {
558 return CreateEventRecord(counter, eventRecord, errorMessage);
559 }
560
561 bool CreateCategoryRecordTest(const CategoryPtr& category,
562 const Counters& counters,
563 CategoryRecord& categoryRecord,
564 std::string& errorMessage)
565 {
566 return CreateCategoryRecord(category, counters, categoryRecord, errorMessage);
567 }
568};
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100569
570} // namespace profiling
571
572} // namespace armnn