ArmNN
 20.11
Profiler Class Referencefinal

#include <Profiling.hpp>

Inheritance diagram for Profiler:
IProfiler

Public Types

using InstrumentPtr = std::unique_ptr< Instrument >
 

Public Member Functions

 Profiler ()
 
 ~Profiler ()
 
EventBeginEvent (const BackendId &backendId, const std::string &name, std::vector< InstrumentPtr > &&instruments)
 
void EndEvent (Event *event)
 
void EnableProfiling (bool enableProfiling) override
 Enables/disables profiling for this profiler. More...
 
bool IsProfilingEnabled () override
 Checks whether profiling is enabled. More...
 
void UpdateEventTag ()
 
void AnalyzeEventsAndWriteResults (std::ostream &outStream) const override
 Analyzes the tracked events and writes the results to the given output stream. More...
 
void Print (std::ostream &outStream) const override
 Print stats for events in JSON Format to the given output stream. More...
 
uint32_t GetEventColor (const BackendId &backendId) const
 
template<typename ItertType >
void AnalyzeEventSequenceAndWriteResults (ItertType first, ItertType last, std::ostream &outStream) const
 

Friends

size_t GetProfilerEventSequenceSize (armnn::Profiler *profiler)
 

Additional Inherited Members

- Protected Member Functions inherited from IProfiler
 ~IProfiler ()
 

Detailed Description

Definition at line 27 of file Profiling.hpp.

Member Typedef Documentation

◆ InstrumentPtr

using InstrumentPtr = std::unique_ptr<Instrument>

Definition at line 32 of file Profiling.hpp.

Constructor & Destructor Documentation

◆ Profiler()

Profiler ( )

Definition at line 165 of file Profiling.cpp.

166  : m_ProfilingEnabled(false)
167 {
168  m_EventSequence.reserve(g_ProfilingEventCountHint);
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

◆ ~Profiler()

~Profiler ( )

Definition at line 176 of file Profiling.cpp.

References ProfilerManager::GetInstance(), Profiler::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
void RegisterProfiler(Profiler *profiler)
Definition: Profiling.cpp:493
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:486
void Print(std::ostream &outStream) const override
Print stats for events in JSON Format to the given output stream.
Definition: Profiling.cpp:330

Member Function Documentation

◆ AnalyzeEventsAndWriteResults()

void AnalyzeEventsAndWriteResults ( std::ostream &  outStream) const
overridevirtual

Analyzes the tracked events and writes the results to the given output stream.

Please refer to the configuration variables in Profiling.cpp to customize the information written.

Parameters
[out]outStreamThe stream where to write the profiling results to.

Implements IProfiler.

Definition at line 380 of file Profiling.cpp.

References armnn::CalcLevel().

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

◆ AnalyzeEventSequenceAndWriteResults()

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

Definition at line 114 of file Profiling.cpp.

References armnn::FindMeasurement(), BackendId::Get(), Event::GetBackendId(), armnn::GetEventPtr(), Event::GetName(), 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
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 ( const BackendId backendId,
const std::string &  name,
std::vector< InstrumentPtr > &&  instruments 
)

Definition at line 200 of file Profiling.cpp.

References Profiler::GetEventColor().

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

◆ EnableProfiling()

void EnableProfiling ( bool  enableProfiling)
overridevirtual

Enables/disables profiling for this profiler.

Parameters
[in]enableProfilingA flag that indicates whether profiling should be enabled or not.

Implements IProfiler.

Definition at line 195 of file Profiling.cpp.

196 {
197  m_ProfilingEnabled = enableProfiling;
198 }

◆ EndEvent()

void EndEvent ( Event event)

Definition at line 217 of file Profiling.cpp.

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

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

◆ GetEventColor()

std::uint32_t GetEventColor ( const BackendId backendId) const

Definition at line 463 of file Profiling.cpp.

Referenced by Profiler::BeginEvent().

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

◆ IsProfilingEnabled()

bool IsProfilingEnabled ( )
overridevirtual

Checks whether profiling is enabled.

Profiling is disabled by default.

Returns
true if profiling is enabled, false otherwise.

Implements IProfiler.

Definition at line 190 of file Profiling.cpp.

191 {
192  return m_ProfilingEnabled;
193 }

◆ Print()

void Print ( std::ostream &  outStream) const
overridevirtual

Print stats for events in JSON Format to the given output stream.

Parameters
[out]outStreamThe stream where to write the profiling results to.

Implements IProfiler.

Definition at line 330 of file Profiling.cpp.

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

Referenced by BOOST_AUTO_TEST_CASE(), and Profiler::~Profiler().

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

◆ UpdateEventTag()

void UpdateEventTag ( )

Friends And Related Function Documentation

◆ GetProfilerEventSequenceSize

size_t GetProfilerEventSequenceSize ( armnn::Profiler profiler)
friend

Definition at line 22 of file ProfilerTests.cpp.

23 {
24  if (!profiler)
25  {
26  return static_cast<size_t>(-1);
27  }
28 
29  return profiler->m_EventSequence.size();
30 }

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