1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
29 #define _MPRINTF_REPLACE /* use our functions only */
30 #include <curl/mprintf.h>
32 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
34 static void time2str(char *r, curl_off_t seconds)
36 curl_off_t d, h, m, s;
38 strcpy(r, "--:--:--");
41 h = seconds / CURL_OFF_T_C(3600);
42 if(h <= CURL_OFF_T_C(99)) {
43 m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
44 s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
45 snprintf(r, 9, "%2" FORMAT_OFF_T ":%02" FORMAT_OFF_T ":%02" FORMAT_OFF_T,
49 /* this equals to more than 99 hours, switch to a more suitable output
50 format to fit within the limits. */
51 d = seconds / CURL_OFF_T_C(86400);
52 h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
53 if(d <= CURL_OFF_T_C(999))
54 snprintf(r, 9, "%3" FORMAT_OFF_T "d %02" FORMAT_OFF_T "h", d, h);
56 snprintf(r, 9, "%7" FORMAT_OFF_T "d", d);
60 /* The point of this function would be to return a string of the input data,
61 but never longer than 5 columns (+ one zero byte).
62 Add suffix k, M, G when suitable... */
63 static char *max5data(curl_off_t bytes, char *max5)
65 #define ONE_KILOBYTE CURL_OFF_T_C(1024)
66 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
67 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
68 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
69 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
71 if(bytes < CURL_OFF_T_C(100000))
72 snprintf(max5, 6, "%5" FORMAT_OFF_T, bytes);
74 else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
75 snprintf(max5, 6, "%4" FORMAT_OFF_T "k", bytes/ONE_KILOBYTE);
77 else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
78 /* 'XX.XM' is good as long as we're less than 100 megs */
79 snprintf(max5, 6, "%2" FORMAT_OFF_T ".%0" FORMAT_OFF_T "M",
81 (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
83 #if (CURL_SIZEOF_CURL_OFF_T > 4)
85 else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
86 /* 'XXXXM' is good until we're at 10000MB or above */
87 snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
89 else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
90 /* 10000 MB - 100 GB, we show it as XX.XG */
91 snprintf(max5, 6, "%2" FORMAT_OFF_T ".%0" FORMAT_OFF_T "G",
93 (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
95 else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
96 /* up to 10000GB, display without decimal: XXXXG */
97 snprintf(max5, 6, "%4" FORMAT_OFF_T "G", bytes/ONE_GIGABYTE);
99 else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
100 /* up to 10000TB, display without decimal: XXXXT */
101 snprintf(max5, 6, "%4" FORMAT_OFF_T "T", bytes/ONE_TERABYTE);
104 /* up to 10000PB, display without decimal: XXXXP */
105 snprintf(max5, 6, "%4" FORMAT_OFF_T "P", bytes/ONE_PETABYTE);
107 /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
108 can hold, but our data type is signed so 8192PB will be the maximum. */
113 snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
122 New proposed interface, 9th of February 2000:
124 pgrsStartNow() - sets start time
125 pgrsSetDownloadSize(x) - known expected download size
126 pgrsSetUploadSize(x) - known expected upload size
127 pgrsSetDownloadCounter() - amount of data currently downloaded
128 pgrsSetUploadCounter() - amount of data currently uploaded
129 pgrsUpdate() - show progress
130 pgrsDone() - transfer complete
134 void Curl_pgrsDone(struct connectdata *conn)
136 struct SessionHandle *data = conn->data;
137 data->progress.lastshow=0;
138 Curl_pgrsUpdate(conn); /* the final (forced) update */
140 if(!(data->progress.flags & PGRS_HIDE) &&
141 !data->progress.callback)
142 /* only output if we don't use a progress callback and we're not
144 fprintf(data->set.err, "\n");
146 data->progress.speeder_c = 0; /* reset the progress meter display */
149 /* reset all times except redirect */
150 void Curl_pgrsResetTimes(struct SessionHandle *data)
152 data->progress.t_nslookup = 0.0;
153 data->progress.t_connect = 0.0;
154 data->progress.t_pretransfer = 0.0;
155 data->progress.t_starttransfer = 0.0;
158 void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
160 struct timeval now = Curl_tvnow();
167 case TIMER_STARTSINGLE:
168 /* This is set at the start of a single fetch */
169 data->progress.t_startsingle = now;
172 case TIMER_STARTACCEPT:
173 data->progress.t_acceptdata = Curl_tvnow();
176 case TIMER_NAMELOOKUP:
177 data->progress.t_nslookup =
178 Curl_tvdiff_secs(now, data->progress.t_startsingle);
181 data->progress.t_connect =
182 Curl_tvdiff_secs(now, data->progress.t_startsingle);
184 case TIMER_APPCONNECT:
185 data->progress.t_appconnect =
186 Curl_tvdiff_secs(now, data->progress.t_startsingle);
188 case TIMER_PRETRANSFER:
189 data->progress.t_pretransfer =
190 Curl_tvdiff_secs(now, data->progress.t_startsingle);
192 case TIMER_STARTTRANSFER:
193 data->progress.t_starttransfer =
194 Curl_tvdiff_secs(now, data->progress.t_startsingle);
196 case TIMER_POSTRANSFER:
197 /* this is the normal end-of-transfer thing */
200 data->progress.t_redirect = Curl_tvdiff_secs(now, data->progress.start);
205 void Curl_pgrsStartNow(struct SessionHandle *data)
207 data->progress.speeder_c = 0; /* reset the progress meter display */
208 data->progress.start = Curl_tvnow();
209 /* clear all bits except HIDE and HEADERS_OUT */
210 data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
213 void Curl_pgrsSetDownloadCounter(struct SessionHandle *data, curl_off_t size)
215 data->progress.downloaded = size;
218 void Curl_pgrsSetUploadCounter(struct SessionHandle *data, curl_off_t size)
220 data->progress.uploaded = size;
223 void Curl_pgrsSetDownloadSize(struct SessionHandle *data, curl_off_t size)
225 data->progress.size_dl = size;
227 data->progress.flags |= PGRS_DL_SIZE_KNOWN;
229 data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
232 void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size)
234 data->progress.size_ul = size;
236 data->progress.flags |= PGRS_UL_SIZE_KNOWN;
238 data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
241 int Curl_pgrsUpdate(struct connectdata *conn)
246 curl_off_t dlpercen=0;
247 curl_off_t ulpercen=0;
248 curl_off_t total_percen=0;
249 curl_off_t total_transfer;
250 curl_off_t total_expected_transfer;
251 curl_off_t timespent;
252 struct SessionHandle *data = conn->data;
253 int nowindex = data->progress.speeder_c% CURR_TIME;
255 int countindex; /* amount of seconds stored in the speeder array */
259 curl_off_t ulestimate=0;
260 curl_off_t dlestimate=0;
261 curl_off_t total_estimate;
264 now = Curl_tvnow(); /* what time is it */
266 /* The time spent so far (from the start) */
267 data->progress.timespent =
268 (double)(now.tv_sec - data->progress.start.tv_sec) +
269 (double)(now.tv_usec - data->progress.start.tv_usec)/1000000.0;
270 timespent = (curl_off_t)data->progress.timespent;
272 /* The average download speed this far */
273 data->progress.dlspeed = (curl_off_t)
274 ((double)data->progress.downloaded/
275 (data->progress.timespent>0?data->progress.timespent:1));
277 /* The average upload speed this far */
278 data->progress.ulspeed = (curl_off_t)
279 ((double)data->progress.uploaded/
280 (data->progress.timespent>0?data->progress.timespent:1));
282 /* Calculations done at most once a second, unless end is reached */
283 if(data->progress.lastshow != (long)now.tv_sec) {
286 data->progress.lastshow = now.tv_sec;
288 /* Let's do the "current speed" thing, which should use the fastest
289 of the dl/ul speeds. Store the faster speed at entry 'nowindex'. */
290 data->progress.speeder[ nowindex ] =
291 data->progress.downloaded>data->progress.uploaded?
292 data->progress.downloaded:data->progress.uploaded;
294 /* remember the exact time for this moment */
295 data->progress.speeder_time [ nowindex ] = now;
297 /* advance our speeder_c counter, which is increased every time we get
298 here and we expect it to never wrap as 2^32 is a lot of seconds! */
299 data->progress.speeder_c++;
301 /* figure out how many index entries of data we have stored in our speeder
302 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
303 transfer. Imagine, after one second we have filled in two entries,
304 after two seconds we've filled in three entries etc. */
305 countindex = ((data->progress.speeder_c>=CURR_TIME)?
306 CURR_TIME:data->progress.speeder_c) - 1;
308 /* first of all, we don't do this if there's no counted seconds yet */
312 /* Get the index position to compare with the 'nowindex' position.
313 Get the oldest entry possible. While we have less than CURR_TIME
314 entries, the first entry will remain the oldest. */
315 checkindex = (data->progress.speeder_c>=CURR_TIME)?
316 data->progress.speeder_c%CURR_TIME:0;
318 /* Figure out the exact time for the time span */
319 span_ms = Curl_tvdiff(now,
320 data->progress.speeder_time[checkindex]);
322 span_ms=1; /* at least one millisecond MUST have passed */
324 /* Calculate the average speed the last 'span_ms' milliseconds */
326 curl_off_t amount = data->progress.speeder[nowindex]-
327 data->progress.speeder[checkindex];
329 if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
330 /* the 'amount' value is bigger than would fit in 32 bits if
331 multiplied with 1000, so we use the double math for this */
332 data->progress.current_speed = (curl_off_t)
333 ((double)amount/((double)span_ms/1000.0));
335 /* the 'amount' value is small enough to fit within 32 bits even
336 when multiplied with 1000 */
337 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
341 /* the first second we use the main average */
342 data->progress.current_speed =
343 (data->progress.ulspeed>data->progress.dlspeed)?
344 data->progress.ulspeed:data->progress.dlspeed;
346 } /* Calculations end */
348 if(!(data->progress.flags & PGRS_HIDE)) {
350 /* progress meter has not been shut off */
352 if(data->set.fprogress) {
353 /* There's a callback set, so we call that instead of writing
354 anything ourselves. This really is the way to go. */
355 result= data->set.fprogress(data->set.progress_client,
356 (double)data->progress.size_dl,
357 (double)data->progress.downloaded,
358 (double)data->progress.size_ul,
359 (double)data->progress.uploaded);
361 failf(data, "Callback aborted");
366 /* only show the internal progress meter once per second */
369 /* If there's no external callback set, use internal code to show
372 if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
373 if(data->state.resume_from) {
374 fprintf(data->set.err,
375 "** Resuming transfer from byte position %" FORMAT_OFF_T "\n",
376 data->state.resume_from);
378 fprintf(data->set.err,
379 " %% Total %% Received %% Xferd Average Speed "
380 "Time Time Time Current\n"
382 "Total Spent Left Speed\n");
383 data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
386 /* Figure out the estimated time of arrival for the upload */
387 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
388 (data->progress.ulspeed > CURL_OFF_T_C(0))) {
389 ulestimate = data->progress.size_ul / data->progress.ulspeed;
391 if(data->progress.size_ul > CURL_OFF_T_C(10000))
392 ulpercen = data->progress.uploaded /
393 (data->progress.size_ul/CURL_OFF_T_C(100));
394 else if(data->progress.size_ul > CURL_OFF_T_C(0))
395 ulpercen = (data->progress.uploaded*100) /
396 data->progress.size_ul;
399 /* ... and the download */
400 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
401 (data->progress.dlspeed > CURL_OFF_T_C(0))) {
402 dlestimate = data->progress.size_dl / data->progress.dlspeed;
404 if(data->progress.size_dl > CURL_OFF_T_C(10000))
405 dlpercen = data->progress.downloaded /
406 (data->progress.size_dl/CURL_OFF_T_C(100));
407 else if(data->progress.size_dl > CURL_OFF_T_C(0))
408 dlpercen = (data->progress.downloaded*100) /
409 data->progress.size_dl;
412 /* Now figure out which of them is slower and use that one for the
414 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
416 /* create the three time strings */
417 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
418 time2str(time_total, total_estimate);
419 time2str(time_spent, timespent);
421 /* Get the total amount of data expected to get transferred */
422 total_expected_transfer =
423 (data->progress.flags & PGRS_UL_SIZE_KNOWN?
424 data->progress.size_ul:data->progress.uploaded)+
425 (data->progress.flags & PGRS_DL_SIZE_KNOWN?
426 data->progress.size_dl:data->progress.downloaded);
428 /* We have transferred this much so far */
429 total_transfer = data->progress.downloaded + data->progress.uploaded;
431 /* Get the percentage of data transferred so far */
432 if(total_expected_transfer > CURL_OFF_T_C(10000))
433 total_percen = total_transfer /
434 (total_expected_transfer/CURL_OFF_T_C(100));
435 else if(total_expected_transfer > CURL_OFF_T_C(0))
436 total_percen = (total_transfer*100) / total_expected_transfer;
438 fprintf(data->set.err,
440 "%3" FORMAT_OFF_T " %s "
441 "%3" FORMAT_OFF_T " %s "
442 "%3" FORMAT_OFF_T " %s %s %s %s %s %s %s",
443 total_percen, /* 3 letters */ /* total % */
444 max5data(total_expected_transfer, max5[2]), /* total size */
445 dlpercen, /* 3 letters */ /* rcvd % */
446 max5data(data->progress.downloaded, max5[0]), /* rcvd size */
447 ulpercen, /* 3 letters */ /* xfer % */
448 max5data(data->progress.uploaded, max5[1]), /* xfer size */
449 max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
450 max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
451 time_total, /* 8 letters */ /* total time */
452 time_spent, /* 8 letters */ /* time spent */
453 time_left, /* 8 letters */ /* time left */
454 max5data(data->progress.current_speed, max5[5]) /* current speed */
457 /* we flush the output stream to make it appear as soon as possible */
458 fflush(data->set.err);
460 } /* !(data->progress.flags & PGRS_HIDE) */