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