Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 8 | #include <armnn/profiling/ILocalPacketHandler.hpp> |
Jim Flynn | decd08b | 2022-03-13 22:35:46 +0000 | [diff] [blame^] | 9 | #include <armnn/profiling/ProfilingOptions.hpp> |
| 10 | |
| 11 | #include "DirectoryCaptureCommandHandler.hpp" |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 12 | #include "IProfilingConnection.hpp" |
| 13 | #include "ProfilingUtils.hpp" |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 14 | |
Jim Flynn | 4a96211 | 2022-03-13 20:18:58 +0000 | [diff] [blame] | 15 | #include <common/include/Assert.hpp> |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 16 | #include <common/include/Packet.hpp> |
| 17 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 18 | #include <atomic> |
Colm Donelan | 4cace32 | 2019-11-20 14:59:12 +0000 | [diff] [blame] | 19 | #include <condition_variable> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 20 | #include <fstream> |
Jim Flynn | decd08b | 2022-03-13 22:35:46 +0000 | [diff] [blame^] | 21 | #include <map> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 22 | #include <mutex> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 23 | #include <queue> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 24 | #include <thread> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 25 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 26 | namespace arm |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 27 | { |
| 28 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 29 | namespace pipe |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 30 | { |
| 31 | |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 32 | // forward declaration |
| 33 | class FileOnlyProfilingConnection; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 34 | |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 35 | class StreamMetaDataProcessor : public ILocalPacketHandler |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 36 | { |
| 37 | public: |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 38 | explicit StreamMetaDataProcessor(FileOnlyProfilingConnection* fileOnlyProfilingConnection) : |
| 39 | m_FileOnlyProfilingConnection(fileOnlyProfilingConnection), |
| 40 | m_MetaDataPacketHeader(ConstructHeader(0, 0)) {}; |
| 41 | |
| 42 | std::vector<uint32_t> GetHeadersAccepted() override; |
| 43 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 44 | void HandlePacket(const arm::pipe::Packet& packet) override; |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 45 | |
| 46 | private: |
| 47 | FileOnlyProfilingConnection* m_FileOnlyProfilingConnection; |
| 48 | uint32_t m_MetaDataPacketHeader; |
| 49 | |
| 50 | static uint32_t ToUint32(const unsigned char* data, TargetEndianness endianness); |
| 51 | }; |
| 52 | |
| 53 | class FileOnlyProfilingConnection : public IProfilingConnection, public IInternalProfilingConnection |
| 54 | { |
| 55 | public: |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 56 | explicit FileOnlyProfilingConnection(const ProfilingOptions& options) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 57 | : m_Options(options) |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 58 | , m_Endianness(TargetEndianness::LeWire) // Set a sensible default. |
| 59 | // StreamMetaDataProcessor will set a real value. |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 60 | , m_IsRunning(false) |
| 61 | , m_KeepRunning(false) |
| 62 | , m_Timeout(1000) |
| 63 | { |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 64 | // add the StreamMetaDataProcessor |
| 65 | auto streamMetaDataProcessor = std::make_shared<StreamMetaDataProcessor>(this); |
| 66 | AddLocalPacketHandler(streamMetaDataProcessor); |
| 67 | // and any additional ones added by the users |
| 68 | for (const ILocalPacketHandlerSharedPtr& localPacketHandler : options.m_LocalPacketHandlers) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 69 | { |
| 70 | AddLocalPacketHandler(localPacketHandler); |
| 71 | } |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 72 | if (!m_PacketHandlers.empty()) |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 73 | { |
| 74 | StartProcessingThread(); |
| 75 | } |
| 76 | // NOTE: could add timeout to the external profiling options |
| 77 | }; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 78 | |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 79 | ~FileOnlyProfilingConnection() override; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 80 | |
| 81 | bool IsOpen() const override; |
| 82 | |
| 83 | void Close() override; |
| 84 | |
| 85 | // This is effectively receiving a data packet from ArmNN. |
| 86 | bool WritePacket(const unsigned char* buffer, uint32_t length) override; |
| 87 | |
| 88 | // Sending a packet back to ArmNN. |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 89 | arm::pipe::Packet ReadPacket(uint32_t timeout) override; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 90 | |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 91 | void SetEndianess(const TargetEndianness& endianness) override //IInternalProfilingConnection |
| 92 | { |
| 93 | m_Endianness = endianness; |
| 94 | } |
| 95 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 96 | void ReturnPacket(arm::pipe::Packet& packet) override; //IInternalProfilingConnection |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 97 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 98 | private: |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 99 | void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler); |
| 100 | void StartProcessingThread(); |
| 101 | void ClearReadableList(); |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 102 | void DispatchPacketToHandlers(const arm::pipe::Packet& packet); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 103 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 104 | void Fail(const std::string& errorMessage); |
| 105 | |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 106 | void ForwardPacketToHandlers(arm::pipe::Packet& packet); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 107 | void ServiceLocalHandlers(); |
| 108 | |
Jim Flynn | 4c9ed1d | 2022-01-23 23:57:20 +0000 | [diff] [blame] | 109 | ProfilingOptions m_Options; |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 110 | std::queue<arm::pipe::Packet> m_PacketQueue; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 111 | TargetEndianness m_Endianness; |
Colm Donelan | 4cace32 | 2019-11-20 14:59:12 +0000 | [diff] [blame] | 112 | |
| 113 | std::mutex m_PacketAvailableMutex; |
| 114 | std::condition_variable m_ConditionPacketAvailable; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 115 | |
| 116 | std::vector<ILocalPacketHandlerSharedPtr> m_PacketHandlers; |
| 117 | std::map<uint32_t, std::vector<ILocalPacketHandlerSharedPtr>> m_IndexedHandlers; |
| 118 | std::vector<ILocalPacketHandlerSharedPtr> m_UniversalHandlers; |
| 119 | |
| 120 | // List of readable packets for the local packet handlers |
Jim Flynn | bbfe603 | 2020-07-20 16:57:44 +0100 | [diff] [blame] | 121 | std::queue<arm::pipe::Packet> m_ReadableList; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 122 | // Mutex and condition variable for the readable packet list |
| 123 | std::mutex m_ReadableMutex; |
| 124 | std::condition_variable m_ConditionPacketReadable; |
| 125 | // thread that takes items from the readable list and dispatches them |
| 126 | // to the handlers. |
| 127 | std::thread m_LocalHandlersThread; |
| 128 | // atomic booleans that control the operation of the local handlers thread |
| 129 | std::atomic<bool> m_IsRunning; |
| 130 | std::atomic<bool> m_KeepRunning; |
| 131 | int m_Timeout; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 134 | } // namespace pipe |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 135 | |
Cathal Corbett | 5aa9fd7 | 2022-02-25 15:33:28 +0000 | [diff] [blame] | 136 | } // namespace arm |