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

@ -261,7 +261,7 @@ static CURLcode sendrecv_dl(struct Curl_easy *data,
if(bytestoread && Curl_rlimit_active(&data->progress.dl.rlimit)) {
curl_off_t dl_avail = Curl_rlimit_avail(&data->progress.dl.rlimit,
curlx_now());
&data->progress.now);
/* DEBUGF(infof(data, "dl_rlimit, available=%" FMT_OFF_T, dl_avail));
*/
/* In case of rate limited downloads: if this loop already got
@ -364,12 +364,11 @@ static CURLcode sendrecv_ul(struct Curl_easy *data)
* Curl_sendrecv() is the low-level function to be called when data is to
* be read and written to/from the connection.
*/
CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
CURLcode Curl_sendrecv(struct Curl_easy *data)
{
struct SingleRequest *k = &data->req;
CURLcode result = CURLE_OK;
DEBUGASSERT(nowp);
if(Curl_xfer_is_blocked(data)) {
result = CURLE_OK;
goto out;
@ -395,18 +394,20 @@ CURLcode Curl_sendrecv(struct Curl_easy *data, struct curltime *nowp)
goto out;
if(k->keepon) {
if(Curl_timeleft_ms(data, nowp, FALSE) < 0) {
if(Curl_timeleft_ms(data, FALSE) < 0) {
if(k->size != -1) {
failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
" milliseconds with %" FMT_OFF_T " out of %"
FMT_OFF_T " bytes received",
curlx_timediff_ms(*nowp, data->progress.t_startsingle),
curlx_timediff_ms(data->progress.now,
data->progress.t_startsingle),
k->bytecount, k->size);
}
else {
failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
" milliseconds with %" FMT_OFF_T " bytes received",
curlx_timediff_ms(*nowp, data->progress.t_startsingle),
curlx_timediff_ms(data->progress.now,
data->progress.t_startsingle),
k->bytecount);
}
result = CURLE_OPERATION_TIMEDOUT;
@ -902,7 +903,7 @@ bool Curl_xfer_recv_is_paused(struct Curl_easy *data)
CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable)
{
CURLcode result = CURLE_OK;
Curl_rlimit_block(&data->progress.ul.rlimit, enable, curlx_now());
Curl_rlimit_block(&data->progress.ul.rlimit, enable, &data->progress.now);
if(!enable && Curl_creader_is_paused(data))
result = Curl_creader_unpause(data);
Curl_pgrsSendPause(data, enable);
@ -912,7 +913,7 @@ CURLcode Curl_xfer_pause_send(struct Curl_easy *data, bool enable)
CURLcode Curl_xfer_pause_recv(struct Curl_easy *data, bool enable)
{
CURLcode result = CURLE_OK;
Curl_rlimit_block(&data->progress.dl.rlimit, enable, curlx_now());
Curl_rlimit_block(&data->progress.dl.rlimit, enable, &data->progress.now);
if(!enable && Curl_cwriter_is_paused(data))
result = Curl_cwriter_unpause(data);
Curl_conn_ev_data_pause(data, enable);