1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * $Id: progress.c,v 1.92 2008-10-11 01:56:04 yangtse Exp $
22 ***************************************************************************/
23
24 #include "setup.h"
25
26 #include "urldata.h"
27 #include "sendf.h"
28 #include "progress.h"
29
30 #define _MPRINTF_REPLACE /* use our functions only */
31 #include <curl/mprintf.h>
32
33 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
34 byte) */
time2str(char * r,curl_off_t seconds)35 static void time2str(char *r, curl_off_t seconds)
36 {
37 curl_off_t d, h, m, s;
38 if(seconds <= 0) {
39 strcpy(r, "--:--:--");
40 return;
41 }
42 h = seconds / CURL_OFF_T_C(3600);
43 if(h <= CURL_OFF_T_C(99)) {
44 m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
45 s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
46 snprintf(r, 9, "%2" FORMAT_OFF_T ":%02" FORMAT_OFF_T ":%02" FORMAT_OFF_T,
47 h, m, s);
48 }
49 else {
50 /* this equals to more than 99 hours, switch to a more suitable output
51 format to fit within the limits. */
52 d = seconds / CURL_OFF_T_C(86400);
53 h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
54 if(d <= CURL_OFF_T_C(999))
55 snprintf(r, 9, "%3" FORMAT_OFF_T "d %02" FORMAT_OFF_T "h", d, h);
56 else
57 snprintf(r, 9, "%7" FORMAT_OFF_T "d", d);
58 }
59 }
60
61 /* The point of this function would be to return a string of the input data,
62 but never longer than 5 columns (+ one zero byte).
63 Add suffix k, M, G when suitable... */
max5data(curl_off_t bytes,char * max5)64 static char *max5data(curl_off_t bytes, char *max5)
65 {
66 #define ONE_KILOBYTE CURL_OFF_T_C(1024)
67 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
68 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
69 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
70 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
71
72 if(bytes < CURL_OFF_T_C(100000))
73 snprintf(max5, 6, "%5" FORMAT_OFF_T, bytes);
74
75 else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
76 snprintf(max5, 6, "%4" FORMAT_OFF_T "k", bytes/ONE_KILOBYTE);
77
78 else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
79 /* 'XX.XM' is good as long as we're less than 100 megs */
80 snprintf(max5, 6, "%2" FORMAT_OFF_T ".%0" FORMAT_OFF_T "M",
81 bytes/ONE_MEGABYTE,
82 (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
83
84 #if (CURL_SIZEOF_CURL_OFF_T > 4)
85
86 else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
87 /* 'XXXXM' is good until we're at 10000MB or above */
88 snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
89
90 else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
91 /* 10000 MB - 100 GB, we show it as XX.XG */
92 snprintf(max5, 6, "%2" FORMAT_OFF_T ".%0" FORMAT_OFF_T "G",
93 bytes/ONE_GIGABYTE,
94 (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
95
96 else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
97 /* up to 10000GB, display without decimal: XXXXG */
98 snprintf(max5, 6, "%4" FORMAT_OFF_T "G", bytes/ONE_GIGABYTE);
99
100 else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
101 /* up to 10000TB, display without decimal: XXXXT */
102 snprintf(max5, 6, "%4" FORMAT_OFF_T "T", bytes/ONE_TERABYTE);
103
104 else
105 /* up to 10000PB, display without decimal: XXXXP */
106 snprintf(max5, 6, "%4" FORMAT_OFF_T "P", bytes/ONE_PETABYTE);
107
108 /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
109 can hold, but our data type is signed so 8192PB will be the maximum. */
110
111 #else
112
113 else
114 snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
115
116 #endif
117
118 return max5;
119 }
120
121 /*
122
123 New proposed interface, 9th of February 2000:
124
125 pgrsStartNow() - sets start time
126 pgrsSetDownloadSize(x) - known expected download size
127 pgrsSetUploadSize(x) - known expected upload size
128 pgrsSetDownloadCounter() - amount of data currently downloaded
129 pgrsSetUploadCounter() - amount of data currently uploaded
130 pgrsUpdate() - show progress
131 pgrsDone() - transfer complete
132
133 */
134
Curl_pgrsDone(struct connectdata * conn)135 void Curl_pgrsDone(struct connectdata *conn)
136 {
137 struct SessionHandle *data = conn->data;
138 data->progress.lastshow=0;
139 Curl_pgrsUpdate(conn); /* the final (forced) update */
140
141 data->progress.speeder_c = 0; /* reset the progress meter display */
142 }
143
144 /* reset all times except redirect */
Curl_pgrsResetTimes(struct SessionHandle * data)145 void Curl_pgrsResetTimes(struct SessionHandle *data)
146 {
147 data->progress.t_nslookup = 0.0;
148 data->progress.t_connect = 0.0;
149 data->progress.t_pretransfer = 0.0;
150 data->progress.t_starttransfer = 0.0;
151 }
152
Curl_pgrsTime(struct SessionHandle * data,timerid timer)153 void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
154 {
155 switch(timer) {
156 default:
157 case TIMER_NONE:
158 /* mistake filter */
159 break;
160 case TIMER_STARTSINGLE:
161 /* This is set at the start of a single fetch */
162 data->progress.t_startsingle = Curl_tvnow();
163 break;
164
165 case TIMER_NAMELOOKUP:
166 data->progress.t_nslookup =
167 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
168 break;
169 case TIMER_CONNECT:
170 data->progress.t_connect =
171 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
172 break;
173 case TIMER_APPCONNECT:
174 data->progress.t_appconnect =
175 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
176 break;
177 case TIMER_PRETRANSFER:
178 data->progress.t_pretransfer =
179 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
180 break;
181 case TIMER_STARTTRANSFER:
182 data->progress.t_starttransfer =
183 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
184 break;
185 case TIMER_POSTRANSFER:
186 /* this is the normal end-of-transfer thing */
187 break;
188 case TIMER_REDIRECT:
189 data->progress.t_redirect =
190 Curl_tvdiff_secs(Curl_tvnow(), data->progress.start);
191 break;
192 }
193 }
194
Curl_pgrsStartNow(struct SessionHandle * data)195 void Curl_pgrsStartNow(struct SessionHandle *data)
196 {
197 data->progress.speeder_c = 0; /* reset the progress meter display */
198 data->progress.start = Curl_tvnow();
199 }
200
Curl_pgrsSetDownloadCounter(struct SessionHandle * data,curl_off_t size)201 void Curl_pgrsSetDownloadCounter(struct SessionHandle *data, curl_off_t size)
202 {
203 data->progress.downloaded = size;
204 }
205
Curl_pgrsSetUploadCounter(struct SessionHandle * data,curl_off_t size)206 void Curl_pgrsSetUploadCounter(struct SessionHandle *data, curl_off_t size)
207 {
208 data->progress.uploaded = size;
209 }
210
Curl_pgrsSetDownloadSize(struct SessionHandle * data,curl_off_t size)211 void Curl_pgrsSetDownloadSize(struct SessionHandle *data, curl_off_t size)
212 {
213 data->progress.size_dl = size;
214 if(size > 0)
215 data->progress.flags |= PGRS_DL_SIZE_KNOWN;
216 else
217 data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
218 }
219
Curl_pgrsSetUploadSize(struct SessionHandle * data,curl_off_t size)220 void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size)
221 {
222 data->progress.size_ul = size;
223 if(size > 0)
224 data->progress.flags |= PGRS_UL_SIZE_KNOWN;
225 else
226 data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
227 }
228
Curl_pgrsUpdate(struct connectdata * conn)229 int Curl_pgrsUpdate(struct connectdata *conn)
230 {
231 struct timeval now;
232 int result;
233 char max5[6][10];
234 curl_off_t dlpercen=0;
235 curl_off_t ulpercen=0;
236 curl_off_t total_percen=0;
237 curl_off_t total_transfer;
238 curl_off_t total_expected_transfer;
239 curl_off_t timespent;
240 struct SessionHandle *data = conn->data;
241 int nowindex = data->progress.speeder_c% CURR_TIME;
242 int checkindex;
243 int countindex; /* amount of seconds stored in the speeder array */
244 char time_left[10];
245 char time_total[10];
246 char time_spent[10];
247 curl_off_t ulestimate=0;
248 curl_off_t dlestimate=0;
249 curl_off_t total_estimate;
250 bool shownow=FALSE;
251
252 now = Curl_tvnow(); /* what time is it */
253
254 /* The time spent so far (from the start) */
255 data->progress.timespent =
256 (double)(now.tv_sec - data->progress.start.tv_sec) +
257 (double)(now.tv_usec - data->progress.start.tv_usec)/1000000.0;
258 timespent = (curl_off_t)data->progress.timespent;
259
260 /* The average download speed this far */
261 data->progress.dlspeed = (curl_off_t)
262 ((double)data->progress.downloaded/
263 (data->progress.timespent>0?data->progress.timespent:1));
264
265 /* The average upload speed this far */
266 data->progress.ulspeed = (curl_off_t)
267 ((double)data->progress.uploaded/
268 (data->progress.timespent>0?data->progress.timespent:1));
269
270 /* Calculations done at most once a second, unless end is reached */
271 if(data->progress.lastshow != (long)now.tv_sec) {
272 shownow = TRUE;
273
274 data->progress.lastshow = now.tv_sec;
275
276 /* Let's do the "current speed" thing, which should use the fastest
277 of the dl/ul speeds. Store the faster speed at entry 'nowindex'. */
278 data->progress.speeder[ nowindex ] =
279 data->progress.downloaded>data->progress.uploaded?
280 data->progress.downloaded:data->progress.uploaded;
281
282 /* remember the exact time for this moment */
283 data->progress.speeder_time [ nowindex ] = now;
284
285 /* advance our speeder_c counter, which is increased every time we get
286 here and we expect it to never wrap as 2^32 is a lot of seconds! */
287 data->progress.speeder_c++;
288
289 /* figure out how many index entries of data we have stored in our speeder
290 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
291 transfer. Imagine, after one second we have filled in two entries,
292 after two seconds we've filled in three entries etc. */
293 countindex = ((data->progress.speeder_c>=CURR_TIME)?
294 CURR_TIME:data->progress.speeder_c) - 1;
295
296 /* first of all, we don't do this if there's no counted seconds yet */
297 if(countindex) {
298 long span_ms;
299
300 /* Get the index position to compare with the 'nowindex' position.
301 Get the oldest entry possible. While we have less than CURR_TIME
302 entries, the first entry will remain the oldest. */
303 checkindex = (data->progress.speeder_c>=CURR_TIME)?
304 data->progress.speeder_c%CURR_TIME:0;
305
306 /* Figure out the exact time for the time span */
307 span_ms = Curl_tvdiff(now,
308 data->progress.speeder_time[checkindex]);
309 if(0 == span_ms)
310 span_ms=1; /* at least one millisecond MUST have passed */
311
312 /* Calculate the average speed the last 'span_ms' milliseconds */
313 {
314 curl_off_t amount = data->progress.speeder[nowindex]-
315 data->progress.speeder[checkindex];
316
317 if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
318 /* the 'amount' value is bigger than would fit in 32 bits if
319 multiplied with 1000, so we use the double math for this */
320 data->progress.current_speed = (curl_off_t)
321 ((double)amount/((double)span_ms/1000.0));
322 else
323 /* the 'amount' value is small enough to fit within 32 bits even
324 when multiplied with 1000 */
325 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
326 }
327 }
328 else
329 /* the first second we use the main average */
330 data->progress.current_speed =
331 (data->progress.ulspeed>data->progress.dlspeed)?
332 data->progress.ulspeed:data->progress.dlspeed;
333
334 } /* Calculations end */
335
336 if(!(data->progress.flags & PGRS_HIDE)) {
337
338 /* progress meter has not been shut off */
339
340 if(data->set.fprogress) {
341 /* There's a callback set, so we call that instead of writing
342 anything ourselves. This really is the way to go. */
343 result= data->set.fprogress(data->set.progress_client,
344 (double)data->progress.size_dl,
345 (double)data->progress.downloaded,
346 (double)data->progress.size_ul,
347 (double)data->progress.uploaded);
348 if(result)
349 failf(data, "Callback aborted");
350 return result;
351 }
352
353 if(!shownow)
354 /* only show the internal progress meter once per second */
355 return 0;
356
357 /* If there's no external callback set, use internal code to show
358 progress */
359
360 if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
361 if(data->state.resume_from) {
362 fprintf(data->set.err,
363 "** Resuming transfer from byte position %" FORMAT_OFF_T "\n",
364 data->state.resume_from);
365 }
366 fprintf(data->set.err,
367 " %% Total %% Received %% Xferd Average Speed Time Time Time Current\n"
368 " Dload Upload Total Spent Left Speed\n");
369 data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
370 }
371
372 /* Figure out the estimated time of arrival for the upload */
373 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
374 (data->progress.ulspeed > CURL_OFF_T_C(0)) &&
375 (data->progress.size_ul > CURL_OFF_T_C(100)) ) {
376 ulestimate = data->progress.size_ul / data->progress.ulspeed;
377 ulpercen = data->progress.uploaded /
378 (data->progress.size_ul/CURL_OFF_T_C(100));
379 }
380
381 /* ... and the download */
382 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
383 (data->progress.dlspeed > CURL_OFF_T_C(0)) &&
384 (data->progress.size_dl > CURL_OFF_T_C(100))) {
385 dlestimate = data->progress.size_dl / data->progress.dlspeed;
386 dlpercen = data->progress.downloaded /
387 (data->progress.size_dl/CURL_OFF_T_C(100));
388 }
389
390 /* Now figure out which of them is slower and use that one for the
391 total estimate! */
392 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
393
394 /* create the three time strings */
395 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
396 time2str(time_total, total_estimate);
397 time2str(time_spent, timespent);
398
399 /* Get the total amount of data expected to get transfered */
400 total_expected_transfer =
401 (data->progress.flags & PGRS_UL_SIZE_KNOWN?
402 data->progress.size_ul:data->progress.uploaded)+
403 (data->progress.flags & PGRS_DL_SIZE_KNOWN?
404 data->progress.size_dl:data->progress.downloaded);
405
406 /* We have transfered this much so far */
407 total_transfer = data->progress.downloaded + data->progress.uploaded;
408
409 /* Get the percentage of data transfered so far */
410 if(total_expected_transfer > CURL_OFF_T_C(100))
411 total_percen = total_transfer /
412 (total_expected_transfer/CURL_OFF_T_C(100));
413
414 fprintf(data->set.err,
415 "\r"
416 "%3" FORMAT_OFF_T " %s "
417 "%3" FORMAT_OFF_T " %s "
418 "%3" FORMAT_OFF_T " %s %s %s %s %s %s %s",
419 total_percen, /* 3 letters */ /* total % */
420 max5data(total_expected_transfer, max5[2]), /* total size */
421 dlpercen, /* 3 letters */ /* rcvd % */
422 max5data(data->progress.downloaded, max5[0]), /* rcvd size */
423 ulpercen, /* 3 letters */ /* xfer % */
424 max5data(data->progress.uploaded, max5[1]), /* xfer size */
425 max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
426 max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
427 time_total, /* 8 letters */ /* total time */
428 time_spent, /* 8 letters */ /* time spent */
429 time_left, /* 8 letters */ /* time left */
430 max5data(data->progress.current_speed, max5[5]) /* current speed */
431 );
432
433 /* we flush the output stream to make it appear as soon as possible */
434 fflush(data->set.err);
435
436 } /* !(data->progress.flags & PGRS_HIDE) */
437
438 return 0;
439 }
440