www.mooseframework.org
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static 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 (const std::string &root_name)
 Create a new PerfGraph. More...
 
 ~PerfGraph ()=default
 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

const std::string _root_name
 The name to display for the root of the graph when printing the entire graph. More...
 
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...
 

Static Protected Attributes

static const std::string ROOT_NAME = "Root"
 The name (handle) of the root node. 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 ( const std::string &  root_name)

Create a new PerfGraph.

Definition at line 26 of file PerfGraph.C.

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

◆ ~PerfGraph()

PerfGraph::~PerfGraph ( )
default

Destructor.

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:283

◆ 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:274

◆ getNumCalls()

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

Get the number of calls for a section.

Definition at line 71 of file PerfGraph.C.

Referenced by PerfGraphData::getValue().

72 {
73  updateTiming();
74 
75  auto section_it = _section_time.find(section_name);
76 
77  if (section_it == _section_time.end())
78  mooseError(
79  "Unknown section_name: ",
80  section_name,
81  " in PerfGraph::getNumCalls()\nIf you are attempting to retrieve the root use \"Root\".");
82 
83  return section_it->second._num_calls;
84 }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:274
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:160

◆ 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:274

◆ getTime()

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

Get a reference to the time for a section.

Definition at line 87 of file PerfGraph.C.

Referenced by PerfGraphData::getValue().

88 {
89  updateTiming();
90 
91  auto section_it = _section_time.find(section_name);
92 
93  if (section_it == _section_time.end())
94  mooseError(
95  "Unknown section_name: ",
96  section_name,
97  " in PerfGraph::getTime()\nIf you are attempting to retrieve the root use \"Root\".");
98 
99  auto app_time = _section_time_ptrs[0]->_total;
100 
101  switch (type)
102  {
103  case SELF:
104  return section_it->second._self;
105  case CHILDREN:
106  return section_it->second._children;
107  case TOTAL:
108  return section_it->second._total;
109  case SELF_AVG:
110  return section_it->second._self / static_cast<Real>(section_it->second._num_calls);
111  case CHILDREN_AVG:
112  return section_it->second._children / static_cast<Real>(section_it->second._num_calls);
113  case TOTAL_AVG:
114  return section_it->second._total / static_cast<Real>(section_it->second._num_calls);
115  case SELF_PERCENT:
116  return 100. * (section_it->second._self / app_time);
117  case CHILDREN_PERCENT:
118  return 100. * (section_it->second._children / app_time);
119  case TOTAL_PERCENT:
120  return 100. * (section_it->second._total / app_time);
121  default:
122  ::mooseError("Unknown TimeType");
123  }
124 }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:274
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:160
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:280

◆ 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:274

◆ pop()

void PerfGraph::pop ( )
protected

Remove a Node from the end of the current scope.

Note: only accessible by using PerfGuard!

Definition at line 149 of file PerfGraph.C.

Referenced by PerfGuard::~PerfGuard().

150 {
151  if (!_active)
152  return;
153 
154  _stack[_current_position]->addTime(std::chrono::steady_clock::now());
155 
157 }
unsigned int _current_position
The current node position in the stack.
Definition: PerfGraph.h:251
std::array< PerfNode *, MAX_STACK_SIZE > _stack
The full callstack. Currently capped at a depth of 100.
Definition: PerfGraph.h:254
bool _active
Whether or not timing is active.
Definition: PerfGraph.h:283

◆ 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 334 of file PerfGraph.C.

Referenced by PerfGraphOutput::output().

335 {
336  updateTiming();
337 
338  console << "\nPerformance Graph:\n";
339  FullTable vtable({"Section",
340  "Calls",
341  "Self(s)",
342  "Avg(s)",
343  "%",
344  "Children(s)",
345  "Avg(s)",
346  "%",
347  "Total(s)",
348  "Avg(s)",
349  "%"},
350  10);
351 
352  vtable.setColumnFormat({VariadicTableColumnFormat::AUTO, // Section Name
363 
364  vtable.setColumnPrecision({1, 0, 3, 3, 2, 3, 3, 2, 3, 3, 2});
365 
366  recursivelyPrintGraph(_root_node.get(), vtable, level);
367  vtable.print(console);
368 }
void recursivelyPrintGraph(PerfNode *current_node, FullTable &vtable, unsigned int level, unsigned int current_depth=0)
Helper for printing out the graph.
Definition: PerfGraph.C:220
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:160
std::unique_ptr< PerfNode > _root_node
The root node of the graph.
Definition: PerfGraph.h:248

◆ 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 371 of file PerfGraph.C.

Referenced by PerfGraphOutput::output().

372 {
373  updateTiming();
374 
375  console << "\nHeaviest Branch:\n";
376  FullTable vtable({"Section",
377  "Calls",
378  "Self(s)",
379  "Avg(s)",
380  "%",
381  "Children(s)",
382  "Avg(s)",
383  "%",
384  "Total(s)",
385  "Avg(s)",
386  "%"},
387  10);
388 
389  vtable.setColumnFormat({VariadicTableColumnFormat::AUTO, // Section Name
400 
401  vtable.setColumnPrecision({1, 0, 3, 3, 2, 3, 3, 2, 3, 3, 2});
402 
404  vtable.print(console);
405 }
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:160
std::unique_ptr< PerfNode > _root_node
The root node of the graph.
Definition: PerfGraph.h:248
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:276

◆ 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 408 of file PerfGraph.C.

Referenced by PerfGraphOutput::output().

409 {
410  updateTiming();
411 
412  console << "\nHeaviest Sections:\n";
413 
414  // Indirect Sort The Self Time
415  std::vector<size_t> sorted;
417  _section_time_ptrs.end(),
418  sorted,
419  [](SectionTime * lhs, SectionTime * rhs) {
420  if (lhs && rhs)
421  return lhs->_self > rhs->_self;
422 
423  // If the LHS exists - it's definitely bigger than a non-existant RHS
424  if (lhs)
425  return true;
426 
427  // Both don't exist - so it doesn't matter how we sort them
428  return false;
429  });
430 
431  HeaviestTable vtable({"Section", "Calls", "Self(s)", "Avg.", "%"}, 10);
432 
433  vtable.setColumnFormat({VariadicTableColumnFormat::AUTO, // Doesn't matter
438 
439  vtable.setColumnPrecision({1, 1, 3, 3, 2});
440 
441  mooseAssert(!_section_time_ptrs.empty(),
442  "updateTiming() must be run before printHeaviestSections()!");
443 
444  // The total time of the root node
445  auto total_root_time = _section_time_ptrs[0]->_total;
446 
447  // Now print out the largest ones
448  for (unsigned int i = 0; i < num_sections; i++)
449  {
450  auto id = sorted[i];
451 
452  vtable.addRow(id == 0 ? _root_name : _id_to_section_name[id],
453  _section_time_ptrs[id]->_num_calls,
454  _section_time_ptrs[id]->_self,
455  _section_time_ptrs[id]->_self /
456  static_cast<Real>(_section_time_ptrs[id]->_num_calls),
457  100 * _section_time_ptrs[id]->_self / total_root_time);
458  }
459 
460  vtable.print(console);
461 }
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:160
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:260
VariadicTable< std::string, unsigned long int, Real, Real, Real > HeaviestTable
Definition: PerfGraph.h:172
const std::string _root_name
The name to display for the root of the graph when printing the entire graph.
Definition: PerfGraph.h:245
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:280

◆ 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 127 of file PerfGraph.C.

Referenced by PerfGuard::PerfGuard().

128 {
129  if (!_active)
130  return;
131 
132  auto new_node = _stack[_current_position]->getChild(id);
133 
134  // Set the start time
135  new_node->setStartTime(std::chrono::steady_clock::now());
136 
137  // Increment the number of calls
138  new_node->incrementNumCalls();
139 
141 
142  if (_current_position >= MAX_STACK_SIZE)
143  mooseError("PerfGraph is out of stack space!");
144 
145  _stack[_current_position] = new_node;
146 }
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:251
std::array< PerfNode *, MAX_STACK_SIZE > _stack
The full callstack. Currently capped at a depth of 100.
Definition: PerfGraph.h:254
bool _active
Whether or not timing is active.
Definition: PerfGraph.h:283

◆ 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 198 of file PerfGraph.C.

Referenced by updateTiming().

199 {
200  auto id = current_node->id();
201 
202  auto self = std::chrono::duration<double>(current_node->selfTime()).count();
203  auto children = std::chrono::duration<double>(current_node->childrenTime()).count();
204  auto total = std::chrono::duration<double>(current_node->totalTime()).count();
205  auto num_calls = current_node->numCalls();
206 
207  // RHS insertion on purpose
208  auto & section_time = _section_time[_id_to_section_name[id]];
209 
210  section_time._self += self;
211  section_time._children += children;
212  section_time._total += total;
213  section_time._num_calls += num_calls;
214 
215  for (auto & child_it : current_node->children())
216  recursivelyFillTime(child_it.second.get());
217 }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:274
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:260
void recursivelyFillTime(PerfNode *current_node)
Updates the cumulative self/children/total time.
Definition: PerfGraph.C:198
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 220 of file PerfGraph.C.

Referenced by print().

224 {
225  mooseAssert(_id_to_section_name.find(current_node->id()) != _id_to_section_name.end(),
226  "Unable to find section name!");
227 
228  auto & name = current_node->id() == 0 ? _root_name : _id_to_section_name[current_node->id()];
229 
230  mooseAssert(_id_to_level.find(current_node->id()) != _id_to_level.end(), "Unable to find level!");
231  auto & node_level = _id_to_level[current_node->id()];
232 
233  if (node_level <= level)
234  {
235  mooseAssert(!_section_time_ptrs.empty(),
236  "updateTiming() must be run before recursivelyPrintGraph!");
237 
238  auto section = std::string(current_depth * 2, ' ') + name;
239 
240  // The total time of the root node
241  auto total_root_time = _section_time_ptrs[0]->_total;
242 
243  auto num_calls = current_node->numCalls();
244  auto self = std::chrono::duration<double>(current_node->selfTime()).count();
245  auto self_avg = self / static_cast<Real>(num_calls);
246  auto self_percent = 100. * self / total_root_time;
247 
248  auto children = std::chrono::duration<double>(current_node->childrenTime()).count();
249  auto children_avg = children / static_cast<Real>(num_calls);
250  auto children_percent = 100. * children / total_root_time;
251 
252  auto total = std::chrono::duration<double>(current_node->totalTime()).count();
253  auto total_avg = total / static_cast<Real>(num_calls);
254  auto total_percent = 100. * total / total_root_time;
255 
256  vtable.addRow(section,
257  num_calls,
258  self,
259  self_avg,
260  self_percent,
261  children,
262  children_avg,
263  children_percent,
264  total,
265  total_avg,
266  total_percent);
267 
268  current_depth++;
269  }
270 
271  for (auto & child_it : current_node->children())
272  recursivelyPrintGraph(child_it.second.get(), vtable, level, current_depth);
273 }
void recursivelyPrintGraph(PerfNode *current_node, FullTable &vtable, unsigned int level, unsigned int current_depth=0)
Helper for printing out the graph.
Definition: PerfGraph.C:220
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:260
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:263
const std::string _root_name
The name to display for the root of the graph when printing the entire graph.
Definition: PerfGraph.h:245
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:280

◆ 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 276 of file PerfGraph.C.

Referenced by printHeaviestBranch().

279 {
280  mooseAssert(!_section_time_ptrs.empty(),
281  "updateTiming() must be run before recursivelyPrintGraph!");
282 
283  auto & name = current_node->id() == 0 ? _root_name : _id_to_section_name[current_node->id()];
284 
285  auto section = std::string(current_depth * 2, ' ') + name;
286 
287  // The total time of the root node
288  auto total_root_time = _section_time_ptrs[0]->_total;
289 
290  auto num_calls = current_node->numCalls();
291  auto self = std::chrono::duration<double>(current_node->selfTime()).count();
292  auto self_avg = self / static_cast<Real>(num_calls);
293  auto self_percent = 100. * self / total_root_time;
294 
295  auto children = std::chrono::duration<double>(current_node->childrenTime()).count();
296  auto children_avg = children / static_cast<Real>(num_calls);
297  auto children_percent = 100. * children / total_root_time;
298 
299  auto total = std::chrono::duration<double>(current_node->totalTime()).count();
300  auto total_avg = total / static_cast<Real>(num_calls);
301  auto total_percent = 100. * total / total_root_time;
302 
303  vtable.addRow(section,
304  num_calls,
305  self,
306  self_avg,
307  self_percent,
308  children,
309  children_avg,
310  children_percent,
311  total,
312  total_avg,
313  total_percent);
314 
315  current_depth++;
316 
317  if (!current_node->children().empty())
318  {
319  PerfNode * heaviest_child = nullptr;
320 
321  for (auto & child_it : current_node->children())
322  {
323  auto current_child = child_it.second.get();
324 
325  if (!heaviest_child || (current_child->totalTime() > heaviest_child->totalTime()))
326  heaviest_child = current_child;
327  }
328 
329  recursivelyPrintHeaviestGraph(heaviest_child, vtable, current_depth);
330  }
331 }
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:260
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
const std::string _root_name
The name to display for the root of the graph when printing the entire graph.
Definition: PerfGraph.h:245
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:276
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:280

◆ 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 42 of file PerfGraph.C.

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

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

◆ sectionName()

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

Grab the name of a section.

Definition at line 60 of file PerfGraph.C.

61 {
62  auto find_it = _id_to_section_name.find(id);
63 
64  if (find_it == _id_to_section_name.end())
65  mooseError("PerfGraph cannot find a section name associated with id: ", id);
66 
67  return find_it->second;
68 }
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:260

◆ 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:283

◆ updateTiming()

void PerfGraph::updateTiming ( )

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

Definition at line 160 of file PerfGraph.C.

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

161 {
162  // First update all of the currently running nodes
163  auto now = std::chrono::steady_clock::now();
164  for (unsigned int i = 0; i <= _current_position; i++)
165  {
166  auto node = _stack[i];
167  node->addTime(now);
168  node->setStartTime(now);
169  }
170 
171  // Zero out the entries
172  for (auto & section_time_it : _section_time)
173  {
174  auto & section_time = section_time_it.second;
175 
176  section_time._num_calls = 0;
177  section_time._self = 0.;
178  section_time._children = 0.;
179  section_time._total = 0.;
180  }
181 
183 
184  // Update vector pointing to section times
185  // Note: we are doing this _after_ recursively filling
186  // because new entries may have been created
188 
189  for (auto & section_time_it : _section_time)
190  {
191  auto id = _section_name_to_id[section_time_it.first];
192 
193  _section_time_ptrs[id] = &section_time_it.second;
194  }
195 }
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:274
unsigned int _current_position
The current node position in the stack.
Definition: PerfGraph.h:251
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:260
void recursivelyFillTime(PerfNode *current_node)
Updates the cumulative self/children/total time.
Definition: PerfGraph.C:198
std::array< PerfNode *, MAX_STACK_SIZE > _stack
The full callstack. Currently capped at a depth of 100.
Definition: PerfGraph.h:254
std::unique_ptr< PerfNode > _root_node
The root node of the graph.
Definition: PerfGraph.h:248
std::map< std::string, PerfID > _section_name_to_id
Map of section names to IDs.
Definition: PerfGraph.h:257
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:280

Friends And Related Function Documentation

◆ PerfGuard

friend class PerfGuard
friend

Definition at line 286 of file PerfGraph.h.

Member Data Documentation

◆ _active

bool PerfGraph::_active
protected

Whether or not timing is active.

Definition at line 283 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 251 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 263 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_name

const std::string PerfGraph::_root_name
protected

The name to display for the root of the graph when printing the entire graph.

Definition at line 245 of file PerfGraph.h.

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

◆ _root_node

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

The root node of the graph.

Definition at line 248 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 257 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 274 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 280 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 254 of file PerfGraph.h.

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

◆ ROOT_NAME

const std::string PerfGraph::ROOT_NAME = "Root"
staticprotected

The name (handle) of the root node.

Definition at line 242 of file PerfGraph.h.

Referenced by PerfGraph().


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