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