OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Profiler.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 /****************************************************************************
17 ** TAU Portable Profiling Package **
18 ** http://www.acl.lanl.gov/tau **
19 *****************************************************************************
20 ** Copyright 1997 **
21 ** Department of Computer and Information Science, University of Oregon **
22 ** Advanced Computing Laboratory, Los Alamos National Laboratory **
23 ****************************************************************************/
24 /***************************************************************************
25 ** File : Profiler.h **
26 ** Description : TAU Profiling Package **
27 ** Author : Sameer Shende **
28 ** Contact : sameer@cs.uoregon.edu sameer@acl.lanl.gov **
29 ** Flags : Compile with **
30 ** -DPROFILING_ON to enable profiling (ESSENTIAL) **
31 ** -DPROFILE_STATS for Std. Deviation of Excl Time **
32 ** -DPROFILE_CALLS for trace of each invocation **
33 ** -DDEBUG_PROF for internal debugging messages **
34 ** Documentation : See http://www.acl.lanl.gov/tau **
35 ***************************************************************************/
36 #ifndef PROFILER_H
37 #define PROFILER_H
38 
39 /* TAU PROFILING GROUPS. More will be added later. */
40 #define TAU_DEFAULT 0xffffffff /* All profiling groups enabled*/
41 #define TAU_MESSAGE 0x00000001 /* Message 'm'*/
42 #define TAU_PETE 0x00000002 /* PETE 'p' */
43 #define TAU_VIZ 0x00000004 /* ACLVIZ 'v' */
44 #define TAU_ASSIGN 0x00000008 /* ASSIGN Expression Evaluation 'a' */
45 #define TAU_IO 0x00000010 /* IO routines 'i' */
46 #define TAU_FIELD 0x00000020 /* Field Classes 'f' */
47 #define TAU_LAYOUT 0x00000040 /* Field Layout 'l' */
48 #define TAU_SPARSE 0x00000080 /* Sparse Index 's' */
49 #define TAU_DOMAINMAP 0x00000100 /* Domain Map 'd' */
50 #define TAU_UTILITY 0x00000200 /* Utility 'Ut' */
51 #define TAU_REGION 0x00000400 /* Region 'r' */
52 #define TAU_PARTICLE 0x00000800 /* Particle 'pa' */
53 #define TAU_MESHES 0x00001000 /* Meshes 'mesh' */
54 #define TAU_SUBFIELD 0x00002000 /* SubField 'su' */
55 #define TAU_COMMUNICATION 0x00004000 /* A++ Commm 'c' */
56 #define TAU_DESCRIPTOR_OVERHEAD 0x00008000 /* A++ Descriptor Overhead 'de' */
57 /*
58 SPACE for 0x00010000
59 SPACE for 0x00020000
60 SPACE for 0x00040000
61 SPACE for 0x00080000
62 */
63 #define TAU_FFT 0x00100000 /* FFT 'ff' */
64 #define TAU_ACLMPL 0x00200000 /* ACLMPL 'ac' */
65 #define TAU_PAWS1 0x00400000 /* PAWS1 'paws1' */
66 #define TAU_PAWS2 0x00800000 /* PAWS2 'paws2' */
67 #define TAU_PAWS3 0x01000000 /* PAWS3 'paws3' */
68 /* SPACE for 0x02000000
69  SPACE for 0x04000000
70 */
71 #define TAU_USER4 0x08000000 /* User4 '4' */
72 #define TAU_USER3 0x10000000 /* User3 '3' */
73 #define TAU_USER2 0x20000000 /* User2 '2' */
74 #define TAU_USER1 0x40000000 /* User1 '1' */
75 #define TAU_USER 0x80000000 /* User 'u' */
76 
77 #define TAU_MAX_THREADS 1024
78 
79 #include <typeinfo>
80 #include <vector>
81 #include <utility>
82 #include <list>
83 
84 
86 //
87 // class FunctionInfo
88 //
89 // This class is intended to be instantiated once per function
90 // (or other code block to be timed) as a static variable.
91 //
92 // It will be constructed the first time the function is called,
93 // and that constructor registers this object (and therefore the
94 // function) with the timer system.
95 //
97 
99 {
100 public:
101  // Construct with the name of the function and its type.
102  FunctionInfo(const char* name, const char * type,
103  unsigned int ProfileGroup = TAU_DEFAULT,
104  const char *ProfileGroupName = "TAU_DEFAULT");
105  FunctionInfo(std::string& name, const char* type,
106  unsigned int ProfileGroup = TAU_DEFAULT,
107  const char *ProfileGroupName = "TAU_DEFAULT");
108  FunctionInfo(const char* name, std::string& type,
109  unsigned int ProfileGroup = TAU_DEFAULT,
110  const char *ProfileGroupName = "TAU_DEFAULT");
111  FunctionInfo(std::string& name, std::string& type,
112  unsigned int ProfileGroup = TAU_DEFAULT,
113  const char *ProfileGroupName = "TAU_DEFAULT");
114 
115  FunctionInfo(const FunctionInfo& X) ;
116  // When we exit, we have to clean up.
117  ~FunctionInfo();
119 
120 
121  // Tell it about a function call finishing.
122  void ExcludeTime(double t);
123  // Removing void IncludeTime(double t);
124  // and replacing with
125  void AddInclTime(double t);
126  void AddExclTime(double t);
127  void IncrNumCalls(void);
128 
129  bool GetAlreadyOnStack(void);
130  void SetAlreadyOnStack(bool value);
131 
132  // A container of all of these.
133  // The ctor registers with this.
134 
135  static std::vector<FunctionInfo*> FunctionDB[TAU_MAX_THREADS];
136 
137 #ifdef PROFILE_CALLS
138  list < std::pair<double,double> > *ExclInclCallList;
139  // Make this a ptr to a list so that ~FunctionInfo doesn't destroy it.
140  // time spent in each call
141 
142  int AppendExclInclTimeThisCall(double ex, double in);
143  // to ExclInclCallList
144 #endif // PROFILE_CALLS
145 
146 private:
147 
148  // A record of the information unique to this function.
149  // Statistics about calling this function.
150  long NumCalls;
151  long NumSubrs;
152  double ExclTime;
153  double InclTime;
155 #ifdef PROFILE_STATS
156  double SumExclSqr;
157 #endif // PROFILE_STATS
158 
159 public:
160  std::string Name;
161  std::string Type;
162  std::string GroupName;
164  // Cough up the information about this function.
165  const char* GetName() const { return Name.c_str(); }
166  const char* GetType() const { return Type.c_str(); }
167  const char* GetPrimaryGroup() const { return GroupName.c_str(); }
168  long GetFunctionId() const { return FunctionId; }
169  long GetCalls() const { return NumCalls; }
170  long GetSubrs() const { return NumSubrs; }
171  double GetExclTime() const { return ExclTime; }
172  double GetInclTime() const { return InclTime; }
173  unsigned int GetProfileGroup() const {return MyProfileGroup_; }
174 #ifdef PROFILE_STATS
175  double GetSumExclSqr() const { return SumExclSqr; }
176  void AddSumExclSqr(double ExclSqr) { SumExclSqr += ExclSqr; }
177 #endif // PROFILE_STATS
178 
179 private:
180  unsigned int MyProfileGroup_;
181  // There is a class that will do some initialization
182  // of FunctionStack that can't be done with
183  // just the constructor.
184  //friend class ProfilerInitializer;
185 };
186 
187 //
188 // For efficiency, make the timing updates inline.
189 //
190 inline void
192 { // called by a function to decrease its parent functions time
193  ++NumSubrs;
194  ExclTime -= t; // exclude from it the time spent in child function
195 }
196 
197 
198 inline void
200 {
201  InclTime += t; // Add Inclusive time
202 }
203 
204 inline void
206 {
207  ExclTime += t; // Add Total Time to Exclusive time (-ve)
208 }
209 
210 inline void
212 {
213  NumCalls++; // Increment number of calls
214 }
215 
216 inline void
218 {
219  AlreadyOnStack = value;
220 }
221 
222 inline bool
224 {
225  return AlreadyOnStack;
226 }
227 
228 
230 //
231 // class Profiler
232 //
233 // This class is intended to be instantiated once per function
234 // (or other code block to be timed) as an auto variable.
235 //
236 // It will be constructed each time the block is entered
237 // and destroyed when the block is exited. The constructor
238 // turns on the timer, and the destructor turns it off.
239 //
241 class Profiler
242 {
243 public:
244  Profiler(FunctionInfo * fi, unsigned int ProfileGroup = TAU_DEFAULT,
245  bool StartStop = false);
246 
247  void Start();
248  Profiler(const Profiler& X);
249  Profiler& operator= (const Profiler& X);
250  // Clean up data from this invocation.
251  void Stop();
252  ~Profiler();
253  static void ProfileExit(const char *message=0);
254  int StoreData(void);
255 
256 #if ( defined(PROFILE_CALLS) || defined(PROFILE_STATS) )
257  int ExcludeTimeThisCall(double t);
258  double ExclTimeThisCall; // for this invocation of the function
259 #endif // PROFILE_CALLS || PROFILE_STATS
260 
262  double StartTime;
265 
266 private:
267  unsigned int MyProfileGroup_;
269  bool AddInclFlag;
270  // There is a class that will do some initialization
271  // of FunctionStack that can't be done with
272  // just the constructor.
273  //friend class ProfilerInitializer;
274 };
275 
277 //
278 // class RtsLayer
279 //
280 // This class is used for porting the TAU Profiling package to other
281 // platforms and software frameworks. It contains functions to get
282 // the node id, thread id etc. When Threads are implemented, myThread()
283 // method should return the thread id in {0..N-1} where N is the total
284 // number of threads. All interaction with the outside world should be
285 // restrained to this class.
287 class RtsLayer
288 { // Layer for Profiler to interact with the Runtime System
289  public:
290  static unsigned int ProfileMask;
291  static int Node;
292  RtsLayer () { } // defaults
293  ~RtsLayer () { }
294 
295  static unsigned int enableProfileGroup(unsigned int ProfileGroup) ;
296 
297  static unsigned int resetProfileGroup(void) ;
298 
299  static int setAndParseProfileGroups (char *prog, char *str) ;
300 
301  static bool isEnabled(unsigned int ProfileGroup) ;
302 
303  static void ProfileInit(int argc, char **argv);
304 
305  static std::string PrimaryGroup(const char *ProfileGroupName);
306 
307  static bool isCtorDtor(const char *name);
308 
309  static void TraceSendMsg(int type, int destination, int length);
310  static void TraceRecvMsg(int type, int source, int length);
311 
312  inline
313  static const char * CheckNotNull(const char * str) {
314  if (str == 0) return " ";
315  else return str;
316  }
317 
318 
319  static int SetEventCounter(void);
320  static double GetEventCounter(void);
321 
322  static double getUSecD(void);
323 
324  static int setMyNode(int NodeId);
325 
326  // For tracing
327  static int DumpEDF(void);
328 
329  // Return the number of the 'current' node.
330  static int myNode() { return Node;}
331 
332  // Return the number of the 'current' context.
333  static int myContext() { return 0; }
334 
335  // Return the number of the 'current' thread. 0..TAU_MAX_THREADS-1
336  inline
337  static int myThread() { return 0; }
338 
339 };
340 
342 // TAU PROFILING API MACROS.
343 // To ensure that Profiling does not add any runtime overhead when it
344 // is turned off, these macros expand to null.
346 
347 #define TAU_TYPE_STRING(profileString, str) static string profileString(str);
348 #define TAU_PROFILE(name, type, group) \
349  static FunctionInfo tauFI(name, type, group, #group); \
350  Profiler tauFP(&tauFI, group);
351 #define TAU_PROFILE_TIMER(var, name, type, group) \
352  static FunctionInfo var##fi(name, type, group, #group); \
353  Profiler var(&var##fi, group, true);
354 
355 // Construct a Profiler obj and a FunctionInfo obj with an extended name
356 // e.g., FunctionInfo loop1fi(); Profiler loop1();
357 #define TAU_PROFILE_START(var) var.Start();
358 #define TAU_PROFILE_STOP(var) var.Stop();
359 #define TAU_PROFILE_STMT(stmt) stmt;
360 #define TAU_PROFILE_EXIT(msg) Profiler::ProfileExit(msg);
361 #define TAU_PROFILE_INIT(argc, argv) RtsLayer::ProfileInit(argc, argv);
362 #define TAU_PROFILE_SET_NODE(node) RtsLayer::setMyNode(node);
363 
364 #ifdef NO_RTTI
365 #define CT(obj) std::string(#obj)
366 #else // RTTI is present
367 #define CT(obj) std::string(RtsLayer::CheckNotNull(typeid(obj).name()))
368 #endif //NO_RTTI
369 
370 // Use DEBUGPROFMSG macro as in
371 // DEBUGPROF("Node" << RtsLayer::myNode() << " Message " << endl;);
372 // We're deliberately not using *IpplInfo::Debug because some profiling
373 // debug messages come from the destructor of Profiler in main and by then
374 // IpplInfo object may have been destroyed.
375 
376 #else /* PROFILING_ON */
377 /* In the absence of profiling, define the functions as null */
378 #define TYPE_STRING(profileString, str)
379 #define PROFILED_BLOCK(name, type)
380 
381 #define TAU_TYPE_STRING(profileString, str)
382 #define TAU_PROFILE(name, type, group)
383 #define TAU_PROFILE_TIMER(var, name, type, group)
384 #define TAU_PROFILE_START(var)
385 #define TAU_PROFILE_STOP(var)
386 #define TAU_PROFILE_STMT(stmt)
387 #define TAU_PROFILE_EXIT(msg)
388 #define TAU_PROFILE_INIT(argc, argv)
389 #define TAU_PROFILE_SET_NODE(node)
390 #define CT(obj)
391 
392 #endif /* PROFILING_ON */
393 
394 #ifdef TRACING_ON
395 #define TAU_TRACE_SENDMSG(type, destination, length) RtsLayer::TraceSendMsg(type, destination, length);
396 #define TAU_TRACE_RECVMSG(type, source, length) RtsLayer::TraceRecvMsg(type, source, length);
397 
398 #else /* TRACING_ON */
399 #define TAU_TRACE_SENDMSG(type, destination, length)
400 #define TAU_TRACE_RECVMSG(type, source, length)
401 #endif /* TRACING_ON */
402 
403 #endif /* PROFILER_H */
404 /***************************************************************************
405  * $RCSfile: Profiler.h,v $ $Author: adelmann $
406  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:32 $
407  * IPPL_VERSION_ID: $Id: Profiler.h,v 1.1.1.1 2003/01/23 07:40:32 adelmann Exp $
408  ***************************************************************************/
static Profiler * CurrentProfiler[1024]
Definition: Profiler.h:261
double InclTime
Definition: Profiler.h:153
static unsigned int enableProfileGroup(unsigned int ProfileGroup)
Definition: Profiler.cpp:700
static int Node
Definition: Profiler.h:291
long GetCalls() const
Definition: Profiler.h:169
void IncrNumCalls(void)
Definition: Profiler.h:211
const char * GetName() const
Definition: Profiler.h:165
double StartTime
Definition: Profiler.h:262
unsigned int MyProfileGroup_
Definition: Profiler.h:180
static unsigned int resetProfileGroup(void)
Definition: Profiler.cpp:708
std::string GroupName
Definition: Profiler.h:162
double GetExclTime() const
Definition: Profiler.h:171
#define TAU_DEFAULT
Definition: Profiler.h:40
Definition: rbendmap.h:8
FunctionInfo * ThisFunction
Definition: Profiler.h:263
static const char * CheckNotNull(const char *str)
Definition: Profiler.h:313
long NumCalls
Definition: Profiler.h:150
static int myContext()
Definition: Profiler.h:333
~RtsLayer()
Definition: Profiler.h:293
static int setAndParseProfileGroups(char *prog, char *str)
Definition: Profiler.cpp:837
const char * GetPrimaryGroup() const
Definition: Profiler.h:167
bool AlreadyOnStack
Definition: Profiler.h:154
Profiler & operator=(const Profiler &X)
Definition: Profiler.cpp:409
long NumSubrs
Definition: Profiler.h:151
void AddExclTime(double t)
Definition: Profiler.h:205
static void ProfileExit(const char *message=0)
Definition: Profiler.cpp:525
void AddInclTime(double t)
Definition: Profiler.h:199
static std::string PrimaryGroup(const char *ProfileGroupName)
Definition: Profiler.cpp:1012
void SetAlreadyOnStack(bool value)
Definition: Profiler.h:217
unsigned int MyProfileGroup_
Definition: Profiler.h:267
static bool isCtorDtor(const char *name)
Definition: Profiler.cpp:988
static std::vector< FunctionInfo * > FunctionDB[1024]
Definition: Profiler.h:135
std::string Type
Definition: Profiler.h:161
long GetFunctionId() const
Definition: Profiler.h:168
static int setMyNode(int NodeId)
Definition: Profiler.cpp:714
void Start()
Definition: Profiler.cpp:330
bool AddInclFlag
Definition: Profiler.h:269
bool StartStopUsed_
Definition: Profiler.h:268
static int myThread()
Definition: Profiler.h:337
static void TraceSendMsg(int type, int destination, int length)
Definition: Profiler.cpp:1033
static void TraceRecvMsg(int type, int source, int length)
Definition: Profiler.cpp:1063
long GetSubrs() const
Definition: Profiler.h:170
long FunctionId
Definition: Profiler.h:163
static int myNode()
Definition: Profiler.h:330
static double GetEventCounter(void)
FunctionInfo & operator=(const FunctionInfo &X)
Definition: Profiler.cpp:288
Profiler * ParentProfiler
Definition: Profiler.h:264
static bool isEnabled(unsigned int ProfileGroup)
Definition: Profiler.cpp:732
static int DumpEDF(void)
Definition: Profiler.cpp:1094
bool GetAlreadyOnStack(void)
Definition: Profiler.h:223
void ExcludeTime(double t)
Definition: Profiler.h:191
const char * GetType() const
Definition: Profiler.h:166
const std::string name
int StoreData(void)
Definition: Profiler.cpp:558
#define TAU_MAX_THREADS
Definition: Profiler.h:77
void Stop()
Definition: Profiler.cpp:429
static unsigned int ProfileMask
Definition: Profiler.h:290
double ExclTime
Definition: Profiler.h:152
std::string Name
Definition: Profiler.h:160
unsigned int GetProfileGroup() const
Definition: Profiler.h:173
static int SetEventCounter(void)
RtsLayer()
Definition: Profiler.h:292
Profiler(FunctionInfo *fi, unsigned int ProfileGroup=0xffffffff, bool StartStop=false)
Definition: Profiler.cpp:376
FunctionInfo(const char *name, const char *type, unsigned int ProfileGroup=0xffffffff, const char *ProfileGroupName="TAU_DEFAULT")
Definition: Profiler.cpp:102
double GetInclTime() const
Definition: Profiler.h:172
static double getUSecD(void)
Definition: Profiler.cpp:811
static void ProfileInit(int argc, char **argv)
Definition: Profiler.cpp:970