1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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"
28 #include "curl_printf.h"
30 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
32 static void time2str(char *r, curl_off_t seconds)
34 curl_off_t d, h, m, s;
36 strcpy(r, "--:--:--");
39 h = seconds / CURL_OFF_T_C(3600);
40 if(h <= CURL_OFF_T_C(99)) {
41 m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
42 s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
43 snprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
44 ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
47 /* this equals to more than 99 hours, switch to a more suitable output
48 format to fit within the limits. */
49 d = seconds / CURL_OFF_T_C(86400);
50 h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
51 if(d <= CURL_OFF_T_C(999))
52 snprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
53 "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
55 snprintf(r, 9, "%7" CURL_FORMAT_CURL_OFF_T "d", d);
59 /* The point of this function would be to return a string of the input data,
60 but never longer than 5 columns (+ one zero byte).
61 Add suffix k, M, G when suitable... */
62 static char *max5data(curl_off_t bytes, char *max5)
64 #define ONE_KILOBYTE CURL_OFF_T_C(1024)
65 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
66 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
67 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
68 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
70 if(bytes < CURL_OFF_T_C(100000))
71 snprintf(max5, 6, "%5" CURL_FORMAT_CURL_OFF_T, bytes);
73 else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
74 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "k", bytes/ONE_KILOBYTE);
76 else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
77 /* 'XX.XM' is good as long as we're less than 100 megs */
78 snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
79 CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
80 (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
82 #if (CURL_SIZEOF_CURL_OFF_T > 4)
84 else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
85 /* 'XXXXM' is good until we're at 10000MB or above */
86 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
88 else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
89 /* 10000 MB - 100 GB, we show it as XX.XG */
90 snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
91 CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
92 (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
94 else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
95 /* up to 10000GB, display without decimal: XXXXG */
96 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
98 else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
99 /* up to 10000TB, display without decimal: XXXXT */
100 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
103 /* up to 10000PB, display without decimal: XXXXP */
104 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
106 /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
107 can hold, but our data type is signed so 8192PB will be the maximum. */
112 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
121 New proposed interface, 9th of February 2000:
123 pgrsStartNow() - sets start time
124 pgrsSetDownloadSize(x) - known expected download size
125 pgrsSetUploadSize(x) - known expected upload size
126 pgrsSetDownloadCounter() - amount of data currently downloaded
127 pgrsSetUploadCounter() - amount of data currently uploaded
128 pgrsUpdate() - show progress
129 pgrsDone() - transfer complete
133 int Curl_pgrsDone(struct connectdata *conn)
136 struct SessionHandle *data = conn->data;
137 data->progress.lastshow=0;
138 rc = Curl_pgrsUpdate(conn); /* the final (forced) update */
142 if(!(data->progress.flags & PGRS_HIDE) &&
143 !data->progress.callback)
144 /* only output if we don't use a progress callback and we're not
146 fprintf(data->set.err, "\n");
148 data->progress.speeder_c = 0; /* reset the progress meter display */
152 /* reset all times except redirect, and reset the known transfer sizes */
153 void Curl_pgrsResetTimesSizes(struct SessionHandle *data)
155 data->progress.t_nslookup = 0.0;
156 data->progress.t_connect = 0.0;
157 data->progress.t_pretransfer = 0.0;
158 data->progress.t_starttransfer = 0.0;
160 Curl_pgrsSetDownloadSize(data, -1);
161 Curl_pgrsSetUploadSize(data, -1);
164 void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
166 struct timeval now = Curl_tvnow();
174 /* This is set at the start of a transfer */
175 data->progress.t_startop = now;
177 case TIMER_STARTSINGLE:
178 /* This is set at the start of each single fetch */
179 data->progress.t_startsingle = now;
182 case TIMER_STARTACCEPT:
183 data->progress.t_acceptdata = Curl_tvnow();
186 case TIMER_NAMELOOKUP:
187 data->progress.t_nslookup =
188 Curl_tvdiff_secs(now, data->progress.t_startsingle);
191 data->progress.t_connect =
192 Curl_tvdiff_secs(now, data->progress.t_startsingle);
194 case TIMER_APPCONNECT:
195 data->progress.t_appconnect =
196 Curl_tvdiff_secs(now, data->progress.t_startsingle);
198 case TIMER_PRETRANSFER:
199 data->progress.t_pretransfer =
200 Curl_tvdiff_secs(now, data->progress.t_startsingle);
202 case TIMER_STARTTRANSFER:
203 data->progress.t_starttransfer =
204 Curl_tvdiff_secs(now, data->progress.t_startsingle);
206 case TIMER_POSTRANSFER:
207 /* this is the normal end-of-transfer thing */
210 data->progress.t_redirect = Curl_tvdiff_secs(now, data->progress.start);
215 void Curl_pgrsStartNow(struct SessionHandle *data)
217 data->progress.speeder_c = 0; /* reset the progress meter display */
218 data->progress.start = Curl_tvnow();
219 /* clear all bits except HIDE and HEADERS_OUT */
220 data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
223 void Curl_pgrsSetDownloadCounter(struct SessionHandle *data, curl_off_t size)
225 data->progress.downloaded = size;
228 void Curl_pgrsSetUploadCounter(struct SessionHandle *data, curl_off_t size)
230 data->progress.uploaded = size;
233 void Curl_pgrsSetDownloadSize(struct SessionHandle *data, curl_off_t size)
236 data->progress.size_dl = size;
237 data->progress.flags |= PGRS_DL_SIZE_KNOWN;
240 data->progress.size_dl = 0;
241 data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
245 void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size)
248 data->progress.size_ul = size;
249 data->progress.flags |= PGRS_UL_SIZE_KNOWN;
252 data->progress.size_ul = 0;
253 data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
258 * Curl_pgrsUpdate() returns 0 for success or the value returned by the
261 int Curl_pgrsUpdate(struct connectdata *conn)
266 curl_off_t dlpercen=0;
267 curl_off_t ulpercen=0;
268 curl_off_t total_percen=0;
269 curl_off_t total_transfer;
270 curl_off_t total_expected_transfer;
271 curl_off_t timespent;
272 struct SessionHandle *data = conn->data;
273 int nowindex = data->progress.speeder_c% CURR_TIME;
275 int countindex; /* amount of seconds stored in the speeder array */
279 curl_off_t ulestimate=0;
280 curl_off_t dlestimate=0;
281 curl_off_t total_estimate;
284 now = Curl_tvnow(); /* what time is it */
286 /* The time spent so far (from the start) */
287 data->progress.timespent =
288 (double)(now.tv_sec - data->progress.start.tv_sec) +
289 (double)(now.tv_usec - data->progress.start.tv_usec)/1000000.0;
290 timespent = (curl_off_t)data->progress.timespent;
292 /* The average download speed this far */
293 data->progress.dlspeed = (curl_off_t)
294 ((double)data->progress.downloaded/
295 (data->progress.timespent>0?data->progress.timespent:1));
297 /* The average upload speed this far */
298 data->progress.ulspeed = (curl_off_t)
299 ((double)data->progress.uploaded/
300 (data->progress.timespent>0?data->progress.timespent:1));
302 /* Calculations done at most once a second, unless end is reached */
303 if(data->progress.lastshow != (long)now.tv_sec) {
306 data->progress.lastshow = now.tv_sec;
308 /* Let's do the "current speed" thing, which should use the fastest
309 of the dl/ul speeds. Store the faster speed at entry 'nowindex'. */
310 data->progress.speeder[ nowindex ] =
311 data->progress.downloaded>data->progress.uploaded?
312 data->progress.downloaded:data->progress.uploaded;
314 /* remember the exact time for this moment */
315 data->progress.speeder_time [ nowindex ] = now;
317 /* advance our speeder_c counter, which is increased every time we get
318 here and we expect it to never wrap as 2^32 is a lot of seconds! */
319 data->progress.speeder_c++;
321 /* figure out how many index entries of data we have stored in our speeder
322 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
323 transfer. Imagine, after one second we have filled in two entries,
324 after two seconds we've filled in three entries etc. */
325 countindex = ((data->progress.speeder_c>=CURR_TIME)?
326 CURR_TIME:data->progress.speeder_c) - 1;
328 /* first of all, we don't do this if there's no counted seconds yet */
332 /* Get the index position to compare with the 'nowindex' position.
333 Get the oldest entry possible. While we have less than CURR_TIME
334 entries, the first entry will remain the oldest. */
335 checkindex = (data->progress.speeder_c>=CURR_TIME)?
336 data->progress.speeder_c%CURR_TIME:0;
338 /* Figure out the exact time for the time span */
339 span_ms = Curl_tvdiff(now,
340 data->progress.speeder_time[checkindex]);
342 span_ms=1; /* at least one millisecond MUST have passed */
344 /* Calculate the average speed the last 'span_ms' milliseconds */
346 curl_off_t amount = data->progress.speeder[nowindex]-
347 data->progress.speeder[checkindex];
349 if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
350 /* the 'amount' value is bigger than would fit in 32 bits if
351 multiplied with 1000, so we use the double math for this */
352 data->progress.current_speed = (curl_off_t)
353 ((double)amount/((double)span_ms/1000.0));
355 /* the 'amount' value is small enough to fit within 32 bits even
356 when multiplied with 1000 */
357 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
361 /* the first second we use the main average */
362 data->progress.current_speed =
363 (data->progress.ulspeed>data->progress.dlspeed)?
364 data->progress.ulspeed:data->progress.dlspeed;
366 } /* Calculations end */
368 if(!(data->progress.flags & PGRS_HIDE)) {
369 /* progress meter has not been shut off */
371 if(data->set.fxferinfo) {
372 /* There's a callback set, call that */
373 result= data->set.fxferinfo(data->set.progress_client,
374 data->progress.size_dl,
375 data->progress.downloaded,
376 data->progress.size_ul,
377 data->progress.uploaded);
379 failf(data, "Callback aborted");
382 else if(data->set.fprogress) {
383 /* The older deprecated callback is set, call that */
384 result= data->set.fprogress(data->set.progress_client,
385 (double)data->progress.size_dl,
386 (double)data->progress.downloaded,
387 (double)data->progress.size_ul,
388 (double)data->progress.uploaded);
390 failf(data, "Callback aborted");
395 /* only show the internal progress meter once per second */
398 /* If there's no external callback set, use internal code to show
401 if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
402 if(data->state.resume_from) {
403 fprintf(data->set.err,
404 "** Resuming transfer from byte position %"
405 CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
407 fprintf(data->set.err,
408 " %% Total %% Received %% Xferd Average Speed "
409 "Time Time Time Current\n"
411 "Total Spent Left Speed\n");
412 data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
415 /* Figure out the estimated time of arrival for the upload */
416 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
417 (data->progress.ulspeed > CURL_OFF_T_C(0))) {
418 ulestimate = data->progress.size_ul / data->progress.ulspeed;
420 if(data->progress.size_ul > CURL_OFF_T_C(10000))
421 ulpercen = data->progress.uploaded /
422 (data->progress.size_ul/CURL_OFF_T_C(100));
423 else if(data->progress.size_ul > CURL_OFF_T_C(0))
424 ulpercen = (data->progress.uploaded*100) /
425 data->progress.size_ul;
428 /* ... and the download */
429 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
430 (data->progress.dlspeed > CURL_OFF_T_C(0))) {
431 dlestimate = data->progress.size_dl / data->progress.dlspeed;
433 if(data->progress.size_dl > CURL_OFF_T_C(10000))
434 dlpercen = data->progress.downloaded /
435 (data->progress.size_dl/CURL_OFF_T_C(100));
436 else if(data->progress.size_dl > CURL_OFF_T_C(0))
437 dlpercen = (data->progress.downloaded*100) /
438 data->progress.size_dl;
441 /* Now figure out which of them is slower and use that one for the
443 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
445 /* create the three time strings */
446 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
447 time2str(time_total, total_estimate);
448 time2str(time_spent, timespent);
450 /* Get the total amount of data expected to get transferred */
451 total_expected_transfer =
452 (data->progress.flags & PGRS_UL_SIZE_KNOWN?
453 data->progress.size_ul:data->progress.uploaded)+
454 (data->progress.flags & PGRS_DL_SIZE_KNOWN?
455 data->progress.size_dl:data->progress.downloaded);
457 /* We have transferred this much so far */
458 total_transfer = data->progress.downloaded + data->progress.uploaded;
460 /* Get the percentage of data transferred so far */
461 if(total_expected_transfer > CURL_OFF_T_C(10000))
462 total_percen = total_transfer /
463 (total_expected_transfer/CURL_OFF_T_C(100));
464 else if(total_expected_transfer > CURL_OFF_T_C(0))
465 total_percen = (total_transfer*100) / total_expected_transfer;
467 fprintf(data->set.err,
469 "%3" CURL_FORMAT_CURL_OFF_T " %s "
470 "%3" CURL_FORMAT_CURL_OFF_T " %s "
471 "%3" CURL_FORMAT_CURL_OFF_T " %s %s %s %s %s %s %s",
472 total_percen, /* 3 letters */ /* total % */
473 max5data(total_expected_transfer, max5[2]), /* total size */
474 dlpercen, /* 3 letters */ /* rcvd % */
475 max5data(data->progress.downloaded, max5[0]), /* rcvd size */
476 ulpercen, /* 3 letters */ /* xfer % */
477 max5data(data->progress.uploaded, max5[1]), /* xfer size */
478 max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
479 max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
480 time_total, /* 8 letters */ /* total time */
481 time_spent, /* 8 letters */ /* time spent */
482 time_left, /* 8 letters */ /* time left */
483 max5data(data->progress.current_speed, max5[5]) /* current speed */
486 /* we flush the output stream to make it appear as soon as possible */
487 fflush(data->set.err);
489 } /* !(data->progress.flags & PGRS_HIDE) */