ArmNN
 20.02
SendCounterPacket.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. 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  static const unsigned int PIPE_MAGIC = 0x45495434;
44 
45 private:
46  template <typename ExceptionType>
47  void CancelOperationAndThrow(const std::string& errorMessage)
48  {
49  // Throw a runtime exception with the given error message
50  throw ExceptionType(errorMessage);
51  }
52 
53  template <typename ExceptionType>
54  void CancelOperationAndThrow(IPacketBufferPtr& writerBuffer, const std::string& errorMessage)
55  {
56  if (std::is_same<ExceptionType, armnn::profiling::BufferExhaustion>::value)
57  {
58  m_BufferManager.FlushReadList();
59  }
60 
61  if (writerBuffer != nullptr)
62  {
63  // Cancel the operation
64  m_BufferManager.Release(writerBuffer);
65  }
66 
67  // Throw a runtime exception with the given error message
68  throw ExceptionType(errorMessage);
69  }
70 
71  IBufferManager& m_BufferManager;
72 
73 protected:
74  // Helper methods, protected for testing
75  bool CreateCategoryRecord(const CategoryPtr& category,
76  const Counters& counters,
77  CategoryRecord& categoryRecord,
78  std::string& errorMessage);
79  bool CreateDeviceRecord(const DevicePtr& device,
80  DeviceRecord& deviceRecord,
81  std::string& errorMessage);
82  bool CreateCounterSetRecord(const CounterSetPtr& counterSet,
83  CounterSetRecord& counterSetRecord,
84  std::string& errorMessage);
85  bool CreateEventRecord(const CounterPtr& counter,
86  EventRecord& eventRecord,
87  std::string& errorMessage);
88 };
89 
90 } // namespace profiling
91 
92 } // 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) 2020 ARM Limited.
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
static const unsigned int PIPE_MAGIC
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)