ArmNN
 20.05
FileOnlyProfilingConnection.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
10 #include "IProfilingConnection.hpp"
11 #include <Packet.hpp>
12 #include "ProfilingUtils.hpp"
13 #include "Runtime.hpp"
14 
15 #include <atomic>
16 #include <condition_variable>
17 #include <fstream>
18 #include <mutex>
19 #include <queue>
20 #include <thread>
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 {
38  Unknown
39 };
40 
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.
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  };
63 
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:
77  void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler);
78  void StartProcessingThread();
79  void ClearReadableList();
80  void DispatchPacketToHandlers(const Packet& packet);
81 
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 
90  PackageActivity GetPackageActivity(const Packet& packet, uint32_t headerAsWords[2]);
91 
92  void Fail(const std::string& errorMessage);
93 
94  void ForwardPacketToHandlers(Packet& packet);
95  void ServiceLocalHandlers();
96 
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;
102 
103  std::mutex m_PacketAvailableMutex;
104  std::condition_variable m_ConditionPacketAvailable;
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;
122 };
123 
124 } // namespace profiling
125 
126 } // namespace armnn
std::shared_ptr< ILocalPacketHandler > ILocalPacketHandlerSharedPtr
DataLayout::NHWC false
Copyright (c) 2020 ARM Limited.
FileOnlyProfilingConnection(const Runtime::CreationOptions::ExternalProfilingOptions &options, const bool quietOp=true)
armnn::Runtime::CreationOptions::ExternalProfilingOptions options