1 // -*- c++ -*- |
1 // -*- c++ -*- |
2 #ifndef HUGO_TIME_MEASURE_H |
2 #ifndef HUGO_TIME_MEASURE_H |
3 #define HUGO_TIME_MEASURE_H |
3 #define HUGO_TIME_MEASURE_H |
|
4 |
|
5 ///ingroup graphs |
|
6 ///\file |
|
7 ///\brief Tools for measuring cpu usage |
4 |
8 |
5 #include <sys/time.h> |
9 #include <sys/time.h> |
6 #include <sys/times.h> |
10 #include <sys/times.h> |
7 #include <fstream> |
11 #include <fstream> |
8 #include <iostream> |
12 #include <iostream> |
9 #include <unistd.h> |
13 #include <unistd.h> |
10 |
14 |
11 namespace hugo { |
15 namespace hugo { |
12 |
16 |
13 // double currTime() { |
17 /// \addtogroup misc |
14 // timeval tv; |
18 /// @{ |
15 // //timezone tz; |
|
16 // gettimeofday(&tv, 0); |
|
17 // return double(tv.tv_sec)+double(tv.tv_usec)/1000000.0; |
|
18 // } |
|
19 |
19 |
20 /// Class to store (cpu)time instances. |
20 /// A class to store (cpu)time instances. |
21 |
21 |
22 /// This class stores five time values. |
22 /// This class stores five time values. |
23 /// - a real time |
23 /// - a real time |
24 /// - a user cpu time |
24 /// - a user cpu time |
25 /// - a system cpu time |
25 /// - a system cpu time |
26 /// - a user cpu time of children |
26 /// - a user cpu time of children |
27 /// - a system cpu time of children |
27 /// - a system cpu time of children |
28 /// |
28 /// |
29 /// TimeStamp's can be added to or substracted from each other and |
29 /// TimeStamp's can be added to or substracted from each other and |
30 /// they can be push to a stream. |
30 /// they can be pushed to a stream. |
31 |
31 |
32 class TimeStamp |
32 class TimeStamp |
33 { |
33 { |
34 tms ts; |
34 tms ts; |
35 double real_time; |
35 double real_time; |
36 |
36 |
37 public: |
37 public: |
38 |
38 |
39 tms &getTms() {return ts;} |
39 tms &getTms() {return ts;} |
40 const tms &getTms() const {return ts;} |
40 const tms &getTms() const {return ts;} |
41 double getRealTime() const {return real_time;} |
41 ///Read the current time values of the process |
42 ///Read the current time values of the process. |
|
43 void stamp() |
42 void stamp() |
44 { |
43 { |
45 timeval tv; |
44 timeval tv; |
46 times(&ts); |
45 times(&ts); |
47 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; |
46 gettimeofday(&tv, 0);real_time=tv.tv_sec+double(tv.tv_usec)/1e6; |
48 } |
47 } |
49 |
48 |
50 /// Constructor initializing with zero. |
49 /// Constructor initializing with zero |
51 TimeStamp() |
50 TimeStamp() |
52 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;} |
51 { ts.tms_utime=ts.tms_stime=ts.tms_cutime=ts.tms_cstime=0; real_time=0;} |
53 ///Constructor initializing with the current time values of the process. |
52 ///Constructor initializing with the current time values of the process |
54 TimeStamp(void *) { stamp();} |
53 TimeStamp(void *) { stamp();} |
55 |
54 |
|
55 /// |
56 TimeStamp &operator+=(const TimeStamp &b) |
56 TimeStamp &operator+=(const TimeStamp &b) |
57 { |
57 { |
58 ts.tms_utime+=b.ts.tms_utime; |
58 ts.tms_utime+=b.ts.tms_utime; |
59 ts.tms_stime+=b.ts.tms_stime; |
59 ts.tms_stime+=b.ts.tms_stime; |
60 ts.tms_cutime+=b.ts.tms_cutime; |
60 ts.tms_cutime+=b.ts.tms_cutime; |
61 ts.tms_cstime+=b.ts.tms_cstime; |
61 ts.tms_cstime+=b.ts.tms_cstime; |
62 real_time+=b.real_time; |
62 real_time+=b.real_time; |
63 return *this; |
63 return *this; |
64 } |
64 } |
|
65 /// |
65 TimeStamp operator+(const TimeStamp &b) const |
66 TimeStamp operator+(const TimeStamp &b) const |
66 { |
67 { |
67 TimeStamp t(*this); |
68 TimeStamp t(*this); |
68 return t+=b; |
69 return t+=b; |
69 } |
70 } |
|
71 /// |
70 TimeStamp &operator-=(const TimeStamp &b) |
72 TimeStamp &operator-=(const TimeStamp &b) |
71 { |
73 { |
72 ts.tms_utime-=b.ts.tms_utime; |
74 ts.tms_utime-=b.ts.tms_utime; |
73 ts.tms_stime-=b.ts.tms_stime; |
75 ts.tms_stime-=b.ts.tms_stime; |
74 ts.tms_cutime-=b.ts.tms_cutime; |
76 ts.tms_cutime-=b.ts.tms_cutime; |
75 ts.tms_cstime-=b.ts.tms_cstime; |
77 ts.tms_cstime-=b.ts.tms_cstime; |
76 real_time-=b.real_time; |
78 real_time-=b.real_time; |
77 return *this; |
79 return *this; |
78 } |
80 } |
|
81 /// |
79 TimeStamp operator-(const TimeStamp &b) const |
82 TimeStamp operator-(const TimeStamp &b) const |
80 { |
83 { |
81 TimeStamp t(*this); |
84 TimeStamp t(*this); |
82 return t-=b; |
85 return t-=b; |
83 } |
86 } |
89 return t-*this; |
92 return t-*this; |
90 } |
93 } |
91 |
94 |
92 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
95 friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
93 |
96 |
|
97 ///Gives back the user time of the process |
94 double getUserTime() const |
98 double getUserTime() const |
95 { |
99 { |
96 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK); |
100 return double(ts.tms_utime)/sysconf(_SC_CLK_TCK); |
97 } |
101 } |
|
102 ///Gives back the system time of the process |
98 double getSystemTime() const |
103 double getSystemTime() const |
99 { |
104 { |
100 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK); |
105 return double(ts.tms_stime)/sysconf(_SC_CLK_TCK); |
101 } |
106 } |
|
107 ///Gives back the user time of the process' children |
102 double getCUserTime() const |
108 double getCUserTime() const |
103 { |
109 { |
104 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK); |
110 return double(ts.tms_cutime)/sysconf(_SC_CLK_TCK); |
105 } |
111 } |
|
112 ///Gives back the user time of the process' children |
106 double getCSystemTime() const |
113 double getCSystemTime() const |
107 { |
114 { |
108 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); |
115 return double(ts.tms_cstime)/sysconf(_SC_CLK_TCK); |
109 } |
116 } |
|
117 ///Gives back the real time of the process |
|
118 double getRealTime() const {return real_time;} |
110 }; |
119 }; |
111 |
120 |
112 ///Class measuring the cpu time and real time usage of the process. |
121 ///Class measuring the cpu time and real time usage of the process |
113 class Timer |
122 class Timer |
114 { |
123 { |
115 TimeStamp start_time; |
124 TimeStamp start_time; |
116 |
125 |
117 void _reset() {start_time.stamp();} |
126 void _reset() {start_time.stamp();} |
118 |
127 |
119 public: |
128 public: |
120 ///Constructor. It starts with zero time counters. |
129 ///Constructor. It starts with zero time counters |
121 Timer() {_reset();} |
130 Timer() {_reset();} |
122 |
131 |
123 ///Computes the ellapsed time. |
132 ///Computes the ellapsed time |
124 |
133 |
125 ///This conversion computes the ellapsed time |
134 ///This conversion computes the ellapsed time |
126 ///since the construction of \c t or since |
135 ///since the construction of \c t or since |
127 ///the last \c t.reset(). |
136 ///the last \c t.reset(). |
128 operator TimeStamp () |
137 operator TimeStamp () |
130 TimeStamp t; |
139 TimeStamp t; |
131 t.stamp(); |
140 t.stamp(); |
132 return t-start_time; |
141 return t-start_time; |
133 } |
142 } |
134 |
143 |
135 ///Resets the time counters. |
144 ///Resets the time counters |
136 TimeStamp reset() |
145 TimeStamp reset() |
137 { |
146 { |
138 TimeStamp t(start_time); |
147 TimeStamp t(start_time); |
139 _reset(); |
148 _reset(); |
140 return start_time-t; |
149 return start_time-t; |
141 } |
150 } |
142 }; |
151 }; |
143 |
152 |
144 ///Prints the time counters. |
153 ///Prints the time counters |
|
154 |
|
155 ///Prints the time counters in the folloing form: |
|
156 /// |
|
157 /// u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs |
|
158 /// |
|
159 /// where the values are the |
|
160 /// - a user cpu time, |
|
161 /// - a system cpu time, |
|
162 /// - a user cpu time of children, |
|
163 /// - a system cpu time of children and |
|
164 /// - a real time, |
|
165 /// |
|
166 ///respectively |
145 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
167 inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
146 { |
168 { |
147 long cls = sysconf(_SC_CLK_TCK); |
169 long cls = sysconf(_SC_CLK_TCK); |
148 os << "u: " << double(t.getTms().tms_utime)/cls << |
170 os << "u: " << double(t.getTms().tms_utime)/cls << |
149 "s, s: " << double(t.getTms().tms_stime)/cls << |
171 "s, s: " << double(t.getTms().tms_stime)/cls << |