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 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 148 | IPacketBufferPtr 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 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 162 | void Commit(IPacketBufferPtr& 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 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 168 | IPacketBufferPtr GetReadableBuffer() override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 169 | { |
| 170 | return std::move(m_Buffer); |
| 171 | } |
| 172 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 173 | void Release(IPacketBufferPtr& packetBuffer) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 174 | { |
| 175 | packetBuffer->Release(); |
| 176 | m_Buffer = std::move(packetBuffer); |
| 177 | } |
| 178 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 179 | void MarkRead(IPacketBufferPtr& packetBuffer) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 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; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 187 | IPacketBufferPtr 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 | MockStreamCounterBuffer(unsigned int maxBufferSize = 4096) |
| 194 | : m_MaxBufferSize(maxBufferSize) |
| 195 | , m_BufferList() |
| 196 | , m_CommittedSize(0) |
| 197 | , m_ReadableSize(0) |
| 198 | , m_ReadSize(0) |
| 199 | {} |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 200 | ~MockStreamCounterBuffer() {} |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 201 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 202 | IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 203 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 204 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 205 | |
| 206 | reservedSize = 0; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 207 | if (requestedSize > m_MaxBufferSize) |
Narumol Prangnawarat | 7be47ef | 2019-09-27 18:00:11 +0100 | [diff] [blame] | 208 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 209 | throw armnn::InvalidArgumentException("The maximum buffer size that can be requested is [" + |
| 210 | std::to_string(m_MaxBufferSize) + "] bytes"); |
Narumol Prangnawarat | 7be47ef | 2019-09-27 18:00:11 +0100 | [diff] [blame] | 211 | } |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 212 | reservedSize = requestedSize; |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 213 | return std::make_unique<MockPacketBuffer>(requestedSize); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 216 | void Commit(IPacketBufferPtr& packetBuffer, unsigned int size) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 217 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 218 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 219 | |
| 220 | packetBuffer->Commit(size); |
| 221 | m_BufferList.push_back(std::move(packetBuffer)); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 222 | m_CommittedSize += size; |
| 223 | } |
| 224 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 225 | void Release(IPacketBufferPtr& packetBuffer) override |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 226 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 227 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 228 | |
| 229 | packetBuffer->Release(); |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 232 | IPacketBufferPtr GetReadableBuffer() override |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 233 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 234 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 235 | |
| 236 | if (m_BufferList.empty()) |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 237 | { |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 238 | return nullptr; |
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 | IPacketBufferPtr buffer = std::move(m_BufferList.back()); |
| 241 | m_BufferList.pop_back(); |
| 242 | m_ReadableSize += buffer->GetSize(); |
| 243 | return buffer; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 246 | void MarkRead(IPacketBufferPtr& packetBuffer) override |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 247 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 248 | std::lock_guard<std::mutex> lock(m_Mutex); |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 249 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 250 | m_ReadSize += packetBuffer->GetSize(); |
| 251 | packetBuffer->MarkRead(); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 254 | unsigned int GetCommittedSize() const { return m_CommittedSize; } |
| 255 | unsigned int GetReadableSize() const { return m_ReadableSize; } |
| 256 | unsigned int GetReadSize() const { return m_ReadSize; } |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 257 | |
| 258 | private: |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 259 | // The maximum buffer size when creating a new buffer |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 260 | unsigned int m_MaxBufferSize; |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 261 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 262 | // A list of buffers |
| 263 | std::vector<IPacketBufferPtr> m_BufferList; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 264 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 265 | // The mutex to synchronize this mock's methods |
| 266 | std::mutex m_Mutex; |
| 267 | |
| 268 | // The total size of the buffers that has been committed for reading |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 269 | unsigned int m_CommittedSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 270 | |
Matteo Martincigh | 61d6f73 | 2019-10-03 11:21:18 +0100 | [diff] [blame] | 271 | // The total size of the buffers that can be read |
| 272 | unsigned int m_ReadableSize; |
| 273 | |
| 274 | // The total size of the buffers that has already been read |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 275 | unsigned int m_ReadSize; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 276 | }; |
| 277 | |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 278 | class MockSendCounterPacket : public ISendCounterPacket |
| 279 | { |
| 280 | public: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 281 | MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 282 | |
| 283 | void SendStreamMetaDataPacket() override |
| 284 | { |
| 285 | std::string message("SendStreamMetaDataPacket"); |
| 286 | unsigned int reserved = 0; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 287 | IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 288 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 289 | m_BufferManager.Commit(buffer, reserved); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 292 | void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 293 | { |
| 294 | std::string message("SendCounterDirectoryPacket"); |
| 295 | unsigned int reserved = 0; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 296 | IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 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 SendPeriodicCounterCapturePacket(uint64_t timestamp, |
| 302 | const std::vector<std::pair<uint16_t, uint32_t>>& values) override |
| 303 | { |
| 304 | std::string message("SendPeriodicCounterCapturePacket"); |
| 305 | unsigned int reserved = 0; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 306 | IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 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 | |
| 311 | void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 312 | const std::vector<uint16_t>& selectedCounterIds) override |
| 313 | { |
| 314 | std::string message("SendPeriodicCounterSelectionPacket"); |
| 315 | unsigned int reserved = 0; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 316 | IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved); |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 317 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 318 | m_BufferManager.Commit(buffer, reserved); |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 321 | void SetReadyToRead() override {} |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 322 | |
| 323 | private: |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 324 | IBufferManager& m_BufferManager; |
Ferran Balaguer | 1b94172 | 2019-08-28 16:57:18 +0100 | [diff] [blame] | 325 | }; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 326 | |
| 327 | class MockCounterDirectory : public ICounterDirectory |
| 328 | { |
| 329 | public: |
| 330 | MockCounterDirectory() = default; |
| 331 | ~MockCounterDirectory() = default; |
| 332 | |
| 333 | // Register profiling objects |
| 334 | const Category* RegisterCategory(const std::string& categoryName, |
| 335 | const armnn::Optional<uint16_t>& deviceUid = armnn::EmptyOptional(), |
| 336 | const armnn::Optional<uint16_t>& counterSetUid = armnn::EmptyOptional()) |
| 337 | { |
| 338 | // Get the device UID |
| 339 | uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0; |
| 340 | |
| 341 | // Get the counter set UID |
| 342 | uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0; |
| 343 | |
| 344 | // Create the category |
| 345 | CategoryPtr category = std::make_unique<Category>(categoryName, deviceUidValue, counterSetUidValue); |
| 346 | BOOST_ASSERT(category); |
| 347 | |
| 348 | // Get the raw category pointer |
| 349 | const Category* categoryPtr = category.get(); |
| 350 | BOOST_ASSERT(categoryPtr); |
| 351 | |
| 352 | // Register the category |
| 353 | m_Categories.insert(std::move(category)); |
| 354 | |
| 355 | return categoryPtr; |
| 356 | } |
| 357 | |
| 358 | const Device* RegisterDevice(const std::string& deviceName, |
| 359 | uint16_t cores = 0, |
| 360 | const armnn::Optional<std::string>& parentCategoryName = armnn::EmptyOptional()) |
| 361 | { |
| 362 | // Get the device UID |
| 363 | uint16_t deviceUid = GetNextUid(); |
| 364 | |
| 365 | // Create the device |
| 366 | DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores); |
| 367 | BOOST_ASSERT(device); |
| 368 | |
| 369 | // Get the raw device pointer |
| 370 | const Device* devicePtr = device.get(); |
| 371 | BOOST_ASSERT(devicePtr); |
| 372 | |
| 373 | // Register the device |
| 374 | m_Devices.insert(std::make_pair(deviceUid, std::move(device))); |
| 375 | |
| 376 | // Connect the counter set to the parent category, if required |
| 377 | if (parentCategoryName.has_value()) |
| 378 | { |
| 379 | // Set the counter set UID in the parent category |
| 380 | Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName.value())); |
| 381 | BOOST_ASSERT(parentCategory); |
| 382 | parentCategory->m_DeviceUid = deviceUid; |
| 383 | } |
| 384 | |
| 385 | return devicePtr; |
| 386 | } |
| 387 | |
| 388 | const CounterSet* RegisterCounterSet( |
| 389 | const std::string& counterSetName, |
| 390 | uint16_t count = 0, |
| 391 | const armnn::Optional<std::string>& parentCategoryName = armnn::EmptyOptional()) |
| 392 | { |
| 393 | // Get the counter set UID |
| 394 | uint16_t counterSetUid = GetNextUid(); |
| 395 | |
| 396 | // Create the counter set |
| 397 | CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count); |
| 398 | BOOST_ASSERT(counterSet); |
| 399 | |
| 400 | // Get the raw counter set pointer |
| 401 | const CounterSet* counterSetPtr = counterSet.get(); |
| 402 | BOOST_ASSERT(counterSetPtr); |
| 403 | |
| 404 | // Register the counter set |
| 405 | m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet))); |
| 406 | |
| 407 | // Connect the counter set to the parent category, if required |
| 408 | if (parentCategoryName.has_value()) |
| 409 | { |
| 410 | // Set the counter set UID in the parent category |
| 411 | Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName.value())); |
| 412 | BOOST_ASSERT(parentCategory); |
| 413 | parentCategory->m_CounterSetUid = counterSetUid; |
| 414 | } |
| 415 | |
| 416 | return counterSetPtr; |
| 417 | } |
| 418 | |
| 419 | const Counter* RegisterCounter(const std::string& parentCategoryName, |
| 420 | uint16_t counterClass, |
| 421 | uint16_t interpolation, |
| 422 | double multiplier, |
| 423 | const std::string& name, |
| 424 | const std::string& description, |
| 425 | const armnn::Optional<std::string>& units = armnn::EmptyOptional(), |
| 426 | const armnn::Optional<uint16_t>& numberOfCores = armnn::EmptyOptional(), |
| 427 | const armnn::Optional<uint16_t>& deviceUid = armnn::EmptyOptional(), |
| 428 | const armnn::Optional<uint16_t>& counterSetUid = armnn::EmptyOptional()) |
| 429 | { |
| 430 | // Get the number of cores from the argument only |
| 431 | uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0; |
| 432 | |
| 433 | // Get the device UID |
| 434 | uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0; |
| 435 | |
| 436 | // Get the counter set UID |
| 437 | uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0; |
| 438 | |
| 439 | // Get the counter UIDs and calculate the max counter UID |
| 440 | std::vector<uint16_t> counterUids = GetNextCounterUids(deviceCores); |
| 441 | BOOST_ASSERT(!counterUids.empty()); |
| 442 | uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back(); |
| 443 | |
| 444 | // Get the counter units |
| 445 | const std::string unitsValue = units.has_value() ? units.value() : ""; |
| 446 | |
| 447 | // Create the counter |
| 448 | CounterPtr counter = std::make_shared<Counter>(counterUids.front(), |
| 449 | maxCounterUid, |
| 450 | counterClass, |
| 451 | interpolation, |
| 452 | multiplier, |
| 453 | name, |
| 454 | description, |
| 455 | unitsValue, |
| 456 | deviceUidValue, |
| 457 | counterSetUidValue); |
| 458 | BOOST_ASSERT(counter); |
| 459 | |
| 460 | // Get the raw counter pointer |
| 461 | const Counter* counterPtr = counter.get(); |
| 462 | BOOST_ASSERT(counterPtr); |
| 463 | |
| 464 | // Process multiple counters if necessary |
| 465 | for (uint16_t counterUid : counterUids) |
| 466 | { |
| 467 | // Connect the counter to the parent category |
| 468 | Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName)); |
| 469 | BOOST_ASSERT(parentCategory); |
| 470 | parentCategory->m_Counters.push_back(counterUid); |
| 471 | |
| 472 | // Register the counter |
| 473 | m_Counters.insert(std::make_pair(counterUid, counter)); |
| 474 | } |
| 475 | |
| 476 | return counterPtr; |
| 477 | } |
| 478 | |
| 479 | // Getters for counts |
| 480 | uint16_t GetCategoryCount() const override { return boost::numeric_cast<uint16_t>(m_Categories.size()); } |
| 481 | uint16_t GetDeviceCount() const override { return boost::numeric_cast<uint16_t>(m_Devices.size()); } |
| 482 | uint16_t GetCounterSetCount() const override { return boost::numeric_cast<uint16_t>(m_CounterSets.size()); } |
| 483 | uint16_t GetCounterCount() const override { return boost::numeric_cast<uint16_t>(m_Counters.size()); } |
| 484 | |
| 485 | // Getters for collections |
| 486 | const Categories& GetCategories() const override { return m_Categories; } |
| 487 | const Devices& GetDevices() const override { return m_Devices; } |
| 488 | const CounterSets& GetCounterSets() const override { return m_CounterSets; } |
| 489 | const Counters& GetCounters() const override { return m_Counters; } |
| 490 | |
| 491 | // Getters for profiling objects |
| 492 | const Category* GetCategory(const std::string& name) const override |
| 493 | { |
| 494 | auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](const CategoryPtr& category) |
| 495 | { |
| 496 | BOOST_ASSERT(category); |
| 497 | |
| 498 | return category->m_Name == name; |
| 499 | }); |
| 500 | |
| 501 | if (it == m_Categories.end()) |
| 502 | { |
| 503 | return nullptr; |
| 504 | } |
| 505 | |
| 506 | return it->get(); |
| 507 | } |
| 508 | |
| 509 | const Device* GetDevice(uint16_t uid) const override |
| 510 | { |
| 511 | return nullptr; // Not used by the unit tests |
| 512 | } |
| 513 | |
| 514 | const CounterSet* GetCounterSet(uint16_t uid) const override |
| 515 | { |
| 516 | return nullptr; // Not used by the unit tests |
| 517 | } |
| 518 | |
| 519 | const Counter* GetCounter(uint16_t uid) const override |
| 520 | { |
| 521 | return nullptr; // Not used by the unit tests |
| 522 | } |
| 523 | |
| 524 | private: |
| 525 | Categories m_Categories; |
| 526 | Devices m_Devices; |
| 527 | CounterSets m_CounterSets; |
| 528 | Counters m_Counters; |
| 529 | }; |
| 530 | |
| 531 | class SendCounterPacketTest : public SendCounterPacket |
| 532 | { |
| 533 | public: |
Matteo Martincigh | 5d737fb | 2019-10-07 13:05:13 +0100 | [diff] [blame] | 534 | SendCounterPacketTest(ProfilingStateMachine& profilingStateMachine, IBufferManager& buffer) |
| 535 | : SendCounterPacket(profilingStateMachine, buffer) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 536 | {} |
| 537 | |
| 538 | bool CreateDeviceRecordTest(const DevicePtr& device, |
| 539 | DeviceRecord& deviceRecord, |
| 540 | std::string& errorMessage) |
| 541 | { |
| 542 | return CreateDeviceRecord(device, deviceRecord, errorMessage); |
| 543 | } |
| 544 | |
| 545 | bool CreateCounterSetRecordTest(const CounterSetPtr& counterSet, |
| 546 | CounterSetRecord& counterSetRecord, |
| 547 | std::string& errorMessage) |
| 548 | { |
| 549 | return CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage); |
| 550 | } |
| 551 | |
| 552 | bool CreateEventRecordTest(const CounterPtr& counter, |
| 553 | EventRecord& eventRecord, |
| 554 | std::string& errorMessage) |
| 555 | { |
| 556 | return CreateEventRecord(counter, eventRecord, errorMessage); |
| 557 | } |
| 558 | |
| 559 | bool CreateCategoryRecordTest(const CategoryPtr& category, |
| 560 | const Counters& counters, |
| 561 | CategoryRecord& categoryRecord, |
| 562 | std::string& errorMessage) |
| 563 | { |
| 564 | return CreateCategoryRecord(category, counters, categoryRecord, errorMessage); |
| 565 | } |
| 566 | }; |
Matteo Martincigh | 24e8f92 | 2019-09-19 11:57:46 +0100 | [diff] [blame] | 567 | |
| 568 | } // namespace profiling |
| 569 | |
| 570 | } // namespace armnn |