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