blob: 942caec295e6e82895c4771d3583b9f8d978e75e [file] [log] [blame]
Ferran Balaguer73882172019-09-02 16:39:42 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "SendCounterPacket.hpp"
7#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>
Ferran Balaguer73882172019-09-02 16:39:42 +010012
13#include <boost/format.hpp>
14#include <boost/numeric/conversion/cast.hpp>
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010015#include <boost/core/ignore_unused.hpp>
Ferran Balaguer73882172019-09-02 16:39:42 +010016
Matteo Martincigh149528e2019-09-05 12:02:04 +010017#include <cstring>
Ferran Balaguer73882172019-09-02 16:39:42 +010018
19namespace armnn
20{
21
22namespace profiling
23{
24
25using boost::numeric_cast;
26
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010027const unsigned int SendCounterPacket::PIPE_MAGIC;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010028
Ferran Balaguer73882172019-09-02 16:39:42 +010029void SendCounterPacket::SendStreamMetaDataPacket()
30{
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010031 std::string info(GetSoftwareInfo());
32 std::string hardwareVersion(GetHardwareVersion());
33 std::string softwareVersion(GetSoftwareVersion());
34 std::string processName = GetProcessName().substr(0, 60);
35
36 uint32_t infoSize = numeric_cast<uint32_t>(info.size()) > 0 ? numeric_cast<uint32_t>(info.size()) + 1 : 0;
37 uint32_t hardwareVersionSize = numeric_cast<uint32_t>(hardwareVersion.size()) > 0 ?
38 numeric_cast<uint32_t>(hardwareVersion.size()) + 1 : 0;
39 uint32_t softwareVersionSize = numeric_cast<uint32_t>(softwareVersion.size()) > 0 ?
40 numeric_cast<uint32_t>(softwareVersion.size()) + 1 : 0;
41 uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) > 0 ?
42 numeric_cast<uint32_t>(processName.size()) + 1 : 0;
43
44 uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
45
46 uint32_t headerSize = 2 * sizeUint32;
47 uint32_t bodySize = 10 * sizeUint32;
48 uint32_t packetVersionCountSize = sizeUint32;
49
50 // Supported Packets
51 // Stream metadata packet (packet family=0; packet id=0)
52 // Connection Acknowledged packet (packet family=0, packet id=1)
53 // Counter Directory packet (packet family=0; packet id=2)
54 // Request Counter Directory packet (packet family=0, packet id=3)
55 // Periodic Counter Selection packet (packet family=0, packet id=4)
Ferran Balaguer5bf1d322019-09-13 13:31:40 +010056 // Periodic Counter Capture packet (packet family=1, packet class=0, type=0)
57 uint32_t packetVersionEntries = 6;
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010058
59 uint32_t payloadSize = numeric_cast<uint32_t>(infoSize + hardwareVersionSize + softwareVersionSize +
60 processNameSize + packetVersionCountSize +
61 (packetVersionEntries * 2 * sizeUint32));
62
63 uint32_t totalSize = headerSize + bodySize + payloadSize;
64 uint32_t offset = 0;
65 uint32_t reserved = 0;
66
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000067 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010068
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010069 if (writeBuffer == nullptr || reserved < totalSize)
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010070 {
Matteo Martincigh149528e2019-09-05 12:02:04 +010071 CancelOperationAndThrow<BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010072 writeBuffer,
73 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010074 }
75
76 try
77 {
78 // Create header
79
80 WriteUint32(writeBuffer, offset, 0);
81 offset += sizeUint32;
82 WriteUint32(writeBuffer, offset, totalSize - headerSize);
83
84 // Packet body
85
86 offset += sizeUint32;
87 WriteUint32(writeBuffer, offset, PIPE_MAGIC); // pipe_magic
88 offset += sizeUint32;
89 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0)); // stream_metadata_version
90 offset += sizeUint32;
91 WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length
92 offset += sizeUint32;
Rob Hughesbdee4262020-01-07 17:05:24 +000093 int pid = armnnUtils::Processes::GetCurrentId();
94 WriteUint32(writeBuffer, offset, numeric_cast<uint32_t>(pid)); // pid
Ferran Balaguer47d0fe92019-09-04 16:47:34 +010095 offset += sizeUint32;
96 uint32_t poolOffset = bodySize;
97 WriteUint32(writeBuffer, offset, infoSize ? poolOffset : 0); // offset_info
98 offset += sizeUint32;
99 poolOffset += infoSize;
100 WriteUint32(writeBuffer, offset, hardwareVersionSize ? poolOffset : 0); // offset_hw_version
101 offset += sizeUint32;
102 poolOffset += hardwareVersionSize;
103 WriteUint32(writeBuffer, offset, softwareVersionSize ? poolOffset : 0); // offset_sw_version
104 offset += sizeUint32;
105 poolOffset += softwareVersionSize;
106 WriteUint32(writeBuffer, offset, processNameSize ? poolOffset : 0); // offset_process_name
107 offset += sizeUint32;
108 poolOffset += processNameSize;
109 WriteUint32(writeBuffer, offset, packetVersionEntries ? poolOffset : 0); // offset_packet_version_table
110 offset += sizeUint32;
111 WriteUint32(writeBuffer, offset, 0); // reserved
112 offset += sizeUint32;
113
114 // Pool
115
Matteo Martincigh149528e2019-09-05 12:02:04 +0100116 if (infoSize)
117 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100118 memcpy(&writeBuffer->GetWritableData()[offset], info.c_str(), infoSize);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100119 offset += infoSize;
120 }
121
Matteo Martincigh149528e2019-09-05 12:02:04 +0100122 if (hardwareVersionSize)
123 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100124 memcpy(&writeBuffer->GetWritableData()[offset], hardwareVersion.c_str(), hardwareVersionSize);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100125 offset += hardwareVersionSize;
126 }
127
Matteo Martincigh149528e2019-09-05 12:02:04 +0100128 if (softwareVersionSize)
129 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100130 memcpy(&writeBuffer->GetWritableData()[offset], softwareVersion.c_str(), softwareVersionSize);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100131 offset += softwareVersionSize;
132 }
133
Matteo Martincigh149528e2019-09-05 12:02:04 +0100134 if (processNameSize)
135 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100136 memcpy(&writeBuffer->GetWritableData()[offset], processName.c_str(), processNameSize);
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100137 offset += processNameSize;
138 }
139
Matteo Martincigh149528e2019-09-05 12:02:04 +0100140 if (packetVersionEntries)
141 {
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100142 // Packet Version Count
143 WriteUint32(writeBuffer, offset, packetVersionEntries << 16);
144
145 // Packet Version Entries
146 uint32_t packetFamily = 0;
147 uint32_t packetId = 0;
148
149 offset += sizeUint32;
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100150 for (uint32_t i = 0; i < packetVersionEntries - 1; ++i)
151 {
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100152 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId++ & 0x3FF) << 16));
153 offset += sizeUint32;
154 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
155 offset += sizeUint32;
156 }
Ferran Balaguer5bf1d322019-09-13 13:31:40 +0100157
158 packetFamily = 1;
159 packetId = 0;
160
161 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
162 offset += sizeUint32;
163 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100164 }
165 }
166 catch(...)
167 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100168 CancelOperationAndThrow<RuntimeException>(writeBuffer, "Error processing packet.");
Ferran Balaguer47d0fe92019-09-04 16:47:34 +0100169 }
170
Sadik Armagan3896b472020-02-10 12:24:15 +0000171 m_BufferManager.Commit(writeBuffer, totalSize, false);
Ferran Balaguer73882172019-09-02 16:39:42 +0100172}
173
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100174bool SendCounterPacket::CreateCategoryRecord(const CategoryPtr& category,
175 const Counters& counters,
176 CategoryRecord& categoryRecord,
177 std::string& errorMessage)
Ferran Balaguer73882172019-09-02 16:39:42 +0100178{
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100179 using namespace boost::numeric;
180
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100181 BOOST_ASSERT(category);
182
183 const std::string& categoryName = category->m_Name;
184 const std::vector<uint16_t> categoryCounters = category->m_Counters;
185 uint16_t deviceUid = category->m_DeviceUid;
186 uint16_t counterSetUid = category->m_CounterSetUid;
187
188 BOOST_ASSERT(!categoryName.empty());
189
190 // Utils
191 size_t uint32_t_size = sizeof(uint32_t);
192
193 // Category record word 0:
194 // 16:31 [16] device: the uid of a device element which identifies some hardware device that
195 // the category belongs to
196 // 0:15 [16] counter_set: the uid of a counter_set the category is associated with
197 uint32_t categoryRecordWord0 = (static_cast<uint32_t>(deviceUid) << 16) |
198 (static_cast<uint32_t>(counterSetUid));
199
200 // Category record word 1:
201 // 16:31 [16] event_count: number of events belonging to this category
202 // 0:15 [16] reserved: all zeros
203 uint32_t categoryRecordWord1 = static_cast<uint32_t>(categoryCounters.size()) << 16;
204
205 // Category record word 2:
206 // 0:31 [32] event_pointer_table_offset: offset from the beginning of the category data pool to
207 // the event_pointer_table
208 uint32_t categoryRecordWord2 = 0; // The offset is always zero here, as the event pointer table field is always
209 // the first item in the pool
210
211 // Convert the device name into a SWTrace namestring
212 std::vector<uint32_t> categoryNameBuffer;
213 if (!StringToSwTraceString<SwTraceNameCharPolicy>(categoryName, categoryNameBuffer))
214 {
215 errorMessage = boost::str(boost::format("Cannot convert the name of category \"%1%\" to an SWTrace namestring")
216 % categoryName);
217 return false;
218 }
219
220 // Process the event records
221 size_t counterCount = categoryCounters.size();
222 std::vector<EventRecord> eventRecords(counterCount);
223 std::vector<uint32_t> eventRecordOffsets(counterCount, 0);
224 size_t eventRecordsSize = 0;
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100225 uint32_t eventRecordsOffset =
226 numeric_cast<uint32_t>((eventRecords.size() + categoryNameBuffer.size()) * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100227 for (size_t counterIndex = 0, eventRecordIndex = 0, eventRecordOffsetIndex = 0;
228 counterIndex < counterCount;
229 counterIndex++, eventRecordIndex++, eventRecordOffsetIndex++)
230 {
231 uint16_t counterUid = categoryCounters.at(counterIndex);
232 auto it = counters.find(counterUid);
233 BOOST_ASSERT(it != counters.end());
234 const CounterPtr& counter = it->second;
235
236 EventRecord& eventRecord = eventRecords.at(eventRecordIndex);
237 if (!CreateEventRecord(counter, eventRecord, errorMessage))
238 {
239 return false;
240 }
241
242 // Update the total size in words of the event records
243 eventRecordsSize += eventRecord.size();
244
245 // Add the event record offset to the event pointer table offset field
246 eventRecordOffsets[eventRecordOffsetIndex] = eventRecordsOffset;
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100247 eventRecordsOffset += numeric_cast<uint32_t>(eventRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100248 }
249
250 // Category record word 3:
251 // 0:31 [32] name_offset (offset from the beginning of the category data pool to the name field)
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100252 uint32_t categoryRecordWord3 = numeric_cast<uint32_t>(eventRecordOffsets.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100253
254 // Calculate the size in words of the category record
255 size_t categoryRecordSize = 4u + // The size of the fixed part (device + counter_set + event_count + reserved +
256 // event_pointer_table_offset + name_offset)
257 eventRecordOffsets.size() + // The size of the variable part (the event pointer table +
258 categoryNameBuffer.size() + // and the category name including the null-terminator +
259 eventRecordsSize; // the event records)
260
261 // Allocate the necessary space for the category record
262 categoryRecord.resize(categoryRecordSize);
263
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100264 ARMNN_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100265 // Create the category record
266 categoryRecord[0] = categoryRecordWord0; // device + counter_set
267 categoryRecord[1] = categoryRecordWord1; // event_count + reserved
268 categoryRecord[2] = categoryRecordWord2; // event_pointer_table_offset
269 categoryRecord[3] = categoryRecordWord3; // name_offset
270 auto offset = categoryRecord.begin() + 4u;
271 std::copy(eventRecordOffsets.begin(), eventRecordOffsets.end(), offset); // event_pointer_table
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100272 offset += eventRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100273 std::copy(categoryNameBuffer.begin(), categoryNameBuffer.end(), offset); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100274 offset += categoryNameBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100275 for (const EventRecord& eventRecord : eventRecords)
276 {
277 std::copy(eventRecord.begin(), eventRecord.end(), offset); // event_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100278 offset += eventRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100279 }
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100280 ARMNN_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100281
282 return true;
283}
284
285bool SendCounterPacket::CreateDeviceRecord(const DevicePtr& device,
286 DeviceRecord& deviceRecord,
287 std::string& errorMessage)
288{
289 BOOST_ASSERT(device);
290
291 uint16_t deviceUid = device->m_Uid;
292 const std::string& deviceName = device->m_Name;
293 uint16_t deviceCores = device->m_Cores;
294
295 BOOST_ASSERT(!deviceName.empty());
296
297 // Device record word 0:
298 // 16:31 [16] uid: the unique identifier for the device
299 // 0:15 [16] cores: the number of individual streams of counters for one or more cores of some device
300 uint32_t deviceRecordWord0 = (static_cast<uint32_t>(deviceUid) << 16) |
301 (static_cast<uint32_t>(deviceCores));
302
303 // Device record word 1:
304 // 0:31 [32] name_offset: offset from the beginning of the device record pool to the name field
305 uint32_t deviceRecordWord1 = 0; // The offset is always zero here, as the name field is always
306 // the first (and only) item in the pool
307
308 // Convert the device name into a SWTrace string
309 std::vector<uint32_t> deviceNameBuffer;
310 if (!StringToSwTraceString<SwTraceCharPolicy>(deviceName, deviceNameBuffer))
311 {
312 errorMessage = boost::str(boost::format("Cannot convert the name of device %1% (\"%2%\") to an SWTrace string")
313 % deviceUid
314 % deviceName);
315 return false;
316 }
317
318 // Calculate the size in words of the device record
319 size_t deviceRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
320 deviceNameBuffer.size(); // The size of the variable part (the device name including
321 // the null-terminator)
322
323 // Allocate the necessary space for the device record
324 deviceRecord.resize(deviceRecordSize);
325
326 // Create the device record
327 deviceRecord[0] = deviceRecordWord0; // uid + core
328 deviceRecord[1] = deviceRecordWord1; // name_offset
329 auto offset = deviceRecord.begin() + 2u;
330 std::copy(deviceNameBuffer.begin(), deviceNameBuffer.end(), offset); // name
331
332 return true;
333}
334
335bool SendCounterPacket::CreateCounterSetRecord(const CounterSetPtr& counterSet,
336 CounterSetRecord& counterSetRecord,
337 std::string& errorMessage)
338{
339 BOOST_ASSERT(counterSet);
340
341 uint16_t counterSetUid = counterSet->m_Uid;
342 const std::string& counterSetName = counterSet->m_Name;
343 uint16_t counterSetCount = counterSet->m_Count;
344
345 BOOST_ASSERT(!counterSetName.empty());
346
347 // Counter set record word 0:
348 // 16:31 [16] uid: the unique identifier for the counter_set
349 // 0:15 [16] count: the number of counters which can be active in this set at any one time
350 uint32_t counterSetRecordWord0 = (static_cast<uint32_t>(counterSetUid) << 16) |
351 (static_cast<uint32_t>(counterSetCount));
352
353 // Counter set record word 1:
354 // 0:31 [32] name_offset: offset from the beginning of the counter set pool to the name field
355 uint32_t counterSetRecordWord1 = 0; // The offset is always zero here, as the name field is always
356 // the first (and only) item in the pool
357
358 // Convert the device name into a SWTrace namestring
359 std::vector<uint32_t> counterSetNameBuffer;
360 if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterSet->m_Name, counterSetNameBuffer))
361 {
362 errorMessage = boost::str(boost::format("Cannot convert the name of counter set %1% (\"%2%\") to "
363 "an SWTrace namestring")
364 % counterSetUid
365 % counterSetName);
366 return false;
367 }
368
369 // Calculate the size in words of the counter set record
370 size_t counterSetRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
371 counterSetNameBuffer.size(); // The size of the variable part (the counter set name
372 // including the null-terminator)
373
374 // Allocate the space for the counter set record
375 counterSetRecord.resize(counterSetRecordSize);
376
377 // Create the counter set record
378 counterSetRecord[0] = counterSetRecordWord0; // uid + core
379 counterSetRecord[1] = counterSetRecordWord1; // name_offset
380 auto offset = counterSetRecord.begin() + 2u;
381 std::copy(counterSetNameBuffer.begin(), counterSetNameBuffer.end(), offset); // name
382
383 return true;
384}
385
386bool SendCounterPacket::CreateEventRecord(const CounterPtr& counter,
387 EventRecord& eventRecord,
388 std::string& errorMessage)
389{
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100390 using namespace boost::numeric;
391
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100392 BOOST_ASSERT(counter);
393
394 uint16_t counterUid = counter->m_Uid;
395 uint16_t maxCounterUid = counter->m_MaxCounterUid;
396 uint16_t deviceUid = counter->m_DeviceUid;
397 uint16_t counterSetUid = counter->m_CounterSetUid;
398 uint16_t counterClass = counter->m_Class;
399 uint16_t counterInterpolation = counter->m_Interpolation;
400 double counterMultiplier = counter->m_Multiplier;
401 const std::string& counterName = counter->m_Name;
402 const std::string& counterDescription = counter->m_Description;
403 const std::string& counterUnits = counter->m_Units;
404
405 BOOST_ASSERT(counterClass == 0 || counterClass == 1);
406 BOOST_ASSERT(counterInterpolation == 0 || counterInterpolation == 1);
407 BOOST_ASSERT(counterMultiplier);
408
409 // Utils
410 size_t uint32_t_size = sizeof(uint32_t);
411
412 // Event record word 0:
413 // 16:31 [16] max_counter_uid: if the device this event is associated with has more than one core and there
414 // is one of these counters per core this value will be set to
415 // (counter_uid + cores (from device_record)) - 1.
416 // If there is only a single core then this value will be the same as
417 // the counter_uid value
418 // 0:15 [16] count_uid: unique ID for the counter. Must be unique across all counters in all categories
419 uint32_t eventRecordWord0 = (static_cast<uint32_t>(maxCounterUid) << 16) |
420 (static_cast<uint32_t>(counterUid));
421
422 // Event record word 1:
423 // 16:31 [16] device: UID of the device this event is associated with. Set to zero if the event is NOT
424 // associated with a device
425 // 0:15 [16] counter_set: UID of the counter_set this event is associated with. Set to zero if the event
426 // is NOT associated with a counter_set
427 uint32_t eventRecordWord1 = (static_cast<uint32_t>(deviceUid) << 16) |
428 (static_cast<uint32_t>(counterSetUid));
429
430 // Event record word 2:
431 // 16:31 [16] class: type describing how to treat each data point in a stream of data points
432 // 0:15 [16] interpolation: type describing how to interpolate each data point in a stream of data points
433 uint32_t eventRecordWord2 = (static_cast<uint32_t>(counterClass) << 16) |
434 (static_cast<uint32_t>(counterInterpolation));
435
436 // Event record word 3-4:
437 // 0:63 [64] multiplier: internal data stream is represented as integer values, this allows scaling of
438 // those values as if they are fixed point numbers. Zero is not a valid value
439 uint32_t multiplier[2] = { 0u, 0u };
440 BOOST_ASSERT(sizeof(counterMultiplier) == sizeof(multiplier));
441 std::memcpy(multiplier, &counterMultiplier, sizeof(multiplier));
442 uint32_t eventRecordWord3 = multiplier[0];
443 uint32_t eventRecordWord4 = multiplier[1];
444
445 // Event record word 5:
446 // 0:31 [32] name_offset: offset from the beginning of the event record pool to the name field
447 uint32_t eventRecordWord5 = 0; // The offset is always zero here, as the name field is always
448 // the first item in the pool
449
450 // Convert the counter name into a SWTrace string
451 std::vector<uint32_t> counterNameBuffer;
452 if (!StringToSwTraceString<SwTraceCharPolicy>(counterName, counterNameBuffer))
453 {
454 errorMessage = boost::str(boost::format("Cannot convert the name of counter %1% (name: \"%2%\") "
455 "to an SWTrace string")
456 % counterUid
457 % counterName);
458 return false;
459 }
460
461 // Event record word 6:
462 // 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 +0100463 // The size of the name buffer in bytes
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100464 uint32_t eventRecordWord6 = numeric_cast<uint32_t>(counterNameBuffer.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100465
466 // Convert the counter description into a SWTrace string
467 std::vector<uint32_t> counterDescriptionBuffer;
468 if (!StringToSwTraceString<SwTraceCharPolicy>(counterDescription, counterDescriptionBuffer))
469 {
470 errorMessage = boost::str(boost::format("Cannot convert the description of counter %1% (description: \"%2%\") "
471 "to an SWTrace string")
472 % counterUid
473 % counterName);
474 return false;
475 }
476
477 // Event record word 7:
478 // 0:31 [32] units_offset: (optional) offset from the beginning of the event record pool to the units field.
479 // An offset value of zero indicates this field is not provided
480 bool includeUnits = !counterUnits.empty();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100481 // The size of the description buffer in bytes
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100482 uint32_t eventRecordWord7 = includeUnits ?
483 eventRecordWord6 +
484 numeric_cast<uint32_t>(counterDescriptionBuffer.size() * uint32_t_size) :
485 0;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100486
487 // Convert the counter units into a SWTrace namestring (optional)
488 std::vector<uint32_t> counterUnitsBuffer;
489 if (includeUnits)
490 {
491 // Convert the counter units into a SWTrace namestring
492 if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterUnits, counterUnitsBuffer))
493 {
494 errorMessage = boost::str(boost::format("Cannot convert the units of counter %1% (units: \"%2%\") "
495 "to an SWTrace string")
496 % counterUid
497 % counterName);
498 return false;
499 }
500 }
501
502 // Calculate the size in words of the event record
503 size_t eventRecordSize = 8u + // The size of the fixed part (counter_uid + max_counter_uid + device +
504 // counter_set + class + interpolation +
505 // multiplier + name_offset + description_offset +
506 // units_offset)
507 counterNameBuffer.size() + // The size of the variable part (the counter name,
508 counterDescriptionBuffer.size() + // description and units including the null-terminator)
509 counterUnitsBuffer.size();
510
511 // Allocate the space for the event record
512 eventRecord.resize(eventRecordSize);
513
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100514 ARMNN_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100515 // Create the event record
516 eventRecord[0] = eventRecordWord0; // max_counter_uid + counter_uid
517 eventRecord[1] = eventRecordWord1; // device + counter_set
518 eventRecord[2] = eventRecordWord2; // class + interpolation
519 eventRecord[3] = eventRecordWord3; // multiplier
520 eventRecord[4] = eventRecordWord4; // multiplier
521 eventRecord[5] = eventRecordWord5; // name_offset
522 eventRecord[6] = eventRecordWord6; // description_offset
523 eventRecord[7] = eventRecordWord7; // units_offset
524 auto offset = eventRecord.begin() + 8u;
525 std::copy(counterNameBuffer.begin(), counterNameBuffer.end(), offset); // name
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100526 offset += counterNameBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100527 std::copy(counterDescriptionBuffer.begin(), counterDescriptionBuffer.end(), offset); // description
528 if (includeUnits)
529 {
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100530 offset += counterDescriptionBuffer.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100531 std::copy(counterUnitsBuffer.begin(), counterUnitsBuffer.end(), offset); // units
532 }
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100533 ARMNN_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100534
535 return true;
536}
537
538void SendCounterPacket::SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory)
539{
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100540 using namespace boost::numeric;
541
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100542 // Get the amount of data that needs to be put into the packet
543 uint16_t categoryCount = counterDirectory.GetCategoryCount();
544 uint16_t deviceCount = counterDirectory.GetDeviceCount();
545 uint16_t counterSetCount = counterDirectory.GetCounterSetCount();
546
547 // Utils
548 size_t uint32_t_size = sizeof(uint32_t);
549 size_t packetHeaderSize = 2u;
550 size_t bodyHeaderSize = 6u;
551
552 // Initialize the offset for the pointer tables
553 uint32_t pointerTableOffset = 0;
554
555 // --------------
556 // Device records
557 // --------------
558
559 // Process device records
560 std::vector<DeviceRecord> deviceRecords(deviceCount);
561 const Devices& devices = counterDirectory.GetDevices();
562 std::vector<uint32_t> deviceRecordOffsets(deviceCount, 0); // device_records_pointer_table
563 size_t deviceRecordsSize = 0;
564 size_t deviceIndex = 0;
565 size_t deviceRecordOffsetIndex = 0;
566 for (auto it = devices.begin(); it != devices.end(); it++)
567 {
568 const DevicePtr& device = it->second;
569 DeviceRecord& deviceRecord = deviceRecords.at(deviceIndex);
570
571 std::string errorMessage;
572 if (!CreateDeviceRecord(device, deviceRecord, errorMessage))
573 {
574 CancelOperationAndThrow<RuntimeException>(errorMessage);
575 }
576
577 // Update the total size in words of the device records
578 deviceRecordsSize += deviceRecord.size();
579
580 // Add the device record offset to the device records pointer table offset field
581 deviceRecordOffsets[deviceRecordOffsetIndex] = pointerTableOffset;
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100582 pointerTableOffset += numeric_cast<uint32_t>(deviceRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100583
584 deviceIndex++;
585 deviceRecordOffsetIndex++;
586 }
587
588 // -------------------
589 // Counter set records
590 // -------------------
591
592 // Process counter set records
593 std::vector<CounterSetRecord> counterSetRecords(counterSetCount);
594 const CounterSets& counterSets = counterDirectory.GetCounterSets();
595 std::vector<uint32_t> counterSetRecordOffsets(counterSetCount, 0); // counter_set_records_pointer_table
596 size_t counterSetRecordsSize = 0;
597 size_t counterSetIndex = 0;
598 size_t counterSetRecordOffsetIndex = 0;
599 for (auto it = counterSets.begin(); it != counterSets.end(); it++)
600 {
601 const CounterSetPtr& counterSet = it->second;
602 CounterSetRecord& counterSetRecord = counterSetRecords.at(counterSetIndex);
603
604 std::string errorMessage;
605 if (!CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage))
606 {
607 CancelOperationAndThrow<RuntimeException>(errorMessage);
608 }
609
610 // Update the total size in words of the counter set records
611 counterSetRecordsSize += counterSetRecord.size();
612
613 // Add the counter set record offset to the counter set records pointer table offset field
614 counterSetRecordOffsets[counterSetRecordOffsetIndex] = pointerTableOffset;
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100615 pointerTableOffset += numeric_cast<uint32_t>(counterSetRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100616
617 counterSetIndex++;
618 counterSetRecordOffsetIndex++;
619 }
620
621 // ----------------
622 // Category records
623 // ----------------
624
625 // Process category records
626 std::vector<CategoryRecord> categoryRecords(categoryCount);
627 const Categories& categories = counterDirectory.GetCategories();
628 std::vector<uint32_t> categoryRecordOffsets(categoryCount, 0); // category_records_pointer_table
629 size_t categoryRecordsSize = 0;
630 size_t categoryIndex = 0;
631 size_t categoryRecordOffsetIndex = 0;
632 for (auto it = categories.begin(); it != categories.end(); it++)
633 {
634 const CategoryPtr& category = *it;
635 CategoryRecord& categoryRecord = categoryRecords.at(categoryIndex);
636
637 std::string errorMessage;
638 if (!CreateCategoryRecord(category, counterDirectory.GetCounters(), categoryRecord, errorMessage))
639 {
640 CancelOperationAndThrow<RuntimeException>(errorMessage);
641 }
642
643 // Update the total size in words of the category records
644 categoryRecordsSize += categoryRecord.size();
645
646 // Add the category record offset to the category records pointer table offset field
647 categoryRecordOffsets[categoryRecordOffsetIndex] = pointerTableOffset;
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100648 pointerTableOffset += numeric_cast<uint32_t>(categoryRecord.size() * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100649
650 categoryIndex++;
651 categoryRecordOffsetIndex++;
652 }
653
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100654
655
656 // Calculate the length in words of the counter directory packet's data (excludes the packet header size)
657 size_t counterDirectoryPacketDataLength =
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100658 bodyHeaderSize + // The size of the body header
659 deviceRecordOffsets.size() + // The size of the device records pointer table
660 counterSetRecordOffsets.size() + // The size of counter set pointer table
661 categoryRecordOffsets.size() + // The size of category records pointer table
662 deviceRecordsSize + // The total size of the device records
663 counterSetRecordsSize + // The total size of the counter set records
664 categoryRecordsSize; // The total size of the category records
665
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100666 // Calculate the size in words of the counter directory packet (the data length plus the packet header size)
667 size_t counterDirectoryPacketSize = packetHeaderSize + // The size of the packet header
668 counterDirectoryPacketDataLength; // The data length
669
670
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100671 // Allocate the necessary space for the counter directory packet
672 std::vector<uint32_t> counterDirectoryPacket(counterDirectoryPacketSize, 0);
673
674 // -------------
675 // Packet header
676 // -------------
677
678 // Packet header word 0:
679 // 26:31 [6] packet_family: control Packet Family
680 // 16:25 [10] packet_id: packet identifier
681 // 8:15 [8] reserved: all zeros
682 // 0:7 [8] reserved: all zeros
683 uint32_t packetFamily = 0;
684 uint32_t packetId = 2;
685 uint32_t packetHeaderWord0 = ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16);
686
687 // Packet header word 1:
688 // 0:31 [32] data_length: length of data, in bytes
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100689 uint32_t packetHeaderWord1 = numeric_cast<uint32_t>(counterDirectoryPacketDataLength * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100690
691 // Create the packet header
692 uint32_t packetHeader[2]
693 {
694 packetHeaderWord0, // packet_family + packet_id + reserved + reserved
695 packetHeaderWord1 // data_length
696 };
697
698 // -----------
699 // Body header
700 // -----------
701
702 // Body header word 0:
703 // 16:31 [16] device_records_count: number of entries in the device_records_pointer_table
704 // 0:15 [16] reserved: all zeros
705 uint32_t bodyHeaderWord0 = static_cast<uint32_t>(deviceCount) << 16;
706
707 // Body header word 1:
708 // 0:31 [32] device_records_pointer_table_offset: offset to the device_records_pointer_table
709 uint32_t bodyHeaderWord1 = 0; // The offset is always zero here, as the device record pointer table field is always
710 // the first item in the pool
711
712 // Body header word 2:
713 // 16:31 [16] counter_set_count: number of entries in the counter_set_pointer_table
714 // 0:15 [16] reserved: all zeros
715 uint32_t bodyHeaderWord2 = static_cast<uint32_t>(counterSetCount) << 16;
716
717 // Body header word 3:
718 // 0:31 [32] counter_set_pointer_table_offset: offset to the counter_set_pointer_table
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100719 uint32_t bodyHeaderWord3 =
720 numeric_cast<uint32_t>(deviceRecordOffsets.size() * uint32_t_size); // The size of the device records
721 // pointer table
722
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100723
724 // Body header word 4:
725 // 16:31 [16] categories_count: number of entries in the categories_pointer_table
726 // 0:15 [16] reserved: all zeros
727 uint32_t bodyHeaderWord4 = static_cast<uint32_t>(categoryCount) << 16;
728
729 // Body header word 3:
730 // 0:31 [32] categories_pointer_table_offset: offset to the categories_pointer_table
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100731 uint32_t bodyHeaderWord5 =
732 numeric_cast<uint32_t>(deviceRecordOffsets.size() * uint32_t_size + // The size of the device records
733 counterSetRecordOffsets.size() * uint32_t_size); // pointer table, plus the size of
734 // the counter set pointer table
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100735
736 // Create the body header
737 uint32_t bodyHeader[6]
738 {
739 bodyHeaderWord0, // device_records_count + reserved
740 bodyHeaderWord1, // device_records_pointer_table_offset
741 bodyHeaderWord2, // counter_set_count + reserved
742 bodyHeaderWord3, // counter_set_pointer_table_offset
743 bodyHeaderWord4, // categories_count + reserved
744 bodyHeaderWord5 // categories_pointer_table_offset
745 };
746
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100747 ARMNN_NO_CONVERSION_WARN_BEGIN
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100748 // Create the counter directory packet
749 auto counterDirectoryPacketOffset = counterDirectoryPacket.begin();
750 // packet_header
751 std::copy(packetHeader, packetHeader + packetHeaderSize, counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100752 counterDirectoryPacketOffset += packetHeaderSize;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100753 // body_header
754 std::copy(bodyHeader, bodyHeader + bodyHeaderSize, counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100755 counterDirectoryPacketOffset += bodyHeaderSize;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100756 // device_records_pointer_table
757 std::copy(deviceRecordOffsets.begin(), deviceRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100758 counterDirectoryPacketOffset += deviceRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100759 // counter_set_pointer_table
760 std::copy(counterSetRecordOffsets.begin(), counterSetRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100761 counterDirectoryPacketOffset += counterSetRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100762 // category_pointer_table
763 std::copy(categoryRecordOffsets.begin(), categoryRecordOffsets.end(), counterDirectoryPacketOffset);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100764 counterDirectoryPacketOffset += categoryRecordOffsets.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100765 // device_records
766 for (const DeviceRecord& deviceRecord : deviceRecords)
767 {
768 std::copy(deviceRecord.begin(), deviceRecord.end(), counterDirectoryPacketOffset); // device_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100769 counterDirectoryPacketOffset += deviceRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100770 }
771 // counter_set_records
772 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
773 {
774 std::copy(counterSetRecord.begin(), counterSetRecord.end(), counterDirectoryPacketOffset); // counter_set_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100775 counterDirectoryPacketOffset += counterSetRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100776 }
777 // category_records
778 for (const CategoryRecord& categoryRecord : categoryRecords)
779 {
780 std::copy(categoryRecord.begin(), categoryRecord.end(), counterDirectoryPacketOffset); // category_record
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100781 counterDirectoryPacketOffset += categoryRecord.size();
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100782 }
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100783 ARMNN_NO_CONVERSION_WARN_END
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100784
785 // Calculate the total size in bytes of the counter directory packet
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100786 uint32_t totalSize = numeric_cast<uint32_t>(counterDirectoryPacketSize * uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100787
788 // Reserve space in the buffer for the packet
789 uint32_t reserved = 0;
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000790 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100791
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100792 if (writeBuffer == nullptr || reserved < totalSize)
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100793 {
794 CancelOperationAndThrow<BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100795 writeBuffer,
796 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100797 }
798
799 // Offset for writing to the buffer
800 uint32_t offset = 0;
801
802 // Write the counter directory packet to the buffer
803 for (uint32_t counterDirectoryPacketWord : counterDirectoryPacket)
804 {
805 WriteUint32(writeBuffer, offset, counterDirectoryPacketWord);
Matteo Martincighf74ff2f2019-09-24 11:38:32 +0100806 offset += numeric_cast<uint32_t>(uint32_t_size);
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +0100807 }
808
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100809 m_BufferManager.Commit(writeBuffer, totalSize);
Ferran Balaguer73882172019-09-02 16:39:42 +0100810}
811
Francis Murtagh3a161982019-09-04 15:25:02 +0100812void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
Ferran Balaguer73882172019-09-02 16:39:42 +0100813{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100814 uint32_t uint16_t_size = sizeof(uint16_t);
815 uint32_t uint32_t_size = sizeof(uint32_t);
816 uint32_t uint64_t_size = sizeof(uint64_t);
817
Jim Flynnfc365622019-12-04 10:07:20 +0000818 uint32_t packetFamily = 3;
Francis Murtagh3a161982019-09-04 15:25:02 +0100819 uint32_t packetClass = 0;
820 uint32_t packetType = 0;
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100821 uint32_t headerSize = 2 * uint32_t_size;
822 uint32_t bodySize = uint64_t_size + numeric_cast<uint32_t>(values.size()) * (uint16_t_size + uint32_t_size);
Francis Murtagh3a161982019-09-04 15:25:02 +0100823 uint32_t totalSize = headerSize + bodySize;
824 uint32_t offset = 0;
825 uint32_t reserved = 0;
826
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000827 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Francis Murtagh3a161982019-09-04 15:25:02 +0100828
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100829 if (writeBuffer == nullptr || reserved < totalSize)
Francis Murtagh3a161982019-09-04 15:25:02 +0100830 {
Matteo Martincigh149528e2019-09-05 12:02:04 +0100831 CancelOperationAndThrow<BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100832 writeBuffer,
833 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
Francis Murtagh3a161982019-09-04 15:25:02 +0100834 }
835
836 // Create header.
837 WriteUint32(writeBuffer,
838 offset,
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100839 ((packetFamily & 0x0000003F) << 26) |
840 ((packetClass & 0x0000007F) << 19) |
841 ((packetType & 0x00000007) << 16));
842 offset += uint32_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100843 WriteUint32(writeBuffer, offset, bodySize);
844
845 // Copy captured Timestamp.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100846 offset += uint32_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100847 WriteUint64(writeBuffer, offset, timestamp);
848
849 // Copy selectedCounterIds.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100850 offset += uint64_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100851 for (const auto& pair: values)
852 {
853 WriteUint16(writeBuffer, offset, pair.first);
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100854 offset += uint16_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100855 WriteUint32(writeBuffer, offset, pair.second);
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100856 offset += uint32_t_size;
Francis Murtagh3a161982019-09-04 15:25:02 +0100857 }
858
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100859 m_BufferManager.Commit(writeBuffer, totalSize);
Ferran Balaguer73882172019-09-02 16:39:42 +0100860}
861
862void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
863 const std::vector<uint16_t>& selectedCounterIds)
864{
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100865 uint32_t uint16_t_size = sizeof(uint16_t);
866 uint32_t uint32_t_size = sizeof(uint32_t);
867
Ferran Balaguer73882172019-09-02 16:39:42 +0100868 uint32_t packetFamily = 0;
869 uint32_t packetId = 4;
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100870 uint32_t headerSize = 2 * uint32_t_size;
871 uint32_t bodySize = uint32_t_size + numeric_cast<uint32_t>(selectedCounterIds.size()) * uint16_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100872 uint32_t totalSize = headerSize + bodySize;
873 uint32_t offset = 0;
874 uint32_t reserved = 0;
875
Matteo Martincigh2ffcc412019-11-05 11:47:40 +0000876 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
Ferran Balaguer73882172019-09-02 16:39:42 +0100877
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100878 if (writeBuffer == nullptr || reserved < totalSize)
Ferran Balaguer73882172019-09-02 16:39:42 +0100879 {
Matteo Martincigh149528e2019-09-05 12:02:04 +0100880 CancelOperationAndThrow<BufferExhaustion>(
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +0100881 writeBuffer,
882 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
Ferran Balaguer73882172019-09-02 16:39:42 +0100883 }
884
885 // Create header.
886 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100887 offset += uint32_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100888 WriteUint32(writeBuffer, offset, bodySize);
889
890 // Copy capturePeriod.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100891 offset += uint32_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100892 WriteUint32(writeBuffer, offset, capturePeriod);
893
894 // Copy selectedCounterIds.
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100895 offset += uint32_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100896 for(const uint16_t& id: selectedCounterIds)
897 {
898 WriteUint16(writeBuffer, offset, id);
Matteo Martincigh8d9590e2019-10-15 09:35:29 +0100899 offset += uint16_t_size;
Ferran Balaguer73882172019-09-02 16:39:42 +0100900 }
901
Narumol Prangnawarat404b2752019-09-24 17:23:16 +0100902 m_BufferManager.Commit(writeBuffer, totalSize);
Ferran Balaguer73882172019-09-02 16:39:42 +0100903}
904
Ferran Balaguer73882172019-09-02 16:39:42 +0100905} // namespace profiling
906
Matteo Martincigh149528e2019-09-05 12:02:04 +0100907} // namespace armnn