Francis Murtagh | 1f7db45 | 2019-08-14 09:49:34 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "../CommandHandlerKey.hpp" |
Francis Murtagh | 11f99b4 | 2019-08-16 11:28:52 +0100 | [diff] [blame] | 7 | #include "../CommandHandlerFunctor.hpp" |
Francis Murtagh | 94d7915 | 2019-08-16 17:45:07 +0100 | [diff] [blame] | 8 | #include "../CommandHandlerRegistry.hpp" |
Nikhil Raj | d88e47c | 2019-08-19 10:04:23 +0100 | [diff] [blame] | 9 | #include "../EncodeVersion.hpp" |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 10 | #include "../Holder.hpp" |
Nikhil Raj | bc62605 | 2019-08-15 15:49:45 +0100 | [diff] [blame] | 11 | #include "../Packet.hpp" |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 12 | #include "../PacketVersionResolver.hpp" |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 13 | #include "../ProfilingStateMachine.hpp" |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 14 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 15 | #include "../ProfilingService.hpp" |
| 16 | |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 17 | #include <boost/test/unit_test.hpp> |
Francis Murtagh | 1f7db45 | 2019-08-14 09:49:34 +0100 | [diff] [blame] | 18 | |
Nikhil Raj | bc62605 | 2019-08-15 15:49:45 +0100 | [diff] [blame] | 19 | #include <cstdint> |
| 20 | #include <cstring> |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 21 | #include <limits> |
Francis Murtagh | 11f99b4 | 2019-08-16 11:28:52 +0100 | [diff] [blame] | 22 | #include <map> |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 23 | #include <random> |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 24 | #include <thread> |
Francis Murtagh | 1f7db45 | 2019-08-14 09:49:34 +0100 | [diff] [blame] | 25 | |
| 26 | BOOST_AUTO_TEST_SUITE(ExternalProfiling) |
| 27 | |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 28 | using namespace armnn::profiling; |
| 29 | |
Francis Murtagh | 1f7db45 | 2019-08-14 09:49:34 +0100 | [diff] [blame] | 30 | BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons) |
| 31 | { |
| 32 | CommandHandlerKey testKey0(1, 1); |
| 33 | CommandHandlerKey testKey1(1, 1); |
| 34 | CommandHandlerKey testKey2(1, 1); |
| 35 | CommandHandlerKey testKey3(0, 0); |
| 36 | CommandHandlerKey testKey4(2, 2); |
| 37 | CommandHandlerKey testKey5(0, 2); |
| 38 | |
| 39 | BOOST_CHECK(testKey1<testKey4); |
| 40 | BOOST_CHECK(testKey1>testKey3); |
| 41 | BOOST_CHECK(testKey1<=testKey4); |
| 42 | BOOST_CHECK(testKey1>=testKey3); |
| 43 | BOOST_CHECK(testKey1<=testKey2); |
| 44 | BOOST_CHECK(testKey1>=testKey2); |
| 45 | BOOST_CHECK(testKey1==testKey2); |
| 46 | BOOST_CHECK(testKey1==testKey1); |
| 47 | |
| 48 | BOOST_CHECK(!(testKey1==testKey5)); |
| 49 | BOOST_CHECK(!(testKey1!=testKey1)); |
| 50 | BOOST_CHECK(testKey1!=testKey5); |
| 51 | |
| 52 | BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1); |
| 53 | BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2); |
| 54 | |
| 55 | BOOST_CHECK(testKey1.GetPacketId()==1); |
| 56 | BOOST_CHECK(testKey1.GetVersion()==1); |
| 57 | |
| 58 | std::vector<CommandHandlerKey> vect = |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 59 | { |
| 60 | CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0), |
| 61 | CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1), |
| 62 | CommandHandlerKey(2,0), CommandHandlerKey(0,0) |
| 63 | }; |
Francis Murtagh | 1f7db45 | 2019-08-14 09:49:34 +0100 | [diff] [blame] | 64 | |
| 65 | std::sort(vect.begin(), vect.end()); |
| 66 | |
| 67 | std::vector<CommandHandlerKey> expectedVect = |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 68 | { |
| 69 | CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1), |
| 70 | CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0), |
| 71 | CommandHandlerKey(2,0), CommandHandlerKey(2,1) |
| 72 | }; |
Francis Murtagh | 1f7db45 | 2019-08-14 09:49:34 +0100 | [diff] [blame] | 73 | |
| 74 | BOOST_CHECK(vect == expectedVect); |
| 75 | } |
| 76 | |
Nikhil Raj | d88e47c | 2019-08-19 10:04:23 +0100 | [diff] [blame] | 77 | BOOST_AUTO_TEST_CASE(CheckEncodeVersion) |
| 78 | { |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 79 | Version version1(12); |
Nikhil Raj | d88e47c | 2019-08-19 10:04:23 +0100 | [diff] [blame] | 80 | |
| 81 | BOOST_CHECK(version1.GetMajor() == 0); |
| 82 | BOOST_CHECK(version1.GetMinor() == 0); |
| 83 | BOOST_CHECK(version1.GetPatch() == 12); |
| 84 | |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 85 | Version version2(4108); |
Nikhil Raj | d88e47c | 2019-08-19 10:04:23 +0100 | [diff] [blame] | 86 | |
| 87 | BOOST_CHECK(version2.GetMajor() == 0); |
| 88 | BOOST_CHECK(version2.GetMinor() == 1); |
| 89 | BOOST_CHECK(version2.GetPatch() == 12); |
| 90 | |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 91 | Version version3(4198412); |
Nikhil Raj | d88e47c | 2019-08-19 10:04:23 +0100 | [diff] [blame] | 92 | |
| 93 | BOOST_CHECK(version3.GetMajor() == 1); |
| 94 | BOOST_CHECK(version3.GetMinor() == 1); |
| 95 | BOOST_CHECK(version3.GetPatch() == 12); |
| 96 | |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 97 | Version version4(0); |
Nikhil Raj | d88e47c | 2019-08-19 10:04:23 +0100 | [diff] [blame] | 98 | |
| 99 | BOOST_CHECK(version4.GetMajor() == 0); |
| 100 | BOOST_CHECK(version4.GetMinor() == 0); |
| 101 | BOOST_CHECK(version4.GetPatch() == 0); |
| 102 | |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 103 | Version version5(1, 0, 0); |
Nikhil Raj | d88e47c | 2019-08-19 10:04:23 +0100 | [diff] [blame] | 104 | BOOST_CHECK(version5.GetEncodedValue() == 4194304); |
| 105 | } |
| 106 | |
Nikhil Raj | bc62605 | 2019-08-15 15:49:45 +0100 | [diff] [blame] | 107 | BOOST_AUTO_TEST_CASE(CheckPacketClass) |
| 108 | { |
| 109 | const char* data = "test"; |
| 110 | unsigned int length = static_cast<unsigned int>(std::strlen(data)); |
| 111 | |
| 112 | Packet packetTest1(472580096,length,data); |
| 113 | BOOST_CHECK_THROW(Packet packetTest2(472580096,0,""), armnn::Exception); |
| 114 | |
| 115 | Packet packetTest3(472580096,0, nullptr); |
| 116 | |
| 117 | BOOST_CHECK(packetTest1.GetLength() == length); |
| 118 | BOOST_CHECK(packetTest1.GetData() == data); |
| 119 | |
| 120 | BOOST_CHECK(packetTest1.GetPacketFamily() == 7); |
| 121 | BOOST_CHECK(packetTest1.GetPacketId() == 43); |
| 122 | BOOST_CHECK(packetTest1.GetPacketType() == 3); |
| 123 | BOOST_CHECK(packetTest1.GetPacketClass() == 5); |
| 124 | } |
| 125 | |
Francis Murtagh | 94d7915 | 2019-08-16 17:45:07 +0100 | [diff] [blame] | 126 | // Create Derived Classes |
| 127 | class TestFunctorA : public CommandHandlerFunctor |
| 128 | { |
| 129 | public: |
| 130 | using CommandHandlerFunctor::CommandHandlerFunctor; |
| 131 | |
| 132 | int GetCount() { return m_Count; } |
| 133 | |
| 134 | void operator()(const Packet& packet) override |
| 135 | { |
| 136 | m_Count++; |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | int m_Count = 0; |
| 141 | }; |
| 142 | |
| 143 | class TestFunctorB : public TestFunctorA |
| 144 | { |
| 145 | using TestFunctorA::TestFunctorA; |
| 146 | }; |
| 147 | |
| 148 | class TestFunctorC : public TestFunctorA |
| 149 | { |
| 150 | using TestFunctorA::TestFunctorA; |
| 151 | }; |
| 152 | |
Francis Murtagh | 11f99b4 | 2019-08-16 11:28:52 +0100 | [diff] [blame] | 153 | BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor) |
| 154 | { |
Francis Murtagh | 11f99b4 | 2019-08-16 11:28:52 +0100 | [diff] [blame] | 155 | // Hard code the version as it will be the same during a single profiling session |
| 156 | uint32_t version = 1; |
| 157 | |
| 158 | TestFunctorA testFunctorA(461, version); |
| 159 | TestFunctorB testFunctorB(963, version); |
| 160 | TestFunctorC testFunctorC(983, version); |
| 161 | |
| 162 | CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion()); |
| 163 | CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion()); |
| 164 | CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion()); |
| 165 | |
| 166 | // Create the unwrapped map to simulate the Command Handler Registry |
| 167 | std::map<CommandHandlerKey, CommandHandlerFunctor*> registry; |
| 168 | |
| 169 | registry.insert(std::make_pair(keyB, &testFunctorB)); |
| 170 | registry.insert(std::make_pair(keyA, &testFunctorA)); |
| 171 | registry.insert(std::make_pair(keyC, &testFunctorC)); |
| 172 | |
| 173 | // Check the order of the map is correct |
| 174 | auto it = registry.begin(); |
| 175 | BOOST_CHECK(it->first==keyA); |
| 176 | it++; |
| 177 | BOOST_CHECK(it->first==keyB); |
| 178 | it++; |
| 179 | BOOST_CHECK(it->first==keyC); |
| 180 | |
| 181 | Packet packetA(500000000, 0, nullptr); |
| 182 | Packet packetB(600000000, 0, nullptr); |
| 183 | Packet packetC(400000000, 0, nullptr); |
| 184 | |
| 185 | // Check the correct operator of derived class is called |
| 186 | registry.at(CommandHandlerKey(packetA.GetPacketId(), version))->operator()(packetA); |
| 187 | BOOST_CHECK(testFunctorA.GetCount() == 1); |
| 188 | BOOST_CHECK(testFunctorB.GetCount() == 0); |
| 189 | BOOST_CHECK(testFunctorC.GetCount() == 0); |
| 190 | |
| 191 | registry.at(CommandHandlerKey(packetB.GetPacketId(), version))->operator()(packetB); |
| 192 | BOOST_CHECK(testFunctorA.GetCount() == 1); |
| 193 | BOOST_CHECK(testFunctorB.GetCount() == 1); |
| 194 | BOOST_CHECK(testFunctorC.GetCount() == 0); |
| 195 | |
| 196 | registry.at(CommandHandlerKey(packetC.GetPacketId(), version))->operator()(packetC); |
| 197 | BOOST_CHECK(testFunctorA.GetCount() == 1); |
| 198 | BOOST_CHECK(testFunctorB.GetCount() == 1); |
| 199 | BOOST_CHECK(testFunctorC.GetCount() == 1); |
| 200 | } |
| 201 | |
Francis Murtagh | 94d7915 | 2019-08-16 17:45:07 +0100 | [diff] [blame] | 202 | BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry) |
| 203 | { |
| 204 | // Hard code the version as it will be the same during a single profiling session |
| 205 | uint32_t version = 1; |
| 206 | |
| 207 | TestFunctorA testFunctorA(461, version); |
| 208 | TestFunctorB testFunctorB(963, version); |
| 209 | TestFunctorC testFunctorC(983, version); |
| 210 | |
| 211 | // Create the Command Handler Registry |
| 212 | CommandHandlerRegistry registry; |
| 213 | |
| 214 | // Register multiple different derived classes |
| 215 | registry.RegisterFunctor(&testFunctorA, testFunctorA.GetPacketId(), testFunctorA.GetVersion()); |
| 216 | registry.RegisterFunctor(&testFunctorB, testFunctorB.GetPacketId(), testFunctorB.GetVersion()); |
| 217 | registry.RegisterFunctor(&testFunctorC, testFunctorC.GetPacketId(), testFunctorC.GetVersion()); |
| 218 | |
| 219 | Packet packetA(500000000, 0, nullptr); |
| 220 | Packet packetB(600000000, 0, nullptr); |
| 221 | Packet packetC(400000000, 0, nullptr); |
| 222 | |
| 223 | // Check the correct operator of derived class is called |
| 224 | registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetA); |
| 225 | BOOST_CHECK(testFunctorA.GetCount() == 1); |
| 226 | BOOST_CHECK(testFunctorB.GetCount() == 0); |
| 227 | BOOST_CHECK(testFunctorC.GetCount() == 0); |
| 228 | |
| 229 | registry.GetFunctor(packetB.GetPacketId(), version)->operator()(packetB); |
| 230 | BOOST_CHECK(testFunctorA.GetCount() == 1); |
| 231 | BOOST_CHECK(testFunctorB.GetCount() == 1); |
| 232 | BOOST_CHECK(testFunctorC.GetCount() == 0); |
| 233 | |
| 234 | registry.GetFunctor(packetC.GetPacketId(), version)->operator()(packetC); |
| 235 | BOOST_CHECK(testFunctorA.GetCount() == 1); |
| 236 | BOOST_CHECK(testFunctorB.GetCount() == 1); |
| 237 | BOOST_CHECK(testFunctorC.GetCount() == 1); |
| 238 | |
| 239 | // Re-register an existing key with a new function |
| 240 | registry.RegisterFunctor(&testFunctorC, testFunctorA.GetPacketId(), version); |
| 241 | registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetC); |
| 242 | BOOST_CHECK(testFunctorA.GetCount() == 1); |
| 243 | BOOST_CHECK(testFunctorB.GetCount() == 1); |
| 244 | BOOST_CHECK(testFunctorC.GetCount() == 2); |
| 245 | |
| 246 | // Check that non-existent key returns nullptr for its functor |
| 247 | BOOST_CHECK_THROW(registry.GetFunctor(0, 0), armnn::Exception); |
| 248 | } |
| 249 | |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 250 | BOOST_AUTO_TEST_CASE(CheckPacketVersionResolver) |
| 251 | { |
| 252 | // Set up random number generator for generating packetId values |
| 253 | std::random_device device; |
| 254 | std::mt19937 generator(device()); |
| 255 | std::uniform_int_distribution<uint32_t> distribution(std::numeric_limits<uint32_t>::min(), |
| 256 | std::numeric_limits<uint32_t>::max()); |
| 257 | |
| 258 | // NOTE: Expected version is always 1.0.0, regardless of packetId |
| 259 | const Version expectedVersion(1, 0, 0); |
| 260 | |
| 261 | PacketVersionResolver packetVersionResolver; |
| 262 | |
| 263 | constexpr unsigned int numTests = 10u; |
| 264 | |
| 265 | for (unsigned int i = 0u; i < numTests; ++i) |
| 266 | { |
| 267 | const uint32_t packetId = distribution(generator); |
| 268 | Version resolvedVersion = packetVersionResolver.ResolvePacketVersion(packetId); |
| 269 | |
| 270 | BOOST_TEST(resolvedVersion == expectedVersion); |
| 271 | } |
| 272 | } |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 273 | void ProfilingCurrentStateThreadImpl(ProfilingStateMachine& states) |
| 274 | { |
| 275 | ProfilingState newState = ProfilingState::NotConnected; |
| 276 | states.GetCurrentState(); |
| 277 | states.TransitionToState(newState); |
| 278 | } |
| 279 | |
| 280 | BOOST_AUTO_TEST_CASE(CheckProfilingStateMachine) |
| 281 | { |
| 282 | ProfilingStateMachine profilingState1(ProfilingState::Uninitialised); |
| 283 | profilingState1.TransitionToState(ProfilingState::Uninitialised); |
| 284 | BOOST_CHECK(profilingState1.GetCurrentState() == ProfilingState::Uninitialised); |
| 285 | |
| 286 | ProfilingStateMachine profilingState2(ProfilingState::Uninitialised); |
| 287 | profilingState2.TransitionToState(ProfilingState::NotConnected); |
| 288 | BOOST_CHECK(profilingState2.GetCurrentState() == ProfilingState::NotConnected); |
| 289 | |
| 290 | ProfilingStateMachine profilingState3(ProfilingState::NotConnected); |
| 291 | profilingState3.TransitionToState(ProfilingState::NotConnected); |
| 292 | BOOST_CHECK(profilingState3.GetCurrentState() == ProfilingState::NotConnected); |
| 293 | |
| 294 | ProfilingStateMachine profilingState4(ProfilingState::NotConnected); |
| 295 | profilingState4.TransitionToState(ProfilingState::WaitingForAck); |
| 296 | BOOST_CHECK(profilingState4.GetCurrentState() == ProfilingState::WaitingForAck); |
| 297 | |
| 298 | ProfilingStateMachine profilingState5(ProfilingState::WaitingForAck); |
| 299 | profilingState5.TransitionToState(ProfilingState::WaitingForAck); |
| 300 | BOOST_CHECK(profilingState5.GetCurrentState() == ProfilingState::WaitingForAck); |
| 301 | |
| 302 | ProfilingStateMachine profilingState6(ProfilingState::WaitingForAck); |
| 303 | profilingState6.TransitionToState(ProfilingState::Active); |
| 304 | BOOST_CHECK(profilingState6.GetCurrentState() == ProfilingState::Active); |
| 305 | |
| 306 | ProfilingStateMachine profilingState7(ProfilingState::Active); |
| 307 | profilingState7.TransitionToState(ProfilingState::NotConnected); |
| 308 | BOOST_CHECK(profilingState7.GetCurrentState() == ProfilingState::NotConnected); |
| 309 | |
| 310 | ProfilingStateMachine profilingState8(ProfilingState::Active); |
| 311 | profilingState8.TransitionToState(ProfilingState::Active); |
| 312 | BOOST_CHECK(profilingState8.GetCurrentState() == ProfilingState::Active); |
| 313 | |
| 314 | ProfilingStateMachine profilingState9(ProfilingState::Uninitialised); |
| 315 | BOOST_CHECK_THROW(profilingState9.TransitionToState(ProfilingState::WaitingForAck), |
| 316 | armnn::Exception); |
| 317 | |
| 318 | ProfilingStateMachine profilingState10(ProfilingState::Uninitialised); |
| 319 | BOOST_CHECK_THROW(profilingState10.TransitionToState(ProfilingState::Active), |
| 320 | armnn::Exception); |
| 321 | |
| 322 | ProfilingStateMachine profilingState11(ProfilingState::NotConnected); |
| 323 | BOOST_CHECK_THROW(profilingState11.TransitionToState(ProfilingState::Uninitialised), |
| 324 | armnn::Exception); |
| 325 | |
| 326 | ProfilingStateMachine profilingState12(ProfilingState::NotConnected); |
| 327 | BOOST_CHECK_THROW(profilingState12.TransitionToState(ProfilingState::Active), |
| 328 | armnn::Exception); |
| 329 | |
| 330 | ProfilingStateMachine profilingState13(ProfilingState::WaitingForAck); |
| 331 | BOOST_CHECK_THROW(profilingState13.TransitionToState(ProfilingState::Uninitialised), |
| 332 | armnn::Exception); |
| 333 | |
| 334 | ProfilingStateMachine profilingState14(ProfilingState::WaitingForAck); |
| 335 | BOOST_CHECK_THROW(profilingState14.TransitionToState(ProfilingState::NotConnected), |
| 336 | armnn::Exception); |
| 337 | |
| 338 | ProfilingStateMachine profilingState15(ProfilingState::Active); |
| 339 | BOOST_CHECK_THROW(profilingState15.TransitionToState(ProfilingState::Uninitialised), |
| 340 | armnn::Exception); |
| 341 | |
| 342 | ProfilingStateMachine profilingState16(armnn::profiling::ProfilingState::Active); |
| 343 | BOOST_CHECK_THROW(profilingState16.TransitionToState(ProfilingState::WaitingForAck), |
| 344 | armnn::Exception); |
| 345 | |
| 346 | ProfilingStateMachine profilingState17(ProfilingState::Uninitialised); |
| 347 | |
| 348 | std::thread thread1 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17)); |
| 349 | std::thread thread2 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17)); |
| 350 | std::thread thread3 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17)); |
| 351 | std::thread thread4 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17)); |
| 352 | std::thread thread5 (ProfilingCurrentStateThreadImpl,std::ref(profilingState17)); |
| 353 | |
| 354 | thread1.join(); |
| 355 | thread2.join(); |
| 356 | thread3.join(); |
| 357 | thread4.join(); |
| 358 | thread5.join(); |
| 359 | |
| 360 | BOOST_TEST((profilingState17.GetCurrentState() == ProfilingState::NotConnected)); |
| 361 | } |
Aron Virginas-Tar | e898db9 | 2019-08-22 12:56:34 +0100 | [diff] [blame] | 362 | |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 363 | void CaptureDataWriteThreadImpl(Holder &holder, uint32_t capturePeriod, std::vector<uint16_t>& counterIds) |
| 364 | { |
| 365 | holder.SetCaptureData(capturePeriod, counterIds); |
| 366 | } |
| 367 | |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 368 | void CaptureDataReadThreadImpl(const Holder& holder, CaptureData& captureData) |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 369 | { |
| 370 | captureData = holder.GetCaptureData(); |
| 371 | } |
| 372 | |
| 373 | BOOST_AUTO_TEST_CASE(CheckCaptureDataHolder) |
| 374 | { |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 375 | std::map<uint32_t, std::vector<uint16_t>> periodIdMap; |
| 376 | std::vector<uint16_t> counterIds; |
| 377 | uint16_t numThreads = 50; |
| 378 | for (uint16_t i = 0; i < numThreads; ++i) |
| 379 | { |
| 380 | counterIds.emplace_back(i); |
| 381 | periodIdMap.insert(std::make_pair(i, counterIds)); |
| 382 | } |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 383 | |
| 384 | // Check CaptureData functions |
| 385 | CaptureData capture; |
| 386 | BOOST_CHECK(capture.GetCapturePeriod() == 0); |
| 387 | BOOST_CHECK((capture.GetCounterIds()).empty()); |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 388 | capture.SetCapturePeriod(0); |
| 389 | capture.SetCounterIds(periodIdMap[0]); |
| 390 | BOOST_CHECK(capture.GetCapturePeriod() == 0); |
| 391 | BOOST_CHECK(capture.GetCounterIds() == periodIdMap[0]); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 392 | |
| 393 | Holder holder; |
| 394 | BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 0); |
| 395 | BOOST_CHECK(((holder.GetCaptureData()).GetCounterIds()).empty()); |
| 396 | |
| 397 | // Check Holder functions |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 398 | std::thread thread1(CaptureDataWriteThreadImpl, std::ref(holder), 2, std::ref(periodIdMap[2])); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 399 | thread1.join(); |
| 400 | |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 401 | BOOST_CHECK((holder.GetCaptureData()).GetCapturePeriod() == 2); |
| 402 | BOOST_CHECK((holder.GetCaptureData()).GetCounterIds() == periodIdMap[2]); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 403 | |
| 404 | CaptureData captureData; |
| 405 | std::thread thread2(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData)); |
| 406 | thread2.join(); |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 407 | BOOST_CHECK(captureData.GetCounterIds() == periodIdMap[2]); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 408 | |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 409 | std::vector<std::thread> threadsVect; |
| 410 | for (int i = 0; i < numThreads; i+=2) |
| 411 | { |
| 412 | threadsVect.emplace_back(std::thread(CaptureDataWriteThreadImpl, |
| 413 | std::ref(holder), |
| 414 | i, |
| 415 | std::ref(periodIdMap[static_cast<uint16_t >(i)]))); |
Francis Murtagh | 0696569 | 2019-09-05 16:29:01 +0100 | [diff] [blame] | 416 | |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 417 | threadsVect.emplace_back(std::thread(CaptureDataReadThreadImpl, |
| 418 | std::ref(holder), |
| 419 | std::ref(captureData))); |
| 420 | } |
| 421 | |
| 422 | for (uint16_t i = 0; i < numThreads; ++i) |
| 423 | { |
| 424 | threadsVect[i].join(); |
| 425 | } |
| 426 | |
| 427 | std::vector<std::thread> readThreadsVect; |
| 428 | for (uint16_t i = 0; i < numThreads; ++i) |
| 429 | { |
| 430 | readThreadsVect.emplace_back( |
| 431 | std::thread(CaptureDataReadThreadImpl, std::ref(holder), std::ref(captureData))); |
| 432 | } |
| 433 | |
| 434 | for (uint16_t i = 0; i < numThreads; ++i) |
| 435 | { |
| 436 | readThreadsVect[i].join(); |
| 437 | } |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 438 | |
| 439 | // Check CaptureData was written/read correctly from multiple threads |
| 440 | std::vector<uint16_t> captureIds = captureData.GetCounterIds(); |
| 441 | uint32_t capturePeriod = captureData.GetCapturePeriod(); |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 442 | |
| 443 | BOOST_CHECK(captureIds == periodIdMap[capturePeriod]); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 444 | |
| 445 | std::vector<uint16_t> readIds = holder.GetCaptureData().GetCounterIds(); |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 446 | BOOST_CHECK(captureIds == readIds); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 447 | |
| 448 | // Check assignment operator |
| 449 | CaptureData assignableCaptureData; |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 450 | assignableCaptureData.SetCapturePeriod(3); |
| 451 | assignableCaptureData.SetCounterIds(periodIdMap[3]); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 452 | |
| 453 | CaptureData secondCaptureData; |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 454 | secondCaptureData.SetCapturePeriod(2); |
| 455 | secondCaptureData.SetCounterIds(periodIdMap[2]); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 456 | |
| 457 | BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 2); |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 458 | BOOST_CHECK(secondCaptureData.GetCounterIds() == periodIdMap[2]); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 459 | |
| 460 | secondCaptureData = assignableCaptureData; |
| 461 | BOOST_CHECK(secondCaptureData.GetCapturePeriod() == 3); |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 462 | BOOST_CHECK(secondCaptureData.GetCounterIds() == periodIdMap[3]); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 463 | |
| 464 | // Check copy constructor |
| 465 | CaptureData copyConstructedCaptureData(assignableCaptureData); |
| 466 | |
| 467 | BOOST_CHECK(copyConstructedCaptureData.GetCapturePeriod() == 3); |
Francis Murtagh | bd70716 | 2019-09-09 11:26:44 +0100 | [diff] [blame^] | 468 | BOOST_CHECK(copyConstructedCaptureData.GetCounterIds() == periodIdMap[3]); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 469 | } |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 470 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 471 | BOOST_AUTO_TEST_CASE(CheckProfilingServiceDisabled) |
| 472 | { |
| 473 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 474 | ProfilingService service(options); |
| 475 | BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised); |
| 476 | service.Run(); |
| 477 | BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised); |
| 478 | } |
| 479 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 480 | BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabled) |
| 481 | { |
| 482 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 483 | options.m_EnableProfiling = true; |
| 484 | ProfilingService service(options); |
| 485 | BOOST_CHECK(service.GetCurrentState() == ProfilingState::NotConnected); |
| 486 | service.Run(); |
| 487 | BOOST_CHECK(service.GetCurrentState() == ProfilingState::WaitingForAck); |
| 488 | } |
| 489 | |
| 490 | |
| 491 | BOOST_AUTO_TEST_CASE(CheckProfilingServiceEnabledRuntime) |
| 492 | { |
| 493 | armnn::Runtime::CreationOptions::ExternalProfilingOptions options; |
| 494 | ProfilingService service(options); |
| 495 | BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised); |
| 496 | service.Run(); |
| 497 | BOOST_CHECK(service.GetCurrentState() == ProfilingState::Uninitialised); |
| 498 | service.m_Options.m_EnableProfiling = true; |
| 499 | service.Run(); |
| 500 | BOOST_CHECK(service.GetCurrentState() == ProfilingState::NotConnected); |
| 501 | service.Run(); |
| 502 | BOOST_CHECK(service.GetCurrentState() == ProfilingState::WaitingForAck); |
Francis Murtagh | 68f78d8 | 2019-09-04 16:42:29 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Francis Murtagh | 1f7db45 | 2019-08-14 09:49:34 +0100 | [diff] [blame] | 505 | BOOST_AUTO_TEST_SUITE_END() |