1 // 2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 6 #pragma once 7 8 #include <common/include/CommandHandlerFunctor.hpp> 9 #include <common/include/CounterDirectory.hpp> 10 11 #include <atomic> 12 13 namespace arm 14 { 15 16 namespace pipe 17 { 18 19 struct CounterDirectoryEventRecord 20 { 21 uint16_t m_CounterClass; 22 std::string m_CounterDescription; 23 uint16_t m_CounterInterpolation; 24 double m_CounterMultiplier; 25 std::string m_CounterName; 26 uint16_t m_CounterSetUid; 27 uint16_t m_CounterUid; 28 arm::pipe::Optional<std::string> m_CounterUnits; 29 uint16_t m_DeviceUid; 30 uint16_t m_MaxCounterUid; 31 }; 32 33 class DirectoryCaptureCommandHandler : public arm::pipe::CommandHandlerFunctor 34 { 35 36 public: DirectoryCaptureCommandHandler(const std::string & applicationName,uint32_t familyId,uint32_t packetId,uint32_t version,bool quietOperation=true)37 DirectoryCaptureCommandHandler(const std::string& applicationName, 38 uint32_t familyId, 39 uint32_t packetId, 40 uint32_t version, 41 bool quietOperation = true) 42 : CommandHandlerFunctor(familyId, packetId, version) 43 , m_ApplicationName(applicationName) 44 , m_QuietOperation(quietOperation) 45 , m_AlreadyParsed(false) 46 {} 47 48 void operator()(const arm::pipe::Packet& packet) override; 49 50 const ICounterDirectory& GetCounterDirectory() const; 51 ParsedCounterDirectory()52 bool ParsedCounterDirectory() 53 { 54 return m_AlreadyParsed.load(); 55 } 56 57 /** 58 * Given a Uid that came from a copy of the counter directory translate it to the original. 59 * 60 * @param copyUid 61 * @return the original Uid that the copy maps to. 62 */ TranslateUIDCopyToOriginal(uint16_t copyUid)63 uint16_t TranslateUIDCopyToOriginal(uint16_t copyUid) 64 { 65 return m_UidTranslation[copyUid]; 66 } 67 68 private: 69 void ParseData(const arm::pipe::Packet& packet); 70 71 void ReadCategoryRecords(const unsigned char* data, uint32_t offset, std::vector<uint32_t> categoryOffsets); 72 73 std::vector<CounterDirectoryEventRecord> 74 ReadEventRecords(const unsigned char* data, uint32_t offset, std::vector<uint32_t> eventRecordsOffsets); 75 76 std::string GetStringNameFromBuffer(const unsigned char* data, uint32_t offset); 77 bool IsValidChar(unsigned char c); 78 79 std::string m_ApplicationName; 80 CounterDirectory m_CounterDirectory; 81 std::unordered_map<uint16_t, uint16_t> m_UidTranslation; 82 bool m_QuietOperation; 83 // We can only parse the counter directory once per instance. It won't change anyway as it's static 84 // per instance of ArmNN. 85 std::atomic<bool> m_AlreadyParsed; 86 }; 87 88 } // namespace pipe 89 90 } // namespace arm 91