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 | |
| 8 | namespace armnn |
| 9 | { |
| 10 | |
| 11 | namespace profiling |
| 12 | { |
| 13 | |
| 14 | ProfilingService::ProfilingService(const Runtime::CreationOptions::ExternalProfilingOptions& options) |
| 15 | : m_Options(options) |
| 16 | { |
| 17 | Initialise(); |
| 18 | } |
| 19 | |
| 20 | void ProfilingService::Initialise() |
| 21 | { |
| 22 | if (m_Options.m_EnableProfiling == true) |
| 23 | { |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 24 | // Setup provisional Counter Directory example - this should only be created if profiling is enabled |
| 25 | // Setup provisional Counter meta example |
| 26 | const std::string categoryName = "Category"; |
| 27 | |
| 28 | m_CounterDirectory.RegisterCategory(categoryName); |
| 29 | m_CounterDirectory.RegisterDevice("device name", 0, categoryName); |
| 30 | m_CounterDirectory.RegisterCounterSet("counterSet_name", 2, categoryName); |
| 31 | |
| 32 | m_CounterDirectory.RegisterCounter(categoryName, |
| 33 | 0, |
| 34 | 1, |
| 35 | 123.45f, |
| 36 | "counter name 1", |
| 37 | "counter description"); |
| 38 | |
| 39 | m_CounterDirectory.RegisterCounter(categoryName, |
| 40 | 0, |
| 41 | 1, |
| 42 | 123.45f, |
| 43 | "counter name 2", |
| 44 | "counter description"); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 45 | |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 46 | for (unsigned short i = 0; i < m_CounterDirectory.GetCounterCount(); ++i) |
| 47 | { |
| 48 | m_CounterIdToValue[i] = 0; |
| 49 | } |
| 50 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 51 | // For now until CounterDirectory setup is implemented, change m_State once everything initialised |
| 52 | m_State.TransitionToState(ProfilingState::NotConnected); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void ProfilingService::Run() |
| 57 | { |
| 58 | if (m_State.GetCurrentState() == ProfilingState::NotConnected) |
| 59 | { |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 60 | try |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 61 | { |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 62 | m_Factory.GetProfilingConnection(m_Options); |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 63 | m_State.TransitionToState(ProfilingState::WaitingForAck); |
| 64 | } |
Sadik Armagan | bd9e2c5 | 2019-09-26 23:13:31 +0100 | [diff] [blame] | 65 | catch (const armnn::Exception& e) |
| 66 | { |
| 67 | std::cerr << e.what() << std::endl; |
| 68 | } |
| 69 | } |
| 70 | else if (m_State.GetCurrentState() == ProfilingState::Uninitialised && m_Options.m_EnableProfiling == true) |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 71 | { |
| 72 | Initialise(); |
| 73 | } |
| 74 | } |
| 75 | |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 76 | const ICounterDirectory& ProfilingService::GetCounterDirectory() const |
| 77 | { |
| 78 | return m_CounterDirectory; |
| 79 | } |
| 80 | |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 81 | void ProfilingService::SetCounterValue(uint16_t counterIndex, uint32_t value) |
| 82 | { |
| 83 | CheckIndexSize(counterIndex); |
| 84 | m_CounterIdToValue.at(counterIndex).store(value, std::memory_order::memory_order_relaxed); |
| 85 | } |
| 86 | |
| 87 | void ProfilingService::GetCounterValue(uint16_t counterIndex, uint32_t& value) const |
| 88 | { |
| 89 | CheckIndexSize(counterIndex); |
| 90 | value = m_CounterIdToValue.at(counterIndex).load(std::memory_order::memory_order_relaxed); |
| 91 | } |
| 92 | |
| 93 | void ProfilingService::AddCounterValue(uint16_t counterIndex, uint32_t value) |
| 94 | { |
| 95 | CheckIndexSize(counterIndex); |
| 96 | m_CounterIdToValue.at(counterIndex).fetch_add(value, std::memory_order::memory_order_relaxed); |
| 97 | } |
| 98 | |
| 99 | void ProfilingService::SubtractCounterValue(uint16_t counterIndex, uint32_t value) |
| 100 | { |
| 101 | CheckIndexSize(counterIndex); |
| 102 | m_CounterIdToValue.at(counterIndex).fetch_sub(value, std::memory_order::memory_order_relaxed); |
| 103 | } |
| 104 | |
| 105 | void ProfilingService::IncrementCounterValue(uint16_t counterIndex) |
| 106 | { |
| 107 | CheckIndexSize(counterIndex); |
| 108 | m_CounterIdToValue.at(counterIndex).operator++(std::memory_order::memory_order_relaxed); |
| 109 | } |
| 110 | |
| 111 | void ProfilingService::DecrementCounterValue(uint16_t counterIndex) |
| 112 | { |
| 113 | CheckIndexSize(counterIndex); |
| 114 | m_CounterIdToValue.at(counterIndex).operator--(std::memory_order::memory_order_relaxed); |
| 115 | } |
| 116 | |
| 117 | uint16_t ProfilingService::GetCounterCount() const |
| 118 | { |
| 119 | return m_CounterDirectory.GetCounterCount(); |
| 120 | } |
| 121 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 122 | ProfilingState ProfilingService::GetCurrentState() const |
| 123 | { |
| 124 | return m_State.GetCurrentState(); |
| 125 | } |
FinnWilliamsArm | ce2d9d1 | 2019-09-18 10:28:16 +0100 | [diff] [blame] | 126 | |
| 127 | void ProfilingService::ResetExternalProfilingOptions(const Runtime::CreationOptions::ExternalProfilingOptions& options) |
| 128 | { |
| 129 | if(!m_Options.m_EnableProfiling) |
| 130 | { |
| 131 | m_Options = options; |
| 132 | Initialise(); |
| 133 | return; |
| 134 | } |
| 135 | m_Options = options; |
| 136 | } |
FinnWilliamsArm | f6e534a | 2019-09-16 15:45:42 +0100 | [diff] [blame] | 137 | |
| 138 | inline void ProfilingService::CheckIndexSize(uint16_t counterIndex) const |
| 139 | { |
| 140 | if (counterIndex >= m_CounterDirectory.GetCounterCount()) |
| 141 | { |
| 142 | throw InvalidArgumentException("Counter index is out of range"); |
| 143 | } |
| 144 | } |
| 145 | |
Keith Davis | 02356de | 2019-08-26 18:28:17 +0100 | [diff] [blame] | 146 | } // namespace profiling |
| 147 | |
| 148 | } // namespace armnn |