blob: c7e60f564e54d0df30f57aa631fd5ce6f8f18312 [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
Keith Davis3201eea2019-10-24 17:30:41 +01008#include "IProfilingConnection.hpp"
9#include "ProfilingUtils.hpp"
Keith Davis3201eea2019-10-24 17:30:41 +010010
Jim Flynn27761832022-03-20 21:52:17 +000011#include <client/include/ILocalPacketHandler.hpp>
12#include <client/include/ProfilingOptions.hpp>
13
Jim Flynn4a962112022-03-13 20:18:58 +000014#include <common/include/Assert.hpp>
Jim Flynnbbfe6032020-07-20 16:57:44 +010015#include <common/include/Packet.hpp>
16
Jim Flynnc454ac92022-03-16 18:43:18 +000017#include <server/include/timelineDecoder/DirectoryCaptureCommandHandler.hpp>
18
Jim Flynn4e755a52020-03-29 17:48:26 +010019#include <atomic>
Colm Donelan4cace322019-11-20 14:59:12 +000020#include <condition_variable>
Keith Davis3201eea2019-10-24 17:30:41 +010021#include <fstream>
Jim Flynndecd08b2022-03-13 22:35:46 +000022#include <map>
Jim Flynn4e755a52020-03-29 17:48:26 +010023#include <mutex>
Keith Davis3201eea2019-10-24 17:30:41 +010024#include <queue>
Jim Flynn4e755a52020-03-29 17:48:26 +010025#include <thread>
Keith Davis3201eea2019-10-24 17:30:41 +010026
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000027namespace arm
Keith Davis3201eea2019-10-24 17:30:41 +010028{
29
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000030namespace pipe
Keith Davis3201eea2019-10-24 17:30:41 +010031{
32
Jim Flynn01d02812020-04-29 21:12:13 +010033// forward declaration
34class FileOnlyProfilingConnection;
Keith Davis3201eea2019-10-24 17:30:41 +010035
Jim Flynn01d02812020-04-29 21:12:13 +010036class StreamMetaDataProcessor : public ILocalPacketHandler
Keith Davis3201eea2019-10-24 17:30:41 +010037{
38public:
Jim Flynn01d02812020-04-29 21:12:13 +010039 explicit StreamMetaDataProcessor(FileOnlyProfilingConnection* fileOnlyProfilingConnection) :
40 m_FileOnlyProfilingConnection(fileOnlyProfilingConnection),
41 m_MetaDataPacketHeader(ConstructHeader(0, 0)) {};
42
43 std::vector<uint32_t> GetHeadersAccepted() override;
44
Jim Flynnbbfe6032020-07-20 16:57:44 +010045 void HandlePacket(const arm::pipe::Packet& packet) override;
Jim Flynn01d02812020-04-29 21:12:13 +010046
47private:
48 FileOnlyProfilingConnection* m_FileOnlyProfilingConnection;
49 uint32_t m_MetaDataPacketHeader;
50
51 static uint32_t ToUint32(const unsigned char* data, TargetEndianness endianness);
52};
53
54class FileOnlyProfilingConnection : public IProfilingConnection, public IInternalProfilingConnection
55{
56public:
Jim Flynn4c9ed1d2022-01-23 23:57:20 +000057 explicit FileOnlyProfilingConnection(const ProfilingOptions& options)
Keith Davis3201eea2019-10-24 17:30:41 +010058 : m_Options(options)
Jim Flynn01d02812020-04-29 21:12:13 +010059 , m_Endianness(TargetEndianness::LeWire) // Set a sensible default.
60 // StreamMetaDataProcessor will set a real value.
Jim Flynn4e755a52020-03-29 17:48:26 +010061 , m_IsRunning(false)
62 , m_KeepRunning(false)
63 , m_Timeout(1000)
64 {
Jim Flynn01d02812020-04-29 21:12:13 +010065 // add the StreamMetaDataProcessor
66 auto streamMetaDataProcessor = std::make_shared<StreamMetaDataProcessor>(this);
67 AddLocalPacketHandler(streamMetaDataProcessor);
68 // and any additional ones added by the users
69 for (const ILocalPacketHandlerSharedPtr& localPacketHandler : options.m_LocalPacketHandlers)
Jim Flynn4e755a52020-03-29 17:48:26 +010070 {
71 AddLocalPacketHandler(localPacketHandler);
72 }
Jim Flynn01d02812020-04-29 21:12:13 +010073 if (!m_PacketHandlers.empty())
Jim Flynn4e755a52020-03-29 17:48:26 +010074 {
75 StartProcessingThread();
76 }
77 // NOTE: could add timeout to the external profiling options
78 };
Keith Davis3201eea2019-10-24 17:30:41 +010079
Jim Flynn01d02812020-04-29 21:12:13 +010080 ~FileOnlyProfilingConnection() override;
Keith Davis3201eea2019-10-24 17:30:41 +010081
82 bool IsOpen() const override;
83
84 void Close() override;
85
86 // This is effectively receiving a data packet from ArmNN.
87 bool WritePacket(const unsigned char* buffer, uint32_t length) override;
88
89 // Sending a packet back to ArmNN.
Jim Flynnbbfe6032020-07-20 16:57:44 +010090 arm::pipe::Packet ReadPacket(uint32_t timeout) override;
Keith Davis3201eea2019-10-24 17:30:41 +010091
Jim Flynn01d02812020-04-29 21:12:13 +010092 void SetEndianess(const TargetEndianness& endianness) override //IInternalProfilingConnection
93 {
94 m_Endianness = endianness;
95 }
96
Jim Flynnbbfe6032020-07-20 16:57:44 +010097 void ReturnPacket(arm::pipe::Packet& packet) override; //IInternalProfilingConnection
Jim Flynn01d02812020-04-29 21:12:13 +010098
Keith Davis3201eea2019-10-24 17:30:41 +010099private:
Jim Flynn4e755a52020-03-29 17:48:26 +0100100 void AddLocalPacketHandler(ILocalPacketHandlerSharedPtr localPacketHandler);
101 void StartProcessingThread();
102 void ClearReadableList();
Jim Flynnbbfe6032020-07-20 16:57:44 +0100103 void DispatchPacketToHandlers(const arm::pipe::Packet& packet);
Jim Flynn4e755a52020-03-29 17:48:26 +0100104
Keith Davis3201eea2019-10-24 17:30:41 +0100105 void Fail(const std::string& errorMessage);
106
Jim Flynnbbfe6032020-07-20 16:57:44 +0100107 void ForwardPacketToHandlers(arm::pipe::Packet& packet);
Jim Flynn4e755a52020-03-29 17:48:26 +0100108 void ServiceLocalHandlers();
109
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000110 ProfilingOptions m_Options;
Jim Flynnbbfe6032020-07-20 16:57:44 +0100111 std::queue<arm::pipe::Packet> m_PacketQueue;
Keith Davis3201eea2019-10-24 17:30:41 +0100112 TargetEndianness m_Endianness;
Colm Donelan4cace322019-11-20 14:59:12 +0000113
114 std::mutex m_PacketAvailableMutex;
115 std::condition_variable m_ConditionPacketAvailable;
Jim Flynn4e755a52020-03-29 17:48:26 +0100116
117 std::vector<ILocalPacketHandlerSharedPtr> m_PacketHandlers;
118 std::map<uint32_t, std::vector<ILocalPacketHandlerSharedPtr>> m_IndexedHandlers;
119 std::vector<ILocalPacketHandlerSharedPtr> m_UniversalHandlers;
120
121 // List of readable packets for the local packet handlers
Jim Flynnbbfe6032020-07-20 16:57:44 +0100122 std::queue<arm::pipe::Packet> m_ReadableList;
Jim Flynn4e755a52020-03-29 17:48:26 +0100123 // Mutex and condition variable for the readable packet list
124 std::mutex m_ReadableMutex;
125 std::condition_variable m_ConditionPacketReadable;
126 // thread that takes items from the readable list and dispatches them
127 // to the handlers.
128 std::thread m_LocalHandlersThread;
129 // atomic booleans that control the operation of the local handlers thread
130 std::atomic<bool> m_IsRunning;
131 std::atomic<bool> m_KeepRunning;
132 int m_Timeout;
Keith Davis3201eea2019-10-24 17:30:41 +0100133};
134
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000135} // namespace pipe
Keith Davis3201eea2019-10-24 17:30:41 +0100136
Cathal Corbett5aa9fd72022-02-25 15:33:28 +0000137} // namespace arm