| 1 |
#include "stopwatch.h" |
| 2 |
#ifdef _WIN32 |
| 3 |
LARGE_INTEGER getFILETIMEoffset(void); |
| 4 |
|
| 5 |
LARGE_INTEGER getFILETIMEoffset() { |
| 6 |
SYSTEMTIME s; |
| 7 |
FILETIME f; |
| 8 |
LARGE_INTEGER t; |
| 9 |
|
| 10 |
s.wYear = 1970; |
| 11 |
s.wMonth = 1; |
| 12 |
s.wDay = 1; |
| 13 |
s.wHour = 0; |
| 14 |
s.wMinute = 0; |
| 15 |
s.wSecond = 0; |
| 16 |
s.wMilliseconds = 0; |
| 17 |
SystemTimeToFileTime(&s, &f); |
| 18 |
t.QuadPart = f.dwHighDateTime; |
| 19 |
t.QuadPart <<= 32; |
| 20 |
t.QuadPart |= f.dwLowDateTime; |
| 21 |
return (t); |
| 22 |
} |
| 23 |
|
| 24 |
void time_now(struct timespec *tv) { |
| 25 |
LARGE_INTEGER t; |
| 26 |
FILETIME f; |
| 27 |
double microseconds; |
| 28 |
static LARGE_INTEGER offset; |
| 29 |
static double frequencyToMicroseconds; |
| 30 |
static int initialized = 0; |
| 31 |
static BOOL usePerformanceCounter = 0; |
| 32 |
|
| 33 |
if (!initialized) { |
| 34 |
LARGE_INTEGER performanceFrequency; |
| 35 |
initialized = 1; |
| 36 |
usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency); |
| 37 |
if (usePerformanceCounter) { |
| 38 |
QueryPerformanceCounter(&offset); |
| 39 |
frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.0; |
| 40 |
} else { |
| 41 |
offset = getFILETIMEoffset(); |
| 42 |
frequencyToMicroseconds = 10.0; |
| 43 |
} |
| 44 |
} |
| 45 |
if (usePerformanceCounter) |
| 46 |
QueryPerformanceCounter(&t); |
| 47 |
else { |
| 48 |
GetSystemTimeAsFileTime(&f); |
| 49 |
t.QuadPart = f.dwHighDateTime; |
| 50 |
t.QuadPart <<= 32; |
| 51 |
t.QuadPart |= f.dwLowDateTime; |
| 52 |
} |
| 53 |
|
| 54 |
t.QuadPart -= offset.QuadPart; |
| 55 |
microseconds = (double)t.QuadPart / frequencyToMicroseconds; |
| 56 |
t.QuadPart = microseconds; |
| 57 |
tv->tv_sec = t.QuadPart / 1000000; |
| 58 |
tv->tv_nsec = t.QuadPart % 1000000 * 1000; |
| 59 |
} |
| 60 |
#elif __MACH__ |
| 61 |
#define ORWL_NANO (+1.0E-9) |
| 62 |
#define ORWL_GIGA UINT64_C(1000000000) |
| 63 |
|
| 64 |
static double orwl_timebase = 0.0; |
| 65 |
static uint64_t orwl_timestart = 0; |
| 66 |
|
| 67 |
void time_now(struct timespec *tv) { |
| 68 |
// be more careful in a multithreaded environement |
| 69 |
if (!orwl_timestart) { |
| 70 |
mach_timebase_info_data_t tb = {0}; |
| 71 |
mach_timebase_info(&tb); |
| 72 |
orwl_timebase = tb.numer; |
| 73 |
orwl_timebase /= tb.denom; |
| 74 |
orwl_timestart = mach_absolute_time(); |
| 75 |
} |
| 76 |
double diff = (mach_absolute_time() - orwl_timestart) * orwl_timebase; |
| 77 |
tv->tv_sec = diff * ORWL_NANO; |
| 78 |
tv->tv_nsec = diff - (tv->tv_sec * ORWL_GIGA); |
| 79 |
} |
| 80 |
#else |
| 81 |
void time_now(struct timespec *tv) { |
| 82 |
clock_gettime(CLOCK_REALTIME, tv); |
| 83 |
} |
| 84 |
#endif |
| 85 |
|
| 86 |
#define BILLION 1000000000 |
| 87 |
|
| 88 |
static void timespec_diff(struct timespec *start, struct timespec *stop, struct timespec *result) { |
| 89 |
if ((stop->tv_nsec - start->tv_nsec) < 0) { |
| 90 |
result->tv_sec = stop->tv_sec - start->tv_sec - 1; |
| 91 |
result->tv_nsec = stop->tv_nsec - start->tv_nsec + BILLION; |
| 92 |
} else { |
| 93 |
result->tv_sec = stop->tv_sec - start->tv_sec; |
| 94 |
result->tv_nsec = stop->tv_nsec - start->tv_nsec; |
| 95 |
} |
| 96 |
|
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
sds push_stopwatch(struct timespec *sofar) { |
| 101 |
struct timespec ends; |
| 102 |
time_now(&ends); |
| 103 |
struct timespec diff; |
| 104 |
timespec_diff(sofar, &ends, &diff); |
| 105 |
*sofar = ends; |
| 106 |
sds log = sdscatprintf(sdsempty(), "Step time = %gs.\n", diff.tv_sec + diff.tv_nsec / (double)BILLION); |
| 107 |
return log; |
| 108 |
} |