https://mooseframework.inl.gov
PerfNode.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 
19 #include "MooseTypes.h"
20 #include "MemoryUtils.h"
21 
22 #include <chrono>
23 #include <map>
24 
25 class PerfNode
26 {
27 public:
32 
36  PerfID id() const { return _id; }
37 
41  void setStartTimeAndMemory(const std::chrono::time_point<std::chrono::steady_clock> time,
42  const long int memory)
43  {
44  _start_time = time;
45  _start_memory = memory;
46  }
47 
52  const std::chrono::time_point<std::chrono::steady_clock> & startTime() const
53  {
54  return _start_time;
55  }
56 
61  const long unsigned int & startMemory() const { return _start_memory; }
62 
66  void addTimeAndMemory(const std::chrono::time_point<std::chrono::steady_clock> time,
67  const long int memory)
68  {
69  _total_time += time - _start_time;
70  _total_memory += memory - _start_memory;
71  }
72 
77 
81  void addTimeAndMemory(const std::chrono::steady_clock::duration time) { _total_time += time; }
82 
93  PerfNode * getChild(const PerfID id)
94  {
95  // RHS insertion on purpose
96  auto & child_node = _children[id];
97 
98  if (!child_node)
99  child_node = std::make_unique<PerfNode>(id);
100 
101  return child_node.get();
102  }
103 
107  const std::map<PerfID, std::unique_ptr<PerfNode>> & children() const { return _children; }
108 
112  std::chrono::steady_clock::duration selfTime() const;
116  Real selfTimeSec() const { return std::chrono::duration<Real>(selfTime()).count(); }
120  Real selfTimeAvg() const { return selfTimeSec() / static_cast<Real>(numCalls()); }
121 
125  std::chrono::steady_clock::duration totalTime() const;
129  Real totalTimeSec() const { return std::chrono::duration<Real>(totalTime()).count(); }
133  Real totalTimeAvg() const { return totalTimeSec() / static_cast<Real>(numCalls()); }
134 
138  std::chrono::steady_clock::duration childrenTime() const;
142  Real childrenTimeSec() const { return std::chrono::duration<Real>(childrenTime()).count(); }
143 
147  unsigned long int numCalls() const { return _num_calls; }
148 
152  long int selfMemory() const;
153 
157  long int childrenMemory() const;
158 
162  long int totalMemory() const { return _total_memory; }
163 
164 private:
166  const PerfID _id;
167 
169  std::chrono::time_point<std::chrono::steady_clock> _start_time;
170 
172  std::chrono::steady_clock::duration _total_time;
173 
175  long unsigned int _start_memory;
176 
178  long unsigned int _num_calls;
179 
181  long unsigned int _total_memory;
182 
184  std::map<PerfID, std::unique_ptr<PerfNode>> _children;
185 
186  friend void dataStore(std::ostream &, const std::unique_ptr<PerfNode> &, void *);
187  friend void dataLoad(std::istream &, const std::unique_ptr<PerfNode> &, void *);
188 };
189 
190 void dataStore(std::ostream & stream, const std::unique_ptr<PerfNode> & node, void * context);
191 void dataLoad(std::istream & stream, const std::unique_ptr<PerfNode> & node, void * context);
long int totalMemory() const
Get the amount of memory added by this node.
Definition: PerfNode.h:162
Real selfTimeSec() const
Get the time this node took in seconds.
Definition: PerfNode.h:116
friend void dataLoad(std::istream &, const std::unique_ptr< PerfNode > &, void *)
Definition: PerfNode.C:79
void setStartTimeAndMemory(const std::chrono::time_point< std::chrono::steady_clock > time, const long int memory)
Set the current start time.
Definition: PerfNode.h:41
long unsigned int _start_memory
The starting memory for this node.
Definition: PerfNode.h:175
long int selfMemory() const
Get the amount of memory added by this node.
Definition: PerfNode.C:41
void dataStore(std::ostream &stream, const std::unique_ptr< PerfNode > &node, void *context)
Definition: PerfNode.C:58
Real selfTimeAvg() const
The average time this node took in seconds.
Definition: PerfNode.h:120
const PerfID _id
The unique ID for the section this Node corresponds to.
Definition: PerfNode.h:166
long int childrenMemory() const
Get the amount of memory added by this node.
Definition: PerfNode.C:47
long unsigned int _num_calls
Number of times this node has been called.
Definition: PerfNode.h:178
Real totalTimeSec() const
The time this Node plus all of its children took in seconds.
Definition: PerfNode.h:129
std::chrono::steady_clock::duration childrenTime() const
Get the time this nodes children took.
Definition: PerfNode.C:30
unsigned int PerfID
Definition: MooseTypes.h:240
const std::chrono::time_point< std::chrono::steady_clock > & startTime() const
Get the currnet start time Only makes sense if this node is running.
Definition: PerfNode.h:52
void incrementNumCalls()
Increments the number of calls.
Definition: PerfNode.h:76
std::chrono::steady_clock::duration _total_time
The total elapsed time for this node.
Definition: PerfNode.h:172
unsigned long int numCalls() const
Get the number of times this node was called.
Definition: PerfNode.h:147
const std::map< PerfID, std::unique_ptr< PerfNode > > & children() const
Get the children.
Definition: PerfNode.h:107
Real childrenTimeSec() const
The time this node&#39;s children took in seconds.
Definition: PerfNode.h:142
PerfID id() const
Get the ID of this Node.
Definition: PerfNode.h:36
void addTimeAndMemory(const std::chrono::steady_clock::duration time)
Add some time into this Node.
Definition: PerfNode.h:81
std::chrono::steady_clock::duration totalTime() const
The time this Node plus all of it&#39;s children took.
Definition: PerfNode.C:22
std::map< PerfID, std::unique_ptr< PerfNode > > _children
Timers that are directly underneath this node.
Definition: PerfNode.h:184
PerfNode * getChild(const PerfID id)
Get a child node with the unique id given.
Definition: PerfNode.h:93
void dataLoad(std::istream &stream, const std::unique_ptr< PerfNode > &node, void *context)
Definition: PerfNode.C:79
long unsigned int _total_memory
The total memory added while this node is active.
Definition: PerfNode.h:181
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Real totalTimeAvg() const
The average time this Node plus all of its children took in seconds.
Definition: PerfNode.h:133
std::chrono::time_point< std::chrono::steady_clock > _start_time
The current start_time for this node (if it&#39;s on the stack)
Definition: PerfNode.h:169
friend void dataStore(std::ostream &, const std::unique_ptr< PerfNode > &, void *)
Definition: PerfNode.C:58
void addTimeAndMemory(const std::chrono::time_point< std::chrono::steady_clock > time, const long int memory)
Add some time into this Node by taking the difference with the time passed in.
Definition: PerfNode.h:66
std::chrono::steady_clock::duration selfTime() const
Get the time this node took.
Definition: PerfNode.C:16
PerfNode(const PerfID id)
Create a PerfNode with the given ID.
Definition: PerfNode.h:31
const long unsigned int & startMemory() const
Get the current start memory Only makes sense if this node is running.
Definition: PerfNode.h:61
A node in the PerfGraph.
Definition: PerfNode.h:25