Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 6 | #include "PeriodicCounterCaptureCommandHandler.hpp" |
| 7 | |
Matteo Martincigh | 6598427 | 2019-10-17 13:26:21 +0100 | [diff] [blame] | 8 | #include <ProfilingUtils.hpp> |
| 9 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 10 | #include <boost/numeric/conversion/cast.hpp> |
| 11 | |
Matteo Martincigh | 6598427 | 2019-10-17 13:26:21 +0100 | [diff] [blame] | 12 | #include <iostream> |
| 13 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 14 | namespace armnn |
| 15 | { |
| 16 | |
| 17 | namespace gatordmock |
| 18 | { |
| 19 | |
| 20 | using boost::numeric_cast; |
| 21 | |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 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 | |
| 71 | m_CounterCaptureValues.m_Timestamp = timestamp; |
| 72 | m_CounterCaptureValues.m_Uids = counterIds; |
| 73 | m_CounterCaptureValues.m_Values = counterValues; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void PeriodicCounterCaptureCommandHandler::operator()(const profiling::Packet& packet) |
| 78 | { |
| 79 | ParseData(packet); |
Colm Donelan | b682d84 | 2019-10-16 12:24:20 +0100 | [diff] [blame] | 80 | if (!m_QuietOperation) // Are we supposed to print to stdout? |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 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 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 96 | body.append(profiling::CentreAlignFormatting(std::to_string(m_CounterCaptureValues.m_Timestamp), 10)); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 97 | body.append(" | "); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 98 | body.append(profiling::CentreAlignFormatting(std::to_string(m_CurrentPeriodValue), 13)); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 99 | body.append(" | "); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 100 | body.append(profiling::CentreAlignFormatting(uidString, 10)); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 101 | body.append(" | "); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 102 | body.append(profiling::CentreAlignFormatting(valueString, 10)); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 103 | body.append("\n"); |
| 104 | |
| 105 | if (!m_HeaderPrinted) |
| 106 | { |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 107 | header.append(profiling::CentreAlignFormatting(" Timestamp", 11)); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 108 | header.append(" | "); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 109 | header.append(profiling::CentreAlignFormatting("Period (us)", 13)); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 110 | header.append(" | "); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 111 | header.append(profiling::CentreAlignFormatting("UID's", static_cast<int>(uidString.size()))); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 112 | header.append(" | "); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 113 | header.append(profiling::CentreAlignFormatting("Values", 10)); |
Colm Donelan | a21620d | 2019-10-11 13:09:49 +0100 | [diff] [blame] | 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 | |
Matteo Martincigh | 6598427 | 2019-10-17 13:26:21 +0100 | [diff] [blame] | 128 | } // namespace armnn |