2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "SendCounterPacket.hpp"
7 #include "EncodeVersion.hpp"
8 #include "ProfilingUtils.hpp"
10 #include <armnn/Exceptions.hpp>
12 #include <boost/format.hpp>
13 #include <boost/numeric/conversion/cast.hpp>
14 #include <boost/core/ignore_unused.hpp>
24 using boost::numeric_cast;
26 const unsigned int SendCounterPacket::PIPE_MAGIC;
27 const unsigned int SendCounterPacket::MAX_METADATA_PACKET_LENGTH;
29 void SendCounterPacket::SendStreamMetaDataPacket()
31 std::string info(GetSoftwareInfo());
32 std::string hardwareVersion(GetHardwareVersion());
33 std::string softwareVersion(GetSoftwareVersion());
34 std::string processName = GetProcessName().substr(0, 60);
36 uint32_t infoSize = numeric_cast<uint32_t>(info.size()) > 0 ? numeric_cast<uint32_t>(info.size()) + 1 : 0;
37 uint32_t hardwareVersionSize = numeric_cast<uint32_t>(hardwareVersion.size()) > 0 ?
38 numeric_cast<uint32_t>(hardwareVersion.size()) + 1 : 0;
39 uint32_t softwareVersionSize = numeric_cast<uint32_t>(softwareVersion.size()) > 0 ?
40 numeric_cast<uint32_t>(softwareVersion.size()) + 1 : 0;
41 uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) > 0 ?
42 numeric_cast<uint32_t>(processName.size()) + 1 : 0;
44 uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
46 uint32_t headerSize = 2 * sizeUint32;
47 uint32_t bodySize = 10 * sizeUint32;
48 uint32_t packetVersionCountSize = sizeUint32;
51 // Stream metadata packet (packet family=0; packet id=0)
52 // Connection Acknowledged packet (packet family=0, packet id=1)
53 // Counter Directory packet (packet family=0; packet id=2)
54 // Request Counter Directory packet (packet family=0, packet id=3)
55 // Periodic Counter Selection packet (packet family=0, packet id=4)
56 // Periodic Counter Capture packet (packet family=1, packet class=0, type=0)
57 uint32_t packetVersionEntries = 6;
59 uint32_t payloadSize = numeric_cast<uint32_t>(infoSize + hardwareVersionSize + softwareVersionSize +
60 processNameSize + packetVersionCountSize +
61 (packetVersionEntries * 2 * sizeUint32));
63 uint32_t totalSize = headerSize + bodySize + payloadSize;
65 uint32_t reserved = 0;
67 unsigned char *writeBuffer = m_Buffer.Reserve(totalSize, reserved);
69 if (reserved < totalSize)
71 CancelOperationAndThrow<BufferExhaustion>(
72 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.")
76 if (writeBuffer == nullptr)
78 CancelOperationAndThrow<RuntimeException>("Error reserving buffer memory.");
85 WriteUint32(writeBuffer, offset, 0);
87 WriteUint32(writeBuffer, offset, totalSize - headerSize);
92 WriteUint32(writeBuffer, offset, PIPE_MAGIC); // pipe_magic
94 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0)); // stream_metadata_version
96 WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length
98 WriteUint32(writeBuffer, offset, numeric_cast<uint32_t>(getpid())); // pid
100 uint32_t poolOffset = bodySize;
101 WriteUint32(writeBuffer, offset, infoSize ? poolOffset : 0); // offset_info
102 offset += sizeUint32;
103 poolOffset += infoSize;
104 WriteUint32(writeBuffer, offset, hardwareVersionSize ? poolOffset : 0); // offset_hw_version
105 offset += sizeUint32;
106 poolOffset += hardwareVersionSize;
107 WriteUint32(writeBuffer, offset, softwareVersionSize ? poolOffset : 0); // offset_sw_version
108 offset += sizeUint32;
109 poolOffset += softwareVersionSize;
110 WriteUint32(writeBuffer, offset, processNameSize ? poolOffset : 0); // offset_process_name
111 offset += sizeUint32;
112 poolOffset += processNameSize;
113 WriteUint32(writeBuffer, offset, packetVersionEntries ? poolOffset : 0); // offset_packet_version_table
114 offset += sizeUint32;
115 WriteUint32(writeBuffer, offset, 0); // reserved
116 offset += sizeUint32;
122 memcpy(&writeBuffer[offset], info.c_str(), infoSize);
126 if (hardwareVersionSize)
128 memcpy(&writeBuffer[offset], hardwareVersion.c_str(), hardwareVersionSize);
129 offset += hardwareVersionSize;
132 if (softwareVersionSize)
134 memcpy(&writeBuffer[offset], softwareVersion.c_str(), softwareVersionSize);
135 offset += softwareVersionSize;
140 memcpy(&writeBuffer[offset], processName.c_str(), processNameSize);
141 offset += processNameSize;
144 if (packetVersionEntries)
146 // Packet Version Count
147 WriteUint32(writeBuffer, offset, packetVersionEntries << 16);
149 // Packet Version Entries
150 uint32_t packetFamily = 0;
151 uint32_t packetId = 0;
153 offset += sizeUint32;
154 for (uint32_t i = 0; i < packetVersionEntries - 1; ++i)
156 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId++ & 0x3FF) << 16));
157 offset += sizeUint32;
158 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
159 offset += sizeUint32;
165 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
166 offset += sizeUint32;
167 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
172 CancelOperationAndThrow<RuntimeException>("Error processing packet.");
175 m_Buffer.Commit(totalSize);
178 void SendCounterPacket::SendCounterDirectoryPacket(const CounterDirectory& counterDirectory)
180 throw armnn::UnimplementedException();
183 void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
185 uint32_t packetFamily = 1;
186 uint32_t packetClass = 0;
187 uint32_t packetType = 0;
188 uint32_t headerSize = numeric_cast<uint32_t>(2 * sizeof(uint32_t));
189 uint32_t bodySize = numeric_cast<uint32_t>((1 * sizeof(uint64_t)) +
190 (values.size() * (sizeof(uint16_t) + sizeof(uint32_t))));
191 uint32_t totalSize = headerSize + bodySize;
193 uint32_t reserved = 0;
195 unsigned char* writeBuffer = m_Buffer.Reserve(totalSize, reserved);
197 if (reserved < totalSize)
199 CancelOperationAndThrow<BufferExhaustion>(
200 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.")
204 if (writeBuffer == nullptr)
206 CancelOperationAndThrow<RuntimeException>("Error reserving buffer memory.");
210 WriteUint32(writeBuffer,
212 ((packetFamily & 0x3F) << 26) | ((packetClass & 0x3FF) << 19) | ((packetType & 0x3FFF) << 16));
213 offset += numeric_cast<uint32_t>(sizeof(uint32_t));
214 WriteUint32(writeBuffer, offset, bodySize);
216 // Copy captured Timestamp.
217 offset += numeric_cast<uint32_t>(sizeof(uint32_t));
218 WriteUint64(writeBuffer, offset, timestamp);
220 // Copy selectedCounterIds.
221 offset += numeric_cast<uint32_t>(sizeof(uint64_t));
222 for (const auto& pair: values)
224 WriteUint16(writeBuffer, offset, pair.first);
225 offset += numeric_cast<uint32_t>(sizeof(uint16_t));
226 WriteUint32(writeBuffer, offset, pair.second);
227 offset += numeric_cast<uint32_t>(sizeof(uint32_t));
230 m_Buffer.Commit(totalSize);
233 void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
234 const std::vector<uint16_t>& selectedCounterIds)
236 uint32_t packetFamily = 0;
237 uint32_t packetId = 4;
238 uint32_t headerSize = numeric_cast<uint32_t>(2 * sizeof(uint32_t));
239 uint32_t bodySize = numeric_cast<uint32_t>((1 * sizeof(uint32_t)) +
240 (selectedCounterIds.size() * sizeof(uint16_t)));
241 uint32_t totalSize = headerSize + bodySize;
243 uint32_t reserved = 0;
245 unsigned char* writeBuffer = m_Buffer.Reserve(totalSize, reserved);
247 if (reserved < totalSize)
249 CancelOperationAndThrow<BufferExhaustion>(
250 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.")
254 if (writeBuffer == nullptr)
256 CancelOperationAndThrow<RuntimeException>("Error reserving buffer memory.");
260 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
261 offset += numeric_cast<uint32_t>(sizeof(uint32_t));
262 WriteUint32(writeBuffer, offset, bodySize);
264 // Copy capturePeriod.
265 offset += numeric_cast<uint32_t>(sizeof(uint32_t));
266 WriteUint32(writeBuffer, offset, capturePeriod);
268 // Copy selectedCounterIds.
269 offset += numeric_cast<uint32_t>(sizeof(uint32_t));
270 for(const uint16_t& id: selectedCounterIds)
272 WriteUint16(writeBuffer, offset, id);
273 offset += numeric_cast<uint32_t>(sizeof(uint16_t));
276 m_Buffer.Commit(totalSize);
279 void SendCounterPacket::SetReadyToRead()
281 m_ReadyToRead = true;
284 } // namespace profiling