aboutsummaryrefslogtreecommitdiff
path: root/src/profiling/SendCounterPacket.cpp
blob: 645b79196e597b2f14927135a318f35eaf48f64d (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "SendCounterPacket.hpp"
#include "EncodeVersion.hpp"
#include "ProfilingUtils.hpp"

#include <armnn/Exceptions.hpp>

#include <boost/format.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include <unistd.h>

namespace armnn
{

namespace profiling
{

using boost::numeric_cast;

void SendCounterPacket::SendStreamMetaDataPacket()
{
    throw armnn::UnimplementedException();
}

void SendCounterPacket::SendCounterDirectoryPacket(const Category& category, const std::vector<Counter>& counters)
{
    throw armnn::UnimplementedException();
}

void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values)
{
    uint32_t packetFamily = 1;
    uint32_t packetClass = 0;
    uint32_t packetType = 0;
    uint32_t headerSize = numeric_cast<uint32_t>(2 * sizeof(uint32_t));
    uint32_t bodySize = numeric_cast<uint32_t>((1 * sizeof(uint64_t)) +
                                               (values.size() * (sizeof(uint16_t) + sizeof(uint32_t))));
    uint32_t totalSize = headerSize + bodySize;
    uint32_t offset = 0;
    uint32_t reserved = 0;

    unsigned char* writeBuffer = m_Buffer.Reserve(totalSize, reserved);

    if (reserved < totalSize)
    {
        // Cancel the operation.
        m_Buffer.Commit(0);
        throw profiling::BufferExhaustion(
                boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.") % totalSize));
    }

    if (writeBuffer == nullptr)
    {
        // Cancel the operation.
        m_Buffer.Commit(0);
        throw RuntimeException("Error reserving buffer memory.");
    }

    // Create header.
    WriteUint32(writeBuffer,
                offset,
                ((packetFamily & 0x3F) << 26) | ((packetClass & 0x3FF) << 19) | ((packetType & 0x3FFF) << 16));
    offset += numeric_cast<uint32_t>(sizeof(uint32_t));
    WriteUint32(writeBuffer, offset, bodySize);

    // Copy captured Timestamp.
    offset += numeric_cast<uint32_t>(sizeof(uint32_t));
    WriteUint64(writeBuffer, offset, timestamp);

    // Copy selectedCounterIds.
    offset += numeric_cast<uint32_t>(sizeof(uint64_t));
    for (const auto& pair: values)
    {
        WriteUint16(writeBuffer, offset, pair.first);
        offset += numeric_cast<uint32_t>(sizeof(uint16_t));
        WriteUint32(writeBuffer, offset, pair.second);
        offset += numeric_cast<uint32_t>(sizeof(uint32_t));
    }

    m_Buffer.Commit(totalSize);
}

void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
                                                           const std::vector<uint16_t>& selectedCounterIds)
{
    uint32_t packetFamily = 0;
    uint32_t packetId = 4;
    uint32_t headerSize = numeric_cast<uint32_t>(2 * sizeof(uint32_t));
    uint32_t bodySize   = numeric_cast<uint32_t>((1 * sizeof(uint32_t)) +
                                                 (selectedCounterIds.size() * sizeof(uint16_t)));
    uint32_t totalSize = headerSize + bodySize;
    uint32_t offset = 0;
    uint32_t reserved = 0;

    unsigned char* writeBuffer = m_Buffer.Reserve(totalSize, reserved);

    if (reserved < totalSize)
    {
        // Cancel the operation.
        m_Buffer.Commit(0);
        throw RuntimeException(boost::str(boost::format("No space left in buffer. Unable to reserve (%1%) bytes.")
                               % totalSize));
    }

    if (writeBuffer == nullptr)
    {
        // Cancel the operation.
        m_Buffer.Commit(0);
        throw RuntimeException("Error reserving buffer memory.");
    }

    // Create header.
    WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16));
    offset += numeric_cast<uint32_t>(sizeof(uint32_t));
    WriteUint32(writeBuffer, offset, bodySize);

    // Copy capturePeriod.
    offset += numeric_cast<uint32_t>(sizeof(uint32_t));
    WriteUint32(writeBuffer, offset, capturePeriod);

    // Copy selectedCounterIds.
    offset += numeric_cast<uint32_t>(sizeof(uint32_t));
    for(const uint16_t& id: selectedCounterIds)
    {
        WriteUint16(writeBuffer, offset, id);
        offset += numeric_cast<uint32_t>(sizeof(uint16_t));
    }

    m_Buffer.Commit(totalSize);
}

void SendCounterPacket::SetReadyToRead()
{
    m_ReadyToRead = true;
}

} // namespace profiling

} // namespace armnn