dac7f8d6eca65de75acb065bb182135d3cc23e66
[platform/upstream/curl.git] / lib / progress.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, 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  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #include "urldata.h"
26 #include "sendf.h"
27 #include "progress.h"
28
29 #define _MPRINTF_REPLACE /* use our functions only */
30 #include <curl/mprintf.h>
31
32 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
33    byte) */
34 static void time2str(char *r, curl_off_t seconds)
35 {
36   curl_off_t d, h, m, s;
37   if(seconds <= 0) {
38     strcpy(r, "--:--:--");
39     return;
40   }
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,
46              h, m, s);
47   }
48   else {
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);
55     else
56       snprintf(r, 9, "%7" FORMAT_OFF_T "d", d);
57   }
58 }
59
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)
64 {
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)
70
71   if(bytes < CURL_OFF_T_C(100000))
72     snprintf(max5, 6, "%5" FORMAT_OFF_T, bytes);
73
74   else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
75     snprintf(max5, 6, "%4" FORMAT_OFF_T "k", bytes/ONE_KILOBYTE);
76
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",
80               bytes/ONE_MEGABYTE,
81              (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
82
83 #if (CURL_SIZEOF_CURL_OFF_T > 4)
84
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);
88
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",
92               bytes/ONE_GIGABYTE,
93              (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
94
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);
98
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);
102
103   else
104     /* up to 10000PB, display without decimal: XXXXP */
105     snprintf(max5, 6, "%4" FORMAT_OFF_T "P", bytes/ONE_PETABYTE);
106
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. */
109
110 #else
111
112   else
113     snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
114
115 #endif
116
117   return max5;
118 }
119
120 /*
121
122    New proposed interface, 9th of February 2000:
123
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
131
132 */
133
134 int Curl_pgrsDone(struct connectdata *conn)
135 {
136   int rc;
137   struct SessionHandle *data = conn->data;
138   data->progress.lastshow=0;
139   rc = Curl_pgrsUpdate(conn); /* the final (forced) update */
140   if(rc)
141     return rc;
142
143   if(!(data->progress.flags & PGRS_HIDE) &&
144      !data->progress.callback)
145     /* only output if we don't use a progress callback and we're not
146      * hidden */
147     fprintf(data->set.err, "\n");
148
149   data->progress.speeder_c = 0; /* reset the progress meter display */
150   return 0;
151 }
152
153 /* reset all times except redirect, and reset the known transfer sizes */
154 void Curl_pgrsResetTimesSizes(struct SessionHandle *data)
155 {
156   data->progress.t_nslookup = 0.0;
157   data->progress.t_connect = 0.0;
158   data->progress.t_pretransfer = 0.0;
159   data->progress.t_starttransfer = 0.0;
160
161   Curl_pgrsSetDownloadSize(data, 0);
162   Curl_pgrsSetUploadSize(data, 0);
163 }
164
165 void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
166 {
167   struct timeval now = Curl_tvnow();
168
169   switch(timer) {
170   default:
171   case TIMER_NONE:
172     /* mistake filter */
173     break;
174   case TIMER_STARTSINGLE:
175     /* This is set at the start of a single fetch */
176     data->progress.t_startsingle = now;
177     break;
178
179   case TIMER_STARTACCEPT:
180     data->progress.t_acceptdata = Curl_tvnow();
181     break;
182
183   case TIMER_NAMELOOKUP:
184     data->progress.t_nslookup =
185       Curl_tvdiff_secs(now, data->progress.t_startsingle);
186     break;
187   case TIMER_CONNECT:
188     data->progress.t_connect =
189       Curl_tvdiff_secs(now, data->progress.t_startsingle);
190     break;
191   case TIMER_APPCONNECT:
192     data->progress.t_appconnect =
193       Curl_tvdiff_secs(now, data->progress.t_startsingle);
194     break;
195   case TIMER_PRETRANSFER:
196     data->progress.t_pretransfer =
197       Curl_tvdiff_secs(now, data->progress.t_startsingle);
198     break;
199   case TIMER_STARTTRANSFER:
200     data->progress.t_starttransfer =
201       Curl_tvdiff_secs(now, data->progress.t_startsingle);
202     break;
203   case TIMER_POSTRANSFER:
204     /* this is the normal end-of-transfer thing */
205     break;
206   case TIMER_REDIRECT:
207     data->progress.t_redirect = Curl_tvdiff_secs(now, data->progress.start);
208     break;
209   }
210 }
211
212 void Curl_pgrsStartNow(struct SessionHandle *data)
213 {
214   data->progress.speeder_c = 0; /* reset the progress meter display */
215   data->progress.start = Curl_tvnow();
216   /* clear all bits except HIDE and HEADERS_OUT */
217   data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
218 }
219
220 void Curl_pgrsSetDownloadCounter(struct SessionHandle *data, curl_off_t size)
221 {
222   data->progress.downloaded = size;
223 }
224
225 void Curl_pgrsSetUploadCounter(struct SessionHandle *data, curl_off_t size)
226 {
227   data->progress.uploaded = size;
228 }
229
230 void Curl_pgrsSetDownloadSize(struct SessionHandle *data, curl_off_t size)
231 {
232   data->progress.size_dl = size;
233   if(size >= 0)
234     data->progress.flags |= PGRS_DL_SIZE_KNOWN;
235   else
236     data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
237 }
238
239 void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size)
240 {
241   data->progress.size_ul = size;
242   if(size >= 0)
243     data->progress.flags |= PGRS_UL_SIZE_KNOWN;
244   else
245     data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
246 }
247
248 /*
249  * Curl_pgrsUpdate() returns 0 for success or the value returned by the
250  * progress callback!
251  */
252 int Curl_pgrsUpdate(struct connectdata *conn)
253 {
254   struct timeval now;
255   int result;
256   char max5[6][10];
257   curl_off_t dlpercen=0;
258   curl_off_t ulpercen=0;
259   curl_off_t total_percen=0;
260   curl_off_t total_transfer;
261   curl_off_t total_expected_transfer;
262   curl_off_t timespent;
263   struct SessionHandle *data = conn->data;
264   int nowindex = data->progress.speeder_c% CURR_TIME;
265   int checkindex;
266   int countindex; /* amount of seconds stored in the speeder array */
267   char time_left[10];
268   char time_total[10];
269   char time_spent[10];
270   curl_off_t ulestimate=0;
271   curl_off_t dlestimate=0;
272   curl_off_t total_estimate;
273   bool shownow=FALSE;
274
275   now = Curl_tvnow(); /* what time is it */
276
277   /* The time spent so far (from the start) */
278   data->progress.timespent =
279     (double)(now.tv_sec - data->progress.start.tv_sec) +
280     (double)(now.tv_usec - data->progress.start.tv_usec)/1000000.0;
281   timespent = (curl_off_t)data->progress.timespent;
282
283   /* The average download speed this far */
284   data->progress.dlspeed = (curl_off_t)
285     ((double)data->progress.downloaded/
286      (data->progress.timespent>0?data->progress.timespent:1));
287
288   /* The average upload speed this far */
289   data->progress.ulspeed = (curl_off_t)
290     ((double)data->progress.uploaded/
291      (data->progress.timespent>0?data->progress.timespent:1));
292
293   /* Calculations done at most once a second, unless end is reached */
294   if(data->progress.lastshow != (long)now.tv_sec) {
295     shownow = TRUE;
296
297     data->progress.lastshow = now.tv_sec;
298
299     /* Let's do the "current speed" thing, which should use the fastest
300        of the dl/ul speeds. Store the faster speed at entry 'nowindex'. */
301     data->progress.speeder[ nowindex ] =
302       data->progress.downloaded>data->progress.uploaded?
303       data->progress.downloaded:data->progress.uploaded;
304
305     /* remember the exact time for this moment */
306     data->progress.speeder_time [ nowindex ] = now;
307
308     /* advance our speeder_c counter, which is increased every time we get
309        here and we expect it to never wrap as 2^32 is a lot of seconds! */
310     data->progress.speeder_c++;
311
312     /* figure out how many index entries of data we have stored in our speeder
313        array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
314        transfer. Imagine, after one second we have filled in two entries,
315        after two seconds we've filled in three entries etc. */
316     countindex = ((data->progress.speeder_c>=CURR_TIME)?
317                   CURR_TIME:data->progress.speeder_c) - 1;
318
319     /* first of all, we don't do this if there's no counted seconds yet */
320     if(countindex) {
321       long span_ms;
322
323       /* Get the index position to compare with the 'nowindex' position.
324          Get the oldest entry possible. While we have less than CURR_TIME
325          entries, the first entry will remain the oldest. */
326       checkindex = (data->progress.speeder_c>=CURR_TIME)?
327         data->progress.speeder_c%CURR_TIME:0;
328
329       /* Figure out the exact time for the time span */
330       span_ms = Curl_tvdiff(now,
331                             data->progress.speeder_time[checkindex]);
332       if(0 == span_ms)
333         span_ms=1; /* at least one millisecond MUST have passed */
334
335       /* Calculate the average speed the last 'span_ms' milliseconds */
336       {
337         curl_off_t amount = data->progress.speeder[nowindex]-
338           data->progress.speeder[checkindex];
339
340         if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
341           /* the 'amount' value is bigger than would fit in 32 bits if
342              multiplied with 1000, so we use the double math for this */
343           data->progress.current_speed = (curl_off_t)
344             ((double)amount/((double)span_ms/1000.0));
345         else
346           /* the 'amount' value is small enough to fit within 32 bits even
347              when multiplied with 1000 */
348           data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
349       }
350     }
351     else
352       /* the first second we use the main average */
353       data->progress.current_speed =
354         (data->progress.ulspeed>data->progress.dlspeed)?
355         data->progress.ulspeed:data->progress.dlspeed;
356
357   } /* Calculations end */
358
359   if(!(data->progress.flags & PGRS_HIDE)) {
360     /* progress meter has not been shut off */
361
362     if(data->set.fxferinfo) {
363       /* There's a callback set, call that */
364       result= data->set.fxferinfo(data->set.progress_client,
365                                   data->progress.size_dl,
366                                   data->progress.downloaded,
367                                   data->progress.size_ul,
368                                   data->progress.uploaded);
369       if(result)
370         failf(data, "Callback aborted");
371       return result;
372     }
373     else if(data->set.fprogress) {
374       /* The older deprecated callback is set, call that */
375       result= data->set.fprogress(data->set.progress_client,
376                                   (double)data->progress.size_dl,
377                                   (double)data->progress.downloaded,
378                                   (double)data->progress.size_ul,
379                                   (double)data->progress.uploaded);
380       if(result)
381         failf(data, "Callback aborted");
382       return result;
383     }
384
385     if(!shownow)
386       /* only show the internal progress meter once per second */
387       return 0;
388
389     /* If there's no external callback set, use internal code to show
390        progress */
391
392     if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
393       if(data->state.resume_from) {
394         fprintf(data->set.err,
395                 "** Resuming transfer from byte position %" FORMAT_OFF_T "\n",
396                 data->state.resume_from);
397       }
398       fprintf(data->set.err,
399               "  %% Total    %% Received %% Xferd  Average Speed   "
400               "Time    Time     Time  Current\n"
401               "                                 Dload  Upload   "
402               "Total   Spent    Left  Speed\n");
403       data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
404     }
405
406     /* Figure out the estimated time of arrival for the upload */
407     if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
408        (data->progress.ulspeed > CURL_OFF_T_C(0))) {
409       ulestimate = data->progress.size_ul / data->progress.ulspeed;
410
411       if(data->progress.size_ul > CURL_OFF_T_C(10000))
412         ulpercen = data->progress.uploaded /
413           (data->progress.size_ul/CURL_OFF_T_C(100));
414       else if(data->progress.size_ul > CURL_OFF_T_C(0))
415         ulpercen = (data->progress.uploaded*100) /
416           data->progress.size_ul;
417     }
418
419     /* ... and the download */
420     if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
421        (data->progress.dlspeed > CURL_OFF_T_C(0))) {
422       dlestimate = data->progress.size_dl / data->progress.dlspeed;
423
424       if(data->progress.size_dl > CURL_OFF_T_C(10000))
425         dlpercen = data->progress.downloaded /
426           (data->progress.size_dl/CURL_OFF_T_C(100));
427       else if(data->progress.size_dl > CURL_OFF_T_C(0))
428         dlpercen = (data->progress.downloaded*100) /
429           data->progress.size_dl;
430     }
431
432     /* Now figure out which of them is slower and use that one for the
433        total estimate! */
434     total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
435
436     /* create the three time strings */
437     time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
438     time2str(time_total, total_estimate);
439     time2str(time_spent, timespent);
440
441     /* Get the total amount of data expected to get transferred */
442     total_expected_transfer =
443       (data->progress.flags & PGRS_UL_SIZE_KNOWN?
444        data->progress.size_ul:data->progress.uploaded)+
445       (data->progress.flags & PGRS_DL_SIZE_KNOWN?
446        data->progress.size_dl:data->progress.downloaded);
447
448     /* We have transferred this much so far */
449     total_transfer = data->progress.downloaded + data->progress.uploaded;
450
451     /* Get the percentage of data transferred so far */
452     if(total_expected_transfer > CURL_OFF_T_C(10000))
453       total_percen = total_transfer /
454         (total_expected_transfer/CURL_OFF_T_C(100));
455     else if(total_expected_transfer > CURL_OFF_T_C(0))
456       total_percen = (total_transfer*100) / total_expected_transfer;
457
458     fprintf(data->set.err,
459             "\r"
460             "%3" FORMAT_OFF_T " %s  "
461             "%3" FORMAT_OFF_T " %s  "
462             "%3" FORMAT_OFF_T " %s  %s  %s %s %s %s %s",
463             total_percen,  /* 3 letters */                /* total % */
464             max5data(total_expected_transfer, max5[2]),   /* total size */
465             dlpercen,      /* 3 letters */                /* rcvd % */
466             max5data(data->progress.downloaded, max5[0]), /* rcvd size */
467             ulpercen,      /* 3 letters */                /* xfer % */
468             max5data(data->progress.uploaded, max5[1]),   /* xfer size */
469             max5data(data->progress.dlspeed, max5[3]),    /* avrg dl speed */
470             max5data(data->progress.ulspeed, max5[4]),    /* avrg ul speed */
471             time_total,    /* 8 letters */                /* total time */
472             time_spent,    /* 8 letters */                /* time spent */
473             time_left,     /* 8 letters */                /* time left */
474             max5data(data->progress.current_speed, max5[5]) /* current speed */
475             );
476
477     /* we flush the output stream to make it appear as soon as possible */
478     fflush(data->set.err);
479
480   } /* !(data->progress.flags & PGRS_HIDE) */
481
482   return 0;
483 }