blob: 9002bfc0654d4afff0111eb1473954d9b6926ff0 [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
8namespace armnn
9{
10
11namespace profiling
12{
13
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010014void PeriodicCounterCapture::Start()
15{
16 // Check if the capture thread is already running
17 if (m_IsRunning.load())
18 {
19 // The capture thread is already running
20 return;
21 }
22
23 // Mark the capture thread as running
24 m_IsRunning.store(true);
25
26 // Keep the capture procedure going until the capture thread is signalled to stop
27 m_KeepRunning.store(true);
28
29 // Start the new capture thread.
30 m_PeriodCaptureThread = std::thread(&PeriodicCounterCapture::Capture,
31 this,
32 std::ref(m_ReadCounterValues));
33}
34
35void PeriodicCounterCapture::Stop()
36{
37 m_KeepRunning.store(false);
38
39 if (m_PeriodCaptureThread.joinable())
40 {
41 m_PeriodCaptureThread.join();
42 }
43}
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010044
45CaptureData PeriodicCounterCapture::ReadCaptureData()
46{
47 return m_CaptureDataHolder.GetCaptureData();
48}
49
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010050void PeriodicCounterCapture::Capture(const IReadCounterValues& readCounterValues)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010051{
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010052 while (m_KeepRunning.load())
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010053 {
54 auto currentCaptureData = ReadCaptureData();
55 std::vector<uint16_t> counterIds = currentCaptureData.GetCounterIds();
56 if (currentCaptureData.GetCapturePeriod() == 0 || counterIds.empty())
57 {
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010058 m_KeepRunning.store(false);
59 break;
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010060 }
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010061
62 std::vector<std::pair<uint16_t, uint32_t>> values;
63 auto numCounters = counterIds.size();
64 values.reserve(numCounters);
65
66 // Create vector of pairs of CounterIndexes and Values
67 uint32_t counterValue = 0;
68 for (uint16_t index = 0; index < numCounters; ++index)
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010069 {
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010070 auto requestedId = counterIds[index];
71 counterValue = readCounterValues.GetCounterValue(requestedId);
72 values.emplace_back(std::make_pair(requestedId, counterValue));
73 }
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010074
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010075 #if USE_CLOCK_MONOTONIC_RAW
76 using clock = MonotonicClockRaw;
77 #else
78 using clock = std::chrono::steady_clock;
79 #endif
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010080
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010081 // Take a timestamp
82 auto timestamp = clock::now();
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010083
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010084 m_SendCounterPacket.SendPeriodicCounterCapturePacket(
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010085 static_cast<uint64_t>(timestamp.time_since_epoch().count()), values);
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010086 std::this_thread::sleep_for(std::chrono::milliseconds(currentCaptureData.GetCapturePeriod()));
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010087 }
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010088
Matteo Martincighe0e6efc2019-10-04 17:17:42 +010089 m_IsRunning.store(false);
Francis Murtaghfcb8ef62019-09-20 15:40:09 +010090}
91
92} // namespace profiling
93
94} // namespace armnn