blob: db893c026af1db71e22e27235557a40c6ff1b36c [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 Martincigh24e8f922019-09-19 11:57:46 +01009#include "ICounterDirectory.hpp"
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010010#include "ISendCounterPacket.hpp"
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010011#include "ProfilingUtils.hpp"
Matteo Martincigh24e8f922019-09-19 11:57:46 +010012
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010013#include <type_traits>
Ferran Balaguer73882172019-09-02 16:39:42 +010014
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000015namespace arm
Ferran Balaguer73882172019-09-02 16:39:42 +010016{
17
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000018namespace pipe
Ferran Balaguer73882172019-09-02 16:39:42 +010019{
20
21class SendCounterPacket : public ISendCounterPacket
22{
23public:
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010024 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>;
Finn Williams032bc742020-02-12 11:02:34 +000028 using IndexValuePairsVector = std::vector<CounterValue>;
Francis Murtagh3a161982019-09-04 15:25:02 +010029
Sadik Armagan3896b472020-02-10 12:24:15 +000030 SendCounterPacket(IBufferManager& buffer)
31 : m_BufferManager(buffer)
Matteo Martincigh149528e2019-09-05 12:02:04 +010032 {}
Ferran Balaguer73882172019-09-02 16:39:42 +010033
34 void SendStreamMetaDataPacket() override;
35
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010036 void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override;
Ferran Balaguer73882172019-09-02 16:39:42 +010037
Francis Murtagh3a161982019-09-04 15:25:02 +010038 void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) override;
Ferran Balaguer73882172019-09-02 16:39:42 +010039
40 void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
41 const std::vector<uint16_t>& selectedCounterIds) override;
42
Ferran Balaguer73882172019-09-02 16:39:42 +010043private:
Matteo Martincigh149528e2019-09-05 12:02:04 +010044 template <typename ExceptionType>
45 void CancelOperationAndThrow(const std::string& errorMessage)
46 {
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010047 // Throw a runtime exception with the given error message
48 throw ExceptionType(errorMessage);
49 }
50
51 template <typename ExceptionType>
Matteo Martincigh2ffcc412019-11-05 11:47:40 +000052 void CancelOperationAndThrow(IPacketBufferPtr& writerBuffer, const std::string& errorMessage)
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010053 {
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000054 if (std::is_same<ExceptionType, BufferExhaustion>::value)
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010055 {
Sadik Armagan3896b472020-02-10 12:24:15 +000056 m_BufferManager.FlushReadList();
Narumol Prangnawarat0ec068f2019-09-30 16:20:20 +010057 }
Matteo Martincigh5d737fb2019-10-07 13:05:13 +010058
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010059 if (writerBuffer != nullptr)
60 {
61 // Cancel the operation
62 m_BufferManager.Release(writerBuffer);
63 }
Matteo Martincigh149528e2019-09-05 12:02:04 +010064
65 // Throw a runtime exception with the given error message
66 throw ExceptionType(errorMessage);
67 }
68
Narumol Prangnawarat404b2752019-09-24 17:23:16 +010069 IBufferManager& m_BufferManager;
Matteo Martincigh42f9d9e2019-09-05 12:02:04 +010070
71protected:
72 // Helper methods, protected for testing
73 bool CreateCategoryRecord(const CategoryPtr& category,
74 const Counters& counters,
75 CategoryRecord& categoryRecord,
76 std::string& errorMessage);
77 bool CreateDeviceRecord(const DevicePtr& device,
78 DeviceRecord& deviceRecord,
79 std::string& errorMessage);
80 bool CreateCounterSetRecord(const CounterSetPtr& counterSet,
81 CounterSetRecord& counterSetRecord,
82 std::string& errorMessage);
83 bool CreateEventRecord(const CounterPtr& counter,
84 EventRecord& eventRecord,
85 std::string& errorMessage);
Ferran Balaguer73882172019-09-02 16:39:42 +010086};
87
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000088} // namespace pipe
Ferran Balaguer73882172019-09-02 16:39:42 +010089
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000090} // namespace arm