ArmNN
 21.08
ProfilerImpl Class Reference

#include <Profiling.hpp>

Classes

struct  Marker
 
struct  ProfilingEventStats
 

Public Types

using InstrumentPtr = std::unique_ptr< Instrument >
 
using EventPtr = std::unique_ptr< Event >
 
using DescPtr = std::unique_ptr< ProfilingDetails >
 

Public Member Functions

 ProfilerImpl ()
 
 ~ProfilerImpl ()
 
EventBeginEvent (armnn::IProfiler *profiler, const BackendId &backendId, const std::string &name, std::vector< InstrumentPtr > &&instruments, const Optional< profiling::ProfilingGuid > &guid)
 
template<typename DescriptorType >
void AddLayerDetails (const std::string &label, const DescriptorType &desc, const WorkloadInfo &infos, const profiling::ProfilingGuid guid)
 
void EndEvent (Event *event)
 
void EnableProfiling (bool enableProfiling)
 
bool IsProfilingEnabled ()
 
void EnableNetworkDetailsToStdOut ()
 
void UpdateEventTag ()
 
void AnalyzeEventsAndWriteResults (std::ostream &outStream) const
 
void Print (std::ostream &outStream) const
 
uint32_t GetEventColor (const BackendId &backendId) const
 
template<typename EventIterType >
void AnalyzeEventSequenceAndWriteResults (EventIterType first, EventIterType last, std::ostream &outStream) const
 
std::map< std::string, ProfilingEventStatsCalculateProfilingEventStats () const
 
void PopulateInferences (std::vector< const Event *> &outInferences, int &outBaseLevel) const
 
void PopulateDescendants (std::map< const Event *, std::vector< const Event *>> &outDescendantsMap) const
 
template<typename ItertType >
void AnalyzeEventSequenceAndWriteResults (ItertType first, ItertType last, std::ostream &outStream) const
 

Public Attributes

std::stack< Event * > m_Parents
 
std::vector< EventPtrm_EventSequence
 
DescPtr m_ProfilingDetails = std::make_unique<ProfilingDetails>()
 
bool m_ProfilingEnabled
 
bool m_EnableDetailsToStdOut
 

Detailed Description

Definition at line 29 of file Profiling.hpp.

Member Typedef Documentation

◆ DescPtr

using DescPtr = std::unique_ptr<ProfilingDetails>

Definition at line 79 of file Profiling.hpp.

◆ EventPtr

using EventPtr = std::unique_ptr<Event>

Definition at line 78 of file Profiling.hpp.

◆ InstrumentPtr

using InstrumentPtr = std::unique_ptr<Instrument>

Definition at line 34 of file Profiling.hpp.

Constructor & Destructor Documentation

◆ ProfilerImpl()

Definition at line 164 of file Profiling.cpp.

References ProfilerImpl::m_EventSequence.

165  : m_ProfilingEnabled(false),
167 {
169 
170 #if ARMNN_STREAMLINE_ENABLED
171  // Initialises streamline annotations.
172  ANNOTATE_SETUP;
173 #endif
174 }
constexpr std::size_t g_ProfilingEventCountHint
Definition: Profiling.cpp:29
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:102

◆ ~ProfilerImpl()

Definition at line 176 of file Profiling.cpp.

References ProfilerManager::GetInstance(), ProfilerImpl::m_ProfilingEnabled, ProfilerImpl::Print(), and ProfilerManager::RegisterProfiler().

177 {
178  if (m_ProfilingEnabled)
179  {
181  {
182  Print(std::cout);
183  }
184  }
185 
186  // Un-register this profiler from the current thread.
188 }
constexpr bool g_WriteReportToStdOutOnProfilerDestruction
Definition: Profiling.cpp:41
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:526
void Print(std::ostream &outStream) const
Definition: Profiling.cpp:356
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:533

Member Function Documentation

◆ AddLayerDetails()

void AddLayerDetails ( const std::string &  label,
const DescriptorType &  desc,
const WorkloadInfo infos,
const profiling::ProfilingGuid  guid 
)
inline

◆ AnalyzeEventsAndWriteResults()

void AnalyzeEventsAndWriteResults ( std::ostream &  outStream) const

Definition at line 414 of file Profiling.cpp.

References ProfilerImpl::AnalyzeEventSequenceAndWriteResults(), armnn::CalcLevel(), ProfilerImpl::m_EventSequence, ProfilerImpl::m_Parents, ProfilerImpl::PopulateDescendants(), and ProfilerImpl::PopulateInferences().

Referenced by ProfilerImpl::AddLayerDetails().

415 {
416  // Stack should be empty now.
417  const bool saneMarkerSequence = m_Parents.empty();
418 
419  // Abort if the sequence of markers was found to have incorrect information:
420  // The stats cannot be trusted.
421  if (!saneMarkerSequence)
422  {
423  outStream << "Cannot write profiling stats. "
424  "Unexpected errors were found when analyzing the sequence of logged events, "
425  "which may lead to plainly wrong stats. The profiling system may contain implementation "
426  "issues or could have been used in an unsafe manner." << std::endl;
427  return;
428  }
429 
430  // Analyzes the full sequence of events.
432  m_EventSequence.cend(),
433  outStream);
434 
435  // Aggregates events by tag if requested (spams the output stream if done for all tags).
437  {
438  outStream << std::endl;
439  outStream << "***" << std::endl;
440  outStream << "*** Per Inference Stats" << std::endl;
441  outStream << "***" << std::endl;
442  outStream << std::endl;
443 
444  int baseLevel = -1;
445  std::vector<const Event*> inferences;
446  PopulateInferences(inferences, baseLevel);
447 
448  // Second map out descendants hierarchy
449  std::map<const Event*, std::vector<const Event*>> descendantsMap;
450  PopulateDescendants(descendantsMap);
451 
452  std::function<void(const Event*, std::vector<const Event*>&)>
453  FindDescendantEvents = [&](const Event* eventPtr, std::vector<const Event*>& sequence)
454  {
455  sequence.push_back(eventPtr);
456 
457  if (CalcLevel(eventPtr) > baseLevel+2) //We only care about levels as deep as workload executions.
458  {
459  return;
460  }
461 
462  auto children = descendantsMap.find(eventPtr);
463  if (children == descendantsMap.end())
464  {
465  return;
466  }
467 
468  if (!(children->second.empty()))
469  {
470  return FindDescendantEvents(children->second[0], sequence);
471  }
472  };
473 
474  // Third, find events belonging to each inference
475  int inferenceIdx = 0;
476  for (auto inference : inferences)
477  {
478  std::vector<const Event*> sequence;
479 
480  //build sequence, depth first
481  FindDescendantEvents(inference, sequence);
482 
483  outStream << "> Begin Inference: " << inferenceIdx << std::endl;
484  outStream << std::endl;
485  AnalyzeEventSequenceAndWriteResults(sequence.cbegin(),
486  sequence.cend(),
487  outStream);
488  outStream << std::endl;
489  outStream << "> End Inference: " << inferenceIdx << std::endl;
490 
491  inferenceIdx++;
492  }
493  }
494 }
int CalcLevel(const Event *eventPtr)
Definition: Profiling.cpp:246
void AnalyzeEventSequenceAndWriteResults(EventIterType first, EventIterType last, std::ostream &outStream) const
constexpr bool g_AggregateProfilingEventsByInference
Definition: Profiling.cpp:37
void PopulateInferences(std::vector< const Event *> &outInferences, int &outBaseLevel) const
Definition: Profiling.cpp:257
std::stack< Event * > m_Parents
Definition: Profiling.hpp:101
void PopulateDescendants(std::map< const Event *, std::vector< const Event *>> &outDescendantsMap) const
Definition: Profiling.cpp:271
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:102

◆ AnalyzeEventSequenceAndWriteResults() [1/2]

void AnalyzeEventSequenceAndWriteResults ( EventIterType  first,
EventIterType  last,
std::ostream &  outStream 
) const

◆ AnalyzeEventSequenceAndWriteResults() [2/2]

void AnalyzeEventSequenceAndWriteResults ( ItertType  first,
ItertType  last,
std::ostream &  outStream 
) const

Definition at line 113 of file Profiling.cpp.

References ProfilerImpl::CalculateProfilingEventStats(), armnn::FindMeasurement(), BackendId::Get(), Event::GetBackendId(), armnn::GetEventPtr(), Event::GetName(), ProfilerImpl::ProfilingEventStats::m_Count, ProfilerImpl::ProfilingEventStats::m_MaxMs, ProfilerImpl::ProfilingEventStats::m_MinMs, ProfilerImpl::ProfilingEventStats::m_TotalMs, Measurement::m_Value, WallClockTimer::WALL_CLOCK_TIME, WallClockTimer::WALL_CLOCK_TIME_START, and WallClockTimer::WALL_CLOCK_TIME_STOP.

114 {
115  // Outputs event sequence, if needed.
117  {
118  // Makes sure timestamps are output with 6 decimals, and save old settings.
119  std::streamsize oldPrecision = outStream.precision();
120  outStream.precision(6);
121  std::ios_base::fmtflags oldFlags = outStream.flags();
122  outStream.setf(std::ios::fixed);
123  // Outputs fields.
124  outStream << "Event Sequence - Name | Duration (ms) | Start (ms) | Stop (ms) | Device" << std::endl;
125  for (auto event = first; event != last; ++event)
126  {
127  const Event* eventPtr = GetEventPtr((*event));
128  double startTimeMs = FindMeasurement(WallClockTimer::WALL_CLOCK_TIME_START, eventPtr).m_Value;
129  double stopTimeMs = FindMeasurement(WallClockTimer::WALL_CLOCK_TIME_STOP, eventPtr).m_Value;
130 
131  // Find the WallClock measurement if there is one.
132  double durationMs = FindMeasurement(WallClockTimer::WALL_CLOCK_TIME, eventPtr).m_Value;
133  outStream << std::setw(50) << eventPtr->GetName() << " "
134  << std::setw(20) << durationMs
135  << std::setw(20) << startTimeMs
136  << std::setw(20) << stopTimeMs
137  << std::setw(20) << eventPtr->GetBackendId().Get()
138  << std::endl;
139  }
140  outStream << std::endl;
141  // Restores previous precision settings.
142  outStream.flags(oldFlags);
143  outStream.precision(oldPrecision);
144  }
145 
146  // Aggregates results per event name.
147  std::map<std::string, ProfilingEventStats> nameToStatsMap = CalculateProfilingEventStats();
148 
149  // Outputs aggregated stats.
150  outStream << "Event Stats - Name | Avg (ms) | Min (ms) | Max (ms) | Total (ms) | Count" << std::endl;
151  for (const auto& pair : nameToStatsMap)
152  {
153  const std::string& eventLabel = pair.first;
154  const ProfilingEventStats& eventStats = pair.second;
155  const double avgMs = eventStats.m_TotalMs / double(eventStats.m_Count);
156 
157  outStream << "\t" << std::setw(50) << eventLabel << " " << std::setw(9) << avgMs << " "
158  << std::setw(9) << eventStats.m_MinMs << " " << std::setw(9) << eventStats.m_MaxMs << " "
159  << std::setw(9) << eventStats.m_TotalMs << " " << std::setw(9) << eventStats.m_Count << std::endl;
160  }
161  outStream << std::endl;
162 }
const Event * GetEventPtr(const Event *ptr)
Definition: Profiling.cpp:109
Measurement FindMeasurement(const std::string &name, const Event *event)
Definition: Profiling.cpp:43
std::map< std::string, ProfilingEventStats > CalculateProfilingEventStats() const
Definition: Profiling.cpp:82
static const std::string WALL_CLOCK_TIME_STOP
static const std::string WALL_CLOCK_TIME_START
static const std::string WALL_CLOCK_TIME
constexpr bool g_WriteProfilingEventSequence
Definition: Profiling.cpp:32

◆ BeginEvent()

Event * BeginEvent ( armnn::IProfiler profiler,
const BackendId backendId,
const std::string &  name,
std::vector< InstrumentPtr > &&  instruments,
const Optional< profiling::ProfilingGuid > &  guid 
)

Definition at line 205 of file Profiling.cpp.

References ProfilerImpl::GetEventColor(), ProfilerImpl::m_EventSequence, and ProfilerImpl::m_Parents.

210 {
211  Event* parent = m_Parents.empty() ? nullptr : m_Parents.top();
212  m_EventSequence.push_back(std::make_unique<Event>(label,
213  profiler,
214  parent,
215  backendId,
216  std::move(instruments),
217  guid));
218  Event* event = m_EventSequence.back().get();
219  event->Start();
220 
221 #if ARMNN_STREAMLINE_ENABLED
222  ANNOTATE_CHANNEL_COLOR(uint32_t(m_Parents.size()), GetEventColor(backendId), label.c_str());
223 #endif
224 
225  m_Parents.push(event);
226  return event;
227 }
uint32_t GetEventColor(const BackendId &backendId) const
Definition: Profiling.cpp:496
std::stack< Event * > m_Parents
Definition: Profiling.hpp:101
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:102

◆ CalculateProfilingEventStats()

std::map< std::string, ProfilerImpl::ProfilingEventStats > CalculateProfilingEventStats ( ) const

Definition at line 82 of file Profiling.cpp.

References armnn::FindMeasurement(), ProfilerImpl::ProfilingEventStats::m_Count, ProfilerImpl::m_EventSequence, ProfilerImpl::ProfilingEventStats::m_MaxMs, ProfilerImpl::ProfilingEventStats::m_MinMs, ProfilerImpl::ProfilingEventStats::m_TotalMs, Measurement::m_Value, and WallClockTimer::WALL_CLOCK_TIME.

Referenced by ProfilerImpl::AnalyzeEventSequenceAndWriteResults().

83 {
84  std::map<std::string, ProfilingEventStats> nameToStatsMap;
85 
86  for (const auto& event : m_EventSequence)
87  {
89 
90  double durationMs = measurement.m_Value;
91  auto it = nameToStatsMap.find(event->GetName());
92  if (it != nameToStatsMap.end())
93  {
94  ProfilingEventStats& stats = it->second;
95  stats.m_TotalMs += durationMs;
96  stats.m_MinMs = std::min(stats.m_MinMs, durationMs);
97  stats.m_MaxMs = std::max(stats.m_MaxMs, durationMs);
98  ++stats.m_Count;
99  }
100  else
101  {
102  nameToStatsMap.emplace(event->GetName(), ProfilingEventStats{ durationMs, durationMs, durationMs, 1 });
103  }
104  }
105 
106  return nameToStatsMap;
107 }
Measurement FindMeasurement(const std::string &name, const Event *event)
Definition: Profiling.cpp:43
static const std::string WALL_CLOCK_TIME
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:102

◆ EnableNetworkDetailsToStdOut()

void EnableNetworkDetailsToStdOut ( )

Definition at line 200 of file Profiling.cpp.

References ProfilerImpl::m_EnableDetailsToStdOut.

Referenced by ProfilerImpl::AddLayerDetails().

201 {
203 }

◆ EnableProfiling()

void EnableProfiling ( bool  enableProfiling)

Definition at line 195 of file Profiling.cpp.

References ProfilerImpl::m_ProfilingEnabled.

Referenced by ProfilerImpl::AddLayerDetails().

196 {
197  m_ProfilingEnabled = enableProfiling;
198 }

◆ EndEvent()

void EndEvent ( Event event)

Definition at line 229 of file Profiling.cpp.

References ARMNN_ASSERT, Event::GetParentEvent(), armnn::IgnoreUnused(), and ProfilerImpl::m_Parents.

Referenced by ProfilerImpl::AddLayerDetails().

230 {
231  event->Stop();
232 
233  ARMNN_ASSERT(!m_Parents.empty());
234  ARMNN_ASSERT(event == m_Parents.top());
235  m_Parents.pop();
236 
237  Event* parent = m_Parents.empty() ? nullptr : m_Parents.top();
238  IgnoreUnused(parent);
239  ARMNN_ASSERT(event->GetParentEvent() == parent);
240 
241 #if ARMNN_STREAMLINE_ENABLED
242  ANNOTATE_CHANNEL_END(uint32_t(m_Parents.size()));
243 #endif
244 }
void IgnoreUnused(Ts &&...)
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::stack< Event * > m_Parents
Definition: Profiling.hpp:101

◆ GetEventColor()

std::uint32_t GetEventColor ( const BackendId backendId) const

Definition at line 496 of file Profiling.cpp.

Referenced by ProfilerImpl::AddLayerDetails(), and ProfilerImpl::BeginEvent().

497 {
498  static BackendId cpuRef("CpuRef");
499  static BackendId cpuAcc("CpuAcc");
500  static BackendId gpuAcc("GpuAcc");
501  if (backendId == cpuRef)
502  {
503  // Cyan
504  return 0xffff001b;
505  }
506  else if (backendId == cpuAcc)
507  {
508  // Green
509  return 0x00ff001b;
510  }
511  else if (backendId == gpuAcc)
512  {
513  // Purple
514  return 0xff007f1b;
515  }
516  else
517  {
518  // Dark gray
519  return 0x5555551b;
520  }
521 }

◆ IsProfilingEnabled()

bool IsProfilingEnabled ( )

Definition at line 190 of file Profiling.cpp.

References ProfilerImpl::m_ProfilingEnabled.

Referenced by ProfilerImpl::AddLayerDetails().

191 {
192  return m_ProfilingEnabled;
193 }

◆ PopulateDescendants()

void PopulateDescendants ( std::map< const Event *, std::vector< const Event *>> &  outDescendantsMap) const

Definition at line 271 of file Profiling.cpp.

References Event::GetParentEvent(), and ProfilerImpl::m_EventSequence.

Referenced by ProfilerImpl::AnalyzeEventsAndWriteResults(), and ProfilerImpl::Print().

272 {
273  for (const auto& event : m_EventSequence)
274  {
275  const Event* eventPtrRaw = event.get();
276  const Event* parent = eventPtrRaw->GetParentEvent();
277 
278  if (!parent)
279  {
280  continue;
281  }
282 
283  auto it = outDescendantsMap.find(parent);
284  if (it == outDescendantsMap.end())
285  {
286  outDescendantsMap.emplace(parent, std::vector<const Event*>({ eventPtrRaw }));
287  }
288  else
289  {
290  it->second.push_back(eventPtrRaw);
291  }
292  }
293 }
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:102

◆ PopulateInferences()

void PopulateInferences ( std::vector< const Event *> &  outInferences,
int &  outBaseLevel 
) const

Definition at line 257 of file Profiling.cpp.

References armnn::CalcLevel(), Event::GetName(), and ProfilerImpl::m_EventSequence.

Referenced by ProfilerImpl::AnalyzeEventsAndWriteResults(), and ProfilerImpl::Print().

258 {
259  outInferences.reserve(m_EventSequence.size());
260  for (const auto& event : m_EventSequence)
261  {
262  const Event* eventPtrRaw = event.get();
263  if (eventPtrRaw->GetName() == "EnqueueWorkload")
264  {
265  outBaseLevel = (outBaseLevel == -1) ? CalcLevel(eventPtrRaw) : outBaseLevel;
266  outInferences.push_back(eventPtrRaw);
267  }
268  }
269 }
int CalcLevel(const Event *eventPtr)
Definition: Profiling.cpp:246
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:102

◆ Print()

void Print ( std::ostream &  outStream) const

Definition at line 356 of file Profiling.cpp.

References armnn::ConfigureDetailsObject(), armnn::ExtractJsonObjects(), ProfilerImpl::m_EnableDetailsToStdOut, ProfilerImpl::m_ProfilingDetails, ProfilerImpl::PopulateDescendants(), ProfilerImpl::PopulateInferences(), JsonUtils::PrintArmNNHeader(), JsonUtils::PrintFooter(), JsonUtils::PrintHeader(), JsonPrinter::PrintJsonChildObject(), and JsonUtils::PrintNewLine().

Referenced by ProfilerImpl::AddLayerDetails(), and ProfilerImpl::~ProfilerImpl().

357 {
358  // Makes sure timestamps are output with 6 decimals, and save old settings.
359  std::streamsize oldPrecision = outStream.precision();
360  outStream.precision(6);
361  std::ios_base::fmtflags oldFlags = outStream.flags();
362  outStream.setf(std::ios::fixed);
363  JsonPrinter printer(outStream);
364 
365  // First find all the "inference" Events and print out duration measurements.
366  int baseLevel = -1;
367  std::vector<const Event*> inferences;
368  PopulateInferences(inferences, baseLevel);
369 
370  // Second map out descendants hierarchy
371  std::map<const Event*, std::vector<const Event*>> descendantsMap;
372  PopulateDescendants(descendantsMap);
373 
374  JsonChildObject inferenceObject{ "inference_measurements" };
375  std::vector<JsonChildObject> workloadObjects;
376  std::map<unsigned int, std::vector<JsonChildObject>> workloadToKernelObjects;
377 
378  for (unsigned int inferenceIndex = 0; inferenceIndex < inferences.size(); ++inferenceIndex)
379  {
380  auto inference = inferences[inferenceIndex];
381  ExtractJsonObjects(inferenceIndex, inference, inferenceObject, descendantsMap);
382  }
383 
384  printer.PrintHeader();
385  printer.PrintArmNNHeader();
386 
387  if (m_ProfilingDetails.get()->DetailsExist() && m_EnableDetailsToStdOut)
388  {
389  JsonChildObject detailsObject{ "layer_details" };
390  ConfigureDetailsObject(detailsObject, m_ProfilingDetails.get()->GetProfilingDetails());
391 
392  size_t id = 0;
393  printer.PrintJsonChildObject(detailsObject, id);
394  }
395 
396  // print inference object, also prints child layer and kernel measurements
397  size_t id = 0;
398  printer.PrintJsonChildObject(inferenceObject, id);
399 
400  // end of ArmNN
401  printer.PrintNewLine();
402  printer.PrintFooter();
403 
404  // end of main JSON object
405  printer.PrintNewLine();
406  printer.PrintFooter();
407  printer.PrintNewLine();
408 
409  // Restores previous precision settings.
410  outStream.flags(oldFlags);
411  outStream.precision(oldPrecision);
412 }
void ConfigureDetailsObject(JsonChildObject &detailsObject, std::string layerDetailsStr)
Definition: Profiling.cpp:295
void ExtractJsonObjects(unsigned int inferenceIndex, const Event *parentEvent, JsonChildObject &parentObject, std::map< const Event *, std::vector< const Event *>> descendantsMap)
Definition: Profiling.cpp:303
DescPtr m_ProfilingDetails
Definition: Profiling.hpp:103
void PopulateInferences(std::vector< const Event *> &outInferences, int &outBaseLevel) const
Definition: Profiling.cpp:257
void PopulateDescendants(std::map< const Event *, std::vector< const Event *>> &outDescendantsMap) const
Definition: Profiling.cpp:271

◆ UpdateEventTag()

void UpdateEventTag ( )

Member Data Documentation

◆ m_EnableDetailsToStdOut

bool m_EnableDetailsToStdOut

◆ m_EventSequence

◆ m_Parents

std::stack<Event*> m_Parents

◆ m_ProfilingDetails

DescPtr m_ProfilingDetails = std::make_unique<ProfilingDetails>()

Definition at line 103 of file Profiling.hpp.

Referenced by ProfilerImpl::AddLayerDetails(), and ProfilerImpl::Print().

◆ m_ProfilingEnabled

bool m_ProfilingEnabled

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