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