IVGCVSW-4454 Remove the CounterSet and Device fields from Category
[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
13 #include <boost/format.hpp>
14 #include <boost/numeric/conversion/cast.hpp>
15 #include <boost/core/ignore_unused.hpp>
16
17 #include <cstring>
18
19 namespace armnn
20 {
21
22 namespace profiling
23 {
24
25 using boost::numeric_cast;
26
27 const unsigned int SendCounterPacket::PIPE_MAGIC;
28
29 void SendCounterPacket::SendStreamMetaDataPacket()
30 {
31     std::string info(GetSoftwareInfo());
32     std::string hardwareVersion(GetHardwareVersion());
33     std::string softwareVersion(GetSoftwareVersion());
34     std::string processName = GetProcessName().substr(0, 60);
35
36     uint32_t infoSize = numeric_cast<uint32_t>(info.size()) > 0 ? numeric_cast<uint32_t>(info.size()) + 1 : 0;
37     uint32_t hardwareVersionSize = numeric_cast<uint32_t>(hardwareVersion.size()) > 0 ?
38                                    numeric_cast<uint32_t>(hardwareVersion.size()) + 1 : 0;
39     uint32_t softwareVersionSize = numeric_cast<uint32_t>(softwareVersion.size()) > 0 ?
40                                    numeric_cast<uint32_t>(softwareVersion.size()) + 1 : 0;
41     uint32_t processNameSize = numeric_cast<uint32_t>(processName.size()) > 0 ?
42                                numeric_cast<uint32_t>(processName.size()) + 1 : 0;
43
44     uint32_t sizeUint32 = numeric_cast<uint32_t>(sizeof(uint32_t));
45
46     uint32_t headerSize = 2 * sizeUint32;
47     uint32_t bodySize = 10 * sizeUint32;
48     uint32_t packetVersionCountSize = sizeUint32;
49
50     // Supported Packets
51     // Stream metadata packet            (packet family=0; packet id=0)
52     // Connection Acknowledged packet    (packet family=0, packet id=1)
53     // Counter Directory packet          (packet family=0; packet id=2)
54     // Request Counter Directory packet  (packet family=0, packet id=3)
55     // Periodic Counter Selection packet (packet family=0, packet id=4)
56     // Periodic Counter Capture packet   (packet family=1, packet class=0, type=0)
57     uint32_t packetVersionEntries = 6;
58
59     uint32_t payloadSize = numeric_cast<uint32_t>(infoSize + hardwareVersionSize + softwareVersionSize +
60                                                   processNameSize + packetVersionCountSize +
61                                                   (packetVersionEntries * 2 * sizeUint32));
62
63     uint32_t totalSize = headerSize + bodySize + payloadSize;
64     uint32_t offset = 0;
65     uint32_t reserved = 0;
66
67     IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
68
69     if (writeBuffer == nullptr || reserved < totalSize)
70     {
71         CancelOperationAndThrow<BufferExhaustion>(
72             writeBuffer,
73             boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
74     }
75
76     try
77     {
78         // Create header
79
80         WriteUint32(writeBuffer, offset, 0);
81         offset += sizeUint32;
82         WriteUint32(writeBuffer, offset, totalSize - headerSize);
83
84         // Packet body
85
86         offset += sizeUint32;
87         WriteUint32(writeBuffer, offset, PIPE_MAGIC); // pipe_magic
88         offset += sizeUint32;
89         WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0)); // stream_metadata_version
90         offset += sizeUint32;
91         WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length
92         offset += sizeUint32;
93         int pid = armnnUtils::Processes::GetCurrentId();
94         WriteUint32(writeBuffer, offset, numeric_cast<uint32_t>(pid)); // pid
95         offset += sizeUint32;
96         uint32_t poolOffset = bodySize;
97         WriteUint32(writeBuffer, offset, infoSize ? poolOffset : 0); // offset_info
98         offset += sizeUint32;
99         poolOffset += infoSize;
100         WriteUint32(writeBuffer, offset, hardwareVersionSize ? poolOffset : 0); // offset_hw_version
101         offset += sizeUint32;
102         poolOffset += hardwareVersionSize;
103         WriteUint32(writeBuffer, offset, softwareVersionSize ? poolOffset : 0); // offset_sw_version
104         offset += sizeUint32;
105         poolOffset += softwareVersionSize;
106         WriteUint32(writeBuffer, offset, processNameSize ? poolOffset : 0); // offset_process_name
107         offset += sizeUint32;
108         poolOffset += processNameSize;
109         WriteUint32(writeBuffer, offset, packetVersionEntries ? poolOffset : 0); // offset_packet_version_table
110         offset += sizeUint32;
111         WriteUint32(writeBuffer, offset, 0); // reserved
112         offset += sizeUint32;
113
114         // Pool
115
116         if (infoSize)
117         {
118             memcpy(&writeBuffer->GetWritableData()[offset], info.c_str(), infoSize);
119             offset += infoSize;
120         }
121
122         if (hardwareVersionSize)
123         {
124             memcpy(&writeBuffer->GetWritableData()[offset], hardwareVersion.c_str(), hardwareVersionSize);
125             offset += hardwareVersionSize;
126         }
127
128         if (softwareVersionSize)
129         {
130             memcpy(&writeBuffer->GetWritableData()[offset], softwareVersion.c_str(), softwareVersionSize);
131             offset += softwareVersionSize;
132         }
133
134         if (processNameSize)
135         {
136             memcpy(&writeBuffer->GetWritableData()[offset], processName.c_str(), processNameSize);
137             offset += processNameSize;
138         }
139
140         if (packetVersionEntries)
141         {
142             // Packet Version Count
143             WriteUint32(writeBuffer, offset, packetVersionEntries << 16);
144
145             // Packet Version Entries
146             uint32_t packetFamily = 0;
147             uint32_t packetId = 0;
148
149             offset += sizeUint32;
150             for (uint32_t i = 0; i < packetVersionEntries - 1; ++i)
151             {
152                 WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId++ & 0x3FF) << 16));
153                 offset += sizeUint32;
154                 WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
155                 offset += sizeUint32;
156             }
157
158             packetFamily = 1;
159             packetId = 0;
160
161             WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
162             offset += sizeUint32;
163             WriteUint32(writeBuffer, offset, EncodeVersion(1, 0, 0));
164         }
165     }
166     catch(...)
167     {
168         CancelOperationAndThrow<RuntimeException>(writeBuffer, "Error processing packet.");
169     }
170
171     m_BufferManager.Commit(writeBuffer, totalSize, false);
172 }
173
174 bool SendCounterPacket::CreateCategoryRecord(const CategoryPtr& category,
175                                              const Counters& counters,
176                                              CategoryRecord& categoryRecord,
177                                              std::string& errorMessage)
178 {
179     using namespace boost::numeric;
180
181     BOOST_ASSERT(category);
182
183     const std::string& categoryName = category->m_Name;
184     const std::vector<uint16_t> categoryCounters = category->m_Counters;
185
186     BOOST_ASSERT(!categoryName.empty());
187
188     // Utils
189     size_t uint32_t_size = sizeof(uint32_t);
190
191     // Category record word 1:
192     // 16:31 [16] event_count: number of events belonging to this category
193     // 0:15  [16] reserved: all zeros
194     uint32_t categoryRecordWord1 = static_cast<uint32_t>(categoryCounters.size()) << 16;
195
196     // Category record word 2:
197     // 0:31 [32] event_pointer_table_offset: offset from the beginning of the category data pool to
198     //                                       the event_pointer_table
199     uint32_t categoryRecordWord2 = 0; // The offset is always zero here, as the event pointer table field is always
200                                       // the first item in the pool
201
202     // Convert the device name into a SWTrace namestring
203     std::vector<uint32_t> categoryNameBuffer;
204     if (!StringToSwTraceString<SwTraceNameCharPolicy>(categoryName, categoryNameBuffer))
205     {
206         errorMessage = boost::str(boost::format("Cannot convert the name of category \"%1%\" to an SWTrace namestring")
207                                   % categoryName);
208         return false;
209     }
210
211     // Process the event records
212     size_t counterCount = categoryCounters.size();
213     std::vector<EventRecord> eventRecords(counterCount);
214     std::vector<uint32_t> eventRecordOffsets(counterCount, 0);
215     size_t eventRecordsSize = 0;
216     uint32_t eventRecordsOffset =
217             numeric_cast<uint32_t>((eventRecords.size() + categoryNameBuffer.size()) * uint32_t_size);
218     for (size_t counterIndex = 0, eventRecordIndex = 0, eventRecordOffsetIndex = 0;
219          counterIndex < counterCount;
220          counterIndex++, eventRecordIndex++, eventRecordOffsetIndex++)
221     {
222         uint16_t counterUid = categoryCounters.at(counterIndex);
223         auto it = counters.find(counterUid);
224         BOOST_ASSERT(it != counters.end());
225         const CounterPtr& counter = it->second;
226
227         EventRecord& eventRecord = eventRecords.at(eventRecordIndex);
228         if (!CreateEventRecord(counter, eventRecord, errorMessage))
229         {
230             return false;
231         }
232
233         // Update the total size in words of the event records
234         eventRecordsSize += eventRecord.size();
235
236         // Add the event record offset to the event pointer table offset field
237         eventRecordOffsets[eventRecordOffsetIndex] = eventRecordsOffset;
238         eventRecordsOffset += numeric_cast<uint32_t>(eventRecord.size() * uint32_t_size);
239     }
240
241     // Category record word 3:
242     // 0:31 [32] name_offset (offset from the beginning of the category data pool to the name field)
243     uint32_t categoryRecordWord3 = numeric_cast<uint32_t>(eventRecordOffsets.size() * uint32_t_size);
244
245     // Calculate the size in words of the category record
246     size_t categoryRecordSize = 3u + // The size of the fixed part (device + counter_set + event_count + reserved +
247                                      // event_pointer_table_offset + name_offset)
248                                 eventRecordOffsets.size() + // The size of the variable part (the event pointer table +
249                                 categoryNameBuffer.size() + // and the category name including the null-terminator +
250                                 eventRecordsSize;           // the event records)
251
252     // Allocate the necessary space for the category record
253     categoryRecord.resize(categoryRecordSize);
254
255     ARMNN_NO_CONVERSION_WARN_BEGIN
256     // Create the category record
257     categoryRecord[0] = categoryRecordWord1; // event_count + reserved
258     categoryRecord[1] = categoryRecordWord2; // event_pointer_table_offset
259     categoryRecord[2] = categoryRecordWord3; // name_offset
260     auto offset = categoryRecord.begin() + 3u;
261     std::copy(eventRecordOffsets.begin(), eventRecordOffsets.end(), offset); // event_pointer_table
262     offset += eventRecordOffsets.size();
263     std::copy(categoryNameBuffer.begin(), categoryNameBuffer.end(), offset); // name
264     offset += categoryNameBuffer.size();
265     for (const EventRecord& eventRecord : eventRecords)
266     {
267         std::copy(eventRecord.begin(), eventRecord.end(), offset); // event_record
268         offset += eventRecord.size();
269     }
270     ARMNN_NO_CONVERSION_WARN_END
271
272     return true;
273 }
274
275 bool SendCounterPacket::CreateDeviceRecord(const DevicePtr& device,
276                                            DeviceRecord& deviceRecord,
277                                            std::string& errorMessage)
278 {
279     BOOST_ASSERT(device);
280
281     uint16_t deviceUid = device->m_Uid;
282     const std::string& deviceName = device->m_Name;
283     uint16_t deviceCores = device->m_Cores;
284
285     BOOST_ASSERT(!deviceName.empty());
286
287     // Device record word 0:
288     // 16:31 [16] uid: the unique identifier for the device
289     // 0:15  [16] cores: the number of individual streams of counters for one or more cores of some device
290     uint32_t deviceRecordWord0 = (static_cast<uint32_t>(deviceUid) << 16) |
291                                  (static_cast<uint32_t>(deviceCores));
292
293     // Device record word 1:
294     // 0:31 [32] name_offset: offset from the beginning of the device record pool to the name field
295     uint32_t deviceRecordWord1 = 0; // The offset is always zero here, as the name field is always
296                                     // the first (and only) item in the pool
297
298     // Convert the device name into a SWTrace string
299     std::vector<uint32_t> deviceNameBuffer;
300     if (!StringToSwTraceString<SwTraceCharPolicy>(deviceName, deviceNameBuffer))
301     {
302         errorMessage = boost::str(boost::format("Cannot convert the name of device %1% (\"%2%\") to an SWTrace string")
303                                   % deviceUid
304                                   % deviceName);
305         return false;
306     }
307
308     // Calculate the size in words of the device record
309     size_t deviceRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
310                               deviceNameBuffer.size(); // The size of the variable part (the device name including
311                                                        // the null-terminator)
312
313     // Allocate the necessary space for the device record
314     deviceRecord.resize(deviceRecordSize);
315
316     // Create the device record
317     deviceRecord[0] = deviceRecordWord0; // uid + core
318     deviceRecord[1] = deviceRecordWord1; // name_offset
319     auto offset = deviceRecord.begin() + 2u;
320     std::copy(deviceNameBuffer.begin(), deviceNameBuffer.end(), offset); // name
321
322     return true;
323 }
324
325 bool SendCounterPacket::CreateCounterSetRecord(const CounterSetPtr& counterSet,
326                                                CounterSetRecord& counterSetRecord,
327                                                std::string& errorMessage)
328 {
329     BOOST_ASSERT(counterSet);
330
331     uint16_t counterSetUid = counterSet->m_Uid;
332     const std::string& counterSetName = counterSet->m_Name;
333     uint16_t counterSetCount = counterSet->m_Count;
334
335     BOOST_ASSERT(!counterSetName.empty());
336
337     // Counter set record word 0:
338     // 16:31 [16] uid: the unique identifier for the counter_set
339     // 0:15  [16] count: the number of counters which can be active in this set at any one time
340     uint32_t counterSetRecordWord0 = (static_cast<uint32_t>(counterSetUid) << 16) |
341                                      (static_cast<uint32_t>(counterSetCount));
342
343     // Counter set record word 1:
344     // 0:31 [32] name_offset: offset from the beginning of the counter set pool to the name field
345     uint32_t counterSetRecordWord1 = 0; // The offset is always zero here, as the name field is always
346                                         // the first (and only) item in the pool
347
348     // Convert the device name into a SWTrace namestring
349     std::vector<uint32_t> counterSetNameBuffer;
350     if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterSet->m_Name, counterSetNameBuffer))
351     {
352         errorMessage = boost::str(boost::format("Cannot convert the name of counter set %1% (\"%2%\") to "
353                                                 "an SWTrace namestring")
354                                   % counterSetUid
355                                   % counterSetName);
356         return false;
357     }
358
359     // Calculate the size in words of the counter set record
360     size_t counterSetRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset)
361                                   counterSetNameBuffer.size(); // The size of the variable part (the counter set name
362                                                                // including the null-terminator)
363
364     // Allocate the space for the counter set record
365     counterSetRecord.resize(counterSetRecordSize);
366
367     // Create the counter set record
368     counterSetRecord[0] = counterSetRecordWord0; // uid + core
369     counterSetRecord[1] = counterSetRecordWord1; // name_offset
370     auto offset = counterSetRecord.begin() + 2u;
371     std::copy(counterSetNameBuffer.begin(), counterSetNameBuffer.end(), offset); // name
372
373     return true;
374 }
375
376 bool SendCounterPacket::CreateEventRecord(const CounterPtr& counter,
377                                           EventRecord& eventRecord,
378                                           std::string& errorMessage)
379 {
380     using namespace boost::numeric;
381
382     BOOST_ASSERT(counter);
383
384     uint16_t           counterUid           = counter->m_Uid;
385     uint16_t           maxCounterUid        = counter->m_MaxCounterUid;
386     uint16_t           deviceUid            = counter->m_DeviceUid;
387     uint16_t           counterSetUid        = counter->m_CounterSetUid;
388     uint16_t           counterClass         = counter->m_Class;
389     uint16_t           counterInterpolation = counter->m_Interpolation;
390     double             counterMultiplier    = counter->m_Multiplier;
391     const std::string& counterName          = counter->m_Name;
392     const std::string& counterDescription   = counter->m_Description;
393     const std::string& counterUnits         = counter->m_Units;
394
395     BOOST_ASSERT(counterClass == 0 || counterClass == 1);
396     BOOST_ASSERT(counterInterpolation == 0 || counterInterpolation == 1);
397     BOOST_ASSERT(counterMultiplier);
398
399     // Utils
400     size_t uint32_t_size = sizeof(uint32_t);
401
402     // Event record word 0:
403     // 16:31 [16] max_counter_uid: if the device this event is associated with has more than one core and there
404     //                             is one of these counters per core this value will be set to
405     //                             (counter_uid + cores (from device_record)) - 1.
406     //                             If there is only a single core then this value will be the same as
407     //                             the counter_uid value
408     // 0:15  [16] count_uid: unique ID for the counter. Must be unique across all counters in all categories
409     uint32_t eventRecordWord0 = (static_cast<uint32_t>(maxCounterUid) << 16) |
410                                 (static_cast<uint32_t>(counterUid));
411
412     // Event record word 1:
413     // 16:31 [16] device: UID of the device this event is associated with. Set to zero if the event is NOT
414     //                    associated with a device
415     // 0:15  [16] counter_set: UID of the counter_set this event is associated with. Set to zero if the event
416     //                         is NOT associated with a counter_set
417     uint32_t eventRecordWord1 = (static_cast<uint32_t>(deviceUid) << 16) |
418                                 (static_cast<uint32_t>(counterSetUid));
419
420     // Event record word 2:
421     // 16:31 [16] class: type describing how to treat each data point in a stream of data points
422     // 0:15  [16] interpolation: type describing how to interpolate each data point in a stream of data points
423     uint32_t eventRecordWord2 = (static_cast<uint32_t>(counterClass) << 16) |
424                                 (static_cast<uint32_t>(counterInterpolation));
425
426     // Event record word 3-4:
427     // 0:63 [64] multiplier: internal data stream is represented as integer values, this allows scaling of
428     //                       those values as if they are fixed point numbers. Zero is not a valid value
429     uint32_t multiplier[2] = { 0u, 0u };
430     BOOST_ASSERT(sizeof(counterMultiplier) == sizeof(multiplier));
431     std::memcpy(multiplier, &counterMultiplier, sizeof(multiplier));
432     uint32_t eventRecordWord3 = multiplier[0];
433     uint32_t eventRecordWord4 = multiplier[1];
434
435     // Event record word 5:
436     // 0:31 [32] name_offset: offset from the beginning of the event record pool to the name field
437     uint32_t eventRecordWord5 = 0; // The offset is always zero here, as the name field is always
438                                    // the first item in the pool
439
440     // Convert the counter name into a SWTrace string
441     std::vector<uint32_t> counterNameBuffer;
442     if (!StringToSwTraceString<SwTraceCharPolicy>(counterName, counterNameBuffer))
443     {
444         errorMessage = boost::str(boost::format("Cannot convert the name of counter %1% (name: \"%2%\") "
445                                                 "to an SWTrace string")
446                                   % counterUid
447                                   % counterName);
448         return false;
449     }
450
451     // Event record word 6:
452     // 0:31 [32] description_offset: offset from the beginning of the event record pool to the description field
453     // The size of the name buffer in bytes
454     uint32_t eventRecordWord6 = numeric_cast<uint32_t>(counterNameBuffer.size() * uint32_t_size);
455
456     // Convert the counter description into a SWTrace string
457     std::vector<uint32_t> counterDescriptionBuffer;
458     if (!StringToSwTraceString<SwTraceCharPolicy>(counterDescription, counterDescriptionBuffer))
459     {
460         errorMessage = boost::str(boost::format("Cannot convert the description of counter %1% (description: \"%2%\") "
461                                                 "to an SWTrace string")
462                                   % counterUid
463                                   % counterName);
464         return false;
465     }
466
467     // Event record word 7:
468     // 0:31 [32] units_offset: (optional) offset from the beginning of the event record pool to the units field.
469     //                         An offset value of zero indicates this field is not provided
470     bool includeUnits = !counterUnits.empty();
471     // The size of the description buffer in bytes
472     uint32_t eventRecordWord7 = includeUnits ?
473                                 eventRecordWord6 +
474                                 numeric_cast<uint32_t>(counterDescriptionBuffer.size() * uint32_t_size) :
475                                 0;
476
477     // Convert the counter units into a SWTrace namestring (optional)
478     std::vector<uint32_t> counterUnitsBuffer;
479     if (includeUnits)
480     {
481         // Convert the counter units into a SWTrace namestring
482         if (!StringToSwTraceString<SwTraceNameCharPolicy>(counterUnits, counterUnitsBuffer))
483         {
484             errorMessage = boost::str(boost::format("Cannot convert the units of counter %1% (units: \"%2%\") "
485                                                     "to an SWTrace string")
486                                       % counterUid
487                                       % counterName);
488             return false;
489         }
490     }
491
492     // Calculate the size in words of the event record
493     size_t eventRecordSize = 8u + // The size of the fixed part (counter_uid + max_counter_uid + device +
494                                   //                             counter_set + class + interpolation +
495                                   //                             multiplier + name_offset + description_offset +
496                                   //                             units_offset)
497                              counterNameBuffer.size() +        // The size of the variable part (the counter name,
498                              counterDescriptionBuffer.size() + // description and units including the null-terminator)
499                              counterUnitsBuffer.size();
500
501     // Allocate the space for the event record
502     eventRecord.resize(eventRecordSize);
503
504     ARMNN_NO_CONVERSION_WARN_BEGIN
505     // Create the event record
506     eventRecord[0] = eventRecordWord0; // max_counter_uid + counter_uid
507     eventRecord[1] = eventRecordWord1; // device + counter_set
508     eventRecord[2] = eventRecordWord2; // class + interpolation
509     eventRecord[3] = eventRecordWord3; // multiplier
510     eventRecord[4] = eventRecordWord4; // multiplier
511     eventRecord[5] = eventRecordWord5; // name_offset
512     eventRecord[6] = eventRecordWord6; // description_offset
513     eventRecord[7] = eventRecordWord7; // units_offset
514     auto offset = eventRecord.begin() + 8u;
515     std::copy(counterNameBuffer.begin(), counterNameBuffer.end(), offset); // name
516     offset += counterNameBuffer.size();
517     std::copy(counterDescriptionBuffer.begin(), counterDescriptionBuffer.end(), offset); // description
518     if (includeUnits)
519     {
520         offset += counterDescriptionBuffer.size();
521         std::copy(counterUnitsBuffer.begin(), counterUnitsBuffer.end(), offset); // units
522     }
523     ARMNN_NO_CONVERSION_WARN_END
524
525     return true;
526 }
527
528 void SendCounterPacket::SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory)
529 {
530     using namespace boost::numeric;
531
532     // Get the amount of data that needs to be put into the packet
533     uint16_t categoryCount    = counterDirectory.GetCategoryCount();
534     uint16_t deviceCount      = counterDirectory.GetDeviceCount();
535     uint16_t counterSetCount  = counterDirectory.GetCounterSetCount();
536
537     // Utils
538     size_t uint32_t_size = sizeof(uint32_t);
539     size_t packetHeaderSize = 2u;
540     size_t bodyHeaderSize = 6u;
541
542     // Initialize the offset for the pointer tables
543     uint32_t pointerTableOffset = 0;
544
545     // --------------
546     // Device records
547     // --------------
548
549     // Process device records
550     std::vector<DeviceRecord> deviceRecords(deviceCount);
551     const Devices& devices = counterDirectory.GetDevices();
552     std::vector<uint32_t> deviceRecordOffsets(deviceCount, 0); // device_records_pointer_table
553     size_t deviceRecordsSize = 0;
554     size_t deviceIndex = 0;
555     size_t deviceRecordOffsetIndex = 0;
556     for (auto it = devices.begin(); it != devices.end(); it++)
557     {
558         const DevicePtr& device = it->second;
559         DeviceRecord& deviceRecord = deviceRecords.at(deviceIndex);
560
561         std::string errorMessage;
562         if (!CreateDeviceRecord(device, deviceRecord, errorMessage))
563         {
564             CancelOperationAndThrow<RuntimeException>(errorMessage);
565         }
566
567         // Update the total size in words of the device records
568         deviceRecordsSize += deviceRecord.size();
569
570         // Add the device record offset to the device records pointer table offset field
571         deviceRecordOffsets[deviceRecordOffsetIndex] = pointerTableOffset;
572         pointerTableOffset += numeric_cast<uint32_t>(deviceRecord.size() * uint32_t_size);
573
574         deviceIndex++;
575         deviceRecordOffsetIndex++;
576     }
577
578     // -------------------
579     // Counter set records
580     // -------------------
581
582     // Process counter set records
583     std::vector<CounterSetRecord> counterSetRecords(counterSetCount);
584     const CounterSets& counterSets = counterDirectory.GetCounterSets();
585     std::vector<uint32_t> counterSetRecordOffsets(counterSetCount, 0); // counter_set_records_pointer_table
586     size_t counterSetRecordsSize = 0;
587     size_t counterSetIndex = 0;
588     size_t counterSetRecordOffsetIndex = 0;
589     for (auto it = counterSets.begin(); it != counterSets.end(); it++)
590     {
591         const CounterSetPtr& counterSet = it->second;
592         CounterSetRecord& counterSetRecord = counterSetRecords.at(counterSetIndex);
593
594         std::string errorMessage;
595         if (!CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage))
596         {
597             CancelOperationAndThrow<RuntimeException>(errorMessage);
598         }
599
600         // Update the total size in words of the counter set records
601         counterSetRecordsSize += counterSetRecord.size();
602
603         // Add the counter set record offset to the counter set records pointer table offset field
604         counterSetRecordOffsets[counterSetRecordOffsetIndex] = pointerTableOffset;
605         pointerTableOffset += numeric_cast<uint32_t>(counterSetRecord.size() * uint32_t_size);
606
607         counterSetIndex++;
608         counterSetRecordOffsetIndex++;
609     }
610
611     // ----------------
612     // Category records
613     // ----------------
614
615     // Process category records
616     std::vector<CategoryRecord> categoryRecords(categoryCount);
617     const Categories& categories = counterDirectory.GetCategories();
618     std::vector<uint32_t> categoryRecordOffsets(categoryCount, 0); // category_records_pointer_table
619     size_t categoryRecordsSize = 0;
620     size_t categoryIndex = 0;
621     size_t categoryRecordOffsetIndex = 0;
622     for (auto it = categories.begin(); it != categories.end(); it++)
623     {
624         const CategoryPtr& category = *it;
625         CategoryRecord& categoryRecord = categoryRecords.at(categoryIndex);
626
627         std::string errorMessage;
628         if (!CreateCategoryRecord(category, counterDirectory.GetCounters(), categoryRecord, errorMessage))
629         {
630             CancelOperationAndThrow<RuntimeException>(errorMessage);
631         }
632
633         // Update the total size in words of the category records
634         categoryRecordsSize += categoryRecord.size();
635
636         // Add the category record offset to the category records pointer table offset field
637         categoryRecordOffsets[categoryRecordOffsetIndex] = pointerTableOffset;
638         pointerTableOffset += numeric_cast<uint32_t>(categoryRecord.size() * uint32_t_size);
639
640         categoryIndex++;
641         categoryRecordOffsetIndex++;
642     }
643
644
645
646     // Calculate the length in words of the counter directory packet's data (excludes the packet header size)
647     size_t counterDirectoryPacketDataLength =
648             bodyHeaderSize +                 // The size of the body header
649             deviceRecordOffsets.size() +     // The size of the device records pointer table
650             counterSetRecordOffsets.size() + // The size of counter set pointer table
651             categoryRecordOffsets.size() +   // The size of category records pointer table
652             deviceRecordsSize +              // The total size of the device records
653             counterSetRecordsSize +          // The total size of the counter set records
654             categoryRecordsSize;             // The total size of the category records
655
656     // Calculate the size in words of the counter directory packet (the data length plus the packet header size)
657     size_t counterDirectoryPacketSize = packetHeaderSize +                // The size of the packet header
658                                         counterDirectoryPacketDataLength; // The data length
659
660
661     // Allocate the necessary space for the counter directory packet
662     std::vector<uint32_t> counterDirectoryPacket(counterDirectoryPacketSize, 0);
663
664     // -------------
665     // Packet header
666     // -------------
667
668     // Packet header word 0:
669     // 26:31 [6]  packet_family: control Packet Family
670     // 16:25 [10] packet_id: packet identifier
671     // 8:15  [8]  reserved: all zeros
672     // 0:7   [8]  reserved: all zeros
673     uint32_t packetFamily = 0;
674     uint32_t packetId = 2;
675     uint32_t packetHeaderWord0 = ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16);
676
677     // Packet header word 1:
678     // 0:31 [32] data_length: length of data, in bytes
679     uint32_t packetHeaderWord1 = numeric_cast<uint32_t>(counterDirectoryPacketDataLength * uint32_t_size);
680
681     // Create the packet header
682     uint32_t packetHeader[2]
683     {
684         packetHeaderWord0, // packet_family + packet_id + reserved + reserved
685         packetHeaderWord1  // data_length
686     };
687
688     // -----------
689     // Body header
690     // -----------
691
692     // Body header word 0:
693     // 16:31 [16] device_records_count: number of entries in the device_records_pointer_table
694     // 0:15  [16] reserved: all zeros
695     uint32_t bodyHeaderWord0 = static_cast<uint32_t>(deviceCount) << 16;
696
697     // Body header word 1:
698     // 0:31 [32] device_records_pointer_table_offset: offset to the device_records_pointer_table
699     uint32_t bodyHeaderWord1 = 0; // The offset is always zero here, as the device record pointer table field is always
700                                   // the first item in the pool
701
702     // Body header word 2:
703     // 16:31 [16] counter_set_count: number of entries in the counter_set_pointer_table
704     // 0:15  [16] reserved: all zeros
705     uint32_t bodyHeaderWord2 = static_cast<uint32_t>(counterSetCount) << 16;
706
707     // Body header word 3:
708     // 0:31 [32] counter_set_pointer_table_offset: offset to the counter_set_pointer_table
709     uint32_t bodyHeaderWord3 =
710             numeric_cast<uint32_t>(deviceRecordOffsets.size() * uint32_t_size); // The size of the device records
711                                                                                 // pointer table
712
713
714     // Body header word 4:
715     // 16:31 [16] categories_count: number of entries in the categories_pointer_table
716     // 0:15  [16] reserved: all zeros
717     uint32_t bodyHeaderWord4 = static_cast<uint32_t>(categoryCount) << 16;
718
719     // Body header word 3:
720     // 0:31 [32] categories_pointer_table_offset: offset to the categories_pointer_table
721     uint32_t bodyHeaderWord5 =
722             numeric_cast<uint32_t>(deviceRecordOffsets.size() * uint32_t_size +     // The size of the device records
723                                    counterSetRecordOffsets.size() * uint32_t_size); // pointer table, plus the size of
724                                                                                     // the counter set pointer table
725
726     // Create the body header
727     uint32_t bodyHeader[6]
728     {
729         bodyHeaderWord0, // device_records_count + reserved
730         bodyHeaderWord1, // device_records_pointer_table_offset
731         bodyHeaderWord2, // counter_set_count + reserved
732         bodyHeaderWord3, // counter_set_pointer_table_offset
733         bodyHeaderWord4, // categories_count + reserved
734         bodyHeaderWord5  // categories_pointer_table_offset
735     };
736
737     ARMNN_NO_CONVERSION_WARN_BEGIN
738     // Create the counter directory packet
739     auto counterDirectoryPacketOffset = counterDirectoryPacket.begin();
740     // packet_header
741     std::copy(packetHeader, packetHeader + packetHeaderSize, counterDirectoryPacketOffset);
742     counterDirectoryPacketOffset += packetHeaderSize;
743     // body_header
744     std::copy(bodyHeader, bodyHeader + bodyHeaderSize, counterDirectoryPacketOffset);
745     counterDirectoryPacketOffset += bodyHeaderSize;
746     // device_records_pointer_table
747     std::copy(deviceRecordOffsets.begin(), deviceRecordOffsets.end(), counterDirectoryPacketOffset);
748     counterDirectoryPacketOffset += deviceRecordOffsets.size();
749     // counter_set_pointer_table
750     std::copy(counterSetRecordOffsets.begin(), counterSetRecordOffsets.end(), counterDirectoryPacketOffset);
751     counterDirectoryPacketOffset += counterSetRecordOffsets.size();
752     // category_pointer_table
753     std::copy(categoryRecordOffsets.begin(), categoryRecordOffsets.end(), counterDirectoryPacketOffset);
754     counterDirectoryPacketOffset += categoryRecordOffsets.size();
755     // device_records
756     for (const DeviceRecord& deviceRecord : deviceRecords)
757     {
758         std::copy(deviceRecord.begin(), deviceRecord.end(), counterDirectoryPacketOffset); // device_record
759         counterDirectoryPacketOffset += deviceRecord.size();
760     }
761     // counter_set_records
762     for (const CounterSetRecord& counterSetRecord : counterSetRecords)
763     {
764         std::copy(counterSetRecord.begin(), counterSetRecord.end(), counterDirectoryPacketOffset); // counter_set_record
765         counterDirectoryPacketOffset += counterSetRecord.size();
766     }
767     // category_records
768     for (const CategoryRecord& categoryRecord : categoryRecords)
769     {
770         std::copy(categoryRecord.begin(), categoryRecord.end(), counterDirectoryPacketOffset); // category_record
771         counterDirectoryPacketOffset += categoryRecord.size();
772     }
773     ARMNN_NO_CONVERSION_WARN_END
774
775     // Calculate the total size in bytes of the counter directory packet
776     uint32_t totalSize = numeric_cast<uint32_t>(counterDirectoryPacketSize * uint32_t_size);
777
778     // Reserve space in the buffer for the packet
779     uint32_t reserved = 0;
780     IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
781
782     if (writeBuffer == nullptr || reserved < totalSize)
783     {
784         CancelOperationAndThrow<BufferExhaustion>(
785             writeBuffer,
786             boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
787     }
788
789     // Offset for writing to the buffer
790     uint32_t offset = 0;
791
792     // Write the counter directory packet to the buffer
793     for (uint32_t counterDirectoryPacketWord : counterDirectoryPacket)
794     {
795         WriteUint32(writeBuffer, offset, counterDirectoryPacketWord);
796         offset += numeric_cast<uint32_t>(uint32_t_size);
797     }
798
799     m_BufferManager.Commit(writeBuffer, totalSize);
800 }
801
802 void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
803 {
804     uint32_t uint16_t_size = sizeof(uint16_t);
805     uint32_t uint32_t_size = sizeof(uint32_t);
806     uint32_t uint64_t_size = sizeof(uint64_t);
807
808     uint32_t packetFamily = 3;
809     uint32_t packetClass = 0;
810     uint32_t packetType = 0;
811     uint32_t headerSize = 2 * uint32_t_size;
812     uint32_t bodySize = uint64_t_size + numeric_cast<uint32_t>(values.size()) * (uint16_t_size + uint32_t_size);
813     uint32_t totalSize = headerSize + bodySize;
814     uint32_t offset = 0;
815     uint32_t reserved = 0;
816
817     IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
818
819     if (writeBuffer == nullptr || reserved < totalSize)
820     {
821         CancelOperationAndThrow<BufferExhaustion>(
822             writeBuffer,
823             boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
824     }
825
826     // Create header.
827     WriteUint32(writeBuffer,
828                 offset,
829                 ((packetFamily & 0x0000003F) << 26) |
830                 ((packetClass  & 0x0000007F) << 19) |
831                 ((packetType   & 0x00000007) << 16));
832     offset += uint32_t_size;
833     WriteUint32(writeBuffer, offset, bodySize);
834
835     // Copy captured Timestamp.
836     offset += uint32_t_size;
837     WriteUint64(writeBuffer, offset, timestamp);
838
839     // Copy selectedCounterIds.
840     offset += uint64_t_size;
841     for (const auto& pair: values)
842     {
843         WriteUint16(writeBuffer, offset, pair.counterId);
844         offset += uint16_t_size;
845         WriteUint32(writeBuffer, offset, pair.counterValue);
846         offset += uint32_t_size;
847     }
848
849     m_BufferManager.Commit(writeBuffer, totalSize);
850 }
851
852 void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
853                                                            const std::vector<uint16_t>& selectedCounterIds)
854 {
855     uint32_t uint16_t_size = sizeof(uint16_t);
856     uint32_t uint32_t_size = sizeof(uint32_t);
857
858     uint32_t packetFamily = 0;
859     uint32_t packetId = 4;
860     uint32_t headerSize = 2 * uint32_t_size;
861     uint32_t bodySize = uint32_t_size + numeric_cast<uint32_t>(selectedCounterIds.size()) * uint16_t_size;
862     uint32_t totalSize = headerSize + bodySize;
863     uint32_t offset = 0;
864     uint32_t reserved = 0;
865
866     IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved);
867
868     if (writeBuffer == nullptr || reserved < totalSize)
869     {
870         CancelOperationAndThrow<BufferExhaustion>(
871             writeBuffer,
872             boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
873     }
874
875     // Create header.
876     WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
877     offset += uint32_t_size;
878     WriteUint32(writeBuffer, offset, bodySize);
879
880     // Copy capturePeriod.
881     offset += uint32_t_size;
882     WriteUint32(writeBuffer, offset, capturePeriod);
883
884     // Copy selectedCounterIds.
885     offset += uint32_t_size;
886     for(const uint16_t& id: selectedCounterIds)
887     {
888         WriteUint16(writeBuffer, offset, id);
889         offset += uint16_t_size;
890     }
891
892     m_BufferManager.Commit(writeBuffer, totalSize);
893 }
894
895 } // namespace profiling
896
897 } // namespace armnn