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