blob: 25d1a1789a616cdb216d543f1868c8bc6b078a13 [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
Jim Flynn6c9f17d2022-03-10 23:13:01 +00008#include <common/include/Logging.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +00009
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.
Jim Flynne195a042022-04-12 17:19:28 +010034#if !defined(ARMNN_DISABLE_THREADS)
Matteo Martincigh8d9590e2019-10-15 09:35:29 +010035 m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Capture, this, std::ref(m_ReadCounterValues));
Jim Flynne195a042022-04-12 17:19:28 +010036#endif
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010037}
38
39void PeriodicCounterCapture::Stop()
40{
Matteo Martincighe8485382019-10-10 14:08:21 +010041 // Signal the capture thread to stop
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010042 m_KeepRunning.store(false);
43
Jim Flynne195a042022-04-12 17:19:28 +010044#if !defined(ARMNN_DISABLE_THREADS)
Matteo Martincighe8485382019-10-10 14:08:21 +010045 // Check that the capture thread is running
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010046 if (m_PeriodCaptureThread.joinable())
47 {
Matteo Martincighe8485382019-10-10 14:08:21 +010048 // Wait for the capture thread to complete operations
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010049 m_PeriodCaptureThread.join();
50 }
Jim Flynne195a042022-04-12 17:19:28 +010051#endif
Finn Williamsf4d59a62019-10-14 15:55:18 +010052
Matteo Martincigh8d9590e2019-10-15 09:35:29 +010053 // Mark the capture thread as not running
Finn Williamsf4d59a62019-10-14 15:55:18 +010054 m_IsRunning = false;
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010055}
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010056
57CaptureData PeriodicCounterCapture::ReadCaptureData()
58{
59 return m_CaptureDataHolder.GetCaptureData();
60}
61
Finn Williams032bc742020-02-12 11:02:34 +000062void PeriodicCounterCapture::DispatchPeriodicCounterCapturePacket(
Cathal Corbett6f073722022-03-04 12:11:09 +000063 const std::string& backendId, const std::vector<Timestamp>& timestampValues)
Finn Williams032bc742020-02-12 11:02:34 +000064{
65 // Report counter values
Derek Lambertieac4adb2020-08-25 13:05:59 +010066 for (const auto& timestampInfo : timestampValues)
Finn Williams032bc742020-02-12 11:02:34 +000067 {
68 std::vector<CounterValue> backendCounterValues = timestampInfo.counterValues;
69 for_each(backendCounterValues.begin(), backendCounterValues.end(), [&](CounterValue& backendCounterValue)
70 {
71 // translate the counterId to globalCounterId
72 backendCounterValue.counterId = m_CounterIdMap.GetGlobalId(backendCounterValue.counterId, backendId);
73 });
74
75 // Send Periodic Counter Capture Packet for the Timestamp
76 m_SendCounterPacket.SendPeriodicCounterCapturePacket(timestampInfo.timestamp, backendCounterValues);
77 }
78}
79
Finn Williamsf3fcf322020-05-11 14:38:02 +010080void PeriodicCounterCapture::Capture(IReadCounterValues& readCounterValues)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010081{
Finn Williamsf4d59a62019-10-14 15:55:18 +010082 do
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010083 {
Matteo Martincighe8485382019-10-10 14:08:21 +010084 // Check if the current capture data indicates that there's data capture
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010085 auto currentCaptureData = ReadCaptureData();
Matteo Martincighe8485382019-10-10 14:08:21 +010086 const std::vector<uint16_t>& counterIds = currentCaptureData.GetCounterIds();
Finn Williams032bc742020-02-12 11:02:34 +000087 const uint32_t capturePeriod = currentCaptureData.GetCapturePeriod();
Finn Williamsf4d59a62019-10-14 15:55:18 +010088
Finn Williams032bc742020-02-12 11:02:34 +000089 if (capturePeriod == 0)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010090 {
Finn Williams032bc742020-02-12 11:02:34 +000091 // No data capture, wait the indicated capture period (milliseconds), if it is not zero
Jim Flynne195a042022-04-12 17:19:28 +010092#if !defined(ARMNN_DISABLE_THREADS)
Finn Williams032bc742020-02-12 11:02:34 +000093 std::this_thread::sleep_for(std::chrono::milliseconds(50u));
Jim Flynne195a042022-04-12 17:19:28 +010094#endif
Finn Williamsf4d59a62019-10-14 15:55:18 +010095 continue;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010096 }
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010097
Finn Williams032bc742020-02-12 11:02:34 +000098 if(counterIds.size() != 0)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010099 {
Finn Williams032bc742020-02-12 11:02:34 +0000100 std::vector<CounterValue> counterValues;
101
102 auto numCounters = counterIds.size();
103 counterValues.reserve(numCounters);
104
105 // Create a vector of pairs of CounterIndexes and Values
106 for (uint16_t index = 0; index < numCounters; ++index)
Matteo Martincighe8485382019-10-10 14:08:21 +0100107 {
Finn Williams032bc742020-02-12 11:02:34 +0000108 auto requestedId = counterIds[index];
109 uint32_t counterValue = 0;
110 try
111 {
Finn Williamsf3fcf322020-05-11 14:38:02 +0100112 counterValue = readCounterValues.GetDeltaCounterValue(requestedId);
Finn Williams032bc742020-02-12 11:02:34 +0000113 }
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000114 catch (const arm::pipe::ProfilingException& e)
Finn Williams032bc742020-02-12 11:02:34 +0000115 {
116 // Report the error and continue
Jim Flynn6c9f17d2022-03-10 23:13:01 +0000117 ARM_PIPE_LOG(warning) << "An error has occurred when getting a counter value: "
Finn Williams032bc742020-02-12 11:02:34 +0000118 << e.what();
119 continue;
120 }
121
122 counterValues.emplace_back(CounterValue {requestedId, counterValue });
Matteo Martincighe8485382019-10-10 14:08:21 +0100123 }
Finn Williams032bc742020-02-12 11:02:34 +0000124
125 // Send Periodic Counter Capture Packet for the Timestamp
126 m_SendCounterPacket.SendPeriodicCounterCapturePacket(GetTimestamp(), counterValues);
Matteo Martincighe0e6efc2019-10-04 17:17:42 +0100127 }
Francis Murtaghfcb8ef62019-09-20 15:40:09 +0100128
Finn Williams032bc742020-02-12 11:02:34 +0000129 // Report counter values for each active backend
130 auto activeBackends = currentCaptureData.GetActiveBackends();
Cathal Corbett6f073722022-03-04 12:11:09 +0000131 for_each(activeBackends.begin(), activeBackends.end(), [&](const std::string& backendId)
Finn Williams032bc742020-02-12 11:02:34 +0000132 {
133 DispatchPeriodicCounterCapturePacket(
Finn Williamsfe5a24b2020-04-09 16:05:28 +0100134 backendId, m_BackendProfilingContexts.at(backendId)->ReportCounterValues());
Finn Williams032bc742020-02-12 11:02:34 +0000135 });
Matteo Martincighe8485382019-10-10 14:08:21 +0100136
Matteo Martincighe8485382019-10-10 14:08:21 +0100137 // Wait the indicated capture period (microseconds)
Jim Flynne195a042022-04-12 17:19:28 +0100138#if !defined(ARMNN_DISABLE_THREADS)
Finn Williams032bc742020-02-12 11:02:34 +0000139 std::this_thread::sleep_for(std::chrono::microseconds(capturePeriod));
Jim Flynne195a042022-04-12 17:19:28 +0100140#endif
Finn Williamsf4d59a62019-10-14 15:55:18 +0100141 }
142 while (m_KeepRunning.load());
Francis Murtaghfcb8ef62019-09-20 15:40:09 +0100143}
144
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000145} // namespace pipe
Francis Murtaghfcb8ef62019-09-20 15:40:09 +0100146
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000147} // namespace arm