Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 1 | // |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Jim Flynn | 3e9bc19 | 2022-03-23 23:01:26 +0000 | [diff] [blame] | 8 | #include <client/src/IProfilingConnectionFactory.hpp> |
| 9 | #include <client/src/ProfilingService.hpp> |
| 10 | #include <client/src/ProfilingUtils.hpp> |
| 11 | #include <client/src/SendCounterPacket.hpp> |
| 12 | #include <client/src/SendThread.hpp> |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 13 | |
Jim Flynn | 9c85b41 | 2022-03-16 00:27:43 +0000 | [diff] [blame] | 14 | #include <armnn/BackendId.hpp> |
| 15 | #include <armnn/profiling/ArmNNProfiling.hpp> |
| 16 | |
Jim Flynn | 2776183 | 2022-03-20 21:52:17 +0000 | [diff] [blame] | 17 | #include <client/include/Holder.hpp> |
| 18 | #include <client/include/IProfilingServiceStatus.hpp> |
| 19 | |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 20 | #include <common/include/Assert.hpp> |
Jim Flynn | c454ac9 | 2022-03-16 18:43:18 +0000 | [diff] [blame] | 21 | #include <common/include/CommonProfilingUtils.hpp> |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 22 | #include <common/include/IgnoreUnused.hpp> |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 23 | #include <common/include/NumericCast.hpp> |
Jim Flynn | decd08b | 2022-03-13 22:35:46 +0000 | [diff] [blame] | 24 | #include <common/include/Optional.hpp> |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 25 | #include <common/include/ProfilingException.hpp> |
Nikhil Raj | 5b1bcc9 | 2021-06-08 12:31:50 +0100 | [diff] [blame] | 26 | #include <common/include/ProfilingGuidGenerator.hpp> |
Nikhil Raj | 7dcc697 | 2021-04-30 15:44:24 +0100 | [diff] [blame] | 27 | |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 28 | #include <atomic> |
| 29 | #include <condition_variable> |
| 30 | #include <mutex> |
| 31 | #include <thread> |
| 32 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 33 | namespace arm |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 34 | { |
| 35 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 36 | namespace pipe |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 37 | { |
| 38 | |
| 39 | class MockProfilingConnection : public IProfilingConnection |
| 40 | { |
| 41 | public: |
| 42 | MockProfilingConnection() |
| 43 | : m_IsOpen(true) |
| 44 | , m_WrittenData() |
| 45 | , m_Packet() |
| 46 | {} |
| 47 | |
| 48 | enum class PacketType |
| 49 | { |
| 50 | StreamMetaData, |
| 51 | ConnectionAcknowledge, |
| 52 | CounterDirectory, |
| 53 | ReqCounterDirectory, |
| 54 | PeriodicCounterSelection, |
| 55 | PerJobCounterSelection, |
| 56 | TimelineMessageDirectory, |
| 57 | PeriodicCounterCapture, |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 58 | ActivateTimelineReporting, |
| 59 | DeactivateTimelineReporting, |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 60 | Unknown |
| 61 | }; |
| 62 | |
| 63 | bool IsOpen() const override |
| 64 | { |
| 65 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 66 | |
| 67 | return m_IsOpen; |
| 68 | } |
| 69 | |
| 70 | void Close() override |
| 71 | { |
| 72 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 73 | |
| 74 | m_IsOpen = false; |
| 75 | } |
| 76 | |
| 77 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 78 | { |
| 79 | if (buffer == nullptr || length == 0) |
| 80 | { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | uint32_t header = ReadUint32(buffer, 0); |
| 85 | |
| 86 | uint32_t packetFamily = (header >> 26); |
| 87 | uint32_t packetId = ((header >> 16) & 1023); |
| 88 | |
| 89 | PacketType packetType; |
| 90 | |
| 91 | switch (packetFamily) |
| 92 | { |
| 93 | case 0: |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 94 | packetType = packetId < 8 ? PacketType(packetId) : PacketType::Unknown; |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 95 | break; |
| 96 | case 1: |
| 97 | packetType = packetId == 0 ? PacketType::TimelineMessageDirectory : PacketType::Unknown; |
| 98 | break; |
| 99 | case 3: |
| 100 | packetType = packetId == 0 ? PacketType::PeriodicCounterCapture : PacketType::Unknown; |
| 101 | break; |
| 102 | default: |
| 103 | packetType = PacketType::Unknown; |
| 104 | } |
| 105 | |
| 106 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 107 | |
| 108 | m_WrittenData.push_back({ packetType, length }); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | long CheckForPacket(const std::pair<PacketType, uint32_t> packetInfo) |
| 113 | { |
| 114 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 115 | |
| 116 | if(packetInfo.second != 0) |
| 117 | { |
Rob Hughes | bb46dde | 2020-05-20 15:27:37 +0100 | [diff] [blame] | 118 | return static_cast<long>(std::count(m_WrittenData.begin(), m_WrittenData.end(), packetInfo)); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 119 | } |
| 120 | else |
| 121 | { |
Rob Hughes | bb46dde | 2020-05-20 15:27:37 +0100 | [diff] [blame] | 122 | return static_cast<long>(std::count_if(m_WrittenData.begin(), m_WrittenData.end(), |
| 123 | [&packetInfo](const std::pair<PacketType, uint32_t> pair) { return packetInfo.first == pair.first; })); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 127 | bool WritePacket(arm::pipe::Packet&& packet) |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 128 | { |
| 129 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 130 | |
| 131 | m_Packet = std::move(packet); |
| 132 | return true; |
| 133 | } |
| 134 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 135 | arm::pipe::Packet ReadPacket(uint32_t timeout) override |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 136 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 137 | arm::pipe::IgnoreUnused(timeout); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 138 | |
| 139 | // Simulate a delay in the reading process. The default timeout is way too long. |
| 140 | std::this_thread::sleep_for(std::chrono::milliseconds(5)); |
| 141 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 142 | return std::move(m_Packet); |
| 143 | } |
| 144 | |
| 145 | unsigned long GetWrittenDataSize() |
| 146 | { |
| 147 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 148 | |
Rob Hughes | bb46dde | 2020-05-20 15:27:37 +0100 | [diff] [blame] | 149 | return static_cast<unsigned long>(m_WrittenData.size()); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void Clear() |
| 153 | { |
| 154 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 155 | |
| 156 | m_WrittenData.clear(); |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | bool m_IsOpen; |
| 161 | std::vector<std::pair<PacketType, uint32_t>> m_WrittenData; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 162 | arm::pipe::Packet m_Packet; |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 163 | mutable std::mutex m_Mutex; |
| 164 | }; |
| 165 | |
| 166 | class MockProfilingConnectionFactory : public IProfilingConnectionFactory |
| 167 | { |
| 168 | public: |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 169 | IProfilingConnectionPtr GetProfilingConnection(const ProfilingOptions& options) const override |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 170 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 171 | arm::pipe::IgnoreUnused(options); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 172 | return std::make_unique<MockProfilingConnection>(); |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | class MockPacketBuffer : public IPacketBuffer |
| 177 | { |
| 178 | public: |
| 179 | MockPacketBuffer(unsigned int maxSize) |
| 180 | : m_MaxSize(maxSize) |
| 181 | , m_Size(0) |
| 182 | , m_Data(std::make_unique<unsigned char[]>(m_MaxSize)) |
| 183 | {} |
| 184 | |
| 185 | ~MockPacketBuffer() {} |
| 186 | |
| 187 | const unsigned char* GetReadableData() const override { return m_Data.get(); } |
| 188 | |
| 189 | unsigned int GetSize() const override { return m_Size; } |
| 190 | |
| 191 | void MarkRead() override { m_Size = 0; } |
| 192 | |
| 193 | void Commit(unsigned int size) override { m_Size = size; } |
| 194 | |
| 195 | void Release() override { m_Size = 0; } |
| 196 | |
| 197 | unsigned char* GetWritableData() override { return m_Data.get(); } |
| 198 | |
Jim Flynn | 0204f09 | 2020-06-22 20:41:43 +0100 | [diff] [blame] | 199 | void Destroy() override {m_Data.reset(nullptr); m_Size = 0; m_MaxSize =0;} |
| 200 | |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 201 | private: |
| 202 | unsigned int m_MaxSize; |
| 203 | unsigned int m_Size; |
| 204 | std::unique_ptr<unsigned char[]> m_Data; |
| 205 | }; |
| 206 | |
| 207 | class MockBufferManager : public IBufferManager |
| 208 | { |
| 209 | public: |
| 210 | MockBufferManager(unsigned int size) |
| 211 | : m_BufferSize(size), |
| 212 | m_Buffer(std::make_unique<MockPacketBuffer>(size)) {} |
| 213 | |
| 214 | ~MockBufferManager() {} |
| 215 | |
| 216 | IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override |
| 217 | { |
| 218 | if (requestedSize > m_BufferSize) |
| 219 | { |
| 220 | reservedSize = m_BufferSize; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | reservedSize = requestedSize; |
| 225 | } |
| 226 | |
| 227 | return std::move(m_Buffer); |
| 228 | } |
| 229 | |
| 230 | void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override |
| 231 | { |
| 232 | packetBuffer->Commit(size); |
| 233 | m_Buffer = std::move(packetBuffer); |
| 234 | |
| 235 | if (notifyConsumer) |
| 236 | { |
| 237 | FlushReadList(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | IPacketBufferPtr GetReadableBuffer() override |
| 242 | { |
| 243 | return std::move(m_Buffer); |
| 244 | } |
| 245 | |
| 246 | void Release(IPacketBufferPtr& packetBuffer) override |
| 247 | { |
| 248 | packetBuffer->Release(); |
| 249 | m_Buffer = std::move(packetBuffer); |
| 250 | } |
| 251 | |
| 252 | void MarkRead(IPacketBufferPtr& packetBuffer) override |
| 253 | { |
| 254 | packetBuffer->MarkRead(); |
| 255 | m_Buffer = std::move(packetBuffer); |
| 256 | } |
| 257 | |
| 258 | void SetConsumer(IConsumer* consumer) override |
| 259 | { |
| 260 | if (consumer != nullptr) |
| 261 | { |
| 262 | m_Consumer = consumer; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void FlushReadList() override |
| 267 | { |
| 268 | // notify consumer that packet is ready to read |
| 269 | if (m_Consumer != nullptr) |
| 270 | { |
| 271 | m_Consumer->SetReadyToRead(); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | private: |
| 276 | unsigned int m_BufferSize; |
| 277 | IPacketBufferPtr m_Buffer; |
| 278 | IConsumer* m_Consumer = nullptr; |
| 279 | }; |
| 280 | |
| 281 | class MockStreamCounterBuffer : public IBufferManager |
| 282 | { |
| 283 | public: |
| 284 | MockStreamCounterBuffer(unsigned int maxBufferSize = 4096) |
| 285 | : m_MaxBufferSize(maxBufferSize) |
| 286 | , m_BufferList() |
| 287 | , m_CommittedSize(0) |
| 288 | , m_ReadableSize(0) |
| 289 | , m_ReadSize(0) |
| 290 | {} |
| 291 | ~MockStreamCounterBuffer() {} |
| 292 | |
| 293 | IPacketBufferPtr Reserve(unsigned int requestedSize, unsigned int& reservedSize) override |
| 294 | { |
| 295 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 296 | |
| 297 | reservedSize = 0; |
| 298 | if (requestedSize > m_MaxBufferSize) |
| 299 | { |
Jim Flynn | f9db3ef | 2022-03-08 21:23:44 +0000 | [diff] [blame] | 300 | throw arm::pipe::InvalidArgumentException("The maximum buffer size that can be requested is [" + |
| 301 | std::to_string(m_MaxBufferSize) + "] bytes"); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 302 | } |
| 303 | reservedSize = requestedSize; |
| 304 | return std::make_unique<MockPacketBuffer>(requestedSize); |
| 305 | } |
| 306 | |
| 307 | void Commit(IPacketBufferPtr& packetBuffer, unsigned int size, bool notifyConsumer = true) override |
| 308 | { |
| 309 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 310 | |
| 311 | packetBuffer->Commit(size); |
| 312 | m_BufferList.push_back(std::move(packetBuffer)); |
| 313 | m_CommittedSize += size; |
| 314 | |
| 315 | if (notifyConsumer) |
| 316 | { |
| 317 | FlushReadList(); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void Release(IPacketBufferPtr& packetBuffer) override |
| 322 | { |
| 323 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 324 | |
| 325 | packetBuffer->Release(); |
| 326 | } |
| 327 | |
| 328 | IPacketBufferPtr GetReadableBuffer() override |
| 329 | { |
| 330 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 331 | |
| 332 | if (m_BufferList.empty()) |
| 333 | { |
| 334 | return nullptr; |
| 335 | } |
| 336 | IPacketBufferPtr buffer = std::move(m_BufferList.back()); |
| 337 | m_BufferList.pop_back(); |
| 338 | m_ReadableSize += buffer->GetSize(); |
| 339 | return buffer; |
| 340 | } |
| 341 | |
| 342 | void MarkRead(IPacketBufferPtr& packetBuffer) override |
| 343 | { |
| 344 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 345 | |
| 346 | m_ReadSize += packetBuffer->GetSize(); |
| 347 | packetBuffer->MarkRead(); |
| 348 | } |
| 349 | |
| 350 | void SetConsumer(IConsumer* consumer) override |
| 351 | { |
| 352 | if (consumer != nullptr) |
| 353 | { |
| 354 | m_Consumer = consumer; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void FlushReadList() override |
| 359 | { |
| 360 | // notify consumer that packet is ready to read |
| 361 | if (m_Consumer != nullptr) |
| 362 | { |
| 363 | m_Consumer->SetReadyToRead(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | unsigned int GetCommittedSize() const { return m_CommittedSize; } |
| 368 | unsigned int GetReadableSize() const { return m_ReadableSize; } |
| 369 | unsigned int GetReadSize() const { return m_ReadSize; } |
| 370 | |
| 371 | private: |
| 372 | // The maximum buffer size when creating a new buffer |
| 373 | unsigned int m_MaxBufferSize; |
| 374 | |
| 375 | // A list of buffers |
| 376 | std::vector<IPacketBufferPtr> m_BufferList; |
| 377 | |
| 378 | // The mutex to synchronize this mock's methods |
| 379 | std::mutex m_Mutex; |
| 380 | |
| 381 | // The total size of the buffers that has been committed for reading |
| 382 | unsigned int m_CommittedSize; |
| 383 | |
| 384 | // The total size of the buffers that can be read |
| 385 | unsigned int m_ReadableSize; |
| 386 | |
| 387 | // The total size of the buffers that has already been read |
| 388 | unsigned int m_ReadSize; |
| 389 | |
| 390 | // Consumer thread to notify packet is ready to read |
| 391 | IConsumer* m_Consumer = nullptr; |
| 392 | }; |
| 393 | |
| 394 | class MockSendCounterPacket : public ISendCounterPacket |
| 395 | { |
| 396 | public: |
| 397 | MockSendCounterPacket(IBufferManager& sendBuffer) : m_BufferManager(sendBuffer) {} |
| 398 | |
| 399 | void SendStreamMetaDataPacket() override |
| 400 | { |
| 401 | std::string message("SendStreamMetaDataPacket"); |
| 402 | unsigned int reserved = 0; |
| 403 | IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved); |
| 404 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 405 | m_BufferManager.Commit(buffer, reserved, false); |
| 406 | } |
| 407 | |
| 408 | void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override |
| 409 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 410 | arm::pipe::IgnoreUnused(counterDirectory); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 411 | |
| 412 | std::string message("SendCounterDirectoryPacket"); |
| 413 | unsigned int reserved = 0; |
| 414 | IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved); |
| 415 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 416 | m_BufferManager.Commit(buffer, reserved); |
| 417 | } |
| 418 | |
| 419 | void SendPeriodicCounterCapturePacket(uint64_t timestamp, |
| 420 | const std::vector<CounterValue>& values) override |
| 421 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 422 | arm::pipe::IgnoreUnused(timestamp, values); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 423 | |
| 424 | std::string message("SendPeriodicCounterCapturePacket"); |
| 425 | unsigned int reserved = 0; |
| 426 | IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved); |
| 427 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 428 | m_BufferManager.Commit(buffer, reserved); |
| 429 | } |
| 430 | |
| 431 | void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, |
| 432 | const std::vector<uint16_t>& selectedCounterIds) override |
| 433 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 434 | arm::pipe::IgnoreUnused(capturePeriod, selectedCounterIds); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 435 | |
| 436 | std::string message("SendPeriodicCounterSelectionPacket"); |
| 437 | unsigned int reserved = 0; |
| 438 | IPacketBufferPtr buffer = m_BufferManager.Reserve(1024, reserved); |
| 439 | memcpy(buffer->GetWritableData(), message.c_str(), static_cast<unsigned int>(message.size()) + 1); |
| 440 | m_BufferManager.Commit(buffer, reserved); |
| 441 | } |
| 442 | |
| 443 | private: |
| 444 | IBufferManager& m_BufferManager; |
| 445 | }; |
| 446 | |
| 447 | class MockCounterDirectory : public ICounterDirectory |
| 448 | { |
| 449 | public: |
| 450 | MockCounterDirectory() = default; |
| 451 | ~MockCounterDirectory() = default; |
| 452 | |
| 453 | // Register profiling objects |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 454 | const Category* RegisterCategory(const std::string& categoryName) |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 455 | { |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 456 | // Create the category |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 457 | CategoryPtr category = std::make_unique<Category>(categoryName); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 458 | ARM_PIPE_ASSERT(category); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 459 | |
| 460 | // Get the raw category pointer |
| 461 | const Category* categoryPtr = category.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 462 | ARM_PIPE_ASSERT(categoryPtr); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 463 | |
| 464 | // Register the category |
| 465 | m_Categories.insert(std::move(category)); |
| 466 | |
| 467 | return categoryPtr; |
| 468 | } |
| 469 | |
| 470 | const Device* RegisterDevice(const std::string& deviceName, |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 471 | uint16_t cores = 0) |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 472 | { |
| 473 | // Get the device UID |
| 474 | uint16_t deviceUid = GetNextUid(); |
| 475 | |
| 476 | // Create the device |
| 477 | DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 478 | ARM_PIPE_ASSERT(device); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 479 | |
| 480 | // Get the raw device pointer |
| 481 | const Device* devicePtr = device.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 482 | ARM_PIPE_ASSERT(devicePtr); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 483 | |
| 484 | // Register the device |
| 485 | m_Devices.insert(std::make_pair(deviceUid, std::move(device))); |
| 486 | |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 487 | return devicePtr; |
| 488 | } |
| 489 | |
| 490 | const CounterSet* RegisterCounterSet( |
| 491 | const std::string& counterSetName, |
Sadik Armagan | 4c99899 | 2020-02-25 12:44:44 +0000 | [diff] [blame] | 492 | uint16_t count = 0) |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 493 | { |
| 494 | // Get the counter set UID |
| 495 | uint16_t counterSetUid = GetNextUid(); |
| 496 | |
| 497 | // Create the counter set |
| 498 | CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 499 | ARM_PIPE_ASSERT(counterSet); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 500 | |
| 501 | // Get the raw counter set pointer |
| 502 | const CounterSet* counterSetPtr = counterSet.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 503 | ARM_PIPE_ASSERT(counterSetPtr); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 504 | |
| 505 | // Register the counter set |
| 506 | m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet))); |
| 507 | |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 508 | return counterSetPtr; |
| 509 | } |
| 510 | |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 511 | const Counter* RegisterCounter(const std::string& backendId, |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 512 | const uint16_t uid, |
| 513 | const std::string& parentCategoryName, |
| 514 | uint16_t counterClass, |
| 515 | uint16_t interpolation, |
| 516 | double multiplier, |
| 517 | const std::string& name, |
| 518 | const std::string& description, |
Jim Flynn | decd08b | 2022-03-13 22:35:46 +0000 | [diff] [blame] | 519 | const arm::pipe::Optional<std::string>& units = arm::pipe::EmptyOptional(), |
| 520 | const arm::pipe::Optional<uint16_t>& numberOfCores = arm::pipe::EmptyOptional(), |
| 521 | const arm::pipe::Optional<uint16_t>& deviceUid = arm::pipe::EmptyOptional(), |
| 522 | const arm::pipe::Optional<uint16_t>& counterSetUid = arm::pipe::EmptyOptional()) |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 523 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 524 | arm::pipe::IgnoreUnused(backendId); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 525 | |
| 526 | // Get the number of cores from the argument only |
| 527 | uint16_t deviceCores = numberOfCores.has_value() ? numberOfCores.value() : 0; |
| 528 | |
| 529 | // Get the device UID |
| 530 | uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0; |
| 531 | |
| 532 | // Get the counter set UID |
| 533 | uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0; |
| 534 | |
| 535 | // Get the counter UIDs and calculate the max counter UID |
| 536 | std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 537 | ARM_PIPE_ASSERT(!counterUids.empty()); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 538 | uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back(); |
| 539 | |
| 540 | // Get the counter units |
| 541 | const std::string unitsValue = units.has_value() ? units.value() : ""; |
| 542 | |
| 543 | // Create the counter |
| 544 | CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID, |
| 545 | counterUids.front(), |
| 546 | maxCounterUid, |
| 547 | counterClass, |
| 548 | interpolation, |
| 549 | multiplier, |
| 550 | name, |
| 551 | description, |
| 552 | unitsValue, |
| 553 | deviceUidValue, |
| 554 | counterSetUidValue); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 555 | ARM_PIPE_ASSERT(counter); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 556 | |
| 557 | // Get the raw counter pointer |
| 558 | const Counter* counterPtr = counter.get(); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 559 | ARM_PIPE_ASSERT(counterPtr); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 560 | |
| 561 | // Process multiple counters if necessary |
| 562 | for (uint16_t counterUid : counterUids) |
| 563 | { |
| 564 | // Connect the counter to the parent category |
| 565 | Category* parentCategory = const_cast<Category*>(GetCategory(parentCategoryName)); |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 566 | ARM_PIPE_ASSERT(parentCategory); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 567 | parentCategory->m_Counters.push_back(counterUid); |
| 568 | |
| 569 | // Register the counter |
| 570 | m_Counters.insert(std::make_pair(counterUid, counter)); |
| 571 | } |
| 572 | |
| 573 | return counterPtr; |
| 574 | } |
| 575 | |
| 576 | // Getters for counts |
Jim Flynn | 75c14f4 | 2022-03-10 22:05:42 +0000 | [diff] [blame] | 577 | uint16_t GetCategoryCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Categories.size()); } |
| 578 | uint16_t GetDeviceCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Devices.size()); } |
| 579 | uint16_t GetCounterSetCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_CounterSets.size()); } |
| 580 | uint16_t GetCounterCount() const override { return arm::pipe::numeric_cast<uint16_t>(m_Counters.size()); } |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 581 | |
| 582 | // Getters for collections |
| 583 | const Categories& GetCategories() const override { return m_Categories; } |
| 584 | const Devices& GetDevices() const override { return m_Devices; } |
| 585 | const CounterSets& GetCounterSets() const override { return m_CounterSets; } |
| 586 | const Counters& GetCounters() const override { return m_Counters; } |
| 587 | |
| 588 | // Getters for profiling objects |
| 589 | const Category* GetCategory(const std::string& name) const override |
| 590 | { |
| 591 | auto it = std::find_if(m_Categories.begin(), m_Categories.end(), [&name](const CategoryPtr& category) |
| 592 | { |
Jim Flynn | 6730fe9 | 2022-03-10 22:57:47 +0000 | [diff] [blame] | 593 | ARM_PIPE_ASSERT(category); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 594 | |
| 595 | return category->m_Name == name; |
| 596 | }); |
| 597 | |
| 598 | if (it == m_Categories.end()) |
| 599 | { |
| 600 | return nullptr; |
| 601 | } |
| 602 | |
| 603 | return it->get(); |
| 604 | } |
| 605 | |
| 606 | const Device* GetDevice(uint16_t uid) const override |
| 607 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 608 | arm::pipe::IgnoreUnused(uid); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 609 | return nullptr; // Not used by the unit tests |
| 610 | } |
| 611 | |
| 612 | const CounterSet* GetCounterSet(uint16_t uid) const override |
| 613 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 614 | arm::pipe::IgnoreUnused(uid); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 615 | return nullptr; // Not used by the unit tests |
| 616 | } |
| 617 | |
| 618 | const Counter* GetCounter(uint16_t uid) const override |
| 619 | { |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 620 | arm::pipe::IgnoreUnused(uid); |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 621 | return nullptr; // Not used by the unit tests |
| 622 | } |
| 623 | |
| 624 | private: |
| 625 | Categories m_Categories; |
| 626 | Devices m_Devices; |
| 627 | CounterSets m_CounterSets; |
| 628 | Counters m_Counters; |
| 629 | }; |
| 630 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 631 | class MockProfilingService : public ProfilingService |
Sadik Armagan | cab588a | 2020-02-17 11:33:31 +0000 | [diff] [blame] | 632 | { |
| 633 | public: |
Jim Flynn | 3443025 | 2022-03-04 15:03:58 +0000 | [diff] [blame] | 634 | MockProfilingService(uint16_t maxGlobalCounterId, |
| 635 | IInitialiseProfilingService& initialiser, |
| 636 | MockBufferManager& mockBufferManager, |
Sadik Armagan | cab588a | 2020-02-17 11:33:31 +0000 | [diff] [blame] | 637 | bool isProfilingEnabled, |
| 638 | const CaptureData& captureData) : |
Jim Flynn | 9c85b41 | 2022-03-16 00:27:43 +0000 | [diff] [blame] | 639 | ProfilingService(maxGlobalCounterId, |
| 640 | initialiser, |
| 641 | arm::pipe::ARMNN_SOFTWARE_INFO, |
| 642 | arm::pipe::ARMNN_SOFTWARE_VERSION, |
| 643 | arm::pipe::ARMNN_HARDWARE_VERSION), |
| 644 | m_SendCounterPacket(mockBufferManager, |
| 645 | arm::pipe::ARMNN_SOFTWARE_INFO, |
| 646 | arm::pipe::ARMNN_SOFTWARE_VERSION, |
| 647 | arm::pipe::ARMNN_HARDWARE_VERSION), |
Sadik Armagan | cab588a | 2020-02-17 11:33:31 +0000 | [diff] [blame] | 648 | m_IsProfilingEnabled(isProfilingEnabled), |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 649 | m_CaptureData(captureData) |
| 650 | {} |
Sadik Armagan | cab588a | 2020-02-17 11:33:31 +0000 | [diff] [blame] | 651 | |
| 652 | /// Return the next random Guid in the sequence |
| 653 | ProfilingDynamicGuid NextGuid() override |
| 654 | { |
| 655 | return m_GuidGenerator.NextGuid(); |
| 656 | } |
| 657 | |
| 658 | /// Create a ProfilingStaticGuid based on a hash of the string |
| 659 | ProfilingStaticGuid GenerateStaticId(const std::string& str) override |
| 660 | { |
| 661 | return m_GuidGenerator.GenerateStaticId(str); |
| 662 | } |
| 663 | |
| 664 | std::unique_ptr<ISendTimelinePacket> GetSendTimelinePacket() const override |
| 665 | { |
| 666 | return nullptr; |
| 667 | } |
| 668 | |
| 669 | const ICounterMappings& GetCounterMappings() const override |
| 670 | { |
| 671 | return m_CounterMapping; |
| 672 | } |
| 673 | |
| 674 | ISendCounterPacket& GetSendCounterPacket() override |
| 675 | { |
| 676 | return m_SendCounterPacket; |
| 677 | } |
| 678 | |
| 679 | bool IsProfilingEnabled() const override |
| 680 | { |
| 681 | return m_IsProfilingEnabled; |
| 682 | } |
| 683 | |
| 684 | CaptureData GetCaptureData() override |
| 685 | { |
| 686 | CaptureData copy(m_CaptureData); |
| 687 | return copy; |
| 688 | } |
| 689 | |
| 690 | void RegisterMapping(uint16_t globalCounterId, |
| 691 | uint16_t backendCounterId, |
Cathal Corbett | 6f07372 | 2022-03-04 12:11:09 +0000 | [diff] [blame] | 692 | const std::string& backendId) |
Sadik Armagan | cab588a | 2020-02-17 11:33:31 +0000 | [diff] [blame] | 693 | { |
| 694 | m_CounterMapping.RegisterMapping(globalCounterId, backendCounterId, backendId); |
| 695 | } |
| 696 | |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 697 | void Reset() |
Sadik Armagan | cab588a | 2020-02-17 11:33:31 +0000 | [diff] [blame] | 698 | { |
| 699 | m_CounterMapping.Reset(); |
| 700 | } |
| 701 | |
| 702 | private: |
| 703 | ProfilingGuidGenerator m_GuidGenerator; |
Keith Davis | 33ed221 | 2020-03-30 10:43:41 +0100 | [diff] [blame] | 704 | CounterIdMap m_CounterMapping; |
| 705 | SendCounterPacket m_SendCounterPacket; |
| 706 | bool m_IsProfilingEnabled; |
| 707 | CaptureData m_CaptureData; |
Sadik Armagan | cab588a | 2020-02-17 11:33:31 +0000 | [diff] [blame] | 708 | }; |
| 709 | |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 710 | class MockProfilingServiceStatus : public IProfilingServiceStatus |
| 711 | { |
| 712 | public: |
| 713 | void NotifyProfilingServiceActive() override {} |
Jim Flynn | 9265a88 | 2022-03-10 23:35:26 +0000 | [diff] [blame] | 714 | void WaitForProfilingServiceActivation(unsigned int timeout) override { arm::pipe::IgnoreUnused(timeout); } |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 715 | }; |
| 716 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 717 | } // namespace pipe |
Jim Flynn | 6406355 | 2020-02-14 10:18:08 +0000 | [diff] [blame] | 718 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 719 | } // namespace arm |