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> |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 10 | #include <IProfilingConnectionFactory.hpp> |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 11 | |
| 12 | #include <armnn/Exceptions.hpp> |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 13 | #include <armnn/Optional.hpp> |
| 14 | #include <armnn/Conversion.hpp> |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 15 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 16 | #include <boost/assert.hpp> |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 17 | #include <boost/numeric/conversion/cast.hpp> |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 18 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 19 | namespace armnn |
| 20 | { |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 21 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 22 | namespace profiling |
| 23 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 24 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 25 | class MockProfilingConnection : public IProfilingConnection |
| 26 | { |
| 27 | public: |
| 28 | MockProfilingConnection() |
| 29 | : m_IsOpen(true) |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 30 | , m_WrittenData() |
| 31 | , m_Packet() |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 32 | {} |
| 33 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 34 | bool IsOpen() const override |
| 35 | { |
| 36 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 37 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 38 | 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 Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 47 | |
| 48 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 49 | { |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 50 | if (buffer == nullptr || length == 0) |
| 51 | { |
| 52 | return false; |
| 53 | } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 54 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 55 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 56 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 57 | m_WrittenData.push_back(length); |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 58 | return true; |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 59 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 60 | bool WritePacket(Packet&& packet) |
| 61 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 62 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 63 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 64 | m_Packet = std::move(packet); |
| 65 | return true; |
| 66 | } |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 67 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 68 | Packet ReadPacket(uint32_t timeout) override |
| 69 | { |
| 70 | // Simulate a delay in the reading process |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 71 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 72 | |
| 73 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 74 | |
| 75 | return std::move(m_Packet); |
| 76 | } |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 77 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 78 | const std::vector<uint32_t> GetWrittenData() |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 79 | { |
| 80 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 81 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 82 | std::vector<uint32_t> writtenData = m_WrittenData; |
| 83 | m_WrittenData.clear(); |
| 84 | return writtenData; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void Clear() |
| 88 | { |
| 89 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 90 | |
| 91 | m_WrittenData.clear(); |
| 92 | } |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 93 | |
| 94 | private: |
| 95 | bool m_IsOpen; |
| 96 | std::vector<uint32_t> m_WrittenData; |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 97 | Packet m_Packet; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 98 | mutable std::mutex m_Mutex; |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 101 | class MockProfilingConnectionFactory : public IProfilingConnectionFactory |
| 102 | { |
| 103 | public: |
| 104 | IProfilingConnectionPtr GetProfilingConnection(const ExternalProfilingOptions& options) const override |
| 105 | { |
| 106 | return std::make_unique<MockProfilingConnection>(); |
| 107 | } |
| 108 | }; |
| 109 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 110 | class MockPacketBuffer : public IPacketBuffer |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 111 | { |
| 112 | public: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 113 | MockPacketBuffer(unsigned int maxSize) |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 114 | : m_MaxSize(maxSize) |
| 115 | , m_Size(0) |
| 116 | , m_Data(std::make_unique<unsigned char[]>(m_MaxSize)) |
| 117 | {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 118 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 119 | ~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 Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 125 | void MarkRead() override { m_Size = 0; } |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 126 | |
| 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 | |
| 133 | private: |
| 134 | unsigned int m_MaxSize; |
| 135 | unsigned int m_Size; |
| 136 | std::unique_ptr<unsigned char[]> m_Data; |
| 137 | }; |
| 138 | |
| 139 | class MockBufferManager : public IBufferManager |
| 140 | { |
| 141 | public: |
| 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 149 | { |
| 150 | if (requestedSize > m_BufferSize) |
| 151 | { |
| 152 | reservedSize = m_BufferSize; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | reservedSize = requestedSize; |
| 157 | } |
| 158 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 159 | return std::move(m_Buffer); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 162 | void Commit(std::unique_ptr<IPacketBuffer>& packetBuffer, unsigned int size) override |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 163 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 164 | packetBuffer->Commit(size); |
| 165 | m_Buffer = std::move(packetBuffer); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 168 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | unsigned int m_BufferSize; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 187 | std::unique_ptr<IPacketBuffer> m_Buffer; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 188 | }; |
| 189 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 190 | class MockStreamCounterBuffer : public IBufferManager |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 191 | { |
| 192 | public: |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 193 | using IPacketBufferPtr = std::unique_ptr<IPacketBuffer>; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 194 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 195 | 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 202 | ~MockStreamCounterBuffer() {} |
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 | IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 205 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 206 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 207 | |
| 208 | reservedSize = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 209 | if (requestedSize > m_MaxBufferSize) |
Narumol Prangnawarat | 7be47ef | 2019-09-27 18:00:11 +0100 | [diff] [blame] | 210 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 211 | throw armnn::InvalidArgumentException("The maximum buffer size that can be requested is [" + |
| 212 | std::to_string(m_MaxBufferSize) + "] bytes"); |
Narumol Prangnawarat | 7be47ef | 2019-09-27 18:00:11 +0100 | [diff] [blame] | 213 | } |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 214 | reservedSize = requestedSize; |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 215 | return std::make_unique<MockPacketBuffer>(requestedSize); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 218 | void Commit(IPacketBufferPtr& packetBuffer, unsigned int size) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 219 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 220 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 221 | |
| 222 | packetBuffer->Commit(size); |
| 223 | m_BufferList.push_back(std::move(packetBuffer)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 224 | m_CommittedSize += size; |
| 225 | } |
| 226 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 227 | void Release(IPacketBufferPtr& packetBuffer) override |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 228 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 229 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 230 | |
| 231 | packetBuffer->Release(); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 234 | IPacketBufferPtr GetReadableBuffer() override |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 235 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 236 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 237 | |
| 238 | if (m_BufferList.empty()) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 239 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 240 | return nullptr; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 241 | } |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 242 | IPacketBufferPtr buffer = std::move(m_BufferList.back()); |
| 243 | m_BufferList.pop_back(); |
| 244 | m_ReadableSize += buffer->GetSize(); |
| 245 | return buffer; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 248 | void MarkRead(IPacketBufferPtr& packetBuffer) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 249 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 250 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 251 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 252 | m_ReadSize += packetBuffer->GetSize(); |
| 253 | packetBuffer->MarkRead(); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 256 | unsigned int GetCommittedSize() const { return m_CommittedSize; } |
| 257 | unsigned int GetReadableSize() const { return m_ReadableSize; } |
| 258 | unsigned int GetReadSize() const { return m_ReadSize; } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 259 | |
| 260 | private: |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 261 | // The maximum buffer size when creating a new buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 262 | unsigned int m_MaxBufferSize; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 263 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 264 | // A list of buffers |
| 265 | std::vector<IPacketBufferPtr> m_BufferList; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 266 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 267 | // 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 271 | unsigned int m_CommittedSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 272 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 273 | // 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 277 | unsigned int m_ReadSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 278 | }; |
| 279 | |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 280 | class MockSendCounterPacket : public ISendCounterPacket |
| 281 | { |
| 282 | public: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 283 | MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 284 | |
| 285 | void SendStreamMetaDataPacket() override |
| 286 | { |
| 287 | std::string message("SendStreamMetaDataPacket"); |
| 288 | unsigned int reserved = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 289 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 294 | void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 295 | { |
| 296 | std::string message("SendCounterDirectoryPacket"); |
| 297 | unsigned int reserved = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 298 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 301 | } |
| 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 308 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 311 | } |
| 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 Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 318 | 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 Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 323 | void SetReadyToRead() override {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 324 | |
| 325 | private: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 326 | IBufferManager& m_BufferManager; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 327 | }; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 328 | |
| 329 | class MockCounterDirectory : public ICounterDirectory |
| 330 | { |
| 331 | public: |
| 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 | |
| 526 | private: |
| 527 | Categories m_Categories; |
| 528 | Devices m_Devices; |
| 529 | CounterSets m_CounterSets; |
| 530 | Counters m_Counters; |
| 531 | }; |
| 532 | |
| 533 | class SendCounterPacketTest : public SendCounterPacket |
| 534 | { |
| 535 | public: |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 536 | SendCounterPacketTest(ProfilingStateMachine& profilingStateMachine, IBufferManager& buffer) |
| 537 | : SendCounterPacket(profilingStateMachine, buffer) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 538 | {} |
| 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 Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 569 | |
| 570 | } // namespace profiling |
| 571 | |
| 572 | } // namespace armnn |