blob: 11ad6511fee7c4fff8ee19b7dfd7fb2f7e60776d [file] [log] [blame]
Francis Murtaghfcb8ef62019-09-20 15:40:09 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "PeriodicCounterCapture.hpp"
7
Derek Lamberti08446972019-11-26 16:38:31 +00008#include <armnn/Logging.hpp>
9
Finn Williamsf4d59a62019-10-14 15:55:18 +010010#include <iostream>
Matteo Martincighe8485382019-10-10 14:08:21 +010011
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000012namespace arm
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010013{
14
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000015namespace pipe
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010016{
17
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010018void PeriodicCounterCapture::Start()
19{
20 // Check if the capture thread is already running
Finn Williamsf4d59a62019-10-14 15:55:18 +010021 if (m_IsRunning)
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010022 {
23 // The capture thread is already running
24 return;
25 }
26
27 // Mark the capture thread as running
Finn Williamsf4d59a62019-10-14 15:55:18 +010028 m_IsRunning = true;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010029
30 // Keep the capture procedure going until the capture thread is signalled to stop
31 m_KeepRunning.store(true);
32
33 // Start the new capture thread.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +010034 m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Capture, this, std::ref(m_ReadCounterValues));
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010035}
36
37void PeriodicCounterCapture::Stop()
38{
Matteo Martincighe8485382019-10-10 14:08:21 +010039 // Signal the capture thread to stop
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010040 m_KeepRunning.store(false);
41
Matteo Martincighe8485382019-10-10 14:08:21 +010042 // Check that the capture thread is running
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010043 if (m_PeriodCaptureThread.joinable())
44 {
Matteo Martincighe8485382019-10-10 14:08:21 +010045 // Wait for the capture thread to complete operations
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010046 m_PeriodCaptureThread.join();
47 }
Finn Williamsf4d59a62019-10-14 15:55:18 +010048
Matteo Martincigh8d9590e2019-10-15 09:35:29 +010049 // Mark the capture thread as not running
Finn Williamsf4d59a62019-10-14 15:55:18 +010050 m_IsRunning = false;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010051}
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010052
53CaptureData PeriodicCounterCapture::ReadCaptureData()
54{
55 return m_CaptureDataHolder.GetCaptureData();
56}
57
Finn Williams032bc742020-02-12 11:02:34 +000058void PeriodicCounterCapture::DispatchPeriodicCounterCapturePacket(
59 const armnn::BackendId& backendId, const std::vector<Timestamp>& timestampValues)
60{
61 // Report counter values
Derek Lambertieac4adb2020-08-25 13:05:59 +010062 for (const auto& timestampInfo : timestampValues)
Finn Williams032bc742020-02-12 11:02:34 +000063 {
64 std::vector<CounterValue> backendCounterValues = timestampInfo.counterValues;
65 for_each(backendCounterValues.begin(), backendCounterValues.end(), [&](CounterValue& backendCounterValue)
66 {
67 // translate the counterId to globalCounterId
68 backendCounterValue.counterId = m_CounterIdMap.GetGlobalId(backendCounterValue.counterId, backendId);
69 });
70
71 // Send Periodic Counter Capture Packet for the Timestamp
72 m_SendCounterPacket.SendPeriodicCounterCapturePacket(timestampInfo.timestamp, backendCounterValues);
73 }
74}
75
Finn Williamsf3fcf322020-05-11 14:38:02 +010076void PeriodicCounterCapture::Capture(IReadCounterValues& readCounterValues)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010077{
Finn Williamsf4d59a62019-10-14 15:55:18 +010078 do
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010079 {
Matteo Martincighe8485382019-10-10 14:08:21 +010080 // Check if the current capture data indicates that there's data capture
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010081 auto currentCaptureData = ReadCaptureData();
Matteo Martincighe8485382019-10-10 14:08:21 +010082 const std::vector<uint16_t>& counterIds = currentCaptureData.GetCounterIds();
Finn Williams032bc742020-02-12 11:02:34 +000083 const uint32_t capturePeriod = currentCaptureData.GetCapturePeriod();
Finn Williamsf4d59a62019-10-14 15:55:18 +010084
Finn Williams032bc742020-02-12 11:02:34 +000085 if (capturePeriod == 0)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010086 {
Finn Williams032bc742020-02-12 11:02:34 +000087 // No data capture, wait the indicated capture period (milliseconds), if it is not zero
88 std::this_thread::sleep_for(std::chrono::milliseconds(50u));
Finn Williamsf4d59a62019-10-14 15:55:18 +010089 continue;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010090 }
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010091
Finn Williams032bc742020-02-12 11:02:34 +000092 if(counterIds.size() != 0)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010093 {
Finn Williams032bc742020-02-12 11:02:34 +000094 std::vector<CounterValue> counterValues;
95
96 auto numCounters = counterIds.size();
97 counterValues.reserve(numCounters);
98
99 // Create a vector of pairs of CounterIndexes and Values
100 for (uint16_t index = 0; index < numCounters; ++index)
Matteo Martincighe8485382019-10-10 14:08:21 +0100101 {
Finn Williams032bc742020-02-12 11:02:34 +0000102 auto requestedId = counterIds[index];
103 uint32_t counterValue = 0;
104 try
105 {
Finn Williamsf3fcf322020-05-11 14:38:02 +0100106 counterValue = readCounterValues.GetDeltaCounterValue(requestedId);
Finn Williams032bc742020-02-12 11:02:34 +0000107 }
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000108 catch (const armnn::Exception& e)
Finn Williams032bc742020-02-12 11:02:34 +0000109 {
110 // Report the error and continue
111 ARMNN_LOG(warning) << "An error has occurred when getting a counter value: "
112 << e.what();
113 continue;
114 }
115
116 counterValues.emplace_back(CounterValue {requestedId, counterValue });
Matteo Martincighe8485382019-10-10 14:08:21 +0100117 }
Finn Williams032bc742020-02-12 11:02:34 +0000118
119 // Send Periodic Counter Capture Packet for the Timestamp
120 m_SendCounterPacket.SendPeriodicCounterCapturePacket(GetTimestamp(), counterValues);
Matteo Martincighe0e6efc2019-10-04 17:17:42 +0100121 }
Francis Murtaghfcb8ef62019-09-20 15:40:09 +0100122
Finn Williams032bc742020-02-12 11:02:34 +0000123 // Report counter values for each active backend
124 auto activeBackends = currentCaptureData.GetActiveBackends();
125 for_each(activeBackends.begin(), activeBackends.end(), [&](const armnn::BackendId& backendId)
126 {
127 DispatchPeriodicCounterCapturePacket(
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100128 backendId, m_BackendProfilingContexts.at(backendId)->ReportCounterValues());
Finn Williams032bc742020-02-12 11:02:34 +0000129 });
Matteo Martincighe8485382019-10-10 14:08:21 +0100130
Matteo Martincighe8485382019-10-10 14:08:21 +0100131 // Wait the indicated capture period (microseconds)
Finn Williams032bc742020-02-12 11:02:34 +0000132 std::this_thread::sleep_for(std::chrono::microseconds(capturePeriod));
Finn Williamsf4d59a62019-10-14 15:55:18 +0100133 }
134 while (m_KeepRunning.load());
Francis Murtaghfcb8ef62019-09-20 15:40:09 +0100135}
136
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000137} // namespace pipe
Francis Murtaghfcb8ef62019-09-20 15:40:09 +0100138
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000139} // namespace arm