blob: 3776dbcbdb785583e7669e038a402a87cb9a44c5 [file] [log] [blame]
Keith Davis3201eea2019-10-24 17:30:41 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Jim Flynn4e755a52020-03-29 17:48:26 +01008#include <armnn/profiling/ILocalPacketHandler.hpp>
Keith Davis3201eea2019-10-24 17:30:41 +01009#include "DirectoryCaptureCommandHandler.hpp"
10#include "IProfilingConnection.hpp"
Finn Williams56b465d2020-05-15 13:34:12 +010011#include <Packet.hpp>
Keith Davis3201eea2019-10-24 17:30:41 +010012#include "ProfilingUtils.hpp"
13#include "Runtime.hpp"
14
Jim Flynn4e755a52020-03-29 17:48:26 +010015#include <atomic>
Colm Donelan4cace322019-11-20 14:59:12 +000016#include <condition_variable>
Keith Davis3201eea2019-10-24 17:30:41 +010017#include <fstream>
Jim Flynn4e755a52020-03-29 17:48:26 +010018#include <mutex>
Keith Davis3201eea2019-10-24 17:30:41 +010019#include <queue>
Jim Flynn4e755a52020-03-29 17:48:26 +010020#include <thread>
Keith Davis3201eea2019-10-24 17:30:41 +010021
22namespace armnn
23{
24
25namespace profiling
26{
27
28enum class TargetEndianness
29{
30 BeWire,
31 LeWire
32};
33
34enum class PackageActivity
35{
36 StreamMetaData,
37 CounterDirectory,
38 Unknown
39};
40
41class FileOnlyProfilingConnection : public IProfilingConnection
42{
43public:
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 Flynn4e755a52020-03-29 17:48:26 +010049 , 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 Davis3201eea2019-10-24 17:30:41 +010063
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
76private:
Jim Flynn4e755a52020-03-29 17:48:26 +010077 void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler);
78 void StartProcessingThread();
79 void ClearReadableList();
80 void DispatchPacketToHandlers(const Packet& packet);
81
Keith Davis3201eea2019-10-24 17:30:41 +010082 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 Flynn4e755a52020-03-29 17:48:26 +010090 PackageActivity GetPackageActivity(const Packet& packet, uint32_t headerAsWords[2]);
Keith Davis3201eea2019-10-24 17:30:41 +010091
92 void Fail(const std::string& errorMessage);
93
Jim Flynn4e755a52020-03-29 17:48:26 +010094 void ForwardPacketToHandlers(Packet& packet);
95 void ServiceLocalHandlers();
96
Keith Davis3201eea2019-10-24 17:30:41 +010097 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 Donelan4cace322019-11-20 14:59:12 +0000102
103 std::mutex m_PacketAvailableMutex;
104 std::condition_variable m_ConditionPacketAvailable;
Jim Flynn4e755a52020-03-29 17:48:26 +0100105
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 Davis3201eea2019-10-24 17:30:41 +0100122};
123
124} // namespace profiling
125
126} // namespace armnn