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 <armnn/utility/IgnoreUnused.hpp>
15 #include <boost/format.hpp>
16 #include <boost/numeric/conversion/cast.hpp>
26 using boost::numeric_cast;
28 const unsigned int SendCounterPacket::PIPE_MAGIC;
30 void SendCounterPacket::SendStreamMetaDataPacket()
32 std::string info(GetSoftwareInfo());
33 std::string hardwareVersion(GetHardwareVersion());
34 std::string softwareVersion(GetSoftwareVersion());
35 std::string processName = GetProcessName().substr(0, 60);
37 uint32_t infoSize = numeric_cast<uint32_t>(info.size()) > 0 ? numeric_cast<uint32_t>(info.size()) + 1 : 0;
38 uint32_t hardwareVersionSize = numeric_cast<uint32_t>(hardwareVersion.size()) > 0 ?
39 numeric_cast<uint32_t>(hardwareVersion.size()) + 1 : 0;
40 uint32_t softwareVersionSize = numeric_cast<uint32_t>(softwareVersion.size()) > 0 ?
41 numeric_cast<uint32_t>(softwareVersion.size()) + 1 : 0;
42 uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) > 0 ?
43 numeric_cast<uint32_t>(processName.size()) + 1 : 0;
45 uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
47 uint32_t headerSize = 2 * sizeUint32;
48 uint32_t bodySize = 10 * sizeUint32;
49 uint32_t packetVersionCountSize = sizeUint32;
52 // Stream metadata packet (packet family=0; packet id=0)
53 // Connection Acknowledged packet (packet family=0, packet id=1)
54 // Counter Directory packet (packet family=0; packet id=2)
55 // Request Counter Directory packet (packet family=0, packet id=3)
56 // Periodic Counter Selection packet (packet family=0, packet id=4)
57 // Periodic Counter Capture packet (packet family=1, packet class=0, type=0)
58 uint32_t packetVersionEntries = 6;
60 uint32_t payloadSize = numeric_cast<uint32_t>(infoSize + hardwareVersionSize + softwareVersionSize +
61 processNameSize + packetVersionCountSize +
62 (packetVersionEntries * 2 * sizeUint32));
64 uint32_t totalSize = headerSize + bodySize + payloadSize;
66 uint32_t reserved = 0;
68 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
70 if (writeBuffer == nullptr || reserved < totalSize)
72 CancelOperationAndThrow<BufferExhaustion>(
74 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
81 WriteUint32(writeBuffer, offset, 0);
83 WriteUint32(writeBuffer, offset, totalSize - headerSize);
88 WriteUint32(writeBuffer, offset, PIPE_MAGIC); // pipe_magic
90 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0)); // stream_metadata_version
92 WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length
94 int pid = armnnUtils::Processes::GetCurrentId();
95 WriteUint32(writeBuffer, offset, numeric_cast<uint32_t>(pid)); // pid
97 uint32_t poolOffset = bodySize;
98 WriteUint32(writeBuffer, offset, infoSize ? poolOffset : 0); // offset_info
100 poolOffset += infoSize;
101 WriteUint32(writeBuffer, offset, hardwareVersionSize ? poolOffset : 0); // offset_hw_version
102 offset += sizeUint32;
103 poolOffset += hardwareVersionSize;
104 WriteUint32(writeBuffer, offset, softwareVersionSize ? poolOffset : 0); // offset_sw_version
105 offset += sizeUint32;
106 poolOffset += softwareVersionSize;
107 WriteUint32(writeBuffer, offset, processNameSize ? poolOffset : 0); // offset_process_name
108 offset += sizeUint32;
109 poolOffset += processNameSize;
110 WriteUint32(writeBuffer, offset, packetVersionEntries ? poolOffset : 0); // offset_packet_version_table
111 offset += sizeUint32;
112 WriteUint32(writeBuffer, offset, 0); // reserved
113 offset += sizeUint32;
119 memcpy(&writeBuffer->GetWritableData()[offset], info.c_str(), infoSize);
123 if (hardwareVersionSize)
125 memcpy(&writeBuffer->GetWritableData()[offset], hardwareVersion.c_str(), hardwareVersionSize);
126 offset += hardwareVersionSize;
129 if (softwareVersionSize)
131 memcpy(&writeBuffer->GetWritableData()[offset], softwareVersion.c_str(), softwareVersionSize);
132 offset += softwareVersionSize;
137 memcpy(&writeBuffer->GetWritableData()[offset], processName.c_str(), processNameSize);
138 offset += processNameSize;
141 if (packetVersionEntries)
143 // Packet Version Count
144 WriteUint32(writeBuffer, offset, packetVersionEntries << 16);
146 // Packet Version Entries
147 uint32_t packetFamily = 0;
148 uint32_t packetId = 0;
150 offset += sizeUint32;
151 for (uint32_t i = 0; i < packetVersionEntries - 1; ++i)
153 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId++ & 0x3FF) << 16));
154 offset += sizeUint32;
155 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
156 offset += sizeUint32;
162 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
163 offset += sizeUint32;
164 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
169 CancelOperationAndThrow<RuntimeException>(writeBuffer, "Error processing packet.");
172 m_BufferManager.Commit(writeBuffer, totalSize, false);
175 bool SendCounterPacket::CreateCategoryRecord(const CategoryPtr& category,
176 const Counters& counters,
177 CategoryRecord& categoryRecord,
178 std::string& errorMessage)
180 using namespace boost::numeric;
182 ARMNN_ASSERT(category);
184 const std::string& categoryName = category->m_Name;
185 ARMNN_ASSERT(!categoryName.empty());
187 // Remove any duplicate counters
188 std::vector<uint16_t> categoryCounters;
189 for (size_t counterIndex = 0; counterIndex < category->m_Counters.size(); ++counterIndex)
191 uint16_t counterUid = category->m_Counters.at(counterIndex);
192 auto it = counters.find(counterUid);
193 if (it == counters.end())
195 errorMessage = boost::str(boost::format("Counter (%1%) not found in category (%2%)")
196 % counterUid % category->m_Name );
200 const CounterPtr& counter = it->second;
202 if (counterUid == counter->m_MaxCounterUid)
204 categoryCounters.emplace_back(counterUid);
207 if (categoryCounters.empty())
209 errorMessage = boost::str(boost::format("No valid counters found in category (%1%)")% categoryName);
214 size_t uint32_t_size = sizeof(uint32_t);
216 // Category record word 1:
217 // 16:31 [16] event_count: number of events belonging to this category
218 // 0:15 [16] reserved: all zeros
219 uint32_t categoryRecordWord1 = static_cast<uint32_t>(categoryCounters.size()) << 16;
221 // Category record word 2:
222 // 0:31 [32] event_pointer_table_offset: offset from the beginning of the category data pool to
223 // the event_pointer_table
224 uint32_t categoryRecordWord2 = 0; // The offset is always zero here, as the event pointer table field is always
225 // the first item in the pool
227 // Convert the device name into a SWTrace namestring
228 std::vector<uint32_t> categoryNameBuffer;
229 if (!StringToSwTraceString<SwTraceNameCharPolicy>(categoryName, categoryNameBuffer))
231 errorMessage = boost::str(boost::format("Cannot convert the name of category (%1%) to an SWTrace namestring")
236 // Process the event records
237 size_t counterCount = categoryCounters.size();
238 std::vector<EventRecord> eventRecords(counterCount);
239 std::vector<uint32_t> eventRecordOffsets(counterCount, 0);
240 size_t eventRecordsSize = 0;
241 uint32_t eventRecordsOffset =
242 numeric_cast<uint32_t>((eventRecords.size() + categoryNameBuffer.size()) * uint32_t_size);
243 for (size_t counterIndex = 0, eventRecordIndex = 0, eventRecordOffsetIndex = 0;
244 counterIndex < counterCount;
245 counterIndex++, eventRecordIndex++, eventRecordOffsetIndex++)
247 uint16_t counterUid = categoryCounters.at(counterIndex);
248 auto it = counters.find(counterUid);
249 const CounterPtr& counter = it->second;
251 EventRecord& eventRecord = eventRecords.at(eventRecordIndex);
252 if (!CreateEventRecord(counter, eventRecord, errorMessage))
257 // Update the total size in words of the event records
258 eventRecordsSize += eventRecord.size();
260 // Add the event record offset to the event pointer table offset field
261 eventRecordOffsets[eventRecordOffsetIndex] = eventRecordsOffset;
262 eventRecordsOffset += numeric_cast<uint32_t>(eventRecord.size() * uint32_t_size);
265 // Category record word 3:
266 // 0:31 [32] name_offset (offset from the beginning of the category data pool to the name field)
267 uint32_t categoryRecordWord3 = numeric_cast<uint32_t>(eventRecordOffsets.size() * uint32_t_size);
269 // Calculate the size in words of the category record
270 size_t categoryRecordSize = 3u + // The size of the fixed part (device + counter_set + event_count + reserved +
271 // event_pointer_table_offset + name_offset)
272 eventRecordOffsets.size() + // The size of the variable part (the event pointer table +
273 categoryNameBuffer.size() + // and the category name including the null-terminator +
274 eventRecordsSize; // the event records)
276 // Allocate the necessary space for the category record
277 categoryRecord.resize(categoryRecordSize);
279 ARMNN_NO_CONVERSION_WARN_BEGIN
280 // Create the category record
281 categoryRecord[0] = categoryRecordWord1; // event_count + reserved
282 categoryRecord[1] = categoryRecordWord2; // event_pointer_table_offset
283 categoryRecord[2] = categoryRecordWord3; // name_offset
284 auto offset = categoryRecord.begin() + 3u;
285 std::copy(eventRecordOffsets.begin(), eventRecordOffsets.end(), offset); // event_pointer_table
286 offset += eventRecordOffsets.size();
287 std::copy(categoryNameBuffer.begin(), categoryNameBuffer.end(), offset); // name
288 offset += categoryNameBuffer.size();
289 for (const EventRecord& eventRecord : eventRecords)
291 std::copy(eventRecord.begin(), eventRecord.end(), offset); // event_record
292 offset += eventRecord.size();
294 ARMNN_NO_CONVERSION_WARN_END
299 bool SendCounterPacket::CreateDeviceRecord(const DevicePtr& device,
300 DeviceRecord& deviceRecord,
301 std::string& errorMessage)
303 ARMNN_ASSERT(device);
305 uint16_t deviceUid = device->m_Uid;
306 const std::string& deviceName = device->m_Name;
307 uint16_t deviceCores = device->m_Cores;
309 ARMNN_ASSERT(!deviceName.empty());
311 // Device record word 0:
312 // 16:31 [16] uid: the unique identifier for the device
313 // 0:15 [16] cores: the number of individual streams of counters for one or more cores of some device
314 uint32_t deviceRecordWord0 = (static_cast<uint32_t>(deviceUid) << 16) |
315 (static_cast<uint32_t>(deviceCores));
317 // Device record word 1:
318 // 0:31 [32] name_offset: offset from the beginning of the device record pool to the name field
319 uint32_t deviceRecordWord1 = 0; // The offset is always zero here, as the name field is always
320 // the first (and only) item in the pool
322 // Convert the device name into a SWTrace string
323 std::vector<uint32_t> deviceNameBuffer;
324 if (!StringToSwTraceString<SwTraceCharPolicy>(deviceName, deviceNameBuffer))
326 errorMessage = boost::str(boost::format("Cannot convert the name of device %1% (%2%) to an SWTrace string")
332 // Calculate the size in words of the device record
333 size_t deviceRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
334 deviceNameBuffer.size(); // The size of the variable part (the device name including
335 // the null-terminator)
337 // Allocate the necessary space for the device record
338 deviceRecord.resize(deviceRecordSize);
340 // Create the device record
341 deviceRecord[0] = deviceRecordWord0; // uid + core
342 deviceRecord[1] = deviceRecordWord1; // name_offset
343 auto offset = deviceRecord.begin() + 2u;
344 std::copy(deviceNameBuffer.begin(), deviceNameBuffer.end(), offset); // name
349 bool SendCounterPacket::CreateCounterSetRecord(const CounterSetPtr& counterSet,
350 CounterSetRecord& counterSetRecord,
351 std::string& errorMessage)
353 ARMNN_ASSERT(counterSet);
355 uint16_t counterSetUid = counterSet->m_Uid;
356 const std::string& counterSetName = counterSet->m_Name;
357 uint16_t counterSetCount = counterSet->m_Count;
359 ARMNN_ASSERT(!counterSetName.empty());
361 // Counter set record word 0:
362 // 16:31 [16] uid: the unique identifier for the counter_set
363 // 0:15 [16] count: the number of counters which can be active in this set at any one time
364 uint32_t counterSetRecordWord0 = (static_cast<uint32_t>(counterSetUid) << 16) |
365 (static_cast<uint32_t>(counterSetCount));
367 // Counter set record word 1:
368 // 0:31 [32] name_offset: offset from the beginning of the counter set pool to the name field
369 uint32_t counterSetRecordWord1 = 0; // The offset is always zero here, as the name field is always
370 // the first (and only) item in the pool
372 // Convert the device name into a SWTrace namestring
373 std::vector<uint32_t> counterSetNameBuffer;
374 if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterSet->m_Name, counterSetNameBuffer))
376 errorMessage = boost::str(boost::format("Cannot convert the name of counter set %1% (%2%) to "
377 "an SWTrace namestring")
383 // Calculate the size in words of the counter set record
384 size_t counterSetRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
385 counterSetNameBuffer.size(); // The size of the variable part (the counter set name
386 // including the null-terminator)
388 // Allocate the space for the counter set record
389 counterSetRecord.resize(counterSetRecordSize);
391 // Create the counter set record
392 counterSetRecord[0] = counterSetRecordWord0; // uid + core
393 counterSetRecord[1] = counterSetRecordWord1; // name_offset
394 auto offset = counterSetRecord.begin() + 2u;
395 std::copy(counterSetNameBuffer.begin(), counterSetNameBuffer.end(), offset); // name
400 bool SendCounterPacket::CreateEventRecord(const CounterPtr& counter,
401 EventRecord& eventRecord,
402 std::string& errorMessage)
404 using namespace boost::numeric;
406 ARMNN_ASSERT(counter);
408 uint16_t counterUid = counter->m_Uid;
409 uint16_t maxCounterUid = counter->m_MaxCounterUid;
410 uint16_t deviceUid = counter->m_DeviceUid;
411 uint16_t counterSetUid = counter->m_CounterSetUid;
412 uint16_t counterClass = counter->m_Class;
413 uint16_t counterInterpolation = counter->m_Interpolation;
414 double counterMultiplier = counter->m_Multiplier;
415 const std::string& counterName = counter->m_Name;
416 const std::string& counterDescription = counter->m_Description;
417 const std::string& counterUnits = counter->m_Units;
419 ARMNN_ASSERT(counterClass == 0 || counterClass == 1);
420 ARMNN_ASSERT(counterInterpolation == 0 || counterInterpolation == 1);
421 ARMNN_ASSERT(counterMultiplier);
424 size_t uint32_t_size = sizeof(uint32_t);
426 // Event record word 0:
427 // 16:31 [16] max_counter_uid: if the device this event is associated with has more than one core and there
428 // is one of these counters per core this value will be set to
429 // (counter_uid + cores (from device_record)) - 1.
430 // If there is only a single core then this value will be the same as
431 // the counter_uid value
432 // 0:15 [16] count_uid: unique ID for the counter. Must be unique across all counters in all categories
433 uint32_t eventRecordWord0 = (static_cast<uint32_t>(maxCounterUid) << 16) |
434 (static_cast<uint32_t>(counterUid));
436 // Event record word 1:
437 // 16:31 [16] device: UID of the device this event is associated with. Set to zero if the event is NOT
438 // associated with a device
439 // 0:15 [16] counter_set: UID of the counter_set this event is associated with. Set to zero if the event
440 // is NOT associated with a counter_set
441 uint32_t eventRecordWord1 = (static_cast<uint32_t>(deviceUid) << 16) |
442 (static_cast<uint32_t>(counterSetUid));
444 // Event record word 2:
445 // 16:31 [16] class: type describing how to treat each data point in a stream of data points
446 // 0:15 [16] interpolation: type describing how to interpolate each data point in a stream of data points
447 uint32_t eventRecordWord2 = (static_cast<uint32_t>(counterClass) << 16) |
448 (static_cast<uint32_t>(counterInterpolation));
450 // Event record word 3-4:
451 // 0:63 [64] multiplier: internal data stream is represented as integer values, this allows scaling of
452 // those values as if they are fixed point numbers. Zero is not a valid value
453 uint32_t multiplier[2] = { 0u, 0u };
454 ARMNN_ASSERT(sizeof(counterMultiplier) == sizeof(multiplier));
455 std::memcpy(multiplier, &counterMultiplier, sizeof(multiplier));
456 uint32_t eventRecordWord3 = multiplier[0];
457 uint32_t eventRecordWord4 = multiplier[1];
459 // Event record word 5:
460 // 0:31 [32] name_offset: offset from the beginning of the event record pool to the name field
461 uint32_t eventRecordWord5 = 0; // The offset is always zero here, as the name field is always
462 // the first item in the pool
464 // Convert the counter name into a SWTrace string
465 std::vector<uint32_t> counterNameBuffer;
466 if (!StringToSwTraceString<SwTraceCharPolicy>(counterName, counterNameBuffer))
468 errorMessage = boost::str(boost::format("Cannot convert the name of counter %1% (name: %2%) "
469 "to an SWTrace string")
475 // Event record word 6:
476 // 0:31 [32] description_offset: offset from the beginning of the event record pool to the description field
477 // The size of the name buffer in bytes
478 uint32_t eventRecordWord6 = numeric_cast<uint32_t>(counterNameBuffer.size() * uint32_t_size);
480 // Convert the counter description into a SWTrace string
481 std::vector<uint32_t> counterDescriptionBuffer;
482 if (!StringToSwTraceString<SwTraceCharPolicy>(counterDescription, counterDescriptionBuffer))
484 errorMessage = boost::str(boost::format("Cannot convert the description of counter %1% (description: %2%) "
485 "to an SWTrace string")
491 // Event record word 7:
492 // 0:31 [32] units_offset: (optional) offset from the beginning of the event record pool to the units field.
493 // An offset value of zero indicates this field is not provided
494 bool includeUnits = !counterUnits.empty();
495 // The size of the description buffer in bytes
496 uint32_t eventRecordWord7 = includeUnits ?
498 numeric_cast<uint32_t>(counterDescriptionBuffer.size() * uint32_t_size) :
501 // Convert the counter units into a SWTrace namestring (optional)
502 std::vector<uint32_t> counterUnitsBuffer;
505 // Convert the counter units into a SWTrace namestring
506 if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterUnits, counterUnitsBuffer))
508 errorMessage = boost::str(boost::format("Cannot convert the units of counter %1% (units: %2%) "
509 "to an SWTrace string")
516 // Calculate the size in words of the event record
517 size_t eventRecordSize = 8u + // The size of the fixed part (counter_uid + max_counter_uid + device +
518 // counter_set + class + interpolation +
519 // multiplier + name_offset + description_offset +
521 counterNameBuffer.size() + // The size of the variable part (the counter name,
522 counterDescriptionBuffer.size() + // description and units including the null-terminator)
523 counterUnitsBuffer.size();
525 // Allocate the space for the event record
526 eventRecord.resize(eventRecordSize);
528 ARMNN_NO_CONVERSION_WARN_BEGIN
529 // Create the event record
530 eventRecord[0] = eventRecordWord0; // max_counter_uid + counter_uid
531 eventRecord[1] = eventRecordWord1; // device + counter_set
532 eventRecord[2] = eventRecordWord2; // class + interpolation
533 eventRecord[3] = eventRecordWord3; // multiplier
534 eventRecord[4] = eventRecordWord4; // multiplier
535 eventRecord[5] = eventRecordWord5; // name_offset
536 eventRecord[6] = eventRecordWord6; // description_offset
537 eventRecord[7] = eventRecordWord7; // units_offset
538 auto offset = eventRecord.begin() + 8u;
539 std::copy(counterNameBuffer.begin(), counterNameBuffer.end(), offset); // name
540 offset += counterNameBuffer.size();
541 std::copy(counterDescriptionBuffer.begin(), counterDescriptionBuffer.end(), offset); // description
544 offset += counterDescriptionBuffer.size();
545 std::copy(counterUnitsBuffer.begin(), counterUnitsBuffer.end(), offset); // units
547 ARMNN_NO_CONVERSION_WARN_END
552 void SendCounterPacket::SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory)
554 using namespace boost::numeric;
556 // Get the amount of data that needs to be put into the packet
557 uint16_t categoryCount = counterDirectory.GetCategoryCount();
558 uint16_t deviceCount = counterDirectory.GetDeviceCount();
559 uint16_t counterSetCount = counterDirectory.GetCounterSetCount();
562 size_t uint32_t_size = sizeof(uint32_t);
563 size_t packetHeaderSize = 2u;
564 size_t bodyHeaderSize = 6u;
566 // Initialize the offset for the pointer tables
567 uint32_t pointerTableOffset = 0;
573 // Process device records
574 std::vector<DeviceRecord> deviceRecords(deviceCount);
575 const Devices& devices = counterDirectory.GetDevices();
576 std::vector<uint32_t> deviceRecordOffsets(deviceCount, 0); // device_records_pointer_table
577 size_t deviceRecordsSize = 0;
578 size_t deviceIndex = 0;
579 size_t deviceRecordOffsetIndex = 0;
580 for (auto it = devices.begin(); it != devices.end(); it++)
582 const DevicePtr& device = it->second;
583 DeviceRecord& deviceRecord = deviceRecords.at(deviceIndex);
585 std::string errorMessage;
586 if (!CreateDeviceRecord(device, deviceRecord, errorMessage))
588 CancelOperationAndThrow<RuntimeException>(errorMessage);
591 // Update the total size in words of the device records
592 deviceRecordsSize += deviceRecord.size();
594 // Add the device record offset to the device records pointer table offset field
595 deviceRecordOffsets[deviceRecordOffsetIndex] = pointerTableOffset;
596 pointerTableOffset += numeric_cast<uint32_t>(deviceRecord.size() * uint32_t_size);
599 deviceRecordOffsetIndex++;
602 // -------------------
603 // Counter set records
604 // -------------------
606 // Process counter set records
607 std::vector<CounterSetRecord> counterSetRecords(counterSetCount);
608 const CounterSets& counterSets = counterDirectory.GetCounterSets();
609 std::vector<uint32_t> counterSetRecordOffsets(counterSetCount, 0); // counter_set_records_pointer_table
610 size_t counterSetRecordsSize = 0;
611 size_t counterSetIndex = 0;
612 size_t counterSetRecordOffsetIndex = 0;
613 for (auto it = counterSets.begin(); it != counterSets.end(); it++)
615 const CounterSetPtr& counterSet = it->second;
616 CounterSetRecord& counterSetRecord = counterSetRecords.at(counterSetIndex);
618 std::string errorMessage;
619 if (!CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage))
621 CancelOperationAndThrow<RuntimeException>(errorMessage);
624 // Update the total size in words of the counter set records
625 counterSetRecordsSize += counterSetRecord.size();
627 // Add the counter set record offset to the counter set records pointer table offset field
628 counterSetRecordOffsets[counterSetRecordOffsetIndex] = pointerTableOffset;
629 pointerTableOffset += numeric_cast<uint32_t>(counterSetRecord.size() * uint32_t_size);
632 counterSetRecordOffsetIndex++;
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;
646 for (auto it = categories.begin(); it != categories.end(); it++)
648 const CategoryPtr& category = *it;
649 CategoryRecord& categoryRecord = categoryRecords.at(categoryIndex);
651 std::string errorMessage;
652 if (!CreateCategoryRecord(category, counterDirectory.GetCounters(), categoryRecord, errorMessage))
654 CancelOperationAndThrow<RuntimeException>(errorMessage);
657 // Update the total size in words of the category records
658 categoryRecordsSize += categoryRecord.size();
660 // Add the category record offset to the category records pointer table offset field
661 categoryRecordOffsets[categoryRecordOffsetIndex] = pointerTableOffset;
662 pointerTableOffset += numeric_cast<uint32_t>(categoryRecord.size() * uint32_t_size);
665 categoryRecordOffsetIndex++;
670 // Calculate the length in words of the counter directory packet's data (excludes the packet header size)
671 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
680 // Calculate the size in words of the counter directory packet (the data length plus the packet header size)
681 size_t counterDirectoryPacketSize = packetHeaderSize + // The size of the packet header
682 counterDirectoryPacketDataLength; // The data length
685 // Allocate the necessary space for the counter directory packet
686 std::vector<uint32_t> counterDirectoryPacket(counterDirectoryPacketSize, 0);
692 // Packet header word 0:
693 // 26:31 [6] packet_family: control Packet Family
694 // 16:25 [10] packet_id: packet identifier
695 // 8:15 [8] reserved: all zeros
696 // 0:7 [8] reserved: all zeros
697 uint32_t packetFamily = 0;
698 uint32_t packetId = 2;
699 uint32_t packetHeaderWord0 = ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16);
701 // Packet header word 1:
702 // 0:31 [32] data_length: length of data, in bytes
703 uint32_t packetHeaderWord1 = numeric_cast<uint32_t>(counterDirectoryPacketDataLength * uint32_t_size);
705 // Create the packet header
706 uint32_t packetHeader[2]
708 packetHeaderWord0, // packet_family + packet_id + reserved + reserved
709 packetHeaderWord1 // data_length
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
719 uint32_t bodyHeaderWord0 = static_cast<uint32_t>(deviceCount) << 16;
721 // Body header word 1:
722 // 0:31 [32] device_records_pointer_table_offset: offset to the device_records_pointer_table
723 uint32_t bodyHeaderWord1 = 0; // The offset is always zero here, as the device record pointer table field is always
724 // the first item in the pool
726 // Body header word 2:
727 // 16:31 [16] counter_set_count: number of entries in the counter_set_pointer_table
728 // 0:15 [16] reserved: all zeros
729 uint32_t bodyHeaderWord2 = static_cast<uint32_t>(counterSetCount) << 16;
731 // Body header word 3:
732 // 0:31 [32] counter_set_pointer_table_offset: offset to the counter_set_pointer_table
733 uint32_t bodyHeaderWord3 =
734 numeric_cast<uint32_t>(deviceRecordOffsets.size() * uint32_t_size); // The size of the device records
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
741 uint32_t bodyHeaderWord4 = static_cast<uint32_t>(categoryCount) << 16;
743 // Body header word 3:
744 // 0:31 [32] categories_pointer_table_offset: offset to the categories_pointer_table
745 uint32_t bodyHeaderWord5 =
746 numeric_cast<uint32_t>(deviceRecordOffsets.size() * uint32_t_size + // The size of the device records
747 counterSetRecordOffsets.size() * uint32_t_size); // pointer table, plus the size of
748 // the counter set pointer table
750 // Create the body header
751 uint32_t bodyHeader[6]
753 bodyHeaderWord0, // device_records_count + reserved
754 bodyHeaderWord1, // device_records_pointer_table_offset
755 bodyHeaderWord2, // counter_set_count + reserved
756 bodyHeaderWord3, // counter_set_pointer_table_offset
757 bodyHeaderWord4, // categories_count + reserved
758 bodyHeaderWord5 // categories_pointer_table_offset
761 ARMNN_NO_CONVERSION_WARN_BEGIN
762 // Create the counter directory packet
763 auto counterDirectoryPacketOffset = counterDirectoryPacket.begin();
765 std::copy(packetHeader, packetHeader + packetHeaderSize, counterDirectoryPacketOffset);
766 counterDirectoryPacketOffset += packetHeaderSize;
768 std::copy(bodyHeader, bodyHeader + bodyHeaderSize, counterDirectoryPacketOffset);
769 counterDirectoryPacketOffset += bodyHeaderSize;
770 // device_records_pointer_table
771 std::copy(deviceRecordOffsets.begin(), deviceRecordOffsets.end(), counterDirectoryPacketOffset);
772 counterDirectoryPacketOffset += deviceRecordOffsets.size();
773 // counter_set_pointer_table
774 std::copy(counterSetRecordOffsets.begin(), counterSetRecordOffsets.end(), counterDirectoryPacketOffset);
775 counterDirectoryPacketOffset += counterSetRecordOffsets.size();
776 // category_pointer_table
777 std::copy(categoryRecordOffsets.begin(), categoryRecordOffsets.end(), counterDirectoryPacketOffset);
778 counterDirectoryPacketOffset += categoryRecordOffsets.size();
780 for (const DeviceRecord& deviceRecord : deviceRecords)
782 std::copy(deviceRecord.begin(), deviceRecord.end(), counterDirectoryPacketOffset); // device_record
783 counterDirectoryPacketOffset += deviceRecord.size();
785 // counter_set_records
786 for (const CounterSetRecord& counterSetRecord : counterSetRecords)
788 std::copy(counterSetRecord.begin(), counterSetRecord.end(), counterDirectoryPacketOffset); // counter_set_record
789 counterDirectoryPacketOffset += counterSetRecord.size();
792 for (const CategoryRecord& categoryRecord : categoryRecords)
794 std::copy(categoryRecord.begin(), categoryRecord.end(), counterDirectoryPacketOffset); // category_record
795 counterDirectoryPacketOffset += categoryRecord.size();
797 ARMNN_NO_CONVERSION_WARN_END
799 // Calculate the total size in bytes of the counter directory packet
800 uint32_t totalSize = numeric_cast<uint32_t>(counterDirectoryPacketSize * uint32_t_size);
802 // Reserve space in the buffer for the packet
803 uint32_t reserved = 0;
804 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
806 if (writeBuffer == nullptr || reserved < totalSize)
808 CancelOperationAndThrow<BufferExhaustion>(
810 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
813 // Offset for writing to the buffer
816 // Write the counter directory packet to the buffer
817 for (uint32_t counterDirectoryPacketWord : counterDirectoryPacket)
819 WriteUint32(writeBuffer, offset, counterDirectoryPacketWord);
820 offset += numeric_cast<uint32_t>(uint32_t_size);
823 m_BufferManager.Commit(writeBuffer, totalSize);
826 void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
828 uint32_t uint16_t_size = sizeof(uint16_t);
829 uint32_t uint32_t_size = sizeof(uint32_t);
830 uint32_t uint64_t_size = sizeof(uint64_t);
832 uint32_t packetFamily = 3;
833 uint32_t packetClass = 0;
834 uint32_t packetType = 0;
835 uint32_t headerSize = 2 * uint32_t_size;
836 uint32_t bodySize = uint64_t_size + numeric_cast<uint32_t>(values.size()) * (uint16_t_size + uint32_t_size);
837 uint32_t totalSize = headerSize + bodySize;
839 uint32_t reserved = 0;
841 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
843 if (writeBuffer == nullptr || reserved < totalSize)
845 CancelOperationAndThrow<BufferExhaustion>(
847 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
851 WriteUint32(writeBuffer,
853 ((packetFamily & 0x0000003F) << 26) |
854 ((packetClass & 0x0000007F) << 19) |
855 ((packetType & 0x00000007) << 16));
856 offset += uint32_t_size;
857 WriteUint32(writeBuffer, offset, bodySize);
859 // Copy captured Timestamp.
860 offset += uint32_t_size;
861 WriteUint64(writeBuffer, offset, timestamp);
863 // Copy selectedCounterIds.
864 offset += uint64_t_size;
865 for (const auto& pair: values)
867 WriteUint16(writeBuffer, offset, pair.counterId);
868 offset += uint16_t_size;
869 WriteUint32(writeBuffer, offset, pair.counterValue);
870 offset += uint32_t_size;
873 m_BufferManager.Commit(writeBuffer, totalSize);
876 void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
877 const std::vector<uint16_t>& selectedCounterIds)
879 uint32_t uint16_t_size = sizeof(uint16_t);
880 uint32_t uint32_t_size = sizeof(uint32_t);
882 uint32_t packetFamily = 0;
883 uint32_t packetId = 4;
884 uint32_t headerSize = 2 * uint32_t_size;
885 uint32_t bodySize = uint32_t_size + numeric_cast<uint32_t>(selectedCounterIds.size()) * uint16_t_size;
886 uint32_t totalSize = headerSize + bodySize;
888 uint32_t reserved = 0;
890 IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
892 if (writeBuffer == nullptr || reserved < totalSize)
894 CancelOperationAndThrow<BufferExhaustion>(
896 boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
900 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
901 offset += uint32_t_size;
902 WriteUint32(writeBuffer, offset, bodySize);
904 // Copy capturePeriod.
905 offset += uint32_t_size;
906 WriteUint32(writeBuffer, offset, capturePeriod);
908 // Copy selectedCounterIds.
909 offset += uint32_t_size;
910 for(const uint16_t& id: selectedCounterIds)
912 WriteUint16(writeBuffer, offset, id);
913 offset += uint16_t_size;
916 m_BufferManager.Commit(writeBuffer, totalSize);
919 } // namespace profiling