Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prof_time.h
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: prof_time.h
4 // Created: Tue Jan 11 15:13:48 EST 2011
5 // Creator: davidl (on Darwin eleanor.jlab.org 10.6.0 i386)
6 //
7 
8 #ifndef _prof_time_
9 #define _prof_time_
10 
11 #include <sys/time.h>
12 #include <map>
13 #include <string>
14 
15 /// This utility class is to help in doing detailed profiling of code.
16 /// It is uses the unix itimer system (see man getitimer). It is assumed
17 /// that all 3 of the hi-res timers are running. They should be because
18 /// JANA starts them all at program start up.
19 ///
20 /// To use this, one would want to do something like the following:
21 ///
22 /// Declare a container to hold timing info outside of evnt loop
23 ///
24 /// static map<string, prof_time::time_diffs> prof_times;
25 ///
26 /// Then, wrap the block of code you want to time with a prof_time
27 /// variable and a call to its TimeDiffNow(...) method.
28 ///
29 /// prof_time start_time;
30 ///
31 /// <... do something ...>
32 ///
33 /// start_time.TimeDiffNow(prof_times, "My code block 1");
34 ///
35 /// Note that the current time is copied into the prof_time variable
36 /// upon it's creation. To force the recording of a time at a
37 /// position other than the line creating the object, call it's GetTime()
38 /// method.
39 ///
40 /// This system is designed to check all 3 timers. This might add a little
41 /// more overhead than simply checking one timer, but at least this way
42 /// this can be used for a broad range of purposes.
43 
44 class prof_time{
45  public:
46 
47  // Class for holding all times as doubles
48  class time_diffs {
49  public:
50  double real;
51  double virt;
52  double prof;
53 
54  time_diffs():real(0),virt(0),prof(0){}
55  };
56 
57 
58  //----------------------
59  // ctor
61 
62  //----------------------
63  // dtor
64  virtual ~prof_time(){}
65 
66  //----------------------
67  // GetTime
68  void GetTime(void){
69  getitimer(ITIMER_REAL, &real_tmr);
70  getitimer(ITIMER_VIRTUAL, &virt_tmr);
71  getitimer(ITIMER_PROF, &prof_tmr);
72  }
73 
74  //----------------------
75  // TimeDiffNow
76  void TimeDiffNow(std::map<std::string, time_diffs> &prof_times, std::string what){ // update(add to) entry in map or create new one
77  time_diffs tdiffs;
78  prof_time now;
79  TimeDiff(now, tdiffs.real, tdiffs.virt, tdiffs.prof);
80 
81  std::map<string, time_diffs>::iterator iter = prof_times.find(what);
82  if(iter==prof_times.end()){
83  prof_times[what] = tdiffs;
84  }else{
85  (iter->second).real += tdiffs.real;
86  (iter->second).virt += tdiffs.virt;
87  (iter->second).prof += tdiffs.prof;
88  }
89  }
90 
91  //----------------------
92  // TimeDiff
93  void TimeDiff(const prof_time &other_time, double &tdiff_real, double &tdiff_virt, double &tdiff_prof){
94  struct itimerval tmp_real;
95  struct itimerval tmp_virt;
96  struct itimerval tmp_prof;
97 
98  // Automatically decide which to subtract from which depending
99  // on which is greater. Assume if REAL timer is >, then other
100  // timers follow suit.
101  if(timercmp(&real_tmr.it_value, &other_time.real_tmr.it_value, >)){
102  timersub(&real_tmr.it_value, &other_time.real_tmr.it_value, &tmp_real.it_value);
103  timersub(&virt_tmr.it_value, &other_time.virt_tmr.it_value, &tmp_virt.it_value);
104  timersub(&prof_tmr.it_value, &other_time.prof_tmr.it_value, &tmp_prof.it_value);
105  }else{
106  timersub(&other_time.real_tmr.it_value, &real_tmr.it_value, &tmp_real.it_value);
107  timersub(&other_time.virt_tmr.it_value, &virt_tmr.it_value, &tmp_virt.it_value);
108  timersub(&other_time.prof_tmr.it_value, &prof_tmr.it_value, &tmp_prof.it_value);
109  }
110  tdiff_real = (double)tmp_real.it_value.tv_sec + (double)tmp_real.it_value.tv_usec/1.0E6;
111  tdiff_virt = (double)tmp_virt.it_value.tv_sec + (double)tmp_virt.it_value.tv_usec/1.0E6;
112  tdiff_prof = (double)tmp_prof.it_value.tv_sec + (double)tmp_prof.it_value.tv_usec/1.0E6;
113  }
114 
115  struct itimerval real_tmr;
116  struct itimerval virt_tmr;
117  struct itimerval prof_tmr;
118 
119 };
120 
121 #endif // _prof_time_
122 
void TimeDiffNow(std::map< std::string, time_diffs > &prof_times, std::string what)
Definition: prof_time.h:76
void GetTime(void)
Definition: prof_time.h:68
struct itimerval real_tmr
Definition: prof_time.h:115
char string[256]
prof_time()
Definition: prof_time.h:60
struct itimerval prof_tmr
Definition: prof_time.h:117
void TimeDiff(const prof_time &other_time, double &tdiff_real, double &tdiff_virt, double &tdiff_prof)
Definition: prof_time.h:93
struct itimerval virt_tmr
Definition: prof_time.h:116
This utility class is to help in doing detailed profiling of code. It is uses the unix itimer system ...
Definition: prof_time.h:44
virtual ~prof_time()
Definition: prof_time.h:64
float real
Definition: grkuta.cc:42