lib: keep timestamp in easy handle

Use `data->progress.now` as the timestamp of proecssing a transfer.
Update it on significant events and refrain from calling `curlx_now()`
in many places.

The problem this addresses is
a) calling curlx_now() has costs, depending on platform. Calling it
   every time results in 25% increase `./runtest` duration on macOS.
b) we used to pass a `struct curltime *` around to save on calls, but
   when some method directly use `curx_now()` and some use the passed
   pointer, the transfer experienes non-linear time. This results in
   timeline checks to report events in the wrong order.

By keeping a timestamp in the easy handle and updating it there, no
longer invoking `curlx_now()` in the "lower" methods, the transfer
can observer a steady clock progression.

Add documentation in docs/internals/TIME-KEEPING.md

Reported-by: Viktor Szakats
Fixes #19935
Closes #19961
This commit is contained in:
Stefan Eissing 2025-12-15 14:28:09 +01:00 committed by Daniel Stenberg
parent 425a2aa1af
commit 2de22a00c7
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
67 changed files with 598 additions and 458 deletions

View file

@ -253,7 +253,6 @@ static void dnscache_unlock(struct Curl_easy *data,
void Curl_dnscache_prune(struct Curl_easy *data)
{
struct Curl_dnscache *dnscache = dnscache_get(data);
struct curltime now;
/* the timeout may be set -1 (forever) */
timediff_t timeout_ms = data->set.dns_cache_timeout_ms;
@ -263,11 +262,10 @@ void Curl_dnscache_prune(struct Curl_easy *data)
dnscache_lock(data, dnscache);
now = curlx_now();
do {
/* Remove outdated and unused entries from the hostcache */
timediff_t oldest_ms = dnscache_prune(&dnscache->entries, timeout_ms, now);
timediff_t oldest_ms =
dnscache_prune(&dnscache->entries, timeout_ms, data->progress.now);
if(Curl_hash_count(&dnscache->entries) > MAX_DNS_CACHE_SIZE)
/* prune the ones over half this age */
@ -333,7 +331,7 @@ static struct Curl_dns_entry *fetch_addr(struct Curl_easy *data,
/* See whether the returned entry is stale. Done before we release lock */
struct dnscache_prune_data user;
user.now = curlx_now();
user.now = data->progress.now;
user.max_age_ms = data->set.dns_cache_timeout_ms;
user.oldest_ms = 0;
@ -522,7 +520,7 @@ Curl_dnscache_mk_entry(struct Curl_easy *data,
dns->timestamp.tv_usec = 0; /* an entry that never goes stale */
}
else {
dns->timestamp = curlx_now();
dns->timestamp = data->progress.now;
}
dns->hostport = port;
if(hostlen)
@ -1138,7 +1136,7 @@ clean_up:
the time we spent until now! */
if(prev_alarm) {
/* there was an alarm() set before us, now put it back */
timediff_t elapsed_secs = curlx_timediff_ms(curlx_now(),
timediff_t elapsed_secs = curlx_timediff_ms(data->progress.now,
data->conn->created) / 1000;
/* the alarm period is counted in even number of seconds */