aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/ProfilingService.cpp
blob: 9f5978888debed486f10fb17d38f9369b50d757f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ProfilingService.hpp"

namespace armnn
{

namespace profiling
{

ProfilingService::ProfilingService(const Runtime::CreationOptions::ExternalProfilingOptions& options)
    : m_Options(options)
{
    Initialise();
}

void ProfilingService::Initialise()
{
    if (m_Options.m_EnableProfiling == true)
    {
        // Setup provisional Counter Directory example - this should only be created if profiling is enabled
        // Setup provisional Counter meta example
        const std::string categoryName = "Category";

        m_CounterDirectory.RegisterCategory(categoryName);
        m_CounterDirectory.RegisterDevice("device name", 0, categoryName);
        m_CounterDirectory.RegisterCounterSet("counterSet_name", 2, categoryName);

        m_CounterDirectory.RegisterCounter(categoryName,
                                           0,
                                           1,
                                           123.45f,
                                           "counter name 1",
                                           "counter description");

        m_CounterDirectory.RegisterCounter(categoryName,
                                           0,
                                           1,
                                           123.45f,
                                           "counter name 2",
                                           "counter description");

        for (unsigned short i = 0; i < m_CounterDirectory.GetCounterCount(); ++i)
        {
            m_CounterIdToValue[i] = 0;
        }

        // For now until CounterDirectory setup is implemented, change m_State once everything initialised
        m_State.TransitionToState(ProfilingState::NotConnected);
    }
}

void ProfilingService::Run()
{
    if (m_State.GetCurrentState() == ProfilingState::NotConnected)
    {
        //  Since GetProfilingConnection is not implemented, if !NULL,
        //  then change to WaitingForAck. This will need to change once there is implementation
        //  for the IProfilingConnection
        if (!m_Factory.GetProfilingConnection(m_Options))
        {
            m_State.TransitionToState(ProfilingState::WaitingForAck);
        }
    } else if (m_State.GetCurrentState() == ProfilingState::Uninitialised && m_Options.m_EnableProfiling == true)
    {
        Initialise();
    }
}

const ICounterDirectory& ProfilingService::GetCounterDirectory() const
{
    return m_CounterDirectory;
}

void ProfilingService::SetCounterValue(uint16_t counterIndex, uint32_t value)
{
    CheckIndexSize(counterIndex);
    m_CounterIdToValue.at(counterIndex).store(value, std::memory_order::memory_order_relaxed);
}

void ProfilingService::GetCounterValue(uint16_t counterIndex, uint32_t& value) const
{
    CheckIndexSize(counterIndex);
    value = m_CounterIdToValue.at(counterIndex).load(std::memory_order::memory_order_relaxed);
}

void ProfilingService::AddCounterValue(uint16_t counterIndex, uint32_t value)
{
    CheckIndexSize(counterIndex);
    m_CounterIdToValue.at(counterIndex).fetch_add(value, std::memory_order::memory_order_relaxed);
}

void ProfilingService::SubtractCounterValue(uint16_t counterIndex, uint32_t value)
{
    CheckIndexSize(counterIndex);
    m_CounterIdToValue.at(counterIndex).fetch_sub(value, std::memory_order::memory_order_relaxed);
}

void ProfilingService::IncrementCounterValue(uint16_t counterIndex)
{
    CheckIndexSize(counterIndex);
    m_CounterIdToValue.at(counterIndex).operator++(std::memory_order::memory_order_relaxed);
}

void ProfilingService::DecrementCounterValue(uint16_t counterIndex)
{
    CheckIndexSize(counterIndex);
    m_CounterIdToValue.at(counterIndex).operator--(std::memory_order::memory_order_relaxed);
}

uint16_t ProfilingService::GetCounterCount() const
{
    return m_CounterDirectory.GetCounterCount();
}

ProfilingState ProfilingService::GetCurrentState() const
{
    return m_State.GetCurrentState();
}

void ProfilingService::ResetExternalProfilingOptions(const Runtime::CreationOptions::ExternalProfilingOptions& options)
{
    if(!m_Options.m_EnableProfiling)
    {
        m_Options = options;
        Initialise();
        return;
    }
    m_Options = options;
}

inline void ProfilingService::CheckIndexSize(uint16_t counterIndex) const
{
    if (counterIndex >= m_CounterDirectory.GetCounterCount())
    {
        throw InvalidArgumentException("Counter index is out of range");
    }
}

} // namespace profiling

} // namespace armnn