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