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