1 | /* |
2 | * Copyright 2016-present 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/ClockGettimeWrappers.h> |
18 | #include <folly/Likely.h> |
19 | #include <folly/portability/Time.h> |
20 | |
21 | #include <chrono> |
22 | |
23 | #include <time.h> |
24 | |
25 | #ifndef _WIN32 |
26 | #define _GNU_SOURCE 1 |
27 | #include <dlfcn.h> |
28 | #endif |
29 | |
30 | namespace folly { |
31 | namespace chrono { |
32 | |
33 | static int64_t clock_gettime_ns_fallback(clockid_t clock) { |
34 | struct timespec ts; |
35 | int r = clock_gettime(clock, &ts); |
36 | if (UNLIKELY(r != 0)) { |
37 | // Mimic what __clock_gettime_ns does (even though this can be a legit |
38 | // value). |
39 | return -1; |
40 | } |
41 | std::chrono::nanoseconds result = |
42 | std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec); |
43 | return result.count(); |
44 | } |
45 | |
46 | // Initialize with default behavior, which we might override on Linux hosts |
47 | // with VDSO support. |
48 | int (*clock_gettime)(clockid_t, timespec* ts) = &::clock_gettime; |
49 | int64_t (*clock_gettime_ns)(clockid_t) = &clock_gettime_ns_fallback; |
50 | |
51 | #ifdef FOLLY_HAVE_LINUX_VDSO |
52 | |
53 | namespace { |
54 | |
55 | struct VdsoInitializer { |
56 | VdsoInitializer() { |
57 | m_handle = dlopen("linux-vdso.so.1" , RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD); |
58 | if (!m_handle) { |
59 | return; |
60 | } |
61 | |
62 | void* p = dlsym(m_handle, "__vdso_clock_gettime" ); |
63 | if (p) { |
64 | folly::chrono::clock_gettime = (int (*)(clockid_t, timespec*))p; |
65 | } |
66 | p = dlsym(m_handle, "__vdso_clock_gettime_ns" ); |
67 | if (p) { |
68 | folly::chrono::clock_gettime_ns = (int64_t(*)(clockid_t))p; |
69 | } |
70 | } |
71 | |
72 | ~VdsoInitializer() { |
73 | if (m_handle) { |
74 | clock_gettime = &::clock_gettime; |
75 | clock_gettime_ns = &clock_gettime_ns_fallback; |
76 | dlclose(m_handle); |
77 | } |
78 | } |
79 | |
80 | private: |
81 | void* m_handle; |
82 | }; |
83 | |
84 | static const VdsoInitializer vdso_initializer; |
85 | } // namespace |
86 | |
87 | #endif |
88 | } // namespace chrono |
89 | } // namespace folly |
90 | |