IVGCVSW-4485 Remove Boost assert
[platform/upstream/armnn.git] / src / profiling / SendCounterPacket.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "SendCounterPacket.hpp"
7 #include "EncodeVersion.hpp"
8
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>
14
15 #include <boost/format.hpp>
16 #include <boost/numeric/conversion/cast.hpp>
17
18 #include <cstring>
19
20 namespace armnn
21 {
22
23 namespace profiling
24 {
25
26 using boost::numeric_cast;
27
28 const unsigned int SendCounterPacket::PIPE_MAGIC;
29
30 void SendCounterPacket::SendStreamMetaDataPacket()
31 {
32     std::string info(GetSoftwareInfo());
33     std::string hardwareVersion(GetHardwareVersion());
34     std::string softwareVersion(GetSoftwareVersion());
35     std::string processName = GetProcessName().substr(0, 60);
36
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;
44
45     uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
46
47     uint32_t headerSize = 2 * sizeUint32;
48     uint32_t bodySize = 10 * sizeUint32;
49     uint32_t packetVersionCountSize = sizeUint32;
50
51     // Supported Packets
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;
59
60     uint32_t payloadSize = numeric_cast<uint32_t>(infoSize + hardwareVersionSize + softwareVersionSize +
61                                                   processNameSize + packetVersionCountSize +
62                                                   (packetVersionEntries * 2 * sizeUint32));
63
64     uint32_t totalSize = headerSize + bodySize + payloadSize;
65     uint32_t offset = 0;
66     uint32_t reserved = 0;
67
68     IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
69
70     if (writeBuffer == nullptr || reserved < totalSize)
71     {
72         CancelOperationAndThrow<BufferExhaustion>(
73             writeBuffer,
74             boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
75     }
76
77     try
78     {
79         // Create header
80
81         WriteUint32(writeBuffer, offset, 0);
82         offset += sizeUint32;
83         WriteUint32(writeBuffer, offset, totalSize - headerSize);
84
85         // Packet body
86
87         offset += sizeUint32;
88         WriteUint32(writeBuffer, offset, PIPE_MAGIC); // pipe_magic
89         offset += sizeUint32;
90         WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0)); // stream_metadata_version
91         offset += sizeUint32;
92         WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length
93         offset += sizeUint32;
94         int pid = armnnUtils::Processes::GetCurrentId();
95         WriteUint32(writeBuffer, offset, numeric_cast<uint32_t>(pid)); // pid
96         offset += sizeUint32;
97         uint32_t poolOffset = bodySize;
98         WriteUint32(writeBuffer, offset, infoSize ? poolOffset : 0); // offset_info
99         offset += sizeUint32;
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;
114
115         // Pool
116
117         if (infoSize)
118         {
119             memcpy(&writeBuffer->GetWritableData()[offset], info.c_str(), infoSize);
120             offset += infoSize;
121         }
122
123         if (hardwareVersionSize)
124         {
125             memcpy(&writeBuffer->GetWritableData()[offset], hardwareVersion.c_str(), hardwareVersionSize);
126             offset += hardwareVersionSize;
127         }
128
129         if (softwareVersionSize)
130         {
131             memcpy(&writeBuffer->GetWritableData()[offset], softwareVersion.c_str(), softwareVersionSize);
132             offset += softwareVersionSize;
133         }
134
135         if (processNameSize)
136         {
137             memcpy(&writeBuffer->GetWritableData()[offset], processName.c_str(), processNameSize);
138             offset += processNameSize;
139         }
140
141         if (packetVersionEntries)
142         {
143             // Packet Version Count
144             WriteUint32(writeBuffer, offset, packetVersionEntries << 16);
145
146             // Packet Version Entries
147             uint32_t packetFamily = 0;
148             uint32_t packetId = 0;
149
150             offset += sizeUint32;
151             for (uint32_t i = 0; i < packetVersionEntries - 1; ++i)
152             {
153                 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId++ & 0x3FF) << 16));
154                 offset += sizeUint32;
155                 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
156                 offset += sizeUint32;
157             }
158
159             packetFamily = 1;
160             packetId = 0;
161
162             WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
163             offset += sizeUint32;
164             WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
165         }
166     }
167     catch(...)
168     {
169         CancelOperationAndThrow<RuntimeException>(writeBuffer, "Error processing packet.");
170     }
171
172     m_BufferManager.Commit(writeBuffer, totalSize, false);
173 }
174
175 bool SendCounterPacket::CreateCategoryRecord(const CategoryPtr& category,
176                                              const Counters& counters,
177                                              CategoryRecord& categoryRecord,
178                                              std::string& errorMessage)
179 {
180     using namespace boost::numeric;
181
182     ARMNN_ASSERT(category);
183
184     const std::string& categoryName = category->m_Name;
185     ARMNN_ASSERT(!categoryName.empty());
186
187     // Remove any duplicate counters
188     std::vector<uint16_t> categoryCounters;
189     for (size_t counterIndex = 0; counterIndex < category->m_Counters.size(); ++counterIndex)
190     {
191         uint16_t counterUid = category->m_Counters.at(counterIndex);
192         auto it = counters.find(counterUid);
193         if (it == counters.end())
194         {
195             errorMessage = boost::str(boost::format("Counter (%1%) not found in category (%2%)")
196                                       % counterUid % category->m_Name );
197             return false;
198         }
199
200         const CounterPtr& counter = it->second;
201
202         if (counterUid == counter->m_MaxCounterUid)
203         {
204             categoryCounters.emplace_back(counterUid);
205         }
206     }
207     if (categoryCounters.empty())
208     {
209         errorMessage = boost::str(boost::format("No valid counters found in category (%1%)")% categoryName);
210         return false;
211     }
212
213     // Utils
214     size_t uint32_t_size = sizeof(uint32_t);
215
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;
220
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
226
227     // Convert the device name into a SWTrace namestring
228     std::vector<uint32_t> categoryNameBuffer;
229     if (!StringToSwTraceString<SwTraceNameCharPolicy>(categoryName, categoryNameBuffer))
230     {
231         errorMessage = boost::str(boost::format("Cannot convert the name of category (%1%) to an SWTrace namestring")
232                                   % categoryName);
233         return false;
234     }
235
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++)
246     {
247         uint16_t counterUid = categoryCounters.at(counterIndex);
248         auto it = counters.find(counterUid);
249         const CounterPtr& counter = it->second;
250
251         EventRecord& eventRecord = eventRecords.at(eventRecordIndex);
252         if (!CreateEventRecord(counter, eventRecord, errorMessage))
253         {
254             return false;
255         }
256
257         // Update the total size in words of the event records
258         eventRecordsSize += eventRecord.size();
259
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);
263     }
264
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);
268
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)
275
276     // Allocate the necessary space for the category record
277     categoryRecord.resize(categoryRecordSize);
278
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)
290     {
291         std::copy(eventRecord.begin(), eventRecord.end(), offset); // event_record
292         offset += eventRecord.size();
293     }
294     ARMNN_NO_CONVERSION_WARN_END
295
296     return true;
297 }
298
299 bool SendCounterPacket::CreateDeviceRecord(const DevicePtr& device,
300                                            DeviceRecord& deviceRecord,
301                                            std::string& errorMessage)
302 {
303     ARMNN_ASSERT(device);
304
305     uint16_t deviceUid = device->m_Uid;
306     const std::string& deviceName = device->m_Name;
307     uint16_t deviceCores = device->m_Cores;
308
309     ARMNN_ASSERT(!deviceName.empty());
310
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));
316
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
321
322     // Convert the device name into a SWTrace string
323     std::vector<uint32_t> deviceNameBuffer;
324     if (!StringToSwTraceString<SwTraceCharPolicy>(deviceName, deviceNameBuffer))
325     {
326         errorMessage = boost::str(boost::format("Cannot convert the name of device %1% (%2%) to an SWTrace string")
327                                   % deviceUid
328                                   % deviceName);
329         return false;
330     }
331
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)
336
337     // Allocate the necessary space for the device record
338     deviceRecord.resize(deviceRecordSize);
339
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
345
346     return true;
347 }
348
349 bool SendCounterPacket::CreateCounterSetRecord(const CounterSetPtr& counterSet,
350                                                CounterSetRecord& counterSetRecord,
351                                                std::string& errorMessage)
352 {
353     ARMNN_ASSERT(counterSet);
354
355     uint16_t counterSetUid = counterSet->m_Uid;
356     const std::string& counterSetName = counterSet->m_Name;
357     uint16_t counterSetCount = counterSet->m_Count;
358
359     ARMNN_ASSERT(!counterSetName.empty());
360
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));
366
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
371
372     // Convert the device name into a SWTrace namestring
373     std::vector<uint32_t> counterSetNameBuffer;
374     if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterSet->m_Name, counterSetNameBuffer))
375     {
376         errorMessage = boost::str(boost::format("Cannot convert the name of counter set %1% (%2%) to "
377                                                 "an SWTrace namestring")
378                                   % counterSetUid
379                                   % counterSetName);
380         return false;
381     }
382
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)
387
388     // Allocate the space for the counter set record
389     counterSetRecord.resize(counterSetRecordSize);
390
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
396
397     return true;
398 }
399
400 bool SendCounterPacket::CreateEventRecord(const CounterPtr& counter,
401                                           EventRecord& eventRecord,
402                                           std::string& errorMessage)
403 {
404     using namespace boost::numeric;
405
406     ARMNN_ASSERT(counter);
407
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;
418
419     ARMNN_ASSERT(counterClass == 0 || counterClass == 1);
420     ARMNN_ASSERT(counterInterpolation == 0 || counterInterpolation == 1);
421     ARMNN_ASSERT(counterMultiplier);
422
423     // Utils
424     size_t uint32_t_size = sizeof(uint32_t);
425
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));
435
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));
443
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));
449
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];
458
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
463
464     // Convert the counter name into a SWTrace string
465     std::vector<uint32_t> counterNameBuffer;
466     if (!StringToSwTraceString<SwTraceCharPolicy>(counterName, counterNameBuffer))
467     {
468         errorMessage = boost::str(boost::format("Cannot convert the name of counter %1% (name: %2%) "
469                                                 "to an SWTrace string")
470                                   % counterUid
471                                   % counterName);
472         return false;
473     }
474
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);
479
480     // Convert the counter description into a SWTrace string
481     std::vector<uint32_t> counterDescriptionBuffer;
482     if (!StringToSwTraceString<SwTraceCharPolicy>(counterDescription, counterDescriptionBuffer))
483     {
484         errorMessage = boost::str(boost::format("Cannot convert the description of counter %1% (description: %2%) "
485                                                 "to an SWTrace string")
486                                   % counterUid
487                                   % counterName);
488         return false;
489     }
490
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 ?
497                                 eventRecordWord6 +
498                                 numeric_cast<uint32_t>(counterDescriptionBuffer.size() * uint32_t_size) :
499                                 0;
500
501     // Convert the counter units into a SWTrace namestring (optional)
502     std::vector<uint32_t> counterUnitsBuffer;
503     if (includeUnits)
504     {
505         // Convert the counter units into a SWTrace namestring
506         if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterUnits, counterUnitsBuffer))
507         {
508             errorMessage = boost::str(boost::format("Cannot convert the units of counter %1% (units: %2%) "
509                                                     "to an SWTrace string")
510                                       % counterUid
511                                       % counterName);
512             return false;
513         }
514     }
515
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 +
520                                   //                             units_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();
524
525     // Allocate the space for the event record
526     eventRecord.resize(eventRecordSize);
527
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
542     if (includeUnits)
543     {
544         offset += counterDescriptionBuffer.size();
545         std::copy(counterUnitsBuffer.begin(), counterUnitsBuffer.end(), offset); // units
546     }
547     ARMNN_NO_CONVERSION_WARN_END
548
549     return true;
550 }
551
552 void SendCounterPacket::SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory)
553 {
554     using namespace boost::numeric;
555
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();
560
561     // Utils
562     size_t uint32_t_size = sizeof(uint32_t);
563     size_t packetHeaderSize = 2u;
564     size_t bodyHeaderSize = 6u;
565
566     // Initialize the offset for the pointer tables
567     uint32_t pointerTableOffset = 0;
568
569     // --------------
570     // Device records
571     // --------------
572
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++)
581     {
582         const DevicePtr& device = it->second;
583         DeviceRecord& deviceRecord = deviceRecords.at(deviceIndex);
584
585         std::string errorMessage;
586         if (!CreateDeviceRecord(device, deviceRecord, errorMessage))
587         {
588             CancelOperationAndThrow<RuntimeException>(errorMessage);
589         }
590
591         // Update the total size in words of the device records
592         deviceRecordsSize += deviceRecord.size();
593
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);
597
598         deviceIndex++;
599         deviceRecordOffsetIndex++;
600     }
601
602     // -------------------
603     // Counter set records
604     // -------------------
605
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++)
614     {
615         const CounterSetPtr& counterSet = it->second;
616         CounterSetRecord& counterSetRecord = counterSetRecords.at(counterSetIndex);
617
618         std::string errorMessage;
619         if (!CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage))
620         {
621             CancelOperationAndThrow<RuntimeException>(errorMessage);
622         }
623
624         // Update the total size in words of the counter set records
625         counterSetRecordsSize += counterSetRecord.size();
626
627         // Add the counter set record offset to the counter set records pointer table offset field
628         counterSetRecordOffsets[counterSetRecordOffsetIndex] = pointerTableOffset;
629         pointerTableOffset += numeric_cast<uint32_t>(counterSetRecord.size() * uint32_t_size);
630
631         counterSetIndex++;
632         counterSetRecordOffsetIndex++;
633     }
634
635     // ----------------
636     // Category records
637     // ----------------
638
639     // Process category records
640     std::vector<CategoryRecord> categoryRecords(categoryCount);
641     const Categories& categories = counterDirectory.GetCategories();
642     std::vector<uint32_t> categoryRecordOffsets(categoryCount, 0); // category_records_pointer_table
643     size_t categoryRecordsSize = 0;
644     size_t categoryIndex = 0;
645     size_t categoryRecordOffsetIndex = 0;
646     for (auto it = categories.begin(); it != categories.end(); it++)
647     {
648         const CategoryPtr& category = *it;
649         CategoryRecord& categoryRecord = categoryRecords.at(categoryIndex);
650
651         std::string errorMessage;
652         if (!CreateCategoryRecord(category, counterDirectory.GetCounters(), categoryRecord, errorMessage))
653         {
654             CancelOperationAndThrow<RuntimeException>(errorMessage);
655         }
656
657         // Update the total size in words of the category records
658         categoryRecordsSize += categoryRecord.size();
659
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);
663
664         categoryIndex++;
665         categoryRecordOffsetIndex++;
666     }
667
668
669
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
679
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
683
684
685     // Allocate the necessary space for the counter directory packet
686     std::vector<uint32_t> counterDirectoryPacket(counterDirectoryPacketSize, 0);
687
688     // -------------
689     // Packet header
690     // -------------
691
692     // Packet header word 0:
693     // 26:31 [6]  packet_family: control Packet Family
694     // 16:25 [10] packet_id: packet identifier
695     // 8:15  [8]  reserved: all zeros
696     // 0:7   [8]  reserved: all zeros
697     uint32_t packetFamily = 0;
698     uint32_t packetId = 2;
699     uint32_t packetHeaderWord0 = ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16);
700
701     // Packet header word 1:
702     // 0:31 [32] data_length: length of data, in bytes
703     uint32_t packetHeaderWord1 = numeric_cast<uint32_t>(counterDirectoryPacketDataLength * uint32_t_size);
704
705     // Create the packet header
706     uint32_t packetHeader[2]
707     {
708         packetHeaderWord0, // packet_family + packet_id + reserved + reserved
709         packetHeaderWord1  // data_length
710     };
711
712     // -----------
713     // Body header
714     // -----------
715
716     // Body header word 0:
717     // 16:31 [16] device_records_count: number of entries in the device_records_pointer_table
718     // 0:15  [16] reserved: all zeros
719     uint32_t bodyHeaderWord0 = static_cast<uint32_t>(deviceCount) << 16;
720
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
725
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;
730
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
735                                                                                 // pointer table
736
737
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;
742
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
749
750     // Create the body header
751     uint32_t bodyHeader[6]
752     {
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
759     };
760
761     ARMNN_NO_CONVERSION_WARN_BEGIN
762     // Create the counter directory packet
763     auto counterDirectoryPacketOffset = counterDirectoryPacket.begin();
764     // packet_header
765     std::copy(packetHeader, packetHeader + packetHeaderSize, counterDirectoryPacketOffset);
766     counterDirectoryPacketOffset += packetHeaderSize;
767     // body_header
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();
779     // device_records
780     for (const DeviceRecord& deviceRecord : deviceRecords)
781     {
782         std::copy(deviceRecord.begin(), deviceRecord.end(), counterDirectoryPacketOffset); // device_record
783         counterDirectoryPacketOffset += deviceRecord.size();
784     }
785     // counter_set_records
786     for (const CounterSetRecord& counterSetRecord : counterSetRecords)
787     {
788         std::copy(counterSetRecord.begin(), counterSetRecord.end(), counterDirectoryPacketOffset); // counter_set_record
789         counterDirectoryPacketOffset += counterSetRecord.size();
790     }
791     // category_records
792     for (const CategoryRecord& categoryRecord : categoryRecords)
793     {
794         std::copy(categoryRecord.begin(), categoryRecord.end(), counterDirectoryPacketOffset); // category_record
795         counterDirectoryPacketOffset += categoryRecord.size();
796     }
797     ARMNN_NO_CONVERSION_WARN_END
798
799     // Calculate the total size in bytes of the counter directory packet
800     uint32_t totalSize = numeric_cast<uint32_t>(counterDirectoryPacketSize * uint32_t_size);
801
802     // Reserve space in the buffer for the packet
803     uint32_t reserved = 0;
804     IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
805
806     if (writeBuffer == nullptr || reserved < totalSize)
807     {
808         CancelOperationAndThrow<BufferExhaustion>(
809             writeBuffer,
810             boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
811     }
812
813     // Offset for writing to the buffer
814     uint32_t offset = 0;
815
816     // Write the counter directory packet to the buffer
817     for (uint32_t counterDirectoryPacketWord : counterDirectoryPacket)
818     {
819         WriteUint32(writeBuffer, offset, counterDirectoryPacketWord);
820         offset += numeric_cast<uint32_t>(uint32_t_size);
821     }
822
823     m_BufferManager.Commit(writeBuffer, totalSize);
824 }
825
826 void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
827 {
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);
831
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;
838     uint32_t offset = 0;
839     uint32_t reserved = 0;
840
841     IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
842
843     if (writeBuffer == nullptr || reserved < totalSize)
844     {
845         CancelOperationAndThrow<BufferExhaustion>(
846             writeBuffer,
847             boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
848     }
849
850     // Create header.
851     WriteUint32(writeBuffer,
852                 offset,
853                 ((packetFamily & 0x0000003F) << 26) |
854                 ((packetClass  & 0x0000007F) << 19) |
855                 ((packetType   & 0x00000007) << 16));
856     offset += uint32_t_size;
857     WriteUint32(writeBuffer, offset, bodySize);
858
859     // Copy captured Timestamp.
860     offset += uint32_t_size;
861     WriteUint64(writeBuffer, offset, timestamp);
862
863     // Copy selectedCounterIds.
864     offset += uint64_t_size;
865     for (const auto& pair: values)
866     {
867         WriteUint16(writeBuffer, offset, pair.counterId);
868         offset += uint16_t_size;
869         WriteUint32(writeBuffer, offset, pair.counterValue);
870         offset += uint32_t_size;
871     }
872
873     m_BufferManager.Commit(writeBuffer, totalSize);
874 }
875
876 void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
877                                                            const std::vector<uint16_t>& selectedCounterIds)
878 {
879     uint32_t uint16_t_size = sizeof(uint16_t);
880     uint32_t uint32_t_size = sizeof(uint32_t);
881
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;
887     uint32_t offset = 0;
888     uint32_t reserved = 0;
889
890     IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
891
892     if (writeBuffer == nullptr || reserved < totalSize)
893     {
894         CancelOperationAndThrow<BufferExhaustion>(
895             writeBuffer,
896             boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
897     }
898
899     // Create header.
900     WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
901     offset += uint32_t_size;
902     WriteUint32(writeBuffer, offset, bodySize);
903
904     // Copy capturePeriod.
905     offset += uint32_t_size;
906     WriteUint32(writeBuffer, offset, capturePeriod);
907
908     // Copy selectedCounterIds.
909     offset += uint32_t_size;
910     for(const uint16_t& id: selectedCounterIds)
911     {
912         WriteUint16(writeBuffer, offset, id);
913         offset += uint16_t_size;
914     }
915
916     m_BufferManager.Commit(writeBuffer, totalSize);
917 }
918
919 } // namespace profiling
920
921 } // namespace armnn