1 // 2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 // 5 6 #pragma once 7 8 #include "IBufferManager.hpp" 9 #include "ProfilingUtils.hpp" 10 11 #include <client/include/ISendCounterPacket.hpp> 12 13 #include <type_traits> 14 15 namespace arm 16 { 17 18 namespace pipe 19 { 20 21 class SendCounterPacket : public ISendCounterPacket 22 { 23 public: 24 using CategoryRecord = std::vector<uint32_t>; 25 using DeviceRecord = std::vector<uint32_t>; 26 using CounterSetRecord = std::vector<uint32_t>; 27 using EventRecord = std::vector<uint32_t>; 28 using IndexValuePairsVector = std::vector<CounterValue>; 29 SendCounterPacket(IBufferManager & buffer,const std::string & softwareInfo,const std::string & softwareVersion,const std::string & hardwareVersion)30 SendCounterPacket(IBufferManager& buffer, 31 const std::string& softwareInfo, 32 const std::string& softwareVersion, 33 const std::string& hardwareVersion) 34 : m_BufferManager(buffer), 35 m_SoftwareInfo(softwareInfo), 36 m_SoftwareVersion(softwareVersion), 37 m_HardwareVersion(hardwareVersion) 38 {} 39 40 void SendStreamMetaDataPacket() override; 41 42 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override; 43 44 void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) override; 45 46 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, 47 const std::vector<uint16_t>& selectedCounterIds) override; 48 49 private: 50 template <typename ExceptionType> CancelOperationAndThrow(const std::string & errorMessage)51 void CancelOperationAndThrow(const std::string& errorMessage) 52 { 53 // Throw a runtime exception with the given error message 54 throw ExceptionType(errorMessage); 55 } 56 57 template <typename ExceptionType> CancelOperationAndThrow(IPacketBufferPtr & writerBuffer,const std::string & errorMessage)58 void CancelOperationAndThrow(IPacketBufferPtr& writerBuffer, const std::string& errorMessage) 59 { 60 if (std::is_same<ExceptionType, arm::pipe::BufferExhaustion>::value) 61 { 62 m_BufferManager.FlushReadList(); 63 } 64 65 if (writerBuffer != nullptr) 66 { 67 // Cancel the operation 68 m_BufferManager.Release(writerBuffer); 69 } 70 71 // Throw a runtime exception with the given error message 72 throw ExceptionType(errorMessage); 73 } 74 75 IBufferManager& m_BufferManager; 76 77 protected: 78 // Helper methods, protected for testing 79 bool CreateCategoryRecord(const CategoryPtr& category, 80 const Counters& counters, 81 CategoryRecord& categoryRecord, 82 std::string& errorMessage); 83 bool CreateDeviceRecord(const DevicePtr& device, 84 DeviceRecord& deviceRecord, 85 std::string& errorMessage); 86 bool CreateCounterSetRecord(const CounterSetPtr& counterSet, 87 CounterSetRecord& counterSetRecord, 88 std::string& errorMessage); 89 bool CreateEventRecord(const CounterPtr& counter, 90 EventRecord& eventRecord, 91 std::string& errorMessage); 92 private: 93 std::string m_SoftwareInfo; 94 std::string m_SoftwareVersion; 95 std::string m_HardwareVersion; 96 }; 97 98 } // namespace pipe 99 100 } // namespace arm 101