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 
18 #include "MooseTypes.h"
19 #include "MemoryUtils.h"
20 
21 #include <chrono>
22 #include <map>
23 
24 class PerfNode
25 {
26 public:
31 
35  PerfID id() const { return _id; }
36 
40  void setStartTimeAndMemory(const std::chrono::time_point<std::chrono::steady_clock> time,
41  const long int memory)
42  {
43  _start_time = time;
44  _start_memory = memory;
45  }
46 
51  const std::chrono::time_point<std::chrono::steady_clock> & startTime() const
52  {
53  return _start_time;
54  }
55 
60  const long unsigned int & startMemory() const { return _start_memory; }
61 
65  void addTimeAndMemory(const std::chrono::time_point<std::chrono::steady_clock> time,
66  const long int memory)
67  {
68  _total_time += time - _start_time;
69  _total_memory += memory - _start_memory;
70  }
71 
76 
80  void addTimeAndMemory(const std::chrono::steady_clock::duration time) { _total_time += time; }
81 
92  PerfNode * getChild(const PerfID id)
93  {
94  // RHS insertion on purpose
95  auto & child_node = _children[id];
96 
97  if (!child_node)
98  child_node = std::make_unique<PerfNode>(id);
99 
100  return child_node.get();
101  }
102 
106  const std::map<PerfID, std::unique_ptr<PerfNode>> & children() const { return _children; }
107 
111  std::chrono::steady_clock::duration selfTime() const;
115  Real selfTimeSec() const { return std::chrono::duration<Real>(selfTime()).count(); }
119  Real selfTimeAvg() const { return selfTimeSec() / static_cast<Real>(numCalls()); }
120 
124  std::chrono::steady_clock::duration totalTime() const;
128  Real totalTimeSec() const { return std::chrono::duration<Real>(totalTime()).count(); }
132  Real totalTimeAvg() const { return totalTimeSec() / static_cast<Real>(numCalls()); }
133 
137  std::chrono::steady_clock::duration childrenTime() const;
141  Real childrenTimeSec() const { return std::chrono::duration<Real>(childrenTime()).count(); }
142 
146  unsigned long int numCalls() const { return _num_calls; }
147 
151  long int selfMemory() const;
152 
156  long int childrenMemory() const;
157 
161  long int totalMemory() const { return _total_memory; }
162 
163 private:
165  const PerfID _id;
166 
168  std::chrono::time_point<std::chrono::steady_clock> _start_time;
169 
171  std::chrono::steady_clock::duration _total_time;
172 
174  long unsigned int _start_memory;
175 
177  long unsigned int _num_calls;
178 
180  long unsigned int _total_memory;
181 
183  std::map<PerfID, std::unique_ptr<PerfNode>> _children;
184 
185  friend void dataStore(std::ostream &, const std::unique_ptr<PerfNode> &, void *);
186  friend void dataLoad(std::istream &, const std::unique_ptr<PerfNode> &, void *);
187 };
188 
189 void dataStore(std::ostream & stream, const std::unique_ptr<PerfNode> & node, void * context);
190 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:161
Real selfTimeSec() const
Get the time this node took in seconds.
Definition: PerfNode.h:115
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:40
long unsigned int _start_memory
The starting memory for this node.
Definition: PerfNode.h:174
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:119
const PerfID _id
The unique ID for the section this Node corresponds to.
Definition: PerfNode.h:165
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:177
Real totalTimeSec() const
The time this Node plus all of its children took in seconds.
Definition: PerfNode.h:128
std::chrono::steady_clock::duration childrenTime() const
Get the time this nodes children took.
Definition: PerfNode.C:30
unsigned int PerfID
Definition: MooseTypes.h:212
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:51
void incrementNumCalls()
Increments the number of calls.
Definition: PerfNode.h:75
std::chrono::steady_clock::duration _total_time
The total elapsed time for this node.
Definition: PerfNode.h:171
unsigned long int numCalls() const
Get the number of times this node was called.
Definition: PerfNode.h:146
const std::map< PerfID, std::unique_ptr< PerfNode > > & children() const
Get the children.
Definition: PerfNode.h:106
Real childrenTimeSec() const
The time this node&#39;s children took in seconds.
Definition: PerfNode.h:141
PerfID id() const
Get the ID of this Node.
Definition: PerfNode.h:35
void addTimeAndMemory(const std::chrono::steady_clock::duration time)
Add some time into this Node.
Definition: PerfNode.h:80
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:183
PerfNode * getChild(const PerfID id)
Get a child node with the unique id given.
Definition: PerfNode.h:92
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:180
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:132
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:168
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:65
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:30
const long unsigned int & startMemory() const
Get the current start memory Only makes sense if this node is running.
Definition: PerfNode.h:60
A node in the PerfGraph.
Definition: PerfNode.h:24