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