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