blob: b0bfeb5677f847276dc33580bb4189cb985b980b [file] [log] [blame]
Ferran Balaguer73882172019-09-02 16:39:42 +01001//
Jim Flynn83d08a92020-07-09 13:48:16 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Ferran Balaguer73882172019-09-02 16:39:42 +01003// SPDX-License-Identifier: MIT
4//
5
6#include "SendCounterPacket.hpp"
Jim Flynnbbfe6032020-07-20 16:57:44 +01007#include <common/include/EncodeVersion.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +01008
9#include <armnn/Exceptions.hpp>
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010010#include <armnn/Conversion.hpp>
Rob Hughesbdee4262020-01-07 17:05:24 +000011#include <Processes.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010012#include <armnn/utility/Assert.hpp>
Matthew Sloyan371b70e2020-09-11 10:14:57 +010013#include <armnn/utility/NumericCast.hpp>
Finn Williamse09fc822020-04-29 13:17:30 +010014#include <common/include/Constants.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010015#include <common/include/SwTrace.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +010016
Jan Eilers156113c2020-09-09 19:11:16 +010017#include <fmt/format.h>
Ferran Balaguer73882172019-09-02 16:39:42 +010018
Matteo Martincigh149528e2019-09-05 12:02:04 +010019#include <cstring>
Ferran Balaguer73882172019-09-02 16:39:42 +010020
21namespace armnn
22{
23
24namespace profiling
25{
26
Ferran Balaguer73882172019-09-02 16:39:42 +010027void SendCounterPacket::SendStreamMetaDataPacket()
28{
Finn Williamsd44815f2020-05-01 13:25:55 +010029 const std::string info(GetSoftwareInfo());
30 const std::string hardwareVersion(GetHardwareVersion());
31 const std::string softwareVersion(GetSoftwareVersion());
32 const std::string processName = GetProcessName().substr(0, 60);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010033
Matthew Sloyan371b70e2020-09-11 10:14:57 +010034 const uint32_t infoSize = armnn::numeric_cast<uint32_t>(info.size()) + 1;
35 const uint32_t hardwareVersionSize = armnn::numeric_cast<uint32_t>(hardwareVersion.size()) + 1;
36 const uint32_t softwareVersionSize = armnn::numeric_cast<uint32_t>(softwareVersion.size()) + 1;
37 const uint32_t processNameSize = armnn::numeric_cast<uint32_t>(processName.size()) + 1;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010038
Finn Williamsd44815f2020-05-01 13:25:55 +010039 const uint32_t sizeUint32 = sizeof(uint32_t);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010040
Finn Williamsd44815f2020-05-01 13:25:55 +010041 const uint32_t headerSize = 2 * sizeUint32;
42 const uint32_t bodySize = 10 * sizeUint32;
43 const uint32_t packetVersionCountSize = sizeUint32;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010044
45 // Supported Packets
Jim Flynn83d08a92020-07-09 13:48:16 +010046 // Packet Encoding version 1.0.0
47 // Control packet family
48 // Stream metadata packet (packet family=0; packet id=0)
49 // Connection Acknowledged packet ( packet family=0, packet id=1) Version 1.0.0
50 // Counter Directory packet (packet family=0; packet id=2) Version 1.0.0
51 // Request Counter Directory packet ( packet family=0, packet id=3) Version 1.0.0
52 // Periodic Counter Selection packet ( packet family=0, packet id=4) Version 1.0.0
53 // Per Job Counter Selection packet ( packet family=0, packet id=5) Version 1.0.0
54 // Activate Timeline Reporting (packet family = 0, packet id = 6) Version 1.0.0
55 // Deactivate Timeline Reporting (packet family = 0, packet id = 7) Version 1.0.0
56 // Counter Packet Family
57 // Periodic Counter Capture (packet_family = 3, packet_class = 0, packet_type = 0) Version 1.0.0
58 // Per-Job Counter Capture (packet_family = 3, packet_class = 1, packet_type = 0,1) Version 1.0.0
59 // Timeline Packet Family
60 // Timeline Message Directory (packet_family = 1, packet_class = 0, packet_type = 0) Version 1.0.0
61 // Timeline Message (packet_family = 1, packet_class = 0, packet_type = 1) Version 1.0.0
62 std::vector<std::pair<uint32_t, uint32_t>> packetVersions;
Jim Flynnbbfe6032020-07-20 16:57:44 +010063 packetVersions.push_back(std::make_pair(ConstructHeader(0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
64 packetVersions.push_back(std::make_pair(ConstructHeader(0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
65 packetVersions.push_back(std::make_pair(ConstructHeader(0, 2), arm::pipe::EncodeVersion(1, 0, 0)));
66 packetVersions.push_back(std::make_pair(ConstructHeader(0, 3), arm::pipe::EncodeVersion(1, 0, 0)));
67 packetVersions.push_back(std::make_pair(ConstructHeader(0, 4), arm::pipe::EncodeVersion(1, 0, 0)));
68 packetVersions.push_back(std::make_pair(ConstructHeader(0, 5), arm::pipe::EncodeVersion(1, 0, 0)));
69 packetVersions.push_back(std::make_pair(ConstructHeader(0, 6), arm::pipe::EncodeVersion(1, 0, 0)));
70 packetVersions.push_back(std::make_pair(ConstructHeader(0, 7), arm::pipe::EncodeVersion(1, 0, 0)));
71 packetVersions.push_back(std::make_pair(ConstructHeader(3, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
72 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 0), arm::pipe::EncodeVersion(1, 0, 0)));
73 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 1), arm::pipe::EncodeVersion(1, 0, 0)));
74 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
75 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
Matthew Sloyan371b70e2020-09-11 10:14:57 +010076 uint32_t numberOfVersions = armnn::numeric_cast<uint32_t>(packetVersions.size());
77 uint32_t packetVersionSize = armnn::numeric_cast<uint32_t>(numberOfVersions * 2 * sizeUint32);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010078
Matthew Sloyan371b70e2020-09-11 10:14:57 +010079 const uint32_t payloadSize = armnn::numeric_cast<uint32_t>(infoSize + hardwareVersionSize +
80 softwareVersionSize + processNameSize +
81 packetVersionCountSize + packetVersionSize);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010082
Finn Williamsd44815f2020-05-01 13:25:55 +010083 const uint32_t totalSize = headerSize + bodySize + payloadSize;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010084 uint32_t offset = 0;
85 uint32_t reserved = 0;
86
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000087 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010088
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010089 if (writeBuffer == nullptr || reserved < totalSize)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010090 {
Matteo Martincigh149528e2019-09-05 12:02:04 +010091 CancelOperationAndThrow<BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010092 writeBuffer,
Jan Eilers156113c2020-09-09 19:11:16 +010093 fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010094 }
95
96 try
97 {
98 // Create header
99
100 WriteUint32(writeBuffer, offset, 0);
101 offset += sizeUint32;
102 WriteUint32(writeBuffer, offset, totalSize - headerSize);
103
104 // Packet body
105
106 offset += sizeUint32;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100107 WriteUint32(writeBuffer, offset, arm::pipe::PIPE_MAGIC); // pipe_magic
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100108 offset += sizeUint32;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100109 WriteUint32(writeBuffer, offset, arm::pipe::EncodeVersion(1, 0, 0)); // stream_metadata_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100110 offset += sizeUint32;
111 WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length
112 offset += sizeUint32;
Rob Hughesbdee4262020-01-07 17:05:24 +0000113 int pid = armnnUtils::Processes::GetCurrentId();
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100114 WriteUint32(writeBuffer, offset, armnn::numeric_cast<uint32_t>(pid)); // pid
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100115 offset += sizeUint32;
116 uint32_t poolOffset = bodySize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100117 WriteUint32(writeBuffer, offset, poolOffset); // offset_info
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100118 offset += sizeUint32;
119 poolOffset += infoSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100120 WriteUint32(writeBuffer, offset, poolOffset); // offset_hw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100121 offset += sizeUint32;
122 poolOffset += hardwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100123 WriteUint32(writeBuffer, offset, poolOffset); // offset_sw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100124 offset += sizeUint32;
125 poolOffset += softwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100126 WriteUint32(writeBuffer, offset, poolOffset); // offset_process_name
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100127 offset += sizeUint32;
128 poolOffset += processNameSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100129 WriteUint32(writeBuffer, offset, poolOffset); // offset_packet_version_table
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100130 offset += sizeUint32;
131 WriteUint32(writeBuffer, offset, 0); // reserved
132 offset += sizeUint32;
133
134 // Pool
135
Matteo Martincigh149528e2019-09-05 12:02:04 +0100136 if (infoSize)
137 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100138 memcpy(&writeBuffer->GetWritableData()[offset], info.c_str(), infoSize);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100139 offset += infoSize;
140 }
141
Finn Williamsa0de0562020-04-22 12:27:37 +0100142 memcpy(&writeBuffer->GetWritableData()[offset], hardwareVersion.c_str(), hardwareVersionSize);
143 offset += hardwareVersionSize;
144 memcpy(&writeBuffer->GetWritableData()[offset], softwareVersion.c_str(), softwareVersionSize);
145 offset += softwareVersionSize;
146 memcpy(&writeBuffer->GetWritableData()[offset], processName.c_str(), processNameSize);
147 offset += processNameSize;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100148
Jim Flynn83d08a92020-07-09 13:48:16 +0100149 if (!packetVersions.empty())
Matteo Martincigh149528e2019-09-05 12:02:04 +0100150 {
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100151 // Packet Version Count
Jim Flynn83d08a92020-07-09 13:48:16 +0100152 WriteUint32(writeBuffer, offset, numberOfVersions << 16);
153 offset += sizeUint32;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100154
155 // Packet Version Entries
Jim Flynn83d08a92020-07-09 13:48:16 +0100156 for (std::pair<uint32_t, uint32_t>& packetVersion : packetVersions)
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100157 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100158 WriteUint32(writeBuffer, offset, packetVersion.first);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100159 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100160 WriteUint32(writeBuffer, offset, packetVersion.second);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100161 offset += sizeUint32;
162 }
163 }
164 }
165 catch(...)
166 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100167 CancelOperationAndThrow<RuntimeException>(writeBuffer, "Error processing packet.");
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100168 }
169
Sadik Armagan3896b472020-02-10 12:24:15 +0000170 m_BufferManager.Commit(writeBuffer, totalSize, false);
Ferran Balaguer73882172019-09-02 16:39:42 +0100171}
172
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100173bool SendCounterPacket::CreateCategoryRecord(const CategoryPtr& category,
174 const Counters& counters,
175 CategoryRecord& categoryRecord,
176 std::string& errorMessage)
Ferran Balaguer73882172019-09-02 16:39:42 +0100177{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100178 ARMNN_ASSERT(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100179
180 const std::string& categoryName = category->m_Name;
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100181 ARMNN_ASSERT(!categoryName.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100182
Keith Davis33ed2212020-03-30 10:43:41 +0100183 // Remove any duplicate counters
184 std::vector<uint16_t> categoryCounters;
185 for (size_t counterIndex = 0; counterIndex < category->m_Counters.size(); ++counterIndex)
186 {
187 uint16_t counterUid = category->m_Counters.at(counterIndex);
188 auto it = counters.find(counterUid);
189 if (it == counters.end())
190 {
Jan Eilers156113c2020-09-09 19:11:16 +0100191 errorMessage = fmt::format("Counter ({}) not found in category ({})",
192 counterUid,
193 category->m_Name );
Keith Davis33ed2212020-03-30 10:43:41 +0100194 return false;
195 }
196
197 const CounterPtr& counter = it->second;
198
199 if (counterUid == counter->m_MaxCounterUid)
200 {
201 categoryCounters.emplace_back(counterUid);
202 }
203 }
204 if (categoryCounters.empty())
205 {
Jan Eilers156113c2020-09-09 19:11:16 +0100206 errorMessage = fmt::format("No valid counters found in category ({})", categoryName);
Keith Davis33ed2212020-03-30 10:43:41 +0100207 return false;
208 }
209
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100210 // Utils
Finn Williamsd44815f2020-05-01 13:25:55 +0100211 const size_t uint32_t_size = sizeof(uint32_t);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100212
213 // Convert the device name into a SWTrace namestring
214 std::vector<uint32_t> categoryNameBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100215 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(categoryName, categoryNameBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100216 {
Jan Eilers156113c2020-09-09 19:11:16 +0100217 errorMessage = fmt::format("Cannot convert the name of category ({}) to an SWTrace namestring",
218 categoryName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100219 return false;
220 }
221
Finn Williamsd44815f2020-05-01 13:25:55 +0100222 // Category record word 1:
223 // 16:31 [16] event_count: number of events belonging to this category
224 // 0:15 [16] reserved: all zeros
225 const uint32_t categoryRecordWord1 = static_cast<uint32_t>(categoryCounters.size()) << 16;
226
227 // Category record word 2:
228 // 0:31 [32] event_pointer_table_offset: offset from the beginning of the category data pool to
229 // the event_pointer_table
230 const uint32_t categoryRecordWord2 = static_cast<uint32_t>(3u * uint32_t_size);
231
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100232 // Process the event records
Finn Williamsd44815f2020-05-01 13:25:55 +0100233 const size_t counterCount = categoryCounters.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100234 std::vector<EventRecord> eventRecords(counterCount);
235 std::vector<uint32_t> eventRecordOffsets(counterCount, 0);
236 size_t eventRecordsSize = 0;
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100237 uint32_t eventRecordsOffset = armnn::numeric_cast<uint32_t>(
238 (eventRecords.size() + categoryNameBuffer.size()) * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100239 for (size_t counterIndex = 0, eventRecordIndex = 0, eventRecordOffsetIndex = 0;
240 counterIndex < counterCount;
241 counterIndex++, eventRecordIndex++, eventRecordOffsetIndex++)
242 {
243 uint16_t counterUid = categoryCounters.at(counterIndex);
244 auto it = counters.find(counterUid);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100245 const CounterPtr& counter = it->second;
246
247 EventRecord& eventRecord = eventRecords.at(eventRecordIndex);
248 if (!CreateEventRecord(counter, eventRecord, errorMessage))
249 {
250 return false;
251 }
252
253 // Update the total size in words of the event records
254 eventRecordsSize += eventRecord.size();
255
256 // Add the event record offset to the event pointer table offset field
257 eventRecordOffsets[eventRecordOffsetIndex] = eventRecordsOffset;
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100258 eventRecordsOffset += armnn::numeric_cast<uint32_t>(eventRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100259 }
260
261 // Category record word 3:
262 // 0:31 [32] name_offset (offset from the beginning of the category data pool to the name field)
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100263 const uint32_t categoryRecordWord3 = armnn::numeric_cast<uint32_t>(
264 (3u + eventRecordOffsets.size()) * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100265
266 // Calculate the size in words of the category record
Finn Williamsd44815f2020-05-01 13:25:55 +0100267 const size_t categoryRecordSize = 3u +// The size of the fixed part (device + counter_set + event_count +
268 // reserved + event_pointer_table_offset + name_offset)
269 eventRecordOffsets.size() + // The size of the variable part (
270 categoryNameBuffer.size() + // the event pointer table + the category name
271 eventRecordsSize; // including the null-terminator + the event records)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100272
273 // Allocate the necessary space for the category record
274 categoryRecord.resize(categoryRecordSize);
275
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100276 ARMNN_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100277 // Create the category record
Sadik Armagan4c998992020-02-25 12:44:44 +0000278 categoryRecord[0] = categoryRecordWord1; // event_count + reserved
279 categoryRecord[1] = categoryRecordWord2; // event_pointer_table_offset
280 categoryRecord[2] = categoryRecordWord3; // name_offset
281 auto offset = categoryRecord.begin() + 3u;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100282 std::copy(eventRecordOffsets.begin(), eventRecordOffsets.end(), offset); // event_pointer_table
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100283 offset += eventRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100284 std::copy(categoryNameBuffer.begin(), categoryNameBuffer.end(), offset); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100285 offset += categoryNameBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100286 for (const EventRecord& eventRecord : eventRecords)
287 {
288 std::copy(eventRecord.begin(), eventRecord.end(), offset); // event_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100289 offset += eventRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100290 }
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100291 ARMNN_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100292
293 return true;
294}
295
296bool SendCounterPacket::CreateDeviceRecord(const DevicePtr& device,
297 DeviceRecord& deviceRecord,
298 std::string& errorMessage)
299{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100300 ARMNN_ASSERT(device);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100301
302 uint16_t deviceUid = device->m_Uid;
303 const std::string& deviceName = device->m_Name;
304 uint16_t deviceCores = device->m_Cores;
305
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100306 ARMNN_ASSERT(!deviceName.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100307
308 // Device record word 0:
309 // 16:31 [16] uid: the unique identifier for the device
310 // 0:15 [16] cores: the number of individual streams of counters for one or more cores of some device
Finn Williamsd44815f2020-05-01 13:25:55 +0100311 const uint32_t deviceRecordWord0 = (static_cast<uint32_t>(deviceUid) << 16) |
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100312 (static_cast<uint32_t>(deviceCores));
313
314 // Device record word 1:
315 // 0:31 [32] name_offset: offset from the beginning of the device record pool to the name field
Finn Williams6be1e9b2020-05-15 11:21:54 +0100316 const uint32_t deviceRecordWord1 = 8u; // The offset is always eight here, as the name field is always
Finn Williamsd44815f2020-05-01 13:25:55 +0100317 // the first (and only) item in the pool and there are two device words
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100318
319 // Convert the device name into a SWTrace string
320 std::vector<uint32_t> deviceNameBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100321 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(deviceName, deviceNameBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100322 {
Jan Eilers156113c2020-09-09 19:11:16 +0100323 errorMessage = fmt::format("Cannot convert the name of device {} ({}) to an SWTrace string",
324 deviceUid,
325 deviceName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100326 return false;
327 }
328
329 // Calculate the size in words of the device record
Finn Williamsd44815f2020-05-01 13:25:55 +0100330 const size_t deviceRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100331 deviceNameBuffer.size(); // The size of the variable part (the device name including
332 // the null-terminator)
333
334 // Allocate the necessary space for the device record
335 deviceRecord.resize(deviceRecordSize);
336
337 // Create the device record
338 deviceRecord[0] = deviceRecordWord0; // uid + core
339 deviceRecord[1] = deviceRecordWord1; // name_offset
340 auto offset = deviceRecord.begin() + 2u;
341 std::copy(deviceNameBuffer.begin(), deviceNameBuffer.end(), offset); // name
342
343 return true;
344}
345
346bool SendCounterPacket::CreateCounterSetRecord(const CounterSetPtr& counterSet,
347 CounterSetRecord& counterSetRecord,
348 std::string& errorMessage)
349{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100350 ARMNN_ASSERT(counterSet);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100351
352 uint16_t counterSetUid = counterSet->m_Uid;
353 const std::string& counterSetName = counterSet->m_Name;
354 uint16_t counterSetCount = counterSet->m_Count;
355
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100356 ARMNN_ASSERT(!counterSetName.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100357
358 // Counter set record word 0:
359 // 16:31 [16] uid: the unique identifier for the counter_set
360 // 0:15 [16] count: the number of counters which can be active in this set at any one time
Finn Williamsd44815f2020-05-01 13:25:55 +0100361 const uint32_t counterSetRecordWord0 = (static_cast<uint32_t>(counterSetUid) << 16) |
362 (static_cast<uint32_t>(counterSetCount));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100363
364 // Counter set record word 1:
365 // 0:31 [32] name_offset: offset from the beginning of the counter set pool to the name field
Finn Williams6be1e9b2020-05-15 11:21:54 +0100366 const uint32_t counterSetRecordWord1 = 8u; // The offset is always eight here, as the name field is always
Finn Williamsd44815f2020-05-01 13:25:55 +0100367 // the first (and only) item in the pool after the two counter set words
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100368
369 // Convert the device name into a SWTrace namestring
370 std::vector<uint32_t> counterSetNameBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100371 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(counterSet->m_Name, counterSetNameBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100372 {
Jan Eilers156113c2020-09-09 19:11:16 +0100373 errorMessage = fmt::format("Cannot convert the name of counter set {} ({}) to an SWTrace namestring",
374 counterSetUid,
375 counterSetName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100376 return false;
377 }
378
379 // Calculate the size in words of the counter set record
Finn Williamsd44815f2020-05-01 13:25:55 +0100380 const size_t counterSetRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
381 counterSetNameBuffer.size(); // The size of the variable part (the counter set
382 // name including the null-terminator)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100383
384 // Allocate the space for the counter set record
385 counterSetRecord.resize(counterSetRecordSize);
386
387 // Create the counter set record
388 counterSetRecord[0] = counterSetRecordWord0; // uid + core
389 counterSetRecord[1] = counterSetRecordWord1; // name_offset
390 auto offset = counterSetRecord.begin() + 2u;
391 std::copy(counterSetNameBuffer.begin(), counterSetNameBuffer.end(), offset); // name
392
393 return true;
394}
395
396bool SendCounterPacket::CreateEventRecord(const CounterPtr& counter,
397 EventRecord& eventRecord,
398 std::string& errorMessage)
399{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100400 ARMNN_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100401
402 uint16_t counterUid = counter->m_Uid;
403 uint16_t maxCounterUid = counter->m_MaxCounterUid;
404 uint16_t deviceUid = counter->m_DeviceUid;
405 uint16_t counterSetUid = counter->m_CounterSetUid;
406 uint16_t counterClass = counter->m_Class;
407 uint16_t counterInterpolation = counter->m_Interpolation;
408 double counterMultiplier = counter->m_Multiplier;
409 const std::string& counterName = counter->m_Name;
410 const std::string& counterDescription = counter->m_Description;
411 const std::string& counterUnits = counter->m_Units;
412
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100413 ARMNN_ASSERT(counterClass == 0 || counterClass == 1);
414 ARMNN_ASSERT(counterInterpolation == 0 || counterInterpolation == 1);
415 ARMNN_ASSERT(counterMultiplier);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100416
417 // Utils
Finn Williamsd44815f2020-05-01 13:25:55 +0100418 const size_t uint32_t_size = sizeof(uint32_t);
419 // eventRecordBlockSize is the size of the fixed part
420 // (counter_uid + max_counter_uid + device +
421 // counter_set + class + interpolation +
422 // multiplier + name_offset + description_offset +
423 // units_offset)
424 const size_t eventRecordBlockSize = 8u;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100425
426 // Event record word 0:
427 // 16:31 [16] max_counter_uid: if the device this event is associated with has more than one core and there
428 // is one of these counters per core this value will be set to
429 // (counter_uid + cores (from device_record)) - 1.
430 // If there is only a single core then this value will be the same as
431 // the counter_uid value
432 // 0:15 [16] count_uid: unique ID for the counter. Must be unique across all counters in all categories
Finn Williamsd44815f2020-05-01 13:25:55 +0100433 const uint32_t eventRecordWord0 = (static_cast<uint32_t>(maxCounterUid) << 16) |
434 (static_cast<uint32_t>(counterUid));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100435
436 // Event record word 1:
437 // 16:31 [16] device: UID of the device this event is associated with. Set to zero if the event is NOT
438 // associated with a device
439 // 0:15 [16] counter_set: UID of the counter_set this event is associated with. Set to zero if the event
440 // is NOT associated with a counter_set
Finn Williamsd44815f2020-05-01 13:25:55 +0100441 const uint32_t eventRecordWord1 = (static_cast<uint32_t>(deviceUid) << 16) |
442 (static_cast<uint32_t>(counterSetUid));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100443
444 // Event record word 2:
445 // 16:31 [16] class: type describing how to treat each data point in a stream of data points
446 // 0:15 [16] interpolation: type describing how to interpolate each data point in a stream of data points
Finn Williamsd44815f2020-05-01 13:25:55 +0100447 const uint32_t eventRecordWord2 = (static_cast<uint32_t>(counterClass) << 16) |
448 (static_cast<uint32_t>(counterInterpolation));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100449
450 // Event record word 3-4:
451 // 0:63 [64] multiplier: internal data stream is represented as integer values, this allows scaling of
452 // those values as if they are fixed point numbers. Zero is not a valid value
453 uint32_t multiplier[2] = { 0u, 0u };
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100454 ARMNN_ASSERT(sizeof(counterMultiplier) == sizeof(multiplier));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100455 std::memcpy(multiplier, &counterMultiplier, sizeof(multiplier));
Finn Williamsd44815f2020-05-01 13:25:55 +0100456 const uint32_t eventRecordWord3 = multiplier[0];
457 const uint32_t eventRecordWord4 = multiplier[1];
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100458
459 // Event record word 5:
460 // 0:31 [32] name_offset: offset from the beginning of the event record pool to the name field
Finn Williamsd44815f2020-05-01 13:25:55 +0100461 const uint32_t eventRecordWord5 = static_cast<uint32_t>(eventRecordBlockSize * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100462
463 // Convert the counter name into a SWTrace string
464 std::vector<uint32_t> counterNameBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100465 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(counterName, counterNameBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100466 {
Jan Eilers156113c2020-09-09 19:11:16 +0100467 errorMessage = fmt::format("Cannot convert the name of counter {} (name: {}) to an SWTrace string",
468 counterUid,
469 counterName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100470 return false;
471 }
472
473 // Event record word 6:
474 // 0:31 [32] description_offset: offset from the beginning of the event record pool to the description field
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100475 // The size of the name buffer in bytes
Finn Williamsd44815f2020-05-01 13:25:55 +0100476 uint32_t eventRecordWord6 =
477 static_cast<uint32_t>((counterNameBuffer.size() + eventRecordBlockSize) * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100478
479 // Convert the counter description into a SWTrace string
480 std::vector<uint32_t> counterDescriptionBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100481 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(counterDescription, counterDescriptionBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100482 {
Jan Eilers156113c2020-09-09 19:11:16 +0100483 errorMessage = fmt::format("Cannot convert the description of counter {} (description: {}) "
484 "to an SWTrace string",
485 counterUid,
486 counterName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100487 return false;
488 }
489
490 // Event record word 7:
491 // 0:31 [32] units_offset: (optional) offset from the beginning of the event record pool to the units field.
492 // An offset value of zero indicates this field is not provided
493 bool includeUnits = !counterUnits.empty();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100494 // The size of the description buffer in bytes
Finn Williamsd44815f2020-05-01 13:25:55 +0100495 const uint32_t eventRecordWord7 = includeUnits ?
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100496 eventRecordWord6 +
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100497 armnn::numeric_cast<uint32_t>(counterDescriptionBuffer.size()
Finn Williamsd44815f2020-05-01 13:25:55 +0100498 * uint32_t_size) :
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100499 0;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100500
501 // Convert the counter units into a SWTrace namestring (optional)
502 std::vector<uint32_t> counterUnitsBuffer;
503 if (includeUnits)
504 {
505 // Convert the counter units into a SWTrace namestring
Jim Flynnbbfe6032020-07-20 16:57:44 +0100506 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(counterUnits, counterUnitsBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100507 {
Jan Eilers156113c2020-09-09 19:11:16 +0100508 errorMessage = fmt::format("Cannot convert the units of counter {} (units: {}) to an SWTrace string",
509 counterUid,
510 counterName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100511 return false;
512 }
513 }
514
515 // Calculate the size in words of the event record
Finn Williamsd44815f2020-05-01 13:25:55 +0100516 const size_t eventRecordSize = eventRecordBlockSize +
517 counterNameBuffer.size() + // The size of the variable part (the counter name,
518 counterDescriptionBuffer.size() + // description and units
519 counterUnitsBuffer.size(); // including the null-terminator)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100520
521 // Allocate the space for the event record
522 eventRecord.resize(eventRecordSize);
523
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100524 ARMNN_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100525 // Create the event record
526 eventRecord[0] = eventRecordWord0; // max_counter_uid + counter_uid
527 eventRecord[1] = eventRecordWord1; // device + counter_set
528 eventRecord[2] = eventRecordWord2; // class + interpolation
529 eventRecord[3] = eventRecordWord3; // multiplier
530 eventRecord[4] = eventRecordWord4; // multiplier
531 eventRecord[5] = eventRecordWord5; // name_offset
532 eventRecord[6] = eventRecordWord6; // description_offset
533 eventRecord[7] = eventRecordWord7; // units_offset
534 auto offset = eventRecord.begin() + 8u;
535 std::copy(counterNameBuffer.begin(), counterNameBuffer.end(), offset); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100536 offset += counterNameBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100537 std::copy(counterDescriptionBuffer.begin(), counterDescriptionBuffer.end(), offset); // description
538 if (includeUnits)
539 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100540 offset += counterDescriptionBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100541 std::copy(counterUnitsBuffer.begin(), counterUnitsBuffer.end(), offset); // units
542 }
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100543 ARMNN_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100544
545 return true;
546}
547
548void SendCounterPacket::SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory)
549{
550 // Get the amount of data that needs to be put into the packet
Finn Williams985fecf2020-04-30 11:06:43 +0100551 const uint16_t categoryCount = counterDirectory.GetCategoryCount();
552 const uint16_t deviceCount = counterDirectory.GetDeviceCount();
553 const uint16_t counterSetCount = counterDirectory.GetCounterSetCount();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100554
555 // Utils
Finn Williams985fecf2020-04-30 11:06:43 +0100556 const size_t uint32_t_size = sizeof(uint32_t);
557 const size_t packetHeaderSize = 2u;
558 const size_t bodyHeaderSize = 6u;
559 const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * uint32_t_size;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100560
561 // Initialize the offset for the pointer tables
562 uint32_t pointerTableOffset = 0;
563
564 // --------------
565 // Device records
566 // --------------
567
568 // Process device records
569 std::vector<DeviceRecord> deviceRecords(deviceCount);
570 const Devices& devices = counterDirectory.GetDevices();
571 std::vector<uint32_t> deviceRecordOffsets(deviceCount, 0); // device_records_pointer_table
572 size_t deviceRecordsSize = 0;
573 size_t deviceIndex = 0;
574 size_t deviceRecordOffsetIndex = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +0100575
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100576 pointerTableOffset = armnn::numeric_cast<uint32_t>(deviceCount * uint32_t_size +
577 counterSetCount * uint32_t_size +
578 categoryCount * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100579 for (auto it = devices.begin(); it != devices.end(); it++)
580 {
581 const DevicePtr& device = it->second;
582 DeviceRecord& deviceRecord = deviceRecords.at(deviceIndex);
583
584 std::string errorMessage;
585 if (!CreateDeviceRecord(device, deviceRecord, errorMessage))
586 {
587 CancelOperationAndThrow<RuntimeException>(errorMessage);
588 }
589
590 // Update the total size in words of the device records
591 deviceRecordsSize += deviceRecord.size();
592
593 // Add the device record offset to the device records pointer table offset field
594 deviceRecordOffsets[deviceRecordOffsetIndex] = pointerTableOffset;
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100595 pointerTableOffset += armnn::numeric_cast<uint32_t>(deviceRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100596
597 deviceIndex++;
598 deviceRecordOffsetIndex++;
599 }
600
601 // -------------------
602 // Counter set records
603 // -------------------
604
605 // Process counter set records
606 std::vector<CounterSetRecord> counterSetRecords(counterSetCount);
607 const CounterSets& counterSets = counterDirectory.GetCounterSets();
608 std::vector<uint32_t> counterSetRecordOffsets(counterSetCount, 0); // counter_set_records_pointer_table
609 size_t counterSetRecordsSize = 0;
610 size_t counterSetIndex = 0;
611 size_t counterSetRecordOffsetIndex = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +0100612
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100613 pointerTableOffset -= armnn::numeric_cast<uint32_t>(deviceCount * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100614 for (auto it = counterSets.begin(); it != counterSets.end(); it++)
615 {
616 const CounterSetPtr& counterSet = it->second;
617 CounterSetRecord& counterSetRecord = counterSetRecords.at(counterSetIndex);
618
619 std::string errorMessage;
620 if (!CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage))
621 {
622 CancelOperationAndThrow<RuntimeException>(errorMessage);
623 }
624
625 // Update the total size in words of the counter set records
626 counterSetRecordsSize += counterSetRecord.size();
627
628 // Add the counter set record offset to the counter set records pointer table offset field
629 counterSetRecordOffsets[counterSetRecordOffsetIndex] = pointerTableOffset;
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100630 pointerTableOffset += armnn::numeric_cast<uint32_t>(counterSetRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100631
632 counterSetIndex++;
633 counterSetRecordOffsetIndex++;
634 }
635
636 // ----------------
637 // Category records
638 // ----------------
639
640 // Process category records
641 std::vector<CategoryRecord> categoryRecords(categoryCount);
642 const Categories& categories = counterDirectory.GetCategories();
643 std::vector<uint32_t> categoryRecordOffsets(categoryCount, 0); // category_records_pointer_table
644 size_t categoryRecordsSize = 0;
645 size_t categoryIndex = 0;
646 size_t categoryRecordOffsetIndex = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +0100647
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100648 pointerTableOffset -= armnn::numeric_cast<uint32_t>(counterSetCount * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100649 for (auto it = categories.begin(); it != categories.end(); it++)
650 {
651 const CategoryPtr& category = *it;
652 CategoryRecord& categoryRecord = categoryRecords.at(categoryIndex);
653
654 std::string errorMessage;
655 if (!CreateCategoryRecord(category, counterDirectory.GetCounters(), categoryRecord, errorMessage))
656 {
657 CancelOperationAndThrow<RuntimeException>(errorMessage);
658 }
659
660 // Update the total size in words of the category records
661 categoryRecordsSize += categoryRecord.size();
662
663 // Add the category record offset to the category records pointer table offset field
664 categoryRecordOffsets[categoryRecordOffsetIndex] = pointerTableOffset;
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100665 pointerTableOffset += armnn::numeric_cast<uint32_t>(categoryRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100666
667 categoryIndex++;
668 categoryRecordOffsetIndex++;
669 }
670
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100671 // Calculate the length in words of the counter directory packet's data (excludes the packet header size)
Finn Williams985fecf2020-04-30 11:06:43 +0100672 const size_t counterDirectoryPacketDataLength =
673 bodyHeaderSize + // The size of the body header
674 deviceRecordOffsets.size() + // The size of the device records pointer table
675 counterSetRecordOffsets.size() + // The size of counter set pointer table
676 categoryRecordOffsets.size() + // The size of category records pointer table
677 deviceRecordsSize + // The total size of the device records
678 counterSetRecordsSize + // The total size of the counter set records
679 categoryRecordsSize; // The total size of the category records
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100680
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100681 // Calculate the size in words of the counter directory packet (the data length plus the packet header size)
Finn Williams985fecf2020-04-30 11:06:43 +0100682 const size_t counterDirectoryPacketSize = packetHeaderSize + // The size of the packet header
683 counterDirectoryPacketDataLength; // The data length
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100684
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100685 // Allocate the necessary space for the counter directory packet
686 std::vector<uint32_t> counterDirectoryPacket(counterDirectoryPacketSize, 0);
687
688 // -------------
689 // Packet header
690 // -------------
691
692 // Packet header word 0:
693 // 26:31 [6] packet_family: control Packet Family
694 // 16:25 [10] packet_id: packet identifier
695 // 8:15 [8] reserved: all zeros
696 // 0:7 [8] reserved: all zeros
697 uint32_t packetFamily = 0;
698 uint32_t packetId = 2;
699 uint32_t packetHeaderWord0 = ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16);
700
701 // Packet header word 1:
702 // 0:31 [32] data_length: length of data, in bytes
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100703 uint32_t packetHeaderWord1 = armnn::numeric_cast<uint32_t>(
704 counterDirectoryPacketDataLength * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100705
706 // Create the packet header
707 uint32_t packetHeader[2]
708 {
709 packetHeaderWord0, // packet_family + packet_id + reserved + reserved
710 packetHeaderWord1 // data_length
711 };
712
713 // -----------
714 // Body header
715 // -----------
716
717 // Body header word 0:
718 // 16:31 [16] device_records_count: number of entries in the device_records_pointer_table
719 // 0:15 [16] reserved: all zeros
Finn Williams985fecf2020-04-30 11:06:43 +0100720 const uint32_t bodyHeaderWord0 = static_cast<uint32_t>(deviceCount) << 16;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100721
722 // Body header word 1:
723 // 0:31 [32] device_records_pointer_table_offset: offset to the device_records_pointer_table
Finn Williams985fecf2020-04-30 11:06:43 +0100724 const uint32_t bodyHeaderWord1 = bodyHeaderSizeBytes; // The offset is always the bodyHeaderSize,
725 // as the device record pointer table field
726 // is always the first item in the pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100727
728 // Body header word 2:
729 // 16:31 [16] counter_set_count: number of entries in the counter_set_pointer_table
730 // 0:15 [16] reserved: all zeros
Finn Williams985fecf2020-04-30 11:06:43 +0100731 const uint32_t bodyHeaderWord2 = static_cast<uint32_t>(counterSetCount) << 16;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100732
733 // Body header word 3:
734 // 0:31 [32] counter_set_pointer_table_offset: offset to the counter_set_pointer_table
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100735 const uint32_t bodyHeaderWord3 = armnn::numeric_cast<uint32_t>(deviceRecordOffsets.size() *
736 uint32_t_size + // The size of the
737 bodyHeaderSizeBytes); // device records pointer table
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100738
739 // Body header word 4:
740 // 16:31 [16] categories_count: number of entries in the categories_pointer_table
741 // 0:15 [16] reserved: all zeros
Finn Williams985fecf2020-04-30 11:06:43 +0100742 const uint32_t bodyHeaderWord4 = static_cast<uint32_t>(categoryCount) << 16;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100743
744 // Body header word 3:
745 // 0:31 [32] categories_pointer_table_offset: offset to the categories_pointer_table
Finn Williams985fecf2020-04-30 11:06:43 +0100746 const uint32_t bodyHeaderWord5 =
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100747 armnn::numeric_cast<uint32_t>(
Finn Williams985fecf2020-04-30 11:06:43 +0100748 deviceRecordOffsets.size() * uint32_t_size + // The size of the device records
749 counterSetRecordOffsets.size() * uint32_t_size // pointer table, plus the size of
750 + bodyHeaderSizeBytes); // the counter set pointer table
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100751
752 // Create the body header
Finn Williams985fecf2020-04-30 11:06:43 +0100753 const uint32_t bodyHeader[bodyHeaderSize]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100754 {
755 bodyHeaderWord0, // device_records_count + reserved
756 bodyHeaderWord1, // device_records_pointer_table_offset
757 bodyHeaderWord2, // counter_set_count + reserved
758 bodyHeaderWord3, // counter_set_pointer_table_offset
759 bodyHeaderWord4, // categories_count + reserved
760 bodyHeaderWord5 // categories_pointer_table_offset
761 };
762
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100763 ARMNN_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100764 // Create the counter directory packet
765 auto counterDirectoryPacketOffset = counterDirectoryPacket.begin();
766 // packet_header
767 std::copy(packetHeader, packetHeader + packetHeaderSize, counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100768 counterDirectoryPacketOffset += packetHeaderSize;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100769 // body_header
770 std::copy(bodyHeader, bodyHeader + bodyHeaderSize, counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100771 counterDirectoryPacketOffset += bodyHeaderSize;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100772 // device_records_pointer_table
773 std::copy(deviceRecordOffsets.begin(), deviceRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100774 counterDirectoryPacketOffset += deviceRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100775 // counter_set_pointer_table
776 std::copy(counterSetRecordOffsets.begin(), counterSetRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100777 counterDirectoryPacketOffset += counterSetRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100778 // category_pointer_table
779 std::copy(categoryRecordOffsets.begin(), categoryRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100780 counterDirectoryPacketOffset += categoryRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100781 // device_records
782 for (const DeviceRecord& deviceRecord : deviceRecords)
783 {
784 std::copy(deviceRecord.begin(), deviceRecord.end(), counterDirectoryPacketOffset); // device_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100785 counterDirectoryPacketOffset += deviceRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100786 }
787 // counter_set_records
788 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
789 {
790 std::copy(counterSetRecord.begin(), counterSetRecord.end(), counterDirectoryPacketOffset); // counter_set_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100791 counterDirectoryPacketOffset += counterSetRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100792 }
793 // category_records
794 for (const CategoryRecord& categoryRecord : categoryRecords)
795 {
796 std::copy(categoryRecord.begin(), categoryRecord.end(), counterDirectoryPacketOffset); // category_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100797 counterDirectoryPacketOffset += categoryRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100798 }
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100799 ARMNN_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100800
801 // Calculate the total size in bytes of the counter directory packet
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100802 uint32_t totalSize = armnn::numeric_cast<uint32_t>(counterDirectoryPacketSize * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100803
804 // Reserve space in the buffer for the packet
805 uint32_t reserved = 0;
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000806 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100807
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100808 if (writeBuffer == nullptr || reserved < totalSize)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100809 {
810 CancelOperationAndThrow<BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100811 writeBuffer,
Jan Eilers156113c2020-09-09 19:11:16 +0100812 fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100813 }
814
815 // Offset for writing to the buffer
816 uint32_t offset = 0;
817
818 // Write the counter directory packet to the buffer
819 for (uint32_t counterDirectoryPacketWord : counterDirectoryPacket)
820 {
821 WriteUint32(writeBuffer, offset, counterDirectoryPacketWord);
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100822 offset += armnn::numeric_cast<uint32_t>(uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100823 }
824
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100825 m_BufferManager.Commit(writeBuffer, totalSize);
Ferran Balaguer73882172019-09-02 16:39:42 +0100826}
827
Francis Murtagh3a161982019-09-04 15:25:02 +0100828void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
Ferran Balaguer73882172019-09-02 16:39:42 +0100829{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100830 uint32_t uint16_t_size = sizeof(uint16_t);
831 uint32_t uint32_t_size = sizeof(uint32_t);
832 uint32_t uint64_t_size = sizeof(uint64_t);
833
Jim Flynnfc365622019-12-04 10:07:20 +0000834 uint32_t packetFamily = 3;
Francis Murtagh3a161982019-09-04 15:25:02 +0100835 uint32_t packetClass = 0;
836 uint32_t packetType = 0;
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100837 uint32_t headerSize = 2 * uint32_t_size;
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100838 uint32_t bodySize = uint64_t_size + armnn::numeric_cast<uint32_t>(values.size()) * (uint16_t_size + uint32_t_size);
Francis Murtagh3a161982019-09-04 15:25:02 +0100839 uint32_t totalSize = headerSize + bodySize;
840 uint32_t offset = 0;
841 uint32_t reserved = 0;
842
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000843 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Francis Murtagh3a161982019-09-04 15:25:02 +0100844
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100845 if (writeBuffer == nullptr || reserved < totalSize)
Francis Murtagh3a161982019-09-04 15:25:02 +0100846 {
Matteo Martincigh149528e2019-09-05 12:02:04 +0100847 CancelOperationAndThrow<BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100848 writeBuffer,
Jan Eilers156113c2020-09-09 19:11:16 +0100849 fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize));
Francis Murtagh3a161982019-09-04 15:25:02 +0100850 }
851
852 // Create header.
853 WriteUint32(writeBuffer,
854 offset,
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100855 ((packetFamily & 0x0000003F) << 26) |
856 ((packetClass & 0x0000007F) << 19) |
857 ((packetType & 0x00000007) << 16));
858 offset += uint32_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100859 WriteUint32(writeBuffer, offset, bodySize);
860
861 // Copy captured Timestamp.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100862 offset += uint32_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100863 WriteUint64(writeBuffer, offset, timestamp);
864
865 // Copy selectedCounterIds.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100866 offset += uint64_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100867 for (const auto& pair: values)
868 {
Finn Williams032bc742020-02-12 11:02:34 +0000869 WriteUint16(writeBuffer, offset, pair.counterId);
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100870 offset += uint16_t_size;
Finn Williams032bc742020-02-12 11:02:34 +0000871 WriteUint32(writeBuffer, offset, pair.counterValue);
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100872 offset += uint32_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100873 }
874
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100875 m_BufferManager.Commit(writeBuffer, totalSize);
Ferran Balaguer73882172019-09-02 16:39:42 +0100876}
877
878void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
879 const std::vector<uint16_t>& selectedCounterIds)
880{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100881 uint32_t uint16_t_size = sizeof(uint16_t);
882 uint32_t uint32_t_size = sizeof(uint32_t);
883
Ferran Balaguer73882172019-09-02 16:39:42 +0100884 uint32_t packetFamily = 0;
885 uint32_t packetId = 4;
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100886 uint32_t headerSize = 2 * uint32_t_size;
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100887 uint32_t bodySize = uint32_t_size + armnn::numeric_cast<uint32_t>(selectedCounterIds.size()) * uint16_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100888 uint32_t totalSize = headerSize + bodySize;
889 uint32_t offset = 0;
890 uint32_t reserved = 0;
891
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000892 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Ferran Balaguer73882172019-09-02 16:39:42 +0100893
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100894 if (writeBuffer == nullptr || reserved < totalSize)
Ferran Balaguer73882172019-09-02 16:39:42 +0100895 {
Matteo Martincigh149528e2019-09-05 12:02:04 +0100896 CancelOperationAndThrow<BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100897 writeBuffer,
Jan Eilers156113c2020-09-09 19:11:16 +0100898 fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize));
Ferran Balaguer73882172019-09-02 16:39:42 +0100899 }
900
901 // Create header.
902 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100903 offset += uint32_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100904 WriteUint32(writeBuffer, offset, bodySize);
905
906 // Copy capturePeriod.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100907 offset += uint32_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100908 WriteUint32(writeBuffer, offset, capturePeriod);
909
910 // Copy selectedCounterIds.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100911 offset += uint32_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100912 for(const uint16_t& id: selectedCounterIds)
913 {
914 WriteUint16(writeBuffer, offset, id);
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100915 offset += uint16_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100916 }
917
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100918 m_BufferManager.Commit(writeBuffer, totalSize);
Ferran Balaguer73882172019-09-02 16:39:42 +0100919}
920
Ferran Balaguer73882172019-09-02 16:39:42 +0100921} // namespace profiling
922
Matteo Martincigh149528e2019-09-05 12:02:04 +0100923} // namespace armnn