www.mooseframework.org
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Friends | List of all members
PerfGraph Class Reference

The PerfGraph will hold the master list of all registered performance segments and the head PerfNode. More...

#include <PerfGraph.h>

Classes

struct  SectionTime
 Use to hold the time for each section. More...
 

Public Types

enum  TimeType : char {
  SELF, CHILDREN, TOTAL, SELF_AVG,
  CHILDREN_AVG, TOTAL_AVG, SELF_PERCENT, CHILDREN_PERCENT,
  TOTAL_PERCENT
}
 For retrieving values. More...
 

Public Member Functions

 PerfGraph ()
 Create a new PerfGraph. More...
 
 ~PerfGraph ()
 Destructor. More...
 
PerfID registerSection (const std::string &section_name, unsigned int level)
 Registers a named section of code. More...
 
void print (const ConsoleStream &console, unsigned int level)
 Print the tree out. More...
 
void printHeaviestBranch (const ConsoleStream &console)
 Print out the heaviest branch through the tree. More...
 
void printHeaviestSections (const ConsoleStream &console, const unsigned int num_sections)
 Print out the heaviest sections that were timed. More...
 
const std::string & sectionName (const PerfID id) const
 Grab the name of a section. More...
 
bool active () const
 Whether or not timing is active. More...
 
void setActive (bool active)
 Turn on or off timing. More...
 
unsigned long int getNumCalls (const std::string &section_name)
 Get the number of calls for a section. More...
 
Real getTime (const TimeType type, const std::string &section_name)
 Get a reference to the time for a section. More...
 
const Real & getSelfTime (const std::string &section_name)
 Get a reference to the self time for a section. More...
 
const Real & getChildrenTime (const std::string &section_name)
 Get a reference to the children time for a section. More...
 
const Real & getTotalTime (const std::string &section_name)
 Get a reference to the total time for a section. More...
 
void updateTiming ()
 Udates the time section_time and time for all currently running nodes. More...
 

Protected Types

typedef VariadicTable< std::string, unsigned long int, Real, Real, Real, Real, Real, Real, Real, Real, Real > FullTable
 
typedef VariadicTable< std::string, unsigned long int, Real, Real, Real > HeaviestTable
 

Protected Member Functions

void push (const PerfID id)
 Add a Node onto the end of the end of the current callstack. More...
 
void pop ()
 Remove a Node from the end of the current scope. More...
 
void recursivelyPrintGraph (PerfNode *current_node, FullTable &vtable, unsigned int level, unsigned int current_depth=0)
 Helper for printing out the graph. More...
 
void recursivelyPrintHeaviestGraph (PerfNode *current_node, FullTable &vtable, unsigned int current_depth=0)
 Helper for printing out the trace that has taken the most time. More...
 
void recursivelyFillTime (PerfNode *current_node)
 Updates the cumulative self/children/total time. More...
 
void printHeaviestSections (const ConsoleStream &console)
 Helper for printing out the heaviest sections. More...
 

Protected Attributes

std::unique_ptr< PerfNode_root_node
 The root node of the graph. More...
 
unsigned int _current_position
 The current node position in the stack. More...
 
std::array< PerfNode *, MAX_STACK_SIZE > _stack
 The full callstack. Currently capped at a depth of 100. More...
 
std::map< std::string, PerfID_section_name_to_id
 Map of section names to IDs. More...
 
std::map< PerfID, std::string > _id_to_section_name
 Map of IDs to section names. More...
 
std::map< PerfID, unsigned int > _id_to_level
 Map of IDs to level. More...
 
std::map< std::string, SectionTime_section_time
 The time for each section. More...
 
std::vector< SectionTime * > _section_time_ptrs
 Pointers into _section_time indexed on PerfID This is here for convenience and speed so we don't need to iterate over the above map much - and it makes it easier to sort. More...
 
bool _active
 Whether or not timing is active. More...
 

Friends

class PerfGuard
 

Detailed Description

The PerfGraph will hold the master list of all registered performance segments and the head PerfNode.

Definition at line 35 of file PerfGraph.h.

Member Typedef Documentation

◆ FullTable

typedef VariadicTable<std::string, unsigned long int, Real, Real, Real, Real, Real, Real, Real, Real, Real> PerfGraph::FullTable
protected

Definition at line 170 of file PerfGraph.h.

◆ HeaviestTable

typedef VariadicTable<std::string, unsigned long int, Real, Real, Real> PerfGraph::HeaviestTable
protected

Definition at line 172 of file PerfGraph.h.

Member Enumeration Documentation

◆ TimeType

enum PerfGraph::TimeType : char

For retrieving values.

Enumerator
SELF 
CHILDREN 
TOTAL 
SELF_AVG 
CHILDREN_AVG 
TOTAL_AVG 
SELF_PERCENT 
CHILDREN_PERCENT 
TOTAL_PERCENT 

Definition at line 41 of file PerfGraph.h.

Constructor & Destructor Documentation

◆ PerfGraph()

PerfGraph::PerfGraph ( )

Create a new PerfGraph.

Definition at line 24 of file PerfGraph.C.

24  : _current_position(0), _active(true)
25 {
26  // Not done in the initialization list on purpose because this object needs to be complete first
27  _root_node = libmesh_make_unique<PerfNode>(registerSection("App", 0));
28 
29  // Set the initial time
30  _root_node->setStartTime(std::chrono::steady_clock::now());
31 
32  // Add a call
33  _root_node->incrementNumCalls();
34 
35  _stack[0] = _root_node.get();
36 }
unsigned int _current_position
The current node position in the stack.
Definition: PerfGraph.h:245
std::array< PerfNode *, MAX_STACK_SIZE > _stack
The full callstack. Currently capped at a depth of 100.
Definition: PerfGraph.h:248
bool _active
Whether or not timing is active.
Definition: PerfGraph.h:277
std::unique_ptr< PerfNode > _root_node
The root node of the graph.
Definition: PerfGraph.h:242
PerfID registerSection(const std::string &section_name, unsigned int level)
Registers a named section of code.
Definition: PerfGraph.C:41

◆ ~PerfGraph()

PerfGraph::~PerfGraph ( )

Destructor.

Definition at line 38 of file PerfGraph.C.

38 {}

Member Function Documentation

◆ active()

bool PerfGraph::active ( ) const
inline

Whether or not timing is active.

When not active no timing information will be kept

Definition at line 103 of file PerfGraph.h.

Referenced by setActive().

103 { return _active; }
bool _active
Whether or not timing is active.
Definition: PerfGraph.h:277

◆ getChildrenTime()

const Real& PerfGraph::getChildrenTime ( const std::string &  section_name)
inline

Get a reference to the children time for a section.

This reference can be held onto and the value will be updated anytime updateTiming() is called.

Definition at line 137 of file PerfGraph.h.

138  {
139  return _section_time[section_name]._children;
140  }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:268

◆ getNumCalls()

unsigned long int PerfGraph::getNumCalls ( const std::string &  section_name)

Get the number of calls for a section.

Definition at line 70 of file PerfGraph.C.

Referenced by PerfGraphData::getValue().

71 {
72  updateTiming();
73 
74  auto section_it = _section_time.find(section_name);
75 
76  if (section_it == _section_time.end())
77  mooseError("Unknown section_name: ", section_name, " in PerfGraph::getNumCalls() ");
78 
79  return section_it->second._num_calls;
80 }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:268
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
void updateTiming()
Udates the time section_time and time for all currently running nodes.
Definition: PerfGraph.C:153

◆ getSelfTime()

const Real& PerfGraph::getSelfTime ( const std::string &  section_name)
inline

Get a reference to the self time for a section.

This reference can be held onto and the value will be updated anytime updateTiming() is called.

Definition at line 126 of file PerfGraph.h.

127  {
128  return _section_time[section_name]._self;
129  }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:268

◆ getTime()

Real PerfGraph::getTime ( const TimeType  type,
const std::string &  section_name 
)

Get a reference to the time for a section.

Definition at line 83 of file PerfGraph.C.

Referenced by PerfGraphData::getValue().

84 {
85  updateTiming();
86 
87  auto section_it = _section_time.find(section_name);
88 
89  if (section_it == _section_time.end())
90  mooseError("Unknown section_name: ", section_name, " in PerfGraph::getTime() ");
91 
92  auto app_time = _section_time_ptrs[0]->_total;
93 
94  switch (type)
95  {
96  case SELF:
97  return section_it->second._self;
98  case CHILDREN:
99  return section_it->second._children;
100  case TOTAL:
101  return section_it->second._total;
102  case SELF_AVG:
103  return section_it->second._self / static_cast<Real>(section_it->second._num_calls);
104  case CHILDREN_AVG:
105  return section_it->second._children / static_cast<Real>(section_it->second._num_calls);
106  case TOTAL_AVG:
107  return section_it->second._total / static_cast<Real>(section_it->second._num_calls);
108  case SELF_PERCENT:
109  return 100. * (section_it->second._self / app_time);
110  case CHILDREN_PERCENT:
111  return 100. * (section_it->second._children / app_time);
112  case TOTAL_PERCENT:
113  return 100. * (section_it->second._total / app_time);
114  default:
115  ::mooseError("Unknown TimeType");
116  }
117 }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:268
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
void updateTiming()
Udates the time section_time and time for all currently running nodes.
Definition: PerfGraph.C:153
MatType type
std::vector< SectionTime * > _section_time_ptrs
Pointers into _section_time indexed on PerfID This is here for convenience and speed so we don&#39;t need...
Definition: PerfGraph.h:274

◆ getTotalTime()

const Real& PerfGraph::getTotalTime ( const std::string &  section_name)
inline

Get a reference to the total time for a section.

This reference can be held onto and the value will be updated anytime updateTiming() is called.

Definition at line 148 of file PerfGraph.h.

149  {
150  return _section_time[section_name]._total;
151  }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:268

◆ pop()

void PerfGraph::pop ( )
protected

Remove a Node from the end of the current scope.

Note: only accessible by using PerfGuard!

Definition at line 142 of file PerfGraph.C.

Referenced by PerfGuard::~PerfGuard().

143 {
144  if (!_active)
145  return;
146 
147  _stack[_current_position]->addTime(std::chrono::steady_clock::now());
148 
150 }
unsigned int _current_position
The current node position in the stack.
Definition: PerfGraph.h:245
std::array< PerfNode *, MAX_STACK_SIZE > _stack
The full callstack. Currently capped at a depth of 100.
Definition: PerfGraph.h:248
bool _active
Whether or not timing is active.
Definition: PerfGraph.h:277

◆ print()

void PerfGraph::print ( const ConsoleStream console,
unsigned int  level 
)

Print the tree out.

Parameters
consoleThe output stream to output to
levelThe log level, the higher the number the more output you get

Definition at line 326 of file PerfGraph.C.

Referenced by PerfGraphOutput::output().

327 {
328  updateTiming();
329 
330  console << "\nPerformance Graph:\n";
331  FullTable vtable({"Section",
332  "Calls",
333  "Self(s)",
334  "Avg(s)",
335  "%",
336  "Children(s)",
337  "Avg(s)",
338  "%",
339  "Total(s)",
340  "Avg(s)",
341  "%"},
342  10);
343 
344  vtable.setColumnFormat({VariadicTableColumnFormat::AUTO, // Section Name
355 
356  vtable.setColumnPrecision({1, 0, 3, 3, 2, 3, 3, 2, 3, 3, 2});
357 
358  recursivelyPrintGraph(_root_node.get(), vtable, level);
359  vtable.print(console);
360 }
void recursivelyPrintGraph(PerfNode *current_node, FullTable &vtable, unsigned int level, unsigned int current_depth=0)
Helper for printing out the graph.
Definition: PerfGraph.C:213
VariadicTable< std::string, unsigned long int, Real, Real, Real, Real, Real, Real, Real, Real, Real > FullTable
Definition: PerfGraph.h:170
void updateTiming()
Udates the time section_time and time for all currently running nodes.
Definition: PerfGraph.C:153
std::unique_ptr< PerfNode > _root_node
The root node of the graph.
Definition: PerfGraph.h:242

◆ printHeaviestBranch()

void PerfGraph::printHeaviestBranch ( const ConsoleStream console)

Print out the heaviest branch through the tree.

Parameters
consoleThe output stream to output to

Definition at line 363 of file PerfGraph.C.

Referenced by PerfGraphOutput::output().

364 {
365  updateTiming();
366 
367  console << "\nHeaviest Branch:\n";
368  FullTable vtable({"Section",
369  "Calls",
370  "Self(s)",
371  "Avg(s)",
372  "%",
373  "Children(s)",
374  "Avg(s)",
375  "%",
376  "Total(s)",
377  "Avg(s)",
378  "%"},
379  10);
380 
381  vtable.setColumnFormat({VariadicTableColumnFormat::AUTO, // Section Name
392 
393  vtable.setColumnPrecision({1, 0, 3, 3, 2, 3, 3, 2, 3, 3, 2});
394 
396  vtable.print(console);
397 }
VariadicTable< std::string, unsigned long int, Real, Real, Real, Real, Real, Real, Real, Real, Real > FullTable
Definition: PerfGraph.h:170
void updateTiming()
Udates the time section_time and time for all currently running nodes.
Definition: PerfGraph.C:153
std::unique_ptr< PerfNode > _root_node
The root node of the graph.
Definition: PerfGraph.h:242
void recursivelyPrintHeaviestGraph(PerfNode *current_node, FullTable &vtable, unsigned int current_depth=0)
Helper for printing out the trace that has taken the most time.
Definition: PerfGraph.C:268

◆ printHeaviestSections() [1/2]

void PerfGraph::printHeaviestSections ( const ConsoleStream console,
const unsigned int  num_sections 
)

Print out the heaviest sections that were timed.

Parameters
consoleThe output stream to output to

Definition at line 400 of file PerfGraph.C.

Referenced by PerfGraphOutput::output().

401 {
402  updateTiming();
403 
404  console << "\nHeaviest Sections:\n";
405 
406  // Indirect Sort The Self Time
407  std::vector<size_t> sorted;
409  _section_time_ptrs.end(),
410  sorted,
411  [](SectionTime * lhs, SectionTime * rhs) {
412 
413  if (lhs && rhs)
414  return lhs->_self > rhs->_self;
415 
416  // If the LHS exists - it's definitely bigger than a non-existant RHS
417  if (lhs)
418  return true;
419 
420  // Both don't exist - so it doesn't matter how we sort them
421  return false;
422  });
423 
424  HeaviestTable vtable({"Section", "Calls", "Self(s)", "Avg.", "%"}, 10);
425 
426  vtable.setColumnFormat({VariadicTableColumnFormat::AUTO, // Doesn't matter
431 
432  vtable.setColumnPrecision({1, 1, 3, 3, 2});
433 
434  mooseAssert(!_section_time_ptrs.empty(),
435  "updateTiming() must be run before printHeaviestSections()!");
436 
437  // The total time of the root node
438  auto total_root_time = _section_time_ptrs[0]->_total;
439 
440  // Now print out the largest ones
441  for (unsigned int i = 0; i < num_sections; i++)
442  {
443  auto id = sorted[i];
444 
445  vtable.addRow(_id_to_section_name[id],
446  _section_time_ptrs[id]->_num_calls,
447  _section_time_ptrs[id]->_self,
448  _section_time_ptrs[id]->_self /
449  static_cast<Real>(_section_time_ptrs[id]->_num_calls),
450  100 * _section_time_ptrs[id]->_self / total_root_time);
451  }
452 
453  vtable.print(console);
454 }
void indirectSort(RandomAccessIterator beg, RandomAccessIterator end, std::vector< size_t > &b)
Definition: IndirectSort.h:69
void updateTiming()
Udates the time section_time and time for all currently running nodes.
Definition: PerfGraph.C:153
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:254
VariadicTable< std::string, unsigned long int, Real, Real, Real > HeaviestTable
Definition: PerfGraph.h:172
std::vector< SectionTime * > _section_time_ptrs
Pointers into _section_time indexed on PerfID This is here for convenience and speed so we don&#39;t need...
Definition: PerfGraph.h:274

◆ printHeaviestSections() [2/2]

void PerfGraph::printHeaviestSections ( const ConsoleStream console)
protected

Helper for printing out the heaviest sections.

Parameters
consoleWhere to print to

◆ push()

void PerfGraph::push ( const PerfID  id)
protected

Add a Node onto the end of the end of the current callstack.

Note: only accessible by using PerfGuard!

Definition at line 120 of file PerfGraph.C.

Referenced by PerfGuard::PerfGuard().

121 {
122  if (!_active)
123  return;
124 
125  auto new_node = _stack[_current_position]->getChild(id);
126 
127  // Set the start time
128  new_node->setStartTime(std::chrono::steady_clock::now());
129 
130  // Increment the number of calls
131  new_node->incrementNumCalls();
132 
134 
135  if (_current_position >= MAX_STACK_SIZE)
136  mooseError("PerfGraph is out of stack space!");
137 
138  _stack[_current_position] = new_node;
139 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
unsigned int _current_position
The current node position in the stack.
Definition: PerfGraph.h:245
std::array< PerfNode *, MAX_STACK_SIZE > _stack
The full callstack. Currently capped at a depth of 100.
Definition: PerfGraph.h:248
bool _active
Whether or not timing is active.
Definition: PerfGraph.h:277

◆ recursivelyFillTime()

void PerfGraph::recursivelyFillTime ( PerfNode current_node)
protected

Updates the cumulative self/children/total time.

Note: requires that self/children/total time are resized and zeroed before calling.

Parameters
current_nodeThe current node to work on

Definition at line 191 of file PerfGraph.C.

Referenced by updateTiming().

192 {
193  auto id = current_node->id();
194 
195  auto self = std::chrono::duration<double>(current_node->selfTime()).count();
196  auto children = std::chrono::duration<double>(current_node->childrenTime()).count();
197  auto total = std::chrono::duration<double>(current_node->totalTime()).count();
198  auto num_calls = current_node->numCalls();
199 
200  // RHS insertion on purpose
201  auto & section_time = _section_time[_id_to_section_name[id]];
202 
203  section_time._self += self;
204  section_time._children += children;
205  section_time._total += total;
206  section_time._num_calls += num_calls;
207 
208  for (auto & child_it : current_node->children())
209  recursivelyFillTime(child_it.second.get());
210 }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:268
std::chrono::steady_clock::duration childrenTime() const
Get the time this nodes children took.
Definition: PerfNode.C:27
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:254
void recursivelyFillTime(PerfNode *current_node)
Updates the cumulative self/children/total time.
Definition: PerfGraph.C:191
const std::map< PerfID, std::unique_ptr< PerfNode > > & children() const
Get the children.
Definition: PerfNode.h:87
PerfID id() const
Get the ID of this Node.
Definition: PerfNode.h:35
std::chrono::steady_clock::duration totalTime() const
The time this Node plus all of it&#39;s children took.
Definition: PerfNode.C:19
std::chrono::steady_clock::duration selfTime() const
Get the time this node took.
Definition: PerfNode.C:13
unsigned long int numCalls()
Get the number of times this node was called.
Definition: PerfNode.h:107

◆ recursivelyPrintGraph()

void PerfGraph::recursivelyPrintGraph ( PerfNode current_node,
FullTable vtable,
unsigned int  level,
unsigned int  current_depth = 0 
)
protected

Helper for printing out the graph.

Parameters
current_nodeThe node to be working on right now
consoleWhere to print to
levelThe level to print out below (<=)
current_depth- Used in the recursion

Definition at line 213 of file PerfGraph.C.

Referenced by print().

217 {
218  mooseAssert(_id_to_section_name.find(current_node->id()) != _id_to_section_name.end(),
219  "Unable to find section name!");
220  auto & name = _id_to_section_name[current_node->id()];
221 
222  mooseAssert(_id_to_level.find(current_node->id()) != _id_to_level.end(), "Unable to find level!");
223  auto & node_level = _id_to_level[current_node->id()];
224 
225  if (node_level <= level)
226  {
227  mooseAssert(!_section_time_ptrs.empty(),
228  "updateTiming() must be run before recursivelyPrintGraph!");
229 
230  auto section = std::string(current_depth * 2, ' ') + name;
231 
232  // The total time of the root node
233  auto total_root_time = _section_time_ptrs[0]->_total;
234 
235  auto num_calls = current_node->numCalls();
236  auto self = std::chrono::duration<double>(current_node->selfTime()).count();
237  auto self_avg = self / static_cast<Real>(num_calls);
238  auto self_percent = 100. * self / total_root_time;
239 
240  auto children = std::chrono::duration<double>(current_node->childrenTime()).count();
241  auto children_avg = children / static_cast<Real>(num_calls);
242  auto children_percent = 100. * children / total_root_time;
243 
244  auto total = std::chrono::duration<double>(current_node->totalTime()).count();
245  auto total_avg = total / static_cast<Real>(num_calls);
246  auto total_percent = 100. * total / total_root_time;
247 
248  vtable.addRow(section,
249  num_calls,
250  self,
251  self_avg,
252  self_percent,
253  children,
254  children_avg,
255  children_percent,
256  total,
257  total_avg,
258  total_percent);
259 
260  current_depth++;
261  }
262 
263  for (auto & child_it : current_node->children())
264  recursivelyPrintGraph(child_it.second.get(), vtable, level, current_depth);
265 }
void recursivelyPrintGraph(PerfNode *current_node, FullTable &vtable, unsigned int level, unsigned int current_depth=0)
Helper for printing out the graph.
Definition: PerfGraph.C:213
std::chrono::steady_clock::duration childrenTime() const
Get the time this nodes children took.
Definition: PerfNode.C:27
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:254
const std::map< PerfID, std::unique_ptr< PerfNode > > & children() const
Get the children.
Definition: PerfNode.h:87
PerfID id() const
Get the ID of this Node.
Definition: PerfNode.h:35
std::chrono::steady_clock::duration totalTime() const
The time this Node plus all of it&#39;s children took.
Definition: PerfNode.C:19
std::map< PerfID, unsigned int > _id_to_level
Map of IDs to level.
Definition: PerfGraph.h:257
std::chrono::steady_clock::duration selfTime() const
Get the time this node took.
Definition: PerfNode.C:13
unsigned long int numCalls()
Get the number of times this node was called.
Definition: PerfNode.h:107
std::vector< SectionTime * > _section_time_ptrs
Pointers into _section_time indexed on PerfID This is here for convenience and speed so we don&#39;t need...
Definition: PerfGraph.h:274

◆ recursivelyPrintHeaviestGraph()

void PerfGraph::recursivelyPrintHeaviestGraph ( PerfNode current_node,
FullTable vtable,
unsigned int  current_depth = 0 
)
protected

Helper for printing out the trace that has taken the most time.

Parameters
current_nodeThe node to be working on right now
consoleWhere to print to
current_depth- Used in the recursion

Definition at line 268 of file PerfGraph.C.

Referenced by printHeaviestBranch().

271 {
272  mooseAssert(!_section_time_ptrs.empty(),
273  "updateTiming() must be run before recursivelyPrintGraph!");
274 
275  auto & name = _id_to_section_name[current_node->id()];
276 
277  auto section = std::string(current_depth * 2, ' ') + name;
278 
279  // The total time of the root node
280  auto total_root_time = _section_time_ptrs[0]->_total;
281 
282  auto num_calls = current_node->numCalls();
283  auto self = std::chrono::duration<double>(current_node->selfTime()).count();
284  auto self_avg = self / static_cast<Real>(num_calls);
285  auto self_percent = 100. * self / total_root_time;
286 
287  auto children = std::chrono::duration<double>(current_node->childrenTime()).count();
288  auto children_avg = children / static_cast<Real>(num_calls);
289  auto children_percent = 100. * children / total_root_time;
290 
291  auto total = std::chrono::duration<double>(current_node->totalTime()).count();
292  auto total_avg = total / static_cast<Real>(num_calls);
293  auto total_percent = 100. * total / total_root_time;
294 
295  vtable.addRow(section,
296  num_calls,
297  self,
298  self_avg,
299  self_percent,
300  children,
301  children_avg,
302  children_percent,
303  total,
304  total_avg,
305  total_percent);
306 
307  current_depth++;
308 
309  if (!current_node->children().empty())
310  {
311  PerfNode * heaviest_child = nullptr;
312 
313  for (auto & child_it : current_node->children())
314  {
315  auto current_child = child_it.second.get();
316 
317  if (!heaviest_child || (current_child->totalTime() > heaviest_child->totalTime()))
318  heaviest_child = current_child;
319  }
320 
321  recursivelyPrintHeaviestGraph(heaviest_child, vtable, current_depth);
322  }
323 }
std::chrono::steady_clock::duration childrenTime() const
Get the time this nodes children took.
Definition: PerfNode.C:27
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:254
const std::map< PerfID, std::unique_ptr< PerfNode > > & children() const
Get the children.
Definition: PerfNode.h:87
PerfID id() const
Get the ID of this Node.
Definition: PerfNode.h:35
std::chrono::steady_clock::duration totalTime() const
The time this Node plus all of it&#39;s children took.
Definition: PerfNode.C:19
std::chrono::steady_clock::duration selfTime() const
Get the time this node took.
Definition: PerfNode.C:13
void recursivelyPrintHeaviestGraph(PerfNode *current_node, FullTable &vtable, unsigned int current_depth=0)
Helper for printing out the trace that has taken the most time.
Definition: PerfGraph.C:268
unsigned long int numCalls()
Get the number of times this node was called.
Definition: PerfNode.h:107
std::vector< SectionTime * > _section_time_ptrs
Pointers into _section_time indexed on PerfID This is here for convenience and speed so we don&#39;t need...
Definition: PerfGraph.h:274

◆ registerSection()

unsigned int PerfGraph::registerSection ( const std::string &  section_name,
unsigned int  level 
)

Registers a named section of code.

Returns
The unique ID to use for that section

Definition at line 41 of file PerfGraph.C.

Referenced by PerfGraph(), and PerfGraphInterface::registerTimedSection().

42 {
43  auto it = _section_name_to_id.lower_bound(section_name);
44 
45  // Is it already registered?
46  if (it != _section_name_to_id.end() && it->first == section_name)
47  return it->second;
48 
49  // It's not...
50  auto id = _section_name_to_id.size();
51  _section_name_to_id.emplace_hint(it, section_name, id);
52  _id_to_section_name[id] = section_name;
53  _id_to_level[id] = level;
54 
55  return id;
56 }
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:254
std::map< PerfID, unsigned int > _id_to_level
Map of IDs to level.
Definition: PerfGraph.h:257
std::map< std::string, PerfID > _section_name_to_id
Map of section names to IDs.
Definition: PerfGraph.h:251

◆ sectionName()

const std::string & PerfGraph::sectionName ( const PerfID  id) const

Grab the name of a section.

Definition at line 59 of file PerfGraph.C.

60 {
61  auto find_it = _id_to_section_name.find(id);
62 
63  if (find_it == _id_to_section_name.end())
64  mooseError("PerfGraph cannot find a section name associated with id: ", id);
65 
66  return find_it->second;
67 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:254

◆ setActive()

void PerfGraph::setActive ( bool  active)
inline

Turn on or off timing.

Definition at line 108 of file PerfGraph.h.

Referenced by MooseApp::setupOptions().

108 { _active = active; }
bool active() const
Whether or not timing is active.
Definition: PerfGraph.h:103
bool _active
Whether or not timing is active.
Definition: PerfGraph.h:277

◆ updateTiming()

void PerfGraph::updateTiming ( )

Udates the time section_time and time for all currently running nodes.

Definition at line 153 of file PerfGraph.C.

Referenced by getNumCalls(), getTime(), print(), printHeaviestBranch(), and printHeaviestSections().

154 {
155  // First update all of the currently running nodes
156  auto now = std::chrono::steady_clock::now();
157  for (unsigned int i = 0; i <= _current_position; i++)
158  {
159  auto node = _stack[i];
160  node->addTime(now);
161  node->setStartTime(now);
162  }
163 
164  // Zero out the entries
165  for (auto & section_time_it : _section_time)
166  {
167  auto & section_time = section_time_it.second;
168 
169  section_time._num_calls = 0;
170  section_time._self = 0.;
171  section_time._children = 0.;
172  section_time._total = 0.;
173  }
174 
176 
177  // Update vector pointing to section times
178  // Note: we are doing this _after_ recursively filling
179  // because new entries may have been created
181 
182  for (auto & section_time_it : _section_time)
183  {
184  auto id = _section_name_to_id[section_time_it.first];
185 
186  _section_time_ptrs[id] = &section_time_it.second;
187  }
188 }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:268
unsigned int _current_position
The current node position in the stack.
Definition: PerfGraph.h:245
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:254
void recursivelyFillTime(PerfNode *current_node)
Updates the cumulative self/children/total time.
Definition: PerfGraph.C:191
std::array< PerfNode *, MAX_STACK_SIZE > _stack
The full callstack. Currently capped at a depth of 100.
Definition: PerfGraph.h:248
std::unique_ptr< PerfNode > _root_node
The root node of the graph.
Definition: PerfGraph.h:242
std::map< std::string, PerfID > _section_name_to_id
Map of section names to IDs.
Definition: PerfGraph.h:251
std::vector< SectionTime * > _section_time_ptrs
Pointers into _section_time indexed on PerfID This is here for convenience and speed so we don&#39;t need...
Definition: PerfGraph.h:274

Friends And Related Function Documentation

◆ PerfGuard

friend class PerfGuard
friend

Definition at line 280 of file PerfGraph.h.

Member Data Documentation

◆ _active

bool PerfGraph::_active
protected

Whether or not timing is active.

Definition at line 277 of file PerfGraph.h.

Referenced by active(), pop(), push(), and setActive().

◆ _current_position

unsigned int PerfGraph::_current_position
protected

The current node position in the stack.

Definition at line 245 of file PerfGraph.h.

Referenced by pop(), push(), and updateTiming().

◆ _id_to_level

std::map<PerfID, unsigned int> PerfGraph::_id_to_level
protected

Map of IDs to level.

Definition at line 257 of file PerfGraph.h.

Referenced by recursivelyPrintGraph(), and registerSection().

◆ _id_to_section_name

std::map<PerfID, std::string> PerfGraph::_id_to_section_name
protected

◆ _root_node

std::unique_ptr<PerfNode> PerfGraph::_root_node
protected

The root node of the graph.

Definition at line 242 of file PerfGraph.h.

Referenced by PerfGraph(), print(), printHeaviestBranch(), and updateTiming().

◆ _section_name_to_id

std::map<std::string, PerfID> PerfGraph::_section_name_to_id
protected

Map of section names to IDs.

Definition at line 251 of file PerfGraph.h.

Referenced by registerSection(), and updateTiming().

◆ _section_time

std::map<std::string, SectionTime> PerfGraph::_section_time
protected

The time for each section.

This is updated on updateTiming() Note that this is total cumulative time across every place that section is in the graph

I'm making this a map so that we can give out references to the values The three values are: self, children The map is on std::string because we might need to be able to retrieve timing values in a "late binding" situation before the section has been registered.

Definition at line 268 of file PerfGraph.h.

Referenced by getChildrenTime(), getNumCalls(), getSelfTime(), getTime(), getTotalTime(), recursivelyFillTime(), and updateTiming().

◆ _section_time_ptrs

std::vector<SectionTime *> PerfGraph::_section_time_ptrs
protected

Pointers into _section_time indexed on PerfID This is here for convenience and speed so we don't need to iterate over the above map much - and it makes it easier to sort.

Definition at line 274 of file PerfGraph.h.

Referenced by getTime(), printHeaviestSections(), recursivelyPrintGraph(), recursivelyPrintHeaviestGraph(), and updateTiming().

◆ _stack

std::array<PerfNode *, MAX_STACK_SIZE> PerfGraph::_stack
protected

The full callstack. Currently capped at a depth of 100.

Definition at line 248 of file PerfGraph.h.

Referenced by PerfGraph(), pop(), push(), and updateTiming().


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