1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "PeriodicCounterCaptureCommandHandler.hpp"
7
8 #include <common/include/CommonProfilingUtils.hpp>
9
10 #include <armnn/utility/NumericCast.hpp>
11
12 #include <iostream>
13
14 namespace armnn
15 {
16
17 namespace gatordmock
18 {
19
ParseData(const arm::pipe::Packet & packet)20 void PeriodicCounterCaptureCommandHandler::ParseData(const arm::pipe::Packet& packet)
21 {
22 std::vector<uint16_t> counterIds;
23 std::vector<uint32_t> counterValues;
24
25 uint32_t sizeOfUint64 = armnn::numeric_cast<uint32_t>(sizeof(uint64_t));
26 uint32_t sizeOfUint32 = armnn::numeric_cast<uint32_t>(sizeof(uint32_t));
27 uint32_t sizeOfUint16 = armnn::numeric_cast<uint32_t>(sizeof(uint16_t));
28
29 uint32_t offset = 0;
30
31 if (packet.GetLength() >= 8)
32 {
33 offset = 0;
34
35 uint64_t timestamp = arm::pipe::ReadUint64(reinterpret_cast<const unsigned char*>(packet.GetData()), offset);
36
37 if (m_FirstTimestamp == 0) // detect the first timestamp we receive.
38 {
39 m_FirstTimestamp = timestamp;
40 }
41 else
42 {
43 m_SecondTimestamp = timestamp;
44 m_CurrentPeriodValue = m_SecondTimestamp - m_FirstTimestamp;
45 m_FirstTimestamp = m_SecondTimestamp;
46 }
47
48 // Length minus timestamp and header divided by the length of an indexPair
49 unsigned int counters = (packet.GetLength() - 8) / 6;
50
51 if (counters > 0)
52 {
53 counterIds.reserve(counters);
54 counterValues.reserve(counters);
55 // Move offset over timestamp area
56 offset += sizeOfUint64;
57 for (unsigned int pos = 0; pos < counters; ++pos)
58 {
59 counterIds.emplace_back(
60 arm::pipe::ReadUint16(reinterpret_cast<const unsigned char*>(packet.GetData()), offset));
61 offset += sizeOfUint16;
62
63 counterValues.emplace_back(
64 arm::pipe::ReadUint32(reinterpret_cast<const unsigned char*>(packet.GetData()), offset));
65 offset += sizeOfUint32;
66 }
67 }
68
69 m_CounterCaptureValues.m_Timestamp = timestamp;
70 m_CounterCaptureValues.m_Uids = counterIds;
71 m_CounterCaptureValues.m_Values = counterValues;
72 }
73 }
74
operator ()(const arm::pipe::Packet & packet)75 void PeriodicCounterCaptureCommandHandler::operator()(const arm::pipe::Packet& packet)
76 {
77 ParseData(packet);
78 if (!m_QuietOperation) // Are we supposed to print to stdout?
79 {
80 std::string header, body, uidString, valueString;
81
82 for (uint16_t uid : m_CounterCaptureValues.m_Uids)
83 {
84 uidString.append(std::to_string(uid));
85 uidString.append(", ");
86 }
87
88 for (uint32_t val : m_CounterCaptureValues.m_Values)
89 {
90 valueString.append(std::to_string(val));
91 valueString.append(", ");
92 }
93
94 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_CounterCaptureValues.m_Timestamp), 10));
95 body.append(" | ");
96 body.append(arm::pipe::CentreAlignFormatting(std::to_string(m_CurrentPeriodValue), 13));
97 body.append(" | ");
98 body.append(arm::pipe::CentreAlignFormatting(uidString, 10));
99 body.append(" | ");
100 body.append(arm::pipe::CentreAlignFormatting(valueString, 10));
101 body.append("\n");
102
103 if (!m_HeaderPrinted)
104 {
105 header.append(arm::pipe::CentreAlignFormatting(" Timestamp", 11));
106 header.append(" | ");
107 header.append(arm::pipe::CentreAlignFormatting("Period (us)", 13));
108 header.append(" | ");
109 header.append(arm::pipe::CentreAlignFormatting("UID's", static_cast<int>(uidString.size())));
110 header.append(" | ");
111 header.append(arm::pipe::CentreAlignFormatting("Values", 10));
112 header.append("\n");
113
114 std::cout << header;
115 m_HeaderPrinted = true;
116 }
117
118 std::cout << std::string(body.size(), '-') << "\n";
119
120 std::cout << body;
121 }
122 }
123
124 } // namespace gatordmock
125
126 } // namespace armnn
127