IFEM  90A354
Profiler.h
Go to the documentation of this file.
1 // $Id$
2 //==============================================================================
12 //==============================================================================
13 
14 #ifndef _PROFILER_H
15 #define _PROFILER_H
16 
17 #include <iostream>
18 #include <string>
19 #include <vector>
20 #include <map>
21 #include <ctime>
22 
23 #ifdef HAS_TRACY
24 #define TRACY_ENABLE 1
25 #include <tracy/Tracy.hpp>
26 #endif
27 
28 
41 class Profiler
42 {
43 public:
51  explicit Profiler(const std::string& name, bool doReport = true);
54  ~Profiler();
55 
57  void start(const std::string& funcName);
59  void stop(const std::string& funcName);
60 
62  template<class Stream> void report(Stream& os) const;
64  void clear() { myTimers.clear(); allCPU = allWall = 0.0; nRunners = 0; }
65 
66  static bool indentReport;
67 
68 private:
70  struct Profile
71  {
72  clock_t startCPU;
73  double startWall;
74  double totalCPU;
75  double totalWall;
76  size_t nCalls;
77  bool running;
78  int level;
79 
81  Profile() : nCalls(0), running(false), level(-1)
82  { startWall = totalCPU = totalWall = 0.0; startCPU = 0; }
84  bool haveTime() const { return totalCPU >= 0.005 || totalWall >= 0.005; }
85  };
86 
88  friend std::ostream& operator<<(std::ostream& os, const Profile& p);
89 
90  std::string myName;
91  bool autoReport;
92 
93  using ProfileMap = std::map<std::string,Profile>;
94 
96  std::vector<ProfileMap> myMTimers;
97 
98  double allCPU;
99  double allWall;
100 
107  int nRunners;
108 };
109 
110 
111 namespace utl
112 {
113  extern Profiler* profiler;
114 
116  class prof
117  {
118  const char* name;
119  public:
121  explicit prof(const char* tag) : name(tag)
122  { if (profiler) profiler->start(name); }
125  };
126 }
127 
128 
129 #ifdef HAS_TRACY
131 #define PROFILE(label) ZoneNamedN(_prof, #label, true)
132 #else
134 #define PROFILE(label) utl::prof _prof(label)
135 #endif
136 
137 #if PROFILE_LEVEL >= 1
138 #define PROFILE1(label) PROFILE(label)
139 #else
141 #define PROFILE1(label)
142 #endif
143 
144 #if PROFILE_LEVEL >= 2
145 #define PROFILE2(label) PROFILE(label)
146 #else
148 #define PROFILE2(label)
149 #endif
150 
151 #if PROFILE_LEVEL >= 3
152 #define PROFILE3(label) PROFILE(label)
153 #else
155 #define PROFILE3(label)
156 #endif
157 
158 #if PROFILE_LEVEL >= 4
159 #define PROFILE4(label) PROFILE(label)
160 #else
162 #define PROFILE4(label)
163 #endif
164 
165 #endif
Simple class for profiling of computational tasks.
Definition: Profiler.h:42
void stop(const std::string &funcName)
Stops profiling of task funcName and decrements nRunners.
Definition: Profiler.C:110
~Profiler()
The destructor prints the profiling report to the console, if autoReport is true.
Definition: Profiler.C:49
int nRunners
Number of tasks currently running.
Definition: Profiler.h:107
ProfileMap myTimers
The task profiles with names.
Definition: Profiler.h:95
bool autoReport
Whether or not to report results on program exit.
Definition: Profiler.h:91
void clear()
Clears the profiler.
Definition: Profiler.h:64
static bool indentReport
If true, indicate task level by indent.
Definition: Profiler.h:66
std::map< std::string, Profile > ProfileMap
Map of profilers.
Definition: Profiler.h:93
std::string myName
Name of this profiler.
Definition: Profiler.h:90
double allCPU
Accumulated CPU time from all "main" tasks.
Definition: Profiler.h:98
friend std::ostream & operator<<(std::ostream &os, const Profile &p)
Global stream operator printing a Profile instance.
Definition: Profiler.C:144
double allWall
Accumulated wall clock time of all "main" tasks.
Definition: Profiler.h:99
Profiler(const std::string &name, bool doReport=true)
The constructor initializes the profiler object.
Definition: Profiler.C:31
void start(const std::string &funcName)
Starts profiling of task funcName and increments nRunners.
Definition: Profiler.C:86
std::vector< ProfileMap > myMTimers
Task profiles with names and thread iD.
Definition: Profiler.h:96
void report(Stream &os) const
Prints a profiling report for all tasks that have been measured.
Definition: Profiler.C:175
Convenience class to profile the local scope.
Definition: Profiler.h:117
prof(const char *tag)
The constructor starts the profiling of the named task.
Definition: Profiler.h:121
const char * name
Name tag on the local scope to profile.
Definition: Profiler.h:118
~prof()
The destructor stops the profiling.
Definition: Profiler.h:124
General utility classes and functions.
Definition: SIMoptions.h:22
Profiler * profiler
Pointer to the one and only profiler object.
Definition: Profiler.C:27
Stores profiling data for one computational task.
Definition: Profiler.h:71
double totalWall
Total wall clock time consumed by this task so far.
Definition: Profiler.h:75
clock_t startCPU
The last starting CPU time of this task.
Definition: Profiler.h:72
bool running
Flag indicating if this task is currently running.
Definition: Profiler.h:77
double startWall
The last starting wall clock time of this task.
Definition: Profiler.h:73
double totalCPU
Total CPU time consumed by this task so far.
Definition: Profiler.h:74
size_t nCalls
Number of invokations of this task.
Definition: Profiler.h:76
Profile()
The constructor initializes the total times to zero.
Definition: Profiler.h:81
int level
Task level (used for indent in profiling report)
Definition: Profiler.h:78
bool haveTime() const
Checks if this profile item have any timing to report.
Definition: Profiler.h:84