aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/test/RequestCountersPacketHandler.cpp
blob: b688b2d0c375f6de96a08af0a458e94d08751983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "RequestCountersPacketHandler.hpp"

#include "DirectoryCaptureCommandHandler.hpp"

#include <common/include/NumericCast.hpp>
#include <common/include/PacketVersionResolver.hpp>
#include <common/include/ProfilingException.hpp>

namespace arm
{

namespace pipe
{

std::vector<uint32_t> RequestCountersPacketHandler::GetHeadersAccepted()
{
    std::vector<uint32_t> headers;
    headers.push_back(m_CounterDirectoryMessageHeader); // counter directory
    return headers;
}

void RequestCountersPacketHandler::HandlePacket(const arm::pipe::Packet& packet)
{
    if (packet.GetHeader() != m_CounterDirectoryMessageHeader)
    {
        return;
    }
    arm::pipe::PacketVersionResolver packetVersionResolver;
    DirectoryCaptureCommandHandler directoryCaptureCommandHandler(
            0, 2, packetVersionResolver.ResolvePacketVersion(0, 2).GetEncodedValue());
    directoryCaptureCommandHandler.operator()(packet);
    const ICounterDirectory& counterDirectory = directoryCaptureCommandHandler.GetCounterDirectory();
    for (auto& category : counterDirectory.GetCategories())
    {
        // Remember we need to translate the Uid's from our CounterDirectory instance to the parent one.
        std::vector<uint16_t> translatedCounters;
        for (auto const& copyUid : category->m_Counters)
        {
            translatedCounters.emplace_back(directoryCaptureCommandHandler.TranslateUIDCopyToOriginal(copyUid));
        }
        m_IdList.insert(std::end(m_IdList), std::begin(translatedCounters), std::end(translatedCounters));
    }
    SendCounterSelectionPacket();
}

void RequestCountersPacketHandler::SendCounterSelectionPacket()
{
    uint32_t uint16_t_size = sizeof(uint16_t);
    uint32_t uint32_t_size = sizeof(uint32_t);

    uint32_t offset   = 0;
    uint32_t bodySize = uint32_t_size + arm::pipe::numeric_cast<uint32_t>(m_IdList.size()) * uint16_t_size;

    auto uniqueData     = std::make_unique<unsigned char[]>(bodySize);
    auto data = reinterpret_cast<unsigned char*>(uniqueData.get());

    // Copy capturePeriod
    WriteUint32(data, offset, m_CapturePeriod);

    // Copy m_IdList
    offset += uint32_t_size;
    for (const uint16_t& id : m_IdList)
    {
        WriteUint16(data, offset, id);
        offset += uint16_t_size;
    }

    arm::pipe::Packet packet(0x40000, bodySize, uniqueData);
    m_Connection->ReturnPacket(packet);
}

} // namespace pipe

} // namespace arm