Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 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> |
| 10 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 11 | #include <boost/format.hpp> |
| 12 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 13 | namespace armnn |
| 14 | { |
| 15 | |
| 16 | namespace profiling |
| 17 | { |
| 18 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 19 | void ProfilingService::ResetExternalProfilingOptions(const ExternalProfilingOptions& options, |
| 20 | bool resetProfilingService) |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 21 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 22 | // Update the profiling options |
| 23 | m_Options = options; |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 24 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 25 | // Check if the profiling service needs to be reset |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 26 | if (resetProfilingService) |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 27 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 28 | // Reset the profiling service |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 29 | Reset(); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 33 | bool ProfilingService::IsProfilingEnabled() |
| 34 | { |
| 35 | return m_Options.m_EnableProfiling; |
| 36 | } |
| 37 | |
Jim Flynn | 672d06e | 2019-10-15 10:18:11 +0100 | [diff] [blame] | 38 | ProfilingState ProfilingService::ConfigureProfilingService( |
| 39 | const ExternalProfilingOptions& options, |
| 40 | bool resetProfilingService) |
| 41 | { |
| 42 | ResetExternalProfilingOptions(options, resetProfilingService); |
| 43 | ProfilingState currentState = m_StateMachine.GetCurrentState(); |
| 44 | if (options.m_EnableProfiling) |
| 45 | { |
| 46 | switch (currentState) |
| 47 | { |
| 48 | case ProfilingState::Uninitialised: |
| 49 | Update(); // should transition to NotConnected |
| 50 | Update(); // will either stay in NotConnected because there is no server |
| 51 | // or will enter WaitingForAck. |
| 52 | currentState = m_StateMachine.GetCurrentState(); |
| 53 | if (currentState == ProfilingState::WaitingForAck) |
| 54 | { |
| 55 | Update(); // poke it again to send out the metadata packet |
| 56 | } |
| 57 | currentState = m_StateMachine.GetCurrentState(); |
| 58 | return currentState; |
| 59 | case ProfilingState::NotConnected: |
| 60 | Update(); // will either stay in NotConnected because there is no server |
| 61 | // or will enter WaitingForAck |
| 62 | currentState = m_StateMachine.GetCurrentState(); |
| 63 | if (currentState == ProfilingState::WaitingForAck) |
| 64 | { |
| 65 | Update(); // poke it again to send out the metadata packet |
| 66 | } |
| 67 | currentState = m_StateMachine.GetCurrentState(); |
| 68 | return currentState; |
| 69 | default: |
| 70 | return currentState; |
| 71 | } |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | // Make sure profiling is shutdown |
| 76 | switch (currentState) |
| 77 | { |
| 78 | case ProfilingState::Uninitialised: |
| 79 | case ProfilingState::NotConnected: |
| 80 | return currentState; |
| 81 | default: |
| 82 | Stop(); |
| 83 | return m_StateMachine.GetCurrentState(); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 88 | void ProfilingService::Update() |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 89 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 90 | if (!m_Options.m_EnableProfiling) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 91 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 92 | // Don't run if profiling is disabled |
| 93 | return; |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 94 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 95 | |
| 96 | ProfilingState currentState = m_StateMachine.GetCurrentState(); |
| 97 | switch (currentState) |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 98 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 99 | case ProfilingState::Uninitialised: |
janeil01 | 811ca55 | 2019-12-03 17:01:32 +0000 | [diff] [blame] | 100 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 101 | // Initialize the profiling service |
| 102 | Initialize(); |
| 103 | |
| 104 | // Move to the next state |
| 105 | m_StateMachine.TransitionToState(ProfilingState::NotConnected); |
| 106 | break; |
| 107 | case ProfilingState::NotConnected: |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 108 | // Stop the command thread (if running) |
| 109 | m_CommandHandler.Stop(); |
| 110 | |
| 111 | // Stop the send thread (if running) |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 112 | m_SendThread.Stop(false); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 113 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 114 | // Stop the periodic counter capture thread (if running) |
| 115 | m_PeriodicCounterCapture.Stop(); |
| 116 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 117 | // Reset any existing profiling connection |
| 118 | m_ProfilingConnection.reset(); |
| 119 | |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 120 | try |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 121 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 122 | // Setup the profiling connection |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 123 | BOOST_ASSERT(m_ProfilingConnectionFactory); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 124 | m_ProfilingConnection = m_ProfilingConnectionFactory->GetProfilingConnection(m_Options); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 125 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 126 | catch (const Exception& e) |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 127 | { |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 128 | ARMNN_LOG(warning) << "An error has occurred when creating the profiling connection: " |
| 129 | << e.what(); |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 130 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 131 | |
| 132 | // Move to the next state |
| 133 | m_StateMachine.TransitionToState(m_ProfilingConnection |
| 134 | ? ProfilingState::WaitingForAck // Profiling connection obtained, wait for ack |
| 135 | : ProfilingState::NotConnected); // Profiling connection failed, stay in the |
| 136 | // "NotConnected" state |
| 137 | break; |
| 138 | case ProfilingState::WaitingForAck: |
| 139 | BOOST_ASSERT(m_ProfilingConnection); |
| 140 | |
| 141 | // Start the command thread |
| 142 | m_CommandHandler.Start(*m_ProfilingConnection); |
| 143 | |
| 144 | // Start the send thread, while in "WaitingForAck" state it'll send out a "Stream MetaData" packet waiting for |
| 145 | // a valid "Connection Acknowledged" packet confirming the connection |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 146 | m_SendThread.Start(*m_ProfilingConnection); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 147 | |
| 148 | // The connection acknowledged command handler will automatically transition the state to "Active" once a |
| 149 | // valid "Connection Acknowledged" packet has been received |
| 150 | |
| 151 | break; |
| 152 | case ProfilingState::Active: |
| 153 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 154 | // The period counter capture thread is started by the Periodic Counter Selection command handler upon |
| 155 | // request by an external profiling service |
| 156 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 157 | break; |
| 158 | default: |
| 159 | throw RuntimeException(boost::str(boost::format("Unknown profiling service state: %1") |
| 160 | % static_cast<int>(currentState))); |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 161 | } |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 164 | void ProfilingService::Disconnect() |
| 165 | { |
| 166 | ProfilingState currentState = m_StateMachine.GetCurrentState(); |
| 167 | switch (currentState) |
| 168 | { |
| 169 | case ProfilingState::Uninitialised: |
| 170 | case ProfilingState::NotConnected: |
| 171 | case ProfilingState::WaitingForAck: |
| 172 | return; // NOP |
| 173 | case ProfilingState::Active: |
| 174 | // Stop the command thread (if running) |
| 175 | Stop(); |
| 176 | |
| 177 | break; |
| 178 | default: |
| 179 | throw RuntimeException(boost::str(boost::format("Unknown profiling service state: %1") |
| 180 | % static_cast<int>(currentState))); |
| 181 | } |
| 182 | } |
| 183 | |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 184 | // Store a profiling context returned from a backend that support profiling, and register its counters |
| 185 | void ProfilingService::AddBackendProfilingContext(const BackendId backendId, |
| 186 | std::shared_ptr<armnn::profiling::IBackendProfilingContext> profilingContext) |
| 187 | { |
| 188 | BOOST_ASSERT(profilingContext != nullptr); |
| 189 | // Register the backend counters |
| 190 | m_MaxGlobalCounterId = profilingContext->RegisterCounters(m_MaxGlobalCounterId); |
| 191 | m_BackendProfilingContexts.emplace(backendId, std::move(profilingContext)); |
| 192 | } |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 193 | const ICounterDirectory& ProfilingService::GetCounterDirectory() const |
| 194 | { |
| 195 | return m_CounterDirectory; |
| 196 | } |
| 197 | |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 198 | ICounterRegistry& ProfilingService::GetCounterRegistry() |
| 199 | { |
| 200 | return m_CounterDirectory; |
| 201 | } |
| 202 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 203 | ProfilingState ProfilingService::GetCurrentState() const |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 204 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 205 | return m_StateMachine.GetCurrentState(); |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | uint16_t ProfilingService::GetCounterCount() const |
| 209 | { |
| 210 | return m_CounterDirectory.GetCounterCount(); |
| 211 | } |
| 212 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 213 | bool ProfilingService::IsCounterRegistered(uint16_t counterUid) const |
| 214 | { |
| 215 | return counterUid < m_CounterIndex.size(); |
| 216 | } |
| 217 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 218 | uint32_t ProfilingService::GetCounterValue(uint16_t counterUid) const |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 219 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 220 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 221 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
| 222 | BOOST_ASSERT(counterValuePtr); |
| 223 | return counterValuePtr->load(std::memory_order::memory_order_relaxed); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 224 | } |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 225 | |
Jim Flynn | 8e0c7a6 | 2020-01-30 14:10:55 +0000 | [diff] [blame] | 226 | const ICounterMappings& ProfilingService::GetCounterMappings() const |
| 227 | { |
| 228 | return m_CounterIdMap; |
| 229 | } |
| 230 | |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 231 | IRegisterCounterMapping& ProfilingService::GetCounterMappingRegistry() |
Jim Flynn | 8e0c7a6 | 2020-01-30 14:10:55 +0000 | [diff] [blame] | 232 | { |
| 233 | return m_CounterIdMap; |
| 234 | } |
| 235 | |
James Conroy | 2dcd3fe | 2020-02-06 18:34:52 +0000 | [diff] [blame] | 236 | CaptureData ProfilingService::GetCaptureData() |
| 237 | { |
| 238 | return m_Holder.GetCaptureData(); |
| 239 | } |
| 240 | |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame^] | 241 | void ProfilingService::SetCaptureData(uint32_t capturePeriod, |
| 242 | const std::vector<uint16_t>& counterIds, |
| 243 | const std::set<BackendId>& activeBackends) |
James Conroy | 2dcd3fe | 2020-02-06 18:34:52 +0000 | [diff] [blame] | 244 | { |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame^] | 245 | m_Holder.SetCaptureData(capturePeriod, counterIds, activeBackends); |
James Conroy | 2dcd3fe | 2020-02-06 18:34:52 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 248 | void ProfilingService::SetCounterValue(uint16_t counterUid, uint32_t value) |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 249 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 250 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 251 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
| 252 | BOOST_ASSERT(counterValuePtr); |
| 253 | counterValuePtr->store(value, std::memory_order::memory_order_relaxed); |
| 254 | } |
| 255 | |
| 256 | uint32_t ProfilingService::AddCounterValue(uint16_t counterUid, uint32_t value) |
| 257 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 258 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 259 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
| 260 | BOOST_ASSERT(counterValuePtr); |
| 261 | return counterValuePtr->fetch_add(value, std::memory_order::memory_order_relaxed); |
| 262 | } |
| 263 | |
| 264 | uint32_t ProfilingService::SubtractCounterValue(uint16_t counterUid, uint32_t value) |
| 265 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 266 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 267 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
| 268 | BOOST_ASSERT(counterValuePtr); |
| 269 | return counterValuePtr->fetch_sub(value, std::memory_order::memory_order_relaxed); |
| 270 | } |
| 271 | |
| 272 | uint32_t ProfilingService::IncrementCounterValue(uint16_t counterUid) |
| 273 | { |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 274 | CheckCounterUid(counterUid); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 275 | std::atomic<uint32_t>* counterValuePtr = m_CounterIndex.at(counterUid); |
| 276 | BOOST_ASSERT(counterValuePtr); |
| 277 | return counterValuePtr->operator++(std::memory_order::memory_order_relaxed); |
| 278 | } |
| 279 | |
Jim Flynn | 00f3aaf | 2019-10-24 11:58:06 +0100 | [diff] [blame] | 280 | ProfilingDynamicGuid ProfilingService::NextGuid() |
| 281 | { |
| 282 | return m_GuidGenerator.NextGuid(); |
| 283 | } |
| 284 | |
| 285 | ProfilingStaticGuid ProfilingService::GenerateStaticId(const std::string& str) |
| 286 | { |
| 287 | return m_GuidGenerator.GenerateStaticId(str); |
| 288 | } |
| 289 | |
Jim Flynn | 8b20065 | 2019-10-24 18:07:44 +0100 | [diff] [blame] | 290 | std::unique_ptr<ISendTimelinePacket> ProfilingService::GetSendTimelinePacket() const |
| 291 | { |
| 292 | return m_TimelinePacketWriterFactory.GetSendTimelinePacket(); |
| 293 | } |
| 294 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 295 | void ProfilingService::Initialize() |
| 296 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 297 | // Register a category for the basic runtime counters |
| 298 | if (!m_CounterDirectory.IsCategoryRegistered("ArmNN_Runtime")) |
| 299 | { |
| 300 | m_CounterDirectory.RegisterCategory("ArmNN_Runtime"); |
| 301 | } |
| 302 | |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 303 | // Register a counter for the number of Network loads |
| 304 | if (!m_CounterDirectory.IsCounterRegistered("Network loads")) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 305 | { |
| 306 | const Counter* loadedNetworksCounter = |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 307 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 308 | armnn::profiling::NETWORK_LOADS, |
| 309 | "ArmNN_Runtime", |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 310 | 0, |
| 311 | 0, |
| 312 | 1.f, |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 313 | "Network loads", |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 314 | "The number of networks loaded at runtime", |
| 315 | std::string("networks")); |
| 316 | BOOST_ASSERT(loadedNetworksCounter); |
| 317 | InitializeCounterValue(loadedNetworksCounter->m_Uid); |
| 318 | } |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 319 | // Register a counter for the number of unloaded networks |
| 320 | if (!m_CounterDirectory.IsCounterRegistered("Network unloads")) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 321 | { |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 322 | const Counter* unloadedNetworksCounter = |
| 323 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 324 | armnn::profiling::NETWORK_UNLOADS, |
| 325 | "ArmNN_Runtime", |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 326 | 0, |
| 327 | 0, |
| 328 | 1.f, |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 329 | "Network unloads", |
| 330 | "The number of networks unloaded at runtime", |
| 331 | std::string("networks")); |
| 332 | BOOST_ASSERT(unloadedNetworksCounter); |
| 333 | InitializeCounterValue(unloadedNetworksCounter->m_Uid); |
| 334 | } |
| 335 | // Register a counter for the number of registered backends |
| 336 | if (!m_CounterDirectory.IsCounterRegistered("Backends registered")) |
| 337 | { |
| 338 | const Counter* registeredBackendsCounter = |
| 339 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 340 | armnn::profiling::REGISTERED_BACKENDS, |
| 341 | "ArmNN_Runtime", |
| 342 | 0, |
| 343 | 0, |
| 344 | 1.f, |
| 345 | "Backends registered", |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 346 | "The number of registered backends", |
| 347 | std::string("backends")); |
| 348 | BOOST_ASSERT(registeredBackendsCounter); |
| 349 | InitializeCounterValue(registeredBackendsCounter->m_Uid); |
| 350 | } |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 351 | // Register a counter for the number of registered backends |
| 352 | if (!m_CounterDirectory.IsCounterRegistered("Backends unregistered")) |
| 353 | { |
| 354 | const Counter* unregisteredBackendsCounter = |
| 355 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 356 | armnn::profiling::UNREGISTERED_BACKENDS, |
| 357 | "ArmNN_Runtime", |
| 358 | 0, |
| 359 | 0, |
| 360 | 1.f, |
| 361 | "Backends unregistered", |
| 362 | "The number of unregistered backends", |
| 363 | std::string("backends")); |
| 364 | BOOST_ASSERT(unregisteredBackendsCounter); |
| 365 | InitializeCounterValue(unregisteredBackendsCounter->m_Uid); |
| 366 | } |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 367 | // Register a counter for the number of inferences run |
| 368 | if (!m_CounterDirectory.IsCounterRegistered("Inferences run")) |
| 369 | { |
| 370 | const Counter* inferencesRunCounter = |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 371 | m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 372 | armnn::profiling::INFERENCES_RUN, |
| 373 | "ArmNN_Runtime", |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 374 | 0, |
| 375 | 0, |
| 376 | 1.f, |
| 377 | "Inferences run", |
| 378 | "The number of inferences run", |
| 379 | std::string("inferences")); |
| 380 | BOOST_ASSERT(inferencesRunCounter); |
| 381 | InitializeCounterValue(inferencesRunCounter->m_Uid); |
| 382 | } |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 383 | } |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 384 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 385 | void ProfilingService::InitializeCounterValue(uint16_t counterUid) |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 386 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 387 | // Increase the size of the counter index if necessary |
| 388 | if (counterUid >= m_CounterIndex.size()) |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 389 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 390 | m_CounterIndex.resize(boost::numeric_cast<size_t>(counterUid) + 1); |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 391 | } |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 392 | |
| 393 | // Create a new atomic counter and add it to the list |
| 394 | m_CounterValues.emplace_back(0); |
| 395 | |
| 396 | // Register the new counter to the counter index for quick access |
| 397 | std::atomic<uint32_t>* counterValuePtr = &(m_CounterValues.back()); |
| 398 | m_CounterIndex.at(counterUid) = counterValuePtr; |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 401 | void ProfilingService::Reset() |
| 402 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 403 | // Stop the profiling service... |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 404 | Stop(); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 405 | |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 406 | // ...then delete all the counter data and configuration... |
| 407 | m_CounterIndex.clear(); |
| 408 | m_CounterValues.clear(); |
| 409 | m_CounterDirectory.Clear(); |
Jim Flynn | 9789702 | 2020-02-02 12:52:59 +0000 | [diff] [blame] | 410 | m_CounterIdMap.Reset(); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 411 | m_BufferManager.Reset(); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 412 | |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 413 | // ...finally reset the profiling state machine |
| 414 | m_StateMachine.Reset(); |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 415 | m_BackendProfilingContexts.clear(); |
David Monahan | c1536d6 | 2020-02-12 15:52:35 +0000 | [diff] [blame] | 416 | m_MaxGlobalCounterId = armnn::profiling::INFERENCES_RUN; |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void ProfilingService::Stop() |
| 420 | { |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 421 | // The order in which we reset/stop the components is not trivial! |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 422 | // First stop the producing threads |
| 423 | // 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] | 424 | m_CommandHandler.Stop(); |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 425 | m_PeriodicCounterCapture.Stop(); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 426 | // The the consuming thread |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 427 | m_SendThread.Stop(false); |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 428 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 429 | // ...then close and destroy the profiling connection... |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 430 | if (m_ProfilingConnection != nullptr && m_ProfilingConnection->IsOpen()) |
| 431 | { |
| 432 | m_ProfilingConnection->Close(); |
| 433 | } |
Matteo Martincigh | d0613b5 | 2019-10-09 16:47:04 +0100 | [diff] [blame] | 434 | m_ProfilingConnection.reset(); |
| 435 | |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 436 | // ...then move to the "NotConnected" state |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 437 | m_StateMachine.TransitionToState(ProfilingState::NotConnected); |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Matteo Martincigh | e848538 | 2019-10-10 14:08:21 +0100 | [diff] [blame] | 440 | inline void ProfilingService::CheckCounterUid(uint16_t counterUid) const |
| 441 | { |
| 442 | if (!IsCounterRegistered(counterUid)) |
| 443 | { |
| 444 | throw InvalidArgumentException(boost::str(boost::format("Counter UID %1% is not registered") % counterUid)); |
| 445 | } |
| 446 | } |
| 447 | |
janeil01 | 811ca55 | 2019-12-03 17:01:32 +0000 | [diff] [blame] | 448 | ProfilingService::~ProfilingService() |
| 449 | { |
| 450 | Stop(); |
| 451 | } |
| 452 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 453 | } // namespace profiling |
| 454 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 455 | } // namespace armnn |