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