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