ArmNN
 20.02
CounterDirectory Class Referencefinal

#include <CounterDirectory.hpp>

Inheritance diagram for CounterDirectory:
ICounterDirectory ICounterRegistry

Public Member Functions

 CounterDirectory ()=default
 
 ~CounterDirectory ()=default
 
const CategoryRegisterCategory (const std::string &categoryName) override
 
const DeviceRegisterDevice (const std::string &deviceName, uint16_t cores=0, const Optional< std::string > &parentCategoryName=EmptyOptional()) override
 
const CounterSetRegisterCounterSet (const std::string &counterSetName, uint16_t count=0, const Optional< std::string > &parentCategoryName=EmptyOptional()) override
 
const CounterRegisterCounter (const BackendId &backendId, const uint16_t uid, const std::string &parentCategoryName, uint16_t counterClass, uint16_t interpolation, double multiplier, const std::string &name, const std::string &description, const Optional< std::string > &units=EmptyOptional(), const Optional< uint16_t > &numberOfCores=EmptyOptional(), const Optional< uint16_t > &deviceUid=EmptyOptional(), const Optional< uint16_t > &counterSetUid=EmptyOptional()) override
 
uint16_t GetCategoryCount () const override
 
uint16_t GetDeviceCount () const override
 
uint16_t GetCounterSetCount () const override
 
uint16_t GetCounterCount () const override
 
const CategoriesGetCategories () const override
 
const DevicesGetDevices () const override
 
const CounterSetsGetCounterSets () const override
 
const CountersGetCounters () const override
 
const CategoryGetCategory (const std::string &name) const override
 
const DeviceGetDevice (uint16_t uid) const override
 
const CounterSetGetCounterSet (uint16_t uid) const override
 
const CounterGetCounter (uint16_t uid) const override
 
bool IsCategoryRegistered (const std::string &categoryName) const
 
bool IsDeviceRegistered (uint16_t deviceUid) const
 
bool IsDeviceRegistered (const std::string &deviceName) const
 
bool IsCounterSetRegistered (uint16_t counterSetUid) const
 
bool IsCounterSetRegistered (const std::string &counterSetName) const
 
bool IsCounterRegistered (uint16_t counterUid) const
 
bool IsCounterRegistered (const std::string &counterName) const
 
void Clear ()
 
- Public Member Functions inherited from ICounterDirectory
virtual ~ICounterDirectory ()
 
- Public Member Functions inherited from ICounterRegistry
virtual ~ICounterRegistry ()
 

Detailed Description

Definition at line 23 of file CounterDirectory.hpp.

Constructor & Destructor Documentation

◆ CounterDirectory()

CounterDirectory ( )
default

◆ ~CounterDirectory()

~CounterDirectory ( )
default

Member Function Documentation

◆ Clear()

void Clear ( )

Definition at line 439 of file CounterDirectory.cpp.

References OptionalBase::has_value(), and OptionalReferenceSwitch< std::is_reference< T >::value, T >::value().

Referenced by CounterDirectory::GetCounters(), and ProfilingService::GetSendTimelinePacket().

440 {
441  // Clear all the counter directory contents
442  m_Categories.clear();
443  m_Devices.clear();
444  m_CounterSets.clear();
445  m_Counters.clear();
446 }

◆ GetCategories()

const Categories& GetCategories ( ) const
inlineoverridevirtual

Implements ICounterDirectory.

Definition at line 57 of file CounterDirectory.hpp.

57 { return m_Categories; }

◆ GetCategory()

const Category * GetCategory ( const std::string &  name) const
overridevirtual

Implements ICounterDirectory.

Definition at line 330 of file CounterDirectory.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), and CounterDirectory::GetCounters().

331 {
332  auto it = FindCategory(categoryName);
333  if (it == m_Categories.end())
334  {
335  return nullptr;
336  }
337 
338  const Category* category = it->get();
339  BOOST_ASSERT(category);
340 
341  return category;
342 }

◆ GetCategoryCount()

uint16_t GetCategoryCount ( ) const
inlineoverridevirtual

Implements ICounterDirectory.

Definition at line 51 of file CounterDirectory.hpp.

References armnn::numeric_cast().

Referenced by BOOST_AUTO_TEST_CASE().

51 { return boost::numeric_cast<uint16_t>(m_Categories.size()); }
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33

◆ GetCounter()

const Counter * GetCounter ( uint16_t  uid) const
overridevirtual

Implements ICounterDirectory.

Definition at line 374 of file CounterDirectory.cpp.

References Counter::m_MaxCounterUid, and Counter::m_Uid.

Referenced by BOOST_AUTO_TEST_CASE(), CounterDirectory::GetCounters(), and CounterDirectory::RegisterCounter().

375 {
376  auto it = FindCounter(counterUid);
377  if (it == m_Counters.end())
378  {
379  return nullptr;
380  }
381 
382  const Counter* counter = it->second.get();
383  BOOST_ASSERT(counter);
384  BOOST_ASSERT(counter->m_Uid <= counterUid);
385  BOOST_ASSERT(counter->m_Uid <= counter->m_MaxCounterUid);
386 
387  return counter;
388 }

◆ GetCounterCount()

uint16_t GetCounterCount ( ) const
inlineoverridevirtual

Implements ICounterDirectory.

Definition at line 54 of file CounterDirectory.hpp.

References armnn::numeric_cast().

Referenced by BOOST_AUTO_TEST_CASE(), and ProfilingService::GetCounterCount().

54 { return boost::numeric_cast<uint16_t>(m_Counters.size()); }
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33

◆ GetCounters()

◆ GetCounterSet()

const CounterSet * GetCounterSet ( uint16_t  uid) const
overridevirtual

Implements ICounterDirectory.

Definition at line 359 of file CounterDirectory.cpp.

References CounterSet::m_Uid.

Referenced by BOOST_AUTO_TEST_CASE(), and CounterDirectory::GetCounters().

360 {
361  auto it = FindCounterSet(counterSetUid);
362  if (it == m_CounterSets.end())
363  {
364  return nullptr;
365  }
366 
367  const CounterSet* counterSet = it->second.get();
368  BOOST_ASSERT(counterSet);
369  BOOST_ASSERT(counterSet->m_Uid == counterSetUid);
370 
371  return counterSet;
372 }

◆ GetCounterSetCount()

uint16_t GetCounterSetCount ( ) const
inlineoverridevirtual

Implements ICounterDirectory.

Definition at line 53 of file CounterDirectory.hpp.

References armnn::numeric_cast().

Referenced by BOOST_AUTO_TEST_CASE().

53 { return boost::numeric_cast<uint16_t>(m_CounterSets.size()); }
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33

◆ GetCounterSets()

const CounterSets& GetCounterSets ( ) const
inlineoverridevirtual

Implements ICounterDirectory.

Definition at line 59 of file CounterDirectory.hpp.

59 { return m_CounterSets; }

◆ GetDevice()

const Device * GetDevice ( uint16_t  uid) const
overridevirtual

Implements ICounterDirectory.

Definition at line 344 of file CounterDirectory.cpp.

References Device::m_Uid.

Referenced by BOOST_AUTO_TEST_CASE(), and CounterDirectory::GetCounters().

345 {
346  auto it = FindDevice(deviceUid);
347  if (it == m_Devices.end())
348  {
349  return nullptr;
350  }
351 
352  const Device* device = it->second.get();
353  BOOST_ASSERT(device);
354  BOOST_ASSERT(device->m_Uid == deviceUid);
355 
356  return device;
357 }

◆ GetDeviceCount()

uint16_t GetDeviceCount ( ) const
inlineoverridevirtual

Implements ICounterDirectory.

Definition at line 52 of file CounterDirectory.hpp.

References armnn::numeric_cast().

Referenced by BOOST_AUTO_TEST_CASE().

52 { return boost::numeric_cast<uint16_t>(m_Devices.size()); }
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33

◆ GetDevices()

const Devices& GetDevices ( ) const
inlineoverridevirtual

Implements ICounterDirectory.

Definition at line 58 of file CounterDirectory.hpp.

58 { return m_Devices; }

◆ IsCategoryRegistered()

bool IsCategoryRegistered ( const std::string &  categoryName) const

Definition at line 390 of file CounterDirectory.cpp.

Referenced by CounterDirectory::GetCounters(), ProfilingService::GetSendTimelinePacket(), and CounterDirectory::RegisterCategory().

391 {
392  auto it = FindCategory(categoryName);
393 
394  return it != m_Categories.end();
395 }

◆ IsCounterRegistered() [1/2]

bool IsCounterRegistered ( uint16_t  counterUid) const

Definition at line 425 of file CounterDirectory.cpp.

Referenced by CounterDirectory::GetCounters(), and ProfilingService::GetSendTimelinePacket().

426 {
427  auto it = FindCounter(counterUid);
428 
429  return it != m_Counters.end();
430 }

◆ IsCounterRegistered() [2/2]

bool IsCounterRegistered ( const std::string &  counterName) const

Definition at line 432 of file CounterDirectory.cpp.

433 {
434  auto it = FindCounter(counterName);
435 
436  return it != m_Counters.end();
437 }

◆ IsCounterSetRegistered() [1/2]

bool IsCounterSetRegistered ( uint16_t  counterSetUid) const

Definition at line 411 of file CounterDirectory.cpp.

Referenced by CounterDirectory::GetCounters(), CounterDirectory::RegisterCounter(), and CounterDirectory::RegisterCounterSet().

412 {
413  auto it = FindCounterSet(counterSetUid);
414 
415  return it != m_CounterSets.end();
416 }

◆ IsCounterSetRegistered() [2/2]

bool IsCounterSetRegistered ( const std::string &  counterSetName) const

Definition at line 418 of file CounterDirectory.cpp.

419 {
420  auto it = FindCounterSet(counterSetName);
421 
422  return it != m_CounterSets.end();
423 }

◆ IsDeviceRegistered() [1/2]

bool IsDeviceRegistered ( uint16_t  deviceUid) const

Definition at line 397 of file CounterDirectory.cpp.

Referenced by CounterDirectory::GetCounters(), and CounterDirectory::RegisterDevice().

398 {
399  auto it = FindDevice(deviceUid);
400 
401  return it != m_Devices.end();
402 }

◆ IsDeviceRegistered() [2/2]

bool IsDeviceRegistered ( const std::string &  deviceName) const

Definition at line 404 of file CounterDirectory.cpp.

405 {
406  auto it = FindDevice(deviceName);
407 
408  return it != m_Devices.end();
409 }

◆ RegisterCategory()

const Category * RegisterCategory ( const std::string &  categoryName)
overridevirtual

Implements ICounterRegistry.

Definition at line 21 of file CounterDirectory.cpp.

References CounterDirectory::IsCategoryRegistered().

Referenced by BOOST_AUTO_TEST_CASE(), and ProfilingService::GetSendTimelinePacket().

22 {
23  // Check that the given category name is valid
24  if (categoryName.empty() ||
25  !IsValidSwTraceString<SwTraceNameCharPolicy>(categoryName))
26  {
27  throw InvalidArgumentException("Trying to register a category with an invalid name");
28  }
29 
30  // Check that the given category is not already registered
31  if (IsCategoryRegistered(categoryName))
32  {
33  throw InvalidArgumentException(
34  boost::str(boost::format("Trying to register a category already registered (\"%1%\")")
35  % categoryName));
36  }
37 
38  // Create the category
39  CategoryPtr category = std::make_unique<Category>(categoryName);
40  BOOST_ASSERT(category);
41 
42  // Get the raw category pointer
43  const Category* categoryPtr = category.get();
44  BOOST_ASSERT(categoryPtr);
45 
46  // Register the category
47  m_Categories.insert(std::move(category));
48 
49  return categoryPtr;
50 }
bool IsCategoryRegistered(const std::string &categoryName) const
std::unique_ptr< Category > CategoryPtr

◆ RegisterCounter()

const Counter * RegisterCounter ( const BackendId backendId,
const uint16_t  uid,
const std::string &  parentCategoryName,
uint16_t  counterClass,
uint16_t  interpolation,
double  multiplier,
const std::string &  name,
const std::string &  description,
const Optional< std::string > &  units = EmptyOptional(),
const Optional< uint16_t > &  numberOfCores = EmptyOptional(),
const Optional< uint16_t > &  deviceUid = EmptyOptional(),
const Optional< uint16_t > &  counterSetUid = EmptyOptional() 
)
overridevirtual

Implements ICounterRegistry.

Definition at line 181 of file CounterDirectory.cpp.

References CounterDirectory::GetCounter(), armnn::profiling::GetNextCounterUids(), OptionalBase::has_value(), armnn::IgnoreUnused(), CounterDirectory::IsCounterSetRegistered(), Counter::m_Name, and OptionalReferenceSwitch< std::is_reference< T >::value, T >::value().

Referenced by BOOST_AUTO_TEST_CASE(), and ProfilingService::GetSendTimelinePacket().

193 {
194  IgnoreUnused(backendId);
195 
196  // Check that the given parent category name is valid
197  if (parentCategoryName.empty() ||
198  !IsValidSwTraceString<SwTraceNameCharPolicy>(parentCategoryName))
199  {
200  throw InvalidArgumentException("Trying to register a counter with an invalid parent category name");
201  }
202 
203  // Check that the given class is valid
204  if (counterClass != 0 && counterClass != 1)
205  {
206  throw InvalidArgumentException("Trying to register a counter with an invalid class");
207  }
208 
209  // Check that the given interpolation is valid
210  if (interpolation != 0 && interpolation != 1)
211  {
212  throw InvalidArgumentException("Trying to register a counter with an invalid interpolation");
213  }
214 
215  // Check that the given multiplier is valid
216  if (multiplier == .0f)
217  {
218  throw InvalidArgumentException("Trying to register a counter with an invalid multiplier");
219  }
220 
221  // Check that the given name is valid
222  if (name.empty() ||
223  !IsValidSwTraceString<SwTraceCharPolicy>(name))
224  {
225  throw InvalidArgumentException("Trying to register a counter with an invalid name");
226  }
227 
228  // Check that the given description is valid
229  if (description.empty() ||
230  !IsValidSwTraceString<SwTraceCharPolicy>(description))
231  {
232  throw InvalidArgumentException("Trying to register a counter with an invalid description");
233  }
234 
235  // Check that the given units are valid
236  if (units.has_value()
237  && !IsValidSwTraceString<SwTraceNameCharPolicy>(units.value()))
238  {
239  throw InvalidArgumentException("Trying to register a counter with a invalid units");
240  }
241 
242  // Check that the given parent category is registered
243  auto categoryIt = FindCategory(parentCategoryName);
244  if (categoryIt == m_Categories.end())
245  {
246  throw InvalidArgumentException(
247  boost::str(boost::format("Trying to connect a counter to a category "
248  "that is not registered (name: \"%1%\")")
249  % parentCategoryName));
250  }
251 
252  // Get the parent category
253  const CategoryPtr& parentCategory = *categoryIt;
254  BOOST_ASSERT(parentCategory);
255 
256  // Check that a counter with the given name is not already registered within the parent category
257  const std::vector<uint16_t>& parentCategoryCounters = parentCategory->m_Counters;
258  for (uint16_t parentCategoryCounterUid : parentCategoryCounters)
259  {
260  const Counter* parentCategoryCounter = GetCounter(parentCategoryCounterUid);
261  BOOST_ASSERT(parentCategoryCounter);
262 
263  if (parentCategoryCounter->m_Name == name)
264  {
265  throw InvalidArgumentException(
266  boost::str(boost::format("Trying to register a counter to category \"%1%\" with a name that "
267  "is already used within that category (name: \"%2%\")")
268  % parentCategoryName
269  % name));
270  }
271  }
272 
273  // Check that a counter set with the given (optional) UID is already registered
274  uint16_t counterSetUidValue = counterSetUid.has_value() ? counterSetUid.value() : 0;
275  if (counterSetUidValue > 0)
276  {
277  // Check that the (optional) counter set is already registered
278  if (!IsCounterSetRegistered(counterSetUidValue))
279  {
280  throw InvalidArgumentException(
281  boost::str(boost::format("Trying to connect a counter to a counter set that is "
282  "not registered (counter set UID: %1%)")
283  % counterSetUidValue));
284  }
285  }
286 
287  // Get the number of cores (this call may throw)
288  uint16_t deviceUidValue = deviceUid.has_value() ? deviceUid.value() : 0;
289  uint16_t deviceCores = GetNumberOfCores(numberOfCores, deviceUidValue);
290 
291  // Get the counter UIDs and calculate the max counter UID
292  std::vector<uint16_t> counterUids = GetNextCounterUids(uid, deviceCores);
293  BOOST_ASSERT(!counterUids.empty());
294  uint16_t maxCounterUid = deviceCores <= 1 ? counterUids.front() : counterUids.back();
295 
296  // Get the counter units
297  const std::string unitsValue = units.has_value() ? units.value() : "";
298 
299  // Create the counter
300  CounterPtr counter = std::make_shared<Counter>(armnn::profiling::BACKEND_ID,
301  counterUids.front(),
302  maxCounterUid,
303  counterClass,
304  interpolation,
305  multiplier,
306  name,
307  description,
308  unitsValue,
309  deviceUidValue,
310  counterSetUidValue);
311  BOOST_ASSERT(counter);
312 
313  // Get the raw counter pointer
314  const Counter* counterPtr = counter.get();
315  BOOST_ASSERT(counterPtr);
316 
317  // Process multiple counters if necessary
318  for (uint16_t counterUid : counterUids)
319  {
320  // Connect the counter to the parent category
321  parentCategory->m_Counters.push_back(counterUid);
322 
323  // Register the counter
324  m_Counters.insert(std::make_pair(counterUid, counter));
325  }
326 
327  return counterPtr;
328 }
void IgnoreUnused(Ts &&...)
std::shared_ptr< Counter > CounterPtr
std::vector< uint16_t > GetNextCounterUids(uint16_t firstUid, uint16_t cores)
const Counter * GetCounter(uint16_t uid) const override
std::unique_ptr< Category > CategoryPtr
bool IsCounterSetRegistered(uint16_t counterSetUid) const

◆ RegisterCounterSet()

const CounterSet * RegisterCounterSet ( const std::string &  counterSetName,
uint16_t  count = 0,
const Optional< std::string > &  parentCategoryName = EmptyOptional() 
)
overridevirtual

Implements ICounterRegistry.

Definition at line 114 of file CounterDirectory.cpp.

References armnn::profiling::GetNextUid(), OptionalBase::has_value(), CounterDirectory::IsCounterSetRegistered(), and OptionalReferenceSwitch< std::is_reference< T >::value, T >::value().

Referenced by BOOST_AUTO_TEST_CASE().

117 {
118  // Check that the given counter set name is valid
119  if (counterSetName.empty() ||
120  !IsValidSwTraceString<SwTraceNameCharPolicy>(counterSetName))
121  {
122  throw InvalidArgumentException("Trying to register a counter set with an invalid name");
123  }
124 
125  // Check that a counter set with the given name is not already registered
126  if (IsCounterSetRegistered(counterSetName))
127  {
128  throw InvalidArgumentException(
129  boost::str(boost::format("Trying to register a counter set already registered (\"%1%\")")
130  % counterSetName));
131  }
132 
133  // Peek the next UID, do not get an actual valid UID just now as we don't want to waste a good UID in case
134  // the registration fails. We'll get a proper one once we're sure that the counter set can be registered
135  uint16_t counterSetUidPeek = GetNextUid(true);
136 
137  // Check that a category with the given (optional) parent category name is already registered
138  if (parentCategoryName.has_value())
139  {
140  // Get the (optional) parent category name
141  const std::string& parentCategoryNameValue = parentCategoryName.value();
142  if (parentCategoryNameValue.empty())
143  {
144  throw InvalidArgumentException(
145  boost::str(boost::format("Trying to connect a counter set (UID: %1%) to an invalid "
146  "parent category (name: \"%2%\")")
147  % counterSetUidPeek
148  % parentCategoryNameValue));
149  }
150 
151  // Check that the given parent category is already registered
152  auto it = FindCategory(parentCategoryNameValue);
153  if (it == m_Categories.end())
154  {
155  throw InvalidArgumentException(
156  boost::str(boost::format("Trying to connect a counter set (UID: %1%) to a parent category "
157  "that is not registered (name: \"%2%\")")
158  % counterSetUidPeek
159  % parentCategoryNameValue));
160  }
161  }
162 
163  // Get the counter set UID
164  uint16_t counterSetUid = GetNextUid();
165  BOOST_ASSERT(counterSetUid == counterSetUidPeek);
166 
167  // Create the counter set
168  CounterSetPtr counterSet = std::make_unique<CounterSet>(counterSetUid, counterSetName, count);
169  BOOST_ASSERT(counterSet);
170 
171  // Get the raw counter set pointer
172  const CounterSet* counterSetPtr = counterSet.get();
173  BOOST_ASSERT(counterSetPtr);
174 
175  // Register the counter set
176  m_CounterSets.insert(std::make_pair(counterSetUid, std::move(counterSet)));
177 
178  return counterSetPtr;
179 }
std::unique_ptr< CounterSet > CounterSetPtr
uint16_t GetNextUid(bool peekOnly)
bool IsCounterSetRegistered(uint16_t counterSetUid) const

◆ RegisterDevice()

const Device * RegisterDevice ( const std::string &  deviceName,
uint16_t  cores = 0,
const Optional< std::string > &  parentCategoryName = EmptyOptional() 
)
overridevirtual

Implements ICounterRegistry.

Definition at line 52 of file CounterDirectory.cpp.

References armnn::profiling::GetNextUid(), OptionalBase::has_value(), CounterDirectory::IsDeviceRegistered(), and OptionalReferenceSwitch< std::is_reference< T >::value, T >::value().

Referenced by BOOST_AUTO_TEST_CASE().

55 {
56  // Check that the given device name is valid
57  if (deviceName.empty() ||
58  !IsValidSwTraceString<SwTraceCharPolicy>(deviceName))
59  {
60  throw InvalidArgumentException("Trying to register a device with an invalid name");
61  }
62 
63  // Check that a device with the given name is not already registered
64  if (IsDeviceRegistered(deviceName))
65  {
66  throw InvalidArgumentException(
67  boost::str(boost::format("Trying to register a device already registered (\"%1%\")")
68  % deviceName));
69  }
70 
71  // Check that a category with the given (optional) parent category name is already registered
72  if (parentCategoryName.has_value())
73  {
74  // Get the (optional) parent category name
75  const std::string& parentCategoryNameValue = parentCategoryName.value();
76  if (parentCategoryNameValue.empty())
77  {
78  throw InvalidArgumentException(
79  boost::str(boost::format("Trying to connect a device (name: \"%1%\") to an invalid "
80  "parent category (name: \"%2%\")")
81  % deviceName
82  % parentCategoryNameValue));
83  }
84 
85  // Check that the given parent category is already registered
86  auto categoryIt = FindCategory(parentCategoryNameValue);
87  if (categoryIt == m_Categories.end())
88  {
89  throw InvalidArgumentException(
90  boost::str(boost::format("Trying to connect a device (name: \"%1%\") to a parent category that "
91  "is not registered (name: \"%2%\")")
92  % deviceName
93  % parentCategoryNameValue));
94  }
95  }
96 
97  // Get the device UID
98  uint16_t deviceUid = GetNextUid();
99 
100  // Create the device
101  DevicePtr device = std::make_unique<Device>(deviceUid, deviceName, cores);
102  BOOST_ASSERT(device);
103 
104  // Get the raw device pointer
105  const Device* devicePtr = device.get();
106  BOOST_ASSERT(devicePtr);
107 
108  // Register the device
109  m_Devices.insert(std::make_pair(deviceUid, std::move(device)));
110 
111  return devicePtr;
112 }
std::unique_ptr< Device > DevicePtr
bool IsDeviceRegistered(uint16_t deviceUid) const
uint16_t GetNextUid(bool peekOnly)

The documentation for this class was generated from the following files: