aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/PeriodicCounterSelectionCommandHandler.cpp
blob: d218433d93c60a94317a975e3aec2f1009a143a1 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// Copyright © 2019 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "PeriodicCounterSelectionCommandHandler.hpp"
#include "ProfilingUtils.hpp"

#include <armnn/Types.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/format.hpp>

#include <vector>

namespace armnn
{

namespace profiling
{

void PeriodicCounterSelectionCommandHandler::ParseData(const Packet& packet, CaptureData& captureData)
{
    std::vector<uint16_t> counterIds;
    uint32_t sizeOfUint32 = boost::numeric_cast<uint32_t>(sizeof(uint32_t));
    uint32_t sizeOfUint16 = boost::numeric_cast<uint32_t>(sizeof(uint16_t));
    uint32_t offset = 0;

    if (packet.GetLength() < 4)
    {
        // Insufficient packet size
        return;
    }

    // Parse the capture period
    uint32_t capturePeriod = ReadUint32(packet.GetData(), offset);

    // Set the capture period
    captureData.SetCapturePeriod(capturePeriod);

    // Parse the counter ids
    unsigned int counters = (packet.GetLength() - 4) / 2;
    if (counters > 0)
    {
        counterIds.reserve(counters);
        offset += sizeOfUint32;
        for (unsigned int i = 0; i < counters; ++i)
        {
            // Parse the counter id
            uint16_t counterId = ReadUint16(packet.GetData(), offset);
            counterIds.emplace_back(counterId);
            offset += sizeOfUint16;
        }
    }

    // Set the counter ids
    captureData.SetCounterIds(counterIds);
}

void PeriodicCounterSelectionCommandHandler::operator()(const Packet& packet)
{
    ProfilingState currentState = m_StateMachine.GetCurrentState();
    switch (currentState)
    {
    case ProfilingState::Uninitialised:
    case ProfilingState::NotConnected:
    case ProfilingState::WaitingForAck:
        throw RuntimeException(boost::str(boost::format("Periodic Counter Selection Command Handler invoked while in "
                                                        "an wrong state: %1%")
                                          % GetProfilingStateName(currentState)));
    case ProfilingState::Active:
    {
        // Process the packet
        if (!(packet.GetPacketFamily() == 0u && packet.GetPacketId() == 4u))
        {
            throw armnn::InvalidArgumentException(boost::str(boost::format("Expected Packet family = 0, id = 4 but "
                                                                           "received family = %1%, id = %2%")
                                                  % packet.GetPacketFamily()
                                                  % packet.GetPacketId()));
        }

        // Parse the packet to get the capture period and counter UIDs
        CaptureData captureData;
        ParseData(packet, captureData);

        // Get the capture data
        uint32_t capturePeriod = captureData.GetCapturePeriod();
        // Validate that the capture period is within the acceptable range.
        if (capturePeriod > 0  && capturePeriod < LOWEST_CAPTURE_PERIOD)
        {
            capturePeriod = LOWEST_CAPTURE_PERIOD;
        }
        const std::vector<uint16_t>& counterIds = captureData.GetCounterIds();

        // Check whether the selected counter UIDs are valid
        std::vector<uint16_t> validCounterIds;
        for (uint16_t counterId : counterIds)
        {
            // Check whether the counter is registered
            if (!m_ReadCounterValues.IsCounterRegistered(counterId))
            {
                // Invalid counter UID, ignore it and continue
                continue;
            }
            // The counter is valid
            validCounterIds.emplace_back(counterId);
        }

        std::sort(validCounterIds.begin(), validCounterIds.end());

        auto backendIdStart = std::find_if(validCounterIds.begin(), validCounterIds.end(), [&](uint16_t& counterId)
        {
            return counterId > m_MaxArmCounterId;
        });

        std::set<armnn::BackendId> activeBackends;
        std::set<uint16_t> backendCounterIds = std::set<uint16_t>(backendIdStart, validCounterIds.end());

        if (m_BackendCounterMap.size() != 0)
        {
            std::set<uint16_t> newCounterIds;
            std::set<uint16_t> unusedCounterIds;

            // Get any backend counter ids that is in backendCounterIds but not in m_PrevBackendCounterIds
            std::set_difference(backendCounterIds.begin(), backendCounterIds.end(),
                                m_PrevBackendCounterIds.begin(), m_PrevBackendCounterIds.end(),
                                std::inserter(newCounterIds, newCounterIds.begin()));

            // Get any backend counter ids that is in m_PrevBackendCounterIds but not in backendCounterIds
            std::set_difference(m_PrevBackendCounterIds.begin(), m_PrevBackendCounterIds.end(),
                                backendCounterIds.begin(), backendCounterIds.end(),
                                std::inserter(unusedCounterIds, unusedCounterIds.begin()));

            activeBackends = ProcessBackendCounterIds(capturePeriod, newCounterIds, unusedCounterIds);
        }
        else
        {
            activeBackends = ProcessBackendCounterIds(capturePeriod, backendCounterIds, {});
        }

        // save the new backend counter ids for next time
        m_PrevBackendCounterIds = backendCounterIds;


        // Set the capture data with only the valid armnn counter UIDs
        m_CaptureDataHolder.SetCaptureData(capturePeriod, {validCounterIds.begin(), backendIdStart}, activeBackends);

        // Echo back the Periodic Counter Selection packet to the Counter Stream Buffer
        m_SendCounterPacket.SendPeriodicCounterSelectionPacket(capturePeriod, validCounterIds);

        if (capturePeriod == 0 || validCounterIds.empty())
        {
            // No data capture stop the thread
            m_PeriodicCounterCapture.Stop();
        }
        else
        {
            // Start the Period Counter Capture thread (if not running already)
            m_PeriodicCounterCapture.Start();
        }

        break;
    }
    default:
        throw RuntimeException(boost::str(boost::format("Unknown profiling service state: %1%")
                                          % static_cast<int>(currentState)));
    }
}

std::set<armnn::BackendId> PeriodicCounterSelectionCommandHandler::ProcessBackendCounterIds(
                                                                      const u_int32_t capturePeriod,
                                                                      std::set<uint16_t> newCounterIds,
                                                                      std::set<uint16_t> unusedCounterIds)
{
    std::set<armnn::BackendId> changedBackends;
    std::set<armnn::BackendId> activeBackends = m_CaptureDataHolder.GetCaptureData().GetActiveBackends();

    for (uint16_t counterId : newCounterIds)
    {
        auto backendId = m_CounterIdMap.GetBackendId(counterId);
        m_BackendCounterMap[backendId.second].emplace_back(backendId.first);
        changedBackends.insert(backendId.second);
    }
    // Add any new backends to active backends
    activeBackends.insert(changedBackends.begin(), changedBackends.end());

    for (uint16_t counterId : unusedCounterIds)
    {
        auto backendId = m_CounterIdMap.GetBackendId(counterId);
        std::vector<uint16_t>& backendCounters = m_BackendCounterMap[backendId.second];

        backendCounters.erase(std::remove(backendCounters.begin(), backendCounters.end(), backendId.first));

        if(backendCounters.size() == 0)
        {
            // If a backend has no counters associated with it we remove it from active backends and
            // send a capture period of zero with an empty vector, this will deactivate all the backends counters
            activeBackends.erase(backendId.second);
            ActivateBackedCounters(backendId.second, 0, {});
        }
        else
        {
            changedBackends.insert(backendId.second);
        }
    }

    // If the capture period remains the same we only need to update the backends who's counters have changed
    if(capturePeriod == m_PrevCapturePeriod)
    {
        for (auto backend : changedBackends)
        {
            ActivateBackedCounters(backend, capturePeriod, m_BackendCounterMap[backend]);
        }
    }
    // Otherwise update all the backends with the new capture period and any new/unused counters
    else
    {
        for (auto backend : m_BackendCounterMap)
        {
            ActivateBackedCounters(backend.first, capturePeriod, backend.second);
        }
        if(capturePeriod == 0)
        {
            activeBackends = {};
        }
        m_PrevCapturePeriod = capturePeriod;
    }

    return activeBackends;
}

} // namespace profiling

} // namespace armnn