HOPS
HOPS class reference
MHO_Profiler.hh
Go to the documentation of this file.
1 #ifndef MHO_Profiler_HH__
2 #define MHO_Profiler_HH__
3 
4 #include <cstdlib>
5 #include <cstring>
6 #include <iostream>
7 #include <mutex>
8 #include <string>
9 #include <thread>
10 #include <vector>
11 
12 #include "MHO_SelfName.hh"
13 #include "MHO_Timer.hh"
14 
15 #define HOPS_USE_PROFILER
16 
17 namespace hops
18 {
19 
34 namespace sn = selfname;
35 
36 enum MHO_ProfilerFlag : int
37 {
38  pStartFlag = 1, //start time for this segment
39  pStopFlag = 2 //stop timer for this segment
40 };
41 
42 //short hand aliases
45 
46 using hops::pStart;
47 using hops::pStop;
48 
49 #define PROFILE_INFO_LEN 128
50 
55 {
56  int fFlag; //indicates start/stop
57  int fLineNumber; //line number of the file
58  uint64_t fThreadID;
59  double fTime;
60  char fFilename[PROFILE_INFO_LEN]; //truncated filename
61  char fFuncname[PROFILE_INFO_LEN]; //truncated function name
62 };
63 
64 
69 {
70 
71  public:
72  //since this is a singleton we need to remove ability to copy/move
73  MHO_Profiler(MHO_Profiler const&) = delete;
75  MHO_Profiler& operator=(MHO_Profiler const&) = delete;
77 
85  {
86  if(fInstance == nullptr)
87  {
88  fInstance = new MHO_Profiler();
89  }
90  return *fInstance;
91  }
92 
96  void Enable() { fDisabled = false; };
97 
101  void Disable() { fDisabled = true; };
102 
108  bool IsEnabled() const { return !fDisabled; }
109 
110 
119  void Lock() { fMutex.lock(); };
120 
124  void Unlock() { fMutex.unlock(); };
125 
135  void AddEntry(int flag, uint64_t thread_id, std::string filename, int line_num, std::string func_name);
136 
142  void GetEvents(std::vector< MHO_ProfileEvent >& events) { events = fEvents; }
143 
147  void DumpEvents();
148 
149  private:
150  MHO_Profiler(): fNThreads(1)
151  {
152  fDisabled = true; //disabled by default
153  fEvents.reserve(1000);
154  fTimer.Start();
155  };
156 
157  virtual ~MHO_Profiler(){};
158 
159  std::mutex fMutex;
160  static MHO_Profiler* fInstance; //static global class instance
161  std::size_t fNThreads;
162  bool fDisabled;
163 
164  //map each thread to a vector of events
165  // std::vector< std::vector< MHO_ProfileEvent > > fThreadEvents;
166  std::vector< MHO_ProfileEvent > fEvents;
167 
168  MHO_Timer fTimer;
169 };
170 
171 #ifdef HOPS_USE_PROFILER
172 
173  //abuse do-while for multiline macros
174  #define profiler_start() \
175  do \
176  { \
177  MHO_Profiler::GetInstance().Lock(); \
178  MHO_Profiler::GetInstance().AddEntry(pStart, std::hash< std::thread::id >{}(std::this_thread::get_id()), \
179  std::string(sn::file_basename(__FILE__)), __LINE__, \
180  std::string(__PRETTY_FUNCTION__)); \
181  MHO_Profiler::GetInstance().Unlock(); \
182  } \
183  while(0)
184 
185  #define profiler_stop() \
186  do \
187  { \
188  MHO_Profiler::GetInstance().Lock(); \
189  MHO_Profiler::GetInstance().AddEntry(pStop, std::hash< std::thread::id >{}(std::this_thread::get_id()), \
190  std::string(sn::file_basename(__FILE__)), __LINE__, \
191  std::string(__PRETTY_FUNCTION__)); \
192  MHO_Profiler::GetInstance().Unlock(); \
193  } \
194  while(0)
195 
196 #else
197 
198  //profiling is not turned on, ifdef out of compilation
199  #define profiler_start()
200  #define profiler_stop()
201 
202 #endif
203 
204 } // namespace hops
205 
206 #endif
#define PROFILE_INFO_LEN
Definition: MHO_Profiler.hh:49
constexpr to strip path prefix from FILE macros
Class MHO_Profiler - uses the singleton pattern.
Definition: MHO_Profiler.hh:69
bool IsEnabled() const
Checks if the feature is enabled.
Definition: MHO_Profiler.hh:108
void Unlock()
Releases control of the mutex.
Definition: MHO_Profiler.hh:124
static MHO_Profiler & GetInstance()
provide public access to the only static instance
Definition: MHO_Profiler.hh:84
MHO_Profiler(MHO_Profiler const &)=delete
void Enable()
Sets the enabled state to true.
Definition: MHO_Profiler.hh:96
void AddEntry(int flag, uint64_t thread_id, std::string filename, int line_num, std::string func_name)
Adds a profiling event to the internal list if not disabled.
Definition: MHO_Profiler.cc:9
void GetEvents(std::vector< MHO_ProfileEvent > &events)
Getter for events - at end of program, retrieve and utilize the profiler events.
Definition: MHO_Profiler.hh:142
MHO_Profiler(MHO_Profiler &&)=delete
void Disable()
Sets HOPS_COLOR_MSG to disabled state.
Definition: MHO_Profiler.hh:101
MHO_Profiler & operator=(MHO_Profiler const &)=delete
void DumpEvents()
Prints all stored events along with their details.
Definition: MHO_Profiler.cc:28
void Lock()
Acquires a lock using fMutex for thread synchronization. TODO we need to eliminate the need for locks...
Definition: MHO_Profiler.hh:119
MHO_Profiler & operator=(MHO_Profiler &&)=delete
void Start()
Definition: Message/src/MHO_Timer.cc:37
Definition: MHO_ChannelLabeler.hh:17
int fLineNumber
Definition: MHO_Profiler.hh:57
double fTime
Definition: MHO_Profiler.hh:59
char fFuncname[PROFILE_INFO_LEN]
Definition: MHO_Profiler.hh:61
MHO_ProfilerFlag
Definition: MHO_Profiler.hh:37
@ pStartFlag
Definition: MHO_Profiler.hh:38
@ pStopFlag
Definition: MHO_Profiler.hh:39
int fFlag
Definition: MHO_Profiler.hh:56
char fFilename[PROFILE_INFO_LEN]
Definition: MHO_Profiler.hh:60
uint64_t fThreadID
Definition: MHO_Profiler.hh:58
Class MHO_ProfileEvent.
Definition: MHO_Profiler.hh:55