Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 8 | #include <SendCounterPacket.hpp> |
| 9 | #include <ProfilingUtils.hpp> |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 10 | |
| 11 | #include <armnn/Exceptions.hpp> |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 12 | #include <armnn/Optional.hpp> |
| 13 | #include <armnn/Conversion.hpp> |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 14 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 15 | #include <boost/numeric/conversion/cast.hpp> |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 16 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 17 | namespace armnn |
| 18 | { |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 19 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 20 | namespace profiling |
| 21 | { |
| 22 | |
| 23 | class MockProfilingConnection : public IProfilingConnection |
| 24 | { |
| 25 | public: |
| 26 | MockProfilingConnection() |
| 27 | : m_IsOpen(true) |
| 28 | {} |
| 29 | |
| 30 | bool IsOpen() override { return m_IsOpen; } |
| 31 | |
| 32 | void Close() override { m_IsOpen = false; } |
| 33 | |
| 34 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 35 | { |
| 36 | return buffer != nullptr && length > 0; |
| 37 | } |
| 38 | |
| 39 | Packet ReadPacket(uint32_t timeout) override { return Packet(); } |
| 40 | |
| 41 | private: |
| 42 | bool m_IsOpen; |
| 43 | }; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 44 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 45 | class MockWriteProfilingConnection : public IProfilingConnection |
| 46 | { |
| 47 | public: |
| 48 | MockWriteProfilingConnection() |
| 49 | : m_IsOpen(true) |
| 50 | {} |
| 51 | |
| 52 | bool IsOpen() override { return m_IsOpen; } |
| 53 | |
| 54 | void Close() override { m_IsOpen = false; } |
| 55 | |
| 56 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 57 | { |
| 58 | m_WrittenData.push_back(length); |
| 59 | return buffer != nullptr && length > 0; |
| 60 | } |
| 61 | |
| 62 | Packet ReadPacket(uint32_t timeout) override { return Packet(); } |
| 63 | |
| 64 | std::vector<uint32_t> GetWrittenData() |
| 65 | { |
| 66 | return m_WrittenData; |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | bool m_IsOpen; |
| 71 | std::vector<uint32_t> m_WrittenData; |
| 72 | }; |
| 73 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 74 | class MockPacketBuffer : public IPacketBuffer |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 75 | { |
| 76 | public: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 77 | MockPacketBuffer(unsigned int maxSize) |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 78 | : m_MaxSize(maxSize) |
| 79 | , m_Size(0) |
| 80 | , m_Data(std::make_unique<unsigned char[]>(m_MaxSize)) |
| 81 | {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 82 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 83 | ~MockPacketBuffer() {} |
| 84 | |
| 85 | const unsigned char* const GetReadableData() const override { return m_Data.get(); } |
| 86 | |
| 87 | unsigned int GetSize() const override { return m_Size; } |
| 88 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 89 | void MarkRead() override { m_Size = 0; } |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 90 | |
| 91 | void Commit(unsigned int size) override { m_Size = size; } |
| 92 | |
| 93 | void Release() override { m_Size = 0; } |
| 94 | |
| 95 | unsigned char* GetWritableData() override { return m_Data.get(); } |
| 96 | |
| 97 | private: |
| 98 | unsigned int m_MaxSize; |
| 99 | unsigned int m_Size; |
| 100 | std::unique_ptr<unsigned char[]> m_Data; |
| 101 | }; |
| 102 | |
| 103 | class MockBufferManager : public IBufferManager |
| 104 | { |
| 105 | public: |
| 106 | MockBufferManager(unsigned int size) |
| 107 | : m_BufferSize(size), |
| 108 | m_Buffer(std::make_unique<MockPacketBuffer>(size)) {} |
| 109 | |
| 110 | ~MockBufferManager() {} |
| 111 | |
| 112 | std::unique_ptr<IPacketBuffer> Reserve(unsigned int requestedSize, unsigned int& reservedSize) override |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 113 | { |
| 114 | if (requestedSize > m_BufferSize) |
| 115 | { |
| 116 | reservedSize = m_BufferSize; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | reservedSize = requestedSize; |
| 121 | } |
| 122 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 123 | return std::move(m_Buffer); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 126 | void Commit(std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int size) override |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 127 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 128 | packetBuffer->Commit(size); |
| 129 | m_Buffer = std::move(packetBuffer); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 132 | std::unique_ptr<IPacketBuffer> GetReadableBuffer() override |
| 133 | { |
| 134 | return std::move(m_Buffer); |
| 135 | } |
| 136 | |
| 137 | void Release(std::unique_ptr<IPacketBuffer>& packetBuffer) override |
| 138 | { |
| 139 | packetBuffer->Release(); |
| 140 | m_Buffer = std::move(packetBuffer); |
| 141 | } |
| 142 | |
| 143 | void MarkRead(std::unique_ptr<IPacketBuffer>& packetBuffer) override |
| 144 | { |
| 145 | packetBuffer->MarkRead(); |
| 146 | m_Buffer = std::move(packetBuffer); |
| 147 | } |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | unsigned int m_BufferSize; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 151 | std::unique_ptr<IPacketBuffer> m_Buffer; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 154 | class MockStreamCounterBuffer : public IBufferManager |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 155 | { |
| 156 | public: |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 157 | using IPacketBufferPtr = std::unique_ptr<IPacketBuffer>; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 158 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 159 | MockStreamCounterBuffer(unsigned int maxBufferSize = 4096) |
| 160 | : m_MaxBufferSize(maxBufferSize) |
| 161 | , m_BufferList() |
| 162 | , m_CommittedSize(0) |
| 163 | , m_ReadableSize(0) |
| 164 | , m_ReadSize(0) |
| 165 | {} |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 166 | ~MockStreamCounterBuffer() {} |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 167 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 168 | IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 169 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 170 | std::unique_lock<std::mutex> lock(m_Mutex); |
| 171 | |
| 172 | reservedSize = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 173 | if (requestedSize > m_MaxBufferSize) |
Narumol Prangnawarat | 7be47ef | 2019-09-27 18:00:11 +0100 | [diff] [blame] | 174 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 175 | throw armnn::InvalidArgumentException("The maximum buffer size that can be requested is [" + |
| 176 | std::to_string(m_MaxBufferSize) + "] bytes"); |
Narumol Prangnawarat | 7be47ef | 2019-09-27 18:00:11 +0100 | [diff] [blame] | 177 | } |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 178 | reservedSize = requestedSize; |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 179 | return std::make_unique<MockPacketBuffer>(requestedSize); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 182 | void Commit(IPacketBufferPtr& packetBuffer, unsigned int size) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 183 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 184 | std::unique_lock<std::mutex> lock(m_Mutex); |
| 185 | |
| 186 | packetBuffer->Commit(size); |
| 187 | m_BufferList.push_back(std::move(packetBuffer)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 188 | m_CommittedSize += size; |
| 189 | } |
| 190 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 191 | void Release(IPacketBufferPtr& packetBuffer) override |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 192 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 193 | std::unique_lock<std::mutex> lock(m_Mutex); |
| 194 | |
| 195 | packetBuffer->Release(); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 198 | IPacketBufferPtr GetReadableBuffer() override |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 199 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 200 | std::unique_lock<std::mutex> lock(m_Mutex); |
| 201 | |
| 202 | if (m_BufferList.empty()) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 203 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 204 | return nullptr; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 205 | } |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 206 | IPacketBufferPtr buffer = std::move(m_BufferList.back()); |
| 207 | m_BufferList.pop_back(); |
| 208 | m_ReadableSize += buffer->GetSize(); |
| 209 | return buffer; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 212 | void MarkRead(IPacketBufferPtr& packetBuffer) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 213 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 214 | std::unique_lock<std::mutex> lock(m_Mutex); |
| 215 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 216 | m_ReadSize += packetBuffer->GetSize(); |
| 217 | packetBuffer->MarkRead(); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 220 | unsigned int GetCommittedSize() const { return m_CommittedSize; } |
| 221 | unsigned int GetReadableSize() const { return m_ReadableSize; } |
| 222 | unsigned int GetReadSize() const { return m_ReadSize; } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 223 | |
| 224 | private: |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 225 | // The maximum buffer size when creating a new buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 226 | unsigned int m_MaxBufferSize; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 227 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 228 | // A list of buffers |
| 229 | std::vector<IPacketBufferPtr> m_BufferList; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 230 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 231 | // The mutex to synchronize this mock's methods |
| 232 | std::mutex m_Mutex; |
| 233 | |
| 234 | // The total size of the buffers that has been committed for reading |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 235 | unsigned int m_CommittedSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 236 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 237 | // The total size of the buffers that can be read |
| 238 | unsigned int m_ReadableSize; |
| 239 | |
| 240 | // The total size of the buffers that has already been read |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 241 | unsigned int m_ReadSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 242 | }; |
| 243 | |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 244 | class MockSendCounterPacket : public ISendCounterPacket |
| 245 | { |
| 246 | public: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 247 | MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 248 | |
| 249 | void SendStreamMetaDataPacket() override |
| 250 | { |
| 251 | std::string message("SendStreamMetaDataPacket"); |
| 252 | unsigned int reserved = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 253 | std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved); |
| 254 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 255 | m_BufferManager.Commit(buffer, reserved); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 258 | void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 259 | { |
| 260 | std::string message("SendCounterDirectoryPacket"); |
| 261 | unsigned int reserved = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 262 | std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved); |
| 263 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 264 | m_BufferManager.Commit(buffer, reserved); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void SendPeriodicCounterCapturePacket(uint64_t timestamp, |
| 268 | const std::vector<std::pair<uint16_t, uint32_t>>& values) override |
| 269 | { |
| 270 | std::string message("SendPeriodicCounterCapturePacket"); |
| 271 | unsigned int reserved = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 272 | std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved); |
| 273 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 274 | m_BufferManager.Commit(buffer, reserved); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 278 | const std::vector<uint16_t>& selectedCounterIds) override |
| 279 | { |
| 280 | std::string message("SendPeriodicCounterSelectionPacket"); |
| 281 | unsigned int reserved = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 282 | std::unique_ptr<IPacketBuffer> buffer = m_BufferManager.Reserve(1024, reserved); |
| 283 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 284 | m_BufferManager.Commit(buffer, reserved); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 287 | void SetReadyToRead() override {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 288 | |
| 289 | private: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 290 | IBufferManager& m_BufferManager; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 291 | }; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 292 | |
| 293 | class MockCounterDirectory : public ICounterDirectory |
| 294 | { |
| 295 | public: |
| 296 | MockCounterDirectory() = default; |
| 297 | ~MockCounterDirectory() = default; |
| 298 | |
| 299 | // Register profiling objects |
| 300 | const Category* RegisterCategory(const std::string& categoryName, |
| 301 | const armnn::Optional<uint16_t>& deviceUid = armnn::EmptyOptional(), |
| 302 | const armnn::Optional<uint16_t>& counterSetUid = armnn::EmptyOptional()) |
| 303 | { |
| 304 | // Get the device UID |
| 305 | uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0; |
| 306 | |
| 307 | // Get the counter set UID |
| 308 | uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0; |
| 309 | |
| 310 | // Create the category |
| 311 | CategoryPtr category = std::make_unique<Category>(categoryName, deviceUidValue, counterSetUidValue); |
| 312 | BOOST_ASSERT(category); |
| 313 | |
| 314 | // Get the raw category pointer |
| 315 | const Category* categoryPtr = category.get(); |
| 316 | BOOST_ASSERT(categoryPtr); |
| 317 | |
| 318 | // Register the category |
| 319 | m_Categories.insert(std::move(category)); |
| 320 | |
| 321 | return categoryPtr; |
| 322 | } |
| 323 | |
| 324 | const Device* RegisterDevice(const std::string& deviceName, |
| 325 | uint16_t cores = 0, |
| 326 | const armnn::Optional<std::string>& parentCategoryName = armnn::EmptyOptional()) |
| 327 | { |
| 328 | // Get the device UID |
| 329 | uint16_t deviceUid = GetNextUid(); |
| 330 | |
| 331 | // Create the device |
| 332 | DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores); |
| 333 | BOOST_ASSERT(device); |
| 334 | |
| 335 | // Get the raw device pointer |
| 336 | const Device* devicePtr = device.get(); |
| 337 | BOOST_ASSERT(devicePtr); |
| 338 | |
| 339 | // Register the device |
| 340 | m_Devices.insert(std::make_pair(deviceUid, std::move(device))); |
| 341 | |
| 342 | // Connect the counter set to the parent category, if required |
| 343 | if (parentCategoryName.has_value()) |
| 344 | { |
| 345 | // Set the counter set UID in the parent category |
| 346 | Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName.value())); |
| 347 | BOOST_ASSERT(parentCategory); |
| 348 | parentCategory->m_DeviceUid = deviceUid; |
| 349 | } |
| 350 | |
| 351 | return devicePtr; |
| 352 | } |
| 353 | |
| 354 | const CounterSet* RegisterCounterSet( |
| 355 | const std::string& counterSetName, |
| 356 | uint16_t count = 0, |
| 357 | const armnn::Optional<std::string>& parentCategoryName = armnn::EmptyOptional()) |
| 358 | { |
| 359 | // Get the counter set UID |
| 360 | uint16_t counterSetUid = GetNextUid(); |
| 361 | |
| 362 | // Create the counter set |
| 363 | CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count); |
| 364 | BOOST_ASSERT(counterSet); |
| 365 | |
| 366 | // Get the raw counter set pointer |
| 367 | const CounterSet* counterSetPtr = counterSet.get(); |
| 368 | BOOST_ASSERT(counterSetPtr); |
| 369 | |
| 370 | // Register the counter set |
| 371 | m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet))); |
| 372 | |
| 373 | // Connect the counter set to the parent category, if required |
| 374 | if (parentCategoryName.has_value()) |
| 375 | { |
| 376 | // Set the counter set UID in the parent category |
| 377 | Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName.value())); |
| 378 | BOOST_ASSERT(parentCategory); |
| 379 | parentCategory->m_CounterSetUid = counterSetUid; |
| 380 | } |
| 381 | |
| 382 | return counterSetPtr; |
| 383 | } |
| 384 | |
| 385 | const Counter* RegisterCounter(const std::string& parentCategoryName, |
| 386 | uint16_t counterClass, |
| 387 | uint16_t interpolation, |
| 388 | double multiplier, |
| 389 | const std::string& name, |
| 390 | const std::string& description, |
| 391 | const armnn::Optional<std::string>& units = armnn::EmptyOptional(), |
| 392 | const armnn::Optional<uint16_t>& numberOfCores = armnn::EmptyOptional(), |
| 393 | const armnn::Optional<uint16_t>& deviceUid = armnn::EmptyOptional(), |
| 394 | const armnn::Optional<uint16_t>& counterSetUid = armnn::EmptyOptional()) |
| 395 | { |
| 396 | // Get the number of cores from the argument only |
| 397 | uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0; |
| 398 | |
| 399 | // Get the device UID |
| 400 | uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0; |
| 401 | |
| 402 | // Get the counter set UID |
| 403 | uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0; |
| 404 | |
| 405 | // Get the counter UIDs and calculate the max counter UID |
| 406 | std::vector<uint16_t> counterUids = GetNextCounterUids(deviceCores); |
| 407 | BOOST_ASSERT(!counterUids.empty()); |
| 408 | uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back(); |
| 409 | |
| 410 | // Get the counter units |
| 411 | const std::string unitsValue = units.has_value() ? units.value() : ""; |
| 412 | |
| 413 | // Create the counter |
| 414 | CounterPtr counter = std::make_shared<Counter>(counterUids.front(), |
| 415 | maxCounterUid, |
| 416 | counterClass, |
| 417 | interpolation, |
| 418 | multiplier, |
| 419 | name, |
| 420 | description, |
| 421 | unitsValue, |
| 422 | deviceUidValue, |
| 423 | counterSetUidValue); |
| 424 | BOOST_ASSERT(counter); |
| 425 | |
| 426 | // Get the raw counter pointer |
| 427 | const Counter* counterPtr = counter.get(); |
| 428 | BOOST_ASSERT(counterPtr); |
| 429 | |
| 430 | // Process multiple counters if necessary |
| 431 | for (uint16_t counterUid : counterUids) |
| 432 | { |
| 433 | // Connect the counter to the parent category |
| 434 | Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName)); |
| 435 | BOOST_ASSERT(parentCategory); |
| 436 | parentCategory->m_Counters.push_back(counterUid); |
| 437 | |
| 438 | // Register the counter |
| 439 | m_Counters.insert(std::make_pair(counterUid, counter)); |
| 440 | } |
| 441 | |
| 442 | return counterPtr; |
| 443 | } |
| 444 | |
| 445 | // Getters for counts |
| 446 | uint16_t GetCategoryCount() const override { return boost::numeric_cast<uint16_t>(m_Categories.size()); } |
| 447 | uint16_t GetDeviceCount() const override { return boost::numeric_cast<uint16_t>(m_Devices.size()); } |
| 448 | uint16_t GetCounterSetCount() const override { return boost::numeric_cast<uint16_t>(m_CounterSets.size()); } |
| 449 | uint16_t GetCounterCount() const override { return boost::numeric_cast<uint16_t>(m_Counters.size()); } |
| 450 | |
| 451 | // Getters for collections |
| 452 | const Categories& GetCategories() const override { return m_Categories; } |
| 453 | const Devices& GetDevices() const override { return m_Devices; } |
| 454 | const CounterSets& GetCounterSets() const override { return m_CounterSets; } |
| 455 | const Counters& GetCounters() const override { return m_Counters; } |
| 456 | |
| 457 | // Getters for profiling objects |
| 458 | const Category* GetCategory(const std::string& name) const override |
| 459 | { |
| 460 | auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](const CategoryPtr& category) |
| 461 | { |
| 462 | BOOST_ASSERT(category); |
| 463 | |
| 464 | return category->m_Name == name; |
| 465 | }); |
| 466 | |
| 467 | if (it == m_Categories.end()) |
| 468 | { |
| 469 | return nullptr; |
| 470 | } |
| 471 | |
| 472 | return it->get(); |
| 473 | } |
| 474 | |
| 475 | const Device* GetDevice(uint16_t uid) const override |
| 476 | { |
| 477 | return nullptr; // Not used by the unit tests |
| 478 | } |
| 479 | |
| 480 | const CounterSet* GetCounterSet(uint16_t uid) const override |
| 481 | { |
| 482 | return nullptr; // Not used by the unit tests |
| 483 | } |
| 484 | |
| 485 | const Counter* GetCounter(uint16_t uid) const override |
| 486 | { |
| 487 | return nullptr; // Not used by the unit tests |
| 488 | } |
| 489 | |
| 490 | private: |
| 491 | Categories m_Categories; |
| 492 | Devices m_Devices; |
| 493 | CounterSets m_CounterSets; |
| 494 | Counters m_Counters; |
| 495 | }; |
| 496 | |
| 497 | class SendCounterPacketTest : public SendCounterPacket |
| 498 | { |
| 499 | public: |
Matteo Martincigh | e61ffd0 | 2019-10-07 10:19:35 +0100 | [diff] [blame] | 500 | SendCounterPacketTest(IBufferManager& buffer) |
| 501 | : SendCounterPacket(buffer) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 502 | {} |
| 503 | |
| 504 | bool CreateDeviceRecordTest(const DevicePtr& device, |
| 505 | DeviceRecord& deviceRecord, |
| 506 | std::string& errorMessage) |
| 507 | { |
| 508 | return CreateDeviceRecord(device, deviceRecord, errorMessage); |
| 509 | } |
| 510 | |
| 511 | bool CreateCounterSetRecordTest(const CounterSetPtr& counterSet, |
| 512 | CounterSetRecord& counterSetRecord, |
| 513 | std::string& errorMessage) |
| 514 | { |
| 515 | return CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage); |
| 516 | } |
| 517 | |
| 518 | bool CreateEventRecordTest(const CounterPtr& counter, |
| 519 | EventRecord& eventRecord, |
| 520 | std::string& errorMessage) |
| 521 | { |
| 522 | return CreateEventRecord(counter, eventRecord, errorMessage); |
| 523 | } |
| 524 | |
| 525 | bool CreateCategoryRecordTest(const CategoryPtr& category, |
| 526 | const Counters& counters, |
| 527 | CategoryRecord& categoryRecord, |
| 528 | std::string& errorMessage) |
| 529 | { |
| 530 | return CreateCategoryRecord(category, counters, categoryRecord, errorMessage); |
| 531 | } |
| 532 | }; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 533 | |
| 534 | } // namespace profiling |
| 535 | |
| 536 | } // namespace armnn |