David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 6 | #include "CounterDirectory.hpp" |
| 7 | #include "CounterIdMap.hpp" |
| 8 | #include "Holder.hpp" |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 9 | #include "MockBackend.hpp" |
| 10 | #include "MockBackendId.hpp" |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 11 | #include "PeriodicCounterCapture.hpp" |
| 12 | #include "PeriodicCounterSelectionCommandHandler.hpp" |
| 13 | #include "ProfilingStateMachine.hpp" |
| 14 | #include "ProfilingUtils.hpp" |
| 15 | #include "RequestCounterDirectoryCommandHandler.hpp" |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 16 | |
Sadik Armagan | ea41b57 | 2020-03-19 18:16:46 +0000 | [diff] [blame] | 17 | #include <test/TestUtils.hpp> |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 18 | |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 19 | #include <armnn/utility/IgnoreUnused.hpp> |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 20 | #include <armnn/BackendId.hpp> |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 21 | #include <armnn/Logging.hpp> |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 22 | #include <armnn/profiling/ISendTimelinePacket.hpp> |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 23 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 24 | #include <boost/numeric/conversion/cast.hpp> |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 25 | #include <boost/test/unit_test.hpp> |
| 26 | #include <vector> |
| 27 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 28 | #include <cstdint> |
| 29 | #include <limits> |
| 30 | #include <backends/BackendProfiling.hpp> |
| 31 | |
| 32 | using namespace armnn::profiling; |
| 33 | |
| 34 | class ReadCounterVals : public IReadCounterValues |
| 35 | { |
| 36 | virtual bool IsCounterRegistered(uint16_t counterUid) const override |
| 37 | { |
| 38 | return (counterUid > 4 && counterUid < 11); |
| 39 | } |
| 40 | virtual uint16_t GetCounterCount() const override |
| 41 | { |
| 42 | return 1; |
| 43 | } |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 44 | virtual uint32_t GetAbsoluteCounterValue(uint16_t counterUid) const override |
| 45 | { |
| 46 | return counterUid; |
| 47 | } |
| 48 | virtual uint32_t GetDeltaCounterValue(uint16_t counterUid) override |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 49 | { |
| 50 | return counterUid; |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | class MockBackendSendCounterPacket : public ISendCounterPacket |
| 55 | { |
| 56 | public: |
| 57 | using IndexValuePairsVector = std::vector<CounterValue>; |
| 58 | |
| 59 | /// Create and write a StreamMetaDataPacket in the buffer |
| 60 | virtual void SendStreamMetaDataPacket() {} |
| 61 | |
| 62 | /// Create and write a CounterDirectoryPacket from the parameters to the buffer. |
| 63 | virtual void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) |
| 64 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 65 | armnn::IgnoreUnused(counterDirectory); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /// Create and write a PeriodicCounterCapturePacket from the parameters to the buffer. |
| 69 | virtual void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) |
| 70 | { |
| 71 | m_timestamps.emplace_back(Timestamp{timestamp, values}); |
| 72 | } |
| 73 | |
| 74 | /// Create and write a PeriodicCounterSelectionPacket from the parameters to the buffer. |
| 75 | virtual void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 76 | const std::vector<uint16_t>& selectedCounterIds) |
| 77 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 78 | armnn::IgnoreUnused(capturePeriod); |
| 79 | armnn::IgnoreUnused(selectedCounterIds); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | std::vector<Timestamp> GetTimestamps() |
| 83 | { |
| 84 | return m_timestamps; |
| 85 | } |
| 86 | |
| 87 | void ClearTimestamps() |
| 88 | { |
| 89 | m_timestamps.clear(); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | std::vector<Timestamp> m_timestamps; |
| 94 | }; |
| 95 | |
| 96 | Packet PacketWriter(uint32_t period, std::vector<uint16_t> countervalues) |
| 97 | { |
| 98 | const uint32_t packetId = 0x40000; |
| 99 | uint32_t offset = 0; |
| 100 | uint32_t dataLength = static_cast<uint32_t>(4 + countervalues.size() * 2); |
| 101 | std::unique_ptr<unsigned char[]> uniqueData = std::make_unique<unsigned char[]>(dataLength); |
| 102 | unsigned char* data1 = reinterpret_cast<unsigned char*>(uniqueData.get()); |
| 103 | |
| 104 | WriteUint32(data1, offset, period); |
| 105 | offset += 4; |
| 106 | for (auto countervalue : countervalues) |
| 107 | { |
| 108 | WriteUint16(data1, offset, countervalue); |
| 109 | offset += 2; |
| 110 | } |
| 111 | |
| 112 | return {packetId, dataLength, uniqueData}; |
| 113 | } |
| 114 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 115 | BOOST_AUTO_TEST_SUITE(BackendProfilingTestSuite) |
| 116 | |
| 117 | BOOST_AUTO_TEST_CASE(BackendProfilingCounterRegisterMockBackendTest) |
| 118 | { |
| 119 | // Reset the profiling service to the uninitialized state |
| 120 | armnn::IRuntime::CreationOptions options; |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 121 | options.m_ProfilingOptions.m_EnableProfiling = true; |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 122 | |
| 123 | armnn::MockBackendInitialiser initialiser; |
| 124 | // Create a runtime |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 125 | armnn::Runtime runtime(options); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 126 | |
| 127 | // Check if the MockBackends 3 dummy counters {0, 1, 2-5 (four cores)} are registered |
| 128 | armnn::BackendId mockId = armnn::MockBackendId(); |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 129 | const armnn::profiling::ICounterMappings& counterMap = GetProfilingService(&runtime).GetCounterMappings(); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 130 | BOOST_CHECK(counterMap.GetGlobalId(0, mockId) == 5); |
| 131 | BOOST_CHECK(counterMap.GetGlobalId(1, mockId) == 6); |
| 132 | BOOST_CHECK(counterMap.GetGlobalId(2, mockId) == 7); |
| 133 | BOOST_CHECK(counterMap.GetGlobalId(3, mockId) == 8); |
| 134 | BOOST_CHECK(counterMap.GetGlobalId(4, mockId) == 9); |
| 135 | BOOST_CHECK(counterMap.GetGlobalId(5, mockId) == 10); |
| 136 | options.m_ProfilingOptions.m_EnableProfiling = false; |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 137 | GetProfilingService(&runtime).ResetExternalProfilingOptions(options.m_ProfilingOptions, true); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 140 | BOOST_AUTO_TEST_CASE(TestBackendCounters) |
| 141 | { |
| 142 | Holder holder; |
| 143 | PacketVersionResolver packetVersionResolver; |
| 144 | ProfilingStateMachine stateMachine; |
| 145 | ReadCounterVals readCounterVals; |
| 146 | CounterIdMap counterIdMap; |
| 147 | MockBackendSendCounterPacket sendCounterPacket; |
| 148 | |
| 149 | const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc); |
| 150 | const armnn::BackendId gpuAccId(armnn::Compute::GpuAcc); |
| 151 | |
| 152 | armnn::IRuntime::CreationOptions options; |
| 153 | options.m_ProfilingOptions.m_EnableProfiling = true; |
| 154 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 155 | armnn::profiling::ProfilingService profilingService; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 156 | |
| 157 | std::unique_ptr<armnn::profiling::IBackendProfiling> cpuBackendProfilingPtr = |
| 158 | std::make_unique<BackendProfiling>(options, profilingService, cpuAccId); |
| 159 | std::unique_ptr<armnn::profiling::IBackendProfiling> gpuBackendProfilingPtr = |
| 160 | std::make_unique<BackendProfiling>(options, profilingService, gpuAccId); |
| 161 | |
| 162 | std::shared_ptr<armnn::profiling::IBackendProfilingContext> cpuProfilingContextPtr = |
| 163 | std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr); |
| 164 | std::shared_ptr<armnn::profiling::IBackendProfilingContext> gpuProfilingContextPtr = |
| 165 | std::make_shared<armnn::MockBackendProfilingContext>(gpuBackendProfilingPtr); |
| 166 | |
| 167 | std::unordered_map<armnn::BackendId, |
| 168 | std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContexts; |
| 169 | |
| 170 | backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr; |
| 171 | backendProfilingContexts[gpuAccId] = gpuProfilingContextPtr; |
| 172 | |
| 173 | uint16_t globalId = 5; |
| 174 | |
| 175 | counterIdMap.RegisterMapping(globalId++, 0, cpuAccId); |
| 176 | counterIdMap.RegisterMapping(globalId++, 1, cpuAccId); |
| 177 | counterIdMap.RegisterMapping(globalId++, 2, cpuAccId); |
| 178 | |
| 179 | counterIdMap.RegisterMapping(globalId++, 0, gpuAccId); |
| 180 | counterIdMap.RegisterMapping(globalId++, 1, gpuAccId); |
| 181 | counterIdMap.RegisterMapping(globalId++, 2, gpuAccId); |
| 182 | |
| 183 | backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr; |
| 184 | backendProfilingContexts[gpuAccId] = gpuProfilingContextPtr; |
| 185 | |
| 186 | PeriodicCounterCapture periodicCounterCapture(holder, sendCounterPacket, readCounterVals, |
| 187 | counterIdMap, backendProfilingContexts); |
| 188 | |
| 189 | uint16_t maxArmnnCounterId = 4; |
| 190 | |
| 191 | PeriodicCounterSelectionCommandHandler periodicCounterSelectionCommandHandler(0, |
| 192 | 4, |
| 193 | packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), |
| 194 | backendProfilingContexts, |
| 195 | counterIdMap, |
| 196 | holder, |
| 197 | maxArmnnCounterId, |
| 198 | periodicCounterCapture, |
| 199 | readCounterVals, |
| 200 | sendCounterPacket, |
| 201 | stateMachine); |
| 202 | |
| 203 | stateMachine.TransitionToState(ProfilingState::NotConnected); |
| 204 | stateMachine.TransitionToState(ProfilingState::WaitingForAck); |
| 205 | stateMachine.TransitionToState(ProfilingState::Active); |
| 206 | |
| 207 | uint32_t period = 12345u; |
| 208 | |
| 209 | std::vector<uint16_t> cpuCounters{5, 6, 7}; |
| 210 | std::vector<uint16_t> gpuCounters{8, 9, 10}; |
| 211 | |
| 212 | // Request only gpu counters |
| 213 | periodicCounterSelectionCommandHandler(PacketWriter(period, gpuCounters)); |
| 214 | periodicCounterCapture.Stop(); |
| 215 | |
| 216 | std::set<armnn::BackendId> activeIds = holder.GetCaptureData().GetActiveBackends(); |
| 217 | BOOST_CHECK(activeIds.size() == 1); |
| 218 | BOOST_CHECK(activeIds.find(gpuAccId) != activeIds.end()); |
| 219 | |
| 220 | std::vector<Timestamp> recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 221 | |
| 222 | BOOST_CHECK(recievedTimestamp[0].timestamp == period); |
| 223 | BOOST_CHECK(recievedTimestamp.size() == 1); |
| 224 | BOOST_CHECK(recievedTimestamp[0].counterValues.size() == gpuCounters.size()); |
| 225 | for (unsigned long i=0; i< gpuCounters.size(); ++i) |
| 226 | { |
| 227 | BOOST_CHECK(recievedTimestamp[0].counterValues[i].counterId == gpuCounters[i]); |
| 228 | BOOST_CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u); |
| 229 | } |
| 230 | sendCounterPacket.ClearTimestamps(); |
| 231 | |
| 232 | // Request only cpu counters |
| 233 | periodicCounterSelectionCommandHandler(PacketWriter(period, cpuCounters)); |
| 234 | periodicCounterCapture.Stop(); |
| 235 | |
| 236 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
| 237 | BOOST_CHECK(activeIds.size() == 1); |
| 238 | BOOST_CHECK(activeIds.find(cpuAccId) != activeIds.end()); |
| 239 | |
| 240 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 241 | |
| 242 | BOOST_CHECK(recievedTimestamp[0].timestamp == period); |
| 243 | BOOST_CHECK(recievedTimestamp.size() == 1); |
| 244 | BOOST_CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size()); |
| 245 | for (unsigned long i=0; i< cpuCounters.size(); ++i) |
| 246 | { |
| 247 | BOOST_CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]); |
| 248 | BOOST_CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u); |
| 249 | } |
| 250 | sendCounterPacket.ClearTimestamps(); |
| 251 | |
| 252 | // Request combination of cpu & gpu counters with new period |
| 253 | period = 12222u; |
| 254 | periodicCounterSelectionCommandHandler(PacketWriter(period, {cpuCounters[0], gpuCounters[2], |
| 255 | gpuCounters[1], cpuCounters[1], gpuCounters[0]})); |
| 256 | periodicCounterCapture.Stop(); |
| 257 | |
| 258 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
| 259 | BOOST_CHECK(activeIds.size() == 2); |
| 260 | BOOST_CHECK(activeIds.find(cpuAccId) != activeIds.end()); |
| 261 | BOOST_CHECK(activeIds.find(gpuAccId) != activeIds.end()); |
| 262 | |
| 263 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 264 | |
| 265 | BOOST_CHECK(recievedTimestamp[0].timestamp == period); |
| 266 | BOOST_CHECK(recievedTimestamp[1].timestamp == period); |
| 267 | |
| 268 | BOOST_CHECK(recievedTimestamp.size() == 2); |
| 269 | BOOST_CHECK(recievedTimestamp[0].counterValues.size() == 2); |
| 270 | BOOST_CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size()); |
| 271 | |
| 272 | BOOST_CHECK(recievedTimestamp[0].counterValues[0].counterId == cpuCounters[0]); |
| 273 | BOOST_CHECK(recievedTimestamp[0].counterValues[0].counterValue == 1u); |
| 274 | BOOST_CHECK(recievedTimestamp[0].counterValues[1].counterId == cpuCounters[1]); |
| 275 | BOOST_CHECK(recievedTimestamp[0].counterValues[1].counterValue == 2u); |
| 276 | |
| 277 | for (unsigned long i=0; i< gpuCounters.size(); ++i) |
| 278 | { |
| 279 | BOOST_CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]); |
| 280 | BOOST_CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u); |
| 281 | } |
| 282 | |
| 283 | sendCounterPacket.ClearTimestamps(); |
| 284 | |
| 285 | // Request all counters |
| 286 | std::vector<uint16_t> counterValues; |
| 287 | counterValues.insert(counterValues.begin(), cpuCounters.begin(), cpuCounters.end()); |
| 288 | counterValues.insert(counterValues.begin(), gpuCounters.begin(), gpuCounters.end()); |
| 289 | |
| 290 | periodicCounterSelectionCommandHandler(PacketWriter(period, counterValues)); |
| 291 | periodicCounterCapture.Stop(); |
| 292 | |
| 293 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
| 294 | BOOST_CHECK(activeIds.size() == 2); |
| 295 | BOOST_CHECK(activeIds.find(cpuAccId) != activeIds.end()); |
| 296 | BOOST_CHECK(activeIds.find(gpuAccId) != activeIds.end()); |
| 297 | |
| 298 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 299 | |
| 300 | BOOST_CHECK(recievedTimestamp[0].counterValues.size() == cpuCounters.size()); |
| 301 | for (unsigned long i=0; i< cpuCounters.size(); ++i) |
| 302 | { |
| 303 | BOOST_CHECK(recievedTimestamp[0].counterValues[i].counterId == cpuCounters[i]); |
| 304 | BOOST_CHECK(recievedTimestamp[0].counterValues[i].counterValue == i + 1u); |
| 305 | } |
| 306 | |
| 307 | BOOST_CHECK(recievedTimestamp[1].counterValues.size() == gpuCounters.size()); |
| 308 | for (unsigned long i=0; i< gpuCounters.size(); ++i) |
| 309 | { |
| 310 | BOOST_CHECK(recievedTimestamp[1].counterValues[i].counterId == gpuCounters[i]); |
| 311 | BOOST_CHECK(recievedTimestamp[1].counterValues[i].counterValue == i + 1u); |
| 312 | } |
| 313 | sendCounterPacket.ClearTimestamps(); |
| 314 | |
| 315 | // Request random counters with duplicates and invalid counters |
| 316 | counterValues = {0, 0, 200, cpuCounters[2], gpuCounters[0],3 ,30, cpuCounters[0],cpuCounters[2], gpuCounters[1], 3, |
| 317 | 90, 0, 30, gpuCounters[0], gpuCounters[0]}; |
| 318 | |
| 319 | periodicCounterSelectionCommandHandler(PacketWriter(period, counterValues)); |
| 320 | periodicCounterCapture.Stop(); |
| 321 | |
| 322 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
| 323 | BOOST_CHECK(activeIds.size() == 2); |
| 324 | BOOST_CHECK(activeIds.find(cpuAccId) != activeIds.end()); |
| 325 | BOOST_CHECK(activeIds.find(gpuAccId) != activeIds.end()); |
| 326 | |
| 327 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 328 | |
| 329 | BOOST_CHECK(recievedTimestamp.size() == 2); |
| 330 | |
| 331 | BOOST_CHECK(recievedTimestamp[0].counterValues.size() == 2); |
| 332 | |
| 333 | BOOST_CHECK(recievedTimestamp[0].counterValues[0].counterId == cpuCounters[0]); |
| 334 | BOOST_CHECK(recievedTimestamp[0].counterValues[0].counterValue == 1u); |
| 335 | BOOST_CHECK(recievedTimestamp[0].counterValues[1].counterId == cpuCounters[2]); |
| 336 | BOOST_CHECK(recievedTimestamp[0].counterValues[1].counterValue == 3u); |
| 337 | |
| 338 | BOOST_CHECK(recievedTimestamp[1].counterValues.size() == 2); |
| 339 | |
| 340 | BOOST_CHECK(recievedTimestamp[1].counterValues[0].counterId == gpuCounters[0]); |
| 341 | BOOST_CHECK(recievedTimestamp[1].counterValues[0].counterValue == 1u); |
| 342 | BOOST_CHECK(recievedTimestamp[1].counterValues[1].counterId == gpuCounters[1]); |
| 343 | BOOST_CHECK(recievedTimestamp[1].counterValues[1].counterValue == 2u); |
| 344 | |
| 345 | sendCounterPacket.ClearTimestamps(); |
| 346 | |
| 347 | // Request no counters |
| 348 | periodicCounterSelectionCommandHandler(PacketWriter(period, {})); |
| 349 | periodicCounterCapture.Stop(); |
| 350 | |
| 351 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
| 352 | BOOST_CHECK(activeIds.size() == 0); |
| 353 | |
| 354 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 355 | BOOST_CHECK(recievedTimestamp.size() == 0); |
| 356 | |
| 357 | sendCounterPacket.ClearTimestamps(); |
| 358 | |
| 359 | // Request period of zero |
| 360 | periodicCounterSelectionCommandHandler(PacketWriter(0, counterValues)); |
| 361 | periodicCounterCapture.Stop(); |
| 362 | |
| 363 | activeIds = holder.GetCaptureData().GetActiveBackends(); |
| 364 | BOOST_CHECK(activeIds.size() == 0); |
| 365 | |
| 366 | recievedTimestamp = sendCounterPacket.GetTimestamps(); |
| 367 | BOOST_CHECK(recievedTimestamp.size() == 0); |
| 368 | } |
| 369 | |
| 370 | BOOST_AUTO_TEST_CASE(TestBackendCounterLogging) |
| 371 | { |
| 372 | std::stringstream ss; |
| 373 | |
| 374 | struct StreamRedirector |
| 375 | { |
| 376 | public: |
| 377 | StreamRedirector(std::ostream &stream, std::streambuf *newStreamBuffer) |
| 378 | : m_Stream(stream), m_BackupBuffer(m_Stream.rdbuf(newStreamBuffer)) |
| 379 | {} |
| 380 | |
| 381 | ~StreamRedirector() |
| 382 | { m_Stream.rdbuf(m_BackupBuffer); } |
| 383 | |
| 384 | private: |
| 385 | std::ostream &m_Stream; |
| 386 | std::streambuf *m_BackupBuffer; |
| 387 | }; |
| 388 | |
| 389 | Holder holder; |
| 390 | PacketVersionResolver packetVersionResolver; |
| 391 | ProfilingStateMachine stateMachine; |
| 392 | ReadCounterVals readCounterVals; |
| 393 | StreamRedirector redirect(std::cout, ss.rdbuf()); |
| 394 | CounterIdMap counterIdMap; |
| 395 | MockBackendSendCounterPacket sendCounterPacket; |
| 396 | |
| 397 | const armnn::BackendId cpuAccId(armnn::Compute::CpuAcc); |
| 398 | const armnn::BackendId gpuAccId(armnn::Compute::GpuAcc); |
| 399 | |
| 400 | armnn::IRuntime::CreationOptions options; |
| 401 | options.m_ProfilingOptions.m_EnableProfiling = true; |
| 402 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 403 | armnn::profiling::ProfilingService profilingService; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 404 | |
| 405 | std::unique_ptr<armnn::profiling::IBackendProfiling> cpuBackendProfilingPtr = |
| 406 | std::make_unique<BackendProfiling>(options, profilingService, cpuAccId); |
| 407 | |
| 408 | std::shared_ptr<armnn::profiling::IBackendProfilingContext> cpuProfilingContextPtr = |
| 409 | std::make_shared<armnn::MockBackendProfilingContext>(cpuBackendProfilingPtr); |
| 410 | |
| 411 | std::unordered_map<armnn::BackendId, |
| 412 | std::shared_ptr<armnn::profiling::IBackendProfilingContext>> backendProfilingContexts; |
| 413 | |
| 414 | uint16_t globalId = 5; |
| 415 | counterIdMap.RegisterMapping(globalId, 0, cpuAccId); |
| 416 | backendProfilingContexts[cpuAccId] = cpuProfilingContextPtr; |
| 417 | |
| 418 | PeriodicCounterCapture periodicCounterCapture(holder, sendCounterPacket, readCounterVals, |
| 419 | counterIdMap, backendProfilingContexts); |
| 420 | |
| 421 | uint16_t maxArmnnCounterId = 4; |
| 422 | |
| 423 | PeriodicCounterSelectionCommandHandler periodicCounterSelectionCommandHandler(0, |
| 424 | 4, |
| 425 | packetVersionResolver.ResolvePacketVersion(0, 4).GetEncodedValue(), |
| 426 | backendProfilingContexts, |
| 427 | counterIdMap, |
| 428 | holder, |
| 429 | maxArmnnCounterId, |
| 430 | periodicCounterCapture, |
| 431 | readCounterVals, |
| 432 | sendCounterPacket, |
| 433 | stateMachine); |
| 434 | |
| 435 | stateMachine.TransitionToState(ProfilingState::NotConnected); |
| 436 | stateMachine.TransitionToState(ProfilingState::WaitingForAck); |
| 437 | stateMachine.TransitionToState(ProfilingState::Active); |
| 438 | |
| 439 | uint32_t period = 15939u; |
| 440 | |
| 441 | armnn::SetAllLoggingSinks(true, false, false); |
| 442 | SetLogFilter(armnn::LogSeverity::Warning); |
| 443 | periodicCounterSelectionCommandHandler(PacketWriter(period, {5})); |
| 444 | periodicCounterCapture.Stop(); |
| 445 | SetLogFilter(armnn::LogSeverity::Fatal); |
| 446 | |
David Monahan | a8837bf | 2020-04-16 10:01:56 +0100 | [diff] [blame] | 447 | BOOST_CHECK(ss.str().find("ActivateCounters example test error") != std::string::npos); |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 450 | BOOST_AUTO_TEST_CASE(BackendProfilingContextGetSendTimelinePacket) |
| 451 | { |
| 452 | // Reset the profiling service to the uninitialized state |
| 453 | armnn::IRuntime::CreationOptions options; |
| 454 | options.m_ProfilingOptions.m_EnableProfiling = true; |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 455 | armnn::profiling::ProfilingService profilingService; |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 456 | profilingService.ConfigureProfilingService(options.m_ProfilingOptions, true); |
| 457 | |
| 458 | armnn::MockBackendInitialiser initialiser; |
| 459 | // Create a runtime. During this the mock backend will be registered and context returned. |
| 460 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 461 | armnn::MockBackendProfilingService mockProfilingService = armnn::MockBackendProfilingService::Instance(); |
| 462 | armnn::MockBackendProfilingContext *mockBackEndProfilingContext = mockProfilingService.GetContext(); |
| 463 | // Check that there is a valid context set. |
| 464 | BOOST_CHECK(mockBackEndProfilingContext); |
| 465 | armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface = |
| 466 | mockBackEndProfilingContext->GetBackendProfiling(); |
| 467 | BOOST_CHECK(backendProfilingIface); |
| 468 | |
| 469 | // Now for the meat of the test. We're just going to send a random packet and make sure there |
| 470 | // are no exceptions or errors. The sending of packets is already tested in SendTimelinePacketTests. |
| 471 | std::unique_ptr<armnn::profiling::ISendTimelinePacket> timelinePacket = |
| 472 | backendProfilingIface->GetSendTimelinePacket(); |
| 473 | // Send TimelineEntityClassBinaryPacket |
| 474 | const uint64_t entityBinaryPacketProfilingGuid = 123456u; |
| 475 | timelinePacket->SendTimelineEntityBinaryPacket(entityBinaryPacketProfilingGuid); |
| 476 | timelinePacket->Commit(); |
| 477 | |
| 478 | // Reset the profiling servie after the test. |
| 479 | options.m_ProfilingOptions.m_EnableProfiling = false; |
| 480 | profilingService.ResetExternalProfilingOptions(options.m_ProfilingOptions, true); |
| 481 | } |
| 482 | |
| 483 | BOOST_AUTO_TEST_CASE(GetProfilingGuidGenerator) |
| 484 | { |
| 485 | // Reset the profiling service to the uninitialized state |
| 486 | armnn::IRuntime::CreationOptions options; |
| 487 | options.m_ProfilingOptions.m_EnableProfiling = true; |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 488 | |
| 489 | armnn::MockBackendInitialiser initialiser; |
| 490 | // Create a runtime. During this the mock backend will be registered and context returned. |
| 491 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 492 | armnn::MockBackendProfilingService mockProfilingService = armnn::MockBackendProfilingService::Instance(); |
| 493 | armnn::MockBackendProfilingContext *mockBackEndProfilingContext = mockProfilingService.GetContext(); |
| 494 | // Check that there is a valid context set. |
| 495 | BOOST_CHECK(mockBackEndProfilingContext); |
| 496 | armnn::IBackendInternal::IBackendProfilingPtr& backendProfilingIface = |
| 497 | mockBackEndProfilingContext->GetBackendProfiling(); |
| 498 | BOOST_CHECK(backendProfilingIface); |
| 499 | |
| 500 | // Get the Guid generator and check the getting two Guid's results in the second being greater than the first. |
| 501 | armnn::profiling::IProfilingGuidGenerator& guidGenerator = backendProfilingIface->GetProfilingGuidGenerator(); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 502 | const armnn::profiling::ProfilingDynamicGuid& firstGuid = guidGenerator.NextGuid(); |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 503 | const armnn::profiling::ProfilingDynamicGuid& secondGuid = guidGenerator.NextGuid(); |
| 504 | BOOST_CHECK(secondGuid > firstGuid); |
| 505 | |
| 506 | // Reset the profiling servie after the test. |
| 507 | options.m_ProfilingOptions.m_EnableProfiling = false; |
Colm Donelan | fcb802b | 2020-02-13 20:47:08 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | BOOST_AUTO_TEST_SUITE_END() |