blob: 871ca741247d8306fffc2c6dc961e43e79162319 [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>
Ferran Balaguer1b941722019-08-28 16:57:18 +010010
11#include <armnn/Exceptions.hpp>
Matteo Martincigh24e8f922019-09-19 11:57:46 +010012#include <armnn/Optional.hpp>
13#include <armnn/Conversion.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010014
Matteo Martincighd0613b52019-10-09 16:47:04 +010015#include <boost/assert.hpp>
Matteo Martincigh24e8f922019-09-19 11:57:46 +010016#include <boost/numeric/conversion/cast.hpp>
Ferran Balaguer1b941722019-08-28 16:57:18 +010017
Matteo Martincigh24e8f922019-09-19 11:57:46 +010018namespace armnn
19{
Ferran Balaguer1b941722019-08-28 16:57:18 +010020
Matteo Martincigh24e8f922019-09-19 11:57:46 +010021namespace profiling
22{
Matteo Martincighd0613b52019-10-09 16:47:04 +010023
Matteo Martincigh24e8f922019-09-19 11:57:46 +010024class MockProfilingConnection : public IProfilingConnection
25{
26public:
27 MockProfilingConnection()
28 : m_IsOpen(true)
Matteo Martincigh54fb9572019-10-02 12:50:57 +010029 , m_WrittenData()
30 , m_Packet()
Matteo Martincigh24e8f922019-09-19 11:57:46 +010031 {}
32
Matteo Martincighd0613b52019-10-09 16:47:04 +010033 bool IsOpen() const override
34 {
35 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh24e8f922019-09-19 11:57:46 +010036
Matteo Martincighd0613b52019-10-09 16:47:04 +010037 return m_IsOpen;
38 }
39
40 void Close() override
41 {
42 std::lock_guard<std::mutex> lock(m_Mutex);
43
44 m_IsOpen = false;
45 }
Matteo Martincigh24e8f922019-09-19 11:57:46 +010046
47 bool WritePacket(const unsigned char* buffer, uint32_t length) override
48 {
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010049 if (buffer == nullptr || length == 0)
50 {
51 return false;
52 }
Matteo Martincigh24e8f922019-09-19 11:57:46 +010053
Matteo Martincighd0613b52019-10-09 16:47:04 +010054 std::lock_guard<std::mutex> lock(m_Mutex);
55
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010056 m_WrittenData.push_back(length);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010057 return true;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010058 }
Matteo Martincigh54fb9572019-10-02 12:50:57 +010059 bool WritePacket(Packet&& packet)
60 {
Matteo Martincighd0613b52019-10-09 16:47:04 +010061 std::lock_guard<std::mutex> lock(m_Mutex);
62
Matteo Martincigh54fb9572019-10-02 12:50:57 +010063 m_Packet = std::move(packet);
64 return true;
65 }
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010066
Matteo Martincigh54fb9572019-10-02 12:50:57 +010067 Packet ReadPacket(uint32_t timeout) override
68 {
69 // Simulate a delay in the reading process
Matteo Martincighd0613b52019-10-09 16:47:04 +010070 std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
71
72 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh54fb9572019-10-02 12:50:57 +010073
74 return std::move(m_Packet);
75 }
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010076
Matteo Martincighd0613b52019-10-09 16:47:04 +010077 const std::vector<uint32_t> GetWrittenData() const
78 {
79 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010080
Matteo Martincighd0613b52019-10-09 16:47:04 +010081 return m_WrittenData;
82 }
83
84 void Clear()
85 {
86 std::lock_guard<std::mutex> lock(m_Mutex);
87
88 m_WrittenData.clear();
89 }
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010090
91private:
92 bool m_IsOpen;
93 std::vector<uint32_t> m_WrittenData;
Matteo Martincigh54fb9572019-10-02 12:50:57 +010094 Packet m_Packet;
Matteo Martincighd0613b52019-10-09 16:47:04 +010095 mutable std::mutex m_Mutex;
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010096};
97
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010098class MockPacketBuffer : public IPacketBuffer
Ferran Balaguer1b941722019-08-28 16:57:18 +010099{
100public:
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100101 MockPacketBuffer(unsigned int maxSize)
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100102 : m_MaxSize(maxSize)
103 , m_Size(0)
104 , m_Data(std::make_unique<unsigned char[]>(m_MaxSize))
105 {}
Ferran Balaguer1b941722019-08-28 16:57:18 +0100106
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100107 ~MockPacketBuffer() {}
108
109 const unsigned char* const GetReadableData() const override { return m_Data.get(); }
110
111 unsigned int GetSize() const override { return m_Size; }
112
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100113 void MarkRead() override { m_Size = 0; }
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100114
115 void Commit(unsigned int size) override { m_Size = size; }
116
117 void Release() override { m_Size = 0; }
118
119 unsigned char* GetWritableData() override { return m_Data.get(); }
120
121private:
122 unsigned int m_MaxSize;
123 unsigned int m_Size;
124 std::unique_ptr<unsigned char[]> m_Data;
125};
126
127class MockBufferManager : public IBufferManager
128{
129public:
130 MockBufferManager(unsigned int size)
131 : m_BufferSize(size),
132 m_Buffer(std::make_unique<MockPacketBuffer>(size)) {}
133
134 ~MockBufferManager() {}
135
136 std::unique_ptr<IPacketBuffer> Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
Ferran Balaguer1b941722019-08-28 16:57:18 +0100137 {
138 if (requestedSize > m_BufferSize)
139 {
140 reservedSize = m_BufferSize;
141 }
142 else
143 {
144 reservedSize = requestedSize;
145 }
146
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100147 return std::move(m_Buffer);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100148 }
149
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100150 void Commit(std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int size) override
Ferran Balaguer1b941722019-08-28 16:57:18 +0100151 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100152 packetBuffer->Commit(size);
153 m_Buffer = std::move(packetBuffer);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100154 }
155
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100156 std::unique_ptr<IPacketBuffer> GetReadableBuffer() override
157 {
158 return std::move(m_Buffer);
159 }
160
161 void Release(std::unique_ptr<IPacketBuffer>& packetBuffer) override
162 {
163 packetBuffer->Release();
164 m_Buffer = std::move(packetBuffer);
165 }
166
167 void MarkRead(std::unique_ptr<IPacketBuffer>& packetBuffer) override
168 {
169 packetBuffer->MarkRead();
170 m_Buffer = std::move(packetBuffer);
171 }
Ferran Balaguer1b941722019-08-28 16:57:18 +0100172
173private:
174 unsigned int m_BufferSize;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100175 std::unique_ptr<IPacketBuffer> m_Buffer;
Ferran Balaguer1b941722019-08-28 16:57:18 +0100176};
177
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100178class MockStreamCounterBuffer : public IBufferManager
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100179{
180public:
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100181 using IPacketBufferPtr = std::unique_ptr<IPacketBuffer>;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100182
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100183 MockStreamCounterBuffer(unsigned int maxBufferSize = 4096)
184 : m_MaxBufferSize(maxBufferSize)
185 , m_BufferList()
186 , m_CommittedSize(0)
187 , m_ReadableSize(0)
188 , m_ReadSize(0)
189 {}
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100190 ~MockStreamCounterBuffer() {}
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100191
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100192 IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100193 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100194 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100195
196 reservedSize = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100197 if (requestedSize > m_MaxBufferSize)
Narumol Prangnawarat7be47ef2019-09-27 18:00:11 +0100198 {
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100199 throw armnn::InvalidArgumentException("The maximum buffer size that can be requested is [" +
200 std::to_string(m_MaxBufferSize) + "] bytes");
Narumol Prangnawarat7be47ef2019-09-27 18:00:11 +0100201 }
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100202 reservedSize = requestedSize;
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100203 return std::make_unique<MockPacketBuffer>(requestedSize);
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100204 }
205
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100206 void Commit(IPacketBufferPtr& packetBuffer, unsigned int size) override
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100207 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100208 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100209
210 packetBuffer->Commit(size);
211 m_BufferList.push_back(std::move(packetBuffer));
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100212 m_CommittedSize += size;
213 }
214
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100215 void Release(IPacketBufferPtr& packetBuffer) override
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100216 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100217 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100218
219 packetBuffer->Release();
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100220 }
221
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100222 IPacketBufferPtr GetReadableBuffer() override
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100223 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100224 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100225
226 if (m_BufferList.empty())
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100227 {
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100228 return nullptr;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100229 }
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100230 IPacketBufferPtr buffer = std::move(m_BufferList.back());
231 m_BufferList.pop_back();
232 m_ReadableSize += buffer->GetSize();
233 return buffer;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100234 }
235
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100236 void MarkRead(IPacketBufferPtr& packetBuffer) override
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100237 {
Matteo Martincighd0613b52019-10-09 16:47:04 +0100238 std::lock_guard<std::mutex> lock(m_Mutex);
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100239
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100240 m_ReadSize += packetBuffer->GetSize();
241 packetBuffer->MarkRead();
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100242 }
243
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100244 unsigned int GetCommittedSize() const { return m_CommittedSize; }
245 unsigned int GetReadableSize() const { return m_ReadableSize; }
246 unsigned int GetReadSize() const { return m_ReadSize; }
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100247
248private:
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100249 // The maximum buffer size when creating a new buffer
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100250 unsigned int m_MaxBufferSize;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100251
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100252 // A list of buffers
253 std::vector<IPacketBufferPtr> m_BufferList;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100254
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100255 // The mutex to synchronize this mock's methods
256 std::mutex m_Mutex;
257
258 // The total size of the buffers that has been committed for reading
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100259 unsigned int m_CommittedSize;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100260
Matteo Martincigh61d6f732019-10-03 11:21:18 +0100261 // The total size of the buffers that can be read
262 unsigned int m_ReadableSize;
263
264 // The total size of the buffers that has already been read
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100265 unsigned int m_ReadSize;
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100266};
267
Ferran Balaguer1b941722019-08-28 16:57:18 +0100268class MockSendCounterPacket : public ISendCounterPacket
269{
270public:
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100271 MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {}
Ferran Balaguer1b941722019-08-28 16:57:18 +0100272
273 void SendStreamMetaDataPacket() override
274 {
275 std::string message("SendStreamMetaDataPacket");
276 unsigned int reserved = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100277 std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
278 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
279 m_BufferManager.Commit(buffer, reserved);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100280 }
281
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100282 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override
Ferran Balaguer1b941722019-08-28 16:57:18 +0100283 {
284 std::string message("SendCounterDirectoryPacket");
285 unsigned int reserved = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100286 std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
287 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
288 m_BufferManager.Commit(buffer, reserved);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100289 }
290
291 void SendPeriodicCounterCapturePacket(uint64_t timestamp,
292 const std::vector<std::pair<uint16_t, uint32_t>>& values) override
293 {
294 std::string message("SendPeriodicCounterCapturePacket");
295 unsigned int reserved = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100296 std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
297 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
298 m_BufferManager.Commit(buffer, reserved);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100299 }
300
301 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
302 const std::vector<uint16_t>& selectedCounterIds) override
303 {
304 std::string message("SendPeriodicCounterSelectionPacket");
305 unsigned int reserved = 0;
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100306 std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved);
307 memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1);
308 m_BufferManager.Commit(buffer, reserved);
Ferran Balaguer1b941722019-08-28 16:57:18 +0100309 }
310
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100311 void SetReadyToRead() override {}
Ferran Balaguer1b941722019-08-28 16:57:18 +0100312
313private:
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100314 IBufferManager& m_BufferManager;
Ferran Balaguer1b941722019-08-28 16:57:18 +0100315};
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100316
317class MockCounterDirectory : public ICounterDirectory
318{
319public:
320 MockCounterDirectory() = default;
321 ~MockCounterDirectory() = default;
322
323 // Register profiling objects
324 const Category* RegisterCategory(const std::string& categoryName,
325 const armnn::Optional<uint16_t>& deviceUid = armnn::EmptyOptional(),
326 const armnn::Optional<uint16_t>& counterSetUid = armnn::EmptyOptional())
327 {
328 // Get the device UID
329 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
330
331 // Get the counter set UID
332 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
333
334 // Create the category
335 CategoryPtr category = std::make_unique<Category>(categoryName, deviceUidValue, counterSetUidValue);
336 BOOST_ASSERT(category);
337
338 // Get the raw category pointer
339 const Category* categoryPtr = category.get();
340 BOOST_ASSERT(categoryPtr);
341
342 // Register the category
343 m_Categories.insert(std::move(category));
344
345 return categoryPtr;
346 }
347
348 const Device* RegisterDevice(const std::string& deviceName,
349 uint16_t cores = 0,
350 const armnn::Optional<std::string>& parentCategoryName = armnn::EmptyOptional())
351 {
352 // Get the device UID
353 uint16_t deviceUid = GetNextUid();
354
355 // Create the device
356 DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
357 BOOST_ASSERT(device);
358
359 // Get the raw device pointer
360 const Device* devicePtr = device.get();
361 BOOST_ASSERT(devicePtr);
362
363 // Register the device
364 m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
365
366 // Connect the counter set to the parent category, if required
367 if (parentCategoryName.has_value())
368 {
369 // Set the counter set UID in the parent category
370 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName.value()));
371 BOOST_ASSERT(parentCategory);
372 parentCategory->m_DeviceUid = deviceUid;
373 }
374
375 return devicePtr;
376 }
377
378 const CounterSet* RegisterCounterSet(
379 const std::string& counterSetName,
380 uint16_t count = 0,
381 const armnn::Optional<std::string>& parentCategoryName = armnn::EmptyOptional())
382 {
383 // Get the counter set UID
384 uint16_t counterSetUid = GetNextUid();
385
386 // Create the counter set
387 CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
388 BOOST_ASSERT(counterSet);
389
390 // Get the raw counter set pointer
391 const CounterSet* counterSetPtr = counterSet.get();
392 BOOST_ASSERT(counterSetPtr);
393
394 // Register the counter set
395 m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
396
397 // Connect the counter set to the parent category, if required
398 if (parentCategoryName.has_value())
399 {
400 // Set the counter set UID in the parent category
401 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName.value()));
402 BOOST_ASSERT(parentCategory);
403 parentCategory->m_CounterSetUid = counterSetUid;
404 }
405
406 return counterSetPtr;
407 }
408
409 const Counter* RegisterCounter(const std::string& parentCategoryName,
410 uint16_t counterClass,
411 uint16_t interpolation,
412 double multiplier,
413 const std::string& name,
414 const std::string& description,
415 const armnn::Optional<std::string>& units = armnn::EmptyOptional(),
416 const armnn::Optional<uint16_t>& numberOfCores = armnn::EmptyOptional(),
417 const armnn::Optional<uint16_t>& deviceUid = armnn::EmptyOptional(),
418 const armnn::Optional<uint16_t>& counterSetUid = armnn::EmptyOptional())
419 {
420 // Get the number of cores from the argument only
421 uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0;
422
423 // Get the device UID
424 uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
425
426 // Get the counter set UID
427 uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
428
429 // Get the counter UIDs and calculate the max counter UID
430 std::vector<uint16_t> counterUids = GetNextCounterUids(deviceCores);
431 BOOST_ASSERT(!counterUids.empty());
432 uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
433
434 // Get the counter units
435 const std::string unitsValue = units.has_value() ? units.value() : "";
436
437 // Create the counter
438 CounterPtr counter = std::make_shared<Counter>(counterUids.front(),
439 maxCounterUid,
440 counterClass,
441 interpolation,
442 multiplier,
443 name,
444 description,
445 unitsValue,
446 deviceUidValue,
447 counterSetUidValue);
448 BOOST_ASSERT(counter);
449
450 // Get the raw counter pointer
451 const Counter* counterPtr = counter.get();
452 BOOST_ASSERT(counterPtr);
453
454 // Process multiple counters if necessary
455 for (uint16_t counterUid : counterUids)
456 {
457 // Connect the counter to the parent category
458 Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName));
459 BOOST_ASSERT(parentCategory);
460 parentCategory->m_Counters.push_back(counterUid);
461
462 // Register the counter
463 m_Counters.insert(std::make_pair(counterUid, counter));
464 }
465
466 return counterPtr;
467 }
468
469 // Getters for counts
470 uint16_t GetCategoryCount() const override { return boost::numeric_cast<uint16_t>(m_Categories.size()); }
471 uint16_t GetDeviceCount() const override { return boost::numeric_cast<uint16_t>(m_Devices.size()); }
472 uint16_t GetCounterSetCount() const override { return boost::numeric_cast<uint16_t>(m_CounterSets.size()); }
473 uint16_t GetCounterCount() const override { return boost::numeric_cast<uint16_t>(m_Counters.size()); }
474
475 // Getters for collections
476 const Categories& GetCategories() const override { return m_Categories; }
477 const Devices& GetDevices() const override { return m_Devices; }
478 const CounterSets& GetCounterSets() const override { return m_CounterSets; }
479 const Counters& GetCounters() const override { return m_Counters; }
480
481 // Getters for profiling objects
482 const Category* GetCategory(const std::string& name) const override
483 {
484 auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](const CategoryPtr& category)
485 {
486 BOOST_ASSERT(category);
487
488 return category->m_Name == name;
489 });
490
491 if (it == m_Categories.end())
492 {
493 return nullptr;
494 }
495
496 return it->get();
497 }
498
499 const Device* GetDevice(uint16_t uid) const override
500 {
501 return nullptr; // Not used by the unit tests
502 }
503
504 const CounterSet* GetCounterSet(uint16_t uid) const override
505 {
506 return nullptr; // Not used by the unit tests
507 }
508
509 const Counter* GetCounter(uint16_t uid) const override
510 {
511 return nullptr; // Not used by the unit tests
512 }
513
514private:
515 Categories m_Categories;
516 Devices m_Devices;
517 CounterSets m_CounterSets;
518 Counters m_Counters;
519};
520
521class SendCounterPacketTest : public SendCounterPacket
522{
523public:
Matteo Martincigh5d737fb2019-10-07 13:05:13 +0100524 SendCounterPacketTest(ProfilingStateMachine& profilingStateMachine, IBufferManager& buffer)
525 : SendCounterPacket(profilingStateMachine, buffer)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100526 {}
527
528 bool CreateDeviceRecordTest(const DevicePtr& device,
529 DeviceRecord& deviceRecord,
530 std::string& errorMessage)
531 {
532 return CreateDeviceRecord(device, deviceRecord, errorMessage);
533 }
534
535 bool CreateCounterSetRecordTest(const CounterSetPtr& counterSet,
536 CounterSetRecord& counterSetRecord,
537 std::string& errorMessage)
538 {
539 return CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage);
540 }
541
542 bool CreateEventRecordTest(const CounterPtr& counter,
543 EventRecord& eventRecord,
544 std::string& errorMessage)
545 {
546 return CreateEventRecord(counter, eventRecord, errorMessage);
547 }
548
549 bool CreateCategoryRecordTest(const CategoryPtr& category,
550 const Counters& counters,
551 CategoryRecord& categoryRecord,
552 std::string& errorMessage)
553 {
554 return CreateCategoryRecord(category, counters, categoryRecord, errorMessage);
555 }
556};
Matteo Martincigh24e8f922019-09-19 11:57:46 +0100557
558} // namespace profiling
559
560} // namespace armnn