1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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 http://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 * $Id: progress.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
30 #define _MPRINTF_REPLACE /* use our functions only */
31 #include <curl/mprintf.h>
33 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
35 static void time2str(char *r
, long t
)
39 strcpy(r
, "--:--:--");
44 long m
= (t
-(h
*3600))/60;
45 long s
= (t
-(h
*3600)-(m
*60));
46 snprintf(r
, 9, "%2ld:%02ld:%02ld",h
,m
,s
);
49 /* this equals to more than 99 hours, switch to a more suitable output
50 format to fit within the limits. */
52 snprintf(r
, 9, "%3ldd %02ldh", h
/24, h
-(h
/24)*24);
54 snprintf(r
, 9, "%7ldd", h
/24);
58 /* The point of this function would be to return a string of the input data,
59 but never longer than 5 columns (+ one zero byte).
60 Add suffix k, M, G when suitable... */
61 static char *max5data(curl_off_t bytes
, char *max5
)
63 #define ONE_KILOBYTE CURL_OFF_T_C(1024)
64 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
65 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
66 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
67 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
69 if(bytes
< CURL_OFF_T_C(100000))
70 snprintf(max5
, 6, "%5" FORMAT_OFF_T
, bytes
);
72 else if(bytes
< CURL_OFF_T_C(10000) * ONE_KILOBYTE
)
73 snprintf(max5
, 6, "%4" FORMAT_OFF_T
"k", bytes
/ONE_KILOBYTE
);
75 else if(bytes
< CURL_OFF_T_C(100) * ONE_MEGABYTE
)
76 /* 'XX.XM' is good as long as we're less than 100 megs */
77 snprintf(max5
, 6, "%2" FORMAT_OFF_T
".%0" FORMAT_OFF_T
"M",
79 (bytes
%ONE_MEGABYTE
) / (ONE_MEGABYTE
/CURL_OFF_T_C(10)) );
81 #if (CURL_SIZEOF_CURL_OFF_T > 4)
83 else if(bytes
< CURL_OFF_T_C(10000) * ONE_MEGABYTE
)
84 /* 'XXXXM' is good until we're at 10000MB or above */
85 snprintf(max5
, 6, "%4" FORMAT_OFF_T
"M", bytes
/ONE_MEGABYTE
);
87 else if(bytes
< CURL_OFF_T_C(100) * ONE_GIGABYTE
)
88 /* 10000 MB - 100 GB, we show it as XX.XG */
89 snprintf(max5
, 6, "%2" FORMAT_OFF_T
".%0" FORMAT_OFF_T
"G",
91 (bytes
%ONE_GIGABYTE
) / (ONE_GIGABYTE
/CURL_OFF_T_C(10)) );
93 else if(bytes
< CURL_OFF_T_C(10000) * ONE_GIGABYTE
)
94 /* up to 10000GB, display without decimal: XXXXG */
95 snprintf(max5
, 6, "%4" FORMAT_OFF_T
"G", bytes
/ONE_GIGABYTE
);
97 else if(bytes
< CURL_OFF_T_C(10000) * ONE_TERABYTE
)
98 /* up to 10000TB, display without decimal: XXXXT */
99 snprintf(max5
, 6, "%4" FORMAT_OFF_T
"T", bytes
/ONE_TERABYTE
);
102 /* up to 10000PB, display without decimal: XXXXP */
103 snprintf(max5
, 6, "%4" FORMAT_OFF_T
"P", bytes
/ONE_PETABYTE
);
105 /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
106 can hold, but our data type is signed so 8192PB will be the maximum. */
111 snprintf(max5
, 6, "%4" FORMAT_OFF_T
"M", bytes
/ONE_MEGABYTE
);
120 New proposed interface, 9th of February 2000:
122 pgrsStartNow() - sets start time
123 pgrsSetDownloadSize(x) - known expected download size
124 pgrsSetUploadSize(x) - known expected upload size
125 pgrsSetDownloadCounter() - amount of data currently downloaded
126 pgrsSetUploadCounter() - amount of data currently uploaded
127 pgrsUpdate() - show progress
128 pgrsDone() - transfer complete
132 void Curl_pgrsDone(struct connectdata
*conn
)
134 struct SessionHandle
*data
= conn
->data
;
135 data
->progress
.lastshow
=0;
136 Curl_pgrsUpdate(conn
); /* the final (forced) update */
138 data
->progress
.speeder_c
= 0; /* reset the progress meter display */
141 /* reset all times except redirect */
142 void Curl_pgrsResetTimes(struct SessionHandle
*data
)
144 data
->progress
.t_nslookup
= 0.0;
145 data
->progress
.t_connect
= 0.0;
146 data
->progress
.t_pretransfer
= 0.0;
147 data
->progress
.t_starttransfer
= 0.0;
150 void Curl_pgrsTime(struct SessionHandle
*data
, timerid timer
)
157 case TIMER_STARTSINGLE
:
158 /* This is set at the start of a single fetch */
159 data
->progress
.t_startsingle
= Curl_tvnow();
162 case TIMER_NAMELOOKUP
:
163 data
->progress
.t_nslookup
=
164 Curl_tvdiff_secs(Curl_tvnow(), data
->progress
.t_startsingle
);
167 data
->progress
.t_connect
=
168 Curl_tvdiff_secs(Curl_tvnow(), data
->progress
.t_startsingle
);
170 case TIMER_APPCONNECT
:
171 data
->progress
.t_appconnect
=
172 Curl_tvdiff_secs(Curl_tvnow(), data
->progress
.t_startsingle
);
174 case TIMER_PRETRANSFER
:
175 data
->progress
.t_pretransfer
=
176 Curl_tvdiff_secs(Curl_tvnow(), data
->progress
.t_startsingle
);
178 case TIMER_STARTTRANSFER
:
179 data
->progress
.t_starttransfer
=
180 Curl_tvdiff_secs(Curl_tvnow(), data
->progress
.t_startsingle
);
182 case TIMER_POSTRANSFER
:
183 /* this is the normal end-of-transfer thing */
186 data
->progress
.t_redirect
=
187 Curl_tvdiff_secs(Curl_tvnow(), data
->progress
.start
);
192 void Curl_pgrsStartNow(struct SessionHandle
*data
)
194 data
->progress
.speeder_c
= 0; /* reset the progress meter display */
195 data
->progress
.start
= Curl_tvnow();
198 void Curl_pgrsSetDownloadCounter(struct SessionHandle
*data
, curl_off_t size
)
200 data
->progress
.downloaded
= size
;
203 void Curl_pgrsSetUploadCounter(struct SessionHandle
*data
, curl_off_t size
)
205 data
->progress
.uploaded
= size
;
208 void Curl_pgrsSetDownloadSize(struct SessionHandle
*data
, curl_off_t size
)
210 data
->progress
.size_dl
= size
;
212 data
->progress
.flags
|= PGRS_DL_SIZE_KNOWN
;
214 data
->progress
.flags
&= ~PGRS_DL_SIZE_KNOWN
;
217 void Curl_pgrsSetUploadSize(struct SessionHandle
*data
, curl_off_t size
)
219 data
->progress
.size_ul
= size
;
221 data
->progress
.flags
|= PGRS_UL_SIZE_KNOWN
;
223 data
->progress
.flags
&= ~PGRS_UL_SIZE_KNOWN
;
226 int Curl_pgrsUpdate(struct connectdata
*conn
)
234 curl_off_t total_transfer
;
235 curl_off_t total_expected_transfer
;
237 struct SessionHandle
*data
= conn
->data
;
238 int nowindex
= data
->progress
.speeder_c
% CURR_TIME
;
240 int countindex
; /* amount of seconds stored in the speeder array */
249 now
= Curl_tvnow(); /* what time is it */
251 /* The time spent so far (from the start) */
252 data
->progress
.timespent
=
253 (double)(now
.tv_sec
- data
->progress
.start
.tv_sec
) +
254 (double)(now
.tv_usec
- data
->progress
.start
.tv_usec
)/1000000.0;
255 timespent
= (long)data
->progress
.timespent
;
257 /* The average download speed this far */
258 data
->progress
.dlspeed
= (curl_off_t
)
259 ((double)data
->progress
.downloaded
/
260 (data
->progress
.timespent
>0?data
->progress
.timespent
:1));
262 /* The average upload speed this far */
263 data
->progress
.ulspeed
= (curl_off_t
)
264 ((double)data
->progress
.uploaded
/
265 (data
->progress
.timespent
>0?data
->progress
.timespent
:1));
267 /* Calculations done at most once a second, unless end is reached */
268 if(data
->progress
.lastshow
!= (long)now
.tv_sec
) {
271 data
->progress
.lastshow
= now
.tv_sec
;
273 /* Let's do the "current speed" thing, which should use the fastest
274 of the dl/ul speeds. Store the faster speed at entry 'nowindex'. */
275 data
->progress
.speeder
[ nowindex
] =
276 data
->progress
.downloaded
>data
->progress
.uploaded
?
277 data
->progress
.downloaded
:data
->progress
.uploaded
;
279 /* remember the exact time for this moment */
280 data
->progress
.speeder_time
[ nowindex
] = now
;
282 /* advance our speeder_c counter, which is increased every time we get
283 here and we expect it to never wrap as 2^32 is a lot of seconds! */
284 data
->progress
.speeder_c
++;
286 /* figure out how many index entries of data we have stored in our speeder
287 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
288 transfer. Imagine, after one second we have filled in two entries,
289 after two seconds we've filled in three entries etc. */
290 countindex
= ((data
->progress
.speeder_c
>=CURR_TIME
)?
291 CURR_TIME
:data
->progress
.speeder_c
) - 1;
293 /* first of all, we don't do this if there's no counted seconds yet */
297 /* Get the index position to compare with the 'nowindex' position.
298 Get the oldest entry possible. While we have less than CURR_TIME
299 entries, the first entry will remain the oldest. */
300 checkindex
= (data
->progress
.speeder_c
>=CURR_TIME
)?
301 data
->progress
.speeder_c
%CURR_TIME
:0;
303 /* Figure out the exact time for the time span */
304 span_ms
= Curl_tvdiff(now
,
305 data
->progress
.speeder_time
[checkindex
]);
307 span_ms
=1; /* at least one millisecond MUST have passed */
309 /* Calculate the average speed the last 'span_ms' milliseconds */
311 curl_off_t amount
= data
->progress
.speeder
[nowindex
]-
312 data
->progress
.speeder
[checkindex
];
314 if(amount
> CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
315 /* the 'amount' value is bigger than would fit in 32 bits if
316 multiplied with 1000, so we use the double math for this */
317 data
->progress
.current_speed
= (curl_off_t
)
318 ((double)amount
/((double)span_ms
/1000.0));
320 /* the 'amount' value is small enough to fit within 32 bits even
321 when multiplied with 1000 */
322 data
->progress
.current_speed
= amount
*CURL_OFF_T_C(1000)/span_ms
;
326 /* the first second we use the main average */
327 data
->progress
.current_speed
=
328 (data
->progress
.ulspeed
>data
->progress
.dlspeed
)?
329 data
->progress
.ulspeed
:data
->progress
.dlspeed
;
331 } /* Calculations end */
333 if(!(data
->progress
.flags
& PGRS_HIDE
)) {
335 /* progress meter has not been shut off */
337 if(data
->set
.fprogress
) {
338 /* There's a callback set, so we call that instead of writing
339 anything ourselves. This really is the way to go. */
340 result
= data
->set
.fprogress(data
->set
.progress_client
,
341 (double)data
->progress
.size_dl
,
342 (double)data
->progress
.downloaded
,
343 (double)data
->progress
.size_ul
,
344 (double)data
->progress
.uploaded
);
346 failf(data
, "Callback aborted");
351 /* only show the internal progress meter once per second */
354 /* If there's no external callback set, use internal code to show
357 if(!(data
->progress
.flags
& PGRS_HEADERS_OUT
)) {
358 if(data
->state
.resume_from
) {
359 fprintf(data
->set
.err
,
360 "** Resuming transfer from byte position %" FORMAT_OFF_T
"\n",
361 data
->state
.resume_from
);
363 fprintf(data
->set
.err
,
364 " %% Total %% Received %% Xferd Average Speed Time Time Time Current\n"
365 " Dload Upload Total Spent Left Speed\n");
366 data
->progress
.flags
|= PGRS_HEADERS_OUT
; /* headers are shown */
369 /* Figure out the estimated time of arrival for the upload */
370 if((data
->progress
.flags
& PGRS_UL_SIZE_KNOWN
) &&
371 (data
->progress
.ulspeed
>0) &&
372 (data
->progress
.size_ul
> 100) ) {
373 ulestimate
= (long)(data
->progress
.size_ul
/ data
->progress
.ulspeed
);
374 ulpercen
= (int)(100*(data
->progress
.uploaded
/100) /
375 (data
->progress
.size_ul
/100) );
378 /* ... and the download */
379 if((data
->progress
.flags
& PGRS_DL_SIZE_KNOWN
) &&
380 (data
->progress
.dlspeed
>0) &&
381 (data
->progress
.size_dl
>100)) {
382 dlestimate
= (long)(data
->progress
.size_dl
/ data
->progress
.dlspeed
);
383 dlpercen
= (int)(100*(data
->progress
.downloaded
/100) /
384 (data
->progress
.size_dl
/100));
387 /* Now figure out which of them is slower and use that one for the
389 total_estimate
= ulestimate
>dlestimate
?ulestimate
:dlestimate
;
391 /* create the three time strings */
392 time2str(time_left
, total_estimate
> 0?(total_estimate
- timespent
):0);
393 time2str(time_total
, total_estimate
);
394 time2str(time_spent
, timespent
);
396 /* Get the total amount of data expected to get transfered */
397 total_expected_transfer
=
398 (data
->progress
.flags
& PGRS_UL_SIZE_KNOWN
?
399 data
->progress
.size_ul
:data
->progress
.uploaded
)+
400 (data
->progress
.flags
& PGRS_DL_SIZE_KNOWN
?
401 data
->progress
.size_dl
:data
->progress
.downloaded
);
403 /* We have transfered this much so far */
404 total_transfer
= data
->progress
.downloaded
+ data
->progress
.uploaded
;
406 /* Get the percentage of data transfered so far */
407 if(total_expected_transfer
> 100)
408 total_percen
=(int)(100*(total_transfer
/100) /
409 (total_expected_transfer
/100) );
411 fprintf(data
->set
.err
,
412 "\r%3d %s %3d %s %3d %s %s %s %s %s %s %s",
413 total_percen
, /* 3 letters */ /* total % */
414 max5data(total_expected_transfer
, max5
[2]), /* total size */
415 dlpercen
, /* 3 letters */ /* rcvd % */
416 max5data(data
->progress
.downloaded
, max5
[0]), /* rcvd size */
417 ulpercen
, /* 3 letters */ /* xfer % */
418 max5data(data
->progress
.uploaded
, max5
[1]), /* xfer size */
419 max5data(data
->progress
.dlspeed
, max5
[3]), /* avrg dl speed */
420 max5data(data
->progress
.ulspeed
, max5
[4]), /* avrg ul speed */
421 time_total
, /* 8 letters */ /* total time */
422 time_spent
, /* 8 letters */ /* time spent */
423 time_left
, /* 8 letters */ /* time left */
424 max5data(data
->progress
.current_speed
, max5
[5]) /* current speed */
427 /* we flush the output stream to make it appear as soon as possible */
428 fflush(data
->set
.err
);
430 } /* !(data->progress.flags & PGRS_HIDE) */