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