Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 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" |
Finn Williams | 56b465d | 2020-05-15 13:34:12 +0100 | [diff] [blame] | 11 | #include <Packet.hpp> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 12 | #include "ProfilingUtils.hpp" |
| 13 | #include "Runtime.hpp" |
| 14 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 15 | #include <atomic> |
Colm Donelan | 4cace32 | 2019-11-20 14:59:12 +0000 | [diff] [blame] | 16 | #include <condition_variable> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 17 | #include <fstream> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 18 | #include <mutex> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 19 | #include <queue> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 20 | #include <thread> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 21 | |
| 22 | namespace armnn |
| 23 | { |
| 24 | |
| 25 | namespace profiling |
| 26 | { |
| 27 | |
| 28 | enum class TargetEndianness |
| 29 | { |
| 30 | BeWire, |
| 31 | LeWire |
| 32 | }; |
| 33 | |
| 34 | enum class PackageActivity |
| 35 | { |
| 36 | StreamMetaData, |
| 37 | CounterDirectory, |
| 38 | Unknown |
| 39 | }; |
| 40 | |
| 41 | class FileOnlyProfilingConnection : public IProfilingConnection |
| 42 | { |
| 43 | public: |
| 44 | FileOnlyProfilingConnection(const Runtime::CreationOptions::ExternalProfilingOptions& options, |
| 45 | const bool quietOp = true) |
| 46 | : m_Options(options) |
| 47 | , m_QuietOp(quietOp) |
| 48 | , m_Endianness(TargetEndianness::LeWire) // Set a sensible default. WaitForStreamMeta will set a real value. |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 49 | , m_IsRunning(false) |
| 50 | , m_KeepRunning(false) |
| 51 | , m_Timeout(1000) |
| 52 | { |
| 53 | for (ILocalPacketHandlerSharedPtr localPacketHandler : options.m_LocalPacketHandlers) |
| 54 | { |
| 55 | AddLocalPacketHandler(localPacketHandler); |
| 56 | } |
| 57 | if (!options.m_LocalPacketHandlers.empty()) |
| 58 | { |
| 59 | StartProcessingThread(); |
| 60 | } |
| 61 | // NOTE: could add timeout to the external profiling options |
| 62 | }; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 63 | |
| 64 | ~FileOnlyProfilingConnection(); |
| 65 | |
| 66 | bool IsOpen() const override; |
| 67 | |
| 68 | void Close() override; |
| 69 | |
| 70 | // This is effectively receiving a data packet from ArmNN. |
| 71 | bool WritePacket(const unsigned char* buffer, uint32_t length) override; |
| 72 | |
| 73 | // Sending a packet back to ArmNN. |
| 74 | Packet ReadPacket(uint32_t timeout) override; |
| 75 | |
| 76 | private: |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 77 | void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler); |
| 78 | void StartProcessingThread(); |
| 79 | void ClearReadableList(); |
| 80 | void DispatchPacketToHandlers(const Packet& packet); |
| 81 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 82 | bool WaitForStreamMeta(const unsigned char* buffer, uint32_t length); |
| 83 | |
| 84 | uint32_t ToUint32(const unsigned char* data, TargetEndianness endianness); |
| 85 | |
| 86 | void SendConnectionAck(); |
| 87 | |
| 88 | bool SendCounterSelectionPacket(); |
| 89 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 90 | PackageActivity GetPackageActivity(const Packet& packet, uint32_t headerAsWords[2]); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 91 | |
| 92 | void Fail(const std::string& errorMessage); |
| 93 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 94 | void ForwardPacketToHandlers(Packet& packet); |
| 95 | void ServiceLocalHandlers(); |
| 96 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 97 | Runtime::CreationOptions::ExternalProfilingOptions m_Options; |
| 98 | bool m_QuietOp; |
| 99 | std::vector<uint16_t> m_IdList; |
| 100 | std::queue<Packet> m_PacketQueue; |
| 101 | TargetEndianness m_Endianness; |
Colm Donelan | 4cace32 | 2019-11-20 14:59:12 +0000 | [diff] [blame] | 102 | |
| 103 | std::mutex m_PacketAvailableMutex; |
| 104 | std::condition_variable m_ConditionPacketAvailable; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 105 | |
| 106 | std::vector<ILocalPacketHandlerSharedPtr> m_PacketHandlers; |
| 107 | std::map<uint32_t, std::vector<ILocalPacketHandlerSharedPtr>> m_IndexedHandlers; |
| 108 | std::vector<ILocalPacketHandlerSharedPtr> m_UniversalHandlers; |
| 109 | |
| 110 | // List of readable packets for the local packet handlers |
| 111 | std::queue<Packet> m_ReadableList; |
| 112 | // Mutex and condition variable for the readable packet list |
| 113 | std::mutex m_ReadableMutex; |
| 114 | std::condition_variable m_ConditionPacketReadable; |
| 115 | // thread that takes items from the readable list and dispatches them |
| 116 | // to the handlers. |
| 117 | std::thread m_LocalHandlersThread; |
| 118 | // atomic booleans that control the operation of the local handlers thread |
| 119 | std::atomic<bool> m_IsRunning; |
| 120 | std::atomic<bool> m_KeepRunning; |
| 121 | int m_Timeout; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace profiling |
| 125 | |
| 126 | } // namespace armnn |