1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
29 #include "curl_printf.h"
31 /* check rate limits within this many recent milliseconds, at minimum. */
32 #define MIN_RATE_LIMIT_PERIOD 3000
34 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
36 static void time2str(char *r, curl_off_t seconds)
40 strcpy(r, "--:--:--");
43 h = seconds / CURL_OFF_T_C(3600);
44 if(h <= CURL_OFF_T_C(99)) {
45 curl_off_t m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
46 curl_off_t s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
47 snprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
48 ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
51 /* this equals to more than 99 hours, switch to a more suitable output
52 format to fit within the limits. */
53 curl_off_t d = seconds / CURL_OFF_T_C(86400);
54 h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
55 if(d <= CURL_OFF_T_C(999))
56 snprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
57 "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
59 snprintf(r, 9, "%7" CURL_FORMAT_CURL_OFF_T "d", d);
63 /* The point of this function would be to return a string of the input data,
64 but never longer than 5 columns (+ one zero byte).
65 Add suffix k, M, G when suitable... */
66 static char *max5data(curl_off_t bytes, char *max5)
68 #define ONE_KILOBYTE CURL_OFF_T_C(1024)
69 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
70 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
71 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
72 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
74 if(bytes < CURL_OFF_T_C(100000))
75 snprintf(max5, 6, "%5" CURL_FORMAT_CURL_OFF_T, bytes);
77 else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
78 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "k", bytes/ONE_KILOBYTE);
80 else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
81 /* 'XX.XM' is good as long as we're less than 100 megs */
82 snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
83 CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
84 (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
86 #if (CURL_SIZEOF_CURL_OFF_T > 4)
88 else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
89 /* 'XXXXM' is good until we're at 10000MB or above */
90 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
92 else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
93 /* 10000 MB - 100 GB, we show it as XX.XG */
94 snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
95 CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
96 (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
98 else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
99 /* up to 10000GB, display without decimal: XXXXG */
100 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
102 else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
103 /* up to 10000TB, display without decimal: XXXXT */
104 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
107 /* up to 10000PB, display without decimal: XXXXP */
108 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
110 /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
111 can hold, but our data type is signed so 8192PB will be the maximum. */
116 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
125 New proposed interface, 9th of February 2000:
127 pgrsStartNow() - sets start time
128 pgrsSetDownloadSize(x) - known expected download size
129 pgrsSetUploadSize(x) - known expected upload size
130 pgrsSetDownloadCounter() - amount of data currently downloaded
131 pgrsSetUploadCounter() - amount of data currently uploaded
132 pgrsUpdate() - show progress
133 pgrsDone() - transfer complete
137 int Curl_pgrsDone(struct connectdata *conn)
140 struct Curl_easy *data = conn->data;
141 data->progress.lastshow = 0;
142 rc = Curl_pgrsUpdate(conn); /* the final (forced) update */
146 if(!(data->progress.flags & PGRS_HIDE) &&
147 !data->progress.callback)
148 /* only output if we don't use a progress callback and we're not
150 fprintf(data->set.err, "\n");
152 data->progress.speeder_c = 0; /* reset the progress meter display */
156 /* reset the known transfer sizes */
157 void Curl_pgrsResetTransferSizes(struct Curl_easy *data)
159 Curl_pgrsSetDownloadSize(data, -1);
160 Curl_pgrsSetUploadSize(data, -1);
166 void Curl_pgrsTime(struct Curl_easy *data, timerid timer)
168 struct curltime now = Curl_now();
169 time_t *delta = NULL;
177 /* This is set at the start of a transfer */
178 data->progress.t_startop = now;
180 case TIMER_STARTSINGLE:
181 /* This is set at the start of each single fetch */
182 data->progress.t_startsingle = now;
183 data->progress.is_t_startransfer_set = false;
185 case TIMER_STARTACCEPT:
186 data->progress.t_acceptdata = now;
188 case TIMER_NAMELOOKUP:
189 delta = &data->progress.t_nslookup;
192 delta = &data->progress.t_connect;
194 case TIMER_APPCONNECT:
195 delta = &data->progress.t_appconnect;
197 case TIMER_PRETRANSFER:
198 delta = &data->progress.t_pretransfer;
200 case TIMER_STARTTRANSFER:
201 delta = &data->progress.t_starttransfer;
202 /* prevent updating t_starttransfer unless:
203 * 1) this is the first time we're setting t_starttransfer
204 * 2) a redirect has occurred since the last time t_starttransfer was set
205 * This prevents repeated invocations of the function from incorrectly
206 * changing the t_starttransfer time.
208 if(data->progress.is_t_startransfer_set) {
212 data->progress.is_t_startransfer_set = true;
215 case TIMER_POSTRANSFER:
216 /* this is the normal end-of-transfer thing */
219 data->progress.t_redirect = Curl_timediff_us(now, data->progress.start);
223 timediff_t us = Curl_timediff_us(now, data->progress.t_startsingle);
225 us = 1; /* make sure at least one microsecond passed */
230 void Curl_pgrsStartNow(struct Curl_easy *data)
232 data->progress.speeder_c = 0; /* reset the progress meter display */
233 data->progress.start = Curl_now();
234 data->progress.is_t_startransfer_set = false;
235 data->progress.ul_limit_start.tv_sec = 0;
236 data->progress.ul_limit_start.tv_usec = 0;
237 data->progress.dl_limit_start.tv_sec = 0;
238 data->progress.dl_limit_start.tv_usec = 0;
239 /* clear all bits except HIDE and HEADERS_OUT */
240 data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
241 Curl_ratelimit(data, data->progress.start);
245 * This is used to handle speed limits, calculating how many milliseconds to
246 * wait until we're back under the speed limit, if needed.
248 * The way it works is by having a "starting point" (time & amount of data
249 * transferred by then) used in the speed computation, to be used instead of
250 * the start of the transfer. This starting point is regularly moved as
251 * transfer goes on, to keep getting accurate values (instead of average over
252 * the entire transfer).
254 * This function takes the current amount of data transferred, the amount at
255 * the starting point, the limit (in bytes/s), the time of the starting point
256 * and the current time.
258 * Returns 0 if no waiting is needed or when no waiting is needed but the
259 * starting point should be reset (to current); or the number of milliseconds
260 * to wait to get back under the speed limit.
262 timediff_t Curl_pgrsLimitWaitTime(curl_off_t cursize,
263 curl_off_t startsize,
265 struct curltime start,
268 curl_off_t size = cursize - startsize;
276 * 'minimum' is the number of milliseconds 'size' should take to download to
277 * stay below 'limit'.
279 if(size < CURL_OFF_T_MAX/1000)
280 minimum = (time_t) (CURL_OFF_T_C(1000) * size / limit);
282 minimum = (time_t) (size / limit);
283 if(minimum < TIME_T_MAX/1000)
286 minimum = TIME_T_MAX;
290 * 'actual' is the time in milliseconds it took to actually download the
293 actual = Curl_timediff(now, start);
294 if(actual < minimum) {
295 /* if it downloaded the data faster than the limit, make it wait the
297 return (minimum - actual);
304 * Set the number of downloaded bytes so far.
306 void Curl_pgrsSetDownloadCounter(struct Curl_easy *data, curl_off_t size)
308 data->progress.downloaded = size;
312 * Update the timestamp and sizestamp to use for rate limit calculations.
314 void Curl_ratelimit(struct Curl_easy *data, struct curltime now)
316 /* don't set a new stamp unless the time since last update is long enough */
317 if(data->set.max_recv_speed > 0) {
318 if(Curl_timediff(now, data->progress.dl_limit_start) >=
319 MIN_RATE_LIMIT_PERIOD) {
320 data->progress.dl_limit_start = now;
321 data->progress.dl_limit_size = data->progress.downloaded;
324 if(data->set.max_send_speed > 0) {
325 if(Curl_timediff(now, data->progress.ul_limit_start) >=
326 MIN_RATE_LIMIT_PERIOD) {
327 data->progress.ul_limit_start = now;
328 data->progress.ul_limit_size = data->progress.uploaded;
334 * Set the number of uploaded bytes so far.
336 void Curl_pgrsSetUploadCounter(struct Curl_easy *data, curl_off_t size)
338 data->progress.uploaded = size;
341 void Curl_pgrsSetDownloadSize(struct Curl_easy *data, curl_off_t size)
344 data->progress.size_dl = size;
345 data->progress.flags |= PGRS_DL_SIZE_KNOWN;
348 data->progress.size_dl = 0;
349 data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
353 void Curl_pgrsSetUploadSize(struct Curl_easy *data, curl_off_t size)
356 data->progress.size_ul = size;
357 data->progress.flags |= PGRS_UL_SIZE_KNOWN;
360 data->progress.size_ul = 0;
361 data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
366 * Curl_pgrsUpdate() returns 0 for success or the value returned by the
369 int Curl_pgrsUpdate(struct connectdata *conn)
372 curl_off_t timespent;
373 curl_off_t timespent_ms; /* milliseconds */
374 struct Curl_easy *data = conn->data;
375 int nowindex = data->progress.speeder_c% CURR_TIME;
376 bool shownow = FALSE;
377 curl_off_t dl = data->progress.downloaded;
378 curl_off_t ul = data->progress.uploaded;
380 now = Curl_now(); /* what time is it */
382 /* The time spent so far (from the start) */
383 data->progress.timespent = Curl_timediff_us(now, data->progress.start);
384 timespent = (curl_off_t)data->progress.timespent/1000000; /* seconds */
385 timespent_ms = (curl_off_t)data->progress.timespent/1000; /* ms */
387 /* The average download speed this far */
388 if(dl < CURL_OFF_T_MAX/1000)
389 data->progress.dlspeed = (dl * 1000 / (timespent_ms>0?timespent_ms:1));
391 data->progress.dlspeed = (dl / (timespent>0?timespent:1));
393 /* The average upload speed this far */
394 if(ul < CURL_OFF_T_MAX/1000)
395 data->progress.ulspeed = (ul * 1000 / (timespent_ms>0?timespent_ms:1));
397 data->progress.ulspeed = (ul / (timespent>0?timespent:1));
399 /* Calculations done at most once a second, unless end is reached */
400 if(data->progress.lastshow != now.tv_sec) {
401 int countindex; /* amount of seconds stored in the speeder array */
404 data->progress.lastshow = now.tv_sec;
406 /* Let's do the "current speed" thing, with the dl + ul speeds
407 combined. Store the speed at entry 'nowindex'. */
408 data->progress.speeder[ nowindex ] =
409 data->progress.downloaded + data->progress.uploaded;
411 /* remember the exact time for this moment */
412 data->progress.speeder_time [ nowindex ] = now;
414 /* advance our speeder_c counter, which is increased every time we get
415 here and we expect it to never wrap as 2^32 is a lot of seconds! */
416 data->progress.speeder_c++;
418 /* figure out how many index entries of data we have stored in our speeder
419 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
420 transfer. Imagine, after one second we have filled in two entries,
421 after two seconds we've filled in three entries etc. */
422 countindex = ((data->progress.speeder_c >= CURR_TIME)?
423 CURR_TIME:data->progress.speeder_c) - 1;
425 /* first of all, we don't do this if there's no counted seconds yet */
430 /* Get the index position to compare with the 'nowindex' position.
431 Get the oldest entry possible. While we have less than CURR_TIME
432 entries, the first entry will remain the oldest. */
433 checkindex = (data->progress.speeder_c >= CURR_TIME)?
434 data->progress.speeder_c%CURR_TIME:0;
436 /* Figure out the exact time for the time span */
437 span_ms = Curl_timediff(now,
438 data->progress.speeder_time[checkindex]);
440 span_ms = 1; /* at least one millisecond MUST have passed */
442 /* Calculate the average speed the last 'span_ms' milliseconds */
444 curl_off_t amount = data->progress.speeder[nowindex]-
445 data->progress.speeder[checkindex];
447 if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
448 /* the 'amount' value is bigger than would fit in 32 bits if
449 multiplied with 1000, so we use the double math for this */
450 data->progress.current_speed = (curl_off_t)
451 ((double)amount/((double)span_ms/1000.0));
453 /* the 'amount' value is small enough to fit within 32 bits even
454 when multiplied with 1000 */
455 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
459 /* the first second we use the average */
460 data->progress.current_speed =
461 data->progress.ulspeed + data->progress.dlspeed;
463 } /* Calculations end */
465 if(!(data->progress.flags & PGRS_HIDE)) {
466 /* progress meter has not been shut off */
468 curl_off_t dlpercen = 0;
469 curl_off_t ulpercen = 0;
470 curl_off_t total_percen = 0;
471 curl_off_t total_transfer;
472 curl_off_t total_expected_transfer;
476 curl_off_t ulestimate = 0;
477 curl_off_t dlestimate = 0;
478 curl_off_t total_estimate;
480 if(data->set.fxferinfo) {
482 /* There's a callback set, call that */
483 Curl_set_in_callback(data, true);
484 result = data->set.fxferinfo(data->set.progress_client,
485 data->progress.size_dl,
486 data->progress.downloaded,
487 data->progress.size_ul,
488 data->progress.uploaded);
489 Curl_set_in_callback(data, false);
491 failf(data, "Callback aborted");
494 if(data->set.fprogress) {
496 /* The older deprecated callback is set, call that */
497 Curl_set_in_callback(data, true);
498 result = data->set.fprogress(data->set.progress_client,
499 (double)data->progress.size_dl,
500 (double)data->progress.downloaded,
501 (double)data->progress.size_ul,
502 (double)data->progress.uploaded);
503 Curl_set_in_callback(data, false);
505 failf(data, "Callback aborted");
510 /* only show the internal progress meter once per second */
513 /* If there's no external callback set, use internal code to show
516 if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
517 if(data->state.resume_from) {
518 fprintf(data->set.err,
519 "** Resuming transfer from byte position %"
520 CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
522 fprintf(data->set.err,
523 " %% Total %% Received %% Xferd Average Speed "
524 "Time Time Time Current\n"
526 "Total Spent Left Speed\n");
527 data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
530 /* Figure out the estimated time of arrival for the upload */
531 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
532 (data->progress.ulspeed > CURL_OFF_T_C(0))) {
533 ulestimate = data->progress.size_ul / data->progress.ulspeed;
535 if(data->progress.size_ul > CURL_OFF_T_C(10000))
536 ulpercen = data->progress.uploaded /
537 (data->progress.size_ul/CURL_OFF_T_C(100));
538 else if(data->progress.size_ul > CURL_OFF_T_C(0))
539 ulpercen = (data->progress.uploaded*100) /
540 data->progress.size_ul;
543 /* ... and the download */
544 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
545 (data->progress.dlspeed > CURL_OFF_T_C(0))) {
546 dlestimate = data->progress.size_dl / data->progress.dlspeed;
548 if(data->progress.size_dl > CURL_OFF_T_C(10000))
549 dlpercen = data->progress.downloaded /
550 (data->progress.size_dl/CURL_OFF_T_C(100));
551 else if(data->progress.size_dl > CURL_OFF_T_C(0))
552 dlpercen = (data->progress.downloaded*100) /
553 data->progress.size_dl;
556 /* Now figure out which of them is slower and use that one for the
558 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
560 /* create the three time strings */
561 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
562 time2str(time_total, total_estimate);
563 time2str(time_spent, timespent);
565 /* Get the total amount of data expected to get transferred */
566 total_expected_transfer =
567 (data->progress.flags & PGRS_UL_SIZE_KNOWN?
568 data->progress.size_ul:data->progress.uploaded)+
569 (data->progress.flags & PGRS_DL_SIZE_KNOWN?
570 data->progress.size_dl:data->progress.downloaded);
572 /* We have transferred this much so far */
573 total_transfer = data->progress.downloaded + data->progress.uploaded;
575 /* Get the percentage of data transferred so far */
576 if(total_expected_transfer > CURL_OFF_T_C(10000))
577 total_percen = total_transfer /
578 (total_expected_transfer/CURL_OFF_T_C(100));
579 else if(total_expected_transfer > CURL_OFF_T_C(0))
580 total_percen = (total_transfer*100) / total_expected_transfer;
582 fprintf(data->set.err,
584 "%3" CURL_FORMAT_CURL_OFF_T " %s "
585 "%3" CURL_FORMAT_CURL_OFF_T " %s "
586 "%3" CURL_FORMAT_CURL_OFF_T " %s %s %s %s %s %s %s",
587 total_percen, /* 3 letters */ /* total % */
588 max5data(total_expected_transfer, max5[2]), /* total size */
589 dlpercen, /* 3 letters */ /* rcvd % */
590 max5data(data->progress.downloaded, max5[0]), /* rcvd size */
591 ulpercen, /* 3 letters */ /* xfer % */
592 max5data(data->progress.uploaded, max5[1]), /* xfer size */
593 max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
594 max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
595 time_total, /* 8 letters */ /* total time */
596 time_spent, /* 8 letters */ /* time spent */
597 time_left, /* 8 letters */ /* time left */
598 max5data(data->progress.current_speed, max5[5]) /* current speed */
601 /* we flush the output stream to make it appear as soon as possible */
602 fflush(data->set.err);
604 } /* !(data->progress.flags & PGRS_HIDE) */