1 |
|
|
/* |
2 |
|
|
Copyright (c) 2010-2013 Tommaso Urli |
3 |
|
|
|
4 |
|
|
Tommaso Urli tommaso.urli@uniud.it University of Udine |
5 |
|
|
|
6 |
|
|
Permission is hereby granted, free of charge, to any person obtaining |
7 |
|
|
a copy of this software and associated documentation files (the |
8 |
|
|
"Software"), to deal in the Software without restriction, including |
9 |
|
|
without limitation the rights to use, copy, modify, merge, publish, |
10 |
|
|
distribute, sublicense, and/or sell copies of the Software, and to |
11 |
|
|
permit persons to whom the Software is furnished to do so, subject to |
12 |
|
|
the following conditions: |
13 |
|
|
|
14 |
|
|
The above copyright notice and this permission notice shall be |
15 |
|
|
included in all copies or substantial portions of the Software. |
16 |
|
|
|
17 |
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18 |
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19 |
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
20 |
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
21 |
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
22 |
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
23 |
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 |
|
|
|
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
#include "hpp/rbprm/utils/Stdafx.hh" |
28 |
|
|
|
29 |
|
|
#ifndef WIN32 |
30 |
|
|
#include <sys/time.h> |
31 |
|
|
#else |
32 |
|
|
#include <Windows.h> |
33 |
|
|
|
34 |
|
|
#include <iomanip> |
35 |
|
|
#endif |
36 |
|
|
|
37 |
|
|
#include <iomanip> // std::setprecision |
38 |
|
|
|
39 |
|
|
#include "hpp/rbprm/utils/stop-watch.hh" |
40 |
|
|
|
41 |
|
|
using std::map; |
42 |
|
|
using std::ostringstream; |
43 |
|
|
using std::string; |
44 |
|
|
|
45 |
|
|
Stopwatch& getProfiler() { |
46 |
|
|
static Stopwatch s(REAL_TIME); // alternatives are CPU_TIME and REAL_TIME |
47 |
|
|
return s; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
Stopwatch::Stopwatch(StopwatchMode _mode) : active(true), mode(_mode) { |
51 |
|
|
records_of = new map<string, PerformanceData>(); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
Stopwatch::~Stopwatch() { delete records_of; } |
55 |
|
|
|
56 |
|
|
void Stopwatch::set_mode(StopwatchMode new_mode) { mode = new_mode; } |
57 |
|
|
|
58 |
|
|
bool Stopwatch::performance_exists(string perf_name) { |
59 |
|
|
return (records_of->find(perf_name) != records_of->end()); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
long double Stopwatch::take_time() { |
63 |
|
|
if (mode == CPU_TIME) { |
64 |
|
|
// Use ctime |
65 |
|
|
return clock(); |
66 |
|
|
|
67 |
|
|
} else if (mode == REAL_TIME) { |
68 |
|
|
// Query operating system |
69 |
|
|
|
70 |
|
|
#ifdef WIN32 |
71 |
|
|
/* In case of usage under Windows */ |
72 |
|
|
FILETIME ft; |
73 |
|
|
LARGE_INTEGER intervals; |
74 |
|
|
|
75 |
|
|
// Get the amount of 100 nanoseconds intervals elapsed since January 1, 1601 |
76 |
|
|
// (UTC) |
77 |
|
|
GetSystemTimeAsFileTime(&ft); |
78 |
|
|
intervals.LowPart = ft.dwLowDateTime; |
79 |
|
|
intervals.HighPart = ft.dwHighDateTime; |
80 |
|
|
|
81 |
|
|
long double measure = intervals.QuadPart; |
82 |
|
|
measure -= 116444736000000000.0; // Convert to UNIX epoch time |
83 |
|
|
measure /= 10000000.0; // Convert to seconds |
84 |
|
|
|
85 |
|
|
return measure; |
86 |
|
|
#else |
87 |
|
|
/* Linux, MacOS, ... */ |
88 |
|
|
struct timeval tv; |
89 |
|
|
gettimeofday(&tv, NULL); |
90 |
|
|
|
91 |
|
|
long double measure = tv.tv_usec; |
92 |
|
|
measure /= 1000000.0; // Convert to seconds |
93 |
|
|
measure += tv.tv_sec; // Add seconds part |
94 |
|
|
|
95 |
|
|
return measure; |
96 |
|
|
#endif |
97 |
|
|
|
98 |
|
|
} else { |
99 |
|
|
// If mode == NONE, clock has not been initialized, then throw exception |
100 |
|
|
throw StopwatchException("Clock not initialized to a time taking mode!"); |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
void Stopwatch::start(string perf_name) { |
105 |
|
|
if (!active) return; |
106 |
|
|
|
107 |
|
|
// Just works if not already present |
108 |
|
|
records_of->insert(make_pair(perf_name, PerformanceData())); |
109 |
|
|
|
110 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
111 |
|
|
|
112 |
|
|
// Take ctime |
113 |
|
|
perf_info.clock_start = take_time(); |
114 |
|
|
|
115 |
|
|
// If this is a new start (i.e. not a restart) |
116 |
|
|
// if (!perf_info.paused) |
117 |
|
|
// perf_info.last_time = 0; |
118 |
|
|
|
119 |
|
|
perf_info.paused = false; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
void Stopwatch::stop(string perf_name) { |
123 |
|
|
if (!active) return; |
124 |
|
|
|
125 |
|
|
long double clock_end = take_time(); |
126 |
|
|
|
127 |
|
|
// Try to recover performance data |
128 |
|
|
if (!performance_exists(perf_name)) |
129 |
|
|
throw StopwatchException("Performance not initialized."); |
130 |
|
|
|
131 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
132 |
|
|
|
133 |
|
|
perf_info.stops++; |
134 |
|
|
long double lapse = clock_end - perf_info.clock_start; |
135 |
|
|
|
136 |
|
|
if (mode == CPU_TIME) lapse /= (double)CLOCKS_PER_SEC; |
137 |
|
|
|
138 |
|
|
// Update last time |
139 |
|
|
perf_info.last_time = lapse; |
140 |
|
|
|
141 |
|
|
// Update min/max time |
142 |
|
|
if (lapse >= perf_info.max_time) perf_info.max_time = lapse; |
143 |
|
|
if (lapse <= perf_info.min_time || perf_info.min_time == 0) |
144 |
|
|
perf_info.min_time = lapse; |
145 |
|
|
|
146 |
|
|
// Update total time |
147 |
|
|
perf_info.total_time += lapse; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
void Stopwatch::pause(string perf_name) { |
151 |
|
|
if (!active) return; |
152 |
|
|
|
153 |
|
|
long double clock_end = clock(); |
154 |
|
|
|
155 |
|
|
// Try to recover performance data |
156 |
|
|
if (!performance_exists(perf_name)) |
157 |
|
|
throw StopwatchException("Performance not initialized."); |
158 |
|
|
|
159 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
160 |
|
|
|
161 |
|
|
long double lapse = clock_end - perf_info.clock_start; |
162 |
|
|
|
163 |
|
|
// Update total time |
164 |
|
|
perf_info.last_time += lapse; |
165 |
|
|
perf_info.total_time += lapse; |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
void Stopwatch::reset_all() { |
169 |
|
|
if (!active) return; |
170 |
|
|
|
171 |
|
|
map<string, PerformanceData>::iterator it; |
172 |
|
|
|
173 |
|
|
for (it = records_of->begin(); it != records_of->end(); ++it) { |
174 |
|
|
reset(it->first); |
175 |
|
|
} |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
void Stopwatch::report_all(int precision, std::ostream& output) { |
179 |
|
|
if (!active) return; |
180 |
|
|
|
181 |
|
|
output << "\n*** PROFILING RESULTS [ms] (min - avg - max - total time - " |
182 |
|
|
"nSamples) ***\n"; |
183 |
|
|
map<string, PerformanceData>::iterator it; |
184 |
|
|
for (it = records_of->begin(); it != records_of->end(); ++it) { |
185 |
|
|
report(it->first, precision, output); |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
void Stopwatch::reset(string perf_name) { |
190 |
|
|
if (!active) return; |
191 |
|
|
|
192 |
|
|
// Try to recover performance data |
193 |
|
|
if (!performance_exists(perf_name)) |
194 |
|
|
throw StopwatchException("Performance not initialized."); |
195 |
|
|
|
196 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
197 |
|
|
|
198 |
|
|
perf_info.clock_start = 0; |
199 |
|
|
perf_info.total_time = 0; |
200 |
|
|
perf_info.min_time = 0; |
201 |
|
|
perf_info.max_time = 0; |
202 |
|
|
perf_info.last_time = 0; |
203 |
|
|
perf_info.paused = false; |
204 |
|
|
perf_info.stops = 0; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
void Stopwatch::turn_on() { |
208 |
|
|
std::cout << "Stopwatch active." << std::endl; |
209 |
|
|
active = true; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
void Stopwatch::turn_off() { |
213 |
|
|
std::cout << "Stopwatch inactive." << std::endl; |
214 |
|
|
active = false; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
void Stopwatch::report(string perf_name, int precision, std::ostream& output) { |
218 |
|
|
if (!active) return; |
219 |
|
|
|
220 |
|
|
// Try to recover performance data |
221 |
|
|
if (!performance_exists(perf_name)) |
222 |
|
|
throw StopwatchException("Performance not initialized."); |
223 |
|
|
|
224 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
225 |
|
|
|
226 |
|
|
const int MAX_NAME_LENGTH = 40; |
227 |
|
|
string pad = ""; |
228 |
|
|
for (int i = (int)perf_name.length(); i < MAX_NAME_LENGTH; i++) |
229 |
|
|
pad.append(" "); |
230 |
|
|
|
231 |
|
|
output << perf_name << pad; |
232 |
|
|
output << std::fixed << std::setprecision(precision) |
233 |
|
|
<< (perf_info.min_time * 1e3) << "\t"; |
234 |
|
|
output << std::fixed << std::setprecision(precision) |
235 |
|
|
<< (perf_info.total_time * 1e3 / (long double)perf_info.stops) << "\t"; |
236 |
|
|
output << std::fixed << std::setprecision(precision) |
237 |
|
|
<< (perf_info.max_time * 1e3) << "\t"; |
238 |
|
|
output << std::fixed << std::setprecision(precision) |
239 |
|
|
<< (perf_info.total_time * 1e3) << "\t"; |
240 |
|
|
output << std::fixed << std::setprecision(precision) << perf_info.stops |
241 |
|
|
<< std::endl; |
242 |
|
|
|
243 |
|
|
// ostringstream stops; |
244 |
|
|
// stops << perf_info.stops; |
245 |
|
|
// output << " * Stops " << stops.str() << std::endl; |
246 |
|
|
// output << std::endl; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
long double Stopwatch::get_time_so_far(string perf_name) { |
250 |
|
|
// Try to recover performance data |
251 |
|
|
if (!performance_exists(perf_name)) |
252 |
|
|
throw StopwatchException("Performance not initialized."); |
253 |
|
|
|
254 |
|
|
long double lapse = |
255 |
|
|
(take_time() - (records_of->find(perf_name)->second).clock_start); |
256 |
|
|
|
257 |
|
|
if (mode == CPU_TIME) lapse /= (double)CLOCKS_PER_SEC; |
258 |
|
|
|
259 |
|
|
return lapse; |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
long double Stopwatch::get_total_time(string perf_name) { |
263 |
|
|
// Try to recover performance data |
264 |
|
|
if (!performance_exists(perf_name)) |
265 |
|
|
throw StopwatchException("Performance not initialized."); |
266 |
|
|
|
267 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
268 |
|
|
|
269 |
|
|
return perf_info.total_time; |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
long double Stopwatch::get_average_time(string perf_name) { |
273 |
|
|
// Try to recover performance data |
274 |
|
|
if (!performance_exists(perf_name)) |
275 |
|
|
throw StopwatchException("Performance not initialized."); |
276 |
|
|
|
277 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
278 |
|
|
|
279 |
|
|
return (perf_info.total_time / (long double)perf_info.stops); |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
long double Stopwatch::get_min_time(string perf_name) { |
283 |
|
|
// Try to recover performance data |
284 |
|
|
if (!performance_exists(perf_name)) |
285 |
|
|
throw StopwatchException("Performance not initialized."); |
286 |
|
|
|
287 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
288 |
|
|
|
289 |
|
|
return perf_info.min_time; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
long double Stopwatch::get_max_time(string perf_name) { |
293 |
|
|
// Try to recover performance data |
294 |
|
|
if (!performance_exists(perf_name)) |
295 |
|
|
throw StopwatchException("Performance not initialized."); |
296 |
|
|
|
297 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
298 |
|
|
|
299 |
|
|
return perf_info.max_time; |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
long double Stopwatch::get_last_time(string perf_name) { |
303 |
|
|
// Try to recover performance data |
304 |
|
|
if (!performance_exists(perf_name)) |
305 |
|
|
throw StopwatchException("Performance not initialized."); |
306 |
|
|
|
307 |
|
|
PerformanceData& perf_info = records_of->find(perf_name)->second; |
308 |
|
|
|
309 |
|
|
return perf_info.last_time; |
310 |
|
|
} |