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