blob: 48fb4d37f2826bbcae4aa624e615aa0b01822b6b [file] [log] [blame]
Keith Davis3201eea2019-10-24 17:30:41 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Keith Davis3201eea2019-10-24 17:30:41 +01003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Jim Flynn4e755a52020-03-29 17:48:26 +01008#include <armnn/profiling/ILocalPacketHandler.hpp>
Jim Flynndecd08b2022-03-13 22:35:46 +00009#include <armnn/profiling/ProfilingOptions.hpp>
10
11#include "DirectoryCaptureCommandHandler.hpp"
Keith Davis3201eea2019-10-24 17:30:41 +010012#include "IProfilingConnection.hpp"
13#include "ProfilingUtils.hpp"
Keith Davis3201eea2019-10-24 17:30:41 +010014
Jim Flynn4a962112022-03-13 20:18:58 +000015#include <common/include/Assert.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010016#include <common/include/Packet.hpp>
17
Jim Flynn4e755a52020-03-29 17:48:26 +010018#include <atomic>
Colm Donelan4cace322019-11-20 14:59:12 +000019#include <condition_variable>
Keith Davis3201eea2019-10-24 17:30:41 +010020#include <fstream>
Jim Flynndecd08b2022-03-13 22:35:46 +000021#include <map>
Jim Flynn4e755a52020-03-29 17:48:26 +010022#include <mutex>
Keith Davis3201eea2019-10-24 17:30:41 +010023#include <queue>
Jim Flynn4e755a52020-03-29 17:48:26 +010024#include <thread>
Keith Davis3201eea2019-10-24 17:30:41 +010025
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000026namespace arm
Keith Davis3201eea2019-10-24 17:30:41 +010027{
28
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000029namespace pipe
Keith Davis3201eea2019-10-24 17:30:41 +010030{
31
Jim Flynn01d02812020-04-29 21:12:13 +010032// forward declaration
33class FileOnlyProfilingConnection;
Keith Davis3201eea2019-10-24 17:30:41 +010034
Jim Flynn01d02812020-04-29 21:12:13 +010035class StreamMetaDataProcessor : public ILocalPacketHandler
Keith Davis3201eea2019-10-24 17:30:41 +010036{
37public:
Jim Flynn01d02812020-04-29 21:12:13 +010038 explicit StreamMetaDataProcessor(FileOnlyProfilingConnection* fileOnlyProfilingConnection) :
39 m_FileOnlyProfilingConnection(fileOnlyProfilingConnection),
40 m_MetaDataPacketHeader(ConstructHeader(0, 0)) {};
41
42 std::vector<uint32_t> GetHeadersAccepted() override;
43
Jim Flynnbbfe6032020-07-20 16:57:44 +010044 void HandlePacket(const arm::pipe::Packet& packet) override;
Jim Flynn01d02812020-04-29 21:12:13 +010045
46private:
47 FileOnlyProfilingConnection* m_FileOnlyProfilingConnection;
48 uint32_t m_MetaDataPacketHeader;
49
50 static uint32_t ToUint32(const unsigned char* data, TargetEndianness endianness);
51};
52
53class FileOnlyProfilingConnection : public IProfilingConnection, public IInternalProfilingConnection
54{
55public:
Jim Flynn4c9ed1d2022-01-23 23:57:20 +000056 explicit FileOnlyProfilingConnection(const ProfilingOptions& options)
Keith Davis3201eea2019-10-24 17:30:41 +010057 : m_Options(options)
Jim Flynn01d02812020-04-29 21:12:13 +010058 , m_Endianness(TargetEndianness::LeWire) // Set a sensible default.
59 // StreamMetaDataProcessor will set a real value.
Jim Flynn4e755a52020-03-29 17:48:26 +010060 , m_IsRunning(false)
61 , m_KeepRunning(false)
62 , m_Timeout(1000)
63 {
Jim Flynn01d02812020-04-29 21:12:13 +010064 // add the StreamMetaDataProcessor
65 auto streamMetaDataProcessor = std::make_shared<StreamMetaDataProcessor>(this);
66 AddLocalPacketHandler(streamMetaDataProcessor);
67 // and any additional ones added by the users
68 for (const ILocalPacketHandlerSharedPtr& localPacketHandler : options.m_LocalPacketHandlers)
Jim Flynn4e755a52020-03-29 17:48:26 +010069 {
70 AddLocalPacketHandler(localPacketHandler);
71 }
Jim Flynn01d02812020-04-29 21:12:13 +010072 if (!m_PacketHandlers.empty())
Jim Flynn4e755a52020-03-29 17:48:26 +010073 {
74 StartProcessingThread();
75 }
76 // NOTE: could add timeout to the external profiling options
77 };
Keith Davis3201eea2019-10-24 17:30:41 +010078
Jim Flynn01d02812020-04-29 21:12:13 +010079 ~FileOnlyProfilingConnection() override;
Keith Davis3201eea2019-10-24 17:30:41 +010080
81 bool IsOpen() const override;
82
83 void Close() override;
84
85 // This is effectively receiving a data packet from ArmNN.
86 bool WritePacket(const unsigned char* buffer, uint32_t length) override;
87
88 // Sending a packet back to ArmNN.
Jim Flynnbbfe6032020-07-20 16:57:44 +010089 arm::pipe::Packet ReadPacket(uint32_t timeout) override;
Keith Davis3201eea2019-10-24 17:30:41 +010090
Jim Flynn01d02812020-04-29 21:12:13 +010091 void SetEndianess(const TargetEndianness& endianness) override //IInternalProfilingConnection
92 {
93 m_Endianness = endianness;
94 }
95
Jim Flynnbbfe6032020-07-20 16:57:44 +010096 void ReturnPacket(arm::pipe::Packet& packet) override; //IInternalProfilingConnection
Jim Flynn01d02812020-04-29 21:12:13 +010097
Keith Davis3201eea2019-10-24 17:30:41 +010098private:
Jim Flynn4e755a52020-03-29 17:48:26 +010099 void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler);
100 void StartProcessingThread();
101 void ClearReadableList();
Jim Flynnbbfe6032020-07-20 16:57:44 +0100102 void DispatchPacketToHandlers(const arm::pipe::Packet& packet);
Jim Flynn4e755a52020-03-29 17:48:26 +0100103
Keith Davis3201eea2019-10-24 17:30:41 +0100104 void Fail(const std::string& errorMessage);
105
Jim Flynnbbfe6032020-07-20 16:57:44 +0100106 void ForwardPacketToHandlers(arm::pipe::Packet& packet);
Jim Flynn4e755a52020-03-29 17:48:26 +0100107 void ServiceLocalHandlers();
108
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000109 ProfilingOptions m_Options;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100110 std::queue<arm::pipe::Packet> m_PacketQueue;
Keith Davis3201eea2019-10-24 17:30:41 +0100111 TargetEndianness m_Endianness;
Colm Donelan4cace322019-11-20 14:59:12 +0000112
113 std::mutex m_PacketAvailableMutex;
114 std::condition_variable m_ConditionPacketAvailable;
Jim Flynn4e755a52020-03-29 17:48:26 +0100115
116 std::vector<ILocalPacketHandlerSharedPtr> m_PacketHandlers;
117 std::map<uint32_t, std::vector<ILocalPacketHandlerSharedPtr>> m_IndexedHandlers;
118 std::vector<ILocalPacketHandlerSharedPtr> m_UniversalHandlers;
119
120 // List of readable packets for the local packet handlers
Jim Flynnbbfe6032020-07-20 16:57:44 +0100121 std::queue<arm::pipe::Packet> m_ReadableList;
Jim Flynn4e755a52020-03-29 17:48:26 +0100122 // Mutex and condition variable for the readable packet list
123 std::mutex m_ReadableMutex;
124 std::condition_variable m_ConditionPacketReadable;
125 // thread that takes items from the readable list and dispatches them
126 // to the handlers.
127 std::thread m_LocalHandlersThread;
128 // atomic booleans that control the operation of the local handlers thread
129 std::atomic<bool> m_IsRunning;
130 std::atomic<bool> m_KeepRunning;
131 int m_Timeout;
Keith Davis3201eea2019-10-24 17:30:41 +0100132};
133
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000134} // namespace pipe
Keith Davis3201eea2019-10-24 17:30:41 +0100135
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000136} // namespace arm