ArmNN
 21.02
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 >
 

Public Member Functions

 ProfilerImpl ()
 
 ~ProfilerImpl ()
 
EventBeginEvent (armnn::IProfiler *profiler, const BackendId &backendId, const std::string &name, std::vector< InstrumentPtr > &&instruments)
 
void EndEvent (Event *event)
 
void EnableProfiling (bool enableProfiling)
 
bool IsProfilingEnabled ()
 
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
 
bool m_ProfilingEnabled
 

Detailed Description

Definition at line 27 of file Profiling.hpp.

Member Typedef Documentation

◆ EventPtr

using EventPtr = std::unique_ptr<Event>

Definition at line 63 of file Profiling.hpp.

◆ InstrumentPtr

using InstrumentPtr = std::unique_ptr<Instrument>

Definition at line 32 of file Profiling.hpp.

Constructor & Destructor Documentation

◆ ProfilerImpl()

Definition at line 165 of file Profiling.cpp.

References ProfilerImpl::m_EventSequence.

166  : 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:30
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:85

◆ ~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:42
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:489
void Print(std::ostream &outStream) const
Definition: Profiling.cpp:331
void RegisterProfiler(IProfiler *profiler)
Definition: Profiling.cpp:496

Member Function Documentation

◆ AnalyzeEventsAndWriteResults()

void AnalyzeEventsAndWriteResults ( std::ostream &  outStream) const

Definition at line 381 of file Profiling.cpp.

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

382 {
383  // Stack should be empty now.
384  const bool saneMarkerSequence = m_Parents.empty();
385 
386  // Abort if the sequence of markers was found to have incorrect information:
387  // The stats cannot be trusted.
388  if (!saneMarkerSequence)
389  {
390  outStream << "Cannot write profiling stats. "
391  "Unexpected errors were found when analyzing the sequence of logged events, which may lead to plainly "
392  "wrong stats. The profiling system may contain implementation issues or could have been used in an "
393  "unsafe manner." << std::endl;
394  return;
395  }
396 
397  // Analyzes the full sequence of events.
399  m_EventSequence.cend(),
400  outStream);
401 
402  // Aggregates events by tag if requested (spams the output stream if done for all tags).
404  {
405  outStream << std::endl;
406  outStream << "***" << std::endl;
407  outStream << "*** Per Inference Stats" << std::endl;
408  outStream << "***" << std::endl;
409  outStream << std::endl;
410 
411  int baseLevel = -1;
412  std::vector<const Event*> inferences;
413  PopulateInferences(inferences, baseLevel);
414 
415  // Second map out descendants hierarchy
416  std::map<const Event*, std::vector<const Event*>> descendantsMap;
417  PopulateDescendants(descendantsMap);
418 
419  std::function<void (const Event*, std::vector<const Event*>&)>
420  FindDescendantEvents = [&](const Event* eventPtr,
421  std::vector<const Event*>& sequence)
422  {
423  sequence.push_back(eventPtr);
424 
425  if (CalcLevel(eventPtr) > baseLevel+2) //We only care about levels as deep as workload executions.
426  {
427  return;
428  }
429 
430  auto children = descendantsMap.find(eventPtr);
431  if (children == descendantsMap.end())
432  {
433  return;
434  }
435 
436  if (!(children->second.empty()))
437  {
438  return FindDescendantEvents(children->second[0], sequence);
439  }
440  };
441 
442  // Third, find events belonging to each inference
443  int inferenceIdx = 0;
444  for (auto inference : inferences)
445  {
446  std::vector<const Event*> sequence;
447 
448  //build sequence, depth first
449  FindDescendantEvents(inference, sequence);
450 
451  outStream << "> Begin Inference: " << inferenceIdx << std::endl;
452  outStream << std::endl;
453  AnalyzeEventSequenceAndWriteResults(sequence.cbegin(),
454  sequence.cend(),
455  outStream);
456  outStream << std::endl;
457  outStream << "> End Inference: " << inferenceIdx << std::endl;
458 
459  inferenceIdx++;
460  }
461  }
462 }
int CalcLevel(const Event *eventPtr)
Definition: Profiling.cpp:235
void AnalyzeEventSequenceAndWriteResults(EventIterType first, EventIterType last, std::ostream &outStream) const
constexpr bool g_AggregateProfilingEventsByInference
Definition: Profiling.cpp:38
void PopulateInferences(std::vector< const Event *> &outInferences, int &outBaseLevel) const
Definition: Profiling.cpp:246
std::stack< Event * > m_Parents
Definition: Profiling.hpp:84
void PopulateDescendants(std::map< const Event *, std::vector< const Event *>> &outDescendantsMap) const
Definition: Profiling.cpp:260
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:85

◆ 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 114 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.

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

◆ BeginEvent()

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

Definition at line 200 of file Profiling.cpp.

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

204 {
205  Event* parent = m_Parents.empty() ? nullptr : m_Parents.top();
206  m_EventSequence.push_back(std::make_unique<Event>(label, profiler, parent, backendId, std::move(instruments)));
207  Event* event = m_EventSequence.back().get();
208  event->Start();
209 
210 #if ARMNN_STREAMLINE_ENABLED
211  ANNOTATE_CHANNEL_COLOR(uint32_t(m_Parents.size()), GetEventColor(backendId), label.c_str());
212 #endif
213 
214  m_Parents.push(event);
215  return event;
216 }
uint32_t GetEventColor(const BackendId &backendId) const
Definition: Profiling.cpp:464
std::stack< Event * > m_Parents
Definition: Profiling.hpp:84
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:85

◆ CalculateProfilingEventStats()

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

Definition at line 83 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().

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

◆ EnableProfiling()

void EnableProfiling ( bool  enableProfiling)

Definition at line 195 of file Profiling.cpp.

References ProfilerImpl::m_ProfilingEnabled.

196 {
197  m_ProfilingEnabled = enableProfiling;
198 }

◆ EndEvent()

void EndEvent ( Event event)

Definition at line 218 of file Profiling.cpp.

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

219 {
220  event->Stop();
221 
222  ARMNN_ASSERT(!m_Parents.empty());
223  ARMNN_ASSERT(event == m_Parents.top());
224  m_Parents.pop();
225 
226  Event* parent = m_Parents.empty() ? nullptr : m_Parents.top();
227  IgnoreUnused(parent);
228  ARMNN_ASSERT(event->GetParentEvent() == parent);
229 
230 #if ARMNN_STREAMLINE_ENABLED
231  ANNOTATE_CHANNEL_END(uint32_t(m_Parents.size()));
232 #endif
233 }
void IgnoreUnused(Ts &&...)
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
std::stack< Event * > m_Parents
Definition: Profiling.hpp:84

◆ GetEventColor()

std::uint32_t GetEventColor ( const BackendId backendId) const

Definition at line 464 of file Profiling.cpp.

Referenced by ProfilerImpl::BeginEvent().

465 {
466  static BackendId cpuRef("CpuRef");
467  static BackendId cpuAcc("CpuAcc");
468  static BackendId gpuAcc("GpuAcc");
469  if (backendId == cpuRef) {
470  // Cyan
471  return 0xffff001b;
472  } else if (backendId == cpuAcc) {
473  // Green
474  return 0x00ff001b;
475  } else if (backendId == gpuAcc) {
476  // Purple
477  return 0xff007f1b;
478  } else {
479  // Dark gray
480  return 0x5555551b;
481  }
482 }

◆ IsProfilingEnabled()

bool IsProfilingEnabled ( )

Definition at line 190 of file Profiling.cpp.

References ProfilerImpl::m_ProfilingEnabled.

191 {
192  return m_ProfilingEnabled;
193 }

◆ PopulateDescendants()

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

Definition at line 260 of file Profiling.cpp.

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

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

261 {
262  for (const auto& event : m_EventSequence)
263  {
264  const Event* eventPtrRaw = event.get();
265  const Event* parent = eventPtrRaw->GetParentEvent();
266 
267  if (!parent)
268  {
269  continue;
270  }
271 
272  auto it = outDescendantsMap.find(parent);
273  if (it == outDescendantsMap.end())
274  {
275  outDescendantsMap.emplace(parent, std::vector<const Event*>({eventPtrRaw}));
276  }
277  else
278  {
279  it->second.push_back(eventPtrRaw);
280  }
281  }
282 }
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:85

◆ PopulateInferences()

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

Definition at line 246 of file Profiling.cpp.

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

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

247 {
248  outInferences.reserve(m_EventSequence.size());
249  for (const auto& event : m_EventSequence)
250  {
251  const Event* eventPtrRaw = event.get();
252  if (eventPtrRaw->GetName() == "EnqueueWorkload")
253  {
254  outBaseLevel = (outBaseLevel == -1) ? CalcLevel(eventPtrRaw) : outBaseLevel;
255  outInferences.push_back(eventPtrRaw);
256  }
257  }
258 }
int CalcLevel(const Event *eventPtr)
Definition: Profiling.cpp:235
std::vector< EventPtr > m_EventSequence
Definition: Profiling.hpp:85

◆ Print()

void Print ( std::ostream &  outStream) const

Definition at line 331 of file Profiling.cpp.

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

Referenced by ProfilerImpl::~ProfilerImpl().

332 {
333  // Makes sure timestamps are output with 6 decimals, and save old settings.
334  std::streamsize oldPrecision = outStream.precision();
335  outStream.precision(6);
336  std::ios_base::fmtflags oldFlags = outStream.flags();
337  outStream.setf(std::ios::fixed);
338  JsonPrinter printer(outStream);
339 
340  // First find all the "inference" Events and print out duration measurements.
341  int baseLevel = -1;
342  std::vector<const Event*> inferences;
343  PopulateInferences(inferences, baseLevel);
344 
345  // Second map out descendants hierarchy
346  std::map<const Event*, std::vector<const Event*>> descendantsMap;
347  PopulateDescendants(descendantsMap);
348 
349  JsonChildObject inferenceObject{"inference_measurements"};
350  JsonChildObject layerObject{"layer_measurements"};
351  std::vector<JsonChildObject> workloadObjects;
352  std::map<unsigned int, std::vector<JsonChildObject>> workloadToKernelObjects;
353 
354  for (unsigned int inferenceIndex = 0; inferenceIndex < inferences.size(); ++inferenceIndex)
355  {
356  auto inference = inferences[inferenceIndex];
357  ExtractJsonObjects(inferenceIndex, inference, inferenceObject, descendantsMap);
358  }
359 
360  printer.PrintHeader();
361  printer.PrintArmNNHeader();
362 
363  // print inference object, also prints child layer and kernel measurements
364  size_t id=0;
365  printer.PrintJsonChildObject(inferenceObject, id);
366 
367  // end of ArmNN
368  printer.PrintNewLine();
369  printer.PrintFooter();
370 
371  // end of main JSON object
372  printer.PrintNewLine();
373  printer.PrintFooter();
374  printer.PrintNewLine();
375 
376  // Restores previous precision settings.
377  outStream.flags(oldFlags);
378  outStream.precision(oldPrecision);
379 }
void ExtractJsonObjects(unsigned int inferenceIndex, const Event *parentEvent, JsonChildObject &parentObject, std::map< const Event *, std::vector< const Event *>> descendantsMap)
Definition: Profiling.cpp:285
void PopulateInferences(std::vector< const Event *> &outInferences, int &outBaseLevel) const
Definition: Profiling.cpp:246
void PopulateDescendants(std::map< const Event *, std::vector< const Event *>> &outDescendantsMap) const
Definition: Profiling.cpp:260

◆ UpdateEventTag()

void UpdateEventTag ( )

Member Data Documentation

◆ m_EventSequence

◆ m_Parents

std::stack<Event*> m_Parents

◆ m_ProfilingEnabled

bool m_ProfilingEnabled

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