| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/centroidal-dynamics/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/centroidal-dynamics/stop-watch.hh" | ||
| 40 | |||
| 41 | using std::map; | ||
| 42 | using std::ostringstream; | ||
| 43 | using std::string; | ||
| 44 | |||
| 45 | 18081 | Stopwatch& getProfiler() { | |
| 46 |
4/8✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18080 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
18081 | static Stopwatch s(REAL_TIME); // alternatives are CPU_TIME and REAL_TIME |
| 47 | 18081 | return s; | |
| 48 | } | ||
| 49 | |||
| 50 | 1 | Stopwatch::Stopwatch(StopwatchMode _mode) : active(true), mode(_mode) { | |
| 51 | 1 | records_of = new map<string, PerformanceData>(); | |
| 52 | 1 | } | |
| 53 | |||
| 54 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | Stopwatch::~Stopwatch() { delete records_of; } |
| 55 | |||
| 56 | ✗ | void Stopwatch::set_mode(StopwatchMode new_mode) { mode = new_mode; } | |
| 57 | |||
| 58 | 9046 | bool Stopwatch::performance_exists(string perf_name) { | |
| 59 |
1/2✓ Branch 2 taken 9046 times.
✗ Branch 3 not taken.
|
9046 | return (records_of->find(perf_name) != records_of->end()); |
| 60 | } | ||
| 61 | |||
| 62 | 18080 | long double Stopwatch::take_time() { | |
| 63 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18080 times.
|
18080 | if (mode == CPU_TIME) { |
| 64 | // Use ctime | ||
| 65 | ✗ | return clock(); | |
| 66 | |||
| 67 |
1/2✓ Branch 0 taken 18080 times.
✗ Branch 1 not taken.
|
18080 | } 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 | 18080 | gettimeofday(&tv, NULL); | |
| 90 | |||
| 91 | 18080 | long double measure = tv.tv_usec; | |
| 92 | 18080 | measure /= 1000000.0; // Convert to seconds | |
| 93 | 18080 | measure += tv.tv_sec; // Add seconds part | |
| 94 | |||
| 95 | 18080 | 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 | 9040 | void Stopwatch::start(string perf_name) { | |
| 105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9040 times.
|
9040 | if (!active) return; |
| 106 | |||
| 107 | // Just works if not already present | ||
| 108 |
2/4✓ Branch 2 taken 9040 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 9040 times.
✗ Branch 6 not taken.
|
9040 | records_of->insert(make_pair(perf_name, PerformanceData())); |
| 109 | |||
| 110 |
1/2✓ Branch 1 taken 9040 times.
✗ Branch 2 not taken.
|
9040 | PerformanceData& perf_info = records_of->find(perf_name)->second; |
| 111 | |||
| 112 | // Take ctime | ||
| 113 | 9040 | 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 | 9040 | perf_info.paused = false; | |
| 120 | } | ||
| 121 | |||
| 122 | 9040 | void Stopwatch::stop(string perf_name) { | |
| 123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9040 times.
|
9040 | if (!active) return; |
| 124 | |||
| 125 | 9040 | long double clock_end = take_time(); | |
| 126 | |||
| 127 | // Try to recover performance data | ||
| 128 |
3/6✓ Branch 1 taken 9040 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9040 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 9040 times.
|
9040 | if (!performance_exists(perf_name)) |
| 129 | ✗ | throw StopwatchException("Performance not initialized."); | |
| 130 | |||
| 131 |
1/2✓ Branch 1 taken 9040 times.
✗ Branch 2 not taken.
|
9040 | PerformanceData& perf_info = records_of->find(perf_name)->second; |
| 132 | |||
| 133 | 9040 | perf_info.stops++; | |
| 134 | 9040 | long double lapse = clock_end - perf_info.clock_start; | |
| 135 | |||
| 136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9040 times.
|
9040 | if (mode == CPU_TIME) lapse /= (double)CLOCKS_PER_SEC; |
| 137 | |||
| 138 | // Update last time | ||
| 139 | 9040 | perf_info.last_time = lapse; | |
| 140 | |||
| 141 | // Update min/max time | ||
| 142 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 9027 times.
|
9040 | if (lapse >= perf_info.max_time) perf_info.max_time = lapse; |
| 143 |
4/4✓ Branch 0 taken 8966 times.
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 8960 times.
|
9040 | if (lapse <= perf_info.min_time || perf_info.min_time == 0) |
| 144 | 80 | perf_info.min_time = lapse; | |
| 145 | |||
| 146 | // Update total time | ||
| 147 | 9040 | 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 | 1 | void Stopwatch::report_all(int precision, std::ostream& output) { | |
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!active) return; |
| 180 | |||
| 181 | output << "\n*** PROFILING RESULTS [ms] (min - avg - max - lastTime - " | ||
| 182 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | "nSamples) ***\n"; |
| 183 | 1 | map<string, PerformanceData>::iterator it; | |
| 184 |
2/2✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
|
7 | for (it = records_of->begin(); it != records_of->end(); ++it) { |
| 185 |
2/4✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
|
6 | 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 | 6 | void Stopwatch::report(string perf_name, int precision, std::ostream& output) { | |
| 218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!active) return; |
| 219 | |||
| 220 | // Try to recover performance data | ||
| 221 |
3/6✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
|
6 | if (!performance_exists(perf_name)) |
| 222 | ✗ | throw StopwatchException("Performance not initialized."); | |
| 223 | |||
| 224 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | PerformanceData& perf_info = records_of->find(perf_name)->second; |
| 225 | |||
| 226 | 6 | const long unsigned int MAX_NAME_LENGTH = 60; | |
| 227 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | string pad = ""; |
| 228 |
2/2✓ Branch 1 taken 164 times.
✓ Branch 2 taken 6 times.
|
170 | for (long unsigned int i = perf_name.length(); i < MAX_NAME_LENGTH; i++) |
| 229 |
1/2✓ Branch 1 taken 164 times.
✗ Branch 2 not taken.
|
164 | pad.append(" "); |
| 230 | |||
| 231 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | output << perf_name << pad; |
| 232 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | output << std::fixed << std::setprecision(precision) |
| 233 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | << (perf_info.min_time * 1e3) << "\t"; |
| 234 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | output << std::fixed << std::setprecision(precision) |
| 235 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | << (perf_info.total_time * 1e3 / (long double)perf_info.stops) << "\t"; |
| 236 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | output << std::fixed << std::setprecision(precision) |
| 237 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | << (perf_info.max_time * 1e3) << "\t"; |
| 238 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | output << std::fixed << std::setprecision(precision) |
| 239 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
|
6 | << (perf_info.last_time * 1e3) << "\t"; |
| 240 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
|
6 | output << std::fixed << std::setprecision(precision) << perf_info.stops |
| 241 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | << std::endl; |
| 242 | |||
| 243 | // ostringstream stops; | ||
| 244 | // stops << perf_info.stops; | ||
| 245 | // output << " * Stops " << stops.str() << std::endl; | ||
| 246 | // output << std::endl; | ||
| 247 | 6 | } | |
| 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 | } | ||
| 311 |