2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "SendCounterPacket.hpp"
7 #include "EncodeVersion.hpp"
9 #include <armnn/Exceptions.hpp>
10 #include <armnn/Conversion.hpp>
11 #include <Processes.hpp>
12 #include <armnn/utility/Assert.hpp>
13 #include <common/include/Constants.hpp>
15 #include <boost/format.hpp>
16 #include <boost/numeric/conversion/cast.hpp>
26 using boost::numeric_cast;
28 void SendCounterPacket::SendStreamMetaDataPacket()
30 const std::string info(GetSoftwareInfo());
31 const std::string hardwareVersion(GetHardwareVersion());
32 const std::string softwareVersion(GetSoftwareVersion());
33 const std::string processName = GetProcessName().substr(0, 60);
35 const uint32_t infoSize = numeric_cast<uint32_t>(info.size()) + 1;
36 const uint32_t hardwareVersionSize = numeric_cast<uint32_t>(hardwareVersion.size()) + 1;
37 const uint32_t softwareVersionSize = numeric_cast<uint32_t>(softwareVersion.size()) + 1;
38 const uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) + 1;
40 const uint32_t sizeUint32 = sizeof(uint32_t);
42 const uint32_t headerSize = 2 * sizeUint32;
43 const uint32_t bodySize = 10 * sizeUint32;
44 const uint32_t packetVersionCountSize = sizeUint32;
47 // Stream metadata packet (packet family=0; packet id=0)
48 // Connection Acknowledged packet (packet family=0, packet id=1)
49 // Counter Directory packet (packet family=0; packet id=2)
50 // Request Counter Directory packet (packet family=0, packet id=3)
51 // Periodic Counter Selection packet (packet family=0, packet id=4)
52 // Periodic Counter Capture packet (packet family=1, packet class=0, type=0)
53 const uint32_t packetVersionEntries = 6;
55 const uint32_t payloadSize = numeric_cast<uint32_t>(infoSize + hardwareVersionSize + softwareVersionSize +
56 processNameSize + packetVersionCountSize +
57 (packetVersionEntries * 2 * sizeUint32));
59 const uint32_t totalSize = headerSize + bodySize + payloadSize;
61 uint32_t reserved = 0;
63 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
65 if (writeBuffer == nullptr || reserved < totalSize)
67 CancelOperationAndThrow<BufferExhaustion>(
69 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
76 WriteUint32(writeBuffer, offset, 0);
78 WriteUint32(writeBuffer, offset, totalSize - headerSize);
83 WriteUint32(writeBuffer, offset, armnnProfiling::PIPE_MAGIC); // pipe_magic
85 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0)); // stream_metadata_version
87 WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length
89 int pid = armnnUtils::Processes::GetCurrentId();
90 WriteUint32(writeBuffer, offset, numeric_cast<uint32_t>(pid)); // pid
92 uint32_t poolOffset = bodySize;
93 WriteUint32(writeBuffer, offset, poolOffset); // offset_info
95 poolOffset += infoSize;
96 WriteUint32(writeBuffer, offset, poolOffset); // offset_hw_version
98 poolOffset += hardwareVersionSize;
99 WriteUint32(writeBuffer, offset, poolOffset); // offset_sw_version
100 offset += sizeUint32;
101 poolOffset += softwareVersionSize;
102 WriteUint32(writeBuffer, offset, poolOffset); // offset_process_name
103 offset += sizeUint32;
104 poolOffset += processNameSize;
105 WriteUint32(writeBuffer, offset, poolOffset); // offset_packet_version_table
106 offset += sizeUint32;
107 WriteUint32(writeBuffer, offset, 0); // reserved
108 offset += sizeUint32;
114 memcpy(&writeBuffer->GetWritableData()[offset], info.c_str(), infoSize);
118 memcpy(&writeBuffer->GetWritableData()[offset], hardwareVersion.c_str(), hardwareVersionSize);
119 offset += hardwareVersionSize;
120 memcpy(&writeBuffer->GetWritableData()[offset], softwareVersion.c_str(), softwareVersionSize);
121 offset += softwareVersionSize;
122 memcpy(&writeBuffer->GetWritableData()[offset], processName.c_str(), processNameSize);
123 offset += processNameSize;
125 if (packetVersionEntries)
127 // Packet Version Count
128 WriteUint32(writeBuffer, offset, packetVersionEntries << 16);
130 // Packet Version Entries
131 uint32_t packetFamily = 0;
132 uint32_t packetId = 0;
134 offset += sizeUint32;
135 for (uint32_t i = 0; i < packetVersionEntries - 1; ++i)
137 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId++ & 0x3FF) << 16));
138 offset += sizeUint32;
139 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
140 offset += sizeUint32;
146 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
147 offset += sizeUint32;
148 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
153 CancelOperationAndThrow<RuntimeException>(writeBuffer, "Error processing packet.");
156 m_BufferManager.Commit(writeBuffer, totalSize, false);
159 bool SendCounterPacket::CreateCategoryRecord(const CategoryPtr& category,
160 const Counters& counters,
161 CategoryRecord& categoryRecord,
162 std::string& errorMessage)
164 using namespace boost::numeric;
166 ARMNN_ASSERT(category);
168 const std::string& categoryName = category->m_Name;
169 ARMNN_ASSERT(!categoryName.empty());
171 // Remove any duplicate counters
172 std::vector<uint16_t> categoryCounters;
173 for (size_t counterIndex = 0; counterIndex < category->m_Counters.size(); ++counterIndex)
175 uint16_t counterUid = category->m_Counters.at(counterIndex);
176 auto it = counters.find(counterUid);
177 if (it == counters.end())
179 errorMessage = boost::str(boost::format("Counter (%1%) not found in category (%2%)")
180 % counterUid % category->m_Name );
184 const CounterPtr& counter = it->second;
186 if (counterUid == counter->m_MaxCounterUid)
188 categoryCounters.emplace_back(counterUid);
191 if (categoryCounters.empty())
193 errorMessage = boost::str(boost::format("No valid counters found in category (%1%)")% categoryName);
198 const size_t uint32_t_size = sizeof(uint32_t);
200 // Convert the device name into a SWTrace namestring
201 std::vector<uint32_t> categoryNameBuffer;
202 if (!StringToSwTraceString<SwTraceNameCharPolicy>(categoryName, categoryNameBuffer))
204 errorMessage = boost::str(boost::format("Cannot convert the name of category (%1%) to an SWTrace namestring")
209 // Category record word 1:
210 // 16:31 [16] event_count: number of events belonging to this category
211 // 0:15 [16] reserved: all zeros
212 const uint32_t categoryRecordWord1 = static_cast<uint32_t>(categoryCounters.size()) << 16;
214 // Category record word 2:
215 // 0:31 [32] event_pointer_table_offset: offset from the beginning of the category data pool to
216 // the event_pointer_table
217 const uint32_t categoryRecordWord2 = static_cast<uint32_t>(3u * uint32_t_size);
219 // Process the event records
220 const size_t counterCount = categoryCounters.size();
221 std::vector<EventRecord> eventRecords(counterCount);
222 std::vector<uint32_t> eventRecordOffsets(counterCount, 0);
223 size_t eventRecordsSize = 0;
224 uint32_t eventRecordsOffset =
225 numeric_cast<uint32_t>((eventRecords.size() + categoryNameBuffer.size()) * uint32_t_size);
226 for (size_t counterIndex = 0, eventRecordIndex = 0, eventRecordOffsetIndex = 0;
227 counterIndex < counterCount;
228 counterIndex++, eventRecordIndex++, eventRecordOffsetIndex++)
230 uint16_t counterUid = categoryCounters.at(counterIndex);
231 auto it = counters.find(counterUid);
232 const CounterPtr& counter = it->second;
234 EventRecord& eventRecord = eventRecords.at(eventRecordIndex);
235 if (!CreateEventRecord(counter, eventRecord, errorMessage))
240 // Update the total size in words of the event records
241 eventRecordsSize += eventRecord.size();
243 // Add the event record offset to the event pointer table offset field
244 eventRecordOffsets[eventRecordOffsetIndex] = eventRecordsOffset;
245 eventRecordsOffset += numeric_cast<uint32_t>(eventRecord.size() * uint32_t_size);
248 // Category record word 3:
249 // 0:31 [32] name_offset (offset from the beginning of the category data pool to the name field)
250 const uint32_t categoryRecordWord3 = numeric_cast<uint32_t>((3u + eventRecordOffsets.size()) * uint32_t_size);
252 // Calculate the size in words of the category record
253 const size_t categoryRecordSize = 3u +// The size of the fixed part (device + counter_set + event_count +
254 // reserved + event_pointer_table_offset + name_offset)
255 eventRecordOffsets.size() + // The size of the variable part (
256 categoryNameBuffer.size() + // the event pointer table + the category name
257 eventRecordsSize; // including the null-terminator + the event records)
259 // Allocate the necessary space for the category record
260 categoryRecord.resize(categoryRecordSize);
262 ARMNN_NO_CONVERSION_WARN_BEGIN
263 // Create the category record
264 categoryRecord[0] = categoryRecordWord1; // event_count + reserved
265 categoryRecord[1] = categoryRecordWord2; // event_pointer_table_offset
266 categoryRecord[2] = categoryRecordWord3; // name_offset
267 auto offset = categoryRecord.begin() + 3u;
268 std::copy(eventRecordOffsets.begin(), eventRecordOffsets.end(), offset); // event_pointer_table
269 offset += eventRecordOffsets.size();
270 std::copy(categoryNameBuffer.begin(), categoryNameBuffer.end(), offset); // name
271 offset += categoryNameBuffer.size();
272 for (const EventRecord& eventRecord : eventRecords)
274 std::copy(eventRecord.begin(), eventRecord.end(), offset); // event_record
275 offset += eventRecord.size();
277 ARMNN_NO_CONVERSION_WARN_END
282 bool SendCounterPacket::CreateDeviceRecord(const DevicePtr& device,
283 DeviceRecord& deviceRecord,
284 std::string& errorMessage)
286 ARMNN_ASSERT(device);
288 uint16_t deviceUid = device->m_Uid;
289 const std::string& deviceName = device->m_Name;
290 uint16_t deviceCores = device->m_Cores;
292 ARMNN_ASSERT(!deviceName.empty());
294 // Device record word 0:
295 // 16:31 [16] uid: the unique identifier for the device
296 // 0:15 [16] cores: the number of individual streams of counters for one or more cores of some device
297 const uint32_t deviceRecordWord0 = (static_cast<uint32_t>(deviceUid) << 16) |
298 (static_cast<uint32_t>(deviceCores));
300 // Device record word 1:
301 // 0:31 [32] name_offset: offset from the beginning of the device record pool to the name field
302 const uint32_t deviceRecordWord1 = 8u; // The offset is always eight here, as the name field is always
303 // the first (and only) item in the pool and there are two device words
305 // Convert the device name into a SWTrace string
306 std::vector<uint32_t> deviceNameBuffer;
307 if (!StringToSwTraceString<SwTraceCharPolicy>(deviceName, deviceNameBuffer))
309 errorMessage = boost::str(boost::format("Cannot convert the name of device %1% (%2%) to an SWTrace string")
315 // Calculate the size in words of the device record
316 const size_t deviceRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
317 deviceNameBuffer.size(); // The size of the variable part (the device name including
318 // the null-terminator)
320 // Allocate the necessary space for the device record
321 deviceRecord.resize(deviceRecordSize);
323 // Create the device record
324 deviceRecord[0] = deviceRecordWord0; // uid + core
325 deviceRecord[1] = deviceRecordWord1; // name_offset
326 auto offset = deviceRecord.begin() + 2u;
327 std::copy(deviceNameBuffer.begin(), deviceNameBuffer.end(), offset); // name
332 bool SendCounterPacket::CreateCounterSetRecord(const CounterSetPtr& counterSet,
333 CounterSetRecord& counterSetRecord,
334 std::string& errorMessage)
336 ARMNN_ASSERT(counterSet);
338 uint16_t counterSetUid = counterSet->m_Uid;
339 const std::string& counterSetName = counterSet->m_Name;
340 uint16_t counterSetCount = counterSet->m_Count;
342 ARMNN_ASSERT(!counterSetName.empty());
344 // Counter set record word 0:
345 // 16:31 [16] uid: the unique identifier for the counter_set
346 // 0:15 [16] count: the number of counters which can be active in this set at any one time
347 const uint32_t counterSetRecordWord0 = (static_cast<uint32_t>(counterSetUid) << 16) |
348 (static_cast<uint32_t>(counterSetCount));
350 // Counter set record word 1:
351 // 0:31 [32] name_offset: offset from the beginning of the counter set pool to the name field
352 const uint32_t counterSetRecordWord1 = 8u; // The offset is always eight here, as the name field is always
353 // the first (and only) item in the pool after the two counter set words
355 // Convert the device name into a SWTrace namestring
356 std::vector<uint32_t> counterSetNameBuffer;
357 if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterSet->m_Name, counterSetNameBuffer))
359 errorMessage = boost::str(boost::format("Cannot convert the name of counter set %1% (%2%) to "
360 "an SWTrace namestring")
366 // Calculate the size in words of the counter set record
367 const size_t counterSetRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
368 counterSetNameBuffer.size(); // The size of the variable part (the counter set
369 // name including the null-terminator)
371 // Allocate the space for the counter set record
372 counterSetRecord.resize(counterSetRecordSize);
374 // Create the counter set record
375 counterSetRecord[0] = counterSetRecordWord0; // uid + core
376 counterSetRecord[1] = counterSetRecordWord1; // name_offset
377 auto offset = counterSetRecord.begin() + 2u;
378 std::copy(counterSetNameBuffer.begin(), counterSetNameBuffer.end(), offset); // name
383 bool SendCounterPacket::CreateEventRecord(const CounterPtr& counter,
384 EventRecord& eventRecord,
385 std::string& errorMessage)
387 using namespace boost::numeric;
389 ARMNN_ASSERT(counter);
391 uint16_t counterUid = counter->m_Uid;
392 uint16_t maxCounterUid = counter->m_MaxCounterUid;
393 uint16_t deviceUid = counter->m_DeviceUid;
394 uint16_t counterSetUid = counter->m_CounterSetUid;
395 uint16_t counterClass = counter->m_Class;
396 uint16_t counterInterpolation = counter->m_Interpolation;
397 double counterMultiplier = counter->m_Multiplier;
398 const std::string& counterName = counter->m_Name;
399 const std::string& counterDescription = counter->m_Description;
400 const std::string& counterUnits = counter->m_Units;
402 ARMNN_ASSERT(counterClass == 0 || counterClass == 1);
403 ARMNN_ASSERT(counterInterpolation == 0 || counterInterpolation == 1);
404 ARMNN_ASSERT(counterMultiplier);
407 const size_t uint32_t_size = sizeof(uint32_t);
408 // eventRecordBlockSize is the size of the fixed part
409 // (counter_uid + max_counter_uid + device +
410 // counter_set + class + interpolation +
411 // multiplier + name_offset + description_offset +
413 const size_t eventRecordBlockSize = 8u;
415 // Event record word 0:
416 // 16:31 [16] max_counter_uid: if the device this event is associated with has more than one core and there
417 // is one of these counters per core this value will be set to
418 // (counter_uid + cores (from device_record)) - 1.
419 // If there is only a single core then this value will be the same as
420 // the counter_uid value
421 // 0:15 [16] count_uid: unique ID for the counter. Must be unique across all counters in all categories
422 const uint32_t eventRecordWord0 = (static_cast<uint32_t>(maxCounterUid) << 16) |
423 (static_cast<uint32_t>(counterUid));
425 // Event record word 1:
426 // 16:31 [16] device: UID of the device this event is associated with. Set to zero if the event is NOT
427 // associated with a device
428 // 0:15 [16] counter_set: UID of the counter_set this event is associated with. Set to zero if the event
429 // is NOT associated with a counter_set
430 const uint32_t eventRecordWord1 = (static_cast<uint32_t>(deviceUid) << 16) |
431 (static_cast<uint32_t>(counterSetUid));
433 // Event record word 2:
434 // 16:31 [16] class: type describing how to treat each data point in a stream of data points
435 // 0:15 [16] interpolation: type describing how to interpolate each data point in a stream of data points
436 const uint32_t eventRecordWord2 = (static_cast<uint32_t>(counterClass) << 16) |
437 (static_cast<uint32_t>(counterInterpolation));
439 // Event record word 3-4:
440 // 0:63 [64] multiplier: internal data stream is represented as integer values, this allows scaling of
441 // those values as if they are fixed point numbers. Zero is not a valid value
442 uint32_t multiplier[2] = { 0u, 0u };
443 ARMNN_ASSERT(sizeof(counterMultiplier) == sizeof(multiplier));
444 std::memcpy(multiplier, &counterMultiplier, sizeof(multiplier));
445 const uint32_t eventRecordWord3 = multiplier[0];
446 const uint32_t eventRecordWord4 = multiplier[1];
448 // Event record word 5:
449 // 0:31 [32] name_offset: offset from the beginning of the event record pool to the name field
450 const uint32_t eventRecordWord5 = static_cast<uint32_t>(eventRecordBlockSize * uint32_t_size);
452 // Convert the counter name into a SWTrace string
453 std::vector<uint32_t> counterNameBuffer;
454 if (!StringToSwTraceString<SwTraceCharPolicy>(counterName, counterNameBuffer))
456 errorMessage = boost::str(boost::format("Cannot convert the name of counter %1% (name: %2%) "
457 "to an SWTrace string")
463 // Event record word 6:
464 // 0:31 [32] description_offset: offset from the beginning of the event record pool to the description field
465 // The size of the name buffer in bytes
466 uint32_t eventRecordWord6 =
467 static_cast<uint32_t>((counterNameBuffer.size() + eventRecordBlockSize) * uint32_t_size);
469 // Convert the counter description into a SWTrace string
470 std::vector<uint32_t> counterDescriptionBuffer;
471 if (!StringToSwTraceString<SwTraceCharPolicy>(counterDescription, counterDescriptionBuffer))
473 errorMessage = boost::str(boost::format("Cannot convert the description of counter %1% (description: %2%) "
474 "to an SWTrace string")
480 // Event record word 7:
481 // 0:31 [32] units_offset: (optional) offset from the beginning of the event record pool to the units field.
482 // An offset value of zero indicates this field is not provided
483 bool includeUnits = !counterUnits.empty();
484 // The size of the description buffer in bytes
485 const uint32_t eventRecordWord7 = includeUnits ?
487 numeric_cast<uint32_t>(counterDescriptionBuffer.size()
491 // Convert the counter units into a SWTrace namestring (optional)
492 std::vector<uint32_t> counterUnitsBuffer;
495 // Convert the counter units into a SWTrace namestring
496 if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterUnits, counterUnitsBuffer))
498 errorMessage = boost::str(boost::format("Cannot convert the units of counter %1% (units: %2%) "
499 "to an SWTrace string")
506 // Calculate the size in words of the event record
507 const size_t eventRecordSize = eventRecordBlockSize +
508 counterNameBuffer.size() + // The size of the variable part (the counter name,
509 counterDescriptionBuffer.size() + // description and units
510 counterUnitsBuffer.size(); // including the null-terminator)
512 // Allocate the space for the event record
513 eventRecord.resize(eventRecordSize);
515 ARMNN_NO_CONVERSION_WARN_BEGIN
516 // Create the event record
517 eventRecord[0] = eventRecordWord0; // max_counter_uid + counter_uid
518 eventRecord[1] = eventRecordWord1; // device + counter_set
519 eventRecord[2] = eventRecordWord2; // class + interpolation
520 eventRecord[3] = eventRecordWord3; // multiplier
521 eventRecord[4] = eventRecordWord4; // multiplier
522 eventRecord[5] = eventRecordWord5; // name_offset
523 eventRecord[6] = eventRecordWord6; // description_offset
524 eventRecord[7] = eventRecordWord7; // units_offset
525 auto offset = eventRecord.begin() + 8u;
526 std::copy(counterNameBuffer.begin(), counterNameBuffer.end(), offset); // name
527 offset += counterNameBuffer.size();
528 std::copy(counterDescriptionBuffer.begin(), counterDescriptionBuffer.end(), offset); // description
531 offset += counterDescriptionBuffer.size();
532 std::copy(counterUnitsBuffer.begin(), counterUnitsBuffer.end(), offset); // units
534 ARMNN_NO_CONVERSION_WARN_END
539 void SendCounterPacket::SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory)
541 using namespace boost::numeric;
543 // Get the amount of data that needs to be put into the packet
544 const uint16_t categoryCount = counterDirectory.GetCategoryCount();
545 const uint16_t deviceCount = counterDirectory.GetDeviceCount();
546 const uint16_t counterSetCount = counterDirectory.GetCounterSetCount();
549 const size_t uint32_t_size = sizeof(uint32_t);
550 const size_t packetHeaderSize = 2u;
551 const size_t bodyHeaderSize = 6u;
552 const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * uint32_t_size;
554 // Initialize the offset for the pointer tables
555 uint32_t pointerTableOffset = 0;
561 // Process device records
562 std::vector<DeviceRecord> deviceRecords(deviceCount);
563 const Devices& devices = counterDirectory.GetDevices();
564 std::vector<uint32_t> deviceRecordOffsets(deviceCount, 0); // device_records_pointer_table
565 size_t deviceRecordsSize = 0;
566 size_t deviceIndex = 0;
567 size_t deviceRecordOffsetIndex = 0;
569 pointerTableOffset = numeric_cast<uint32_t>(deviceCount * uint32_t_size +
570 counterSetCount * uint32_t_size +
571 categoryCount * uint32_t_size);
572 for (auto it = devices.begin(); it != devices.end(); it++)
574 const DevicePtr& device = it->second;
575 DeviceRecord& deviceRecord = deviceRecords.at(deviceIndex);
577 std::string errorMessage;
578 if (!CreateDeviceRecord(device, deviceRecord, errorMessage))
580 CancelOperationAndThrow<RuntimeException>(errorMessage);
583 // Update the total size in words of the device records
584 deviceRecordsSize += deviceRecord.size();
586 // Add the device record offset to the device records pointer table offset field
587 deviceRecordOffsets[deviceRecordOffsetIndex] = pointerTableOffset;
588 pointerTableOffset += numeric_cast<uint32_t>(deviceRecord.size() * uint32_t_size);
591 deviceRecordOffsetIndex++;
594 // -------------------
595 // Counter set records
596 // -------------------
598 // Process counter set records
599 std::vector<CounterSetRecord> counterSetRecords(counterSetCount);
600 const CounterSets& counterSets = counterDirectory.GetCounterSets();
601 std::vector<uint32_t> counterSetRecordOffsets(counterSetCount, 0); // counter_set_records_pointer_table
602 size_t counterSetRecordsSize = 0;
603 size_t counterSetIndex = 0;
604 size_t counterSetRecordOffsetIndex = 0;
606 pointerTableOffset -= numeric_cast<uint32_t>(deviceCount * uint32_t_size);
607 for (auto it = counterSets.begin(); it != counterSets.end(); it++)
609 const CounterSetPtr& counterSet = it->second;
610 CounterSetRecord& counterSetRecord = counterSetRecords.at(counterSetIndex);
612 std::string errorMessage;
613 if (!CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage))
615 CancelOperationAndThrow<RuntimeException>(errorMessage);
618 // Update the total size in words of the counter set records
619 counterSetRecordsSize += counterSetRecord.size();
621 // Add the counter set record offset to the counter set records pointer table offset field
622 counterSetRecordOffsets[counterSetRecordOffsetIndex] = pointerTableOffset;
623 pointerTableOffset += numeric_cast<uint32_t>(counterSetRecord.size() * uint32_t_size);
626 counterSetRecordOffsetIndex++;
633 // Process category records
634 std::vector<CategoryRecord> categoryRecords(categoryCount);
635 const Categories& categories = counterDirectory.GetCategories();
636 std::vector<uint32_t> categoryRecordOffsets(categoryCount, 0); // category_records_pointer_table
637 size_t categoryRecordsSize = 0;
638 size_t categoryIndex = 0;
639 size_t categoryRecordOffsetIndex = 0;
641 pointerTableOffset -= numeric_cast<uint32_t>(counterSetCount * uint32_t_size);
642 for (auto it = categories.begin(); it != categories.end(); it++)
644 const CategoryPtr& category = *it;
645 CategoryRecord& categoryRecord = categoryRecords.at(categoryIndex);
647 std::string errorMessage;
648 if (!CreateCategoryRecord(category, counterDirectory.GetCounters(), categoryRecord, errorMessage))
650 CancelOperationAndThrow<RuntimeException>(errorMessage);
653 // Update the total size in words of the category records
654 categoryRecordsSize += categoryRecord.size();
656 // Add the category record offset to the category records pointer table offset field
657 categoryRecordOffsets[categoryRecordOffsetIndex] = pointerTableOffset;
658 pointerTableOffset += numeric_cast<uint32_t>(categoryRecord.size() * uint32_t_size);
661 categoryRecordOffsetIndex++;
664 // Calculate the length in words of the counter directory packet's data (excludes the packet header size)
665 const size_t counterDirectoryPacketDataLength =
666 bodyHeaderSize + // The size of the body header
667 deviceRecordOffsets.size() + // The size of the device records pointer table
668 counterSetRecordOffsets.size() + // The size of counter set pointer table
669 categoryRecordOffsets.size() + // The size of category records pointer table
670 deviceRecordsSize + // The total size of the device records
671 counterSetRecordsSize + // The total size of the counter set records
672 categoryRecordsSize; // The total size of the category records
674 // Calculate the size in words of the counter directory packet (the data length plus the packet header size)
675 const size_t counterDirectoryPacketSize = packetHeaderSize + // The size of the packet header
676 counterDirectoryPacketDataLength; // The data length
678 // Allocate the necessary space for the counter directory packet
679 std::vector<uint32_t> counterDirectoryPacket(counterDirectoryPacketSize, 0);
685 // Packet header word 0:
686 // 26:31 [6] packet_family: control Packet Family
687 // 16:25 [10] packet_id: packet identifier
688 // 8:15 [8] reserved: all zeros
689 // 0:7 [8] reserved: all zeros
690 uint32_t packetFamily = 0;
691 uint32_t packetId = 2;
692 uint32_t packetHeaderWord0 = ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16);
694 // Packet header word 1:
695 // 0:31 [32] data_length: length of data, in bytes
696 uint32_t packetHeaderWord1 = numeric_cast<uint32_t>(counterDirectoryPacketDataLength * uint32_t_size);
698 // Create the packet header
699 uint32_t packetHeader[2]
701 packetHeaderWord0, // packet_family + packet_id + reserved + reserved
702 packetHeaderWord1 // data_length
709 // Body header word 0:
710 // 16:31 [16] device_records_count: number of entries in the device_records_pointer_table
711 // 0:15 [16] reserved: all zeros
712 const uint32_t bodyHeaderWord0 = static_cast<uint32_t>(deviceCount) << 16;
714 // Body header word 1:
715 // 0:31 [32] device_records_pointer_table_offset: offset to the device_records_pointer_table
716 const uint32_t bodyHeaderWord1 = bodyHeaderSizeBytes; // The offset is always the bodyHeaderSize,
717 // as the device record pointer table field
718 // is always the first item in the pool
720 // Body header word 2:
721 // 16:31 [16] counter_set_count: number of entries in the counter_set_pointer_table
722 // 0:15 [16] reserved: all zeros
723 const uint32_t bodyHeaderWord2 = static_cast<uint32_t>(counterSetCount) << 16;
725 // Body header word 3:
726 // 0:31 [32] counter_set_pointer_table_offset: offset to the counter_set_pointer_table
727 const uint32_t bodyHeaderWord3 =
728 numeric_cast<uint32_t>(deviceRecordOffsets.size() * uint32_t_size // The size of the
729 + bodyHeaderSizeBytes); // device records pointer table
731 // Body header word 4:
732 // 16:31 [16] categories_count: number of entries in the categories_pointer_table
733 // 0:15 [16] reserved: all zeros
734 const uint32_t bodyHeaderWord4 = static_cast<uint32_t>(categoryCount) << 16;
736 // Body header word 3:
737 // 0:31 [32] categories_pointer_table_offset: offset to the categories_pointer_table
738 const uint32_t bodyHeaderWord5 =
739 numeric_cast<uint32_t>(
740 deviceRecordOffsets.size() * uint32_t_size + // The size of the device records
741 counterSetRecordOffsets.size() * uint32_t_size // pointer table, plus the size of
742 + bodyHeaderSizeBytes); // the counter set pointer table
744 // Create the body header
745 const uint32_t bodyHeader[bodyHeaderSize]
747 bodyHeaderWord0, // device_records_count + reserved
748 bodyHeaderWord1, // device_records_pointer_table_offset
749 bodyHeaderWord2, // counter_set_count + reserved
750 bodyHeaderWord3, // counter_set_pointer_table_offset
751 bodyHeaderWord4, // categories_count + reserved
752 bodyHeaderWord5 // categories_pointer_table_offset
755 ARMNN_NO_CONVERSION_WARN_BEGIN
756 // Create the counter directory packet
757 auto counterDirectoryPacketOffset = counterDirectoryPacket.begin();
759 std::copy(packetHeader, packetHeader + packetHeaderSize, counterDirectoryPacketOffset);
760 counterDirectoryPacketOffset += packetHeaderSize;
762 std::copy(bodyHeader, bodyHeader + bodyHeaderSize, counterDirectoryPacketOffset);
763 counterDirectoryPacketOffset += bodyHeaderSize;
764 // device_records_pointer_table
765 std::copy(deviceRecordOffsets.begin(), deviceRecordOffsets.end(), counterDirectoryPacketOffset);
766 counterDirectoryPacketOffset += deviceRecordOffsets.size();
767 // counter_set_pointer_table
768 std::copy(counterSetRecordOffsets.begin(), counterSetRecordOffsets.end(), counterDirectoryPacketOffset);
769 counterDirectoryPacketOffset += counterSetRecordOffsets.size();
770 // category_pointer_table
771 std::copy(categoryRecordOffsets.begin(), categoryRecordOffsets.end(), counterDirectoryPacketOffset);
772 counterDirectoryPacketOffset += categoryRecordOffsets.size();
774 for (const DeviceRecord& deviceRecord : deviceRecords)
776 std::copy(deviceRecord.begin(), deviceRecord.end(), counterDirectoryPacketOffset); // device_record
777 counterDirectoryPacketOffset += deviceRecord.size();
779 // counter_set_records
780 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
782 std::copy(counterSetRecord.begin(), counterSetRecord.end(), counterDirectoryPacketOffset); // counter_set_record
783 counterDirectoryPacketOffset += counterSetRecord.size();
786 for (const CategoryRecord& categoryRecord : categoryRecords)
788 std::copy(categoryRecord.begin(), categoryRecord.end(), counterDirectoryPacketOffset); // category_record
789 counterDirectoryPacketOffset += categoryRecord.size();
791 ARMNN_NO_CONVERSION_WARN_END
793 // Calculate the total size in bytes of the counter directory packet
794 uint32_t totalSize = numeric_cast<uint32_t>(counterDirectoryPacketSize * uint32_t_size);
796 // Reserve space in the buffer for the packet
797 uint32_t reserved = 0;
798 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
800 if (writeBuffer == nullptr || reserved < totalSize)
802 CancelOperationAndThrow<BufferExhaustion>(
804 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
807 // Offset for writing to the buffer
810 // Write the counter directory packet to the buffer
811 for (uint32_t counterDirectoryPacketWord : counterDirectoryPacket)
813 WriteUint32(writeBuffer, offset, counterDirectoryPacketWord);
814 offset += numeric_cast<uint32_t>(uint32_t_size);
817 m_BufferManager.Commit(writeBuffer, totalSize);
820 void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
822 uint32_t uint16_t_size = sizeof(uint16_t);
823 uint32_t uint32_t_size = sizeof(uint32_t);
824 uint32_t uint64_t_size = sizeof(uint64_t);
826 uint32_t packetFamily = 3;
827 uint32_t packetClass = 0;
828 uint32_t packetType = 0;
829 uint32_t headerSize = 2 * uint32_t_size;
830 uint32_t bodySize = uint64_t_size + numeric_cast<uint32_t>(values.size()) * (uint16_t_size + uint32_t_size);
831 uint32_t totalSize = headerSize + bodySize;
833 uint32_t reserved = 0;
835 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
837 if (writeBuffer == nullptr || reserved < totalSize)
839 CancelOperationAndThrow<BufferExhaustion>(
841 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
845 WriteUint32(writeBuffer,
847 ((packetFamily & 0x0000003F) << 26) |
848 ((packetClass & 0x0000007F) << 19) |
849 ((packetType & 0x00000007) << 16));
850 offset += uint32_t_size;
851 WriteUint32(writeBuffer, offset, bodySize);
853 // Copy captured Timestamp.
854 offset += uint32_t_size;
855 WriteUint64(writeBuffer, offset, timestamp);
857 // Copy selectedCounterIds.
858 offset += uint64_t_size;
859 for (const auto& pair: values)
861 WriteUint16(writeBuffer, offset, pair.counterId);
862 offset += uint16_t_size;
863 WriteUint32(writeBuffer, offset, pair.counterValue);
864 offset += uint32_t_size;
867 m_BufferManager.Commit(writeBuffer, totalSize);
870 void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
871 const std::vector<uint16_t>& selectedCounterIds)
873 uint32_t uint16_t_size = sizeof(uint16_t);
874 uint32_t uint32_t_size = sizeof(uint32_t);
876 uint32_t packetFamily = 0;
877 uint32_t packetId = 4;
878 uint32_t headerSize = 2 * uint32_t_size;
879 uint32_t bodySize = uint32_t_size + numeric_cast<uint32_t>(selectedCounterIds.size()) * uint16_t_size;
880 uint32_t totalSize = headerSize + bodySize;
882 uint32_t reserved = 0;
884 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
886 if (writeBuffer == nullptr || reserved < totalSize)
888 CancelOperationAndThrow<BufferExhaustion>(
890 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
894 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
895 offset += uint32_t_size;
896 WriteUint32(writeBuffer, offset, bodySize);
898 // Copy capturePeriod.
899 offset += uint32_t_size;
900 WriteUint32(writeBuffer, offset, capturePeriod);
902 // Copy selectedCounterIds.
903 offset += uint32_t_size;
904 for(const uint16_t& id: selectedCounterIds)
906 WriteUint16(writeBuffer, offset, id);
907 offset += uint16_t_size;
910 m_BufferManager.Commit(writeBuffer, totalSize);
913 } // namespace profiling