master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curlx/timeval.h"
26#ifdef _WIN32
28static LARGE_INTEGER s_time_freq;
30/* For tool or tests, we must initialize before calling curlx_now().
31 Providing this function here is wrong. */
32void curlx_now_init(void)
33{
34 QueryPerformanceFrequency(&s_time_freq);
35}
37/* In case of bug fix this function has a counterpart in tool_util.c */
38void curlx_pnow(struct curltime *pnow)
39{
40 LARGE_INTEGER count;
41 DEBUGASSERT(s_time_freq.QuadPart);
42 QueryPerformanceCounter(&count);
43 pnow->tv_sec = (time_t)(count.QuadPart / s_time_freq.QuadPart);
44 pnow->tv_usec = (int)((count.QuadPart % s_time_freq.QuadPart) * 1000000 /
45 s_time_freq.QuadPart);
46}
48#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) || \
49 defined(HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
51void curlx_pnow(struct curltime *pnow)
52{
53 /*
54 * clock_gettime() is granted to be increased monotonically when the
55 * monotonic clock is queried. Time starting point is unspecified, it
56 * could be the system start-up time, the Epoch, or something else,
57 * in any case the time starting point does not change once that the
58 * system has started up.
59 */
60 struct timespec tsnow;
62 /*
63 * clock_gettime() may be defined by Apple's SDK as weak symbol thus
64 * code compiles but fails during runtime if clock_gettime() is
65 * called on unsupported OS version.
66 */
67#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
68 (HAVE_BUILTIN_AVAILABLE == 1)
69 bool have_clock_gettime = FALSE;
70 if(__builtin_available(macOS 10.12, iOS 10, tvOS 10, watchOS 3, *))
71 have_clock_gettime = TRUE;
72#endif
74#ifdef HAVE_CLOCK_GETTIME_MONOTONIC_RAW
75 if(
76#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
77 (HAVE_BUILTIN_AVAILABLE == 1)
78 have_clock_gettime &&
79#endif
80 (clock_gettime(CLOCK_MONOTONIC_RAW, &tsnow) == 0)) {
81 pnow->tv_sec = tsnow.tv_sec;
82 pnow->tv_usec = (int)(tsnow.tv_nsec / 1000);
83 }
84 else
85#endif
87 if(
88#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \
89 (HAVE_BUILTIN_AVAILABLE == 1)
90 have_clock_gettime &&
91#endif
92 (clock_gettime(CLOCK_MONOTONIC, &tsnow) == 0)) {
93 pnow->tv_sec = tsnow.tv_sec;
94 pnow->tv_usec = (int)(tsnow.tv_nsec / 1000);
95 }
96 /*
97 * Even when the configure process has truly detected monotonic clock
98 * availability, it might happen that it is not actually available at
99 * runtime. When this occurs, fallback to other time source.
100 */
101#ifdef HAVE_GETTIMEOFDAY
102 else {
103 struct timeval now;
104 (void)gettimeofday(&now, NULL);
105 pnow->tv_sec = now.tv_sec;
106 pnow->tv_usec = (int)now.tv_usec;
107 }
108#else
109 else {
110 pnow->tv_sec = time(NULL);
111 pnow->tv_usec = 0;
112 }
113#endif
114}
116#elif defined(HAVE_MACH_ABSOLUTE_TIME)
118#include <mach/mach_time.h>
120void curlx_pnow(struct curltime *pnow)
121{
122 /*
123 * Monotonic timer on macOS is provided by mach_absolute_time(), which
124 * returns time in Mach "absolute time units," which are platform-dependent.
125 * To convert to nanoseconds, one must use conversion factors specified by
126 * mach_timebase_info().
127 */
128 static mach_timebase_info_data_t timebase;
129 uint64_t usecs;
131 if(timebase.denom == 0)
132 (void)mach_timebase_info(&timebase);
134 usecs = mach_absolute_time();
135 usecs *= timebase.numer; /* spellchecker:disable-line */
136 usecs /= timebase.denom;
137 usecs /= 1000;
139 pnow->tv_sec = usecs / 1000000;
140 pnow->tv_usec = (int)(usecs % 1000000);
141}
143#elif defined(HAVE_GETTIMEOFDAY)
145void curlx_pnow(struct curltime *pnow)
146{
147 /*
148 * gettimeofday() is not granted to be increased monotonically, due to
149 * clock drifting and external source time synchronization it can jump
150 * forward or backward in time.
151 */
152 struct timeval now;
153 (void)gettimeofday(&now, NULL);
154 pnow->tv_sec = now.tv_sec;
155 pnow->tv_usec = (int)now.tv_usec;
156}
158#else
160void curlx_pnow(struct curltime *pnow)
161{
162 /*
163 * time() returns the value of time in seconds since the Epoch.
164 */
165 pnow->tv_sec = time(NULL);
166 pnow->tv_usec = 0;
167 if(!pnow->tv_sec) /* avoid a `now` fully zero */
168 pnow->tv_usec = 1;
169}
171#endif
173struct curltime curlx_now(void)
174{
175 struct curltime now;
176 curlx_pnow(&now);
177 return now;
178}
180/*
181 * Returns: time difference in number of milliseconds. For too large diffs it
182 * returns max value.
183 *
184 * @unittest: 1323
185 */
186timediff_t curlx_ptimediff_ms(const struct curltime *newer,
187 const struct curltime *older)
188{
189 timediff_t diff = (timediff_t)newer->tv_sec - older->tv_sec;
190 if(diff >= (TIMEDIFF_T_MAX / 1000))
191 return TIMEDIFF_T_MAX;
192 else if(diff <= (TIMEDIFF_T_MIN / 1000))
193 return TIMEDIFF_T_MIN;
194 return (diff * 1000) + ((newer->tv_usec - older->tv_usec) / 1000);
195}
197timediff_t curlx_timediff_ms(struct curltime newer, struct curltime older)
198{
199 return curlx_ptimediff_ms(&newer, &older);
200}
202/*
203 * Returns: time difference in number of milliseconds, rounded up.
204 * For too large diffs it returns max value.
205 */
206timediff_t curlx_timediff_ceil_ms(struct curltime newer,
207 struct curltime older)
208{
209 timediff_t diff = (timediff_t)newer.tv_sec - older.tv_sec;
210 if(diff >= (TIMEDIFF_T_MAX / 1000))
211 return TIMEDIFF_T_MAX;
212 else if(diff <= (TIMEDIFF_T_MIN / 1000))
213 return TIMEDIFF_T_MIN;
214 return (diff * 1000) + ((newer.tv_usec - older.tv_usec + 999) / 1000);
215}
217timediff_t curlx_us_to_ceil_ms(timediff_t us)
218{
219 return (us / 1000) + ((us > 0) && (us % 1000));
220}
222/*
223 * Returns: time difference in number of microseconds. For too large diffs it
224 * returns max value.
225 */
226timediff_t curlx_ptimediff_us(const struct curltime *newer,
227 const struct curltime *older)
228{
229 timediff_t diff = (timediff_t)newer->tv_sec - older->tv_sec;
230 if(diff >= (TIMEDIFF_T_MAX / 1000000))
231 return TIMEDIFF_T_MAX;
232 else if(diff <= (TIMEDIFF_T_MIN / 1000000))
233 return TIMEDIFF_T_MIN;
234 return (diff * 1000000) + newer->tv_usec - older->tv_usec;
235}
237timediff_t curlx_timediff_us(struct curltime newer, struct curltime older)
238{
239 return curlx_ptimediff_us(&newer, &older);
240}
242#if defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR <= 3)
243#include <sec_api/time_s.h> /* for _gmtime32_s(), _gmtime64_s() */
244#ifdef _USE_32BIT_TIME_T
245#define gmtime_s _gmtime32_s
246#else
247#define gmtime_s _gmtime64_s
248#endif
249#endif
251/*
252 * curlx_gmtime() is a gmtime() replacement for portability. Do not use
253 * the gmtime_s(), gmtime_r() or gmtime() functions anywhere else but here.
254 */
255CURLcode curlx_gmtime(time_t intime, struct tm *store)
256{
257#ifdef _WIN32
258 if(gmtime_s(store, &intime)) /* thread-safe */
259 return CURLE_BAD_FUNCTION_ARGUMENT;
260#elif defined(HAVE_GMTIME_R)
261 const struct tm *tm;
262 tm = gmtime_r(&intime, store); /* thread-safe */
263 if(!tm)
264 return CURLE_BAD_FUNCTION_ARGUMENT;
265#else
266 const struct tm *tm;
267 /* !checksrc! disable BANNEDFUNC 1 */
268 tm = gmtime(&intime); /* not thread-safe */
269 if(tm)
270 *store = *tm; /* copy the pointed struct to the local copy */
271 else
272 return CURLE_BAD_FUNCTION_ARGUMENT;
273#endif
275 return CURLE_OK;
276}
Diagnostics4 git calls · 343 ms
CallStatusSizeTime0343 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.8 KiB338 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=338 git.response.bytes=2845 git.response.cache=MISS git.response.duration_ms=338 git.fetch.wants=b43aedf15cc218eefca3865192cf794e59bd6a23 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2845 git.pkt.payload.max=2824 git.sideband.band1.bytes=2823 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e310838e60bdc9
- Request
- 1bcff3f4…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format