IVGCVSW-3693 Implement SendCounterPacket.SendPeriodicCounterSelectionPacket() function
[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 #include "ProfilingUtils.hpp"
9
10 #include <armnn/Exceptions.hpp>
11
12 #include <boost/format.hpp>
13 #include <boost/numeric/conversion/cast.hpp>
14
15 #include <unistd.h>
16
17 namespace armnn
18 {
19
20 namespace profiling
21 {
22
23 using boost::numeric_cast;
24
25 void SendCounterPacket::SendStreamMetaDataPacket()
26 {
27     throw armnn::UnimplementedException();
28 }
29
30 void SendCounterPacket::SendCounterDirectoryPacket(const Category& category, const std::vector<Counter>& counters)
31 {
32     throw armnn::UnimplementedException();
33 }
34
35 void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const std::vector<uint32_t>& counterValues,
36                                                          const std::vector<uint16_t>& counterUids)
37 {
38     throw armnn::UnimplementedException();
39 }
40
41 void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
42                                                            const std::vector<uint16_t>& selectedCounterIds)
43 {
44     uint32_t packetFamily = 0;
45     uint32_t packetId = 4;
46     uint32_t headerSize = numeric_cast<uint32_t>(2 * sizeof(uint32_t));
47     uint32_t bodySize = numeric_cast<uint32_t>((1 * sizeof(uint32_t)) + (selectedCounterIds.size() * sizeof(uint16_t)));
48     uint32_t totalSize = headerSize + bodySize;
49     uint32_t offset = 0;
50     uint32_t reserved = 0;
51
52     unsigned char* writeBuffer = m_Buffer.Reserve(totalSize, reserved);
53
54     if (reserved < totalSize)
55     {
56         // Cancel the operation.
57         m_Buffer.Commit(0);
58         throw RuntimeException(boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.")
59                                % totalSize));
60     }
61
62     if (writeBuffer == nullptr)
63     {
64         // Cancel the operation.
65         m_Buffer.Commit(0);
66         throw RuntimeException("Error reserving buffer memory.");
67     }
68
69     // Create header.
70     WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
71     offset += numeric_cast<uint32_t>(sizeof(uint32_t));
72     WriteUint32(writeBuffer, offset, bodySize);
73
74     // Copy capturePeriod.
75     offset += numeric_cast<uint32_t>(sizeof(uint32_t));
76     WriteUint32(writeBuffer, offset, capturePeriod);
77
78     // Copy selectedCounterIds.
79     offset += numeric_cast<uint32_t>(sizeof(uint32_t));
80     for(const uint16_t& id: selectedCounterIds)
81     {
82         WriteUint16(writeBuffer, offset, id);
83         offset += numeric_cast<uint32_t>(sizeof(uint16_t));
84     }
85
86     m_Buffer.Commit(totalSize);
87 }
88
89 void SendCounterPacket::SetReadyToRead()
90 {
91     m_ReadyToRead = true;
92 }
93
94 } // namespace profiling
95
96 } // namespace armnn