blob: 047dd3c7c97e97b5bf8e15d108ed1f0d870603eb [file] [log] [blame]
Ferran Balaguer73882172019-09-02 16:39:42 +01001//
Jim Flynn83d08a92020-07-09 13:48:16 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Ferran Balaguer73882172019-09-02 16:39:42 +01003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Narumol Prangnawarat404b2752019-09-24 17:23:16 +01008#include "IBufferManager.hpp"
Matteo Martincigh5d737fb2019-10-07 13:05:13 +01009#include "ISendCounterPacket.hpp"
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010010#include "ProfilingUtils.hpp"
Matteo Martincigh24e8f922019-09-19 11:57:46 +010011
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010012#include <type_traits>
Ferran Balaguer73882172019-09-02 16:39:42 +010013
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000014namespace arm
Ferran Balaguer73882172019-09-02 16:39:42 +010015{
16
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000017namespace pipe
Ferran Balaguer73882172019-09-02 16:39:42 +010018{
19
20class SendCounterPacket : public ISendCounterPacket
21{
22public:
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010023 using CategoryRecord = std::vector<uint32_t>;
24 using DeviceRecord = std::vector<uint32_t>;
25 using CounterSetRecord = std::vector<uint32_t>;
26 using EventRecord = std::vector<uint32_t>;
Finn Williams032bc742020-02-12 11:02:34 +000027 using IndexValuePairsVector = std::vector<CounterValue>;
Francis Murtagh3a161982019-09-04 15:25:02 +010028
Jim Flynn9c85b412022-03-16 00:27:43 +000029 SendCounterPacket(IBufferManager& buffer,
30 const std::string& softwareInfo,
31 const std::string& softwareVersion,
32 const std::string& hardwareVersion)
33 : m_BufferManager(buffer),
34 m_SoftwareInfo(softwareInfo),
35 m_SoftwareVersion(softwareVersion),
36 m_HardwareVersion(hardwareVersion)
Matteo Martincigh149528e2019-09-05 12:02:04 +010037 {}
Ferran Balaguer73882172019-09-02 16:39:42 +010038
39 void SendStreamMetaDataPacket() override;
40
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010041 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override;
Ferran Balaguer73882172019-09-02 16:39:42 +010042
Francis Murtagh3a161982019-09-04 15:25:02 +010043 void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) override;
Ferran Balaguer73882172019-09-02 16:39:42 +010044
45 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
46 const std::vector<uint16_t>& selectedCounterIds) override;
47
Ferran Balaguer73882172019-09-02 16:39:42 +010048private:
Matteo Martincigh149528e2019-09-05 12:02:04 +010049 template <typename ExceptionType>
50 void CancelOperationAndThrow(const std::string& errorMessage)
51 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010052 // Throw a runtime exception with the given error message
53 throw ExceptionType(errorMessage);
54 }
55
56 template <typename ExceptionType>
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000057 void CancelOperationAndThrow(IPacketBufferPtr& writerBuffer, const std::string& errorMessage)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010058 {
Jim Flynnf9db3ef2022-03-08 21:23:44 +000059 if (std::is_same<ExceptionType, arm::pipe::BufferExhaustion>::value)
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010060 {
Sadik Armagan3896b472020-02-10 12:24:15 +000061 m_BufferManager.FlushReadList();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010062 }
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010063
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010064 if (writerBuffer != nullptr)
65 {
66 // Cancel the operation
67 m_BufferManager.Release(writerBuffer);
68 }
Matteo Martincigh149528e2019-09-05 12:02:04 +010069
70 // Throw a runtime exception with the given error message
71 throw ExceptionType(errorMessage);
72 }
73
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010074 IBufferManager& m_BufferManager;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010075
76protected:
77 // Helper methods, protected for testing
78 bool CreateCategoryRecord(const CategoryPtr& category,
79 const Counters& counters,
80 CategoryRecord& categoryRecord,
81 std::string& errorMessage);
82 bool CreateDeviceRecord(const DevicePtr& device,
83 DeviceRecord& deviceRecord,
84 std::string& errorMessage);
85 bool CreateCounterSetRecord(const CounterSetPtr& counterSet,
86 CounterSetRecord& counterSetRecord,
87 std::string& errorMessage);
88 bool CreateEventRecord(const CounterPtr& counter,
89 EventRecord& eventRecord,
90 std::string& errorMessage);
Jim Flynn9c85b412022-03-16 00:27:43 +000091private:
92 std::string m_SoftwareInfo;
93 std::string m_SoftwareVersion;
94 std::string m_HardwareVersion;
Ferran Balaguer73882172019-09-02 16:39:42 +010095};
96
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000097} // namespace pipe
Ferran Balaguer73882172019-09-02 16:39:42 +010098
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000099} // namespace arm