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 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 24 | namespace arm |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 25 | { |
| 26 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 27 | namespace pipe |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 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 | { |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 82 | armnn::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)); |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 99 | throw arm::pipe::TimeoutException("Simulate a timeout error\n"); |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 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; |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 120 | throw arm::pipe::TimeoutException("Simulate a timeout error\n"); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 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 | { |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 144 | armnn::IgnoreUnused(timeout); |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 145 | ++m_ReadRequests; |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 146 | throw arm::pipe::ProfilingException("Simulate a non-timeout error"); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 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 | { |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 163 | armnn::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 | { |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 186 | armnn::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 | |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 209 | SwapProfilingConnectionFactoryHelper(uint16_t maxGlobalCounterId, |
| 210 | IInitialiseProfilingService& initialiser, |
| 211 | ProfilingService& profilingService) |
| 212 | : ProfilingService(maxGlobalCounterId, initialiser) |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 213 | , m_ProfilingService(profilingService) |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 214 | , m_MockProfilingConnectionFactory(new MockProfilingConnectionFactory()) |
| 215 | , m_BackupProfilingConnectionFactory(nullptr) |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 216 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 217 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 218 | CHECK(m_MockProfilingConnectionFactory); |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 219 | SwapProfilingConnectionFactory(m_ProfilingService, |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 220 | m_MockProfilingConnectionFactory.get(), |
| 221 | m_BackupProfilingConnectionFactory); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 222 | CHECK(m_BackupProfilingConnectionFactory); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 223 | } |
| 224 | ~SwapProfilingConnectionFactoryHelper() |
| 225 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 226 | CHECK(m_BackupProfilingConnectionFactory); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 227 | IProfilingConnectionFactory* temp = nullptr; |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 228 | SwapProfilingConnectionFactory(m_ProfilingService, |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 229 | m_BackupProfilingConnectionFactory, |
| 230 | temp); |
| 231 | } |
| 232 | |
| 233 | MockProfilingConnection* GetMockProfilingConnection() |
| 234 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 235 | IProfilingConnection* profilingConnection = GetProfilingConnection(m_ProfilingService); |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 236 | return armnn::PolymorphicDowncast<MockProfilingConnection*>(profilingConnection); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 239 | void ForceTransitionToState(ProfilingState newState) |
| 240 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 241 | TransitionToState(m_ProfilingService, newState); |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 244 | long WaitForPacketsSent(MockProfilingConnection* mockProfilingConnection, |
| 245 | MockProfilingConnection::PacketType packetType, |
| 246 | uint32_t length = 0, |
| 247 | uint32_t timeout = 1000) |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 248 | { |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 249 | long packetCount = mockProfilingConnection->CheckForPacket({ packetType, length }); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 250 | // The first packet we receive may not be the one we are looking for, so keep looping until till we find it, |
| 251 | // or until WaitForPacketsSent times out |
| 252 | while(packetCount == 0 && timeout != 0) |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 253 | { |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 254 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
| 255 | // Wait for a notification from the send thread |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 256 | ProfilingService::WaitForPacketSent(m_ProfilingService, timeout); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 257 | |
| 258 | std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); |
| 259 | |
| 260 | // We need to make sure the timeout does not reset each time we call WaitForPacketsSent |
| 261 | uint32_t elapsedTime = static_cast<uint32_t>( |
| 262 | std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()); |
| 263 | |
| 264 | packetCount = mockProfilingConnection->CheckForPacket({packetType, length}); |
| 265 | |
| 266 | if (elapsedTime > timeout) |
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 | break; |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 269 | } |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 270 | |
| 271 | timeout -= elapsedTime; |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 272 | } |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 273 | return packetCount; |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 276 | private: |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 277 | ProfilingService& m_ProfilingService; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 278 | MockProfilingConnectionFactoryPtr m_MockProfilingConnectionFactory; |
| 279 | IProfilingConnectionFactory* m_BackupProfilingConnectionFactory; |
| 280 | }; |
| 281 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 282 | } // namespace pipe |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 283 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 284 | } // namespace arm |