profiler  0.0
Profiler.h
1 #ifndef PROFILER_SRC_MOLPRO_PROFILER_PROFILER_H
2 #define PROFILER_SRC_MOLPRO_PROFILER_PROFILER_H
3 #ifdef MOLPRO_PROFILER_MPI
4 #include <mpi.h>
5 #endif
6 
7 #include <molpro/profiler/SortBy.h>
8 
9 #include <fstream>
10 #include <memory>
11 #include <ostream>
12 #include <stdexcept>
13 #include <string>
14 #include <vector>
15 
16 namespace molpro {
17 namespace profiler {
18 
19 static int hot_default[3] = {255, 0, 0};
20 static int cool_default[3] = {0, 0, 255};
21 static std::vector<std::pair<double, double>> default_heat_adjust = {{}};
22 
23 class Counter;
24 template <class CounterT>
25 class Node;
26 
41 class Profiler {
42 protected:
43  std::string m_description;
44  std::string m_root_name = "All";
46  int m_current_depth = 0;
47 public:
48  std::shared_ptr<profiler::Node<profiler::Counter>> root;
49  std::shared_ptr<profiler::Node<profiler::Counter>> active_node;
56  explicit Profiler(std::string description_, bool with_wall = true, bool with_cpu = false);
57  Profiler(Profiler&&) = default;
58  Profiler& operator=(Profiler&&) = default;
59  ~Profiler();
60 
61  Profiler() = delete;
62  Profiler(const Profiler&) = delete;
63  Profiler& operator=(const Profiler&) = delete;
64 
65  const std::string& description() const { return m_description; }
66 
69  static std::shared_ptr<Profiler> single(const std::string& description_, bool with_wall = true,
70  bool with_cpu = false);
72  static std::shared_ptr<Profiler> single();
74  static void erase(const std::string& description);
75 
84  int get_max_depth() const;
86  void set_max_depth(int new_max_depth);
89  int get_current_depth() const;
90 
96  Profiler& start(const std::string& name);
97 
99  Profiler& stop(const std::string& name = "");
100 
102  Profiler& stop_all();
103 
106 
107  // FIXME Why is this even needed?
109  Profiler& reset(const std::string& name);
110 
115  void operator+=(size_t operations);
116 
120  void operator++();
121 
122  size_t operator++(int);
123 
124 protected:
126  struct Proxy {
128 
129  Proxy(Profiler& prof, const std::string& name) : prof(prof) { prof.start(name); }
130  ~Proxy() { prof.stop(); }
131 
133  Proxy push(const std::string& name) { return Proxy(prof, name); }
134 
139  void operator+=(size_t operations);
140 
144  void operator++();
145 
146  size_t operator++(int);
147  };
148 
149 public:
151  Proxy push(const std::string& name) { return Proxy(*this, name); }
152 
155  std::string str(bool cumulative = true, profiler::SortBy sort_by = profiler::SortBy::wall) const;
156 
173  std::string dotgraph(std::string path, double threshold = 0.01, bool cumulative = true, int hot[3] = hot_default,
174  int cool[3] = cool_default, SortBy sort_by = profiler::SortBy::none,
175  std::vector<std::pair<double, double>> heat_adjust = default_heat_adjust,
176  bool get_percentage_time = false);
177 
178 #ifdef MOLPRO_PROFILER_MPI
179  std::string str(MPI_Comm communicator, bool cumulative = true,
180  profiler::SortBy sort_by = profiler::SortBy::wall) const;
181 
200  std::string dotgraph(std::string path, MPI_Comm communicator, int root_process, double threshold = 0.01,
201  bool cumulative = true, int hot[3] = hot_default, int cool[3] = cool_default,
202  SortBy sort_by = profiler::SortBy::none,
203  std::vector<std::pair<double, double>> heat_adjust = default_heat_adjust,
204  bool get_percentage_time = false);
205 #endif
206 
207  friend std::ostream& operator<<(std::ostream& os, const Profiler& obj);
208 };
209 
210 } // namespace profiler
211 } // namespace molpro
212 #endif // PROFILER_SRC_MOLPRO_PROFILER_PROFILER_H
Resource counter used for storing operation count, call count, timing information.
Definition: Counter.h:15
A node in a parameter tree storing a Counter object aliased by a name.
Definition: Node.h:20
Instrumental profiler for timing sections of code.
Definition: Profiler.h:41
std::shared_ptr< profiler::Node< profiler::Counter > > root
root node of the profiler call tree
Definition: Profiler.h:48
void operator++()
Advance the counter holding the notional number of operations executed in the code segment.
Definition: Profiler.cpp:78
std::string dotgraph(std::string path, double threshold=0.01, bool cumulative=true, int hot[3]=hot_default, int cool[3]=cool_default, SortBy sort_by=profiler::SortBy::none, std::vector< std::pair< double, double >> heat_adjust=default_heat_adjust, bool get_percentage_time=false)
Get a graphviz .dot markup file for a profile.
Definition: Profiler.cpp:116
profiler::Counter & counter()
Access counter at the top of the call stack.
Definition: Profiler.cpp:74
static void erase(const std::string &description)
Remove Profiler with specified description from the singleton register.
Definition: Profiler.cpp:33
int m_max_depth
max depth level of profiler tree counting root as 0. Defaults to largest possible value.
Definition: Profiler.h:45
std::shared_ptr< profiler::Node< profiler::Counter > > active_node
the most recent active node.
Definition: Profiler.h:49
std::string m_description
name of the root node
Definition: Profiler.h:43
Profiler & stop_all()
Stop all nodes and traverse up to the root.
Definition: Profiler.cpp:67
std::string m_root_name
name of the root node
Definition: Profiler.h:44
void set_max_depth(int new_max_depth)
Set the maximum depth the profiler tree is allowed to reach.
Definition: Profiler.cpp:36
Profiler & operator=(Profiler &&)=default
void operator+=(size_t operations)
Advance the counter holding the notional number of operations executed in the code segment.
Definition: Profiler.cpp:76
int get_max_depth() const
Get the maximum depth the profiler tree is allowed to reach.
Definition: Profiler.cpp:35
Profiler & start(const std::string &name)
Traverse down to a child node and start timing.
Definition: Profiler.cpp:39
std::string str(bool cumulative=true, profiler::SortBy sort_by=profiler::SortBy::wall) const
Definition: Profiler.cpp:96
const std::string & description() const
Definition: Profiler.h:65
Profiler & operator=(const Profiler &)=delete
~Profiler()
Definition: Profiler.cpp:16
friend std::ostream & operator<<(std::ostream &os, const Profiler &obj)
Definition: Profiler.cpp:151
Profiler & stop(const std::string &name="")
Stop the active node and traverse up to its parent.
Definition: Profiler.cpp:55
int m_current_depth
current depth of the active node
Definition: Profiler.h:46
Proxy push(const std::string &name)
Returns a proxy object which will start() on construction and stop on destruction.
Definition: Profiler.h:151
Profiler & reset(const std::string &name)
Erases all data and starts from root again.
Definition: Profiler.cpp:155
int get_current_depth() const
Definition: Profiler.cpp:37
Profiler(const Profiler &)=delete
static std::shared_ptr< Profiler > single()
Access the last registered Profiler.
Definition: Profiler.cpp:31
Profiler(Profiler &&)=default
SortBy
Definition: SortBy.h:7
Definition: Profiler.h:5
Proxy object that calls start() on creation and stop() on destruction.
Definition: Profiler.h:126
Profiler & prof
reference to the underlying profiler
Definition: Profiler.h:127
~Proxy()
Definition: Profiler.h:130
void operator+=(size_t operations)
Advance the counter holding the notional number of operations executed in the code segment.
Definition: Profiler.cpp:86
Proxy(Profiler &prof, const std::string &name)
Definition: Profiler.h:129
void operator++()
Advance the counter holding the notional number of operations executed in the code segment.
Definition: Profiler.cpp:88
Proxy push(const std::string &name)
Push a new proxy.
Definition: Profiler.h:133