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