blob: 786bfae12e0f66c02356d49e896339ea13889af9 [file] [log] [blame]
Keith Davis02356de2019-08-26 18:28:17 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "ProfilingService.hpp"
7
8namespace armnn
9{
10
11namespace profiling
12{
13
14ProfilingService::ProfilingService(const Runtime::CreationOptions::ExternalProfilingOptions& options)
15 : m_Options(options)
16{
17 Initialise();
18}
19
20void ProfilingService::Initialise()
21{
22 if (m_Options.m_EnableProfiling == true)
23 {
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +010024 // 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 Davis02356de2019-08-26 18:28:17 +010045
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +010046 for (unsigned short i = 0; i < m_CounterDirectory.GetCounterCount(); ++i)
47 {
48 m_CounterIdToValue[i] = 0;
49 }
50
Keith Davis02356de2019-08-26 18:28:17 +010051 // For now until CounterDirectory setup is implemented, change m_State once everything initialised
52 m_State.TransitionToState(ProfilingState::NotConnected);
53 }
54}
55
56void ProfilingService::Run()
57{
58 if (m_State.GetCurrentState() == ProfilingState::NotConnected)
59 {
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010060 try
Keith Davis02356de2019-08-26 18:28:17 +010061 {
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010062 m_Factory.GetProfilingConnection(m_Options);
Keith Davis02356de2019-08-26 18:28:17 +010063 m_State.TransitionToState(ProfilingState::WaitingForAck);
64 }
Sadik Armaganbd9e2c52019-09-26 23:13:31 +010065 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 Davis02356de2019-08-26 18:28:17 +010071 {
72 Initialise();
73 }
74}
75
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +010076const ICounterDirectory& ProfilingService::GetCounterDirectory() const
77{
78 return m_CounterDirectory;
79}
80
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +010081void 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
87void 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
93void 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
99void 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
105void ProfilingService::IncrementCounterValue(uint16_t counterIndex)
106{
107 CheckIndexSize(counterIndex);
108 m_CounterIdToValue.at(counterIndex).operator++(std::memory_order::memory_order_relaxed);
109}
110
111void ProfilingService::DecrementCounterValue(uint16_t counterIndex)
112{
113 CheckIndexSize(counterIndex);
114 m_CounterIdToValue.at(counterIndex).operator--(std::memory_order::memory_order_relaxed);
115}
116
117uint16_t ProfilingService::GetCounterCount() const
118{
119 return m_CounterDirectory.GetCounterCount();
120}
121
Keith Davis02356de2019-08-26 18:28:17 +0100122ProfilingState ProfilingService::GetCurrentState() const
123{
124 return m_State.GetCurrentState();
125}
FinnWilliamsArmce2d9d12019-09-18 10:28:16 +0100126
127void 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}
FinnWilliamsArmf6e534a2019-09-16 15:45:42 +0100137
138inline 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 Davis02356de2019-08-26 18:28:17 +0100146} // namespace profiling
147
148} // namespace armnn