blob: 9f992d080eadb198ca0140fa6cb68c366eabf670 [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>
Keith Davis3201eea2019-10-24 17:30:41 +01009#include "DirectoryCaptureCommandHandler.hpp"
10#include "IProfilingConnection.hpp"
11#include "ProfilingUtils.hpp"
12#include "Runtime.hpp"
13
Jim Flynnbbfe6032020-07-20 16:57:44 +010014#include <common/include/Packet.hpp>
15
Jim Flynn4e755a52020-03-29 17:48:26 +010016#include <atomic>
Colm Donelan4cace322019-11-20 14:59:12 +000017#include <condition_variable>
Keith Davis3201eea2019-10-24 17:30:41 +010018#include <fstream>
Jim Flynn4e755a52020-03-29 17:48:26 +010019#include <mutex>
Keith Davis3201eea2019-10-24 17:30:41 +010020#include <queue>
Jim Flynn4e755a52020-03-29 17:48:26 +010021#include <thread>
Keith Davis3201eea2019-10-24 17:30:41 +010022
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000023namespace arm
Keith Davis3201eea2019-10-24 17:30:41 +010024{
25
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000026namespace pipe
Keith Davis3201eea2019-10-24 17:30:41 +010027{
28
Jim Flynn01d02812020-04-29 21:12:13 +010029// forward declaration
30class FileOnlyProfilingConnection;
Keith Davis3201eea2019-10-24 17:30:41 +010031
Jim Flynn01d02812020-04-29 21:12:13 +010032class StreamMetaDataProcessor : public ILocalPacketHandler
Keith Davis3201eea2019-10-24 17:30:41 +010033{
34public:
Jim Flynn01d02812020-04-29 21:12:13 +010035 explicit StreamMetaDataProcessor(FileOnlyProfilingConnection* fileOnlyProfilingConnection) :
36 m_FileOnlyProfilingConnection(fileOnlyProfilingConnection),
37 m_MetaDataPacketHeader(ConstructHeader(0, 0)) {};
38
39 std::vector<uint32_t> GetHeadersAccepted() override;
40
Jim Flynnbbfe6032020-07-20 16:57:44 +010041 void HandlePacket(const arm::pipe::Packet& packet) override;
Jim Flynn01d02812020-04-29 21:12:13 +010042
43private:
44 FileOnlyProfilingConnection* m_FileOnlyProfilingConnection;
45 uint32_t m_MetaDataPacketHeader;
46
47 static uint32_t ToUint32(const unsigned char* data, TargetEndianness endianness);
48};
49
50class FileOnlyProfilingConnection : public IProfilingConnection, public IInternalProfilingConnection
51{
52public:
Jim Flynn4c9ed1d2022-01-23 23:57:20 +000053 explicit FileOnlyProfilingConnection(const ProfilingOptions& options)
Keith Davis3201eea2019-10-24 17:30:41 +010054 : m_Options(options)
Jim Flynn01d02812020-04-29 21:12:13 +010055 , m_Endianness(TargetEndianness::LeWire) // Set a sensible default.
56 // StreamMetaDataProcessor will set a real value.
Jim Flynn4e755a52020-03-29 17:48:26 +010057 , m_IsRunning(false)
58 , m_KeepRunning(false)
59 , m_Timeout(1000)
60 {
Jim Flynn01d02812020-04-29 21:12:13 +010061 // 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 Flynn4e755a52020-03-29 17:48:26 +010066 {
67 AddLocalPacketHandler(localPacketHandler);
68 }
Jim Flynn01d02812020-04-29 21:12:13 +010069 if (!m_PacketHandlers.empty())
Jim Flynn4e755a52020-03-29 17:48:26 +010070 {
71 StartProcessingThread();
72 }
73 // NOTE: could add timeout to the external profiling options
74 };
Keith Davis3201eea2019-10-24 17:30:41 +010075
Jim Flynn01d02812020-04-29 21:12:13 +010076 ~FileOnlyProfilingConnection() override;
Keith Davis3201eea2019-10-24 17:30:41 +010077
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 Flynnbbfe6032020-07-20 16:57:44 +010086 arm::pipe::Packet ReadPacket(uint32_t timeout) override;
Keith Davis3201eea2019-10-24 17:30:41 +010087
Jim Flynn01d02812020-04-29 21:12:13 +010088 void SetEndianess(const TargetEndianness& endianness) override //IInternalProfilingConnection
89 {
90 m_Endianness = endianness;
91 }
92
Jim Flynnbbfe6032020-07-20 16:57:44 +010093 void ReturnPacket(arm::pipe::Packet& packet) override; //IInternalProfilingConnection
Jim Flynn01d02812020-04-29 21:12:13 +010094
Keith Davis3201eea2019-10-24 17:30:41 +010095private:
Jim Flynn4e755a52020-03-29 17:48:26 +010096 void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler);
97 void StartProcessingThread();
98 void ClearReadableList();
Jim Flynnbbfe6032020-07-20 16:57:44 +010099 void DispatchPacketToHandlers(const arm::pipe::Packet& packet);
Jim Flynn4e755a52020-03-29 17:48:26 +0100100
Keith Davis3201eea2019-10-24 17:30:41 +0100101 void Fail(const std::string& errorMessage);
102
Jim Flynnbbfe6032020-07-20 16:57:44 +0100103 void ForwardPacketToHandlers(arm::pipe::Packet& packet);
Jim Flynn4e755a52020-03-29 17:48:26 +0100104 void ServiceLocalHandlers();
105
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000106 ProfilingOptions m_Options;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100107 std::queue<arm::pipe::Packet> m_PacketQueue;
Keith Davis3201eea2019-10-24 17:30:41 +0100108 TargetEndianness m_Endianness;
Colm Donelan4cace322019-11-20 14:59:12 +0000109
110 std::mutex m_PacketAvailableMutex;
111 std::condition_variable m_ConditionPacketAvailable;
Jim Flynn4e755a52020-03-29 17:48:26 +0100112
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 Flynnbbfe6032020-07-20 16:57:44 +0100118 std::queue<arm::pipe::Packet> m_ReadableList;
Jim Flynn4e755a52020-03-29 17:48:26 +0100119 // 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 Davis3201eea2019-10-24 17:30:41 +0100129};
130
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000131} // namespace pipe
Keith Davis3201eea2019-10-24 17:30:41 +0100132
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000133} // namespace arm