Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 83d08a9 | 2020-07-09 13:48:16 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "SendCounterPacket.hpp" |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 7 | #include <common/include/EncodeVersion.hpp> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 8 | |
| 9 | #include <armnn/Exceptions.hpp> |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 10 | #include <armnn/Conversion.hpp> |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 11 | #include <Processes.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 12 | #include <armnn/utility/Assert.hpp> |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 13 | #include <armnn/utility/NumericCast.hpp> |
Finn Williams | e09fc82 | 2020-04-29 13:17:30 +0100 | [diff] [blame] | 14 | #include <common/include/Constants.hpp> |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 15 | #include <common/include/SwTrace.hpp> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 16 | |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 17 | #include <fmt/format.h> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 18 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 19 | #include <cstring> |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 20 | |
| 21 | namespace armnn |
| 22 | { |
| 23 | |
| 24 | namespace profiling |
| 25 | { |
| 26 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 27 | void SendCounterPacket::SendStreamMetaDataPacket() |
| 28 | { |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 29 | const std::string info(GetSoftwareInfo()); |
| 30 | const std::string hardwareVersion(GetHardwareVersion()); |
| 31 | const std::string softwareVersion(GetSoftwareVersion()); |
| 32 | const std::string processName = GetProcessName().substr(0, 60); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 33 | |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 34 | const uint32_t infoSize = armnn::numeric_cast<uint32_t>(info.size()) + 1; |
| 35 | const uint32_t hardwareVersionSize = armnn::numeric_cast<uint32_t>(hardwareVersion.size()) + 1; |
| 36 | const uint32_t softwareVersionSize = armnn::numeric_cast<uint32_t>(softwareVersion.size()) + 1; |
| 37 | const uint32_t processNameSize = armnn::numeric_cast<uint32_t>(processName.size()) + 1; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 38 | |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 39 | const uint32_t sizeUint32 = sizeof(uint32_t); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 40 | |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 41 | const uint32_t headerSize = 2 * sizeUint32; |
| 42 | const uint32_t bodySize = 10 * sizeUint32; |
| 43 | const uint32_t packetVersionCountSize = sizeUint32; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 44 | |
| 45 | // Supported Packets |
Jim Flynn | 83d08a9 | 2020-07-09 13:48:16 +0100 | [diff] [blame] | 46 | // Packet Encoding version 1.0.0 |
| 47 | // Control packet family |
| 48 | // Stream metadata packet (packet family=0; packet id=0) |
| 49 | // Connection Acknowledged packet ( packet family=0, packet id=1) Version 1.0.0 |
| 50 | // Counter Directory packet (packet family=0; packet id=2) Version 1.0.0 |
| 51 | // Request Counter Directory packet ( packet family=0, packet id=3) Version 1.0.0 |
| 52 | // Periodic Counter Selection packet ( packet family=0, packet id=4) Version 1.0.0 |
| 53 | // Per Job Counter Selection packet ( packet family=0, packet id=5) Version 1.0.0 |
| 54 | // Activate Timeline Reporting (packet family = 0, packet id = 6) Version 1.0.0 |
| 55 | // Deactivate Timeline Reporting (packet family = 0, packet id = 7) Version 1.0.0 |
| 56 | // Counter Packet Family |
| 57 | // Periodic Counter Capture (packet_family = 3, packet_class = 0, packet_type = 0) Version 1.0.0 |
| 58 | // Per-Job Counter Capture (packet_family = 3, packet_class = 1, packet_type = 0,1) Version 1.0.0 |
| 59 | // Timeline Packet Family |
| 60 | // Timeline Message Directory (packet_family = 1, packet_class = 0, packet_type = 0) Version 1.0.0 |
| 61 | // Timeline Message (packet_family = 1, packet_class = 0, packet_type = 1) Version 1.0.0 |
| 62 | std::vector<std::pair<uint32_t, uint32_t>> packetVersions; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 63 | packetVersions.push_back(std::make_pair(ConstructHeader(0, 0), arm::pipe::EncodeVersion(1, 0, 0))); |
| 64 | packetVersions.push_back(std::make_pair(ConstructHeader(0, 1), arm::pipe::EncodeVersion(1, 0, 0))); |
| 65 | packetVersions.push_back(std::make_pair(ConstructHeader(0, 2), arm::pipe::EncodeVersion(1, 0, 0))); |
| 66 | packetVersions.push_back(std::make_pair(ConstructHeader(0, 3), arm::pipe::EncodeVersion(1, 0, 0))); |
| 67 | packetVersions.push_back(std::make_pair(ConstructHeader(0, 4), arm::pipe::EncodeVersion(1, 0, 0))); |
| 68 | packetVersions.push_back(std::make_pair(ConstructHeader(0, 5), arm::pipe::EncodeVersion(1, 0, 0))); |
| 69 | packetVersions.push_back(std::make_pair(ConstructHeader(0, 6), arm::pipe::EncodeVersion(1, 0, 0))); |
| 70 | packetVersions.push_back(std::make_pair(ConstructHeader(0, 7), arm::pipe::EncodeVersion(1, 0, 0))); |
| 71 | packetVersions.push_back(std::make_pair(ConstructHeader(3, 0, 0), arm::pipe::EncodeVersion(1, 0, 0))); |
| 72 | packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 0), arm::pipe::EncodeVersion(1, 0, 0))); |
| 73 | packetVersions.push_back(std::make_pair(ConstructHeader(3, 1, 1), arm::pipe::EncodeVersion(1, 0, 0))); |
| 74 | packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 0), arm::pipe::EncodeVersion(1, 0, 0))); |
| 75 | packetVersions.push_back(std::make_pair(ConstructHeader(1, 0, 1), arm::pipe::EncodeVersion(1, 0, 0))); |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 76 | uint32_t numberOfVersions = armnn::numeric_cast<uint32_t>(packetVersions.size()); |
| 77 | uint32_t packetVersionSize = armnn::numeric_cast<uint32_t>(numberOfVersions * 2 * sizeUint32); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 78 | |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 79 | const uint32_t payloadSize = armnn::numeric_cast<uint32_t>(infoSize + hardwareVersionSize + |
| 80 | softwareVersionSize + processNameSize + |
| 81 | packetVersionCountSize + packetVersionSize); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 82 | |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 83 | const uint32_t totalSize = headerSize + bodySize + payloadSize; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 84 | uint32_t offset = 0; |
| 85 | uint32_t reserved = 0; |
| 86 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 87 | IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 88 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 89 | if (writeBuffer == nullptr || reserved < totalSize) |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 90 | { |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 91 | CancelOperationAndThrow<BufferExhaustion>( |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 92 | writeBuffer, |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 93 | fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize)); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | try |
| 97 | { |
| 98 | // Create header |
| 99 | |
| 100 | WriteUint32(writeBuffer, offset, 0); |
| 101 | offset += sizeUint32; |
| 102 | WriteUint32(writeBuffer, offset, totalSize - headerSize); |
| 103 | |
| 104 | // Packet body |
| 105 | |
| 106 | offset += sizeUint32; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 107 | WriteUint32(writeBuffer, offset, arm::pipe::PIPE_MAGIC); // pipe_magic |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 108 | offset += sizeUint32; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 109 | WriteUint32(writeBuffer, offset, arm::pipe::EncodeVersion(1, 0, 0)); // stream_metadata_version |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 110 | offset += sizeUint32; |
| 111 | WriteUint32(writeBuffer, offset, MAX_METADATA_PACKET_LENGTH); // max_data_length |
| 112 | offset += sizeUint32; |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 113 | int pid = armnnUtils::Processes::GetCurrentId(); |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 114 | WriteUint32(writeBuffer, offset, armnn::numeric_cast<uint32_t>(pid)); // pid |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 115 | offset += sizeUint32; |
| 116 | uint32_t poolOffset = bodySize; |
Finn Williams | a0de056 | 2020-04-22 12:27:37 +0100 | [diff] [blame] | 117 | WriteUint32(writeBuffer, offset, poolOffset); // offset_info |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 118 | offset += sizeUint32; |
| 119 | poolOffset += infoSize; |
Finn Williams | a0de056 | 2020-04-22 12:27:37 +0100 | [diff] [blame] | 120 | WriteUint32(writeBuffer, offset, poolOffset); // offset_hw_version |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 121 | offset += sizeUint32; |
| 122 | poolOffset += hardwareVersionSize; |
Finn Williams | a0de056 | 2020-04-22 12:27:37 +0100 | [diff] [blame] | 123 | WriteUint32(writeBuffer, offset, poolOffset); // offset_sw_version |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 124 | offset += sizeUint32; |
| 125 | poolOffset += softwareVersionSize; |
Finn Williams | a0de056 | 2020-04-22 12:27:37 +0100 | [diff] [blame] | 126 | WriteUint32(writeBuffer, offset, poolOffset); // offset_process_name |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 127 | offset += sizeUint32; |
| 128 | poolOffset += processNameSize; |
Finn Williams | a0de056 | 2020-04-22 12:27:37 +0100 | [diff] [blame] | 129 | WriteUint32(writeBuffer, offset, poolOffset); // offset_packet_version_table |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 130 | offset += sizeUint32; |
| 131 | WriteUint32(writeBuffer, offset, 0); // reserved |
| 132 | offset += sizeUint32; |
| 133 | |
| 134 | // Pool |
| 135 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 136 | if (infoSize) |
| 137 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 138 | memcpy(&writeBuffer->GetWritableData()[offset], info.c_str(), infoSize); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 139 | offset += infoSize; |
| 140 | } |
| 141 | |
Finn Williams | a0de056 | 2020-04-22 12:27:37 +0100 | [diff] [blame] | 142 | memcpy(&writeBuffer->GetWritableData()[offset], hardwareVersion.c_str(), hardwareVersionSize); |
| 143 | offset += hardwareVersionSize; |
| 144 | memcpy(&writeBuffer->GetWritableData()[offset], softwareVersion.c_str(), softwareVersionSize); |
| 145 | offset += softwareVersionSize; |
| 146 | memcpy(&writeBuffer->GetWritableData()[offset], processName.c_str(), processNameSize); |
| 147 | offset += processNameSize; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 148 | |
Jim Flynn | 83d08a9 | 2020-07-09 13:48:16 +0100 | [diff] [blame] | 149 | if (!packetVersions.empty()) |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 150 | { |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 151 | // Packet Version Count |
Jim Flynn | 83d08a9 | 2020-07-09 13:48:16 +0100 | [diff] [blame] | 152 | WriteUint32(writeBuffer, offset, numberOfVersions << 16); |
| 153 | offset += sizeUint32; |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 154 | |
| 155 | // Packet Version Entries |
Jim Flynn | 83d08a9 | 2020-07-09 13:48:16 +0100 | [diff] [blame] | 156 | for (std::pair<uint32_t, uint32_t>& packetVersion : packetVersions) |
Ferran Balaguer | 5bf1d32 | 2019-09-13 13:31:40 +0100 | [diff] [blame] | 157 | { |
Jim Flynn | 83d08a9 | 2020-07-09 13:48:16 +0100 | [diff] [blame] | 158 | WriteUint32(writeBuffer, offset, packetVersion.first); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 159 | offset += sizeUint32; |
Jim Flynn | 83d08a9 | 2020-07-09 13:48:16 +0100 | [diff] [blame] | 160 | WriteUint32(writeBuffer, offset, packetVersion.second); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 161 | offset += sizeUint32; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | catch(...) |
| 166 | { |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 167 | CancelOperationAndThrow<RuntimeException>(writeBuffer, "Error processing packet."); |
Ferran Balaguer | 47d0fe9 | 2019-09-04 16:47:34 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Sadik Armagan | 3896b47 | 2020-02-10 12:24:15 +0000 | [diff] [blame] | 170 | m_BufferManager.Commit(writeBuffer, totalSize, false); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 173 | bool SendCounterPacket::CreateCategoryRecord(const CategoryPtr& category, |
| 174 | const Counters& counters, |
| 175 | CategoryRecord& categoryRecord, |
| 176 | std::string& errorMessage) |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 177 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 178 | ARMNN_ASSERT(category); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 179 | |
| 180 | const std::string& categoryName = category->m_Name; |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 181 | ARMNN_ASSERT(!categoryName.empty()); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 182 | |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 183 | // Remove any duplicate counters |
| 184 | std::vector<uint16_t> categoryCounters; |
| 185 | for (size_t counterIndex = 0; counterIndex < category->m_Counters.size(); ++counterIndex) |
| 186 | { |
| 187 | uint16_t counterUid = category->m_Counters.at(counterIndex); |
| 188 | auto it = counters.find(counterUid); |
| 189 | if (it == counters.end()) |
| 190 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 191 | errorMessage = fmt::format("Counter ({}) not found in category ({})", |
| 192 | counterUid, |
| 193 | category->m_Name ); |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 194 | return false; |
| 195 | } |
| 196 | |
| 197 | const CounterPtr& counter = it->second; |
| 198 | |
| 199 | if (counterUid == counter->m_MaxCounterUid) |
| 200 | { |
| 201 | categoryCounters.emplace_back(counterUid); |
| 202 | } |
| 203 | } |
| 204 | if (categoryCounters.empty()) |
| 205 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 206 | errorMessage = fmt::format("No valid counters found in category ({})", categoryName); |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 210 | // Utils |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 211 | const size_t uint32_t_size = sizeof(uint32_t); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 212 | |
| 213 | // Convert the device name into a SWTrace namestring |
| 214 | std::vector<uint32_t> categoryNameBuffer; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 215 | if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(categoryName, categoryNameBuffer)) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 216 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 217 | errorMessage = fmt::format("Cannot convert the name of category ({}) to an SWTrace namestring", |
| 218 | categoryName); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 222 | // Category record word 1: |
| 223 | // 16:31 [16] event_count: number of events belonging to this category |
| 224 | // 0:15 [16] reserved: all zeros |
| 225 | const uint32_t categoryRecordWord1 = static_cast<uint32_t>(categoryCounters.size()) << 16; |
| 226 | |
| 227 | // Category record word 2: |
| 228 | // 0:31 [32] event_pointer_table_offset: offset from the beginning of the category data pool to |
| 229 | // the event_pointer_table |
| 230 | const uint32_t categoryRecordWord2 = static_cast<uint32_t>(3u * uint32_t_size); |
| 231 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 232 | // Process the event records |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 233 | const size_t counterCount = categoryCounters.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 234 | std::vector<EventRecord> eventRecords(counterCount); |
| 235 | std::vector<uint32_t> eventRecordOffsets(counterCount, 0); |
| 236 | size_t eventRecordsSize = 0; |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 237 | uint32_t eventRecordsOffset = armnn::numeric_cast<uint32_t>( |
| 238 | (eventRecords.size() + categoryNameBuffer.size()) * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 239 | for (size_t counterIndex = 0, eventRecordIndex = 0, eventRecordOffsetIndex = 0; |
| 240 | counterIndex < counterCount; |
| 241 | counterIndex++, eventRecordIndex++, eventRecordOffsetIndex++) |
| 242 | { |
| 243 | uint16_t counterUid = categoryCounters.at(counterIndex); |
| 244 | auto it = counters.find(counterUid); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 245 | const CounterPtr& counter = it->second; |
| 246 | |
| 247 | EventRecord& eventRecord = eventRecords.at(eventRecordIndex); |
| 248 | if (!CreateEventRecord(counter, eventRecord, errorMessage)) |
| 249 | { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | // Update the total size in words of the event records |
| 254 | eventRecordsSize += eventRecord.size(); |
| 255 | |
| 256 | // Add the event record offset to the event pointer table offset field |
| 257 | eventRecordOffsets[eventRecordOffsetIndex] = eventRecordsOffset; |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 258 | eventRecordsOffset += armnn::numeric_cast<uint32_t>(eventRecord.size() * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Category record word 3: |
| 262 | // 0:31 [32] name_offset (offset from the beginning of the category data pool to the name field) |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 263 | const uint32_t categoryRecordWord3 = armnn::numeric_cast<uint32_t>( |
| 264 | (3u + eventRecordOffsets.size()) * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 265 | |
| 266 | // Calculate the size in words of the category record |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 267 | const size_t categoryRecordSize = 3u +// The size of the fixed part (device + counter_set + event_count + |
| 268 | // reserved + event_pointer_table_offset + name_offset) |
| 269 | eventRecordOffsets.size() + // The size of the variable part ( |
| 270 | categoryNameBuffer.size() + // the event pointer table + the category name |
| 271 | eventRecordsSize; // including the null-terminator + the event records) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 272 | |
| 273 | // Allocate the necessary space for the category record |
| 274 | categoryRecord.resize(categoryRecordSize); |
| 275 | |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 276 | ARMNN_NO_CONVERSION_WARN_BEGIN |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 277 | // Create the category record |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 278 | categoryRecord[0] = categoryRecordWord1; // event_count + reserved |
| 279 | categoryRecord[1] = categoryRecordWord2; // event_pointer_table_offset |
| 280 | categoryRecord[2] = categoryRecordWord3; // name_offset |
| 281 | auto offset = categoryRecord.begin() + 3u; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 282 | std::copy(eventRecordOffsets.begin(), eventRecordOffsets.end(), offset); // event_pointer_table |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 283 | offset += eventRecordOffsets.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 284 | std::copy(categoryNameBuffer.begin(), categoryNameBuffer.end(), offset); // name |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 285 | offset += categoryNameBuffer.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 286 | for (const EventRecord& eventRecord : eventRecords) |
| 287 | { |
| 288 | std::copy(eventRecord.begin(), eventRecord.end(), offset); // event_record |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 289 | offset += eventRecord.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 290 | } |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 291 | ARMNN_NO_CONVERSION_WARN_END |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | bool SendCounterPacket::CreateDeviceRecord(const DevicePtr& device, |
| 297 | DeviceRecord& deviceRecord, |
| 298 | std::string& errorMessage) |
| 299 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 300 | ARMNN_ASSERT(device); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 301 | |
| 302 | uint16_t deviceUid = device->m_Uid; |
| 303 | const std::string& deviceName = device->m_Name; |
| 304 | uint16_t deviceCores = device->m_Cores; |
| 305 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 306 | ARMNN_ASSERT(!deviceName.empty()); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 307 | |
| 308 | // Device record word 0: |
| 309 | // 16:31 [16] uid: the unique identifier for the device |
| 310 | // 0:15 [16] cores: the number of individual streams of counters for one or more cores of some device |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 311 | const uint32_t deviceRecordWord0 = (static_cast<uint32_t>(deviceUid) << 16) | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 312 | (static_cast<uint32_t>(deviceCores)); |
| 313 | |
| 314 | // Device record word 1: |
| 315 | // 0:31 [32] name_offset: offset from the beginning of the device record pool to the name field |
Finn Williams | 6be1e9b | 2020-05-15 11:21:54 +0100 | [diff] [blame] | 316 | const uint32_t deviceRecordWord1 = 8u; // The offset is always eight here, as the name field is always |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 317 | // the first (and only) item in the pool and there are two device words |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 318 | |
| 319 | // Convert the device name into a SWTrace string |
| 320 | std::vector<uint32_t> deviceNameBuffer; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 321 | if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(deviceName, deviceNameBuffer)) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 322 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 323 | errorMessage = fmt::format("Cannot convert the name of device {} ({}) to an SWTrace string", |
| 324 | deviceUid, |
| 325 | deviceName); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | |
| 329 | // Calculate the size in words of the device record |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 330 | const size_t deviceRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 331 | deviceNameBuffer.size(); // The size of the variable part (the device name including |
| 332 | // the null-terminator) |
| 333 | |
| 334 | // Allocate the necessary space for the device record |
| 335 | deviceRecord.resize(deviceRecordSize); |
| 336 | |
| 337 | // Create the device record |
| 338 | deviceRecord[0] = deviceRecordWord0; // uid + core |
| 339 | deviceRecord[1] = deviceRecordWord1; // name_offset |
| 340 | auto offset = deviceRecord.begin() + 2u; |
| 341 | std::copy(deviceNameBuffer.begin(), deviceNameBuffer.end(), offset); // name |
| 342 | |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | bool SendCounterPacket::CreateCounterSetRecord(const CounterSetPtr& counterSet, |
| 347 | CounterSetRecord& counterSetRecord, |
| 348 | std::string& errorMessage) |
| 349 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 350 | ARMNN_ASSERT(counterSet); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 351 | |
| 352 | uint16_t counterSetUid = counterSet->m_Uid; |
| 353 | const std::string& counterSetName = counterSet->m_Name; |
| 354 | uint16_t counterSetCount = counterSet->m_Count; |
| 355 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 356 | ARMNN_ASSERT(!counterSetName.empty()); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 357 | |
| 358 | // Counter set record word 0: |
| 359 | // 16:31 [16] uid: the unique identifier for the counter_set |
| 360 | // 0:15 [16] count: the number of counters which can be active in this set at any one time |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 361 | const uint32_t counterSetRecordWord0 = (static_cast<uint32_t>(counterSetUid) << 16) | |
| 362 | (static_cast<uint32_t>(counterSetCount)); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 363 | |
| 364 | // Counter set record word 1: |
| 365 | // 0:31 [32] name_offset: offset from the beginning of the counter set pool to the name field |
Finn Williams | 6be1e9b | 2020-05-15 11:21:54 +0100 | [diff] [blame] | 366 | const uint32_t counterSetRecordWord1 = 8u; // The offset is always eight here, as the name field is always |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 367 | // the first (and only) item in the pool after the two counter set words |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 368 | |
| 369 | // Convert the device name into a SWTrace namestring |
| 370 | std::vector<uint32_t> counterSetNameBuffer; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 371 | if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(counterSet->m_Name, counterSetNameBuffer)) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 372 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 373 | errorMessage = fmt::format("Cannot convert the name of counter set {} ({}) to an SWTrace namestring", |
| 374 | counterSetUid, |
| 375 | counterSetName); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 376 | return false; |
| 377 | } |
| 378 | |
| 379 | // Calculate the size in words of the counter set record |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 380 | const size_t counterSetRecordSize = 2u + // The size of the fixed part (uid + cores + name_offset) |
| 381 | counterSetNameBuffer.size(); // The size of the variable part (the counter set |
| 382 | // name including the null-terminator) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 383 | |
| 384 | // Allocate the space for the counter set record |
| 385 | counterSetRecord.resize(counterSetRecordSize); |
| 386 | |
| 387 | // Create the counter set record |
| 388 | counterSetRecord[0] = counterSetRecordWord0; // uid + core |
| 389 | counterSetRecord[1] = counterSetRecordWord1; // name_offset |
| 390 | auto offset = counterSetRecord.begin() + 2u; |
| 391 | std::copy(counterSetNameBuffer.begin(), counterSetNameBuffer.end(), offset); // name |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | bool SendCounterPacket::CreateEventRecord(const CounterPtr& counter, |
| 397 | EventRecord& eventRecord, |
| 398 | std::string& errorMessage) |
| 399 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 400 | ARMNN_ASSERT(counter); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 401 | |
| 402 | uint16_t counterUid = counter->m_Uid; |
| 403 | uint16_t maxCounterUid = counter->m_MaxCounterUid; |
| 404 | uint16_t deviceUid = counter->m_DeviceUid; |
| 405 | uint16_t counterSetUid = counter->m_CounterSetUid; |
| 406 | uint16_t counterClass = counter->m_Class; |
| 407 | uint16_t counterInterpolation = counter->m_Interpolation; |
| 408 | double counterMultiplier = counter->m_Multiplier; |
| 409 | const std::string& counterName = counter->m_Name; |
| 410 | const std::string& counterDescription = counter->m_Description; |
| 411 | const std::string& counterUnits = counter->m_Units; |
| 412 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 413 | ARMNN_ASSERT(counterClass == 0 || counterClass == 1); |
| 414 | ARMNN_ASSERT(counterInterpolation == 0 || counterInterpolation == 1); |
| 415 | ARMNN_ASSERT(counterMultiplier); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 416 | |
| 417 | // Utils |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 418 | const size_t uint32_t_size = sizeof(uint32_t); |
| 419 | // eventRecordBlockSize is the size of the fixed part |
| 420 | // (counter_uid + max_counter_uid + device + |
| 421 | // counter_set + class + interpolation + |
| 422 | // multiplier + name_offset + description_offset + |
| 423 | // units_offset) |
| 424 | const size_t eventRecordBlockSize = 8u; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 425 | |
| 426 | // Event record word 0: |
| 427 | // 16:31 [16] max_counter_uid: if the device this event is associated with has more than one core and there |
| 428 | // is one of these counters per core this value will be set to |
| 429 | // (counter_uid + cores (from device_record)) - 1. |
| 430 | // If there is only a single core then this value will be the same as |
| 431 | // the counter_uid value |
| 432 | // 0:15 [16] count_uid: unique ID for the counter. Must be unique across all counters in all categories |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 433 | const uint32_t eventRecordWord0 = (static_cast<uint32_t>(maxCounterUid) << 16) | |
| 434 | (static_cast<uint32_t>(counterUid)); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 435 | |
| 436 | // Event record word 1: |
| 437 | // 16:31 [16] device: UID of the device this event is associated with. Set to zero if the event is NOT |
| 438 | // associated with a device |
| 439 | // 0:15 [16] counter_set: UID of the counter_set this event is associated with. Set to zero if the event |
| 440 | // is NOT associated with a counter_set |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 441 | const uint32_t eventRecordWord1 = (static_cast<uint32_t>(deviceUid) << 16) | |
| 442 | (static_cast<uint32_t>(counterSetUid)); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 443 | |
| 444 | // Event record word 2: |
| 445 | // 16:31 [16] class: type describing how to treat each data point in a stream of data points |
| 446 | // 0:15 [16] interpolation: type describing how to interpolate each data point in a stream of data points |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 447 | const uint32_t eventRecordWord2 = (static_cast<uint32_t>(counterClass) << 16) | |
| 448 | (static_cast<uint32_t>(counterInterpolation)); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 449 | |
| 450 | // Event record word 3-4: |
| 451 | // 0:63 [64] multiplier: internal data stream is represented as integer values, this allows scaling of |
| 452 | // those values as if they are fixed point numbers. Zero is not a valid value |
| 453 | uint32_t multiplier[2] = { 0u, 0u }; |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 454 | ARMNN_ASSERT(sizeof(counterMultiplier) == sizeof(multiplier)); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 455 | std::memcpy(multiplier, &counterMultiplier, sizeof(multiplier)); |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 456 | const uint32_t eventRecordWord3 = multiplier[0]; |
| 457 | const uint32_t eventRecordWord4 = multiplier[1]; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 458 | |
| 459 | // Event record word 5: |
| 460 | // 0:31 [32] name_offset: offset from the beginning of the event record pool to the name field |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 461 | const uint32_t eventRecordWord5 = static_cast<uint32_t>(eventRecordBlockSize * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 462 | |
| 463 | // Convert the counter name into a SWTrace string |
| 464 | std::vector<uint32_t> counterNameBuffer; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 465 | if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(counterName, counterNameBuffer)) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 466 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 467 | errorMessage = fmt::format("Cannot convert the name of counter {} (name: {}) to an SWTrace string", |
| 468 | counterUid, |
| 469 | counterName); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
| 473 | // Event record word 6: |
| 474 | // 0:31 [32] description_offset: offset from the beginning of the event record pool to the description field |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 475 | // The size of the name buffer in bytes |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 476 | uint32_t eventRecordWord6 = |
| 477 | static_cast<uint32_t>((counterNameBuffer.size() + eventRecordBlockSize) * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 478 | |
| 479 | // Convert the counter description into a SWTrace string |
| 480 | std::vector<uint32_t> counterDescriptionBuffer; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 481 | if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceCharPolicy>(counterDescription, counterDescriptionBuffer)) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 482 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 483 | errorMessage = fmt::format("Cannot convert the description of counter {} (description: {}) " |
| 484 | "to an SWTrace string", |
| 485 | counterUid, |
| 486 | counterName); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | |
| 490 | // Event record word 7: |
| 491 | // 0:31 [32] units_offset: (optional) offset from the beginning of the event record pool to the units field. |
| 492 | // An offset value of zero indicates this field is not provided |
| 493 | bool includeUnits = !counterUnits.empty(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 494 | // The size of the description buffer in bytes |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 495 | const uint32_t eventRecordWord7 = includeUnits ? |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 496 | eventRecordWord6 + |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 497 | armnn::numeric_cast<uint32_t>(counterDescriptionBuffer.size() |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 498 | * uint32_t_size) : |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 499 | 0; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 500 | |
| 501 | // Convert the counter units into a SWTrace namestring (optional) |
| 502 | std::vector<uint32_t> counterUnitsBuffer; |
| 503 | if (includeUnits) |
| 504 | { |
| 505 | // Convert the counter units into a SWTrace namestring |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 506 | if (!arm::pipe::StringToSwTraceString<arm::pipe::SwTraceNameCharPolicy>(counterUnits, counterUnitsBuffer)) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 507 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 508 | errorMessage = fmt::format("Cannot convert the units of counter {} (units: {}) to an SWTrace string", |
| 509 | counterUid, |
| 510 | counterName); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // Calculate the size in words of the event record |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 516 | const size_t eventRecordSize = eventRecordBlockSize + |
| 517 | counterNameBuffer.size() + // The size of the variable part (the counter name, |
| 518 | counterDescriptionBuffer.size() + // description and units |
| 519 | counterUnitsBuffer.size(); // including the null-terminator) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 520 | |
| 521 | // Allocate the space for the event record |
| 522 | eventRecord.resize(eventRecordSize); |
| 523 | |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 524 | ARMNN_NO_CONVERSION_WARN_BEGIN |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 525 | // Create the event record |
| 526 | eventRecord[0] = eventRecordWord0; // max_counter_uid + counter_uid |
| 527 | eventRecord[1] = eventRecordWord1; // device + counter_set |
| 528 | eventRecord[2] = eventRecordWord2; // class + interpolation |
| 529 | eventRecord[3] = eventRecordWord3; // multiplier |
| 530 | eventRecord[4] = eventRecordWord4; // multiplier |
| 531 | eventRecord[5] = eventRecordWord5; // name_offset |
| 532 | eventRecord[6] = eventRecordWord6; // description_offset |
| 533 | eventRecord[7] = eventRecordWord7; // units_offset |
| 534 | auto offset = eventRecord.begin() + 8u; |
| 535 | std::copy(counterNameBuffer.begin(), counterNameBuffer.end(), offset); // name |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 536 | offset += counterNameBuffer.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 537 | std::copy(counterDescriptionBuffer.begin(), counterDescriptionBuffer.end(), offset); // description |
| 538 | if (includeUnits) |
| 539 | { |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 540 | offset += counterDescriptionBuffer.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 541 | std::copy(counterUnitsBuffer.begin(), counterUnitsBuffer.end(), offset); // units |
| 542 | } |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 543 | ARMNN_NO_CONVERSION_WARN_END |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 544 | |
| 545 | return true; |
| 546 | } |
| 547 | |
| 548 | void SendCounterPacket::SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) |
| 549 | { |
| 550 | // Get the amount of data that needs to be put into the packet |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 551 | const uint16_t categoryCount = counterDirectory.GetCategoryCount(); |
| 552 | const uint16_t deviceCount = counterDirectory.GetDeviceCount(); |
| 553 | const uint16_t counterSetCount = counterDirectory.GetCounterSetCount(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 554 | |
| 555 | // Utils |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 556 | const size_t uint32_t_size = sizeof(uint32_t); |
| 557 | const size_t packetHeaderSize = 2u; |
| 558 | const size_t bodyHeaderSize = 6u; |
| 559 | const uint32_t bodyHeaderSizeBytes = bodyHeaderSize * uint32_t_size; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 560 | |
| 561 | // Initialize the offset for the pointer tables |
| 562 | uint32_t pointerTableOffset = 0; |
| 563 | |
| 564 | // -------------- |
| 565 | // Device records |
| 566 | // -------------- |
| 567 | |
| 568 | // Process device records |
| 569 | std::vector<DeviceRecord> deviceRecords(deviceCount); |
| 570 | const Devices& devices = counterDirectory.GetDevices(); |
| 571 | std::vector<uint32_t> deviceRecordOffsets(deviceCount, 0); // device_records_pointer_table |
| 572 | size_t deviceRecordsSize = 0; |
| 573 | size_t deviceIndex = 0; |
| 574 | size_t deviceRecordOffsetIndex = 0; |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 575 | |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 576 | pointerTableOffset = armnn::numeric_cast<uint32_t>(deviceCount * uint32_t_size + |
| 577 | counterSetCount * uint32_t_size + |
| 578 | categoryCount * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 579 | for (auto it = devices.begin(); it != devices.end(); it++) |
| 580 | { |
| 581 | const DevicePtr& device = it->second; |
| 582 | DeviceRecord& deviceRecord = deviceRecords.at(deviceIndex); |
| 583 | |
| 584 | std::string errorMessage; |
| 585 | if (!CreateDeviceRecord(device, deviceRecord, errorMessage)) |
| 586 | { |
| 587 | CancelOperationAndThrow<RuntimeException>(errorMessage); |
| 588 | } |
| 589 | |
| 590 | // Update the total size in words of the device records |
| 591 | deviceRecordsSize += deviceRecord.size(); |
| 592 | |
| 593 | // Add the device record offset to the device records pointer table offset field |
| 594 | deviceRecordOffsets[deviceRecordOffsetIndex] = pointerTableOffset; |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 595 | pointerTableOffset += armnn::numeric_cast<uint32_t>(deviceRecord.size() * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 596 | |
| 597 | deviceIndex++; |
| 598 | deviceRecordOffsetIndex++; |
| 599 | } |
| 600 | |
| 601 | // ------------------- |
| 602 | // Counter set records |
| 603 | // ------------------- |
| 604 | |
| 605 | // Process counter set records |
| 606 | std::vector<CounterSetRecord> counterSetRecords(counterSetCount); |
| 607 | const CounterSets& counterSets = counterDirectory.GetCounterSets(); |
| 608 | std::vector<uint32_t> counterSetRecordOffsets(counterSetCount, 0); // counter_set_records_pointer_table |
| 609 | size_t counterSetRecordsSize = 0; |
| 610 | size_t counterSetIndex = 0; |
| 611 | size_t counterSetRecordOffsetIndex = 0; |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 612 | |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 613 | pointerTableOffset -= armnn::numeric_cast<uint32_t>(deviceCount * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 614 | for (auto it = counterSets.begin(); it != counterSets.end(); it++) |
| 615 | { |
| 616 | const CounterSetPtr& counterSet = it->second; |
| 617 | CounterSetRecord& counterSetRecord = counterSetRecords.at(counterSetIndex); |
| 618 | |
| 619 | std::string errorMessage; |
| 620 | if (!CreateCounterSetRecord(counterSet, counterSetRecord, errorMessage)) |
| 621 | { |
| 622 | CancelOperationAndThrow<RuntimeException>(errorMessage); |
| 623 | } |
| 624 | |
| 625 | // Update the total size in words of the counter set records |
| 626 | counterSetRecordsSize += counterSetRecord.size(); |
| 627 | |
| 628 | // Add the counter set record offset to the counter set records pointer table offset field |
| 629 | counterSetRecordOffsets[counterSetRecordOffsetIndex] = pointerTableOffset; |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 630 | pointerTableOffset += armnn::numeric_cast<uint32_t>(counterSetRecord.size() * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 631 | |
| 632 | counterSetIndex++; |
| 633 | counterSetRecordOffsetIndex++; |
| 634 | } |
| 635 | |
| 636 | // ---------------- |
| 637 | // Category records |
| 638 | // ---------------- |
| 639 | |
| 640 | // Process category records |
| 641 | std::vector<CategoryRecord> categoryRecords(categoryCount); |
| 642 | const Categories& categories = counterDirectory.GetCategories(); |
| 643 | std::vector<uint32_t> categoryRecordOffsets(categoryCount, 0); // category_records_pointer_table |
| 644 | size_t categoryRecordsSize = 0; |
| 645 | size_t categoryIndex = 0; |
| 646 | size_t categoryRecordOffsetIndex = 0; |
Finn Williams | d44815f | 2020-05-01 13:25:55 +0100 | [diff] [blame] | 647 | |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 648 | pointerTableOffset -= armnn::numeric_cast<uint32_t>(counterSetCount * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 649 | for (auto it = categories.begin(); it != categories.end(); it++) |
| 650 | { |
| 651 | const CategoryPtr& category = *it; |
| 652 | CategoryRecord& categoryRecord = categoryRecords.at(categoryIndex); |
| 653 | |
| 654 | std::string errorMessage; |
| 655 | if (!CreateCategoryRecord(category, counterDirectory.GetCounters(), categoryRecord, errorMessage)) |
| 656 | { |
| 657 | CancelOperationAndThrow<RuntimeException>(errorMessage); |
| 658 | } |
| 659 | |
| 660 | // Update the total size in words of the category records |
| 661 | categoryRecordsSize += categoryRecord.size(); |
| 662 | |
| 663 | // Add the category record offset to the category records pointer table offset field |
| 664 | categoryRecordOffsets[categoryRecordOffsetIndex] = pointerTableOffset; |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 665 | pointerTableOffset += armnn::numeric_cast<uint32_t>(categoryRecord.size() * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 666 | |
| 667 | categoryIndex++; |
| 668 | categoryRecordOffsetIndex++; |
| 669 | } |
| 670 | |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 671 | // Calculate the length in words of the counter directory packet's data (excludes the packet header size) |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 672 | const size_t counterDirectoryPacketDataLength = |
| 673 | bodyHeaderSize + // The size of the body header |
| 674 | deviceRecordOffsets.size() + // The size of the device records pointer table |
| 675 | counterSetRecordOffsets.size() + // The size of counter set pointer table |
| 676 | categoryRecordOffsets.size() + // The size of category records pointer table |
| 677 | deviceRecordsSize + // The total size of the device records |
| 678 | counterSetRecordsSize + // The total size of the counter set records |
| 679 | categoryRecordsSize; // The total size of the category records |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 680 | |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 681 | // Calculate the size in words of the counter directory packet (the data length plus the packet header size) |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 682 | const size_t counterDirectoryPacketSize = packetHeaderSize + // The size of the packet header |
| 683 | counterDirectoryPacketDataLength; // The data length |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 684 | |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 685 | // Allocate the necessary space for the counter directory packet |
| 686 | std::vector<uint32_t> counterDirectoryPacket(counterDirectoryPacketSize, 0); |
| 687 | |
| 688 | // ------------- |
| 689 | // Packet header |
| 690 | // ------------- |
| 691 | |
| 692 | // Packet header word 0: |
| 693 | // 26:31 [6] packet_family: control Packet Family |
| 694 | // 16:25 [10] packet_id: packet identifier |
| 695 | // 8:15 [8] reserved: all zeros |
| 696 | // 0:7 [8] reserved: all zeros |
| 697 | uint32_t packetFamily = 0; |
| 698 | uint32_t packetId = 2; |
| 699 | uint32_t packetHeaderWord0 = ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16); |
| 700 | |
| 701 | // Packet header word 1: |
| 702 | // 0:31 [32] data_length: length of data, in bytes |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 703 | uint32_t packetHeaderWord1 = armnn::numeric_cast<uint32_t>( |
| 704 | counterDirectoryPacketDataLength * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 705 | |
| 706 | // Create the packet header |
| 707 | uint32_t packetHeader[2] |
| 708 | { |
| 709 | packetHeaderWord0, // packet_family + packet_id + reserved + reserved |
| 710 | packetHeaderWord1 // data_length |
| 711 | }; |
| 712 | |
| 713 | // ----------- |
| 714 | // Body header |
| 715 | // ----------- |
| 716 | |
| 717 | // Body header word 0: |
| 718 | // 16:31 [16] device_records_count: number of entries in the device_records_pointer_table |
| 719 | // 0:15 [16] reserved: all zeros |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 720 | const uint32_t bodyHeaderWord0 = static_cast<uint32_t>(deviceCount) << 16; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 721 | |
| 722 | // Body header word 1: |
| 723 | // 0:31 [32] device_records_pointer_table_offset: offset to the device_records_pointer_table |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 724 | const uint32_t bodyHeaderWord1 = bodyHeaderSizeBytes; // The offset is always the bodyHeaderSize, |
| 725 | // as the device record pointer table field |
| 726 | // is always the first item in the pool |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 727 | |
| 728 | // Body header word 2: |
| 729 | // 16:31 [16] counter_set_count: number of entries in the counter_set_pointer_table |
| 730 | // 0:15 [16] reserved: all zeros |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 731 | const uint32_t bodyHeaderWord2 = static_cast<uint32_t>(counterSetCount) << 16; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 732 | |
| 733 | // Body header word 3: |
| 734 | // 0:31 [32] counter_set_pointer_table_offset: offset to the counter_set_pointer_table |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 735 | const uint32_t bodyHeaderWord3 = armnn::numeric_cast<uint32_t>(deviceRecordOffsets.size() * |
| 736 | uint32_t_size + // The size of the |
| 737 | bodyHeaderSizeBytes); // device records pointer table |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 738 | |
| 739 | // Body header word 4: |
| 740 | // 16:31 [16] categories_count: number of entries in the categories_pointer_table |
| 741 | // 0:15 [16] reserved: all zeros |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 742 | const uint32_t bodyHeaderWord4 = static_cast<uint32_t>(categoryCount) << 16; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 743 | |
| 744 | // Body header word 3: |
| 745 | // 0:31 [32] categories_pointer_table_offset: offset to the categories_pointer_table |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 746 | const uint32_t bodyHeaderWord5 = |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 747 | armnn::numeric_cast<uint32_t>( |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 748 | deviceRecordOffsets.size() * uint32_t_size + // The size of the device records |
| 749 | counterSetRecordOffsets.size() * uint32_t_size // pointer table, plus the size of |
| 750 | + bodyHeaderSizeBytes); // the counter set pointer table |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 751 | |
| 752 | // Create the body header |
Finn Williams | 985fecf | 2020-04-30 11:06:43 +0100 | [diff] [blame] | 753 | const uint32_t bodyHeader[bodyHeaderSize] |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 754 | { |
| 755 | bodyHeaderWord0, // device_records_count + reserved |
| 756 | bodyHeaderWord1, // device_records_pointer_table_offset |
| 757 | bodyHeaderWord2, // counter_set_count + reserved |
| 758 | bodyHeaderWord3, // counter_set_pointer_table_offset |
| 759 | bodyHeaderWord4, // categories_count + reserved |
| 760 | bodyHeaderWord5 // categories_pointer_table_offset |
| 761 | }; |
| 762 | |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 763 | ARMNN_NO_CONVERSION_WARN_BEGIN |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 764 | // Create the counter directory packet |
| 765 | auto counterDirectoryPacketOffset = counterDirectoryPacket.begin(); |
| 766 | // packet_header |
| 767 | std::copy(packetHeader, packetHeader + packetHeaderSize, counterDirectoryPacketOffset); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 768 | counterDirectoryPacketOffset += packetHeaderSize; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 769 | // body_header |
| 770 | std::copy(bodyHeader, bodyHeader + bodyHeaderSize, counterDirectoryPacketOffset); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 771 | counterDirectoryPacketOffset += bodyHeaderSize; |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 772 | // device_records_pointer_table |
| 773 | std::copy(deviceRecordOffsets.begin(), deviceRecordOffsets.end(), counterDirectoryPacketOffset); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 774 | counterDirectoryPacketOffset += deviceRecordOffsets.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 775 | // counter_set_pointer_table |
| 776 | std::copy(counterSetRecordOffsets.begin(), counterSetRecordOffsets.end(), counterDirectoryPacketOffset); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 777 | counterDirectoryPacketOffset += counterSetRecordOffsets.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 778 | // category_pointer_table |
| 779 | std::copy(categoryRecordOffsets.begin(), categoryRecordOffsets.end(), counterDirectoryPacketOffset); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 780 | counterDirectoryPacketOffset += categoryRecordOffsets.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 781 | // device_records |
| 782 | for (const DeviceRecord& deviceRecord : deviceRecords) |
| 783 | { |
| 784 | std::copy(deviceRecord.begin(), deviceRecord.end(), counterDirectoryPacketOffset); // device_record |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 785 | counterDirectoryPacketOffset += deviceRecord.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 786 | } |
| 787 | // counter_set_records |
| 788 | for (const CounterSetRecord& counterSetRecord : counterSetRecords) |
| 789 | { |
| 790 | std::copy(counterSetRecord.begin(), counterSetRecord.end(), counterDirectoryPacketOffset); // counter_set_record |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 791 | counterDirectoryPacketOffset += counterSetRecord.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 792 | } |
| 793 | // category_records |
| 794 | for (const CategoryRecord& categoryRecord : categoryRecords) |
| 795 | { |
| 796 | std::copy(categoryRecord.begin(), categoryRecord.end(), counterDirectoryPacketOffset); // category_record |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 797 | counterDirectoryPacketOffset += categoryRecord.size(); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 798 | } |
Matteo Martincigh | f74ff2f | 2019-09-24 11:38:32 +0100 | [diff] [blame] | 799 | ARMNN_NO_CONVERSION_WARN_END |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 800 | |
| 801 | // Calculate the total size in bytes of the counter directory packet |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 802 | uint32_t totalSize = armnn::numeric_cast<uint32_t>(counterDirectoryPacketSize * uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 803 | |
| 804 | // Reserve space in the buffer for the packet |
| 805 | uint32_t reserved = 0; |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 806 | IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 807 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 808 | if (writeBuffer == nullptr || reserved < totalSize) |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 809 | { |
| 810 | CancelOperationAndThrow<BufferExhaustion>( |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 811 | writeBuffer, |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 812 | fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize)); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | // Offset for writing to the buffer |
| 816 | uint32_t offset = 0; |
| 817 | |
| 818 | // Write the counter directory packet to the buffer |
| 819 | for (uint32_t counterDirectoryPacketWord : counterDirectoryPacket) |
| 820 | { |
| 821 | WriteUint32(writeBuffer, offset, counterDirectoryPacketWord); |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 822 | offset += armnn::numeric_cast<uint32_t>(uint32_t_size); |
Matteo Martincigh | 42f9d9e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 825 | m_BufferManager.Commit(writeBuffer, totalSize); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 828 | void SendCounterPacket::SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 829 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 830 | uint32_t uint16_t_size = sizeof(uint16_t); |
| 831 | uint32_t uint32_t_size = sizeof(uint32_t); |
| 832 | uint32_t uint64_t_size = sizeof(uint64_t); |
| 833 | |
Jim Flynn | fc36562 | 2019-12-04 10:07:20 +0000 | [diff] [blame] | 834 | uint32_t packetFamily = 3; |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 835 | uint32_t packetClass = 0; |
| 836 | uint32_t packetType = 0; |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 837 | uint32_t headerSize = 2 * uint32_t_size; |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 838 | uint32_t bodySize = uint64_t_size + armnn::numeric_cast<uint32_t>(values.size()) * (uint16_t_size + uint32_t_size); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 839 | uint32_t totalSize = headerSize + bodySize; |
| 840 | uint32_t offset = 0; |
| 841 | uint32_t reserved = 0; |
| 842 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 843 | IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 844 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 845 | if (writeBuffer == nullptr || reserved < totalSize) |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 846 | { |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 847 | CancelOperationAndThrow<BufferExhaustion>( |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 848 | writeBuffer, |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 849 | fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize)); |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | // Create header. |
| 853 | WriteUint32(writeBuffer, |
| 854 | offset, |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 855 | ((packetFamily & 0x0000003F) << 26) | |
| 856 | ((packetClass & 0x0000007F) << 19) | |
| 857 | ((packetType & 0x00000007) << 16)); |
| 858 | offset += uint32_t_size; |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 859 | WriteUint32(writeBuffer, offset, bodySize); |
| 860 | |
| 861 | // Copy captured Timestamp. |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 862 | offset += uint32_t_size; |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 863 | WriteUint64(writeBuffer, offset, timestamp); |
| 864 | |
| 865 | // Copy selectedCounterIds. |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 866 | offset += uint64_t_size; |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 867 | for (const auto& pair: values) |
| 868 | { |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 869 | WriteUint16(writeBuffer, offset, pair.counterId); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 870 | offset += uint16_t_size; |
Finn Williams | 032bc74 | 2020-02-12 11:02:34 +0000 | [diff] [blame] | 871 | WriteUint32(writeBuffer, offset, pair.counterValue); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 872 | offset += uint32_t_size; |
Francis Murtagh | 3a16198 | 2019-09-04 15:25:02 +0100 | [diff] [blame] | 873 | } |
| 874 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 875 | m_BufferManager.Commit(writeBuffer, totalSize); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | void SendCounterPacket::SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 879 | const std::vector<uint16_t>& selectedCounterIds) |
| 880 | { |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 881 | uint32_t uint16_t_size = sizeof(uint16_t); |
| 882 | uint32_t uint32_t_size = sizeof(uint32_t); |
| 883 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 884 | uint32_t packetFamily = 0; |
| 885 | uint32_t packetId = 4; |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 886 | uint32_t headerSize = 2 * uint32_t_size; |
Matthew Sloyan | 371b70e | 2020-09-11 10:14:57 +0100 | [diff] [blame] | 887 | uint32_t bodySize = uint32_t_size + armnn::numeric_cast<uint32_t>(selectedCounterIds.size()) * uint16_t_size; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 888 | uint32_t totalSize = headerSize + bodySize; |
| 889 | uint32_t offset = 0; |
| 890 | uint32_t reserved = 0; |
| 891 | |
Matteo Martincigh | 2ffcc41 | 2019-11-05 11:47:40 +0000 | [diff] [blame] | 892 | IPacketBufferPtr writeBuffer = m_BufferManager.Reserve(totalSize, reserved); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 893 | |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 894 | if (writeBuffer == nullptr || reserved < totalSize) |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 895 | { |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 896 | CancelOperationAndThrow<BufferExhaustion>( |
Narumol Prangnawarat | 0ec068f | 2019-09-30 16:20:20 +0100 | [diff] [blame] | 897 | writeBuffer, |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 898 | fmt::format("No space left in buffer. Unable to reserve ({}) bytes.", totalSize)); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | // Create header. |
| 902 | WriteUint32(writeBuffer, offset, ((packetFamily & 0x3F) << 26) | ((packetId & 0x3FF) << 16)); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 903 | offset += uint32_t_size; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 904 | WriteUint32(writeBuffer, offset, bodySize); |
| 905 | |
| 906 | // Copy capturePeriod. |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 907 | offset += uint32_t_size; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 908 | WriteUint32(writeBuffer, offset, capturePeriod); |
| 909 | |
| 910 | // Copy selectedCounterIds. |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 911 | offset += uint32_t_size; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 912 | for(const uint16_t& id: selectedCounterIds) |
| 913 | { |
| 914 | WriteUint16(writeBuffer, offset, id); |
Matteo Martincigh | 8d9590e | 2019-10-15 09:35:29 +0100 | [diff] [blame] | 915 | offset += uint16_t_size; |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Narumol Prangnawarat | 404b275 | 2019-09-24 17:23:16 +0100 | [diff] [blame] | 918 | m_BufferManager.Commit(writeBuffer, totalSize); |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 919 | } |
| 920 | |
Ferran Balaguer | 7388217 | 2019-09-02 16:39:42 +0100 | [diff] [blame] | 921 | } // namespace profiling |
| 922 | |
Matteo Martincigh | 149528e | 2019-09-05 12:02:04 +0100 | [diff] [blame] | 923 | } // namespace armnn |