ArmNN
 25.11
Loading...
Searching...
No Matches
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< arm::pipe::ProfilingGuid > &guid)
template<typename DescriptorType>
void AddLayerDetails (const std::string &label, const DescriptorType &desc, const WorkloadInfo &infos, const arm::pipe::ProfilingGuid guid)
void EndEvent (Event *event)
void EnableProfiling (bool enableProfiling)
bool IsProfilingEnabled ()
void EnableNetworkDetailsToStdOut (ProfilingDetailsMethod detailsMethod)
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 PopulateParent (std::vector< const Event * > &outEvents, int &outBaseLevel, std::string parentName) 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
ProfilingDetailsMethod m_DetailsToStdOutMethod

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.

165 : m_ProfilingEnabled(false),
166 m_DetailsToStdOutMethod(ProfilingDetailsMethod::Undefined)
167{
168 m_EventSequence.reserve(g_ProfilingEventCountHint);
169
170#if ARMNN_STREAMLINE_ENABLED
171 // Initialises streamline annotations.
172 ANNOTATE_SETUP;
173#endif
174}

References armnn::g_ProfilingEventCountHint, m_DetailsToStdOutMethod, m_EventSequence, m_ProfilingEnabled, and armnn::Undefined.

◆ ~ProfilerImpl()

Definition at line 176 of file Profiling.cpp.

177{
178 if (m_ProfilingEnabled)
179 {
180 if (g_WriteReportToStdOutOnProfilerDestruction)
181 {
182 Print(std::cout);
183 }
184 }
185
186 // Un-register this profiler from the current thread.
187 ProfilerManager::GetInstance().RegisterProfiler(nullptr);
188}

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

Member Function Documentation

◆ AddLayerDetails()

template<typename DescriptorType>
void AddLayerDetails ( const std::string & label,
const DescriptorType & desc,
const WorkloadInfo & infos,
const arm::pipe::ProfilingGuid guid )
inline

Definition at line 45 of file Profiling.hpp.

49 {
50 m_ProfilingDetails->AddDetailsToString(label, desc, infos, guid);
51 }

References m_ProfilingDetails.

◆ AnalyzeEventsAndWriteResults()

void AnalyzeEventsAndWriteResults ( std::ostream & outStream) const

Definition at line 490 of file Profiling.cpp.

491{
492 // Stack should be empty now.
493 const bool saneMarkerSequence = m_Parents.empty();
494
495 // Abort if the sequence of markers was found to have incorrect information:
496 // The stats cannot be trusted.
497 if (!saneMarkerSequence)
498 {
499 outStream << "Cannot write profiling stats. "
500 "Unexpected errors were found when analyzing the sequence of logged events, "
501 "which may lead to plainly wrong stats. The profiling system may contain implementation "
502 "issues or could have been used in an unsafe manner." << std::endl;
503 return;
504 }
505
506 // Analyzes the full sequence of events.
507 AnalyzeEventSequenceAndWriteResults(m_EventSequence.cbegin(),
508 m_EventSequence.cend(),
509 outStream);
510
511 // Aggregates events by tag if requested (spams the output stream if done for all tags).
512 if (g_AggregateProfilingEventsByInference)
513 {
514 outStream << std::endl;
515 outStream << "***" << std::endl;
516 outStream << "*** Per Inference Stats" << std::endl;
517 outStream << "***" << std::endl;
518 outStream << std::endl;
519
520 int baseLevel = -1;
521 std::vector<const Event*> inferences;
522 PopulateParent(inferences, baseLevel, "EnqueueWorkload");
523
524 // Second map out descendants hierarchy
525 std::map<const Event*, std::vector<const Event*>> descendantsMap;
526 PopulateDescendants(descendantsMap);
527
528 std::function<void(const Event*, std::vector<const Event*>&)>
529 FindDescendantEvents = [&](const Event* eventPtr, std::vector<const Event*>& sequence)
530 {
531 sequence.push_back(eventPtr);
532
533 if (CalcLevel(eventPtr) > baseLevel+2) //We only care about levels as deep as workload executions.
534 {
535 return;
536 }
537
538 auto children = descendantsMap.find(eventPtr);
539 if (children == descendantsMap.end())
540 {
541 return;
542 }
543
544 if (!(children->second.empty()))
545 {
546 return FindDescendantEvents(children->second[0], sequence);
547 }
548 };
549
550 // Third, find events belonging to each inference
551 int inferenceIdx = 0;
552 for (auto inference : inferences)
553 {
554 std::vector<const Event*> sequence;
555
556 //build sequence, depth first
557 FindDescendantEvents(inference, sequence);
558
559 outStream << "> Begin Inference: " << inferenceIdx << std::endl;
560 outStream << std::endl;
561 AnalyzeEventSequenceAndWriteResults(sequence.cbegin(),
562 sequence.cend(),
563 outStream);
564 outStream << std::endl;
565 outStream << "> End Inference: " << inferenceIdx << std::endl;
566
567 inferenceIdx++;
568 }
569 }
570}
int CalcLevel(const Event *eventPtr)

References AnalyzeEventSequenceAndWriteResults(), armnn::CalcLevel(), armnn::g_AggregateProfilingEventsByInference, m_EventSequence, m_Parents, PopulateDescendants(), and PopulateParent().

◆ AnalyzeEventSequenceAndWriteResults() [1/2]

template<typename EventIterType>
void AnalyzeEventSequenceAndWriteResults ( EventIterType first,
EventIterType last,
std::ostream & outStream ) const

◆ AnalyzeEventSequenceAndWriteResults() [2/2]

template<typename ItertType>
void AnalyzeEventSequenceAndWriteResults ( ItertType first,
ItertType last,
std::ostream & outStream ) const

Definition at line 113 of file Profiling.cpp.

114{
115 // Outputs event sequence, if needed.
116 if (g_WriteProfilingEventSequence)
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)
Measurement FindMeasurement(const std::string &name, const Event *event)
Definition Profiling.cpp:43

References CalculateProfilingEventStats(), armnn::FindMeasurement(), armnn::g_WriteProfilingEventSequence, 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.

◆ BeginEvent()

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

Definition at line 205 of file Profiling.cpp.

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}

References GetEventColor(), m_EventSequence, and m_Parents.

◆ CalculateProfilingEventStats()

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

Definition at line 82 of file Profiling.cpp.

83{
84 std::map<std::string, ProfilingEventStats> nameToStatsMap;
85
86 for (const auto& event : m_EventSequence)
87 {
88 Measurement measurement = FindMeasurement(WallClockTimer::WALL_CLOCK_TIME, event.get());
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}

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

Referenced by AnalyzeEventSequenceAndWriteResults().

◆ EnableNetworkDetailsToStdOut()

void EnableNetworkDetailsToStdOut ( ProfilingDetailsMethod detailsMethod)

Definition at line 200 of file Profiling.cpp.

201{
202 m_DetailsToStdOutMethod = details;
203}

References m_DetailsToStdOutMethod.

◆ EnableProfiling()

void EnableProfiling ( bool enableProfiling)

Definition at line 195 of file Profiling.cpp.

196{
197 m_ProfilingEnabled = enableProfiling;
198}

References m_ProfilingEnabled.

◆ EndEvent()

void EndEvent ( Event * event)

Definition at line 229 of file Profiling.cpp.

230{
231 event->Stop();
232
233 if (!!m_Parents.empty())
234 {
235 throw armnn::Exception("m_Parents must not be empty.");
236 }
237
238 if (event != m_Parents.top())
239 {
240 throw armnn::Exception("event must match the top of m_Parents.");
241 }
242
243 m_Parents.pop();
244
245 Event* parent = m_Parents.empty() ? nullptr : m_Parents.top();
246
247 if (event->GetParentEvent() != parent)
248 {
249 throw armnn::Exception("parent events must match.");
250 }
251
252#if ARMNN_STREAMLINE_ENABLED
253 ANNOTATE_CHANNEL_END(uint32_t(m_Parents.size()));
254#endif
255}

References Event::GetParentEvent(), and m_Parents.

◆ GetEventColor()

std::uint32_t GetEventColor ( const BackendId & backendId) const

Definition at line 572 of file Profiling.cpp.

573{
574 static BackendId cpuRef("CpuRef");
575 static BackendId cpuAcc("CpuAcc");
576 static BackendId gpuAcc("GpuAcc");
577 if (backendId == cpuRef)
578 {
579 // Cyan
580 return 0xffff001b;
581 }
582 else if (backendId == cpuAcc)
583 {
584 // Green
585 return 0x00ff001b;
586 }
587 else if (backendId == gpuAcc)
588 {
589 // Purple
590 return 0xff007f1b;
591 }
592 else
593 {
594 // Dark gray
595 return 0x5555551b;
596 }
597}

Referenced by BeginEvent().

◆ IsProfilingEnabled()

bool IsProfilingEnabled ( )

Definition at line 190 of file Profiling.cpp.

191{
192 return m_ProfilingEnabled;
193}

References m_ProfilingEnabled.

◆ PopulateDescendants()

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

Definition at line 282 of file Profiling.cpp.

283{
284 for (const auto& event : m_EventSequence)
285 {
286 const Event* eventPtrRaw = event.get();
287 const Event* parent = eventPtrRaw->GetParentEvent();
288
289 if (!parent)
290 {
291 continue;
292 }
293
294 auto it = outDescendantsMap.find(parent);
295 if (it == outDescendantsMap.end())
296 {
297 outDescendantsMap.emplace(parent, std::vector<const Event*>({ eventPtrRaw }));
298 }
299 else
300 {
301 it->second.push_back(eventPtrRaw);
302 }
303 }
304}

References Event::GetParentEvent(), and m_EventSequence.

Referenced by AnalyzeEventsAndWriteResults(), and Print().

◆ PopulateParent()

void PopulateParent ( std::vector< const Event * > & outEvents,
int & outBaseLevel,
std::string parentName ) const

Definition at line 268 of file Profiling.cpp.

269{
270 outEvents.reserve(m_EventSequence.size());
271 for (const auto& event : m_EventSequence)
272 {
273 const Event* eventPtrRaw = event.get();
274 if (eventPtrRaw->GetName() == parentName)
275 {
276 outBaseLevel = (outBaseLevel == -1) ? CalcLevel(eventPtrRaw) : outBaseLevel;
277 outEvents.push_back(eventPtrRaw);
278 }
279 }
280}

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

Referenced by AnalyzeEventsAndWriteResults(), and Print().

◆ Print()

void Print ( std::ostream & outStream) const

Definition at line 395 of file Profiling.cpp.

396{
397 // Makes sure timestamps are output with 6 decimals, and save old settings.
398 std::streamsize oldPrecision = outStream.precision();
399 outStream.precision(6);
400 std::ios_base::fmtflags oldFlags = outStream.flags();
401 outStream.setf(std::ios::fixed);
402 JsonPrinter printer(outStream);
403
404 // First find all the parent Events and print out duration measurements.
405 int baseLevel = -1;
406
407 std::vector<const Event*> optimizations;
408 PopulateParent(optimizations, baseLevel, "Optimizer");
409
410 std::vector<const Event*> loadedNetworks;
411 PopulateParent(loadedNetworks, baseLevel, "LoadedNetwork");
412
413 std::vector<const Event*> inferences;
414 PopulateParent(inferences, baseLevel, "EnqueueWorkload");
415
416 // Second map out descendants hierarchy
417 std::map<const Event*, std::vector<const Event*>> descendantsMap;
418 PopulateDescendants(descendantsMap);
419
420 // Extract json objects for each parent event type
421 JsonChildObject optimizeObject{ "optimize_measurements" };
422
423 for (unsigned int optimizeIndex = 0; optimizeIndex < optimizations.size(); ++optimizeIndex)
424 {
425 auto optimization = optimizations[optimizeIndex];
426 ExtractJsonObjects(optimizeIndex, optimization, optimizeObject, descendantsMap);
427 }
428
429 JsonChildObject loadedNetworkObject{ "loaded_network_measurements" };
430
431 for (unsigned int loadedNetworkIndex = 0; loadedNetworkIndex < loadedNetworks.size(); ++loadedNetworkIndex)
432 {
433 auto loadedNetwork = loadedNetworks[loadedNetworkIndex];
434 ExtractJsonObjects(loadedNetworkIndex, loadedNetwork, loadedNetworkObject, descendantsMap);
435 }
436
437 JsonChildObject inferenceObject{ "inference_measurements" };
438
439 for (unsigned int inferenceIndex = 0; inferenceIndex < inferences.size(); ++inferenceIndex)
440 {
441 auto inference = inferences[inferenceIndex];
442 ExtractJsonObjects(inferenceIndex, inference, inferenceObject, descendantsMap);
443 }
444
445 printer.PrintHeader();
446 printer.PrintArmNNHeader();
447
448 if (m_ProfilingDetails.get()->DetailsExist() &&
449 (m_DetailsToStdOutMethod == ProfilingDetailsMethod::DetailsOnly
450 || m_DetailsToStdOutMethod == ProfilingDetailsMethod::DetailsWithEvents))
451 {
452 JsonChildObject detailsObject{ "layer_details" };
453 if (m_DetailsToStdOutMethod == ProfilingDetailsMethod::DetailsOnly)
454 {
455 detailsObject.EnableDetailsOnly();
456 }
457 detailsObject.SetType(JsonObjectType::ExecObjectDesc);
458 detailsObject.SetAndParseDetails(m_ProfilingDetails.get()->GetProfilingDetails());
459
460 size_t id = 0;
461 printer.PrintJsonChildObject(detailsObject, id);
462 }
463
464 // print inference object, also prints child layer and kernel measurements
465 size_t id = 0;
466 if (m_DetailsToStdOutMethod != ProfilingDetailsMethod::DetailsOnly)
467 {
468 printer.PrintJsonChildObject(optimizeObject, id);
469 printer.PrintSeparator();
470 printer.PrintNewLine();
471 printer.PrintJsonChildObject(loadedNetworkObject, id);
472 printer.PrintSeparator();
473 printer.PrintNewLine();
474 printer.PrintJsonChildObject(inferenceObject, id);
475 printer.PrintNewLine();
476 }
477 // end of ArmNN
478 printer.PrintFooter();
479
480 // end of main JSON object
481 printer.PrintNewLine();
482 printer.PrintFooter();
483 printer.PrintNewLine();
484
485 // Restores previous precision settings.
486 outStream.flags(oldFlags);
487 outStream.precision(oldPrecision);
488
489}
void ExtractJsonObjects(unsigned int inferenceIndex, const Event *parentEvent, JsonChildObject &parentObject, std::map< const Event *, std::vector< const Event * > > descendantsMap)

References armnn::DetailsOnly, armnn::DetailsWithEvents, JsonChildObject::EnableDetailsOnly(), armnn::ExecObjectDesc, armnn::ExtractJsonObjects(), m_DetailsToStdOutMethod, m_ProfilingDetails, PopulateDescendants(), PopulateParent(), JsonUtils::PrintArmNNHeader(), JsonUtils::PrintFooter(), JsonUtils::PrintHeader(), JsonPrinter::PrintJsonChildObject(), JsonUtils::PrintNewLine(), JsonUtils::PrintSeparator(), JsonChildObject::SetAndParseDetails(), and JsonChildObject::SetType().

Referenced by ~ProfilerImpl().

◆ UpdateEventTag()

void UpdateEventTag ( )

Member Data Documentation

◆ m_DetailsToStdOutMethod

ProfilingDetailsMethod m_DetailsToStdOutMethod

Definition at line 105 of file Profiling.hpp.

Referenced by EnableNetworkDetailsToStdOut(), Print(), and ProfilerImpl().

◆ m_EventSequence

◆ m_Parents

std::stack<Event*> m_Parents

Definition at line 101 of file Profiling.hpp.

Referenced by AnalyzeEventsAndWriteResults(), BeginEvent(), and EndEvent().

◆ m_ProfilingDetails

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

Definition at line 103 of file Profiling.hpp.

Referenced by AddLayerDetails(), and Print().

◆ m_ProfilingEnabled

bool m_ProfilingEnabled

Definition at line 104 of file Profiling.hpp.

Referenced by EnableProfiling(), IsProfilingEnabled(), ProfilerImpl(), and ~ProfilerImpl().


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