]> git.saurik.com Git - apple/libc.git/blame - gen/clock_gettime.c
Libc-1244.50.9.tar.gz
[apple/libc.git] / gen / clock_gettime.c
CommitLineData
974e3884
A
1/*
2 * Copyright (c) 2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include <errno.h>
25#include <stdatomic.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <stdint.h>
29#include <string.h>
30#include <time.h>
31#include <unistd.h>
32#include <sys/time.h>
33#include <sys/types.h>
34#include <sys/sysctl.h>
35#include <mach/mach_error.h>
36#include <mach/mach_time.h>
37#include <os/base_private.h>
38
39extern uint64_t __thread_selfusage(void);
40
41#define timeval2nsec(tv) (tv.tv_sec * NSEC_PER_SEC + tv.tv_usec * NSEC_PER_USEC)
42
43static uint64_t
44_boottime_fallback_usec(void)
45{
46 struct timeval tv;
47 size_t len = sizeof(tv);
48 int ret = sysctlbyname("kern.boottime", &tv, &len, NULL, 0);
49 if (ret == -1) return 0;
50 return (uint64_t)tv.tv_sec * USEC_PER_SEC + (uint64_t)tv.tv_usec;
51}
52
53static int
54_mach_boottime_usec(uint64_t *boottime, struct timeval *realtime)
55{
56 uint64_t bt1 = 0, bt2 = 0;
57 int ret;
58 do {
59 bt1 = mach_boottime_usec();
60 if (os_slowpath(bt1 == 0)) bt1 = _boottime_fallback_usec();
61
62 atomic_thread_fence(memory_order_seq_cst);
63
64 ret = gettimeofday(realtime, NULL);
65 if (ret != 0) return ret;
66
67 atomic_thread_fence(memory_order_seq_cst);
68
69 bt2 = mach_boottime_usec();
70 if (os_slowpath(bt2 == 0)) bt2 = _boottime_fallback_usec();
71 } while (os_slowpath(bt1 != bt2));
72 *boottime = bt1;
73 return 0;
74}
75
76uint64_t
77clock_gettime_nsec_np(clockid_t clock_id)
78{
79 switch(clock_id){
80 case CLOCK_REALTIME: {
81 struct timeval tv;
82 int ret = gettimeofday(&tv, NULL);
83 if (ret) return 0;
84 return timeval2nsec(tv);
85 }
86 case CLOCK_MONOTONIC: {
87 struct timeval tv;
88 uint64_t boottime;
89 int ret = _mach_boottime_usec(&boottime, &tv);
90 if (ret) return 0;
91 boottime *= NSEC_PER_USEC;
92 return timeval2nsec(tv) - boottime;
93 }
94 case CLOCK_PROCESS_CPUTIME_ID: {
95 struct rusage ru;
96 int ret = getrusage(RUSAGE_SELF, &ru);
97 if (ret) return 0;
98 return timeval2nsec(ru.ru_utime) + timeval2nsec(ru.ru_stime);
99 }
100 default:
101 // calls that use mach_absolute_time units fall through into a common path
102 break;
103 }
104
105 // Mach Absolute Time unit-based calls
106 mach_timebase_info_data_t tb_info;
107 if (mach_timebase_info(&tb_info)) return 0;
108 uint64_t mach_time;
109
110 switch(clock_id){
111 case CLOCK_MONOTONIC_RAW:
112 mach_time = mach_continuous_time();
113 break;
114 case CLOCK_MONOTONIC_RAW_APPROX:
115 mach_time = mach_continuous_approximate_time();
116 break;
117 case CLOCK_UPTIME_RAW:
118 mach_time = mach_absolute_time();
119 break;
120 case CLOCK_UPTIME_RAW_APPROX:
121 mach_time = mach_approximate_time();
122 break;
123 case CLOCK_THREAD_CPUTIME_ID:
124 mach_time = __thread_selfusage();
125 break;
126 default:
127 errno = EINVAL;
128 return 0;
129 }
130
131 return (mach_time * tb_info.numer) / tb_info.denom;
132}
133
134int
135clock_gettime(clockid_t clk_id, struct timespec *tp)
136{
137 switch(clk_id){
138 case CLOCK_REALTIME: {
139 struct timeval tv;
140 int ret = gettimeofday(&tv, NULL);
141 TIMEVAL_TO_TIMESPEC(&tv, tp);
142 return ret;
143 }
144 case CLOCK_MONOTONIC: {
145 struct timeval tv;
146 uint64_t boottime_usec;
147 int ret = _mach_boottime_usec(&boottime_usec, &tv);
148 struct timeval boottime = {
149 .tv_sec = boottime_usec / USEC_PER_SEC,
150 .tv_usec = boottime_usec % USEC_PER_SEC
151 };
152 timersub(&tv, &boottime, &tv);
153 TIMEVAL_TO_TIMESPEC(&tv, tp);
154 return ret;
155 }
156 case CLOCK_PROCESS_CPUTIME_ID: {
157 struct rusage ru;
158 int ret = getrusage(RUSAGE_SELF, &ru);
159 timeradd(&ru.ru_utime, &ru.ru_stime, &ru.ru_utime);
160 TIMEVAL_TO_TIMESPEC(&ru.ru_utime, tp);
161 return ret;
162 }
163 case CLOCK_MONOTONIC_RAW:
164 case CLOCK_MONOTONIC_RAW_APPROX:
165 case CLOCK_UPTIME_RAW:
166 case CLOCK_UPTIME_RAW_APPROX:
167 case CLOCK_THREAD_CPUTIME_ID: {
168 uint64_t ns = clock_gettime_nsec_np(clk_id);
169 if (!ns) return -1;
170
171 tp->tv_sec = ns/NSEC_PER_SEC;
172 tp->tv_nsec = ns % NSEC_PER_SEC;
173 return 0;
174 }
175 default:
176 errno = EINVAL;
177 return -1;
178 }
179}
180
181int
182clock_getres(clockid_t clk_id, struct timespec *res)
183{
184 switch(clk_id){
185 case CLOCK_REALTIME:
186 case CLOCK_MONOTONIC:
187 case CLOCK_PROCESS_CPUTIME_ID:
188 res->tv_nsec = NSEC_PER_USEC;
189 res->tv_sec = 0;
190 return 0;
191
192 case CLOCK_MONOTONIC_RAW:
193 case CLOCK_MONOTONIC_RAW_APPROX:
194 case CLOCK_UPTIME_RAW:
195 case CLOCK_UPTIME_RAW_APPROX:
196 case CLOCK_THREAD_CPUTIME_ID: {
197 mach_timebase_info_data_t tb_info;
198 if (mach_timebase_info(&tb_info)){
199 return -1;
200 }
201 res->tv_nsec = tb_info.numer / tb_info.denom + (tb_info.numer % tb_info.denom != 0);
202 res->tv_sec = 0;
203 return 0;
204 }
205
206 default:
207 errno = EINVAL;
208 return -1;
209 }
210}
211
212int
213clock_settime(clockid_t clk_id, const struct timespec *tp)
214{
215 switch(clk_id){
216 case CLOCK_REALTIME: {
b061a43b
A
217 struct timeval tv = {
218 .tv_sec = (time_t)tp->tv_sec,
219 .tv_usec = (suseconds_t)(tp->tv_nsec / NSEC_PER_USEC)
220 };
974e3884
A
221 return settimeofday(&tv, NULL);
222 }
223 default:
224 errno = EINVAL;
225 return -1;
226 }
227}