ArmNN
 21.05
SendCounterPacket.hpp
Go to the documentation of this file.
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 
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 
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>
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>
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
std::vector< uint32_t > CounterSetRecord
std::vector< CounterValue > IndexValuePairsVector
bool CreateDeviceRecord(const DevicePtr &device, DeviceRecord &deviceRecord, std::string &errorMessage)
void SendCounterDirectoryPacket(const ICounterDirectory &counterDirectory) override
Create and write a CounterDirectoryPacket from the parameters to the buffer.
std::unordered_map< uint16_t, CounterPtr > Counters
std::unique_ptr< Device > DevicePtr
void SendStreamMetaDataPacket() override
Create and write a StreamMetaDataPacket in the buffer.
std::unique_ptr< CounterSet > CounterSetPtr
Copyright (c) 2021 ARM Limited and Contributors.
std::shared_ptr< Counter > CounterPtr
void SendPeriodicCounterCapturePacket(uint64_t timestamp, const IndexValuePairsVector &values) override
Create and write a PeriodicCounterCapturePacket from the parameters to the buffer.
bool CreateCounterSetRecord(const CounterSetPtr &counterSet, CounterSetRecord &counterSetRecord, std::string &errorMessage)
virtual void Release(IPacketBufferPtr &packetBuffer)=0
SendCounterPacket(IBufferManager &buffer)
void SendPeriodicCounterSelectionPacket(uint32_t capturePeriod, const std::vector< uint16_t > &selectedCounterIds) override
Create and write a PeriodicCounterSelectionPacket from the parameters to the buffer.
std::vector< uint32_t > CategoryRecord
std::unique_ptr< Category > CategoryPtr
bool CreateEventRecord(const CounterPtr &counter, EventRecord &eventRecord, std::string &errorMessage)
std::unique_ptr< IPacketBuffer > IPacketBufferPtr
bool CreateCategoryRecord(const CategoryPtr &category, const Counters &counters, CategoryRecord &categoryRecord, std::string &errorMessage)