Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 5 | #include "DirectoryCaptureCommandHandler.hpp" |
| 6 | |
| 7 | #include <armnn/BackendId.hpp> |
| 8 | #include "ProfilingUtils.hpp" |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 9 | |
Finn Williams | e63a026 | 2019-10-22 10:30:49 +0100 | [diff] [blame] | 10 | #include <atomic> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 11 | #include <iostream> |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 12 | |
| 13 | namespace armnn |
| 14 | { |
| 15 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 16 | namespace profiling |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 17 | { |
| 18 | |
| 19 | // Utils |
| 20 | uint32_t uint16_t_size = sizeof(uint16_t); |
| 21 | uint32_t uint32_t_size = sizeof(uint32_t); |
| 22 | |
| 23 | void DirectoryCaptureCommandHandler::ParseData(const armnn::profiling::Packet& packet) |
| 24 | { |
| 25 | uint16_t categoryRecordCount; |
| 26 | uint16_t counterSetRecordCount; |
| 27 | uint16_t deviceRecordCount; |
| 28 | |
| 29 | uint32_t offset = 0; |
| 30 | |
| 31 | if (packet.GetLength() < 8) |
| 32 | { |
| 33 | std::cout << "Counter directory packet received." << std::endl; |
| 34 | return; |
| 35 | } |
| 36 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 37 | const unsigned char* data = packet.GetData(); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 38 | // Body header word 0: |
| 39 | // 0:15 [16] reserved: all zeros |
| 40 | offset += uint16_t_size; |
| 41 | // 16:31 [16] device_records_count: number of entries in the device_records_pointer_table |
| 42 | deviceRecordCount = profiling::ReadUint16(data, offset); |
| 43 | offset += uint16_t_size; |
| 44 | |
| 45 | // Body header word 1: |
| 46 | // 0:31 [32] device_records_pointer_table_offset: offset to the device_records_pointer_table |
| 47 | // The offset is always zero here, as the device record pointer table field is always the first item in the pool |
| 48 | offset += uint32_t_size; |
| 49 | |
| 50 | // Body header word 2: |
| 51 | // 0:15 [16] reserved: all zeros |
| 52 | offset += uint16_t_size; |
| 53 | // 16:31 [16] counter_set_count: number of entries in the counter_set_pointer_table |
| 54 | counterSetRecordCount = profiling::ReadUint16(data, offset); |
| 55 | offset += uint16_t_size; |
| 56 | |
| 57 | // Body header word 3: |
| 58 | // 0:31 [32] counter_set_pointer_table_offset: offset to the counter_set_pointer_table |
| 59 | // counterPointerTableSetOffset = profiling::ReadUint32(data, offset); |
| 60 | offset += uint32_t_size; |
| 61 | |
| 62 | // Body header word 4: |
| 63 | // 0:15 [16] reserved: all zeros |
| 64 | offset += uint16_t_size; |
| 65 | // 16:31 [16] categories_count: number of entries in the categories_pointer_table |
| 66 | categoryRecordCount = profiling::ReadUint16(data, offset); |
| 67 | offset += uint16_t_size; |
| 68 | |
| 69 | // Body header word 5: |
| 70 | // 0:31 [32] categories_pointer_table_offset: offset to the categories_pointer_table |
| 71 | // categoriesPointerTableOffset = profiling::ReadUint32(data, offset); |
| 72 | offset += uint32_t_size; |
| 73 | |
| 74 | std::vector<uint32_t> deviceRecordOffsets(deviceRecordCount); |
| 75 | std::vector<uint32_t> counterSetOffsets(counterSetRecordCount); |
| 76 | std::vector<uint32_t> categoryOffsets(categoryRecordCount); |
| 77 | |
| 78 | for (uint32_t i = 0; i < deviceRecordCount; ++i) |
| 79 | { |
| 80 | deviceRecordOffsets[i] = profiling::ReadUint32(data, offset); |
| 81 | offset += uint32_t_size; |
| 82 | } |
| 83 | |
| 84 | for (uint32_t i = 0; i < counterSetRecordCount; ++i) |
| 85 | { |
| 86 | counterSetOffsets[i] = profiling::ReadUint32(data, offset); |
| 87 | offset += uint32_t_size; |
| 88 | } |
| 89 | |
| 90 | for (uint32_t i = 0; i < categoryRecordCount; ++i) |
| 91 | { |
| 92 | categoryOffsets[i] = profiling::ReadUint32(data, offset); |
| 93 | offset += uint32_t_size; |
| 94 | } |
| 95 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 96 | for (uint32_t deviceIndex = 0; deviceIndex < deviceRecordCount; ++deviceIndex) |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 97 | { |
| 98 | uint32_t deviceRecordOffset = offset + deviceRecordOffsets[deviceIndex]; |
| 99 | // Device record word 0: |
| 100 | // 0:15 [16] cores: the number of individual streams of counters for one or more cores of some device |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 101 | uint16_t deviceCores = profiling::ReadUint16(data, deviceRecordOffset); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 102 | // 16:31 [16] deviceUid: the unique identifier for the device |
| 103 | deviceRecordOffset += uint16_t_size; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 104 | uint16_t deviceUid = profiling::ReadUint16(data, deviceRecordOffset); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 105 | deviceRecordOffset += uint16_t_size; |
| 106 | |
| 107 | // Device record word 1: |
| 108 | // Offset from the beginning of the device record pool to the name field. |
| 109 | uint32_t nameOffset = profiling::ReadUint32(data, deviceRecordOffset); |
| 110 | |
| 111 | deviceRecordOffset += uint32_t_size; |
| 112 | deviceRecordOffset += uint32_t_size; |
| 113 | deviceRecordOffset += nameOffset; |
| 114 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 115 | const std::string& deviceName = GetStringNameFromBuffer(data, deviceRecordOffset); |
| 116 | const Device* registeredDevice = m_CounterDirectory.RegisterDevice(deviceName, deviceCores); |
| 117 | m_UidTranslation[registeredDevice->m_Uid] = deviceUid; |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 120 | for (uint32_t counterSetIndex = 0; counterSetIndex < counterSetRecordCount; ++counterSetIndex) |
| 121 | { |
| 122 | uint32_t counterSetOffset = offset + counterSetOffsets[counterSetIndex]; |
| 123 | |
| 124 | // Counter set record word 0: |
| 125 | // 0:15 [16] count: the number of counters which can be active in this set at any one time |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 126 | uint16_t counterSetCount = profiling::ReadUint16(data, counterSetOffset); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 127 | counterSetOffset += uint16_t_size; |
| 128 | |
| 129 | // 16:31 [16] deviceUid: the unique identifier for the counter_set |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 130 | uint16_t counterSetUid = profiling::ReadUint16(data, counterSetOffset); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 131 | counterSetOffset += uint16_t_size; |
| 132 | |
| 133 | // Counter set record word 1: |
| 134 | // 0:31 [32] name_offset: offset from the beginning of the counter set pool to the name field |
| 135 | // The offset is always zero here, as the name field is always the first (and only) item in the pool |
| 136 | counterSetOffset += uint32_t_size; |
| 137 | counterSetOffset += uint32_t_size; |
| 138 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 139 | auto counterSet = |
| 140 | m_CounterDirectory.RegisterCounterSet(GetStringNameFromBuffer(data, counterSetOffset), counterSetCount); |
| 141 | m_UidTranslation[counterSet->m_Uid] = counterSetUid; |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 142 | } |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 143 | ReadCategoryRecords(data, offset, categoryOffsets); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 146 | void DirectoryCaptureCommandHandler::ReadCategoryRecords(const unsigned char* const data, |
| 147 | uint32_t offset, |
| 148 | std::vector<uint32_t> categoryOffsets) |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 149 | { |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 150 | uint32_t categoryRecordCount = static_cast<uint32_t>(categoryOffsets.size()); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 151 | |
| 152 | for (uint32_t categoryIndex = 0; categoryIndex < categoryRecordCount; ++categoryIndex) |
| 153 | { |
| 154 | uint32_t categoryRecordOffset = offset + categoryOffsets[categoryIndex]; |
| 155 | |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 156 | // Category record word 1: |
| 157 | // 0:15 Reserved, value 0x0000. |
| 158 | categoryRecordOffset += uint16_t_size; |
| 159 | // 16:31 Number of events belonging to this category. |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 160 | uint32_t eventCount = profiling::ReadUint16(data, categoryRecordOffset); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 161 | categoryRecordOffset += uint16_t_size; |
| 162 | |
| 163 | // Category record word 2 |
| 164 | // 0:31 Offset from the beginning of the category data pool to the event_pointer_table |
| 165 | uint32_t eventPointerTableOffset = profiling::ReadUint32(data, categoryRecordOffset); |
| 166 | categoryRecordOffset += uint32_t_size; |
| 167 | |
| 168 | // Category record word 3 |
| 169 | // 0:31 Offset from the beginning of the category data pool to the name field. |
| 170 | uint32_t nameOffset = profiling::ReadUint32(data, categoryRecordOffset); |
| 171 | categoryRecordOffset += uint32_t_size; |
| 172 | |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 173 | std::vector<uint32_t> eventRecordsOffsets(eventCount); |
| 174 | |
| 175 | eventPointerTableOffset += categoryRecordOffset; |
| 176 | |
| 177 | for (uint32_t eventIndex = 0; eventIndex < eventCount; ++eventIndex) |
| 178 | { |
| 179 | eventRecordsOffsets[eventIndex] = |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 180 | profiling::ReadUint32(data, eventPointerTableOffset + uint32_t_size * eventIndex); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 183 | const std::vector<CounterDirectoryEventRecord>& eventRecords = |
| 184 | ReadEventRecords(data, categoryRecordOffset, eventRecordsOffsets); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 185 | categoryRecordOffset += uint32_t_size; |
| 186 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 187 | const Category* category = m_CounterDirectory.RegisterCategory( |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 188 | GetStringNameFromBuffer(data, categoryRecordOffset + nameOffset)); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 189 | for (auto& counter : eventRecords) |
| 190 | { |
Keith Davis | e394bd9 | 2019-12-02 15:12:19 +0000 | [diff] [blame] | 191 | const Counter* registeredCounter = m_CounterDirectory.RegisterCounter(armnn::profiling::BACKEND_ID, |
| 192 | counter.m_CounterUid, |
| 193 | category->m_Name, |
| 194 | counter.m_CounterClass, |
| 195 | counter.m_CounterInterpolation, |
| 196 | counter.m_CounterMultiplier, |
| 197 | counter.m_CounterName, |
| 198 | counter.m_CounterDescription, |
| 199 | counter.m_CounterUnits); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 200 | m_UidTranslation[registeredCounter->m_Uid] = counter.m_CounterUid; |
| 201 | } |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 202 | } |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 205 | std::vector<CounterDirectoryEventRecord> DirectoryCaptureCommandHandler::ReadEventRecords( |
| 206 | const unsigned char* data, uint32_t offset, std::vector<uint32_t> eventRecordsOffsets) |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 207 | { |
| 208 | uint32_t eventCount = static_cast<uint32_t>(eventRecordsOffsets.size()); |
| 209 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 210 | std::vector<CounterDirectoryEventRecord> eventRecords(eventCount); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 211 | for (unsigned long i = 0; i < eventCount; ++i) |
| 212 | { |
| 213 | uint32_t eventRecordOffset = eventRecordsOffsets[i] + offset; |
| 214 | |
| 215 | // Event record word 0: |
| 216 | // 0:15 [16] count_uid: unique ID for the counter. Must be unique across all counters in all categories |
| 217 | eventRecords[i].m_CounterUid = profiling::ReadUint16(data, eventRecordOffset); |
| 218 | eventRecordOffset += uint16_t_size; |
| 219 | // 16:31 [16] max_counter_uid: if the device this event is associated with has more than one core and there |
| 220 | // is one of these counters per core this value will be set to |
| 221 | // (counter_uid + cores (from device_record)) - 1. |
| 222 | // If there is only a single core then this value will be the same as |
| 223 | // the counter_uid value |
| 224 | eventRecords[i].m_MaxCounterUid = profiling::ReadUint16(data, eventRecordOffset); |
| 225 | eventRecordOffset += uint16_t_size; |
| 226 | |
| 227 | // Event record word 1: |
| 228 | // 0:15 [16] counter_set: UID of the counter_set this event is associated with. Set to zero if the event |
| 229 | // is NOT associated with a counter_set |
| 230 | eventRecords[i].m_DeviceUid = profiling::ReadUint16(data, eventRecordOffset); |
| 231 | eventRecordOffset += uint16_t_size; |
| 232 | |
| 233 | // 16:31 [16] device: UID of the device this event is associated with. Set to zero if the event is NOT |
| 234 | // associated with a device |
| 235 | eventRecords[i].m_CounterSetUid = profiling::ReadUint16(data, eventRecordOffset); |
| 236 | eventRecordOffset += uint16_t_size; |
| 237 | |
| 238 | // Event record word 2: |
| 239 | // 0:15 [16] interpolation: type describing how to interpolate each data point in a stream of data points |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 240 | eventRecords[i].m_CounterClass = profiling::ReadUint16(data, eventRecordOffset); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 241 | eventRecordOffset += uint16_t_size; |
| 242 | |
| 243 | // 16:31 [16] class: type describing how to treat each data point in a stream of data points |
| 244 | eventRecords[i].m_CounterInterpolation = profiling::ReadUint16(data, eventRecordOffset); |
| 245 | eventRecordOffset += uint16_t_size; |
| 246 | |
| 247 | // Event record word 3-4: |
| 248 | // 0:63 [64] multiplier: internal data stream is represented as integer values, this allows scaling of |
| 249 | // those values as if they are fixed point numbers. Zero is not a valid value |
| 250 | uint32_t multiplier[2] = { 0u, 0u }; |
| 251 | |
| 252 | multiplier[0] = profiling::ReadUint32(data, eventRecordOffset); |
| 253 | eventRecordOffset += uint32_t_size; |
| 254 | multiplier[1] = profiling::ReadUint32(data, eventRecordOffset); |
| 255 | eventRecordOffset += uint32_t_size; |
| 256 | |
| 257 | std::memcpy(&eventRecords[i].m_CounterMultiplier, &multiplier, sizeof(multiplier)); |
| 258 | |
| 259 | // Event record word 5: |
| 260 | // 0:31 [32] name_eventRecordOffset: eventRecordOffset from the |
| 261 | // beginning of the event record pool to the name field |
| 262 | // The eventRecordOffset is always zero here, as the name field is always the first item in the pool |
| 263 | eventRecordOffset += uint32_t_size; |
| 264 | |
| 265 | // Event record word 6: |
| 266 | // 0:31 [32] description_eventRecordOffset: eventRecordOffset from the |
| 267 | // beginning of the event record pool to the description field |
| 268 | // The size of the name buffer in bytes |
| 269 | uint32_t descriptionOffset = profiling::ReadUint32(data, eventRecordOffset); |
| 270 | eventRecordOffset += uint32_t_size; |
| 271 | |
| 272 | // Event record word 7: |
| 273 | // 0:31 [32] units_eventRecordOffset: (optional) eventRecordOffset from the |
| 274 | // beginning of the event record pool to the units field. |
| 275 | // An eventRecordOffset value of zero indicates this field is not provided |
| 276 | uint32_t unitsOffset = profiling::ReadUint32(data, eventRecordOffset); |
| 277 | eventRecordOffset += uint32_t_size; |
| 278 | eventRecordOffset += uint32_t_size; |
| 279 | |
| 280 | eventRecords[i].m_CounterName = GetStringNameFromBuffer(data, eventRecordOffset); |
| 281 | |
| 282 | eventRecords[i].m_CounterDescription = GetStringNameFromBuffer(data, eventRecordOffset + descriptionOffset); |
| 283 | |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame^] | 284 | eventRecords[i].m_CounterUnits = unitsOffset == 0 ? Optional<std::string>() : |
| 285 | GetStringNameFromBuffer(data, eventRecordOffset + unitsOffset); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return eventRecords; |
| 289 | } |
| 290 | |
| 291 | void DirectoryCaptureCommandHandler::operator()(const profiling::Packet& packet) |
| 292 | { |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 293 | if (!m_QuietOperation) // Are we supposed to print to stdout? |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 294 | { |
| 295 | std::cout << "Counter directory packet received." << std::endl; |
| 296 | } |
| 297 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 298 | // The ArmNN counter directory is static per ArmNN instance. Ensure we don't parse it a second time. |
| 299 | if (!ParsedCounterDirectory()) |
| 300 | { |
| 301 | ParseData(packet); |
| 302 | m_AlreadyParsed = true; |
| 303 | } |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 304 | |
| 305 | if (!m_QuietOperation) |
| 306 | { |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 307 | armnn::profiling::PrintCounterDirectory(m_CounterDirectory); |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 311 | const ICounterDirectory& DirectoryCaptureCommandHandler::GetCounterDirectory() const |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 312 | { |
| 313 | return m_CounterDirectory; |
| 314 | } |
| 315 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 316 | std::string DirectoryCaptureCommandHandler::GetStringNameFromBuffer(const unsigned char* const data, uint32_t offset) |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 317 | { |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 318 | std::string deviceName; |
Rob Hughes | 270233f | 2019-11-13 11:53:48 +0000 | [diff] [blame] | 319 | uint8_t nextChar = profiling::ReadUint8(data, offset); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 320 | |
| 321 | while (isprint(nextChar)) |
| 322 | { |
| 323 | deviceName += static_cast<char>(nextChar); |
| 324 | offset++; |
| 325 | nextChar = profiling::ReadUint8(data, offset); |
| 326 | } |
| 327 | |
| 328 | return deviceName; |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 331 | } // namespace profiling |
Finn Williams | 15db745 | 2019-10-15 14:22:13 +0100 | [diff] [blame] | 332 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 333 | } // namespace armnn |