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 | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 15 | #include <boost/assert.hpp> |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 16 | #include <boost/numeric/conversion/cast.hpp> |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 17 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 18 | namespace armnn |
| 19 | { |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 20 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 21 | namespace profiling |
| 22 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 23 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 24 | class MockProfilingConnection : public IProfilingConnection |
| 25 | { |
| 26 | public: |
| 27 | MockProfilingConnection() |
| 28 | : m_IsOpen(true) |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 29 | , m_WrittenData() |
| 30 | , m_Packet() |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 31 | {} |
| 32 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 33 | bool IsOpen() const override |
| 34 | { |
| 35 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 36 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 37 | 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 Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 46 | |
| 47 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 48 | { |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 49 | if (buffer == nullptr || length == 0) |
| 50 | { |
| 51 | return false; |
| 52 | } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 53 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 54 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 55 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 56 | m_WrittenData.push_back(length); |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 57 | return true; |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 58 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 59 | bool WritePacket(Packet&& packet) |
| 60 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 61 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 62 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 63 | m_Packet = std::move(packet); |
| 64 | return true; |
| 65 | } |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 66 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 67 | Packet ReadPacket(uint32_t timeout) override |
| 68 | { |
| 69 | // Simulate a delay in the reading process |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 70 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 71 | |
| 72 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 73 | |
| 74 | return std::move(m_Packet); |
| 75 | } |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 76 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 77 | const std::vector<uint32_t> GetWrittenData() const |
| 78 | { |
| 79 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 80 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 81 | 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 Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | bool m_IsOpen; |
| 93 | std::vector<uint32_t> m_WrittenData; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 94 | Packet m_Packet; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 95 | mutable std::mutex m_Mutex; |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 96 | }; |
| 97 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 98 | class MockPacketBuffer : public IPacketBuffer |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 99 | { |
| 100 | public: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 101 | MockPacketBuffer(unsigned int maxSize) |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 102 | : m_MaxSize(maxSize) |
| 103 | , m_Size(0) |
| 104 | , m_Data(std::make_unique<unsigned char[]>(m_MaxSize)) |
| 105 | {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 106 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 107 | ~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 Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 113 | void MarkRead() override { m_Size = 0; } |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 114 | |
| 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 | |
| 121 | private: |
| 122 | unsigned int m_MaxSize; |
| 123 | unsigned int m_Size; |
| 124 | std::unique_ptr<unsigned char[]> m_Data; |
| 125 | }; |
| 126 | |
| 127 | class MockBufferManager : public IBufferManager |
| 128 | { |
| 129 | public: |
| 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 137 | { |
| 138 | if (requestedSize > m_BufferSize) |
| 139 | { |
| 140 | reservedSize = m_BufferSize; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | reservedSize = requestedSize; |
| 145 | } |
| 146 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 147 | return std::move(m_Buffer); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 150 | void Commit(std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int size) override |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 151 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 152 | packetBuffer->Commit(size); |
| 153 | m_Buffer = std::move(packetBuffer); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 156 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 172 | |
| 173 | private: |
| 174 | unsigned int m_BufferSize; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 175 | std::unique_ptr<IPacketBuffer> m_Buffer; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 176 | }; |
| 177 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 178 | class MockStreamCounterBuffer : public IBufferManager |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 179 | { |
| 180 | public: |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 181 | using IPacketBufferPtr = std::unique_ptr<IPacketBuffer>; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 182 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 183 | 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 190 | ~MockStreamCounterBuffer() {} |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 191 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 192 | IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 193 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 194 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 195 | |
| 196 | reservedSize = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 197 | if (requestedSize > m_MaxBufferSize) |
Narumol Prangnawarat | 7be47ef | 2019-09-27 18:00:11 +0100 | [diff] [blame] | 198 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 199 | throw armnn::InvalidArgumentException("The maximum buffer size that can be requested is [" + |
| 200 | std::to_string(m_MaxBufferSize) + "] bytes"); |
Narumol Prangnawarat | 7be47ef | 2019-09-27 18:00:11 +0100 | [diff] [blame] | 201 | } |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 202 | reservedSize = requestedSize; |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 203 | return std::make_unique<MockPacketBuffer>(requestedSize); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 206 | void Commit(IPacketBufferPtr& packetBuffer, unsigned int size) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 207 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 208 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 209 | |
| 210 | packetBuffer->Commit(size); |
| 211 | m_BufferList.push_back(std::move(packetBuffer)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 212 | m_CommittedSize += size; |
| 213 | } |
| 214 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 215 | void Release(IPacketBufferPtr& packetBuffer) override |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 216 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 217 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 218 | |
| 219 | packetBuffer->Release(); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 222 | IPacketBufferPtr GetReadableBuffer() override |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 223 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 224 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 225 | |
| 226 | if (m_BufferList.empty()) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 227 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 228 | return nullptr; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 229 | } |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 230 | IPacketBufferPtr buffer = std::move(m_BufferList.back()); |
| 231 | m_BufferList.pop_back(); |
| 232 | m_ReadableSize += buffer->GetSize(); |
| 233 | return buffer; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 236 | void MarkRead(IPacketBufferPtr& packetBuffer) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 237 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame^] | 238 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 239 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 240 | m_ReadSize += packetBuffer->GetSize(); |
| 241 | packetBuffer->MarkRead(); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 244 | unsigned int GetCommittedSize() const { return m_CommittedSize; } |
| 245 | unsigned int GetReadableSize() const { return m_ReadableSize; } |
| 246 | unsigned int GetReadSize() const { return m_ReadSize; } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 247 | |
| 248 | private: |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 249 | // The maximum buffer size when creating a new buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 250 | unsigned int m_MaxBufferSize; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 251 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 252 | // A list of buffers |
| 253 | std::vector<IPacketBufferPtr> m_BufferList; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 254 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 255 | // 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 259 | unsigned int m_CommittedSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 260 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 261 | // 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 265 | unsigned int m_ReadSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 266 | }; |
| 267 | |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 268 | class MockSendCounterPacket : public ISendCounterPacket |
| 269 | { |
| 270 | public: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 271 | MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 272 | |
| 273 | void SendStreamMetaDataPacket() override |
| 274 | { |
| 275 | std::string message("SendStreamMetaDataPacket"); |
| 276 | unsigned int reserved = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 277 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 282 | void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 283 | { |
| 284 | std::string message("SendCounterDirectoryPacket"); |
| 285 | unsigned int reserved = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 286 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 289 | } |
| 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 296 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 299 | } |
| 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 306 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 311 | void SetReadyToRead() override {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 312 | |
| 313 | private: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 314 | IBufferManager& m_BufferManager; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 315 | }; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 316 | |
| 317 | class MockCounterDirectory : public ICounterDirectory |
| 318 | { |
| 319 | public: |
| 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 | |
| 514 | private: |
| 515 | Categories m_Categories; |
| 516 | Devices m_Devices; |
| 517 | CounterSets m_CounterSets; |
| 518 | Counters m_Counters; |
| 519 | }; |
| 520 | |
| 521 | class SendCounterPacketTest : public SendCounterPacket |
| 522 | { |
| 523 | public: |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 524 | SendCounterPacketTest(ProfilingStateMachine& profilingStateMachine, IBufferManager& buffer) |
| 525 | : SendCounterPacket(profilingStateMachine, buffer) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 526 | {} |
| 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 Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 557 | |
| 558 | } // namespace profiling |
| 559 | |
| 560 | } // namespace armnn |