blob: 9dd7064c901479809b2558904f6d2b236e3b4eb5 [file] [log] [blame]
Colm Donelana21620d2019-10-11 13:09:49 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Colm Donelana21620d2019-10-11 13:09:49 +01006#include "PeriodicCounterCaptureCommandHandler.hpp"
7
Matteo Martincigh65984272019-10-17 13:26:21 +01008#include <ProfilingUtils.hpp>
9
Colm Donelana21620d2019-10-11 13:09:49 +010010#include <boost/numeric/conversion/cast.hpp>
11
Matteo Martincigh65984272019-10-17 13:26:21 +010012#include <iostream>
13
Colm Donelana21620d2019-10-11 13:09:49 +010014namespace armnn
15{
16
17namespace gatordmock
18{
19
20using boost::numeric_cast;
21
Colm Donelana21620d2019-10-11 13:09:49 +010022void 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
71 m_CounterCaptureValues.m_Timestamp = timestamp;
72 m_CounterCaptureValues.m_Uids = counterIds;
73 m_CounterCaptureValues.m_Values = counterValues;
74 }
75}
76
77void PeriodicCounterCaptureCommandHandler::operator()(const profiling::Packet& packet)
78{
79 ParseData(packet);
Colm Donelanb682d842019-10-16 12:24:20 +010080 if (!m_QuietOperation) // Are we supposed to print to stdout?
Colm Donelana21620d2019-10-11 13:09:49 +010081 {
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
Keith Davis3201eea2019-10-24 17:30:41 +010096 body.append(profiling::CentreAlignFormatting(std::to_string(m_CounterCaptureValues.m_Timestamp), 10));
Colm Donelana21620d2019-10-11 13:09:49 +010097 body.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +010098 body.append(profiling::CentreAlignFormatting(std::to_string(m_CurrentPeriodValue), 13));
Colm Donelana21620d2019-10-11 13:09:49 +010099 body.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100100 body.append(profiling::CentreAlignFormatting(uidString, 10));
Colm Donelana21620d2019-10-11 13:09:49 +0100101 body.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100102 body.append(profiling::CentreAlignFormatting(valueString, 10));
Colm Donelana21620d2019-10-11 13:09:49 +0100103 body.append("\n");
104
105 if (!m_HeaderPrinted)
106 {
Keith Davis3201eea2019-10-24 17:30:41 +0100107 header.append(profiling::CentreAlignFormatting(" Timestamp", 11));
Colm Donelana21620d2019-10-11 13:09:49 +0100108 header.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100109 header.append(profiling::CentreAlignFormatting("Period (us)", 13));
Colm Donelana21620d2019-10-11 13:09:49 +0100110 header.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100111 header.append(profiling::CentreAlignFormatting("UID's", static_cast<int>(uidString.size())));
Colm Donelana21620d2019-10-11 13:09:49 +0100112 header.append(" | ");
Keith Davis3201eea2019-10-24 17:30:41 +0100113 header.append(profiling::CentreAlignFormatting("Values", 10));
Colm Donelana21620d2019-10-11 13:09:49 +0100114 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
Matteo Martincigh65984272019-10-17 13:26:21 +0100128} // namespace armnn