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