Keith Davis | 02356de | 2019-08-26 18:28:17 +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. |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ProfilingService.hpp" |
| 7 | |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 8 | #include <armnn/BackendId.hpp> |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 9 | #include <armnn/Logging.hpp> |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 10 | #include <armnn/utility/NumericCast.hpp> |
| 11 | |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 12 | #include <common/include/SocketConnectionException.hpp> |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 13 | |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 14 | #include <fmt/format.h> |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 15 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 16 | namespace armnn |
| 17 | { |
| 18 | |
| 19 | namespace profiling |
| 20 | { |
| 21 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 22 | ProfilingGuidGenerator ProfilingService::m_GuidGenerator; |
| 23 | |
| 24 | ProfilingDynamicGuid ProfilingService::GetNextGuid() |
| 25 | { |
| 26 | return m_GuidGenerator.NextGuid(); |
| 27 | } |
| 28 | |
| 29 | ProfilingStaticGuid ProfilingService::GetStaticId(const std::string& str) |
| 30 | { |
| 31 | return m_GuidGenerator.GenerateStaticId(str); |
| 32 | } |
| 33 | |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 34 | void ProfilingService::ResetGuidGenerator() |
| 35 | { |
| 36 | m_GuidGenerator.Reset(); |
| 37 | } |
| 38 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 39 | void ProfilingService::ResetExternalProfilingOptions(const ExternalProfilingOptions& options, |
| 40 | bool resetProfilingService) |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 41 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 42 | // Update the profiling options |
| 43 | m_Options = options; |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 44 | m_TimelineReporting = options.m_TimelineEnabled; |
Finn Williams | d7fcafa | 2020-04-23 17:55:18 +0100 | [diff] [blame] | 45 | m_ConnectionAcknowledgedCommandHandler.setTimelineEnabled(options.m_TimelineEnabled); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 46 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 47 | // Check if the profiling service needs to be reset |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 48 | if (resetProfilingService) |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 49 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 50 | // Reset the profiling service |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 51 | Reset(); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 55 | bool ProfilingService::IsProfilingEnabled() const |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 56 | { |
| 57 | return m_Options.m_EnableProfiling; |
| 58 | } |
| 59 | |
Jim Flynn | 672d06e | 2019-10-15 10:18:11 +0100 | [diff] [blame] | 60 | ProfilingState ProfilingService::ConfigureProfilingService( |
| 61 | const ExternalProfilingOptions& options, |
| 62 | bool resetProfilingService) |
| 63 | { |
| 64 | ResetExternalProfilingOptions(options, resetProfilingService); |
| 65 | ProfilingState currentState = m_StateMachine.GetCurrentState(); |
| 66 | if (options.m_EnableProfiling) |
| 67 | { |
| 68 | switch (currentState) |
| 69 | { |
| 70 | case ProfilingState::Uninitialised: |
| 71 | Update(); // should transition to NotConnected |
| 72 | Update(); // will either stay in NotConnected because there is no server |
| 73 | // or will enter WaitingForAck. |
| 74 | currentState = m_StateMachine.GetCurrentState(); |
| 75 | if (currentState == ProfilingState::WaitingForAck) |
| 76 | { |
| 77 | Update(); // poke it again to send out the metadata packet |
| 78 | } |
| 79 | currentState = m_StateMachine.GetCurrentState(); |
| 80 | return currentState; |
| 81 | case ProfilingState::NotConnected: |
| 82 | Update(); // will either stay in NotConnected because there is no server |
| 83 | // or will enter WaitingForAck |
| 84 | currentState = m_StateMachine.GetCurrentState(); |
| 85 | if (currentState == ProfilingState::WaitingForAck) |
| 86 | { |
| 87 | Update(); // poke it again to send out the metadata packet |
| 88 | } |
| 89 | currentState = m_StateMachine.GetCurrentState(); |
| 90 | return currentState; |
| 91 | default: |
| 92 | return currentState; |
| 93 | } |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | // Make sure profiling is shutdown |
| 98 | switch (currentState) |
| 99 | { |
| 100 | case ProfilingState::Uninitialised: |
| 101 | case ProfilingState::NotConnected: |
| 102 | return currentState; |
| 103 | default: |
| 104 | Stop(); |
| 105 | return m_StateMachine.GetCurrentState(); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 110 | void ProfilingService::Update() |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 111 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 112 | if (!m_Options.m_EnableProfiling) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 113 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 114 | // Don't run if profiling is disabled |
| 115 | return; |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 116 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 117 | |
| 118 | ProfilingState currentState = m_StateMachine.GetCurrentState(); |
| 119 | switch (currentState) |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 120 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 121 | case ProfilingState::Uninitialised: |
janeil01 | 811ca55 | 2019-12-03 17:01:32 +0000 | [diff] [blame] | 122 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 123 | // Initialize the profiling service |
| 124 | Initialize(); |
| 125 | |
| 126 | // Move to the next state |
| 127 | m_StateMachine.TransitionToState(ProfilingState::NotConnected); |
| 128 | break; |
| 129 | case ProfilingState::NotConnected: |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 130 | // Stop the command thread (if running) |
| 131 | m_CommandHandler.Stop(); |
| 132 | |
| 133 | // Stop the send thread (if running) |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 134 | m_SendThread.Stop(false); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 135 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 136 | // Stop the periodic counter capture thread (if running) |
| 137 | m_PeriodicCounterCapture.Stop(); |
| 138 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 139 | // Reset any existing profiling connection |
| 140 | m_ProfilingConnection.reset(); |
| 141 | |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 142 | try |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 143 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 144 | // Setup the profiling connection |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 145 | ARMNN_ASSERT(m_ProfilingConnectionFactory); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 146 | m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 147 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 148 | catch (const Exception& e) |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 149 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 150 | ARMNN_LOG(warning) << "An error has occurred when creating the profiling connection: " |
| 151 | << e.what(); |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 152 | } |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 153 | catch (const arm::pipe::SocketConnectionException& e) |
Sadik Armagan | a97a0be | 2020-03-03 10:44:56 +0000 | [diff] [blame] | 154 | { |
| 155 | ARMNN_LOG(warning) << "An error has occurred when creating the profiling connection [" |
| 156 | << e.what() << "] on socket [" << e.GetSocketFd() << "]."; |
| 157 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 158 | |
| 159 | // Move to the next state |
| 160 | m_StateMachine.TransitionToState(m_ProfilingConnection |
| 161 | ? ProfilingState::WaitingForAck // Profiling connection obtained, wait for ack |
| 162 | : ProfilingState::NotConnected); // Profiling connection failed, stay in the |
| 163 | // "NotConnected" state |
| 164 | break; |
| 165 | case ProfilingState::WaitingForAck: |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 166 | ARMNN_ASSERT(m_ProfilingConnection); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 167 | |
| 168 | // Start the command thread |
| 169 | m_CommandHandler.Start(*m_ProfilingConnection); |
| 170 | |
| 171 | // Start the send thread, while in "WaitingForAck" state it'll send out a "Stream MetaData" packet waiting for |
| 172 | // a valid "Connection Acknowledged" packet confirming the connection |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 173 | m_SendThread.Start(*m_ProfilingConnection); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 174 | |
| 175 | // The connection acknowledged command handler will automatically transition the state to "Active" once a |
| 176 | // valid "Connection Acknowledged" packet has been received |
| 177 | |
| 178 | break; |
| 179 | case ProfilingState::Active: |
| 180 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 181 | // The period counter capture thread is started by the Periodic Counter Selection command handler upon |
| 182 | // request by an external profiling service |
| 183 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 184 | break; |
| 185 | default: |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 186 | throw RuntimeException(fmt::format("Unknown profiling service state: {}", |
| 187 | static_cast<int>(currentState))); |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 188 | } |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 191 | void ProfilingService::Disconnect() |
| 192 | { |
| 193 | ProfilingState currentState = m_StateMachine.GetCurrentState(); |
| 194 | switch (currentState) |
| 195 | { |
| 196 | case ProfilingState::Uninitialised: |
| 197 | case ProfilingState::NotConnected: |
| 198 | case ProfilingState::WaitingForAck: |
| 199 | return; // NOP |
| 200 | case ProfilingState::Active: |
| 201 | // Stop the command thread (if running) |
| 202 | Stop(); |
| 203 | |
| 204 | break; |
| 205 | default: |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 206 | throw RuntimeException(fmt::format("Unknown profiling service state: {}", |
| 207 | static_cast<int>(currentState))); |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 211 | // Store a profiling context returned from a backend that support profiling, and register its counters |
| 212 | void ProfilingService::AddBackendProfilingContext(const BackendId backendId, |
| 213 | std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext) |
| 214 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 215 | ARMNN_ASSERT(profilingContext != nullptr); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 216 | // Register the backend counters |
| 217 | m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId); |
| 218 | m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext)); |
| 219 | } |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 220 | const ICounterDirectory& ProfilingService::GetCounterDirectory() const |
| 221 | { |
| 222 | return m_CounterDirectory; |
| 223 | } |
| 224 | |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 225 | ICounterRegistry& ProfilingService::GetCounterRegistry() |
| 226 | { |
| 227 | return m_CounterDirectory; |
| 228 | } |
| 229 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 230 | ProfilingState ProfilingService::GetCurrentState() const |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 231 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 232 | return m_StateMachine.GetCurrentState(); |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | uint16_t ProfilingService::GetCounterCount() const |
| 236 | { |
| 237 | return m_CounterDirectory.GetCounterCount(); |
| 238 | } |
| 239 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 240 | bool ProfilingService::IsCounterRegistered(uint16_t counterUid) const |
| 241 | { |
Finn Williams | b205a33 | 2020-05-13 15:04:25 +0100 | [diff] [blame] | 242 | return m_CounterDirectory.IsCounterRegistered(counterUid); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 245 | uint32_t ProfilingService::GetAbsoluteCounterValue(uint16_t counterUid) const |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 246 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 247 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 248 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 249 | ARMNN_ASSERT(counterValuePtr); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 250 | return counterValuePtr->load(std::memory_order::memory_order_relaxed); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 251 | } |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 252 | |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 253 | uint32_t ProfilingService::GetDeltaCounterValue(uint16_t counterUid) |
| 254 | { |
| 255 | CheckCounterUid(counterUid); |
| 256 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
| 257 | ARMNN_ASSERT(counterValuePtr); |
| 258 | const uint32_t counterValue = counterValuePtr->load(std::memory_order::memory_order_relaxed); |
| 259 | SubtractCounterValue(counterUid, counterValue); |
| 260 | return counterValue; |
| 261 | } |
| 262 | |
Jim Flynn | 8e0c7a6 | 2020-01-30 14:10:55 +0000 | [diff] [blame] | 263 | const ICounterMappings& ProfilingService::GetCounterMappings() const |
| 264 | { |
| 265 | return m_CounterIdMap; |
| 266 | } |
| 267 | |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 268 | IRegisterCounterMapping& ProfilingService::GetCounterMappingRegistry() |
Jim Flynn | 8e0c7a6 | 2020-01-30 14:10:55 +0000 | [diff] [blame] | 269 | { |
| 270 | return m_CounterIdMap; |
| 271 | } |
| 272 | |
James Conroy | 2dcd3fe | 2020-02-06 18:34:52 +0000 | [diff] [blame] | 273 | CaptureData ProfilingService::GetCaptureData() |
| 274 | { |
| 275 | return m_Holder.GetCaptureData(); |
| 276 | } |
| 277 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 278 | void ProfilingService::SetCaptureData(uint32_t capturePeriod, |
| 279 | const std::vector<uint16_t>& counterIds, |
| 280 | const std::set<BackendId>& activeBackends) |
James Conroy | 2dcd3fe | 2020-02-06 18:34:52 +0000 | [diff] [blame] | 281 | { |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 282 | m_Holder.SetCaptureData(capturePeriod, counterIds, activeBackends); |
James Conroy | 2dcd3fe | 2020-02-06 18:34:52 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 285 | void ProfilingService::SetCounterValue(uint16_t counterUid, uint32_t value) |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 286 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 287 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 288 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 289 | ARMNN_ASSERT(counterValuePtr); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 290 | counterValuePtr->store(value, std::memory_order::memory_order_relaxed); |
| 291 | } |
| 292 | |
| 293 | uint32_t ProfilingService::AddCounterValue(uint16_t counterUid, uint32_t value) |
| 294 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 295 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 296 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 297 | ARMNN_ASSERT(counterValuePtr); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 298 | return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed); |
| 299 | } |
| 300 | |
| 301 | uint32_t ProfilingService::SubtractCounterValue(uint16_t counterUid, uint32_t value) |
| 302 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 303 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 304 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 305 | ARMNN_ASSERT(counterValuePtr); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 306 | return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed); |
| 307 | } |
| 308 | |
| 309 | uint32_t ProfilingService::IncrementCounterValue(uint16_t counterUid) |
| 310 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 311 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 312 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 313 | ARMNN_ASSERT(counterValuePtr); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 314 | return counterValuePtr->operator++(std::memory_order::memory_order_relaxed); |
| 315 | } |
| 316 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 317 | ProfilingDynamicGuid ProfilingService::NextGuid() |
| 318 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 319 | return ProfilingService::GetNextGuid(); |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | ProfilingStaticGuid ProfilingService::GenerateStaticId(const std::string& str) |
| 323 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 324 | return ProfilingService::GetStaticId(str); |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 327 | std::unique_ptr<ISendTimelinePacket> ProfilingService::GetSendTimelinePacket() const |
| 328 | { |
| 329 | return m_TimelinePacketWriterFactory.GetSendTimelinePacket(); |
| 330 | } |
| 331 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 332 | void ProfilingService::Initialize() |
| 333 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 334 | // Register a category for the basic runtime counters |
| 335 | if (!m_CounterDirectory.IsCategoryRegistered("ArmNN_Runtime")) |
| 336 | { |
| 337 | m_CounterDirectory.RegisterCategory("ArmNN_Runtime"); |
| 338 | } |
| 339 | |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 340 | // Register a counter for the number of Network loads |
| 341 | if (!m_CounterDirectory.IsCounterRegistered("Network loads")) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 342 | { |
| 343 | const Counter* loadedNetworksCounter = |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 344 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 345 | armnn::profiling::NETWORK_LOADS, |
| 346 | "ArmNN_Runtime", |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 347 | 0, |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 348 | 0, |
| 349 | 1.f, |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 350 | "Network loads", |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 351 | "The number of networks loaded at runtime", |
| 352 | std::string("networks")); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 353 | ARMNN_ASSERT(loadedNetworksCounter); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 354 | InitializeCounterValue(loadedNetworksCounter->m_Uid); |
| 355 | } |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 356 | // Register a counter for the number of unloaded networks |
| 357 | if (!m_CounterDirectory.IsCounterRegistered("Network unloads")) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 358 | { |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 359 | const Counter* unloadedNetworksCounter = |
| 360 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 361 | armnn::profiling::NETWORK_UNLOADS, |
| 362 | "ArmNN_Runtime", |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 363 | 0, |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 364 | 0, |
| 365 | 1.f, |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 366 | "Network unloads", |
| 367 | "The number of networks unloaded at runtime", |
| 368 | std::string("networks")); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 369 | ARMNN_ASSERT(unloadedNetworksCounter); |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 370 | InitializeCounterValue(unloadedNetworksCounter->m_Uid); |
| 371 | } |
| 372 | // Register a counter for the number of registered backends |
| 373 | if (!m_CounterDirectory.IsCounterRegistered("Backends registered")) |
| 374 | { |
| 375 | const Counter* registeredBackendsCounter = |
| 376 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 377 | armnn::profiling::REGISTERED_BACKENDS, |
| 378 | "ArmNN_Runtime", |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 379 | 0, |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 380 | 0, |
| 381 | 1.f, |
| 382 | "Backends registered", |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 383 | "The number of registered backends", |
| 384 | std::string("backends")); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 385 | ARMNN_ASSERT(registeredBackendsCounter); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 386 | InitializeCounterValue(registeredBackendsCounter->m_Uid); |
Finn Williams | 45a7362 | 2020-05-15 18:41:05 +0100 | [diff] [blame] | 387 | |
| 388 | // Due to backends being registered before the profiling service becomes active, |
| 389 | // we need to set the counter to the correct value here |
| 390 | SetCounterValue(armnn::profiling::REGISTERED_BACKENDS, static_cast<uint32_t>(BackendRegistryInstance().Size())); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 391 | } |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 392 | // Register a counter for the number of registered backends |
| 393 | if (!m_CounterDirectory.IsCounterRegistered("Backends unregistered")) |
| 394 | { |
| 395 | const Counter* unregisteredBackendsCounter = |
| 396 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 397 | armnn::profiling::UNREGISTERED_BACKENDS, |
| 398 | "ArmNN_Runtime", |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 399 | 0, |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 400 | 0, |
| 401 | 1.f, |
| 402 | "Backends unregistered", |
| 403 | "The number of unregistered backends", |
| 404 | std::string("backends")); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 405 | ARMNN_ASSERT(unregisteredBackendsCounter); |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 406 | InitializeCounterValue(unregisteredBackendsCounter->m_Uid); |
| 407 | } |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 408 | // Register a counter for the number of inferences run |
| 409 | if (!m_CounterDirectory.IsCounterRegistered("Inferences run")) |
| 410 | { |
| 411 | const Counter* inferencesRunCounter = |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 412 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 413 | armnn::profiling::INFERENCES_RUN, |
| 414 | "ArmNN_Runtime", |
Finn Williams | f3fcf32 | 2020-05-11 14:38:02 +0100 | [diff] [blame] | 415 | 0, |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 416 | 0, |
| 417 | 1.f, |
| 418 | "Inferences run", |
| 419 | "The number of inferences run", |
| 420 | std::string("inferences")); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 421 | ARMNN_ASSERT(inferencesRunCounter); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 422 | InitializeCounterValue(inferencesRunCounter->m_Uid); |
| 423 | } |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 424 | } |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 425 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 426 | void ProfilingService::InitializeCounterValue(uint16_t counterUid) |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 427 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 428 | // Increase the size of the counter index if necessary |
| 429 | if (counterUid >= m_CounterIndex.size()) |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 430 | { |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 431 | m_CounterIndex.resize(armnn::numeric_cast<size_t>(counterUid) + 1); |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 432 | } |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 433 | |
| 434 | // Create a new atomic counter and add it to the list |
| 435 | m_CounterValues.emplace_back(0); |
| 436 | |
| 437 | // Register the new counter to the counter index for quick access |
| 438 | std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back()); |
| 439 | m_CounterIndex.at(counterUid) = counterValuePtr; |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 442 | void ProfilingService::Reset() |
| 443 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 444 | // Stop the profiling service... |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 445 | Stop(); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 446 | |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 447 | // ...then delete all the counter data and configuration... |
| 448 | m_CounterIndex.clear(); |
| 449 | m_CounterValues.clear(); |
| 450 | m_CounterDirectory.Clear(); |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 451 | m_CounterIdMap.Reset(); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 452 | m_BufferManager.Reset(); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 453 | |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 454 | // ...finally reset the profiling state machine |
| 455 | m_StateMachine.Reset(); |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 456 | m_BackendProfilingContexts.clear(); |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 457 | m_MaxGlobalCounterId = armnn::profiling::MAX_ARMNN_COUNTER; |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void ProfilingService::Stop() |
| 461 | { |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 462 | { // only lock when we are updating the inference completed variable |
| 463 | std::unique_lock<std::mutex> lck(m_ServiceActiveMutex); |
| 464 | m_ServiceActive = false; |
| 465 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 466 | // The order in which we reset/stop the components is not trivial! |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 467 | // First stop the producing threads |
| 468 | // Command Handler first as it is responsible for launching then Periodic Counter capture thread |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 469 | m_CommandHandler.Stop(); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 470 | m_PeriodicCounterCapture.Stop(); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 471 | // The the consuming thread |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 472 | m_SendThread.Stop(false); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 473 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 474 | // ...then close and destroy the profiling connection... |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 475 | if (m_ProfilingConnection != nullptr && m_ProfilingConnection->IsOpen()) |
| 476 | { |
| 477 | m_ProfilingConnection->Close(); |
| 478 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 479 | m_ProfilingConnection.reset(); |
| 480 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 481 | // ...then move to the "NotConnected" state |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 482 | m_StateMachine.TransitionToState(ProfilingState::NotConnected); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 485 | inline void ProfilingService::CheckCounterUid(uint16_t counterUid) const |
| 486 | { |
| 487 | if (!IsCounterRegistered(counterUid)) |
| 488 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 489 | throw InvalidArgumentException(fmt::format("Counter UID {} is not registered", counterUid)); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 493 | void ProfilingService::NotifyBackendsForTimelineReporting() |
| 494 | { |
| 495 | BackendProfilingContext::iterator it = m_BackendProfilingContexts.begin(); |
| 496 | while (it != m_BackendProfilingContexts.end()) |
| 497 | { |
| 498 | auto& backendProfilingContext = it->second; |
| 499 | backendProfilingContext->EnableTimelineReporting(m_TimelineReporting); |
| 500 | // Increment the Iterator to point to next entry |
| 501 | it++; |
| 502 | } |
| 503 | } |
| 504 | |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 505 | void ProfilingService::NotifyProfilingServiceActive() |
| 506 | { |
| 507 | { // only lock when we are updating the inference completed variable |
| 508 | std::unique_lock<std::mutex> lck(m_ServiceActiveMutex); |
| 509 | m_ServiceActive = true; |
| 510 | } |
| 511 | m_ServiceActiveConditionVariable.notify_one(); |
| 512 | } |
| 513 | |
| 514 | void ProfilingService::WaitForProfilingServiceActivation(unsigned int timeout) |
| 515 | { |
| 516 | std::unique_lock<std::mutex> lck(m_ServiceActiveMutex); |
| 517 | |
| 518 | auto start = std::chrono::high_resolution_clock::now(); |
| 519 | // Here we we will go back to sleep after a spurious wake up if |
| 520 | // m_InferenceCompleted is not yet true. |
| 521 | if (!m_ServiceActiveConditionVariable.wait_for(lck, |
| 522 | std::chrono::milliseconds(timeout), |
| 523 | [&]{return m_ServiceActive == true;})) |
| 524 | { |
| 525 | if (m_ServiceActive == true) |
| 526 | { |
| 527 | return; |
| 528 | } |
| 529 | auto finish = std::chrono::high_resolution_clock::now(); |
| 530 | std::chrono::duration<double, std::milli> elapsed = finish - start; |
| 531 | std::stringstream ss; |
| 532 | ss << "Timed out waiting on profiling service activation for " << elapsed.count() << " ms"; |
| 533 | ARMNN_LOG(warning) << ss.str(); |
| 534 | } |
| 535 | return; |
| 536 | } |
| 537 | |
janeil01 | 811ca55 | 2019-12-03 17:01:32 +0000 | [diff] [blame] | 538 | ProfilingService::~ProfilingService() |
| 539 | { |
| 540 | Stop(); |
| 541 | } |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 542 | } // namespace profiling |
| 543 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 544 | } // namespace armnn |