Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 8 | #include "ProfilingMocks.hpp" |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 9 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 10 | #include <armnn/Logging.hpp> |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 11 | #include <armnn/utility/PolymorphicDowncast.hpp> |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 12 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 13 | #include <IProfilingConnection.hpp> |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 14 | #include <ProfilingService.hpp> |
| 15 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 16 | #include <common/include/CommandHandlerFunctor.hpp> |
| 17 | |
| 18 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 19 | #include <doctest/doctest.h> |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 20 | |
| 21 | #include <chrono> |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 22 | #include <thread> |
| 23 | |
| 24 | namespace armnn |
| 25 | { |
| 26 | |
| 27 | namespace profiling |
| 28 | { |
| 29 | |
| 30 | struct LogLevelSwapper |
| 31 | { |
| 32 | public: |
| 33 | LogLevelSwapper(armnn::LogSeverity severity) |
| 34 | { |
| 35 | // Set the new log level |
| 36 | armnn::ConfigureLogging(true, true, severity); |
| 37 | } |
| 38 | ~LogLevelSwapper() |
| 39 | { |
| 40 | // The default log level for unit tests is "Fatal" |
| 41 | armnn::ConfigureLogging(true, true, armnn::LogSeverity::Fatal); |
| 42 | } |
| 43 | }; |
| 44 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 45 | struct StreamRedirector |
| 46 | { |
| 47 | public: |
| 48 | StreamRedirector(std::ostream& stream, std::streambuf* newStreamBuffer) |
| 49 | : m_Stream(stream) |
| 50 | , m_BackupBuffer(m_Stream.rdbuf(newStreamBuffer)) |
| 51 | {} |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 52 | |
| 53 | ~StreamRedirector() { CancelRedirect(); } |
| 54 | |
| 55 | void CancelRedirect() |
| 56 | { |
| 57 | // Only cancel the redirect once. |
| 58 | if (m_BackupBuffer != nullptr ) |
| 59 | { |
| 60 | m_Stream.rdbuf(m_BackupBuffer); |
| 61 | m_BackupBuffer = nullptr; |
| 62 | } |
| 63 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | std::ostream& m_Stream; |
| 67 | std::streambuf* m_BackupBuffer; |
| 68 | }; |
| 69 | |
| 70 | class TestProfilingConnectionBase : public IProfilingConnection |
| 71 | { |
| 72 | public: |
| 73 | TestProfilingConnectionBase() = default; |
| 74 | ~TestProfilingConnectionBase() = default; |
| 75 | |
| 76 | bool IsOpen() const override { return true; } |
| 77 | |
| 78 | void Close() override {} |
| 79 | |
Derek Lamberti | 1dd75b3 | 2019-12-10 21:23:23 +0000 | [diff] [blame] | 80 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 81 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 82 | IgnoreUnused(buffer, length); |
Derek Lamberti | 1dd75b3 | 2019-12-10 21:23:23 +0000 | [diff] [blame] | 83 | |
| 84 | return false; |
| 85 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 86 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 87 | arm::pipe::Packet ReadPacket(uint32_t timeout) override |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 88 | { |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 89 | // First time we're called return a connection ack packet. After that always timeout. |
| 90 | if (m_FirstCall) |
| 91 | { |
| 92 | m_FirstCall = false; |
| 93 | // Return connection acknowledged packet |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 94 | return arm::pipe::Packet(65536); |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 95 | } |
| 96 | else |
| 97 | { |
| 98 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 99 | throw armnn::TimeoutException("Simulate a timeout error\n"); |
| 100 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 101 | } |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 102 | |
| 103 | bool m_FirstCall = true; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | class TestProfilingConnectionTimeoutError : public TestProfilingConnectionBase |
| 107 | { |
| 108 | public: |
| 109 | TestProfilingConnectionTimeoutError() |
| 110 | : m_ReadRequests(0) |
| 111 | {} |
| 112 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 113 | arm::pipe::Packet ReadPacket(uint32_t timeout) override |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 114 | { |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 115 | // Return connection acknowledged packet after three timeouts |
| 116 | if (m_ReadRequests % 3 == 0) |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 117 | { |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 118 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 119 | ++m_ReadRequests; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 120 | throw armnn::TimeoutException("Simulate a timeout error\n"); |
| 121 | } |
| 122 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 123 | return arm::pipe::Packet(65536); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 126 | int ReadCalledCount() |
| 127 | { |
| 128 | return m_ReadRequests.load(); |
| 129 | } |
| 130 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 131 | private: |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 132 | std::atomic<int> m_ReadRequests; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class TestProfilingConnectionArmnnError : public TestProfilingConnectionBase |
| 136 | { |
| 137 | public: |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 138 | TestProfilingConnectionArmnnError() |
| 139 | : m_ReadRequests(0) |
| 140 | {} |
| 141 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 142 | arm::pipe::Packet ReadPacket(uint32_t timeout) override |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 143 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 144 | IgnoreUnused(timeout); |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 145 | ++m_ReadRequests; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 146 | throw armnn::Exception("Simulate a non-timeout error"); |
| 147 | } |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 148 | |
| 149 | int ReadCalledCount() |
| 150 | { |
| 151 | return m_ReadRequests.load(); |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | std::atomic<int> m_ReadRequests; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 158 | class TestProfilingConnectionBadAckPacket : public TestProfilingConnectionBase |
| 159 | { |
| 160 | public: |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 161 | arm::pipe::Packet ReadPacket(uint32_t timeout) override |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 162 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 163 | IgnoreUnused(timeout); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 164 | // Connection Acknowledged Packet header (word 0, word 1 is always zero): |
| 165 | // 26:31 [6] packet_family: Control Packet Family, value 0b000000 |
| 166 | // 16:25 [10] packet_id: Packet identifier, value 0b0000000001 |
| 167 | // 8:15 [8] reserved: Reserved, value 0b00000000 |
| 168 | // 0:7 [8] reserved: Reserved, value 0b00000000 |
| 169 | uint32_t packetFamily = 0; |
| 170 | uint32_t packetId = 37; // Wrong packet id!!! |
| 171 | uint32_t header = ((packetFamily & 0x0000003F) << 26) | ((packetId & 0x000003FF) << 16); |
| 172 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 173 | return arm::pipe::Packet(header); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 174 | } |
| 175 | }; |
| 176 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 177 | class TestFunctorA : public arm::pipe::CommandHandlerFunctor |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 178 | { |
| 179 | public: |
| 180 | using CommandHandlerFunctor::CommandHandlerFunctor; |
| 181 | |
| 182 | int GetCount() { return m_Count; } |
| 183 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 184 | void operator()(const arm::pipe::Packet& packet) override |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 185 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 186 | IgnoreUnused(packet); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 187 | m_Count++; |
| 188 | } |
| 189 | |
| 190 | private: |
| 191 | int m_Count = 0; |
| 192 | }; |
| 193 | |
| 194 | class TestFunctorB : public TestFunctorA |
| 195 | { |
| 196 | using TestFunctorA::TestFunctorA; |
| 197 | }; |
| 198 | |
| 199 | class TestFunctorC : public TestFunctorA |
| 200 | { |
| 201 | using TestFunctorA::TestFunctorA; |
| 202 | }; |
| 203 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 204 | class SwapProfilingConnectionFactoryHelper : public ProfilingService |
| 205 | { |
| 206 | public: |
| 207 | using MockProfilingConnectionFactoryPtr = std::unique_ptr<MockProfilingConnectionFactory>; |
| 208 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 209 | SwapProfilingConnectionFactoryHelper(armnn::profiling::ProfilingService& profilingService) |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 210 | : ProfilingService() |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 211 | , m_ProfilingService(profilingService) |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 212 | , m_MockProfilingConnectionFactory(new MockProfilingConnectionFactory()) |
| 213 | , m_BackupProfilingConnectionFactory(nullptr) |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 214 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 215 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 216 | CHECK(m_MockProfilingConnectionFactory); |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 217 | SwapProfilingConnectionFactory(m_ProfilingService, |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 218 | m_MockProfilingConnectionFactory.get(), |
| 219 | m_BackupProfilingConnectionFactory); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 220 | CHECK(m_BackupProfilingConnectionFactory); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 221 | } |
| 222 | ~SwapProfilingConnectionFactoryHelper() |
| 223 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 224 | CHECK(m_BackupProfilingConnectionFactory); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 225 | IProfilingConnectionFactory* temp = nullptr; |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 226 | SwapProfilingConnectionFactory(m_ProfilingService, |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 227 | m_BackupProfilingConnectionFactory, |
| 228 | temp); |
| 229 | } |
| 230 | |
| 231 | MockProfilingConnection* GetMockProfilingConnection() |
| 232 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 233 | IProfilingConnection* profilingConnection = GetProfilingConnection(m_ProfilingService); |
Jan Eilers | bb446e5 | 2020-04-02 13:56:54 +0100 | [diff] [blame] | 234 | return PolymorphicDowncast<MockProfilingConnection*>(profilingConnection); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 237 | void ForceTransitionToState(ProfilingState newState) |
| 238 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 239 | TransitionToState(m_ProfilingService, newState); |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 242 | long WaitForPacketsSent(MockProfilingConnection* mockProfilingConnection, |
| 243 | MockProfilingConnection::PacketType packetType, |
| 244 | uint32_t length = 0, |
| 245 | uint32_t timeout = 1000) |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 246 | { |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 247 | long packetCount = mockProfilingConnection->CheckForPacket({ packetType, length }); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 248 | // The first packet we receive may not be the one we are looking for, so keep looping until till we find it, |
| 249 | // or until WaitForPacketsSent times out |
| 250 | while(packetCount == 0 && timeout != 0) |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 251 | { |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 252 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
| 253 | // Wait for a notification from the send thread |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 254 | ProfilingService::WaitForPacketSent(m_ProfilingService, timeout); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 255 | |
| 256 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); |
| 257 | |
| 258 | // We need to make sure the timeout does not reset each time we call WaitForPacketsSent |
| 259 | uint32_t elapsedTime = static_cast<uint32_t>( |
| 260 | std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()); |
| 261 | |
| 262 | packetCount = mockProfilingConnection->CheckForPacket({packetType, length}); |
| 263 | |
| 264 | if (elapsedTime > timeout) |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 265 | { |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 266 | break; |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 267 | } |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 268 | |
| 269 | timeout -= elapsedTime; |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 270 | } |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 271 | return packetCount; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 274 | private: |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 275 | armnn::profiling::ProfilingService& m_ProfilingService; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 276 | MockProfilingConnectionFactoryPtr m_MockProfilingConnectionFactory; |
| 277 | IProfilingConnectionFactory* m_BackupProfilingConnectionFactory; |
| 278 | }; |
| 279 | |
| 280 | } // namespace profiling |
| 281 | |
| 282 | } // namespace armnn |