Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include "SendCounterPacketTests.hpp" |
| 9 | |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 10 | #include <armnn/Logging.hpp> |
| 11 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 12 | #include <CommandHandlerFunctor.hpp> |
| 13 | #include <IProfilingConnection.hpp> |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 14 | #include <ProfilingService.hpp> |
| 15 | |
Narumol Prangnawarat | 85ad78c | 2019-11-18 15:34:23 +0000 | [diff] [blame] | 16 | #include <boost/polymorphic_cast.hpp> |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 17 | #include <boost/test/unit_test.hpp> |
| 18 | |
| 19 | #include <chrono> |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 20 | #include <thread> |
| 21 | |
| 22 | namespace armnn |
| 23 | { |
| 24 | |
| 25 | namespace profiling |
| 26 | { |
| 27 | |
| 28 | struct LogLevelSwapper |
| 29 | { |
| 30 | public: |
| 31 | LogLevelSwapper(armnn::LogSeverity severity) |
| 32 | { |
| 33 | // Set the new log level |
| 34 | armnn::ConfigureLogging(true, true, severity); |
| 35 | } |
| 36 | ~LogLevelSwapper() |
| 37 | { |
| 38 | // The default log level for unit tests is "Fatal" |
| 39 | armnn::ConfigureLogging(true, true, armnn::LogSeverity::Fatal); |
| 40 | } |
| 41 | }; |
| 42 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 43 | struct StreamRedirector |
| 44 | { |
| 45 | public: |
| 46 | StreamRedirector(std::ostream& stream, std::streambuf* newStreamBuffer) |
| 47 | : m_Stream(stream) |
| 48 | , m_BackupBuffer(m_Stream.rdbuf(newStreamBuffer)) |
| 49 | {} |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 50 | |
| 51 | ~StreamRedirector() { CancelRedirect(); } |
| 52 | |
| 53 | void CancelRedirect() |
| 54 | { |
| 55 | // Only cancel the redirect once. |
| 56 | if (m_BackupBuffer != nullptr ) |
| 57 | { |
| 58 | m_Stream.rdbuf(m_BackupBuffer); |
| 59 | m_BackupBuffer = nullptr; |
| 60 | } |
| 61 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | std::ostream& m_Stream; |
| 65 | std::streambuf* m_BackupBuffer; |
| 66 | }; |
| 67 | |
| 68 | class TestProfilingConnectionBase : public IProfilingConnection |
| 69 | { |
| 70 | public: |
| 71 | TestProfilingConnectionBase() = default; |
| 72 | ~TestProfilingConnectionBase() = default; |
| 73 | |
| 74 | bool IsOpen() const override { return true; } |
| 75 | |
| 76 | void Close() override {} |
| 77 | |
Derek Lamberti | 1dd75b3 | 2019-12-10 21:23:23 +0000 | [diff] [blame] | 78 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 79 | { |
| 80 | boost::ignore_unused(buffer, length); |
| 81 | |
| 82 | return false; |
| 83 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 84 | |
| 85 | Packet ReadPacket(uint32_t timeout) override |
| 86 | { |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 87 | // First time we're called return a connection ack packet. After that always timeout. |
| 88 | if (m_FirstCall) |
| 89 | { |
| 90 | m_FirstCall = false; |
| 91 | // Return connection acknowledged packet |
| 92 | return Packet(65536); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 97 | throw armnn::TimeoutException("Simulate a timeout error\n"); |
| 98 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 99 | } |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 100 | |
| 101 | bool m_FirstCall = true; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | class TestProfilingConnectionTimeoutError : public TestProfilingConnectionBase |
| 105 | { |
| 106 | public: |
| 107 | TestProfilingConnectionTimeoutError() |
| 108 | : m_ReadRequests(0) |
| 109 | {} |
| 110 | |
| 111 | Packet ReadPacket(uint32_t timeout) override |
| 112 | { |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 113 | // Return connection acknowledged packet after three timeouts |
| 114 | if (m_ReadRequests % 3 == 0) |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 115 | { |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 116 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 117 | ++m_ReadRequests; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 118 | throw armnn::TimeoutException("Simulate a timeout error\n"); |
| 119 | } |
| 120 | |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame] | 121 | return Packet(65536); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 124 | int ReadCalledCount() |
| 125 | { |
| 126 | return m_ReadRequests.load(); |
| 127 | } |
| 128 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 129 | private: |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 130 | std::atomic<int> m_ReadRequests; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | class TestProfilingConnectionArmnnError : public TestProfilingConnectionBase |
| 134 | { |
| 135 | public: |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 136 | TestProfilingConnectionArmnnError() |
| 137 | : m_ReadRequests(0) |
| 138 | {} |
| 139 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 140 | Packet ReadPacket(uint32_t timeout) override |
| 141 | { |
Derek Lamberti | 1dd75b3 | 2019-12-10 21:23:23 +0000 | [diff] [blame] | 142 | boost::ignore_unused(timeout); |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 143 | ++m_ReadRequests; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 144 | throw armnn::Exception("Simulate a non-timeout error"); |
| 145 | } |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 146 | |
| 147 | int ReadCalledCount() |
| 148 | { |
| 149 | return m_ReadRequests.load(); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | std::atomic<int> m_ReadRequests; |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | class TestFunctorA : public CommandHandlerFunctor |
| 157 | { |
| 158 | public: |
| 159 | using CommandHandlerFunctor::CommandHandlerFunctor; |
| 160 | |
| 161 | int GetCount() { return m_Count; } |
| 162 | |
| 163 | void operator()(const Packet& packet) override |
| 164 | { |
Derek Lamberti | 1dd75b3 | 2019-12-10 21:23:23 +0000 | [diff] [blame] | 165 | boost::ignore_unused(packet); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 166 | m_Count++; |
| 167 | } |
| 168 | |
| 169 | private: |
| 170 | int m_Count = 0; |
| 171 | }; |
| 172 | |
| 173 | class TestFunctorB : public TestFunctorA |
| 174 | { |
| 175 | using TestFunctorA::TestFunctorA; |
| 176 | }; |
| 177 | |
| 178 | class TestFunctorC : public TestFunctorA |
| 179 | { |
| 180 | using TestFunctorA::TestFunctorA; |
| 181 | }; |
| 182 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 183 | class SwapProfilingConnectionFactoryHelper : public ProfilingService |
| 184 | { |
| 185 | public: |
| 186 | using MockProfilingConnectionFactoryPtr = std::unique_ptr<MockProfilingConnectionFactory>; |
| 187 | |
| 188 | SwapProfilingConnectionFactoryHelper() |
| 189 | : ProfilingService() |
| 190 | , m_MockProfilingConnectionFactory(new MockProfilingConnectionFactory()) |
| 191 | , m_BackupProfilingConnectionFactory(nullptr) |
| 192 | { |
| 193 | BOOST_CHECK(m_MockProfilingConnectionFactory); |
| 194 | SwapProfilingConnectionFactory(ProfilingService::Instance(), |
| 195 | m_MockProfilingConnectionFactory.get(), |
| 196 | m_BackupProfilingConnectionFactory); |
| 197 | BOOST_CHECK(m_BackupProfilingConnectionFactory); |
| 198 | } |
| 199 | ~SwapProfilingConnectionFactoryHelper() |
| 200 | { |
| 201 | BOOST_CHECK(m_BackupProfilingConnectionFactory); |
| 202 | IProfilingConnectionFactory* temp = nullptr; |
| 203 | SwapProfilingConnectionFactory(ProfilingService::Instance(), |
| 204 | m_BackupProfilingConnectionFactory, |
| 205 | temp); |
| 206 | } |
| 207 | |
| 208 | MockProfilingConnection* GetMockProfilingConnection() |
| 209 | { |
| 210 | IProfilingConnection* profilingConnection = GetProfilingConnection(ProfilingService::Instance()); |
| 211 | return boost::polymorphic_downcast<MockProfilingConnection*>(profilingConnection); |
| 212 | } |
| 213 | |
Matteo Martincigh | 8efc500 | 2019-10-10 14:30:29 +0100 | [diff] [blame] | 214 | void ForceTransitionToState(ProfilingState newState) |
| 215 | { |
| 216 | TransitionToState(ProfilingService::Instance(), newState); |
| 217 | } |
| 218 | |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 219 | void WaitForProfilingPacketsSent(MockProfilingConnection* mockProfilingConnection, uint32_t timeout = 1000) |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 220 | { |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 221 | if (!mockProfilingConnection->HasWrittenData()) |
| 222 | { |
| 223 | WaitForPacketSent(ProfilingService::Instance(), timeout); |
| 224 | // It's possible the wait has timed out. Check there is some data. |
| 225 | if (!mockProfilingConnection->HasWrittenData()) |
| 226 | { |
| 227 | throw RuntimeException("ProfilingTests::WaitForProfilingPacketsSent timeout waiting for packet."); |
| 228 | } |
| 229 | } |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 232 | private: |
| 233 | MockProfilingConnectionFactoryPtr m_MockProfilingConnectionFactory; |
| 234 | IProfilingConnectionFactory* m_BackupProfilingConnectionFactory; |
| 235 | }; |
| 236 | |
| 237 | } // namespace profiling |
| 238 | |
| 239 | } // namespace armnn |