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