Debian Codegraph @

contrib/time-shim.c

raw symbols SCIP
1 /* librist. Copyright © 2019 SipRadius LLC. All right reserved.
2 * Author: Antonio Cardce <[email protected]>
3 * Author: Kuldeep Singh Dhaka <[email protected]>
4 * Author: Sergio Ammirata, Ph.D. <[email protected]>
5 * Author: Marvin Scholz <epirat07 at gmail dot com>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10 #include "time-shim.h"
11
12 #if defined(_WIN32) && !HAVE_CLOCK_GETTIME
13 #define _WINSOCKAPI_
14 # include <windows.h>
15 # include <errno.h>
16 # include <stdlib.h>
17 # include <limits.h>
18 # include <stdint.h>
19 #include "common/attributes.h"
20
21
22 //(un)graciously taken & modified from mingw-w64/misc/gettimeofday.c
23 /**
24 * This file has no copyright assigned and is placed in the Public Domain.
25 * This file is part of the mingw-w64 runtime package.
26 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
27 */
28
29
30 #define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */
31 #define HECTONANOSEC_PER_SEC 10000000ull
32
33 int gettimeofday(struct timeval *tv, void * not_implemented)
34 {
35 RIST_MARK_UNUSED(not_implemented);
36
37 union {
38 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
39 FILETIME ft;
40 } _now;
41
42 if (tv != NULL)
43 {
44 struct timespec tp;
45 GetSystemTimeAsFileTime (&_now.ft); /* 100-nanoseconds since 1-1-1601 */
46 /* The actual accuracy on XP seems to be 125,000 nanoseconds = 125 microseconds = 0.125 milliseconds */
47 _now.ns100 -= FILETIME_1970; /* 100 nano-seconds since 1-1-1970 */
48 tp.tv_sec = _now.ns100 / HECTONANOSEC_PER_SEC; /* seconds since 1-1-1970 */
49 tp.tv_nsec = (long) (_now.ns100 % HECTONANOSEC_PER_SEC) * 100; /* nanoseconds */
50 tv->tv_sec = tp.tv_sec;
51 tv->tv_usec = tp.tv_nsec/1000;
52 }
53 return 0;
54 }
55
56
57 //(un)graciously copied from mingw-w64/winpthreads/src/clock.c
58 /**
59 * This file has no copyright assigned and is placed in the Public Domain.
60 * This file is part of the w64 mingw-runtime package.
61 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
62 */
63
64
65 #define POW10_7 10000000
66 #define POW10_9 1000000000
67
68
69 /* Number of 100ns-seconds between the beginning of the Windows epoch
70 * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970)
71 */
72 #define DELTA_EPOCH_IN_100NS INT64_C(116444736000000000)
73
74 static inline int lc_set_errno(int result)
75 {
76 if (result != 0) {
77 errno = result;
78 return -1;
79 }
80 return 0;
81 }
82
83 int clock_gettime(clockid_t clock_id, struct timespec *tp)
84 {
85 uint64_t t;
86 LARGE_INTEGER pf, pc;
87 union {
88 uint64_t u64;
89 FILETIME ft;
90 } ct, et, kt, ut;
91
92 switch(clock_id) {
93 case CLOCK_REALTIME:
94 {
95 GetSystemTimeAsFileTime(&ct.ft);
96 t = ct.u64 - DELTA_EPOCH_IN_100NS;
97 tp->tv_sec = t / POW10_7;
98 tp->tv_nsec = ((int) (t % POW10_7)) * 100;
99
100 return 0;
101 }
102
103 case CLOCK_MONOTONIC:
104 {
105 if (QueryPerformanceFrequency(&pf) == 0)
106 return lc_set_errno(EINVAL);
107
108 if (QueryPerformanceCounter(&pc) == 0)
109 return lc_set_errno(EINVAL);
110
111 tp->tv_sec = pc.QuadPart / pf.QuadPart;
112 tp->tv_nsec = (int) (((pc.QuadPart % pf.QuadPart) * POW10_9 + (pf.QuadPart >> 1)) / pf.QuadPart);
113 if (tp->tv_nsec >= POW10_9) {
114 tp->tv_sec ++;
115 tp->tv_nsec -= POW10_9;
116 }
117
118 return 0;
119 }
120
121 case CLOCK_PROCESS_CPUTIME_ID:
122 {
123 if(0 == GetProcessTimes(GetCurrentProcess(), &ct.ft, &et.ft, &kt.ft, &ut.ft))
124 return lc_set_errno(EINVAL);
125 t = kt.u64 + ut.u64;
126 tp->tv_sec = t / POW10_7;
127 tp->tv_nsec = ((int) (t % POW10_7)) * 100;
128
129 return 0;
130 }
131
132 case CLOCK_THREAD_CPUTIME_ID:
133 {
134 if(0 == GetThreadTimes(GetCurrentThread(), &ct.ft, &et.ft, &kt.ft, &ut.ft))
135 return lc_set_errno(EINVAL);
136 t = kt.u64 + ut.u64;
137 tp->tv_sec = t / POW10_7;
138 tp->tv_nsec = ((int) (t % POW10_7)) * 100;
139
140 return 0;
141 }
142
143 default:
144 break;
145 }
146
147 return lc_set_errno(EINVAL);
148 }
149
150 #endif
151
152 #ifdef __APPLE__
153 #include <sys/errno.h>
154 #include <sys/types.h>
155 #include <sys/time.h>
156 #include <sys/sysctl.h>
157
158 /**
159 * Get the absolute time at which the system was booted
160 *
161 * This time is changed whenever the clock is adjusted to
162 * correctly reflect the boot time with the adjusted clock,
163 * so just querying it once and reusing the value is not safe.
164 *
165 * \param[out] tv Timeval struct to write the boottime to
166 *
167 * \note
168 * The boot time only has microsecond precision
169 *
170 * \return 0 on success, else -1 and errno set
171 */
172 static int rist__get_system_boottime(struct timeval *tv)
173 {
174 /* sysctl() sets errno on failure and returns -1. Honour that contract
175 * instead of returning the errno value as the result, so callers (and
176 * in turn clock_gettime_osx) can follow the standard "-1 + errno set"
177 * convention documented above. */
178 return sysctl((int[]){ CTL_KERN, KERN_BOOTTIME }, 2,
179 tv, &(size_t){ sizeof(*tv) }, NULL, 0);
180 }
181
182 /**
183 * Get the monotonic time (CLOCK_MONOTONIC)
184 *
185 * Calculates a monotically incrasing time since system boot
186 * that continues to increment when the system is asleep.
187 *
188 * Warnings to everyone trying to simplify this:
189 * - Using mach_continuous_time is not equivalent to this, see
190 * the darwin manpage about CLOCK_MONOTONIC_RAW for an explanation.
191 * - Using mach_absolute_time is not equivalent to this, as it does
192 * not continue to increment when the system is asleep.
193 * - The boot time is queried twice in a loop and only used if both
194 * match. This is done to detect if the boot time change since we
195 * obtained the current time of day, as the boot time can change
196 * when the system wallclock is adjusted, as that will adjust the
197 * boot time accordingly.
198 *
199 * \param[out] tv Timeval struct to write the monotonic time to
200 *
201 * \return 0 on success, else -1 and errno set
202 */
203 static int rist__get_monotonic(struct timeval *tv)
204 {
205 int ret;
206 struct timeval currenttime;
207 struct timeval boottime_begin;
208 struct timeval boottime_end;
209
210 do {
211 ret = rist__get_system_boottime(&boottime_begin);
212 if (ret != 0)
213 return ret;
214
215 ret = gettimeofday(&currenttime, NULL);
216 if (ret != 0)
217 return ret;
218
219 ret = rist__get_system_boottime(&boottime_end);
220 if (ret != 0)
221 return ret;
222 } while (timercmp(&boottime_begin, &boottime_end, !=));
223
224 timersub(&currenttime, &boottime_begin, tv);
225 return 0;
226 }
227
228 #if HAVE_CLOCK_GETTIME
229 int clock_gettime_osx(clock_id_t clock_id, timespec_t *tp)
230 #else
231 int clock_gettime_osx(clockid_t_osx clock_id, timespec_t *tp)
232 #endif
233 {
234 int ret = 0;
235 struct timeval tv;
236
237 switch (clock_id) {
238 case CLOCK_MONOTONIC_OSX:
239 ret = rist__get_monotonic(&tv);
240 break;
241 case CLOCK_REALTIME_OSX:
242 ret = gettimeofday(&tv, NULL);
243 break;
244 default:
245 errno = EINVAL;
246 return -1;
247 }
248
249 if (ret == 0)
250 TIMEVAL_TO_TIMESPEC(&tv, tp);
251 return ret;
252 }
253 #endif