Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "ProfilingStateMachine.hpp" |
| 7 | |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 8 | #include <common/include/ProfilingException.hpp> |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 9 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 10 | #include <sstream> |
| 11 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 12 | namespace arm |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 13 | { |
| 14 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 15 | namespace pipe |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 16 | { |
| 17 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 18 | namespace |
| 19 | { |
| 20 | |
| 21 | void ThrowStateTransitionException(ProfilingState expectedState, ProfilingState newState) |
| 22 | { |
| 23 | std::stringstream ss; |
| 24 | ss << "Cannot transition from state [" << GetProfilingStateName(expectedState) << "] " |
| 25 | << "to state [" << GetProfilingStateName(newState) << "]"; |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 26 | throw arm::pipe::ProfilingException(ss.str()); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | } // Anonymous namespace |
| 30 | |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 31 | ProfilingState ProfilingStateMachine::GetCurrentState() const |
| 32 | { |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 33 | return m_State.load(); |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void ProfilingStateMachine::TransitionToState(ProfilingState newState) |
| 37 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 38 | ProfilingState currentState = m_State.load(std::memory_order::memory_order_relaxed); |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 39 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 40 | switch (newState) |
| 41 | { |
| 42 | case ProfilingState::Uninitialised: |
| 43 | do |
| 44 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 45 | if (!IsOneOfStates(currentState, ProfilingState::Uninitialised)) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 46 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 47 | ThrowStateTransitionException(currentState, newState); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 48 | } |
| 49 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 50 | while (!m_State.compare_exchange_strong(currentState, newState, std::memory_order::memory_order_relaxed)); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 51 | break; |
| 52 | case ProfilingState::NotConnected: |
| 53 | do |
| 54 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 55 | if (!IsOneOfStates(currentState, ProfilingState::Uninitialised, ProfilingState::NotConnected, |
Jim Flynn | 53e4699 | 2019-10-14 12:31:10 +0100 | [diff] [blame] | 56 | ProfilingState::Active, ProfilingState::WaitingForAck)) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 57 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 58 | ThrowStateTransitionException(currentState, newState); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 61 | while (!m_State.compare_exchange_strong(currentState, newState, std::memory_order::memory_order_relaxed)); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 62 | break; |
| 63 | case ProfilingState::WaitingForAck: |
| 64 | do |
| 65 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 66 | if (!IsOneOfStates(currentState, ProfilingState::NotConnected, ProfilingState::WaitingForAck)) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 67 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 68 | ThrowStateTransitionException(currentState, newState); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 69 | } |
| 70 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 71 | while (!m_State.compare_exchange_strong(currentState, newState, std::memory_order::memory_order_relaxed)); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 72 | break; |
| 73 | case ProfilingState::Active: |
| 74 | do |
| 75 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 76 | if (!IsOneOfStates(currentState, ProfilingState::WaitingForAck, ProfilingState::Active)) |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 77 | { |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 78 | ThrowStateTransitionException(currentState, newState); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 81 | while (!m_State.compare_exchange_strong(currentState, newState, std::memory_order::memory_order_relaxed)); |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 82 | break; |
| 83 | default: |
| 84 | break; |
| 85 | } |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 88 | void ProfilingStateMachine::Reset() |
| 89 | { |
| 90 | m_State.store(ProfilingState::Uninitialised); |
| 91 | } |
Nikhil Raj | 3ecc510 | 2019-09-03 15:55:33 +0100 | [diff] [blame] | 92 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 93 | } // namespace pipe |
Matteo Martincigh | a84edee | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 94 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 95 | } // namespace arm |