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