blob: 96adb953beec91448dd7aad375fc4ad1bc7ca825 [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"
Ferran Balaguer73882172019-09-02 16:39:42 +01007
Jim Flynn6730fe92022-03-10 22:57:47 +00008#include <common/include/Assert.hpp>
Jim Flynn0a013a72022-03-13 16:57:04 +00009#include <common/include/Conversion.hpp>
Jim Flynnf9db3ef2022-03-08 21:23:44 +000010#include <common/include/Constants.hpp>
11#include <common/include/EncodeVersion.hpp>
Jim Flynn77b284e2022-03-13 20:53:35 +000012#include <common/include/Processes.hpp>
Jim Flynnf9db3ef2022-03-08 21:23:44 +000013#include <common/include/ProfilingException.hpp>
14#include <common/include/SwTrace.hpp>
15
Jan Eilers156113c2020-09-09 19:11:16 +010016#include <fmt/format.h>
Ferran Balaguer73882172019-09-02 16:39:42 +010017
Matteo Martincigh149528e2019-09-05 12:02:04 +010018#include <cstring>
Ferran Balaguer73882172019-09-02 16:39:42 +010019
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000020namespace arm
Ferran Balaguer73882172019-09-02 16:39:42 +010021{
22
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000023namespace pipe
Ferran Balaguer73882172019-09-02 16:39:42 +010024{
25
Ferran Balaguer73882172019-09-02 16:39:42 +010026void SendCounterPacket::SendStreamMetaDataPacket()
27{
Jim Flynn9c85b412022-03-16 00:27:43 +000028 const std::string info(m_SoftwareInfo);
29 const std::string hardwareVersion(m_HardwareVersion);
30 const std::string softwareVersion(m_SoftwareVersion);
Finn Williamsd44815f2020-05-01 13:25:55 +010031 const std::string processName = GetProcessName().substr(0, 60);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010032
Jim Flynn75c14f42022-03-10 22:05:42 +000033 const uint32_t infoSize = arm::pipe::numeric_cast<uint32_t>(info.size()) + 1;
34 const uint32_t hardwareVersionSize = arm::pipe::numeric_cast<uint32_t>(hardwareVersion.size()) + 1;
35 const uint32_t softwareVersionSize = arm::pipe::numeric_cast<uint32_t>(softwareVersion.size()) + 1;
36 const uint32_t processNameSize = arm::pipe::numeric_cast<uint32_t>(processName.size()) + 1;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010037
Finn Williamsd44815f2020-05-01 13:25:55 +010038 const uint32_t sizeUint32 = sizeof(uint32_t);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010039
Finn Williamsd44815f2020-05-01 13:25:55 +010040 const uint32_t headerSize = 2 * sizeUint32;
41 const uint32_t bodySize = 10 * sizeUint32;
42 const uint32_t packetVersionCountSize = sizeUint32;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010043
44 // Supported Packets
Jim Flynn83d08a92020-07-09 13:48:16 +010045 // Packet Encoding version 1.0.0
46 // Control packet family
47 // Stream metadata packet (packet family=0; packet id=0)
48 // Connection Acknowledged packet ( packet family=0, packet id=1) Version 1.0.0
49 // Counter Directory packet (packet family=0; packet id=2) Version 1.0.0
50 // Request Counter Directory packet ( packet family=0, packet id=3) Version 1.0.0
51 // Periodic Counter Selection packet ( packet family=0, packet id=4) Version 1.0.0
52 // Per Job Counter Selection packet ( packet family=0, packet id=5) Version 1.0.0
53 // Activate Timeline Reporting (packet family = 0, packet id = 6) Version 1.0.0
54 // Deactivate Timeline Reporting (packet family = 0, packet id = 7) Version 1.0.0
55 // Counter Packet Family
56 // Periodic Counter Capture (packet_family = 3, packet_class = 0, packet_type = 0) Version 1.0.0
57 // Per-Job Counter Capture (packet_family = 3, packet_class = 1, packet_type = 0,1) Version 1.0.0
58 // Timeline Packet Family
59 // Timeline Message Directory (packet_family = 1, packet_class = 0, packet_type = 0) Version 1.0.0
60 // Timeline Message (packet_family = 1, packet_class = 0, packet_type = 1) Version 1.0.0
61 std::vector<std::pair<uint32_t, uint32_t>> packetVersions;
Jim Flynnbbfe6032020-07-20 16:57:44 +010062 packetVersions.push_back(std::make_pair(ConstructHeader(0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
63 packetVersions.push_back(std::make_pair(ConstructHeader(0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
64 packetVersions.push_back(std::make_pair(ConstructHeader(0, 2), arm::pipe::EncodeVersion(1, 0, 0)));
65 packetVersions.push_back(std::make_pair(ConstructHeader(0, 3), arm::pipe::EncodeVersion(1, 0, 0)));
66 packetVersions.push_back(std::make_pair(ConstructHeader(0, 4), arm::pipe::EncodeVersion(1, 0, 0)));
67 packetVersions.push_back(std::make_pair(ConstructHeader(0, 5), arm::pipe::EncodeVersion(1, 0, 0)));
68 packetVersions.push_back(std::make_pair(ConstructHeader(0, 6), arm::pipe::EncodeVersion(1, 0, 0)));
69 packetVersions.push_back(std::make_pair(ConstructHeader(0, 7), arm::pipe::EncodeVersion(1, 0, 0)));
70 packetVersions.push_back(std::make_pair(ConstructHeader(3, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
71 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 0), arm::pipe::EncodeVersion(1, 0, 0)));
72 packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 1), arm::pipe::EncodeVersion(1, 0, 0)));
73 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 0), arm::pipe::EncodeVersion(1, 0, 0)));
74 packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 1), arm::pipe::EncodeVersion(1, 0, 0)));
Jim Flynn75c14f42022-03-10 22:05:42 +000075 uint32_t numberOfVersions = arm::pipe::numeric_cast<uint32_t>(packetVersions.size());
76 uint32_t packetVersionSize = arm::pipe::numeric_cast<uint32_t>(numberOfVersions * 2 * sizeUint32);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010077
Jim Flynn75c14f42022-03-10 22:05:42 +000078 const uint32_t payloadSize = arm::pipe::numeric_cast<uint32_t>(infoSize + hardwareVersionSize +
Matthew Sloyan371b70e2020-09-11 10:14:57 +010079 softwareVersionSize + processNameSize +
80 packetVersionCountSize + packetVersionSize);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010081
Finn Williamsd44815f2020-05-01 13:25:55 +010082 const uint32_t totalSize = headerSize + bodySize + payloadSize;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010083 uint32_t offset = 0;
84 uint32_t reserved = 0;
85
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000086 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010087
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010088 if (writeBuffer == nullptr || reserved < totalSize)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010089 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +000090 CancelOperationAndThrow<arm::pipe::BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010091 writeBuffer,
Jan Eilers156113c2020-09-09 19:11:16 +010092 fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010093 }
94
95 try
96 {
97 // Create header
98
99 WriteUint32(writeBuffer, offset, 0);
100 offset += sizeUint32;
101 WriteUint32(writeBuffer, offset, totalSize - headerSize);
102
103 // Packet body
104
105 offset += sizeUint32;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100106 WriteUint32(writeBuffer, offset, arm::pipe::PIPE_MAGIC); // pipe_magic
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100107 offset += sizeUint32;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100108 WriteUint32(writeBuffer, offset, arm::pipe::EncodeVersion(1, 0, 0)); // stream_metadata_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100109 offset += sizeUint32;
110 WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length
111 offset += sizeUint32;
Jim Flynn9c85b412022-03-16 00:27:43 +0000112 int pid = arm::pipe::GetCurrentProcessId();
Jim Flynn75c14f42022-03-10 22:05:42 +0000113 WriteUint32(writeBuffer, offset, arm::pipe::numeric_cast<uint32_t>(pid)); // pid
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100114 offset += sizeUint32;
115 uint32_t poolOffset = bodySize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100116 WriteUint32(writeBuffer, offset, poolOffset); // offset_info
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100117 offset += sizeUint32;
118 poolOffset += infoSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100119 WriteUint32(writeBuffer, offset, poolOffset); // offset_hw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100120 offset += sizeUint32;
121 poolOffset += hardwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100122 WriteUint32(writeBuffer, offset, poolOffset); // offset_sw_version
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100123 offset += sizeUint32;
124 poolOffset += softwareVersionSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100125 WriteUint32(writeBuffer, offset, poolOffset); // offset_process_name
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100126 offset += sizeUint32;
127 poolOffset += processNameSize;
Finn Williamsa0de0562020-04-22 12:27:37 +0100128 WriteUint32(writeBuffer, offset, poolOffset); // offset_packet_version_table
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100129 offset += sizeUint32;
130 WriteUint32(writeBuffer, offset, 0); // reserved
131 offset += sizeUint32;
132
133 // Pool
134
Matteo Martincigh149528e2019-09-05 12:02:04 +0100135 if (infoSize)
136 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100137 memcpy(&writeBuffer->GetWritableData()[offset], info.c_str(), infoSize);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100138 offset += infoSize;
139 }
140
Finn Williamsa0de0562020-04-22 12:27:37 +0100141 memcpy(&writeBuffer->GetWritableData()[offset], hardwareVersion.c_str(), hardwareVersionSize);
142 offset += hardwareVersionSize;
143 memcpy(&writeBuffer->GetWritableData()[offset], softwareVersion.c_str(), softwareVersionSize);
144 offset += softwareVersionSize;
145 memcpy(&writeBuffer->GetWritableData()[offset], processName.c_str(), processNameSize);
146 offset += processNameSize;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100147
Jim Flynn83d08a92020-07-09 13:48:16 +0100148 if (!packetVersions.empty())
Matteo Martincigh149528e2019-09-05 12:02:04 +0100149 {
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100150 // Packet Version Count
Jim Flynn83d08a92020-07-09 13:48:16 +0100151 WriteUint32(writeBuffer, offset, numberOfVersions << 16);
152 offset += sizeUint32;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100153
154 // Packet Version Entries
Jim Flynn83d08a92020-07-09 13:48:16 +0100155 for (std::pair<uint32_t, uint32_t>& packetVersion : packetVersions)
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100156 {
Jim Flynn83d08a92020-07-09 13:48:16 +0100157 WriteUint32(writeBuffer, offset, packetVersion.first);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100158 offset += sizeUint32;
Jim Flynn83d08a92020-07-09 13:48:16 +0100159 WriteUint32(writeBuffer, offset, packetVersion.second);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100160 offset += sizeUint32;
161 }
162 }
163 }
164 catch(...)
165 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000166 CancelOperationAndThrow<arm::pipe::ProfilingException>(writeBuffer, "Error processing packet.");
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100167 }
168
Sadik Armagan3896b472020-02-10 12:24:15 +0000169 m_BufferManager.Commit(writeBuffer, totalSize, false);
Ferran Balaguer73882172019-09-02 16:39:42 +0100170}
171
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100172bool SendCounterPacket::CreateCategoryRecord(const CategoryPtr& category,
173 const Counters& counters,
174 CategoryRecord& categoryRecord,
175 std::string& errorMessage)
Ferran Balaguer73882172019-09-02 16:39:42 +0100176{
Jim Flynn6730fe92022-03-10 22:57:47 +0000177 ARM_PIPE_ASSERT(category);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100178
179 const std::string& categoryName = category->m_Name;
Jim Flynn6730fe92022-03-10 22:57:47 +0000180 ARM_PIPE_ASSERT(!categoryName.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100181
Keith Davis33ed2212020-03-30 10:43:41 +0100182 // Remove any duplicate counters
183 std::vector<uint16_t> categoryCounters;
184 for (size_t counterIndex = 0; counterIndex < category->m_Counters.size(); ++counterIndex)
185 {
186 uint16_t counterUid = category->m_Counters.at(counterIndex);
187 auto it = counters.find(counterUid);
188 if (it == counters.end())
189 {
Jan Eilers156113c2020-09-09 19:11:16 +0100190 errorMessage = fmt::format("Counter ({}) not found in category ({})",
191 counterUid,
192 category->m_Name );
Keith Davis33ed2212020-03-30 10:43:41 +0100193 return false;
194 }
195
196 const CounterPtr& counter = it->second;
197
198 if (counterUid == counter->m_MaxCounterUid)
199 {
200 categoryCounters.emplace_back(counterUid);
201 }
202 }
203 if (categoryCounters.empty())
204 {
Jan Eilers156113c2020-09-09 19:11:16 +0100205 errorMessage = fmt::format("No valid counters found in category ({})", categoryName);
Keith Davis33ed2212020-03-30 10:43:41 +0100206 return false;
207 }
208
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100209 // Utils
Finn Williamsd44815f2020-05-01 13:25:55 +0100210 const size_t uint32_t_size = sizeof(uint32_t);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100211
212 // Convert the device name into a SWTrace namestring
213 std::vector<uint32_t> categoryNameBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100214 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(categoryName, categoryNameBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100215 {
Jan Eilers156113c2020-09-09 19:11:16 +0100216 errorMessage = fmt::format("Cannot convert the name of category ({}) to an SWTrace namestring",
217 categoryName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100218 return false;
219 }
220
Finn Williamsd44815f2020-05-01 13:25:55 +0100221 // Category record word 1:
222 // 16:31 [16] event_count: number of events belonging to this category
223 // 0:15 [16] reserved: all zeros
224 const uint32_t categoryRecordWord1 = static_cast<uint32_t>(categoryCounters.size()) << 16;
225
226 // Category record word 2:
227 // 0:31 [32] event_pointer_table_offset: offset from the beginning of the category data pool to
228 // the event_pointer_table
229 const uint32_t categoryRecordWord2 = static_cast<uint32_t>(3u * uint32_t_size);
230
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100231 // Process the event records
Finn Williamsd44815f2020-05-01 13:25:55 +0100232 const size_t counterCount = categoryCounters.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100233 std::vector<EventRecord> eventRecords(counterCount);
234 std::vector<uint32_t> eventRecordOffsets(counterCount, 0);
235 size_t eventRecordsSize = 0;
Jim Flynn75c14f42022-03-10 22:05:42 +0000236 uint32_t eventRecordsOffset = arm::pipe::numeric_cast<uint32_t>(
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100237 (eventRecords.size() + categoryNameBuffer.size()) * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100238 for (size_t counterIndex = 0, eventRecordIndex = 0, eventRecordOffsetIndex = 0;
239 counterIndex < counterCount;
240 counterIndex++, eventRecordIndex++, eventRecordOffsetIndex++)
241 {
242 uint16_t counterUid = categoryCounters.at(counterIndex);
243 auto it = counters.find(counterUid);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100244 const CounterPtr& counter = it->second;
245
246 EventRecord& eventRecord = eventRecords.at(eventRecordIndex);
247 if (!CreateEventRecord(counter, eventRecord, errorMessage))
248 {
249 return false;
250 }
251
252 // Update the total size in words of the event records
253 eventRecordsSize += eventRecord.size();
254
255 // Add the event record offset to the event pointer table offset field
256 eventRecordOffsets[eventRecordOffsetIndex] = eventRecordsOffset;
Jim Flynn75c14f42022-03-10 22:05:42 +0000257 eventRecordsOffset += arm::pipe::numeric_cast<uint32_t>(eventRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100258 }
259
260 // Category record word 3:
261 // 0:31 [32] name_offset (offset from the beginning of the category data pool to the name field)
Jim Flynn75c14f42022-03-10 22:05:42 +0000262 const uint32_t categoryRecordWord3 = arm::pipe::numeric_cast<uint32_t>(
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100263 (3u + eventRecordOffsets.size()) * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100264
265 // Calculate the size in words of the category record
Finn Williamsd44815f2020-05-01 13:25:55 +0100266 const size_t categoryRecordSize = 3u +// The size of the fixed part (device + counter_set + event_count +
267 // reserved + event_pointer_table_offset + name_offset)
268 eventRecordOffsets.size() + // The size of the variable part (
269 categoryNameBuffer.size() + // the event pointer table + the category name
270 eventRecordsSize; // including the null-terminator + the event records)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100271
272 // Allocate the necessary space for the category record
273 categoryRecord.resize(categoryRecordSize);
274
Jim Flynn0a013a72022-03-13 16:57:04 +0000275 ARM_PIPE_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100276 // Create the category record
Sadik Armagan4c998992020-02-25 12:44:44 +0000277 categoryRecord[0] = categoryRecordWord1; // event_count + reserved
278 categoryRecord[1] = categoryRecordWord2; // event_pointer_table_offset
279 categoryRecord[2] = categoryRecordWord3; // name_offset
280 auto offset = categoryRecord.begin() + 3u;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100281 std::copy(eventRecordOffsets.begin(), eventRecordOffsets.end(), offset); // event_pointer_table
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100282 offset += eventRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100283 std::copy(categoryNameBuffer.begin(), categoryNameBuffer.end(), offset); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100284 offset += categoryNameBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100285 for (const EventRecord& eventRecord : eventRecords)
286 {
287 std::copy(eventRecord.begin(), eventRecord.end(), offset); // event_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100288 offset += eventRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100289 }
Jim Flynn0a013a72022-03-13 16:57:04 +0000290 ARM_PIPE_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100291
292 return true;
293}
294
295bool SendCounterPacket::CreateDeviceRecord(const DevicePtr& device,
296 DeviceRecord& deviceRecord,
297 std::string& errorMessage)
298{
Jim Flynn6730fe92022-03-10 22:57:47 +0000299 ARM_PIPE_ASSERT(device);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100300
301 uint16_t deviceUid = device->m_Uid;
302 const std::string& deviceName = device->m_Name;
303 uint16_t deviceCores = device->m_Cores;
304
Jim Flynn6730fe92022-03-10 22:57:47 +0000305 ARM_PIPE_ASSERT(!deviceName.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100306
307 // Device record word 0:
308 // 16:31 [16] uid: the unique identifier for the device
309 // 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 +0100310 const uint32_t deviceRecordWord0 = (static_cast<uint32_t>(deviceUid) << 16) |
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100311 (static_cast<uint32_t>(deviceCores));
312
313 // Device record word 1:
314 // 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 +0100315 const uint32_t deviceRecordWord1 = 8u; // The offset is always eight here, as the name field is always
Finn Williamsd44815f2020-05-01 13:25:55 +0100316 // the first (and only) item in the pool and there are two device words
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100317
318 // Convert the device name into a SWTrace string
319 std::vector<uint32_t> deviceNameBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100320 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(deviceName, deviceNameBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100321 {
Jan Eilers156113c2020-09-09 19:11:16 +0100322 errorMessage = fmt::format("Cannot convert the name of device {} ({}) to an SWTrace string",
323 deviceUid,
324 deviceName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100325 return false;
326 }
327
328 // Calculate the size in words of the device record
Finn Williamsd44815f2020-05-01 13:25:55 +0100329 const size_t deviceRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100330 deviceNameBuffer.size(); // The size of the variable part (the device name including
331 // the null-terminator)
332
333 // Allocate the necessary space for the device record
334 deviceRecord.resize(deviceRecordSize);
335
336 // Create the device record
337 deviceRecord[0] = deviceRecordWord0; // uid + core
338 deviceRecord[1] = deviceRecordWord1; // name_offset
339 auto offset = deviceRecord.begin() + 2u;
340 std::copy(deviceNameBuffer.begin(), deviceNameBuffer.end(), offset); // name
341
342 return true;
343}
344
345bool SendCounterPacket::CreateCounterSetRecord(const CounterSetPtr& counterSet,
346 CounterSetRecord& counterSetRecord,
347 std::string& errorMessage)
348{
Jim Flynn6730fe92022-03-10 22:57:47 +0000349 ARM_PIPE_ASSERT(counterSet);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100350
351 uint16_t counterSetUid = counterSet->m_Uid;
352 const std::string& counterSetName = counterSet->m_Name;
353 uint16_t counterSetCount = counterSet->m_Count;
354
Jim Flynn6730fe92022-03-10 22:57:47 +0000355 ARM_PIPE_ASSERT(!counterSetName.empty());
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100356
357 // Counter set record word 0:
358 // 16:31 [16] uid: the unique identifier for the counter_set
359 // 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 +0100360 const uint32_t counterSetRecordWord0 = (static_cast<uint32_t>(counterSetUid) << 16) |
361 (static_cast<uint32_t>(counterSetCount));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100362
363 // Counter set record word 1:
364 // 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 +0100365 const uint32_t counterSetRecordWord1 = 8u; // The offset is always eight here, as the name field is always
Finn Williamsd44815f2020-05-01 13:25:55 +0100366 // the first (and only) item in the pool after the two counter set words
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100367
368 // Convert the device name into a SWTrace namestring
369 std::vector<uint32_t> counterSetNameBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100370 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(counterSet->m_Name, counterSetNameBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100371 {
Jan Eilers156113c2020-09-09 19:11:16 +0100372 errorMessage = fmt::format("Cannot convert the name of counter set {} ({}) to an SWTrace namestring",
373 counterSetUid,
374 counterSetName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100375 return false;
376 }
377
378 // Calculate the size in words of the counter set record
Finn Williamsd44815f2020-05-01 13:25:55 +0100379 const size_t counterSetRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
380 counterSetNameBuffer.size(); // The size of the variable part (the counter set
381 // name including the null-terminator)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100382
383 // Allocate the space for the counter set record
384 counterSetRecord.resize(counterSetRecordSize);
385
386 // Create the counter set record
387 counterSetRecord[0] = counterSetRecordWord0; // uid + core
388 counterSetRecord[1] = counterSetRecordWord1; // name_offset
389 auto offset = counterSetRecord.begin() + 2u;
390 std::copy(counterSetNameBuffer.begin(), counterSetNameBuffer.end(), offset); // name
391
392 return true;
393}
394
395bool SendCounterPacket::CreateEventRecord(const CounterPtr& counter,
396 EventRecord& eventRecord,
397 std::string& errorMessage)
398{
Jim Flynn6730fe92022-03-10 22:57:47 +0000399 ARM_PIPE_ASSERT(counter);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100400
401 uint16_t counterUid = counter->m_Uid;
402 uint16_t maxCounterUid = counter->m_MaxCounterUid;
403 uint16_t deviceUid = counter->m_DeviceUid;
404 uint16_t counterSetUid = counter->m_CounterSetUid;
405 uint16_t counterClass = counter->m_Class;
406 uint16_t counterInterpolation = counter->m_Interpolation;
407 double counterMultiplier = counter->m_Multiplier;
408 const std::string& counterName = counter->m_Name;
409 const std::string& counterDescription = counter->m_Description;
410 const std::string& counterUnits = counter->m_Units;
411
Jim Flynn6730fe92022-03-10 22:57:47 +0000412 ARM_PIPE_ASSERT(counterClass == 0 || counterClass == 1);
413 ARM_PIPE_ASSERT(counterInterpolation == 0 || counterInterpolation == 1);
414 ARM_PIPE_ASSERT(counterMultiplier);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100415
416 // Utils
Finn Williamsd44815f2020-05-01 13:25:55 +0100417 const size_t uint32_t_size = sizeof(uint32_t);
418 // eventRecordBlockSize is the size of the fixed part
419 // (counter_uid + max_counter_uid + device +
420 // counter_set + class + interpolation +
421 // multiplier + name_offset + description_offset +
422 // units_offset)
423 const size_t eventRecordBlockSize = 8u;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100424
425 // Event record word 0:
426 // 16:31 [16] max_counter_uid: if the device this event is associated with has more than one core and there
427 // is one of these counters per core this value will be set to
428 // (counter_uid + cores (from device_record)) - 1.
429 // If there is only a single core then this value will be the same as
430 // the counter_uid value
431 // 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 +0100432 const uint32_t eventRecordWord0 = (static_cast<uint32_t>(maxCounterUid) << 16) |
433 (static_cast<uint32_t>(counterUid));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100434
435 // Event record word 1:
436 // 16:31 [16] device: UID of the device this event is associated with. Set to zero if the event is NOT
437 // associated with a device
438 // 0:15 [16] counter_set: UID of the counter_set this event is associated with. Set to zero if the event
439 // is NOT associated with a counter_set
Finn Williamsd44815f2020-05-01 13:25:55 +0100440 const uint32_t eventRecordWord1 = (static_cast<uint32_t>(deviceUid) << 16) |
441 (static_cast<uint32_t>(counterSetUid));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100442
443 // Event record word 2:
444 // 16:31 [16] class: type describing how to treat each data point in a stream of data points
445 // 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 +0100446 const uint32_t eventRecordWord2 = (static_cast<uint32_t>(counterClass) << 16) |
447 (static_cast<uint32_t>(counterInterpolation));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100448
449 // Event record word 3-4:
450 // 0:63 [64] multiplier: internal data stream is represented as integer values, this allows scaling of
451 // those values as if they are fixed point numbers. Zero is not a valid value
452 uint32_t multiplier[2] = { 0u, 0u };
Jim Flynn6730fe92022-03-10 22:57:47 +0000453 ARM_PIPE_ASSERT(sizeof(counterMultiplier) == sizeof(multiplier));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100454 std::memcpy(multiplier, &counterMultiplier, sizeof(multiplier));
Finn Williamsd44815f2020-05-01 13:25:55 +0100455 const uint32_t eventRecordWord3 = multiplier[0];
456 const uint32_t eventRecordWord4 = multiplier[1];
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100457
458 // Event record word 5:
459 // 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 +0100460 const uint32_t eventRecordWord5 = static_cast<uint32_t>(eventRecordBlockSize * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100461
462 // Convert the counter name into a SWTrace string
463 std::vector<uint32_t> counterNameBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100464 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(counterName, counterNameBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100465 {
Jan Eilers156113c2020-09-09 19:11:16 +0100466 errorMessage = fmt::format("Cannot convert the name of counter {} (name: {}) to an SWTrace string",
467 counterUid,
468 counterName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100469 return false;
470 }
471
472 // Event record word 6:
473 // 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 +0100474 // The size of the name buffer in bytes
Finn Williamsd44815f2020-05-01 13:25:55 +0100475 uint32_t eventRecordWord6 =
476 static_cast<uint32_t>((counterNameBuffer.size() + eventRecordBlockSize) * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100477
478 // Convert the counter description into a SWTrace string
479 std::vector<uint32_t> counterDescriptionBuffer;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100480 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(counterDescription, counterDescriptionBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100481 {
Jan Eilers156113c2020-09-09 19:11:16 +0100482 errorMessage = fmt::format("Cannot convert the description of counter {} (description: {}) "
483 "to an SWTrace string",
484 counterUid,
485 counterName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100486 return false;
487 }
488
489 // Event record word 7:
490 // 0:31 [32] units_offset: (optional) offset from the beginning of the event record pool to the units field.
491 // An offset value of zero indicates this field is not provided
492 bool includeUnits = !counterUnits.empty();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100493 // The size of the description buffer in bytes
Finn Williamsd44815f2020-05-01 13:25:55 +0100494 const uint32_t eventRecordWord7 = includeUnits ?
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100495 eventRecordWord6 +
Jim Flynn75c14f42022-03-10 22:05:42 +0000496 arm::pipe::numeric_cast<uint32_t>(counterDescriptionBuffer.size()
Finn Williamsd44815f2020-05-01 13:25:55 +0100497 * uint32_t_size) :
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100498 0;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100499
500 // Convert the counter units into a SWTrace namestring (optional)
501 std::vector<uint32_t> counterUnitsBuffer;
502 if (includeUnits)
503 {
504 // Convert the counter units into a SWTrace namestring
Jim Flynnbbfe6032020-07-20 16:57:44 +0100505 if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(counterUnits, counterUnitsBuffer))
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100506 {
Jan Eilers156113c2020-09-09 19:11:16 +0100507 errorMessage = fmt::format("Cannot convert the units of counter {} (units: {}) to an SWTrace string",
508 counterUid,
509 counterName);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100510 return false;
511 }
512 }
513
514 // Calculate the size in words of the event record
Finn Williamsd44815f2020-05-01 13:25:55 +0100515 const size_t eventRecordSize = eventRecordBlockSize +
516 counterNameBuffer.size() + // The size of the variable part (the counter name,
517 counterDescriptionBuffer.size() + // description and units
518 counterUnitsBuffer.size(); // including the null-terminator)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100519
520 // Allocate the space for the event record
521 eventRecord.resize(eventRecordSize);
522
Jim Flynn0a013a72022-03-13 16:57:04 +0000523 ARM_PIPE_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100524 // Create the event record
525 eventRecord[0] = eventRecordWord0; // max_counter_uid + counter_uid
526 eventRecord[1] = eventRecordWord1; // device + counter_set
527 eventRecord[2] = eventRecordWord2; // class + interpolation
528 eventRecord[3] = eventRecordWord3; // multiplier
529 eventRecord[4] = eventRecordWord4; // multiplier
530 eventRecord[5] = eventRecordWord5; // name_offset
531 eventRecord[6] = eventRecordWord6; // description_offset
532 eventRecord[7] = eventRecordWord7; // units_offset
533 auto offset = eventRecord.begin() + 8u;
534 std::copy(counterNameBuffer.begin(), counterNameBuffer.end(), offset); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100535 offset += counterNameBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100536 std::copy(counterDescriptionBuffer.begin(), counterDescriptionBuffer.end(), offset); // description
537 if (includeUnits)
538 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100539 offset += counterDescriptionBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100540 std::copy(counterUnitsBuffer.begin(), counterUnitsBuffer.end(), offset); // units
541 }
Jim Flynn0a013a72022-03-13 16:57:04 +0000542 ARM_PIPE_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100543
544 return true;
545}
546
547void SendCounterPacket::SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory)
548{
549 // Get the amount of data that needs to be put into the packet
Finn Williams985fecf2020-04-30 11:06:43 +0100550 const uint16_t categoryCount = counterDirectory.GetCategoryCount();
551 const uint16_t deviceCount = counterDirectory.GetDeviceCount();
552 const uint16_t counterSetCount = counterDirectory.GetCounterSetCount();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100553
554 // Utils
Finn Williams985fecf2020-04-30 11:06:43 +0100555 const size_t uint32_t_size = sizeof(uint32_t);
556 const size_t packetHeaderSize = 2u;
557 const size_t bodyHeaderSize = 6u;
558 const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * uint32_t_size;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100559
560 // Initialize the offset for the pointer tables
561 uint32_t pointerTableOffset = 0;
562
563 // --------------
564 // Device records
565 // --------------
566
567 // Process device records
568 std::vector<DeviceRecord> deviceRecords(deviceCount);
569 const Devices& devices = counterDirectory.GetDevices();
570 std::vector<uint32_t> deviceRecordOffsets(deviceCount, 0); // device_records_pointer_table
571 size_t deviceRecordsSize = 0;
572 size_t deviceIndex = 0;
573 size_t deviceRecordOffsetIndex = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +0100574
Jim Flynn75c14f42022-03-10 22:05:42 +0000575 pointerTableOffset = arm::pipe::numeric_cast<uint32_t>(deviceCount * uint32_t_size +
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100576 counterSetCount * uint32_t_size +
577 categoryCount * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100578 for (auto it = devices.begin(); it != devices.end(); it++)
579 {
580 const DevicePtr& device = it->second;
581 DeviceRecord& deviceRecord = deviceRecords.at(deviceIndex);
582
583 std::string errorMessage;
584 if (!CreateDeviceRecord(device, deviceRecord, errorMessage))
585 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000586 CancelOperationAndThrow<arm::pipe::ProfilingException>(errorMessage);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100587 }
588
589 // Update the total size in words of the device records
590 deviceRecordsSize += deviceRecord.size();
591
592 // Add the device record offset to the device records pointer table offset field
593 deviceRecordOffsets[deviceRecordOffsetIndex] = pointerTableOffset;
Jim Flynn75c14f42022-03-10 22:05:42 +0000594 pointerTableOffset += arm::pipe::numeric_cast<uint32_t>(deviceRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100595
596 deviceIndex++;
597 deviceRecordOffsetIndex++;
598 }
599
600 // -------------------
601 // Counter set records
602 // -------------------
603
604 // Process counter set records
605 std::vector<CounterSetRecord> counterSetRecords(counterSetCount);
606 const CounterSets& counterSets = counterDirectory.GetCounterSets();
607 std::vector<uint32_t> counterSetRecordOffsets(counterSetCount, 0); // counter_set_records_pointer_table
608 size_t counterSetRecordsSize = 0;
609 size_t counterSetIndex = 0;
610 size_t counterSetRecordOffsetIndex = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +0100611
Jim Flynn75c14f42022-03-10 22:05:42 +0000612 pointerTableOffset -= arm::pipe::numeric_cast<uint32_t>(deviceCount * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100613 for (auto it = counterSets.begin(); it != counterSets.end(); it++)
614 {
615 const CounterSetPtr& counterSet = it->second;
616 CounterSetRecord& counterSetRecord = counterSetRecords.at(counterSetIndex);
617
618 std::string errorMessage;
619 if (!CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage))
620 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000621 CancelOperationAndThrow<arm::pipe::ProfilingException>(errorMessage);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100622 }
623
624 // Update the total size in words of the counter set records
625 counterSetRecordsSize += counterSetRecord.size();
626
627 // Add the counter set record offset to the counter set records pointer table offset field
628 counterSetRecordOffsets[counterSetRecordOffsetIndex] = pointerTableOffset;
Jim Flynn75c14f42022-03-10 22:05:42 +0000629 pointerTableOffset += arm::pipe::numeric_cast<uint32_t>(counterSetRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100630
631 counterSetIndex++;
632 counterSetRecordOffsetIndex++;
633 }
634
635 // ----------------
636 // Category records
637 // ----------------
638
639 // Process category records
640 std::vector<CategoryRecord> categoryRecords(categoryCount);
641 const Categories& categories = counterDirectory.GetCategories();
642 std::vector<uint32_t> categoryRecordOffsets(categoryCount, 0); // category_records_pointer_table
643 size_t categoryRecordsSize = 0;
644 size_t categoryIndex = 0;
645 size_t categoryRecordOffsetIndex = 0;
Finn Williamsd44815f2020-05-01 13:25:55 +0100646
Jim Flynn75c14f42022-03-10 22:05:42 +0000647 pointerTableOffset -= arm::pipe::numeric_cast<uint32_t>(counterSetCount * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100648 for (auto it = categories.begin(); it != categories.end(); it++)
649 {
650 const CategoryPtr& category = *it;
651 CategoryRecord& categoryRecord = categoryRecords.at(categoryIndex);
652
653 std::string errorMessage;
654 if (!CreateCategoryRecord(category, counterDirectory.GetCounters(), categoryRecord, errorMessage))
655 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000656 CancelOperationAndThrow<arm::pipe::ProfilingException>(errorMessage);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100657 }
658
659 // Update the total size in words of the category records
660 categoryRecordsSize += categoryRecord.size();
661
662 // Add the category record offset to the category records pointer table offset field
663 categoryRecordOffsets[categoryRecordOffsetIndex] = pointerTableOffset;
Jim Flynn75c14f42022-03-10 22:05:42 +0000664 pointerTableOffset += arm::pipe::numeric_cast<uint32_t>(categoryRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100665
666 categoryIndex++;
667 categoryRecordOffsetIndex++;
668 }
669
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100670 // Calculate the length in words of the counter directory packet's data (excludes the packet header size)
Finn Williams985fecf2020-04-30 11:06:43 +0100671 const size_t counterDirectoryPacketDataLength =
672 bodyHeaderSize + // The size of the body header
673 deviceRecordOffsets.size() + // The size of the device records pointer table
674 counterSetRecordOffsets.size() + // The size of counter set pointer table
675 categoryRecordOffsets.size() + // The size of category records pointer table
676 deviceRecordsSize + // The total size of the device records
677 counterSetRecordsSize + // The total size of the counter set records
678 categoryRecordsSize; // The total size of the category records
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100679
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100680 // 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 +0100681 const size_t counterDirectoryPacketSize = packetHeaderSize + // The size of the packet header
682 counterDirectoryPacketDataLength; // The data length
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100683
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100684 // Allocate the necessary space for the counter directory packet
685 std::vector<uint32_t> counterDirectoryPacket(counterDirectoryPacketSize, 0);
686
687 // -------------
688 // Packet header
689 // -------------
690
691 // Packet header word 0:
692 // 26:31 [6] packet_family: control Packet Family
693 // 16:25 [10] packet_id: packet identifier
694 // 8:15 [8] reserved: all zeros
695 // 0:7 [8] reserved: all zeros
696 uint32_t packetFamily = 0;
697 uint32_t packetId = 2;
698 uint32_t packetHeaderWord0 = ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16);
699
700 // Packet header word 1:
701 // 0:31 [32] data_length: length of data, in bytes
Jim Flynn75c14f42022-03-10 22:05:42 +0000702 uint32_t packetHeaderWord1 = arm::pipe::numeric_cast<uint32_t>(
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100703 counterDirectoryPacketDataLength * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100704
705 // Create the packet header
706 uint32_t packetHeader[2]
707 {
708 packetHeaderWord0, // packet_family + packet_id + reserved + reserved
709 packetHeaderWord1 // data_length
710 };
711
712 // -----------
713 // Body header
714 // -----------
715
716 // Body header word 0:
717 // 16:31 [16] device_records_count: number of entries in the device_records_pointer_table
718 // 0:15 [16] reserved: all zeros
Finn Williams985fecf2020-04-30 11:06:43 +0100719 const uint32_t bodyHeaderWord0 = static_cast<uint32_t>(deviceCount) << 16;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100720
721 // Body header word 1:
722 // 0:31 [32] device_records_pointer_table_offset: offset to the device_records_pointer_table
Finn Williams985fecf2020-04-30 11:06:43 +0100723 const uint32_t bodyHeaderWord1 = bodyHeaderSizeBytes; // The offset is always the bodyHeaderSize,
724 // as the device record pointer table field
725 // is always the first item in the pool
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100726
727 // Body header word 2:
728 // 16:31 [16] counter_set_count: number of entries in the counter_set_pointer_table
729 // 0:15 [16] reserved: all zeros
Finn Williams985fecf2020-04-30 11:06:43 +0100730 const uint32_t bodyHeaderWord2 = static_cast<uint32_t>(counterSetCount) << 16;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100731
732 // Body header word 3:
733 // 0:31 [32] counter_set_pointer_table_offset: offset to the counter_set_pointer_table
Jim Flynn75c14f42022-03-10 22:05:42 +0000734 const uint32_t bodyHeaderWord3 = arm::pipe::numeric_cast<uint32_t>(deviceRecordOffsets.size() *
Matthew Sloyan371b70e2020-09-11 10:14:57 +0100735 uint32_t_size + // The size of the
736 bodyHeaderSizeBytes); // device records pointer table
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100737
738 // Body header word 4:
739 // 16:31 [16] categories_count: number of entries in the categories_pointer_table
740 // 0:15 [16] reserved: all zeros
Finn Williams985fecf2020-04-30 11:06:43 +0100741 const uint32_t bodyHeaderWord4 = static_cast<uint32_t>(categoryCount) << 16;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100742
743 // Body header word 3:
744 // 0:31 [32] categories_pointer_table_offset: offset to the categories_pointer_table
Finn Williams985fecf2020-04-30 11:06:43 +0100745 const uint32_t bodyHeaderWord5 =
Jim Flynn75c14f42022-03-10 22:05:42 +0000746 arm::pipe::numeric_cast<uint32_t>(
Finn Williams985fecf2020-04-30 11:06:43 +0100747 deviceRecordOffsets.size() * uint32_t_size + // The size of the device records
748 counterSetRecordOffsets.size() * uint32_t_size // pointer table, plus the size of
749 + bodyHeaderSizeBytes); // the counter set pointer table
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100750
751 // Create the body header
Finn Williams985fecf2020-04-30 11:06:43 +0100752 const uint32_t bodyHeader[bodyHeaderSize]
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100753 {
754 bodyHeaderWord0, // device_records_count + reserved
755 bodyHeaderWord1, // device_records_pointer_table_offset
756 bodyHeaderWord2, // counter_set_count + reserved
757 bodyHeaderWord3, // counter_set_pointer_table_offset
758 bodyHeaderWord4, // categories_count + reserved
759 bodyHeaderWord5 // categories_pointer_table_offset
760 };
761
Jim Flynn0a013a72022-03-13 16:57:04 +0000762 ARM_PIPE_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100763 // Create the counter directory packet
764 auto counterDirectoryPacketOffset = counterDirectoryPacket.begin();
765 // packet_header
766 std::copy(packetHeader, packetHeader + packetHeaderSize, counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100767 counterDirectoryPacketOffset += packetHeaderSize;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100768 // body_header
769 std::copy(bodyHeader, bodyHeader + bodyHeaderSize, counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100770 counterDirectoryPacketOffset += bodyHeaderSize;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100771 // device_records_pointer_table
772 std::copy(deviceRecordOffsets.begin(), deviceRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100773 counterDirectoryPacketOffset += deviceRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100774 // counter_set_pointer_table
775 std::copy(counterSetRecordOffsets.begin(), counterSetRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100776 counterDirectoryPacketOffset += counterSetRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100777 // category_pointer_table
778 std::copy(categoryRecordOffsets.begin(), categoryRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100779 counterDirectoryPacketOffset += categoryRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100780 // device_records
781 for (const DeviceRecord& deviceRecord : deviceRecords)
782 {
783 std::copy(deviceRecord.begin(), deviceRecord.end(), counterDirectoryPacketOffset); // device_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100784 counterDirectoryPacketOffset += deviceRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100785 }
786 // counter_set_records
787 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
788 {
789 std::copy(counterSetRecord.begin(), counterSetRecord.end(), counterDirectoryPacketOffset); // counter_set_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100790 counterDirectoryPacketOffset += counterSetRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100791 }
792 // category_records
793 for (const CategoryRecord& categoryRecord : categoryRecords)
794 {
795 std::copy(categoryRecord.begin(), categoryRecord.end(), counterDirectoryPacketOffset); // category_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100796 counterDirectoryPacketOffset += categoryRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100797 }
Jim Flynn0a013a72022-03-13 16:57:04 +0000798 ARM_PIPE_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100799
800 // Calculate the total size in bytes of the counter directory packet
Jim Flynn75c14f42022-03-10 22:05:42 +0000801 uint32_t totalSize = arm::pipe::numeric_cast<uint32_t>(counterDirectoryPacketSize * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100802
803 // Reserve space in the buffer for the packet
804 uint32_t reserved = 0;
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000805 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100806
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100807 if (writeBuffer == nullptr || reserved < totalSize)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100808 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000809 CancelOperationAndThrow<arm::pipe::BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100810 writeBuffer,
Jan Eilers156113c2020-09-09 19:11:16 +0100811 fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100812 }
813
814 // Offset for writing to the buffer
815 uint32_t offset = 0;
816
817 // Write the counter directory packet to the buffer
818 for (uint32_t counterDirectoryPacketWord : counterDirectoryPacket)
819 {
820 WriteUint32(writeBuffer, offset, counterDirectoryPacketWord);
Jim Flynn75c14f42022-03-10 22:05:42 +0000821 offset += arm::pipe::numeric_cast<uint32_t>(uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100822 }
823
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100824 m_BufferManager.Commit(writeBuffer, totalSize);
Ferran Balaguer73882172019-09-02 16:39:42 +0100825}
826
Francis Murtagh3a161982019-09-04 15:25:02 +0100827void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
Ferran Balaguer73882172019-09-02 16:39:42 +0100828{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100829 uint32_t uint16_t_size = sizeof(uint16_t);
830 uint32_t uint32_t_size = sizeof(uint32_t);
831 uint32_t uint64_t_size = sizeof(uint64_t);
832
Jim Flynnfc365622019-12-04 10:07:20 +0000833 uint32_t packetFamily = 3;
Francis Murtagh3a161982019-09-04 15:25:02 +0100834 uint32_t packetClass = 0;
835 uint32_t packetType = 0;
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100836 uint32_t headerSize = 2 * uint32_t_size;
Jim Flynn75c14f42022-03-10 22:05:42 +0000837 uint32_t bodySize = uint64_t_size + arm::pipe::numeric_cast<uint32_t>(
838 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 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000847 CancelOperationAndThrow<arm::pipe::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;
Jim Flynn75c14f42022-03-10 22:05:42 +0000887 uint32_t bodySize = uint32_t_size + arm::pipe::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 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +0000896 CancelOperationAndThrow<arm::pipe::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
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000921} // namespace pipe
Ferran Balaguer73882172019-09-02 16:39:42 +0100922
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000923} // namespace arm