• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ICounterDirectory.hpp"
10 #include "ISendCounterPacket.hpp"
11 #include "ProfilingUtils.hpp"
12 
13 #include <type_traits>
14 
15 namespace armnn
16 {
17 
18 namespace profiling
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)30     SendCounterPacket(IBufferManager& buffer)
31         : m_BufferManager(buffer)
32     {}
33 
34     void SendStreamMetaDataPacket() override;
35 
36     void SendCounterDirectoryPacket(const ICounterDirectory& counterDirectory) override;
37 
38     void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector& values) override;
39 
40     void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod,
41                                             const std::vector<uint16_t>& selectedCounterIds) override;
42 
43 private:
44     template <typename ExceptionType>
CancelOperationAndThrow(const std::string & errorMessage)45     void CancelOperationAndThrow(const std::string& errorMessage)
46     {
47         // Throw a runtime exception with the given error message
48         throw ExceptionType(errorMessage);
49     }
50 
51     template <typename ExceptionType>
CancelOperationAndThrow(IPacketBufferPtr & writerBuffer,const std::string & errorMessage)52     void CancelOperationAndThrow(IPacketBufferPtr& writerBuffer, const std::string& errorMessage)
53     {
54         if (std::is_same<ExceptionType, armnn::profiling::BufferExhaustion>::value)
55         {
56             m_BufferManager.FlushReadList();
57         }
58 
59         if (writerBuffer != nullptr)
60         {
61             // Cancel the operation
62             m_BufferManager.Release(writerBuffer);
63         }
64 
65         // Throw a runtime exception with the given error message
66         throw ExceptionType(errorMessage);
67     }
68 
69     IBufferManager& m_BufferManager;
70 
71 protected:
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);
86 };
87 
88 } // namespace profiling
89 
90 } // namespace armnn
91