ArmNN
 20.02
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 166 of file Profiling.cpp.

167  : m_ProfilingEnabled(false)
168 {
169  m_EventSequence.reserve(g_ProfilingEventCountHint);
170 
171 #if ARMNN_STREAMLINE_ENABLED
172  // Initialises streamline annotations.
173  ANNOTATE_SETUP;
174 #endif
175 }
constexpr std::size_t g_ProfilingEventCountHint
Definition: Profiling.cpp:31

◆ ~Profiler()

~Profiler ( )

Definition at line 177 of file Profiling.cpp.

References ProfilerManager::GetInstance(), Profiler::Print(), and ProfilerManager::RegisterProfiler().

178 {
179  if (m_ProfilingEnabled)
180  {
182  {
183  Print(std::cout);
184  }
185  }
186 
187  // Un-register this profiler from the current thread.
189 }
constexpr bool g_WriteReportToStdOutOnProfilerDestruction
Definition: Profiling.cpp:43
void RegisterProfiler(Profiler *profiler)
Definition: Profiling.cpp:494
static ProfilerManager & GetInstance()
Definition: Profiling.cpp:487
void Print(std::ostream &outStream) const override
Print stats for events in JSON Format to the given output stream.
Definition: Profiling.cpp:331

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 381 of file Profiling.cpp.

References armnn::CalcLevel().

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.
398  AnalyzeEventSequenceAndWriteResults(m_EventSequence.cbegin(),
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  for (const Event* child : children->second)
437  {
438  return FindDescendantEvents(child, 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
constexpr bool g_AggregateProfilingEventsByInference
Definition: Profiling.cpp:39

◆ AnalyzeEventSequenceAndWriteResults()

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

Definition at line 115 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.

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

◆ BeginEvent()

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

Definition at line 201 of file Profiling.cpp.

References Profiler::GetEventColor().

204 {
205  Event* parent = m_Parents.empty() ? nullptr : m_Parents.top();
206  m_EventSequence.push_back(std::make_unique<Event>(label, this, 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

◆ 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 196 of file Profiling.cpp.

197 {
198  m_ProfilingEnabled = enableProfiling;
199 }

◆ EndEvent()

void EndEvent ( Event event)

Definition at line 218 of file Profiling.cpp.

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

219 {
220  event->Stop();
221 
222  BOOST_ASSERT(!m_Parents.empty());
223  BOOST_ASSERT(event == m_Parents.top());
224  m_Parents.pop();
225 
226  Event* parent = m_Parents.empty() ? nullptr : m_Parents.top();
227  IgnoreUnused(parent);
228  BOOST_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 &&...)

◆ GetEventColor()

std::uint32_t GetEventColor ( const BackendId backendId) const

Definition at line 464 of file Profiling.cpp.

Referenced by Profiler::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 ( )
overridevirtual

Checks whether profiling is enabled.

Profiling is disabled by default.

Returns
true if profiling is enabled, false otherwise.

Implements IProfiler.

Definition at line 191 of file Profiling.cpp.

192 {
193  return m_ProfilingEnabled;
194 }

◆ 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 331 of file Profiling.cpp.

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

Referenced by Profiler::~Profiler().

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

◆ UpdateEventTag()

void UpdateEventTag ( )

Friends And Related Function Documentation

◆ GetProfilerEventSequenceSize

size_t GetProfilerEventSequenceSize ( armnn::Profiler profiler)
friend

Definition at line 23 of file ProfilerTests.cpp.

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

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