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