1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 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 191 192
|
#include "clock.h"
#ifdef _WIN32
// ***********************
// ******** WIN32 ********
// ***********************
#include <windows.h>
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define U64(x) x##Ui64
#else
#define U64(x) x##ULL
#endif
#define DELTA_EPOCH_IN_100NS U64(116444736000000000)
long ticks_to_nanos(LONGLONG subsecond_time, LONGLONG frequency)
{
return (long)((1e9 * subsecond_time) / frequency);
}
ULONGLONG to_quad_100ns(FILETIME ft)
{
ULARGE_INTEGER li;
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
return li.QuadPart;
}
void to_timespec_from_100ns(ULONGLONG t_100ns, long *t)
{
t[0] = (long)(t_100ns / 10000000UL);
t[1] = 100*(long)(t_100ns % 10000000UL);
}
void clock_readtime_monotonic(long* t)
{
LARGE_INTEGER time;
LARGE_INTEGER frequency;
QueryPerformanceCounter(&time);
QueryPerformanceFrequency(&frequency);
// seconds
t[0] = time.QuadPart / frequency.QuadPart;
// nanos =
t[1] = ticks_to_nanos(time.QuadPart % frequency.QuadPart, frequency.QuadPart);
}
void clock_readtime_realtime(long* t)
{
FILETIME ft;
ULONGLONG tmp;
GetSystemTimeAsFileTime(&ft);
tmp = to_quad_100ns(ft);
tmp -= DELTA_EPOCH_IN_100NS;
to_timespec_from_100ns(tmp, t);
}
void clock_readtime_processtime(long* t)
{
FILETIME creation_time, exit_time, kernel_time, user_time;
ULONGLONG time;
GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time, &kernel_time, &user_time);
// Both kernel and user, acc. to http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_117
time = to_quad_100ns(user_time) + to_quad_100ns(kernel_time);
to_timespec_from_100ns(time, t);
}
void clock_readtime_threadtime(long* t)
{
FILETIME creation_time, exit_time, kernel_time, user_time;
ULONGLONG time;
GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time, &kernel_time, &user_time);
// Both kernel and user, acc. to http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_117
time = to_quad_100ns(user_time) + to_quad_100ns(kernel_time);
to_timespec_from_100ns(time, t);
}
void clock_readres_monotonic(long* t)
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
ULONGLONG resolution = U64(1000000000)/frequency.QuadPart;
t[0] = resolution / U64(1000000000);
t[1] = resolution % U64(1000000000);
}
void clock_readres_realtime(long* t)
{
t[0] = 0;
t[1] = 100;
}
void clock_readres_processtime(long* t)
{
t[0] = 0;
t[1] = 100;
}
void clock_readres_threadtime(long* t)
{
t[0] = 0;
t[1] = 100;
}
#else // Not _WIN32
// ***********************
// ******** POSIX ********
// ***********************
#include <time.h>
void time_(clockid_t clock, long* t)
{
clock_gettime(clock, (struct timespec*)t);
/* struct timespec a;
clock_gettime(clock, &a);
t[0] = a.tv_sec ;
t[1] = a.tv_nsec;*/
}
void clock_readtime_monotonic(long* t)
{
time_(CLOCK_MONOTONIC, t);
}
void clock_readtime_realtime(long* t)
{
time_(CLOCK_REALTIME, t);
}
void clock_readtime_processtime(long* t)
{
time_(CLOCK_PROCESS_CPUTIME_ID , t);
}
void clock_readtime_threadtime(long* t)
{
time_(CLOCK_THREAD_CPUTIME_ID , t);
}
void res_(clockid_t clock, long* t)
{
clock_getres(clock, (struct timespec*)t);
/* struct timespec a;
clock_getres(clock, &a);
t[0] = a.tv_sec ;
t[1] = a.tv_nsec;*/
}
void clock_readres_monotonic(long* t)
{
res_(CLOCK_MONOTONIC, t);
}
void clock_readres_realtime(long* t)
{
res_(CLOCK_REALTIME, t);
}
void clock_readres_processtime(long* t)
{
res_(CLOCK_PROCESS_CPUTIME_ID , t);
}
void clock_readres_threadtime(long* t)
{
res_(CLOCK_THREAD_CPUTIME_ID , t);
}
#endif
|