www.mooseframework.org
PerfGraph.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE Includes
13 #include "MooseTypes.h"
14 #include "PerfNode.h"
15 #include "IndirectSort.h"
16 #include "ConsoleStream.h"
17 #include "MooseError.h"
18 
19 // System Includes
20 #include <array>
21 
22 // Forward Declarations
23 class PerfGuard;
24 
25 template <class... Ts>
27 
28 #define MAX_STACK_SIZE 100
29 
34 class PerfGraph
35 {
36 public:
40  enum TimeType : char
41  {
51  };
52 
56  PerfGraph(const std::string & root_name);
57 
61  ~PerfGraph() = default;
62 
68  PerfID registerSection(const std::string & section_name, unsigned int level);
69 
76  void print(const ConsoleStream & console, unsigned int level);
77 
83  void printHeaviestBranch(const ConsoleStream & console);
84 
90  void printHeaviestSections(const ConsoleStream & console, const unsigned int num_sections);
91 
95  const std::string & sectionName(const PerfID id) const;
96 
102  bool active() const { return _active; }
103 
107  void setActive(bool active) { _active = active; }
108 
112  unsigned long int getNumCalls(const std::string & section_name);
113 
117  Real getTime(const TimeType type, const std::string & section_name);
118 
125  const Real & getSelfTime(const std::string & section_name)
126  {
127  return _section_time[section_name]._self;
128  }
129 
136  const Real & getChildrenTime(const std::string & section_name)
137  {
138  return _section_time[section_name]._children;
139  }
140 
147  const Real & getTotalTime(const std::string & section_name)
148  {
149  return _section_time[section_name]._total;
150  }
151 
155  void updateTiming();
156 
157 protected:
158  typedef VariadicTable<std::string,
159  unsigned long int,
160  Real,
161  Real,
162  Real,
163  Real,
164  Real,
165  Real,
166  Real,
167  Real,
168  Real>
170 
172 
178  struct SectionTime
179  {
180  Real _self = 0.;
181  Real _children = 0.;
182  Real _total = 0.;
183  unsigned long int _num_calls = 0;
184  };
185 
191  void push(const PerfID id);
192 
198  void pop();
199 
208  void recursivelyPrintGraph(PerfNode * current_node,
209  FullTable & vtable,
210  unsigned int level,
211  unsigned int current_depth = 0);
212 
220  void recursivelyPrintHeaviestGraph(PerfNode * current_node,
221  FullTable & vtable,
222  unsigned int current_depth = 0);
223 
231  void recursivelyFillTime(PerfNode * current_node);
232 
238  void printHeaviestSections(const ConsoleStream & console);
239 
241  static const std::string ROOT_NAME;
242 
244  const std::string _root_name;
245 
247  std::unique_ptr<PerfNode> _root_node;
248 
250  unsigned int _current_position;
251 
253  std::array<PerfNode *, MAX_STACK_SIZE> _stack;
254 
256  std::map<std::string, PerfID> _section_name_to_id;
257 
259  std::map<PerfID, std::string> _id_to_section_name;
260 
262  std::map<PerfID, unsigned int> _id_to_level;
263 
273  std::map<std::string, SectionTime> _section_time;
274 
279  std::vector<SectionTime *> _section_time_ptrs;
280 
282  bool _active;
283 
284  // Here so PerfGuard is the only thing that can call push/pop
285  friend class PerfGuard;
286 };
287 
A helper class for re-directing output streams to Console output objects form MooseObjects.
Definition: ConsoleStream.h:28
std::map< std::string, SectionTime > _section_time
The time for each section.
Definition: PerfGraph.h:273
static const std::string ROOT_NAME
The name (handle) of the root node.
Definition: PerfGraph.h:241
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
unsigned long int getNumCalls(const std::string &section_name)
Get the number of calls for a section.
Definition: PerfGraph.C:71
A class for "pretty printing" a table of data.
Definition: PerfGraph.h:26
VariadicTable< std::string, unsigned long int, Real, Real, Real, Real, Real, Real, Real, Real, Real > FullTable
Definition: PerfGraph.h:169
void updateTiming()
Udates the time section_time and time for all currently running nodes.
Definition: PerfGraph.C:160
unsigned int PerfID
Definition: MooseTypes.h:163
unsigned long int _num_calls
Definition: PerfGraph.h:183
unsigned int _current_position
The current node position in the stack.
Definition: PerfGraph.h:250
std::map< PerfID, std::string > _id_to_section_name
Map of IDs to section names.
Definition: PerfGraph.h:259
Scope guard for starting and stopping timing for a node.
Definition: PerfGuard.h:24
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:253
~PerfGraph()=default
Destructor.
void setActive(bool active)
Turn on or off timing.
Definition: PerfGraph.h:107
bool active() const
Whether or not timing is active.
Definition: PerfGraph.h:102
bool _active
Whether or not timing is active.
Definition: PerfGraph.h:282
std::unique_ptr< PerfNode > _root_node
The root node of the graph.
Definition: PerfGraph.h:247
void pop()
Remove a Node from the end of the current scope.
Definition: PerfGraph.C:149
VariadicTable< std::string, unsigned long int, Real, Real, Real > HeaviestTable
Definition: PerfGraph.h:171
void print(const ConsoleStream &console, unsigned int level)
Print the tree out.
Definition: PerfGraph.C:334
const Real & getTotalTime(const std::string &section_name)
Get a reference to the total time for a section.
Definition: PerfGraph.h:147
std::map< PerfID, unsigned int > _id_to_level
Map of IDs to level.
Definition: PerfGraph.h:262
MatType type
const Real & getSelfTime(const std::string &section_name)
Get a reference to the self time for a section.
Definition: PerfGraph.h:125
void printHeaviestBranch(const ConsoleStream &console)
Print out the heaviest branch through the tree.
Definition: PerfGraph.C:371
const std::string & sectionName(const PerfID id) const
Grab the name of a section.
Definition: PerfGraph.C:60
TimeType
For retrieving values.
Definition: PerfGraph.h:40
const std::string _root_name
The name to display for the root of the graph when printing the entire graph.
Definition: PerfGraph.h:244
void printHeaviestSections(const ConsoleStream &console, const unsigned int num_sections)
Print out the heaviest sections that were timed.
Definition: PerfGraph.C:408
Real getTime(const TimeType type, const std::string &section_name)
Get a reference to the time for a section.
Definition: PerfGraph.C:87
std::map< std::string, PerfID > _section_name_to_id
Map of section names to IDs.
Definition: PerfGraph.h:256
void push(const PerfID id)
Add a Node onto the end of the end of the current callstack.
Definition: PerfGraph.C:127
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
PerfID registerSection(const std::string &section_name, unsigned int level)
Registers a named section of code.
Definition: PerfGraph.C:42
PerfGraph(const std::string &root_name)
Create a new PerfGraph.
Definition: PerfGraph.C:26
The PerfGraph will hold the master list of all registered performance segments and the head PerfNode...
Definition: PerfGraph.h:34
Use to hold the time for each section.
Definition: PerfGraph.h:178
const Real & getChildrenTime(const std::string &section_name)
Get a reference to the children time for a section.
Definition: PerfGraph.h:136
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:279
A node in the PerfGraph.
Definition: PerfNode.h:23