forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClock.cpp
165 lines (158 loc) · 5.75 KB
/
Clock.cpp
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
//===--- Clock.cpp - Time and clock resolution ----------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Runtime/Concurrency.h"
#include "swift/Runtime/Once.h"
#include <time.h>
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <realtimeapiset.h>
#endif
#include "Error.h"
using namespace swift;
SWIFT_EXPORT_FROM(swift_Concurrency)
SWIFT_CC(swift)
void swift_get_time(
long long *seconds,
long long *nanoseconds,
swift_clock_id clock_id) {
switch (clock_id) {
case swift_clock_id_continuous: {
struct timespec continuous;
#if defined(__linux__)
clock_gettime(CLOCK_BOOTTIME, &continuous);
#elif defined(__APPLE__)
clock_gettime(CLOCK_MONOTONIC_RAW, &continuous);
#elif (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__wasi__))
clock_gettime(CLOCK_MONOTONIC, &continuous);
#elif defined(_WIN32)
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
// Divide count (number of ticks) by frequency (number of ticks per
// second) to get the counter in seconds. We also need to multiply the
// count by 1,000,000,000 to get nanosecond resolution. By multiplying
// first, we maintain high precision. The resulting value is the tick
// count in nanoseconds. Use 128-bit math to avoid overflowing.
auto quadPart = static_cast<unsigned _BitInt(128)>(count.QuadPart);
auto ns = (quadPart * 1'000'000'000) / freq.QuadPart;
continuous.tv_sec = ns / 1'000'000'000;
continuous.tv_nsec = ns % 1'000'000'000;
#else
#error Missing platform continuous time definition
#endif
*seconds = continuous.tv_sec;
*nanoseconds = continuous.tv_nsec;
return;
}
case swift_clock_id_suspending: {
struct timespec suspending;
#if defined(__linux__)
clock_gettime(CLOCK_MONOTONIC, &suspending);
#elif defined(__APPLE__)
clock_gettime(CLOCK_UPTIME_RAW, &suspending);
#elif defined(__wasi__)
clock_gettime(CLOCK_MONOTONIC, &suspending);
#elif (defined(__OpenBSD__) || defined(__FreeBSD__))
clock_gettime(CLOCK_UPTIME, &suspending);
#elif defined(_WIN32)
// QueryUnbiasedInterruptTimePrecise() was added in Windows 10 and is, as
// the name suggests, more precise than QueryUnbiasedInterruptTime().
// Unfortunately, the symbol is not listed in any .lib file in the SDK and
// must be looked up dynamically at runtime even if our minimum deployment
// target is Windows 10.
typedef decltype(QueryUnbiasedInterruptTimePrecise) *QueryUITP_FP;
static QueryUITP_FP queryUITP = nullptr;
static swift::once_t onceToken;
swift::once(onceToken, [] {
if (HMODULE hKernelBase = GetModuleHandleW(L"KernelBase.dll")) {
queryUITP = reinterpret_cast<QueryUITP_FP>(
GetProcAddress(hKernelBase, "QueryUnbiasedInterruptTimePrecise")
);
}
});
// Call whichever API is available. Both output a value measured in 100ns
// units. We must divide the output by 10,000,000 to get a value in
// seconds and multiply the remainder by 100 to get nanoseconds.
ULONGLONG unbiasedTime;
if (queryUITP) {
(* queryUITP)(&unbiasedTime);
} else {
// Fall back to the older, less precise API.
(void)QueryUnbiasedInterruptTime(&unbiasedTime);
}
suspending.tv_sec = unbiasedTime / 10'000'000;
suspending.tv_nsec = (unbiasedTime % 10'000'000) * 100;
#else
#error Missing platform suspending time definition
#endif
*seconds = suspending.tv_sec;
*nanoseconds = suspending.tv_nsec;
return;
}
}
swift_Concurrency_fatalError(0, "Fatal error: invalid clock ID %d\n",
clock_id);
}
SWIFT_EXPORT_FROM(swift_Concurrency)
SWIFT_CC(swift)
void swift_get_clock_res(
long long *seconds,
long long *nanoseconds,
swift_clock_id clock_id) {
switch (clock_id) {
case swift_clock_id_continuous: {
struct timespec continuous;
#if defined(__linux__)
clock_getres(CLOCK_BOOTTIME, &continuous);
#elif defined(__APPLE__)
clock_getres(CLOCK_MONOTONIC_RAW, &continuous);
#elif (defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__wasi__))
clock_getres(CLOCK_MONOTONIC, &continuous);
#elif defined(_WIN32)
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
continuous.tv_sec = 0;
continuous.tv_nsec = 1'000'000'000 / freq.QuadPart;
#else
#error Missing platform continuous time definition
#endif
*seconds = continuous.tv_sec;
*nanoseconds = continuous.tv_nsec;
return;
}
case swift_clock_id_suspending: {
struct timespec suspending;
#if defined(__linux__)
clock_getres(CLOCK_MONOTONIC_RAW, &suspending);
#elif defined(__APPLE__)
clock_getres(CLOCK_UPTIME_RAW, &suspending);
#elif defined(__wasi__)
clock_getres(CLOCK_MONOTONIC, &suspending);
#elif (defined(__OpenBSD__) || defined(__FreeBSD__))
clock_getres(CLOCK_UPTIME, &suspending);
#elif defined(_WIN32)
suspending.tv_sec = 0;
suspending.tv_nsec = 100;
#else
#error Missing platform suspending time definition
#endif
*seconds = suspending.tv_sec;
*nanoseconds = suspending.tv_nsec;
return;
}
}
swift_Concurrency_fatalError(0, "Fatal error: invalid clock ID %d\n",
clock_id);
}