a9160356f33fb7983b1dc76e8cb48a513a115de1
[folly.git] / folly / portability / Time.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/portability/Time.h>
18
19 #if !FOLLY_HAVE_CLOCK_GETTIME
20 #if __MACH__
21 #include <errno.h>
22 #include <mach/mach_time.h>
23
24 static const mach_timebase_info_data_t* tbInfo() {
25   static auto info = [] {
26     static mach_timebase_info_data_t info;
27     return (mach_timebase_info(&info) == KERN_SUCCESS) ? &info : nullptr;
28   }();
29   return info;
30 }
31
32 int clock_gettime(clockid_t clk_id, struct timespec* ts) {
33   auto tb_info = tbInfo();
34   if (tb_info == nullptr) {
35     errno = EINVAL;
36     return -1;
37   }
38
39   uint64_t now_ticks = mach_absolute_time();
40   uint64_t now_ns = (now_ticks * tb_info->numer) / tb_info->denom;
41   ts->tv_sec = now_ns / 1000000000;
42   ts->tv_nsec = now_ns % 1000000000;
43
44   return 0;
45 }
46
47 int clock_getres(clockid_t clk_id, struct timespec* ts) {
48   auto tb_info = tbInfo();
49   if (tb_info == nullptr) {
50     errno = EINVAL;
51     return -1;
52   }
53
54   ts->tv_sec = 0;
55   ts->tv_nsec = tb_info->numer / tb_info->denom;
56
57   return 0;
58 }
59 #elif defined(_WIN32)
60 #include <errno.h>
61 #include <locale.h>
62 #include <stdint.h>
63 #include <stdlib.h>
64
65 #include <folly/portability/Windows.h>
66
67 static constexpr size_t kNsPerSec = 1000000000;
68
69 extern "C" int clock_getres(clockid_t clock_id, struct timespec* res) {
70   if (!res) {
71     errno = EFAULT;
72     return -1;
73   }
74
75   switch (clock_id) {
76     case CLOCK_MONOTONIC: {
77       LARGE_INTEGER freq;
78       if (!QueryPerformanceFrequency(&freq)) {
79         errno = EINVAL;
80         return -1;
81       }
82
83       res->tv_sec = 0;
84       res->tv_nsec = (long)((kNsPerSec + (freq.QuadPart >> 1)) / freq.QuadPart);
85       if (res->tv_nsec < 1) {
86         res->tv_nsec = 1;
87       }
88
89       return 0;
90     }
91
92     case CLOCK_REALTIME:
93     case CLOCK_PROCESS_CPUTIME_ID:
94     case CLOCK_THREAD_CPUTIME_ID: {
95       DWORD adj, timeIncrement;
96       BOOL adjDisabled;
97       if (!GetSystemTimeAdjustment(&adj, &timeIncrement, &adjDisabled)) {
98         errno = EINVAL;
99         return -1;
100       }
101
102       res->tv_sec = 0;
103       res->tv_nsec = timeIncrement * 100;
104       return 0;
105     }
106
107     default:
108       errno = EINVAL;
109       return -1;
110   }
111 }
112
113 extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
114   if (!tp) {
115     errno = EFAULT;
116     return -1;
117   }
118
119   const auto ftToUint = [](FILETIME ft) -> uint64_t {
120     ULARGE_INTEGER i;
121     i.HighPart = ft.dwHighDateTime;
122     i.LowPart = ft.dwLowDateTime;
123     return i.QuadPart;
124   };
125   const auto timeToTimespec = [](timespec* tp, uint64_t t) -> int {
126     constexpr size_t k100NsPerSec = kNsPerSec / 100;
127
128     // The filetimes t is based on are represented in
129     // 100ns's. (ie. a value of 4 is 400ns)
130     tp->tv_sec = t / k100NsPerSec;
131     tp->tv_nsec = ((long)(t % k100NsPerSec)) * 100;
132     return 0;
133   };
134
135   FILETIME createTime, exitTime, kernalTime, userTime;
136   switch (clock_id) {
137     case CLOCK_REALTIME: {
138       constexpr size_t kDeltaEpochIn100NS = 116444736000000000ULL;
139
140       GetSystemTimeAsFileTime(&createTime);
141       return timeToTimespec(tp, ftToUint(createTime) - kDeltaEpochIn100NS);
142     }
143     case CLOCK_PROCESS_CPUTIME_ID: {
144       if (!GetProcessTimes(
145               GetCurrentProcess(),
146               &createTime,
147               &exitTime,
148               &kernalTime,
149               &userTime)) {
150         errno = EINVAL;
151         return -1;
152       }
153
154       return timeToTimespec(tp, ftToUint(kernalTime) + ftToUint(userTime));
155     }
156     case CLOCK_THREAD_CPUTIME_ID: {
157       if (!GetThreadTimes(
158               GetCurrentThread(),
159               &createTime,
160               &exitTime,
161               &kernalTime,
162               &userTime)) {
163         errno = EINVAL;
164         return -1;
165       }
166
167       return timeToTimespec(tp, ftToUint(kernalTime) + ftToUint(userTime));
168     }
169     case CLOCK_MONOTONIC: {
170       LARGE_INTEGER fl, cl;
171       if (!QueryPerformanceFrequency(&fl) || !QueryPerformanceCounter(&cl)) {
172         errno = EINVAL;
173         return -1;
174       }
175
176       int64_t freq = fl.QuadPart;
177       int64_t counter = cl.QuadPart;
178       tp->tv_sec = counter / freq;
179       tp->tv_nsec = (long)(((counter % freq) * kNsPerSec + (freq >> 1)) / freq);
180       if (tp->tv_nsec >= kNsPerSec) {
181         tp->tv_sec++;
182         tp->tv_nsec -= kNsPerSec;
183       }
184
185       return 0;
186     }
187
188     default:
189       errno = EINVAL;
190       return -1;
191   }
192 }
193 #else
194 #error No clock_gettime(3) compatibility wrapper available for this platform.
195 #endif
196 #endif
197
198 #ifdef _WIN32
199 #include <iomanip>
200 #include <sstream>
201
202 #include <folly/portability/Windows.h>
203
204 extern "C" {
205 char* asctime_r(const tm* tm, char* buf) {
206   char tmpBuf[64];
207   if (asctime_s(tmpBuf, tm)) {
208     return nullptr;
209   }
210   // Nothing we can do if the buff is to small :(
211   return strcpy(buf, tmpBuf);
212 }
213
214 char* ctime_r(const time_t* t, char* buf) {
215   char tmpBuf[64];
216   if (ctime_s(tmpBuf, 64, t)) {
217     return nullptr;
218   }
219   // Nothing we can do if the buff is to small :(
220   return strcpy(buf, tmpBuf);
221 }
222
223 tm* gmtime_r(const time_t* t, tm* res) {
224   if (!gmtime_s(res, t)) {
225     return res;
226   }
227   return nullptr;
228 }
229
230 tm* localtime_r(const time_t* t, tm* o) {
231   if (!localtime_s(o, t)) {
232     return o;
233   }
234   return nullptr;
235 }
236
237 int nanosleep(const struct timespec* request, struct timespec* remain) {
238   Sleep((DWORD)((request->tv_sec * 1000) + (request->tv_nsec / 1000000)));
239   remain->tv_nsec = 0;
240   remain->tv_sec = 0;
241   return 0;
242 }
243
244 char* strptime(const char* __restrict s,
245                const char* __restrict f,
246                struct tm* __restrict tm) {
247   // Isn't the C++ standard lib nice? std::get_time is defined such that its
248   // format parameters are the exact same as strptime. Of course, we have to
249   // create a string stream first, and imbue it with the current C locale, and
250   // we also have to make sure we return the right things if it fails, or
251   // if it succeeds, but this is still far simpler an implementation than any
252   // of the versions in any of the C standard libraries.
253   std::istringstream input(s);
254   input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
255   input >> std::get_time(tm, f);
256   if (input.fail()) {
257     return nullptr;
258   }
259   return const_cast<char*>(s + input.tellg());
260 }
261 }
262 #endif