20 #include <ndb_global.h>
23 #define NANOSEC_PER_SEC 1000000000
24 #define MICROSEC_PER_SEC 1000000
25 #define MILLISEC_PER_SEC 1000
26 #define MICROSEC_PER_MILLISEC 1000
27 #define NANOSEC_PER_MILLISEC 1000000
28 #define NANOSEC_PER_MICROSEC 1000
30 #ifdef HAVE_CLOCK_GETTIME
32 #ifdef CLOCK_MONOTONIC
33 static clockid_t NdbTick_clk_id = CLOCK_MONOTONIC;
35 static clockid_t NdbTick_clk_id = CLOCK_REALTIME;
38 void NdbTick_Init(
int need_monotonic)
43 NdbTick_clk_id = CLOCK_REALTIME;
45 if (clock_gettime(NdbTick_clk_id, &tick_time) == 0)
47 #ifdef CLOCK_MONOTONIC
48 fprintf(stderr,
"Failed to use CLOCK_MONOTONIC for clock_realtime,"
49 " errno= %u\n", errno);
51 NdbTick_clk_id = CLOCK_REALTIME;
52 if (clock_gettime(NdbTick_clk_id, &tick_time) == 0)
55 fprintf(stderr,
"Failed to use CLOCK_REALTIME for clock_realtime,"
56 " errno=%u. Aborting\n", errno);
61 NDB_TICKS NdbTick_CurrentMillisecond(
void)
64 clock_gettime(NdbTick_clk_id, &tick_time);
67 ((NDB_TICKS)tick_time.tv_sec) * ((NDB_TICKS)MILLISEC_PER_SEC) +
68 ((NDB_TICKS)tick_time.tv_nsec) / ((NDB_TICKS)NANOSEC_PER_MILLISEC);
72 NdbTick_CurrentMicrosecond(NDB_TICKS * secs, Uint32 * micros){
74 int res = clock_gettime(NdbTick_clk_id, &t);
76 * micros = t.tv_nsec / 1000;
80 NDB_TICKS NdbTick_CurrentNanosecond(
void)
83 clock_gettime(NdbTick_clk_id, &tick_time);
86 (((NDB_TICKS)tick_time.tv_sec) * ((NDB_TICKS)NANOSEC_PER_SEC)) +
87 ((NDB_TICKS)tick_time.tv_nsec);
90 void NdbTick_Init(
int need_monotonic)
94 NDB_TICKS NdbTick_CurrentMillisecond(
void)
99 NdbTick_CurrentMicrosecond(&secs, µs);
100 return secs*1000 + micros/1000;
103 gettimeofday(&tick_time, 0);
106 ((NDB_TICKS)tick_time.tv_sec) * ((NDB_TICKS)MILLISEC_PER_SEC) +
107 ((NDB_TICKS)tick_time.tv_usec) / ((NDB_TICKS)MICROSEC_PER_MILLISEC);
112 NdbTick_CurrentMicrosecond(NDB_TICKS * secs, Uint32 * micros)
115 ulonglong time, timemicro, micropart, secpart;
117 GetSystemTimeAsFileTime((FILETIME*)&time);
120 secpart = timemicro/1000000;
121 micropart = timemicro%1000000;
122 assert(micropart <= ULONG_MAX);
123 assert(secpart*1000000+micropart == timemicro);
125 *micros = (Uint32)micropart;
131 int res = gettimeofday(&tick_time, 0);
134 NDB_TICKS local_secs = tick_time.tv_sec;
135 *micros = tick_time.tv_usec;
136 *micros = local_secs*1000000+*micros;
138 * secs = tick_time.tv_sec;
139 * micros = tick_time.tv_usec;
145 NDB_TICKS NdbTick_CurrentNanosecond(
void)
150 NdbTick_CurrentMicrosecond(&secs, µs);
151 return secs*NANOSEC_PER_SEC + micros*NANOSEC_PER_MICROSEC;
154 gettimeofday(&tick_time, 0);
157 (((NDB_TICKS)tick_time.tv_sec) * ((NDB_TICKS)NANOSEC_PER_SEC)) +
158 (((NDB_TICKS)tick_time.tv_usec) * ((NDB_TICKS)NANOSEC_PER_MICROSEC));
170 ret_value = NdbTick_CurrentMicrosecond(&secs, &mics);
171 input_timer->seconds = secs;
172 input_timer->micro_seconds = (NDB_TICKS)mics;
180 NDB_TICKS ret_value = (NDB_TICKS)0;
181 if (start.seconds < stop.seconds) {
182 NDB_TICKS sec_passed = stop.seconds - start.seconds;
183 ret_value = ((NDB_TICKS)MICROSEC_PER_SEC) * sec_passed;
184 }
else if (start.seconds > stop.seconds) {
187 if (start.micro_seconds < stop.micro_seconds) {
188 ret_value += (stop.micro_seconds - start.micro_seconds);
189 }
else if (ret_value != (NDB_TICKS)0) {
190 ret_value -= (start.micro_seconds - stop.micro_seconds);