| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include <esl_config.h> |
| 11 |
|
| 12 |
#include "easel.h" |
| 13 |
#include "esl_stopwatch.h" |
| 14 |
|
| 15 |
static double stopwatch_getRealTime(void); |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
ESL_STOPWATCH * |
| 32 |
esl_stopwatch_Create(void) |
| 33 |
{ |
| 34 |
ESL_STOPWATCH *w = NULL; |
| 35 |
int status; |
| 36 |
|
| 37 |
ESL_ALLOC(w, sizeof(ESL_STOPWATCH)); |
| 38 |
w->elapsed = 0.; |
| 39 |
w->user = 0.; |
| 40 |
w->sys = 0.; |
| 41 |
return w; |
| 42 |
|
| 43 |
ERROR: |
| 44 |
esl_stopwatch_Destroy(w); |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
void |
| 53 |
esl_stopwatch_Destroy(ESL_STOPWATCH *w) |
| 54 |
{ |
| 55 |
if (w) |
| 56 |
free(w); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
int |
| 71 |
esl_stopwatch_Start(ESL_STOPWATCH *w) |
| 72 |
{ |
| 73 |
#if defined HAVE_TIMES && defined eslSTOPWATCH_HIGHRES |
| 74 |
times(&(w->cpu0)); |
| 75 |
w->t0 = stopwatch_getRealTime(); |
| 76 |
#elif HAVE_TIMES |
| 77 |
w->t0 = times(&(w->cpu0)); |
| 78 |
#else |
| 79 |
w->t0 = time(NULL); |
| 80 |
w->cpu0 = clock(); |
| 81 |
#endif |
| 82 |
|
| 83 |
w->elapsed = 0.; |
| 84 |
w->user = 0.; |
| 85 |
w->sys = 0.; |
| 86 |
return eslOK; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
int |
| 98 |
esl_stopwatch_Stop(ESL_STOPWATCH *w) |
| 99 |
{ |
| 100 |
#if defined eslSTOPWATCH_HIGHRES && defined HAVE_TIMES |
| 101 |
double t1; |
| 102 |
struct tms cpu1; |
| 103 |
double clk_tck; |
| 104 |
#elif defined HAVE_TIMES |
| 105 |
clock_t t1; |
| 106 |
struct tms cpu1; |
| 107 |
double clk_tck; |
| 108 |
#else |
| 109 |
time_t t1; |
| 110 |
clock_t cpu1; |
| 111 |
#endif |
| 112 |
|
| 113 |
|
| 114 |
#if defined eslSTOPWATCH_HIGHRES && defined HAVE_TIMES |
| 115 |
t1 = stopwatch_getRealTime(); |
| 116 |
w->elapsed = t1 - w->t0; |
| 117 |
clk_tck = (double) sysconf(_SC_CLK_TCK); |
| 118 |
times(&cpu1); |
| 119 |
w->user = (double) (cpu1.tms_utime + cpu1.tms_cutime - |
| 120 |
w->cpu0.tms_utime - w->cpu0.tms_cutime) / clk_tck; |
| 121 |
w->sys = (double) (cpu1.tms_stime + cpu1.tms_cstime - |
| 122 |
w->cpu0.tms_stime - w->cpu0.tms_cstime) / clk_tck; |
| 123 |
#elif defined HAVE_TIMES |
| 124 |
t1 = times(&cpu1); |
| 125 |
clk_tck = (double) sysconf(_SC_CLK_TCK); |
| 126 |
w->elapsed = (double) (t1 - w->t0) / clk_tck; |
| 127 |
w->user = (double) (cpu1.tms_utime + cpu1.tms_cutime - |
| 128 |
w->cpu0.tms_utime - w->cpu0.tms_cutime) / clk_tck; |
| 129 |
w->sys = (double) (cpu1.tms_stime + cpu1.tms_cstime - |
| 130 |
w->cpu0.tms_stime - w->cpu0.tms_cstime) / clk_tck; |
| 131 |
#else |
| 132 |
t1 = time(NULL); |
| 133 |
cpu1 = clock(); |
| 134 |
w->elapsed = difftime(t1, w->t0); |
| 135 |
w->user = (double) (cpu1- w->cpu0) / (double) CLOCKS_PER_SEC; |
| 136 |
w->sys = 0.; |
| 137 |
#endif |
| 138 |
|
| 139 |
return eslOK; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static void |
| 153 |
format_time_string(char *buf, double sec, int do_frac) |
| 154 |
{ |
| 155 |
int h, m, s, hs; |
| 156 |
|
| 157 |
h = (int) (sec / 3600.); |
| 158 |
m = (int) (sec / 60.) - h * 60; |
| 159 |
s = (int) (sec) - h * 3600 - m * 60; |
| 160 |
if (do_frac) { |
| 161 |
hs = (int) (sec * 100.) - h * 360000 - m * 6000 - s * 100; |
| 162 |
snprintf(buf, 128, "%02d:%02d:%02d.%02d", h,m,s,hs); |
| 163 |
} else { |
| 164 |
snprintf(buf, 128, "%02d:%02d:%02d", h,m,s); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
int |
| 191 |
esl_stopwatch_Display(FILE *fp, ESL_STOPWATCH *w, char *prefix) |
| 192 |
{ |
| 193 |
char buf[128]; |
| 194 |
|
| 195 |
if (prefix == NULL) { if (fputs("CPU Time: ", fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "stopwatch display write failed"); } |
| 196 |
else { if (fputs(prefix, fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "stopwatch display write failed"); } |
| 197 |
|
| 198 |
format_time_string(buf, w->user+w->sys, TRUE); |
| 199 |
#ifdef HAVE_TIMES |
| 200 |
if (fprintf(fp, "%.2fu %.2fs %s ", w->user, w->sys, buf) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "stopwatch display write failed"); |
| 201 |
#else |
| 202 |
if (fprintf(fp, "%.2fu %s ", w->user, buf) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "stopwatch display write failed"); |
| 203 |
#endif |
| 204 |
format_time_string(buf, w->elapsed, TRUE); |
| 205 |
if (fprintf(fp, "Elapsed: %s\n", buf) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "stopwatch display write failed"); |
| 206 |
return eslOK; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
double |
| 222 |
esl_stopwatch_GetElapsed(ESL_STOPWATCH *w) |
| 223 |
{ |
| 224 |
return w->elapsed; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
int |
| 252 |
esl_stopwatch_Include(ESL_STOPWATCH *master, ESL_STOPWATCH *w) |
| 253 |
{ |
| 254 |
master->user += w->user; |
| 255 |
master->sys += w->sys; |
| 256 |
return eslOK; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
#ifdef eslSTOPWATCH_HIGHRES |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
#if defined(_WIN32) |
| 280 |
#include <Windows.h> |
| 281 |
|
| 282 |
#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) |
| 283 |
#include <unistd.h> |
| 284 |
#include <time.h> |
| 285 |
#include <sys/time.h> |
| 286 |
|
| 287 |
#if defined(__MACH__) && defined(__APPLE__) |
| 288 |
#include <mach/mach.h> |
| 289 |
#include <mach/mach_time.h> |
| 290 |
#endif |
| 291 |
|
| 292 |
#else |
| 293 |
#error "Unable to define getRealTime( ) for an unknown OS." |
| 294 |
#endif |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
static double |
| 304 |
stopwatch_getRealTime(void) |
| 305 |
{ |
| 306 |
#if defined(_WIN32) |
| 307 |
FILETIME tm; |
| 308 |
ULONGLONG t; |
| 309 |
#if defined(NTDDI_WIN8) && NTDDI_VERSION >= NTDDI_WIN8 |
| 310 |
|
| 311 |
GetSystemTimePreciseAsFileTime( &tm ); |
| 312 |
#else |
| 313 |
|
| 314 |
GetSystemTimeAsFileTime( &tm ); |
| 315 |
#endif |
| 316 |
t = ((ULONGLONG)tm.dwHighDateTime << 32) | (ULONGLONG)tm.dwLowDateTime; |
| 317 |
return (double)t / 10000000.0; |
| 318 |
|
| 319 |
#elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__))) |
| 320 |
|
| 321 |
return (double)gethrtime( ) / 1000000000.0; |
| 322 |
|
| 323 |
#elif defined(__MACH__) && defined(__APPLE__) |
| 324 |
|
| 325 |
static double timeConvert = 0.0; |
| 326 |
if ( timeConvert == 0.0 ) |
| 327 |
{ |
| 328 |
mach_timebase_info_data_t timeBase; |
| 329 |
(void)mach_timebase_info( &timeBase ); |
| 330 |
timeConvert = (double)timeBase.numer / |
| 331 |
(double)timeBase.denom / |
| 332 |
1000000000.0; |
| 333 |
} |
| 334 |
return (double)mach_absolute_time( ) * timeConvert; |
| 335 |
|
| 336 |
#elif defined(_POSIX_VERSION) |
| 337 |
|
| 338 |
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) |
| 339 |
{ |
| 340 |
struct timespec ts; |
| 341 |
#if defined(CLOCK_MONOTONIC_PRECISE) |
| 342 |
|
| 343 |
const clockid_t id = CLOCK_MONOTONIC_PRECISE; |
| 344 |
#elif defined(CLOCK_MONOTONIC_RAW) |
| 345 |
|
| 346 |
const clockid_t id = CLOCK_MONOTONIC_RAW; |
| 347 |
#elif defined(CLOCK_HIGHRES) |
| 348 |
|
| 349 |
const clockid_t id = CLOCK_HIGHRES; |
| 350 |
#elif defined(CLOCK_MONOTONIC) |
| 351 |
|
| 352 |
const clockid_t id = CLOCK_MONOTONIC; |
| 353 |
#elif defined(CLOCK_REALTIME) |
| 354 |
|
| 355 |
const clockid_t id = CLOCK_REALTIME; |
| 356 |
#else |
| 357 |
const clockid_t id = (clockid_t)-1; |
| 358 |
#endif |
| 359 |
if ( id != (clockid_t)-1 && clock_gettime( id, &ts ) != -1 ) |
| 360 |
return (double)ts.tv_sec + |
| 361 |
(double)ts.tv_nsec / 1000000000.0; |
| 362 |
|
| 363 |
} |
| 364 |
#endif |
| 365 |
|
| 366 |
|
| 367 |
struct timeval tm; |
| 368 |
gettimeofday( &tm, NULL ); |
| 369 |
return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0; |
| 370 |
#else |
| 371 |
return -1.0; |
| 372 |
#endif |
| 373 |
} |
| 374 |
#endif |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
#ifdef eslSTOPWATCH_EXAMPLE |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
#include "easel.h" |
| 385 |
#include "esl_stopwatch.h" |
| 386 |
|
| 387 |
int |
| 388 |
main(void) |
| 389 |
{ |
| 390 |
ESL_STOPWATCH *w; |
| 391 |
double t = 0.; |
| 392 |
|
| 393 |
w = esl_stopwatch_Create(); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
esl_stopwatch_Start(w); |
| 400 |
while (t == 0.) |
| 401 |
{ |
| 402 |
esl_stopwatch_Stop(w); |
| 403 |
t = esl_stopwatch_GetElapsed(w); |
| 404 |
} |
| 405 |
|
| 406 |
printf("Elapsed time clock has practical resolution of around: %g sec\n", t); |
| 407 |
|
| 408 |
esl_stopwatch_Display(stdout, w, "CPU Time: "); |
| 409 |
esl_stopwatch_Destroy(w); |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
#endif |
| 414 |
|
| 415 |
|