3ba50503a1196565df20a7747858e7043d093aa4
[platform/upstream/armnn.git] / src / profiling / test / RequestCountersPacketHandler.cpp
1 //
2 // Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "RequestCountersPacketHandler.hpp"
7
8 #include "DirectoryCaptureCommandHandler.hpp"
9
10 #include <armnn/utility/NumericCast.hpp>
11
12 #include <common/include/PacketVersionResolver.hpp>
13 #include <common/include/ProfilingException.hpp>
14
15 namespace armnn
16 {
17
18 namespace profiling
19 {
20
21 std::vector<uint32_t> RequestCountersPacketHandler::GetHeadersAccepted()
22 {
23     std::vector<uint32_t> headers;
24     headers.push_back(m_CounterDirectoryMessageHeader); // counter directory
25     return headers;
26 }
27
28 void RequestCountersPacketHandler::HandlePacket(const arm::pipe::Packet& packet)
29 {
30     if (packet.GetHeader() != m_CounterDirectoryMessageHeader)
31     {
32         return;
33     }
34     arm::pipe::PacketVersionResolver packetVersionResolver;
35     DirectoryCaptureCommandHandler directoryCaptureCommandHandler(
36             0, 2, packetVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue());
37     directoryCaptureCommandHandler.operator()(packet);
38     const ICounterDirectory& counterDirectory = directoryCaptureCommandHandler.GetCounterDirectory();
39     for (auto& category : counterDirectory.GetCategories())
40     {
41         // Remember we need to translate the Uid's from our CounterDirectory instance to the parent one.
42         std::vector<uint16_t> translatedCounters;
43         for (auto const& copyUid : category->m_Counters)
44         {
45             translatedCounters.emplace_back(directoryCaptureCommandHandler.TranslateUIDCopyToOriginal(copyUid));
46         }
47         m_IdList.insert(std::end(m_IdList), std::begin(translatedCounters), std::end(translatedCounters));
48     }
49     SendCounterSelectionPacket();
50 }
51
52 void RequestCountersPacketHandler::SendCounterSelectionPacket()
53 {
54     uint32_t uint16_t_size = sizeof(uint16_t);
55     uint32_t uint32_t_size = sizeof(uint32_t);
56
57     uint32_t offset   = 0;
58     uint32_t bodySize = uint32_t_size + armnn::numeric_cast<uint32_t>(m_IdList.size()) * uint16_t_size;
59
60     auto uniqueData     = std::make_unique<unsigned char[]>(bodySize);
61     auto data = reinterpret_cast<unsigned char*>(uniqueData.get());
62
63     // Copy capturePeriod
64     WriteUint32(data, offset, m_CapturePeriod);
65
66     // Copy m_IdList
67     offset += uint32_t_size;
68     for (const uint16_t& id : m_IdList)
69     {
70         WriteUint16(data, offset, id);
71         offset += uint16_t_size;
72     }
73
74     arm::pipe::Packet packet(0x40000, bodySize, uniqueData);
75     m_Connection->ReturnPacket(packet);
76 }
77
78 } // namespace profiling
79
80 } // namespace armnn